radeon_display.c 49 KB

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