radeon_display.c 49 KB

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