blob: fc0820c2b4b4b1dbcfa1d7d875204b8f26a92eef [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änzerb9b603d2006-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änzerb9b603d2006-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);
Alex Deucherd7463eb2008-05-28 11:46:36 +1000475 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200)
476 RADEON_WRITE(RADEON_AGP_BASE_2, 0);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000477 radeon_write_agp_location(dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000478 (((dev_priv->gart_vm_start - 1 +
479 dev_priv->gart_size) & 0xffff0000) |
480 (dev_priv->gart_vm_start >> 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 ring_start = (dev_priv->cp_ring->offset
483 - dev->agp->base
484 + dev_priv->gart_vm_start);
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100485 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486#endif
487 ring_start = (dev_priv->cp_ring->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100488 - (unsigned long)dev->sg->virtual
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 + dev_priv->gart_vm_start);
490
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000491 RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 /* Set the write pointer delay */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496 /* Initialize the ring buffer's read and write pointers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000497 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
498 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
499 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 dev_priv->ring.tail = cur_read_ptr;
501
502#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000503 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
505 dev_priv->ring_rptr->offset
506 - dev->agp->base + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 } else
508#endif
509 {
Dave Airlie55910512007-07-11 16:53:40 +1000510 struct drm_sg_mem *entry = dev->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 unsigned long tmp_ofs, page_ofs;
512
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100513 tmp_ofs = dev_priv->ring_rptr->offset -
514 (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 page_ofs = tmp_ofs >> PAGE_SHIFT;
516
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR, entry->busaddr[page_ofs]);
518 DRM_DEBUG("ring rptr: offset=0x%08lx handle=0x%08lx\n",
519 (unsigned long)entry->busaddr[page_ofs],
520 entry->handle + tmp_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
Dave Airlied5ea7022006-03-19 19:37:55 +1100523 /* Set ring buffer size */
524#ifdef __BIG_ENDIAN
525 RADEON_WRITE(RADEON_CP_RB_CNTL,
Roland Scheidegger576cc452008-02-07 14:59:24 +1000526 RADEON_BUF_SWAP_32BIT |
527 (dev_priv->ring.fetch_size_l2ow << 18) |
528 (dev_priv->ring.rptr_update_l2qw << 8) |
529 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100530#else
Roland Scheidegger576cc452008-02-07 14:59:24 +1000531 RADEON_WRITE(RADEON_CP_RB_CNTL,
532 (dev_priv->ring.fetch_size_l2ow << 18) |
533 (dev_priv->ring.rptr_update_l2qw << 8) |
534 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100535#endif
536
537 /* Start with assuming that writeback doesn't work */
538 dev_priv->writeback_works = 0;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 /* Initialize the scratch register pointer. This will cause
541 * the scratch register values to be written out to memory
542 * whenever they are updated.
543 *
544 * We simply put this behind the ring read pointer, this works
545 * with PCI GART as well as (whatever kind of) AGP GART
546 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
548 + RADEON_SCRATCH_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 dev_priv->scratch = ((__volatile__ u32 *)
551 dev_priv->ring_rptr->handle +
552 (RADEON_SCRATCH_REG_OFFSET / sizeof(u32)));
553
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Dave Airlied5ea7022006-03-19 19:37:55 +1100556 /* Turn on bus mastering */
557 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
558 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
559
560 dev_priv->sarea_priv->last_frame = dev_priv->scratch[0] = 0;
561 RADEON_WRITE(RADEON_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame);
562
563 dev_priv->sarea_priv->last_dispatch = dev_priv->scratch[1] = 0;
564 RADEON_WRITE(RADEON_LAST_DISPATCH_REG,
565 dev_priv->sarea_priv->last_dispatch);
566
567 dev_priv->sarea_priv->last_clear = dev_priv->scratch[2] = 0;
568 RADEON_WRITE(RADEON_LAST_CLEAR_REG, dev_priv->sarea_priv->last_clear);
569
570 radeon_do_wait_for_idle(dev_priv);
571
572 /* Sync everything up */
573 RADEON_WRITE(RADEON_ISYNC_CNTL,
574 (RADEON_ISYNC_ANY2D_IDLE3D |
575 RADEON_ISYNC_ANY3D_IDLE2D |
576 RADEON_ISYNC_WAIT_IDLEGUI |
577 RADEON_ISYNC_CPSCRATCH_IDLEGUI));
578
579}
580
581static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
582{
583 u32 tmp;
584
585 /* Writeback doesn't seem to work everywhere, test it here and possibly
586 * enable it if it appears to work
587 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000588 DRM_WRITE32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1), 0);
589 RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000591 for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
592 if (DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1)) ==
593 0xdeadbeef)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 }
597
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 if (tmp < dev_priv->usec_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 dev_priv->writeback_works = 1;
Dave Airlied5ea7022006-03-19 19:37:55 +1100600 DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 } else {
602 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100603 DRM_INFO("writeback test failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
Dave Airlie689b9d72005-09-30 17:09:07 +1000605 if (radeon_no_wb == 1) {
606 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100607 DRM_INFO("writeback forced off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
Michel Dänzerae1b1a482006-08-07 20:37:46 +1000609
610 if (!dev_priv->writeback_works) {
611 /* Disable writeback to avoid unnecessary bus master transfer */
612 RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
613 RADEON_RB_NO_UPDATE);
614 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Dave Airlief2b04cd2007-05-08 15:19:23 +1000618/* Enable or disable IGP GART on the chip */
619static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
620{
Maciej Cencora60f92682008-02-19 21:32:45 +1000621 u32 temp;
622
623 if (on) {
Alex Deucher45e51902008-05-28 13:28:59 +1000624 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
Maciej Cencora60f92682008-02-19 21:32:45 +1000625 dev_priv->gart_vm_start,
626 (long)dev_priv->gart_info.bus_addr,
627 dev_priv->gart_size);
628
Alex Deucher45e51902008-05-28 13:28:59 +1000629 temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
630 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690)
631 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
632 RS690_BLOCK_GFX_D3_EN));
633 else
634 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
Maciej Cencora60f92682008-02-19 21:32:45 +1000635
Alex Deucher45e51902008-05-28 13:28:59 +1000636 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
637 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000638
Alex Deucher45e51902008-05-28 13:28:59 +1000639 temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
640 IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
641 RS480_TLB_ENABLE |
642 RS480_GTW_LAC_EN |
643 RS480_1LEVEL_GART));
Maciej Cencora60f92682008-02-19 21:32:45 +1000644
Dave Airliefa0d71b2008-05-28 11:27:01 +1000645 temp = dev_priv->gart_info.bus_addr & 0xfffff000;
646 temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
Alex Deucher45e51902008-05-28 13:28:59 +1000647 IGP_WRITE_MCIND(RS480_GART_BASE, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000648
Alex Deucher45e51902008-05-28 13:28:59 +1000649 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
650 IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
651 RS480_REQ_TYPE_SNOOP_DIS));
Maciej Cencora60f92682008-02-19 21:32:45 +1000652
Alex Deucher45e51902008-05-28 13:28:59 +1000653 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) {
654 IGP_WRITE_MCIND(RS690_MC_AGP_BASE,
655 (unsigned int)dev_priv->gart_vm_start);
656 IGP_WRITE_MCIND(RS690_MC_AGP_BASE_2, 0);
657 } else {
658 RADEON_WRITE(RADEON_AGP_BASE, (unsigned int)dev_priv->gart_vm_start);
659 RADEON_WRITE(RS480_AGP_BASE_2, 0);
660 }
Dave Airlie3722bfc2008-05-28 11:28:27 +1000661
Maciej Cencora60f92682008-02-19 21:32:45 +1000662 dev_priv->gart_size = 32*1024*1024;
663 temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
664 0xffff0000) | (dev_priv->gart_vm_start >> 16));
665
Alex Deucher45e51902008-05-28 13:28:59 +1000666 radeon_write_agp_location(dev_priv, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000667
Alex Deucher45e51902008-05-28 13:28:59 +1000668 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
669 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
670 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000671
672 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000673 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
674 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000675 break;
676 DRM_UDELAY(1);
677 } while (1);
678
Alex Deucher45e51902008-05-28 13:28:59 +1000679 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
680 RS480_GART_CACHE_INVALIDATE);
Alex Deucher27359772008-05-28 12:54:16 +1000681
Maciej Cencora60f92682008-02-19 21:32:45 +1000682 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000683 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
684 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000685 break;
686 DRM_UDELAY(1);
687 } while (1);
688
Alex Deucher45e51902008-05-28 13:28:59 +1000689 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000690 } else {
Alex Deucher45e51902008-05-28 13:28:59 +1000691 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000692 }
693}
694
Dave Airlieea98a922005-09-11 20:28:11 +1000695static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Dave Airlieea98a922005-09-11 20:28:11 +1000697 u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
698 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Dave Airlieea98a922005-09-11 20:28:11 +1000700 DRM_DEBUG("programming pcie %08X %08lX %08X\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 dev_priv->gart_vm_start,
702 (long)dev_priv->gart_info.bus_addr,
Dave Airlieea98a922005-09-11 20:28:11 +1000703 dev_priv->gart_size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
705 dev_priv->gart_vm_start);
706 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
707 dev_priv->gart_info.bus_addr);
708 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
709 dev_priv->gart_vm_start);
710 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
711 dev_priv->gart_vm_start +
712 dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000714 radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000716 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
717 RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000719 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
720 tmp & ~RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
722}
723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724/* Enable or disable PCI GART on the chip */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000725static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Dave Airlied985c102006-01-02 21:32:48 +1100727 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Alex Deucher45e51902008-05-28 13:28:59 +1000729 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
730 (dev_priv->flags & RADEON_IS_IGPGART)) {
Dave Airlief2b04cd2007-05-08 15:19:23 +1000731 radeon_set_igpgart(dev_priv, on);
732 return;
733 }
734
Dave Airlie54a56ac2006-09-22 04:25:09 +1000735 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieea98a922005-09-11 20:28:11 +1000736 radeon_set_pciegart(dev_priv, on);
737 return;
738 }
739
Dave Airliebc5f4522007-11-05 12:50:58 +1000740 tmp = RADEON_READ(RADEON_AIC_CNTL);
Dave Airlied985c102006-01-02 21:32:48 +1100741
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 if (on) {
743 RADEON_WRITE(RADEON_AIC_CNTL,
744 tmp | RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /* set PCI GART page-table base address
747 */
Dave Airlieea98a922005-09-11 20:28:11 +1000748 RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 /* set address range for PCI address translate
751 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000752 RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
753 RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
754 + dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 /* Turn off AGP aperture -- is this required for PCI GART?
757 */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000758 radeon_write_agp_location(dev_priv, 0xffffffc0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000759 RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000761 RADEON_WRITE(RADEON_AIC_CNTL,
762 tmp & ~RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764}
765
Dave Airlie84b1fd12007-07-11 15:53:27 +1000766static int radeon_do_init_cp(struct drm_device * dev, drm_radeon_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Dave Airlied985c102006-01-02 21:32:48 +1100768 drm_radeon_private_t *dev_priv = dev->dev_private;
769
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000770 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Dave Airlief3dd5c32006-03-25 18:09:46 +1100772 /* if we require new memory map but we don't have it fail */
Dave Airlie54a56ac2006-09-22 04:25:09 +1000773 if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
Dave Airlieb15ec362006-08-19 17:43:52 +1000774 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 +1100775 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000776 return -EINVAL;
Dave Airlief3dd5c32006-03-25 18:09:46 +1100777 }
778
Dave Airlie54a56ac2006-09-22 04:25:09 +1000779 if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
Dave Airlied985c102006-01-02 21:32:48 +1100780 DRM_DEBUG("Forcing AGP card to PCI mode\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000781 dev_priv->flags &= ~RADEON_IS_AGP;
782 } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
Dave Airlieb15ec362006-08-19 17:43:52 +1000783 && !init->is_pci) {
784 DRM_DEBUG("Restoring AGP flag\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000785 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlied985c102006-01-02 21:32:48 +1100786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Dave Airlie54a56ac2006-09-22 04:25:09 +1000788 if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000789 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000791 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
794 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000795 if (dev_priv->usec_timeout < 1 ||
796 dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
797 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000799 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Dave Airlieddbee332007-07-11 12:16:01 +1000802 /* Enable vblank on CRTC1 for older X servers
803 */
804 dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
805
Dave Airlied985c102006-01-02 21:32:48 +1100806 switch(init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 case RADEON_INIT_R200_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000808 dev_priv->microcode_version = UCODE_R200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 break;
810 case RADEON_INIT_R300_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000811 dev_priv->microcode_version = UCODE_R300;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
813 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000814 dev_priv->microcode_version = UCODE_R100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 dev_priv->do_boxes = 0;
818 dev_priv->cp_mode = init->cp_mode;
819
820 /* We don't support anything other than bus-mastering ring mode,
821 * but the ring can be in either AGP or PCI space for the ring
822 * read pointer.
823 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
825 (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
826 DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000828 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
830
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 case 16:
833 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
834 break;
835 case 32:
836 default:
837 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
838 break;
839 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 dev_priv->front_offset = init->front_offset;
841 dev_priv->front_pitch = init->front_pitch;
842 dev_priv->back_offset = init->back_offset;
843 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 case 16:
847 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
848 break;
849 case 32:
850 default:
851 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
852 break;
853 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 dev_priv->depth_offset = init->depth_offset;
855 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 /* Hardware state for depth clears. Remove this if/when we no
858 * longer clear the depth buffer with a 3D rectangle. Hard-code
859 * all values to prevent unwanted 3D state from slipping through
860 * and screwing with the clear operation.
861 */
862 dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
863 (dev_priv->color_fmt << 10) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000864 (dev_priv->microcode_version ==
865 UCODE_R100 ? RADEON_ZBLOCK16 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000867 dev_priv->depth_clear.rb3d_zstencilcntl =
868 (dev_priv->depth_fmt |
869 RADEON_Z_TEST_ALWAYS |
870 RADEON_STENCIL_TEST_ALWAYS |
871 RADEON_STENCIL_S_FAIL_REPLACE |
872 RADEON_STENCIL_ZPASS_REPLACE |
873 RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
876 RADEON_BFACE_SOLID |
877 RADEON_FFACE_SOLID |
878 RADEON_FLAT_SHADE_VTX_LAST |
879 RADEON_DIFFUSE_SHADE_FLAT |
880 RADEON_ALPHA_SHADE_FLAT |
881 RADEON_SPECULAR_SHADE_FLAT |
882 RADEON_FOG_SHADE_FLAT |
883 RADEON_VTX_PIX_CENTER_OGL |
884 RADEON_ROUND_MODE_TRUNC |
885 RADEON_ROUND_PREC_8TH_PIX);
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 dev_priv->ring_offset = init->ring_offset;
889 dev_priv->ring_rptr_offset = init->ring_rptr_offset;
890 dev_priv->buffers_offset = init->buffers_offset;
891 dev_priv->gart_textures_offset = init->gart_textures_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000892
Dave Airlieda509d72007-05-26 05:04:51 +1000893 dev_priv->sarea = drm_getsarea(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000894 if (!dev_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 DRM_ERROR("could not find sarea!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000897 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 }
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 dev_priv->cp_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000901 if (!dev_priv->cp_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 DRM_ERROR("could not find cp ring region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000904 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 }
906 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000907 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 DRM_ERROR("could not find ring read pointer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000910 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000912 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000914 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 DRM_ERROR("could not find dma buffer region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000917 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 }
919
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 if (init->gart_textures_offset) {
921 dev_priv->gart_textures =
922 drm_core_findmap(dev, init->gart_textures_offset);
923 if (!dev_priv->gart_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 DRM_ERROR("could not find GART texture region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000926 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
928 }
929
930 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 (drm_radeon_sarea_t *) ((u8 *) dev_priv->sarea->handle +
932 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000935 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936 drm_core_ioremap(dev_priv->cp_ring, dev);
937 drm_core_ioremap(dev_priv->ring_rptr, dev);
938 drm_core_ioremap(dev->agp_buffer_map, dev);
939 if (!dev_priv->cp_ring->handle ||
940 !dev_priv->ring_rptr->handle ||
941 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 DRM_ERROR("could not find ioremap agp regions!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000944 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946 } else
947#endif
948 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000949 dev_priv->cp_ring->handle = (void *)dev_priv->cp_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 dev_priv->ring_rptr->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 (void *)dev_priv->ring_rptr->offset;
952 dev->agp_buffer_map->handle =
953 (void *)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000955 DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
956 dev_priv->cp_ring->handle);
957 DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
958 dev_priv->ring_rptr->handle);
959 DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
960 dev->agp_buffer_map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000963 dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
Dave Airliebc5f4522007-11-05 12:50:58 +1000964 dev_priv->fb_size =
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000965 ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
Dave Airlied5ea7022006-03-19 19:37:55 +1100966 - dev_priv->fb_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000968 dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
969 ((dev_priv->front_offset
970 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000972 dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
973 ((dev_priv->back_offset
974 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000976 dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
977 ((dev_priv->depth_offset
978 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 dev_priv->gart_size = init->gart_size;
Dave Airlied5ea7022006-03-19 19:37:55 +1100981
982 /* New let's set the memory map ... */
983 if (dev_priv->new_memmap) {
984 u32 base = 0;
985
986 DRM_INFO("Setting GART location based on new memory map\n");
987
988 /* If using AGP, try to locate the AGP aperture at the same
989 * location in the card and on the bus, though we have to
990 * align it down.
991 */
992#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000993 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +1100994 base = dev->agp->base;
995 /* Check if valid */
Michel Dänzer80b2c382007-02-18 18:03:21 +1100996 if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
997 base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
Dave Airlied5ea7022006-03-19 19:37:55 +1100998 DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
999 dev->agp->base);
1000 base = 0;
1001 }
1002 }
1003#endif
1004 /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
1005 if (base == 0) {
1006 base = dev_priv->fb_location + dev_priv->fb_size;
Michel Dänzer80b2c382007-02-18 18:03:21 +11001007 if (base < dev_priv->fb_location ||
1008 ((base + dev_priv->gart_size) & 0xfffffffful) < base)
Dave Airlied5ea7022006-03-19 19:37:55 +11001009 base = dev_priv->fb_location
1010 - dev_priv->gart_size;
Dave Airliebc5f4522007-11-05 12:50:58 +10001011 }
Dave Airlied5ea7022006-03-19 19:37:55 +11001012 dev_priv->gart_vm_start = base & 0xffc00000u;
1013 if (dev_priv->gart_vm_start != base)
1014 DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
1015 base, dev_priv->gart_vm_start);
1016 } else {
1017 DRM_INFO("Setting GART location based on old memory map\n");
1018 dev_priv->gart_vm_start = dev_priv->fb_location +
1019 RADEON_READ(RADEON_CONFIG_APER_SIZE);
1020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001023 if (dev_priv->flags & RADEON_IS_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001025 - dev->agp->base
1026 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 else
1028#endif
1029 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +01001030 - (unsigned long)dev->sg->virtual
1031 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001033 DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
1034 DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
1035 DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
1036 dev_priv->gart_buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
1039 dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 + init->ring_size / sizeof(u32));
1041 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001042 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Roland Scheidegger576cc452008-02-07 14:59:24 +10001044 dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
1045 dev_priv->ring.rptr_update_l2qw = drm_order( /* init->rptr_update */ 4096 / 8);
1046
1047 dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
1048 dev_priv->ring.fetch_size_l2ow = drm_order( /* init->fetch_size */ 32 / 16);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001049 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
1051 dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1052
1053#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001054 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 } else
1058#endif
1059 {
Dave Airlieb05c2382008-03-17 10:24:24 +10001060 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +10001061 /* if we have an offset set from userspace */
Dave Airlief2b04cd2007-05-08 15:19:23 +10001062 if (dev_priv->pcigart_offset_set) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001063 dev_priv->gart_info.bus_addr =
1064 dev_priv->pcigart_offset + dev_priv->fb_location;
Dave Airlief26c4732006-01-02 17:18:39 +11001065 dev_priv->gart_info.mapping.offset =
Dave Airlie7fc86862007-11-05 10:45:27 +10001066 dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
Dave Airlief26c4732006-01-02 17:18:39 +11001067 dev_priv->gart_info.mapping.size =
Dave Airlief2b04cd2007-05-08 15:19:23 +10001068 dev_priv->gart_info.table_size;
Dave Airlief26c4732006-01-02 17:18:39 +11001069
1070 drm_core_ioremap(&dev_priv->gart_info.mapping, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001071 dev_priv->gart_info.addr =
Dave Airlief26c4732006-01-02 17:18:39 +11001072 dev_priv->gart_info.mapping.handle;
Dave Airlieea98a922005-09-11 20:28:11 +10001073
Dave Airlief2b04cd2007-05-08 15:19:23 +10001074 if (dev_priv->flags & RADEON_IS_PCIE)
1075 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
1076 else
1077 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001078 dev_priv->gart_info.gart_table_location =
1079 DRM_ATI_GART_FB;
1080
Dave Airlief26c4732006-01-02 17:18:39 +11001081 DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001082 dev_priv->gart_info.addr,
1083 dev_priv->pcigart_offset);
1084 } else {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001085 if (dev_priv->flags & RADEON_IS_IGPGART)
1086 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
1087 else
1088 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 dev_priv->gart_info.gart_table_location =
1090 DRM_ATI_GART_MAIN;
Dave Airlief26c4732006-01-02 17:18:39 +11001091 dev_priv->gart_info.addr = NULL;
1092 dev_priv->gart_info.bus_addr = 0;
Dave Airlie54a56ac2006-09-22 04:25:09 +10001093 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001094 DRM_ERROR
1095 ("Cannot use PCI Express without GART in FB memory\n");
Dave Airlieea98a922005-09-11 20:28:11 +10001096 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001097 return -EINVAL;
Dave Airlieea98a922005-09-11 20:28:11 +10001098 }
1099 }
1100
1101 if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001102 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001104 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
1107 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001108 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001111 radeon_cp_load_microcode(dev_priv);
1112 radeon_cp_init_ring_buffer(dev, dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 dev_priv->last_buf = 0;
1115
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001116 radeon_do_engine_reset(dev);
Dave Airlied5ea7022006-03-19 19:37:55 +11001117 radeon_test_writeback(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
1119 return 0;
1120}
1121
Dave Airlie84b1fd12007-07-11 15:53:27 +10001122static int radeon_do_cleanup_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001125 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /* Make sure interrupts are disabled here because the uninstall ioctl
1128 * may not have been called from userspace and after dev_private
1129 * is freed, it's too late.
1130 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001131 if (dev->irq_enabled)
1132 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001135 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied985c102006-01-02 21:32:48 +11001136 if (dev_priv->cp_ring != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001137 drm_core_ioremapfree(dev_priv->cp_ring, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001138 dev_priv->cp_ring = NULL;
1139 }
1140 if (dev_priv->ring_rptr != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001141 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001142 dev_priv->ring_rptr = NULL;
1143 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001144 if (dev->agp_buffer_map != NULL) {
1145 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 dev->agp_buffer_map = NULL;
1147 }
1148 } else
1149#endif
1150 {
Dave Airlied985c102006-01-02 21:32:48 +11001151
1152 if (dev_priv->gart_info.bus_addr) {
1153 /* Turn off PCI GART */
1154 radeon_set_pcigart(dev_priv, 0);
Dave Airlieea98a922005-09-11 20:28:11 +10001155 if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
1156 DRM_ERROR("failed to cleanup PCI GART!\n");
Dave Airlied985c102006-01-02 21:32:48 +11001157 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001158
Dave Airlied985c102006-01-02 21:32:48 +11001159 if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
1160 {
Dave Airlief26c4732006-01-02 17:18:39 +11001161 drm_core_ioremapfree(&dev_priv->gart_info.mapping, dev);
Dave Airlief2b04cd2007-05-08 15:19:23 +10001162 dev_priv->gart_info.addr = 0;
Dave Airlieea98a922005-09-11 20:28:11 +10001163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 /* only clear to the start of flags */
1166 memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
1167
1168 return 0;
1169}
1170
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001171/* This code will reinit the Radeon CP hardware after a resume from disc.
1172 * AFAIK, it would be very difficult to pickle the state at suspend time, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 * here we make sure that all Radeon hardware initialisation is re-done without
1174 * affecting running applications.
1175 *
1176 * Charl P. Botha <http://cpbotha.net>
1177 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001178static int radeon_do_resume_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
1180 drm_radeon_private_t *dev_priv = dev->dev_private;
1181
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001182 if (!dev_priv) {
1183 DRM_ERROR("Called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001184 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
1187 DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1188
1189#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001190 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001192 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 } else
1194#endif
1195 {
1196 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001197 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001200 radeon_cp_load_microcode(dev_priv);
1201 radeon_cp_init_ring_buffer(dev, dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001203 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
1205 DRM_DEBUG("radeon_do_resume_cp() complete\n");
1206
1207 return 0;
1208}
1209
Eric Anholtc153f452007-09-03 12:06:45 +10001210int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211{
Eric Anholtc153f452007-09-03 12:06:45 +10001212 drm_radeon_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Eric Anholt6c340ea2007-08-25 20:23:09 +10001214 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Eric Anholtc153f452007-09-03 12:06:45 +10001216 if (init->func == RADEON_INIT_R300_CP)
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001217 r300_init_reg_flags(dev);
Dave Airlie414ed532005-08-16 20:43:16 +10001218
Eric Anholtc153f452007-09-03 12:06:45 +10001219 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case RADEON_INIT_CP:
1221 case RADEON_INIT_R200_CP:
1222 case RADEON_INIT_R300_CP:
Eric Anholtc153f452007-09-03 12:06:45 +10001223 return radeon_do_init_cp(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 case RADEON_CLEANUP_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001225 return radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Eric Anholt20caafa2007-08-25 19:22:43 +10001228 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Eric Anholtc153f452007-09-03 12:06:45 +10001231int radeon_cp_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001234 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Eric Anholt6c340ea2007-08-25 20:23:09 +10001236 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001238 if (dev_priv->cp_running) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001239 DRM_DEBUG("while CP running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return 0;
1241 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001242 if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001243 DRM_DEBUG("called with bogus CP mode (%d)\n",
1244 dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 0;
1246 }
1247
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001248 radeon_do_cp_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 return 0;
1251}
1252
1253/* Stop the CP. The engine must have been idled before calling this
1254 * routine.
1255 */
Eric Anholtc153f452007-09-03 12:06:45 +10001256int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001259 drm_radeon_cp_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001261 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Eric Anholt6c340ea2007-08-25 20:23:09 +10001263 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (!dev_priv->cp_running)
1266 return 0;
1267
1268 /* Flush any pending CP commands. This ensures any outstanding
1269 * commands are exectuted by the engine before we turn it off.
1270 */
Eric Anholtc153f452007-09-03 12:06:45 +10001271 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001272 radeon_do_cp_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
1274
1275 /* If we fail to make the engine go idle, we return an error
1276 * code so that the DRM ioctl wrapper can try again.
1277 */
Eric Anholtc153f452007-09-03 12:06:45 +10001278 if (stop->idle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001279 ret = radeon_do_cp_idle(dev_priv);
1280 if (ret)
1281 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
1284 /* Finally, we can turn off the CP. If the engine isn't idle,
1285 * we will get some dropped triangles as they won't be fully
1286 * rendered before the CP is shut down.
1287 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001288 radeon_do_cp_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289
1290 /* Reset the engine */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001291 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
1293 return 0;
1294}
1295
Dave Airlie84b1fd12007-07-11 15:53:27 +10001296void radeon_do_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297{
1298 drm_radeon_private_t *dev_priv = dev->dev_private;
1299 int i, ret;
1300
1301 if (dev_priv) {
1302 if (dev_priv->cp_running) {
1303 /* Stop the cp */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001304 while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1306#ifdef __linux__
1307 schedule();
1308#else
1309 tsleep(&ret, PZERO, "rdnrel", 1);
1310#endif
1311 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001312 radeon_do_cp_stop(dev_priv);
1313 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315
1316 /* Disable *all* interrupts */
1317 if (dev_priv->mmio) /* remove this after permanent addmaps */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001318 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 if (dev_priv->mmio) { /* remove all surfaces */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001322 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
1323 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
1324 16 * i, 0);
1325 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
1326 16 * i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328 }
1329
1330 /* Free memory heap structures */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001331 radeon_mem_takedown(&(dev_priv->gart_heap));
1332 radeon_mem_takedown(&(dev_priv->fb_heap));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* deallocate kernel resources */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001335 radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337}
1338
1339/* Just reset the CP ring. Called as part of an X Server engine reset.
1340 */
Eric Anholtc153f452007-09-03 12:06:45 +10001341int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001344 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Eric Anholt6c340ea2007-08-25 20:23:09 +10001346 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001348 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001349 DRM_DEBUG("called before init done\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001350 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 }
1352
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001353 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /* The CP is no longer running after an engine reset */
1356 dev_priv->cp_running = 0;
1357
1358 return 0;
1359}
1360
Eric Anholtc153f452007-09-03 12:06:45 +10001361int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001364 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Eric Anholt6c340ea2007-08-25 20:23:09 +10001366 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001368 return radeon_do_cp_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
1371/* Added by Charl P. Botha to call radeon_do_resume_cp().
1372 */
Eric Anholtc153f452007-09-03 12:06:45 +10001373int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 return radeon_do_resume_cp(dev);
1377}
1378
Eric Anholtc153f452007-09-03 12:06:45 +10001379int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001381 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Eric Anholt6c340ea2007-08-25 20:23:09 +10001383 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001385 return radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388/* ================================================================
1389 * Fullscreen mode
1390 */
1391
1392/* KW: Deprecated to say the least:
1393 */
Eric Anholtc153f452007-09-03 12:06:45 +10001394int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395{
1396 return 0;
1397}
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399/* ================================================================
1400 * Freelist management
1401 */
1402
1403/* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1404 * bufs until freelist code is used. Note this hides a problem with
1405 * the scratch register * (used to keep track of last buffer
1406 * completed) being written to before * the last buffer has actually
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001407 * completed rendering.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 *
1409 * KW: It's also a good way to find free buffers quickly.
1410 *
1411 * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1412 * sleep. However, bugs in older versions of radeon_accel.c mean that
1413 * we essentially have to do this, else old clients will break.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001414 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 * However, it does leave open a potential deadlock where all the
1416 * buffers are held by other clients, which can't release them because
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001417 * they can't get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 */
1419
Dave Airlie056219e2007-07-11 16:17:42 +10001420struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421{
Dave Airliecdd55a22007-07-11 16:32:08 +10001422 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 drm_radeon_private_t *dev_priv = dev->dev_private;
1424 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001425 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 int i, t;
1427 int start;
1428
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001429 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 dev_priv->last_buf = 0;
1431
1432 start = dev_priv->last_buf;
1433
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001434 for (t = 0; t < dev_priv->usec_timeout; t++) {
1435 u32 done_age = GET_SCRATCH(1);
1436 DRM_DEBUG("done_age = %d\n", done_age);
1437 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 buf = dma->buflist[i];
1439 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001440 if (buf->file_priv == NULL || (buf->pending &&
1441 buf_priv->age <=
1442 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 dev_priv->stats.requested_bufs++;
1444 buf->pending = 0;
1445 return buf;
1446 }
1447 start = 0;
1448 }
1449
1450 if (t) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001451 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 dev_priv->stats.freelist_loops++;
1453 }
1454 }
1455
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001456 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 return NULL;
1458}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001459
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460#if 0
Dave Airlie056219e2007-07-11 16:17:42 +10001461struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
Dave Airliecdd55a22007-07-11 16:32:08 +10001463 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 drm_radeon_private_t *dev_priv = dev->dev_private;
1465 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001466 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 int i, t;
1468 int start;
1469 u32 done_age = DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1));
1470
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001471 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 dev_priv->last_buf = 0;
1473
1474 start = dev_priv->last_buf;
1475 dev_priv->stats.freelist_loops++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001476
1477 for (t = 0; t < 2; t++) {
1478 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 buf = dma->buflist[i];
1480 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001481 if (buf->file_priv == 0 || (buf->pending &&
1482 buf_priv->age <=
1483 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 dev_priv->stats.requested_bufs++;
1485 buf->pending = 0;
1486 return buf;
1487 }
1488 }
1489 start = 0;
1490 }
1491
1492 return NULL;
1493}
1494#endif
1495
Dave Airlie84b1fd12007-07-11 15:53:27 +10001496void radeon_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Dave Airliecdd55a22007-07-11 16:32:08 +10001498 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 drm_radeon_private_t *dev_priv = dev->dev_private;
1500 int i;
1501
1502 dev_priv->last_buf = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001503 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001504 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1506 buf_priv->age = 0;
1507 }
1508}
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510/* ================================================================
1511 * CP command submission
1512 */
1513
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001514int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1517 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001518 u32 last_head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001520 for (i = 0; i < dev_priv->usec_timeout; i++) {
1521 u32 head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
1523 ring->space = (head - ring->tail) * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001524 if (ring->space <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 ring->space += ring->size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001526 if (ring->space > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
1530
1531 if (head != last_head)
1532 i = 0;
1533 last_head = head;
1534
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001535 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537
1538 /* FIXME: This return value is ignored in the BEGIN_RING macro! */
1539#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 radeon_status(dev_priv);
1541 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542#endif
Eric Anholt20caafa2007-08-25 19:22:43 +10001543 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544}
1545
Eric Anholt6c340ea2007-08-25 20:23:09 +10001546static int radeon_cp_get_buffers(struct drm_device *dev,
1547 struct drm_file *file_priv,
Dave Airliec60ce622007-07-11 15:27:12 +10001548 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001551 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001553 for (i = d->granted_count; i < d->request_count; i++) {
1554 buf = radeon_freelist_get(dev);
1555 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001556 return -EBUSY; /* NOTE: broken client */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Eric Anholt6c340ea2007-08-25 20:23:09 +10001558 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001560 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
1561 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001562 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001563 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
1564 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001565 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 d->granted_count++;
1568 }
1569 return 0;
1570}
1571
Eric Anholtc153f452007-09-03 12:06:45 +10001572int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
Dave Airliecdd55a22007-07-11 16:32:08 +10001574 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001576 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Eric Anholt6c340ea2007-08-25 20:23:09 +10001578 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 /* Please don't send us buffers.
1581 */
Eric Anholtc153f452007-09-03 12:06:45 +10001582 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001583 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001584 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001585 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
1588 /* We'll send you buffers.
1589 */
Eric Anholtc153f452007-09-03 12:06:45 +10001590 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001591 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001592 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001593 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595
Eric Anholtc153f452007-09-03 12:06:45 +10001596 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Eric Anholtc153f452007-09-03 12:06:45 +10001598 if (d->request_count) {
1599 ret = radeon_cp_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return ret;
1603}
1604
Dave Airlie22eae942005-11-10 22:16:34 +11001605int radeon_driver_load(struct drm_device *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
1607 drm_radeon_private_t *dev_priv;
1608 int ret = 0;
1609
1610 dev_priv = drm_alloc(sizeof(drm_radeon_private_t), DRM_MEM_DRIVER);
1611 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10001612 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 memset(dev_priv, 0, sizeof(drm_radeon_private_t));
1615 dev->dev_private = (void *)dev_priv;
1616 dev_priv->flags = flags;
1617
Dave Airlie54a56ac2006-09-22 04:25:09 +10001618 switch (flags & RADEON_FAMILY_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 case CHIP_R100:
1620 case CHIP_RV200:
1621 case CHIP_R200:
1622 case CHIP_R300:
Dave Airlieb15ec362006-08-19 17:43:52 +10001623 case CHIP_R350:
Dave Airlie414ed532005-08-16 20:43:16 +10001624 case CHIP_R420:
Dave Airlieb15ec362006-08-19 17:43:52 +10001625 case CHIP_RV410:
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001626 case CHIP_RV515:
1627 case CHIP_R520:
1628 case CHIP_RV570:
1629 case CHIP_R580:
Dave Airlie54a56ac2006-09-22 04:25:09 +10001630 dev_priv->flags |= RADEON_HAS_HIERZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 break;
1632 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001633 /* all other chips have no hierarchical z buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 break;
1635 }
Dave Airlie414ed532005-08-16 20:43:16 +10001636
1637 if (drm_device_is_agp(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001638 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlieb15ec362006-08-19 17:43:52 +10001639 else if (drm_device_is_pcie(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001640 dev_priv->flags |= RADEON_IS_PCIE;
Dave Airlieb15ec362006-08-19 17:43:52 +10001641 else
Dave Airlie54a56ac2006-09-22 04:25:09 +10001642 dev_priv->flags |= RADEON_IS_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +10001643
Dave Airlie414ed532005-08-16 20:43:16 +10001644 DRM_DEBUG("%s card detected\n",
Dave Airlie54a56ac2006-09-22 04:25:09 +10001645 ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return ret;
1647}
1648
Dave Airlie22eae942005-11-10 22:16:34 +11001649/* Create mappings for registers and framebuffer so userland doesn't necessarily
1650 * have to find them.
1651 */
1652int radeon_driver_firstopen(struct drm_device *dev)
Dave Airlie836cf042005-07-10 19:27:04 +10001653{
1654 int ret;
1655 drm_local_map_t *map;
1656 drm_radeon_private_t *dev_priv = dev->dev_private;
1657
Dave Airlief2b04cd2007-05-08 15:19:23 +10001658 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
1659
Dave Airlie836cf042005-07-10 19:27:04 +10001660 ret = drm_addmap(dev, drm_get_resource_start(dev, 2),
1661 drm_get_resource_len(dev, 2), _DRM_REGISTERS,
1662 _DRM_READ_ONLY, &dev_priv->mmio);
1663 if (ret != 0)
1664 return ret;
1665
Dave Airlie7fc86862007-11-05 10:45:27 +10001666 dev_priv->fb_aper_offset = drm_get_resource_start(dev, 0);
1667 ret = drm_addmap(dev, dev_priv->fb_aper_offset,
Dave Airlie836cf042005-07-10 19:27:04 +10001668 drm_get_resource_len(dev, 0), _DRM_FRAME_BUFFER,
1669 _DRM_WRITE_COMBINING, &map);
1670 if (ret != 0)
1671 return ret;
1672
1673 return 0;
1674}
1675
Dave Airlie22eae942005-11-10 22:16:34 +11001676int radeon_driver_unload(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677{
1678 drm_radeon_private_t *dev_priv = dev->dev_private;
1679
1680 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER);
1682
1683 dev->dev_private = NULL;
1684 return 0;
1685}