1 /******************************************************************************
2  * gntdev.h
3  *
4  * Interface to /dev/xen/gntdev.
5  *
6  * Copyright (c) 2007, D G Murray
7  * Copyright (c) 2018, Oleksandr Andrushchenko, EPAM Systems Inc.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License version 2
11  * as published by the Free Software Foundation; or, when distributed
12  * separately from the Linux kernel or incorporated into other
13  * software packages, subject to the following license:
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining a copy
16  * of this source file (the "Software"), to deal in the Software without
17  * restriction, including without limitation the rights to use, copy, modify,
18  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
19  * and to permit persons to whom the Software is furnished to do so, subject to
20  * the following conditions:
21  *
22  * The above copyright notice and this permission notice shall be included in
23  * all copies or substantial portions of the Software.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
30  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
31  * IN THE SOFTWARE.
32  */
33 
34 #ifndef __LINUX_PUBLIC_GNTDEV_H__
35 #define __LINUX_PUBLIC_GNTDEV_H__
36 
37 struct ioctl_gntdev_grant_ref {
38 	/* The domain ID of the grant to be mapped. */
39 	uint32_t domid;
40 	/* The grant reference of the grant to be mapped. */
41 	uint32_t ref;
42 };
43 
44 /*
45  * Inserts the grant references into the mapping table of an instance
46  * of gntdev. N.B. This does not perform the mapping, which is deferred
47  * until mmap() is called with @index as the offset.
48  */
49 #define IOCTL_GNTDEV_MAP_GRANT_REF \
50 _IOC(_IOC_NONE, 'G', 0, sizeof(struct ioctl_gntdev_map_grant_ref))
51 struct ioctl_gntdev_map_grant_ref {
52 	/* IN parameters */
53 	/* The number of grants to be mapped. */
54 	uint32_t count;
55 	uint32_t pad;
56 	/* OUT parameters */
57 	/* The offset to be used on a subsequent call to mmap(). */
58 	uint64_t index;
59 	/* Variable IN parameter. */
60 	/* Array of grant references, of size @count. */
61 	struct ioctl_gntdev_grant_ref refs[1];
62 };
63 
64 /*
65  * Removes the grant references from the mapping table of an instance of
66  * of gntdev. N.B. munmap() must be called on the relevant virtual address(es)
67  * before this ioctl is called, or an error will result.
68  */
69 #define IOCTL_GNTDEV_UNMAP_GRANT_REF \
70 _IOC(_IOC_NONE, 'G', 1, sizeof(struct ioctl_gntdev_unmap_grant_ref))
71 struct ioctl_gntdev_unmap_grant_ref {
72 	/* IN parameters */
73 	/* The offset was returned by the corresponding map operation. */
74 	uint64_t index;
75 	/* The number of pages to be unmapped. */
76 	uint32_t count;
77 	uint32_t pad;
78 };
79 
80 /*
81  * Returns the offset in the driver's address space that corresponds
82  * to @vaddr. This can be used to perform a munmap(), followed by an
83  * UNMAP_GRANT_REF ioctl, where no state about the offset is retained by
84  * the caller. The number of pages that were allocated at the same time as
85  * @vaddr is returned in @count.
86  *
87  * N.B. Where more than one page has been mapped into a contiguous range, the
88  *      supplied @vaddr must correspond to the start of the range; otherwise
89  *      an error will result. It is only possible to munmap() the entire
90  *      contiguously-allocated range at once, and not any subrange thereof.
91  */
92 #define IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR \
93 _IOC(_IOC_NONE, 'G', 2, sizeof(struct ioctl_gntdev_get_offset_for_vaddr))
94 struct ioctl_gntdev_get_offset_for_vaddr {
95 	/* IN parameters */
96 	/* The virtual address of the first mapped page in a range. */
97 	uint64_t vaddr;
98 	/* OUT parameters */
99 	/* The offset that was used in the initial mmap() operation. */
100 	uint64_t offset;
101 	/* The number of pages mapped in the VM area that begins at @vaddr. */
102 	uint32_t count;
103 	uint32_t pad;
104 };
105 
106 /*
107  * Sets the maximum number of grants that may mapped at once by this gntdev
108  * instance.
109  *
110  * N.B. This must be called before any other ioctl is performed on the device.
111  */
112 #define IOCTL_GNTDEV_SET_MAX_GRANTS \
113 _IOC(_IOC_NONE, 'G', 3, sizeof(struct ioctl_gntdev_set_max_grants))
114 struct ioctl_gntdev_set_max_grants {
115 	/* IN parameter */
116 	/* The maximum number of grants that may be mapped at once. */
117 	uint32_t count;
118 };
119 
120 /*
121  * Sets up an unmap notification within the page, so that the other side can do
122  * cleanup if this side crashes. Required to implement cross-domain robust
123  * mutexes or close notification on communication channels.
124  *
125  * Each mapped page only supports one notification; multiple calls referring to
126  * the same page overwrite the previous notification. You must clear the
127  * notification prior to the IOCTL_GNTALLOC_DEALLOC_GREF if you do not want it
128  * to occur.
129  */
130 #define IOCTL_GNTDEV_SET_UNMAP_NOTIFY \
131 _IOC(_IOC_NONE, 'G', 7, sizeof(struct ioctl_gntdev_unmap_notify))
132 struct ioctl_gntdev_unmap_notify {
133 	/* IN parameters */
134 	/* Offset in the file descriptor for a byte within the page. This offset
135 	 * is the result of the IOCTL_GNTDEV_MAP_GRANT_REF and is the same as
136 	 * is used with mmap(). If using UNMAP_NOTIFY_CLEAR_BYTE, this is the byte
137 	 * within the page to be cleared.
138 	 */
139 	uint64_t index;
140 	/* Action(s) to take on unmap */
141 	uint32_t action;
142 	/* Event channel to notify */
143 	uint32_t event_channel_port;
144 };
145 
146 /* Clear (set to zero) the byte specified by index */
147 #define UNMAP_NOTIFY_CLEAR_BYTE 0x1
148 /* Send an interrupt on the indicated event channel */
149 #define UNMAP_NOTIFY_SEND_EVENT 0x2
150 
151 struct ioctl_gntdev_grant_copy_segment {
152     union {
153         void *virt;
154         struct {
155             uint32_t ref;
156             uint16_t offset;
157             uint16_t domid;
158         } foreign;
159     } source, dest;
160     uint16_t len;
161     uint16_t flags;
162     int16_t status;
163 };
164 
165 #define IOCTL_GNTDEV_GRANT_COPY \
166 _IOC(_IOC_NONE, 'G', 8, sizeof(struct ioctl_gntdev_grant_copy))
167 struct ioctl_gntdev_grant_copy {
168     unsigned int count;
169     struct ioctl_gntdev_grant_copy_segment *segments;
170 };
171 
172 /*
173  * Flags to be used while requesting memory mapping's backing storage
174  * to be allocated with DMA API.
175  */
176 
177 /*
178  * The buffer is backed with memory allocated with dma_alloc_wc.
179  */
180 #define GNTDEV_DMA_FLAG_WC		(1 << 0)
181 
182 /*
183  * The buffer is backed with memory allocated with dma_alloc_coherent.
184  */
185 #define GNTDEV_DMA_FLAG_COHERENT	(1 << 1)
186 
187 /*
188  * Create a dma-buf [1] from grant references @refs of count @count provided
189  * by the foreign domain @domid with flags @flags.
190  *
191  * By default dma-buf is backed by system memory pages, but by providing
192  * one of the GNTDEV_DMA_FLAG_XXX flags it can also be created as
193  * a DMA write-combine or coherent buffer, e.g. allocated with dma_alloc_wc/
194  * dma_alloc_coherent.
195  *
196  * Returns 0 if dma-buf was successfully created and the corresponding
197  * dma-buf's file descriptor is returned in @fd.
198  *
199  * [1] https://elixir.bootlin.com/linux/latest/source/Documentation/driver-api/dma-buf.rst
200  */
201 
202 #define IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS \
203     _IOC(_IOC_NONE, 'G', 9, \
204          sizeof(struct ioctl_gntdev_dmabuf_exp_from_refs))
205 struct ioctl_gntdev_dmabuf_exp_from_refs {
206     /* IN parameters. */
207     /* Specific options for this dma-buf: see GNTDEV_DMABUF_FLAG_XXX. */
208     uint32_t flags;
209     /* Number of grant references in @refs array. */
210     uint32_t count;
211     /* OUT parameters. */
212     /* File descriptor of the dma-buf. */
213     uint32_t fd;
214     /* The domain ID of the grant references to be mapped. */
215     uint32_t domid;
216     /* Variable IN parameter. */
217     /* Array of grant references of size @count. */
218     uint32_t refs[1];
219 };
220 
221 /*
222  * This will block until the dma-buf with the file descriptor @fd is
223  * released. This is only valid for buffers created with
224  * IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS.
225  *
226  * If withing @wait_to_ms milliseconds the buffer is not released
227  * then -ETIMEDOUT error is returned.
228  * If the buffer with file descriptor @fd does not exist or has already
229  * been released, then -ENOENT is returned. For valid file descriptors
230  * this must not be treated as error.
231  */
232 #define IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED \
233     _IOC(_IOC_NONE, 'G', 10, \
234          sizeof(struct ioctl_gntdev_dmabuf_exp_wait_released))
235 struct ioctl_gntdev_dmabuf_exp_wait_released {
236     /* IN parameters */
237     uint32_t fd;
238     uint32_t wait_to_ms;
239 };
240 
241 /*
242  * Import a dma-buf with file descriptor @fd and export granted references
243  * to the pages of that dma-buf into array @refs of size @count.
244  */
245 #define IOCTL_GNTDEV_DMABUF_IMP_TO_REFS \
246     _IOC(_IOC_NONE, 'G', 11, \
247          sizeof(struct ioctl_gntdev_dmabuf_imp_to_refs))
248 struct ioctl_gntdev_dmabuf_imp_to_refs {
249     /* IN parameters. */
250     /* File descriptor of the dma-buf. */
251     uint32_t fd;
252     /* Number of grant references in @refs array. */
253     uint32_t count;
254     /* The domain ID for which references to be granted. */
255     uint32_t domid;
256     /* Reserved - must be zero. */
257     uint32_t reserved;
258     /* OUT parameters. */
259     /* Array of grant references of size @count. */
260     uint32_t refs[1];
261 };
262 
263 /*
264  * This will close all references to an imported buffer, so it can be
265  * released by the owner. This is only valid for buffers created with
266  * IOCTL_GNTDEV_DMABUF_IMP_TO_REFS.
267  */
268 #define IOCTL_GNTDEV_DMABUF_IMP_RELEASE \
269     _IOC(_IOC_NONE, 'G', 12, \
270          sizeof(struct ioctl_gntdev_dmabuf_imp_release))
271 struct ioctl_gntdev_dmabuf_imp_release {
272     /* IN parameters */
273     uint32_t fd;
274     uint32_t reserved;
275 };
276 
277 #endif /* __LINUX_PUBLIC_GNTDEV_H__ */
278