xref: /linux/drivers/gpu/drm/panthor/panthor_device.h (revision bbda2935dc045176cd175b5ba9229a58481003d5)
1 /* SPDX-License-Identifier: GPL-2.0 or MIT */
2 /* Copyright 2018 Marty E. Plummer <hanetzer@startmail.com> */
3 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
4 /* Copyright 2023 Collabora ltd. */
5 
6 #ifndef __PANTHOR_DEVICE_H__
7 #define __PANTHOR_DEVICE_H__
8 
9 #include <linux/atomic.h>
10 #include <linux/io-pgtable.h>
11 #include <linux/regulator/consumer.h>
12 #include <linux/pm_runtime.h>
13 #include <linux/sched.h>
14 #include <linux/spinlock.h>
15 
16 #include <drm/drm_device.h>
17 #include <drm/drm_gem.h>
18 #include <drm/drm_mm.h>
19 #include <drm/gpu_scheduler.h>
20 #include <drm/panthor_drm.h>
21 
22 struct panthor_csf;
23 struct panthor_csf_ctx;
24 struct panthor_device;
25 struct panthor_gpu;
26 struct panthor_group_pool;
27 struct panthor_heap_pool;
28 struct panthor_hw;
29 struct panthor_job;
30 struct panthor_mmu;
31 struct panthor_fw;
32 struct panthor_perfcnt;
33 struct panthor_pwr;
34 struct panthor_vm;
35 struct panthor_vm_pool;
36 
37 /**
38  * struct panthor_soc_data - Panthor SoC Data
39  */
40 struct panthor_soc_data {
41 	/** @asn_hash_enable: True if GPU_L2_CONFIG_ASN_HASH_ENABLE must be set. */
42 	bool asn_hash_enable;
43 
44 	/** @asn_hash: ASN_HASH values when asn_hash_enable is true. */
45 	u32 asn_hash[3];
46 };
47 
48 /**
49  * enum panthor_device_pm_state - PM state
50  */
51 enum panthor_device_pm_state {
52 	/** @PANTHOR_DEVICE_PM_STATE_SUSPENDED: Device is suspended. */
53 	PANTHOR_DEVICE_PM_STATE_SUSPENDED = 0,
54 
55 	/** @PANTHOR_DEVICE_PM_STATE_RESUMING: Device is being resumed. */
56 	PANTHOR_DEVICE_PM_STATE_RESUMING,
57 
58 	/** @PANTHOR_DEVICE_PM_STATE_ACTIVE: Device is active. */
59 	PANTHOR_DEVICE_PM_STATE_ACTIVE,
60 
61 	/** @PANTHOR_DEVICE_PM_STATE_SUSPENDING: Device is being suspended. */
62 	PANTHOR_DEVICE_PM_STATE_SUSPENDING,
63 };
64 
65 enum panthor_irq_state {
66 	/** @PANTHOR_IRQ_STATE_ACTIVE: IRQ is active and ready to process events. */
67 	PANTHOR_IRQ_STATE_ACTIVE = 0,
68 	/** @PANTHOR_IRQ_STATE_PROCESSING: IRQ is currently processing events. */
69 	PANTHOR_IRQ_STATE_PROCESSING,
70 	/** @PANTHOR_IRQ_STATE_SUSPENDED: IRQ is suspended. */
71 	PANTHOR_IRQ_STATE_SUSPENDED,
72 	/** @PANTHOR_IRQ_STATE_SUSPENDING: IRQ is being suspended. */
73 	PANTHOR_IRQ_STATE_SUSPENDING,
74 };
75 
76 /**
77  * struct panthor_irq - IRQ data
78  *
79  * Used to automate IRQ handling for the 3 different IRQs we have in this driver.
80  */
81 struct panthor_irq {
82 	/** @ptdev: Panthor device */
83 	struct panthor_device *ptdev;
84 
85 	/** @iomem: CPU mapping of IRQ base address */
86 	void __iomem *iomem;
87 
88 	/** @irq: IRQ number. */
89 	int irq;
90 
91 	/** @mask: Values to write to xxx_INT_MASK if active. */
92 	u32 mask;
93 
94 	/**
95 	 * @lock: protects modifications to _INT_MASK, @mask and @state.
96 	 *
97 	 * In paths where _INT_MASK is updated based on a state
98 	 * transition/check, it's crucial for the state update/check to be
99 	 * inside the locked section, otherwise it introduces a race window
100 	 * leading to potential _INT_MASK inconsistencies.
101 	 */
102 	spinlock_t lock;
103 
104 	/**
105 	 * @state: one of &enum panthor_irq_state reflecting the current state.
106 	 *
107 	 * Must be accessed with lock held.
108 	 */
109 	enum panthor_irq_state state;
110 };
111 
112 /**
113  * enum panthor_device_profiling_mode - Profiling state
114  */
115 enum panthor_device_profiling_flags {
116 	/** @PANTHOR_DEVICE_PROFILING_DISABLED: Profiling is disabled. */
117 	PANTHOR_DEVICE_PROFILING_DISABLED = 0,
118 
119 	/** @PANTHOR_DEVICE_PROFILING_CYCLES: Sampling job cycles. */
120 	PANTHOR_DEVICE_PROFILING_CYCLES = BIT(0),
121 
122 	/** @PANTHOR_DEVICE_PROFILING_TIMESTAMP: Sampling job timestamp. */
123 	PANTHOR_DEVICE_PROFILING_TIMESTAMP = BIT(1),
124 
125 	/** @PANTHOR_DEVICE_PROFILING_ALL: Sampling everything. */
126 	PANTHOR_DEVICE_PROFILING_ALL =
127 	PANTHOR_DEVICE_PROFILING_CYCLES |
128 	PANTHOR_DEVICE_PROFILING_TIMESTAMP,
129 };
130 
131 /**
132  * struct panthor_device - Panthor device
133  */
134 struct panthor_device {
135 	/** @base: Base drm_device. */
136 	struct drm_device base;
137 
138 	/** @soc_data: Optional SoC data. */
139 	const struct panthor_soc_data *soc_data;
140 
141 	/** @phys_addr: Physical address of the iomem region. */
142 	phys_addr_t phys_addr;
143 
144 	/** @iomem: CPU mapping of the IOMEM region. */
145 	void __iomem *iomem;
146 
147 	/** @clks: GPU clocks. */
148 	struct {
149 		/** @core: Core clock. */
150 		struct clk *core;
151 
152 		/** @stacks: Stacks clock. This clock is optional. */
153 		struct clk *stacks;
154 
155 		/** @coregroup: Core group clock. This clock is optional. */
156 		struct clk *coregroup;
157 	} clks;
158 
159 	/** @coherent: True if the CPU/GPU are memory coherent. */
160 	bool coherent;
161 
162 	/** @gpu_info: GPU information. */
163 	struct drm_panthor_gpu_info gpu_info;
164 
165 	/** @csif_info: Command stream interface information. */
166 	struct drm_panthor_csif_info csif_info;
167 
168 	/** @mmu_info: MMU info */
169 	struct drm_panthor_mmu_info mmu_info;
170 
171 	/** @hw: GPU-specific data. */
172 	struct panthor_hw *hw;
173 
174 	/** @pwr: Power control management data. */
175 	struct panthor_pwr *pwr;
176 
177 	/** @gpu: GPU management data. */
178 	struct panthor_gpu *gpu;
179 
180 	/** @fw: FW management data. */
181 	struct panthor_fw *fw;
182 
183 	/** @mmu: MMU management data. */
184 	struct panthor_mmu *mmu;
185 
186 	/** @scheduler: Scheduler management data. */
187 	struct panthor_scheduler *scheduler;
188 
189 	/** @devfreq: Device frequency scaling management data. */
190 	struct panthor_devfreq *devfreq;
191 
192 	/** @reclaim: Reclaim related stuff */
193 	struct {
194 		/** @reclaim.shrinker: Shrinker instance */
195 		struct shrinker *shrinker;
196 
197 		/**
198 		 * @reclaim.unused: BOs with unused pages
199 		 *
200 		 * Basically all buffers that got mmapped, vmapped or GPU mapped and
201 		 * then unmapped. There should be no contention on these buffers,
202 		 * making them ideal to reclaim.
203 		 */
204 		struct drm_gem_lru unused;
205 
206 		/**
207 		 * @reclaim.mmapped: mmap()-ed buffers
208 		 *
209 		 * Those are relatively easy to reclaim since we don't need user
210 		 * agreement, we can simply teardown the mapping and let it fault on
211 		 * the next access.
212 		 */
213 		struct drm_gem_lru mmapped;
214 
215 		/**
216 		 * @reclaim.gpu_mapped_shared: shared BO LRU list
217 		 *
218 		 * That's the most tricky BO type to reclaim, because it involves
219 		 * tearing down all mappings in all VMs where this BO is mapped,
220 		 * which increases the risk of contention and thus decreases the
221 		 * likeliness of success.
222 		 */
223 		struct drm_gem_lru gpu_mapped_shared;
224 
225 		/**
226 		 * @reclaim.vms: VM LRU list
227 		 *
228 		 * VMs that have reclaimable BOs only mapped to a single VM are placed
229 		 * in this LRU. Reclaiming such BOs implies waiting for VM idleness
230 		 * (no in-flight GPU jobs targeting this VM), meaning we can't reclaim
231 		 * those if we're in a context where we can't block/sleep.
232 		 */
233 		struct list_head vms;
234 
235 		/**
236 		 * @reclaim.gpu_mapped_count: Global counter of pages that are GPU mapped
237 		 *
238 		 * Allows us to get the number of reclaimable pages without walking
239 		 * the vms and gpu_mapped_shared LRUs.
240 		 */
241 		long gpu_mapped_count;
242 
243 		/**
244 		 * @reclaim.retry_count: Number of times we ran the shrinker without being
245 		 * able to reclaim stuff
246 		 *
247 		 * Used to stop scanning GEMs when too many attempts were made
248 		 * without progress.
249 		 */
250 		atomic_t retry_count;
251 
252 #ifdef CONFIG_DEBUG_FS
253 		/**
254 		 * @reclaim.nr_pages_reclaimed_on_last_scan: Number of pages reclaimed on the last
255 		 * shrinker scan
256 		 */
257 		unsigned long nr_pages_reclaimed_on_last_scan;
258 #endif
259 	} reclaim;
260 
261 	/** @unplug: Device unplug related fields. */
262 	struct {
263 		/** @lock: Lock used to serialize unplug operations. */
264 		struct mutex lock;
265 
266 		/**
267 		 * @done: Completion object signaled when the unplug
268 		 * operation is done.
269 		 */
270 		struct completion done;
271 	} unplug;
272 
273 	/** @reset: Reset related fields. */
274 	struct {
275 		/** @wq: Ordered worqueud used to schedule reset operations. */
276 		struct workqueue_struct *wq;
277 
278 		/** @work: Reset work. */
279 		struct work_struct work;
280 
281 		/** @pending: Set to true if a reset is pending. */
282 		atomic_t pending;
283 
284 		/**
285 		 * @fast: True if the post_reset logic can proceed with a fast reset.
286 		 *
287 		 * A fast reset is just a reset where the driver doesn't reload the FW sections.
288 		 *
289 		 * Any time the firmware is properly suspended, a fast reset can take place.
290 		 * On the other hand, if the halt operation failed, the driver will reload
291 		 * all FW sections to make sure we start from a fresh state.
292 		 */
293 		bool fast;
294 	} reset;
295 
296 	/** @pm: Power management related data. */
297 	struct {
298 		/** @state: Power state. */
299 		atomic_t state;
300 
301 		/**
302 		 * @mmio_lock: Lock protecting MMIO userspace CPU mappings.
303 		 *
304 		 * This is needed to ensure we map the dummy IO pages when
305 		 * the device is being suspended, and the real IO pages when
306 		 * the device is being resumed. We can't just do with the
307 		 * state atomicity to deal with this race.
308 		 */
309 		struct mutex mmio_lock;
310 
311 		/**
312 		 * @dummy_latest_flush: Dummy LATEST_FLUSH page.
313 		 *
314 		 * Used to replace the real LATEST_FLUSH page when the GPU
315 		 * is suspended.
316 		 */
317 		struct page *dummy_latest_flush;
318 
319 		/** @recovery_needed: True when a resume attempt failed. */
320 		atomic_t recovery_needed;
321 	} pm;
322 
323 	/** @profile_mask: User-set profiling flags for job accounting. */
324 	u32 profile_mask;
325 
326 	/** @fast_rate: Maximum device clock frequency. Set by DVFS */
327 	unsigned long fast_rate;
328 
329 #ifdef CONFIG_DEBUG_FS
330 	/** @gems: Device-wide list of GEM objects owned by at least one file. */
331 	struct {
332 		/** @gems.lock: Protects the device-wide list of GEM objects. */
333 		struct mutex lock;
334 
335 		/** @node: Used to keep track of all the device's DRM objects */
336 		struct list_head node;
337 	} gems;
338 #endif
339 };
340 
341 struct panthor_gpu_usage {
342 	u64 time;
343 	u64 cycles;
344 };
345 
346 /**
347  * struct panthor_file - Panthor file
348  */
349 struct panthor_file {
350 	/** @ptdev: Device attached to this file. */
351 	struct panthor_device *ptdev;
352 
353 	/** @user_mmio: User MMIO related fields. */
354 	struct {
355 		/**
356 		 * @offset: Offset used for user MMIO mappings.
357 		 *
358 		 * This offset should not be used to check the type of mapping
359 		 * except in panthor_mmap(). After that point, MMIO mapping
360 		 * offsets have been adjusted to match
361 		 * DRM_PANTHOR_USER_MMIO_OFFSET and that macro should be used
362 		 * instead.
363 		 * Make sure this rule is followed at all times, because
364 		 * userspace is in control of the offset, and can change the
365 		 * value behind our back. Otherwise it can lead to erroneous
366 		 * branching happening in kernel space.
367 		 */
368 		u64 offset;
369 	} user_mmio;
370 
371 	/** @vms: VM pool attached to this file. */
372 	struct panthor_vm_pool *vms;
373 
374 	/** @groups: Scheduling group pool attached to this file. */
375 	struct panthor_group_pool *groups;
376 
377 	/** @stats: cycle and timestamp measures for job execution. */
378 	struct panthor_gpu_usage stats;
379 };
380 
381 int panthor_device_init(struct panthor_device *ptdev);
382 void panthor_device_unplug(struct panthor_device *ptdev);
383 
384 /**
385  * panthor_device_schedule_reset() - Schedules a reset operation
386  */
387 static inline void panthor_device_schedule_reset(struct panthor_device *ptdev)
388 {
389 	if (!atomic_cmpxchg(&ptdev->reset.pending, 0, 1) &&
390 	    atomic_read(&ptdev->pm.state) == PANTHOR_DEVICE_PM_STATE_ACTIVE)
391 		queue_work(ptdev->reset.wq, &ptdev->reset.work);
392 }
393 
394 /**
395  * panthor_device_reset_is_pending() - Checks if a reset is pending.
396  *
397  * Return: true if a reset is pending, false otherwise.
398  */
399 static inline bool panthor_device_reset_is_pending(struct panthor_device *ptdev)
400 {
401 	return atomic_read(&ptdev->reset.pending) != 0;
402 }
403 
404 int panthor_device_mmap_io(struct panthor_device *ptdev,
405 			   struct vm_area_struct *vma);
406 
407 int panthor_device_resume(struct device *dev);
408 int panthor_device_suspend(struct device *dev);
409 
410 static inline int panthor_device_resume_and_get(struct panthor_device *ptdev)
411 {
412 	int ret = pm_runtime_resume_and_get(ptdev->base.dev);
413 
414 	/* If the resume failed, we need to clear the runtime_error, which
415 	 * can done by forcing the RPM state to suspended. If multiple
416 	 * threads called panthor_device_resume_and_get(), we only want
417 	 * one of them to update the state, hence the cmpxchg. Note that a
418 	 * thread might enter panthor_device_resume_and_get() and call
419 	 * pm_runtime_resume_and_get() after another thread had attempted
420 	 * to resume and failed. This means we will end up with an error
421 	 * without even attempting a resume ourselves. The only risk here
422 	 * is to report an error when the second resume attempt might have
423 	 * succeeded. Given resume errors are not expected, this is probably
424 	 * something we can live with.
425 	 */
426 	if (ret && atomic_cmpxchg(&ptdev->pm.recovery_needed, 1, 0) == 1)
427 		pm_runtime_set_suspended(ptdev->base.dev);
428 
429 	return ret;
430 }
431 
432 enum drm_panthor_exception_type {
433 	DRM_PANTHOR_EXCEPTION_OK = 0x00,
434 	DRM_PANTHOR_EXCEPTION_TERMINATED = 0x04,
435 	DRM_PANTHOR_EXCEPTION_KABOOM = 0x05,
436 	DRM_PANTHOR_EXCEPTION_EUREKA = 0x06,
437 	DRM_PANTHOR_EXCEPTION_ACTIVE = 0x08,
438 	DRM_PANTHOR_EXCEPTION_CS_RES_TERM = 0x0f,
439 	DRM_PANTHOR_EXCEPTION_MAX_NON_FAULT = 0x3f,
440 	DRM_PANTHOR_EXCEPTION_CS_CONFIG_FAULT = 0x40,
441 	DRM_PANTHOR_EXCEPTION_CS_UNRECOVERABLE = 0x41,
442 	DRM_PANTHOR_EXCEPTION_CS_ENDPOINT_FAULT = 0x44,
443 	DRM_PANTHOR_EXCEPTION_CS_BUS_FAULT = 0x48,
444 	DRM_PANTHOR_EXCEPTION_CS_INSTR_INVALID = 0x49,
445 	DRM_PANTHOR_EXCEPTION_CS_CALL_STACK_OVERFLOW = 0x4a,
446 	DRM_PANTHOR_EXCEPTION_CS_INHERIT_FAULT = 0x4b,
447 	DRM_PANTHOR_EXCEPTION_INSTR_INVALID_PC = 0x50,
448 	DRM_PANTHOR_EXCEPTION_INSTR_INVALID_ENC = 0x51,
449 	DRM_PANTHOR_EXCEPTION_INSTR_BARRIER_FAULT = 0x55,
450 	DRM_PANTHOR_EXCEPTION_DATA_INVALID_FAULT = 0x58,
451 	DRM_PANTHOR_EXCEPTION_TILE_RANGE_FAULT = 0x59,
452 	DRM_PANTHOR_EXCEPTION_ADDR_RANGE_FAULT = 0x5a,
453 	DRM_PANTHOR_EXCEPTION_IMPRECISE_FAULT = 0x5b,
454 	DRM_PANTHOR_EXCEPTION_OOM = 0x60,
455 	DRM_PANTHOR_EXCEPTION_CSF_FW_INTERNAL_ERROR = 0x68,
456 	DRM_PANTHOR_EXCEPTION_CSF_RES_EVICTION_TIMEOUT = 0x69,
457 	DRM_PANTHOR_EXCEPTION_GPU_BUS_FAULT = 0x80,
458 	DRM_PANTHOR_EXCEPTION_GPU_SHAREABILITY_FAULT = 0x88,
459 	DRM_PANTHOR_EXCEPTION_SYS_SHAREABILITY_FAULT = 0x89,
460 	DRM_PANTHOR_EXCEPTION_GPU_CACHEABILITY_FAULT = 0x8a,
461 	DRM_PANTHOR_EXCEPTION_TRANSLATION_FAULT_0 = 0xc0,
462 	DRM_PANTHOR_EXCEPTION_TRANSLATION_FAULT_1 = 0xc1,
463 	DRM_PANTHOR_EXCEPTION_TRANSLATION_FAULT_2 = 0xc2,
464 	DRM_PANTHOR_EXCEPTION_TRANSLATION_FAULT_3 = 0xc3,
465 	DRM_PANTHOR_EXCEPTION_TRANSLATION_FAULT_4 = 0xc4,
466 	DRM_PANTHOR_EXCEPTION_PERM_FAULT_0 = 0xc8,
467 	DRM_PANTHOR_EXCEPTION_PERM_FAULT_1 = 0xc9,
468 	DRM_PANTHOR_EXCEPTION_PERM_FAULT_2 = 0xca,
469 	DRM_PANTHOR_EXCEPTION_PERM_FAULT_3 = 0xcb,
470 	DRM_PANTHOR_EXCEPTION_ACCESS_FLAG_1 = 0xd9,
471 	DRM_PANTHOR_EXCEPTION_ACCESS_FLAG_2 = 0xda,
472 	DRM_PANTHOR_EXCEPTION_ACCESS_FLAG_3 = 0xdb,
473 	DRM_PANTHOR_EXCEPTION_ADDR_SIZE_FAULT_IN = 0xe0,
474 	DRM_PANTHOR_EXCEPTION_ADDR_SIZE_FAULT_OUT0 = 0xe4,
475 	DRM_PANTHOR_EXCEPTION_ADDR_SIZE_FAULT_OUT1 = 0xe5,
476 	DRM_PANTHOR_EXCEPTION_ADDR_SIZE_FAULT_OUT2 = 0xe6,
477 	DRM_PANTHOR_EXCEPTION_ADDR_SIZE_FAULT_OUT3 = 0xe7,
478 	DRM_PANTHOR_EXCEPTION_MEM_ATTR_FAULT_0 = 0xe8,
479 	DRM_PANTHOR_EXCEPTION_MEM_ATTR_FAULT_1 = 0xe9,
480 	DRM_PANTHOR_EXCEPTION_MEM_ATTR_FAULT_2 = 0xea,
481 	DRM_PANTHOR_EXCEPTION_MEM_ATTR_FAULT_3 = 0xeb,
482 };
483 
484 /**
485  * panthor_exception_is_fault() - Checks if an exception is a fault.
486  *
487  * Return: true if the exception is a fault, false otherwise.
488  */
489 static inline bool
490 panthor_exception_is_fault(u32 exception_code)
491 {
492 	return exception_code > DRM_PANTHOR_EXCEPTION_MAX_NON_FAULT;
493 }
494 
495 const char *panthor_exception_name(struct panthor_device *ptdev,
496 				   u32 exception_code);
497 
498 static inline void gpu_write(void __iomem *iomem, u32 reg, u32 data)
499 {
500 	writel(data, iomem + reg);
501 }
502 
503 static inline u32 gpu_read(void __iomem *iomem, u32 reg)
504 {
505 	return readl(iomem + reg);
506 }
507 
508 static inline u32 gpu_read_relaxed(void __iomem *iomem, u32 reg)
509 {
510 	return readl_relaxed(iomem + reg);
511 }
512 
513 static inline void gpu_write64(void __iomem *iomem, u32 reg, u64 data)
514 {
515 	gpu_write(iomem, reg, lower_32_bits(data));
516 	gpu_write(iomem, reg + 4, upper_32_bits(data));
517 }
518 
519 static inline u64 gpu_read64(void __iomem *iomem, u32 reg)
520 {
521 	return (gpu_read(iomem, reg) | ((u64)gpu_read(iomem, reg + 4) << 32));
522 }
523 
524 static inline u64 gpu_read64_relaxed(void __iomem *iomem, u32 reg)
525 {
526 	return (gpu_read_relaxed(iomem, reg) |
527 		((u64)gpu_read_relaxed(iomem, reg + 4) << 32));
528 }
529 
530 static inline u64 gpu_read64_counter(void __iomem *iomem, u32 reg)
531 {
532 	u32 lo, hi1, hi2;
533 	do {
534 		hi1 = gpu_read(iomem, reg + 4);
535 		lo = gpu_read(iomem, reg);
536 		hi2 = gpu_read(iomem, reg + 4);
537 	} while (hi1 != hi2);
538 	return lo | ((u64)hi2 << 32);
539 }
540 
541 #define gpu_read_poll_timeout(iomem, reg, val, cond, delay_us, timeout_us)	\
542 	read_poll_timeout(gpu_read, val, cond, delay_us, timeout_us, false,	\
543 			  iomem, reg)
544 
545 #define gpu_read_poll_timeout_atomic(iomem, reg, val, cond, delay_us,		\
546 				     timeout_us)				\
547 	read_poll_timeout_atomic(gpu_read, val, cond, delay_us, timeout_us,	\
548 				 false, iomem, reg)
549 
550 #define gpu_read64_poll_timeout(iomem, reg, val, cond, delay_us, timeout_us)	\
551 	read_poll_timeout(gpu_read64, val, cond, delay_us, timeout_us, false,	\
552 			  iomem, reg)
553 
554 #define gpu_read64_poll_timeout_atomic(iomem, reg, val, cond, delay_us,		\
555 				       timeout_us)				\
556 	read_poll_timeout_atomic(gpu_read64, val, cond, delay_us, timeout_us,	\
557 				 false, iomem, reg)
558 
559 #define gpu_read_relaxed_poll_timeout_atomic(iomem, reg, val, cond, delay_us,	\
560 					     timeout_us)			\
561 	read_poll_timeout_atomic(gpu_read_relaxed, val, cond, delay_us,		\
562 				 timeout_us, false, iomem, reg)
563 
564 #define gpu_read64_relaxed_poll_timeout(iomem, reg, val, cond, delay_us,	\
565 					timeout_us)				\
566 	read_poll_timeout(gpu_read64_relaxed, val, cond, delay_us, timeout_us,	\
567 			  false, iomem, reg)
568 
569 #define INT_RAWSTAT 0x0
570 #define INT_CLEAR   0x4
571 #define INT_MASK    0x8
572 #define INT_STAT    0xc
573 
574 static inline irqreturn_t panthor_irq_default_raw_handler(int irq, void *data)
575 {
576 	struct panthor_irq *pirq = data;
577 
578 	guard(spinlock_irqsave)(&pirq->lock);
579 	if (pirq->state != PANTHOR_IRQ_STATE_ACTIVE)
580 		return IRQ_NONE;
581 
582 	if (!gpu_read(pirq->iomem, INT_STAT))
583 		return IRQ_NONE;
584 
585 	pirq->state = PANTHOR_IRQ_STATE_PROCESSING;
586 	gpu_write(pirq->iomem, INT_MASK, 0);
587 	return IRQ_WAKE_THREAD;
588 }
589 
590 static __always_inline irqreturn_t
591 panthor_irq_default_threaded_handler(void *data,
592 				     void (*slow_handler)(struct panthor_irq *, u32))
593 {
594 	struct panthor_irq *pirq = data;
595 	irqreturn_t ret = IRQ_NONE;
596 
597 	while (true) {
598 		/* It's safe to access pirq->mask without the lock held here. If a new
599 		 * event gets added to the mask and the corresponding IRQ is pending,
600 		 * we'll process it right away instead of adding an extra raw -> threaded
601 		 * round trip. If an event is removed and the status bit is set, it will
602 		 * be ignored, just like it would have been if the mask had been adjusted
603 		 * right before the HW event kicks in. TLDR; it's all expected races we're
604 		 * covered for.
605 		 */
606 		u32 status = gpu_read(pirq->iomem, INT_RAWSTAT) & pirq->mask;
607 
608 		if (!status)
609 			break;
610 
611 		slow_handler(pirq, status);
612 		ret = IRQ_HANDLED;
613 	}
614 
615 	scoped_guard(spinlock_irqsave, &pirq->lock) {
616 		if (pirq->state == PANTHOR_IRQ_STATE_PROCESSING) {
617 			pirq->state = PANTHOR_IRQ_STATE_ACTIVE;
618 			gpu_write(pirq->iomem, INT_MASK, pirq->mask);
619 		}
620 	}
621 
622 	return ret;
623 }
624 
625 static inline void panthor_irq_suspend(struct panthor_irq *pirq)
626 {
627 	scoped_guard(spinlock_irqsave, &pirq->lock) {
628 		pirq->state = PANTHOR_IRQ_STATE_SUSPENDING;
629 		gpu_write(pirq->iomem, INT_MASK, 0);
630 	}
631 	synchronize_irq(pirq->irq);
632 	scoped_guard(spinlock_irqsave, &pirq->lock)
633 		pirq->state = PANTHOR_IRQ_STATE_SUSPENDED;
634 }
635 
636 static inline void panthor_irq_resume(struct panthor_irq *pirq)
637 {
638 	guard(spinlock_irqsave)(&pirq->lock);
639 	pirq->state = PANTHOR_IRQ_STATE_ACTIVE;
640 	gpu_write(pirq->iomem, INT_CLEAR, pirq->mask);
641 	gpu_write(pirq->iomem, INT_MASK, pirq->mask);
642 }
643 
644 static inline void panthor_irq_enable_events(struct panthor_irq *pirq, u32 mask)
645 {
646 	guard(spinlock_irqsave)(&pirq->lock);
647 	pirq->mask |= mask;
648 
649 	/* The only situation where we need to write the new mask is if the IRQ is active.
650 	 * If it's being processed, the mask will be restored for us in _irq_threaded_handler()
651 	 * on the PROCESSING -> ACTIVE transition.
652 	 * If the IRQ is suspended/suspending, the mask is restored at resume time.
653 	 */
654 	if (pirq->state == PANTHOR_IRQ_STATE_ACTIVE)
655 		gpu_write(pirq->iomem, INT_MASK, pirq->mask);
656 }
657 
658 static inline void panthor_irq_disable_events(struct panthor_irq *pirq, u32 mask)
659 {
660 	guard(spinlock_irqsave)(&pirq->lock);
661 	pirq->mask &= ~mask;
662 
663 	/* The only situation where we need to write the new mask is if the IRQ is active.
664 	 * If it's being processed, the mask will be restored for us in _irq_threaded_handler()
665 	 * on the PROCESSING -> ACTIVE transition.
666 	 * If the IRQ is suspended/suspending, the mask is restored at resume time.
667 	 */
668 	if (pirq->state == PANTHOR_IRQ_STATE_ACTIVE)
669 		gpu_write(pirq->iomem, INT_MASK, pirq->mask);
670 }
671 
672 static inline int
673 panthor_irq_request(struct panthor_device *ptdev, struct panthor_irq *pirq,
674 		    int irq, void __iomem *iomem, const char *name,
675 		    irqreturn_t (*threaded_handler)(int, void *data))
676 {
677 	const char *full_name;
678 
679 	pirq->ptdev = ptdev;
680 	pirq->irq = irq;
681 	pirq->mask = 0;
682 	pirq->iomem = iomem;
683 	spin_lock_init(&pirq->lock);
684 	pirq->state = PANTHOR_IRQ_STATE_SUSPENDED;
685 
686 	full_name = devm_kasprintf(ptdev->base.dev, GFP_KERNEL, KBUILD_MODNAME "-%s", name);
687 	if (!full_name)
688 		return -ENOMEM;
689 
690 	gpu_write(pirq->iomem, INT_MASK, 0);
691 	return devm_request_threaded_irq(ptdev->base.dev, irq,
692 					 panthor_irq_default_raw_handler,
693 					 threaded_handler,
694 					 IRQF_SHARED, full_name, pirq);
695 }
696 
697 extern struct workqueue_struct *panthor_cleanup_wq;
698 
699 #endif
700