radeon_cp.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  1. /* radeon_cp.c -- CP support for Radeon -*- linux-c -*- */
  2. /*
  3. * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
  4. * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
  5. * Copyright 2007 Advanced Micro Devices, Inc.
  6. * All Rights Reserved.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a
  9. * copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice (including the next
  16. * paragraph) shall be included in all copies or substantial portions of the
  17. * Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  22. * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  23. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  24. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  25. * DEALINGS IN THE SOFTWARE.
  26. *
  27. * Authors:
  28. * Kevin E. Martin <martin@valinux.com>
  29. * Gareth Hughes <gareth@valinux.com>
  30. *
  31. * ------------------------ This file is DEPRECATED! -------------------------
  32. */
  33. #include <linux/module.h>
  34. #include <drm/drmP.h>
  35. #include <drm/radeon_drm.h>
  36. #include "radeon_drv.h"
  37. #include "r300_reg.h"
  38. #define RADEON_FIFO_DEBUG 0
  39. /* Firmware Names */
  40. #define FIRMWARE_R100 "radeon/R100_cp.bin"
  41. #define FIRMWARE_R200 "radeon/R200_cp.bin"
  42. #define FIRMWARE_R300 "radeon/R300_cp.bin"
  43. #define FIRMWARE_R420 "radeon/R420_cp.bin"
  44. #define FIRMWARE_RS690 "radeon/RS690_cp.bin"
  45. #define FIRMWARE_RS600 "radeon/RS600_cp.bin"
  46. #define FIRMWARE_R520 "radeon/R520_cp.bin"
  47. MODULE_FIRMWARE(FIRMWARE_R100);
  48. MODULE_FIRMWARE(FIRMWARE_R200);
  49. MODULE_FIRMWARE(FIRMWARE_R300);
  50. MODULE_FIRMWARE(FIRMWARE_R420);
  51. MODULE_FIRMWARE(FIRMWARE_RS690);
  52. MODULE_FIRMWARE(FIRMWARE_RS600);
  53. MODULE_FIRMWARE(FIRMWARE_R520);
  54. static int radeon_do_cleanup_cp(struct drm_device * dev);
  55. static void radeon_do_cp_start(drm_radeon_private_t * dev_priv);
  56. u32 radeon_read_ring_rptr(drm_radeon_private_t *dev_priv, u32 off)
  57. {
  58. u32 val;
  59. if (dev_priv->flags & RADEON_IS_AGP) {
  60. val = DRM_READ32(dev_priv->ring_rptr, off);
  61. } else {
  62. val = *(((volatile u32 *)
  63. dev_priv->ring_rptr->handle) +
  64. (off / sizeof(u32)));
  65. val = le32_to_cpu(val);
  66. }
  67. return val;
  68. }
  69. u32 radeon_get_ring_head(drm_radeon_private_t *dev_priv)
  70. {
  71. if (dev_priv->writeback_works)
  72. return radeon_read_ring_rptr(dev_priv, 0);
  73. else {
  74. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  75. return RADEON_READ(R600_CP_RB_RPTR);
  76. else
  77. return RADEON_READ(RADEON_CP_RB_RPTR);
  78. }
  79. }
  80. void radeon_write_ring_rptr(drm_radeon_private_t *dev_priv, u32 off, u32 val)
  81. {
  82. if (dev_priv->flags & RADEON_IS_AGP)
  83. DRM_WRITE32(dev_priv->ring_rptr, off, val);
  84. else
  85. *(((volatile u32 *) dev_priv->ring_rptr->handle) +
  86. (off / sizeof(u32))) = cpu_to_le32(val);
  87. }
  88. void radeon_set_ring_head(drm_radeon_private_t *dev_priv, u32 val)
  89. {
  90. radeon_write_ring_rptr(dev_priv, 0, val);
  91. }
  92. u32 radeon_get_scratch(drm_radeon_private_t *dev_priv, int index)
  93. {
  94. if (dev_priv->writeback_works) {
  95. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  96. return radeon_read_ring_rptr(dev_priv,
  97. R600_SCRATCHOFF(index));
  98. else
  99. return radeon_read_ring_rptr(dev_priv,
  100. RADEON_SCRATCHOFF(index));
  101. } else {
  102. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  103. return RADEON_READ(R600_SCRATCH_REG0 + 4*index);
  104. else
  105. return RADEON_READ(RADEON_SCRATCH_REG0 + 4*index);
  106. }
  107. }
  108. static u32 R500_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
  109. {
  110. u32 ret;
  111. RADEON_WRITE(R520_MC_IND_INDEX, 0x7f0000 | (addr & 0xff));
  112. ret = RADEON_READ(R520_MC_IND_DATA);
  113. RADEON_WRITE(R520_MC_IND_INDEX, 0);
  114. return ret;
  115. }
  116. static u32 RS480_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
  117. {
  118. u32 ret;
  119. RADEON_WRITE(RS480_NB_MC_INDEX, addr & 0xff);
  120. ret = RADEON_READ(RS480_NB_MC_DATA);
  121. RADEON_WRITE(RS480_NB_MC_INDEX, 0xff);
  122. return ret;
  123. }
  124. static u32 RS690_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
  125. {
  126. u32 ret;
  127. RADEON_WRITE(RS690_MC_INDEX, (addr & RS690_MC_INDEX_MASK));
  128. ret = RADEON_READ(RS690_MC_DATA);
  129. RADEON_WRITE(RS690_MC_INDEX, RS690_MC_INDEX_MASK);
  130. return ret;
  131. }
  132. static u32 RS600_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
  133. {
  134. u32 ret;
  135. RADEON_WRITE(RS600_MC_INDEX, ((addr & RS600_MC_ADDR_MASK) |
  136. RS600_MC_IND_CITF_ARB0));
  137. ret = RADEON_READ(RS600_MC_DATA);
  138. return ret;
  139. }
  140. static u32 IGP_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
  141. {
  142. if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  143. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
  144. return RS690_READ_MCIND(dev_priv, addr);
  145. else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
  146. return RS600_READ_MCIND(dev_priv, addr);
  147. else
  148. return RS480_READ_MCIND(dev_priv, addr);
  149. }
  150. u32 radeon_read_fb_location(drm_radeon_private_t *dev_priv)
  151. {
  152. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
  153. return RADEON_READ(R700_MC_VM_FB_LOCATION);
  154. else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  155. return RADEON_READ(R600_MC_VM_FB_LOCATION);
  156. else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
  157. return R500_READ_MCIND(dev_priv, RV515_MC_FB_LOCATION);
  158. else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  159. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
  160. return RS690_READ_MCIND(dev_priv, RS690_MC_FB_LOCATION);
  161. else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
  162. return RS600_READ_MCIND(dev_priv, RS600_MC_FB_LOCATION);
  163. else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
  164. return R500_READ_MCIND(dev_priv, R520_MC_FB_LOCATION);
  165. else
  166. return RADEON_READ(RADEON_MC_FB_LOCATION);
  167. }
  168. static void radeon_write_fb_location(drm_radeon_private_t *dev_priv, u32 fb_loc)
  169. {
  170. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
  171. RADEON_WRITE(R700_MC_VM_FB_LOCATION, fb_loc);
  172. else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  173. RADEON_WRITE(R600_MC_VM_FB_LOCATION, fb_loc);
  174. else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
  175. R500_WRITE_MCIND(RV515_MC_FB_LOCATION, fb_loc);
  176. else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  177. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
  178. RS690_WRITE_MCIND(RS690_MC_FB_LOCATION, fb_loc);
  179. else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
  180. RS600_WRITE_MCIND(RS600_MC_FB_LOCATION, fb_loc);
  181. else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
  182. R500_WRITE_MCIND(R520_MC_FB_LOCATION, fb_loc);
  183. else
  184. RADEON_WRITE(RADEON_MC_FB_LOCATION, fb_loc);
  185. }
  186. void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_loc)
  187. {
  188. /*R6xx/R7xx: AGP_TOP and BOT are actually 18 bits each */
  189. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770) {
  190. RADEON_WRITE(R700_MC_VM_AGP_BOT, agp_loc & 0xffff); /* FIX ME */
  191. RADEON_WRITE(R700_MC_VM_AGP_TOP, (agp_loc >> 16) & 0xffff);
  192. } else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
  193. RADEON_WRITE(R600_MC_VM_AGP_BOT, agp_loc & 0xffff); /* FIX ME */
  194. RADEON_WRITE(R600_MC_VM_AGP_TOP, (agp_loc >> 16) & 0xffff);
  195. } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
  196. R500_WRITE_MCIND(RV515_MC_AGP_LOCATION, agp_loc);
  197. else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  198. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
  199. RS690_WRITE_MCIND(RS690_MC_AGP_LOCATION, agp_loc);
  200. else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
  201. RS600_WRITE_MCIND(RS600_MC_AGP_LOCATION, agp_loc);
  202. else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
  203. R500_WRITE_MCIND(R520_MC_AGP_LOCATION, agp_loc);
  204. else
  205. RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc);
  206. }
  207. void radeon_write_agp_base(drm_radeon_private_t *dev_priv, u64 agp_base)
  208. {
  209. u32 agp_base_hi = upper_32_bits(agp_base);
  210. u32 agp_base_lo = agp_base & 0xffffffff;
  211. u32 r6xx_agp_base = (agp_base >> 22) & 0x3ffff;
  212. /* R6xx/R7xx must be aligned to a 4MB boundary */
  213. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
  214. RADEON_WRITE(R700_MC_VM_AGP_BASE, r6xx_agp_base);
  215. else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  216. RADEON_WRITE(R600_MC_VM_AGP_BASE, r6xx_agp_base);
  217. else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) {
  218. R500_WRITE_MCIND(RV515_MC_AGP_BASE, agp_base_lo);
  219. R500_WRITE_MCIND(RV515_MC_AGP_BASE_2, agp_base_hi);
  220. } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  221. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
  222. RS690_WRITE_MCIND(RS690_MC_AGP_BASE, agp_base_lo);
  223. RS690_WRITE_MCIND(RS690_MC_AGP_BASE_2, agp_base_hi);
  224. } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
  225. RS600_WRITE_MCIND(RS600_AGP_BASE, agp_base_lo);
  226. RS600_WRITE_MCIND(RS600_AGP_BASE_2, agp_base_hi);
  227. } else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515) {
  228. R500_WRITE_MCIND(R520_MC_AGP_BASE, agp_base_lo);
  229. R500_WRITE_MCIND(R520_MC_AGP_BASE_2, agp_base_hi);
  230. } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
  231. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
  232. RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
  233. RADEON_WRITE(RS480_AGP_BASE_2, agp_base_hi);
  234. } else {
  235. RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
  236. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200)
  237. RADEON_WRITE(RADEON_AGP_BASE_2, agp_base_hi);
  238. }
  239. }
  240. void radeon_enable_bm(struct drm_radeon_private *dev_priv)
  241. {
  242. u32 tmp;
  243. /* Turn on bus mastering */
  244. if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  245. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
  246. /* rs600/rs690/rs740 */
  247. tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RS600_BUS_MASTER_DIS;
  248. RADEON_WRITE(RADEON_BUS_CNTL, tmp);
  249. } else if (((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV350) ||
  250. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
  251. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
  252. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
  253. /* r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
  254. tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
  255. RADEON_WRITE(RADEON_BUS_CNTL, tmp);
  256. } /* PCIE cards appears to not need this */
  257. }
  258. static int RADEON_READ_PLL(struct drm_device * dev, int addr)
  259. {
  260. drm_radeon_private_t *dev_priv = dev->dev_private;
  261. RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
  262. return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
  263. }
  264. static u32 RADEON_READ_PCIE(drm_radeon_private_t *dev_priv, int addr)
  265. {
  266. RADEON_WRITE8(RADEON_PCIE_INDEX, addr & 0xff);
  267. return RADEON_READ(RADEON_PCIE_DATA);
  268. }
  269. #if RADEON_FIFO_DEBUG
  270. static void radeon_status(drm_radeon_private_t * dev_priv)
  271. {
  272. printk("%s:\n", __func__);
  273. printk("RBBM_STATUS = 0x%08x\n",
  274. (unsigned int)RADEON_READ(RADEON_RBBM_STATUS));
  275. printk("CP_RB_RTPR = 0x%08x\n",
  276. (unsigned int)RADEON_READ(RADEON_CP_RB_RPTR));
  277. printk("CP_RB_WTPR = 0x%08x\n",
  278. (unsigned int)RADEON_READ(RADEON_CP_RB_WPTR));
  279. printk("AIC_CNTL = 0x%08x\n",
  280. (unsigned int)RADEON_READ(RADEON_AIC_CNTL));
  281. printk("AIC_STAT = 0x%08x\n",
  282. (unsigned int)RADEON_READ(RADEON_AIC_STAT));
  283. printk("AIC_PT_BASE = 0x%08x\n",
  284. (unsigned int)RADEON_READ(RADEON_AIC_PT_BASE));
  285. printk("TLB_ADDR = 0x%08x\n",
  286. (unsigned int)RADEON_READ(RADEON_AIC_TLB_ADDR));
  287. printk("TLB_DATA = 0x%08x\n",
  288. (unsigned int)RADEON_READ(RADEON_AIC_TLB_DATA));
  289. }
  290. #endif
  291. /* ================================================================
  292. * Engine, FIFO control
  293. */
  294. static int radeon_do_pixcache_flush(drm_radeon_private_t * dev_priv)
  295. {
  296. u32 tmp;
  297. int i;
  298. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  299. if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV280) {
  300. tmp = RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT);
  301. tmp |= RADEON_RB3D_DC_FLUSH_ALL;
  302. RADEON_WRITE(RADEON_RB3D_DSTCACHE_CTLSTAT, tmp);
  303. for (i = 0; i < dev_priv->usec_timeout; i++) {
  304. if (!(RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT)
  305. & RADEON_RB3D_DC_BUSY)) {
  306. return 0;
  307. }
  308. DRM_UDELAY(1);
  309. }
  310. } else {
  311. /* don't flush or purge cache here or lockup */
  312. return 0;
  313. }
  314. #if RADEON_FIFO_DEBUG
  315. DRM_ERROR("failed!\n");
  316. radeon_status(dev_priv);
  317. #endif
  318. return -EBUSY;
  319. }
  320. static int radeon_do_wait_for_fifo(drm_radeon_private_t * dev_priv, int entries)
  321. {
  322. int i;
  323. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  324. for (i = 0; i < dev_priv->usec_timeout; i++) {
  325. int slots = (RADEON_READ(RADEON_RBBM_STATUS)
  326. & RADEON_RBBM_FIFOCNT_MASK);
  327. if (slots >= entries)
  328. return 0;
  329. DRM_UDELAY(1);
  330. }
  331. DRM_DEBUG("wait for fifo failed status : 0x%08X 0x%08X\n",
  332. RADEON_READ(RADEON_RBBM_STATUS),
  333. RADEON_READ(R300_VAP_CNTL_STATUS));
  334. #if RADEON_FIFO_DEBUG
  335. DRM_ERROR("failed!\n");
  336. radeon_status(dev_priv);
  337. #endif
  338. return -EBUSY;
  339. }
  340. static int radeon_do_wait_for_idle(drm_radeon_private_t * dev_priv)
  341. {
  342. int i, ret;
  343. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  344. ret = radeon_do_wait_for_fifo(dev_priv, 64);
  345. if (ret)
  346. return ret;
  347. for (i = 0; i < dev_priv->usec_timeout; i++) {
  348. if (!(RADEON_READ(RADEON_RBBM_STATUS)
  349. & RADEON_RBBM_ACTIVE)) {
  350. radeon_do_pixcache_flush(dev_priv);
  351. return 0;
  352. }
  353. DRM_UDELAY(1);
  354. }
  355. DRM_DEBUG("wait idle failed status : 0x%08X 0x%08X\n",
  356. RADEON_READ(RADEON_RBBM_STATUS),
  357. RADEON_READ(R300_VAP_CNTL_STATUS));
  358. #if RADEON_FIFO_DEBUG
  359. DRM_ERROR("failed!\n");
  360. radeon_status(dev_priv);
  361. #endif
  362. return -EBUSY;
  363. }
  364. static void radeon_init_pipes(struct drm_device *dev)
  365. {
  366. drm_radeon_private_t *dev_priv = dev->dev_private;
  367. uint32_t gb_tile_config, gb_pipe_sel = 0;
  368. if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) {
  369. uint32_t z_pipe_sel = RADEON_READ(RV530_GB_PIPE_SELECT2);
  370. if ((z_pipe_sel & 3) == 3)
  371. dev_priv->num_z_pipes = 2;
  372. else
  373. dev_priv->num_z_pipes = 1;
  374. } else
  375. dev_priv->num_z_pipes = 1;
  376. /* RS4xx/RS6xx/R4xx/R5xx */
  377. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R420) {
  378. gb_pipe_sel = RADEON_READ(R400_GB_PIPE_SELECT);
  379. dev_priv->num_gb_pipes = ((gb_pipe_sel >> 12) & 0x3) + 1;
  380. /* SE cards have 1 pipe */
  381. if ((dev->pdev->device == 0x5e4c) ||
  382. (dev->pdev->device == 0x5e4f))
  383. dev_priv->num_gb_pipes = 1;
  384. } else {
  385. /* R3xx */
  386. if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300 &&
  387. dev->pdev->device != 0x4144) ||
  388. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350 &&
  389. dev->pdev->device != 0x4148)) {
  390. dev_priv->num_gb_pipes = 2;
  391. } else {
  392. /* RV3xx/R300 AD/R350 AH */
  393. dev_priv->num_gb_pipes = 1;
  394. }
  395. }
  396. DRM_INFO("Num pipes: %d\n", dev_priv->num_gb_pipes);
  397. gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16 /*| R300_SUBPIXEL_1_16*/);
  398. switch (dev_priv->num_gb_pipes) {
  399. case 2: gb_tile_config |= R300_PIPE_COUNT_R300; break;
  400. case 3: gb_tile_config |= R300_PIPE_COUNT_R420_3P; break;
  401. case 4: gb_tile_config |= R300_PIPE_COUNT_R420; break;
  402. default:
  403. case 1: gb_tile_config |= R300_PIPE_COUNT_RV350; break;
  404. }
  405. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV515) {
  406. RADEON_WRITE_PLL(R500_DYN_SCLK_PWMEM_PIPE, (1 | ((gb_pipe_sel >> 8) & 0xf) << 4));
  407. RADEON_WRITE(R300_SU_REG_DEST, ((1 << dev_priv->num_gb_pipes) - 1));
  408. }
  409. RADEON_WRITE(R300_GB_TILE_CONFIG, gb_tile_config);
  410. radeon_do_wait_for_idle(dev_priv);
  411. RADEON_WRITE(R300_DST_PIPE_CONFIG, RADEON_READ(R300_DST_PIPE_CONFIG) | R300_PIPE_AUTO_CONFIG);
  412. RADEON_WRITE(R300_RB2D_DSTCACHE_MODE, (RADEON_READ(R300_RB2D_DSTCACHE_MODE) |
  413. R300_DC_AUTOFLUSH_ENABLE |
  414. R300_DC_DC_DISABLE_IGNORE_PE));
  415. }
  416. /* ================================================================
  417. * CP control, initialization
  418. */
  419. /* Load the microcode for the CP */
  420. static int radeon_cp_init_microcode(drm_radeon_private_t *dev_priv)
  421. {
  422. struct platform_device *pdev;
  423. const char *fw_name = NULL;
  424. int err;
  425. DRM_DEBUG("\n");
  426. pdev = platform_device_register_simple("radeon_cp", 0, NULL, 0);
  427. err = IS_ERR(pdev);
  428. if (err) {
  429. printk(KERN_ERR "radeon_cp: Failed to register firmware\n");
  430. return -EINVAL;
  431. }
  432. if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R100) ||
  433. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV100) ||
  434. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV200) ||
  435. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS100) ||
  436. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS200)) {
  437. DRM_INFO("Loading R100 Microcode\n");
  438. fw_name = FIRMWARE_R100;
  439. } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R200) ||
  440. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV250) ||
  441. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV280) ||
  442. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS300)) {
  443. DRM_INFO("Loading R200 Microcode\n");
  444. fw_name = FIRMWARE_R200;
  445. } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
  446. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350) ||
  447. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV350) ||
  448. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
  449. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
  450. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
  451. DRM_INFO("Loading R300 Microcode\n");
  452. fw_name = FIRMWARE_R300;
  453. } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
  454. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R423) ||
  455. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV410)) {
  456. DRM_INFO("Loading R400 Microcode\n");
  457. fw_name = FIRMWARE_R420;
  458. } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  459. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
  460. DRM_INFO("Loading RS690/RS740 Microcode\n");
  461. fw_name = FIRMWARE_RS690;
  462. } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
  463. DRM_INFO("Loading RS600 Microcode\n");
  464. fw_name = FIRMWARE_RS600;
  465. } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) ||
  466. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R520) ||
  467. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) ||
  468. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R580) ||
  469. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV560) ||
  470. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV570)) {
  471. DRM_INFO("Loading R500 Microcode\n");
  472. fw_name = FIRMWARE_R520;
  473. }
  474. err = request_firmware(&dev_priv->me_fw, fw_name, &pdev->dev);
  475. platform_device_unregister(pdev);
  476. if (err) {
  477. printk(KERN_ERR "radeon_cp: Failed to load firmware \"%s\"\n",
  478. fw_name);
  479. } else if (dev_priv->me_fw->size % 8) {
  480. printk(KERN_ERR
  481. "radeon_cp: Bogus length %zu in firmware \"%s\"\n",
  482. dev_priv->me_fw->size, fw_name);
  483. err = -EINVAL;
  484. release_firmware(dev_priv->me_fw);
  485. dev_priv->me_fw = NULL;
  486. }
  487. return err;
  488. }
  489. static void radeon_cp_load_microcode(drm_radeon_private_t *dev_priv)
  490. {
  491. const __be32 *fw_data;
  492. int i, size;
  493. radeon_do_wait_for_idle(dev_priv);
  494. if (dev_priv->me_fw) {
  495. size = dev_priv->me_fw->size / 4;
  496. fw_data = (const __be32 *)&dev_priv->me_fw->data[0];
  497. RADEON_WRITE(RADEON_CP_ME_RAM_ADDR, 0);
  498. for (i = 0; i < size; i += 2) {
  499. RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
  500. be32_to_cpup(&fw_data[i]));
  501. RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
  502. be32_to_cpup(&fw_data[i + 1]));
  503. }
  504. }
  505. }
  506. /* Flush any pending commands to the CP. This should only be used just
  507. * prior to a wait for idle, as it informs the engine that the command
  508. * stream is ending.
  509. */
  510. static void radeon_do_cp_flush(drm_radeon_private_t * dev_priv)
  511. {
  512. DRM_DEBUG("\n");
  513. #if 0
  514. u32 tmp;
  515. tmp = RADEON_READ(RADEON_CP_RB_WPTR) | (1 << 31);
  516. RADEON_WRITE(RADEON_CP_RB_WPTR, tmp);
  517. #endif
  518. }
  519. /* Wait for the CP to go idle.
  520. */
  521. int radeon_do_cp_idle(drm_radeon_private_t * dev_priv)
  522. {
  523. RING_LOCALS;
  524. DRM_DEBUG("\n");
  525. BEGIN_RING(6);
  526. RADEON_PURGE_CACHE();
  527. RADEON_PURGE_ZCACHE();
  528. RADEON_WAIT_UNTIL_IDLE();
  529. ADVANCE_RING();
  530. COMMIT_RING();
  531. return radeon_do_wait_for_idle(dev_priv);
  532. }
  533. /* Start the Command Processor.
  534. */
  535. static void radeon_do_cp_start(drm_radeon_private_t * dev_priv)
  536. {
  537. RING_LOCALS;
  538. DRM_DEBUG("\n");
  539. radeon_do_wait_for_idle(dev_priv);
  540. RADEON_WRITE(RADEON_CP_CSQ_CNTL, dev_priv->cp_mode);
  541. dev_priv->cp_running = 1;
  542. /* on r420, any DMA from CP to system memory while 2D is active
  543. * can cause a hang. workaround is to queue a CP RESYNC token
  544. */
  545. if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) {
  546. BEGIN_RING(3);
  547. OUT_RING(CP_PACKET0(R300_CP_RESYNC_ADDR, 1));
  548. OUT_RING(5); /* scratch reg 5 */
  549. OUT_RING(0xdeadbeef);
  550. ADVANCE_RING();
  551. COMMIT_RING();
  552. }
  553. BEGIN_RING(8);
  554. /* isync can only be written through cp on r5xx write it here */
  555. OUT_RING(CP_PACKET0(RADEON_ISYNC_CNTL, 0));
  556. OUT_RING(RADEON_ISYNC_ANY2D_IDLE3D |
  557. RADEON_ISYNC_ANY3D_IDLE2D |
  558. RADEON_ISYNC_WAIT_IDLEGUI |
  559. RADEON_ISYNC_CPSCRATCH_IDLEGUI);
  560. RADEON_PURGE_CACHE();
  561. RADEON_PURGE_ZCACHE();
  562. RADEON_WAIT_UNTIL_IDLE();
  563. ADVANCE_RING();
  564. COMMIT_RING();
  565. dev_priv->track_flush |= RADEON_FLUSH_EMITED | RADEON_PURGE_EMITED;
  566. }
  567. /* Reset the Command Processor. This will not flush any pending
  568. * commands, so you must wait for the CP command stream to complete
  569. * before calling this routine.
  570. */
  571. static void radeon_do_cp_reset(drm_radeon_private_t * dev_priv)
  572. {
  573. u32 cur_read_ptr;
  574. DRM_DEBUG("\n");
  575. cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
  576. RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
  577. SET_RING_HEAD(dev_priv, cur_read_ptr);
  578. dev_priv->ring.tail = cur_read_ptr;
  579. }
  580. /* Stop the Command Processor. This will not flush any pending
  581. * commands, so you must flush the command stream and wait for the CP
  582. * to go idle before calling this routine.
  583. */
  584. static void radeon_do_cp_stop(drm_radeon_private_t * dev_priv)
  585. {
  586. RING_LOCALS;
  587. DRM_DEBUG("\n");
  588. /* finish the pending CP_RESYNC token */
  589. if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) {
  590. BEGIN_RING(2);
  591. OUT_RING(CP_PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
  592. OUT_RING(R300_RB3D_DC_FINISH);
  593. ADVANCE_RING();
  594. COMMIT_RING();
  595. radeon_do_wait_for_idle(dev_priv);
  596. }
  597. RADEON_WRITE(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS);
  598. dev_priv->cp_running = 0;
  599. }
  600. /* Reset the engine. This will stop the CP if it is running.
  601. */
  602. static int radeon_do_engine_reset(struct drm_device * dev)
  603. {
  604. drm_radeon_private_t *dev_priv = dev->dev_private;
  605. u32 clock_cntl_index = 0, mclk_cntl = 0, rbbm_soft_reset;
  606. DRM_DEBUG("\n");
  607. radeon_do_pixcache_flush(dev_priv);
  608. if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
  609. /* may need something similar for newer chips */
  610. clock_cntl_index = RADEON_READ(RADEON_CLOCK_CNTL_INDEX);
  611. mclk_cntl = RADEON_READ_PLL(dev, RADEON_MCLK_CNTL);
  612. RADEON_WRITE_PLL(RADEON_MCLK_CNTL, (mclk_cntl |
  613. RADEON_FORCEON_MCLKA |
  614. RADEON_FORCEON_MCLKB |
  615. RADEON_FORCEON_YCLKA |
  616. RADEON_FORCEON_YCLKB |
  617. RADEON_FORCEON_MC |
  618. RADEON_FORCEON_AIC));
  619. }
  620. rbbm_soft_reset = RADEON_READ(RADEON_RBBM_SOFT_RESET);
  621. RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset |
  622. RADEON_SOFT_RESET_CP |
  623. RADEON_SOFT_RESET_HI |
  624. RADEON_SOFT_RESET_SE |
  625. RADEON_SOFT_RESET_RE |
  626. RADEON_SOFT_RESET_PP |
  627. RADEON_SOFT_RESET_E2 |
  628. RADEON_SOFT_RESET_RB));
  629. RADEON_READ(RADEON_RBBM_SOFT_RESET);
  630. RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset &
  631. ~(RADEON_SOFT_RESET_CP |
  632. RADEON_SOFT_RESET_HI |
  633. RADEON_SOFT_RESET_SE |
  634. RADEON_SOFT_RESET_RE |
  635. RADEON_SOFT_RESET_PP |
  636. RADEON_SOFT_RESET_E2 |
  637. RADEON_SOFT_RESET_RB)));
  638. RADEON_READ(RADEON_RBBM_SOFT_RESET);
  639. if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
  640. RADEON_WRITE_PLL(RADEON_MCLK_CNTL, mclk_cntl);
  641. RADEON_WRITE(RADEON_CLOCK_CNTL_INDEX, clock_cntl_index);
  642. RADEON_WRITE(RADEON_RBBM_SOFT_RESET, rbbm_soft_reset);
  643. }
  644. /* setup the raster pipes */
  645. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R300)
  646. radeon_init_pipes(dev);
  647. /* Reset the CP ring */
  648. radeon_do_cp_reset(dev_priv);
  649. /* The CP is no longer running after an engine reset */
  650. dev_priv->cp_running = 0;
  651. /* Reset any pending vertex, indirect buffers */
  652. radeon_freelist_reset(dev);
  653. return 0;
  654. }
  655. static void radeon_cp_init_ring_buffer(struct drm_device * dev,
  656. drm_radeon_private_t *dev_priv,
  657. struct drm_file *file_priv)
  658. {
  659. struct drm_radeon_master_private *master_priv;
  660. u32 ring_start, cur_read_ptr;
  661. /* Initialize the memory controller. With new memory map, the fb location
  662. * is not changed, it should have been properly initialized already. Part
  663. * of the problem is that the code below is bogus, assuming the GART is
  664. * always appended to the fb which is not necessarily the case
  665. */
  666. if (!dev_priv->new_memmap)
  667. radeon_write_fb_location(dev_priv,
  668. ((dev_priv->gart_vm_start - 1) & 0xffff0000)
  669. | (dev_priv->fb_location >> 16));
  670. #if IS_ENABLED(CONFIG_AGP)
  671. if (dev_priv->flags & RADEON_IS_AGP) {
  672. radeon_write_agp_base(dev_priv, dev->agp->base);
  673. radeon_write_agp_location(dev_priv,
  674. (((dev_priv->gart_vm_start - 1 +
  675. dev_priv->gart_size) & 0xffff0000) |
  676. (dev_priv->gart_vm_start >> 16)));
  677. ring_start = (dev_priv->cp_ring->offset
  678. - dev->agp->base
  679. + dev_priv->gart_vm_start);
  680. } else
  681. #endif
  682. ring_start = (dev_priv->cp_ring->offset
  683. - (unsigned long)dev->sg->virtual
  684. + dev_priv->gart_vm_start);
  685. RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
  686. /* Set the write pointer delay */
  687. RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
  688. /* Initialize the ring buffer's read and write pointers */
  689. cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
  690. RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
  691. SET_RING_HEAD(dev_priv, cur_read_ptr);
  692. dev_priv->ring.tail = cur_read_ptr;
  693. #if IS_ENABLED(CONFIG_AGP)
  694. if (dev_priv->flags & RADEON_IS_AGP) {
  695. RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
  696. dev_priv->ring_rptr->offset
  697. - dev->agp->base + dev_priv->gart_vm_start);
  698. } else
  699. #endif
  700. {
  701. RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
  702. dev_priv->ring_rptr->offset
  703. - ((unsigned long) dev->sg->virtual)
  704. + dev_priv->gart_vm_start);
  705. }
  706. /* Set ring buffer size */
  707. #ifdef __BIG_ENDIAN
  708. RADEON_WRITE(RADEON_CP_RB_CNTL,
  709. RADEON_BUF_SWAP_32BIT |
  710. (dev_priv->ring.fetch_size_l2ow << 18) |
  711. (dev_priv->ring.rptr_update_l2qw << 8) |
  712. dev_priv->ring.size_l2qw);
  713. #else
  714. RADEON_WRITE(RADEON_CP_RB_CNTL,
  715. (dev_priv->ring.fetch_size_l2ow << 18) |
  716. (dev_priv->ring.rptr_update_l2qw << 8) |
  717. dev_priv->ring.size_l2qw);
  718. #endif
  719. /* Initialize the scratch register pointer. This will cause
  720. * the scratch register values to be written out to memory
  721. * whenever they are updated.
  722. *
  723. * We simply put this behind the ring read pointer, this works
  724. * with PCI GART as well as (whatever kind of) AGP GART
  725. */
  726. RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
  727. + RADEON_SCRATCH_REG_OFFSET);
  728. RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
  729. radeon_enable_bm(dev_priv);
  730. radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(0), 0);
  731. RADEON_WRITE(RADEON_LAST_FRAME_REG, 0);
  732. radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1), 0);
  733. RADEON_WRITE(RADEON_LAST_DISPATCH_REG, 0);
  734. radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(2), 0);
  735. RADEON_WRITE(RADEON_LAST_CLEAR_REG, 0);
  736. /* reset sarea copies of these */
  737. master_priv = file_priv->master->driver_priv;
  738. if (master_priv->sarea_priv) {
  739. master_priv->sarea_priv->last_frame = 0;
  740. master_priv->sarea_priv->last_dispatch = 0;
  741. master_priv->sarea_priv->last_clear = 0;
  742. }
  743. radeon_do_wait_for_idle(dev_priv);
  744. /* Sync everything up */
  745. RADEON_WRITE(RADEON_ISYNC_CNTL,
  746. (RADEON_ISYNC_ANY2D_IDLE3D |
  747. RADEON_ISYNC_ANY3D_IDLE2D |
  748. RADEON_ISYNC_WAIT_IDLEGUI |
  749. RADEON_ISYNC_CPSCRATCH_IDLEGUI));
  750. }
  751. static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
  752. {
  753. u32 tmp;
  754. /* Start with assuming that writeback doesn't work */
  755. dev_priv->writeback_works = 0;
  756. /* Writeback doesn't seem to work everywhere, test it here and possibly
  757. * enable it if it appears to work
  758. */
  759. radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1), 0);
  760. RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
  761. for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
  762. u32 val;
  763. val = radeon_read_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1));
  764. if (val == 0xdeadbeef)
  765. break;
  766. DRM_UDELAY(1);
  767. }
  768. if (tmp < dev_priv->usec_timeout) {
  769. dev_priv->writeback_works = 1;
  770. DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
  771. } else {
  772. dev_priv->writeback_works = 0;
  773. DRM_INFO("writeback test failed\n");
  774. }
  775. if (radeon_no_wb == 1) {
  776. dev_priv->writeback_works = 0;
  777. DRM_INFO("writeback forced off\n");
  778. }
  779. if (!dev_priv->writeback_works) {
  780. /* Disable writeback to avoid unnecessary bus master transfer */
  781. RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
  782. RADEON_RB_NO_UPDATE);
  783. RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
  784. }
  785. }
  786. /* Enable or disable IGP GART on the chip */
  787. static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
  788. {
  789. u32 temp;
  790. if (on) {
  791. DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
  792. dev_priv->gart_vm_start,
  793. (long)dev_priv->gart_info.bus_addr,
  794. dev_priv->gart_size);
  795. temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
  796. if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  797. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
  798. IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
  799. RS690_BLOCK_GFX_D3_EN));
  800. else
  801. IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
  802. IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
  803. RS480_VA_SIZE_32MB));
  804. temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
  805. IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
  806. RS480_TLB_ENABLE |
  807. RS480_GTW_LAC_EN |
  808. RS480_1LEVEL_GART));
  809. temp = dev_priv->gart_info.bus_addr & 0xfffff000;
  810. temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
  811. IGP_WRITE_MCIND(RS480_GART_BASE, temp);
  812. temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
  813. IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
  814. RS480_REQ_TYPE_SNOOP_DIS));
  815. radeon_write_agp_base(dev_priv, dev_priv->gart_vm_start);
  816. dev_priv->gart_size = 32*1024*1024;
  817. temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
  818. 0xffff0000) | (dev_priv->gart_vm_start >> 16));
  819. radeon_write_agp_location(dev_priv, temp);
  820. temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
  821. IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
  822. RS480_VA_SIZE_32MB));
  823. do {
  824. temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
  825. if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
  826. break;
  827. DRM_UDELAY(1);
  828. } while (1);
  829. IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
  830. RS480_GART_CACHE_INVALIDATE);
  831. do {
  832. temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
  833. if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
  834. break;
  835. DRM_UDELAY(1);
  836. } while (1);
  837. IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
  838. } else {
  839. IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
  840. }
  841. }
  842. /* Enable or disable IGP GART on the chip */
  843. static void rs600_set_igpgart(drm_radeon_private_t *dev_priv, int on)
  844. {
  845. u32 temp;
  846. int i;
  847. if (on) {
  848. DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
  849. dev_priv->gart_vm_start,
  850. (long)dev_priv->gart_info.bus_addr,
  851. dev_priv->gart_size);
  852. IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, (RS600_EFFECTIVE_L2_CACHE_SIZE(6) |
  853. RS600_EFFECTIVE_L2_QUEUE_SIZE(6)));
  854. for (i = 0; i < 19; i++)
  855. IGP_WRITE_MCIND(RS600_MC_PT0_CLIENT0_CNTL + i,
  856. (RS600_ENABLE_TRANSLATION_MODE_OVERRIDE |
  857. RS600_SYSTEM_ACCESS_MODE_IN_SYS |
  858. RS600_SYSTEM_APERTURE_UNMAPPED_ACCESS_PASSTHROUGH |
  859. RS600_EFFECTIVE_L1_CACHE_SIZE(3) |
  860. RS600_ENABLE_FRAGMENT_PROCESSING |
  861. RS600_EFFECTIVE_L1_QUEUE_SIZE(3)));
  862. IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_CNTL, (RS600_ENABLE_PAGE_TABLE |
  863. RS600_PAGE_TABLE_TYPE_FLAT));
  864. /* disable all other contexts */
  865. for (i = 1; i < 8; i++)
  866. IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_CNTL + i, 0);
  867. /* setup the page table aperture */
  868. IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_BASE_ADDR,
  869. dev_priv->gart_info.bus_addr);
  870. IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_START_ADDR,
  871. dev_priv->gart_vm_start);
  872. IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_END_ADDR,
  873. (dev_priv->gart_vm_start + dev_priv->gart_size - 1));
  874. IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_DEFAULT_READ_ADDR, 0);
  875. /* setup the system aperture */
  876. IGP_WRITE_MCIND(RS600_MC_PT0_SYSTEM_APERTURE_LOW_ADDR,
  877. dev_priv->gart_vm_start);
  878. IGP_WRITE_MCIND(RS600_MC_PT0_SYSTEM_APERTURE_HIGH_ADDR,
  879. (dev_priv->gart_vm_start + dev_priv->gart_size - 1));
  880. /* enable page tables */
  881. temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
  882. IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, (temp | RS600_ENABLE_PT));
  883. temp = IGP_READ_MCIND(dev_priv, RS600_MC_CNTL1);
  884. IGP_WRITE_MCIND(RS600_MC_CNTL1, (temp | RS600_ENABLE_PAGE_TABLES));
  885. /* invalidate the cache */
  886. temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
  887. temp &= ~(RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE);
  888. IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
  889. temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
  890. temp |= RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE;
  891. IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
  892. temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
  893. temp &= ~(RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE);
  894. IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
  895. temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
  896. } else {
  897. IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, 0);
  898. temp = IGP_READ_MCIND(dev_priv, RS600_MC_CNTL1);
  899. temp &= ~RS600_ENABLE_PAGE_TABLES;
  900. IGP_WRITE_MCIND(RS600_MC_CNTL1, temp);
  901. }
  902. }
  903. static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
  904. {
  905. u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
  906. if (on) {
  907. DRM_DEBUG("programming pcie %08X %08lX %08X\n",
  908. dev_priv->gart_vm_start,
  909. (long)dev_priv->gart_info.bus_addr,
  910. dev_priv->gart_size);
  911. RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
  912. dev_priv->gart_vm_start);
  913. RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
  914. dev_priv->gart_info.bus_addr);
  915. RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
  916. dev_priv->gart_vm_start);
  917. RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
  918. dev_priv->gart_vm_start +
  919. dev_priv->gart_size - 1);
  920. radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
  921. RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
  922. RADEON_PCIE_TX_GART_EN);
  923. } else {
  924. RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
  925. tmp & ~RADEON_PCIE_TX_GART_EN);
  926. }
  927. }
  928. /* Enable or disable PCI GART on the chip */
  929. static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
  930. {
  931. u32 tmp;
  932. if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
  933. ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740) ||
  934. (dev_priv->flags & RADEON_IS_IGPGART)) {
  935. radeon_set_igpgart(dev_priv, on);
  936. return;
  937. }
  938. if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
  939. rs600_set_igpgart(dev_priv, on);
  940. return;
  941. }
  942. if (dev_priv->flags & RADEON_IS_PCIE) {
  943. radeon_set_pciegart(dev_priv, on);
  944. return;
  945. }
  946. tmp = RADEON_READ(RADEON_AIC_CNTL);
  947. if (on) {
  948. RADEON_WRITE(RADEON_AIC_CNTL,
  949. tmp | RADEON_PCIGART_TRANSLATE_EN);
  950. /* set PCI GART page-table base address
  951. */
  952. RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
  953. /* set address range for PCI address translate
  954. */
  955. RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
  956. RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
  957. + dev_priv->gart_size - 1);
  958. /* Turn off AGP aperture -- is this required for PCI GART?
  959. */
  960. radeon_write_agp_location(dev_priv, 0xffffffc0);
  961. RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
  962. } else {
  963. RADEON_WRITE(RADEON_AIC_CNTL,
  964. tmp & ~RADEON_PCIGART_TRANSLATE_EN);
  965. }
  966. }
  967. static int radeon_setup_pcigart_surface(drm_radeon_private_t *dev_priv)
  968. {
  969. struct drm_ati_pcigart_info *gart_info = &dev_priv->gart_info;
  970. struct radeon_virt_surface *vp;
  971. int i;
  972. for (i = 0; i < RADEON_MAX_SURFACES * 2; i++) {
  973. if (!dev_priv->virt_surfaces[i].file_priv ||
  974. dev_priv->virt_surfaces[i].file_priv == PCIGART_FILE_PRIV)
  975. break;
  976. }
  977. if (i >= 2 * RADEON_MAX_SURFACES)
  978. return -ENOMEM;
  979. vp = &dev_priv->virt_surfaces[i];
  980. for (i = 0; i < RADEON_MAX_SURFACES; i++) {
  981. struct radeon_surface *sp = &dev_priv->surfaces[i];
  982. if (sp->refcount)
  983. continue;
  984. vp->surface_index = i;
  985. vp->lower = gart_info->bus_addr;
  986. vp->upper = vp->lower + gart_info->table_size;
  987. vp->flags = 0;
  988. vp->file_priv = PCIGART_FILE_PRIV;
  989. sp->refcount = 1;
  990. sp->lower = vp->lower;
  991. sp->upper = vp->upper;
  992. sp->flags = 0;
  993. RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, sp->flags);
  994. RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * i, sp->lower);
  995. RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * i, sp->upper);
  996. return 0;
  997. }
  998. return -ENOMEM;
  999. }
  1000. static int radeon_do_init_cp(struct drm_device *dev, drm_radeon_init_t *init,
  1001. struct drm_file *file_priv)
  1002. {
  1003. drm_radeon_private_t *dev_priv = dev->dev_private;
  1004. struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
  1005. DRM_DEBUG("\n");
  1006. /* if we require new memory map but we don't have it fail */
  1007. if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
  1008. DRM_ERROR("Cannot initialise DRM on this card\nThis card requires a new X.org DDX for 3D\n");
  1009. radeon_do_cleanup_cp(dev);
  1010. return -EINVAL;
  1011. }
  1012. if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
  1013. DRM_DEBUG("Forcing AGP card to PCI mode\n");
  1014. dev_priv->flags &= ~RADEON_IS_AGP;
  1015. } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
  1016. && !init->is_pci) {
  1017. DRM_DEBUG("Restoring AGP flag\n");
  1018. dev_priv->flags |= RADEON_IS_AGP;
  1019. }
  1020. if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
  1021. DRM_ERROR("PCI GART memory not allocated!\n");
  1022. radeon_do_cleanup_cp(dev);
  1023. return -EINVAL;
  1024. }
  1025. dev_priv->usec_timeout = init->usec_timeout;
  1026. if (dev_priv->usec_timeout < 1 ||
  1027. dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
  1028. DRM_DEBUG("TIMEOUT problem!\n");
  1029. radeon_do_cleanup_cp(dev);
  1030. return -EINVAL;
  1031. }
  1032. /* Enable vblank on CRTC1 for older X servers
  1033. */
  1034. dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
  1035. switch(init->func) {
  1036. case RADEON_INIT_R200_CP:
  1037. dev_priv->microcode_version = UCODE_R200;
  1038. break;
  1039. case RADEON_INIT_R300_CP:
  1040. dev_priv->microcode_version = UCODE_R300;
  1041. break;
  1042. default:
  1043. dev_priv->microcode_version = UCODE_R100;
  1044. }
  1045. dev_priv->do_boxes = 0;
  1046. dev_priv->cp_mode = init->cp_mode;
  1047. /* We don't support anything other than bus-mastering ring mode,
  1048. * but the ring can be in either AGP or PCI space for the ring
  1049. * read pointer.
  1050. */
  1051. if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
  1052. (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
  1053. DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
  1054. radeon_do_cleanup_cp(dev);
  1055. return -EINVAL;
  1056. }
  1057. switch (init->fb_bpp) {
  1058. case 16:
  1059. dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
  1060. break;
  1061. case 32:
  1062. default:
  1063. dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
  1064. break;
  1065. }
  1066. dev_priv->front_offset = init->front_offset;
  1067. dev_priv->front_pitch = init->front_pitch;
  1068. dev_priv->back_offset = init->back_offset;
  1069. dev_priv->back_pitch = init->back_pitch;
  1070. switch (init->depth_bpp) {
  1071. case 16:
  1072. dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
  1073. break;
  1074. case 32:
  1075. default:
  1076. dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
  1077. break;
  1078. }
  1079. dev_priv->depth_offset = init->depth_offset;
  1080. dev_priv->depth_pitch = init->depth_pitch;
  1081. /* Hardware state for depth clears. Remove this if/when we no
  1082. * longer clear the depth buffer with a 3D rectangle. Hard-code
  1083. * all values to prevent unwanted 3D state from slipping through
  1084. * and screwing with the clear operation.
  1085. */
  1086. dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
  1087. (dev_priv->color_fmt << 10) |
  1088. (dev_priv->microcode_version ==
  1089. UCODE_R100 ? RADEON_ZBLOCK16 : 0));
  1090. dev_priv->depth_clear.rb3d_zstencilcntl =
  1091. (dev_priv->depth_fmt |
  1092. RADEON_Z_TEST_ALWAYS |
  1093. RADEON_STENCIL_TEST_ALWAYS |
  1094. RADEON_STENCIL_S_FAIL_REPLACE |
  1095. RADEON_STENCIL_ZPASS_REPLACE |
  1096. RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
  1097. dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
  1098. RADEON_BFACE_SOLID |
  1099. RADEON_FFACE_SOLID |
  1100. RADEON_FLAT_SHADE_VTX_LAST |
  1101. RADEON_DIFFUSE_SHADE_FLAT |
  1102. RADEON_ALPHA_SHADE_FLAT |
  1103. RADEON_SPECULAR_SHADE_FLAT |
  1104. RADEON_FOG_SHADE_FLAT |
  1105. RADEON_VTX_PIX_CENTER_OGL |
  1106. RADEON_ROUND_MODE_TRUNC |
  1107. RADEON_ROUND_PREC_8TH_PIX);
  1108. dev_priv->ring_offset = init->ring_offset;
  1109. dev_priv->ring_rptr_offset = init->ring_rptr_offset;
  1110. dev_priv->buffers_offset = init->buffers_offset;
  1111. dev_priv->gart_textures_offset = init->gart_textures_offset;
  1112. master_priv->sarea = drm_legacy_getsarea(dev);
  1113. if (!master_priv->sarea) {
  1114. DRM_ERROR("could not find sarea!\n");
  1115. radeon_do_cleanup_cp(dev);
  1116. return -EINVAL;
  1117. }
  1118. dev_priv->cp_ring = drm_legacy_findmap(dev, init->ring_offset);
  1119. if (!dev_priv->cp_ring) {
  1120. DRM_ERROR("could not find cp ring region!\n");
  1121. radeon_do_cleanup_cp(dev);
  1122. return -EINVAL;
  1123. }
  1124. dev_priv->ring_rptr = drm_legacy_findmap(dev, init->ring_rptr_offset);
  1125. if (!dev_priv->ring_rptr) {
  1126. DRM_ERROR("could not find ring read pointer!\n");
  1127. radeon_do_cleanup_cp(dev);
  1128. return -EINVAL;
  1129. }
  1130. dev->agp_buffer_token = init->buffers_offset;
  1131. dev->agp_buffer_map = drm_legacy_findmap(dev, init->buffers_offset);
  1132. if (!dev->agp_buffer_map) {
  1133. DRM_ERROR("could not find dma buffer region!\n");
  1134. radeon_do_cleanup_cp(dev);
  1135. return -EINVAL;
  1136. }
  1137. if (init->gart_textures_offset) {
  1138. dev_priv->gart_textures =
  1139. drm_legacy_findmap(dev, init->gart_textures_offset);
  1140. if (!dev_priv->gart_textures) {
  1141. DRM_ERROR("could not find GART texture region!\n");
  1142. radeon_do_cleanup_cp(dev);
  1143. return -EINVAL;
  1144. }
  1145. }
  1146. #if IS_ENABLED(CONFIG_AGP)
  1147. if (dev_priv->flags & RADEON_IS_AGP) {
  1148. drm_legacy_ioremap_wc(dev_priv->cp_ring, dev);
  1149. drm_legacy_ioremap_wc(dev_priv->ring_rptr, dev);
  1150. drm_legacy_ioremap_wc(dev->agp_buffer_map, dev);
  1151. if (!dev_priv->cp_ring->handle ||
  1152. !dev_priv->ring_rptr->handle ||
  1153. !dev->agp_buffer_map->handle) {
  1154. DRM_ERROR("could not find ioremap agp regions!\n");
  1155. radeon_do_cleanup_cp(dev);
  1156. return -EINVAL;
  1157. }
  1158. } else
  1159. #endif
  1160. {
  1161. dev_priv->cp_ring->handle =
  1162. (void *)(unsigned long)dev_priv->cp_ring->offset;
  1163. dev_priv->ring_rptr->handle =
  1164. (void *)(unsigned long)dev_priv->ring_rptr->offset;
  1165. dev->agp_buffer_map->handle =
  1166. (void *)(unsigned long)dev->agp_buffer_map->offset;
  1167. DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
  1168. dev_priv->cp_ring->handle);
  1169. DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
  1170. dev_priv->ring_rptr->handle);
  1171. DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
  1172. dev->agp_buffer_map->handle);
  1173. }
  1174. dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
  1175. dev_priv->fb_size =
  1176. ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
  1177. - dev_priv->fb_location;
  1178. dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
  1179. ((dev_priv->front_offset
  1180. + dev_priv->fb_location) >> 10));
  1181. dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
  1182. ((dev_priv->back_offset
  1183. + dev_priv->fb_location) >> 10));
  1184. dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
  1185. ((dev_priv->depth_offset
  1186. + dev_priv->fb_location) >> 10));
  1187. dev_priv->gart_size = init->gart_size;
  1188. /* New let's set the memory map ... */
  1189. if (dev_priv->new_memmap) {
  1190. u32 base = 0;
  1191. DRM_INFO("Setting GART location based on new memory map\n");
  1192. /* If using AGP, try to locate the AGP aperture at the same
  1193. * location in the card and on the bus, though we have to
  1194. * align it down.
  1195. */
  1196. #if IS_ENABLED(CONFIG_AGP)
  1197. if (dev_priv->flags & RADEON_IS_AGP) {
  1198. base = dev->agp->base;
  1199. /* Check if valid */
  1200. if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
  1201. base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
  1202. DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
  1203. dev->agp->base);
  1204. base = 0;
  1205. }
  1206. }
  1207. #endif
  1208. /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
  1209. if (base == 0) {
  1210. base = dev_priv->fb_location + dev_priv->fb_size;
  1211. if (base < dev_priv->fb_location ||
  1212. ((base + dev_priv->gart_size) & 0xfffffffful) < base)
  1213. base = dev_priv->fb_location
  1214. - dev_priv->gart_size;
  1215. }
  1216. dev_priv->gart_vm_start = base & 0xffc00000u;
  1217. if (dev_priv->gart_vm_start != base)
  1218. DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
  1219. base, dev_priv->gart_vm_start);
  1220. } else {
  1221. DRM_INFO("Setting GART location based on old memory map\n");
  1222. dev_priv->gart_vm_start = dev_priv->fb_location +
  1223. RADEON_READ(RADEON_CONFIG_APER_SIZE);
  1224. }
  1225. #if IS_ENABLED(CONFIG_AGP)
  1226. if (dev_priv->flags & RADEON_IS_AGP)
  1227. dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
  1228. - dev->agp->base
  1229. + dev_priv->gart_vm_start);
  1230. else
  1231. #endif
  1232. dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
  1233. - (unsigned long)dev->sg->virtual
  1234. + dev_priv->gart_vm_start);
  1235. DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
  1236. DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
  1237. DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
  1238. dev_priv->gart_buffers_offset);
  1239. dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
  1240. dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
  1241. + init->ring_size / sizeof(u32));
  1242. dev_priv->ring.size = init->ring_size;
  1243. dev_priv->ring.size_l2qw = order_base_2(init->ring_size / 8);
  1244. dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
  1245. dev_priv->ring.rptr_update_l2qw = order_base_2( /* init->rptr_update */ 4096 / 8);
  1246. dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
  1247. dev_priv->ring.fetch_size_l2ow = order_base_2( /* init->fetch_size */ 32 / 16);
  1248. dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
  1249. dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
  1250. #if IS_ENABLED(CONFIG_AGP)
  1251. if (dev_priv->flags & RADEON_IS_AGP) {
  1252. /* Turn off PCI GART */
  1253. radeon_set_pcigart(dev_priv, 0);
  1254. } else
  1255. #endif
  1256. {
  1257. u32 sctrl;
  1258. int ret;
  1259. dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
  1260. /* if we have an offset set from userspace */
  1261. if (dev_priv->pcigart_offset_set) {
  1262. dev_priv->gart_info.bus_addr =
  1263. (resource_size_t)dev_priv->pcigart_offset + dev_priv->fb_location;
  1264. dev_priv->gart_info.mapping.offset =
  1265. dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
  1266. dev_priv->gart_info.mapping.size =
  1267. dev_priv->gart_info.table_size;
  1268. drm_legacy_ioremap_wc(&dev_priv->gart_info.mapping, dev);
  1269. dev_priv->gart_info.addr =
  1270. dev_priv->gart_info.mapping.handle;
  1271. if (dev_priv->flags & RADEON_IS_PCIE)
  1272. dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
  1273. else
  1274. dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
  1275. dev_priv->gart_info.gart_table_location =
  1276. DRM_ATI_GART_FB;
  1277. DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
  1278. dev_priv->gart_info.addr,
  1279. dev_priv->pcigart_offset);
  1280. } else {
  1281. if (dev_priv->flags & RADEON_IS_IGPGART)
  1282. dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
  1283. else
  1284. dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
  1285. dev_priv->gart_info.gart_table_location =
  1286. DRM_ATI_GART_MAIN;
  1287. dev_priv->gart_info.addr = NULL;
  1288. dev_priv->gart_info.bus_addr = 0;
  1289. if (dev_priv->flags & RADEON_IS_PCIE) {
  1290. DRM_ERROR
  1291. ("Cannot use PCI Express without GART in FB memory\n");
  1292. radeon_do_cleanup_cp(dev);
  1293. return -EINVAL;
  1294. }
  1295. }
  1296. sctrl = RADEON_READ(RADEON_SURFACE_CNTL);
  1297. RADEON_WRITE(RADEON_SURFACE_CNTL, 0);
  1298. if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
  1299. ret = r600_page_table_init(dev);
  1300. else
  1301. ret = drm_ati_pcigart_init(dev, &dev_priv->gart_info);
  1302. RADEON_WRITE(RADEON_SURFACE_CNTL, sctrl);
  1303. if (!ret) {
  1304. DRM_ERROR("failed to init PCI GART!\n");
  1305. radeon_do_cleanup_cp(dev);
  1306. return -ENOMEM;
  1307. }
  1308. ret = radeon_setup_pcigart_surface(dev_priv);
  1309. if (ret) {
  1310. DRM_ERROR("failed to setup GART surface!\n");
  1311. if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
  1312. r600_page_table_cleanup(dev, &dev_priv->gart_info);
  1313. else
  1314. drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info);
  1315. radeon_do_cleanup_cp(dev);
  1316. return ret;
  1317. }
  1318. /* Turn on PCI GART */
  1319. radeon_set_pcigart(dev_priv, 1);
  1320. }
  1321. if (!dev_priv->me_fw) {
  1322. int err = radeon_cp_init_microcode(dev_priv);
  1323. if (err) {
  1324. DRM_ERROR("Failed to load firmware!\n");
  1325. radeon_do_cleanup_cp(dev);
  1326. return err;
  1327. }
  1328. }
  1329. radeon_cp_load_microcode(dev_priv);
  1330. radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
  1331. dev_priv->last_buf = 0;
  1332. radeon_do_engine_reset(dev);
  1333. radeon_test_writeback(dev_priv);
  1334. return 0;
  1335. }
  1336. static int radeon_do_cleanup_cp(struct drm_device * dev)
  1337. {
  1338. drm_radeon_private_t *dev_priv = dev->dev_private;
  1339. DRM_DEBUG("\n");
  1340. /* Make sure interrupts are disabled here because the uninstall ioctl
  1341. * may not have been called from userspace and after dev_private
  1342. * is freed, it's too late.
  1343. */
  1344. if (dev->irq_enabled)
  1345. drm_irq_uninstall(dev);
  1346. #if IS_ENABLED(CONFIG_AGP)
  1347. if (dev_priv->flags & RADEON_IS_AGP) {
  1348. if (dev_priv->cp_ring != NULL) {
  1349. drm_legacy_ioremapfree(dev_priv->cp_ring, dev);
  1350. dev_priv->cp_ring = NULL;
  1351. }
  1352. if (dev_priv->ring_rptr != NULL) {
  1353. drm_legacy_ioremapfree(dev_priv->ring_rptr, dev);
  1354. dev_priv->ring_rptr = NULL;
  1355. }
  1356. if (dev->agp_buffer_map != NULL) {
  1357. drm_legacy_ioremapfree(dev->agp_buffer_map, dev);
  1358. dev->agp_buffer_map = NULL;
  1359. }
  1360. } else
  1361. #endif
  1362. {
  1363. if (dev_priv->gart_info.bus_addr) {
  1364. /* Turn off PCI GART */
  1365. radeon_set_pcigart(dev_priv, 0);
  1366. if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
  1367. r600_page_table_cleanup(dev, &dev_priv->gart_info);
  1368. else {
  1369. if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
  1370. DRM_ERROR("failed to cleanup PCI GART!\n");
  1371. }
  1372. }
  1373. if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
  1374. {
  1375. drm_legacy_ioremapfree(&dev_priv->gart_info.mapping, dev);
  1376. dev_priv->gart_info.addr = NULL;
  1377. }
  1378. }
  1379. /* only clear to the start of flags */
  1380. memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
  1381. return 0;
  1382. }
  1383. /* This code will reinit the Radeon CP hardware after a resume from disc.
  1384. * AFAIK, it would be very difficult to pickle the state at suspend time, so
  1385. * here we make sure that all Radeon hardware initialisation is re-done without
  1386. * affecting running applications.
  1387. *
  1388. * Charl P. Botha <http://cpbotha.net>
  1389. */
  1390. static int radeon_do_resume_cp(struct drm_device *dev, struct drm_file *file_priv)
  1391. {
  1392. drm_radeon_private_t *dev_priv = dev->dev_private;
  1393. if (!dev_priv) {
  1394. DRM_ERROR("Called with no initialization\n");
  1395. return -EINVAL;
  1396. }
  1397. DRM_DEBUG("Starting radeon_do_resume_cp()\n");
  1398. #if IS_ENABLED(CONFIG_AGP)
  1399. if (dev_priv->flags & RADEON_IS_AGP) {
  1400. /* Turn off PCI GART */
  1401. radeon_set_pcigart(dev_priv, 0);
  1402. } else
  1403. #endif
  1404. {
  1405. /* Turn on PCI GART */
  1406. radeon_set_pcigart(dev_priv, 1);
  1407. }
  1408. radeon_cp_load_microcode(dev_priv);
  1409. radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
  1410. dev_priv->have_z_offset = 0;
  1411. radeon_do_engine_reset(dev);
  1412. radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
  1413. DRM_DEBUG("radeon_do_resume_cp() complete\n");
  1414. return 0;
  1415. }
  1416. int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1417. {
  1418. drm_radeon_private_t *dev_priv = dev->dev_private;
  1419. drm_radeon_init_t *init = data;
  1420. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1421. if (init->func == RADEON_INIT_R300_CP)
  1422. r300_init_reg_flags(dev);
  1423. switch (init->func) {
  1424. case RADEON_INIT_CP:
  1425. case RADEON_INIT_R200_CP:
  1426. case RADEON_INIT_R300_CP:
  1427. return radeon_do_init_cp(dev, init, file_priv);
  1428. case RADEON_INIT_R600_CP:
  1429. return r600_do_init_cp(dev, init, file_priv);
  1430. case RADEON_CLEANUP_CP:
  1431. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1432. return r600_do_cleanup_cp(dev);
  1433. else
  1434. return radeon_do_cleanup_cp(dev);
  1435. }
  1436. return -EINVAL;
  1437. }
  1438. int radeon_cp_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1439. {
  1440. drm_radeon_private_t *dev_priv = dev->dev_private;
  1441. DRM_DEBUG("\n");
  1442. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1443. if (dev_priv->cp_running) {
  1444. DRM_DEBUG("while CP running\n");
  1445. return 0;
  1446. }
  1447. if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
  1448. DRM_DEBUG("called with bogus CP mode (%d)\n",
  1449. dev_priv->cp_mode);
  1450. return 0;
  1451. }
  1452. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1453. r600_do_cp_start(dev_priv);
  1454. else
  1455. radeon_do_cp_start(dev_priv);
  1456. return 0;
  1457. }
  1458. /* Stop the CP. The engine must have been idled before calling this
  1459. * routine.
  1460. */
  1461. int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1462. {
  1463. drm_radeon_private_t *dev_priv = dev->dev_private;
  1464. drm_radeon_cp_stop_t *stop = data;
  1465. int ret;
  1466. DRM_DEBUG("\n");
  1467. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1468. if (!dev_priv->cp_running)
  1469. return 0;
  1470. /* Flush any pending CP commands. This ensures any outstanding
  1471. * commands are exectuted by the engine before we turn it off.
  1472. */
  1473. if (stop->flush) {
  1474. radeon_do_cp_flush(dev_priv);
  1475. }
  1476. /* If we fail to make the engine go idle, we return an error
  1477. * code so that the DRM ioctl wrapper can try again.
  1478. */
  1479. if (stop->idle) {
  1480. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1481. ret = r600_do_cp_idle(dev_priv);
  1482. else
  1483. ret = radeon_do_cp_idle(dev_priv);
  1484. if (ret)
  1485. return ret;
  1486. }
  1487. /* Finally, we can turn off the CP. If the engine isn't idle,
  1488. * we will get some dropped triangles as they won't be fully
  1489. * rendered before the CP is shut down.
  1490. */
  1491. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1492. r600_do_cp_stop(dev_priv);
  1493. else
  1494. radeon_do_cp_stop(dev_priv);
  1495. /* Reset the engine */
  1496. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1497. r600_do_engine_reset(dev);
  1498. else
  1499. radeon_do_engine_reset(dev);
  1500. return 0;
  1501. }
  1502. void radeon_do_release(struct drm_device * dev)
  1503. {
  1504. drm_radeon_private_t *dev_priv = dev->dev_private;
  1505. int i, ret;
  1506. if (dev_priv) {
  1507. if (dev_priv->cp_running) {
  1508. /* Stop the cp */
  1509. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
  1510. while ((ret = r600_do_cp_idle(dev_priv)) != 0) {
  1511. DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
  1512. #ifdef __linux__
  1513. schedule();
  1514. #else
  1515. tsleep(&ret, PZERO, "rdnrel", 1);
  1516. #endif
  1517. }
  1518. } else {
  1519. while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
  1520. DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
  1521. #ifdef __linux__
  1522. schedule();
  1523. #else
  1524. tsleep(&ret, PZERO, "rdnrel", 1);
  1525. #endif
  1526. }
  1527. }
  1528. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
  1529. r600_do_cp_stop(dev_priv);
  1530. r600_do_engine_reset(dev);
  1531. } else {
  1532. radeon_do_cp_stop(dev_priv);
  1533. radeon_do_engine_reset(dev);
  1534. }
  1535. }
  1536. if ((dev_priv->flags & RADEON_FAMILY_MASK) < CHIP_R600) {
  1537. /* Disable *all* interrupts */
  1538. if (dev_priv->mmio) /* remove this after permanent addmaps */
  1539. RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
  1540. if (dev_priv->mmio) { /* remove all surfaces */
  1541. for (i = 0; i < RADEON_MAX_SURFACES; i++) {
  1542. RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
  1543. RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
  1544. 16 * i, 0);
  1545. RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
  1546. 16 * i, 0);
  1547. }
  1548. }
  1549. }
  1550. /* Free memory heap structures */
  1551. radeon_mem_takedown(&(dev_priv->gart_heap));
  1552. radeon_mem_takedown(&(dev_priv->fb_heap));
  1553. /* deallocate kernel resources */
  1554. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1555. r600_do_cleanup_cp(dev);
  1556. else
  1557. radeon_do_cleanup_cp(dev);
  1558. release_firmware(dev_priv->me_fw);
  1559. dev_priv->me_fw = NULL;
  1560. release_firmware(dev_priv->pfp_fw);
  1561. dev_priv->pfp_fw = NULL;
  1562. }
  1563. }
  1564. /* Just reset the CP ring. Called as part of an X Server engine reset.
  1565. */
  1566. int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1567. {
  1568. drm_radeon_private_t *dev_priv = dev->dev_private;
  1569. DRM_DEBUG("\n");
  1570. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1571. if (!dev_priv) {
  1572. DRM_DEBUG("called before init done\n");
  1573. return -EINVAL;
  1574. }
  1575. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1576. r600_do_cp_reset(dev_priv);
  1577. else
  1578. radeon_do_cp_reset(dev_priv);
  1579. /* The CP is no longer running after an engine reset */
  1580. dev_priv->cp_running = 0;
  1581. return 0;
  1582. }
  1583. int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1584. {
  1585. drm_radeon_private_t *dev_priv = dev->dev_private;
  1586. DRM_DEBUG("\n");
  1587. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1588. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1589. return r600_do_cp_idle(dev_priv);
  1590. else
  1591. return radeon_do_cp_idle(dev_priv);
  1592. }
  1593. /* Added by Charl P. Botha to call radeon_do_resume_cp().
  1594. */
  1595. int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1596. {
  1597. drm_radeon_private_t *dev_priv = dev->dev_private;
  1598. DRM_DEBUG("\n");
  1599. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1600. return r600_do_resume_cp(dev, file_priv);
  1601. else
  1602. return radeon_do_resume_cp(dev, file_priv);
  1603. }
  1604. int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1605. {
  1606. drm_radeon_private_t *dev_priv = dev->dev_private;
  1607. DRM_DEBUG("\n");
  1608. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1609. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
  1610. return r600_do_engine_reset(dev);
  1611. else
  1612. return radeon_do_engine_reset(dev);
  1613. }
  1614. /* ================================================================
  1615. * Fullscreen mode
  1616. */
  1617. /* KW: Deprecated to say the least:
  1618. */
  1619. int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1620. {
  1621. return 0;
  1622. }
  1623. /* ================================================================
  1624. * Freelist management
  1625. */
  1626. /* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
  1627. * bufs until freelist code is used. Note this hides a problem with
  1628. * the scratch register * (used to keep track of last buffer
  1629. * completed) being written to before * the last buffer has actually
  1630. * completed rendering.
  1631. *
  1632. * KW: It's also a good way to find free buffers quickly.
  1633. *
  1634. * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
  1635. * sleep. However, bugs in older versions of radeon_accel.c mean that
  1636. * we essentially have to do this, else old clients will break.
  1637. *
  1638. * However, it does leave open a potential deadlock where all the
  1639. * buffers are held by other clients, which can't release them because
  1640. * they can't get the lock.
  1641. */
  1642. struct drm_buf *radeon_freelist_get(struct drm_device * dev)
  1643. {
  1644. struct drm_device_dma *dma = dev->dma;
  1645. drm_radeon_private_t *dev_priv = dev->dev_private;
  1646. drm_radeon_buf_priv_t *buf_priv;
  1647. struct drm_buf *buf;
  1648. int i, t;
  1649. int start;
  1650. if (++dev_priv->last_buf >= dma->buf_count)
  1651. dev_priv->last_buf = 0;
  1652. start = dev_priv->last_buf;
  1653. for (t = 0; t < dev_priv->usec_timeout; t++) {
  1654. u32 done_age = GET_SCRATCH(dev_priv, 1);
  1655. DRM_DEBUG("done_age = %d\n", done_age);
  1656. for (i = 0; i < dma->buf_count; i++) {
  1657. buf = dma->buflist[start];
  1658. buf_priv = buf->dev_private;
  1659. if (buf->file_priv == NULL || (buf->pending &&
  1660. buf_priv->age <=
  1661. done_age)) {
  1662. dev_priv->stats.requested_bufs++;
  1663. buf->pending = 0;
  1664. return buf;
  1665. }
  1666. if (++start >= dma->buf_count)
  1667. start = 0;
  1668. }
  1669. if (t) {
  1670. DRM_UDELAY(1);
  1671. dev_priv->stats.freelist_loops++;
  1672. }
  1673. }
  1674. return NULL;
  1675. }
  1676. void radeon_freelist_reset(struct drm_device * dev)
  1677. {
  1678. struct drm_device_dma *dma = dev->dma;
  1679. drm_radeon_private_t *dev_priv = dev->dev_private;
  1680. int i;
  1681. dev_priv->last_buf = 0;
  1682. for (i = 0; i < dma->buf_count; i++) {
  1683. struct drm_buf *buf = dma->buflist[i];
  1684. drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
  1685. buf_priv->age = 0;
  1686. }
  1687. }
  1688. /* ================================================================
  1689. * CP command submission
  1690. */
  1691. int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
  1692. {
  1693. drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
  1694. int i;
  1695. u32 last_head = GET_RING_HEAD(dev_priv);
  1696. for (i = 0; i < dev_priv->usec_timeout; i++) {
  1697. u32 head = GET_RING_HEAD(dev_priv);
  1698. ring->space = (head - ring->tail) * sizeof(u32);
  1699. if (ring->space <= 0)
  1700. ring->space += ring->size;
  1701. if (ring->space > n)
  1702. return 0;
  1703. dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
  1704. if (head != last_head)
  1705. i = 0;
  1706. last_head = head;
  1707. DRM_UDELAY(1);
  1708. }
  1709. /* FIXME: This return value is ignored in the BEGIN_RING macro! */
  1710. #if RADEON_FIFO_DEBUG
  1711. radeon_status(dev_priv);
  1712. DRM_ERROR("failed!\n");
  1713. #endif
  1714. return -EBUSY;
  1715. }
  1716. static int radeon_cp_get_buffers(struct drm_device *dev,
  1717. struct drm_file *file_priv,
  1718. struct drm_dma * d)
  1719. {
  1720. int i;
  1721. struct drm_buf *buf;
  1722. for (i = d->granted_count; i < d->request_count; i++) {
  1723. buf = radeon_freelist_get(dev);
  1724. if (!buf)
  1725. return -EBUSY; /* NOTE: broken client */
  1726. buf->file_priv = file_priv;
  1727. if (copy_to_user(&d->request_indices[i], &buf->idx,
  1728. sizeof(buf->idx)))
  1729. return -EFAULT;
  1730. if (copy_to_user(&d->request_sizes[i], &buf->total,
  1731. sizeof(buf->total)))
  1732. return -EFAULT;
  1733. d->granted_count++;
  1734. }
  1735. return 0;
  1736. }
  1737. int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
  1738. {
  1739. struct drm_device_dma *dma = dev->dma;
  1740. int ret = 0;
  1741. struct drm_dma *d = data;
  1742. LOCK_TEST_WITH_RETURN(dev, file_priv);
  1743. /* Please don't send us buffers.
  1744. */
  1745. if (d->send_count != 0) {
  1746. DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
  1747. DRM_CURRENTPID, d->send_count);
  1748. return -EINVAL;
  1749. }
  1750. /* We'll send you buffers.
  1751. */
  1752. if (d->request_count < 0 || d->request_count > dma->buf_count) {
  1753. DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
  1754. DRM_CURRENTPID, d->request_count, dma->buf_count);
  1755. return -EINVAL;
  1756. }
  1757. d->granted_count = 0;
  1758. if (d->request_count) {
  1759. ret = radeon_cp_get_buffers(dev, file_priv, d);
  1760. }
  1761. return ret;
  1762. }
  1763. int radeon_driver_load(struct drm_device *dev, unsigned long flags)
  1764. {
  1765. drm_radeon_private_t *dev_priv;
  1766. int ret = 0;
  1767. dev_priv = kzalloc(sizeof(drm_radeon_private_t), GFP_KERNEL);
  1768. if (dev_priv == NULL)
  1769. return -ENOMEM;
  1770. dev->dev_private = (void *)dev_priv;
  1771. dev_priv->flags = flags;
  1772. switch (flags & RADEON_FAMILY_MASK) {
  1773. case CHIP_R100:
  1774. case CHIP_RV200:
  1775. case CHIP_R200:
  1776. case CHIP_R300:
  1777. case CHIP_R350:
  1778. case CHIP_R420:
  1779. case CHIP_R423:
  1780. case CHIP_RV410:
  1781. case CHIP_RV515:
  1782. case CHIP_R520:
  1783. case CHIP_RV570:
  1784. case CHIP_R580:
  1785. dev_priv->flags |= RADEON_HAS_HIERZ;
  1786. break;
  1787. default:
  1788. /* all other chips have no hierarchical z buffer */
  1789. break;
  1790. }
  1791. pci_set_master(dev->pdev);
  1792. if (drm_pci_device_is_agp(dev))
  1793. dev_priv->flags |= RADEON_IS_AGP;
  1794. else if (pci_is_pcie(dev->pdev))
  1795. dev_priv->flags |= RADEON_IS_PCIE;
  1796. else
  1797. dev_priv->flags |= RADEON_IS_PCI;
  1798. ret = drm_legacy_addmap(dev, pci_resource_start(dev->pdev, 2),
  1799. pci_resource_len(dev->pdev, 2), _DRM_REGISTERS,
  1800. _DRM_READ_ONLY | _DRM_DRIVER, &dev_priv->mmio);
  1801. if (ret != 0)
  1802. return ret;
  1803. ret = drm_vblank_init(dev, 2);
  1804. if (ret) {
  1805. radeon_driver_unload(dev);
  1806. return ret;
  1807. }
  1808. DRM_DEBUG("%s card detected\n",
  1809. ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
  1810. return ret;
  1811. }
  1812. int radeon_master_create(struct drm_device *dev, struct drm_master *master)
  1813. {
  1814. struct drm_radeon_master_private *master_priv;
  1815. unsigned long sareapage;
  1816. int ret;
  1817. master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
  1818. if (!master_priv)
  1819. return -ENOMEM;
  1820. /* prebuild the SAREA */
  1821. sareapage = max_t(unsigned long, SAREA_MAX, PAGE_SIZE);
  1822. ret = drm_legacy_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK,
  1823. &master_priv->sarea);
  1824. if (ret) {
  1825. DRM_ERROR("SAREA setup failed\n");
  1826. kfree(master_priv);
  1827. return ret;
  1828. }
  1829. master_priv->sarea_priv = master_priv->sarea->handle + sizeof(struct drm_sarea);
  1830. master_priv->sarea_priv->pfCurrentPage = 0;
  1831. master->driver_priv = master_priv;
  1832. return 0;
  1833. }
  1834. void radeon_master_destroy(struct drm_device *dev, struct drm_master *master)
  1835. {
  1836. struct drm_radeon_master_private *master_priv = master->driver_priv;
  1837. if (!master_priv)
  1838. return;
  1839. if (master_priv->sarea_priv &&
  1840. master_priv->sarea_priv->pfCurrentPage != 0)
  1841. radeon_cp_dispatch_flip(dev, master);
  1842. master_priv->sarea_priv = NULL;
  1843. if (master_priv->sarea)
  1844. drm_legacy_rmmap_locked(dev, master_priv->sarea);
  1845. kfree(master_priv);
  1846. master->driver_priv = NULL;
  1847. }
  1848. /* Create mappings for registers and framebuffer so userland doesn't necessarily
  1849. * have to find them.
  1850. */
  1851. int radeon_driver_firstopen(struct drm_device *dev)
  1852. {
  1853. int ret;
  1854. drm_local_map_t *map;
  1855. drm_radeon_private_t *dev_priv = dev->dev_private;
  1856. dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
  1857. dev_priv->fb_aper_offset = pci_resource_start(dev->pdev, 0);
  1858. ret = drm_legacy_addmap(dev, dev_priv->fb_aper_offset,
  1859. pci_resource_len(dev->pdev, 0),
  1860. _DRM_FRAME_BUFFER, _DRM_WRITE_COMBINING, &map);
  1861. if (ret != 0)
  1862. return ret;
  1863. return 0;
  1864. }
  1865. int radeon_driver_unload(struct drm_device *dev)
  1866. {
  1867. drm_radeon_private_t *dev_priv = dev->dev_private;
  1868. DRM_DEBUG("\n");
  1869. drm_legacy_rmmap(dev, dev_priv->mmio);
  1870. kfree(dev_priv);
  1871. dev->dev_private = NULL;
  1872. return 0;
  1873. }
  1874. void radeon_commit_ring(drm_radeon_private_t *dev_priv)
  1875. {
  1876. int i;
  1877. u32 *ring;
  1878. int tail_aligned;
  1879. /* check if the ring is padded out to 16-dword alignment */
  1880. tail_aligned = dev_priv->ring.tail & (RADEON_RING_ALIGN-1);
  1881. if (tail_aligned) {
  1882. int num_p2 = RADEON_RING_ALIGN - tail_aligned;
  1883. ring = dev_priv->ring.start;
  1884. /* pad with some CP_PACKET2 */
  1885. for (i = 0; i < num_p2; i++)
  1886. ring[dev_priv->ring.tail + i] = CP_PACKET2();
  1887. dev_priv->ring.tail += i;
  1888. dev_priv->ring.space -= num_p2 * sizeof(u32);
  1889. }
  1890. dev_priv->ring.tail &= dev_priv->ring.tail_mask;
  1891. mb();
  1892. GET_RING_HEAD( dev_priv );
  1893. if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
  1894. RADEON_WRITE(R600_CP_RB_WPTR, dev_priv->ring.tail);
  1895. /* read from PCI bus to ensure correct posting */
  1896. RADEON_READ(R600_CP_RB_RPTR);
  1897. } else {
  1898. RADEON_WRITE(RADEON_CP_RB_WPTR, dev_priv->ring.tail);
  1899. /* read from PCI bus to ensure correct posting */
  1900. RADEON_READ(RADEON_CP_RB_RPTR);
  1901. }
  1902. }