Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 1 | /* via_dma.c -- DMA support for the VIA Unichrome/Pro |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 2 | * |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 3 | * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas. |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Copyright 2004 Digeo, Inc., Palo Alto, CA, U.S.A. |
| 7 | * All Rights Reserved. |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 8 | * |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 9 | * Copyright 2004 The Unichrome project. |
| 10 | * All Rights Reserved. |
| 11 | * |
| 12 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 13 | * copy of this software and associated documentation files (the "Software"), |
| 14 | * to deal in the Software without restriction, including without limitation |
| 15 | * the rights to use, copy, modify, merge, publish, distribute, sub license, |
| 16 | * and/or sell copies of the Software, and to permit persons to whom the |
| 17 | * Software is furnished to do so, subject to the following conditions: |
| 18 | * |
| 19 | * The above copyright notice and this permission notice (including the |
| 20 | * next paragraph) shall be included in all copies or substantial portions |
| 21 | * of the Software. |
| 22 | * |
| 23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 26 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 27 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 28 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 29 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 30 | * |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 31 | * Authors: |
| 32 | * Tungsten Graphics, |
| 33 | * Erdi Chen, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 34 | * Thomas Hellstrom. |
| 35 | */ |
| 36 | |
| 37 | #include "drmP.h" |
| 38 | #include "drm.h" |
| 39 | #include "via_drm.h" |
| 40 | #include "via_drv.h" |
| 41 | #include "via_3d_reg.h" |
| 42 | |
| 43 | #define CMDBUF_ALIGNMENT_SIZE (0x100) |
| 44 | #define CMDBUF_ALIGNMENT_MASK (0x0ff) |
| 45 | |
| 46 | /* defines for VIA 3D registers */ |
| 47 | #define VIA_REG_STATUS 0x400 |
| 48 | #define VIA_REG_TRANSET 0x43C |
| 49 | #define VIA_REG_TRANSPACE 0x440 |
| 50 | |
| 51 | /* VIA_REG_STATUS(0x400): Engine Status */ |
| 52 | #define VIA_CMD_RGTR_BUSY 0x00000080 /* Command Regulator is busy */ |
| 53 | #define VIA_2D_ENG_BUSY 0x00000001 /* 2D Engine is busy */ |
| 54 | #define VIA_3D_ENG_BUSY 0x00000002 /* 3D Engine is busy */ |
| 55 | #define VIA_VR_QUEUE_BUSY 0x00020000 /* Virtual Queue is busy */ |
| 56 | |
| 57 | #define SetReg2DAGP(nReg, nData) { \ |
| 58 | *((uint32_t *)(vb)) = ((nReg) >> 2) | HALCYON_HEADER1; \ |
| 59 | *((uint32_t *)(vb) + 1) = (nData); \ |
| 60 | vb = ((uint32_t *)vb) + 2; \ |
| 61 | dev_priv->dma_low +=8; \ |
| 62 | } |
| 63 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 64 | #define via_flush_write_combine() DRM_MEMORYBARRIER() |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 65 | |
| 66 | #define VIA_OUT_RING_QW(w1,w2) \ |
| 67 | *vb++ = (w1); \ |
| 68 | *vb++ = (w2); \ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 69 | dev_priv->dma_low += 8; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 70 | |
| 71 | static void via_cmdbuf_start(drm_via_private_t * dev_priv); |
| 72 | static void via_cmdbuf_pause(drm_via_private_t * dev_priv); |
| 73 | static void via_cmdbuf_reset(drm_via_private_t * dev_priv); |
| 74 | static void via_cmdbuf_rewind(drm_via_private_t * dev_priv); |
| 75 | static int via_wait_idle(drm_via_private_t * dev_priv); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 76 | static void via_pad_cache(drm_via_private_t * dev_priv, int qwords); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 77 | |
| 78 | /* |
| 79 | * Free space in command buffer. |
| 80 | */ |
| 81 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 82 | static uint32_t via_cmdbuf_space(drm_via_private_t * dev_priv) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 83 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 84 | uint32_t agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 85 | uint32_t hw_addr = *(dev_priv->hw_addr_ptr) - agp_base; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 86 | |
| 87 | return ((hw_addr <= dev_priv->dma_low) ? |
| 88 | (dev_priv->dma_high + hw_addr - dev_priv->dma_low) : |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 89 | (hw_addr - dev_priv->dma_low)); |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * How much does the command regulator lag behind? |
| 94 | */ |
| 95 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 96 | static uint32_t via_cmdbuf_lag(drm_via_private_t * dev_priv) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 97 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 98 | uint32_t agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 99 | uint32_t hw_addr = *(dev_priv->hw_addr_ptr) - agp_base; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 100 | |
| 101 | return ((hw_addr <= dev_priv->dma_low) ? |
| 102 | (dev_priv->dma_low - hw_addr) : |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 103 | (dev_priv->dma_wrap + dev_priv->dma_low - hw_addr)); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Check that the given size fits in the buffer, otherwise wait. |
| 108 | */ |
| 109 | |
| 110 | static inline int |
| 111 | via_cmdbuf_wait(drm_via_private_t * dev_priv, unsigned int size) |
| 112 | { |
| 113 | uint32_t agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr; |
| 114 | uint32_t cur_addr, hw_addr, next_addr; |
| 115 | volatile uint32_t *hw_addr_ptr; |
| 116 | uint32_t count; |
| 117 | hw_addr_ptr = dev_priv->hw_addr_ptr; |
| 118 | cur_addr = dev_priv->dma_low; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 119 | next_addr = cur_addr + size + 512 * 1024; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 120 | count = 1000000; |
| 121 | do { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 122 | hw_addr = *hw_addr_ptr - agp_base; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 123 | if (count-- == 0) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 124 | DRM_ERROR |
| 125 | ("via_cmdbuf_wait timed out hw %x cur_addr %x next_addr %x\n", |
| 126 | hw_addr, cur_addr, next_addr); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 127 | return -1; |
| 128 | } |
| 129 | } while ((cur_addr < hw_addr) && (next_addr >= hw_addr)); |
| 130 | return 0; |
| 131 | } |
| 132 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 133 | /* |
| 134 | * Checks whether buffer head has reach the end. Rewind the ring buffer |
| 135 | * when necessary. |
| 136 | * |
| 137 | * Returns virtual pointer to ring buffer. |
| 138 | */ |
| 139 | |
| 140 | static inline uint32_t *via_check_dma(drm_via_private_t * dev_priv, |
| 141 | unsigned int size) |
| 142 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 143 | if ((dev_priv->dma_low + size + 4 * CMDBUF_ALIGNMENT_SIZE) > |
| 144 | dev_priv->dma_high) { |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 145 | via_cmdbuf_rewind(dev_priv); |
| 146 | } |
| 147 | if (via_cmdbuf_wait(dev_priv, size) != 0) { |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | return (uint32_t *) (dev_priv->dma_ptr + dev_priv->dma_low); |
| 152 | } |
| 153 | |
| 154 | int via_dma_cleanup(drm_device_t * dev) |
| 155 | { |
| 156 | if (dev->dev_private) { |
| 157 | drm_via_private_t *dev_priv = |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 158 | (drm_via_private_t *) dev->dev_private; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 159 | |
| 160 | if (dev_priv->ring.virtual_start) { |
| 161 | via_cmdbuf_reset(dev_priv); |
| 162 | |
| 163 | drm_core_ioremapfree(&dev_priv->ring.map, dev); |
| 164 | dev_priv->ring.virtual_start = NULL; |
| 165 | } |
| 166 | |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int via_initialize(drm_device_t * dev, |
| 173 | drm_via_private_t * dev_priv, |
| 174 | drm_via_dma_init_t * init) |
| 175 | { |
| 176 | if (!dev_priv || !dev_priv->mmio) { |
| 177 | DRM_ERROR("via_dma_init called before via_map_init\n"); |
| 178 | return DRM_ERR(EFAULT); |
| 179 | } |
| 180 | |
| 181 | if (dev_priv->ring.virtual_start != NULL) { |
| 182 | DRM_ERROR("%s called again without calling cleanup\n", |
| 183 | __FUNCTION__); |
| 184 | return DRM_ERR(EFAULT); |
| 185 | } |
| 186 | |
| 187 | if (!dev->agp || !dev->agp->base) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 188 | DRM_ERROR("%s called with no agp memory available\n", |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 189 | __FUNCTION__); |
| 190 | return DRM_ERR(EFAULT); |
| 191 | } |
| 192 | |
| 193 | dev_priv->ring.map.offset = dev->agp->base + init->offset; |
| 194 | dev_priv->ring.map.size = init->size; |
| 195 | dev_priv->ring.map.type = 0; |
| 196 | dev_priv->ring.map.flags = 0; |
| 197 | dev_priv->ring.map.mtrr = 0; |
| 198 | |
| 199 | drm_core_ioremap(&dev_priv->ring.map, dev); |
| 200 | |
| 201 | if (dev_priv->ring.map.handle == NULL) { |
| 202 | via_dma_cleanup(dev); |
| 203 | DRM_ERROR("can not ioremap virtual address for" |
| 204 | " ring buffer\n"); |
| 205 | return DRM_ERR(ENOMEM); |
| 206 | } |
| 207 | |
| 208 | dev_priv->ring.virtual_start = dev_priv->ring.map.handle; |
| 209 | |
| 210 | dev_priv->dma_ptr = dev_priv->ring.virtual_start; |
| 211 | dev_priv->dma_low = 0; |
| 212 | dev_priv->dma_high = init->size; |
| 213 | dev_priv->dma_wrap = init->size; |
| 214 | dev_priv->dma_offset = init->offset; |
| 215 | dev_priv->last_pause_ptr = NULL; |
Dave Airlie | 9251424 | 2005-11-12 21:52:46 +1100 | [diff] [blame] | 216 | dev_priv->hw_addr_ptr = |
| 217 | (volatile uint32_t *)((char *)dev_priv->mmio->handle + |
| 218 | init->reg_pause_addr); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 219 | |
| 220 | via_cmdbuf_start(dev_priv); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Dave Airlie | ce60fe0 | 2006-02-02 19:21:38 +1100 | [diff] [blame] | 225 | static int via_dma_init(DRM_IOCTL_ARGS) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 226 | { |
| 227 | DRM_DEVICE; |
| 228 | drm_via_private_t *dev_priv = (drm_via_private_t *) dev->dev_private; |
| 229 | drm_via_dma_init_t init; |
| 230 | int retcode = 0; |
| 231 | |
Alexey Dobriyan | bbaf364 | 2005-07-27 11:43:56 -0700 | [diff] [blame] | 232 | DRM_COPY_FROM_USER_IOCTL(init, (drm_via_dma_init_t __user *) data, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 233 | sizeof(init)); |
| 234 | |
| 235 | switch (init.func) { |
| 236 | case VIA_INIT_DMA: |
Dave Airlie | 9251424 | 2005-11-12 21:52:46 +1100 | [diff] [blame] | 237 | if (!DRM_SUSER(DRM_CURPROC)) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 238 | retcode = DRM_ERR(EPERM); |
| 239 | else |
| 240 | retcode = via_initialize(dev, dev_priv, &init); |
| 241 | break; |
| 242 | case VIA_CLEANUP_DMA: |
Dave Airlie | 9251424 | 2005-11-12 21:52:46 +1100 | [diff] [blame] | 243 | if (!DRM_SUSER(DRM_CURPROC)) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 244 | retcode = DRM_ERR(EPERM); |
| 245 | else |
| 246 | retcode = via_dma_cleanup(dev); |
| 247 | break; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 248 | case VIA_DMA_INITIALIZED: |
| 249 | retcode = (dev_priv->ring.virtual_start != NULL) ? |
| 250 | 0 : DRM_ERR(EFAULT); |
| 251 | break; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 252 | default: |
| 253 | retcode = DRM_ERR(EINVAL); |
| 254 | break; |
| 255 | } |
| 256 | |
| 257 | return retcode; |
| 258 | } |
| 259 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 260 | static int via_dispatch_cmdbuffer(drm_device_t * dev, drm_via_cmdbuffer_t * cmd) |
| 261 | { |
| 262 | drm_via_private_t *dev_priv; |
| 263 | uint32_t *vb; |
| 264 | int ret; |
| 265 | |
| 266 | dev_priv = (drm_via_private_t *) dev->dev_private; |
| 267 | |
| 268 | if (dev_priv->ring.virtual_start == NULL) { |
| 269 | DRM_ERROR("%s called without initializing AGP ring buffer.\n", |
| 270 | __FUNCTION__); |
| 271 | return DRM_ERR(EFAULT); |
| 272 | } |
| 273 | |
| 274 | if (cmd->size > VIA_PCI_BUF_SIZE) { |
| 275 | return DRM_ERR(ENOMEM); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 276 | } |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 277 | |
| 278 | if (DRM_COPY_FROM_USER(dev_priv->pci_buf, cmd->buf, cmd->size)) |
| 279 | return DRM_ERR(EFAULT); |
| 280 | |
| 281 | /* |
| 282 | * Running this function on AGP memory is dead slow. Therefore |
| 283 | * we run it on a temporary cacheable system memory buffer and |
| 284 | * copy it to AGP memory when ready. |
| 285 | */ |
| 286 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 287 | if ((ret = |
| 288 | via_verify_command_stream((uint32_t *) dev_priv->pci_buf, |
| 289 | cmd->size, dev, 1))) { |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 290 | return ret; |
| 291 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 292 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 293 | vb = via_check_dma(dev_priv, (cmd->size < 0x100) ? 0x102 : cmd->size); |
| 294 | if (vb == NULL) { |
| 295 | return DRM_ERR(EAGAIN); |
| 296 | } |
| 297 | |
| 298 | memcpy(vb, dev_priv->pci_buf, cmd->size); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 299 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 300 | dev_priv->dma_low += cmd->size; |
| 301 | |
| 302 | /* |
| 303 | * Small submissions somehow stalls the CPU. (AGP cache effects?) |
| 304 | * pad to greater size. |
| 305 | */ |
| 306 | |
| 307 | if (cmd->size < 0x100) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 308 | via_pad_cache(dev_priv, (0x100 - cmd->size) >> 3); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 309 | via_cmdbuf_pause(dev_priv); |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
| 314 | int via_driver_dma_quiescent(drm_device_t * dev) |
| 315 | { |
| 316 | drm_via_private_t *dev_priv = dev->dev_private; |
| 317 | |
| 318 | if (!via_wait_idle(dev_priv)) { |
| 319 | return DRM_ERR(EBUSY); |
| 320 | } |
| 321 | return 0; |
| 322 | } |
| 323 | |
Dave Airlie | ce60fe0 | 2006-02-02 19:21:38 +1100 | [diff] [blame] | 324 | static int via_flush_ioctl(DRM_IOCTL_ARGS) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 325 | { |
| 326 | DRM_DEVICE; |
| 327 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 328 | LOCK_TEST_WITH_RETURN(dev, filp); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 329 | |
| 330 | return via_driver_dma_quiescent(dev); |
| 331 | } |
| 332 | |
Dave Airlie | ce60fe0 | 2006-02-02 19:21:38 +1100 | [diff] [blame] | 333 | static int via_cmdbuffer(DRM_IOCTL_ARGS) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 334 | { |
| 335 | DRM_DEVICE; |
| 336 | drm_via_cmdbuffer_t cmdbuf; |
| 337 | int ret; |
| 338 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 339 | LOCK_TEST_WITH_RETURN(dev, filp); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 340 | |
Alexey Dobriyan | bbaf364 | 2005-07-27 11:43:56 -0700 | [diff] [blame] | 341 | DRM_COPY_FROM_USER_IOCTL(cmdbuf, (drm_via_cmdbuffer_t __user *) data, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 342 | sizeof(cmdbuf)); |
| 343 | |
| 344 | DRM_DEBUG("via cmdbuffer, buf %p size %lu\n", cmdbuf.buf, cmdbuf.size); |
| 345 | |
| 346 | ret = via_dispatch_cmdbuffer(dev, &cmdbuf); |
| 347 | if (ret) { |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 354 | static int via_dispatch_pci_cmdbuffer(drm_device_t * dev, |
| 355 | drm_via_cmdbuffer_t * cmd) |
| 356 | { |
| 357 | drm_via_private_t *dev_priv = dev->dev_private; |
| 358 | int ret; |
| 359 | |
| 360 | if (cmd->size > VIA_PCI_BUF_SIZE) { |
| 361 | return DRM_ERR(ENOMEM); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 362 | } |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 363 | if (DRM_COPY_FROM_USER(dev_priv->pci_buf, cmd->buf, cmd->size)) |
| 364 | return DRM_ERR(EFAULT); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 365 | |
| 366 | if ((ret = |
| 367 | via_verify_command_stream((uint32_t *) dev_priv->pci_buf, |
| 368 | cmd->size, dev, 0))) { |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 369 | return ret; |
| 370 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 371 | |
| 372 | ret = |
| 373 | via_parse_command_stream(dev, (const uint32_t *)dev_priv->pci_buf, |
| 374 | cmd->size); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 375 | return ret; |
| 376 | } |
| 377 | |
Dave Airlie | ce60fe0 | 2006-02-02 19:21:38 +1100 | [diff] [blame] | 378 | static int via_pci_cmdbuffer(DRM_IOCTL_ARGS) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 379 | { |
| 380 | DRM_DEVICE; |
| 381 | drm_via_cmdbuffer_t cmdbuf; |
| 382 | int ret; |
| 383 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 384 | LOCK_TEST_WITH_RETURN(dev, filp); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 385 | |
Alexey Dobriyan | bbaf364 | 2005-07-27 11:43:56 -0700 | [diff] [blame] | 386 | DRM_COPY_FROM_USER_IOCTL(cmdbuf, (drm_via_cmdbuffer_t __user *) data, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 387 | sizeof(cmdbuf)); |
| 388 | |
| 389 | DRM_DEBUG("via_pci_cmdbuffer, buf %p size %lu\n", cmdbuf.buf, |
| 390 | cmdbuf.size); |
| 391 | |
| 392 | ret = via_dispatch_pci_cmdbuffer(dev, &cmdbuf); |
| 393 | if (ret) { |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | return 0; |
| 398 | } |
| 399 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 400 | static inline uint32_t *via_align_buffer(drm_via_private_t * dev_priv, |
| 401 | uint32_t * vb, int qw_count) |
| 402 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 403 | for (; qw_count > 0; --qw_count) { |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 404 | VIA_OUT_RING_QW(HC_DUMMY, HC_DUMMY); |
| 405 | } |
| 406 | return vb; |
| 407 | } |
| 408 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 409 | /* |
| 410 | * This function is used internally by ring buffer mangement code. |
| 411 | * |
| 412 | * Returns virtual pointer to ring buffer. |
| 413 | */ |
| 414 | static inline uint32_t *via_get_dma(drm_via_private_t * dev_priv) |
| 415 | { |
| 416 | return (uint32_t *) (dev_priv->dma_ptr + dev_priv->dma_low); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Hooks a segment of data into the tail of the ring-buffer by |
| 421 | * modifying the pause address stored in the buffer itself. If |
| 422 | * the regulator has already paused, restart it. |
| 423 | */ |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 424 | static int via_hook_segment(drm_via_private_t * dev_priv, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 425 | uint32_t pause_addr_hi, uint32_t pause_addr_lo, |
| 426 | int no_pci_fire) |
| 427 | { |
| 428 | int paused, count; |
| 429 | volatile uint32_t *paused_at = dev_priv->last_pause_ptr; |
| 430 | |
| 431 | via_flush_write_combine(); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 432 | while (!*(via_get_dma(dev_priv) - 1)) ; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 433 | *dev_priv->last_pause_ptr = pause_addr_lo; |
| 434 | via_flush_write_combine(); |
| 435 | |
| 436 | /* |
| 437 | * The below statement is inserted to really force the flush. |
| 438 | * Not sure it is needed. |
| 439 | */ |
| 440 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 441 | while (!*dev_priv->last_pause_ptr) ; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 442 | dev_priv->last_pause_ptr = via_get_dma(dev_priv) - 1; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 443 | while (!*dev_priv->last_pause_ptr) ; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 444 | |
| 445 | paused = 0; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 446 | count = 20; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 447 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 448 | while (!(paused = (VIA_READ(0x41c) & 0x80000000)) && count--) ; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 449 | if ((count <= 8) && (count >= 0)) { |
| 450 | uint32_t rgtr, ptr; |
| 451 | rgtr = *(dev_priv->hw_addr_ptr); |
Dave Airlie | 9251424 | 2005-11-12 21:52:46 +1100 | [diff] [blame] | 452 | ptr = ((volatile char *)dev_priv->last_pause_ptr - |
| 453 | dev_priv->dma_ptr) + dev_priv->dma_offset + |
| 454 | (uint32_t) dev_priv->agpAddr + 4 - CMDBUF_ALIGNMENT_SIZE; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 455 | if (rgtr <= ptr) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 456 | DRM_ERROR |
| 457 | ("Command regulator\npaused at count %d, address %x, " |
| 458 | "while current pause address is %x.\n" |
| 459 | "Please mail this message to " |
| 460 | "<unichrome-devel@lists.sourceforge.net>\n", count, |
| 461 | rgtr, ptr); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 462 | } |
| 463 | } |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 464 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 465 | if (paused && !no_pci_fire) { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 466 | uint32_t rgtr, ptr; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 467 | uint32_t ptr_low; |
| 468 | |
| 469 | count = 1000000; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 470 | while ((VIA_READ(VIA_REG_STATUS) & VIA_CMD_RGTR_BUSY) |
| 471 | && count--) ; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 472 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 473 | rgtr = *(dev_priv->hw_addr_ptr); |
Dave Airlie | 9251424 | 2005-11-12 21:52:46 +1100 | [diff] [blame] | 474 | ptr = ((volatile char *)paused_at - dev_priv->dma_ptr) + |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 475 | dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr + 4; |
| 476 | |
| 477 | ptr_low = (ptr > 3 * CMDBUF_ALIGNMENT_SIZE) ? |
| 478 | ptr - 3 * CMDBUF_ALIGNMENT_SIZE : 0; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 479 | if (rgtr <= ptr && rgtr >= ptr_low) { |
| 480 | VIA_WRITE(VIA_REG_TRANSET, (HC_ParaType_PreCR << 16)); |
| 481 | VIA_WRITE(VIA_REG_TRANSPACE, pause_addr_hi); |
| 482 | VIA_WRITE(VIA_REG_TRANSPACE, pause_addr_lo); |
Thomas Hellstrom | 76f6255 | 2007-01-08 21:03:23 +1100 | [diff] [blame^] | 483 | VIA_READ(VIA_REG_TRANSPACE); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 484 | } |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 485 | } |
| 486 | return paused; |
| 487 | } |
| 488 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 489 | static int via_wait_idle(drm_via_private_t * dev_priv) |
| 490 | { |
| 491 | int count = 10000000; |
| 492 | while (count-- && (VIA_READ(VIA_REG_STATUS) & |
| 493 | (VIA_CMD_RGTR_BUSY | VIA_2D_ENG_BUSY | |
| 494 | VIA_3D_ENG_BUSY))) ; |
| 495 | return count; |
| 496 | } |
| 497 | |
| 498 | static uint32_t *via_align_cmd(drm_via_private_t * dev_priv, uint32_t cmd_type, |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 499 | uint32_t addr, uint32_t * cmd_addr_hi, |
| 500 | uint32_t * cmd_addr_lo, int skip_wait) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 501 | { |
| 502 | uint32_t agp_base; |
| 503 | uint32_t cmd_addr, addr_lo, addr_hi; |
| 504 | uint32_t *vb; |
| 505 | uint32_t qw_pad_count; |
| 506 | |
| 507 | if (!skip_wait) |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 508 | via_cmdbuf_wait(dev_priv, 2 * CMDBUF_ALIGNMENT_SIZE); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 509 | |
| 510 | vb = via_get_dma(dev_priv); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 511 | VIA_OUT_RING_QW(HC_HEADER2 | ((VIA_REG_TRANSET >> 2) << 12) | |
| 512 | (VIA_REG_TRANSPACE >> 2), HC_ParaType_PreCR << 16); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 513 | agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr; |
| 514 | qw_pad_count = (CMDBUF_ALIGNMENT_SIZE >> 3) - |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 515 | ((dev_priv->dma_low & CMDBUF_ALIGNMENT_MASK) >> 3); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 516 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 517 | cmd_addr = (addr) ? addr : |
| 518 | agp_base + dev_priv->dma_low - 8 + (qw_pad_count << 3); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 519 | addr_lo = ((HC_SubA_HAGPBpL << 24) | (cmd_type & HC_HAGPBpID_MASK) | |
| 520 | (cmd_addr & HC_HAGPBpL_MASK)); |
| 521 | addr_hi = ((HC_SubA_HAGPBpH << 24) | (cmd_addr >> 24)); |
| 522 | |
| 523 | vb = via_align_buffer(dev_priv, vb, qw_pad_count - 1); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 524 | VIA_OUT_RING_QW(*cmd_addr_hi = addr_hi, *cmd_addr_lo = addr_lo); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 525 | return vb; |
| 526 | } |
| 527 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 528 | static void via_cmdbuf_start(drm_via_private_t * dev_priv) |
| 529 | { |
| 530 | uint32_t pause_addr_lo, pause_addr_hi; |
| 531 | uint32_t start_addr, start_addr_lo; |
| 532 | uint32_t end_addr, end_addr_lo; |
| 533 | uint32_t command; |
| 534 | uint32_t agp_base; |
| 535 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 536 | dev_priv->dma_low = 0; |
| 537 | |
| 538 | agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr; |
| 539 | start_addr = agp_base; |
| 540 | end_addr = agp_base + dev_priv->dma_high; |
| 541 | |
| 542 | start_addr_lo = ((HC_SubA_HAGPBstL << 24) | (start_addr & 0xFFFFFF)); |
| 543 | end_addr_lo = ((HC_SubA_HAGPBendL << 24) | (end_addr & 0xFFFFFF)); |
| 544 | command = ((HC_SubA_HAGPCMNT << 24) | (start_addr >> 24) | |
| 545 | ((end_addr & 0xff000000) >> 16)); |
| 546 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 547 | dev_priv->last_pause_ptr = |
| 548 | via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, |
| 549 | &pause_addr_hi, &pause_addr_lo, 1) - 1; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 550 | |
| 551 | via_flush_write_combine(); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 552 | while (!*dev_priv->last_pause_ptr) ; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 553 | |
| 554 | VIA_WRITE(VIA_REG_TRANSET, (HC_ParaType_PreCR << 16)); |
| 555 | VIA_WRITE(VIA_REG_TRANSPACE, command); |
| 556 | VIA_WRITE(VIA_REG_TRANSPACE, start_addr_lo); |
| 557 | VIA_WRITE(VIA_REG_TRANSPACE, end_addr_lo); |
| 558 | |
| 559 | VIA_WRITE(VIA_REG_TRANSPACE, pause_addr_hi); |
| 560 | VIA_WRITE(VIA_REG_TRANSPACE, pause_addr_lo); |
Thomas Hellstrom | 76f6255 | 2007-01-08 21:03:23 +1100 | [diff] [blame^] | 561 | DRM_WRITEMEMORYBARRIER(); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 562 | VIA_WRITE(VIA_REG_TRANSPACE, command | HC_HAGPCMNT_MASK); |
Thomas Hellstrom | 76f6255 | 2007-01-08 21:03:23 +1100 | [diff] [blame^] | 563 | VIA_READ(VIA_REG_TRANSPACE); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 564 | } |
| 565 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 566 | static void via_pad_cache(drm_via_private_t * dev_priv, int qwords) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 567 | { |
| 568 | uint32_t *vb; |
| 569 | |
| 570 | via_cmdbuf_wait(dev_priv, qwords + 2); |
| 571 | vb = via_get_dma(dev_priv); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 572 | VIA_OUT_RING_QW(HC_HEADER2, HC_ParaType_NotTex << 16); |
| 573 | via_align_buffer(dev_priv, vb, qwords); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static inline void via_dummy_bitblt(drm_via_private_t * dev_priv) |
| 577 | { |
| 578 | uint32_t *vb = via_get_dma(dev_priv); |
| 579 | SetReg2DAGP(0x0C, (0 | (0 << 16))); |
| 580 | SetReg2DAGP(0x10, 0 | (0 << 16)); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 581 | SetReg2DAGP(0x0, 0x1 | 0x2000 | 0xAA000000); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 582 | } |
| 583 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 584 | static void via_cmdbuf_jump(drm_via_private_t * dev_priv) |
| 585 | { |
| 586 | uint32_t agp_base; |
| 587 | uint32_t pause_addr_lo, pause_addr_hi; |
| 588 | uint32_t jump_addr_lo, jump_addr_hi; |
| 589 | volatile uint32_t *last_pause_ptr; |
| 590 | uint32_t dma_low_save1, dma_low_save2; |
| 591 | |
| 592 | agp_base = dev_priv->dma_offset + (uint32_t) dev_priv->agpAddr; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 593 | via_align_cmd(dev_priv, HC_HAGPBpID_JUMP, 0, &jump_addr_hi, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 594 | &jump_addr_lo, 0); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 595 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 596 | dev_priv->dma_wrap = dev_priv->dma_low; |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 597 | |
| 598 | /* |
| 599 | * Wrap command buffer to the beginning. |
| 600 | */ |
| 601 | |
| 602 | dev_priv->dma_low = 0; |
| 603 | if (via_cmdbuf_wait(dev_priv, CMDBUF_ALIGNMENT_SIZE) != 0) { |
| 604 | DRM_ERROR("via_cmdbuf_jump failed\n"); |
| 605 | } |
| 606 | |
| 607 | via_dummy_bitblt(dev_priv); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 608 | via_dummy_bitblt(dev_priv); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 609 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 610 | last_pause_ptr = |
| 611 | via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, &pause_addr_hi, |
| 612 | &pause_addr_lo, 0) - 1; |
| 613 | via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, &pause_addr_hi, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 614 | &pause_addr_lo, 0); |
| 615 | |
| 616 | *last_pause_ptr = pause_addr_lo; |
| 617 | dma_low_save1 = dev_priv->dma_low; |
| 618 | |
| 619 | /* |
| 620 | * Now, set a trap that will pause the regulator if it tries to rerun the old |
| 621 | * command buffer. (Which may happen if via_hook_segment detecs a command regulator pause |
| 622 | * and reissues the jump command over PCI, while the regulator has already taken the jump |
| 623 | * and actually paused at the current buffer end). |
| 624 | * There appears to be no other way to detect this condition, since the hw_addr_pointer |
| 625 | * does not seem to get updated immediately when a jump occurs. |
| 626 | */ |
| 627 | |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 628 | last_pause_ptr = |
| 629 | via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, &pause_addr_hi, |
| 630 | &pause_addr_lo, 0) - 1; |
| 631 | via_align_cmd(dev_priv, HC_HAGPBpID_PAUSE, 0, &pause_addr_hi, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 632 | &pause_addr_lo, 0); |
| 633 | *last_pause_ptr = pause_addr_lo; |
| 634 | |
| 635 | dma_low_save2 = dev_priv->dma_low; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 636 | dev_priv->dma_low = dma_low_save1; |
| 637 | via_hook_segment(dev_priv, jump_addr_hi, jump_addr_lo, 0); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 638 | dev_priv->dma_low = dma_low_save2; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 639 | via_hook_segment(dev_priv, pause_addr_hi, pause_addr_lo, 0); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 640 | } |
| 641 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 642 | static void via_cmdbuf_rewind(drm_via_private_t * dev_priv) |
| 643 | { |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 644 | via_cmdbuf_jump(dev_priv); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | static void via_cmdbuf_flush(drm_via_private_t * dev_priv, uint32_t cmd_type) |
| 648 | { |
| 649 | uint32_t pause_addr_lo, pause_addr_hi; |
| 650 | |
| 651 | via_align_cmd(dev_priv, cmd_type, 0, &pause_addr_hi, &pause_addr_lo, 0); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 652 | via_hook_segment(dev_priv, pause_addr_hi, pause_addr_lo, 0); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 653 | } |
| 654 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 655 | static void via_cmdbuf_pause(drm_via_private_t * dev_priv) |
| 656 | { |
| 657 | via_cmdbuf_flush(dev_priv, HC_HAGPBpID_PAUSE); |
| 658 | } |
| 659 | |
| 660 | static void via_cmdbuf_reset(drm_via_private_t * dev_priv) |
| 661 | { |
| 662 | via_cmdbuf_flush(dev_priv, HC_HAGPBpID_STOP); |
| 663 | via_wait_idle(dev_priv); |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | * User interface to the space and lag functions. |
| 668 | */ |
| 669 | |
Dave Airlie | ce60fe0 | 2006-02-02 19:21:38 +1100 | [diff] [blame] | 670 | static int via_cmdbuf_size(DRM_IOCTL_ARGS) |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 671 | { |
| 672 | DRM_DEVICE; |
| 673 | drm_via_cmdbuf_size_t d_siz; |
| 674 | int ret = 0; |
| 675 | uint32_t tmp_size, count; |
| 676 | drm_via_private_t *dev_priv; |
| 677 | |
| 678 | DRM_DEBUG("via cmdbuf_size\n"); |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 679 | LOCK_TEST_WITH_RETURN(dev, filp); |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 680 | |
| 681 | dev_priv = (drm_via_private_t *) dev->dev_private; |
| 682 | |
| 683 | if (dev_priv->ring.virtual_start == NULL) { |
| 684 | DRM_ERROR("%s called without initializing AGP ring buffer.\n", |
| 685 | __FUNCTION__); |
| 686 | return DRM_ERR(EFAULT); |
| 687 | } |
| 688 | |
Alexey Dobriyan | bbaf364 | 2005-07-27 11:43:56 -0700 | [diff] [blame] | 689 | DRM_COPY_FROM_USER_IOCTL(d_siz, (drm_via_cmdbuf_size_t __user *) data, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 690 | sizeof(d_siz)); |
| 691 | |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 692 | count = 1000000; |
| 693 | tmp_size = d_siz.size; |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 694 | switch (d_siz.func) { |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 695 | case VIA_CMDBUF_SPACE: |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 696 | while (((tmp_size = via_cmdbuf_space(dev_priv)) < d_siz.size) |
| 697 | && count--) { |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 698 | if (!d_siz.wait) { |
| 699 | break; |
| 700 | } |
| 701 | } |
| 702 | if (!count) { |
| 703 | DRM_ERROR("VIA_CMDBUF_SPACE timed out.\n"); |
| 704 | ret = DRM_ERR(EAGAIN); |
| 705 | } |
| 706 | break; |
| 707 | case VIA_CMDBUF_LAG: |
Dave Airlie | b5e89ed | 2005-09-25 14:28:13 +1000 | [diff] [blame] | 708 | while (((tmp_size = via_cmdbuf_lag(dev_priv)) > d_siz.size) |
| 709 | && count--) { |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 710 | if (!d_siz.wait) { |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | if (!count) { |
| 715 | DRM_ERROR("VIA_CMDBUF_LAG timed out.\n"); |
| 716 | ret = DRM_ERR(EAGAIN); |
| 717 | } |
| 718 | break; |
| 719 | default: |
| 720 | ret = DRM_ERR(EFAULT); |
| 721 | } |
| 722 | d_siz.size = tmp_size; |
| 723 | |
Alexey Dobriyan | bbaf364 | 2005-07-27 11:43:56 -0700 | [diff] [blame] | 724 | DRM_COPY_TO_USER_IOCTL((drm_via_cmdbuf_size_t __user *) data, d_siz, |
Dave Airlie | 22f579c | 2005-06-28 22:48:56 +1000 | [diff] [blame] | 725 | sizeof(d_siz)); |
| 726 | return ret; |
| 727 | } |
Dave Airlie | 9251424 | 2005-11-12 21:52:46 +1100 | [diff] [blame] | 728 | |
| 729 | drm_ioctl_desc_t via_ioctls[] = { |
Dave Airlie | a7a2cc3 | 2006-01-02 13:54:04 +1100 | [diff] [blame] | 730 | [DRM_IOCTL_NR(DRM_VIA_ALLOCMEM)] = {via_mem_alloc, DRM_AUTH}, |
| 731 | [DRM_IOCTL_NR(DRM_VIA_FREEMEM)] = {via_mem_free, DRM_AUTH}, |
| 732 | [DRM_IOCTL_NR(DRM_VIA_AGP_INIT)] = {via_agp_init, DRM_AUTH|DRM_MASTER}, |
| 733 | [DRM_IOCTL_NR(DRM_VIA_FB_INIT)] = {via_fb_init, DRM_AUTH|DRM_MASTER}, |
| 734 | [DRM_IOCTL_NR(DRM_VIA_MAP_INIT)] = {via_map_init, DRM_AUTH|DRM_MASTER}, |
| 735 | [DRM_IOCTL_NR(DRM_VIA_DEC_FUTEX)] = {via_decoder_futex, DRM_AUTH}, |
| 736 | [DRM_IOCTL_NR(DRM_VIA_DMA_INIT)] = {via_dma_init, DRM_AUTH}, |
| 737 | [DRM_IOCTL_NR(DRM_VIA_CMDBUFFER)] = {via_cmdbuffer, DRM_AUTH}, |
| 738 | [DRM_IOCTL_NR(DRM_VIA_FLUSH)] = {via_flush_ioctl, DRM_AUTH}, |
| 739 | [DRM_IOCTL_NR(DRM_VIA_PCICMD)] = {via_pci_cmdbuffer, DRM_AUTH}, |
| 740 | [DRM_IOCTL_NR(DRM_VIA_CMDBUF_SIZE)] = {via_cmdbuf_size, DRM_AUTH}, |
Dave Airlie | 443448d | 2006-01-02 14:26:20 +1100 | [diff] [blame] | 741 | [DRM_IOCTL_NR(DRM_VIA_WAIT_IRQ)] = {via_wait_irq, DRM_AUTH}, |
| 742 | [DRM_IOCTL_NR(DRM_VIA_DMA_BLIT)] = {via_dma_blit, DRM_AUTH}, |
| 743 | [DRM_IOCTL_NR(DRM_VIA_BLIT_SYNC)] = {via_dma_blit_sync, DRM_AUTH} |
Dave Airlie | 9251424 | 2005-11-12 21:52:46 +1100 | [diff] [blame] | 744 | }; |
| 745 | |
| 746 | int via_max_ioctl = DRM_ARRAY_SIZE(via_ioctls); |