1f3ba9122SRob Herring // SPDX-License-Identifier: GPL-2.0
2f3ba9122SRob Herring /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
3f3ba9122SRob Herring /* Copyright 2019 Linaro, Ltd., Rob Herring <robh@kernel.org> */
4f3ba9122SRob Herring /* Copyright 2019 Collabora ltd. */
5d9b631f0SRobin Murphy #include <linux/bitfield.h>
6f3ba9122SRob Herring #include <linux/bitmap.h>
7f3ba9122SRob Herring #include <linux/delay.h>
8d9b631f0SRobin Murphy #include <linux/dma-mapping.h>
9f3ba9122SRob Herring #include <linux/interrupt.h>
10f3ba9122SRob Herring #include <linux/io.h>
11f3ba9122SRob Herring #include <linux/iopoll.h>
12f3ba9122SRob Herring #include <linux/platform_device.h>
133a74265cSTomeu Vizoso #include <linux/pm_runtime.h>
14f3ba9122SRob Herring
15f3ba9122SRob Herring #include "panfrost_device.h"
16f3ba9122SRob Herring #include "panfrost_features.h"
17f3ba9122SRob Herring #include "panfrost_issues.h"
18f3ba9122SRob Herring #include "panfrost_gpu.h"
197786fd10SBoris Brezillon #include "panfrost_perfcnt.h"
20f3ba9122SRob Herring #include "panfrost_regs.h"
21f3ba9122SRob Herring
panfrost_gpu_irq_handler(int irq,void * data)22f3ba9122SRob Herring static irqreturn_t panfrost_gpu_irq_handler(int irq, void *data)
23f3ba9122SRob Herring {
24f3ba9122SRob Herring struct panfrost_device *pfdev = data;
25f3ba9122SRob Herring u32 state = gpu_read(pfdev, GPU_INT_STAT);
26f3ba9122SRob Herring u32 fault_status = gpu_read(pfdev, GPU_FAULT_STATUS);
27f3ba9122SRob Herring
28f3ba9122SRob Herring if (!state)
29f3ba9122SRob Herring return IRQ_NONE;
30f3ba9122SRob Herring
31f3ba9122SRob Herring if (state & GPU_IRQ_MASK_ERROR) {
32f3ba9122SRob Herring u64 address = (u64) gpu_read(pfdev, GPU_FAULT_ADDRESS_HI) << 32;
33f3ba9122SRob Herring address |= gpu_read(pfdev, GPU_FAULT_ADDRESS_LO);
34f3ba9122SRob Herring
35f3ba9122SRob Herring dev_warn(pfdev->dev, "GPU Fault 0x%08x (%s) at 0x%016llx\n",
367dc924d7SChunyouTang fault_status, panfrost_exception_name(fault_status & 0xFF),
37f3ba9122SRob Herring address);
38f3ba9122SRob Herring
39f3ba9122SRob Herring if (state & GPU_IRQ_MULTIPLE_FAULT)
40f3ba9122SRob Herring dev_warn(pfdev->dev, "There were multiple GPU faults - some have not been reported\n");
41f3ba9122SRob Herring
42f3ba9122SRob Herring gpu_write(pfdev, GPU_INT_MASK, 0);
43f3ba9122SRob Herring }
44f3ba9122SRob Herring
457786fd10SBoris Brezillon if (state & GPU_IRQ_PERFCNT_SAMPLE_COMPLETED)
467786fd10SBoris Brezillon panfrost_perfcnt_sample_done(pfdev);
477786fd10SBoris Brezillon
487786fd10SBoris Brezillon if (state & GPU_IRQ_CLEAN_CACHES_COMPLETED)
497786fd10SBoris Brezillon panfrost_perfcnt_clean_cache_done(pfdev);
507786fd10SBoris Brezillon
51f3ba9122SRob Herring gpu_write(pfdev, GPU_INT_CLEAR, state);
52f3ba9122SRob Herring
53f3ba9122SRob Herring return IRQ_HANDLED;
54f3ba9122SRob Herring }
55f3ba9122SRob Herring
panfrost_gpu_soft_reset(struct panfrost_device * pfdev)56f3ba9122SRob Herring int panfrost_gpu_soft_reset(struct panfrost_device *pfdev)
57f3ba9122SRob Herring {
58f3ba9122SRob Herring int ret;
59f3ba9122SRob Herring u32 val;
60f3ba9122SRob Herring
61f3ba9122SRob Herring gpu_write(pfdev, GPU_INT_MASK, 0);
62f3ba9122SRob Herring gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_RESET_COMPLETED);
63f3ba9122SRob Herring gpu_write(pfdev, GPU_CMD, GPU_CMD_SOFT_RESET);
64f3ba9122SRob Herring
65f3ba9122SRob Herring ret = readl_relaxed_poll_timeout(pfdev->iomem + GPU_INT_RAWSTAT,
66f3ba9122SRob Herring val, val & GPU_IRQ_RESET_COMPLETED, 100, 10000);
67f3ba9122SRob Herring
68f3ba9122SRob Herring if (ret) {
69f3ba9122SRob Herring dev_err(pfdev->dev, "gpu soft reset timed out\n");
70f3ba9122SRob Herring return ret;
71f3ba9122SRob Herring }
72f3ba9122SRob Herring
73f3ba9122SRob Herring gpu_write(pfdev, GPU_INT_CLEAR, GPU_IRQ_MASK_ALL);
744687cb57SAngeloGioacchino Del Regno
754687cb57SAngeloGioacchino Del Regno /* Only enable the interrupts we care about */
764687cb57SAngeloGioacchino Del Regno gpu_write(pfdev, GPU_INT_MASK,
774687cb57SAngeloGioacchino Del Regno GPU_IRQ_MASK_ERROR |
784687cb57SAngeloGioacchino Del Regno GPU_IRQ_PERFCNT_SAMPLE_COMPLETED |
794687cb57SAngeloGioacchino Del Regno GPU_IRQ_CLEAN_CACHES_COMPLETED);
80f3ba9122SRob Herring
81f3ba9122SRob Herring return 0;
82f3ba9122SRob Herring }
83f3ba9122SRob Herring
panfrost_gpu_amlogic_quirk(struct panfrost_device * pfdev)8411000300SNeil Armstrong void panfrost_gpu_amlogic_quirk(struct panfrost_device *pfdev)
8511000300SNeil Armstrong {
8611000300SNeil Armstrong /*
8711000300SNeil Armstrong * The Amlogic integrated Mali-T820, Mali-G31 & Mali-G52 needs
8811000300SNeil Armstrong * these undocumented bits in GPU_PWR_OVERRIDE1 to be set in order
8911000300SNeil Armstrong * to operate correctly.
9011000300SNeil Armstrong */
9111000300SNeil Armstrong gpu_write(pfdev, GPU_PWR_KEY, GPU_PWR_KEY_UNLOCK);
9211000300SNeil Armstrong gpu_write(pfdev, GPU_PWR_OVERRIDE1, 0xfff | (0x20 << 16));
9311000300SNeil Armstrong }
9411000300SNeil Armstrong
panfrost_gpu_init_quirks(struct panfrost_device * pfdev)95f3ba9122SRob Herring static void panfrost_gpu_init_quirks(struct panfrost_device *pfdev)
96f3ba9122SRob Herring {
97f3ba9122SRob Herring u32 quirks = 0;
98f3ba9122SRob Herring
99f3ba9122SRob Herring if (panfrost_has_hw_issue(pfdev, HW_ISSUE_8443) ||
100f3ba9122SRob Herring panfrost_has_hw_issue(pfdev, HW_ISSUE_11035))
101f3ba9122SRob Herring quirks |= SC_LS_PAUSEBUFFER_DISABLE;
102f3ba9122SRob Herring
103f3ba9122SRob Herring if (panfrost_has_hw_issue(pfdev, HW_ISSUE_10327))
104f3ba9122SRob Herring quirks |= SC_SDC_DISABLE_OQ_DISCARD;
105f3ba9122SRob Herring
106f3ba9122SRob Herring if (panfrost_has_hw_issue(pfdev, HW_ISSUE_10797))
107f3ba9122SRob Herring quirks |= SC_ENABLE_TEXGRD_FLAGS;
108f3ba9122SRob Herring
109f3ba9122SRob Herring if (!panfrost_has_hw_issue(pfdev, GPUCORE_1619)) {
110f3ba9122SRob Herring if (panfrost_model_cmp(pfdev, 0x750) < 0) /* T60x, T62x, T72x */
111f3ba9122SRob Herring quirks |= SC_LS_ATTR_CHECK_DISABLE;
112f3ba9122SRob Herring else if (panfrost_model_cmp(pfdev, 0x880) <= 0) /* T76x, T8xx */
113f3ba9122SRob Herring quirks |= SC_LS_ALLOW_ATTR_TYPES;
114f3ba9122SRob Herring }
115f3ba9122SRob Herring
11638243570SAlyssa Rosenzweig if (panfrost_has_hw_issue(pfdev, HW_ISSUE_TTRX_2968_TTRX_3162))
11738243570SAlyssa Rosenzweig quirks |= SC_VAR_ALGORITHM;
11838243570SAlyssa Rosenzweig
119f3ba9122SRob Herring if (panfrost_has_hw_feature(pfdev, HW_FEATURE_TLS_HASHING))
120f3ba9122SRob Herring quirks |= SC_TLS_HASH_ENABLE;
121f3ba9122SRob Herring
122f3ba9122SRob Herring if (quirks)
123f3ba9122SRob Herring gpu_write(pfdev, GPU_SHADER_CONFIG, quirks);
124f3ba9122SRob Herring
125f3ba9122SRob Herring
126f3ba9122SRob Herring quirks = gpu_read(pfdev, GPU_TILER_CONFIG);
127f3ba9122SRob Herring
128f3ba9122SRob Herring /* Set tiler clock gate override if required */
129f3ba9122SRob Herring if (panfrost_has_hw_issue(pfdev, HW_ISSUE_T76X_3953))
130f3ba9122SRob Herring quirks |= TC_CLOCK_GATE_OVERRIDE;
131f3ba9122SRob Herring
132f3ba9122SRob Herring gpu_write(pfdev, GPU_TILER_CONFIG, quirks);
133f3ba9122SRob Herring
134f3ba9122SRob Herring
135f3ba9122SRob Herring quirks = 0;
136f3ba9122SRob Herring if ((panfrost_model_eq(pfdev, 0x860) || panfrost_model_eq(pfdev, 0x880)) &&
137f3ba9122SRob Herring pfdev->features.revision >= 0x2000)
138f3ba9122SRob Herring quirks |= JM_MAX_JOB_THROTTLE_LIMIT << JM_JOB_THROTTLE_LIMIT_SHIFT;
139f3ba9122SRob Herring else if (panfrost_model_eq(pfdev, 0x6000) &&
140f3ba9122SRob Herring pfdev->features.coherency_features == COHERENCY_ACE)
141f3ba9122SRob Herring quirks |= (COHERENCY_ACE_LITE | COHERENCY_ACE) <<
142f3ba9122SRob Herring JM_FORCE_COHERENCY_FEATURES_SHIFT;
143f3ba9122SRob Herring
1442e87309eSAlyssa Rosenzweig if (panfrost_has_hw_feature(pfdev, HW_FEATURE_IDVS_GROUP_SIZE))
1452e87309eSAlyssa Rosenzweig quirks |= JM_DEFAULT_IDVS_GROUP_SIZE << JM_IDVS_GROUP_SIZE_SHIFT;
1462e87309eSAlyssa Rosenzweig
147f3ba9122SRob Herring if (quirks)
148f3ba9122SRob Herring gpu_write(pfdev, GPU_JM_CONFIG, quirks);
14991e89097SNeil Armstrong
15091e89097SNeil Armstrong /* Here goes platform specific quirks */
15191e89097SNeil Armstrong if (pfdev->comp->vendor_quirk)
15291e89097SNeil Armstrong pfdev->comp->vendor_quirk(pfdev);
153f3ba9122SRob Herring }
154f3ba9122SRob Herring
155f3ba9122SRob Herring #define MAX_HW_REVS 6
156f3ba9122SRob Herring
157f3ba9122SRob Herring struct panfrost_model {
158f3ba9122SRob Herring const char *name;
159f3ba9122SRob Herring u32 id;
160f3ba9122SRob Herring u64 features;
161f3ba9122SRob Herring u64 issues;
162f3ba9122SRob Herring struct {
163f3ba9122SRob Herring u32 revision;
164f3ba9122SRob Herring u64 issues;
165f3ba9122SRob Herring } revs[MAX_HW_REVS];
166f3ba9122SRob Herring };
167f3ba9122SRob Herring
168f3ba9122SRob Herring #define GPU_MODEL(_name, _id, ...) \
169f3ba9122SRob Herring {\
170f3ba9122SRob Herring .name = __stringify(_name), \
171f3ba9122SRob Herring .id = _id, \
172f3ba9122SRob Herring .features = hw_features_##_name, \
173f3ba9122SRob Herring .issues = hw_issues_##_name, \
174f3ba9122SRob Herring .revs = { __VA_ARGS__ }, \
175f3ba9122SRob Herring }
176f3ba9122SRob Herring
177f3ba9122SRob Herring #define GPU_REV_EXT(name, _rev, _p, _s, stat) \
178f3ba9122SRob Herring {\
179f3ba9122SRob Herring .revision = (_rev) << 12 | (_p) << 4 | (_s), \
180f3ba9122SRob Herring .issues = hw_issues_##name##_r##_rev##p##_p##stat, \
181f3ba9122SRob Herring }
182f3ba9122SRob Herring #define GPU_REV(name, r, p) GPU_REV_EXT(name, r, p, 0, )
183f3ba9122SRob Herring
184f3ba9122SRob Herring static const struct panfrost_model gpu_models[] = {
185f3ba9122SRob Herring /* T60x has an oddball version */
186f3ba9122SRob Herring GPU_MODEL(t600, 0x600,
187f3ba9122SRob Herring GPU_REV_EXT(t600, 0, 0, 1, _15dev0)),
188f3ba9122SRob Herring GPU_MODEL(t620, 0x620,
189f3ba9122SRob Herring GPU_REV(t620, 0, 1), GPU_REV(t620, 1, 0)),
190f3ba9122SRob Herring GPU_MODEL(t720, 0x720),
191f3ba9122SRob Herring GPU_MODEL(t760, 0x750,
192f3ba9122SRob Herring GPU_REV(t760, 0, 0), GPU_REV(t760, 0, 1),
193f3ba9122SRob Herring GPU_REV_EXT(t760, 0, 1, 0, _50rel0),
194f3ba9122SRob Herring GPU_REV(t760, 0, 2), GPU_REV(t760, 0, 3)),
195f3ba9122SRob Herring GPU_MODEL(t820, 0x820),
196f3ba9122SRob Herring GPU_MODEL(t830, 0x830),
197f3ba9122SRob Herring GPU_MODEL(t860, 0x860),
198f3ba9122SRob Herring GPU_MODEL(t880, 0x880),
199f3ba9122SRob Herring
200f3ba9122SRob Herring GPU_MODEL(g71, 0x6000,
201f3ba9122SRob Herring GPU_REV_EXT(g71, 0, 0, 1, _05dev0)),
202f3ba9122SRob Herring GPU_MODEL(g72, 0x6001),
203f3ba9122SRob Herring GPU_MODEL(g51, 0x7000),
204f3ba9122SRob Herring GPU_MODEL(g76, 0x7001),
205f3ba9122SRob Herring GPU_MODEL(g52, 0x7002),
206f3ba9122SRob Herring GPU_MODEL(g31, 0x7003,
207f3ba9122SRob Herring GPU_REV(g31, 1, 0)),
2085ba99fcaSAlyssa Rosenzweig
2095ba99fcaSAlyssa Rosenzweig GPU_MODEL(g57, 0x9001,
2105ba99fcaSAlyssa Rosenzweig GPU_REV(g57, 0, 0)),
2116ba1fd69SAlyssa Rosenzweig
2126ba1fd69SAlyssa Rosenzweig /* MediaTek MT8192 has a Mali-G57 with a different GPU ID from the
2136ba1fd69SAlyssa Rosenzweig * standard. Arm's driver does not appear to handle this model.
2146ba1fd69SAlyssa Rosenzweig * ChromeOS has a hack downstream for it. Treat it as equivalent to
2156ba1fd69SAlyssa Rosenzweig * standard Mali-G57 for now.
2166ba1fd69SAlyssa Rosenzweig */
2176ba1fd69SAlyssa Rosenzweig GPU_MODEL(g57, 0x9003,
2186ba1fd69SAlyssa Rosenzweig GPU_REV(g57, 0, 0)),
219f3ba9122SRob Herring };
220f3ba9122SRob Herring
panfrost_gpu_init_features(struct panfrost_device * pfdev)221f3ba9122SRob Herring static void panfrost_gpu_init_features(struct panfrost_device *pfdev)
222f3ba9122SRob Herring {
223f3ba9122SRob Herring u32 gpu_id, num_js, major, minor, status, rev;
224f3ba9122SRob Herring const char *name = "unknown";
225f3ba9122SRob Herring u64 hw_feat = 0;
226f3ba9122SRob Herring u64 hw_issues = hw_issues_all;
227f3ba9122SRob Herring const struct panfrost_model *model;
228f3ba9122SRob Herring int i;
229f3ba9122SRob Herring
230f3ba9122SRob Herring pfdev->features.l2_features = gpu_read(pfdev, GPU_L2_FEATURES);
231f3ba9122SRob Herring pfdev->features.core_features = gpu_read(pfdev, GPU_CORE_FEATURES);
232f3ba9122SRob Herring pfdev->features.tiler_features = gpu_read(pfdev, GPU_TILER_FEATURES);
233f3ba9122SRob Herring pfdev->features.mem_features = gpu_read(pfdev, GPU_MEM_FEATURES);
234f3ba9122SRob Herring pfdev->features.mmu_features = gpu_read(pfdev, GPU_MMU_FEATURES);
235f3ba9122SRob Herring pfdev->features.thread_features = gpu_read(pfdev, GPU_THREAD_FEATURES);
236eda6d764SSteven Price pfdev->features.max_threads = gpu_read(pfdev, GPU_THREAD_MAX_THREADS);
237eda6d764SSteven Price pfdev->features.thread_max_workgroup_sz = gpu_read(pfdev, GPU_THREAD_MAX_WORKGROUP_SIZE);
238eda6d764SSteven Price pfdev->features.thread_max_barrier_sz = gpu_read(pfdev, GPU_THREAD_MAX_BARRIER_SIZE);
239f3ba9122SRob Herring pfdev->features.coherency_features = gpu_read(pfdev, GPU_COHERENCY_FEATURES);
2403e2926f8SAlyssa Rosenzweig pfdev->features.afbc_features = gpu_read(pfdev, GPU_AFBC_FEATURES);
241f3ba9122SRob Herring for (i = 0; i < 4; i++)
242f3ba9122SRob Herring pfdev->features.texture_features[i] = gpu_read(pfdev, GPU_TEXTURE_FEATURES(i));
243f3ba9122SRob Herring
244f3ba9122SRob Herring pfdev->features.as_present = gpu_read(pfdev, GPU_AS_PRESENT);
245f3ba9122SRob Herring
246f3ba9122SRob Herring pfdev->features.js_present = gpu_read(pfdev, GPU_JS_PRESENT);
247f3ba9122SRob Herring num_js = hweight32(pfdev->features.js_present);
248f3ba9122SRob Herring for (i = 0; i < num_js; i++)
249f3ba9122SRob Herring pfdev->features.js_features[i] = gpu_read(pfdev, GPU_JS_FEATURES(i));
250f3ba9122SRob Herring
251f3ba9122SRob Herring pfdev->features.shader_present = gpu_read(pfdev, GPU_SHADER_PRESENT_LO);
252f3ba9122SRob Herring pfdev->features.shader_present |= (u64)gpu_read(pfdev, GPU_SHADER_PRESENT_HI) << 32;
253f3ba9122SRob Herring
254f3ba9122SRob Herring pfdev->features.tiler_present = gpu_read(pfdev, GPU_TILER_PRESENT_LO);
255f3ba9122SRob Herring pfdev->features.tiler_present |= (u64)gpu_read(pfdev, GPU_TILER_PRESENT_HI) << 32;
256f3ba9122SRob Herring
257f3ba9122SRob Herring pfdev->features.l2_present = gpu_read(pfdev, GPU_L2_PRESENT_LO);
258f3ba9122SRob Herring pfdev->features.l2_present |= (u64)gpu_read(pfdev, GPU_L2_PRESENT_HI) << 32;
259f3ba9122SRob Herring pfdev->features.nr_core_groups = hweight64(pfdev->features.l2_present);
260f3ba9122SRob Herring
261f3ba9122SRob Herring pfdev->features.stack_present = gpu_read(pfdev, GPU_STACK_PRESENT_LO);
262f3ba9122SRob Herring pfdev->features.stack_present |= (u64)gpu_read(pfdev, GPU_STACK_PRESENT_HI) << 32;
263f3ba9122SRob Herring
2644bced8beSSteven Price pfdev->features.thread_tls_alloc = gpu_read(pfdev, GPU_THREAD_TLS_ALLOC);
2654bced8beSSteven Price
266f3ba9122SRob Herring gpu_id = gpu_read(pfdev, GPU_ID);
267f3ba9122SRob Herring pfdev->features.revision = gpu_id & 0xffff;
268f3ba9122SRob Herring pfdev->features.id = gpu_id >> 16;
269f3ba9122SRob Herring
270f3ba9122SRob Herring /* The T60x has an oddball ID value. Fix it up to the standard Midgard
271f3ba9122SRob Herring * format so we (and userspace) don't have to special case it.
272f3ba9122SRob Herring */
273f3ba9122SRob Herring if (pfdev->features.id == 0x6956)
274f3ba9122SRob Herring pfdev->features.id = 0x0600;
275f3ba9122SRob Herring
276f3ba9122SRob Herring major = (pfdev->features.revision >> 12) & 0xf;
277f3ba9122SRob Herring minor = (pfdev->features.revision >> 4) & 0xff;
278f3ba9122SRob Herring status = pfdev->features.revision & 0xf;
279f3ba9122SRob Herring rev = pfdev->features.revision;
280f3ba9122SRob Herring
281f3ba9122SRob Herring gpu_id = pfdev->features.id;
282f3ba9122SRob Herring
283f3ba9122SRob Herring for (model = gpu_models; model->name; model++) {
284f3ba9122SRob Herring int best = -1;
285f3ba9122SRob Herring
286f3ba9122SRob Herring if (!panfrost_model_eq(pfdev, model->id))
287f3ba9122SRob Herring continue;
288f3ba9122SRob Herring
289f3ba9122SRob Herring name = model->name;
290f3ba9122SRob Herring hw_feat = model->features;
291f3ba9122SRob Herring hw_issues |= model->issues;
292f3ba9122SRob Herring for (i = 0; i < MAX_HW_REVS; i++) {
293f3ba9122SRob Herring if (model->revs[i].revision == rev) {
294f3ba9122SRob Herring best = i;
295f3ba9122SRob Herring break;
296f3ba9122SRob Herring } else if (model->revs[i].revision == (rev & ~0xf))
297f3ba9122SRob Herring best = i;
298f3ba9122SRob Herring }
299f3ba9122SRob Herring
300f3ba9122SRob Herring if (best >= 0)
301f3ba9122SRob Herring hw_issues |= model->revs[best].issues;
302f3ba9122SRob Herring
303f3ba9122SRob Herring break;
304f3ba9122SRob Herring }
305f3ba9122SRob Herring
306f3ba9122SRob Herring bitmap_from_u64(pfdev->features.hw_features, hw_feat);
307f3ba9122SRob Herring bitmap_from_u64(pfdev->features.hw_issues, hw_issues);
308f3ba9122SRob Herring
309f3ba9122SRob Herring dev_info(pfdev->dev, "mali-%s id 0x%x major 0x%x minor 0x%x status 0x%x",
310f3ba9122SRob Herring name, gpu_id, major, minor, status);
311f3ba9122SRob Herring dev_info(pfdev->dev, "features: %64pb, issues: %64pb",
312f3ba9122SRob Herring pfdev->features.hw_features,
313f3ba9122SRob Herring pfdev->features.hw_issues);
314f3ba9122SRob Herring
315f3ba9122SRob Herring dev_info(pfdev->dev, "Features: L2:0x%08x Shader:0x%08x Tiler:0x%08x Mem:0x%0x MMU:0x%08x AS:0x%x JS:0x%x",
316b208146bSRobin Murphy pfdev->features.l2_features,
317b208146bSRobin Murphy pfdev->features.core_features,
318b208146bSRobin Murphy pfdev->features.tiler_features,
319b208146bSRobin Murphy pfdev->features.mem_features,
320b208146bSRobin Murphy pfdev->features.mmu_features,
321b208146bSRobin Murphy pfdev->features.as_present,
322b208146bSRobin Murphy pfdev->features.js_present);
323f3ba9122SRob Herring
324f3ba9122SRob Herring dev_info(pfdev->dev, "shader_present=0x%0llx l2_present=0x%0llx",
325f3ba9122SRob Herring pfdev->features.shader_present, pfdev->features.l2_present);
326f3ba9122SRob Herring }
327f3ba9122SRob Herring
panfrost_get_core_mask(struct panfrost_device * pfdev)328305f1f46SAngeloGioacchino Del Regno static u64 panfrost_get_core_mask(struct panfrost_device *pfdev)
329f3ba9122SRob Herring {
330305f1f46SAngeloGioacchino Del Regno u64 core_mask;
331f3ba9122SRob Herring
332305f1f46SAngeloGioacchino Del Regno if (pfdev->features.l2_present == 1)
333305f1f46SAngeloGioacchino Del Regno return U64_MAX;
3348c3c818cSSteven Price
3356e55d273SAlexey Sheplyakov /*
3366e55d273SAlexey Sheplyakov * Only support one core group now.
3376e55d273SAlexey Sheplyakov * ~(l2_present - 1) unsets all bits in l2_present except
3386e55d273SAlexey Sheplyakov * the bottom bit. (l2_present - 2) has all the bits in
3396e55d273SAlexey Sheplyakov * the first core group set. AND them together to generate
3406e55d273SAlexey Sheplyakov * a mask of cores in the first core group.
3416e55d273SAlexey Sheplyakov */
3426e55d273SAlexey Sheplyakov core_mask = ~(pfdev->features.l2_present - 1) &
3436e55d273SAlexey Sheplyakov (pfdev->features.l2_present - 2);
3446e55d273SAlexey Sheplyakov dev_info_once(pfdev->dev, "using only 1st core group (%lu cores from %lu)\n",
3456e55d273SAlexey Sheplyakov hweight64(core_mask),
3466e55d273SAlexey Sheplyakov hweight64(pfdev->features.shader_present));
347305f1f46SAngeloGioacchino Del Regno
348305f1f46SAngeloGioacchino Del Regno return core_mask;
3496e55d273SAlexey Sheplyakov }
350305f1f46SAngeloGioacchino Del Regno
panfrost_gpu_power_on(struct panfrost_device * pfdev)351305f1f46SAngeloGioacchino Del Regno void panfrost_gpu_power_on(struct panfrost_device *pfdev)
352305f1f46SAngeloGioacchino Del Regno {
353305f1f46SAngeloGioacchino Del Regno int ret;
354305f1f46SAngeloGioacchino Del Regno u32 val;
355305f1f46SAngeloGioacchino Del Regno u64 core_mask;
356305f1f46SAngeloGioacchino Del Regno
357305f1f46SAngeloGioacchino Del Regno panfrost_gpu_init_quirks(pfdev);
358305f1f46SAngeloGioacchino Del Regno core_mask = panfrost_get_core_mask(pfdev);
359305f1f46SAngeloGioacchino Del Regno
3606e55d273SAlexey Sheplyakov gpu_write(pfdev, L2_PWRON_LO, pfdev->features.l2_present & core_mask);
361f3ba9122SRob Herring ret = readl_relaxed_poll_timeout(pfdev->iomem + L2_READY_LO,
3626e55d273SAlexey Sheplyakov val, val == (pfdev->features.l2_present & core_mask),
3636e55d273SAlexey Sheplyakov 100, 20000);
364a9d73b30SNicolas Boichat if (ret)
365a9d73b30SNicolas Boichat dev_err(pfdev->dev, "error powering up gpu L2");
366f3ba9122SRob Herring
3676e55d273SAlexey Sheplyakov gpu_write(pfdev, SHADER_PWRON_LO,
3686e55d273SAlexey Sheplyakov pfdev->features.shader_present & core_mask);
369a9d73b30SNicolas Boichat ret = readl_relaxed_poll_timeout(pfdev->iomem + SHADER_READY_LO,
3706e55d273SAlexey Sheplyakov val, val == (pfdev->features.shader_present & core_mask),
3716e55d273SAlexey Sheplyakov 100, 20000);
372a9d73b30SNicolas Boichat if (ret)
373a9d73b30SNicolas Boichat dev_err(pfdev->dev, "error powering up gpu shader");
374f3ba9122SRob Herring
375f3ba9122SRob Herring gpu_write(pfdev, TILER_PWRON_LO, pfdev->features.tiler_present);
376a9d73b30SNicolas Boichat ret = readl_relaxed_poll_timeout(pfdev->iomem + TILER_READY_LO,
377f3ba9122SRob Herring val, val == pfdev->features.tiler_present, 100, 1000);
378f3ba9122SRob Herring if (ret)
379a9d73b30SNicolas Boichat dev_err(pfdev->dev, "error powering up gpu tiler");
380f3ba9122SRob Herring }
381f3ba9122SRob Herring
panfrost_gpu_power_off(struct panfrost_device * pfdev)382f3ba9122SRob Herring void panfrost_gpu_power_off(struct panfrost_device *pfdev)
383f3ba9122SRob Herring {
384305f1f46SAngeloGioacchino Del Regno int ret;
385305f1f46SAngeloGioacchino Del Regno u32 val;
386305f1f46SAngeloGioacchino Del Regno
3874687cb57SAngeloGioacchino Del Regno gpu_write(pfdev, SHADER_PWROFF_LO, pfdev->features.shader_present);
388305f1f46SAngeloGioacchino Del Regno ret = readl_relaxed_poll_timeout(pfdev->iomem + SHADER_PWRTRANS_LO,
389*9e3941c9SChristian Hewitt val, !val, 1, 2000);
390305f1f46SAngeloGioacchino Del Regno if (ret)
391305f1f46SAngeloGioacchino Del Regno dev_err(pfdev->dev, "shader power transition timeout");
392305f1f46SAngeloGioacchino Del Regno
393305f1f46SAngeloGioacchino Del Regno gpu_write(pfdev, TILER_PWROFF_LO, pfdev->features.tiler_present);
394305f1f46SAngeloGioacchino Del Regno ret = readl_relaxed_poll_timeout(pfdev->iomem + TILER_PWRTRANS_LO,
395*9e3941c9SChristian Hewitt val, !val, 1, 2000);
396305f1f46SAngeloGioacchino Del Regno if (ret)
397305f1f46SAngeloGioacchino Del Regno dev_err(pfdev->dev, "tiler power transition timeout");
398305f1f46SAngeloGioacchino Del Regno
3994687cb57SAngeloGioacchino Del Regno gpu_write(pfdev, L2_PWROFF_LO, pfdev->features.l2_present);
400305f1f46SAngeloGioacchino Del Regno ret = readl_poll_timeout(pfdev->iomem + L2_PWRTRANS_LO,
401*9e3941c9SChristian Hewitt val, !val, 0, 2000);
402305f1f46SAngeloGioacchino Del Regno if (ret)
403305f1f46SAngeloGioacchino Del Regno dev_err(pfdev->dev, "l2 power transition timeout");
404f3ba9122SRob Herring }
405f3ba9122SRob Herring
panfrost_gpu_init(struct panfrost_device * pfdev)406f3ba9122SRob Herring int panfrost_gpu_init(struct panfrost_device *pfdev)
407f3ba9122SRob Herring {
408f3ba9122SRob Herring int err, irq;
409f3ba9122SRob Herring
410f3ba9122SRob Herring err = panfrost_gpu_soft_reset(pfdev);
411f3ba9122SRob Herring if (err)
412f3ba9122SRob Herring return err;
413f3ba9122SRob Herring
414f3ba9122SRob Herring panfrost_gpu_init_features(pfdev);
415f3ba9122SRob Herring
41644ab30b0SJiasheng Jiang err = dma_set_mask_and_coherent(pfdev->dev,
417d9b631f0SRobin Murphy DMA_BIT_MASK(FIELD_GET(0xff00, pfdev->features.mmu_features)));
41844ab30b0SJiasheng Jiang if (err)
41944ab30b0SJiasheng Jiang return err;
42044ab30b0SJiasheng Jiang
421ac5037afSRobin Murphy dma_set_max_seg_size(pfdev->dev, UINT_MAX);
422d9b631f0SRobin Murphy
423f3ba9122SRob Herring irq = platform_get_irq_byname(to_platform_device(pfdev->dev), "gpu");
424f3ba9122SRob Herring if (irq <= 0)
425f3ba9122SRob Herring return -ENODEV;
426f3ba9122SRob Herring
427f3ba9122SRob Herring err = devm_request_irq(pfdev->dev, irq, panfrost_gpu_irq_handler,
42873896f60SEzequiel Garcia IRQF_SHARED, KBUILD_MODNAME "-gpu", pfdev);
429f3ba9122SRob Herring if (err) {
430f3ba9122SRob Herring dev_err(pfdev->dev, "failed to request gpu irq");
431f3ba9122SRob Herring return err;
432f3ba9122SRob Herring }
433f3ba9122SRob Herring
434f3ba9122SRob Herring panfrost_gpu_power_on(pfdev);
435f3ba9122SRob Herring
436f3ba9122SRob Herring return 0;
437f3ba9122SRob Herring }
438f3ba9122SRob Herring
panfrost_gpu_fini(struct panfrost_device * pfdev)439f3ba9122SRob Herring void panfrost_gpu_fini(struct panfrost_device *pfdev)
440f3ba9122SRob Herring {
441f3ba9122SRob Herring panfrost_gpu_power_off(pfdev);
442f3ba9122SRob Herring }
443f3ba9122SRob Herring
panfrost_gpu_get_latest_flush_id(struct panfrost_device * pfdev)444f3ba9122SRob Herring u32 panfrost_gpu_get_latest_flush_id(struct panfrost_device *pfdev)
445f3ba9122SRob Herring {
4463a74265cSTomeu Vizoso u32 flush_id;
4473a74265cSTomeu Vizoso
4483a74265cSTomeu Vizoso if (panfrost_has_hw_feature(pfdev, HW_FEATURE_FLUSH_REDUCTION)) {
4493a74265cSTomeu Vizoso /* Flush reduction only makes sense when the GPU is kept powered on between jobs */
4503a74265cSTomeu Vizoso if (pm_runtime_get_if_in_use(pfdev->dev)) {
4513a74265cSTomeu Vizoso flush_id = gpu_read(pfdev, GPU_LATEST_FLUSH_ID);
4523a74265cSTomeu Vizoso pm_runtime_put(pfdev->dev);
4533a74265cSTomeu Vizoso return flush_id;
4543a74265cSTomeu Vizoso }
4553a74265cSTomeu Vizoso }
4563a74265cSTomeu Vizoso
457f3ba9122SRob Herring return 0;
458f3ba9122SRob Herring }
459