radeon_display.c 49 KB

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