3737#include <linux/slab.h>
3838#include <linux/highmem.h>
3939#include <linux/refcount.h>
40+ #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
41+ #include <linux/of_device.h>
42+ #endif
4043
4144#include <xen/xen.h>
4245#include <xen/grant_table.h>
@@ -72,6 +75,11 @@ struct gntdev_priv {
7275 struct mutex lock ;
7376 struct mm_struct * mm ;
7477 struct mmu_notifier mn ;
78+
79+ #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
80+ /* Device for which DMA memory is allocated. */
81+ struct device * dma_dev ;
82+ #endif
7583};
7684
7785struct unmap_notify {
@@ -96,10 +104,27 @@ struct grant_map {
96104 struct gnttab_unmap_grant_ref * kunmap_ops ;
97105 struct page * * pages ;
98106 unsigned long pages_vm_start ;
107+
108+ #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
109+ /*
110+ * If dmabuf_vaddr is not NULL then this mapping is backed by DMA
111+ * capable memory.
112+ */
113+
114+ struct device * dma_dev ;
115+ /* Flags used to create this DMA buffer: GNTDEV_DMA_FLAG_XXX. */
116+ int dma_flags ;
117+ void * dma_vaddr ;
118+ dma_addr_t dma_bus_addr ;
119+ /* Needed to avoid allocation in gnttab_dma_free_pages(). */
120+ xen_pfn_t * frames ;
121+ #endif
99122};
100123
101124static int unmap_grant_pages (struct grant_map * map , int offset , int pages );
102125
126+ static struct miscdevice gntdev_miscdev ;
127+
103128/* ------------------------------------------------------------------ */
104129
105130static void gntdev_print_maps (struct gntdev_priv * priv ,
@@ -121,8 +146,27 @@ static void gntdev_free_map(struct grant_map *map)
121146 if (map == NULL )
122147 return ;
123148
149+ #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
150+ if (map -> dma_vaddr ) {
151+ struct gnttab_dma_alloc_args args ;
152+
153+ args .dev = map -> dma_dev ;
154+ args .coherent = !!(map -> dma_flags & GNTDEV_DMA_FLAG_COHERENT );
155+ args .nr_pages = map -> count ;
156+ args .pages = map -> pages ;
157+ args .frames = map -> frames ;
158+ args .vaddr = map -> dma_vaddr ;
159+ args .dev_bus_addr = map -> dma_bus_addr ;
160+
161+ gnttab_dma_free_pages (& args );
162+ } else
163+ #endif
124164 if (map -> pages )
125165 gnttab_free_pages (map -> count , map -> pages );
166+
167+ #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
168+ kfree (map -> frames );
169+ #endif
126170 kfree (map -> pages );
127171 kfree (map -> grants );
128172 kfree (map -> map_ops );
@@ -132,7 +176,8 @@ static void gntdev_free_map(struct grant_map *map)
132176 kfree (map );
133177}
134178
135- static struct grant_map * gntdev_alloc_map (struct gntdev_priv * priv , int count )
179+ static struct grant_map * gntdev_alloc_map (struct gntdev_priv * priv , int count ,
180+ int dma_flags )
136181{
137182 struct grant_map * add ;
138183 int i ;
@@ -155,6 +200,37 @@ static struct grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count)
155200 NULL == add -> pages )
156201 goto err ;
157202
203+ #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
204+ add -> dma_flags = dma_flags ;
205+
206+ /*
207+ * Check if this mapping is requested to be backed
208+ * by a DMA buffer.
209+ */
210+ if (dma_flags & (GNTDEV_DMA_FLAG_WC | GNTDEV_DMA_FLAG_COHERENT )) {
211+ struct gnttab_dma_alloc_args args ;
212+
213+ add -> frames = kcalloc (count , sizeof (add -> frames [0 ]),
214+ GFP_KERNEL );
215+ if (!add -> frames )
216+ goto err ;
217+
218+ /* Remember the device, so we can free DMA memory. */
219+ add -> dma_dev = priv -> dma_dev ;
220+
221+ args .dev = priv -> dma_dev ;
222+ args .coherent = !!(dma_flags & GNTDEV_DMA_FLAG_COHERENT );
223+ args .nr_pages = count ;
224+ args .pages = add -> pages ;
225+ args .frames = add -> frames ;
226+
227+ if (gnttab_dma_alloc_pages (& args ))
228+ goto err ;
229+
230+ add -> dma_vaddr = args .vaddr ;
231+ add -> dma_bus_addr = args .dev_bus_addr ;
232+ } else
233+ #endif
158234 if (gnttab_alloc_pages (count , add -> pages ))
159235 goto err ;
160236
@@ -325,6 +401,14 @@ static int map_grant_pages(struct grant_map *map)
325401 map -> unmap_ops [i ].handle = map -> map_ops [i ].handle ;
326402 if (use_ptemod )
327403 map -> kunmap_ops [i ].handle = map -> kmap_ops [i ].handle ;
404+ #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
405+ else if (map -> dma_vaddr ) {
406+ unsigned long bfn ;
407+
408+ bfn = pfn_to_bfn (page_to_pfn (map -> pages [i ]));
409+ map -> unmap_ops [i ].dev_bus_addr = __pfn_to_phys (bfn );
410+ }
411+ #endif
328412 }
329413 return err ;
330414}
@@ -548,6 +632,17 @@ static int gntdev_open(struct inode *inode, struct file *flip)
548632 }
549633
550634 flip -> private_data = priv ;
635+ #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
636+ priv -> dma_dev = gntdev_miscdev .this_device ;
637+
638+ /*
639+ * The device is not spawn from a device tree, so arch_setup_dma_ops
640+ * is not called, thus leaving the device with dummy DMA ops.
641+ * Fix this by calling of_dma_configure() with a NULL node to set
642+ * default DMA ops.
643+ */
644+ of_dma_configure (priv -> dma_dev , NULL , true);
645+ #endif
551646 pr_debug ("priv %p\n" , priv );
552647
553648 return 0 ;
@@ -589,7 +684,7 @@ static long gntdev_ioctl_map_grant_ref(struct gntdev_priv *priv,
589684 return - EINVAL ;
590685
591686 err = - ENOMEM ;
592- map = gntdev_alloc_map (priv , op .count );
687+ map = gntdev_alloc_map (priv , op .count , 0 /* This is not a dma-buf. */ );
593688 if (!map )
594689 return err ;
595690
0 commit comments