radeon_display.c 53 KB

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