xref: /OK3568_Linux_fs/kernel/drivers/dma-buf/rk_heaps/rk-dma-heap.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * DMABUF Heaps Allocation Infrastructure
4  *
5  * Copyright (C) 2011 Google, Inc.
6  * Copyright (C) 2019 Linaro Ltd.
7  * Copyright (C) 2022 Rockchip Electronics Co. Ltd.
8  * Author: Simon Xue <xxm@rock-chips.com>
9  */
10 
11 #ifndef _RK_DMA_HEAPS_H
12 #define _RK_DMA_HEAPS_H
13 
14 #include <linux/cdev.h>
15 #include <linux/types.h>
16 #include <linux/dma-buf.h>
17 #include <linux/rk-dma-heap.h>
18 
19 #if defined(CONFIG_DMABUF_RK_HEAPS_DEBUG_PRINT)
20 #define dma_heap_print(fmt, ...)	\
21 	printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
22 #else
23 #define dma_heap_print(fmt, ...)	\
24 	no_printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__)
25 #endif
26 
27 #define RK_DMA_HEAP_NAME_LEN 16
28 
29 struct rk_vmap_pfn_data {
30 	unsigned long	pfn; /* first pfn of contiguous */
31 	pgprot_t	prot;
32 };
33 
34 /**
35  * struct rk_dma_heap_ops - ops to operate on a given heap
36  * @allocate:		allocate dmabuf and return struct dma_buf ptr
37  * @get_pool_size:	if heap maintains memory pools, get pool size in bytes
38  *
39  * allocate returns dmabuf on success, ERR_PTR(-errno) on error.
40  */
41 struct rk_dma_heap_ops {
42 	struct dma_buf *(*allocate)(struct rk_dma_heap *heap,
43 			unsigned long len,
44 			unsigned long fd_flags,
45 			unsigned long heap_flags,
46 			const char *name);
47 	struct page *(*alloc_contig_pages)(struct rk_dma_heap *heap,
48 					   size_t len, const char *name);
49 	void (*free_contig_pages)(struct rk_dma_heap *heap,
50 				  struct page *pages, size_t len,
51 				  const char *name);
52 	long (*get_pool_size)(struct rk_dma_heap *heap);
53 };
54 
55 /**
56  * struct rk_dma_heap_export_info - information needed to export a new dmabuf heap
57  * @name:	used for debugging/device-node name
58  * @ops:	ops struct for this heap
59  * @priv:	heap exporter private data
60  *
61  * Information needed to export a new dmabuf heap.
62  */
63 struct rk_dma_heap_export_info {
64 	const char *name;
65 	const struct rk_dma_heap_ops *ops;
66 	void *priv;
67 	bool support_cma;
68 };
69 
70 /**
71  * struct rk_dma_heap - represents a dmabuf heap in the system
72  * @name:		used for debugging/device-node name
73  * @ops:		ops struct for this heap
74  * @heap_devt		heap device node
75  * @list		list head connecting to list of heaps
76  * @heap_cdev		heap char device
77  * @heap_dev		heap device struct
78  *
79  * Represents a heap of memory from which buffers can be made.
80  */
81 struct rk_dma_heap {
82 	const char *name;
83 	const struct rk_dma_heap_ops *ops;
84 	void *priv;
85 	dev_t heap_devt;
86 	struct list_head list;
87 	struct list_head dmabuf_list; /* dmabuf attach to this node */
88 	struct mutex dmabuf_lock;
89 	struct list_head contig_list; /* contig buffer attach to this node */
90 	struct mutex contig_lock;
91 	struct cdev heap_cdev;
92 	struct kref refcount;
93 	struct device *heap_dev;
94 	bool support_cma;
95 	struct seq_file *s;
96 	struct proc_dir_entry *procfs;
97 	unsigned long total_size;
98 };
99 
100 struct rk_dma_heap_dmabuf {
101 	struct list_head node;
102 	struct dma_buf *dmabuf;
103 	const char *orig_alloc;
104 	phys_addr_t start;
105 	phys_addr_t end;
106 };
107 
108 struct rk_dma_heap_contig_buf {
109 	struct list_head node;
110 	const char *orig_alloc;
111 	phys_addr_t start;
112 	phys_addr_t end;
113 };
114 
115 /**
116  * rk_dma_heap_get_drvdata() - get per-heap driver data
117  * @heap: DMA-Heap to retrieve private data for
118  *
119  * Returns:
120  * The per-heap data for the heap.
121  */
122 void *rk_dma_heap_get_drvdata(struct rk_dma_heap *heap);
123 
124 /**
125  * rk_dma_heap_get_dev() - get device struct for the heap
126  * @heap: DMA-Heap to retrieve device struct from
127  *
128  * Returns:
129  * The device struct for the heap.
130  */
131 struct device *rk_dma_heap_get_dev(struct rk_dma_heap *heap);
132 
133 /**
134  * rk_dma_heap_get_name() - get heap name
135  * @heap: DMA-Heap to retrieve private data for
136  *
137  * Returns:
138  * The char* for the heap name.
139  */
140 const char *rk_dma_heap_get_name(struct rk_dma_heap *heap);
141 
142 /**
143  * rk_dma_heap_add - adds a heap to dmabuf heaps
144  * @exp_info:		information needed to register this heap
145  */
146 struct rk_dma_heap *rk_dma_heap_add(const struct rk_dma_heap_export_info *exp_info);
147 
148 /**
149  * rk_dma_heap_put - drops a reference to a dmabuf heaps, potentially freeing it
150  * @heap:		heap pointer
151  */
152 void rk_dma_heap_put(struct rk_dma_heap *heap);
153 
154 /**
155  * rk_vmap_contig_pfn - Map contiguous pfn to vm area
156  * @pfn:	indicate the first pfn of contig
157  * @count:	count of pfns
158  * @prot:	for mapping
159  */
160 void *rk_vmap_contig_pfn(unsigned long pfn, unsigned int count,
161 				 pgprot_t prot);
162 /**
163  * rk_dma_heap_total_inc - Increase total buffer size
164  * @heap:	dma_heap to increase
165  * @len:	length to increase
166  */
167 void rk_dma_heap_total_inc(struct rk_dma_heap *heap, size_t len);
168 /**
169  * rk_dma_heap_total_dec - Decrease total buffer size
170  * @heap:	dma_heap to decrease
171  * @len:	length to decrease
172  */
173 void rk_dma_heap_total_dec(struct rk_dma_heap *heap, size_t len);
174 /**
175  * rk_dma_heap_get_cma - get cma structure
176  */
177 struct cma *rk_dma_heap_get_cma(void);
178 #endif /* _DMA_HEAPS_H */
179