xref: /OK3568_Linux_fs/kernel/include/linux/dma-heap.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * DMABUF Heaps Allocation Infrastructure
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011 Google, Inc.
6*4882a593Smuzhiyun  * Copyright (C) 2019 Linaro Ltd.
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #ifndef _DMA_HEAPS_H
10*4882a593Smuzhiyun #define _DMA_HEAPS_H
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include <linux/cdev.h>
13*4882a593Smuzhiyun #include <linux/types.h>
14*4882a593Smuzhiyun #include <uapi/linux/dma-heap.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun struct dma_heap;
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun /**
19*4882a593Smuzhiyun  * struct dma_heap_ops - ops to operate on a given heap
20*4882a593Smuzhiyun  * @allocate:		allocate dmabuf and return struct dma_buf ptr
21*4882a593Smuzhiyun  * @get_pool_size:	if heap maintains memory pools, get pool size in bytes
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * allocate returns dmabuf on success, ERR_PTR(-errno) on error.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun struct dma_heap_ops {
26*4882a593Smuzhiyun 	struct dma_buf *(*allocate)(struct dma_heap *heap,
27*4882a593Smuzhiyun 			unsigned long len,
28*4882a593Smuzhiyun 			unsigned long fd_flags,
29*4882a593Smuzhiyun 			unsigned long heap_flags);
30*4882a593Smuzhiyun 	long (*get_pool_size)(struct dma_heap *heap);
31*4882a593Smuzhiyun #if IS_ENABLED(CONFIG_NO_GKI)
32*4882a593Smuzhiyun 	int (*get_phys)(struct dma_heap *heap, struct dma_heap_phys_data *phys);
33*4882a593Smuzhiyun #endif
34*4882a593Smuzhiyun };
35*4882a593Smuzhiyun 
36*4882a593Smuzhiyun /**
37*4882a593Smuzhiyun  * struct dma_heap_export_info - information needed to export a new dmabuf heap
38*4882a593Smuzhiyun  * @name:	used for debugging/device-node name
39*4882a593Smuzhiyun  * @ops:	ops struct for this heap
40*4882a593Smuzhiyun  * @priv:	heap exporter private data
41*4882a593Smuzhiyun  *
42*4882a593Smuzhiyun  * Information needed to export a new dmabuf heap.
43*4882a593Smuzhiyun  */
44*4882a593Smuzhiyun struct dma_heap_export_info {
45*4882a593Smuzhiyun 	const char *name;
46*4882a593Smuzhiyun 	const struct dma_heap_ops *ops;
47*4882a593Smuzhiyun 	void *priv;
48*4882a593Smuzhiyun };
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun /**
51*4882a593Smuzhiyun  * dma_heap_get_drvdata() - get per-heap driver data
52*4882a593Smuzhiyun  * @heap: DMA-Heap to retrieve private data for
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * Returns:
55*4882a593Smuzhiyun  * The per-heap data for the heap.
56*4882a593Smuzhiyun  */
57*4882a593Smuzhiyun void *dma_heap_get_drvdata(struct dma_heap *heap);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun /**
60*4882a593Smuzhiyun  * dma_heap_get_dev() - get device struct for the heap
61*4882a593Smuzhiyun  * @heap: DMA-Heap to retrieve device struct from
62*4882a593Smuzhiyun  *
63*4882a593Smuzhiyun  * Returns:
64*4882a593Smuzhiyun  * The device struct for the heap.
65*4882a593Smuzhiyun  */
66*4882a593Smuzhiyun struct device *dma_heap_get_dev(struct dma_heap *heap);
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun /**
69*4882a593Smuzhiyun  * dma_heap_get_name() - get heap name
70*4882a593Smuzhiyun  * @heap: DMA-Heap to retrieve private data for
71*4882a593Smuzhiyun  *
72*4882a593Smuzhiyun  * Returns:
73*4882a593Smuzhiyun  * The char* for the heap name.
74*4882a593Smuzhiyun  */
75*4882a593Smuzhiyun const char *dma_heap_get_name(struct dma_heap *heap);
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun /**
78*4882a593Smuzhiyun  * dma_heap_add - adds a heap to dmabuf heaps
79*4882a593Smuzhiyun  * @exp_info:		information needed to register this heap
80*4882a593Smuzhiyun  */
81*4882a593Smuzhiyun struct dma_heap *dma_heap_add(const struct dma_heap_export_info *exp_info);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun /**
84*4882a593Smuzhiyun  * dma_heap_put - drops a reference to a dmabuf heaps, potentially freeing it
85*4882a593Smuzhiyun  * @heap:		heap pointer
86*4882a593Smuzhiyun  */
87*4882a593Smuzhiyun void dma_heap_put(struct dma_heap *heap);
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun /**
90*4882a593Smuzhiyun  * dma_heap_find - Returns the registered dma_heap with the specified name
91*4882a593Smuzhiyun  * @name: Name of the heap to find
92*4882a593Smuzhiyun  *
93*4882a593Smuzhiyun  * NOTE: dma_heaps returned from this function MUST be released
94*4882a593Smuzhiyun  * using dma_heap_put() when the user is done.
95*4882a593Smuzhiyun  */
96*4882a593Smuzhiyun struct dma_heap *dma_heap_find(const char *name);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun  * dma_heap_buffer_alloc - Allocate dma-buf from a dma_heap
100*4882a593Smuzhiyun  * @heap:	dma_heap to allocate from
101*4882a593Smuzhiyun  * @len:	size to allocate
102*4882a593Smuzhiyun  * @fd_flags:	flags to set on returned dma-buf fd
103*4882a593Smuzhiyun  * @heap_flags:	flags to pass to the dma heap
104*4882a593Smuzhiyun  *
105*4882a593Smuzhiyun  * This is for internal dma-buf allocations only.
106*4882a593Smuzhiyun  */
107*4882a593Smuzhiyun struct dma_buf *dma_heap_buffer_alloc(struct dma_heap *heap, size_t len,
108*4882a593Smuzhiyun 				      unsigned int fd_flags,
109*4882a593Smuzhiyun 				      unsigned int heap_flags);
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun /** dma_heap_buffer_free - Free dma_buf allocated by dma_heap_buffer_alloc
112*4882a593Smuzhiyun  * @dma_buf:	dma_buf to free
113*4882a593Smuzhiyun  *
114*4882a593Smuzhiyun  * This is really only a simple wrapper to dma_buf_put()
115*4882a593Smuzhiyun  */
116*4882a593Smuzhiyun void dma_heap_buffer_free(struct dma_buf *);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun /**
119*4882a593Smuzhiyun  * dma_heap_bufferfd_alloc - Allocate dma-buf fd from a dma_heap
120*4882a593Smuzhiyun  * @heap:	dma_heap to allocate from
121*4882a593Smuzhiyun  * @len:	size to allocate
122*4882a593Smuzhiyun  * @fd_flags:	flags to set on returned dma-buf fd
123*4882a593Smuzhiyun  * @heap_flags:	flags to pass to the dma heap
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun int dma_heap_bufferfd_alloc(struct dma_heap *heap, size_t len,
126*4882a593Smuzhiyun 			    unsigned int fd_flags,
127*4882a593Smuzhiyun 			    unsigned int heap_flags);
128*4882a593Smuzhiyun #endif /* _DMA_HEAPS_H */
129