1 /* mga_bufs.c -- IOCTLs to manage buffers -*- linux-c -*-
2 * Created: Thu Jan 6 01:47:26 2000 by jhartmann@precisioninsight.com
3 *
4 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
5 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the "Software"),
10 * to deal in the Software without restriction, including without limitation
11 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 * and/or sell copies of the Software, and to permit persons to whom the
13 * Software is furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the next
16 * paragraph) shall be included in all copies or substantial portions of the
17 * Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 * DEALINGS IN THE SOFTWARE.
26 *
27 * Authors: Rickard E. (Rik) Faith <faith@valinux.com>
28 * Jeff Hartmann <jhartmann@valinux.com>
29 *
30 */
31
32 #define __NO_VERSION__
33 #include "drmP.h"
34 #include "mga_drv.h"
35 #include "linux/un.h"
36
37
mga_addbufs_agp(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)38 int mga_addbufs_agp(struct inode *inode, struct file *filp, unsigned int cmd,
39 unsigned long arg)
40 {
41 drm_file_t *priv = filp->private_data;
42 drm_device_t *dev = priv->dev;
43 drm_device_dma_t *dma = dev->dma;
44 drm_buf_desc_t request;
45 drm_buf_entry_t *entry;
46 drm_buf_t *buf;
47 unsigned long offset;
48 unsigned long agp_offset;
49 int count;
50 int order;
51 int size;
52 int alignment;
53 int page_order;
54 int total;
55 int byte_count;
56 int i;
57
58 if (!dma) return -EINVAL;
59
60 if (copy_from_user(&request,
61 (drm_buf_desc_t *)arg,
62 sizeof(request)))
63 return -EFAULT;
64
65 count = request.count;
66 order = drm_order(request.size);
67 size = 1 << order;
68 agp_offset = request.agp_start;
69 alignment = (request.flags & _DRM_PAGE_ALIGN) ? PAGE_ALIGN(size):size;
70 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
71 total = PAGE_SIZE << page_order;
72 byte_count = 0;
73
74 DRM_DEBUG("count: %d\n", count);
75 DRM_DEBUG("order: %d\n", order);
76 DRM_DEBUG("size: %d\n", size);
77 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
78 DRM_DEBUG("alignment: %d\n", alignment);
79 DRM_DEBUG("page_order: %d\n", page_order);
80 DRM_DEBUG("total: %d\n", total);
81 DRM_DEBUG("byte_count: %d\n", byte_count);
82
83 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) return -EINVAL;
84 if (dev->queue_count) return -EBUSY; /* Not while in use */
85 spin_lock(&dev->count_lock);
86 if (dev->buf_use) {
87 spin_unlock(&dev->count_lock);
88 return -EBUSY;
89 }
90 atomic_inc(&dev->buf_alloc);
91 spin_unlock(&dev->count_lock);
92
93 down(&dev->struct_sem);
94 entry = &dma->bufs[order];
95 if (entry->buf_count) {
96 up(&dev->struct_sem);
97 atomic_dec(&dev->buf_alloc);
98 return -ENOMEM; /* May only call once for each order */
99 }
100
101 /* This isnt neccessarily a good limit, but we have to stop a dumb
102 32 bit overflow problem below */
103
104 if ( count < 0 || count > 4096)
105 {
106 up(&dev->struct_sem);
107 atomic_dec(&dev->buf_alloc);
108 return -EINVAL;
109 }
110
111 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
112 DRM_MEM_BUFS);
113 if (!entry->buflist) {
114 up(&dev->struct_sem);
115 atomic_dec(&dev->buf_alloc);
116 return -ENOMEM;
117 }
118 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
119
120 entry->buf_size = size;
121 entry->page_order = page_order;
122 offset = 0;
123
124
125 while(entry->buf_count < count) {
126 buf = &entry->buflist[entry->buf_count];
127 buf->idx = dma->buf_count + entry->buf_count;
128 buf->total = alignment;
129 buf->order = order;
130 buf->used = 0;
131
132 buf->offset = offset; /* Hrm */
133 buf->bus_address = dev->agp->base + agp_offset + offset;
134 buf->address = (void *)(agp_offset + offset + dev->agp->base);
135 buf->next = NULL;
136 buf->waiting = 0;
137 buf->pending = 0;
138 init_waitqueue_head(&buf->dma_wait);
139 buf->pid = 0;
140
141 buf->dev_private = drm_alloc(sizeof(drm_mga_buf_priv_t),
142 DRM_MEM_BUFS);
143 buf->dev_priv_size = sizeof(drm_mga_buf_priv_t);
144
145 #if DRM_DMA_HISTOGRAM
146 buf->time_queued = 0;
147 buf->time_dispatched = 0;
148 buf->time_completed = 0;
149 buf->time_freed = 0;
150 #endif
151 offset = offset + alignment;
152 entry->buf_count++;
153 byte_count += PAGE_SIZE << page_order;
154 }
155
156 dma->buflist = drm_realloc(dma->buflist,
157 dma->buf_count * sizeof(*dma->buflist),
158 (dma->buf_count + entry->buf_count)
159 * sizeof(*dma->buflist),
160 DRM_MEM_BUFS);
161 for (i = dma->buf_count; i < dma->buf_count + entry->buf_count; i++)
162 dma->buflist[i] = &entry->buflist[i - dma->buf_count];
163
164 dma->buf_count += entry->buf_count;
165
166 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
167
168 dma->byte_count += byte_count;
169
170 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
171
172 drm_freelist_create(&entry->freelist, entry->buf_count);
173 for (i = 0; i < entry->buf_count; i++) {
174 drm_freelist_put(dev, &entry->freelist, &entry->buflist[i]);
175 }
176
177 up(&dev->struct_sem);
178
179 request.count = entry->buf_count;
180 request.size = size;
181
182 if (copy_to_user((drm_buf_desc_t *)arg,
183 &request,
184 sizeof(request)))
185 return -EFAULT;
186
187 atomic_dec(&dev->buf_alloc);
188
189 DRM_DEBUG("count: %d\n", count);
190 DRM_DEBUG("order: %d\n", order);
191 DRM_DEBUG("size: %d\n", size);
192 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
193 DRM_DEBUG("alignment: %d\n", alignment);
194 DRM_DEBUG("page_order: %d\n", page_order);
195 DRM_DEBUG("total: %d\n", total);
196 DRM_DEBUG("byte_count: %d\n", byte_count);
197
198 dma->flags = _DRM_DMA_USE_AGP;
199
200 DRM_DEBUG("dma->flags : %x\n", dma->flags);
201
202 return 0;
203 }
204
mga_addbufs_pci(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)205 int mga_addbufs_pci(struct inode *inode, struct file *filp, unsigned int cmd,
206 unsigned long arg)
207 {
208 drm_file_t *priv = filp->private_data;
209 drm_device_t *dev = priv->dev;
210 drm_device_dma_t *dma = dev->dma;
211 drm_buf_desc_t request;
212 int count;
213 int order;
214 int size;
215 int total;
216 int page_order;
217 drm_buf_entry_t *entry;
218 unsigned long page;
219 drm_buf_t *buf;
220 int alignment;
221 unsigned long offset;
222 int i;
223 int byte_count;
224 int page_count;
225
226 if (!dma) return -EINVAL;
227
228 if (copy_from_user(&request,
229 (drm_buf_desc_t *)arg,
230 sizeof(request)))
231 return -EFAULT;
232
233 count = request.count;
234 order = drm_order(request.size);
235 size = 1 << order;
236
237 DRM_DEBUG("count = %d, size = %d (%d), order = %d, queue_count = %d\n",
238 request.count, request.size, size, order, dev->queue_count);
239
240 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) return -EINVAL;
241 if (dev->queue_count) return -EBUSY; /* Not while in use */
242
243 alignment = (request.flags & _DRM_PAGE_ALIGN) ? PAGE_ALIGN(size):size;
244 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
245 total = PAGE_SIZE << page_order;
246
247 spin_lock(&dev->count_lock);
248 if (dev->buf_use) {
249 spin_unlock(&dev->count_lock);
250 return -EBUSY;
251 }
252 atomic_inc(&dev->buf_alloc);
253 spin_unlock(&dev->count_lock);
254
255 down(&dev->struct_sem);
256 entry = &dma->bufs[order];
257 if (entry->buf_count) {
258 up(&dev->struct_sem);
259 atomic_dec(&dev->buf_alloc);
260 return -ENOMEM; /* May only call once for each order */
261 }
262
263 if(count < 0 || count > 4096)
264 {
265 up(&dev->struct_sem);
266 atomic_dec(&dev->buf_alloc);
267 return -EINVAL;
268 }
269
270 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
271 DRM_MEM_BUFS);
272 if (!entry->buflist) {
273 up(&dev->struct_sem);
274 atomic_dec(&dev->buf_alloc);
275 return -ENOMEM;
276 }
277 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
278
279 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
280 DRM_MEM_SEGS);
281 if (!entry->seglist) {
282 drm_free(entry->buflist,
283 count * sizeof(*entry->buflist),
284 DRM_MEM_BUFS);
285 up(&dev->struct_sem);
286 atomic_dec(&dev->buf_alloc);
287 return -ENOMEM;
288 }
289 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
290
291 dma->pagelist = drm_realloc(dma->pagelist,
292 dma->page_count * sizeof(*dma->pagelist),
293 (dma->page_count + (count << page_order))
294 * sizeof(*dma->pagelist),
295 DRM_MEM_PAGES);
296 DRM_DEBUG("pagelist: %d entries\n",
297 dma->page_count + (count << page_order));
298
299
300 entry->buf_size = size;
301 entry->page_order = page_order;
302 byte_count = 0;
303 page_count = 0;
304 while (entry->buf_count < count) {
305 if (!(page = drm_alloc_pages(page_order, DRM_MEM_DMA))) break;
306 entry->seglist[entry->seg_count++] = page;
307 for (i = 0; i < (1 << page_order); i++) {
308 DRM_DEBUG("page %d @ 0x%08lx\n",
309 dma->page_count + page_count,
310 page + PAGE_SIZE * i);
311 dma->pagelist[dma->page_count + page_count++]
312 = page + PAGE_SIZE * i;
313 }
314 for (offset = 0;
315 offset + size <= total && entry->buf_count < count;
316 offset += alignment, ++entry->buf_count) {
317 buf = &entry->buflist[entry->buf_count];
318 buf->idx = dma->buf_count + entry->buf_count;
319 buf->total = alignment;
320 buf->order = order;
321 buf->used = 0;
322 buf->offset = (dma->byte_count + byte_count + offset);
323 buf->address = (void *)(page + offset);
324 buf->next = NULL;
325 buf->waiting = 0;
326 buf->pending = 0;
327 init_waitqueue_head(&buf->dma_wait);
328 buf->pid = 0;
329 #if DRM_DMA_HISTOGRAM
330 buf->time_queued = 0;
331 buf->time_dispatched = 0;
332 buf->time_completed = 0;
333 buf->time_freed = 0;
334 #endif
335 DRM_DEBUG("buffer %d @ %p\n",
336 entry->buf_count, buf->address);
337 }
338 byte_count += PAGE_SIZE << page_order;
339 }
340
341 dma->buflist = drm_realloc(dma->buflist,
342 dma->buf_count * sizeof(*dma->buflist),
343 (dma->buf_count + entry->buf_count)
344 * sizeof(*dma->buflist),
345 DRM_MEM_BUFS);
346 for (i = dma->buf_count; i < dma->buf_count + entry->buf_count; i++)
347 dma->buflist[i] = &entry->buflist[i - dma->buf_count];
348
349 dma->buf_count += entry->buf_count;
350 dma->seg_count += entry->seg_count;
351 dma->page_count += entry->seg_count << page_order;
352 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
353
354 drm_freelist_create(&entry->freelist, entry->buf_count);
355 for (i = 0; i < entry->buf_count; i++) {
356 drm_freelist_put(dev, &entry->freelist, &entry->buflist[i]);
357 }
358
359 up(&dev->struct_sem);
360
361 request.count = entry->buf_count;
362 request.size = size;
363
364 if (copy_to_user((drm_buf_desc_t *)arg,
365 &request,
366 sizeof(request)))
367 return -EFAULT;
368
369 atomic_dec(&dev->buf_alloc);
370 return 0;
371 }
372
mga_addbufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)373 int mga_addbufs(struct inode *inode, struct file *filp, unsigned int cmd,
374 unsigned long arg)
375 {
376 drm_buf_desc_t request;
377
378 if (copy_from_user(&request,
379 (drm_buf_desc_t *)arg,
380 sizeof(request)))
381 return -EFAULT;
382
383 if(request.flags & _DRM_AGP_BUFFER)
384 return mga_addbufs_agp(inode, filp, cmd, arg);
385 else
386 return mga_addbufs_pci(inode, filp, cmd, arg);
387 }
388
mga_infobufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)389 int mga_infobufs(struct inode *inode, struct file *filp, unsigned int cmd,
390 unsigned long arg)
391 {
392 drm_file_t *priv = filp->private_data;
393 drm_device_t *dev = priv->dev;
394 drm_device_dma_t *dma = dev->dma;
395 drm_buf_info_t request;
396 int i;
397 int count;
398
399 if (!dma) return -EINVAL;
400
401 spin_lock(&dev->count_lock);
402 if (atomic_read(&dev->buf_alloc)) {
403 spin_unlock(&dev->count_lock);
404 return -EBUSY;
405 }
406 ++dev->buf_use; /* Can't allocate more after this call */
407 spin_unlock(&dev->count_lock);
408
409 if (copy_from_user(&request,
410 (drm_buf_info_t *)arg,
411 sizeof(request)))
412 return -EFAULT;
413
414 for (i = 0, count = 0; i < DRM_MAX_ORDER+1; i++) {
415 if (dma->bufs[i].buf_count) ++count;
416 }
417
418 if (request.count >= count) {
419 for (i = 0, count = 0; i < DRM_MAX_ORDER+1; i++) {
420 if (dma->bufs[i].buf_count) {
421 if (copy_to_user(&request.list[count].count,
422 &dma->bufs[i].buf_count,
423 sizeof(dma->bufs[0]
424 .buf_count)) ||
425 copy_to_user(&request.list[count].size,
426 &dma->bufs[i].buf_size,
427 sizeof(dma->bufs[0].buf_size)) ||
428 copy_to_user(&request.list[count].low_mark,
429 &dma->bufs[i]
430 .freelist.low_mark,
431 sizeof(dma->bufs[0]
432 .freelist.low_mark)) ||
433 copy_to_user(&request.list[count]
434 .high_mark,
435 &dma->bufs[i]
436 .freelist.high_mark,
437 sizeof(dma->bufs[0]
438 .freelist.high_mark)))
439 return -EFAULT;
440 ++count;
441 }
442 }
443 }
444 request.count = count;
445
446 if (copy_to_user((drm_buf_info_t *)arg,
447 &request,
448 sizeof(request)))
449 return -EFAULT;
450
451 return 0;
452 }
453
mga_markbufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)454 int mga_markbufs(struct inode *inode, struct file *filp, unsigned int cmd,
455 unsigned long arg)
456 {
457 drm_file_t *priv = filp->private_data;
458 drm_device_t *dev = priv->dev;
459 drm_device_dma_t *dma = dev->dma;
460 drm_buf_desc_t request;
461 int order;
462 drm_buf_entry_t *entry;
463
464 if (!dma) return -EINVAL;
465
466 if (copy_from_user(&request, (drm_buf_desc_t *)arg, sizeof(request)))
467 return -EFAULT;
468
469 order = drm_order(request.size);
470 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER) return -EINVAL;
471 entry = &dma->bufs[order];
472
473 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
474 return -EINVAL;
475 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
476 return -EINVAL;
477
478 entry->freelist.low_mark = request.low_mark;
479 entry->freelist.high_mark = request.high_mark;
480
481 return 0;
482 }
483
mga_freebufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)484 int mga_freebufs(struct inode *inode, struct file *filp, unsigned int cmd,
485 unsigned long arg)
486 {
487 drm_file_t *priv = filp->private_data;
488 drm_device_t *dev = priv->dev;
489 drm_device_dma_t *dma = dev->dma;
490 drm_buf_free_t request;
491 int i;
492 int idx;
493 drm_buf_t *buf;
494
495 if (!dma) return -EINVAL;
496
497 if (copy_from_user(&request,
498 (drm_buf_free_t *)arg,
499 sizeof(request)))
500 return -EFAULT;
501
502 for (i = 0; i < request.count; i++) {
503 if (copy_from_user(&idx,
504 &request.list[i],
505 sizeof(idx)))
506 return -EFAULT;
507 if (idx < 0 || idx >= dma->buf_count) {
508 DRM_ERROR("Index %d (of %d max)\n",
509 idx, dma->buf_count - 1);
510 return -EINVAL;
511 }
512 buf = dma->buflist[idx];
513 if (buf->pid != current->pid) {
514 DRM_ERROR("Process %d freeing buffer owned by %d\n",
515 current->pid, buf->pid);
516 return -EINVAL;
517 }
518 drm_free_buffer(dev, buf);
519 }
520
521 return 0;
522 }
523
mga_mapbufs(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)524 int mga_mapbufs(struct inode *inode, struct file *filp, unsigned int cmd,
525 unsigned long arg)
526 {
527 drm_file_t *priv = filp->private_data;
528 drm_device_t *dev = priv->dev;
529 drm_device_dma_t *dma = dev->dma;
530 int retcode = 0;
531 const int zero = 0;
532 unsigned long virtual;
533 unsigned long address;
534 drm_buf_map_t request;
535 int i;
536
537 if (!dma) return -EINVAL;
538
539 spin_lock(&dev->count_lock);
540 if (atomic_read(&dev->buf_alloc)) {
541 spin_unlock(&dev->count_lock);
542 return -EBUSY;
543 }
544 ++dev->buf_use; /* Can't allocate more after this call */
545 spin_unlock(&dev->count_lock);
546
547 if (copy_from_user(&request,
548 (drm_buf_map_t *)arg,
549 sizeof(request)))
550 return -EFAULT;
551
552 if (request.count >= dma->buf_count) {
553 if(dma->flags & _DRM_DMA_USE_AGP) {
554 drm_mga_private_t *dev_priv = dev->dev_private;
555 drm_map_t *map = NULL;
556
557 map = dev->maplist[dev_priv->buffer_map_idx];
558 if (!map) {
559 retcode = -EINVAL;
560 goto done;
561 }
562
563 DRM_DEBUG("map->offset : %lx\n", map->offset);
564 DRM_DEBUG("map->size : %lx\n", map->size);
565 DRM_DEBUG("map->type : %d\n", map->type);
566 DRM_DEBUG("map->flags : %x\n", map->flags);
567 DRM_DEBUG("map->handle : %p\n", map->handle);
568 DRM_DEBUG("map->mtrr : %d\n", map->mtrr);
569 down_write(¤t->mm->mmap_sem);
570 virtual = do_mmap(filp, 0, map->size,
571 PROT_READ|PROT_WRITE,
572 MAP_SHARED,
573 (unsigned long)map->offset);
574 up_write(¤t->mm->mmap_sem);
575 } else {
576 down_write(¤t->mm->mmap_sem);
577 virtual = do_mmap(filp, 0, dma->byte_count,
578 PROT_READ|PROT_WRITE, MAP_SHARED, 0);
579 up_write(¤t->mm->mmap_sem);
580 }
581 if (virtual > -1024UL) {
582 /* Real error */
583 DRM_DEBUG("mmap error\n");
584 retcode = (signed long)virtual;
585 goto done;
586 }
587 request.virtual = (void *)virtual;
588
589 for (i = 0; i < dma->buf_count; i++) {
590 if (copy_to_user(&request.list[i].idx,
591 &dma->buflist[i]->idx,
592 sizeof(request.list[0].idx))) {
593 retcode = -EFAULT;
594 goto done;
595 }
596 if (copy_to_user(&request.list[i].total,
597 &dma->buflist[i]->total,
598 sizeof(request.list[0].total))) {
599 retcode = -EFAULT;
600 goto done;
601 }
602 if (copy_to_user(&request.list[i].used,
603 &zero,
604 sizeof(zero))) {
605 retcode = -EFAULT;
606 goto done;
607 }
608 address = virtual + dma->buflist[i]->offset;
609 if (copy_to_user(&request.list[i].address,
610 &address,
611 sizeof(address))) {
612 retcode = -EFAULT;
613 goto done;
614 }
615 }
616 }
617 done:
618 request.count = dma->buf_count;
619 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
620
621 if (copy_to_user((drm_buf_map_t *)arg,
622 &request,
623 sizeof(request)))
624 return -EFAULT;
625
626 DRM_DEBUG("retcode : %d\n", retcode);
627
628 return retcode;
629 }
630