radeon_display.c 59 KB

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