radeon_display.c 50 KB

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