radeon_display.c 43 KB

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