xref: /OK3568_Linux_fs/kernel/drivers/xen/gntdev-common.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun /*
4*4882a593Smuzhiyun  * Common functionality of grant device.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (c) 2006-2007, D G Murray.
7*4882a593Smuzhiyun  *           (c) 2009 Gerd Hoffmann <kraxel@redhat.com>
8*4882a593Smuzhiyun  *           (c) 2018 Oleksandr Andrushchenko, EPAM Systems Inc.
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #ifndef _GNTDEV_COMMON_H
12*4882a593Smuzhiyun #define _GNTDEV_COMMON_H
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include <linux/mm.h>
15*4882a593Smuzhiyun #include <linux/mman.h>
16*4882a593Smuzhiyun #include <linux/mmu_notifier.h>
17*4882a593Smuzhiyun #include <linux/types.h>
18*4882a593Smuzhiyun #include <xen/interface/event_channel.h>
19*4882a593Smuzhiyun #include <xen/grant_table.h>
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun struct gntdev_dmabuf_priv;
22*4882a593Smuzhiyun 
23*4882a593Smuzhiyun struct gntdev_priv {
24*4882a593Smuzhiyun 	/* Maps with visible offsets in the file descriptor. */
25*4882a593Smuzhiyun 	struct list_head maps;
26*4882a593Smuzhiyun 	/* lock protects maps and freeable_maps. */
27*4882a593Smuzhiyun 	struct mutex lock;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
30*4882a593Smuzhiyun 	/* Device for which DMA memory is allocated. */
31*4882a593Smuzhiyun 	struct device *dma_dev;
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun #ifdef CONFIG_XEN_GNTDEV_DMABUF
35*4882a593Smuzhiyun 	struct gntdev_dmabuf_priv *dmabuf_priv;
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun };
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun struct gntdev_unmap_notify {
40*4882a593Smuzhiyun 	int flags;
41*4882a593Smuzhiyun 	/* Address relative to the start of the gntdev_grant_map. */
42*4882a593Smuzhiyun 	int addr;
43*4882a593Smuzhiyun 	evtchn_port_t event;
44*4882a593Smuzhiyun };
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun struct gntdev_grant_map {
47*4882a593Smuzhiyun 	atomic_t in_use;
48*4882a593Smuzhiyun 	struct mmu_interval_notifier notifier;
49*4882a593Smuzhiyun 	bool notifier_init;
50*4882a593Smuzhiyun 	struct list_head next;
51*4882a593Smuzhiyun 	int index;
52*4882a593Smuzhiyun 	int count;
53*4882a593Smuzhiyun 	int flags;
54*4882a593Smuzhiyun 	refcount_t users;
55*4882a593Smuzhiyun 	struct gntdev_unmap_notify notify;
56*4882a593Smuzhiyun 	struct ioctl_gntdev_grant_ref *grants;
57*4882a593Smuzhiyun 	struct gnttab_map_grant_ref   *map_ops;
58*4882a593Smuzhiyun 	struct gnttab_unmap_grant_ref *unmap_ops;
59*4882a593Smuzhiyun 	struct gnttab_map_grant_ref   *kmap_ops;
60*4882a593Smuzhiyun 	struct gnttab_unmap_grant_ref *kunmap_ops;
61*4882a593Smuzhiyun 	bool *being_removed;
62*4882a593Smuzhiyun 	struct page **pages;
63*4882a593Smuzhiyun 	unsigned long pages_vm_start;
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun #ifdef CONFIG_XEN_GRANT_DMA_ALLOC
66*4882a593Smuzhiyun 	/*
67*4882a593Smuzhiyun 	 * If dmabuf_vaddr is not NULL then this mapping is backed by DMA
68*4882a593Smuzhiyun 	 * capable memory.
69*4882a593Smuzhiyun 	 */
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	struct device *dma_dev;
72*4882a593Smuzhiyun 	/* Flags used to create this DMA buffer: GNTDEV_DMA_FLAG_XXX. */
73*4882a593Smuzhiyun 	int dma_flags;
74*4882a593Smuzhiyun 	void *dma_vaddr;
75*4882a593Smuzhiyun 	dma_addr_t dma_bus_addr;
76*4882a593Smuzhiyun 	/* Needed to avoid allocation in gnttab_dma_free_pages(). */
77*4882a593Smuzhiyun 	xen_pfn_t *frames;
78*4882a593Smuzhiyun #endif
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	/* Number of live grants */
81*4882a593Smuzhiyun 	atomic_t live_grants;
82*4882a593Smuzhiyun 	/* Needed to avoid allocation in __unmap_grant_pages */
83*4882a593Smuzhiyun 	struct gntab_unmap_queue_data unmap_data;
84*4882a593Smuzhiyun };
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun struct gntdev_grant_map *gntdev_alloc_map(struct gntdev_priv *priv, int count,
87*4882a593Smuzhiyun 					  int dma_flags);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun void gntdev_add_map(struct gntdev_priv *priv, struct gntdev_grant_map *add);
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun void gntdev_put_map(struct gntdev_priv *priv, struct gntdev_grant_map *map);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun bool gntdev_test_page_count(unsigned int count);
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun int gntdev_map_grant_pages(struct gntdev_grant_map *map);
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun #endif
98