radeon_display.c 62 KB

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