radeon_display.c 49 KB

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