blob: d47dff95fe5283a22e3736d33c399d9adecfb357 [file] [log] [blame]
Dave Airlie282a1672005-08-07 15:43:54 +10001/* savage_bci.c -- BCI support for Savage
2 *
3 * Copyright 2004 Felix Kuehling
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NON-INFRINGEMENT. IN NO EVENT SHALL FELIX KUEHLING BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
22 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
David Howells760285e2012-10-02 18:01:07 +010025#include <drm/drmP.h>
26#include <drm/savage_drm.h>
Dave Airlie282a1672005-08-07 15:43:54 +100027#include "savage_drv.h"
28
29/* Need a long timeout for shadow status updates can take a while
30 * and so can waiting for events when the queue is full. */
Dave Airlieb5e89ed2005-09-25 14:28:13 +100031#define SAVAGE_DEFAULT_USEC_TIMEOUT 1000000 /* 1s */
32#define SAVAGE_EVENT_USEC_TIMEOUT 5000000 /* 5s */
Dave Airlie282a1672005-08-07 15:43:54 +100033#define SAVAGE_FREELIST_DEBUG 0
34
Dave Airlieeddca552007-07-11 16:09:54 +100035static int savage_do_cleanup_bci(struct drm_device *dev);
Dave Airliece60fe02006-02-02 19:21:38 +110036
Dave Airlie282a1672005-08-07 15:43:54 +100037static int
Dave Airlieb5e89ed2005-09-25 14:28:13 +100038savage_bci_wait_fifo_shadow(drm_savage_private_t * dev_priv, unsigned int n)
Dave Airlie282a1672005-08-07 15:43:54 +100039{
40 uint32_t mask = dev_priv->status_used_mask;
41 uint32_t threshold = dev_priv->bci_threshold_hi;
42 uint32_t status;
43 int i;
44
45#if SAVAGE_BCI_DEBUG
46 if (n > dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - threshold)
47 DRM_ERROR("Trying to emit %d words "
48 "(more than guaranteed space in COB)\n", n);
49#endif
50
51 for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
Daniel Vetter85b23312013-12-11 11:34:45 +010052 mb();
Dave Airlie282a1672005-08-07 15:43:54 +100053 status = dev_priv->status_ptr[0];
54 if ((status & mask) < threshold)
55 return 0;
56 DRM_UDELAY(1);
57 }
58
59#if SAVAGE_BCI_DEBUG
60 DRM_ERROR("failed!\n");
61 DRM_INFO(" status=0x%08x, threshold=0x%08x\n", status, threshold);
62#endif
Eric Anholt20caafa2007-08-25 19:22:43 +100063 return -EBUSY;
Dave Airlie282a1672005-08-07 15:43:54 +100064}
65
66static int
Dave Airlieb5e89ed2005-09-25 14:28:13 +100067savage_bci_wait_fifo_s3d(drm_savage_private_t * dev_priv, unsigned int n)
Dave Airlie282a1672005-08-07 15:43:54 +100068{
69 uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n;
70 uint32_t status;
71 int i;
72
73 for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
74 status = SAVAGE_READ(SAVAGE_STATUS_WORD0);
75 if ((status & SAVAGE_FIFO_USED_MASK_S3D) <= maxUsed)
76 return 0;
77 DRM_UDELAY(1);
78 }
79
80#if SAVAGE_BCI_DEBUG
81 DRM_ERROR("failed!\n");
82 DRM_INFO(" status=0x%08x\n", status);
83#endif
Eric Anholt20caafa2007-08-25 19:22:43 +100084 return -EBUSY;
Dave Airlie282a1672005-08-07 15:43:54 +100085}
86
87static int
Dave Airlieb5e89ed2005-09-25 14:28:13 +100088savage_bci_wait_fifo_s4(drm_savage_private_t * dev_priv, unsigned int n)
Dave Airlie282a1672005-08-07 15:43:54 +100089{
90 uint32_t maxUsed = dev_priv->cob_size + SAVAGE_BCI_FIFO_SIZE - n;
91 uint32_t status;
92 int i;
93
94 for (i = 0; i < SAVAGE_DEFAULT_USEC_TIMEOUT; i++) {
95 status = SAVAGE_READ(SAVAGE_ALT_STATUS_WORD0);
96 if ((status & SAVAGE_FIFO_USED_MASK_S4) <= maxUsed)
97 return 0;
98 DRM_UDELAY(1);
99 }
100
101#if SAVAGE_BCI_DEBUG
102 DRM_ERROR("failed!\n");
103 DRM_INFO(" status=0x%08x\n", status);
104#endif
Eric Anholt20caafa2007-08-25 19:22:43 +1000105 return -EBUSY;
Dave Airlie282a1672005-08-07 15:43:54 +1000106}
107
108/*
109 * Waiting for events.
110 *
111 * The BIOSresets the event tag to 0 on mode changes. Therefore we
112 * never emit 0 to the event tag. If we find a 0 event tag we know the
113 * BIOS stomped on it and return success assuming that the BIOS waited
114 * for engine idle.
115 *
116 * Note: if the Xserver uses the event tag it has to follow the same
117 * rule. Otherwise there may be glitches every 2^16 events.
118 */
119static int
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000120savage_bci_wait_event_shadow(drm_savage_private_t * dev_priv, uint16_t e)
Dave Airlie282a1672005-08-07 15:43:54 +1000121{
122 uint32_t status;
123 int i;
124
125 for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) {
Daniel Vetter85b23312013-12-11 11:34:45 +0100126 mb();
Dave Airlie282a1672005-08-07 15:43:54 +1000127 status = dev_priv->status_ptr[1];
128 if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff ||
129 (status & 0xffff) == 0)
130 return 0;
131 DRM_UDELAY(1);
132 }
133
134#if SAVAGE_BCI_DEBUG
135 DRM_ERROR("failed!\n");
136 DRM_INFO(" status=0x%08x, e=0x%04x\n", status, e);
137#endif
138
Eric Anholt20caafa2007-08-25 19:22:43 +1000139 return -EBUSY;
Dave Airlie282a1672005-08-07 15:43:54 +1000140}
141
142static int
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000143savage_bci_wait_event_reg(drm_savage_private_t * dev_priv, uint16_t e)
Dave Airlie282a1672005-08-07 15:43:54 +1000144{
145 uint32_t status;
146 int i;
147
148 for (i = 0; i < SAVAGE_EVENT_USEC_TIMEOUT; i++) {
149 status = SAVAGE_READ(SAVAGE_STATUS_WORD1);
150 if ((((status & 0xffff) - e) & 0xffff) <= 0x7fff ||
151 (status & 0xffff) == 0)
152 return 0;
153 DRM_UDELAY(1);
154 }
155
156#if SAVAGE_BCI_DEBUG
157 DRM_ERROR("failed!\n");
158 DRM_INFO(" status=0x%08x, e=0x%04x\n", status, e);
159#endif
160
Eric Anholt20caafa2007-08-25 19:22:43 +1000161 return -EBUSY;
Dave Airlie282a1672005-08-07 15:43:54 +1000162}
163
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000164uint16_t savage_bci_emit_event(drm_savage_private_t * dev_priv,
Dave Airlie282a1672005-08-07 15:43:54 +1000165 unsigned int flags)
166{
167 uint16_t count;
168 BCI_LOCALS;
169
170 if (dev_priv->status_ptr) {
171 /* coordinate with Xserver */
172 count = dev_priv->status_ptr[1023];
173 if (count < dev_priv->event_counter)
174 dev_priv->event_wrap++;
175 } else {
176 count = dev_priv->event_counter;
177 }
178 count = (count + 1) & 0xffff;
179 if (count == 0) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000180 count++; /* See the comment above savage_wait_event_*. */
Dave Airlie282a1672005-08-07 15:43:54 +1000181 dev_priv->event_wrap++;
182 }
183 dev_priv->event_counter = count;
184 if (dev_priv->status_ptr)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000185 dev_priv->status_ptr[1023] = (uint32_t) count;
Dave Airlie282a1672005-08-07 15:43:54 +1000186
187 if ((flags & (SAVAGE_WAIT_2D | SAVAGE_WAIT_3D))) {
188 unsigned int wait_cmd = BCI_CMD_WAIT;
189 if ((flags & SAVAGE_WAIT_2D))
190 wait_cmd |= BCI_CMD_WAIT_2D;
191 if ((flags & SAVAGE_WAIT_3D))
192 wait_cmd |= BCI_CMD_WAIT_3D;
193 BEGIN_BCI(2);
194 BCI_WRITE(wait_cmd);
195 } else {
196 BEGIN_BCI(1);
197 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000198 BCI_WRITE(BCI_CMD_UPDATE_EVENT_TAG | (uint32_t) count);
Dave Airlie282a1672005-08-07 15:43:54 +1000199
200 return count;
201}
202
203/*
204 * Freelist management
205 */
Dave Airlieeddca552007-07-11 16:09:54 +1000206static int savage_freelist_init(struct drm_device * dev)
Dave Airlie282a1672005-08-07 15:43:54 +1000207{
208 drm_savage_private_t *dev_priv = dev->dev_private;
Dave Airliecdd55a22007-07-11 16:32:08 +1000209 struct drm_device_dma *dma = dev->dma;
Dave Airlie056219e2007-07-11 16:17:42 +1000210 struct drm_buf *buf;
Dave Airlie282a1672005-08-07 15:43:54 +1000211 drm_savage_buf_priv_t *entry;
212 int i;
213 DRM_DEBUG("count=%d\n", dma->buf_count);
214
215 dev_priv->head.next = &dev_priv->tail;
216 dev_priv->head.prev = NULL;
217 dev_priv->head.buf = NULL;
218
219 dev_priv->tail.next = NULL;
220 dev_priv->tail.prev = &dev_priv->head;
221 dev_priv->tail.buf = NULL;
222
223 for (i = 0; i < dma->buf_count; i++) {
224 buf = dma->buflist[i];
225 entry = buf->dev_private;
226
227 SET_AGE(&entry->age, 0, 0);
228 entry->buf = buf;
229
230 entry->next = dev_priv->head.next;
231 entry->prev = &dev_priv->head;
232 dev_priv->head.next->prev = entry;
233 dev_priv->head.next = entry;
234 }
235
236 return 0;
237}
238
Dave Airlie056219e2007-07-11 16:17:42 +1000239static struct drm_buf *savage_freelist_get(struct drm_device * dev)
Dave Airlie282a1672005-08-07 15:43:54 +1000240{
241 drm_savage_private_t *dev_priv = dev->dev_private;
242 drm_savage_buf_priv_t *tail = dev_priv->tail.prev;
243 uint16_t event;
244 unsigned int wrap;
245 DRM_DEBUG("\n");
246
247 UPDATE_EVENT_COUNTER();
248 if (dev_priv->status_ptr)
249 event = dev_priv->status_ptr[1] & 0xffff;
250 else
251 event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
252 wrap = dev_priv->event_wrap;
253 if (event > dev_priv->event_counter)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000254 wrap--; /* hardware hasn't passed the last wrap yet */
Dave Airlie282a1672005-08-07 15:43:54 +1000255
256 DRM_DEBUG(" tail=0x%04x %d\n", tail->age.event, tail->age.wrap);
257 DRM_DEBUG(" head=0x%04x %d\n", event, wrap);
258
259 if (tail->buf && (TEST_AGE(&tail->age, event, wrap) || event == 0)) {
260 drm_savage_buf_priv_t *next = tail->next;
261 drm_savage_buf_priv_t *prev = tail->prev;
262 prev->next = next;
263 next->prev = prev;
264 tail->next = tail->prev = NULL;
265 return tail->buf;
266 }
267
268 DRM_DEBUG("returning NULL, tail->buf=%p!\n", tail->buf);
269 return NULL;
270}
271
Dave Airlie056219e2007-07-11 16:17:42 +1000272void savage_freelist_put(struct drm_device * dev, struct drm_buf * buf)
Dave Airlie282a1672005-08-07 15:43:54 +1000273{
274 drm_savage_private_t *dev_priv = dev->dev_private;
275 drm_savage_buf_priv_t *entry = buf->dev_private, *prev, *next;
276
277 DRM_DEBUG("age=0x%04x wrap=%d\n", entry->age.event, entry->age.wrap);
278
279 if (entry->next != NULL || entry->prev != NULL) {
280 DRM_ERROR("entry already on freelist.\n");
281 return;
282 }
283
284 prev = &dev_priv->head;
285 next = prev->next;
286 prev->next = entry;
287 next->prev = entry;
288 entry->prev = prev;
289 entry->next = next;
290}
291
292/*
293 * Command DMA
294 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295static int savage_dma_init(drm_savage_private_t * dev_priv)
Dave Airlie282a1672005-08-07 15:43:54 +1000296{
297 unsigned int i;
298
299 dev_priv->nr_dma_pages = dev_priv->cmd_dma->size /
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000300 (SAVAGE_DMA_PAGE_SIZE * 4);
Eric Anholt9a298b22009-03-24 12:23:04 -0700301 dev_priv->dma_pages = kmalloc(sizeof(drm_savage_dma_page_t) *
302 dev_priv->nr_dma_pages, GFP_KERNEL);
Dave Airlie282a1672005-08-07 15:43:54 +1000303 if (dev_priv->dma_pages == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000304 return -ENOMEM;
Dave Airlie282a1672005-08-07 15:43:54 +1000305
306 for (i = 0; i < dev_priv->nr_dma_pages; ++i) {
307 SET_AGE(&dev_priv->dma_pages[i].age, 0, 0);
308 dev_priv->dma_pages[i].used = 0;
309 dev_priv->dma_pages[i].flushed = 0;
310 }
311 SET_AGE(&dev_priv->last_dma_age, 0, 0);
312
313 dev_priv->first_dma_page = 0;
314 dev_priv->current_dma_page = 0;
315
316 return 0;
317}
318
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000319void savage_dma_reset(drm_savage_private_t * dev_priv)
Dave Airlie282a1672005-08-07 15:43:54 +1000320{
321 uint16_t event;
322 unsigned int wrap, i;
323 event = savage_bci_emit_event(dev_priv, 0);
324 wrap = dev_priv->event_wrap;
325 for (i = 0; i < dev_priv->nr_dma_pages; ++i) {
326 SET_AGE(&dev_priv->dma_pages[i].age, event, wrap);
327 dev_priv->dma_pages[i].used = 0;
328 dev_priv->dma_pages[i].flushed = 0;
329 }
330 SET_AGE(&dev_priv->last_dma_age, event, wrap);
331 dev_priv->first_dma_page = dev_priv->current_dma_page = 0;
332}
333
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000334void savage_dma_wait(drm_savage_private_t * dev_priv, unsigned int page)
Dave Airlie282a1672005-08-07 15:43:54 +1000335{
336 uint16_t event;
337 unsigned int wrap;
338
339 /* Faked DMA buffer pages don't age. */
340 if (dev_priv->cmd_dma == &dev_priv->fake_dma)
341 return;
342
343 UPDATE_EVENT_COUNTER();
344 if (dev_priv->status_ptr)
345 event = dev_priv->status_ptr[1] & 0xffff;
346 else
347 event = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
348 wrap = dev_priv->event_wrap;
349 if (event > dev_priv->event_counter)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000350 wrap--; /* hardware hasn't passed the last wrap yet */
Dave Airlie282a1672005-08-07 15:43:54 +1000351
352 if (dev_priv->dma_pages[page].age.wrap > wrap ||
353 (dev_priv->dma_pages[page].age.wrap == wrap &&
354 dev_priv->dma_pages[page].age.event > event)) {
355 if (dev_priv->wait_evnt(dev_priv,
356 dev_priv->dma_pages[page].age.event)
357 < 0)
358 DRM_ERROR("wait_evnt failed!\n");
359 }
360}
361
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000362uint32_t *savage_dma_alloc(drm_savage_private_t * dev_priv, unsigned int n)
Dave Airlie282a1672005-08-07 15:43:54 +1000363{
364 unsigned int cur = dev_priv->current_dma_page;
365 unsigned int rest = SAVAGE_DMA_PAGE_SIZE -
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000366 dev_priv->dma_pages[cur].used;
367 unsigned int nr_pages = (n - rest + SAVAGE_DMA_PAGE_SIZE - 1) /
368 SAVAGE_DMA_PAGE_SIZE;
Dave Airlie282a1672005-08-07 15:43:54 +1000369 uint32_t *dma_ptr;
370 unsigned int i;
371
372 DRM_DEBUG("cur=%u, cur->used=%u, n=%u, rest=%u, nr_pages=%u\n",
373 cur, dev_priv->dma_pages[cur].used, n, rest, nr_pages);
374
375 if (cur + nr_pages < dev_priv->nr_dma_pages) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000376 dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
377 cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used;
Dave Airlie282a1672005-08-07 15:43:54 +1000378 if (n < rest)
379 rest = n;
380 dev_priv->dma_pages[cur].used += rest;
381 n -= rest;
382 cur++;
383 } else {
384 dev_priv->dma_flush(dev_priv);
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000385 nr_pages =
386 (n + SAVAGE_DMA_PAGE_SIZE - 1) / SAVAGE_DMA_PAGE_SIZE;
Dave Airlie282a1672005-08-07 15:43:54 +1000387 for (i = cur; i < dev_priv->nr_dma_pages; ++i) {
388 dev_priv->dma_pages[i].age = dev_priv->last_dma_age;
389 dev_priv->dma_pages[i].used = 0;
390 dev_priv->dma_pages[i].flushed = 0;
391 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000392 dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle;
Dave Airlie282a1672005-08-07 15:43:54 +1000393 dev_priv->first_dma_page = cur = 0;
394 }
395 for (i = cur; nr_pages > 0; ++i, --nr_pages) {
396#if SAVAGE_DMA_DEBUG
397 if (dev_priv->dma_pages[i].used) {
398 DRM_ERROR("unflushed page %u: used=%u\n",
399 i, dev_priv->dma_pages[i].used);
400 }
401#endif
402 if (n > SAVAGE_DMA_PAGE_SIZE)
403 dev_priv->dma_pages[i].used = SAVAGE_DMA_PAGE_SIZE;
404 else
405 dev_priv->dma_pages[i].used = n;
406 n -= SAVAGE_DMA_PAGE_SIZE;
407 }
408 dev_priv->current_dma_page = --i;
409
410 DRM_DEBUG("cur=%u, cur->used=%u, n=%u\n",
411 i, dev_priv->dma_pages[i].used, n);
412
413 savage_dma_wait(dev_priv, dev_priv->current_dma_page);
414
415 return dma_ptr;
416}
417
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000418static void savage_dma_flush(drm_savage_private_t * dev_priv)
Dave Airlie282a1672005-08-07 15:43:54 +1000419{
420 unsigned int first = dev_priv->first_dma_page;
421 unsigned int cur = dev_priv->current_dma_page;
422 uint16_t event;
423 unsigned int wrap, pad, align, len, i;
424 unsigned long phys_addr;
425 BCI_LOCALS;
426
427 if (first == cur &&
428 dev_priv->dma_pages[cur].used == dev_priv->dma_pages[cur].flushed)
429 return;
430
431 /* pad length to multiples of 2 entries
432 * align start of next DMA block to multiles of 8 entries */
433 pad = -dev_priv->dma_pages[cur].used & 1;
434 align = -(dev_priv->dma_pages[cur].used + pad) & 7;
435
436 DRM_DEBUG("first=%u, cur=%u, first->flushed=%u, cur->used=%u, "
437 "pad=%u, align=%u\n",
438 first, cur, dev_priv->dma_pages[first].flushed,
439 dev_priv->dma_pages[cur].used, pad, align);
440
441 /* pad with noops */
442 if (pad) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000443 uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
444 cur * SAVAGE_DMA_PAGE_SIZE + dev_priv->dma_pages[cur].used;
Dave Airlie282a1672005-08-07 15:43:54 +1000445 dev_priv->dma_pages[cur].used += pad;
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000446 while (pad != 0) {
Dave Airlie282a1672005-08-07 15:43:54 +1000447 *dma_ptr++ = BCI_CMD_WAIT;
448 pad--;
449 }
450 }
451
Daniel Vetter85b23312013-12-11 11:34:45 +0100452 mb();
Dave Airlie282a1672005-08-07 15:43:54 +1000453
454 /* do flush ... */
455 phys_addr = dev_priv->cmd_dma->offset +
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000456 (first * SAVAGE_DMA_PAGE_SIZE +
457 dev_priv->dma_pages[first].flushed) * 4;
Dave Airlie282a1672005-08-07 15:43:54 +1000458 len = (cur - first) * SAVAGE_DMA_PAGE_SIZE +
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000459 dev_priv->dma_pages[cur].used - dev_priv->dma_pages[first].flushed;
Dave Airlie282a1672005-08-07 15:43:54 +1000460
461 DRM_DEBUG("phys_addr=%lx, len=%u\n",
462 phys_addr | dev_priv->dma_type, len);
463
464 BEGIN_BCI(3);
465 BCI_SET_REGISTERS(SAVAGE_DMABUFADDR, 1);
466 BCI_WRITE(phys_addr | dev_priv->dma_type);
467 BCI_DMA(len);
468
469 /* fix alignment of the start of the next block */
470 dev_priv->dma_pages[cur].used += align;
471
472 /* age DMA pages */
473 event = savage_bci_emit_event(dev_priv, 0);
474 wrap = dev_priv->event_wrap;
475 for (i = first; i < cur; ++i) {
476 SET_AGE(&dev_priv->dma_pages[i].age, event, wrap);
477 dev_priv->dma_pages[i].used = 0;
478 dev_priv->dma_pages[i].flushed = 0;
479 }
480 /* age the current page only when it's full */
481 if (dev_priv->dma_pages[cur].used == SAVAGE_DMA_PAGE_SIZE) {
482 SET_AGE(&dev_priv->dma_pages[cur].age, event, wrap);
483 dev_priv->dma_pages[cur].used = 0;
484 dev_priv->dma_pages[cur].flushed = 0;
485 /* advance to next page */
486 cur++;
487 if (cur == dev_priv->nr_dma_pages)
488 cur = 0;
489 dev_priv->first_dma_page = dev_priv->current_dma_page = cur;
490 } else {
491 dev_priv->first_dma_page = cur;
492 dev_priv->dma_pages[cur].flushed = dev_priv->dma_pages[i].used;
493 }
494 SET_AGE(&dev_priv->last_dma_age, event, wrap);
495
496 DRM_DEBUG("first=cur=%u, cur->used=%u, cur->flushed=%u\n", cur,
497 dev_priv->dma_pages[cur].used,
498 dev_priv->dma_pages[cur].flushed);
499}
500
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000501static void savage_fake_dma_flush(drm_savage_private_t * dev_priv)
Dave Airlie282a1672005-08-07 15:43:54 +1000502{
503 unsigned int i, j;
504 BCI_LOCALS;
505
506 if (dev_priv->first_dma_page == dev_priv->current_dma_page &&
507 dev_priv->dma_pages[dev_priv->current_dma_page].used == 0)
508 return;
509
510 DRM_DEBUG("first=%u, cur=%u, cur->used=%u\n",
511 dev_priv->first_dma_page, dev_priv->current_dma_page,
512 dev_priv->dma_pages[dev_priv->current_dma_page].used);
513
514 for (i = dev_priv->first_dma_page;
515 i <= dev_priv->current_dma_page && dev_priv->dma_pages[i].used;
516 ++i) {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000517 uint32_t *dma_ptr = (uint32_t *) dev_priv->cmd_dma->handle +
518 i * SAVAGE_DMA_PAGE_SIZE;
Dave Airlie282a1672005-08-07 15:43:54 +1000519#if SAVAGE_DMA_DEBUG
520 /* Sanity check: all pages except the last one must be full. */
521 if (i < dev_priv->current_dma_page &&
522 dev_priv->dma_pages[i].used != SAVAGE_DMA_PAGE_SIZE) {
523 DRM_ERROR("partial DMA page %u: used=%u",
524 i, dev_priv->dma_pages[i].used);
525 }
526#endif
527 BEGIN_BCI(dev_priv->dma_pages[i].used);
528 for (j = 0; j < dev_priv->dma_pages[i].used; ++j) {
529 BCI_WRITE(dma_ptr[j]);
530 }
531 dev_priv->dma_pages[i].used = 0;
532 }
533
534 /* reset to first page */
535 dev_priv->first_dma_page = dev_priv->current_dma_page = 0;
536}
537
Dave Airlieeddca552007-07-11 16:09:54 +1000538int savage_driver_load(struct drm_device *dev, unsigned long chipset)
Dave Airlie282a1672005-08-07 15:43:54 +1000539{
540 drm_savage_private_t *dev_priv;
Dave Airlie282a1672005-08-07 15:43:54 +1000541
Julia Lawall6ebc22e2010-05-13 21:58:56 +0200542 dev_priv = kzalloc(sizeof(drm_savage_private_t), GFP_KERNEL);
Dave Airlie282a1672005-08-07 15:43:54 +1000543 if (dev_priv == NULL)
Eric Anholt20caafa2007-08-25 19:22:43 +1000544 return -ENOMEM;
Dave Airlie282a1672005-08-07 15:43:54 +1000545
Dave Airlie282a1672005-08-07 15:43:54 +1000546 dev->dev_private = (void *)dev_priv;
Dave Airlie22eae942005-11-10 22:16:34 +1100547
Dave Airlie282a1672005-08-07 15:43:54 +1000548 dev_priv->chipset = (enum savage_family)chipset;
549
Florian Zumbiehldf86b572012-10-02 12:20:37 +0000550 pci_set_master(dev->pdev);
551
Dave Airlie22eae942005-11-10 22:16:34 +1100552 return 0;
553}
554
555
556/*
Uwe Kleine-König421f91d2010-06-11 12:17:00 +0200557 * Initialize mappings. On Savage4 and SavageIX the alignment
Dave Airlie22eae942005-11-10 22:16:34 +1100558 * and size of the aperture is not suitable for automatic MTRR setup
David Herrmann9fc5cde2014-08-29 12:12:28 +0200559 * in drm_legacy_addmap. Therefore we add them manually before the maps are
Dave Airlie22eae942005-11-10 22:16:34 +1100560 * initialized, and tear them down on last close.
561 */
Dave Airlieeddca552007-07-11 16:09:54 +1000562int savage_driver_firstopen(struct drm_device *dev)
Dave Airlie22eae942005-11-10 22:16:34 +1100563{
564 drm_savage_private_t *dev_priv = dev->dev_private;
565 unsigned long mmio_base, fb_base, fb_size, aperture_base;
566 /* fb_rsrc and aper_rsrc aren't really used currently, but still exist
567 * in case we decide we need information on the BAR for BSD in the
568 * future.
569 */
570 unsigned int fb_rsrc, aper_rsrc;
571 int ret = 0;
572
Dave Airlie282a1672005-08-07 15:43:54 +1000573 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
574 fb_rsrc = 0;
Jordan Crouse01d73a62010-05-27 13:40:24 -0600575 fb_base = pci_resource_start(dev->pdev, 0);
Dave Airlie282a1672005-08-07 15:43:54 +1000576 fb_size = SAVAGE_FB_SIZE_S3;
577 mmio_base = fb_base + SAVAGE_FB_SIZE_S3;
578 aper_rsrc = 0;
579 aperture_base = fb_base + SAVAGE_APERTURE_OFFSET;
580 /* this should always be true */
Jordan Crouse01d73a62010-05-27 13:40:24 -0600581 if (pci_resource_len(dev->pdev, 0) == 0x08000000) {
Dave Airlie282a1672005-08-07 15:43:54 +1000582 /* Don't make MMIO write-cobining! We need 3
583 * MTRRs. */
Andy Lutomirski247d36d2013-05-13 23:58:41 +0000584 dev_priv->mtrr_handles[0] =
585 arch_phys_wc_add(fb_base, 0x01000000);
586 dev_priv->mtrr_handles[1] =
587 arch_phys_wc_add(fb_base + 0x02000000,
588 0x02000000);
589 dev_priv->mtrr_handles[2] =
590 arch_phys_wc_add(fb_base + 0x04000000,
591 0x04000000);
Dave Airlie282a1672005-08-07 15:43:54 +1000592 } else {
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +1100593 DRM_ERROR("strange pci_resource_len %08llx\n",
Jordan Crouse01d73a62010-05-27 13:40:24 -0600594 (unsigned long long)
595 pci_resource_len(dev->pdev, 0));
Dave Airlie282a1672005-08-07 15:43:54 +1000596 }
Dave Airlie22eae942005-11-10 22:16:34 +1100597 } else if (dev_priv->chipset != S3_SUPERSAVAGE &&
598 dev_priv->chipset != S3_SAVAGE2000) {
Jordan Crouse01d73a62010-05-27 13:40:24 -0600599 mmio_base = pci_resource_start(dev->pdev, 0);
Dave Airlie282a1672005-08-07 15:43:54 +1000600 fb_rsrc = 1;
Jordan Crouse01d73a62010-05-27 13:40:24 -0600601 fb_base = pci_resource_start(dev->pdev, 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000602 fb_size = SAVAGE_FB_SIZE_S4;
603 aper_rsrc = 1;
604 aperture_base = fb_base + SAVAGE_APERTURE_OFFSET;
605 /* this should always be true */
Jordan Crouse01d73a62010-05-27 13:40:24 -0600606 if (pci_resource_len(dev->pdev, 1) == 0x08000000) {
Dave Airlie282a1672005-08-07 15:43:54 +1000607 /* Can use one MTRR to cover both fb and
608 * aperture. */
Andy Lutomirski247d36d2013-05-13 23:58:41 +0000609 dev_priv->mtrr_handles[0] =
610 arch_phys_wc_add(fb_base,
611 0x08000000);
Dave Airlie282a1672005-08-07 15:43:54 +1000612 } else {
Benjamin Herrenschmidtd883f7f2009-02-02 16:55:45 +1100613 DRM_ERROR("strange pci_resource_len %08llx\n",
Jordan Crouse01d73a62010-05-27 13:40:24 -0600614 (unsigned long long)
615 pci_resource_len(dev->pdev, 1));
Dave Airlie282a1672005-08-07 15:43:54 +1000616 }
617 } else {
Jordan Crouse01d73a62010-05-27 13:40:24 -0600618 mmio_base = pci_resource_start(dev->pdev, 0);
Dave Airlie282a1672005-08-07 15:43:54 +1000619 fb_rsrc = 1;
Jordan Crouse01d73a62010-05-27 13:40:24 -0600620 fb_base = pci_resource_start(dev->pdev, 1);
621 fb_size = pci_resource_len(dev->pdev, 1);
Dave Airlie282a1672005-08-07 15:43:54 +1000622 aper_rsrc = 2;
Jordan Crouse01d73a62010-05-27 13:40:24 -0600623 aperture_base = pci_resource_start(dev->pdev, 2);
Dave Airlie282a1672005-08-07 15:43:54 +1000624 /* Automatic MTRR setup will do the right thing. */
625 }
626
David Herrmann9fc5cde2014-08-29 12:12:28 +0200627 ret = drm_legacy_addmap(dev, mmio_base, SAVAGE_MMIO_SIZE,
628 _DRM_REGISTERS, _DRM_READ_ONLY,
629 &dev_priv->mmio);
Dave Airlie282a1672005-08-07 15:43:54 +1000630 if (ret)
631 return ret;
632
David Herrmann9fc5cde2014-08-29 12:12:28 +0200633 ret = drm_legacy_addmap(dev, fb_base, fb_size, _DRM_FRAME_BUFFER,
634 _DRM_WRITE_COMBINING, &dev_priv->fb);
Dave Airlie282a1672005-08-07 15:43:54 +1000635 if (ret)
636 return ret;
637
David Herrmann9fc5cde2014-08-29 12:12:28 +0200638 ret = drm_legacy_addmap(dev, aperture_base, SAVAGE_APERTURE_SIZE,
639 _DRM_FRAME_BUFFER, _DRM_WRITE_COMBINING,
640 &dev_priv->aperture);
Dave Airlie282a1672005-08-07 15:43:54 +1000641 return ret;
642}
643
644/*
645 * Delete MTRRs and free device-private data.
646 */
Dave Airlieeddca552007-07-11 16:09:54 +1000647void savage_driver_lastclose(struct drm_device *dev)
Dave Airlie282a1672005-08-07 15:43:54 +1000648{
649 drm_savage_private_t *dev_priv = dev->dev_private;
650 int i;
651
Andy Lutomirski247d36d2013-05-13 23:58:41 +0000652 for (i = 0; i < 3; ++i) {
653 arch_phys_wc_del(dev_priv->mtrr_handles[i]);
654 dev_priv->mtrr_handles[i] = 0;
655 }
Dave Airlie22eae942005-11-10 22:16:34 +1100656}
657
Dave Airlieeddca552007-07-11 16:09:54 +1000658int savage_driver_unload(struct drm_device *dev)
Dave Airlie22eae942005-11-10 22:16:34 +1100659{
660 drm_savage_private_t *dev_priv = dev->dev_private;
Dave Airlie282a1672005-08-07 15:43:54 +1000661
Eric Anholt9a298b22009-03-24 12:23:04 -0700662 kfree(dev_priv);
Dave Airlie282a1672005-08-07 15:43:54 +1000663
664 return 0;
665}
666
Dave Airlieeddca552007-07-11 16:09:54 +1000667static int savage_do_init_bci(struct drm_device * dev, drm_savage_init_t * init)
Dave Airlie282a1672005-08-07 15:43:54 +1000668{
669 drm_savage_private_t *dev_priv = dev->dev_private;
670
671 if (init->fb_bpp != 16 && init->fb_bpp != 32) {
672 DRM_ERROR("invalid frame buffer bpp %d!\n", init->fb_bpp);
Eric Anholt20caafa2007-08-25 19:22:43 +1000673 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000674 }
675 if (init->depth_bpp != 16 && init->depth_bpp != 32) {
676 DRM_ERROR("invalid depth buffer bpp %d!\n", init->fb_bpp);
Eric Anholt20caafa2007-08-25 19:22:43 +1000677 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000678 }
679 if (init->dma_type != SAVAGE_DMA_AGP &&
680 init->dma_type != SAVAGE_DMA_PCI) {
681 DRM_ERROR("invalid dma memory type %d!\n", init->dma_type);
Eric Anholt20caafa2007-08-25 19:22:43 +1000682 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000683 }
684
685 dev_priv->cob_size = init->cob_size;
686 dev_priv->bci_threshold_lo = init->bci_threshold_lo;
687 dev_priv->bci_threshold_hi = init->bci_threshold_hi;
688 dev_priv->dma_type = init->dma_type;
689
690 dev_priv->fb_bpp = init->fb_bpp;
691 dev_priv->front_offset = init->front_offset;
692 dev_priv->front_pitch = init->front_pitch;
693 dev_priv->back_offset = init->back_offset;
694 dev_priv->back_pitch = init->back_pitch;
695 dev_priv->depth_bpp = init->depth_bpp;
696 dev_priv->depth_offset = init->depth_offset;
697 dev_priv->depth_pitch = init->depth_pitch;
698
699 dev_priv->texture_offset = init->texture_offset;
700 dev_priv->texture_size = init->texture_size;
701
David Herrmann9fc5cde2014-08-29 12:12:28 +0200702 dev_priv->sarea = drm_legacy_getsarea(dev);
Dave Airlie282a1672005-08-07 15:43:54 +1000703 if (!dev_priv->sarea) {
704 DRM_ERROR("could not find sarea!\n");
705 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000706 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000707 }
708 if (init->status_offset != 0) {
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200709 dev_priv->status = drm_legacy_findmap(dev, init->status_offset);
Dave Airlie282a1672005-08-07 15:43:54 +1000710 if (!dev_priv->status) {
711 DRM_ERROR("could not find shadow status region!\n");
712 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000713 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000714 }
715 } else {
716 dev_priv->status = NULL;
717 }
718 if (dev_priv->dma_type == SAVAGE_DMA_AGP && init->buffers_offset) {
Michael Karcher10eee0f2006-10-24 21:46:55 +1000719 dev->agp_buffer_token = init->buffers_offset;
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200720 dev->agp_buffer_map = drm_legacy_findmap(dev,
Dave Airlie282a1672005-08-07 15:43:54 +1000721 init->buffers_offset);
722 if (!dev->agp_buffer_map) {
723 DRM_ERROR("could not find DMA buffer region!\n");
724 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000725 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000726 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200727 drm_legacy_ioremap(dev->agp_buffer_map, dev);
Dan Carpenter10f5ab02012-05-17 10:09:44 +0300728 if (!dev->agp_buffer_map->handle) {
Dave Airlie282a1672005-08-07 15:43:54 +1000729 DRM_ERROR("failed to ioremap DMA buffer region!\n");
730 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000731 return -ENOMEM;
Dave Airlie282a1672005-08-07 15:43:54 +1000732 }
733 }
734 if (init->agp_textures_offset) {
735 dev_priv->agp_textures =
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200736 drm_legacy_findmap(dev, init->agp_textures_offset);
Dave Airlie282a1672005-08-07 15:43:54 +1000737 if (!dev_priv->agp_textures) {
738 DRM_ERROR("could not find agp texture region!\n");
739 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000740 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000741 }
742 } else {
743 dev_priv->agp_textures = NULL;
744 }
745
746 if (init->cmd_dma_offset) {
747 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
748 DRM_ERROR("command DMA not supported on "
749 "Savage3D/MX/IX.\n");
750 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000751 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000752 }
753 if (dev->dma && dev->dma->buflist) {
754 DRM_ERROR("command and vertex DMA not supported "
755 "at the same time.\n");
756 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000757 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000758 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200759 dev_priv->cmd_dma = drm_legacy_findmap(dev, init->cmd_dma_offset);
Dave Airlie282a1672005-08-07 15:43:54 +1000760 if (!dev_priv->cmd_dma) {
761 DRM_ERROR("could not find command DMA region!\n");
762 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000763 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000764 }
765 if (dev_priv->dma_type == SAVAGE_DMA_AGP) {
766 if (dev_priv->cmd_dma->type != _DRM_AGP) {
767 DRM_ERROR("AGP command DMA region is not a "
768 "_DRM_AGP map!\n");
769 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000770 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000771 }
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200772 drm_legacy_ioremap(dev_priv->cmd_dma, dev);
Dave Airlie282a1672005-08-07 15:43:54 +1000773 if (!dev_priv->cmd_dma->handle) {
774 DRM_ERROR("failed to ioremap command "
775 "DMA region!\n");
776 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000777 return -ENOMEM;
Dave Airlie282a1672005-08-07 15:43:54 +1000778 }
779 } else if (dev_priv->cmd_dma->type != _DRM_CONSISTENT) {
780 DRM_ERROR("PCI command DMA region is not a "
781 "_DRM_CONSISTENT map!\n");
782 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000783 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000784 }
785 } else {
786 dev_priv->cmd_dma = NULL;
787 }
788
789 dev_priv->dma_flush = savage_dma_flush;
790 if (!dev_priv->cmd_dma) {
791 DRM_DEBUG("falling back to faked command DMA.\n");
792 dev_priv->fake_dma.offset = 0;
793 dev_priv->fake_dma.size = SAVAGE_FAKE_DMA_SIZE;
794 dev_priv->fake_dma.type = _DRM_SHM;
Eric Anholt9a298b22009-03-24 12:23:04 -0700795 dev_priv->fake_dma.handle = kmalloc(SAVAGE_FAKE_DMA_SIZE,
796 GFP_KERNEL);
Dave Airlie282a1672005-08-07 15:43:54 +1000797 if (!dev_priv->fake_dma.handle) {
798 DRM_ERROR("could not allocate faked DMA buffer!\n");
799 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000800 return -ENOMEM;
Dave Airlie282a1672005-08-07 15:43:54 +1000801 }
802 dev_priv->cmd_dma = &dev_priv->fake_dma;
803 dev_priv->dma_flush = savage_fake_dma_flush;
804 }
805
806 dev_priv->sarea_priv =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000807 (drm_savage_sarea_t *) ((uint8_t *) dev_priv->sarea->handle +
808 init->sarea_priv_offset);
Dave Airlie282a1672005-08-07 15:43:54 +1000809
810 /* setup bitmap descriptors */
811 {
812 unsigned int color_tile_format;
813 unsigned int depth_tile_format;
814 unsigned int front_stride, back_stride, depth_stride;
815 if (dev_priv->chipset <= S3_SAVAGE4) {
816 color_tile_format = dev_priv->fb_bpp == 16 ?
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000817 SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP;
Dave Airlie282a1672005-08-07 15:43:54 +1000818 depth_tile_format = dev_priv->depth_bpp == 16 ?
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000819 SAVAGE_BD_TILE_16BPP : SAVAGE_BD_TILE_32BPP;
Dave Airlie282a1672005-08-07 15:43:54 +1000820 } else {
821 color_tile_format = SAVAGE_BD_TILE_DEST;
822 depth_tile_format = SAVAGE_BD_TILE_DEST;
823 }
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000824 front_stride = dev_priv->front_pitch / (dev_priv->fb_bpp / 8);
825 back_stride = dev_priv->back_pitch / (dev_priv->fb_bpp / 8);
826 depth_stride =
827 dev_priv->depth_pitch / (dev_priv->depth_bpp / 8);
Dave Airlie282a1672005-08-07 15:43:54 +1000828
829 dev_priv->front_bd = front_stride | SAVAGE_BD_BW_DISABLE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000830 (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) |
831 (color_tile_format << SAVAGE_BD_TILE_SHIFT);
Dave Airlie282a1672005-08-07 15:43:54 +1000832
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000833 dev_priv->back_bd = back_stride | SAVAGE_BD_BW_DISABLE |
834 (dev_priv->fb_bpp << SAVAGE_BD_BPP_SHIFT) |
835 (color_tile_format << SAVAGE_BD_TILE_SHIFT);
Dave Airlie282a1672005-08-07 15:43:54 +1000836
837 dev_priv->depth_bd = depth_stride | SAVAGE_BD_BW_DISABLE |
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000838 (dev_priv->depth_bpp << SAVAGE_BD_BPP_SHIFT) |
839 (depth_tile_format << SAVAGE_BD_TILE_SHIFT);
Dave Airlie282a1672005-08-07 15:43:54 +1000840 }
841
842 /* setup status and bci ptr */
843 dev_priv->event_counter = 0;
844 dev_priv->event_wrap = 0;
845 dev_priv->bci_ptr = (volatile uint32_t *)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000846 ((uint8_t *) dev_priv->mmio->handle + SAVAGE_BCI_OFFSET);
Dave Airlie282a1672005-08-07 15:43:54 +1000847 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
848 dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S3D;
849 } else {
850 dev_priv->status_used_mask = SAVAGE_FIFO_USED_MASK_S4;
851 }
852 if (dev_priv->status != NULL) {
853 dev_priv->status_ptr =
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000854 (volatile uint32_t *)dev_priv->status->handle;
Dave Airlie282a1672005-08-07 15:43:54 +1000855 dev_priv->wait_fifo = savage_bci_wait_fifo_shadow;
856 dev_priv->wait_evnt = savage_bci_wait_event_shadow;
857 dev_priv->status_ptr[1023] = dev_priv->event_counter;
858 } else {
859 dev_priv->status_ptr = NULL;
860 if (S3_SAVAGE3D_SERIES(dev_priv->chipset)) {
861 dev_priv->wait_fifo = savage_bci_wait_fifo_s3d;
862 } else {
863 dev_priv->wait_fifo = savage_bci_wait_fifo_s4;
864 }
865 dev_priv->wait_evnt = savage_bci_wait_event_reg;
866 }
867
868 /* cliprect functions */
869 if (S3_SAVAGE3D_SERIES(dev_priv->chipset))
870 dev_priv->emit_clip_rect = savage_emit_clip_rect_s3d;
871 else
872 dev_priv->emit_clip_rect = savage_emit_clip_rect_s4;
873
874 if (savage_freelist_init(dev) < 0) {
875 DRM_ERROR("could not initialize freelist\n");
876 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000877 return -ENOMEM;
Dave Airlie282a1672005-08-07 15:43:54 +1000878 }
879
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000880 if (savage_dma_init(dev_priv) < 0) {
Dave Airlie282a1672005-08-07 15:43:54 +1000881 DRM_ERROR("could not initialize command DMA\n");
882 savage_do_cleanup_bci(dev);
Eric Anholt20caafa2007-08-25 19:22:43 +1000883 return -ENOMEM;
Dave Airlie282a1672005-08-07 15:43:54 +1000884 }
885
886 return 0;
887}
888
Dave Airlieeddca552007-07-11 16:09:54 +1000889static int savage_do_cleanup_bci(struct drm_device * dev)
Dave Airlie282a1672005-08-07 15:43:54 +1000890{
891 drm_savage_private_t *dev_priv = dev->dev_private;
892
893 if (dev_priv->cmd_dma == &dev_priv->fake_dma) {
Eric Anholt9a298b22009-03-24 12:23:04 -0700894 kfree(dev_priv->fake_dma.handle);
Dave Airlie282a1672005-08-07 15:43:54 +1000895 } else if (dev_priv->cmd_dma && dev_priv->cmd_dma->handle &&
896 dev_priv->cmd_dma->type == _DRM_AGP &&
897 dev_priv->dma_type == SAVAGE_DMA_AGP)
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200898 drm_legacy_ioremapfree(dev_priv->cmd_dma, dev);
Dave Airlie282a1672005-08-07 15:43:54 +1000899
900 if (dev_priv->dma_type == SAVAGE_DMA_AGP &&
901 dev->agp_buffer_map && dev->agp_buffer_map->handle) {
Daniel Vetter86c1fbd2014-09-10 12:43:56 +0200902 drm_legacy_ioremapfree(dev->agp_buffer_map, dev);
Dave Airlie282a1672005-08-07 15:43:54 +1000903 /* make sure the next instance (which may be running
904 * in PCI mode) doesn't try to use an old
905 * agp_buffer_map. */
906 dev->agp_buffer_map = NULL;
907 }
908
Eric Anholt9a298b22009-03-24 12:23:04 -0700909 kfree(dev_priv->dma_pages);
Dave Airlie282a1672005-08-07 15:43:54 +1000910
911 return 0;
912}
913
Eric Anholtc153f452007-09-03 12:06:45 +1000914static int savage_bci_init(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie282a1672005-08-07 15:43:54 +1000915{
Eric Anholtc153f452007-09-03 12:06:45 +1000916 drm_savage_init_t *init = data;
Dave Airlie282a1672005-08-07 15:43:54 +1000917
Eric Anholt6c340ea2007-08-25 20:23:09 +1000918 LOCK_TEST_WITH_RETURN(dev, file_priv);
Dave Airlie282a1672005-08-07 15:43:54 +1000919
Eric Anholtc153f452007-09-03 12:06:45 +1000920 switch (init->func) {
Dave Airlie282a1672005-08-07 15:43:54 +1000921 case SAVAGE_INIT_BCI:
Eric Anholtc153f452007-09-03 12:06:45 +1000922 return savage_do_init_bci(dev, init);
Dave Airlie282a1672005-08-07 15:43:54 +1000923 case SAVAGE_CLEANUP_BCI:
924 return savage_do_cleanup_bci(dev);
925 }
926
Eric Anholt20caafa2007-08-25 19:22:43 +1000927 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +1000928}
929
Eric Anholtc153f452007-09-03 12:06:45 +1000930static int savage_bci_event_emit(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie282a1672005-08-07 15:43:54 +1000931{
Dave Airlie282a1672005-08-07 15:43:54 +1000932 drm_savage_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000933 drm_savage_event_emit_t *event = data;
Dave Airlie282a1672005-08-07 15:43:54 +1000934
935 DRM_DEBUG("\n");
936
Eric Anholt6c340ea2007-08-25 20:23:09 +1000937 LOCK_TEST_WITH_RETURN(dev, file_priv);
Dave Airlie282a1672005-08-07 15:43:54 +1000938
Eric Anholtc153f452007-09-03 12:06:45 +1000939 event->count = savage_bci_emit_event(dev_priv, event->flags);
940 event->count |= dev_priv->event_wrap << 16;
Dave Airlie282a1672005-08-07 15:43:54 +1000941
Dave Airlie282a1672005-08-07 15:43:54 +1000942 return 0;
943}
944
Eric Anholtc153f452007-09-03 12:06:45 +1000945static int savage_bci_event_wait(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie282a1672005-08-07 15:43:54 +1000946{
Dave Airlie282a1672005-08-07 15:43:54 +1000947 drm_savage_private_t *dev_priv = dev->dev_private;
Eric Anholtc153f452007-09-03 12:06:45 +1000948 drm_savage_event_wait_t *event = data;
Dave Airlie282a1672005-08-07 15:43:54 +1000949 unsigned int event_e, hw_e;
950 unsigned int event_w, hw_w;
951
952 DRM_DEBUG("\n");
953
Dave Airlie282a1672005-08-07 15:43:54 +1000954 UPDATE_EVENT_COUNTER();
955 if (dev_priv->status_ptr)
956 hw_e = dev_priv->status_ptr[1] & 0xffff;
957 else
958 hw_e = SAVAGE_READ(SAVAGE_STATUS_WORD1) & 0xffff;
959 hw_w = dev_priv->event_wrap;
960 if (hw_e > dev_priv->event_counter)
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000961 hw_w--; /* hardware hasn't passed the last wrap yet */
Dave Airlie282a1672005-08-07 15:43:54 +1000962
Eric Anholtc153f452007-09-03 12:06:45 +1000963 event_e = event->count & 0xffff;
964 event_w = event->count >> 16;
Dave Airlie282a1672005-08-07 15:43:54 +1000965
966 /* Don't need to wait if
967 * - event counter wrapped since the event was emitted or
968 * - the hardware has advanced up to or over the event to wait for.
969 */
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000970 if (event_w < hw_w || (event_w == hw_w && event_e <= hw_e))
Dave Airlie282a1672005-08-07 15:43:54 +1000971 return 0;
972 else
973 return dev_priv->wait_evnt(dev_priv, event_e);
974}
975
976/*
977 * DMA buffer management
978 */
979
Eric Anholt6c340ea2007-08-25 20:23:09 +1000980static int savage_bci_get_buffers(struct drm_device *dev,
981 struct drm_file *file_priv,
982 struct drm_dma *d)
Dave Airlie282a1672005-08-07 15:43:54 +1000983{
Dave Airlie056219e2007-07-11 16:17:42 +1000984 struct drm_buf *buf;
Dave Airlie282a1672005-08-07 15:43:54 +1000985 int i;
986
987 for (i = d->granted_count; i < d->request_count; i++) {
988 buf = savage_freelist_get(dev);
989 if (!buf)
Eric Anholt20caafa2007-08-25 19:22:43 +1000990 return -EAGAIN;
Dave Airlie282a1672005-08-07 15:43:54 +1000991
Eric Anholt6c340ea2007-08-25 20:23:09 +1000992 buf->file_priv = file_priv;
Dave Airlie282a1672005-08-07 15:43:54 +1000993
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100994 if (copy_to_user(&d->request_indices[i],
Dave Airlie282a1672005-08-07 15:43:54 +1000995 &buf->idx, sizeof(buf->idx)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000996 return -EFAULT;
Daniel Vetter1d6ac182013-12-11 11:34:44 +0100997 if (copy_to_user(&d->request_sizes[i],
Dave Airlie282a1672005-08-07 15:43:54 +1000998 &buf->total, sizeof(buf->total)))
Eric Anholt20caafa2007-08-25 19:22:43 +1000999 return -EFAULT;
Dave Airlie282a1672005-08-07 15:43:54 +10001000
1001 d->granted_count++;
1002 }
1003 return 0;
1004}
1005
Eric Anholtc153f452007-09-03 12:06:45 +10001006int savage_bci_buffers(struct drm_device *dev, void *data, struct drm_file *file_priv)
Dave Airlie282a1672005-08-07 15:43:54 +10001007{
Dave Airliecdd55a22007-07-11 16:32:08 +10001008 struct drm_device_dma *dma = dev->dma;
Eric Anholtc153f452007-09-03 12:06:45 +10001009 struct drm_dma *d = data;
Dave Airlie282a1672005-08-07 15:43:54 +10001010 int ret = 0;
1011
Eric Anholt6c340ea2007-08-25 20:23:09 +10001012 LOCK_TEST_WITH_RETURN(dev, file_priv);
Dave Airlie282a1672005-08-07 15:43:54 +10001013
Dave Airlie282a1672005-08-07 15:43:54 +10001014 /* Please don't send us buffers.
1015 */
Eric Anholtc153f452007-09-03 12:06:45 +10001016 if (d->send_count != 0) {
Dave Airlie282a1672005-08-07 15:43:54 +10001017 DRM_ERROR("Process %d trying to send %d buffers via drmDMA\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001018 DRM_CURRENTPID, d->send_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001019 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +10001020 }
1021
1022 /* We'll send you buffers.
1023 */
Eric Anholtc153f452007-09-03 12:06:45 +10001024 if (d->request_count < 0 || d->request_count > dma->buf_count) {
Dave Airlie282a1672005-08-07 15:43:54 +10001025 DRM_ERROR("Process %d trying to get %d buffers (of %d max)\n",
Eric Anholtc153f452007-09-03 12:06:45 +10001026 DRM_CURRENTPID, d->request_count, dma->buf_count);
Eric Anholt20caafa2007-08-25 19:22:43 +10001027 return -EINVAL;
Dave Airlie282a1672005-08-07 15:43:54 +10001028 }
1029
Eric Anholtc153f452007-09-03 12:06:45 +10001030 d->granted_count = 0;
Dave Airlie282a1672005-08-07 15:43:54 +10001031
Eric Anholtc153f452007-09-03 12:06:45 +10001032 if (d->request_count) {
1033 ret = savage_bci_get_buffers(dev, file_priv, d);
Dave Airlie282a1672005-08-07 15:43:54 +10001034 }
1035
Dave Airlie282a1672005-08-07 15:43:54 +10001036 return ret;
1037}
1038
Eric Anholt6c340ea2007-08-25 20:23:09 +10001039void savage_reclaim_buffers(struct drm_device *dev, struct drm_file *file_priv)
Dave Airlieb5e89ed2005-09-25 14:28:13 +10001040{
Dave Airliecdd55a22007-07-11 16:32:08 +10001041 struct drm_device_dma *dma = dev->dma;
Dave Airlie282a1672005-08-07 15:43:54 +10001042 drm_savage_private_t *dev_priv = dev->dev_private;
Daniel Vettere2b3c5b2011-10-26 00:14:15 +02001043 int release_idlelock = 0;
Dave Airlie282a1672005-08-07 15:43:54 +10001044 int i;
1045
1046 if (!dma)
1047 return;
1048 if (!dev_priv)
1049 return;
1050 if (!dma->buflist)
1051 return;
1052
Daniel Vettere2b3c5b2011-10-26 00:14:15 +02001053 if (file_priv->master && file_priv->master->lock.hw_lock) {
David Herrmannbb6d8222014-08-29 12:12:46 +02001054 drm_legacy_idlelock_take(&file_priv->master->lock);
Daniel Vettere2b3c5b2011-10-26 00:14:15 +02001055 release_idlelock = 1;
1056 }
Dave Airlie282a1672005-08-07 15:43:54 +10001057
1058 for (i = 0; i < dma->buf_count; i++) {
Dave Airlie056219e2007-07-11 16:17:42 +10001059 struct drm_buf *buf = dma->buflist[i];
Dave Airlie282a1672005-08-07 15:43:54 +10001060 drm_savage_buf_priv_t *buf_priv = buf->dev_private;
1061
Eric Anholt6c340ea2007-08-25 20:23:09 +10001062 if (buf->file_priv == file_priv && buf_priv &&
Dave Airlie282a1672005-08-07 15:43:54 +10001063 buf_priv->next == NULL && buf_priv->prev == NULL) {
1064 uint16_t event;
1065 DRM_DEBUG("reclaimed from client\n");
1066 event = savage_bci_emit_event(dev_priv, SAVAGE_WAIT_3D);
1067 SET_AGE(&buf_priv->age, event, dev_priv->event_wrap);
1068 savage_freelist_put(dev, buf);
1069 }
1070 }
1071
Daniel Vettere2b3c5b2011-10-26 00:14:15 +02001072 if (release_idlelock)
David Herrmannbb6d8222014-08-29 12:12:46 +02001073 drm_legacy_idlelock_release(&file_priv->master->lock);
Dave Airlie282a1672005-08-07 15:43:54 +10001074}
1075
Rob Clarkbaa70942013-08-02 13:27:49 -04001076const struct drm_ioctl_desc savage_ioctls[] = {
Dave Airlie1b2f1482010-08-14 20:20:34 +10001077 DRM_IOCTL_DEF_DRV(SAVAGE_BCI_INIT, savage_bci_init, DRM_AUTH|DRM_MASTER|DRM_ROOT_ONLY),
1078 DRM_IOCTL_DEF_DRV(SAVAGE_BCI_CMDBUF, savage_bci_cmdbuf, DRM_AUTH),
1079 DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_EMIT, savage_bci_event_emit, DRM_AUTH),
1080 DRM_IOCTL_DEF_DRV(SAVAGE_BCI_EVENT_WAIT, savage_bci_event_wait, DRM_AUTH),
Dave Airlie282a1672005-08-07 15:43:54 +10001081};
1082
Damien Lespiauf95aeb12014-06-09 14:39:49 +01001083int savage_max_ioctl = ARRAY_SIZE(savage_ioctls);