blob: 4c39a407aa4af51b6f3554c3a40fc9bd9acffb7f [file] [log] [blame]
Dave Airliebc5f4522007-11-05 12:50:58 +10001/* r128_cce.c -- ATI Rage 128 driver -*- linux-c -*-
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Created: Wed Apr 5 19:24:19 2000 by kevin@precisioninsight.com
Dave Airlief26c4732006-01-02 17:18:39 +11003 */
4/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Copyright 2000 Precision Insight, Inc., Cedar Park, Texas.
6 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
7 * All Rights Reserved.
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a
10 * copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the next
17 * paragraph) shall be included in all copies or substantial portions of the
18 * Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
24 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 *
28 * Authors:
29 * Gareth Hughes <gareth@valinux.com>
30 */
31
Ben Hutchings52f97df2009-08-23 18:37:26 +010032#include <linux/firmware.h>
33#include <linux/platform_device.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "drmP.h"
36#include "drm.h"
37#include "r128_drm.h"
38#include "r128_drv.h"
39
40#define R128_FIFO_DEBUG 0
41
Ben Hutchings52f97df2009-08-23 18:37:26 +010042#define FIRMWARE_NAME "r128/r128_cce.bin"
43
44MODULE_FIRMWARE(FIRMWARE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Dave Airlieeddca552007-07-11 16:09:54 +100046static int R128_READ_PLL(struct drm_device * dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 drm_r128_private_t *dev_priv = dev->dev_private;
49
50 R128_WRITE8(R128_CLOCK_CNTL_INDEX, addr & 0x1f);
51 return R128_READ(R128_CLOCK_CNTL_DATA);
52}
53
54#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +100055static void r128_status(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100057 printk("GUI_STAT = 0x%08x\n",
58 (unsigned int)R128_READ(R128_GUI_STAT));
59 printk("PM4_STAT = 0x%08x\n",
60 (unsigned int)R128_READ(R128_PM4_STAT));
61 printk("PM4_BUFFER_DL_WPTR = 0x%08x\n",
62 (unsigned int)R128_READ(R128_PM4_BUFFER_DL_WPTR));
63 printk("PM4_BUFFER_DL_RPTR = 0x%08x\n",
64 (unsigned int)R128_READ(R128_PM4_BUFFER_DL_RPTR));
65 printk("PM4_MICRO_CNTL = 0x%08x\n",
66 (unsigned int)R128_READ(R128_PM4_MICRO_CNTL));
67 printk("PM4_BUFFER_CNTL = 0x%08x\n",
68 (unsigned int)R128_READ(R128_PM4_BUFFER_CNTL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70#endif
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072/* ================================================================
73 * Engine, FIFO control
74 */
75
Dave Airlieb5e89ed2005-09-25 14:28:13 +100076static int r128_do_pixcache_flush(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 u32 tmp;
79 int i;
80
Dave Airlieb5e89ed2005-09-25 14:28:13 +100081 tmp = R128_READ(R128_PC_NGUI_CTLSTAT) | R128_PC_FLUSH_ALL;
82 R128_WRITE(R128_PC_NGUI_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Dave Airlieb5e89ed2005-09-25 14:28:13 +100084 for (i = 0; i < dev_priv->usec_timeout; i++) {
85 if (!(R128_READ(R128_PC_NGUI_CTLSTAT) & R128_PC_BUSY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 return 0;
87 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +100088 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
90
91#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +100092 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#endif
Eric Anholt20caafa2007-08-25 19:22:43 +100094 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Dave Airlieb5e89ed2005-09-25 14:28:13 +100097static int r128_do_wait_for_fifo(drm_r128_private_t * dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 int i;
100
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000101 for (i = 0; i < dev_priv->usec_timeout; i++) {
102 int slots = R128_READ(R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK;
103 if (slots >= entries)
104 return 0;
105 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
107
108#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000109 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000111 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000114static int r128_do_wait_for_idle(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 int i, ret;
117
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000118 ret = r128_do_wait_for_fifo(dev_priv, 64);
119 if (ret)
120 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000122 for (i = 0; i < dev_priv->usec_timeout; i++) {
123 if (!(R128_READ(R128_GUI_STAT) & R128_GUI_ACTIVE)) {
124 r128_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return 0;
126 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000127 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000131 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000133 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/* ================================================================
137 * CCE control, initialization
138 */
139
140/* Load the microcode for the CCE */
Ben Hutchings52f97df2009-08-23 18:37:26 +0100141static int r128_cce_load_microcode(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Ben Hutchings52f97df2009-08-23 18:37:26 +0100143 struct platform_device *pdev;
144 const struct firmware *fw;
145 const __be32 *fw_data;
146 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000148 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Ben Hutchings52f97df2009-08-23 18:37:26 +0100150 pdev = platform_device_register_simple("r128_cce", 0, NULL, 0);
151 if (IS_ERR(pdev)) {
152 printk(KERN_ERR "r128_cce: Failed to register firmware\n");
153 return PTR_ERR(pdev);
154 }
155 rc = request_firmware(&fw, FIRMWARE_NAME, &pdev->dev);
156 platform_device_unregister(pdev);
157 if (rc) {
158 printk(KERN_ERR "r128_cce: Failed to load firmware \"%s\"\n",
159 FIRMWARE_NAME);
160 return rc;
161 }
162
163 if (fw->size != 256 * 8) {
164 printk(KERN_ERR
165 "r128_cce: Bogus length %zu in firmware \"%s\"\n",
166 fw->size, FIRMWARE_NAME);
167 rc = -EINVAL;
168 goto out_release;
169 }
170
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000171 r128_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Ben Hutchings52f97df2009-08-23 18:37:26 +0100173 fw_data = (const __be32 *)fw->data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000174 R128_WRITE(R128_PM4_MICROCODE_ADDR, 0);
175 for (i = 0; i < 256; i++) {
Ben Hutchings52f97df2009-08-23 18:37:26 +0100176 R128_WRITE(R128_PM4_MICROCODE_DATAH,
177 be32_to_cpup(&fw_data[i * 2]));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000178 R128_WRITE(R128_PM4_MICROCODE_DATAL,
Ben Hutchings52f97df2009-08-23 18:37:26 +0100179 be32_to_cpup(&fw_data[i * 2 + 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 }
Ben Hutchings52f97df2009-08-23 18:37:26 +0100181
182out_release:
183 release_firmware(fw);
184 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/* Flush any pending commands to the CCE. This should only be used just
188 * prior to a wait for idle, as it informs the engine that the command
189 * stream is ending.
190 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000191static void r128_do_cce_flush(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
193 u32 tmp;
194
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000195 tmp = R128_READ(R128_PM4_BUFFER_DL_WPTR) | R128_PM4_BUFFER_DL_DONE;
196 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
199/* Wait for the CCE to go idle.
200 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000201int r128_do_cce_idle(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
203 int i;
204
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000205 for (i = 0; i < dev_priv->usec_timeout; i++) {
206 if (GET_RING_HEAD(dev_priv) == dev_priv->ring.tail) {
207 int pm4stat = R128_READ(R128_PM4_STAT);
208 if (((pm4stat & R128_PM4_FIFOCNT_MASK) >=
209 dev_priv->cce_fifo_size) &&
210 !(pm4stat & (R128_PM4_BUSY |
211 R128_PM4_GUI_ACTIVE))) {
212 return r128_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000215 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000219 DRM_ERROR("failed!\n");
220 r128_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000222 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225/* Start the Concurrent Command Engine.
226 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000227static void r128_do_cce_start(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000229 r128_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000231 R128_WRITE(R128_PM4_BUFFER_CNTL,
232 dev_priv->cce_mode | dev_priv->ring.size_l2qw
233 | R128_PM4_BUFFER_CNTL_NOUPDATE);
234 R128_READ(R128_PM4_BUFFER_ADDR); /* as per the sample code */
235 R128_WRITE(R128_PM4_MICRO_CNTL, R128_PM4_MICRO_FREERUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 dev_priv->cce_running = 1;
238}
239
240/* Reset the Concurrent Command Engine. This will not flush any pending
241 * commands, so you must wait for the CCE command stream to complete
242 * before calling this routine.
243 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000244static void r128_do_cce_reset(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000246 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0);
247 R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dev_priv->ring.tail = 0;
249}
250
251/* Stop the Concurrent Command Engine. This will not flush any pending
252 * commands, so you must flush the command stream and wait for the CCE
253 * to go idle before calling this routine.
254 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000255static void r128_do_cce_stop(drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000257 R128_WRITE(R128_PM4_MICRO_CNTL, 0);
258 R128_WRITE(R128_PM4_BUFFER_CNTL,
259 R128_PM4_NONPM4 | R128_PM4_BUFFER_CNTL_NOUPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 dev_priv->cce_running = 0;
262}
263
264/* Reset the engine. This will stop the CCE if it is running.
265 */
Dave Airlieeddca552007-07-11 16:09:54 +1000266static int r128_do_engine_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
268 drm_r128_private_t *dev_priv = dev->dev_private;
269 u32 clock_cntl_index, mclk_cntl, gen_reset_cntl;
270
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000271 r128_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000273 clock_cntl_index = R128_READ(R128_CLOCK_CNTL_INDEX);
274 mclk_cntl = R128_READ_PLL(dev, R128_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000276 R128_WRITE_PLL(R128_MCLK_CNTL,
277 mclk_cntl | R128_FORCE_GCP | R128_FORCE_PIPE3D_CP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000279 gen_reset_cntl = R128_READ(R128_GEN_RESET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 /* Taken from the sample code - do not change */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000282 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl | R128_SOFT_RESET_GUI);
283 R128_READ(R128_GEN_RESET_CNTL);
284 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl & ~R128_SOFT_RESET_GUI);
285 R128_READ(R128_GEN_RESET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000287 R128_WRITE_PLL(R128_MCLK_CNTL, mclk_cntl);
288 R128_WRITE(R128_CLOCK_CNTL_INDEX, clock_cntl_index);
289 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 /* Reset the CCE ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000292 r128_do_cce_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294 /* The CCE is no longer running after an engine reset */
295 dev_priv->cce_running = 0;
296
297 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000298 r128_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299
300 return 0;
301}
302
Dave Airlieeddca552007-07-11 16:09:54 +1000303static void r128_cce_init_ring_buffer(struct drm_device * dev,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000304 drm_r128_private_t * dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
306 u32 ring_start;
307 u32 tmp;
308
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000309 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311 /* The manual (p. 2) says this address is in "VM space". This
312 * means it's an offset from the start of AGP space.
313 */
314#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000315 if (!dev_priv->is_pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 ring_start = dev_priv->cce_ring->offset - dev->agp->base;
317 else
318#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000319 ring_start = dev_priv->cce_ring->offset -
320 (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000322 R128_WRITE(R128_PM4_BUFFER_OFFSET, ring_start | R128_AGP_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000324 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0);
325 R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 /* Set watermark control */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000328 R128_WRITE(R128_PM4_BUFFER_WM_CNTL,
329 ((R128_WATERMARK_L / 4) << R128_WMA_SHIFT)
330 | ((R128_WATERMARK_M / 4) << R128_WMB_SHIFT)
331 | ((R128_WATERMARK_N / 4) << R128_WMC_SHIFT)
332 | ((R128_WATERMARK_K / 64) << R128_WB_WM_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Force read. Why? Because it's in the examples... */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000335 R128_READ(R128_PM4_BUFFER_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /* Turn on bus mastering */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000338 tmp = R128_READ(R128_BUS_CNTL) & ~R128_BUS_MASTER_DIS;
339 R128_WRITE(R128_BUS_CNTL, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Dave Airlieeddca552007-07-11 16:09:54 +1000342static int r128_do_init_cce(struct drm_device * dev, drm_r128_init_t * init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
344 drm_r128_private_t *dev_priv;
Ben Hutchings52f97df2009-08-23 18:37:26 +0100345 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000347 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100349 if (dev->dev_private) {
350 DRM_DEBUG("called when already initialized\n");
351 return -EINVAL;
352 }
353
Eric Anholt9a298b22009-03-24 12:23:04 -0700354 dev_priv = kzalloc(sizeof(drm_r128_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000355 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000356 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 dev_priv->is_pci = init->is_pci;
359
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000360 if (dev_priv->is_pci && !dev->sg) {
361 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000363 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000364 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
367 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000368 if (dev_priv->usec_timeout < 1 ||
369 dev_priv->usec_timeout > R128_MAX_USEC_TIMEOUT) {
370 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000372 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000373 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
375
376 dev_priv->cce_mode = init->cce_mode;
377
378 /* GH: Simple idle check.
379 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000380 atomic_set(&dev_priv->idle_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 /* We don't support anything other than bus-mastering ring mode,
383 * but the ring can be in either AGP or PCI space for the ring
384 * read pointer.
385 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000386 if ((init->cce_mode != R128_PM4_192BM) &&
387 (init->cce_mode != R128_PM4_128BM_64INDBM) &&
388 (init->cce_mode != R128_PM4_64BM_128INDBM) &&
389 (init->cce_mode != R128_PM4_64BM_64VCBM_64INDBM)) {
390 DRM_DEBUG("Bad cce_mode!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000393 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000396 switch (init->cce_mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 case R128_PM4_NONPM4:
398 dev_priv->cce_fifo_size = 0;
399 break;
400 case R128_PM4_192PIO:
401 case R128_PM4_192BM:
402 dev_priv->cce_fifo_size = 192;
403 break;
404 case R128_PM4_128PIO_64INDBM:
405 case R128_PM4_128BM_64INDBM:
406 dev_priv->cce_fifo_size = 128;
407 break;
408 case R128_PM4_64PIO_128INDBM:
409 case R128_PM4_64BM_128INDBM:
410 case R128_PM4_64PIO_64VCBM_64INDBM:
411 case R128_PM4_64BM_64VCBM_64INDBM:
412 case R128_PM4_64PIO_64VCPIO_64INDPIO:
413 dev_priv->cce_fifo_size = 64;
414 break;
415 }
416
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000417 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 case 16:
419 dev_priv->color_fmt = R128_DATATYPE_RGB565;
420 break;
421 case 32:
422 default:
423 dev_priv->color_fmt = R128_DATATYPE_ARGB8888;
424 break;
425 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000426 dev_priv->front_offset = init->front_offset;
427 dev_priv->front_pitch = init->front_pitch;
428 dev_priv->back_offset = init->back_offset;
429 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000431 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 case 16:
433 dev_priv->depth_fmt = R128_DATATYPE_RGB565;
434 break;
435 case 24:
436 case 32:
437 default:
438 dev_priv->depth_fmt = R128_DATATYPE_ARGB8888;
439 break;
440 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000441 dev_priv->depth_offset = init->depth_offset;
442 dev_priv->depth_pitch = init->depth_pitch;
443 dev_priv->span_offset = init->span_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000445 dev_priv->front_pitch_offset_c = (((dev_priv->front_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 (dev_priv->front_offset >> 5));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000447 dev_priv->back_pitch_offset_c = (((dev_priv->back_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 (dev_priv->back_offset >> 5));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000449 dev_priv->depth_pitch_offset_c = (((dev_priv->depth_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 (dev_priv->depth_offset >> 5) |
451 R128_DST_TILE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000452 dev_priv->span_pitch_offset_c = (((dev_priv->depth_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 (dev_priv->span_offset >> 5));
454
Dave Airlieda509d72007-05-26 05:04:51 +1000455 dev_priv->sarea = drm_getsarea(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 if (!dev_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 DRM_ERROR("could not find sarea!\n");
458 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000459 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000460 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462
463 dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000464 if (!dev_priv->mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 DRM_ERROR("could not find mmio region!\n");
466 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000467 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000468 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 dev_priv->cce_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000471 if (!dev_priv->cce_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 DRM_ERROR("could not find cce ring region!\n");
473 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000474 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000475 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
477 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000478 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 DRM_ERROR("could not find ring read pointer!\n");
480 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000481 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000482 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000484 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000486 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 DRM_ERROR("could not find dma buffer region!\n");
488 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000489 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000490 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000493 if (!dev_priv->is_pci) {
494 dev_priv->agp_textures =
495 drm_core_findmap(dev, init->agp_textures_offset);
496 if (!dev_priv->agp_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 DRM_ERROR("could not find agp texture region!\n");
498 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000499 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000500 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502 }
503
504 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000505 (drm_r128_sarea_t *) ((u8 *) dev_priv->sarea->handle +
506 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
508#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000509 if (!dev_priv->is_pci) {
Dave Airlie42beefc2009-05-06 09:04:52 +1000510 drm_core_ioremap_wc(dev_priv->cce_ring, dev);
511 drm_core_ioremap_wc(dev_priv->ring_rptr, dev);
512 drm_core_ioremap_wc(dev->agp_buffer_map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000513 if (!dev_priv->cce_ring->handle ||
514 !dev_priv->ring_rptr->handle ||
515 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 DRM_ERROR("Could not ioremap agp regions!\n");
517 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000518 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000519 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
521 } else
522#endif
523 {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100524 dev_priv->cce_ring->handle =
525 (void *)(unsigned long)dev_priv->cce_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 dev_priv->ring_rptr->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100527 (void *)(unsigned long)dev_priv->ring_rptr->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000528 dev->agp_buffer_map->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100529 (void *)(unsigned long)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 }
531
532#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000533 if (!dev_priv->is_pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 dev_priv->cce_buffers_offset = dev->agp->base;
535 else
536#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000537 dev_priv->cce_buffers_offset = (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000539 dev_priv->ring.start = (u32 *) dev_priv->cce_ring->handle;
540 dev_priv->ring.end = ((u32 *) dev_priv->cce_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 + init->ring_size / sizeof(u32));
542 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000543 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000545 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
547 dev_priv->ring.high_mark = 128;
548
549 dev_priv->sarea_priv->last_frame = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000550 R128_WRITE(R128_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552 dev_priv->sarea_priv->last_dispatch = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000553 R128_WRITE(R128_LAST_DISPATCH_REG, dev_priv->sarea_priv->last_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000556 if (dev_priv->is_pci) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557#endif
Dave Airlieb05c2382008-03-17 10:24:24 +1000558 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +1000559 dev_priv->gart_info.gart_table_location = DRM_ATI_GART_MAIN;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000560 dev_priv->gart_info.table_size = R128_PCIGART_TABLE_SIZE;
Dave Airlief26c4732006-01-02 17:18:39 +1100561 dev_priv->gart_info.addr = NULL;
562 dev_priv->gart_info.bus_addr = 0;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000563 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +1000564 if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000565 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000567 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000568 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
Dave Airlieea98a922005-09-11 20:28:11 +1000570 R128_WRITE(R128_PCI_GART_PAGE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571#if __OS_HAS_AGP
572 }
573#endif
574
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000575 r128_cce_init_ring_buffer(dev, dev_priv);
Ben Hutchings52f97df2009-08-23 18:37:26 +0100576 rc = r128_cce_load_microcode(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
578 dev->dev_private = (void *)dev_priv;
579
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000580 r128_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Ben Hutchings52f97df2009-08-23 18:37:26 +0100582 if (rc) {
583 DRM_ERROR("Failed to load firmware!\n");
584 r128_do_cleanup_cce(dev);
585 }
586
587 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
Dave Airlieeddca552007-07-11 16:09:54 +1000590int r128_do_cleanup_cce(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591{
592
593 /* Make sure interrupts are disabled here because the uninstall ioctl
594 * may not have been called from userspace and after dev_private
595 * is freed, it's too late.
596 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000597 if (dev->irq_enabled)
598 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000600 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 drm_r128_private_t *dev_priv = dev->dev_private;
602
603#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000604 if (!dev_priv->is_pci) {
605 if (dev_priv->cce_ring != NULL)
606 drm_core_ioremapfree(dev_priv->cce_ring, dev);
607 if (dev_priv->ring_rptr != NULL)
608 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlief26c4732006-01-02 17:18:39 +1100609 if (dev->agp_buffer_map != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000610 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Dave Airlief26c4732006-01-02 17:18:39 +1100611 dev->agp_buffer_map = NULL;
612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 } else
614#endif
615 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000616 if (dev_priv->gart_info.bus_addr)
617 if (!drm_ati_pcigart_cleanup(dev,
Dave Airlief26c4732006-01-02 17:18:39 +1100618 &dev_priv->gart_info))
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000619 DRM_ERROR
620 ("failed to cleanup PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 }
622
Eric Anholt9a298b22009-03-24 12:23:04 -0700623 kfree(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 dev->dev_private = NULL;
625 }
626
627 return 0;
628}
629
Eric Anholtc153f452007-09-03 12:06:45 +1000630int r128_cce_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Eric Anholtc153f452007-09-03 12:06:45 +1000632 drm_r128_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000634 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Eric Anholt6c340ea2007-08-25 20:23:09 +1000636 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Eric Anholtc153f452007-09-03 12:06:45 +1000638 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 case R128_INIT_CCE:
Eric Anholtc153f452007-09-03 12:06:45 +1000640 return r128_do_init_cce(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 case R128_CLEANUP_CCE:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000642 return r128_do_cleanup_cce(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Eric Anholt20caafa2007-08-25 19:22:43 +1000645 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Eric Anholtc153f452007-09-03 12:06:45 +1000648int r128_cce_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000651 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Eric Anholt6c340ea2007-08-25 20:23:09 +1000653 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100655 DEV_INIT_TEST_WITH_RETURN(dev_priv);
656
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000657 if (dev_priv->cce_running || dev_priv->cce_mode == R128_PM4_NONPM4) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000658 DRM_DEBUG("while CCE running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return 0;
660 }
661
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000662 r128_do_cce_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664 return 0;
665}
666
667/* Stop the CCE. The engine must have been idled before calling this
668 * routine.
669 */
Eric Anholtc153f452007-09-03 12:06:45 +1000670int r128_cce_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000673 drm_r128_cce_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000675 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Eric Anholt6c340ea2007-08-25 20:23:09 +1000677 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100679 DEV_INIT_TEST_WITH_RETURN(dev_priv);
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /* Flush any pending CCE commands. This ensures any outstanding
682 * commands are exectuted by the engine before we turn it off.
683 */
Eric Anholtc153f452007-09-03 12:06:45 +1000684 if (stop->flush) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000685 r128_do_cce_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 }
687
688 /* If we fail to make the engine go idle, we return an error
689 * code so that the DRM ioctl wrapper can try again.
690 */
Eric Anholtc153f452007-09-03 12:06:45 +1000691 if (stop->idle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000692 ret = r128_do_cce_idle(dev_priv);
693 if (ret)
694 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
696
697 /* Finally, we can turn off the CCE. If the engine isn't idle,
698 * we will get some dropped triangles as they won't be fully
699 * rendered before the CCE is shut down.
700 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000701 r128_do_cce_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703 /* Reset the engine */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000704 r128_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 return 0;
707}
708
709/* Just reset the CCE ring. Called as part of an X Server engine reset.
710 */
Eric Anholtc153f452007-09-03 12:06:45 +1000711int r128_cce_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000714 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Eric Anholt6c340ea2007-08-25 20:23:09 +1000716 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100718 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000720 r128_do_cce_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 /* The CCE is no longer running after an engine reset */
723 dev_priv->cce_running = 0;
724
725 return 0;
726}
727
Eric Anholtc153f452007-09-03 12:06:45 +1000728int r128_cce_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000731 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Eric Anholt6c340ea2007-08-25 20:23:09 +1000733 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100735 DEV_INIT_TEST_WITH_RETURN(dev_priv);
736
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000737 if (dev_priv->cce_running) {
738 r128_do_cce_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000741 return r128_do_cce_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Eric Anholtc153f452007-09-03 12:06:45 +1000744int r128_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000746 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Eric Anholt6c340ea2007-08-25 20:23:09 +1000748 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100750 DEV_INIT_TEST_WITH_RETURN(dev->dev_private);
751
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000752 return r128_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
754
Eric Anholtc153f452007-09-03 12:06:45 +1000755int r128_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
Eric Anholt20caafa2007-08-25 19:22:43 +1000757 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/* ================================================================
761 * Freelist management
762 */
763#define R128_BUFFER_USED 0xffffffff
764#define R128_BUFFER_FREE 0
765
766#if 0
Dave Airlieeddca552007-07-11 16:09:54 +1000767static int r128_freelist_init(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768{
Dave Airliecdd55a22007-07-11 16:32:08 +1000769 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlie056219e2007-07-11 16:17:42 +1000771 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 drm_r128_buf_priv_t *buf_priv;
773 drm_r128_freelist_t *entry;
774 int i;
775
Eric Anholt9a298b22009-03-24 12:23:04 -0700776 dev_priv->head = kzalloc(sizeof(drm_r128_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000777 if (dev_priv->head == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000778 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 dev_priv->head->age = R128_BUFFER_USED;
781
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000782 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 buf = dma->buflist[i];
784 buf_priv = buf->dev_private;
785
Eric Anholt9a298b22009-03-24 12:23:04 -0700786 entry = kmalloc(sizeof(drm_r128_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000787 if (!entry)
Eric Anholt20caafa2007-08-25 19:22:43 +1000788 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 entry->age = R128_BUFFER_FREE;
791 entry->buf = buf;
792 entry->prev = dev_priv->head;
793 entry->next = dev_priv->head->next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000794 if (!entry->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 dev_priv->tail = entry;
796
797 buf_priv->discard = 0;
798 buf_priv->dispatched = 0;
799 buf_priv->list_entry = entry;
800
801 dev_priv->head->next = entry;
802
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000803 if (dev_priv->head->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 dev_priv->head->next->prev = entry;
805 }
806
807 return 0;
808
809}
810#endif
811
Dave Airlie056219e2007-07-11 16:17:42 +1000812static struct drm_buf *r128_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Dave Airliecdd55a22007-07-11 16:32:08 +1000814 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 drm_r128_private_t *dev_priv = dev->dev_private;
816 drm_r128_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +1000817 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 int i, t;
819
820 /* FIXME: Optimize -- use freelist code */
821
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000822 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 buf = dma->buflist[i];
824 buf_priv = buf->dev_private;
Harvey Harrison8da56302008-04-28 14:13:20 -0700825 if (!buf->file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 return buf;
827 }
828
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 for (t = 0; t < dev_priv->usec_timeout; t++) {
830 u32 done_age = R128_READ(R128_LAST_DISPATCH_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 buf = dma->buflist[i];
834 buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000835 if (buf->pending && buf_priv->age <= done_age) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 /* The buffer has been processed, so it
837 * can now be used.
838 */
839 buf->pending = 0;
840 return buf;
841 }
842 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 }
845
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return NULL;
848}
849
Dave Airlieeddca552007-07-11 16:09:54 +1000850void r128_freelist_reset(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
Dave Airliecdd55a22007-07-11 16:32:08 +1000852 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 int i;
854
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000855 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000856 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
858 buf_priv->age = 0;
859 }
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/* ================================================================
863 * CCE command submission
864 */
865
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000866int r128_wait_ring(drm_r128_private_t * dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 drm_r128_ring_buffer_t *ring = &dev_priv->ring;
869 int i;
870
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000871 for (i = 0; i < dev_priv->usec_timeout; i++) {
872 r128_update_ring_snapshot(dev_priv);
873 if (ring->space >= n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000875 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
877
878 /* FIXME: This is being ignored... */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000879 DRM_ERROR("failed!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000880 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Eric Anholt6c340ea2007-08-25 20:23:09 +1000883static int r128_cce_get_buffers(struct drm_device * dev,
884 struct drm_file *file_priv,
885 struct drm_dma * d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
887 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000888 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000890 for (i = d->granted_count; i < d->request_count; i++) {
891 buf = r128_freelist_get(dev);
892 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +1000893 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Eric Anholt6c340ea2007-08-25 20:23:09 +1000895 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000897 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
898 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000899 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000900 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
901 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000902 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 d->granted_count++;
905 }
906 return 0;
907}
908
Eric Anholtc153f452007-09-03 12:06:45 +1000909int r128_cce_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Dave Airliecdd55a22007-07-11 16:32:08 +1000911 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +1000913 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Eric Anholt6c340ea2007-08-25 20:23:09 +1000915 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 /* Please don't send us buffers.
918 */
Eric Anholtc153f452007-09-03 12:06:45 +1000919 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000920 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000921 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000922 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 }
924
925 /* We'll send you buffers.
926 */
Eric Anholtc153f452007-09-03 12:06:45 +1000927 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000928 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000929 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000930 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Eric Anholtc153f452007-09-03 12:06:45 +1000933 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Eric Anholtc153f452007-09-03 12:06:45 +1000935 if (d->request_count) {
936 ret = r128_cce_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return ret;
940}