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