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