Branch data Line data Source code
1 : : /* SPDX-License-Identifier: GPL-2.0-only */ 2 : : /* 3 : : * Header file for dma buffer sharing framework. 4 : : * 5 : : * Copyright(C) 2011 Linaro Limited. All rights reserved. 6 : : * Author: Sumit Semwal <sumit.semwal@ti.com> 7 : : * 8 : : * Many thanks to linaro-mm-sig list, and specially 9 : : * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and 10 : : * Daniel Vetter <daniel@ffwll.ch> for their support in creation and 11 : : * refining of this idea. 12 : : */ 13 : : #ifndef __DMA_BUF_H__ 14 : : #define __DMA_BUF_H__ 15 : : 16 : : #include <linux/file.h> 17 : : #include <linux/err.h> 18 : : #include <linux/scatterlist.h> 19 : : #include <linux/list.h> 20 : : #include <linux/dma-mapping.h> 21 : : #include <linux/fs.h> 22 : : #include <linux/dma-fence.h> 23 : : #include <linux/wait.h> 24 : : 25 : : struct device; 26 : : struct dma_buf; 27 : : struct dma_buf_attachment; 28 : : 29 : : /** 30 : : * struct dma_buf_ops - operations possible on struct dma_buf 31 : : * @vmap: [optional] creates a virtual mapping for the buffer into kernel 32 : : * address space. Same restrictions as for vmap and friends apply. 33 : : * @vunmap: [optional] unmaps a vmap from the buffer 34 : : */ 35 : : struct dma_buf_ops { 36 : : /** 37 : : * @cache_sgt_mapping: 38 : : * 39 : : * If true the framework will cache the first mapping made for each 40 : : * attachment. This avoids creating mappings for attachments multiple 41 : : * times. 42 : : */ 43 : : bool cache_sgt_mapping; 44 : : 45 : : /** 46 : : * @attach: 47 : : * 48 : : * This is called from dma_buf_attach() to make sure that a given 49 : : * &dma_buf_attachment.dev can access the provided &dma_buf. Exporters 50 : : * which support buffer objects in special locations like VRAM or 51 : : * device-specific carveout areas should check whether the buffer could 52 : : * be move to system memory (or directly accessed by the provided 53 : : * device), and otherwise need to fail the attach operation. 54 : : * 55 : : * The exporter should also in general check whether the current 56 : : * allocation fullfills the DMA constraints of the new device. If this 57 : : * is not the case, and the allocation cannot be moved, it should also 58 : : * fail the attach operation. 59 : : * 60 : : * Any exporter-private housekeeping data can be stored in the 61 : : * &dma_buf_attachment.priv pointer. 62 : : * 63 : : * This callback is optional. 64 : : * 65 : : * Returns: 66 : : * 67 : : * 0 on success, negative error code on failure. It might return -EBUSY 68 : : * to signal that backing storage is already allocated and incompatible 69 : : * with the requirements of requesting device. 70 : : */ 71 : : int (*attach)(struct dma_buf *, struct dma_buf_attachment *); 72 : : 73 : : /** 74 : : * @detach: 75 : : * 76 : : * This is called by dma_buf_detach() to release a &dma_buf_attachment. 77 : : * Provided so that exporters can clean up any housekeeping for an 78 : : * &dma_buf_attachment. 79 : : * 80 : : * This callback is optional. 81 : : */ 82 : : void (*detach)(struct dma_buf *, struct dma_buf_attachment *); 83 : : 84 : : /** 85 : : * @map_dma_buf: 86 : : * 87 : : * This is called by dma_buf_map_attachment() and is used to map a 88 : : * shared &dma_buf into device address space, and it is mandatory. It 89 : : * can only be called if @attach has been called successfully. This 90 : : * essentially pins the DMA buffer into place, and it cannot be moved 91 : : * any more 92 : : * 93 : : * This call may sleep, e.g. when the backing storage first needs to be 94 : : * allocated, or moved to a location suitable for all currently attached 95 : : * devices. 96 : : * 97 : : * Note that any specific buffer attributes required for this function 98 : : * should get added to device_dma_parameters accessible via 99 : : * &device.dma_params from the &dma_buf_attachment. The @attach callback 100 : : * should also check these constraints. 101 : : * 102 : : * If this is being called for the first time, the exporter can now 103 : : * choose to scan through the list of attachments for this buffer, 104 : : * collate the requirements of the attached devices, and choose an 105 : : * appropriate backing storage for the buffer. 106 : : * 107 : : * Based on enum dma_data_direction, it might be possible to have 108 : : * multiple users accessing at the same time (for reading, maybe), or 109 : : * any other kind of sharing that the exporter might wish to make 110 : : * available to buffer-users. 111 : : * 112 : : * Returns: 113 : : * 114 : : * A &sg_table scatter list of or the backing storage of the DMA buffer, 115 : : * already mapped into the device address space of the &device attached 116 : : * with the provided &dma_buf_attachment. 117 : : * 118 : : * On failure, returns a negative error value wrapped into a pointer. 119 : : * May also return -EINTR when a signal was received while being 120 : : * blocked. 121 : : */ 122 : : struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *, 123 : : enum dma_data_direction); 124 : : /** 125 : : * @unmap_dma_buf: 126 : : * 127 : : * This is called by dma_buf_unmap_attachment() and should unmap and 128 : : * release the &sg_table allocated in @map_dma_buf, and it is mandatory. 129 : : * It should also unpin the backing storage if this is the last mapping 130 : : * of the DMA buffer, it the exporter supports backing storage 131 : : * migration. 132 : : */ 133 : : void (*unmap_dma_buf)(struct dma_buf_attachment *, 134 : : struct sg_table *, 135 : : enum dma_data_direction); 136 : : 137 : : /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY 138 : : * if the call would block. 139 : : */ 140 : : 141 : : /** 142 : : * @release: 143 : : * 144 : : * Called after the last dma_buf_put to release the &dma_buf, and 145 : : * mandatory. 146 : : */ 147 : : void (*release)(struct dma_buf *); 148 : : 149 : : /** 150 : : * @begin_cpu_access: 151 : : * 152 : : * This is called from dma_buf_begin_cpu_access() and allows the 153 : : * exporter to ensure that the memory is actually available for cpu 154 : : * access - the exporter might need to allocate or swap-in and pin the 155 : : * backing storage. The exporter also needs to ensure that cpu access is 156 : : * coherent for the access direction. The direction can be used by the 157 : : * exporter to optimize the cache flushing, i.e. access with a different 158 : : * direction (read instead of write) might return stale or even bogus 159 : : * data (e.g. when the exporter needs to copy the data to temporary 160 : : * storage). 161 : : * 162 : : * This callback is optional. 163 : : * 164 : : * FIXME: This is both called through the DMA_BUF_IOCTL_SYNC command 165 : : * from userspace (where storage shouldn't be pinned to avoid handing 166 : : * de-factor mlock rights to userspace) and for the kernel-internal 167 : : * users of the various kmap interfaces, where the backing storage must 168 : : * be pinned to guarantee that the atomic kmap calls can succeed. Since 169 : : * there's no in-kernel users of the kmap interfaces yet this isn't a 170 : : * real problem. 171 : : * 172 : : * Returns: 173 : : * 174 : : * 0 on success or a negative error code on failure. This can for 175 : : * example fail when the backing storage can't be allocated. Can also 176 : : * return -ERESTARTSYS or -EINTR when the call has been interrupted and 177 : : * needs to be restarted. 178 : : */ 179 : : int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction); 180 : : 181 : : /** 182 : : * @end_cpu_access: 183 : : * 184 : : * This is called from dma_buf_end_cpu_access() when the importer is 185 : : * done accessing the CPU. The exporter can use this to flush caches and 186 : : * unpin any resources pinned in @begin_cpu_access. 187 : : * The result of any dma_buf kmap calls after end_cpu_access is 188 : : * undefined. 189 : : * 190 : : * This callback is optional. 191 : : * 192 : : * Returns: 193 : : * 194 : : * 0 on success or a negative error code on failure. Can return 195 : : * -ERESTARTSYS or -EINTR when the call has been interrupted and needs 196 : : * to be restarted. 197 : : */ 198 : : int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction); 199 : : 200 : : /** 201 : : * @mmap: 202 : : * 203 : : * This callback is used by the dma_buf_mmap() function 204 : : * 205 : : * Note that the mapping needs to be incoherent, userspace is expected 206 : : * to braket CPU access using the DMA_BUF_IOCTL_SYNC interface. 207 : : * 208 : : * Because dma-buf buffers have invariant size over their lifetime, the 209 : : * dma-buf core checks whether a vma is too large and rejects such 210 : : * mappings. The exporter hence does not need to duplicate this check. 211 : : * Drivers do not need to check this themselves. 212 : : * 213 : : * If an exporter needs to manually flush caches and hence needs to fake 214 : : * coherency for mmap support, it needs to be able to zap all the ptes 215 : : * pointing at the backing storage. Now linux mm needs a struct 216 : : * address_space associated with the struct file stored in vma->vm_file 217 : : * to do that with the function unmap_mapping_range. But the dma_buf 218 : : * framework only backs every dma_buf fd with the anon_file struct file, 219 : : * i.e. all dma_bufs share the same file. 220 : : * 221 : : * Hence exporters need to setup their own file (and address_space) 222 : : * association by setting vma->vm_file and adjusting vma->vm_pgoff in 223 : : * the dma_buf mmap callback. In the specific case of a gem driver the 224 : : * exporter could use the shmem file already provided by gem (and set 225 : : * vm_pgoff = 0). Exporters can then zap ptes by unmapping the 226 : : * corresponding range of the struct address_space associated with their 227 : : * own file. 228 : : * 229 : : * This callback is optional. 230 : : * 231 : : * Returns: 232 : : * 233 : : * 0 on success or a negative error code on failure. 234 : : */ 235 : : int (*mmap)(struct dma_buf *, struct vm_area_struct *vma); 236 : : 237 : : /** 238 : : * @map: 239 : : * 240 : : * Maps a page from the buffer into kernel address space. The page is 241 : : * specified by offset into the buffer in PAGE_SIZE units. 242 : : * 243 : : * This callback is optional. 244 : : * 245 : : * Returns: 246 : : * 247 : : * Virtual address pointer where requested page can be accessed. NULL 248 : : * on error or when this function is unimplemented by the exporter. 249 : : */ 250 : : void *(*map)(struct dma_buf *, unsigned long); 251 : : 252 : : /** 253 : : * @unmap: 254 : : * 255 : : * Unmaps a page from the buffer. Page offset and address pointer should 256 : : * be the same as the one passed to and returned by matching call to map. 257 : : * 258 : : * This callback is optional. 259 : : */ 260 : : void (*unmap)(struct dma_buf *, unsigned long, void *); 261 : : 262 : : void *(*vmap)(struct dma_buf *); 263 : : void (*vunmap)(struct dma_buf *, void *vaddr); 264 : : }; 265 : : 266 : : /** 267 : : * struct dma_buf - shared buffer object 268 : : * @size: size of the buffer 269 : : * @file: file pointer used for sharing buffers across, and for refcounting. 270 : : * @attachments: list of dma_buf_attachment that denotes all devices attached. 271 : : * @ops: dma_buf_ops associated with this buffer object. 272 : : * @lock: used internally to serialize list manipulation, attach/detach and 273 : : * vmap/unmap, and accesses to name 274 : : * @vmapping_counter: used internally to refcnt the vmaps 275 : : * @vmap_ptr: the current vmap ptr if vmapping_counter > 0 276 : : * @exp_name: name of the exporter; useful for debugging. 277 : : * @name: userspace-provided name; useful for accounting and debugging. 278 : : * @owner: pointer to exporter module; used for refcounting when exporter is a 279 : : * kernel module. 280 : : * @list_node: node for dma_buf accounting and debugging. 281 : : * @priv: exporter specific private data for this buffer object. 282 : : * @resv: reservation object linked to this dma-buf 283 : : * @poll: for userspace poll support 284 : : * @cb_excl: for userspace poll support 285 : : * @cb_shared: for userspace poll support 286 : : * 287 : : * This represents a shared buffer, created by calling dma_buf_export(). The 288 : : * userspace representation is a normal file descriptor, which can be created by 289 : : * calling dma_buf_fd(). 290 : : * 291 : : * Shared dma buffers are reference counted using dma_buf_put() and 292 : : * get_dma_buf(). 293 : : * 294 : : * Device DMA access is handled by the separate &struct dma_buf_attachment. 295 : : */ 296 : : struct dma_buf { 297 : : size_t size; 298 : : struct file *file; 299 : : struct list_head attachments; 300 : : const struct dma_buf_ops *ops; 301 : : struct mutex lock; 302 : : unsigned vmapping_counter; 303 : : void *vmap_ptr; 304 : : const char *exp_name; 305 : : const char *name; 306 : : spinlock_t name_lock; /* spinlock to protect name access */ 307 : : struct module *owner; 308 : : struct list_head list_node; 309 : : void *priv; 310 : : struct dma_resv *resv; 311 : : 312 : : /* poll support */ 313 : : wait_queue_head_t poll; 314 : : 315 : : struct dma_buf_poll_cb_t { 316 : : struct dma_fence_cb cb; 317 : : wait_queue_head_t *poll; 318 : : 319 : : __poll_t active; 320 : : } cb_excl, cb_shared; 321 : : }; 322 : : 323 : : /** 324 : : * struct dma_buf_attachment - holds device-buffer attachment data 325 : : * @dmabuf: buffer for this attachment. 326 : : * @dev: device attached to the buffer. 327 : : * @node: list of dma_buf_attachment. 328 : : * @sgt: cached mapping. 329 : : * @dir: direction of cached mapping. 330 : : * @priv: exporter specific attachment data. 331 : : * 332 : : * This structure holds the attachment information between the dma_buf buffer 333 : : * and its user device(s). The list contains one attachment struct per device 334 : : * attached to the buffer. 335 : : * 336 : : * An attachment is created by calling dma_buf_attach(), and released again by 337 : : * calling dma_buf_detach(). The DMA mapping itself needed to initiate a 338 : : * transfer is created by dma_buf_map_attachment() and freed again by calling 339 : : * dma_buf_unmap_attachment(). 340 : : */ 341 : : struct dma_buf_attachment { 342 : : struct dma_buf *dmabuf; 343 : : struct device *dev; 344 : : struct list_head node; 345 : : struct sg_table *sgt; 346 : : enum dma_data_direction dir; 347 : : void *priv; 348 : : }; 349 : : 350 : : /** 351 : : * struct dma_buf_export_info - holds information needed to export a dma_buf 352 : : * @exp_name: name of the exporter - useful for debugging. 353 : : * @owner: pointer to exporter module - used for refcounting kernel module 354 : : * @ops: Attach allocator-defined dma buf ops to the new buffer 355 : : * @size: Size of the buffer 356 : : * @flags: mode flags for the file 357 : : * @resv: reservation-object, NULL to allocate default one 358 : : * @priv: Attach private data of allocator to this buffer 359 : : * 360 : : * This structure holds the information required to export the buffer. Used 361 : : * with dma_buf_export() only. 362 : : */ 363 : : struct dma_buf_export_info { 364 : : const char *exp_name; 365 : : struct module *owner; 366 : : const struct dma_buf_ops *ops; 367 : : size_t size; 368 : : int flags; 369 : : struct dma_resv *resv; 370 : : void *priv; 371 : : }; 372 : : 373 : : /** 374 : : * DEFINE_DMA_BUF_EXPORT_INFO - helper macro for exporters 375 : : * @name: export-info name 376 : : * 377 : : * DEFINE_DMA_BUF_EXPORT_INFO macro defines the &struct dma_buf_export_info, 378 : : * zeroes it out and pre-populates exp_name in it. 379 : : */ 380 : : #define DEFINE_DMA_BUF_EXPORT_INFO(name) \ 381 : : struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \ 382 : : .owner = THIS_MODULE } 383 : : 384 : : /** 385 : : * get_dma_buf - convenience wrapper for get_file. 386 : : * @dmabuf: [in] pointer to dma_buf 387 : : * 388 : : * Increments the reference count on the dma-buf, needed in case of drivers 389 : : * that either need to create additional references to the dmabuf on the 390 : : * kernel side. For example, an exporter that needs to keep a dmabuf ptr 391 : : * so that subsequent exports don't create a new dmabuf. 392 : : */ 393 : 0 : static inline void get_dma_buf(struct dma_buf *dmabuf) 394 : : { 395 : 0 : get_file(dmabuf->file); 396 : 0 : } 397 : : 398 : : struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf, 399 : : struct device *dev); 400 : : void dma_buf_detach(struct dma_buf *dmabuf, 401 : : struct dma_buf_attachment *dmabuf_attach); 402 : : 403 : : struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info); 404 : : 405 : : int dma_buf_fd(struct dma_buf *dmabuf, int flags); 406 : : struct dma_buf *dma_buf_get(int fd); 407 : : void dma_buf_put(struct dma_buf *dmabuf); 408 : : 409 : : struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *, 410 : : enum dma_data_direction); 411 : : void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *, 412 : : enum dma_data_direction); 413 : : int dma_buf_begin_cpu_access(struct dma_buf *dma_buf, 414 : : enum dma_data_direction dir); 415 : : int dma_buf_end_cpu_access(struct dma_buf *dma_buf, 416 : : enum dma_data_direction dir); 417 : : void *dma_buf_kmap(struct dma_buf *, unsigned long); 418 : : void dma_buf_kunmap(struct dma_buf *, unsigned long, void *); 419 : : 420 : : int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *, 421 : : unsigned long); 422 : : void *dma_buf_vmap(struct dma_buf *); 423 : : void dma_buf_vunmap(struct dma_buf *, void *vaddr); 424 : : #endif /* __DMA_BUF_H__ */