6edeb9228c4e45d6cf77f9c07202944e469ea38e
[platform/kernel/linux-starfive.git] / include / linux / host1x.h
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (c) 2009-2013, NVIDIA Corporation. All rights reserved.
4  */
5
6 #ifndef __LINUX_HOST1X_H
7 #define __LINUX_HOST1X_H
8
9 #include <linux/device.h>
10 #include <linux/types.h>
11
12 enum host1x_class {
13         HOST1X_CLASS_HOST1X = 0x1,
14         HOST1X_CLASS_GR2D = 0x51,
15         HOST1X_CLASS_GR2D_SB = 0x52,
16         HOST1X_CLASS_VIC = 0x5D,
17         HOST1X_CLASS_GR3D = 0x60,
18 };
19
20 struct host1x_client;
21 struct iommu_group;
22
23 /**
24  * struct host1x_client_ops - host1x client operations
25  * @init: host1x client initialization code
26  * @exit: host1x client tear down code
27  */
28 struct host1x_client_ops {
29         int (*init)(struct host1x_client *client);
30         int (*exit)(struct host1x_client *client);
31 };
32
33 /**
34  * struct host1x_client - host1x client structure
35  * @list: list node for the host1x client
36  * @parent: pointer to struct device representing the host1x controller
37  * @dev: pointer to struct device backing this host1x client
38  * @group: IOMMU group that this client is a member of
39  * @ops: host1x client operations
40  * @class: host1x class represented by this client
41  * @channel: host1x channel associated with this client
42  * @syncpts: array of syncpoints requested for this client
43  * @num_syncpts: number of syncpoints requested for this client
44  */
45 struct host1x_client {
46         struct list_head list;
47         struct device *parent;
48         struct device *dev;
49         struct iommu_group *group;
50
51         const struct host1x_client_ops *ops;
52
53         enum host1x_class class;
54         struct host1x_channel *channel;
55
56         struct host1x_syncpt **syncpts;
57         unsigned int num_syncpts;
58 };
59
60 /*
61  * host1x buffer objects
62  */
63
64 struct host1x_bo;
65 struct sg_table;
66
67 struct host1x_bo_ops {
68         struct host1x_bo *(*get)(struct host1x_bo *bo);
69         void (*put)(struct host1x_bo *bo);
70         struct sg_table *(*pin)(struct device *dev, struct host1x_bo *bo,
71                                 dma_addr_t *phys);
72         void (*unpin)(struct device *dev, struct sg_table *sgt);
73         void *(*mmap)(struct host1x_bo *bo);
74         void (*munmap)(struct host1x_bo *bo, void *addr);
75 };
76
77 struct host1x_bo {
78         const struct host1x_bo_ops *ops;
79 };
80
81 static inline void host1x_bo_init(struct host1x_bo *bo,
82                                   const struct host1x_bo_ops *ops)
83 {
84         bo->ops = ops;
85 }
86
87 static inline struct host1x_bo *host1x_bo_get(struct host1x_bo *bo)
88 {
89         return bo->ops->get(bo);
90 }
91
92 static inline void host1x_bo_put(struct host1x_bo *bo)
93 {
94         bo->ops->put(bo);
95 }
96
97 static inline struct sg_table *host1x_bo_pin(struct device *dev,
98                                              struct host1x_bo *bo,
99                                              dma_addr_t *phys)
100 {
101         return bo->ops->pin(dev, bo, phys);
102 }
103
104 static inline void host1x_bo_unpin(struct device *dev, struct host1x_bo *bo,
105                                    struct sg_table *sgt)
106 {
107         bo->ops->unpin(dev, sgt);
108 }
109
110 static inline void *host1x_bo_mmap(struct host1x_bo *bo)
111 {
112         return bo->ops->mmap(bo);
113 }
114
115 static inline void host1x_bo_munmap(struct host1x_bo *bo, void *addr)
116 {
117         bo->ops->munmap(bo, addr);
118 }
119
120 /*
121  * host1x syncpoints
122  */
123
124 #define HOST1X_SYNCPT_CLIENT_MANAGED    (1 << 0)
125 #define HOST1X_SYNCPT_HAS_BASE          (1 << 1)
126
127 struct host1x_syncpt_base;
128 struct host1x_syncpt;
129 struct host1x;
130
131 struct host1x_syncpt *host1x_syncpt_get(struct host1x *host, u32 id);
132 u32 host1x_syncpt_id(struct host1x_syncpt *sp);
133 u32 host1x_syncpt_read_min(struct host1x_syncpt *sp);
134 u32 host1x_syncpt_read_max(struct host1x_syncpt *sp);
135 u32 host1x_syncpt_read(struct host1x_syncpt *sp);
136 int host1x_syncpt_incr(struct host1x_syncpt *sp);
137 u32 host1x_syncpt_incr_max(struct host1x_syncpt *sp, u32 incrs);
138 int host1x_syncpt_wait(struct host1x_syncpt *sp, u32 thresh, long timeout,
139                        u32 *value);
140 struct host1x_syncpt *host1x_syncpt_request(struct host1x_client *client,
141                                             unsigned long flags);
142 void host1x_syncpt_free(struct host1x_syncpt *sp);
143
144 struct host1x_syncpt_base *host1x_syncpt_get_base(struct host1x_syncpt *sp);
145 u32 host1x_syncpt_base_id(struct host1x_syncpt_base *base);
146
147 /*
148  * host1x channel
149  */
150
151 struct host1x_channel;
152 struct host1x_job;
153
154 struct host1x_channel *host1x_channel_request(struct host1x_client *client);
155 struct host1x_channel *host1x_channel_get(struct host1x_channel *channel);
156 void host1x_channel_put(struct host1x_channel *channel);
157 int host1x_job_submit(struct host1x_job *job);
158
159 /*
160  * host1x job
161  */
162
163 #define HOST1X_RELOC_READ       (1 << 0)
164 #define HOST1X_RELOC_WRITE      (1 << 1)
165
166 struct host1x_reloc {
167         struct {
168                 struct host1x_bo *bo;
169                 unsigned long offset;
170         } cmdbuf;
171         struct {
172                 struct host1x_bo *bo;
173                 unsigned long offset;
174         } target;
175         unsigned long shift;
176         unsigned long flags;
177 };
178
179 struct host1x_job {
180         /* When refcount goes to zero, job can be freed */
181         struct kref ref;
182
183         /* List entry */
184         struct list_head list;
185
186         /* Channel where job is submitted to */
187         struct host1x_channel *channel;
188
189         /* client where the job originated */
190         struct host1x_client *client;
191
192         /* Gathers and their memory */
193         struct host1x_job_gather *gathers;
194         unsigned int num_gathers;
195
196         /* Array of handles to be pinned & unpinned */
197         struct host1x_reloc *relocs;
198         unsigned int num_relocs;
199         struct host1x_job_unpin_data *unpins;
200         unsigned int num_unpins;
201
202         dma_addr_t *addr_phys;
203         dma_addr_t *gather_addr_phys;
204         dma_addr_t *reloc_addr_phys;
205
206         /* Sync point id, number of increments and end related to the submit */
207         u32 syncpt_id;
208         u32 syncpt_incrs;
209         u32 syncpt_end;
210
211         /* Maximum time to wait for this job */
212         unsigned int timeout;
213
214         /* Index and number of slots used in the push buffer */
215         unsigned int first_get;
216         unsigned int num_slots;
217
218         /* Copy of gathers */
219         size_t gather_copy_size;
220         dma_addr_t gather_copy;
221         u8 *gather_copy_mapped;
222
223         /* Check if register is marked as an address reg */
224         int (*is_addr_reg)(struct device *dev, u32 class, u32 reg);
225
226         /* Check if class belongs to the unit */
227         int (*is_valid_class)(u32 class);
228
229         /* Request a SETCLASS to this class */
230         u32 class;
231
232         /* Add a channel wait for previous ops to complete */
233         bool serialize;
234 };
235
236 struct host1x_job *host1x_job_alloc(struct host1x_channel *ch,
237                                     u32 num_cmdbufs, u32 num_relocs);
238 void host1x_job_add_gather(struct host1x_job *job, struct host1x_bo *bo,
239                            unsigned int words, unsigned int offset);
240 struct host1x_job *host1x_job_get(struct host1x_job *job);
241 void host1x_job_put(struct host1x_job *job);
242 int host1x_job_pin(struct host1x_job *job, struct device *dev);
243 void host1x_job_unpin(struct host1x_job *job);
244
245 /*
246  * subdevice probe infrastructure
247  */
248
249 struct host1x_device;
250
251 /**
252  * struct host1x_driver - host1x logical device driver
253  * @driver: core driver
254  * @subdevs: table of OF device IDs matching subdevices for this driver
255  * @list: list node for the driver
256  * @probe: called when the host1x logical device is probed
257  * @remove: called when the host1x logical device is removed
258  * @shutdown: called when the host1x logical device is shut down
259  */
260 struct host1x_driver {
261         struct device_driver driver;
262
263         const struct of_device_id *subdevs;
264         struct list_head list;
265
266         int (*probe)(struct host1x_device *device);
267         int (*remove)(struct host1x_device *device);
268         void (*shutdown)(struct host1x_device *device);
269 };
270
271 static inline struct host1x_driver *
272 to_host1x_driver(struct device_driver *driver)
273 {
274         return container_of(driver, struct host1x_driver, driver);
275 }
276
277 int host1x_driver_register_full(struct host1x_driver *driver,
278                                 struct module *owner);
279 void host1x_driver_unregister(struct host1x_driver *driver);
280
281 #define host1x_driver_register(driver) \
282         host1x_driver_register_full(driver, THIS_MODULE)
283
284 struct host1x_device {
285         struct host1x_driver *driver;
286         struct list_head list;
287         struct device dev;
288
289         struct mutex subdevs_lock;
290         struct list_head subdevs;
291         struct list_head active;
292
293         struct mutex clients_lock;
294         struct list_head clients;
295
296         bool registered;
297
298         struct device_dma_parameters dma_parms;
299 };
300
301 static inline struct host1x_device *to_host1x_device(struct device *dev)
302 {
303         return container_of(dev, struct host1x_device, dev);
304 }
305
306 int host1x_device_init(struct host1x_device *device);
307 int host1x_device_exit(struct host1x_device *device);
308
309 int host1x_client_register(struct host1x_client *client);
310 int host1x_client_unregister(struct host1x_client *client);
311
312 struct tegra_mipi_device;
313
314 struct tegra_mipi_device *tegra_mipi_request(struct device *device);
315 void tegra_mipi_free(struct tegra_mipi_device *device);
316 int tegra_mipi_enable(struct tegra_mipi_device *device);
317 int tegra_mipi_disable(struct tegra_mipi_device *device);
318 int tegra_mipi_calibrate(struct tegra_mipi_device *device);
319
320 #endif