radeon_display.c 43 KB

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