summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoshua Bakita <jbakita@cs.unc.edu>2022-05-24 21:16:30 -0400
committerJoshua Bakita <jbakita@cs.unc.edu>2022-05-24 21:16:30 -0400
commit2c076a01e5bd0949032ef81cd0e2d37bdecafba5 (patch)
tree12cc1d66b55a6a72ecdd40a416a86b117f04641a
parent70c252977fc080c829b195fece6f46f73586f1e4 (diff)
gpu-paging: Functions to support initial working versionrtss22-aegpu-paging
- Buffer deallocation (+IOCTL) - Buffer reallocation - Private dmabuf user list and accessor
-rw-r--r--drivers/video/tegra/nvmap/nvmap_alloc.c86
-rw-r--r--drivers/video/tegra/nvmap/nvmap_dev.c4
-rw-r--r--drivers/video/tegra/nvmap/nvmap_dmabuf.c11
-rw-r--r--drivers/video/tegra/nvmap/nvmap_ioctl.c13
-rw-r--r--drivers/video/tegra/nvmap/nvmap_ioctl.h2
-rw-r--r--drivers/video/tegra/nvmap/nvmap_priv.h8
-rw-r--r--include/linux/nvmap.h18
-rw-r--r--include/uapi/linux/nvmap.h2
8 files changed, 131 insertions, 13 deletions
diff --git a/drivers/video/tegra/nvmap/nvmap_alloc.c b/drivers/video/tegra/nvmap/nvmap_alloc.c
index 74c9e4b90..ea648b8bf 100644
--- a/drivers/video/tegra/nvmap/nvmap_alloc.c
+++ b/drivers/video/tegra/nvmap/nvmap_alloc.c
@@ -775,6 +775,7 @@ int nvmap_alloc_handle(struct nvmap_client *client,
775 nvmap_stats_read(NS_TOTAL), 775 nvmap_stats_read(NS_TOTAL),
776 nvmap_stats_read(NS_ALLOC)); 776 nvmap_stats_read(NS_ALLOC));
777 h->userflags = flags; 777 h->userflags = flags;
778 h->userheap_mask = heap_mask;
778 nr_page = ((h->size + PAGE_SIZE - 1) >> PAGE_SHIFT); 779 nr_page = ((h->size + PAGE_SIZE - 1) >> PAGE_SHIFT);
779 /* Force mapping to uncached for VPR memory. */ 780 /* Force mapping to uncached for VPR memory. */
780 if (heap_mask & (NVMAP_HEAP_CARVEOUT_VPR | ~nvmap_dev->cpu_access_mask)) 781 if (heap_mask & (NVMAP_HEAP_CARVEOUT_VPR | ~nvmap_dev->cpu_access_mask))
@@ -911,20 +912,9 @@ int nvmap_alloc_handle_from_va(struct nvmap_client *client,
911 return err; 912 return err;
912} 913}
913 914
914void _nvmap_handle_free(struct nvmap_handle *h) 915void __nvmap_handle_free(struct nvmap_handle *h)
915{ 916{
916 unsigned int i, nr_page, page_index = 0; 917 unsigned int i, nr_page, page_index = 0;
917 struct nvmap_handle_dmabuf_priv *curr, *next;
918
919 list_for_each_entry_safe(curr, next, &h->dmabuf_priv, list) {
920 curr->priv_release(curr->priv);
921 list_del(&curr->list);
922 kzfree(curr);
923 }
924
925 if (nvmap_handle_remove(nvmap_dev, h) != 0)
926 return;
927
928 if (!h->alloc) 918 if (!h->alloc)
929 goto out; 919 goto out;
930 920
@@ -980,8 +970,24 @@ void _nvmap_handle_free(struct nvmap_handle *h)
980 } 970 }
981 971
982 nvmap_altfree(h->pgalloc.pages, nr_page * sizeof(struct page *)); 972 nvmap_altfree(h->pgalloc.pages, nr_page * sizeof(struct page *));
983
984out: 973out:
974 h->alloc = false;
975}
976
977void _nvmap_handle_free(struct nvmap_handle *h)
978{
979 struct nvmap_handle_dmabuf_priv *curr, *next;
980
981 list_for_each_entry_safe(curr, next, &h->dmabuf_priv, list) {
982 curr->priv_release(curr->priv);
983 list_del(&curr->list);
984 kzfree(curr);
985 }
986
987 if (nvmap_handle_remove(nvmap_dev, h) != 0)
988 return;
989 __nvmap_handle_free(h);
990 // Next two calls were originally in the `out` clause
985 NVMAP_TAG_TRACE(trace_nvmap_destroy_handle, 991 NVMAP_TAG_TRACE(trace_nvmap_destroy_handle,
986 NULL, get_current()->pid, 0, NVMAP_TP_ARGS_H(h)); 992 NULL, get_current()->pid, 0, NVMAP_TP_ARGS_H(h));
987 kfree(h); 993 kfree(h);
@@ -1041,3 +1047,57 @@ void nvmap_free_handle_fd(struct nvmap_client *client,
1041 nvmap_handle_put(handle); 1047 nvmap_handle_put(handle);
1042 } 1048 }
1043} 1049}
1050
1051// Frees backing pages. Handle remains valid.
1052void nvmap_dealloc_fd(struct nvmap_client *client, int fd)
1053{
1054 struct nvmap_handle *handle = nvmap_handle_get_from_fd(fd);
1055 if (handle)
1056 __nvmap_handle_free(handle);
1057}
1058
1059// For deallocations from elsewhere in the kernel.
1060// (Userspace should use NVMAP_IOC_DEALLOC ioctl.)
1061int nvmap_dealloc_dmabuf(struct dma_buf *dmabuf) {
1062 struct nvmap_handle_info *info;
1063
1064 if (!dmabuf_is_nvmap(dmabuf))
1065 return -EINVAL;
1066 info = dmabuf->priv;
1067 // Fail if mapped into kernelspace
1068 // (Unclear if this is necessary, but no handles have been observed with this
1069 // set, and so testing has not been possible.)
1070 if (atomic_read(&info->handle->kmap_count))
1071 goto out_busy;
1072 // Buffer cannot currently be mapped into userspace (via mmap)
1073 if (atomic_read(&info->handle->umap_count))
1074 goto out_busy;
1075 // Other devices should not still have this pinned into DMA memory
1076 if (atomic_read(&info->handle->pin))
1077 goto out_busy;
1078 __nvmap_handle_free(info->handle);
1079 return 0;
1080out_busy:
1081 printk(KERN_WARNING "nvmap: Unmap requested on FD that is currently in-use! (%d h->pin, %d h->kmap_count, %d h->umap_count)\n",
1082 atomic_read(&info->handle->pin),
1083 atomic_read(&info->handle->kmap_count),
1084 atomic_read(&info->handle->umap_count));
1085 return -EBUSY;
1086}
1087EXPORT_SYMBOL(nvmap_dealloc_dmabuf);
1088
1089// For reallocations from elsewhere in the kernel.
1090// (Userspace should use NVMAP_IOC_ALLOC ioctl.)
1091int nvmap_realloc_dmabuf(struct dma_buf *dmabuf) {
1092 struct nvmap_handle_info *info;
1093 struct nvmap_handle *h;
1094
1095 if (!dmabuf_is_nvmap(dmabuf))
1096 return -EINVAL;
1097 info = dmabuf->priv;
1098 h = info->handle;
1099 return nvmap_alloc_handle(h->owner, h, h->userheap_mask,
1100 h->align, 0, h->userflags,
1101 NVMAP_IVM_INVALID_PEER);
1102}
1103EXPORT_SYMBOL(nvmap_realloc_dmabuf);
diff --git a/drivers/video/tegra/nvmap/nvmap_dev.c b/drivers/video/tegra/nvmap/nvmap_dev.c
index eb5791464..a11e3edfb 100644
--- a/drivers/video/tegra/nvmap/nvmap_dev.c
+++ b/drivers/video/tegra/nvmap/nvmap_dev.c
@@ -360,6 +360,10 @@ static long nvmap_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
360 err = nvmap_ioctl_alloc(filp, uarg); 360 err = nvmap_ioctl_alloc(filp, uarg);
361 break; 361 break;
362 362
363 case NVMAP_IOC_DEALLOC:
364 err = nvmap_ioctl_dealloc(filp, arg);
365 break;
366
363 case NVMAP_IOC_ALLOC_IVM: 367 case NVMAP_IOC_ALLOC_IVM:
364 err = nvmap_ioctl_alloc_ivm(filp, uarg); 368 err = nvmap_ioctl_alloc_ivm(filp, uarg);
365 break; 369 break;
diff --git a/drivers/video/tegra/nvmap/nvmap_dmabuf.c b/drivers/video/tegra/nvmap/nvmap_dmabuf.c
index 850e07e06..33dcf5fe5 100644
--- a/drivers/video/tegra/nvmap/nvmap_dmabuf.c
+++ b/drivers/video/tegra/nvmap/nvmap_dmabuf.c
@@ -659,6 +659,7 @@ struct dma_buf *__nvmap_make_dmabuf(struct nvmap_client *client,
659 info->handle = handle; 659 info->handle = handle;
660 INIT_LIST_HEAD(&info->maps); 660 INIT_LIST_HEAD(&info->maps);
661 mutex_init(&info->maps_lock); 661 mutex_init(&info->maps_lock);
662 INIT_LIST_HEAD(&info->user_priv_list);
662 663
663 dmabuf = __dma_buf_export(info, handle->size, ro_buf); 664 dmabuf = __dma_buf_export(info, handle->size, ro_buf);
664 if (IS_ERR(dmabuf)) { 665 if (IS_ERR(dmabuf)) {
@@ -791,3 +792,13 @@ error:
791 dma_buf_put(dmabuf); 792 dma_buf_put(dmabuf);
792 return ret; 793 return ret;
793} 794}
795
796struct list_head* nvmap_get_priv_list(struct dma_buf *dmabuf) {
797 struct nvmap_handle_info *info;
798
799 if (!dmabuf_is_nvmap(dmabuf))
800 return ERR_PTR(-EINVAL);
801 info = dmabuf->priv;
802 return &info->user_priv_list;
803}
804EXPORT_SYMBOL(nvmap_get_priv_list);
diff --git a/drivers/video/tegra/nvmap/nvmap_ioctl.c b/drivers/video/tegra/nvmap/nvmap_ioctl.c
index 7fd13e625..93a418eb3 100644
--- a/drivers/video/tegra/nvmap/nvmap_ioctl.c
+++ b/drivers/video/tegra/nvmap/nvmap_ioctl.c
@@ -460,6 +460,19 @@ int nvmap_ioctl_free(struct file *filp, unsigned long arg)
460 return sys_close(arg); 460 return sys_close(arg);
461} 461}
462 462
463// Returns memory freed by deallocation
464int nvmap_ioctl_dealloc(struct file *filp, unsigned long arg)
465{
466 struct nvmap_client *client = filp->private_data;
467 u64 before = nvmap_stats_read(NS_TOTAL);
468
469 if (!arg)
470 return 0;
471
472 nvmap_dealloc_fd(client, arg);
473 return before - nvmap_stats_read(NS_TOTAL);
474