radeon_display.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625
  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. drm_gem_object_unreference_unlocked(old_radeon_fb->obj);
  413. spin_unlock_irqrestore(&dev->event_lock, flags);
  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. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  628. if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  629. dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) && dig->dp_i2c_bus)
  630. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &dig->dp_i2c_bus->adapter);
  631. }
  632. if (!radeon_connector->ddc_bus)
  633. return -1;
  634. if (!radeon_connector->edid) {
  635. radeon_connector->edid = drm_get_edid(&radeon_connector->base, &radeon_connector->ddc_bus->adapter);
  636. }
  637. if (!radeon_connector->edid) {
  638. if (rdev->is_atom_bios) {
  639. /* some laptops provide a hardcoded edid in rom for LCDs */
  640. if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
  641. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
  642. radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  643. } else
  644. /* some servers provide a hardcoded edid in rom for KVMs */
  645. radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  646. }
  647. if (radeon_connector->edid) {
  648. drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  649. ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  650. return ret;
  651. }
  652. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  653. return 0;
  654. }
  655. static int radeon_ddc_dump(struct drm_connector *connector)
  656. {
  657. struct edid *edid;
  658. struct radeon_connector *radeon_connector = to_radeon_connector(connector);
  659. int ret = 0;
  660. /* on hw with routers, select right port */
  661. if (radeon_connector->router.ddc_valid)
  662. radeon_router_select_ddc_port(radeon_connector);
  663. if (!radeon_connector->ddc_bus)
  664. return -1;
  665. edid = drm_get_edid(connector, &radeon_connector->ddc_bus->adapter);
  666. /* Log EDID retrieval status here. In particular with regard to
  667. * connectors with requires_extended_probe flag set, that will prevent
  668. * function radeon_dvi_detect() to fetch EDID on this connector,
  669. * as long as there is no valid EDID header found */
  670. if (edid) {
  671. DRM_INFO("Radeon display connector %s: Found valid EDID",
  672. drm_get_connector_name(connector));
  673. kfree(edid);
  674. } else {
  675. DRM_INFO("Radeon display connector %s: No monitor connected or invalid EDID",
  676. drm_get_connector_name(connector));
  677. }
  678. return ret;
  679. }
  680. /* avivo */
  681. static void avivo_get_fb_div(struct radeon_pll *pll,
  682. u32 target_clock,
  683. u32 post_div,
  684. u32 ref_div,
  685. u32 *fb_div,
  686. u32 *frac_fb_div)
  687. {
  688. u32 tmp = post_div * ref_div;
  689. tmp *= target_clock;
  690. *fb_div = tmp / pll->reference_freq;
  691. *frac_fb_div = tmp % pll->reference_freq;
  692. if (*fb_div > pll->max_feedback_div)
  693. *fb_div = pll->max_feedback_div;
  694. else if (*fb_div < pll->min_feedback_div)
  695. *fb_div = pll->min_feedback_div;
  696. }
  697. static u32 avivo_get_post_div(struct radeon_pll *pll,
  698. u32 target_clock)
  699. {
  700. u32 vco, post_div, tmp;
  701. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  702. return pll->post_div;
  703. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  704. if (pll->flags & RADEON_PLL_IS_LCD)
  705. vco = pll->lcd_pll_out_min;
  706. else
  707. vco = pll->pll_out_min;
  708. } else {
  709. if (pll->flags & RADEON_PLL_IS_LCD)
  710. vco = pll->lcd_pll_out_max;
  711. else
  712. vco = pll->pll_out_max;
  713. }
  714. post_div = vco / target_clock;
  715. tmp = vco % target_clock;
  716. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP) {
  717. if (tmp)
  718. post_div++;
  719. } else {
  720. if (!tmp)
  721. post_div--;
  722. }
  723. if (post_div > pll->max_post_div)
  724. post_div = pll->max_post_div;
  725. else if (post_div < pll->min_post_div)
  726. post_div = pll->min_post_div;
  727. return post_div;
  728. }
  729. #define MAX_TOLERANCE 10
  730. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  731. u32 freq,
  732. u32 *dot_clock_p,
  733. u32 *fb_div_p,
  734. u32 *frac_fb_div_p,
  735. u32 *ref_div_p,
  736. u32 *post_div_p)
  737. {
  738. u32 target_clock = freq / 10;
  739. u32 post_div = avivo_get_post_div(pll, target_clock);
  740. u32 ref_div = pll->min_ref_div;
  741. u32 fb_div = 0, frac_fb_div = 0, tmp;
  742. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  743. ref_div = pll->reference_div;
  744. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  745. avivo_get_fb_div(pll, target_clock, post_div, ref_div, &fb_div, &frac_fb_div);
  746. frac_fb_div = (100 * frac_fb_div) / pll->reference_freq;
  747. if (frac_fb_div >= 5) {
  748. frac_fb_div -= 5;
  749. frac_fb_div = frac_fb_div / 10;
  750. frac_fb_div++;
  751. }
  752. if (frac_fb_div >= 10) {
  753. fb_div++;
  754. frac_fb_div = 0;
  755. }
  756. } else {
  757. while (ref_div <= pll->max_ref_div) {
  758. avivo_get_fb_div(pll, target_clock, post_div, ref_div,
  759. &fb_div, &frac_fb_div);
  760. if (frac_fb_div >= (pll->reference_freq / 2))
  761. fb_div++;
  762. frac_fb_div = 0;
  763. tmp = (pll->reference_freq * fb_div) / (post_div * ref_div);
  764. tmp = (tmp * 10000) / target_clock;
  765. if (tmp > (10000 + MAX_TOLERANCE))
  766. ref_div++;
  767. else if (tmp >= (10000 - MAX_TOLERANCE))
  768. break;
  769. else
  770. ref_div++;
  771. }
  772. }
  773. *dot_clock_p = ((pll->reference_freq * fb_div * 10) + (pll->reference_freq * frac_fb_div)) /
  774. (ref_div * post_div * 10);
  775. *fb_div_p = fb_div;
  776. *frac_fb_div_p = frac_fb_div;
  777. *ref_div_p = ref_div;
  778. *post_div_p = post_div;
  779. DRM_DEBUG_KMS("%d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  780. *dot_clock_p, fb_div, frac_fb_div, ref_div, post_div);
  781. }
  782. /* pre-avivo */
  783. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  784. {
  785. uint64_t mod;
  786. n += d / 2;
  787. mod = do_div(n, d);
  788. return n;
  789. }
  790. void radeon_compute_pll_legacy(struct radeon_pll *pll,
  791. uint64_t freq,
  792. uint32_t *dot_clock_p,
  793. uint32_t *fb_div_p,
  794. uint32_t *frac_fb_div_p,
  795. uint32_t *ref_div_p,
  796. uint32_t *post_div_p)
  797. {
  798. uint32_t min_ref_div = pll->min_ref_div;
  799. uint32_t max_ref_div = pll->max_ref_div;
  800. uint32_t min_post_div = pll->min_post_div;
  801. uint32_t max_post_div = pll->max_post_div;
  802. uint32_t min_fractional_feed_div = 0;
  803. uint32_t max_fractional_feed_div = 0;
  804. uint32_t best_vco = pll->best_vco;
  805. uint32_t best_post_div = 1;
  806. uint32_t best_ref_div = 1;
  807. uint32_t best_feedback_div = 1;
  808. uint32_t best_frac_feedback_div = 0;
  809. uint32_t best_freq = -1;
  810. uint32_t best_error = 0xffffffff;
  811. uint32_t best_vco_diff = 1;
  812. uint32_t post_div;
  813. u32 pll_out_min, pll_out_max;
  814. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  815. freq = freq * 1000;
  816. if (pll->flags & RADEON_PLL_IS_LCD) {
  817. pll_out_min = pll->lcd_pll_out_min;
  818. pll_out_max = pll->lcd_pll_out_max;
  819. } else {
  820. pll_out_min = pll->pll_out_min;
  821. pll_out_max = pll->pll_out_max;
  822. }
  823. if (pll_out_min > 64800)
  824. pll_out_min = 64800;
  825. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  826. min_ref_div = max_ref_div = pll->reference_div;
  827. else {
  828. while (min_ref_div < max_ref_div-1) {
  829. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  830. uint32_t pll_in = pll->reference_freq / mid;
  831. if (pll_in < pll->pll_in_min)
  832. max_ref_div = mid;
  833. else if (pll_in > pll->pll_in_max)
  834. min_ref_div = mid;
  835. else
  836. break;
  837. }
  838. }
  839. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  840. min_post_div = max_post_div = pll->post_div;
  841. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  842. min_fractional_feed_div = pll->min_frac_feedback_div;
  843. max_fractional_feed_div = pll->max_frac_feedback_div;
  844. }
  845. for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  846. uint32_t ref_div;
  847. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  848. continue;
  849. /* legacy radeons only have a few post_divs */
  850. if (pll->flags & RADEON_PLL_LEGACY) {
  851. if ((post_div == 5) ||
  852. (post_div == 7) ||
  853. (post_div == 9) ||
  854. (post_div == 10) ||
  855. (post_div == 11) ||
  856. (post_div == 13) ||
  857. (post_div == 14) ||
  858. (post_div == 15))
  859. continue;
  860. }
  861. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  862. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  863. uint32_t pll_in = pll->reference_freq / ref_div;
  864. uint32_t min_feed_div = pll->min_feedback_div;
  865. uint32_t max_feed_div = pll->max_feedback_div + 1;
  866. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  867. continue;
  868. while (min_feed_div < max_feed_div) {
  869. uint32_t vco;
  870. uint32_t min_frac_feed_div = min_fractional_feed_div;
  871. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  872. uint32_t frac_feedback_div;
  873. uint64_t tmp;
  874. feedback_div = (min_feed_div + max_feed_div) / 2;
  875. tmp = (uint64_t)pll->reference_freq * feedback_div;
  876. vco = radeon_div(tmp, ref_div);
  877. if (vco < pll_out_min) {
  878. min_feed_div = feedback_div + 1;
  879. continue;
  880. } else if (vco > pll_out_max) {
  881. max_feed_div = feedback_div;
  882. continue;
  883. }
  884. while (min_frac_feed_div < max_frac_feed_div) {
  885. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  886. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  887. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  888. current_freq = radeon_div(tmp, ref_div * post_div);
  889. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  890. if (freq < current_freq)
  891. error = 0xffffffff;
  892. else
  893. error = freq - current_freq;
  894. } else
  895. error = abs(current_freq - freq);
  896. vco_diff = abs(vco - best_vco);
  897. if ((best_vco == 0 && error < best_error) ||
  898. (best_vco != 0 &&
  899. ((best_error > 100 && error < best_error - 100) ||
  900. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  901. best_post_div = post_div;
  902. best_ref_div = ref_div;
  903. best_feedback_div = feedback_div;
  904. best_frac_feedback_div = frac_feedback_div;
  905. best_freq = current_freq;
  906. best_error = error;
  907. best_vco_diff = vco_diff;
  908. } else if (current_freq == freq) {
  909. if (best_freq == -1) {
  910. best_post_div = post_div;
  911. best_ref_div = ref_div;
  912. best_feedback_div = feedback_div;
  913. best_frac_feedback_div = frac_feedback_div;
  914. best_freq = current_freq;
  915. best_error = error;
  916. best_vco_diff = vco_diff;
  917. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  918. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  919. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  920. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  921. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  922. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  923. best_post_div = post_div;
  924. best_ref_div = ref_div;
  925. best_feedback_div = feedback_div;
  926. best_frac_feedback_div = frac_feedback_div;
  927. best_freq = current_freq;
  928. best_error = error;
  929. best_vco_diff = vco_diff;
  930. }
  931. }
  932. if (current_freq < freq)
  933. min_frac_feed_div = frac_feedback_div + 1;
  934. else
  935. max_frac_feed_div = frac_feedback_div;
  936. }
  937. if (current_freq < freq)
  938. min_feed_div = feedback_div + 1;
  939. else
  940. max_feed_div = feedback_div;
  941. }
  942. }
  943. }
  944. *dot_clock_p = best_freq / 10000;
  945. *fb_div_p = best_feedback_div;
  946. *frac_fb_div_p = best_frac_feedback_div;
  947. *ref_div_p = best_ref_div;
  948. *post_div_p = best_post_div;
  949. DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  950. (long long)freq,
  951. best_freq / 1000, best_feedback_div, best_frac_feedback_div,
  952. best_ref_div, best_post_div);
  953. }
  954. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  955. {
  956. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  957. if (radeon_fb->obj) {
  958. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  959. }
  960. drm_framebuffer_cleanup(fb);
  961. kfree(radeon_fb);
  962. }
  963. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  964. struct drm_file *file_priv,
  965. unsigned int *handle)
  966. {
  967. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  968. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  969. }
  970. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  971. .destroy = radeon_user_framebuffer_destroy,
  972. .create_handle = radeon_user_framebuffer_create_handle,
  973. };
  974. void
  975. radeon_framebuffer_init(struct drm_device *dev,
  976. struct radeon_framebuffer *rfb,
  977. struct drm_mode_fb_cmd *mode_cmd,
  978. struct drm_gem_object *obj)
  979. {
  980. rfb->obj = obj;
  981. drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  982. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  983. }
  984. static struct drm_framebuffer *
  985. radeon_user_framebuffer_create(struct drm_device *dev,
  986. struct drm_file *file_priv,
  987. struct drm_mode_fb_cmd *mode_cmd)
  988. {
  989. struct drm_gem_object *obj;
  990. struct radeon_framebuffer *radeon_fb;
  991. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handle);
  992. if (obj == NULL) {
  993. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  994. "can't create framebuffer\n", mode_cmd->handle);
  995. return ERR_PTR(-ENOENT);
  996. }
  997. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  998. if (radeon_fb == NULL)
  999. return ERR_PTR(-ENOMEM);
  1000. radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  1001. return &radeon_fb->base;
  1002. }
  1003. static void radeon_output_poll_changed(struct drm_device *dev)
  1004. {
  1005. struct radeon_device *rdev = dev->dev_private;
  1006. radeon_fb_output_poll_changed(rdev);
  1007. }
  1008. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  1009. .fb_create = radeon_user_framebuffer_create,
  1010. .output_poll_changed = radeon_output_poll_changed
  1011. };
  1012. struct drm_prop_enum_list {
  1013. int type;
  1014. char *name;
  1015. };
  1016. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  1017. { { 0, "driver" },
  1018. { 1, "bios" },
  1019. };
  1020. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  1021. { { TV_STD_NTSC, "ntsc" },
  1022. { TV_STD_PAL, "pal" },
  1023. { TV_STD_PAL_M, "pal-m" },
  1024. { TV_STD_PAL_60, "pal-60" },
  1025. { TV_STD_NTSC_J, "ntsc-j" },
  1026. { TV_STD_SCART_PAL, "scart-pal" },
  1027. { TV_STD_PAL_CN, "pal-cn" },
  1028. { TV_STD_SECAM, "secam" },
  1029. };
  1030. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  1031. { { UNDERSCAN_OFF, "off" },
  1032. { UNDERSCAN_ON, "on" },
  1033. { UNDERSCAN_AUTO, "auto" },
  1034. };
  1035. static int radeon_modeset_create_props(struct radeon_device *rdev)
  1036. {
  1037. int i, sz;
  1038. if (rdev->is_atom_bios) {
  1039. rdev->mode_info.coherent_mode_property =
  1040. drm_property_create(rdev->ddev,
  1041. DRM_MODE_PROP_RANGE,
  1042. "coherent", 2);
  1043. if (!rdev->mode_info.coherent_mode_property)
  1044. return -ENOMEM;
  1045. rdev->mode_info.coherent_mode_property->values[0] = 0;
  1046. rdev->mode_info.coherent_mode_property->values[1] = 1;
  1047. }
  1048. if (!ASIC_IS_AVIVO(rdev)) {
  1049. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  1050. rdev->mode_info.tmds_pll_property =
  1051. drm_property_create(rdev->ddev,
  1052. DRM_MODE_PROP_ENUM,
  1053. "tmds_pll", sz);
  1054. for (i = 0; i < sz; i++) {
  1055. drm_property_add_enum(rdev->mode_info.tmds_pll_property,
  1056. i,
  1057. radeon_tmds_pll_enum_list[i].type,
  1058. radeon_tmds_pll_enum_list[i].name);
  1059. }
  1060. }
  1061. rdev->mode_info.load_detect_property =
  1062. drm_property_create(rdev->ddev,
  1063. DRM_MODE_PROP_RANGE,
  1064. "load detection", 2);
  1065. if (!rdev->mode_info.load_detect_property)
  1066. return -ENOMEM;
  1067. rdev->mode_info.load_detect_property->values[0] = 0;
  1068. rdev->mode_info.load_detect_property->values[1] = 1;
  1069. drm_mode_create_scaling_mode_property(rdev->ddev);
  1070. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  1071. rdev->mode_info.tv_std_property =
  1072. drm_property_create(rdev->ddev,
  1073. DRM_MODE_PROP_ENUM,
  1074. "tv standard", sz);
  1075. for (i = 0; i < sz; i++) {
  1076. drm_property_add_enum(rdev->mode_info.tv_std_property,
  1077. i,
  1078. radeon_tv_std_enum_list[i].type,
  1079. radeon_tv_std_enum_list[i].name);
  1080. }
  1081. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  1082. rdev->mode_info.underscan_property =
  1083. drm_property_create(rdev->ddev,
  1084. DRM_MODE_PROP_ENUM,
  1085. "underscan", sz);
  1086. for (i = 0; i < sz; i++) {
  1087. drm_property_add_enum(rdev->mode_info.underscan_property,
  1088. i,
  1089. radeon_underscan_enum_list[i].type,
  1090. radeon_underscan_enum_list[i].name);
  1091. }
  1092. rdev->mode_info.underscan_hborder_property =
  1093. drm_property_create(rdev->ddev,
  1094. DRM_MODE_PROP_RANGE,
  1095. "underscan hborder", 2);
  1096. if (!rdev->mode_info.underscan_hborder_property)
  1097. return -ENOMEM;
  1098. rdev->mode_info.underscan_hborder_property->values[0] = 0;
  1099. rdev->mode_info.underscan_hborder_property->values[1] = 128;
  1100. rdev->mode_info.underscan_vborder_property =
  1101. drm_property_create(rdev->ddev,
  1102. DRM_MODE_PROP_RANGE,
  1103. "underscan vborder", 2);
  1104. if (!rdev->mode_info.underscan_vborder_property)
  1105. return -ENOMEM;
  1106. rdev->mode_info.underscan_vborder_property->values[0] = 0;
  1107. rdev->mode_info.underscan_vborder_property->values[1] = 128;
  1108. return 0;
  1109. }
  1110. void radeon_update_display_priority(struct radeon_device *rdev)
  1111. {
  1112. /* adjustment options for the display watermarks */
  1113. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  1114. /* set display priority to high for r3xx, rv515 chips
  1115. * this avoids flickering due to underflow to the
  1116. * display controllers during heavy acceleration.
  1117. * Don't force high on rs4xx igp chips as it seems to
  1118. * affect the sound card. See kernel bug 15982.
  1119. */
  1120. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  1121. !(rdev->flags & RADEON_IS_IGP))
  1122. rdev->disp_priority = 2;
  1123. else
  1124. rdev->disp_priority = 0;
  1125. } else
  1126. rdev->disp_priority = radeon_disp_priority;
  1127. }
  1128. int radeon_modeset_init(struct radeon_device *rdev)
  1129. {
  1130. int i;
  1131. int ret;
  1132. drm_mode_config_init(rdev->ddev);
  1133. rdev->mode_info.mode_config_initialized = true;
  1134. rdev->ddev->mode_config.funcs = (void *)&radeon_mode_funcs;
  1135. if (ASIC_IS_DCE5(rdev)) {
  1136. rdev->ddev->mode_config.max_width = 16384;
  1137. rdev->ddev->mode_config.max_height = 16384;
  1138. } else if (ASIC_IS_AVIVO(rdev)) {
  1139. rdev->ddev->mode_config.max_width = 8192;
  1140. rdev->ddev->mode_config.max_height = 8192;
  1141. } else {
  1142. rdev->ddev->mode_config.max_width = 4096;
  1143. rdev->ddev->mode_config.max_height = 4096;
  1144. }
  1145. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  1146. ret = radeon_modeset_create_props(rdev);
  1147. if (ret) {
  1148. return ret;
  1149. }
  1150. /* init i2c buses */
  1151. radeon_i2c_init(rdev);
  1152. /* check combios for a valid hardcoded EDID - Sun servers */
  1153. if (!rdev->is_atom_bios) {
  1154. /* check for hardcoded EDID in BIOS */
  1155. radeon_combios_check_hardcoded_edid(rdev);
  1156. }
  1157. /* allocate crtcs */
  1158. for (i = 0; i < rdev->num_crtc; i++) {
  1159. radeon_crtc_init(rdev->ddev, i);
  1160. }
  1161. /* okay we should have all the bios connectors */
  1162. ret = radeon_setup_enc_conn(rdev->ddev);
  1163. if (!ret) {
  1164. return ret;
  1165. }
  1166. /* init dig PHYs */
  1167. if (rdev->is_atom_bios)
  1168. radeon_atom_encoder_init(rdev);
  1169. /* initialize hpd */
  1170. radeon_hpd_init(rdev);
  1171. /* Initialize power management */
  1172. radeon_pm_init(rdev);
  1173. radeon_fbdev_init(rdev);
  1174. drm_kms_helper_poll_init(rdev->ddev);
  1175. return 0;
  1176. }
  1177. void radeon_modeset_fini(struct radeon_device *rdev)
  1178. {
  1179. radeon_fbdev_fini(rdev);
  1180. kfree(rdev->mode_info.bios_hardcoded_edid);
  1181. radeon_pm_fini(rdev);
  1182. if (rdev->mode_info.mode_config_initialized) {
  1183. drm_kms_helper_poll_fini(rdev->ddev);
  1184. radeon_hpd_fini(rdev);
  1185. drm_mode_config_cleanup(rdev->ddev);
  1186. rdev->mode_info.mode_config_initialized = false;
  1187. }
  1188. /* free i2c buses */
  1189. radeon_i2c_fini(rdev);
  1190. }
  1191. static bool is_hdtv_mode(struct drm_display_mode *mode)
  1192. {
  1193. /* try and guess if this is a tv or a monitor */
  1194. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1195. (mode->vdisplay == 576) || /* 576p */
  1196. (mode->vdisplay == 720) || /* 720p */
  1197. (mode->vdisplay == 1080)) /* 1080p */
  1198. return true;
  1199. else
  1200. return false;
  1201. }
  1202. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1203. struct drm_display_mode *mode,
  1204. struct drm_display_mode *adjusted_mode)
  1205. {
  1206. struct drm_device *dev = crtc->dev;
  1207. struct radeon_device *rdev = dev->dev_private;
  1208. struct drm_encoder *encoder;
  1209. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1210. struct radeon_encoder *radeon_encoder;
  1211. struct drm_connector *connector;
  1212. struct radeon_connector *radeon_connector;
  1213. bool first = true;
  1214. u32 src_v = 1, dst_v = 1;
  1215. u32 src_h = 1, dst_h = 1;
  1216. radeon_crtc->h_border = 0;
  1217. radeon_crtc->v_border = 0;
  1218. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1219. if (encoder->crtc != crtc)
  1220. continue;
  1221. radeon_encoder = to_radeon_encoder(encoder);
  1222. connector = radeon_get_connector_for_encoder(encoder);
  1223. radeon_connector = to_radeon_connector(connector);
  1224. if (first) {
  1225. /* set scaling */
  1226. if (radeon_encoder->rmx_type == RMX_OFF)
  1227. radeon_crtc->rmx_type = RMX_OFF;
  1228. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1229. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1230. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1231. else
  1232. radeon_crtc->rmx_type = RMX_OFF;
  1233. /* copy native mode */
  1234. memcpy(&radeon_crtc->native_mode,
  1235. &radeon_encoder->native_mode,
  1236. sizeof(struct drm_display_mode));
  1237. src_v = crtc->mode.vdisplay;
  1238. dst_v = radeon_crtc->native_mode.vdisplay;
  1239. src_h = crtc->mode.hdisplay;
  1240. dst_h = radeon_crtc->native_mode.hdisplay;
  1241. /* fix up for overscan on hdmi */
  1242. if (ASIC_IS_AVIVO(rdev) &&
  1243. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1244. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1245. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1246. drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1247. is_hdtv_mode(mode)))) {
  1248. if (radeon_encoder->underscan_hborder != 0)
  1249. radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1250. else
  1251. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1252. if (radeon_encoder->underscan_vborder != 0)
  1253. radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1254. else
  1255. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1256. radeon_crtc->rmx_type = RMX_FULL;
  1257. src_v = crtc->mode.vdisplay;
  1258. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1259. src_h = crtc->mode.hdisplay;
  1260. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1261. }
  1262. first = false;
  1263. } else {
  1264. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1265. /* WARNING: Right now this can't happen but
  1266. * in the future we need to check that scaling
  1267. * are consistent across different encoder
  1268. * (ie all encoder can work with the same
  1269. * scaling).
  1270. */
  1271. DRM_ERROR("Scaling not consistent across encoder.\n");
  1272. return false;
  1273. }
  1274. }
  1275. }
  1276. if (radeon_crtc->rmx_type != RMX_OFF) {
  1277. fixed20_12 a, b;
  1278. a.full = dfixed_const(src_v);
  1279. b.full = dfixed_const(dst_v);
  1280. radeon_crtc->vsc.full = dfixed_div(a, b);
  1281. a.full = dfixed_const(src_h);
  1282. b.full = dfixed_const(dst_h);
  1283. radeon_crtc->hsc.full = dfixed_div(a, b);
  1284. } else {
  1285. radeon_crtc->vsc.full = dfixed_const(1);
  1286. radeon_crtc->hsc.full = dfixed_const(1);
  1287. }
  1288. return true;
  1289. }
  1290. /*
  1291. * Retrieve current video scanout position of crtc on a given gpu.
  1292. *
  1293. * \param dev Device to query.
  1294. * \param crtc Crtc to query.
  1295. * \param *vpos Location where vertical scanout position should be stored.
  1296. * \param *hpos Location where horizontal scanout position should go.
  1297. *
  1298. * Returns vpos as a positive number while in active scanout area.
  1299. * Returns vpos as a negative number inside vblank, counting the number
  1300. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1301. * until start of active scanout / end of vblank."
  1302. *
  1303. * \return Flags, or'ed together as follows:
  1304. *
  1305. * DRM_SCANOUTPOS_VALID = Query successful.
  1306. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1307. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1308. * this flag means that returned position may be offset by a constant but
  1309. * unknown small number of scanlines wrt. real scanout position.
  1310. *
  1311. */
  1312. int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, int *vpos, int *hpos)
  1313. {
  1314. u32 stat_crtc = 0, vbl = 0, position = 0;
  1315. int vbl_start, vbl_end, vtotal, ret = 0;
  1316. bool in_vbl = true;
  1317. struct radeon_device *rdev = dev->dev_private;
  1318. if (ASIC_IS_DCE4(rdev)) {
  1319. if (crtc == 0) {
  1320. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1321. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1322. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1323. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1324. ret |= DRM_SCANOUTPOS_VALID;
  1325. }
  1326. if (crtc == 1) {
  1327. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1328. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1329. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1330. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1331. ret |= DRM_SCANOUTPOS_VALID;
  1332. }
  1333. if (crtc == 2) {
  1334. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1335. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1336. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1337. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1338. ret |= DRM_SCANOUTPOS_VALID;
  1339. }
  1340. if (crtc == 3) {
  1341. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1342. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1343. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1344. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1345. ret |= DRM_SCANOUTPOS_VALID;
  1346. }
  1347. if (crtc == 4) {
  1348. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1349. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1350. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1351. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1352. ret |= DRM_SCANOUTPOS_VALID;
  1353. }
  1354. if (crtc == 5) {
  1355. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1356. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1357. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1358. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1359. ret |= DRM_SCANOUTPOS_VALID;
  1360. }
  1361. } else if (ASIC_IS_AVIVO(rdev)) {
  1362. if (crtc == 0) {
  1363. vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1364. position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1365. ret |= DRM_SCANOUTPOS_VALID;
  1366. }
  1367. if (crtc == 1) {
  1368. vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1369. position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1370. ret |= DRM_SCANOUTPOS_VALID;
  1371. }
  1372. } else {
  1373. /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1374. if (crtc == 0) {
  1375. /* Assume vbl_end == 0, get vbl_start from
  1376. * upper 16 bits.
  1377. */
  1378. vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1379. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1380. /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1381. position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1382. stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1383. if (!(stat_crtc & 1))
  1384. in_vbl = false;
  1385. ret |= DRM_SCANOUTPOS_VALID;
  1386. }
  1387. if (crtc == 1) {
  1388. vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1389. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1390. position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1391. stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1392. if (!(stat_crtc & 1))
  1393. in_vbl = false;
  1394. ret |= DRM_SCANOUTPOS_VALID;
  1395. }
  1396. }
  1397. /* Decode into vertical and horizontal scanout position. */
  1398. *vpos = position & 0x1fff;
  1399. *hpos = (position >> 16) & 0x1fff;
  1400. /* Valid vblank area boundaries from gpu retrieved? */
  1401. if (vbl > 0) {
  1402. /* Yes: Decode. */
  1403. ret |= DRM_SCANOUTPOS_ACCURATE;
  1404. vbl_start = vbl & 0x1fff;
  1405. vbl_end = (vbl >> 16) & 0x1fff;
  1406. }
  1407. else {
  1408. /* No: Fake something reasonable which gives at least ok results. */
  1409. vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1410. vbl_end = 0;
  1411. }
  1412. /* Test scanout position against vblank region. */
  1413. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1414. in_vbl = false;
  1415. /* Check if inside vblank area and apply corrective offsets:
  1416. * vpos will then be >=0 in video scanout area, but negative
  1417. * within vblank area, counting down the number of lines until
  1418. * start of scanout.
  1419. */
  1420. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1421. if (in_vbl && (*vpos >= vbl_start)) {
  1422. vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1423. *vpos = *vpos - vtotal;
  1424. }
  1425. /* Correct for shifted end of vbl at vbl_end. */
  1426. *vpos = *vpos - vbl_end;
  1427. /* In vblank? */
  1428. if (in_vbl)
  1429. ret |= DRM_SCANOUTPOS_INVBL;
  1430. return ret;
  1431. }