1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
3 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
4
5 #ifndef __PANFROST_DEVICE_H__
6 #define __PANFROST_DEVICE_H__
7
8 #include <linux/atomic.h>
9 #include <linux/io-pgtable.h>
10 #include <linux/regulator/consumer.h>
11 #include <linux/spinlock.h>
12 #include <drm/drm_device.h>
13 #include <drm/drm_mm.h>
14 #include <drm/gpu_scheduler.h>
15
16 #include "panfrost_devfreq.h"
17
18 struct panfrost_device;
19 struct panfrost_mmu;
20 struct panfrost_job_slot;
21 struct panfrost_job;
22 struct panfrost_perfcnt;
23
24 #define NUM_JOB_SLOTS 3
25 #define MAX_PM_DOMAINS 3
26
27 struct panfrost_features {
28 u16 id;
29 u16 revision;
30
31 u64 shader_present;
32 u64 tiler_present;
33 u64 l2_present;
34 u64 stack_present;
35 u32 as_present;
36 u32 js_present;
37
38 u32 l2_features;
39 u32 core_features;
40 u32 tiler_features;
41 u32 mem_features;
42 u32 mmu_features;
43 u32 thread_features;
44 u32 max_threads;
45 u32 thread_max_workgroup_sz;
46 u32 thread_max_barrier_sz;
47 u32 coherency_features;
48 u32 texture_features[4];
49 u32 js_features[16];
50
51 u32 nr_core_groups;
52 u32 thread_tls_alloc;
53 #if IS_ENABLED(CONFIG_ARCH_SUN50IW9)
54 u32 afbc_features;
55 #endif
56 unsigned long hw_features[64 / BITS_PER_LONG];
57 unsigned long hw_issues[64 / BITS_PER_LONG];
58 };
59
60 /*
61 * Features that cannot be automatically detected and need matching using the
62 * compatible string, typically SoC-specific.
63 */
64 struct panfrost_compatible {
65 /* Supplies count and names. */
66 int num_supplies;
67 const char * const *supply_names;
68 /*
69 * Number of power domains required, note that values 0 and 1 are
70 * handled identically, as only values > 1 need special handling.
71 */
72 int num_pm_domains;
73 /* Only required if num_pm_domains > 1. */
74 const char * const *pm_domain_names;
75
76 /* Vendor implementation quirks callback */
77 void (*vendor_quirk)(struct panfrost_device *pfdev);
78 };
79
80 struct panfrost_device {
81 struct device *dev;
82 struct drm_device *ddev;
83 struct platform_device *pdev;
84
85 void __iomem *iomem;
86 struct clk *clock;
87 struct clk *bus_clock;
88 struct regulator_bulk_data *regulators;
89 struct reset_control *rstc;
90 /* pm_domains for devices with more than one. */
91 struct device *pm_domain_devs[MAX_PM_DOMAINS];
92 struct device_link *pm_domain_links[MAX_PM_DOMAINS];
93 bool coherent;
94
95 struct panfrost_features features;
96 const struct panfrost_compatible *comp;
97
98 spinlock_t as_lock;
99 unsigned long as_in_use_mask;
100 unsigned long as_alloc_mask;
101 struct list_head as_lru_list;
102
103 struct panfrost_job_slot *js;
104
105 struct panfrost_job *jobs[NUM_JOB_SLOTS];
106 struct list_head scheduled_jobs;
107
108 struct panfrost_perfcnt *perfcnt;
109
110 struct mutex sched_lock;
111
112 struct {
113 struct work_struct work;
114 atomic_t pending;
115 } reset;
116
117 struct mutex shrinker_lock;
118 struct list_head shrinker_list;
119 struct shrinker shrinker;
120
121 struct panfrost_devfreq pfdevfreq;
122
123 struct clk *parent_clock;
124 struct clk *bak_clock;
125 };
126
127 struct panfrost_mmu {
128 struct panfrost_device *pfdev;
129 struct kref refcount;
130 struct io_pgtable_cfg pgtbl_cfg;
131 struct io_pgtable_ops *pgtbl_ops;
132 struct drm_mm mm;
133 spinlock_t mm_lock;
134 int as;
135 atomic_t as_count;
136 struct list_head list;
137 };
138
139 struct panfrost_file_priv {
140 struct panfrost_device *pfdev;
141
142 struct drm_sched_entity sched_entity[NUM_JOB_SLOTS];
143
144 struct panfrost_mmu *mmu;
145 };
146
to_panfrost_device(struct drm_device * ddev)147 static inline struct panfrost_device *to_panfrost_device(struct drm_device *ddev)
148 {
149 return ddev->dev_private;
150 }
151
panfrost_model_cmp(struct panfrost_device * pfdev,s32 id)152 static inline int panfrost_model_cmp(struct panfrost_device *pfdev, s32 id)
153 {
154 s32 match_id = pfdev->features.id;
155
156 if (match_id & 0xf000)
157 match_id &= 0xf00f;
158 return match_id - id;
159 }
160
panfrost_model_is_bifrost(struct panfrost_device * pfdev)161 static inline bool panfrost_model_is_bifrost(struct panfrost_device *pfdev)
162 {
163 return panfrost_model_cmp(pfdev, 0x1000) >= 0;
164 }
165
panfrost_model_eq(struct panfrost_device * pfdev,s32 id)166 static inline bool panfrost_model_eq(struct panfrost_device *pfdev, s32 id)
167 {
168 return !panfrost_model_cmp(pfdev, id);
169 }
170
171 int panfrost_unstable_ioctl_check(void);
172
173 int panfrost_device_init(struct panfrost_device *pfdev);
174 void panfrost_device_fini(struct panfrost_device *pfdev);
175 void panfrost_device_reset(struct panfrost_device *pfdev);
176
177 int panfrost_device_resume(struct device *dev);
178 int panfrost_device_suspend(struct device *dev);
179
180 const char *panfrost_exception_name(struct panfrost_device *pfdev, u32 exception_code);
181
182 #endif
183