blob: dc6eba6b96dd1f5476cd3f6bda470e0cda6b245b [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
39#define RADEON_FIFO_DEBUG 0
40
Ben Hutchings70967ab2009-08-29 14:53:51 +010041/* Firmware Names */
42#define FIRMWARE_R100 "radeon/R100_cp.bin"
43#define FIRMWARE_R200 "radeon/R200_cp.bin"
44#define FIRMWARE_R300 "radeon/R300_cp.bin"
45#define FIRMWARE_R420 "radeon/R420_cp.bin"
46#define FIRMWARE_RS690 "radeon/RS690_cp.bin"
47#define FIRMWARE_RS600 "radeon/RS600_cp.bin"
48#define FIRMWARE_R520 "radeon/R520_cp.bin"
49
50MODULE_FIRMWARE(FIRMWARE_R100);
51MODULE_FIRMWARE(FIRMWARE_R200);
52MODULE_FIRMWARE(FIRMWARE_R300);
53MODULE_FIRMWARE(FIRMWARE_R420);
54MODULE_FIRMWARE(FIRMWARE_RS690);
55MODULE_FIRMWARE(FIRMWARE_RS600);
56MODULE_FIRMWARE(FIRMWARE_R520);
57
Dave Airlie84b1fd12007-07-11 15:53:27 +100058static int radeon_do_cleanup_cp(struct drm_device * dev);
Jerome Glisse54f961a2008-08-13 09:46:31 +100059static void radeon_do_cp_start(drm_radeon_private_t * dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Alex Deucherc05ce082009-02-24 16:22:29 -050061u32 radeon_read_ring_rptr(drm_radeon_private_t *dev_priv, u32 off)
David Millerb07fa022009-02-12 02:15:37 -080062{
63 u32 val;
64
65 if (dev_priv->flags & RADEON_IS_AGP) {
66 val = DRM_READ32(dev_priv->ring_rptr, off);
67 } else {
68 val = *(((volatile u32 *)
69 dev_priv->ring_rptr->handle) +
70 (off / sizeof(u32)));
71 val = le32_to_cpu(val);
72 }
73 return val;
74}
75
76u32 radeon_get_ring_head(drm_radeon_private_t *dev_priv)
77{
78 if (dev_priv->writeback_works)
79 return radeon_read_ring_rptr(dev_priv, 0);
Alex Deucherc05ce082009-02-24 16:22:29 -050080 else {
81 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
82 return RADEON_READ(R600_CP_RB_RPTR);
83 else
84 return RADEON_READ(RADEON_CP_RB_RPTR);
85 }
David Millerb07fa022009-02-12 02:15:37 -080086}
87
Alex Deucherc05ce082009-02-24 16:22:29 -050088void radeon_write_ring_rptr(drm_radeon_private_t *dev_priv, u32 off, u32 val)
David Millerb07fa022009-02-12 02:15:37 -080089{
90 if (dev_priv->flags & RADEON_IS_AGP)
91 DRM_WRITE32(dev_priv->ring_rptr, off, val);
92 else
93 *(((volatile u32 *) dev_priv->ring_rptr->handle) +
94 (off / sizeof(u32))) = cpu_to_le32(val);
95}
96
97void radeon_set_ring_head(drm_radeon_private_t *dev_priv, u32 val)
98{
99 radeon_write_ring_rptr(dev_priv, 0, val);
100}
101
102u32 radeon_get_scratch(drm_radeon_private_t *dev_priv, int index)
103{
Alex Deucherc05ce082009-02-24 16:22:29 -0500104 if (dev_priv->writeback_works) {
105 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
106 return radeon_read_ring_rptr(dev_priv,
107 R600_SCRATCHOFF(index));
108 else
109 return radeon_read_ring_rptr(dev_priv,
110 RADEON_SCRATCHOFF(index));
111 } else {
112 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
113 return RADEON_READ(R600_SCRATCH_REG0 + 4*index);
114 else
115 return RADEON_READ(RADEON_SCRATCH_REG0 + 4*index);
116 }
David Millerb07fa022009-02-12 02:15:37 -0800117}
118
Alex Deucherbefb73c2009-02-24 14:02:13 -0500119u32 RADEON_READ_MM(drm_radeon_private_t *dev_priv, int addr)
120{
121 u32 ret;
122
123 if (addr < 0x10000)
124 ret = DRM_READ32(dev_priv->mmio, addr);
125 else {
126 DRM_WRITE32(dev_priv->mmio, RADEON_MM_INDEX, addr);
127 ret = DRM_READ32(dev_priv->mmio, RADEON_MM_DATA);
128 }
129
130 return ret;
131}
132
Alex Deucher45e51902008-05-28 13:28:59 +1000133static u32 R500_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000134{
135 u32 ret;
136 RADEON_WRITE(R520_MC_IND_INDEX, 0x7f0000 | (addr & 0xff));
137 ret = RADEON_READ(R520_MC_IND_DATA);
138 RADEON_WRITE(R520_MC_IND_INDEX, 0);
139 return ret;
140}
141
Alex Deucher45e51902008-05-28 13:28:59 +1000142static u32 RS480_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
143{
144 u32 ret;
145 RADEON_WRITE(RS480_NB_MC_INDEX, addr & 0xff);
146 ret = RADEON_READ(RS480_NB_MC_DATA);
147 RADEON_WRITE(RS480_NB_MC_INDEX, 0xff);
148 return ret;
149}
150
Maciej Cencora60f92682008-02-19 21:32:45 +1000151static u32 RS690_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
152{
Alex Deucher45e51902008-05-28 13:28:59 +1000153 u32 ret;
Maciej Cencora60f92682008-02-19 21:32:45 +1000154 RADEON_WRITE(RS690_MC_INDEX, (addr & RS690_MC_INDEX_MASK));
Alex Deucher45e51902008-05-28 13:28:59 +1000155 ret = RADEON_READ(RS690_MC_DATA);
156 RADEON_WRITE(RS690_MC_INDEX, RS690_MC_INDEX_MASK);
157 return ret;
158}
159
Alex Deucherc1556f72009-02-25 16:57:49 -0500160static u32 RS600_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
161{
162 u32 ret;
163 RADEON_WRITE(RS600_MC_INDEX, ((addr & RS600_MC_ADDR_MASK) |
164 RS600_MC_IND_CITF_ARB0));
165 ret = RADEON_READ(RS600_MC_DATA);
166 return ret;
167}
168
Alex Deucher45e51902008-05-28 13:28:59 +1000169static u32 IGP_READ_MCIND(drm_radeon_private_t *dev_priv, int addr)
170{
Alex Deucherf0738e92008-10-16 17:12:02 +1000171 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
172 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +1000173 return RS690_READ_MCIND(dev_priv, addr);
Alex Deucherc1556f72009-02-25 16:57:49 -0500174 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
175 return RS600_READ_MCIND(dev_priv, addr);
Alex Deucher45e51902008-05-28 13:28:59 +1000176 else
177 return RS480_READ_MCIND(dev_priv, addr);
Maciej Cencora60f92682008-02-19 21:32:45 +1000178}
179
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000180u32 radeon_read_fb_location(drm_radeon_private_t *dev_priv)
181{
182
Alex Deucherc05ce082009-02-24 16:22:29 -0500183 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
184 return RADEON_READ(R700_MC_VM_FB_LOCATION);
185 else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
186 return RADEON_READ(R600_MC_VM_FB_LOCATION);
187 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000188 return R500_READ_MCIND(dev_priv, RV515_MC_FB_LOCATION);
Alex Deucherf0738e92008-10-16 17:12:02 +1000189 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
190 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000191 return RS690_READ_MCIND(dev_priv, RS690_MC_FB_LOCATION);
Alex Deucherc1556f72009-02-25 16:57:49 -0500192 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
193 return RS600_READ_MCIND(dev_priv, RS600_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000194 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000195 return R500_READ_MCIND(dev_priv, R520_MC_FB_LOCATION);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000196 else
197 return RADEON_READ(RADEON_MC_FB_LOCATION);
198}
199
200static void radeon_write_fb_location(drm_radeon_private_t *dev_priv, u32 fb_loc)
201{
Alex Deucherc05ce082009-02-24 16:22:29 -0500202 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
203 RADEON_WRITE(R700_MC_VM_FB_LOCATION, fb_loc);
204 else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
205 RADEON_WRITE(R600_MC_VM_FB_LOCATION, fb_loc);
206 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000207 R500_WRITE_MCIND(RV515_MC_FB_LOCATION, fb_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000208 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
209 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000210 RS690_WRITE_MCIND(RS690_MC_FB_LOCATION, fb_loc);
Alex Deucherc1556f72009-02-25 16:57:49 -0500211 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
212 RS600_WRITE_MCIND(RS600_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000213 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000214 R500_WRITE_MCIND(R520_MC_FB_LOCATION, fb_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000215 else
216 RADEON_WRITE(RADEON_MC_FB_LOCATION, fb_loc);
217}
218
Alex Deucherc05ce082009-02-24 16:22:29 -0500219void radeon_write_agp_location(drm_radeon_private_t *dev_priv, u32 agp_loc)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000220{
Alex Deucherc05ce082009-02-24 16:22:29 -0500221 /*R6xx/R7xx: AGP_TOP and BOT are actually 18 bits each */
222 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770) {
223 RADEON_WRITE(R700_MC_VM_AGP_BOT, agp_loc & 0xffff); /* FIX ME */
224 RADEON_WRITE(R700_MC_VM_AGP_TOP, (agp_loc >> 16) & 0xffff);
225 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
226 RADEON_WRITE(R600_MC_VM_AGP_BOT, agp_loc & 0xffff); /* FIX ME */
227 RADEON_WRITE(R600_MC_VM_AGP_TOP, (agp_loc >> 16) & 0xffff);
228 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000229 R500_WRITE_MCIND(RV515_MC_AGP_LOCATION, agp_loc);
Alex Deucherf0738e92008-10-16 17:12:02 +1000230 else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
231 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Maciej Cencora60f92682008-02-19 21:32:45 +1000232 RS690_WRITE_MCIND(RS690_MC_AGP_LOCATION, agp_loc);
Alex Deucherc1556f72009-02-25 16:57:49 -0500233 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
234 RS600_WRITE_MCIND(RS600_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000235 else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515)
Alex Deucher45e51902008-05-28 13:28:59 +1000236 R500_WRITE_MCIND(R520_MC_AGP_LOCATION, agp_loc);
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000237 else
238 RADEON_WRITE(RADEON_MC_AGP_LOCATION, agp_loc);
239}
240
Alex Deucherc05ce082009-02-24 16:22:29 -0500241void radeon_write_agp_base(drm_radeon_private_t *dev_priv, u64 agp_base)
Dave Airlie70b13d52008-06-19 11:40:44 +1000242{
243 u32 agp_base_hi = upper_32_bits(agp_base);
244 u32 agp_base_lo = agp_base & 0xffffffff;
Alex Deucherc05ce082009-02-24 16:22:29 -0500245 u32 r6xx_agp_base = (agp_base >> 22) & 0x3ffff;
Dave Airlie70b13d52008-06-19 11:40:44 +1000246
Alex Deucherc05ce082009-02-24 16:22:29 -0500247 /* R6xx/R7xx must be aligned to a 4MB boundry */
248 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV770)
249 RADEON_WRITE(R700_MC_VM_AGP_BASE, r6xx_agp_base);
250 else if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
251 RADEON_WRITE(R600_MC_VM_AGP_BASE, r6xx_agp_base);
252 else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000253 R500_WRITE_MCIND(RV515_MC_AGP_BASE, agp_base_lo);
254 R500_WRITE_MCIND(RV515_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherf0738e92008-10-16 17:12:02 +1000255 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
256 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000257 RS690_WRITE_MCIND(RS690_MC_AGP_BASE, agp_base_lo);
258 RS690_WRITE_MCIND(RS690_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherc1556f72009-02-25 16:57:49 -0500259 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
260 RS600_WRITE_MCIND(RS600_AGP_BASE, agp_base_lo);
261 RS600_WRITE_MCIND(RS600_AGP_BASE_2, agp_base_hi);
Dave Airlie70b13d52008-06-19 11:40:44 +1000262 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) > CHIP_RV515) {
263 R500_WRITE_MCIND(R520_MC_AGP_BASE, agp_base_lo);
264 R500_WRITE_MCIND(R520_MC_AGP_BASE_2, agp_base_hi);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000265 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
266 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Alex Deucher5cfb6952008-06-19 12:38:29 +1000267 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000268 RADEON_WRITE(RS480_AGP_BASE_2, agp_base_hi);
Dave Airlie70b13d52008-06-19 11:40:44 +1000269 } else {
270 RADEON_WRITE(RADEON_AGP_BASE, agp_base_lo);
271 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R200)
272 RADEON_WRITE(RADEON_AGP_BASE_2, agp_base_hi);
273 }
274}
275
Alex Deucherc05ce082009-02-24 16:22:29 -0500276void radeon_enable_bm(struct drm_radeon_private *dev_priv)
Dave Airliedd8d7cb2009-02-20 13:28:59 +1000277{
278 u32 tmp;
279 /* Turn on bus mastering */
280 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
281 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
282 /* rs600/rs690/rs740 */
283 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RS600_BUS_MASTER_DIS;
284 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
285 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV350) ||
286 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
287 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
288 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
289 /* r1xx, r2xx, r300, r(v)350, r420/r481, rs400/rs480 */
290 tmp = RADEON_READ(RADEON_BUS_CNTL) & ~RADEON_BUS_MASTER_DIS;
291 RADEON_WRITE(RADEON_BUS_CNTL, tmp);
292 } /* PCIE cards appears to not need this */
293}
294
Dave Airlie84b1fd12007-07-11 15:53:27 +1000295static int RADEON_READ_PLL(struct drm_device * dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 drm_radeon_private_t *dev_priv = dev->dev_private;
298
299 RADEON_WRITE8(RADEON_CLOCK_CNTL_INDEX, addr & 0x1f);
300 return RADEON_READ(RADEON_CLOCK_CNTL_DATA);
301}
302
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000303static u32 RADEON_READ_PCIE(drm_radeon_private_t *dev_priv, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
Dave Airlieea98a922005-09-11 20:28:11 +1000305 RADEON_WRITE8(RADEON_PCIE_INDEX, addr & 0xff);
306 return RADEON_READ(RADEON_PCIE_DATA);
307}
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310static void radeon_status(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700312 printk("%s:\n", __func__);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000313 printk("RBBM_STATUS = 0x%08x\n",
314 (unsigned int)RADEON_READ(RADEON_RBBM_STATUS));
315 printk("CP_RB_RTPR = 0x%08x\n",
316 (unsigned int)RADEON_READ(RADEON_CP_RB_RPTR));
317 printk("CP_RB_WTPR = 0x%08x\n",
318 (unsigned int)RADEON_READ(RADEON_CP_RB_WPTR));
319 printk("AIC_CNTL = 0x%08x\n",
320 (unsigned int)RADEON_READ(RADEON_AIC_CNTL));
321 printk("AIC_STAT = 0x%08x\n",
322 (unsigned int)RADEON_READ(RADEON_AIC_STAT));
323 printk("AIC_PT_BASE = 0x%08x\n",
324 (unsigned int)RADEON_READ(RADEON_AIC_PT_BASE));
325 printk("TLB_ADDR = 0x%08x\n",
326 (unsigned int)RADEON_READ(RADEON_AIC_TLB_ADDR));
327 printk("TLB_DATA = 0x%08x\n",
328 (unsigned int)RADEON_READ(RADEON_AIC_TLB_DATA));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330#endif
331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332/* ================================================================
333 * Engine, FIFO control
334 */
335
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336static int radeon_do_pixcache_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 u32 tmp;
339 int i;
340
341 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
342
Alex Deucher259434a2008-05-28 11:51:12 +1000343 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV280) {
344 tmp = RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT);
345 tmp |= RADEON_RB3D_DC_FLUSH_ALL;
346 RADEON_WRITE(RADEON_RB3D_DSTCACHE_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Alex Deucher259434a2008-05-28 11:51:12 +1000348 for (i = 0; i < dev_priv->usec_timeout; i++) {
349 if (!(RADEON_READ(RADEON_RB3D_DSTCACHE_CTLSTAT)
350 & RADEON_RB3D_DC_BUSY)) {
351 return 0;
352 }
353 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Alex Deucher259434a2008-05-28 11:51:12 +1000355 } else {
Jerome Glisse54f961a2008-08-13 09:46:31 +1000356 /* don't flush or purge cache here or lockup */
357 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 }
359
360#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 DRM_ERROR("failed!\n");
362 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000364 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000367static int radeon_do_wait_for_fifo(drm_radeon_private_t * dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 int i;
370
371 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
372
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 for (i = 0; i < dev_priv->usec_timeout; i++) {
374 int slots = (RADEON_READ(RADEON_RBBM_STATUS)
375 & RADEON_RBBM_FIFOCNT_MASK);
376 if (slots >= entries)
377 return 0;
378 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000380 DRM_DEBUG("wait for fifo failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000381 RADEON_READ(RADEON_RBBM_STATUS),
382 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385 DRM_ERROR("failed!\n");
386 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000388 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000391static int radeon_do_wait_for_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int i, ret;
394
395 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
396
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000397 ret = radeon_do_wait_for_fifo(dev_priv, 64);
398 if (ret)
399 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000401 for (i = 0; i < dev_priv->usec_timeout; i++) {
402 if (!(RADEON_READ(RADEON_RBBM_STATUS)
403 & RADEON_RBBM_ACTIVE)) {
404 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return 0;
406 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000407 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Dave Airlie6c7be292008-09-01 08:51:52 +1000409 DRM_DEBUG("wait idle failed status : 0x%08X 0x%08X\n",
Jerome Glisse54f961a2008-08-13 09:46:31 +1000410 RADEON_READ(RADEON_RBBM_STATUS),
411 RADEON_READ(R300_VAP_CNTL_STATUS));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000414 DRM_ERROR("failed!\n");
415 radeon_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000417 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Alex Deucher5b92c402008-05-28 11:57:40 +1000420static void radeon_init_pipes(drm_radeon_private_t *dev_priv)
421{
422 uint32_t gb_tile_config, gb_pipe_sel = 0;
423
Alex Deucherf779b3e2009-08-19 19:11:39 -0400424 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) {
425 uint32_t z_pipe_sel = RADEON_READ(RV530_GB_PIPE_SELECT2);
426 if ((z_pipe_sel & 3) == 3)
427 dev_priv->num_z_pipes = 2;
428 else
429 dev_priv->num_z_pipes = 1;
430 } else
431 dev_priv->num_z_pipes = 1;
432
Alex Deucher5b92c402008-05-28 11:57:40 +1000433 /* RS4xx/RS6xx/R4xx/R5xx */
434 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R420) {
435 gb_pipe_sel = RADEON_READ(R400_GB_PIPE_SELECT);
436 dev_priv->num_gb_pipes = ((gb_pipe_sel >> 12) & 0x3) + 1;
437 } else {
438 /* R3xx */
439 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
440 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350)) {
441 dev_priv->num_gb_pipes = 2;
442 } else {
443 /* R3Vxx */
444 dev_priv->num_gb_pipes = 1;
445 }
446 }
447 DRM_INFO("Num pipes: %d\n", dev_priv->num_gb_pipes);
448
449 gb_tile_config = (R300_ENABLE_TILING | R300_TILE_SIZE_16 /*| R300_SUBPIXEL_1_16*/);
450
451 switch (dev_priv->num_gb_pipes) {
452 case 2: gb_tile_config |= R300_PIPE_COUNT_R300; break;
453 case 3: gb_tile_config |= R300_PIPE_COUNT_R420_3P; break;
454 case 4: gb_tile_config |= R300_PIPE_COUNT_R420; break;
455 default:
456 case 1: gb_tile_config |= R300_PIPE_COUNT_RV350; break;
457 }
458
459 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RV515) {
460 RADEON_WRITE_PLL(R500_DYN_SCLK_PWMEM_PIPE, (1 | ((gb_pipe_sel >> 8) & 0xf) << 4));
Maciej Cencoraaf7ae352009-03-24 01:48:50 +0100461 RADEON_WRITE(R300_SU_REG_DEST, ((1 << dev_priv->num_gb_pipes) - 1));
Alex Deucher5b92c402008-05-28 11:57:40 +1000462 }
463 RADEON_WRITE(R300_GB_TILE_CONFIG, gb_tile_config);
464 radeon_do_wait_for_idle(dev_priv);
465 RADEON_WRITE(R300_DST_PIPE_CONFIG, RADEON_READ(R300_DST_PIPE_CONFIG) | R300_PIPE_AUTO_CONFIG);
466 RADEON_WRITE(R300_RB2D_DSTCACHE_MODE, (RADEON_READ(R300_RB2D_DSTCACHE_MODE) |
467 R300_DC_AUTOFLUSH_ENABLE |
468 R300_DC_DC_DISABLE_IGNORE_PE));
469
470
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473/* ================================================================
474 * CP control, initialization
475 */
476
477/* Load the microcode for the CP */
Ben Hutchings70967ab2009-08-29 14:53:51 +0100478static int radeon_cp_init_microcode(drm_radeon_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Ben Hutchings70967ab2009-08-29 14:53:51 +0100480 struct platform_device *pdev;
481 const char *fw_name = NULL;
482 int err;
483
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000484 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Ben Hutchings70967ab2009-08-29 14:53:51 +0100486 pdev = platform_device_register_simple("radeon_cp", 0, NULL, 0);
487 err = IS_ERR(pdev);
488 if (err) {
489 printk(KERN_ERR "radeon_cp: Failed to register firmware\n");
490 return -EINVAL;
491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Alex Deucher9f184092008-05-28 11:21:25 +1000493 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R100) ||
494 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV100) ||
495 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV200) ||
496 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS100) ||
497 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS200)) {
498 DRM_INFO("Loading R100 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100499 fw_name = FIRMWARE_R100;
Alex Deucher9f184092008-05-28 11:21:25 +1000500 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R200) ||
501 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV250) ||
502 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV280) ||
503 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS300)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 DRM_INFO("Loading R200 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100505 fw_name = FIRMWARE_R200;
Alex Deucher9f184092008-05-28 11:21:25 +1000506 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R300) ||
507 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R350) ||
508 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV350) ||
509 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV380) ||
Alex Deucherb2ceddf2008-10-17 09:19:33 +1000510 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS400) ||
Alex Deucher45e51902008-05-28 13:28:59 +1000511 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS480)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 DRM_INFO("Loading R300 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100513 fw_name = FIRMWARE_R300;
Alex Deucher9f184092008-05-28 11:21:25 +1000514 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) ||
Alex Deucheredc6f382008-10-17 09:21:45 +1000515 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R423) ||
Alex Deucher9f184092008-05-28 11:21:25 +1000516 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV410)) {
517 DRM_INFO("Loading R400 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100518 fw_name = FIRMWARE_R420;
Alex Deucherf0738e92008-10-16 17:12:02 +1000519 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
520 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740)) {
521 DRM_INFO("Loading RS690/RS740 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100522 fw_name = FIRMWARE_RS690;
Alex Deucherc1556f72009-02-25 16:57:49 -0500523 } else if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
524 DRM_INFO("Loading RS600 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100525 fw_name = FIRMWARE_RS600;
Alex Deucher9f184092008-05-28 11:21:25 +1000526 } else if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV515) ||
527 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R520) ||
528 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV530) ||
529 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R580) ||
530 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV560) ||
531 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RV570)) {
532 DRM_INFO("Loading R500 Microcode\n");
Ben Hutchings70967ab2009-08-29 14:53:51 +0100533 fw_name = FIRMWARE_R520;
534 }
535
536 err = request_firmware(&dev_priv->me_fw, fw_name, &pdev->dev);
537 platform_device_unregister(pdev);
538 if (err) {
539 printk(KERN_ERR "radeon_cp: Failed to load firmware \"%s\"\n",
540 fw_name);
541 } else if (dev_priv->me_fw->size % 8) {
542 printk(KERN_ERR
543 "radeon_cp: Bogus length %zu in firmware \"%s\"\n",
544 dev_priv->me_fw->size, fw_name);
545 err = -EINVAL;
546 release_firmware(dev_priv->me_fw);
547 dev_priv->me_fw = NULL;
548 }
549 return err;
550}
551
552static void radeon_cp_load_microcode(drm_radeon_private_t *dev_priv)
553{
554 const __be32 *fw_data;
555 int i, size;
556
557 radeon_do_wait_for_idle(dev_priv);
558
559 if (dev_priv->me_fw) {
560 size = dev_priv->me_fw->size / 4;
561 fw_data = (const __be32 *)&dev_priv->me_fw->data[0];
562 RADEON_WRITE(RADEON_CP_ME_RAM_ADDR, 0);
563 for (i = 0; i < size; i += 2) {
Alex Deucher9f184092008-05-28 11:21:25 +1000564 RADEON_WRITE(RADEON_CP_ME_RAM_DATAH,
Ben Hutchings70967ab2009-08-29 14:53:51 +0100565 be32_to_cpup(&fw_data[i]));
Alex Deucher9f184092008-05-28 11:21:25 +1000566 RADEON_WRITE(RADEON_CP_ME_RAM_DATAL,
Ben Hutchings70967ab2009-08-29 14:53:51 +0100567 be32_to_cpup(&fw_data[i + 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 }
570}
571
572/* Flush any pending commands to the CP. This should only be used just
573 * prior to a wait for idle, as it informs the engine that the command
574 * stream is ending.
575 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576static void radeon_do_cp_flush(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000578 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579#if 0
580 u32 tmp;
581
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000582 tmp = RADEON_READ(RADEON_CP_RB_WPTR) | (1 << 31);
583 RADEON_WRITE(RADEON_CP_RB_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584#endif
585}
586
587/* Wait for the CP to go idle.
588 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000589int radeon_do_cp_idle(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000592 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000594 BEGIN_RING(6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 RADEON_PURGE_CACHE();
597 RADEON_PURGE_ZCACHE();
598 RADEON_WAIT_UNTIL_IDLE();
599
600 ADVANCE_RING();
601 COMMIT_RING();
602
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000603 return radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/* Start the Command Processor.
607 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000608static void radeon_do_cp_start(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000613 radeon_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 RADEON_WRITE(RADEON_CP_CSQ_CNTL, dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 dev_priv->cp_running = 1;
618
Alex Deucheraadd4e12009-09-21 14:48:45 +1000619 /* on r420, any DMA from CP to system memory while 2D is active
620 * can cause a hang. workaround is to queue a CP RESYNC token
621 */
622 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) {
623 BEGIN_RING(3);
624 OUT_RING(CP_PACKET0(R300_CP_RESYNC_ADDR, 1));
625 OUT_RING(5); /* scratch reg 5 */
626 OUT_RING(0xdeadbeef);
627 ADVANCE_RING();
628 COMMIT_RING();
629 }
630
Jerome Glisse54f961a2008-08-13 09:46:31 +1000631 BEGIN_RING(8);
632 /* isync can only be written through cp on r5xx write it here */
633 OUT_RING(CP_PACKET0(RADEON_ISYNC_CNTL, 0));
634 OUT_RING(RADEON_ISYNC_ANY2D_IDLE3D |
635 RADEON_ISYNC_ANY3D_IDLE2D |
636 RADEON_ISYNC_WAIT_IDLEGUI |
637 RADEON_ISYNC_CPSCRATCH_IDLEGUI);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 RADEON_PURGE_CACHE();
639 RADEON_PURGE_ZCACHE();
640 RADEON_WAIT_UNTIL_IDLE();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 ADVANCE_RING();
642 COMMIT_RING();
Jerome Glisse54f961a2008-08-13 09:46:31 +1000643
644 dev_priv->track_flush |= RADEON_FLUSH_EMITED | RADEON_PURGE_EMITED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
647/* Reset the Command Processor. This will not flush any pending
648 * commands, so you must wait for the CP command stream to complete
649 * before calling this routine.
650 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000651static void radeon_do_cp_reset(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
653 u32 cur_read_ptr;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000654 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
657 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
658 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 dev_priv->ring.tail = cur_read_ptr;
660}
661
662/* Stop the Command Processor. This will not flush any pending
663 * commands, so you must flush the command stream and wait for the CP
664 * to go idle before calling this routine.
665 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000666static void radeon_do_cp_stop(drm_radeon_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Alex Deucheraadd4e12009-09-21 14:48:45 +1000668 RING_LOCALS;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000669 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Alex Deucheraadd4e12009-09-21 14:48:45 +1000671 /* finish the pending CP_RESYNC token */
672 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_R420) {
673 BEGIN_RING(2);
674 OUT_RING(CP_PACKET0(R300_RB3D_DSTCACHE_CTLSTAT, 0));
675 OUT_RING(R300_RB3D_DC_FINISH);
676 ADVANCE_RING();
677 COMMIT_RING();
678 radeon_do_wait_for_idle(dev_priv);
679 }
680
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000681 RADEON_WRITE(RADEON_CP_CSQ_CNTL, RADEON_CSQ_PRIDIS_INDDIS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 dev_priv->cp_running = 0;
684}
685
686/* Reset the engine. This will stop the CP if it is running.
687 */
Dave Airlie84b1fd12007-07-11 15:53:27 +1000688static int radeon_do_engine_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 drm_radeon_private_t *dev_priv = dev->dev_private;
Alex Deucherd396db32008-05-28 11:54:06 +1000691 u32 clock_cntl_index = 0, mclk_cntl = 0, rbbm_soft_reset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000694 radeon_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Alex Deucherd396db32008-05-28 11:54:06 +1000696 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
697 /* may need something similar for newer chips */
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000698 clock_cntl_index = RADEON_READ(RADEON_CLOCK_CNTL_INDEX);
699 mclk_cntl = RADEON_READ_PLL(dev, RADEON_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000701 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, (mclk_cntl |
702 RADEON_FORCEON_MCLKA |
703 RADEON_FORCEON_MCLKB |
704 RADEON_FORCEON_YCLKA |
705 RADEON_FORCEON_YCLKB |
706 RADEON_FORCEON_MC |
707 RADEON_FORCEON_AIC));
Alex Deucherd396db32008-05-28 11:54:06 +1000708 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709
Alex Deucherd396db32008-05-28 11:54:06 +1000710 rbbm_soft_reset = RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Alex Deucherd396db32008-05-28 11:54:06 +1000712 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset |
713 RADEON_SOFT_RESET_CP |
714 RADEON_SOFT_RESET_HI |
715 RADEON_SOFT_RESET_SE |
716 RADEON_SOFT_RESET_RE |
717 RADEON_SOFT_RESET_PP |
718 RADEON_SOFT_RESET_E2 |
719 RADEON_SOFT_RESET_RB));
720 RADEON_READ(RADEON_RBBM_SOFT_RESET);
721 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, (rbbm_soft_reset &
722 ~(RADEON_SOFT_RESET_CP |
723 RADEON_SOFT_RESET_HI |
724 RADEON_SOFT_RESET_SE |
725 RADEON_SOFT_RESET_RE |
726 RADEON_SOFT_RESET_PP |
727 RADEON_SOFT_RESET_E2 |
728 RADEON_SOFT_RESET_RB)));
729 RADEON_READ(RADEON_RBBM_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Alex Deucherd396db32008-05-28 11:54:06 +1000731 if ((dev_priv->flags & RADEON_FAMILY_MASK) <= CHIP_RV410) {
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000732 RADEON_WRITE_PLL(RADEON_MCLK_CNTL, mclk_cntl);
733 RADEON_WRITE(RADEON_CLOCK_CNTL_INDEX, clock_cntl_index);
734 RADEON_WRITE(RADEON_RBBM_SOFT_RESET, rbbm_soft_reset);
735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Alex Deucher5b92c402008-05-28 11:57:40 +1000737 /* setup the raster pipes */
738 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R300)
739 radeon_init_pipes(dev_priv);
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 /* Reset the CP ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000742 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /* The CP is no longer running after an engine reset */
745 dev_priv->cp_running = 0;
746
747 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000748 radeon_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 return 0;
751}
752
Dave Airlie84b1fd12007-07-11 15:53:27 +1000753static void radeon_cp_init_ring_buffer(struct drm_device * dev,
etienne3d161182009-02-20 09:44:45 +1000754 drm_radeon_private_t *dev_priv,
755 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
etienne3d161182009-02-20 09:44:45 +1000757 struct drm_radeon_master_private *master_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 u32 ring_start, cur_read_ptr;
Dave Airliebc5f4522007-11-05 12:50:58 +1000759
Dave Airlied5ea7022006-03-19 19:37:55 +1100760 /* Initialize the memory controller. With new memory map, the fb location
761 * is not changed, it should have been properly initialized already. Part
762 * of the problem is that the code below is bogus, assuming the GART is
763 * always appended to the fb which is not necessarily the case
764 */
765 if (!dev_priv->new_memmap)
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000766 radeon_write_fb_location(dev_priv,
Dave Airlied5ea7022006-03-19 19:37:55 +1100767 ((dev_priv->gart_vm_start - 1) & 0xffff0000)
768 | (dev_priv->fb_location >> 16));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000771 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie70b13d52008-06-19 11:40:44 +1000772 radeon_write_agp_base(dev_priv, dev->agp->base);
773
Dave Airlie3d5e2c12008-02-07 15:01:05 +1000774 radeon_write_agp_location(dev_priv,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000775 (((dev_priv->gart_vm_start - 1 +
776 dev_priv->gart_size) & 0xffff0000) |
777 (dev_priv->gart_vm_start >> 16)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 ring_start = (dev_priv->cp_ring->offset
780 - dev->agp->base
781 + dev_priv->gart_vm_start);
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100782 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783#endif
784 ring_start = (dev_priv->cp_ring->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +0100785 - (unsigned long)dev->sg->virtual
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 + dev_priv->gart_vm_start);
787
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000788 RADEON_WRITE(RADEON_CP_RB_BASE, ring_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 /* Set the write pointer delay */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 RADEON_WRITE(RADEON_CP_RB_WPTR_DELAY, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 /* Initialize the ring buffer's read and write pointers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 cur_read_ptr = RADEON_READ(RADEON_CP_RB_RPTR);
795 RADEON_WRITE(RADEON_CP_RB_WPTR, cur_read_ptr);
796 SET_RING_HEAD(dev_priv, cur_read_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 dev_priv->ring.tail = cur_read_ptr;
798
799#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +1000800 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000801 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
802 dev_priv->ring_rptr->offset
803 - dev->agp->base + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 } else
805#endif
806 {
David Millere8a89432009-02-12 02:15:44 -0800807 RADEON_WRITE(RADEON_CP_RB_RPTR_ADDR,
808 dev_priv->ring_rptr->offset
809 - ((unsigned long) dev->sg->virtual)
810 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 }
812
Dave Airlied5ea7022006-03-19 19:37:55 +1100813 /* Set ring buffer size */
814#ifdef __BIG_ENDIAN
815 RADEON_WRITE(RADEON_CP_RB_CNTL,
Roland Scheidegger576cc452008-02-07 14:59:24 +1000816 RADEON_BUF_SWAP_32BIT |
817 (dev_priv->ring.fetch_size_l2ow << 18) |
818 (dev_priv->ring.rptr_update_l2qw << 8) |
819 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100820#else
Roland Scheidegger576cc452008-02-07 14:59:24 +1000821 RADEON_WRITE(RADEON_CP_RB_CNTL,
822 (dev_priv->ring.fetch_size_l2ow << 18) |
823 (dev_priv->ring.rptr_update_l2qw << 8) |
824 dev_priv->ring.size_l2qw);
Dave Airlied5ea7022006-03-19 19:37:55 +1100825#endif
826
Dave Airlied5ea7022006-03-19 19:37:55 +1100827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 /* Initialize the scratch register pointer. This will cause
829 * the scratch register values to be written out to memory
830 * whenever they are updated.
831 *
832 * We simply put this behind the ring read pointer, this works
833 * with PCI GART as well as (whatever kind of) AGP GART
834 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 RADEON_WRITE(RADEON_SCRATCH_ADDR, RADEON_READ(RADEON_CP_RB_RPTR_ADDR)
836 + RADEON_SCRATCH_REG_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0x7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
Dave Airliedd8d7cb2009-02-20 13:28:59 +1000840 radeon_enable_bm(dev_priv);
Dave Airlied5ea7022006-03-19 19:37:55 +1100841
David Millerb07fa022009-02-12 02:15:37 -0800842 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(0), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000843 RADEON_WRITE(RADEON_LAST_FRAME_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100844
David Millerb07fa022009-02-12 02:15:37 -0800845 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000846 RADEON_WRITE(RADEON_LAST_DISPATCH_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100847
David Millerb07fa022009-02-12 02:15:37 -0800848 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(2), 0);
Dave Airlie7c1c2872008-11-28 14:22:24 +1000849 RADEON_WRITE(RADEON_LAST_CLEAR_REG, 0);
Dave Airlied5ea7022006-03-19 19:37:55 +1100850
etienne3d161182009-02-20 09:44:45 +1000851 /* reset sarea copies of these */
852 master_priv = file_priv->master->driver_priv;
853 if (master_priv->sarea_priv) {
854 master_priv->sarea_priv->last_frame = 0;
855 master_priv->sarea_priv->last_dispatch = 0;
856 master_priv->sarea_priv->last_clear = 0;
857 }
858
Dave Airlied5ea7022006-03-19 19:37:55 +1100859 radeon_do_wait_for_idle(dev_priv);
860
861 /* Sync everything up */
862 RADEON_WRITE(RADEON_ISYNC_CNTL,
863 (RADEON_ISYNC_ANY2D_IDLE3D |
864 RADEON_ISYNC_ANY3D_IDLE2D |
865 RADEON_ISYNC_WAIT_IDLEGUI |
866 RADEON_ISYNC_CPSCRATCH_IDLEGUI));
867
868}
869
870static void radeon_test_writeback(drm_radeon_private_t * dev_priv)
871{
872 u32 tmp;
873
Dave Airlie6b79d522008-09-02 10:10:16 +1000874 /* Start with assuming that writeback doesn't work */
875 dev_priv->writeback_works = 0;
876
Dave Airlied5ea7022006-03-19 19:37:55 +1100877 /* Writeback doesn't seem to work everywhere, test it here and possibly
878 * enable it if it appears to work
879 */
David Millerb07fa022009-02-12 02:15:37 -0800880 radeon_write_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1), 0);
881
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000882 RADEON_WRITE(RADEON_SCRATCH_REG1, 0xdeadbeef);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000884 for (tmp = 0; tmp < dev_priv->usec_timeout; tmp++) {
David Millerb07fa022009-02-12 02:15:37 -0800885 u32 val;
886
887 val = radeon_read_ring_rptr(dev_priv, RADEON_SCRATCHOFF(1));
888 if (val == 0xdeadbeef)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 break;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000893 if (tmp < dev_priv->usec_timeout) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 dev_priv->writeback_works = 1;
Dave Airlied5ea7022006-03-19 19:37:55 +1100895 DRM_INFO("writeback test succeeded in %d usecs\n", tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 } else {
897 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100898 DRM_INFO("writeback test failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
Dave Airlie689b9d72005-09-30 17:09:07 +1000900 if (radeon_no_wb == 1) {
901 dev_priv->writeback_works = 0;
Dave Airlied5ea7022006-03-19 19:37:55 +1100902 DRM_INFO("writeback forced off\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Michel Dänzerae1b1a482006-08-07 20:37:46 +1000904
905 if (!dev_priv->writeback_works) {
906 /* Disable writeback to avoid unnecessary bus master transfer */
907 RADEON_WRITE(RADEON_CP_RB_CNTL, RADEON_READ(RADEON_CP_RB_CNTL) |
908 RADEON_RB_NO_UPDATE);
909 RADEON_WRITE(RADEON_SCRATCH_UMSK, 0);
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911}
912
Dave Airlief2b04cd2007-05-08 15:19:23 +1000913/* Enable or disable IGP GART on the chip */
914static void radeon_set_igpgart(drm_radeon_private_t * dev_priv, int on)
915{
Maciej Cencora60f92682008-02-19 21:32:45 +1000916 u32 temp;
917
918 if (on) {
Alex Deucher45e51902008-05-28 13:28:59 +1000919 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
Maciej Cencora60f92682008-02-19 21:32:45 +1000920 dev_priv->gart_vm_start,
921 (long)dev_priv->gart_info.bus_addr,
922 dev_priv->gart_size);
923
Alex Deucher45e51902008-05-28 13:28:59 +1000924 temp = IGP_READ_MCIND(dev_priv, RS480_MC_MISC_CNTL);
Alex Deucherf0738e92008-10-16 17:12:02 +1000925 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
926 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740))
Alex Deucher45e51902008-05-28 13:28:59 +1000927 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, (RS480_GART_INDEX_REG_EN |
928 RS690_BLOCK_GFX_D3_EN));
929 else
930 IGP_WRITE_MCIND(RS480_MC_MISC_CNTL, RS480_GART_INDEX_REG_EN);
Maciej Cencora60f92682008-02-19 21:32:45 +1000931
Alex Deucher45e51902008-05-28 13:28:59 +1000932 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
933 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000934
Alex Deucher45e51902008-05-28 13:28:59 +1000935 temp = IGP_READ_MCIND(dev_priv, RS480_GART_FEATURE_ID);
936 IGP_WRITE_MCIND(RS480_GART_FEATURE_ID, (RS480_HANG_EN |
937 RS480_TLB_ENABLE |
938 RS480_GTW_LAC_EN |
939 RS480_1LEVEL_GART));
Maciej Cencora60f92682008-02-19 21:32:45 +1000940
Dave Airliefa0d71b2008-05-28 11:27:01 +1000941 temp = dev_priv->gart_info.bus_addr & 0xfffff000;
942 temp |= (upper_32_bits(dev_priv->gart_info.bus_addr) & 0xff) << 4;
Alex Deucher45e51902008-05-28 13:28:59 +1000943 IGP_WRITE_MCIND(RS480_GART_BASE, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000944
Alex Deucher45e51902008-05-28 13:28:59 +1000945 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_MODE_CNTL);
946 IGP_WRITE_MCIND(RS480_AGP_MODE_CNTL, ((1 << RS480_REQ_TYPE_SNOOP_SHIFT) |
947 RS480_REQ_TYPE_SNOOP_DIS));
Maciej Cencora60f92682008-02-19 21:32:45 +1000948
Alex Deucher5cfb6952008-06-19 12:38:29 +1000949 radeon_write_agp_base(dev_priv, dev_priv->gart_vm_start);
Dave Airlie3722bfc2008-05-28 11:28:27 +1000950
Maciej Cencora60f92682008-02-19 21:32:45 +1000951 dev_priv->gart_size = 32*1024*1024;
952 temp = (((dev_priv->gart_vm_start - 1 + dev_priv->gart_size) &
953 0xffff0000) | (dev_priv->gart_vm_start >> 16));
954
Alex Deucher45e51902008-05-28 13:28:59 +1000955 radeon_write_agp_location(dev_priv, temp);
Maciej Cencora60f92682008-02-19 21:32:45 +1000956
Alex Deucher45e51902008-05-28 13:28:59 +1000957 temp = IGP_READ_MCIND(dev_priv, RS480_AGP_ADDRESS_SPACE_SIZE);
958 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, (RS480_GART_EN |
959 RS480_VA_SIZE_32MB));
Maciej Cencora60f92682008-02-19 21:32:45 +1000960
961 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000962 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
963 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000964 break;
965 DRM_UDELAY(1);
966 } while (1);
967
Alex Deucher45e51902008-05-28 13:28:59 +1000968 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL,
969 RS480_GART_CACHE_INVALIDATE);
Alex Deucher27359772008-05-28 12:54:16 +1000970
Maciej Cencora60f92682008-02-19 21:32:45 +1000971 do {
Alex Deucher45e51902008-05-28 13:28:59 +1000972 temp = IGP_READ_MCIND(dev_priv, RS480_GART_CACHE_CNTRL);
973 if ((temp & RS480_GART_CACHE_INVALIDATE) == 0)
Maciej Cencora60f92682008-02-19 21:32:45 +1000974 break;
975 DRM_UDELAY(1);
976 } while (1);
977
Alex Deucher45e51902008-05-28 13:28:59 +1000978 IGP_WRITE_MCIND(RS480_GART_CACHE_CNTRL, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000979 } else {
Alex Deucher45e51902008-05-28 13:28:59 +1000980 IGP_WRITE_MCIND(RS480_AGP_ADDRESS_SPACE_SIZE, 0);
Maciej Cencora60f92682008-02-19 21:32:45 +1000981 }
982}
983
Alex Deucherc1556f72009-02-25 16:57:49 -0500984/* Enable or disable IGP GART on the chip */
985static void rs600_set_igpgart(drm_radeon_private_t *dev_priv, int on)
986{
987 u32 temp;
988 int i;
989
990 if (on) {
991 DRM_DEBUG("programming igp gart %08X %08lX %08X\n",
992 dev_priv->gart_vm_start,
993 (long)dev_priv->gart_info.bus_addr,
994 dev_priv->gart_size);
995
996 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, (RS600_EFFECTIVE_L2_CACHE_SIZE(6) |
997 RS600_EFFECTIVE_L2_QUEUE_SIZE(6)));
998
999 for (i = 0; i < 19; i++)
1000 IGP_WRITE_MCIND(RS600_MC_PT0_CLIENT0_CNTL + i,
1001 (RS600_ENABLE_TRANSLATION_MODE_OVERRIDE |
1002 RS600_SYSTEM_ACCESS_MODE_IN_SYS |
1003 RS600_SYSTEM_APERTURE_UNMAPPED_ACCESS_PASSTHROUGH |
1004 RS600_EFFECTIVE_L1_CACHE_SIZE(3) |
1005 RS600_ENABLE_FRAGMENT_PROCESSING |
1006 RS600_EFFECTIVE_L1_QUEUE_SIZE(3)));
1007
1008 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_CNTL, (RS600_ENABLE_PAGE_TABLE |
1009 RS600_PAGE_TABLE_TYPE_FLAT));
1010
1011 /* disable all other contexts */
1012 for (i = 1; i < 8; i++)
1013 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_CNTL + i, 0);
1014
1015 /* setup the page table aperture */
1016 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_BASE_ADDR,
1017 dev_priv->gart_info.bus_addr);
1018 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_START_ADDR,
1019 dev_priv->gart_vm_start);
1020 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_FLAT_END_ADDR,
1021 (dev_priv->gart_vm_start + dev_priv->gart_size - 1));
1022 IGP_WRITE_MCIND(RS600_MC_PT0_CONTEXT0_DEFAULT_READ_ADDR, 0);
1023
1024 /* setup the system aperture */
1025 IGP_WRITE_MCIND(RS600_MC_PT0_SYSTEM_APERTURE_LOW_ADDR,
1026 dev_priv->gart_vm_start);
1027 IGP_WRITE_MCIND(RS600_MC_PT0_SYSTEM_APERTURE_HIGH_ADDR,
1028 (dev_priv->gart_vm_start + dev_priv->gart_size - 1));
1029
1030 /* enable page tables */
1031 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
1032 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, (temp | RS600_ENABLE_PT));
1033
1034 temp = IGP_READ_MCIND(dev_priv, RS600_MC_CNTL1);
1035 IGP_WRITE_MCIND(RS600_MC_CNTL1, (temp | RS600_ENABLE_PAGE_TABLES));
1036
1037 /* invalidate the cache */
1038 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
1039
1040 temp &= ~(RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE);
1041 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
1042 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
1043
1044 temp |= RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE;
1045 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
1046 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
1047
1048 temp &= ~(RS600_INVALIDATE_ALL_L1_TLBS | RS600_INVALIDATE_L2_CACHE);
1049 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, temp);
1050 temp = IGP_READ_MCIND(dev_priv, RS600_MC_PT0_CNTL);
1051
1052 } else {
1053 IGP_WRITE_MCIND(RS600_MC_PT0_CNTL, 0);
1054 temp = IGP_READ_MCIND(dev_priv, RS600_MC_CNTL1);
1055 temp &= ~RS600_ENABLE_PAGE_TABLES;
1056 IGP_WRITE_MCIND(RS600_MC_CNTL1, temp);
1057 }
1058}
1059
Dave Airlieea98a922005-09-11 20:28:11 +10001060static void radeon_set_pciegart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Dave Airlieea98a922005-09-11 20:28:11 +10001062 u32 tmp = RADEON_READ_PCIE(dev_priv, RADEON_PCIE_TX_GART_CNTL);
1063 if (on) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Dave Airlieea98a922005-09-11 20:28:11 +10001065 DRM_DEBUG("programming pcie %08X %08lX %08X\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001066 dev_priv->gart_vm_start,
1067 (long)dev_priv->gart_info.bus_addr,
Dave Airlieea98a922005-09-11 20:28:11 +10001068 dev_priv->gart_size);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001069 RADEON_WRITE_PCIE(RADEON_PCIE_TX_DISCARD_RD_ADDR_LO,
1070 dev_priv->gart_vm_start);
1071 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_BASE,
1072 dev_priv->gart_info.bus_addr);
1073 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_START_LO,
1074 dev_priv->gart_vm_start);
1075 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_END_LO,
1076 dev_priv->gart_vm_start +
1077 dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001079 radeon_write_agp_location(dev_priv, 0xffffffc0); /* ?? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001081 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
1082 RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001084 RADEON_WRITE_PCIE(RADEON_PCIE_TX_GART_CNTL,
1085 tmp & ~RADEON_PCIE_TX_GART_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
1087}
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/* Enable or disable PCI GART on the chip */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001090static void radeon_set_pcigart(drm_radeon_private_t * dev_priv, int on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Dave Airlied985c102006-01-02 21:32:48 +11001092 u32 tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Alex Deucher45e51902008-05-28 13:28:59 +10001094 if (((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS690) ||
Alex Deucherf0738e92008-10-16 17:12:02 +10001095 ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS740) ||
Alex Deucher45e51902008-05-28 13:28:59 +10001096 (dev_priv->flags & RADEON_IS_IGPGART)) {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001097 radeon_set_igpgart(dev_priv, on);
1098 return;
1099 }
1100
Alex Deucherc1556f72009-02-25 16:57:49 -05001101 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600) {
1102 rs600_set_igpgart(dev_priv, on);
1103 return;
1104 }
1105
Dave Airlie54a56ac2006-09-22 04:25:09 +10001106 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieea98a922005-09-11 20:28:11 +10001107 radeon_set_pciegart(dev_priv, on);
1108 return;
1109 }
1110
Dave Airliebc5f4522007-11-05 12:50:58 +10001111 tmp = RADEON_READ(RADEON_AIC_CNTL);
Dave Airlied985c102006-01-02 21:32:48 +11001112
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001113 if (on) {
1114 RADEON_WRITE(RADEON_AIC_CNTL,
1115 tmp | RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 /* set PCI GART page-table base address
1118 */
Dave Airlieea98a922005-09-11 20:28:11 +10001119 RADEON_WRITE(RADEON_AIC_PT_BASE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 /* set address range for PCI address translate
1122 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001123 RADEON_WRITE(RADEON_AIC_LO_ADDR, dev_priv->gart_vm_start);
1124 RADEON_WRITE(RADEON_AIC_HI_ADDR, dev_priv->gart_vm_start
1125 + dev_priv->gart_size - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /* Turn off AGP aperture -- is this required for PCI GART?
1128 */
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001129 radeon_write_agp_location(dev_priv, 0xffffffc0);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001130 RADEON_WRITE(RADEON_AGP_COMMAND, 0); /* clear AGP_COMMAND */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 } else {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001132 RADEON_WRITE(RADEON_AIC_CNTL,
1133 tmp & ~RADEON_PCIGART_TRANSLATE_EN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135}
1136
David Miller6abf6bb2009-02-14 01:51:07 -08001137static int radeon_setup_pcigart_surface(drm_radeon_private_t *dev_priv)
1138{
1139 struct drm_ati_pcigart_info *gart_info = &dev_priv->gart_info;
1140 struct radeon_virt_surface *vp;
1141 int i;
1142
1143 for (i = 0; i < RADEON_MAX_SURFACES * 2; i++) {
1144 if (!dev_priv->virt_surfaces[i].file_priv ||
1145 dev_priv->virt_surfaces[i].file_priv == PCIGART_FILE_PRIV)
1146 break;
1147 }
1148 if (i >= 2 * RADEON_MAX_SURFACES)
1149 return -ENOMEM;
1150 vp = &dev_priv->virt_surfaces[i];
1151
1152 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
1153 struct radeon_surface *sp = &dev_priv->surfaces[i];
1154 if (sp->refcount)
1155 continue;
1156
1157 vp->surface_index = i;
1158 vp->lower = gart_info->bus_addr;
1159 vp->upper = vp->lower + gart_info->table_size;
1160 vp->flags = 0;
1161 vp->file_priv = PCIGART_FILE_PRIV;
1162
1163 sp->refcount = 1;
1164 sp->lower = vp->lower;
1165 sp->upper = vp->upper;
1166 sp->flags = 0;
1167
1168 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, sp->flags);
1169 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND + 16 * i, sp->lower);
1170 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND + 16 * i, sp->upper);
1171 return 0;
1172 }
1173
1174 return -ENOMEM;
1175}
1176
Dave Airlie7c1c2872008-11-28 14:22:24 +10001177static int radeon_do_init_cp(struct drm_device *dev, drm_radeon_init_t *init,
1178 struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Dave Airlied985c102006-01-02 21:32:48 +11001180 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlie7c1c2872008-11-28 14:22:24 +10001181 struct drm_radeon_master_private *master_priv = file_priv->master->driver_priv;
Dave Airlied985c102006-01-02 21:32:48 +11001182
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001183 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Dave Airlief3dd5c32006-03-25 18:09:46 +11001185 /* if we require new memory map but we don't have it fail */
Dave Airlie54a56ac2006-09-22 04:25:09 +10001186 if ((dev_priv->flags & RADEON_NEW_MEMMAP) && !dev_priv->new_memmap) {
Dave Airlieb15ec362006-08-19 17:43:52 +10001187 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 +11001188 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001189 return -EINVAL;
Dave Airlief3dd5c32006-03-25 18:09:46 +11001190 }
1191
Dave Airlie54a56ac2006-09-22 04:25:09 +10001192 if (init->is_pci && (dev_priv->flags & RADEON_IS_AGP)) {
Dave Airlied985c102006-01-02 21:32:48 +11001193 DRM_DEBUG("Forcing AGP card to PCI mode\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +10001194 dev_priv->flags &= ~RADEON_IS_AGP;
1195 } else if (!(dev_priv->flags & (RADEON_IS_AGP | RADEON_IS_PCI | RADEON_IS_PCIE))
Dave Airlieb15ec362006-08-19 17:43:52 +10001196 && !init->is_pci) {
1197 DRM_DEBUG("Restoring AGP flag\n");
Dave Airlie54a56ac2006-09-22 04:25:09 +10001198 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlied985c102006-01-02 21:32:48 +11001199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Dave Airlie54a56ac2006-09-22 04:25:09 +10001201 if ((!(dev_priv->flags & RADEON_IS_AGP)) && !dev->sg) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001202 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001204 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206
1207 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001208 if (dev_priv->usec_timeout < 1 ||
1209 dev_priv->usec_timeout > RADEON_MAX_USEC_TIMEOUT) {
1210 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001212 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
1214
Dave Airlieddbee332007-07-11 12:16:01 +10001215 /* Enable vblank on CRTC1 for older X servers
1216 */
1217 dev_priv->vblank_crtc = DRM_RADEON_VBLANK_CRTC1;
1218
Dave Airlied985c102006-01-02 21:32:48 +11001219 switch(init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 case RADEON_INIT_R200_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001221 dev_priv->microcode_version = UCODE_R200;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 break;
1223 case RADEON_INIT_R300_CP:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001224 dev_priv->microcode_version = UCODE_R300;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 break;
1226 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001227 dev_priv->microcode_version = UCODE_R100;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 dev_priv->do_boxes = 0;
1231 dev_priv->cp_mode = init->cp_mode;
1232
1233 /* We don't support anything other than bus-mastering ring mode,
1234 * but the ring can be in either AGP or PCI space for the ring
1235 * read pointer.
1236 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001237 if ((init->cp_mode != RADEON_CSQ_PRIBM_INDDIS) &&
1238 (init->cp_mode != RADEON_CSQ_PRIBM_INDBM)) {
1239 DRM_DEBUG("BAD cp_mode (%x)!\n", init->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001241 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001244 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 case 16:
1246 dev_priv->color_fmt = RADEON_COLOR_FORMAT_RGB565;
1247 break;
1248 case 32:
1249 default:
1250 dev_priv->color_fmt = RADEON_COLOR_FORMAT_ARGB8888;
1251 break;
1252 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001253 dev_priv->front_offset = init->front_offset;
1254 dev_priv->front_pitch = init->front_pitch;
1255 dev_priv->back_offset = init->back_offset;
1256 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001258 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 case 16:
1260 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_16BIT_INT_Z;
1261 break;
1262 case 32:
1263 default:
1264 dev_priv->depth_fmt = RADEON_DEPTH_FORMAT_24BIT_INT_Z;
1265 break;
1266 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001267 dev_priv->depth_offset = init->depth_offset;
1268 dev_priv->depth_pitch = init->depth_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /* Hardware state for depth clears. Remove this if/when we no
1271 * longer clear the depth buffer with a 3D rectangle. Hard-code
1272 * all values to prevent unwanted 3D state from slipping through
1273 * and screwing with the clear operation.
1274 */
1275 dev_priv->depth_clear.rb3d_cntl = (RADEON_PLANE_MASK_ENABLE |
1276 (dev_priv->color_fmt << 10) |
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001277 (dev_priv->microcode_version ==
1278 UCODE_R100 ? RADEON_ZBLOCK16 : 0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001280 dev_priv->depth_clear.rb3d_zstencilcntl =
1281 (dev_priv->depth_fmt |
1282 RADEON_Z_TEST_ALWAYS |
1283 RADEON_STENCIL_TEST_ALWAYS |
1284 RADEON_STENCIL_S_FAIL_REPLACE |
1285 RADEON_STENCIL_ZPASS_REPLACE |
1286 RADEON_STENCIL_ZFAIL_REPLACE | RADEON_Z_WRITE_ENABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287
1288 dev_priv->depth_clear.se_cntl = (RADEON_FFACE_CULL_CW |
1289 RADEON_BFACE_SOLID |
1290 RADEON_FFACE_SOLID |
1291 RADEON_FLAT_SHADE_VTX_LAST |
1292 RADEON_DIFFUSE_SHADE_FLAT |
1293 RADEON_ALPHA_SHADE_FLAT |
1294 RADEON_SPECULAR_SHADE_FLAT |
1295 RADEON_FOG_SHADE_FLAT |
1296 RADEON_VTX_PIX_CENTER_OGL |
1297 RADEON_ROUND_MODE_TRUNC |
1298 RADEON_ROUND_PREC_8TH_PIX);
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 dev_priv->ring_offset = init->ring_offset;
1302 dev_priv->ring_rptr_offset = init->ring_rptr_offset;
1303 dev_priv->buffers_offset = init->buffers_offset;
1304 dev_priv->gart_textures_offset = init->gart_textures_offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001305
Dave Airlie7c1c2872008-11-28 14:22:24 +10001306 master_priv->sarea = drm_getsarea(dev);
1307 if (!master_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 DRM_ERROR("could not find sarea!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001310 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 dev_priv->cp_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001314 if (!dev_priv->cp_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 DRM_ERROR("could not find cp ring region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001317 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 }
1319 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001320 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 DRM_ERROR("could not find ring read pointer!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001323 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 }
Dave Airlied1f2b552005-08-05 22:11:22 +10001325 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001327 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 DRM_ERROR("could not find dma buffer region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001330 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
1332
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001333 if (init->gart_textures_offset) {
1334 dev_priv->gart_textures =
1335 drm_core_findmap(dev, init->gart_textures_offset);
1336 if (!dev_priv->gart_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 DRM_ERROR("could not find GART texture region!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001339 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 }
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001344 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlie9b8d5a12009-02-07 11:15:41 +10001345 drm_core_ioremap_wc(dev_priv->cp_ring, dev);
1346 drm_core_ioremap_wc(dev_priv->ring_rptr, dev);
1347 drm_core_ioremap_wc(dev->agp_buffer_map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001348 if (!dev_priv->cp_ring->handle ||
1349 !dev_priv->ring_rptr->handle ||
1350 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 DRM_ERROR("could not find ioremap agp regions!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001353 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 }
1355 } else
1356#endif
1357 {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001358 dev_priv->cp_ring->handle =
1359 (void *)(unsigned long)dev_priv->cp_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 dev_priv->ring_rptr->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001361 (void *)(unsigned long)dev_priv->ring_rptr->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001362 dev->agp_buffer_map->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001363 (void *)(unsigned long)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001365 DRM_DEBUG("dev_priv->cp_ring->handle %p\n",
1366 dev_priv->cp_ring->handle);
1367 DRM_DEBUG("dev_priv->ring_rptr->handle %p\n",
1368 dev_priv->ring_rptr->handle);
1369 DRM_DEBUG("dev->agp_buffer_map->handle %p\n",
1370 dev->agp_buffer_map->handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
1372
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001373 dev_priv->fb_location = (radeon_read_fb_location(dev_priv) & 0xffff) << 16;
Dave Airliebc5f4522007-11-05 12:50:58 +10001374 dev_priv->fb_size =
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001375 ((radeon_read_fb_location(dev_priv) & 0xffff0000u) + 0x10000)
Dave Airlied5ea7022006-03-19 19:37:55 +11001376 - dev_priv->fb_location;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001378 dev_priv->front_pitch_offset = (((dev_priv->front_pitch / 64) << 22) |
1379 ((dev_priv->front_offset
1380 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001382 dev_priv->back_pitch_offset = (((dev_priv->back_pitch / 64) << 22) |
1383 ((dev_priv->back_offset
1384 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001386 dev_priv->depth_pitch_offset = (((dev_priv->depth_pitch / 64) << 22) |
1387 ((dev_priv->depth_offset
1388 + dev_priv->fb_location) >> 10));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
1390 dev_priv->gart_size = init->gart_size;
Dave Airlied5ea7022006-03-19 19:37:55 +11001391
1392 /* New let's set the memory map ... */
1393 if (dev_priv->new_memmap) {
1394 u32 base = 0;
1395
1396 DRM_INFO("Setting GART location based on new memory map\n");
1397
1398 /* If using AGP, try to locate the AGP aperture at the same
1399 * location in the card and on the bus, though we have to
1400 * align it down.
1401 */
1402#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001403 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001404 base = dev->agp->base;
1405 /* Check if valid */
Michel Dänzer80b2c382007-02-18 18:03:21 +11001406 if ((base + dev_priv->gart_size - 1) >= dev_priv->fb_location &&
1407 base < (dev_priv->fb_location + dev_priv->fb_size - 1)) {
Dave Airlied5ea7022006-03-19 19:37:55 +11001408 DRM_INFO("Can't use AGP base @0x%08lx, won't fit\n",
1409 dev->agp->base);
1410 base = 0;
1411 }
1412 }
1413#endif
1414 /* If not or if AGP is at 0 (Macs), try to put it elsewhere */
1415 if (base == 0) {
1416 base = dev_priv->fb_location + dev_priv->fb_size;
Michel Dänzer80b2c382007-02-18 18:03:21 +11001417 if (base < dev_priv->fb_location ||
1418 ((base + dev_priv->gart_size) & 0xfffffffful) < base)
Dave Airlied5ea7022006-03-19 19:37:55 +11001419 base = dev_priv->fb_location
1420 - dev_priv->gart_size;
Dave Airliebc5f4522007-11-05 12:50:58 +10001421 }
Dave Airlied5ea7022006-03-19 19:37:55 +11001422 dev_priv->gart_vm_start = base & 0xffc00000u;
1423 if (dev_priv->gart_vm_start != base)
1424 DRM_INFO("GART aligned down from 0x%08x to 0x%08x\n",
1425 base, dev_priv->gart_vm_start);
1426 } else {
1427 DRM_INFO("Setting GART location based on old memory map\n");
1428 dev_priv->gart_vm_start = dev_priv->fb_location +
1429 RADEON_READ(RADEON_CONFIG_APER_SIZE);
1430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
1432#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001433 if (dev_priv->flags & RADEON_IS_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001435 - dev->agp->base
1436 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 else
1438#endif
1439 dev_priv->gart_buffers_offset = (dev->agp_buffer_map->offset
Ivan Kokshayskyb0917bd2005-10-26 11:05:25 +01001440 - (unsigned long)dev->sg->virtual
1441 + dev_priv->gart_vm_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001443 DRM_DEBUG("dev_priv->gart_size %d\n", dev_priv->gart_size);
1444 DRM_DEBUG("dev_priv->gart_vm_start 0x%x\n", dev_priv->gart_vm_start);
1445 DRM_DEBUG("dev_priv->gart_buffers_offset 0x%lx\n",
1446 dev_priv->gart_buffers_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001448 dev_priv->ring.start = (u32 *) dev_priv->cp_ring->handle;
1449 dev_priv->ring.end = ((u32 *) dev_priv->cp_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 + init->ring_size / sizeof(u32));
1451 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001452 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
Roland Scheidegger576cc452008-02-07 14:59:24 +10001454 dev_priv->ring.rptr_update = /* init->rptr_update */ 4096;
1455 dev_priv->ring.rptr_update_l2qw = drm_order( /* init->rptr_update */ 4096 / 8);
1456
1457 dev_priv->ring.fetch_size = /* init->fetch_size */ 32;
1458 dev_priv->ring.fetch_size_l2ow = drm_order( /* init->fetch_size */ 32 / 16);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001459 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 dev_priv->ring.high_mark = RADEON_RING_HIGH_MARK;
1462
1463#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001464 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001466 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 } else
1468#endif
1469 {
David Miller6abf6bb2009-02-14 01:51:07 -08001470 u32 sctrl;
1471 int ret;
1472
Dave Airlieb05c2382008-03-17 10:24:24 +10001473 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +10001474 /* if we have an offset set from userspace */
Dave Airlief2b04cd2007-05-08 15:19:23 +10001475 if (dev_priv->pcigart_offset_set) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001476 dev_priv->gart_info.bus_addr =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +11001477 (resource_size_t)dev_priv->pcigart_offset + dev_priv->fb_location;
Dave Airlief26c4732006-01-02 17:18:39 +11001478 dev_priv->gart_info.mapping.offset =
Dave Airlie7fc86862007-11-05 10:45:27 +10001479 dev_priv->pcigart_offset + dev_priv->fb_aper_offset;
Dave Airlief26c4732006-01-02 17:18:39 +11001480 dev_priv->gart_info.mapping.size =
Dave Airlief2b04cd2007-05-08 15:19:23 +10001481 dev_priv->gart_info.table_size;
Dave Airlief26c4732006-01-02 17:18:39 +11001482
Dave Airlie242e3df2008-07-15 15:48:05 +10001483 drm_core_ioremap_wc(&dev_priv->gart_info.mapping, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001484 dev_priv->gart_info.addr =
Dave Airlief26c4732006-01-02 17:18:39 +11001485 dev_priv->gart_info.mapping.handle;
Dave Airlieea98a922005-09-11 20:28:11 +10001486
Dave Airlief2b04cd2007-05-08 15:19:23 +10001487 if (dev_priv->flags & RADEON_IS_PCIE)
1488 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCIE;
1489 else
1490 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001491 dev_priv->gart_info.gart_table_location =
1492 DRM_ATI_GART_FB;
1493
Dave Airlief26c4732006-01-02 17:18:39 +11001494 DRM_DEBUG("Setting phys_pci_gart to %p %08lX\n",
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001495 dev_priv->gart_info.addr,
1496 dev_priv->pcigart_offset);
1497 } else {
Dave Airlief2b04cd2007-05-08 15:19:23 +10001498 if (dev_priv->flags & RADEON_IS_IGPGART)
1499 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_IGP;
1500 else
1501 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001502 dev_priv->gart_info.gart_table_location =
1503 DRM_ATI_GART_MAIN;
Dave Airlief26c4732006-01-02 17:18:39 +11001504 dev_priv->gart_info.addr = NULL;
1505 dev_priv->gart_info.bus_addr = 0;
Dave Airlie54a56ac2006-09-22 04:25:09 +10001506 if (dev_priv->flags & RADEON_IS_PCIE) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001507 DRM_ERROR
1508 ("Cannot use PCI Express without GART in FB memory\n");
Dave Airlieea98a922005-09-11 20:28:11 +10001509 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001510 return -EINVAL;
Dave Airlieea98a922005-09-11 20:28:11 +10001511 }
1512 }
1513
David Miller6abf6bb2009-02-14 01:51:07 -08001514 sctrl = RADEON_READ(RADEON_SURFACE_CNTL);
1515 RADEON_WRITE(RADEON_SURFACE_CNTL, 0);
Alex Deucherc1556f72009-02-25 16:57:49 -05001516 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
1517 ret = r600_page_table_init(dev);
1518 else
1519 ret = drm_ati_pcigart_init(dev, &dev_priv->gart_info);
David Miller6abf6bb2009-02-14 01:51:07 -08001520 RADEON_WRITE(RADEON_SURFACE_CNTL, sctrl);
1521
1522 if (!ret) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001523 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 radeon_do_cleanup_cp(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +10001525 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 }
1527
David Miller6abf6bb2009-02-14 01:51:07 -08001528 ret = radeon_setup_pcigart_surface(dev_priv);
1529 if (ret) {
1530 DRM_ERROR("failed to setup GART surface!\n");
Alex Deucherc1556f72009-02-25 16:57:49 -05001531 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
1532 r600_page_table_cleanup(dev, &dev_priv->gart_info);
1533 else
1534 drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info);
David Miller6abf6bb2009-02-14 01:51:07 -08001535 radeon_do_cleanup_cp(dev);
1536 return ret;
1537 }
1538
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001540 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542
Ben Hutchings70967ab2009-08-29 14:53:51 +01001543 if (!dev_priv->me_fw) {
1544 int err = radeon_cp_init_microcode(dev_priv);
1545 if (err) {
1546 DRM_ERROR("Failed to load firmware!\n");
1547 radeon_do_cleanup_cp(dev);
1548 return err;
1549 }
1550 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001551 radeon_cp_load_microcode(dev_priv);
etienne3d161182009-02-20 09:44:45 +10001552 radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
1554 dev_priv->last_buf = 0;
1555
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001556 radeon_do_engine_reset(dev);
Dave Airlied5ea7022006-03-19 19:37:55 +11001557 radeon_test_writeback(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 return 0;
1560}
1561
Dave Airlie84b1fd12007-07-11 15:53:27 +10001562static int radeon_do_cleanup_cp(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
1564 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001565 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
1567 /* Make sure interrupts are disabled here because the uninstall ioctl
1568 * may not have been called from userspace and after dev_private
1569 * is freed, it's too late.
1570 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001571 if (dev->irq_enabled)
1572 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
1574#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001575 if (dev_priv->flags & RADEON_IS_AGP) {
Dave Airlied985c102006-01-02 21:32:48 +11001576 if (dev_priv->cp_ring != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001577 drm_core_ioremapfree(dev_priv->cp_ring, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001578 dev_priv->cp_ring = NULL;
1579 }
1580 if (dev_priv->ring_rptr != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001581 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlied985c102006-01-02 21:32:48 +11001582 dev_priv->ring_rptr = NULL;
1583 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001584 if (dev->agp_buffer_map != NULL) {
1585 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 dev->agp_buffer_map = NULL;
1587 }
1588 } else
1589#endif
1590 {
Dave Airlied985c102006-01-02 21:32:48 +11001591
1592 if (dev_priv->gart_info.bus_addr) {
1593 /* Turn off PCI GART */
1594 radeon_set_pcigart(dev_priv, 0);
Alex Deucherc1556f72009-02-25 16:57:49 -05001595 if ((dev_priv->flags & RADEON_FAMILY_MASK) == CHIP_RS600)
1596 r600_page_table_cleanup(dev, &dev_priv->gart_info);
1597 else {
1598 if (!drm_ati_pcigart_cleanup(dev, &dev_priv->gart_info))
1599 DRM_ERROR("failed to cleanup PCI GART!\n");
1600 }
Dave Airlied985c102006-01-02 21:32:48 +11001601 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001602
Dave Airlied985c102006-01-02 21:32:48 +11001603 if (dev_priv->gart_info.gart_table_location == DRM_ATI_GART_FB)
1604 {
Dave Airlief26c4732006-01-02 17:18:39 +11001605 drm_core_ioremapfree(&dev_priv->gart_info.mapping, dev);
Hannes Eder8f497aa2009-03-05 20:14:18 +01001606 dev_priv->gart_info.addr = NULL;
Dave Airlieea98a922005-09-11 20:28:11 +10001607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 /* only clear to the start of flags */
1610 memset(dev_priv, 0, offsetof(drm_radeon_private_t, flags));
1611
1612 return 0;
1613}
1614
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001615/* This code will reinit the Radeon CP hardware after a resume from disc.
1616 * AFAIK, it would be very difficult to pickle the state at suspend time, so
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * here we make sure that all Radeon hardware initialisation is re-done without
1618 * affecting running applications.
1619 *
1620 * Charl P. Botha <http://cpbotha.net>
1621 */
etienne3d161182009-02-20 09:44:45 +10001622static int radeon_do_resume_cp(struct drm_device *dev, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 drm_radeon_private_t *dev_priv = dev->dev_private;
1625
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001626 if (!dev_priv) {
1627 DRM_ERROR("Called with no initialization\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001628 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630
1631 DRM_DEBUG("Starting radeon_do_resume_cp()\n");
1632
1633#if __OS_HAS_AGP
Dave Airlie54a56ac2006-09-22 04:25:09 +10001634 if (dev_priv->flags & RADEON_IS_AGP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 /* Turn off PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001636 radeon_set_pcigart(dev_priv, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 } else
1638#endif
1639 {
1640 /* Turn on PCI GART */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001641 radeon_set_pcigart(dev_priv, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 }
1643
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001644 radeon_cp_load_microcode(dev_priv);
etienne3d161182009-02-20 09:44:45 +10001645 radeon_cp_init_ring_buffer(dev, dev_priv, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Dave Airlie566d84d2010-02-24 17:17:13 +10001647 dev_priv->have_z_offset = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001648 radeon_do_engine_reset(dev);
Jesse Barnes0a3e67a2008-09-30 12:14:26 -07001649 radeon_irq_set_state(dev, RADEON_SW_INT_ENABLE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
1651 DRM_DEBUG("radeon_do_resume_cp() complete\n");
1652
1653 return 0;
1654}
1655
Eric Anholtc153f452007-09-03 12:06:45 +10001656int radeon_cp_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Alex Deucherc05ce082009-02-24 16:22:29 -05001658 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001659 drm_radeon_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
Eric Anholt6c340ea2007-08-25 20:23:09 +10001661 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Eric Anholtc153f452007-09-03 12:06:45 +10001663 if (init->func == RADEON_INIT_R300_CP)
Dave Airlie3d5e2c12008-02-07 15:01:05 +10001664 r300_init_reg_flags(dev);
Dave Airlie414ed532005-08-16 20:43:16 +10001665
Eric Anholtc153f452007-09-03 12:06:45 +10001666 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 case RADEON_INIT_CP:
1668 case RADEON_INIT_R200_CP:
1669 case RADEON_INIT_R300_CP:
Dave Airlie7c1c2872008-11-28 14:22:24 +10001670 return radeon_do_init_cp(dev, init, file_priv);
Alex Deucherc05ce082009-02-24 16:22:29 -05001671 case RADEON_INIT_R600_CP:
1672 return r600_do_init_cp(dev, init, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 case RADEON_CLEANUP_CP:
Alex Deucherc05ce082009-02-24 16:22:29 -05001674 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1675 return r600_do_cleanup_cp(dev);
1676 else
1677 return radeon_do_cleanup_cp(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
1679
Eric Anholt20caafa2007-08-25 19:22:43 +10001680 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Eric Anholtc153f452007-09-03 12:06:45 +10001683int radeon_cp_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001686 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
Eric Anholt6c340ea2007-08-25 20:23:09 +10001688 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001690 if (dev_priv->cp_running) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001691 DRM_DEBUG("while CP running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return 0;
1693 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001694 if (dev_priv->cp_mode == RADEON_CSQ_PRIDIS_INDDIS) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001695 DRM_DEBUG("called with bogus CP mode (%d)\n",
1696 dev_priv->cp_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 return 0;
1698 }
1699
Alex Deucherc05ce082009-02-24 16:22:29 -05001700 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1701 r600_do_cp_start(dev_priv);
1702 else
1703 radeon_do_cp_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
1705 return 0;
1706}
1707
1708/* Stop the CP. The engine must have been idled before calling this
1709 * routine.
1710 */
Eric Anholtc153f452007-09-03 12:06:45 +10001711int radeon_cp_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 drm_radeon_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +10001714 drm_radeon_cp_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001716 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Eric Anholt6c340ea2007-08-25 20:23:09 +10001718 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (!dev_priv->cp_running)
1721 return 0;
1722
1723 /* Flush any pending CP commands. This ensures any outstanding
1724 * commands are exectuted by the engine before we turn it off.
1725 */
Eric Anholtc153f452007-09-03 12:06:45 +10001726 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001727 radeon_do_cp_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729
1730 /* If we fail to make the engine go idle, we return an error
1731 * code so that the DRM ioctl wrapper can try again.
1732 */
Eric Anholtc153f452007-09-03 12:06:45 +10001733 if (stop->idle) {
Alex Deucherc05ce082009-02-24 16:22:29 -05001734 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1735 ret = r600_do_cp_idle(dev_priv);
1736 else
1737 ret = radeon_do_cp_idle(dev_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001738 if (ret)
1739 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 }
1741
1742 /* Finally, we can turn off the CP. If the engine isn't idle,
1743 * we will get some dropped triangles as they won't be fully
1744 * rendered before the CP is shut down.
1745 */
Alex Deucherc05ce082009-02-24 16:22:29 -05001746 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1747 r600_do_cp_stop(dev_priv);
1748 else
1749 radeon_do_cp_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 /* Reset the engine */
Alex Deucherc05ce082009-02-24 16:22:29 -05001752 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1753 r600_do_engine_reset(dev);
1754 else
1755 radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 return 0;
1758}
1759
Dave Airlie84b1fd12007-07-11 15:53:27 +10001760void radeon_do_release(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
1762 drm_radeon_private_t *dev_priv = dev->dev_private;
1763 int i, ret;
1764
1765 if (dev_priv) {
1766 if (dev_priv->cp_running) {
1767 /* Stop the cp */
Dave Airlie53c379e2009-03-09 12:12:28 +10001768 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
Alex Deucherc05ce082009-02-24 16:22:29 -05001769 while ((ret = r600_do_cp_idle(dev_priv)) != 0) {
1770 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771#ifdef __linux__
Alex Deucherc05ce082009-02-24 16:22:29 -05001772 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773#else
Alex Deucherc05ce082009-02-24 16:22:29 -05001774 tsleep(&ret, PZERO, "rdnrel", 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775#endif
Alex Deucherc05ce082009-02-24 16:22:29 -05001776 }
1777 } else {
1778 while ((ret = radeon_do_cp_idle(dev_priv)) != 0) {
1779 DRM_DEBUG("radeon_do_cp_idle %d\n", ret);
1780#ifdef __linux__
1781 schedule();
1782#else
1783 tsleep(&ret, PZERO, "rdnrel", 1);
1784#endif
1785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
Alex Deucherc05ce082009-02-24 16:22:29 -05001787 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
1788 r600_do_cp_stop(dev_priv);
1789 r600_do_engine_reset(dev);
1790 } else {
1791 radeon_do_cp_stop(dev_priv);
1792 radeon_do_engine_reset(dev);
1793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795
Alex Deucherc05ce082009-02-24 16:22:29 -05001796 if ((dev_priv->flags & RADEON_FAMILY_MASK) < CHIP_R600) {
1797 /* Disable *all* interrupts */
1798 if (dev_priv->mmio) /* remove this after permanent addmaps */
1799 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Alex Deucherc05ce082009-02-24 16:22:29 -05001801 if (dev_priv->mmio) { /* remove all surfaces */
1802 for (i = 0; i < RADEON_MAX_SURFACES; i++) {
1803 RADEON_WRITE(RADEON_SURFACE0_INFO + 16 * i, 0);
1804 RADEON_WRITE(RADEON_SURFACE0_LOWER_BOUND +
1805 16 * i, 0);
1806 RADEON_WRITE(RADEON_SURFACE0_UPPER_BOUND +
1807 16 * i, 0);
1808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810 }
1811
1812 /* Free memory heap structures */
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001813 radeon_mem_takedown(&(dev_priv->gart_heap));
1814 radeon_mem_takedown(&(dev_priv->fb_heap));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
1816 /* deallocate kernel resources */
Alex Deucherc05ce082009-02-24 16:22:29 -05001817 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1818 r600_do_cleanup_cp(dev);
1819 else
1820 radeon_do_cleanup_cp(dev);
Ben Hutchings70967ab2009-08-29 14:53:51 +01001821 if (dev_priv->me_fw) {
1822 release_firmware(dev_priv->me_fw);
1823 dev_priv->me_fw = NULL;
1824 }
1825 if (dev_priv->pfp_fw) {
1826 release_firmware(dev_priv->pfp_fw);
1827 dev_priv->pfp_fw = NULL;
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
1830}
1831
1832/* Just reset the CP ring. Called as part of an X Server engine reset.
1833 */
Eric Anholtc153f452007-09-03 12:06:45 +10001834int radeon_cp_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001837 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Eric Anholt6c340ea2007-08-25 20:23:09 +10001839 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001841 if (!dev_priv) {
Márton Németh3e684ea2008-01-24 15:58:57 +10001842 DRM_DEBUG("called before init done\n");
Eric Anholt20caafa2007-08-25 19:22:43 +10001843 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845
Alex Deucherc05ce082009-02-24 16:22:29 -05001846 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1847 r600_do_cp_reset(dev_priv);
1848 else
1849 radeon_do_cp_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* The CP is no longer running after an engine reset */
1852 dev_priv->cp_running = 0;
1853
1854 return 0;
1855}
1856
Eric Anholtc153f452007-09-03 12:06:45 +10001857int radeon_cp_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001860 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Eric Anholt6c340ea2007-08-25 20:23:09 +10001862 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Alex Deucherc05ce082009-02-24 16:22:29 -05001864 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1865 return r600_do_cp_idle(dev_priv);
1866 else
1867 return radeon_do_cp_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870/* Added by Charl P. Botha to call radeon_do_resume_cp().
1871 */
Eric Anholtc153f452007-09-03 12:06:45 +10001872int radeon_cp_resume(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Alex Deucherc05ce082009-02-24 16:22:29 -05001874 drm_radeon_private_t *dev_priv = dev->dev_private;
1875 DRM_DEBUG("\n");
1876
1877 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1878 return r600_do_resume_cp(dev, file_priv);
1879 else
1880 return radeon_do_resume_cp(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Eric Anholtc153f452007-09-03 12:06:45 +10001883int radeon_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Alex Deucherc05ce082009-02-24 16:22:29 -05001885 drm_radeon_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001886 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Eric Anholt6c340ea2007-08-25 20:23:09 +10001888 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Alex Deucherc05ce082009-02-24 16:22:29 -05001890 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
1891 return r600_do_engine_reset(dev);
1892 else
1893 return radeon_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894}
1895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896/* ================================================================
1897 * Fullscreen mode
1898 */
1899
1900/* KW: Deprecated to say the least:
1901 */
Eric Anholtc153f452007-09-03 12:06:45 +10001902int radeon_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903{
1904 return 0;
1905}
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907/* ================================================================
1908 * Freelist management
1909 */
1910
1911/* Original comment: FIXME: ROTATE_BUFS is a hack to cycle through
1912 * bufs until freelist code is used. Note this hides a problem with
1913 * the scratch register * (used to keep track of last buffer
1914 * completed) being written to before * the last buffer has actually
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001915 * completed rendering.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 *
1917 * KW: It's also a good way to find free buffers quickly.
1918 *
1919 * KW: Ideally this loop wouldn't exist, and freelist_get wouldn't
1920 * sleep. However, bugs in older versions of radeon_accel.c mean that
1921 * we essentially have to do this, else old clients will break.
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001922 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 * However, it does leave open a potential deadlock where all the
1924 * buffers are held by other clients, which can't release them because
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001925 * they can't get the lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 */
1927
Dave Airlie056219e2007-07-11 16:17:42 +10001928struct drm_buf *radeon_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Dave Airliecdd55a22007-07-11 16:32:08 +10001930 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 drm_radeon_private_t *dev_priv = dev->dev_private;
1932 drm_radeon_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +10001933 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 int i, t;
1935 int start;
1936
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001937 if (++dev_priv->last_buf >= dma->buf_count)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 dev_priv->last_buf = 0;
1939
1940 start = dev_priv->last_buf;
1941
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001942 for (t = 0; t < dev_priv->usec_timeout; t++) {
David Millerb07fa022009-02-12 02:15:37 -08001943 u32 done_age = GET_SCRATCH(dev_priv, 1);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001944 DRM_DEBUG("done_age = %d\n", done_age);
Robert Noland0a5c1e62009-10-20 07:23:07 -05001945 for (i = 0; i < dma->buf_count; i++) {
1946 buf = dma->buflist[start];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 buf_priv = buf->dev_private;
Eric Anholt6c340ea2007-08-25 20:23:09 +10001948 if (buf->file_priv == NULL || (buf->pending &&
1949 buf_priv->age <=
1950 done_age)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 dev_priv->stats.requested_bufs++;
1952 buf->pending = 0;
1953 return buf;
1954 }
Robert Noland0a5c1e62009-10-20 07:23:07 -05001955 if (++start >= dma->buf_count)
1956 start = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 }
1958
1959 if (t) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001960 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 dev_priv->stats.freelist_loops++;
1962 }
1963 }
1964
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 return NULL;
1966}
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001967
Dave Airlie84b1fd12007-07-11 15:53:27 +10001968void radeon_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Dave Airliecdd55a22007-07-11 16:32:08 +10001970 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 drm_radeon_private_t *dev_priv = dev->dev_private;
1972 int i;
1973
1974 dev_priv->last_buf = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001975 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001976 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 drm_radeon_buf_priv_t *buf_priv = buf->dev_private;
1978 buf_priv->age = 0;
1979 }
1980}
1981
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982/* ================================================================
1983 * CP command submission
1984 */
1985
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001986int radeon_wait_ring(drm_radeon_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987{
1988 drm_radeon_ring_buffer_t *ring = &dev_priv->ring;
1989 int i;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001990 u32 last_head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001992 for (i = 0; i < dev_priv->usec_timeout; i++) {
1993 u32 head = GET_RING_HEAD(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 ring->space = (head - ring->tail) * sizeof(u32);
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001996 if (ring->space <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 ring->space += ring->size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001998 if (ring->space > n)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002000
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 dev_priv->stats.boxes |= RADEON_BOX_WAIT_IDLE;
2002
2003 if (head != last_head)
2004 i = 0;
2005 last_head = head;
2006
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002007 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009
2010 /* FIXME: This return value is ignored in the BEGIN_RING macro! */
2011#if RADEON_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002012 radeon_status(dev_priv);
2013 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014#endif
Eric Anholt20caafa2007-08-25 19:22:43 +10002015 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016}
2017
Eric Anholt6c340ea2007-08-25 20:23:09 +10002018static int radeon_cp_get_buffers(struct drm_device *dev,
2019 struct drm_file *file_priv,
Dave Airliec60ce622007-07-11 15:27:12 +10002020 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 int i;
Dave Airlie056219e2007-07-11 16:17:42 +10002023 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002025 for (i = d->granted_count; i < d->request_count; i++) {
2026 buf = radeon_freelist_get(dev);
2027 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +10002028 return -EBUSY; /* NOTE: broken client */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
Eric Anholt6c340ea2007-08-25 20:23:09 +10002030 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002032 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
2033 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002034 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002035 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
2036 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +10002037 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039 d->granted_count++;
2040 }
2041 return 0;
2042}
2043
Eric Anholtc153f452007-09-03 12:06:45 +10002044int radeon_cp_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Dave Airliecdd55a22007-07-11 16:32:08 +10002046 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +10002048 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Eric Anholt6c340ea2007-08-25 20:23:09 +10002050 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 /* Please don't send us buffers.
2053 */
Eric Anholtc153f452007-09-03 12:06:45 +10002054 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002055 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002056 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10002057 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059
2060 /* We'll send you buffers.
2061 */
Eric Anholtc153f452007-09-03 12:06:45 +10002062 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002063 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10002064 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10002065 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 }
2067
Eric Anholtc153f452007-09-03 12:06:45 +10002068 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Eric Anholtc153f452007-09-03 12:06:45 +10002070 if (d->request_count) {
2071 ret = radeon_cp_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 }
2073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 return ret;
2075}
2076
Dave Airlie22eae942005-11-10 22:16:34 +11002077int radeon_driver_load(struct drm_device *dev, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 drm_radeon_private_t *dev_priv;
2080 int ret = 0;
2081
Eric Anholt9a298b22009-03-24 12:23:04 -07002082 dev_priv = kzalloc(sizeof(drm_radeon_private_t), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +10002084 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 dev->dev_private = (void *)dev_priv;
2087 dev_priv->flags = flags;
2088
Dave Airlie54a56ac2006-09-22 04:25:09 +10002089 switch (flags & RADEON_FAMILY_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 case CHIP_R100:
2091 case CHIP_RV200:
2092 case CHIP_R200:
2093 case CHIP_R300:
Dave Airlieb15ec362006-08-19 17:43:52 +10002094 case CHIP_R350:
Dave Airlie414ed532005-08-16 20:43:16 +10002095 case CHIP_R420:
Alex Deucheredc6f382008-10-17 09:21:45 +10002096 case CHIP_R423:
Dave Airlieb15ec362006-08-19 17:43:52 +10002097 case CHIP_RV410:
Dave Airlie3d5e2c12008-02-07 15:01:05 +10002098 case CHIP_RV515:
2099 case CHIP_R520:
2100 case CHIP_RV570:
2101 case CHIP_R580:
Dave Airlie54a56ac2006-09-22 04:25:09 +10002102 dev_priv->flags |= RADEON_HAS_HIERZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 break;
2104 default:
Dave Airlieb5e89ed2005-09-25 14:28:13 +10002105 /* all other chips have no hierarchical z buffer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 break;
2107 }
Dave Airlie414ed532005-08-16 20:43:16 +10002108
2109 if (drm_device_is_agp(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10002110 dev_priv->flags |= RADEON_IS_AGP;
Dave Airlieb15ec362006-08-19 17:43:52 +10002111 else if (drm_device_is_pcie(dev))
Dave Airlie54a56ac2006-09-22 04:25:09 +10002112 dev_priv->flags |= RADEON_IS_PCIE;
Dave Airlieb15ec362006-08-19 17:43:52 +10002113 else
Dave Airlie54a56ac2006-09-22 04:25:09 +10002114 dev_priv->flags |= RADEON_IS_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +10002115
Dave Airlie78538bf2008-11-11 17:56:16 +10002116 ret = drm_addmap(dev, drm_get_resource_start(dev, 2),
2117 drm_get_resource_len(dev, 2), _DRM_REGISTERS,
2118 _DRM_READ_ONLY | _DRM_DRIVER, &dev_priv->mmio);
2119 if (ret != 0)
2120 return ret;
2121
Keith Packard52440212008-11-18 09:30:25 -08002122 ret = drm_vblank_init(dev, 2);
2123 if (ret) {
2124 radeon_driver_unload(dev);
2125 return ret;
2126 }
2127
Dave Airlie414ed532005-08-16 20:43:16 +10002128 DRM_DEBUG("%s card detected\n",
Dave Airlie54a56ac2006-09-22 04:25:09 +10002129 ((dev_priv->flags & RADEON_IS_AGP) ? "AGP" : (((dev_priv->flags & RADEON_IS_PCIE) ? "PCIE" : "PCI"))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 return ret;
2131}
2132
Dave Airlie7c1c2872008-11-28 14:22:24 +10002133int radeon_master_create(struct drm_device *dev, struct drm_master *master)
2134{
2135 struct drm_radeon_master_private *master_priv;
2136 unsigned long sareapage;
2137 int ret;
2138
Eric Anholt9a298b22009-03-24 12:23:04 -07002139 master_priv = kzalloc(sizeof(*master_priv), GFP_KERNEL);
Dave Airlie7c1c2872008-11-28 14:22:24 +10002140 if (!master_priv)
2141 return -ENOMEM;
2142
2143 /* prebuild the SAREA */
Dave Airliebdf539a2008-12-18 16:56:11 +10002144 sareapage = max_t(unsigned long, SAREA_MAX, PAGE_SIZE);
Dave Airliedf4f7fe2009-06-11 16:16:10 +10002145 ret = drm_addmap(dev, 0, sareapage, _DRM_SHM, _DRM_CONTAINS_LOCK,
Dave Airlie7c1c2872008-11-28 14:22:24 +10002146 &master_priv->sarea);
2147 if (ret) {
2148 DRM_ERROR("SAREA setup failed\n");
Jiri Slaby5eb22612010-01-06 17:39:31 +01002149 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10002150 return ret;
2151 }
2152 master_priv->sarea_priv = master_priv->sarea->handle + sizeof(struct drm_sarea);
2153 master_priv->sarea_priv->pfCurrentPage = 0;
2154
2155 master->driver_priv = master_priv;
2156 return 0;
2157}
2158
2159void radeon_master_destroy(struct drm_device *dev, struct drm_master *master)
2160{
2161 struct drm_radeon_master_private *master_priv = master->driver_priv;
2162
2163 if (!master_priv)
2164 return;
2165
2166 if (master_priv->sarea_priv &&
2167 master_priv->sarea_priv->pfCurrentPage != 0)
2168 radeon_cp_dispatch_flip(dev, master);
2169
2170 master_priv->sarea_priv = NULL;
2171 if (master_priv->sarea)
Dave Airlie4e74f362008-12-19 10:23:14 +11002172 drm_rmmap_locked(dev, master_priv->sarea);
Dave Airlie7c1c2872008-11-28 14:22:24 +10002173
Eric Anholt9a298b22009-03-24 12:23:04 -07002174 kfree(master_priv);
Dave Airlie7c1c2872008-11-28 14:22:24 +10002175
2176 master->driver_priv = NULL;
2177}
2178
Dave Airlie22eae942005-11-10 22:16:34 +11002179/* Create mappings for registers and framebuffer so userland doesn't necessarily
2180 * have to find them.
2181 */
2182int radeon_driver_firstopen(struct drm_device *dev)
Dave Airlie836cf042005-07-10 19:27:04 +10002183{
2184 int ret;
2185 drm_local_map_t *map;
2186 drm_radeon_private_t *dev_priv = dev->dev_private;
2187
Dave Airlief2b04cd2007-05-08 15:19:23 +10002188 dev_priv->gart_info.table_size = RADEON_PCIGART_TABLE_SIZE;
2189
Dave Airlie7fc86862007-11-05 10:45:27 +10002190 dev_priv->fb_aper_offset = drm_get_resource_start(dev, 0);
2191 ret = drm_addmap(dev, dev_priv->fb_aper_offset,
Dave Airlie836cf042005-07-10 19:27:04 +10002192 drm_get_resource_len(dev, 0), _DRM_FRAME_BUFFER,
2193 _DRM_WRITE_COMBINING, &map);
2194 if (ret != 0)
2195 return ret;
2196
2197 return 0;
2198}
2199
Dave Airlie22eae942005-11-10 22:16:34 +11002200int radeon_driver_unload(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 drm_radeon_private_t *dev_priv = dev->dev_private;
2203
2204 DRM_DEBUG("\n");
Dave Airlie78538bf2008-11-11 17:56:16 +10002205
2206 drm_rmmap(dev, dev_priv->mmio);
2207
Eric Anholt9a298b22009-03-24 12:23:04 -07002208 kfree(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 dev->dev_private = NULL;
2211 return 0;
2212}
Dave Airlie4247ca92009-02-20 13:28:34 +10002213
2214void radeon_commit_ring(drm_radeon_private_t *dev_priv)
2215{
2216 int i;
2217 u32 *ring;
2218 int tail_aligned;
2219
2220 /* check if the ring is padded out to 16-dword alignment */
2221
Dave Airlie98638712009-06-04 07:08:13 +10002222 tail_aligned = dev_priv->ring.tail & (RADEON_RING_ALIGN-1);
Dave Airlie4247ca92009-02-20 13:28:34 +10002223 if (tail_aligned) {
Dave Airlie98638712009-06-04 07:08:13 +10002224 int num_p2 = RADEON_RING_ALIGN - tail_aligned;
Dave Airlie4247ca92009-02-20 13:28:34 +10002225
2226 ring = dev_priv->ring.start;
2227 /* pad with some CP_PACKET2 */
2228 for (i = 0; i < num_p2; i++)
2229 ring[dev_priv->ring.tail + i] = CP_PACKET2();
2230
2231 dev_priv->ring.tail += i;
2232
2233 dev_priv->ring.space -= num_p2 * sizeof(u32);
2234 }
2235
2236 dev_priv->ring.tail &= dev_priv->ring.tail_mask;
2237
2238 DRM_MEMORYBARRIER();
2239 GET_RING_HEAD( dev_priv );
2240
Alex Deucherc05ce082009-02-24 16:22:29 -05002241 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600) {
2242 RADEON_WRITE(R600_CP_RB_WPTR, dev_priv->ring.tail);
2243 /* read from PCI bus to ensure correct posting */
2244 RADEON_READ(R600_CP_RB_RPTR);
2245 } else {
2246 RADEON_WRITE(RADEON_CP_RB_WPTR, dev_priv->ring.tail);
2247 /* read from PCI bus to ensure correct posting */
2248 RADEON_READ(RADEON_CP_RB_RPTR);
2249 }
Dave Airlie4247ca92009-02-20 13:28:34 +10002250}