radeon_display.c 51 KB

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