radeon_display.c 51 KB

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