radeon_display.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953
  1. /*
  2. * Copyright 2007-8 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the "Software"),
  7. * to deal in the Software without restriction, including without limitation
  8. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  9. * and/or sell copies of the Software, and to permit persons to whom the
  10. * Software is furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  19. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  20. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. * OTHER DEALINGS IN THE SOFTWARE.
  22. *
  23. * Authors: Dave Airlie
  24. * Alex Deucher
  25. */
  26. #include <drm/drmP.h>
  27. #include <drm/radeon_drm.h>
  28. #include "radeon.h"
  29. #include "atom.h"
  30. #include <asm/div64.h>
  31. #include <linux/pm_runtime.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/drm_edid.h>
  34. #include <linux/gcd.h>
  35. static void avivo_crtc_load_lut(struct drm_crtc *crtc)
  36. {
  37. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  38. struct drm_device *dev = crtc->dev;
  39. struct radeon_device *rdev = dev->dev_private;
  40. int i;
  41. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  42. WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
  43. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  44. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  45. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  46. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  47. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  48. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  49. WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
  50. WREG32(AVIVO_DC_LUT_RW_MODE, 0);
  51. WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
  52. WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
  53. for (i = 0; i < 256; i++) {
  54. WREG32(AVIVO_DC_LUT_30_COLOR,
  55. (radeon_crtc->lut_r[i] << 20) |
  56. (radeon_crtc->lut_g[i] << 10) |
  57. (radeon_crtc->lut_b[i] << 0));
  58. }
  59. /* Only change bit 0 of LUT_SEL, other bits are set elsewhere */
  60. WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id, ~1);
  61. }
  62. static void dce4_crtc_load_lut(struct drm_crtc *crtc)
  63. {
  64. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  65. struct drm_device *dev = crtc->dev;
  66. struct radeon_device *rdev = dev->dev_private;
  67. int i;
  68. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  69. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  70. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  71. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  72. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  73. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  74. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  75. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  76. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  77. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  78. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  79. for (i = 0; i < 256; i++) {
  80. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  81. (radeon_crtc->lut_r[i] << 20) |
  82. (radeon_crtc->lut_g[i] << 10) |
  83. (radeon_crtc->lut_b[i] << 0));
  84. }
  85. }
  86. static void dce5_crtc_load_lut(struct drm_crtc *crtc)
  87. {
  88. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  89. struct drm_device *dev = crtc->dev;
  90. struct radeon_device *rdev = dev->dev_private;
  91. int i;
  92. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  93. WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  94. (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
  95. NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
  96. WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
  97. NI_GRPH_PRESCALE_BYPASS);
  98. WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
  99. NI_OVL_PRESCALE_BYPASS);
  100. WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
  101. (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
  102. NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
  103. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  104. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  105. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  106. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  107. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  108. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  109. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  110. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  111. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  112. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  113. for (i = 0; i < 256; i++) {
  114. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  115. (radeon_crtc->lut_r[i] << 20) |
  116. (radeon_crtc->lut_g[i] << 10) |
  117. (radeon_crtc->lut_b[i] << 0));
  118. }
  119. WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
  120. (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  121. NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  122. NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  123. NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
  124. WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
  125. (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
  126. NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
  127. WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
  128. (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
  129. NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
  130. WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  131. (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
  132. NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
  133. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  134. WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
  135. if (ASIC_IS_DCE8(rdev)) {
  136. /* XXX this only needs to be programmed once per crtc at startup,
  137. * not sure where the best place for it is
  138. */
  139. WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
  140. CIK_CURSOR_ALPHA_BLND_ENA);
  141. }
  142. }
  143. static void legacy_crtc_load_lut(struct drm_crtc *crtc)
  144. {
  145. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  146. struct drm_device *dev = crtc->dev;
  147. struct radeon_device *rdev = dev->dev_private;
  148. int i;
  149. uint32_t dac2_cntl;
  150. dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  151. if (radeon_crtc->crtc_id == 0)
  152. dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  153. else
  154. dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  155. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  156. WREG8(RADEON_PALETTE_INDEX, 0);
  157. for (i = 0; i < 256; i++) {
  158. WREG32(RADEON_PALETTE_30_DATA,
  159. (radeon_crtc->lut_r[i] << 20) |
  160. (radeon_crtc->lut_g[i] << 10) |
  161. (radeon_crtc->lut_b[i] << 0));
  162. }
  163. }
  164. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  165. {
  166. struct drm_device *dev = crtc->dev;
  167. struct radeon_device *rdev = dev->dev_private;
  168. if (!crtc->enabled)
  169. return;
  170. if (ASIC_IS_DCE5(rdev))
  171. dce5_crtc_load_lut(crtc);
  172. else if (ASIC_IS_DCE4(rdev))
  173. dce4_crtc_load_lut(crtc);
  174. else if (ASIC_IS_AVIVO(rdev))
  175. avivo_crtc_load_lut(crtc);
  176. else
  177. legacy_crtc_load_lut(crtc);
  178. }
  179. /** Sets the color ramps on behalf of fbcon */
  180. void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  181. u16 blue, int regno)
  182. {
  183. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  184. radeon_crtc->lut_r[regno] = red >> 6;
  185. radeon_crtc->lut_g[regno] = green >> 6;
  186. radeon_crtc->lut_b[regno] = blue >> 6;
  187. }
  188. /** Gets the color ramps on behalf of fbcon */
  189. void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  190. u16 *blue, int regno)
  191. {
  192. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  193. *red = radeon_crtc->lut_r[regno] << 6;
  194. *green = radeon_crtc->lut_g[regno] << 6;
  195. *blue = radeon_crtc->lut_b[regno] << 6;
  196. }
  197. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  198. u16 *blue, uint32_t start, uint32_t size)
  199. {
  200. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  201. int end = (start + size > 256) ? 256 : start + size, i;
  202. /* userspace palettes are always correct as is */
  203. for (i = start; i < end; i++) {
  204. radeon_crtc->lut_r[i] = red[i] >> 6;
  205. radeon_crtc->lut_g[i] = green[i] >> 6;
  206. radeon_crtc->lut_b[i] = blue[i] >> 6;
  207. }
  208. radeon_crtc_load_lut(crtc);
  209. }
  210. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  211. {
  212. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  213. drm_crtc_cleanup(crtc);
  214. destroy_workqueue(radeon_crtc->flip_queue);
  215. kfree(radeon_crtc);
  216. }
  217. /**
  218. * radeon_unpin_work_func - unpin old buffer object
  219. *
  220. * @__work - kernel work item
  221. *
  222. * Unpin the old frame buffer object outside of the interrupt handler
  223. */
  224. static void radeon_unpin_work_func(struct work_struct *__work)
  225. {
  226. struct radeon_flip_work *work =
  227. container_of(__work, struct radeon_flip_work, unpin_work);
  228. int r;
  229. /* unpin of the old buffer */
  230. r = radeon_bo_reserve(work->old_rbo, false);
  231. if (likely(r == 0)) {
  232. r = radeon_bo_unpin(work->old_rbo);
  233. if (unlikely(r != 0)) {
  234. DRM_ERROR("failed to unpin buffer after flip\n");
  235. }
  236. radeon_bo_unreserve(work->old_rbo);
  237. } else
  238. DRM_ERROR("failed to reserve buffer after flip\n");
  239. drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
  240. kfree(work);
  241. }
  242. void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
  243. {
  244. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  245. unsigned long flags;
  246. u32 update_pending;
  247. int vpos, hpos;
  248. /* can happen during initialization */
  249. if (radeon_crtc == NULL)
  250. return;
  251. /* Skip the pageflip completion check below (based on polling) on
  252. * asics which reliably support hw pageflip completion irqs. pflip
  253. * irqs are a reliable and race-free method of handling pageflip
  254. * completion detection. A use_pflipirq module parameter < 2 allows
  255. * to override this in case of asics with faulty pflip irqs.
  256. * A module parameter of 0 would only use this polling based path,
  257. * a parameter of 1 would use pflip irq only as a backup to this
  258. * path, as in Linux 3.16.
  259. */
  260. if ((radeon_use_pflipirq == 2) && ASIC_IS_DCE4(rdev))
  261. return;
  262. spin_lock_irqsave(&rdev->ddev->event_lock, flags);
  263. if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
  264. DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
  265. "RADEON_FLIP_SUBMITTED(%d)\n",
  266. radeon_crtc->flip_status,
  267. RADEON_FLIP_SUBMITTED);
  268. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  269. return;
  270. }
  271. update_pending = radeon_page_flip_pending(rdev, crtc_id);
  272. /* Has the pageflip already completed in crtc, or is it certain
  273. * to complete in this vblank?
  274. */
  275. if (update_pending &&
  276. (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
  277. &vpos, &hpos, NULL, NULL)) &&
  278. ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
  279. (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
  280. /* crtc didn't flip in this target vblank interval,
  281. * but flip is pending in crtc. Based on the current
  282. * scanout position we know that the current frame is
  283. * (nearly) complete and the flip will (likely)
  284. * complete before the start of the next frame.
  285. */
  286. update_pending = 0;
  287. }
  288. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  289. if (!update_pending)
  290. radeon_crtc_handle_flip(rdev, crtc_id);
  291. }
  292. /**
  293. * radeon_crtc_handle_flip - page flip completed
  294. *
  295. * @rdev: radeon device pointer
  296. * @crtc_id: crtc number this event is for
  297. *
  298. * Called when we are sure that a page flip for this crtc is completed.
  299. */
  300. void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
  301. {
  302. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  303. struct radeon_flip_work *work;
  304. unsigned long flags;
  305. /* this can happen at init */
  306. if (radeon_crtc == NULL)
  307. return;
  308. spin_lock_irqsave(&rdev->ddev->event_lock, flags);
  309. work = radeon_crtc->flip_work;
  310. if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
  311. DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
  312. "RADEON_FLIP_SUBMITTED(%d)\n",
  313. radeon_crtc->flip_status,
  314. RADEON_FLIP_SUBMITTED);
  315. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  316. return;
  317. }
  318. /* Pageflip completed. Clean up. */
  319. radeon_crtc->flip_status = RADEON_FLIP_NONE;
  320. radeon_crtc->flip_work = NULL;
  321. /* wakeup userspace */
  322. if (work->event)
  323. drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
  324. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  325. drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
  326. radeon_irq_kms_pflip_irq_put(rdev, work->crtc_id);
  327. queue_work(radeon_crtc->flip_queue, &work->unpin_work);
  328. }
  329. /**
  330. * radeon_flip_work_func - page flip framebuffer
  331. *
  332. * @work - kernel work item
  333. *
  334. * Wait for the buffer object to become idle and do the actual page flip
  335. */
  336. static void radeon_flip_work_func(struct work_struct *__work)
  337. {
  338. struct radeon_flip_work *work =
  339. container_of(__work, struct radeon_flip_work, flip_work);
  340. struct radeon_device *rdev = work->rdev;
  341. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[work->crtc_id];
  342. struct drm_crtc *crtc = &radeon_crtc->base;
  343. unsigned long flags;
  344. int r;
  345. down_read(&rdev->exclusive_lock);
  346. if (work->fence) {
  347. struct radeon_fence *fence;
  348. fence = to_radeon_fence(work->fence);
  349. if (fence && fence->rdev == rdev) {
  350. r = radeon_fence_wait(fence, false);
  351. if (r == -EDEADLK) {
  352. up_read(&rdev->exclusive_lock);
  353. do {
  354. r = radeon_gpu_reset(rdev);
  355. } while (r == -EAGAIN);
  356. down_read(&rdev->exclusive_lock);
  357. }
  358. } else
  359. r = fence_wait(work->fence, false);
  360. if (r)
  361. DRM_ERROR("failed to wait on page flip fence (%d)!\n", r);
  362. /* We continue with the page flip even if we failed to wait on
  363. * the fence, otherwise the DRM core and userspace will be
  364. * confused about which BO the CRTC is scanning out
  365. */
  366. fence_put(work->fence);
  367. work->fence = NULL;
  368. }
  369. /* We borrow the event spin lock for protecting flip_status */
  370. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  371. /* set the proper interrupt */
  372. radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
  373. /* do the flip (mmio) */
  374. radeon_page_flip(rdev, radeon_crtc->crtc_id, work->base);
  375. radeon_crtc->flip_status = RADEON_FLIP_SUBMITTED;
  376. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  377. up_read(&rdev->exclusive_lock);
  378. }
  379. static int radeon_crtc_page_flip(struct drm_crtc *crtc,
  380. struct drm_framebuffer *fb,
  381. struct drm_pending_vblank_event *event,
  382. uint32_t page_flip_flags)
  383. {
  384. struct drm_device *dev = crtc->dev;
  385. struct radeon_device *rdev = dev->dev_private;
  386. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  387. struct radeon_framebuffer *old_radeon_fb;
  388. struct radeon_framebuffer *new_radeon_fb;
  389. struct drm_gem_object *obj;
  390. struct radeon_flip_work *work;
  391. struct radeon_bo *new_rbo;
  392. uint32_t tiling_flags, pitch_pixels;
  393. uint64_t base;
  394. unsigned long flags;
  395. int r;
  396. work = kzalloc(sizeof *work, GFP_KERNEL);
  397. if (work == NULL)
  398. return -ENOMEM;
  399. INIT_WORK(&work->flip_work, radeon_flip_work_func);
  400. INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
  401. work->rdev = rdev;
  402. work->crtc_id = radeon_crtc->crtc_id;
  403. work->event = event;
  404. /* schedule unpin of the old buffer */
  405. old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
  406. obj = old_radeon_fb->obj;
  407. /* take a reference to the old object */
  408. drm_gem_object_reference(obj);
  409. work->old_rbo = gem_to_radeon_bo(obj);
  410. new_radeon_fb = to_radeon_framebuffer(fb);
  411. obj = new_radeon_fb->obj;
  412. new_rbo = gem_to_radeon_bo(obj);
  413. /* pin the new buffer */
  414. DRM_DEBUG_DRIVER("flip-ioctl() cur_rbo = %p, new_rbo = %p\n",
  415. work->old_rbo, new_rbo);
  416. r = radeon_bo_reserve(new_rbo, false);
  417. if (unlikely(r != 0)) {
  418. DRM_ERROR("failed to reserve new rbo buffer before flip\n");
  419. goto cleanup;
  420. }
  421. /* Only 27 bit offset for legacy CRTC */
  422. r = radeon_bo_pin_restricted(new_rbo, RADEON_GEM_DOMAIN_VRAM,
  423. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
  424. if (unlikely(r != 0)) {
  425. radeon_bo_unreserve(new_rbo);
  426. r = -EINVAL;
  427. DRM_ERROR("failed to pin new rbo buffer before flip\n");
  428. goto cleanup;
  429. }
  430. work->fence = fence_get(reservation_object_get_excl(new_rbo->tbo.resv));
  431. radeon_bo_get_tiling_flags(new_rbo, &tiling_flags, NULL);
  432. radeon_bo_unreserve(new_rbo);
  433. if (!ASIC_IS_AVIVO(rdev)) {
  434. /* crtc offset is from display base addr not FB location */
  435. base -= radeon_crtc->legacy_display_base_addr;
  436. pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
  437. if (tiling_flags & RADEON_TILING_MACRO) {
  438. if (ASIC_IS_R300(rdev)) {
  439. base &= ~0x7ff;
  440. } else {
  441. int byteshift = fb->bits_per_pixel >> 4;
  442. int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
  443. base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
  444. }
  445. } else {
  446. int offset = crtc->y * pitch_pixels + crtc->x;
  447. switch (fb->bits_per_pixel) {
  448. case 8:
  449. default:
  450. offset *= 1;
  451. break;
  452. case 15:
  453. case 16:
  454. offset *= 2;
  455. break;
  456. case 24:
  457. offset *= 3;
  458. break;
  459. case 32:
  460. offset *= 4;
  461. break;
  462. }
  463. base += offset;
  464. }
  465. base &= ~7;
  466. }
  467. work->base = base;
  468. r = drm_vblank_get(crtc->dev, radeon_crtc->crtc_id);
  469. if (r) {
  470. DRM_ERROR("failed to get vblank before flip\n");
  471. goto pflip_cleanup;
  472. }
  473. /* We borrow the event spin lock for protecting flip_work */
  474. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  475. if (radeon_crtc->flip_status != RADEON_FLIP_NONE) {
  476. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  477. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  478. r = -EBUSY;
  479. goto vblank_cleanup;
  480. }
  481. radeon_crtc->flip_status = RADEON_FLIP_PENDING;
  482. radeon_crtc->flip_work = work;
  483. /* update crtc fb */
  484. crtc->primary->fb = fb;
  485. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  486. queue_work(radeon_crtc->flip_queue, &work->flip_work);
  487. return 0;
  488. vblank_cleanup:
  489. drm_vblank_put(crtc->dev, radeon_crtc->crtc_id);
  490. pflip_cleanup:
  491. if (unlikely(radeon_bo_reserve(new_rbo, false) != 0)) {
  492. DRM_ERROR("failed to reserve new rbo in error path\n");
  493. goto cleanup;
  494. }
  495. if (unlikely(radeon_bo_unpin(new_rbo) != 0)) {
  496. DRM_ERROR("failed to unpin new rbo in error path\n");
  497. }
  498. radeon_bo_unreserve(new_rbo);
  499. cleanup:
  500. drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
  501. fence_put(work->fence);
  502. kfree(work);
  503. return r;
  504. }
  505. static int
  506. radeon_crtc_set_config(struct drm_mode_set *set)
  507. {
  508. struct drm_device *dev;
  509. struct radeon_device *rdev;
  510. struct drm_crtc *crtc;
  511. bool active = false;
  512. int ret;
  513. if (!set || !set->crtc)
  514. return -EINVAL;
  515. dev = set->crtc->dev;
  516. ret = pm_runtime_get_sync(dev->dev);
  517. if (ret < 0)
  518. return ret;
  519. ret = drm_crtc_helper_set_config(set);
  520. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  521. if (crtc->enabled)
  522. active = true;
  523. pm_runtime_mark_last_busy(dev->dev);
  524. rdev = dev->dev_private;
  525. /* if we have active crtcs and we don't have a power ref,
  526. take the current one */
  527. if (active && !rdev->have_disp_power_ref) {
  528. rdev->have_disp_power_ref = true;
  529. return ret;
  530. }
  531. /* if we have no active crtcs, then drop the power ref
  532. we got before */
  533. if (!active && rdev->have_disp_power_ref) {
  534. pm_runtime_put_autosuspend(dev->dev);
  535. rdev->have_disp_power_ref = false;
  536. }
  537. /* drop the power reference we got coming in here */
  538. pm_runtime_put_autosuspend(dev->dev);
  539. return ret;
  540. }
  541. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  542. .cursor_set = radeon_crtc_cursor_set,
  543. .cursor_move = radeon_crtc_cursor_move,
  544. .gamma_set = radeon_crtc_gamma_set,
  545. .set_config = radeon_crtc_set_config,
  546. .destroy = radeon_crtc_destroy,
  547. .page_flip = radeon_crtc_page_flip,
  548. };
  549. static void radeon_crtc_init(struct drm_device *dev, int index)
  550. {
  551. struct radeon_device *rdev = dev->dev_private;
  552. struct radeon_crtc *radeon_crtc;
  553. int i;
  554. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  555. if (radeon_crtc == NULL)
  556. return;
  557. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  558. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  559. radeon_crtc->crtc_id = index;
  560. radeon_crtc->flip_queue = create_singlethread_workqueue("radeon-crtc");
  561. rdev->mode_info.crtcs[index] = radeon_crtc;
  562. if (rdev->family >= CHIP_BONAIRE) {
  563. radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
  564. radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
  565. } else {
  566. radeon_crtc->max_cursor_width = CURSOR_WIDTH;
  567. radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
  568. }
  569. dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
  570. dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
  571. #if 0
  572. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  573. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  574. radeon_crtc->mode_set.num_connectors = 0;
  575. #endif
  576. for (i = 0; i < 256; i++) {
  577. radeon_crtc->lut_r[i] = i << 2;
  578. radeon_crtc->lut_g[i] = i << 2;
  579. radeon_crtc->lut_b[i] = i << 2;
  580. }
  581. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  582. radeon_atombios_init_crtc(dev, radeon_crtc);
  583. else
  584. radeon_legacy_init_crtc(dev, radeon_crtc);
  585. }
  586. static const char *encoder_names[38] = {
  587. "NONE",
  588. "INTERNAL_LVDS",
  589. "INTERNAL_TMDS1",
  590. "INTERNAL_TMDS2",
  591. "INTERNAL_DAC1",
  592. "INTERNAL_DAC2",
  593. "INTERNAL_SDVOA",
  594. "INTERNAL_SDVOB",
  595. "SI170B",
  596. "CH7303",
  597. "CH7301",
  598. "INTERNAL_DVO1",
  599. "EXTERNAL_SDVOA",
  600. "EXTERNAL_SDVOB",
  601. "TITFP513",
  602. "INTERNAL_LVTM1",
  603. "VT1623",
  604. "HDMI_SI1930",
  605. "HDMI_INTERNAL",
  606. "INTERNAL_KLDSCP_TMDS1",
  607. "INTERNAL_KLDSCP_DVO1",
  608. "INTERNAL_KLDSCP_DAC1",
  609. "INTERNAL_KLDSCP_DAC2",
  610. "SI178",
  611. "MVPU_FPGA",
  612. "INTERNAL_DDI",
  613. "VT1625",
  614. "HDMI_SI1932",
  615. "DP_AN9801",
  616. "DP_DP501",
  617. "INTERNAL_UNIPHY",
  618. "INTERNAL_KLDSCP_LVTMA",
  619. "INTERNAL_UNIPHY1",
  620. "INTERNAL_UNIPHY2",
  621. "NUTMEG",
  622. "TRAVIS",
  623. "INTERNAL_VCE",
  624. "INTERNAL_UNIPHY3",
  625. };
  626. static const char *hpd_names[6] = {
  627. "HPD1",
  628. "HPD2",
  629. "HPD3",
  630. "HPD4",
  631. "HPD5",
  632. "HPD6",
  633. };
  634. static void radeon_print_display_setup(struct drm_device *dev)
  635. {
  636. struct drm_connector *connector;
  637. struct radeon_connector *radeon_connector;
  638. struct drm_encoder *encoder;
  639. struct radeon_encoder *radeon_encoder;
  640. uint32_t devices;
  641. int i = 0;
  642. DRM_INFO("Radeon Display Connectors\n");
  643. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  644. radeon_connector = to_radeon_connector(connector);
  645. DRM_INFO("Connector %d:\n", i);
  646. DRM_INFO(" %s\n", connector->name);
  647. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  648. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  649. if (radeon_connector->ddc_bus) {
  650. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  651. radeon_connector->ddc_bus->rec.mask_clk_reg,
  652. radeon_connector->ddc_bus->rec.mask_data_reg,
  653. radeon_connector->ddc_bus->rec.a_clk_reg,
  654. radeon_connector->ddc_bus->rec.a_data_reg,
  655. radeon_connector->ddc_bus->rec.en_clk_reg,
  656. radeon_connector->ddc_bus->rec.en_data_reg,
  657. radeon_connector->ddc_bus->rec.y_clk_reg,
  658. radeon_connector->ddc_bus->rec.y_data_reg);
  659. if (radeon_connector->router.ddc_valid)
  660. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  661. radeon_connector->router.ddc_mux_control_pin,
  662. radeon_connector->router.ddc_mux_state);
  663. if (radeon_connector->router.cd_valid)
  664. DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
  665. radeon_connector->router.cd_mux_control_pin,
  666. radeon_connector->router.cd_mux_state);
  667. } else {
  668. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  669. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  670. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  671. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  672. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  673. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  674. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  675. }
  676. DRM_INFO(" Encoders:\n");
  677. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  678. radeon_encoder = to_radeon_encoder(encoder);
  679. devices = radeon_encoder->devices & radeon_connector->devices;
  680. if (devices) {
  681. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  682. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  683. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  684. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  685. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  686. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  687. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  688. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  689. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  690. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  691. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  692. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  693. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  694. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  695. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  696. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  697. if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  698. DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
  699. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  700. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  701. if (devices & ATOM_DEVICE_CV_SUPPORT)
  702. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  703. }
  704. }
  705. i++;
  706. }
  707. }
  708. static bool radeon_setup_enc_conn(struct drm_device *dev)
  709. {
  710. struct radeon_device *rdev = dev->dev_private;
  711. bool ret = false;
  712. if (rdev->bios) {
  713. if (rdev->is_atom_bios) {
  714. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  715. if (ret == false)
  716. ret = radeon_get_atom_connector_info_from_object_table(dev);
  717. } else {
  718. ret = radeon_get_legacy_connector_info_from_bios(dev);
  719. if (ret == false)
  720. ret = radeon_get_legacy_connector_info_from_table(dev);
  721. }
  722. } else {
  723. if (!ASIC_IS_AVIVO(rdev))
  724. ret = radeon_get_legacy_connector_info_from_table(dev);
  725. }
  726. if (ret) {
  727. radeon_setup_encoder_clones(dev);
  728. radeon_print_display_setup(dev);
  729. }
  730. return ret;
  731. }
  732. /* avivo */
  733. /**
  734. * avivo_reduce_ratio - fractional number reduction
  735. *
  736. * @nom: nominator
  737. * @den: denominator
  738. * @nom_min: minimum value for nominator
  739. * @den_min: minimum value for denominator
  740. *
  741. * Find the greatest common divisor and apply it on both nominator and
  742. * denominator, but make nominator and denominator are at least as large
  743. * as their minimum values.
  744. */
  745. static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
  746. unsigned nom_min, unsigned den_min)
  747. {
  748. unsigned tmp;
  749. /* reduce the numbers to a simpler ratio */
  750. tmp = gcd(*nom, *den);
  751. *nom /= tmp;
  752. *den /= tmp;
  753. /* make sure nominator is large enough */
  754. if (*nom < nom_min) {
  755. tmp = DIV_ROUND_UP(nom_min, *nom);
  756. *nom *= tmp;
  757. *den *= tmp;
  758. }
  759. /* make sure the denominator is large enough */
  760. if (*den < den_min) {
  761. tmp = DIV_ROUND_UP(den_min, *den);
  762. *nom *= tmp;
  763. *den *= tmp;
  764. }
  765. }
  766. /**
  767. * avivo_get_fb_ref_div - feedback and ref divider calculation
  768. *
  769. * @nom: nominator
  770. * @den: denominator
  771. * @post_div: post divider
  772. * @fb_div_max: feedback divider maximum
  773. * @ref_div_max: reference divider maximum
  774. * @fb_div: resulting feedback divider
  775. * @ref_div: resulting reference divider
  776. *
  777. * Calculate feedback and reference divider for a given post divider. Makes
  778. * sure we stay within the limits.
  779. */
  780. static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
  781. unsigned fb_div_max, unsigned ref_div_max,
  782. unsigned *fb_div, unsigned *ref_div)
  783. {
  784. /* limit reference * post divider to a maximum */
  785. ref_div_max = max(min(100 / post_div, ref_div_max), 1u);
  786. /* get matching reference and feedback divider */
  787. *ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
  788. *fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
  789. /* limit fb divider to its maximum */
  790. if (*fb_div > fb_div_max) {
  791. *ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
  792. *fb_div = fb_div_max;
  793. }
  794. }
  795. /**
  796. * radeon_compute_pll_avivo - compute PLL paramaters
  797. *
  798. * @pll: information about the PLL
  799. * @dot_clock_p: resulting pixel clock
  800. * fb_div_p: resulting feedback divider
  801. * frac_fb_div_p: fractional part of the feedback divider
  802. * ref_div_p: resulting reference divider
  803. * post_div_p: resulting reference divider
  804. *
  805. * Try to calculate the PLL parameters to generate the given frequency:
  806. * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
  807. */
  808. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  809. u32 freq,
  810. u32 *dot_clock_p,
  811. u32 *fb_div_p,
  812. u32 *frac_fb_div_p,
  813. u32 *ref_div_p,
  814. u32 *post_div_p)
  815. {
  816. unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
  817. freq : freq / 10;
  818. unsigned fb_div_min, fb_div_max, fb_div;
  819. unsigned post_div_min, post_div_max, post_div;
  820. unsigned ref_div_min, ref_div_max, ref_div;
  821. unsigned post_div_best, diff_best;
  822. unsigned nom, den;
  823. /* determine allowed feedback divider range */
  824. fb_div_min = pll->min_feedback_div;
  825. fb_div_max = pll->max_feedback_div;
  826. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  827. fb_div_min *= 10;
  828. fb_div_max *= 10;
  829. }
  830. /* determine allowed ref divider range */
  831. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  832. ref_div_min = pll->reference_div;
  833. else
  834. ref_div_min = pll->min_ref_div;
  835. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
  836. pll->flags & RADEON_PLL_USE_REF_DIV)
  837. ref_div_max = pll->reference_div;
  838. else if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
  839. /* fix for problems on RS880 */
  840. ref_div_max = min(pll->max_ref_div, 7u);
  841. else
  842. ref_div_max = pll->max_ref_div;
  843. /* determine allowed post divider range */
  844. if (pll->flags & RADEON_PLL_USE_POST_DIV) {
  845. post_div_min = pll->post_div;
  846. post_div_max = pll->post_div;
  847. } else {
  848. unsigned vco_min, vco_max;
  849. if (pll->flags & RADEON_PLL_IS_LCD) {
  850. vco_min = pll->lcd_pll_out_min;
  851. vco_max = pll->lcd_pll_out_max;
  852. } else {
  853. vco_min = pll->pll_out_min;
  854. vco_max = pll->pll_out_max;
  855. }
  856. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  857. vco_min *= 10;
  858. vco_max *= 10;
  859. }
  860. post_div_min = vco_min / target_clock;
  861. if ((target_clock * post_div_min) < vco_min)
  862. ++post_div_min;
  863. if (post_div_min < pll->min_post_div)
  864. post_div_min = pll->min_post_div;
  865. post_div_max = vco_max / target_clock;
  866. if ((target_clock * post_div_max) > vco_max)
  867. --post_div_max;
  868. if (post_div_max > pll->max_post_div)
  869. post_div_max = pll->max_post_div;
  870. }
  871. /* represent the searched ratio as fractional number */
  872. nom = target_clock;
  873. den = pll->reference_freq;
  874. /* reduce the numbers to a simpler ratio */
  875. avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
  876. /* now search for a post divider */
  877. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
  878. post_div_best = post_div_min;
  879. else
  880. post_div_best = post_div_max;
  881. diff_best = ~0;
  882. for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
  883. unsigned diff;
  884. avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
  885. ref_div_max, &fb_div, &ref_div);
  886. diff = abs(target_clock - (pll->reference_freq * fb_div) /
  887. (ref_div * post_div));
  888. if (diff < diff_best || (diff == diff_best &&
  889. !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
  890. post_div_best = post_div;
  891. diff_best = diff;
  892. }
  893. }
  894. post_div = post_div_best;
  895. /* get the feedback and reference divider for the optimal value */
  896. avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
  897. &fb_div, &ref_div);
  898. /* reduce the numbers to a simpler ratio once more */
  899. /* this also makes sure that the reference divider is large enough */
  900. avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
  901. /* avoid high jitter with small fractional dividers */
  902. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV && (fb_div % 10)) {
  903. fb_div_min = max(fb_div_min, (9 - (fb_div % 10)) * 20 + 50);
  904. if (fb_div < fb_div_min) {
  905. unsigned tmp = DIV_ROUND_UP(fb_div_min, fb_div);
  906. fb_div *= tmp;
  907. ref_div *= tmp;
  908. }
  909. }
  910. /* and finally save the result */
  911. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  912. *fb_div_p = fb_div / 10;
  913. *frac_fb_div_p = fb_div % 10;
  914. } else {
  915. *fb_div_p = fb_div;
  916. *frac_fb_div_p = 0;
  917. }
  918. *dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
  919. (pll->reference_freq * *frac_fb_div_p)) /
  920. (ref_div * post_div * 10);
  921. *ref_div_p = ref_div;
  922. *post_div_p = post_div;
  923. DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  924. freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
  925. ref_div, post_div);
  926. }
  927. /* pre-avivo */
  928. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  929. {
  930. uint64_t mod;
  931. n += d / 2;
  932. mod = do_div(n, d);
  933. return n;
  934. }
  935. void radeon_compute_pll_legacy(struct radeon_pll *pll,
  936. uint64_t freq,
  937. uint32_t *dot_clock_p,
  938. uint32_t *fb_div_p,
  939. uint32_t *frac_fb_div_p,
  940. uint32_t *ref_div_p,
  941. uint32_t *post_div_p)
  942. {
  943. uint32_t min_ref_div = pll->min_ref_div;
  944. uint32_t max_ref_div = pll->max_ref_div;
  945. uint32_t min_post_div = pll->min_post_div;
  946. uint32_t max_post_div = pll->max_post_div;
  947. uint32_t min_fractional_feed_div = 0;
  948. uint32_t max_fractional_feed_div = 0;
  949. uint32_t best_vco = pll->best_vco;
  950. uint32_t best_post_div = 1;
  951. uint32_t best_ref_div = 1;
  952. uint32_t best_feedback_div = 1;
  953. uint32_t best_frac_feedback_div = 0;
  954. uint32_t best_freq = -1;
  955. uint32_t best_error = 0xffffffff;
  956. uint32_t best_vco_diff = 1;
  957. uint32_t post_div;
  958. u32 pll_out_min, pll_out_max;
  959. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  960. freq = freq * 1000;
  961. if (pll->flags & RADEON_PLL_IS_LCD) {
  962. pll_out_min = pll->lcd_pll_out_min;
  963. pll_out_max = pll->lcd_pll_out_max;
  964. } else {
  965. pll_out_min = pll->pll_out_min;
  966. pll_out_max = pll->pll_out_max;
  967. }
  968. if (pll_out_min > 64800)
  969. pll_out_min = 64800;
  970. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  971. min_ref_div = max_ref_div = pll->reference_div;
  972. else {
  973. while (min_ref_div < max_ref_div-1) {
  974. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  975. uint32_t pll_in = pll->reference_freq / mid;
  976. if (pll_in < pll->pll_in_min)
  977. max_ref_div = mid;
  978. else if (pll_in > pll->pll_in_max)
  979. min_ref_div = mid;
  980. else
  981. break;
  982. }
  983. }
  984. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  985. min_post_div = max_post_div = pll->post_div;
  986. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  987. min_fractional_feed_div = pll->min_frac_feedback_div;
  988. max_fractional_feed_div = pll->max_frac_feedback_div;
  989. }
  990. for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  991. uint32_t ref_div;
  992. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  993. continue;
  994. /* legacy radeons only have a few post_divs */
  995. if (pll->flags & RADEON_PLL_LEGACY) {
  996. if ((post_div == 5) ||
  997. (post_div == 7) ||
  998. (post_div == 9) ||
  999. (post_div == 10) ||
  1000. (post_div == 11) ||
  1001. (post_div == 13) ||
  1002. (post_div == 14) ||
  1003. (post_div == 15))
  1004. continue;
  1005. }
  1006. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  1007. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  1008. uint32_t pll_in = pll->reference_freq / ref_div;
  1009. uint32_t min_feed_div = pll->min_feedback_div;
  1010. uint32_t max_feed_div = pll->max_feedback_div + 1;
  1011. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  1012. continue;
  1013. while (min_feed_div < max_feed_div) {
  1014. uint32_t vco;
  1015. uint32_t min_frac_feed_div = min_fractional_feed_div;
  1016. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  1017. uint32_t frac_feedback_div;
  1018. uint64_t tmp;
  1019. feedback_div = (min_feed_div + max_feed_div) / 2;
  1020. tmp = (uint64_t)pll->reference_freq * feedback_div;
  1021. vco = radeon_div(tmp, ref_div);
  1022. if (vco < pll_out_min) {
  1023. min_feed_div = feedback_div + 1;
  1024. continue;
  1025. } else if (vco > pll_out_max) {
  1026. max_feed_div = feedback_div;
  1027. continue;
  1028. }
  1029. while (min_frac_feed_div < max_frac_feed_div) {
  1030. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  1031. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  1032. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  1033. current_freq = radeon_div(tmp, ref_div * post_div);
  1034. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  1035. if (freq < current_freq)
  1036. error = 0xffffffff;
  1037. else
  1038. error = freq - current_freq;
  1039. } else
  1040. error = abs(current_freq - freq);
  1041. vco_diff = abs(vco - best_vco);
  1042. if ((best_vco == 0 && error < best_error) ||
  1043. (best_vco != 0 &&
  1044. ((best_error > 100 && error < best_error - 100) ||
  1045. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  1046. best_post_div = post_div;
  1047. best_ref_div = ref_div;
  1048. best_feedback_div = feedback_div;
  1049. best_frac_feedback_div = frac_feedback_div;
  1050. best_freq = current_freq;
  1051. best_error = error;
  1052. best_vco_diff = vco_diff;
  1053. } else if (current_freq == freq) {
  1054. if (best_freq == -1) {
  1055. best_post_div = post_div;
  1056. best_ref_div = ref_div;
  1057. best_feedback_div = feedback_div;
  1058. best_frac_feedback_div = frac_feedback_div;
  1059. best_freq = current_freq;
  1060. best_error = error;
  1061. best_vco_diff = vco_diff;
  1062. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  1063. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  1064. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  1065. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  1066. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  1067. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  1068. best_post_div = post_div;
  1069. best_ref_div = ref_div;
  1070. best_feedback_div = feedback_div;
  1071. best_frac_feedback_div = frac_feedback_div;
  1072. best_freq = current_freq;
  1073. best_error = error;
  1074. best_vco_diff = vco_diff;
  1075. }
  1076. }
  1077. if (current_freq < freq)
  1078. min_frac_feed_div = frac_feedback_div + 1;
  1079. else
  1080. max_frac_feed_div = frac_feedback_div;
  1081. }
  1082. if (current_freq < freq)
  1083. min_feed_div = feedback_div + 1;
  1084. else
  1085. max_feed_div = feedback_div;
  1086. }
  1087. }
  1088. }
  1089. *dot_clock_p = best_freq / 10000;
  1090. *fb_div_p = best_feedback_div;
  1091. *frac_fb_div_p = best_frac_feedback_div;
  1092. *ref_div_p = best_ref_div;
  1093. *post_div_p = best_post_div;
  1094. DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  1095. (long long)freq,
  1096. best_freq / 1000, best_feedback_div, best_frac_feedback_div,
  1097. best_ref_div, best_post_div);
  1098. }
  1099. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  1100. {
  1101. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  1102. if (radeon_fb->obj) {
  1103. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  1104. }
  1105. drm_framebuffer_cleanup(fb);
  1106. kfree(radeon_fb);
  1107. }
  1108. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  1109. struct drm_file *file_priv,
  1110. unsigned int *handle)
  1111. {
  1112. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  1113. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  1114. }
  1115. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  1116. .destroy = radeon_user_framebuffer_destroy,
  1117. .create_handle = radeon_user_framebuffer_create_handle,
  1118. };
  1119. int
  1120. radeon_framebuffer_init(struct drm_device *dev,
  1121. struct radeon_framebuffer *rfb,
  1122. struct drm_mode_fb_cmd2 *mode_cmd,
  1123. struct drm_gem_object *obj)
  1124. {
  1125. int ret;
  1126. rfb->obj = obj;
  1127. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  1128. ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  1129. if (ret) {
  1130. rfb->obj = NULL;
  1131. return ret;
  1132. }
  1133. return 0;
  1134. }
  1135. static struct drm_framebuffer *
  1136. radeon_user_framebuffer_create(struct drm_device *dev,
  1137. struct drm_file *file_priv,
  1138. struct drm_mode_fb_cmd2 *mode_cmd)
  1139. {
  1140. struct drm_gem_object *obj;
  1141. struct radeon_framebuffer *radeon_fb;
  1142. int ret;
  1143. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  1144. if (obj == NULL) {
  1145. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  1146. "can't create framebuffer\n", mode_cmd->handles[0]);
  1147. return ERR_PTR(-ENOENT);
  1148. }
  1149. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  1150. if (radeon_fb == NULL) {
  1151. drm_gem_object_unreference_unlocked(obj);
  1152. return ERR_PTR(-ENOMEM);
  1153. }
  1154. ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  1155. if (ret) {
  1156. kfree(radeon_fb);
  1157. drm_gem_object_unreference_unlocked(obj);
  1158. return ERR_PTR(ret);
  1159. }
  1160. return &radeon_fb->base;
  1161. }
  1162. static void radeon_output_poll_changed(struct drm_device *dev)
  1163. {
  1164. struct radeon_device *rdev = dev->dev_private;
  1165. radeon_fb_output_poll_changed(rdev);
  1166. }
  1167. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  1168. .fb_create = radeon_user_framebuffer_create,
  1169. .output_poll_changed = radeon_output_poll_changed
  1170. };
  1171. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  1172. { { 0, "driver" },
  1173. { 1, "bios" },
  1174. };
  1175. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  1176. { { TV_STD_NTSC, "ntsc" },
  1177. { TV_STD_PAL, "pal" },
  1178. { TV_STD_PAL_M, "pal-m" },
  1179. { TV_STD_PAL_60, "pal-60" },
  1180. { TV_STD_NTSC_J, "ntsc-j" },
  1181. { TV_STD_SCART_PAL, "scart-pal" },
  1182. { TV_STD_PAL_CN, "pal-cn" },
  1183. { TV_STD_SECAM, "secam" },
  1184. };
  1185. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  1186. { { UNDERSCAN_OFF, "off" },
  1187. { UNDERSCAN_ON, "on" },
  1188. { UNDERSCAN_AUTO, "auto" },
  1189. };
  1190. static struct drm_prop_enum_list radeon_audio_enum_list[] =
  1191. { { RADEON_AUDIO_DISABLE, "off" },
  1192. { RADEON_AUDIO_ENABLE, "on" },
  1193. { RADEON_AUDIO_AUTO, "auto" },
  1194. };
  1195. /* XXX support different dither options? spatial, temporal, both, etc. */
  1196. static struct drm_prop_enum_list radeon_dither_enum_list[] =
  1197. { { RADEON_FMT_DITHER_DISABLE, "off" },
  1198. { RADEON_FMT_DITHER_ENABLE, "on" },
  1199. };
  1200. static int radeon_modeset_create_props(struct radeon_device *rdev)
  1201. {
  1202. int sz;
  1203. if (rdev->is_atom_bios) {
  1204. rdev->mode_info.coherent_mode_property =
  1205. drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
  1206. if (!rdev->mode_info.coherent_mode_property)
  1207. return -ENOMEM;
  1208. }
  1209. if (!ASIC_IS_AVIVO(rdev)) {
  1210. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  1211. rdev->mode_info.tmds_pll_property =
  1212. drm_property_create_enum(rdev->ddev, 0,
  1213. "tmds_pll",
  1214. radeon_tmds_pll_enum_list, sz);
  1215. }
  1216. rdev->mode_info.load_detect_property =
  1217. drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
  1218. if (!rdev->mode_info.load_detect_property)
  1219. return -ENOMEM;
  1220. drm_mode_create_scaling_mode_property(rdev->ddev);
  1221. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  1222. rdev->mode_info.tv_std_property =
  1223. drm_property_create_enum(rdev->ddev, 0,
  1224. "tv standard",
  1225. radeon_tv_std_enum_list, sz);
  1226. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  1227. rdev->mode_info.underscan_property =
  1228. drm_property_create_enum(rdev->ddev, 0,
  1229. "underscan",
  1230. radeon_underscan_enum_list, sz);
  1231. rdev->mode_info.underscan_hborder_property =
  1232. drm_property_create_range(rdev->ddev, 0,
  1233. "underscan hborder", 0, 128);
  1234. if (!rdev->mode_info.underscan_hborder_property)
  1235. return -ENOMEM;
  1236. rdev->mode_info.underscan_vborder_property =
  1237. drm_property_create_range(rdev->ddev, 0,
  1238. "underscan vborder", 0, 128);
  1239. if (!rdev->mode_info.underscan_vborder_property)
  1240. return -ENOMEM;
  1241. sz = ARRAY_SIZE(radeon_audio_enum_list);
  1242. rdev->mode_info.audio_property =
  1243. drm_property_create_enum(rdev->ddev, 0,
  1244. "audio",
  1245. radeon_audio_enum_list, sz);
  1246. sz = ARRAY_SIZE(radeon_dither_enum_list);
  1247. rdev->mode_info.dither_property =
  1248. drm_property_create_enum(rdev->ddev, 0,
  1249. "dither",
  1250. radeon_dither_enum_list, sz);
  1251. return 0;
  1252. }
  1253. void radeon_update_display_priority(struct radeon_device *rdev)
  1254. {
  1255. /* adjustment options for the display watermarks */
  1256. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  1257. /* set display priority to high for r3xx, rv515 chips
  1258. * this avoids flickering due to underflow to the
  1259. * display controllers during heavy acceleration.
  1260. * Don't force high on rs4xx igp chips as it seems to
  1261. * affect the sound card. See kernel bug 15982.
  1262. */
  1263. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  1264. !(rdev->flags & RADEON_IS_IGP))
  1265. rdev->disp_priority = 2;
  1266. else
  1267. rdev->disp_priority = 0;
  1268. } else
  1269. rdev->disp_priority = radeon_disp_priority;
  1270. }
  1271. /*
  1272. * Allocate hdmi structs and determine register offsets
  1273. */
  1274. static void radeon_afmt_init(struct radeon_device *rdev)
  1275. {
  1276. int i;
  1277. for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
  1278. rdev->mode_info.afmt[i] = NULL;
  1279. if (ASIC_IS_NODCE(rdev)) {
  1280. /* nothing to do */
  1281. } else if (ASIC_IS_DCE4(rdev)) {
  1282. static uint32_t eg_offsets[] = {
  1283. EVERGREEN_CRTC0_REGISTER_OFFSET,
  1284. EVERGREEN_CRTC1_REGISTER_OFFSET,
  1285. EVERGREEN_CRTC2_REGISTER_OFFSET,
  1286. EVERGREEN_CRTC3_REGISTER_OFFSET,
  1287. EVERGREEN_CRTC4_REGISTER_OFFSET,
  1288. EVERGREEN_CRTC5_REGISTER_OFFSET,
  1289. 0x13830 - 0x7030,
  1290. };
  1291. int num_afmt;
  1292. /* DCE8 has 7 audio blocks tied to DIG encoders */
  1293. /* DCE6 has 6 audio blocks tied to DIG encoders */
  1294. /* DCE4/5 has 6 audio blocks tied to DIG encoders */
  1295. /* DCE4.1 has 2 audio blocks tied to DIG encoders */
  1296. if (ASIC_IS_DCE8(rdev))
  1297. num_afmt = 7;
  1298. else if (ASIC_IS_DCE6(rdev))
  1299. num_afmt = 6;
  1300. else if (ASIC_IS_DCE5(rdev))
  1301. num_afmt = 6;
  1302. else if (ASIC_IS_DCE41(rdev))
  1303. num_afmt = 2;
  1304. else /* DCE4 */
  1305. num_afmt = 6;
  1306. BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
  1307. for (i = 0; i < num_afmt; i++) {
  1308. rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1309. if (rdev->mode_info.afmt[i]) {
  1310. rdev->mode_info.afmt[i]->offset = eg_offsets[i];
  1311. rdev->mode_info.afmt[i]->id = i;
  1312. }
  1313. }
  1314. } else if (ASIC_IS_DCE3(rdev)) {
  1315. /* DCE3.x has 2 audio blocks tied to DIG encoders */
  1316. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1317. if (rdev->mode_info.afmt[0]) {
  1318. rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
  1319. rdev->mode_info.afmt[0]->id = 0;
  1320. }
  1321. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1322. if (rdev->mode_info.afmt[1]) {
  1323. rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
  1324. rdev->mode_info.afmt[1]->id = 1;
  1325. }
  1326. } else if (ASIC_IS_DCE2(rdev)) {
  1327. /* DCE2 has at least 1 routable audio block */
  1328. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1329. if (rdev->mode_info.afmt[0]) {
  1330. rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
  1331. rdev->mode_info.afmt[0]->id = 0;
  1332. }
  1333. /* r6xx has 2 routable audio blocks */
  1334. if (rdev->family >= CHIP_R600) {
  1335. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1336. if (rdev->mode_info.afmt[1]) {
  1337. rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
  1338. rdev->mode_info.afmt[1]->id = 1;
  1339. }
  1340. }
  1341. }
  1342. }
  1343. static void radeon_afmt_fini(struct radeon_device *rdev)
  1344. {
  1345. int i;
  1346. for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
  1347. kfree(rdev->mode_info.afmt[i]);
  1348. rdev->mode_info.afmt[i] = NULL;
  1349. }
  1350. }
  1351. int radeon_modeset_init(struct radeon_device *rdev)
  1352. {
  1353. int i;
  1354. int ret;
  1355. drm_mode_config_init(rdev->ddev);
  1356. rdev->mode_info.mode_config_initialized = true;
  1357. rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
  1358. if (ASIC_IS_DCE5(rdev)) {
  1359. rdev->ddev->mode_config.max_width = 16384;
  1360. rdev->ddev->mode_config.max_height = 16384;
  1361. } else if (ASIC_IS_AVIVO(rdev)) {
  1362. rdev->ddev->mode_config.max_width = 8192;
  1363. rdev->ddev->mode_config.max_height = 8192;
  1364. } else {
  1365. rdev->ddev->mode_config.max_width = 4096;
  1366. rdev->ddev->mode_config.max_height = 4096;
  1367. }
  1368. rdev->ddev->mode_config.preferred_depth = 24;
  1369. rdev->ddev->mode_config.prefer_shadow = 1;
  1370. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  1371. ret = radeon_modeset_create_props(rdev);
  1372. if (ret) {
  1373. return ret;
  1374. }
  1375. /* init i2c buses */
  1376. radeon_i2c_init(rdev);
  1377. /* check combios for a valid hardcoded EDID - Sun servers */
  1378. if (!rdev->is_atom_bios) {
  1379. /* check for hardcoded EDID in BIOS */
  1380. radeon_combios_check_hardcoded_edid(rdev);
  1381. }
  1382. /* allocate crtcs */
  1383. for (i = 0; i < rdev->num_crtc; i++) {
  1384. radeon_crtc_init(rdev->ddev, i);
  1385. }
  1386. /* okay we should have all the bios connectors */
  1387. ret = radeon_setup_enc_conn(rdev->ddev);
  1388. if (!ret) {
  1389. return ret;
  1390. }
  1391. /* init dig PHYs, disp eng pll */
  1392. if (rdev->is_atom_bios) {
  1393. radeon_atom_encoder_init(rdev);
  1394. radeon_atom_disp_eng_pll_init(rdev);
  1395. }
  1396. /* initialize hpd */
  1397. radeon_hpd_init(rdev);
  1398. /* setup afmt */
  1399. radeon_afmt_init(rdev);
  1400. radeon_fbdev_init(rdev);
  1401. drm_kms_helper_poll_init(rdev->ddev);
  1402. if (rdev->pm.dpm_enabled) {
  1403. /* do dpm late init */
  1404. ret = radeon_pm_late_init(rdev);
  1405. if (ret) {
  1406. rdev->pm.dpm_enabled = false;
  1407. DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
  1408. }
  1409. /* set the dpm state for PX since there won't be
  1410. * a modeset to call this.
  1411. */
  1412. radeon_pm_compute_clocks(rdev);
  1413. }
  1414. return 0;
  1415. }
  1416. void radeon_modeset_fini(struct radeon_device *rdev)
  1417. {
  1418. radeon_fbdev_fini(rdev);
  1419. kfree(rdev->mode_info.bios_hardcoded_edid);
  1420. if (rdev->mode_info.mode_config_initialized) {
  1421. radeon_afmt_fini(rdev);
  1422. drm_kms_helper_poll_fini(rdev->ddev);
  1423. radeon_hpd_fini(rdev);
  1424. drm_mode_config_cleanup(rdev->ddev);
  1425. rdev->mode_info.mode_config_initialized = false;
  1426. }
  1427. /* free i2c buses */
  1428. radeon_i2c_fini(rdev);
  1429. }
  1430. static bool is_hdtv_mode(const struct drm_display_mode *mode)
  1431. {
  1432. /* try and guess if this is a tv or a monitor */
  1433. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1434. (mode->vdisplay == 576) || /* 576p */
  1435. (mode->vdisplay == 720) || /* 720p */
  1436. (mode->vdisplay == 1080)) /* 1080p */
  1437. return true;
  1438. else
  1439. return false;
  1440. }
  1441. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1442. const struct drm_display_mode *mode,
  1443. struct drm_display_mode *adjusted_mode)
  1444. {
  1445. struct drm_device *dev = crtc->dev;
  1446. struct radeon_device *rdev = dev->dev_private;
  1447. struct drm_encoder *encoder;
  1448. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1449. struct radeon_encoder *radeon_encoder;
  1450. struct drm_connector *connector;
  1451. struct radeon_connector *radeon_connector;
  1452. bool first = true;
  1453. u32 src_v = 1, dst_v = 1;
  1454. u32 src_h = 1, dst_h = 1;
  1455. radeon_crtc->h_border = 0;
  1456. radeon_crtc->v_border = 0;
  1457. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1458. if (encoder->crtc != crtc)
  1459. continue;
  1460. radeon_encoder = to_radeon_encoder(encoder);
  1461. connector = radeon_get_connector_for_encoder(encoder);
  1462. radeon_connector = to_radeon_connector(connector);
  1463. if (first) {
  1464. /* set scaling */
  1465. if (radeon_encoder->rmx_type == RMX_OFF)
  1466. radeon_crtc->rmx_type = RMX_OFF;
  1467. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1468. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1469. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1470. else
  1471. radeon_crtc->rmx_type = RMX_OFF;
  1472. /* copy native mode */
  1473. memcpy(&radeon_crtc->native_mode,
  1474. &radeon_encoder->native_mode,
  1475. sizeof(struct drm_display_mode));
  1476. src_v = crtc->mode.vdisplay;
  1477. dst_v = radeon_crtc->native_mode.vdisplay;
  1478. src_h = crtc->mode.hdisplay;
  1479. dst_h = radeon_crtc->native_mode.hdisplay;
  1480. /* fix up for overscan on hdmi */
  1481. if (ASIC_IS_AVIVO(rdev) &&
  1482. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1483. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1484. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1485. drm_detect_hdmi_monitor(radeon_connector_edid(connector)) &&
  1486. is_hdtv_mode(mode)))) {
  1487. if (radeon_encoder->underscan_hborder != 0)
  1488. radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1489. else
  1490. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1491. if (radeon_encoder->underscan_vborder != 0)
  1492. radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1493. else
  1494. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1495. radeon_crtc->rmx_type = RMX_FULL;
  1496. src_v = crtc->mode.vdisplay;
  1497. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1498. src_h = crtc->mode.hdisplay;
  1499. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1500. }
  1501. first = false;
  1502. } else {
  1503. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1504. /* WARNING: Right now this can't happen but
  1505. * in the future we need to check that scaling
  1506. * are consistent across different encoder
  1507. * (ie all encoder can work with the same
  1508. * scaling).
  1509. */
  1510. DRM_ERROR("Scaling not consistent across encoder.\n");
  1511. return false;
  1512. }
  1513. }
  1514. }
  1515. if (radeon_crtc->rmx_type != RMX_OFF) {
  1516. fixed20_12 a, b;
  1517. a.full = dfixed_const(src_v);
  1518. b.full = dfixed_const(dst_v);
  1519. radeon_crtc->vsc.full = dfixed_div(a, b);
  1520. a.full = dfixed_const(src_h);
  1521. b.full = dfixed_const(dst_h);
  1522. radeon_crtc->hsc.full = dfixed_div(a, b);
  1523. } else {
  1524. radeon_crtc->vsc.full = dfixed_const(1);
  1525. radeon_crtc->hsc.full = dfixed_const(1);
  1526. }
  1527. return true;
  1528. }
  1529. /*
  1530. * Retrieve current video scanout position of crtc on a given gpu, and
  1531. * an optional accurate timestamp of when query happened.
  1532. *
  1533. * \param dev Device to query.
  1534. * \param crtc Crtc to query.
  1535. * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
  1536. * \param *vpos Location where vertical scanout position should be stored.
  1537. * \param *hpos Location where horizontal scanout position should go.
  1538. * \param *stime Target location for timestamp taken immediately before
  1539. * scanout position query. Can be NULL to skip timestamp.
  1540. * \param *etime Target location for timestamp taken immediately after
  1541. * scanout position query. Can be NULL to skip timestamp.
  1542. *
  1543. * Returns vpos as a positive number while in active scanout area.
  1544. * Returns vpos as a negative number inside vblank, counting the number
  1545. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1546. * until start of active scanout / end of vblank."
  1547. *
  1548. * \return Flags, or'ed together as follows:
  1549. *
  1550. * DRM_SCANOUTPOS_VALID = Query successful.
  1551. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1552. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1553. * this flag means that returned position may be offset by a constant but
  1554. * unknown small number of scanlines wrt. real scanout position.
  1555. *
  1556. */
  1557. int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
  1558. int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
  1559. {
  1560. u32 stat_crtc = 0, vbl = 0, position = 0;
  1561. int vbl_start, vbl_end, vtotal, ret = 0;
  1562. bool in_vbl = true;
  1563. struct radeon_device *rdev = dev->dev_private;
  1564. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  1565. /* Get optional system timestamp before query. */
  1566. if (stime)
  1567. *stime = ktime_get();
  1568. if (ASIC_IS_DCE4(rdev)) {
  1569. if (crtc == 0) {
  1570. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1571. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1572. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1573. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1574. ret |= DRM_SCANOUTPOS_VALID;
  1575. }
  1576. if (crtc == 1) {
  1577. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1578. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1579. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1580. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1581. ret |= DRM_SCANOUTPOS_VALID;
  1582. }
  1583. if (crtc == 2) {
  1584. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1585. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1586. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1587. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1588. ret |= DRM_SCANOUTPOS_VALID;
  1589. }
  1590. if (crtc == 3) {
  1591. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1592. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1593. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1594. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1595. ret |= DRM_SCANOUTPOS_VALID;
  1596. }
  1597. if (crtc == 4) {
  1598. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1599. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1600. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1601. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1602. ret |= DRM_SCANOUTPOS_VALID;
  1603. }
  1604. if (crtc == 5) {
  1605. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1606. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1607. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1608. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1609. ret |= DRM_SCANOUTPOS_VALID;
  1610. }
  1611. } else if (ASIC_IS_AVIVO(rdev)) {
  1612. if (crtc == 0) {
  1613. vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1614. position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1615. ret |= DRM_SCANOUTPOS_VALID;
  1616. }
  1617. if (crtc == 1) {
  1618. vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1619. position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1620. ret |= DRM_SCANOUTPOS_VALID;
  1621. }
  1622. } else {
  1623. /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1624. if (crtc == 0) {
  1625. /* Assume vbl_end == 0, get vbl_start from
  1626. * upper 16 bits.
  1627. */
  1628. vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1629. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1630. /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1631. position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1632. stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1633. if (!(stat_crtc & 1))
  1634. in_vbl = false;
  1635. ret |= DRM_SCANOUTPOS_VALID;
  1636. }
  1637. if (crtc == 1) {
  1638. vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1639. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1640. position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1641. stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1642. if (!(stat_crtc & 1))
  1643. in_vbl = false;
  1644. ret |= DRM_SCANOUTPOS_VALID;
  1645. }
  1646. }
  1647. /* Get optional system timestamp after query. */
  1648. if (etime)
  1649. *etime = ktime_get();
  1650. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  1651. /* Decode into vertical and horizontal scanout position. */
  1652. *vpos = position & 0x1fff;
  1653. *hpos = (position >> 16) & 0x1fff;
  1654. /* Valid vblank area boundaries from gpu retrieved? */
  1655. if (vbl > 0) {
  1656. /* Yes: Decode. */
  1657. ret |= DRM_SCANOUTPOS_ACCURATE;
  1658. vbl_start = vbl & 0x1fff;
  1659. vbl_end = (vbl >> 16) & 0x1fff;
  1660. }
  1661. else {
  1662. /* No: Fake something reasonable which gives at least ok results. */
  1663. vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1664. vbl_end = 0;
  1665. }
  1666. /* Test scanout position against vblank region. */
  1667. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1668. in_vbl = false;
  1669. /* Check if inside vblank area and apply corrective offsets:
  1670. * vpos will then be >=0 in video scanout area, but negative
  1671. * within vblank area, counting down the number of lines until
  1672. * start of scanout.
  1673. */
  1674. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1675. if (in_vbl && (*vpos >= vbl_start)) {
  1676. vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1677. *vpos = *vpos - vtotal;
  1678. }
  1679. /* Correct for shifted end of vbl at vbl_end. */
  1680. *vpos = *vpos - vbl_end;
  1681. /* In vblank? */
  1682. if (in_vbl)
  1683. ret |= DRM_SCANOUTPOS_IN_VBLANK;
  1684. /* Is vpos outside nominal vblank area, but less than
  1685. * 1/100 of a frame height away from start of vblank?
  1686. * If so, assume this isn't a massively delayed vblank
  1687. * interrupt, but a vblank interrupt that fired a few
  1688. * microseconds before true start of vblank. Compensate
  1689. * by adding a full frame duration to the final timestamp.
  1690. * Happens, e.g., on ATI R500, R600.
  1691. *
  1692. * We only do this if DRM_CALLED_FROM_VBLIRQ.
  1693. */
  1694. if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
  1695. vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1696. vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1697. if (vbl_start - *vpos < vtotal / 100) {
  1698. *vpos -= vtotal;
  1699. /* Signal this correction as "applied". */
  1700. ret |= 0x8;
  1701. }
  1702. }
  1703. return ret;
  1704. }