radeon_display.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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 <drm/drmP.h>
  27. #include <drm/radeon_drm.h>
  28. #include "radeon.h"
  29. #include "atom.h"
  30. #include <asm/div64.h>
  31. #include <linux/pm_runtime.h>
  32. #include <drm/drm_crtc_helper.h>
  33. #include <drm/drm_edid.h>
  34. #include <linux/gcd.h>
  35. static void avivo_crtc_load_lut(struct drm_crtc *crtc)
  36. {
  37. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  38. struct drm_device *dev = crtc->dev;
  39. struct radeon_device *rdev = dev->dev_private;
  40. int i;
  41. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  42. WREG32(AVIVO_DC_LUTA_CONTROL + radeon_crtc->crtc_offset, 0);
  43. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  44. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  45. WREG32(AVIVO_DC_LUTA_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  46. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  47. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  48. WREG32(AVIVO_DC_LUTA_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  49. WREG32(AVIVO_DC_LUT_RW_SELECT, radeon_crtc->crtc_id);
  50. WREG32(AVIVO_DC_LUT_RW_MODE, 0);
  51. WREG32(AVIVO_DC_LUT_WRITE_EN_MASK, 0x0000003f);
  52. WREG8(AVIVO_DC_LUT_RW_INDEX, 0);
  53. for (i = 0; i < 256; i++) {
  54. WREG32(AVIVO_DC_LUT_30_COLOR,
  55. (radeon_crtc->lut_r[i] << 20) |
  56. (radeon_crtc->lut_g[i] << 10) |
  57. (radeon_crtc->lut_b[i] << 0));
  58. }
  59. /* Only change bit 0 of LUT_SEL, other bits are set elsewhere */
  60. WREG32_P(AVIVO_D1GRPH_LUT_SEL + radeon_crtc->crtc_offset, radeon_crtc->crtc_id, ~1);
  61. }
  62. static void dce4_crtc_load_lut(struct drm_crtc *crtc)
  63. {
  64. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  65. struct drm_device *dev = crtc->dev;
  66. struct radeon_device *rdev = dev->dev_private;
  67. int i;
  68. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  69. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  70. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  71. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  72. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  73. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  74. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  75. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  76. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  77. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  78. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  79. for (i = 0; i < 256; i++) {
  80. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  81. (radeon_crtc->lut_r[i] << 20) |
  82. (radeon_crtc->lut_g[i] << 10) |
  83. (radeon_crtc->lut_b[i] << 0));
  84. }
  85. }
  86. static void dce5_crtc_load_lut(struct drm_crtc *crtc)
  87. {
  88. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  89. struct drm_device *dev = crtc->dev;
  90. struct radeon_device *rdev = dev->dev_private;
  91. int i;
  92. DRM_DEBUG_KMS("%d\n", radeon_crtc->crtc_id);
  93. WREG32(NI_INPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  94. (NI_INPUT_CSC_GRPH_MODE(NI_INPUT_CSC_BYPASS) |
  95. NI_INPUT_CSC_OVL_MODE(NI_INPUT_CSC_BYPASS)));
  96. WREG32(NI_PRESCALE_GRPH_CONTROL + radeon_crtc->crtc_offset,
  97. NI_GRPH_PRESCALE_BYPASS);
  98. WREG32(NI_PRESCALE_OVL_CONTROL + radeon_crtc->crtc_offset,
  99. NI_OVL_PRESCALE_BYPASS);
  100. WREG32(NI_INPUT_GAMMA_CONTROL + radeon_crtc->crtc_offset,
  101. (NI_GRPH_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT) |
  102. NI_OVL_INPUT_GAMMA_MODE(NI_INPUT_GAMMA_USE_LUT)));
  103. WREG32(EVERGREEN_DC_LUT_CONTROL + radeon_crtc->crtc_offset, 0);
  104. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_BLUE + radeon_crtc->crtc_offset, 0);
  105. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_GREEN + radeon_crtc->crtc_offset, 0);
  106. WREG32(EVERGREEN_DC_LUT_BLACK_OFFSET_RED + radeon_crtc->crtc_offset, 0);
  107. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_BLUE + radeon_crtc->crtc_offset, 0xffff);
  108. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_GREEN + radeon_crtc->crtc_offset, 0xffff);
  109. WREG32(EVERGREEN_DC_LUT_WHITE_OFFSET_RED + radeon_crtc->crtc_offset, 0xffff);
  110. WREG32(EVERGREEN_DC_LUT_RW_MODE + radeon_crtc->crtc_offset, 0);
  111. WREG32(EVERGREEN_DC_LUT_WRITE_EN_MASK + radeon_crtc->crtc_offset, 0x00000007);
  112. WREG32(EVERGREEN_DC_LUT_RW_INDEX + radeon_crtc->crtc_offset, 0);
  113. for (i = 0; i < 256; i++) {
  114. WREG32(EVERGREEN_DC_LUT_30_COLOR + radeon_crtc->crtc_offset,
  115. (radeon_crtc->lut_r[i] << 20) |
  116. (radeon_crtc->lut_g[i] << 10) |
  117. (radeon_crtc->lut_b[i] << 0));
  118. }
  119. WREG32(NI_DEGAMMA_CONTROL + radeon_crtc->crtc_offset,
  120. (NI_GRPH_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  121. NI_OVL_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  122. NI_ICON_DEGAMMA_MODE(NI_DEGAMMA_BYPASS) |
  123. NI_CURSOR_DEGAMMA_MODE(NI_DEGAMMA_BYPASS)));
  124. WREG32(NI_GAMUT_REMAP_CONTROL + radeon_crtc->crtc_offset,
  125. (NI_GRPH_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS) |
  126. NI_OVL_GAMUT_REMAP_MODE(NI_GAMUT_REMAP_BYPASS)));
  127. WREG32(NI_REGAMMA_CONTROL + radeon_crtc->crtc_offset,
  128. (NI_GRPH_REGAMMA_MODE(NI_REGAMMA_BYPASS) |
  129. NI_OVL_REGAMMA_MODE(NI_REGAMMA_BYPASS)));
  130. WREG32(NI_OUTPUT_CSC_CONTROL + radeon_crtc->crtc_offset,
  131. (NI_OUTPUT_CSC_GRPH_MODE(NI_OUTPUT_CSC_BYPASS) |
  132. NI_OUTPUT_CSC_OVL_MODE(NI_OUTPUT_CSC_BYPASS)));
  133. /* XXX match this to the depth of the crtc fmt block, move to modeset? */
  134. WREG32(0x6940 + radeon_crtc->crtc_offset, 0);
  135. if (ASIC_IS_DCE8(rdev)) {
  136. /* XXX this only needs to be programmed once per crtc at startup,
  137. * not sure where the best place for it is
  138. */
  139. WREG32(CIK_ALPHA_CONTROL + radeon_crtc->crtc_offset,
  140. CIK_CURSOR_ALPHA_BLND_ENA);
  141. }
  142. }
  143. static void legacy_crtc_load_lut(struct drm_crtc *crtc)
  144. {
  145. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  146. struct drm_device *dev = crtc->dev;
  147. struct radeon_device *rdev = dev->dev_private;
  148. int i;
  149. uint32_t dac2_cntl;
  150. dac2_cntl = RREG32(RADEON_DAC_CNTL2);
  151. if (radeon_crtc->crtc_id == 0)
  152. dac2_cntl &= (uint32_t)~RADEON_DAC2_PALETTE_ACC_CTL;
  153. else
  154. dac2_cntl |= RADEON_DAC2_PALETTE_ACC_CTL;
  155. WREG32(RADEON_DAC_CNTL2, dac2_cntl);
  156. WREG8(RADEON_PALETTE_INDEX, 0);
  157. for (i = 0; i < 256; i++) {
  158. WREG32(RADEON_PALETTE_30_DATA,
  159. (radeon_crtc->lut_r[i] << 20) |
  160. (radeon_crtc->lut_g[i] << 10) |
  161. (radeon_crtc->lut_b[i] << 0));
  162. }
  163. }
  164. void radeon_crtc_load_lut(struct drm_crtc *crtc)
  165. {
  166. struct drm_device *dev = crtc->dev;
  167. struct radeon_device *rdev = dev->dev_private;
  168. if (!crtc->enabled)
  169. return;
  170. if (ASIC_IS_DCE5(rdev))
  171. dce5_crtc_load_lut(crtc);
  172. else if (ASIC_IS_DCE4(rdev))
  173. dce4_crtc_load_lut(crtc);
  174. else if (ASIC_IS_AVIVO(rdev))
  175. avivo_crtc_load_lut(crtc);
  176. else
  177. legacy_crtc_load_lut(crtc);
  178. }
  179. /** Sets the color ramps on behalf of fbcon */
  180. void radeon_crtc_fb_gamma_set(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. radeon_crtc->lut_r[regno] = red >> 6;
  185. radeon_crtc->lut_g[regno] = green >> 6;
  186. radeon_crtc->lut_b[regno] = blue >> 6;
  187. }
  188. /** Gets the color ramps on behalf of fbcon */
  189. void radeon_crtc_fb_gamma_get(struct drm_crtc *crtc, u16 *red, u16 *green,
  190. u16 *blue, int regno)
  191. {
  192. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  193. *red = radeon_crtc->lut_r[regno] << 6;
  194. *green = radeon_crtc->lut_g[regno] << 6;
  195. *blue = radeon_crtc->lut_b[regno] << 6;
  196. }
  197. static void radeon_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
  198. u16 *blue, uint32_t start, uint32_t size)
  199. {
  200. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  201. int end = (start + size > 256) ? 256 : start + size, i;
  202. /* userspace palettes are always correct as is */
  203. for (i = start; i < end; i++) {
  204. radeon_crtc->lut_r[i] = red[i] >> 6;
  205. radeon_crtc->lut_g[i] = green[i] >> 6;
  206. radeon_crtc->lut_b[i] = blue[i] >> 6;
  207. }
  208. radeon_crtc_load_lut(crtc);
  209. }
  210. static void radeon_crtc_destroy(struct drm_crtc *crtc)
  211. {
  212. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  213. drm_crtc_cleanup(crtc);
  214. destroy_workqueue(radeon_crtc->flip_queue);
  215. kfree(radeon_crtc);
  216. }
  217. /**
  218. * radeon_unpin_work_func - unpin old buffer object
  219. *
  220. * @__work - kernel work item
  221. *
  222. * Unpin the old frame buffer object outside of the interrupt handler
  223. */
  224. static void radeon_unpin_work_func(struct work_struct *__work)
  225. {
  226. struct radeon_flip_work *work =
  227. container_of(__work, struct radeon_flip_work, unpin_work);
  228. int r;
  229. /* unpin of the old buffer */
  230. r = radeon_bo_reserve(work->old_rbo, false);
  231. if (likely(r == 0)) {
  232. r = radeon_bo_unpin(work->old_rbo);
  233. if (unlikely(r != 0)) {
  234. DRM_ERROR("failed to unpin buffer after flip\n");
  235. }
  236. radeon_bo_unreserve(work->old_rbo);
  237. } else
  238. DRM_ERROR("failed to reserve buffer after flip\n");
  239. drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
  240. kfree(work);
  241. }
  242. void radeon_crtc_handle_vblank(struct radeon_device *rdev, int crtc_id)
  243. {
  244. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  245. unsigned long flags;
  246. u32 update_pending;
  247. int vpos, hpos;
  248. /* can happen during initialization */
  249. if (radeon_crtc == NULL)
  250. return;
  251. spin_lock_irqsave(&rdev->ddev->event_lock, flags);
  252. if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
  253. DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
  254. "RADEON_FLIP_SUBMITTED(%d)\n",
  255. radeon_crtc->flip_status,
  256. RADEON_FLIP_SUBMITTED);
  257. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  258. return;
  259. }
  260. update_pending = radeon_page_flip_pending(rdev, crtc_id);
  261. /* Has the pageflip already completed in crtc, or is it certain
  262. * to complete in this vblank?
  263. */
  264. if (update_pending &&
  265. (DRM_SCANOUTPOS_VALID & radeon_get_crtc_scanoutpos(rdev->ddev, crtc_id, 0,
  266. &vpos, &hpos, NULL, NULL)) &&
  267. ((vpos >= (99 * rdev->mode_info.crtcs[crtc_id]->base.hwmode.crtc_vdisplay)/100) ||
  268. (vpos < 0 && !ASIC_IS_AVIVO(rdev)))) {
  269. /* crtc didn't flip in this target vblank interval,
  270. * but flip is pending in crtc. Based on the current
  271. * scanout position we know that the current frame is
  272. * (nearly) complete and the flip will (likely)
  273. * complete before the start of the next frame.
  274. */
  275. update_pending = 0;
  276. }
  277. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  278. if (!update_pending)
  279. radeon_crtc_handle_flip(rdev, crtc_id);
  280. }
  281. /**
  282. * radeon_crtc_handle_flip - page flip completed
  283. *
  284. * @rdev: radeon device pointer
  285. * @crtc_id: crtc number this event is for
  286. *
  287. * Called when we are sure that a page flip for this crtc is completed.
  288. */
  289. void radeon_crtc_handle_flip(struct radeon_device *rdev, int crtc_id)
  290. {
  291. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[crtc_id];
  292. struct radeon_flip_work *work;
  293. unsigned long flags;
  294. /* this can happen at init */
  295. if (radeon_crtc == NULL)
  296. return;
  297. spin_lock_irqsave(&rdev->ddev->event_lock, flags);
  298. work = radeon_crtc->flip_work;
  299. if (radeon_crtc->flip_status != RADEON_FLIP_SUBMITTED) {
  300. DRM_DEBUG_DRIVER("radeon_crtc->flip_status = %d != "
  301. "RADEON_FLIP_SUBMITTED(%d)\n",
  302. radeon_crtc->flip_status,
  303. RADEON_FLIP_SUBMITTED);
  304. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  305. return;
  306. }
  307. /* Pageflip completed. Clean up. */
  308. radeon_crtc->flip_status = RADEON_FLIP_NONE;
  309. radeon_crtc->flip_work = NULL;
  310. /* wakeup userspace */
  311. if (work->event)
  312. drm_send_vblank_event(rdev->ddev, crtc_id, work->event);
  313. spin_unlock_irqrestore(&rdev->ddev->event_lock, flags);
  314. drm_vblank_put(rdev->ddev, radeon_crtc->crtc_id);
  315. radeon_irq_kms_pflip_irq_put(rdev, work->crtc_id);
  316. queue_work(radeon_crtc->flip_queue, &work->unpin_work);
  317. }
  318. /**
  319. * radeon_flip_work_func - page flip framebuffer
  320. *
  321. * @work - kernel work item
  322. *
  323. * Wait for the buffer object to become idle and do the actual page flip
  324. */
  325. static void radeon_flip_work_func(struct work_struct *__work)
  326. {
  327. struct radeon_flip_work *work =
  328. container_of(__work, struct radeon_flip_work, flip_work);
  329. struct radeon_device *rdev = work->rdev;
  330. struct radeon_crtc *radeon_crtc = rdev->mode_info.crtcs[work->crtc_id];
  331. struct drm_crtc *crtc = &radeon_crtc->base;
  332. unsigned long flags;
  333. int r;
  334. down_read(&rdev->exclusive_lock);
  335. if (work->fence) {
  336. r = radeon_fence_wait(work->fence, false);
  337. if (r == -EDEADLK) {
  338. up_read(&rdev->exclusive_lock);
  339. r = radeon_gpu_reset(rdev);
  340. down_read(&rdev->exclusive_lock);
  341. }
  342. if (r)
  343. DRM_ERROR("failed to wait on page flip fence (%d)!\n", r);
  344. /* We continue with the page flip even if we failed to wait on
  345. * the fence, otherwise the DRM core and userspace will be
  346. * confused about which BO the CRTC is scanning out
  347. */
  348. radeon_fence_unref(&work->fence);
  349. }
  350. /* We borrow the event spin lock for protecting flip_status */
  351. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  352. /* set the proper interrupt */
  353. radeon_irq_kms_pflip_irq_get(rdev, radeon_crtc->crtc_id);
  354. /* do the flip (mmio) */
  355. radeon_page_flip(rdev, radeon_crtc->crtc_id, work->base);
  356. radeon_crtc->flip_status = RADEON_FLIP_SUBMITTED;
  357. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  358. up_read(&rdev->exclusive_lock);
  359. }
  360. static int radeon_crtc_page_flip(struct drm_crtc *crtc,
  361. struct drm_framebuffer *fb,
  362. struct drm_pending_vblank_event *event,
  363. uint32_t page_flip_flags)
  364. {
  365. struct drm_device *dev = crtc->dev;
  366. struct radeon_device *rdev = dev->dev_private;
  367. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  368. struct radeon_framebuffer *old_radeon_fb;
  369. struct radeon_framebuffer *new_radeon_fb;
  370. struct drm_gem_object *obj;
  371. struct radeon_flip_work *work;
  372. struct radeon_bo *new_rbo;
  373. uint32_t tiling_flags, pitch_pixels;
  374. uint64_t base;
  375. unsigned long flags;
  376. int r;
  377. work = kzalloc(sizeof *work, GFP_KERNEL);
  378. if (work == NULL)
  379. return -ENOMEM;
  380. INIT_WORK(&work->flip_work, radeon_flip_work_func);
  381. INIT_WORK(&work->unpin_work, radeon_unpin_work_func);
  382. work->rdev = rdev;
  383. work->crtc_id = radeon_crtc->crtc_id;
  384. work->event = event;
  385. /* schedule unpin of the old buffer */
  386. old_radeon_fb = to_radeon_framebuffer(crtc->primary->fb);
  387. obj = old_radeon_fb->obj;
  388. /* take a reference to the old object */
  389. drm_gem_object_reference(obj);
  390. work->old_rbo = gem_to_radeon_bo(obj);
  391. new_radeon_fb = to_radeon_framebuffer(fb);
  392. obj = new_radeon_fb->obj;
  393. new_rbo = gem_to_radeon_bo(obj);
  394. spin_lock(&new_rbo->tbo.bdev->fence_lock);
  395. if (new_rbo->tbo.sync_obj)
  396. work->fence = radeon_fence_ref(new_rbo->tbo.sync_obj);
  397. spin_unlock(&new_rbo->tbo.bdev->fence_lock);
  398. /* pin the new buffer */
  399. DRM_DEBUG_DRIVER("flip-ioctl() cur_rbo = %p, new_rbo = %p\n",
  400. work->old_rbo, new_rbo);
  401. r = radeon_bo_reserve(new_rbo, false);
  402. if (unlikely(r != 0)) {
  403. DRM_ERROR("failed to reserve new rbo buffer before flip\n");
  404. goto cleanup;
  405. }
  406. /* Only 27 bit offset for legacy CRTC */
  407. r = radeon_bo_pin_restricted(new_rbo, RADEON_GEM_DOMAIN_VRAM,
  408. ASIC_IS_AVIVO(rdev) ? 0 : 1 << 27, &base);
  409. if (unlikely(r != 0)) {
  410. radeon_bo_unreserve(new_rbo);
  411. r = -EINVAL;
  412. DRM_ERROR("failed to pin new rbo buffer before flip\n");
  413. goto cleanup;
  414. }
  415. radeon_bo_get_tiling_flags(new_rbo, &tiling_flags, NULL);
  416. radeon_bo_unreserve(new_rbo);
  417. if (!ASIC_IS_AVIVO(rdev)) {
  418. /* crtc offset is from display base addr not FB location */
  419. base -= radeon_crtc->legacy_display_base_addr;
  420. pitch_pixels = fb->pitches[0] / (fb->bits_per_pixel / 8);
  421. if (tiling_flags & RADEON_TILING_MACRO) {
  422. if (ASIC_IS_R300(rdev)) {
  423. base &= ~0x7ff;
  424. } else {
  425. int byteshift = fb->bits_per_pixel >> 4;
  426. int tile_addr = (((crtc->y >> 3) * pitch_pixels + crtc->x) >> (8 - byteshift)) << 11;
  427. base += tile_addr + ((crtc->x << byteshift) % 256) + ((crtc->y % 8) << 8);
  428. }
  429. } else {
  430. int offset = crtc->y * pitch_pixels + crtc->x;
  431. switch (fb->bits_per_pixel) {
  432. case 8:
  433. default:
  434. offset *= 1;
  435. break;
  436. case 15:
  437. case 16:
  438. offset *= 2;
  439. break;
  440. case 24:
  441. offset *= 3;
  442. break;
  443. case 32:
  444. offset *= 4;
  445. break;
  446. }
  447. base += offset;
  448. }
  449. base &= ~7;
  450. }
  451. work->base = base;
  452. r = drm_vblank_get(crtc->dev, radeon_crtc->crtc_id);
  453. if (r) {
  454. DRM_ERROR("failed to get vblank before flip\n");
  455. goto pflip_cleanup;
  456. }
  457. /* We borrow the event spin lock for protecting flip_work */
  458. spin_lock_irqsave(&crtc->dev->event_lock, flags);
  459. if (radeon_crtc->flip_status != RADEON_FLIP_NONE) {
  460. DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
  461. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  462. r = -EBUSY;
  463. goto vblank_cleanup;
  464. }
  465. radeon_crtc->flip_status = RADEON_FLIP_PENDING;
  466. radeon_crtc->flip_work = work;
  467. /* update crtc fb */
  468. crtc->primary->fb = fb;
  469. spin_unlock_irqrestore(&crtc->dev->event_lock, flags);
  470. queue_work(radeon_crtc->flip_queue, &work->flip_work);
  471. return 0;
  472. vblank_cleanup:
  473. drm_vblank_put(crtc->dev, radeon_crtc->crtc_id);
  474. pflip_cleanup:
  475. if (unlikely(radeon_bo_reserve(new_rbo, false) != 0)) {
  476. DRM_ERROR("failed to reserve new rbo in error path\n");
  477. goto cleanup;
  478. }
  479. if (unlikely(radeon_bo_unpin(new_rbo) != 0)) {
  480. DRM_ERROR("failed to unpin new rbo in error path\n");
  481. }
  482. radeon_bo_unreserve(new_rbo);
  483. cleanup:
  484. drm_gem_object_unreference_unlocked(&work->old_rbo->gem_base);
  485. radeon_fence_unref(&work->fence);
  486. kfree(work);
  487. return r;
  488. }
  489. static int
  490. radeon_crtc_set_config(struct drm_mode_set *set)
  491. {
  492. struct drm_device *dev;
  493. struct radeon_device *rdev;
  494. struct drm_crtc *crtc;
  495. bool active = false;
  496. int ret;
  497. if (!set || !set->crtc)
  498. return -EINVAL;
  499. dev = set->crtc->dev;
  500. ret = pm_runtime_get_sync(dev->dev);
  501. if (ret < 0)
  502. return ret;
  503. ret = drm_crtc_helper_set_config(set);
  504. list_for_each_entry(crtc, &dev->mode_config.crtc_list, head)
  505. if (crtc->enabled)
  506. active = true;
  507. pm_runtime_mark_last_busy(dev->dev);
  508. rdev = dev->dev_private;
  509. /* if we have active crtcs and we don't have a power ref,
  510. take the current one */
  511. if (active && !rdev->have_disp_power_ref) {
  512. rdev->have_disp_power_ref = true;
  513. return ret;
  514. }
  515. /* if we have no active crtcs, then drop the power ref
  516. we got before */
  517. if (!active && rdev->have_disp_power_ref) {
  518. pm_runtime_put_autosuspend(dev->dev);
  519. rdev->have_disp_power_ref = false;
  520. }
  521. /* drop the power reference we got coming in here */
  522. pm_runtime_put_autosuspend(dev->dev);
  523. return ret;
  524. }
  525. static const struct drm_crtc_funcs radeon_crtc_funcs = {
  526. .cursor_set = radeon_crtc_cursor_set,
  527. .cursor_move = radeon_crtc_cursor_move,
  528. .gamma_set = radeon_crtc_gamma_set,
  529. .set_config = radeon_crtc_set_config,
  530. .destroy = radeon_crtc_destroy,
  531. .page_flip = radeon_crtc_page_flip,
  532. };
  533. static void radeon_crtc_init(struct drm_device *dev, int index)
  534. {
  535. struct radeon_device *rdev = dev->dev_private;
  536. struct radeon_crtc *radeon_crtc;
  537. int i;
  538. radeon_crtc = kzalloc(sizeof(struct radeon_crtc) + (RADEONFB_CONN_LIMIT * sizeof(struct drm_connector *)), GFP_KERNEL);
  539. if (radeon_crtc == NULL)
  540. return;
  541. drm_crtc_init(dev, &radeon_crtc->base, &radeon_crtc_funcs);
  542. drm_mode_crtc_set_gamma_size(&radeon_crtc->base, 256);
  543. radeon_crtc->crtc_id = index;
  544. radeon_crtc->flip_queue = create_singlethread_workqueue("radeon-crtc");
  545. rdev->mode_info.crtcs[index] = radeon_crtc;
  546. if (rdev->family >= CHIP_BONAIRE) {
  547. radeon_crtc->max_cursor_width = CIK_CURSOR_WIDTH;
  548. radeon_crtc->max_cursor_height = CIK_CURSOR_HEIGHT;
  549. } else {
  550. radeon_crtc->max_cursor_width = CURSOR_WIDTH;
  551. radeon_crtc->max_cursor_height = CURSOR_HEIGHT;
  552. }
  553. dev->mode_config.cursor_width = radeon_crtc->max_cursor_width;
  554. dev->mode_config.cursor_height = radeon_crtc->max_cursor_height;
  555. #if 0
  556. radeon_crtc->mode_set.crtc = &radeon_crtc->base;
  557. radeon_crtc->mode_set.connectors = (struct drm_connector **)(radeon_crtc + 1);
  558. radeon_crtc->mode_set.num_connectors = 0;
  559. #endif
  560. for (i = 0; i < 256; i++) {
  561. radeon_crtc->lut_r[i] = i << 2;
  562. radeon_crtc->lut_g[i] = i << 2;
  563. radeon_crtc->lut_b[i] = i << 2;
  564. }
  565. if (rdev->is_atom_bios && (ASIC_IS_AVIVO(rdev) || radeon_r4xx_atom))
  566. radeon_atombios_init_crtc(dev, radeon_crtc);
  567. else
  568. radeon_legacy_init_crtc(dev, radeon_crtc);
  569. }
  570. static const char *encoder_names[38] = {
  571. "NONE",
  572. "INTERNAL_LVDS",
  573. "INTERNAL_TMDS1",
  574. "INTERNAL_TMDS2",
  575. "INTERNAL_DAC1",
  576. "INTERNAL_DAC2",
  577. "INTERNAL_SDVOA",
  578. "INTERNAL_SDVOB",
  579. "SI170B",
  580. "CH7303",
  581. "CH7301",
  582. "INTERNAL_DVO1",
  583. "EXTERNAL_SDVOA",
  584. "EXTERNAL_SDVOB",
  585. "TITFP513",
  586. "INTERNAL_LVTM1",
  587. "VT1623",
  588. "HDMI_SI1930",
  589. "HDMI_INTERNAL",
  590. "INTERNAL_KLDSCP_TMDS1",
  591. "INTERNAL_KLDSCP_DVO1",
  592. "INTERNAL_KLDSCP_DAC1",
  593. "INTERNAL_KLDSCP_DAC2",
  594. "SI178",
  595. "MVPU_FPGA",
  596. "INTERNAL_DDI",
  597. "VT1625",
  598. "HDMI_SI1932",
  599. "DP_AN9801",
  600. "DP_DP501",
  601. "INTERNAL_UNIPHY",
  602. "INTERNAL_KLDSCP_LVTMA",
  603. "INTERNAL_UNIPHY1",
  604. "INTERNAL_UNIPHY2",
  605. "NUTMEG",
  606. "TRAVIS",
  607. "INTERNAL_VCE",
  608. "INTERNAL_UNIPHY3",
  609. };
  610. static const char *hpd_names[6] = {
  611. "HPD1",
  612. "HPD2",
  613. "HPD3",
  614. "HPD4",
  615. "HPD5",
  616. "HPD6",
  617. };
  618. static void radeon_print_display_setup(struct drm_device *dev)
  619. {
  620. struct drm_connector *connector;
  621. struct radeon_connector *radeon_connector;
  622. struct drm_encoder *encoder;
  623. struct radeon_encoder *radeon_encoder;
  624. uint32_t devices;
  625. int i = 0;
  626. DRM_INFO("Radeon Display Connectors\n");
  627. list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
  628. radeon_connector = to_radeon_connector(connector);
  629. DRM_INFO("Connector %d:\n", i);
  630. DRM_INFO(" %s\n", connector->name);
  631. if (radeon_connector->hpd.hpd != RADEON_HPD_NONE)
  632. DRM_INFO(" %s\n", hpd_names[radeon_connector->hpd.hpd]);
  633. if (radeon_connector->ddc_bus) {
  634. DRM_INFO(" DDC: 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\n",
  635. radeon_connector->ddc_bus->rec.mask_clk_reg,
  636. radeon_connector->ddc_bus->rec.mask_data_reg,
  637. radeon_connector->ddc_bus->rec.a_clk_reg,
  638. radeon_connector->ddc_bus->rec.a_data_reg,
  639. radeon_connector->ddc_bus->rec.en_clk_reg,
  640. radeon_connector->ddc_bus->rec.en_data_reg,
  641. radeon_connector->ddc_bus->rec.y_clk_reg,
  642. radeon_connector->ddc_bus->rec.y_data_reg);
  643. if (radeon_connector->router.ddc_valid)
  644. DRM_INFO(" DDC Router 0x%x/0x%x\n",
  645. radeon_connector->router.ddc_mux_control_pin,
  646. radeon_connector->router.ddc_mux_state);
  647. if (radeon_connector->router.cd_valid)
  648. DRM_INFO(" Clock/Data Router 0x%x/0x%x\n",
  649. radeon_connector->router.cd_mux_control_pin,
  650. radeon_connector->router.cd_mux_state);
  651. } else {
  652. if (connector->connector_type == DRM_MODE_CONNECTOR_VGA ||
  653. connector->connector_type == DRM_MODE_CONNECTOR_DVII ||
  654. connector->connector_type == DRM_MODE_CONNECTOR_DVID ||
  655. connector->connector_type == DRM_MODE_CONNECTOR_DVIA ||
  656. connector->connector_type == DRM_MODE_CONNECTOR_HDMIA ||
  657. connector->connector_type == DRM_MODE_CONNECTOR_HDMIB)
  658. DRM_INFO(" DDC: no ddc bus - possible BIOS bug - please report to xorg-driver-ati@lists.x.org\n");
  659. }
  660. DRM_INFO(" Encoders:\n");
  661. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  662. radeon_encoder = to_radeon_encoder(encoder);
  663. devices = radeon_encoder->devices & radeon_connector->devices;
  664. if (devices) {
  665. if (devices & ATOM_DEVICE_CRT1_SUPPORT)
  666. DRM_INFO(" CRT1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  667. if (devices & ATOM_DEVICE_CRT2_SUPPORT)
  668. DRM_INFO(" CRT2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  669. if (devices & ATOM_DEVICE_LCD1_SUPPORT)
  670. DRM_INFO(" LCD1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  671. if (devices & ATOM_DEVICE_DFP1_SUPPORT)
  672. DRM_INFO(" DFP1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  673. if (devices & ATOM_DEVICE_DFP2_SUPPORT)
  674. DRM_INFO(" DFP2: %s\n", encoder_names[radeon_encoder->encoder_id]);
  675. if (devices & ATOM_DEVICE_DFP3_SUPPORT)
  676. DRM_INFO(" DFP3: %s\n", encoder_names[radeon_encoder->encoder_id]);
  677. if (devices & ATOM_DEVICE_DFP4_SUPPORT)
  678. DRM_INFO(" DFP4: %s\n", encoder_names[radeon_encoder->encoder_id]);
  679. if (devices & ATOM_DEVICE_DFP5_SUPPORT)
  680. DRM_INFO(" DFP5: %s\n", encoder_names[radeon_encoder->encoder_id]);
  681. if (devices & ATOM_DEVICE_DFP6_SUPPORT)
  682. DRM_INFO(" DFP6: %s\n", encoder_names[radeon_encoder->encoder_id]);
  683. if (devices & ATOM_DEVICE_TV1_SUPPORT)
  684. DRM_INFO(" TV1: %s\n", encoder_names[radeon_encoder->encoder_id]);
  685. if (devices & ATOM_DEVICE_CV_SUPPORT)
  686. DRM_INFO(" CV: %s\n", encoder_names[radeon_encoder->encoder_id]);
  687. }
  688. }
  689. i++;
  690. }
  691. }
  692. static bool radeon_setup_enc_conn(struct drm_device *dev)
  693. {
  694. struct radeon_device *rdev = dev->dev_private;
  695. bool ret = false;
  696. if (rdev->bios) {
  697. if (rdev->is_atom_bios) {
  698. ret = radeon_get_atom_connector_info_from_supported_devices_table(dev);
  699. if (ret == false)
  700. ret = radeon_get_atom_connector_info_from_object_table(dev);
  701. } else {
  702. ret = radeon_get_legacy_connector_info_from_bios(dev);
  703. if (ret == false)
  704. ret = radeon_get_legacy_connector_info_from_table(dev);
  705. }
  706. } else {
  707. if (!ASIC_IS_AVIVO(rdev))
  708. ret = radeon_get_legacy_connector_info_from_table(dev);
  709. }
  710. if (ret) {
  711. radeon_setup_encoder_clones(dev);
  712. radeon_print_display_setup(dev);
  713. }
  714. return ret;
  715. }
  716. int radeon_ddc_get_modes(struct radeon_connector *radeon_connector)
  717. {
  718. struct drm_device *dev = radeon_connector->base.dev;
  719. struct radeon_device *rdev = dev->dev_private;
  720. int ret = 0;
  721. /* don't leak the edid if we already fetched it in detect() */
  722. if (radeon_connector->edid)
  723. goto got_edid;
  724. /* on hw with routers, select right port */
  725. if (radeon_connector->router.ddc_valid)
  726. radeon_router_select_ddc_port(radeon_connector);
  727. if (radeon_connector_encoder_get_dp_bridge_encoder_id(&radeon_connector->base) !=
  728. ENCODER_OBJECT_ID_NONE) {
  729. if (radeon_connector->ddc_bus->has_aux)
  730. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  731. &radeon_connector->ddc_bus->aux.ddc);
  732. } else if ((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_DisplayPort) ||
  733. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)) {
  734. struct radeon_connector_atom_dig *dig = radeon_connector->con_priv;
  735. if ((dig->dp_sink_type == CONNECTOR_OBJECT_ID_DISPLAYPORT ||
  736. dig->dp_sink_type == CONNECTOR_OBJECT_ID_eDP) &&
  737. radeon_connector->ddc_bus->has_aux)
  738. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  739. &radeon_connector->ddc_bus->aux.ddc);
  740. else if (radeon_connector->ddc_bus && !radeon_connector->edid)
  741. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  742. &radeon_connector->ddc_bus->adapter);
  743. } else {
  744. if (radeon_connector->ddc_bus && !radeon_connector->edid)
  745. radeon_connector->edid = drm_get_edid(&radeon_connector->base,
  746. &radeon_connector->ddc_bus->adapter);
  747. }
  748. if (!radeon_connector->edid) {
  749. if (rdev->is_atom_bios) {
  750. /* some laptops provide a hardcoded edid in rom for LCDs */
  751. if (((radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_LVDS) ||
  752. (radeon_connector->base.connector_type == DRM_MODE_CONNECTOR_eDP)))
  753. radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  754. } else
  755. /* some servers provide a hardcoded edid in rom for KVMs */
  756. radeon_connector->edid = radeon_bios_get_hardcoded_edid(rdev);
  757. }
  758. if (radeon_connector->edid) {
  759. got_edid:
  760. drm_mode_connector_update_edid_property(&radeon_connector->base, radeon_connector->edid);
  761. ret = drm_add_edid_modes(&radeon_connector->base, radeon_connector->edid);
  762. drm_edid_to_eld(&radeon_connector->base, radeon_connector->edid);
  763. return ret;
  764. }
  765. drm_mode_connector_update_edid_property(&radeon_connector->base, NULL);
  766. return 0;
  767. }
  768. /* avivo */
  769. /**
  770. * avivo_reduce_ratio - fractional number reduction
  771. *
  772. * @nom: nominator
  773. * @den: denominator
  774. * @nom_min: minimum value for nominator
  775. * @den_min: minimum value for denominator
  776. *
  777. * Find the greatest common divisor and apply it on both nominator and
  778. * denominator, but make nominator and denominator are at least as large
  779. * as their minimum values.
  780. */
  781. static void avivo_reduce_ratio(unsigned *nom, unsigned *den,
  782. unsigned nom_min, unsigned den_min)
  783. {
  784. unsigned tmp;
  785. /* reduce the numbers to a simpler ratio */
  786. tmp = gcd(*nom, *den);
  787. *nom /= tmp;
  788. *den /= tmp;
  789. /* make sure nominator is large enough */
  790. if (*nom < nom_min) {
  791. tmp = DIV_ROUND_UP(nom_min, *nom);
  792. *nom *= tmp;
  793. *den *= tmp;
  794. }
  795. /* make sure the denominator is large enough */
  796. if (*den < den_min) {
  797. tmp = DIV_ROUND_UP(den_min, *den);
  798. *nom *= tmp;
  799. *den *= tmp;
  800. }
  801. }
  802. /**
  803. * avivo_get_fb_ref_div - feedback and ref divider calculation
  804. *
  805. * @nom: nominator
  806. * @den: denominator
  807. * @post_div: post divider
  808. * @fb_div_max: feedback divider maximum
  809. * @ref_div_max: reference divider maximum
  810. * @fb_div: resulting feedback divider
  811. * @ref_div: resulting reference divider
  812. *
  813. * Calculate feedback and reference divider for a given post divider. Makes
  814. * sure we stay within the limits.
  815. */
  816. static void avivo_get_fb_ref_div(unsigned nom, unsigned den, unsigned post_div,
  817. unsigned fb_div_max, unsigned ref_div_max,
  818. unsigned *fb_div, unsigned *ref_div)
  819. {
  820. /* limit reference * post divider to a maximum */
  821. ref_div_max = max(min(100 / post_div, ref_div_max), 1u);
  822. /* get matching reference and feedback divider */
  823. *ref_div = min(max(DIV_ROUND_CLOSEST(den, post_div), 1u), ref_div_max);
  824. *fb_div = DIV_ROUND_CLOSEST(nom * *ref_div * post_div, den);
  825. /* limit fb divider to its maximum */
  826. if (*fb_div > fb_div_max) {
  827. *ref_div = DIV_ROUND_CLOSEST(*ref_div * fb_div_max, *fb_div);
  828. *fb_div = fb_div_max;
  829. }
  830. }
  831. /**
  832. * radeon_compute_pll_avivo - compute PLL paramaters
  833. *
  834. * @pll: information about the PLL
  835. * @dot_clock_p: resulting pixel clock
  836. * fb_div_p: resulting feedback divider
  837. * frac_fb_div_p: fractional part of the feedback divider
  838. * ref_div_p: resulting reference divider
  839. * post_div_p: resulting reference divider
  840. *
  841. * Try to calculate the PLL parameters to generate the given frequency:
  842. * dot_clock = (ref_freq * feedback_div) / (ref_div * post_div)
  843. */
  844. void radeon_compute_pll_avivo(struct radeon_pll *pll,
  845. u32 freq,
  846. u32 *dot_clock_p,
  847. u32 *fb_div_p,
  848. u32 *frac_fb_div_p,
  849. u32 *ref_div_p,
  850. u32 *post_div_p)
  851. {
  852. unsigned target_clock = pll->flags & RADEON_PLL_USE_FRAC_FB_DIV ?
  853. freq : freq / 10;
  854. unsigned fb_div_min, fb_div_max, fb_div;
  855. unsigned post_div_min, post_div_max, post_div;
  856. unsigned ref_div_min, ref_div_max, ref_div;
  857. unsigned post_div_best, diff_best;
  858. unsigned nom, den;
  859. /* determine allowed feedback divider range */
  860. fb_div_min = pll->min_feedback_div;
  861. fb_div_max = pll->max_feedback_div;
  862. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  863. fb_div_min *= 10;
  864. fb_div_max *= 10;
  865. }
  866. /* determine allowed ref divider range */
  867. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  868. ref_div_min = pll->reference_div;
  869. else
  870. ref_div_min = pll->min_ref_div;
  871. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV &&
  872. pll->flags & RADEON_PLL_USE_REF_DIV)
  873. ref_div_max = pll->reference_div;
  874. else
  875. ref_div_max = pll->max_ref_div;
  876. /* determine allowed post divider range */
  877. if (pll->flags & RADEON_PLL_USE_POST_DIV) {
  878. post_div_min = pll->post_div;
  879. post_div_max = pll->post_div;
  880. } else {
  881. unsigned vco_min, vco_max;
  882. if (pll->flags & RADEON_PLL_IS_LCD) {
  883. vco_min = pll->lcd_pll_out_min;
  884. vco_max = pll->lcd_pll_out_max;
  885. } else {
  886. vco_min = pll->pll_out_min;
  887. vco_max = pll->pll_out_max;
  888. }
  889. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  890. vco_min *= 10;
  891. vco_max *= 10;
  892. }
  893. post_div_min = vco_min / target_clock;
  894. if ((target_clock * post_div_min) < vco_min)
  895. ++post_div_min;
  896. if (post_div_min < pll->min_post_div)
  897. post_div_min = pll->min_post_div;
  898. post_div_max = vco_max / target_clock;
  899. if ((target_clock * post_div_max) > vco_max)
  900. --post_div_max;
  901. if (post_div_max > pll->max_post_div)
  902. post_div_max = pll->max_post_div;
  903. }
  904. /* represent the searched ratio as fractional number */
  905. nom = target_clock;
  906. den = pll->reference_freq;
  907. /* reduce the numbers to a simpler ratio */
  908. avivo_reduce_ratio(&nom, &den, fb_div_min, post_div_min);
  909. /* now search for a post divider */
  910. if (pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP)
  911. post_div_best = post_div_min;
  912. else
  913. post_div_best = post_div_max;
  914. diff_best = ~0;
  915. for (post_div = post_div_min; post_div <= post_div_max; ++post_div) {
  916. unsigned diff;
  917. avivo_get_fb_ref_div(nom, den, post_div, fb_div_max,
  918. ref_div_max, &fb_div, &ref_div);
  919. diff = abs(target_clock - (pll->reference_freq * fb_div) /
  920. (ref_div * post_div));
  921. if (diff < diff_best || (diff == diff_best &&
  922. !(pll->flags & RADEON_PLL_PREFER_MINM_OVER_MAXP))) {
  923. post_div_best = post_div;
  924. diff_best = diff;
  925. }
  926. }
  927. post_div = post_div_best;
  928. /* get the feedback and reference divider for the optimal value */
  929. avivo_get_fb_ref_div(nom, den, post_div, fb_div_max, ref_div_max,
  930. &fb_div, &ref_div);
  931. /* reduce the numbers to a simpler ratio once more */
  932. /* this also makes sure that the reference divider is large enough */
  933. avivo_reduce_ratio(&fb_div, &ref_div, fb_div_min, ref_div_min);
  934. /* avoid high jitter with small fractional dividers */
  935. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV && (fb_div % 10)) {
  936. fb_div_min = max(fb_div_min, (9 - (fb_div % 10)) * 20 + 50);
  937. if (fb_div < fb_div_min) {
  938. unsigned tmp = DIV_ROUND_UP(fb_div_min, fb_div);
  939. fb_div *= tmp;
  940. ref_div *= tmp;
  941. }
  942. }
  943. /* and finally save the result */
  944. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  945. *fb_div_p = fb_div / 10;
  946. *frac_fb_div_p = fb_div % 10;
  947. } else {
  948. *fb_div_p = fb_div;
  949. *frac_fb_div_p = 0;
  950. }
  951. *dot_clock_p = ((pll->reference_freq * *fb_div_p * 10) +
  952. (pll->reference_freq * *frac_fb_div_p)) /
  953. (ref_div * post_div * 10);
  954. *ref_div_p = ref_div;
  955. *post_div_p = post_div;
  956. DRM_DEBUG_KMS("%d - %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  957. freq, *dot_clock_p * 10, *fb_div_p, *frac_fb_div_p,
  958. ref_div, post_div);
  959. }
  960. /* pre-avivo */
  961. static inline uint32_t radeon_div(uint64_t n, uint32_t d)
  962. {
  963. uint64_t mod;
  964. n += d / 2;
  965. mod = do_div(n, d);
  966. return n;
  967. }
  968. void radeon_compute_pll_legacy(struct radeon_pll *pll,
  969. uint64_t freq,
  970. uint32_t *dot_clock_p,
  971. uint32_t *fb_div_p,
  972. uint32_t *frac_fb_div_p,
  973. uint32_t *ref_div_p,
  974. uint32_t *post_div_p)
  975. {
  976. uint32_t min_ref_div = pll->min_ref_div;
  977. uint32_t max_ref_div = pll->max_ref_div;
  978. uint32_t min_post_div = pll->min_post_div;
  979. uint32_t max_post_div = pll->max_post_div;
  980. uint32_t min_fractional_feed_div = 0;
  981. uint32_t max_fractional_feed_div = 0;
  982. uint32_t best_vco = pll->best_vco;
  983. uint32_t best_post_div = 1;
  984. uint32_t best_ref_div = 1;
  985. uint32_t best_feedback_div = 1;
  986. uint32_t best_frac_feedback_div = 0;
  987. uint32_t best_freq = -1;
  988. uint32_t best_error = 0xffffffff;
  989. uint32_t best_vco_diff = 1;
  990. uint32_t post_div;
  991. u32 pll_out_min, pll_out_max;
  992. DRM_DEBUG_KMS("PLL freq %llu %u %u\n", freq, pll->min_ref_div, pll->max_ref_div);
  993. freq = freq * 1000;
  994. if (pll->flags & RADEON_PLL_IS_LCD) {
  995. pll_out_min = pll->lcd_pll_out_min;
  996. pll_out_max = pll->lcd_pll_out_max;
  997. } else {
  998. pll_out_min = pll->pll_out_min;
  999. pll_out_max = pll->pll_out_max;
  1000. }
  1001. if (pll_out_min > 64800)
  1002. pll_out_min = 64800;
  1003. if (pll->flags & RADEON_PLL_USE_REF_DIV)
  1004. min_ref_div = max_ref_div = pll->reference_div;
  1005. else {
  1006. while (min_ref_div < max_ref_div-1) {
  1007. uint32_t mid = (min_ref_div + max_ref_div) / 2;
  1008. uint32_t pll_in = pll->reference_freq / mid;
  1009. if (pll_in < pll->pll_in_min)
  1010. max_ref_div = mid;
  1011. else if (pll_in > pll->pll_in_max)
  1012. min_ref_div = mid;
  1013. else
  1014. break;
  1015. }
  1016. }
  1017. if (pll->flags & RADEON_PLL_USE_POST_DIV)
  1018. min_post_div = max_post_div = pll->post_div;
  1019. if (pll->flags & RADEON_PLL_USE_FRAC_FB_DIV) {
  1020. min_fractional_feed_div = pll->min_frac_feedback_div;
  1021. max_fractional_feed_div = pll->max_frac_feedback_div;
  1022. }
  1023. for (post_div = max_post_div; post_div >= min_post_div; --post_div) {
  1024. uint32_t ref_div;
  1025. if ((pll->flags & RADEON_PLL_NO_ODD_POST_DIV) && (post_div & 1))
  1026. continue;
  1027. /* legacy radeons only have a few post_divs */
  1028. if (pll->flags & RADEON_PLL_LEGACY) {
  1029. if ((post_div == 5) ||
  1030. (post_div == 7) ||
  1031. (post_div == 9) ||
  1032. (post_div == 10) ||
  1033. (post_div == 11) ||
  1034. (post_div == 13) ||
  1035. (post_div == 14) ||
  1036. (post_div == 15))
  1037. continue;
  1038. }
  1039. for (ref_div = min_ref_div; ref_div <= max_ref_div; ++ref_div) {
  1040. uint32_t feedback_div, current_freq = 0, error, vco_diff;
  1041. uint32_t pll_in = pll->reference_freq / ref_div;
  1042. uint32_t min_feed_div = pll->min_feedback_div;
  1043. uint32_t max_feed_div = pll->max_feedback_div + 1;
  1044. if (pll_in < pll->pll_in_min || pll_in > pll->pll_in_max)
  1045. continue;
  1046. while (min_feed_div < max_feed_div) {
  1047. uint32_t vco;
  1048. uint32_t min_frac_feed_div = min_fractional_feed_div;
  1049. uint32_t max_frac_feed_div = max_fractional_feed_div + 1;
  1050. uint32_t frac_feedback_div;
  1051. uint64_t tmp;
  1052. feedback_div = (min_feed_div + max_feed_div) / 2;
  1053. tmp = (uint64_t)pll->reference_freq * feedback_div;
  1054. vco = radeon_div(tmp, ref_div);
  1055. if (vco < pll_out_min) {
  1056. min_feed_div = feedback_div + 1;
  1057. continue;
  1058. } else if (vco > pll_out_max) {
  1059. max_feed_div = feedback_div;
  1060. continue;
  1061. }
  1062. while (min_frac_feed_div < max_frac_feed_div) {
  1063. frac_feedback_div = (min_frac_feed_div + max_frac_feed_div) / 2;
  1064. tmp = (uint64_t)pll->reference_freq * 10000 * feedback_div;
  1065. tmp += (uint64_t)pll->reference_freq * 1000 * frac_feedback_div;
  1066. current_freq = radeon_div(tmp, ref_div * post_div);
  1067. if (pll->flags & RADEON_PLL_PREFER_CLOSEST_LOWER) {
  1068. if (freq < current_freq)
  1069. error = 0xffffffff;
  1070. else
  1071. error = freq - current_freq;
  1072. } else
  1073. error = abs(current_freq - freq);
  1074. vco_diff = abs(vco - best_vco);
  1075. if ((best_vco == 0 && error < best_error) ||
  1076. (best_vco != 0 &&
  1077. ((best_error > 100 && error < best_error - 100) ||
  1078. (abs(error - best_error) < 100 && vco_diff < best_vco_diff)))) {
  1079. best_post_div = post_div;
  1080. best_ref_div = ref_div;
  1081. best_feedback_div = feedback_div;
  1082. best_frac_feedback_div = frac_feedback_div;
  1083. best_freq = current_freq;
  1084. best_error = error;
  1085. best_vco_diff = vco_diff;
  1086. } else if (current_freq == freq) {
  1087. if (best_freq == -1) {
  1088. best_post_div = post_div;
  1089. best_ref_div = ref_div;
  1090. best_feedback_div = feedback_div;
  1091. best_frac_feedback_div = frac_feedback_div;
  1092. best_freq = current_freq;
  1093. best_error = error;
  1094. best_vco_diff = vco_diff;
  1095. } else if (((pll->flags & RADEON_PLL_PREFER_LOW_REF_DIV) && (ref_div < best_ref_div)) ||
  1096. ((pll->flags & RADEON_PLL_PREFER_HIGH_REF_DIV) && (ref_div > best_ref_div)) ||
  1097. ((pll->flags & RADEON_PLL_PREFER_LOW_FB_DIV) && (feedback_div < best_feedback_div)) ||
  1098. ((pll->flags & RADEON_PLL_PREFER_HIGH_FB_DIV) && (feedback_div > best_feedback_div)) ||
  1099. ((pll->flags & RADEON_PLL_PREFER_LOW_POST_DIV) && (post_div < best_post_div)) ||
  1100. ((pll->flags & RADEON_PLL_PREFER_HIGH_POST_DIV) && (post_div > best_post_div))) {
  1101. best_post_div = post_div;
  1102. best_ref_div = ref_div;
  1103. best_feedback_div = feedback_div;
  1104. best_frac_feedback_div = frac_feedback_div;
  1105. best_freq = current_freq;
  1106. best_error = error;
  1107. best_vco_diff = vco_diff;
  1108. }
  1109. }
  1110. if (current_freq < freq)
  1111. min_frac_feed_div = frac_feedback_div + 1;
  1112. else
  1113. max_frac_feed_div = frac_feedback_div;
  1114. }
  1115. if (current_freq < freq)
  1116. min_feed_div = feedback_div + 1;
  1117. else
  1118. max_feed_div = feedback_div;
  1119. }
  1120. }
  1121. }
  1122. *dot_clock_p = best_freq / 10000;
  1123. *fb_div_p = best_feedback_div;
  1124. *frac_fb_div_p = best_frac_feedback_div;
  1125. *ref_div_p = best_ref_div;
  1126. *post_div_p = best_post_div;
  1127. DRM_DEBUG_KMS("%lld %d, pll dividers - fb: %d.%d ref: %d, post %d\n",
  1128. (long long)freq,
  1129. best_freq / 1000, best_feedback_div, best_frac_feedback_div,
  1130. best_ref_div, best_post_div);
  1131. }
  1132. static void radeon_user_framebuffer_destroy(struct drm_framebuffer *fb)
  1133. {
  1134. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  1135. if (radeon_fb->obj) {
  1136. drm_gem_object_unreference_unlocked(radeon_fb->obj);
  1137. }
  1138. drm_framebuffer_cleanup(fb);
  1139. kfree(radeon_fb);
  1140. }
  1141. static int radeon_user_framebuffer_create_handle(struct drm_framebuffer *fb,
  1142. struct drm_file *file_priv,
  1143. unsigned int *handle)
  1144. {
  1145. struct radeon_framebuffer *radeon_fb = to_radeon_framebuffer(fb);
  1146. return drm_gem_handle_create(file_priv, radeon_fb->obj, handle);
  1147. }
  1148. static const struct drm_framebuffer_funcs radeon_fb_funcs = {
  1149. .destroy = radeon_user_framebuffer_destroy,
  1150. .create_handle = radeon_user_framebuffer_create_handle,
  1151. };
  1152. int
  1153. radeon_framebuffer_init(struct drm_device *dev,
  1154. struct radeon_framebuffer *rfb,
  1155. struct drm_mode_fb_cmd2 *mode_cmd,
  1156. struct drm_gem_object *obj)
  1157. {
  1158. int ret;
  1159. rfb->obj = obj;
  1160. drm_helper_mode_fill_fb_struct(&rfb->base, mode_cmd);
  1161. ret = drm_framebuffer_init(dev, &rfb->base, &radeon_fb_funcs);
  1162. if (ret) {
  1163. rfb->obj = NULL;
  1164. return ret;
  1165. }
  1166. return 0;
  1167. }
  1168. static struct drm_framebuffer *
  1169. radeon_user_framebuffer_create(struct drm_device *dev,
  1170. struct drm_file *file_priv,
  1171. struct drm_mode_fb_cmd2 *mode_cmd)
  1172. {
  1173. struct drm_gem_object *obj;
  1174. struct radeon_framebuffer *radeon_fb;
  1175. int ret;
  1176. obj = drm_gem_object_lookup(dev, file_priv, mode_cmd->handles[0]);
  1177. if (obj == NULL) {
  1178. dev_err(&dev->pdev->dev, "No GEM object associated to handle 0x%08X, "
  1179. "can't create framebuffer\n", mode_cmd->handles[0]);
  1180. return ERR_PTR(-ENOENT);
  1181. }
  1182. radeon_fb = kzalloc(sizeof(*radeon_fb), GFP_KERNEL);
  1183. if (radeon_fb == NULL) {
  1184. drm_gem_object_unreference_unlocked(obj);
  1185. return ERR_PTR(-ENOMEM);
  1186. }
  1187. ret = radeon_framebuffer_init(dev, radeon_fb, mode_cmd, obj);
  1188. if (ret) {
  1189. kfree(radeon_fb);
  1190. drm_gem_object_unreference_unlocked(obj);
  1191. return ERR_PTR(ret);
  1192. }
  1193. return &radeon_fb->base;
  1194. }
  1195. static void radeon_output_poll_changed(struct drm_device *dev)
  1196. {
  1197. struct radeon_device *rdev = dev->dev_private;
  1198. radeon_fb_output_poll_changed(rdev);
  1199. }
  1200. static const struct drm_mode_config_funcs radeon_mode_funcs = {
  1201. .fb_create = radeon_user_framebuffer_create,
  1202. .output_poll_changed = radeon_output_poll_changed
  1203. };
  1204. static struct drm_prop_enum_list radeon_tmds_pll_enum_list[] =
  1205. { { 0, "driver" },
  1206. { 1, "bios" },
  1207. };
  1208. static struct drm_prop_enum_list radeon_tv_std_enum_list[] =
  1209. { { TV_STD_NTSC, "ntsc" },
  1210. { TV_STD_PAL, "pal" },
  1211. { TV_STD_PAL_M, "pal-m" },
  1212. { TV_STD_PAL_60, "pal-60" },
  1213. { TV_STD_NTSC_J, "ntsc-j" },
  1214. { TV_STD_SCART_PAL, "scart-pal" },
  1215. { TV_STD_PAL_CN, "pal-cn" },
  1216. { TV_STD_SECAM, "secam" },
  1217. };
  1218. static struct drm_prop_enum_list radeon_underscan_enum_list[] =
  1219. { { UNDERSCAN_OFF, "off" },
  1220. { UNDERSCAN_ON, "on" },
  1221. { UNDERSCAN_AUTO, "auto" },
  1222. };
  1223. static struct drm_prop_enum_list radeon_audio_enum_list[] =
  1224. { { RADEON_AUDIO_DISABLE, "off" },
  1225. { RADEON_AUDIO_ENABLE, "on" },
  1226. { RADEON_AUDIO_AUTO, "auto" },
  1227. };
  1228. /* XXX support different dither options? spatial, temporal, both, etc. */
  1229. static struct drm_prop_enum_list radeon_dither_enum_list[] =
  1230. { { RADEON_FMT_DITHER_DISABLE, "off" },
  1231. { RADEON_FMT_DITHER_ENABLE, "on" },
  1232. };
  1233. static int radeon_modeset_create_props(struct radeon_device *rdev)
  1234. {
  1235. int sz;
  1236. if (rdev->is_atom_bios) {
  1237. rdev->mode_info.coherent_mode_property =
  1238. drm_property_create_range(rdev->ddev, 0 , "coherent", 0, 1);
  1239. if (!rdev->mode_info.coherent_mode_property)
  1240. return -ENOMEM;
  1241. }
  1242. if (!ASIC_IS_AVIVO(rdev)) {
  1243. sz = ARRAY_SIZE(radeon_tmds_pll_enum_list);
  1244. rdev->mode_info.tmds_pll_property =
  1245. drm_property_create_enum(rdev->ddev, 0,
  1246. "tmds_pll",
  1247. radeon_tmds_pll_enum_list, sz);
  1248. }
  1249. rdev->mode_info.load_detect_property =
  1250. drm_property_create_range(rdev->ddev, 0, "load detection", 0, 1);
  1251. if (!rdev->mode_info.load_detect_property)
  1252. return -ENOMEM;
  1253. drm_mode_create_scaling_mode_property(rdev->ddev);
  1254. sz = ARRAY_SIZE(radeon_tv_std_enum_list);
  1255. rdev->mode_info.tv_std_property =
  1256. drm_property_create_enum(rdev->ddev, 0,
  1257. "tv standard",
  1258. radeon_tv_std_enum_list, sz);
  1259. sz = ARRAY_SIZE(radeon_underscan_enum_list);
  1260. rdev->mode_info.underscan_property =
  1261. drm_property_create_enum(rdev->ddev, 0,
  1262. "underscan",
  1263. radeon_underscan_enum_list, sz);
  1264. rdev->mode_info.underscan_hborder_property =
  1265. drm_property_create_range(rdev->ddev, 0,
  1266. "underscan hborder", 0, 128);
  1267. if (!rdev->mode_info.underscan_hborder_property)
  1268. return -ENOMEM;
  1269. rdev->mode_info.underscan_vborder_property =
  1270. drm_property_create_range(rdev->ddev, 0,
  1271. "underscan vborder", 0, 128);
  1272. if (!rdev->mode_info.underscan_vborder_property)
  1273. return -ENOMEM;
  1274. sz = ARRAY_SIZE(radeon_audio_enum_list);
  1275. rdev->mode_info.audio_property =
  1276. drm_property_create_enum(rdev->ddev, 0,
  1277. "audio",
  1278. radeon_audio_enum_list, sz);
  1279. sz = ARRAY_SIZE(radeon_dither_enum_list);
  1280. rdev->mode_info.dither_property =
  1281. drm_property_create_enum(rdev->ddev, 0,
  1282. "dither",
  1283. radeon_dither_enum_list, sz);
  1284. return 0;
  1285. }
  1286. void radeon_update_display_priority(struct radeon_device *rdev)
  1287. {
  1288. /* adjustment options for the display watermarks */
  1289. if ((radeon_disp_priority == 0) || (radeon_disp_priority > 2)) {
  1290. /* set display priority to high for r3xx, rv515 chips
  1291. * this avoids flickering due to underflow to the
  1292. * display controllers during heavy acceleration.
  1293. * Don't force high on rs4xx igp chips as it seems to
  1294. * affect the sound card. See kernel bug 15982.
  1295. */
  1296. if ((ASIC_IS_R300(rdev) || (rdev->family == CHIP_RV515)) &&
  1297. !(rdev->flags & RADEON_IS_IGP))
  1298. rdev->disp_priority = 2;
  1299. else
  1300. rdev->disp_priority = 0;
  1301. } else
  1302. rdev->disp_priority = radeon_disp_priority;
  1303. }
  1304. /*
  1305. * Allocate hdmi structs and determine register offsets
  1306. */
  1307. static void radeon_afmt_init(struct radeon_device *rdev)
  1308. {
  1309. int i;
  1310. for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++)
  1311. rdev->mode_info.afmt[i] = NULL;
  1312. if (ASIC_IS_NODCE(rdev)) {
  1313. /* nothing to do */
  1314. } else if (ASIC_IS_DCE4(rdev)) {
  1315. static uint32_t eg_offsets[] = {
  1316. EVERGREEN_CRTC0_REGISTER_OFFSET,
  1317. EVERGREEN_CRTC1_REGISTER_OFFSET,
  1318. EVERGREEN_CRTC2_REGISTER_OFFSET,
  1319. EVERGREEN_CRTC3_REGISTER_OFFSET,
  1320. EVERGREEN_CRTC4_REGISTER_OFFSET,
  1321. EVERGREEN_CRTC5_REGISTER_OFFSET,
  1322. 0x13830 - 0x7030,
  1323. };
  1324. int num_afmt;
  1325. /* DCE8 has 7 audio blocks tied to DIG encoders */
  1326. /* DCE6 has 6 audio blocks tied to DIG encoders */
  1327. /* DCE4/5 has 6 audio blocks tied to DIG encoders */
  1328. /* DCE4.1 has 2 audio blocks tied to DIG encoders */
  1329. if (ASIC_IS_DCE8(rdev))
  1330. num_afmt = 7;
  1331. else if (ASIC_IS_DCE6(rdev))
  1332. num_afmt = 6;
  1333. else if (ASIC_IS_DCE5(rdev))
  1334. num_afmt = 6;
  1335. else if (ASIC_IS_DCE41(rdev))
  1336. num_afmt = 2;
  1337. else /* DCE4 */
  1338. num_afmt = 6;
  1339. BUG_ON(num_afmt > ARRAY_SIZE(eg_offsets));
  1340. for (i = 0; i < num_afmt; i++) {
  1341. rdev->mode_info.afmt[i] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1342. if (rdev->mode_info.afmt[i]) {
  1343. rdev->mode_info.afmt[i]->offset = eg_offsets[i];
  1344. rdev->mode_info.afmt[i]->id = i;
  1345. }
  1346. }
  1347. } else if (ASIC_IS_DCE3(rdev)) {
  1348. /* DCE3.x has 2 audio blocks tied to DIG encoders */
  1349. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1350. if (rdev->mode_info.afmt[0]) {
  1351. rdev->mode_info.afmt[0]->offset = DCE3_HDMI_OFFSET0;
  1352. rdev->mode_info.afmt[0]->id = 0;
  1353. }
  1354. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1355. if (rdev->mode_info.afmt[1]) {
  1356. rdev->mode_info.afmt[1]->offset = DCE3_HDMI_OFFSET1;
  1357. rdev->mode_info.afmt[1]->id = 1;
  1358. }
  1359. } else if (ASIC_IS_DCE2(rdev)) {
  1360. /* DCE2 has at least 1 routable audio block */
  1361. rdev->mode_info.afmt[0] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1362. if (rdev->mode_info.afmt[0]) {
  1363. rdev->mode_info.afmt[0]->offset = DCE2_HDMI_OFFSET0;
  1364. rdev->mode_info.afmt[0]->id = 0;
  1365. }
  1366. /* r6xx has 2 routable audio blocks */
  1367. if (rdev->family >= CHIP_R600) {
  1368. rdev->mode_info.afmt[1] = kzalloc(sizeof(struct radeon_afmt), GFP_KERNEL);
  1369. if (rdev->mode_info.afmt[1]) {
  1370. rdev->mode_info.afmt[1]->offset = DCE2_HDMI_OFFSET1;
  1371. rdev->mode_info.afmt[1]->id = 1;
  1372. }
  1373. }
  1374. }
  1375. }
  1376. static void radeon_afmt_fini(struct radeon_device *rdev)
  1377. {
  1378. int i;
  1379. for (i = 0; i < RADEON_MAX_AFMT_BLOCKS; i++) {
  1380. kfree(rdev->mode_info.afmt[i]);
  1381. rdev->mode_info.afmt[i] = NULL;
  1382. }
  1383. }
  1384. int radeon_modeset_init(struct radeon_device *rdev)
  1385. {
  1386. int i;
  1387. int ret;
  1388. drm_mode_config_init(rdev->ddev);
  1389. rdev->mode_info.mode_config_initialized = true;
  1390. rdev->ddev->mode_config.funcs = &radeon_mode_funcs;
  1391. if (ASIC_IS_DCE5(rdev)) {
  1392. rdev->ddev->mode_config.max_width = 16384;
  1393. rdev->ddev->mode_config.max_height = 16384;
  1394. } else if (ASIC_IS_AVIVO(rdev)) {
  1395. rdev->ddev->mode_config.max_width = 8192;
  1396. rdev->ddev->mode_config.max_height = 8192;
  1397. } else {
  1398. rdev->ddev->mode_config.max_width = 4096;
  1399. rdev->ddev->mode_config.max_height = 4096;
  1400. }
  1401. rdev->ddev->mode_config.preferred_depth = 24;
  1402. rdev->ddev->mode_config.prefer_shadow = 1;
  1403. rdev->ddev->mode_config.fb_base = rdev->mc.aper_base;
  1404. ret = radeon_modeset_create_props(rdev);
  1405. if (ret) {
  1406. return ret;
  1407. }
  1408. /* init i2c buses */
  1409. radeon_i2c_init(rdev);
  1410. /* check combios for a valid hardcoded EDID - Sun servers */
  1411. if (!rdev->is_atom_bios) {
  1412. /* check for hardcoded EDID in BIOS */
  1413. radeon_combios_check_hardcoded_edid(rdev);
  1414. }
  1415. /* allocate crtcs */
  1416. for (i = 0; i < rdev->num_crtc; i++) {
  1417. radeon_crtc_init(rdev->ddev, i);
  1418. }
  1419. /* okay we should have all the bios connectors */
  1420. ret = radeon_setup_enc_conn(rdev->ddev);
  1421. if (!ret) {
  1422. return ret;
  1423. }
  1424. /* init dig PHYs, disp eng pll */
  1425. if (rdev->is_atom_bios) {
  1426. radeon_atom_encoder_init(rdev);
  1427. radeon_atom_disp_eng_pll_init(rdev);
  1428. }
  1429. /* initialize hpd */
  1430. radeon_hpd_init(rdev);
  1431. /* setup afmt */
  1432. radeon_afmt_init(rdev);
  1433. radeon_fbdev_init(rdev);
  1434. drm_kms_helper_poll_init(rdev->ddev);
  1435. if (rdev->pm.dpm_enabled) {
  1436. /* do dpm late init */
  1437. ret = radeon_pm_late_init(rdev);
  1438. if (ret) {
  1439. rdev->pm.dpm_enabled = false;
  1440. DRM_ERROR("radeon_pm_late_init failed, disabling dpm\n");
  1441. }
  1442. /* set the dpm state for PX since there won't be
  1443. * a modeset to call this.
  1444. */
  1445. radeon_pm_compute_clocks(rdev);
  1446. }
  1447. return 0;
  1448. }
  1449. void radeon_modeset_fini(struct radeon_device *rdev)
  1450. {
  1451. radeon_fbdev_fini(rdev);
  1452. kfree(rdev->mode_info.bios_hardcoded_edid);
  1453. if (rdev->mode_info.mode_config_initialized) {
  1454. radeon_afmt_fini(rdev);
  1455. drm_kms_helper_poll_fini(rdev->ddev);
  1456. radeon_hpd_fini(rdev);
  1457. drm_mode_config_cleanup(rdev->ddev);
  1458. rdev->mode_info.mode_config_initialized = false;
  1459. }
  1460. /* free i2c buses */
  1461. radeon_i2c_fini(rdev);
  1462. }
  1463. static bool is_hdtv_mode(const struct drm_display_mode *mode)
  1464. {
  1465. /* try and guess if this is a tv or a monitor */
  1466. if ((mode->vdisplay == 480 && mode->hdisplay == 720) || /* 480p */
  1467. (mode->vdisplay == 576) || /* 576p */
  1468. (mode->vdisplay == 720) || /* 720p */
  1469. (mode->vdisplay == 1080)) /* 1080p */
  1470. return true;
  1471. else
  1472. return false;
  1473. }
  1474. bool radeon_crtc_scaling_mode_fixup(struct drm_crtc *crtc,
  1475. const struct drm_display_mode *mode,
  1476. struct drm_display_mode *adjusted_mode)
  1477. {
  1478. struct drm_device *dev = crtc->dev;
  1479. struct radeon_device *rdev = dev->dev_private;
  1480. struct drm_encoder *encoder;
  1481. struct radeon_crtc *radeon_crtc = to_radeon_crtc(crtc);
  1482. struct radeon_encoder *radeon_encoder;
  1483. struct drm_connector *connector;
  1484. struct radeon_connector *radeon_connector;
  1485. bool first = true;
  1486. u32 src_v = 1, dst_v = 1;
  1487. u32 src_h = 1, dst_h = 1;
  1488. radeon_crtc->h_border = 0;
  1489. radeon_crtc->v_border = 0;
  1490. list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
  1491. if (encoder->crtc != crtc)
  1492. continue;
  1493. radeon_encoder = to_radeon_encoder(encoder);
  1494. connector = radeon_get_connector_for_encoder(encoder);
  1495. radeon_connector = to_radeon_connector(connector);
  1496. if (first) {
  1497. /* set scaling */
  1498. if (radeon_encoder->rmx_type == RMX_OFF)
  1499. radeon_crtc->rmx_type = RMX_OFF;
  1500. else if (mode->hdisplay < radeon_encoder->native_mode.hdisplay ||
  1501. mode->vdisplay < radeon_encoder->native_mode.vdisplay)
  1502. radeon_crtc->rmx_type = radeon_encoder->rmx_type;
  1503. else
  1504. radeon_crtc->rmx_type = RMX_OFF;
  1505. /* copy native mode */
  1506. memcpy(&radeon_crtc->native_mode,
  1507. &radeon_encoder->native_mode,
  1508. sizeof(struct drm_display_mode));
  1509. src_v = crtc->mode.vdisplay;
  1510. dst_v = radeon_crtc->native_mode.vdisplay;
  1511. src_h = crtc->mode.hdisplay;
  1512. dst_h = radeon_crtc->native_mode.hdisplay;
  1513. /* fix up for overscan on hdmi */
  1514. if (ASIC_IS_AVIVO(rdev) &&
  1515. (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) &&
  1516. ((radeon_encoder->underscan_type == UNDERSCAN_ON) ||
  1517. ((radeon_encoder->underscan_type == UNDERSCAN_AUTO) &&
  1518. drm_detect_hdmi_monitor(radeon_connector->edid) &&
  1519. is_hdtv_mode(mode)))) {
  1520. if (radeon_encoder->underscan_hborder != 0)
  1521. radeon_crtc->h_border = radeon_encoder->underscan_hborder;
  1522. else
  1523. radeon_crtc->h_border = (mode->hdisplay >> 5) + 16;
  1524. if (radeon_encoder->underscan_vborder != 0)
  1525. radeon_crtc->v_border = radeon_encoder->underscan_vborder;
  1526. else
  1527. radeon_crtc->v_border = (mode->vdisplay >> 5) + 16;
  1528. radeon_crtc->rmx_type = RMX_FULL;
  1529. src_v = crtc->mode.vdisplay;
  1530. dst_v = crtc->mode.vdisplay - (radeon_crtc->v_border * 2);
  1531. src_h = crtc->mode.hdisplay;
  1532. dst_h = crtc->mode.hdisplay - (radeon_crtc->h_border * 2);
  1533. }
  1534. first = false;
  1535. } else {
  1536. if (radeon_crtc->rmx_type != radeon_encoder->rmx_type) {
  1537. /* WARNING: Right now this can't happen but
  1538. * in the future we need to check that scaling
  1539. * are consistent across different encoder
  1540. * (ie all encoder can work with the same
  1541. * scaling).
  1542. */
  1543. DRM_ERROR("Scaling not consistent across encoder.\n");
  1544. return false;
  1545. }
  1546. }
  1547. }
  1548. if (radeon_crtc->rmx_type != RMX_OFF) {
  1549. fixed20_12 a, b;
  1550. a.full = dfixed_const(src_v);
  1551. b.full = dfixed_const(dst_v);
  1552. radeon_crtc->vsc.full = dfixed_div(a, b);
  1553. a.full = dfixed_const(src_h);
  1554. b.full = dfixed_const(dst_h);
  1555. radeon_crtc->hsc.full = dfixed_div(a, b);
  1556. } else {
  1557. radeon_crtc->vsc.full = dfixed_const(1);
  1558. radeon_crtc->hsc.full = dfixed_const(1);
  1559. }
  1560. return true;
  1561. }
  1562. /*
  1563. * Retrieve current video scanout position of crtc on a given gpu, and
  1564. * an optional accurate timestamp of when query happened.
  1565. *
  1566. * \param dev Device to query.
  1567. * \param crtc Crtc to query.
  1568. * \param flags Flags from caller (DRM_CALLED_FROM_VBLIRQ or 0).
  1569. * \param *vpos Location where vertical scanout position should be stored.
  1570. * \param *hpos Location where horizontal scanout position should go.
  1571. * \param *stime Target location for timestamp taken immediately before
  1572. * scanout position query. Can be NULL to skip timestamp.
  1573. * \param *etime Target location for timestamp taken immediately after
  1574. * scanout position query. Can be NULL to skip timestamp.
  1575. *
  1576. * Returns vpos as a positive number while in active scanout area.
  1577. * Returns vpos as a negative number inside vblank, counting the number
  1578. * of scanlines to go until end of vblank, e.g., -1 means "one scanline
  1579. * until start of active scanout / end of vblank."
  1580. *
  1581. * \return Flags, or'ed together as follows:
  1582. *
  1583. * DRM_SCANOUTPOS_VALID = Query successful.
  1584. * DRM_SCANOUTPOS_INVBL = Inside vblank.
  1585. * DRM_SCANOUTPOS_ACCURATE = Returned position is accurate. A lack of
  1586. * this flag means that returned position may be offset by a constant but
  1587. * unknown small number of scanlines wrt. real scanout position.
  1588. *
  1589. */
  1590. int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc, unsigned int flags,
  1591. int *vpos, int *hpos, ktime_t *stime, ktime_t *etime)
  1592. {
  1593. u32 stat_crtc = 0, vbl = 0, position = 0;
  1594. int vbl_start, vbl_end, vtotal, ret = 0;
  1595. bool in_vbl = true;
  1596. struct radeon_device *rdev = dev->dev_private;
  1597. /* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
  1598. /* Get optional system timestamp before query. */
  1599. if (stime)
  1600. *stime = ktime_get();
  1601. if (ASIC_IS_DCE4(rdev)) {
  1602. if (crtc == 0) {
  1603. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1604. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1605. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1606. EVERGREEN_CRTC0_REGISTER_OFFSET);
  1607. ret |= DRM_SCANOUTPOS_VALID;
  1608. }
  1609. if (crtc == 1) {
  1610. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1611. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1612. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1613. EVERGREEN_CRTC1_REGISTER_OFFSET);
  1614. ret |= DRM_SCANOUTPOS_VALID;
  1615. }
  1616. if (crtc == 2) {
  1617. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1618. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1619. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1620. EVERGREEN_CRTC2_REGISTER_OFFSET);
  1621. ret |= DRM_SCANOUTPOS_VALID;
  1622. }
  1623. if (crtc == 3) {
  1624. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1625. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1626. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1627. EVERGREEN_CRTC3_REGISTER_OFFSET);
  1628. ret |= DRM_SCANOUTPOS_VALID;
  1629. }
  1630. if (crtc == 4) {
  1631. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1632. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1633. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1634. EVERGREEN_CRTC4_REGISTER_OFFSET);
  1635. ret |= DRM_SCANOUTPOS_VALID;
  1636. }
  1637. if (crtc == 5) {
  1638. vbl = RREG32(EVERGREEN_CRTC_V_BLANK_START_END +
  1639. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1640. position = RREG32(EVERGREEN_CRTC_STATUS_POSITION +
  1641. EVERGREEN_CRTC5_REGISTER_OFFSET);
  1642. ret |= DRM_SCANOUTPOS_VALID;
  1643. }
  1644. } else if (ASIC_IS_AVIVO(rdev)) {
  1645. if (crtc == 0) {
  1646. vbl = RREG32(AVIVO_D1CRTC_V_BLANK_START_END);
  1647. position = RREG32(AVIVO_D1CRTC_STATUS_POSITION);
  1648. ret |= DRM_SCANOUTPOS_VALID;
  1649. }
  1650. if (crtc == 1) {
  1651. vbl = RREG32(AVIVO_D2CRTC_V_BLANK_START_END);
  1652. position = RREG32(AVIVO_D2CRTC_STATUS_POSITION);
  1653. ret |= DRM_SCANOUTPOS_VALID;
  1654. }
  1655. } else {
  1656. /* Pre-AVIVO: Different encoding of scanout pos and vblank interval. */
  1657. if (crtc == 0) {
  1658. /* Assume vbl_end == 0, get vbl_start from
  1659. * upper 16 bits.
  1660. */
  1661. vbl = (RREG32(RADEON_CRTC_V_TOTAL_DISP) &
  1662. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1663. /* Only retrieve vpos from upper 16 bits, set hpos == 0. */
  1664. position = (RREG32(RADEON_CRTC_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1665. stat_crtc = RREG32(RADEON_CRTC_STATUS);
  1666. if (!(stat_crtc & 1))
  1667. in_vbl = false;
  1668. ret |= DRM_SCANOUTPOS_VALID;
  1669. }
  1670. if (crtc == 1) {
  1671. vbl = (RREG32(RADEON_CRTC2_V_TOTAL_DISP) &
  1672. RADEON_CRTC_V_DISP) >> RADEON_CRTC_V_DISP_SHIFT;
  1673. position = (RREG32(RADEON_CRTC2_VLINE_CRNT_VLINE) >> 16) & RADEON_CRTC_V_TOTAL;
  1674. stat_crtc = RREG32(RADEON_CRTC2_STATUS);
  1675. if (!(stat_crtc & 1))
  1676. in_vbl = false;
  1677. ret |= DRM_SCANOUTPOS_VALID;
  1678. }
  1679. }
  1680. /* Get optional system timestamp after query. */
  1681. if (etime)
  1682. *etime = ktime_get();
  1683. /* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
  1684. /* Decode into vertical and horizontal scanout position. */
  1685. *vpos = position & 0x1fff;
  1686. *hpos = (position >> 16) & 0x1fff;
  1687. /* Valid vblank area boundaries from gpu retrieved? */
  1688. if (vbl > 0) {
  1689. /* Yes: Decode. */
  1690. ret |= DRM_SCANOUTPOS_ACCURATE;
  1691. vbl_start = vbl & 0x1fff;
  1692. vbl_end = (vbl >> 16) & 0x1fff;
  1693. }
  1694. else {
  1695. /* No: Fake something reasonable which gives at least ok results. */
  1696. vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1697. vbl_end = 0;
  1698. }
  1699. /* Test scanout position against vblank region. */
  1700. if ((*vpos < vbl_start) && (*vpos >= vbl_end))
  1701. in_vbl = false;
  1702. /* Check if inside vblank area and apply corrective offsets:
  1703. * vpos will then be >=0 in video scanout area, but negative
  1704. * within vblank area, counting down the number of lines until
  1705. * start of scanout.
  1706. */
  1707. /* Inside "upper part" of vblank area? Apply corrective offset if so: */
  1708. if (in_vbl && (*vpos >= vbl_start)) {
  1709. vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1710. *vpos = *vpos - vtotal;
  1711. }
  1712. /* Correct for shifted end of vbl at vbl_end. */
  1713. *vpos = *vpos - vbl_end;
  1714. /* In vblank? */
  1715. if (in_vbl)
  1716. ret |= DRM_SCANOUTPOS_INVBL;
  1717. /* Is vpos outside nominal vblank area, but less than
  1718. * 1/100 of a frame height away from start of vblank?
  1719. * If so, assume this isn't a massively delayed vblank
  1720. * interrupt, but a vblank interrupt that fired a few
  1721. * microseconds before true start of vblank. Compensate
  1722. * by adding a full frame duration to the final timestamp.
  1723. * Happens, e.g., on ATI R500, R600.
  1724. *
  1725. * We only do this if DRM_CALLED_FROM_VBLIRQ.
  1726. */
  1727. if ((flags & DRM_CALLED_FROM_VBLIRQ) && !in_vbl) {
  1728. vbl_start = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vdisplay;
  1729. vtotal = rdev->mode_info.crtcs[crtc]->base.hwmode.crtc_vtotal;
  1730. if (vbl_start - *vpos < vtotal / 100) {
  1731. *vpos -= vtotal;
  1732. /* Signal this correction as "applied". */
  1733. ret |= 0x8;
  1734. }
  1735. }
  1736. return ret;
  1737. }