1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright (c) 2023 Imagination Technologies Ltd. */
4 #include "pvr_device.h"
5 #include "pvr_device_info.h"
8 #include "pvr_params.h"
10 #include "pvr_queue.h"
11 #include "pvr_rogue_cr_defs.h"
12 #include "pvr_stream.h"
15 #include <drm/drm_print.h>
17 #include <linux/bitfield.h>
18 #include <linux/clk.h>
19 #include <linux/compiler_attributes.h>
20 #include <linux/compiler_types.h>
21 #include <linux/dma-mapping.h>
22 #include <linux/err.h>
23 #include <linux/firmware.h>
24 #include <linux/gfp.h>
25 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
27 #include <linux/pm_runtime.h>
28 #include <linux/slab.h>
29 #include <linux/stddef.h>
30 #include <linux/types.h>
31 #include <linux/workqueue.h>
33 /* Major number for the supported version of the firmware. */
34 #define PVR_FW_VERSION_MAJOR 1
37 * pvr_device_reg_init() - Initialize kernel access to a PowerVR device's
39 * @pvr_dev: Target PowerVR device.
41 * Sets struct pvr_device->regs.
43 * This method of mapping the device control registers into memory ensures that
44 * they are unmapped when the driver is detached (i.e. no explicit cleanup is
49 * * Any error returned by devm_platform_ioremap_resource().
52 pvr_device_reg_init(struct pvr_device *pvr_dev)
54 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
55 struct platform_device *plat_dev = to_platform_device(drm_dev->dev);
56 struct resource *regs_resource;
59 pvr_dev->regs_resource = NULL;
62 regs = devm_platform_get_and_ioremap_resource(plat_dev, 0, ®s_resource);
64 return dev_err_probe(drm_dev->dev, PTR_ERR(regs),
65 "failed to ioremap gpu registers\n");
68 pvr_dev->regs_resource = regs_resource;
74 * pvr_device_clk_init() - Initialize clocks required by a PowerVR device
75 * @pvr_dev: Target PowerVR device.
77 * Sets struct pvr_device->core_clk, struct pvr_device->sys_clk and
78 * struct pvr_device->mem_clk.
80 * Three clocks are required by the PowerVR device: core, sys and mem. On
81 * return, this function guarantees that the clocks are in one of the following
84 * * All successfully initialized,
85 * * Core errored, sys and mem uninitialized,
86 * * Core deinitialized, sys errored, mem uninitialized, or
87 * * Core and sys deinitialized, mem errored.
91 * * Any error returned by devm_clk_get(), or
92 * * Any error returned by devm_clk_get_optional().
94 static int pvr_device_clk_init(struct pvr_device *pvr_dev)
96 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
101 core_clk = devm_clk_get(drm_dev->dev, "core");
102 if (IS_ERR(core_clk))
103 return dev_err_probe(drm_dev->dev, PTR_ERR(core_clk),
104 "failed to get core clock\n");
106 sys_clk = devm_clk_get_optional(drm_dev->dev, "sys");
108 return dev_err_probe(drm_dev->dev, PTR_ERR(sys_clk),
109 "failed to get sys clock\n");
111 mem_clk = devm_clk_get_optional(drm_dev->dev, "mem");
113 return dev_err_probe(drm_dev->dev, PTR_ERR(mem_clk),
114 "failed to get mem clock\n");
116 pvr_dev->core_clk = core_clk;
117 pvr_dev->sys_clk = sys_clk;
118 pvr_dev->mem_clk = mem_clk;
124 * pvr_device_process_active_queues() - Process all queue related events.
125 * @pvr_dev: PowerVR device to check
127 * This is called any time we receive a FW event. It iterates over all
128 * active queues and calls pvr_queue_process() on them.
130 static void pvr_device_process_active_queues(struct pvr_device *pvr_dev)
132 struct pvr_queue *queue, *tmp_queue;
133 LIST_HEAD(active_queues);
135 mutex_lock(&pvr_dev->queues.lock);
137 /* Move all active queues to a temporary list. Queues that remain
138 * active after we're done processing them are re-inserted to
139 * the queues.active list by pvr_queue_process().
141 list_splice_init(&pvr_dev->queues.active, &active_queues);
143 list_for_each_entry_safe(queue, tmp_queue, &active_queues, node)
144 pvr_queue_process(queue);
146 mutex_unlock(&pvr_dev->queues.lock);
149 static irqreturn_t pvr_device_irq_thread_handler(int irq, void *data)
151 struct pvr_device *pvr_dev = data;
152 irqreturn_t ret = IRQ_NONE;
154 /* We are in the threaded handler, we can keep dequeuing events until we
155 * don't see any. This should allow us to reduce the number of interrupts
156 * when the GPU is receiving a massive amount of short jobs.
158 while (pvr_fw_irq_pending(pvr_dev)) {
159 pvr_fw_irq_clear(pvr_dev);
161 if (pvr_dev->fw_dev.booted) {
162 pvr_fwccb_process(pvr_dev);
163 pvr_kccb_wake_up_waiters(pvr_dev);
164 pvr_device_process_active_queues(pvr_dev);
167 pm_runtime_mark_last_busy(from_pvr_device(pvr_dev)->dev);
172 /* Unmask FW irqs before returning, so new interrupts can be received. */
173 pvr_fw_irq_enable(pvr_dev);
177 static irqreturn_t pvr_device_irq_handler(int irq, void *data)
179 struct pvr_device *pvr_dev = data;
181 if (!pvr_fw_irq_pending(pvr_dev))
182 return IRQ_NONE; /* Spurious IRQ - ignore. */
184 /* Mask the FW interrupts before waking up the thread. Will be unmasked
185 * when the thread handler is done processing events.
187 pvr_fw_irq_disable(pvr_dev);
188 return IRQ_WAKE_THREAD;
192 * pvr_device_irq_init() - Initialise IRQ required by a PowerVR device
193 * @pvr_dev: Target PowerVR device.
197 * * Any error returned by platform_get_irq_byname(), or
198 * * Any error returned by request_irq().
201 pvr_device_irq_init(struct pvr_device *pvr_dev)
203 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
204 struct platform_device *plat_dev = to_platform_device(drm_dev->dev);
206 init_waitqueue_head(&pvr_dev->kccb.rtn_q);
208 pvr_dev->irq = platform_get_irq(plat_dev, 0);
209 if (pvr_dev->irq < 0)
212 /* Clear any pending events before requesting the IRQ line. */
213 pvr_fw_irq_clear(pvr_dev);
214 pvr_fw_irq_enable(pvr_dev);
216 return request_threaded_irq(pvr_dev->irq, pvr_device_irq_handler,
217 pvr_device_irq_thread_handler,
218 IRQF_SHARED, "gpu", pvr_dev);
222 * pvr_device_irq_fini() - Deinitialise IRQ required by a PowerVR device
223 * @pvr_dev: Target PowerVR device.
226 pvr_device_irq_fini(struct pvr_device *pvr_dev)
228 free_irq(pvr_dev->irq, pvr_dev);
232 * pvr_build_firmware_filename() - Construct a PowerVR firmware filename
233 * @pvr_dev: Target PowerVR device.
234 * @base: First part of the filename.
235 * @major: Major version number.
237 * A PowerVR firmware filename consists of three parts separated by underscores
238 * (``'_'``) along with a '.fw' file suffix. The first part is the exact value
239 * of @base, the second part is the hardware version string derived from @pvr_fw
240 * and the final part is the firmware version number constructed from @major with
241 * a 'v' prefix, e.g. powervr/rogue_4.40.2.51_v1.fw.
243 * The returned string will have been slab allocated and must be freed with
247 * * The constructed filename on success, or
248 * * Any error returned by kasprintf().
251 pvr_build_firmware_filename(struct pvr_device *pvr_dev, const char *base,
254 struct pvr_gpu_id *gpu_id = &pvr_dev->gpu_id;
256 return kasprintf(GFP_KERNEL, "%s_%d.%d.%d.%d_v%d.fw", base, gpu_id->b,
257 gpu_id->v, gpu_id->n, gpu_id->c, major);
261 pvr_release_firmware(void *data)
263 struct pvr_device *pvr_dev = data;
265 release_firmware(pvr_dev->fw_dev.firmware);
269 * pvr_request_firmware() - Load firmware for a PowerVR device
270 * @pvr_dev: Target PowerVR device.
272 * See pvr_build_firmware_filename() for details on firmware file naming.
276 * * Any error returned by pvr_build_firmware_filename(), or
277 * * Any error returned by request_firmware().
280 pvr_request_firmware(struct pvr_device *pvr_dev)
282 struct drm_device *drm_dev = &pvr_dev->base;
284 const struct firmware *fw;
287 filename = pvr_build_firmware_filename(pvr_dev, "powervr/rogue",
288 PVR_FW_VERSION_MAJOR);
293 * This function takes a copy of &filename, meaning we can free our
294 * instance before returning.
296 err = request_firmware(&fw, filename, pvr_dev->base.dev);
298 drm_err(drm_dev, "failed to load firmware %s (err=%d)\n",
300 goto err_free_filename;
303 drm_info(drm_dev, "loaded firmware %s\n", filename);
306 pvr_dev->fw_dev.firmware = fw;
308 return devm_add_action_or_reset(drm_dev->dev, pvr_release_firmware, pvr_dev);
317 * pvr_load_gpu_id() - Load a PowerVR device's GPU ID (BVNC) from control registers.
319 * Sets struct pvr_dev.gpu_id.
321 * @pvr_dev: Target PowerVR device.
324 pvr_load_gpu_id(struct pvr_device *pvr_dev)
326 struct pvr_gpu_id *gpu_id = &pvr_dev->gpu_id;
330 * Try reading the BVNC using the newer (cleaner) method first. If the
331 * B value is zero, fall back to the older method.
333 bvnc = pvr_cr_read64(pvr_dev, ROGUE_CR_CORE_ID__PBVNC);
335 gpu_id->b = PVR_CR_FIELD_GET(bvnc, CORE_ID__PBVNC__BRANCH_ID);
336 if (gpu_id->b != 0) {
337 gpu_id->v = PVR_CR_FIELD_GET(bvnc, CORE_ID__PBVNC__VERSION_ID);
338 gpu_id->n = PVR_CR_FIELD_GET(bvnc, CORE_ID__PBVNC__NUMBER_OF_SCALABLE_UNITS);
339 gpu_id->c = PVR_CR_FIELD_GET(bvnc, CORE_ID__PBVNC__CONFIG_ID);
341 u32 core_rev = pvr_cr_read32(pvr_dev, ROGUE_CR_CORE_REVISION);
342 u32 core_id = pvr_cr_read32(pvr_dev, ROGUE_CR_CORE_ID);
343 u16 core_id_config = PVR_CR_FIELD_GET(core_id, CORE_ID_CONFIG);
345 gpu_id->b = PVR_CR_FIELD_GET(core_rev, CORE_REVISION_MAJOR);
346 gpu_id->v = PVR_CR_FIELD_GET(core_rev, CORE_REVISION_MINOR);
347 gpu_id->n = FIELD_GET(0xFF00, core_id_config);
348 gpu_id->c = FIELD_GET(0x00FF, core_id_config);
353 * pvr_set_dma_info() - Set PowerVR device DMA information
354 * @pvr_dev: Target PowerVR device.
356 * Sets the DMA mask and max segment size for the PowerVR device.
360 * * Any error returned by PVR_FEATURE_VALUE(), or
361 * * Any error returned by dma_set_mask().
365 pvr_set_dma_info(struct pvr_device *pvr_dev)
367 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
371 err = PVR_FEATURE_VALUE(pvr_dev, phys_bus_width, &phys_bus_width);
373 drm_err(drm_dev, "Failed to get device physical bus width\n");
377 err = dma_set_mask(drm_dev->dev, DMA_BIT_MASK(phys_bus_width));
379 drm_err(drm_dev, "Failed to set DMA mask (err=%d)\n", err);
383 dma_set_max_seg_size(drm_dev->dev, UINT_MAX);
389 * pvr_device_gpu_init() - GPU-specific initialization for a PowerVR device
390 * @pvr_dev: Target PowerVR device.
392 * The following steps are taken to ensure the device is ready:
394 * 1. Read the hardware version information from control registers,
395 * 2. Initialise the hardware feature information,
396 * 3. Setup the device DMA information,
397 * 4. Setup the device-scoped memory context, and
398 * 5. Load firmware into the device.
402 * * -%ENODEV if the GPU is not supported,
403 * * Any error returned by pvr_set_dma_info(),
404 * * Any error returned by pvr_memory_context_init(), or
405 * * Any error returned by pvr_request_firmware().
408 pvr_device_gpu_init(struct pvr_device *pvr_dev)
412 pvr_load_gpu_id(pvr_dev);
414 err = pvr_request_firmware(pvr_dev);
418 err = pvr_fw_validate_init_device_info(pvr_dev);
422 if (PVR_HAS_FEATURE(pvr_dev, meta))
423 pvr_dev->fw_dev.processor_type = PVR_FW_PROCESSOR_TYPE_META;
424 else if (PVR_HAS_FEATURE(pvr_dev, mips))
425 pvr_dev->fw_dev.processor_type = PVR_FW_PROCESSOR_TYPE_MIPS;
426 else if (PVR_HAS_FEATURE(pvr_dev, riscv_fw_processor))
427 pvr_dev->fw_dev.processor_type = PVR_FW_PROCESSOR_TYPE_RISCV;
431 pvr_stream_create_musthave_masks(pvr_dev);
433 err = pvr_set_dma_info(pvr_dev);
437 if (pvr_dev->fw_dev.processor_type != PVR_FW_PROCESSOR_TYPE_MIPS) {
438 pvr_dev->kernel_vm_ctx = pvr_vm_create_context(pvr_dev, false);
439 if (IS_ERR(pvr_dev->kernel_vm_ctx))
440 return PTR_ERR(pvr_dev->kernel_vm_ctx);
443 err = pvr_fw_init(pvr_dev);
450 if (pvr_dev->fw_dev.processor_type != PVR_FW_PROCESSOR_TYPE_MIPS) {
451 pvr_vm_context_put(pvr_dev->kernel_vm_ctx);
452 pvr_dev->kernel_vm_ctx = NULL;
459 * pvr_device_gpu_fini() - GPU-specific deinitialization for a PowerVR device
460 * @pvr_dev: Target PowerVR device.
463 pvr_device_gpu_fini(struct pvr_device *pvr_dev)
465 pvr_fw_fini(pvr_dev);
467 if (pvr_dev->fw_dev.processor_type != PVR_FW_PROCESSOR_TYPE_MIPS) {
468 WARN_ON(!pvr_vm_context_put(pvr_dev->kernel_vm_ctx));
469 pvr_dev->kernel_vm_ctx = NULL;
474 * pvr_device_init() - Initialize a PowerVR device
475 * @pvr_dev: Target PowerVR device.
477 * If this function returns successfully, the device will have been fully
478 * initialized. Otherwise, any parts of the device initialized before an error
479 * occurs will be de-initialized before returning.
481 * NOTE: The initialization steps currently taken are the bare minimum required
482 * to read from the control registers. The device is unlikely to function
483 * until further initialization steps are added. [This note should be
484 * removed when that happens.]
488 * * Any error returned by pvr_device_reg_init(),
489 * * Any error returned by pvr_device_clk_init(), or
490 * * Any error returned by pvr_device_gpu_init().
493 pvr_device_init(struct pvr_device *pvr_dev)
495 struct drm_device *drm_dev = from_pvr_device(pvr_dev);
496 struct device *dev = drm_dev->dev;
500 * Setup device parameters. We do this first in case other steps
503 err = pvr_device_params_init(&pvr_dev->params);
507 /* Enable and initialize clocks required for the device to operate. */
508 err = pvr_device_clk_init(pvr_dev);
512 /* Explicitly power the GPU so we can access control registers before the FW is booted. */
513 err = pm_runtime_resume_and_get(dev);
517 /* Map the control registers into memory. */
518 err = pvr_device_reg_init(pvr_dev);
520 goto err_pm_runtime_put;
522 /* Perform GPU-specific initialization steps. */
523 err = pvr_device_gpu_init(pvr_dev);
525 goto err_pm_runtime_put;
527 err = pvr_device_irq_init(pvr_dev);
529 goto err_device_gpu_fini;
536 pvr_device_gpu_fini(pvr_dev);
539 pm_runtime_put_sync_suspend(dev);
545 * pvr_device_fini() - Deinitialize a PowerVR device
546 * @pvr_dev: Target PowerVR device.
549 pvr_device_fini(struct pvr_device *pvr_dev)
552 * Deinitialization stages are performed in reverse order compared to
553 * the initialization stages in pvr_device_init().
555 pvr_device_irq_fini(pvr_dev);
556 pvr_device_gpu_fini(pvr_dev);
560 pvr_device_has_uapi_quirk(struct pvr_device *pvr_dev, u32 quirk)
564 return PVR_HAS_QUIRK(pvr_dev, 47217);
566 return PVR_HAS_QUIRK(pvr_dev, 48545);
568 return PVR_HAS_QUIRK(pvr_dev, 49927);
570 return PVR_HAS_QUIRK(pvr_dev, 51764);
572 return PVR_HAS_QUIRK(pvr_dev, 62269);
579 pvr_device_has_uapi_enhancement(struct pvr_device *pvr_dev, u32 enhancement)
581 switch (enhancement) {
583 return PVR_HAS_ENHANCEMENT(pvr_dev, 35421);
585 return PVR_HAS_ENHANCEMENT(pvr_dev, 42064);
592 * pvr_device_has_feature() - Look up device feature based on feature definition
593 * @pvr_dev: Device pointer.
594 * @feature: Feature to look up. Should be one of %PVR_FEATURE_*.
597 * * %true if feature is present on device, or
598 * * %false if feature is not present on device.
601 pvr_device_has_feature(struct pvr_device *pvr_dev, u32 feature)
604 case PVR_FEATURE_CLUSTER_GROUPING:
605 return PVR_HAS_FEATURE(pvr_dev, cluster_grouping);
607 case PVR_FEATURE_COMPUTE_MORTON_CAPABLE:
608 return PVR_HAS_FEATURE(pvr_dev, compute_morton_capable);
610 case PVR_FEATURE_FB_CDC_V4:
611 return PVR_HAS_FEATURE(pvr_dev, fb_cdc_v4);
613 case PVR_FEATURE_GPU_MULTICORE_SUPPORT:
614 return PVR_HAS_FEATURE(pvr_dev, gpu_multicore_support);
616 case PVR_FEATURE_ISP_ZLS_D24_S8_PACKING_OGL_MODE:
617 return PVR_HAS_FEATURE(pvr_dev, isp_zls_d24_s8_packing_ogl_mode);
619 case PVR_FEATURE_S7_TOP_INFRASTRUCTURE:
620 return PVR_HAS_FEATURE(pvr_dev, s7_top_infrastructure);
622 case PVR_FEATURE_TESSELLATION:
623 return PVR_HAS_FEATURE(pvr_dev, tessellation);
625 case PVR_FEATURE_TPU_DM_GLOBAL_REGISTERS:
626 return PVR_HAS_FEATURE(pvr_dev, tpu_dm_global_registers);
628 case PVR_FEATURE_VDM_DRAWINDIRECT:
629 return PVR_HAS_FEATURE(pvr_dev, vdm_drawindirect);
631 case PVR_FEATURE_VDM_OBJECT_LEVEL_LLS:
632 return PVR_HAS_FEATURE(pvr_dev, vdm_object_level_lls);
634 case PVR_FEATURE_ZLS_SUBTILE:
635 return PVR_HAS_FEATURE(pvr_dev, zls_subtile);
637 /* Derived features. */
638 case PVR_FEATURE_CDM_USER_MODE_QUEUE: {
639 u8 cdm_control_stream_format = 0;
641 PVR_FEATURE_VALUE(pvr_dev, cdm_control_stream_format, &cdm_control_stream_format);
642 return (cdm_control_stream_format >= 2 && cdm_control_stream_format <= 4);
645 case PVR_FEATURE_REQUIRES_FB_CDC_ZLS_SETUP:
646 if (PVR_HAS_FEATURE(pvr_dev, fbcdc_algorithm)) {
647 u8 fbcdc_algorithm = 0;
649 PVR_FEATURE_VALUE(pvr_dev, fbcdc_algorithm, &fbcdc_algorithm);
650 return (fbcdc_algorithm < 3 || PVR_HAS_FEATURE(pvr_dev, fb_cdc_v4));
655 WARN(true, "Looking up undefined feature %u\n", feature);