radeon_display.c 51 KB

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