37 #include <linux/slab.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;
57 map->type != entry->map->type ||
58 entry->master != dev->primary->master)
67 if ((entry->map->offset & 0xffffffff) ==
68 (map->offset & 0xffffffff))
73 if (entry->map->offset == map->offset)
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 & 0xFFFFFFFF00000000
UL) || 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) {
124 user_token, 32 - PAGE_SHIFT - 3,
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;
164 DRM_DEBUG(
"offset = 0x%08llx, size = 0x%08lx, type = %d\n",
165 (
unsigned long long)map->offset, map->size, map->type);
184 #if !defined(__sparc__) && !defined(__alpha__) && !defined(__ia64__) && !defined(__powerpc64__) && !defined(__x86_64__) && !defined(__arm__)
185 if (map->offset + (map->size-1) < map->offset ||
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 (drm_core_has_MTRR(dev)) {
213 map->mtrr =
mtrr_add(map->offset, map->size,
218 map->handle =
ioremap(map->offset, map->size);
227 list = drm_find_matching_map(dev, map);
229 if(list->map->size != map->size) {
230 DRM_DEBUG(
"Matching maps of type %d with "
231 "mismatched sizes, (%ld vs %ld)\n",
232 map->type, map->size, list->map->size);
233 list->map->size = map->size;
241 DRM_DEBUG(
"%lu %d %p\n",
242 map->size,
drm_order(map->size), map->handle);
247 map->offset = (
unsigned long)map->handle;
250 if (dev->primary->master->lock.hw_lock !=
NULL) {
255 dev->sigdata.lock = dev->primary->master->lock.hw_lock = map->handle;
259 struct drm_agp_mem *
entry;
262 if (!drm_core_has_AGP(dev)) {
267 map->offset += dev->hose->mem_space->start;
275 if (map->offset < dev->agp->base ||
276 map->offset > dev->agp->base +
277 dev->agp->agp_info.aper_size * 1024 * 1024 - 1) {
278 map->offset += dev->agp->base;
280 map->mtrr = dev->agp->agp_mtrr;
289 if ((map->offset >= entry->bound) &&
290 (map->offset + map->size <= entry->bound + entry->pages *
PAGE_SIZE)) {
295 if (!list_empty(&dev->agp->memory) && !valid) {
299 DRM_DEBUG(
"AGP offset = 0x%08llx, size = 0x%08lx\n",
300 (
unsigned long long)map->offset, map->size);
305 DRM_ERROR(
"tried to addmap GEM object\n");
312 map->offset += (
unsigned long)dev->sg->virtual;
324 map->handle = dmah->vaddr;
325 map->offset = (
unsigned long)dmah->busaddr;
343 list_add(&list->head, &dev->maplist);
347 user_token = (map->type ==
_DRM_SHM) ? (
unsigned long)map->handle :
349 ret = drm_map_handle(dev, &list->hash, user_token, 0,
360 list->user_token = list->hash.key <<
PAGE_SHIFT;
364 list->master = dev->primary->master;
373 struct drm_map_list *
list;
376 rc = drm_addmap_core(dev, offset, size, type, flags, &list);
378 *map_ptr = list->map;
396 struct drm_file *file_priv)
399 struct drm_map_list *maplist;
406 map->
flags, &maplist);
412 map->
handle = (
void *)(
unsigned long)maplist->user_token;
428 struct drm_map_list *r_list =
NULL, *list_t;
429 drm_dma_handle_t dmah;
431 struct drm_master *master;
435 if (r_list->map == map) {
436 master = r_list->master;
439 r_list->user_token >> PAGE_SHIFT);
454 if (drm_core_has_MTRR(dev) && map->mtrr >= 0) {
456 retcode =
mtrr_del(map->mtrr, map->offset, map->size);
457 DRM_DEBUG(
"mtrr_del=%d\n", retcode);
463 if (dev->sigdata.lock == master->lock.hw_lock)
464 dev->sigdata.lock =
NULL;
465 master->lock.hw_lock =
NULL;
466 master->lock.file_priv =
NULL;
474 dmah.vaddr = map->handle;
475 dmah.busaddr = map->offset;
476 dmah.size = map->size;
480 DRM_ERROR(
"tried to rmmap GEM object\n");
517 struct drm_file *file_priv)
520 struct drm_local_map *map =
NULL;
521 struct drm_map_list *r_list;
527 r_list->user_token == (
unsigned long)request->
handle &&
537 if (list_empty(&dev->maplist) || !map) {
563 static void drm_cleanup_buf_error(
struct drm_device * dev,
564 struct drm_buf_entry * entry)
568 if (entry->seg_count) {
569 for (i = 0; i < entry->seg_count; i++) {
570 if (entry->seglist[i]) {
574 kfree(entry->seglist);
576 entry->seg_count = 0;
579 if (entry->buf_count) {
580 for (i = 0; i < entry->buf_count; i++) {
581 kfree(entry->buflist[i].dev_private);
583 kfree(entry->buflist);
585 entry->buf_count = 0;
603 struct drm_device_dma *
dma = dev->dma;
604 struct drm_buf_entry *
entry;
605 struct drm_agp_mem *agp_entry;
608 unsigned long agp_offset;
617 struct drm_buf **temp_buflist;
622 count = request->
count;
626 alignment = (request->
flags & _DRM_PAGE_ALIGN)
628 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
632 agp_offset = dev->agp->base + request->
agp_start;
634 DRM_DEBUG(
"count: %d\n", count);
635 DRM_DEBUG(
"order: %d\n", order);
636 DRM_DEBUG(
"size: %d\n", size);
637 DRM_DEBUG(
"agp_offset: %lx\n", agp_offset);
638 DRM_DEBUG(
"alignment: %d\n", alignment);
639 DRM_DEBUG(
"page_order: %d\n", page_order);
640 DRM_DEBUG(
"total: %d\n", total);
648 if ((agp_offset >= agp_entry->bound) &&
649 (agp_offset + total * count <= agp_entry->bound + agp_entry->pages *
PAGE_SIZE)) {
654 if (!list_empty(&dev->agp->memory) && !valid) {
655 DRM_DEBUG(
"zone invalid\n");
658 spin_lock(&dev->count_lock);
660 spin_unlock(&dev->count_lock);
664 spin_unlock(&dev->count_lock);
667 entry = &dma->bufs[
order];
668 if (entry->buf_count) {
674 if (count < 0 || count > 4096) {
680 entry->buflist = kzalloc(count *
sizeof(*entry->buflist),
GFP_KERNEL);
681 if (!entry->buflist) {
687 entry->buf_size =
size;
688 entry->page_order = page_order;
692 while (entry->buf_count < count) {
693 buf = &entry->buflist[entry->buf_count];
694 buf->idx = dma->buf_count + entry->buf_count;
699 buf->offset = (dma->byte_count +
offset);
700 buf->bus_address = agp_offset +
offset;
701 buf->address = (
void *)(agp_offset + offset);
705 buf->file_priv =
NULL;
707 buf->dev_priv_size = dev->driver->dev_priv_size;
708 buf->dev_private = kzalloc(buf->dev_priv_size,
GFP_KERNEL);
709 if (!buf->dev_private) {
711 entry->buf_count =
count;
712 drm_cleanup_buf_error(dev, entry);
718 DRM_DEBUG(
"buffer %d @ %p\n", entry->buf_count, buf->address);
725 DRM_DEBUG(
"byte_count: %d\n", byte_count);
727 temp_buflist =
krealloc(dma->buflist,
728 (dma->buf_count + entry->buf_count) *
732 drm_cleanup_buf_error(dev, entry);
737 dma->buflist = temp_buflist;
739 for (i = 0; i < entry->buf_count; i++) {
740 dma->buflist[i + dma->buf_count] = &entry->buflist[
i];
743 dma->buf_count += entry->buf_count;
744 dma->seg_count += entry->seg_count;
746 dma->byte_count += byte_count;
748 DRM_DEBUG(
"dma->buf_count : %d\n", dma->buf_count);
749 DRM_DEBUG(
"entry->buf_count : %d\n", entry->buf_count);
753 request->
count = entry->buf_count;
756 dma->flags = _DRM_DMA_USE_AGP;
766 struct drm_device_dma *dma = dev->dma;
772 struct drm_buf_entry *
entry;
773 drm_dma_handle_t *dmah;
780 unsigned long *temp_pagelist;
781 struct drm_buf **temp_buflist;
783 if (!drm_core_check_feature(dev, DRIVER_PCI_DMA))
792 count = request->
count;
796 DRM_DEBUG(
"count=%d, size=%d (%d), order=%d\n",
797 request->
count, request->
size, size, order);
799 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
802 alignment = (request->
flags & _DRM_PAGE_ALIGN)
804 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
807 spin_lock(&dev->count_lock);
809 spin_unlock(&dev->count_lock);
813 spin_unlock(&dev->count_lock);
816 entry = &dma->bufs[
order];
817 if (entry->buf_count) {
823 if (count < 0 || count > 4096) {
829 entry->buflist = kzalloc(count *
sizeof(*entry->buflist),
GFP_KERNEL);
830 if (!entry->buflist) {
836 entry->seglist = kzalloc(count *
sizeof(*entry->seglist),
GFP_KERNEL);
837 if (!entry->seglist) {
838 kfree(entry->buflist);
847 temp_pagelist =
kmalloc((dma->page_count + (count << page_order)) *
849 if (!temp_pagelist) {
850 kfree(entry->buflist);
851 kfree(entry->seglist);
857 dma->pagelist, dma->page_count *
sizeof(*dma->pagelist));
858 DRM_DEBUG(
"pagelist: %d entries\n",
859 dma->page_count + (count << page_order));
861 entry->buf_size =
size;
862 entry->page_order = page_order;
866 while (entry->buf_count < count) {
872 entry->buf_count =
count;
873 entry->seg_count =
count;
874 drm_cleanup_buf_error(dev, entry);
875 kfree(temp_pagelist);
880 entry->seglist[entry->seg_count++] = dmah;
881 for (i = 0; i < (1 << page_order); i++) {
882 DRM_DEBUG(
"page %d @ 0x%08lx\n",
883 dma->page_count + page_count,
884 (
unsigned long)dmah->vaddr +
PAGE_SIZE * i);
885 temp_pagelist[dma->page_count + page_count++]
889 offset + size <= total && entry->buf_count <
count;
890 offset +=
alignment, ++entry->buf_count) {
891 buf = &entry->buflist[entry->buf_count];
892 buf->idx = dma->buf_count + entry->buf_count;
896 buf->offset = (dma->byte_count + byte_count +
offset);
897 buf->address = (
void *)(dmah->vaddr + offset);
898 buf->bus_address = dmah->busaddr +
offset;
902 buf->file_priv =
NULL;
904 buf->dev_priv_size = dev->driver->dev_priv_size;
905 buf->dev_private = kzalloc(buf->dev_priv_size,
907 if (!buf->dev_private) {
909 entry->buf_count =
count;
910 entry->seg_count =
count;
911 drm_cleanup_buf_error(dev, entry);
912 kfree(temp_pagelist);
918 DRM_DEBUG(
"buffer %d @ %p\n",
919 entry->buf_count, buf->address);
924 temp_buflist =
krealloc(dma->buflist,
925 (dma->buf_count + entry->buf_count) *
929 drm_cleanup_buf_error(dev, entry);
930 kfree(temp_pagelist);
935 dma->buflist = temp_buflist;
937 for (i = 0; i < entry->buf_count; i++) {
938 dma->buflist[i + dma->buf_count] = &entry->buflist[
i];
944 if (dma->page_count) {
945 kfree(dma->pagelist);
947 dma->pagelist = temp_pagelist;
949 dma->buf_count += entry->buf_count;
950 dma->seg_count += entry->seg_count;
951 dma->page_count += entry->seg_count << page_order;
952 dma->byte_count +=
PAGE_SIZE * (entry->seg_count << page_order);
956 request->
count = entry->buf_count;
959 if (request->
flags & _DRM_PCI_BUFFER_RO)
960 dma->flags = _DRM_DMA_USE_PCI_RO;
970 struct drm_device_dma *dma = dev->dma;
971 struct drm_buf_entry *
entry;
974 unsigned long agp_offset;
983 struct drm_buf **temp_buflist;
985 if (!drm_core_check_feature(dev, DRIVER_SG))
994 count = request->
count;
998 alignment = (request->
flags & _DRM_PAGE_ALIGN)
1000 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
1006 DRM_DEBUG(
"count: %d\n", count);
1007 DRM_DEBUG(
"order: %d\n", order);
1008 DRM_DEBUG(
"size: %d\n", size);
1009 DRM_DEBUG(
"agp_offset: %lu\n", agp_offset);
1010 DRM_DEBUG(
"alignment: %d\n", alignment);
1011 DRM_DEBUG(
"page_order: %d\n", page_order);
1012 DRM_DEBUG(
"total: %d\n", total);
1014 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1017 spin_lock(&dev->count_lock);
1019 spin_unlock(&dev->count_lock);
1023 spin_unlock(&dev->count_lock);
1026 entry = &dma->bufs[
order];
1027 if (entry->buf_count) {
1033 if (count < 0 || count > 4096) {
1039 entry->buflist = kzalloc(count *
sizeof(*entry->buflist),
1041 if (!entry->buflist) {
1047 entry->buf_size =
size;
1048 entry->page_order = page_order;
1052 while (entry->buf_count < count) {
1053 buf = &entry->buflist[entry->buf_count];
1054 buf->idx = dma->buf_count + entry->buf_count;
1059 buf->offset = (dma->byte_count +
offset);
1060 buf->bus_address = agp_offset +
offset;
1061 buf->address = (
void *)(agp_offset + offset
1062 + (
unsigned long)dev->sg->virtual);
1066 buf->file_priv =
NULL;
1068 buf->dev_priv_size = dev->driver->dev_priv_size;
1069 buf->dev_private = kzalloc(buf->dev_priv_size,
GFP_KERNEL);
1070 if (!buf->dev_private) {
1072 entry->buf_count =
count;
1073 drm_cleanup_buf_error(dev, entry);
1079 DRM_DEBUG(
"buffer %d @ %p\n", entry->buf_count, buf->address);
1086 DRM_DEBUG(
"byte_count: %d\n", byte_count);
1088 temp_buflist =
krealloc(dma->buflist,
1089 (dma->buf_count + entry->buf_count) *
1091 if (!temp_buflist) {
1093 drm_cleanup_buf_error(dev, entry);
1098 dma->buflist = temp_buflist;
1100 for (i = 0; i < entry->buf_count; i++) {
1101 dma->buflist[i + dma->buf_count] = &entry->buflist[
i];
1104 dma->buf_count += entry->buf_count;
1105 dma->seg_count += entry->seg_count;
1107 dma->byte_count += byte_count;
1109 DRM_DEBUG(
"dma->buf_count : %d\n", dma->buf_count);
1110 DRM_DEBUG(
"entry->buf_count : %d\n", entry->buf_count);
1114 request->
count = entry->buf_count;
1117 dma->flags = _DRM_DMA_USE_SG;
1125 struct drm_device_dma *dma = dev->dma;
1126 struct drm_buf_entry *
entry;
1127 struct drm_buf *
buf;
1129 unsigned long agp_offset;
1138 struct drm_buf **temp_buflist;
1140 if (!drm_core_check_feature(dev, DRIVER_FB_DMA))
1149 count = request->
count;
1153 alignment = (request->
flags & _DRM_PAGE_ALIGN)
1155 page_order = order - PAGE_SHIFT > 0 ? order - PAGE_SHIFT : 0;
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);
1169 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1172 spin_lock(&dev->count_lock);
1174 spin_unlock(&dev->count_lock);
1178 spin_unlock(&dev->count_lock);
1181 entry = &dma->bufs[
order];
1182 if (entry->buf_count) {
1188 if (count < 0 || count > 4096) {
1194 entry->buflist = kzalloc(count *
sizeof(*entry->buflist),
1196 if (!entry->buflist) {
1202 entry->buf_size =
size;
1203 entry->page_order = page_order;
1207 while (entry->buf_count < count) {
1208 buf = &entry->buflist[entry->buf_count];
1209 buf->idx = dma->buf_count + entry->buf_count;
1214 buf->offset = (dma->byte_count +
offset);
1215 buf->bus_address = agp_offset +
offset;
1216 buf->address = (
void *)(agp_offset + offset);
1220 buf->file_priv =
NULL;
1222 buf->dev_priv_size = dev->driver->dev_priv_size;
1223 buf->dev_private = kzalloc(buf->dev_priv_size,
GFP_KERNEL);
1224 if (!buf->dev_private) {
1226 entry->buf_count =
count;
1227 drm_cleanup_buf_error(dev, entry);
1233 DRM_DEBUG(
"buffer %d @ %p\n", entry->buf_count, buf->address);
1240 DRM_DEBUG(
"byte_count: %d\n", byte_count);
1242 temp_buflist =
krealloc(dma->buflist,
1243 (dma->buf_count + entry->buf_count) *
1245 if (!temp_buflist) {
1247 drm_cleanup_buf_error(dev, entry);
1252 dma->buflist = temp_buflist;
1254 for (i = 0; i < entry->buf_count; i++) {
1255 dma->buflist[i + dma->buf_count] = &entry->buflist[
i];
1258 dma->buf_count += entry->buf_count;
1259 dma->seg_count += entry->seg_count;
1261 dma->byte_count += byte_count;
1263 DRM_DEBUG(
"dma->buf_count : %d\n", dma->buf_count);
1264 DRM_DEBUG(
"entry->buf_count : %d\n", entry->buf_count);
1268 request->
count = entry->buf_count;
1271 dma->flags = _DRM_DMA_USE_FB;
1293 struct drm_file *file_priv)
1298 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1303 ret = drm_addbufs_agp(dev, request);
1307 ret = drm_addbufs_sg(dev, request);
1309 ret = drm_addbufs_fb(dev, request);
1334 struct drm_file *file_priv)
1336 struct drm_device_dma *dma = dev->dma;
1341 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1347 spin_lock(&dev->count_lock);
1349 spin_unlock(&dev->count_lock);
1353 spin_unlock(&dev->count_lock);
1355 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1356 if (dma->bufs[i].buf_count)
1360 DRM_DEBUG(
"count = %d\n", count);
1362 if (request->
count >= count) {
1363 for (i = 0, count = 0; i < DRM_MAX_ORDER + 1; i++) {
1364 if (dma->bufs[i].buf_count) {
1367 struct drm_buf_entry *
from = &dma->bufs[
i];
1368 struct drm_freelist *list = &dma->bufs[
i].freelist;
1371 sizeof(from->buf_count)) ||
1374 sizeof(from->buf_size)) ||
1377 sizeof(list->low_mark)) ||
1380 sizeof(list->high_mark)))
1383 DRM_DEBUG(
"%d %d %d %d %d\n",
1385 dma->bufs[i].buf_count,
1386 dma->bufs[i].buf_size,
1387 dma->bufs[i].freelist.low_mark,
1388 dma->bufs[i].freelist.high_mark);
1413 struct drm_file *file_priv)
1415 struct drm_device_dma *dma = dev->dma;
1418 struct drm_buf_entry *
entry;
1420 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1426 DRM_DEBUG(
"%d, %d, %d\n",
1429 if (order < DRM_MIN_ORDER || order > DRM_MAX_ORDER)
1431 entry = &dma->bufs[
order];
1438 entry->freelist.low_mark = request->
low_mark;
1439 entry->freelist.high_mark = request->
high_mark;
1457 struct drm_file *file_priv)
1459 struct drm_device_dma *dma = dev->dma;
1463 struct drm_buf *
buf;
1465 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1471 DRM_DEBUG(
"%d\n", request->
count);
1472 for (i = 0; i < request->
count; i++) {
1475 if (idx < 0 || idx >= dma->buf_count) {
1476 DRM_ERROR(
"Index %d (of %d max)\n",
1477 idx, dma->buf_count - 1);
1480 buf = dma->buflist[
idx];
1481 if (buf->file_priv != file_priv) {
1482 DRM_ERROR(
"Process %d freeing buffer not owned\n",
1507 struct drm_file *file_priv)
1509 struct drm_device_dma *dma = dev->dma;
1512 unsigned long virtual;
1517 if (!drm_core_check_feature(dev, DRIVER_HAVE_DMA))
1523 spin_lock(&dev->count_lock);
1525 spin_unlock(&dev->count_lock);
1529 spin_unlock(&dev->count_lock);
1531 if (request->
count >= dma->buf_count) {
1532 if ((drm_core_has_AGP(dev) && (dma->flags & _DRM_DMA_USE_AGP))
1533 || (drm_core_check_feature(dev, DRIVER_SG)
1534 && (dma->flags & _DRM_DMA_USE_SG))
1535 || (drm_core_check_feature(dev, DRIVER_FB_DMA)
1536 && (dma->flags & _DRM_DMA_USE_FB))) {
1537 struct drm_local_map *map = dev->agp_buffer_map;
1538 unsigned long token = dev->agp_buffer_token;
1544 virtual =
vm_mmap(file_priv->filp, 0, map->size,
1549 virtual =
vm_mmap(file_priv->filp, 0, dma->byte_count,
1553 if (
virtual > -1024
UL) {
1555 retcode = (
signed long)
virtual;
1560 for (i = 0; i < dma->buf_count; i++) {
1562 &dma->buflist[i]->idx,
1563 sizeof(request->
list[0].idx))) {
1568 &dma->buflist[i]->total,
1569 sizeof(request->
list[0].total))) {
1574 &zero,
sizeof(zero))) {
1578 address =
virtual + dma->buflist[
i]->offset;
1580 &address,
sizeof(address))) {
1587 request->
count = dma->buf_count;
1588 DRM_DEBUG(
"%d buffers, retcode = %d\n", request->
count, retcode);
1607 for (order = 0, tmp = size >> 1;
tmp; tmp >>= 1, order++) ;
1609 if (size & (size - 1))