radeon_display.c 51 KB

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