r100.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336
  1. /*
  2. * Copyright 2008 Advanced Micro Devices, Inc.
  3. * Copyright 2008 Red Hat Inc.
  4. * Copyright 2009 Jerome Glisse.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. *
  24. * Authors: Dave Airlie
  25. * Alex Deucher
  26. * Jerome Glisse
  27. */
  28. #include <linux/seq_file.h>
  29. #include "drmP.h"
  30. #include "drm.h"
  31. #include "radeon_drm.h"
  32. #include "radeon_microcode.h"
  33. #include "radeon_reg.h"
  34. #include "radeon.h"
  35. /* This files gather functions specifics to:
  36. * r100,rv100,rs100,rv200,rs200,r200,rv250,rs300,rv280
  37. *
  38. * Some of these functions might be used by newer ASICs.
  39. */
  40. void r100_hdp_reset(struct radeon_device *rdev);
  41. void r100_gpu_init(struct radeon_device *rdev);
  42. int r100_gui_wait_for_idle(struct radeon_device *rdev);
  43. int r100_mc_wait_for_idle(struct radeon_device *rdev);
  44. void r100_gpu_wait_for_vsync(struct radeon_device *rdev);
  45. void r100_gpu_wait_for_vsync2(struct radeon_device *rdev);
  46. int r100_debugfs_mc_info_init(struct radeon_device *rdev);
  47. /*
  48. * PCI GART
  49. */
  50. void r100_pci_gart_tlb_flush(struct radeon_device *rdev)
  51. {
  52. /* TODO: can we do somethings here ? */
  53. /* It seems hw only cache one entry so we should discard this
  54. * entry otherwise if first GPU GART read hit this entry it
  55. * could end up in wrong address. */
  56. }
  57. int r100_pci_gart_enable(struct radeon_device *rdev)
  58. {
  59. uint32_t tmp;
  60. int r;
  61. /* Initialize common gart structure */
  62. r = radeon_gart_init(rdev);
  63. if (r) {
  64. return r;
  65. }
  66. if (rdev->gart.table.ram.ptr == NULL) {
  67. rdev->gart.table_size = rdev->gart.num_gpu_pages * 4;
  68. r = radeon_gart_table_ram_alloc(rdev);
  69. if (r) {
  70. return r;
  71. }
  72. }
  73. /* discard memory request outside of configured range */
  74. tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
  75. WREG32(RADEON_AIC_CNTL, tmp);
  76. /* set address range for PCI address translate */
  77. WREG32(RADEON_AIC_LO_ADDR, rdev->mc.gtt_location);
  78. tmp = rdev->mc.gtt_location + rdev->mc.gtt_size - 1;
  79. WREG32(RADEON_AIC_HI_ADDR, tmp);
  80. /* Enable bus mastering */
  81. tmp = RREG32(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
  82. WREG32(RADEON_BUS_CNTL, tmp);
  83. /* set PCI GART page-table base address */
  84. WREG32(RADEON_AIC_PT_BASE, rdev->gart.table_addr);
  85. tmp = RREG32(RADEON_AIC_CNTL) | RADEON_PCIGART_TRANSLATE_EN;
  86. WREG32(RADEON_AIC_CNTL, tmp);
  87. r100_pci_gart_tlb_flush(rdev);
  88. rdev->gart.ready = true;
  89. return 0;
  90. }
  91. void r100_pci_gart_disable(struct radeon_device *rdev)
  92. {
  93. uint32_t tmp;
  94. /* discard memory request outside of configured range */
  95. tmp = RREG32(RADEON_AIC_CNTL) | RADEON_DIS_OUT_OF_PCI_GART_ACCESS;
  96. WREG32(RADEON_AIC_CNTL, tmp & ~RADEON_PCIGART_TRANSLATE_EN);
  97. WREG32(RADEON_AIC_LO_ADDR, 0);
  98. WREG32(RADEON_AIC_HI_ADDR, 0);
  99. }
  100. int r100_pci_gart_set_page(struct radeon_device *rdev, int i, uint64_t addr)
  101. {
  102. if (i < 0 || i > rdev->gart.num_gpu_pages) {
  103. return -EINVAL;
  104. }
  105. rdev->gart.table.ram.ptr[i] = cpu_to_le32(lower_32_bits(addr));
  106. return 0;
  107. }
  108. int r100_gart_enable(struct radeon_device *rdev)
  109. {
  110. if (rdev->flags & RADEON_IS_AGP) {
  111. r100_pci_gart_disable(rdev);
  112. return 0;
  113. }
  114. return r100_pci_gart_enable(rdev);
  115. }
  116. /*
  117. * MC
  118. */
  119. void r100_mc_disable_clients(struct radeon_device *rdev)
  120. {
  121. uint32_t ov0_scale_cntl, crtc_ext_cntl, crtc_gen_cntl, crtc2_gen_cntl;
  122. /* FIXME: is this function correct for rs100,rs200,rs300 ? */
  123. if (r100_gui_wait_for_idle(rdev)) {
  124. printk(KERN_WARNING "Failed to wait GUI idle while "
  125. "programming pipes. Bad things might happen.\n");
  126. }
  127. /* stop display and memory access */
  128. ov0_scale_cntl = RREG32(RADEON_OV0_SCALE_CNTL);
  129. WREG32(RADEON_OV0_SCALE_CNTL, ov0_scale_cntl & ~RADEON_SCALER_ENABLE);
  130. crtc_ext_cntl = RREG32(RADEON_CRTC_EXT_CNTL);
  131. WREG32(RADEON_CRTC_EXT_CNTL, crtc_ext_cntl | RADEON_CRTC_DISPLAY_DIS);
  132. crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL);
  133. r100_gpu_wait_for_vsync(rdev);
  134. WREG32(RADEON_CRTC_GEN_CNTL,
  135. (crtc_gen_cntl & ~(RADEON_CRTC_CUR_EN | RADEON_CRTC_ICON_EN)) |
  136. RADEON_CRTC_DISP_REQ_EN_B | RADEON_CRTC_EXT_DISP_EN);
  137. if (!(rdev->flags & RADEON_SINGLE_CRTC)) {
  138. crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL);
  139. r100_gpu_wait_for_vsync2(rdev);
  140. WREG32(RADEON_CRTC2_GEN_CNTL,
  141. (crtc2_gen_cntl &
  142. ~(RADEON_CRTC2_CUR_EN | RADEON_CRTC2_ICON_EN)) |
  143. RADEON_CRTC2_DISP_REQ_EN_B);
  144. }
  145. udelay(500);
  146. }
  147. void r100_mc_setup(struct radeon_device *rdev)
  148. {
  149. uint32_t tmp;
  150. int r;
  151. r = r100_debugfs_mc_info_init(rdev);
  152. if (r) {
  153. DRM_ERROR("Failed to register debugfs file for R100 MC !\n");
  154. }
  155. /* Write VRAM size in case we are limiting it */
  156. WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
  157. /* Novell bug 204882 for RN50/M6/M7 with 8/16/32MB VRAM,
  158. * if the aperture is 64MB but we have 32MB VRAM
  159. * we report only 32MB VRAM but we have to set MC_FB_LOCATION
  160. * to 64MB, otherwise the gpu accidentially dies */
  161. tmp = rdev->mc.vram_location + rdev->mc.mc_vram_size - 1;
  162. tmp = REG_SET(RADEON_MC_FB_TOP, tmp >> 16);
  163. tmp |= REG_SET(RADEON_MC_FB_START, rdev->mc.vram_location >> 16);
  164. WREG32(RADEON_MC_FB_LOCATION, tmp);
  165. /* Enable bus mastering */
  166. tmp = RREG32(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
  167. WREG32(RADEON_BUS_CNTL, tmp);
  168. if (rdev->flags & RADEON_IS_AGP) {
  169. tmp = rdev->mc.gtt_location + rdev->mc.gtt_size - 1;
  170. tmp = REG_SET(RADEON_MC_AGP_TOP, tmp >> 16);
  171. tmp |= REG_SET(RADEON_MC_AGP_START, rdev->mc.gtt_location >> 16);
  172. WREG32(RADEON_MC_AGP_LOCATION, tmp);
  173. WREG32(RADEON_AGP_BASE, rdev->mc.agp_base);
  174. } else {
  175. WREG32(RADEON_MC_AGP_LOCATION, 0x0FFFFFFF);
  176. WREG32(RADEON_AGP_BASE, 0);
  177. }
  178. tmp = RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL;
  179. tmp |= (7 << 28);
  180. WREG32(RADEON_HOST_PATH_CNTL, tmp | RADEON_HDP_SOFT_RESET | RADEON_HDP_READ_BUFFER_INVALIDATE);
  181. (void)RREG32(RADEON_HOST_PATH_CNTL);
  182. WREG32(RADEON_HOST_PATH_CNTL, tmp);
  183. (void)RREG32(RADEON_HOST_PATH_CNTL);
  184. }
  185. int r100_mc_init(struct radeon_device *rdev)
  186. {
  187. int r;
  188. if (r100_debugfs_rbbm_init(rdev)) {
  189. DRM_ERROR("Failed to register debugfs file for RBBM !\n");
  190. }
  191. r100_gpu_init(rdev);
  192. /* Disable gart which also disable out of gart access */
  193. r100_pci_gart_disable(rdev);
  194. /* Setup GPU memory space */
  195. rdev->mc.gtt_location = 0xFFFFFFFFUL;
  196. if (rdev->flags & RADEON_IS_AGP) {
  197. r = radeon_agp_init(rdev);
  198. if (r) {
  199. printk(KERN_WARNING "[drm] Disabling AGP\n");
  200. rdev->flags &= ~RADEON_IS_AGP;
  201. rdev->mc.gtt_size = radeon_gart_size * 1024 * 1024;
  202. } else {
  203. rdev->mc.gtt_location = rdev->mc.agp_base;
  204. }
  205. }
  206. r = radeon_mc_setup(rdev);
  207. if (r) {
  208. return r;
  209. }
  210. r100_mc_disable_clients(rdev);
  211. if (r100_mc_wait_for_idle(rdev)) {
  212. printk(KERN_WARNING "Failed to wait MC idle while "
  213. "programming pipes. Bad things might happen.\n");
  214. }
  215. r100_mc_setup(rdev);
  216. return 0;
  217. }
  218. void r100_mc_fini(struct radeon_device *rdev)
  219. {
  220. r100_pci_gart_disable(rdev);
  221. radeon_gart_table_ram_free(rdev);
  222. radeon_gart_fini(rdev);
  223. }
  224. /*
  225. * Interrupts
  226. */
  227. int r100_irq_set(struct radeon_device *rdev)
  228. {
  229. uint32_t tmp = 0;
  230. if (rdev->irq.sw_int) {
  231. tmp |= RADEON_SW_INT_ENABLE;
  232. }
  233. if (rdev->irq.crtc_vblank_int[0]) {
  234. tmp |= RADEON_CRTC_VBLANK_MASK;
  235. }
  236. if (rdev->irq.crtc_vblank_int[1]) {
  237. tmp |= RADEON_CRTC2_VBLANK_MASK;
  238. }
  239. WREG32(RADEON_GEN_INT_CNTL, tmp);
  240. return 0;
  241. }
  242. static inline uint32_t r100_irq_ack(struct radeon_device *rdev)
  243. {
  244. uint32_t irqs = RREG32(RADEON_GEN_INT_STATUS);
  245. uint32_t irq_mask = RADEON_SW_INT_TEST | RADEON_CRTC_VBLANK_STAT |
  246. RADEON_CRTC2_VBLANK_STAT;
  247. if (irqs) {
  248. WREG32(RADEON_GEN_INT_STATUS, irqs);
  249. }
  250. return irqs & irq_mask;
  251. }
  252. int r100_irq_process(struct radeon_device *rdev)
  253. {
  254. uint32_t status;
  255. status = r100_irq_ack(rdev);
  256. if (!status) {
  257. return IRQ_NONE;
  258. }
  259. while (status) {
  260. /* SW interrupt */
  261. if (status & RADEON_SW_INT_TEST) {
  262. radeon_fence_process(rdev);
  263. }
  264. /* Vertical blank interrupts */
  265. if (status & RADEON_CRTC_VBLANK_STAT) {
  266. drm_handle_vblank(rdev->ddev, 0);
  267. }
  268. if (status & RADEON_CRTC2_VBLANK_STAT) {
  269. drm_handle_vblank(rdev->ddev, 1);
  270. }
  271. status = r100_irq_ack(rdev);
  272. }
  273. return IRQ_HANDLED;
  274. }
  275. u32 r100_get_vblank_counter(struct radeon_device *rdev, int crtc)
  276. {
  277. if (crtc == 0)
  278. return RREG32(RADEON_CRTC_CRNT_FRAME);
  279. else
  280. return RREG32(RADEON_CRTC2_CRNT_FRAME);
  281. }
  282. /*
  283. * Fence emission
  284. */
  285. void r100_fence_ring_emit(struct radeon_device *rdev,
  286. struct radeon_fence *fence)
  287. {
  288. /* Who ever call radeon_fence_emit should call ring_lock and ask
  289. * for enough space (today caller are ib schedule and buffer move) */
  290. /* Wait until IDLE & CLEAN */
  291. radeon_ring_write(rdev, PACKET0(0x1720, 0));
  292. radeon_ring_write(rdev, (1 << 16) | (1 << 17));
  293. /* Emit fence sequence & fire IRQ */
  294. radeon_ring_write(rdev, PACKET0(rdev->fence_drv.scratch_reg, 0));
  295. radeon_ring_write(rdev, fence->seq);
  296. radeon_ring_write(rdev, PACKET0(RADEON_GEN_INT_STATUS, 0));
  297. radeon_ring_write(rdev, RADEON_SW_INT_FIRE);
  298. }
  299. /*
  300. * Writeback
  301. */
  302. int r100_wb_init(struct radeon_device *rdev)
  303. {
  304. int r;
  305. if (rdev->wb.wb_obj == NULL) {
  306. r = radeon_object_create(rdev, NULL, 4096,
  307. true,
  308. RADEON_GEM_DOMAIN_GTT,
  309. false, &rdev->wb.wb_obj);
  310. if (r) {
  311. DRM_ERROR("radeon: failed to create WB buffer (%d).\n", r);
  312. return r;
  313. }
  314. r = radeon_object_pin(rdev->wb.wb_obj,
  315. RADEON_GEM_DOMAIN_GTT,
  316. &rdev->wb.gpu_addr);
  317. if (r) {
  318. DRM_ERROR("radeon: failed to pin WB buffer (%d).\n", r);
  319. return r;
  320. }
  321. r = radeon_object_kmap(rdev->wb.wb_obj, (void **)&rdev->wb.wb);
  322. if (r) {
  323. DRM_ERROR("radeon: failed to map WB buffer (%d).\n", r);
  324. return r;
  325. }
  326. }
  327. WREG32(0x774, rdev->wb.gpu_addr);
  328. WREG32(0x70C, rdev->wb.gpu_addr + 1024);
  329. WREG32(0x770, 0xff);
  330. return 0;
  331. }
  332. void r100_wb_fini(struct radeon_device *rdev)
  333. {
  334. if (rdev->wb.wb_obj) {
  335. radeon_object_kunmap(rdev->wb.wb_obj);
  336. radeon_object_unpin(rdev->wb.wb_obj);
  337. radeon_object_unref(&rdev->wb.wb_obj);
  338. rdev->wb.wb = NULL;
  339. rdev->wb.wb_obj = NULL;
  340. }
  341. }
  342. int r100_copy_blit(struct radeon_device *rdev,
  343. uint64_t src_offset,
  344. uint64_t dst_offset,
  345. unsigned num_pages,
  346. struct radeon_fence *fence)
  347. {
  348. uint32_t cur_pages;
  349. uint32_t stride_bytes = PAGE_SIZE;
  350. uint32_t pitch;
  351. uint32_t stride_pixels;
  352. unsigned ndw;
  353. int num_loops;
  354. int r = 0;
  355. /* radeon limited to 16k stride */
  356. stride_bytes &= 0x3fff;
  357. /* radeon pitch is /64 */
  358. pitch = stride_bytes / 64;
  359. stride_pixels = stride_bytes / 4;
  360. num_loops = DIV_ROUND_UP(num_pages, 8191);
  361. /* Ask for enough room for blit + flush + fence */
  362. ndw = 64 + (10 * num_loops);
  363. r = radeon_ring_lock(rdev, ndw);
  364. if (r) {
  365. DRM_ERROR("radeon: moving bo (%d) asking for %u dw.\n", r, ndw);
  366. return -EINVAL;
  367. }
  368. while (num_pages > 0) {
  369. cur_pages = num_pages;
  370. if (cur_pages > 8191) {
  371. cur_pages = 8191;
  372. }
  373. num_pages -= cur_pages;
  374. /* pages are in Y direction - height
  375. page width in X direction - width */
  376. radeon_ring_write(rdev, PACKET3(PACKET3_BITBLT_MULTI, 8));
  377. radeon_ring_write(rdev,
  378. RADEON_GMC_SRC_PITCH_OFFSET_CNTL |
  379. RADEON_GMC_DST_PITCH_OFFSET_CNTL |
  380. RADEON_GMC_SRC_CLIPPING |
  381. RADEON_GMC_DST_CLIPPING |
  382. RADEON_GMC_BRUSH_NONE |
  383. (RADEON_COLOR_FORMAT_ARGB8888 << 8) |
  384. RADEON_GMC_SRC_DATATYPE_COLOR |
  385. RADEON_ROP3_S |
  386. RADEON_DP_SRC_SOURCE_MEMORY |
  387. RADEON_GMC_CLR_CMP_CNTL_DIS |
  388. RADEON_GMC_WR_MSK_DIS);
  389. radeon_ring_write(rdev, (pitch << 22) | (src_offset >> 10));
  390. radeon_ring_write(rdev, (pitch << 22) | (dst_offset >> 10));
  391. radeon_ring_write(rdev, (0x1fff) | (0x1fff << 16));
  392. radeon_ring_write(rdev, 0);
  393. radeon_ring_write(rdev, (0x1fff) | (0x1fff << 16));
  394. radeon_ring_write(rdev, num_pages);
  395. radeon_ring_write(rdev, num_pages);
  396. radeon_ring_write(rdev, cur_pages | (stride_pixels << 16));
  397. }
  398. radeon_ring_write(rdev, PACKET0(RADEON_DSTCACHE_CTLSTAT, 0));
  399. radeon_ring_write(rdev, RADEON_RB2D_DC_FLUSH_ALL);
  400. radeon_ring_write(rdev, PACKET0(RADEON_WAIT_UNTIL, 0));
  401. radeon_ring_write(rdev,
  402. RADEON_WAIT_2D_IDLECLEAN |
  403. RADEON_WAIT_HOST_IDLECLEAN |
  404. RADEON_WAIT_DMA_GUI_IDLE);
  405. if (fence) {
  406. r = radeon_fence_emit(rdev, fence);
  407. }
  408. radeon_ring_unlock_commit(rdev);
  409. return r;
  410. }
  411. /*
  412. * CP
  413. */
  414. void r100_ring_start(struct radeon_device *rdev)
  415. {
  416. int r;
  417. r = radeon_ring_lock(rdev, 2);
  418. if (r) {
  419. return;
  420. }
  421. radeon_ring_write(rdev, PACKET0(RADEON_ISYNC_CNTL, 0));
  422. radeon_ring_write(rdev,
  423. RADEON_ISYNC_ANY2D_IDLE3D |
  424. RADEON_ISYNC_ANY3D_IDLE2D |
  425. RADEON_ISYNC_WAIT_IDLEGUI |
  426. RADEON_ISYNC_CPSCRATCH_IDLEGUI);
  427. radeon_ring_unlock_commit(rdev);
  428. }
  429. static void r100_cp_load_microcode(struct radeon_device *rdev)
  430. {
  431. int i;
  432. if (r100_gui_wait_for_idle(rdev)) {
  433. printk(KERN_WARNING "Failed to wait GUI idle while "
  434. "programming pipes. Bad things might happen.\n");
  435. }
  436. WREG32(RADEON_CP_ME_RAM_ADDR, 0);
  437. if ((rdev->family == CHIP_R100) || (rdev->family == CHIP_RV100) ||
  438. (rdev->family == CHIP_RV200) || (rdev->family == CHIP_RS100) ||
  439. (rdev->family == CHIP_RS200)) {
  440. DRM_INFO("Loading R100 Microcode\n");
  441. for (i = 0; i < 256; i++) {
  442. WREG32(RADEON_CP_ME_RAM_DATAH, R100_cp_microcode[i][1]);
  443. WREG32(RADEON_CP_ME_RAM_DATAL, R100_cp_microcode[i][0]);
  444. }
  445. } else if ((rdev->family == CHIP_R200) ||
  446. (rdev->family == CHIP_RV250) ||
  447. (rdev->family == CHIP_RV280) ||
  448. (rdev->family == CHIP_RS300)) {
  449. DRM_INFO("Loading R200 Microcode\n");
  450. for (i = 0; i < 256; i++) {
  451. WREG32(RADEON_CP_ME_RAM_DATAH, R200_cp_microcode[i][1]);
  452. WREG32(RADEON_CP_ME_RAM_DATAL, R200_cp_microcode[i][0]);
  453. }
  454. } else if ((rdev->family == CHIP_R300) ||
  455. (rdev->family == CHIP_R350) ||
  456. (rdev->family == CHIP_RV350) ||
  457. (rdev->family == CHIP_RV380) ||
  458. (rdev->family == CHIP_RS400) ||
  459. (rdev->family == CHIP_RS480)) {
  460. DRM_INFO("Loading R300 Microcode\n");
  461. for (i = 0; i < 256; i++) {
  462. WREG32(RADEON_CP_ME_RAM_DATAH, R300_cp_microcode[i][1]);
  463. WREG32(RADEON_CP_ME_RAM_DATAL, R300_cp_microcode[i][0]);
  464. }
  465. } else if ((rdev->family == CHIP_R420) ||
  466. (rdev->family == CHIP_R423) ||
  467. (rdev->family == CHIP_RV410)) {
  468. DRM_INFO("Loading R400 Microcode\n");
  469. for (i = 0; i < 256; i++) {
  470. WREG32(RADEON_CP_ME_RAM_DATAH, R420_cp_microcode[i][1]);
  471. WREG32(RADEON_CP_ME_RAM_DATAL, R420_cp_microcode[i][0]);
  472. }
  473. } else if ((rdev->family == CHIP_RS690) ||
  474. (rdev->family == CHIP_RS740)) {
  475. DRM_INFO("Loading RS690/RS740 Microcode\n");
  476. for (i = 0; i < 256; i++) {
  477. WREG32(RADEON_CP_ME_RAM_DATAH, RS690_cp_microcode[i][1]);
  478. WREG32(RADEON_CP_ME_RAM_DATAL, RS690_cp_microcode[i][0]);
  479. }
  480. } else if (rdev->family == CHIP_RS600) {
  481. DRM_INFO("Loading RS600 Microcode\n");
  482. for (i = 0; i < 256; i++) {
  483. WREG32(RADEON_CP_ME_RAM_DATAH, RS600_cp_microcode[i][1]);
  484. WREG32(RADEON_CP_ME_RAM_DATAL, RS600_cp_microcode[i][0]);
  485. }
  486. } else if ((rdev->family == CHIP_RV515) ||
  487. (rdev->family == CHIP_R520) ||
  488. (rdev->family == CHIP_RV530) ||
  489. (rdev->family == CHIP_R580) ||
  490. (rdev->family == CHIP_RV560) ||
  491. (rdev->family == CHIP_RV570)) {
  492. DRM_INFO("Loading R500 Microcode\n");
  493. for (i = 0; i < 256; i++) {
  494. WREG32(RADEON_CP_ME_RAM_DATAH, R520_cp_microcode[i][1]);
  495. WREG32(RADEON_CP_ME_RAM_DATAL, R520_cp_microcode[i][0]);
  496. }
  497. }
  498. }
  499. int r100_cp_init(struct radeon_device *rdev, unsigned ring_size)
  500. {
  501. unsigned rb_bufsz;
  502. unsigned rb_blksz;
  503. unsigned max_fetch;
  504. unsigned pre_write_timer;
  505. unsigned pre_write_limit;
  506. unsigned indirect2_start;
  507. unsigned indirect1_start;
  508. uint32_t tmp;
  509. int r;
  510. if (r100_debugfs_cp_init(rdev)) {
  511. DRM_ERROR("Failed to register debugfs file for CP !\n");
  512. }
  513. /* Reset CP */
  514. tmp = RREG32(RADEON_CP_CSQ_STAT);
  515. if ((tmp & (1 << 31))) {
  516. DRM_INFO("radeon: cp busy (0x%08X) resetting\n", tmp);
  517. WREG32(RADEON_CP_CSQ_MODE, 0);
  518. WREG32(RADEON_CP_CSQ_CNTL, 0);
  519. WREG32(RADEON_RBBM_SOFT_RESET, RADEON_SOFT_RESET_CP);
  520. tmp = RREG32(RADEON_RBBM_SOFT_RESET);
  521. mdelay(2);
  522. WREG32(RADEON_RBBM_SOFT_RESET, 0);
  523. tmp = RREG32(RADEON_RBBM_SOFT_RESET);
  524. mdelay(2);
  525. tmp = RREG32(RADEON_CP_CSQ_STAT);
  526. if ((tmp & (1 << 31))) {
  527. DRM_INFO("radeon: cp reset failed (0x%08X)\n", tmp);
  528. }
  529. } else {
  530. DRM_INFO("radeon: cp idle (0x%08X)\n", tmp);
  531. }
  532. /* Align ring size */
  533. rb_bufsz = drm_order(ring_size / 8);
  534. ring_size = (1 << (rb_bufsz + 1)) * 4;
  535. r100_cp_load_microcode(rdev);
  536. r = radeon_ring_init(rdev, ring_size);
  537. if (r) {
  538. return r;
  539. }
  540. /* Each time the cp read 1024 bytes (16 dword/quadword) update
  541. * the rptr copy in system ram */
  542. rb_blksz = 9;
  543. /* cp will read 128bytes at a time (4 dwords) */
  544. max_fetch = 1;
  545. rdev->cp.align_mask = 16 - 1;
  546. /* Write to CP_RB_WPTR will be delayed for pre_write_timer clocks */
  547. pre_write_timer = 64;
  548. /* Force CP_RB_WPTR write if written more than one time before the
  549. * delay expire
  550. */
  551. pre_write_limit = 0;
  552. /* Setup the cp cache like this (cache size is 96 dwords) :
  553. * RING 0 to 15
  554. * INDIRECT1 16 to 79
  555. * INDIRECT2 80 to 95
  556. * So ring cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
  557. * indirect1 cache size is 64dwords (> (2 * max_fetch = 2 * 4dwords))
  558. * indirect2 cache size is 16dwords (> (2 * max_fetch = 2 * 4dwords))
  559. * Idea being that most of the gpu cmd will be through indirect1 buffer
  560. * so it gets the bigger cache.
  561. */
  562. indirect2_start = 80;
  563. indirect1_start = 16;
  564. /* cp setup */
  565. WREG32(0x718, pre_write_timer | (pre_write_limit << 28));
  566. WREG32(RADEON_CP_RB_CNTL,
  567. #ifdef __BIG_ENDIAN
  568. RADEON_BUF_SWAP_32BIT |
  569. #endif
  570. REG_SET(RADEON_RB_BUFSZ, rb_bufsz) |
  571. REG_SET(RADEON_RB_BLKSZ, rb_blksz) |
  572. REG_SET(RADEON_MAX_FETCH, max_fetch) |
  573. RADEON_RB_NO_UPDATE);
  574. /* Set ring address */
  575. DRM_INFO("radeon: ring at 0x%016lX\n", (unsigned long)rdev->cp.gpu_addr);
  576. WREG32(RADEON_CP_RB_BASE, rdev->cp.gpu_addr);
  577. /* Force read & write ptr to 0 */
  578. tmp = RREG32(RADEON_CP_RB_CNTL);
  579. WREG32(RADEON_CP_RB_CNTL, tmp | RADEON_RB_RPTR_WR_ENA);
  580. WREG32(RADEON_CP_RB_RPTR_WR, 0);
  581. WREG32(RADEON_CP_RB_WPTR, 0);
  582. WREG32(RADEON_CP_RB_CNTL, tmp);
  583. udelay(10);
  584. rdev->cp.rptr = RREG32(RADEON_CP_RB_RPTR);
  585. rdev->cp.wptr = RREG32(RADEON_CP_RB_WPTR);
  586. /* Set cp mode to bus mastering & enable cp*/
  587. WREG32(RADEON_CP_CSQ_MODE,
  588. REG_SET(RADEON_INDIRECT2_START, indirect2_start) |
  589. REG_SET(RADEON_INDIRECT1_START, indirect1_start));
  590. WREG32(0x718, 0);
  591. WREG32(0x744, 0x00004D4D);
  592. WREG32(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIBM_INDBM);
  593. radeon_ring_start(rdev);
  594. r = radeon_ring_test(rdev);
  595. if (r) {
  596. DRM_ERROR("radeon: cp isn't working (%d).\n", r);
  597. return r;
  598. }
  599. rdev->cp.ready = true;
  600. return 0;
  601. }
  602. void r100_cp_fini(struct radeon_device *rdev)
  603. {
  604. /* Disable ring */
  605. rdev->cp.ready = false;
  606. WREG32(RADEON_CP_CSQ_CNTL, 0);
  607. radeon_ring_fini(rdev);
  608. DRM_INFO("radeon: cp finalized\n");
  609. }
  610. void r100_cp_disable(struct radeon_device *rdev)
  611. {
  612. /* Disable ring */
  613. rdev->cp.ready = false;
  614. WREG32(RADEON_CP_CSQ_MODE, 0);
  615. WREG32(RADEON_CP_CSQ_CNTL, 0);
  616. if (r100_gui_wait_for_idle(rdev)) {
  617. printk(KERN_WARNING "Failed to wait GUI idle while "
  618. "programming pipes. Bad things might happen.\n");
  619. }
  620. }
  621. int r100_cp_reset(struct radeon_device *rdev)
  622. {
  623. uint32_t tmp;
  624. bool reinit_cp;
  625. int i;
  626. reinit_cp = rdev->cp.ready;
  627. rdev->cp.ready = false;
  628. WREG32(RADEON_CP_CSQ_MODE, 0);
  629. WREG32(RADEON_CP_CSQ_CNTL, 0);
  630. WREG32(RADEON_RBBM_SOFT_RESET, RADEON_SOFT_RESET_CP);
  631. (void)RREG32(RADEON_RBBM_SOFT_RESET);
  632. udelay(200);
  633. WREG32(RADEON_RBBM_SOFT_RESET, 0);
  634. /* Wait to prevent race in RBBM_STATUS */
  635. mdelay(1);
  636. for (i = 0; i < rdev->usec_timeout; i++) {
  637. tmp = RREG32(RADEON_RBBM_STATUS);
  638. if (!(tmp & (1 << 16))) {
  639. DRM_INFO("CP reset succeed (RBBM_STATUS=0x%08X)\n",
  640. tmp);
  641. if (reinit_cp) {
  642. return r100_cp_init(rdev, rdev->cp.ring_size);
  643. }
  644. return 0;
  645. }
  646. DRM_UDELAY(1);
  647. }
  648. tmp = RREG32(RADEON_RBBM_STATUS);
  649. DRM_ERROR("Failed to reset CP (RBBM_STATUS=0x%08X)!\n", tmp);
  650. return -1;
  651. }
  652. /*
  653. * CS functions
  654. */
  655. int r100_cs_parse_packet0(struct radeon_cs_parser *p,
  656. struct radeon_cs_packet *pkt,
  657. const unsigned *auth, unsigned n,
  658. radeon_packet0_check_t check)
  659. {
  660. unsigned reg;
  661. unsigned i, j, m;
  662. unsigned idx;
  663. int r;
  664. idx = pkt->idx + 1;
  665. reg = pkt->reg;
  666. /* Check that register fall into register range
  667. * determined by the number of entry (n) in the
  668. * safe register bitmap.
  669. */
  670. if (pkt->one_reg_wr) {
  671. if ((reg >> 7) > n) {
  672. return -EINVAL;
  673. }
  674. } else {
  675. if (((reg + (pkt->count << 2)) >> 7) > n) {
  676. return -EINVAL;
  677. }
  678. }
  679. for (i = 0; i <= pkt->count; i++, idx++) {
  680. j = (reg >> 7);
  681. m = 1 << ((reg >> 2) & 31);
  682. if (auth[j] & m) {
  683. r = check(p, pkt, idx, reg);
  684. if (r) {
  685. return r;
  686. }
  687. }
  688. if (pkt->one_reg_wr) {
  689. if (!(auth[j] & m)) {
  690. break;
  691. }
  692. } else {
  693. reg += 4;
  694. }
  695. }
  696. return 0;
  697. }
  698. void r100_cs_dump_packet(struct radeon_cs_parser *p,
  699. struct radeon_cs_packet *pkt)
  700. {
  701. struct radeon_cs_chunk *ib_chunk;
  702. volatile uint32_t *ib;
  703. unsigned i;
  704. unsigned idx;
  705. ib = p->ib->ptr;
  706. ib_chunk = &p->chunks[p->chunk_ib_idx];
  707. idx = pkt->idx;
  708. for (i = 0; i <= (pkt->count + 1); i++, idx++) {
  709. DRM_INFO("ib[%d]=0x%08X\n", idx, ib[idx]);
  710. }
  711. }
  712. /**
  713. * r100_cs_packet_parse() - parse cp packet and point ib index to next packet
  714. * @parser: parser structure holding parsing context.
  715. * @pkt: where to store packet informations
  716. *
  717. * Assume that chunk_ib_index is properly set. Will return -EINVAL
  718. * if packet is bigger than remaining ib size. or if packets is unknown.
  719. **/
  720. int r100_cs_packet_parse(struct radeon_cs_parser *p,
  721. struct radeon_cs_packet *pkt,
  722. unsigned idx)
  723. {
  724. struct radeon_cs_chunk *ib_chunk = &p->chunks[p->chunk_ib_idx];
  725. uint32_t header;
  726. if (idx >= ib_chunk->length_dw) {
  727. DRM_ERROR("Can not parse packet at %d after CS end %d !\n",
  728. idx, ib_chunk->length_dw);
  729. return -EINVAL;
  730. }
  731. header = ib_chunk->kdata[idx];
  732. pkt->idx = idx;
  733. pkt->type = CP_PACKET_GET_TYPE(header);
  734. pkt->count = CP_PACKET_GET_COUNT(header);
  735. switch (pkt->type) {
  736. case PACKET_TYPE0:
  737. pkt->reg = CP_PACKET0_GET_REG(header);
  738. pkt->one_reg_wr = CP_PACKET0_GET_ONE_REG_WR(header);
  739. break;
  740. case PACKET_TYPE3:
  741. pkt->opcode = CP_PACKET3_GET_OPCODE(header);
  742. break;
  743. case PACKET_TYPE2:
  744. pkt->count = -1;
  745. break;
  746. default:
  747. DRM_ERROR("Unknown packet type %d at %d !\n", pkt->type, idx);
  748. return -EINVAL;
  749. }
  750. if ((pkt->count + 1 + pkt->idx) >= ib_chunk->length_dw) {
  751. DRM_ERROR("Packet (%d:%d:%d) end after CS buffer (%d) !\n",
  752. pkt->idx, pkt->type, pkt->count, ib_chunk->length_dw);
  753. return -EINVAL;
  754. }
  755. return 0;
  756. }
  757. /**
  758. * r100_cs_packet_next_vline() - parse userspace VLINE packet
  759. * @parser: parser structure holding parsing context.
  760. *
  761. * Userspace sends a special sequence for VLINE waits.
  762. * PACKET0 - VLINE_START_END + value
  763. * PACKET0 - WAIT_UNTIL +_value
  764. * RELOC (P3) - crtc_id in reloc.
  765. *
  766. * This function parses this and relocates the VLINE START END
  767. * and WAIT UNTIL packets to the correct crtc.
  768. * It also detects a switched off crtc and nulls out the
  769. * wait in that case.
  770. */
  771. int r100_cs_packet_parse_vline(struct radeon_cs_parser *p)
  772. {
  773. struct radeon_cs_chunk *ib_chunk;
  774. struct drm_mode_object *obj;
  775. struct drm_crtc *crtc;
  776. struct radeon_crtc *radeon_crtc;
  777. struct radeon_cs_packet p3reloc, waitreloc;
  778. int crtc_id;
  779. int r;
  780. uint32_t header, h_idx, reg;
  781. ib_chunk = &p->chunks[p->chunk_ib_idx];
  782. /* parse the wait until */
  783. r = r100_cs_packet_parse(p, &waitreloc, p->idx);
  784. if (r)
  785. return r;
  786. /* check its a wait until and only 1 count */
  787. if (waitreloc.reg != RADEON_WAIT_UNTIL ||
  788. waitreloc.count != 0) {
  789. DRM_ERROR("vline wait had illegal wait until segment\n");
  790. r = -EINVAL;
  791. return r;
  792. }
  793. if (ib_chunk->kdata[waitreloc.idx + 1] != RADEON_WAIT_CRTC_VLINE) {
  794. DRM_ERROR("vline wait had illegal wait until\n");
  795. r = -EINVAL;
  796. return r;
  797. }
  798. /* jump over the NOP */
  799. r = r100_cs_packet_parse(p, &p3reloc, p->idx);
  800. if (r)
  801. return r;
  802. h_idx = p->idx - 2;
  803. p->idx += waitreloc.count;
  804. p->idx += p3reloc.count;
  805. header = ib_chunk->kdata[h_idx];
  806. crtc_id = ib_chunk->kdata[h_idx + 5];
  807. reg = ib_chunk->kdata[h_idx] >> 2;
  808. mutex_lock(&p->rdev->ddev->mode_config.mutex);
  809. obj = drm_mode_object_find(p->rdev->ddev, crtc_id, DRM_MODE_OBJECT_CRTC);
  810. if (!obj) {
  811. DRM_ERROR("cannot find crtc %d\n", crtc_id);
  812. r = -EINVAL;
  813. goto out;
  814. }
  815. crtc = obj_to_crtc(obj);
  816. radeon_crtc = to_radeon_crtc(crtc);
  817. crtc_id = radeon_crtc->crtc_id;
  818. if (!crtc->enabled) {
  819. /* if the CRTC isn't enabled - we need to nop out the wait until */
  820. ib_chunk->kdata[h_idx + 2] = PACKET2(0);
  821. ib_chunk->kdata[h_idx + 3] = PACKET2(0);
  822. } else if (crtc_id == 1) {
  823. switch (reg) {
  824. case AVIVO_D1MODE_VLINE_START_END:
  825. header &= R300_CP_PACKET0_REG_MASK;
  826. header |= AVIVO_D2MODE_VLINE_START_END >> 2;
  827. break;
  828. case RADEON_CRTC_GUI_TRIG_VLINE:
  829. header &= R300_CP_PACKET0_REG_MASK;
  830. header |= RADEON_CRTC2_GUI_TRIG_VLINE >> 2;
  831. break;
  832. default:
  833. DRM_ERROR("unknown crtc reloc\n");
  834. r = -EINVAL;
  835. goto out;
  836. }
  837. ib_chunk->kdata[h_idx] = header;
  838. ib_chunk->kdata[h_idx + 3] |= RADEON_ENG_DISPLAY_SELECT_CRTC1;
  839. }
  840. out:
  841. mutex_unlock(&p->rdev->ddev->mode_config.mutex);
  842. return r;
  843. }
  844. /**
  845. * r100_cs_packet_next_reloc() - parse next packet which should be reloc packet3
  846. * @parser: parser structure holding parsing context.
  847. * @data: pointer to relocation data
  848. * @offset_start: starting offset
  849. * @offset_mask: offset mask (to align start offset on)
  850. * @reloc: reloc informations
  851. *
  852. * Check next packet is relocation packet3, do bo validation and compute
  853. * GPU offset using the provided start.
  854. **/
  855. int r100_cs_packet_next_reloc(struct radeon_cs_parser *p,
  856. struct radeon_cs_reloc **cs_reloc)
  857. {
  858. struct radeon_cs_chunk *ib_chunk;
  859. struct radeon_cs_chunk *relocs_chunk;
  860. struct radeon_cs_packet p3reloc;
  861. unsigned idx;
  862. int r;
  863. if (p->chunk_relocs_idx == -1) {
  864. DRM_ERROR("No relocation chunk !\n");
  865. return -EINVAL;
  866. }
  867. *cs_reloc = NULL;
  868. ib_chunk = &p->chunks[p->chunk_ib_idx];
  869. relocs_chunk = &p->chunks[p->chunk_relocs_idx];
  870. r = r100_cs_packet_parse(p, &p3reloc, p->idx);
  871. if (r) {
  872. return r;
  873. }
  874. p->idx += p3reloc.count + 2;
  875. if (p3reloc.type != PACKET_TYPE3 || p3reloc.opcode != PACKET3_NOP) {
  876. DRM_ERROR("No packet3 for relocation for packet at %d.\n",
  877. p3reloc.idx);
  878. r100_cs_dump_packet(p, &p3reloc);
  879. return -EINVAL;
  880. }
  881. idx = ib_chunk->kdata[p3reloc.idx + 1];
  882. if (idx >= relocs_chunk->length_dw) {
  883. DRM_ERROR("Relocs at %d after relocations chunk end %d !\n",
  884. idx, relocs_chunk->length_dw);
  885. r100_cs_dump_packet(p, &p3reloc);
  886. return -EINVAL;
  887. }
  888. /* FIXME: we assume reloc size is 4 dwords */
  889. *cs_reloc = p->relocs_ptr[(idx / 4)];
  890. return 0;
  891. }
  892. static int r100_packet0_check(struct radeon_cs_parser *p,
  893. struct radeon_cs_packet *pkt)
  894. {
  895. struct radeon_cs_chunk *ib_chunk;
  896. struct radeon_cs_reloc *reloc;
  897. volatile uint32_t *ib;
  898. uint32_t tmp;
  899. unsigned reg;
  900. unsigned i;
  901. unsigned idx;
  902. bool onereg;
  903. int r;
  904. u32 tile_flags = 0;
  905. ib = p->ib->ptr;
  906. ib_chunk = &p->chunks[p->chunk_ib_idx];
  907. idx = pkt->idx + 1;
  908. reg = pkt->reg;
  909. onereg = false;
  910. if (CP_PACKET0_GET_ONE_REG_WR(ib_chunk->kdata[pkt->idx])) {
  911. onereg = true;
  912. }
  913. for (i = 0; i <= pkt->count; i++, idx++, reg += 4) {
  914. switch (reg) {
  915. case RADEON_CRTC_GUI_TRIG_VLINE:
  916. r = r100_cs_packet_parse_vline(p);
  917. if (r) {
  918. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  919. idx, reg);
  920. r100_cs_dump_packet(p, pkt);
  921. return r;
  922. }
  923. break;
  924. /* FIXME: only allow PACKET3 blit? easier to check for out of
  925. * range access */
  926. case RADEON_DST_PITCH_OFFSET:
  927. case RADEON_SRC_PITCH_OFFSET:
  928. r = r100_cs_packet_next_reloc(p, &reloc);
  929. if (r) {
  930. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  931. idx, reg);
  932. r100_cs_dump_packet(p, pkt);
  933. return r;
  934. }
  935. tmp = ib_chunk->kdata[idx] & 0x003fffff;
  936. tmp += (((u32)reloc->lobj.gpu_offset) >> 10);
  937. if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
  938. tile_flags |= RADEON_DST_TILE_MACRO;
  939. if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO) {
  940. if (reg == RADEON_SRC_PITCH_OFFSET) {
  941. DRM_ERROR("Cannot src blit from microtiled surface\n");
  942. r100_cs_dump_packet(p, pkt);
  943. return -EINVAL;
  944. }
  945. tile_flags |= RADEON_DST_TILE_MICRO;
  946. }
  947. tmp |= tile_flags;
  948. ib[idx] = (ib_chunk->kdata[idx] & 0x3fc00000) | tmp;
  949. break;
  950. case RADEON_RB3D_DEPTHOFFSET:
  951. case RADEON_RB3D_COLOROFFSET:
  952. case R300_RB3D_COLOROFFSET0:
  953. case R300_ZB_DEPTHOFFSET:
  954. case R200_PP_TXOFFSET_0:
  955. case R200_PP_TXOFFSET_1:
  956. case R200_PP_TXOFFSET_2:
  957. case R200_PP_TXOFFSET_3:
  958. case R200_PP_TXOFFSET_4:
  959. case R200_PP_TXOFFSET_5:
  960. case RADEON_PP_TXOFFSET_0:
  961. case RADEON_PP_TXOFFSET_1:
  962. case RADEON_PP_TXOFFSET_2:
  963. case R300_TX_OFFSET_0:
  964. case R300_TX_OFFSET_0+4:
  965. case R300_TX_OFFSET_0+8:
  966. case R300_TX_OFFSET_0+12:
  967. case R300_TX_OFFSET_0+16:
  968. case R300_TX_OFFSET_0+20:
  969. case R300_TX_OFFSET_0+24:
  970. case R300_TX_OFFSET_0+28:
  971. case R300_TX_OFFSET_0+32:
  972. case R300_TX_OFFSET_0+36:
  973. case R300_TX_OFFSET_0+40:
  974. case R300_TX_OFFSET_0+44:
  975. case R300_TX_OFFSET_0+48:
  976. case R300_TX_OFFSET_0+52:
  977. case R300_TX_OFFSET_0+56:
  978. case R300_TX_OFFSET_0+60:
  979. /* rn50 has no 3D engine so fail on any 3d setup */
  980. if (ASIC_IS_RN50(p->rdev)) {
  981. DRM_ERROR("attempt to use RN50 3D engine failed\n");
  982. return -EINVAL;
  983. }
  984. r = r100_cs_packet_next_reloc(p, &reloc);
  985. if (r) {
  986. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  987. idx, reg);
  988. r100_cs_dump_packet(p, pkt);
  989. return r;
  990. }
  991. ib[idx] = ib_chunk->kdata[idx] + ((u32)reloc->lobj.gpu_offset);
  992. break;
  993. case R300_RB3D_COLORPITCH0:
  994. case RADEON_RB3D_COLORPITCH:
  995. r = r100_cs_packet_next_reloc(p, &reloc);
  996. if (r) {
  997. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  998. idx, reg);
  999. r100_cs_dump_packet(p, pkt);
  1000. return r;
  1001. }
  1002. if (reloc->lobj.tiling_flags & RADEON_TILING_MACRO)
  1003. tile_flags |= RADEON_COLOR_TILE_ENABLE;
  1004. if (reloc->lobj.tiling_flags & RADEON_TILING_MICRO)
  1005. tile_flags |= RADEON_COLOR_MICROTILE_ENABLE;
  1006. tmp = ib_chunk->kdata[idx] & ~(0x7 << 16);
  1007. tmp |= tile_flags;
  1008. ib[idx] = tmp;
  1009. break;
  1010. case RADEON_RB3D_ZPASS_ADDR:
  1011. r = r100_cs_packet_next_reloc(p, &reloc);
  1012. if (r) {
  1013. DRM_ERROR("No reloc for ib[%d]=0x%04X\n",
  1014. idx, reg);
  1015. r100_cs_dump_packet(p, pkt);
  1016. return r;
  1017. }
  1018. ib[idx] = ib_chunk->kdata[idx] + ((u32)reloc->lobj.gpu_offset);
  1019. break;
  1020. default:
  1021. /* FIXME: we don't want to allow anyothers packet */
  1022. break;
  1023. }
  1024. if (onereg) {
  1025. /* FIXME: forbid onereg write to register on relocate */
  1026. break;
  1027. }
  1028. }
  1029. return 0;
  1030. }
  1031. int r100_cs_track_check_pkt3_indx_buffer(struct radeon_cs_parser *p,
  1032. struct radeon_cs_packet *pkt,
  1033. struct radeon_object *robj)
  1034. {
  1035. struct radeon_cs_chunk *ib_chunk;
  1036. unsigned idx;
  1037. ib_chunk = &p->chunks[p->chunk_ib_idx];
  1038. idx = pkt->idx + 1;
  1039. if ((ib_chunk->kdata[idx+2] + 1) > radeon_object_size(robj)) {
  1040. DRM_ERROR("[drm] Buffer too small for PACKET3 INDX_BUFFER "
  1041. "(need %u have %lu) !\n",
  1042. ib_chunk->kdata[idx+2] + 1,
  1043. radeon_object_size(robj));
  1044. return -EINVAL;
  1045. }
  1046. return 0;
  1047. }
  1048. static int r100_packet3_check(struct radeon_cs_parser *p,
  1049. struct radeon_cs_packet *pkt)
  1050. {
  1051. struct radeon_cs_chunk *ib_chunk;
  1052. struct radeon_cs_reloc *reloc;
  1053. unsigned idx;
  1054. unsigned i, c;
  1055. volatile uint32_t *ib;
  1056. int r;
  1057. ib = p->ib->ptr;
  1058. ib_chunk = &p->chunks[p->chunk_ib_idx];
  1059. idx = pkt->idx + 1;
  1060. switch (pkt->opcode) {
  1061. case PACKET3_3D_LOAD_VBPNTR:
  1062. c = ib_chunk->kdata[idx++];
  1063. for (i = 0; i < (c - 1); i += 2, idx += 3) {
  1064. r = r100_cs_packet_next_reloc(p, &reloc);
  1065. if (r) {
  1066. DRM_ERROR("No reloc for packet3 %d\n",
  1067. pkt->opcode);
  1068. r100_cs_dump_packet(p, pkt);
  1069. return r;
  1070. }
  1071. ib[idx+1] = ib_chunk->kdata[idx+1] + ((u32)reloc->lobj.gpu_offset);
  1072. r = r100_cs_packet_next_reloc(p, &reloc);
  1073. if (r) {
  1074. DRM_ERROR("No reloc for packet3 %d\n",
  1075. pkt->opcode);
  1076. r100_cs_dump_packet(p, pkt);
  1077. return r;
  1078. }
  1079. ib[idx+2] = ib_chunk->kdata[idx+2] + ((u32)reloc->lobj.gpu_offset);
  1080. }
  1081. if (c & 1) {
  1082. r = r100_cs_packet_next_reloc(p, &reloc);
  1083. if (r) {
  1084. DRM_ERROR("No reloc for packet3 %d\n",
  1085. pkt->opcode);
  1086. r100_cs_dump_packet(p, pkt);
  1087. return r;
  1088. }
  1089. ib[idx+1] = ib_chunk->kdata[idx+1] + ((u32)reloc->lobj.gpu_offset);
  1090. }
  1091. break;
  1092. case PACKET3_INDX_BUFFER:
  1093. r = r100_cs_packet_next_reloc(p, &reloc);
  1094. if (r) {
  1095. DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
  1096. r100_cs_dump_packet(p, pkt);
  1097. return r;
  1098. }
  1099. ib[idx+1] = ib_chunk->kdata[idx+1] + ((u32)reloc->lobj.gpu_offset);
  1100. r = r100_cs_track_check_pkt3_indx_buffer(p, pkt, reloc->robj);
  1101. if (r) {
  1102. return r;
  1103. }
  1104. break;
  1105. case 0x23:
  1106. /* FIXME: cleanup */
  1107. /* 3D_RNDR_GEN_INDX_PRIM on r100/r200 */
  1108. r = r100_cs_packet_next_reloc(p, &reloc);
  1109. if (r) {
  1110. DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
  1111. r100_cs_dump_packet(p, pkt);
  1112. return r;
  1113. }
  1114. ib[idx] = ib_chunk->kdata[idx] + ((u32)reloc->lobj.gpu_offset);
  1115. break;
  1116. case PACKET3_3D_DRAW_IMMD:
  1117. /* triggers drawing using in-packet vertex data */
  1118. case PACKET3_3D_DRAW_IMMD_2:
  1119. /* triggers drawing using in-packet vertex data */
  1120. case PACKET3_3D_DRAW_VBUF_2:
  1121. /* triggers drawing of vertex buffers setup elsewhere */
  1122. case PACKET3_3D_DRAW_INDX_2:
  1123. /* triggers drawing using indices to vertex buffer */
  1124. case PACKET3_3D_DRAW_VBUF:
  1125. /* triggers drawing of vertex buffers setup elsewhere */
  1126. case PACKET3_3D_DRAW_INDX:
  1127. /* triggers drawing using indices to vertex buffer */
  1128. case PACKET3_NOP:
  1129. break;
  1130. default:
  1131. DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
  1132. return -EINVAL;
  1133. }
  1134. return 0;
  1135. }
  1136. int r100_cs_parse(struct radeon_cs_parser *p)
  1137. {
  1138. struct radeon_cs_packet pkt;
  1139. int r;
  1140. do {
  1141. r = r100_cs_packet_parse(p, &pkt, p->idx);
  1142. if (r) {
  1143. return r;
  1144. }
  1145. p->idx += pkt.count + 2;
  1146. switch (pkt.type) {
  1147. case PACKET_TYPE0:
  1148. r = r100_packet0_check(p, &pkt);
  1149. break;
  1150. case PACKET_TYPE2:
  1151. break;
  1152. case PACKET_TYPE3:
  1153. r = r100_packet3_check(p, &pkt);
  1154. break;
  1155. default:
  1156. DRM_ERROR("Unknown packet type %d !\n",
  1157. pkt.type);
  1158. return -EINVAL;
  1159. }
  1160. if (r) {
  1161. return r;
  1162. }
  1163. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  1164. return 0;
  1165. }
  1166. /*
  1167. * Global GPU functions
  1168. */
  1169. void r100_errata(struct radeon_device *rdev)
  1170. {
  1171. rdev->pll_errata = 0;
  1172. if (rdev->family == CHIP_RV200 || rdev->family == CHIP_RS200) {
  1173. rdev->pll_errata |= CHIP_ERRATA_PLL_DUMMYREADS;
  1174. }
  1175. if (rdev->family == CHIP_RV100 ||
  1176. rdev->family == CHIP_RS100 ||
  1177. rdev->family == CHIP_RS200) {
  1178. rdev->pll_errata |= CHIP_ERRATA_PLL_DELAY;
  1179. }
  1180. }
  1181. /* Wait for vertical sync on primary CRTC */
  1182. void r100_gpu_wait_for_vsync(struct radeon_device *rdev)
  1183. {
  1184. uint32_t crtc_gen_cntl, tmp;
  1185. int i;
  1186. crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL);
  1187. if ((crtc_gen_cntl & RADEON_CRTC_DISP_REQ_EN_B) ||
  1188. !(crtc_gen_cntl & RADEON_CRTC_EN)) {
  1189. return;
  1190. }
  1191. /* Clear the CRTC_VBLANK_SAVE bit */
  1192. WREG32(RADEON_CRTC_STATUS, RADEON_CRTC_VBLANK_SAVE_CLEAR);
  1193. for (i = 0; i < rdev->usec_timeout; i++) {
  1194. tmp = RREG32(RADEON_CRTC_STATUS);
  1195. if (tmp & RADEON_CRTC_VBLANK_SAVE) {
  1196. return;
  1197. }
  1198. DRM_UDELAY(1);
  1199. }
  1200. }
  1201. /* Wait for vertical sync on secondary CRTC */
  1202. void r100_gpu_wait_for_vsync2(struct radeon_device *rdev)
  1203. {
  1204. uint32_t crtc2_gen_cntl, tmp;
  1205. int i;
  1206. crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL);
  1207. if ((crtc2_gen_cntl & RADEON_CRTC2_DISP_REQ_EN_B) ||
  1208. !(crtc2_gen_cntl & RADEON_CRTC2_EN))
  1209. return;
  1210. /* Clear the CRTC_VBLANK_SAVE bit */
  1211. WREG32(RADEON_CRTC2_STATUS, RADEON_CRTC2_VBLANK_SAVE_CLEAR);
  1212. for (i = 0; i < rdev->usec_timeout; i++) {
  1213. tmp = RREG32(RADEON_CRTC2_STATUS);
  1214. if (tmp & RADEON_CRTC2_VBLANK_SAVE) {
  1215. return;
  1216. }
  1217. DRM_UDELAY(1);
  1218. }
  1219. }
  1220. int r100_rbbm_fifo_wait_for_entry(struct radeon_device *rdev, unsigned n)
  1221. {
  1222. unsigned i;
  1223. uint32_t tmp;
  1224. for (i = 0; i < rdev->usec_timeout; i++) {
  1225. tmp = RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_FIFOCNT_MASK;
  1226. if (tmp >= n) {
  1227. return 0;
  1228. }
  1229. DRM_UDELAY(1);
  1230. }
  1231. return -1;
  1232. }
  1233. int r100_gui_wait_for_idle(struct radeon_device *rdev)
  1234. {
  1235. unsigned i;
  1236. uint32_t tmp;
  1237. if (r100_rbbm_fifo_wait_for_entry(rdev, 64)) {
  1238. printk(KERN_WARNING "radeon: wait for empty RBBM fifo failed !"
  1239. " Bad things might happen.\n");
  1240. }
  1241. for (i = 0; i < rdev->usec_timeout; i++) {
  1242. tmp = RREG32(RADEON_RBBM_STATUS);
  1243. if (!(tmp & (1 << 31))) {
  1244. return 0;
  1245. }
  1246. DRM_UDELAY(1);
  1247. }
  1248. return -1;
  1249. }
  1250. int r100_mc_wait_for_idle(struct radeon_device *rdev)
  1251. {
  1252. unsigned i;
  1253. uint32_t tmp;
  1254. for (i = 0; i < rdev->usec_timeout; i++) {
  1255. /* read MC_STATUS */
  1256. tmp = RREG32(0x0150);
  1257. if (tmp & (1 << 2)) {
  1258. return 0;
  1259. }
  1260. DRM_UDELAY(1);
  1261. }
  1262. return -1;
  1263. }
  1264. void r100_gpu_init(struct radeon_device *rdev)
  1265. {
  1266. /* TODO: anythings to do here ? pipes ? */
  1267. r100_hdp_reset(rdev);
  1268. }
  1269. void r100_hdp_reset(struct radeon_device *rdev)
  1270. {
  1271. uint32_t tmp;
  1272. tmp = RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL;
  1273. tmp |= (7 << 28);
  1274. WREG32(RADEON_HOST_PATH_CNTL, tmp | RADEON_HDP_SOFT_RESET | RADEON_HDP_READ_BUFFER_INVALIDATE);
  1275. (void)RREG32(RADEON_HOST_PATH_CNTL);
  1276. udelay(200);
  1277. WREG32(RADEON_RBBM_SOFT_RESET, 0);
  1278. WREG32(RADEON_HOST_PATH_CNTL, tmp);
  1279. (void)RREG32(RADEON_HOST_PATH_CNTL);
  1280. }
  1281. int r100_rb2d_reset(struct radeon_device *rdev)
  1282. {
  1283. uint32_t tmp;
  1284. int i;
  1285. WREG32(RADEON_RBBM_SOFT_RESET, RADEON_SOFT_RESET_E2);
  1286. (void)RREG32(RADEON_RBBM_SOFT_RESET);
  1287. udelay(200);
  1288. WREG32(RADEON_RBBM_SOFT_RESET, 0);
  1289. /* Wait to prevent race in RBBM_STATUS */
  1290. mdelay(1);
  1291. for (i = 0; i < rdev->usec_timeout; i++) {
  1292. tmp = RREG32(RADEON_RBBM_STATUS);
  1293. if (!(tmp & (1 << 26))) {
  1294. DRM_INFO("RB2D reset succeed (RBBM_STATUS=0x%08X)\n",
  1295. tmp);
  1296. return 0;
  1297. }
  1298. DRM_UDELAY(1);
  1299. }
  1300. tmp = RREG32(RADEON_RBBM_STATUS);
  1301. DRM_ERROR("Failed to reset RB2D (RBBM_STATUS=0x%08X)!\n", tmp);
  1302. return -1;
  1303. }
  1304. int r100_gpu_reset(struct radeon_device *rdev)
  1305. {
  1306. uint32_t status;
  1307. /* reset order likely matter */
  1308. status = RREG32(RADEON_RBBM_STATUS);
  1309. /* reset HDP */
  1310. r100_hdp_reset(rdev);
  1311. /* reset rb2d */
  1312. if (status & ((1 << 17) | (1 << 18) | (1 << 27))) {
  1313. r100_rb2d_reset(rdev);
  1314. }
  1315. /* TODO: reset 3D engine */
  1316. /* reset CP */
  1317. status = RREG32(RADEON_RBBM_STATUS);
  1318. if (status & (1 << 16)) {
  1319. r100_cp_reset(rdev);
  1320. }
  1321. /* Check if GPU is idle */
  1322. status = RREG32(RADEON_RBBM_STATUS);
  1323. if (status & (1 << 31)) {
  1324. DRM_ERROR("Failed to reset GPU (RBBM_STATUS=0x%08X)\n", status);
  1325. return -1;
  1326. }
  1327. DRM_INFO("GPU reset succeed (RBBM_STATUS=0x%08X)\n", status);
  1328. return 0;
  1329. }
  1330. /*
  1331. * VRAM info
  1332. */
  1333. static void r100_vram_get_type(struct radeon_device *rdev)
  1334. {
  1335. uint32_t tmp;
  1336. rdev->mc.vram_is_ddr = false;
  1337. if (rdev->flags & RADEON_IS_IGP)
  1338. rdev->mc.vram_is_ddr = true;
  1339. else if (RREG32(RADEON_MEM_SDRAM_MODE_REG) & RADEON_MEM_CFG_TYPE_DDR)
  1340. rdev->mc.vram_is_ddr = true;
  1341. if ((rdev->family == CHIP_RV100) ||
  1342. (rdev->family == CHIP_RS100) ||
  1343. (rdev->family == CHIP_RS200)) {
  1344. tmp = RREG32(RADEON_MEM_CNTL);
  1345. if (tmp & RV100_HALF_MODE) {
  1346. rdev->mc.vram_width = 32;
  1347. } else {
  1348. rdev->mc.vram_width = 64;
  1349. }
  1350. if (rdev->flags & RADEON_SINGLE_CRTC) {
  1351. rdev->mc.vram_width /= 4;
  1352. rdev->mc.vram_is_ddr = true;
  1353. }
  1354. } else if (rdev->family <= CHIP_RV280) {
  1355. tmp = RREG32(RADEON_MEM_CNTL);
  1356. if (tmp & RADEON_MEM_NUM_CHANNELS_MASK) {
  1357. rdev->mc.vram_width = 128;
  1358. } else {
  1359. rdev->mc.vram_width = 64;
  1360. }
  1361. } else {
  1362. /* newer IGPs */
  1363. rdev->mc.vram_width = 128;
  1364. }
  1365. }
  1366. static u32 r100_get_accessible_vram(struct radeon_device *rdev)
  1367. {
  1368. u32 aper_size;
  1369. u8 byte;
  1370. aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
  1371. /* Set HDP_APER_CNTL only on cards that are known not to be broken,
  1372. * that is has the 2nd generation multifunction PCI interface
  1373. */
  1374. if (rdev->family == CHIP_RV280 ||
  1375. rdev->family >= CHIP_RV350) {
  1376. WREG32_P(RADEON_HOST_PATH_CNTL, RADEON_HDP_APER_CNTL,
  1377. ~RADEON_HDP_APER_CNTL);
  1378. DRM_INFO("Generation 2 PCI interface, using max accessible memory\n");
  1379. return aper_size * 2;
  1380. }
  1381. /* Older cards have all sorts of funny issues to deal with. First
  1382. * check if it's a multifunction card by reading the PCI config
  1383. * header type... Limit those to one aperture size
  1384. */
  1385. pci_read_config_byte(rdev->pdev, 0xe, &byte);
  1386. if (byte & 0x80) {
  1387. DRM_INFO("Generation 1 PCI interface in multifunction mode\n");
  1388. DRM_INFO("Limiting VRAM to one aperture\n");
  1389. return aper_size;
  1390. }
  1391. /* Single function older card. We read HDP_APER_CNTL to see how the BIOS
  1392. * have set it up. We don't write this as it's broken on some ASICs but
  1393. * we expect the BIOS to have done the right thing (might be too optimistic...)
  1394. */
  1395. if (RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL)
  1396. return aper_size * 2;
  1397. return aper_size;
  1398. }
  1399. void r100_vram_init_sizes(struct radeon_device *rdev)
  1400. {
  1401. u64 config_aper_size;
  1402. u32 accessible;
  1403. config_aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
  1404. if (rdev->flags & RADEON_IS_IGP) {
  1405. uint32_t tom;
  1406. /* read NB_TOM to get the amount of ram stolen for the GPU */
  1407. tom = RREG32(RADEON_NB_TOM);
  1408. rdev->mc.real_vram_size = (((tom >> 16) - (tom & 0xffff) + 1) << 16);
  1409. /* for IGPs we need to keep VRAM where it was put by the BIOS */
  1410. rdev->mc.vram_location = (tom & 0xffff) << 16;
  1411. WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
  1412. rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
  1413. } else {
  1414. rdev->mc.real_vram_size = RREG32(RADEON_CONFIG_MEMSIZE);
  1415. /* Some production boards of m6 will report 0
  1416. * if it's 8 MB
  1417. */
  1418. if (rdev->mc.real_vram_size == 0) {
  1419. rdev->mc.real_vram_size = 8192 * 1024;
  1420. WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
  1421. }
  1422. /* let driver place VRAM */
  1423. rdev->mc.vram_location = 0xFFFFFFFFUL;
  1424. /* Fix for RN50, M6, M7 with 8/16/32(??) MBs of VRAM -
  1425. * Novell bug 204882 + along with lots of ubuntu ones */
  1426. if (config_aper_size > rdev->mc.real_vram_size)
  1427. rdev->mc.mc_vram_size = config_aper_size;
  1428. else
  1429. rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
  1430. }
  1431. /* work out accessible VRAM */
  1432. accessible = r100_get_accessible_vram(rdev);
  1433. rdev->mc.aper_base = drm_get_resource_start(rdev->ddev, 0);
  1434. rdev->mc.aper_size = drm_get_resource_len(rdev->ddev, 0);
  1435. if (accessible > rdev->mc.aper_size)
  1436. accessible = rdev->mc.aper_size;
  1437. if (rdev->mc.mc_vram_size > rdev->mc.aper_size)
  1438. rdev->mc.mc_vram_size = rdev->mc.aper_size;
  1439. if (rdev->mc.real_vram_size > rdev->mc.aper_size)
  1440. rdev->mc.real_vram_size = rdev->mc.aper_size;
  1441. }
  1442. void r100_vram_info(struct radeon_device *rdev)
  1443. {
  1444. r100_vram_get_type(rdev);
  1445. r100_vram_init_sizes(rdev);
  1446. }
  1447. /*
  1448. * Indirect registers accessor
  1449. */
  1450. void r100_pll_errata_after_index(struct radeon_device *rdev)
  1451. {
  1452. if (!(rdev->pll_errata & CHIP_ERRATA_PLL_DUMMYREADS)) {
  1453. return;
  1454. }
  1455. (void)RREG32(RADEON_CLOCK_CNTL_DATA);
  1456. (void)RREG32(RADEON_CRTC_GEN_CNTL);
  1457. }
  1458. static void r100_pll_errata_after_data(struct radeon_device *rdev)
  1459. {
  1460. /* This workarounds is necessary on RV100, RS100 and RS200 chips
  1461. * or the chip could hang on a subsequent access
  1462. */
  1463. if (rdev->pll_errata & CHIP_ERRATA_PLL_DELAY) {
  1464. udelay(5000);
  1465. }
  1466. /* This function is required to workaround a hardware bug in some (all?)
  1467. * revisions of the R300. This workaround should be called after every
  1468. * CLOCK_CNTL_INDEX register access. If not, register reads afterward
  1469. * may not be correct.
  1470. */
  1471. if (rdev->pll_errata & CHIP_ERRATA_R300_CG) {
  1472. uint32_t save, tmp;
  1473. save = RREG32(RADEON_CLOCK_CNTL_INDEX);
  1474. tmp = save & ~(0x3f | RADEON_PLL_WR_EN);
  1475. WREG32(RADEON_CLOCK_CNTL_INDEX, tmp);
  1476. tmp = RREG32(RADEON_CLOCK_CNTL_DATA);
  1477. WREG32(RADEON_CLOCK_CNTL_INDEX, save);
  1478. }
  1479. }
  1480. uint32_t r100_pll_rreg(struct radeon_device *rdev, uint32_t reg)
  1481. {
  1482. uint32_t data;
  1483. WREG8(RADEON_CLOCK_CNTL_INDEX, reg & 0x3f);
  1484. r100_pll_errata_after_index(rdev);
  1485. data = RREG32(RADEON_CLOCK_CNTL_DATA);
  1486. r100_pll_errata_after_data(rdev);
  1487. return data;
  1488. }
  1489. void r100_pll_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
  1490. {
  1491. WREG8(RADEON_CLOCK_CNTL_INDEX, ((reg & 0x3f) | RADEON_PLL_WR_EN));
  1492. r100_pll_errata_after_index(rdev);
  1493. WREG32(RADEON_CLOCK_CNTL_DATA, v);
  1494. r100_pll_errata_after_data(rdev);
  1495. }
  1496. int r100_init(struct radeon_device *rdev)
  1497. {
  1498. return 0;
  1499. }
  1500. /*
  1501. * Debugfs info
  1502. */
  1503. #if defined(CONFIG_DEBUG_FS)
  1504. static int r100_debugfs_rbbm_info(struct seq_file *m, void *data)
  1505. {
  1506. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1507. struct drm_device *dev = node->minor->dev;
  1508. struct radeon_device *rdev = dev->dev_private;
  1509. uint32_t reg, value;
  1510. unsigned i;
  1511. seq_printf(m, "RBBM_STATUS 0x%08x\n", RREG32(RADEON_RBBM_STATUS));
  1512. seq_printf(m, "RBBM_CMDFIFO_STAT 0x%08x\n", RREG32(0xE7C));
  1513. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  1514. for (i = 0; i < 64; i++) {
  1515. WREG32(RADEON_RBBM_CMDFIFO_ADDR, i | 0x100);
  1516. reg = (RREG32(RADEON_RBBM_CMDFIFO_DATA) - 1) >> 2;
  1517. WREG32(RADEON_RBBM_CMDFIFO_ADDR, i);
  1518. value = RREG32(RADEON_RBBM_CMDFIFO_DATA);
  1519. seq_printf(m, "[0x%03X] 0x%04X=0x%08X\n", i, reg, value);
  1520. }
  1521. return 0;
  1522. }
  1523. static int r100_debugfs_cp_ring_info(struct seq_file *m, void *data)
  1524. {
  1525. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1526. struct drm_device *dev = node->minor->dev;
  1527. struct radeon_device *rdev = dev->dev_private;
  1528. uint32_t rdp, wdp;
  1529. unsigned count, i, j;
  1530. radeon_ring_free_size(rdev);
  1531. rdp = RREG32(RADEON_CP_RB_RPTR);
  1532. wdp = RREG32(RADEON_CP_RB_WPTR);
  1533. count = (rdp + rdev->cp.ring_size - wdp) & rdev->cp.ptr_mask;
  1534. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  1535. seq_printf(m, "CP_RB_WPTR 0x%08x\n", wdp);
  1536. seq_printf(m, "CP_RB_RPTR 0x%08x\n", rdp);
  1537. seq_printf(m, "%u free dwords in ring\n", rdev->cp.ring_free_dw);
  1538. seq_printf(m, "%u dwords in ring\n", count);
  1539. for (j = 0; j <= count; j++) {
  1540. i = (rdp + j) & rdev->cp.ptr_mask;
  1541. seq_printf(m, "r[%04d]=0x%08x\n", i, rdev->cp.ring[i]);
  1542. }
  1543. return 0;
  1544. }
  1545. static int r100_debugfs_cp_csq_fifo(struct seq_file *m, void *data)
  1546. {
  1547. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1548. struct drm_device *dev = node->minor->dev;
  1549. struct radeon_device *rdev = dev->dev_private;
  1550. uint32_t csq_stat, csq2_stat, tmp;
  1551. unsigned r_rptr, r_wptr, ib1_rptr, ib1_wptr, ib2_rptr, ib2_wptr;
  1552. unsigned i;
  1553. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  1554. seq_printf(m, "CP_CSQ_MODE 0x%08x\n", RREG32(RADEON_CP_CSQ_MODE));
  1555. csq_stat = RREG32(RADEON_CP_CSQ_STAT);
  1556. csq2_stat = RREG32(RADEON_CP_CSQ2_STAT);
  1557. r_rptr = (csq_stat >> 0) & 0x3ff;
  1558. r_wptr = (csq_stat >> 10) & 0x3ff;
  1559. ib1_rptr = (csq_stat >> 20) & 0x3ff;
  1560. ib1_wptr = (csq2_stat >> 0) & 0x3ff;
  1561. ib2_rptr = (csq2_stat >> 10) & 0x3ff;
  1562. ib2_wptr = (csq2_stat >> 20) & 0x3ff;
  1563. seq_printf(m, "CP_CSQ_STAT 0x%08x\n", csq_stat);
  1564. seq_printf(m, "CP_CSQ2_STAT 0x%08x\n", csq2_stat);
  1565. seq_printf(m, "Ring rptr %u\n", r_rptr);
  1566. seq_printf(m, "Ring wptr %u\n", r_wptr);
  1567. seq_printf(m, "Indirect1 rptr %u\n", ib1_rptr);
  1568. seq_printf(m, "Indirect1 wptr %u\n", ib1_wptr);
  1569. seq_printf(m, "Indirect2 rptr %u\n", ib2_rptr);
  1570. seq_printf(m, "Indirect2 wptr %u\n", ib2_wptr);
  1571. /* FIXME: 0, 128, 640 depends on fifo setup see cp_init_kms
  1572. * 128 = indirect1_start * 8 & 640 = indirect2_start * 8 */
  1573. seq_printf(m, "Ring fifo:\n");
  1574. for (i = 0; i < 256; i++) {
  1575. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  1576. tmp = RREG32(RADEON_CP_CSQ_DATA);
  1577. seq_printf(m, "rfifo[%04d]=0x%08X\n", i, tmp);
  1578. }
  1579. seq_printf(m, "Indirect1 fifo:\n");
  1580. for (i = 256; i <= 512; i++) {
  1581. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  1582. tmp = RREG32(RADEON_CP_CSQ_DATA);
  1583. seq_printf(m, "ib1fifo[%04d]=0x%08X\n", i, tmp);
  1584. }
  1585. seq_printf(m, "Indirect2 fifo:\n");
  1586. for (i = 640; i < ib1_wptr; i++) {
  1587. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  1588. tmp = RREG32(RADEON_CP_CSQ_DATA);
  1589. seq_printf(m, "ib2fifo[%04d]=0x%08X\n", i, tmp);
  1590. }
  1591. return 0;
  1592. }
  1593. static int r100_debugfs_mc_info(struct seq_file *m, void *data)
  1594. {
  1595. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1596. struct drm_device *dev = node->minor->dev;
  1597. struct radeon_device *rdev = dev->dev_private;
  1598. uint32_t tmp;
  1599. tmp = RREG32(RADEON_CONFIG_MEMSIZE);
  1600. seq_printf(m, "CONFIG_MEMSIZE 0x%08x\n", tmp);
  1601. tmp = RREG32(RADEON_MC_FB_LOCATION);
  1602. seq_printf(m, "MC_FB_LOCATION 0x%08x\n", tmp);
  1603. tmp = RREG32(RADEON_BUS_CNTL);
  1604. seq_printf(m, "BUS_CNTL 0x%08x\n", tmp);
  1605. tmp = RREG32(RADEON_MC_AGP_LOCATION);
  1606. seq_printf(m, "MC_AGP_LOCATION 0x%08x\n", tmp);
  1607. tmp = RREG32(RADEON_AGP_BASE);
  1608. seq_printf(m, "AGP_BASE 0x%08x\n", tmp);
  1609. tmp = RREG32(RADEON_HOST_PATH_CNTL);
  1610. seq_printf(m, "HOST_PATH_CNTL 0x%08x\n", tmp);
  1611. tmp = RREG32(0x01D0);
  1612. seq_printf(m, "AIC_CTRL 0x%08x\n", tmp);
  1613. tmp = RREG32(RADEON_AIC_LO_ADDR);
  1614. seq_printf(m, "AIC_LO_ADDR 0x%08x\n", tmp);
  1615. tmp = RREG32(RADEON_AIC_HI_ADDR);
  1616. seq_printf(m, "AIC_HI_ADDR 0x%08x\n", tmp);
  1617. tmp = RREG32(0x01E4);
  1618. seq_printf(m, "AIC_TLB_ADDR 0x%08x\n", tmp);
  1619. return 0;
  1620. }
  1621. static struct drm_info_list r100_debugfs_rbbm_list[] = {
  1622. {"r100_rbbm_info", r100_debugfs_rbbm_info, 0, NULL},
  1623. };
  1624. static struct drm_info_list r100_debugfs_cp_list[] = {
  1625. {"r100_cp_ring_info", r100_debugfs_cp_ring_info, 0, NULL},
  1626. {"r100_cp_csq_fifo", r100_debugfs_cp_csq_fifo, 0, NULL},
  1627. };
  1628. static struct drm_info_list r100_debugfs_mc_info_list[] = {
  1629. {"r100_mc_info", r100_debugfs_mc_info, 0, NULL},
  1630. };
  1631. #endif
  1632. int r100_debugfs_rbbm_init(struct radeon_device *rdev)
  1633. {
  1634. #if defined(CONFIG_DEBUG_FS)
  1635. return radeon_debugfs_add_files(rdev, r100_debugfs_rbbm_list, 1);
  1636. #else
  1637. return 0;
  1638. #endif
  1639. }
  1640. int r100_debugfs_cp_init(struct radeon_device *rdev)
  1641. {
  1642. #if defined(CONFIG_DEBUG_FS)
  1643. return radeon_debugfs_add_files(rdev, r100_debugfs_cp_list, 2);
  1644. #else
  1645. return 0;
  1646. #endif
  1647. }
  1648. int r100_debugfs_mc_info_init(struct radeon_device *rdev)
  1649. {
  1650. #if defined(CONFIG_DEBUG_FS)
  1651. return radeon_debugfs_add_files(rdev, r100_debugfs_mc_info_list, 1);
  1652. #else
  1653. return 0;
  1654. #endif
  1655. }
  1656. int r100_set_surface_reg(struct radeon_device *rdev, int reg,
  1657. uint32_t tiling_flags, uint32_t pitch,
  1658. uint32_t offset, uint32_t obj_size)
  1659. {
  1660. int surf_index = reg * 16;
  1661. int flags = 0;
  1662. /* r100/r200 divide by 16 */
  1663. if (rdev->family < CHIP_R300)
  1664. flags = pitch / 16;
  1665. else
  1666. flags = pitch / 8;
  1667. if (rdev->family <= CHIP_RS200) {
  1668. if ((tiling_flags & (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
  1669. == (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
  1670. flags |= RADEON_SURF_TILE_COLOR_BOTH;
  1671. if (tiling_flags & RADEON_TILING_MACRO)
  1672. flags |= RADEON_SURF_TILE_COLOR_MACRO;
  1673. } else if (rdev->family <= CHIP_RV280) {
  1674. if (tiling_flags & (RADEON_TILING_MACRO))
  1675. flags |= R200_SURF_TILE_COLOR_MACRO;
  1676. if (tiling_flags & RADEON_TILING_MICRO)
  1677. flags |= R200_SURF_TILE_COLOR_MICRO;
  1678. } else {
  1679. if (tiling_flags & RADEON_TILING_MACRO)
  1680. flags |= R300_SURF_TILE_MACRO;
  1681. if (tiling_flags & RADEON_TILING_MICRO)
  1682. flags |= R300_SURF_TILE_MICRO;
  1683. }
  1684. DRM_DEBUG("writing surface %d %d %x %x\n", reg, flags, offset, offset+obj_size-1);
  1685. WREG32(RADEON_SURFACE0_INFO + surf_index, flags);
  1686. WREG32(RADEON_SURFACE0_LOWER_BOUND + surf_index, offset);
  1687. WREG32(RADEON_SURFACE0_UPPER_BOUND + surf_index, offset + obj_size - 1);
  1688. return 0;
  1689. }
  1690. void r100_clear_surface_reg(struct radeon_device *rdev, int reg)
  1691. {
  1692. int surf_index = reg * 16;
  1693. WREG32(RADEON_SURFACE0_INFO + surf_index, 0);
  1694. }
  1695. void r100_bandwidth_update(struct radeon_device *rdev)
  1696. {
  1697. fixed20_12 trcd_ff, trp_ff, tras_ff, trbs_ff, tcas_ff;
  1698. fixed20_12 sclk_ff, mclk_ff, sclk_eff_ff, sclk_delay_ff;
  1699. fixed20_12 peak_disp_bw, mem_bw, pix_clk, pix_clk2, temp_ff, crit_point_ff;
  1700. uint32_t temp, data, mem_trcd, mem_trp, mem_tras;
  1701. fixed20_12 memtcas_ff[8] = {
  1702. fixed_init(1),
  1703. fixed_init(2),
  1704. fixed_init(3),
  1705. fixed_init(0),
  1706. fixed_init_half(1),
  1707. fixed_init_half(2),
  1708. fixed_init(0),
  1709. };
  1710. fixed20_12 memtcas_rs480_ff[8] = {
  1711. fixed_init(0),
  1712. fixed_init(1),
  1713. fixed_init(2),
  1714. fixed_init(3),
  1715. fixed_init(0),
  1716. fixed_init_half(1),
  1717. fixed_init_half(2),
  1718. fixed_init_half(3),
  1719. };
  1720. fixed20_12 memtcas2_ff[8] = {
  1721. fixed_init(0),
  1722. fixed_init(1),
  1723. fixed_init(2),
  1724. fixed_init(3),
  1725. fixed_init(4),
  1726. fixed_init(5),
  1727. fixed_init(6),
  1728. fixed_init(7),
  1729. };
  1730. fixed20_12 memtrbs[8] = {
  1731. fixed_init(1),
  1732. fixed_init_half(1),
  1733. fixed_init(2),
  1734. fixed_init_half(2),
  1735. fixed_init(3),
  1736. fixed_init_half(3),
  1737. fixed_init(4),
  1738. fixed_init_half(4)
  1739. };
  1740. fixed20_12 memtrbs_r4xx[8] = {
  1741. fixed_init(4),
  1742. fixed_init(5),
  1743. fixed_init(6),
  1744. fixed_init(7),
  1745. fixed_init(8),
  1746. fixed_init(9),
  1747. fixed_init(10),
  1748. fixed_init(11)
  1749. };
  1750. fixed20_12 min_mem_eff;
  1751. fixed20_12 mc_latency_sclk, mc_latency_mclk, k1;
  1752. fixed20_12 cur_latency_mclk, cur_latency_sclk;
  1753. fixed20_12 disp_latency, disp_latency_overhead, disp_drain_rate,
  1754. disp_drain_rate2, read_return_rate;
  1755. fixed20_12 time_disp1_drop_priority;
  1756. int c;
  1757. int cur_size = 16; /* in octawords */
  1758. int critical_point = 0, critical_point2;
  1759. /* uint32_t read_return_rate, time_disp1_drop_priority; */
  1760. int stop_req, max_stop_req;
  1761. struct drm_display_mode *mode1 = NULL;
  1762. struct drm_display_mode *mode2 = NULL;
  1763. uint32_t pixel_bytes1 = 0;
  1764. uint32_t pixel_bytes2 = 0;
  1765. if (rdev->mode_info.crtcs[0]->base.enabled) {
  1766. mode1 = &rdev->mode_info.crtcs[0]->base.mode;
  1767. pixel_bytes1 = rdev->mode_info.crtcs[0]->base.fb->bits_per_pixel / 8;
  1768. }
  1769. if (rdev->mode_info.crtcs[1]->base.enabled) {
  1770. mode2 = &rdev->mode_info.crtcs[1]->base.mode;
  1771. pixel_bytes2 = rdev->mode_info.crtcs[1]->base.fb->bits_per_pixel / 8;
  1772. }
  1773. min_mem_eff.full = rfixed_const_8(0);
  1774. /* get modes */
  1775. if ((rdev->disp_priority == 2) && ASIC_IS_R300(rdev)) {
  1776. uint32_t mc_init_misc_lat_timer = RREG32(R300_MC_INIT_MISC_LAT_TIMER);
  1777. mc_init_misc_lat_timer &= ~(R300_MC_DISP1R_INIT_LAT_MASK << R300_MC_DISP1R_INIT_LAT_SHIFT);
  1778. mc_init_misc_lat_timer &= ~(R300_MC_DISP0R_INIT_LAT_MASK << R300_MC_DISP0R_INIT_LAT_SHIFT);
  1779. /* check crtc enables */
  1780. if (mode2)
  1781. mc_init_misc_lat_timer |= (1 << R300_MC_DISP1R_INIT_LAT_SHIFT);
  1782. if (mode1)
  1783. mc_init_misc_lat_timer |= (1 << R300_MC_DISP0R_INIT_LAT_SHIFT);
  1784. WREG32(R300_MC_INIT_MISC_LAT_TIMER, mc_init_misc_lat_timer);
  1785. }
  1786. /*
  1787. * determine is there is enough bw for current mode
  1788. */
  1789. mclk_ff.full = rfixed_const(rdev->clock.default_mclk);
  1790. temp_ff.full = rfixed_const(100);
  1791. mclk_ff.full = rfixed_div(mclk_ff, temp_ff);
  1792. sclk_ff.full = rfixed_const(rdev->clock.default_sclk);
  1793. sclk_ff.full = rfixed_div(sclk_ff, temp_ff);
  1794. temp = (rdev->mc.vram_width / 8) * (rdev->mc.vram_is_ddr ? 2 : 1);
  1795. temp_ff.full = rfixed_const(temp);
  1796. mem_bw.full = rfixed_mul(mclk_ff, temp_ff);
  1797. pix_clk.full = 0;
  1798. pix_clk2.full = 0;
  1799. peak_disp_bw.full = 0;
  1800. if (mode1) {
  1801. temp_ff.full = rfixed_const(1000);
  1802. pix_clk.full = rfixed_const(mode1->clock); /* convert to fixed point */
  1803. pix_clk.full = rfixed_div(pix_clk, temp_ff);
  1804. temp_ff.full = rfixed_const(pixel_bytes1);
  1805. peak_disp_bw.full += rfixed_mul(pix_clk, temp_ff);
  1806. }
  1807. if (mode2) {
  1808. temp_ff.full = rfixed_const(1000);
  1809. pix_clk2.full = rfixed_const(mode2->clock); /* convert to fixed point */
  1810. pix_clk2.full = rfixed_div(pix_clk2, temp_ff);
  1811. temp_ff.full = rfixed_const(pixel_bytes2);
  1812. peak_disp_bw.full += rfixed_mul(pix_clk2, temp_ff);
  1813. }
  1814. mem_bw.full = rfixed_mul(mem_bw, min_mem_eff);
  1815. if (peak_disp_bw.full >= mem_bw.full) {
  1816. DRM_ERROR("You may not have enough display bandwidth for current mode\n"
  1817. "If you have flickering problem, try to lower resolution, refresh rate, or color depth\n");
  1818. }
  1819. /* Get values from the EXT_MEM_CNTL register...converting its contents. */
  1820. temp = RREG32(RADEON_MEM_TIMING_CNTL);
  1821. if ((rdev->family == CHIP_RV100) || (rdev->flags & RADEON_IS_IGP)) { /* RV100, M6, IGPs */
  1822. mem_trcd = ((temp >> 2) & 0x3) + 1;
  1823. mem_trp = ((temp & 0x3)) + 1;
  1824. mem_tras = ((temp & 0x70) >> 4) + 1;
  1825. } else if (rdev->family == CHIP_R300 ||
  1826. rdev->family == CHIP_R350) { /* r300, r350 */
  1827. mem_trcd = (temp & 0x7) + 1;
  1828. mem_trp = ((temp >> 8) & 0x7) + 1;
  1829. mem_tras = ((temp >> 11) & 0xf) + 4;
  1830. } else if (rdev->family == CHIP_RV350 ||
  1831. rdev->family <= CHIP_RV380) {
  1832. /* rv3x0 */
  1833. mem_trcd = (temp & 0x7) + 3;
  1834. mem_trp = ((temp >> 8) & 0x7) + 3;
  1835. mem_tras = ((temp >> 11) & 0xf) + 6;
  1836. } else if (rdev->family == CHIP_R420 ||
  1837. rdev->family == CHIP_R423 ||
  1838. rdev->family == CHIP_RV410) {
  1839. /* r4xx */
  1840. mem_trcd = (temp & 0xf) + 3;
  1841. if (mem_trcd > 15)
  1842. mem_trcd = 15;
  1843. mem_trp = ((temp >> 8) & 0xf) + 3;
  1844. if (mem_trp > 15)
  1845. mem_trp = 15;
  1846. mem_tras = ((temp >> 12) & 0x1f) + 6;
  1847. if (mem_tras > 31)
  1848. mem_tras = 31;
  1849. } else { /* RV200, R200 */
  1850. mem_trcd = (temp & 0x7) + 1;
  1851. mem_trp = ((temp >> 8) & 0x7) + 1;
  1852. mem_tras = ((temp >> 12) & 0xf) + 4;
  1853. }
  1854. /* convert to FF */
  1855. trcd_ff.full = rfixed_const(mem_trcd);
  1856. trp_ff.full = rfixed_const(mem_trp);
  1857. tras_ff.full = rfixed_const(mem_tras);
  1858. /* Get values from the MEM_SDRAM_MODE_REG register...converting its */
  1859. temp = RREG32(RADEON_MEM_SDRAM_MODE_REG);
  1860. data = (temp & (7 << 20)) >> 20;
  1861. if ((rdev->family == CHIP_RV100) || rdev->flags & RADEON_IS_IGP) {
  1862. if (rdev->family == CHIP_RS480) /* don't think rs400 */
  1863. tcas_ff = memtcas_rs480_ff[data];
  1864. else
  1865. tcas_ff = memtcas_ff[data];
  1866. } else
  1867. tcas_ff = memtcas2_ff[data];
  1868. if (rdev->family == CHIP_RS400 ||
  1869. rdev->family == CHIP_RS480) {
  1870. /* extra cas latency stored in bits 23-25 0-4 clocks */
  1871. data = (temp >> 23) & 0x7;
  1872. if (data < 5)
  1873. tcas_ff.full += rfixed_const(data);
  1874. }
  1875. if (ASIC_IS_R300(rdev) && !(rdev->flags & RADEON_IS_IGP)) {
  1876. /* on the R300, Tcas is included in Trbs.
  1877. */
  1878. temp = RREG32(RADEON_MEM_CNTL);
  1879. data = (R300_MEM_NUM_CHANNELS_MASK & temp);
  1880. if (data == 1) {
  1881. if (R300_MEM_USE_CD_CH_ONLY & temp) {
  1882. temp = RREG32(R300_MC_IND_INDEX);
  1883. temp &= ~R300_MC_IND_ADDR_MASK;
  1884. temp |= R300_MC_READ_CNTL_CD_mcind;
  1885. WREG32(R300_MC_IND_INDEX, temp);
  1886. temp = RREG32(R300_MC_IND_DATA);
  1887. data = (R300_MEM_RBS_POSITION_C_MASK & temp);
  1888. } else {
  1889. temp = RREG32(R300_MC_READ_CNTL_AB);
  1890. data = (R300_MEM_RBS_POSITION_A_MASK & temp);
  1891. }
  1892. } else {
  1893. temp = RREG32(R300_MC_READ_CNTL_AB);
  1894. data = (R300_MEM_RBS_POSITION_A_MASK & temp);
  1895. }
  1896. if (rdev->family == CHIP_RV410 ||
  1897. rdev->family == CHIP_R420 ||
  1898. rdev->family == CHIP_R423)
  1899. trbs_ff = memtrbs_r4xx[data];
  1900. else
  1901. trbs_ff = memtrbs[data];
  1902. tcas_ff.full += trbs_ff.full;
  1903. }
  1904. sclk_eff_ff.full = sclk_ff.full;
  1905. if (rdev->flags & RADEON_IS_AGP) {
  1906. fixed20_12 agpmode_ff;
  1907. agpmode_ff.full = rfixed_const(radeon_agpmode);
  1908. temp_ff.full = rfixed_const_666(16);
  1909. sclk_eff_ff.full -= rfixed_mul(agpmode_ff, temp_ff);
  1910. }
  1911. /* TODO PCIE lanes may affect this - agpmode == 16?? */
  1912. if (ASIC_IS_R300(rdev)) {
  1913. sclk_delay_ff.full = rfixed_const(250);
  1914. } else {
  1915. if ((rdev->family == CHIP_RV100) ||
  1916. rdev->flags & RADEON_IS_IGP) {
  1917. if (rdev->mc.vram_is_ddr)
  1918. sclk_delay_ff.full = rfixed_const(41);
  1919. else
  1920. sclk_delay_ff.full = rfixed_const(33);
  1921. } else {
  1922. if (rdev->mc.vram_width == 128)
  1923. sclk_delay_ff.full = rfixed_const(57);
  1924. else
  1925. sclk_delay_ff.full = rfixed_const(41);
  1926. }
  1927. }
  1928. mc_latency_sclk.full = rfixed_div(sclk_delay_ff, sclk_eff_ff);
  1929. if (rdev->mc.vram_is_ddr) {
  1930. if (rdev->mc.vram_width == 32) {
  1931. k1.full = rfixed_const(40);
  1932. c = 3;
  1933. } else {
  1934. k1.full = rfixed_const(20);
  1935. c = 1;
  1936. }
  1937. } else {
  1938. k1.full = rfixed_const(40);
  1939. c = 3;
  1940. }
  1941. temp_ff.full = rfixed_const(2);
  1942. mc_latency_mclk.full = rfixed_mul(trcd_ff, temp_ff);
  1943. temp_ff.full = rfixed_const(c);
  1944. mc_latency_mclk.full += rfixed_mul(tcas_ff, temp_ff);
  1945. temp_ff.full = rfixed_const(4);
  1946. mc_latency_mclk.full += rfixed_mul(tras_ff, temp_ff);
  1947. mc_latency_mclk.full += rfixed_mul(trp_ff, temp_ff);
  1948. mc_latency_mclk.full += k1.full;
  1949. mc_latency_mclk.full = rfixed_div(mc_latency_mclk, mclk_ff);
  1950. mc_latency_mclk.full += rfixed_div(temp_ff, sclk_eff_ff);
  1951. /*
  1952. HW cursor time assuming worst case of full size colour cursor.
  1953. */
  1954. temp_ff.full = rfixed_const((2 * (cur_size - (rdev->mc.vram_is_ddr + 1))));
  1955. temp_ff.full += trcd_ff.full;
  1956. if (temp_ff.full < tras_ff.full)
  1957. temp_ff.full = tras_ff.full;
  1958. cur_latency_mclk.full = rfixed_div(temp_ff, mclk_ff);
  1959. temp_ff.full = rfixed_const(cur_size);
  1960. cur_latency_sclk.full = rfixed_div(temp_ff, sclk_eff_ff);
  1961. /*
  1962. Find the total latency for the display data.
  1963. */
  1964. disp_latency_overhead.full = rfixed_const(80);
  1965. disp_latency_overhead.full = rfixed_div(disp_latency_overhead, sclk_ff);
  1966. mc_latency_mclk.full += disp_latency_overhead.full + cur_latency_mclk.full;
  1967. mc_latency_sclk.full += disp_latency_overhead.full + cur_latency_sclk.full;
  1968. if (mc_latency_mclk.full > mc_latency_sclk.full)
  1969. disp_latency.full = mc_latency_mclk.full;
  1970. else
  1971. disp_latency.full = mc_latency_sclk.full;
  1972. /* setup Max GRPH_STOP_REQ default value */
  1973. if (ASIC_IS_RV100(rdev))
  1974. max_stop_req = 0x5c;
  1975. else
  1976. max_stop_req = 0x7c;
  1977. if (mode1) {
  1978. /* CRTC1
  1979. Set GRPH_BUFFER_CNTL register using h/w defined optimal values.
  1980. GRPH_STOP_REQ <= MIN[ 0x7C, (CRTC_H_DISP + 1) * (bit depth) / 0x10 ]
  1981. */
  1982. stop_req = mode1->hdisplay * pixel_bytes1 / 16;
  1983. if (stop_req > max_stop_req)
  1984. stop_req = max_stop_req;
  1985. /*
  1986. Find the drain rate of the display buffer.
  1987. */
  1988. temp_ff.full = rfixed_const((16/pixel_bytes1));
  1989. disp_drain_rate.full = rfixed_div(pix_clk, temp_ff);
  1990. /*
  1991. Find the critical point of the display buffer.
  1992. */
  1993. crit_point_ff.full = rfixed_mul(disp_drain_rate, disp_latency);
  1994. crit_point_ff.full += rfixed_const_half(0);
  1995. critical_point = rfixed_trunc(crit_point_ff);
  1996. if (rdev->disp_priority == 2) {
  1997. critical_point = 0;
  1998. }
  1999. /*
  2000. The critical point should never be above max_stop_req-4. Setting
  2001. GRPH_CRITICAL_CNTL = 0 will thus force high priority all the time.
  2002. */
  2003. if (max_stop_req - critical_point < 4)
  2004. critical_point = 0;
  2005. if (critical_point == 0 && mode2 && rdev->family == CHIP_R300) {
  2006. /* some R300 cards have problem with this set to 0, when CRTC2 is enabled.*/
  2007. critical_point = 0x10;
  2008. }
  2009. temp = RREG32(RADEON_GRPH_BUFFER_CNTL);
  2010. temp &= ~(RADEON_GRPH_STOP_REQ_MASK);
  2011. temp |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
  2012. temp &= ~(RADEON_GRPH_START_REQ_MASK);
  2013. if ((rdev->family == CHIP_R350) &&
  2014. (stop_req > 0x15)) {
  2015. stop_req -= 0x10;
  2016. }
  2017. temp |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
  2018. temp |= RADEON_GRPH_BUFFER_SIZE;
  2019. temp &= ~(RADEON_GRPH_CRITICAL_CNTL |
  2020. RADEON_GRPH_CRITICAL_AT_SOF |
  2021. RADEON_GRPH_STOP_CNTL);
  2022. /*
  2023. Write the result into the register.
  2024. */
  2025. WREG32(RADEON_GRPH_BUFFER_CNTL, ((temp & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
  2026. (critical_point << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
  2027. #if 0
  2028. if ((rdev->family == CHIP_RS400) ||
  2029. (rdev->family == CHIP_RS480)) {
  2030. /* attempt to program RS400 disp regs correctly ??? */
  2031. temp = RREG32(RS400_DISP1_REG_CNTL);
  2032. temp &= ~(RS400_DISP1_START_REQ_LEVEL_MASK |
  2033. RS400_DISP1_STOP_REQ_LEVEL_MASK);
  2034. WREG32(RS400_DISP1_REQ_CNTL1, (temp |
  2035. (critical_point << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
  2036. (critical_point << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
  2037. temp = RREG32(RS400_DMIF_MEM_CNTL1);
  2038. temp &= ~(RS400_DISP1_CRITICAL_POINT_START_MASK |
  2039. RS400_DISP1_CRITICAL_POINT_STOP_MASK);
  2040. WREG32(RS400_DMIF_MEM_CNTL1, (temp |
  2041. (critical_point << RS400_DISP1_CRITICAL_POINT_START_SHIFT) |
  2042. (critical_point << RS400_DISP1_CRITICAL_POINT_STOP_SHIFT)));
  2043. }
  2044. #endif
  2045. DRM_DEBUG("GRPH_BUFFER_CNTL from to %x\n",
  2046. /* (unsigned int)info->SavedReg->grph_buffer_cntl, */
  2047. (unsigned int)RREG32(RADEON_GRPH_BUFFER_CNTL));
  2048. }
  2049. if (mode2) {
  2050. u32 grph2_cntl;
  2051. stop_req = mode2->hdisplay * pixel_bytes2 / 16;
  2052. if (stop_req > max_stop_req)
  2053. stop_req = max_stop_req;
  2054. /*
  2055. Find the drain rate of the display buffer.
  2056. */
  2057. temp_ff.full = rfixed_const((16/pixel_bytes2));
  2058. disp_drain_rate2.full = rfixed_div(pix_clk2, temp_ff);
  2059. grph2_cntl = RREG32(RADEON_GRPH2_BUFFER_CNTL);
  2060. grph2_cntl &= ~(RADEON_GRPH_STOP_REQ_MASK);
  2061. grph2_cntl |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
  2062. grph2_cntl &= ~(RADEON_GRPH_START_REQ_MASK);
  2063. if ((rdev->family == CHIP_R350) &&
  2064. (stop_req > 0x15)) {
  2065. stop_req -= 0x10;
  2066. }
  2067. grph2_cntl |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
  2068. grph2_cntl |= RADEON_GRPH_BUFFER_SIZE;
  2069. grph2_cntl &= ~(RADEON_GRPH_CRITICAL_CNTL |
  2070. RADEON_GRPH_CRITICAL_AT_SOF |
  2071. RADEON_GRPH_STOP_CNTL);
  2072. if ((rdev->family == CHIP_RS100) ||
  2073. (rdev->family == CHIP_RS200))
  2074. critical_point2 = 0;
  2075. else {
  2076. temp = (rdev->mc.vram_width * rdev->mc.vram_is_ddr + 1)/128;
  2077. temp_ff.full = rfixed_const(temp);
  2078. temp_ff.full = rfixed_mul(mclk_ff, temp_ff);
  2079. if (sclk_ff.full < temp_ff.full)
  2080. temp_ff.full = sclk_ff.full;
  2081. read_return_rate.full = temp_ff.full;
  2082. if (mode1) {
  2083. temp_ff.full = read_return_rate.full - disp_drain_rate.full;
  2084. time_disp1_drop_priority.full = rfixed_div(crit_point_ff, temp_ff);
  2085. } else {
  2086. time_disp1_drop_priority.full = 0;
  2087. }
  2088. crit_point_ff.full = disp_latency.full + time_disp1_drop_priority.full + disp_latency.full;
  2089. crit_point_ff.full = rfixed_mul(crit_point_ff, disp_drain_rate2);
  2090. crit_point_ff.full += rfixed_const_half(0);
  2091. critical_point2 = rfixed_trunc(crit_point_ff);
  2092. if (rdev->disp_priority == 2) {
  2093. critical_point2 = 0;
  2094. }
  2095. if (max_stop_req - critical_point2 < 4)
  2096. critical_point2 = 0;
  2097. }
  2098. if (critical_point2 == 0 && rdev->family == CHIP_R300) {
  2099. /* some R300 cards have problem with this set to 0 */
  2100. critical_point2 = 0x10;
  2101. }
  2102. WREG32(RADEON_GRPH2_BUFFER_CNTL, ((grph2_cntl & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
  2103. (critical_point2 << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
  2104. if ((rdev->family == CHIP_RS400) ||
  2105. (rdev->family == CHIP_RS480)) {
  2106. #if 0
  2107. /* attempt to program RS400 disp2 regs correctly ??? */
  2108. temp = RREG32(RS400_DISP2_REQ_CNTL1);
  2109. temp &= ~(RS400_DISP2_START_REQ_LEVEL_MASK |
  2110. RS400_DISP2_STOP_REQ_LEVEL_MASK);
  2111. WREG32(RS400_DISP2_REQ_CNTL1, (temp |
  2112. (critical_point2 << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
  2113. (critical_point2 << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
  2114. temp = RREG32(RS400_DISP2_REQ_CNTL2);
  2115. temp &= ~(RS400_DISP2_CRITICAL_POINT_START_MASK |
  2116. RS400_DISP2_CRITICAL_POINT_STOP_MASK);
  2117. WREG32(RS400_DISP2_REQ_CNTL2, (temp |
  2118. (critical_point2 << RS400_DISP2_CRITICAL_POINT_START_SHIFT) |
  2119. (critical_point2 << RS400_DISP2_CRITICAL_POINT_STOP_SHIFT)));
  2120. #endif
  2121. WREG32(RS400_DISP2_REQ_CNTL1, 0x105DC1CC);
  2122. WREG32(RS400_DISP2_REQ_CNTL2, 0x2749D000);
  2123. WREG32(RS400_DMIF_MEM_CNTL1, 0x29CA71DC);
  2124. WREG32(RS400_DISP1_REQ_CNTL1, 0x28FBC3AC);
  2125. }
  2126. DRM_DEBUG("GRPH2_BUFFER_CNTL from to %x\n",
  2127. (unsigned int)RREG32(RADEON_GRPH2_BUFFER_CNTL));
  2128. }
  2129. }