radeon_display.c 48 KB

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