/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR MIT) */1/******************************************************************************2* gntdev.h3*4* Interface to /dev/xen/gntdev.5*6* Copyright (c) 2007, D G Murray7* Copyright (c) 2018, Oleksandr Andrushchenko, EPAM Systems Inc.8*9* This program is free software; you can redistribute it and/or10* modify it under the terms of the GNU General Public License version 211* as published by the Free Software Foundation; or, when distributed12* separately from the Linux kernel or incorporated into other13* software packages, subject to the following license:14*15* Permission is hereby granted, free of charge, to any person obtaining a copy16* of this source file (the "Software"), to deal in the Software without17* 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 to20* the following conditions:21*22* The above copyright notice and this permission notice shall be included in23* all copies or substantial portions of the Software.24*25* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR26* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,27* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE28* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER29* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING30* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS31* IN THE SOFTWARE.32*/3334#ifndef __LINUX_PUBLIC_GNTDEV_H__35#define __LINUX_PUBLIC_GNTDEV_H__3637#include <linux/types.h>3839struct ioctl_gntdev_grant_ref {40/* The domain ID of the grant to be mapped. */41__u32 domid;42/* The grant reference of the grant to be mapped. */43__u32 ref;44};4546/*47* Inserts the grant references into the mapping table of an instance48* of gntdev. N.B. This does not perform the mapping, which is deferred49* until mmap() is called with @index as the offset. @index should be50* considered opaque to userspace, with one exception: if no grant51* references have ever been inserted into the mapping table of this52* instance, @index will be set to 0. This is necessary to use gntdev53* with userspace APIs that expect a file descriptor that can be54* mmap()'d at offset 0, such as Wayland. If @count is set to 0, this55* ioctl will fail.56*/57#define IOCTL_GNTDEV_MAP_GRANT_REF \58_IOC(_IOC_NONE, 'G', 0, sizeof(struct ioctl_gntdev_map_grant_ref))59struct ioctl_gntdev_map_grant_ref {60/* IN parameters */61/* The number of grants to be mapped. */62__u32 count;63__u32 pad;64/* OUT parameters */65/* The offset to be used on a subsequent call to mmap(). */66__u64 index;67/* Variable IN parameter. */68/* Array of grant references, of size @count. */69struct ioctl_gntdev_grant_ref refs[1];70};7172/*73* Removes the grant references from the mapping table of an instance of74* gntdev. N.B. munmap() must be called on the relevant virtual address(es)75* before this ioctl is called, or an error will result.76*/77#define IOCTL_GNTDEV_UNMAP_GRANT_REF \78_IOC(_IOC_NONE, 'G', 1, sizeof(struct ioctl_gntdev_unmap_grant_ref))79struct ioctl_gntdev_unmap_grant_ref {80/* IN parameters */81/* The offset was returned by the corresponding map operation. */82__u64 index;83/* The number of pages to be unmapped. */84__u32 count;85__u32 pad;86};8788/*89* Returns the offset in the driver's address space that corresponds90* to @vaddr. This can be used to perform a munmap(), followed by an91* UNMAP_GRANT_REF ioctl, where no state about the offset is retained by92* the caller. The number of pages that were allocated at the same time as93* @vaddr is returned in @count.94*95* N.B. Where more than one page has been mapped into a contiguous range, the96* supplied @vaddr must correspond to the start of the range; otherwise97* an error will result. It is only possible to munmap() the entire98* contiguously-allocated range at once, and not any subrange thereof.99*/100#define IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR \101_IOC(_IOC_NONE, 'G', 2, sizeof(struct ioctl_gntdev_get_offset_for_vaddr))102struct ioctl_gntdev_get_offset_for_vaddr {103/* IN parameters */104/* The virtual address of the first mapped page in a range. */105__u64 vaddr;106/* OUT parameters */107/* The offset that was used in the initial mmap() operation. */108__u64 offset;109/* The number of pages mapped in the VM area that begins at @vaddr. */110__u32 count;111__u32 pad;112};113114/*115* Sets the maximum number of grants that may mapped at once by this gntdev116* instance.117*118* N.B. This must be called before any other ioctl is performed on the device.119*/120#define IOCTL_GNTDEV_SET_MAX_GRANTS \121_IOC(_IOC_NONE, 'G', 3, sizeof(struct ioctl_gntdev_set_max_grants))122struct ioctl_gntdev_set_max_grants {123/* IN parameter */124/* The maximum number of grants that may be mapped at once. */125__u32 count;126};127128/*129* Sets up an unmap notification within the page, so that the other side can do130* cleanup if this side crashes. Required to implement cross-domain robust131* mutexes or close notification on communication channels.132*133* Each mapped page only supports one notification; multiple calls referring to134* the same page overwrite the previous notification. You must clear the135* notification prior to the IOCTL_GNTALLOC_DEALLOC_GREF if you do not want it136* to occur.137*/138#define IOCTL_GNTDEV_SET_UNMAP_NOTIFY \139_IOC(_IOC_NONE, 'G', 7, sizeof(struct ioctl_gntdev_unmap_notify))140struct ioctl_gntdev_unmap_notify {141/* IN parameters */142/* Offset in the file descriptor for a byte within the page (same as143* used in mmap). If using UNMAP_NOTIFY_CLEAR_BYTE, this is the byte to144* be cleared. Otherwise, it can be any byte in the page whose145* notification we are adjusting.146*/147__u64 index;148/* Action(s) to take on unmap */149__u32 action;150/* Event channel to notify */151__u32 event_channel_port;152};153154struct gntdev_grant_copy_segment {155union {156void __user *virt;157struct {158grant_ref_t ref;159__u16 offset;160domid_t domid;161} foreign;162} source, dest;163__u16 len;164165__u16 flags; /* GNTCOPY_* */166__s16 status; /* GNTST_* */167};168169/*170* Copy between grant references and local buffers.171*172* The copy is split into @count @segments, each of which can copy173* to/from one grant reference.174*175* Each segment is similar to struct gnttab_copy in the hypervisor ABI176* except the local buffer is specified using a virtual address177* (instead of a GFN and offset).178*179* The local buffer may cross a Xen page boundary -- the driver will180* split segments into multiple ops if required.181*182* Returns 0 if all segments have been processed and @status in each183* segment is valid. Note that one or more segments may have failed184* (status != GNTST_okay).185*186* If the driver had to split a segment into two or more ops, @status187* includes the status of the first failed op for that segment (or188* GNTST_okay if all ops were successful).189*190* If -1 is returned, the status of all segments is undefined.191*192* EINVAL: A segment has local buffers for both source and193* destination.194* EINVAL: A segment crosses the boundary of a foreign page.195* EFAULT: A segment's local buffer is not accessible.196*/197#define IOCTL_GNTDEV_GRANT_COPY \198_IOC(_IOC_NONE, 'G', 8, sizeof(struct ioctl_gntdev_grant_copy))199struct ioctl_gntdev_grant_copy {200unsigned int count;201struct gntdev_grant_copy_segment __user *segments;202};203204/* Clear (set to zero) the byte specified by index */205#define UNMAP_NOTIFY_CLEAR_BYTE 0x1206/* Send an interrupt on the indicated event channel */207#define UNMAP_NOTIFY_SEND_EVENT 0x2208209/*210* Flags to be used while requesting memory mapping's backing storage211* to be allocated with DMA API.212*/213214/*215* The buffer is backed with memory allocated with dma_alloc_wc.216*/217#define GNTDEV_DMA_FLAG_WC (1 << 0)218219/*220* The buffer is backed with memory allocated with dma_alloc_coherent.221*/222#define GNTDEV_DMA_FLAG_COHERENT (1 << 1)223224/*225* Create a dma-buf [1] from grant references @refs of count @count provided226* by the foreign domain @domid with flags @flags.227*228* By default dma-buf is backed by system memory pages, but by providing229* one of the GNTDEV_DMA_FLAG_XXX flags it can also be created as230* a DMA write-combine or coherent buffer, e.g. allocated with dma_alloc_wc/231* dma_alloc_coherent.232*233* Returns 0 if dma-buf was successfully created and the corresponding234* dma-buf's file descriptor is returned in @fd.235*236* [1] Documentation/driver-api/dma-buf.rst237*/238239#define IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS \240_IOC(_IOC_NONE, 'G', 9, \241sizeof(struct ioctl_gntdev_dmabuf_exp_from_refs))242struct ioctl_gntdev_dmabuf_exp_from_refs {243/* IN parameters. */244/* Specific options for this dma-buf: see GNTDEV_DMA_FLAG_XXX. */245__u32 flags;246/* Number of grant references in @refs array. */247__u32 count;248/* OUT parameters. */249/* File descriptor of the dma-buf. */250__u32 fd;251/* The domain ID of the grant references to be mapped. */252__u32 domid;253/* Variable IN parameter. */254/* Array of grant references of size @count. */255__u32 refs[1];256};257258/*259* This will block until the dma-buf with the file descriptor @fd is260* released. This is only valid for buffers created with261* IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS.262*263* If within @wait_to_ms milliseconds the buffer is not released264* then -ETIMEDOUT error is returned.265* If the buffer with the file descriptor @fd does not exist or has already266* been released, then -ENOENT is returned. For valid file descriptors267* this must not be treated as error.268*/269#define IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED \270_IOC(_IOC_NONE, 'G', 10, \271sizeof(struct ioctl_gntdev_dmabuf_exp_wait_released))272struct ioctl_gntdev_dmabuf_exp_wait_released {273/* IN parameters */274__u32 fd;275__u32 wait_to_ms;276};277278/*279* Import a dma-buf with file descriptor @fd and export granted references280* to the pages of that dma-buf into array @refs of size @count.281*/282#define IOCTL_GNTDEV_DMABUF_IMP_TO_REFS \283_IOC(_IOC_NONE, 'G', 11, \284sizeof(struct ioctl_gntdev_dmabuf_imp_to_refs))285struct ioctl_gntdev_dmabuf_imp_to_refs {286/* IN parameters. */287/* File descriptor of the dma-buf. */288__u32 fd;289/* Number of grant references in @refs array. */290__u32 count;291/* The domain ID for which references to be granted. */292__u32 domid;293/* Reserved - must be zero. */294__u32 reserved;295/* OUT parameters. */296/* Array of grant references of size @count. */297__u32 refs[1];298};299300/*301* This will close all references to the imported buffer with file descriptor302* @fd, so it can be released by the owner. This is only valid for buffers303* created with IOCTL_GNTDEV_DMABUF_IMP_TO_REFS.304*/305#define IOCTL_GNTDEV_DMABUF_IMP_RELEASE \306_IOC(_IOC_NONE, 'G', 12, \307sizeof(struct ioctl_gntdev_dmabuf_imp_release))308struct ioctl_gntdev_dmabuf_imp_release {309/* IN parameters */310__u32 fd;311__u32 reserved;312};313314#endif /* __LINUX_PUBLIC_GNTDEV_H__ */315316317