blob: 0bc8c57196acae69984ed09e716f8d24b03fef23 [file] [log] [blame]
Eric Anholtc8b75bc2015-03-02 13:01:12 -08001/*
2 * Copyright (C) 2015 Broadcom
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include "drmP.h"
10#include "drm_gem_cma_helper.h"
11
12struct vc4_dev {
13 struct drm_device *dev;
14
15 struct vc4_hdmi *hdmi;
16 struct vc4_hvs *hvs;
17 struct vc4_crtc *crtc[3];
Eric Anholtd3f51682015-03-02 13:01:12 -080018 struct vc4_v3d *v3d;
Derek Foreman48666d52015-07-02 11:19:54 -050019
20 struct drm_fbdev_cma *fbdev;
Eric Anholtc826a6e2015-10-09 20:25:07 -070021
22 /* The kernel-space BO cache. Tracks buffers that have been
23 * unreferenced by all other users (refcounts of 0!) but not
24 * yet freed, so we can do cheap allocations.
25 */
26 struct vc4_bo_cache {
27 /* Array of list heads for entries in the BO cache,
28 * based on number of pages, so we can do O(1) lookups
29 * in the cache when allocating.
30 */
31 struct list_head *size_list;
32 uint32_t size_list_size;
33
34 /* List of all BOs in the cache, ordered by age, so we
35 * can do O(1) lookups when trying to free old
36 * buffers.
37 */
38 struct list_head time_list;
39 struct work_struct time_work;
40 struct timer_list time_timer;
41 } bo_cache;
42
43 struct vc4_bo_stats {
44 u32 num_allocated;
45 u32 size_allocated;
46 u32 num_cached;
47 u32 size_cached;
48 } bo_stats;
49
50 /* Protects bo_cache and the BO stats. */
51 struct mutex bo_lock;
Eric Anholtd5b1a782015-11-30 12:13:37 -080052
53 /* Sequence number for the last job queued in job_list.
54 * Starts at 0 (no jobs emitted).
55 */
56 uint64_t emit_seqno;
57
58 /* Sequence number for the last completed job on the GPU.
59 * Starts at 0 (no jobs completed).
60 */
61 uint64_t finished_seqno;
62
63 /* List of all struct vc4_exec_info for jobs to be executed.
64 * The first job in the list is the one currently programmed
65 * into ct0ca/ct1ca for execution.
66 */
67 struct list_head job_list;
68 /* List of the finished vc4_exec_infos waiting to be freed by
69 * job_done_work.
70 */
71 struct list_head job_done_list;
72 /* Spinlock used to synchronize the job_list and seqno
73 * accesses between the IRQ handler and GEM ioctls.
74 */
75 spinlock_t job_lock;
76 wait_queue_head_t job_wait_queue;
77 struct work_struct job_done_work;
78
79 /* The binner overflow memory that's currently set up in
80 * BPOA/BPOS registers. When overflow occurs and a new one is
81 * allocated, the previous one will be moved to
82 * vc4->current_exec's free list.
83 */
84 struct vc4_bo *overflow_mem;
85 struct work_struct overflow_mem_work;
86
87 struct {
88 uint32_t last_ct0ca, last_ct1ca;
89 struct timer_list timer;
90 struct work_struct reset_work;
91 } hangcheck;
92
93 struct semaphore async_modeset;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080094};
95
96static inline struct vc4_dev *
97to_vc4_dev(struct drm_device *dev)
98{
99 return (struct vc4_dev *)dev->dev_private;
100}
101
102struct vc4_bo {
103 struct drm_gem_cma_object base;
Eric Anholtc826a6e2015-10-09 20:25:07 -0700104
Eric Anholtd5b1a782015-11-30 12:13:37 -0800105 /* seqno of the last job to render to this BO. */
106 uint64_t seqno;
107
Eric Anholtc826a6e2015-10-09 20:25:07 -0700108 /* List entry for the BO's position in either
109 * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
110 */
111 struct list_head unref_head;
112
113 /* Time in jiffies when the BO was put in vc4->bo_cache. */
114 unsigned long free_time;
115
116 /* List entry for the BO's position in vc4_dev->bo_cache.size_list */
117 struct list_head size_head;
Eric Anholt463873d2015-11-30 11:41:40 -0800118
119 /* Struct for shader validation state, if created by
120 * DRM_IOCTL_VC4_CREATE_SHADER_BO.
121 */
122 struct vc4_validated_shader_info *validated_shader;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800123};
124
125static inline struct vc4_bo *
126to_vc4_bo(struct drm_gem_object *bo)
127{
128 return (struct vc4_bo *)bo;
129}
130
Eric Anholtd3f51682015-03-02 13:01:12 -0800131struct vc4_v3d {
132 struct platform_device *pdev;
133 void __iomem *regs;
134};
135
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800136struct vc4_hvs {
137 struct platform_device *pdev;
138 void __iomem *regs;
139 void __iomem *dlist;
140};
141
142struct vc4_plane {
143 struct drm_plane base;
144};
145
146static inline struct vc4_plane *
147to_vc4_plane(struct drm_plane *plane)
148{
149 return (struct vc4_plane *)plane;
150}
151
152enum vc4_encoder_type {
153 VC4_ENCODER_TYPE_HDMI,
154 VC4_ENCODER_TYPE_VEC,
155 VC4_ENCODER_TYPE_DSI0,
156 VC4_ENCODER_TYPE_DSI1,
157 VC4_ENCODER_TYPE_SMI,
158 VC4_ENCODER_TYPE_DPI,
159};
160
161struct vc4_encoder {
162 struct drm_encoder base;
163 enum vc4_encoder_type type;
164 u32 clock_select;
165};
166
167static inline struct vc4_encoder *
168to_vc4_encoder(struct drm_encoder *encoder)
169{
170 return container_of(encoder, struct vc4_encoder, base);
171}
172
Eric Anholtd3f51682015-03-02 13:01:12 -0800173#define V3D_READ(offset) readl(vc4->v3d->regs + offset)
174#define V3D_WRITE(offset, val) writel(val, vc4->v3d->regs + offset)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800175#define HVS_READ(offset) readl(vc4->hvs->regs + offset)
176#define HVS_WRITE(offset, val) writel(val, vc4->hvs->regs + offset)
177
Eric Anholtd5b1a782015-11-30 12:13:37 -0800178struct vc4_exec_info {
179 /* Sequence number for this bin/render job. */
180 uint64_t seqno;
181
182 /* Kernel-space copy of the ioctl arguments */
183 struct drm_vc4_submit_cl *args;
184
185 /* This is the array of BOs that were looked up at the start of exec.
186 * Command validation will use indices into this array.
187 */
188 struct drm_gem_cma_object **bo;
189 uint32_t bo_count;
190
191 /* Pointers for our position in vc4->job_list */
192 struct list_head head;
193
194 /* List of other BOs used in the job that need to be released
195 * once the job is complete.
196 */
197 struct list_head unref_list;
198
199 /* Current unvalidated indices into @bo loaded by the non-hardware
200 * VC4_PACKET_GEM_HANDLES.
201 */
202 uint32_t bo_index[2];
203
204 /* This is the BO where we store the validated command lists, shader
205 * records, and uniforms.
206 */
207 struct drm_gem_cma_object *exec_bo;
208
209 /**
210 * This tracks the per-shader-record state (packet 64) that
211 * determines the length of the shader record and the offset
212 * it's expected to be found at. It gets read in from the
213 * command lists.
214 */
215 struct vc4_shader_state {
216 uint32_t addr;
217 /* Maximum vertex index referenced by any primitive using this
218 * shader state.
219 */
220 uint32_t max_index;
221 } *shader_state;
222
223 /** How many shader states the user declared they were using. */
224 uint32_t shader_state_size;
225 /** How many shader state records the validator has seen. */
226 uint32_t shader_state_count;
227
228 bool found_tile_binning_mode_config_packet;
229 bool found_start_tile_binning_packet;
230 bool found_increment_semaphore_packet;
231 bool found_flush;
232 uint8_t bin_tiles_x, bin_tiles_y;
233 struct drm_gem_cma_object *tile_bo;
234 uint32_t tile_alloc_offset;
235
236 /**
237 * Computed addresses pointing into exec_bo where we start the
238 * bin thread (ct0) and render thread (ct1).
239 */
240 uint32_t ct0ca, ct0ea;
241 uint32_t ct1ca, ct1ea;
242
243 /* Pointer to the unvalidated bin CL (if present). */
244 void *bin_u;
245
246 /* Pointers to the shader recs. These paddr gets incremented as CL
247 * packets are relocated in validate_gl_shader_state, and the vaddrs
248 * (u and v) get incremented and size decremented as the shader recs
249 * themselves are validated.
250 */
251 void *shader_rec_u;
252 void *shader_rec_v;
253 uint32_t shader_rec_p;
254 uint32_t shader_rec_size;
255
256 /* Pointers to the uniform data. These pointers are incremented, and
257 * size decremented, as each batch of uniforms is uploaded.
258 */
259 void *uniforms_u;
260 void *uniforms_v;
261 uint32_t uniforms_p;
262 uint32_t uniforms_size;
263};
264
265static inline struct vc4_exec_info *
266vc4_first_job(struct vc4_dev *vc4)
267{
268 if (list_empty(&vc4->job_list))
269 return NULL;
270 return list_first_entry(&vc4->job_list, struct vc4_exec_info, head);
271}
272
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800273/**
Eric Anholt463873d2015-11-30 11:41:40 -0800274 * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
275 * setup parameters.
276 *
277 * This will be used at draw time to relocate the reference to the texture
278 * contents in p0, and validate that the offset combined with
279 * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
280 * Note that the hardware treats unprovided config parameters as 0, so not all
281 * of them need to be set up for every texure sample, and we'll store ~0 as
282 * the offset to mark the unused ones.
283 *
284 * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
285 * Setup") for definitions of the texture parameters.
286 */
287struct vc4_texture_sample_info {
288 bool is_direct;
289 uint32_t p_offset[4];
290};
291
292/**
293 * struct vc4_validated_shader_info - information about validated shaders that
294 * needs to be used from command list validation.
295 *
296 * For a given shader, each time a shader state record references it, we need
297 * to verify that the shader doesn't read more uniforms than the shader state
298 * record's uniform BO pointer can provide, and we need to apply relocations
299 * and validate the shader state record's uniforms that define the texture
300 * samples.
301 */
302struct vc4_validated_shader_info {
303 uint32_t uniforms_size;
304 uint32_t uniforms_src_size;
305 uint32_t num_texture_samples;
306 struct vc4_texture_sample_info *texture_samples;
307};
308
309/**
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800310 * _wait_for - magic (register) wait macro
311 *
312 * Does the right thing for modeset paths when run under kdgb or similar atomic
313 * contexts. Note that it's important that we check the condition again after
314 * having timed out, since the timeout could be due to preemption or similar and
315 * we've never had a chance to check the condition before the timeout.
316 */
317#define _wait_for(COND, MS, W) ({ \
318 unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1; \
319 int ret__ = 0; \
320 while (!(COND)) { \
321 if (time_after(jiffies, timeout__)) { \
322 if (!(COND)) \
323 ret__ = -ETIMEDOUT; \
324 break; \
325 } \
326 if (W && drm_can_sleep()) { \
327 msleep(W); \
328 } else { \
329 cpu_relax(); \
330 } \
331 } \
332 ret__; \
333})
334
335#define wait_for(COND, MS) _wait_for(COND, MS, 1)
336
337/* vc4_bo.c */
Eric Anholtc826a6e2015-10-09 20:25:07 -0700338struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800339void vc4_free_object(struct drm_gem_object *gem_obj);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700340struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
341 bool from_cache);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800342int vc4_dumb_create(struct drm_file *file_priv,
343 struct drm_device *dev,
344 struct drm_mode_create_dumb *args);
345struct dma_buf *vc4_prime_export(struct drm_device *dev,
346 struct drm_gem_object *obj, int flags);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300347int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
348 struct drm_file *file_priv);
Eric Anholt463873d2015-11-30 11:41:40 -0800349int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
350 struct drm_file *file_priv);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300351int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
352 struct drm_file *file_priv);
Eric Anholt463873d2015-11-30 11:41:40 -0800353int vc4_mmap(struct file *filp, struct vm_area_struct *vma);
354int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
355void *vc4_prime_vmap(struct drm_gem_object *obj);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700356void vc4_bo_cache_init(struct drm_device *dev);
357void vc4_bo_cache_destroy(struct drm_device *dev);
358int vc4_bo_stats_debugfs(struct seq_file *m, void *arg);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800359
360/* vc4_crtc.c */
361extern struct platform_driver vc4_crtc_driver;
Dave Airlie1f437102015-10-22 10:23:31 +1000362int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id);
363void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800364void vc4_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file);
365int vc4_crtc_debugfs_regs(struct seq_file *m, void *arg);
366
367/* vc4_debugfs.c */
368int vc4_debugfs_init(struct drm_minor *minor);
369void vc4_debugfs_cleanup(struct drm_minor *minor);
370
371/* vc4_drv.c */
372void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
373
Eric Anholtd5b1a782015-11-30 12:13:37 -0800374/* vc4_gem.c */
375void vc4_gem_init(struct drm_device *dev);
376void vc4_gem_destroy(struct drm_device *dev);
377int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
378 struct drm_file *file_priv);
379int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
380 struct drm_file *file_priv);
381int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
382 struct drm_file *file_priv);
383void vc4_submit_next_job(struct drm_device *dev);
384int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
385 uint64_t timeout_ns, bool interruptible);
386void vc4_job_handle_completed(struct vc4_dev *vc4);
387
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800388/* vc4_hdmi.c */
389extern struct platform_driver vc4_hdmi_driver;
390int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused);
391
Eric Anholtd5b1a782015-11-30 12:13:37 -0800392/* vc4_irq.c */
393irqreturn_t vc4_irq(int irq, void *arg);
394void vc4_irq_preinstall(struct drm_device *dev);
395int vc4_irq_postinstall(struct drm_device *dev);
396void vc4_irq_uninstall(struct drm_device *dev);
397void vc4_irq_reset(struct drm_device *dev);
398
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800399/* vc4_hvs.c */
400extern struct platform_driver vc4_hvs_driver;
401void vc4_hvs_dump_state(struct drm_device *dev);
402int vc4_hvs_debugfs_regs(struct seq_file *m, void *unused);
403
404/* vc4_kms.c */
405int vc4_kms_load(struct drm_device *dev);
406
407/* vc4_plane.c */
408struct drm_plane *vc4_plane_init(struct drm_device *dev,
409 enum drm_plane_type type);
410u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
411u32 vc4_plane_dlist_size(struct drm_plane_state *state);
Eric Anholt463873d2015-11-30 11:41:40 -0800412
Eric Anholtd3f51682015-03-02 13:01:12 -0800413/* vc4_v3d.c */
414extern struct platform_driver vc4_v3d_driver;
415int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused);
416int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800417int vc4_v3d_set_power(struct vc4_dev *vc4, bool on);
418
419/* vc4_validate.c */
420int
421vc4_validate_bin_cl(struct drm_device *dev,
422 void *validated,
423 void *unvalidated,
424 struct vc4_exec_info *exec);
425
426int
427vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
428
429struct drm_gem_cma_object *vc4_use_bo(struct vc4_exec_info *exec,
430 uint32_t hindex);
431
432int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
433
434bool vc4_check_tex_size(struct vc4_exec_info *exec,
435 struct drm_gem_cma_object *fbo,
436 uint32_t offset, uint8_t tiling_format,
437 uint32_t width, uint32_t height, uint8_t cpp);
Eric Anholtd3f51682015-03-02 13:01:12 -0800438
Eric Anholt463873d2015-11-30 11:41:40 -0800439/* vc4_validate_shader.c */
440struct vc4_validated_shader_info *
441vc4_validate_shader(struct drm_gem_cma_object *shader_obj);