radeon_display.c 49 KB

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