radeon_display.c 49 KB

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