summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/imagination/pvr_device.h
blob: ecdd5767d8ef535dd8ced7ab2f7d3d3539aff263 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
/* Copyright (c) 2023 Imagination Technologies Ltd. */

#ifndef PVR_DEVICE_H
#define PVR_DEVICE_H

#include "pvr_ccb.h"
#include "pvr_device_info.h"
#include "pvr_fw.h"
#include "pvr_params.h"
#include "pvr_rogue_fwif_stream.h"
#include "pvr_stream.h"

#include <drm/drm_device.h>
#include <drm/drm_file.h>
#include <drm/drm_mm.h>

#include <linux/bits.h>
#include <linux/compiler_attributes.h>
#include <linux/compiler_types.h>
#include <linux/io.h>
#include <linux/iopoll.h>
#include <linux/kernel.h>
#include <linux/math.h>
#include <linux/mutex.h>
#include <linux/timer.h>
#include <linux/types.h>
#include <linux/wait.h>
#include <linux/workqueue.h>
#include <linux/xarray.h>

/* Forward declaration from <linux/clk.h>. */
struct clk;

/* Forward declaration from <linux/firmware.h>. */
struct firmware;

/**
 * struct pvr_gpu_id - Hardware GPU ID information for a PowerVR device
 * @b: Branch ID.
 * @v: Version ID.
 * @n: Number of scalable units.
 * @c: Config ID.
 */
struct pvr_gpu_id {
	u16 b, v, n, c;
};

/**
 * struct pvr_fw_version - Firmware version information
 * @major: Major version number.
 * @minor: Minor version number.
 */
struct pvr_fw_version {
	u16 major, minor;
};

/**
 * struct pvr_device - powervr-specific wrapper for &struct drm_device
 */
struct pvr_device {
	/**
	 * @base: The underlying &struct drm_device.
	 *
	 * Do not access this member directly, instead call
	 * from_pvr_device().
	 */
	struct drm_device base;

	/** @gpu_id: GPU ID detected at runtime. */
	struct pvr_gpu_id gpu_id;

	/**
	 * @features: Hardware feature information.
	 *
	 * Do not access this member directly, instead use PVR_HAS_FEATURE()
	 * or PVR_FEATURE_VALUE() macros.
	 */
	struct pvr_device_features features;

	/**
	 * @quirks: Hardware quirk information.
	 *
	 * Do not access this member directly, instead use PVR_HAS_QUIRK().
	 */
	struct pvr_device_quirks quirks;

	/**
	 * @enhancements: Hardware enhancement information.
	 *
	 * Do not access this member directly, instead use
	 * PVR_HAS_ENHANCEMENT().
	 */
	struct pvr_device_enhancements enhancements;

	/** @fw_version: Firmware version detected at runtime. */
	struct pvr_fw_version fw_version;

	/** @regs_resource: Resource representing device control registers. */
	struct resource *regs_resource;

	/**
	 * @regs: Device control registers.
	 *
	 * These are mapped into memory when the device is initialized; that
	 * location is where this pointer points.
	 */
	void __iomem *regs;

	/**
	 * @core_clk: General core clock.
	 *
	 * This is the primary clock used by the entire GPU core.
	 */
	struct clk *core_clk;

	/**
	 * @sys_clk: Optional system bus clock.
	 *
	 * This may be used on some platforms to provide an independent clock to the SoC Interface
	 * (SOCIF). If present, this needs to be enabled/disabled together with @core_clk.
	 */
	struct clk *sys_clk;

	/**
	 * @mem_clk: Optional memory clock.
	 *
	 * This may be used on some platforms to provide an independent clock to the Memory
	 * Interface (MEMIF). If present, this needs to be enabled/disabled together with @core_clk.
	 */
	struct clk *mem_clk;

	/** @irq: IRQ number. */
	int irq;

	/** @fwccb: Firmware CCB. */
	struct pvr_ccb fwccb;

