radeon_display.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432
  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[34] = {
  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. };
  457. static const char *connector_names[15] = {
  458. "Unknown",
  459. "VGA",
  460. "DVI-I",
  461. "DVI-D",
  462. "DVI-A",
  463. "Composite",
  464. "S-video",
  465. "LVDS",
  466. "Component",
  467. "DIN",
  468. "DisplayPort",
  469. "HDMI-A",
  470. "HDMI-B",
  471. "TV",
  472. "eDP",
  473. };
  474. static const char *hpd_names[6] = {
  475. "HPD1",
  476. "HPD2",
  477. "HPD3",
  478. "HPD4",
  479. "HPD5",
  480. "HPD6",
  481. };
  482. static void radeon_print_display_setup(struct drm_device *dev)
  483. {
  484. struct drm_connector *connector;
  485. struct radeon_connector *radeon_connector;
  486. struct drm_encoder *encoder;
  487. struct radeon_encoder *radeon_encoder;
  488. uint32_t devices;
  489. int i = 0;
  490. DRM_INFO("Radeon Display Connectors\n");
  491. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  492. radeon_connector = to_radeon_connector(connector);
  493. DRM_INFO("Connector %d:\n", i);
  494. DRM_INFO(" %s\n", connector_names[connector->connector_type]);
  495. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  496. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  497. if (radeon_connector->ddc_bus) {
  498. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  499. radeon_connector->ddc_bus->rec.mask_clk_reg,
  500. radeon_connector->ddc_bus->rec.mask_data_reg,
  501. radeon_connector->ddc_bus->rec.a_clk_reg,
  502. radeon_connector->ddc_bus->rec.a_data_reg,
  503. radeon_connector->ddc_bus->rec.en_clk_reg,
  504. radeon_connector->ddc_bus->rec.en_data_reg,
  505. radeon_connector->ddc_bus->rec.y_clk_reg,
  506. radeon_connector->ddc_bus->rec.y_data_reg);
  507. if (radeon_connector->router.ddc_valid)
  508. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  509. radeon_connector->router.ddc_mux_control_pin,
  510. radeon_connector->router.ddc_mux_state);
  511. if (radeon_connector->router.cd_valid)
  512. DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
  513. radeon_connector->router.cd_mux_control_pin,
  514. radeon_connector->router.cd_mux_state);
  515. } else {
  516. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  517. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  518. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  519. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  520. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  521. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  522. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  523. }
  524. DRM_INFO(" Encoders:\n");
  525. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  526. radeon_encoder = to_radeon_encoder(encoder);
  527. devices = radeon_encoder->devices & radeon_connector->devices;
  528. if (devices) {
  529. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  530. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  531. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  532. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  533. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  534. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  535. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  536. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  537. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  538. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  539. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  540. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  541. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  542. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  543. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  544. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  545. if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  546. DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
  547. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  548. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  549. if (devices & ATOM_DEVICE_CV_SUPPORT)
  550. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  551. }
  552. }
  553. i++;
  554. }
  555. }
  556. static bool radeon_setup_enc_conn(struct drm_device *dev)
  557. {
  558. struct radeon_device *rdev = dev->dev_private;
  559. struct drm_connector *drm_connector;
  560. bool ret = false;
  561. if (rdev->bios) {
  562. if (rdev->is_atom_bios) {
  563. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  564. if (ret == false)
  565. ret = radeon_get_atom_connector_info_from_object_table(dev);
  566. } else {
  567. ret = radeon_get_legacy_connector_info_from_bios(dev);
  568. if (ret == false)
  569. ret = radeon_get_legacy_connector_info_from_table(dev);
  570. }
  571. } else {
  572. if (!ASIC_IS_AVIVO(rdev))
  573. ret = radeon_get_legacy_connector_info_from_table(dev);
  574. }
  575. if (ret) {
  576. radeon_setup_encoder_clones(dev);
  577. radeon_print_display_setup(dev);
  578. list_for_each_entry(drm_connector, &dev->mode_config.connector_list, head)
  579. radeon_ddc_dump(drm_connector);
  580. }
  581. return ret;
  582. }
  583. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  584. {
  585. struct drm_device *dev = radeon_connector->base.dev;
  586. struct radeon_device *rdev = dev->dev_private;
  587. int ret = 0;
  588. /* on hw with routers, select right port */
  589. if (radeon_connector->router.ddc_valid)
  590. radeon_router_select_ddc_port(radeon_connector);
  591. if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  592. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
  593. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  594. if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  595. dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  596. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
  597. }
  598. if (!radeon_connector->ddc_bus)
  599. return -1;
  600. if (!radeon_connector->edid) {
  601. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  602. }
  603. /* some servers provide a hardcoded edid in rom for KVMs */
  604. if (!radeon_connector->edid)
  605. radeon_connector->edid = radeon_combios_get_hardcoded_edid(rdev);
  606. if (radeon_connector->edid) {
  607. drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  608. ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  609. return ret;
  610. }
  611. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  612. return 0;
  613. }
  614. static int radeon_ddc_dump(struct drm_connector *connector)
  615. {
  616. struct edid *edid;
  617. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  618. int ret = 0;
  619. /* on hw with routers, select right port */
  620. if (radeon_connector->router.ddc_valid)
  621. radeon_router_select_ddc_port(radeon_connector);
  622. if (!radeon_connector->ddc_bus)
  623. return -1;
  624. edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  625. if (edid) {
  626. kfree(edid);
  627. }
  628. return ret;
  629. }
  630. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  631. {
  632. uint64_t mod;
  633. n += d / 2;
  634. mod = do_div(n, d);
  635. return n;
  636. }
  637. void radeon_compute_pll(struct radeon_pll *pll,
  638. uint64_t freq,
  639. uint32_t *dot_clock_p,
  640. uint32_t *fb_div_p,
  641. uint32_t *frac_fb_div_p,
  642. uint32_t *ref_div_p,
  643. uint32_t *post_div_p)
  644. {
  645. uint32_t min_ref_div = pll->min_ref_div;
  646. uint32_t max_ref_div = pll->max_ref_div;
  647. uint32_t min_post_div = pll->min_post_div;
  648. uint32_t max_post_div = pll->max_post_div;
  649. uint32_t min_fractional_feed_div = 0;
  650. uint32_t max_fractional_feed_div = 0;
  651. uint32_t best_vco = pll->best_vco;
  652. uint32_t best_post_div = 1;
  653. uint32_t best_ref_div = 1;
  654. uint32_t best_feedback_div = 1;
  655. uint32_t best_frac_feedback_div = 0;
  656. uint32_t best_freq = -1;
  657. uint32_t best_error = 0xffffffff;
  658. uint32_t best_vco_diff = 1;
  659. uint32_t post_div;
  660. u32 pll_out_min, pll_out_max;
  661. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  662. freq = freq * 1000;
  663. if (pll->flags & RADEON_PLL_IS_LCD) {
  664. pll_out_min = pll->lcd_pll_out_min;
  665. pll_out_max = pll->lcd_pll_out_max;
  666. } else {
  667. pll_out_min = pll->pll_out_min;
  668. pll_out_max = pll->pll_out_max;
  669. }
  670. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  671. min_ref_div = max_ref_div = pll->reference_div;
  672. else {
  673. while (min_ref_div < max_ref_div-1) {
  674. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  675. uint32_t pll_in = pll->reference_freq / mid;
  676. if (pll_in < pll->pll_in_min)
  677. max_ref_div = mid;
  678. else if (pll_in > pll->pll_in_max)
  679. min_ref_div = mid;
  680. else
  681. break;
  682. }
  683. }
  684. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  685. min_post_div = max_post_div = pll->post_div;
  686. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  687. min_fractional_feed_div = pll->min_frac_feedback_div;
  688. max_fractional_feed_div = pll->max_frac_feedback_div;
  689. }
  690. for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  691. uint32_t ref_div;
  692. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  693. continue;
  694. /* legacy radeons only have a few post_divs */
  695. if (pll->flags & RADEON_PLL_LEGACY) {
  696. if ((post_div == 5) ||
  697. (post_div == 7) ||
  698. (post_div == 9) ||
  699. (post_div == 10) ||
  700. (post_div == 11) ||
  701. (post_div == 13) ||
  702. (post_div == 14) ||
  703. (post_div == 15))
  704. continue;
  705. }
  706. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  707. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  708. uint32_t pll_in = pll->reference_freq / ref_div;
  709. uint32_t min_feed_div = pll->min_feedback_div;
  710. uint32_t max_feed_div = pll->max_feedback_div + 1;
  711. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  712. continue;
  713. while (min_feed_div < max_feed_div) {
  714. uint32_t vco;
  715. uint32_t min_frac_feed_div = min_fractional_feed_div;
  716. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  717. uint32_t frac_feedback_div;
  718. uint64_t tmp;
  719. feedback_div = (min_feed_div + max_feed_div) / 2;
  720. tmp = (uint64_t)pll->reference_freq * feedback_div;
  721. vco = radeon_div(tmp, ref_div);
  722. if (vco < pll_out_min) {
  723. min_feed_div = feedback_div + 1;
  724. continue;
  725. } else if (vco > pll_out_max) {
  726. max_feed_div = feedback_div;
  727. continue;
  728. }
  729. while (min_frac_feed_div < max_frac_feed_div) {
  730. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  731. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  732. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  733. current_freq = radeon_div(tmp, ref_div * post_div);
  734. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  735. if (freq < current_freq)
  736. error = 0xffffffff;
  737. else
  738. error = freq - current_freq;
  739. } else
  740. error = abs(current_freq - freq);
  741. vco_diff = abs(vco - best_vco);
  742. if ((best_vco == 0 && error < best_error) ||
  743. (best_vco != 0 &&
  744. ((best_error > 100 && error < best_error - 100) ||
  745. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  746. best_post_div = post_div;
  747. best_ref_div = ref_div;
  748. best_feedback_div = feedback_div;
  749. best_frac_feedback_div = frac_feedback_div;
  750. best_freq = current_freq;
  751. best_error = error;
  752. best_vco_diff = vco_diff;
  753. } else if (current_freq == freq) {
  754. if (best_freq == -1) {
  755. best_post_div = post_div;
  756. best_ref_div = ref_div;
  757. best_feedback_div = feedback_div;
  758. best_frac_feedback_div = frac_feedback_div;
  759. best_freq = current_freq;
  760. best_error = error;
  761. best_vco_diff = vco_diff;
  762. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  763. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  764. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  765. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  766. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  767. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  768. best_post_div = post_div;
  769. best_ref_div = ref_div;
  770. best_feedback_div = feedback_div;
  771. best_frac_feedback_div = frac_feedback_div;
  772. best_freq = current_freq;
  773. best_error = error;
  774. best_vco_diff = vco_diff;
  775. }
  776. }
  777. if (current_freq < freq)
  778. min_frac_feed_div = frac_feedback_div + 1;
  779. else
  780. max_frac_feed_div = frac_feedback_div;
  781. }
  782. if (current_freq < freq)
  783. min_feed_div = feedback_div + 1;
  784. else
  785. max_feed_div = feedback_div;
  786. }
  787. }
  788. }
  789. *dot_clock_p = best_freq / 10000;
  790. *fb_div_p = best_feedback_div;
  791. *frac_fb_div_p = best_frac_feedback_div;
  792. *ref_div_p = best_ref_div;
  793. *post_div_p = best_post_div;
  794. }
  795. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  796. {
  797. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  798. if (radeon_fb->obj) {
  799. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  800. }
  801. drm_framebuffer_cleanup(fb);
  802. kfree(radeon_fb);
  803. }
  804. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  805. struct drm_file *file_priv,
  806. unsigned int *handle)
  807. {
  808. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  809. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  810. }
  811. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  812. .destroy = radeon_user_framebuffer_destroy,
  813. .create_handle = radeon_user_framebuffer_create_handle,
  814. };
  815. void
  816. radeon_framebuffer_init(struct drm_device *dev,
  817. struct radeon_framebuffer *rfb,
  818. struct drm_mode_fb_cmd *mode_cmd,
  819. struct drm_gem_object *obj)
  820. {
  821. rfb->obj = obj;
  822. drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  823. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  824. }
  825. static struct drm_framebuffer *
  826. radeon_user_framebuffer_create(struct drm_device *dev,
  827. struct drm_file *file_priv,
  828. struct drm_mode_fb_cmd *mode_cmd)
  829. {
  830. struct drm_gem_object *obj;
  831. struct radeon_framebuffer *radeon_fb;
  832. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  833. if (obj == NULL) {
  834. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  835. "can't create framebuffer\n", mode_cmd->handle);
  836. return ERR_PTR(-ENOENT);
  837. }
  838. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  839. if (radeon_fb == NULL)
  840. return ERR_PTR(-ENOMEM);
  841. radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  842. return &radeon_fb->base;
  843. }
  844. static void radeon_output_poll_changed(struct drm_device *dev)
  845. {
  846. struct radeon_device *rdev = dev->dev_private;
  847. radeon_fb_output_poll_changed(rdev);
  848. }
  849. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  850. .fb_create = radeon_user_framebuffer_create,
  851. .output_poll_changed = radeon_output_poll_changed
  852. };
  853. struct drm_prop_enum_list {
  854. int type;
  855. char *name;
  856. };
  857. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  858. { { 0, "driver" },
  859. { 1, "bios" },
  860. };
  861. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  862. { { TV_STD_NTSC, "ntsc" },
  863. { TV_STD_PAL, "pal" },
  864. { TV_STD_PAL_M, "pal-m" },
  865. { TV_STD_PAL_60, "pal-60" },
  866. { TV_STD_NTSC_J, "ntsc-j" },
  867. { TV_STD_SCART_PAL, "scart-pal" },
  868. { TV_STD_PAL_CN, "pal-cn" },
  869. { TV_STD_SECAM, "secam" },
  870. };
  871. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  872. { { UNDERSCAN_OFF, "off" },
  873. { UNDERSCAN_ON, "on" },
  874. { UNDERSCAN_AUTO, "auto" },
  875. };
  876. static int radeon_modeset_create_props(struct radeon_device *rdev)
  877. {
  878. int i, sz;
  879. if (rdev->is_atom_bios) {
  880. rdev->mode_info.coherent_mode_property =
  881. drm_property_create(rdev->ddev,
  882. DRM_MODE_PROP_RANGE,
  883. "coherent", 2);
  884. if (!rdev->mode_info.coherent_mode_property)
  885. return -ENOMEM;
  886. rdev->mode_info.coherent_mode_property->values[0] = 0;
  887. rdev->mode_info.coherent_mode_property->values[1] = 1;
  888. }
  889. if (!ASIC_IS_AVIVO(rdev)) {
  890. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  891. rdev->mode_info.tmds_pll_property =
  892. drm_property_create(rdev->ddev,
  893. DRM_MODE_PROP_ENUM,
  894. "tmds_pll", sz);
  895. for (i = 0; i < sz; i++) {
  896. drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  897. i,
  898. radeon_tmds_pll_enum_list[i].type,
  899. radeon_tmds_pll_enum_list[i].name);
  900. }
  901. }
  902. rdev->mode_info.load_detect_property =
  903. drm_property_create(rdev->ddev,
  904. DRM_MODE_PROP_RANGE,
  905. "load detection", 2);
  906. if (!rdev->mode_info.load_detect_property)
  907. return -ENOMEM;
  908. rdev->mode_info.load_detect_property->values[0] = 0;
  909. rdev->mode_info.load_detect_property->values[1] = 1;
  910. drm_mode_create_scaling_mode_property(rdev->ddev);
  911. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  912. rdev->mode_info.tv_std_property =
  913. drm_property_create(rdev->ddev,
  914. DRM_MODE_PROP_ENUM,
  915. "tv standard", sz);
  916. for (i = 0; i < sz; i++) {
  917. drm_property_add_enum(rdev->mode_info.tv_std_property,
  918. i,
  919. radeon_tv_std_enum_list[i].type,
  920. radeon_tv_std_enum_list[i].name);
  921. }
  922. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  923. rdev->mode_info.underscan_property =
  924. drm_property_create(rdev->ddev,
  925. DRM_MODE_PROP_ENUM,
  926. "underscan", sz);
  927. for (i = 0; i < sz; i++) {
  928. drm_property_add_enum(rdev->mode_info.underscan_property,
  929. i,
  930. radeon_underscan_enum_list[i].type,
  931. radeon_underscan_enum_list[i].name);
  932. }
  933. rdev->mode_info.underscan_hborder_property =
  934. drm_property_create(rdev->ddev,
  935. DRM_MODE_PROP_RANGE,
  936. "underscan hborder", 2);
  937. if (!rdev->mode_info.underscan_hborder_property)
  938. return -ENOMEM;
  939. rdev->mode_info.underscan_hborder_property->values[0] = 0;
  940. rdev->mode_info.underscan_hborder_property->values[1] = 128;
  941. rdev->mode_info.underscan_vborder_property =
  942. drm_property_create(rdev->ddev,
  943. DRM_MODE_PROP_RANGE,
  944. "underscan vborder", 2);
  945. if (!rdev->mode_info.underscan_vborder_property)
  946. return -ENOMEM;
  947. rdev->mode_info.underscan_vborder_property->values[0] = 0;
  948. rdev->mode_info.underscan_vborder_property->values[1] = 128;
  949. return 0;
  950. }
  951. void radeon_update_display_priority(struct radeon_device *rdev)
  952. {
  953. /* adjustment options for the display watermarks */
  954. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  955. /* set display priority to high for r3xx, rv515 chips
  956. * this avoids flickering due to underflow to the
  957. * display controllers during heavy acceleration.
  958. * Don't force high on rs4xx igp chips as it seems to
  959. * affect the sound card. See kernel bug 15982.
  960. */
  961. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  962. !(rdev->flags & RADEON_IS_IGP))
  963. rdev->disp_priority = 2;
  964. else
  965. rdev->disp_priority = 0;
  966. } else
  967. rdev->disp_priority = radeon_disp_priority;
  968. }
  969. int radeon_modeset_init(struct radeon_device *rdev)
  970. {
  971. int i;
  972. int ret;
  973. drm_mode_config_init(rdev->ddev);
  974. rdev->mode_info.mode_config_initialized = true;
  975. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  976. if (ASIC_IS_AVIVO(rdev)) {
  977. rdev->ddev->mode_config.max_width = 8192;
  978. rdev->ddev->mode_config.max_height = 8192;
  979. } else {
  980. rdev->ddev->mode_config.max_width = 4096;
  981. rdev->ddev->mode_config.max_height = 4096;
  982. }
  983. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  984. ret = radeon_modeset_create_props(rdev);
  985. if (ret) {
  986. return ret;
  987. }
  988. /* init i2c buses */
  989. radeon_i2c_init(rdev);
  990. /* check combios for a valid hardcoded EDID - Sun servers */
  991. if (!rdev->is_atom_bios) {
  992. /* check for hardcoded EDID in BIOS */
  993. radeon_combios_check_hardcoded_edid(rdev);
  994. }
  995. /* allocate crtcs */
  996. for (i = 0; i < rdev->num_crtc; i++) {
  997. radeon_crtc_init(rdev->ddev, i);
  998. }
  999. /* okay we should have all the bios connectors */
  1000. ret = radeon_setup_enc_conn(rdev->ddev);
  1001. if (!ret) {
  1002. return ret;
  1003. }
  1004. /* initialize hpd */
  1005. radeon_hpd_init(rdev);
  1006. /* Initialize power management */
  1007. radeon_pm_init(rdev);
  1008. radeon_fbdev_init(rdev);
  1009. drm_kms_helper_poll_init(rdev->ddev);
  1010. return 0;
  1011. }
  1012. void radeon_modeset_fini(struct radeon_device *rdev)
  1013. {
  1014. radeon_fbdev_fini(rdev);
  1015. kfree(rdev->mode_info.bios_hardcoded_edid);
  1016. radeon_pm_fini(rdev);
  1017. if (rdev->mode_info.mode_config_initialized) {
  1018. drm_kms_helper_poll_fini(rdev->ddev);
  1019. radeon_hpd_fini(rdev);
  1020. drm_mode_config_cleanup(rdev->ddev);
  1021. rdev->mode_info.mode_config_initialized = false;
  1022. }
  1023. /* free i2c buses */
  1024. radeon_i2c_fini(rdev);
  1025. }
  1026. static bool is_hdtv_mode(struct drm_display_mode *mode)
  1027. {
  1028. /* try and guess if this is a tv or a monitor */
  1029. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1030. (mode->vdisplay == 576) || /* 576p */
  1031. (mode->vdisplay == 720) || /* 720p */
  1032. (mode->vdisplay == 1080)) /* 1080p */
  1033. return true;
  1034. else
  1035. return false;
  1036. }
  1037. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1038. struct drm_display_mode *mode,
  1039. struct drm_display_mode *adjusted_mode)
  1040. {
  1041. struct drm_device *dev = crtc->dev;
  1042. struct radeon_device *rdev = dev->dev_private;
  1043. struct drm_encoder *encoder;
  1044. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1045. struct radeon_encoder *radeon_encoder;
  1046. struct drm_connector *connector;
  1047. struct radeon_connector *radeon_connector;
  1048. bool first = true;
  1049. u32 src_v = 1, dst_v = 1;
  1050. u32 src_h = 1, dst_h = 1;
  1051. radeon_crtc->h_border = 0;
  1052. radeon_crtc->v_border = 0;
  1053. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1054. if (encoder->crtc != crtc)
  1055. continue;
  1056. radeon_encoder = to_radeon_encoder(encoder);
  1057. connector = radeon_get_connector_for_encoder(encoder);
  1058. radeon_connector = to_radeon_connector(connector);
  1059. if (first) {
  1060. /* set scaling */
  1061. if (radeon_encoder->rmx_type == RMX_OFF)
  1062. radeon_crtc->rmx_type = RMX_OFF;
  1063. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1064. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1065. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1066. else
  1067. radeon_crtc->rmx_type = RMX_OFF;
  1068. /* copy native mode */
  1069. memcpy(&radeon_crtc->native_mode,
  1070. &radeon_encoder->native_mode,
  1071. sizeof(struct drm_display_mode));
  1072. src_v = crtc->mode.vdisplay;
  1073. dst_v = radeon_crtc->native_mode.vdisplay;
  1074. src_h = crtc->mode.hdisplay;
  1075. dst_h = radeon_crtc->native_mode.hdisplay;
  1076. /* fix up for overscan on hdmi */
  1077. if (ASIC_IS_AVIVO(rdev) &&
  1078. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1079. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1080. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1081. drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1082. is_hdtv_mode(mode)))) {
  1083. if (radeon_encoder->underscan_hborder != 0)
  1084. radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1085. else
  1086. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1087. if (radeon_encoder->underscan_vborder != 0)
  1088. radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1089. else
  1090. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1091. radeon_crtc->rmx_type = RMX_FULL;
  1092. src_v = crtc->mode.vdisplay;
  1093. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1094. src_h = crtc->mode.hdisplay;
  1095. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1096. }
  1097. first = false;
  1098. } else {
  1099. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1100. /* WARNING: Right now this can't happen but
  1101. * in the future we need to check that scaling
  1102. * are consistent across different encoder
  1103. * (ie all encoder can work with the same
  1104. * scaling).
  1105. */
  1106. DRM_ERROR("Scaling not consistent across encoder.\n");
  1107. return false;
  1108. }
  1109. }
  1110. }
  1111. if (radeon_crtc->rmx_type != RMX_OFF) {
  1112. fixed20_12 a, b;
  1113. a.full = dfixed_const(src_v);
  1114. b.full = dfixed_const(dst_v);
  1115. radeon_crtc->vsc.full = dfixed_div(a, b);
  1116. a.full = dfixed_const(src_h);
  1117. b.full = dfixed_const(dst_h);
  1118. radeon_crtc->hsc.full = dfixed_div(a, b);
  1119. } else {
  1120. radeon_crtc->vsc.full = dfixed_const(1);
  1121. radeon_crtc->hsc.full = dfixed_const(1);
  1122. }
  1123. return true;
  1124. }
  1125. /*
  1126. * Retrieve current video scanout position of crtc on a given gpu.
  1127. *
  1128. * \param dev Device to query.
  1129. * \param crtc Crtc to query.
  1130. * \param *vpos Location where vertical scanout position should be stored.
  1131. * \param *hpos Location where horizontal scanout position should go.
  1132. *
  1133. * Returns vpos as a positive number while in active scanout area.
  1134. * Returns vpos as a negative number inside vblank, counting the number
  1135. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1136. * until start of active scanout / end of vblank."
  1137. *
  1138. * \return Flags, or'ed together as follows:
  1139. *
  1140. * DRM_SCANOUTPOS_VALID = Query successfull.
  1141. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1142. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1143. * this flag means that returned position may be offset by a constant but
  1144. * unknown small number of scanlines wrt. real scanout position.
  1145. *
  1146. */
  1147. int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, int *vpos, int *hpos)
  1148. {
  1149. u32 stat_crtc = 0, vbl = 0, position = 0;
  1150. int vbl_start, vbl_end, vtotal, ret = 0;
  1151. bool in_vbl = true;
  1152. struct radeon_device *rdev = dev->dev_private;
  1153. if (ASIC_IS_DCE4(rdev)) {
  1154. if (crtc == 0) {
  1155. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1156. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1157. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1158. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1159. ret |= DRM_SCANOUTPOS_VALID;
  1160. }
  1161. if (crtc == 1) {
  1162. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1163. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1164. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1165. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1166. ret |= DRM_SCANOUTPOS_VALID;
  1167. }
  1168. if (crtc == 2) {
  1169. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1170. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1171. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1172. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1173. ret |= DRM_SCANOUTPOS_VALID;
  1174. }
  1175. if (crtc == 3) {
  1176. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1177. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1178. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1179. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1180. ret |= DRM_SCANOUTPOS_VALID;
  1181. }
  1182. if (crtc == 4) {
  1183. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1184. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1185. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1186. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1187. ret |= DRM_SCANOUTPOS_VALID;
  1188. }
  1189. if (crtc == 5) {
  1190. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1191. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1192. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1193. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1194. ret |= DRM_SCANOUTPOS_VALID;
  1195. }
  1196. } else if (ASIC_IS_AVIVO(rdev)) {
  1197. if (crtc == 0) {
  1198. vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1199. position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1200. ret |= DRM_SCANOUTPOS_VALID;
  1201. }
  1202. if (crtc == 1) {
  1203. vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1204. position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1205. ret |= DRM_SCANOUTPOS_VALID;
  1206. }
  1207. } else {
  1208. /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1209. if (crtc == 0) {
  1210. /* Assume vbl_end == 0, get vbl_start from
  1211. * upper 16 bits.
  1212. */
  1213. vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1214. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1215. /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1216. position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1217. stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1218. if (!(stat_crtc & 1))
  1219. in_vbl = false;
  1220. ret |= DRM_SCANOUTPOS_VALID;
  1221. }
  1222. if (crtc == 1) {
  1223. vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1224. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1225. position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1226. stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1227. if (!(stat_crtc & 1))
  1228. in_vbl = false;
  1229. ret |= DRM_SCANOUTPOS_VALID;
  1230. }
  1231. }
  1232. /* Decode into vertical and horizontal scanout position. */
  1233. *vpos = position & 0x1fff;
  1234. *hpos = (position >> 16) & 0x1fff;
  1235. /* Valid vblank area boundaries from gpu retrieved? */
  1236. if (vbl > 0) {
  1237. /* Yes: Decode. */
  1238. ret |= DRM_SCANOUTPOS_ACCURATE;
  1239. vbl_start = vbl & 0x1fff;
  1240. vbl_end = (vbl >> 16) & 0x1fff;
  1241. }
  1242. else {
  1243. /* No: Fake something reasonable which gives at least ok results. */
  1244. vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1245. vbl_end = 0;
  1246. }
  1247. /* Test scanout position against vblank region. */
  1248. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1249. in_vbl = false;
  1250. /* Check if inside vblank area and apply corrective offsets:
  1251. * vpos will then be >=0 in video scanout area, but negative
  1252. * within vblank area, counting down the number of lines until
  1253. * start of scanout.
  1254. */
  1255. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1256. if (in_vbl && (*vpos >= vbl_start)) {
  1257. vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1258. *vpos = *vpos - vtotal;
  1259. }
  1260. /* Correct for shifted end of vbl at vbl_end. */
  1261. *vpos = *vpos - vbl_end;
  1262. /* In vblank? */
  1263. if (in_vbl)
  1264. ret |= DRM_SCANOUTPOS_INVBL;
  1265. return ret;
  1266. }