blob: 59a2132a8f57a5e833b83011191de047dc9ec43e [file] [log] [blame]
Dave Airlief26c4732006-01-02 17:18:39 +11001/* radeon_cp.c -- CP support for Radeon -*- linux-c -*- */
2/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
4 * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
Alex Deucher45e51902008-05-28 13:28:59 +10005 * Copyright 2007 Advanced Micro Devices, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 * Kevin E. Martin <martin@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
30 */
31
32#include "drmP.h"
33#include "drm.h"
34#include "radeon_drm.h"
35#include "radeon_drv.h"
Dave Airlie414ed532005-08-16 20:43:16 +100036#include "r300_reg.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Alex Deucher9f184092008-05-28 11:21:25 +100038#include "radeon_microcode.h"
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define RADEON_FIFO_DEBUG 0
41
Dave Airlie84b1fd12007-07-11 15:53:27 +100042static int radeon_do_cleanup_cp(struct drm_device * dev);
Jerome Glisse54f961a2008-08-13 09:46:31 +100043static void radeon_do_cp_start(drm_radeon_private_t * dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Alex Deucher45e51902008-05-28 13:28:59 +100045static u32 R500_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
Dave Airlie3d5e2c12008-02-07 15:01:05 +100046{
47 u32 ret;
48 RADEON_WRITE(R520_MC_IND_INDEX, 0x7f0000 | (addr & 0xff));
49 ret = RADEON_READ(R520_MC_IND_DATA);
50 RADEON_WRITE(R520_MC_IND_INDEX, 0);
51 return ret;
52}
53
Alex Deucher45e51902008-05-28 13:28:59 +100054static u32 RS480_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
55{
56 u32 ret;
57 RADEON_WRITE(RS480_NB_MC_INDEX, addr & 0xff);
58 ret = RADEON_READ(RS480_NB_MC_DATA);
59 RADEON_WRITE(RS480_NB_MC_INDEX, 0xff);
60 return ret;
61}
62
Maciej Cencora60f92682008-02-19 21:32:45 +100063static u32 RS690_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
64{
Alex Deucher45e51902008-05-28 13:28:59 +100065 u32 ret;
Maciej Cencora60f92682008-02-19 21:32:45 +100066 RADEON_WRITE(RS690_MC_INDEX, (addr & RS690_MC_INDEX_MASK));
Alex Deucher45e51902008-05-28 13:28:59 +100067 ret = RADEON_READ(RS690_MC_DATA);
68 RADEON_WRITE(RS690_MC_INDEX, RS690_MC_INDEX_MASK);
69 return ret;
70}
71
72static u32 IGP_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
73{
Alex Deucherf0738e92008-10-16 17:12:02 +100074 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
75 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +100076 return RS690_READ_MCIND(dev_priv, addr);
77 else
78 return RS480_READ_MCIND(dev_priv, addr);
Maciej Cencora60f92682008-02-19 21:32:45 +100079}
80
Dave Airlie3d5e2c12008-02-07 15:01:05 +100081u32 radeon_read_fb_location(drm_radeon_private_t *dev_priv)
82{
83
84 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100085 return R500_READ_MCIND(dev_priv, RV515_MC_FB_LOCATION);
Alex Deucherf0738e92008-10-16 17:12:02 +100086 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
87 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +100088 return RS690_READ_MCIND(dev_priv, RS690_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100089 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100090 return R500_READ_MCIND(dev_priv, R520_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100091 else
92 return RADEON_READ(RADEON_MC_FB_LOCATION);
93}
94
95static void radeon_write_fb_location(drm_radeon_private_t *dev_priv, u32 fb_loc)
96{
97 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100098 R500_WRITE_MCIND(RV515_MC_FB_LOCATION, fb_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +100099 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
100 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000101 RS690_WRITE_MCIND(RS690_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000102 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000103 R500_WRITE_MCIND(R520_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000104 else
105 RADEON_WRITE(RADEON_MC_FB_LOCATION, fb_loc);
106}
107
108static void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_loc)
109{
110 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000111 R500_WRITE_MCIND(RV515_MC_AGP_LOCATION, agp_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000112 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
113 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000114 RS690_WRITE_MCIND(RS690_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000115 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000116 R500_WRITE_MCIND(R520_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000117 else
118 RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc);
119}
120
Dave Airlie70b13d52008-06-19 11:40:44 +1000121static void radeon_write_agp_base(drm_radeon_private_t *dev_priv, u64 agp_base)
122{
123 u32 agp_base_hi = upper_32_bits(agp_base);
124 u32 agp_base_lo = agp_base & 0xffffffff;
125
126 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) {
127 R500_WRITE_MCIND(RV515_MC_AGP_BASE, agp_base_lo);
128 R500_WRITE_MCIND(RV515_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherf0738e92008-10-16 17:12:02 +1000129 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
130 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000131 RS690_WRITE_MCIND(RS690_MC_AGP_BASE, agp_base_lo);
132 RS690_WRITE_MCIND(RS690_MC_AGP_BASE_2, agp_base_hi);
133 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515) {
134 R500_WRITE_MCIND(R520_MC_AGP_BASE, agp_base_lo);
135 R500_WRITE_MCIND(R520_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000136 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
137 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Alex Deucher5cfb6952008-06-19 12:38:29 +1000138 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000139 RADEON_WRITE(RS480_AGP_BASE_2, agp_base_hi);
Dave Airlie70b13d52008-06-19 11:40:44 +1000140 } else {
141 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
142 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200)
143 RADEON_WRITE(RADEON_AGP_BASE_2, agp_base_hi);
144 }
145}
146
Dave Airlie84b1fd12007-07-11 15:53:27 +1000147static int RADEON_READ_PLL(struct drm_device * dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
149 drm_radeon_private_t *dev_priv = dev->dev_private;
150
151 RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
152 return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
153}
154
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000155static u32 RADEON_READ_PCIE(drm_radeon_private_t *dev_priv, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Dave Airlieea98a922005-09-11 20:28:11 +1000157 RADEON_WRITE8(RADEON_PCIE_INDEX, addr & 0xff);
158 return RADEON_READ(RADEON_PCIE_DATA);
159}
160
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000162static void radeon_status(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700164 printk("%s:\n", __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000165 printk("RBBM_STATUS = 0x%08x\n",
166 (unsigned int)RADEON_READ(RADEON_RBBM_STATUS));
167 printk("CP_RB_RTPR = 0x%08x\n",
168 (unsigned int)RADEON_READ(RADEON_CP_RB_RPTR));
169 printk("CP_RB_WTPR = 0x%08x\n",
170 (unsigned int)RADEON_READ(RADEON_CP_RB_WPTR));
171 printk("AIC_CNTL = 0x%08x\n",
172 (unsigned int)RADEON_READ(RADEON_AIC_CNTL));
173 printk("AIC_STAT = 0x%08x\n",
174 (unsigned int)RADEON_READ(RADEON_AIC_STAT));
175 printk("AIC_PT_BASE = 0x%08x\n",
176 (unsigned int)RADEON_READ(RADEON_AIC_PT_BASE));
177 printk("TLB_ADDR = 0x%08x\n",
178 (unsigned int)RADEON_READ(RADEON_AIC_TLB_ADDR));
179 printk("TLB_DATA = 0x%08x\n",
180 (unsigned int)RADEON_READ(RADEON_AIC_TLB_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182#endif
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* ================================================================
185 * Engine, FIFO control
186 */
187
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000188static int radeon_do_pixcache_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 u32 tmp;
191 int i;
192
193 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
194
Alex Deucher259434a2008-05-28 11:51:12 +1000195 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV280) {
196 tmp = RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT);
197 tmp |= RADEON_RB3D_DC_FLUSH_ALL;
198 RADEON_WRITE(RADEON_RB3D_DSTCACHE_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Alex Deucher259434a2008-05-28 11:51:12 +1000200 for (i = 0; i < dev_priv->usec_timeout; i++) {
201 if (!(RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT)
202 & RADEON_RB3D_DC_BUSY)) {
203 return 0;
204 }
205 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
Alex Deucher259434a2008-05-28 11:51:12 +1000207 } else {
Jerome Glisse54f961a2008-08-13 09:46:31 +1000208 /* don't flush or purge cache here or lockup */
209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
211
212#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000213 DRM_ERROR("failed!\n");
214 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000216 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219static int radeon_do_wait_for_fifo(drm_radeon_private_t * dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 int i;
222
223 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
224
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000225 for (i = 0; i < dev_priv->usec_timeout; i++) {
226 int slots = (RADEON_READ(RADEON_RBBM_STATUS)
227 & RADEON_RBBM_FIFOCNT_MASK);
228 if (slots >= entries)
229 return 0;
230 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000232 DRM_DEBUG("wait for fifo failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000233 RADEON_READ(RADEON_RBBM_STATUS),
234 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000237 DRM_ERROR("failed!\n");
238 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000240 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000243static int radeon_do_wait_for_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int i, ret;
246
247 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
248
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000249 ret = radeon_do_wait_for_fifo(dev_priv, 64);
250 if (ret)
251 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000253 for (i = 0; i < dev_priv->usec_timeout; i++) {
254 if (!(RADEON_READ(RADEON_RBBM_STATUS)
255 & RADEON_RBBM_ACTIVE)) {
256 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return 0;
258 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000259 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000261 DRM_DEBUG("wait idle failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000262 RADEON_READ(RADEON_RBBM_STATUS),
263 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000266 DRM_ERROR("failed!\n");
267 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000269 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Alex Deucher5b92c402008-05-28 11:57:40 +1000272static void radeon_init_pipes(drm_radeon_private_t *dev_priv)
273{
274 uint32_t gb_tile_config, gb_pipe_sel = 0;
275
276 /* RS4xx/RS6xx/R4xx/R5xx */
277 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R420) {
278 gb_pipe_sel = RADEON_READ(R400_GB_PIPE_SELECT);
279 dev_priv->num_gb_pipes = ((gb_pipe_sel >> 12) & 0x3) + 1;
280 } else {
281 /* R3xx */
282 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
283 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350)) {
284 dev_priv->num_gb_pipes = 2;
285 } else {
286 /* R3Vxx */
287 dev_priv->num_gb_pipes = 1;
288 }
289 }
290 DRM_INFO("Num pipes: %d\n", dev_priv->num_gb_pipes);
291
292 gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16 /*| R300_SUBPIXEL_1_16*/);
293
294 switch (dev_priv->num_gb_pipes) {
295 case 2: gb_tile_config |= R300_PIPE_COUNT_R300; break;
296 case 3: gb_tile_config |= R300_PIPE_COUNT_R420_3P; break;
297 case 4: gb_tile_config |= R300_PIPE_COUNT_R420; break;
298 default:
299 case 1: gb_tile_config |= R300_PIPE_COUNT_RV350; break;
300 }
301
302 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV515) {
303 RADEON_WRITE_PLL(R500_DYN_SCLK_PWMEM_PIPE, (1 | ((gb_pipe_sel >> 8) & 0xf) << 4));
304 RADEON_WRITE(R500_SU_REG_DEST, ((1 << dev_priv->num_gb_pipes) - 1));
305 }
306 RADEON_WRITE(R300_GB_TILE_CONFIG, gb_tile_config);
307 radeon_do_wait_for_idle(dev_priv);
308 RADEON_WRITE(R300_DST_PIPE_CONFIG, RADEON_READ(R300_DST_PIPE_CONFIG) | R300_PIPE_AUTO_CONFIG);
309 RADEON_WRITE(R300_RB2D_DSTCACHE_MODE, (RADEON_READ(R300_RB2D_DSTCACHE_MODE) |
310 R300_DC_AUTOFLUSH_ENABLE |
311 R300_DC_DC_DISABLE_IGNORE_PE));
312
313
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316/* ================================================================
317 * CP control, initialization
318 */
319
320/* Load the microcode for the CP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321static void radeon_cp_load_microcode(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000326 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328 RADEON_WRITE(RADEON_CP_ME_RAM_ADDR, 0);
Alex Deucher9f184092008-05-28 11:21:25 +1000329 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R100) ||
330 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV100) ||
331 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV200) ||
332 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS100) ||
333 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS200)) {
334 DRM_INFO("Loading R100 Microcode\n");
335 for (i = 0; i < 256; i++) {
336 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
337 R100_cp_microcode[i][1]);
338 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
339 R100_cp_microcode[i][0]);
340 }
341 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R200) ||
342 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV250) ||
343 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV280) ||
344 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS300)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 DRM_INFO("Loading R200 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 for (i = 0; i < 256; i++) {
347 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
348 R200_cp_microcode[i][1]);
349 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
350 R200_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
Alex Deucher9f184092008-05-28 11:21:25 +1000352 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
353 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350) ||
354 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV350) ||
355 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000356 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000357 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 DRM_INFO("Loading R300 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000359 for (i = 0; i < 256; i++) {
360 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
361 R300_cp_microcode[i][1]);
362 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
363 R300_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
Alex Deucher9f184092008-05-28 11:21:25 +1000365 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
Alex Deucheredc6f382008-10-17 09:21:45 +1000366 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R423) ||
Alex Deucher9f184092008-05-28 11:21:25 +1000367 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV410)) {
368 DRM_INFO("Loading R400 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000369 for (i = 0; i < 256; i++) {
370 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
Alex Deucher9f184092008-05-28 11:21:25 +1000371 R420_cp_microcode[i][1]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
Alex Deucher9f184092008-05-28 11:21:25 +1000373 R420_cp_microcode[i][0]);
374 }
Alex Deucherf0738e92008-10-16 17:12:02 +1000375 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
376 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
377 DRM_INFO("Loading RS690/RS740 Microcode\n");
Alex Deucher9f184092008-05-28 11:21:25 +1000378 for (i = 0; i < 256; i++) {
379 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
380 RS690_cp_microcode[i][1]);
381 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
382 RS690_cp_microcode[i][0]);
383 }
384 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) ||
385 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R520) ||
386 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) ||
387 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R580) ||
388 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV560) ||
389 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV570)) {
390 DRM_INFO("Loading R500 Microcode\n");
391 for (i = 0; i < 256; i++) {
392 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
393 R520_cp_microcode[i][1]);
394 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
395 R520_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397 }
398}
399
400/* Flush any pending commands to the CP. This should only be used just
401 * prior to a wait for idle, as it informs the engine that the command
402 * stream is ending.
403 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000404static void radeon_do_cp_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000406 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407#if 0
408 u32 tmp;
409
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000410 tmp = RADEON_READ(RADEON_CP_RB_WPTR) | (1 << 31);
411 RADEON_WRITE(RADEON_CP_RB_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#endif
413}
414
415/* Wait for the CP to go idle.
416 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417int radeon_do_cp_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000420 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000422 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
424 RADEON_PURGE_CACHE();
425 RADEON_PURGE_ZCACHE();
426 RADEON_WAIT_UNTIL_IDLE();
427
428 ADVANCE_RING();
429 COMMIT_RING();
430
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 return radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434/* Start the Command Processor.
435 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000436static void radeon_do_cp_start(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000439 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 RADEON_WRITE(RADEON_CP_CSQ_CNTL, dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 dev_priv->cp_running = 1;
446
Jerome Glisse54f961a2008-08-13 09:46:31 +1000447 BEGIN_RING(8);
448 /* isync can only be written through cp on r5xx write it here */
449 OUT_RING(CP_PACKET0(RADEON_ISYNC_CNTL, 0));
450 OUT_RING(RADEON_ISYNC_ANY2D_IDLE3D |
451 RADEON_ISYNC_ANY3D_IDLE2D |
452 RADEON_ISYNC_WAIT_IDLEGUI |
453 RADEON_ISYNC_CPSCRATCH_IDLEGUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 RADEON_PURGE_CACHE();
455 RADEON_PURGE_ZCACHE();
456 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 ADVANCE_RING();
458 COMMIT_RING();
Jerome Glisse54f961a2008-08-13 09:46:31 +1000459
460 dev_priv->track_flush |= RADEON_FLUSH_EMITED | RADEON_PURGE_EMITED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/* Reset the Command Processor. This will not flush any pending
464 * commands, so you must wait for the CP command stream to complete
465 * before calling this routine.
466 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467static void radeon_do_cp_reset(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 u32 cur_read_ptr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
473 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
474 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 dev_priv->ring.tail = cur_read_ptr;
476}
477
478/* Stop the Command Processor. This will not flush any pending
479 * commands, so you must flush the command stream and wait for the CP
480 * to go idle before calling this routine.
481 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482static void radeon_do_cp_stop(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000484 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000486 RADEON_WRITE(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
488 dev_priv->cp_running = 0;
489}
490
491/* Reset the engine. This will stop the CP if it is running.
492 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000493static int radeon_do_engine_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 drm_radeon_private_t *dev_priv = dev->dev_private;
Alex Deucherd396db32008-05-28 11:54:06 +1000496 u32 clock_cntl_index = 0, mclk_cntl = 0, rbbm_soft_reset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000497 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Alex Deucherd396db32008-05-28 11:54:06 +1000501 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
502 /* may need something similar for newer chips */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000503 clock_cntl_index = RADEON_READ(RADEON_CLOCK_CNTL_INDEX);
504 mclk_cntl = RADEON_READ_PLL(dev, RADEON_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000506 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, (mclk_cntl |
507 RADEON_FORCEON_MCLKA |
508 RADEON_FORCEON_MCLKB |
509 RADEON_FORCEON_YCLKA |
510 RADEON_FORCEON_YCLKB |
511 RADEON_FORCEON_MC |
512 RADEON_FORCEON_AIC));
Alex Deucherd396db32008-05-28 11:54:06 +1000513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
Alex Deucherd396db32008-05-28 11:54:06 +1000515 rbbm_soft_reset = RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Alex Deucherd396db32008-05-28 11:54:06 +1000517 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset |
518 RADEON_SOFT_RESET_CP |
519 RADEON_SOFT_RESET_HI |
520 RADEON_SOFT_RESET_SE |
521 RADEON_SOFT_RESET_RE |
522 RADEON_SOFT_RESET_PP |
523 RADEON_SOFT_RESET_E2 |
524 RADEON_SOFT_RESET_RB));
525 RADEON_READ(RADEON_RBBM_SOFT_RESET);
526 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset &
527 ~(RADEON_SOFT_RESET_CP |
528 RADEON_SOFT_RESET_HI |
529 RADEON_SOFT_RESET_SE |
530 RADEON_SOFT_RESET_RE |
531 RADEON_SOFT_RESET_PP |
532 RADEON_SOFT_RESET_E2 |
533 RADEON_SOFT_RESET_RB)));
534 RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Alex Deucherd396db32008-05-28 11:54:06 +1000536 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000537 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, mclk_cntl);
538 RADEON_WRITE(RADEON_CLOCK_CNTL_INDEX, clock_cntl_index);
539 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, rbbm_soft_reset);
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Alex Deucher5b92c402008-05-28 11:57:40 +1000542 /* setup the raster pipes */
543 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R300)
544 radeon_init_pipes(dev_priv);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 /* Reset the CP ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000547 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 /* The CP is no longer running after an engine reset */
550 dev_priv->cp_running = 0;
551
552 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 radeon_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 return 0;
556}
557
Dave Airlie84b1fd12007-07-11 15:53:27 +1000558static void radeon_cp_init_ring_buffer(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000559 drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 u32 ring_start, cur_read_ptr;
562 u32 tmp;
Dave Airliebc5f4522007-11-05 12:50:58 +1000563
Dave Airlied5ea7022006-03-19 19:37:55 +1100564 /* Initialize the memory controller. With new memory map, the fb location
565 * is not changed, it should have been properly initialized already. Part
566 * of the problem is that the code below is bogus, assuming the GART is
567 * always appended to the fb which is not necessarily the case
568 */
569 if (!dev_priv->new_memmap)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000570 radeon_write_fb_location(dev_priv,
Dave Airlied5ea7022006-03-19 19:37:55 +1100571 ((dev_priv->gart_vm_start - 1) & 0xffff0000)
572 | (dev_priv->fb_location >> 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000575 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000576 radeon_write_agp_base(dev_priv, dev->agp->base);
577
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000578 radeon_write_agp_location(dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 (((dev_priv->gart_vm_start - 1 +
580 dev_priv->gart_size) & 0xffff0000) |
581 (dev_priv->gart_vm_start >> 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 ring_start = (dev_priv->cp_ring->offset
584 - dev->agp->base
585 + dev_priv->gart_vm_start);
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100586 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#endif
588 ring_start = (dev_priv->cp_ring->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100589 - (unsigned long)dev->sg->virtual
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 + dev_priv->gart_vm_start);
591
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000592 RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 /* Set the write pointer delay */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Initialize the ring buffer's read and write pointers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
599 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
600 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 dev_priv->ring.tail = cur_read_ptr;
602
603#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000604 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000605 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
606 dev_priv->ring_rptr->offset
607 - dev->agp->base + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 } else
609#endif
610 {
Dave Airlie55910512007-07-11 16:53:40 +1000611 struct drm_sg_mem *entry = dev->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 unsigned long tmp_ofs, page_ofs;
613
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100614 tmp_ofs = dev_priv->ring_rptr->offset -
615 (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 page_ofs = tmp_ofs >> PAGE_SHIFT;
617
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR, entry->busaddr[page_ofs]);
619 DRM_DEBUG("ring rptr: offset=0x%08lx handle=0x%08lx\n",
620 (unsigned long)entry->busaddr[page_ofs],
621 entry->handle + tmp_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
Dave Airlied5ea7022006-03-19 19:37:55 +1100624 /* Set ring buffer size */
625#ifdef __BIG_ENDIAN
626 RADEON_WRITE(RADEON_CP_RB_CNTL,
Roland Scheidegger576cc452008-02-07 14:59:24 +1000627 RADEON_BUF_SWAP_32BIT |
628 (dev_priv->ring.fetch_size_l2ow << 18) |
629 (dev_priv->ring.rptr_update_l2qw << 8) |
630 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100631#else
Roland Scheidegger576cc452008-02-07 14:59:24 +1000632 RADEON_WRITE(RADEON_CP_RB_CNTL,
633 (dev_priv->ring.fetch_size_l2ow << 18) |
634 (dev_priv->ring.rptr_update_l2qw << 8) |
635 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100636#endif
637
Dave Airlied5ea7022006-03-19 19:37:55 +1100638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /* Initialize the scratch register pointer. This will cause
640 * the scratch register values to be written out to memory
641 * whenever they are updated.
642 *
643 * We simply put this behind the ring read pointer, this works
644 * with PCI GART as well as (whatever kind of) AGP GART
645 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000646 RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
647 + RADEON_SCRATCH_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 dev_priv->scratch = ((__volatile__ u32 *)
650 dev_priv->ring_rptr->handle +
651 (RADEON_SCRATCH_REG_OFFSET / sizeof(u32)));
652
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000653 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Dave Airlied5ea7022006-03-19 19:37:55 +1100655 /* Turn on bus mastering */
Alex Deucheredc6f382008-10-17 09:21:45 +1000656 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
657 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
658 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
659 /* rs400, rs690/rs740 */
660 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RS400_BUS_MASTER_DIS;
661 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
662 } else if (!(((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
663 ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R423))) {
664 /* r1xx, r2xx, r300, r(v)350, r420/r481, rs480 */
665 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
666 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
667 } /* PCIE cards appears to not need this */
Dave Airlied5ea7022006-03-19 19:37:55 +1100668
669 dev_priv->sarea_priv->last_frame = dev_priv->scratch[0] = 0;
670 RADEON_WRITE(RADEON_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame);
671
672 dev_priv->sarea_priv->last_dispatch = dev_priv->scratch[1] = 0;
673 RADEON_WRITE(RADEON_LAST_DISPATCH_REG,
674 dev_priv->sarea_priv->last_dispatch);
675
676 dev_priv->sarea_priv->last_clear = dev_priv->scratch[2] = 0;
677 RADEON_WRITE(RADEON_LAST_CLEAR_REG, dev_priv->sarea_priv->last_clear);
678
679 radeon_do_wait_for_idle(dev_priv);
680
681 /* Sync everything up */
682 RADEON_WRITE(RADEON_ISYNC_CNTL,
683 (RADEON_ISYNC_ANY2D_IDLE3D |
684 RADEON_ISYNC_ANY3D_IDLE2D |
685 RADEON_ISYNC_WAIT_IDLEGUI |
686 RADEON_ISYNC_CPSCRATCH_IDLEGUI));
687
688}
689
690static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
691{
692 u32 tmp;
693
Dave Airlie6b79d522008-09-02 10:10:16 +1000694 /* Start with assuming that writeback doesn't work */
695 dev_priv->writeback_works = 0;
696
Dave Airlied5ea7022006-03-19 19:37:55 +1100697 /* Writeback doesn't seem to work everywhere, test it here and possibly
698 * enable it if it appears to work
699 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000700 DRM_WRITE32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1), 0);
701 RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000703 for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
704 if (DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1)) ==
705 0xdeadbeef)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000707 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000710 if (tmp < dev_priv->usec_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 dev_priv->writeback_works = 1;
Dave Airlied5ea7022006-03-19 19:37:55 +1100712 DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 } else {
714 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100715 DRM_INFO("writeback test failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Dave Airlie689b9d72005-09-30 17:09:07 +1000717 if (radeon_no_wb == 1) {
718 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100719 DRM_INFO("writeback forced off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Michel Dänzerae1b1a482006-08-07 20:37:46 +1000721
722 if (!dev_priv->writeback_works) {
723 /* Disable writeback to avoid unnecessary bus master transfer */
724 RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
725 RADEON_RB_NO_UPDATE);
726 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Dave Airlief2b04cd2007-05-08 15:19:23 +1000730/* Enable or disable IGP GART on the chip */
731static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
732{
Maciej Cencora60f92682008-02-19 21:32:45 +1000733 u32 temp;
734
735 if (on) {
Alex Deucher45e51902008-05-28 13:28:59 +1000736 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
Maciej Cencora60f92682008-02-19 21:32:45 +1000737 dev_priv->gart_vm_start,
738 (long)dev_priv->gart_info.bus_addr,
739 dev_priv->gart_size);
740
Alex Deucher45e51902008-05-28 13:28:59 +1000741 temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
Alex Deucherf0738e92008-10-16 17:12:02 +1000742 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
743 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +1000744 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
745 RS690_BLOCK_GFX_D3_EN));
746 else
747 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
Maciej Cencora60f92682008-02-19 21:32:45 +1000748
Alex Deucher45e51902008-05-28 13:28:59 +1000749 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
750 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000751
Alex Deucher45e51902008-05-28 13:28:59 +1000752 temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
753 IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
754 RS480_TLB_ENABLE |
755 RS480_GTW_LAC_EN |
756 RS480_1LEVEL_GART));
Maciej Cencora60f92682008-02-19 21:32:45 +1000757
Dave Airliefa0d71b2008-05-28 11:27:01 +1000758 temp = dev_priv->gart_info.bus_addr & 0xfffff000;
759 temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
Alex Deucher45e51902008-05-28 13:28:59 +1000760 IGP_WRITE_MCIND(RS480_GART_BASE, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000761
Alex Deucher45e51902008-05-28 13:28:59 +1000762 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
763 IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
764 RS480_REQ_TYPE_SNOOP_DIS));
Maciej Cencora60f92682008-02-19 21:32:45 +1000765
Alex Deucher5cfb6952008-06-19 12:38:29 +1000766 radeon_write_agp_base(dev_priv, dev_priv->gart_vm_start);
Dave Airlie3722bfc2008-05-28 11:28:27 +1000767
Maciej Cencora60f92682008-02-19 21:32:45 +1000768 dev_priv->gart_size = 32*1024*1024;
769 temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
770 0xffff0000) | (dev_priv->gart_vm_start >> 16));
771
Alex Deucher45e51902008-05-28 13:28:59 +1000772 radeon_write_agp_location(dev_priv, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000773
Alex Deucher45e51902008-05-28 13:28:59 +1000774 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
775 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
776 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000777
778 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000779 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
780 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000781 break;
782 DRM_UDELAY(1);
783 } while (1);
784
Alex Deucher45e51902008-05-28 13:28:59 +1000785 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
786 RS480_GART_CACHE_INVALIDATE);
Alex Deucher27359772008-05-28 12:54:16 +1000787
Maciej Cencora60f92682008-02-19 21:32:45 +1000788 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000789 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
790 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000791 break;
792 DRM_UDELAY(1);
793 } while (1);
794
Alex Deucher45e51902008-05-28 13:28:59 +1000795 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000796 } else {
Alex Deucher45e51902008-05-28 13:28:59 +1000797 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000798 }
799}
800
Dave Airlieea98a922005-09-11 20:28:11 +1000801static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Dave Airlieea98a922005-09-11 20:28:11 +1000803 u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
804 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Dave Airlieea98a922005-09-11 20:28:11 +1000806 DRM_DEBUG("programming pcie %08X %08lX %08X\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 dev_priv->gart_vm_start,
808 (long)dev_priv->gart_info.bus_addr,
Dave Airlieea98a922005-09-11 20:28:11 +1000809 dev_priv->gart_size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000810 RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
811 dev_priv->gart_vm_start);
812 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
813 dev_priv->gart_info.bus_addr);
814 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
815 dev_priv->gart_vm_start);
816 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
817 dev_priv->gart_vm_start +
818 dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000820 radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
823 RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000825 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
826 tmp & ~RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830/* Enable or disable PCI GART on the chip */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Dave Airlied985c102006-01-02 21:32:48 +1100833 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
Alex Deucher45e51902008-05-28 13:28:59 +1000835 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
Alex Deucherf0738e92008-10-16 17:12:02 +1000836 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000837 (dev_priv->flags & RADEON_IS_IGPGART)) {
Dave Airlief2b04cd2007-05-08 15:19:23 +1000838 radeon_set_igpgart(dev_priv, on);
839 return;
840 }
841
Dave Airlie54a56ac2006-09-22 04:25:09 +1000842 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieea98a922005-09-11 20:28:11 +1000843 radeon_set_pciegart(dev_priv, on);
844 return;
845 }
846
Dave Airliebc5f4522007-11-05 12:50:58 +1000847 tmp = RADEON_READ(RADEON_AIC_CNTL);
Dave Airlied985c102006-01-02 21:32:48 +1100848
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000849 if (on) {
850 RADEON_WRITE(RADEON_AIC_CNTL,
851 tmp | RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 /* set PCI GART page-table base address
854 */
Dave Airlieea98a922005-09-11 20:28:11 +1000855 RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 /* set address range for PCI address translate
858 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000859 RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
860 RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
861 + dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /* Turn off AGP aperture -- is this required for PCI GART?
864 */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000865 radeon_write_agp_location(dev_priv, 0xffffffc0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866 RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 RADEON_WRITE(RADEON_AIC_CNTL,
869 tmp & ~RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 }
871}
872
Dave Airlie84b1fd12007-07-11 15:53:27 +1000873static int radeon_do_init_cp(struct drm_device * dev, drm_radeon_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
Dave Airlied985c102006-01-02 21:32:48 +1100875 drm_radeon_private_t *dev_priv = dev->dev_private;
876
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Dave Airlief3dd5c32006-03-25 18:09:46 +1100879 /* if we require new memory map but we don't have it fail */
Dave Airlie54a56ac2006-09-22 04:25:09 +1000880 if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
Dave Airlieb15ec362006-08-19 17:43:52 +1000881 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 +1100882 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000883 return -EINVAL;
Dave Airlief3dd5c32006-03-25 18:09:46 +1100884 }
885
Dave Airlie54a56ac2006-09-22 04:25:09 +1000886 if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
Dave Airlied985c102006-01-02 21:32:48 +1100887 DRM_DEBUG("Forcing AGP card to PCI mode\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000888 dev_priv->flags &= ~RADEON_IS_AGP;
889 } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
Dave Airlieb15ec362006-08-19 17:43:52 +1000890 && !init->is_pci) {
891 DRM_DEBUG("Restoring AGP flag\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000892 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlied985c102006-01-02 21:32:48 +1100893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Dave Airlie54a56ac2006-09-22 04:25:09 +1000895 if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000898 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
901 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000902 if (dev_priv->usec_timeout < 1 ||
903 dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
904 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000906 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 }
908
Dave Airlieddbee332007-07-11 12:16:01 +1000909 /* Enable vblank on CRTC1 for older X servers
910 */
911 dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
912
Dave Airlied985c102006-01-02 21:32:48 +1100913 switch(init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 case RADEON_INIT_R200_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 dev_priv->microcode_version = UCODE_R200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
917 case RADEON_INIT_R300_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000918 dev_priv->microcode_version = UCODE_R300;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 break;
920 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000921 dev_priv->microcode_version = UCODE_R100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 dev_priv->do_boxes = 0;
925 dev_priv->cp_mode = init->cp_mode;
926
927 /* We don't support anything other than bus-mastering ring mode,
928 * but the ring can be in either AGP or PCI space for the ring
929 * read pointer.
930 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
932 (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
933 DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000935 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 }
937
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000938 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 case 16:
940 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
941 break;
942 case 32:
943 default:
944 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
945 break;
946 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000947 dev_priv->front_offset = init->front_offset;
948 dev_priv->front_pitch = init->front_pitch;
949 dev_priv->back_offset = init->back_offset;
950 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000952 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 case 16:
954 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
955 break;
956 case 32:
957 default:
958 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
959 break;
960 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000961 dev_priv->depth_offset = init->depth_offset;
962 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* Hardware state for depth clears. Remove this if/when we no
965 * longer clear the depth buffer with a 3D rectangle. Hard-code
966 * all values to prevent unwanted 3D state from slipping through
967 * and screwing with the clear operation.
968 */
969 dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
970 (dev_priv->color_fmt << 10) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000971 (dev_priv->microcode_version ==
972 UCODE_R100 ? RADEON_ZBLOCK16 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 dev_priv->depth_clear.rb3d_zstencilcntl =
975 (dev_priv->depth_fmt |
976 RADEON_Z_TEST_ALWAYS |
977 RADEON_STENCIL_TEST_ALWAYS |
978 RADEON_STENCIL_S_FAIL_REPLACE |
979 RADEON_STENCIL_ZPASS_REPLACE |
980 RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
982 dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
983 RADEON_BFACE_SOLID |
984 RADEON_FFACE_SOLID |
985 RADEON_FLAT_SHADE_VTX_LAST |
986 RADEON_DIFFUSE_SHADE_FLAT |
987 RADEON_ALPHA_SHADE_FLAT |
988 RADEON_SPECULAR_SHADE_FLAT |
989 RADEON_FOG_SHADE_FLAT |
990 RADEON_VTX_PIX_CENTER_OGL |
991 RADEON_ROUND_MODE_TRUNC |
992 RADEON_ROUND_PREC_8TH_PIX);
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 dev_priv->ring_offset = init->ring_offset;
996 dev_priv->ring_rptr_offset = init->ring_rptr_offset;
997 dev_priv->buffers_offset = init->buffers_offset;
998 dev_priv->gart_textures_offset = init->gart_textures_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000999
Dave Airlieda509d72007-05-26 05:04:51 +10001000 dev_priv->sarea = drm_getsarea(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001001 if (!dev_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 DRM_ERROR("could not find sarea!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001004 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 }
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 dev_priv->cp_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001008 if (!dev_priv->cp_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 DRM_ERROR("could not find cp ring region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
1013 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001014 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 DRM_ERROR("could not find ring read pointer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001017 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 }
Dave Airlied1f2b552005-08-05 22:11:22 +10001019 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 DRM_ERROR("could not find dma buffer region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001024 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 if (init->gart_textures_offset) {
1028 dev_priv->gart_textures =
1029 drm_core_findmap(dev, init->gart_textures_offset);
1030 if (!dev_priv->gart_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 DRM_ERROR("could not find GART texture region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001033 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
1035 }
1036
1037 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001038 (drm_radeon_sarea_t *) ((u8 *) dev_priv->sarea->handle +
1039 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001042 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001043 drm_core_ioremap(dev_priv->cp_ring, dev);
1044 drm_core_ioremap(dev_priv->ring_rptr, dev);
1045 drm_core_ioremap(dev->agp_buffer_map, dev);
1046 if (!dev_priv->cp_ring->handle ||
1047 !dev_priv->ring_rptr->handle ||
1048 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 DRM_ERROR("could not find ioremap agp regions!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001051 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
1053 } else
1054#endif
1055 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001056 dev_priv->cp_ring->handle = (void *)dev_priv->cp_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 dev_priv->ring_rptr->handle =
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001058 (void *)dev_priv->ring_rptr->offset;
1059 dev->agp_buffer_map->handle =
1060 (void *)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001062 DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
1063 dev_priv->cp_ring->handle);
1064 DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
1065 dev_priv->ring_rptr->handle);
1066 DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
1067 dev->agp_buffer_map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001070 dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
Dave Airliebc5f4522007-11-05 12:50:58 +10001071 dev_priv->fb_size =
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001072 ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
Dave Airlied5ea7022006-03-19 19:37:55 +11001073 - dev_priv->fb_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001075 dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
1076 ((dev_priv->front_offset
1077 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001079 dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
1080 ((dev_priv->back_offset
1081 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001083 dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
1084 ((dev_priv->depth_offset
1085 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 dev_priv->gart_size = init->gart_size;
Dave Airlied5ea7022006-03-19 19:37:55 +11001088
1089 /* New let's set the memory map ... */
1090 if (dev_priv->new_memmap) {
1091 u32 base = 0;
1092
1093 DRM_INFO("Setting GART location based on new memory map\n");
1094
1095 /* If using AGP, try to locate the AGP aperture at the same
1096 * location in the card and on the bus, though we have to
1097 * align it down.
1098 */
1099#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001100 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001101 base = dev->agp->base;
1102 /* Check if valid */
Michel Dänzer80b2c382007-02-18 18:03:21 +11001103 if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
1104 base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001105 DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
1106 dev->agp->base);
1107 base = 0;
1108 }
1109 }
1110#endif
1111 /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
1112 if (base == 0) {
1113 base = dev_priv->fb_location + dev_priv->fb_size;
Michel Dänzer80b2c382007-02-18 18:03:21 +11001114 if (base < dev_priv->fb_location ||
1115 ((base + dev_priv->gart_size) & 0xfffffffful) < base)
Dave Airlied5ea7022006-03-19 19:37:55 +11001116 base = dev_priv->fb_location
1117 - dev_priv->gart_size;
Dave Airliebc5f4522007-11-05 12:50:58 +10001118 }
Dave Airlied5ea7022006-03-19 19:37:55 +11001119 dev_priv->gart_vm_start = base & 0xffc00000u;
1120 if (dev_priv->gart_vm_start != base)
1121 DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
1122 base, dev_priv->gart_vm_start);
1123 } else {
1124 DRM_INFO("Setting GART location based on old memory map\n");
1125 dev_priv->gart_vm_start = dev_priv->fb_location +
1126 RADEON_READ(RADEON_CONFIG_APER_SIZE);
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001130 if (dev_priv->flags & RADEON_IS_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 - dev->agp->base
1133 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 else
1135#endif
1136 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +01001137 - (unsigned long)dev->sg->virtual
1138 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001140 DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
1141 DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
1142 DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
1143 dev_priv->gart_buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001145 dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
1146 dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 + init->ring_size / sizeof(u32));
1148 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001149 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Roland Scheidegger576cc452008-02-07 14:59:24 +10001151 dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
1152 dev_priv->ring.rptr_update_l2qw = drm_order( /* init->rptr_update */ 4096 / 8);
1153
1154 dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
1155 dev_priv->ring.fetch_size_l2ow = drm_order( /* init->fetch_size */ 32 / 16);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001156 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1159
1160#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001161 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001163 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 } else
1165#endif
1166 {
Dave Airlieb05c2382008-03-17 10:24:24 +10001167 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +10001168 /* if we have an offset set from userspace */
Dave Airlief2b04cd2007-05-08 15:19:23 +10001169 if (dev_priv->pcigart_offset_set) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001170 dev_priv->gart_info.bus_addr =
1171 dev_priv->pcigart_offset + dev_priv->fb_location;
Dave Airlief26c4732006-01-02 17:18:39 +11001172 dev_priv->gart_info.mapping.offset =
Dave Airlie7fc86862007-11-05 10:45:27 +10001173 dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
Dave Airlief26c4732006-01-02 17:18:39 +11001174 dev_priv->gart_info.mapping.size =
Dave Airlief2b04cd2007-05-08 15:19:23 +10001175 dev_priv->gart_info.table_size;
Dave Airlief26c4732006-01-02 17:18:39 +11001176
Dave Airlie242e3df2008-07-15 15:48:05 +10001177 drm_core_ioremap_wc(&dev_priv->gart_info.mapping, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001178 dev_priv->gart_info.addr =
Dave Airlief26c4732006-01-02 17:18:39 +11001179 dev_priv->gart_info.mapping.handle;
Dave Airlieea98a922005-09-11 20:28:11 +10001180
Dave Airlief2b04cd2007-05-08 15:19:23 +10001181 if (dev_priv->flags & RADEON_IS_PCIE)
1182 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
1183 else
1184 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001185 dev_priv->gart_info.gart_table_location =
1186 DRM_ATI_GART_FB;
1187
Dave Airlief26c4732006-01-02 17:18:39 +11001188 DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001189 dev_priv->gart_info.addr,
1190 dev_priv->pcigart_offset);
1191 } else {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001192 if (dev_priv->flags & RADEON_IS_IGPGART)
1193 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
1194 else
1195 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001196 dev_priv->gart_info.gart_table_location =
1197 DRM_ATI_GART_MAIN;
Dave Airlief26c4732006-01-02 17:18:39 +11001198 dev_priv->gart_info.addr = NULL;
1199 dev_priv->gart_info.bus_addr = 0;
Dave Airlie54a56ac2006-09-22 04:25:09 +10001200 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001201 DRM_ERROR
1202 ("Cannot use PCI Express without GART in FB memory\n");
Dave Airlieea98a922005-09-11 20:28:11 +10001203 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001204 return -EINVAL;
Dave Airlieea98a922005-09-11 20:28:11 +10001205 }
1206 }
1207
1208 if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001209 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001211 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 }
1213
1214 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001215 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001218 radeon_cp_load_microcode(dev_priv);
1219 radeon_cp_init_ring_buffer(dev, dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 dev_priv->last_buf = 0;
1222
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001223 radeon_do_engine_reset(dev);
Dave Airlied5ea7022006-03-19 19:37:55 +11001224 radeon_test_writeback(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return 0;
1227}
1228
Dave Airlie84b1fd12007-07-11 15:53:27 +10001229static int radeon_do_cleanup_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 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
1234 /* Make sure interrupts are disabled here because the uninstall ioctl
1235 * may not have been called from userspace and after dev_private
1236 * is freed, it's too late.
1237 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001238 if (dev->irq_enabled)
1239 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
1241#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001242 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied985c102006-01-02 21:32:48 +11001243 if (dev_priv->cp_ring != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001244 drm_core_ioremapfree(dev_priv->cp_ring, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001245 dev_priv->cp_ring = NULL;
1246 }
1247 if (dev_priv->ring_rptr != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001248 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001249 dev_priv->ring_rptr = NULL;
1250 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001251 if (dev->agp_buffer_map != NULL) {
1252 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 dev->agp_buffer_map = NULL;
1254 }
1255 } else
1256#endif
1257 {
Dave Airlied985c102006-01-02 21:32:48 +11001258
1259 if (dev_priv->gart_info.bus_addr) {
1260 /* Turn off PCI GART */
1261 radeon_set_pcigart(dev_priv, 0);
Dave Airlieea98a922005-09-11 20:28:11 +10001262 if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
1263 DRM_ERROR("failed to cleanup PCI GART!\n");
Dave Airlied985c102006-01-02 21:32:48 +11001264 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001265
Dave Airlied985c102006-01-02 21:32:48 +11001266 if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
1267 {
Dave Airlief26c4732006-01-02 17:18:39 +11001268 drm_core_ioremapfree(&dev_priv->gart_info.mapping, dev);
Dave Airlief2b04cd2007-05-08 15:19:23 +10001269 dev_priv->gart_info.addr = 0;
Dave Airlieea98a922005-09-11 20:28:11 +10001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 /* only clear to the start of flags */
1273 memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
1274
1275 return 0;
1276}
1277
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001278/* This code will reinit the Radeon CP hardware after a resume from disc.
1279 * AFAIK, it would be very difficult to pickle the state at suspend time, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 * here we make sure that all Radeon hardware initialisation is re-done without
1281 * affecting running applications.
1282 *
1283 * Charl P. Botha <http://cpbotha.net>
1284 */
Dave Airlie84b1fd12007-07-11 15:53:27 +10001285static int radeon_do_resume_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 drm_radeon_private_t *dev_priv = dev->dev_private;
1288
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001289 if (!dev_priv) {
1290 DRM_ERROR("Called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001291 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
1293
1294 DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1295
1296#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001297 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001299 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 } else
1301#endif
1302 {
1303 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001304 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001307 radeon_cp_load_microcode(dev_priv);
1308 radeon_cp_init_ring_buffer(dev, dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001310 radeon_do_engine_reset(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001311 radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 DRM_DEBUG("radeon_do_resume_cp() complete\n");
1314
1315 return 0;
1316}
1317
Eric Anholtc153f452007-09-03 12:06:45 +10001318int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
Eric Anholtc153f452007-09-03 12:06:45 +10001320 drm_radeon_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Eric Anholt6c340ea2007-08-25 20:23:09 +10001322 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Eric Anholtc153f452007-09-03 12:06:45 +10001324 if (init->func == RADEON_INIT_R300_CP)
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001325 r300_init_reg_flags(dev);
Dave Airlie414ed532005-08-16 20:43:16 +10001326
Eric Anholtc153f452007-09-03 12:06:45 +10001327 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 case RADEON_INIT_CP:
1329 case RADEON_INIT_R200_CP:
1330 case RADEON_INIT_R300_CP:
Eric Anholtc153f452007-09-03 12:06:45 +10001331 return radeon_do_init_cp(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 case RADEON_CLEANUP_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 return radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 }
1335
Eric Anholt20caafa2007-08-25 19:22:43 +10001336 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337}
1338
Eric Anholtc153f452007-09-03 12:06:45 +10001339int radeon_cp_start(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->cp_running) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001347 DRM_DEBUG("while CP running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 return 0;
1349 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001350 if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001351 DRM_DEBUG("called with bogus CP mode (%d)\n",
1352 dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return 0;
1354 }
1355
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001356 radeon_do_cp_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
1358 return 0;
1359}
1360
1361/* Stop the CP. The engine must have been idled before calling this
1362 * routine.
1363 */
Eric Anholtc153f452007-09-03 12:06:45 +10001364int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001367 drm_radeon_cp_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001369 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Eric Anholt6c340ea2007-08-25 20:23:09 +10001371 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 if (!dev_priv->cp_running)
1374 return 0;
1375
1376 /* Flush any pending CP commands. This ensures any outstanding
1377 * commands are exectuted by the engine before we turn it off.
1378 */
Eric Anholtc153f452007-09-03 12:06:45 +10001379 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001380 radeon_do_cp_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
1382
1383 /* If we fail to make the engine go idle, we return an error
1384 * code so that the DRM ioctl wrapper can try again.
1385 */
Eric Anholtc153f452007-09-03 12:06:45 +10001386 if (stop->idle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001387 ret = radeon_do_cp_idle(dev_priv);
1388 if (ret)
1389 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392 /* Finally, we can turn off the CP. If the engine isn't idle,
1393 * we will get some dropped triangles as they won't be fully
1394 * rendered before the CP is shut down.
1395 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001396 radeon_do_cp_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397
1398 /* Reset the engine */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001399 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400
1401 return 0;
1402}
1403
Dave Airlie84b1fd12007-07-11 15:53:27 +10001404void radeon_do_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
1406 drm_radeon_private_t *dev_priv = dev->dev_private;
1407 int i, ret;
1408
1409 if (dev_priv) {
1410 if (dev_priv->cp_running) {
1411 /* Stop the cp */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001412 while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1414#ifdef __linux__
1415 schedule();
1416#else
1417 tsleep(&ret, PZERO, "rdnrel", 1);
1418#endif
1419 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001420 radeon_do_cp_stop(dev_priv);
1421 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
1423
1424 /* Disable *all* interrupts */
1425 if (dev_priv->mmio) /* remove this after permanent addmaps */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001426 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001428 if (dev_priv->mmio) { /* remove all surfaces */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
1431 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
1432 16 * i, 0);
1433 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
1434 16 * i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 }
1436 }
1437
1438 /* Free memory heap structures */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001439 radeon_mem_takedown(&(dev_priv->gart_heap));
1440 radeon_mem_takedown(&(dev_priv->fb_heap));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
1442 /* deallocate kernel resources */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001443 radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 }
1445}
1446
1447/* Just reset the CP ring. Called as part of an X Server engine reset.
1448 */
Eric Anholtc153f452007-09-03 12:06:45 +10001449int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001452 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Eric Anholt6c340ea2007-08-25 20:23:09 +10001454 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001456 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001457 DRM_DEBUG("called before init done\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001458 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 }
1460
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001461 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /* The CP is no longer running after an engine reset */
1464 dev_priv->cp_running = 0;
1465
1466 return 0;
1467}
1468
Eric Anholtc153f452007-09-03 12:06:45 +10001469int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001472 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Eric Anholt6c340ea2007-08-25 20:23:09 +10001474 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001476 return radeon_do_cp_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
1479/* Added by Charl P. Botha to call radeon_do_resume_cp().
1480 */
Eric Anholtc153f452007-09-03 12:06:45 +10001481int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 return radeon_do_resume_cp(dev);
1485}
1486
Eric Anholtc153f452007-09-03 12:06:45 +10001487int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001489 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Eric Anholt6c340ea2007-08-25 20:23:09 +10001491 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001493 return radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496/* ================================================================
1497 * Fullscreen mode
1498 */
1499
1500/* KW: Deprecated to say the least:
1501 */
Eric Anholtc153f452007-09-03 12:06:45 +10001502int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503{
1504 return 0;
1505}
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507/* ================================================================
1508 * Freelist management
1509 */
1510
1511/* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1512 * bufs until freelist code is used. Note this hides a problem with
1513 * the scratch register * (used to keep track of last buffer
1514 * completed) being written to before * the last buffer has actually
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001515 * completed rendering.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 *
1517 * KW: It's also a good way to find free buffers quickly.
1518 *
1519 * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1520 * sleep. However, bugs in older versions of radeon_accel.c mean that
1521 * we essentially have to do this, else old clients will break.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001522 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 * However, it does leave open a potential deadlock where all the
1524 * buffers are held by other clients, which can't release them because
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001525 * they can't get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
1527
Dave Airlie056219e2007-07-11 16:17:42 +10001528struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Dave Airliecdd55a22007-07-11 16:32:08 +10001530 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 drm_radeon_private_t *dev_priv = dev->dev_private;
1532 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001533 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int i, t;
1535 int start;
1536
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001537 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 dev_priv->last_buf = 0;
1539
1540 start = dev_priv->last_buf;
1541
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001542 for (t = 0; t < dev_priv->usec_timeout; t++) {
1543 u32 done_age = GET_SCRATCH(1);
1544 DRM_DEBUG("done_age = %d\n", done_age);
1545 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 buf = dma->buflist[i];
1547 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001548 if (buf->file_priv == NULL || (buf->pending &&
1549 buf_priv->age <=
1550 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 dev_priv->stats.requested_bufs++;
1552 buf->pending = 0;
1553 return buf;
1554 }
1555 start = 0;
1556 }
1557
1558 if (t) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001559 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 dev_priv->stats.freelist_loops++;
1561 }
1562 }
1563
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001564 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 return NULL;
1566}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568#if 0
Dave Airlie056219e2007-07-11 16:17:42 +10001569struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Dave Airliecdd55a22007-07-11 16:32:08 +10001571 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 drm_radeon_private_t *dev_priv = dev->dev_private;
1573 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001574 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 int i, t;
1576 int start;
1577 u32 done_age = DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1));
1578
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001579 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 dev_priv->last_buf = 0;
1581
1582 start = dev_priv->last_buf;
1583 dev_priv->stats.freelist_loops++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001584
1585 for (t = 0; t < 2; t++) {
1586 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 buf = dma->buflist[i];
1588 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001589 if (buf->file_priv == 0 || (buf->pending &&
1590 buf_priv->age <=
1591 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 dev_priv->stats.requested_bufs++;
1593 buf->pending = 0;
1594 return buf;
1595 }
1596 }
1597 start = 0;
1598 }
1599
1600 return NULL;
1601}
1602#endif
1603
Dave Airlie84b1fd12007-07-11 15:53:27 +10001604void radeon_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Dave Airliecdd55a22007-07-11 16:32:08 +10001606 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 drm_radeon_private_t *dev_priv = dev->dev_private;
1608 int i;
1609
1610 dev_priv->last_buf = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001611 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001612 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1614 buf_priv->age = 0;
1615 }
1616}
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618/* ================================================================
1619 * CP command submission
1620 */
1621
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001622int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1625 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001626 u32 last_head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001628 for (i = 0; i < dev_priv->usec_timeout; i++) {
1629 u32 head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 ring->space = (head - ring->tail) * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001632 if (ring->space <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 ring->space += ring->size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001634 if (ring->space > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
1638
1639 if (head != last_head)
1640 i = 0;
1641 last_head = head;
1642
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001643 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 }
1645
1646 /* FIXME: This return value is ignored in the BEGIN_RING macro! */
1647#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001648 radeon_status(dev_priv);
1649 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650#endif
Eric Anholt20caafa2007-08-25 19:22:43 +10001651 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652}
1653
Eric Anholt6c340ea2007-08-25 20:23:09 +10001654static int radeon_cp_get_buffers(struct drm_device *dev,
1655 struct drm_file *file_priv,
Dave Airliec60ce622007-07-11 15:27:12 +10001656 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001659 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001661 for (i = d->granted_count; i < d->request_count; i++) {
1662 buf = radeon_freelist_get(dev);
1663 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001664 return -EBUSY; /* NOTE: broken client */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Eric Anholt6c340ea2007-08-25 20:23:09 +10001666 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001668 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
1669 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001670 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001671 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
1672 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001673 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 d->granted_count++;
1676 }
1677 return 0;
1678}
1679
Eric Anholtc153f452007-09-03 12:06:45 +10001680int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Dave Airliecdd55a22007-07-11 16:32:08 +10001682 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001684 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Eric Anholt6c340ea2007-08-25 20:23:09 +10001686 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 /* Please don't send us buffers.
1689 */
Eric Anholtc153f452007-09-03 12:06:45 +10001690 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001691 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001692 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001693 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695
1696 /* We'll send you buffers.
1697 */
Eric Anholtc153f452007-09-03 12:06:45 +10001698 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001699 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001700 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001701 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 }
1703
Eric Anholtc153f452007-09-03 12:06:45 +10001704 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
Eric Anholtc153f452007-09-03 12:06:45 +10001706 if (d->request_count) {
1707 ret = radeon_cp_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return ret;
1711}
1712
Dave Airlie22eae942005-11-10 22:16:34 +11001713int radeon_driver_load(struct drm_device *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
1715 drm_radeon_private_t *dev_priv;
1716 int ret = 0;
1717
1718 dev_priv = drm_alloc(sizeof(drm_radeon_private_t), DRM_MEM_DRIVER);
1719 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10001720 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 memset(dev_priv, 0, sizeof(drm_radeon_private_t));
1723 dev->dev_private = (void *)dev_priv;
1724 dev_priv->flags = flags;
1725
Dave Airlie54a56ac2006-09-22 04:25:09 +10001726 switch (flags & RADEON_FAMILY_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 case CHIP_R100:
1728 case CHIP_RV200:
1729 case CHIP_R200:
1730 case CHIP_R300:
Dave Airlieb15ec362006-08-19 17:43:52 +10001731 case CHIP_R350:
Dave Airlie414ed532005-08-16 20:43:16 +10001732 case CHIP_R420:
Alex Deucheredc6f382008-10-17 09:21:45 +10001733 case CHIP_R423:
Dave Airlieb15ec362006-08-19 17:43:52 +10001734 case CHIP_RV410:
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001735 case CHIP_RV515:
1736 case CHIP_R520:
1737 case CHIP_RV570:
1738 case CHIP_R580:
Dave Airlie54a56ac2006-09-22 04:25:09 +10001739 dev_priv->flags |= RADEON_HAS_HIERZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 break;
1741 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001742 /* all other chips have no hierarchical z buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 break;
1744 }
Dave Airlie414ed532005-08-16 20:43:16 +10001745
1746 if (drm_device_is_agp(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001747 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlieb15ec362006-08-19 17:43:52 +10001748 else if (drm_device_is_pcie(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001749 dev_priv->flags |= RADEON_IS_PCIE;
Dave Airlieb15ec362006-08-19 17:43:52 +10001750 else
Dave Airlie54a56ac2006-09-22 04:25:09 +10001751 dev_priv->flags |= RADEON_IS_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +10001752
Dave Airlie414ed532005-08-16 20:43:16 +10001753 DRM_DEBUG("%s card detected\n",
Dave Airlie54a56ac2006-09-22 04:25:09 +10001754 ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return ret;
1756}
1757
Dave Airlie22eae942005-11-10 22:16:34 +11001758/* Create mappings for registers and framebuffer so userland doesn't necessarily
1759 * have to find them.
1760 */
1761int radeon_driver_firstopen(struct drm_device *dev)
Dave Airlie836cf042005-07-10 19:27:04 +10001762{
1763 int ret;
1764 drm_local_map_t *map;
1765 drm_radeon_private_t *dev_priv = dev->dev_private;
1766
Dave Airlief2b04cd2007-05-08 15:19:23 +10001767 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
1768
Dave Airlie836cf042005-07-10 19:27:04 +10001769 ret = drm_addmap(dev, drm_get_resource_start(dev, 2),
1770 drm_get_resource_len(dev, 2), _DRM_REGISTERS,
1771 _DRM_READ_ONLY, &dev_priv->mmio);
1772 if (ret != 0)
1773 return ret;
1774
Dave Airlie7fc86862007-11-05 10:45:27 +10001775 dev_priv->fb_aper_offset = drm_get_resource_start(dev, 0);
1776 ret = drm_addmap(dev, dev_priv->fb_aper_offset,
Dave Airlie836cf042005-07-10 19:27:04 +10001777 drm_get_resource_len(dev, 0), _DRM_FRAME_BUFFER,
1778 _DRM_WRITE_COMBINING, &map);
1779 if (ret != 0)
1780 return ret;
1781
1782 return 0;
1783}
1784
Dave Airlie22eae942005-11-10 22:16:34 +11001785int radeon_driver_unload(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 drm_radeon_private_t *dev_priv = dev->dev_private;
1788
1789 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER);
1791
1792 dev->dev_private = NULL;
1793 return 0;
1794}