radeon_display.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  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 <drm/drm_crtc_helper.h>
  32. #include <drm/drm_edid.h>
  33. static void avivo_crtc_load_lut(struct drm_crtc *crtc)
  34. {
  35. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  36. struct drm_device *dev = crtc->dev;
  37. struct radeon_device *rdev = dev->dev_private;
  38. int i;
  39. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  40. WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
  41. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  42. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  43. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  44. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  45. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  46. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  47. WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
  48. WREG32(AVIVO_DC_LUT_RW_MODE, 0);
  49. WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
  50. WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
  51. for (i = 0; i < 256; i++) {
  52. WREG32(AVIVO_DC_LUT_30_COLOR,
  53. (radeon_crtc->lut_r[i] << 20) |
  54. (radeon_crtc->lut_g[i] << 10) |
  55. (radeon_crtc->lut_b[i] << 0));
  56. }
  57. WREG32(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id);
  58. }
  59. static void dce4_crtc_load_lut(struct drm_crtc *crtc)
  60. {
  61. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  62. struct drm_device *dev = crtc->dev;
  63. struct radeon_device *rdev = dev->dev_private;
  64. int i;
  65. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  66. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  67. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  68. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  69. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  70. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  71. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  72. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  73. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  74. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  75. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  76. for (i = 0; i < 256; i++) {
  77. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  78. (radeon_crtc->lut_r[i] << 20) |
  79. (radeon_crtc->lut_g[i] << 10) |
  80. (radeon_crtc->lut_b[i] << 0));
  81. }
  82. }
  83. static void dce5_crtc_load_lut(struct drm_crtc *crtc)
  84. {
  85. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  86. struct drm_device *dev = crtc->dev;
  87. struct radeon_device *rdev = dev->dev_private;
  88. int i;
  89. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  90. WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  91. (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
  92. NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
  93. WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
  94. NI_GRPH_PRESCALE_BYPASS);
  95. WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
  96. NI_OVL_PRESCALE_BYPASS);
  97. WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
  98. (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
  99. NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
  100. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  101. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  102. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  103. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  104. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  105. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  106. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  107. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  108. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  109. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  110. for (i = 0; i < 256; i++) {
  111. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  112. (radeon_crtc->lut_r[i] << 20) |
  113. (radeon_crtc->lut_g[i] << 10) |
  114. (radeon_crtc->lut_b[i] << 0));
  115. }
  116. WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
  117. (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  118. NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  119. NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  120. NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
  121. WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
  122. (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
  123. NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
  124. WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
  125. (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
  126. NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
  127. WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  128. (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
  129. NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
  130. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  131. WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
  132. if (ASIC_IS_DCE8(rdev)) {
  133. /* XXX this only needs to be programmed once per crtc at startup,
  134. * not sure where the best place for it is
  135. */
  136. WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
  137. CIK_CURSOR_ALPHA_BLND_ENA);
  138. }
  139. }
  140. static void legacy_crtc_load_lut(struct drm_crtc *crtc)
  141. {
  142. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  143. struct drm_device *dev = crtc->dev;
  144. struct radeon_device *rdev = dev->dev_private;
  145. int i;
  146. uint32_t dac2_cntl;
  147. dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  148. if (radeon_crtc->crtc_id == 0)
  149. dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  150. else
  151. dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  152. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  153. WREG8(RADEON_PALETTE_INDEX, 0);
  154. for (i = 0; i < 256; i++) {
  155. WREG32(RADEON_PALETTE_30_DATA,
  156. (radeon_crtc->lut_r[i] << 20) |
  157. (radeon_crtc->lut_g[i] << 10) |
  158. (radeon_crtc->lut_b[i] << 0));
  159. }
  160. }
  161. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  162. {
  163. struct drm_device *dev = crtc->dev;
  164. struct radeon_device *rdev = dev->dev_private;
  165. if (!crtc->enabled)
  166. return;
  167. if (ASIC_IS_DCE5(rdev))
  168. dce5_crtc_load_lut(crtc);
  169. else if (ASIC_IS_DCE4(rdev))
  170. dce4_crtc_load_lut(crtc);
  171. else if (ASIC_IS_AVIVO(rdev))
  172. avivo_crtc_load_lut(crtc);
  173. else
  174. legacy_crtc_load_lut(crtc);
  175. }
  176. /** Sets the color ramps on behalf of fbcon */
  177. void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  178. u16 blue, int regno)
  179. {
  180. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  181. radeon_crtc->lut_r[regno] = red >> 6;
  182. radeon_crtc->lut_g[regno] = green >> 6;
  183. radeon_crtc->lut_b[regno] = blue >> 6;
  184. }
  185. /** Gets the color ramps on behalf of fbcon */
  186. void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  187. u16 *blue, int regno)
  188. {
  189. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  190. *red = radeon_crtc->lut_r[regno] << 6;
  191. *green = radeon_crtc->lut_g[regno] << 6;
  192. *blue = radeon_crtc->lut_b[regno] << 6;
  193. }
  194. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  195. u16 *blue, uint32_t start, uint32_t size)
  196. {
  197. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  198. int end = (start + size > 256) ? 256 : start + size, i;
  199. /* userspace palettes are always correct as is */
  200. for (i = start; i < end; i++) {
  201. radeon_crtc->lut_r[i] = red[i] >> 6;
  202. radeon_crtc->lut_g[i] = green[i] >> 6;
  203. radeon_crtc->lut_b[i] = blue[i] >> 6;
  204. }
  205. radeon_crtc_load_lut(crtc);
  206. }
  207. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  208. {
  209. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  210. drm_crtc_cleanup(crtc);
  211. kfree(radeon_crtc);
  212. }
  213. /*
  214. * Handle unpin events outside the interrupt handler proper.
  215. */
  216. static void radeon_unpin_work_func(struct work_struct *__work)
  217. {
  218. struct radeon_unpin_work *work =
  219. container_of(__work, struct radeon_unpin_work, work);
  220. int r;
  221. /* unpin of the old buffer */
  222. r = radeon_bo_reserve(work->old_rbo, false);
  223. if (likely(r == 0)) {
  224. r = radeon_bo_unpin(work->old_rbo);
  225. if (unlikely(r != 0)) {
  226. DRM_ERROR("failed to unpin buffer after flip\n");
  227. }
  228. radeon_bo_unreserve(work->old_rbo);
  229. } else
  230. DRM_ERROR("failed to reserve buffer after flip\n");
  231. drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
  232. kfree(work);
  233. }
  234. void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
  235. {
  236. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  237. struct radeon_unpin_work *work;
  238. unsigned long flags;
  239. u32 update_pending;
  240. int vpos, hpos;
  241. spin_lock_irqsave(&rdev->ddev->event_lock, flags);
  242. work = radeon_crtc->unpin_work;
  243. if (work == NULL ||
  244. (work->fence && !radeon_fence_signaled(work->fence))) {
  245. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  246. return;
  247. }
  248. /* New pageflip, or just completion of a previous one? */
  249. if (!radeon_crtc->deferred_flip_completion) {
  250. /* do the flip (mmio) */
  251. update_pending = radeon_page_flip(rdev, crtc_id, work->new_crtc_base);
  252. } else {
  253. /* This is just a completion of a flip queued in crtc
  254. * at last invocation. Make sure we go directly to
  255. * completion routine.
  256. */
  257. update_pending = 0;
  258. radeon_crtc->deferred_flip_completion = 0;
  259. }
  260. /* Has the pageflip already completed in crtc, or is it certain
  261. * to complete in this vblank?
  262. */
  263. if (update_pending &&
  264. (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id,
  265. &vpos, &hpos)) &&
  266. ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
  267. (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
  268. /* crtc didn't flip in this target vblank interval,
  269. * but flip is pending in crtc. Based on the current
  270. * scanout position we know that the current frame is
  271. * (nearly) complete and the flip will (likely)
  272. * complete before the start of the next frame.
  273. */
  274. update_pending = 0;
  275. }
  276. if (update_pending) {
  277. /* crtc didn't flip in this target vblank interval,
  278. * but flip is pending in crtc. It will complete it
  279. * in next vblank interval, so complete the flip at
  280. * next vblank irq.
  281. */
  282. radeon_crtc->deferred_flip_completion = 1;
  283. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  284. return;
  285. }
  286. /* Pageflip (will be) certainly completed in this vblank. Clean up. */
  287. radeon_crtc->unpin_work = NULL;
  288. /* wakeup userspace */
  289. if (work->event)
  290. drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
  291. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  292. drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
  293. radeon_fence_unref(&work->fence);
  294. radeon_post_page_flip(work->rdev, work->crtc_id);
  295. schedule_work(&work->work);
  296. }
  297. static int radeon_crtc_page_flip(struct drm_crtc *crtc,
  298. struct drm_framebuffer *fb,
  299. struct drm_pending_vblank_event *event)
  300. {
  301. struct drm_device *dev = crtc->dev;
  302. struct radeon_device *rdev = dev->dev_private;
  303. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  304. struct radeon_framebuffer *old_radeon_fb;
  305. struct radeon_framebuffer *new_radeon_fb;
  306. struct drm_gem_object *obj;
  307. struct radeon_bo *rbo;
  308. struct radeon_unpin_work *work;
  309. unsigned long flags;
  310. u32 tiling_flags, pitch_pixels;
  311. u64 base;
  312. int r;
  313. work = kzalloc(sizeof *work, GFP_KERNEL);
  314. if (work == NULL)
  315. return -ENOMEM;
  316. work->event = event;
  317. work->rdev = rdev;
  318. work->crtc_id = radeon_crtc->crtc_id;
  319. old_radeon_fb = to_radeon_framebuffer(crtc->fb);
  320. new_radeon_fb = to_radeon_framebuffer(fb);
  321. /* schedule unpin of the old buffer */
  322. obj = old_radeon_fb->obj;
  323. /* take a reference to the old object */
  324. drm_gem_object_reference(obj);
  325. rbo = gem_to_radeon_bo(obj);
  326. work->old_rbo = rbo;
  327. obj = new_radeon_fb->obj;
  328. rbo = gem_to_radeon_bo(obj);
  329. spin_lock(&rbo->tbo.bdev->fence_lock);
  330. if (rbo->tbo.sync_obj)
  331. work->fence = radeon_fence_ref(rbo->tbo.sync_obj);
  332. spin_unlock(&rbo->tbo.bdev->fence_lock);
  333. INIT_WORK(&work->work, radeon_unpin_work_func);
  334. /* We borrow the event spin lock for protecting unpin_work */
  335. spin_lock_irqsave(&dev->event_lock, flags);
  336. if (radeon_crtc->unpin_work) {
  337. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  338. r = -EBUSY;
  339. goto unlock_free;
  340. }
  341. radeon_crtc->unpin_work = work;
  342. radeon_crtc->deferred_flip_completion = 0;
  343. spin_unlock_irqrestore(&dev->event_lock, flags);
  344. /* pin the new buffer */
  345. DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
  346. work->old_rbo, rbo);
  347. r = radeon_bo_reserve(rbo, false);
  348. if (unlikely(r != 0)) {
  349. DRM_ERROR("failed to reserve new rbo buffer before flip\n");
  350. goto pflip_cleanup;
  351. }
  352. /* Only 27 bit offset for legacy CRTC */
  353. r = radeon_bo_pin_restricted(rbo, RADEON_GEM_DOMAIN_VRAM,
  354. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
  355. if (unlikely(r != 0)) {
  356. radeon_bo_unreserve(rbo);
  357. r = -EINVAL;
  358. DRM_ERROR("failed to pin new rbo buffer before flip\n");
  359. goto pflip_cleanup;
  360. }
  361. radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
  362. radeon_bo_unreserve(rbo);
  363. if (!ASIC_IS_AVIVO(rdev)) {
  364. /* crtc offset is from display base addr not FB location */
  365. base -= radeon_crtc->legacy_display_base_addr;
  366. pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
  367. if (tiling_flags & RADEON_TILING_MACRO) {
  368. if (ASIC_IS_R300(rdev)) {
  369. base &= ~0x7ff;
  370. } else {
  371. int byteshift = fb->bits_per_pixel >> 4;
  372. int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
  373. base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
  374. }
  375. } else {
  376. int offset = crtc->y * pitch_pixels + crtc->x;
  377. switch (fb->bits_per_pixel) {
  378. case 8:
  379. default:
  380. offset *= 1;
  381. break;
  382. case 15:
  383. case 16:
  384. offset *= 2;
  385. break;
  386. case 24:
  387. offset *= 3;
  388. break;
  389. case 32:
  390. offset *= 4;
  391. break;
  392. }
  393. base += offset;
  394. }
  395. base &= ~7;
  396. }
  397. spin_lock_irqsave(&dev->event_lock, flags);
  398. work->new_crtc_base = base;
  399. spin_unlock_irqrestore(&dev->event_lock, flags);
  400. /* update crtc fb */
  401. crtc->fb = fb;
  402. r = drm_vblank_get(dev, radeon_crtc->crtc_id);
  403. if (r) {
  404. DRM_ERROR("failed to get vblank before flip\n");
  405. goto pflip_cleanup1;
  406. }
  407. /* set the proper interrupt */
  408. radeon_pre_page_flip(rdev, radeon_crtc->crtc_id);
  409. return 0;
  410. pflip_cleanup1:
  411. if (unlikely(radeon_bo_reserve(rbo, false) != 0)) {
  412. DRM_ERROR("failed to reserve new rbo in error path\n");
  413. goto pflip_cleanup;
  414. }
  415. if (unlikely(radeon_bo_unpin(rbo) != 0)) {
  416. DRM_ERROR("failed to unpin new rbo in error path\n");
  417. }
  418. radeon_bo_unreserve(rbo);
  419. pflip_cleanup:
  420. spin_lock_irqsave(&dev->event_lock, flags);
  421. radeon_crtc->unpin_work = NULL;
  422. unlock_free:
  423. spin_unlock_irqrestore(&dev->event_lock, flags);
  424. drm_gem_object_unreference_unlocked(old_radeon_fb->obj);
  425. radeon_fence_unref(&work->fence);
  426. kfree(work);
  427. return r;
  428. }
  429. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  430. .cursor_set = radeon_crtc_cursor_set,
  431. .cursor_move = radeon_crtc_cursor_move,
  432. .gamma_set = radeon_crtc_gamma_set,
  433. .set_config = drm_crtc_helper_set_config,
  434. .destroy = radeon_crtc_destroy,
  435. .page_flip = radeon_crtc_page_flip,
  436. };
  437. static void radeon_crtc_init(struct drm_device *dev, int index)
  438. {
  439. struct radeon_device *rdev = dev->dev_private;
  440. struct radeon_crtc *radeon_crtc;
  441. int i;
  442. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  443. if (radeon_crtc == NULL)
  444. return;
  445. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  446. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  447. radeon_crtc->crtc_id = index;
  448. rdev->mode_info.crtcs[index] = radeon_crtc;
  449. if (rdev->family >= CHIP_BONAIRE) {
  450. radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
  451. radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
  452. } else {
  453. radeon_crtc->max_cursor_width = CURSOR_WIDTH;
  454. radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
  455. }
  456. #if 0
  457. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  458. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  459. radeon_crtc->mode_set.num_connectors = 0;
  460. #endif
  461. for (i = 0; i < 256; i++) {
  462. radeon_crtc->lut_r[i] = i << 2;
  463. radeon_crtc->lut_g[i] = i << 2;
  464. radeon_crtc->lut_b[i] = i << 2;
  465. }
  466. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  467. radeon_atombios_init_crtc(dev, radeon_crtc);
  468. else
  469. radeon_legacy_init_crtc(dev, radeon_crtc);
  470. }
  471. static const char *encoder_names[38] = {
  472. "NONE",
  473. "INTERNAL_LVDS",
  474. "INTERNAL_TMDS1",
  475. "INTERNAL_TMDS2",
  476. "INTERNAL_DAC1",
  477. "INTERNAL_DAC2",
  478. "INTERNAL_SDVOA",
  479. "INTERNAL_SDVOB",
  480. "SI170B",
  481. "CH7303",
  482. "CH7301",
  483. "INTERNAL_DVO1",
  484. "EXTERNAL_SDVOA",
  485. "EXTERNAL_SDVOB",
  486. "TITFP513",
  487. "INTERNAL_LVTM1",
  488. "VT1623",
  489. "HDMI_SI1930",
  490. "HDMI_INTERNAL",
  491. "INTERNAL_KLDSCP_TMDS1",
  492. "INTERNAL_KLDSCP_DVO1",
  493. "INTERNAL_KLDSCP_DAC1",
  494. "INTERNAL_KLDSCP_DAC2",
  495. "SI178",
  496. "MVPU_FPGA",
  497. "INTERNAL_DDI",
  498. "VT1625",
  499. "HDMI_SI1932",
  500. "DP_AN9801",
  501. "DP_DP501",
  502. "INTERNAL_UNIPHY",
  503. "INTERNAL_KLDSCP_LVTMA",
  504. "INTERNAL_UNIPHY1",
  505. "INTERNAL_UNIPHY2",
  506. "NUTMEG",
  507. "TRAVIS",
  508. "INTERNAL_VCE",
  509. "INTERNAL_UNIPHY3",
  510. };
  511. static const char *hpd_names[6] = {
  512. "HPD1",
  513. "HPD2",
  514. "HPD3",
  515. "HPD4",
  516. "HPD5",
  517. "HPD6",
  518. };
  519. static void radeon_print_display_setup(struct drm_device *dev)
  520. {
  521. struct drm_connector *connector;
  522. struct radeon_connector *radeon_connector;
  523. struct drm_encoder *encoder;
  524. struct radeon_encoder *radeon_encoder;
  525. uint32_t devices;
  526. int i = 0;
  527. DRM_INFO("Radeon Display Connectors\n");
  528. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  529. radeon_connector = to_radeon_connector(connector);
  530. DRM_INFO("Connector %d:\n", i);
  531. DRM_INFO(" %s\n", drm_get_connector_name(connector));
  532. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  533. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  534. if (radeon_connector->ddc_bus) {
  535. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  536. radeon_connector->ddc_bus->rec.mask_clk_reg,
  537. radeon_connector->ddc_bus->rec.mask_data_reg,
  538. radeon_connector->ddc_bus->rec.a_clk_reg,
  539. radeon_connector->ddc_bus->rec.a_data_reg,
  540. radeon_connector->ddc_bus->rec.en_clk_reg,
  541. radeon_connector->ddc_bus->rec.en_data_reg,
  542. radeon_connector->ddc_bus->rec.y_clk_reg,
  543. radeon_connector->ddc_bus->rec.y_data_reg);
  544. if (radeon_connector->router.ddc_valid)
  545. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  546. radeon_connector->router.ddc_mux_control_pin,
  547. radeon_connector->router.ddc_mux_state);
  548. if (radeon_connector->router.cd_valid)
  549. DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
  550. radeon_connector->router.cd_mux_control_pin,
  551. radeon_connector->router.cd_mux_state);
  552. } else {
  553. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  554. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  555. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  556. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  557. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  558. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  559. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  560. }
  561. DRM_INFO(" Encoders:\n");
  562. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  563. radeon_encoder = to_radeon_encoder(encoder);
  564. devices = radeon_encoder->devices & radeon_connector->devices;
  565. if (devices) {
  566. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  567. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  568. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  569. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  570. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  571. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  572. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  573. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  574. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  575. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  576. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  577. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  578. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  579. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  580. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  581. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  582. if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  583. DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
  584. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  585. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  586. if (devices & ATOM_DEVICE_CV_SUPPORT)
  587. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  588. }
  589. }
  590. i++;
  591. }
  592. }
  593. static bool radeon_setup_enc_conn(struct drm_device *dev)
  594. {
  595. struct radeon_device *rdev = dev->dev_private;
  596. bool ret = false;
  597. if (rdev->bios) {
  598. if (rdev->is_atom_bios) {
  599. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  600. if (ret == false)
  601. ret = radeon_get_atom_connector_info_from_object_table(dev);
  602. } else {
  603. ret = radeon_get_legacy_connector_info_from_bios(dev);
  604. if (ret == false)
  605. ret = radeon_get_legacy_connector_info_from_table(dev);
  606. }
  607. } else {
  608. if (!ASIC_IS_AVIVO(rdev))
  609. ret = radeon_get_legacy_connector_info_from_table(dev);
  610. }
  611. if (ret) {
  612. radeon_setup_encoder_clones(dev);
  613. radeon_print_display_setup(dev);
  614. }
  615. return ret;
  616. }
  617. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  618. {
  619. struct drm_device *dev = radeon_connector->base.dev;
  620. struct radeon_device *rdev = dev->dev_private;
  621. int ret = 0;
  622. /* on hw with routers, select right port */
  623. if (radeon_connector->router.ddc_valid)
  624. radeon_router_select_ddc_port(radeon_connector);
  625. if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
  626. ENCODER_OBJECT_ID_NONE) {
  627. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  628. if (dig->dp_i2c_bus)
  629. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  630. &dig->dp_i2c_bus->adapter);
  631. } else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  632. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
  633. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  634. if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  635. dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  636. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  637. &dig->dp_i2c_bus->adapter);
  638. else if (radeon_connector->ddc_bus && !radeon_connector->edid)
  639. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  640. &radeon_connector->ddc_bus->adapter);
  641. } else {
  642. if (radeon_connector->ddc_bus && !radeon_connector->edid)
  643. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  644. &radeon_connector->ddc_bus->adapter);
  645. }
  646. if (!radeon_connector->edid) {
  647. if (rdev->is_atom_bios) {
  648. /* some laptops provide a hardcoded edid in rom for LCDs */
  649. if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
  650. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
  651. radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  652. } else
  653. /* some servers provide a hardcoded edid in rom for KVMs */
  654. radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  655. }
  656. if (radeon_connector->edid) {
  657. drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  658. ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  659. return ret;
  660. }
  661. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  662. return 0;
  663. }
  664. /* avivo */
  665. static void avivo_get_fb_div(struct radeon_pll *pll,
  666. u32 target_clock,
  667. u32 post_div,
  668. u32 ref_div,
  669. u32 *fb_div,
  670. u32 *frac_fb_div)
  671. {
  672. u32 tmp = post_div * ref_div;
  673. tmp *= target_clock;
  674. *fb_div = tmp / pll->reference_freq;
  675. *frac_fb_div = tmp % pll->reference_freq;
  676. if (*fb_div > pll->max_feedback_div)
  677. *fb_div = pll->max_feedback_div;
  678. else if (*fb_div < pll->min_feedback_div)
  679. *fb_div = pll->min_feedback_div;
  680. }
  681. static u32 avivo_get_post_div(struct radeon_pll *pll,
  682. u32 target_clock)
  683. {
  684. u32 vco, post_div, tmp;
  685. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  686. return pll->post_div;
  687. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  688. if (pll->flags & RADEON_PLL_IS_LCD)
  689. vco = pll->lcd_pll_out_min;
  690. else
  691. vco = pll->pll_out_min;
  692. } else {
  693. if (pll->flags & RADEON_PLL_IS_LCD)
  694. vco = pll->lcd_pll_out_max;
  695. else
  696. vco = pll->pll_out_max;
  697. }
  698. post_div = vco / target_clock;
  699. tmp = vco % target_clock;
  700. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  701. if (tmp)
  702. post_div++;
  703. } else {
  704. if (!tmp)
  705. post_div--;
  706. }
  707. if (post_div > pll->max_post_div)
  708. post_div = pll->max_post_div;
  709. else if (post_div < pll->min_post_div)
  710. post_div = pll->min_post_div;
  711. return post_div;
  712. }
  713. #define MAX_TOLERANCE 10
  714. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  715. u32 freq,
  716. u32 *dot_clock_p,
  717. u32 *fb_div_p,
  718. u32 *frac_fb_div_p,
  719. u32 *ref_div_p,
  720. u32 *post_div_p)
  721. {
  722. u32 target_clock = freq / 10;
  723. u32 post_div = avivo_get_post_div(pll, target_clock);
  724. u32 ref_div = pll->min_ref_div;
  725. u32 fb_div = 0, frac_fb_div = 0, tmp;
  726. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  727. ref_div = pll->reference_div;
  728. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  729. avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
  730. frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
  731. if (frac_fb_div >= 5) {
  732. frac_fb_div -= 5;
  733. frac_fb_div = frac_fb_div / 10;
  734. frac_fb_div++;
  735. }
  736. if (frac_fb_div >= 10) {
  737. fb_div++;
  738. frac_fb_div = 0;
  739. }
  740. } else {
  741. while (ref_div <= pll->max_ref_div) {
  742. avivo_get_fb_div(pll, target_clock, post_div, ref_div,
  743. &fb_div, &frac_fb_div);
  744. if (frac_fb_div >= (pll->reference_freq / 2))
  745. fb_div++;
  746. frac_fb_div = 0;
  747. tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
  748. tmp = (tmp * 10000) / target_clock;
  749. if (tmp > (10000 + MAX_TOLERANCE))
  750. ref_div++;
  751. else if (tmp >= (10000 - MAX_TOLERANCE))
  752. break;
  753. else
  754. ref_div++;
  755. }
  756. }
  757. *dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
  758. (ref_div * post_div * 10);
  759. *fb_div_p = fb_div;
  760. *frac_fb_div_p = frac_fb_div;
  761. *ref_div_p = ref_div;
  762. *post_div_p = post_div;
  763. DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  764. *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
  765. }
  766. /* pre-avivo */
  767. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  768. {
  769. uint64_t mod;
  770. n += d / 2;
  771. mod = do_div(n, d);
  772. return n;
  773. }
  774. void radeon_compute_pll_legacy(struct radeon_pll *pll,
  775. uint64_t freq,
  776. uint32_t *dot_clock_p,
  777. uint32_t *fb_div_p,
  778. uint32_t *frac_fb_div_p,
  779. uint32_t *ref_div_p,
  780. uint32_t *post_div_p)
  781. {
  782. uint32_t min_ref_div = pll->min_ref_div;
  783. uint32_t max_ref_div = pll->max_ref_div;
  784. uint32_t min_post_div = pll->min_post_div;
  785. uint32_t max_post_div = pll->max_post_div;
  786. uint32_t min_fractional_feed_div = 0;
  787. uint32_t max_fractional_feed_div = 0;
  788. uint32_t best_vco = pll->best_vco;
  789. uint32_t best_post_div = 1;
  790. uint32_t best_ref_div = 1;
  791. uint32_t best_feedback_div = 1;
  792. uint32_t best_frac_feedback_div = 0;
  793. uint32_t best_freq = -1;
  794. uint32_t best_error = 0xffffffff;
  795. uint32_t best_vco_diff = 1;
  796. uint32_t post_div;
  797. u32 pll_out_min, pll_out_max;
  798. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  799. freq = freq * 1000;
  800. if (pll->flags & RADEON_PLL_IS_LCD) {
  801. pll_out_min = pll->lcd_pll_out_min;
  802. pll_out_max = pll->lcd_pll_out_max;
  803. } else {
  804. pll_out_min = pll->pll_out_min;
  805. pll_out_max = pll->pll_out_max;
  806. }
  807. if (pll_out_min > 64800)
  808. pll_out_min = 64800;
  809. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  810. min_ref_div = max_ref_div = pll->reference_div;
  811. else {
  812. while (min_ref_div < max_ref_div-1) {
  813. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  814. uint32_t pll_in = pll->reference_freq / mid;
  815. if (pll_in < pll->pll_in_min)
  816. max_ref_div = mid;
  817. else if (pll_in > pll->pll_in_max)
  818. min_ref_div = mid;
  819. else
  820. break;
  821. }
  822. }
  823. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  824. min_post_div = max_post_div = pll->post_div;
  825. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  826. min_fractional_feed_div = pll->min_frac_feedback_div;
  827. max_fractional_feed_div = pll->max_frac_feedback_div;
  828. }
  829. for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  830. uint32_t ref_div;
  831. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  832. continue;
  833. /* legacy radeons only have a few post_divs */
  834. if (pll->flags & RADEON_PLL_LEGACY) {
  835. if ((post_div == 5) ||
  836. (post_div == 7) ||
  837. (post_div == 9) ||
  838. (post_div == 10) ||
  839. (post_div == 11) ||
  840. (post_div == 13) ||
  841. (post_div == 14) ||
  842. (post_div == 15))
  843. continue;
  844. }
  845. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  846. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  847. uint32_t pll_in = pll->reference_freq / ref_div;
  848. uint32_t min_feed_div = pll->min_feedback_div;
  849. uint32_t max_feed_div = pll->max_feedback_div + 1;
  850. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  851. continue;
  852. while (min_feed_div < max_feed_div) {
  853. uint32_t vco;
  854. uint32_t min_frac_feed_div = min_fractional_feed_div;
  855. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  856. uint32_t frac_feedback_div;
  857. uint64_t tmp;
  858. feedback_div = (min_feed_div + max_feed_div) / 2;
  859. tmp = (uint64_t)pll->reference_freq * feedback_div;
  860. vco = radeon_div(tmp, ref_div);
  861. if (vco < pll_out_min) {
  862. min_feed_div = feedback_div + 1;
  863. continue;
  864. } else if (vco > pll_out_max) {
  865. max_feed_div = feedback_div;
  866. continue;
  867. }
  868. while (min_frac_feed_div < max_frac_feed_div) {
  869. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  870. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  871. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  872. current_freq = radeon_div(tmp, ref_div * post_div);
  873. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  874. if (freq < current_freq)
  875. error = 0xffffffff;
  876. else
  877. error = freq - current_freq;
  878. } else
  879. error = abs(current_freq - freq);
  880. vco_diff = abs(vco - best_vco);
  881. if ((best_vco == 0 && error < best_error) ||
  882. (best_vco != 0 &&
  883. ((best_error > 100 && error < best_error - 100) ||
  884. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  885. best_post_div = post_div;
  886. best_ref_div = ref_div;
  887. best_feedback_div = feedback_div;
  888. best_frac_feedback_div = frac_feedback_div;
  889. best_freq = current_freq;
  890. best_error = error;
  891. best_vco_diff = vco_diff;
  892. } else if (current_freq == freq) {
  893. if (best_freq == -1) {
  894. best_post_div = post_div;
  895. best_ref_div = ref_div;
  896. best_feedback_div = feedback_div;
  897. best_frac_feedback_div = frac_feedback_div;
  898. best_freq = current_freq;
  899. best_error = error;
  900. best_vco_diff = vco_diff;
  901. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  902. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  903. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  904. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  905. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  906. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  907. best_post_div = post_div;
  908. best_ref_div = ref_div;
  909. best_feedback_div = feedback_div;
  910. best_frac_feedback_div = frac_feedback_div;
  911. best_freq = current_freq;
  912. best_error = error;
  913. best_vco_diff = vco_diff;
  914. }
  915. }
  916. if (current_freq < freq)
  917. min_frac_feed_div = frac_feedback_div + 1;
  918. else
  919. max_frac_feed_div = frac_feedback_div;
  920. }
  921. if (current_freq < freq)
  922. min_feed_div = feedback_div + 1;
  923. else
  924. max_feed_div = feedback_div;
  925. }
  926. }
  927. }
  928. *dot_clock_p = best_freq / 10000;
  929. *fb_div_p = best_feedback_div;
  930. *frac_fb_div_p = best_frac_feedback_div;
  931. *ref_div_p = best_ref_div;
  932. *post_div_p = best_post_div;
  933. DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  934. (long long)freq,
  935. best_freq / 1000, best_feedback_div, best_frac_feedback_div,
  936. best_ref_div, best_post_div);
  937. }
  938. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  939. {
  940. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  941. if (radeon_fb->obj) {
  942. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  943. }
  944. drm_framebuffer_cleanup(fb);
  945. kfree(radeon_fb);
  946. }
  947. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  948. struct drm_file *file_priv,
  949. unsigned int *handle)
  950. {
  951. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  952. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  953. }
  954. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  955. .destroy = radeon_user_framebuffer_destroy,
  956. .create_handle = radeon_user_framebuffer_create_handle,
  957. };
  958. int
  959. radeon_framebuffer_init(struct drm_device *dev,
  960. struct radeon_framebuffer *rfb,
  961. struct drm_mode_fb_cmd2 *mode_cmd,
  962. struct drm_gem_object *obj)
  963. {
  964. int ret;
  965. rfb->obj = obj;
  966. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  967. ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  968. if (ret) {
  969. rfb->obj = NULL;
  970. return ret;
  971. }
  972. return 0;
  973. }
  974. static struct drm_framebuffer *
  975. radeon_user_framebuffer_create(struct drm_device *dev,
  976. struct drm_file *file_priv,
  977. struct drm_mode_fb_cmd2 *mode_cmd)
  978. {
  979. struct drm_gem_object *obj;
  980. struct radeon_framebuffer *radeon_fb;
  981. int ret;
  982. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  983. if (obj == NULL) {
  984. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  985. "can't create framebuffer\n", mode_cmd->handles[0]);
  986. return ERR_PTR(-ENOENT);
  987. }
  988. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  989. if (radeon_fb == NULL) {
  990. drm_gem_object_unreference_unlocked(obj);
  991. return ERR_PTR(-ENOMEM);
  992. }
  993. ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  994. if (ret) {
  995. kfree(radeon_fb);
  996. drm_gem_object_unreference_unlocked(obj);
  997. return ERR_PTR(ret);
  998. }
  999. return &radeon_fb->base;
  1000. }
  1001. static void radeon_output_poll_changed(struct drm_device *dev)
  1002. {
  1003. struct radeon_device *rdev = dev->dev_private;
  1004. radeon_fb_output_poll_changed(rdev);
  1005. }
  1006. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  1007. .fb_create = radeon_user_framebuffer_create,
  1008. .output_poll_changed = radeon_output_poll_changed
  1009. };
  1010. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  1011. { { 0, "driver" },
  1012. { 1, "bios" },
  1013. };
  1014. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  1015. { { TV_STD_NTSC, "ntsc" },
  1016. { TV_STD_PAL, "pal" },
  1017. { TV_STD_PAL_M, "pal-m" },
  1018. { TV_STD_PAL_60, "pal-60" },
  1019. { TV_STD_NTSC_J, "ntsc-j" },
  1020. { TV_STD_SCART_PAL, "scart-pal" },
  1021. { TV_STD_PAL_CN, "pal-cn" },
  1022. { TV_STD_SECAM, "secam" },
  1023. };
  1024. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  1025. { { UNDERSCAN_OFF, "off" },
  1026. { UNDERSCAN_ON, "on" },
  1027. { UNDERSCAN_AUTO, "auto" },
  1028. };
  1029. static int radeon_modeset_create_props(struct radeon_device *rdev)
  1030. {
  1031. int sz;
  1032. if (rdev->is_atom_bios) {
  1033. rdev->mode_info.coherent_mode_property =
  1034. drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
  1035. if (!rdev->mode_info.coherent_mode_property)
  1036. return -ENOMEM;
  1037. }
  1038. if (!ASIC_IS_AVIVO(rdev)) {
  1039. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  1040. rdev->mode_info.tmds_pll_property =
  1041. drm_property_create_enum(rdev->ddev, 0,
  1042. "tmds_pll",
  1043. radeon_tmds_pll_enum_list, sz);
  1044. }
  1045. rdev->mode_info.load_detect_property =
  1046. drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
  1047. if (!rdev->mode_info.load_detect_property)
  1048. return -ENOMEM;
  1049. drm_mode_create_scaling_mode_property(rdev->ddev);
  1050. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  1051. rdev->mode_info.tv_std_property =
  1052. drm_property_create_enum(rdev->ddev, 0,
  1053. "tv standard",
  1054. radeon_tv_std_enum_list, sz);
  1055. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  1056. rdev->mode_info.underscan_property =
  1057. drm_property_create_enum(rdev->ddev, 0,
  1058. "underscan",
  1059. radeon_underscan_enum_list, sz);
  1060. rdev->mode_info.underscan_hborder_property =
  1061. drm_property_create_range(rdev->ddev, 0,
  1062. "underscan hborder", 0, 128);
  1063. if (!rdev->mode_info.underscan_hborder_property)
  1064. return -ENOMEM;
  1065. rdev->mode_info.underscan_vborder_property =
  1066. drm_property_create_range(rdev->ddev, 0,
  1067. "underscan vborder", 0, 128);
  1068. if (!rdev->mode_info.underscan_vborder_property)
  1069. return -ENOMEM;
  1070. return 0;
  1071. }
  1072. void radeon_update_display_priority(struct radeon_device *rdev)
  1073. {
  1074. /* adjustment options for the display watermarks */
  1075. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  1076. /* set display priority to high for r3xx, rv515 chips
  1077. * this avoids flickering due to underflow to the
  1078. * display controllers during heavy acceleration.
  1079. * Don't force high on rs4xx igp chips as it seems to
  1080. * affect the sound card. See kernel bug 15982.
  1081. */
  1082. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  1083. !(rdev->flags & RADEON_IS_IGP))
  1084. rdev->disp_priority = 2;
  1085. else
  1086. rdev->disp_priority = 0;
  1087. } else
  1088. rdev->disp_priority = radeon_disp_priority;
  1089. }
  1090. /*
  1091. * Allocate hdmi structs and determine register offsets
  1092. */
  1093. static void radeon_afmt_init(struct radeon_device *rdev)
  1094. {
  1095. int i;
  1096. for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
  1097. rdev->mode_info.afmt[i] = NULL;
  1098. if (ASIC_IS_DCE6(rdev)) {
  1099. /* todo */
  1100. } else if (ASIC_IS_DCE4(rdev)) {
  1101. /* DCE4/5 has 6 audio blocks tied to DIG encoders */
  1102. /* DCE4.1 has 2 audio blocks tied to DIG encoders */
  1103. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1104. if (rdev->mode_info.afmt[0]) {
  1105. rdev->mode_info.afmt[0]->offset = EVERGREEN_CRTC0_REGISTER_OFFSET;
  1106. rdev->mode_info.afmt[0]->id = 0;
  1107. }
  1108. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1109. if (rdev->mode_info.afmt[1]) {
  1110. rdev->mode_info.afmt[1]->offset = EVERGREEN_CRTC1_REGISTER_OFFSET;
  1111. rdev->mode_info.afmt[1]->id = 1;
  1112. }
  1113. if (!ASIC_IS_DCE41(rdev)) {
  1114. rdev->mode_info.afmt[2] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1115. if (rdev->mode_info.afmt[2]) {
  1116. rdev->mode_info.afmt[2]->offset = EVERGREEN_CRTC2_REGISTER_OFFSET;
  1117. rdev->mode_info.afmt[2]->id = 2;
  1118. }
  1119. rdev->mode_info.afmt[3] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1120. if (rdev->mode_info.afmt[3]) {
  1121. rdev->mode_info.afmt[3]->offset = EVERGREEN_CRTC3_REGISTER_OFFSET;
  1122. rdev->mode_info.afmt[3]->id = 3;
  1123. }
  1124. rdev->mode_info.afmt[4] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1125. if (rdev->mode_info.afmt[4]) {
  1126. rdev->mode_info.afmt[4]->offset = EVERGREEN_CRTC4_REGISTER_OFFSET;
  1127. rdev->mode_info.afmt[4]->id = 4;
  1128. }
  1129. rdev->mode_info.afmt[5] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1130. if (rdev->mode_info.afmt[5]) {
  1131. rdev->mode_info.afmt[5]->offset = EVERGREEN_CRTC5_REGISTER_OFFSET;
  1132. rdev->mode_info.afmt[5]->id = 5;
  1133. }
  1134. }
  1135. } else if (ASIC_IS_DCE3(rdev)) {
  1136. /* DCE3.x has 2 audio blocks tied to DIG encoders */
  1137. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1138. if (rdev->mode_info.afmt[0]) {
  1139. rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
  1140. rdev->mode_info.afmt[0]->id = 0;
  1141. }
  1142. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1143. if (rdev->mode_info.afmt[1]) {
  1144. rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
  1145. rdev->mode_info.afmt[1]->id = 1;
  1146. }
  1147. } else if (ASIC_IS_DCE2(rdev)) {
  1148. /* DCE2 has at least 1 routable audio block */
  1149. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1150. if (rdev->mode_info.afmt[0]) {
  1151. rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
  1152. rdev->mode_info.afmt[0]->id = 0;
  1153. }
  1154. /* r6xx has 2 routable audio blocks */
  1155. if (rdev->family >= CHIP_R600) {
  1156. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1157. if (rdev->mode_info.afmt[1]) {
  1158. rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
  1159. rdev->mode_info.afmt[1]->id = 1;
  1160. }
  1161. }
  1162. }
  1163. }
  1164. static void radeon_afmt_fini(struct radeon_device *rdev)
  1165. {
  1166. int i;
  1167. for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
  1168. kfree(rdev->mode_info.afmt[i]);
  1169. rdev->mode_info.afmt[i] = NULL;
  1170. }
  1171. }
  1172. int radeon_modeset_init(struct radeon_device *rdev)
  1173. {
  1174. int i;
  1175. int ret;
  1176. drm_mode_config_init(rdev->ddev);
  1177. rdev->mode_info.mode_config_initialized = true;
  1178. rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
  1179. if (ASIC_IS_DCE5(rdev)) {
  1180. rdev->ddev->mode_config.max_width = 16384;
  1181. rdev->ddev->mode_config.max_height = 16384;
  1182. } else if (ASIC_IS_AVIVO(rdev)) {
  1183. rdev->ddev->mode_config.max_width = 8192;
  1184. rdev->ddev->mode_config.max_height = 8192;
  1185. } else {
  1186. rdev->ddev->mode_config.max_width = 4096;
  1187. rdev->ddev->mode_config.max_height = 4096;
  1188. }
  1189. rdev->ddev->mode_config.preferred_depth = 24;
  1190. rdev->ddev->mode_config.prefer_shadow = 1;
  1191. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  1192. ret = radeon_modeset_create_props(rdev);
  1193. if (ret) {
  1194. return ret;
  1195. }
  1196. /* init i2c buses */
  1197. radeon_i2c_init(rdev);
  1198. /* check combios for a valid hardcoded EDID - Sun servers */
  1199. if (!rdev->is_atom_bios) {
  1200. /* check for hardcoded EDID in BIOS */
  1201. radeon_combios_check_hardcoded_edid(rdev);
  1202. }
  1203. /* allocate crtcs */
  1204. for (i = 0; i < rdev->num_crtc; i++) {
  1205. radeon_crtc_init(rdev->ddev, i);
  1206. }
  1207. /* okay we should have all the bios connectors */
  1208. ret = radeon_setup_enc_conn(rdev->ddev);
  1209. if (!ret) {
  1210. return ret;
  1211. }
  1212. /* init dig PHYs, disp eng pll */
  1213. if (rdev->is_atom_bios) {
  1214. radeon_atom_encoder_init(rdev);
  1215. radeon_atom_disp_eng_pll_init(rdev);
  1216. }
  1217. /* initialize hpd */
  1218. radeon_hpd_init(rdev);
  1219. /* setup afmt */
  1220. radeon_afmt_init(rdev);
  1221. /* Initialize power management */
  1222. radeon_pm_init(rdev);
  1223. radeon_fbdev_init(rdev);
  1224. drm_kms_helper_poll_init(rdev->ddev);
  1225. return 0;
  1226. }
  1227. void radeon_modeset_fini(struct radeon_device *rdev)
  1228. {
  1229. radeon_fbdev_fini(rdev);
  1230. kfree(rdev->mode_info.bios_hardcoded_edid);
  1231. radeon_pm_fini(rdev);
  1232. if (rdev->mode_info.mode_config_initialized) {
  1233. radeon_afmt_fini(rdev);
  1234. drm_kms_helper_poll_fini(rdev->ddev);
  1235. radeon_hpd_fini(rdev);
  1236. drm_mode_config_cleanup(rdev->ddev);
  1237. rdev->mode_info.mode_config_initialized = false;
  1238. }
  1239. /* free i2c buses */
  1240. radeon_i2c_fini(rdev);
  1241. }
  1242. static bool is_hdtv_mode(const struct drm_display_mode *mode)
  1243. {
  1244. /* try and guess if this is a tv or a monitor */
  1245. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1246. (mode->vdisplay == 576) || /* 576p */
  1247. (mode->vdisplay == 720) || /* 720p */
  1248. (mode->vdisplay == 1080)) /* 1080p */
  1249. return true;
  1250. else
  1251. return false;
  1252. }
  1253. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1254. const struct drm_display_mode *mode,
  1255. struct drm_display_mode *adjusted_mode)
  1256. {
  1257. struct drm_device *dev = crtc->dev;
  1258. struct radeon_device *rdev = dev->dev_private;
  1259. struct drm_encoder *encoder;
  1260. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1261. struct radeon_encoder *radeon_encoder;
  1262. struct drm_connector *connector;
  1263. struct radeon_connector *radeon_connector;
  1264. bool first = true;
  1265. u32 src_v = 1, dst_v = 1;
  1266. u32 src_h = 1, dst_h = 1;
  1267. radeon_crtc->h_border = 0;
  1268. radeon_crtc->v_border = 0;
  1269. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1270. if (encoder->crtc != crtc)
  1271. continue;
  1272. radeon_encoder = to_radeon_encoder(encoder);
  1273. connector = radeon_get_connector_for_encoder(encoder);
  1274. radeon_connector = to_radeon_connector(connector);
  1275. if (first) {
  1276. /* set scaling */
  1277. if (radeon_encoder->rmx_type == RMX_OFF)
  1278. radeon_crtc->rmx_type = RMX_OFF;
  1279. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1280. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1281. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1282. else
  1283. radeon_crtc->rmx_type = RMX_OFF;
  1284. /* copy native mode */
  1285. memcpy(&radeon_crtc->native_mode,
  1286. &radeon_encoder->native_mode,
  1287. sizeof(struct drm_display_mode));
  1288. src_v = crtc->mode.vdisplay;
  1289. dst_v = radeon_crtc->native_mode.vdisplay;
  1290. src_h = crtc->mode.hdisplay;
  1291. dst_h = radeon_crtc->native_mode.hdisplay;
  1292. /* fix up for overscan on hdmi */
  1293. if (ASIC_IS_AVIVO(rdev) &&
  1294. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1295. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1296. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1297. drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1298. is_hdtv_mode(mode)))) {
  1299. if (radeon_encoder->underscan_hborder != 0)
  1300. radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1301. else
  1302. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1303. if (radeon_encoder->underscan_vborder != 0)
  1304. radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1305. else
  1306. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1307. radeon_crtc->rmx_type = RMX_FULL;
  1308. src_v = crtc->mode.vdisplay;
  1309. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1310. src_h = crtc->mode.hdisplay;
  1311. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1312. }
  1313. first = false;
  1314. } else {
  1315. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1316. /* WARNING: Right now this can't happen but
  1317. * in the future we need to check that scaling
  1318. * are consistent across different encoder
  1319. * (ie all encoder can work with the same
  1320. * scaling).
  1321. */
  1322. DRM_ERROR("Scaling not consistent across encoder.\n");
  1323. return false;
  1324. }
  1325. }
  1326. }
  1327. if (radeon_crtc->rmx_type != RMX_OFF) {
  1328. fixed20_12 a, b;
  1329. a.full = dfixed_const(src_v);
  1330. b.full = dfixed_const(dst_v);
  1331. radeon_crtc->vsc.full = dfixed_div(a, b);
  1332. a.full = dfixed_const(src_h);
  1333. b.full = dfixed_const(dst_h);
  1334. radeon_crtc->hsc.full = dfixed_div(a, b);
  1335. } else {
  1336. radeon_crtc->vsc.full = dfixed_const(1);
  1337. radeon_crtc->hsc.full = dfixed_const(1);
  1338. }
  1339. return true;
  1340. }
  1341. /*
  1342. * Retrieve current video scanout position of crtc on a given gpu.
  1343. *
  1344. * \param dev Device to query.
  1345. * \param crtc Crtc to query.
  1346. * \param *vpos Location where vertical scanout position should be stored.
  1347. * \param *hpos Location where horizontal scanout position should go.
  1348. *
  1349. * Returns vpos as a positive number while in active scanout area.
  1350. * Returns vpos as a negative number inside vblank, counting the number
  1351. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1352. * until start of active scanout / end of vblank."
  1353. *
  1354. * \return Flags, or'ed together as follows:
  1355. *
  1356. * DRM_SCANOUTPOS_VALID = Query successful.
  1357. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1358. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1359. * this flag means that returned position may be offset by a constant but
  1360. * unknown small number of scanlines wrt. real scanout position.
  1361. *
  1362. */
  1363. int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, int *vpos, int *hpos)
  1364. {
  1365. u32 stat_crtc = 0, vbl = 0, position = 0;
  1366. int vbl_start, vbl_end, vtotal, ret = 0;
  1367. bool in_vbl = true;
  1368. struct radeon_device *rdev = dev->dev_private;
  1369. if (ASIC_IS_DCE4(rdev)) {
  1370. if (crtc == 0) {
  1371. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1372. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1373. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1374. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1375. ret |= DRM_SCANOUTPOS_VALID;
  1376. }
  1377. if (crtc == 1) {
  1378. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1379. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1380. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1381. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1382. ret |= DRM_SCANOUTPOS_VALID;
  1383. }
  1384. if (crtc == 2) {
  1385. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1386. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1387. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1388. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1389. ret |= DRM_SCANOUTPOS_VALID;
  1390. }
  1391. if (crtc == 3) {
  1392. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1393. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1394. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1395. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1396. ret |= DRM_SCANOUTPOS_VALID;
  1397. }
  1398. if (crtc == 4) {
  1399. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1400. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1401. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1402. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1403. ret |= DRM_SCANOUTPOS_VALID;
  1404. }
  1405. if (crtc == 5) {
  1406. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1407. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1408. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1409. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1410. ret |= DRM_SCANOUTPOS_VALID;
  1411. }
  1412. } else if (ASIC_IS_AVIVO(rdev)) {
  1413. if (crtc == 0) {
  1414. vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1415. position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1416. ret |= DRM_SCANOUTPOS_VALID;
  1417. }
  1418. if (crtc == 1) {
  1419. vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1420. position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1421. ret |= DRM_SCANOUTPOS_VALID;
  1422. }
  1423. } else {
  1424. /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1425. if (crtc == 0) {
  1426. /* Assume vbl_end == 0, get vbl_start from
  1427. * upper 16 bits.
  1428. */
  1429. vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1430. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1431. /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1432. position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1433. stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1434. if (!(stat_crtc & 1))
  1435. in_vbl = false;
  1436. ret |= DRM_SCANOUTPOS_VALID;
  1437. }
  1438. if (crtc == 1) {
  1439. vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1440. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1441. position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1442. stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1443. if (!(stat_crtc & 1))
  1444. in_vbl = false;
  1445. ret |= DRM_SCANOUTPOS_VALID;
  1446. }
  1447. }
  1448. /* Decode into vertical and horizontal scanout position. */
  1449. *vpos = position & 0x1fff;
  1450. *hpos = (position >> 16) & 0x1fff;
  1451. /* Valid vblank area boundaries from gpu retrieved? */
  1452. if (vbl > 0) {
  1453. /* Yes: Decode. */
  1454. ret |= DRM_SCANOUTPOS_ACCURATE;
  1455. vbl_start = vbl & 0x1fff;
  1456. vbl_end = (vbl >> 16) & 0x1fff;
  1457. }
  1458. else {
  1459. /* No: Fake something reasonable which gives at least ok results. */
  1460. vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1461. vbl_end = 0;
  1462. }
  1463. /* Test scanout position against vblank region. */
  1464. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1465. in_vbl = false;
  1466. /* Check if inside vblank area and apply corrective offsets:
  1467. * vpos will then be >=0 in video scanout area, but negative
  1468. * within vblank area, counting down the number of lines until
  1469. * start of scanout.
  1470. */
  1471. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1472. if (in_vbl && (*vpos >= vbl_start)) {
  1473. vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1474. *vpos = *vpos - vtotal;
  1475. }
  1476. /* Correct for shifted end of vbl at vbl_end. */
  1477. *vpos = *vpos - vbl_end;
  1478. /* In vblank? */
  1479. if (in_vbl)
  1480. ret |= DRM_SCANOUTPOS_INVBL;
  1481. return ret;
  1482. }