xref: /linux/drivers/gpu/drm/imagination/pvr_device.h (revision 3519e9ea13b49e7b37a20fa3a11a9e1fc5441af5)
1 /* SPDX-License-Identifier: GPL-2.0-only OR MIT */
2 /* Copyright (c) 2023 Imagination Technologies Ltd. */
3 
4 #ifndef PVR_DEVICE_H
5 #define PVR_DEVICE_H
6 
7 #include "pvr_ccb.h"
8 #include "pvr_device_info.h"
9 #include "pvr_fw.h"
10 #include "pvr_rogue_fwif_stream.h"
11 #include "pvr_stream.h"
12 
13 #include <drm/drm_device.h>
14 #include <drm/drm_file.h>
15 #include <drm/drm_mm.h>
16 
17 #include <linux/bits.h>
18 #include <linux/compiler_attributes.h>
19 #include <linux/compiler_types.h>
20 #include <linux/device.h>
21 #include <linux/io.h>
22 #include <linux/iopoll.h>
23 #include <linux/kernel.h>
24 #include <linux/math.h>
25 #include <linux/mutex.h>
26 #include <linux/spinlock_types.h>
27 #include <linux/timer.h>
28 #include <linux/types.h>
29 #include <linux/wait.h>
30 #include <linux/workqueue.h>
31 #include <linux/xarray.h>
32 
33 /* Forward declaration from <linux/clk.h>. */
34 struct clk;
35 
36 /* Forward declaration from <linux/firmware.h>. */
37 struct firmware;
38 
39 /* Forward declaration from <linux/pwrseq/consumer.h> */
40 struct pwrseq_desc;
41 
42 #define PVR_GPUID_STRING_MIN_LENGTH 7U
43 #define PVR_GPUID_STRING_MAX_LENGTH 32U
44 
45 /**
46  * struct pvr_gpu_id - Hardware GPU ID information for a PowerVR device
47  * @b: Branch ID.
48  * @v: Version ID.
49  * @n: Number of scalable units.
50  * @c: Config ID.
51  */
52 struct pvr_gpu_id {
53 	u16 b, v, n, c;
54 };
55 
56 /**
57  * struct pvr_fw_version - Firmware version information
58  * @major: Major version number.
59  * @minor: Minor version number.
60  */
61 struct pvr_fw_version {
62 	u16 major, minor;
63 };
64 
65 /**
66  * struct pvr_device_data - Platform specific data associated with a compatible string.
67  * @pwr_ops: Pointer to a structure with platform-specific power functions.
68  */
69 struct pvr_device_data {
70 	const struct pvr_power_sequence_ops *pwr_ops;
71 };
72 
73 /**
74  * struct pvr_device - powervr-specific wrapper for &struct drm_device
75  */
76 struct pvr_device {
77 	/**
78 	 * @base: The underlying &struct drm_device.
79 	 *
80 	 * Do not access this member directly, instead call
81 	 * from_pvr_device().
82 	 */
83 	struct drm_device base;
84 
85 	/** @gpu_id: GPU ID detected at runtime. */
86 	struct pvr_gpu_id gpu_id;
87 
88 	/**
89 	 * @features: Hardware feature information.
90 	 *
91 	 * Do not access this member directly, instead use PVR_HAS_FEATURE()
92 	 * or PVR_FEATURE_VALUE() macros.
93 	 */
94 	struct pvr_device_features features;
95 
96 	/**
97 	 * @quirks: Hardware quirk information.
98 	 *
99 	 * Do not access this member directly, instead use PVR_HAS_QUIRK().
100 	 */
101 	struct pvr_device_quirks quirks;
102 
103 	/**
104 	 * @enhancements: Hardware enhancement information.
105 	 *
106 	 * Do not access this member directly, instead use
107 	 * PVR_HAS_ENHANCEMENT().
108 	 */
109 	struct pvr_device_enhancements enhancements;
110 
111 	/** @fw_version: Firmware version detected at runtime. */
112 	struct pvr_fw_version fw_version;
113 
114 	/** @device_data: Pointer to platform-specific data. */
115 	const struct pvr_device_data *device_data;
116 
117 	/** @regs_resource: Resource representing device control registers. */
118 	struct resource *regs_resource;
119 
120 	/**
121 	 * @regs: Device control registers.
122 	 *
123 	 * These are mapped into memory when the device is initialized; that
124 	 * location is where this pointer points.
125 	 */
126 	void __iomem *regs;
127 
128 	/**
129 	 * @core_clk: General core clock.
130 	 *
131 	 * This is the primary clock used by the entire GPU core.
132 	 */
133 	struct clk *core_clk;
134 
135 	/**
136 	 * @sys_clk: Optional system bus clock.
137 	 *
138 	 * This may be used on some platforms to provide an independent clock to the SoC Interface
139 	 * (SOCIF). If present, this needs to be enabled/disabled together with @core_clk.
140 	 */
141 	struct clk *sys_clk;
142 
143 	/**
144 	 * @mem_clk: Optional memory clock.
145 	 *
146 	 * This may be used on some platforms to provide an independent clock to the Memory
147 	 * Interface (MEMIF). If present, this needs to be enabled/disabled together with @core_clk.
148 	 */
149 	struct clk *mem_clk;
150 
151 	/**
152 	 * @power: Optional power domain devices.
153 	 *
154 	 * On platforms with more than one power domain for the GPU, they are
155 	 * stored here in @domain_devs, along with links between them in
156 	 * @domain_links. The size of @domain_devs is given by @domain_count,
157 	 * while the size of @domain_links is (2 * @domain_count) - 1.
158 	 */
159 	struct pvr_device_power {
160 		struct device **domain_devs;
161 		struct device_link **domain_links;
162 
163 		u32 domain_count;
164 	} power;
165 
166 	/**
167 	 * @reset: Optional reset line.
168 	 *
169 	 * This may be used on some platforms to provide a reset line that needs to be de-asserted
170 	 * after power-up procedure. It would also need to be asserted after the power-down
171 	 * procedure.
172 	 */
173 	struct reset_control *reset;
174 
175 	/** @pwrseq: Pointer to a power sequencer, if one is used. */
176 	struct pwrseq_desc *pwrseq;
177 
178 	/** @irq: IRQ number. */
179 	int irq;
180 
181 	/** @fwccb: Firmware CCB. */
182 	struct pvr_ccb fwccb;
183 
184 	/**
185 	 * @kernel_vm_ctx: Virtual memory context used for kernel mappings.
186 	 *
187 	 * This is used for mappings in the firmware address region when a META firmware processor
188 	 * is in use.
189 	 *
190 	 * When a MIPS firmware processor is in use, this will be %NULL.
191 	 */
192 	struct pvr_vm_context *kernel_vm_ctx;
193 
194 	/** @fw_dev: Firmware related data. */
195 	struct pvr_fw_device fw_dev;
196 
197 	/** @stream_musthave_quirks: Bit array of "must-have" quirks for stream commands. */
198 	u32 stream_musthave_quirks[PVR_STREAM_TYPE_MAX][PVR_STREAM_EXTHDR_TYPE_MAX];
199 
200 	/**
201 	 * @mmu_flush_cache_flags: Records which MMU caches require flushing
202 	 * before submitting the next job.
203 	 */
204 	atomic_t mmu_flush_cache_flags;
205 
206 	/**
207 	 * @ctx_ids: Array of contexts belonging to this device. Array members
208 	 *           are of type "struct pvr_context *".
209 	 *
210 	 * This array is used to allocate IDs used by the firmware.
211 	 */
212 	struct xarray ctx_ids;
213 
214 	/**
215 	 * @free_list_ids: Array of free lists belonging to this device. Array members
216 	 *                 are of type "struct pvr_free_list *".
217 	 *
218 	 * This array is used to allocate IDs used by the firmware.
219 	 */
220 	struct xarray free_list_ids;
221 
222 	/**
223 	 * @job_ids: Array of jobs belonging to this device. Array members
224 	 *           are of type "struct pvr_job *".
225 	 */
226 	struct xarray job_ids;
227 
228 	/**
229 	 * @queues: Queue-related fields.
230 	 */
231 	struct {
232 		/** @queues.active: Active queue list. */
233 		struct list_head active;
234 
235 		/** @queues.idle: Idle queue list. */
236 		struct list_head idle;
237 
238 		/** @queues.lock: Lock protecting access to the active/idle
239 		 *  lists. */
240 		struct mutex lock;
241 	} queues;
242 
243 	/**
244 	 * @watchdog: Watchdog for communications with firmware.
245 	 */
246 	struct {
247 		/** @watchdog.work: Work item for watchdog callback. */
248 		struct delayed_work work;
249 
250 		/**
251 		 * @watchdog.old_kccb_cmds_executed: KCCB command execution
252 		 * count at last watchdog poll.
253 		 */
254 		u32 old_kccb_cmds_executed;
255 
256 		/**
257 		 * @watchdog.kccb_stall_count: Number of watchdog polls
258 		 * KCCB has been stalled for.
259 		 */
260 		u32 kccb_stall_count;
261 	} watchdog;
262 
263 	/**
264 	 * @kccb: Circular buffer for communications with firmware.
265 	 */
266 	struct {
267 		/** @kccb.ccb: Kernel CCB. */
268 		struct pvr_ccb ccb;
269 
270 		/** @kccb.rtn_q: Waitqueue for KCCB command return waiters. */
271 		wait_queue_head_t rtn_q;
272 
273 		/** @kccb.rtn_obj: Object representing KCCB return slots. */
274 		struct pvr_fw_object *rtn_obj;
275 
276 		/**
277 		 * @kccb.rtn: Pointer to CPU mapping of KCCB return slots.
278 		 * Must be accessed by READ_ONCE()/WRITE_ONCE().
279 		 */
280 		u32 *rtn;
281 
282 		/** @kccb.slot_count: Total number of KCCB slots available. */
283 		u32 slot_count;
284 
285 		/** @kccb.reserved_count: Number of KCCB slots reserved for
286 		 *  future use. */
287 		u32 reserved_count;
288 
289 		/**
290 		 * @kccb.waiters: List of KCCB slot waiters.
291 		 */
292 		struct list_head waiters;
293 
294 		/** @kccb.fence_ctx: KCCB fence context. */
295 		struct {
296 			/** @kccb.fence_ctx.id: KCCB fence context ID
297 			 *  allocated with dma_fence_context_alloc(). */
298 			u64 id;
299 
300 			/** @kccb.fence_ctx.seqno: Sequence number incremented
301 			 *  each time a fence is created. */
302 			atomic_t seqno;
303 
304 			/**
305 			 * @kccb.fence_ctx.lock: Lock used to synchronize
306 			 * access to fences allocated by this context.
307 			 */
308 			spinlock_t lock;
309 		} fence_ctx;
310 	} kccb;
311 
312 	/**
313 	 * @lost: %true if the device has been lost.
314 	 *
315 	 * This variable is set if the device has become irretrievably unavailable, e.g. if the
316 	 * firmware processor has stopped responding and can not be revived via a hard reset.
317 	 */
318 	bool lost;
319 
320 	/**
321 	 * @reset_sem: Reset semaphore.
322 	 *
323 	 * GPU reset code will lock this for writing. Any code that submits commands to the firmware
324 	 * that isn't in an IRQ handler or on the scheduler workqueue must lock this for reading.
325 	 * Once this has been successfully locked, &pvr_dev->lost _must_ be checked, and -%EIO must
326 	 * be returned if it is set.
327 	 */
328 	struct rw_semaphore reset_sem;
329 
330 	/** @sched_wq: Workqueue for schedulers. */
331 	struct workqueue_struct *sched_wq;
332 
333 	/**
334 	 * @ctx_list_lock: Lock to be held when accessing the context list in
335 	 *  struct pvr_file.
336 	 */
337 	spinlock_t ctx_list_lock;
338 
339 	/** @has_safety_events: Whether this device can raise safety events. */
340 	bool has_safety_events;
341 };
342 
343 /**
344  * struct pvr_file - powervr-specific data to be assigned to &struct
345  * drm_file.driver_priv
346  */
347 struct pvr_file {
348 	/**
349 	 * @file: A reference to the parent &struct drm_file.
350 	 *
351 	 * Do not access this member directly, instead call from_pvr_file().
352 	 */
353 	struct drm_file *file;
354 
355 	/**
356 	 * @pvr_dev: A reference to the powervr-specific wrapper for the
357 	 * associated device. Saves on repeated calls to to_pvr_device().
358 	 */
359 	struct pvr_device *pvr_dev;
360 
361 	/**
362 	 * @ctx_handles: Array of contexts belonging to this file. Array members
363 	 * are of type "struct pvr_context *".
364 	 *
365 	 * This array is used to allocate handles returned to userspace.
366 	 */
367 	struct xarray ctx_handles;
368 
369 	/**
370 	 * @free_list_handles: Array of free lists belonging to this file. Array
371 	 * members are of type "struct pvr_free_list *".
372 	 *
373 	 * This array is used to allocate handles returned to userspace.
374 	 */
375 	struct xarray free_list_handles;
376 
377 	/**
378 	 * @hwrt_handles: Array of HWRT datasets belonging to this file. Array
379 	 * members are of type "struct pvr_hwrt_dataset *".
380 	 *
381 	 * This array is used to allocate handles returned to userspace.
382 	 */
383 	struct xarray hwrt_handles;
384 
385 	/**
386 	 * @vm_ctx_handles: Array of VM contexts belonging to this file. Array
387 	 * members are of type "struct pvr_vm_context *".
388 	 *
389 	 * This array is used to allocate handles returned to userspace.
390 	 */
391 	struct xarray vm_ctx_handles;
392 
393 	/** @contexts: PVR context list. */
394 	struct list_head contexts;
395 };
396 
397 /**
398  * PVR_HAS_FEATURE() - Tests whether a PowerVR device has a given feature
399  * @pvr_dev: [IN] Target PowerVR device.
400  * @feature: [IN] Hardware feature name.
401  *
402  * Feature names are derived from those found in &struct pvr_device_features by
403  * dropping the 'has_' prefix, which is applied by this macro.
404  *
405  * Return:
406  *  * true if the named feature is present in the hardware
407  *  * false if the named feature is not present in the hardware
408  */
409 #define PVR_HAS_FEATURE(pvr_dev, feature) ((pvr_dev)->features.has_##feature)
410 
411 /**
412  * PVR_FEATURE_VALUE() - Gets a PowerVR device feature value
413  * @pvr_dev: [IN] Target PowerVR device.
414  * @feature: [IN] Feature name.
415  * @value_out: [OUT] Feature value.
416  *
417  * This macro will get a feature value for those features that have values.
418  * If the feature is not present, nothing will be stored to @value_out.
419  *
420  * Feature names are derived from those found in &struct pvr_device_features by
421  * dropping the 'has_' prefix.
422  *
423  * Return:
424  *  * 0 on success, or
425  *  * -%EINVAL if the named feature is not present in the hardware
426  */
427 #define PVR_FEATURE_VALUE(pvr_dev, feature, value_out)             \
428 	({                                                         \
429 		struct pvr_device *_pvr_dev = pvr_dev;             \
430 		int _ret = -EINVAL;                                \
431 		if (_pvr_dev->features.has_##feature) {            \
432 			*(value_out) = _pvr_dev->features.feature; \
433 			_ret = 0;                                  \
434 		}                                                  \
435 		_ret;                                              \
436 	})
437 
438 /**
439  * PVR_HAS_QUIRK() - Tests whether a physical device has a given quirk
440  * @pvr_dev: [IN] Target PowerVR device.
441  * @quirk: [IN] Hardware quirk name.
442  *
443  * Quirk numbers are derived from those found in #pvr_device_quirks by
444  * dropping the 'has_brn' prefix, which is applied by this macro.
445  *
446  * Returns
447  *  * true if the quirk is present in the hardware, or
448  *  * false if the quirk is not present in the hardware.
449  */
450 #define PVR_HAS_QUIRK(pvr_dev, quirk) ((pvr_dev)->quirks.has_brn##quirk)
451 
452 /**
453  * PVR_HAS_ENHANCEMENT() - Tests whether a physical device has a given
454  *                         enhancement
455  * @pvr_dev: [IN] Target PowerVR device.
456  * @enhancement: [IN] Hardware enhancement name.
457  *
458  * Enhancement numbers are derived from those found in #pvr_device_enhancements
459  * by dropping the 'has_ern' prefix, which is applied by this macro.
460  *
461  * Returns
462  *  * true if the enhancement is present in the hardware, or
463  *  * false if the enhancement is not present in the hardware.
464  */
465 #define PVR_HAS_ENHANCEMENT(pvr_dev, enhancement) ((pvr_dev)->enhancements.has_ern##enhancement)
466 
467 #define from_pvr_device(pvr_dev) (&(pvr_dev)->base)
468 
469 #define to_pvr_device(drm_dev) container_of_const(drm_dev, struct pvr_device, base)
470 
471 #define from_pvr_file(pvr_file) ((pvr_file)->file)
472 
473 #define to_pvr_file(file) ((file)->driver_priv)
474 
475 /**
476  * PVR_PACKED_BVNC() - Packs B, V, N and C values into a 64-bit unsigned integer
477  * @b: Branch ID.
478  * @v: Version ID.
479  * @n: Number of scalable units.
480  * @c: Config ID.
481  *
482  * The packed layout is as follows:
483  *
484  *    +--------+--------+--------+-------+
485  *    | 63..48 | 47..32 | 31..16 | 15..0 |
486  *    +========+========+========+=======+
487  *    | B      | V      | N      | C     |
488  *    +--------+--------+--------+-------+
489  *
490  * pvr_gpu_id_to_packed_bvnc() should be used instead of this macro when a
491  * &struct pvr_gpu_id is available in order to ensure proper type checking.
492  *
493  * Return: Packed BVNC.
494  */
495 /* clang-format off */
496 #define PVR_PACKED_BVNC(b, v, n, c) \
497 	((((u64)(b) & GENMASK_ULL(15, 0)) << 48) | \
498 	 (((u64)(v) & GENMASK_ULL(15, 0)) << 32) | \
499 	 (((u64)(n) & GENMASK_ULL(15, 0)) << 16) | \
500 	 (((u64)(c) & GENMASK_ULL(15, 0)) <<  0))
501 /* clang-format on */
502 
503 /**
504  * pvr_gpu_id_to_packed_bvnc() - Packs B, V, N and C values into a 64-bit
505  * unsigned integer
506  * @gpu_id: GPU ID.
507  *
508  * The packed layout is as follows:
509  *
510  *    +--------+--------+--------+-------+
511  *    | 63..48 | 47..32 | 31..16 | 15..0 |
512  *    +========+========+========+=======+
513  *    | B      | V      | N      | C     |
514  *    +--------+--------+--------+-------+
515  *
516  * This should be used in preference to PVR_PACKED_BVNC() when a &struct
517  * pvr_gpu_id is available in order to ensure proper type checking.
518  *
519  * Return: Packed BVNC.
520  */
521 static __always_inline u64
522 pvr_gpu_id_to_packed_bvnc(const struct pvr_gpu_id *gpu_id)
523 {
524 	return PVR_PACKED_BVNC(gpu_id->b, gpu_id->v, gpu_id->n, gpu_id->c);
525 }
526 
527 static __always_inline void
528 packed_bvnc_to_pvr_gpu_id(u64 bvnc, struct pvr_gpu_id *gpu_id)
529 {
530 	gpu_id->b = (bvnc & GENMASK_ULL(63, 48)) >> 48;
531 	gpu_id->v = (bvnc & GENMASK_ULL(47, 32)) >> 32;
532 	gpu_id->n = (bvnc & GENMASK_ULL(31, 16)) >> 16;
533 	gpu_id->c = bvnc & GENMASK_ULL(15, 0);
534 }
535 
536 int pvr_device_init(struct pvr_device *pvr_dev);
537 void pvr_device_fini(struct pvr_device *pvr_dev);
538 void pvr_device_reset(struct pvr_device *pvr_dev);
539 
540 bool
541 pvr_device_has_uapi_quirk(struct pvr_device *pvr_dev, u32 quirk);
542 bool
543 pvr_device_has_uapi_enhancement(struct pvr_device *pvr_dev, u32 enhancement);
544 bool
545 pvr_device_has_feature(struct pvr_device *pvr_dev, u32 feature);
546 
547 #if IS_ENABLED(CONFIG_KUNIT)
548 int pvr_gpuid_decode_string(const struct pvr_device *pvr_dev,
549 			    const char *param_bvnc, struct pvr_gpu_id *gpu_id);
550 #endif
551 
552 /**
553  * PVR_CR_FIELD_GET() - Extract a single field from a PowerVR control register
554  * @val: Value of the target register.
555  * @field: Field specifier, as defined in "pvr_rogue_cr_defs.h".
556  *
557  * Return: The extracted field.
558  */
559 #define PVR_CR_FIELD_GET(val, field) FIELD_GET(~ROGUE_CR_##field##_CLRMSK, val)
560 
561 /**
562  * pvr_cr_read32() - Read a 32-bit register from a PowerVR device
563  * @pvr_dev: Target PowerVR device.
564  * @reg: Target register.
565  *
566  * Return: The value of the requested register.
567  */
568 static __always_inline u32
569 pvr_cr_read32(const struct pvr_device *pvr_dev, u32 reg)
570 {
571 	return ioread32(pvr_dev->regs + reg);
572 }
573 
574 /**
575  * pvr_cr_read64() - Read a 64-bit register from a PowerVR device
576  * @pvr_dev: Target PowerVR device.
577  * @reg: Target register.
578  *
579  * Return: The value of the requested register.
580  */
581 static __always_inline u64
582 pvr_cr_read64(const struct pvr_device *pvr_dev, u32 reg)
583 {
584 	return ioread64(pvr_dev->regs + reg);
585 }
586 
587 /**
588  * pvr_cr_write32() - Write to a 32-bit register in a PowerVR device
589  * @pvr_dev: Target PowerVR device.
590  * @reg: Target register.
591  * @val: Value to write.
592  */
593 static __always_inline void
594 pvr_cr_write32(struct pvr_device *pvr_dev, u32 reg, u32 val)
595 {
596 	iowrite32(val, pvr_dev->regs + reg);
597 }
598 
599 /**
600  * pvr_cr_write64() - Write to a 64-bit register in a PowerVR device
601  * @pvr_dev: Target PowerVR device.
602  * @reg: Target register.
603  * @val: Value to write.
604  */
605 static __always_inline void
606 pvr_cr_write64(struct pvr_device *pvr_dev, u32 reg, u64 val)
607 {
608 	iowrite64(val, pvr_dev->regs + reg);
609 }
610 
611 /**
612  * pvr_cr_poll_reg32() - Wait for a 32-bit register to match a given value by
613  *                       polling
614  * @pvr_dev: Target PowerVR device.
615  * @reg_addr: Address of register.
616  * @reg_value: Expected register value (after masking).
617  * @reg_mask: Mask of bits valid for comparison with @reg_value.
618  * @timeout_usec: Timeout length, in us.
619  *
620  * Returns:
621  *  * 0 on success, or
622  *  * -%ETIMEDOUT on timeout.
623  */
624 static __always_inline int
625 pvr_cr_poll_reg32(struct pvr_device *pvr_dev, u32 reg_addr, u32 reg_value,
626 		  u32 reg_mask, u64 timeout_usec)
627 {
628 	u32 value;
629 
630 	return readl_poll_timeout(pvr_dev->regs + reg_addr, value,
631 		(value & reg_mask) == reg_value, 0, timeout_usec);
632 }
633 
634 /**
635  * pvr_cr_poll_reg64() - Wait for a 64-bit register to match a given value by
636  *                       polling
637  * @pvr_dev: Target PowerVR device.
638  * @reg_addr: Address of register.
639  * @reg_value: Expected register value (after masking).
640  * @reg_mask: Mask of bits valid for comparison with @reg_value.
641  * @timeout_usec: Timeout length, in us.
642  *
643  * Returns:
644  *  * 0 on success, or
645  *  * -%ETIMEDOUT on timeout.
646  */
647 static __always_inline int
648 pvr_cr_poll_reg64(struct pvr_device *pvr_dev, u32 reg_addr, u64 reg_value,
649 		  u64 reg_mask, u64 timeout_usec)
650 {
651 	u64 value;
652 
653 	return readq_poll_timeout(pvr_dev->regs + reg_addr, value,
654 		(value & reg_mask) == reg_value, 0, timeout_usec);
655 }
656 
657 /**
658  * pvr_round_up_to_cacheline_size() - Round up a provided size to be cacheline
659  *                                    aligned
660  * @pvr_dev: Target PowerVR device.
661  * @size: Initial size, in bytes.
662  *
663  * Returns:
664  *  * Size aligned to cacheline size.
665  */
666 static __always_inline size_t
667 pvr_round_up_to_cacheline_size(struct pvr_device *pvr_dev, size_t size)
668 {
669 	u16 slc_cacheline_size_bits = 0;
670 	u16 slc_cacheline_size_bytes;
671 
672 	WARN_ON(!PVR_HAS_FEATURE(pvr_dev, slc_cache_line_size_bits));
673 	PVR_FEATURE_VALUE(pvr_dev, slc_cache_line_size_bits,
674 			  &slc_cacheline_size_bits);
675 	slc_cacheline_size_bytes = slc_cacheline_size_bits / 8;
676 
677 	return round_up(size, slc_cacheline_size_bytes);
678 }
679 
680 /**
681  * DOC: IOCTL validation helpers
682  *
683  * To validate the constraints imposed on IOCTL argument structs, a collection
684  * of macros and helper functions exist in ``pvr_device.h``.
685  *
686  * Of the current helpers, it should only be necessary to call
687  * PVR_IOCTL_UNION_PADDING_CHECK() directly. This macro should be used once in
688  * every code path which extracts a union member from a struct passed from
689  * userspace.
690  */
691 
692 /**
693  * pvr_ioctl_union_padding_check() - Validate that the implicit padding between
694  * the end of a union member and the end of the union itself is zeroed.
695  * @instance: Pointer to the instance of the struct to validate.
696  * @union_offset: Offset into the type of @instance of the target union. Must
697  * be 64-bit aligned.
698  * @union_size: Size of the target union in the type of @instance. Must be
699  * 64-bit aligned.
700  * @member_size: Size of the target member in the target union specified by
701  * @union_offset and @union_size. It is assumed that the offset of the target
702  * member is zero relative to @union_offset. Must be 64-bit aligned.
703  *
704  * You probably want to use PVR_IOCTL_UNION_PADDING_CHECK() instead of calling
705  * this function directly, since that macro abstracts away much of the setup,
706  * and also provides some static validation. See its docs for details.
707  *
708  * Return:
709  *  * %true if every byte between the end of the used member of the union and
710  *    the end of that union is zeroed, or
711  *  * %false otherwise.
712  */
713 static __always_inline bool
714 pvr_ioctl_union_padding_check(void *instance, size_t union_offset,
715 			      size_t union_size, size_t member_size)
716 {
717 	/*
718 	 * void pointer arithmetic is technically illegal - cast to a byte
719 	 * pointer so this addition works safely.
720 	 */
721 	void *padding_start = ((u8 *)instance) + union_offset + member_size;
722 	size_t padding_size = union_size - member_size;
723 
724 	return mem_is_zero(padding_start, padding_size);
725 }
726 
727 /**
728  * PVR_STATIC_ASSERT_64BIT_ALIGNED() - Inline assertion for 64-bit alignment.
729  * @static_expr_: Target expression to evaluate.
730  *
731  * If @static_expr_ does not evaluate to a constant integer which would be a
732  * 64-bit aligned address (i.e. a multiple of 8), compilation will fail.
733  *
734  * Return:
735  * The value of @static_expr_.
736  */
737 #define PVR_STATIC_ASSERT_64BIT_ALIGNED(static_expr_)                     \
738 	({                                                                \
739 		static_assert(((static_expr_) & (sizeof(u64) - 1)) == 0); \
740 		(static_expr_);                                           \
741 	})
742 
743 /**
744  * PVR_IOCTL_UNION_PADDING_CHECK() - Validate that the implicit padding between
745  * the end of a union member and the end of the union itself is zeroed.
746  * @struct_instance_: An expression which evaluates to a pointer to a UAPI data
747  * struct.
748  * @union_: The name of the union member of @struct_instance_ to check. If the
749  * union member is nested within the type of @struct_instance_, this may
750  * contain the member access operator (".").
751  * @member_: The name of the member of @union_ to assess.
752  *
753  * This is a wrapper around pvr_ioctl_union_padding_check() which performs
754  * alignment checks and simplifies things for the caller.
755  *
756  * Return:
757  *  * %true if every byte in @struct_instance_ between the end of @member_ and
758  *    the end of @union_ is zeroed, or
759  *  * %false otherwise.
760  */
761 #define PVR_IOCTL_UNION_PADDING_CHECK(struct_instance_, union_, member_)     \
762 	({                                                                   \
763 		typeof(struct_instance_) __instance = (struct_instance_);    \
764 		size_t __union_offset = PVR_STATIC_ASSERT_64BIT_ALIGNED(     \
765 			offsetof(typeof(*__instance), union_));              \
766 		size_t __union_size = PVR_STATIC_ASSERT_64BIT_ALIGNED(       \
767 			sizeof(__instance->union_));                         \
768 		size_t __member_size = PVR_STATIC_ASSERT_64BIT_ALIGNED(      \
769 			sizeof(__instance->union_.member_));                 \
770 		pvr_ioctl_union_padding_check(__instance, __union_offset,    \
771 					      __union_size, __member_size);  \
772 	})
773 
774 /*
775  * These utility functions should more properly be placed in pvr_fw.h, but that
776  * would cause a dependency cycle between that header and this one. Since
777  * they're primarily used in pvr_device.c, let's put them in here for now.
778  */
779 
780 static __always_inline bool
781 pvr_fw_irq_pending(struct pvr_device *pvr_dev)
782 {
783 	return pvr_dev->fw_dev.defs->irq_pending(pvr_dev);
784 }
785 
786 static __always_inline void
787 pvr_fw_irq_clear(struct pvr_device *pvr_dev)
788 {
789 	pvr_dev->fw_dev.defs->irq_clear(pvr_dev);
790 }
791 
792 #endif /* PVR_DEVICE_H */
793