radeon_display.c 43 KB

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