Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / external / bsd / drm / dist / bsd-core / drm_bufs.c
blob2bf2346e4d100f5b87ae3053c1e5bed76ced2dd6
1 /*-
2 * Copyright 1999, 2000 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4 * All Rights Reserved.
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
25 * Authors:
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
31 /** @file drm_bufs.c
32 * Implementation of the ioctls for setup of DRM mappings and DMA buffers.
35 #if defined(__FreeBSD__)
36 #include "dev/pci/pcireg.h"
37 #endif
39 #include "drmP.h"
41 /* Allocation of PCI memory resources (framebuffer, registers, etc.) for
42 * drm_get_resource_*. Note that they are not RF_ACTIVE, so there's no virtual
43 * address for accessing them. Cleaned up at unload.
45 static int drm_alloc_resource(struct drm_device *dev, int resource)
47 #if defined(__FreeBSD__)
48 if (resource >= DRM_MAX_PCI_RESOURCE) {
49 DRM_ERROR("Resource %d too large\n", resource);
50 return 1;
53 DRM_UNLOCK();
54 if (dev->pcir[resource] != NULL) {
55 DRM_LOCK();
56 return 0;
59 dev->pcirid[resource] = PCIR_BAR(resource);
60 dev->pcir[resource] = bus_alloc_resource_any(dev->device,
61 SYS_RES_MEMORY, &dev->pcirid[resource], RF_SHAREABLE);
62 DRM_LOCK();
64 if (dev->pcir[resource] == NULL) {
65 DRM_ERROR("Couldn't find resource 0x%x\n", resource);
66 return 1;
68 #elif defined(__NetBSD__)
69 /* XXX This space _not_ intentionally left blank! */
70 #endif
72 return 0;
75 unsigned long drm_get_resource_start(struct drm_device *dev,
76 unsigned int resource)
78 if (drm_alloc_resource(dev, resource) != 0)
79 return 0;
81 #if defined(__FreeBSD__)
82 return rman_get_start(dev->pcir[resource]);
83 #elif defined(__NetBSD__)
84 return dev->pci_map_data[resource].base;
85 #endif
88 unsigned long drm_get_resource_len(struct drm_device *dev,
89 unsigned int resource)
91 if (drm_alloc_resource(dev, resource) != 0)
92 return 0;
94 #if defined(__FreeBSD__)
95 return rman_get_size(dev->pcir[resource]);
96 #elif defined(__NetBSD__)
97 return dev->pci_map_data[resource].size;
98 #endif
101 int drm_addmap(struct drm_device * dev, unsigned long offset,
102 unsigned long size,
103 enum drm_map_type type, enum drm_map_flags flags, drm_local_map_t **map_ptr)
105 drm_local_map_t *map;
106 int align;
107 /*drm_agp_mem_t *entry;
108 int valid;*/
110 /* Only allow shared memory to be removable since we only keep enough
111 * book keeping information about shared memory to allow for removal
112 * when processes fork.
114 if ((flags & _DRM_REMOVABLE) && type != _DRM_SHM) {
115 DRM_ERROR("Requested removable map for non-DRM_SHM\n");
116 return EINVAL;
118 if ((offset & PAGE_MASK) || (size & PAGE_MASK)) {
119 DRM_ERROR("offset/size not page aligned: 0x%lx/0x%lx\n",
120 offset, size);
121 return EINVAL;
123 if (offset + size < offset) {
124 DRM_ERROR("offset and size wrap around: 0x%lx/0x%lx\n",
125 offset, size);
126 return EINVAL;
129 DRM_DEBUG("offset = 0x%08lx, size = 0x%08lx, type = %d\n", offset,
130 size, type);
132 /* Check if this is just another version of a kernel-allocated map, and
133 * just hand that back if so.
135 if (type == _DRM_REGISTERS || type == _DRM_FRAME_BUFFER ||
136 type == _DRM_SHM) {
137 TAILQ_FOREACH(map, &dev->maplist, link) {
138 if (map->type == type && (map->offset == offset ||
139 (map->type == _DRM_SHM &&
140 map->flags == _DRM_CONTAINS_LOCK))) {
141 map->size = size;
142 DRM_DEBUG("Found kernel map %d\n", type);
143 goto done;
147 DRM_UNLOCK();
149 /* Allocate a new map structure, fill it in, and do any type-specific
150 * initialization necessary.
152 map = malloc(sizeof(*map), DRM_MEM_MAPS, M_ZERO | M_NOWAIT);
153 if (!map) {
154 DRM_LOCK();
155 return ENOMEM;
158 map->offset = offset;
159 map->size = size;
160 map->type = type;
161 map->flags = flags;
162 #ifdef __NetBSD__
163 map->cnt = NULL;
164 map->mapsize = 0;
165 #endif
167 switch (map->type) {
168 case _DRM_REGISTERS:
169 map->handle = drm_ioremap(dev, map);
170 if (!(map->flags & _DRM_WRITE_COMBINING))
171 break;
172 /* FALLTHROUGH */
173 case _DRM_FRAME_BUFFER:
174 if (drm_mtrr_add(map->offset, map->size, DRM_MTRR_WC) == 0)
175 map->mtrr = 1;
176 break;
177 case _DRM_SHM:
178 map->handle = malloc(map->size, DRM_MEM_MAPS, M_NOWAIT);
179 DRM_DEBUG("%lu %d %p\n",
180 map->size, drm_order(map->size), map->handle);
181 if (!map->handle) {
182 free(map, DRM_MEM_MAPS);
183 DRM_LOCK();
184 return ENOMEM;
186 map->offset = (unsigned long)map->handle;
187 if (map->flags & _DRM_CONTAINS_LOCK) {
188 /* Prevent a 2nd X Server from creating a 2nd lock */
189 DRM_LOCK();
190 if (dev->lock.hw_lock != NULL) {
191 DRM_UNLOCK();
192 free(map->handle, DRM_MEM_MAPS);
193 free(map, DRM_MEM_MAPS);
194 return EBUSY;
196 dev->lock.hw_lock = map->handle; /* Pointer to lock */
197 DRM_UNLOCK();
199 break;
200 case _DRM_AGP:
201 /*valid = 0;*/
202 /* In some cases (i810 driver), user space may have already
203 * added the AGP base itself, because dev->agp->base previously
204 * only got set during AGP enable. So, only add the base
205 * address if the map's offset isn't already within the
206 * aperture.
208 if (map->offset < dev->agp->base ||
209 map->offset > dev->agp->base +
210 dev->agp->info.ai_aperture_size - 1) {
211 map->offset += dev->agp->base;
213 map->mtrr = dev->agp->mtrr; /* for getmap */
214 /*for (entry = dev->agp->memory; entry; entry = entry->next) {
215 if ((map->offset >= entry->bound) &&
216 (map->offset + map->size <=
217 entry->bound + entry->pages * PAGE_SIZE)) {
218 valid = 1;
219 break;
222 if (!valid) {
223 free(map, DRM_MEM_MAPS);
224 DRM_LOCK();
225 return EACCES;
227 break;
228 case _DRM_SCATTER_GATHER:
229 if (!dev->sg) {
230 free(map, DRM_MEM_MAPS);
231 DRM_LOCK();
232 return EINVAL;
234 map->offset += dev->sg->handle;
235 break;
236 case _DRM_CONSISTENT:
237 /* Unfortunately, we don't get any alignment specification from
238 * the caller, so we have to guess. drm_pci_alloc requires
239 * a power-of-two alignment, so try to align the bus address of
240 * the map to it size if possible, otherwise just assume
241 * PAGE_SIZE alignment.
243 align = map->size;
244 if ((align & (align - 1)) != 0)
245 align = PAGE_SIZE;
246 map->dmah = drm_pci_alloc(dev, map->size, align, 0xfffffffful);
247 if (map->dmah == NULL) {
248 free(map, DRM_MEM_MAPS);
249 DRM_LOCK();
250 return ENOMEM;
252 map->handle = map->dmah->vaddr;
253 map->offset = map->dmah->busaddr;
254 break;
255 default:
256 DRM_ERROR("Bad map type %d\n", map->type);
257 free(map, DRM_MEM_MAPS);
258 DRM_LOCK();
259 return EINVAL;
262 DRM_LOCK();
263 TAILQ_INSERT_TAIL(&dev->maplist, map, link);
265 done:
266 /* Jumped to, with lock held, when a kernel map is found. */
268 DRM_DEBUG("Added map %d 0x%lx/0x%lx\n", map->type, map->offset,
269 map->size);
271 *map_ptr = map;
273 return 0;
276 int drm_addmap_ioctl(struct drm_device *dev, void *data,
277 struct drm_file *file_priv)
279 struct drm_map *request = data;
280 drm_local_map_t *map;
281 int err;
283 if (!(dev->flags & (FREAD|FWRITE)))
284 return EACCES; /* Require read/write */
286 if (!DRM_SUSER(DRM_CURPROC) && request->type != _DRM_AGP)
287 return EACCES;
289 DRM_LOCK();
290 err = drm_addmap(dev, request->offset, request->size, request->type,
291 request->flags, &map);
292 DRM_UNLOCK();
293 if (err != 0)
294 return err;
296 request->offset = map->offset;
297 request->size = map->size;
298 request->type = map->type;
299 request->flags = map->flags;
300 request->mtrr = map->mtrr;
301 request->handle = map->handle;
303 if (request->type != _DRM_SHM) {
304 request->handle = (void *)request->offset;
307 return 0;
310 void drm_rmmap(struct drm_device *dev, drm_local_map_t *map)
312 DRM_SPINLOCK_ASSERT(&dev->dev_lock);
314 TAILQ_REMOVE(&dev->maplist, map, link);
316 switch (map->type) {
317 case _DRM_REGISTERS:
318 #if defined(__FreeBSD__)
319 if (map->bsr == NULL)
320 #endif
321 drm_ioremapfree(map);
322 /* FALLTHROUGH */
323 case _DRM_FRAME_BUFFER:
324 if (map->mtrr) {
325 int __unused retcode;
326 retcode = drm_mtrr_del(0, map->offset, map->size,
327 DRM_MTRR_WC);
328 DRM_DEBUG("mtrr_del = %d\n", retcode);
330 break;
331 case _DRM_SHM:
332 free(map->handle, DRM_MEM_MAPS);
333 break;
334 case _DRM_AGP:
335 case _DRM_SCATTER_GATHER:
336 break;
337 case _DRM_CONSISTENT:
338 drm_pci_free(dev, map->dmah);
339 break;
340 default:
341 DRM_ERROR("Bad map type %d\n", map->type);
342 break;
345 #if defined(__FreeBSD__)
346 if (map->bsr != NULL) {
347 bus_release_resource(dev->device, SYS_RES_MEMORY, map->rid,
348 map->bsr);
350 #endif
352 free(map, DRM_MEM_MAPS);
355 /* Remove a map private from list and deallocate resources if the mapping
356 * isn't in use.
359 int drm_rmmap_ioctl(struct drm_device *dev, void *data,
360 struct drm_file *file_priv)
362 drm_local_map_t *map;
363 struct drm_map *request = data;
365 DRM_LOCK();
366 TAILQ_FOREACH(map, &dev->maplist, link) {
367 if (map->handle == request->handle &&
368 map->flags & _DRM_REMOVABLE)
369 break;
372 /* No match found. */
373 if (map == NULL) {
374 DRM_UNLOCK();
375 return EINVAL;
378 drm_rmmap(dev, map);
380 DRM_UNLOCK();
382 return 0;
386 static void drm_cleanup_buf_error(struct drm_device *dev,
387 drm_buf_entry_t *entry)
389 int i;
391 if (entry->seg_count) {
392 for (i = 0; i < entry->seg_count; i++) {
393 drm_pci_free(dev, entry->seglist[i]);
395 free(entry->seglist, DRM_MEM_SEGS);
397 entry->seg_count = 0;
400 if (entry->buf_count) {
401 for (i = 0; i < entry->buf_count; i++) {
402 free(entry->buflist[i].dev_private, DRM_MEM_BUFS);
404 free(entry->buflist, DRM_MEM_BUFS);
406 entry->buf_count = 0;
410 static int drm_do_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
412 drm_device_dma_t *dma = dev->dma;
413 drm_buf_entry_t *entry;
414 /*drm_agp_mem_t *agp_entry;
415 int valid*/
416 drm_buf_t *buf;
417 unsigned long offset;
418 unsigned long agp_offset;
419 int count;
420 int order;
421 int size;
422 int alignment;
423 int page_order;
424 int total;
425 int byte_count;
426 int i;
427 drm_buf_t **temp_buflist;
429 count = request->count;
430 order = drm_order(request->size);
431 size = 1 << order;
433 alignment = (request->flags & _DRM_PAGE_ALIGN)
434 ? round_page(size) : size;
435 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
436 total = PAGE_SIZE << page_order;
438 byte_count = 0;
439 agp_offset = dev->agp->base + request->agp_start;
441 DRM_DEBUG("count: %d\n", count);
442 DRM_DEBUG("order: %d\n", order);
443 DRM_DEBUG("size: %d\n", size);
444 DRM_DEBUG("agp_offset: 0x%lx\n", agp_offset);
445 DRM_DEBUG("alignment: %d\n", alignment);
446 DRM_DEBUG("page_order: %d\n", page_order);
447 DRM_DEBUG("total: %d\n", total);
449 /* Make sure buffers are located in AGP memory that we own */
450 /* Breaks MGA due to drm_alloc_agp not setting up entries for the
451 * memory. Safe to ignore for now because these ioctls are still
452 * root-only.
454 /*valid = 0;
455 for (agp_entry = dev->agp->memory; agp_entry;
456 agp_entry = agp_entry->next) {
457 if ((agp_offset >= agp_entry->bound) &&
458 (agp_offset + total * count <=
459 agp_entry->bound + agp_entry->pages * PAGE_SIZE)) {
460 valid = 1;
461 break;
464 if (!valid) {
465 DRM_DEBUG("zone invalid\n");
466 return EINVAL;
469 entry = &dma->bufs[order];
471 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
472 M_NOWAIT | M_ZERO);
473 if (!entry->buflist) {
474 return ENOMEM;
477 entry->buf_size = size;
478 entry->page_order = page_order;
480 offset = 0;
482 while (entry->buf_count < count) {
483 buf = &entry->buflist[entry->buf_count];
484 buf->idx = dma->buf_count + entry->buf_count;
485 buf->total = alignment;
486 buf->order = order;
487 buf->used = 0;
489 buf->offset = (dma->byte_count + offset);
490 buf->bus_address = agp_offset + offset;
491 buf->address = (void *)(agp_offset + offset);
492 buf->next = NULL;
493 buf->pending = 0;
494 buf->file_priv = NULL;
496 buf->dev_priv_size = dev->driver->buf_priv_size;
497 buf->dev_private = malloc(buf->dev_priv_size, DRM_MEM_BUFS,
498 M_NOWAIT | M_ZERO);
499 if (buf->dev_private == NULL) {
500 /* Set count correctly so we free the proper amount. */
501 entry->buf_count = count;
502 drm_cleanup_buf_error(dev, entry);
503 return ENOMEM;
506 offset += alignment;
507 entry->buf_count++;
508 byte_count += PAGE_SIZE << page_order;
511 DRM_DEBUG("byte_count: %d\n", byte_count);
513 temp_buflist = realloc(dma->buflist,
514 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
515 DRM_MEM_BUFS, M_NOWAIT);
516 if (temp_buflist == NULL) {
517 /* Free the entry because it isn't valid */
518 drm_cleanup_buf_error(dev, entry);
519 return ENOMEM;
521 dma->buflist = temp_buflist;
523 for (i = 0; i < entry->buf_count; i++) {
524 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
527 dma->buf_count += entry->buf_count;
528 dma->byte_count += byte_count;
530 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
531 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
533 request->count = entry->buf_count;
534 request->size = size;
536 dma->flags = _DRM_DMA_USE_AGP;
538 return 0;
541 static int drm_do_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
543 drm_device_dma_t *dma = dev->dma;
544 int count;
545 int order;
546 int size;
547 int total;
548 int page_order;
549 drm_buf_entry_t *entry;
550 drm_buf_t *buf;
551 int alignment;
552 unsigned long offset;
553 int i;
554 int byte_count;
555 int page_count;
556 unsigned long *temp_pagelist;
557 drm_buf_t **temp_buflist;
559 count = request->count;
560 order = drm_order(request->size);
561 size = 1 << order;
563 DRM_DEBUG("count=%d, size=%d (%d), order=%d\n",
564 request->count, request->size, size, order);
566 alignment = (request->flags & _DRM_PAGE_ALIGN)
567 ? round_page(size) : size;
568 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
569 total = PAGE_SIZE << page_order;
571 entry = &dma->bufs[order];
573 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
574 M_NOWAIT | M_ZERO);
575 entry->seglist = malloc(count * sizeof(*entry->seglist), DRM_MEM_SEGS,
576 M_NOWAIT | M_ZERO);
578 /* Keep the original pagelist until we know all the allocations
579 * have succeeded
581 temp_pagelist = malloc((dma->page_count + (count << page_order)) *
582 sizeof(*dma->pagelist), DRM_MEM_PAGES, M_NOWAIT);
584 if (entry->buflist == NULL || entry->seglist == NULL ||
585 temp_pagelist == NULL) {
586 if (temp_pagelist)
587 free(temp_pagelist, DRM_MEM_PAGES);
588 if (entry->seglist)
589 free(entry->seglist, DRM_MEM_SEGS);
590 if (entry->buflist)
591 free(entry->buflist, DRM_MEM_BUFS);
592 return ENOMEM;
595 memcpy(temp_pagelist, dma->pagelist, dma->page_count *
596 sizeof(*dma->pagelist));
598 DRM_DEBUG("pagelist: %d entries\n",
599 dma->page_count + (count << page_order));
601 entry->buf_size = size;
602 entry->page_order = page_order;
603 byte_count = 0;
604 page_count = 0;
606 while (entry->buf_count < count) {
607 DRM_SPINUNLOCK(&dev->dma_lock);
608 drm_dma_handle_t *dmah = drm_pci_alloc(dev, size, alignment,
609 0xfffffffful);
610 DRM_SPINLOCK(&dev->dma_lock);
611 if (dmah == NULL) {
612 /* Set count correctly so we free the proper amount. */
613 entry->buf_count = count;
614 entry->seg_count = count;
615 drm_cleanup_buf_error(dev, entry);
616 free(temp_pagelist, DRM_MEM_PAGES);
617 return ENOMEM;
620 entry->seglist[entry->seg_count++] = dmah;
621 for (i = 0; i < (1 << page_order); i++) {
622 DRM_DEBUG("page %d @ %p\n",
623 dma->page_count + page_count,
624 (char *)dmah->vaddr + PAGE_SIZE * i);
625 temp_pagelist[dma->page_count + page_count++] =
626 (long)dmah->vaddr + PAGE_SIZE * i;
628 for (offset = 0;
629 offset + size <= total && entry->buf_count < count;
630 offset += alignment, ++entry->buf_count) {
631 buf = &entry->buflist[entry->buf_count];
632 buf->idx = dma->buf_count + entry->buf_count;
633 buf->total = alignment;
634 buf->order = order;
635 buf->used = 0;
636 buf->offset = (dma->byte_count + byte_count + offset);
637 buf->address = ((char *)dmah->vaddr + offset);
638 buf->bus_address = dmah->busaddr + offset;
639 buf->next = NULL;
640 buf->pending = 0;
641 buf->file_priv = NULL;
643 buf->dev_priv_size = dev->driver->buf_priv_size;
644 buf->dev_private = malloc(buf->dev_priv_size,
645 DRM_MEM_BUFS, M_NOWAIT | M_ZERO);
646 if (buf->dev_private == NULL) {
647 /* Set count correctly so we free the proper amount. */
648 entry->buf_count = count;
649 entry->seg_count = count;
650 drm_cleanup_buf_error(dev, entry);
651 free(temp_pagelist, DRM_MEM_PAGES);
652 return ENOMEM;
655 DRM_DEBUG("buffer %d @ %p\n",
656 entry->buf_count, buf->address);
658 byte_count += PAGE_SIZE << page_order;
661 temp_buflist = realloc(dma->buflist,
662 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
663 DRM_MEM_BUFS, M_NOWAIT);
664 if (temp_buflist == NULL) {
665 /* Free the entry because it isn't valid */
666 drm_cleanup_buf_error(dev, entry);
667 free(temp_pagelist, DRM_MEM_PAGES);
668 return ENOMEM;
670 dma->buflist = temp_buflist;
672 for (i = 0; i < entry->buf_count; i++) {
673 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
676 /* No allocations failed, so now we can replace the orginal pagelist
677 * with the new one.
679 free(dma->pagelist, DRM_MEM_PAGES);
680 dma->pagelist = temp_pagelist;
682 dma->buf_count += entry->buf_count;
683 dma->seg_count += entry->seg_count;
684 dma->page_count += entry->seg_count << page_order;
685 dma->byte_count += PAGE_SIZE * (entry->seg_count << page_order);
687 request->count = entry->buf_count;
688 request->size = size;
690 return 0;
694 static int drm_do_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
696 drm_device_dma_t *dma = dev->dma;
697 drm_buf_entry_t *entry;
698 drm_buf_t *buf;
699 unsigned long offset;
700 unsigned long agp_offset;
701 int count;
702 int order;
703 int size;
704 int alignment;
705 int page_order;
706 int total;
707 int byte_count;
708 int i;
709 drm_buf_t **temp_buflist;
711 count = request->count;
712 order = drm_order(request->size);
713 size = 1 << order;
715 alignment = (request->flags & _DRM_PAGE_ALIGN)
716 ? round_page(size) : size;
717 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
718 total = PAGE_SIZE << page_order;
720 byte_count = 0;
721 agp_offset = request->agp_start;
723 DRM_DEBUG("count: %d\n", count);
724 DRM_DEBUG("order: %d\n", order);
725 DRM_DEBUG("size: %d\n", size);
726 DRM_DEBUG("agp_offset: %ld\n", agp_offset);
727 DRM_DEBUG("alignment: %d\n", alignment);
728 DRM_DEBUG("page_order: %d\n", page_order);
729 DRM_DEBUG("total: %d\n", total);
731 entry = &dma->bufs[order];
733 entry->buflist = malloc(count * sizeof(*entry->buflist), DRM_MEM_BUFS,
734 M_NOWAIT | M_ZERO);
735 if (entry->buflist == NULL)
736 return ENOMEM;
738 entry->buf_size = size;
739 entry->page_order = page_order;
741 offset = 0;
743 while (entry->buf_count < count) {
744 buf = &entry->buflist[entry->buf_count];
745 buf->idx = dma->buf_count + entry->buf_count;
746 buf->total = alignment;
747 buf->order = order;
748 buf->used = 0;
750 buf->offset = (dma->byte_count + offset);
751 buf->bus_address = agp_offset + offset;
752 buf->address = (void *)(agp_offset + offset + dev->sg->handle);
753 buf->next = NULL;
754 buf->pending = 0;
755 buf->file_priv = NULL;
757 buf->dev_priv_size = dev->driver->buf_priv_size;
758 buf->dev_private = malloc(buf->dev_priv_size, DRM_MEM_BUFS,
759 M_NOWAIT | M_ZERO);
760 if (buf->dev_private == NULL) {
761 /* Set count correctly so we free the proper amount. */
762 entry->buf_count = count;
763 drm_cleanup_buf_error(dev, entry);
764 return ENOMEM;
767 DRM_DEBUG("buffer %d @ %p\n",
768 entry->buf_count, buf->address);
770 offset += alignment;
771 entry->buf_count++;
772 byte_count += PAGE_SIZE << page_order;
775 DRM_DEBUG("byte_count: %d\n", byte_count);
777 temp_buflist = realloc(dma->buflist,
778 (dma->buf_count + entry->buf_count) * sizeof(*dma->buflist),
779 DRM_MEM_BUFS, M_NOWAIT);
780 if (temp_buflist == NULL) {
781 /* Free the entry because it isn't valid */
782 drm_cleanup_buf_error(dev, entry);
783 return ENOMEM;
785 dma->buflist = temp_buflist;
787 for (i = 0; i < entry->buf_count; i++) {
788 dma->buflist[i + dma->buf_count] = &entry->buflist[i];
791 dma->buf_count += entry->buf_count;
792 dma->byte_count += byte_count;
794 DRM_DEBUG("dma->buf_count : %d\n", dma->buf_count);
795 DRM_DEBUG("entry->buf_count : %d\n", entry->buf_count);
797 request->count = entry->buf_count;
798 request->size = size;
800 dma->flags = _DRM_DMA_USE_SG;
802 return 0;
805 int drm_addbufs_agp(struct drm_device *dev, struct drm_buf_desc *request)
807 int order, ret;
809 if (request->count < 0 || request->count > 4096)
810 return EINVAL;
812 order = drm_order(request->size);
813 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
814 return EINVAL;
816 DRM_SPINLOCK(&dev->dma_lock);
818 /* No more allocations after first buffer-using ioctl. */
819 if (dev->buf_use != 0) {
820 DRM_SPINUNLOCK(&dev->dma_lock);
821 return EBUSY;
823 /* No more than one allocation per order */
824 if (dev->dma->bufs[order].buf_count != 0) {
825 DRM_SPINUNLOCK(&dev->dma_lock);
826 return ENOMEM;
829 ret = drm_do_addbufs_agp(dev, request);
831 DRM_SPINUNLOCK(&dev->dma_lock);
833 return ret;
836 int drm_addbufs_sg(struct drm_device *dev, struct drm_buf_desc *request)
838 int order, ret;
840 if (!DRM_SUSER(DRM_CURPROC))
841 return EACCES;
843 if (request->count < 0 || request->count > 4096)
844 return EINVAL;
846 order = drm_order(request->size);
847 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
848 return EINVAL;
850 DRM_SPINLOCK(&dev->dma_lock);
852 /* No more allocations after first buffer-using ioctl. */
853 if (dev->buf_use != 0) {
854 DRM_SPINUNLOCK(&dev->dma_lock);
855 return EBUSY;
857 /* No more than one allocation per order */
858 if (dev->dma->bufs[order].buf_count != 0) {
859 DRM_SPINUNLOCK(&dev->dma_lock);
860 return ENOMEM;
863 ret = drm_do_addbufs_sg(dev, request);
865 DRM_SPINUNLOCK(&dev->dma_lock);
867 return ret;
870 int drm_addbufs_pci(struct drm_device *dev, struct drm_buf_desc *request)
872 int order, ret;
874 if (!DRM_SUSER(DRM_CURPROC))
875 return EACCES;
877 if (request->count < 0 || request->count > 4096)
878 return EINVAL;
880 order = drm_order(request->size);
881 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
882 return EINVAL;
884 DRM_SPINLOCK(&dev->dma_lock);
886 /* No more allocations after first buffer-using ioctl. */
887 if (dev->buf_use != 0) {
888 DRM_SPINUNLOCK(&dev->dma_lock);
889 return EBUSY;
891 /* No more than one allocation per order */
892 if (dev->dma->bufs[order].buf_count != 0) {
893 DRM_SPINUNLOCK(&dev->dma_lock);
894 return ENOMEM;
897 ret = drm_do_addbufs_pci(dev, request);
899 DRM_SPINUNLOCK(&dev->dma_lock);
901 return ret;
904 int drm_addbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
906 struct drm_buf_desc *request = data;
907 int err;
909 if (request->flags & _DRM_AGP_BUFFER)
910 err = drm_addbufs_agp(dev, request);
911 else if (request->flags & _DRM_SG_BUFFER)
912 err = drm_addbufs_sg(dev, request);
913 else
914 err = drm_addbufs_pci(dev, request);
916 return err;
919 int drm_infobufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
921 drm_device_dma_t *dma = dev->dma;
922 struct drm_buf_info *request = data;
923 int i;
924 int count;
925 int retcode = 0;
927 DRM_SPINLOCK(&dev->dma_lock);
928 ++dev->buf_use; /* Can't allocate more after this call */
929 DRM_SPINUNLOCK(&dev->dma_lock);
931 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
932 if (dma->bufs[i].buf_count)
933 ++count;
936 DRM_DEBUG("count = %d\n", count);
938 if (request->count >= count) {
939 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
940 if (dma->bufs[i].buf_count) {
941 struct drm_buf_desc from;
943 from.count = dma->bufs[i].buf_count;
944 from.size = dma->bufs[i].buf_size;
945 from.low_mark = dma->bufs[i].freelist.low_mark;
946 from.high_mark = dma->bufs[i].freelist.high_mark;
948 if (DRM_COPY_TO_USER(&request->list[count], &from,
949 sizeof(struct drm_buf_desc)) != 0) {
950 retcode = EFAULT;
951 break;
954 DRM_DEBUG("%d %d %d %d %d\n",
955 i, dma->bufs[i].buf_count,
956 dma->bufs[i].buf_size,
957 dma->bufs[i].freelist.low_mark,
958 dma->bufs[i].freelist.high_mark);
959 ++count;
963 request->count = count;
965 return retcode;
968 int drm_markbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
970 drm_device_dma_t *dma = dev->dma;
971 struct drm_buf_desc *request = data;
972 int order;
974 DRM_DEBUG("%d, %d, %d\n",
975 request->size, request->low_mark, request->high_mark);
978 order = drm_order(request->size);
979 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER ||
980 request->low_mark < 0 || request->high_mark < 0) {
981 return EINVAL;
984 DRM_SPINLOCK(&dev->dma_lock);
985 if (request->low_mark > dma->bufs[order].buf_count ||
986 request->high_mark > dma->bufs[order].buf_count) {
987 DRM_SPINUNLOCK(&dev->dma_lock);
988 return EINVAL;
991 dma->bufs[order].freelist.low_mark = request->low_mark;
992 dma->bufs[order].freelist.high_mark = request->high_mark;
993 DRM_SPINUNLOCK(&dev->dma_lock);
995 return 0;
998 int drm_freebufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
1000 drm_device_dma_t *dma = dev->dma;
1001 struct drm_buf_free *request = data;
1002 int i;
1003 int idx;
1004 drm_buf_t *buf;
1005 int retcode = 0;
1007 DRM_DEBUG("%d\n", request->count);
1009 DRM_SPINLOCK(&dev->dma_lock);
1010 for (i = 0; i < request->count; i++) {
1011 if (DRM_COPY_FROM_USER(&idx, &request->list[i], sizeof(idx))) {
1012 retcode = EFAULT;
1013 break;
1015 if (idx < 0 || idx >= dma->buf_count) {
1016 DRM_ERROR("Index %d (of %d max)\n",
1017 idx, dma->buf_count - 1);
1018 retcode = EINVAL;
1019 break;
1021 buf = dma->buflist[idx];
1022 if (buf->file_priv != file_priv) {
1023 DRM_ERROR("Process %d freeing buffer not owned\n",
1024 DRM_CURRENTPID);
1025 retcode = EINVAL;
1026 break;
1028 drm_free_buffer(dev, buf);
1030 DRM_SPINUNLOCK(&dev->dma_lock);
1032 return retcode;
1035 int drm_mapbufs(struct drm_device *dev, void *data, struct drm_file *file_priv)
1037 drm_device_dma_t *dma = dev->dma;
1038 int retcode = 0;
1039 const int zero = 0;
1040 vm_offset_t address;
1041 struct vmspace *vms;
1042 #if defined(__FreeBSD__)
1043 vm_ooffset_t foff;
1044 vm_size_t size;
1045 vm_offset_t vaddr;
1046 #elif defined(__NetBSD__)
1047 struct vnode *vn;
1048 voff_t foff;
1049 vsize_t size, rsize;
1050 vaddr_t vaddr;
1051 #endif
1052 struct drm_buf_map *request = data;
1053 int i;
1055 #if defined(__NetBSD__)
1056 if (!vfinddev(dev->kdev, VCHR, &vn))
1057 return 0; /* FIXME: Shouldn't this be EINVAL or something? */
1058 #endif /* __NetBSD__ || __OpenBSD */
1060 #if defined(__FreeBSD__)
1061 vms = DRM_CURPROC->td_proc->p_vmspace;
1062 #elif defined(__NetBSD__)
1063 vms = DRM_CURPROC->p_vmspace;
1064 #endif
1066 DRM_SPINLOCK(&dev->dma_lock);
1067 dev->buf_use++; /* Can't allocate more after this call */
1068 DRM_SPINUNLOCK(&dev->dma_lock);
1070 if (request->count < dma->buf_count)
1071 goto done;
1073 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP)) ||
1074 (drm_core_check_feature(dev, DRIVER_SG) &&
1075 (dma->flags & _DRM_DMA_USE_SG))) {
1076 drm_local_map_t *map = dev->agp_buffer_map;
1078 if (map == NULL) {
1079 retcode = EINVAL;
1080 goto done;
1082 size = round_page(map->size);
1083 foff = map->offset;
1084 } else {
1085 size = round_page(dma->byte_count),
1086 foff = 0;
1089 #if defined(__FreeBSD__)
1090 vaddr = round_page((vm_offset_t)vms->vm_daddr + MAXDSIZ);
1091 #if __FreeBSD_version >= 600023
1092 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1093 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC, OBJT_DEVICE, dev->devnode, foff);
1094 #else
1095 retcode = vm_mmap(&vms->vm_map, &vaddr, size, PROT_READ | PROT_WRITE,
1096 VM_PROT_ALL, MAP_SHARED | MAP_NOSYNC, SLIST_FIRST(&dev->devnode->si_hlist),
1097 foff);
1098 #endif
1099 #elif defined(__NetBSD__)
1100 /* XXXNETBSD */
1101 vaddr = curlwp->l_proc->p_emul->e_vm_default_addr(curlwp->l_proc,
1102 (vaddr_t)vms->vm_daddr, size);
1103 rsize = round_page(size);
1104 DRM_DEBUG("mmap %#lx/%#lx foff %#llx\n", vaddr, rsize, (long long)foff);
1105 retcode = uvm_mmap(&vms->vm_map, &vaddr, rsize,
1106 UVM_PROT_READ | UVM_PROT_WRITE, UVM_PROT_ALL, MAP_SHARED,
1107 &vn->v_uobj, foff, curproc->p_rlimit[RLIMIT_MEMLOCK].rlim_cur);
1108 #endif
1109 if (retcode)
1110 goto done;
1112 request->virtual = (void *)vaddr;
1114 for (i = 0; i < dma->buf_count; i++) {
1115 if (DRM_COPY_TO_USER(&request->list[i].idx,
1116 &dma->buflist[i]->idx, sizeof(request->list[0].idx))) {
1117 retcode = EFAULT;
1118 goto done;
1120 if (DRM_COPY_TO_USER(&request->list[i].total,
1121 &dma->buflist[i]->total, sizeof(request->list[0].total))) {
1122 retcode = EFAULT;
1123 goto done;
1125 if (DRM_COPY_TO_USER(&request->list[i].used, &zero,
1126 sizeof(zero))) {
1127 retcode = EFAULT;
1128 goto done;
1130 address = vaddr + dma->buflist[i]->offset; /* *** */
1131 if (DRM_COPY_TO_USER(&request->list[i].address, &address,
1132 sizeof(address))) {
1133 retcode = EFAULT;
1134 goto done;
1138 done:
1139 request->count = dma->buf_count;
1141 DRM_DEBUG("%d buffers, retcode = %d\n", request->count, retcode);
1143 return retcode;
1147 * Compute order. Can be made faster.
1149 int drm_order(unsigned long size)
1151 #if defined(__FreeBSD__)
1152 int order;
1154 if (size == 0)
1155 return 0;
1157 order = flsl(size) - 1;
1158 if (size & ~(1ul << order))
1159 ++order;
1161 return order;
1162 #elif defined(__NetBSD__)
1163 int order;
1164 unsigned long tmp;
1166 for ( order = 0, tmp = size ; tmp >>= 1 ; ++order );
1168 if ( size & ~(1 << order) )
1169 ++order;
1171 return order;
1172 #endif