162306a36Sopenharmony_ci/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR MIT) */ 262306a36Sopenharmony_ci/****************************************************************************** 362306a36Sopenharmony_ci * gntdev.h 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Interface to /dev/xen/gntdev. 662306a36Sopenharmony_ci * 762306a36Sopenharmony_ci * Copyright (c) 2007, D G Murray 862306a36Sopenharmony_ci * Copyright (c) 2018, Oleksandr Andrushchenko, EPAM Systems Inc. 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * This program is free software; you can redistribute it and/or 1162306a36Sopenharmony_ci * modify it under the terms of the GNU General Public License version 2 1262306a36Sopenharmony_ci * as published by the Free Software Foundation; or, when distributed 1362306a36Sopenharmony_ci * separately from the Linux kernel or incorporated into other 1462306a36Sopenharmony_ci * software packages, subject to the following license: 1562306a36Sopenharmony_ci * 1662306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy 1762306a36Sopenharmony_ci * of this source file (the "Software"), to deal in the Software without 1862306a36Sopenharmony_ci * restriction, including without limitation the rights to use, copy, modify, 1962306a36Sopenharmony_ci * merge, publish, distribute, sublicense, and/or sell copies of the Software, 2062306a36Sopenharmony_ci * and to permit persons to whom the Software is furnished to do so, subject to 2162306a36Sopenharmony_ci * the following conditions: 2262306a36Sopenharmony_ci * 2362306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in 2462306a36Sopenharmony_ci * all copies or substantial portions of the Software. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2762306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2862306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2962306a36Sopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 3062306a36Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 3162306a36Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 3262306a36Sopenharmony_ci * IN THE SOFTWARE. 3362306a36Sopenharmony_ci */ 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_ci#ifndef __LINUX_PUBLIC_GNTDEV_H__ 3662306a36Sopenharmony_ci#define __LINUX_PUBLIC_GNTDEV_H__ 3762306a36Sopenharmony_ci 3862306a36Sopenharmony_ci#include <linux/types.h> 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_cistruct ioctl_gntdev_grant_ref { 4162306a36Sopenharmony_ci /* The domain ID of the grant to be mapped. */ 4262306a36Sopenharmony_ci __u32 domid; 4362306a36Sopenharmony_ci /* The grant reference of the grant to be mapped. */ 4462306a36Sopenharmony_ci __u32 ref; 4562306a36Sopenharmony_ci}; 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci/* 4862306a36Sopenharmony_ci * Inserts the grant references into the mapping table of an instance 4962306a36Sopenharmony_ci * of gntdev. N.B. This does not perform the mapping, which is deferred 5062306a36Sopenharmony_ci * until mmap() is called with @index as the offset. @index should be 5162306a36Sopenharmony_ci * considered opaque to userspace, with one exception: if no grant 5262306a36Sopenharmony_ci * references have ever been inserted into the mapping table of this 5362306a36Sopenharmony_ci * instance, @index will be set to 0. This is necessary to use gntdev 5462306a36Sopenharmony_ci * with userspace APIs that expect a file descriptor that can be 5562306a36Sopenharmony_ci * mmap()'d at offset 0, such as Wayland. If @count is set to 0, this 5662306a36Sopenharmony_ci * ioctl will fail. 5762306a36Sopenharmony_ci */ 5862306a36Sopenharmony_ci#define IOCTL_GNTDEV_MAP_GRANT_REF \ 5962306a36Sopenharmony_ci_IOC(_IOC_NONE, 'G', 0, sizeof(struct ioctl_gntdev_map_grant_ref)) 6062306a36Sopenharmony_cistruct ioctl_gntdev_map_grant_ref { 6162306a36Sopenharmony_ci /* IN parameters */ 6262306a36Sopenharmony_ci /* The number of grants to be mapped. */ 6362306a36Sopenharmony_ci __u32 count; 6462306a36Sopenharmony_ci __u32 pad; 6562306a36Sopenharmony_ci /* OUT parameters */ 6662306a36Sopenharmony_ci /* The offset to be used on a subsequent call to mmap(). */ 6762306a36Sopenharmony_ci __u64 index; 6862306a36Sopenharmony_ci /* Variable IN parameter. */ 6962306a36Sopenharmony_ci /* Array of grant references, of size @count. */ 7062306a36Sopenharmony_ci struct ioctl_gntdev_grant_ref refs[1]; 7162306a36Sopenharmony_ci}; 7262306a36Sopenharmony_ci 7362306a36Sopenharmony_ci/* 7462306a36Sopenharmony_ci * Removes the grant references from the mapping table of an instance of 7562306a36Sopenharmony_ci * gntdev. N.B. munmap() must be called on the relevant virtual address(es) 7662306a36Sopenharmony_ci * before this ioctl is called, or an error will result. 7762306a36Sopenharmony_ci */ 7862306a36Sopenharmony_ci#define IOCTL_GNTDEV_UNMAP_GRANT_REF \ 7962306a36Sopenharmony_ci_IOC(_IOC_NONE, 'G', 1, sizeof(struct ioctl_gntdev_unmap_grant_ref)) 8062306a36Sopenharmony_cistruct ioctl_gntdev_unmap_grant_ref { 8162306a36Sopenharmony_ci /* IN parameters */ 8262306a36Sopenharmony_ci /* The offset was returned by the corresponding map operation. */ 8362306a36Sopenharmony_ci __u64 index; 8462306a36Sopenharmony_ci /* The number of pages to be unmapped. */ 8562306a36Sopenharmony_ci __u32 count; 8662306a36Sopenharmony_ci __u32 pad; 8762306a36Sopenharmony_ci}; 8862306a36Sopenharmony_ci 8962306a36Sopenharmony_ci/* 9062306a36Sopenharmony_ci * Returns the offset in the driver's address space that corresponds 9162306a36Sopenharmony_ci * to @vaddr. This can be used to perform a munmap(), followed by an 9262306a36Sopenharmony_ci * UNMAP_GRANT_REF ioctl, where no state about the offset is retained by 9362306a36Sopenharmony_ci * the caller. The number of pages that were allocated at the same time as 9462306a36Sopenharmony_ci * @vaddr is returned in @count. 9562306a36Sopenharmony_ci * 9662306a36Sopenharmony_ci * N.B. Where more than one page has been mapped into a contiguous range, the 9762306a36Sopenharmony_ci * supplied @vaddr must correspond to the start of the range; otherwise 9862306a36Sopenharmony_ci * an error will result. It is only possible to munmap() the entire 9962306a36Sopenharmony_ci * contiguously-allocated range at once, and not any subrange thereof. 10062306a36Sopenharmony_ci */ 10162306a36Sopenharmony_ci#define IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR \ 10262306a36Sopenharmony_ci_IOC(_IOC_NONE, 'G', 2, sizeof(struct ioctl_gntdev_get_offset_for_vaddr)) 10362306a36Sopenharmony_cistruct ioctl_gntdev_get_offset_for_vaddr { 10462306a36Sopenharmony_ci /* IN parameters */ 10562306a36Sopenharmony_ci /* The virtual address of the first mapped page in a range. */ 10662306a36Sopenharmony_ci __u64 vaddr; 10762306a36Sopenharmony_ci /* OUT parameters */ 10862306a36Sopenharmony_ci /* The offset that was used in the initial mmap() operation. */ 10962306a36Sopenharmony_ci __u64 offset; 11062306a36Sopenharmony_ci /* The number of pages mapped in the VM area that begins at @vaddr. */ 11162306a36Sopenharmony_ci __u32 count; 11262306a36Sopenharmony_ci __u32 pad; 11362306a36Sopenharmony_ci}; 11462306a36Sopenharmony_ci 11562306a36Sopenharmony_ci/* 11662306a36Sopenharmony_ci * Sets the maximum number of grants that may mapped at once by this gntdev 11762306a36Sopenharmony_ci * instance. 11862306a36Sopenharmony_ci * 11962306a36Sopenharmony_ci * N.B. This must be called before any other ioctl is performed on the device. 12062306a36Sopenharmony_ci */ 12162306a36Sopenharmony_ci#define IOCTL_GNTDEV_SET_MAX_GRANTS \ 12262306a36Sopenharmony_ci_IOC(_IOC_NONE, 'G', 3, sizeof(struct ioctl_gntdev_set_max_grants)) 12362306a36Sopenharmony_cistruct ioctl_gntdev_set_max_grants { 12462306a36Sopenharmony_ci /* IN parameter */ 12562306a36Sopenharmony_ci /* The maximum number of grants that may be mapped at once. */ 12662306a36Sopenharmony_ci __u32 count; 12762306a36Sopenharmony_ci}; 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci/* 13062306a36Sopenharmony_ci * Sets up an unmap notification within the page, so that the other side can do 13162306a36Sopenharmony_ci * cleanup if this side crashes. Required to implement cross-domain robust 13262306a36Sopenharmony_ci * mutexes or close notification on communication channels. 13362306a36Sopenharmony_ci * 13462306a36Sopenharmony_ci * Each mapped page only supports one notification; multiple calls referring to 13562306a36Sopenharmony_ci * the same page overwrite the previous notification. You must clear the 13662306a36Sopenharmony_ci * notification prior to the IOCTL_GNTALLOC_DEALLOC_GREF if you do not want it 13762306a36Sopenharmony_ci * to occur. 13862306a36Sopenharmony_ci */ 13962306a36Sopenharmony_ci#define IOCTL_GNTDEV_SET_UNMAP_NOTIFY \ 14062306a36Sopenharmony_ci_IOC(_IOC_NONE, 'G', 7, sizeof(struct ioctl_gntdev_unmap_notify)) 14162306a36Sopenharmony_cistruct ioctl_gntdev_unmap_notify { 14262306a36Sopenharmony_ci /* IN parameters */ 14362306a36Sopenharmony_ci /* Offset in the file descriptor for a byte within the page (same as 14462306a36Sopenharmony_ci * used in mmap). If using UNMAP_NOTIFY_CLEAR_BYTE, this is the byte to 14562306a36Sopenharmony_ci * be cleared. Otherwise, it can be any byte in the page whose 14662306a36Sopenharmony_ci * notification we are adjusting. 14762306a36Sopenharmony_ci */ 14862306a36Sopenharmony_ci __u64 index; 14962306a36Sopenharmony_ci /* Action(s) to take on unmap */ 15062306a36Sopenharmony_ci __u32 action; 15162306a36Sopenharmony_ci /* Event channel to notify */ 15262306a36Sopenharmony_ci __u32 event_channel_port; 15362306a36Sopenharmony_ci}; 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_cistruct gntdev_grant_copy_segment { 15662306a36Sopenharmony_ci union { 15762306a36Sopenharmony_ci void __user *virt; 15862306a36Sopenharmony_ci struct { 15962306a36Sopenharmony_ci grant_ref_t ref; 16062306a36Sopenharmony_ci __u16 offset; 16162306a36Sopenharmony_ci domid_t domid; 16262306a36Sopenharmony_ci } foreign; 16362306a36Sopenharmony_ci } source, dest; 16462306a36Sopenharmony_ci __u16 len; 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci __u16 flags; /* GNTCOPY_* */ 16762306a36Sopenharmony_ci __s16 status; /* GNTST_* */ 16862306a36Sopenharmony_ci}; 16962306a36Sopenharmony_ci 17062306a36Sopenharmony_ci/* 17162306a36Sopenharmony_ci * Copy between grant references and local buffers. 17262306a36Sopenharmony_ci * 17362306a36Sopenharmony_ci * The copy is split into @count @segments, each of which can copy 17462306a36Sopenharmony_ci * to/from one grant reference. 17562306a36Sopenharmony_ci * 17662306a36Sopenharmony_ci * Each segment is similar to struct gnttab_copy in the hypervisor ABI 17762306a36Sopenharmony_ci * except the local buffer is specified using a virtual address 17862306a36Sopenharmony_ci * (instead of a GFN and offset). 17962306a36Sopenharmony_ci * 18062306a36Sopenharmony_ci * The local buffer may cross a Xen page boundary -- the driver will 18162306a36Sopenharmony_ci * split segments into multiple ops if required. 18262306a36Sopenharmony_ci * 18362306a36Sopenharmony_ci * Returns 0 if all segments have been processed and @status in each 18462306a36Sopenharmony_ci * segment is valid. Note that one or more segments may have failed 18562306a36Sopenharmony_ci * (status != GNTST_okay). 18662306a36Sopenharmony_ci * 18762306a36Sopenharmony_ci * If the driver had to split a segment into two or more ops, @status 18862306a36Sopenharmony_ci * includes the status of the first failed op for that segment (or 18962306a36Sopenharmony_ci * GNTST_okay if all ops were successful). 19062306a36Sopenharmony_ci * 19162306a36Sopenharmony_ci * If -1 is returned, the status of all segments is undefined. 19262306a36Sopenharmony_ci * 19362306a36Sopenharmony_ci * EINVAL: A segment has local buffers for both source and 19462306a36Sopenharmony_ci * destination. 19562306a36Sopenharmony_ci * EINVAL: A segment crosses the boundary of a foreign page. 19662306a36Sopenharmony_ci * EFAULT: A segment's local buffer is not accessible. 19762306a36Sopenharmony_ci */ 19862306a36Sopenharmony_ci#define IOCTL_GNTDEV_GRANT_COPY \ 19962306a36Sopenharmony_ci _IOC(_IOC_NONE, 'G', 8, sizeof(struct ioctl_gntdev_grant_copy)) 20062306a36Sopenharmony_cistruct ioctl_gntdev_grant_copy { 20162306a36Sopenharmony_ci unsigned int count; 20262306a36Sopenharmony_ci struct gntdev_grant_copy_segment __user *segments; 20362306a36Sopenharmony_ci}; 20462306a36Sopenharmony_ci 20562306a36Sopenharmony_ci/* Clear (set to zero) the byte specified by index */ 20662306a36Sopenharmony_ci#define UNMAP_NOTIFY_CLEAR_BYTE 0x1 20762306a36Sopenharmony_ci/* Send an interrupt on the indicated event channel */ 20862306a36Sopenharmony_ci#define UNMAP_NOTIFY_SEND_EVENT 0x2 20962306a36Sopenharmony_ci 21062306a36Sopenharmony_ci/* 21162306a36Sopenharmony_ci * Flags to be used while requesting memory mapping's backing storage 21262306a36Sopenharmony_ci * to be allocated with DMA API. 21362306a36Sopenharmony_ci */ 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci/* 21662306a36Sopenharmony_ci * The buffer is backed with memory allocated with dma_alloc_wc. 21762306a36Sopenharmony_ci */ 21862306a36Sopenharmony_ci#define GNTDEV_DMA_FLAG_WC (1 << 0) 21962306a36Sopenharmony_ci 22062306a36Sopenharmony_ci/* 22162306a36Sopenharmony_ci * The buffer is backed with memory allocated with dma_alloc_coherent. 22262306a36Sopenharmony_ci */ 22362306a36Sopenharmony_ci#define GNTDEV_DMA_FLAG_COHERENT (1 << 1) 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci/* 22662306a36Sopenharmony_ci * Create a dma-buf [1] from grant references @refs of count @count provided 22762306a36Sopenharmony_ci * by the foreign domain @domid with flags @flags. 22862306a36Sopenharmony_ci * 22962306a36Sopenharmony_ci * By default dma-buf is backed by system memory pages, but by providing 23062306a36Sopenharmony_ci * one of the GNTDEV_DMA_FLAG_XXX flags it can also be created as 23162306a36Sopenharmony_ci * a DMA write-combine or coherent buffer, e.g. allocated with dma_alloc_wc/ 23262306a36Sopenharmony_ci * dma_alloc_coherent. 23362306a36Sopenharmony_ci * 23462306a36Sopenharmony_ci * Returns 0 if dma-buf was successfully created and the corresponding 23562306a36Sopenharmony_ci * dma-buf's file descriptor is returned in @fd. 23662306a36Sopenharmony_ci * 23762306a36Sopenharmony_ci * [1] Documentation/driver-api/dma-buf.rst 23862306a36Sopenharmony_ci */ 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci#define IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS \ 24162306a36Sopenharmony_ci _IOC(_IOC_NONE, 'G', 9, \ 24262306a36Sopenharmony_ci sizeof(struct ioctl_gntdev_dmabuf_exp_from_refs)) 24362306a36Sopenharmony_cistruct ioctl_gntdev_dmabuf_exp_from_refs { 24462306a36Sopenharmony_ci /* IN parameters. */ 24562306a36Sopenharmony_ci /* Specific options for this dma-buf: see GNTDEV_DMA_FLAG_XXX. */ 24662306a36Sopenharmony_ci __u32 flags; 24762306a36Sopenharmony_ci /* Number of grant references in @refs array. */ 24862306a36Sopenharmony_ci __u32 count; 24962306a36Sopenharmony_ci /* OUT parameters. */ 25062306a36Sopenharmony_ci /* File descriptor of the dma-buf. */ 25162306a36Sopenharmony_ci __u32 fd; 25262306a36Sopenharmony_ci /* The domain ID of the grant references to be mapped. */ 25362306a36Sopenharmony_ci __u32 domid; 25462306a36Sopenharmony_ci /* Variable IN parameter. */ 25562306a36Sopenharmony_ci /* Array of grant references of size @count. */ 25662306a36Sopenharmony_ci __u32 refs[1]; 25762306a36Sopenharmony_ci}; 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_ci/* 26062306a36Sopenharmony_ci * This will block until the dma-buf with the file descriptor @fd is 26162306a36Sopenharmony_ci * released. This is only valid for buffers created with 26262306a36Sopenharmony_ci * IOCTL_GNTDEV_DMABUF_EXP_FROM_REFS. 26362306a36Sopenharmony_ci * 26462306a36Sopenharmony_ci * If within @wait_to_ms milliseconds the buffer is not released 26562306a36Sopenharmony_ci * then -ETIMEDOUT error is returned. 26662306a36Sopenharmony_ci * If the buffer with the file descriptor @fd does not exist or has already 26762306a36Sopenharmony_ci * been released, then -ENOENT is returned. For valid file descriptors 26862306a36Sopenharmony_ci * this must not be treated as error. 26962306a36Sopenharmony_ci */ 27062306a36Sopenharmony_ci#define IOCTL_GNTDEV_DMABUF_EXP_WAIT_RELEASED \ 27162306a36Sopenharmony_ci _IOC(_IOC_NONE, 'G', 10, \ 27262306a36Sopenharmony_ci sizeof(struct ioctl_gntdev_dmabuf_exp_wait_released)) 27362306a36Sopenharmony_cistruct ioctl_gntdev_dmabuf_exp_wait_released { 27462306a36Sopenharmony_ci /* IN parameters */ 27562306a36Sopenharmony_ci __u32 fd; 27662306a36Sopenharmony_ci __u32 wait_to_ms; 27762306a36Sopenharmony_ci}; 27862306a36Sopenharmony_ci 27962306a36Sopenharmony_ci/* 28062306a36Sopenharmony_ci * Import a dma-buf with file descriptor @fd and export granted references 28162306a36Sopenharmony_ci * to the pages of that dma-buf into array @refs of size @count. 28262306a36Sopenharmony_ci */ 28362306a36Sopenharmony_ci#define IOCTL_GNTDEV_DMABUF_IMP_TO_REFS \ 28462306a36Sopenharmony_ci _IOC(_IOC_NONE, 'G', 11, \ 28562306a36Sopenharmony_ci sizeof(struct ioctl_gntdev_dmabuf_imp_to_refs)) 28662306a36Sopenharmony_cistruct ioctl_gntdev_dmabuf_imp_to_refs { 28762306a36Sopenharmony_ci /* IN parameters. */ 28862306a36Sopenharmony_ci /* File descriptor of the dma-buf. */ 28962306a36Sopenharmony_ci __u32 fd; 29062306a36Sopenharmony_ci /* Number of grant references in @refs array. */ 29162306a36Sopenharmony_ci __u32 count; 29262306a36Sopenharmony_ci /* The domain ID for which references to be granted. */ 29362306a36Sopenharmony_ci __u32 domid; 29462306a36Sopenharmony_ci /* Reserved - must be zero. */ 29562306a36Sopenharmony_ci __u32 reserved; 29662306a36Sopenharmony_ci /* OUT parameters. */ 29762306a36Sopenharmony_ci /* Array of grant references of size @count. */ 29862306a36Sopenharmony_ci __u32 refs[1]; 29962306a36Sopenharmony_ci}; 30062306a36Sopenharmony_ci 30162306a36Sopenharmony_ci/* 30262306a36Sopenharmony_ci * This will close all references to the imported buffer with file descriptor 30362306a36Sopenharmony_ci * @fd, so it can be released by the owner. This is only valid for buffers 30462306a36Sopenharmony_ci * created with IOCTL_GNTDEV_DMABUF_IMP_TO_REFS. 30562306a36Sopenharmony_ci */ 30662306a36Sopenharmony_ci#define IOCTL_GNTDEV_DMABUF_IMP_RELEASE \ 30762306a36Sopenharmony_ci _IOC(_IOC_NONE, 'G', 12, \ 30862306a36Sopenharmony_ci sizeof(struct ioctl_gntdev_dmabuf_imp_release)) 30962306a36Sopenharmony_cistruct ioctl_gntdev_dmabuf_imp_release { 31062306a36Sopenharmony_ci /* IN parameters */ 31162306a36Sopenharmony_ci __u32 fd; 31262306a36Sopenharmony_ci __u32 reserved; 31362306a36Sopenharmony_ci}; 31462306a36Sopenharmony_ci 31562306a36Sopenharmony_ci#endif /* __LINUX_PUBLIC_GNTDEV_H__ */ 316