1 /* mach64_dma.c -- DMA support for mach64 (Rage Pro) driver -*- linux-c -*- */
4 * DMA support for mach64 (Rage Pro) driver
6 * \author Gareth Hughes <gareth@valinux.com>
7 * \author Frank C. Earl <fearl@airmail.net>
8 * \author Leif Delgass <ldelgass@retinalburn.net>
9 * \author José Fonseca <j_r_fonseca@yahoo.co.uk>
13 * Copyright 2000 Gareth Hughes
14 * Copyright 2002 Frank C. Earl
15 * Copyright 2002-2003 Leif Delgass
16 * All Rights Reserved.
18 * Permission is hereby granted, free of charge, to any person obtaining a
19 * copy of this software and associated documentation files (the "Software"),
20 * to deal in the Software without restriction, including without limitation
21 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
22 * and/or sell copies of the Software, and to permit persons to whom the
23 * Software is furnished to do so, subject to the following conditions:
25 * The above copyright notice and this permission notice (including the next
26 * paragraph) shall be included in all copies or substantial portions of the
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
32 * THE COPYRIGHT OWNER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include "dev/drm/drmP.h"
41 #include "dev/drm/drm.h"
42 #include "dev/drm/mach64_drm.h"
43 #include "dev/drm/mach64_drv.h"
45 /*******************************************************************/
46 /** \name Engine, FIFO control */
50 * Waits for free entries in the FIFO.
52 * \note Most writes to Mach64 registers are automatically routed through
53 * command FIFO which is 16 entry deep. Prior to writing to any draw engine
54 * register one has to ensure that enough FIFO entries are available by calling
55 * this function. Failure to do so may cause the engine to lock.
57 * \param dev_priv pointer to device private data structure.
58 * \param entries number of free entries in the FIFO to wait for.
60 * \returns zero on success, or -EBUSY if the timeout (specificed by
61 * drm_mach64_private::usec_timeout) occurs.
63 int mach64_do_wait_for_fifo(drm_mach64_private_t *dev_priv, int entries)
67 for (i = 0; i < dev_priv->usec_timeout; i++) {
68 slots = (MACH64_READ(MACH64_FIFO_STAT) & MACH64_FIFO_SLOT_MASK);
69 if (slots <= (0x8000 >> entries))
74 DRM_INFO("failed! slots=%d entries=%d\n", slots, entries);
79 * Wait for the draw engine to be idle.
81 int mach64_do_wait_for_idle(drm_mach64_private_t *dev_priv)
85 ret = mach64_do_wait_for_fifo(dev_priv, 16);
89 for (i = 0; i < dev_priv->usec_timeout; i++) {
90 if (!(MACH64_READ(MACH64_GUI_STAT) & MACH64_GUI_ACTIVE))
95 DRM_INFO("failed! GUI_STAT=0x%08x\n", MACH64_READ(MACH64_GUI_STAT));
96 mach64_dump_ring_info(dev_priv);
101 * Wait for free entries in the ring buffer.
103 * The Mach64 bus master can be configured to act as a virtual FIFO, using a
104 * circular buffer (commonly referred as "ring buffer" in other drivers) with
105 * pointers to engine commands. This allows the CPU to do other things while
106 * the graphics engine is busy, i.e., DMA mode.
108 * This function should be called before writing new entries to the ring
111 * \param dev_priv pointer to device private data structure.
112 * \param n number of free entries in the ring buffer to wait for.
114 * \returns zero on success, or -EBUSY if the timeout (specificed by
115 * drm_mach64_private_t::usec_timeout) occurs.
117 * \sa mach64_dump_ring_info()
119 int mach64_wait_ring(drm_mach64_private_t *dev_priv, int n)
121 drm_mach64_descriptor_ring_t *ring = &dev_priv->ring;
124 for (i = 0; i < dev_priv->usec_timeout; i++) {
125 mach64_update_ring_snapshot(dev_priv);
126 if (ring->space >= n) {
128 DRM_DEBUG("%d usecs\n", i);
134 /* FIXME: This is being ignored... */
135 DRM_ERROR("failed!\n");
136 mach64_dump_ring_info(dev_priv);
141 * Wait until all DMA requests have been processed...
143 * \sa mach64_wait_ring()
145 static int mach64_ring_idle(drm_mach64_private_t *dev_priv)
147 drm_mach64_descriptor_ring_t *ring = &dev_priv->ring;
153 while (i < dev_priv->usec_timeout) {
154 mach64_update_ring_snapshot(dev_priv);
155 if (ring->head == ring->tail &&
156 !(MACH64_READ(MACH64_GUI_STAT) & MACH64_GUI_ACTIVE)) {
158 DRM_DEBUG("%d usecs\n", i);
161 if (ring->head == head) {
170 DRM_INFO("failed! GUI_STAT=0x%08x\n", MACH64_READ(MACH64_GUI_STAT));
171 mach64_dump_ring_info(dev_priv);
176 * Reset the ring buffer descriptors.
178 * \sa mach64_do_engine_reset()
180 static void mach64_ring_reset(drm_mach64_private_t *dev_priv)
182 drm_mach64_descriptor_ring_t *ring = &dev_priv->ring;
184 mach64_do_release_used_buffers(dev_priv);
185 ring->head_addr = ring->start_addr;
186 ring->head = ring->tail = 0;
187 ring->space = ring->size;
189 MACH64_WRITE(MACH64_BM_GUI_TABLE_CMD,
190 ring->head_addr | MACH64_CIRCULAR_BUF_SIZE_16KB);
192 dev_priv->ring_running = 0;
196 * Ensure the all the queued commands will be processed.
198 int mach64_do_dma_flush(drm_mach64_private_t *dev_priv)
200 /* FIXME: It's not necessary to wait for idle when flushing
201 * we just need to ensure the ring will be completely processed
202 * in finite time without another ioctl
204 return mach64_ring_idle(dev_priv);
208 * Stop all DMA activity.
210 int mach64_do_dma_idle(drm_mach64_private_t *dev_priv)
214 /* wait for completion */
215 if ((ret = mach64_ring_idle(dev_priv)) < 0) {
216 DRM_ERROR("failed BM_GUI_TABLE=0x%08x tail: %u\n",
217 MACH64_READ(MACH64_BM_GUI_TABLE),
218 dev_priv->ring.tail);
222 mach64_ring_stop(dev_priv);
224 /* clean up after pass */
225 mach64_do_release_used_buffers(dev_priv);
230 * Reset the engine. This will stop the DMA if it is running.
232 int mach64_do_engine_reset(drm_mach64_private_t *dev_priv)
238 /* Kill off any outstanding DMA transfers.
240 tmp = MACH64_READ(MACH64_BUS_CNTL);
241 MACH64_WRITE(MACH64_BUS_CNTL, tmp | MACH64_BUS_MASTER_DIS);
243 /* Reset the GUI engine (high to low transition).
245 tmp = MACH64_READ(MACH64_GEN_TEST_CNTL);
246 MACH64_WRITE(MACH64_GEN_TEST_CNTL, tmp & ~MACH64_GUI_ENGINE_ENABLE);
247 /* Enable the GUI engine
249 tmp = MACH64_READ(MACH64_GEN_TEST_CNTL);
250 MACH64_WRITE(MACH64_GEN_TEST_CNTL, tmp | MACH64_GUI_ENGINE_ENABLE);
252 /* ensure engine is not locked up by clearing any FIFO or HOST errors
254 tmp = MACH64_READ(MACH64_BUS_CNTL);
255 MACH64_WRITE(MACH64_BUS_CNTL, tmp | 0x00a00000);
257 /* Once GUI engine is restored, disable bus mastering */
258 MACH64_WRITE(MACH64_SRC_CNTL, 0);
260 /* Reset descriptor ring */
261 mach64_ring_reset(dev_priv);
269 /*******************************************************************/
270 /** \name Debugging output */
274 * Dump engine registers values.
276 void mach64_dump_engine_info(drm_mach64_private_t *dev_priv)
279 if (!dev_priv->is_pci) {
280 DRM_INFO(" AGP_BASE = 0x%08x\n",
281 MACH64_READ(MACH64_AGP_BASE));
282 DRM_INFO(" AGP_CNTL = 0x%08x\n",
283 MACH64_READ(MACH64_AGP_CNTL));
285 DRM_INFO(" ALPHA_TST_CNTL = 0x%08x\n",
286 MACH64_READ(MACH64_ALPHA_TST_CNTL));
288 DRM_INFO(" BM_COMMAND = 0x%08x\n",
289 MACH64_READ(MACH64_BM_COMMAND));
290 DRM_INFO("BM_FRAME_BUF_OFFSET = 0x%08x\n",
291 MACH64_READ(MACH64_BM_FRAME_BUF_OFFSET));
292 DRM_INFO(" BM_GUI_TABLE = 0x%08x\n",
293 MACH64_READ(MACH64_BM_GUI_TABLE));
294 DRM_INFO(" BM_STATUS = 0x%08x\n",
295 MACH64_READ(MACH64_BM_STATUS));
296 DRM_INFO(" BM_SYSTEM_MEM_ADDR = 0x%08x\n",
297 MACH64_READ(MACH64_BM_SYSTEM_MEM_ADDR));
298 DRM_INFO(" BM_SYSTEM_TABLE = 0x%08x\n",
299 MACH64_READ(MACH64_BM_SYSTEM_TABLE));
300 DRM_INFO(" BUS_CNTL = 0x%08x\n",
301 MACH64_READ(MACH64_BUS_CNTL));
303 /* DRM_INFO( " CLOCK_CNTL = 0x%08x\n", MACH64_READ( MACH64_CLOCK_CNTL ) ); */
304 DRM_INFO(" CLR_CMP_CLR = 0x%08x\n",
305 MACH64_READ(MACH64_CLR_CMP_CLR));
306 DRM_INFO(" CLR_CMP_CNTL = 0x%08x\n",
307 MACH64_READ(MACH64_CLR_CMP_CNTL));
308 /* DRM_INFO( " CLR_CMP_MSK = 0x%08x\n", MACH64_READ( MACH64_CLR_CMP_MSK ) ); */
309 DRM_INFO(" CONFIG_CHIP_ID = 0x%08x\n",
310 MACH64_READ(MACH64_CONFIG_CHIP_ID));
311 DRM_INFO(" CONFIG_CNTL = 0x%08x\n",
312 MACH64_READ(MACH64_CONFIG_CNTL));
313 DRM_INFO(" CONFIG_STAT0 = 0x%08x\n",
314 MACH64_READ(MACH64_CONFIG_STAT0));
315 DRM_INFO(" CONFIG_STAT1 = 0x%08x\n",
316 MACH64_READ(MACH64_CONFIG_STAT1));
317 DRM_INFO(" CONFIG_STAT2 = 0x%08x\n",
318 MACH64_READ(MACH64_CONFIG_STAT2));
319 DRM_INFO(" CRC_SIG = 0x%08x\n", MACH64_READ(MACH64_CRC_SIG));
320 DRM_INFO(" CUSTOM_MACRO_CNTL = 0x%08x\n",
321 MACH64_READ(MACH64_CUSTOM_MACRO_CNTL));
323 /* DRM_INFO( " DAC_CNTL = 0x%08x\n", MACH64_READ( MACH64_DAC_CNTL ) ); */
324 /* DRM_INFO( " DAC_REGS = 0x%08x\n", MACH64_READ( MACH64_DAC_REGS ) ); */
325 DRM_INFO(" DP_BKGD_CLR = 0x%08x\n",
326 MACH64_READ(MACH64_DP_BKGD_CLR));
327 DRM_INFO(" DP_FRGD_CLR = 0x%08x\n",
328 MACH64_READ(MACH64_DP_FRGD_CLR));
329 DRM_INFO(" DP_MIX = 0x%08x\n", MACH64_READ(MACH64_DP_MIX));
330 DRM_INFO(" DP_PIX_WIDTH = 0x%08x\n",
331 MACH64_READ(MACH64_DP_PIX_WIDTH));
332 DRM_INFO(" DP_SRC = 0x%08x\n", MACH64_READ(MACH64_DP_SRC));
333 DRM_INFO(" DP_WRITE_MASK = 0x%08x\n",
334 MACH64_READ(MACH64_DP_WRITE_MASK));
335 DRM_INFO(" DSP_CONFIG = 0x%08x\n",
336 MACH64_READ(MACH64_DSP_CONFIG));
337 DRM_INFO(" DSP_ON_OFF = 0x%08x\n",
338 MACH64_READ(MACH64_DSP_ON_OFF));
339 DRM_INFO(" DST_CNTL = 0x%08x\n",
340 MACH64_READ(MACH64_DST_CNTL));
341 DRM_INFO(" DST_OFF_PITCH = 0x%08x\n",
342 MACH64_READ(MACH64_DST_OFF_PITCH));
344 /* DRM_INFO( " EXT_DAC_REGS = 0x%08x\n", MACH64_READ( MACH64_EXT_DAC_REGS ) ); */
345 DRM_INFO(" EXT_MEM_CNTL = 0x%08x\n",
346 MACH64_READ(MACH64_EXT_MEM_CNTL));
348 DRM_INFO(" FIFO_STAT = 0x%08x\n",
349 MACH64_READ(MACH64_FIFO_STAT));
351 DRM_INFO(" GEN_TEST_CNTL = 0x%08x\n",
352 MACH64_READ(MACH64_GEN_TEST_CNTL));
353 /* DRM_INFO( " GP_IO = 0x%08x\n", MACH64_READ( MACH64_GP_IO ) ); */
354 DRM_INFO(" GUI_CMDFIFO_DATA = 0x%08x\n",
355 MACH64_READ(MACH64_GUI_CMDFIFO_DATA));
356 DRM_INFO(" GUI_CMDFIFO_DEBUG = 0x%08x\n",
357 MACH64_READ(MACH64_GUI_CMDFIFO_DEBUG));
358 DRM_INFO(" GUI_CNTL = 0x%08x\n",
359 MACH64_READ(MACH64_GUI_CNTL));
360 DRM_INFO(" GUI_STAT = 0x%08x\n",
361 MACH64_READ(MACH64_GUI_STAT));
362 DRM_INFO(" GUI_TRAJ_CNTL = 0x%08x\n",
363 MACH64_READ(MACH64_GUI_TRAJ_CNTL));
365 DRM_INFO(" HOST_CNTL = 0x%08x\n",
366 MACH64_READ(MACH64_HOST_CNTL));
367 DRM_INFO(" HW_DEBUG = 0x%08x\n",
368 MACH64_READ(MACH64_HW_DEBUG));
370 DRM_INFO(" MEM_ADDR_CONFIG = 0x%08x\n",
371 MACH64_READ(MACH64_MEM_ADDR_CONFIG));
372 DRM_INFO(" MEM_BUF_CNTL = 0x%08x\n",
373 MACH64_READ(MACH64_MEM_BUF_CNTL));
375 DRM_INFO(" PAT_REG0 = 0x%08x\n",
376 MACH64_READ(MACH64_PAT_REG0));
377 DRM_INFO(" PAT_REG1 = 0x%08x\n",
378 MACH64_READ(MACH64_PAT_REG1));
380 DRM_INFO(" SC_LEFT = 0x%08x\n", MACH64_READ(MACH64_SC_LEFT));
381 DRM_INFO(" SC_RIGHT = 0x%08x\n",
382 MACH64_READ(MACH64_SC_RIGHT));
383 DRM_INFO(" SC_TOP = 0x%08x\n", MACH64_READ(MACH64_SC_TOP));
384 DRM_INFO(" SC_BOTTOM = 0x%08x\n",
385 MACH64_READ(MACH64_SC_BOTTOM));
387 DRM_INFO(" SCALE_3D_CNTL = 0x%08x\n",
388 MACH64_READ(MACH64_SCALE_3D_CNTL));
389 DRM_INFO(" SCRATCH_REG0 = 0x%08x\n",
390 MACH64_READ(MACH64_SCRATCH_REG0));
391 DRM_INFO(" SCRATCH_REG1 = 0x%08x\n",
392 MACH64_READ(MACH64_SCRATCH_REG1));
393 DRM_INFO(" SETUP_CNTL = 0x%08x\n",
394 MACH64_READ(MACH64_SETUP_CNTL));
395 DRM_INFO(" SRC_CNTL = 0x%08x\n",
396 MACH64_READ(MACH64_SRC_CNTL));
398 DRM_INFO(" TEX_CNTL = 0x%08x\n",
399 MACH64_READ(MACH64_TEX_CNTL));
400 DRM_INFO(" TEX_SIZE_PITCH = 0x%08x\n",
401 MACH64_READ(MACH64_TEX_SIZE_PITCH));
402 DRM_INFO(" TIMER_CONFIG = 0x%08x\n",
403 MACH64_READ(MACH64_TIMER_CONFIG));
405 DRM_INFO(" Z_CNTL = 0x%08x\n", MACH64_READ(MACH64_Z_CNTL));
406 DRM_INFO(" Z_OFF_PITCH = 0x%08x\n",
407 MACH64_READ(MACH64_Z_OFF_PITCH));
411 #define MACH64_DUMP_CONTEXT 3
414 * Used by mach64_dump_ring_info() to dump the contents of the current buffer
415 * pointed by the ring head.
417 static void mach64_dump_buf_info(drm_mach64_private_t *dev_priv,
420 u32 addr = GETBUFADDR(buf);
421 u32 used = buf->used >> 2;
422 u32 sys_addr = MACH64_READ(MACH64_BM_SYSTEM_MEM_ADDR);
423 u32 *p = GETBUFPTR(buf);
426 DRM_INFO("buffer contents:\n");
431 reg = le32_to_cpu(*p++);
432 if (addr <= GETBUFADDR(buf) + MACH64_DUMP_CONTEXT * 4 ||
433 (addr >= sys_addr - MACH64_DUMP_CONTEXT * 4 &&
434 addr <= sys_addr + MACH64_DUMP_CONTEXT * 4) ||
436 GETBUFADDR(buf) + buf->used - MACH64_DUMP_CONTEXT * 4) {
437 DRM_INFO("%08x: 0x%08x\n", addr, reg);
442 count = (reg >> 16) + 1;
445 while (count && used) {
446 if (addr <= GETBUFADDR(buf) + MACH64_DUMP_CONTEXT * 4 ||
447 (addr >= sys_addr - MACH64_DUMP_CONTEXT * 4 &&
448 addr <= sys_addr + MACH64_DUMP_CONTEXT * 4) ||
450 GETBUFADDR(buf) + buf->used -
451 MACH64_DUMP_CONTEXT * 4) {
452 DRM_INFO("%08x: 0x%04x = 0x%08x\n", addr,
453 reg, le32_to_cpu(*p));
474 * Dump the ring state and contents, including the contents of the buffer being
475 * processed by the graphics engine.
477 void mach64_dump_ring_info(drm_mach64_private_t *dev_priv)
479 drm_mach64_descriptor_ring_t *ring = &dev_priv->ring;
484 DRM_INFO("ring contents:\n");
485 DRM_INFO(" head_addr: 0x%08x head: %u tail: %u\n\n",
486 ring->head_addr, ring->head, ring->tail);
489 for (i = 0; i < ring->size / sizeof(u32); i += 4) {
490 if (i <= MACH64_DUMP_CONTEXT * 4 ||
491 i >= ring->size / sizeof(u32) - MACH64_DUMP_CONTEXT * 4 ||
492 (i >= ring->tail - MACH64_DUMP_CONTEXT * 4 &&
493 i <= ring->tail + MACH64_DUMP_CONTEXT * 4) ||
494 (i >= ring->head - MACH64_DUMP_CONTEXT * 4 &&
495 i <= ring->head + MACH64_DUMP_CONTEXT * 4)) {
496 DRM_INFO(" 0x%08x: 0x%08x 0x%08x 0x%08x 0x%08x%s%s\n",
497 (u32)(ring->start_addr + i * sizeof(u32)),
498 le32_to_cpu(((u32 *) ring->start)[i + 0]),
499 le32_to_cpu(((u32 *) ring->start)[i + 1]),
500 le32_to_cpu(((u32 *) ring->start)[i + 2]),
501 le32_to_cpu(((u32 *) ring->start)[i + 3]),
502 i == ring->head ? " (head)" : "",
503 i == ring->tail ? " (tail)" : "");
515 if (ring->head < ring->size / sizeof(u32)) {
516 struct list_head *ptr;
517 u32 addr = le32_to_cpu(((u32 *) ring->start)[ring->head + 1]);
519 list_for_each(ptr, &dev_priv->pending) {
520 drm_mach64_freelist_t *entry =
521 list_entry(ptr, drm_mach64_freelist_t, list);
522 struct drm_buf *buf = entry->buf;
524 u32 buf_addr = GETBUFADDR(buf);
526 if (buf_addr <= addr && addr < buf_addr + buf->used)
527 mach64_dump_buf_info(dev_priv, buf);
532 DRM_INFO(" BM_GUI_TABLE = 0x%08x\n",
533 MACH64_READ(MACH64_BM_GUI_TABLE));
535 DRM_INFO("BM_FRAME_BUF_OFFSET = 0x%08x\n",
536 MACH64_READ(MACH64_BM_FRAME_BUF_OFFSET));
537 DRM_INFO(" BM_SYSTEM_MEM_ADDR = 0x%08x\n",
538 MACH64_READ(MACH64_BM_SYSTEM_MEM_ADDR));
539 DRM_INFO(" BM_COMMAND = 0x%08x\n",
540 MACH64_READ(MACH64_BM_COMMAND));
542 DRM_INFO(" BM_STATUS = 0x%08x\n",
543 MACH64_READ(MACH64_BM_STATUS));
544 DRM_INFO(" BUS_CNTL = 0x%08x\n",
545 MACH64_READ(MACH64_BUS_CNTL));
546 DRM_INFO(" FIFO_STAT = 0x%08x\n",
547 MACH64_READ(MACH64_FIFO_STAT));
548 DRM_INFO(" GUI_STAT = 0x%08x\n",
549 MACH64_READ(MACH64_GUI_STAT));
550 DRM_INFO(" SRC_CNTL = 0x%08x\n",
551 MACH64_READ(MACH64_SRC_CNTL));
557 /*******************************************************************/
558 /** \name DMA descriptor ring macros */
562 * Add the end mark to the ring's new tail position.
564 * The bus master engine will keep processing the DMA buffers listed in the ring
565 * until it finds this mark, making it stop.
567 * \sa mach64_clear_dma_eol
569 static __inline__ void mach64_set_dma_eol(volatile u32 *addr)
571 #if defined(__i386__)
574 /* Taken from include/asm-i386/bitops.h linux header */
575 __asm__ __volatile__("lock;" "btsl %1,%0":"=m"(*addr)
577 #elif defined(__powerpc__)
579 u32 mask = cpu_to_le32(MACH64_DMA_EOL);
581 /* Taken from the include/asm-ppc/bitops.h linux header */
582 __asm__ __volatile__("\n\
586 bne- 1b":"=&r"(old), "=m"(*addr)
587 :"r"(mask), "r"(addr), "m"(*addr)
589 #elif defined(__alpha__)
591 u32 mask = MACH64_DMA_EOL;
593 /* Taken from the include/asm-alpha/bitops.h linux header */
594 __asm__ __volatile__("1: ldl_l %0,%3\n"
600 ".previous":"=&r"(temp), "=m"(*addr)
601 :"Ir"(mask), "m"(*addr));
603 u32 mask = cpu_to_le32(MACH64_DMA_EOL);
610 * Remove the end mark from the ring's old tail position.
612 * It should be called after calling mach64_set_dma_eol to mark the ring's new
615 * We update the end marks while the bus master engine is in operation. Since
616 * the bus master engine may potentially be reading from the same position
617 * that we write, we must change atomically to avoid having intermediary bad
620 static __inline__ void mach64_clear_dma_eol(volatile u32 *addr)
622 #if defined(__i386__)
625 /* Taken from include/asm-i386/bitops.h linux header */
626 __asm__ __volatile__("lock;" "btrl %1,%0":"=m"(*addr)
628 #elif defined(__powerpc__)
630 u32 mask = cpu_to_le32(MACH64_DMA_EOL);
632 /* Taken from the include/asm-ppc/bitops.h linux header */
633 __asm__ __volatile__("\n\
637 bne- 1b":"=&r"(old), "=m"(*addr)
638 :"r"(mask), "r"(addr), "m"(*addr)
640 #elif defined(__alpha__)
642 u32 mask = ~MACH64_DMA_EOL;
644 /* Taken from the include/asm-alpha/bitops.h linux header */
645 __asm__ __volatile__("1: ldl_l %0,%3\n"
651 ".previous":"=&r"(temp), "=m"(*addr)
652 :"Ir"(mask), "m"(*addr));
654 u32 mask = cpu_to_le32(~MACH64_DMA_EOL);
660 #define RING_LOCALS \
661 int _ring_tail, _ring_write; unsigned int _ring_mask; volatile u32 *_ring
663 #define RING_WRITE_OFS _ring_write
665 #define BEGIN_RING(n) \
667 if (MACH64_VERBOSE) { \
668 DRM_INFO( "BEGIN_RING( %d ) \n", \
671 if (dev_priv->ring.space <= (n) * sizeof(u32)) { \
673 if ((ret = mach64_wait_ring( dev_priv, (n) * sizeof(u32))) < 0 ) { \
674 DRM_ERROR( "wait_ring failed, resetting engine\n"); \
675 mach64_dump_engine_info( dev_priv ); \
676 mach64_do_engine_reset( dev_priv ); \
680 dev_priv->ring.space -= (n) * sizeof(u32); \
681 _ring = (u32 *) dev_priv->ring.start; \
682 _ring_tail = _ring_write = dev_priv->ring.tail; \
683 _ring_mask = dev_priv->ring.tail_mask; \
686 #define OUT_RING( x ) \
688 if (MACH64_VERBOSE) { \
689 DRM_INFO( " OUT_RING( 0x%08x ) at 0x%x\n", \
690 (unsigned int)(x), _ring_write ); \
692 _ring[_ring_write++] = cpu_to_le32( x ); \
693 _ring_write &= _ring_mask; \
696 #define ADVANCE_RING() \
698 if (MACH64_VERBOSE) { \
699 DRM_INFO( "ADVANCE_RING() wr=0x%06x tail=0x%06x\n", \
700 _ring_write, _ring_tail ); \
702 DRM_MEMORYBARRIER(); \
703 mach64_clear_dma_eol( &_ring[(_ring_tail - 2) & _ring_mask] ); \
704 DRM_MEMORYBARRIER(); \
705 dev_priv->ring.tail = _ring_write; \
706 mach64_ring_tick( dev_priv, &(dev_priv)->ring ); \
710 * Queue a DMA buffer of registers writes into the ring buffer.
712 int mach64_add_buf_to_ring(drm_mach64_private_t *dev_priv,
713 drm_mach64_freelist_t *entry)
715 int bytes, pages, remainder;
718 struct drm_buf *buf = entry->buf;
722 address = GETBUFADDR( buf );
723 pages = (bytes + MACH64_DMA_CHUNKSIZE - 1) / MACH64_DMA_CHUNKSIZE;
725 BEGIN_RING( pages * 4 );
727 for ( i = 0 ; i < pages-1 ; i++ ) {
728 page = address + i * MACH64_DMA_CHUNKSIZE;
729 OUT_RING( MACH64_APERTURE_OFFSET + MACH64_BM_ADDR );
731 OUT_RING( MACH64_DMA_CHUNKSIZE | MACH64_DMA_HOLD_OFFSET );
735 /* generate the final descriptor for any remaining commands in this buffer */
736 page = address + i * MACH64_DMA_CHUNKSIZE;
737 remainder = bytes - i * MACH64_DMA_CHUNKSIZE;
739 /* Save dword offset of last descriptor for this buffer.
740 * This is needed to check for completion of the buffer in freelist_get
742 entry->ring_ofs = RING_WRITE_OFS;
744 OUT_RING( MACH64_APERTURE_OFFSET + MACH64_BM_ADDR );
746 OUT_RING( remainder | MACH64_DMA_HOLD_OFFSET | MACH64_DMA_EOL );
755 * Queue DMA buffer controlling host data tranfers (e.g., blit).
757 * Almost identical to mach64_add_buf_to_ring.
759 int mach64_add_hostdata_buf_to_ring(drm_mach64_private_t *dev_priv,
760 drm_mach64_freelist_t *entry)
762 int bytes, pages, remainder;
765 struct drm_buf *buf = entry->buf;
768 bytes = buf->used - MACH64_HOSTDATA_BLIT_OFFSET;
769 pages = (bytes + MACH64_DMA_CHUNKSIZE - 1) / MACH64_DMA_CHUNKSIZE;
770 address = GETBUFADDR( buf );
772 BEGIN_RING( 4 + pages * 4 );
774 OUT_RING( MACH64_APERTURE_OFFSET + MACH64_BM_ADDR );
776 OUT_RING( MACH64_HOSTDATA_BLIT_OFFSET | MACH64_DMA_HOLD_OFFSET );
778 address += MACH64_HOSTDATA_BLIT_OFFSET;
780 for ( i = 0 ; i < pages-1 ; i++ ) {
781 page = address + i * MACH64_DMA_CHUNKSIZE;
782 OUT_RING( MACH64_APERTURE_OFFSET + MACH64_BM_HOSTDATA );
784 OUT_RING( MACH64_DMA_CHUNKSIZE | MACH64_DMA_HOLD_OFFSET );
788 /* generate the final descriptor for any remaining commands in this buffer */
789 page = address + i * MACH64_DMA_CHUNKSIZE;
790 remainder = bytes - i * MACH64_DMA_CHUNKSIZE;
792 /* Save dword offset of last descriptor for this buffer.
793 * This is needed to check for completion of the buffer in freelist_get
795 entry->ring_ofs = RING_WRITE_OFS;
797 OUT_RING( MACH64_APERTURE_OFFSET + MACH64_BM_HOSTDATA );
799 OUT_RING( remainder | MACH64_DMA_HOLD_OFFSET | MACH64_DMA_EOL );
810 /*******************************************************************/
811 /** \name DMA test and initialization */
815 * Perform a simple DMA operation using the pattern registers to test whether
818 * \return zero if successful.
820 * \note This function was the testbed for many experiences regarding Mach64
821 * DMA operation. It is left here since it so tricky to get DMA operating
822 * properly in some architectures and hardware.
824 static int mach64_bm_dma_test(struct drm_device * dev)
826 drm_mach64_private_t *dev_priv = dev->dev_private;
827 drm_dma_handle_t *cpu_addr_dmah;
831 u32 src_cntl, pat_reg0, pat_reg1;
832 int i, count, failed;
836 table = (u32 *) dev_priv->ring.start;
838 /* FIXME: get a dma buffer from the freelist here */
839 DRM_DEBUG("Allocating data memory ...\n");
844 drm_pci_alloc(dev, 0x1000, 0x1000, 0xfffffffful);
848 if (!cpu_addr_dmah) {
849 DRM_INFO("data-memory allocation failed!\n");
852 data = (u32 *) cpu_addr_dmah->vaddr;
853 data_addr = (u32) cpu_addr_dmah->busaddr;
856 /* Save the X server's value for SRC_CNTL and restore it
857 * in case our test fails. This prevents the X server
858 * from disabling it's cache for this register
860 src_cntl = MACH64_READ(MACH64_SRC_CNTL);
861 pat_reg0 = MACH64_READ(MACH64_PAT_REG0);
862 pat_reg1 = MACH64_READ(MACH64_PAT_REG1);
864 mach64_do_wait_for_fifo(dev_priv, 3);
866 MACH64_WRITE(MACH64_SRC_CNTL, 0);
867 MACH64_WRITE(MACH64_PAT_REG0, 0x11111111);
868 MACH64_WRITE(MACH64_PAT_REG1, 0x11111111);
870 mach64_do_wait_for_idle(dev_priv);
872 for (i = 0; i < 2; i++) {
874 reg = MACH64_READ((MACH64_PAT_REG0 + i * 4));
875 DRM_DEBUG("(Before DMA Transfer) reg %d = 0x%08x\n", i, reg);
876 if (reg != 0x11111111) {
877 DRM_INFO("Error initializing test registers\n");
878 DRM_INFO("resetting engine ...\n");
879 mach64_do_engine_reset(dev_priv);
880 DRM_INFO("freeing data buffer memory.\n");
881 drm_pci_free(dev, cpu_addr_dmah);
886 /* fill up a buffer with sets of 2 consecutive writes starting with PAT_REG0 */
889 data[count++] = cpu_to_le32(DMAREG(MACH64_PAT_REG0) | (1 << 16));
890 data[count++] = expected[0] = 0x22222222;
891 data[count++] = expected[1] = 0xaaaaaaaa;
893 while (count < 1020) {
895 cpu_to_le32(DMAREG(MACH64_PAT_REG0) | (1 << 16));
896 data[count++] = 0x22222222;
897 data[count++] = 0xaaaaaaaa;
899 data[count++] = cpu_to_le32(DMAREG(MACH64_SRC_CNTL) | (0 << 16));
902 DRM_DEBUG("Preparing table ...\n");
903 table[MACH64_DMA_FRAME_BUF_OFFSET] = cpu_to_le32(MACH64_BM_ADDR +
904 MACH64_APERTURE_OFFSET);
905 table[MACH64_DMA_SYS_MEM_ADDR] = cpu_to_le32(data_addr);
906 table[MACH64_DMA_COMMAND] = cpu_to_le32(count * sizeof(u32)
907 | MACH64_DMA_HOLD_OFFSET
909 table[MACH64_DMA_RESERVED] = 0;
911 DRM_DEBUG("table[0] = 0x%08x\n", table[0]);
912 DRM_DEBUG("table[1] = 0x%08x\n", table[1]);
913 DRM_DEBUG("table[2] = 0x%08x\n", table[2]);
914 DRM_DEBUG("table[3] = 0x%08x\n", table[3]);
916 for (i = 0; i < 6; i++) {
917 DRM_DEBUG(" data[%d] = 0x%08x\n", i, data[i]);
920 for (i = count - 5; i < count; i++) {
921 DRM_DEBUG(" data[%d] = 0x%08x\n", i, data[i]);
926 DRM_DEBUG("waiting for idle...\n");
927 if ((i = mach64_do_wait_for_idle(dev_priv))) {
928 DRM_INFO("mach64_do_wait_for_idle failed (result=%d)\n", i);
929 DRM_INFO("resetting engine ...\n");
930 mach64_do_engine_reset(dev_priv);
931 mach64_do_wait_for_fifo(dev_priv, 3);
932 MACH64_WRITE(MACH64_SRC_CNTL, src_cntl);
933 MACH64_WRITE(MACH64_PAT_REG0, pat_reg0);
934 MACH64_WRITE(MACH64_PAT_REG1, pat_reg1);
935 DRM_INFO("freeing data buffer memory.\n");
936 drm_pci_free(dev, cpu_addr_dmah);
939 DRM_DEBUG("waiting for idle...done\n");
941 DRM_DEBUG("BUS_CNTL = 0x%08x\n", MACH64_READ(MACH64_BUS_CNTL));
942 DRM_DEBUG("SRC_CNTL = 0x%08x\n", MACH64_READ(MACH64_SRC_CNTL));
944 DRM_DEBUG("data bus addr = 0x%08x\n", data_addr);
945 DRM_DEBUG("table bus addr = 0x%08x\n", dev_priv->ring.start_addr);
947 DRM_DEBUG("starting DMA transfer...\n");
948 MACH64_WRITE(MACH64_BM_GUI_TABLE_CMD,
949 dev_priv->ring.start_addr | MACH64_CIRCULAR_BUF_SIZE_16KB);
951 MACH64_WRITE(MACH64_SRC_CNTL,
952 MACH64_SRC_BM_ENABLE | MACH64_SRC_BM_SYNC |
953 MACH64_SRC_BM_OP_SYSTEM_TO_REG);
955 /* Kick off the transfer */
956 DRM_DEBUG("starting DMA transfer... done.\n");
957 MACH64_WRITE(MACH64_DST_HEIGHT_WIDTH, 0);
959 DRM_DEBUG("waiting for idle...\n");
961 if ((i = mach64_do_wait_for_idle(dev_priv))) {
962 /* engine locked up, dump register state and reset */
963 DRM_INFO("mach64_do_wait_for_idle failed (result=%d)\n", i);
964 mach64_dump_engine_info(dev_priv);
965 DRM_INFO("resetting engine ...\n");
966 mach64_do_engine_reset(dev_priv);
967 mach64_do_wait_for_fifo(dev_priv, 3);
968 MACH64_WRITE(MACH64_SRC_CNTL, src_cntl);
969 MACH64_WRITE(MACH64_PAT_REG0, pat_reg0);
970 MACH64_WRITE(MACH64_PAT_REG1, pat_reg1);
971 DRM_INFO("freeing data buffer memory.\n");
972 drm_pci_free(dev, cpu_addr_dmah);
976 DRM_DEBUG("waiting for idle...done\n");
978 /* restore SRC_CNTL */
979 mach64_do_wait_for_fifo(dev_priv, 1);
980 MACH64_WRITE(MACH64_SRC_CNTL, src_cntl);
984 /* Check register values to see if the GUI master operation succeeded */
985 for (i = 0; i < 2; i++) {
987 reg = MACH64_READ((MACH64_PAT_REG0 + i * 4));
988 DRM_DEBUG("(After DMA Transfer) reg %d = 0x%08x\n", i, reg);
989 if (reg != expected[i]) {
994 /* restore pattern registers */
995 mach64_do_wait_for_fifo(dev_priv, 2);
996 MACH64_WRITE(MACH64_PAT_REG0, pat_reg0);
997 MACH64_WRITE(MACH64_PAT_REG1, pat_reg1);
999 DRM_DEBUG("freeing data buffer memory.\n");
1000 drm_pci_free(dev, cpu_addr_dmah);
1001 DRM_DEBUG("returning ...\n");
1007 * Called during the DMA initialization ioctl to initialize all the necessary
1008 * software and hardware state for DMA operation.
1010 static int mach64_do_dma_init(struct drm_device * dev, drm_mach64_init_t * init)
1012 drm_mach64_private_t *dev_priv;
1018 dev_priv = drm_alloc(sizeof(drm_mach64_private_t), DRM_MEM_DRIVER);
1019 if (dev_priv == NULL)
1022 memset(dev_priv, 0, sizeof(drm_mach64_private_t));
1024 dev_priv->is_pci = init->is_pci;
1026 dev_priv->fb_bpp = init->fb_bpp;
1027 dev_priv->front_offset = init->front_offset;
1028 dev_priv->front_pitch = init->front_pitch;
1029 dev_priv->back_offset = init->back_offset;
1030 dev_priv->back_pitch = init->back_pitch;
1032 dev_priv->depth_bpp = init->depth_bpp;
1033 dev_priv->depth_offset = init->depth_offset;
1034 dev_priv->depth_pitch = init->depth_pitch;
1036 dev_priv->front_offset_pitch = (((dev_priv->front_pitch / 8) << 22) |
1037 (dev_priv->front_offset >> 3));
1038 dev_priv->back_offset_pitch = (((dev_priv->back_pitch / 8) << 22) |
1039 (dev_priv->back_offset >> 3));
1040 dev_priv->depth_offset_pitch = (((dev_priv->depth_pitch / 8) << 22) |
1041 (dev_priv->depth_offset >> 3));
1043 dev_priv->usec_timeout = 1000000;
1045 /* Set up the freelist, placeholder list and pending list */
1046 INIT_LIST_HEAD(&dev_priv->free_list);
1047 INIT_LIST_HEAD(&dev_priv->placeholders);
1048 INIT_LIST_HEAD(&dev_priv->pending);
1050 dev_priv->sarea = drm_getsarea(dev);
1051 if (!dev_priv->sarea) {
1052 DRM_ERROR("can not find sarea!\n");
1053 dev->dev_private = (void *)dev_priv;
1054 mach64_do_cleanup_dma(dev);
1057 dev_priv->fb = drm_core_findmap(dev, init->fb_offset);
1058 if (!dev_priv->fb) {
1059 DRM_ERROR("can not find frame buffer map!\n");
1060 dev->dev_private = (void *)dev_priv;
1061 mach64_do_cleanup_dma(dev);
1064 dev_priv->mmio = drm_core_findmap(dev, init->mmio_offset);
1065 if (!dev_priv->mmio) {
1066 DRM_ERROR("can not find mmio map!\n");
1067 dev->dev_private = (void *)dev_priv;
1068 mach64_do_cleanup_dma(dev);
1072 dev_priv->ring_map = drm_core_findmap(dev, init->ring_offset);
1073 if (!dev_priv->ring_map) {
1074 DRM_ERROR("can not find ring map!\n");
1075 dev->dev_private = (void *)dev_priv;
1076 mach64_do_cleanup_dma(dev);
1080 dev_priv->sarea_priv = (drm_mach64_sarea_t *)
1081 ((u8 *) dev_priv->sarea->virtual + init->sarea_priv_offset);
1083 if (!dev_priv->is_pci) {
1084 drm_core_ioremap(dev_priv->ring_map, dev);
1085 if (!dev_priv->ring_map->virtual) {
1086 DRM_ERROR("can not ioremap virtual address for"
1087 " descriptor ring\n");
1088 dev->dev_private = (void *)dev_priv;
1089 mach64_do_cleanup_dma(dev);
1092 dev->agp_buffer_token = init->buffers_offset;
1093 dev->agp_buffer_map =
1094 drm_core_findmap(dev, init->buffers_offset);
1095 if (!dev->agp_buffer_map) {
1096 DRM_ERROR("can not find dma buffer map!\n");
1097 dev->dev_private = (void *)dev_priv;
1098 mach64_do_cleanup_dma(dev);
1101 /* there might be a nicer way to do this -
1102 dev isn't passed all the way though the mach64 - DA */
1103 dev_priv->dev_buffers = dev->agp_buffer_map;
1105 drm_core_ioremap(dev->agp_buffer_map, dev);
1106 if (!dev->agp_buffer_map->virtual) {
1107 DRM_ERROR("can not ioremap virtual address for"
1109 dev->dev_private = (void *)dev_priv;
1110 mach64_do_cleanup_dma(dev);
1113 dev_priv->agp_textures =
1114 drm_core_findmap(dev, init->agp_textures_offset);
1115 if (!dev_priv->agp_textures) {
1116 DRM_ERROR("can not find agp texture region!\n");
1117 dev->dev_private = (void *)dev_priv;
1118 mach64_do_cleanup_dma(dev);
1123 dev->dev_private = (void *)dev_priv;
1125 dev_priv->driver_mode = init->dma_mode;
1127 /* changing the FIFO size from the default causes problems with DMA */
1128 tmp = MACH64_READ(MACH64_GUI_CNTL);
1129 if ((tmp & MACH64_CMDFIFO_SIZE_MASK) != MACH64_CMDFIFO_SIZE_128) {
1130 DRM_INFO("Setting FIFO size to 128 entries\n");
1131 /* FIFO must be empty to change the FIFO depth */
1132 if ((ret = mach64_do_wait_for_idle(dev_priv))) {
1134 ("wait for idle failed before changing FIFO depth!\n");
1135 mach64_do_cleanup_dma(dev);
1138 MACH64_WRITE(MACH64_GUI_CNTL, ((tmp & ~MACH64_CMDFIFO_SIZE_MASK)
1139 | MACH64_CMDFIFO_SIZE_128));
1140 /* need to read GUI_STAT for proper sync according to docs */
1141 if ((ret = mach64_do_wait_for_idle(dev_priv))) {
1143 ("wait for idle failed when changing FIFO depth!\n");
1144 mach64_do_cleanup_dma(dev);
1149 dev_priv->ring.size = 0x4000; /* 16KB */
1150 dev_priv->ring.start = dev_priv->ring_map->virtual;
1151 dev_priv->ring.start_addr = (u32) dev_priv->ring_map->offset;
1153 memset(dev_priv->ring.start, 0, dev_priv->ring.size);
1154 DRM_INFO("descriptor ring: cpu addr %p, bus addr: 0x%08x\n",
1155 dev_priv->ring.start, dev_priv->ring.start_addr);
1158 if (dev_priv->driver_mode != MACH64_MODE_MMIO) {
1160 /* enable block 1 registers and bus mastering */
1161 MACH64_WRITE(MACH64_BUS_CNTL, ((MACH64_READ(MACH64_BUS_CNTL)
1162 | MACH64_BUS_EXT_REG_EN)
1163 & ~MACH64_BUS_MASTER_DIS));
1165 /* try a DMA GUI-mastering pass and fall back to MMIO if it fails */
1166 DRM_DEBUG("Starting DMA test...\n");
1167 if ((ret = mach64_bm_dma_test(dev))) {
1168 dev_priv->driver_mode = MACH64_MODE_MMIO;
1172 switch (dev_priv->driver_mode) {
1173 case MACH64_MODE_MMIO:
1174 MACH64_WRITE(MACH64_BUS_CNTL, (MACH64_READ(MACH64_BUS_CNTL)
1175 | MACH64_BUS_EXT_REG_EN
1176 | MACH64_BUS_MASTER_DIS));
1177 if (init->dma_mode == MACH64_MODE_MMIO)
1178 DRM_INFO("Forcing pseudo-DMA mode\n");
1181 ("DMA test failed (ret=%d), using pseudo-DMA mode\n",
1184 case MACH64_MODE_DMA_SYNC:
1185 DRM_INFO("DMA test succeeded, using synchronous DMA mode\n");
1187 case MACH64_MODE_DMA_ASYNC:
1189 DRM_INFO("DMA test succeeded, using asynchronous DMA mode\n");
1192 dev_priv->ring_running = 0;
1194 /* setup offsets for physical address of table start and end */
1195 dev_priv->ring.head_addr = dev_priv->ring.start_addr;
1196 dev_priv->ring.head = dev_priv->ring.tail = 0;
1197 dev_priv->ring.tail_mask = (dev_priv->ring.size / sizeof(u32)) - 1;
1198 dev_priv->ring.space = dev_priv->ring.size;
1200 /* setup physical address and size of descriptor table */
1201 mach64_do_wait_for_fifo(dev_priv, 1);
1202 MACH64_WRITE(MACH64_BM_GUI_TABLE_CMD,
1204 head_addr | MACH64_CIRCULAR_BUF_SIZE_16KB));
1206 /* init frame counter */
1207 dev_priv->sarea_priv->frames_queued = 0;
1208 for (i = 0; i < MACH64_MAX_QUEUED_FRAMES; i++) {
1209 dev_priv->frame_ofs[i] = ~0; /* All ones indicates placeholder */
1212 /* Allocate the DMA buffer freelist */
1213 if ((ret = mach64_init_freelist(dev))) {
1214 DRM_ERROR("Freelist allocation failed\n");
1215 mach64_do_cleanup_dma(dev);
1222 /*******************************************************************/
1223 /** MMIO Pseudo-DMA (intended primarily for debugging, not performance)
1226 int mach64_do_dispatch_pseudo_dma(drm_mach64_private_t *dev_priv)
1228 drm_mach64_descriptor_ring_t *ring = &dev_priv->ring;
1229 volatile u32 *ring_read;
1230 struct list_head *ptr;
1231 drm_mach64_freelist_t *entry;
1232 struct drm_buf *buf = NULL;
1234 u32 used, reg, target;
1235 int fifo, count, found, ret, no_idle_wait;
1237 fifo = count = reg = no_idle_wait = 0;
1238 target = MACH64_BM_ADDR;
1240 if ((ret = mach64_do_wait_for_idle(dev_priv)) < 0) {
1241 DRM_INFO("idle failed before pseudo-dma dispatch, resetting engine\n");
1242 mach64_dump_engine_info(dev_priv);
1243 mach64_do_engine_reset(dev_priv);
1247 ring_read = (u32 *) ring->start;
1249 while (ring->tail != ring->head) {
1250 u32 buf_addr, new_target, offset;
1251 u32 bytes, remaining, head, eol;
1256 le32_to_cpu(ring_read[head++]) - MACH64_APERTURE_OFFSET;
1257 buf_addr = le32_to_cpu(ring_read[head++]);
1258 eol = le32_to_cpu(ring_read[head]) & MACH64_DMA_EOL;
1259 bytes = le32_to_cpu(ring_read[head++])
1260 & ~(MACH64_DMA_HOLD_OFFSET | MACH64_DMA_EOL);
1262 head &= ring->tail_mask;
1264 /* can't wait for idle between a blit setup descriptor
1265 * and a HOSTDATA descriptor or the engine will lock
1267 if (new_target == MACH64_BM_HOSTDATA
1268 && target == MACH64_BM_ADDR)
1271 target = new_target;
1275 list_for_each(ptr, &dev_priv->pending) {
1276 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1278 offset = buf_addr - GETBUFADDR(buf);
1279 if (offset < MACH64_BUFFER_SIZE) {
1285 if (!found || buf == NULL) {
1287 ("Couldn't find pending buffer: head: %u tail: %u buf_addr: 0x%08x %s\n",
1288 head, ring->tail, buf_addr, (eol ? "eol" : ""));
1289 mach64_dump_ring_info(dev_priv);
1290 mach64_do_engine_reset(dev_priv);
1294 /* Hand feed the buffer to the card via MMIO, waiting for the fifo
1297 DRM_DEBUG("target: (0x%08x) %s\n", target,
1299 MACH64_BM_HOSTDATA ? "BM_HOSTDATA" : "BM_ADDR"));
1300 DRM_DEBUG("offset: %u bytes: %u used: %u\n", offset, bytes,
1303 remaining = (buf->used - offset) >> 2; /* dwords remaining in buffer */
1304 used = bytes >> 2; /* dwords in buffer for this descriptor */
1305 buf_ptr = (u32 *) ((char *)GETBUFPTR(buf) + offset);
1310 if (target == MACH64_BM_HOSTDATA) {
1311 reg = DMAREG(MACH64_HOST_DATA0);
1313 (remaining > 16) ? 16 : remaining;
1316 reg = le32_to_cpu(*buf_ptr++);
1318 count = (reg >> 16) + 1;
1322 reg = MMSELECT(reg);
1324 while (count && used) {
1328 mach64_do_wait_for_fifo
1329 (dev_priv, 16)) < 0) {
1335 mach64_do_wait_for_idle
1343 MACH64_WRITE(reg, le32_to_cpu(*buf_ptr++));
1352 ring->head_addr = ring->start_addr + (ring->head * sizeof(u32));
1353 ring->space += (4 * sizeof(u32));
1356 if ((ret = mach64_do_wait_for_idle(dev_priv)) < 0) {
1359 MACH64_WRITE(MACH64_BM_GUI_TABLE_CMD,
1360 ring->head_addr | MACH64_CIRCULAR_BUF_SIZE_16KB);
1362 DRM_DEBUG("completed\n");
1369 /*******************************************************************/
1370 /** \name DMA cleanup */
1373 int mach64_do_cleanup_dma(struct drm_device * dev)
1377 /* Make sure interrupts are disabled here because the uninstall ioctl
1378 * may not have been called from userspace and after dev_private
1379 * is freed, it's too late.
1382 drm_irq_uninstall(dev);
1384 if (dev->dev_private) {
1385 drm_mach64_private_t *dev_priv = dev->dev_private;
1387 if (!dev_priv->is_pci) {
1388 if (dev_priv->ring_map)
1389 drm_core_ioremapfree(dev_priv->ring_map, dev);
1391 if (dev->agp_buffer_map) {
1392 drm_core_ioremapfree(dev->agp_buffer_map, dev);
1393 dev->agp_buffer_map = NULL;
1397 mach64_destroy_freelist(dev);
1399 drm_free(dev_priv, sizeof(drm_mach64_private_t),
1401 dev->dev_private = NULL;
1410 /*******************************************************************/
1411 /** \name IOCTL handlers */
1414 int mach64_dma_init(struct drm_device *dev, void *data,
1415 struct drm_file *file_priv)
1417 drm_mach64_init_t *init = data;
1421 LOCK_TEST_WITH_RETURN(dev, file_priv);
1423 switch (init->func) {
1424 case DRM_MACH64_INIT_DMA:
1425 return mach64_do_dma_init(dev, init);
1426 case DRM_MACH64_CLEANUP_DMA:
1427 return mach64_do_cleanup_dma(dev);
1433 int mach64_dma_idle(struct drm_device *dev, void *data,
1434 struct drm_file *file_priv)
1436 drm_mach64_private_t *dev_priv = dev->dev_private;
1440 LOCK_TEST_WITH_RETURN(dev, file_priv);
1442 return mach64_do_dma_idle(dev_priv);
1445 int mach64_dma_flush(struct drm_device *dev, void *data,
1446 struct drm_file *file_priv)
1448 drm_mach64_private_t *dev_priv = dev->dev_private;
1452 LOCK_TEST_WITH_RETURN(dev, file_priv);
1454 return mach64_do_dma_flush(dev_priv);
1457 int mach64_engine_reset(struct drm_device *dev, void *data,
1458 struct drm_file *file_priv)
1460 drm_mach64_private_t *dev_priv = dev->dev_private;
1464 LOCK_TEST_WITH_RETURN(dev, file_priv);
1466 return mach64_do_engine_reset(dev_priv);
1472 /*******************************************************************/
1473 /** \name Freelist management */
1476 int mach64_init_freelist(struct drm_device * dev)
1478 struct drm_device_dma *dma = dev->dma;
1479 drm_mach64_private_t *dev_priv = dev->dev_private;
1480 drm_mach64_freelist_t *entry;
1481 struct list_head *ptr;
1484 DRM_DEBUG("adding %d buffers to freelist\n", dma->buf_count);
1486 for (i = 0; i < dma->buf_count; i++) {
1488 (drm_mach64_freelist_t *)
1489 drm_alloc(sizeof(drm_mach64_freelist_t),
1490 DRM_MEM_BUFLISTS)) == NULL)
1492 memset(entry, 0, sizeof(drm_mach64_freelist_t));
1493 entry->buf = dma->buflist[i];
1495 list_add_tail(ptr, &dev_priv->free_list);
1501 void mach64_destroy_freelist(struct drm_device * dev)
1503 drm_mach64_private_t *dev_priv = dev->dev_private;
1504 drm_mach64_freelist_t *entry;
1505 struct list_head *ptr;
1506 struct list_head *tmp;
1510 list_for_each_safe(ptr, tmp, &dev_priv->pending) {
1512 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1513 drm_free(entry, sizeof(*entry), DRM_MEM_BUFLISTS);
1515 list_for_each_safe(ptr, tmp, &dev_priv->placeholders) {
1517 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1518 drm_free(entry, sizeof(*entry), DRM_MEM_BUFLISTS);
1521 list_for_each_safe(ptr, tmp, &dev_priv->free_list) {
1523 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1524 drm_free(entry, sizeof(*entry), DRM_MEM_BUFLISTS);
1528 /* IMPORTANT: This function should only be called when the engine is idle or locked up,
1529 * as it assumes all buffers in the pending list have been completed by the hardware.
1531 int mach64_do_release_used_buffers(drm_mach64_private_t *dev_priv)
1533 struct list_head *ptr;
1534 struct list_head *tmp;
1535 drm_mach64_freelist_t *entry;
1538 if (list_empty(&dev_priv->pending))
1541 /* Iterate the pending list and move all buffers into the freelist... */
1543 list_for_each_safe(ptr, tmp, &dev_priv->pending) {
1544 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1545 if (entry->discard) {
1546 entry->buf->pending = 0;
1548 list_add_tail(ptr, &dev_priv->free_list);
1553 DRM_DEBUG("released %d buffers from pending list\n", i);
1558 static int mach64_do_reclaim_completed(drm_mach64_private_t *dev_priv)
1560 drm_mach64_descriptor_ring_t *ring = &dev_priv->ring;
1561 struct list_head *ptr;
1562 struct list_head *tmp;
1563 drm_mach64_freelist_t *entry;
1564 u32 head, tail, ofs;
1566 mach64_ring_tick(dev_priv, ring);
1571 #if MACH64_EXTRA_CHECKING
1572 if (MACH64_READ(MACH64_GUI_STAT) & MACH64_GUI_ACTIVE) {
1573 DRM_ERROR("Empty ring with non-idle engine!\n");
1574 mach64_dump_ring_info(dev_priv);
1578 /* last pass is complete, so release everything */
1579 mach64_do_release_used_buffers(dev_priv);
1580 DRM_DEBUG("idle engine, freed all buffers.\n");
1581 if (list_empty(&dev_priv->free_list)) {
1582 DRM_ERROR("Freelist empty with idle engine\n");
1587 /* Look for a completed buffer and bail out of the loop
1588 * as soon as we find one -- don't waste time trying
1589 * to free extra bufs here, leave that to do_release_used_buffers
1591 list_for_each_safe(ptr, tmp, &dev_priv->pending) {
1592 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1593 ofs = entry->ring_ofs;
1594 if (entry->discard &&
1595 ((head < tail && (ofs < head || ofs >= tail)) ||
1596 (head > tail && (ofs < head && ofs >= tail)))) {
1597 #if MACH64_EXTRA_CHECKING
1600 for (i = head; i != tail; i = (i + 4) & ring->tail_mask)
1602 u32 o1 = le32_to_cpu(((u32 *) ring->
1604 u32 o2 = GETBUFADDR(entry->buf);
1608 ("Attempting to free used buffer: "
1609 "i=%d buf=0x%08x\n",
1611 mach64_dump_ring_info(dev_priv);
1616 /* found a processed buffer */
1617 entry->buf->pending = 0;
1619 list_add_tail(ptr, &dev_priv->free_list);
1621 ("freed processed buffer (head=%d tail=%d "
1622 "buf ring ofs=%d).\n",
1631 struct drm_buf *mach64_freelist_get(drm_mach64_private_t *dev_priv)
1633 drm_mach64_descriptor_ring_t *ring = &dev_priv->ring;
1634 drm_mach64_freelist_t *entry;
1635 struct list_head *ptr;
1638 if (list_empty(&dev_priv->free_list)) {
1639 if (list_empty(&dev_priv->pending)) {
1641 ("Couldn't get buffer - pending and free lists empty\n");
1643 list_for_each(ptr, &dev_priv->placeholders) {
1646 DRM_INFO("Placeholders: %d\n", t);
1650 for (t = 0; t < dev_priv->usec_timeout; t++) {
1653 ret = mach64_do_reclaim_completed(dev_priv);
1655 goto _freelist_entry_found;
1661 mach64_dump_ring_info(dev_priv);
1663 ("timeout waiting for buffers: ring head_addr: 0x%08x head: %d tail: %d\n",
1664 ring->head_addr, ring->head, ring->tail);
1668 _freelist_entry_found:
1669 ptr = dev_priv->free_list.next;
1671 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1672 entry->buf->used = 0;
1673 list_add_tail(ptr, &dev_priv->placeholders);
1677 int mach64_freelist_put(drm_mach64_private_t *dev_priv, struct drm_buf *copy_buf)
1679 struct list_head *ptr;
1680 drm_mach64_freelist_t *entry;
1682 #if MACH64_EXTRA_CHECKING
1683 list_for_each(ptr, &dev_priv->pending) {
1684 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1685 if (copy_buf == entry->buf) {
1686 DRM_ERROR("Trying to release a pending buf\n");
1691 ptr = dev_priv->placeholders.next;
1692 entry = list_entry(ptr, drm_mach64_freelist_t, list);
1693 copy_buf->pending = 0;
1695 entry->buf = copy_buf;
1698 list_add_tail(ptr, &dev_priv->free_list);
1706 /*******************************************************************/
1707 /** \name DMA buffer request and submission IOCTL handler */
1710 static int mach64_dma_get_buffers(struct drm_device *dev,
1711 struct drm_file *file_priv,
1715 struct drm_buf *buf;
1716 drm_mach64_private_t *dev_priv = dev->dev_private;
1718 for (i = d->granted_count; i < d->request_count; i++) {
1719 buf = mach64_freelist_get(dev_priv);
1720 #if MACH64_EXTRA_CHECKING
1728 buf->file_priv = file_priv;
1730 if (DRM_COPY_TO_USER(&d->request_indices[i], &buf->idx,
1733 if (DRM_COPY_TO_USER(&d->request_sizes[i], &buf->total,
1734 sizeof(buf->total)))
1742 int mach64_dma_buffers(struct drm_device *dev, void *data,
1743 struct drm_file *file_priv)
1745 struct drm_device_dma *dma = dev->dma;
1746 struct drm_dma *d = data;
1749 LOCK_TEST_WITH_RETURN(dev, file_priv);
1751 /* Please don't send us buffers.
1753 if (d->send_count != 0) {
1754 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
1755 DRM_CURRENTPID, d->send_count);
1759 /* We'll send you buffers.
1761 if (d->request_count < 0 || d->request_count > dma->buf_count) {
1762 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
1763 DRM_CURRENTPID, d->request_count, dma->buf_count);
1767 d->granted_count = 0;
1769 if (d->request_count) {
1770 ret = mach64_dma_get_buffers(dev, file_priv, d);
1776 void mach64_driver_lastclose(struct drm_device * dev)
1778 mach64_do_cleanup_dma(dev);