radeon_display.c 62 KB

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