]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - drivers/char/drm/drm_bufs.c
drm: detypef waitlist/freelist/buf_entry/device_dma/drm_queue structs
[mirror_ubuntu-focal-kernel.git] / drivers / char / drm / drm_bufs.c
1 /**
2 * \file drm_bufs.c
3 * Generic buffer template
4 *
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Gareth Hughes <gareth@valinux.com>
7 */
8
9 /*
10 * Created: Thu Nov 23 03:10:50 2000 by gareth@valinux.com
11 *
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
15 *
16 * Permission is hereby granted, free of charge, to any person obtaining a
17 * copy of this software and associated documentation files (the "Software"),
18 * to deal in the Software without restriction, including without limitation
19 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
20 * and/or sell copies of the Software, and to permit persons to whom the
21 * Software is furnished to do so, subject to the following conditions:
22 *
23 * The above copyright notice and this permission notice (including the next
24 * paragraph) shall be included in all copies or substantial portions of the
25 * Software.
26 *
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
31 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
32 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
33 * OTHER DEALINGS IN THE SOFTWARE.
34 */
35
36 #include <linux/vmalloc.h>
37 #include "drmP.h"
38
39 unsigned long drm_get_resource_start(struct drm_device *dev, unsigned int resource)
40 {
41 return pci_resource_start(dev->pdev, resource);
42 }
43 EXPORT_SYMBOL(drm_get_resource_start);
44
45 unsigned long drm_get_resource_len(struct drm_device *dev, unsigned int resource)
46 {
47 return pci_resource_len(dev->pdev, resource);
48 }
49
50 EXPORT_SYMBOL(drm_get_resource_len);
51
52 static drm_map_list_t *drm_find_matching_map(struct drm_device *dev,
53 drm_local_map_t *map)
54 {
55 drm_map_list_t *entry;
56 list_for_each_entry(entry, &dev->maplist, head) {
57 if (entry->map && map->type == entry->map->type &&
58 ((entry->map->offset == map->offset) ||
59 (map->type == _DRM_SHM && map->flags==_DRM_CONTAINS_LOCK))) {
60 return entry;
61 }
62 }
63
64 return NULL;
65 }
66
67 static int drm_map_handle(struct drm_device *dev, drm_hash_item_t *hash,
68 unsigned long user_token, int hashed_handle)
69 {
70 int use_hashed_handle;
71 #if (BITS_PER_LONG == 64)
72 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
73 #elif (BITS_PER_LONG == 32)
74 use_hashed_handle = hashed_handle;
75 #else
76 #error Unsupported long size. Neither 64 nor 32 bits.
77 #endif
78
79 if (!use_hashed_handle) {
80 int ret;
81 hash->key = user_token >> PAGE_SHIFT;
82 ret = drm_ht_insert_item(&dev->map_hash, hash);
83 if (ret != -EINVAL)
84 return ret;
85 }
86 return drm_ht_just_insert_please(&dev->map_hash, hash,
87 user_token, 32 - PAGE_SHIFT - 3,
88 0, DRM_MAP_HASH_OFFSET >> PAGE_SHIFT);
89 }
90
91 /**
92 * Ioctl to specify a range of memory that is available for mapping by a non-root process.
93 *
94 * \param inode device inode.
95 * \param filp file pointer.
96 * \param cmd command.
97 * \param arg pointer to a drm_map structure.
98 * \return zero on success or a negative value on error.
99 *
100 * Adjusts the memory offset to its absolute value according to the mapping
101 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
102 * applicable and if supported by the kernel.
103 */
104 static int drm_addmap_core(struct drm_device * dev, unsigned int offset,
105 unsigned int size, enum drm_map_type type,
106 enum drm_map_flags flags, drm_map_list_t ** maplist)
107 {
108 struct drm_map *map;
109 drm_map_list_t *list;
110 drm_dma_handle_t *dmah;
111 unsigned long user_token;
112 int ret;
113
114 map = drm_alloc(sizeof(*map), DRM_MEM_MAPS);
115 if (!map)
116 return -ENOMEM;
117
118 map->offset = offset;
119 map->size = size;
120 map->flags = flags;
121 map->type = type;
122
123 /* Only allow shared memory to be removable since we only keep enough
124 * book keeping information about shared memory to allow for removal
125 * when processes fork.
126 */
127 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
128 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
129 return -EINVAL;
130 }
131 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n",
132 map->offset, map->size, map->type);
133 if ((map->offset & (~PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
134 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
135 return -EINVAL;
136 }
137 map->mtrr = -1;
138 map->handle = NULL;
139
140 switch (map->type) {
141 case _DRM_REGISTERS:
142 case _DRM_FRAME_BUFFER:
143 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__)
144 if (map->offset + (map->size-1) < map->offset ||
145 map->offset < virt_to_phys(high_memory)) {
146 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
147 return -EINVAL;
148 }
149 #endif
150 #ifdef __alpha__
151 map->offset += dev->hose->mem_space->start;
152 #endif
153 /* Some drivers preinitialize some maps, without the X Server
154 * needing to be aware of it. Therefore, we just return success
155 * when the server tries to create a duplicate map.
156 */
157 list = drm_find_matching_map(dev, map);
158 if (list != NULL) {
159 if (list->map->size != map->size) {
160 DRM_DEBUG("Matching maps of type %d with "
161 "mismatched sizes, (%ld vs %ld)\n",
162 map->type, map->size,
163 list->map->size);
164 list->map->size = map->size;
165 }
166
167 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
168 *maplist = list;
169 return 0;
170 }
171
172 if (drm_core_has_MTRR(dev)) {
173 if (map->type == _DRM_FRAME_BUFFER ||
174 (map->flags & _DRM_WRITE_COMBINING)) {
175 map->mtrr = mtrr_add(map->offset, map->size,
176 MTRR_TYPE_WRCOMB, 1);
177 }
178 }
179 if (map->type == _DRM_REGISTERS)
180 map->handle = ioremap(map->offset, map->size);
181 break;
182 case _DRM_SHM:
183 list = drm_find_matching_map(dev, map);
184 if (list != NULL) {
185 if(list->map->size != map->size) {
186 DRM_DEBUG("Matching maps of type %d with "
187 "mismatched sizes, (%ld vs %ld)\n",
188 map->type, map->size, list->map->size);
189 list->map->size = map->size;
190 }
191
192 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
193 *maplist = list;
194 return 0;
195 }
196 map->handle = vmalloc_user(map->size);
197 DRM_DEBUG("%lu %d %p\n",
198 map->size, drm_order(map->size), map->handle);
199 if (!map->handle) {
200 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
201 return -ENOMEM;
202 }
203 map->offset = (unsigned long)map->handle;
204 if (map->flags & _DRM_CONTAINS_LOCK) {
205 /* Prevent a 2nd X Server from creating a 2nd lock */
206 if (dev->lock.hw_lock != NULL) {
207 vfree(map->handle);
208 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
209 return -EBUSY;
210 }
211 dev->sigdata.lock = dev->lock.hw_lock = map->handle; /* Pointer to lock */
212 }
213 break;
214 case _DRM_AGP: {
215 drm_agp_mem_t *entry;
216 int valid = 0;
217
218 if (!drm_core_has_AGP(dev)) {
219 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
220 return -EINVAL;
221 }
222 #ifdef __alpha__
223 map->offset += dev->hose->mem_space->start;
224 #endif
225 /* Note: dev->agp->base may actually be 0 when the DRM
226 * is not in control of AGP space. But if user space is
227 * it should already have added the AGP base itself.
228 */
229 map->offset += dev->agp->base;
230 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
231
232 /* This assumes the DRM is in total control of AGP space.
233 * It's not always the case as AGP can be in the control
234 * of user space (i.e. i810 driver). So this loop will get
235 * skipped and we double check that dev->agp->memory is
236 * actually set as well as being invalid before EPERM'ing
237 */
238 list_for_each_entry(entry, &dev->agp->memory, head) {
239 if ((map->offset >= entry->bound) &&
240 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
241 valid = 1;
242 break;
243 }
244 }
245 if (!list_empty(&dev->agp->memory) && !valid) {
246 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
247 return -EPERM;
248 }
249 DRM_DEBUG("AGP offset = 0x%08lx, size = 0x%08lx\n", map->offset, map->size);
250
251 break;
252 }
253 case _DRM_SCATTER_GATHER:
254 if (!dev->sg) {
255 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
256 return -EINVAL;
257 }
258 map->offset += (unsigned long)dev->sg->virtual;
259 break;
260 case _DRM_CONSISTENT:
261 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
262 * As we're limiting the address to 2^32-1 (or less),
263 * casting it down to 32 bits is no problem, but we
264 * need to point to a 64bit variable first. */
265 dmah = drm_pci_alloc(dev, map->size, map->size, 0xffffffffUL);
266 if (!dmah) {
267 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
268 return -ENOMEM;
269 }
270 map->handle = dmah->vaddr;
271 map->offset = (unsigned long)dmah->busaddr;
272 kfree(dmah);
273 break;
274 default:
275 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
276 return -EINVAL;
277 }
278
279 list = drm_alloc(sizeof(*list), DRM_MEM_MAPS);
280 if (!list) {
281 if (map->type == _DRM_REGISTERS)
282 iounmap(map->handle);
283 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
284 return -EINVAL;
285 }
286 memset(list, 0, sizeof(*list));
287 list->map = map;
288
289 mutex_lock(&dev->struct_mutex);
290 list_add(&list->head, &dev->maplist);
291
292 /* Assign a 32-bit handle */
293 /* We do it here so that dev->struct_mutex protects the increment */
294 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
295 map->offset;
296 ret = drm_map_handle(dev, &list->hash, user_token, 0);
297 if (ret) {
298 if (map->type == _DRM_REGISTERS)
299 iounmap(map->handle);
300 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
301 drm_free(list, sizeof(*list), DRM_MEM_MAPS);
302 mutex_unlock(&dev->struct_mutex);
303 return ret;
304 }
305
306 list->user_token = list->hash.key << PAGE_SHIFT;
307 mutex_unlock(&dev->struct_mutex);
308
309 *maplist = list;
310 return 0;
311 }
312
313 int drm_addmap(struct drm_device * dev, unsigned int offset,
314 unsigned int size, enum drm_map_type type,
315 enum drm_map_flags flags, drm_local_map_t ** map_ptr)
316 {
317 drm_map_list_t *list;
318 int rc;
319
320 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
321 if (!rc)
322 *map_ptr = list->map;
323 return rc;
324 }
325
326 EXPORT_SYMBOL(drm_addmap);
327
328 int drm_addmap_ioctl(struct inode *inode, struct file *filp,
329 unsigned int cmd, unsigned long arg)
330 {
331 struct drm_file *priv = filp->private_data;
332 struct drm_device *dev = priv->head->dev;
333 struct drm_map map;
334 drm_map_list_t *maplist;
335 struct drm_map __user *argp = (void __user *)arg;
336 int err;
337
338 if (!(filp->f_mode & 3))
339 return -EACCES; /* Require read/write */
340
341 if (copy_from_user(&map, argp, sizeof(map))) {
342 return -EFAULT;
343 }
344
345 if (!(capable(CAP_SYS_ADMIN) || map.type == _DRM_AGP))
346 return -EPERM;
347
348 err = drm_addmap_core(dev, map.offset, map.size, map.type, map.flags,
349 &maplist);
350
351 if (err)
352 return err;
353
354 if (copy_to_user(argp, maplist->map, sizeof(struct drm_map)))
355 return -EFAULT;
356
357 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
358 if (put_user((void *)(unsigned long)maplist->user_token, &argp->handle))
359 return -EFAULT;
360 return 0;
361 }
362
363 /**
364 * Remove a map private from list and deallocate resources if the mapping
365 * isn't in use.
366 *
367 * \param inode device inode.
368 * \param filp file pointer.
369 * \param cmd command.
370 * \param arg pointer to a struct drm_map structure.
371 * \return zero on success or a negative value on error.
372 *
373 * Searches the map on drm_device::maplist, removes it from the list, see if
374 * its being used, and free any associate resource (such as MTRR's) if it's not
375 * being on use.
376 *
377 * \sa drm_addmap
378 */
379 int drm_rmmap_locked(struct drm_device *dev, drm_local_map_t *map)
380 {
381 drm_map_list_t *r_list = NULL, *list_t;
382 drm_dma_handle_t dmah;
383 int found = 0;
384
385 /* Find the list entry for the map and remove it */
386 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
387 if (r_list->map == map) {
388 list_del(&r_list->head);
389 drm_ht_remove_key(&dev->map_hash,
390 r_list->user_token >> PAGE_SHIFT);
391 drm_free(r_list, sizeof(*r_list), DRM_MEM_MAPS);
392 found = 1;
393 break;
394 }
395 }
396
397 if (!found)
398 return -EINVAL;
399
400 switch (map->type) {
401 case _DRM_REGISTERS:
402 iounmap(map->handle);
403 /* FALLTHROUGH */
404 case _DRM_FRAME_BUFFER:
405 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
406 int retcode;
407 retcode = mtrr_del(map->mtrr, map->offset, map->size);
408 DRM_DEBUG("mtrr_del=%d\n", retcode);
409 }
410 break;
411 case _DRM_SHM:
412 vfree(map->handle);
413 break;
414 case _DRM_AGP:
415 case _DRM_SCATTER_GATHER:
416 break;
417 case _DRM_CONSISTENT:
418 dmah.vaddr = map->handle;
419 dmah.busaddr = map->offset;
420 dmah.size = map->size;
421 __drm_pci_free(dev, &dmah);
422 break;
423 }
424 drm_free(map, sizeof(*map), DRM_MEM_MAPS);
425
426 return 0;
427 }
428
429 int drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
430 {
431 int ret;
432
433 mutex_lock(&dev->struct_mutex);
434 ret = drm_rmmap_locked(dev, map);
435 mutex_unlock(&dev->struct_mutex);
436
437 return ret;
438 }
439
440 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
441 * the last close of the device, and this is necessary for cleanup when things
442 * exit uncleanly. Therefore, having userland manually remove mappings seems
443 * like a pointless exercise since they're going away anyway.
444 *
445 * One use case might be after addmap is allowed for normal users for SHM and
446 * gets used by drivers that the server doesn't need to care about. This seems
447 * unlikely.
448 */
449 int drm_rmmap_ioctl(struct inode *inode, struct file *filp,
450 unsigned int cmd, unsigned long arg)
451 {
452 struct drm_file *priv = filp->private_data;
453 struct drm_device *dev = priv->head->dev;
454 struct drm_map request;
455 drm_local_map_t *map = NULL;
456 drm_map_list_t *r_list;
457 int ret;
458
459 if (copy_from_user(&request, (struct drm_map __user *) arg, sizeof(request))) {
460 return -EFAULT;
461 }
462
463 mutex_lock(&dev->struct_mutex);
464 list_for_each_entry(r_list, &dev->maplist, head) {
465 if (r_list->map &&
466 r_list->user_token == (unsigned long)request.handle &&
467 r_list->map->flags & _DRM_REMOVABLE) {
468 map = r_list->map;
469 break;
470 }
471 }
472
473 /* List has wrapped around to the head pointer, or its empty we didn't
474 * find anything.
475 */
476 if (list_empty(&dev->maplist) || !map) {
477 mutex_unlock(&dev->struct_mutex);
478 return -EINVAL;
479 }
480
481 if (!map) {
482 mutex_unlock(&dev->struct_mutex);
483 return -EINVAL;
484 }
485
486 /* Register and framebuffer maps are permanent */
487 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
488 mutex_unlock(&dev->struct_mutex);
489 return 0;
490 }
491
492 ret = drm_rmmap_locked(dev, map);
493
494 mutex_unlock(&dev->struct_mutex);
495
496 return ret;
497 }
498
499 /**
500 * Cleanup after an error on one of the addbufs() functions.
501 *
502 * \param dev DRM device.
503 * \param entry buffer entry where the error occurred.
504 *
505 * Frees any pages and buffers associated with the given entry.
506 */
507 static void drm_cleanup_buf_error(struct drm_device * dev,
508 struct drm_buf_entry * entry)
509 {
510 int i;
511
512 if (entry->seg_count) {
513 for (i = 0; i < entry->seg_count; i++) {
514 if (entry->seglist[i]) {
515 drm_pci_free(dev, entry->seglist[i]);
516 }
517 }
518 drm_free(entry->seglist,
519 entry->seg_count *
520 sizeof(*entry->seglist), DRM_MEM_SEGS);
521
522 entry->seg_count = 0;
523 }
524
525 if (entry->buf_count) {
526 for (i = 0; i < entry->buf_count; i++) {
527 if (entry->buflist[i].dev_private) {
528 drm_free(entry->buflist[i].dev_private,
529 entry->buflist[i].dev_priv_size,
530 DRM_MEM_BUFS);
531 }
532 }
533 drm_free(entry->buflist,
534 entry->buf_count *
535 sizeof(*entry->buflist), DRM_MEM_BUFS);
536
537 entry->buf_count = 0;
538 }
539 }
540
541 #if __OS_HAS_AGP
542 /**
543 * Add AGP buffers for DMA transfers.
544 *
545 * \param dev struct drm_device to which the buffers are to be added.
546 * \param request pointer to a struct drm_buf_desc describing the request.
547 * \return zero on success or a negative number on failure.
548 *
549 * After some sanity checks creates a drm_buf structure for each buffer and
550 * reallocates the buffer list of the same size order to accommodate the new
551 * buffers.
552 */
553 int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
554 {
555 struct drm_device_dma *dma = dev->dma;
556 struct drm_buf_entry *entry;
557 drm_agp_mem_t *agp_entry;
558 struct drm_buf *buf;
559 unsigned long offset;
560 unsigned long agp_offset;
561 int count;
562 int order;
563 int size;
564 int alignment;
565 int page_order;
566 int total;
567 int byte_count;
568 int i, valid;
569 struct drm_buf **temp_buflist;
570
571 if (!dma)
572 return -EINVAL;
573
574 count = request->count;
575 order = drm_order(request->size);
576 size = 1 << order;
577
578 alignment = (request->flags & _DRM_PAGE_ALIGN)
579 ? PAGE_ALIGN(size) : size;
580 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
581 total = PAGE_SIZE << page_order;
582
583 byte_count = 0;
584 agp_offset = dev->agp->base + request->agp_start;
585
586 DRM_DEBUG("count: %d\n", count);
587 DRM_DEBUG("order: %d\n", order);
588 DRM_DEBUG("size: %d\n", size);
589 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
590 DRM_DEBUG("alignment: %d\n", alignment);
591 DRM_DEBUG("page_order: %d\n", page_order);
592 DRM_DEBUG("total: %d\n", total);
593
594 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
595 return -EINVAL;
596 if (dev->queue_count)
597 return -EBUSY; /* Not while in use */
598
599 /* Make sure buffers are located in AGP memory that we own */
600 valid = 0;
601 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
602 if ((agp_offset >= agp_entry->bound) &&
603 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
604 valid = 1;
605 break;
606 }
607 }
608 if (!list_empty(&dev->agp->memory) && !valid) {
609 DRM_DEBUG("zone invalid\n");
610 return -EINVAL;
611 }
612 spin_lock(&dev->count_lock);
613 if (dev->buf_use) {
614 spin_unlock(&dev->count_lock);
615 return -EBUSY;
616 }
617 atomic_inc(&dev->buf_alloc);
618 spin_unlock(&dev->count_lock);
619
620 mutex_lock(&dev->struct_mutex);
621 entry = &dma->bufs[order];
622 if (entry->buf_count) {
623 mutex_unlock(&dev->struct_mutex);
624 atomic_dec(&dev->buf_alloc);
625 return -ENOMEM; /* May only call once for each order */
626 }
627
628 if (count < 0 || count > 4096) {
629 mutex_unlock(&dev->struct_mutex);
630 atomic_dec(&dev->buf_alloc);
631 return -EINVAL;
632 }
633
634 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
635 DRM_MEM_BUFS);
636 if (!entry->buflist) {
637 mutex_unlock(&dev->struct_mutex);
638 atomic_dec(&dev->buf_alloc);
639 return -ENOMEM;
640 }
641 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
642
643 entry->buf_size = size;
644 entry->page_order = page_order;
645
646 offset = 0;
647
648 while (entry->buf_count < count) {
649 buf = &entry->buflist[entry->buf_count];
650 buf->idx = dma->buf_count + entry->buf_count;
651 buf->total = alignment;
652 buf->order = order;
653 buf->used = 0;
654
655 buf->offset = (dma->byte_count + offset);
656 buf->bus_address = agp_offset + offset;
657 buf->address = (void *)(agp_offset + offset);
658 buf->next = NULL;
659 buf->waiting = 0;
660 buf->pending = 0;
661 init_waitqueue_head(&buf->dma_wait);
662 buf->filp = NULL;
663
664 buf->dev_priv_size = dev->driver->dev_priv_size;
665 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
666 if (!buf->dev_private) {
667 /* Set count correctly so we free the proper amount. */
668 entry->buf_count = count;
669 drm_cleanup_buf_error(dev, entry);
670 mutex_unlock(&dev->struct_mutex);
671 atomic_dec(&dev->buf_alloc);
672 return -ENOMEM;
673 }
674 memset(buf->dev_private, 0, buf->dev_priv_size);
675
676 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
677
678 offset += alignment;
679 entry->buf_count++;
680 byte_count += PAGE_SIZE << page_order;
681 }
682
683 DRM_DEBUG("byte_count: %d\n", byte_count);
684
685 temp_buflist = drm_realloc(dma->buflist,
686 dma->buf_count * sizeof(*dma->buflist),
687 (dma->buf_count + entry->buf_count)
688 * sizeof(*dma->buflist), DRM_MEM_BUFS);
689 if (!temp_buflist) {
690 /* Free the entry because it isn't valid */
691 drm_cleanup_buf_error(dev, entry);
692 mutex_unlock(&dev->struct_mutex);
693 atomic_dec(&dev->buf_alloc);
694 return -ENOMEM;
695 }
696 dma->buflist = temp_buflist;
697
698 for (i = 0; i < entry->buf_count; i++) {
699 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
700 }
701
702 dma->buf_count += entry->buf_count;
703 dma->seg_count += entry->seg_count;
704 dma->page_count += byte_count >> PAGE_SHIFT;
705 dma->byte_count += byte_count;
706
707 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
708 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
709
710 mutex_unlock(&dev->struct_mutex);
711
712 request->count = entry->buf_count;
713 request->size = size;
714
715 dma->flags = _DRM_DMA_USE_AGP;
716
717 atomic_dec(&dev->buf_alloc);
718 return 0;
719 }
720 EXPORT_SYMBOL(drm_addbufs_agp);
721 #endif /* __OS_HAS_AGP */
722
723 int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
724 {
725 struct drm_device_dma *dma = dev->dma;
726 int count;
727 int order;
728 int size;
729 int total;
730 int page_order;
731 struct drm_buf_entry *entry;
732 drm_dma_handle_t *dmah;
733 struct drm_buf *buf;
734 int alignment;
735 unsigned long offset;
736 int i;
737 int byte_count;
738 int page_count;
739 unsigned long *temp_pagelist;
740 struct drm_buf **temp_buflist;
741
742 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
743 return -EINVAL;
744
745 if (!dma)
746 return -EINVAL;
747
748 if (!capable(CAP_SYS_ADMIN))
749 return -EPERM;
750
751 count = request->count;
752 order = drm_order(request->size);
753 size = 1 << order;
754
755 DRM_DEBUG("count=%d, size=%d (%d), order=%d, queue_count=%d\n",
756 request->count, request->size, size, order, dev->queue_count);
757
758 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
759 return -EINVAL;
760 if (dev->queue_count)
761 return -EBUSY; /* Not while in use */
762
763 alignment = (request->flags & _DRM_PAGE_ALIGN)
764 ? PAGE_ALIGN(size) : size;
765 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
766 total = PAGE_SIZE << page_order;
767
768 spin_lock(&dev->count_lock);
769 if (dev->buf_use) {
770 spin_unlock(&dev->count_lock);
771 return -EBUSY;
772 }
773 atomic_inc(&dev->buf_alloc);
774 spin_unlock(&dev->count_lock);
775
776 mutex_lock(&dev->struct_mutex);
777 entry = &dma->bufs[order];
778 if (entry->buf_count) {
779 mutex_unlock(&dev->struct_mutex);
780 atomic_dec(&dev->buf_alloc);
781 return -ENOMEM; /* May only call once for each order */
782 }
783
784 if (count < 0 || count > 4096) {
785 mutex_unlock(&dev->struct_mutex);
786 atomic_dec(&dev->buf_alloc);
787 return -EINVAL;
788 }
789
790 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
791 DRM_MEM_BUFS);
792 if (!entry->buflist) {
793 mutex_unlock(&dev->struct_mutex);
794 atomic_dec(&dev->buf_alloc);
795 return -ENOMEM;
796 }
797 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
798
799 entry->seglist = drm_alloc(count * sizeof(*entry->seglist),
800 DRM_MEM_SEGS);
801 if (!entry->seglist) {
802 drm_free(entry->buflist,
803 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
804 mutex_unlock(&dev->struct_mutex);
805 atomic_dec(&dev->buf_alloc);
806 return -ENOMEM;
807 }
808 memset(entry->seglist, 0, count * sizeof(*entry->seglist));
809
810 /* Keep the original pagelist until we know all the allocations
811 * have succeeded
812 */
813 temp_pagelist = drm_alloc((dma->page_count + (count << page_order))
814 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
815 if (!temp_pagelist) {
816 drm_free(entry->buflist,
817 count * sizeof(*entry->buflist), DRM_MEM_BUFS);
818 drm_free(entry->seglist,
819 count * sizeof(*entry->seglist), DRM_MEM_SEGS);
820 mutex_unlock(&dev->struct_mutex);
821 atomic_dec(&dev->buf_alloc);
822 return -ENOMEM;
823 }
824 memcpy(temp_pagelist,
825 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
826 DRM_DEBUG("pagelist: %d entries\n",
827 dma->page_count + (count << page_order));
828
829 entry->buf_size = size;
830 entry->page_order = page_order;
831 byte_count = 0;
832 page_count = 0;
833
834 while (entry->buf_count < count) {
835
836 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000, 0xfffffffful);
837
838 if (!dmah) {
839 /* Set count correctly so we free the proper amount. */
840 entry->buf_count = count;
841 entry->seg_count = count;
842 drm_cleanup_buf_error(dev, entry);
843 drm_free(temp_pagelist,
844 (dma->page_count + (count << page_order))
845 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
846 mutex_unlock(&dev->struct_mutex);
847 atomic_dec(&dev->buf_alloc);
848 return -ENOMEM;
849 }
850 entry->seglist[entry->seg_count++] = dmah;
851 for (i = 0; i < (1 << page_order); i++) {
852 DRM_DEBUG("page %d @ 0x%08lx\n",
853 dma->page_count + page_count,
854 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
855 temp_pagelist[dma->page_count + page_count++]
856 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
857 }
858 for (offset = 0;
859 offset + size <= total && entry->buf_count < count;
860 offset += alignment, ++entry->buf_count) {
861 buf = &entry->buflist[entry->buf_count];
862 buf->idx = dma->buf_count + entry->buf_count;
863 buf->total = alignment;
864 buf->order = order;
865 buf->used = 0;
866 buf->offset = (dma->byte_count + byte_count + offset);
867 buf->address = (void *)(dmah->vaddr + offset);
868 buf->bus_address = dmah->busaddr + offset;
869 buf->next = NULL;
870 buf->waiting = 0;
871 buf->pending = 0;
872 init_waitqueue_head(&buf->dma_wait);
873 buf->filp = NULL;
874
875 buf->dev_priv_size = dev->driver->dev_priv_size;
876 buf->dev_private = drm_alloc(buf->dev_priv_size,
877 DRM_MEM_BUFS);
878 if (!buf->dev_private) {
879 /* Set count correctly so we free the proper amount. */
880 entry->buf_count = count;
881 entry->seg_count = count;
882 drm_cleanup_buf_error(dev, entry);
883 drm_free(temp_pagelist,
884 (dma->page_count +
885 (count << page_order))
886 * sizeof(*dma->pagelist),
887 DRM_MEM_PAGES);
888 mutex_unlock(&dev->struct_mutex);
889 atomic_dec(&dev->buf_alloc);
890 return -ENOMEM;
891 }
892 memset(buf->dev_private, 0, buf->dev_priv_size);
893
894 DRM_DEBUG("buffer %d @ %p\n",
895 entry->buf_count, buf->address);
896 }
897 byte_count += PAGE_SIZE << page_order;
898 }
899
900 temp_buflist = drm_realloc(dma->buflist,
901 dma->buf_count * sizeof(*dma->buflist),
902 (dma->buf_count + entry->buf_count)
903 * sizeof(*dma->buflist), DRM_MEM_BUFS);
904 if (!temp_buflist) {
905 /* Free the entry because it isn't valid */
906 drm_cleanup_buf_error(dev, entry);
907 drm_free(temp_pagelist,
908 (dma->page_count + (count << page_order))
909 * sizeof(*dma->pagelist), DRM_MEM_PAGES);
910 mutex_unlock(&dev->struct_mutex);
911 atomic_dec(&dev->buf_alloc);
912 return -ENOMEM;
913 }
914 dma->buflist = temp_buflist;
915
916 for (i = 0; i < entry->buf_count; i++) {
917 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
918 }
919
920 /* No allocations failed, so now we can replace the orginal pagelist
921 * with the new one.
922 */
923 if (dma->page_count) {
924 drm_free(dma->pagelist,
925 dma->page_count * sizeof(*dma->pagelist),
926 DRM_MEM_PAGES);
927 }
928 dma->pagelist = temp_pagelist;
929
930 dma->buf_count += entry->buf_count;
931 dma->seg_count += entry->seg_count;
932 dma->page_count += entry->seg_count << page_order;
933 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
934
935 mutex_unlock(&dev->struct_mutex);
936
937 request->count = entry->buf_count;
938 request->size = size;
939
940 if (request->flags & _DRM_PCI_BUFFER_RO)
941 dma->flags = _DRM_DMA_USE_PCI_RO;
942
943 atomic_dec(&dev->buf_alloc);
944 return 0;
945
946 }
947 EXPORT_SYMBOL(drm_addbufs_pci);
948
949 static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
950 {
951 struct drm_device_dma *dma = dev->dma;
952 struct drm_buf_entry *entry;
953 struct drm_buf *buf;
954 unsigned long offset;
955 unsigned long agp_offset;
956 int count;
957 int order;
958 int size;
959 int alignment;
960 int page_order;
961 int total;
962 int byte_count;
963 int i;
964 struct drm_buf **temp_buflist;
965
966 if (!drm_core_check_feature(dev, DRIVER_SG))
967 return -EINVAL;
968
969 if (!dma)
970 return -EINVAL;
971
972 if (!capable(CAP_SYS_ADMIN))
973 return -EPERM;
974
975 count = request->count;
976 order = drm_order(request->size);
977 size = 1 << order;
978
979 alignment = (request->flags & _DRM_PAGE_ALIGN)
980 ? PAGE_ALIGN(size) : size;
981 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
982 total = PAGE_SIZE << page_order;
983
984 byte_count = 0;
985 agp_offset = request->agp_start;
986
987 DRM_DEBUG("count: %d\n", count);
988 DRM_DEBUG("order: %d\n", order);
989 DRM_DEBUG("size: %d\n", size);
990 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
991 DRM_DEBUG("alignment: %d\n", alignment);
992 DRM_DEBUG("page_order: %d\n", page_order);
993 DRM_DEBUG("total: %d\n", total);
994
995 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
996 return -EINVAL;
997 if (dev->queue_count)
998 return -EBUSY; /* Not while in use */
999
1000 spin_lock(&dev->count_lock);
1001 if (dev->buf_use) {
1002 spin_unlock(&dev->count_lock);
1003 return -EBUSY;
1004 }
1005 atomic_inc(&dev->buf_alloc);
1006 spin_unlock(&dev->count_lock);
1007
1008 mutex_lock(&dev->struct_mutex);
1009 entry = &dma->bufs[order];
1010 if (entry->buf_count) {
1011 mutex_unlock(&dev->struct_mutex);
1012 atomic_dec(&dev->buf_alloc);
1013 return -ENOMEM; /* May only call once for each order */
1014 }
1015
1016 if (count < 0 || count > 4096) {
1017 mutex_unlock(&dev->struct_mutex);
1018 atomic_dec(&dev->buf_alloc);
1019 return -EINVAL;
1020 }
1021
1022 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1023 DRM_MEM_BUFS);
1024 if (!entry->buflist) {
1025 mutex_unlock(&dev->struct_mutex);
1026 atomic_dec(&dev->buf_alloc);
1027 return -ENOMEM;
1028 }
1029 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1030
1031 entry->buf_size = size;
1032 entry->page_order = page_order;
1033
1034 offset = 0;
1035
1036 while (entry->buf_count < count) {
1037 buf = &entry->buflist[entry->buf_count];
1038 buf->idx = dma->buf_count + entry->buf_count;
1039 buf->total = alignment;
1040 buf->order = order;
1041 buf->used = 0;
1042
1043 buf->offset = (dma->byte_count + offset);
1044 buf->bus_address = agp_offset + offset;
1045 buf->address = (void *)(agp_offset + offset
1046 + (unsigned long)dev->sg->virtual);
1047 buf->next = NULL;
1048 buf->waiting = 0;
1049 buf->pending = 0;
1050 init_waitqueue_head(&buf->dma_wait);
1051 buf->filp = NULL;
1052
1053 buf->dev_priv_size = dev->driver->dev_priv_size;
1054 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1055 if (!buf->dev_private) {
1056 /* Set count correctly so we free the proper amount. */
1057 entry->buf_count = count;
1058 drm_cleanup_buf_error(dev, entry);
1059 mutex_unlock(&dev->struct_mutex);
1060 atomic_dec(&dev->buf_alloc);
1061 return -ENOMEM;
1062 }
1063
1064 memset(buf->dev_private, 0, buf->dev_priv_size);
1065
1066 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1067
1068 offset += alignment;
1069 entry->buf_count++;
1070 byte_count += PAGE_SIZE << page_order;
1071 }
1072
1073 DRM_DEBUG("byte_count: %d\n", byte_count);
1074
1075 temp_buflist = drm_realloc(dma->buflist,
1076 dma->buf_count * sizeof(*dma->buflist),
1077 (dma->buf_count + entry->buf_count)
1078 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1079 if (!temp_buflist) {
1080 /* Free the entry because it isn't valid */
1081 drm_cleanup_buf_error(dev, entry);
1082 mutex_unlock(&dev->struct_mutex);
1083 atomic_dec(&dev->buf_alloc);
1084 return -ENOMEM;
1085 }
1086 dma->buflist = temp_buflist;
1087
1088 for (i = 0; i < entry->buf_count; i++) {
1089 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1090 }
1091
1092 dma->buf_count += entry->buf_count;
1093 dma->seg_count += entry->seg_count;
1094 dma->page_count += byte_count >> PAGE_SHIFT;
1095 dma->byte_count += byte_count;
1096
1097 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1098 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1099
1100 mutex_unlock(&dev->struct_mutex);
1101
1102 request->count = entry->buf_count;
1103 request->size = size;
1104
1105 dma->flags = _DRM_DMA_USE_SG;
1106
1107 atomic_dec(&dev->buf_alloc);
1108 return 0;
1109 }
1110
1111 static int drm_addbufs_fb(struct drm_device * dev, struct drm_buf_desc * request)
1112 {
1113 struct drm_device_dma *dma = dev->dma;
1114 struct drm_buf_entry *entry;
1115 struct drm_buf *buf;
1116 unsigned long offset;
1117 unsigned long agp_offset;
1118 int count;
1119 int order;
1120 int size;
1121 int alignment;
1122 int page_order;
1123 int total;
1124 int byte_count;
1125 int i;
1126 struct drm_buf **temp_buflist;
1127
1128 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1129 return -EINVAL;
1130
1131 if (!dma)
1132 return -EINVAL;
1133
1134 if (!capable(CAP_SYS_ADMIN))
1135 return -EPERM;
1136
1137 count = request->count;
1138 order = drm_order(request->size);
1139 size = 1 << order;
1140
1141 alignment = (request->flags & _DRM_PAGE_ALIGN)
1142 ? PAGE_ALIGN(size) : size;
1143 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1144 total = PAGE_SIZE << page_order;
1145
1146 byte_count = 0;
1147 agp_offset = request->agp_start;
1148
1149 DRM_DEBUG("count: %d\n", count);
1150 DRM_DEBUG("order: %d\n", order);
1151 DRM_DEBUG("size: %d\n", size);
1152 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1153 DRM_DEBUG("alignment: %d\n", alignment);
1154 DRM_DEBUG("page_order: %d\n", page_order);
1155 DRM_DEBUG("total: %d\n", total);
1156
1157 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1158 return -EINVAL;
1159 if (dev->queue_count)
1160 return -EBUSY; /* Not while in use */
1161
1162 spin_lock(&dev->count_lock);
1163 if (dev->buf_use) {
1164 spin_unlock(&dev->count_lock);
1165 return -EBUSY;
1166 }
1167 atomic_inc(&dev->buf_alloc);
1168 spin_unlock(&dev->count_lock);
1169
1170 mutex_lock(&dev->struct_mutex);
1171 entry = &dma->bufs[order];
1172 if (entry->buf_count) {
1173 mutex_unlock(&dev->struct_mutex);
1174 atomic_dec(&dev->buf_alloc);
1175 return -ENOMEM; /* May only call once for each order */
1176 }
1177
1178 if (count < 0 || count > 4096) {
1179 mutex_unlock(&dev->struct_mutex);
1180 atomic_dec(&dev->buf_alloc);
1181 return -EINVAL;
1182 }
1183
1184 entry->buflist = drm_alloc(count * sizeof(*entry->buflist),
1185 DRM_MEM_BUFS);
1186 if (!entry->buflist) {
1187 mutex_unlock(&dev->struct_mutex);
1188 atomic_dec(&dev->buf_alloc);
1189 return -ENOMEM;
1190 }
1191 memset(entry->buflist, 0, count * sizeof(*entry->buflist));
1192
1193 entry->buf_size = size;
1194 entry->page_order = page_order;
1195
1196 offset = 0;
1197
1198 while (entry->buf_count < count) {
1199 buf = &entry->buflist[entry->buf_count];
1200 buf->idx = dma->buf_count + entry->buf_count;
1201 buf->total = alignment;
1202 buf->order = order;
1203 buf->used = 0;
1204
1205 buf->offset = (dma->byte_count + offset);
1206 buf->bus_address = agp_offset + offset;
1207 buf->address = (void *)(agp_offset + offset);
1208 buf->next = NULL;
1209 buf->waiting = 0;
1210 buf->pending = 0;
1211 init_waitqueue_head(&buf->dma_wait);
1212 buf->filp = NULL;
1213
1214 buf->dev_priv_size = dev->driver->dev_priv_size;
1215 buf->dev_private = drm_alloc(buf->dev_priv_size, DRM_MEM_BUFS);
1216 if (!buf->dev_private) {
1217 /* Set count correctly so we free the proper amount. */
1218 entry->buf_count = count;
1219 drm_cleanup_buf_error(dev, entry);
1220 mutex_unlock(&dev->struct_mutex);
1221 atomic_dec(&dev->buf_alloc);
1222 return -ENOMEM;
1223 }
1224 memset(buf->dev_private, 0, buf->dev_priv_size);
1225
1226 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1227
1228 offset += alignment;
1229 entry->buf_count++;
1230 byte_count += PAGE_SIZE << page_order;
1231 }
1232
1233 DRM_DEBUG("byte_count: %d\n", byte_count);
1234
1235 temp_buflist = drm_realloc(dma->buflist,
1236 dma->buf_count * sizeof(*dma->buflist),
1237 (dma->buf_count + entry->buf_count)
1238 * sizeof(*dma->buflist), DRM_MEM_BUFS);
1239 if (!temp_buflist) {
1240 /* Free the entry because it isn't valid */
1241 drm_cleanup_buf_error(dev, entry);
1242 mutex_unlock(&dev->struct_mutex);
1243 atomic_dec(&dev->buf_alloc);
1244 return -ENOMEM;
1245 }
1246 dma->buflist = temp_buflist;
1247
1248 for (i = 0; i < entry->buf_count; i++) {
1249 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1250 }
1251
1252 dma->buf_count += entry->buf_count;
1253 dma->seg_count += entry->seg_count;
1254 dma->page_count += byte_count >> PAGE_SHIFT;
1255 dma->byte_count += byte_count;
1256
1257 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1258 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1259
1260 mutex_unlock(&dev->struct_mutex);
1261
1262 request->count = entry->buf_count;
1263 request->size = size;
1264
1265 dma->flags = _DRM_DMA_USE_FB;
1266
1267 atomic_dec(&dev->buf_alloc);
1268 return 0;
1269 }
1270
1271
1272 /**
1273 * Add buffers for DMA transfers (ioctl).
1274 *
1275 * \param inode device inode.
1276 * \param filp file pointer.
1277 * \param cmd command.
1278 * \param arg pointer to a struct drm_buf_desc request.
1279 * \return zero on success or a negative number on failure.
1280 *
1281 * According with the memory type specified in drm_buf_desc::flags and the
1282 * build options, it dispatches the call either to addbufs_agp(),
1283 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1284 * PCI memory respectively.
1285 */
1286 int drm_addbufs(struct inode *inode, struct file *filp,
1287 unsigned int cmd, unsigned long arg)
1288 {
1289 struct drm_buf_desc request;
1290 struct drm_file *priv = filp->private_data;
1291 struct drm_device *dev = priv->head->dev;
1292 int ret;
1293
1294 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1295 return -EINVAL;
1296
1297 if (copy_from_user(&request, (struct drm_buf_desc __user *) arg,
1298 sizeof(request)))
1299 return -EFAULT;
1300
1301 #if __OS_HAS_AGP
1302 if (request.flags & _DRM_AGP_BUFFER)
1303 ret = drm_addbufs_agp(dev, &request);
1304 else
1305 #endif
1306 if (request.flags & _DRM_SG_BUFFER)
1307 ret = drm_addbufs_sg(dev, &request);
1308 else if (request.flags & _DRM_FB_BUFFER)
1309 ret = drm_addbufs_fb(dev, &request);
1310 else
1311 ret = drm_addbufs_pci(dev, &request);
1312
1313 if (ret == 0) {
1314 if (copy_to_user((void __user *)arg, &request, sizeof(request))) {
1315 ret = -EFAULT;
1316 }
1317 }
1318 return ret;
1319 }
1320
1321 /**
1322 * Get information about the buffer mappings.
1323 *
1324 * This was originally mean for debugging purposes, or by a sophisticated
1325 * client library to determine how best to use the available buffers (e.g.,
1326 * large buffers can be used for image transfer).
1327 *
1328 * \param inode device inode.
1329 * \param filp file pointer.
1330 * \param cmd command.
1331 * \param arg pointer to a drm_buf_info structure.
1332 * \return zero on success or a negative number on failure.
1333 *
1334 * Increments drm_device::buf_use while holding the drm_device::count_lock
1335 * lock, preventing of allocating more buffers after this call. Information
1336 * about each requested buffer is then copied into user space.
1337 */
1338 int drm_infobufs(struct inode *inode, struct file *filp,
1339 unsigned int cmd, unsigned long arg)
1340 {
1341 struct drm_file *priv = filp->private_data;
1342 struct drm_device *dev = priv->head->dev;
1343 struct drm_device_dma *dma = dev->dma;
1344 struct drm_buf_info request;
1345 struct drm_buf_info __user *argp = (void __user *)arg;
1346 int i;
1347 int count;
1348
1349 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1350 return -EINVAL;
1351
1352 if (!dma)
1353 return -EINVAL;
1354
1355 spin_lock(&dev->count_lock);
1356 if (atomic_read(&dev->buf_alloc)) {
1357 spin_unlock(&dev->count_lock);
1358 return -EBUSY;
1359 }
1360 ++dev->buf_use; /* Can't allocate more after this call */
1361 spin_unlock(&dev->count_lock);
1362
1363 if (copy_from_user(&request, argp, sizeof(request)))
1364 return -EFAULT;
1365
1366 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1367 if (dma->bufs[i].buf_count)
1368 ++count;
1369 }
1370
1371 DRM_DEBUG("count = %d\n", count);
1372
1373 if (request.count >= count) {
1374 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1375 if (dma->bufs[i].buf_count) {
1376 struct drm_buf_desc __user *to =
1377 &request.list[count];
1378 struct drm_buf_entry *from = &dma->bufs[i];
1379 struct drm_freelist *list = &dma->bufs[i].freelist;
1380 if (copy_to_user(&to->count,
1381 &from->buf_count,
1382 sizeof(from->buf_count)) ||
1383 copy_to_user(&to->size,
1384 &from->buf_size,
1385 sizeof(from->buf_size)) ||
1386 copy_to_user(&to->low_mark,
1387 &list->low_mark,
1388 sizeof(list->low_mark)) ||
1389 copy_to_user(&to->high_mark,
1390 &list->high_mark,
1391 sizeof(list->high_mark)))
1392 return -EFAULT;
1393
1394 DRM_DEBUG("%d %d %d %d %d\n",
1395 i,
1396 dma->bufs[i].buf_count,
1397 dma->bufs[i].buf_size,
1398 dma->bufs[i].freelist.low_mark,
1399 dma->bufs[i].freelist.high_mark);
1400 ++count;
1401 }
1402 }
1403 }
1404 request.count = count;
1405
1406 if (copy_to_user(argp, &request, sizeof(request)))
1407 return -EFAULT;
1408
1409 return 0;
1410 }
1411
1412 /**
1413 * Specifies a low and high water mark for buffer allocation
1414 *
1415 * \param inode device inode.
1416 * \param filp file pointer.
1417 * \param cmd command.
1418 * \param arg a pointer to a drm_buf_desc structure.
1419 * \return zero on success or a negative number on failure.
1420 *
1421 * Verifies that the size order is bounded between the admissible orders and
1422 * updates the respective drm_device_dma::bufs entry low and high water mark.
1423 *
1424 * \note This ioctl is deprecated and mostly never used.
1425 */
1426 int drm_markbufs(struct inode *inode, struct file *filp,
1427 unsigned int cmd, unsigned long arg)
1428 {
1429 struct drm_file *priv = filp->private_data;
1430 struct drm_device *dev = priv->head->dev;
1431 struct drm_device_dma *dma = dev->dma;
1432 struct drm_buf_desc request;
1433 int order;
1434 struct drm_buf_entry *entry;
1435
1436 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1437 return -EINVAL;
1438
1439 if (!dma)
1440 return -EINVAL;
1441
1442 if (copy_from_user(&request,
1443 (struct drm_buf_desc __user *) arg, sizeof(request)))
1444 return -EFAULT;
1445
1446 DRM_DEBUG("%d, %d, %d\n",
1447 request.size, request.low_mark, request.high_mark);
1448 order = drm_order(request.size);
1449 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1450 return -EINVAL;
1451 entry = &dma->bufs[order];
1452
1453 if (request.low_mark < 0 || request.low_mark > entry->buf_count)
1454 return -EINVAL;
1455 if (request.high_mark < 0 || request.high_mark > entry->buf_count)
1456 return -EINVAL;
1457
1458 entry->freelist.low_mark = request.low_mark;
1459 entry->freelist.high_mark = request.high_mark;
1460
1461 return 0;
1462 }
1463
1464 /**
1465 * Unreserve the buffers in list, previously reserved using drmDMA.
1466 *
1467 * \param inode device inode.
1468 * \param filp file pointer.
1469 * \param cmd command.
1470 * \param arg pointer to a drm_buf_free structure.
1471 * \return zero on success or a negative number on failure.
1472 *
1473 * Calls free_buffer() for each used buffer.
1474 * This function is primarily used for debugging.
1475 */
1476 int drm_freebufs(struct inode *inode, struct file *filp,
1477 unsigned int cmd, unsigned long arg)
1478 {
1479 struct drm_file *priv = filp->private_data;
1480 struct drm_device *dev = priv->head->dev;
1481 struct drm_device_dma *dma = dev->dma;
1482 struct drm_buf_free request;
1483 int i;
1484 int idx;
1485 struct drm_buf *buf;
1486
1487 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1488 return -EINVAL;
1489
1490 if (!dma)
1491 return -EINVAL;
1492
1493 if (copy_from_user(&request,
1494 (struct drm_buf_free __user *) arg, sizeof(request)))
1495 return -EFAULT;
1496
1497 DRM_DEBUG("%d\n", request.count);
1498 for (i = 0; i < request.count; i++) {
1499 if (copy_from_user(&idx, &request.list[i], sizeof(idx)))
1500 return -EFAULT;
1501 if (idx < 0 || idx >= dma->buf_count) {
1502 DRM_ERROR("Index %d (of %d max)\n",
1503 idx, dma->buf_count - 1);
1504 return -EINVAL;
1505 }
1506 buf = dma->buflist[idx];
1507 if (buf->filp != filp) {
1508 DRM_ERROR("Process %d freeing buffer not owned\n",
1509 current->pid);
1510 return -EINVAL;
1511 }
1512 drm_free_buffer(dev, buf);
1513 }
1514
1515 return 0;
1516 }
1517
1518 /**
1519 * Maps all of the DMA buffers into client-virtual space (ioctl).
1520 *
1521 * \param inode device inode.
1522 * \param filp file pointer.
1523 * \param cmd command.
1524 * \param arg pointer to a drm_buf_map structure.
1525 * \return zero on success or a negative number on failure.
1526 *
1527 * Maps the AGP, SG or PCI buffer region with do_mmap(), and copies information
1528 * about each buffer into user space. For PCI buffers, it calls do_mmap() with
1529 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1530 * drm_mmap_dma().
1531 */
1532 int drm_mapbufs(struct inode *inode, struct file *filp,
1533 unsigned int cmd, unsigned long arg)
1534 {
1535 struct drm_file *priv = filp->private_data;
1536 struct drm_device *dev = priv->head->dev;
1537 struct drm_device_dma *dma = dev->dma;
1538 struct drm_buf_map __user *argp = (void __user *)arg;
1539 int retcode = 0;
1540 const int zero = 0;
1541 unsigned long virtual;
1542 unsigned long address;
1543 struct drm_buf_map request;
1544 int i;
1545
1546 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1547 return -EINVAL;
1548
1549 if (!dma)
1550 return -EINVAL;
1551
1552 spin_lock(&dev->count_lock);
1553 if (atomic_read(&dev->buf_alloc)) {
1554 spin_unlock(&dev->count_lock);
1555 return -EBUSY;
1556 }
1557 dev->buf_use++; /* Can't allocate more after this call */
1558 spin_unlock(&dev->count_lock);
1559
1560 if (copy_from_user(&request, argp, sizeof(request)))
1561 return -EFAULT;
1562
1563 if (request.count >= dma->buf_count) {
1564 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1565 || (drm_core_check_feature(dev, DRIVER_SG)
1566 && (dma->flags & _DRM_DMA_USE_SG))
1567 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1568 && (dma->flags & _DRM_DMA_USE_FB))) {
1569 struct drm_map *map = dev->agp_buffer_map;
1570 unsigned long token = dev->agp_buffer_token;
1571
1572 if (!map) {
1573 retcode = -EINVAL;
1574 goto done;
1575 }
1576
1577 down_write(&current->mm->mmap_sem);
1578 virtual = do_mmap(filp, 0, map->size,
1579 PROT_READ | PROT_WRITE,
1580 MAP_SHARED, token);
1581 up_write(&current->mm->mmap_sem);
1582 } else {
1583 down_write(&current->mm->mmap_sem);
1584 virtual = do_mmap(filp, 0, dma->byte_count,
1585 PROT_READ | PROT_WRITE,
1586 MAP_SHARED, 0);
1587 up_write(&current->mm->mmap_sem);
1588 }
1589 if (virtual > -1024UL) {
1590 /* Real error */
1591 retcode = (signed long)virtual;
1592 goto done;
1593 }
1594 request.virtual = (void __user *)virtual;
1595
1596 for (i = 0; i < dma->buf_count; i++) {
1597 if (copy_to_user(&request.list[i].idx,
1598 &dma->buflist[i]->idx,
1599 sizeof(request.list[0].idx))) {
1600 retcode = -EFAULT;
1601 goto done;
1602 }
1603 if (copy_to_user(&request.list[i].total,
1604 &dma->buflist[i]->total,
1605 sizeof(request.list[0].total))) {
1606 retcode = -EFAULT;
1607 goto done;
1608 }
1609 if (copy_to_user(&request.list[i].used,
1610 &zero, sizeof(zero))) {
1611 retcode = -EFAULT;
1612 goto done;
1613 }
1614 address = virtual + dma->buflist[i]->offset; /* *** */
1615 if (copy_to_user(&request.list[i].address,
1616 &address, sizeof(address))) {
1617 retcode = -EFAULT;
1618 goto done;
1619 }
1620 }
1621 }
1622 done:
1623 request.count = dma->buf_count;
1624 DRM_DEBUG("%d buffers, retcode = %d\n", request.count, retcode);
1625
1626 if (copy_to_user(argp, &request, sizeof(request)))
1627 return -EFAULT;
1628
1629 return retcode;
1630 }
1631
1632 /**
1633 * Compute size order. Returns the exponent of the smaller power of two which
1634 * is greater or equal to given number.
1635 *
1636 * \param size size.
1637 * \return order.
1638 *
1639 * \todo Can be made faster.
1640 */
1641 int drm_order(unsigned long size)
1642 {
1643 int order;
1644 unsigned long tmp;
1645
1646 for (order = 0, tmp = size >> 1; tmp; tmp >>= 1, order++) ;
1647
1648 if (size & (size - 1))
1649 ++order;
1650
1651 return order;
1652 }
1653 EXPORT_SYMBOL(drm_order);
1654
1655