3 * Generic buffer template
12 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
13 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
14 * All Rights Reserved.
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:
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
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.
36 #include <linux/vmalloc.h>
37 #include <linux/slab.h>
38 #include <linux/log2.h>
39 #include <linux/export.h>
40 #include <asm/shmparam.h>
43 static struct drm_map_list *drm_find_matching_map(struct drm_device *dev,
44 struct drm_local_map *map)
46 struct drm_map_list *entry;
47 list_for_each_entry(entry, &dev->maplist, head) {
49 * Because the kernel-userspace ABI is fixed at a 32-bit offset
50 * while PCI resources may live above that, we only compare the
51 * lower 32 bits of the map offset for maps of type
52 * _DRM_FRAMEBUFFER or _DRM_REGISTERS.
53 * It is assumed that if a driver have more than one resource
54 * of each type, the lower 32 bits are different.
57 map->type != entry->map->type ||
58 entry->master != dev->primary->master)
62 if (map->flags != _DRM_CONTAINS_LOCK)
66 case _DRM_FRAME_BUFFER:
67 if ((entry->map->offset & 0xffffffff) ==
68 (map->offset & 0xffffffff))
70 default: /* Make gcc happy */
73 if (entry->map->offset == map->offset)
80 static int drm_map_handle(struct drm_device *dev, struct drm_hash_item *hash,
81 unsigned long user_token, int hashed_handle, int shm)
83 int use_hashed_handle, shift;
86 #if (BITS_PER_LONG == 64)
87 use_hashed_handle = ((user_token & 0xFFFFFFFF00000000UL) || hashed_handle);
88 #elif (BITS_PER_LONG == 32)
89 use_hashed_handle = hashed_handle;
91 #error Unsupported long size. Neither 64 nor 32 bits.
94 if (!use_hashed_handle) {
96 hash->key = user_token >> PAGE_SHIFT;
97 ret = drm_ht_insert_item(&dev->map_hash, hash);
103 add = DRM_MAP_HASH_OFFSET >> PAGE_SHIFT;
104 if (shm && (SHMLBA > PAGE_SIZE)) {
105 int bits = ilog2(SHMLBA >> PAGE_SHIFT) + 1;
107 /* For shared memory, we have to preserve the SHMLBA
108 * bits of the eventual vma->vm_pgoff value during
109 * mmap(). Otherwise we run into cache aliasing problems
110 * on some platforms. On these platforms, the pgoff of
111 * a mmap() request is used to pick a suitable virtual
112 * address for the mmap() region such that it will not
113 * cause cache aliasing problems.
115 * Therefore, make sure the SHMLBA relevant bits of the
116 * hash value we use are equal to those in the original
117 * kernel virtual address.
120 add |= ((user_token >> PAGE_SHIFT) & ((1UL << bits) - 1UL));
123 return drm_ht_just_insert_please(&dev->map_hash, hash,
124 user_token, 32 - PAGE_SHIFT - 3,
129 * Core function to create a range of memory available for mapping by a
132 * Adjusts the memory offset to its absolute value according to the mapping
133 * type. Adds the map to the map list drm_device::maplist. Adds MTRR's where
134 * applicable and if supported by the kernel.
136 static int drm_addmap_core(struct drm_device * dev, resource_size_t offset,
137 unsigned int size, enum drm_map_type type,
138 enum drm_map_flags flags,
139 struct drm_map_list ** maplist)
141 struct drm_local_map *map;
142 struct drm_map_list *list;
143 drm_dma_handle_t *dmah;
144 unsigned long user_token;
147 map = kmalloc(sizeof(*map), GFP_KERNEL);
151 map->offset = offset;
156 /* Only allow shared memory to be removable since we only keep enough
157 * book keeping information about shared memory to allow for removal
158 * when processes fork.
160 if ((map->flags & _DRM_REMOVABLE) && map->type != _DRM_SHM) {
164 DRM_DEBUG("offset = 0x%08llx, size = 0x%08lx, type = %d\n",
165 (unsigned long long)map->offset, map->size, map->type);
167 /* page-align _DRM_SHM maps. They are allocated here so there is no security
168 * hole created by that and it works around various broken drivers that use
169 * a non-aligned quantity to map the SAREA. --BenH
171 if (map->type == _DRM_SHM)
172 map->size = PAGE_ALIGN(map->size);
174 if ((map->offset & (~(resource_size_t)PAGE_MASK)) || (map->size & (~PAGE_MASK))) {
183 case _DRM_FRAME_BUFFER:
184 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
185 if (map->offset + (map->size-1) < map->offset ||
186 map->offset < virt_to_phys(high_memory)) {
191 /* Some drivers preinitialize some maps, without the X Server
192 * needing to be aware of it. Therefore, we just return success
193 * when the server tries to create a duplicate map.
195 list = drm_find_matching_map(dev, map);
197 if (list->map->size != map->size) {
198 DRM_DEBUG("Matching maps of type %d with "
199 "mismatched sizes, (%ld vs %ld)\n",
200 map->type, map->size,
202 list->map->size = map->size;
210 if (map->type == _DRM_FRAME_BUFFER ||
211 (map->flags & _DRM_WRITE_COMBINING)) {
213 arch_phys_wc_add(map->offset, map->size);
215 if (map->type == _DRM_REGISTERS) {
216 if (map->flags & _DRM_WRITE_COMBINING)
217 map->handle = ioremap_wc(map->offset,
220 map->handle = ioremap(map->offset, map->size);
229 list = drm_find_matching_map(dev, map);
231 if(list->map->size != map->size) {
232 DRM_DEBUG("Matching maps of type %d with "
233 "mismatched sizes, (%ld vs %ld)\n",
234 map->type, map->size, list->map->size);
235 list->map->size = map->size;
242 map->handle = vmalloc_user(map->size);
243 DRM_DEBUG("%lu %d %p\n",
244 map->size, order_base_2(map->size), map->handle);
249 map->offset = (unsigned long)map->handle;
250 if (map->flags & _DRM_CONTAINS_LOCK) {
251 /* Prevent a 2nd X Server from creating a 2nd lock */
252 if (dev->primary->master->lock.hw_lock != NULL) {
257 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle; /* Pointer to lock */
261 struct drm_agp_mem *entry;
264 if (!drm_core_has_AGP(dev)) {
269 map->offset += dev->hose->mem_space->start;
271 /* In some cases (i810 driver), user space may have already
272 * added the AGP base itself, because dev->agp->base previously
273 * only got set during AGP enable. So, only add the base
274 * address if the map's offset isn't already within the
277 if (map->offset < dev->agp->base ||
278 map->offset > dev->agp->base +
279 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
280 map->offset += dev->agp->base;
282 map->mtrr = dev->agp->agp_mtrr; /* for getmap */
284 /* This assumes the DRM is in total control of AGP space.
285 * It's not always the case as AGP can be in the control
286 * of user space (i.e. i810 driver). So this loop will get
287 * skipped and we double check that dev->agp->memory is
288 * actually set as well as being invalid before EPERM'ing
290 list_for_each_entry(entry, &dev->agp->memory, head) {
291 if ((map->offset >= entry->bound) &&
292 (map->offset + map->size <= entry->bound + entry->pages * PAGE_SIZE)) {
297 if (!list_empty(&dev->agp->memory) && !valid) {
301 DRM_DEBUG("AGP offset = 0x%08llx, size = 0x%08lx\n",
302 (unsigned long long)map->offset, map->size);
307 DRM_ERROR("tried to addmap GEM object\n");
309 case _DRM_SCATTER_GATHER:
314 map->offset += (unsigned long)dev->sg->virtual;
316 case _DRM_CONSISTENT:
317 /* dma_addr_t is 64bit on i386 with CONFIG_HIGHMEM64G,
318 * As we're limiting the address to 2^32-1 (or less),
319 * casting it down to 32 bits is no problem, but we
320 * need to point to a 64bit variable first. */
321 dmah = drm_pci_alloc(dev, map->size, map->size);
326 map->handle = dmah->vaddr;
327 map->offset = (unsigned long)dmah->busaddr;
335 list = kzalloc(sizeof(*list), GFP_KERNEL);
337 if (map->type == _DRM_REGISTERS)
338 iounmap(map->handle);
344 mutex_lock(&dev->struct_mutex);
345 list_add(&list->head, &dev->maplist);
347 /* Assign a 32-bit handle */
348 /* We do it here so that dev->struct_mutex protects the increment */
349 user_token = (map->type == _DRM_SHM) ? (unsigned long)map->handle :
351 ret = drm_map_handle(dev, &list->hash, user_token, 0,
352 (map->type == _DRM_SHM));
354 if (map->type == _DRM_REGISTERS)
355 iounmap(map->handle);
358 mutex_unlock(&dev->struct_mutex);
362 list->user_token = list->hash.key << PAGE_SHIFT;
363 mutex_unlock(&dev->struct_mutex);
365 if (!(map->flags & _DRM_DRIVER))
366 list->master = dev->primary->master;
371 int drm_addmap(struct drm_device * dev, resource_size_t offset,
372 unsigned int size, enum drm_map_type type,
373 enum drm_map_flags flags, struct drm_local_map ** map_ptr)
375 struct drm_map_list *list;
378 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
380 *map_ptr = list->map;
384 EXPORT_SYMBOL(drm_addmap);
387 * Ioctl to specify a range of memory that is available for mapping by a
390 * \param inode device inode.
391 * \param file_priv DRM file private.
392 * \param cmd command.
393 * \param arg pointer to a drm_map structure.
394 * \return zero on success or a negative value on error.
397 int drm_addmap_ioctl(struct drm_device *dev, void *data,
398 struct drm_file *file_priv)
400 struct drm_map *map = data;
401 struct drm_map_list *maplist;
404 if (!(capable(CAP_SYS_ADMIN) || map->type == _DRM_AGP || map->type == _DRM_SHM))
407 err = drm_addmap_core(dev, map->offset, map->size, map->type,
408 map->flags, &maplist);
413 /* avoid a warning on 64-bit, this casting isn't very nice, but the API is set so too late */
414 map->handle = (void *)(unsigned long)maplist->user_token;
417 * It appears that there are no users of this value whatsoever --
418 * drmAddMap just discards it. Let's not encourage its use.
419 * (Keeping drm_addmap_core's returned mtrr value would be wrong --
420 * it's not a real mtrr index anymore.)
428 * Remove a map private from list and deallocate resources if the mapping
431 * Searches the map on drm_device::maplist, removes it from the list, see if
432 * its being used, and free any associate resource (such as MTRR's) if it's not
437 int drm_rmmap_locked(struct drm_device *dev, struct drm_local_map *map)
439 struct drm_map_list *r_list = NULL, *list_t;
440 drm_dma_handle_t dmah;
442 struct drm_master *master;
444 /* Find the list entry for the map and remove it */
445 list_for_each_entry_safe(r_list, list_t, &dev->maplist, head) {
446 if (r_list->map == map) {
447 master = r_list->master;
448 list_del(&r_list->head);
449 drm_ht_remove_key(&dev->map_hash,
450 r_list->user_token >> PAGE_SHIFT);
462 iounmap(map->handle);
464 case _DRM_FRAME_BUFFER:
465 arch_phys_wc_del(map->mtrr);
470 if (dev->sigdata.lock == master->lock.hw_lock)
471 dev->sigdata.lock = NULL;
472 master->lock.hw_lock = NULL; /* SHM removed */
473 master->lock.file_priv = NULL;
474 wake_up_interruptible_all(&master->lock.lock_queue);
478 case _DRM_SCATTER_GATHER:
480 case _DRM_CONSISTENT:
481 dmah.vaddr = map->handle;
482 dmah.busaddr = map->offset;
483 dmah.size = map->size;
484 __drm_pci_free(dev, &dmah);
487 DRM_ERROR("tried to rmmap GEM object\n");
494 EXPORT_SYMBOL(drm_rmmap_locked);
496 int drm_rmmap(struct drm_device *dev, struct drm_local_map *map)
500 mutex_lock(&dev->struct_mutex);
501 ret = drm_rmmap_locked(dev, map);
502 mutex_unlock(&dev->struct_mutex);
506 EXPORT_SYMBOL(drm_rmmap);
508 /* The rmmap ioctl appears to be unnecessary. All mappings are torn down on
509 * the last close of the device, and this is necessary for cleanup when things
510 * exit uncleanly. Therefore, having userland manually remove mappings seems
511 * like a pointless exercise since they're going away anyway.
513 * One use case might be after addmap is allowed for normal users for SHM and
514 * gets used by drivers that the server doesn't need to care about. This seems
517 * \param inode device inode.
518 * \param file_priv DRM file private.
519 * \param cmd command.
520 * \param arg pointer to a struct drm_map structure.
521 * \return zero on success or a negative value on error.
523 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
524 struct drm_file *file_priv)
526 struct drm_map *request = data;
527 struct drm_local_map *map = NULL;
528 struct drm_map_list *r_list;
531 mutex_lock(&dev->struct_mutex);
532 list_for_each_entry(r_list, &dev->maplist, head) {
534 r_list->user_token == (unsigned long)request->handle &&
535 r_list->map->flags & _DRM_REMOVABLE) {
541 /* List has wrapped around to the head pointer, or its empty we didn't
544 if (list_empty(&dev->maplist) || !map) {
545 mutex_unlock(&dev->struct_mutex);
549 /* Register and framebuffer maps are permanent */
550 if ((map->type == _DRM_REGISTERS) || (map->type == _DRM_FRAME_BUFFER)) {
551 mutex_unlock(&dev->struct_mutex);
555 ret = drm_rmmap_locked(dev, map);
557 mutex_unlock(&dev->struct_mutex);
563 * Cleanup after an error on one of the addbufs() functions.
565 * \param dev DRM device.
566 * \param entry buffer entry where the error occurred.
568 * Frees any pages and buffers associated with the given entry.
570 static void drm_cleanup_buf_error(struct drm_device * dev,
571 struct drm_buf_entry * entry)
575 if (entry->seg_count) {
576 for (i = 0; i < entry->seg_count; i++) {
577 if (entry->seglist[i]) {
578 drm_pci_free(dev, entry->seglist[i]);
581 kfree(entry->seglist);
583 entry->seg_count = 0;
586 if (entry->buf_count) {
587 for (i = 0; i < entry->buf_count; i++) {
588 kfree(entry->buflist[i].dev_private);
590 kfree(entry->buflist);
592 entry->buf_count = 0;
598 * Add AGP buffers for DMA transfers.
600 * \param dev struct drm_device to which the buffers are to be added.
601 * \param request pointer to a struct drm_buf_desc describing the request.
602 * \return zero on success or a negative number on failure.
604 * After some sanity checks creates a drm_buf structure for each buffer and
605 * reallocates the buffer list of the same size order to accommodate the new
608 int drm_addbufs_agp(struct drm_device * dev, struct drm_buf_desc * request)
610 struct drm_device_dma *dma = dev->dma;
611 struct drm_buf_entry *entry;
612 struct drm_agp_mem *agp_entry;
614 unsigned long offset;
615 unsigned long agp_offset;
624 struct drm_buf **temp_buflist;
629 count = request->count;
630 order = order_base_2(request->size);
633 alignment = (request->flags & _DRM_PAGE_ALIGN)
634 ? PAGE_ALIGN(size) : size;
635 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
636 total = PAGE_SIZE << page_order;
639 agp_offset = dev->agp->base + request->agp_start;
641 DRM_DEBUG("count: %d\n", count);
642 DRM_DEBUG("order: %d\n", order);
643 DRM_DEBUG("size: %d\n", size);
644 DRM_DEBUG("agp_offset: %lx\n", agp_offset);
645 DRM_DEBUG("alignment: %d\n", alignment);
646 DRM_DEBUG("page_order: %d\n", page_order);
647 DRM_DEBUG("total: %d\n", total);
649 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
652 /* Make sure buffers are located in AGP memory that we own */
654 list_for_each_entry(agp_entry, &dev->agp->memory, head) {
655 if ((agp_offset >= agp_entry->bound) &&
656 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
661 if (!list_empty(&dev->agp->memory) && !valid) {
662 DRM_DEBUG("zone invalid\n");
665 spin_lock(&dev->count_lock);
667 spin_unlock(&dev->count_lock);
670 atomic_inc(&dev->buf_alloc);
671 spin_unlock(&dev->count_lock);
673 mutex_lock(&dev->struct_mutex);
674 entry = &dma->bufs[order];
675 if (entry->buf_count) {
676 mutex_unlock(&dev->struct_mutex);
677 atomic_dec(&dev->buf_alloc);
678 return -ENOMEM; /* May only call once for each order */
681 if (count < 0 || count > 4096) {
682 mutex_unlock(&dev->struct_mutex);
683 atomic_dec(&dev->buf_alloc);
687 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
688 if (!entry->buflist) {
689 mutex_unlock(&dev->struct_mutex);
690 atomic_dec(&dev->buf_alloc);
694 entry->buf_size = size;
695 entry->page_order = page_order;
699 while (entry->buf_count < count) {
700 buf = &entry->buflist[entry->buf_count];
701 buf->idx = dma->buf_count + entry->buf_count;
702 buf->total = alignment;
706 buf->offset = (dma->byte_count + offset);
707 buf->bus_address = agp_offset + offset;
708 buf->address = (void *)(agp_offset + offset);
712 buf->file_priv = NULL;
714 buf->dev_priv_size = dev->driver->dev_priv_size;
715 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
716 if (!buf->dev_private) {
717 /* Set count correctly so we free the proper amount. */
718 entry->buf_count = count;
719 drm_cleanup_buf_error(dev, entry);
720 mutex_unlock(&dev->struct_mutex);
721 atomic_dec(&dev->buf_alloc);
725 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
729 byte_count += PAGE_SIZE << page_order;
732 DRM_DEBUG("byte_count: %d\n", byte_count);
734 temp_buflist = krealloc(dma->buflist,
735 (dma->buf_count + entry->buf_count) *
736 sizeof(*dma->buflist), GFP_KERNEL);
738 /* Free the entry because it isn't valid */
739 drm_cleanup_buf_error(dev, entry);
740 mutex_unlock(&dev->struct_mutex);
741 atomic_dec(&dev->buf_alloc);
744 dma->buflist = temp_buflist;
746 for (i = 0; i < entry->buf_count; i++) {
747 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
750 dma->buf_count += entry->buf_count;
751 dma->seg_count += entry->seg_count;
752 dma->page_count += byte_count >> PAGE_SHIFT;
753 dma->byte_count += byte_count;
755 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
756 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
758 mutex_unlock(&dev->struct_mutex);
760 request->count = entry->buf_count;
761 request->size = size;
763 dma->flags = _DRM_DMA_USE_AGP;
765 atomic_dec(&dev->buf_alloc);
768 EXPORT_SYMBOL(drm_addbufs_agp);
769 #endif /* __OS_HAS_AGP */
771 int drm_addbufs_pci(struct drm_device * dev, struct drm_buf_desc * request)
773 struct drm_device_dma *dma = dev->dma;
779 struct drm_buf_entry *entry;
780 drm_dma_handle_t *dmah;
783 unsigned long offset;
787 unsigned long *temp_pagelist;
788 struct drm_buf **temp_buflist;
790 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
796 if (!capable(CAP_SYS_ADMIN))
799 count = request->count;
800 order = order_base_2(request->size);
803 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
804 request->count, request->size, size, order);
806 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
809 alignment = (request->flags & _DRM_PAGE_ALIGN)
810 ? PAGE_ALIGN(size) : size;
811 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
812 total = PAGE_SIZE << page_order;
814 spin_lock(&dev->count_lock);
816 spin_unlock(&dev->count_lock);
819 atomic_inc(&dev->buf_alloc);
820 spin_unlock(&dev->count_lock);
822 mutex_lock(&dev->struct_mutex);
823 entry = &dma->bufs[order];
824 if (entry->buf_count) {
825 mutex_unlock(&dev->struct_mutex);
826 atomic_dec(&dev->buf_alloc);
827 return -ENOMEM; /* May only call once for each order */
830 if (count < 0 || count > 4096) {
831 mutex_unlock(&dev->struct_mutex);
832 atomic_dec(&dev->buf_alloc);
836 entry->buflist = kzalloc(count * sizeof(*entry->buflist), GFP_KERNEL);
837 if (!entry->buflist) {
838 mutex_unlock(&dev->struct_mutex);
839 atomic_dec(&dev->buf_alloc);
843 entry->seglist = kzalloc(count * sizeof(*entry->seglist), GFP_KERNEL);
844 if (!entry->seglist) {
845 kfree(entry->buflist);
846 mutex_unlock(&dev->struct_mutex);
847 atomic_dec(&dev->buf_alloc);
851 /* Keep the original pagelist until we know all the allocations
854 temp_pagelist = kmalloc((dma->page_count + (count << page_order)) *
855 sizeof(*dma->pagelist), GFP_KERNEL);
856 if (!temp_pagelist) {
857 kfree(entry->buflist);
858 kfree(entry->seglist);
859 mutex_unlock(&dev->struct_mutex);
860 atomic_dec(&dev->buf_alloc);
863 memcpy(temp_pagelist,
864 dma->pagelist, dma->page_count * sizeof(*dma->pagelist));
865 DRM_DEBUG("pagelist: %d entries\n",
866 dma->page_count + (count << page_order));
868 entry->buf_size = size;
869 entry->page_order = page_order;
873 while (entry->buf_count < count) {
875 dmah = drm_pci_alloc(dev, PAGE_SIZE << page_order, 0x1000);
878 /* Set count correctly so we free the proper amount. */
879 entry->buf_count = count;
880 entry->seg_count = count;
881 drm_cleanup_buf_error(dev, entry);
882 kfree(temp_pagelist);
883 mutex_unlock(&dev->struct_mutex);
884 atomic_dec(&dev->buf_alloc);
887 entry->seglist[entry->seg_count++] = dmah;
888 for (i = 0; i < (1 << page_order); i++) {
889 DRM_DEBUG("page %d @ 0x%08lx\n",
890 dma->page_count + page_count,
891 (unsigned long)dmah->vaddr + PAGE_SIZE * i);
892 temp_pagelist[dma->page_count + page_count++]
893 = (unsigned long)dmah->vaddr + PAGE_SIZE * i;
896 offset + size <= total && entry->buf_count < count;
897 offset += alignment, ++entry->buf_count) {
898 buf = &entry->buflist[entry->buf_count];
899 buf->idx = dma->buf_count + entry->buf_count;
900 buf->total = alignment;
903 buf->offset = (dma->byte_count + byte_count + offset);
904 buf->address = (void *)(dmah->vaddr + offset);
905 buf->bus_address = dmah->busaddr + offset;
909 buf->file_priv = NULL;
911 buf->dev_priv_size = dev->driver->dev_priv_size;
912 buf->dev_private = kzalloc(buf->dev_priv_size,
914 if (!buf->dev_private) {
915 /* Set count correctly so we free the proper amount. */
916 entry->buf_count = count;
917 entry->seg_count = count;
918 drm_cleanup_buf_error(dev, entry);
919 kfree(temp_pagelist);
920 mutex_unlock(&dev->struct_mutex);
921 atomic_dec(&dev->buf_alloc);
925 DRM_DEBUG("buffer %d @ %p\n",
926 entry->buf_count, buf->address);
928 byte_count += PAGE_SIZE << page_order;
931 temp_buflist = krealloc(dma->buflist,
932 (dma->buf_count + entry->buf_count) *
933 sizeof(*dma->buflist), GFP_KERNEL);
935 /* Free the entry because it isn't valid */
936 drm_cleanup_buf_error(dev, entry);
937 kfree(temp_pagelist);
938 mutex_unlock(&dev->struct_mutex);
939 atomic_dec(&dev->buf_alloc);
942 dma->buflist = temp_buflist;
944 for (i = 0; i < entry->buf_count; i++) {
945 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
948 /* No allocations failed, so now we can replace the original pagelist
951 if (dma->page_count) {
952 kfree(dma->pagelist);
954 dma->pagelist = temp_pagelist;
956 dma->buf_count += entry->buf_count;
957 dma->seg_count += entry->seg_count;
958 dma->page_count += entry->seg_count << page_order;
959 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
961 mutex_unlock(&dev->struct_mutex);
963 request->count = entry->buf_count;
964 request->size = size;
966 if (request->flags & _DRM_PCI_BUFFER_RO)
967 dma->flags = _DRM_DMA_USE_PCI_RO;
969 atomic_dec(&dev->buf_alloc);
973 EXPORT_SYMBOL(drm_addbufs_pci);
975 static int drm_addbufs_sg(struct drm_device * dev, struct drm_buf_desc * request)
977 struct drm_device_dma *dma = dev->dma;
978 struct drm_buf_entry *entry;
980 unsigned long offset;
981 unsigned long agp_offset;
990 struct drm_buf **temp_buflist;
992 if (!drm_core_check_feature(dev, DRIVER_SG))
998 if (!capable(CAP_SYS_ADMIN))
1001 count = request->count;
1002 order = order_base_2(request->size);
1005 alignment = (request->flags & _DRM_PAGE_ALIGN)
1006 ? PAGE_ALIGN(size) : size;
1007 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1008 total = PAGE_SIZE << page_order;
1011 agp_offset = request->agp_start;
1013 DRM_DEBUG("count: %d\n", count);
1014 DRM_DEBUG("order: %d\n", order);
1015 DRM_DEBUG("size: %d\n", size);
1016 DRM_DEBUG("agp_offset: %lu\n", agp_offset);
1017 DRM_DEBUG("alignment: %d\n", alignment);
1018 DRM_DEBUG("page_order: %d\n", page_order);
1019 DRM_DEBUG("total: %d\n", total);
1021 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1024 spin_lock(&dev->count_lock);
1026 spin_unlock(&dev->count_lock);
1029 atomic_inc(&dev->buf_alloc);
1030 spin_unlock(&dev->count_lock);
1032 mutex_lock(&dev->struct_mutex);
1033 entry = &dma->bufs[order];
1034 if (entry->buf_count) {
1035 mutex_unlock(&dev->struct_mutex);
1036 atomic_dec(&dev->buf_alloc);
1037 return -ENOMEM; /* May only call once for each order */
1040 if (count < 0 || count > 4096) {
1041 mutex_unlock(&dev->struct_mutex);
1042 atomic_dec(&dev->buf_alloc);
1046 entry->buflist = kzalloc(count * sizeof(*entry->buflist),
1048 if (!entry->buflist) {
1049 mutex_unlock(&dev->struct_mutex);
1050 atomic_dec(&dev->buf_alloc);
1054 entry->buf_size = size;
1055 entry->page_order = page_order;
1059 while (entry->buf_count < count) {
1060 buf = &entry->buflist[entry->buf_count];
1061 buf->idx = dma->buf_count + entry->buf_count;
1062 buf->total = alignment;
1066 buf->offset = (dma->byte_count + offset);
1067 buf->bus_address = agp_offset + offset;
1068 buf->address = (void *)(agp_offset + offset
1069 + (unsigned long)dev->sg->virtual);
1073 buf->file_priv = NULL;
1075 buf->dev_priv_size = dev->driver->dev_priv_size;
1076 buf->dev_private = kzalloc(buf->dev_priv_size, GFP_KERNEL);
1077 if (!buf->dev_private) {
1078 /* Set count correctly so we free the proper amount. */
1079 entry->buf_count = count;
1080 drm_cleanup_buf_error(dev, entry);
1081 mutex_unlock(&dev->struct_mutex);
1082 atomic_dec(&dev->buf_alloc);
1086 DRM_DEBUG("buffer %d @ %p\n", entry->buf_count, buf->address);
1088 offset += alignment;
1090 byte_count += PAGE_SIZE << page_order;
1093 DRM_DEBUG("byte_count: %d\n", byte_count);
1095 temp_buflist = krealloc(dma->buflist,
1096 (dma->buf_count + entry->buf_count) *
1097 sizeof(*dma->buflist), GFP_KERNEL);
1098 if (!temp_buflist) {
1099 /* Free the entry because it isn't valid */
1100 drm_cleanup_buf_error(dev, entry);
1101 mutex_unlock(&dev->struct_mutex);
1102 atomic_dec(&dev->buf_alloc);
1105 dma->buflist = temp_buflist;
1107 for (i = 0; i < entry->buf_count; i++) {
1108 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
1111 dma->buf_count += entry->buf_count;
1112 dma->seg_count += entry->seg_count;
1113 dma->page_count += byte_count >> PAGE_SHIFT;
1114 dma->byte_count += byte_count;
1116 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
1117 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
1119 mutex_unlock(&dev->struct_mutex);
1121 request->count = entry->buf_count;
1122 request->size = size;
1124 dma->flags = _DRM_DMA_USE_SG;
1126 atomic_dec(&dev->buf_alloc);
1131 * Add buffers for DMA transfers (ioctl).
1133 * \param inode device inode.
1134 * \param file_priv DRM file private.
1135 * \param cmd command.
1136 * \param arg pointer to a struct drm_buf_desc request.
1137 * \return zero on success or a negative number on failure.
1139 * According with the memory type specified in drm_buf_desc::flags and the
1140 * build options, it dispatches the call either to addbufs_agp(),
1141 * addbufs_sg() or addbufs_pci() for AGP, scatter-gather or consistent
1142 * PCI memory respectively.
1144 int drm_addbufs(struct drm_device *dev, void *data,
1145 struct drm_file *file_priv)
1147 struct drm_buf_desc *request = data;
1150 if (drm_core_check_feature(dev, DRIVER_MODESET))
1153 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1157 if (request->flags & _DRM_AGP_BUFFER)
1158 ret = drm_addbufs_agp(dev, request);
1161 if (request->flags & _DRM_SG_BUFFER)
1162 ret = drm_addbufs_sg(dev, request);
1163 else if (request->flags & _DRM_FB_BUFFER)
1166 ret = drm_addbufs_pci(dev, request);
1172 * Get information about the buffer mappings.
1174 * This was originally mean for debugging purposes, or by a sophisticated
1175 * client library to determine how best to use the available buffers (e.g.,
1176 * large buffers can be used for image transfer).
1178 * \param inode device inode.
1179 * \param file_priv DRM file private.
1180 * \param cmd command.
1181 * \param arg pointer to a drm_buf_info structure.
1182 * \return zero on success or a negative number on failure.
1184 * Increments drm_device::buf_use while holding the drm_device::count_lock
1185 * lock, preventing of allocating more buffers after this call. Information
1186 * about each requested buffer is then copied into user space.
1188 int drm_infobufs(struct drm_device *dev, void *data,
1189 struct drm_file *file_priv)
1191 struct drm_device_dma *dma = dev->dma;
1192 struct drm_buf_info *request = data;
1196 if (drm_core_check_feature(dev, DRIVER_MODESET))
1199 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1205 spin_lock(&dev->count_lock);
1206 if (atomic_read(&dev->buf_alloc)) {
1207 spin_unlock(&dev->count_lock);
1210 ++dev->buf_use; /* Can't allocate more after this call */
1211 spin_unlock(&dev->count_lock);
1213 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1214 if (dma->bufs[i].buf_count)
1218 DRM_DEBUG("count = %d\n", count);
1220 if (request->count >= count) {
1221 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1222 if (dma->bufs[i].buf_count) {
1223 struct drm_buf_desc __user *to =
1224 &request->list[count];
1225 struct drm_buf_entry *from = &dma->bufs[i];
1226 struct drm_freelist *list = &dma->bufs[i].freelist;
1227 if (copy_to_user(&to->count,
1229 sizeof(from->buf_count)) ||
1230 copy_to_user(&to->size,
1232 sizeof(from->buf_size)) ||
1233 copy_to_user(&to->low_mark,
1235 sizeof(list->low_mark)) ||
1236 copy_to_user(&to->high_mark,
1238 sizeof(list->high_mark)))
1241 DRM_DEBUG("%d %d %d %d %d\n",
1243 dma->bufs[i].buf_count,
1244 dma->bufs[i].buf_size,
1245 dma->bufs[i].freelist.low_mark,
1246 dma->bufs[i].freelist.high_mark);
1251 request->count = count;
1257 * Specifies a low and high water mark for buffer allocation
1259 * \param inode device inode.
1260 * \param file_priv DRM file private.
1261 * \param cmd command.
1262 * \param arg a pointer to a drm_buf_desc structure.
1263 * \return zero on success or a negative number on failure.
1265 * Verifies that the size order is bounded between the admissible orders and
1266 * updates the respective drm_device_dma::bufs entry low and high water mark.
1268 * \note This ioctl is deprecated and mostly never used.
1270 int drm_markbufs(struct drm_device *dev, void *data,
1271 struct drm_file *file_priv)
1273 struct drm_device_dma *dma = dev->dma;
1274 struct drm_buf_desc *request = data;
1276 struct drm_buf_entry *entry;
1278 if (drm_core_check_feature(dev, DRIVER_MODESET))
1281 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1287 DRM_DEBUG("%d, %d, %d\n",
1288 request->size, request->low_mark, request->high_mark);
1289 order = order_base_2(request->size);
1290 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1292 entry = &dma->bufs[order];
1294 if (request->low_mark < 0 || request->low_mark > entry->buf_count)
1296 if (request->high_mark < 0 || request->high_mark > entry->buf_count)
1299 entry->freelist.low_mark = request->low_mark;
1300 entry->freelist.high_mark = request->high_mark;
1306 * Unreserve the buffers in list, previously reserved using drmDMA.
1308 * \param inode device inode.
1309 * \param file_priv DRM file private.
1310 * \param cmd command.
1311 * \param arg pointer to a drm_buf_free structure.
1312 * \return zero on success or a negative number on failure.
1314 * Calls free_buffer() for each used buffer.
1315 * This function is primarily used for debugging.
1317 int drm_freebufs(struct drm_device *dev, void *data,
1318 struct drm_file *file_priv)
1320 struct drm_device_dma *dma = dev->dma;
1321 struct drm_buf_free *request = data;
1324 struct drm_buf *buf;
1326 if (drm_core_check_feature(dev, DRIVER_MODESET))
1329 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1335 DRM_DEBUG("%d\n", request->count);
1336 for (i = 0; i < request->count; i++) {
1337 if (copy_from_user(&idx, &request->list[i], sizeof(idx)))
1339 if (idx < 0 || idx >= dma->buf_count) {
1340 DRM_ERROR("Index %d (of %d max)\n",
1341 idx, dma->buf_count - 1);
1344 buf = dma->buflist[idx];
1345 if (buf->file_priv != file_priv) {
1346 DRM_ERROR("Process %d freeing buffer not owned\n",
1347 task_pid_nr(current));
1350 drm_free_buffer(dev, buf);
1357 * Maps all of the DMA buffers into client-virtual space (ioctl).
1359 * \param inode device inode.
1360 * \param file_priv DRM file private.
1361 * \param cmd command.
1362 * \param arg pointer to a drm_buf_map structure.
1363 * \return zero on success or a negative number on failure.
1365 * Maps the AGP, SG or PCI buffer region with vm_mmap(), and copies information
1366 * about each buffer into user space. For PCI buffers, it calls vm_mmap() with
1367 * offset equal to 0, which drm_mmap() interpretes as PCI buffers and calls
1370 int drm_mapbufs(struct drm_device *dev, void *data,
1371 struct drm_file *file_priv)
1373 struct drm_device_dma *dma = dev->dma;
1376 unsigned long virtual;
1377 unsigned long address;
1378 struct drm_buf_map *request = data;
1381 if (drm_core_check_feature(dev, DRIVER_MODESET))
1384 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1390 spin_lock(&dev->count_lock);
1391 if (atomic_read(&dev->buf_alloc)) {
1392 spin_unlock(&dev->count_lock);
1395 dev->buf_use++; /* Can't allocate more after this call */
1396 spin_unlock(&dev->count_lock);
1398 if (request->count >= dma->buf_count) {
1399 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1400 || (drm_core_check_feature(dev, DRIVER_SG)
1401 && (dma->flags & _DRM_DMA_USE_SG))) {
1402 struct drm_local_map *map = dev->agp_buffer_map;
1403 unsigned long token = dev->agp_buffer_token;
1409 virtual = vm_mmap(file_priv->filp, 0, map->size,
1410 PROT_READ | PROT_WRITE,
1414 virtual = vm_mmap(file_priv->filp, 0, dma->byte_count,
1415 PROT_READ | PROT_WRITE,
1418 if (virtual > -1024UL) {
1420 retcode = (signed long)virtual;
1423 request->virtual = (void __user *)virtual;
1425 for (i = 0; i < dma->buf_count; i++) {
1426 if (copy_to_user(&request->list[i].idx,
1427 &dma->buflist[i]->idx,
1428 sizeof(request->list[0].idx))) {
1432 if (copy_to_user(&request->list[i].total,
1433 &dma->buflist[i]->total,
1434 sizeof(request->list[0].total))) {
1438 if (copy_to_user(&request->list[i].used,
1439 &zero, sizeof(zero))) {
1443 address = virtual + dma->buflist[i]->offset; /* *** */
1444 if (copy_to_user(&request->list[i].address,
1445 &address, sizeof(address))) {
1452 request->count = dma->buf_count;
1453 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1458 int drm_dma_ioctl(struct drm_device *dev, void *data,
1459 struct drm_file *file_priv)
1461 if (drm_core_check_feature(dev, DRIVER_MODESET))
1464 if (dev->driver->dma_ioctl)
1465 return dev->driver->dma_ioctl(dev, data, file_priv);
1470 struct drm_local_map *drm_getsarea(struct drm_device *dev)
1472 struct drm_map_list *entry;
1474 list_for_each_entry(entry, &dev->maplist, head) {
1475 if (entry->map && entry->map->type == _DRM_SHM &&
1476 (entry->map->flags & _DRM_CONTAINS_LOCK)) {
1482 EXPORT_SYMBOL(drm_getsarea);