blob: 34c0b3f0c29ecac69768a4bf237fc668ad21c1f5 [file] [log] [blame]
Dave Airlief26c4732006-01-02 17:18:39 +11001/* radeon_cp.c -- CP support for Radeon -*- linux-c -*- */
2/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
4 * Copyright 2000 VA Linux Systems, Inc., Fremont, California.
Alex Deucher45e51902008-05-28 13:28:59 +10005 * Copyright 2007 Advanced Micro Devices, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors:
28 * Kevin E. Martin <martin@valinux.com>
29 * Gareth Hughes <gareth@valinux.com>
30 */
31
32#include "drmP.h"
33#include "drm.h"
Dave Airlie7c1c2872008-11-28 14:22:24 +100034#include "drm_sarea.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "radeon_drm.h"
36#include "radeon_drv.h"
Dave Airlie414ed532005-08-16 20:43:16 +100037#include "r300_reg.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Alex Deucher9f184092008-05-28 11:21:25 +100039#include "radeon_microcode.h"
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#define RADEON_FIFO_DEBUG 0
42
Dave Airlie84b1fd12007-07-11 15:53:27 +100043static int radeon_do_cleanup_cp(struct drm_device * dev);
Jerome Glisse54f961a2008-08-13 09:46:31 +100044static void radeon_do_cp_start(drm_radeon_private_t * dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Alex Deucher45e51902008-05-28 13:28:59 +100046static u32 R500_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
Dave Airlie3d5e2c12008-02-07 15:01:05 +100047{
48 u32 ret;
49 RADEON_WRITE(R520_MC_IND_INDEX, 0x7f0000 | (addr & 0xff));
50 ret = RADEON_READ(R520_MC_IND_DATA);
51 RADEON_WRITE(R520_MC_IND_INDEX, 0);
52 return ret;
53}
54
Alex Deucher45e51902008-05-28 13:28:59 +100055static u32 RS480_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
56{
57 u32 ret;
58 RADEON_WRITE(RS480_NB_MC_INDEX, addr & 0xff);
59 ret = RADEON_READ(RS480_NB_MC_DATA);
60 RADEON_WRITE(RS480_NB_MC_INDEX, 0xff);
61 return ret;
62}
63
Maciej Cencora60f92682008-02-19 21:32:45 +100064static u32 RS690_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
65{
Alex Deucher45e51902008-05-28 13:28:59 +100066 u32 ret;
Maciej Cencora60f92682008-02-19 21:32:45 +100067 RADEON_WRITE(RS690_MC_INDEX, (addr & RS690_MC_INDEX_MASK));
Alex Deucher45e51902008-05-28 13:28:59 +100068 ret = RADEON_READ(RS690_MC_DATA);
69 RADEON_WRITE(RS690_MC_INDEX, RS690_MC_INDEX_MASK);
70 return ret;
71}
72
73static u32 IGP_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
74{
Alex Deucherf0738e92008-10-16 17:12:02 +100075 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
76 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +100077 return RS690_READ_MCIND(dev_priv, addr);
78 else
79 return RS480_READ_MCIND(dev_priv, addr);
Maciej Cencora60f92682008-02-19 21:32:45 +100080}
81
Dave Airlie3d5e2c12008-02-07 15:01:05 +100082u32 radeon_read_fb_location(drm_radeon_private_t *dev_priv)
83{
84
85 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100086 return R500_READ_MCIND(dev_priv, RV515_MC_FB_LOCATION);
Alex Deucherf0738e92008-10-16 17:12:02 +100087 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
88 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +100089 return RS690_READ_MCIND(dev_priv, RS690_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100090 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100091 return R500_READ_MCIND(dev_priv, R520_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +100092 else
93 return RADEON_READ(RADEON_MC_FB_LOCATION);
94}
95
96static void radeon_write_fb_location(drm_radeon_private_t *dev_priv, u32 fb_loc)
97{
98 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +100099 R500_WRITE_MCIND(RV515_MC_FB_LOCATION, fb_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000100 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
101 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000102 RS690_WRITE_MCIND(RS690_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000103 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000104 R500_WRITE_MCIND(R520_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000105 else
106 RADEON_WRITE(RADEON_MC_FB_LOCATION, fb_loc);
107}
108
109static void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_loc)
110{
111 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000112 R500_WRITE_MCIND(RV515_MC_AGP_LOCATION, agp_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000113 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
114 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000115 RS690_WRITE_MCIND(RS690_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000116 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000117 R500_WRITE_MCIND(R520_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000118 else
119 RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc);
120}
121
Dave Airlie70b13d52008-06-19 11:40:44 +1000122static void radeon_write_agp_base(drm_radeon_private_t *dev_priv, u64 agp_base)
123{
124 u32 agp_base_hi = upper_32_bits(agp_base);
125 u32 agp_base_lo = agp_base & 0xffffffff;
126
127 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) {
128 R500_WRITE_MCIND(RV515_MC_AGP_BASE, agp_base_lo);
129 R500_WRITE_MCIND(RV515_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherf0738e92008-10-16 17:12:02 +1000130 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
131 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000132 RS690_WRITE_MCIND(RS690_MC_AGP_BASE, agp_base_lo);
133 RS690_WRITE_MCIND(RS690_MC_AGP_BASE_2, agp_base_hi);
134 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515) {
135 R500_WRITE_MCIND(R520_MC_AGP_BASE, agp_base_lo);
136 R500_WRITE_MCIND(R520_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000137 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
138 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Alex Deucher5cfb6952008-06-19 12:38:29 +1000139 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000140 RADEON_WRITE(RS480_AGP_BASE_2, agp_base_hi);
Dave Airlie70b13d52008-06-19 11:40:44 +1000141 } else {
142 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
143 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200)
144 RADEON_WRITE(RADEON_AGP_BASE_2, agp_base_hi);
145 }
146}
147
Dave Airlie84b1fd12007-07-11 15:53:27 +1000148static int RADEON_READ_PLL(struct drm_device * dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149{
150 drm_radeon_private_t *dev_priv = dev->dev_private;
151
152 RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
153 return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
154}
155
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000156static u32 RADEON_READ_PCIE(drm_radeon_private_t *dev_priv, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Dave Airlieea98a922005-09-11 20:28:11 +1000158 RADEON_WRITE8(RADEON_PCIE_INDEX, addr & 0xff);
159 return RADEON_READ(RADEON_PCIE_DATA);
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000163static void radeon_status(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700165 printk("%s:\n", __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000166 printk("RBBM_STATUS = 0x%08x\n",
167 (unsigned int)RADEON_READ(RADEON_RBBM_STATUS));
168 printk("CP_RB_RTPR = 0x%08x\n",
169 (unsigned int)RADEON_READ(RADEON_CP_RB_RPTR));
170 printk("CP_RB_WTPR = 0x%08x\n",
171 (unsigned int)RADEON_READ(RADEON_CP_RB_WPTR));
172 printk("AIC_CNTL = 0x%08x\n",
173 (unsigned int)RADEON_READ(RADEON_AIC_CNTL));
174 printk("AIC_STAT = 0x%08x\n",
175 (unsigned int)RADEON_READ(RADEON_AIC_STAT));
176 printk("AIC_PT_BASE = 0x%08x\n",
177 (unsigned int)RADEON_READ(RADEON_AIC_PT_BASE));
178 printk("TLB_ADDR = 0x%08x\n",
179 (unsigned int)RADEON_READ(RADEON_AIC_TLB_ADDR));
180 printk("TLB_DATA = 0x%08x\n",
181 (unsigned int)RADEON_READ(RADEON_AIC_TLB_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183#endif
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/* ================================================================
186 * Engine, FIFO control
187 */
188
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000189static int radeon_do_pixcache_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 u32 tmp;
192 int i;
193
194 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
195
Alex Deucher259434a2008-05-28 11:51:12 +1000196 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV280) {
197 tmp = RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT);
198 tmp |= RADEON_RB3D_DC_FLUSH_ALL;
199 RADEON_WRITE(RADEON_RB3D_DSTCACHE_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Alex Deucher259434a2008-05-28 11:51:12 +1000201 for (i = 0; i < dev_priv->usec_timeout; i++) {
202 if (!(RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT)
203 & RADEON_RB3D_DC_BUSY)) {
204 return 0;
205 }
206 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
Alex Deucher259434a2008-05-28 11:51:12 +1000208 } else {
Jerome Glisse54f961a2008-08-13 09:46:31 +1000209 /* don't flush or purge cache here or lockup */
210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 }
212
213#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 DRM_ERROR("failed!\n");
215 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000217 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218}
219
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000220static int radeon_do_wait_for_fifo(drm_radeon_private_t * dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
222 int i;
223
224 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
225
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000226 for (i = 0; i < dev_priv->usec_timeout; i++) {
227 int slots = (RADEON_READ(RADEON_RBBM_STATUS)
228 & RADEON_RBBM_FIFOCNT_MASK);
229 if (slots >= entries)
230 return 0;
231 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000233 DRM_DEBUG("wait for fifo failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000234 RADEON_READ(RADEON_RBBM_STATUS),
235 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000238 DRM_ERROR("failed!\n");
239 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000241 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242}
243
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244static int radeon_do_wait_for_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int i, ret;
247
248 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
249
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000250 ret = radeon_do_wait_for_fifo(dev_priv, 64);
251 if (ret)
252 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 for (i = 0; i < dev_priv->usec_timeout; i++) {
255 if (!(RADEON_READ(RADEON_RBBM_STATUS)
256 & RADEON_RBBM_ACTIVE)) {
257 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
259 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000260 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000262 DRM_DEBUG("wait idle failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000263 RADEON_READ(RADEON_RBBM_STATUS),
264 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
266#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000267 DRM_ERROR("failed!\n");
268 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000270 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Alex Deucher5b92c402008-05-28 11:57:40 +1000273static void radeon_init_pipes(drm_radeon_private_t *dev_priv)
274{
275 uint32_t gb_tile_config, gb_pipe_sel = 0;
276
277 /* RS4xx/RS6xx/R4xx/R5xx */
278 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R420) {
279 gb_pipe_sel = RADEON_READ(R400_GB_PIPE_SELECT);
280 dev_priv->num_gb_pipes = ((gb_pipe_sel >> 12) & 0x3) + 1;
281 } else {
282 /* R3xx */
283 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
284 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350)) {
285 dev_priv->num_gb_pipes = 2;
286 } else {
287 /* R3Vxx */
288 dev_priv->num_gb_pipes = 1;
289 }
290 }
291 DRM_INFO("Num pipes: %d\n", dev_priv->num_gb_pipes);
292
293 gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16 /*| R300_SUBPIXEL_1_16*/);
294
295 switch (dev_priv->num_gb_pipes) {
296 case 2: gb_tile_config |= R300_PIPE_COUNT_R300; break;
297 case 3: gb_tile_config |= R300_PIPE_COUNT_R420_3P; break;
298 case 4: gb_tile_config |= R300_PIPE_COUNT_R420; break;
299 default:
300 case 1: gb_tile_config |= R300_PIPE_COUNT_RV350; break;
301 }
302
303 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV515) {
304 RADEON_WRITE_PLL(R500_DYN_SCLK_PWMEM_PIPE, (1 | ((gb_pipe_sel >> 8) & 0xf) << 4));
305 RADEON_WRITE(R500_SU_REG_DEST, ((1 << dev_priv->num_gb_pipes) - 1));
306 }
307 RADEON_WRITE(R300_GB_TILE_CONFIG, gb_tile_config);
308 radeon_do_wait_for_idle(dev_priv);
309 RADEON_WRITE(R300_DST_PIPE_CONFIG, RADEON_READ(R300_DST_PIPE_CONFIG) | R300_PIPE_AUTO_CONFIG);
310 RADEON_WRITE(R300_RB2D_DSTCACHE_MODE, (RADEON_READ(R300_RB2D_DSTCACHE_MODE) |
311 R300_DC_AUTOFLUSH_ENABLE |
312 R300_DC_DC_DISABLE_IGNORE_PE));
313
314
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317/* ================================================================
318 * CP control, initialization
319 */
320
321/* Load the microcode for the CP */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322static void radeon_cp_load_microcode(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 RADEON_WRITE(RADEON_CP_ME_RAM_ADDR, 0);
Alex Deucher9f184092008-05-28 11:21:25 +1000330 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R100) ||
331 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV100) ||
332 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV200) ||
333 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS100) ||
334 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS200)) {
335 DRM_INFO("Loading R100 Microcode\n");
336 for (i = 0; i < 256; i++) {
337 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
338 R100_cp_microcode[i][1]);
339 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
340 R100_cp_microcode[i][0]);
341 }
342 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R200) ||
343 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV250) ||
344 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV280) ||
345 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS300)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 DRM_INFO("Loading R200 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 for (i = 0; i < 256; i++) {
348 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
349 R200_cp_microcode[i][1]);
350 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
351 R200_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
Alex Deucher9f184092008-05-28 11:21:25 +1000353 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
354 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350) ||
355 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV350) ||
356 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000357 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000358 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 DRM_INFO("Loading R300 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000360 for (i = 0; i < 256; i++) {
361 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
362 R300_cp_microcode[i][1]);
363 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
364 R300_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
Alex Deucher9f184092008-05-28 11:21:25 +1000366 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
Alex Deucheredc6f382008-10-17 09:21:45 +1000367 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R423) ||
Alex Deucher9f184092008-05-28 11:21:25 +1000368 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV410)) {
369 DRM_INFO("Loading R400 Microcode\n");
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000370 for (i = 0; i < 256; i++) {
371 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
Alex Deucher9f184092008-05-28 11:21:25 +1000372 R420_cp_microcode[i][1]);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
Alex Deucher9f184092008-05-28 11:21:25 +1000374 R420_cp_microcode[i][0]);
375 }
Alex Deucherf0738e92008-10-16 17:12:02 +1000376 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
377 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
378 DRM_INFO("Loading RS690/RS740 Microcode\n");
Alex Deucher9f184092008-05-28 11:21:25 +1000379 for (i = 0; i < 256; i++) {
380 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
381 RS690_cp_microcode[i][1]);
382 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
383 RS690_cp_microcode[i][0]);
384 }
385 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) ||
386 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R520) ||
387 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) ||
388 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R580) ||
389 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV560) ||
390 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV570)) {
391 DRM_INFO("Loading R500 Microcode\n");
392 for (i = 0; i < 256; i++) {
393 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
394 R520_cp_microcode[i][1]);
395 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
396 R520_cp_microcode[i][0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398 }
399}
400
401/* Flush any pending commands to the CP. This should only be used just
402 * prior to a wait for idle, as it informs the engine that the command
403 * stream is ending.
404 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000405static void radeon_do_cp_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408#if 0
409 u32 tmp;
410
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000411 tmp = RADEON_READ(RADEON_CP_RB_WPTR) | (1 << 31);
412 RADEON_WRITE(RADEON_CP_RB_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#endif
414}
415
416/* Wait for the CP to go idle.
417 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418int radeon_do_cp_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000421 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000423 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425 RADEON_PURGE_CACHE();
426 RADEON_PURGE_ZCACHE();
427 RADEON_WAIT_UNTIL_IDLE();
428
429 ADVANCE_RING();
430 COMMIT_RING();
431
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 return radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
435/* Start the Command Processor.
436 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000437static void radeon_do_cp_start(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000440 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444 RADEON_WRITE(RADEON_CP_CSQ_CNTL, dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 dev_priv->cp_running = 1;
447
Jerome Glisse54f961a2008-08-13 09:46:31 +1000448 BEGIN_RING(8);
449 /* isync can only be written through cp on r5xx write it here */
450 OUT_RING(CP_PACKET0(RADEON_ISYNC_CNTL, 0));
451 OUT_RING(RADEON_ISYNC_ANY2D_IDLE3D |
452 RADEON_ISYNC_ANY3D_IDLE2D |
453 RADEON_ISYNC_WAIT_IDLEGUI |
454 RADEON_ISYNC_CPSCRATCH_IDLEGUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 RADEON_PURGE_CACHE();
456 RADEON_PURGE_ZCACHE();
457 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 ADVANCE_RING();
459 COMMIT_RING();
Jerome Glisse54f961a2008-08-13 09:46:31 +1000460
461 dev_priv->track_flush |= RADEON_FLUSH_EMITED | RADEON_PURGE_EMITED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
464/* Reset the Command Processor. This will not flush any pending
465 * commands, so you must wait for the CP command stream to complete
466 * before calling this routine.
467 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468static void radeon_do_cp_reset(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 u32 cur_read_ptr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000471 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
474 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
475 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 dev_priv->ring.tail = cur_read_ptr;
477}
478
479/* Stop the Command Processor. This will not flush any pending
480 * commands, so you must flush the command stream and wait for the CP
481 * to go idle before calling this routine.
482 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000483static void radeon_do_cp_stop(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000487 RADEON_WRITE(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 dev_priv->cp_running = 0;
490}
491
492/* Reset the engine. This will stop the CP if it is running.
493 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000494static int radeon_do_engine_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 drm_radeon_private_t *dev_priv = dev->dev_private;
Alex Deucherd396db32008-05-28 11:54:06 +1000497 u32 clock_cntl_index = 0, mclk_cntl = 0, rbbm_soft_reset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000498 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Alex Deucherd396db32008-05-28 11:54:06 +1000502 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
503 /* may need something similar for newer chips */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000504 clock_cntl_index = RADEON_READ(RADEON_CLOCK_CNTL_INDEX);
505 mclk_cntl = RADEON_READ_PLL(dev, RADEON_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000507 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, (mclk_cntl |
508 RADEON_FORCEON_MCLKA |
509 RADEON_FORCEON_MCLKB |
510 RADEON_FORCEON_YCLKA |
511 RADEON_FORCEON_YCLKB |
512 RADEON_FORCEON_MC |
513 RADEON_FORCEON_AIC));
Alex Deucherd396db32008-05-28 11:54:06 +1000514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Alex Deucherd396db32008-05-28 11:54:06 +1000516 rbbm_soft_reset = RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Alex Deucherd396db32008-05-28 11:54:06 +1000518 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset |
519 RADEON_SOFT_RESET_CP |
520 RADEON_SOFT_RESET_HI |
521 RADEON_SOFT_RESET_SE |
522 RADEON_SOFT_RESET_RE |
523 RADEON_SOFT_RESET_PP |
524 RADEON_SOFT_RESET_E2 |
525 RADEON_SOFT_RESET_RB));
526 RADEON_READ(RADEON_RBBM_SOFT_RESET);
527 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset &
528 ~(RADEON_SOFT_RESET_CP |
529 RADEON_SOFT_RESET_HI |
530 RADEON_SOFT_RESET_SE |
531 RADEON_SOFT_RESET_RE |
532 RADEON_SOFT_RESET_PP |
533 RADEON_SOFT_RESET_E2 |
534 RADEON_SOFT_RESET_RB)));
535 RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Alex Deucherd396db32008-05-28 11:54:06 +1000537 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000538 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, mclk_cntl);
539 RADEON_WRITE(RADEON_CLOCK_CNTL_INDEX, clock_cntl_index);
540 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, rbbm_soft_reset);
541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Alex Deucher5b92c402008-05-28 11:57:40 +1000543 /* setup the raster pipes */
544 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R300)
545 radeon_init_pipes(dev_priv);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Reset the CP ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000548 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* The CP is no longer running after an engine reset */
551 dev_priv->cp_running = 0;
552
553 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 radeon_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556 return 0;
557}
558
Dave Airlie84b1fd12007-07-11 15:53:27 +1000559static void radeon_cp_init_ring_buffer(struct drm_device * dev,
etienne3d161182009-02-20 09:44:45 +1000560 drm_radeon_private_t *dev_priv,
561 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
etienne3d161182009-02-20 09:44:45 +1000563 struct drm_radeon_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 u32 ring_start, cur_read_ptr;
565 u32 tmp;
Dave Airliebc5f4522007-11-05 12:50:58 +1000566
Dave Airlied5ea7022006-03-19 19:37:55 +1100567 /* Initialize the memory controller. With new memory map, the fb location
568 * is not changed, it should have been properly initialized already. Part
569 * of the problem is that the code below is bogus, assuming the GART is
570 * always appended to the fb which is not necessarily the case
571 */
572 if (!dev_priv->new_memmap)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000573 radeon_write_fb_location(dev_priv,
Dave Airlied5ea7022006-03-19 19:37:55 +1100574 ((dev_priv->gart_vm_start - 1) & 0xffff0000)
575 | (dev_priv->fb_location >> 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000578 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000579 radeon_write_agp_base(dev_priv, dev->agp->base);
580
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000581 radeon_write_agp_location(dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 (((dev_priv->gart_vm_start - 1 +
583 dev_priv->gart_size) & 0xffff0000) |
584 (dev_priv->gart_vm_start >> 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586 ring_start = (dev_priv->cp_ring->offset
587 - dev->agp->base
588 + dev_priv->gart_vm_start);
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100589 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590#endif
591 ring_start = (dev_priv->cp_ring->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100592 - (unsigned long)dev->sg->virtual
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 + dev_priv->gart_vm_start);
594
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000595 RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 /* Set the write pointer delay */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 /* Initialize the ring buffer's read and write pointers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
602 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
603 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 dev_priv->ring.tail = cur_read_ptr;
605
606#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000607 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
609 dev_priv->ring_rptr->offset
610 - dev->agp->base + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 } else
612#endif
613 {
Dave Airlie55910512007-07-11 16:53:40 +1000614 struct drm_sg_mem *entry = dev->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 unsigned long tmp_ofs, page_ofs;
616
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100617 tmp_ofs = dev_priv->ring_rptr->offset -
618 (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 page_ofs = tmp_ofs >> PAGE_SHIFT;
620
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000621 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR, entry->busaddr[page_ofs]);
622 DRM_DEBUG("ring rptr: offset=0x%08lx handle=0x%08lx\n",
623 (unsigned long)entry->busaddr[page_ofs],
624 entry->handle + tmp_ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626
Dave Airlied5ea7022006-03-19 19:37:55 +1100627 /* Set ring buffer size */
628#ifdef __BIG_ENDIAN
629 RADEON_WRITE(RADEON_CP_RB_CNTL,
Roland Scheidegger576cc452008-02-07 14:59:24 +1000630 RADEON_BUF_SWAP_32BIT |
631 (dev_priv->ring.fetch_size_l2ow << 18) |
632 (dev_priv->ring.rptr_update_l2qw << 8) |
633 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100634#else
Roland Scheidegger576cc452008-02-07 14:59:24 +1000635 RADEON_WRITE(RADEON_CP_RB_CNTL,
636 (dev_priv->ring.fetch_size_l2ow << 18) |
637 (dev_priv->ring.rptr_update_l2qw << 8) |
638 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100639#endif
640
Dave Airlied5ea7022006-03-19 19:37:55 +1100641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 /* Initialize the scratch register pointer. This will cause
643 * the scratch register values to be written out to memory
644 * whenever they are updated.
645 *
646 * We simply put this behind the ring read pointer, this works
647 * with PCI GART as well as (whatever kind of) AGP GART
648 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000649 RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
650 + RADEON_SCRATCH_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 dev_priv->scratch = ((__volatile__ u32 *)
653 dev_priv->ring_rptr->handle +
654 (RADEON_SCRATCH_REG_OFFSET / sizeof(u32)));
655
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Dave Airlied5ea7022006-03-19 19:37:55 +1100658 /* Turn on bus mastering */
Alex Deucher4e270e92008-10-28 07:48:34 +1000659 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
Alex Deucheredc6f382008-10-17 09:21:45 +1000660 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
Alex Deucher4e270e92008-10-28 07:48:34 +1000661 /* rs600/rs690/rs740 */
662 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RS600_BUS_MASTER_DIS;
Alex Deucheredc6f382008-10-17 09:21:45 +1000663 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
Alex Deucher4e270e92008-10-28 07:48:34 +1000664 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV350) ||
665 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
666 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
667 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
668 /* r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
Alex Deucheredc6f382008-10-17 09:21:45 +1000669 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
670 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
671 } /* PCIE cards appears to not need this */
Dave Airlied5ea7022006-03-19 19:37:55 +1100672
Dave Airlie7c1c2872008-11-28 14:22:24 +1000673 dev_priv->scratch[0] = 0;
674 RADEON_WRITE(RADEON_LAST_FRAME_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100675
Dave Airlie7c1c2872008-11-28 14:22:24 +1000676 dev_priv->scratch[1] = 0;
677 RADEON_WRITE(RADEON_LAST_DISPATCH_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100678
Dave Airlie7c1c2872008-11-28 14:22:24 +1000679 dev_priv->scratch[2] = 0;
680 RADEON_WRITE(RADEON_LAST_CLEAR_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100681
etienne3d161182009-02-20 09:44:45 +1000682 /* reset sarea copies of these */
683 master_priv = file_priv->master->driver_priv;
684 if (master_priv->sarea_priv) {
685 master_priv->sarea_priv->last_frame = 0;
686 master_priv->sarea_priv->last_dispatch = 0;
687 master_priv->sarea_priv->last_clear = 0;
688 }
689
Dave Airlied5ea7022006-03-19 19:37:55 +1100690 radeon_do_wait_for_idle(dev_priv);
691
692 /* Sync everything up */
693 RADEON_WRITE(RADEON_ISYNC_CNTL,
694 (RADEON_ISYNC_ANY2D_IDLE3D |
695 RADEON_ISYNC_ANY3D_IDLE2D |
696 RADEON_ISYNC_WAIT_IDLEGUI |
697 RADEON_ISYNC_CPSCRATCH_IDLEGUI));
698
699}
700
701static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
702{
703 u32 tmp;
704
Dave Airlie6b79d522008-09-02 10:10:16 +1000705 /* Start with assuming that writeback doesn't work */
706 dev_priv->writeback_works = 0;
707
Dave Airlied5ea7022006-03-19 19:37:55 +1100708 /* Writeback doesn't seem to work everywhere, test it here and possibly
709 * enable it if it appears to work
710 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000711 DRM_WRITE32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1), 0);
712 RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
715 if (DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1)) ==
716 0xdeadbeef)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000721 if (tmp < dev_priv->usec_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 dev_priv->writeback_works = 1;
Dave Airlied5ea7022006-03-19 19:37:55 +1100723 DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 } else {
725 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100726 DRM_INFO("writeback test failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
Dave Airlie689b9d72005-09-30 17:09:07 +1000728 if (radeon_no_wb == 1) {
729 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100730 DRM_INFO("writeback forced off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
Michel Dänzerae1b1a482006-08-07 20:37:46 +1000732
733 if (!dev_priv->writeback_works) {
734 /* Disable writeback to avoid unnecessary bus master transfer */
735 RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
736 RADEON_RB_NO_UPDATE);
737 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Dave Airlief2b04cd2007-05-08 15:19:23 +1000741/* Enable or disable IGP GART on the chip */
742static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
743{
Maciej Cencora60f92682008-02-19 21:32:45 +1000744 u32 temp;
745
746 if (on) {
Alex Deucher45e51902008-05-28 13:28:59 +1000747 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
Maciej Cencora60f92682008-02-19 21:32:45 +1000748 dev_priv->gart_vm_start,
749 (long)dev_priv->gart_info.bus_addr,
750 dev_priv->gart_size);
751
Alex Deucher45e51902008-05-28 13:28:59 +1000752 temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
Alex Deucherf0738e92008-10-16 17:12:02 +1000753 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
754 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +1000755 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
756 RS690_BLOCK_GFX_D3_EN));
757 else
758 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
Maciej Cencora60f92682008-02-19 21:32:45 +1000759
Alex Deucher45e51902008-05-28 13:28:59 +1000760 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
761 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000762
Alex Deucher45e51902008-05-28 13:28:59 +1000763 temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
764 IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
765 RS480_TLB_ENABLE |
766 RS480_GTW_LAC_EN |
767 RS480_1LEVEL_GART));
Maciej Cencora60f92682008-02-19 21:32:45 +1000768
Dave Airliefa0d71b2008-05-28 11:27:01 +1000769 temp = dev_priv->gart_info.bus_addr & 0xfffff000;
770 temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
Alex Deucher45e51902008-05-28 13:28:59 +1000771 IGP_WRITE_MCIND(RS480_GART_BASE, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000772
Alex Deucher45e51902008-05-28 13:28:59 +1000773 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
774 IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
775 RS480_REQ_TYPE_SNOOP_DIS));
Maciej Cencora60f92682008-02-19 21:32:45 +1000776
Alex Deucher5cfb6952008-06-19 12:38:29 +1000777 radeon_write_agp_base(dev_priv, dev_priv->gart_vm_start);
Dave Airlie3722bfc2008-05-28 11:28:27 +1000778
Maciej Cencora60f92682008-02-19 21:32:45 +1000779 dev_priv->gart_size = 32*1024*1024;
780 temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
781 0xffff0000) | (dev_priv->gart_vm_start >> 16));
782
Alex Deucher45e51902008-05-28 13:28:59 +1000783 radeon_write_agp_location(dev_priv, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000784
Alex Deucher45e51902008-05-28 13:28:59 +1000785 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
786 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
787 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000788
789 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000790 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
791 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000792 break;
793 DRM_UDELAY(1);
794 } while (1);
795
Alex Deucher45e51902008-05-28 13:28:59 +1000796 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
797 RS480_GART_CACHE_INVALIDATE);
Alex Deucher27359772008-05-28 12:54:16 +1000798
Maciej Cencora60f92682008-02-19 21:32:45 +1000799 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000800 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
801 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000802 break;
803 DRM_UDELAY(1);
804 } while (1);
805
Alex Deucher45e51902008-05-28 13:28:59 +1000806 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000807 } else {
Alex Deucher45e51902008-05-28 13:28:59 +1000808 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000809 }
810}
811
Dave Airlieea98a922005-09-11 20:28:11 +1000812static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Dave Airlieea98a922005-09-11 20:28:11 +1000814 u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
815 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Dave Airlieea98a922005-09-11 20:28:11 +1000817 DRM_DEBUG("programming pcie %08X %08lX %08X\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000818 dev_priv->gart_vm_start,
819 (long)dev_priv->gart_info.bus_addr,
Dave Airlieea98a922005-09-11 20:28:11 +1000820 dev_priv->gart_size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
822 dev_priv->gart_vm_start);
823 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
824 dev_priv->gart_info.bus_addr);
825 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
826 dev_priv->gart_vm_start);
827 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
828 dev_priv->gart_vm_start +
829 dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000831 radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
834 RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000836 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
837 tmp & ~RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/* Enable or disable PCI GART on the chip */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Dave Airlied985c102006-01-02 21:32:48 +1100844 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Alex Deucher45e51902008-05-28 13:28:59 +1000846 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
Alex Deucherf0738e92008-10-16 17:12:02 +1000847 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000848 (dev_priv->flags & RADEON_IS_IGPGART)) {
Dave Airlief2b04cd2007-05-08 15:19:23 +1000849 radeon_set_igpgart(dev_priv, on);
850 return;
851 }
852
Dave Airlie54a56ac2006-09-22 04:25:09 +1000853 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieea98a922005-09-11 20:28:11 +1000854 radeon_set_pciegart(dev_priv, on);
855 return;
856 }
857
Dave Airliebc5f4522007-11-05 12:50:58 +1000858 tmp = RADEON_READ(RADEON_AIC_CNTL);
Dave Airlied985c102006-01-02 21:32:48 +1100859
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000860 if (on) {
861 RADEON_WRITE(RADEON_AIC_CNTL,
862 tmp | RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* set PCI GART page-table base address
865 */
Dave Airlieea98a922005-09-11 20:28:11 +1000866 RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
868 /* set address range for PCI address translate
869 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
871 RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
872 + dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 /* Turn off AGP aperture -- is this required for PCI GART?
875 */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000876 radeon_write_agp_location(dev_priv, 0xffffffc0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000877 RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 RADEON_WRITE(RADEON_AIC_CNTL,
880 tmp & ~RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 }
882}
883
Dave Airlie7c1c2872008-11-28 14:22:24 +1000884static int radeon_do_init_cp(struct drm_device *dev, drm_radeon_init_t *init,
885 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Dave Airlied985c102006-01-02 21:32:48 +1100887 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +1000888 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
Dave Airlied985c102006-01-02 21:32:48 +1100889
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Dave Airlief3dd5c32006-03-25 18:09:46 +1100892 /* if we require new memory map but we don't have it fail */
Dave Airlie54a56ac2006-09-22 04:25:09 +1000893 if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
Dave Airlieb15ec362006-08-19 17:43:52 +1000894 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 +1100895 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000896 return -EINVAL;
Dave Airlief3dd5c32006-03-25 18:09:46 +1100897 }
898
Dave Airlie54a56ac2006-09-22 04:25:09 +1000899 if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
Dave Airlied985c102006-01-02 21:32:48 +1100900 DRM_DEBUG("Forcing AGP card to PCI mode\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000901 dev_priv->flags &= ~RADEON_IS_AGP;
902 } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
Dave Airlieb15ec362006-08-19 17:43:52 +1000903 && !init->is_pci) {
904 DRM_DEBUG("Restoring AGP flag\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +1000905 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlied985c102006-01-02 21:32:48 +1100906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Dave Airlie54a56ac2006-09-22 04:25:09 +1000908 if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000909 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000911 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913
914 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000915 if (dev_priv->usec_timeout < 1 ||
916 dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
917 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000919 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
Dave Airlieddbee332007-07-11 12:16:01 +1000922 /* Enable vblank on CRTC1 for older X servers
923 */
924 dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
925
Dave Airlied985c102006-01-02 21:32:48 +1100926 switch(init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 case RADEON_INIT_R200_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 dev_priv->microcode_version = UCODE_R200;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 break;
930 case RADEON_INIT_R300_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000931 dev_priv->microcode_version = UCODE_R300;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000934 dev_priv->microcode_version = UCODE_R100;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 dev_priv->do_boxes = 0;
938 dev_priv->cp_mode = init->cp_mode;
939
940 /* We don't support anything other than bus-mastering ring mode,
941 * but the ring can be in either AGP or PCI space for the ring
942 * read pointer.
943 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000944 if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
945 (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
946 DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000948 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000951 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 case 16:
953 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
954 break;
955 case 32:
956 default:
957 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
958 break;
959 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000960 dev_priv->front_offset = init->front_offset;
961 dev_priv->front_pitch = init->front_pitch;
962 dev_priv->back_offset = init->back_offset;
963 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000965 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 case 16:
967 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
968 break;
969 case 32:
970 default:
971 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
972 break;
973 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000974 dev_priv->depth_offset = init->depth_offset;
975 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976
977 /* Hardware state for depth clears. Remove this if/when we no
978 * longer clear the depth buffer with a 3D rectangle. Hard-code
979 * all values to prevent unwanted 3D state from slipping through
980 * and screwing with the clear operation.
981 */
982 dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
983 (dev_priv->color_fmt << 10) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000984 (dev_priv->microcode_version ==
985 UCODE_R100 ? RADEON_ZBLOCK16 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000987 dev_priv->depth_clear.rb3d_zstencilcntl =
988 (dev_priv->depth_fmt |
989 RADEON_Z_TEST_ALWAYS |
990 RADEON_STENCIL_TEST_ALWAYS |
991 RADEON_STENCIL_S_FAIL_REPLACE |
992 RADEON_STENCIL_ZPASS_REPLACE |
993 RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
996 RADEON_BFACE_SOLID |
997 RADEON_FFACE_SOLID |
998 RADEON_FLAT_SHADE_VTX_LAST |
999 RADEON_DIFFUSE_SHADE_FLAT |
1000 RADEON_ALPHA_SHADE_FLAT |
1001 RADEON_SPECULAR_SHADE_FLAT |
1002 RADEON_FOG_SHADE_FLAT |
1003 RADEON_VTX_PIX_CENTER_OGL |
1004 RADEON_ROUND_MODE_TRUNC |
1005 RADEON_ROUND_PREC_8TH_PIX);
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 dev_priv->ring_offset = init->ring_offset;
1009 dev_priv->ring_rptr_offset = init->ring_rptr_offset;
1010 dev_priv->buffers_offset = init->buffers_offset;
1011 dev_priv->gart_textures_offset = init->gart_textures_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001012
Dave Airlie7c1c2872008-11-28 14:22:24 +10001013 master_priv->sarea = drm_getsarea(dev);
1014 if (!master_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 DRM_ERROR("could not find sarea!\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 }
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 dev_priv->cp_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001021 if (!dev_priv->cp_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 DRM_ERROR("could not find cp ring 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 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001027 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 DRM_ERROR("could not find ring read pointer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001030 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
Dave Airlied1f2b552005-08-05 22:11:22 +10001032 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001034 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 DRM_ERROR("could not find dma buffer region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001037 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040 if (init->gart_textures_offset) {
1041 dev_priv->gart_textures =
1042 drm_core_findmap(dev, init->gart_textures_offset);
1043 if (!dev_priv->gart_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 DRM_ERROR("could not find GART texture region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001046 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 }
1048 }
1049
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001051 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie9b8d5a12009-02-07 11:15:41 +10001052 drm_core_ioremap_wc(dev_priv->cp_ring, dev);
1053 drm_core_ioremap_wc(dev_priv->ring_rptr, dev);
1054 drm_core_ioremap_wc(dev->agp_buffer_map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001055 if (!dev_priv->cp_ring->handle ||
1056 !dev_priv->ring_rptr->handle ||
1057 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 DRM_ERROR("could not find ioremap agp regions!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001060 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 } else
1063#endif
1064 {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001065 dev_priv->cp_ring->handle =
1066 (void *)(unsigned long)dev_priv->cp_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 dev_priv->ring_rptr->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001068 (void *)(unsigned long)dev_priv->ring_rptr->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 dev->agp_buffer_map->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001070 (void *)(unsigned long)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001072 DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
1073 dev_priv->cp_ring->handle);
1074 DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
1075 dev_priv->ring_rptr->handle);
1076 DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
1077 dev->agp_buffer_map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001080 dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
Dave Airliebc5f4522007-11-05 12:50:58 +10001081 dev_priv->fb_size =
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001082 ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
Dave Airlied5ea7022006-03-19 19:37:55 +11001083 - dev_priv->fb_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001085 dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
1086 ((dev_priv->front_offset
1087 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001089 dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
1090 ((dev_priv->back_offset
1091 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001093 dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
1094 ((dev_priv->depth_offset
1095 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 dev_priv->gart_size = init->gart_size;
Dave Airlied5ea7022006-03-19 19:37:55 +11001098
1099 /* New let's set the memory map ... */
1100 if (dev_priv->new_memmap) {
1101 u32 base = 0;
1102
1103 DRM_INFO("Setting GART location based on new memory map\n");
1104
1105 /* If using AGP, try to locate the AGP aperture at the same
1106 * location in the card and on the bus, though we have to
1107 * align it down.
1108 */
1109#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001110 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001111 base = dev->agp->base;
1112 /* Check if valid */
Michel Dänzer80b2c382007-02-18 18:03:21 +11001113 if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
1114 base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001115 DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
1116 dev->agp->base);
1117 base = 0;
1118 }
1119 }
1120#endif
1121 /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
1122 if (base == 0) {
1123 base = dev_priv->fb_location + dev_priv->fb_size;
Michel Dänzer80b2c382007-02-18 18:03:21 +11001124 if (base < dev_priv->fb_location ||
1125 ((base + dev_priv->gart_size) & 0xfffffffful) < base)
Dave Airlied5ea7022006-03-19 19:37:55 +11001126 base = dev_priv->fb_location
1127 - dev_priv->gart_size;
Dave Airliebc5f4522007-11-05 12:50:58 +10001128 }
Dave Airlied5ea7022006-03-19 19:37:55 +11001129 dev_priv->gart_vm_start = base & 0xffc00000u;
1130 if (dev_priv->gart_vm_start != base)
1131 DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
1132 base, dev_priv->gart_vm_start);
1133 } else {
1134 DRM_INFO("Setting GART location based on old memory map\n");
1135 dev_priv->gart_vm_start = dev_priv->fb_location +
1136 RADEON_READ(RADEON_CONFIG_APER_SIZE);
1137 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
1139#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001140 if (dev_priv->flags & RADEON_IS_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001142 - dev->agp->base
1143 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 else
1145#endif
1146 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +01001147 - (unsigned long)dev->sg->virtual
1148 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001150 DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
1151 DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
1152 DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
1153 dev_priv->gart_buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001155 dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
1156 dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 + init->ring_size / sizeof(u32));
1158 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001159 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Roland Scheidegger576cc452008-02-07 14:59:24 +10001161 dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
1162 dev_priv->ring.rptr_update_l2qw = drm_order( /* init->rptr_update */ 4096 / 8);
1163
1164 dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
1165 dev_priv->ring.fetch_size_l2ow = drm_order( /* init->fetch_size */ 32 / 16);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001166 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1169
1170#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001171 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001173 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 } else
1175#endif
1176 {
Dave Airlieb05c2382008-03-17 10:24:24 +10001177 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +10001178 /* if we have an offset set from userspace */
Dave Airlief2b04cd2007-05-08 15:19:23 +10001179 if (dev_priv->pcigart_offset_set) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001180 dev_priv->gart_info.bus_addr =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001181 (resource_size_t)dev_priv->pcigart_offset + dev_priv->fb_location;
Dave Airlief26c4732006-01-02 17:18:39 +11001182 dev_priv->gart_info.mapping.offset =
Dave Airlie7fc86862007-11-05 10:45:27 +10001183 dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
Dave Airlief26c4732006-01-02 17:18:39 +11001184 dev_priv->gart_info.mapping.size =
Dave Airlief2b04cd2007-05-08 15:19:23 +10001185 dev_priv->gart_info.table_size;
Dave Airlief26c4732006-01-02 17:18:39 +11001186
Dave Airlie242e3df2008-07-15 15:48:05 +10001187 drm_core_ioremap_wc(&dev_priv->gart_info.mapping, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001188 dev_priv->gart_info.addr =
Dave Airlief26c4732006-01-02 17:18:39 +11001189 dev_priv->gart_info.mapping.handle;
Dave Airlieea98a922005-09-11 20:28:11 +10001190
Dave Airlief2b04cd2007-05-08 15:19:23 +10001191 if (dev_priv->flags & RADEON_IS_PCIE)
1192 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
1193 else
1194 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001195 dev_priv->gart_info.gart_table_location =
1196 DRM_ATI_GART_FB;
1197
Dave Airlief26c4732006-01-02 17:18:39 +11001198 DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001199 dev_priv->gart_info.addr,
1200 dev_priv->pcigart_offset);
1201 } else {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001202 if (dev_priv->flags & RADEON_IS_IGPGART)
1203 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
1204 else
1205 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001206 dev_priv->gart_info.gart_table_location =
1207 DRM_ATI_GART_MAIN;
Dave Airlief26c4732006-01-02 17:18:39 +11001208 dev_priv->gart_info.addr = NULL;
1209 dev_priv->gart_info.bus_addr = 0;
Dave Airlie54a56ac2006-09-22 04:25:09 +10001210 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001211 DRM_ERROR
1212 ("Cannot use PCI Express without GART in FB memory\n");
Dave Airlieea98a922005-09-11 20:28:11 +10001213 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001214 return -EINVAL;
Dave Airlieea98a922005-09-11 20:28:11 +10001215 }
1216 }
1217
1218 if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001219 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001221 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
1224 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001225 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001228 radeon_cp_load_microcode(dev_priv);
etienne3d161182009-02-20 09:44:45 +10001229 radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
1231 dev_priv->last_buf = 0;
1232
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001233 radeon_do_engine_reset(dev);
Dave Airlied5ea7022006-03-19 19:37:55 +11001234 radeon_test_writeback(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 return 0;
1237}
1238
Dave Airlie84b1fd12007-07-11 15:53:27 +10001239static int radeon_do_cleanup_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001242 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 /* Make sure interrupts are disabled here because the uninstall ioctl
1245 * may not have been called from userspace and after dev_private
1246 * is freed, it's too late.
1247 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001248 if (dev->irq_enabled)
1249 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001252 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied985c102006-01-02 21:32:48 +11001253 if (dev_priv->cp_ring != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001254 drm_core_ioremapfree(dev_priv->cp_ring, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001255 dev_priv->cp_ring = NULL;
1256 }
1257 if (dev_priv->ring_rptr != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001258 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001259 dev_priv->ring_rptr = NULL;
1260 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001261 if (dev->agp_buffer_map != NULL) {
1262 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 dev->agp_buffer_map = NULL;
1264 }
1265 } else
1266#endif
1267 {
Dave Airlied985c102006-01-02 21:32:48 +11001268
1269 if (dev_priv->gart_info.bus_addr) {
1270 /* Turn off PCI GART */
1271 radeon_set_pcigart(dev_priv, 0);
Dave Airlieea98a922005-09-11 20:28:11 +10001272 if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
1273 DRM_ERROR("failed to cleanup PCI GART!\n");
Dave Airlied985c102006-01-02 21:32:48 +11001274 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001275
Dave Airlied985c102006-01-02 21:32:48 +11001276 if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
1277 {
Dave Airlief26c4732006-01-02 17:18:39 +11001278 drm_core_ioremapfree(&dev_priv->gart_info.mapping, dev);
Dave Airlief2b04cd2007-05-08 15:19:23 +10001279 dev_priv->gart_info.addr = 0;
Dave Airlieea98a922005-09-11 20:28:11 +10001280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 /* only clear to the start of flags */
1283 memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
1284
1285 return 0;
1286}
1287
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001288/* This code will reinit the Radeon CP hardware after a resume from disc.
1289 * AFAIK, it would be very difficult to pickle the state at suspend time, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 * here we make sure that all Radeon hardware initialisation is re-done without
1291 * affecting running applications.
1292 *
1293 * Charl P. Botha <http://cpbotha.net>
1294 */
etienne3d161182009-02-20 09:44:45 +10001295static int radeon_do_resume_cp(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 drm_radeon_private_t *dev_priv = dev->dev_private;
1298
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001299 if (!dev_priv) {
1300 DRM_ERROR("Called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001301 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 }
1303
1304 DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1305
1306#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001307 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001309 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 } else
1311#endif
1312 {
1313 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001317 radeon_cp_load_microcode(dev_priv);
etienne3d161182009-02-20 09:44:45 +10001318 radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 radeon_do_engine_reset(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001321 radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 DRM_DEBUG("radeon_do_resume_cp() complete\n");
1324
1325 return 0;
1326}
1327
Eric Anholtc153f452007-09-03 12:06:45 +10001328int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Eric Anholtc153f452007-09-03 12:06:45 +10001330 drm_radeon_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Eric Anholt6c340ea2007-08-25 20:23:09 +10001332 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Eric Anholtc153f452007-09-03 12:06:45 +10001334 if (init->func == RADEON_INIT_R300_CP)
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001335 r300_init_reg_flags(dev);
Dave Airlie414ed532005-08-16 20:43:16 +10001336
Eric Anholtc153f452007-09-03 12:06:45 +10001337 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 case RADEON_INIT_CP:
1339 case RADEON_INIT_R200_CP:
1340 case RADEON_INIT_R300_CP:
Dave Airlie7c1c2872008-11-28 14:22:24 +10001341 return radeon_do_init_cp(dev, init, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 case RADEON_CLEANUP_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001343 return radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345
Eric Anholt20caafa2007-08-25 19:22:43 +10001346 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347}
1348
Eric Anholtc153f452007-09-03 12:06:45 +10001349int radeon_cp_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001352 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Eric Anholt6c340ea2007-08-25 20:23:09 +10001354 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001356 if (dev_priv->cp_running) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001357 DRM_DEBUG("while CP running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return 0;
1359 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001360 if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001361 DRM_DEBUG("called with bogus CP mode (%d)\n",
1362 dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 return 0;
1364 }
1365
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001366 radeon_do_cp_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
1368 return 0;
1369}
1370
1371/* Stop the CP. The engine must have been idled before calling this
1372 * routine.
1373 */
Eric Anholtc153f452007-09-03 12:06:45 +10001374int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001377 drm_radeon_cp_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001379 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Eric Anholt6c340ea2007-08-25 20:23:09 +10001381 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!dev_priv->cp_running)
1384 return 0;
1385
1386 /* Flush any pending CP commands. This ensures any outstanding
1387 * commands are exectuted by the engine before we turn it off.
1388 */
Eric Anholtc153f452007-09-03 12:06:45 +10001389 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001390 radeon_do_cp_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
1393 /* If we fail to make the engine go idle, we return an error
1394 * code so that the DRM ioctl wrapper can try again.
1395 */
Eric Anholtc153f452007-09-03 12:06:45 +10001396 if (stop->idle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001397 ret = radeon_do_cp_idle(dev_priv);
1398 if (ret)
1399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
1402 /* Finally, we can turn off the CP. If the engine isn't idle,
1403 * we will get some dropped triangles as they won't be fully
1404 * rendered before the CP is shut down.
1405 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001406 radeon_do_cp_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 /* Reset the engine */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001409 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
1411 return 0;
1412}
1413
Dave Airlie84b1fd12007-07-11 15:53:27 +10001414void radeon_do_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415{
1416 drm_radeon_private_t *dev_priv = dev->dev_private;
1417 int i, ret;
1418
1419 if (dev_priv) {
1420 if (dev_priv->cp_running) {
1421 /* Stop the cp */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001422 while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1424#ifdef __linux__
1425 schedule();
1426#else
1427 tsleep(&ret, PZERO, "rdnrel", 1);
1428#endif
1429 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001430 radeon_do_cp_stop(dev_priv);
1431 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 }
1433
1434 /* Disable *all* interrupts */
1435 if (dev_priv->mmio) /* remove this after permanent addmaps */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001436 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001438 if (dev_priv->mmio) { /* remove all surfaces */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001440 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
1441 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
1442 16 * i, 0);
1443 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
1444 16 * i, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446 }
1447
1448 /* Free memory heap structures */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001449 radeon_mem_takedown(&(dev_priv->gart_heap));
1450 radeon_mem_takedown(&(dev_priv->fb_heap));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 /* deallocate kernel resources */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001453 radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455}
1456
1457/* Just reset the CP ring. Called as part of an X Server engine reset.
1458 */
Eric Anholtc153f452007-09-03 12:06:45 +10001459int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001462 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Eric Anholt6c340ea2007-08-25 20:23:09 +10001464 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001467 DRM_DEBUG("called before init done\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001471 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472
1473 /* The CP is no longer running after an engine reset */
1474 dev_priv->cp_running = 0;
1475
1476 return 0;
1477}
1478
Eric Anholtc153f452007-09-03 12:06:45 +10001479int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001482 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
Eric Anholt6c340ea2007-08-25 20:23:09 +10001484 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001486 return radeon_do_cp_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487}
1488
1489/* Added by Charl P. Botha to call radeon_do_resume_cp().
1490 */
Eric Anholtc153f452007-09-03 12:06:45 +10001491int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492{
etienne3d161182009-02-20 09:44:45 +10001493 return radeon_do_resume_cp(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494}
1495
Eric Anholtc153f452007-09-03 12:06:45 +10001496int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001498 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
Eric Anholt6c340ea2007-08-25 20:23:09 +10001500 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001502 return radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505/* ================================================================
1506 * Fullscreen mode
1507 */
1508
1509/* KW: Deprecated to say the least:
1510 */
Eric Anholtc153f452007-09-03 12:06:45 +10001511int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
1513 return 0;
1514}
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/* ================================================================
1517 * Freelist management
1518 */
1519
1520/* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1521 * bufs until freelist code is used. Note this hides a problem with
1522 * the scratch register * (used to keep track of last buffer
1523 * completed) being written to before * the last buffer has actually
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001524 * completed rendering.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 *
1526 * KW: It's also a good way to find free buffers quickly.
1527 *
1528 * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1529 * sleep. However, bugs in older versions of radeon_accel.c mean that
1530 * we essentially have to do this, else old clients will break.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001531 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 * However, it does leave open a potential deadlock where all the
1533 * buffers are held by other clients, which can't release them because
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001534 * they can't get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 */
1536
Dave Airlie056219e2007-07-11 16:17:42 +10001537struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538{
Dave Airliecdd55a22007-07-11 16:32:08 +10001539 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 drm_radeon_private_t *dev_priv = dev->dev_private;
1541 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001542 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 int i, t;
1544 int start;
1545
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001546 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 dev_priv->last_buf = 0;
1548
1549 start = dev_priv->last_buf;
1550
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551 for (t = 0; t < dev_priv->usec_timeout; t++) {
1552 u32 done_age = GET_SCRATCH(1);
1553 DRM_DEBUG("done_age = %d\n", done_age);
1554 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 buf = dma->buflist[i];
1556 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001557 if (buf->file_priv == NULL || (buf->pending &&
1558 buf_priv->age <=
1559 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 dev_priv->stats.requested_bufs++;
1561 buf->pending = 0;
1562 return buf;
1563 }
1564 start = 0;
1565 }
1566
1567 if (t) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001568 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 dev_priv->stats.freelist_loops++;
1570 }
1571 }
1572
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001573 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return NULL;
1575}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577#if 0
Dave Airlie056219e2007-07-11 16:17:42 +10001578struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579{
Dave Airliecdd55a22007-07-11 16:32:08 +10001580 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 drm_radeon_private_t *dev_priv = dev->dev_private;
1582 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001583 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int i, t;
1585 int start;
1586 u32 done_age = DRM_READ32(dev_priv->ring_rptr, RADEON_SCRATCHOFF(1));
1587
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001588 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 dev_priv->last_buf = 0;
1590
1591 start = dev_priv->last_buf;
1592 dev_priv->stats.freelist_loops++;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001593
1594 for (t = 0; t < 2; t++) {
1595 for (i = start; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 buf = dma->buflist[i];
1597 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001598 if (buf->file_priv == 0 || (buf->pending &&
1599 buf_priv->age <=
1600 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 dev_priv->stats.requested_bufs++;
1602 buf->pending = 0;
1603 return buf;
1604 }
1605 }
1606 start = 0;
1607 }
1608
1609 return NULL;
1610}
1611#endif
1612
Dave Airlie84b1fd12007-07-11 15:53:27 +10001613void radeon_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614{
Dave Airliecdd55a22007-07-11 16:32:08 +10001615 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 drm_radeon_private_t *dev_priv = dev->dev_private;
1617 int i;
1618
1619 dev_priv->last_buf = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001620 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001621 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1623 buf_priv->age = 0;
1624 }
1625}
1626
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627/* ================================================================
1628 * CP command submission
1629 */
1630
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001631int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
1633 drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1634 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001635 u32 last_head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001637 for (i = 0; i < dev_priv->usec_timeout; i++) {
1638 u32 head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640 ring->space = (head - ring->tail) * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001641 if (ring->space <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 ring->space += ring->size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001643 if (ring->space > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
1647
1648 if (head != last_head)
1649 i = 0;
1650 last_head = head;
1651
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001652 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654
1655 /* FIXME: This return value is ignored in the BEGIN_RING macro! */
1656#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001657 radeon_status(dev_priv);
1658 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659#endif
Eric Anholt20caafa2007-08-25 19:22:43 +10001660 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Eric Anholt6c340ea2007-08-25 20:23:09 +10001663static int radeon_cp_get_buffers(struct drm_device *dev,
1664 struct drm_file *file_priv,
Dave Airliec60ce622007-07-11 15:27:12 +10001665 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666{
1667 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10001668 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001670 for (i = d->granted_count; i < d->request_count; i++) {
1671 buf = radeon_freelist_get(dev);
1672 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10001673 return -EBUSY; /* NOTE: broken client */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Eric Anholt6c340ea2007-08-25 20:23:09 +10001675 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001677 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
1678 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001679 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001680 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
1681 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10001682 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 d->granted_count++;
1685 }
1686 return 0;
1687}
1688
Eric Anholtc153f452007-09-03 12:06:45 +10001689int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
Dave Airliecdd55a22007-07-11 16:32:08 +10001691 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10001693 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Eric Anholt6c340ea2007-08-25 20:23:09 +10001695 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 /* Please don't send us buffers.
1698 */
Eric Anholtc153f452007-09-03 12:06:45 +10001699 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001700 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001701 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001702 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 }
1704
1705 /* We'll send you buffers.
1706 */
Eric Anholtc153f452007-09-03 12:06:45 +10001707 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001708 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001709 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001710 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 }
1712
Eric Anholtc153f452007-09-03 12:06:45 +10001713 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Eric Anholtc153f452007-09-03 12:06:45 +10001715 if (d->request_count) {
1716 ret = radeon_cp_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 }
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 return ret;
1720}
1721
Dave Airlie22eae942005-11-10 22:16:34 +11001722int radeon_driver_load(struct drm_device *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723{
1724 drm_radeon_private_t *dev_priv;
1725 int ret = 0;
1726
1727 dev_priv = drm_alloc(sizeof(drm_radeon_private_t), DRM_MEM_DRIVER);
1728 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10001729 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
1731 memset(dev_priv, 0, sizeof(drm_radeon_private_t));
1732 dev->dev_private = (void *)dev_priv;
1733 dev_priv->flags = flags;
1734
Dave Airlie54a56ac2006-09-22 04:25:09 +10001735 switch (flags & RADEON_FAMILY_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 case CHIP_R100:
1737 case CHIP_RV200:
1738 case CHIP_R200:
1739 case CHIP_R300:
Dave Airlieb15ec362006-08-19 17:43:52 +10001740 case CHIP_R350:
Dave Airlie414ed532005-08-16 20:43:16 +10001741 case CHIP_R420:
Alex Deucheredc6f382008-10-17 09:21:45 +10001742 case CHIP_R423:
Dave Airlieb15ec362006-08-19 17:43:52 +10001743 case CHIP_RV410:
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001744 case CHIP_RV515:
1745 case CHIP_R520:
1746 case CHIP_RV570:
1747 case CHIP_R580:
Dave Airlie54a56ac2006-09-22 04:25:09 +10001748 dev_priv->flags |= RADEON_HAS_HIERZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 break;
1750 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001751 /* all other chips have no hierarchical z buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 break;
1753 }
Dave Airlie414ed532005-08-16 20:43:16 +10001754
1755 if (drm_device_is_agp(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001756 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlieb15ec362006-08-19 17:43:52 +10001757 else if (drm_device_is_pcie(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10001758 dev_priv->flags |= RADEON_IS_PCIE;
Dave Airlieb15ec362006-08-19 17:43:52 +10001759 else
Dave Airlie54a56ac2006-09-22 04:25:09 +10001760 dev_priv->flags |= RADEON_IS_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +10001761
Dave Airlie78538bf2008-11-11 17:56:16 +10001762 ret = drm_addmap(dev, drm_get_resource_start(dev, 2),
1763 drm_get_resource_len(dev, 2), _DRM_REGISTERS,
1764 _DRM_READ_ONLY | _DRM_DRIVER, &dev_priv->mmio);
1765 if (ret != 0)
1766 return ret;
1767
Keith Packard52440212008-11-18 09:30:25 -08001768 ret = drm_vblank_init(dev, 2);
1769 if (ret) {
1770 radeon_driver_unload(dev);
1771 return ret;
1772 }
1773
Dave Airlie414ed532005-08-16 20:43:16 +10001774 DRM_DEBUG("%s card detected\n",
Dave Airlie54a56ac2006-09-22 04:25:09 +10001775 ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 return ret;
1777}
1778
Dave Airlie7c1c2872008-11-28 14:22:24 +10001779int radeon_master_create(struct drm_device *dev, struct drm_master *master)
1780{
1781 struct drm_radeon_master_private *master_priv;
1782 unsigned long sareapage;
1783 int ret;
1784
1785 master_priv = drm_calloc(1, sizeof(*master_priv), DRM_MEM_DRIVER);
1786 if (!master_priv)
1787 return -ENOMEM;
1788
1789 /* prebuild the SAREA */
Dave Airliebdf539a2008-12-18 16:56:11 +10001790 sareapage = max_t(unsigned long, SAREA_MAX, PAGE_SIZE);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001791 ret = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK|_DRM_DRIVER,
1792 &master_priv->sarea);
1793 if (ret) {
1794 DRM_ERROR("SAREA setup failed\n");
1795 return ret;
1796 }
1797 master_priv->sarea_priv = master_priv->sarea->handle + sizeof(struct drm_sarea);
1798 master_priv->sarea_priv->pfCurrentPage = 0;
1799
1800 master->driver_priv = master_priv;
1801 return 0;
1802}
1803
1804void radeon_master_destroy(struct drm_device *dev, struct drm_master *master)
1805{
1806 struct drm_radeon_master_private *master_priv = master->driver_priv;
1807
1808 if (!master_priv)
1809 return;
1810
1811 if (master_priv->sarea_priv &&
1812 master_priv->sarea_priv->pfCurrentPage != 0)
1813 radeon_cp_dispatch_flip(dev, master);
1814
1815 master_priv->sarea_priv = NULL;
1816 if (master_priv->sarea)
Dave Airlie4e74f362008-12-19 10:23:14 +11001817 drm_rmmap_locked(dev, master_priv->sarea);
Dave Airlie7c1c2872008-11-28 14:22:24 +10001818
1819 drm_free(master_priv, sizeof(*master_priv), DRM_MEM_DRIVER);
1820
1821 master->driver_priv = NULL;
1822}
1823
Dave Airlie22eae942005-11-10 22:16:34 +11001824/* Create mappings for registers and framebuffer so userland doesn't necessarily
1825 * have to find them.
1826 */
1827int radeon_driver_firstopen(struct drm_device *dev)
Dave Airlie836cf042005-07-10 19:27:04 +10001828{
1829 int ret;
1830 drm_local_map_t *map;
1831 drm_radeon_private_t *dev_priv = dev->dev_private;
1832
Dave Airlief2b04cd2007-05-08 15:19:23 +10001833 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
1834
Dave Airlie7fc86862007-11-05 10:45:27 +10001835 dev_priv->fb_aper_offset = drm_get_resource_start(dev, 0);
1836 ret = drm_addmap(dev, dev_priv->fb_aper_offset,
Dave Airlie836cf042005-07-10 19:27:04 +10001837 drm_get_resource_len(dev, 0), _DRM_FRAME_BUFFER,
1838 _DRM_WRITE_COMBINING, &map);
1839 if (ret != 0)
1840 return ret;
1841
1842 return 0;
1843}
1844
Dave Airlie22eae942005-11-10 22:16:34 +11001845int radeon_driver_unload(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846{
1847 drm_radeon_private_t *dev_priv = dev->dev_private;
1848
1849 DRM_DEBUG("\n");
Dave Airlie78538bf2008-11-11 17:56:16 +10001850
1851 drm_rmmap(dev, dev_priv->mmio);
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 drm_free(dev_priv, sizeof(*dev_priv), DRM_MEM_DRIVER);
1854
1855 dev->dev_private = NULL;
1856 return 0;
1857}