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 
5f3ba9122SRob Herring #ifndef __PANFROST_DEVICE_H__
6f3ba9122SRob Herring #define __PANFROST_DEVICE_H__
7f3ba9122SRob Herring 
87282f764SRob Herring #include <linux/atomic.h>
97282f764SRob Herring #include <linux/io-pgtable.h>
1053d36818SPaul Cercueil #include <linux/pm.h>
113e1399bcSNicolas Boichat #include <linux/regulator/consumer.h>
12f3ba9122SRob Herring #include <linux/spinlock.h>
13f3ba9122SRob Herring #include <drm/drm_device.h>
14f3ba9122SRob Herring #include <drm/drm_mm.h>
15f3ba9122SRob Herring #include <drm/gpu_scheduler.h>
16f3ba9122SRob Herring 
179bfacfc8SClément Péron #include "panfrost_devfreq.h"
189bfacfc8SClément Péron 
19f3ba9122SRob Herring struct panfrost_device;
20f3ba9122SRob Herring struct panfrost_mmu;
21f3ba9122SRob Herring struct panfrost_job_slot;
22f3ba9122SRob Herring struct panfrost_job;
237786fd10SBoris Brezillon struct panfrost_perfcnt;
24f3ba9122SRob Herring 
25f3ba9122SRob Herring #define NUM_JOB_SLOTS 3
26*a769a7afSAlyssa Rosenzweig #define MAX_PM_DOMAINS 5
27f3ba9122SRob Herring 
28f3ba9122SRob Herring struct panfrost_features {
29f3ba9122SRob Herring 	u16 id;
30f3ba9122SRob Herring 	u16 revision;
31f3ba9122SRob Herring 
32f3ba9122SRob Herring 	u64 shader_present;
33f3ba9122SRob Herring 	u64 tiler_present;
34f3ba9122SRob Herring 	u64 l2_present;
35f3ba9122SRob Herring 	u64 stack_present;
36f3ba9122SRob Herring 	u32 as_present;
37f3ba9122SRob Herring 	u32 js_present;
38f3ba9122SRob Herring 
39f3ba9122SRob Herring 	u32 l2_features;
40f3ba9122SRob Herring 	u32 core_features;
41f3ba9122SRob Herring 	u32 tiler_features;
42f3ba9122SRob Herring 	u32 mem_features;
43f3ba9122SRob Herring 	u32 mmu_features;
44f3ba9122SRob Herring 	u32 thread_features;
45f3ba9122SRob Herring 	u32 max_threads;
46f3ba9122SRob Herring 	u32 thread_max_workgroup_sz;
47f3ba9122SRob Herring 	u32 thread_max_barrier_sz;
48f3ba9122SRob Herring 	u32 coherency_features;
493e2926f8SAlyssa Rosenzweig 	u32 afbc_features;
50f3ba9122SRob Herring 	u32 texture_features[4];
51f3ba9122SRob Herring 	u32 js_features[16];
52f3ba9122SRob Herring 
53f3ba9122SRob Herring 	u32 nr_core_groups;
544bced8beSSteven Price 	u32 thread_tls_alloc;
55f3ba9122SRob Herring 
56f3ba9122SRob Herring 	unsigned long hw_features[64 / BITS_PER_LONG];
57f3ba9122SRob Herring 	unsigned long hw_issues[64 / BITS_PER_LONG];
58f3ba9122SRob Herring };
59f3ba9122SRob Herring 
603e1399bcSNicolas Boichat /*
613e1399bcSNicolas Boichat  * Features that cannot be automatically detected and need matching using the
623e1399bcSNicolas Boichat  * compatible string, typically SoC-specific.
633e1399bcSNicolas Boichat  */
643e1399bcSNicolas Boichat struct panfrost_compatible {
653e1399bcSNicolas Boichat 	/* Supplies count and names. */
663e1399bcSNicolas Boichat 	int num_supplies;
673e1399bcSNicolas Boichat 	const char * const *supply_names;
68506629c8SNicolas Boichat 	/*
69506629c8SNicolas Boichat 	 * Number of power domains required, note that values 0 and 1 are
70506629c8SNicolas Boichat 	 * handled identically, as only values > 1 need special handling.
71506629c8SNicolas Boichat 	 */
72506629c8SNicolas Boichat 	int num_pm_domains;
73506629c8SNicolas Boichat 	/* Only required if num_pm_domains > 1. */
74506629c8SNicolas Boichat 	const char * const *pm_domain_names;
7591e89097SNeil Armstrong 
7691e89097SNeil Armstrong 	/* Vendor implementation quirks callback */
7791e89097SNeil Armstrong 	void (*vendor_quirk)(struct panfrost_device *pfdev);
783e1399bcSNicolas Boichat };
793e1399bcSNicolas Boichat 
80f3ba9122SRob Herring struct panfrost_device {
81f3ba9122SRob Herring 	struct device *dev;
82f3ba9122SRob Herring 	struct drm_device *ddev;
83f3ba9122SRob Herring 	struct platform_device *pdev;
84f3ba9122SRob Herring 
85f3ba9122SRob Herring 	void __iomem *iomem;
86f3ba9122SRob Herring 	struct clk *clock;
87b681af0bSClément Péron 	struct clk *bus_clock;
88512f2122SClément Péron 	struct regulator_bulk_data *regulators;
89f3ba9122SRob Herring 	struct reset_control *rstc;
90506629c8SNicolas Boichat 	/* pm_domains for devices with more than one. */
91506629c8SNicolas Boichat 	struct device *pm_domain_devs[MAX_PM_DOMAINS];
92506629c8SNicolas Boichat 	struct device_link *pm_domain_links[MAX_PM_DOMAINS];
93268af50fSRobin Murphy 	bool coherent;
94f3ba9122SRob Herring 
95f3ba9122SRob Herring 	struct panfrost_features features;
963e1399bcSNicolas Boichat 	const struct panfrost_compatible *comp;
97f3ba9122SRob Herring 
987282f764SRob Herring 	spinlock_t as_lock;
997282f764SRob Herring 	unsigned long as_in_use_mask;
1007282f764SRob Herring 	unsigned long as_alloc_mask;
101ed7a34c5SBoris Brezillon 	unsigned long as_faulty_mask;
1027282f764SRob Herring 	struct list_head as_lru_list;
1037282f764SRob Herring 
104f3ba9122SRob Herring 	struct panfrost_job_slot *js;
105f3ba9122SRob Herring 
106030761e0SSteven Price 	struct panfrost_job *jobs[NUM_JOB_SLOTS][2];
107f3ba9122SRob Herring 	struct list_head scheduled_jobs;
108f3ba9122SRob Herring 
1097786fd10SBoris Brezillon 	struct panfrost_perfcnt *perfcnt;
1107786fd10SBoris Brezillon 
111f3ba9122SRob Herring 	struct mutex sched_lock;
1125bc5cc28SBoris Brezillon 
1135bc5cc28SBoris Brezillon 	struct {
114a11c4711SBoris Brezillon 		struct workqueue_struct *wq;
1155bc5cc28SBoris Brezillon 		struct work_struct work;
1165bc5cc28SBoris Brezillon 		atomic_t pending;
1175bc5cc28SBoris Brezillon 	} reset;
118f3ba9122SRob Herring 
119013b6510SRob Herring 	struct mutex shrinker_lock;
120013b6510SRob Herring 	struct list_head shrinker_list;
121013b6510SRob Herring 	struct shrinker shrinker;
122013b6510SRob Herring 
1239bfacfc8SClément Péron 	struct panfrost_devfreq pfdevfreq;
124f3ba9122SRob Herring };
125f3ba9122SRob Herring 
1267282f764SRob Herring struct panfrost_mmu {
1277fdc48ccSBoris Brezillon 	struct panfrost_device *pfdev;
1287fdc48ccSBoris Brezillon 	struct kref refcount;
1297282f764SRob Herring 	struct io_pgtable_cfg pgtbl_cfg;
1307282f764SRob Herring 	struct io_pgtable_ops *pgtbl_ops;
1317fdc48ccSBoris Brezillon 	struct drm_mm mm;
1327fdc48ccSBoris Brezillon 	spinlock_t mm_lock;
1337282f764SRob Herring 	int as;
1347282f764SRob Herring 	atomic_t as_count;
1357282f764SRob Herring 	struct list_head list;
1367282f764SRob Herring };
1377282f764SRob Herring 
138f3ba9122SRob Herring struct panfrost_file_priv {
139f3ba9122SRob Herring 	struct panfrost_device *pfdev;
140f3ba9122SRob Herring 
141f3ba9122SRob Herring 	struct drm_sched_entity sched_entity[NUM_JOB_SLOTS];
1427282f764SRob Herring 
1437fdc48ccSBoris Brezillon 	struct panfrost_mmu *mmu;
144f3ba9122SRob Herring };
145f3ba9122SRob Herring 
to_panfrost_device(struct drm_device * ddev)146f3ba9122SRob Herring static inline struct panfrost_device *to_panfrost_device(struct drm_device *ddev)
147f3ba9122SRob Herring {
148f3ba9122SRob Herring 	return ddev->dev_private;
149f3ba9122SRob Herring }
150f3ba9122SRob Herring 
panfrost_model_cmp(struct panfrost_device * pfdev,s32 id)151f3ba9122SRob Herring static inline int panfrost_model_cmp(struct panfrost_device *pfdev, s32 id)
152f3ba9122SRob Herring {
153f3ba9122SRob Herring 	s32 match_id = pfdev->features.id;
154f3ba9122SRob Herring 
155f3ba9122SRob Herring 	if (match_id & 0xf000)
156f3ba9122SRob Herring 		match_id &= 0xf00f;
157f3ba9122SRob Herring 	return match_id - id;
158f3ba9122SRob Herring }
159f3ba9122SRob Herring 
panfrost_model_is_bifrost(struct panfrost_device * pfdev)1601e513480SBoris Brezillon static inline bool panfrost_model_is_bifrost(struct panfrost_device *pfdev)
1611e513480SBoris Brezillon {
1621e513480SBoris Brezillon 	return panfrost_model_cmp(pfdev, 0x1000) >= 0;
1631e513480SBoris Brezillon }
1641e513480SBoris Brezillon 
panfrost_model_eq(struct panfrost_device * pfdev,s32 id)165f3ba9122SRob Herring static inline bool panfrost_model_eq(struct panfrost_device *pfdev, s32 id)
166f3ba9122SRob Herring {
167f3ba9122SRob Herring 	return !panfrost_model_cmp(pfdev, id);
168f3ba9122SRob Herring }
169f3ba9122SRob Herring 
17092f0ad0bSBoris Brezillon int panfrost_unstable_ioctl_check(void);
17192f0ad0bSBoris Brezillon 
172f3ba9122SRob Herring int panfrost_device_init(struct panfrost_device *pfdev);
173f3ba9122SRob Herring void panfrost_device_fini(struct panfrost_device *pfdev);
17473e467f6SRob Herring void panfrost_device_reset(struct panfrost_device *pfdev);
175f3ba9122SRob Herring 
17653d36818SPaul Cercueil extern const struct dev_pm_ops panfrost_pm_ops;
177f3ba9122SRob Herring 
1787319965fSBoris Brezillon enum drm_panfrost_exception_type {
1797319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_OK = 0x00,
1807319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_DONE = 0x01,
1817319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_INTERRUPTED = 0x02,
1827319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_STOPPED = 0x03,
1837319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TERMINATED = 0x04,
1847319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_KABOOM = 0x05,
1857319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_EUREKA = 0x06,
1867319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ACTIVE = 0x08,
18730b5d4edSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MAX_NON_FAULT = 0x3f,
1887319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_JOB_CONFIG_FAULT = 0x40,
1897319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_JOB_POWER_FAULT = 0x41,
1907319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_JOB_READ_FAULT = 0x42,
1917319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_JOB_WRITE_FAULT = 0x43,
1927319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_JOB_AFFINITY_FAULT = 0x44,
1937319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_JOB_BUS_FAULT = 0x48,
1947319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_INSTR_INVALID_PC = 0x50,
1957319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_INSTR_INVALID_ENC = 0x51,
1967319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_INSTR_TYPE_MISMATCH = 0x52,
1977319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_INSTR_OPERAND_FAULT = 0x53,
1987319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_INSTR_TLS_FAULT = 0x54,
1997319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_INSTR_BARRIER_FAULT = 0x55,
2007319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_INSTR_ALIGN_FAULT = 0x56,
2017319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_DATA_INVALID_FAULT = 0x58,
2027319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TILE_RANGE_FAULT = 0x59,
2037319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_RANGE_FAULT = 0x5a,
2047319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_IMPRECISE_FAULT = 0x5b,
2057319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_OOM = 0x60,
2067319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_OOM_AFBC = 0x61,
2077319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_UNKNOWN = 0x7f,
2087319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_DELAYED_BUS_FAULT = 0x80,
2097319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_GPU_SHAREABILITY_FAULT = 0x88,
2107319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_SYS_SHAREABILITY_FAULT = 0x89,
2117319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_GPU_CACHEABILITY_FAULT = 0x8a,
2127319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_0 = 0xc0,
2137319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_1 = 0xc1,
2147319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_2 = 0xc2,
2157319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_3 = 0xc3,
2167319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_4 = 0xc4,
2177319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSLATION_FAULT_IDENTITY = 0xc7,
2187319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_PERM_FAULT_0 = 0xc8,
2197319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_PERM_FAULT_1 = 0xc9,
2207319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_PERM_FAULT_2 = 0xca,
2217319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_PERM_FAULT_3 = 0xcb,
2227319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSTAB_BUS_FAULT_0 = 0xd0,
2237319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSTAB_BUS_FAULT_1 = 0xd1,
2247319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSTAB_BUS_FAULT_2 = 0xd2,
2257319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_TRANSTAB_BUS_FAULT_3 = 0xd3,
2267319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ACCESS_FLAG_0 = 0xd8,
2277319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ACCESS_FLAG_1 = 0xd9,
2287319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ACCESS_FLAG_2 = 0xda,
2297319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ACCESS_FLAG_3 = 0xdb,
2307319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_IN0 = 0xe0,
2317319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_IN1 = 0xe1,
2327319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_IN2 = 0xe2,
2337319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_IN3 = 0xe3,
2347319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_OUT0 = 0xe4,
2357319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_OUT1 = 0xe5,
2367319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_OUT2 = 0xe6,
2377319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_ADDR_SIZE_FAULT_OUT3 = 0xe7,
2387319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MEM_ATTR_FAULT_0 = 0xe8,
2397319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MEM_ATTR_FAULT_1 = 0xe9,
2407319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MEM_ATTR_FAULT_2 = 0xea,
2417319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MEM_ATTR_FAULT_3 = 0xeb,
2427319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MEM_ATTR_NONCACHE_0 = 0xec,
2437319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MEM_ATTR_NONCACHE_1 = 0xed,
2447319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MEM_ATTR_NONCACHE_2 = 0xee,
2457319965fSBoris Brezillon 	DRM_PANFROST_EXCEPTION_MEM_ATTR_NONCACHE_3 = 0xef,
2467319965fSBoris Brezillon };
2477319965fSBoris Brezillon 
24830b5d4edSBoris Brezillon static inline bool
panfrost_exception_is_fault(u32 exception_code)24930b5d4edSBoris Brezillon panfrost_exception_is_fault(u32 exception_code)
25030b5d4edSBoris Brezillon {
25130b5d4edSBoris Brezillon 	return exception_code > DRM_PANFROST_EXCEPTION_MAX_NON_FAULT;
25230b5d4edSBoris Brezillon }
25330b5d4edSBoris Brezillon 
2546ef2f37fSBoris Brezillon const char *panfrost_exception_name(u32 exception_code);
2552905db27SBoris Brezillon bool panfrost_exception_needs_reset(const struct panfrost_device *pfdev,
2562905db27SBoris Brezillon 				    u32 exception_code);
257f3ba9122SRob Herring 
258229f4578SBoris Brezillon static inline void
panfrost_device_schedule_reset(struct panfrost_device * pfdev)259229f4578SBoris Brezillon panfrost_device_schedule_reset(struct panfrost_device *pfdev)
260229f4578SBoris Brezillon {
261a11c4711SBoris Brezillon 	atomic_set(&pfdev->reset.pending, 1);
262a11c4711SBoris Brezillon 	queue_work(pfdev->reset.wq, &pfdev->reset.work);
263229f4578SBoris Brezillon }
264229f4578SBoris Brezillon 
265f3ba9122SRob Herring #endif
266