xref: /linux/include/uapi/xen/gntdev.h (revision 932d6562179efe8e2460a0343dbe0fcacf288a9e)
1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR MIT) */
2564eb714SDavid Vrabel /******************************************************************************
3564eb714SDavid Vrabel  * gntdev.h
4564eb714SDavid Vrabel  *
5564eb714SDavid Vrabel  * Interface to /dev/xen/gntdev.
6564eb714SDavid Vrabel  *
7564eb714SDavid Vrabel  * Copyright (c) 2007, D G Murray
8*932d6562SOleksandr Andrushchenko  * Copyright (c) 2018, Oleksandr Andrushchenko, EPAM Systems Inc.
9564eb714SDavid Vrabel  *
10564eb714SDavid Vrabel  * This program is free software; you can redistribute it and/or
11564eb714SDavid Vrabel  * modify it under the terms of the GNU General Public License version 2
12564eb714SDavid Vrabel  * as published by the Free Software Foundation; or, when distributed
13564eb714SDavid Vrabel  * separately from the Linux kernel or incorporated into other
14564eb714SDavid Vrabel  * software packages, subject to the following license:
15564eb714SDavid Vrabel  *
16564eb714SDavid Vrabel  * Permission is hereby granted, free of charge, to any person obtaining a copy
17564eb714SDavid Vrabel  * of this source file (the "Software"), to deal in the Software without
18564eb714SDavid Vrabel  * restriction, including without limitation the rights to use, copy, modify,
19564eb714SDavid Vrabel  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
20564eb714SDavid Vrabel  * and to permit persons to whom the Software is furnished to do so, subject to
21564eb714SDavid Vrabel  * the following conditions:
22564eb714SDavid Vrabel  *
23564eb714SDavid Vrabel  * The above copyright notice and this permission notice shall be included in
24564eb714SDavid Vrabel  * all copies or substantial portions of the Software.
25564eb714SDavid Vrabel  *
26564eb714SDavid Vrabel  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27564eb714SDavid Vrabel  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28564eb714SDavid Vrabel  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29564eb714SDavid Vrabel  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30564eb714SDavid Vrabel  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
31564eb714SDavid Vrabel  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
32564eb714SDavid Vrabel  * IN THE SOFTWARE.
33564eb714SDavid Vrabel  */
34564eb714SDavid Vrabel 
35564eb714SDavid Vrabel #ifndef __LINUX_PUBLIC_GNTDEV_H__
36564eb714SDavid Vrabel #define __LINUX_PUBLIC_GNTDEV_H__
37564eb714SDavid Vrabel 
38a36012beSMikko Rapeli #include <linux/types.h>
39a36012beSMikko Rapeli 
40564eb714SDavid Vrabel struct ioctl_gntdev_grant_ref {
41564eb714SDavid Vrabel 	/* The domain ID of the grant to be mapped. */
42a36012beSMikko Rapeli 	__u32 domid;
43564eb714SDavid Vrabel 	/* The grant reference of the grant to be mapped. */
44a36012beSMikko Rapeli 	__u32 ref;
45564eb714SDavid Vrabel };
46564eb714SDavid Vrabel 
47564eb714SDavid Vrabel /*
48564eb714SDavid Vrabel  * Inserts the grant references into the mapping table of an instance
49564eb714SDavid Vrabel  * of gntdev. N.B. This does not perform the mapping, which is deferred
50564eb714SDavid Vrabel  * until mmap() is called with @index as the offset.
51564eb714SDavid Vrabel  */
52564eb714SDavid Vrabel #define IOCTL_GNTDEV_MAP_GRANT_REF \
53564eb714SDavid Vrabel _IOC(_IOC_NONE, 'G', 0, sizeof(struct ioctl_gntdev_map_grant_ref))
54564eb714SDavid Vrabel struct ioctl_gntdev_map_grant_ref {
55564eb714SDavid Vrabel 	/* IN parameters */
56564eb714SDavid Vrabel 	/* The number of grants to be mapped. */
57a36012beSMikko Rapeli 	__u32 count;
58a36012beSMikko Rapeli 	__u32 pad;
59564eb714SDavid Vrabel 	/* OUT parameters */
60564eb714SDavid Vrabel 	/* The offset to be used on a subsequent call to mmap(). */
61a36012beSMikko Rapeli 	__u64 index;
62564eb714SDavid Vrabel 	/* Variable IN parameter. */
63564eb714SDavid Vrabel 	/* Array of grant references, of size @count. */
64564eb714SDavid Vrabel 	struct ioctl_gntdev_grant_ref refs[1];
65564eb714SDavid Vrabel };
66564eb714SDavid Vrabel 
67564eb714SDavid Vrabel /*
68564eb714SDavid Vrabel  * Removes the grant references from the mapping table of an instance of
69564eb714SDavid Vrabel  * of gntdev. N.B. munmap() must be called on the relevant virtual address(es)
70564eb714SDavid Vrabel  * before this ioctl is called, or an error will result.
71564eb714SDavid Vrabel  */
72564eb714SDavid Vrabel #define IOCTL_GNTDEV_UNMAP_GRANT_REF \
73564eb714SDavid Vrabel _IOC(_IOC_NONE, 'G', 1, sizeof(struct ioctl_gntdev_unmap_grant_ref))
74564eb714SDavid Vrabel struct ioctl_gntdev_unmap_grant_ref {
75564eb714SDavid Vrabel 	/* IN parameters */
76564eb714SDavid Vrabel 	/* The offset was returned by the corresponding map operation. */
77a36012beSMikko Rapeli 	__u64 index;
78564eb714SDavid Vrabel 	/* The number of pages to be unmapped. */
79a36012beSMikko Rapeli 	__u32 count;
80a36012beSMikko Rapeli 	__u32 pad;
81564eb714SDavid Vrabel };
82564eb714SDavid Vrabel 
83564eb714SDavid Vrabel /*
84564eb714SDavid Vrabel  * Returns the offset in the driver's address space that corresponds
85564eb714SDavid Vrabel  * to @vaddr. This can be used to perform a munmap(), followed by an
86564eb714SDavid Vrabel  * UNMAP_GRANT_REF ioctl, where no state about the offset is retained by
87564eb714SDavid Vrabel  * the caller. The number of pages that were allocated at the same time as
88564eb714SDavid Vrabel  * @vaddr is returned in @count.
89564eb714SDavid Vrabel  *
90564eb714SDavid Vrabel  * N.B. Where more than one page has been mapped into a contiguous range, the
91564eb714SDavid Vrabel  *      supplied @vaddr must correspond to the start of the range; otherwise
92564eb714SDavid Vrabel  *      an error will result. It is only possible to munmap() the entire
93564eb714SDavid Vrabel  *      contiguously-allocated range at once, and not any subrange thereof.
94564eb714SDavid Vrabel  */
95564eb714SDavid Vrabel #define IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR \
96564eb714SDavid Vrabel _IOC(_IOC_NONE, 'G', 2, sizeof(struct ioctl_gntdev_get_offset_for_vaddr))
97564eb714SDavid Vrabel struct ioctl_gntdev_get_offset_for_vaddr {
98564eb714SDavid Vrabel 	/* IN parameters */
99564eb714SDavid Vrabel 	/* The virtual address of the first mapped page in a range. */
100a36012beSMikko Rapeli 	__u64 vaddr;
101564eb714SDavid Vrabel 	/* OUT parameters */
102564eb714SDavid Vrabel 	/* The offset that was used in the initial mmap() operation. */
103a36012beSMikko Rapeli 	__u64 offset;
104564eb714SDavid Vrabel 	/* The number of pages mapped in the VM area that begins at @vaddr. */
105a36012beSMikko Rapeli 	__u32 count;
106a36012beSMikko Rapeli 	__u32 pad;
107564eb714SDavid Vrabel };
108564eb714SDavid Vrabel 
109564eb714SDavid Vrabel /*
110564eb714SDavid Vrabel  * Sets the maximum number of grants that may mapped at once by this gntdev
111564eb714SDavid Vrabel  * instance.
112564eb714SDavid Vrabel  *
113564eb714SDavid Vrabel  * N.B. This must be called before any other ioctl is performed on the device.
114564eb714SDavid Vrabel  */
115564eb714SDavid Vrabel #define IOCTL_GNTDEV_SET_MAX_GRANTS \
116564eb714SDavid Vrabel _IOC(_IOC_NONE, 'G', 3, sizeof(struct ioctl_gntdev_set_max_grants))
117564eb714SDavid Vrabel struct ioctl_gntdev_set_max_grants {
118564eb714SDavid Vrabel 	/* IN parameter */
119564eb714SDavid Vrabel 	/* The maximum number of grants that may be mapped at once. */
120a36012beSMikko Rapeli 	__u32 count;
121564eb714SDavid Vrabel };
122564eb714SDavid Vrabel 
123564eb714SDavid Vrabel /*
124564eb714SDavid Vrabel  * Sets up an unmap notification within the page, so that the other side can do
125564eb714SDavid Vrabel  * cleanup if this side crashes. Required to implement cross-domain robust
126564eb714SDavid Vrabel  * mutexes or close notification on communication channels.
127564eb714SDavid Vrabel  *
128564eb714SDavid Vrabel  * Each mapped page only supports one notification; multiple calls referring to
129564eb714SDavid Vrabel  * the same page overwrite the previous notification. You must clear the
130564eb714SDavid Vrabel  * notification prior to the IOCTL_GNTALLOC_DEALLOC_GREF if you do not want it
131564eb714SDavid Vrabel  * to occur.
132564eb714SDavid Vrabel  */
133564eb714SDavid Vrabel #define IOCTL_GNTDEV_SET_UNMAP_NOTIFY \
134564eb714SDavid Vrabel _IOC(_IOC_NONE, 'G', 7, sizeof(struct ioctl_gntdev_unmap_notify))
135564eb714SDavid Vrabel struct ioctl_gntdev_unmap_notify {
136564eb714SDavid Vrabel 	/* IN parameters */
137564eb714SDavid Vrabel 	/* Offset in the file descriptor for a byte within the page (same as
138564eb714SDavid Vrabel 	 * used in mmap). If using UNMAP_NOTIFY_CLEAR_BYTE, this is the byte to
139564eb714SDavid Vrabel 	 * be cleared. Otherwise, it can be any byte in the page whose
140564eb714SDavid Vrabel 	 * notification we are adjusting.
141564eb714SDavid Vrabel 	 */
142a36012beSMikko Rapeli 	__u64 index;
143564eb714SDavid Vrabel 	/* Action(s) to take on unmap */
144a36012beSMikko Rapeli 	__u32 action;
145564eb714SDavid Vrabel 	/* Event channel to notify */
146a36012beSMikko Rapeli 	__u32 event_channel_port;
147564eb714SDavid Vrabel };
148564eb714SDavid Vrabel 
149a4cdb556SDavid Vrabel struct gntdev_grant_copy_segment {
150a4cdb556SDavid Vrabel 	union {
151a4cdb556SDavid Vrabel 		void __user *virt;
152a4cdb556SDavid Vrabel 		struct {
153a4cdb556SDavid Vrabel 			grant_ref_t ref;
154a4cdb556SDavid Vrabel 			__u16 offset;
155a4cdb556SDavid Vrabel 			domid_t domid;
156a4cdb556SDavid Vrabel 		} foreign;
157a4cdb556SDavid Vrabel 	} source, dest;
158a4cdb556SDavid Vrabel 	__u16 len;
159a4cdb556SDavid Vrabel 
160a4cdb556SDavid Vrabel 	__u16 flags;  /* GNTCOPY_* */
161a4cdb556SDavid Vrabel 	__s16 status; /* GNTST_* */
162a4cdb556SDavid Vrabel };
163a4cdb556SDavid Vrabel 
164a4cdb556SDavid Vrabel /*
165a4cdb556SDavid Vrabel  * Copy between grant references and local buffers.
166a4cdb556SDavid Vrabel  *
167a4cdb556SDavid Vrabel  * The copy is split into @count @segments, each of which can copy
168a4cdb556SDavid Vrabel  * to/from one grant reference.
169a4cdb556SDavid Vrabel  *
170a4cdb556SDavid Vrabel  * Each segment is similar to struct gnttab_copy in the hypervisor ABI
171a4cdb556SDavid Vrabel  * except the local buffer is specified using a virtual address
172a4cdb556SDavid Vrabel  * (instead of a GFN and offset).
173a4cdb556SDavid Vrabel  *
174a4cdb556SDavid Vrabel  * The local buffer may cross a Xen page boundary -- the driver will
175a4cdb556SDavid Vrabel  * split segments into multiple ops if required.
176a4cdb556SDavid Vrabel  *
177a4cdb556SDavid Vrabel  * Returns 0 if all segments have been processed and @status in each
178a4cdb556SDavid Vrabel  * segment is valid.  Note that one or more segments may have failed
179a4cdb556SDavid Vrabel  * (status != GNTST_okay).
180a4cdb556SDavid Vrabel  *
181a4cdb556SDavid Vrabel  * If the driver had to split a segment into two or more ops, @status
182a4cdb556SDavid Vrabel  * includes the status of the first failed op for that segment (or
183a4cdb556SDavid Vrabel  * GNTST_okay if all ops were successful).
184a4cdb556SDavid Vrabel  *
185a4cdb556SDavid Vrabel  * If -1 is returned, the status of all segments is undefined.
186a4cdb556SDavid Vrabel  *
187a4cdb556SDavid Vrabel  * EINVAL: A segment has local buffers for both source and
188a4cdb556SDavid Vrabel  *         destination.
189a4cdb556SDavid Vrabel  * EINVAL: A segment crosses the boundary of a foreign page.
190a4cdb556SDavid Vrabel  * EFAULT: A segment's local buffer is not accessible.
191a4cdb556SDavid Vrabel  */
192a4cdb556SDavid Vrabel #define IOCTL_GNTDEV_GRANT_COPY \
193a4cdb556SDavid Vrabel 	_IOC(_IOC_NONE, 'G', 8, sizeof(struct ioctl_gntdev_grant_copy))
194a4cdb556SDavid Vrabel struct ioctl_gntdev_grant_copy {
195a4cdb556SDavid Vrabel 	unsigned int count;
196a4cdb556SDavid Vrabel 	struct gntdev_grant_copy_segment __user *segments;
197a4cdb556SDavid Vrabel };
198a4cdb556SDavid Vrabel 
199564eb714SDavid Vrabel /* Clear (set to zero) the byte specified by index */
200564eb714SDavid Vrabel #define UNMAP_NOTIFY_CLEAR_BYTE 0x1
201564eb714SDavid Vrabel /* Send an interrupt on the indicated event channel */
202564eb714SDavid Vrabel #define UNMAP_NOTIFY_SEND_EVENT 0x2
203564eb714SDavid Vrabel 
204975ef7ffSOleksandr Andrushchenko /*
205975ef7ffSOleksandr Andrushchenko  * Flags to be used while requesting memory mapping's backing storage
206975ef7ffSOleksandr Andrushchenko  * to be allocated with DMA API.
207975ef7ffSOleksandr Andrushchenko  */
208975ef7ffSOleksandr Andrushchenko 
209975ef7ffSOleksandr Andrushchenko /*
210975ef7ffSOleksandr Andrushchenko  * The buffer is backed with memory allocated with dma_alloc_wc.
211975ef7ffSOleksandr Andrushchenko  */
212975ef7ffSOleksandr Andrushchenko #define GNTDEV_DMA_FLAG_WC		(1 << 0)
213975ef7ffSOleksandr Andrushchenko 
214975ef7ffSOleksandr Andrushchenko /*
215975ef7ffSOleksandr Andrushchenko  * The buffer is backed with memory allocated with dma_alloc_coherent.
216975ef7ffSOleksandr Andrushchenko  */
217975ef7ffSOleksandr Andrushchenko #define GNTDEV_DMA_FLAG_COHERENT	(1 << 1)
218975ef7ffSOleksandr Andrushchenko 
219*932d6562SOleksandr Andrushchenko /*
220*932d6562SOleksandr Andrushchenko  * Create a dma-buf [1] from grant references @refs of count @count provided
221*932d6562SOleksandr Andrushchenko  * by the foreign domain @domid with flags @flags.
222*932d6562SOleksandr Andrushchenko  *
223*932d6562SOleksandr Andrushchenko  * By default dma-buf is backed by system memory pages, but by providing
224*932d6562SOleksandr Andrushchenko  * one of the GNTDEV_DMA_FLAG_XXX flags it can also be created as
225*932d6562SOleksandr Andrushchenko  * a DMA write-combine or coherent buffer, e.g. allocated with dma_alloc_wc/
226*932d6562SOleksandr Andrushchenko  * dma_alloc_coherent.
227*932d6562SOleksandr Andrushchenko  *
228*932d6562SOleksandr Andrushchenko  * Returns 0 if dma-buf was successfully created and the corresponding
229*932d6562SOleksandr Andrushchenko  * dma-buf's file descriptor is returned in @fd.
230*932d6562SOleksandr Andrushchenko  *
231*932d6562SOleksandr Andrushchenko  * [1] Documentation/driver-api/dma-buf.rst
232*932d6562SOleksandr Andrushchenko  */
233*932d6562SOleksandr Andrushchenko 
234*932d6562SOleksandr Andrushchenko #define IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS \
235*932d6562SOleksandr Andrushchenko 	_IOC(_IOC_NONE, 'G', 9, \
236*932d6562SOleksandr Andrushchenko 	     sizeof(struct ioctl_gntdev_dmabuf_exp_from_refs))
237*932d6562SOleksandr Andrushchenko struct ioctl_gntdev_dmabuf_exp_from_refs {
238*932d6562SOleksandr Andrushchenko 	/* IN parameters. */
239*932d6562SOleksandr Andrushchenko 	/* Specific options for this dma-buf: see GNTDEV_DMA_FLAG_XXX. */
240*932d6562SOleksandr Andrushchenko 	__u32 flags;
241*932d6562SOleksandr Andrushchenko 	/* Number of grant references in @refs array. */
242*932d6562SOleksandr Andrushchenko 	__u32 count;
243*932d6562SOleksandr Andrushchenko 	/* OUT parameters. */
244*932d6562SOleksandr Andrushchenko 	/* File descriptor of the dma-buf. */
245*932d6562SOleksandr Andrushchenko 	__u32 fd;
246*932d6562SOleksandr Andrushchenko 	/* The domain ID of the grant references to be mapped. */
247*932d6562SOleksandr Andrushchenko 	__u32 domid;
248*932d6562SOleksandr Andrushchenko 	/* Variable IN parameter. */
249*932d6562SOleksandr Andrushchenko 	/* Array of grant references of size @count. */
250*932d6562SOleksandr Andrushchenko 	__u32 refs[1];
251*932d6562SOleksandr Andrushchenko };
252*932d6562SOleksandr Andrushchenko 
253*932d6562SOleksandr Andrushchenko /*
254*932d6562SOleksandr Andrushchenko  * This will block until the dma-buf with the file descriptor @fd is
255*932d6562SOleksandr Andrushchenko  * released. This is only valid for buffers created with
256*932d6562SOleksandr Andrushchenko  * IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS.
257*932d6562SOleksandr Andrushchenko  *
258*932d6562SOleksandr Andrushchenko  * If within @wait_to_ms milliseconds the buffer is not released
259*932d6562SOleksandr Andrushchenko  * then -ETIMEDOUT error is returned.
260*932d6562SOleksandr Andrushchenko  * If the buffer with the file descriptor @fd does not exist or has already
261*932d6562SOleksandr Andrushchenko  * been released, then -ENOENT is returned. For valid file descriptors
262*932d6562SOleksandr Andrushchenko  * this must not be treated as error.
263*932d6562SOleksandr Andrushchenko  */
264*932d6562SOleksandr Andrushchenko #define IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED \
265*932d6562SOleksandr Andrushchenko 	_IOC(_IOC_NONE, 'G', 10, \
266*932d6562SOleksandr Andrushchenko 	     sizeof(struct ioctl_gntdev_dmabuf_exp_wait_released))
267*932d6562SOleksandr Andrushchenko struct ioctl_gntdev_dmabuf_exp_wait_released {
268*932d6562SOleksandr Andrushchenko 	/* IN parameters */
269*932d6562SOleksandr Andrushchenko 	__u32 fd;
270*932d6562SOleksandr Andrushchenko 	__u32 wait_to_ms;
271*932d6562SOleksandr Andrushchenko };
272*932d6562SOleksandr Andrushchenko 
273*932d6562SOleksandr Andrushchenko /*
274*932d6562SOleksandr Andrushchenko  * Import a dma-buf with file descriptor @fd and export granted references
275*932d6562SOleksandr Andrushchenko  * to the pages of that dma-buf into array @refs of size @count.
276*932d6562SOleksandr Andrushchenko  */
277*932d6562SOleksandr Andrushchenko #define IOCTL_GNTDEV_DMABUF_IMP_TO_REFS \
278*932d6562SOleksandr Andrushchenko 	_IOC(_IOC_NONE, 'G', 11, \
279*932d6562SOleksandr Andrushchenko 	     sizeof(struct ioctl_gntdev_dmabuf_imp_to_refs))
280*932d6562SOleksandr Andrushchenko struct ioctl_gntdev_dmabuf_imp_to_refs {
281*932d6562SOleksandr Andrushchenko 	/* IN parameters. */
282*932d6562SOleksandr Andrushchenko 	/* File descriptor of the dma-buf. */
283*932d6562SOleksandr Andrushchenko 	__u32 fd;
284*932d6562SOleksandr Andrushchenko 	/* Number of grant references in @refs array. */
285*932d6562SOleksandr Andrushchenko 	__u32 count;
286*932d6562SOleksandr Andrushchenko 	/* The domain ID for which references to be granted. */
287*932d6562SOleksandr Andrushchenko 	__u32 domid;
288*932d6562SOleksandr Andrushchenko 	/* Reserved - must be zero. */
289*932d6562SOleksandr Andrushchenko 	__u32 reserved;
290*932d6562SOleksandr Andrushchenko 	/* OUT parameters. */
291*932d6562SOleksandr Andrushchenko 	/* Array of grant references of size @count. */
292*932d6562SOleksandr Andrushchenko 	__u32 refs[1];
293*932d6562SOleksandr Andrushchenko };
294*932d6562SOleksandr Andrushchenko 
295*932d6562SOleksandr Andrushchenko /*
296*932d6562SOleksandr Andrushchenko  * This will close all references to the imported buffer with file descriptor
297*932d6562SOleksandr Andrushchenko  * @fd, so it can be released by the owner. This is only valid for buffers
298*932d6562SOleksandr Andrushchenko  * created with IOCTL_GNTDEV_DMABUF_IMP_TO_REFS.
299*932d6562SOleksandr Andrushchenko  */
300*932d6562SOleksandr Andrushchenko #define IOCTL_GNTDEV_DMABUF_IMP_RELEASE \
301*932d6562SOleksandr Andrushchenko 	_IOC(_IOC_NONE, 'G', 12, \
302*932d6562SOleksandr Andrushchenko 	     sizeof(struct ioctl_gntdev_dmabuf_imp_release))
303*932d6562SOleksandr Andrushchenko struct ioctl_gntdev_dmabuf_imp_release {
304*932d6562SOleksandr Andrushchenko 	/* IN parameters */
305*932d6562SOleksandr Andrushchenko 	__u32 fd;
306*932d6562SOleksandr Andrushchenko 	__u32 reserved;
307*932d6562SOleksandr Andrushchenko };
308*932d6562SOleksandr Andrushchenko 
309564eb714SDavid Vrabel #endif /* __LINUX_PUBLIC_GNTDEV_H__ */
310