blob: 3a424986e85eb106e40c9a08403e887078fcf612 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Alex Deucher45e51902008-05-28 13:28:59 +100044static u32 R500_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
Dave Airlie3d5e2c12008-02-07 15:01:05 +100045{
46 u32 ret;
47 RADEON_WRITE(R520_MC_IND_INDEX, 0x7f0000 | (addr & 0xff));
48 ret = RADEON_READ(R520_MC_IND_DATA);
49 RADEON_WRITE(R520_MC_IND_INDEX, 0);
50 return ret;
51}
52
Alex Deucher45e51902008-05-28 13:28:59 +100053static u32 RS480_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
54{
55 u32 ret;
56 RADEON_WRITE(RS480_NB_MC_INDEX, addr & 0xff);
57 ret = RADEON_READ(RS480_NB_MC_DATA);
58 RADEON_WRITE(RS480_NB_MC_INDEX, 0xff);
59 return ret;
60}
61
Maciej Cencora60f92682008-02-19 21:32:45 +100062static u32 RS690_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
63{
Alex Deucher45e51902008-05-28 13:28:59 +100064 u32 ret;
Maciej Cencora60f92682008-02-19 21:32:45 +100065 RADEON_WRITE(RS690_MC_INDEX, (addr & RS690_MC_INDEX_MASK));
Alex Deucher45e51902008-05-28 13:28:59 +100066 ret = RADEON_READ(RS690_MC_DATA);
67 RADEON_WRITE(RS690_MC_INDEX, RS690_MC_INDEX_MASK);
68 return ret;
69}
70
71static u32 IGP_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
72{
73 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
74 return RS690_READ_MCIND(dev_priv, addr);
75 else
76 return RS480_READ_MCIND(dev_priv, addr);
Maciej Cencora60f92682008-02-19 21:32:45 +100077}
78
Dave Airlie3d5e2c12008-02-07 15:01:05 +100079u32 radeon_read_fb_location(drm_radeon_private_t *dev_priv)
80{
81
82 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100083 return R500_READ_MCIND(dev_priv, RV515_MC_FB_LOCATION);
Maciej Cencora60f92682008-02-19 21:32:45 +100084 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
85 return RS690_READ_MCIND(dev_priv, RS690_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100086 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100087 return R500_READ_MCIND(dev_priv, R520_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100088 else
89 return RADEON_READ(RADEON_MC_FB_LOCATION);
90}
91
92static void radeon_write_fb_location(drm_radeon_private_t *dev_priv, u32 fb_loc)
93{
94 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100095 R500_WRITE_MCIND(RV515_MC_FB_LOCATION, fb_loc);
Maciej Cencora60f92682008-02-19 21:32:45 +100096 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
97 RS690_WRITE_MCIND(RS690_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100098 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100099 R500_WRITE_MCIND(R520_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000100 else
101 RADEON_WRITE(RADEON_MC_FB_LOCATION, fb_loc);
102}
103
104static void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_loc)
105{
106 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000107 R500_WRITE_MCIND(RV515_MC_AGP_LOCATION, agp_loc);
Maciej Cencora60f92682008-02-19 21:32:45 +1000108 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
109 RS690_WRITE_MCIND(RS690_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000110 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000111 R500_WRITE_MCIND(R520_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000112 else
113 RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc);
114}
115
Dave Airlie84b1fd12007-07-11 15:53:27 +1000116static int RADEON_READ_PLL(struct drm_device * dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 drm_radeon_private_t *dev_priv = dev->dev_private;
119
120 RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
121 return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
122}
123
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000124static u32 RADEON_READ_PCIE(drm_radeon_private_t *dev_priv, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Dave Airlieea98a922005-09-11 20:28:11 +1000126 RADEON_WRITE8(RADEON_PCIE_INDEX, addr & 0xff);
127 return RADEON_READ(RADEON_PCIE_DATA);
128}
129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131static void radeon_status(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700133 printk("%s:\n", __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000134 printk("RBBM_STATUS = 0x%08x\n",
135 (unsigned int)RADEON_READ(RADEON_RBBM_STATUS));
136 printk("CP_RB_RTPR = 0x%08x\n",
137 (unsigned int)RADEON_READ(RADEON_CP_RB_RPTR));
138 printk("CP_RB_WTPR = 0x%08x\n",
139 (unsigned int)RADEON_READ(RADEON_CP_RB_WPTR));
140 printk("AIC_CNTL = 0x%08x\n",
141 (unsigned int)RADEON_READ(RADEON_AIC_CNTL));
142 printk("AIC_STAT = 0x%08x\n",
143 (unsigned int)RADEON_READ(RADEON_AIC_STAT));
144 printk("AIC_PT_BASE = 0x%08x\n",
145 (unsigned int)RADEON_READ(RADEON_AIC_PT_BASE));
146 printk("TLB_ADDR = 0x%08x\n",
147 (unsigned int)RADEON_READ(RADEON_AIC_TLB_ADDR));
148 printk("TLB_DATA = 0x%08x\n",
149 (unsigned int)RADEON_READ(RADEON_AIC_TLB_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151#endif
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* ================================================================
154 * Engine, FIFO control
155 */
156
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000157static int radeon_do_pixcache_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 u32 tmp;
160 int i;
161
162 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
163
Michel Dänzerb9b603dd2006-08-07 20:41:53 +1000164 tmp = RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT);
165 tmp |= RADEON_RB3D_DC_FLUSH_ALL;
166 RADEON_WRITE(RADEON_RB3D_DSTCACHE_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000168 for (i = 0; i < dev_priv->usec_timeout; i++) {
Michel Dänzerb9b603dd2006-08-07 20:41:53 +1000169 if (!(RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT)
170 & RADEON_RB3D_DC_BUSY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return 0;
172 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000173 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177 DRM_ERROR("failed!\n");
178 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000180 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000183static int radeon_do_wait_for_fifo(drm_radeon_private_t * dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 int i;
186
187 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
188
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189 for (i = 0; i < dev_priv->usec_timeout; i++) {
190 int slots = (RADEON_READ(RADEON_RBBM_STATUS)
191 & RADEON_RBBM_FIFOCNT_MASK);
192 if (slots >= entries)
193 return 0;
194 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
197#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 DRM_ERROR("failed!\n");
199 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000201 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204static int radeon_do_wait_for_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 int i, ret;
207
208 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
209
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000210 ret = radeon_do_wait_for_fifo(dev_priv, 64);
211 if (ret)
212 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 for (i = 0; i < dev_priv->usec_timeout; i++) {
215 if (!(RADEON_READ(RADEON_RBBM_STATUS)
216 & RADEON_RBBM_ACTIVE)) {
217 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return 0;
219 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000220 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
222
223#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000224 DRM_ERROR("failed!\n");
225 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000227 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* ================================================================
231 * CP control, initialization
232 */
233
234/* Load the microcode for the CP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000235static void radeon_cp_load_microcode(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236{
237 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000240 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000242 RADEON_WRITE(RADEON_CP_ME_RAM_ADDR, 0);
Alex Deucher9f184092008-05-28 11:21:25 +1000243 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R100) ||
244 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV100) ||
245 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV200) ||
246 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS100) ||
247 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS200)) {
248 DRM_INFO("Loading R100 Microcode\n");
249 for (i = 0; i < 256; i++) {
250 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
251 R100_cp_microcode[i][1]);
252 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
253 R100_cp_microcode[i][0]);
254 }
255 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R200) ||
256 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV250) ||
257 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV280) ||
258 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS300)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 DRM_INFO("Loading R200 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 for (i = 0; i < 256; i++) {
261 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
262 R200_cp_microcode[i][1]);
263 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
264 R200_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Alex Deucher9f184092008-05-28 11:21:25 +1000266 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
267 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350) ||
268 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV350) ||
269 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000270 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 DRM_INFO("Loading R300 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 for (i = 0; i < 256; i++) {
273 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
274 R300_cp_microcode[i][1]);
275 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
276 R300_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Alex Deucher9f184092008-05-28 11:21:25 +1000278 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
279 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV410)) {
280 DRM_INFO("Loading R400 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 for (i = 0; i < 256; i++) {
282 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
Alex Deucher9f184092008-05-28 11:21:25 +1000283 R420_cp_microcode[i][1]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000284 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
Alex Deucher9f184092008-05-28 11:21:25 +1000285 R420_cp_microcode[i][0]);
286 }
287 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) {
288 DRM_INFO("Loading RS690 Microcode\n");
289 for (i = 0; i < 256; i++) {
290 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
291 RS690_cp_microcode[i][1]);
292 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
293 RS690_cp_microcode[i][0]);
294 }
295 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) ||
296 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R520) ||
297 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) ||
298 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R580) ||
299 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV560) ||
300 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV570)) {
301 DRM_INFO("Loading R500 Microcode\n");
302 for (i = 0; i < 256; i++) {
303 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
304 R520_cp_microcode[i][1]);
305 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
306 R520_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308 }
309}
310
311/* Flush any pending commands to the CP. This should only be used just
312 * prior to a wait for idle, as it informs the engine that the command
313 * stream is ending.
314 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315static void radeon_do_cp_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000317 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#if 0
319 u32 tmp;
320
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 tmp = RADEON_READ(RADEON_CP_RB_WPTR) | (1 << 31);
322 RADEON_WRITE(RADEON_CP_RB_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323#endif
324}
325
326/* Wait for the CP to go idle.
327 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328int radeon_do_cp_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000331 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000333 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 RADEON_PURGE_CACHE();
336 RADEON_PURGE_ZCACHE();
337 RADEON_WAIT_UNTIL_IDLE();
338
339 ADVANCE_RING();
340 COMMIT_RING();
341
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000342 return radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
345/* Start the Command Processor.
346 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347static void radeon_do_cp_start(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000352 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 RADEON_WRITE(RADEON_CP_CSQ_CNTL, dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 dev_priv->cp_running = 1;
357
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000358 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 RADEON_PURGE_CACHE();
361 RADEON_PURGE_ZCACHE();
362 RADEON_WAIT_UNTIL_IDLE();
363
364 ADVANCE_RING();
365 COMMIT_RING();
366}
367
368/* Reset the Command Processor. This will not flush any pending
369 * commands, so you must wait for the CP command stream to complete
370 * before calling this routine.
371 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372static void radeon_do_cp_reset(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 u32 cur_read_ptr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000375 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000377 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
378 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
379 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 dev_priv->ring.tail = cur_read_ptr;
381}
382
383/* Stop the Command Processor. This will not flush any pending
384 * commands, so you must flush the command stream and wait for the CP
385 * to go idle before calling this routine.
386 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387static void radeon_do_cp_stop(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000389 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000391 RADEON_WRITE(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 dev_priv->cp_running = 0;
394}
395
396/* Reset the engine. This will stop the CP if it is running.
397 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000398static int radeon_do_engine_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 drm_radeon_private_t *dev_priv = dev->dev_private;
401 u32 clock_cntl_index, mclk_cntl, rbbm_soft_reset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000402 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000404 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000406 if ((dev_priv->flags & RADEON_FAMILY_MASK) < CHIP_RV515) {
407 clock_cntl_index = RADEON_READ(RADEON_CLOCK_CNTL_INDEX);
408 mclk_cntl = RADEON_READ_PLL(dev, RADEON_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000410 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, (mclk_cntl |
411 RADEON_FORCEON_MCLKA |
412 RADEON_FORCEON_MCLKB |
413 RADEON_FORCEON_YCLKA |
414 RADEON_FORCEON_YCLKB |
415 RADEON_FORCEON_MC |
416 RADEON_FORCEON_AIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000418 rbbm_soft_reset = RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000420 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset |
421 RADEON_SOFT_RESET_CP |
422 RADEON_SOFT_RESET_HI |
423 RADEON_SOFT_RESET_SE |
424 RADEON_SOFT_RESET_RE |
425 RADEON_SOFT_RESET_PP |
426 RADEON_SOFT_RESET_E2 |
427 RADEON_SOFT_RESET_RB));
428 RADEON_READ(RADEON_RBBM_SOFT_RESET);
429 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset &
430 ~(RADEON_SOFT_RESET_CP |
431 RADEON_SOFT_RESET_HI |
432 RADEON_SOFT_RESET_SE |
433 RADEON_SOFT_RESET_RE |
434 RADEON_SOFT_RESET_PP |
435 RADEON_SOFT_RESET_E2 |
436 RADEON_SOFT_RESET_RB)));
437 RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000439 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, mclk_cntl);
440 RADEON_WRITE(RADEON_CLOCK_CNTL_INDEX, clock_cntl_index);
441 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, rbbm_soft_reset);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 /* Reset the CP ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000445 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 /* The CP is no longer running after an engine reset */
448 dev_priv->cp_running = 0;
449
450 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 radeon_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 return 0;
454}
455
Dave Airlie84b1fd12007-07-11 15:53:27 +1000456static void radeon_cp_init_ring_buffer(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457 drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458{
459 u32 ring_start, cur_read_ptr;
460 u32 tmp;
Dave Airliebc5f4522007-11-05 12:50:58 +1000461
Dave Airlied5ea7022006-03-19 19:37:55 +1100462 /* Initialize the memory controller. With new memory map, the fb location
463 * is not changed, it should have been properly initialized already. Part
464 * of the problem is that the code below is bogus, assuming the GART is
465 * always appended to the fb which is not necessarily the case
466 */
467 if (!dev_priv->new_memmap)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000468 radeon_write_fb_location(dev_priv,
Dave Airlied5ea7022006-03-19 19:37:55 +1100469 ((dev_priv->gart_vm_start - 1) & 0xffff0000)
470 | (dev_priv->fb_location >> 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
472#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000473 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +1100474 RADEON_WRITE(RADEON_AGP_BASE, (unsigned int)dev->agp->base);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000475 radeon_write_agp_location(dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000476 (((dev_priv->gart_vm_start - 1 +
477 dev_priv->gart_size) & 0xffff0000) |
478 (dev_priv->gart_vm_start >> 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 ring_start = (dev_priv->cp_ring->offset
481 - dev->agp->base
482 + dev_priv->gart_vm_start);
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100483 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#endif
485 ring_start = (dev_priv->cp_ring->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100486 - (unsigned long)dev->sg->virtual
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 + dev_priv->gart_vm_start);
488
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489 RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 /* Set the write pointer delay */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Initialize the ring buffer's read and write pointers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
496 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
497 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dev_priv->ring.tail = cur_read_ptr;
499
500#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000501 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000502 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
503 dev_priv->ring_rptr->offset
504 - dev->agp->base + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 } else
506#endif
507 {
Dave Airlie55910512007-07-11 16:53:40 +1000508 struct drm_sg_mem *entry = dev->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 unsigned long tmp_ofs, page_ofs;
510
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100511 tmp_ofs = dev_priv->ring_rptr->offset -
512 (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 page_ofs = tmp_ofs >> PAGE_SHIFT;
514
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000515 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR, entry->busaddr[page_ofs]);
516 DRM_DEBUG("ring rptr: offset=0x%08lx handle=0x%08lx\n",
517 (unsigned long)entry->busaddr[page_ofs],
518 entry->handle + tmp_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
Dave Airlied5ea7022006-03-19 19:37:55 +1100521 /* Set ring buffer size */
522#ifdef __BIG_ENDIAN
523 RADEON_WRITE(RADEON_CP_RB_CNTL,
Roland Scheidegger576cc452008-02-07 14:59:24 +1000524 RADEON_BUF_SWAP_32BIT |
525 (dev_priv->ring.fetch_size_l2ow << 18) |
526 (dev_priv->ring.rptr_update_l2qw << 8) |
527 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100528#else
Roland Scheidegger576cc452008-02-07 14:59:24 +1000529 RADEON_WRITE(RADEON_CP_RB_CNTL,
530 (dev_priv->ring.fetch_size_l2ow << 18) |
531 (dev_priv->ring.rptr_update_l2qw << 8) |
532 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100533#endif
534
535 /* Start with assuming that writeback doesn't work */
536 dev_priv->writeback_works = 0;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 /* Initialize the scratch register pointer. This will cause
539 * the scratch register values to be written out to memory
540 * whenever they are updated.
541 *
542 * We simply put this behind the ring read pointer, this works
543 * with PCI GART as well as (whatever kind of) AGP GART
544 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
546 + RADEON_SCRATCH_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 dev_priv->scratch = ((__volatile__ u32 *)
549 dev_priv->ring_rptr->handle +
550 (RADEON_SCRATCH_REG_OFFSET / sizeof(u32)));
551
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000552 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Dave Airlied5ea7022006-03-19 19:37:55 +1100554 /* Turn on bus mastering */
555 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
556 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
557
558 dev_priv->sarea_priv->last_frame = dev_priv->scratch[0] = 0;
559 RADEON_WRITE(RADEON_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame);
560
561 dev_priv->sarea_priv->last_dispatch = dev_priv->scratch[1] = 0;
562 RADEON_WRITE(RADEON_LAST_DISPATCH_REG,
563 dev_priv->sarea_priv->last_dispatch);
564
565 dev_priv->sarea_priv->last_clear = dev_priv->scratch[2] = 0;
566 RADEON_WRITE(RADEON_LAST_CLEAR_REG, dev_priv->sarea_priv->last_clear);
567
568 radeon_do_wait_for_idle(dev_priv);
569
570 /* Sync everything up */
571 RADEON_WRITE(RADEON_ISYNC_CNTL,
572 (RADEON_ISYNC_ANY2D_IDLE3D |
573 RADEON_ISYNC_ANY3D_IDLE2D |
574 RADEON_ISYNC_WAIT_IDLEGUI |
575 RADEON_ISYNC_CPSCRATCH_IDLEGUI));
576
577}
578
579static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
580{
581 u32 tmp;
582
583 /* Writeback doesn't seem to work everywhere, test it here and possibly
584 * enable it if it appears to work
585 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000586 DRM_WRITE32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1), 0);
587 RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589 for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
590 if (DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1)) ==
591 0xdeadbeef)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000593 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 if (tmp < dev_priv->usec_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 dev_priv->writeback_works = 1;
Dave Airlied5ea7022006-03-19 19:37:55 +1100598 DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else {
600 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100601 DRM_INFO("writeback test failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
Dave Airlie689b9d72005-09-30 17:09:07 +1000603 if (radeon_no_wb == 1) {
604 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100605 DRM_INFO("writeback forced off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Michel Dänzerae1b1a482006-08-07 20:37:46 +1000607
608 if (!dev_priv->writeback_works) {
609 /* Disable writeback to avoid unnecessary bus master transfer */
610 RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
611 RADEON_RB_NO_UPDATE);
612 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Dave Airlief2b04cd2007-05-08 15:19:23 +1000616/* Enable or disable IGP GART on the chip */
617static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
618{
Maciej Cencora60f92682008-02-19 21:32:45 +1000619 u32 temp;
620
621 if (on) {
Alex Deucher45e51902008-05-28 13:28:59 +1000622 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
Maciej Cencora60f92682008-02-19 21:32:45 +1000623 dev_priv->gart_vm_start,
624 (long)dev_priv->gart_info.bus_addr,
625 dev_priv->gart_size);
626
Alex Deucher45e51902008-05-28 13:28:59 +1000627 temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
628 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
629 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
630 RS690_BLOCK_GFX_D3_EN));
631 else
632 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
Maciej Cencora60f92682008-02-19 21:32:45 +1000633
Alex Deucher45e51902008-05-28 13:28:59 +1000634 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
635 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000636
Alex Deucher45e51902008-05-28 13:28:59 +1000637 temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
638 IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
639 RS480_TLB_ENABLE |
640 RS480_GTW_LAC_EN |
641 RS480_1LEVEL_GART));
Maciej Cencora60f92682008-02-19 21:32:45 +1000642
Dave Airliefa0d71b2008-05-28 11:27:01 +1000643 temp = dev_priv->gart_info.bus_addr & 0xfffff000;
644 temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
Alex Deucher45e51902008-05-28 13:28:59 +1000645 IGP_WRITE_MCIND(RS480_GART_BASE, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000646
Alex Deucher45e51902008-05-28 13:28:59 +1000647 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
648 IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
649 RS480_REQ_TYPE_SNOOP_DIS));
Maciej Cencora60f92682008-02-19 21:32:45 +1000650
Alex Deucher45e51902008-05-28 13:28:59 +1000651 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) {
652 IGP_WRITE_MCIND(RS690_MC_AGP_BASE,
653 (unsigned int)dev_priv->gart_vm_start);
654 IGP_WRITE_MCIND(RS690_MC_AGP_BASE_2, 0);
655 } else {
656 RADEON_WRITE(RADEON_AGP_BASE, (unsigned int)dev_priv->gart_vm_start);
657 RADEON_WRITE(RS480_AGP_BASE_2, 0);
658 }
Dave Airlie3722bfc2008-05-28 11:28:27 +1000659
Maciej Cencora60f92682008-02-19 21:32:45 +1000660 dev_priv->gart_size = 32*1024*1024;
661 temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
662 0xffff0000) | (dev_priv->gart_vm_start >> 16));
663
Alex Deucher45e51902008-05-28 13:28:59 +1000664 radeon_write_agp_location(dev_priv, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000665
Alex Deucher45e51902008-05-28 13:28:59 +1000666 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
667 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
668 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000669
670 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000671 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
672 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000673 break;
674 DRM_UDELAY(1);
675 } while (1);
676
Alex Deucher45e51902008-05-28 13:28:59 +1000677 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
678 RS480_GART_CACHE_INVALIDATE);
Alex Deucher27359772008-05-28 12:54:16 +1000679
Maciej Cencora60f92682008-02-19 21:32:45 +1000680 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000681 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
682 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000683 break;
684 DRM_UDELAY(1);
685 } while (1);
686
Alex Deucher45e51902008-05-28 13:28:59 +1000687 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000688 } else {
Alex Deucher45e51902008-05-28 13:28:59 +1000689 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000690 }
691}
692
Dave Airlieea98a922005-09-11 20:28:11 +1000693static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Dave Airlieea98a922005-09-11 20:28:11 +1000695 u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
696 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Dave Airlieea98a922005-09-11 20:28:11 +1000698 DRM_DEBUG("programming pcie %08X %08lX %08X\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 dev_priv->gart_vm_start,
700 (long)dev_priv->gart_info.bus_addr,
Dave Airlieea98a922005-09-11 20:28:11 +1000701 dev_priv->gart_size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000702 RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
703 dev_priv->gart_vm_start);
704 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
705 dev_priv->gart_info.bus_addr);
706 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
707 dev_priv->gart_vm_start);
708 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
709 dev_priv->gart_vm_start +
710 dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000712 radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
715 RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000717 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
718 tmp & ~RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720}
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722/* Enable or disable PCI GART on the chip */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000723static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Dave Airlied985c102006-01-02 21:32:48 +1100725 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
Alex Deucher45e51902008-05-28 13:28:59 +1000727 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
728 (dev_priv->flags & RADEON_IS_IGPGART)) {
Dave Airlief2b04cd2007-05-08 15:19:23 +1000729 radeon_set_igpgart(dev_priv, on);
730 return;
731 }
732
Dave Airlie54a56ac2006-09-22 04:25:09 +1000733 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieea98a922005-09-11 20:28:11 +1000734 radeon_set_pciegart(dev_priv, on);
735 return;
736 }
737
Dave Airliebc5f4522007-11-05 12:50:58 +1000738 tmp = RADEON_READ(RADEON_AIC_CNTL);
Dave Airlied985c102006-01-02 21:32:48 +1100739
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 if (on) {
741 RADEON_WRITE(RADEON_AIC_CNTL,
742 tmp | RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /* set PCI GART page-table base address
745 */
Dave Airlieea98a922005-09-11 20:28:11 +1000746 RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748 /* set address range for PCI address translate
749 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000750 RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
751 RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
752 + dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 /* Turn off AGP aperture -- is this required for PCI GART?
755 */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000756 radeon_write_agp_location(dev_priv, 0xffffffc0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000757 RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 RADEON_WRITE(RADEON_AIC_CNTL,
760 tmp & ~RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762}
763
Dave Airlie84b1fd12007-07-11 15:53:27 +1000764static int radeon_do_init_cp(struct drm_device * dev, drm_radeon_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Dave Airlied985c102006-01-02 21:32:48 +1100766 drm_radeon_private_t *dev_priv = dev->dev_private;
767
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000768 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Dave Airlief3dd5c32006-03-25 18:09:46 +1100770 /* if we require new memory map but we don't have it fail */
Dave Airlie54a56ac2006-09-22 04:25:09 +1000771 if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
Dave Airlieb15ec362006-08-19 17:43:52 +1000772 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 +1100773 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000774 return -EINVAL;
Dave Airlief3dd5c32006-03-25 18:09:46 +1100775 }
776
Dave Airlie54a56ac2006-09-22 04:25:09 +1000777 if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
Dave Airlied985c102006-01-02 21:32:48 +1100778 DRM_DEBUG("Forcing AGP card to PCI mode\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000779 dev_priv->flags &= ~RADEON_IS_AGP;
780 } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
Dave Airlieb15ec362006-08-19 17:43:52 +1000781 && !init->is_pci) {
782 DRM_DEBUG("Restoring AGP flag\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000783 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlied985c102006-01-02 21:32:48 +1100784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Dave Airlie54a56ac2006-09-22 04:25:09 +1000786 if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000789 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 }
791
792 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 if (dev_priv->usec_timeout < 1 ||
794 dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
795 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000797 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
Dave Airlieddbee332007-07-11 12:16:01 +1000800 /* Enable vblank on CRTC1 for older X servers
801 */
802 dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
803
Dave Airlied985c102006-01-02 21:32:48 +1100804 switch(init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 case RADEON_INIT_R200_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000806 dev_priv->microcode_version = UCODE_R200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 break;
808 case RADEON_INIT_R300_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000809 dev_priv->microcode_version = UCODE_R300;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
811 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000812 dev_priv->microcode_version = UCODE_R100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 dev_priv->do_boxes = 0;
816 dev_priv->cp_mode = init->cp_mode;
817
818 /* We don't support anything other than bus-mastering ring mode,
819 * but the ring can be in either AGP or PCI space for the ring
820 * read pointer.
821 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
823 (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
824 DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000826 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 case 16:
831 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
832 break;
833 case 32:
834 default:
835 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
836 break;
837 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 dev_priv->front_offset = init->front_offset;
839 dev_priv->front_pitch = init->front_pitch;
840 dev_priv->back_offset = init->back_offset;
841 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 case 16:
845 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
846 break;
847 case 32:
848 default:
849 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
850 break;
851 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 dev_priv->depth_offset = init->depth_offset;
853 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
855 /* Hardware state for depth clears. Remove this if/when we no
856 * longer clear the depth buffer with a 3D rectangle. Hard-code
857 * all values to prevent unwanted 3D state from slipping through
858 * and screwing with the clear operation.
859 */
860 dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
861 (dev_priv->color_fmt << 10) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000862 (dev_priv->microcode_version ==
863 UCODE_R100 ? RADEON_ZBLOCK16 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000865 dev_priv->depth_clear.rb3d_zstencilcntl =
866 (dev_priv->depth_fmt |
867 RADEON_Z_TEST_ALWAYS |
868 RADEON_STENCIL_TEST_ALWAYS |
869 RADEON_STENCIL_S_FAIL_REPLACE |
870 RADEON_STENCIL_ZPASS_REPLACE |
871 RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
874 RADEON_BFACE_SOLID |
875 RADEON_FFACE_SOLID |
876 RADEON_FLAT_SHADE_VTX_LAST |
877 RADEON_DIFFUSE_SHADE_FLAT |
878 RADEON_ALPHA_SHADE_FLAT |
879 RADEON_SPECULAR_SHADE_FLAT |
880 RADEON_FOG_SHADE_FLAT |
881 RADEON_VTX_PIX_CENTER_OGL |
882 RADEON_ROUND_MODE_TRUNC |
883 RADEON_ROUND_PREC_8TH_PIX);
884
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 dev_priv->ring_offset = init->ring_offset;
887 dev_priv->ring_rptr_offset = init->ring_rptr_offset;
888 dev_priv->buffers_offset = init->buffers_offset;
889 dev_priv->gart_textures_offset = init->gart_textures_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890
Dave Airlieda509d72007-05-26 05:04:51 +1000891 dev_priv->sarea = drm_getsarea(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892 if (!dev_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 DRM_ERROR("could not find sarea!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000895 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 dev_priv->cp_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 if (!dev_priv->cp_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 DRM_ERROR("could not find cp ring region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000902 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000905 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 DRM_ERROR("could not find ring read pointer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000908 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000910 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000912 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 DRM_ERROR("could not find dma buffer region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000915 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 if (init->gart_textures_offset) {
919 dev_priv->gart_textures =
920 drm_core_findmap(dev, init->gart_textures_offset);
921 if (!dev_priv->gart_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 DRM_ERROR("could not find GART texture region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000924 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 }
927
928 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000929 (drm_radeon_sarea_t *) ((u8 *) dev_priv->sarea->handle +
930 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000933 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000934 drm_core_ioremap(dev_priv->cp_ring, dev);
935 drm_core_ioremap(dev_priv->ring_rptr, dev);
936 drm_core_ioremap(dev->agp_buffer_map, dev);
937 if (!dev_priv->cp_ring->handle ||
938 !dev_priv->ring_rptr->handle ||
939 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 DRM_ERROR("could not find ioremap agp regions!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000942 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 } else
945#endif
946 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 dev_priv->cp_ring->handle = (void *)dev_priv->cp_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dev_priv->ring_rptr->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949 (void *)dev_priv->ring_rptr->offset;
950 dev->agp_buffer_map->handle =
951 (void *)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000953 DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
954 dev_priv->cp_ring->handle);
955 DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
956 dev_priv->ring_rptr->handle);
957 DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
958 dev->agp_buffer_map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 }
960
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000961 dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
Dave Airliebc5f4522007-11-05 12:50:58 +1000962 dev_priv->fb_size =
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000963 ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
Dave Airlied5ea7022006-03-19 19:37:55 +1100964 - dev_priv->fb_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000966 dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
967 ((dev_priv->front_offset
968 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000970 dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
971 ((dev_priv->back_offset
972 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
975 ((dev_priv->depth_offset
976 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
978 dev_priv->gart_size = init->gart_size;
Dave Airlied5ea7022006-03-19 19:37:55 +1100979
980 /* New let's set the memory map ... */
981 if (dev_priv->new_memmap) {
982 u32 base = 0;
983
984 DRM_INFO("Setting GART location based on new memory map\n");
985
986 /* If using AGP, try to locate the AGP aperture at the same
987 * location in the card and on the bus, though we have to
988 * align it down.
989 */
990#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000991 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +1100992 base = dev->agp->base;
993 /* Check if valid */
Michel Dänzer80b2c382007-02-18 18:03:21 +1100994 if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
995 base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
Dave Airlied5ea7022006-03-19 19:37:55 +1100996 DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
997 dev->agp->base);
998 base = 0;
999 }
1000 }
1001#endif
1002 /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
1003 if (base == 0) {
1004 base = dev_priv->fb_location + dev_priv->fb_size;
Michel Dänzer80b2c382007-02-18 18:03:21 +11001005 if (base < dev_priv->fb_location ||
1006 ((base + dev_priv->gart_size) & 0xfffffffful) < base)
Dave Airlied5ea7022006-03-19 19:37:55 +11001007 base = dev_priv->fb_location
1008 - dev_priv->gart_size;
Dave Airliebc5f4522007-11-05 12:50:58 +10001009 }
Dave Airlied5ea7022006-03-19 19:37:55 +11001010 dev_priv->gart_vm_start = base & 0xffc00000u;
1011 if (dev_priv->gart_vm_start != base)
1012 DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
1013 base, dev_priv->gart_vm_start);
1014 } else {
1015 DRM_INFO("Setting GART location based on old memory map\n");
1016 dev_priv->gart_vm_start = dev_priv->fb_location +
1017 RADEON_READ(RADEON_CONFIG_APER_SIZE);
1018 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019
1020#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001021 if (dev_priv->flags & RADEON_IS_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001023 - dev->agp->base
1024 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 else
1026#endif
1027 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +01001028 - (unsigned long)dev->sg->virtual
1029 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001031 DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
1032 DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
1033 DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
1034 dev_priv->gart_buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001036 dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
1037 dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 + init->ring_size / sizeof(u32));
1039 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Roland Scheidegger576cc452008-02-07 14:59:24 +10001042 dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
1043 dev_priv->ring.rptr_update_l2qw = drm_order( /* init->rptr_update */ 4096 / 8);
1044
1045 dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
1046 dev_priv->ring.fetch_size_l2ow = drm_order( /* init->fetch_size */ 32 / 16);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001047 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1050
1051#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001052 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001054 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 } else
1056#endif
1057 {
Dave Airlieb05c2382008-03-17 10:24:24 +10001058 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +10001059 /* if we have an offset set from userspace */
Dave Airlief2b04cd2007-05-08 15:19:23 +10001060 if (dev_priv->pcigart_offset_set) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001061 dev_priv->gart_info.bus_addr =
1062 dev_priv->pcigart_offset + dev_priv->fb_location;
Dave Airlief26c4732006-01-02 17:18:39 +11001063 dev_priv->gart_info.mapping.offset =
Dave Airlie7fc86862007-11-05 10:45:27 +10001064 dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
Dave Airlief26c4732006-01-02 17:18:39 +11001065 dev_priv->gart_info.mapping.size =
Dave Airlief2b04cd2007-05-08 15:19:23 +10001066 dev_priv->gart_info.table_size;
Dave Airlief26c4732006-01-02 17:18:39 +11001067
1068 drm_core_ioremap(&dev_priv->gart_info.mapping, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 dev_priv->gart_info.addr =
Dave Airlief26c4732006-01-02 17:18:39 +11001070 dev_priv->gart_info.mapping.handle;
Dave Airlieea98a922005-09-11 20:28:11 +10001071
Dave Airlief2b04cd2007-05-08 15:19:23 +10001072 if (dev_priv->flags & RADEON_IS_PCIE)
1073 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
1074 else
1075 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001076 dev_priv->gart_info.gart_table_location =
1077 DRM_ATI_GART_FB;
1078
Dave Airlief26c4732006-01-02 17:18:39 +11001079 DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001080 dev_priv->gart_info.addr,
1081 dev_priv->pcigart_offset);
1082 } else {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001083 if (dev_priv->flags & RADEON_IS_IGPGART)
1084 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
1085 else
1086 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001087 dev_priv->gart_info.gart_table_location =
1088 DRM_ATI_GART_MAIN;
Dave Airlief26c4732006-01-02 17:18:39 +11001089 dev_priv->gart_info.addr = NULL;
1090 dev_priv->gart_info.bus_addr = 0;
Dave Airlie54a56ac2006-09-22 04:25:09 +10001091 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001092 DRM_ERROR
1093 ("Cannot use PCI Express without GART in FB memory\n");
Dave Airlieea98a922005-09-11 20:28:11 +10001094 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001095 return -EINVAL;
Dave Airlieea98a922005-09-11 20:28:11 +10001096 }
1097 }
1098
1099 if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001100 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001102 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
1104
1105 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001106 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
1108
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001109 radeon_cp_load_microcode(dev_priv);
1110 radeon_cp_init_ring_buffer(dev, dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 dev_priv->last_buf = 0;
1113
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001114 radeon_do_engine_reset(dev);
Dave Airlied5ea7022006-03-19 19:37:55 +11001115 radeon_test_writeback(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 return 0;
1118}
1119
Dave Airlie84b1fd12007-07-11 15:53:27 +10001120static int radeon_do_cleanup_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121{
1122 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
1125 /* Make sure interrupts are disabled here because the uninstall ioctl
1126 * may not have been called from userspace and after dev_private
1127 * is freed, it's too late.
1128 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001129 if (dev->irq_enabled)
1130 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001133 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied985c102006-01-02 21:32:48 +11001134 if (dev_priv->cp_ring != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001135 drm_core_ioremapfree(dev_priv->cp_ring, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001136 dev_priv->cp_ring = NULL;
1137 }
1138 if (dev_priv->ring_rptr != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001139 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001140 dev_priv->ring_rptr = NULL;
1141 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142 if (dev->agp_buffer_map != NULL) {
1143 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 dev->agp_buffer_map = NULL;
1145 }
1146 } else
1147#endif
1148 {
Dave Airlied985c102006-01-02 21:32:48 +11001149
1150 if (dev_priv->gart_info.bus_addr) {
1151 /* Turn off PCI GART */
1152 radeon_set_pcigart(dev_priv, 0);
Dave Airlieea98a922005-09-11 20:28:11 +10001153 if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
1154 DRM_ERROR("failed to cleanup PCI GART!\n");
Dave Airlied985c102006-01-02 21:32:48 +11001155 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156
Dave Airlied985c102006-01-02 21:32:48 +11001157 if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
1158 {
Dave Airlief26c4732006-01-02 17:18:39 +11001159 drm_core_ioremapfree(&dev_priv->gart_info.mapping, dev);
Dave Airlief2b04cd2007-05-08 15:19:23 +10001160 dev_priv->gart_info.addr = 0;
Dave Airlieea98a922005-09-11 20:28:11 +10001161 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 /* only clear to the start of flags */
1164 memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
1165
1166 return 0;
1167}
1168
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001169/* This code will reinit the Radeon CP hardware after a resume from disc.
1170 * AFAIK, it would be very difficult to pickle the state at suspend time, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 * here we make sure that all Radeon hardware initialisation is re-done without
1172 * affecting running applications.
1173 *
1174 * Charl P. Botha <http://cpbotha.net>
1175 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001176static int radeon_do_resume_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 drm_radeon_private_t *dev_priv = dev->dev_private;
1179
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 if (!dev_priv) {
1181 DRM_ERROR("Called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001182 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
1184
1185 DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1186
1187#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001188 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001190 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 } else
1192#endif
1193 {
1194 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001195 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001198 radeon_cp_load_microcode(dev_priv);
1199 radeon_cp_init_ring_buffer(dev, dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 DRM_DEBUG("radeon_do_resume_cp() complete\n");
1204
1205 return 0;
1206}
1207
Eric Anholtc153f452007-09-03 12:06:45 +10001208int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
Eric Anholtc153f452007-09-03 12:06:45 +10001210 drm_radeon_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Eric Anholt6c340ea2007-08-25 20:23:09 +10001212 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Eric Anholtc153f452007-09-03 12:06:45 +10001214 if (init->func == RADEON_INIT_R300_CP)
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001215 r300_init_reg_flags(dev);
Dave Airlie414ed532005-08-16 20:43:16 +10001216
Eric Anholtc153f452007-09-03 12:06:45 +10001217 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 case RADEON_INIT_CP:
1219 case RADEON_INIT_R200_CP:
1220 case RADEON_INIT_R300_CP:
Eric Anholtc153f452007-09-03 12:06:45 +10001221 return radeon_do_init_cp(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 case RADEON_CLEANUP_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001223 return radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 }
1225
Eric Anholt20caafa2007-08-25 19:22:43 +10001226 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227}
1228
Eric Anholtc153f452007-09-03 12:06:45 +10001229int radeon_cp_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001232 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Eric Anholt6c340ea2007-08-25 20:23:09 +10001234 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001236 if (dev_priv->cp_running) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001237 DRM_DEBUG("while CP running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 return 0;
1239 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001240 if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001241 DRM_DEBUG("called with bogus CP mode (%d)\n",
1242 dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return 0;
1244 }
1245
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001246 radeon_do_cp_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 return 0;
1249}
1250
1251/* Stop the CP. The engine must have been idled before calling this
1252 * routine.
1253 */
Eric Anholtc153f452007-09-03 12:06:45 +10001254int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001257 drm_radeon_cp_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001259 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Eric Anholt6c340ea2007-08-25 20:23:09 +10001261 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (!dev_priv->cp_running)
1264 return 0;
1265
1266 /* Flush any pending CP commands. This ensures any outstanding
1267 * commands are exectuted by the engine before we turn it off.
1268 */
Eric Anholtc153f452007-09-03 12:06:45 +10001269 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001270 radeon_do_cp_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
1272
1273 /* If we fail to make the engine go idle, we return an error
1274 * code so that the DRM ioctl wrapper can try again.
1275 */
Eric Anholtc153f452007-09-03 12:06:45 +10001276 if (stop->idle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001277 ret = radeon_do_cp_idle(dev_priv);
1278 if (ret)
1279 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 }
1281
1282 /* Finally, we can turn off the CP. If the engine isn't idle,
1283 * we will get some dropped triangles as they won't be fully
1284 * rendered before the CP is shut down.
1285 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001286 radeon_do_cp_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 /* Reset the engine */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001289 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 return 0;
1292}
1293
Dave Airlie84b1fd12007-07-11 15:53:27 +10001294void radeon_do_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
1296 drm_radeon_private_t *dev_priv = dev->dev_private;
1297 int i, ret;
1298
1299 if (dev_priv) {
1300 if (dev_priv->cp_running) {
1301 /* Stop the cp */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001302 while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1304#ifdef __linux__
1305 schedule();
1306#else
1307 tsleep(&ret, PZERO, "rdnrel", 1);
1308#endif
1309 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001310 radeon_do_cp_stop(dev_priv);
1311 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
1314 /* Disable *all* interrupts */
1315 if (dev_priv->mmio) /* remove this after permanent addmaps */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001316 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001318 if (dev_priv->mmio) { /* remove all surfaces */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
1321 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
1322 16 * i, 0);
1323 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
1324 16 * i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326 }
1327
1328 /* Free memory heap structures */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001329 radeon_mem_takedown(&(dev_priv->gart_heap));
1330 radeon_mem_takedown(&(dev_priv->fb_heap));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332 /* deallocate kernel resources */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335}
1336
1337/* Just reset the CP ring. Called as part of an X Server engine reset.
1338 */
Eric Anholtc153f452007-09-03 12:06:45 +10001339int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001342 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Eric Anholt6c340ea2007-08-25 20:23:09 +10001344 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001346 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001347 DRM_DEBUG("called before init done\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001348 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001351 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
1353 /* The CP is no longer running after an engine reset */
1354 dev_priv->cp_running = 0;
1355
1356 return 0;
1357}
1358
Eric Anholtc153f452007-09-03 12:06:45 +10001359int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Eric Anholt6c340ea2007-08-25 20:23:09 +10001364 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001366 return radeon_do_cp_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
1369/* Added by Charl P. Botha to call radeon_do_resume_cp().
1370 */
Eric Anholtc153f452007-09-03 12:06:45 +10001371int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 return radeon_do_resume_cp(dev);
1375}
1376
Eric Anholtc153f452007-09-03 12:06:45 +10001377int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001379 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Eric Anholt6c340ea2007-08-25 20:23:09 +10001381 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001383 return radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386/* ================================================================
1387 * Fullscreen mode
1388 */
1389
1390/* KW: Deprecated to say the least:
1391 */
Eric Anholtc153f452007-09-03 12:06:45 +10001392int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393{
1394 return 0;
1395}
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397/* ================================================================
1398 * Freelist management
1399 */
1400
1401/* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1402 * bufs until freelist code is used. Note this hides a problem with
1403 * the scratch register * (used to keep track of last buffer
1404 * completed) being written to before * the last buffer has actually
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001405 * completed rendering.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 *
1407 * KW: It's also a good way to find free buffers quickly.
1408 *
1409 * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1410 * sleep. However, bugs in older versions of radeon_accel.c mean that
1411 * we essentially have to do this, else old clients will break.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001412 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 * However, it does leave open a potential deadlock where all the
1414 * buffers are held by other clients, which can't release them because
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001415 * they can't get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 */
1417
Dave Airlie056219e2007-07-11 16:17:42 +10001418struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419{
Dave Airliecdd55a22007-07-11 16:32:08 +10001420 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 drm_radeon_private_t *dev_priv = dev->dev_private;
1422 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001423 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 int i, t;
1425 int start;
1426
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001427 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 dev_priv->last_buf = 0;
1429
1430 start = dev_priv->last_buf;
1431
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001432 for (t = 0; t < dev_priv->usec_timeout; t++) {
1433 u32 done_age = GET_SCRATCH(1);
1434 DRM_DEBUG("done_age = %d\n", done_age);
1435 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 buf = dma->buflist[i];
1437 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001438 if (buf->file_priv == NULL || (buf->pending &&
1439 buf_priv->age <=
1440 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 dev_priv->stats.requested_bufs++;
1442 buf->pending = 0;
1443 return buf;
1444 }
1445 start = 0;
1446 }
1447
1448 if (t) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001449 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 dev_priv->stats.freelist_loops++;
1451 }
1452 }
1453
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001454 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 return NULL;
1456}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458#if 0
Dave Airlie056219e2007-07-11 16:17:42 +10001459struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Dave Airliecdd55a22007-07-11 16:32:08 +10001461 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 drm_radeon_private_t *dev_priv = dev->dev_private;
1463 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001464 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 int i, t;
1466 int start;
1467 u32 done_age = DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1));
1468
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001469 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 dev_priv->last_buf = 0;
1471
1472 start = dev_priv->last_buf;
1473 dev_priv->stats.freelist_loops++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001474
1475 for (t = 0; t < 2; t++) {
1476 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 buf = dma->buflist[i];
1478 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001479 if (buf->file_priv == 0 || (buf->pending &&
1480 buf_priv->age <=
1481 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 dev_priv->stats.requested_bufs++;
1483 buf->pending = 0;
1484 return buf;
1485 }
1486 }
1487 start = 0;
1488 }
1489
1490 return NULL;
1491}
1492#endif
1493
Dave Airlie84b1fd12007-07-11 15:53:27 +10001494void radeon_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495{
Dave Airliecdd55a22007-07-11 16:32:08 +10001496 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 drm_radeon_private_t *dev_priv = dev->dev_private;
1498 int i;
1499
1500 dev_priv->last_buf = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001501 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001502 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1504 buf_priv->age = 0;
1505 }
1506}
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508/* ================================================================
1509 * CP command submission
1510 */
1511
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001512int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1515 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001516 u32 last_head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001518 for (i = 0; i < dev_priv->usec_timeout; i++) {
1519 u32 head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 ring->space = (head - ring->tail) * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001522 if (ring->space <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 ring->space += ring->size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001524 if (ring->space > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
1528
1529 if (head != last_head)
1530 i = 0;
1531 last_head = head;
1532
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001533 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
1536 /* FIXME: This return value is ignored in the BEGIN_RING macro! */
1537#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001538 radeon_status(dev_priv);
1539 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540#endif
Eric Anholt20caafa2007-08-25 19:22:43 +10001541 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
Eric Anholt6c340ea2007-08-25 20:23:09 +10001544static int radeon_cp_get_buffers(struct drm_device *dev,
1545 struct drm_file *file_priv,
Dave Airliec60ce622007-07-11 15:27:12 +10001546 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001549 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551 for (i = d->granted_count; i < d->request_count; i++) {
1552 buf = radeon_freelist_get(dev);
1553 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001554 return -EBUSY; /* NOTE: broken client */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Eric Anholt6c340ea2007-08-25 20:23:09 +10001556 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001558 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
1559 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001560 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001561 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
1562 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001563 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564
1565 d->granted_count++;
1566 }
1567 return 0;
1568}
1569
Eric Anholtc153f452007-09-03 12:06:45 +10001570int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Dave Airliecdd55a22007-07-11 16:32:08 +10001572 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001574 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Eric Anholt6c340ea2007-08-25 20:23:09 +10001576 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 /* Please don't send us buffers.
1579 */
Eric Anholtc153f452007-09-03 12:06:45 +10001580 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001581 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001582 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001583 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
1585
1586 /* We'll send you buffers.
1587 */
Eric Anholtc153f452007-09-03 12:06:45 +10001588 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001589 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001590 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001591 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Eric Anholtc153f452007-09-03 12:06:45 +10001594 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Eric Anholtc153f452007-09-03 12:06:45 +10001596 if (d->request_count) {
1597 ret = radeon_cp_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 return ret;
1601}
1602
Dave Airlie22eae942005-11-10 22:16:34 +11001603int radeon_driver_load(struct drm_device *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 drm_radeon_private_t *dev_priv;
1606 int ret = 0;
1607
1608 dev_priv = drm_alloc(sizeof(drm_radeon_private_t), DRM_MEM_DRIVER);
1609 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10001610 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
1612 memset(dev_priv, 0, sizeof(drm_radeon_private_t));
1613 dev->dev_private = (void *)dev_priv;
1614 dev_priv->flags = flags;
1615
Dave Airlie54a56ac2006-09-22 04:25:09 +10001616 switch (flags & RADEON_FAMILY_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 case CHIP_R100:
1618 case CHIP_RV200:
1619 case CHIP_R200:
1620 case CHIP_R300:
Dave Airlieb15ec362006-08-19 17:43:52 +10001621 case CHIP_R350:
Dave Airlie414ed532005-08-16 20:43:16 +10001622 case CHIP_R420:
Dave Airlieb15ec362006-08-19 17:43:52 +10001623 case CHIP_RV410:
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001624 case CHIP_RV515:
1625 case CHIP_R520:
1626 case CHIP_RV570:
1627 case CHIP_R580:
Dave Airlie54a56ac2006-09-22 04:25:09 +10001628 dev_priv->flags |= RADEON_HAS_HIERZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 break;
1630 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001631 /* all other chips have no hierarchical z buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 break;
1633 }
Dave Airlie414ed532005-08-16 20:43:16 +10001634
1635 if (drm_device_is_agp(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001636 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlieb15ec362006-08-19 17:43:52 +10001637 else if (drm_device_is_pcie(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001638 dev_priv->flags |= RADEON_IS_PCIE;
Dave Airlieb15ec362006-08-19 17:43:52 +10001639 else
Dave Airlie54a56ac2006-09-22 04:25:09 +10001640 dev_priv->flags |= RADEON_IS_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +10001641
Dave Airlie414ed532005-08-16 20:43:16 +10001642 DRM_DEBUG("%s card detected\n",
Dave Airlie54a56ac2006-09-22 04:25:09 +10001643 ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return ret;
1645}
1646
Dave Airlie22eae942005-11-10 22:16:34 +11001647/* Create mappings for registers and framebuffer so userland doesn't necessarily
1648 * have to find them.
1649 */
1650int radeon_driver_firstopen(struct drm_device *dev)
Dave Airlie836cf042005-07-10 19:27:04 +10001651{
1652 int ret;
1653 drm_local_map_t *map;
1654 drm_radeon_private_t *dev_priv = dev->dev_private;
1655
Dave Airlief2b04cd2007-05-08 15:19:23 +10001656 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
1657
Dave Airlie836cf042005-07-10 19:27:04 +10001658 ret = drm_addmap(dev, drm_get_resource_start(dev, 2),
1659 drm_get_resource_len(dev, 2), _DRM_REGISTERS,
1660 _DRM_READ_ONLY, &dev_priv->mmio);
1661 if (ret != 0)
1662 return ret;
1663
Dave Airlie7fc86862007-11-05 10:45:27 +10001664 dev_priv->fb_aper_offset = drm_get_resource_start(dev, 0);
1665 ret = drm_addmap(dev, dev_priv->fb_aper_offset,
Dave Airlie836cf042005-07-10 19:27:04 +10001666 drm_get_resource_len(dev, 0), _DRM_FRAME_BUFFER,
1667 _DRM_WRITE_COMBINING, &map);
1668 if (ret != 0)
1669 return ret;
1670
1671 return 0;
1672}
1673
Dave Airlie22eae942005-11-10 22:16:34 +11001674int radeon_driver_unload(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
1676 drm_radeon_private_t *dev_priv = dev->dev_private;
1677
1678 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER);
1680
1681 dev->dev_private = NULL;
1682 return 0;
1683}