blob: 78a058fc039fa98efaafb671abd6f7e0610b59fd [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
David Millerb07fa022009-02-12 02:15:37 -080046static u32 radeon_read_ring_rptr(drm_radeon_private_t *dev_priv, u32 off)
47{
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);
65 else
66 return RADEON_READ(RADEON_CP_RB_RPTR);
67}
68
69static void radeon_write_ring_rptr(drm_radeon_private_t *dev_priv, u32 off, u32 val)
70{
71 if (dev_priv->flags & RADEON_IS_AGP)
72 DRM_WRITE32(dev_priv->ring_rptr, off, val);
73 else
74 *(((volatile u32 *) dev_priv->ring_rptr->handle) +
75 (off / sizeof(u32))) = cpu_to_le32(val);
76}
77
78void radeon_set_ring_head(drm_radeon_private_t *dev_priv, u32 val)
79{
80 radeon_write_ring_rptr(dev_priv, 0, val);
81}
82
83u32 radeon_get_scratch(drm_radeon_private_t *dev_priv, int index)
84{
85 if (dev_priv->writeback_works)
86 return radeon_read_ring_rptr(dev_priv,
87 RADEON_SCRATCHOFF(index));
88 else
89 return RADEON_READ(RADEON_SCRATCH_REG0 + 4*index);
90}
91
Alex Deucher45e51902008-05-28 13:28:59 +100092static u32 R500_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
Dave Airlie3d5e2c12008-02-07 15:01:05 +100093{
94 u32 ret;
95 RADEON_WRITE(R520_MC_IND_INDEX, 0x7f0000 | (addr & 0xff));
96 ret = RADEON_READ(R520_MC_IND_DATA);
97 RADEON_WRITE(R520_MC_IND_INDEX, 0);
98 return ret;
99}
100
Alex Deucher45e51902008-05-28 13:28:59 +1000101static u32 RS480_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
102{
103 u32 ret;
104 RADEON_WRITE(RS480_NB_MC_INDEX, addr & 0xff);
105 ret = RADEON_READ(RS480_NB_MC_DATA);
106 RADEON_WRITE(RS480_NB_MC_INDEX, 0xff);
107 return ret;
108}
109
Maciej Cencora60f92682008-02-19 21:32:45 +1000110static u32 RS690_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
111{
Alex Deucher45e51902008-05-28 13:28:59 +1000112 u32 ret;
Maciej Cencora60f92682008-02-19 21:32:45 +1000113 RADEON_WRITE(RS690_MC_INDEX, (addr & RS690_MC_INDEX_MASK));
Alex Deucher45e51902008-05-28 13:28:59 +1000114 ret = RADEON_READ(RS690_MC_DATA);
115 RADEON_WRITE(RS690_MC_INDEX, RS690_MC_INDEX_MASK);
116 return ret;
117}
118
119static u32 IGP_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
120{
Alex Deucherf0738e92008-10-16 17:12:02 +1000121 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
122 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +1000123 return RS690_READ_MCIND(dev_priv, addr);
124 else
125 return RS480_READ_MCIND(dev_priv, addr);
Maciej Cencora60f92682008-02-19 21:32:45 +1000126}
127
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000128u32 radeon_read_fb_location(drm_radeon_private_t *dev_priv)
129{
130
131 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000132 return R500_READ_MCIND(dev_priv, RV515_MC_FB_LOCATION);
Alex Deucherf0738e92008-10-16 17:12:02 +1000133 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
134 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000135 return RS690_READ_MCIND(dev_priv, RS690_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000136 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000137 return R500_READ_MCIND(dev_priv, R520_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000138 else
139 return RADEON_READ(RADEON_MC_FB_LOCATION);
140}
141
142static void radeon_write_fb_location(drm_radeon_private_t *dev_priv, u32 fb_loc)
143{
144 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000145 R500_WRITE_MCIND(RV515_MC_FB_LOCATION, fb_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000146 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
147 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000148 RS690_WRITE_MCIND(RS690_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000149 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000150 R500_WRITE_MCIND(R520_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000151 else
152 RADEON_WRITE(RADEON_MC_FB_LOCATION, fb_loc);
153}
154
155static void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_loc)
156{
157 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000158 R500_WRITE_MCIND(RV515_MC_AGP_LOCATION, agp_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000159 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
160 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000161 RS690_WRITE_MCIND(RS690_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000162 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000163 R500_WRITE_MCIND(R520_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000164 else
165 RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc);
166}
167
Dave Airlie70b13d52008-06-19 11:40:44 +1000168static void radeon_write_agp_base(drm_radeon_private_t *dev_priv, u64 agp_base)
169{
170 u32 agp_base_hi = upper_32_bits(agp_base);
171 u32 agp_base_lo = agp_base & 0xffffffff;
172
173 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) {
174 R500_WRITE_MCIND(RV515_MC_AGP_BASE, agp_base_lo);
175 R500_WRITE_MCIND(RV515_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherf0738e92008-10-16 17:12:02 +1000176 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
177 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000178 RS690_WRITE_MCIND(RS690_MC_AGP_BASE, agp_base_lo);
179 RS690_WRITE_MCIND(RS690_MC_AGP_BASE_2, agp_base_hi);
180 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515) {
181 R500_WRITE_MCIND(R520_MC_AGP_BASE, agp_base_lo);
182 R500_WRITE_MCIND(R520_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000183 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
184 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Alex Deucher5cfb6952008-06-19 12:38:29 +1000185 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000186 RADEON_WRITE(RS480_AGP_BASE_2, agp_base_hi);
Dave Airlie70b13d52008-06-19 11:40:44 +1000187 } else {
188 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
189 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200)
190 RADEON_WRITE(RADEON_AGP_BASE_2, agp_base_hi);
191 }
192}
193
Dave Airlie84b1fd12007-07-11 15:53:27 +1000194static int RADEON_READ_PLL(struct drm_device * dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 drm_radeon_private_t *dev_priv = dev->dev_private;
197
198 RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
199 return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
200}
201
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000202static u32 RADEON_READ_PCIE(drm_radeon_private_t *dev_priv, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Dave Airlieea98a922005-09-11 20:28:11 +1000204 RADEON_WRITE8(RADEON_PCIE_INDEX, addr & 0xff);
205 return RADEON_READ(RADEON_PCIE_DATA);
206}
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000209static void radeon_status(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700211 printk("%s:\n", __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000212 printk("RBBM_STATUS = 0x%08x\n",
213 (unsigned int)RADEON_READ(RADEON_RBBM_STATUS));
214 printk("CP_RB_RTPR = 0x%08x\n",
215 (unsigned int)RADEON_READ(RADEON_CP_RB_RPTR));
216 printk("CP_RB_WTPR = 0x%08x\n",
217 (unsigned int)RADEON_READ(RADEON_CP_RB_WPTR));
218 printk("AIC_CNTL = 0x%08x\n",
219 (unsigned int)RADEON_READ(RADEON_AIC_CNTL));
220 printk("AIC_STAT = 0x%08x\n",
221 (unsigned int)RADEON_READ(RADEON_AIC_STAT));
222 printk("AIC_PT_BASE = 0x%08x\n",
223 (unsigned int)RADEON_READ(RADEON_AIC_PT_BASE));
224 printk("TLB_ADDR = 0x%08x\n",
225 (unsigned int)RADEON_READ(RADEON_AIC_TLB_ADDR));
226 printk("TLB_DATA = 0x%08x\n",
227 (unsigned int)RADEON_READ(RADEON_AIC_TLB_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229#endif
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/* ================================================================
232 * Engine, FIFO control
233 */
234
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235static int radeon_do_pixcache_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 u32 tmp;
238 int i;
239
240 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
241
Alex Deucher259434a2008-05-28 11:51:12 +1000242 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV280) {
243 tmp = RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT);
244 tmp |= RADEON_RB3D_DC_FLUSH_ALL;
245 RADEON_WRITE(RADEON_RB3D_DSTCACHE_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Alex Deucher259434a2008-05-28 11:51:12 +1000247 for (i = 0; i < dev_priv->usec_timeout; i++) {
248 if (!(RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT)
249 & RADEON_RB3D_DC_BUSY)) {
250 return 0;
251 }
252 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
Alex Deucher259434a2008-05-28 11:51:12 +1000254 } else {
Jerome Glisse54f961a2008-08-13 09:46:31 +1000255 /* don't flush or purge cache here or lockup */
256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
259#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 DRM_ERROR("failed!\n");
261 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000263 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266static int radeon_do_wait_for_fifo(drm_radeon_private_t * dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 int i;
269
270 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
271
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 for (i = 0; i < dev_priv->usec_timeout; i++) {
273 int slots = (RADEON_READ(RADEON_RBBM_STATUS)
274 & RADEON_RBBM_FIFOCNT_MASK);
275 if (slots >= entries)
276 return 0;
277 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000279 DRM_DEBUG("wait for fifo failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000280 RADEON_READ(RADEON_RBBM_STATUS),
281 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 DRM_ERROR("failed!\n");
285 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000287 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288}
289
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000290static int radeon_do_wait_for_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 int i, ret;
293
294 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
295
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000296 ret = radeon_do_wait_for_fifo(dev_priv, 64);
297 if (ret)
298 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300 for (i = 0; i < dev_priv->usec_timeout; i++) {
301 if (!(RADEON_READ(RADEON_RBBM_STATUS)
302 & RADEON_RBBM_ACTIVE)) {
303 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return 0;
305 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000306 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000308 DRM_DEBUG("wait idle failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000309 RADEON_READ(RADEON_RBBM_STATUS),
310 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 DRM_ERROR("failed!\n");
314 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000316 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Alex Deucher5b92c402008-05-28 11:57:40 +1000319static void radeon_init_pipes(drm_radeon_private_t *dev_priv)
320{
321 uint32_t gb_tile_config, gb_pipe_sel = 0;
322
323 /* RS4xx/RS6xx/R4xx/R5xx */
324 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R420) {
325 gb_pipe_sel = RADEON_READ(R400_GB_PIPE_SELECT);
326 dev_priv->num_gb_pipes = ((gb_pipe_sel >> 12) & 0x3) + 1;
327 } else {
328 /* R3xx */
329 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
330 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350)) {
331 dev_priv->num_gb_pipes = 2;
332 } else {
333 /* R3Vxx */
334 dev_priv->num_gb_pipes = 1;
335 }
336 }
337 DRM_INFO("Num pipes: %d\n", dev_priv->num_gb_pipes);
338
339 gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16 /*| R300_SUBPIXEL_1_16*/);
340
341 switch (dev_priv->num_gb_pipes) {
342 case 2: gb_tile_config |= R300_PIPE_COUNT_R300; break;
343 case 3: gb_tile_config |= R300_PIPE_COUNT_R420_3P; break;
344 case 4: gb_tile_config |= R300_PIPE_COUNT_R420; break;
345 default:
346 case 1: gb_tile_config |= R300_PIPE_COUNT_RV350; break;
347 }
348
349 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV515) {
350 RADEON_WRITE_PLL(R500_DYN_SCLK_PWMEM_PIPE, (1 | ((gb_pipe_sel >> 8) & 0xf) << 4));
351 RADEON_WRITE(R500_SU_REG_DEST, ((1 << dev_priv->num_gb_pipes) - 1));
352 }
353 RADEON_WRITE(R300_GB_TILE_CONFIG, gb_tile_config);
354 radeon_do_wait_for_idle(dev_priv);
355 RADEON_WRITE(R300_DST_PIPE_CONFIG, RADEON_READ(R300_DST_PIPE_CONFIG) | R300_PIPE_AUTO_CONFIG);
356 RADEON_WRITE(R300_RB2D_DSTCACHE_MODE, (RADEON_READ(R300_RB2D_DSTCACHE_MODE) |
357 R300_DC_AUTOFLUSH_ENABLE |
358 R300_DC_DC_DISABLE_IGNORE_PE));
359
360
361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/* ================================================================
364 * CP control, initialization
365 */
366
367/* Load the microcode for the CP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000368static void radeon_cp_load_microcode(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000375 RADEON_WRITE(RADEON_CP_ME_RAM_ADDR, 0);
Alex Deucher9f184092008-05-28 11:21:25 +1000376 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R100) ||
377 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV100) ||
378 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV200) ||
379 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS100) ||
380 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS200)) {
381 DRM_INFO("Loading R100 Microcode\n");
382 for (i = 0; i < 256; i++) {
383 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
384 R100_cp_microcode[i][1]);
385 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
386 R100_cp_microcode[i][0]);
387 }
388 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R200) ||
389 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV250) ||
390 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV280) ||
391 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS300)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 DRM_INFO("Loading R200 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393 for (i = 0; i < 256; i++) {
394 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
395 R200_cp_microcode[i][1]);
396 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
397 R200_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
Alex Deucher9f184092008-05-28 11:21:25 +1000399 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
400 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350) ||
401 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV350) ||
402 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000403 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000404 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 DRM_INFO("Loading R300 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000406 for (i = 0; i < 256; i++) {
407 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
408 R300_cp_microcode[i][1]);
409 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
410 R300_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
Alex Deucher9f184092008-05-28 11:21:25 +1000412 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
Alex Deucheredc6f382008-10-17 09:21:45 +1000413 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R423) ||
Alex Deucher9f184092008-05-28 11:21:25 +1000414 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV410)) {
415 DRM_INFO("Loading R400 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 for (i = 0; i < 256; i++) {
417 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
Alex Deucher9f184092008-05-28 11:21:25 +1000418 R420_cp_microcode[i][1]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000419 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
Alex Deucher9f184092008-05-28 11:21:25 +1000420 R420_cp_microcode[i][0]);
421 }
Alex Deucherf0738e92008-10-16 17:12:02 +1000422 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
423 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
424 DRM_INFO("Loading RS690/RS740 Microcode\n");
Alex Deucher9f184092008-05-28 11:21:25 +1000425 for (i = 0; i < 256; i++) {
426 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
427 RS690_cp_microcode[i][1]);
428 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
429 RS690_cp_microcode[i][0]);
430 }
431 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) ||
432 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R520) ||
433 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) ||
434 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R580) ||
435 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV560) ||
436 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV570)) {
437 DRM_INFO("Loading R500 Microcode\n");
438 for (i = 0; i < 256; i++) {
439 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
440 R520_cp_microcode[i][1]);
441 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
442 R520_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444 }
445}
446
447/* Flush any pending commands to the CP. This should only be used just
448 * prior to a wait for idle, as it informs the engine that the command
449 * stream is ending.
450 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451static void radeon_do_cp_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000453 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454#if 0
455 u32 tmp;
456
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457 tmp = RADEON_READ(RADEON_CP_RB_WPTR) | (1 << 31);
458 RADEON_WRITE(RADEON_CP_RB_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459#endif
460}
461
462/* Wait for the CP to go idle.
463 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000464int radeon_do_cp_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
466 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000469 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 RADEON_PURGE_CACHE();
472 RADEON_PURGE_ZCACHE();
473 RADEON_WAIT_UNTIL_IDLE();
474
475 ADVANCE_RING();
476 COMMIT_RING();
477
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000478 return radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481/* Start the Command Processor.
482 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483static void radeon_do_cp_start(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000486 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 RADEON_WRITE(RADEON_CP_CSQ_CNTL, dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 dev_priv->cp_running = 1;
493
Jerome Glisse54f961a2008-08-13 09:46:31 +1000494 BEGIN_RING(8);
495 /* isync can only be written through cp on r5xx write it here */
496 OUT_RING(CP_PACKET0(RADEON_ISYNC_CNTL, 0));
497 OUT_RING(RADEON_ISYNC_ANY2D_IDLE3D |
498 RADEON_ISYNC_ANY3D_IDLE2D |
499 RADEON_ISYNC_WAIT_IDLEGUI |
500 RADEON_ISYNC_CPSCRATCH_IDLEGUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 RADEON_PURGE_CACHE();
502 RADEON_PURGE_ZCACHE();
503 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 ADVANCE_RING();
505 COMMIT_RING();
Jerome Glisse54f961a2008-08-13 09:46:31 +1000506
507 dev_priv->track_flush |= RADEON_FLUSH_EMITED | RADEON_PURGE_EMITED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
509
510/* Reset the Command Processor. This will not flush any pending
511 * commands, so you must wait for the CP command stream to complete
512 * before calling this routine.
513 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514static void radeon_do_cp_reset(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 u32 cur_read_ptr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
520 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
521 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 dev_priv->ring.tail = cur_read_ptr;
523}
524
525/* Stop the Command Processor. This will not flush any pending
526 * commands, so you must flush the command stream and wait for the CP
527 * to go idle before calling this routine.
528 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000529static void radeon_do_cp_stop(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000531 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 RADEON_WRITE(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 dev_priv->cp_running = 0;
536}
537
538/* Reset the engine. This will stop the CP if it is running.
539 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000540static int radeon_do_engine_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 drm_radeon_private_t *dev_priv = dev->dev_private;
Alex Deucherd396db32008-05-28 11:54:06 +1000543 u32 clock_cntl_index = 0, mclk_cntl = 0, rbbm_soft_reset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000544 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Alex Deucherd396db32008-05-28 11:54:06 +1000548 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
549 /* may need something similar for newer chips */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000550 clock_cntl_index = RADEON_READ(RADEON_CLOCK_CNTL_INDEX);
551 mclk_cntl = RADEON_READ_PLL(dev, RADEON_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000553 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, (mclk_cntl |
554 RADEON_FORCEON_MCLKA |
555 RADEON_FORCEON_MCLKB |
556 RADEON_FORCEON_YCLKA |
557 RADEON_FORCEON_YCLKB |
558 RADEON_FORCEON_MC |
559 RADEON_FORCEON_AIC));
Alex Deucherd396db32008-05-28 11:54:06 +1000560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Alex Deucherd396db32008-05-28 11:54:06 +1000562 rbbm_soft_reset = RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Alex Deucherd396db32008-05-28 11:54:06 +1000564 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset |
565 RADEON_SOFT_RESET_CP |
566 RADEON_SOFT_RESET_HI |
567 RADEON_SOFT_RESET_SE |
568 RADEON_SOFT_RESET_RE |
569 RADEON_SOFT_RESET_PP |
570 RADEON_SOFT_RESET_E2 |
571 RADEON_SOFT_RESET_RB));
572 RADEON_READ(RADEON_RBBM_SOFT_RESET);
573 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset &
574 ~(RADEON_SOFT_RESET_CP |
575 RADEON_SOFT_RESET_HI |
576 RADEON_SOFT_RESET_SE |
577 RADEON_SOFT_RESET_RE |
578 RADEON_SOFT_RESET_PP |
579 RADEON_SOFT_RESET_E2 |
580 RADEON_SOFT_RESET_RB)));
581 RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Alex Deucherd396db32008-05-28 11:54:06 +1000583 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000584 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, mclk_cntl);
585 RADEON_WRITE(RADEON_CLOCK_CNTL_INDEX, clock_cntl_index);
586 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, rbbm_soft_reset);
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Alex Deucher5b92c402008-05-28 11:57:40 +1000589 /* setup the raster pipes */
590 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R300)
591 radeon_init_pipes(dev_priv);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* Reset the CP ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 /* The CP is no longer running after an engine reset */
597 dev_priv->cp_running = 0;
598
599 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 radeon_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602 return 0;
603}
604
Dave Airlie84b1fd12007-07-11 15:53:27 +1000605static void radeon_cp_init_ring_buffer(struct drm_device * dev,
etienne3d161182009-02-20 09:44:45 +1000606 drm_radeon_private_t *dev_priv,
607 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
etienne3d161182009-02-20 09:44:45 +1000609 struct drm_radeon_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 u32 ring_start, cur_read_ptr;
611 u32 tmp;
Dave Airliebc5f4522007-11-05 12:50:58 +1000612
Dave Airlied5ea7022006-03-19 19:37:55 +1100613 /* Initialize the memory controller. With new memory map, the fb location
614 * is not changed, it should have been properly initialized already. Part
615 * of the problem is that the code below is bogus, assuming the GART is
616 * always appended to the fb which is not necessarily the case
617 */
618 if (!dev_priv->new_memmap)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000619 radeon_write_fb_location(dev_priv,
Dave Airlied5ea7022006-03-19 19:37:55 +1100620 ((dev_priv->gart_vm_start - 1) & 0xffff0000)
621 | (dev_priv->fb_location >> 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000624 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000625 radeon_write_agp_base(dev_priv, dev->agp->base);
626
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000627 radeon_write_agp_location(dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000628 (((dev_priv->gart_vm_start - 1 +
629 dev_priv->gart_size) & 0xffff0000) |
630 (dev_priv->gart_vm_start >> 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
632 ring_start = (dev_priv->cp_ring->offset
633 - dev->agp->base
634 + dev_priv->gart_vm_start);
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100635 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636#endif
637 ring_start = (dev_priv->cp_ring->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100638 - (unsigned long)dev->sg->virtual
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 + dev_priv->gart_vm_start);
640
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641 RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 /* Set the write pointer delay */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000644 RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /* Initialize the ring buffer's read and write pointers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000647 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
648 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
649 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 dev_priv->ring.tail = cur_read_ptr;
651
652#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000653 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
655 dev_priv->ring_rptr->offset
656 - dev->agp->base + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } else
658#endif
659 {
David Millere8a89432009-02-12 02:15:44 -0800660 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
661 dev_priv->ring_rptr->offset
662 - ((unsigned long) dev->sg->virtual)
663 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
Dave Airlied5ea7022006-03-19 19:37:55 +1100666 /* Set ring buffer size */
667#ifdef __BIG_ENDIAN
668 RADEON_WRITE(RADEON_CP_RB_CNTL,
Roland Scheidegger576cc452008-02-07 14:59:24 +1000669 RADEON_BUF_SWAP_32BIT |
670 (dev_priv->ring.fetch_size_l2ow << 18) |
671 (dev_priv->ring.rptr_update_l2qw << 8) |
672 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100673#else
Roland Scheidegger576cc452008-02-07 14:59:24 +1000674 RADEON_WRITE(RADEON_CP_RB_CNTL,
675 (dev_priv->ring.fetch_size_l2ow << 18) |
676 (dev_priv->ring.rptr_update_l2qw << 8) |
677 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100678#endif
679
Dave Airlied5ea7022006-03-19 19:37:55 +1100680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* Initialize the scratch register pointer. This will cause
682 * the scratch register values to be written out to memory
683 * whenever they are updated.
684 *
685 * We simply put this behind the ring read pointer, this works
686 * with PCI GART as well as (whatever kind of) AGP GART
687 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000688 RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
689 + RADEON_SCRATCH_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000691 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Dave Airlied5ea7022006-03-19 19:37:55 +1100693 /* Turn on bus mastering */
Alex Deucher4e270e92008-10-28 07:48:34 +1000694 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
Alex Deucheredc6f382008-10-17 09:21:45 +1000695 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
Alex Deucher4e270e92008-10-28 07:48:34 +1000696 /* rs600/rs690/rs740 */
697 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RS600_BUS_MASTER_DIS;
Alex Deucheredc6f382008-10-17 09:21:45 +1000698 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
Alex Deucher4e270e92008-10-28 07:48:34 +1000699 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV350) ||
700 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
701 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
702 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
703 /* r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
Alex Deucheredc6f382008-10-17 09:21:45 +1000704 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
705 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
706 } /* PCIE cards appears to not need this */
Dave Airlied5ea7022006-03-19 19:37:55 +1100707
David Millerb07fa022009-02-12 02:15:37 -0800708 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(0), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000709 RADEON_WRITE(RADEON_LAST_FRAME_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100710
David Millerb07fa022009-02-12 02:15:37 -0800711 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000712 RADEON_WRITE(RADEON_LAST_DISPATCH_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100713
David Millerb07fa022009-02-12 02:15:37 -0800714 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(2), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000715 RADEON_WRITE(RADEON_LAST_CLEAR_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100716
etienne3d161182009-02-20 09:44:45 +1000717 /* reset sarea copies of these */
718 master_priv = file_priv->master->driver_priv;
719 if (master_priv->sarea_priv) {
720 master_priv->sarea_priv->last_frame = 0;
721 master_priv->sarea_priv->last_dispatch = 0;
722 master_priv->sarea_priv->last_clear = 0;
723 }
724
Dave Airlied5ea7022006-03-19 19:37:55 +1100725 radeon_do_wait_for_idle(dev_priv);
726
727 /* Sync everything up */
728 RADEON_WRITE(RADEON_ISYNC_CNTL,
729 (RADEON_ISYNC_ANY2D_IDLE3D |
730 RADEON_ISYNC_ANY3D_IDLE2D |
731 RADEON_ISYNC_WAIT_IDLEGUI |
732 RADEON_ISYNC_CPSCRATCH_IDLEGUI));
733
734}
735
736static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
737{
738 u32 tmp;
739
Dave Airlie6b79d522008-09-02 10:10:16 +1000740 /* Start with assuming that writeback doesn't work */
741 dev_priv->writeback_works = 0;
742
Dave Airlied5ea7022006-03-19 19:37:55 +1100743 /* Writeback doesn't seem to work everywhere, test it here and possibly
744 * enable it if it appears to work
745 */
David Millerb07fa022009-02-12 02:15:37 -0800746 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1), 0);
747
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
David Millerb07fa022009-02-12 02:15:37 -0800751 u32 val;
752
753 val = radeon_read_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1));
754 if (val == 0xdeadbeef)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000756 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 if (tmp < dev_priv->usec_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 dev_priv->writeback_works = 1;
Dave Airlied5ea7022006-03-19 19:37:55 +1100761 DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 } else {
763 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100764 DRM_INFO("writeback test failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
Dave Airlie689b9d72005-09-30 17:09:07 +1000766 if (radeon_no_wb == 1) {
767 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100768 DRM_INFO("writeback forced off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 }
Michel Dänzerae1b1a482006-08-07 20:37:46 +1000770
771 if (!dev_priv->writeback_works) {
772 /* Disable writeback to avoid unnecessary bus master transfer */
773 RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
774 RADEON_RB_NO_UPDATE);
775 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Dave Airlief2b04cd2007-05-08 15:19:23 +1000779/* Enable or disable IGP GART on the chip */
780static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
781{
Maciej Cencora60f92682008-02-19 21:32:45 +1000782 u32 temp;
783
784 if (on) {
Alex Deucher45e51902008-05-28 13:28:59 +1000785 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
Maciej Cencora60f92682008-02-19 21:32:45 +1000786 dev_priv->gart_vm_start,
787 (long)dev_priv->gart_info.bus_addr,
788 dev_priv->gart_size);
789
Alex Deucher45e51902008-05-28 13:28:59 +1000790 temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
Alex Deucherf0738e92008-10-16 17:12:02 +1000791 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
792 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +1000793 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
794 RS690_BLOCK_GFX_D3_EN));
795 else
796 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
Maciej Cencora60f92682008-02-19 21:32:45 +1000797
Alex Deucher45e51902008-05-28 13:28:59 +1000798 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
799 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000800
Alex Deucher45e51902008-05-28 13:28:59 +1000801 temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
802 IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
803 RS480_TLB_ENABLE |
804 RS480_GTW_LAC_EN |
805 RS480_1LEVEL_GART));
Maciej Cencora60f92682008-02-19 21:32:45 +1000806
Dave Airliefa0d71b2008-05-28 11:27:01 +1000807 temp = dev_priv->gart_info.bus_addr & 0xfffff000;
808 temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
Alex Deucher45e51902008-05-28 13:28:59 +1000809 IGP_WRITE_MCIND(RS480_GART_BASE, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000810
Alex Deucher45e51902008-05-28 13:28:59 +1000811 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
812 IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
813 RS480_REQ_TYPE_SNOOP_DIS));
Maciej Cencora60f92682008-02-19 21:32:45 +1000814
Alex Deucher5cfb6952008-06-19 12:38:29 +1000815 radeon_write_agp_base(dev_priv, dev_priv->gart_vm_start);
Dave Airlie3722bfc2008-05-28 11:28:27 +1000816
Maciej Cencora60f92682008-02-19 21:32:45 +1000817 dev_priv->gart_size = 32*1024*1024;
818 temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
819 0xffff0000) | (dev_priv->gart_vm_start >> 16));
820
Alex Deucher45e51902008-05-28 13:28:59 +1000821 radeon_write_agp_location(dev_priv, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000822
Alex Deucher45e51902008-05-28 13:28:59 +1000823 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
824 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
825 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000826
827 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000828 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
829 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000830 break;
831 DRM_UDELAY(1);
832 } while (1);
833
Alex Deucher45e51902008-05-28 13:28:59 +1000834 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
835 RS480_GART_CACHE_INVALIDATE);
Alex Deucher27359772008-05-28 12:54:16 +1000836
Maciej Cencora60f92682008-02-19 21:32:45 +1000837 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000838 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
839 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000840 break;
841 DRM_UDELAY(1);
842 } while (1);
843
Alex Deucher45e51902008-05-28 13:28:59 +1000844 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000845 } else {
Alex Deucher45e51902008-05-28 13:28:59 +1000846 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000847 }
848}
849
Dave Airlieea98a922005-09-11 20:28:11 +1000850static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Dave Airlieea98a922005-09-11 20:28:11 +1000852 u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
853 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Dave Airlieea98a922005-09-11 20:28:11 +1000855 DRM_DEBUG("programming pcie %08X %08lX %08X\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000856 dev_priv->gart_vm_start,
857 (long)dev_priv->gart_info.bus_addr,
Dave Airlieea98a922005-09-11 20:28:11 +1000858 dev_priv->gart_size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
860 dev_priv->gart_vm_start);
861 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
862 dev_priv->gart_info.bus_addr);
863 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
864 dev_priv->gart_vm_start);
865 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
866 dev_priv->gart_vm_start +
867 dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000869 radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
872 RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
875 tmp & ~RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/* Enable or disable PCI GART on the chip */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Dave Airlied985c102006-01-02 21:32:48 +1100882 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Alex Deucher45e51902008-05-28 13:28:59 +1000884 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
Alex Deucherf0738e92008-10-16 17:12:02 +1000885 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000886 (dev_priv->flags & RADEON_IS_IGPGART)) {
Dave Airlief2b04cd2007-05-08 15:19:23 +1000887 radeon_set_igpgart(dev_priv, on);
888 return;
889 }
890
Dave Airlie54a56ac2006-09-22 04:25:09 +1000891 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieea98a922005-09-11 20:28:11 +1000892 radeon_set_pciegart(dev_priv, on);
893 return;
894 }
895
Dave Airliebc5f4522007-11-05 12:50:58 +1000896 tmp = RADEON_READ(RADEON_AIC_CNTL);
Dave Airlied985c102006-01-02 21:32:48 +1100897
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 if (on) {
899 RADEON_WRITE(RADEON_AIC_CNTL,
900 tmp | RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
902 /* set PCI GART page-table base address
903 */
Dave Airlieea98a922005-09-11 20:28:11 +1000904 RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 /* set address range for PCI address translate
907 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000908 RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
909 RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
910 + dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
912 /* Turn off AGP aperture -- is this required for PCI GART?
913 */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000914 radeon_write_agp_location(dev_priv, 0xffffffc0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 RADEON_WRITE(RADEON_AIC_CNTL,
918 tmp & ~RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920}
921
David Miller6abf6bb2009-02-14 01:51:07 -0800922static int radeon_setup_pcigart_surface(drm_radeon_private_t *dev_priv)
923{
924 struct drm_ati_pcigart_info *gart_info = &dev_priv->gart_info;
925 struct radeon_virt_surface *vp;
926 int i;
927
928 for (i = 0; i < RADEON_MAX_SURFACES * 2; i++) {
929 if (!dev_priv->virt_surfaces[i].file_priv ||
930 dev_priv->virt_surfaces[i].file_priv == PCIGART_FILE_PRIV)
931 break;
932 }
933 if (i >= 2 * RADEON_MAX_SURFACES)
934 return -ENOMEM;
935 vp = &dev_priv->virt_surfaces[i];
936
937 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
938 struct radeon_surface *sp = &dev_priv->surfaces[i];
939 if (sp->refcount)
940 continue;
941
942 vp->surface_index = i;
943 vp->lower = gart_info->bus_addr;
944 vp->upper = vp->lower + gart_info->table_size;
945 vp->flags = 0;
946 vp->file_priv = PCIGART_FILE_PRIV;
947
948 sp->refcount = 1;
949 sp->lower = vp->lower;
950 sp->upper = vp->upper;
951 sp->flags = 0;
952
953 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, sp->flags);
954 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * i, sp->lower);
955 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * i, sp->upper);
956 return 0;
957 }
958
959 return -ENOMEM;
960}
961
Dave Airlie7c1c2872008-11-28 14:22:24 +1000962static int radeon_do_init_cp(struct drm_device *dev, drm_radeon_init_t *init,
963 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Dave Airlied985c102006-01-02 21:32:48 +1100965 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000966 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
Dave Airlied985c102006-01-02 21:32:48 +1100967
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Dave Airlief3dd5c32006-03-25 18:09:46 +1100970 /* if we require new memory map but we don't have it fail */
Dave Airlie54a56ac2006-09-22 04:25:09 +1000971 if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
Dave Airlieb15ec362006-08-19 17:43:52 +1000972 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 +1100973 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000974 return -EINVAL;
Dave Airlief3dd5c32006-03-25 18:09:46 +1100975 }
976
Dave Airlie54a56ac2006-09-22 04:25:09 +1000977 if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
Dave Airlied985c102006-01-02 21:32:48 +1100978 DRM_DEBUG("Forcing AGP card to PCI mode\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000979 dev_priv->flags &= ~RADEON_IS_AGP;
980 } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
Dave Airlieb15ec362006-08-19 17:43:52 +1000981 && !init->is_pci) {
982 DRM_DEBUG("Restoring AGP flag\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000983 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlied985c102006-01-02 21:32:48 +1100984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Dave Airlie54a56ac2006-09-22 04:25:09 +1000986 if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000989 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991
992 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000993 if (dev_priv->usec_timeout < 1 ||
994 dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
995 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000997 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
Dave Airlieddbee332007-07-11 12:16:01 +10001000 /* Enable vblank on CRTC1 for older X servers
1001 */
1002 dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
1003
Dave Airlied985c102006-01-02 21:32:48 +11001004 switch(init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 case RADEON_INIT_R200_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001006 dev_priv->microcode_version = UCODE_R200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 break;
1008 case RADEON_INIT_R300_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001009 dev_priv->microcode_version = UCODE_R300;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 break;
1011 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012 dev_priv->microcode_version = UCODE_R100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 dev_priv->do_boxes = 0;
1016 dev_priv->cp_mode = init->cp_mode;
1017
1018 /* We don't support anything other than bus-mastering ring mode,
1019 * but the ring can be in either AGP or PCI space for the ring
1020 * read pointer.
1021 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001022 if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
1023 (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
1024 DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001026 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001029 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 case 16:
1031 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
1032 break;
1033 case 32:
1034 default:
1035 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
1036 break;
1037 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 dev_priv->front_offset = init->front_offset;
1039 dev_priv->front_pitch = init->front_pitch;
1040 dev_priv->back_offset = init->back_offset;
1041 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 case 16:
1045 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
1046 break;
1047 case 32:
1048 default:
1049 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
1050 break;
1051 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001052 dev_priv->depth_offset = init->depth_offset;
1053 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 /* Hardware state for depth clears. Remove this if/when we no
1056 * longer clear the depth buffer with a 3D rectangle. Hard-code
1057 * all values to prevent unwanted 3D state from slipping through
1058 * and screwing with the clear operation.
1059 */
1060 dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
1061 (dev_priv->color_fmt << 10) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 (dev_priv->microcode_version ==
1063 UCODE_R100 ? RADEON_ZBLOCK16 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001065 dev_priv->depth_clear.rb3d_zstencilcntl =
1066 (dev_priv->depth_fmt |
1067 RADEON_Z_TEST_ALWAYS |
1068 RADEON_STENCIL_TEST_ALWAYS |
1069 RADEON_STENCIL_S_FAIL_REPLACE |
1070 RADEON_STENCIL_ZPASS_REPLACE |
1071 RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073 dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
1074 RADEON_BFACE_SOLID |
1075 RADEON_FFACE_SOLID |
1076 RADEON_FLAT_SHADE_VTX_LAST |
1077 RADEON_DIFFUSE_SHADE_FLAT |
1078 RADEON_ALPHA_SHADE_FLAT |
1079 RADEON_SPECULAR_SHADE_FLAT |
1080 RADEON_FOG_SHADE_FLAT |
1081 RADEON_VTX_PIX_CENTER_OGL |
1082 RADEON_ROUND_MODE_TRUNC |
1083 RADEON_ROUND_PREC_8TH_PIX);
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 dev_priv->ring_offset = init->ring_offset;
1087 dev_priv->ring_rptr_offset = init->ring_rptr_offset;
1088 dev_priv->buffers_offset = init->buffers_offset;
1089 dev_priv->gart_textures_offset = init->gart_textures_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090
Dave Airlie7c1c2872008-11-28 14:22:24 +10001091 master_priv->sarea = drm_getsarea(dev);
1092 if (!master_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 DRM_ERROR("could not find sarea!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001095 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 dev_priv->cp_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001099 if (!dev_priv->cp_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 DRM_ERROR("could not find cp ring region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001102 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001105 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 DRM_ERROR("could not find ring read pointer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001108 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Dave Airlied1f2b552005-08-05 22:11:22 +10001110 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001112 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 DRM_ERROR("could not find dma buffer region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001115 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 if (init->gart_textures_offset) {
1119 dev_priv->gart_textures =
1120 drm_core_findmap(dev, init->gart_textures_offset);
1121 if (!dev_priv->gart_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 DRM_ERROR("could not find GART texture region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001124 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 }
1126 }
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001129 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie9b8d5a12009-02-07 11:15:41 +10001130 drm_core_ioremap_wc(dev_priv->cp_ring, dev);
1131 drm_core_ioremap_wc(dev_priv->ring_rptr, dev);
1132 drm_core_ioremap_wc(dev->agp_buffer_map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001133 if (!dev_priv->cp_ring->handle ||
1134 !dev_priv->ring_rptr->handle ||
1135 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 DRM_ERROR("could not find ioremap agp regions!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001138 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 } else
1141#endif
1142 {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001143 dev_priv->cp_ring->handle =
1144 (void *)(unsigned long)dev_priv->cp_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 dev_priv->ring_rptr->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001146 (void *)(unsigned long)dev_priv->ring_rptr->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001147 dev->agp_buffer_map->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001148 (void *)(unsigned long)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150 DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
1151 dev_priv->cp_ring->handle);
1152 DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
1153 dev_priv->ring_rptr->handle);
1154 DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
1155 dev->agp_buffer_map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
1157
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001158 dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
Dave Airliebc5f4522007-11-05 12:50:58 +10001159 dev_priv->fb_size =
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001160 ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
Dave Airlied5ea7022006-03-19 19:37:55 +11001161 - dev_priv->fb_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
1164 ((dev_priv->front_offset
1165 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001167 dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
1168 ((dev_priv->back_offset
1169 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171 dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
1172 ((dev_priv->depth_offset
1173 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 dev_priv->gart_size = init->gart_size;
Dave Airlied5ea7022006-03-19 19:37:55 +11001176
1177 /* New let's set the memory map ... */
1178 if (dev_priv->new_memmap) {
1179 u32 base = 0;
1180
1181 DRM_INFO("Setting GART location based on new memory map\n");
1182
1183 /* If using AGP, try to locate the AGP aperture at the same
1184 * location in the card and on the bus, though we have to
1185 * align it down.
1186 */
1187#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001188 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001189 base = dev->agp->base;
1190 /* Check if valid */
Michel Dänzer80b2c382007-02-18 18:03:21 +11001191 if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
1192 base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001193 DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
1194 dev->agp->base);
1195 base = 0;
1196 }
1197 }
1198#endif
1199 /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
1200 if (base == 0) {
1201 base = dev_priv->fb_location + dev_priv->fb_size;
Michel Dänzer80b2c382007-02-18 18:03:21 +11001202 if (base < dev_priv->fb_location ||
1203 ((base + dev_priv->gart_size) & 0xfffffffful) < base)
Dave Airlied5ea7022006-03-19 19:37:55 +11001204 base = dev_priv->fb_location
1205 - dev_priv->gart_size;
Dave Airliebc5f4522007-11-05 12:50:58 +10001206 }
Dave Airlied5ea7022006-03-19 19:37:55 +11001207 dev_priv->gart_vm_start = base & 0xffc00000u;
1208 if (dev_priv->gart_vm_start != base)
1209 DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
1210 base, dev_priv->gart_vm_start);
1211 } else {
1212 DRM_INFO("Setting GART location based on old memory map\n");
1213 dev_priv->gart_vm_start = dev_priv->fb_location +
1214 RADEON_READ(RADEON_CONFIG_APER_SIZE);
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001218 if (dev_priv->flags & RADEON_IS_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001220 - dev->agp->base
1221 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 else
1223#endif
1224 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +01001225 - (unsigned long)dev->sg->virtual
1226 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001228 DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
1229 DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
1230 DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
1231 dev_priv->gart_buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001233 dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
1234 dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 + init->ring_size / sizeof(u32));
1236 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001237 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Roland Scheidegger576cc452008-02-07 14:59:24 +10001239 dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
1240 dev_priv->ring.rptr_update_l2qw = drm_order( /* init->rptr_update */ 4096 / 8);
1241
1242 dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
1243 dev_priv->ring.fetch_size_l2ow = drm_order( /* init->fetch_size */ 32 / 16);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001244 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
1246 dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1247
1248#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001249 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001251 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 } else
1253#endif
1254 {
David Miller6abf6bb2009-02-14 01:51:07 -08001255 u32 sctrl;
1256 int ret;
1257
Dave Airlieb05c2382008-03-17 10:24:24 +10001258 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +10001259 /* if we have an offset set from userspace */
Dave Airlief2b04cd2007-05-08 15:19:23 +10001260 if (dev_priv->pcigart_offset_set) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001261 dev_priv->gart_info.bus_addr =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001262 (resource_size_t)dev_priv->pcigart_offset + dev_priv->fb_location;
Dave Airlief26c4732006-01-02 17:18:39 +11001263 dev_priv->gart_info.mapping.offset =
Dave Airlie7fc86862007-11-05 10:45:27 +10001264 dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
Dave Airlief26c4732006-01-02 17:18:39 +11001265 dev_priv->gart_info.mapping.size =
Dave Airlief2b04cd2007-05-08 15:19:23 +10001266 dev_priv->gart_info.table_size;
Dave Airlief26c4732006-01-02 17:18:39 +11001267
Dave Airlie242e3df2008-07-15 15:48:05 +10001268 drm_core_ioremap_wc(&dev_priv->gart_info.mapping, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001269 dev_priv->gart_info.addr =
Dave Airlief26c4732006-01-02 17:18:39 +11001270 dev_priv->gart_info.mapping.handle;
Dave Airlieea98a922005-09-11 20:28:11 +10001271
Dave Airlief2b04cd2007-05-08 15:19:23 +10001272 if (dev_priv->flags & RADEON_IS_PCIE)
1273 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
1274 else
1275 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001276 dev_priv->gart_info.gart_table_location =
1277 DRM_ATI_GART_FB;
1278
Dave Airlief26c4732006-01-02 17:18:39 +11001279 DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001280 dev_priv->gart_info.addr,
1281 dev_priv->pcigart_offset);
1282 } else {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001283 if (dev_priv->flags & RADEON_IS_IGPGART)
1284 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
1285 else
1286 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001287 dev_priv->gart_info.gart_table_location =
1288 DRM_ATI_GART_MAIN;
Dave Airlief26c4732006-01-02 17:18:39 +11001289 dev_priv->gart_info.addr = NULL;
1290 dev_priv->gart_info.bus_addr = 0;
Dave Airlie54a56ac2006-09-22 04:25:09 +10001291 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001292 DRM_ERROR
1293 ("Cannot use PCI Express without GART in FB memory\n");
Dave Airlieea98a922005-09-11 20:28:11 +10001294 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001295 return -EINVAL;
Dave Airlieea98a922005-09-11 20:28:11 +10001296 }
1297 }
1298
David Miller6abf6bb2009-02-14 01:51:07 -08001299 sctrl = RADEON_READ(RADEON_SURFACE_CNTL);
1300 RADEON_WRITE(RADEON_SURFACE_CNTL, 0);
1301 ret = drm_ati_pcigart_init(dev, &dev_priv->gart_info);
1302 RADEON_WRITE(RADEON_SURFACE_CNTL, sctrl);
1303
1304 if (!ret) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001307 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309
David Miller6abf6bb2009-02-14 01:51:07 -08001310 ret = radeon_setup_pcigart_surface(dev_priv);
1311 if (ret) {
1312 DRM_ERROR("failed to setup GART surface!\n");
1313 drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info);
1314 radeon_do_cleanup_cp(dev);
1315 return ret;
1316 }
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001319 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 }
1321
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001322 radeon_cp_load_microcode(dev_priv);
etienne3d161182009-02-20 09:44:45 +10001323 radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
1325 dev_priv->last_buf = 0;
1326
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001327 radeon_do_engine_reset(dev);
Dave Airlied5ea7022006-03-19 19:37:55 +11001328 radeon_test_writeback(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
1330 return 0;
1331}
1332
Dave Airlie84b1fd12007-07-11 15:53:27 +10001333static int radeon_do_cleanup_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334{
1335 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001336 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
1338 /* Make sure interrupts are disabled here because the uninstall ioctl
1339 * may not have been called from userspace and after dev_private
1340 * is freed, it's too late.
1341 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001342 if (dev->irq_enabled)
1343 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
1345#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001346 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied985c102006-01-02 21:32:48 +11001347 if (dev_priv->cp_ring != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001348 drm_core_ioremapfree(dev_priv->cp_ring, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001349 dev_priv->cp_ring = NULL;
1350 }
1351 if (dev_priv->ring_rptr != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001352 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001353 dev_priv->ring_rptr = NULL;
1354 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001355 if (dev->agp_buffer_map != NULL) {
1356 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 dev->agp_buffer_map = NULL;
1358 }
1359 } else
1360#endif
1361 {
Dave Airlied985c102006-01-02 21:32:48 +11001362
1363 if (dev_priv->gart_info.bus_addr) {
1364 /* Turn off PCI GART */
1365 radeon_set_pcigart(dev_priv, 0);
Dave Airlieea98a922005-09-11 20:28:11 +10001366 if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
1367 DRM_ERROR("failed to cleanup PCI GART!\n");
Dave Airlied985c102006-01-02 21:32:48 +11001368 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001369
Dave Airlied985c102006-01-02 21:32:48 +11001370 if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
1371 {
Dave Airlief26c4732006-01-02 17:18:39 +11001372 drm_core_ioremapfree(&dev_priv->gart_info.mapping, dev);
Dave Airlief2b04cd2007-05-08 15:19:23 +10001373 dev_priv->gart_info.addr = 0;
Dave Airlieea98a922005-09-11 20:28:11 +10001374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 /* only clear to the start of flags */
1377 memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
1378
1379 return 0;
1380}
1381
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382/* This code will reinit the Radeon CP hardware after a resume from disc.
1383 * AFAIK, it would be very difficult to pickle the state at suspend time, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * here we make sure that all Radeon hardware initialisation is re-done without
1385 * affecting running applications.
1386 *
1387 * Charl P. Botha <http://cpbotha.net>
1388 */
etienne3d161182009-02-20 09:44:45 +10001389static int radeon_do_resume_cp(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
1391 drm_radeon_private_t *dev_priv = dev->dev_private;
1392
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001393 if (!dev_priv) {
1394 DRM_ERROR("Called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001395 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
1398 DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1399
1400#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001401 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001403 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 } else
1405#endif
1406 {
1407 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001411 radeon_cp_load_microcode(dev_priv);
etienne3d161182009-02-20 09:44:45 +10001412 radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001414 radeon_do_engine_reset(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001415 radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 DRM_DEBUG("radeon_do_resume_cp() complete\n");
1418
1419 return 0;
1420}
1421
Eric Anholtc153f452007-09-03 12:06:45 +10001422int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Eric Anholtc153f452007-09-03 12:06:45 +10001424 drm_radeon_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
Eric Anholt6c340ea2007-08-25 20:23:09 +10001426 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Eric Anholtc153f452007-09-03 12:06:45 +10001428 if (init->func == RADEON_INIT_R300_CP)
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001429 r300_init_reg_flags(dev);
Dave Airlie414ed532005-08-16 20:43:16 +10001430
Eric Anholtc153f452007-09-03 12:06:45 +10001431 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 case RADEON_INIT_CP:
1433 case RADEON_INIT_R200_CP:
1434 case RADEON_INIT_R300_CP:
Dave Airlie7c1c2872008-11-28 14:22:24 +10001435 return radeon_do_init_cp(dev, init, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 case RADEON_CLEANUP_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001437 return radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
Eric Anholt20caafa2007-08-25 19:22:43 +10001440 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441}
1442
Eric Anholtc153f452007-09-03 12:06:45 +10001443int radeon_cp_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001446 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Eric Anholt6c340ea2007-08-25 20:23:09 +10001448 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001450 if (dev_priv->cp_running) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001451 DRM_DEBUG("while CP running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 return 0;
1453 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454 if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001455 DRM_DEBUG("called with bogus CP mode (%d)\n",
1456 dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return 0;
1458 }
1459
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001460 radeon_do_cp_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 return 0;
1463}
1464
1465/* Stop the CP. The engine must have been idled before calling this
1466 * routine.
1467 */
Eric Anholtc153f452007-09-03 12:06:45 +10001468int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001471 drm_radeon_cp_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001473 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Eric Anholt6c340ea2007-08-25 20:23:09 +10001475 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 if (!dev_priv->cp_running)
1478 return 0;
1479
1480 /* Flush any pending CP commands. This ensures any outstanding
1481 * commands are exectuted by the engine before we turn it off.
1482 */
Eric Anholtc153f452007-09-03 12:06:45 +10001483 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001484 radeon_do_cp_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 }
1486
1487 /* If we fail to make the engine go idle, we return an error
1488 * code so that the DRM ioctl wrapper can try again.
1489 */
Eric Anholtc153f452007-09-03 12:06:45 +10001490 if (stop->idle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001491 ret = radeon_do_cp_idle(dev_priv);
1492 if (ret)
1493 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
1495
1496 /* Finally, we can turn off the CP. If the engine isn't idle,
1497 * we will get some dropped triangles as they won't be fully
1498 * rendered before the CP is shut down.
1499 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001500 radeon_do_cp_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 /* Reset the engine */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001503 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 return 0;
1506}
1507
Dave Airlie84b1fd12007-07-11 15:53:27 +10001508void radeon_do_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509{
1510 drm_radeon_private_t *dev_priv = dev->dev_private;
1511 int i, ret;
1512
1513 if (dev_priv) {
1514 if (dev_priv->cp_running) {
1515 /* Stop the cp */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001516 while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1518#ifdef __linux__
1519 schedule();
1520#else
1521 tsleep(&ret, PZERO, "rdnrel", 1);
1522#endif
1523 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001524 radeon_do_cp_stop(dev_priv);
1525 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527
1528 /* Disable *all* interrupts */
1529 if (dev_priv->mmio) /* remove this after permanent addmaps */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001530 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001532 if (dev_priv->mmio) { /* remove all surfaces */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001534 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
1535 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
1536 16 * i, 0);
1537 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
1538 16 * i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
1540 }
1541
1542 /* Free memory heap structures */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001543 radeon_mem_takedown(&(dev_priv->gart_heap));
1544 radeon_mem_takedown(&(dev_priv->fb_heap));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 /* deallocate kernel resources */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001547 radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549}
1550
1551/* Just reset the CP ring. Called as part of an X Server engine reset.
1552 */
Eric Anholtc153f452007-09-03 12:06:45 +10001553int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001556 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Eric Anholt6c340ea2007-08-25 20:23:09 +10001558 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001561 DRM_DEBUG("called before init done\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001562 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 /* The CP is no longer running after an engine reset */
1568 dev_priv->cp_running = 0;
1569
1570 return 0;
1571}
1572
Eric Anholtc153f452007-09-03 12:06:45 +10001573int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001576 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Eric Anholt6c340ea2007-08-25 20:23:09 +10001578 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001580 return radeon_do_cp_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
1582
1583/* Added by Charl P. Botha to call radeon_do_resume_cp().
1584 */
Eric Anholtc153f452007-09-03 12:06:45 +10001585int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586{
etienne3d161182009-02-20 09:44:45 +10001587 return radeon_do_resume_cp(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588}
1589
Eric Anholtc153f452007-09-03 12:06:45 +10001590int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001592 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Eric Anholt6c340ea2007-08-25 20:23:09 +10001594 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001596 return radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597}
1598
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599/* ================================================================
1600 * Fullscreen mode
1601 */
1602
1603/* KW: Deprecated to say the least:
1604 */
Eric Anholtc153f452007-09-03 12:06:45 +10001605int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 return 0;
1608}
1609
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610/* ================================================================
1611 * Freelist management
1612 */
1613
1614/* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1615 * bufs until freelist code is used. Note this hides a problem with
1616 * the scratch register * (used to keep track of last buffer
1617 * completed) being written to before * the last buffer has actually
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001618 * completed rendering.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 *
1620 * KW: It's also a good way to find free buffers quickly.
1621 *
1622 * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1623 * sleep. However, bugs in older versions of radeon_accel.c mean that
1624 * we essentially have to do this, else old clients will break.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001625 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 * However, it does leave open a potential deadlock where all the
1627 * buffers are held by other clients, which can't release them because
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001628 * they can't get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 */
1630
Dave Airlie056219e2007-07-11 16:17:42 +10001631struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Dave Airliecdd55a22007-07-11 16:32:08 +10001633 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 drm_radeon_private_t *dev_priv = dev->dev_private;
1635 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001636 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 int i, t;
1638 int start;
1639
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001640 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 dev_priv->last_buf = 0;
1642
1643 start = dev_priv->last_buf;
1644
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001645 for (t = 0; t < dev_priv->usec_timeout; t++) {
David Millerb07fa022009-02-12 02:15:37 -08001646 u32 done_age = GET_SCRATCH(dev_priv, 1);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001647 DRM_DEBUG("done_age = %d\n", done_age);
1648 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 buf = dma->buflist[i];
1650 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001651 if (buf->file_priv == NULL || (buf->pending &&
1652 buf_priv->age <=
1653 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 dev_priv->stats.requested_bufs++;
1655 buf->pending = 0;
1656 return buf;
1657 }
1658 start = 0;
1659 }
1660
1661 if (t) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001662 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 dev_priv->stats.freelist_loops++;
1664 }
1665 }
1666
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001667 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 return NULL;
1669}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001670
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671#if 0
Dave Airlie056219e2007-07-11 16:17:42 +10001672struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673{
Dave Airliecdd55a22007-07-11 16:32:08 +10001674 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 drm_radeon_private_t *dev_priv = dev->dev_private;
1676 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001677 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 int i, t;
1679 int start;
David Millerb07fa022009-02-12 02:15:37 -08001680 u32 done_age;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
David Millerb07fa022009-02-12 02:15:37 -08001682 done_age = radeon_read_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1));
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001683 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 dev_priv->last_buf = 0;
1685
1686 start = dev_priv->last_buf;
1687 dev_priv->stats.freelist_loops++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001688
1689 for (t = 0; t < 2; t++) {
1690 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 buf = dma->buflist[i];
1692 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001693 if (buf->file_priv == 0 || (buf->pending &&
1694 buf_priv->age <=
1695 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 dev_priv->stats.requested_bufs++;
1697 buf->pending = 0;
1698 return buf;
1699 }
1700 }
1701 start = 0;
1702 }
1703
1704 return NULL;
1705}
1706#endif
1707
Dave Airlie84b1fd12007-07-11 15:53:27 +10001708void radeon_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709{
Dave Airliecdd55a22007-07-11 16:32:08 +10001710 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 drm_radeon_private_t *dev_priv = dev->dev_private;
1712 int i;
1713
1714 dev_priv->last_buf = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001715 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001716 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1718 buf_priv->age = 0;
1719 }
1720}
1721
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722/* ================================================================
1723 * CP command submission
1724 */
1725
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001726int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
1728 drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1729 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001730 u32 last_head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001732 for (i = 0; i < dev_priv->usec_timeout; i++) {
1733 u32 head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 ring->space = (head - ring->tail) * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001736 if (ring->space <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 ring->space += ring->size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001738 if (ring->space > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
1742
1743 if (head != last_head)
1744 i = 0;
1745 last_head = head;
1746
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001747 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
1749
1750 /* FIXME: This return value is ignored in the BEGIN_RING macro! */
1751#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001752 radeon_status(dev_priv);
1753 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754#endif
Eric Anholt20caafa2007-08-25 19:22:43 +10001755 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
Eric Anholt6c340ea2007-08-25 20:23:09 +10001758static int radeon_cp_get_buffers(struct drm_device *dev,
1759 struct drm_file *file_priv,
Dave Airliec60ce622007-07-11 15:27:12 +10001760 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001763 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001765 for (i = d->granted_count; i < d->request_count; i++) {
1766 buf = radeon_freelist_get(dev);
1767 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001768 return -EBUSY; /* NOTE: broken client */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Eric Anholt6c340ea2007-08-25 20:23:09 +10001770 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001772 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
1773 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001774 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001775 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
1776 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001777 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
1779 d->granted_count++;
1780 }
1781 return 0;
1782}
1783
Eric Anholtc153f452007-09-03 12:06:45 +10001784int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785{
Dave Airliecdd55a22007-07-11 16:32:08 +10001786 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001788 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Eric Anholt6c340ea2007-08-25 20:23:09 +10001790 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 /* Please don't send us buffers.
1793 */
Eric Anholtc153f452007-09-03 12:06:45 +10001794 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001795 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001796 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001797 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 }
1799
1800 /* We'll send you buffers.
1801 */
Eric Anholtc153f452007-09-03 12:06:45 +10001802 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001803 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001804 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001805 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 }
1807
Eric Anholtc153f452007-09-03 12:06:45 +10001808 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Eric Anholtc153f452007-09-03 12:06:45 +10001810 if (d->request_count) {
1811 ret = radeon_cp_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 }
1813
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 return ret;
1815}
1816
Dave Airlie22eae942005-11-10 22:16:34 +11001817int radeon_driver_load(struct drm_device *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 drm_radeon_private_t *dev_priv;
1820 int ret = 0;
1821
1822 dev_priv = drm_alloc(sizeof(drm_radeon_private_t), DRM_MEM_DRIVER);
1823 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10001824 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 memset(dev_priv, 0, sizeof(drm_radeon_private_t));
1827 dev->dev_private = (void *)dev_priv;
1828 dev_priv->flags = flags;
1829
Dave Airlie54a56ac2006-09-22 04:25:09 +10001830 switch (flags & RADEON_FAMILY_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 case CHIP_R100:
1832 case CHIP_RV200:
1833 case CHIP_R200:
1834 case CHIP_R300:
Dave Airlieb15ec362006-08-19 17:43:52 +10001835 case CHIP_R350:
Dave Airlie414ed532005-08-16 20:43:16 +10001836 case CHIP_R420:
Alex Deucheredc6f382008-10-17 09:21:45 +10001837 case CHIP_R423:
Dave Airlieb15ec362006-08-19 17:43:52 +10001838 case CHIP_RV410:
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001839 case CHIP_RV515:
1840 case CHIP_R520:
1841 case CHIP_RV570:
1842 case CHIP_R580:
Dave Airlie54a56ac2006-09-22 04:25:09 +10001843 dev_priv->flags |= RADEON_HAS_HIERZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 break;
1845 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001846 /* all other chips have no hierarchical z buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 break;
1848 }
Dave Airlie414ed532005-08-16 20:43:16 +10001849
1850 if (drm_device_is_agp(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001851 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlieb15ec362006-08-19 17:43:52 +10001852 else if (drm_device_is_pcie(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001853 dev_priv->flags |= RADEON_IS_PCIE;
Dave Airlieb15ec362006-08-19 17:43:52 +10001854 else
Dave Airlie54a56ac2006-09-22 04:25:09 +10001855 dev_priv->flags |= RADEON_IS_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +10001856
Dave Airlie78538bf2008-11-11 17:56:16 +10001857 ret = drm_addmap(dev, drm_get_resource_start(dev, 2),
1858 drm_get_resource_len(dev, 2), _DRM_REGISTERS,
1859 _DRM_READ_ONLY | _DRM_DRIVER, &dev_priv->mmio);
1860 if (ret != 0)
1861 return ret;
1862
Keith Packard52440212008-11-18 09:30:25 -08001863 ret = drm_vblank_init(dev, 2);
1864 if (ret) {
1865 radeon_driver_unload(dev);
1866 return ret;
1867 }
1868
Dave Airlie414ed532005-08-16 20:43:16 +10001869 DRM_DEBUG("%s card detected\n",
Dave Airlie54a56ac2006-09-22 04:25:09 +10001870 ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 return ret;
1872}
1873
Dave Airlie7c1c2872008-11-28 14:22:24 +10001874int radeon_master_create(struct drm_device *dev, struct drm_master *master)
1875{
1876 struct drm_radeon_master_private *master_priv;
1877 unsigned long sareapage;
1878 int ret;
1879
1880 master_priv = drm_calloc(1, sizeof(*master_priv), DRM_MEM_DRIVER);
1881 if (!master_priv)
1882 return -ENOMEM;
1883
1884 /* prebuild the SAREA */
Dave Airliebdf539a2008-12-18 16:56:11 +10001885 sareapage = max_t(unsigned long, SAREA_MAX, PAGE_SIZE);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001886 ret = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK|_DRM_DRIVER,
1887 &master_priv->sarea);
1888 if (ret) {
1889 DRM_ERROR("SAREA setup failed\n");
1890 return ret;
1891 }
1892 master_priv->sarea_priv = master_priv->sarea->handle + sizeof(struct drm_sarea);
1893 master_priv->sarea_priv->pfCurrentPage = 0;
1894
1895 master->driver_priv = master_priv;
1896 return 0;
1897}
1898
1899void radeon_master_destroy(struct drm_device *dev, struct drm_master *master)
1900{
1901 struct drm_radeon_master_private *master_priv = master->driver_priv;
1902
1903 if (!master_priv)
1904 return;
1905
1906 if (master_priv->sarea_priv &&
1907 master_priv->sarea_priv->pfCurrentPage != 0)
1908 radeon_cp_dispatch_flip(dev, master);
1909
1910 master_priv->sarea_priv = NULL;
1911 if (master_priv->sarea)
Dave Airlie4e74f362008-12-19 10:23:14 +11001912 drm_rmmap_locked(dev, master_priv->sarea);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001913
1914 drm_free(master_priv, sizeof(*master_priv), DRM_MEM_DRIVER);
1915
1916 master->driver_priv = NULL;
1917}
1918
Dave Airlie22eae942005-11-10 22:16:34 +11001919/* Create mappings for registers and framebuffer so userland doesn't necessarily
1920 * have to find them.
1921 */
1922int radeon_driver_firstopen(struct drm_device *dev)
Dave Airlie836cf042005-07-10 19:27:04 +10001923{
1924 int ret;
1925 drm_local_map_t *map;
1926 drm_radeon_private_t *dev_priv = dev->dev_private;
1927
Dave Airlief2b04cd2007-05-08 15:19:23 +10001928 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
1929
Dave Airlie7fc86862007-11-05 10:45:27 +10001930 dev_priv->fb_aper_offset = drm_get_resource_start(dev, 0);
1931 ret = drm_addmap(dev, dev_priv->fb_aper_offset,
Dave Airlie836cf042005-07-10 19:27:04 +10001932 drm_get_resource_len(dev, 0), _DRM_FRAME_BUFFER,
1933 _DRM_WRITE_COMBINING, &map);
1934 if (ret != 0)
1935 return ret;
1936
1937 return 0;
1938}
1939
Dave Airlie22eae942005-11-10 22:16:34 +11001940int radeon_driver_unload(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
1942 drm_radeon_private_t *dev_priv = dev->dev_private;
1943
1944 DRM_DEBUG("\n");
Dave Airlie78538bf2008-11-11 17:56:16 +10001945
1946 drm_rmmap(dev, dev_priv->mmio);
1947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER);
1949
1950 dev->dev_private = NULL;
1951 return 0;
1952}
Dave Airlie4247ca92009-02-20 13:28:34 +10001953
1954void radeon_commit_ring(drm_radeon_private_t *dev_priv)
1955{
1956 int i;
1957 u32 *ring;
1958 int tail_aligned;
1959
1960 /* check if the ring is padded out to 16-dword alignment */
1961
1962 tail_aligned = dev_priv->ring.tail & 0xf;
1963 if (tail_aligned) {
1964 int num_p2 = 16 - tail_aligned;
1965
1966 ring = dev_priv->ring.start;
1967 /* pad with some CP_PACKET2 */
1968 for (i = 0; i < num_p2; i++)
1969 ring[dev_priv->ring.tail + i] = CP_PACKET2();
1970
1971 dev_priv->ring.tail += i;
1972
1973 dev_priv->ring.space -= num_p2 * sizeof(u32);
1974 }
1975
1976 dev_priv->ring.tail &= dev_priv->ring.tail_mask;
1977
1978 DRM_MEMORYBARRIER();
1979 GET_RING_HEAD( dev_priv );
1980
1981 RADEON_WRITE( RADEON_CP_RB_WPTR, dev_priv->ring.tail );
1982 /* read from PCI bus to ensure correct posting */
1983 RADEON_READ( RADEON_CP_RB_RPTR );
1984}