blob: 248ab4a7d39fdb42a1073f48cceca3ed237d28ce [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"
34#include "radeon_drm.h"
35#include "radeon_drv.h"
Dave Airlie414ed532005-08-16 20:43:16 +100036#include "r300_reg.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Alex Deucher9f184092008-05-28 11:21:25 +100038#include "radeon_microcode.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define RADEON_FIFO_DEBUG 0
41
Dave Airlie84b1fd12007-07-11 15:53:27 +100042static int radeon_do_cleanup_cp(struct drm_device * dev);
Jerome Glisse54f961a2008-08-13 09:46:31 +100043static void radeon_do_cp_start(drm_radeon_private_t * dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Alex Deucher45e51902008-05-28 13:28:59 +100045static u32 R500_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
Dave Airlie3d5e2c12008-02-07 15:01:05 +100046{
47 u32 ret;
48 RADEON_WRITE(R520_MC_IND_INDEX, 0x7f0000 | (addr & 0xff));
49 ret = RADEON_READ(R520_MC_IND_DATA);
50 RADEON_WRITE(R520_MC_IND_INDEX, 0);
51 return ret;
52}
53
Alex Deucher45e51902008-05-28 13:28:59 +100054static u32 RS480_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
55{
56 u32 ret;
57 RADEON_WRITE(RS480_NB_MC_INDEX, addr & 0xff);
58 ret = RADEON_READ(RS480_NB_MC_DATA);
59 RADEON_WRITE(RS480_NB_MC_INDEX, 0xff);
60 return ret;
61}
62
Maciej Cencora60f92682008-02-19 21:32:45 +100063static u32 RS690_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
64{
Alex Deucher45e51902008-05-28 13:28:59 +100065 u32 ret;
Maciej Cencora60f92682008-02-19 21:32:45 +100066 RADEON_WRITE(RS690_MC_INDEX, (addr & RS690_MC_INDEX_MASK));
Alex Deucher45e51902008-05-28 13:28:59 +100067 ret = RADEON_READ(RS690_MC_DATA);
68 RADEON_WRITE(RS690_MC_INDEX, RS690_MC_INDEX_MASK);
69 return ret;
70}
71
72static u32 IGP_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
73{
74 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
75 return RS690_READ_MCIND(dev_priv, addr);
76 else
77 return RS480_READ_MCIND(dev_priv, addr);
Maciej Cencora60f92682008-02-19 21:32:45 +100078}
79
Dave Airlie3d5e2c12008-02-07 15:01:05 +100080u32 radeon_read_fb_location(drm_radeon_private_t *dev_priv)
81{
82
83 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100084 return R500_READ_MCIND(dev_priv, RV515_MC_FB_LOCATION);
Maciej Cencora60f92682008-02-19 21:32:45 +100085 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
86 return RS690_READ_MCIND(dev_priv, RS690_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100087 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100088 return R500_READ_MCIND(dev_priv, R520_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100089 else
90 return RADEON_READ(RADEON_MC_FB_LOCATION);
91}
92
93static void radeon_write_fb_location(drm_radeon_private_t *dev_priv, u32 fb_loc)
94{
95 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100096 R500_WRITE_MCIND(RV515_MC_FB_LOCATION, fb_loc);
Maciej Cencora60f92682008-02-19 21:32:45 +100097 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
98 RS690_WRITE_MCIND(RS690_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100099 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000100 R500_WRITE_MCIND(R520_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000101 else
102 RADEON_WRITE(RADEON_MC_FB_LOCATION, fb_loc);
103}
104
105static void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_loc)
106{
107 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000108 R500_WRITE_MCIND(RV515_MC_AGP_LOCATION, agp_loc);
Maciej Cencora60f92682008-02-19 21:32:45 +1000109 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
110 RS690_WRITE_MCIND(RS690_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000111 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000112 R500_WRITE_MCIND(R520_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000113 else
114 RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc);
115}
116
Dave Airlie70b13d52008-06-19 11:40:44 +1000117static void radeon_write_agp_base(drm_radeon_private_t *dev_priv, u64 agp_base)
118{
119 u32 agp_base_hi = upper_32_bits(agp_base);
120 u32 agp_base_lo = agp_base & 0xffffffff;
121
122 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) {
123 R500_WRITE_MCIND(RV515_MC_AGP_BASE, agp_base_lo);
124 R500_WRITE_MCIND(RV515_MC_AGP_BASE_2, agp_base_hi);
125 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) {
126 RS690_WRITE_MCIND(RS690_MC_AGP_BASE, agp_base_lo);
127 RS690_WRITE_MCIND(RS690_MC_AGP_BASE_2, agp_base_hi);
128 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515) {
129 R500_WRITE_MCIND(R520_MC_AGP_BASE, agp_base_lo);
130 R500_WRITE_MCIND(R520_MC_AGP_BASE_2, agp_base_hi);
Alex Deucher5cfb6952008-06-19 12:38:29 +1000131 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480) {
132 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
133 RADEON_WRITE(RS480_AGP_BASE_2, 0);
Dave Airlie70b13d52008-06-19 11:40:44 +1000134 } else {
135 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
136 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200)
137 RADEON_WRITE(RADEON_AGP_BASE_2, agp_base_hi);
138 }
139}
140
Dave Airlie84b1fd12007-07-11 15:53:27 +1000141static int RADEON_READ_PLL(struct drm_device * dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 drm_radeon_private_t *dev_priv = dev->dev_private;
144
145 RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
146 return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
147}
148
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000149static u32 RADEON_READ_PCIE(drm_radeon_private_t *dev_priv, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Dave Airlieea98a922005-09-11 20:28:11 +1000151 RADEON_WRITE8(RADEON_PCIE_INDEX, addr & 0xff);
152 return RADEON_READ(RADEON_PCIE_DATA);
153}
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000156static void radeon_status(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700158 printk("%s:\n", __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000159 printk("RBBM_STATUS = 0x%08x\n",
160 (unsigned int)RADEON_READ(RADEON_RBBM_STATUS));
161 printk("CP_RB_RTPR = 0x%08x\n",
162 (unsigned int)RADEON_READ(RADEON_CP_RB_RPTR));
163 printk("CP_RB_WTPR = 0x%08x\n",
164 (unsigned int)RADEON_READ(RADEON_CP_RB_WPTR));
165 printk("AIC_CNTL = 0x%08x\n",
166 (unsigned int)RADEON_READ(RADEON_AIC_CNTL));
167 printk("AIC_STAT = 0x%08x\n",
168 (unsigned int)RADEON_READ(RADEON_AIC_STAT));
169 printk("AIC_PT_BASE = 0x%08x\n",
170 (unsigned int)RADEON_READ(RADEON_AIC_PT_BASE));
171 printk("TLB_ADDR = 0x%08x\n",
172 (unsigned int)RADEON_READ(RADEON_AIC_TLB_ADDR));
173 printk("TLB_DATA = 0x%08x\n",
174 (unsigned int)RADEON_READ(RADEON_AIC_TLB_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176#endif
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178/* ================================================================
179 * Engine, FIFO control
180 */
181
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000182static int radeon_do_pixcache_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 u32 tmp;
185 int i;
186
187 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
188
Alex Deucher259434a2008-05-28 11:51:12 +1000189 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV280) {
190 tmp = RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT);
191 tmp |= RADEON_RB3D_DC_FLUSH_ALL;
192 RADEON_WRITE(RADEON_RB3D_DSTCACHE_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Alex Deucher259434a2008-05-28 11:51:12 +1000194 for (i = 0; i < dev_priv->usec_timeout; i++) {
195 if (!(RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT)
196 & RADEON_RB3D_DC_BUSY)) {
197 return 0;
198 }
199 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Alex Deucher259434a2008-05-28 11:51:12 +1000201 } else {
Jerome Glisse54f961a2008-08-13 09:46:31 +1000202 /* don't flush or purge cache here or lockup */
203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205
206#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000207 DRM_ERROR("failed!\n");
208 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000210 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213static int radeon_do_wait_for_fifo(drm_radeon_private_t * dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 int i;
216
217 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
218
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 for (i = 0; i < dev_priv->usec_timeout; i++) {
220 int slots = (RADEON_READ(RADEON_RBBM_STATUS)
221 & RADEON_RBBM_FIFOCNT_MASK);
222 if (slots >= entries)
223 return 0;
224 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000226 DRM_DEBUG("wait for fifo failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000227 RADEON_READ(RADEON_RBBM_STATUS),
228 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 DRM_ERROR("failed!\n");
232 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000234 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237static int radeon_do_wait_for_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
239 int i, ret;
240
241 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
242
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243 ret = radeon_do_wait_for_fifo(dev_priv, 64);
244 if (ret)
245 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 for (i = 0; i < dev_priv->usec_timeout; i++) {
248 if (!(RADEON_READ(RADEON_RBBM_STATUS)
249 & RADEON_RBBM_ACTIVE)) {
250 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return 0;
252 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000255 DRM_DEBUG("wait idle failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000256 RADEON_READ(RADEON_RBBM_STATUS),
257 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
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
Alex Deucher5b92c402008-05-28 11:57:40 +1000266static void radeon_init_pipes(drm_radeon_private_t *dev_priv)
267{
268 uint32_t gb_tile_config, gb_pipe_sel = 0;
269
270 /* RS4xx/RS6xx/R4xx/R5xx */
271 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R420) {
272 gb_pipe_sel = RADEON_READ(R400_GB_PIPE_SELECT);
273 dev_priv->num_gb_pipes = ((gb_pipe_sel >> 12) & 0x3) + 1;
274 } else {
275 /* R3xx */
276 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
277 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350)) {
278 dev_priv->num_gb_pipes = 2;
279 } else {
280 /* R3Vxx */
281 dev_priv->num_gb_pipes = 1;
282 }
283 }
284 DRM_INFO("Num pipes: %d\n", dev_priv->num_gb_pipes);
285
286 gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16 /*| R300_SUBPIXEL_1_16*/);
287
288 switch (dev_priv->num_gb_pipes) {
289 case 2: gb_tile_config |= R300_PIPE_COUNT_R300; break;
290 case 3: gb_tile_config |= R300_PIPE_COUNT_R420_3P; break;
291 case 4: gb_tile_config |= R300_PIPE_COUNT_R420; break;
292 default:
293 case 1: gb_tile_config |= R300_PIPE_COUNT_RV350; break;
294 }
295
296 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV515) {
297 RADEON_WRITE_PLL(R500_DYN_SCLK_PWMEM_PIPE, (1 | ((gb_pipe_sel >> 8) & 0xf) << 4));
298 RADEON_WRITE(R500_SU_REG_DEST, ((1 << dev_priv->num_gb_pipes) - 1));
299 }
300 RADEON_WRITE(R300_GB_TILE_CONFIG, gb_tile_config);
301 radeon_do_wait_for_idle(dev_priv);
302 RADEON_WRITE(R300_DST_PIPE_CONFIG, RADEON_READ(R300_DST_PIPE_CONFIG) | R300_PIPE_AUTO_CONFIG);
303 RADEON_WRITE(R300_RB2D_DSTCACHE_MODE, (RADEON_READ(R300_RB2D_DSTCACHE_MODE) |
304 R300_DC_AUTOFLUSH_ENABLE |
305 R300_DC_DC_DISABLE_IGNORE_PE));
306
307
308}
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310/* ================================================================
311 * CP control, initialization
312 */
313
314/* Load the microcode for the CP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315static void radeon_cp_load_microcode(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322 RADEON_WRITE(RADEON_CP_ME_RAM_ADDR, 0);
Alex Deucher9f184092008-05-28 11:21:25 +1000323 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R100) ||
324 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV100) ||
325 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV200) ||
326 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS100) ||
327 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS200)) {
328 DRM_INFO("Loading R100 Microcode\n");
329 for (i = 0; i < 256; i++) {
330 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
331 R100_cp_microcode[i][1]);
332 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
333 R100_cp_microcode[i][0]);
334 }
335 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R200) ||
336 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV250) ||
337 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV280) ||
338 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS300)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 DRM_INFO("Loading R200 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000340 for (i = 0; i < 256; i++) {
341 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
342 R200_cp_microcode[i][1]);
343 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
344 R200_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Alex Deucher9f184092008-05-28 11:21:25 +1000346 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
347 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350) ||
348 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV350) ||
349 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000350 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 DRM_INFO("Loading R300 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000352 for (i = 0; i < 256; i++) {
353 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
354 R300_cp_microcode[i][1]);
355 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
356 R300_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Alex Deucher9f184092008-05-28 11:21:25 +1000358 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
359 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV410)) {
360 DRM_INFO("Loading R400 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 for (i = 0; i < 256; i++) {
362 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
Alex Deucher9f184092008-05-28 11:21:25 +1000363 R420_cp_microcode[i][1]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
Alex Deucher9f184092008-05-28 11:21:25 +1000365 R420_cp_microcode[i][0]);
366 }
367 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) {
368 DRM_INFO("Loading RS690 Microcode\n");
369 for (i = 0; i < 256; i++) {
370 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
371 RS690_cp_microcode[i][1]);
372 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
373 RS690_cp_microcode[i][0]);
374 }
375 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) ||
376 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R520) ||
377 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) ||
378 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R580) ||
379 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV560) ||
380 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV570)) {
381 DRM_INFO("Loading R500 Microcode\n");
382 for (i = 0; i < 256; i++) {
383 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
384 R520_cp_microcode[i][1]);
385 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
386 R520_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 }
389}
390
391/* Flush any pending commands to the CP. This should only be used just
392 * prior to a wait for idle, as it informs the engine that the command
393 * stream is ending.
394 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395static void radeon_do_cp_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000397 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398#if 0
399 u32 tmp;
400
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000401 tmp = RADEON_READ(RADEON_CP_RB_WPTR) | (1 << 31);
402 RADEON_WRITE(RADEON_CP_RB_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403#endif
404}
405
406/* Wait for the CP to go idle.
407 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000408int radeon_do_cp_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000413 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 RADEON_PURGE_CACHE();
416 RADEON_PURGE_ZCACHE();
417 RADEON_WAIT_UNTIL_IDLE();
418
419 ADVANCE_RING();
420 COMMIT_RING();
421
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 return radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
425/* Start the Command Processor.
426 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000427static void radeon_do_cp_start(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000430 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000434 RADEON_WRITE(RADEON_CP_CSQ_CNTL, dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 dev_priv->cp_running = 1;
437
Jerome Glisse54f961a2008-08-13 09:46:31 +1000438 BEGIN_RING(8);
439 /* isync can only be written through cp on r5xx write it here */
440 OUT_RING(CP_PACKET0(RADEON_ISYNC_CNTL, 0));
441 OUT_RING(RADEON_ISYNC_ANY2D_IDLE3D |
442 RADEON_ISYNC_ANY3D_IDLE2D |
443 RADEON_ISYNC_WAIT_IDLEGUI |
444 RADEON_ISYNC_CPSCRATCH_IDLEGUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 RADEON_PURGE_CACHE();
446 RADEON_PURGE_ZCACHE();
447 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 ADVANCE_RING();
449 COMMIT_RING();
Jerome Glisse54f961a2008-08-13 09:46:31 +1000450
451 dev_priv->track_flush |= RADEON_FLUSH_EMITED | RADEON_PURGE_EMITED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452}
453
454/* Reset the Command Processor. This will not flush any pending
455 * commands, so you must wait for the CP command stream to complete
456 * before calling this routine.
457 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458static void radeon_do_cp_reset(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 u32 cur_read_ptr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000461 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
464 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
465 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 dev_priv->ring.tail = cur_read_ptr;
467}
468
469/* Stop the Command Processor. This will not flush any pending
470 * commands, so you must flush the command stream and wait for the CP
471 * to go idle before calling this routine.
472 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473static void radeon_do_cp_stop(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 RADEON_WRITE(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 dev_priv->cp_running = 0;
480}
481
482/* Reset the engine. This will stop the CP if it is running.
483 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000484static int radeon_do_engine_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 drm_radeon_private_t *dev_priv = dev->dev_private;
Alex Deucherd396db32008-05-28 11:54:06 +1000487 u32 clock_cntl_index = 0, mclk_cntl = 0, rbbm_soft_reset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Alex Deucherd396db32008-05-28 11:54:06 +1000492 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
493 /* may need something similar for newer chips */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000494 clock_cntl_index = RADEON_READ(RADEON_CLOCK_CNTL_INDEX);
495 mclk_cntl = RADEON_READ_PLL(dev, RADEON_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000497 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, (mclk_cntl |
498 RADEON_FORCEON_MCLKA |
499 RADEON_FORCEON_MCLKB |
500 RADEON_FORCEON_YCLKA |
501 RADEON_FORCEON_YCLKB |
502 RADEON_FORCEON_MC |
503 RADEON_FORCEON_AIC));
Alex Deucherd396db32008-05-28 11:54:06 +1000504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Alex Deucherd396db32008-05-28 11:54:06 +1000506 rbbm_soft_reset = RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Alex Deucherd396db32008-05-28 11:54:06 +1000508 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset |
509 RADEON_SOFT_RESET_CP |
510 RADEON_SOFT_RESET_HI |
511 RADEON_SOFT_RESET_SE |
512 RADEON_SOFT_RESET_RE |
513 RADEON_SOFT_RESET_PP |
514 RADEON_SOFT_RESET_E2 |
515 RADEON_SOFT_RESET_RB));
516 RADEON_READ(RADEON_RBBM_SOFT_RESET);
517 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset &
518 ~(RADEON_SOFT_RESET_CP |
519 RADEON_SOFT_RESET_HI |
520 RADEON_SOFT_RESET_SE |
521 RADEON_SOFT_RESET_RE |
522 RADEON_SOFT_RESET_PP |
523 RADEON_SOFT_RESET_E2 |
524 RADEON_SOFT_RESET_RB)));
525 RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526
Alex Deucherd396db32008-05-28 11:54:06 +1000527 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000528 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, mclk_cntl);
529 RADEON_WRITE(RADEON_CLOCK_CNTL_INDEX, clock_cntl_index);
530 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, rbbm_soft_reset);
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Alex Deucher5b92c402008-05-28 11:57:40 +1000533 /* setup the raster pipes */
534 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R300)
535 radeon_init_pipes(dev_priv);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /* Reset the CP ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* The CP is no longer running after an engine reset */
541 dev_priv->cp_running = 0;
542
543 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000544 radeon_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 return 0;
547}
548
Dave Airlie84b1fd12007-07-11 15:53:27 +1000549static void radeon_cp_init_ring_buffer(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 u32 ring_start, cur_read_ptr;
553 u32 tmp;
Dave Airliebc5f4522007-11-05 12:50:58 +1000554
Dave Airlied5ea7022006-03-19 19:37:55 +1100555 /* Initialize the memory controller. With new memory map, the fb location
556 * is not changed, it should have been properly initialized already. Part
557 * of the problem is that the code below is bogus, assuming the GART is
558 * always appended to the fb which is not necessarily the case
559 */
560 if (!dev_priv->new_memmap)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000561 radeon_write_fb_location(dev_priv,
Dave Airlied5ea7022006-03-19 19:37:55 +1100562 ((dev_priv->gart_vm_start - 1) & 0xffff0000)
563 | (dev_priv->fb_location >> 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000566 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000567 radeon_write_agp_base(dev_priv, dev->agp->base);
568
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000569 radeon_write_agp_location(dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000570 (((dev_priv->gart_vm_start - 1 +
571 dev_priv->gart_size) & 0xffff0000) |
572 (dev_priv->gart_vm_start >> 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 ring_start = (dev_priv->cp_ring->offset
575 - dev->agp->base
576 + dev_priv->gart_vm_start);
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100577 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578#endif
579 ring_start = (dev_priv->cp_ring->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100580 - (unsigned long)dev->sg->virtual
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 + dev_priv->gart_vm_start);
582
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000583 RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
585 /* Set the write pointer delay */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 /* Initialize the ring buffer's read and write pointers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
590 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
591 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 dev_priv->ring.tail = cur_read_ptr;
593
594#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000595 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
597 dev_priv->ring_rptr->offset
598 - dev->agp->base + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else
600#endif
601 {
Dave Airlie55910512007-07-11 16:53:40 +1000602 struct drm_sg_mem *entry = dev->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 unsigned long tmp_ofs, page_ofs;
604
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100605 tmp_ofs = dev_priv->ring_rptr->offset -
606 (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 page_ofs = tmp_ofs >> PAGE_SHIFT;
608
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000609 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR, entry->busaddr[page_ofs]);
610 DRM_DEBUG("ring rptr: offset=0x%08lx handle=0x%08lx\n",
611 (unsigned long)entry->busaddr[page_ofs],
612 entry->handle + tmp_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
614
Dave Airlied5ea7022006-03-19 19:37:55 +1100615 /* Set ring buffer size */
616#ifdef __BIG_ENDIAN
617 RADEON_WRITE(RADEON_CP_RB_CNTL,
Roland Scheidegger576cc452008-02-07 14:59:24 +1000618 RADEON_BUF_SWAP_32BIT |
619 (dev_priv->ring.fetch_size_l2ow << 18) |
620 (dev_priv->ring.rptr_update_l2qw << 8) |
621 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100622#else
Roland Scheidegger576cc452008-02-07 14:59:24 +1000623 RADEON_WRITE(RADEON_CP_RB_CNTL,
624 (dev_priv->ring.fetch_size_l2ow << 18) |
625 (dev_priv->ring.rptr_update_l2qw << 8) |
626 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100627#endif
628
629 /* Start with assuming that writeback doesn't work */
630 dev_priv->writeback_works = 0;
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Initialize the scratch register pointer. This will cause
633 * the scratch register values to be written out to memory
634 * whenever they are updated.
635 *
636 * We simply put this behind the ring read pointer, this works
637 * with PCI GART as well as (whatever kind of) AGP GART
638 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000639 RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
640 + RADEON_SCRATCH_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 dev_priv->scratch = ((__volatile__ u32 *)
643 dev_priv->ring_rptr->handle +
644 (RADEON_SCRATCH_REG_OFFSET / sizeof(u32)));
645
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Dave Airlied5ea7022006-03-19 19:37:55 +1100648 /* Turn on bus mastering */
649 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
650 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
651
652 dev_priv->sarea_priv->last_frame = dev_priv->scratch[0] = 0;
653 RADEON_WRITE(RADEON_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame);
654
655 dev_priv->sarea_priv->last_dispatch = dev_priv->scratch[1] = 0;
656 RADEON_WRITE(RADEON_LAST_DISPATCH_REG,
657 dev_priv->sarea_priv->last_dispatch);
658
659 dev_priv->sarea_priv->last_clear = dev_priv->scratch[2] = 0;
660 RADEON_WRITE(RADEON_LAST_CLEAR_REG, dev_priv->sarea_priv->last_clear);
661
662 radeon_do_wait_for_idle(dev_priv);
663
664 /* Sync everything up */
665 RADEON_WRITE(RADEON_ISYNC_CNTL,
666 (RADEON_ISYNC_ANY2D_IDLE3D |
667 RADEON_ISYNC_ANY3D_IDLE2D |
668 RADEON_ISYNC_WAIT_IDLEGUI |
669 RADEON_ISYNC_CPSCRATCH_IDLEGUI));
670
671}
672
673static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
674{
675 u32 tmp;
676
677 /* Writeback doesn't seem to work everywhere, test it here and possibly
678 * enable it if it appears to work
679 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000680 DRM_WRITE32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1), 0);
681 RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000683 for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
684 if (DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1)) ==
685 0xdeadbeef)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000687 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
689
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 if (tmp < dev_priv->usec_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 dev_priv->writeback_works = 1;
Dave Airlied5ea7022006-03-19 19:37:55 +1100692 DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 } else {
694 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100695 DRM_INFO("writeback test failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Dave Airlie689b9d72005-09-30 17:09:07 +1000697 if (radeon_no_wb == 1) {
698 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100699 DRM_INFO("writeback forced off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Michel Dänzerae1b1a482006-08-07 20:37:46 +1000701
702 if (!dev_priv->writeback_works) {
703 /* Disable writeback to avoid unnecessary bus master transfer */
704 RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
705 RADEON_RB_NO_UPDATE);
706 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
Dave Airlief2b04cd2007-05-08 15:19:23 +1000710/* Enable or disable IGP GART on the chip */
711static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
712{
Maciej Cencora60f92682008-02-19 21:32:45 +1000713 u32 temp;
714
715 if (on) {
Alex Deucher45e51902008-05-28 13:28:59 +1000716 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
Maciej Cencora60f92682008-02-19 21:32:45 +1000717 dev_priv->gart_vm_start,
718 (long)dev_priv->gart_info.bus_addr,
719 dev_priv->gart_size);
720
Alex Deucher45e51902008-05-28 13:28:59 +1000721 temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
722 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
723 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
724 RS690_BLOCK_GFX_D3_EN));
725 else
726 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
Maciej Cencora60f92682008-02-19 21:32:45 +1000727
Alex Deucher45e51902008-05-28 13:28:59 +1000728 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
729 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000730
Alex Deucher45e51902008-05-28 13:28:59 +1000731 temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
732 IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
733 RS480_TLB_ENABLE |
734 RS480_GTW_LAC_EN |
735 RS480_1LEVEL_GART));
Maciej Cencora60f92682008-02-19 21:32:45 +1000736
Dave Airliefa0d71b2008-05-28 11:27:01 +1000737 temp = dev_priv->gart_info.bus_addr & 0xfffff000;
738 temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
Alex Deucher45e51902008-05-28 13:28:59 +1000739 IGP_WRITE_MCIND(RS480_GART_BASE, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000740
Alex Deucher45e51902008-05-28 13:28:59 +1000741 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
742 IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
743 RS480_REQ_TYPE_SNOOP_DIS));
Maciej Cencora60f92682008-02-19 21:32:45 +1000744
Alex Deucher5cfb6952008-06-19 12:38:29 +1000745 radeon_write_agp_base(dev_priv, dev_priv->gart_vm_start);
Dave Airlie3722bfc2008-05-28 11:28:27 +1000746
Maciej Cencora60f92682008-02-19 21:32:45 +1000747 dev_priv->gart_size = 32*1024*1024;
748 temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
749 0xffff0000) | (dev_priv->gart_vm_start >> 16));
750
Alex Deucher45e51902008-05-28 13:28:59 +1000751 radeon_write_agp_location(dev_priv, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000752
Alex Deucher45e51902008-05-28 13:28:59 +1000753 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
754 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
755 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000756
757 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000758 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
759 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000760 break;
761 DRM_UDELAY(1);
762 } while (1);
763
Alex Deucher45e51902008-05-28 13:28:59 +1000764 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
765 RS480_GART_CACHE_INVALIDATE);
Alex Deucher27359772008-05-28 12:54:16 +1000766
Maciej Cencora60f92682008-02-19 21:32:45 +1000767 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000768 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
769 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000770 break;
771 DRM_UDELAY(1);
772 } while (1);
773
Alex Deucher45e51902008-05-28 13:28:59 +1000774 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000775 } else {
Alex Deucher45e51902008-05-28 13:28:59 +1000776 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000777 }
778}
779
Dave Airlieea98a922005-09-11 20:28:11 +1000780static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Dave Airlieea98a922005-09-11 20:28:11 +1000782 u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
783 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Dave Airlieea98a922005-09-11 20:28:11 +1000785 DRM_DEBUG("programming pcie %08X %08lX %08X\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 dev_priv->gart_vm_start,
787 (long)dev_priv->gart_info.bus_addr,
Dave Airlieea98a922005-09-11 20:28:11 +1000788 dev_priv->gart_size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
790 dev_priv->gart_vm_start);
791 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
792 dev_priv->gart_info.bus_addr);
793 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
794 dev_priv->gart_vm_start);
795 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
796 dev_priv->gart_vm_start +
797 dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000799 radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
802 RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000804 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
805 tmp & ~RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 }
807}
808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809/* Enable or disable PCI GART on the chip */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Dave Airlied985c102006-01-02 21:32:48 +1100812 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Alex Deucher45e51902008-05-28 13:28:59 +1000814 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
815 (dev_priv->flags & RADEON_IS_IGPGART)) {
Dave Airlief2b04cd2007-05-08 15:19:23 +1000816 radeon_set_igpgart(dev_priv, on);
817 return;
818 }
819
Dave Airlie54a56ac2006-09-22 04:25:09 +1000820 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieea98a922005-09-11 20:28:11 +1000821 radeon_set_pciegart(dev_priv, on);
822 return;
823 }
824
Dave Airliebc5f4522007-11-05 12:50:58 +1000825 tmp = RADEON_READ(RADEON_AIC_CNTL);
Dave Airlied985c102006-01-02 21:32:48 +1100826
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000827 if (on) {
828 RADEON_WRITE(RADEON_AIC_CNTL,
829 tmp | RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 /* set PCI GART page-table base address
832 */
Dave Airlieea98a922005-09-11 20:28:11 +1000833 RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* set address range for PCI address translate
836 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000837 RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
838 RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
839 + dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Turn off AGP aperture -- is this required for PCI GART?
842 */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000843 radeon_write_agp_location(dev_priv, 0xffffffc0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000844 RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 RADEON_WRITE(RADEON_AIC_CNTL,
847 tmp & ~RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
849}
850
Dave Airlie84b1fd12007-07-11 15:53:27 +1000851static int radeon_do_init_cp(struct drm_device * dev, drm_radeon_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
Dave Airlied985c102006-01-02 21:32:48 +1100853 drm_radeon_private_t *dev_priv = dev->dev_private;
854
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Dave Airlief3dd5c32006-03-25 18:09:46 +1100857 /* if we require new memory map but we don't have it fail */
Dave Airlie54a56ac2006-09-22 04:25:09 +1000858 if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
Dave Airlieb15ec362006-08-19 17:43:52 +1000859 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 +1100860 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000861 return -EINVAL;
Dave Airlief3dd5c32006-03-25 18:09:46 +1100862 }
863
Dave Airlie54a56ac2006-09-22 04:25:09 +1000864 if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
Dave Airlied985c102006-01-02 21:32:48 +1100865 DRM_DEBUG("Forcing AGP card to PCI mode\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000866 dev_priv->flags &= ~RADEON_IS_AGP;
867 } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
Dave Airlieb15ec362006-08-19 17:43:52 +1000868 && !init->is_pci) {
869 DRM_DEBUG("Restoring AGP flag\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000870 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlied985c102006-01-02 21:32:48 +1100871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Dave Airlie54a56ac2006-09-22 04:25:09 +1000873 if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000876 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878
879 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880 if (dev_priv->usec_timeout < 1 ||
881 dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
882 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000884 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 }
886
Dave Airlieddbee332007-07-11 12:16:01 +1000887 /* Enable vblank on CRTC1 for older X servers
888 */
889 dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
890
Dave Airlied985c102006-01-02 21:32:48 +1100891 switch(init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 case RADEON_INIT_R200_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 dev_priv->microcode_version = UCODE_R200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 break;
895 case RADEON_INIT_R300_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 dev_priv->microcode_version = UCODE_R300;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 dev_priv->microcode_version = UCODE_R100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 dev_priv->do_boxes = 0;
903 dev_priv->cp_mode = init->cp_mode;
904
905 /* We don't support anything other than bus-mastering ring mode,
906 * but the ring can be in either AGP or PCI space for the ring
907 * read pointer.
908 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
910 (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
911 DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000913 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 }
915
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000916 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 case 16:
918 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
919 break;
920 case 32:
921 default:
922 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
923 break;
924 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 dev_priv->front_offset = init->front_offset;
926 dev_priv->front_pitch = init->front_pitch;
927 dev_priv->back_offset = init->back_offset;
928 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000930 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 case 16:
932 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
933 break;
934 case 32:
935 default:
936 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
937 break;
938 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000939 dev_priv->depth_offset = init->depth_offset;
940 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
942 /* Hardware state for depth clears. Remove this if/when we no
943 * longer clear the depth buffer with a 3D rectangle. Hard-code
944 * all values to prevent unwanted 3D state from slipping through
945 * and screwing with the clear operation.
946 */
947 dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
948 (dev_priv->color_fmt << 10) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949 (dev_priv->microcode_version ==
950 UCODE_R100 ? RADEON_ZBLOCK16 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 dev_priv->depth_clear.rb3d_zstencilcntl =
953 (dev_priv->depth_fmt |
954 RADEON_Z_TEST_ALWAYS |
955 RADEON_STENCIL_TEST_ALWAYS |
956 RADEON_STENCIL_S_FAIL_REPLACE |
957 RADEON_STENCIL_ZPASS_REPLACE |
958 RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
961 RADEON_BFACE_SOLID |
962 RADEON_FFACE_SOLID |
963 RADEON_FLAT_SHADE_VTX_LAST |
964 RADEON_DIFFUSE_SHADE_FLAT |
965 RADEON_ALPHA_SHADE_FLAT |
966 RADEON_SPECULAR_SHADE_FLAT |
967 RADEON_FOG_SHADE_FLAT |
968 RADEON_VTX_PIX_CENTER_OGL |
969 RADEON_ROUND_MODE_TRUNC |
970 RADEON_ROUND_PREC_8TH_PIX);
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 dev_priv->ring_offset = init->ring_offset;
974 dev_priv->ring_rptr_offset = init->ring_rptr_offset;
975 dev_priv->buffers_offset = init->buffers_offset;
976 dev_priv->gart_textures_offset = init->gart_textures_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000977
Dave Airlieda509d72007-05-26 05:04:51 +1000978 dev_priv->sarea = drm_getsarea(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000979 if (!dev_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 DRM_ERROR("could not find sarea!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000982 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 dev_priv->cp_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000986 if (!dev_priv->cp_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 DRM_ERROR("could not find cp ring region!\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 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000992 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 DRM_ERROR("could not find ring read pointer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000995 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000997 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 DRM_ERROR("could not find dma buffer region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001002 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001005 if (init->gart_textures_offset) {
1006 dev_priv->gart_textures =
1007 drm_core_findmap(dev, init->gart_textures_offset);
1008 if (!dev_priv->gart_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 DRM_ERROR("could not find GART texture region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 }
1014
1015 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001016 (drm_radeon_sarea_t *) ((u8 *) dev_priv->sarea->handle +
1017 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001020 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 drm_core_ioremap(dev_priv->cp_ring, dev);
1022 drm_core_ioremap(dev_priv->ring_rptr, dev);
1023 drm_core_ioremap(dev->agp_buffer_map, dev);
1024 if (!dev_priv->cp_ring->handle ||
1025 !dev_priv->ring_rptr->handle ||
1026 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 DRM_ERROR("could not find ioremap agp regions!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001029 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 } else
1032#endif
1033 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 dev_priv->cp_ring->handle = (void *)dev_priv->cp_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 dev_priv->ring_rptr->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 (void *)dev_priv->ring_rptr->offset;
1037 dev->agp_buffer_map->handle =
1038 (void *)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
1041 dev_priv->cp_ring->handle);
1042 DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
1043 dev_priv->ring_rptr->handle);
1044 DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
1045 dev->agp_buffer_map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001048 dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
Dave Airliebc5f4522007-11-05 12:50:58 +10001049 dev_priv->fb_size =
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001050 ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
Dave Airlied5ea7022006-03-19 19:37:55 +11001051 - dev_priv->fb_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001053 dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
1054 ((dev_priv->front_offset
1055 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001057 dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
1058 ((dev_priv->back_offset
1059 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
1062 ((dev_priv->depth_offset
1063 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 dev_priv->gart_size = init->gart_size;
Dave Airlied5ea7022006-03-19 19:37:55 +11001066
1067 /* New let's set the memory map ... */
1068 if (dev_priv->new_memmap) {
1069 u32 base = 0;
1070
1071 DRM_INFO("Setting GART location based on new memory map\n");
1072
1073 /* If using AGP, try to locate the AGP aperture at the same
1074 * location in the card and on the bus, though we have to
1075 * align it down.
1076 */
1077#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001078 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001079 base = dev->agp->base;
1080 /* Check if valid */
Michel Dänzer80b2c382007-02-18 18:03:21 +11001081 if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
1082 base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001083 DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
1084 dev->agp->base);
1085 base = 0;
1086 }
1087 }
1088#endif
1089 /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
1090 if (base == 0) {
1091 base = dev_priv->fb_location + dev_priv->fb_size;
Michel Dänzer80b2c382007-02-18 18:03:21 +11001092 if (base < dev_priv->fb_location ||
1093 ((base + dev_priv->gart_size) & 0xfffffffful) < base)
Dave Airlied5ea7022006-03-19 19:37:55 +11001094 base = dev_priv->fb_location
1095 - dev_priv->gart_size;
Dave Airliebc5f4522007-11-05 12:50:58 +10001096 }
Dave Airlied5ea7022006-03-19 19:37:55 +11001097 dev_priv->gart_vm_start = base & 0xffc00000u;
1098 if (dev_priv->gart_vm_start != base)
1099 DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
1100 base, dev_priv->gart_vm_start);
1101 } else {
1102 DRM_INFO("Setting GART location based on old memory map\n");
1103 dev_priv->gart_vm_start = dev_priv->fb_location +
1104 RADEON_READ(RADEON_CONFIG_APER_SIZE);
1105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001108 if (dev_priv->flags & RADEON_IS_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001110 - dev->agp->base
1111 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 else
1113#endif
1114 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +01001115 - (unsigned long)dev->sg->virtual
1116 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001118 DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
1119 DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
1120 DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
1121 dev_priv->gart_buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
1124 dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 + init->ring_size / sizeof(u32));
1126 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001127 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Roland Scheidegger576cc452008-02-07 14:59:24 +10001129 dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
1130 dev_priv->ring.rptr_update_l2qw = drm_order( /* init->rptr_update */ 4096 / 8);
1131
1132 dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
1133 dev_priv->ring.fetch_size_l2ow = drm_order( /* init->fetch_size */ 32 / 16);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001134 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
1136 dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1137
1138#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001139 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 } else
1143#endif
1144 {
Dave Airlieb05c2382008-03-17 10:24:24 +10001145 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +10001146 /* if we have an offset set from userspace */
Dave Airlief2b04cd2007-05-08 15:19:23 +10001147 if (dev_priv->pcigart_offset_set) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001148 dev_priv->gart_info.bus_addr =
1149 dev_priv->pcigart_offset + dev_priv->fb_location;
Dave Airlief26c4732006-01-02 17:18:39 +11001150 dev_priv->gart_info.mapping.offset =
Dave Airlie7fc86862007-11-05 10:45:27 +10001151 dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
Dave Airlief26c4732006-01-02 17:18:39 +11001152 dev_priv->gart_info.mapping.size =
Dave Airlief2b04cd2007-05-08 15:19:23 +10001153 dev_priv->gart_info.table_size;
Dave Airlief26c4732006-01-02 17:18:39 +11001154
Dave Airlie242e3df2008-07-15 15:48:05 +10001155 drm_core_ioremap_wc(&dev_priv->gart_info.mapping, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156 dev_priv->gart_info.addr =
Dave Airlief26c4732006-01-02 17:18:39 +11001157 dev_priv->gart_info.mapping.handle;
Dave Airlieea98a922005-09-11 20:28:11 +10001158
Dave Airlief2b04cd2007-05-08 15:19:23 +10001159 if (dev_priv->flags & RADEON_IS_PCIE)
1160 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
1161 else
1162 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 dev_priv->gart_info.gart_table_location =
1164 DRM_ATI_GART_FB;
1165
Dave Airlief26c4732006-01-02 17:18:39 +11001166 DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001167 dev_priv->gart_info.addr,
1168 dev_priv->pcigart_offset);
1169 } else {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001170 if (dev_priv->flags & RADEON_IS_IGPGART)
1171 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
1172 else
1173 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001174 dev_priv->gart_info.gart_table_location =
1175 DRM_ATI_GART_MAIN;
Dave Airlief26c4732006-01-02 17:18:39 +11001176 dev_priv->gart_info.addr = NULL;
1177 dev_priv->gart_info.bus_addr = 0;
Dave Airlie54a56ac2006-09-22 04:25:09 +10001178 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001179 DRM_ERROR
1180 ("Cannot use PCI Express without GART in FB memory\n");
Dave Airlieea98a922005-09-11 20:28:11 +10001181 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001182 return -EINVAL;
Dave Airlieea98a922005-09-11 20:28:11 +10001183 }
1184 }
1185
1186 if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001187 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001189 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 }
1191
1192 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001193 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
1195
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001196 radeon_cp_load_microcode(dev_priv);
1197 radeon_cp_init_ring_buffer(dev, dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
1199 dev_priv->last_buf = 0;
1200
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 radeon_do_engine_reset(dev);
Dave Airlied5ea7022006-03-19 19:37:55 +11001202 radeon_test_writeback(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
1204 return 0;
1205}
1206
Dave Airlie84b1fd12007-07-11 15:53:27 +10001207static int radeon_do_cleanup_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208{
1209 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001210 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /* Make sure interrupts are disabled here because the uninstall ioctl
1213 * may not have been called from userspace and after dev_private
1214 * is freed, it's too late.
1215 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001216 if (dev->irq_enabled)
1217 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001220 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied985c102006-01-02 21:32:48 +11001221 if (dev_priv->cp_ring != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001222 drm_core_ioremapfree(dev_priv->cp_ring, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001223 dev_priv->cp_ring = NULL;
1224 }
1225 if (dev_priv->ring_rptr != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001226 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001227 dev_priv->ring_rptr = NULL;
1228 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001229 if (dev->agp_buffer_map != NULL) {
1230 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 dev->agp_buffer_map = NULL;
1232 }
1233 } else
1234#endif
1235 {
Dave Airlied985c102006-01-02 21:32:48 +11001236
1237 if (dev_priv->gart_info.bus_addr) {
1238 /* Turn off PCI GART */
1239 radeon_set_pcigart(dev_priv, 0);
Dave Airlieea98a922005-09-11 20:28:11 +10001240 if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
1241 DRM_ERROR("failed to cleanup PCI GART!\n");
Dave Airlied985c102006-01-02 21:32:48 +11001242 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001243
Dave Airlied985c102006-01-02 21:32:48 +11001244 if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
1245 {
Dave Airlief26c4732006-01-02 17:18:39 +11001246 drm_core_ioremapfree(&dev_priv->gart_info.mapping, dev);
Dave Airlief2b04cd2007-05-08 15:19:23 +10001247 dev_priv->gart_info.addr = 0;
Dave Airlieea98a922005-09-11 20:28:11 +10001248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /* only clear to the start of flags */
1251 memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
1252
1253 return 0;
1254}
1255
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001256/* This code will reinit the Radeon CP hardware after a resume from disc.
1257 * AFAIK, it would be very difficult to pickle the state at suspend time, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 * here we make sure that all Radeon hardware initialisation is re-done without
1259 * affecting running applications.
1260 *
1261 * Charl P. Botha <http://cpbotha.net>
1262 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001263static int radeon_do_resume_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 drm_radeon_private_t *dev_priv = dev->dev_private;
1266
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001267 if (!dev_priv) {
1268 DRM_ERROR("Called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001269 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
1271
1272 DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1273
1274#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001275 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001277 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 } else
1279#endif
1280 {
1281 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001282 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001285 radeon_cp_load_microcode(dev_priv);
1286 radeon_cp_init_ring_buffer(dev, dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001288 radeon_do_engine_reset(dev);
Dennis Kasprzyk7ecabc52008-06-19 12:36:55 +10001289 radeon_enable_interrupt(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 DRM_DEBUG("radeon_do_resume_cp() complete\n");
1292
1293 return 0;
1294}
1295
Eric Anholtc153f452007-09-03 12:06:45 +10001296int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
Eric Anholtc153f452007-09-03 12:06:45 +10001298 drm_radeon_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Eric Anholt6c340ea2007-08-25 20:23:09 +10001300 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Eric Anholtc153f452007-09-03 12:06:45 +10001302 if (init->func == RADEON_INIT_R300_CP)
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001303 r300_init_reg_flags(dev);
Dave Airlie414ed532005-08-16 20:43:16 +10001304
Eric Anholtc153f452007-09-03 12:06:45 +10001305 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 case RADEON_INIT_CP:
1307 case RADEON_INIT_R200_CP:
1308 case RADEON_INIT_R300_CP:
Eric Anholtc153f452007-09-03 12:06:45 +10001309 return radeon_do_init_cp(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 case RADEON_CLEANUP_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001311 return radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Eric Anholt20caafa2007-08-25 19:22:43 +10001314 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315}
1316
Eric Anholtc153f452007-09-03 12:06:45 +10001317int radeon_cp_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Eric Anholt6c340ea2007-08-25 20:23:09 +10001322 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001324 if (dev_priv->cp_running) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001325 DRM_DEBUG("while CP running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return 0;
1327 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001328 if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001329 DRM_DEBUG("called with bogus CP mode (%d)\n",
1330 dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 return 0;
1332 }
1333
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001334 radeon_do_cp_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 return 0;
1337}
1338
1339/* Stop the CP. The engine must have been idled before calling this
1340 * routine.
1341 */
Eric Anholtc153f452007-09-03 12:06:45 +10001342int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001345 drm_radeon_cp_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001347 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
Eric Anholt6c340ea2007-08-25 20:23:09 +10001349 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (!dev_priv->cp_running)
1352 return 0;
1353
1354 /* Flush any pending CP commands. This ensures any outstanding
1355 * commands are exectuted by the engine before we turn it off.
1356 */
Eric Anholtc153f452007-09-03 12:06:45 +10001357 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001358 radeon_do_cp_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360
1361 /* If we fail to make the engine go idle, we return an error
1362 * code so that the DRM ioctl wrapper can try again.
1363 */
Eric Anholtc153f452007-09-03 12:06:45 +10001364 if (stop->idle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 ret = radeon_do_cp_idle(dev_priv);
1366 if (ret)
1367 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
1369
1370 /* Finally, we can turn off the CP. If the engine isn't idle,
1371 * we will get some dropped triangles as they won't be fully
1372 * rendered before the CP is shut down.
1373 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001374 radeon_do_cp_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /* Reset the engine */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001377 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 return 0;
1380}
1381
Dave Airlie84b1fd12007-07-11 15:53:27 +10001382void radeon_do_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 drm_radeon_private_t *dev_priv = dev->dev_private;
1385 int i, ret;
1386
1387 if (dev_priv) {
1388 if (dev_priv->cp_running) {
1389 /* Stop the cp */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001390 while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1392#ifdef __linux__
1393 schedule();
1394#else
1395 tsleep(&ret, PZERO, "rdnrel", 1);
1396#endif
1397 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001398 radeon_do_cp_stop(dev_priv);
1399 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402 /* Disable *all* interrupts */
1403 if (dev_priv->mmio) /* remove this after permanent addmaps */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001404 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001406 if (dev_priv->mmio) { /* remove all surfaces */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001408 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
1409 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
1410 16 * i, 0);
1411 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
1412 16 * i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 }
1415
1416 /* Free memory heap structures */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001417 radeon_mem_takedown(&(dev_priv->gart_heap));
1418 radeon_mem_takedown(&(dev_priv->fb_heap));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /* deallocate kernel resources */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001421 radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423}
1424
1425/* Just reset the CP ring. Called as part of an X Server engine reset.
1426 */
Eric Anholtc153f452007-09-03 12:06:45 +10001427int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Eric Anholt6c340ea2007-08-25 20:23:09 +10001432 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001434 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001435 DRM_DEBUG("called before init done\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001436 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 }
1438
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /* The CP is no longer running after an engine reset */
1442 dev_priv->cp_running = 0;
1443
1444 return 0;
1445}
1446
Eric Anholtc153f452007-09-03 12:06:45 +10001447int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001450 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Eric Anholt6c340ea2007-08-25 20:23:09 +10001452 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454 return radeon_do_cp_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
1457/* Added by Charl P. Botha to call radeon_do_resume_cp().
1458 */
Eric Anholtc153f452007-09-03 12:06:45 +10001459int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
1462 return radeon_do_resume_cp(dev);
1463}
1464
Eric Anholtc153f452007-09-03 12:06:45 +10001465int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001467 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Eric Anholt6c340ea2007-08-25 20:23:09 +10001469 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001471 return radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474/* ================================================================
1475 * Fullscreen mode
1476 */
1477
1478/* KW: Deprecated to say the least:
1479 */
Eric Anholtc153f452007-09-03 12:06:45 +10001480int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 return 0;
1483}
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485/* ================================================================
1486 * Freelist management
1487 */
1488
1489/* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1490 * bufs until freelist code is used. Note this hides a problem with
1491 * the scratch register * (used to keep track of last buffer
1492 * completed) being written to before * the last buffer has actually
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001493 * completed rendering.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 *
1495 * KW: It's also a good way to find free buffers quickly.
1496 *
1497 * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1498 * sleep. However, bugs in older versions of radeon_accel.c mean that
1499 * we essentially have to do this, else old clients will break.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001500 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 * However, it does leave open a potential deadlock where all the
1502 * buffers are held by other clients, which can't release them because
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001503 * they can't get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
1505
Dave Airlie056219e2007-07-11 16:17:42 +10001506struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507{
Dave Airliecdd55a22007-07-11 16:32:08 +10001508 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 drm_radeon_private_t *dev_priv = dev->dev_private;
1510 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001511 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 int i, t;
1513 int start;
1514
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 dev_priv->last_buf = 0;
1517
1518 start = dev_priv->last_buf;
1519
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001520 for (t = 0; t < dev_priv->usec_timeout; t++) {
1521 u32 done_age = GET_SCRATCH(1);
1522 DRM_DEBUG("done_age = %d\n", done_age);
1523 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 buf = dma->buflist[i];
1525 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001526 if (buf->file_priv == NULL || (buf->pending &&
1527 buf_priv->age <=
1528 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 dev_priv->stats.requested_bufs++;
1530 buf->pending = 0;
1531 return buf;
1532 }
1533 start = 0;
1534 }
1535
1536 if (t) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001537 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 dev_priv->stats.freelist_loops++;
1539 }
1540 }
1541
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001542 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 return NULL;
1544}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546#if 0
Dave Airlie056219e2007-07-11 16:17:42 +10001547struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548{
Dave Airliecdd55a22007-07-11 16:32:08 +10001549 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 drm_radeon_private_t *dev_priv = dev->dev_private;
1551 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001552 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int i, t;
1554 int start;
1555 u32 done_age = DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1));
1556
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001557 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 dev_priv->last_buf = 0;
1559
1560 start = dev_priv->last_buf;
1561 dev_priv->stats.freelist_loops++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001562
1563 for (t = 0; t < 2; t++) {
1564 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 buf = dma->buflist[i];
1566 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001567 if (buf->file_priv == 0 || (buf->pending &&
1568 buf_priv->age <=
1569 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 dev_priv->stats.requested_bufs++;
1571 buf->pending = 0;
1572 return buf;
1573 }
1574 }
1575 start = 0;
1576 }
1577
1578 return NULL;
1579}
1580#endif
1581
Dave Airlie84b1fd12007-07-11 15:53:27 +10001582void radeon_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Dave Airliecdd55a22007-07-11 16:32:08 +10001584 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 drm_radeon_private_t *dev_priv = dev->dev_private;
1586 int i;
1587
1588 dev_priv->last_buf = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001590 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1592 buf_priv->age = 0;
1593 }
1594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596/* ================================================================
1597 * CP command submission
1598 */
1599
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001600int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1603 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001604 u32 last_head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001606 for (i = 0; i < dev_priv->usec_timeout; i++) {
1607 u32 head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 ring->space = (head - ring->tail) * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001610 if (ring->space <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 ring->space += ring->size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001612 if (ring->space > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
1616
1617 if (head != last_head)
1618 i = 0;
1619 last_head = head;
1620
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001621 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 }
1623
1624 /* FIXME: This return value is ignored in the BEGIN_RING macro! */
1625#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001626 radeon_status(dev_priv);
1627 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628#endif
Eric Anholt20caafa2007-08-25 19:22:43 +10001629 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630}
1631
Eric Anholt6c340ea2007-08-25 20:23:09 +10001632static int radeon_cp_get_buffers(struct drm_device *dev,
1633 struct drm_file *file_priv,
Dave Airliec60ce622007-07-11 15:27:12 +10001634 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001637 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001639 for (i = d->granted_count; i < d->request_count; i++) {
1640 buf = radeon_freelist_get(dev);
1641 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001642 return -EBUSY; /* NOTE: broken client */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Eric Anholt6c340ea2007-08-25 20:23:09 +10001644 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001646 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
1647 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001648 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001649 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
1650 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001651 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 d->granted_count++;
1654 }
1655 return 0;
1656}
1657
Eric Anholtc153f452007-09-03 12:06:45 +10001658int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659{
Dave Airliecdd55a22007-07-11 16:32:08 +10001660 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001662 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Eric Anholt6c340ea2007-08-25 20:23:09 +10001664 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 /* Please don't send us buffers.
1667 */
Eric Anholtc153f452007-09-03 12:06:45 +10001668 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001669 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001670 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001671 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
1674 /* We'll send you buffers.
1675 */
Eric Anholtc153f452007-09-03 12:06:45 +10001676 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001677 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001678 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001679 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 }
1681
Eric Anholtc153f452007-09-03 12:06:45 +10001682 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Eric Anholtc153f452007-09-03 12:06:45 +10001684 if (d->request_count) {
1685 ret = radeon_cp_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 }
1687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 return ret;
1689}
1690
Dave Airlie22eae942005-11-10 22:16:34 +11001691int radeon_driver_load(struct drm_device *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692{
1693 drm_radeon_private_t *dev_priv;
1694 int ret = 0;
1695
1696 dev_priv = drm_alloc(sizeof(drm_radeon_private_t), DRM_MEM_DRIVER);
1697 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10001698 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700 memset(dev_priv, 0, sizeof(drm_radeon_private_t));
1701 dev->dev_private = (void *)dev_priv;
1702 dev_priv->flags = flags;
1703
Dave Airlie54a56ac2006-09-22 04:25:09 +10001704 switch (flags & RADEON_FAMILY_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 case CHIP_R100:
1706 case CHIP_RV200:
1707 case CHIP_R200:
1708 case CHIP_R300:
Dave Airlieb15ec362006-08-19 17:43:52 +10001709 case CHIP_R350:
Dave Airlie414ed532005-08-16 20:43:16 +10001710 case CHIP_R420:
Dave Airlieb15ec362006-08-19 17:43:52 +10001711 case CHIP_RV410:
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001712 case CHIP_RV515:
1713 case CHIP_R520:
1714 case CHIP_RV570:
1715 case CHIP_R580:
Dave Airlie54a56ac2006-09-22 04:25:09 +10001716 dev_priv->flags |= RADEON_HAS_HIERZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 break;
1718 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001719 /* all other chips have no hierarchical z buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 break;
1721 }
Dave Airlie414ed532005-08-16 20:43:16 +10001722
1723 if (drm_device_is_agp(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001724 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlieb15ec362006-08-19 17:43:52 +10001725 else if (drm_device_is_pcie(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001726 dev_priv->flags |= RADEON_IS_PCIE;
Dave Airlieb15ec362006-08-19 17:43:52 +10001727 else
Dave Airlie54a56ac2006-09-22 04:25:09 +10001728 dev_priv->flags |= RADEON_IS_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +10001729
Dave Airlie414ed532005-08-16 20:43:16 +10001730 DRM_DEBUG("%s card detected\n",
Dave Airlie54a56ac2006-09-22 04:25:09 +10001731 ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 return ret;
1733}
1734
Dave Airlie22eae942005-11-10 22:16:34 +11001735/* Create mappings for registers and framebuffer so userland doesn't necessarily
1736 * have to find them.
1737 */
1738int radeon_driver_firstopen(struct drm_device *dev)
Dave Airlie836cf042005-07-10 19:27:04 +10001739{
1740 int ret;
1741 drm_local_map_t *map;
1742 drm_radeon_private_t *dev_priv = dev->dev_private;
1743
Dave Airlief2b04cd2007-05-08 15:19:23 +10001744 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
1745
Dave Airlie836cf042005-07-10 19:27:04 +10001746 ret = drm_addmap(dev, drm_get_resource_start(dev, 2),
1747 drm_get_resource_len(dev, 2), _DRM_REGISTERS,
1748 _DRM_READ_ONLY, &dev_priv->mmio);
1749 if (ret != 0)
1750 return ret;
1751
Dave Airlie7fc86862007-11-05 10:45:27 +10001752 dev_priv->fb_aper_offset = drm_get_resource_start(dev, 0);
1753 ret = drm_addmap(dev, dev_priv->fb_aper_offset,
Dave Airlie836cf042005-07-10 19:27:04 +10001754 drm_get_resource_len(dev, 0), _DRM_FRAME_BUFFER,
1755 _DRM_WRITE_COMBINING, &map);
1756 if (ret != 0)
1757 return ret;
1758
1759 return 0;
1760}
1761
Dave Airlie22eae942005-11-10 22:16:34 +11001762int radeon_driver_unload(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
1764 drm_radeon_private_t *dev_priv = dev->dev_private;
1765
1766 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER);
1768
1769 dev->dev_private = NULL;
1770 return 0;
1771}