blob: c9890afe69d6b0376c686c2442045b91c657f6a3 [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
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/drmP.h>
38#include <drm/r128_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "r128_drv.h"
40
41#define R128_FIFO_DEBUG 0
42
Ben Hutchings52f97df2009-08-23 18:37:26 +010043#define FIRMWARE_NAME "r128/r128_cce.bin"
44
45MODULE_FIRMWARE(FIRMWARE_NAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020047static int R128_READ_PLL(struct drm_device *dev, int addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 drm_r128_private_t *dev_priv = dev->dev_private;
50
51 R128_WRITE8(R128_CLOCK_CNTL_INDEX, addr & 0x1f);
52 return R128_READ(R128_CLOCK_CNTL_DATA);
53}
54
55#if R128_FIFO_DEBUG
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020056static void r128_status(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Dave Airlieb5e89ed2005-09-25 14:28:13 +100058 printk("GUI_STAT = 0x%08x\n",
59 (unsigned int)R128_READ(R128_GUI_STAT));
60 printk("PM4_STAT = 0x%08x\n",
61 (unsigned int)R128_READ(R128_PM4_STAT));
62 printk("PM4_BUFFER_DL_WPTR = 0x%08x\n",
63 (unsigned int)R128_READ(R128_PM4_BUFFER_DL_WPTR));
64 printk("PM4_BUFFER_DL_RPTR = 0x%08x\n",
65 (unsigned int)R128_READ(R128_PM4_BUFFER_DL_RPTR));
66 printk("PM4_MICRO_CNTL = 0x%08x\n",
67 (unsigned int)R128_READ(R128_PM4_MICRO_CNTL));
68 printk("PM4_BUFFER_CNTL = 0x%08x\n",
69 (unsigned int)R128_READ(R128_PM4_BUFFER_CNTL));
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
71#endif
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073/* ================================================================
74 * Engine, FIFO control
75 */
76
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020077static int r128_do_pixcache_flush(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
79 u32 tmp;
80 int i;
81
Dave Airlieb5e89ed2005-09-25 14:28:13 +100082 tmp = R128_READ(R128_PC_NGUI_CTLSTAT) | R128_PC_FLUSH_ALL;
83 R128_WRITE(R128_PC_NGUI_CTLSTAT, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Dave Airlieb5e89ed2005-09-25 14:28:13 +100085 for (i = 0; i < dev_priv->usec_timeout; i++) {
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020086 if (!(R128_READ(R128_PC_NGUI_CTLSTAT) & R128_PC_BUSY))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 return 0;
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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +020097static 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
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200114static 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)) {
Joe Perches8dfe1622017-02-28 04:55:54 -0800152 pr_err("r128_cce: Failed to register firmware\n");
Ben Hutchings52f97df2009-08-23 18:37:26 +0100153 return PTR_ERR(pdev);
154 }
155 rc = request_firmware(&fw, FIRMWARE_NAME, &pdev->dev);
156 platform_device_unregister(pdev);
157 if (rc) {
Joe Perches8dfe1622017-02-28 04:55:54 -0800158 pr_err("r128_cce: Failed to load firmware \"%s\"\n",
Ben Hutchings52f97df2009-08-23 18:37:26 +0100159 FIRMWARE_NAME);
160 return rc;
161 }
162
163 if (fw->size != 256 * 8) {
Joe Perches8dfe1622017-02-28 04:55:54 -0800164 pr_err("r128_cce: Bogus length %zu in firmware \"%s\"\n",
Ben Hutchings52f97df2009-08-23 18:37:26 +0100165 fw->size, FIRMWARE_NAME);
166 rc = -EINVAL;
167 goto out_release;
168 }
169
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000170 r128_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Ben Hutchings52f97df2009-08-23 18:37:26 +0100172 fw_data = (const __be32 *)fw->data;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000173 R128_WRITE(R128_PM4_MICROCODE_ADDR, 0);
174 for (i = 0; i < 256; i++) {
Ben Hutchings52f97df2009-08-23 18:37:26 +0100175 R128_WRITE(R128_PM4_MICROCODE_DATAH,
176 be32_to_cpup(&fw_data[i * 2]));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000177 R128_WRITE(R128_PM4_MICROCODE_DATAL,
Ben Hutchings52f97df2009-08-23 18:37:26 +0100178 be32_to_cpup(&fw_data[i * 2 + 1]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 }
Ben Hutchings52f97df2009-08-23 18:37:26 +0100180
181out_release:
182 release_firmware(fw);
183 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/* Flush any pending commands to the CCE. This should only be used just
187 * prior to a wait for idle, as it informs the engine that the command
188 * stream is ending.
189 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200190static void r128_do_cce_flush(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
192 u32 tmp;
193
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000194 tmp = R128_READ(R128_PM4_BUFFER_DL_WPTR) | R128_PM4_BUFFER_DL_DONE;
195 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/* Wait for the CCE to go idle.
199 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200200int r128_do_cce_idle(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 int i;
203
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000204 for (i = 0; i < dev_priv->usec_timeout; i++) {
205 if (GET_RING_HEAD(dev_priv) == dev_priv->ring.tail) {
206 int pm4stat = R128_READ(R128_PM4_STAT);
207 if (((pm4stat & R128_PM4_FIFOCNT_MASK) >=
208 dev_priv->cce_fifo_size) &&
209 !(pm4stat & (R128_PM4_BUSY |
210 R128_PM4_GUI_ACTIVE))) {
211 return r128_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000214 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217#if R128_FIFO_DEBUG
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000218 DRM_ERROR("failed!\n");
219 r128_status(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000221 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222}
223
224/* Start the Concurrent Command Engine.
225 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200226static void r128_do_cce_start(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000228 r128_do_wait_for_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000230 R128_WRITE(R128_PM4_BUFFER_CNTL,
231 dev_priv->cce_mode | dev_priv->ring.size_l2qw
232 | R128_PM4_BUFFER_CNTL_NOUPDATE);
233 R128_READ(R128_PM4_BUFFER_ADDR); /* as per the sample code */
234 R128_WRITE(R128_PM4_MICRO_CNTL, R128_PM4_MICRO_FREERUN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 dev_priv->cce_running = 1;
237}
238
239/* Reset the Concurrent Command Engine. This will not flush any pending
240 * commands, so you must wait for the CCE command stream to complete
241 * before calling this routine.
242 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200243static void r128_do_cce_reset(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000245 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0);
246 R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 dev_priv->ring.tail = 0;
248}
249
250/* Stop the Concurrent Command Engine. This will not flush any pending
251 * commands, so you must flush the command stream and wait for the CCE
252 * to go idle before calling this routine.
253 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200254static void r128_do_cce_stop(drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000256 R128_WRITE(R128_PM4_MICRO_CNTL, 0);
257 R128_WRITE(R128_PM4_BUFFER_CNTL,
258 R128_PM4_NONPM4 | R128_PM4_BUFFER_CNTL_NOUPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 dev_priv->cce_running = 0;
261}
262
263/* Reset the engine. This will stop the CCE if it is running.
264 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200265static int r128_do_engine_reset(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 drm_r128_private_t *dev_priv = dev->dev_private;
268 u32 clock_cntl_index, mclk_cntl, gen_reset_cntl;
269
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000270 r128_do_pixcache_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000272 clock_cntl_index = R128_READ(R128_CLOCK_CNTL_INDEX);
273 mclk_cntl = R128_READ_PLL(dev, R128_MCLK_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000275 R128_WRITE_PLL(R128_MCLK_CNTL,
276 mclk_cntl | R128_FORCE_GCP | R128_FORCE_PIPE3D_CP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000278 gen_reset_cntl = R128_READ(R128_GEN_RESET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 /* Taken from the sample code - do not change */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000281 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl | R128_SOFT_RESET_GUI);
282 R128_READ(R128_GEN_RESET_CNTL);
283 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl & ~R128_SOFT_RESET_GUI);
284 R128_READ(R128_GEN_RESET_CNTL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000286 R128_WRITE_PLL(R128_MCLK_CNTL, mclk_cntl);
287 R128_WRITE(R128_CLOCK_CNTL_INDEX, clock_cntl_index);
288 R128_WRITE(R128_GEN_RESET_CNTL, gen_reset_cntl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 /* Reset the CCE ring */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000291 r128_do_cce_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 /* The CCE is no longer running after an engine reset */
294 dev_priv->cce_running = 0;
295
296 /* Reset any pending vertex, indirect buffers */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000297 r128_freelist_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299 return 0;
300}
301
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200302static void r128_cce_init_ring_buffer(struct drm_device *dev,
303 drm_r128_private_t *dev_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 u32 ring_start;
306 u32 tmp;
307
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000308 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
310 /* The manual (p. 2) says this address is in "VM space". This
311 * means it's an offset from the start of AGP space.
312 */
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200313#if IS_ENABLED(CONFIG_AGP)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000314 if (!dev_priv->is_pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 ring_start = dev_priv->cce_ring->offset - dev->agp->base;
316 else
317#endif
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000318 ring_start = dev_priv->cce_ring->offset -
319 (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000321 R128_WRITE(R128_PM4_BUFFER_OFFSET, ring_start | R128_AGP_OFFSET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 R128_WRITE(R128_PM4_BUFFER_DL_WPTR, 0);
324 R128_WRITE(R128_PM4_BUFFER_DL_RPTR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 /* Set watermark control */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000327 R128_WRITE(R128_PM4_BUFFER_WM_CNTL,
328 ((R128_WATERMARK_L / 4) << R128_WMA_SHIFT)
329 | ((R128_WATERMARK_M / 4) << R128_WMB_SHIFT)
330 | ((R128_WATERMARK_N / 4) << R128_WMC_SHIFT)
331 | ((R128_WATERMARK_K / 64) << R128_WB_WM_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 /* Force read. Why? Because it's in the examples... */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334 R128_READ(R128_PM4_BUFFER_ADDR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /* Turn on bus mastering */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000337 tmp = R128_READ(R128_BUS_CNTL) & ~R128_BUS_MASTER_DIS;
338 R128_WRITE(R128_BUS_CNTL, tmp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339}
340
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200341static int r128_do_init_cce(struct drm_device *dev, drm_r128_init_t *init)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 drm_r128_private_t *dev_priv;
Ben Hutchings52f97df2009-08-23 18:37:26 +0100344 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000346 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100348 if (dev->dev_private) {
349 DRM_DEBUG("called when already initialized\n");
350 return -EINVAL;
351 }
352
Eric Anholt9a298b22009-03-24 12:23:04 -0700353 dev_priv = kzalloc(sizeof(drm_r128_private_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000354 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000355 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 dev_priv->is_pci = init->is_pci;
358
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000359 if (dev_priv->is_pci && !dev->sg) {
360 DRM_ERROR("PCI GART memory not allocated!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000362 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000363 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 dev_priv->usec_timeout = init->usec_timeout;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000367 if (dev_priv->usec_timeout < 1 ||
368 dev_priv->usec_timeout > R128_MAX_USEC_TIMEOUT) {
369 DRM_DEBUG("TIMEOUT problem!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000371 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000372 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 dev_priv->cce_mode = init->cce_mode;
376
377 /* GH: Simple idle check.
378 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000379 atomic_set(&dev_priv->idle_count, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 /* We don't support anything other than bus-mastering ring mode,
382 * but the ring can be in either AGP or PCI space for the ring
383 * read pointer.
384 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385 if ((init->cce_mode != R128_PM4_192BM) &&
386 (init->cce_mode != R128_PM4_128BM_64INDBM) &&
387 (init->cce_mode != R128_PM4_64BM_128INDBM) &&
388 (init->cce_mode != R128_PM4_64BM_64VCBM_64INDBM)) {
389 DRM_DEBUG("Bad cce_mode!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000391 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000392 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000395 switch (init->cce_mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 case R128_PM4_NONPM4:
397 dev_priv->cce_fifo_size = 0;
398 break;
399 case R128_PM4_192PIO:
400 case R128_PM4_192BM:
401 dev_priv->cce_fifo_size = 192;
402 break;
403 case R128_PM4_128PIO_64INDBM:
404 case R128_PM4_128BM_64INDBM:
405 dev_priv->cce_fifo_size = 128;
406 break;
407 case R128_PM4_64PIO_128INDBM:
408 case R128_PM4_64BM_128INDBM:
409 case R128_PM4_64PIO_64VCBM_64INDBM:
410 case R128_PM4_64BM_64VCBM_64INDBM:
411 case R128_PM4_64PIO_64VCPIO_64INDPIO:
412 dev_priv->cce_fifo_size = 64;
413 break;
414 }
415
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000416 switch (init->fb_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 case 16:
418 dev_priv->color_fmt = R128_DATATYPE_RGB565;
419 break;
420 case 32:
421 default:
422 dev_priv->color_fmt = R128_DATATYPE_ARGB8888;
423 break;
424 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000425 dev_priv->front_offset = init->front_offset;
426 dev_priv->front_pitch = init->front_pitch;
427 dev_priv->back_offset = init->back_offset;
428 dev_priv->back_pitch = init->back_pitch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000430 switch (init->depth_bpp) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 case 16:
432 dev_priv->depth_fmt = R128_DATATYPE_RGB565;
433 break;
434 case 24:
435 case 32:
436 default:
437 dev_priv->depth_fmt = R128_DATATYPE_ARGB8888;
438 break;
439 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000440 dev_priv->depth_offset = init->depth_offset;
441 dev_priv->depth_pitch = init->depth_pitch;
442 dev_priv->span_offset = init->span_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000444 dev_priv->front_pitch_offset_c = (((dev_priv->front_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 (dev_priv->front_offset >> 5));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 dev_priv->back_pitch_offset_c = (((dev_priv->back_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 (dev_priv->back_offset >> 5));
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000448 dev_priv->depth_pitch_offset_c = (((dev_priv->depth_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 (dev_priv->depth_offset >> 5) |
450 R128_DST_TILE);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000451 dev_priv->span_pitch_offset_c = (((dev_priv->depth_pitch / 8) << 21) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 (dev_priv->span_offset >> 5));
453
David Herrmann9fc5cde2014-08-29 12:12:28 +0200454 dev_priv->sarea = drm_legacy_getsarea(dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000455 if (!dev_priv->sarea) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 DRM_ERROR("could not find sarea!\n");
457 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000458 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000459 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200462 dev_priv->mmio = drm_legacy_findmap(dev, init->mmio_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000463 if (!dev_priv->mmio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 DRM_ERROR("could not find mmio region!\n");
465 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000466 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000467 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200469 dev_priv->cce_ring = drm_legacy_findmap(dev, init->ring_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000470 if (!dev_priv->cce_ring) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 DRM_ERROR("could not find cce ring region!\n");
472 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000473 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000474 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200476 dev_priv->ring_rptr = drm_legacy_findmap(dev, init->ring_rptr_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000477 if (!dev_priv->ring_rptr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 DRM_ERROR("could not find ring read pointer!\n");
479 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000480 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000481 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Dave Airlied1f2b552005-08-05 22:11:22 +1000483 dev->agp_buffer_token = init->buffers_offset;
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200484 dev->agp_buffer_map = drm_legacy_findmap(dev, init->buffers_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000485 if (!dev->agp_buffer_map) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 DRM_ERROR("could not find dma buffer region!\n");
487 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000488 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000489 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000492 if (!dev_priv->is_pci) {
493 dev_priv->agp_textures =
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200494 drm_legacy_findmap(dev, init->agp_textures_offset);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000495 if (!dev_priv->agp_textures) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 DRM_ERROR("could not find agp texture region!\n");
497 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000498 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000499 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501 }
502
503 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000504 (drm_r128_sarea_t *) ((u8 *) dev_priv->sarea->handle +
505 init->sarea_priv_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200507#if IS_ENABLED(CONFIG_AGP)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000508 if (!dev_priv->is_pci) {
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200509 drm_legacy_ioremap_wc(dev_priv->cce_ring, dev);
510 drm_legacy_ioremap_wc(dev_priv->ring_rptr, dev);
511 drm_legacy_ioremap_wc(dev->agp_buffer_map, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000512 if (!dev_priv->cce_ring->handle ||
513 !dev_priv->ring_rptr->handle ||
514 !dev->agp_buffer_map->handle) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 DRM_ERROR("Could not ioremap agp regions!\n");
516 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000518 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 } else
521#endif
522 {
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100523 dev_priv->cce_ring->handle =
524 (void *)(unsigned long)dev_priv->cce_ring->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 dev_priv->ring_rptr->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100526 (void *)(unsigned long)dev_priv->ring_rptr->offset;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000527 dev->agp_buffer_map->handle =
Benjamin Herrenschmidt41c2e752009-02-02 16:55:47 +1100528 (void *)(unsigned long)dev->agp_buffer_map->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200531#if IS_ENABLED(CONFIG_AGP)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000532 if (!dev_priv->is_pci)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 dev_priv->cce_buffers_offset = dev->agp->base;
534 else
535#endif
Dave Airlied1f2b552005-08-05 22:11:22 +1000536 dev_priv->cce_buffers_offset = (unsigned long)dev->sg->virtual;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000538 dev_priv->ring.start = (u32 *) dev_priv->cce_ring->handle;
539 dev_priv->ring.end = ((u32 *) dev_priv->cce_ring->handle
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 + init->ring_size / sizeof(u32));
541 dev_priv->ring.size = init->ring_size;
Daniel Vetter0e267942013-07-10 14:11:58 +0200542 dev_priv->ring.size_l2qw = order_base_2(init->ring_size / 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000544 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 dev_priv->ring.high_mark = 128;
547
548 dev_priv->sarea_priv->last_frame = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000549 R128_WRITE(R128_LAST_FRAME_REG, dev_priv->sarea_priv->last_frame);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551 dev_priv->sarea_priv->last_dispatch = 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000552 R128_WRITE(R128_LAST_DISPATCH_REG, dev_priv->sarea_priv->last_dispatch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200554#if IS_ENABLED(CONFIG_AGP)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000555 if (dev_priv->is_pci) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556#endif
Dave Airlieb05c2382008-03-17 10:24:24 +1000557 dev_priv->gart_info.table_mask = DMA_BIT_MASK(32);
Dave Airlieea98a922005-09-11 20:28:11 +1000558 dev_priv->gart_info.gart_table_location = DRM_ATI_GART_MAIN;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000559 dev_priv->gart_info.table_size = R128_PCIGART_TABLE_SIZE;
Dave Airlief26c4732006-01-02 17:18:39 +1100560 dev_priv->gart_info.addr = NULL;
561 dev_priv->gart_info.bus_addr = 0;
Dave Airlief2b04cd2007-05-08 15:19:23 +1000562 dev_priv->gart_info.gart_reg_if = DRM_ATI_GART_PCI;
Dave Airlieea98a922005-09-11 20:28:11 +1000563 if (!drm_ati_pcigart_init(dev, &dev_priv->gart_info)) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000564 DRM_ERROR("failed to init PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 dev->dev_private = (void *)dev_priv;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000566 r128_do_cleanup_cce(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000567 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
Dave Airlieea98a922005-09-11 20:28:11 +1000569 R128_WRITE(R128_PCI_GART_PAGE, dev_priv->gart_info.bus_addr);
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200570#if IS_ENABLED(CONFIG_AGP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572#endif
573
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000574 r128_cce_init_ring_buffer(dev, dev_priv);
Ben Hutchings52f97df2009-08-23 18:37:26 +0100575 rc = r128_cce_load_microcode(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 dev->dev_private = (void *)dev_priv;
578
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000579 r128_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Ben Hutchings52f97df2009-08-23 18:37:26 +0100581 if (rc) {
582 DRM_ERROR("Failed to load firmware!\n");
583 r128_do_cleanup_cce(dev);
584 }
585
586 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200589int r128_do_cleanup_cce(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591
592 /* Make sure interrupts are disabled here because the uninstall ioctl
593 * may not have been called from userspace and after dev_private
594 * is freed, it's too late.
595 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000596 if (dev->irq_enabled)
597 drm_irq_uninstall(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000599 if (dev->dev_private) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 drm_r128_private_t *dev_priv = dev->dev_private;
601
Daniel Vettera7fb8a22015-09-09 16:45:52 +0200602#if IS_ENABLED(CONFIG_AGP)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000603 if (!dev_priv->is_pci) {
604 if (dev_priv->cce_ring != NULL)
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200605 drm_legacy_ioremapfree(dev_priv->cce_ring, dev);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000606 if (dev_priv->ring_rptr != NULL)
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200607 drm_legacy_ioremapfree(dev_priv->ring_rptr, dev);
Dave Airlief26c4732006-01-02 17:18:39 +1100608 if (dev->agp_buffer_map != NULL) {
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200609 drm_legacy_ioremapfree(dev->agp_buffer_map, dev);
Dave Airlief26c4732006-01-02 17:18:39 +1100610 dev->agp_buffer_map = NULL;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 } else
613#endif
614 {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000615 if (dev_priv->gart_info.bus_addr)
616 if (!drm_ati_pcigart_cleanup(dev,
Dave Airlief26c4732006-01-02 17:18:39 +1100617 &dev_priv->gart_info))
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000618 DRM_ERROR
619 ("failed to cleanup PCI GART!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 }
621
Eric Anholt9a298b22009-03-24 12:23:04 -0700622 kfree(dev->dev_private);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 dev->dev_private = NULL;
624 }
625
626 return 0;
627}
628
Eric Anholtc153f452007-09-03 12:06:45 +1000629int r128_cce_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Eric Anholtc153f452007-09-03 12:06:45 +1000631 drm_r128_init_t *init = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000633 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Eric Anholt6c340ea2007-08-25 20:23:09 +1000635 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Eric Anholtc153f452007-09-03 12:06:45 +1000637 switch (init->func) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 case R128_INIT_CCE:
Eric Anholtc153f452007-09-03 12:06:45 +1000639 return r128_do_init_cce(dev, init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 case R128_CLEANUP_CCE:
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000641 return r128_do_cleanup_cce(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Eric Anholt20caafa2007-08-25 19:22:43 +1000644 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Eric Anholtc153f452007-09-03 12:06:45 +1000647int r128_cce_start(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Eric Anholt6c340ea2007-08-25 20:23:09 +1000652 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100654 DEV_INIT_TEST_WITH_RETURN(dev_priv);
655
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000656 if (dev_priv->cce_running || dev_priv->cce_mode == R128_PM4_NONPM4) {
Márton Németh3e684ea2008-01-24 15:58:57 +1000657 DRM_DEBUG("while CCE running\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 0;
659 }
660
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000661 r128_do_cce_start(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
663 return 0;
664}
665
666/* Stop the CCE. The engine must have been idled before calling this
667 * routine.
668 */
Eric Anholtc153f452007-09-03 12:06:45 +1000669int r128_cce_stop(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 drm_r128_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000672 drm_r128_cce_stop_t *stop = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 int ret;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000674 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Eric Anholt6c340ea2007-08-25 20:23:09 +1000676 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100678 DEV_INIT_TEST_WITH_RETURN(dev_priv);
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 /* Flush any pending CCE commands. This ensures any outstanding
681 * commands are exectuted by the engine before we turn it off.
682 */
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200683 if (stop->flush)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000684 r128_do_cce_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* If we fail to make the engine go idle, we return an error
687 * code so that the DRM ioctl wrapper can try again.
688 */
Eric Anholtc153f452007-09-03 12:06:45 +1000689 if (stop->idle) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000690 ret = r128_do_cce_idle(dev_priv);
691 if (ret)
692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694
695 /* Finally, we can turn off the CCE. If the engine isn't idle,
696 * we will get some dropped triangles as they won't be fully
697 * rendered before the CCE is shut down.
698 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000699 r128_do_cce_stop(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
701 /* Reset the engine */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000702 r128_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return 0;
705}
706
707/* Just reset the CCE ring. Called as part of an X Server engine reset.
708 */
Eric Anholtc153f452007-09-03 12:06:45 +1000709int r128_cce_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000712 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Eric Anholt6c340ea2007-08-25 20:23:09 +1000714 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100716 DEV_INIT_TEST_WITH_RETURN(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000718 r128_do_cce_reset(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 /* The CCE is no longer running after an engine reset */
721 dev_priv->cce_running = 0;
722
723 return 0;
724}
725
Eric Anholtc153f452007-09-03 12:06:45 +1000726int r128_cce_idle(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000729 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Eric Anholt6c340ea2007-08-25 20:23:09 +1000731 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100733 DEV_INIT_TEST_WITH_RETURN(dev_priv);
734
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200735 if (dev_priv->cce_running)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000736 r128_do_cce_flush(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000738 return r128_do_cce_idle(dev_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
Eric Anholtc153f452007-09-03 12:06:45 +1000741int r128_engine_reset(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000743 DRM_DEBUG("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
Eric Anholt6c340ea2007-08-25 20:23:09 +1000745 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Ben Hutchings7dc482d2009-08-23 16:59:04 +0100747 DEV_INIT_TEST_WITH_RETURN(dev->dev_private);
748
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000749 return r128_do_engine_reset(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
Eric Anholtc153f452007-09-03 12:06:45 +1000752int r128_fullscreen(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Eric Anholt20caafa2007-08-25 19:22:43 +1000754 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/* ================================================================
758 * Freelist management
759 */
760#define R128_BUFFER_USED 0xffffffff
761#define R128_BUFFER_FREE 0
762
763#if 0
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200764static int r128_freelist_init(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Dave Airliecdd55a22007-07-11 16:32:08 +1000766 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 drm_r128_private_t *dev_priv = dev->dev_private;
Dave Airlie056219e2007-07-11 16:17:42 +1000768 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 drm_r128_buf_priv_t *buf_priv;
770 drm_r128_freelist_t *entry;
771 int i;
772
Eric Anholt9a298b22009-03-24 12:23:04 -0700773 dev_priv->head = kzalloc(sizeof(drm_r128_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000774 if (dev_priv->head == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000775 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 dev_priv->head->age = R128_BUFFER_USED;
778
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000779 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 buf = dma->buflist[i];
781 buf_priv = buf->dev_private;
782
Eric Anholt9a298b22009-03-24 12:23:04 -0700783 entry = kmalloc(sizeof(drm_r128_freelist_t), GFP_KERNEL);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000784 if (!entry)
Eric Anholt20caafa2007-08-25 19:22:43 +1000785 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 entry->age = R128_BUFFER_FREE;
788 entry->buf = buf;
789 entry->prev = dev_priv->head;
790 entry->next = dev_priv->head->next;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000791 if (!entry->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 dev_priv->tail = entry;
793
794 buf_priv->discard = 0;
795 buf_priv->dispatched = 0;
796 buf_priv->list_entry = entry;
797
798 dev_priv->head->next = entry;
799
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000800 if (dev_priv->head->next)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 dev_priv->head->next->prev = entry;
802 }
803
804 return 0;
805
806}
807#endif
808
Dave Airlie056219e2007-07-11 16:17:42 +1000809static struct drm_buf *r128_freelist_get(struct drm_device * dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Dave Airliecdd55a22007-07-11 16:32:08 +1000811 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 drm_r128_private_t *dev_priv = dev->dev_private;
813 drm_r128_buf_priv_t *buf_priv;
Dave Airlie056219e2007-07-11 16:17:42 +1000814 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 int i, t;
816
817 /* FIXME: Optimize -- use freelist code */
818
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 buf = dma->buflist[i];
821 buf_priv = buf->dev_private;
Harvey Harrison8da56302008-04-28 14:13:20 -0700822 if (!buf->file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 return buf;
824 }
825
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000826 for (t = 0; t < dev_priv->usec_timeout; t++) {
827 u32 done_age = R128_READ(R128_LAST_DISPATCH_REG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000829 for (i = 0; i < dma->buf_count; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 buf = dma->buflist[i];
831 buf_priv = buf->dev_private;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000832 if (buf->pending && buf_priv->age <= done_age) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* The buffer has been processed, so it
834 * can now be used.
835 */
836 buf->pending = 0;
837 return buf;
838 }
839 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000840 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 }
842
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000843 DRM_DEBUG("returning NULL!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return NULL;
845}
846
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200847void r128_freelist_reset(struct drm_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
Dave Airliecdd55a22007-07-11 16:32:08 +1000849 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 int i;
851
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000852 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +1000853 struct drm_buf *buf = dma->buflist[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 drm_r128_buf_priv_t *buf_priv = buf->dev_private;
855 buf_priv->age = 0;
856 }
857}
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859/* ================================================================
860 * CCE command submission
861 */
862
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200863int r128_wait_ring(drm_r128_private_t *dev_priv, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 drm_r128_ring_buffer_t *ring = &dev_priv->ring;
866 int i;
867
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000868 for (i = 0; i < dev_priv->usec_timeout; i++) {
869 r128_update_ring_snapshot(dev_priv);
870 if (ring->space >= n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 return 0;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000872 DRM_UDELAY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
875 /* FIXME: This is being ignored... */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000876 DRM_ERROR("failed!\n");
Eric Anholt20caafa2007-08-25 19:22:43 +1000877 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200880static int r128_cce_get_buffers(struct drm_device *dev,
Eric Anholt6c340ea2007-08-25 20:23:09 +1000881 struct drm_file *file_priv,
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200882 struct drm_dma *d)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883{
884 int i;
Dave Airlie056219e2007-07-11 16:17:42 +1000885 struct drm_buf *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000887 for (i = d->granted_count; i < d->request_count; i++) {
888 buf = r128_freelist_get(dev);
889 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +1000890 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Eric Anholt6c340ea2007-08-25 20:23:09 +1000892 buf->file_priv = file_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100894 if (copy_to_user(&d->request_indices[i], &buf->idx,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000895 sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000896 return -EFAULT;
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100897 if (copy_to_user(&d->request_sizes[i], &buf->total,
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000898 sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000899 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 d->granted_count++;
902 }
903 return 0;
904}
905
Eric Anholtc153f452007-09-03 12:06:45 +1000906int r128_cce_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
Dave Airliecdd55a22007-07-11 16:32:08 +1000908 struct drm_device_dma *dma = dev->dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 int ret = 0;
Eric Anholtc153f452007-09-03 12:06:45 +1000910 struct drm_dma *d = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Eric Anholt6c340ea2007-08-25 20:23:09 +1000912 LOCK_TEST_WITH_RETURN(dev, file_priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 /* Please don't send us buffers.
915 */
Eric Anholtc153f452007-09-03 12:06:45 +1000916 if (d->send_count != 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000917 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000918 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000919 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 }
921
922 /* We'll send you buffers.
923 */
Eric Anholtc153f452007-09-03 12:06:45 +1000924 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000925 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +1000926 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +1000927 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 }
929
Eric Anholtc153f452007-09-03 12:06:45 +1000930 d->granted_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
Nicolas Kaiserbc5e9d62010-07-12 13:44:23 +0200932 if (d->request_count)
Eric Anholtc153f452007-09-03 12:06:45 +1000933 ret = r128_cce_get_buffers(dev, file_priv, d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 return ret;
936}