r100.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326
  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. default:
  1011. /* FIXME: we don't want to allow anyothers packet */
  1012. break;
  1013. }
  1014. if (onereg) {
  1015. /* FIXME: forbid onereg write to register on relocate */
  1016. break;
  1017. }
  1018. }
  1019. return 0;
  1020. }
  1021. int r100_cs_track_check_pkt3_indx_buffer(struct radeon_cs_parser *p,
  1022. struct radeon_cs_packet *pkt,
  1023. struct radeon_object *robj)
  1024. {
  1025. struct radeon_cs_chunk *ib_chunk;
  1026. unsigned idx;
  1027. ib_chunk = &p->chunks[p->chunk_ib_idx];
  1028. idx = pkt->idx + 1;
  1029. if ((ib_chunk->kdata[idx+2] + 1) > radeon_object_size(robj)) {
  1030. DRM_ERROR("[drm] Buffer too small for PACKET3 INDX_BUFFER "
  1031. "(need %u have %lu) !\n",
  1032. ib_chunk->kdata[idx+2] + 1,
  1033. radeon_object_size(robj));
  1034. return -EINVAL;
  1035. }
  1036. return 0;
  1037. }
  1038. static int r100_packet3_check(struct radeon_cs_parser *p,
  1039. struct radeon_cs_packet *pkt)
  1040. {
  1041. struct radeon_cs_chunk *ib_chunk;
  1042. struct radeon_cs_reloc *reloc;
  1043. unsigned idx;
  1044. unsigned i, c;
  1045. volatile uint32_t *ib;
  1046. int r;
  1047. ib = p->ib->ptr;
  1048. ib_chunk = &p->chunks[p->chunk_ib_idx];
  1049. idx = pkt->idx + 1;
  1050. switch (pkt->opcode) {
  1051. case PACKET3_3D_LOAD_VBPNTR:
  1052. c = ib_chunk->kdata[idx++];
  1053. for (i = 0; i < (c - 1); i += 2, idx += 3) {
  1054. r = r100_cs_packet_next_reloc(p, &reloc);
  1055. if (r) {
  1056. DRM_ERROR("No reloc for packet3 %d\n",
  1057. pkt->opcode);
  1058. r100_cs_dump_packet(p, pkt);
  1059. return r;
  1060. }
  1061. ib[idx+1] = ib_chunk->kdata[idx+1] + ((u32)reloc->lobj.gpu_offset);
  1062. r = r100_cs_packet_next_reloc(p, &reloc);
  1063. if (r) {
  1064. DRM_ERROR("No reloc for packet3 %d\n",
  1065. pkt->opcode);
  1066. r100_cs_dump_packet(p, pkt);
  1067. return r;
  1068. }
  1069. ib[idx+2] = ib_chunk->kdata[idx+2] + ((u32)reloc->lobj.gpu_offset);
  1070. }
  1071. if (c & 1) {
  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+1] = ib_chunk->kdata[idx+1] + ((u32)reloc->lobj.gpu_offset);
  1080. }
  1081. break;
  1082. case PACKET3_INDX_BUFFER:
  1083. r = r100_cs_packet_next_reloc(p, &reloc);
  1084. if (r) {
  1085. DRM_ERROR("No reloc for packet3 %d\n", 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. r = r100_cs_track_check_pkt3_indx_buffer(p, pkt, reloc->robj);
  1091. if (r) {
  1092. return r;
  1093. }
  1094. break;
  1095. case 0x23:
  1096. /* FIXME: cleanup */
  1097. /* 3D_RNDR_GEN_INDX_PRIM on r100/r200 */
  1098. r = r100_cs_packet_next_reloc(p, &reloc);
  1099. if (r) {
  1100. DRM_ERROR("No reloc for packet3 %d\n", pkt->opcode);
  1101. r100_cs_dump_packet(p, pkt);
  1102. return r;
  1103. }
  1104. ib[idx] = ib_chunk->kdata[idx] + ((u32)reloc->lobj.gpu_offset);
  1105. break;
  1106. case PACKET3_3D_DRAW_IMMD:
  1107. /* triggers drawing using in-packet vertex data */
  1108. case PACKET3_3D_DRAW_IMMD_2:
  1109. /* triggers drawing using in-packet vertex data */
  1110. case PACKET3_3D_DRAW_VBUF_2:
  1111. /* triggers drawing of vertex buffers setup elsewhere */
  1112. case PACKET3_3D_DRAW_INDX_2:
  1113. /* triggers drawing using indices to vertex buffer */
  1114. case PACKET3_3D_DRAW_VBUF:
  1115. /* triggers drawing of vertex buffers setup elsewhere */
  1116. case PACKET3_3D_DRAW_INDX:
  1117. /* triggers drawing using indices to vertex buffer */
  1118. case PACKET3_NOP:
  1119. break;
  1120. default:
  1121. DRM_ERROR("Packet3 opcode %x not supported\n", pkt->opcode);
  1122. return -EINVAL;
  1123. }
  1124. return 0;
  1125. }
  1126. int r100_cs_parse(struct radeon_cs_parser *p)
  1127. {
  1128. struct radeon_cs_packet pkt;
  1129. int r;
  1130. do {
  1131. r = r100_cs_packet_parse(p, &pkt, p->idx);
  1132. if (r) {
  1133. return r;
  1134. }
  1135. p->idx += pkt.count + 2;
  1136. switch (pkt.type) {
  1137. case PACKET_TYPE0:
  1138. r = r100_packet0_check(p, &pkt);
  1139. break;
  1140. case PACKET_TYPE2:
  1141. break;
  1142. case PACKET_TYPE3:
  1143. r = r100_packet3_check(p, &pkt);
  1144. break;
  1145. default:
  1146. DRM_ERROR("Unknown packet type %d !\n",
  1147. pkt.type);
  1148. return -EINVAL;
  1149. }
  1150. if (r) {
  1151. return r;
  1152. }
  1153. } while (p->idx < p->chunks[p->chunk_ib_idx].length_dw);
  1154. return 0;
  1155. }
  1156. /*
  1157. * Global GPU functions
  1158. */
  1159. void r100_errata(struct radeon_device *rdev)
  1160. {
  1161. rdev->pll_errata = 0;
  1162. if (rdev->family == CHIP_RV200 || rdev->family == CHIP_RS200) {
  1163. rdev->pll_errata |= CHIP_ERRATA_PLL_DUMMYREADS;
  1164. }
  1165. if (rdev->family == CHIP_RV100 ||
  1166. rdev->family == CHIP_RS100 ||
  1167. rdev->family == CHIP_RS200) {
  1168. rdev->pll_errata |= CHIP_ERRATA_PLL_DELAY;
  1169. }
  1170. }
  1171. /* Wait for vertical sync on primary CRTC */
  1172. void r100_gpu_wait_for_vsync(struct radeon_device *rdev)
  1173. {
  1174. uint32_t crtc_gen_cntl, tmp;
  1175. int i;
  1176. crtc_gen_cntl = RREG32(RADEON_CRTC_GEN_CNTL);
  1177. if ((crtc_gen_cntl & RADEON_CRTC_DISP_REQ_EN_B) ||
  1178. !(crtc_gen_cntl & RADEON_CRTC_EN)) {
  1179. return;
  1180. }
  1181. /* Clear the CRTC_VBLANK_SAVE bit */
  1182. WREG32(RADEON_CRTC_STATUS, RADEON_CRTC_VBLANK_SAVE_CLEAR);
  1183. for (i = 0; i < rdev->usec_timeout; i++) {
  1184. tmp = RREG32(RADEON_CRTC_STATUS);
  1185. if (tmp & RADEON_CRTC_VBLANK_SAVE) {
  1186. return;
  1187. }
  1188. DRM_UDELAY(1);
  1189. }
  1190. }
  1191. /* Wait for vertical sync on secondary CRTC */
  1192. void r100_gpu_wait_for_vsync2(struct radeon_device *rdev)
  1193. {
  1194. uint32_t crtc2_gen_cntl, tmp;
  1195. int i;
  1196. crtc2_gen_cntl = RREG32(RADEON_CRTC2_GEN_CNTL);
  1197. if ((crtc2_gen_cntl & RADEON_CRTC2_DISP_REQ_EN_B) ||
  1198. !(crtc2_gen_cntl & RADEON_CRTC2_EN))
  1199. return;
  1200. /* Clear the CRTC_VBLANK_SAVE bit */
  1201. WREG32(RADEON_CRTC2_STATUS, RADEON_CRTC2_VBLANK_SAVE_CLEAR);
  1202. for (i = 0; i < rdev->usec_timeout; i++) {
  1203. tmp = RREG32(RADEON_CRTC2_STATUS);
  1204. if (tmp & RADEON_CRTC2_VBLANK_SAVE) {
  1205. return;
  1206. }
  1207. DRM_UDELAY(1);
  1208. }
  1209. }
  1210. int r100_rbbm_fifo_wait_for_entry(struct radeon_device *rdev, unsigned n)
  1211. {
  1212. unsigned i;
  1213. uint32_t tmp;
  1214. for (i = 0; i < rdev->usec_timeout; i++) {
  1215. tmp = RREG32(RADEON_RBBM_STATUS) & RADEON_RBBM_FIFOCNT_MASK;
  1216. if (tmp >= n) {
  1217. return 0;
  1218. }
  1219. DRM_UDELAY(1);
  1220. }
  1221. return -1;
  1222. }
  1223. int r100_gui_wait_for_idle(struct radeon_device *rdev)
  1224. {
  1225. unsigned i;
  1226. uint32_t tmp;
  1227. if (r100_rbbm_fifo_wait_for_entry(rdev, 64)) {
  1228. printk(KERN_WARNING "radeon: wait for empty RBBM fifo failed !"
  1229. " Bad things might happen.\n");
  1230. }
  1231. for (i = 0; i < rdev->usec_timeout; i++) {
  1232. tmp = RREG32(RADEON_RBBM_STATUS);
  1233. if (!(tmp & (1 << 31))) {
  1234. return 0;
  1235. }
  1236. DRM_UDELAY(1);
  1237. }
  1238. return -1;
  1239. }
  1240. int r100_mc_wait_for_idle(struct radeon_device *rdev)
  1241. {
  1242. unsigned i;
  1243. uint32_t tmp;
  1244. for (i = 0; i < rdev->usec_timeout; i++) {
  1245. /* read MC_STATUS */
  1246. tmp = RREG32(0x0150);
  1247. if (tmp & (1 << 2)) {
  1248. return 0;
  1249. }
  1250. DRM_UDELAY(1);
  1251. }
  1252. return -1;
  1253. }
  1254. void r100_gpu_init(struct radeon_device *rdev)
  1255. {
  1256. /* TODO: anythings to do here ? pipes ? */
  1257. r100_hdp_reset(rdev);
  1258. }
  1259. void r100_hdp_reset(struct radeon_device *rdev)
  1260. {
  1261. uint32_t tmp;
  1262. tmp = RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL;
  1263. tmp |= (7 << 28);
  1264. WREG32(RADEON_HOST_PATH_CNTL, tmp | RADEON_HDP_SOFT_RESET | RADEON_HDP_READ_BUFFER_INVALIDATE);
  1265. (void)RREG32(RADEON_HOST_PATH_CNTL);
  1266. udelay(200);
  1267. WREG32(RADEON_RBBM_SOFT_RESET, 0);
  1268. WREG32(RADEON_HOST_PATH_CNTL, tmp);
  1269. (void)RREG32(RADEON_HOST_PATH_CNTL);
  1270. }
  1271. int r100_rb2d_reset(struct radeon_device *rdev)
  1272. {
  1273. uint32_t tmp;
  1274. int i;
  1275. WREG32(RADEON_RBBM_SOFT_RESET, RADEON_SOFT_RESET_E2);
  1276. (void)RREG32(RADEON_RBBM_SOFT_RESET);
  1277. udelay(200);
  1278. WREG32(RADEON_RBBM_SOFT_RESET, 0);
  1279. /* Wait to prevent race in RBBM_STATUS */
  1280. mdelay(1);
  1281. for (i = 0; i < rdev->usec_timeout; i++) {
  1282. tmp = RREG32(RADEON_RBBM_STATUS);
  1283. if (!(tmp & (1 << 26))) {
  1284. DRM_INFO("RB2D reset succeed (RBBM_STATUS=0x%08X)\n",
  1285. tmp);
  1286. return 0;
  1287. }
  1288. DRM_UDELAY(1);
  1289. }
  1290. tmp = RREG32(RADEON_RBBM_STATUS);
  1291. DRM_ERROR("Failed to reset RB2D (RBBM_STATUS=0x%08X)!\n", tmp);
  1292. return -1;
  1293. }
  1294. int r100_gpu_reset(struct radeon_device *rdev)
  1295. {
  1296. uint32_t status;
  1297. /* reset order likely matter */
  1298. status = RREG32(RADEON_RBBM_STATUS);
  1299. /* reset HDP */
  1300. r100_hdp_reset(rdev);
  1301. /* reset rb2d */
  1302. if (status & ((1 << 17) | (1 << 18) | (1 << 27))) {
  1303. r100_rb2d_reset(rdev);
  1304. }
  1305. /* TODO: reset 3D engine */
  1306. /* reset CP */
  1307. status = RREG32(RADEON_RBBM_STATUS);
  1308. if (status & (1 << 16)) {
  1309. r100_cp_reset(rdev);
  1310. }
  1311. /* Check if GPU is idle */
  1312. status = RREG32(RADEON_RBBM_STATUS);
  1313. if (status & (1 << 31)) {
  1314. DRM_ERROR("Failed to reset GPU (RBBM_STATUS=0x%08X)\n", status);
  1315. return -1;
  1316. }
  1317. DRM_INFO("GPU reset succeed (RBBM_STATUS=0x%08X)\n", status);
  1318. return 0;
  1319. }
  1320. /*
  1321. * VRAM info
  1322. */
  1323. static void r100_vram_get_type(struct radeon_device *rdev)
  1324. {
  1325. uint32_t tmp;
  1326. rdev->mc.vram_is_ddr = false;
  1327. if (rdev->flags & RADEON_IS_IGP)
  1328. rdev->mc.vram_is_ddr = true;
  1329. else if (RREG32(RADEON_MEM_SDRAM_MODE_REG) & RADEON_MEM_CFG_TYPE_DDR)
  1330. rdev->mc.vram_is_ddr = true;
  1331. if ((rdev->family == CHIP_RV100) ||
  1332. (rdev->family == CHIP_RS100) ||
  1333. (rdev->family == CHIP_RS200)) {
  1334. tmp = RREG32(RADEON_MEM_CNTL);
  1335. if (tmp & RV100_HALF_MODE) {
  1336. rdev->mc.vram_width = 32;
  1337. } else {
  1338. rdev->mc.vram_width = 64;
  1339. }
  1340. if (rdev->flags & RADEON_SINGLE_CRTC) {
  1341. rdev->mc.vram_width /= 4;
  1342. rdev->mc.vram_is_ddr = true;
  1343. }
  1344. } else if (rdev->family <= CHIP_RV280) {
  1345. tmp = RREG32(RADEON_MEM_CNTL);
  1346. if (tmp & RADEON_MEM_NUM_CHANNELS_MASK) {
  1347. rdev->mc.vram_width = 128;
  1348. } else {
  1349. rdev->mc.vram_width = 64;
  1350. }
  1351. } else {
  1352. /* newer IGPs */
  1353. rdev->mc.vram_width = 128;
  1354. }
  1355. }
  1356. static u32 r100_get_accessible_vram(struct radeon_device *rdev)
  1357. {
  1358. u32 aper_size;
  1359. u8 byte;
  1360. aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
  1361. /* Set HDP_APER_CNTL only on cards that are known not to be broken,
  1362. * that is has the 2nd generation multifunction PCI interface
  1363. */
  1364. if (rdev->family == CHIP_RV280 ||
  1365. rdev->family >= CHIP_RV350) {
  1366. WREG32_P(RADEON_HOST_PATH_CNTL, RADEON_HDP_APER_CNTL,
  1367. ~RADEON_HDP_APER_CNTL);
  1368. DRM_INFO("Generation 2 PCI interface, using max accessible memory\n");
  1369. return aper_size * 2;
  1370. }
  1371. /* Older cards have all sorts of funny issues to deal with. First
  1372. * check if it's a multifunction card by reading the PCI config
  1373. * header type... Limit those to one aperture size
  1374. */
  1375. pci_read_config_byte(rdev->pdev, 0xe, &byte);
  1376. if (byte & 0x80) {
  1377. DRM_INFO("Generation 1 PCI interface in multifunction mode\n");
  1378. DRM_INFO("Limiting VRAM to one aperture\n");
  1379. return aper_size;
  1380. }
  1381. /* Single function older card. We read HDP_APER_CNTL to see how the BIOS
  1382. * have set it up. We don't write this as it's broken on some ASICs but
  1383. * we expect the BIOS to have done the right thing (might be too optimistic...)
  1384. */
  1385. if (RREG32(RADEON_HOST_PATH_CNTL) & RADEON_HDP_APER_CNTL)
  1386. return aper_size * 2;
  1387. return aper_size;
  1388. }
  1389. void r100_vram_init_sizes(struct radeon_device *rdev)
  1390. {
  1391. u64 config_aper_size;
  1392. u32 accessible;
  1393. config_aper_size = RREG32(RADEON_CONFIG_APER_SIZE);
  1394. if (rdev->flags & RADEON_IS_IGP) {
  1395. uint32_t tom;
  1396. /* read NB_TOM to get the amount of ram stolen for the GPU */
  1397. tom = RREG32(RADEON_NB_TOM);
  1398. rdev->mc.real_vram_size = (((tom >> 16) - (tom & 0xffff) + 1) << 16);
  1399. /* for IGPs we need to keep VRAM where it was put by the BIOS */
  1400. rdev->mc.vram_location = (tom & 0xffff) << 16;
  1401. WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
  1402. rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
  1403. } else {
  1404. rdev->mc.real_vram_size = RREG32(RADEON_CONFIG_MEMSIZE);
  1405. /* Some production boards of m6 will report 0
  1406. * if it's 8 MB
  1407. */
  1408. if (rdev->mc.real_vram_size == 0) {
  1409. rdev->mc.real_vram_size = 8192 * 1024;
  1410. WREG32(RADEON_CONFIG_MEMSIZE, rdev->mc.real_vram_size);
  1411. }
  1412. /* let driver place VRAM */
  1413. rdev->mc.vram_location = 0xFFFFFFFFUL;
  1414. /* Fix for RN50, M6, M7 with 8/16/32(??) MBs of VRAM -
  1415. * Novell bug 204882 + along with lots of ubuntu ones */
  1416. if (config_aper_size > rdev->mc.real_vram_size)
  1417. rdev->mc.mc_vram_size = config_aper_size;
  1418. else
  1419. rdev->mc.mc_vram_size = rdev->mc.real_vram_size;
  1420. }
  1421. /* work out accessible VRAM */
  1422. accessible = r100_get_accessible_vram(rdev);
  1423. rdev->mc.aper_base = drm_get_resource_start(rdev->ddev, 0);
  1424. rdev->mc.aper_size = drm_get_resource_len(rdev->ddev, 0);
  1425. if (accessible > rdev->mc.aper_size)
  1426. accessible = rdev->mc.aper_size;
  1427. if (rdev->mc.mc_vram_size > rdev->mc.aper_size)
  1428. rdev->mc.mc_vram_size = rdev->mc.aper_size;
  1429. if (rdev->mc.real_vram_size > rdev->mc.aper_size)
  1430. rdev->mc.real_vram_size = rdev->mc.aper_size;
  1431. }
  1432. void r100_vram_info(struct radeon_device *rdev)
  1433. {
  1434. r100_vram_get_type(rdev);
  1435. r100_vram_init_sizes(rdev);
  1436. }
  1437. /*
  1438. * Indirect registers accessor
  1439. */
  1440. void r100_pll_errata_after_index(struct radeon_device *rdev)
  1441. {
  1442. if (!(rdev->pll_errata & CHIP_ERRATA_PLL_DUMMYREADS)) {
  1443. return;
  1444. }
  1445. (void)RREG32(RADEON_CLOCK_CNTL_DATA);
  1446. (void)RREG32(RADEON_CRTC_GEN_CNTL);
  1447. }
  1448. static void r100_pll_errata_after_data(struct radeon_device *rdev)
  1449. {
  1450. /* This workarounds is necessary on RV100, RS100 and RS200 chips
  1451. * or the chip could hang on a subsequent access
  1452. */
  1453. if (rdev->pll_errata & CHIP_ERRATA_PLL_DELAY) {
  1454. udelay(5000);
  1455. }
  1456. /* This function is required to workaround a hardware bug in some (all?)
  1457. * revisions of the R300. This workaround should be called after every
  1458. * CLOCK_CNTL_INDEX register access. If not, register reads afterward
  1459. * may not be correct.
  1460. */
  1461. if (rdev->pll_errata & CHIP_ERRATA_R300_CG) {
  1462. uint32_t save, tmp;
  1463. save = RREG32(RADEON_CLOCK_CNTL_INDEX);
  1464. tmp = save & ~(0x3f | RADEON_PLL_WR_EN);
  1465. WREG32(RADEON_CLOCK_CNTL_INDEX, tmp);
  1466. tmp = RREG32(RADEON_CLOCK_CNTL_DATA);
  1467. WREG32(RADEON_CLOCK_CNTL_INDEX, save);
  1468. }
  1469. }
  1470. uint32_t r100_pll_rreg(struct radeon_device *rdev, uint32_t reg)
  1471. {
  1472. uint32_t data;
  1473. WREG8(RADEON_CLOCK_CNTL_INDEX, reg & 0x3f);
  1474. r100_pll_errata_after_index(rdev);
  1475. data = RREG32(RADEON_CLOCK_CNTL_DATA);
  1476. r100_pll_errata_after_data(rdev);
  1477. return data;
  1478. }
  1479. void r100_pll_wreg(struct radeon_device *rdev, uint32_t reg, uint32_t v)
  1480. {
  1481. WREG8(RADEON_CLOCK_CNTL_INDEX, ((reg & 0x3f) | RADEON_PLL_WR_EN));
  1482. r100_pll_errata_after_index(rdev);
  1483. WREG32(RADEON_CLOCK_CNTL_DATA, v);
  1484. r100_pll_errata_after_data(rdev);
  1485. }
  1486. int r100_init(struct radeon_device *rdev)
  1487. {
  1488. return 0;
  1489. }
  1490. /*
  1491. * Debugfs info
  1492. */
  1493. #if defined(CONFIG_DEBUG_FS)
  1494. static int r100_debugfs_rbbm_info(struct seq_file *m, void *data)
  1495. {
  1496. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1497. struct drm_device *dev = node->minor->dev;
  1498. struct radeon_device *rdev = dev->dev_private;
  1499. uint32_t reg, value;
  1500. unsigned i;
  1501. seq_printf(m, "RBBM_STATUS 0x%08x\n", RREG32(RADEON_RBBM_STATUS));
  1502. seq_printf(m, "RBBM_CMDFIFO_STAT 0x%08x\n", RREG32(0xE7C));
  1503. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  1504. for (i = 0; i < 64; i++) {
  1505. WREG32(RADEON_RBBM_CMDFIFO_ADDR, i | 0x100);
  1506. reg = (RREG32(RADEON_RBBM_CMDFIFO_DATA) - 1) >> 2;
  1507. WREG32(RADEON_RBBM_CMDFIFO_ADDR, i);
  1508. value = RREG32(RADEON_RBBM_CMDFIFO_DATA);
  1509. seq_printf(m, "[0x%03X] 0x%04X=0x%08X\n", i, reg, value);
  1510. }
  1511. return 0;
  1512. }
  1513. static int r100_debugfs_cp_ring_info(struct seq_file *m, void *data)
  1514. {
  1515. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1516. struct drm_device *dev = node->minor->dev;
  1517. struct radeon_device *rdev = dev->dev_private;
  1518. uint32_t rdp, wdp;
  1519. unsigned count, i, j;
  1520. radeon_ring_free_size(rdev);
  1521. rdp = RREG32(RADEON_CP_RB_RPTR);
  1522. wdp = RREG32(RADEON_CP_RB_WPTR);
  1523. count = (rdp + rdev->cp.ring_size - wdp) & rdev->cp.ptr_mask;
  1524. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  1525. seq_printf(m, "CP_RB_WPTR 0x%08x\n", wdp);
  1526. seq_printf(m, "CP_RB_RPTR 0x%08x\n", rdp);
  1527. seq_printf(m, "%u free dwords in ring\n", rdev->cp.ring_free_dw);
  1528. seq_printf(m, "%u dwords in ring\n", count);
  1529. for (j = 0; j <= count; j++) {
  1530. i = (rdp + j) & rdev->cp.ptr_mask;
  1531. seq_printf(m, "r[%04d]=0x%08x\n", i, rdev->cp.ring[i]);
  1532. }
  1533. return 0;
  1534. }
  1535. static int r100_debugfs_cp_csq_fifo(struct seq_file *m, void *data)
  1536. {
  1537. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1538. struct drm_device *dev = node->minor->dev;
  1539. struct radeon_device *rdev = dev->dev_private;
  1540. uint32_t csq_stat, csq2_stat, tmp;
  1541. unsigned r_rptr, r_wptr, ib1_rptr, ib1_wptr, ib2_rptr, ib2_wptr;
  1542. unsigned i;
  1543. seq_printf(m, "CP_STAT 0x%08x\n", RREG32(RADEON_CP_STAT));
  1544. seq_printf(m, "CP_CSQ_MODE 0x%08x\n", RREG32(RADEON_CP_CSQ_MODE));
  1545. csq_stat = RREG32(RADEON_CP_CSQ_STAT);
  1546. csq2_stat = RREG32(RADEON_CP_CSQ2_STAT);
  1547. r_rptr = (csq_stat >> 0) & 0x3ff;
  1548. r_wptr = (csq_stat >> 10) & 0x3ff;
  1549. ib1_rptr = (csq_stat >> 20) & 0x3ff;
  1550. ib1_wptr = (csq2_stat >> 0) & 0x3ff;
  1551. ib2_rptr = (csq2_stat >> 10) & 0x3ff;
  1552. ib2_wptr = (csq2_stat >> 20) & 0x3ff;
  1553. seq_printf(m, "CP_CSQ_STAT 0x%08x\n", csq_stat);
  1554. seq_printf(m, "CP_CSQ2_STAT 0x%08x\n", csq2_stat);
  1555. seq_printf(m, "Ring rptr %u\n", r_rptr);
  1556. seq_printf(m, "Ring wptr %u\n", r_wptr);
  1557. seq_printf(m, "Indirect1 rptr %u\n", ib1_rptr);
  1558. seq_printf(m, "Indirect1 wptr %u\n", ib1_wptr);
  1559. seq_printf(m, "Indirect2 rptr %u\n", ib2_rptr);
  1560. seq_printf(m, "Indirect2 wptr %u\n", ib2_wptr);
  1561. /* FIXME: 0, 128, 640 depends on fifo setup see cp_init_kms
  1562. * 128 = indirect1_start * 8 & 640 = indirect2_start * 8 */
  1563. seq_printf(m, "Ring fifo:\n");
  1564. for (i = 0; i < 256; i++) {
  1565. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  1566. tmp = RREG32(RADEON_CP_CSQ_DATA);
  1567. seq_printf(m, "rfifo[%04d]=0x%08X\n", i, tmp);
  1568. }
  1569. seq_printf(m, "Indirect1 fifo:\n");
  1570. for (i = 256; i <= 512; i++) {
  1571. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  1572. tmp = RREG32(RADEON_CP_CSQ_DATA);
  1573. seq_printf(m, "ib1fifo[%04d]=0x%08X\n", i, tmp);
  1574. }
  1575. seq_printf(m, "Indirect2 fifo:\n");
  1576. for (i = 640; i < ib1_wptr; i++) {
  1577. WREG32(RADEON_CP_CSQ_ADDR, i << 2);
  1578. tmp = RREG32(RADEON_CP_CSQ_DATA);
  1579. seq_printf(m, "ib2fifo[%04d]=0x%08X\n", i, tmp);
  1580. }
  1581. return 0;
  1582. }
  1583. static int r100_debugfs_mc_info(struct seq_file *m, void *data)
  1584. {
  1585. struct drm_info_node *node = (struct drm_info_node *) m->private;
  1586. struct drm_device *dev = node->minor->dev;
  1587. struct radeon_device *rdev = dev->dev_private;
  1588. uint32_t tmp;
  1589. tmp = RREG32(RADEON_CONFIG_MEMSIZE);
  1590. seq_printf(m, "CONFIG_MEMSIZE 0x%08x\n", tmp);
  1591. tmp = RREG32(RADEON_MC_FB_LOCATION);
  1592. seq_printf(m, "MC_FB_LOCATION 0x%08x\n", tmp);
  1593. tmp = RREG32(RADEON_BUS_CNTL);
  1594. seq_printf(m, "BUS_CNTL 0x%08x\n", tmp);
  1595. tmp = RREG32(RADEON_MC_AGP_LOCATION);
  1596. seq_printf(m, "MC_AGP_LOCATION 0x%08x\n", tmp);
  1597. tmp = RREG32(RADEON_AGP_BASE);
  1598. seq_printf(m, "AGP_BASE 0x%08x\n", tmp);
  1599. tmp = RREG32(RADEON_HOST_PATH_CNTL);
  1600. seq_printf(m, "HOST_PATH_CNTL 0x%08x\n", tmp);
  1601. tmp = RREG32(0x01D0);
  1602. seq_printf(m, "AIC_CTRL 0x%08x\n", tmp);
  1603. tmp = RREG32(RADEON_AIC_LO_ADDR);
  1604. seq_printf(m, "AIC_LO_ADDR 0x%08x\n", tmp);
  1605. tmp = RREG32(RADEON_AIC_HI_ADDR);
  1606. seq_printf(m, "AIC_HI_ADDR 0x%08x\n", tmp);
  1607. tmp = RREG32(0x01E4);
  1608. seq_printf(m, "AIC_TLB_ADDR 0x%08x\n", tmp);
  1609. return 0;
  1610. }
  1611. static struct drm_info_list r100_debugfs_rbbm_list[] = {
  1612. {"r100_rbbm_info", r100_debugfs_rbbm_info, 0, NULL},
  1613. };
  1614. static struct drm_info_list r100_debugfs_cp_list[] = {
  1615. {"r100_cp_ring_info", r100_debugfs_cp_ring_info, 0, NULL},
  1616. {"r100_cp_csq_fifo", r100_debugfs_cp_csq_fifo, 0, NULL},
  1617. };
  1618. static struct drm_info_list r100_debugfs_mc_info_list[] = {
  1619. {"r100_mc_info", r100_debugfs_mc_info, 0, NULL},
  1620. };
  1621. #endif
  1622. int r100_debugfs_rbbm_init(struct radeon_device *rdev)
  1623. {
  1624. #if defined(CONFIG_DEBUG_FS)
  1625. return radeon_debugfs_add_files(rdev, r100_debugfs_rbbm_list, 1);
  1626. #else
  1627. return 0;
  1628. #endif
  1629. }
  1630. int r100_debugfs_cp_init(struct radeon_device *rdev)
  1631. {
  1632. #if defined(CONFIG_DEBUG_FS)
  1633. return radeon_debugfs_add_files(rdev, r100_debugfs_cp_list, 2);
  1634. #else
  1635. return 0;
  1636. #endif
  1637. }
  1638. int r100_debugfs_mc_info_init(struct radeon_device *rdev)
  1639. {
  1640. #if defined(CONFIG_DEBUG_FS)
  1641. return radeon_debugfs_add_files(rdev, r100_debugfs_mc_info_list, 1);
  1642. #else
  1643. return 0;
  1644. #endif
  1645. }
  1646. int r100_set_surface_reg(struct radeon_device *rdev, int reg,
  1647. uint32_t tiling_flags, uint32_t pitch,
  1648. uint32_t offset, uint32_t obj_size)
  1649. {
  1650. int surf_index = reg * 16;
  1651. int flags = 0;
  1652. /* r100/r200 divide by 16 */
  1653. if (rdev->family < CHIP_R300)
  1654. flags = pitch / 16;
  1655. else
  1656. flags = pitch / 8;
  1657. if (rdev->family <= CHIP_RS200) {
  1658. if ((tiling_flags & (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
  1659. == (RADEON_TILING_MACRO|RADEON_TILING_MICRO))
  1660. flags |= RADEON_SURF_TILE_COLOR_BOTH;
  1661. if (tiling_flags & RADEON_TILING_MACRO)
  1662. flags |= RADEON_SURF_TILE_COLOR_MACRO;
  1663. } else if (rdev->family <= CHIP_RV280) {
  1664. if (tiling_flags & (RADEON_TILING_MACRO))
  1665. flags |= R200_SURF_TILE_COLOR_MACRO;
  1666. if (tiling_flags & RADEON_TILING_MICRO)
  1667. flags |= R200_SURF_TILE_COLOR_MICRO;
  1668. } else {
  1669. if (tiling_flags & RADEON_TILING_MACRO)
  1670. flags |= R300_SURF_TILE_MACRO;
  1671. if (tiling_flags & RADEON_TILING_MICRO)
  1672. flags |= R300_SURF_TILE_MICRO;
  1673. }
  1674. DRM_DEBUG("writing surface %d %d %x %x\n", reg, flags, offset, offset+obj_size-1);
  1675. WREG32(RADEON_SURFACE0_INFO + surf_index, flags);
  1676. WREG32(RADEON_SURFACE0_LOWER_BOUND + surf_index, offset);
  1677. WREG32(RADEON_SURFACE0_UPPER_BOUND + surf_index, offset + obj_size - 1);
  1678. return 0;
  1679. }
  1680. void r100_clear_surface_reg(struct radeon_device *rdev, int reg)
  1681. {
  1682. int surf_index = reg * 16;
  1683. WREG32(RADEON_SURFACE0_INFO + surf_index, 0);
  1684. }
  1685. void r100_bandwidth_update(struct radeon_device *rdev)
  1686. {
  1687. fixed20_12 trcd_ff, trp_ff, tras_ff, trbs_ff, tcas_ff;
  1688. fixed20_12 sclk_ff, mclk_ff, sclk_eff_ff, sclk_delay_ff;
  1689. fixed20_12 peak_disp_bw, mem_bw, pix_clk, pix_clk2, temp_ff, crit_point_ff;
  1690. uint32_t temp, data, mem_trcd, mem_trp, mem_tras;
  1691. fixed20_12 memtcas_ff[8] = {
  1692. fixed_init(1),
  1693. fixed_init(2),
  1694. fixed_init(3),
  1695. fixed_init(0),
  1696. fixed_init_half(1),
  1697. fixed_init_half(2),
  1698. fixed_init(0),
  1699. };
  1700. fixed20_12 memtcas_rs480_ff[8] = {
  1701. fixed_init(0),
  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_half(3),
  1709. };
  1710. fixed20_12 memtcas2_ff[8] = {
  1711. fixed_init(0),
  1712. fixed_init(1),
  1713. fixed_init(2),
  1714. fixed_init(3),
  1715. fixed_init(4),
  1716. fixed_init(5),
  1717. fixed_init(6),
  1718. fixed_init(7),
  1719. };
  1720. fixed20_12 memtrbs[8] = {
  1721. fixed_init(1),
  1722. fixed_init_half(1),
  1723. fixed_init(2),
  1724. fixed_init_half(2),
  1725. fixed_init(3),
  1726. fixed_init_half(3),
  1727. fixed_init(4),
  1728. fixed_init_half(4)
  1729. };
  1730. fixed20_12 memtrbs_r4xx[8] = {
  1731. fixed_init(4),
  1732. fixed_init(5),
  1733. fixed_init(6),
  1734. fixed_init(7),
  1735. fixed_init(8),
  1736. fixed_init(9),
  1737. fixed_init(10),
  1738. fixed_init(11)
  1739. };
  1740. fixed20_12 min_mem_eff;
  1741. fixed20_12 mc_latency_sclk, mc_latency_mclk, k1;
  1742. fixed20_12 cur_latency_mclk, cur_latency_sclk;
  1743. fixed20_12 disp_latency, disp_latency_overhead, disp_drain_rate,
  1744. disp_drain_rate2, read_return_rate;
  1745. fixed20_12 time_disp1_drop_priority;
  1746. int c;
  1747. int cur_size = 16; /* in octawords */
  1748. int critical_point = 0, critical_point2;
  1749. /* uint32_t read_return_rate, time_disp1_drop_priority; */
  1750. int stop_req, max_stop_req;
  1751. struct drm_display_mode *mode1 = NULL;
  1752. struct drm_display_mode *mode2 = NULL;
  1753. uint32_t pixel_bytes1 = 0;
  1754. uint32_t pixel_bytes2 = 0;
  1755. if (rdev->mode_info.crtcs[0]->base.enabled) {
  1756. mode1 = &rdev->mode_info.crtcs[0]->base.mode;
  1757. pixel_bytes1 = rdev->mode_info.crtcs[0]->base.fb->bits_per_pixel / 8;
  1758. }
  1759. if (rdev->mode_info.crtcs[1]->base.enabled) {
  1760. mode2 = &rdev->mode_info.crtcs[1]->base.mode;
  1761. pixel_bytes2 = rdev->mode_info.crtcs[1]->base.fb->bits_per_pixel / 8;
  1762. }
  1763. min_mem_eff.full = rfixed_const_8(0);
  1764. /* get modes */
  1765. if ((rdev->disp_priority == 2) && ASIC_IS_R300(rdev)) {
  1766. uint32_t mc_init_misc_lat_timer = RREG32(R300_MC_INIT_MISC_LAT_TIMER);
  1767. mc_init_misc_lat_timer &= ~(R300_MC_DISP1R_INIT_LAT_MASK << R300_MC_DISP1R_INIT_LAT_SHIFT);
  1768. mc_init_misc_lat_timer &= ~(R300_MC_DISP0R_INIT_LAT_MASK << R300_MC_DISP0R_INIT_LAT_SHIFT);
  1769. /* check crtc enables */
  1770. if (mode2)
  1771. mc_init_misc_lat_timer |= (1 << R300_MC_DISP1R_INIT_LAT_SHIFT);
  1772. if (mode1)
  1773. mc_init_misc_lat_timer |= (1 << R300_MC_DISP0R_INIT_LAT_SHIFT);
  1774. WREG32(R300_MC_INIT_MISC_LAT_TIMER, mc_init_misc_lat_timer);
  1775. }
  1776. /*
  1777. * determine is there is enough bw for current mode
  1778. */
  1779. mclk_ff.full = rfixed_const(rdev->clock.default_mclk);
  1780. temp_ff.full = rfixed_const(100);
  1781. mclk_ff.full = rfixed_div(mclk_ff, temp_ff);
  1782. sclk_ff.full = rfixed_const(rdev->clock.default_sclk);
  1783. sclk_ff.full = rfixed_div(sclk_ff, temp_ff);
  1784. temp = (rdev->mc.vram_width / 8) * (rdev->mc.vram_is_ddr ? 2 : 1);
  1785. temp_ff.full = rfixed_const(temp);
  1786. mem_bw.full = rfixed_mul(mclk_ff, temp_ff);
  1787. pix_clk.full = 0;
  1788. pix_clk2.full = 0;
  1789. peak_disp_bw.full = 0;
  1790. if (mode1) {
  1791. temp_ff.full = rfixed_const(1000);
  1792. pix_clk.full = rfixed_const(mode1->clock); /* convert to fixed point */
  1793. pix_clk.full = rfixed_div(pix_clk, temp_ff);
  1794. temp_ff.full = rfixed_const(pixel_bytes1);
  1795. peak_disp_bw.full += rfixed_mul(pix_clk, temp_ff);
  1796. }
  1797. if (mode2) {
  1798. temp_ff.full = rfixed_const(1000);
  1799. pix_clk2.full = rfixed_const(mode2->clock); /* convert to fixed point */
  1800. pix_clk2.full = rfixed_div(pix_clk2, temp_ff);
  1801. temp_ff.full = rfixed_const(pixel_bytes2);
  1802. peak_disp_bw.full += rfixed_mul(pix_clk2, temp_ff);
  1803. }
  1804. mem_bw.full = rfixed_mul(mem_bw, min_mem_eff);
  1805. if (peak_disp_bw.full >= mem_bw.full) {
  1806. DRM_ERROR("You may not have enough display bandwidth for current mode\n"
  1807. "If you have flickering problem, try to lower resolution, refresh rate, or color depth\n");
  1808. }
  1809. /* Get values from the EXT_MEM_CNTL register...converting its contents. */
  1810. temp = RREG32(RADEON_MEM_TIMING_CNTL);
  1811. if ((rdev->family == CHIP_RV100) || (rdev->flags & RADEON_IS_IGP)) { /* RV100, M6, IGPs */
  1812. mem_trcd = ((temp >> 2) & 0x3) + 1;
  1813. mem_trp = ((temp & 0x3)) + 1;
  1814. mem_tras = ((temp & 0x70) >> 4) + 1;
  1815. } else if (rdev->family == CHIP_R300 ||
  1816. rdev->family == CHIP_R350) { /* r300, r350 */
  1817. mem_trcd = (temp & 0x7) + 1;
  1818. mem_trp = ((temp >> 8) & 0x7) + 1;
  1819. mem_tras = ((temp >> 11) & 0xf) + 4;
  1820. } else if (rdev->family == CHIP_RV350 ||
  1821. rdev->family <= CHIP_RV380) {
  1822. /* rv3x0 */
  1823. mem_trcd = (temp & 0x7) + 3;
  1824. mem_trp = ((temp >> 8) & 0x7) + 3;
  1825. mem_tras = ((temp >> 11) & 0xf) + 6;
  1826. } else if (rdev->family == CHIP_R420 ||
  1827. rdev->family == CHIP_R423 ||
  1828. rdev->family == CHIP_RV410) {
  1829. /* r4xx */
  1830. mem_trcd = (temp & 0xf) + 3;
  1831. if (mem_trcd > 15)
  1832. mem_trcd = 15;
  1833. mem_trp = ((temp >> 8) & 0xf) + 3;
  1834. if (mem_trp > 15)
  1835. mem_trp = 15;
  1836. mem_tras = ((temp >> 12) & 0x1f) + 6;
  1837. if (mem_tras > 31)
  1838. mem_tras = 31;
  1839. } else { /* RV200, R200 */
  1840. mem_trcd = (temp & 0x7) + 1;
  1841. mem_trp = ((temp >> 8) & 0x7) + 1;
  1842. mem_tras = ((temp >> 12) & 0xf) + 4;
  1843. }
  1844. /* convert to FF */
  1845. trcd_ff.full = rfixed_const(mem_trcd);
  1846. trp_ff.full = rfixed_const(mem_trp);
  1847. tras_ff.full = rfixed_const(mem_tras);
  1848. /* Get values from the MEM_SDRAM_MODE_REG register...converting its */
  1849. temp = RREG32(RADEON_MEM_SDRAM_MODE_REG);
  1850. data = (temp & (7 << 20)) >> 20;
  1851. if ((rdev->family == CHIP_RV100) || rdev->flags & RADEON_IS_IGP) {
  1852. if (rdev->family == CHIP_RS480) /* don't think rs400 */
  1853. tcas_ff = memtcas_rs480_ff[data];
  1854. else
  1855. tcas_ff = memtcas_ff[data];
  1856. } else
  1857. tcas_ff = memtcas2_ff[data];
  1858. if (rdev->family == CHIP_RS400 ||
  1859. rdev->family == CHIP_RS480) {
  1860. /* extra cas latency stored in bits 23-25 0-4 clocks */
  1861. data = (temp >> 23) & 0x7;
  1862. if (data < 5)
  1863. tcas_ff.full += rfixed_const(data);
  1864. }
  1865. if (ASIC_IS_R300(rdev) && !(rdev->flags & RADEON_IS_IGP)) {
  1866. /* on the R300, Tcas is included in Trbs.
  1867. */
  1868. temp = RREG32(RADEON_MEM_CNTL);
  1869. data = (R300_MEM_NUM_CHANNELS_MASK & temp);
  1870. if (data == 1) {
  1871. if (R300_MEM_USE_CD_CH_ONLY & temp) {
  1872. temp = RREG32(R300_MC_IND_INDEX);
  1873. temp &= ~R300_MC_IND_ADDR_MASK;
  1874. temp |= R300_MC_READ_CNTL_CD_mcind;
  1875. WREG32(R300_MC_IND_INDEX, temp);
  1876. temp = RREG32(R300_MC_IND_DATA);
  1877. data = (R300_MEM_RBS_POSITION_C_MASK & temp);
  1878. } else {
  1879. temp = RREG32(R300_MC_READ_CNTL_AB);
  1880. data = (R300_MEM_RBS_POSITION_A_MASK & temp);
  1881. }
  1882. } else {
  1883. temp = RREG32(R300_MC_READ_CNTL_AB);
  1884. data = (R300_MEM_RBS_POSITION_A_MASK & temp);
  1885. }
  1886. if (rdev->family == CHIP_RV410 ||
  1887. rdev->family == CHIP_R420 ||
  1888. rdev->family == CHIP_R423)
  1889. trbs_ff = memtrbs_r4xx[data];
  1890. else
  1891. trbs_ff = memtrbs[data];
  1892. tcas_ff.full += trbs_ff.full;
  1893. }
  1894. sclk_eff_ff.full = sclk_ff.full;
  1895. if (rdev->flags & RADEON_IS_AGP) {
  1896. fixed20_12 agpmode_ff;
  1897. agpmode_ff.full = rfixed_const(radeon_agpmode);
  1898. temp_ff.full = rfixed_const_666(16);
  1899. sclk_eff_ff.full -= rfixed_mul(agpmode_ff, temp_ff);
  1900. }
  1901. /* TODO PCIE lanes may affect this - agpmode == 16?? */
  1902. if (ASIC_IS_R300(rdev)) {
  1903. sclk_delay_ff.full = rfixed_const(250);
  1904. } else {
  1905. if ((rdev->family == CHIP_RV100) ||
  1906. rdev->flags & RADEON_IS_IGP) {
  1907. if (rdev->mc.vram_is_ddr)
  1908. sclk_delay_ff.full = rfixed_const(41);
  1909. else
  1910. sclk_delay_ff.full = rfixed_const(33);
  1911. } else {
  1912. if (rdev->mc.vram_width == 128)
  1913. sclk_delay_ff.full = rfixed_const(57);
  1914. else
  1915. sclk_delay_ff.full = rfixed_const(41);
  1916. }
  1917. }
  1918. mc_latency_sclk.full = rfixed_div(sclk_delay_ff, sclk_eff_ff);
  1919. if (rdev->mc.vram_is_ddr) {
  1920. if (rdev->mc.vram_width == 32) {
  1921. k1.full = rfixed_const(40);
  1922. c = 3;
  1923. } else {
  1924. k1.full = rfixed_const(20);
  1925. c = 1;
  1926. }
  1927. } else {
  1928. k1.full = rfixed_const(40);
  1929. c = 3;
  1930. }
  1931. temp_ff.full = rfixed_const(2);
  1932. mc_latency_mclk.full = rfixed_mul(trcd_ff, temp_ff);
  1933. temp_ff.full = rfixed_const(c);
  1934. mc_latency_mclk.full += rfixed_mul(tcas_ff, temp_ff);
  1935. temp_ff.full = rfixed_const(4);
  1936. mc_latency_mclk.full += rfixed_mul(tras_ff, temp_ff);
  1937. mc_latency_mclk.full += rfixed_mul(trp_ff, temp_ff);
  1938. mc_latency_mclk.full += k1.full;
  1939. mc_latency_mclk.full = rfixed_div(mc_latency_mclk, mclk_ff);
  1940. mc_latency_mclk.full += rfixed_div(temp_ff, sclk_eff_ff);
  1941. /*
  1942. HW cursor time assuming worst case of full size colour cursor.
  1943. */
  1944. temp_ff.full = rfixed_const((2 * (cur_size - (rdev->mc.vram_is_ddr + 1))));
  1945. temp_ff.full += trcd_ff.full;
  1946. if (temp_ff.full < tras_ff.full)
  1947. temp_ff.full = tras_ff.full;
  1948. cur_latency_mclk.full = rfixed_div(temp_ff, mclk_ff);
  1949. temp_ff.full = rfixed_const(cur_size);
  1950. cur_latency_sclk.full = rfixed_div(temp_ff, sclk_eff_ff);
  1951. /*
  1952. Find the total latency for the display data.
  1953. */
  1954. disp_latency_overhead.full = rfixed_const(80);
  1955. disp_latency_overhead.full = rfixed_div(disp_latency_overhead, sclk_ff);
  1956. mc_latency_mclk.full += disp_latency_overhead.full + cur_latency_mclk.full;
  1957. mc_latency_sclk.full += disp_latency_overhead.full + cur_latency_sclk.full;
  1958. if (mc_latency_mclk.full > mc_latency_sclk.full)
  1959. disp_latency.full = mc_latency_mclk.full;
  1960. else
  1961. disp_latency.full = mc_latency_sclk.full;
  1962. /* setup Max GRPH_STOP_REQ default value */
  1963. if (ASIC_IS_RV100(rdev))
  1964. max_stop_req = 0x5c;
  1965. else
  1966. max_stop_req = 0x7c;
  1967. if (mode1) {
  1968. /* CRTC1
  1969. Set GRPH_BUFFER_CNTL register using h/w defined optimal values.
  1970. GRPH_STOP_REQ <= MIN[ 0x7C, (CRTC_H_DISP + 1) * (bit depth) / 0x10 ]
  1971. */
  1972. stop_req = mode1->hdisplay * pixel_bytes1 / 16;
  1973. if (stop_req > max_stop_req)
  1974. stop_req = max_stop_req;
  1975. /*
  1976. Find the drain rate of the display buffer.
  1977. */
  1978. temp_ff.full = rfixed_const((16/pixel_bytes1));
  1979. disp_drain_rate.full = rfixed_div(pix_clk, temp_ff);
  1980. /*
  1981. Find the critical point of the display buffer.
  1982. */
  1983. crit_point_ff.full = rfixed_mul(disp_drain_rate, disp_latency);
  1984. crit_point_ff.full += rfixed_const_half(0);
  1985. critical_point = rfixed_trunc(crit_point_ff);
  1986. if (rdev->disp_priority == 2) {
  1987. critical_point = 0;
  1988. }
  1989. /*
  1990. The critical point should never be above max_stop_req-4. Setting
  1991. GRPH_CRITICAL_CNTL = 0 will thus force high priority all the time.
  1992. */
  1993. if (max_stop_req - critical_point < 4)
  1994. critical_point = 0;
  1995. if (critical_point == 0 && mode2 && rdev->family == CHIP_R300) {
  1996. /* some R300 cards have problem with this set to 0, when CRTC2 is enabled.*/
  1997. critical_point = 0x10;
  1998. }
  1999. temp = RREG32(RADEON_GRPH_BUFFER_CNTL);
  2000. temp &= ~(RADEON_GRPH_STOP_REQ_MASK);
  2001. temp |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
  2002. temp &= ~(RADEON_GRPH_START_REQ_MASK);
  2003. if ((rdev->family == CHIP_R350) &&
  2004. (stop_req > 0x15)) {
  2005. stop_req -= 0x10;
  2006. }
  2007. temp |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
  2008. temp |= RADEON_GRPH_BUFFER_SIZE;
  2009. temp &= ~(RADEON_GRPH_CRITICAL_CNTL |
  2010. RADEON_GRPH_CRITICAL_AT_SOF |
  2011. RADEON_GRPH_STOP_CNTL);
  2012. /*
  2013. Write the result into the register.
  2014. */
  2015. WREG32(RADEON_GRPH_BUFFER_CNTL, ((temp & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
  2016. (critical_point << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
  2017. #if 0
  2018. if ((rdev->family == CHIP_RS400) ||
  2019. (rdev->family == CHIP_RS480)) {
  2020. /* attempt to program RS400 disp regs correctly ??? */
  2021. temp = RREG32(RS400_DISP1_REG_CNTL);
  2022. temp &= ~(RS400_DISP1_START_REQ_LEVEL_MASK |
  2023. RS400_DISP1_STOP_REQ_LEVEL_MASK);
  2024. WREG32(RS400_DISP1_REQ_CNTL1, (temp |
  2025. (critical_point << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
  2026. (critical_point << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
  2027. temp = RREG32(RS400_DMIF_MEM_CNTL1);
  2028. temp &= ~(RS400_DISP1_CRITICAL_POINT_START_MASK |
  2029. RS400_DISP1_CRITICAL_POINT_STOP_MASK);
  2030. WREG32(RS400_DMIF_MEM_CNTL1, (temp |
  2031. (critical_point << RS400_DISP1_CRITICAL_POINT_START_SHIFT) |
  2032. (critical_point << RS400_DISP1_CRITICAL_POINT_STOP_SHIFT)));
  2033. }
  2034. #endif
  2035. DRM_DEBUG("GRPH_BUFFER_CNTL from to %x\n",
  2036. /* (unsigned int)info->SavedReg->grph_buffer_cntl, */
  2037. (unsigned int)RREG32(RADEON_GRPH_BUFFER_CNTL));
  2038. }
  2039. if (mode2) {
  2040. u32 grph2_cntl;
  2041. stop_req = mode2->hdisplay * pixel_bytes2 / 16;
  2042. if (stop_req > max_stop_req)
  2043. stop_req = max_stop_req;
  2044. /*
  2045. Find the drain rate of the display buffer.
  2046. */
  2047. temp_ff.full = rfixed_const((16/pixel_bytes2));
  2048. disp_drain_rate2.full = rfixed_div(pix_clk2, temp_ff);
  2049. grph2_cntl = RREG32(RADEON_GRPH2_BUFFER_CNTL);
  2050. grph2_cntl &= ~(RADEON_GRPH_STOP_REQ_MASK);
  2051. grph2_cntl |= (stop_req << RADEON_GRPH_STOP_REQ_SHIFT);
  2052. grph2_cntl &= ~(RADEON_GRPH_START_REQ_MASK);
  2053. if ((rdev->family == CHIP_R350) &&
  2054. (stop_req > 0x15)) {
  2055. stop_req -= 0x10;
  2056. }
  2057. grph2_cntl |= (stop_req << RADEON_GRPH_START_REQ_SHIFT);
  2058. grph2_cntl |= RADEON_GRPH_BUFFER_SIZE;
  2059. grph2_cntl &= ~(RADEON_GRPH_CRITICAL_CNTL |
  2060. RADEON_GRPH_CRITICAL_AT_SOF |
  2061. RADEON_GRPH_STOP_CNTL);
  2062. if ((rdev->family == CHIP_RS100) ||
  2063. (rdev->family == CHIP_RS200))
  2064. critical_point2 = 0;
  2065. else {
  2066. temp = (rdev->mc.vram_width * rdev->mc.vram_is_ddr + 1)/128;
  2067. temp_ff.full = rfixed_const(temp);
  2068. temp_ff.full = rfixed_mul(mclk_ff, temp_ff);
  2069. if (sclk_ff.full < temp_ff.full)
  2070. temp_ff.full = sclk_ff.full;
  2071. read_return_rate.full = temp_ff.full;
  2072. if (mode1) {
  2073. temp_ff.full = read_return_rate.full - disp_drain_rate.full;
  2074. time_disp1_drop_priority.full = rfixed_div(crit_point_ff, temp_ff);
  2075. } else {
  2076. time_disp1_drop_priority.full = 0;
  2077. }
  2078. crit_point_ff.full = disp_latency.full + time_disp1_drop_priority.full + disp_latency.full;
  2079. crit_point_ff.full = rfixed_mul(crit_point_ff, disp_drain_rate2);
  2080. crit_point_ff.full += rfixed_const_half(0);
  2081. critical_point2 = rfixed_trunc(crit_point_ff);
  2082. if (rdev->disp_priority == 2) {
  2083. critical_point2 = 0;
  2084. }
  2085. if (max_stop_req - critical_point2 < 4)
  2086. critical_point2 = 0;
  2087. }
  2088. if (critical_point2 == 0 && rdev->family == CHIP_R300) {
  2089. /* some R300 cards have problem with this set to 0 */
  2090. critical_point2 = 0x10;
  2091. }
  2092. WREG32(RADEON_GRPH2_BUFFER_CNTL, ((grph2_cntl & ~RADEON_GRPH_CRITICAL_POINT_MASK) |
  2093. (critical_point2 << RADEON_GRPH_CRITICAL_POINT_SHIFT)));
  2094. if ((rdev->family == CHIP_RS400) ||
  2095. (rdev->family == CHIP_RS480)) {
  2096. #if 0
  2097. /* attempt to program RS400 disp2 regs correctly ??? */
  2098. temp = RREG32(RS400_DISP2_REQ_CNTL1);
  2099. temp &= ~(RS400_DISP2_START_REQ_LEVEL_MASK |
  2100. RS400_DISP2_STOP_REQ_LEVEL_MASK);
  2101. WREG32(RS400_DISP2_REQ_CNTL1, (temp |
  2102. (critical_point2 << RS400_DISP1_START_REQ_LEVEL_SHIFT) |
  2103. (critical_point2 << RS400_DISP1_STOP_REQ_LEVEL_SHIFT)));
  2104. temp = RREG32(RS400_DISP2_REQ_CNTL2);
  2105. temp &= ~(RS400_DISP2_CRITICAL_POINT_START_MASK |
  2106. RS400_DISP2_CRITICAL_POINT_STOP_MASK);
  2107. WREG32(RS400_DISP2_REQ_CNTL2, (temp |
  2108. (critical_point2 << RS400_DISP2_CRITICAL_POINT_START_SHIFT) |
  2109. (critical_point2 << RS400_DISP2_CRITICAL_POINT_STOP_SHIFT)));
  2110. #endif
  2111. WREG32(RS400_DISP2_REQ_CNTL1, 0x105DC1CC);
  2112. WREG32(RS400_DISP2_REQ_CNTL2, 0x2749D000);
  2113. WREG32(RS400_DMIF_MEM_CNTL1, 0x29CA71DC);
  2114. WREG32(RS400_DISP1_REQ_CNTL1, 0x28FBC3AC);
  2115. }
  2116. DRM_DEBUG("GRPH2_BUFFER_CNTL from to %x\n",
  2117. (unsigned int)RREG32(RADEON_GRPH2_BUFFER_CNTL));
  2118. }
  2119. }