xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_drv.h (revision d5bc60f6)
1c8b75bcaSEric Anholt /*
2c8b75bcaSEric Anholt  * Copyright (C) 2015 Broadcom
3c8b75bcaSEric Anholt  *
4c8b75bcaSEric Anholt  * This program is free software; you can redistribute it and/or modify
5c8b75bcaSEric Anholt  * it under the terms of the GNU General Public License version 2 as
6c8b75bcaSEric Anholt  * published by the Free Software Foundation.
7c8b75bcaSEric Anholt  */
8c8b75bcaSEric Anholt 
9c8b75bcaSEric Anholt #include "drmP.h"
10c8b75bcaSEric Anholt #include "drm_gem_cma_helper.h"
11c8b75bcaSEric Anholt 
12c8b75bcaSEric Anholt struct vc4_dev {
13c8b75bcaSEric Anholt 	struct drm_device *dev;
14c8b75bcaSEric Anholt 
15c8b75bcaSEric Anholt 	struct vc4_hdmi *hdmi;
16c8b75bcaSEric Anholt 	struct vc4_hvs *hvs;
17c8b75bcaSEric Anholt 	struct vc4_crtc *crtc[3];
1848666d56SDerek Foreman 
1948666d56SDerek Foreman 	struct drm_fbdev_cma *fbdev;
20c826a6e1SEric Anholt 
21c826a6e1SEric Anholt 	/* The kernel-space BO cache.  Tracks buffers that have been
22c826a6e1SEric Anholt 	 * unreferenced by all other users (refcounts of 0!) but not
23c826a6e1SEric Anholt 	 * yet freed, so we can do cheap allocations.
24c826a6e1SEric Anholt 	 */
25c826a6e1SEric Anholt 	struct vc4_bo_cache {
26c826a6e1SEric Anholt 		/* Array of list heads for entries in the BO cache,
27c826a6e1SEric Anholt 		 * based on number of pages, so we can do O(1) lookups
28c826a6e1SEric Anholt 		 * in the cache when allocating.
29c826a6e1SEric Anholt 		 */
30c826a6e1SEric Anholt 		struct list_head *size_list;
31c826a6e1SEric Anholt 		uint32_t size_list_size;
32c826a6e1SEric Anholt 
33c826a6e1SEric Anholt 		/* List of all BOs in the cache, ordered by age, so we
34c826a6e1SEric Anholt 		 * can do O(1) lookups when trying to free old
35c826a6e1SEric Anholt 		 * buffers.
36c826a6e1SEric Anholt 		 */
37c826a6e1SEric Anholt 		struct list_head time_list;
38c826a6e1SEric Anholt 		struct work_struct time_work;
39c826a6e1SEric Anholt 		struct timer_list time_timer;
40c826a6e1SEric Anholt 	} bo_cache;
41c826a6e1SEric Anholt 
42c826a6e1SEric Anholt 	struct vc4_bo_stats {
43c826a6e1SEric Anholt 		u32 num_allocated;
44c826a6e1SEric Anholt 		u32 size_allocated;
45c826a6e1SEric Anholt 		u32 num_cached;
46c826a6e1SEric Anholt 		u32 size_cached;
47c826a6e1SEric Anholt 	} bo_stats;
48c826a6e1SEric Anholt 
49c826a6e1SEric Anholt 	/* Protects bo_cache and the BO stats. */
50c826a6e1SEric Anholt 	struct mutex bo_lock;
51c8b75bcaSEric Anholt };
52c8b75bcaSEric Anholt 
53c8b75bcaSEric Anholt static inline struct vc4_dev *
54c8b75bcaSEric Anholt to_vc4_dev(struct drm_device *dev)
55c8b75bcaSEric Anholt {
56c8b75bcaSEric Anholt 	return (struct vc4_dev *)dev->dev_private;
57c8b75bcaSEric Anholt }
58c8b75bcaSEric Anholt 
59c8b75bcaSEric Anholt struct vc4_bo {
60c8b75bcaSEric Anholt 	struct drm_gem_cma_object base;
61c826a6e1SEric Anholt 
62c826a6e1SEric Anholt 	/* List entry for the BO's position in either
63c826a6e1SEric Anholt 	 * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
64c826a6e1SEric Anholt 	 */
65c826a6e1SEric Anholt 	struct list_head unref_head;
66c826a6e1SEric Anholt 
67c826a6e1SEric Anholt 	/* Time in jiffies when the BO was put in vc4->bo_cache. */
68c826a6e1SEric Anholt 	unsigned long free_time;
69c826a6e1SEric Anholt 
70c826a6e1SEric Anholt 	/* List entry for the BO's position in vc4_dev->bo_cache.size_list */
71c826a6e1SEric Anholt 	struct list_head size_head;
72c8b75bcaSEric Anholt };
73c8b75bcaSEric Anholt 
74c8b75bcaSEric Anholt static inline struct vc4_bo *
75c8b75bcaSEric Anholt to_vc4_bo(struct drm_gem_object *bo)
76c8b75bcaSEric Anholt {
77c8b75bcaSEric Anholt 	return (struct vc4_bo *)bo;
78c8b75bcaSEric Anholt }
79c8b75bcaSEric Anholt 
80c8b75bcaSEric Anholt struct vc4_hvs {
81c8b75bcaSEric Anholt 	struct platform_device *pdev;
82c8b75bcaSEric Anholt 	void __iomem *regs;
83c8b75bcaSEric Anholt 	void __iomem *dlist;
84c8b75bcaSEric Anholt };
85c8b75bcaSEric Anholt 
86c8b75bcaSEric Anholt struct vc4_plane {
87c8b75bcaSEric Anholt 	struct drm_plane base;
88c8b75bcaSEric Anholt };
89c8b75bcaSEric Anholt 
90c8b75bcaSEric Anholt static inline struct vc4_plane *
91c8b75bcaSEric Anholt to_vc4_plane(struct drm_plane *plane)
92c8b75bcaSEric Anholt {
93c8b75bcaSEric Anholt 	return (struct vc4_plane *)plane;
94c8b75bcaSEric Anholt }
95c8b75bcaSEric Anholt 
96c8b75bcaSEric Anholt enum vc4_encoder_type {
97c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_HDMI,
98c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_VEC,
99c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_DSI0,
100c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_DSI1,
101c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_SMI,
102c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_DPI,
103c8b75bcaSEric Anholt };
104c8b75bcaSEric Anholt 
105c8b75bcaSEric Anholt struct vc4_encoder {
106c8b75bcaSEric Anholt 	struct drm_encoder base;
107c8b75bcaSEric Anholt 	enum vc4_encoder_type type;
108c8b75bcaSEric Anholt 	u32 clock_select;
109c8b75bcaSEric Anholt };
110c8b75bcaSEric Anholt 
111c8b75bcaSEric Anholt static inline struct vc4_encoder *
112c8b75bcaSEric Anholt to_vc4_encoder(struct drm_encoder *encoder)
113c8b75bcaSEric Anholt {
114c8b75bcaSEric Anholt 	return container_of(encoder, struct vc4_encoder, base);
115c8b75bcaSEric Anholt }
116c8b75bcaSEric Anholt 
117c8b75bcaSEric Anholt #define HVS_READ(offset) readl(vc4->hvs->regs + offset)
118c8b75bcaSEric Anholt #define HVS_WRITE(offset, val) writel(val, vc4->hvs->regs + offset)
119c8b75bcaSEric Anholt 
120c8b75bcaSEric Anholt /**
121c8b75bcaSEric Anholt  * _wait_for - magic (register) wait macro
122c8b75bcaSEric Anholt  *
123c8b75bcaSEric Anholt  * Does the right thing for modeset paths when run under kdgb or similar atomic
124c8b75bcaSEric Anholt  * contexts. Note that it's important that we check the condition again after
125c8b75bcaSEric Anholt  * having timed out, since the timeout could be due to preemption or similar and
126c8b75bcaSEric Anholt  * we've never had a chance to check the condition before the timeout.
127c8b75bcaSEric Anholt  */
128c8b75bcaSEric Anholt #define _wait_for(COND, MS, W) ({ \
129c8b75bcaSEric Anholt 	unsigned long timeout__ = jiffies + msecs_to_jiffies(MS) + 1;	\
130c8b75bcaSEric Anholt 	int ret__ = 0;							\
131c8b75bcaSEric Anholt 	while (!(COND)) {						\
132c8b75bcaSEric Anholt 		if (time_after(jiffies, timeout__)) {			\
133c8b75bcaSEric Anholt 			if (!(COND))					\
134c8b75bcaSEric Anholt 				ret__ = -ETIMEDOUT;			\
135c8b75bcaSEric Anholt 			break;						\
136c8b75bcaSEric Anholt 		}							\
137c8b75bcaSEric Anholt 		if (W && drm_can_sleep())  {				\
138c8b75bcaSEric Anholt 			msleep(W);					\
139c8b75bcaSEric Anholt 		} else {						\
140c8b75bcaSEric Anholt 			cpu_relax();					\
141c8b75bcaSEric Anholt 		}							\
142c8b75bcaSEric Anholt 	}								\
143c8b75bcaSEric Anholt 	ret__;								\
144c8b75bcaSEric Anholt })
145c8b75bcaSEric Anholt 
146c8b75bcaSEric Anholt #define wait_for(COND, MS) _wait_for(COND, MS, 1)
147c8b75bcaSEric Anholt 
148c8b75bcaSEric Anholt /* vc4_bo.c */
149c826a6e1SEric Anholt struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
150c8b75bcaSEric Anholt void vc4_free_object(struct drm_gem_object *gem_obj);
151c826a6e1SEric Anholt struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
152c826a6e1SEric Anholt 			     bool from_cache);
153c8b75bcaSEric Anholt int vc4_dumb_create(struct drm_file *file_priv,
154c8b75bcaSEric Anholt 		    struct drm_device *dev,
155c8b75bcaSEric Anholt 		    struct drm_mode_create_dumb *args);
156c8b75bcaSEric Anholt struct dma_buf *vc4_prime_export(struct drm_device *dev,
157c8b75bcaSEric Anholt 				 struct drm_gem_object *obj, int flags);
158d5bc60f6SEric Anholt int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
159d5bc60f6SEric Anholt 			struct drm_file *file_priv);
160d5bc60f6SEric Anholt int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
161d5bc60f6SEric Anholt 		      struct drm_file *file_priv);
162c826a6e1SEric Anholt void vc4_bo_cache_init(struct drm_device *dev);
163c826a6e1SEric Anholt void vc4_bo_cache_destroy(struct drm_device *dev);
164c826a6e1SEric Anholt int vc4_bo_stats_debugfs(struct seq_file *m, void *arg);
165c8b75bcaSEric Anholt 
166c8b75bcaSEric Anholt /* vc4_crtc.c */
167c8b75bcaSEric Anholt extern struct platform_driver vc4_crtc_driver;
1681f43710aSDave Airlie int vc4_enable_vblank(struct drm_device *dev, unsigned int crtc_id);
1691f43710aSDave Airlie void vc4_disable_vblank(struct drm_device *dev, unsigned int crtc_id);
170c8b75bcaSEric Anholt void vc4_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file);
171c8b75bcaSEric Anholt int vc4_crtc_debugfs_regs(struct seq_file *m, void *arg);
172c8b75bcaSEric Anholt 
173c8b75bcaSEric Anholt /* vc4_debugfs.c */
174c8b75bcaSEric Anholt int vc4_debugfs_init(struct drm_minor *minor);
175c8b75bcaSEric Anholt void vc4_debugfs_cleanup(struct drm_minor *minor);
176c8b75bcaSEric Anholt 
177c8b75bcaSEric Anholt /* vc4_drv.c */
178c8b75bcaSEric Anholt void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
179c8b75bcaSEric Anholt 
180c8b75bcaSEric Anholt /* vc4_hdmi.c */
181c8b75bcaSEric Anholt extern struct platform_driver vc4_hdmi_driver;
182c8b75bcaSEric Anholt int vc4_hdmi_debugfs_regs(struct seq_file *m, void *unused);
183c8b75bcaSEric Anholt 
184c8b75bcaSEric Anholt /* vc4_hvs.c */
185c8b75bcaSEric Anholt extern struct platform_driver vc4_hvs_driver;
186c8b75bcaSEric Anholt void vc4_hvs_dump_state(struct drm_device *dev);
187c8b75bcaSEric Anholt int vc4_hvs_debugfs_regs(struct seq_file *m, void *unused);
188c8b75bcaSEric Anholt 
189c8b75bcaSEric Anholt /* vc4_kms.c */
190c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev);
191c8b75bcaSEric Anholt 
192c8b75bcaSEric Anholt /* vc4_plane.c */
193c8b75bcaSEric Anholt struct drm_plane *vc4_plane_init(struct drm_device *dev,
194c8b75bcaSEric Anholt 				 enum drm_plane_type type);
195c8b75bcaSEric Anholt u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
196c8b75bcaSEric Anholt u32 vc4_plane_dlist_size(struct drm_plane_state *state);
197