radeon_display.c 59 KB

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