radeon_display.c 51 KB

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