	/**
	 * @kernel_vm_ctx: Virtual memory context used for kernel mappings.
	 *
	 * This is used for mappings in the firmware address region when a META firmware processor
	 * is in use.
	 *
	 * When a MIPS firmware processor is in use, this will be %NULL.
	 */
	struct pvr_vm_context *kernel_vm_ctx;

	/** @fw_dev: Firmware related data. */
	struct pvr_fw_device fw_dev;

	/**
	 * @params: Device-specific parameters.
	 *
	 *          The values of these parameters are initialized from the
	 *          defaults specified as module parameters. They may be
	 *          modified at runtime via debugfs (if enabled).
	 */
	struct pvr_device_params params;

	/** @stream_musthave_quirks: Bit array of "must-have" quirks for stream commands. */
	u32 stream_musthave_quirks[PVR_STREAM_TYPE_MAX][PVR_STREAM_EXTHDR_TYPE_MAX];

	/**
	 * @mmu_flush_cache_flags: Records which MMU caches require flushing
	 * before submitting the next job.
	 */
	atomic_t mmu_flush_cache_flags;

	/**
	 * @ctx_ids: Array of contexts belonging to this device. Array members
	 *           are of type "struct pvr_context *".
	 *
	 * This array is used to allocate IDs used by the firmware.
	 */
	struct xarray ctx_ids;

	/**
	 * @free_list_ids: Array of free lists belonging to this device. Array members
	 *                 are of type "struct pvr_free_list *".
	 *
	 * This array is used to allocate IDs used by the firmware.
	 */
	struct xarray free_list_ids;

	/**
	 * @job_ids: Array of jobs belonging to this device. Array members
	 *           are of type "struct pvr_job *".
	 */
	struct xarray job_ids;

	/**
	 * @queues: Queue-related fields.
	 */
	struct {
		/** @queues.active: Active queue list. */
		struct list_head active;

		/** @queues.idle: Idle queue list. */
		struct list_head idle;

		/** @queues.lock: Lock protecting access to the active/idle
		 *  lists. */
		struct mutex lock;
	} queues;

	/**
	 * @watchdog: Watchdog for communications with firmware.
	 */
	struct {
		/** @watchdog.work: Work item for watchdog callback. */
		struct delayed_work work;

		/**
		 * @watchdog.old_kccb_cmds_executed: KCCB command execution
		 * count at last watchdog poll.
		 */
		u32 old_kccb_cmds_executed;

		/**
		 * @watchdog.kccb_stall_count: Number of watchdog polls
		 * KCCB has been stalled for.
		 */
		u32 kccb_stall_count;
	} watchdog;

	/**
	 * @kccb: Circular buffer for communications with firmware.
	 */
	struct {
		/** @kccb.ccb: Kernel CCB. */
		struct pvr_ccb ccb;

		/** @kccb.rtn_q: Waitqueue for KCCB command return waiters. */
		wait_queue_head_t rtn_q;

		/** @kccb.rtn_obj: Object representing KCCB return slots. */
		struct pvr_fw_object *rtn_obj;

		/**
		 * @kccb.rtn: Pointer to CPU mapping of KCCB return slots.
		 * Must be accessed by READ_ONCE()/WRITE_ONCE().
		 */
		u32 *rtn;

		/** @kccb.slot_count: Total number of KCCB slots available. */
		u32 slot_count;

		/** @kccb.reserved_count: Number of KCCB slots reserved for
		 *  future use. */
		u32 reserved_count;

		/**
		 * @kccb.waiters: List of KCCB slot waiters.
		 */
		struct list_head waiters;

		/** @kccb.fence_ctx: KCCB fence context. */
		struct {
			/** @kccb.fence_ctx.id: KCCB fence context ID
			 *  allocated with dma_fence_context_alloc(). */
			u64 id;

			/** @kccb.fence_ctx.seqno: Sequence number incremented
			 *  each time a fence is created. */
			atomic_t seqno;

			/**
			 * @kccb.fence_ctx.lock: Lock used to synchronize
			 * access to fences allocated by this context.
			 */
			spinlock_t lock;
		} fence_ctx;
	} kccb;

