radeon_display.c 51 KB

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