radeon_display.c 61 KB

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