radeon_display.c 48 KB

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