]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/vc4/vc4_drv.h
drm/vc4: fix include notation and remove -Iinclude/drm flag
[karo-tx-linux.git] / drivers / gpu / drm / vc4 / vc4_drv.h
1 /*
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 <linux/reservation.h>
10 #include <drm/drmP.h>
11 #include <drm/drm_encoder.h>
12 #include <drm/drm_gem_cma_helper.h>
13
14 struct vc4_dev {
15         struct drm_device *dev;
16
17         struct vc4_hdmi *hdmi;
18         struct vc4_hvs *hvs;
19         struct vc4_v3d *v3d;
20         struct vc4_dpi *dpi;
21         struct vc4_dsi *dsi1;
22         struct vc4_vec *vec;
23
24         struct drm_fbdev_cma *fbdev;
25
26         struct vc4_hang_state *hang_state;
27
28         /* The kernel-space BO cache.  Tracks buffers that have been
29          * unreferenced by all other users (refcounts of 0!) but not
30          * yet freed, so we can do cheap allocations.
31          */
32         struct vc4_bo_cache {
33                 /* Array of list heads for entries in the BO cache,
34                  * based on number of pages, so we can do O(1) lookups
35                  * in the cache when allocating.
36                  */
37                 struct list_head *size_list;
38                 uint32_t size_list_size;
39
40                 /* List of all BOs in the cache, ordered by age, so we
41                  * can do O(1) lookups when trying to free old
42                  * buffers.
43                  */
44                 struct list_head time_list;
45                 struct work_struct time_work;
46                 struct timer_list time_timer;
47         } bo_cache;
48
49         struct vc4_bo_stats {
50                 u32 num_allocated;
51                 u32 size_allocated;
52                 u32 num_cached;
53                 u32 size_cached;
54         } bo_stats;
55
56         /* Protects bo_cache and the BO stats. */
57         struct mutex bo_lock;
58
59         uint64_t dma_fence_context;
60
61         /* Sequence number for the last job queued in bin_job_list.
62          * Starts at 0 (no jobs emitted).
63          */
64         uint64_t emit_seqno;
65
66         /* Sequence number for the last completed job on the GPU.
67          * Starts at 0 (no jobs completed).
68          */
69         uint64_t finished_seqno;
70
71         /* List of all struct vc4_exec_info for jobs to be executed in
72          * the binner.  The first job in the list is the one currently
73          * programmed into ct0ca for execution.
74          */
75         struct list_head bin_job_list;
76
77         /* List of all struct vc4_exec_info for jobs that have
78          * completed binning and are ready for rendering.  The first
79          * job in the list is the one currently programmed into ct1ca
80          * for execution.
81          */
82         struct list_head render_job_list;
83
84         /* List of the finished vc4_exec_infos waiting to be freed by
85          * job_done_work.
86          */
87         struct list_head job_done_list;
88         /* Spinlock used to synchronize the job_list and seqno
89          * accesses between the IRQ handler and GEM ioctls.
90          */
91         spinlock_t job_lock;
92         wait_queue_head_t job_wait_queue;
93         struct work_struct job_done_work;
94
95         /* List of struct vc4_seqno_cb for callbacks to be made from a
96          * workqueue when the given seqno is passed.
97          */
98         struct list_head seqno_cb_list;
99
100         /* The memory used for storing binner tile alloc, tile state,
101          * and overflow memory allocations.  This is freed when V3D
102          * powers down.
103          */
104         struct vc4_bo *bin_bo;
105
106         /* Size of blocks allocated within bin_bo. */
107         uint32_t bin_alloc_size;
108
109         /* Bitmask of the bin_alloc_size chunks in bin_bo that are
110          * used.
111          */
112         uint32_t bin_alloc_used;
113
114         /* Bitmask of the current bin_alloc used for overflow memory. */
115         uint32_t bin_alloc_overflow;
116
117         struct work_struct overflow_mem_work;
118
119         int power_refcount;
120
121         /* Mutex controlling the power refcount. */
122         struct mutex power_lock;
123
124         struct {
125                 struct timer_list timer;
126                 struct work_struct reset_work;
127         } hangcheck;
128
129         struct semaphore async_modeset;
130 };
131
132 static inline struct vc4_dev *
133 to_vc4_dev(struct drm_device *dev)
134 {
135         return (struct vc4_dev *)dev->dev_private;
136 }
137
138 struct vc4_bo {
139         struct drm_gem_cma_object base;
140
141         /* seqno of the last job to render using this BO. */
142         uint64_t seqno;
143
144         /* seqno of the last job to use the RCL to write to this BO.
145          *
146          * Note that this doesn't include binner overflow memory
147          * writes.
148          */
149         uint64_t write_seqno;
150
151         /* List entry for the BO's position in either
152          * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
153          */
154         struct list_head unref_head;
155
156         /* Time in jiffies when the BO was put in vc4->bo_cache. */
157         unsigned long free_time;
158
159         /* List entry for the BO's position in vc4_dev->bo_cache.size_list */
160         struct list_head size_head;
161
162         /* Struct for shader validation state, if created by
163          * DRM_IOCTL_VC4_CREATE_SHADER_BO.
164          */
165         struct vc4_validated_shader_info *validated_shader;
166
167         /* normally (resv == &_resv) except for imported bo's */
168         struct reservation_object *resv;
169         struct reservation_object _resv;
170 };
171
172 static inline struct vc4_bo *
173 to_vc4_bo(struct drm_gem_object *bo)
174 {
175         return (struct vc4_bo *)bo;
176 }
177
178 struct vc4_fence {
179         struct dma_fence base;
180         struct drm_device *dev;
181         /* vc4 seqno for signaled() test */
182         uint64_t seqno;
183 };
184
185 static inline struct vc4_fence *
186 to_vc4_fence(struct dma_fence *fence)
187 {
188         return (struct vc4_fence *)fence;
189 }
190
191 struct vc4_seqno_cb {
192         struct work_struct work;
193         uint64_t seqno;
194         void (*func)(struct vc4_seqno_cb *cb);
195 };
196
197 struct vc4_v3d {
198         struct vc4_dev *vc4;
199         struct platform_device *pdev;
200         void __iomem *regs;
201         struct clk *clk;
202 };
203
204 struct vc4_hvs {
205         struct platform_device *pdev;
206         void __iomem *regs;
207         u32 __iomem *dlist;
208
209         /* Memory manager for CRTCs to allocate space in the display
210          * list.  Units are dwords.
211          */
212         struct drm_mm dlist_mm;
213         /* Memory manager for the LBM memory used by HVS scaling. */
214         struct drm_mm lbm_mm;
215         spinlock_t mm_lock;
216
217         struct drm_mm_node mitchell_netravali_filter;
218 };
219
220 struct vc4_plane {
221         struct drm_plane base;
222 };
223
224 static inline struct vc4_plane *
225 to_vc4_plane(struct drm_plane *plane)
226 {
227         return (struct vc4_plane *)plane;
228 }
229
230 enum vc4_encoder_type {
231         VC4_ENCODER_TYPE_NONE,
232         VC4_ENCODER_TYPE_HDMI,
233         VC4_ENCODER_TYPE_VEC,
234         VC4_ENCODER_TYPE_DSI0,
235         VC4_ENCODER_TYPE_DSI1,
236         VC4_ENCODER_TYPE_SMI,
237         VC4_ENCODER_TYPE_DPI,
238 };
239
240 struct vc4_encoder {
241         struct drm_encoder base;
242         enum vc4_encoder_type type;
243         u32 clock_select;
244 };
245
246 static inline struct vc4_encoder *
247 to_vc4_encoder(struct drm_encoder *encoder)
248 {
249         return container_of(encoder, struct vc4_encoder, base);
250 }
251
252 #define V3D_READ(offset) readl(vc4->v3d->regs + offset)
253 #define V3D_WRITE(offset, val) writel(val, vc4->v3d->regs + offset)
254 #define HVS_READ(offset) readl(vc4->hvs->regs + offset)
255 #define HVS_WRITE(offset, val) writel(val, vc4->hvs->regs + offset)
256
257 struct vc4_exec_info {
258         /* Sequence number for this bin/render job. */
259         uint64_t seqno;
260
261         /* Latest write_seqno of any BO that binning depends on. */
262         uint64_t bin_dep_seqno;
263
264         struct dma_fence *fence;
265
266         /* Last current addresses the hardware was processing when the
267          * hangcheck timer checked on us.
268          */
269         uint32_t last_ct0ca, last_ct1ca;
270
271         /* Kernel-space copy of the ioctl arguments */
272         struct drm_vc4_submit_cl *args;
273
274         /* This is the array of BOs that were looked up at the start of exec.
275          * Command validation will use indices into this array.
276          */
277         struct drm_gem_cma_object **bo;
278         uint32_t bo_count;
279
280         /* List of BOs that are being written by the RCL.  Other than
281          * the binner temporary storage, this is all the BOs written
282          * by the job.
283          */
284         struct drm_gem_cma_object *rcl_write_bo[4];
285         uint32_t rcl_write_bo_count;
286
287         /* Pointers for our position in vc4->job_list */
288         struct list_head head;
289
290         /* List of other BOs used in the job that need to be released
291          * once the job is complete.
292          */
293         struct list_head unref_list;
294
295         /* Current unvalidated indices into @bo loaded by the non-hardware
296          * VC4_PACKET_GEM_HANDLES.
297          */
298         uint32_t bo_index[2];
299
300         /* This is the BO where we store the validated command lists, shader
301          * records, and uniforms.
302          */
303         struct drm_gem_cma_object *exec_bo;
304
305         /**
306          * This tracks the per-shader-record state (packet 64) that
307          * determines the length of the shader record and the offset
308          * it's expected to be found at.  It gets read in from the
309          * command lists.
310          */
311         struct vc4_shader_state {
312                 uint32_t addr;
313                 /* Maximum vertex index referenced by any primitive using this
314                  * shader state.
315                  */
316                 uint32_t max_index;
317         } *shader_state;
318
319         /** How many shader states the user declared they were using. */
320         uint32_t shader_state_size;
321         /** How many shader state records the validator has seen. */
322         uint32_t shader_state_count;
323
324         bool found_tile_binning_mode_config_packet;
325         bool found_start_tile_binning_packet;
326         bool found_increment_semaphore_packet;
327         bool found_flush;
328         uint8_t bin_tiles_x, bin_tiles_y;
329         /* Physical address of the start of the tile alloc array
330          * (where each tile's binned CL will start)
331          */
332         uint32_t tile_alloc_offset;
333         /* Bitmask of which binner slots are freed when this job completes. */
334         uint32_t bin_slots;
335
336         /**
337          * Computed addresses pointing into exec_bo where we start the
338          * bin thread (ct0) and render thread (ct1).
339          */
340         uint32_t ct0ca, ct0ea;
341         uint32_t ct1ca, ct1ea;
342
343         /* Pointer to the unvalidated bin CL (if present). */
344         void *bin_u;
345
346         /* Pointers to the shader recs.  These paddr gets incremented as CL
347          * packets are relocated in validate_gl_shader_state, and the vaddrs
348          * (u and v) get incremented and size decremented as the shader recs
349          * themselves are validated.
350          */
351         void *shader_rec_u;
352         void *shader_rec_v;
353         uint32_t shader_rec_p;
354         uint32_t shader_rec_size;
355
356         /* Pointers to the uniform data.  These pointers are incremented, and
357          * size decremented, as each batch of uniforms is uploaded.
358          */
359         void *uniforms_u;
360         void *uniforms_v;
361         uint32_t uniforms_p;
362         uint32_t uniforms_size;
363 };
364
365 static inline struct vc4_exec_info *
366 vc4_first_bin_job(struct vc4_dev *vc4)
367 {
368         return list_first_entry_or_null(&vc4->bin_job_list,
369                                         struct vc4_exec_info, head);
370 }
371
372 static inline struct vc4_exec_info *
373 vc4_first_render_job(struct vc4_dev *vc4)
374 {
375         return list_first_entry_or_null(&vc4->render_job_list,
376                                         struct vc4_exec_info, head);
377 }
378
379 static inline struct vc4_exec_info *
380 vc4_last_render_job(struct vc4_dev *vc4)
381 {
382         if (list_empty(&vc4->render_job_list))
383                 return NULL;
384         return list_last_entry(&vc4->render_job_list,
385                                struct vc4_exec_info, head);
386 }
387
388 /**
389  * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
390  * setup parameters.
391  *
392  * This will be used at draw time to relocate the reference to the texture
393  * contents in p0, and validate that the offset combined with
394  * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
395  * Note that the hardware treats unprovided config parameters as 0, so not all
396  * of them need to be set up for every texure sample, and we'll store ~0 as
397  * the offset to mark the unused ones.
398  *
399  * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
400  * Setup") for definitions of the texture parameters.
401  */
402 struct vc4_texture_sample_info {
403         bool is_direct;
404         uint32_t p_offset[4];
405 };
406
407 /**
408  * struct vc4_validated_shader_info - information about validated shaders that
409  * needs to be used from command list validation.
410  *
411  * For a given shader, each time a shader state record references it, we need
412  * to verify that the shader doesn't read more uniforms than the shader state
413  * record's uniform BO pointer can provide, and we need to apply relocations
414  * and validate the shader state record's uniforms that define the texture
415  * samples.
416  */
417 struct vc4_validated_shader_info {
418         uint32_t uniforms_size;
419         uint32_t uniforms_src_size;
420         uint32_t num_texture_samples;
421         struct vc4_texture_sample_info *texture_samples;
422
423         uint32_t num_uniform_addr_offsets;
424         uint32_t *uniform_addr_offsets;
425
426         bool is_threaded;
427 };
428
429 /**
430  * _wait_for - magic (register) wait macro
431  *
432  * Does the right thing for modeset paths when run under kdgb or similar atomic
433  * contexts. Note that it's important that we check the condition again after
434  * having timed out, since the timeout could be due to preemption or similar and
435  * we've never had a chance to check the condition before the timeout.
436  */
437 #define _wait_for(COND, MS, W) ({ \
438         unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1;   \
439         int ret__ = 0;                                                  \
440         while (!(COND)) {                                               \
441                 if (time_after(jiffies, timeout__)) {                   \
442                         if (!(COND))                                    \
443                                 ret__ = -ETIMEDOUT;                     \
444                         break;                                          \
445                 }                                                       \
446                 if (W && drm_can_sleep())  {                            \
447                         msleep(W);                                      \
448                 } else {                                                \
449                         cpu_relax();                                    \
450                 }                                                       \
451         }                                                               \
452         ret__;                                                          \
453 })
454
455 #define wait_for(COND, MS) _wait_for(COND, MS, 1)
456
457 /* vc4_bo.c */
458 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
459 void vc4_free_object(struct drm_gem_object *gem_obj);
460 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
461                              bool from_cache);
462 int vc4_dumb_create(struct drm_file *file_priv,
463                     struct drm_device *dev,
464                     struct drm_mode_create_dumb *args);
465 struct dma_buf *vc4_prime_export(struct drm_device *dev,
466                                  struct drm_gem_object *obj, int flags);
467 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
468                         struct drm_file *file_priv);
469 int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
470                                struct drm_file *file_priv);
471 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
472                       struct drm_file *file_priv);
473 int vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
474                              struct drm_file *file_priv);
475 int vc4_mmap(struct file *filp, struct vm_area_struct *vma);
476 struct reservation_object *vc4_prime_res_obj(struct drm_gem_object *obj);
477 int vc4_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma);
478 struct drm_gem_object *vc4_prime_import_sg_table(struct drm_device *dev,
479                                                  struct dma_buf_attachment *attach,
480                                                  struct sg_table *sgt);
481 void *vc4_prime_vmap(struct drm_gem_object *obj);
482 void vc4_bo_cache_init(struct drm_device *dev);
483 void vc4_bo_cache_destroy(struct drm_device *dev);
484 int vc4_bo_stats_debugfs(struct seq_file *m, void *arg);
485
486 /* vc4_crtc.c */
487 extern struct platform_driver vc4_crtc_driver;
488 bool vc4_event_pending(struct drm_crtc *crtc);
489 int vc4_crtc_debugfs_regs(struct seq_file *m, void *arg);
490 bool vc4_crtc_get_scanoutpos(struct drm_device *dev, unsigned int crtc_id,
491                              bool in_vblank_irq, int *vpos, int *hpos,
492                              ktime_t *stime, ktime_t *etime,
493                              const struct drm_display_mode *mode);
494
495 /* vc4_debugfs.c */
496 int vc4_debugfs_init(struct drm_minor *minor);
497
498 /* vc4_drv.c */
499 void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
500
501 /* vc4_dpi.c */
502 extern struct platform_driver vc4_dpi_driver;
503 int vc4_dpi_debugfs_regs(struct seq_file *m, void *unused);
504
505 /* vc4_dsi.c */
506 extern struct platform_driver vc4_dsi_driver;
507 int vc4_dsi_debugfs_regs(struct seq_file *m, void *unused);
508
509 /* vc4_fence.c */
510 extern const struct dma_fence_ops vc4_fence_ops;
511
512 /* vc4_gem.c */
513 void vc4_gem_init(struct drm_device *dev);
514 void vc4_gem_destroy(struct drm_device *dev);
515 int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
516                         struct drm_file *file_priv);
517 int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
518                          struct drm_file *file_priv);
519 int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
520                       struct drm_file *file_priv);
521 void vc4_submit_next_bin_job(struct drm_device *dev);
522 void vc4_submit_next_render_job(struct drm_device *dev);
523 void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
524 int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
525                        uint64_t timeout_ns, bool interruptible);
526 void vc4_job_handle_completed(struct vc4_dev *vc4);
527 int vc4_queue_seqno_cb(struct drm_device *dev,
528                        struct vc4_seqno_cb *cb, uint64_t seqno,
529                        void (*func)(struct vc4_seqno_cb *cb));
530
531 /* vc4_hdmi.c */
532 extern struct platform_driver vc4_hdmi_driver;
533 int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused);
534
535 /* vc4_hdmi.c */
536 extern struct platform_driver vc4_vec_driver;
537 int vc4_vec_debugfs_regs(struct seq_file *m, void *unused);
538
539 /* vc4_irq.c */
540 irqreturn_t vc4_irq(int irq, void *arg);
541 void vc4_irq_preinstall(struct drm_device *dev);
542 int vc4_irq_postinstall(struct drm_device *dev);
543 void vc4_irq_uninstall(struct drm_device *dev);
544 void vc4_irq_reset(struct drm_device *dev);
545
546 /* vc4_hvs.c */
547 extern struct platform_driver vc4_hvs_driver;
548 void vc4_hvs_dump_state(struct drm_device *dev);
549 int vc4_hvs_debugfs_regs(struct seq_file *m, void *unused);
550
551 /* vc4_kms.c */
552 int vc4_kms_load(struct drm_device *dev);
553
554 /* vc4_plane.c */
555 struct drm_plane *vc4_plane_init(struct drm_device *dev,
556                                  enum drm_plane_type type);
557 u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
558 u32 vc4_plane_dlist_size(const struct drm_plane_state *state);
559 void vc4_plane_async_set_fb(struct drm_plane *plane,
560                             struct drm_framebuffer *fb);
561
562 /* vc4_v3d.c */
563 extern struct platform_driver vc4_v3d_driver;
564 int vc4_v3d_debugfs_ident(struct seq_file *m, void *unused);
565 int vc4_v3d_debugfs_regs(struct seq_file *m, void *unused);
566 int vc4_v3d_get_bin_slot(struct vc4_dev *vc4);
567
568 /* vc4_validate.c */
569 int
570 vc4_validate_bin_cl(struct drm_device *dev,
571                     void *validated,
572                     void *unvalidated,
573                     struct vc4_exec_info *exec);
574
575 int
576 vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
577
578 struct drm_gem_cma_object *vc4_use_bo(struct vc4_exec_info *exec,
579                                       uint32_t hindex);
580
581 int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
582
583 bool vc4_check_tex_size(struct vc4_exec_info *exec,
584                         struct drm_gem_cma_object *fbo,
585                         uint32_t offset, uint8_t tiling_format,
586                         uint32_t width, uint32_t height, uint8_t cpp);
587
588 /* vc4_validate_shader.c */
589 struct vc4_validated_shader_info *
590 vc4_validate_shader(struct drm_gem_cma_object *shader_obj);