radeon_display.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598
  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 >=0) &&
  262. (vpos < (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100)) {
  263. /* crtc didn't flip in this target vblank interval,
  264. * but flip is pending in crtc. It will complete it
  265. * in next vblank interval, so complete the flip at
  266. * next vblank irq.
  267. */
  268. radeon_crtc->deferred_flip_completion = 1;
  269. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  270. return;
  271. }
  272. /* Pageflip (will be) certainly completed in this vblank. Clean up. */
  273. radeon_crtc->unpin_work = NULL;
  274. /* wakeup userspace */
  275. if (work->event) {
  276. e = work->event;
  277. e->event.sequence = drm_vblank_count_and_time(rdev->ddev, crtc_id, &now);
  278. e->event.tv_sec = now.tv_sec;
  279. e->event.tv_usec = now.tv_usec;
  280. list_add_tail(&e->base.link, &e->base.file_priv->event_list);
  281. wake_up_interruptible(&e->base.file_priv->event_wait);
  282. }
  283. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  284. drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
  285. radeon_fence_unref(&work->fence);
  286. radeon_post_page_flip(work->rdev, work->crtc_id);
  287. schedule_work(&work->work);
  288. }
  289. static int radeon_crtc_page_flip(struct drm_crtc *crtc,
  290. struct drm_framebuffer *fb,
  291. struct drm_pending_vblank_event *event)
  292. {
  293. struct drm_device *dev = crtc->dev;
  294. struct radeon_device *rdev = dev->dev_private;
  295. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  296. struct radeon_framebuffer *old_radeon_fb;
  297. struct radeon_framebuffer *new_radeon_fb;
  298. struct drm_gem_object *obj;
  299. struct radeon_bo *rbo;
  300. struct radeon_unpin_work *work;
  301. unsigned long flags;
  302. u32 tiling_flags, pitch_pixels;
  303. u64 base;
  304. int r;
  305. work = kzalloc(sizeof *work, GFP_KERNEL);
  306. if (work == NULL)
  307. return -ENOMEM;
  308. work->event = event;
  309. work->rdev = rdev;
  310. work->crtc_id = radeon_crtc->crtc_id;
  311. old_radeon_fb = to_radeon_framebuffer(crtc->fb);
  312. new_radeon_fb = to_radeon_framebuffer(fb);
  313. /* schedule unpin of the old buffer */
  314. obj = old_radeon_fb->obj;
  315. /* take a reference to the old object */
  316. drm_gem_object_reference(obj);
  317. rbo = gem_to_radeon_bo(obj);
  318. work->old_rbo = rbo;
  319. obj = new_radeon_fb->obj;
  320. rbo = gem_to_radeon_bo(obj);
  321. if (rbo->tbo.sync_obj)
  322. work->fence = radeon_fence_ref(rbo->tbo.sync_obj);
  323. INIT_WORK(&work->work, radeon_unpin_work_func);
  324. /* We borrow the event spin lock for protecting unpin_work */
  325. spin_lock_irqsave(&dev->event_lock, flags);
  326. if (radeon_crtc->unpin_work) {
  327. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  328. r = -EBUSY;
  329. goto unlock_free;
  330. }
  331. radeon_crtc->unpin_work = work;
  332. radeon_crtc->deferred_flip_completion = 0;
  333. spin_unlock_irqrestore(&dev->event_lock, flags);
  334. /* pin the new buffer */
  335. DRM_DEBUG_DRIVER("flip-ioctl() cur_fbo = %p, cur_bbo = %p\n",
  336. work->old_rbo, rbo);
  337. r = radeon_bo_reserve(rbo, false);
  338. if (unlikely(r != 0)) {
  339. DRM_ERROR("failed to reserve new rbo buffer before flip\n");
  340. goto pflip_cleanup;
  341. }
  342. r = radeon_bo_pin(rbo, RADEON_GEM_DOMAIN_VRAM, &base);
  343. if (unlikely(r != 0)) {
  344. radeon_bo_unreserve(rbo);
  345. r = -EINVAL;
  346. DRM_ERROR("failed to pin new rbo buffer before flip\n");
  347. goto pflip_cleanup;
  348. }
  349. radeon_bo_get_tiling_flags(rbo, &tiling_flags, NULL);
  350. radeon_bo_unreserve(rbo);
  351. if (!ASIC_IS_AVIVO(rdev)) {
  352. /* crtc offset is from display base addr not FB location */
  353. base -= radeon_crtc->legacy_display_base_addr;
  354. pitch_pixels = fb->pitch / (fb->bits_per_pixel / 8);
  355. if (tiling_flags & RADEON_TILING_MACRO) {
  356. if (ASIC_IS_R300(rdev)) {
  357. base &= ~0x7ff;
  358. } else {
  359. int byteshift = fb->bits_per_pixel >> 4;
  360. int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
  361. base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
  362. }
  363. } else {
  364. int offset = crtc->y * pitch_pixels + crtc->x;
  365. switch (fb->bits_per_pixel) {
  366. case 8:
  367. default:
  368. offset *= 1;
  369. break;
  370. case 15:
  371. case 16:
  372. offset *= 2;
  373. break;
  374. case 24:
  375. offset *= 3;
  376. break;
  377. case 32:
  378. offset *= 4;
  379. break;
  380. }
  381. base += offset;
  382. }
  383. base &= ~7;
  384. }
  385. spin_lock_irqsave(&dev->event_lock, flags);
  386. work->new_crtc_base = base;
  387. spin_unlock_irqrestore(&dev->event_lock, flags);
  388. /* update crtc fb */
  389. crtc->fb = fb;
  390. r = drm_vblank_get(dev, radeon_crtc->crtc_id);
  391. if (r) {
  392. DRM_ERROR("failed to get vblank before flip\n");
  393. goto pflip_cleanup1;
  394. }
  395. /* set the proper interrupt */
  396. radeon_pre_page_flip(rdev, radeon_crtc->crtc_id);
  397. return 0;
  398. pflip_cleanup1:
  399. if (unlikely(radeon_bo_reserve(rbo, false) != 0)) {
  400. DRM_ERROR("failed to reserve new rbo in error path\n");
  401. goto pflip_cleanup;
  402. }
  403. if (unlikely(radeon_bo_unpin(rbo) != 0)) {
  404. DRM_ERROR("failed to unpin new rbo in error path\n");
  405. }
  406. radeon_bo_unreserve(rbo);
  407. pflip_cleanup:
  408. spin_lock_irqsave(&dev->event_lock, flags);
  409. radeon_crtc->unpin_work = NULL;
  410. unlock_free:
  411. spin_unlock_irqrestore(&dev->event_lock, flags);
  412. drm_gem_object_unreference_unlocked(old_radeon_fb->obj);
  413. radeon_fence_unref(&work->fence);
  414. kfree(work);
  415. return r;
  416. }
  417. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  418. .cursor_set = radeon_crtc_cursor_set,
  419. .cursor_move = radeon_crtc_cursor_move,
  420. .gamma_set = radeon_crtc_gamma_set,
  421. .set_config = drm_crtc_helper_set_config,
  422. .destroy = radeon_crtc_destroy,
  423. .page_flip = radeon_crtc_page_flip,
  424. };
  425. static void radeon_crtc_init(struct drm_device *dev, int index)
  426. {
  427. struct radeon_device *rdev = dev->dev_private;
  428. struct radeon_crtc *radeon_crtc;
  429. int i;
  430. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  431. if (radeon_crtc == NULL)
  432. return;
  433. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  434. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  435. radeon_crtc->crtc_id = index;
  436. rdev->mode_info.crtcs[index] = radeon_crtc;
  437. #if 0
  438. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  439. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  440. radeon_crtc->mode_set.num_connectors = 0;
  441. #endif
  442. for (i = 0; i < 256; i++) {
  443. radeon_crtc->lut_r[i] = i << 2;
  444. radeon_crtc->lut_g[i] = i << 2;
  445. radeon_crtc->lut_b[i] = i << 2;
  446. }
  447. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  448. radeon_atombios_init_crtc(dev, radeon_crtc);
  449. else
  450. radeon_legacy_init_crtc(dev, radeon_crtc);
  451. }
  452. static const char *encoder_names[36] = {
  453. "NONE",
  454. "INTERNAL_LVDS",
  455. "INTERNAL_TMDS1",
  456. "INTERNAL_TMDS2",
  457. "INTERNAL_DAC1",
  458. "INTERNAL_DAC2",
  459. "INTERNAL_SDVOA",
  460. "INTERNAL_SDVOB",
  461. "SI170B",
  462. "CH7303",
  463. "CH7301",
  464. "INTERNAL_DVO1",
  465. "EXTERNAL_SDVOA",
  466. "EXTERNAL_SDVOB",
  467. "TITFP513",
  468. "INTERNAL_LVTM1",
  469. "VT1623",
  470. "HDMI_SI1930",
  471. "HDMI_INTERNAL",
  472. "INTERNAL_KLDSCP_TMDS1",
  473. "INTERNAL_KLDSCP_DVO1",
  474. "INTERNAL_KLDSCP_DAC1",
  475. "INTERNAL_KLDSCP_DAC2",
  476. "SI178",
  477. "MVPU_FPGA",
  478. "INTERNAL_DDI",
  479. "VT1625",
  480. "HDMI_SI1932",
  481. "DP_AN9801",
  482. "DP_DP501",
  483. "INTERNAL_UNIPHY",
  484. "INTERNAL_KLDSCP_LVTMA",
  485. "INTERNAL_UNIPHY1",
  486. "INTERNAL_UNIPHY2",
  487. "NUTMEG",
  488. "TRAVIS",
  489. };
  490. static const char *connector_names[15] = {
  491. "Unknown",
  492. "VGA",
  493. "DVI-I",
  494. "DVI-D",
  495. "DVI-A",
  496. "Composite",
  497. "S-video",
  498. "LVDS",
  499. "Component",
  500. "DIN",
  501. "DisplayPort",
  502. "HDMI-A",
  503. "HDMI-B",
  504. "TV",
  505. "eDP",
  506. };
  507. static const char *hpd_names[6] = {
  508. "HPD1",
  509. "HPD2",
  510. "HPD3",
  511. "HPD4",
  512. "HPD5",
  513. "HPD6",
  514. };
  515. static void radeon_print_display_setup(struct drm_device *dev)
  516. {
  517. struct drm_connector *connector;
  518. struct radeon_connector *radeon_connector;
  519. struct drm_encoder *encoder;
  520. struct radeon_encoder *radeon_encoder;
  521. uint32_t devices;
  522. int i = 0;
  523. DRM_INFO("Radeon Display Connectors\n");
  524. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  525. radeon_connector = to_radeon_connector(connector);
  526. DRM_INFO("Connector %d:\n", i);
  527. DRM_INFO(" %s\n", connector_names[connector->connector_type]);
  528. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  529. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  530. if (radeon_connector->ddc_bus) {
  531. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  532. radeon_connector->ddc_bus->rec.mask_clk_reg,
  533. radeon_connector->ddc_bus->rec.mask_data_reg,
  534. radeon_connector->ddc_bus->rec.a_clk_reg,
  535. radeon_connector->ddc_bus->rec.a_data_reg,
  536. radeon_connector->ddc_bus->rec.en_clk_reg,
  537. radeon_connector->ddc_bus->rec.en_data_reg,
  538. radeon_connector->ddc_bus->rec.y_clk_reg,
  539. radeon_connector->ddc_bus->rec.y_data_reg);
  540. if (radeon_connector->router.ddc_valid)
  541. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  542. radeon_connector->router.ddc_mux_control_pin,
  543. radeon_connector->router.ddc_mux_state);
  544. if (radeon_connector->router.cd_valid)
  545. DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
  546. radeon_connector->router.cd_mux_control_pin,
  547. radeon_connector->router.cd_mux_state);
  548. } else {
  549. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  550. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  551. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  552. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  553. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  554. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  555. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  556. }
  557. DRM_INFO(" Encoders:\n");
  558. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  559. radeon_encoder = to_radeon_encoder(encoder);
  560. devices = radeon_encoder->devices & radeon_connector->devices;
  561. if (devices) {
  562. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  563. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  564. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  565. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  566. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  567. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  568. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  569. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  570. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  571. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  572. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  573. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  574. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  575. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  576. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  577. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  578. if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  579. DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
  580. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  581. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  582. if (devices & ATOM_DEVICE_CV_SUPPORT)
  583. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  584. }
  585. }
  586. i++;
  587. }
  588. }
  589. static bool radeon_setup_enc_conn(struct drm_device *dev)
  590. {
  591. struct radeon_device *rdev = dev->dev_private;
  592. bool ret = false;
  593. if (rdev->bios) {
  594. if (rdev->is_atom_bios) {
  595. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  596. if (ret == false)
  597. ret = radeon_get_atom_connector_info_from_object_table(dev);
  598. } else {
  599. ret = radeon_get_legacy_connector_info_from_bios(dev);
  600. if (ret == false)
  601. ret = radeon_get_legacy_connector_info_from_table(dev);
  602. }
  603. } else {
  604. if (!ASIC_IS_AVIVO(rdev))
  605. ret = radeon_get_legacy_connector_info_from_table(dev);
  606. }
  607. if (ret) {
  608. radeon_setup_encoder_clones(dev);
  609. radeon_print_display_setup(dev);
  610. }
  611. return ret;
  612. }
  613. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  614. {
  615. struct drm_device *dev = radeon_connector->base.dev;
  616. struct radeon_device *rdev = dev->dev_private;
  617. int ret = 0;
  618. /* on hw with routers, select right port */
  619. if (radeon_connector->router.ddc_valid)
  620. radeon_router_select_ddc_port(radeon_connector);
  621. if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  622. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP) ||
  623. (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
  624. ENCODER_OBJECT_ID_NONE)) {
  625. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  626. if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  627. dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  628. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  629. &dig->dp_i2c_bus->adapter);
  630. else if (radeon_connector->ddc_bus && !radeon_connector->edid)
  631. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  632. &radeon_connector->ddc_bus->adapter);
  633. } else {
  634. if (radeon_connector->ddc_bus && !radeon_connector->edid)
  635. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  636. &radeon_connector->ddc_bus->adapter);
  637. }
  638. if (!radeon_connector->edid) {
  639. if (rdev->is_atom_bios) {
  640. /* some laptops provide a hardcoded edid in rom for LCDs */
  641. if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
  642. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
  643. radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  644. } else
  645. /* some servers provide a hardcoded edid in rom for KVMs */
  646. radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  647. }
  648. if (radeon_connector->edid) {
  649. drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  650. ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  651. return ret;
  652. }
  653. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  654. return 0;
  655. }
  656. /* avivo */
  657. static void avivo_get_fb_div(struct radeon_pll *pll,
  658. u32 target_clock,
  659. u32 post_div,
  660. u32 ref_div,
  661. u32 *fb_div,
  662. u32 *frac_fb_div)
  663. {
  664. u32 tmp = post_div * ref_div;
  665. tmp *= target_clock;
  666. *fb_div = tmp / pll->reference_freq;
  667. *frac_fb_div = tmp % pll->reference_freq;
  668. if (*fb_div > pll->max_feedback_div)
  669. *fb_div = pll->max_feedback_div;
  670. else if (*fb_div < pll->min_feedback_div)
  671. *fb_div = pll->min_feedback_div;
  672. }
  673. static u32 avivo_get_post_div(struct radeon_pll *pll,
  674. u32 target_clock)
  675. {
  676. u32 vco, post_div, tmp;
  677. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  678. return pll->post_div;
  679. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  680. if (pll->flags & RADEON_PLL_IS_LCD)
  681. vco = pll->lcd_pll_out_min;
  682. else
  683. vco = pll->pll_out_min;
  684. } else {
  685. if (pll->flags & RADEON_PLL_IS_LCD)
  686. vco = pll->lcd_pll_out_max;
  687. else
  688. vco = pll->pll_out_max;
  689. }
  690. post_div = vco / target_clock;
  691. tmp = vco % target_clock;
  692. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  693. if (tmp)
  694. post_div++;
  695. } else {
  696. if (!tmp)
  697. post_div--;
  698. }
  699. if (post_div > pll->max_post_div)
  700. post_div = pll->max_post_div;
  701. else if (post_div < pll->min_post_div)
  702. post_div = pll->min_post_div;
  703. return post_div;
  704. }
  705. #define MAX_TOLERANCE 10
  706. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  707. u32 freq,
  708. u32 *dot_clock_p,
  709. u32 *fb_div_p,
  710. u32 *frac_fb_div_p,
  711. u32 *ref_div_p,
  712. u32 *post_div_p)
  713. {
  714. u32 target_clock = freq / 10;
  715. u32 post_div = avivo_get_post_div(pll, target_clock);
  716. u32 ref_div = pll->min_ref_div;
  717. u32 fb_div = 0, frac_fb_div = 0, tmp;
  718. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  719. ref_div = pll->reference_div;
  720. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  721. avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
  722. frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
  723. if (frac_fb_div >= 5) {
  724. frac_fb_div -= 5;
  725. frac_fb_div = frac_fb_div / 10;
  726. frac_fb_div++;
  727. }
  728. if (frac_fb_div >= 10) {
  729. fb_div++;
  730. frac_fb_div = 0;
  731. }
  732. } else {
  733. while (ref_div <= pll->max_ref_div) {
  734. avivo_get_fb_div(pll, target_clock, post_div, ref_div,
  735. &fb_div, &frac_fb_div);
  736. if (frac_fb_div >= (pll->reference_freq / 2))
  737. fb_div++;
  738. frac_fb_div = 0;
  739. tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
  740. tmp = (tmp * 10000) / target_clock;
  741. if (tmp > (10000 + MAX_TOLERANCE))
  742. ref_div++;
  743. else if (tmp >= (10000 - MAX_TOLERANCE))
  744. break;
  745. else
  746. ref_div++;
  747. }
  748. }
  749. *dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
  750. (ref_div * post_div * 10);
  751. *fb_div_p = fb_div;
  752. *frac_fb_div_p = frac_fb_div;
  753. *ref_div_p = ref_div;
  754. *post_div_p = post_div;
  755. DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  756. *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
  757. }
  758. /* pre-avivo */
  759. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  760. {
  761. uint64_t mod;
  762. n += d / 2;
  763. mod = do_div(n, d);
  764. return n;
  765. }
  766. void radeon_compute_pll_legacy(struct radeon_pll *pll,
  767. uint64_t freq,
  768. uint32_t *dot_clock_p,
  769. uint32_t *fb_div_p,
  770. uint32_t *frac_fb_div_p,
  771. uint32_t *ref_div_p,
  772. uint32_t *post_div_p)
  773. {
  774. uint32_t min_ref_div = pll->min_ref_div;
  775. uint32_t max_ref_div = pll->max_ref_div;
  776. uint32_t min_post_div = pll->min_post_div;
  777. uint32_t max_post_div = pll->max_post_div;
  778. uint32_t min_fractional_feed_div = 0;
  779. uint32_t max_fractional_feed_div = 0;
  780. uint32_t best_vco = pll->best_vco;
  781. uint32_t best_post_div = 1;
  782. uint32_t best_ref_div = 1;
  783. uint32_t best_feedback_div = 1;
  784. uint32_t best_frac_feedback_div = 0;
  785. uint32_t best_freq = -1;
  786. uint32_t best_error = 0xffffffff;
  787. uint32_t best_vco_diff = 1;
  788. uint32_t post_div;
  789. u32 pll_out_min, pll_out_max;
  790. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  791. freq = freq * 1000;
  792. if (pll->flags & RADEON_PLL_IS_LCD) {
  793. pll_out_min = pll->lcd_pll_out_min;
  794. pll_out_max = pll->lcd_pll_out_max;
  795. } else {
  796. pll_out_min = pll->pll_out_min;
  797. pll_out_max = pll->pll_out_max;
  798. }
  799. if (pll_out_min > 64800)
  800. pll_out_min = 64800;
  801. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  802. min_ref_div = max_ref_div = pll->reference_div;
  803. else {
  804. while (min_ref_div < max_ref_div-1) {
  805. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  806. uint32_t pll_in = pll->reference_freq / mid;
  807. if (pll_in < pll->pll_in_min)
  808. max_ref_div = mid;
  809. else if (pll_in > pll->pll_in_max)
  810. min_ref_div = mid;
  811. else
  812. break;
  813. }
  814. }
  815. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  816. min_post_div = max_post_div = pll->post_div;
  817. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  818. min_fractional_feed_div = pll->min_frac_feedback_div;
  819. max_fractional_feed_div = pll->max_frac_feedback_div;
  820. }
  821. for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  822. uint32_t ref_div;
  823. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  824. continue;
  825. /* legacy radeons only have a few post_divs */
  826. if (pll->flags & RADEON_PLL_LEGACY) {
  827. if ((post_div == 5) ||
  828. (post_div == 7) ||
  829. (post_div == 9) ||
  830. (post_div == 10) ||
  831. (post_div == 11) ||
  832. (post_div == 13) ||
  833. (post_div == 14) ||
  834. (post_div == 15))
  835. continue;
  836. }
  837. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  838. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  839. uint32_t pll_in = pll->reference_freq / ref_div;
  840. uint32_t min_feed_div = pll->min_feedback_div;
  841. uint32_t max_feed_div = pll->max_feedback_div + 1;
  842. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  843. continue;
  844. while (min_feed_div < max_feed_div) {
  845. uint32_t vco;
  846. uint32_t min_frac_feed_div = min_fractional_feed_div;
  847. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  848. uint32_t frac_feedback_div;
  849. uint64_t tmp;
  850. feedback_div = (min_feed_div + max_feed_div) / 2;
  851. tmp = (uint64_t)pll->reference_freq * feedback_div;
  852. vco = radeon_div(tmp, ref_div);
  853. if (vco < pll_out_min) {
  854. min_feed_div = feedback_div + 1;
  855. continue;
  856. } else if (vco > pll_out_max) {
  857. max_feed_div = feedback_div;
  858. continue;
  859. }
  860. while (min_frac_feed_div < max_frac_feed_div) {
  861. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  862. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  863. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  864. current_freq = radeon_div(tmp, ref_div * post_div);
  865. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  866. if (freq < current_freq)
  867. error = 0xffffffff;
  868. else
  869. error = freq - current_freq;
  870. } else
  871. error = abs(current_freq - freq);
  872. vco_diff = abs(vco - best_vco);
  873. if ((best_vco == 0 && error < best_error) ||
  874. (best_vco != 0 &&
  875. ((best_error > 100 && error < best_error - 100) ||
  876. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  877. best_post_div = post_div;
  878. best_ref_div = ref_div;
  879. best_feedback_div = feedback_div;
  880. best_frac_feedback_div = frac_feedback_div;
  881. best_freq = current_freq;
  882. best_error = error;
  883. best_vco_diff = vco_diff;
  884. } else if (current_freq == freq) {
  885. if (best_freq == -1) {
  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 (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  894. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  895. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  896. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  897. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  898. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  899. best_post_div = post_div;
  900. best_ref_div = ref_div;
  901. best_feedback_div = feedback_div;
  902. best_frac_feedback_div = frac_feedback_div;
  903. best_freq = current_freq;
  904. best_error = error;
  905. best_vco_diff = vco_diff;
  906. }
  907. }
  908. if (current_freq < freq)
  909. min_frac_feed_div = frac_feedback_div + 1;
  910. else
  911. max_frac_feed_div = frac_feedback_div;
  912. }
  913. if (current_freq < freq)
  914. min_feed_div = feedback_div + 1;
  915. else
  916. max_feed_div = feedback_div;
  917. }
  918. }
  919. }
  920. *dot_clock_p = best_freq / 10000;
  921. *fb_div_p = best_feedback_div;
  922. *frac_fb_div_p = best_frac_feedback_div;
  923. *ref_div_p = best_ref_div;
  924. *post_div_p = best_post_div;
  925. DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  926. (long long)freq,
  927. best_freq / 1000, best_feedback_div, best_frac_feedback_div,
  928. best_ref_div, best_post_div);
  929. }
  930. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  931. {
  932. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  933. if (radeon_fb->obj) {
  934. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  935. }
  936. drm_framebuffer_cleanup(fb);
  937. kfree(radeon_fb);
  938. }
  939. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  940. struct drm_file *file_priv,
  941. unsigned int *handle)
  942. {
  943. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  944. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  945. }
  946. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  947. .destroy = radeon_user_framebuffer_destroy,
  948. .create_handle = radeon_user_framebuffer_create_handle,
  949. };
  950. void
  951. radeon_framebuffer_init(struct drm_device *dev,
  952. struct radeon_framebuffer *rfb,
  953. struct drm_mode_fb_cmd *mode_cmd,
  954. struct drm_gem_object *obj)
  955. {
  956. rfb->obj = obj;
  957. drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  958. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  959. }
  960. static struct drm_framebuffer *
  961. radeon_user_framebuffer_create(struct drm_device *dev,
  962. struct drm_file *file_priv,
  963. struct drm_mode_fb_cmd *mode_cmd)
  964. {
  965. struct drm_gem_object *obj;
  966. struct radeon_framebuffer *radeon_fb;
  967. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  968. if (obj == NULL) {
  969. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  970. "can't create framebuffer\n", mode_cmd->handle);
  971. return ERR_PTR(-ENOENT);
  972. }
  973. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  974. if (radeon_fb == NULL)
  975. return ERR_PTR(-ENOMEM);
  976. radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  977. return &radeon_fb->base;
  978. }
  979. static void radeon_output_poll_changed(struct drm_device *dev)
  980. {
  981. struct radeon_device *rdev = dev->dev_private;
  982. radeon_fb_output_poll_changed(rdev);
  983. }
  984. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  985. .fb_create = radeon_user_framebuffer_create,
  986. .output_poll_changed = radeon_output_poll_changed
  987. };
  988. struct drm_prop_enum_list {
  989. int type;
  990. char *name;
  991. };
  992. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  993. { { 0, "driver" },
  994. { 1, "bios" },
  995. };
  996. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  997. { { TV_STD_NTSC, "ntsc" },
  998. { TV_STD_PAL, "pal" },
  999. { TV_STD_PAL_M, "pal-m" },
  1000. { TV_STD_PAL_60, "pal-60" },
  1001. { TV_STD_NTSC_J, "ntsc-j" },
  1002. { TV_STD_SCART_PAL, "scart-pal" },
  1003. { TV_STD_PAL_CN, "pal-cn" },
  1004. { TV_STD_SECAM, "secam" },
  1005. };
  1006. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  1007. { { UNDERSCAN_OFF, "off" },
  1008. { UNDERSCAN_ON, "on" },
  1009. { UNDERSCAN_AUTO, "auto" },
  1010. };
  1011. static int radeon_modeset_create_props(struct radeon_device *rdev)
  1012. {
  1013. int i, sz;
  1014. if (rdev->is_atom_bios) {
  1015. rdev->mode_info.coherent_mode_property =
  1016. drm_property_create(rdev->ddev,
  1017. DRM_MODE_PROP_RANGE,
  1018. "coherent", 2);
  1019. if (!rdev->mode_info.coherent_mode_property)
  1020. return -ENOMEM;
  1021. rdev->mode_info.coherent_mode_property->values[0] = 0;
  1022. rdev->mode_info.coherent_mode_property->values[1] = 1;
  1023. }
  1024. if (!ASIC_IS_AVIVO(rdev)) {
  1025. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  1026. rdev->mode_info.tmds_pll_property =
  1027. drm_property_create(rdev->ddev,
  1028. DRM_MODE_PROP_ENUM,
  1029. "tmds_pll", sz);
  1030. for (i = 0; i < sz; i++) {
  1031. drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  1032. i,
  1033. radeon_tmds_pll_enum_list[i].type,
  1034. radeon_tmds_pll_enum_list[i].name);
  1035. }
  1036. }
  1037. rdev->mode_info.load_detect_property =
  1038. drm_property_create(rdev->ddev,
  1039. DRM_MODE_PROP_RANGE,
  1040. "load detection", 2);
  1041. if (!rdev->mode_info.load_detect_property)
  1042. return -ENOMEM;
  1043. rdev->mode_info.load_detect_property->values[0] = 0;
  1044. rdev->mode_info.load_detect_property->values[1] = 1;
  1045. drm_mode_create_scaling_mode_property(rdev->ddev);
  1046. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  1047. rdev->mode_info.tv_std_property =
  1048. drm_property_create(rdev->ddev,
  1049. DRM_MODE_PROP_ENUM,
  1050. "tv standard", sz);
  1051. for (i = 0; i < sz; i++) {
  1052. drm_property_add_enum(rdev->mode_info.tv_std_property,
  1053. i,
  1054. radeon_tv_std_enum_list[i].type,
  1055. radeon_tv_std_enum_list[i].name);
  1056. }
  1057. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  1058. rdev->mode_info.underscan_property =
  1059. drm_property_create(rdev->ddev,
  1060. DRM_MODE_PROP_ENUM,
  1061. "underscan", sz);
  1062. for (i = 0; i < sz; i++) {
  1063. drm_property_add_enum(rdev->mode_info.underscan_property,
  1064. i,
  1065. radeon_underscan_enum_list[i].type,
  1066. radeon_underscan_enum_list[i].name);
  1067. }
  1068. rdev->mode_info.underscan_hborder_property =
  1069. drm_property_create(rdev->ddev,
  1070. DRM_MODE_PROP_RANGE,
  1071. "underscan hborder", 2);
  1072. if (!rdev->mode_info.underscan_hborder_property)
  1073. return -ENOMEM;
  1074. rdev->mode_info.underscan_hborder_property->values[0] = 0;
  1075. rdev->mode_info.underscan_hborder_property->values[1] = 128;
  1076. rdev->mode_info.underscan_vborder_property =
  1077. drm_property_create(rdev->ddev,
  1078. DRM_MODE_PROP_RANGE,
  1079. "underscan vborder", 2);
  1080. if (!rdev->mode_info.underscan_vborder_property)
  1081. return -ENOMEM;
  1082. rdev->mode_info.underscan_vborder_property->values[0] = 0;
  1083. rdev->mode_info.underscan_vborder_property->values[1] = 128;
  1084. return 0;
  1085. }
  1086. void radeon_update_display_priority(struct radeon_device *rdev)
  1087. {
  1088. /* adjustment options for the display watermarks */
  1089. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  1090. /* set display priority to high for r3xx, rv515 chips
  1091. * this avoids flickering due to underflow to the
  1092. * display controllers during heavy acceleration.
  1093. * Don't force high on rs4xx igp chips as it seems to
  1094. * affect the sound card. See kernel bug 15982.
  1095. */
  1096. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  1097. !(rdev->flags & RADEON_IS_IGP))
  1098. rdev->disp_priority = 2;
  1099. else
  1100. rdev->disp_priority = 0;
  1101. } else
  1102. rdev->disp_priority = radeon_disp_priority;
  1103. }
  1104. int radeon_modeset_init(struct radeon_device *rdev)
  1105. {
  1106. int i;
  1107. int ret;
  1108. drm_mode_config_init(rdev->ddev);
  1109. rdev->mode_info.mode_config_initialized = true;
  1110. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  1111. if (ASIC_IS_DCE5(rdev)) {
  1112. rdev->ddev->mode_config.max_width = 16384;
  1113. rdev->ddev->mode_config.max_height = 16384;
  1114. } else if (ASIC_IS_AVIVO(rdev)) {
  1115. rdev->ddev->mode_config.max_width = 8192;
  1116. rdev->ddev->mode_config.max_height = 8192;
  1117. } else {
  1118. rdev->ddev->mode_config.max_width = 4096;
  1119. rdev->ddev->mode_config.max_height = 4096;
  1120. }
  1121. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  1122. ret = radeon_modeset_create_props(rdev);
  1123. if (ret) {
  1124. return ret;
  1125. }
  1126. /* init i2c buses */
  1127. radeon_i2c_init(rdev);
  1128. /* check combios for a valid hardcoded EDID - Sun servers */
  1129. if (!rdev->is_atom_bios) {
  1130. /* check for hardcoded EDID in BIOS */
  1131. radeon_combios_check_hardcoded_edid(rdev);
  1132. }
  1133. /* allocate crtcs */
  1134. for (i = 0; i < rdev->num_crtc; i++) {
  1135. radeon_crtc_init(rdev->ddev, i);
  1136. }
  1137. /* okay we should have all the bios connectors */
  1138. ret = radeon_setup_enc_conn(rdev->ddev);
  1139. if (!ret) {
  1140. return ret;
  1141. }
  1142. /* init dig PHYs */
  1143. if (rdev->is_atom_bios)
  1144. radeon_atom_encoder_init(rdev);
  1145. /* initialize hpd */
  1146. radeon_hpd_init(rdev);
  1147. /* Initialize power management */
  1148. radeon_pm_init(rdev);
  1149. radeon_fbdev_init(rdev);
  1150. drm_kms_helper_poll_init(rdev->ddev);
  1151. return 0;
  1152. }
  1153. void radeon_modeset_fini(struct radeon_device *rdev)
  1154. {
  1155. radeon_fbdev_fini(rdev);
  1156. kfree(rdev->mode_info.bios_hardcoded_edid);
  1157. radeon_pm_fini(rdev);
  1158. if (rdev->mode_info.mode_config_initialized) {
  1159. drm_kms_helper_poll_fini(rdev->ddev);
  1160. radeon_hpd_fini(rdev);
  1161. drm_mode_config_cleanup(rdev->ddev);
  1162. rdev->mode_info.mode_config_initialized = false;
  1163. }
  1164. /* free i2c buses */
  1165. radeon_i2c_fini(rdev);
  1166. }
  1167. static bool is_hdtv_mode(struct drm_display_mode *mode)
  1168. {
  1169. /* try and guess if this is a tv or a monitor */
  1170. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1171. (mode->vdisplay == 576) || /* 576p */
  1172. (mode->vdisplay == 720) || /* 720p */
  1173. (mode->vdisplay == 1080)) /* 1080p */
  1174. return true;
  1175. else
  1176. return false;
  1177. }
  1178. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1179. struct drm_display_mode *mode,
  1180. struct drm_display_mode *adjusted_mode)
  1181. {
  1182. struct drm_device *dev = crtc->dev;
  1183. struct radeon_device *rdev = dev->dev_private;
  1184. struct drm_encoder *encoder;
  1185. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1186. struct radeon_encoder *radeon_encoder;
  1187. struct drm_connector *connector;
  1188. struct radeon_connector *radeon_connector;
  1189. bool first = true;
  1190. u32 src_v = 1, dst_v = 1;
  1191. u32 src_h = 1, dst_h = 1;
  1192. radeon_crtc->h_border = 0;
  1193. radeon_crtc->v_border = 0;
  1194. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1195. if (encoder->crtc != crtc)
  1196. continue;
  1197. radeon_encoder = to_radeon_encoder(encoder);
  1198. connector = radeon_get_connector_for_encoder(encoder);
  1199. radeon_connector = to_radeon_connector(connector);
  1200. if (first) {
  1201. /* set scaling */
  1202. if (radeon_encoder->rmx_type == RMX_OFF)
  1203. radeon_crtc->rmx_type = RMX_OFF;
  1204. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1205. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1206. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1207. else
  1208. radeon_crtc->rmx_type = RMX_OFF;
  1209. /* copy native mode */
  1210. memcpy(&radeon_crtc->native_mode,
  1211. &radeon_encoder->native_mode,
  1212. sizeof(struct drm_display_mode));
  1213. src_v = crtc->mode.vdisplay;
  1214. dst_v = radeon_crtc->native_mode.vdisplay;
  1215. src_h = crtc->mode.hdisplay;
  1216. dst_h = radeon_crtc->native_mode.hdisplay;
  1217. /* fix up for overscan on hdmi */
  1218. if (ASIC_IS_AVIVO(rdev) &&
  1219. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1220. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1221. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1222. drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1223. is_hdtv_mode(mode)))) {
  1224. if (radeon_encoder->underscan_hborder != 0)
  1225. radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1226. else
  1227. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1228. if (radeon_encoder->underscan_vborder != 0)
  1229. radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1230. else
  1231. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1232. radeon_crtc->rmx_type = RMX_FULL;
  1233. src_v = crtc->mode.vdisplay;
  1234. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1235. src_h = crtc->mode.hdisplay;
  1236. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1237. }
  1238. first = false;
  1239. } else {
  1240. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1241. /* WARNING: Right now this can't happen but
  1242. * in the future we need to check that scaling
  1243. * are consistent across different encoder
  1244. * (ie all encoder can work with the same
  1245. * scaling).
  1246. */
  1247. DRM_ERROR("Scaling not consistent across encoder.\n");
  1248. return false;
  1249. }
  1250. }
  1251. }
  1252. if (radeon_crtc->rmx_type != RMX_OFF) {
  1253. fixed20_12 a, b;
  1254. a.full = dfixed_const(src_v);
  1255. b.full = dfixed_const(dst_v);
  1256. radeon_crtc->vsc.full = dfixed_div(a, b);
  1257. a.full = dfixed_const(src_h);
  1258. b.full = dfixed_const(dst_h);
  1259. radeon_crtc->hsc.full = dfixed_div(a, b);
  1260. } else {
  1261. radeon_crtc->vsc.full = dfixed_const(1);
  1262. radeon_crtc->hsc.full = dfixed_const(1);
  1263. }
  1264. return true;
  1265. }
  1266. /*
  1267. * Retrieve current video scanout position of crtc on a given gpu.
  1268. *
  1269. * \param dev Device to query.
  1270. * \param crtc Crtc to query.
  1271. * \param *vpos Location where vertical scanout position should be stored.
  1272. * \param *hpos Location where horizontal scanout position should go.
  1273. *
  1274. * Returns vpos as a positive number while in active scanout area.
  1275. * Returns vpos as a negative number inside vblank, counting the number
  1276. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1277. * until start of active scanout / end of vblank."
  1278. *
  1279. * \return Flags, or'ed together as follows:
  1280. *
  1281. * DRM_SCANOUTPOS_VALID = Query successful.
  1282. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1283. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1284. * this flag means that returned position may be offset by a constant but
  1285. * unknown small number of scanlines wrt. real scanout position.
  1286. *
  1287. */
  1288. int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, int *vpos, int *hpos)
  1289. {
  1290. u32 stat_crtc = 0, vbl = 0, position = 0;
  1291. int vbl_start, vbl_end, vtotal, ret = 0;
  1292. bool in_vbl = true;
  1293. struct radeon_device *rdev = dev->dev_private;
  1294. if (ASIC_IS_DCE4(rdev)) {
  1295. if (crtc == 0) {
  1296. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1297. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1298. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1299. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1300. ret |= DRM_SCANOUTPOS_VALID;
  1301. }
  1302. if (crtc == 1) {
  1303. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1304. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1305. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1306. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1307. ret |= DRM_SCANOUTPOS_VALID;
  1308. }
  1309. if (crtc == 2) {
  1310. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1311. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1312. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1313. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1314. ret |= DRM_SCANOUTPOS_VALID;
  1315. }
  1316. if (crtc == 3) {
  1317. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1318. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1319. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1320. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1321. ret |= DRM_SCANOUTPOS_VALID;
  1322. }
  1323. if (crtc == 4) {
  1324. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1325. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1326. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1327. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1328. ret |= DRM_SCANOUTPOS_VALID;
  1329. }
  1330. if (crtc == 5) {
  1331. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1332. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1333. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1334. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1335. ret |= DRM_SCANOUTPOS_VALID;
  1336. }
  1337. } else if (ASIC_IS_AVIVO(rdev)) {
  1338. if (crtc == 0) {
  1339. vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1340. position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1341. ret |= DRM_SCANOUTPOS_VALID;
  1342. }
  1343. if (crtc == 1) {
  1344. vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1345. position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1346. ret |= DRM_SCANOUTPOS_VALID;
  1347. }
  1348. } else {
  1349. /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1350. if (crtc == 0) {
  1351. /* Assume vbl_end == 0, get vbl_start from
  1352. * upper 16 bits.
  1353. */
  1354. vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1355. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1356. /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1357. position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1358. stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1359. if (!(stat_crtc & 1))
  1360. in_vbl = false;
  1361. ret |= DRM_SCANOUTPOS_VALID;
  1362. }
  1363. if (crtc == 1) {
  1364. vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1365. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1366. position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1367. stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1368. if (!(stat_crtc & 1))
  1369. in_vbl = false;
  1370. ret |= DRM_SCANOUTPOS_VALID;
  1371. }
  1372. }
  1373. /* Decode into vertical and horizontal scanout position. */
  1374. *vpos = position & 0x1fff;
  1375. *hpos = (position >> 16) & 0x1fff;
  1376. /* Valid vblank area boundaries from gpu retrieved? */
  1377. if (vbl > 0) {
  1378. /* Yes: Decode. */
  1379. ret |= DRM_SCANOUTPOS_ACCURATE;
  1380. vbl_start = vbl & 0x1fff;
  1381. vbl_end = (vbl >> 16) & 0x1fff;
  1382. }
  1383. else {
  1384. /* No: Fake something reasonable which gives at least ok results. */
  1385. vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1386. vbl_end = 0;
  1387. }
  1388. /* Test scanout position against vblank region. */
  1389. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1390. in_vbl = false;
  1391. /* Check if inside vblank area and apply corrective offsets:
  1392. * vpos will then be >=0 in video scanout area, but negative
  1393. * within vblank area, counting down the number of lines until
  1394. * start of scanout.
  1395. */
  1396. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1397. if (in_vbl && (*vpos >= vbl_start)) {
  1398. vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1399. *vpos = *vpos - vtotal;
  1400. }
  1401. /* Correct for shifted end of vbl at vbl_end. */
  1402. *vpos = *vpos - vbl_end;
  1403. /* In vblank? */
  1404. if (in_vbl)
  1405. ret |= DRM_SCANOUTPOS_INVBL;
  1406. return ret;
  1407. }