radeon_display.c 61 KB

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