1 1.2 riastrad /* $NetBSD: savage_bci.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $ */ 2 1.2 riastrad 3 1.1 riastrad /* savage_bci.c -- BCI support for Savage 4 1.1 riastrad * 5 1.1 riastrad * Copyright 2004 Felix Kuehling 6 1.1 riastrad * All Rights Reserved. 7 1.1 riastrad * 8 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 9 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 10 1.1 riastrad * to deal in the Software without restriction, including without limitation 11 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sub license, 12 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 13 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 14 1.1 riastrad * 15 1.1 riastrad * The above copyright notice and this permission notice (including the 16 1.1 riastrad * next paragraph) shall be included in all copies or substantial portions 17 1.1 riastrad * of the Software. 18 1.1 riastrad * 19 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 1.1 riastrad * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 21 1.1 riastrad * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 1.1 riastrad * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR 23 1.1 riastrad * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 24 1.1 riastrad * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 25 1.1 riastrad * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 26 1.1 riastrad */ 27 1.3 riastrad 28 1.2 riastrad #include <sys/cdefs.h> 29 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: savage_bci.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $"); 30 1.2 riastrad 31 1.3 riastrad #include <linux/delay.h> 32 1.3 riastrad #include <linux/pci.h> 33 1.3 riastrad #include <linux/slab.h> 34 1.3 riastrad #include <linux/uaccess.h> 35 1.3 riastrad 36 1.3 riastrad #include <drm/drm_device.h> 37 1.3 riastrad #include <drm/drm_file.h> 38 1.3 riastrad #include <drm/drm_print.h> 39 1.1 riastrad #include <drm/savage_drm.h> 40 1.3 riastrad 41 1.1 riastrad #include "savage_drv.h" 42 1.1 riastrad 43 1.1 riastrad /* Need a long timeout for shadow status updates can take a while 44 1.1 riastrad * and so can waiting for events when the queue is full. */ 45 1.1 riastrad #define SAVAGE_DEFAULT_USEC_TIMEOUT 1000000 /* 1s */ 46 1.1 riastrad #define SAVAGE_EVENT_USEC_TIMEOUT 5000000 /* 5s */ 47 1.1 riastrad #define SAVAGE_FREELIST_DEBUG 0 48 1.1 riastrad 49 1.1 riastrad static int savage_do_cleanup_bci(struct drm_device *dev); 50 1.1 riastrad 51 1.1 riastrad static int 52 1.1 riastrad savage_bci_wait_fifo_shadow(drm_savage_private_t * dev_priv, unsigned int n) 53 1.1 riastrad { 54 1.1 riastrad uint32_t mask = dev_priv->status_used_mask; 55 1.1 riastrad uint32_t threshold = dev_priv->bci_threshold_hi; 56 1.1 riastrad uint32_t status; 57 1.1 riastrad int i; 58 1.1 riastrad 59 1.1 riastrad #if SAVAGE_BCI_DEBUG 60 1.1 riastrad if (n > dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - threshold) 61 1.1 riastrad DRM_ERROR("Trying to emit %d words " 62 1.1 riastrad "(more than guaranteed space in COB)\n", n); 63 1.1 riastrad #endif 64 1.1 riastrad 65 1.1 riastrad for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) { 66 1.2 riastrad mb(); 67 1.1 riastrad status = dev_priv->status_ptr[0]; 68 1.1 riastrad if ((status & mask) < threshold) 69 1.1 riastrad return 0; 70 1.3 riastrad udelay(1); 71 1.1 riastrad } 72 1.1 riastrad 73 1.1 riastrad #if SAVAGE_BCI_DEBUG 74 1.1 riastrad DRM_ERROR("failed!\n"); 75 1.1 riastrad DRM_INFO(" status=0x%08x, threshold=0x%08x\n", status, threshold); 76 1.1 riastrad #endif 77 1.1 riastrad return -EBUSY; 78 1.1 riastrad } 79 1.1 riastrad 80 1.1 riastrad static int 81 1.1 riastrad savage_bci_wait_fifo_s3d(drm_savage_private_t * dev_priv, unsigned int n) 82 1.1 riastrad { 83 1.1 riastrad uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n; 84 1.1 riastrad uint32_t status; 85 1.1 riastrad int i; 86 1.1 riastrad 87 1.1 riastrad for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) { 88 1.1 riastrad status = SAVAGE_READ(SAVAGE_STATUS_WORD0); 89 1.1 riastrad if ((status & SAVAGE_FIFO_USED_MASK_S3D) <= maxUsed) 90 1.1 riastrad return 0; 91 1.3 riastrad udelay(1); 92 1.1 riastrad } 93 1.1 riastrad 94 1.1 riastrad #if SAVAGE_BCI_DEBUG 95 1.1 riastrad DRM_ERROR("failed!\n"); 96 1.1 riastrad DRM_INFO(" status=0x%08x\n", status); 97 1.1 riastrad #endif 98 1.1 riastrad return -EBUSY; 99 1.1 riastrad } 100 1.1 riastrad 101 1.1 riastrad static int 102 1.1 riastrad savage_bci_wait_fifo_s4(drm_savage_private_t * dev_priv, unsigned int n) 103 1.1 riastrad { 104 1.1 riastrad uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n; 105 1.1 riastrad uint32_t status; 106 1.1 riastrad int i; 107 1.1 riastrad 108 1.1 riastrad for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) { 109 1.1 riastrad status = SAVAGE_READ(SAVAGE_ALT_STATUS_WORD0); 110 1.1 riastrad if ((status & SAVAGE_FIFO_USED_MASK_S4) <= maxUsed) 111 1.1 riastrad return 0; 112 1.3 riastrad udelay(1); 113 1.1 riastrad } 114 1.1 riastrad 115 1.1 riastrad #if SAVAGE_BCI_DEBUG 116 1.1 riastrad DRM_ERROR("failed!\n"); 117 1.1 riastrad DRM_INFO(" status=0x%08x\n", status); 118 1.1 riastrad #endif 119 1.1 riastrad return -EBUSY; 120 1.1 riastrad } 121 1.1 riastrad 122 1.1 riastrad /* 123 1.1 riastrad * Waiting for events. 124 1.1 riastrad * 125 1.1 riastrad * The BIOSresets the event tag to 0 on mode changes. Therefore we 126 1.1 riastrad * never emit 0 to the event tag. If we find a 0 event tag we know the 127 1.1 riastrad * BIOS stomped on it and return success assuming that the BIOS waited 128 1.1 riastrad * for engine idle. 129 1.1 riastrad * 130 1.1 riastrad * Note: if the Xserver uses the event tag it has to follow the same 131 1.1 riastrad * rule. Otherwise there may be glitches every 2^16 events. 132 1.1 riastrad */ 133 1.1 riastrad static int 134 1.1 riastrad savage_bci_wait_event_shadow(drm_savage_private_t * dev_priv, uint16_t e) 135 1.1 riastrad { 136 1.1 riastrad uint32_t status; 137 1.1 riastrad int i; 138 1.1 riastrad 139 1.1 riastrad for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) { 140 1.2 riastrad mb(); 141 1.1 riastrad status = dev_priv->status_ptr[1]; 142 1.1 riastrad if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff || 143 1.1 riastrad (status & 0xffff) == 0) 144 1.1 riastrad return 0; 145 1.3 riastrad udelay(1); 146 1.1 riastrad } 147 1.1 riastrad 148 1.1 riastrad #if SAVAGE_BCI_DEBUG 149 1.1 riastrad DRM_ERROR("failed!\n"); 150 1.1 riastrad DRM_INFO(" status=0x%08x, e=0x%04x\n", status, e); 151 1.1 riastrad #endif 152 1.1 riastrad 153 1.1 riastrad return -EBUSY; 154 1.1 riastrad } 155 1.1 riastrad 156 1.1 riastrad static int 157 1.1 riastrad savage_bci_wait_event_reg(drm_savage_private_t * dev_priv, uint16_t e) 158 1.1 riastrad { 159 1.1 riastrad uint32_t status; 160 1.1 riastrad int i; 161 1.1 riastrad 162 1.1 riastrad for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) { 163 1.1 riastrad status = SAVAGE_READ(SAVAGE_STATUS_WORD1); 164 1.1 riastrad if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff || 165 1.1 riastrad (status & 0xffff) == 0) 166 1.1 riastrad return 0; 167 1.3 riastrad udelay(1); 168 1.1 riastrad } 169 1.1 riastrad 170 1.1 riastrad #if SAVAGE_BCI_DEBUG 171 1.1 riastrad DRM_ERROR("failed!\n"); 172 1.1 riastrad DRM_INFO(" status=0x%08x, e=0x%04x\n", status, e); 173 1.1 riastrad #endif 174 1.1 riastrad 175 1.1 riastrad return -EBUSY; 176 1.1 riastrad } 177 1.1 riastrad 178 1.1 riastrad uint16_t savage_bci_emit_event(drm_savage_private_t * dev_priv, 179 1.1 riastrad unsigned int flags) 180 1.1 riastrad { 181 1.1 riastrad uint16_t count; 182 1.1 riastrad BCI_LOCALS; 183 1.1 riastrad 184 1.1 riastrad if (dev_priv->status_ptr) { 185 1.1 riastrad /* coordinate with Xserver */ 186 1.1 riastrad count = dev_priv->status_ptr[1023]; 187 1.1 riastrad if (count < dev_priv->event_counter) 188 1.1 riastrad dev_priv->event_wrap++; 189 1.1 riastrad } else { 190 1.1 riastrad count = dev_priv->event_counter; 191 1.1 riastrad } 192 1.1 riastrad count = (count + 1) & 0xffff; 193 1.1 riastrad if (count == 0) { 194 1.1 riastrad count++; /* See the comment above savage_wait_event_*. */ 195 1.1 riastrad dev_priv->event_wrap++; 196 1.1 riastrad } 197 1.1 riastrad dev_priv->event_counter = count; 198 1.1 riastrad if (dev_priv->status_ptr) 199 1.1 riastrad dev_priv->status_ptr[1023] = (uint32_t) count; 200 1.1 riastrad 201 1.1 riastrad if ((flags & (SAVAGE_WAIT_2D | SAVAGE_WAIT_3D))) { 202 1.1 riastrad unsigned int wait_cmd = BCI_CMD_WAIT; 203 1.1 riastrad if ((flags & SAVAGE_WAIT_2D)) 204 1.1 riastrad wait_cmd |= BCI_CMD_WAIT_2D; 205 1.1 riastrad if ((flags & SAVAGE_WAIT_3D)) 206 1.1 riastrad wait_cmd |= BCI_CMD_WAIT_3D; 207 1.1 riastrad BEGIN_BCI(2); 208 1.1 riastrad BCI_WRITE(wait_cmd); 209 1.1 riastrad } else { 210 1.1 riastrad BEGIN_BCI(1); 211 1.1 riastrad } 212 1.1 riastrad BCI_WRITE(BCI_CMD_UPDATE_EVENT_TAG | (uint32_t) count); 213 1.1 riastrad 214 1.1 riastrad return count; 215 1.1 riastrad } 216 1.1 riastrad 217 1.1 riastrad /* 218 1.1 riastrad * Freelist management 219 1.1 riastrad */ 220 1.1 riastrad static int savage_freelist_init(struct drm_device * dev) 221 1.1 riastrad { 222 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 223 1.1 riastrad struct drm_device_dma *dma = dev->dma; 224 1.1 riastrad struct drm_buf *buf; 225 1.1 riastrad drm_savage_buf_priv_t *entry; 226 1.1 riastrad int i; 227 1.1 riastrad DRM_DEBUG("count=%d\n", dma->buf_count); 228 1.1 riastrad 229 1.1 riastrad dev_priv->head.next = &dev_priv->tail; 230 1.1 riastrad dev_priv->head.prev = NULL; 231 1.1 riastrad dev_priv->head.buf = NULL; 232 1.1 riastrad 233 1.1 riastrad dev_priv->tail.next = NULL; 234 1.1 riastrad dev_priv->tail.prev = &dev_priv->head; 235 1.1 riastrad dev_priv->tail.buf = NULL; 236 1.1 riastrad 237 1.1 riastrad for (i = 0; i < dma->buf_count; i++) { 238 1.1 riastrad buf = dma->buflist[i]; 239 1.1 riastrad entry = buf->dev_private; 240 1.1 riastrad 241 1.1 riastrad SET_AGE(&entry->age, 0, 0); 242 1.1 riastrad entry->buf = buf; 243 1.1 riastrad 244 1.1 riastrad entry->next = dev_priv->head.next; 245 1.1 riastrad entry->prev = &dev_priv->head; 246 1.1 riastrad dev_priv->head.next->prev = entry; 247 1.1 riastrad dev_priv->head.next = entry; 248 1.1 riastrad } 249 1.1 riastrad 250 1.1 riastrad return 0; 251 1.1 riastrad } 252 1.1 riastrad 253 1.1 riastrad static struct drm_buf *savage_freelist_get(struct drm_device * dev) 254 1.1 riastrad { 255 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 256 1.1 riastrad drm_savage_buf_priv_t *tail = dev_priv->tail.prev; 257 1.1 riastrad uint16_t event; 258 1.1 riastrad unsigned int wrap; 259 1.1 riastrad DRM_DEBUG("\n"); 260 1.1 riastrad 261 1.1 riastrad UPDATE_EVENT_COUNTER(); 262 1.1 riastrad if (dev_priv->status_ptr) 263 1.1 riastrad event = dev_priv->status_ptr[1] & 0xffff; 264 1.1 riastrad else 265 1.1 riastrad event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff; 266 1.1 riastrad wrap = dev_priv->event_wrap; 267 1.1 riastrad if (event > dev_priv->event_counter) 268 1.1 riastrad wrap--; /* hardware hasn't passed the last wrap yet */ 269 1.1 riastrad 270 1.1 riastrad DRM_DEBUG(" tail=0x%04x %d\n", tail->age.event, tail->age.wrap); 271 1.1 riastrad DRM_DEBUG(" head=0x%04x %d\n", event, wrap); 272 1.1 riastrad 273 1.1 riastrad if (tail->buf && (TEST_AGE(&tail->age, event, wrap) || event == 0)) { 274 1.1 riastrad drm_savage_buf_priv_t *next = tail->next; 275 1.1 riastrad drm_savage_buf_priv_t *prev = tail->prev; 276 1.1 riastrad prev->next = next; 277 1.1 riastrad next->prev = prev; 278 1.1 riastrad tail->next = tail->prev = NULL; 279 1.1 riastrad return tail->buf; 280 1.1 riastrad } 281 1.1 riastrad 282 1.1 riastrad DRM_DEBUG("returning NULL, tail->buf=%p!\n", tail->buf); 283 1.1 riastrad return NULL; 284 1.1 riastrad } 285 1.1 riastrad 286 1.1 riastrad void savage_freelist_put(struct drm_device * dev, struct drm_buf * buf) 287 1.1 riastrad { 288 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 289 1.1 riastrad drm_savage_buf_priv_t *entry = buf->dev_private, *prev, *next; 290 1.1 riastrad 291 1.1 riastrad DRM_DEBUG("age=0x%04x wrap=%d\n", entry->age.event, entry->age.wrap); 292 1.1 riastrad 293 1.1 riastrad if (entry->next != NULL || entry->prev != NULL) { 294 1.1 riastrad DRM_ERROR("entry already on freelist.\n"); 295 1.1 riastrad return; 296 1.1 riastrad } 297 1.1 riastrad 298 1.1 riastrad prev = &dev_priv->head; 299 1.1 riastrad next = prev->next; 300 1.1 riastrad prev->next = entry; 301 1.1 riastrad next->prev = entry; 302 1.1 riastrad entry->prev = prev; 303 1.1 riastrad entry->next = next; 304 1.1 riastrad } 305 1.1 riastrad 306 1.1 riastrad /* 307 1.1 riastrad * Command DMA 308 1.1 riastrad */ 309 1.1 riastrad static int savage_dma_init(drm_savage_private_t * dev_priv) 310 1.1 riastrad { 311 1.1 riastrad unsigned int i; 312 1.1 riastrad 313 1.1 riastrad dev_priv->nr_dma_pages = dev_priv->cmd_dma->size / 314 1.1 riastrad (SAVAGE_DMA_PAGE_SIZE * 4); 315 1.3 riastrad dev_priv->dma_pages = kmalloc_array(dev_priv->nr_dma_pages, 316 1.3 riastrad sizeof(drm_savage_dma_page_t), 317 1.3 riastrad GFP_KERNEL); 318 1.1 riastrad if (dev_priv->dma_pages == NULL) 319 1.1 riastrad return -ENOMEM; 320 1.1 riastrad 321 1.1 riastrad for (i = 0; i < dev_priv->nr_dma_pages; ++i) { 322 1.1 riastrad SET_AGE(&dev_priv->dma_pages[i].age, 0, 0); 323 1.1 riastrad dev_priv->dma_pages[i].used = 0; 324 1.1 riastrad dev_priv->dma_pages[i].flushed = 0; 325 1.1 riastrad } 326 1.1 riastrad SET_AGE(&dev_priv->last_dma_age, 0, 0); 327 1.1 riastrad 328 1.1 riastrad dev_priv->first_dma_page = 0; 329 1.1 riastrad dev_priv->current_dma_page = 0; 330 1.1 riastrad 331 1.1 riastrad return 0; 332 1.1 riastrad } 333 1.1 riastrad 334 1.1 riastrad void savage_dma_reset(drm_savage_private_t * dev_priv) 335 1.1 riastrad { 336 1.1 riastrad uint16_t event; 337 1.1 riastrad unsigned int wrap, i; 338 1.1 riastrad event = savage_bci_emit_event(dev_priv, 0); 339 1.1 riastrad wrap = dev_priv->event_wrap; 340 1.1 riastrad for (i = 0; i < dev_priv->nr_dma_pages; ++i) { 341 1.1 riastrad SET_AGE(&dev_priv->dma_pages[i].age, event, wrap); 342 1.1 riastrad dev_priv->dma_pages[i].used = 0; 343 1.1 riastrad dev_priv->dma_pages[i].flushed = 0; 344 1.1 riastrad } 345 1.1 riastrad SET_AGE(&dev_priv->last_dma_age, event, wrap); 346 1.1 riastrad dev_priv->first_dma_page = dev_priv->current_dma_page = 0; 347 1.1 riastrad } 348 1.1 riastrad 349 1.1 riastrad void savage_dma_wait(drm_savage_private_t * dev_priv, unsigned int page) 350 1.1 riastrad { 351 1.1 riastrad uint16_t event; 352 1.1 riastrad unsigned int wrap; 353 1.1 riastrad 354 1.1 riastrad /* Faked DMA buffer pages don't age. */ 355 1.1 riastrad if (dev_priv->cmd_dma == &dev_priv->fake_dma) 356 1.1 riastrad return; 357 1.1 riastrad 358 1.1 riastrad UPDATE_EVENT_COUNTER(); 359 1.1 riastrad if (dev_priv->status_ptr) 360 1.1 riastrad event = dev_priv->status_ptr[1] & 0xffff; 361 1.1 riastrad else 362 1.1 riastrad event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff; 363 1.1 riastrad wrap = dev_priv->event_wrap; 364 1.1 riastrad if (event > dev_priv->event_counter) 365 1.1 riastrad wrap--; /* hardware hasn't passed the last wrap yet */ 366 1.1 riastrad 367 1.1 riastrad if (dev_priv->dma_pages[page].age.wrap > wrap || 368 1.1 riastrad (dev_priv->dma_pages[page].age.wrap == wrap && 369 1.1 riastrad dev_priv->dma_pages[page].age.event > event)) { 370 1.1 riastrad if (dev_priv->wait_evnt(dev_priv, 371 1.1 riastrad dev_priv->dma_pages[page].age.event) 372 1.1 riastrad < 0) 373 1.1 riastrad DRM_ERROR("wait_evnt failed!\n"); 374 1.1 riastrad } 375 1.1 riastrad } 376 1.1 riastrad 377 1.1 riastrad uint32_t *savage_dma_alloc(drm_savage_private_t * dev_priv, unsigned int n) 378 1.1 riastrad { 379 1.1 riastrad unsigned int cur = dev_priv->current_dma_page; 380 1.1 riastrad unsigned int rest = SAVAGE_DMA_PAGE_SIZE - 381 1.1 riastrad dev_priv->dma_pages[cur].used; 382 1.1 riastrad unsigned int nr_pages = (n - rest + SAVAGE_DMA_PAGE_SIZE - 1) / 383 1.1 riastrad SAVAGE_DMA_PAGE_SIZE; 384 1.1 riastrad uint32_t *dma_ptr; 385 1.1 riastrad unsigned int i; 386 1.1 riastrad 387 1.1 riastrad DRM_DEBUG("cur=%u, cur->used=%u, n=%u, rest=%u, nr_pages=%u\n", 388 1.1 riastrad cur, dev_priv->dma_pages[cur].used, n, rest, nr_pages); 389 1.1 riastrad 390 1.1 riastrad if (cur + nr_pages < dev_priv->nr_dma_pages) { 391 1.1 riastrad dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle + 392 1.1 riastrad cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used; 393 1.1 riastrad if (n < rest) 394 1.1 riastrad rest = n; 395 1.1 riastrad dev_priv->dma_pages[cur].used += rest; 396 1.1 riastrad n -= rest; 397 1.1 riastrad cur++; 398 1.1 riastrad } else { 399 1.1 riastrad dev_priv->dma_flush(dev_priv); 400 1.1 riastrad nr_pages = 401 1.1 riastrad (n + SAVAGE_DMA_PAGE_SIZE - 1) / SAVAGE_DMA_PAGE_SIZE; 402 1.1 riastrad for (i = cur; i < dev_priv->nr_dma_pages; ++i) { 403 1.1 riastrad dev_priv->dma_pages[i].age = dev_priv->last_dma_age; 404 1.1 riastrad dev_priv->dma_pages[i].used = 0; 405 1.1 riastrad dev_priv->dma_pages[i].flushed = 0; 406 1.1 riastrad } 407 1.1 riastrad dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle; 408 1.1 riastrad dev_priv->first_dma_page = cur = 0; 409 1.1 riastrad } 410 1.1 riastrad for (i = cur; nr_pages > 0; ++i, --nr_pages) { 411 1.1 riastrad #if SAVAGE_DMA_DEBUG 412 1.1 riastrad if (dev_priv->dma_pages[i].used) { 413 1.1 riastrad DRM_ERROR("unflushed page %u: used=%u\n", 414 1.1 riastrad i, dev_priv->dma_pages[i].used); 415 1.1 riastrad } 416 1.1 riastrad #endif 417 1.1 riastrad if (n > SAVAGE_DMA_PAGE_SIZE) 418 1.1 riastrad dev_priv->dma_pages[i].used = SAVAGE_DMA_PAGE_SIZE; 419 1.1 riastrad else 420 1.1 riastrad dev_priv->dma_pages[i].used = n; 421 1.1 riastrad n -= SAVAGE_DMA_PAGE_SIZE; 422 1.1 riastrad } 423 1.1 riastrad dev_priv->current_dma_page = --i; 424 1.1 riastrad 425 1.1 riastrad DRM_DEBUG("cur=%u, cur->used=%u, n=%u\n", 426 1.1 riastrad i, dev_priv->dma_pages[i].used, n); 427 1.1 riastrad 428 1.1 riastrad savage_dma_wait(dev_priv, dev_priv->current_dma_page); 429 1.1 riastrad 430 1.1 riastrad return dma_ptr; 431 1.1 riastrad } 432 1.1 riastrad 433 1.1 riastrad static void savage_dma_flush(drm_savage_private_t * dev_priv) 434 1.1 riastrad { 435 1.1 riastrad unsigned int first = dev_priv->first_dma_page; 436 1.1 riastrad unsigned int cur = dev_priv->current_dma_page; 437 1.1 riastrad uint16_t event; 438 1.1 riastrad unsigned int wrap, pad, align, len, i; 439 1.1 riastrad unsigned long phys_addr; 440 1.1 riastrad BCI_LOCALS; 441 1.1 riastrad 442 1.1 riastrad if (first == cur && 443 1.1 riastrad dev_priv->dma_pages[cur].used == dev_priv->dma_pages[cur].flushed) 444 1.1 riastrad return; 445 1.1 riastrad 446 1.1 riastrad /* pad length to multiples of 2 entries 447 1.1 riastrad * align start of next DMA block to multiles of 8 entries */ 448 1.1 riastrad pad = -dev_priv->dma_pages[cur].used & 1; 449 1.1 riastrad align = -(dev_priv->dma_pages[cur].used + pad) & 7; 450 1.1 riastrad 451 1.1 riastrad DRM_DEBUG("first=%u, cur=%u, first->flushed=%u, cur->used=%u, " 452 1.1 riastrad "pad=%u, align=%u\n", 453 1.1 riastrad first, cur, dev_priv->dma_pages[first].flushed, 454 1.1 riastrad dev_priv->dma_pages[cur].used, pad, align); 455 1.1 riastrad 456 1.1 riastrad /* pad with noops */ 457 1.1 riastrad if (pad) { 458 1.1 riastrad uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle + 459 1.1 riastrad cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used; 460 1.1 riastrad dev_priv->dma_pages[cur].used += pad; 461 1.1 riastrad while (pad != 0) { 462 1.1 riastrad *dma_ptr++ = BCI_CMD_WAIT; 463 1.1 riastrad pad--; 464 1.1 riastrad } 465 1.1 riastrad } 466 1.1 riastrad 467 1.2 riastrad mb(); 468 1.1 riastrad 469 1.1 riastrad /* do flush ... */ 470 1.1 riastrad phys_addr = dev_priv->cmd_dma->offset + 471 1.1 riastrad (first * SAVAGE_DMA_PAGE_SIZE + 472 1.1 riastrad dev_priv->dma_pages[first].flushed) * 4; 473 1.1 riastrad len = (cur - first) * SAVAGE_DMA_PAGE_SIZE + 474 1.1 riastrad dev_priv->dma_pages[cur].used - dev_priv->dma_pages[first].flushed; 475 1.1 riastrad 476 1.1 riastrad DRM_DEBUG("phys_addr=%lx, len=%u\n", 477 1.1 riastrad phys_addr | dev_priv->dma_type, len); 478 1.1 riastrad 479 1.1 riastrad BEGIN_BCI(3); 480 1.1 riastrad BCI_SET_REGISTERS(SAVAGE_DMABUFADDR, 1); 481 1.1 riastrad BCI_WRITE(phys_addr | dev_priv->dma_type); 482 1.1 riastrad BCI_DMA(len); 483 1.1 riastrad 484 1.1 riastrad /* fix alignment of the start of the next block */ 485 1.1 riastrad dev_priv->dma_pages[cur].used += align; 486 1.1 riastrad 487 1.1 riastrad /* age DMA pages */ 488 1.1 riastrad event = savage_bci_emit_event(dev_priv, 0); 489 1.1 riastrad wrap = dev_priv->event_wrap; 490 1.1 riastrad for (i = first; i < cur; ++i) { 491 1.1 riastrad SET_AGE(&dev_priv->dma_pages[i].age, event, wrap); 492 1.1 riastrad dev_priv->dma_pages[i].used = 0; 493 1.1 riastrad dev_priv->dma_pages[i].flushed = 0; 494 1.1 riastrad } 495 1.1 riastrad /* age the current page only when it's full */ 496 1.1 riastrad if (dev_priv->dma_pages[cur].used == SAVAGE_DMA_PAGE_SIZE) { 497 1.1 riastrad SET_AGE(&dev_priv->dma_pages[cur].age, event, wrap); 498 1.1 riastrad dev_priv->dma_pages[cur].used = 0; 499 1.1 riastrad dev_priv->dma_pages[cur].flushed = 0; 500 1.1 riastrad /* advance to next page */ 501 1.1 riastrad cur++; 502 1.1 riastrad if (cur == dev_priv->nr_dma_pages) 503 1.1 riastrad cur = 0; 504 1.1 riastrad dev_priv->first_dma_page = dev_priv->current_dma_page = cur; 505 1.1 riastrad } else { 506 1.1 riastrad dev_priv->first_dma_page = cur; 507 1.1 riastrad dev_priv->dma_pages[cur].flushed = dev_priv->dma_pages[i].used; 508 1.1 riastrad } 509 1.1 riastrad SET_AGE(&dev_priv->last_dma_age, event, wrap); 510 1.1 riastrad 511 1.1 riastrad DRM_DEBUG("first=cur=%u, cur->used=%u, cur->flushed=%u\n", cur, 512 1.1 riastrad dev_priv->dma_pages[cur].used, 513 1.1 riastrad dev_priv->dma_pages[cur].flushed); 514 1.1 riastrad } 515 1.1 riastrad 516 1.1 riastrad static void savage_fake_dma_flush(drm_savage_private_t * dev_priv) 517 1.1 riastrad { 518 1.1 riastrad unsigned int i, j; 519 1.1 riastrad BCI_LOCALS; 520 1.1 riastrad 521 1.1 riastrad if (dev_priv->first_dma_page == dev_priv->current_dma_page && 522 1.1 riastrad dev_priv->dma_pages[dev_priv->current_dma_page].used == 0) 523 1.1 riastrad return; 524 1.1 riastrad 525 1.1 riastrad DRM_DEBUG("first=%u, cur=%u, cur->used=%u\n", 526 1.1 riastrad dev_priv->first_dma_page, dev_priv->current_dma_page, 527 1.1 riastrad dev_priv->dma_pages[dev_priv->current_dma_page].used); 528 1.1 riastrad 529 1.1 riastrad for (i = dev_priv->first_dma_page; 530 1.1 riastrad i <= dev_priv->current_dma_page && dev_priv->dma_pages[i].used; 531 1.1 riastrad ++i) { 532 1.1 riastrad uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle + 533 1.1 riastrad i * SAVAGE_DMA_PAGE_SIZE; 534 1.1 riastrad #if SAVAGE_DMA_DEBUG 535 1.1 riastrad /* Sanity check: all pages except the last one must be full. */ 536 1.1 riastrad if (i < dev_priv->current_dma_page && 537 1.1 riastrad dev_priv->dma_pages[i].used != SAVAGE_DMA_PAGE_SIZE) { 538 1.1 riastrad DRM_ERROR("partial DMA page %u: used=%u", 539 1.1 riastrad i, dev_priv->dma_pages[i].used); 540 1.1 riastrad } 541 1.1 riastrad #endif 542 1.1 riastrad BEGIN_BCI(dev_priv->dma_pages[i].used); 543 1.1 riastrad for (j = 0; j < dev_priv->dma_pages[i].used; ++j) { 544 1.1 riastrad BCI_WRITE(dma_ptr[j]); 545 1.1 riastrad } 546 1.1 riastrad dev_priv->dma_pages[i].used = 0; 547 1.1 riastrad } 548 1.1 riastrad 549 1.1 riastrad /* reset to first page */ 550 1.1 riastrad dev_priv->first_dma_page = dev_priv->current_dma_page = 0; 551 1.1 riastrad } 552 1.1 riastrad 553 1.1 riastrad int savage_driver_load(struct drm_device *dev, unsigned long chipset) 554 1.1 riastrad { 555 1.1 riastrad drm_savage_private_t *dev_priv; 556 1.1 riastrad 557 1.1 riastrad dev_priv = kzalloc(sizeof(drm_savage_private_t), GFP_KERNEL); 558 1.1 riastrad if (dev_priv == NULL) 559 1.1 riastrad return -ENOMEM; 560 1.1 riastrad 561 1.1 riastrad dev->dev_private = (void *)dev_priv; 562 1.1 riastrad 563 1.1 riastrad dev_priv->chipset = (enum savage_family)chipset; 564 1.1 riastrad 565 1.1 riastrad pci_set_master(dev->pdev); 566 1.1 riastrad 567 1.1 riastrad return 0; 568 1.1 riastrad } 569 1.1 riastrad 570 1.1 riastrad 571 1.1 riastrad /* 572 1.1 riastrad * Initialize mappings. On Savage4 and SavageIX the alignment 573 1.1 riastrad * and size of the aperture is not suitable for automatic MTRR setup 574 1.2 riastrad * in drm_legacy_addmap. Therefore we add them manually before the maps are 575 1.1 riastrad * initialized, and tear them down on last close. 576 1.1 riastrad */ 577 1.1 riastrad int savage_driver_firstopen(struct drm_device *dev) 578 1.1 riastrad { 579 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 580 1.1 riastrad unsigned long mmio_base, fb_base, fb_size, aperture_base; 581 1.1 riastrad /* fb_rsrc and aper_rsrc aren't really used currently, but still exist 582 1.1 riastrad * in case we decide we need information on the BAR for BSD in the 583 1.1 riastrad * future. 584 1.1 riastrad */ 585 1.1 riastrad unsigned int fb_rsrc, aper_rsrc; 586 1.1 riastrad int ret = 0; 587 1.1 riastrad 588 1.1 riastrad if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) { 589 1.1 riastrad fb_rsrc = 0; 590 1.1 riastrad fb_base = pci_resource_start(dev->pdev, 0); 591 1.1 riastrad fb_size = SAVAGE_FB_SIZE_S3; 592 1.1 riastrad mmio_base = fb_base + SAVAGE_FB_SIZE_S3; 593 1.1 riastrad aper_rsrc = 0; 594 1.1 riastrad aperture_base = fb_base + SAVAGE_APERTURE_OFFSET; 595 1.1 riastrad /* this should always be true */ 596 1.1 riastrad if (pci_resource_len(dev->pdev, 0) == 0x08000000) { 597 1.1 riastrad /* Don't make MMIO write-cobining! We need 3 598 1.1 riastrad * MTRRs. */ 599 1.2 riastrad dev_priv->mtrr_handles[0] = 600 1.2 riastrad arch_phys_wc_add(fb_base, 0x01000000); 601 1.2 riastrad dev_priv->mtrr_handles[1] = 602 1.2 riastrad arch_phys_wc_add(fb_base + 0x02000000, 603 1.2 riastrad 0x02000000); 604 1.2 riastrad dev_priv->mtrr_handles[2] = 605 1.2 riastrad arch_phys_wc_add(fb_base + 0x04000000, 606 1.2 riastrad 0x04000000); 607 1.1 riastrad } else { 608 1.1 riastrad DRM_ERROR("strange pci_resource_len %08llx\n", 609 1.1 riastrad (unsigned long long) 610 1.1 riastrad pci_resource_len(dev->pdev, 0)); 611 1.1 riastrad } 612 1.1 riastrad } else if (dev_priv->chipset != S3_SUPERSAVAGE && 613 1.1 riastrad dev_priv->chipset != S3_SAVAGE2000) { 614 1.1 riastrad mmio_base = pci_resource_start(dev->pdev, 0); 615 1.1 riastrad fb_rsrc = 1; 616 1.1 riastrad fb_base = pci_resource_start(dev->pdev, 1); 617 1.1 riastrad fb_size = SAVAGE_FB_SIZE_S4; 618 1.1 riastrad aper_rsrc = 1; 619 1.1 riastrad aperture_base = fb_base + SAVAGE_APERTURE_OFFSET; 620 1.1 riastrad /* this should always be true */ 621 1.1 riastrad if (pci_resource_len(dev->pdev, 1) == 0x08000000) { 622 1.1 riastrad /* Can use one MTRR to cover both fb and 623 1.1 riastrad * aperture. */ 624 1.2 riastrad dev_priv->mtrr_handles[0] = 625 1.2 riastrad arch_phys_wc_add(fb_base, 626 1.2 riastrad 0x08000000); 627 1.1 riastrad } else { 628 1.1 riastrad DRM_ERROR("strange pci_resource_len %08llx\n", 629 1.1 riastrad (unsigned long long) 630 1.1 riastrad pci_resource_len(dev->pdev, 1)); 631 1.1 riastrad } 632 1.1 riastrad } else { 633 1.1 riastrad mmio_base = pci_resource_start(dev->pdev, 0); 634 1.1 riastrad fb_rsrc = 1; 635 1.1 riastrad fb_base = pci_resource_start(dev->pdev, 1); 636 1.1 riastrad fb_size = pci_resource_len(dev->pdev, 1); 637 1.1 riastrad aper_rsrc = 2; 638 1.1 riastrad aperture_base = pci_resource_start(dev->pdev, 2); 639 1.1 riastrad /* Automatic MTRR setup will do the right thing. */ 640 1.1 riastrad } 641 1.1 riastrad 642 1.2 riastrad ret = drm_legacy_addmap(dev, mmio_base, SAVAGE_MMIO_SIZE, 643 1.2 riastrad _DRM_REGISTERS, _DRM_READ_ONLY, 644 1.2 riastrad &dev_priv->mmio); 645 1.1 riastrad if (ret) 646 1.1 riastrad return ret; 647 1.1 riastrad 648 1.2 riastrad ret = drm_legacy_addmap(dev, fb_base, fb_size, _DRM_FRAME_BUFFER, 649 1.2 riastrad _DRM_WRITE_COMBINING, &dev_priv->fb); 650 1.1 riastrad if (ret) 651 1.1 riastrad return ret; 652 1.1 riastrad 653 1.2 riastrad ret = drm_legacy_addmap(dev, aperture_base, SAVAGE_APERTURE_SIZE, 654 1.2 riastrad _DRM_FRAME_BUFFER, _DRM_WRITE_COMBINING, 655 1.2 riastrad &dev_priv->aperture); 656 1.1 riastrad return ret; 657 1.1 riastrad } 658 1.1 riastrad 659 1.1 riastrad /* 660 1.1 riastrad * Delete MTRRs and free device-private data. 661 1.1 riastrad */ 662 1.1 riastrad void savage_driver_lastclose(struct drm_device *dev) 663 1.1 riastrad { 664 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 665 1.1 riastrad int i; 666 1.1 riastrad 667 1.2 riastrad for (i = 0; i < 3; ++i) { 668 1.2 riastrad arch_phys_wc_del(dev_priv->mtrr_handles[i]); 669 1.2 riastrad dev_priv->mtrr_handles[i] = 0; 670 1.2 riastrad } 671 1.1 riastrad } 672 1.1 riastrad 673 1.3 riastrad void savage_driver_unload(struct drm_device *dev) 674 1.1 riastrad { 675 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 676 1.1 riastrad 677 1.1 riastrad kfree(dev_priv); 678 1.1 riastrad } 679 1.1 riastrad 680 1.1 riastrad static int savage_do_init_bci(struct drm_device * dev, drm_savage_init_t * init) 681 1.1 riastrad { 682 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 683 1.1 riastrad 684 1.1 riastrad if (init->fb_bpp != 16 && init->fb_bpp != 32) { 685 1.1 riastrad DRM_ERROR("invalid frame buffer bpp %d!\n", init->fb_bpp); 686 1.1 riastrad return -EINVAL; 687 1.1 riastrad } 688 1.1 riastrad if (init->depth_bpp != 16 && init->depth_bpp != 32) { 689 1.1 riastrad DRM_ERROR("invalid depth buffer bpp %d!\n", init->fb_bpp); 690 1.1 riastrad return -EINVAL; 691 1.1 riastrad } 692 1.1 riastrad if (init->dma_type != SAVAGE_DMA_AGP && 693 1.1 riastrad init->dma_type != SAVAGE_DMA_PCI) { 694 1.1 riastrad DRM_ERROR("invalid dma memory type %d!\n", init->dma_type); 695 1.1 riastrad return -EINVAL; 696 1.1 riastrad } 697 1.1 riastrad 698 1.1 riastrad dev_priv->cob_size = init->cob_size; 699 1.1 riastrad dev_priv->bci_threshold_lo = init->bci_threshold_lo; 700 1.1 riastrad dev_priv->bci_threshold_hi = init->bci_threshold_hi; 701 1.1 riastrad dev_priv->dma_type = init->dma_type; 702 1.1 riastrad 703 1.1 riastrad dev_priv->fb_bpp = init->fb_bpp; 704 1.1 riastrad dev_priv->front_offset = init->front_offset; 705 1.1 riastrad dev_priv->front_pitch = init->front_pitch; 706 1.1 riastrad dev_priv->back_offset = init->back_offset; 707 1.1 riastrad dev_priv->back_pitch = init->back_pitch; 708 1.1 riastrad dev_priv->depth_bpp = init->depth_bpp; 709 1.1 riastrad dev_priv->depth_offset = init->depth_offset; 710 1.1 riastrad dev_priv->depth_pitch = init->depth_pitch; 711 1.1 riastrad 712 1.1 riastrad dev_priv->texture_offset = init->texture_offset; 713 1.1 riastrad dev_priv->texture_size = init->texture_size; 714 1.1 riastrad 715 1.2 riastrad dev_priv->sarea = drm_legacy_getsarea(dev); 716 1.1 riastrad if (!dev_priv->sarea) { 717 1.1 riastrad DRM_ERROR("could not find sarea!\n"); 718 1.1 riastrad savage_do_cleanup_bci(dev); 719 1.1 riastrad return -EINVAL; 720 1.1 riastrad } 721 1.1 riastrad if (init->status_offset != 0) { 722 1.2 riastrad dev_priv->status = drm_legacy_findmap(dev, init->status_offset); 723 1.1 riastrad if (!dev_priv->status) { 724 1.1 riastrad DRM_ERROR("could not find shadow status region!\n"); 725 1.1 riastrad savage_do_cleanup_bci(dev); 726 1.1 riastrad return -EINVAL; 727 1.1 riastrad } 728 1.1 riastrad } else { 729 1.1 riastrad dev_priv->status = NULL; 730 1.1 riastrad } 731 1.1 riastrad if (dev_priv->dma_type == SAVAGE_DMA_AGP && init->buffers_offset) { 732 1.1 riastrad dev->agp_buffer_token = init->buffers_offset; 733 1.2 riastrad dev->agp_buffer_map = drm_legacy_findmap(dev, 734 1.1 riastrad init->buffers_offset); 735 1.1 riastrad if (!dev->agp_buffer_map) { 736 1.1 riastrad DRM_ERROR("could not find DMA buffer region!\n"); 737 1.1 riastrad savage_do_cleanup_bci(dev); 738 1.1 riastrad return -EINVAL; 739 1.1 riastrad } 740 1.2 riastrad drm_legacy_ioremap(dev->agp_buffer_map, dev); 741 1.1 riastrad if (!dev->agp_buffer_map->handle) { 742 1.1 riastrad DRM_ERROR("failed to ioremap DMA buffer region!\n"); 743 1.1 riastrad savage_do_cleanup_bci(dev); 744 1.1 riastrad return -ENOMEM; 745 1.1 riastrad } 746 1.1 riastrad } 747 1.1 riastrad if (init->agp_textures_offset) { 748 1.1 riastrad dev_priv->agp_textures = 749 1.2 riastrad drm_legacy_findmap(dev, init->agp_textures_offset); 750 1.1 riastrad if (!dev_priv->agp_textures) { 751 1.1 riastrad DRM_ERROR("could not find agp texture region!\n"); 752 1.1 riastrad savage_do_cleanup_bci(dev); 753 1.1 riastrad return -EINVAL; 754 1.1 riastrad } 755 1.1 riastrad } else { 756 1.1 riastrad dev_priv->agp_textures = NULL; 757 1.1 riastrad } 758 1.1 riastrad 759 1.1 riastrad if (init->cmd_dma_offset) { 760 1.1 riastrad if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) { 761 1.1 riastrad DRM_ERROR("command DMA not supported on " 762 1.1 riastrad "Savage3D/MX/IX.\n"); 763 1.1 riastrad savage_do_cleanup_bci(dev); 764 1.1 riastrad return -EINVAL; 765 1.1 riastrad } 766 1.1 riastrad if (dev->dma && dev->dma->buflist) { 767 1.1 riastrad DRM_ERROR("command and vertex DMA not supported " 768 1.1 riastrad "at the same time.\n"); 769 1.1 riastrad savage_do_cleanup_bci(dev); 770 1.1 riastrad return -EINVAL; 771 1.1 riastrad } 772 1.2 riastrad dev_priv->cmd_dma = drm_legacy_findmap(dev, init->cmd_dma_offset); 773 1.1 riastrad if (!dev_priv->cmd_dma) { 774 1.1 riastrad DRM_ERROR("could not find command DMA region!\n"); 775 1.1 riastrad savage_do_cleanup_bci(dev); 776 1.1 riastrad return -EINVAL; 777 1.1 riastrad } 778 1.1 riastrad if (dev_priv->dma_type == SAVAGE_DMA_AGP) { 779 1.1 riastrad if (dev_priv->cmd_dma->type != _DRM_AGP) { 780 1.1 riastrad DRM_ERROR("AGP command DMA region is not a " 781 1.1 riastrad "_DRM_AGP map!\n"); 782 1.1 riastrad savage_do_cleanup_bci(dev); 783 1.1 riastrad return -EINVAL; 784 1.1 riastrad } 785 1.2 riastrad drm_legacy_ioremap(dev_priv->cmd_dma, dev); 786 1.1 riastrad if (!dev_priv->cmd_dma->handle) { 787 1.1 riastrad DRM_ERROR("failed to ioremap command " 788 1.1 riastrad "DMA region!\n"); 789 1.1 riastrad savage_do_cleanup_bci(dev); 790 1.1 riastrad return -ENOMEM; 791 1.1 riastrad } 792 1.1 riastrad } else if (dev_priv->cmd_dma->type != _DRM_CONSISTENT) { 793 1.1 riastrad DRM_ERROR("PCI command DMA region is not a " 794 1.1 riastrad "_DRM_CONSISTENT map!\n"); 795 1.1 riastrad savage_do_cleanup_bci(dev); 796 1.1 riastrad return -EINVAL; 797 1.1 riastrad } 798 1.1 riastrad } else { 799 1.1 riastrad dev_priv->cmd_dma = NULL; 800 1.1 riastrad } 801 1.1 riastrad 802 1.1 riastrad dev_priv->dma_flush = savage_dma_flush; 803 1.1 riastrad if (!dev_priv->cmd_dma) { 804 1.1 riastrad DRM_DEBUG("falling back to faked command DMA.\n"); 805 1.1 riastrad dev_priv->fake_dma.offset = 0; 806 1.1 riastrad dev_priv->fake_dma.size = SAVAGE_FAKE_DMA_SIZE; 807 1.1 riastrad dev_priv->fake_dma.type = _DRM_SHM; 808 1.1 riastrad dev_priv->fake_dma.handle = kmalloc(SAVAGE_FAKE_DMA_SIZE, 809 1.1 riastrad GFP_KERNEL); 810 1.1 riastrad if (!dev_priv->fake_dma.handle) { 811 1.1 riastrad DRM_ERROR("could not allocate faked DMA buffer!\n"); 812 1.1 riastrad savage_do_cleanup_bci(dev); 813 1.1 riastrad return -ENOMEM; 814 1.1 riastrad } 815 1.1 riastrad dev_priv->cmd_dma = &dev_priv->fake_dma; 816 1.1 riastrad dev_priv->dma_flush = savage_fake_dma_flush; 817 1.1 riastrad } 818 1.1 riastrad 819 1.1 riastrad dev_priv->sarea_priv = 820 1.1 riastrad (drm_savage_sarea_t *) ((uint8_t *) dev_priv->sarea->handle + 821 1.1 riastrad init->sarea_priv_offset); 822 1.1 riastrad 823 1.1 riastrad /* setup bitmap descriptors */ 824 1.1 riastrad { 825 1.1 riastrad unsigned int color_tile_format; 826 1.1 riastrad unsigned int depth_tile_format; 827 1.1 riastrad unsigned int front_stride, back_stride, depth_stride; 828 1.1 riastrad if (dev_priv->chipset <= S3_SAVAGE4) { 829 1.1 riastrad color_tile_format = dev_priv->fb_bpp == 16 ? 830 1.1 riastrad SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP; 831 1.1 riastrad depth_tile_format = dev_priv->depth_bpp == 16 ? 832 1.1 riastrad SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP; 833 1.1 riastrad } else { 834 1.1 riastrad color_tile_format = SAVAGE_BD_TILE_DEST; 835 1.1 riastrad depth_tile_format = SAVAGE_BD_TILE_DEST; 836 1.1 riastrad } 837 1.1 riastrad front_stride = dev_priv->front_pitch / (dev_priv->fb_bpp / 8); 838 1.1 riastrad back_stride = dev_priv->back_pitch / (dev_priv->fb_bpp / 8); 839 1.1 riastrad depth_stride = 840 1.1 riastrad dev_priv->depth_pitch / (dev_priv->depth_bpp / 8); 841 1.1 riastrad 842 1.1 riastrad dev_priv->front_bd = front_stride | SAVAGE_BD_BW_DISABLE | 843 1.1 riastrad (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) | 844 1.1 riastrad (color_tile_format << SAVAGE_BD_TILE_SHIFT); 845 1.1 riastrad 846 1.1 riastrad dev_priv->back_bd = back_stride | SAVAGE_BD_BW_DISABLE | 847 1.1 riastrad (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) | 848 1.1 riastrad (color_tile_format << SAVAGE_BD_TILE_SHIFT); 849 1.1 riastrad 850 1.1 riastrad dev_priv->depth_bd = depth_stride | SAVAGE_BD_BW_DISABLE | 851 1.1 riastrad (dev_priv->depth_bpp << SAVAGE_BD_BPP_SHIFT) | 852 1.1 riastrad (depth_tile_format << SAVAGE_BD_TILE_SHIFT); 853 1.1 riastrad } 854 1.1 riastrad 855 1.1 riastrad /* setup status and bci ptr */ 856 1.1 riastrad dev_priv->event_counter = 0; 857 1.1 riastrad dev_priv->event_wrap = 0; 858 1.1 riastrad dev_priv->bci_ptr = (volatile uint32_t *) 859 1.1 riastrad ((uint8_t *) dev_priv->mmio->handle + SAVAGE_BCI_OFFSET); 860 1.1 riastrad if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) { 861 1.1 riastrad dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S3D; 862 1.1 riastrad } else { 863 1.1 riastrad dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S4; 864 1.1 riastrad } 865 1.1 riastrad if (dev_priv->status != NULL) { 866 1.1 riastrad dev_priv->status_ptr = 867 1.1 riastrad (volatile uint32_t *)dev_priv->status->handle; 868 1.1 riastrad dev_priv->wait_fifo = savage_bci_wait_fifo_shadow; 869 1.1 riastrad dev_priv->wait_evnt = savage_bci_wait_event_shadow; 870 1.1 riastrad dev_priv->status_ptr[1023] = dev_priv->event_counter; 871 1.1 riastrad } else { 872 1.1 riastrad dev_priv->status_ptr = NULL; 873 1.1 riastrad if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) { 874 1.1 riastrad dev_priv->wait_fifo = savage_bci_wait_fifo_s3d; 875 1.1 riastrad } else { 876 1.1 riastrad dev_priv->wait_fifo = savage_bci_wait_fifo_s4; 877 1.1 riastrad } 878 1.1 riastrad dev_priv->wait_evnt = savage_bci_wait_event_reg; 879 1.1 riastrad } 880 1.1 riastrad 881 1.1 riastrad /* cliprect functions */ 882 1.1 riastrad if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) 883 1.1 riastrad dev_priv->emit_clip_rect = savage_emit_clip_rect_s3d; 884 1.1 riastrad else 885 1.1 riastrad dev_priv->emit_clip_rect = savage_emit_clip_rect_s4; 886 1.1 riastrad 887 1.1 riastrad if (savage_freelist_init(dev) < 0) { 888 1.1 riastrad DRM_ERROR("could not initialize freelist\n"); 889 1.1 riastrad savage_do_cleanup_bci(dev); 890 1.1 riastrad return -ENOMEM; 891 1.1 riastrad } 892 1.1 riastrad 893 1.1 riastrad if (savage_dma_init(dev_priv) < 0) { 894 1.1 riastrad DRM_ERROR("could not initialize command DMA\n"); 895 1.1 riastrad savage_do_cleanup_bci(dev); 896 1.1 riastrad return -ENOMEM; 897 1.1 riastrad } 898 1.1 riastrad 899 1.1 riastrad return 0; 900 1.1 riastrad } 901 1.1 riastrad 902 1.1 riastrad static int savage_do_cleanup_bci(struct drm_device * dev) 903 1.1 riastrad { 904 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 905 1.1 riastrad 906 1.1 riastrad if (dev_priv->cmd_dma == &dev_priv->fake_dma) { 907 1.1 riastrad kfree(dev_priv->fake_dma.handle); 908 1.1 riastrad } else if (dev_priv->cmd_dma && dev_priv->cmd_dma->handle && 909 1.1 riastrad dev_priv->cmd_dma->type == _DRM_AGP && 910 1.1 riastrad dev_priv->dma_type == SAVAGE_DMA_AGP) 911 1.2 riastrad drm_legacy_ioremapfree(dev_priv->cmd_dma, dev); 912 1.1 riastrad 913 1.1 riastrad if (dev_priv->dma_type == SAVAGE_DMA_AGP && 914 1.1 riastrad dev->agp_buffer_map && dev->agp_buffer_map->handle) { 915 1.2 riastrad drm_legacy_ioremapfree(dev->agp_buffer_map, dev); 916 1.1 riastrad /* make sure the next instance (which may be running 917 1.1 riastrad * in PCI mode) doesn't try to use an old 918 1.1 riastrad * agp_buffer_map. */ 919 1.1 riastrad dev->agp_buffer_map = NULL; 920 1.1 riastrad } 921 1.1 riastrad 922 1.1 riastrad kfree(dev_priv->dma_pages); 923 1.1 riastrad 924 1.1 riastrad return 0; 925 1.1 riastrad } 926 1.1 riastrad 927 1.1 riastrad static int savage_bci_init(struct drm_device *dev, void *data, struct drm_file *file_priv) 928 1.1 riastrad { 929 1.1 riastrad drm_savage_init_t *init = data; 930 1.1 riastrad 931 1.1 riastrad LOCK_TEST_WITH_RETURN(dev, file_priv); 932 1.1 riastrad 933 1.1 riastrad switch (init->func) { 934 1.1 riastrad case SAVAGE_INIT_BCI: 935 1.1 riastrad return savage_do_init_bci(dev, init); 936 1.1 riastrad case SAVAGE_CLEANUP_BCI: 937 1.1 riastrad return savage_do_cleanup_bci(dev); 938 1.1 riastrad } 939 1.1 riastrad 940 1.1 riastrad return -EINVAL; 941 1.1 riastrad } 942 1.1 riastrad 943 1.1 riastrad static int savage_bci_event_emit(struct drm_device *dev, void *data, struct drm_file *file_priv) 944 1.1 riastrad { 945 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 946 1.1 riastrad drm_savage_event_emit_t *event = data; 947 1.1 riastrad 948 1.1 riastrad DRM_DEBUG("\n"); 949 1.1 riastrad 950 1.1 riastrad LOCK_TEST_WITH_RETURN(dev, file_priv); 951 1.1 riastrad 952 1.1 riastrad event->count = savage_bci_emit_event(dev_priv, event->flags); 953 1.1 riastrad event->count |= dev_priv->event_wrap << 16; 954 1.1 riastrad 955 1.1 riastrad return 0; 956 1.1 riastrad } 957 1.1 riastrad 958 1.1 riastrad static int savage_bci_event_wait(struct drm_device *dev, void *data, struct drm_file *file_priv) 959 1.1 riastrad { 960 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 961 1.1 riastrad drm_savage_event_wait_t *event = data; 962 1.1 riastrad unsigned int event_e, hw_e; 963 1.1 riastrad unsigned int event_w, hw_w; 964 1.1 riastrad 965 1.1 riastrad DRM_DEBUG("\n"); 966 1.1 riastrad 967 1.1 riastrad UPDATE_EVENT_COUNTER(); 968 1.1 riastrad if (dev_priv->status_ptr) 969 1.1 riastrad hw_e = dev_priv->status_ptr[1] & 0xffff; 970 1.1 riastrad else 971 1.1 riastrad hw_e = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff; 972 1.1 riastrad hw_w = dev_priv->event_wrap; 973 1.1 riastrad if (hw_e > dev_priv->event_counter) 974 1.1 riastrad hw_w--; /* hardware hasn't passed the last wrap yet */ 975 1.1 riastrad 976 1.1 riastrad event_e = event->count & 0xffff; 977 1.1 riastrad event_w = event->count >> 16; 978 1.1 riastrad 979 1.1 riastrad /* Don't need to wait if 980 1.1 riastrad * - event counter wrapped since the event was emitted or 981 1.1 riastrad * - the hardware has advanced up to or over the event to wait for. 982 1.1 riastrad */ 983 1.1 riastrad if (event_w < hw_w || (event_w == hw_w && event_e <= hw_e)) 984 1.1 riastrad return 0; 985 1.1 riastrad else 986 1.1 riastrad return dev_priv->wait_evnt(dev_priv, event_e); 987 1.1 riastrad } 988 1.1 riastrad 989 1.1 riastrad /* 990 1.1 riastrad * DMA buffer management 991 1.1 riastrad */ 992 1.1 riastrad 993 1.1 riastrad static int savage_bci_get_buffers(struct drm_device *dev, 994 1.1 riastrad struct drm_file *file_priv, 995 1.1 riastrad struct drm_dma *d) 996 1.1 riastrad { 997 1.1 riastrad struct drm_buf *buf; 998 1.1 riastrad int i; 999 1.1 riastrad 1000 1.1 riastrad for (i = d->granted_count; i < d->request_count; i++) { 1001 1.1 riastrad buf = savage_freelist_get(dev); 1002 1.1 riastrad if (!buf) 1003 1.1 riastrad return -EAGAIN; 1004 1.1 riastrad 1005 1.1 riastrad buf->file_priv = file_priv; 1006 1.1 riastrad 1007 1.2 riastrad if (copy_to_user(&d->request_indices[i], 1008 1.1 riastrad &buf->idx, sizeof(buf->idx))) 1009 1.1 riastrad return -EFAULT; 1010 1.2 riastrad if (copy_to_user(&d->request_sizes[i], 1011 1.1 riastrad &buf->total, sizeof(buf->total))) 1012 1.1 riastrad return -EFAULT; 1013 1.1 riastrad 1014 1.1 riastrad d->granted_count++; 1015 1.1 riastrad } 1016 1.1 riastrad return 0; 1017 1.1 riastrad } 1018 1.1 riastrad 1019 1.1 riastrad int savage_bci_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv) 1020 1.1 riastrad { 1021 1.1 riastrad struct drm_device_dma *dma = dev->dma; 1022 1.1 riastrad struct drm_dma *d = data; 1023 1.1 riastrad int ret = 0; 1024 1.1 riastrad 1025 1.1 riastrad LOCK_TEST_WITH_RETURN(dev, file_priv); 1026 1.1 riastrad 1027 1.1 riastrad /* Please don't send us buffers. 1028 1.1 riastrad */ 1029 1.1 riastrad if (d->send_count != 0) { 1030 1.1 riastrad DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n", 1031 1.3 riastrad task_pid_nr(current), d->send_count); 1032 1.1 riastrad return -EINVAL; 1033 1.1 riastrad } 1034 1.1 riastrad 1035 1.1 riastrad /* We'll send you buffers. 1036 1.1 riastrad */ 1037 1.1 riastrad if (d->request_count < 0 || d->request_count > dma->buf_count) { 1038 1.1 riastrad DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n", 1039 1.3 riastrad task_pid_nr(current), d->request_count, dma->buf_count); 1040 1.1 riastrad return -EINVAL; 1041 1.1 riastrad } 1042 1.1 riastrad 1043 1.1 riastrad d->granted_count = 0; 1044 1.1 riastrad 1045 1.1 riastrad if (d->request_count) { 1046 1.1 riastrad ret = savage_bci_get_buffers(dev, file_priv, d); 1047 1.1 riastrad } 1048 1.1 riastrad 1049 1.1 riastrad return ret; 1050 1.1 riastrad } 1051 1.1 riastrad 1052 1.1 riastrad void savage_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv) 1053 1.1 riastrad { 1054 1.1 riastrad struct drm_device_dma *dma = dev->dma; 1055 1.1 riastrad drm_savage_private_t *dev_priv = dev->dev_private; 1056 1.1 riastrad int release_idlelock = 0; 1057 1.1 riastrad int i; 1058 1.1 riastrad 1059 1.1 riastrad if (!dma) 1060 1.1 riastrad return; 1061 1.1 riastrad if (!dev_priv) 1062 1.1 riastrad return; 1063 1.1 riastrad if (!dma->buflist) 1064 1.1 riastrad return; 1065 1.1 riastrad 1066 1.1 riastrad if (file_priv->master && file_priv->master->lock.hw_lock) { 1067 1.2 riastrad drm_legacy_idlelock_take(&file_priv->master->lock); 1068 1.1 riastrad release_idlelock = 1; 1069 1.1 riastrad } 1070 1.1 riastrad 1071 1.1 riastrad for (i = 0; i < dma->buf_count; i++) { 1072 1.1 riastrad struct drm_buf *buf = dma->buflist[i]; 1073 1.1 riastrad drm_savage_buf_priv_t *buf_priv = buf->dev_private; 1074 1.1 riastrad 1075 1.1 riastrad if (buf->file_priv == file_priv && buf_priv && 1076 1.1 riastrad buf_priv->next == NULL && buf_priv->prev == NULL) { 1077 1.1 riastrad uint16_t event; 1078 1.1 riastrad DRM_DEBUG("reclaimed from client\n"); 1079 1.1 riastrad event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D); 1080 1.1 riastrad SET_AGE(&buf_priv->age, event, dev_priv->event_wrap); 1081 1.1 riastrad savage_freelist_put(dev, buf); 1082 1.1 riastrad } 1083 1.1 riastrad } 1084 1.1 riastrad 1085 1.1 riastrad if (release_idlelock) 1086 1.2 riastrad drm_legacy_idlelock_release(&file_priv->master->lock); 1087 1.1 riastrad } 1088 1.1 riastrad 1089 1.2 riastrad const struct drm_ioctl_desc savage_ioctls[] = { 1090 1.1 riastrad DRM_IOCTL_DEF_DRV(SAVAGE_BCI_INIT, savage_bci_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY), 1091 1.1 riastrad DRM_IOCTL_DEF_DRV(SAVAGE_BCI_CMDBUF, savage_bci_cmdbuf, DRM_AUTH), 1092 1.1 riastrad DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_EMIT, savage_bci_event_emit, DRM_AUTH), 1093 1.1 riastrad DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_WAIT, savage_bci_event_wait, DRM_AUTH), 1094 1.1 riastrad }; 1095 1.1 riastrad 1096 1.2 riastrad int savage_max_ioctl = ARRAY_SIZE(savage_ioctls); 1097