blob: 521ef6a653568e355845a801690c48c0485fd014 [file] [log] [blame]
Dave Airlief26c4732006-01-02 17:18:39 +11001/* radeon_cp.c -- CP support for Radeon -*- linux-c -*- */
2/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
4 * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
Alex Deucher45e51902008-05-28 13:28:59 +10005 * Copyright 2007 Advanced Micro Devices, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 * Kevin E. Martin <martin@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
30 */
31
32#include "drmP.h"
33#include "drm.h"
Dave Airlie7c1c2872008-11-28 14:22:24 +100034#include "drm_sarea.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "radeon_drm.h"
36#include "radeon_drv.h"
Dave Airlie414ed532005-08-16 20:43:16 +100037#include "r300_reg.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Alex Deucher9f184092008-05-28 11:21:25 +100039#include "radeon_microcode.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define RADEON_FIFO_DEBUG 0
42
Dave Airlie84b1fd12007-07-11 15:53:27 +100043static int radeon_do_cleanup_cp(struct drm_device * dev);
Jerome Glisse54f961a2008-08-13 09:46:31 +100044static void radeon_do_cp_start(drm_radeon_private_t * dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Alex Deucherc05ce082009-02-24 16:22:29 -050046u32 radeon_read_ring_rptr(drm_radeon_private_t *dev_priv, u32 off)
David Millerb07fa022009-02-12 02:15:37 -080047{
48 u32 val;
49
50 if (dev_priv->flags & RADEON_IS_AGP) {
51 val = DRM_READ32(dev_priv->ring_rptr, off);
52 } else {
53 val = *(((volatile u32 *)
54 dev_priv->ring_rptr->handle) +
55 (off / sizeof(u32)));
56 val = le32_to_cpu(val);
57 }
58 return val;
59}
60
61u32 radeon_get_ring_head(drm_radeon_private_t *dev_priv)
62{
63 if (dev_priv->writeback_works)
64 return radeon_read_ring_rptr(dev_priv, 0);
Alex Deucherc05ce082009-02-24 16:22:29 -050065 else {
66 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
67 return RADEON_READ(R600_CP_RB_RPTR);
68 else
69 return RADEON_READ(RADEON_CP_RB_RPTR);
70 }
David Millerb07fa022009-02-12 02:15:37 -080071}
72
Alex Deucherc05ce082009-02-24 16:22:29 -050073void radeon_write_ring_rptr(drm_radeon_private_t *dev_priv, u32 off, u32 val)
David Millerb07fa022009-02-12 02:15:37 -080074{
75 if (dev_priv->flags & RADEON_IS_AGP)
76 DRM_WRITE32(dev_priv->ring_rptr, off, val);
77 else
78 *(((volatile u32 *) dev_priv->ring_rptr->handle) +
79 (off / sizeof(u32))) = cpu_to_le32(val);
80}
81
82void radeon_set_ring_head(drm_radeon_private_t *dev_priv, u32 val)
83{
84 radeon_write_ring_rptr(dev_priv, 0, val);
85}
86
87u32 radeon_get_scratch(drm_radeon_private_t *dev_priv, int index)
88{
Alex Deucherc05ce082009-02-24 16:22:29 -050089 if (dev_priv->writeback_works) {
90 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
91 return radeon_read_ring_rptr(dev_priv,
92 R600_SCRATCHOFF(index));
93 else
94 return radeon_read_ring_rptr(dev_priv,
95 RADEON_SCRATCHOFF(index));
96 } else {
97 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
98 return RADEON_READ(R600_SCRATCH_REG0 + 4*index);
99 else
100 return RADEON_READ(RADEON_SCRATCH_REG0 + 4*index);
101 }
David Millerb07fa022009-02-12 02:15:37 -0800102}
103
Alex Deucherbefb73c2009-02-24 14:02:13 -0500104u32 RADEON_READ_MM(drm_radeon_private_t *dev_priv, int addr)
105{
106 u32 ret;
107
108 if (addr < 0x10000)
109 ret = DRM_READ32(dev_priv->mmio, addr);
110 else {
111 DRM_WRITE32(dev_priv->mmio, RADEON_MM_INDEX, addr);
112 ret = DRM_READ32(dev_priv->mmio, RADEON_MM_DATA);
113 }
114
115 return ret;
116}
117
Alex Deucher45e51902008-05-28 13:28:59 +1000118static u32 R500_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000119{
120 u32 ret;
121 RADEON_WRITE(R520_MC_IND_INDEX, 0x7f0000 | (addr & 0xff));
122 ret = RADEON_READ(R520_MC_IND_DATA);
123 RADEON_WRITE(R520_MC_IND_INDEX, 0);
124 return ret;
125}
126
Alex Deucher45e51902008-05-28 13:28:59 +1000127static u32 RS480_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
128{
129 u32 ret;
130 RADEON_WRITE(RS480_NB_MC_INDEX, addr & 0xff);
131 ret = RADEON_READ(RS480_NB_MC_DATA);
132 RADEON_WRITE(RS480_NB_MC_INDEX, 0xff);
133 return ret;
134}
135
Maciej Cencora60f92682008-02-19 21:32:45 +1000136static u32 RS690_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
137{
Alex Deucher45e51902008-05-28 13:28:59 +1000138 u32 ret;
Maciej Cencora60f92682008-02-19 21:32:45 +1000139 RADEON_WRITE(RS690_MC_INDEX, (addr & RS690_MC_INDEX_MASK));
Alex Deucher45e51902008-05-28 13:28:59 +1000140 ret = RADEON_READ(RS690_MC_DATA);
141 RADEON_WRITE(RS690_MC_INDEX, RS690_MC_INDEX_MASK);
142 return ret;
143}
144
Alex Deucherc1556f72009-02-25 16:57:49 -0500145static u32 RS600_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
146{
147 u32 ret;
148 RADEON_WRITE(RS600_MC_INDEX, ((addr & RS600_MC_ADDR_MASK) |
149 RS600_MC_IND_CITF_ARB0));
150 ret = RADEON_READ(RS600_MC_DATA);
151 return ret;
152}
153
Alex Deucher45e51902008-05-28 13:28:59 +1000154static u32 IGP_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
155{
Alex Deucherf0738e92008-10-16 17:12:02 +1000156 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
157 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +1000158 return RS690_READ_MCIND(dev_priv, addr);
Alex Deucherc1556f72009-02-25 16:57:49 -0500159 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
160 return RS600_READ_MCIND(dev_priv, addr);
Alex Deucher45e51902008-05-28 13:28:59 +1000161 else
162 return RS480_READ_MCIND(dev_priv, addr);
Maciej Cencora60f92682008-02-19 21:32:45 +1000163}
164
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000165u32 radeon_read_fb_location(drm_radeon_private_t *dev_priv)
166{
167
Alex Deucherc05ce082009-02-24 16:22:29 -0500168 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
169 return RADEON_READ(R700_MC_VM_FB_LOCATION);
170 else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
171 return RADEON_READ(R600_MC_VM_FB_LOCATION);
172 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000173 return R500_READ_MCIND(dev_priv, RV515_MC_FB_LOCATION);
Alex Deucherf0738e92008-10-16 17:12:02 +1000174 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
175 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000176 return RS690_READ_MCIND(dev_priv, RS690_MC_FB_LOCATION);
Alex Deucherc1556f72009-02-25 16:57:49 -0500177 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
178 return RS600_READ_MCIND(dev_priv, RS600_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000179 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000180 return R500_READ_MCIND(dev_priv, R520_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000181 else
182 return RADEON_READ(RADEON_MC_FB_LOCATION);
183}
184
185static void radeon_write_fb_location(drm_radeon_private_t *dev_priv, u32 fb_loc)
186{
Alex Deucherc05ce082009-02-24 16:22:29 -0500187 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
188 RADEON_WRITE(R700_MC_VM_FB_LOCATION, fb_loc);
189 else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
190 RADEON_WRITE(R600_MC_VM_FB_LOCATION, fb_loc);
191 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000192 R500_WRITE_MCIND(RV515_MC_FB_LOCATION, fb_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000193 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
194 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000195 RS690_WRITE_MCIND(RS690_MC_FB_LOCATION, fb_loc);
Alex Deucherc1556f72009-02-25 16:57:49 -0500196 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
197 RS600_WRITE_MCIND(RS600_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000198 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000199 R500_WRITE_MCIND(R520_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000200 else
201 RADEON_WRITE(RADEON_MC_FB_LOCATION, fb_loc);
202}
203
Alex Deucherc05ce082009-02-24 16:22:29 -0500204void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_loc)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000205{
Alex Deucherc05ce082009-02-24 16:22:29 -0500206 /*R6xx/R7xx: AGP_TOP and BOT are actually 18 bits each */
207 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770) {
208 RADEON_WRITE(R700_MC_VM_AGP_BOT, agp_loc & 0xffff); /* FIX ME */
209 RADEON_WRITE(R700_MC_VM_AGP_TOP, (agp_loc >> 16) & 0xffff);
210 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
211 RADEON_WRITE(R600_MC_VM_AGP_BOT, agp_loc & 0xffff); /* FIX ME */
212 RADEON_WRITE(R600_MC_VM_AGP_TOP, (agp_loc >> 16) & 0xffff);
213 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000214 R500_WRITE_MCIND(RV515_MC_AGP_LOCATION, agp_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000215 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
216 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000217 RS690_WRITE_MCIND(RS690_MC_AGP_LOCATION, agp_loc);
Alex Deucherc1556f72009-02-25 16:57:49 -0500218 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
219 RS600_WRITE_MCIND(RS600_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000220 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000221 R500_WRITE_MCIND(R520_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000222 else
223 RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc);
224}
225
Alex Deucherc05ce082009-02-24 16:22:29 -0500226void radeon_write_agp_base(drm_radeon_private_t *dev_priv, u64 agp_base)
Dave Airlie70b13d52008-06-19 11:40:44 +1000227{
228 u32 agp_base_hi = upper_32_bits(agp_base);
229 u32 agp_base_lo = agp_base & 0xffffffff;
Alex Deucherc05ce082009-02-24 16:22:29 -0500230 u32 r6xx_agp_base = (agp_base >> 22) & 0x3ffff;
Dave Airlie70b13d52008-06-19 11:40:44 +1000231
Alex Deucherc05ce082009-02-24 16:22:29 -0500232 /* R6xx/R7xx must be aligned to a 4MB boundry */
233 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
234 RADEON_WRITE(R700_MC_VM_AGP_BASE, r6xx_agp_base);
235 else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
236 RADEON_WRITE(R600_MC_VM_AGP_BASE, r6xx_agp_base);
237 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000238 R500_WRITE_MCIND(RV515_MC_AGP_BASE, agp_base_lo);
239 R500_WRITE_MCIND(RV515_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherf0738e92008-10-16 17:12:02 +1000240 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
241 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000242 RS690_WRITE_MCIND(RS690_MC_AGP_BASE, agp_base_lo);
243 RS690_WRITE_MCIND(RS690_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherc1556f72009-02-25 16:57:49 -0500244 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
245 RS600_WRITE_MCIND(RS600_AGP_BASE, agp_base_lo);
246 RS600_WRITE_MCIND(RS600_AGP_BASE_2, agp_base_hi);
Dave Airlie70b13d52008-06-19 11:40:44 +1000247 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515) {
248 R500_WRITE_MCIND(R520_MC_AGP_BASE, agp_base_lo);
249 R500_WRITE_MCIND(R520_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000250 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
251 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Alex Deucher5cfb6952008-06-19 12:38:29 +1000252 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000253 RADEON_WRITE(RS480_AGP_BASE_2, agp_base_hi);
Dave Airlie70b13d52008-06-19 11:40:44 +1000254 } else {
255 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
256 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200)
257 RADEON_WRITE(RADEON_AGP_BASE_2, agp_base_hi);
258 }
259}
260
Alex Deucherc05ce082009-02-24 16:22:29 -0500261void radeon_enable_bm(struct drm_radeon_private *dev_priv)
Dave Airliedd8d7cb2009-02-20 13:28:59 +1000262{
263 u32 tmp;
264 /* Turn on bus mastering */
265 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
266 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
267 /* rs600/rs690/rs740 */
268 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RS600_BUS_MASTER_DIS;
269 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
270 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV350) ||
271 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
272 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
273 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
274 /* r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
275 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
276 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
277 } /* PCIE cards appears to not need this */
278}
279
Dave Airlie84b1fd12007-07-11 15:53:27 +1000280static int RADEON_READ_PLL(struct drm_device * dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
282 drm_radeon_private_t *dev_priv = dev->dev_private;
283
284 RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
285 return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
286}
287
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000288static u32 RADEON_READ_PCIE(drm_radeon_private_t *dev_priv, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
Dave Airlieea98a922005-09-11 20:28:11 +1000290 RADEON_WRITE8(RADEON_PCIE_INDEX, addr & 0xff);
291 return RADEON_READ(RADEON_PCIE_DATA);
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295static void radeon_status(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700297 printk("%s:\n", __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000298 printk("RBBM_STATUS = 0x%08x\n",
299 (unsigned int)RADEON_READ(RADEON_RBBM_STATUS));
300 printk("CP_RB_RTPR = 0x%08x\n",
301 (unsigned int)RADEON_READ(RADEON_CP_RB_RPTR));
302 printk("CP_RB_WTPR = 0x%08x\n",
303 (unsigned int)RADEON_READ(RADEON_CP_RB_WPTR));
304 printk("AIC_CNTL = 0x%08x\n",
305 (unsigned int)RADEON_READ(RADEON_AIC_CNTL));
306 printk("AIC_STAT = 0x%08x\n",
307 (unsigned int)RADEON_READ(RADEON_AIC_STAT));
308 printk("AIC_PT_BASE = 0x%08x\n",
309 (unsigned int)RADEON_READ(RADEON_AIC_PT_BASE));
310 printk("TLB_ADDR = 0x%08x\n",
311 (unsigned int)RADEON_READ(RADEON_AIC_TLB_ADDR));
312 printk("TLB_DATA = 0x%08x\n",
313 (unsigned int)RADEON_READ(RADEON_AIC_TLB_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315#endif
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/* ================================================================
318 * Engine, FIFO control
319 */
320
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321static int radeon_do_pixcache_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 u32 tmp;
324 int i;
325
326 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
327
Alex Deucher259434a2008-05-28 11:51:12 +1000328 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV280) {
329 tmp = RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT);
330 tmp |= RADEON_RB3D_DC_FLUSH_ALL;
331 RADEON_WRITE(RADEON_RB3D_DSTCACHE_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Alex Deucher259434a2008-05-28 11:51:12 +1000333 for (i = 0; i < dev_priv->usec_timeout; i++) {
334 if (!(RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT)
335 & RADEON_RB3D_DC_BUSY)) {
336 return 0;
337 }
338 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Alex Deucher259434a2008-05-28 11:51:12 +1000340 } else {
Jerome Glisse54f961a2008-08-13 09:46:31 +1000341 /* don't flush or purge cache here or lockup */
342 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 DRM_ERROR("failed!\n");
347 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000349 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000352static int radeon_do_wait_for_fifo(drm_radeon_private_t * dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 int i;
355
356 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
357
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 for (i = 0; i < dev_priv->usec_timeout; i++) {
359 int slots = (RADEON_READ(RADEON_RBBM_STATUS)
360 & RADEON_RBBM_FIFOCNT_MASK);
361 if (slots >= entries)
362 return 0;
363 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000365 DRM_DEBUG("wait for fifo failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000366 RADEON_READ(RADEON_RBBM_STATUS),
367 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370 DRM_ERROR("failed!\n");
371 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000373 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374}
375
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376static int radeon_do_wait_for_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 int i, ret;
379
380 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
381
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000382 ret = radeon_do_wait_for_fifo(dev_priv, 64);
383 if (ret)
384 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000386 for (i = 0; i < dev_priv->usec_timeout; i++) {
387 if (!(RADEON_READ(RADEON_RBBM_STATUS)
388 & RADEON_RBBM_ACTIVE)) {
389 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 return 0;
391 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000394 DRM_DEBUG("wait idle failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000395 RADEON_READ(RADEON_RBBM_STATUS),
396 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000399 DRM_ERROR("failed!\n");
400 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000402 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403}
404
Alex Deucher5b92c402008-05-28 11:57:40 +1000405static void radeon_init_pipes(drm_radeon_private_t *dev_priv)
406{
407 uint32_t gb_tile_config, gb_pipe_sel = 0;
408
409 /* RS4xx/RS6xx/R4xx/R5xx */
410 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R420) {
411 gb_pipe_sel = RADEON_READ(R400_GB_PIPE_SELECT);
412 dev_priv->num_gb_pipes = ((gb_pipe_sel >> 12) & 0x3) + 1;
413 } else {
414 /* R3xx */
415 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
416 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350)) {
417 dev_priv->num_gb_pipes = 2;
418 } else {
419 /* R3Vxx */
420 dev_priv->num_gb_pipes = 1;
421 }
422 }
423 DRM_INFO("Num pipes: %d\n", dev_priv->num_gb_pipes);
424
425 gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16 /*| R300_SUBPIXEL_1_16*/);
426
427 switch (dev_priv->num_gb_pipes) {
428 case 2: gb_tile_config |= R300_PIPE_COUNT_R300; break;
429 case 3: gb_tile_config |= R300_PIPE_COUNT_R420_3P; break;
430 case 4: gb_tile_config |= R300_PIPE_COUNT_R420; break;
431 default:
432 case 1: gb_tile_config |= R300_PIPE_COUNT_RV350; break;
433 }
434
435 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV515) {
436 RADEON_WRITE_PLL(R500_DYN_SCLK_PWMEM_PIPE, (1 | ((gb_pipe_sel >> 8) & 0xf) << 4));
Maciej Cencoraaf7ae352009-03-24 01:48:50 +0100437 RADEON_WRITE(R300_SU_REG_DEST, ((1 << dev_priv->num_gb_pipes) - 1));
Alex Deucher5b92c402008-05-28 11:57:40 +1000438 }
439 RADEON_WRITE(R300_GB_TILE_CONFIG, gb_tile_config);
440 radeon_do_wait_for_idle(dev_priv);
441 RADEON_WRITE(R300_DST_PIPE_CONFIG, RADEON_READ(R300_DST_PIPE_CONFIG) | R300_PIPE_AUTO_CONFIG);
442 RADEON_WRITE(R300_RB2D_DSTCACHE_MODE, (RADEON_READ(R300_RB2D_DSTCACHE_MODE) |
443 R300_DC_AUTOFLUSH_ENABLE |
444 R300_DC_DC_DISABLE_IGNORE_PE));
445
446
447}
448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449/* ================================================================
450 * CP control, initialization
451 */
452
453/* Load the microcode for the CP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000454static void radeon_cp_load_microcode(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455{
456 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000459 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 RADEON_WRITE(RADEON_CP_ME_RAM_ADDR, 0);
Alex Deucher9f184092008-05-28 11:21:25 +1000462 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R100) ||
463 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV100) ||
464 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV200) ||
465 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS100) ||
466 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS200)) {
467 DRM_INFO("Loading R100 Microcode\n");
468 for (i = 0; i < 256; i++) {
469 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
470 R100_cp_microcode[i][1]);
471 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
472 R100_cp_microcode[i][0]);
473 }
474 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R200) ||
475 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV250) ||
476 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV280) ||
477 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS300)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 DRM_INFO("Loading R200 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000479 for (i = 0; i < 256; i++) {
480 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
481 R200_cp_microcode[i][1]);
482 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
483 R200_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Alex Deucher9f184092008-05-28 11:21:25 +1000485 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
486 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350) ||
487 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV350) ||
488 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000489 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000490 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 DRM_INFO("Loading R300 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 for (i = 0; i < 256; i++) {
493 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
494 R300_cp_microcode[i][1]);
495 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
496 R300_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
Alex Deucher9f184092008-05-28 11:21:25 +1000498 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
Alex Deucheredc6f382008-10-17 09:21:45 +1000499 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R423) ||
Alex Deucher9f184092008-05-28 11:21:25 +1000500 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV410)) {
501 DRM_INFO("Loading R400 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000502 for (i = 0; i < 256; i++) {
503 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
Alex Deucher9f184092008-05-28 11:21:25 +1000504 R420_cp_microcode[i][1]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
Alex Deucher9f184092008-05-28 11:21:25 +1000506 R420_cp_microcode[i][0]);
507 }
Alex Deucherf0738e92008-10-16 17:12:02 +1000508 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
509 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
510 DRM_INFO("Loading RS690/RS740 Microcode\n");
Alex Deucher9f184092008-05-28 11:21:25 +1000511 for (i = 0; i < 256; i++) {
512 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
513 RS690_cp_microcode[i][1]);
514 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
515 RS690_cp_microcode[i][0]);
516 }
Alex Deucherc1556f72009-02-25 16:57:49 -0500517 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
518 DRM_INFO("Loading RS600 Microcode\n");
519 for (i = 0; i < 256; i++) {
520 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
521 RS600_cp_microcode[i][1]);
522 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
523 RS600_cp_microcode[i][0]);
524 }
Alex Deucher9f184092008-05-28 11:21:25 +1000525 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) ||
526 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R520) ||
527 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) ||
528 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R580) ||
529 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV560) ||
530 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV570)) {
531 DRM_INFO("Loading R500 Microcode\n");
532 for (i = 0; i < 256; i++) {
533 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
534 R520_cp_microcode[i][1]);
535 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
536 R520_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
538 }
539}
540
541/* Flush any pending commands to the CP. This should only be used just
542 * prior to a wait for idle, as it informs the engine that the command
543 * stream is ending.
544 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545static void radeon_do_cp_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#if 0
549 u32 tmp;
550
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 tmp = RADEON_READ(RADEON_CP_RB_WPTR) | (1 << 31);
552 RADEON_WRITE(RADEON_CP_RB_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#endif
554}
555
556/* Wait for the CP to go idle.
557 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000558int radeon_do_cp_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000561 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000563 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 RADEON_PURGE_CACHE();
566 RADEON_PURGE_ZCACHE();
567 RADEON_WAIT_UNTIL_IDLE();
568
569 ADVANCE_RING();
570 COMMIT_RING();
571
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000572 return radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
574
575/* Start the Command Processor.
576 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000577static void radeon_do_cp_start(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
579 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000584 RADEON_WRITE(RADEON_CP_CSQ_CNTL, dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 dev_priv->cp_running = 1;
587
Jerome Glisse54f961a2008-08-13 09:46:31 +1000588 BEGIN_RING(8);
589 /* isync can only be written through cp on r5xx write it here */
590 OUT_RING(CP_PACKET0(RADEON_ISYNC_CNTL, 0));
591 OUT_RING(RADEON_ISYNC_ANY2D_IDLE3D |
592 RADEON_ISYNC_ANY3D_IDLE2D |
593 RADEON_ISYNC_WAIT_IDLEGUI |
594 RADEON_ISYNC_CPSCRATCH_IDLEGUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 RADEON_PURGE_CACHE();
596 RADEON_PURGE_ZCACHE();
597 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ADVANCE_RING();
599 COMMIT_RING();
Jerome Glisse54f961a2008-08-13 09:46:31 +1000600
601 dev_priv->track_flush |= RADEON_FLUSH_EMITED | RADEON_PURGE_EMITED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604/* Reset the Command Processor. This will not flush any pending
605 * commands, so you must wait for the CP command stream to complete
606 * before calling this routine.
607 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608static void radeon_do_cp_reset(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 u32 cur_read_ptr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
614 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
615 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 dev_priv->ring.tail = cur_read_ptr;
617}
618
619/* Stop the Command Processor. This will not flush any pending
620 * commands, so you must flush the command stream and wait for the CP
621 * to go idle before calling this routine.
622 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000623static void radeon_do_cp_stop(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000625 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000627 RADEON_WRITE(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 dev_priv->cp_running = 0;
630}
631
632/* Reset the engine. This will stop the CP if it is running.
633 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000634static int radeon_do_engine_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
636 drm_radeon_private_t *dev_priv = dev->dev_private;
Alex Deucherd396db32008-05-28 11:54:06 +1000637 u32 clock_cntl_index = 0, mclk_cntl = 0, rbbm_soft_reset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000638 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000640 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Alex Deucherd396db32008-05-28 11:54:06 +1000642 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
643 /* may need something similar for newer chips */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000644 clock_cntl_index = RADEON_READ(RADEON_CLOCK_CNTL_INDEX);
645 mclk_cntl = RADEON_READ_PLL(dev, RADEON_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000647 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, (mclk_cntl |
648 RADEON_FORCEON_MCLKA |
649 RADEON_FORCEON_MCLKB |
650 RADEON_FORCEON_YCLKA |
651 RADEON_FORCEON_YCLKB |
652 RADEON_FORCEON_MC |
653 RADEON_FORCEON_AIC));
Alex Deucherd396db32008-05-28 11:54:06 +1000654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Alex Deucherd396db32008-05-28 11:54:06 +1000656 rbbm_soft_reset = RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Alex Deucherd396db32008-05-28 11:54:06 +1000658 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset |
659 RADEON_SOFT_RESET_CP |
660 RADEON_SOFT_RESET_HI |
661 RADEON_SOFT_RESET_SE |
662 RADEON_SOFT_RESET_RE |
663 RADEON_SOFT_RESET_PP |
664 RADEON_SOFT_RESET_E2 |
665 RADEON_SOFT_RESET_RB));
666 RADEON_READ(RADEON_RBBM_SOFT_RESET);
667 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset &
668 ~(RADEON_SOFT_RESET_CP |
669 RADEON_SOFT_RESET_HI |
670 RADEON_SOFT_RESET_SE |
671 RADEON_SOFT_RESET_RE |
672 RADEON_SOFT_RESET_PP |
673 RADEON_SOFT_RESET_E2 |
674 RADEON_SOFT_RESET_RB)));
675 RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Alex Deucherd396db32008-05-28 11:54:06 +1000677 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000678 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, mclk_cntl);
679 RADEON_WRITE(RADEON_CLOCK_CNTL_INDEX, clock_cntl_index);
680 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, rbbm_soft_reset);
681 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Alex Deucher5b92c402008-05-28 11:57:40 +1000683 /* setup the raster pipes */
684 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R300)
685 radeon_init_pipes(dev_priv);
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 /* Reset the CP ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690 /* The CP is no longer running after an engine reset */
691 dev_priv->cp_running = 0;
692
693 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 radeon_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 return 0;
697}
698
Dave Airlie84b1fd12007-07-11 15:53:27 +1000699static void radeon_cp_init_ring_buffer(struct drm_device * dev,
etienne3d161182009-02-20 09:44:45 +1000700 drm_radeon_private_t *dev_priv,
701 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
etienne3d161182009-02-20 09:44:45 +1000703 struct drm_radeon_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 u32 ring_start, cur_read_ptr;
Dave Airliebc5f4522007-11-05 12:50:58 +1000705
Dave Airlied5ea7022006-03-19 19:37:55 +1100706 /* Initialize the memory controller. With new memory map, the fb location
707 * is not changed, it should have been properly initialized already. Part
708 * of the problem is that the code below is bogus, assuming the GART is
709 * always appended to the fb which is not necessarily the case
710 */
711 if (!dev_priv->new_memmap)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000712 radeon_write_fb_location(dev_priv,
Dave Airlied5ea7022006-03-19 19:37:55 +1100713 ((dev_priv->gart_vm_start - 1) & 0xffff0000)
714 | (dev_priv->fb_location >> 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000717 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000718 radeon_write_agp_base(dev_priv, dev->agp->base);
719
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000720 radeon_write_agp_location(dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 (((dev_priv->gart_vm_start - 1 +
722 dev_priv->gart_size) & 0xffff0000) |
723 (dev_priv->gart_vm_start >> 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 ring_start = (dev_priv->cp_ring->offset
726 - dev->agp->base
727 + dev_priv->gart_vm_start);
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100728 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729#endif
730 ring_start = (dev_priv->cp_ring->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100731 - (unsigned long)dev->sg->virtual
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 + dev_priv->gart_vm_start);
733
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000734 RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 /* Set the write pointer delay */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* Initialize the ring buffer's read and write pointers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
741 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
742 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 dev_priv->ring.tail = cur_read_ptr;
744
745#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000746 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000747 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
748 dev_priv->ring_rptr->offset
749 - dev->agp->base + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 } else
751#endif
752 {
David Millere8a89432009-02-12 02:15:44 -0800753 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
754 dev_priv->ring_rptr->offset
755 - ((unsigned long) dev->sg->virtual)
756 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Dave Airlied5ea7022006-03-19 19:37:55 +1100759 /* Set ring buffer size */
760#ifdef __BIG_ENDIAN
761 RADEON_WRITE(RADEON_CP_RB_CNTL,
Roland Scheidegger576cc452008-02-07 14:59:24 +1000762 RADEON_BUF_SWAP_32BIT |
763 (dev_priv->ring.fetch_size_l2ow << 18) |
764 (dev_priv->ring.rptr_update_l2qw << 8) |
765 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100766#else
Roland Scheidegger576cc452008-02-07 14:59:24 +1000767 RADEON_WRITE(RADEON_CP_RB_CNTL,
768 (dev_priv->ring.fetch_size_l2ow << 18) |
769 (dev_priv->ring.rptr_update_l2qw << 8) |
770 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100771#endif
772
Dave Airlied5ea7022006-03-19 19:37:55 +1100773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 /* Initialize the scratch register pointer. This will cause
775 * the scratch register values to be written out to memory
776 * whenever they are updated.
777 *
778 * We simply put this behind the ring read pointer, this works
779 * with PCI GART as well as (whatever kind of) AGP GART
780 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
782 + RADEON_SCRATCH_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Dave Airliedd8d7cb2009-02-20 13:28:59 +1000786 radeon_enable_bm(dev_priv);
Dave Airlied5ea7022006-03-19 19:37:55 +1100787
David Millerb07fa022009-02-12 02:15:37 -0800788 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(0), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000789 RADEON_WRITE(RADEON_LAST_FRAME_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100790
David Millerb07fa022009-02-12 02:15:37 -0800791 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000792 RADEON_WRITE(RADEON_LAST_DISPATCH_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100793
David Millerb07fa022009-02-12 02:15:37 -0800794 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(2), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000795 RADEON_WRITE(RADEON_LAST_CLEAR_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100796
etienne3d161182009-02-20 09:44:45 +1000797 /* reset sarea copies of these */
798 master_priv = file_priv->master->driver_priv;
799 if (master_priv->sarea_priv) {
800 master_priv->sarea_priv->last_frame = 0;
801 master_priv->sarea_priv->last_dispatch = 0;
802 master_priv->sarea_priv->last_clear = 0;
803 }
804
Dave Airlied5ea7022006-03-19 19:37:55 +1100805 radeon_do_wait_for_idle(dev_priv);
806
807 /* Sync everything up */
808 RADEON_WRITE(RADEON_ISYNC_CNTL,
809 (RADEON_ISYNC_ANY2D_IDLE3D |
810 RADEON_ISYNC_ANY3D_IDLE2D |
811 RADEON_ISYNC_WAIT_IDLEGUI |
812 RADEON_ISYNC_CPSCRATCH_IDLEGUI));
813
814}
815
816static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
817{
818 u32 tmp;
819
Dave Airlie6b79d522008-09-02 10:10:16 +1000820 /* Start with assuming that writeback doesn't work */
821 dev_priv->writeback_works = 0;
822
Dave Airlied5ea7022006-03-19 19:37:55 +1100823 /* Writeback doesn't seem to work everywhere, test it here and possibly
824 * enable it if it appears to work
825 */
David Millerb07fa022009-02-12 02:15:37 -0800826 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1), 0);
827
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
David Millerb07fa022009-02-12 02:15:37 -0800831 u32 val;
832
833 val = radeon_read_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1));
834 if (val == 0xdeadbeef)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000839 if (tmp < dev_priv->usec_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 dev_priv->writeback_works = 1;
Dave Airlied5ea7022006-03-19 19:37:55 +1100841 DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 } else {
843 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100844 DRM_INFO("writeback test failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Dave Airlie689b9d72005-09-30 17:09:07 +1000846 if (radeon_no_wb == 1) {
847 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100848 DRM_INFO("writeback forced off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Michel Dänzerae1b1a482006-08-07 20:37:46 +1000850
851 if (!dev_priv->writeback_works) {
852 /* Disable writeback to avoid unnecessary bus master transfer */
853 RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
854 RADEON_RB_NO_UPDATE);
855 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Dave Airlief2b04cd2007-05-08 15:19:23 +1000859/* Enable or disable IGP GART on the chip */
860static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
861{
Maciej Cencora60f92682008-02-19 21:32:45 +1000862 u32 temp;
863
864 if (on) {
Alex Deucher45e51902008-05-28 13:28:59 +1000865 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
Maciej Cencora60f92682008-02-19 21:32:45 +1000866 dev_priv->gart_vm_start,
867 (long)dev_priv->gart_info.bus_addr,
868 dev_priv->gart_size);
869
Alex Deucher45e51902008-05-28 13:28:59 +1000870 temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
Alex Deucherf0738e92008-10-16 17:12:02 +1000871 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
872 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +1000873 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
874 RS690_BLOCK_GFX_D3_EN));
875 else
876 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
Maciej Cencora60f92682008-02-19 21:32:45 +1000877
Alex Deucher45e51902008-05-28 13:28:59 +1000878 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
879 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000880
Alex Deucher45e51902008-05-28 13:28:59 +1000881 temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
882 IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
883 RS480_TLB_ENABLE |
884 RS480_GTW_LAC_EN |
885 RS480_1LEVEL_GART));
Maciej Cencora60f92682008-02-19 21:32:45 +1000886
Dave Airliefa0d71b2008-05-28 11:27:01 +1000887 temp = dev_priv->gart_info.bus_addr & 0xfffff000;
888 temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
Alex Deucher45e51902008-05-28 13:28:59 +1000889 IGP_WRITE_MCIND(RS480_GART_BASE, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000890
Alex Deucher45e51902008-05-28 13:28:59 +1000891 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
892 IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
893 RS480_REQ_TYPE_SNOOP_DIS));
Maciej Cencora60f92682008-02-19 21:32:45 +1000894
Alex Deucher5cfb6952008-06-19 12:38:29 +1000895 radeon_write_agp_base(dev_priv, dev_priv->gart_vm_start);
Dave Airlie3722bfc2008-05-28 11:28:27 +1000896
Maciej Cencora60f92682008-02-19 21:32:45 +1000897 dev_priv->gart_size = 32*1024*1024;
898 temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
899 0xffff0000) | (dev_priv->gart_vm_start >> 16));
900
Alex Deucher45e51902008-05-28 13:28:59 +1000901 radeon_write_agp_location(dev_priv, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000902
Alex Deucher45e51902008-05-28 13:28:59 +1000903 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
904 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
905 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000906
907 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000908 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
909 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000910 break;
911 DRM_UDELAY(1);
912 } while (1);
913
Alex Deucher45e51902008-05-28 13:28:59 +1000914 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
915 RS480_GART_CACHE_INVALIDATE);
Alex Deucher27359772008-05-28 12:54:16 +1000916
Maciej Cencora60f92682008-02-19 21:32:45 +1000917 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000918 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
919 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000920 break;
921 DRM_UDELAY(1);
922 } while (1);
923
Alex Deucher45e51902008-05-28 13:28:59 +1000924 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000925 } else {
Alex Deucher45e51902008-05-28 13:28:59 +1000926 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000927 }
928}
929
Alex Deucherc1556f72009-02-25 16:57:49 -0500930/* Enable or disable IGP GART on the chip */
931static void rs600_set_igpgart(drm_radeon_private_t *dev_priv, int on)
932{
933 u32 temp;
934 int i;
935
936 if (on) {
937 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
938 dev_priv->gart_vm_start,
939 (long)dev_priv->gart_info.bus_addr,
940 dev_priv->gart_size);
941
942 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, (RS600_EFFECTIVE_L2_CACHE_SIZE(6) |
943 RS600_EFFECTIVE_L2_QUEUE_SIZE(6)));
944
945 for (i = 0; i < 19; i++)
946 IGP_WRITE_MCIND(RS600_MC_PT0_CLIENT0_CNTL + i,
947 (RS600_ENABLE_TRANSLATION_MODE_OVERRIDE |
948 RS600_SYSTEM_ACCESS_MODE_IN_SYS |
949 RS600_SYSTEM_APERTURE_UNMAPPED_ACCESS_PASSTHROUGH |
950 RS600_EFFECTIVE_L1_CACHE_SIZE(3) |
951 RS600_ENABLE_FRAGMENT_PROCESSING |
952 RS600_EFFECTIVE_L1_QUEUE_SIZE(3)));
953
954 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_CNTL, (RS600_ENABLE_PAGE_TABLE |
955 RS600_PAGE_TABLE_TYPE_FLAT));
956
957 /* disable all other contexts */
958 for (i = 1; i < 8; i++)
959 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_CNTL + i, 0);
960
961 /* setup the page table aperture */
962 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_BASE_ADDR,
963 dev_priv->gart_info.bus_addr);
964 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_START_ADDR,
965 dev_priv->gart_vm_start);
966 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_END_ADDR,
967 (dev_priv->gart_vm_start + dev_priv->gart_size - 1));
968 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_DEFAULT_READ_ADDR, 0);
969
970 /* setup the system aperture */
971 IGP_WRITE_MCIND(RS600_MC_PT0_SYSTEM_APERTURE_LOW_ADDR,
972 dev_priv->gart_vm_start);
973 IGP_WRITE_MCIND(RS600_MC_PT0_SYSTEM_APERTURE_HIGH_ADDR,
974 (dev_priv->gart_vm_start + dev_priv->gart_size - 1));
975
976 /* enable page tables */
977 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
978 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, (temp | RS600_ENABLE_PT));
979
980 temp = IGP_READ_MCIND(dev_priv, RS600_MC_CNTL1);
981 IGP_WRITE_MCIND(RS600_MC_CNTL1, (temp | RS600_ENABLE_PAGE_TABLES));
982
983 /* invalidate the cache */
984 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
985
986 temp &= ~(RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE);
987 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
988 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
989
990 temp |= RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE;
991 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
992 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
993
994 temp &= ~(RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE);
995 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
996 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
997
998 } else {
999 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, 0);
1000 temp = IGP_READ_MCIND(dev_priv, RS600_MC_CNTL1);
1001 temp &= ~RS600_ENABLE_PAGE_TABLES;
1002 IGP_WRITE_MCIND(RS600_MC_CNTL1, temp);
1003 }
1004}
1005
Dave Airlieea98a922005-09-11 20:28:11 +10001006static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
Dave Airlieea98a922005-09-11 20:28:11 +10001008 u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
1009 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Dave Airlieea98a922005-09-11 20:28:11 +10001011 DRM_DEBUG("programming pcie %08X %08lX %08X\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 dev_priv->gart_vm_start,
1013 (long)dev_priv->gart_info.bus_addr,
Dave Airlieea98a922005-09-11 20:28:11 +10001014 dev_priv->gart_size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001015 RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
1016 dev_priv->gart_vm_start);
1017 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
1018 dev_priv->gart_info.bus_addr);
1019 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
1020 dev_priv->gart_vm_start);
1021 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
1022 dev_priv->gart_vm_start +
1023 dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001025 radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
1028 RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001030 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
1031 tmp & ~RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033}
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/* Enable or disable PCI GART on the chip */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Dave Airlied985c102006-01-02 21:32:48 +11001038 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Alex Deucher45e51902008-05-28 13:28:59 +10001040 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
Alex Deucherf0738e92008-10-16 17:12:02 +10001041 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740) ||
Alex Deucher45e51902008-05-28 13:28:59 +10001042 (dev_priv->flags & RADEON_IS_IGPGART)) {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001043 radeon_set_igpgart(dev_priv, on);
1044 return;
1045 }
1046
Alex Deucherc1556f72009-02-25 16:57:49 -05001047 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
1048 rs600_set_igpgart(dev_priv, on);
1049 return;
1050 }
1051
Dave Airlie54a56ac2006-09-22 04:25:09 +10001052 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieea98a922005-09-11 20:28:11 +10001053 radeon_set_pciegart(dev_priv, on);
1054 return;
1055 }
1056
Dave Airliebc5f4522007-11-05 12:50:58 +10001057 tmp = RADEON_READ(RADEON_AIC_CNTL);
Dave Airlied985c102006-01-02 21:32:48 +11001058
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001059 if (on) {
1060 RADEON_WRITE(RADEON_AIC_CNTL,
1061 tmp | RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 /* set PCI GART page-table base address
1064 */
Dave Airlieea98a922005-09-11 20:28:11 +10001065 RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 /* set address range for PCI address translate
1068 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
1070 RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
1071 + dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 /* Turn off AGP aperture -- is this required for PCI GART?
1074 */
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001075 radeon_write_agp_location(dev_priv, 0xffffffc0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 RADEON_WRITE(RADEON_AIC_CNTL,
1079 tmp & ~RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081}
1082
David Miller6abf6bb2009-02-14 01:51:07 -08001083static int radeon_setup_pcigart_surface(drm_radeon_private_t *dev_priv)
1084{
1085 struct drm_ati_pcigart_info *gart_info = &dev_priv->gart_info;
1086 struct radeon_virt_surface *vp;
1087 int i;
1088
1089 for (i = 0; i < RADEON_MAX_SURFACES * 2; i++) {
1090 if (!dev_priv->virt_surfaces[i].file_priv ||
1091 dev_priv->virt_surfaces[i].file_priv == PCIGART_FILE_PRIV)
1092 break;
1093 }
1094 if (i >= 2 * RADEON_MAX_SURFACES)
1095 return -ENOMEM;
1096 vp = &dev_priv->virt_surfaces[i];
1097
1098 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
1099 struct radeon_surface *sp = &dev_priv->surfaces[i];
1100 if (sp->refcount)
1101 continue;
1102
1103 vp->surface_index = i;
1104 vp->lower = gart_info->bus_addr;
1105 vp->upper = vp->lower + gart_info->table_size;
1106 vp->flags = 0;
1107 vp->file_priv = PCIGART_FILE_PRIV;
1108
1109 sp->refcount = 1;
1110 sp->lower = vp->lower;
1111 sp->upper = vp->upper;
1112 sp->flags = 0;
1113
1114 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, sp->flags);
1115 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * i, sp->lower);
1116 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * i, sp->upper);
1117 return 0;
1118 }
1119
1120 return -ENOMEM;
1121}
1122
Dave Airlie7c1c2872008-11-28 14:22:24 +10001123static int radeon_do_init_cp(struct drm_device *dev, drm_radeon_init_t *init,
1124 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Dave Airlied985c102006-01-02 21:32:48 +11001126 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001127 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
Dave Airlied985c102006-01-02 21:32:48 +11001128
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Dave Airlief3dd5c32006-03-25 18:09:46 +11001131 /* if we require new memory map but we don't have it fail */
Dave Airlie54a56ac2006-09-22 04:25:09 +10001132 if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
Dave Airlieb15ec362006-08-19 17:43:52 +10001133 DRM_ERROR("Cannot initialise DRM on this card\nThis card requires a new X.org DDX for 3D\n");
Dave Airlief3dd5c32006-03-25 18:09:46 +11001134 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001135 return -EINVAL;
Dave Airlief3dd5c32006-03-25 18:09:46 +11001136 }
1137
Dave Airlie54a56ac2006-09-22 04:25:09 +10001138 if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
Dave Airlied985c102006-01-02 21:32:48 +11001139 DRM_DEBUG("Forcing AGP card to PCI mode\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +10001140 dev_priv->flags &= ~RADEON_IS_AGP;
1141 } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
Dave Airlieb15ec362006-08-19 17:43:52 +10001142 && !init->is_pci) {
1143 DRM_DEBUG("Restoring AGP flag\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +10001144 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlied985c102006-01-02 21:32:48 +11001145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Dave Airlie54a56ac2006-09-22 04:25:09 +10001147 if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001148 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001150 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 }
1152
1153 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001154 if (dev_priv->usec_timeout < 1 ||
1155 dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
1156 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001158 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
Dave Airlieddbee332007-07-11 12:16:01 +10001161 /* Enable vblank on CRTC1 for older X servers
1162 */
1163 dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
1164
Dave Airlied985c102006-01-02 21:32:48 +11001165 switch(init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 case RADEON_INIT_R200_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001167 dev_priv->microcode_version = UCODE_R200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 break;
1169 case RADEON_INIT_R300_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170 dev_priv->microcode_version = UCODE_R300;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 break;
1172 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001173 dev_priv->microcode_version = UCODE_R100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 dev_priv->do_boxes = 0;
1177 dev_priv->cp_mode = init->cp_mode;
1178
1179 /* We don't support anything other than bus-mastering ring mode,
1180 * but the ring can be in either AGP or PCI space for the ring
1181 * read pointer.
1182 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001183 if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
1184 (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
1185 DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001187 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001190 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 case 16:
1192 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
1193 break;
1194 case 32:
1195 default:
1196 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
1197 break;
1198 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001199 dev_priv->front_offset = init->front_offset;
1200 dev_priv->front_pitch = init->front_pitch;
1201 dev_priv->back_offset = init->back_offset;
1202 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001204 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 case 16:
1206 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
1207 break;
1208 case 32:
1209 default:
1210 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
1211 break;
1212 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001213 dev_priv->depth_offset = init->depth_offset;
1214 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
1216 /* Hardware state for depth clears. Remove this if/when we no
1217 * longer clear the depth buffer with a 3D rectangle. Hard-code
1218 * all values to prevent unwanted 3D state from slipping through
1219 * and screwing with the clear operation.
1220 */
1221 dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
1222 (dev_priv->color_fmt << 10) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001223 (dev_priv->microcode_version ==
1224 UCODE_R100 ? RADEON_ZBLOCK16 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001226 dev_priv->depth_clear.rb3d_zstencilcntl =
1227 (dev_priv->depth_fmt |
1228 RADEON_Z_TEST_ALWAYS |
1229 RADEON_STENCIL_TEST_ALWAYS |
1230 RADEON_STENCIL_S_FAIL_REPLACE |
1231 RADEON_STENCIL_ZPASS_REPLACE |
1232 RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
1235 RADEON_BFACE_SOLID |
1236 RADEON_FFACE_SOLID |
1237 RADEON_FLAT_SHADE_VTX_LAST |
1238 RADEON_DIFFUSE_SHADE_FLAT |
1239 RADEON_ALPHA_SHADE_FLAT |
1240 RADEON_SPECULAR_SHADE_FLAT |
1241 RADEON_FOG_SHADE_FLAT |
1242 RADEON_VTX_PIX_CENTER_OGL |
1243 RADEON_ROUND_MODE_TRUNC |
1244 RADEON_ROUND_PREC_8TH_PIX);
1245
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 dev_priv->ring_offset = init->ring_offset;
1248 dev_priv->ring_rptr_offset = init->ring_rptr_offset;
1249 dev_priv->buffers_offset = init->buffers_offset;
1250 dev_priv->gart_textures_offset = init->gart_textures_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001251
Dave Airlie7c1c2872008-11-28 14:22:24 +10001252 master_priv->sarea = drm_getsarea(dev);
1253 if (!master_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 DRM_ERROR("could not find sarea!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001256 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 dev_priv->cp_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001260 if (!dev_priv->cp_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 DRM_ERROR("could not find cp ring region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001263 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001266 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 DRM_ERROR("could not find ring read pointer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Dave Airlied1f2b552005-08-05 22:11:22 +10001271 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001273 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 DRM_ERROR("could not find dma buffer region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001276 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001279 if (init->gart_textures_offset) {
1280 dev_priv->gart_textures =
1281 drm_core_findmap(dev, init->gart_textures_offset);
1282 if (!dev_priv->gart_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 DRM_ERROR("could not find GART texture region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001285 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 }
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001290 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie9b8d5a12009-02-07 11:15:41 +10001291 drm_core_ioremap_wc(dev_priv->cp_ring, dev);
1292 drm_core_ioremap_wc(dev_priv->ring_rptr, dev);
1293 drm_core_ioremap_wc(dev->agp_buffer_map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001294 if (!dev_priv->cp_ring->handle ||
1295 !dev_priv->ring_rptr->handle ||
1296 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 DRM_ERROR("could not find ioremap agp regions!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001299 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301 } else
1302#endif
1303 {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001304 dev_priv->cp_ring->handle =
1305 (void *)(unsigned long)dev_priv->cp_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 dev_priv->ring_rptr->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001307 (void *)(unsigned long)dev_priv->ring_rptr->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001308 dev->agp_buffer_map->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001309 (void *)(unsigned long)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001311 DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
1312 dev_priv->cp_ring->handle);
1313 DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
1314 dev_priv->ring_rptr->handle);
1315 DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
1316 dev->agp_buffer_map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001319 dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
Dave Airliebc5f4522007-11-05 12:50:58 +10001320 dev_priv->fb_size =
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001321 ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
Dave Airlied5ea7022006-03-19 19:37:55 +11001322 - dev_priv->fb_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001324 dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
1325 ((dev_priv->front_offset
1326 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001328 dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
1329 ((dev_priv->back_offset
1330 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001332 dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
1333 ((dev_priv->depth_offset
1334 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 dev_priv->gart_size = init->gart_size;
Dave Airlied5ea7022006-03-19 19:37:55 +11001337
1338 /* New let's set the memory map ... */
1339 if (dev_priv->new_memmap) {
1340 u32 base = 0;
1341
1342 DRM_INFO("Setting GART location based on new memory map\n");
1343
1344 /* If using AGP, try to locate the AGP aperture at the same
1345 * location in the card and on the bus, though we have to
1346 * align it down.
1347 */
1348#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001349 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001350 base = dev->agp->base;
1351 /* Check if valid */
Michel Dänzer80b2c382007-02-18 18:03:21 +11001352 if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
1353 base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001354 DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
1355 dev->agp->base);
1356 base = 0;
1357 }
1358 }
1359#endif
1360 /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
1361 if (base == 0) {
1362 base = dev_priv->fb_location + dev_priv->fb_size;
Michel Dänzer80b2c382007-02-18 18:03:21 +11001363 if (base < dev_priv->fb_location ||
1364 ((base + dev_priv->gart_size) & 0xfffffffful) < base)
Dave Airlied5ea7022006-03-19 19:37:55 +11001365 base = dev_priv->fb_location
1366 - dev_priv->gart_size;
Dave Airliebc5f4522007-11-05 12:50:58 +10001367 }
Dave Airlied5ea7022006-03-19 19:37:55 +11001368 dev_priv->gart_vm_start = base & 0xffc00000u;
1369 if (dev_priv->gart_vm_start != base)
1370 DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
1371 base, dev_priv->gart_vm_start);
1372 } else {
1373 DRM_INFO("Setting GART location based on old memory map\n");
1374 dev_priv->gart_vm_start = dev_priv->fb_location +
1375 RADEON_READ(RADEON_CONFIG_APER_SIZE);
1376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
1378#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001379 if (dev_priv->flags & RADEON_IS_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 - dev->agp->base
1382 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 else
1384#endif
1385 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +01001386 - (unsigned long)dev->sg->virtual
1387 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001389 DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
1390 DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
1391 DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
1392 dev_priv->gart_buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001394 dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
1395 dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 + init->ring_size / sizeof(u32));
1397 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001398 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Roland Scheidegger576cc452008-02-07 14:59:24 +10001400 dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
1401 dev_priv->ring.rptr_update_l2qw = drm_order( /* init->rptr_update */ 4096 / 8);
1402
1403 dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
1404 dev_priv->ring.fetch_size_l2ow = drm_order( /* init->fetch_size */ 32 / 16);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1408
1409#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001410 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001412 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 } else
1414#endif
1415 {
David Miller6abf6bb2009-02-14 01:51:07 -08001416 u32 sctrl;
1417 int ret;
1418
Dave Airlieb05c2382008-03-17 10:24:24 +10001419 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +10001420 /* if we have an offset set from userspace */
Dave Airlief2b04cd2007-05-08 15:19:23 +10001421 if (dev_priv->pcigart_offset_set) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422 dev_priv->gart_info.bus_addr =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001423 (resource_size_t)dev_priv->pcigart_offset + dev_priv->fb_location;
Dave Airlief26c4732006-01-02 17:18:39 +11001424 dev_priv->gart_info.mapping.offset =
Dave Airlie7fc86862007-11-05 10:45:27 +10001425 dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
Dave Airlief26c4732006-01-02 17:18:39 +11001426 dev_priv->gart_info.mapping.size =
Dave Airlief2b04cd2007-05-08 15:19:23 +10001427 dev_priv->gart_info.table_size;
Dave Airlief26c4732006-01-02 17:18:39 +11001428
Dave Airlie242e3df2008-07-15 15:48:05 +10001429 drm_core_ioremap_wc(&dev_priv->gart_info.mapping, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 dev_priv->gart_info.addr =
Dave Airlief26c4732006-01-02 17:18:39 +11001431 dev_priv->gart_info.mapping.handle;
Dave Airlieea98a922005-09-11 20:28:11 +10001432
Dave Airlief2b04cd2007-05-08 15:19:23 +10001433 if (dev_priv->flags & RADEON_IS_PCIE)
1434 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
1435 else
1436 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001437 dev_priv->gart_info.gart_table_location =
1438 DRM_ATI_GART_FB;
1439
Dave Airlief26c4732006-01-02 17:18:39 +11001440 DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001441 dev_priv->gart_info.addr,
1442 dev_priv->pcigart_offset);
1443 } else {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001444 if (dev_priv->flags & RADEON_IS_IGPGART)
1445 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
1446 else
1447 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001448 dev_priv->gart_info.gart_table_location =
1449 DRM_ATI_GART_MAIN;
Dave Airlief26c4732006-01-02 17:18:39 +11001450 dev_priv->gart_info.addr = NULL;
1451 dev_priv->gart_info.bus_addr = 0;
Dave Airlie54a56ac2006-09-22 04:25:09 +10001452 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001453 DRM_ERROR
1454 ("Cannot use PCI Express without GART in FB memory\n");
Dave Airlieea98a922005-09-11 20:28:11 +10001455 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001456 return -EINVAL;
Dave Airlieea98a922005-09-11 20:28:11 +10001457 }
1458 }
1459
David Miller6abf6bb2009-02-14 01:51:07 -08001460 sctrl = RADEON_READ(RADEON_SURFACE_CNTL);
1461 RADEON_WRITE(RADEON_SURFACE_CNTL, 0);
Alex Deucherc1556f72009-02-25 16:57:49 -05001462 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
1463 ret = r600_page_table_init(dev);
1464 else
1465 ret = drm_ati_pcigart_init(dev, &dev_priv->gart_info);
David Miller6abf6bb2009-02-14 01:51:07 -08001466 RADEON_WRITE(RADEON_SURFACE_CNTL, sctrl);
1467
1468 if (!ret) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001469 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001471 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
David Miller6abf6bb2009-02-14 01:51:07 -08001474 ret = radeon_setup_pcigart_surface(dev_priv);
1475 if (ret) {
1476 DRM_ERROR("failed to setup GART surface!\n");
Alex Deucherc1556f72009-02-25 16:57:49 -05001477 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
1478 r600_page_table_cleanup(dev, &dev_priv->gart_info);
1479 else
1480 drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info);
David Miller6abf6bb2009-02-14 01:51:07 -08001481 radeon_do_cleanup_cp(dev);
1482 return ret;
1483 }
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001486 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 }
1488
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001489 radeon_cp_load_microcode(dev_priv);
etienne3d161182009-02-20 09:44:45 +10001490 radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 dev_priv->last_buf = 0;
1493
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001494 radeon_do_engine_reset(dev);
Dave Airlied5ea7022006-03-19 19:37:55 +11001495 radeon_test_writeback(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 return 0;
1498}
1499
Dave Airlie84b1fd12007-07-11 15:53:27 +10001500static int radeon_do_cleanup_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001503 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /* Make sure interrupts are disabled here because the uninstall ioctl
1506 * may not have been called from userspace and after dev_private
1507 * is freed, it's too late.
1508 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001509 if (dev->irq_enabled)
1510 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001513 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied985c102006-01-02 21:32:48 +11001514 if (dev_priv->cp_ring != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 drm_core_ioremapfree(dev_priv->cp_ring, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001516 dev_priv->cp_ring = NULL;
1517 }
1518 if (dev_priv->ring_rptr != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001519 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001520 dev_priv->ring_rptr = NULL;
1521 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001522 if (dev->agp_buffer_map != NULL) {
1523 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 dev->agp_buffer_map = NULL;
1525 }
1526 } else
1527#endif
1528 {
Dave Airlied985c102006-01-02 21:32:48 +11001529
1530 if (dev_priv->gart_info.bus_addr) {
1531 /* Turn off PCI GART */
1532 radeon_set_pcigart(dev_priv, 0);
Alex Deucherc1556f72009-02-25 16:57:49 -05001533 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
1534 r600_page_table_cleanup(dev, &dev_priv->gart_info);
1535 else {
1536 if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
1537 DRM_ERROR("failed to cleanup PCI GART!\n");
1538 }
Dave Airlied985c102006-01-02 21:32:48 +11001539 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540
Dave Airlied985c102006-01-02 21:32:48 +11001541 if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
1542 {
Dave Airlief26c4732006-01-02 17:18:39 +11001543 drm_core_ioremapfree(&dev_priv->gart_info.mapping, dev);
Hannes Eder8f497aa2009-03-05 20:14:18 +01001544 dev_priv->gart_info.addr = NULL;
Dave Airlieea98a922005-09-11 20:28:11 +10001545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 /* only clear to the start of flags */
1548 memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
1549
1550 return 0;
1551}
1552
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001553/* This code will reinit the Radeon CP hardware after a resume from disc.
1554 * AFAIK, it would be very difficult to pickle the state at suspend time, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 * here we make sure that all Radeon hardware initialisation is re-done without
1556 * affecting running applications.
1557 *
1558 * Charl P. Botha <http://cpbotha.net>
1559 */
etienne3d161182009-02-20 09:44:45 +10001560static int radeon_do_resume_cp(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
1562 drm_radeon_private_t *dev_priv = dev->dev_private;
1563
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 if (!dev_priv) {
1565 DRM_ERROR("Called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001566 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 }
1568
1569 DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1570
1571#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001572 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001574 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 } else
1576#endif
1577 {
1578 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001579 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 }
1581
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001582 radeon_cp_load_microcode(dev_priv);
etienne3d161182009-02-20 09:44:45 +10001583 radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001585 radeon_do_engine_reset(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001586 radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 DRM_DEBUG("radeon_do_resume_cp() complete\n");
1589
1590 return 0;
1591}
1592
Eric Anholtc153f452007-09-03 12:06:45 +10001593int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
Alex Deucherc05ce082009-02-24 16:22:29 -05001595 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001596 drm_radeon_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Eric Anholt6c340ea2007-08-25 20:23:09 +10001598 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Eric Anholtc153f452007-09-03 12:06:45 +10001600 if (init->func == RADEON_INIT_R300_CP)
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001601 r300_init_reg_flags(dev);
Dave Airlie414ed532005-08-16 20:43:16 +10001602
Eric Anholtc153f452007-09-03 12:06:45 +10001603 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 case RADEON_INIT_CP:
1605 case RADEON_INIT_R200_CP:
1606 case RADEON_INIT_R300_CP:
Dave Airlie7c1c2872008-11-28 14:22:24 +10001607 return radeon_do_init_cp(dev, init, file_priv);
Alex Deucherc05ce082009-02-24 16:22:29 -05001608 case RADEON_INIT_R600_CP:
1609 return r600_do_init_cp(dev, init, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 case RADEON_CLEANUP_CP:
Alex Deucherc05ce082009-02-24 16:22:29 -05001611 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1612 return r600_do_cleanup_cp(dev);
1613 else
1614 return radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616
Eric Anholt20caafa2007-08-25 19:22:43 +10001617 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Eric Anholtc153f452007-09-03 12:06:45 +10001620int radeon_cp_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001623 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Eric Anholt6c340ea2007-08-25 20:23:09 +10001625 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001627 if (dev_priv->cp_running) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001628 DRM_DEBUG("while CP running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 return 0;
1630 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001631 if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001632 DRM_DEBUG("called with bogus CP mode (%d)\n",
1633 dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 return 0;
1635 }
1636
Alex Deucherc05ce082009-02-24 16:22:29 -05001637 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1638 r600_do_cp_start(dev_priv);
1639 else
1640 radeon_do_cp_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
1642 return 0;
1643}
1644
1645/* Stop the CP. The engine must have been idled before calling this
1646 * routine.
1647 */
Eric Anholtc153f452007-09-03 12:06:45 +10001648int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001651 drm_radeon_cp_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001653 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Eric Anholt6c340ea2007-08-25 20:23:09 +10001655 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 if (!dev_priv->cp_running)
1658 return 0;
1659
1660 /* Flush any pending CP commands. This ensures any outstanding
1661 * commands are exectuted by the engine before we turn it off.
1662 */
Eric Anholtc153f452007-09-03 12:06:45 +10001663 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001664 radeon_do_cp_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666
1667 /* If we fail to make the engine go idle, we return an error
1668 * code so that the DRM ioctl wrapper can try again.
1669 */
Eric Anholtc153f452007-09-03 12:06:45 +10001670 if (stop->idle) {
Alex Deucherc05ce082009-02-24 16:22:29 -05001671 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1672 ret = r600_do_cp_idle(dev_priv);
1673 else
1674 ret = radeon_do_cp_idle(dev_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001675 if (ret)
1676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678
1679 /* Finally, we can turn off the CP. If the engine isn't idle,
1680 * we will get some dropped triangles as they won't be fully
1681 * rendered before the CP is shut down.
1682 */
Alex Deucherc05ce082009-02-24 16:22:29 -05001683 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1684 r600_do_cp_stop(dev_priv);
1685 else
1686 radeon_do_cp_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688 /* Reset the engine */
Alex Deucherc05ce082009-02-24 16:22:29 -05001689 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1690 r600_do_engine_reset(dev);
1691 else
1692 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
1694 return 0;
1695}
1696
Dave Airlie84b1fd12007-07-11 15:53:27 +10001697void radeon_do_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 drm_radeon_private_t *dev_priv = dev->dev_private;
1700 int i, ret;
1701
1702 if (dev_priv) {
1703 if (dev_priv->cp_running) {
1704 /* Stop the cp */
Dave Airlie53c379e2009-03-09 12:12:28 +10001705 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
Alex Deucherc05ce082009-02-24 16:22:29 -05001706 while ((ret = r600_do_cp_idle(dev_priv)) != 0) {
1707 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708#ifdef __linux__
Alex Deucherc05ce082009-02-24 16:22:29 -05001709 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710#else
Alex Deucherc05ce082009-02-24 16:22:29 -05001711 tsleep(&ret, PZERO, "rdnrel", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712#endif
Alex Deucherc05ce082009-02-24 16:22:29 -05001713 }
1714 } else {
1715 while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
1716 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1717#ifdef __linux__
1718 schedule();
1719#else
1720 tsleep(&ret, PZERO, "rdnrel", 1);
1721#endif
1722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
Alex Deucherc05ce082009-02-24 16:22:29 -05001724 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
1725 r600_do_cp_stop(dev_priv);
1726 r600_do_engine_reset(dev);
1727 } else {
1728 radeon_do_cp_stop(dev_priv);
1729 radeon_do_engine_reset(dev);
1730 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
1732
Alex Deucherc05ce082009-02-24 16:22:29 -05001733 if ((dev_priv->flags & RADEON_FAMILY_MASK) < CHIP_R600) {
1734 /* Disable *all* interrupts */
1735 if (dev_priv->mmio) /* remove this after permanent addmaps */
1736 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
Alex Deucherc05ce082009-02-24 16:22:29 -05001738 if (dev_priv->mmio) { /* remove all surfaces */
1739 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
1740 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
1741 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
1742 16 * i, 0);
1743 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
1744 16 * i, 0);
1745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 }
1747 }
1748
1749 /* Free memory heap structures */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001750 radeon_mem_takedown(&(dev_priv->gart_heap));
1751 radeon_mem_takedown(&(dev_priv->fb_heap));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
1753 /* deallocate kernel resources */
Alex Deucherc05ce082009-02-24 16:22:29 -05001754 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1755 r600_do_cleanup_cp(dev);
1756 else
1757 radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759}
1760
1761/* Just reset the CP ring. Called as part of an X Server engine reset.
1762 */
Eric Anholtc153f452007-09-03 12:06:45 +10001763int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001766 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Eric Anholt6c340ea2007-08-25 20:23:09 +10001768 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001770 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001771 DRM_DEBUG("called before init done\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001772 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 }
1774
Alex Deucherc05ce082009-02-24 16:22:29 -05001775 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1776 r600_do_cp_reset(dev_priv);
1777 else
1778 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 /* The CP is no longer running after an engine reset */
1781 dev_priv->cp_running = 0;
1782
1783 return 0;
1784}
1785
Eric Anholtc153f452007-09-03 12:06:45 +10001786int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001789 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Eric Anholt6c340ea2007-08-25 20:23:09 +10001791 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Alex Deucherc05ce082009-02-24 16:22:29 -05001793 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1794 return r600_do_cp_idle(dev_priv);
1795 else
1796 return radeon_do_cp_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
1798
1799/* Added by Charl P. Botha to call radeon_do_resume_cp().
1800 */
Eric Anholtc153f452007-09-03 12:06:45 +10001801int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Alex Deucherc05ce082009-02-24 16:22:29 -05001803 drm_radeon_private_t *dev_priv = dev->dev_private;
1804 DRM_DEBUG("\n");
1805
1806 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1807 return r600_do_resume_cp(dev, file_priv);
1808 else
1809 return radeon_do_resume_cp(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
Eric Anholtc153f452007-09-03 12:06:45 +10001812int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Alex Deucherc05ce082009-02-24 16:22:29 -05001814 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001815 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Eric Anholt6c340ea2007-08-25 20:23:09 +10001817 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
Alex Deucherc05ce082009-02-24 16:22:29 -05001819 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1820 return r600_do_engine_reset(dev);
1821 else
1822 return radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823}
1824
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825/* ================================================================
1826 * Fullscreen mode
1827 */
1828
1829/* KW: Deprecated to say the least:
1830 */
Eric Anholtc153f452007-09-03 12:06:45 +10001831int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
1833 return 0;
1834}
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836/* ================================================================
1837 * Freelist management
1838 */
1839
1840/* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1841 * bufs until freelist code is used. Note this hides a problem with
1842 * the scratch register * (used to keep track of last buffer
1843 * completed) being written to before * the last buffer has actually
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001844 * completed rendering.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 *
1846 * KW: It's also a good way to find free buffers quickly.
1847 *
1848 * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1849 * sleep. However, bugs in older versions of radeon_accel.c mean that
1850 * we essentially have to do this, else old clients will break.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001851 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 * However, it does leave open a potential deadlock where all the
1853 * buffers are held by other clients, which can't release them because
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001854 * they can't get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 */
1856
Dave Airlie056219e2007-07-11 16:17:42 +10001857struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Dave Airliecdd55a22007-07-11 16:32:08 +10001859 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 drm_radeon_private_t *dev_priv = dev->dev_private;
1861 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001862 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 int i, t;
1864 int start;
1865
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001866 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 dev_priv->last_buf = 0;
1868
1869 start = dev_priv->last_buf;
1870
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001871 for (t = 0; t < dev_priv->usec_timeout; t++) {
David Millerb07fa022009-02-12 02:15:37 -08001872 u32 done_age = GET_SCRATCH(dev_priv, 1);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001873 DRM_DEBUG("done_age = %d\n", done_age);
1874 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 buf = dma->buflist[i];
1876 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001877 if (buf->file_priv == NULL || (buf->pending &&
1878 buf_priv->age <=
1879 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 dev_priv->stats.requested_bufs++;
1881 buf->pending = 0;
1882 return buf;
1883 }
1884 start = 0;
1885 }
1886
1887 if (t) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001888 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 dev_priv->stats.freelist_loops++;
1890 }
1891 }
1892
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001893 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 return NULL;
1895}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897#if 0
Dave Airlie056219e2007-07-11 16:17:42 +10001898struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899{
Dave Airliecdd55a22007-07-11 16:32:08 +10001900 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 drm_radeon_private_t *dev_priv = dev->dev_private;
1902 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001903 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 int i, t;
1905 int start;
David Millerb07fa022009-02-12 02:15:37 -08001906 u32 done_age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
David Millerb07fa022009-02-12 02:15:37 -08001908 done_age = radeon_read_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1));
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001909 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 dev_priv->last_buf = 0;
1911
1912 start = dev_priv->last_buf;
1913 dev_priv->stats.freelist_loops++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001914
1915 for (t = 0; t < 2; t++) {
1916 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 buf = dma->buflist[i];
1918 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001919 if (buf->file_priv == 0 || (buf->pending &&
1920 buf_priv->age <=
1921 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 dev_priv->stats.requested_bufs++;
1923 buf->pending = 0;
1924 return buf;
1925 }
1926 }
1927 start = 0;
1928 }
1929
1930 return NULL;
1931}
1932#endif
1933
Dave Airlie84b1fd12007-07-11 15:53:27 +10001934void radeon_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
Dave Airliecdd55a22007-07-11 16:32:08 +10001936 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 drm_radeon_private_t *dev_priv = dev->dev_private;
1938 int i;
1939
1940 dev_priv->last_buf = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001941 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001942 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1944 buf_priv->age = 0;
1945 }
1946}
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948/* ================================================================
1949 * CP command submission
1950 */
1951
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001952int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
1954 drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1955 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001956 u32 last_head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001958 for (i = 0; i < dev_priv->usec_timeout; i++) {
1959 u32 head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 ring->space = (head - ring->tail) * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001962 if (ring->space <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 ring->space += ring->size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001964 if (ring->space > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
1968
1969 if (head != last_head)
1970 i = 0;
1971 last_head = head;
1972
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001973 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975
1976 /* FIXME: This return value is ignored in the BEGIN_RING macro! */
1977#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001978 radeon_status(dev_priv);
1979 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980#endif
Eric Anholt20caafa2007-08-25 19:22:43 +10001981 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983
Eric Anholt6c340ea2007-08-25 20:23:09 +10001984static int radeon_cp_get_buffers(struct drm_device *dev,
1985 struct drm_file *file_priv,
Dave Airliec60ce622007-07-11 15:27:12 +10001986 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001989 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001991 for (i = d->granted_count; i < d->request_count; i++) {
1992 buf = radeon_freelist_get(dev);
1993 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001994 return -EBUSY; /* NOTE: broken client */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Eric Anholt6c340ea2007-08-25 20:23:09 +10001996 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001998 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
1999 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002000 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002001 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
2002 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002003 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 d->granted_count++;
2006 }
2007 return 0;
2008}
2009
Eric Anholtc153f452007-09-03 12:06:45 +10002010int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011{
Dave Airliecdd55a22007-07-11 16:32:08 +10002012 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10002014 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015
Eric Anholt6c340ea2007-08-25 20:23:09 +10002016 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 /* Please don't send us buffers.
2019 */
Eric Anholtc153f452007-09-03 12:06:45 +10002020 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002021 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002022 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10002023 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025
2026 /* We'll send you buffers.
2027 */
Eric Anholtc153f452007-09-03 12:06:45 +10002028 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002029 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002030 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10002031 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 }
2033
Eric Anholtc153f452007-09-03 12:06:45 +10002034 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Eric Anholtc153f452007-09-03 12:06:45 +10002036 if (d->request_count) {
2037 ret = radeon_cp_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return ret;
2041}
2042
Dave Airlie22eae942005-11-10 22:16:34 +11002043int radeon_driver_load(struct drm_device *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
2045 drm_radeon_private_t *dev_priv;
2046 int ret = 0;
2047
2048 dev_priv = drm_alloc(sizeof(drm_radeon_private_t), DRM_MEM_DRIVER);
2049 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10002050 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 memset(dev_priv, 0, sizeof(drm_radeon_private_t));
2053 dev->dev_private = (void *)dev_priv;
2054 dev_priv->flags = flags;
2055
Dave Airlie54a56ac2006-09-22 04:25:09 +10002056 switch (flags & RADEON_FAMILY_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 case CHIP_R100:
2058 case CHIP_RV200:
2059 case CHIP_R200:
2060 case CHIP_R300:
Dave Airlieb15ec362006-08-19 17:43:52 +10002061 case CHIP_R350:
Dave Airlie414ed532005-08-16 20:43:16 +10002062 case CHIP_R420:
Alex Deucheredc6f382008-10-17 09:21:45 +10002063 case CHIP_R423:
Dave Airlieb15ec362006-08-19 17:43:52 +10002064 case CHIP_RV410:
Dave Airlie3d5e2c12008-02-07 15:01:05 +10002065 case CHIP_RV515:
2066 case CHIP_R520:
2067 case CHIP_RV570:
2068 case CHIP_R580:
Dave Airlie54a56ac2006-09-22 04:25:09 +10002069 dev_priv->flags |= RADEON_HAS_HIERZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 break;
2071 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002072 /* all other chips have no hierarchical z buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 break;
2074 }
Dave Airlie414ed532005-08-16 20:43:16 +10002075
2076 if (drm_device_is_agp(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10002077 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlieb15ec362006-08-19 17:43:52 +10002078 else if (drm_device_is_pcie(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10002079 dev_priv->flags |= RADEON_IS_PCIE;
Dave Airlieb15ec362006-08-19 17:43:52 +10002080 else
Dave Airlie54a56ac2006-09-22 04:25:09 +10002081 dev_priv->flags |= RADEON_IS_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +10002082
Dave Airlie78538bf2008-11-11 17:56:16 +10002083 ret = drm_addmap(dev, drm_get_resource_start(dev, 2),
2084 drm_get_resource_len(dev, 2), _DRM_REGISTERS,
2085 _DRM_READ_ONLY | _DRM_DRIVER, &dev_priv->mmio);
2086 if (ret != 0)
2087 return ret;
2088
Keith Packard52440212008-11-18 09:30:25 -08002089 ret = drm_vblank_init(dev, 2);
2090 if (ret) {
2091 radeon_driver_unload(dev);
2092 return ret;
2093 }
2094
Dave Airlie414ed532005-08-16 20:43:16 +10002095 DRM_DEBUG("%s card detected\n",
Dave Airlie54a56ac2006-09-22 04:25:09 +10002096 ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 return ret;
2098}
2099
Dave Airlie7c1c2872008-11-28 14:22:24 +10002100int radeon_master_create(struct drm_device *dev, struct drm_master *master)
2101{
2102 struct drm_radeon_master_private *master_priv;
2103 unsigned long sareapage;
2104 int ret;
2105
2106 master_priv = drm_calloc(1, sizeof(*master_priv), DRM_MEM_DRIVER);
2107 if (!master_priv)
2108 return -ENOMEM;
2109
2110 /* prebuild the SAREA */
Dave Airliebdf539a2008-12-18 16:56:11 +10002111 sareapage = max_t(unsigned long, SAREA_MAX, PAGE_SIZE);
Dave Airliedf4f7fe2009-06-11 16:16:10 +10002112 ret = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK,
Dave Airlie7c1c2872008-11-28 14:22:24 +10002113 &master_priv->sarea);
2114 if (ret) {
2115 DRM_ERROR("SAREA setup failed\n");
2116 return ret;
2117 }
2118 master_priv->sarea_priv = master_priv->sarea->handle + sizeof(struct drm_sarea);
2119 master_priv->sarea_priv->pfCurrentPage = 0;
2120
2121 master->driver_priv = master_priv;
2122 return 0;
2123}
2124
2125void radeon_master_destroy(struct drm_device *dev, struct drm_master *master)
2126{
2127 struct drm_radeon_master_private *master_priv = master->driver_priv;
2128
2129 if (!master_priv)
2130 return;
2131
2132 if (master_priv->sarea_priv &&
2133 master_priv->sarea_priv->pfCurrentPage != 0)
2134 radeon_cp_dispatch_flip(dev, master);
2135
2136 master_priv->sarea_priv = NULL;
2137 if (master_priv->sarea)
Dave Airlie4e74f362008-12-19 10:23:14 +11002138 drm_rmmap_locked(dev, master_priv->sarea);
Dave Airlie7c1c2872008-11-28 14:22:24 +10002139
2140 drm_free(master_priv, sizeof(*master_priv), DRM_MEM_DRIVER);
2141
2142 master->driver_priv = NULL;
2143}
2144
Dave Airlie22eae942005-11-10 22:16:34 +11002145/* Create mappings for registers and framebuffer so userland doesn't necessarily
2146 * have to find them.
2147 */
2148int radeon_driver_firstopen(struct drm_device *dev)
Dave Airlie836cf042005-07-10 19:27:04 +10002149{
2150 int ret;
2151 drm_local_map_t *map;
2152 drm_radeon_private_t *dev_priv = dev->dev_private;
2153
Dave Airlief2b04cd2007-05-08 15:19:23 +10002154 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
2155
Dave Airlie7fc86862007-11-05 10:45:27 +10002156 dev_priv->fb_aper_offset = drm_get_resource_start(dev, 0);
2157 ret = drm_addmap(dev, dev_priv->fb_aper_offset,
Dave Airlie836cf042005-07-10 19:27:04 +10002158 drm_get_resource_len(dev, 0), _DRM_FRAME_BUFFER,
2159 _DRM_WRITE_COMBINING, &map);
2160 if (ret != 0)
2161 return ret;
2162
2163 return 0;
2164}
2165
Dave Airlie22eae942005-11-10 22:16:34 +11002166int radeon_driver_unload(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167{
2168 drm_radeon_private_t *dev_priv = dev->dev_private;
2169
2170 DRM_DEBUG("\n");
Dave Airlie78538bf2008-11-11 17:56:16 +10002171
2172 drm_rmmap(dev, dev_priv->mmio);
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER);
2175
2176 dev->dev_private = NULL;
2177 return 0;
2178}
Dave Airlie4247ca92009-02-20 13:28:34 +10002179
2180void radeon_commit_ring(drm_radeon_private_t *dev_priv)
2181{
2182 int i;
2183 u32 *ring;
2184 int tail_aligned;
2185
2186 /* check if the ring is padded out to 16-dword alignment */
2187
2188 tail_aligned = dev_priv->ring.tail & 0xf;
2189 if (tail_aligned) {
2190 int num_p2 = 16 - tail_aligned;
2191
2192 ring = dev_priv->ring.start;
2193 /* pad with some CP_PACKET2 */
2194 for (i = 0; i < num_p2; i++)
2195 ring[dev_priv->ring.tail + i] = CP_PACKET2();
2196
2197 dev_priv->ring.tail += i;
2198
2199 dev_priv->ring.space -= num_p2 * sizeof(u32);
2200 }
2201
2202 dev_priv->ring.tail &= dev_priv->ring.tail_mask;
2203
2204 DRM_MEMORYBARRIER();
2205 GET_RING_HEAD( dev_priv );
2206
Alex Deucherc05ce082009-02-24 16:22:29 -05002207 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
2208 RADEON_WRITE(R600_CP_RB_WPTR, dev_priv->ring.tail);
2209 /* read from PCI bus to ensure correct posting */
2210 RADEON_READ(R600_CP_RB_RPTR);
2211 } else {
2212 RADEON_WRITE(RADEON_CP_RB_WPTR, dev_priv->ring.tail);
2213 /* read from PCI bus to ensure correct posting */
2214 RADEON_READ(RADEON_CP_RB_RPTR);
2215 }
Dave Airlie4247ca92009-02-20 13:28:34 +10002216}