blob: f9927d87369f8338fd4f9d5587747cc15d0a6153 [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
Eric Anholtb501bac2015-11-30 12:34:01 -080079 /* List of struct vc4_seqno_cb for callbacks to be made from a
80 * workqueue when the given seqno is passed.
81 */
82 struct list_head seqno_cb_list;
83
Eric Anholtd5b1a782015-11-30 12:13:37 -080084 /* The binner overflow memory that's currently set up in
85 * BPOA/BPOS registers. When overflow occurs and a new one is
86 * allocated, the previous one will be moved to
87 * vc4->current_exec's free list.
88 */
89 struct vc4_bo *overflow_mem;
90 struct work_struct overflow_mem_work;
91
92 struct {
93 uint32_t last_ct0ca, last_ct1ca;
94 struct timer_list timer;
95 struct work_struct reset_work;
96 } hangcheck;
97
98 struct semaphore async_modeset;
Eric Anholtc8b75bc2015-03-02 13:01:12 -080099};
100
101static inline struct vc4_dev *
102to_vc4_dev(struct drm_device *dev)
103{
104 return (struct vc4_dev *)dev->dev_private;
105}
106
107struct vc4_bo {
108 struct drm_gem_cma_object base;
Eric Anholtc826a6e2015-10-09 20:25:07 -0700109
Eric Anholtd5b1a782015-11-30 12:13:37 -0800110 /* seqno of the last job to render to this BO. */
111 uint64_t seqno;
112
Eric Anholtc826a6e2015-10-09 20:25:07 -0700113 /* List entry for the BO's position in either
114 * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
115 */
116 struct list_head unref_head;
117
118 /* Time in jiffies when the BO was put in vc4->bo_cache. */
119 unsigned long free_time;
120
121 /* List entry for the BO's position in vc4_dev->bo_cache.size_list */
122 struct list_head size_head;
Eric Anholt463873d2015-11-30 11:41:40 -0800123
124 /* Struct for shader validation state, if created by
125 * DRM_IOCTL_VC4_CREATE_SHADER_BO.
126 */
127 struct vc4_validated_shader_info *validated_shader;
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800128};
129
130static inline struct vc4_bo *
131to_vc4_bo(struct drm_gem_object *bo)
132{
133 return (struct vc4_bo *)bo;
134}
135
Eric Anholtb501bac2015-11-30 12:34:01 -0800136struct vc4_seqno_cb {
137 struct work_struct work;
138 uint64_t seqno;
139 void (*func)(struct vc4_seqno_cb *cb);
140};
141
Eric Anholtd3f51682015-03-02 13:01:12 -0800142struct vc4_v3d {
143 struct platform_device *pdev;
144 void __iomem *regs;
145};
146
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800147struct vc4_hvs {
148 struct platform_device *pdev;
149 void __iomem *regs;
150 void __iomem *dlist;
151};
152
153struct vc4_plane {
154 struct drm_plane base;
155};
156
157static inline struct vc4_plane *
158to_vc4_plane(struct drm_plane *plane)
159{
160 return (struct vc4_plane *)plane;
161}
162
163enum vc4_encoder_type {
164 VC4_ENCODER_TYPE_HDMI,
165 VC4_ENCODER_TYPE_VEC,
166 VC4_ENCODER_TYPE_DSI0,
167 VC4_ENCODER_TYPE_DSI1,
168 VC4_ENCODER_TYPE_SMI,
169 VC4_ENCODER_TYPE_DPI,
170};
171
172struct vc4_encoder {
173 struct drm_encoder base;
174 enum vc4_encoder_type type;
175 u32 clock_select;
176};
177
178static inline struct vc4_encoder *
179to_vc4_encoder(struct drm_encoder *encoder)
180{
181 return container_of(encoder, struct vc4_encoder, base);
182}
183
Eric Anholtd3f51682015-03-02 13:01:12 -0800184#define V3D_READ(offset) readl(vc4->v3d->regs + offset)
185#define V3D_WRITE(offset, val) writel(val, vc4->v3d->regs + offset)
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800186#define HVS_READ(offset) readl(vc4->hvs->regs + offset)
187#define HVS_WRITE(offset, val) writel(val, vc4->hvs->regs + offset)
188
Eric Anholtd5b1a782015-11-30 12:13:37 -0800189struct vc4_exec_info {
190 /* Sequence number for this bin/render job. */
191 uint64_t seqno;
192
193 /* Kernel-space copy of the ioctl arguments */
194 struct drm_vc4_submit_cl *args;
195
196 /* This is the array of BOs that were looked up at the start of exec.
197 * Command validation will use indices into this array.
198 */
199 struct drm_gem_cma_object **bo;
200 uint32_t bo_count;
201
202 /* Pointers for our position in vc4->job_list */
203 struct list_head head;
204
205 /* List of other BOs used in the job that need to be released
206 * once the job is complete.
207 */
208 struct list_head unref_list;
209
210 /* Current unvalidated indices into @bo loaded by the non-hardware
211 * VC4_PACKET_GEM_HANDLES.
212 */
213 uint32_t bo_index[2];
214
215 /* This is the BO where we store the validated command lists, shader
216 * records, and uniforms.
217 */
218 struct drm_gem_cma_object *exec_bo;
219
220 /**
221 * This tracks the per-shader-record state (packet 64) that
222 * determines the length of the shader record and the offset
223 * it's expected to be found at. It gets read in from the
224 * command lists.
225 */
226 struct vc4_shader_state {
227 uint32_t addr;
228 /* Maximum vertex index referenced by any primitive using this
229 * shader state.
230 */
231 uint32_t max_index;
232 } *shader_state;
233
234 /** How many shader states the user declared they were using. */
235 uint32_t shader_state_size;
236 /** How many shader state records the validator has seen. */
237 uint32_t shader_state_count;
238
239 bool found_tile_binning_mode_config_packet;
240 bool found_start_tile_binning_packet;
241 bool found_increment_semaphore_packet;
242 bool found_flush;
243 uint8_t bin_tiles_x, bin_tiles_y;
244 struct drm_gem_cma_object *tile_bo;
245 uint32_t tile_alloc_offset;
246
247 /**
248 * Computed addresses pointing into exec_bo where we start the
249 * bin thread (ct0) and render thread (ct1).
250 */
251 uint32_t ct0ca, ct0ea;
252 uint32_t ct1ca, ct1ea;
253
254 /* Pointer to the unvalidated bin CL (if present). */
255 void *bin_u;
256
257 /* Pointers to the shader recs. These paddr gets incremented as CL
258 * packets are relocated in validate_gl_shader_state, and the vaddrs
259 * (u and v) get incremented and size decremented as the shader recs
260 * themselves are validated.
261 */
262 void *shader_rec_u;
263 void *shader_rec_v;
264 uint32_t shader_rec_p;
265 uint32_t shader_rec_size;
266
267 /* Pointers to the uniform data. These pointers are incremented, and
268 * size decremented, as each batch of uniforms is uploaded.
269 */
270 void *uniforms_u;
271 void *uniforms_v;
272 uint32_t uniforms_p;
273 uint32_t uniforms_size;
274};
275
276static inline struct vc4_exec_info *
277vc4_first_job(struct vc4_dev *vc4)
278{
279 if (list_empty(&vc4->job_list))
280 return NULL;
281 return list_first_entry(&vc4->job_list, struct vc4_exec_info, head);
282}
283
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800284/**
Eric Anholt463873d2015-11-30 11:41:40 -0800285 * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
286 * setup parameters.
287 *
288 * This will be used at draw time to relocate the reference to the texture
289 * contents in p0, and validate that the offset combined with
290 * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
291 * Note that the hardware treats unprovided config parameters as 0, so not all
292 * of them need to be set up for every texure sample, and we'll store ~0 as
293 * the offset to mark the unused ones.
294 *
295 * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
296 * Setup") for definitions of the texture parameters.
297 */
298struct vc4_texture_sample_info {
299 bool is_direct;
300 uint32_t p_offset[4];
301};
302
303/**
304 * struct vc4_validated_shader_info - information about validated shaders that
305 * needs to be used from command list validation.
306 *
307 * For a given shader, each time a shader state record references it, we need
308 * to verify that the shader doesn't read more uniforms than the shader state
309 * record's uniform BO pointer can provide, and we need to apply relocations
310 * and validate the shader state record's uniforms that define the texture
311 * samples.
312 */
313struct vc4_validated_shader_info {
314 uint32_t uniforms_size;
315 uint32_t uniforms_src_size;
316 uint32_t num_texture_samples;
317 struct vc4_texture_sample_info *texture_samples;
318};
319
320/**
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800321 * _wait_for - magic (register) wait macro
322 *
323 * Does the right thing for modeset paths when run under kdgb or similar atomic
324 * contexts. Note that it's important that we check the condition again after
325 * having timed out, since the timeout could be due to preemption or similar and
326 * we've never had a chance to check the condition before the timeout.
327 */
328#define _wait_for(COND, MS, W) ({ \
329 unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1; \
330 int ret__ = 0; \
331 while (!(COND)) { \
332 if (time_after(jiffies, timeout__)) { \
333 if (!(COND)) \
334 ret__ = -ETIMEDOUT; \
335 break; \
336 } \
337 if (W && drm_can_sleep()) { \
338 msleep(W); \
339 } else { \
340 cpu_relax(); \
341 } \
342 } \
343 ret__; \
344})
345
346#define wait_for(COND, MS) _wait_for(COND, MS, 1)
347
348/* vc4_bo.c */
Eric Anholtc826a6e2015-10-09 20:25:07 -0700349struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800350void vc4_free_object(struct drm_gem_object *gem_obj);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700351struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
352 bool from_cache);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800353int vc4_dumb_create(struct drm_file *file_priv,
354 struct drm_device *dev,
355 struct drm_mode_create_dumb *args);
356struct dma_buf *vc4_prime_export(struct drm_device *dev,
357 struct drm_gem_object *obj, int flags);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300358int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
359 struct drm_file *file_priv);
Eric Anholt463873d2015-11-30 11:41:40 -0800360int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
361 struct drm_file *file_priv);
Eric Anholtd5bc60f2015-01-18 09:33:17 +1300362int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
363 struct drm_file *file_priv);
Eric Anholt463873d2015-11-30 11:41:40 -0800364int vc4_mmap(struct file *filp, struct vm_area_struct *vma);
365int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
366void *vc4_prime_vmap(struct drm_gem_object *obj);
Eric Anholtc826a6e2015-10-09 20:25:07 -0700367void vc4_bo_cache_init(struct drm_device *dev);
368void vc4_bo_cache_destroy(struct drm_device *dev);
369int vc4_bo_stats_debugfs(struct seq_file *m, void *arg);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800370
371/* vc4_crtc.c */
372extern struct platform_driver vc4_crtc_driver;
Dave Airlie1f437102015-10-22 10:23:31 +1000373int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id);
374void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id);
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800375void vc4_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file);
376int vc4_crtc_debugfs_regs(struct seq_file *m, void *arg);
377
378/* vc4_debugfs.c */
379int vc4_debugfs_init(struct drm_minor *minor);
380void vc4_debugfs_cleanup(struct drm_minor *minor);
381
382/* vc4_drv.c */
383void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
384
Eric Anholtd5b1a782015-11-30 12:13:37 -0800385/* vc4_gem.c */
386void vc4_gem_init(struct drm_device *dev);
387void vc4_gem_destroy(struct drm_device *dev);
388int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
389 struct drm_file *file_priv);
390int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
391 struct drm_file *file_priv);
392int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
393 struct drm_file *file_priv);
394void vc4_submit_next_job(struct drm_device *dev);
395int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
396 uint64_t timeout_ns, bool interruptible);
397void vc4_job_handle_completed(struct vc4_dev *vc4);
Eric Anholtb501bac2015-11-30 12:34:01 -0800398int vc4_queue_seqno_cb(struct drm_device *dev,
399 struct vc4_seqno_cb *cb, uint64_t seqno,
400 void (*func)(struct vc4_seqno_cb *cb));
Eric Anholtd5b1a782015-11-30 12:13:37 -0800401
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800402/* vc4_hdmi.c */
403extern struct platform_driver vc4_hdmi_driver;
404int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused);
405
Eric Anholtd5b1a782015-11-30 12:13:37 -0800406/* vc4_irq.c */
407irqreturn_t vc4_irq(int irq, void *arg);
408void vc4_irq_preinstall(struct drm_device *dev);
409int vc4_irq_postinstall(struct drm_device *dev);
410void vc4_irq_uninstall(struct drm_device *dev);
411void vc4_irq_reset(struct drm_device *dev);
412
Eric Anholtc8b75bc2015-03-02 13:01:12 -0800413/* vc4_hvs.c */
414extern struct platform_driver vc4_hvs_driver;
415void vc4_hvs_dump_state(struct drm_device *dev);
416int vc4_hvs_debugfs_regs(struct seq_file *m, void *unused);
417
418/* vc4_kms.c */
419int vc4_kms_load(struct drm_device *dev);
420
421/* vc4_plane.c */
422struct drm_plane *vc4_plane_init(struct drm_device *dev,
423 enum drm_plane_type type);
424u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
425u32 vc4_plane_dlist_size(struct drm_plane_state *state);
Eric Anholtb501bac2015-11-30 12:34:01 -0800426void vc4_plane_async_set_fb(struct drm_plane *plane,
427 struct drm_framebuffer *fb);
Eric Anholt463873d2015-11-30 11:41:40 -0800428
Eric Anholtd3f51682015-03-02 13:01:12 -0800429/* vc4_v3d.c */
430extern struct platform_driver vc4_v3d_driver;
431int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused);
432int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused);
Eric Anholtd5b1a782015-11-30 12:13:37 -0800433int vc4_v3d_set_power(struct vc4_dev *vc4, bool on);
434
435/* vc4_validate.c */
436int
437vc4_validate_bin_cl(struct drm_device *dev,
438 void *validated,
439 void *unvalidated,
440 struct vc4_exec_info *exec);
441
442int
443vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
444
445struct drm_gem_cma_object *vc4_use_bo(struct vc4_exec_info *exec,
446 uint32_t hindex);
447
448int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
449
450bool vc4_check_tex_size(struct vc4_exec_info *exec,
451 struct drm_gem_cma_object *fbo,
452 uint32_t offset, uint8_t tiling_format,
453 uint32_t width, uint32_t height, uint8_t cpp);
Eric Anholtd3f51682015-03-02 13:01:12 -0800454
Eric Anholt463873d2015-11-30 11:41:40 -0800455/* vc4_validate_shader.c */
456struct vc4_validated_shader_info *
457vc4_validate_shader(struct drm_gem_cma_object *shader_obj);