radeon_display.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026
  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 <linux/pm_runtime.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/drm_plane_helper.h>
  34. #include <drm/drm_edid.h>
  35. #include <linux/gcd.h>
  36. static void avivo_crtc_load_lut(struct drm_crtc *crtc)
  37. {
  38. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  39. struct drm_device *dev = crtc->dev;
  40. struct radeon_device *rdev = dev->dev_private;
  41. int i;
  42. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  43. WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
  44. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  45. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  46. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  47. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  48. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  49. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  50. WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
  51. WREG32(AVIVO_DC_LUT_RW_MODE, 0);
  52. WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
  53. WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
  54. for (i = 0; i < 256; i++) {
  55. WREG32(AVIVO_DC_LUT_30_COLOR,
  56. (radeon_crtc->lut_r[i] << 20) |
  57. (radeon_crtc->lut_g[i] << 10) |
  58. (radeon_crtc->lut_b[i] << 0));
  59. }
  60. /* Only change bit 0 of LUT_SEL, other bits are set elsewhere */
  61. WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id, ~1);
  62. }
  63. static void dce4_crtc_load_lut(struct drm_crtc *crtc)
  64. {
  65. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  66. struct drm_device *dev = crtc->dev;
  67. struct radeon_device *rdev = dev->dev_private;
  68. int i;
  69. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  70. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  71. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  72. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  73. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  74. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  75. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  76. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  77. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  78. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  79. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  80. for (i = 0; i < 256; i++) {
  81. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  82. (radeon_crtc->lut_r[i] << 20) |
  83. (radeon_crtc->lut_g[i] << 10) |
  84. (radeon_crtc->lut_b[i] << 0));
  85. }
  86. }
  87. static void dce5_crtc_load_lut(struct drm_crtc *crtc)
  88. {
  89. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  90. struct drm_device *dev = crtc->dev;
  91. struct radeon_device *rdev = dev->dev_private;
  92. int i;
  93. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  94. WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  95. (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
  96. NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
  97. WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
  98. NI_GRPH_PRESCALE_BYPASS);
  99. WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
  100. NI_OVL_PRESCALE_BYPASS);
  101. WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
  102. (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
  103. NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
  104. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  105. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  106. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  107. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  108. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  109. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  110. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  111. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  112. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  113. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  114. for (i = 0; i < 256; i++) {
  115. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  116. (radeon_crtc->lut_r[i] << 20) |
  117. (radeon_crtc->lut_g[i] << 10) |
  118. (radeon_crtc->lut_b[i] << 0));
  119. }
  120. WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
  121. (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  122. NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  123. NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  124. NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
  125. WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
  126. (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
  127. NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
  128. WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
  129. (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
  130. NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
  131. WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  132. (NI_OUTPUT_CSC_GRPH_MODE(radeon_crtc->output_csc) |
  133. NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
  134. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  135. WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
  136. if (ASIC_IS_DCE8(rdev)) {
  137. /* XXX this only needs to be programmed once per crtc at startup,
  138. * not sure where the best place for it is
  139. */
  140. WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
  141. CIK_CURSOR_ALPHA_BLND_ENA);
  142. }
  143. }
  144. static void legacy_crtc_load_lut(struct drm_crtc *crtc)
  145. {
  146. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  147. struct drm_device *dev = crtc->dev;
  148. struct radeon_device *rdev = dev->dev_private;
  149. int i;
  150. uint32_t dac2_cntl;
  151. dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  152. if (radeon_crtc->crtc_id == 0)
  153. dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  154. else
  155. dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  156. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  157. WREG8(RADEON_PALETTE_INDEX, 0);
  158. for (i = 0; i < 256; i++) {
  159. WREG32(RADEON_PALETTE_30_DATA,
  160. (radeon_crtc->lut_r[i] << 20) |
  161. (radeon_crtc->lut_g[i] << 10) |
  162. (radeon_crtc->lut_b[i] << 0));
  163. }
  164. }
  165. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  166. {
  167. struct drm_device *dev = crtc->dev;
  168. struct radeon_device *rdev = dev->dev_private;
  169. if (!crtc->enabled)
  170. return;
  171. if (ASIC_IS_DCE5(rdev))
  172. dce5_crtc_load_lut(crtc);
  173. else if (ASIC_IS_DCE4(rdev))
  174. dce4_crtc_load_lut(crtc);
  175. else if (ASIC_IS_AVIVO(rdev))
  176. avivo_crtc_load_lut(crtc);
  177. else
  178. legacy_crtc_load_lut(crtc);
  179. }
  180. /** Sets the color ramps on behalf of fbcon */
  181. void radeon_crtc_fb_gamma_set(struct drm_crtc *crtc, u16 red, u16 green,
  182. u16 blue, int regno)
  183. {
  184. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  185. radeon_crtc->lut_r[regno] = red >> 6;
  186. radeon_crtc->lut_g[regno] = green >> 6;
  187. radeon_crtc->lut_b[regno] = blue >> 6;
  188. }
  189. /** Gets the color ramps on behalf of fbcon */
  190. void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  191. u16 *blue, int regno)
  192. {
  193. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  194. *red = radeon_crtc->lut_r[regno] << 6;
  195. *green = radeon_crtc->lut_g[regno] << 6;
  196. *blue = radeon_crtc->lut_b[regno] << 6;
  197. }
  198. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  199. u16 *blue, uint32_t start, uint32_t size)
  200. {
  201. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  202. int end = (start + size > 256) ? 256 : start + size, i;
  203. /* userspace palettes are always correct as is */
  204. for (i = start; i < end; i++) {
  205. radeon_crtc->lut_r[i] = red[i] >> 6;
  206. radeon_crtc->lut_g[i] = green[i] >> 6;
  207. radeon_crtc->lut_b[i] = blue[i] >> 6;
  208. }
  209. radeon_crtc_load_lut(crtc);
  210. }
  211. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  212. {
  213. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  214. drm_crtc_cleanup(crtc);
  215. destroy_workqueue(radeon_crtc->flip_queue);
  216. kfree(radeon_crtc);
  217. }
  218. /**
  219. * radeon_unpin_work_func - unpin old buffer object
  220. *
  221. * @__work - kernel work item
  222. *
  223. * Unpin the old frame buffer object outside of the interrupt handler
  224. */
  225. static void radeon_unpin_work_func(struct work_struct *__work)
  226. {
  227. struct radeon_flip_work *work =
  228. container_of(__work, struct radeon_flip_work, unpin_work);
  229. int r;
  230. /* unpin of the old buffer */
  231. r = radeon_bo_reserve(work->old_rbo, false);
  232. if (likely(r == 0)) {
  233. r = radeon_bo_unpin(work->old_rbo);
  234. if (unlikely(r != 0)) {
  235. DRM_ERROR("failed to unpin buffer after flip\n");
  236. }
  237. radeon_bo_unreserve(work->old_rbo);
  238. } else
  239. DRM_ERROR("failed to reserve buffer after flip\n");
  240. drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
  241. kfree(work);
  242. }
  243. void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
  244. {
  245. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  246. unsigned long flags;
  247. u32 update_pending;
  248. int vpos, hpos;
  249. /* can happen during initialization */
  250. if (radeon_crtc == NULL)
  251. return;
  252. /* Skip the pageflip completion check below (based on polling) on
  253. * asics which reliably support hw pageflip completion irqs. pflip
  254. * irqs are a reliable and race-free method of handling pageflip
  255. * completion detection. A use_pflipirq module parameter < 2 allows
  256. * to override this in case of asics with faulty pflip irqs.
  257. * A module parameter of 0 would only use this polling based path,
  258. * a parameter of 1 would use pflip irq only as a backup to this
  259. * path, as in Linux 3.16.
  260. */
  261. if ((radeon_use_pflipirq == 2) && ASIC_IS_DCE4(rdev))
  262. return;
  263. spin_lock_irqsave(&rdev->ddev->event_lock, flags);
  264. if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
  265. DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
  266. "RADEON_FLIP_SUBMITTED(%d)\n",
  267. radeon_crtc->flip_status,
  268. RADEON_FLIP_SUBMITTED);
  269. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  270. return;
  271. }
  272. update_pending = radeon_page_flip_pending(rdev, crtc_id);
  273. /* Has the pageflip already completed in crtc, or is it certain
  274. * to complete in this vblank?
  275. */
  276. if (update_pending &&
  277. (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev,
  278. crtc_id,
  279. USE_REAL_VBLANKSTART,
  280. &vpos, &hpos, NULL, NULL,
  281. &rdev->mode_info.crtcs[crtc_id]->base.hwmode)) &&
  282. ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
  283. (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
  284. /* crtc didn't flip in this target vblank interval,
  285. * but flip is pending in crtc. Based on the current
  286. * scanout position we know that the current frame is
  287. * (nearly) complete and the flip will (likely)
  288. * complete before the start of the next frame.
  289. */
  290. update_pending = 0;
  291. }
  292. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  293. if (!update_pending)
  294. radeon_crtc_handle_flip(rdev, crtc_id);
  295. }
  296. /**
  297. * radeon_crtc_handle_flip - page flip completed
  298. *
  299. * @rdev: radeon device pointer
  300. * @crtc_id: crtc number this event is for
  301. *
  302. * Called when we are sure that a page flip for this crtc is completed.
  303. */
  304. void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
  305. {
  306. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  307. struct radeon_flip_work *work;
  308. unsigned long flags;
  309. /* this can happen at init */
  310. if (radeon_crtc == NULL)
  311. return;
  312. spin_lock_irqsave(&rdev->ddev->event_lock, flags);
  313. work = radeon_crtc->flip_work;
  314. if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
  315. DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
  316. "RADEON_FLIP_SUBMITTED(%d)\n",
  317. radeon_crtc->flip_status,
  318. RADEON_FLIP_SUBMITTED);
  319. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  320. return;
  321. }
  322. /* Pageflip completed. Clean up. */
  323. radeon_crtc->flip_status = RADEON_FLIP_NONE;
  324. radeon_crtc->flip_work = NULL;
  325. /* wakeup userspace */
  326. if (work->event)
  327. drm_crtc_send_vblank_event(&radeon_crtc->base, work->event);
  328. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  329. drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
  330. radeon_irq_kms_pflip_irq_put(rdev, work->crtc_id);
  331. queue_work(radeon_crtc->flip_queue, &work->unpin_work);
  332. }
  333. /**
  334. * radeon_flip_work_func - page flip framebuffer
  335. *
  336. * @work - kernel work item
  337. *
  338. * Wait for the buffer object to become idle and do the actual page flip
  339. */
  340. static void radeon_flip_work_func(struct work_struct *__work)
  341. {
  342. struct radeon_flip_work *work =
  343. container_of(__work, struct radeon_flip_work, flip_work);
  344. struct radeon_device *rdev = work->rdev;
  345. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[work->crtc_id];
  346. struct drm_crtc *crtc = &radeon_crtc->base;
  347. unsigned long flags;
  348. int r;
  349. int vpos, hpos, stat, min_udelay = 0;
  350. unsigned repcnt = 4;
  351. struct drm_vblank_crtc *vblank = &crtc->dev->vblank[work->crtc_id];
  352. down_read(&rdev->exclusive_lock);
  353. if (work->fence) {
  354. struct radeon_fence *fence;
  355. fence = to_radeon_fence(work->fence);
  356. if (fence && fence->rdev == rdev) {
  357. r = radeon_fence_wait(fence, false);
  358. if (r == -EDEADLK) {
  359. up_read(&rdev->exclusive_lock);
  360. do {
  361. r = radeon_gpu_reset(rdev);
  362. } while (r == -EAGAIN);
  363. down_read(&rdev->exclusive_lock);
  364. }
  365. } else
  366. r = fence_wait(work->fence, false);
  367. if (r)
  368. DRM_ERROR("failed to wait on page flip fence (%d)!\n", r);
  369. /* We continue with the page flip even if we failed to wait on
  370. * the fence, otherwise the DRM core and userspace will be
  371. * confused about which BO the CRTC is scanning out
  372. */
  373. fence_put(work->fence);
  374. work->fence = NULL;
  375. }
  376. /* We borrow the event spin lock for protecting flip_status */
  377. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  378. /* set the proper interrupt */
  379. radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
  380. /* If this happens to execute within the "virtually extended" vblank
  381. * interval before the start of the real vblank interval then it needs
  382. * to delay programming the mmio flip until the real vblank is entered.
  383. * This prevents completing a flip too early due to the way we fudge
  384. * our vblank counter and vblank timestamps in order to work around the
  385. * problem that the hw fires vblank interrupts before actual start of
  386. * vblank (when line buffer refilling is done for a frame). It
  387. * complements the fudging logic in radeon_get_crtc_scanoutpos() for
  388. * timestamping and radeon_get_vblank_counter_kms() for vblank counts.
  389. *
  390. * In practice this won't execute very often unless on very fast
  391. * machines because the time window for this to happen is very small.
  392. */
  393. while (radeon_crtc->enabled && --repcnt) {
  394. /* GET_DISTANCE_TO_VBLANKSTART returns distance to real vblank
  395. * start in hpos, and to the "fudged earlier" vblank start in
  396. * vpos.
  397. */
  398. stat = radeon_get_crtc_scanoutpos(rdev->ddev, work->crtc_id,
  399. GET_DISTANCE_TO_VBLANKSTART,
  400. &vpos, &hpos, NULL, NULL,
  401. &crtc->hwmode);
  402. if ((stat & (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE)) !=
  403. (DRM_SCANOUTPOS_VALID | DRM_SCANOUTPOS_ACCURATE) ||
  404. !(vpos >= 0 && hpos <= 0))
  405. break;
  406. /* Sleep at least until estimated real start of hw vblank */
  407. min_udelay = (-hpos + 1) * max(vblank->linedur_ns / 1000, 5);
  408. if (min_udelay > vblank->framedur_ns / 2000) {
  409. /* Don't wait ridiculously long - something is wrong */
  410. repcnt = 0;
  411. break;
  412. }
  413. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  414. usleep_range(min_udelay, 2 * min_udelay);
  415. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  416. };
  417. if (!repcnt)
  418. DRM_DEBUG_DRIVER("Delay problem on crtc %d: min_udelay %d, "
  419. "framedur %d, linedur %d, stat %d, vpos %d, "
  420. "hpos %d\n", work->crtc_id, min_udelay,
  421. vblank->framedur_ns / 1000,
  422. vblank->linedur_ns / 1000, stat, vpos, hpos);
  423. /* do the flip (mmio) */
  424. radeon_page_flip(rdev, radeon_crtc->crtc_id, work->base);
  425. radeon_crtc->flip_status = RADEON_FLIP_SUBMITTED;
  426. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  427. up_read(&rdev->exclusive_lock);
  428. }
  429. static int radeon_crtc_page_flip(struct drm_crtc *crtc,
  430. struct drm_framebuffer *fb,
  431. struct drm_pending_vblank_event *event,
  432. uint32_t page_flip_flags)
  433. {
  434. struct drm_device *dev = crtc->dev;
  435. struct radeon_device *rdev = dev->dev_private;
  436. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  437. struct radeon_framebuffer *old_radeon_fb;
  438. struct radeon_framebuffer *new_radeon_fb;
  439. struct drm_gem_object *obj;
  440. struct radeon_flip_work *work;
  441. struct radeon_bo *new_rbo;
  442. uint32_t tiling_flags, pitch_pixels;
  443. uint64_t base;
  444. unsigned long flags;
  445. int r;
  446. work = kzalloc(sizeof *work, GFP_KERNEL);
  447. if (work == NULL)
  448. return -ENOMEM;
  449. INIT_WORK(&work->flip_work, radeon_flip_work_func);
  450. INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
  451. work->rdev = rdev;
  452. work->crtc_id = radeon_crtc->crtc_id;
  453. work->event = event;
  454. /* schedule unpin of the old buffer */
  455. old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
  456. obj = old_radeon_fb->obj;
  457. /* take a reference to the old object */
  458. drm_gem_object_reference(obj);
  459. work->old_rbo = gem_to_radeon_bo(obj);
  460. new_radeon_fb = to_radeon_framebuffer(fb);
  461. obj = new_radeon_fb->obj;
  462. new_rbo = gem_to_radeon_bo(obj);
  463. /* pin the new buffer */
  464. DRM_DEBUG_DRIVER("flip-ioctl() cur_rbo = %p, new_rbo = %p\n",
  465. work->old_rbo, new_rbo);
  466. r = radeon_bo_reserve(new_rbo, false);
  467. if (unlikely(r != 0)) {
  468. DRM_ERROR("failed to reserve new rbo buffer before flip\n");
  469. goto cleanup;
  470. }
  471. /* Only 27 bit offset for legacy CRTC */
  472. r = radeon_bo_pin_restricted(new_rbo, RADEON_GEM_DOMAIN_VRAM,
  473. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
  474. if (unlikely(r != 0)) {
  475. radeon_bo_unreserve(new_rbo);
  476. r = -EINVAL;
  477. DRM_ERROR("failed to pin new rbo buffer before flip\n");
  478. goto cleanup;
  479. }
  480. work->fence = fence_get(reservation_object_get_excl(new_rbo->tbo.resv));
  481. radeon_bo_get_tiling_flags(new_rbo, &tiling_flags, NULL);
  482. radeon_bo_unreserve(new_rbo);
  483. if (!ASIC_IS_AVIVO(rdev)) {
  484. /* crtc offset is from display base addr not FB location */
  485. base -= radeon_crtc->legacy_display_base_addr;
  486. pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
  487. if (tiling_flags & RADEON_TILING_MACRO) {
  488. if (ASIC_IS_R300(rdev)) {
  489. base &= ~0x7ff;
  490. } else {
  491. int byteshift = fb->bits_per_pixel >> 4;
  492. int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
  493. base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
  494. }
  495. } else {
  496. int offset = crtc->y * pitch_pixels + crtc->x;
  497. switch (fb->bits_per_pixel) {
  498. case 8:
  499. default:
  500. offset *= 1;
  501. break;
  502. case 15:
  503. case 16:
  504. offset *= 2;
  505. break;
  506. case 24:
  507. offset *= 3;
  508. break;
  509. case 32:
  510. offset *= 4;
  511. break;
  512. }
  513. base += offset;
  514. }
  515. base &= ~7;
  516. }
  517. work->base = base;
  518. r = drm_vblank_get(crtc->dev, radeon_crtc->crtc_id);
  519. if (r) {
  520. DRM_ERROR("failed to get vblank before flip\n");
  521. goto pflip_cleanup;
  522. }
  523. /* We borrow the event spin lock for protecting flip_work */
  524. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  525. if (radeon_crtc->flip_status != RADEON_FLIP_NONE) {
  526. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  527. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  528. r = -EBUSY;
  529. goto vblank_cleanup;
  530. }
  531. radeon_crtc->flip_status = RADEON_FLIP_PENDING;
  532. radeon_crtc->flip_work = work;
  533. /* update crtc fb */
  534. crtc->primary->fb = fb;
  535. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  536. queue_work(radeon_crtc->flip_queue, &work->flip_work);
  537. return 0;
  538. vblank_cleanup:
  539. drm_vblank_put(crtc->dev, radeon_crtc->crtc_id);
  540. pflip_cleanup:
  541. if (unlikely(radeon_bo_reserve(new_rbo, false) != 0)) {
  542. DRM_ERROR("failed to reserve new rbo in error path\n");
  543. goto cleanup;
  544. }
  545. if (unlikely(radeon_bo_unpin(new_rbo) != 0)) {
  546. DRM_ERROR("failed to unpin new rbo in error path\n");
  547. }
  548. radeon_bo_unreserve(new_rbo);
  549. cleanup:
  550. drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
  551. fence_put(work->fence);
  552. kfree(work);
  553. return r;
  554. }
  555. static int
  556. radeon_crtc_set_config(struct drm_mode_set *set)
  557. {
  558. struct drm_device *dev;
  559. struct radeon_device *rdev;
  560. struct drm_crtc *crtc;
  561. bool active = false;
  562. int ret;
  563. if (!set || !set->crtc)
  564. return -EINVAL;
  565. dev = set->crtc->dev;
  566. ret = pm_runtime_get_sync(dev->dev);
  567. if (ret < 0)
  568. return ret;
  569. ret = drm_crtc_helper_set_config(set);
  570. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  571. if (crtc->enabled)
  572. active = true;
  573. pm_runtime_mark_last_busy(dev->dev);
  574. rdev = dev->dev_private;
  575. /* if we have active crtcs and we don't have a power ref,
  576. take the current one */
  577. if (active && !rdev->have_disp_power_ref) {
  578. rdev->have_disp_power_ref = true;
  579. return ret;
  580. }
  581. /* if we have no active crtcs, then drop the power ref
  582. we got before */
  583. if (!active && rdev->have_disp_power_ref) {
  584. pm_runtime_put_autosuspend(dev->dev);
  585. rdev->have_disp_power_ref = false;
  586. }
  587. /* drop the power reference we got coming in here */
  588. pm_runtime_put_autosuspend(dev->dev);
  589. return ret;
  590. }
  591. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  592. .cursor_set2 = radeon_crtc_cursor_set2,
  593. .cursor_move = radeon_crtc_cursor_move,
  594. .gamma_set = radeon_crtc_gamma_set,
  595. .set_config = radeon_crtc_set_config,
  596. .destroy = radeon_crtc_destroy,
  597. .page_flip = radeon_crtc_page_flip,
  598. };
  599. static void radeon_crtc_init(struct drm_device *dev, int index)
  600. {
  601. struct radeon_device *rdev = dev->dev_private;
  602. struct radeon_crtc *radeon_crtc;
  603. int i;
  604. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  605. if (radeon_crtc == NULL)
  606. return;
  607. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  608. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  609. radeon_crtc->crtc_id = index;
  610. radeon_crtc->flip_queue = create_singlethread_workqueue("radeon-crtc");
  611. rdev->mode_info.crtcs[index] = radeon_crtc;
  612. if (rdev->family >= CHIP_BONAIRE) {
  613. radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
  614. radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
  615. } else {
  616. radeon_crtc->max_cursor_width = CURSOR_WIDTH;
  617. radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
  618. }
  619. dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
  620. dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
  621. #if 0
  622. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  623. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  624. radeon_crtc->mode_set.num_connectors = 0;
  625. #endif
  626. for (i = 0; i < 256; i++) {
  627. radeon_crtc->lut_r[i] = i << 2;
  628. radeon_crtc->lut_g[i] = i << 2;
  629. radeon_crtc->lut_b[i] = i << 2;
  630. }
  631. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  632. radeon_atombios_init_crtc(dev, radeon_crtc);
  633. else
  634. radeon_legacy_init_crtc(dev, radeon_crtc);
  635. }
  636. static const char *encoder_names[38] = {
  637. "NONE",
  638. "INTERNAL_LVDS",
  639. "INTERNAL_TMDS1",
  640. "INTERNAL_TMDS2",
  641. "INTERNAL_DAC1",
  642. "INTERNAL_DAC2",
  643. "INTERNAL_SDVOA",
  644. "INTERNAL_SDVOB",
  645. "SI170B",
  646. "CH7303",
  647. "CH7301",
  648. "INTERNAL_DVO1",
  649. "EXTERNAL_SDVOA",
  650. "EXTERNAL_SDVOB",
  651. "TITFP513",
  652. "INTERNAL_LVTM1",
  653. "VT1623",
  654. "HDMI_SI1930",
  655. "HDMI_INTERNAL",
  656. "INTERNAL_KLDSCP_TMDS1",
  657. "INTERNAL_KLDSCP_DVO1",
  658. "INTERNAL_KLDSCP_DAC1",
  659. "INTERNAL_KLDSCP_DAC2",
  660. "SI178",
  661. "MVPU_FPGA",
  662. "INTERNAL_DDI",
  663. "VT1625",
  664. "HDMI_SI1932",
  665. "DP_AN9801",
  666. "DP_DP501",
  667. "INTERNAL_UNIPHY",
  668. "INTERNAL_KLDSCP_LVTMA",
  669. "INTERNAL_UNIPHY1",
  670. "INTERNAL_UNIPHY2",
  671. "NUTMEG",
  672. "TRAVIS",
  673. "INTERNAL_VCE",
  674. "INTERNAL_UNIPHY3",
  675. };
  676. static const char *hpd_names[6] = {
  677. "HPD1",
  678. "HPD2",
  679. "HPD3",
  680. "HPD4",
  681. "HPD5",
  682. "HPD6",
  683. };
  684. static void radeon_print_display_setup(struct drm_device *dev)
  685. {
  686. struct drm_connector *connector;
  687. struct radeon_connector *radeon_connector;
  688. struct drm_encoder *encoder;
  689. struct radeon_encoder *radeon_encoder;
  690. uint32_t devices;
  691. int i = 0;
  692. DRM_INFO("Radeon Display Connectors\n");
  693. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  694. radeon_connector = to_radeon_connector(connector);
  695. DRM_INFO("Connector %d:\n", i);
  696. DRM_INFO(" %s\n", connector->name);
  697. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  698. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  699. if (radeon_connector->ddc_bus) {
  700. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  701. radeon_connector->ddc_bus->rec.mask_clk_reg,
  702. radeon_connector->ddc_bus->rec.mask_data_reg,
  703. radeon_connector->ddc_bus->rec.a_clk_reg,
  704. radeon_connector->ddc_bus->rec.a_data_reg,
  705. radeon_connector->ddc_bus->rec.en_clk_reg,
  706. radeon_connector->ddc_bus->rec.en_data_reg,
  707. radeon_connector->ddc_bus->rec.y_clk_reg,
  708. radeon_connector->ddc_bus->rec.y_data_reg);
  709. if (radeon_connector->router.ddc_valid)
  710. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  711. radeon_connector->router.ddc_mux_control_pin,
  712. radeon_connector->router.ddc_mux_state);
  713. if (radeon_connector->router.cd_valid)
  714. DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
  715. radeon_connector->router.cd_mux_control_pin,
  716. radeon_connector->router.cd_mux_state);
  717. } else {
  718. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  719. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  720. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  721. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  722. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  723. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  724. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  725. }
  726. DRM_INFO(" Encoders:\n");
  727. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  728. radeon_encoder = to_radeon_encoder(encoder);
  729. devices = radeon_encoder->devices & radeon_connector->devices;
  730. if (devices) {
  731. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  732. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  733. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  734. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  735. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  736. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  737. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  738. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  739. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  740. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  741. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  742. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  743. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  744. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  745. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  746. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  747. if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  748. DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
  749. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  750. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  751. if (devices & ATOM_DEVICE_CV_SUPPORT)
  752. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  753. }
  754. }
  755. i++;
  756. }
  757. }
  758. static bool radeon_setup_enc_conn(struct drm_device *dev)
  759. {
  760. struct radeon_device *rdev = dev->dev_private;
  761. bool ret = false;
  762. if (rdev->bios) {
  763. if (rdev->is_atom_bios) {
  764. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  765. if (ret == false)
  766. ret = radeon_get_atom_connector_info_from_object_table(dev);
  767. } else {
  768. ret = radeon_get_legacy_connector_info_from_bios(dev);
  769. if (ret == false)
  770. ret = radeon_get_legacy_connector_info_from_table(dev);
  771. }
  772. } else {
  773. if (!ASIC_IS_AVIVO(rdev))
  774. ret = radeon_get_legacy_connector_info_from_table(dev);
  775. }
  776. if (ret) {
  777. radeon_setup_encoder_clones(dev);
  778. radeon_print_display_setup(dev);
  779. }
  780. return ret;
  781. }
  782. /* avivo */
  783. /**
  784. * avivo_reduce_ratio - fractional number reduction
  785. *
  786. * @nom: nominator
  787. * @den: denominator
  788. * @nom_min: minimum value for nominator
  789. * @den_min: minimum value for denominator
  790. *
  791. * Find the greatest common divisor and apply it on both nominator and
  792. * denominator, but make nominator and denominator are at least as large
  793. * as their minimum values.
  794. */
  795. static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
  796. unsigned nom_min, unsigned den_min)
  797. {
  798. unsigned tmp;
  799. /* reduce the numbers to a simpler ratio */
  800. tmp = gcd(*nom, *den);
  801. *nom /= tmp;
  802. *den /= tmp;
  803. /* make sure nominator is large enough */
  804. if (*nom < nom_min) {
  805. tmp = DIV_ROUND_UP(nom_min, *nom);
  806. *nom *= tmp;
  807. *den *= tmp;
  808. }
  809. /* make sure the denominator is large enough */
  810. if (*den < den_min) {
  811. tmp = DIV_ROUND_UP(den_min, *den);
  812. *nom *= tmp;
  813. *den *= tmp;
  814. }
  815. }
  816. /**
  817. * avivo_get_fb_ref_div - feedback and ref divider calculation
  818. *
  819. * @nom: nominator
  820. * @den: denominator
  821. * @post_div: post divider
  822. * @fb_div_max: feedback divider maximum
  823. * @ref_div_max: reference divider maximum
  824. * @fb_div: resulting feedback divider
  825. * @ref_div: resulting reference divider
  826. *
  827. * Calculate feedback and reference divider for a given post divider. Makes
  828. * sure we stay within the limits.
  829. */
  830. static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
  831. unsigned fb_div_max, unsigned ref_div_max,
  832. unsigned *fb_div, unsigned *ref_div)
  833. {
  834. /* limit reference * post divider to a maximum */
  835. ref_div_max = max(min(100 / post_div, ref_div_max), 1u);
  836. /* get matching reference and feedback divider */
  837. *ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
  838. *fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
  839. /* limit fb divider to its maximum */
  840. if (*fb_div > fb_div_max) {
  841. *ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
  842. *fb_div = fb_div_max;
  843. }
  844. }
  845. /**
  846. * radeon_compute_pll_avivo - compute PLL paramaters
  847. *
  848. * @pll: information about the PLL
  849. * @dot_clock_p: resulting pixel clock
  850. * fb_div_p: resulting feedback divider
  851. * frac_fb_div_p: fractional part of the feedback divider
  852. * ref_div_p: resulting reference divider
  853. * post_div_p: resulting reference divider
  854. *
  855. * Try to calculate the PLL parameters to generate the given frequency:
  856. * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
  857. */
  858. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  859. u32 freq,
  860. u32 *dot_clock_p,
  861. u32 *fb_div_p,
  862. u32 *frac_fb_div_p,
  863. u32 *ref_div_p,
  864. u32 *post_div_p)
  865. {
  866. unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
  867. freq : freq / 10;
  868. unsigned fb_div_min, fb_div_max, fb_div;
  869. unsigned post_div_min, post_div_max, post_div;
  870. unsigned ref_div_min, ref_div_max, ref_div;
  871. unsigned post_div_best, diff_best;
  872. unsigned nom, den;
  873. /* determine allowed feedback divider range */
  874. fb_div_min = pll->min_feedback_div;
  875. fb_div_max = pll->max_feedback_div;
  876. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  877. fb_div_min *= 10;
  878. fb_div_max *= 10;
  879. }
  880. /* determine allowed ref divider range */
  881. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  882. ref_div_min = pll->reference_div;
  883. else
  884. ref_div_min = pll->min_ref_div;
  885. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
  886. pll->flags & RADEON_PLL_USE_REF_DIV)
  887. ref_div_max = pll->reference_div;
  888. else if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
  889. /* fix for problems on RS880 */
  890. ref_div_max = min(pll->max_ref_div, 7u);
  891. else
  892. ref_div_max = pll->max_ref_div;
  893. /* determine allowed post divider range */
  894. if (pll->flags & RADEON_PLL_USE_POST_DIV) {
  895. post_div_min = pll->post_div;
  896. post_div_max = pll->post_div;
  897. } else {
  898. unsigned vco_min, vco_max;
  899. if (pll->flags & RADEON_PLL_IS_LCD) {
  900. vco_min = pll->lcd_pll_out_min;
  901. vco_max = pll->lcd_pll_out_max;
  902. } else {
  903. vco_min = pll->pll_out_min;
  904. vco_max = pll->pll_out_max;
  905. }
  906. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  907. vco_min *= 10;
  908. vco_max *= 10;
  909. }
  910. post_div_min = vco_min / target_clock;
  911. if ((target_clock * post_div_min) < vco_min)
  912. ++post_div_min;
  913. if (post_div_min < pll->min_post_div)
  914. post_div_min = pll->min_post_div;
  915. post_div_max = vco_max / target_clock;
  916. if ((target_clock * post_div_max) > vco_max)
  917. --post_div_max;
  918. if (post_div_max > pll->max_post_div)
  919. post_div_max = pll->max_post_div;
  920. }
  921. /* represent the searched ratio as fractional number */
  922. nom = target_clock;
  923. den = pll->reference_freq;
  924. /* reduce the numbers to a simpler ratio */
  925. avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
  926. /* now search for a post divider */
  927. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
  928. post_div_best = post_div_min;
  929. else
  930. post_div_best = post_div_max;
  931. diff_best = ~0;
  932. for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
  933. unsigned diff;
  934. avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
  935. ref_div_max, &fb_div, &ref_div);
  936. diff = abs(target_clock - (pll->reference_freq * fb_div) /
  937. (ref_div * post_div));
  938. if (diff < diff_best || (diff == diff_best &&
  939. !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
  940. post_div_best = post_div;
  941. diff_best = diff;
  942. }
  943. }
  944. post_div = post_div_best;
  945. /* get the feedback and reference divider for the optimal value */
  946. avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
  947. &fb_div, &ref_div);
  948. /* reduce the numbers to a simpler ratio once more */
  949. /* this also makes sure that the reference divider is large enough */
  950. avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
  951. /* avoid high jitter with small fractional dividers */
  952. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV && (fb_div % 10)) {
  953. fb_div_min = max(fb_div_min, (9 - (fb_div % 10)) * 20 + 50);
  954. if (fb_div < fb_div_min) {
  955. unsigned tmp = DIV_ROUND_UP(fb_div_min, fb_div);
  956. fb_div *= tmp;
  957. ref_div *= tmp;
  958. }
  959. }
  960. /* and finally save the result */
  961. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  962. *fb_div_p = fb_div / 10;
  963. *frac_fb_div_p = fb_div % 10;
  964. } else {
  965. *fb_div_p = fb_div;
  966. *frac_fb_div_p = 0;
  967. }
  968. *dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
  969. (pll->reference_freq * *frac_fb_div_p)) /
  970. (ref_div * post_div * 10);
  971. *ref_div_p = ref_div;
  972. *post_div_p = post_div;
  973. DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  974. freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
  975. ref_div, post_div);
  976. }
  977. /* pre-avivo */
  978. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  979. {
  980. uint64_t mod;
  981. n += d / 2;
  982. mod = do_div(n, d);
  983. return n;
  984. }
  985. void radeon_compute_pll_legacy(struct radeon_pll *pll,
  986. uint64_t freq,
  987. uint32_t *dot_clock_p,
  988. uint32_t *fb_div_p,
  989. uint32_t *frac_fb_div_p,
  990. uint32_t *ref_div_p,
  991. uint32_t *post_div_p)
  992. {
  993. uint32_t min_ref_div = pll->min_ref_div;
  994. uint32_t max_ref_div = pll->max_ref_div;
  995. uint32_t min_post_div = pll->min_post_div;
  996. uint32_t max_post_div = pll->max_post_div;
  997. uint32_t min_fractional_feed_div = 0;
  998. uint32_t max_fractional_feed_div = 0;
  999. uint32_t best_vco = pll->best_vco;
  1000. uint32_t best_post_div = 1;
  1001. uint32_t best_ref_div = 1;
  1002. uint32_t best_feedback_div = 1;
  1003. uint32_t best_frac_feedback_div = 0;
  1004. uint32_t best_freq = -1;
  1005. uint32_t best_error = 0xffffffff;
  1006. uint32_t best_vco_diff = 1;
  1007. uint32_t post_div;
  1008. u32 pll_out_min, pll_out_max;
  1009. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  1010. freq = freq * 1000;
  1011. if (pll->flags & RADEON_PLL_IS_LCD) {
  1012. pll_out_min = pll->lcd_pll_out_min;
  1013. pll_out_max = pll->lcd_pll_out_max;
  1014. } else {
  1015. pll_out_min = pll->pll_out_min;
  1016. pll_out_max = pll->pll_out_max;
  1017. }
  1018. if (pll_out_min > 64800)
  1019. pll_out_min = 64800;
  1020. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  1021. min_ref_div = max_ref_div = pll->reference_div;
  1022. else {
  1023. while (min_ref_div < max_ref_div-1) {
  1024. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  1025. uint32_t pll_in = pll->reference_freq / mid;
  1026. if (pll_in < pll->pll_in_min)
  1027. max_ref_div = mid;
  1028. else if (pll_in > pll->pll_in_max)
  1029. min_ref_div = mid;
  1030. else
  1031. break;
  1032. }
  1033. }
  1034. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  1035. min_post_div = max_post_div = pll->post_div;
  1036. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  1037. min_fractional_feed_div = pll->min_frac_feedback_div;
  1038. max_fractional_feed_div = pll->max_frac_feedback_div;
  1039. }
  1040. for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  1041. uint32_t ref_div;
  1042. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  1043. continue;
  1044. /* legacy radeons only have a few post_divs */
  1045. if (pll->flags & RADEON_PLL_LEGACY) {
  1046. if ((post_div == 5) ||
  1047. (post_div == 7) ||
  1048. (post_div == 9) ||
  1049. (post_div == 10) ||
  1050. (post_div == 11) ||
  1051. (post_div == 13) ||
  1052. (post_div == 14) ||
  1053. (post_div == 15))
  1054. continue;
  1055. }
  1056. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  1057. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  1058. uint32_t pll_in = pll->reference_freq / ref_div;
  1059. uint32_t min_feed_div = pll->min_feedback_div;
  1060. uint32_t max_feed_div = pll->max_feedback_div + 1;
  1061. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  1062. continue;
  1063. while (min_feed_div < max_feed_div) {
  1064. uint32_t vco;
  1065. uint32_t min_frac_feed_div = min_fractional_feed_div;
  1066. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  1067. uint32_t frac_feedback_div;
  1068. uint64_t tmp;
  1069. feedback_div = (min_feed_div + max_feed_div) / 2;
  1070. tmp = (uint64_t)pll->reference_freq * feedback_div;
  1071. vco = radeon_div(tmp, ref_div);
  1072. if (vco < pll_out_min) {
  1073. min_feed_div = feedback_div + 1;
  1074. continue;
  1075. } else if (vco > pll_out_max) {
  1076. max_feed_div = feedback_div;
  1077. continue;
  1078. }
  1079. while (min_frac_feed_div < max_frac_feed_div) {
  1080. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  1081. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  1082. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  1083. current_freq = radeon_div(tmp, ref_div * post_div);
  1084. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  1085. if (freq < current_freq)
  1086. error = 0xffffffff;
  1087. else
  1088. error = freq - current_freq;
  1089. } else
  1090. error = abs(current_freq - freq);
  1091. vco_diff = abs(vco - best_vco);
  1092. if ((best_vco == 0 && error < best_error) ||
  1093. (best_vco != 0 &&
  1094. ((best_error > 100 && error < best_error - 100) ||
  1095. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  1096. best_post_div = post_div;
  1097. best_ref_div = ref_div;
  1098. best_feedback_div = feedback_div;
  1099. best_frac_feedback_div = frac_feedback_div;
  1100. best_freq = current_freq;
  1101. best_error = error;
  1102. best_vco_diff = vco_diff;
  1103. } else if (current_freq == freq) {
  1104. if (best_freq == -1) {
  1105. best_post_div = post_div;
  1106. best_ref_div = ref_div;
  1107. best_feedback_div = feedback_div;
  1108. best_frac_feedback_div = frac_feedback_div;
  1109. best_freq = current_freq;
  1110. best_error = error;
  1111. best_vco_diff = vco_diff;
  1112. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  1113. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  1114. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  1115. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  1116. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  1117. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  1118. best_post_div = post_div;
  1119. best_ref_div = ref_div;
  1120. best_feedback_div = feedback_div;
  1121. best_frac_feedback_div = frac_feedback_div;
  1122. best_freq = current_freq;
  1123. best_error = error;
  1124. best_vco_diff = vco_diff;
  1125. }
  1126. }
  1127. if (current_freq < freq)
  1128. min_frac_feed_div = frac_feedback_div + 1;
  1129. else
  1130. max_frac_feed_div = frac_feedback_div;
  1131. }
  1132. if (current_freq < freq)
  1133. min_feed_div = feedback_div + 1;
  1134. else
  1135. max_feed_div = feedback_div;
  1136. }
  1137. }
  1138. }
  1139. *dot_clock_p = best_freq / 10000;
  1140. *fb_div_p = best_feedback_div;
  1141. *frac_fb_div_p = best_frac_feedback_div;
  1142. *ref_div_p = best_ref_div;
  1143. *post_div_p = best_post_div;
  1144. DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  1145. (long long)freq,
  1146. best_freq / 1000, best_feedback_div, best_frac_feedback_div,
  1147. best_ref_div, best_post_div);
  1148. }
  1149. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  1150. {
  1151. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  1152. if (radeon_fb->obj) {
  1153. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  1154. }
  1155. drm_framebuffer_cleanup(fb);
  1156. kfree(radeon_fb);
  1157. }
  1158. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  1159. struct drm_file *file_priv,
  1160. unsigned int *handle)
  1161. {
  1162. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  1163. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  1164. }
  1165. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  1166. .destroy = radeon_user_framebuffer_destroy,
  1167. .create_handle = radeon_user_framebuffer_create_handle,
  1168. };
  1169. int
  1170. radeon_framebuffer_init(struct drm_device *dev,
  1171. struct radeon_framebuffer *rfb,
  1172. const struct drm_mode_fb_cmd2 *mode_cmd,
  1173. struct drm_gem_object *obj)
  1174. {
  1175. int ret;
  1176. rfb->obj = obj;
  1177. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  1178. ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  1179. if (ret) {
  1180. rfb->obj = NULL;
  1181. return ret;
  1182. }
  1183. return 0;
  1184. }
  1185. static struct drm_framebuffer *
  1186. radeon_user_framebuffer_create(struct drm_device *dev,
  1187. struct drm_file *file_priv,
  1188. const struct drm_mode_fb_cmd2 *mode_cmd)
  1189. {
  1190. struct drm_gem_object *obj;
  1191. struct radeon_framebuffer *radeon_fb;
  1192. int ret;
  1193. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  1194. if (obj == NULL) {
  1195. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  1196. "can't create framebuffer\n", mode_cmd->handles[0]);
  1197. return ERR_PTR(-ENOENT);
  1198. }
  1199. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  1200. if (radeon_fb == NULL) {
  1201. drm_gem_object_unreference_unlocked(obj);
  1202. return ERR_PTR(-ENOMEM);
  1203. }
  1204. ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  1205. if (ret) {
  1206. kfree(radeon_fb);
  1207. drm_gem_object_unreference_unlocked(obj);
  1208. return ERR_PTR(ret);
  1209. }
  1210. return &radeon_fb->base;
  1211. }
  1212. static void radeon_output_poll_changed(struct drm_device *dev)
  1213. {
  1214. struct radeon_device *rdev = dev->dev_private;
  1215. radeon_fb_output_poll_changed(rdev);
  1216. }
  1217. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  1218. .fb_create = radeon_user_framebuffer_create,
  1219. .output_poll_changed = radeon_output_poll_changed
  1220. };
  1221. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  1222. { { 0, "driver" },
  1223. { 1, "bios" },
  1224. };
  1225. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  1226. { { TV_STD_NTSC, "ntsc" },
  1227. { TV_STD_PAL, "pal" },
  1228. { TV_STD_PAL_M, "pal-m" },
  1229. { TV_STD_PAL_60, "pal-60" },
  1230. { TV_STD_NTSC_J, "ntsc-j" },
  1231. { TV_STD_SCART_PAL, "scart-pal" },
  1232. { TV_STD_PAL_CN, "pal-cn" },
  1233. { TV_STD_SECAM, "secam" },
  1234. };
  1235. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  1236. { { UNDERSCAN_OFF, "off" },
  1237. { UNDERSCAN_ON, "on" },
  1238. { UNDERSCAN_AUTO, "auto" },
  1239. };
  1240. static struct drm_prop_enum_list radeon_audio_enum_list[] =
  1241. { { RADEON_AUDIO_DISABLE, "off" },
  1242. { RADEON_AUDIO_ENABLE, "on" },
  1243. { RADEON_AUDIO_AUTO, "auto" },
  1244. };
  1245. /* XXX support different dither options? spatial, temporal, both, etc. */
  1246. static struct drm_prop_enum_list radeon_dither_enum_list[] =
  1247. { { RADEON_FMT_DITHER_DISABLE, "off" },
  1248. { RADEON_FMT_DITHER_ENABLE, "on" },
  1249. };
  1250. static struct drm_prop_enum_list radeon_output_csc_enum_list[] =
  1251. { { RADEON_OUTPUT_CSC_BYPASS, "bypass" },
  1252. { RADEON_OUTPUT_CSC_TVRGB, "tvrgb" },
  1253. { RADEON_OUTPUT_CSC_YCBCR601, "ycbcr601" },
  1254. { RADEON_OUTPUT_CSC_YCBCR709, "ycbcr709" },
  1255. };
  1256. static int radeon_modeset_create_props(struct radeon_device *rdev)
  1257. {
  1258. int sz;
  1259. if (rdev->is_atom_bios) {
  1260. rdev->mode_info.coherent_mode_property =
  1261. drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
  1262. if (!rdev->mode_info.coherent_mode_property)
  1263. return -ENOMEM;
  1264. }
  1265. if (!ASIC_IS_AVIVO(rdev)) {
  1266. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  1267. rdev->mode_info.tmds_pll_property =
  1268. drm_property_create_enum(rdev->ddev, 0,
  1269. "tmds_pll",
  1270. radeon_tmds_pll_enum_list, sz);
  1271. }
  1272. rdev->mode_info.load_detect_property =
  1273. drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
  1274. if (!rdev->mode_info.load_detect_property)
  1275. return -ENOMEM;
  1276. drm_mode_create_scaling_mode_property(rdev->ddev);
  1277. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  1278. rdev->mode_info.tv_std_property =
  1279. drm_property_create_enum(rdev->ddev, 0,
  1280. "tv standard",
  1281. radeon_tv_std_enum_list, sz);
  1282. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  1283. rdev->mode_info.underscan_property =
  1284. drm_property_create_enum(rdev->ddev, 0,
  1285. "underscan",
  1286. radeon_underscan_enum_list, sz);
  1287. rdev->mode_info.underscan_hborder_property =
  1288. drm_property_create_range(rdev->ddev, 0,
  1289. "underscan hborder", 0, 128);
  1290. if (!rdev->mode_info.underscan_hborder_property)
  1291. return -ENOMEM;
  1292. rdev->mode_info.underscan_vborder_property =
  1293. drm_property_create_range(rdev->ddev, 0,
  1294. "underscan vborder", 0, 128);
  1295. if (!rdev->mode_info.underscan_vborder_property)
  1296. return -ENOMEM;
  1297. sz = ARRAY_SIZE(radeon_audio_enum_list);
  1298. rdev->mode_info.audio_property =
  1299. drm_property_create_enum(rdev->ddev, 0,
  1300. "audio",
  1301. radeon_audio_enum_list, sz);
  1302. sz = ARRAY_SIZE(radeon_dither_enum_list);
  1303. rdev->mode_info.dither_property =
  1304. drm_property_create_enum(rdev->ddev, 0,
  1305. "dither",
  1306. radeon_dither_enum_list, sz);
  1307. sz = ARRAY_SIZE(radeon_output_csc_enum_list);
  1308. rdev->mode_info.output_csc_property =
  1309. drm_property_create_enum(rdev->ddev, 0,
  1310. "output_csc",
  1311. radeon_output_csc_enum_list, sz);
  1312. return 0;
  1313. }
  1314. void radeon_update_display_priority(struct radeon_device *rdev)
  1315. {
  1316. /* adjustment options for the display watermarks */
  1317. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  1318. /* set display priority to high for r3xx, rv515 chips
  1319. * this avoids flickering due to underflow to the
  1320. * display controllers during heavy acceleration.
  1321. * Don't force high on rs4xx igp chips as it seems to
  1322. * affect the sound card. See kernel bug 15982.
  1323. */
  1324. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  1325. !(rdev->flags & RADEON_IS_IGP))
  1326. rdev->disp_priority = 2;
  1327. else
  1328. rdev->disp_priority = 0;
  1329. } else
  1330. rdev->disp_priority = radeon_disp_priority;
  1331. }
  1332. /*
  1333. * Allocate hdmi structs and determine register offsets
  1334. */
  1335. static void radeon_afmt_init(struct radeon_device *rdev)
  1336. {
  1337. int i;
  1338. for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
  1339. rdev->mode_info.afmt[i] = NULL;
  1340. if (ASIC_IS_NODCE(rdev)) {
  1341. /* nothing to do */
  1342. } else if (ASIC_IS_DCE4(rdev)) {
  1343. static uint32_t eg_offsets[] = {
  1344. EVERGREEN_CRTC0_REGISTER_OFFSET,
  1345. EVERGREEN_CRTC1_REGISTER_OFFSET,
  1346. EVERGREEN_CRTC2_REGISTER_OFFSET,
  1347. EVERGREEN_CRTC3_REGISTER_OFFSET,
  1348. EVERGREEN_CRTC4_REGISTER_OFFSET,
  1349. EVERGREEN_CRTC5_REGISTER_OFFSET,
  1350. 0x13830 - 0x7030,
  1351. };
  1352. int num_afmt;
  1353. /* DCE8 has 7 audio blocks tied to DIG encoders */
  1354. /* DCE6 has 6 audio blocks tied to DIG encoders */
  1355. /* DCE4/5 has 6 audio blocks tied to DIG encoders */
  1356. /* DCE4.1 has 2 audio blocks tied to DIG encoders */
  1357. if (ASIC_IS_DCE8(rdev))
  1358. num_afmt = 7;
  1359. else if (ASIC_IS_DCE6(rdev))
  1360. num_afmt = 6;
  1361. else if (ASIC_IS_DCE5(rdev))
  1362. num_afmt = 6;
  1363. else if (ASIC_IS_DCE41(rdev))
  1364. num_afmt = 2;
  1365. else /* DCE4 */
  1366. num_afmt = 6;
  1367. BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
  1368. for (i = 0; i < num_afmt; i++) {
  1369. rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1370. if (rdev->mode_info.afmt[i]) {
  1371. rdev->mode_info.afmt[i]->offset = eg_offsets[i];
  1372. rdev->mode_info.afmt[i]->id = i;
  1373. }
  1374. }
  1375. } else if (ASIC_IS_DCE3(rdev)) {
  1376. /* DCE3.x has 2 audio blocks tied to DIG encoders */
  1377. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1378. if (rdev->mode_info.afmt[0]) {
  1379. rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
  1380. rdev->mode_info.afmt[0]->id = 0;
  1381. }
  1382. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1383. if (rdev->mode_info.afmt[1]) {
  1384. rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
  1385. rdev->mode_info.afmt[1]->id = 1;
  1386. }
  1387. } else if (ASIC_IS_DCE2(rdev)) {
  1388. /* DCE2 has at least 1 routable audio block */
  1389. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1390. if (rdev->mode_info.afmt[0]) {
  1391. rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
  1392. rdev->mode_info.afmt[0]->id = 0;
  1393. }
  1394. /* r6xx has 2 routable audio blocks */
  1395. if (rdev->family >= CHIP_R600) {
  1396. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1397. if (rdev->mode_info.afmt[1]) {
  1398. rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
  1399. rdev->mode_info.afmt[1]->id = 1;
  1400. }
  1401. }
  1402. }
  1403. }
  1404. static void radeon_afmt_fini(struct radeon_device *rdev)
  1405. {
  1406. int i;
  1407. for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
  1408. kfree(rdev->mode_info.afmt[i]);
  1409. rdev->mode_info.afmt[i] = NULL;
  1410. }
  1411. }
  1412. int radeon_modeset_init(struct radeon_device *rdev)
  1413. {
  1414. int i;
  1415. int ret;
  1416. drm_mode_config_init(rdev->ddev);
  1417. rdev->mode_info.mode_config_initialized = true;
  1418. rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
  1419. if (ASIC_IS_DCE5(rdev)) {
  1420. rdev->ddev->mode_config.max_width = 16384;
  1421. rdev->ddev->mode_config.max_height = 16384;
  1422. } else if (ASIC_IS_AVIVO(rdev)) {
  1423. rdev->ddev->mode_config.max_width = 8192;
  1424. rdev->ddev->mode_config.max_height = 8192;
  1425. } else {
  1426. rdev->ddev->mode_config.max_width = 4096;
  1427. rdev->ddev->mode_config.max_height = 4096;
  1428. }
  1429. rdev->ddev->mode_config.preferred_depth = 24;
  1430. rdev->ddev->mode_config.prefer_shadow = 1;
  1431. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  1432. ret = radeon_modeset_create_props(rdev);
  1433. if (ret) {
  1434. return ret;
  1435. }
  1436. /* init i2c buses */
  1437. radeon_i2c_init(rdev);
  1438. /* check combios for a valid hardcoded EDID - Sun servers */
  1439. if (!rdev->is_atom_bios) {
  1440. /* check for hardcoded EDID in BIOS */
  1441. radeon_combios_check_hardcoded_edid(rdev);
  1442. }
  1443. /* allocate crtcs */
  1444. for (i = 0; i < rdev->num_crtc; i++) {
  1445. radeon_crtc_init(rdev->ddev, i);
  1446. }
  1447. /* okay we should have all the bios connectors */
  1448. ret = radeon_setup_enc_conn(rdev->ddev);
  1449. if (!ret) {
  1450. return ret;
  1451. }
  1452. /* init dig PHYs, disp eng pll */
  1453. if (rdev->is_atom_bios) {
  1454. radeon_atom_encoder_init(rdev);
  1455. radeon_atom_disp_eng_pll_init(rdev);
  1456. }
  1457. /* initialize hpd */
  1458. radeon_hpd_init(rdev);
  1459. /* setup afmt */
  1460. radeon_afmt_init(rdev);
  1461. radeon_fbdev_init(rdev);
  1462. drm_kms_helper_poll_init(rdev->ddev);
  1463. /* do pm late init */
  1464. ret = radeon_pm_late_init(rdev);
  1465. return 0;
  1466. }
  1467. void radeon_modeset_fini(struct radeon_device *rdev)
  1468. {
  1469. radeon_fbdev_fini(rdev);
  1470. kfree(rdev->mode_info.bios_hardcoded_edid);
  1471. /* free i2c buses */
  1472. radeon_i2c_fini(rdev);
  1473. if (rdev->mode_info.mode_config_initialized) {
  1474. radeon_afmt_fini(rdev);
  1475. drm_kms_helper_poll_fini(rdev->ddev);
  1476. radeon_hpd_fini(rdev);
  1477. drm_mode_config_cleanup(rdev->ddev);
  1478. rdev->mode_info.mode_config_initialized = false;
  1479. }
  1480. }
  1481. static bool is_hdtv_mode(const struct drm_display_mode *mode)
  1482. {
  1483. /* try and guess if this is a tv or a monitor */
  1484. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1485. (mode->vdisplay == 576) || /* 576p */
  1486. (mode->vdisplay == 720) || /* 720p */
  1487. (mode->vdisplay == 1080)) /* 1080p */
  1488. return true;
  1489. else
  1490. return false;
  1491. }
  1492. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1493. const struct drm_display_mode *mode,
  1494. struct drm_display_mode *adjusted_mode)
  1495. {
  1496. struct drm_device *dev = crtc->dev;
  1497. struct radeon_device *rdev = dev->dev_private;
  1498. struct drm_encoder *encoder;
  1499. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1500. struct radeon_encoder *radeon_encoder;
  1501. struct drm_connector *connector;
  1502. struct radeon_connector *radeon_connector;
  1503. bool first = true;
  1504. u32 src_v = 1, dst_v = 1;
  1505. u32 src_h = 1, dst_h = 1;
  1506. radeon_crtc->h_border = 0;
  1507. radeon_crtc->v_border = 0;
  1508. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1509. if (encoder->crtc != crtc)
  1510. continue;
  1511. radeon_encoder = to_radeon_encoder(encoder);
  1512. connector = radeon_get_connector_for_encoder(encoder);
  1513. radeon_connector = to_radeon_connector(connector);
  1514. if (first) {
  1515. /* set scaling */
  1516. if (radeon_encoder->rmx_type == RMX_OFF)
  1517. radeon_crtc->rmx_type = RMX_OFF;
  1518. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1519. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1520. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1521. else
  1522. radeon_crtc->rmx_type = RMX_OFF;
  1523. /* copy native mode */
  1524. memcpy(&radeon_crtc->native_mode,
  1525. &radeon_encoder->native_mode,
  1526. sizeof(struct drm_display_mode));
  1527. src_v = crtc->mode.vdisplay;
  1528. dst_v = radeon_crtc->native_mode.vdisplay;
  1529. src_h = crtc->mode.hdisplay;
  1530. dst_h = radeon_crtc->native_mode.hdisplay;
  1531. /* fix up for overscan on hdmi */
  1532. if (ASIC_IS_AVIVO(rdev) &&
  1533. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1534. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1535. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1536. drm_detect_hdmi_monitor(radeon_connector_edid(connector)) &&
  1537. is_hdtv_mode(mode)))) {
  1538. if (radeon_encoder->underscan_hborder != 0)
  1539. radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1540. else
  1541. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1542. if (radeon_encoder->underscan_vborder != 0)
  1543. radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1544. else
  1545. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1546. radeon_crtc->rmx_type = RMX_FULL;
  1547. src_v = crtc->mode.vdisplay;
  1548. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1549. src_h = crtc->mode.hdisplay;
  1550. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1551. }
  1552. first = false;
  1553. } else {
  1554. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1555. /* WARNING: Right now this can't happen but
  1556. * in the future we need to check that scaling
  1557. * are consistent across different encoder
  1558. * (ie all encoder can work with the same
  1559. * scaling).
  1560. */
  1561. DRM_ERROR("Scaling not consistent across encoder.\n");
  1562. return false;
  1563. }
  1564. }
  1565. }
  1566. if (radeon_crtc->rmx_type != RMX_OFF) {
  1567. fixed20_12 a, b;
  1568. a.full = dfixed_const(src_v);
  1569. b.full = dfixed_const(dst_v);
  1570. radeon_crtc->vsc.full = dfixed_div(a, b);
  1571. a.full = dfixed_const(src_h);
  1572. b.full = dfixed_const(dst_h);
  1573. radeon_crtc->hsc.full = dfixed_div(a, b);
  1574. } else {
  1575. radeon_crtc->vsc.full = dfixed_const(1);
  1576. radeon_crtc->hsc.full = dfixed_const(1);
  1577. }
  1578. return true;
  1579. }
  1580. /*
  1581. * Retrieve current video scanout position of crtc on a given gpu, and
  1582. * an optional accurate timestamp of when query happened.
  1583. *
  1584. * \param dev Device to query.
  1585. * \param crtc Crtc to query.
  1586. * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
  1587. * For driver internal use only also supports these flags:
  1588. *
  1589. * USE_REAL_VBLANKSTART to use the real start of vblank instead
  1590. * of a fudged earlier start of vblank.
  1591. *
  1592. * GET_DISTANCE_TO_VBLANKSTART to return distance to the
  1593. * fudged earlier start of vblank in *vpos and the distance
  1594. * to true start of vblank in *hpos.
  1595. *
  1596. * \param *vpos Location where vertical scanout position should be stored.
  1597. * \param *hpos Location where horizontal scanout position should go.
  1598. * \param *stime Target location for timestamp taken immediately before
  1599. * scanout position query. Can be NULL to skip timestamp.
  1600. * \param *etime Target location for timestamp taken immediately after
  1601. * scanout position query. Can be NULL to skip timestamp.
  1602. *
  1603. * Returns vpos as a positive number while in active scanout area.
  1604. * Returns vpos as a negative number inside vblank, counting the number
  1605. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1606. * until start of active scanout / end of vblank."
  1607. *
  1608. * \return Flags, or'ed together as follows:
  1609. *
  1610. * DRM_SCANOUTPOS_VALID = Query successful.
  1611. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1612. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1613. * this flag means that returned position may be offset by a constant but
  1614. * unknown small number of scanlines wrt. real scanout position.
  1615. *
  1616. */
  1617. int radeon_get_crtc_scanoutpos(struct drm_device *dev, unsigned int pipe,
  1618. unsigned int flags, int *vpos, int *hpos,
  1619. ktime_t *stime, ktime_t *etime,
  1620. const struct drm_display_mode *mode)
  1621. {
  1622. u32 stat_crtc = 0, vbl = 0, position = 0;
  1623. int vbl_start, vbl_end, vtotal, ret = 0;
  1624. bool in_vbl = true;
  1625. struct radeon_device *rdev = dev->dev_private;
  1626. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  1627. /* Get optional system timestamp before query. */
  1628. if (stime)
  1629. *stime = ktime_get();
  1630. if (ASIC_IS_DCE4(rdev)) {
  1631. if (pipe == 0) {
  1632. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1633. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1634. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1635. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1636. ret |= DRM_SCANOUTPOS_VALID;
  1637. }
  1638. if (pipe == 1) {
  1639. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1640. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1641. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1642. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1643. ret |= DRM_SCANOUTPOS_VALID;
  1644. }
  1645. if (pipe == 2) {
  1646. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1647. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1648. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1649. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1650. ret |= DRM_SCANOUTPOS_VALID;
  1651. }
  1652. if (pipe == 3) {
  1653. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1654. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1655. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1656. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1657. ret |= DRM_SCANOUTPOS_VALID;
  1658. }
  1659. if (pipe == 4) {
  1660. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1661. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1662. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1663. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1664. ret |= DRM_SCANOUTPOS_VALID;
  1665. }
  1666. if (pipe == 5) {
  1667. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1668. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1669. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1670. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1671. ret |= DRM_SCANOUTPOS_VALID;
  1672. }
  1673. } else if (ASIC_IS_AVIVO(rdev)) {
  1674. if (pipe == 0) {
  1675. vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1676. position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1677. ret |= DRM_SCANOUTPOS_VALID;
  1678. }
  1679. if (pipe == 1) {
  1680. vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1681. position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1682. ret |= DRM_SCANOUTPOS_VALID;
  1683. }
  1684. } else {
  1685. /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1686. if (pipe == 0) {
  1687. /* Assume vbl_end == 0, get vbl_start from
  1688. * upper 16 bits.
  1689. */
  1690. vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1691. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1692. /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1693. position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1694. stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1695. if (!(stat_crtc & 1))
  1696. in_vbl = false;
  1697. ret |= DRM_SCANOUTPOS_VALID;
  1698. }
  1699. if (pipe == 1) {
  1700. vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1701. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1702. position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1703. stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1704. if (!(stat_crtc & 1))
  1705. in_vbl = false;
  1706. ret |= DRM_SCANOUTPOS_VALID;
  1707. }
  1708. }
  1709. /* Get optional system timestamp after query. */
  1710. if (etime)
  1711. *etime = ktime_get();
  1712. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  1713. /* Decode into vertical and horizontal scanout position. */
  1714. *vpos = position & 0x1fff;
  1715. *hpos = (position >> 16) & 0x1fff;
  1716. /* Valid vblank area boundaries from gpu retrieved? */
  1717. if (vbl > 0) {
  1718. /* Yes: Decode. */
  1719. ret |= DRM_SCANOUTPOS_ACCURATE;
  1720. vbl_start = vbl & 0x1fff;
  1721. vbl_end = (vbl >> 16) & 0x1fff;
  1722. }
  1723. else {
  1724. /* No: Fake something reasonable which gives at least ok results. */
  1725. vbl_start = mode->crtc_vdisplay;
  1726. vbl_end = 0;
  1727. }
  1728. /* Called from driver internal vblank counter query code? */
  1729. if (flags & GET_DISTANCE_TO_VBLANKSTART) {
  1730. /* Caller wants distance from real vbl_start in *hpos */
  1731. *hpos = *vpos - vbl_start;
  1732. }
  1733. /* Fudge vblank to start a few scanlines earlier to handle the
  1734. * problem that vblank irqs fire a few scanlines before start
  1735. * of vblank. Some driver internal callers need the true vblank
  1736. * start to be used and signal this via the USE_REAL_VBLANKSTART flag.
  1737. *
  1738. * The cause of the "early" vblank irq is that the irq is triggered
  1739. * by the line buffer logic when the line buffer read position enters
  1740. * the vblank, whereas our crtc scanout position naturally lags the
  1741. * line buffer read position.
  1742. */
  1743. if (!(flags & USE_REAL_VBLANKSTART))
  1744. vbl_start -= rdev->mode_info.crtcs[pipe]->lb_vblank_lead_lines;
  1745. /* Test scanout position against vblank region. */
  1746. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1747. in_vbl = false;
  1748. /* In vblank? */
  1749. if (in_vbl)
  1750. ret |= DRM_SCANOUTPOS_IN_VBLANK;
  1751. /* Called from driver internal vblank counter query code? */
  1752. if (flags & GET_DISTANCE_TO_VBLANKSTART) {
  1753. /* Caller wants distance from fudged earlier vbl_start */
  1754. *vpos -= vbl_start;
  1755. return ret;
  1756. }
  1757. /* Check if inside vblank area and apply corrective offsets:
  1758. * vpos will then be >=0 in video scanout area, but negative
  1759. * within vblank area, counting down the number of lines until
  1760. * start of scanout.
  1761. */
  1762. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1763. if (in_vbl && (*vpos >= vbl_start)) {
  1764. vtotal = mode->crtc_vtotal;
  1765. *vpos = *vpos - vtotal;
  1766. }
  1767. /* Correct for shifted end of vbl at vbl_end. */
  1768. *vpos = *vpos - vbl_end;
  1769. return ret;
  1770. }