1e2be04c7SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2c11e391dSDaniel Vetter /* 3c11e391dSDaniel Vetter * Framework for buffer objects that can be shared across devices/subsystems. 4c11e391dSDaniel Vetter * 5c11e391dSDaniel Vetter * Copyright(C) 2015 Intel Ltd 6c11e391dSDaniel Vetter * 7c11e391dSDaniel Vetter * This program is free software; you can redistribute it and/or modify it 8c11e391dSDaniel Vetter * under the terms of the GNU General Public License version 2 as published by 9c11e391dSDaniel Vetter * the Free Software Foundation. 10c11e391dSDaniel Vetter * 11c11e391dSDaniel Vetter * This program is distributed in the hope that it will be useful, but WITHOUT 12c11e391dSDaniel Vetter * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13c11e391dSDaniel Vetter * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14c11e391dSDaniel Vetter * more details. 15c11e391dSDaniel Vetter * 16c11e391dSDaniel Vetter * You should have received a copy of the GNU General Public License along with 17c11e391dSDaniel Vetter * this program. If not, see <http://www.gnu.org/licenses/>. 18c11e391dSDaniel Vetter */ 19c11e391dSDaniel Vetter 20c11e391dSDaniel Vetter #ifndef _DMA_BUF_UAPI_H_ 21c11e391dSDaniel Vetter #define _DMA_BUF_UAPI_H_ 22c11e391dSDaniel Vetter 23c11e391dSDaniel Vetter #include <linux/types.h> 24c11e391dSDaniel Vetter 2551f52547SJason Ekstrand /** 2651f52547SJason Ekstrand * struct dma_buf_sync - Synchronize with CPU access. 2751f52547SJason Ekstrand * 2851f52547SJason Ekstrand * When a DMA buffer is accessed from the CPU via mmap, it is not always 2951f52547SJason Ekstrand * possible to guarantee coherency between the CPU-visible map and underlying 3051f52547SJason Ekstrand * memory. To manage coherency, DMA_BUF_IOCTL_SYNC must be used to bracket 3151f52547SJason Ekstrand * any CPU access to give the kernel the chance to shuffle memory around if 3251f52547SJason Ekstrand * needed. 3351f52547SJason Ekstrand * 3451f52547SJason Ekstrand * Prior to accessing the map, the client must call DMA_BUF_IOCTL_SYNC 3551f52547SJason Ekstrand * with DMA_BUF_SYNC_START and the appropriate read/write flags. Once the 3651f52547SJason Ekstrand * access is complete, the client should call DMA_BUF_IOCTL_SYNC with 3751f52547SJason Ekstrand * DMA_BUF_SYNC_END and the same read/write flags. 3851f52547SJason Ekstrand * 3951f52547SJason Ekstrand * The synchronization provided via DMA_BUF_IOCTL_SYNC only provides cache 4051f52547SJason Ekstrand * coherency. It does not prevent other processes or devices from 4151f52547SJason Ekstrand * accessing the memory at the same time. If synchronization with a GPU or 4251f52547SJason Ekstrand * other device driver is required, it is the client's responsibility to 4351f52547SJason Ekstrand * wait for buffer to be ready for reading or writing before calling this 4451f52547SJason Ekstrand * ioctl with DMA_BUF_SYNC_START. Likewise, the client must ensure that 4551f52547SJason Ekstrand * follow-up work is not submitted to GPU or other device driver until 4651f52547SJason Ekstrand * after this ioctl has been called with DMA_BUF_SYNC_END? 4751f52547SJason Ekstrand * 4851f52547SJason Ekstrand * If the driver or API with which the client is interacting uses implicit 4951f52547SJason Ekstrand * synchronization, waiting for prior work to complete can be done via 5051f52547SJason Ekstrand * poll() on the DMA buffer file descriptor. If the driver or API requires 5151f52547SJason Ekstrand * explicit synchronization, the client may have to wait on a sync_file or 5251f52547SJason Ekstrand * other synchronization primitive outside the scope of the DMA buffer API. 5351f52547SJason Ekstrand */ 54c11e391dSDaniel Vetter struct dma_buf_sync { 5551f52547SJason Ekstrand /** 5651f52547SJason Ekstrand * @flags: Set of access flags 5751f52547SJason Ekstrand * 5851f52547SJason Ekstrand * DMA_BUF_SYNC_START: 5951f52547SJason Ekstrand * Indicates the start of a map access session. 6051f52547SJason Ekstrand * 6151f52547SJason Ekstrand * DMA_BUF_SYNC_END: 6251f52547SJason Ekstrand * Indicates the end of a map access session. 6351f52547SJason Ekstrand * 6451f52547SJason Ekstrand * DMA_BUF_SYNC_READ: 6551f52547SJason Ekstrand * Indicates that the mapped DMA buffer will be read by the 6651f52547SJason Ekstrand * client via the CPU map. 6751f52547SJason Ekstrand * 6851f52547SJason Ekstrand * DMA_BUF_SYNC_WRITE: 6951f52547SJason Ekstrand * Indicates that the mapped DMA buffer will be written by the 7051f52547SJason Ekstrand * client via the CPU map. 7151f52547SJason Ekstrand * 7251f52547SJason Ekstrand * DMA_BUF_SYNC_RW: 7351f52547SJason Ekstrand * An alias for DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE. 7451f52547SJason Ekstrand */ 75c11e391dSDaniel Vetter __u64 flags; 76c11e391dSDaniel Vetter }; 77c11e391dSDaniel Vetter 78c11e391dSDaniel Vetter #define DMA_BUF_SYNC_READ (1 << 0) 79c11e391dSDaniel Vetter #define DMA_BUF_SYNC_WRITE (2 << 0) 80c11e391dSDaniel Vetter #define DMA_BUF_SYNC_RW (DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE) 81c11e391dSDaniel Vetter #define DMA_BUF_SYNC_START (0 << 2) 82c11e391dSDaniel Vetter #define DMA_BUF_SYNC_END (1 << 2) 83c11e391dSDaniel Vetter #define DMA_BUF_SYNC_VALID_FLAGS_MASK \ 84c11e391dSDaniel Vetter (DMA_BUF_SYNC_RW | DMA_BUF_SYNC_END) 85c11e391dSDaniel Vetter 86bb2bb903SGreg Hackmann #define DMA_BUF_NAME_LEN 32 87bb2bb903SGreg Hackmann 88*20e10881SJason Ekstrand /** 89*20e10881SJason Ekstrand * struct dma_buf_export_sync_file - Get a sync_file from a dma-buf 90*20e10881SJason Ekstrand * 91*20e10881SJason Ekstrand * Userspace can perform a DMA_BUF_IOCTL_EXPORT_SYNC_FILE to retrieve the 92*20e10881SJason Ekstrand * current set of fences on a dma-buf file descriptor as a sync_file. CPU 93*20e10881SJason Ekstrand * waits via poll() or other driver-specific mechanisms typically wait on 94*20e10881SJason Ekstrand * whatever fences are on the dma-buf at the time the wait begins. This 95*20e10881SJason Ekstrand * is similar except that it takes a snapshot of the current fences on the 96*20e10881SJason Ekstrand * dma-buf for waiting later instead of waiting immediately. This is 97*20e10881SJason Ekstrand * useful for modern graphics APIs such as Vulkan which assume an explicit 98*20e10881SJason Ekstrand * synchronization model but still need to inter-operate with dma-buf. 99*20e10881SJason Ekstrand */ 100*20e10881SJason Ekstrand struct dma_buf_export_sync_file { 101*20e10881SJason Ekstrand /** 102*20e10881SJason Ekstrand * @flags: Read/write flags 103*20e10881SJason Ekstrand * 104*20e10881SJason Ekstrand * Must be DMA_BUF_SYNC_READ, DMA_BUF_SYNC_WRITE, or both. 105*20e10881SJason Ekstrand * 106*20e10881SJason Ekstrand * If DMA_BUF_SYNC_READ is set and DMA_BUF_SYNC_WRITE is not set, 107*20e10881SJason Ekstrand * the returned sync file waits on any writers of the dma-buf to 108*20e10881SJason Ekstrand * complete. Waiting on the returned sync file is equivalent to 109*20e10881SJason Ekstrand * poll() with POLLIN. 110*20e10881SJason Ekstrand * 111*20e10881SJason Ekstrand * If DMA_BUF_SYNC_WRITE is set, the returned sync file waits on 112*20e10881SJason Ekstrand * any users of the dma-buf (read or write) to complete. Waiting 113*20e10881SJason Ekstrand * on the returned sync file is equivalent to poll() with POLLOUT. 114*20e10881SJason Ekstrand * If both DMA_BUF_SYNC_WRITE and DMA_BUF_SYNC_READ are set, this 115*20e10881SJason Ekstrand * is equivalent to just DMA_BUF_SYNC_WRITE. 116*20e10881SJason Ekstrand */ 117*20e10881SJason Ekstrand __u32 flags; 118*20e10881SJason Ekstrand /** @fd: Returned sync file descriptor */ 119*20e10881SJason Ekstrand __s32 fd; 120*20e10881SJason Ekstrand }; 121*20e10881SJason Ekstrand 122c11e391dSDaniel Vetter #define DMA_BUF_BASE 'b' 123c11e391dSDaniel Vetter #define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync) 124a5bff92eSDaniel Vetter 125a5bff92eSDaniel Vetter /* 32/64bitness of this uapi was botched in android, there's no difference 126a5bff92eSDaniel Vetter * between them in actual uapi, they're just different numbers. 127a5bff92eSDaniel Vetter */ 128bb2bb903SGreg Hackmann #define DMA_BUF_SET_NAME _IOW(DMA_BUF_BASE, 1, const char *) 129a5bff92eSDaniel Vetter #define DMA_BUF_SET_NAME_A _IOW(DMA_BUF_BASE, 1, u32) 130a5bff92eSDaniel Vetter #define DMA_BUF_SET_NAME_B _IOW(DMA_BUF_BASE, 1, u64) 131*20e10881SJason Ekstrand #define DMA_BUF_IOCTL_EXPORT_SYNC_FILE _IOWR(DMA_BUF_BASE, 2, struct dma_buf_export_sync_file) 132c11e391dSDaniel Vetter 133c11e391dSDaniel Vetter #endif 134