radeon_display.c 62 KB

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