blob: bcac90b543ad02f78c88cbb8e6dc6718e5e7bc21 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040035#include <linux/module.h>
Ben Hutchings52f97df2009-08-23 18:37:26 +010036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "drmP.h"
38#include "drm.h"
39#include "r128_drm.h"
40#include "r128_drv.h"
41
42#define R128_FIFO_DEBUG 0
43
Ben Hutchings52f97df2009-08-23 18:37:26 +010044#define FIRMWARE_NAME "r128/r128_cce.bin"
45
46MODULE_FIRMWARE(FIRMWARE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020048static int R128_READ_PLL(struct drm_device *dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 drm_r128_private_t *dev_priv = dev->dev_private;
51
52 R128_WRITE8(R128_CLOCK_CNTL_INDEX, addr & 0x1f);
53 return R128_READ(R128_CLOCK_CNTL_DATA);
54}
55
56#if R128_FIFO_DEBUG
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020057static void r128_status(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070058{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100059 printk("GUI_STAT = 0x%08x\n",
60 (unsigned int)R128_READ(R128_GUI_STAT));
61 printk("PM4_STAT = 0x%08x\n",
62 (unsigned int)R128_READ(R128_PM4_STAT));
63 printk("PM4_BUFFER_DL_WPTR = 0x%08x\n",
64 (unsigned int)R128_READ(R128_PM4_BUFFER_DL_WPTR));
65 printk("PM4_BUFFER_DL_RPTR = 0x%08x\n",
66 (unsigned int)R128_READ(R128_PM4_BUFFER_DL_RPTR));
67 printk("PM4_MICRO_CNTL = 0x%08x\n",
68 (unsigned int)R128_READ(R128_PM4_MICRO_CNTL));
69 printk("PM4_BUFFER_CNTL = 0x%08x\n",
70 (unsigned int)R128_READ(R128_PM4_BUFFER_CNTL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071}
72#endif
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074/* ================================================================
75 * Engine, FIFO control
76 */
77
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020078static int r128_do_pixcache_flush(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 u32 tmp;
81 int i;
82
Dave Airlieb5e89ed2005-09-25 14:28:13 +100083 tmp = R128_READ(R128_PC_NGUI_CTLSTAT) | R128_PC_FLUSH_ALL;
84 R128_WRITE(R128_PC_NGUI_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Dave Airlieb5e89ed2005-09-25 14:28:13 +100086 for (i = 0; i < dev_priv->usec_timeout; i++) {
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020087 if (!(R128_READ(R128_PC_NGUI_CTLSTAT) & R128_PC_BUSY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +100089 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 }
91
92#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +100093 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#endif
Eric Anholt20caafa2007-08-25 19:22:43 +100095 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020098static int r128_do_wait_for_fifo(drm_r128_private_t *dev_priv, int entries)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 int i;
101
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000102 for (i = 0; i < dev_priv->usec_timeout; i++) {
103 int slots = R128_READ(R128_GUI_STAT) & R128_GUI_FIFOCNT_MASK;
104 if (slots >= entries)
105 return 0;
106 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 }
108
109#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000110 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000112 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200115static int r128_do_wait_for_idle(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 int i, ret;
118
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000119 ret = r128_do_wait_for_fifo(dev_priv, 64);
120 if (ret)
121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000123 for (i = 0; i < dev_priv->usec_timeout; i++) {
124 if (!(R128_READ(R128_GUI_STAT) & R128_GUI_ACTIVE)) {
125 r128_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return 0;
127 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000128 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
130
131#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000132 DRM_ERROR("failed!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000134 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137/* ================================================================
138 * CCE control, initialization
139 */
140
141/* Load the microcode for the CCE */
Ben Hutchings52f97df2009-08-23 18:37:26 +0100142static int r128_cce_load_microcode(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Ben Hutchings52f97df2009-08-23 18:37:26 +0100144 struct platform_device *pdev;
145 const struct firmware *fw;
146 const __be32 *fw_data;
147 int rc, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000149 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Ben Hutchings52f97df2009-08-23 18:37:26 +0100151 pdev = platform_device_register_simple("r128_cce", 0, NULL, 0);
152 if (IS_ERR(pdev)) {
153 printk(KERN_ERR "r128_cce: Failed to register firmware\n");
154 return PTR_ERR(pdev);
155 }
156 rc = request_firmware(&fw, FIRMWARE_NAME, &pdev->dev);
157 platform_device_unregister(pdev);
158 if (rc) {
159 printk(KERN_ERR "r128_cce: Failed to load firmware \"%s\"\n",
160 FIRMWARE_NAME);
161 return rc;
162 }
163
164 if (fw->size != 256 * 8) {
165 printk(KERN_ERR
166 "r128_cce: Bogus length %zu in firmware \"%s\"\n",
167 fw->size, FIRMWARE_NAME);
168 rc = -EINVAL;
169 goto out_release;
170 }
171
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000172 r128_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Ben Hutchings52f97df2009-08-23 18:37:26 +0100174 fw_data = (const __be32 *)fw->data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000175 R128_WRITE(R128_PM4_MICROCODE_ADDR, 0);
176 for (i = 0; i < 256; i++) {
Ben Hutchings52f97df2009-08-23 18:37:26 +0100177 R128_WRITE(R128_PM4_MICROCODE_DATAH,
178 be32_to_cpup(&fw_data[i * 2]));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000179 R128_WRITE(R128_PM4_MICROCODE_DATAL,
Ben Hutchings52f97df2009-08-23 18:37:26 +0100180 be32_to_cpup(&fw_data[i * 2 + 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
Ben Hutchings52f97df2009-08-23 18:37:26 +0100182
183out_release:
184 release_firmware(fw);
185 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/* Flush any pending commands to the CCE. This should only be used just
189 * prior to a wait for idle, as it informs the engine that the command
190 * stream is ending.
191 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200192static void r128_do_cce_flush(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 u32 tmp;
195
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000196 tmp = R128_READ(R128_PM4_BUFFER_DL_WPTR) | R128_PM4_BUFFER_DL_DONE;
197 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
200/* Wait for the CCE to go idle.
201 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200202int r128_do_cce_idle(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int i;
205
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000206 for (i = 0; i < dev_priv->usec_timeout; i++) {
207 if (GET_RING_HEAD(dev_priv) == dev_priv->ring.tail) {
208 int pm4stat = R128_READ(R128_PM4_STAT);
209 if (((pm4stat & R128_PM4_FIFOCNT_MASK) >=
210 dev_priv->cce_fifo_size) &&
211 !(pm4stat & (R128_PM4_BUSY |
212 R128_PM4_GUI_ACTIVE))) {
213 return r128_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000216 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
219#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000220 DRM_ERROR("failed!\n");
221 r128_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000223 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/* Start the Concurrent Command Engine.
227 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200228static void r128_do_cce_start(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000230 r128_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000232 R128_WRITE(R128_PM4_BUFFER_CNTL,
233 dev_priv->cce_mode | dev_priv->ring.size_l2qw
234 | R128_PM4_BUFFER_CNTL_NOUPDATE);
235 R128_READ(R128_PM4_BUFFER_ADDR); /* as per the sample code */
236 R128_WRITE(R128_PM4_MICRO_CNTL, R128_PM4_MICRO_FREERUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 dev_priv->cce_running = 1;
239}
240
241/* Reset the Concurrent Command Engine. This will not flush any pending
242 * commands, so you must wait for the CCE command stream to complete
243 * before calling this routine.
244 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200245static void r128_do_cce_reset(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000247 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0);
248 R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 dev_priv->ring.tail = 0;
250}
251
252/* Stop the Concurrent Command Engine. This will not flush any pending
253 * commands, so you must flush the command stream and wait for the CCE
254 * to go idle before calling this routine.
255 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200256static void r128_do_cce_stop(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000258 R128_WRITE(R128_PM4_MICRO_CNTL, 0);
259 R128_WRITE(R128_PM4_BUFFER_CNTL,
260 R128_PM4_NONPM4 | R128_PM4_BUFFER_CNTL_NOUPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
262 dev_priv->cce_running = 0;
263}
264
265/* Reset the engine. This will stop the CCE if it is running.
266 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200267static int r128_do_engine_reset(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
269 drm_r128_private_t *dev_priv = dev->dev_private;
270 u32 clock_cntl_index, mclk_cntl, gen_reset_cntl;
271
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 r128_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000274 clock_cntl_index = R128_READ(R128_CLOCK_CNTL_INDEX);
275 mclk_cntl = R128_READ_PLL(dev, R128_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000277 R128_WRITE_PLL(R128_MCLK_CNTL,
278 mclk_cntl | R128_FORCE_GCP | R128_FORCE_PIPE3D_CP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000280 gen_reset_cntl = R128_READ(R128_GEN_RESET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* Taken from the sample code - do not change */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000283 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl | R128_SOFT_RESET_GUI);
284 R128_READ(R128_GEN_RESET_CNTL);
285 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl & ~R128_SOFT_RESET_GUI);
286 R128_READ(R128_GEN_RESET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000288 R128_WRITE_PLL(R128_MCLK_CNTL, mclk_cntl);
289 R128_WRITE(R128_CLOCK_CNTL_INDEX, clock_cntl_index);
290 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* Reset the CCE ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000293 r128_do_cce_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295 /* The CCE is no longer running after an engine reset */
296 dev_priv->cce_running = 0;
297
298 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000299 r128_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 return 0;
302}
303
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200304static void r128_cce_init_ring_buffer(struct drm_device *dev,
305 drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307 u32 ring_start;
308 u32 tmp;
309
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000310 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 /* The manual (p. 2) says this address is in "VM space". This
313 * means it's an offset from the start of AGP space.
314 */
315#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000316 if (!dev_priv->is_pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ring_start = dev_priv->cce_ring->offset - dev->agp->base;
318 else
319#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000320 ring_start = dev_priv->cce_ring->offset -
321 (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 R128_WRITE(R128_PM4_BUFFER_OFFSET, ring_start | R128_AGP_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000325 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0);
326 R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* Set watermark control */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000329 R128_WRITE(R128_PM4_BUFFER_WM_CNTL,
330 ((R128_WATERMARK_L / 4) << R128_WMA_SHIFT)
331 | ((R128_WATERMARK_M / 4) << R128_WMB_SHIFT)
332 | ((R128_WATERMARK_N / 4) << R128_WMC_SHIFT)
333 | ((R128_WATERMARK_K / 64) << R128_WB_WM_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /* Force read. Why? Because it's in the examples... */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000336 R128_READ(R128_PM4_BUFFER_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /* Turn on bus mastering */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000339 tmp = R128_READ(R128_BUS_CNTL) & ~R128_BUS_MASTER_DIS;
340 R128_WRITE(R128_BUS_CNTL, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200343static int r128_do_init_cce(struct drm_device *dev, drm_r128_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 drm_r128_private_t *dev_priv;
Ben Hutchings52f97df2009-08-23 18:37:26 +0100346 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000348 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100350 if (dev->dev_private) {
351 DRM_DEBUG("called when already initialized\n");
352 return -EINVAL;
353 }
354
Eric Anholt9a298b22009-03-24 12:23:04 -0700355 dev_priv = kzalloc(sizeof(drm_r128_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000356 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000357 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 dev_priv->is_pci = init->is_pci;
360
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000361 if (dev_priv->is_pci && !dev->sg) {
362 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000364 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
368 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000369 if (dev_priv->usec_timeout < 1 ||
370 dev_priv->usec_timeout > R128_MAX_USEC_TIMEOUT) {
371 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000373 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000374 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
376
377 dev_priv->cce_mode = init->cce_mode;
378
379 /* GH: Simple idle check.
380 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000381 atomic_set(&dev_priv->idle_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 /* We don't support anything other than bus-mastering ring mode,
384 * but the ring can be in either AGP or PCI space for the ring
385 * read pointer.
386 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000387 if ((init->cce_mode != R128_PM4_192BM) &&
388 (init->cce_mode != R128_PM4_128BM_64INDBM) &&
389 (init->cce_mode != R128_PM4_64BM_128INDBM) &&
390 (init->cce_mode != R128_PM4_64BM_64VCBM_64INDBM)) {
391 DRM_DEBUG("Bad cce_mode!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000393 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000394 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000397 switch (init->cce_mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 case R128_PM4_NONPM4:
399 dev_priv->cce_fifo_size = 0;
400 break;
401 case R128_PM4_192PIO:
402 case R128_PM4_192BM:
403 dev_priv->cce_fifo_size = 192;
404 break;
405 case R128_PM4_128PIO_64INDBM:
406 case R128_PM4_128BM_64INDBM:
407 dev_priv->cce_fifo_size = 128;
408 break;
409 case R128_PM4_64PIO_128INDBM:
410 case R128_PM4_64BM_128INDBM:
411 case R128_PM4_64PIO_64VCBM_64INDBM:
412 case R128_PM4_64BM_64VCBM_64INDBM:
413 case R128_PM4_64PIO_64VCPIO_64INDPIO:
414 dev_priv->cce_fifo_size = 64;
415 break;
416 }
417
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 case 16:
420 dev_priv->color_fmt = R128_DATATYPE_RGB565;
421 break;
422 case 32:
423 default:
424 dev_priv->color_fmt = R128_DATATYPE_ARGB8888;
425 break;
426 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000427 dev_priv->front_offset = init->front_offset;
428 dev_priv->front_pitch = init->front_pitch;
429 dev_priv->back_offset = init->back_offset;
430 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000432 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 case 16:
434 dev_priv->depth_fmt = R128_DATATYPE_RGB565;
435 break;
436 case 24:
437 case 32:
438 default:
439 dev_priv->depth_fmt = R128_DATATYPE_ARGB8888;
440 break;
441 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000442 dev_priv->depth_offset = init->depth_offset;
443 dev_priv->depth_pitch = init->depth_pitch;
444 dev_priv->span_offset = init->span_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 dev_priv->front_pitch_offset_c = (((dev_priv->front_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 (dev_priv->front_offset >> 5));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 dev_priv->back_pitch_offset_c = (((dev_priv->back_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 (dev_priv->back_offset >> 5));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000450 dev_priv->depth_pitch_offset_c = (((dev_priv->depth_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 (dev_priv->depth_offset >> 5) |
452 R128_DST_TILE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000453 dev_priv->span_pitch_offset_c = (((dev_priv->depth_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 (dev_priv->span_offset >> 5));
455
Dave Airlieda509d72007-05-26 05:04:51 +1000456 dev_priv->sarea = drm_getsarea(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000457 if (!dev_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 DRM_ERROR("could not find sarea!\n");
459 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000460 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000461 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000465 if (!dev_priv->mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 DRM_ERROR("could not find mmio region!\n");
467 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000468 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000469 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 dev_priv->cce_ring = drm_core_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000472 if (!dev_priv->cce_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 DRM_ERROR("could not find cce ring region!\n");
474 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000475 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000476 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 dev_priv->ring_rptr = drm_core_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000479 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 DRM_ERROR("could not find ring read pointer!\n");
481 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000482 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000483 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000485 dev->agp_buffer_token = init->buffers_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dev->agp_buffer_map = drm_core_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000487 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 DRM_ERROR("could not find dma buffer region!\n");
489 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000490 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000491 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000494 if (!dev_priv->is_pci) {
495 dev_priv->agp_textures =
496 drm_core_findmap(dev, init->agp_textures_offset);
497 if (!dev_priv->agp_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 DRM_ERROR("could not find agp texture region!\n");
499 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000500 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000501 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503 }
504
505 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000506 (drm_r128_sarea_t *) ((u8 *) dev_priv->sarea->handle +
507 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
509#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000510 if (!dev_priv->is_pci) {
Dave Airlie42beefc2009-05-06 09:04:52 +1000511 drm_core_ioremap_wc(dev_priv->cce_ring, dev);
512 drm_core_ioremap_wc(dev_priv->ring_rptr, dev);
513 drm_core_ioremap_wc(dev->agp_buffer_map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000514 if (!dev_priv->cce_ring->handle ||
515 !dev_priv->ring_rptr->handle ||
516 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 DRM_ERROR("Could not ioremap agp regions!\n");
518 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000519 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000520 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522 } else
523#endif
524 {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100525 dev_priv->cce_ring->handle =
526 (void *)(unsigned long)dev_priv->cce_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 dev_priv->ring_rptr->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100528 (void *)(unsigned long)dev_priv->ring_rptr->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000529 dev->agp_buffer_map->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100530 (void *)(unsigned long)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532
533#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000534 if (!dev_priv->is_pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dev_priv->cce_buffers_offset = dev->agp->base;
536 else
537#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000538 dev_priv->cce_buffers_offset = (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000540 dev_priv->ring.start = (u32 *) dev_priv->cce_ring->handle;
541 dev_priv->ring.end = ((u32 *) dev_priv->cce_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 + init->ring_size / sizeof(u32));
543 dev_priv->ring.size = init->ring_size;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000544 dev_priv->ring.size_l2qw = drm_order(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000546 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 dev_priv->ring.high_mark = 128;
549
550 dev_priv->sarea_priv->last_frame = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000551 R128_WRITE(R128_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 dev_priv->sarea_priv->last_dispatch = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000554 R128_WRITE(R128_LAST_DISPATCH_REG, dev_priv->sarea_priv->last_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
556#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000557 if (dev_priv->is_pci) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558#endif
Dave Airlieb05c2382008-03-17 10:24:24 +1000559 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +1000560 dev_priv->gart_info.gart_table_location = DRM_ATI_GART_MAIN;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000561 dev_priv->gart_info.table_size = R128_PCIGART_TABLE_SIZE;
Dave Airlief26c4732006-01-02 17:18:39 +1100562 dev_priv->gart_info.addr = NULL;
563 dev_priv->gart_info.bus_addr = 0;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000564 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +1000565 if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000566 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000568 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000569 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
Dave Airlieea98a922005-09-11 20:28:11 +1000571 R128_WRITE(R128_PCI_GART_PAGE, dev_priv->gart_info.bus_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572#if __OS_HAS_AGP
573 }
574#endif
575
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000576 r128_cce_init_ring_buffer(dev, dev_priv);
Ben Hutchings52f97df2009-08-23 18:37:26 +0100577 rc = r128_cce_load_microcode(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 dev->dev_private = (void *)dev_priv;
580
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000581 r128_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Ben Hutchings52f97df2009-08-23 18:37:26 +0100583 if (rc) {
584 DRM_ERROR("Failed to load firmware!\n");
585 r128_do_cleanup_cce(dev);
586 }
587
588 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200591int r128_do_cleanup_cce(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
593
594 /* Make sure interrupts are disabled here because the uninstall ioctl
595 * may not have been called from userspace and after dev_private
596 * is freed, it's too late.
597 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000598 if (dev->irq_enabled)
599 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000601 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 drm_r128_private_t *dev_priv = dev->dev_private;
603
604#if __OS_HAS_AGP
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000605 if (!dev_priv->is_pci) {
606 if (dev_priv->cce_ring != NULL)
607 drm_core_ioremapfree(dev_priv->cce_ring, dev);
608 if (dev_priv->ring_rptr != NULL)
609 drm_core_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlief26c4732006-01-02 17:18:39 +1100610 if (dev->agp_buffer_map != NULL) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000611 drm_core_ioremapfree(dev->agp_buffer_map, dev);
Dave Airlief26c4732006-01-02 17:18:39 +1100612 dev->agp_buffer_map = NULL;
613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 } else
615#endif
616 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000617 if (dev_priv->gart_info.bus_addr)
618 if (!drm_ati_pcigart_cleanup(dev,
Dave Airlief26c4732006-01-02 17:18:39 +1100619 &dev_priv->gart_info))
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000620 DRM_ERROR
621 ("failed to cleanup PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623
Eric Anholt9a298b22009-03-24 12:23:04 -0700624 kfree(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 dev->dev_private = NULL;
626 }
627
628 return 0;
629}
630
Eric Anholtc153f452007-09-03 12:06:45 +1000631int r128_cce_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Eric Anholtc153f452007-09-03 12:06:45 +1000633 drm_r128_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000635 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Anholt6c340ea2007-08-25 20:23:09 +1000637 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Eric Anholtc153f452007-09-03 12:06:45 +1000639 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 case R128_INIT_CCE:
Eric Anholtc153f452007-09-03 12:06:45 +1000641 return r128_do_init_cce(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 case R128_CLEANUP_CCE:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000643 return r128_do_cleanup_cce(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Eric Anholt20caafa2007-08-25 19:22:43 +1000646 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
Eric Anholtc153f452007-09-03 12:06:45 +1000649int r128_cce_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000652 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Eric Anholt6c340ea2007-08-25 20:23:09 +1000654 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100656 DEV_INIT_TEST_WITH_RETURN(dev_priv);
657
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000658 if (dev_priv->cce_running || dev_priv->cce_mode == R128_PM4_NONPM4) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000659 DRM_DEBUG("while CCE running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return 0;
661 }
662
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000663 r128_do_cce_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665 return 0;
666}
667
668/* Stop the CCE. The engine must have been idled before calling this
669 * routine.
670 */
Eric Anholtc153f452007-09-03 12:06:45 +1000671int r128_cce_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000674 drm_r128_cce_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000676 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Eric Anholt6c340ea2007-08-25 20:23:09 +1000678 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100680 DEV_INIT_TEST_WITH_RETURN(dev_priv);
681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 /* Flush any pending CCE commands. This ensures any outstanding
683 * commands are exectuted by the engine before we turn it off.
684 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200685 if (stop->flush)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000686 r128_do_cce_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200737 if (dev_priv->cce_running)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 r128_do_cce_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000740 return r128_do_cce_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Eric Anholtc153f452007-09-03 12:06:45 +1000743int r128_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000745 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Eric Anholt6c340ea2007-08-25 20:23:09 +1000747 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100749 DEV_INIT_TEST_WITH_RETURN(dev->dev_private);
750
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000751 return r128_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Eric Anholtc153f452007-09-03 12:06:45 +1000754int r128_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Eric Anholt20caafa2007-08-25 19:22:43 +1000756 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757}
758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759/* ================================================================
760 * Freelist management
761 */
762#define R128_BUFFER_USED 0xffffffff
763#define R128_BUFFER_FREE 0
764
765#if 0
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200766static int r128_freelist_init(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Dave Airliecdd55a22007-07-11 16:32:08 +1000768 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlie056219e2007-07-11 16:17:42 +1000770 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 drm_r128_buf_priv_t *buf_priv;
772 drm_r128_freelist_t *entry;
773 int i;
774
Eric Anholt9a298b22009-03-24 12:23:04 -0700775 dev_priv->head = kzalloc(sizeof(drm_r128_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000776 if (dev_priv->head == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000777 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 dev_priv->head->age = R128_BUFFER_USED;
780
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000781 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 buf = dma->buflist[i];
783 buf_priv = buf->dev_private;
784
Eric Anholt9a298b22009-03-24 12:23:04 -0700785 entry = kmalloc(sizeof(drm_r128_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000786 if (!entry)
Eric Anholt20caafa2007-08-25 19:22:43 +1000787 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 entry->age = R128_BUFFER_FREE;
790 entry->buf = buf;
791 entry->prev = dev_priv->head;
792 entry->next = dev_priv->head->next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000793 if (!entry->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 dev_priv->tail = entry;
795
796 buf_priv->discard = 0;
797 buf_priv->dispatched = 0;
798 buf_priv->list_entry = entry;
799
800 dev_priv->head->next = entry;
801
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000802 if (dev_priv->head->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 dev_priv->head->next->prev = entry;
804 }
805
806 return 0;
807
808}
809#endif
810
Dave Airlie056219e2007-07-11 16:17:42 +1000811static struct drm_buf *r128_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Dave Airliecdd55a22007-07-11 16:32:08 +1000813 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 drm_r128_private_t *dev_priv = dev->dev_private;
815 drm_r128_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +1000816 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 int i, t;
818
819 /* FIXME: Optimize -- use freelist code */
820
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000821 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 buf = dma->buflist[i];
823 buf_priv = buf->dev_private;
Harvey Harrison8da56302008-04-28 14:13:20 -0700824 if (!buf->file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 return buf;
826 }
827
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000828 for (t = 0; t < dev_priv->usec_timeout; t++) {
829 u32 done_age = R128_READ(R128_LAST_DISPATCH_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000831 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 buf = dma->buflist[i];
833 buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000834 if (buf->pending && buf_priv->age <= done_age) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* The buffer has been processed, so it
836 * can now be used.
837 */
838 buf->pending = 0;
839 return buf;
840 }
841 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000842 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000845 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return NULL;
847}
848
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200849void r128_freelist_reset(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Dave Airliecdd55a22007-07-11 16:32:08 +1000851 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 int i;
853
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000855 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
857 buf_priv->age = 0;
858 }
859}
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861/* ================================================================
862 * CCE command submission
863 */
864
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200865int r128_wait_ring(drm_r128_private_t *dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 drm_r128_ring_buffer_t *ring = &dev_priv->ring;
868 int i;
869
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000870 for (i = 0; i < dev_priv->usec_timeout; i++) {
871 r128_update_ring_snapshot(dev_priv);
872 if (ring->space >= n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000874 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
877 /* FIXME: This is being ignored... */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000878 DRM_ERROR("failed!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000879 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880}
881
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200882static int r128_cce_get_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000883 struct drm_file *file_priv,
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200884 struct drm_dma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000887 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000889 for (i = d->granted_count; i < d->request_count; i++) {
890 buf = r128_freelist_get(dev);
891 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +1000892 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Eric Anholt6c340ea2007-08-25 20:23:09 +1000894 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000896 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
897 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000898 return -EFAULT;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000899 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
900 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000901 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
903 d->granted_count++;
904 }
905 return 0;
906}
907
Eric Anholtc153f452007-09-03 12:06:45 +1000908int r128_cce_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
Dave Airliecdd55a22007-07-11 16:32:08 +1000910 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +1000912 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Eric Anholt6c340ea2007-08-25 20:23:09 +1000914 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /* Please don't send us buffers.
917 */
Eric Anholtc153f452007-09-03 12:06:45 +1000918 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000919 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000920 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000921 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 }
923
924 /* We'll send you buffers.
925 */
Eric Anholtc153f452007-09-03 12:06:45 +1000926 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000927 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000928 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000929 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 }
931
Eric Anholtc153f452007-09-03 12:06:45 +1000932 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200934 if (d->request_count)
Eric Anholtc153f452007-09-03 12:06:45 +1000935 ret = r128_cce_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 return ret;
938}