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