radeon_display.c 51 KB

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