	/**
	 * @lost: %true if the device has been lost.
	 *
	 * This variable is set if the device has become irretrievably unavailable, e.g. if the
	 * firmware processor has stopped responding and can not be revived via a hard reset.
	 */
	bool lost;

	/**
	 * @reset_sem: Reset semaphore.
	 *
	 * GPU reset code will lock this for writing. Any code that submits commands to the firmware
	 * that isn't in an IRQ handler or on the scheduler workqueue must lock this for reading.
	 * Once this has been successfully locked, &pvr_dev->lost _must_ be checked, and -%EIO must
	 * be returned if it is set.
	 */
	struct rw_semaphore reset_sem;

	/** @sched_wq: Workqueue for schedulers. */
	struct workqueue_struct *sched_wq;
};

/**
 * struct pvr_file - powervr-specific data to be assigned to &struct
 * drm_file.driver_priv
 */
struct pvr_file {
	/**
	 * @file: A reference to the parent &struct drm_file.
	 *
	 * Do not access this member directly, instead call from_pvr_file().
	 */
	struct drm_file *file;

	/**
	 * @pvr_dev: A reference to the powervr-specific wrapper for the
	 * associated device. Saves on repeated calls to to_pvr_device().
	 */
	struct pvr_device *pvr_dev;

	/**
	 * @ctx_handles: Array of contexts belonging to this file. Array members
	 * are of type "struct pvr_context *".
	 *
	 * This array is used to allocate handles returned to userspace.
	 */
	struct xarray ctx_handles;

	/**
	 * @free_list_handles: Array of free lists belonging to this file. Array
	 * members are of type "struct pvr_free_list *".
	 *
	 * This array is used to allocate handles returned to userspace.
	 */
	struct xarray free_list_handles;

	/**
	 * @hwrt_handles: Array of HWRT datasets belonging to this file. Array
	 * members are of type "struct pvr_hwrt_dataset *".
	 *
	 * This array is used to allocate handles returned to userspace.
	 */
	struct xarray hwrt_handles;

	/**
	 * @vm_ctx_handles: Array of VM contexts belonging to this file. Array
	 * members are of type "struct pvr_vm_context *".
	 *
	 * This array is used to allocate handles returned to userspace.
	 */
	struct xarray vm_ctx_handles;
};

/**
 * PVR_HAS_FEATURE() - Tests whether a PowerVR device has a given feature
 * @pvr_dev: [IN] Target PowerVR device.
 * @feature: [IN] Hardware feature name.
 *
 * Feature names are derived from those found in &struct pvr_device_features by
 * dropping the 'has_' prefix, which is applied by this macro.
 *
 * Return:
 *  * true if the named feature is present in the hardware
 *  * false if the named feature is not present in the hardware
 */
#define PVR_HAS_FEATURE(pvr_dev, feature) ((pvr_dev)->features.has_##feature)

/**
 * PVR_FEATURE_VALUE() - Gets a PowerVR device feature value
 * @pvr_dev: [IN] Target PowerVR device.
 * @feature: [IN] Feature name.
 * @value_out: [OUT] Feature value.
 *
 * This macro will get a feature value for those features that have values.
 * If the feature is not present, nothing will be stored to @value_out.
 *
 * Feature names are derived from those found in &struct pvr_device_features by
 * dropping the 'has_' prefix.
 *
 * Return:
 *  * 0 on success, or
 *  * -%EINVAL if the named feature is not present in the hardware
 */
#define PVR_FEATURE_VALUE(pvr_dev, feature, value_out)             \
	({                                                         \
		struct pvr_device *_pvr_dev = pvr_dev;             \
		int _ret = -EINVAL;                                \
		if (_pvr_dev->features.has_##feature) {            \
			*(value_out) = _pvr_dev->features.feature; \
			_ret = 0;                                  \
		}                                                  \
		_ret;                                              \
	})

/**
 * PVR_HAS_QUIRK() - Tests whether a physical device has a given quirk
 * @pvr_dev: [IN] Target PowerVR device.
 * @quirk: [IN] Hardware quirk name.
 *
 * Quirk numbers are derived from those found in #pvr_device_quirks by
 * dropping the 'has_brn' prefix, which is applied by this macro.
 *
 * Returns
 *  * true if the quirk is present in the hardware, or
 *  * false if the quirk is not present in the hardware.
 */
