radeon_display.c 60 KB

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