#define PVR_HAS_QUIRK(pvr_dev, quirk) ((pvr_dev)->quirks.has_brn##quirk)

/**
 * PVR_HAS_ENHANCEMENT() - Tests whether a physical device has a given
 *                         enhancement
 * @pvr_dev: [IN] Target PowerVR device.
 * @enhancement: [IN] Hardware enhancement name.
 *
 * Enhancement numbers are derived from those found in #pvr_device_enhancements
 * by dropping the 'has_ern' prefix, which is applied by this macro.
 *
 * Returns
 *  * true if the enhancement is present in the hardware, or
 *  * false if the enhancement is not present in the hardware.
 */
#define PVR_HAS_ENHANCEMENT(pvr_dev, enhancement) ((pvr_dev)->enhancements.has_ern##enhancement)

#define from_pvr_device(pvr_dev) (&(pvr_dev)->base)

#define to_pvr_device(drm_dev) container_of_const(drm_dev, struct pvr_device, base)

#define from_pvr_file(pvr_file) ((pvr_file)->file)

#define to_pvr_file(file) ((file)->driver_priv)

/**
 * PVR_PACKED_BVNC() - Packs B, V, N and C values into a 64-bit unsigned integer
 * @b: Branch ID.
 * @v: Version ID.
 * @n: Number of scalable units.
 * @c: Config ID.
 *
 * The packed layout is as follows:
 *
 *    +--------+--------+--------+-------+
 *    | 63..48 | 47..32 | 31..16 | 15..0 |
 *    +========+========+========+=======+
 *    | B      | V      | N      | C     |
 *    +--------+--------+--------+-------+
 *
 * pvr_gpu_id_to_packed_bvnc() should be used instead of this macro when a
 * &struct pvr_gpu_id is available in order to ensure proper type checking.
 *
 * Return: Packed BVNC.
 */
/* clang-format off */
#define PVR_PACKED_BVNC(b, v, n, c) \
	((((u64)(b) & GENMASK_ULL(15, 0)) << 48) | \
	 (((u64)(v) & GENMASK_ULL(15, 0)) << 32) | \
	 (((u64)(n) & GENMASK_ULL(15, 0)) << 16) | \
	 (((u64)(c) & GENMASK_ULL(15, 0)) <<  0))
/* clang-format on */

/**
 * pvr_gpu_id_to_packed_bvnc() - Packs B, V, N and C values into a 64-bit
 * unsigned integer
 * @gpu_id: GPU ID.
 *
 * The packed layout is as follows:
 *
 *    +--------+--------+--------+-------+
 *    | 63..48 | 47..32 | 31..16 | 15..0 |
 *    +========+========+========+=======+
 *    | B      | V      | N      | C     |
 *    +--------+--------+--------+-------+
 *
 * This should be used in preference to PVR_PACKED_BVNC() when a &struct
 * pvr_gpu_id is available in order to ensure proper type checking.
 *
 * Return: Packed BVNC.
 */
static __always_inline u64
pvr_gpu_id_to_packed_bvnc(struct pvr_gpu_id *gpu_id)
{
	return PVR_PACKED_BVNC(gpu_id->b, gpu_id->v, gpu_id->n, gpu_id->c);
}

static __always_inline void
packed_bvnc_to_pvr_gpu_id(u64 bvnc, struct pvr_gpu_id *gpu_id)
{
	gpu_id->b = (bvnc & GENMASK_ULL(63, 48)) >> 48;
	gpu_id->v = (bvnc & GENMASK_ULL(47, 32)) >> 32;
	gpu_id->n = (bvnc & GENMASK_ULL(31, 16)) >> 16;
	gpu_id->c = bvnc & GENMASK_ULL(15, 0);
}

int pvr_device_init(struct pvr_device *pvr_dev);
void pvr_device_fini(struct pvr_device *