xref: /linux/drivers/gpu/drm/msm/adreno/adreno_gpu.c (revision 02195633635c42db9ca29f3c9fa3a758bc1a2cee)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2013 Red Hat
4  * Author: Rob Clark <robdclark@gmail.com>
5  *
6  * Copyright (c) 2014 The Linux Foundation. All rights reserved.
7  */
8 
9 #include <linux/ascii85.h>
10 #include <linux/interconnect.h>
11 #include <linux/firmware/qcom/qcom_scm.h>
12 #include <linux/kernel.h>
13 #include <linux/of_reserved_mem.h>
14 #include <linux/pm_opp.h>
15 #include <linux/slab.h>
16 #include <linux/soc/qcom/mdt_loader.h>
17 #include <linux/nvmem-consumer.h>
18 #include <soc/qcom/ocmem.h>
19 #include "adreno_gpu.h"
20 #include "a6xx_gpu.h"
21 #include "msm_gem.h"
22 #include "msm_mmu.h"
23 
24 static u64 address_space_size = 0;
25 MODULE_PARM_DESC(address_space_size, "Override for size of processes private GPU address space");
26 module_param(address_space_size, ullong, 0600);
27 
28 static bool zap_available = true;
29 
30 static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
31 		u32 pasid)
32 {
33 	struct device *dev = &gpu->pdev->dev;
34 	const struct firmware *fw;
35 	const char *signed_fwname = NULL;
36 	struct device_node *np;
37 	struct resource r;
38 	phys_addr_t mem_phys;
39 	ssize_t mem_size;
40 	void *mem_region = NULL;
41 	int ret;
42 
43 	if (!IS_ENABLED(CONFIG_ARCH_QCOM)) {
44 		zap_available = false;
45 		return -EINVAL;
46 	}
47 
48 	np = of_get_available_child_by_name(dev->of_node, "zap-shader");
49 	if (!np) {
50 		zap_available = false;
51 		return -ENODEV;
52 	}
53 
54 	ret = of_reserved_mem_region_to_resource(np, 0, &r);
55 	if (ret) {
56 		zap_available = false;
57 		return ret;
58 	}
59 	mem_phys = r.start;
60 
61 	/*
62 	 * Check for a firmware-name property.  This is the new scheme
63 	 * to handle firmware that may be signed with device specific
64 	 * keys, allowing us to have a different zap fw path for different
65 	 * devices.
66 	 *
67 	 * If the firmware-name property is found, we bypass the
68 	 * adreno_request_fw() mechanism, because we don't need to handle
69 	 * the /lib/firmware/qcom/... vs /lib/firmware/... case.
70 	 *
71 	 * If the firmware-name property is not found, for backwards
72 	 * compatibility we fall back to the fwname from the gpulist
73 	 * table.
74 	 */
75 	of_property_read_string_index(np, "firmware-name", 0, &signed_fwname);
76 	if (signed_fwname) {
77 		fwname = signed_fwname;
78 		ret = request_firmware_direct(&fw, fwname, gpu->dev->dev);
79 		if (ret)
80 			fw = ERR_PTR(ret);
81 	} else if (fwname) {
82 		/* Request the MDT file from the default location: */
83 		fw = adreno_request_fw(to_adreno_gpu(gpu), fwname);
84 	} else {
85 		/*
86 		 * For new targets, we require the firmware-name property,
87 		 * if a zap-shader is required, rather than falling back
88 		 * to a firmware name specified in gpulist.
89 		 *
90 		 * Because the firmware is signed with a (potentially)
91 		 * device specific key, having the name come from gpulist
92 		 * was a bad idea, and is only provided for backwards
93 		 * compatibility for older targets.
94 		 */
95 		return -ENOENT;
96 	}
97 
98 	if (IS_ERR(fw)) {
99 		DRM_DEV_ERROR(dev, "Unable to load %s\n", fwname);
100 		return PTR_ERR(fw);
101 	}
102 
103 	/* Figure out how much memory we need */
104 	mem_size = qcom_mdt_get_size(fw);
105 	if (mem_size < 0) {
106 		ret = mem_size;
107 		goto out;
108 	}
109 
110 	if (mem_size > resource_size(&r)) {
111 		DRM_DEV_ERROR(dev,
112 			"memory region is too small to load the MDT\n");
113 		ret = -E2BIG;
114 		goto out;
115 	}
116 
117 	/* Allocate memory for the firmware image */
118 	mem_region = memremap(mem_phys, mem_size,  MEMREMAP_WC);
119 	if (!mem_region) {
120 		ret = -ENOMEM;
121 		goto out;
122 	}
123 
124 	/*
125 	 * Load the rest of the MDT
126 	 *
127 	 * Note that we could be dealing with two different paths, since
128 	 * with upstream linux-firmware it would be in a qcom/ subdir..
129 	 * adreno_request_fw() handles this, but qcom_mdt_load() does
130 	 * not.  But since we've already gotten through adreno_request_fw()
131 	 * we know which of the two cases it is:
132 	 */
133 	if (signed_fwname || (to_adreno_gpu(gpu)->fwloc == FW_LOCATION_LEGACY)) {
134 		ret = qcom_mdt_load(dev, fw, fwname, pasid,
135 				mem_region, mem_phys, mem_size, NULL);
136 	} else {
137 		char *newname;
138 
139 		newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname);
140 
141 		ret = qcom_mdt_load(dev, fw, newname, pasid,
142 				mem_region, mem_phys, mem_size, NULL);
143 		kfree(newname);
144 	}
145 	if (ret)
146 		goto out;
147 
148 	/* Send the image to the secure world */
149 	ret = qcom_scm_pas_auth_and_reset(pasid);
150 
151 	/*
152 	 * If the scm call returns -EOPNOTSUPP we assume that this target
153 	 * doesn't need/support the zap shader so quietly fail
154 	 */
155 	if (ret == -EOPNOTSUPP)
156 		zap_available = false;
157 	else if (ret)
158 		DRM_DEV_ERROR(dev, "Unable to authorize the image\n");
159 
160 out:
161 	if (mem_region)
162 		memunmap(mem_region);
163 
164 	release_firmware(fw);
165 
166 	return ret;
167 }
168 
169 int adreno_zap_shader_load(struct msm_gpu *gpu, u32 pasid)
170 {
171 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
172 	struct platform_device *pdev = gpu->pdev;
173 
174 	/* Short cut if we determine the zap shader isn't available/needed */
175 	if (!zap_available)
176 		return -ENODEV;
177 
178 	/* We need SCM to be able to load the firmware */
179 	if (!qcom_scm_is_available()) {
180 		DRM_DEV_ERROR(&pdev->dev, "SCM is not available\n");
181 		return -EPROBE_DEFER;
182 	}
183 
184 	return zap_shader_load_mdt(gpu, adreno_gpu->info->zapfw, pasid);
185 }
186 
187 struct drm_gpuvm *
188 adreno_create_vm(struct msm_gpu *gpu,
189 		 struct platform_device *pdev)
190 {
191 	return adreno_iommu_create_vm(gpu, pdev, 0);
192 }
193 
194 struct drm_gpuvm *
195 adreno_iommu_create_vm(struct msm_gpu *gpu,
196 		       struct platform_device *pdev,
197 		       unsigned long quirks)
198 {
199 	struct iommu_domain_geometry *geometry;
200 	struct msm_mmu *mmu;
201 	struct drm_gpuvm *vm;
202 	u64 start, size;
203 
204 	mmu = msm_iommu_gpu_new(&pdev->dev, gpu, quirks);
205 	if (IS_ERR(mmu))
206 		return ERR_CAST(mmu);
207 
208 	geometry = msm_iommu_get_geometry(mmu);
209 	if (IS_ERR(geometry))
210 		return ERR_CAST(geometry);
211 
212 	/*
213 	 * Use the aperture start or SZ_16M, whichever is greater. This will
214 	 * ensure that we align with the allocated pagetable range while still
215 	 * allowing room in the lower 32 bits for GMEM and whatnot
216 	 */
217 	start = max_t(u64, SZ_16M, geometry->aperture_start);
218 	size = geometry->aperture_end - start + 1;
219 
220 	vm = msm_gem_vm_create(gpu->dev, mmu, "gpu", start & GENMASK_ULL(48, 0),
221 			       size, true);
222 
223 	if (IS_ERR(vm) && !IS_ERR(mmu))
224 		mmu->funcs->destroy(mmu);
225 
226 	return vm;
227 }
228 
229 u64 adreno_private_vm_size(struct msm_gpu *gpu)
230 {
231 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
232 	struct adreno_smmu_priv *adreno_smmu = dev_get_drvdata(&gpu->pdev->dev);
233 	const struct io_pgtable_cfg *ttbr1_cfg;
234 
235 	if (address_space_size)
236 		return address_space_size;
237 
238 	if (adreno_gpu->info->quirks & ADRENO_QUIRK_4GB_VA)
239 		return SZ_4G;
240 
241 	if (!adreno_smmu || !adreno_smmu->get_ttbr1_cfg)
242 		return SZ_4G;
243 
244 	ttbr1_cfg = adreno_smmu->get_ttbr1_cfg(adreno_smmu->cookie);
245 
246 	/*
247 	 * Userspace VM is actually using TTBR0, but both are the same size,
248 	 * with b48 (sign bit) selecting which TTBRn to use.  So if IAS is
249 	 * 48, the total (kernel+user) address space size is effectively
250 	 * 49 bits.  But what userspace is control of is the lower 48.
251 	 */
252 	return BIT(ttbr1_cfg->ias) - ADRENO_VM_START;
253 }
254 
255 void adreno_check_and_reenable_stall(struct adreno_gpu *adreno_gpu)
256 {
257 	struct msm_gpu *gpu = &adreno_gpu->base;
258 	struct msm_drm_private *priv = gpu->dev->dev_private;
259 	unsigned long flags;
260 
261 	/*
262 	 * Wait until the cooldown period has passed and we would actually
263 	 * collect a crashdump to re-enable stall-on-fault.
264 	 */
265 	spin_lock_irqsave(&priv->fault_stall_lock, flags);
266 	if (!priv->stall_enabled &&
267 			ktime_after(ktime_get(), priv->stall_reenable_time) &&
268 			!READ_ONCE(gpu->crashstate)) {
269 		struct msm_mmu *mmu = to_msm_vm(gpu->vm)->mmu;
270 
271 		priv->stall_enabled = true;
272 
273 		mmu->funcs->set_stall(mmu, true);
274 	}
275 	spin_unlock_irqrestore(&priv->fault_stall_lock, flags);
276 }
277 
278 #define ARM_SMMU_FSR_TF                 BIT(1)
279 #define ARM_SMMU_FSR_PF			BIT(3)
280 #define ARM_SMMU_FSR_EF			BIT(4)
281 #define ARM_SMMU_FSR_SS			BIT(30)
282 
283 int adreno_fault_handler(struct msm_gpu *gpu, unsigned long iova, int flags,
284 			 struct adreno_smmu_fault_info *info, const char *block,
285 			 u32 scratch[4])
286 {
287 	struct adreno_gpu *adreno_gpu = container_of(gpu, struct adreno_gpu, base);
288 	struct msm_drm_private *priv = gpu->dev->dev_private;
289 	struct msm_mmu *mmu = to_msm_vm(gpu->vm)->mmu;
290 	const char *type = "UNKNOWN";
291 	bool do_devcoredump = info && (info->fsr & ARM_SMMU_FSR_SS) &&
292 		!READ_ONCE(gpu->crashstate);
293 	unsigned long irq_flags;
294 
295 	/*
296 	 * In case there is a subsequent storm of pagefaults, disable
297 	 * stall-on-fault for at least half a second.
298 	 */
299 	spin_lock_irqsave(&priv->fault_stall_lock, irq_flags);
300 	if (priv->stall_enabled) {
301 		priv->stall_enabled = false;
302 
303 		mmu->funcs->set_stall(mmu, false);
304 	}
305 
306 	priv->stall_reenable_time = ktime_add_ms(ktime_get(), 500);
307 	spin_unlock_irqrestore(&priv->fault_stall_lock, irq_flags);
308 
309 	/*
310 	 * Print a default message if we couldn't get the data from the
311 	 * adreno-smmu-priv
312 	 */
313 	if (!info) {
314 		pr_warn_ratelimited("*** gpu fault: iova=%.16lx flags=%d (%u,%u,%u,%u)\n",
315 			iova, flags,
316 			scratch[0], scratch[1], scratch[2], scratch[3]);
317 
318 		return 0;
319 	}
320 
321 	if (info->fsr & ARM_SMMU_FSR_TF)
322 		type = "TRANSLATION";
323 	else if (info->fsr & ARM_SMMU_FSR_PF)
324 		type = "PERMISSION";
325 	else if (info->fsr & ARM_SMMU_FSR_EF)
326 		type = "EXTERNAL";
327 
328 	pr_warn_ratelimited("*** gpu fault: ttbr0=%.16llx iova=%.16lx dir=%s type=%s source=%s (%u,%u,%u,%u)\n",
329 			info->ttbr0, iova,
330 			flags & IOMMU_FAULT_WRITE ? "WRITE" : "READ",
331 			type, block,
332 			scratch[0], scratch[1], scratch[2], scratch[3]);
333 
334 	if (do_devcoredump) {
335 		struct msm_gpu_fault_info fault_info = {};
336 
337 		/* Turn off the hangcheck timer to keep it from bothering us */
338 		timer_delete(&gpu->hangcheck_timer);
339 
340 		/* Let any concurrent GMU transactions know that the MMU may be
341 		 * blocked for a while and they should wait on us.
342 		 */
343 		reinit_completion(&adreno_gpu->fault_coredump_done);
344 
345 		fault_info.ttbr0 = info->ttbr0;
346 		fault_info.iova  = iova;
347 		fault_info.flags = flags;
348 		fault_info.type  = type;
349 		fault_info.block = block;
350 
351 		msm_gpu_fault_crashstate_capture(gpu, &fault_info);
352 
353 		complete_all(&adreno_gpu->fault_coredump_done);
354 	}
355 
356 	return 0;
357 }
358 
359 int adreno_get_param(struct msm_gpu *gpu, struct msm_context *ctx,
360 		     uint32_t param, uint64_t *value, uint32_t *len)
361 {
362 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
363 	struct drm_device *drm = gpu->dev;
364 	/* Note ctx can be NULL when called from rd_open(): */
365 	struct drm_gpuvm *vm = ctx ? msm_context_vm(drm, ctx) : NULL;
366 
367 	/* No pointer params yet */
368 	if (*len != 0)
369 		return UERR(EINVAL, drm, "invalid len");
370 
371 	switch (param) {
372 	case MSM_PARAM_GPU_ID:
373 		*value = adreno_gpu->info->revn;
374 		return 0;
375 	case MSM_PARAM_GMEM_SIZE:
376 		*value = adreno_gpu->info->gmem;
377 		return 0;
378 	case MSM_PARAM_GMEM_BASE:
379 		if (adreno_gpu->info->family >= ADRENO_6XX_GEN3)
380 			*value = 0;
381 		else
382 			*value = 0x100000;
383 		return 0;
384 	case MSM_PARAM_CHIP_ID:
385 		*value = adreno_gpu->chip_id;
386 		if (!adreno_gpu->info->revn)
387 			*value |= ((uint64_t) adreno_gpu->speedbin) << 32;
388 		return 0;
389 	case MSM_PARAM_MAX_FREQ:
390 		*value = adreno_gpu->base.fast_rate;
391 		return 0;
392 	case MSM_PARAM_TIMESTAMP:
393 		if (adreno_gpu->funcs->get_timestamp) {
394 			pm_runtime_get_sync(&gpu->pdev->dev);
395 			*value = adreno_gpu->funcs->get_timestamp(gpu);
396 			pm_runtime_put_autosuspend(&gpu->pdev->dev);
397 
398 			return 0;
399 		}
400 		return -EINVAL;
401 	case MSM_PARAM_PRIORITIES:
402 		*value = gpu->nr_rings * NR_SCHED_PRIORITIES;
403 		return 0;
404 	case MSM_PARAM_PP_PGTABLE:
405 		*value = 0;
406 		return 0;
407 	case MSM_PARAM_FAULTS:
408 		if (vm)
409 			*value = gpu->global_faults + to_msm_vm(vm)->faults;
410 		else
411 			*value = gpu->global_faults;
412 		return 0;
413 	case MSM_PARAM_SUSPENDS:
414 		*value = gpu->suspend_count;
415 		return 0;
416 	case MSM_PARAM_VA_START:
417 		if (vm == gpu->vm)
418 			return UERR(EINVAL, drm, "requires per-process pgtables");
419 		*value = vm->mm_start;
420 		return 0;
421 	case MSM_PARAM_VA_SIZE:
422 		if (vm == gpu->vm)
423 			return UERR(EINVAL, drm, "requires per-process pgtables");
424 		*value = vm->mm_range;
425 		return 0;
426 	case MSM_PARAM_HIGHEST_BANK_BIT:
427 		if (!adreno_gpu->ubwc_config)
428 			return UERR(ENOENT, drm, "no UBWC on this platform");
429 		*value = adreno_gpu->ubwc_config->highest_bank_bit;
430 		return 0;
431 	case MSM_PARAM_RAYTRACING:
432 		*value = adreno_gpu->has_ray_tracing;
433 		return 0;
434 	case MSM_PARAM_UBWC_SWIZZLE:
435 		if (!adreno_gpu->ubwc_config)
436 			return UERR(ENOENT, drm, "no UBWC on this platform");
437 		*value = qcom_ubwc_swizzle(adreno_gpu->ubwc_config);
438 		return 0;
439 	case MSM_PARAM_MACROTILE_MODE:
440 		if (!adreno_gpu->ubwc_config)
441 			return UERR(ENOENT, drm, "no UBWC on this platform");
442 		*value = qcom_ubwc_macrotile_mode(adreno_gpu->ubwc_config);
443 		return 0;
444 	case MSM_PARAM_UCHE_TRAP_BASE:
445 		*value = adreno_gpu->uche_trap_base;
446 		return 0;
447 	case MSM_PARAM_HAS_PRR:
448 		*value = adreno_smmu_has_prr(gpu);
449 		return 0;
450 	case MSM_PARAM_AQE:
451 		*value = !!(adreno_gpu->funcs->aqe_is_enabled &&
452 			    adreno_gpu->funcs->aqe_is_enabled(adreno_gpu));
453 		return 0;
454 	default:
455 		return UERR(EINVAL, drm, "%s: invalid param: %u", gpu->name, param);
456 	}
457 }
458 
459 int adreno_set_param(struct msm_gpu *gpu, struct msm_context *ctx,
460 		     uint32_t param, uint64_t value, uint32_t len)
461 {
462 	struct drm_device *drm = gpu->dev;
463 
464 	switch (param) {
465 	case MSM_PARAM_COMM:
466 	case MSM_PARAM_CMDLINE:
467 		/* kstrdup_quotable_cmdline() limits to PAGE_SIZE, so
468 		 * that should be a reasonable upper bound
469 		 */
470 		if (len > PAGE_SIZE)
471 			return UERR(EINVAL, drm, "invalid len");
472 		break;
473 	default:
474 		if (len != 0)
475 			return UERR(EINVAL, drm, "invalid len");
476 	}
477 
478 	switch (param) {
479 	case MSM_PARAM_COMM:
480 	case MSM_PARAM_CMDLINE: {
481 		char *str, **paramp;
482 
483 		str = memdup_user_nul(u64_to_user_ptr(value), len);
484 		if (IS_ERR(str))
485 			return PTR_ERR(str);
486 
487 		mutex_lock(&gpu->lock);
488 
489 		if (param == MSM_PARAM_COMM) {
490 			paramp = &ctx->comm;
491 		} else {
492 			paramp = &ctx->cmdline;
493 		}
494 
495 		kfree(*paramp);
496 		*paramp = str;
497 
498 		mutex_unlock(&gpu->lock);
499 
500 		return 0;
501 	}
502 	case MSM_PARAM_SYSPROF:
503 		if (!perfmon_capable())
504 			return UERR(EPERM, drm, "invalid permissions");
505 		return msm_context_set_sysprof(ctx, gpu, value);
506 	case MSM_PARAM_EN_VM_BIND:
507 		/* We can only support VM_BIND with per-process pgtables: */
508 		if (ctx->vm == gpu->vm)
509 			return UERR(EINVAL, drm, "requires per-process pgtables");
510 
511 		/*
512 		 * We can only swtich to VM_BIND mode if the VM has not yet
513 		 * been created:
514 		 */
515 		if (ctx->vm)
516 			return UERR(EBUSY, drm, "VM already created");
517 
518 		ctx->userspace_managed_vm = value;
519 
520 		return 0;
521 	default:
522 		return UERR(EINVAL, drm, "%s: invalid param: %u", gpu->name, param);
523 	}
524 }
525 
526 const struct firmware *
527 adreno_request_fw(struct adreno_gpu *adreno_gpu, const char *fwname)
528 {
529 	struct drm_device *drm = adreno_gpu->base.dev;
530 	const struct firmware *fw = NULL;
531 	char *newname;
532 	int ret;
533 
534 	newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname);
535 	if (!newname)
536 		return ERR_PTR(-ENOMEM);
537 
538 	/*
539 	 * Try first to load from qcom/$fwfile using a direct load (to avoid
540 	 * a potential timeout waiting for usermode helper)
541 	 */
542 	if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) ||
543 	    (adreno_gpu->fwloc == FW_LOCATION_NEW)) {
544 
545 		ret = request_firmware_direct(&fw, newname, drm->dev);
546 		if (!ret) {
547 			DRM_DEV_INFO(drm->dev, "loaded %s from new location\n",
548 				newname);
549 			adreno_gpu->fwloc = FW_LOCATION_NEW;
550 			goto out;
551 		} else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) {
552 			DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n",
553 				newname, ret);
554 			fw = ERR_PTR(ret);
555 			goto out;
556 		}
557 	}
558 
559 	/*
560 	 * Then try the legacy location without qcom/ prefix
561 	 */
562 	if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) ||
563 	    (adreno_gpu->fwloc == FW_LOCATION_LEGACY)) {
564 
565 		ret = request_firmware_direct(&fw, fwname, drm->dev);
566 		if (!ret) {
567 			DRM_DEV_INFO(drm->dev, "loaded %s from legacy location\n",
568 				fwname);
569 			adreno_gpu->fwloc = FW_LOCATION_LEGACY;
570 			goto out;
571 		} else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) {
572 			DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n",
573 				fwname, ret);
574 			fw = ERR_PTR(ret);
575 			goto out;
576 		}
577 	}
578 
579 	/*
580 	 * Finally fall back to request_firmware() for cases where the
581 	 * usermode helper is needed (I think mainly android)
582 	 */
583 	if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) ||
584 	    (adreno_gpu->fwloc == FW_LOCATION_HELPER)) {
585 
586 		ret = request_firmware(&fw, newname, drm->dev);
587 		if (!ret) {
588 			DRM_DEV_INFO(drm->dev, "loaded %s with helper\n",
589 				newname);
590 			adreno_gpu->fwloc = FW_LOCATION_HELPER;
591 			goto out;
592 		} else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) {
593 			DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n",
594 				newname, ret);
595 			fw = ERR_PTR(ret);
596 			goto out;
597 		}
598 	}
599 
600 	DRM_DEV_ERROR(drm->dev, "failed to load %s\n", fwname);
601 	fw = ERR_PTR(-ENOENT);
602 out:
603 	kfree(newname);
604 	return fw;
605 }
606 
607 int adreno_load_fw(struct adreno_gpu *adreno_gpu)
608 {
609 	int i;
610 
611 	for (i = 0; i < ARRAY_SIZE(adreno_gpu->info->fw); i++) {
612 		const struct firmware *fw;
613 
614 		if (!adreno_gpu->info->fw[i])
615 			continue;
616 
617 		/* Skip loading GMU firmware with GMU Wrapper */
618 		if (adreno_has_gmu_wrapper(adreno_gpu) && i == ADRENO_FW_GMU)
619 			continue;
620 
621 		/* Skip if the firmware has already been loaded */
622 		if (adreno_gpu->fw[i])
623 			continue;
624 
625 		fw = adreno_request_fw(adreno_gpu, adreno_gpu->info->fw[i]);
626 		if (IS_ERR(fw))
627 			return PTR_ERR(fw);
628 
629 		adreno_gpu->fw[i] = fw;
630 	}
631 
632 	return 0;
633 }
634 
635 struct drm_gem_object *adreno_fw_create_bo(struct msm_gpu *gpu,
636 		const struct firmware *fw, u64 *iova)
637 {
638 	struct drm_gem_object *bo;
639 	void *ptr;
640 
641 	ptr = msm_gem_kernel_new(gpu->dev, fw->size - 4,
642 		MSM_BO_WC | MSM_BO_GPU_READONLY, gpu->vm, &bo, iova);
643 
644 	if (IS_ERR(ptr))
645 		return ERR_CAST(ptr);
646 
647 	memcpy(ptr, &fw->data[4], fw->size - 4);
648 
649 	msm_gem_put_vaddr(bo);
650 
651 	return bo;
652 }
653 
654 int adreno_hw_init(struct msm_gpu *gpu)
655 {
656 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
657 	int ret;
658 
659 	VERB("%s", gpu->name);
660 
661 	if (adreno_gpu->info->family >= ADRENO_6XX_GEN1 &&
662 	    qcom_scm_set_gpu_smmu_aperture_is_available()) {
663 		/* We currently always use context bank 0, so hard code this */
664 		ret = qcom_scm_set_gpu_smmu_aperture(0);
665 		if (ret)
666 			DRM_DEV_ERROR(gpu->dev->dev, "unable to set SMMU aperture: %d\n", ret);
667 	}
668 
669 	for (int i = 0; i < gpu->nr_rings; i++) {
670 		struct msm_ringbuffer *ring = gpu->rb[i];
671 
672 		if (!ring)
673 			continue;
674 
675 		ring->cur = ring->start;
676 		ring->next = ring->start;
677 		ring->memptrs->rptr = 0;
678 		ring->memptrs->bv_fence = ring->fctx->completed_fence;
679 
680 		/* Detect and clean up an impossible fence, ie. if GPU managed
681 		 * to scribble something invalid, we don't want that to confuse
682 		 * us into mistakingly believing that submits have completed.
683 		 */
684 		if (fence_before(ring->fctx->last_fence, ring->memptrs->fence)) {
685 			ring->memptrs->fence = ring->fctx->last_fence;
686 		}
687 	}
688 
689 	return 0;
690 }
691 
692 /* Use this helper to read rptr, since a430 doesn't update rptr in memory */
693 static uint32_t get_rptr(struct adreno_gpu *adreno_gpu,
694 		struct msm_ringbuffer *ring)
695 {
696 	struct msm_gpu *gpu = &adreno_gpu->base;
697 
698 	return gpu->funcs->get_rptr(gpu, ring);
699 }
700 
701 struct msm_ringbuffer *adreno_active_ring(struct msm_gpu *gpu)
702 {
703 	return gpu->rb[0];
704 }
705 
706 void adreno_recover(struct msm_gpu *gpu)
707 {
708 	struct drm_device *dev = gpu->dev;
709 	int ret;
710 
711 	msm_perfcntr_suspend(gpu);
712 	gpu->funcs->pm_suspend(gpu);
713 	gpu->funcs->pm_resume(gpu);
714 	msm_perfcntr_resume(gpu);
715 
716 	ret = msm_gpu_hw_init(gpu);
717 	if (ret) {
718 		DRM_DEV_ERROR(dev->dev, "gpu hw init failed: %d\n", ret);
719 		/* hmm, oh well? */
720 	}
721 }
722 
723 void adreno_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring, u32 reg)
724 {
725 	uint32_t wptr;
726 
727 	/* Copy the shadow to the actual register */
728 	ring->cur = ring->next;
729 
730 	/*
731 	 * Mask wptr value that we calculate to fit in the HW range. This is
732 	 * to account for the possibility that the last command fit exactly into
733 	 * the ringbuffer and rb->next hasn't wrapped to zero yet
734 	 */
735 	wptr = get_wptr(ring);
736 
737 	/* ensure writes to ringbuffer have hit system memory: */
738 	mb();
739 
740 	gpu_write(gpu, reg, wptr);
741 }
742 
743 bool adreno_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
744 {
745 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
746 	uint32_t wptr = get_wptr(ring);
747 
748 	/* wait for CP to drain ringbuffer: */
749 	if (!spin_until(get_rptr(adreno_gpu, ring) == wptr))
750 		return true;
751 
752 	/* TODO maybe we need to reset GPU here to recover from hang? */
753 	DRM_ERROR("%s: timeout waiting to drain ringbuffer %d rptr/wptr = %X/%X\n",
754 		gpu->name, ring->id, get_rptr(adreno_gpu, ring), wptr);
755 
756 	return false;
757 }
758 
759 int adreno_gpu_state_get(struct msm_gpu *gpu, struct msm_gpu_state *state)
760 {
761 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
762 	int i, count = 0;
763 
764 	WARN_ON(!mutex_is_locked(&gpu->lock));
765 
766 	kref_init(&state->ref);
767 
768 	ktime_get_real_ts64(&state->time);
769 
770 	for (i = 0; i < gpu->nr_rings; i++) {
771 		int size = 0, j;
772 
773 		state->ring[i].fence = gpu->rb[i]->memptrs->fence;
774 		state->ring[i].iova = gpu->rb[i]->iova;
775 		state->ring[i].seqno = gpu->rb[i]->fctx->last_fence;
776 		state->ring[i].rptr = get_rptr(adreno_gpu, gpu->rb[i]);
777 		state->ring[i].wptr = get_wptr(gpu->rb[i]);
778 
779 		/* Copy at least 'wptr' dwords of the data */
780 		size = state->ring[i].wptr;
781 
782 		/* After wptr find the last non zero dword to save space */
783 		for (j = state->ring[i].wptr; j < MSM_GPU_RINGBUFFER_SZ >> 2; j++)
784 			if (gpu->rb[i]->start[j])
785 				size = j + 1;
786 
787 		if (size) {
788 			state->ring[i].data = kvmemdup(gpu->rb[i]->start, size << 2, GFP_KERNEL);
789 			if (state->ring[i].data)
790 				state->ring[i].data_size = size << 2;
791 		}
792 	}
793 
794 	/* Some targets prefer to collect their own registers */
795 	if (!adreno_gpu->registers)
796 		return 0;
797 
798 	/* Count the number of registers */
799 	for (i = 0; adreno_gpu->registers[i] != ~0; i += 2)
800 		count += adreno_gpu->registers[i + 1] -
801 			adreno_gpu->registers[i] + 1;
802 
803 	state->registers = kcalloc(count * 2, sizeof(u32), GFP_KERNEL);
804 	if (state->registers) {
805 		int pos = 0;
806 
807 		for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
808 			u32 start = adreno_gpu->registers[i];
809 			u32 end   = adreno_gpu->registers[i + 1];
810 			u32 addr;
811 
812 			for (addr = start; addr <= end; addr++) {
813 				state->registers[pos++] = addr;
814 				state->registers[pos++] = gpu_read(gpu, addr);
815 			}
816 		}
817 
818 		state->nr_registers = count;
819 	}
820 
821 	return 0;
822 }
823 
824 void adreno_gpu_state_destroy(struct msm_gpu_state *state)
825 {
826 	int i;
827 
828 	for (i = 0; i < ARRAY_SIZE(state->ring); i++)
829 		kvfree(state->ring[i].data);
830 
831 	for (i = 0; state->bos && i < state->nr_bos; i++)
832 		kvfree(state->bos[i].data);
833 
834 	kfree(state->vm_logs);
835 	kfree(state->bos);
836 	kfree(state->comm);
837 	kfree(state->cmd);
838 	kfree(state->registers);
839 }
840 
841 static void adreno_gpu_state_kref_destroy(struct kref *kref)
842 {
843 	struct msm_gpu_state *state = container_of(kref,
844 		struct msm_gpu_state, ref);
845 
846 	adreno_gpu_state_destroy(state);
847 	kfree(state);
848 }
849 
850 int adreno_gpu_state_put(struct msm_gpu_state *state)
851 {
852 	if (IS_ERR_OR_NULL(state))
853 		return 1;
854 
855 	return kref_put(&state->ref, adreno_gpu_state_kref_destroy);
856 }
857 
858 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
859 
860 static char *adreno_gpu_ascii85_encode(u32 *src, size_t len)
861 {
862 	void *buf;
863 	size_t buf_itr = 0, buffer_size;
864 	char out[ASCII85_BUFSZ];
865 	long l;
866 	int i;
867 
868 	if (!src || !len)
869 		return NULL;
870 
871 	l = ascii85_encode_len(len);
872 
873 	/*
874 	 * Ascii85 outputs either a 5 byte string or a 1 byte string. So we
875 	 * account for the worst case of 5 bytes per dword plus the 1 for '\0'
876 	 */
877 	buffer_size = (l * 5) + 1;
878 
879 	buf = kvmalloc(buffer_size, GFP_KERNEL);
880 	if (!buf)
881 		return NULL;
882 
883 	for (i = 0; i < l; i++)
884 		buf_itr += scnprintf(buf + buf_itr, buffer_size - buf_itr, "%s",
885 				ascii85_encode(src[i], out));
886 
887 	return buf;
888 }
889 
890 /* len is expected to be in bytes
891  *
892  * WARNING: *ptr should be allocated with kvmalloc or friends.  It can be free'd
893  * with kvfree() and replaced with a newly kvmalloc'd buffer on the first call
894  * when the unencoded raw data is encoded
895  */
896 void adreno_show_object(struct drm_printer *p, void **ptr, int len,
897 		bool *encoded)
898 {
899 	if (!*ptr || !len)
900 		return;
901 
902 	if (!*encoded) {
903 		long datalen, i;
904 		u32 *buf = *ptr;
905 
906 		/*
907 		 * Only dump the non-zero part of the buffer - rarely will
908 		 * any data completely fill the entire allocated size of
909 		 * the buffer.
910 		 */
911 		for (datalen = 0, i = 0; i < len >> 2; i++)
912 			if (buf[i])
913 				datalen = ((i + 1) << 2);
914 
915 		/*
916 		 * If we reach here, then the originally captured binary buffer
917 		 * will be replaced with the ascii85 encoded string
918 		 */
919 		*ptr = adreno_gpu_ascii85_encode(buf, datalen);
920 
921 		kvfree(buf);
922 
923 		*encoded = true;
924 	}
925 
926 	if (!*ptr)
927 		return;
928 
929 	drm_puts(p, "    data: !!ascii85 |\n");
930 	drm_puts(p, "     ");
931 
932 	drm_puts(p, *ptr);
933 
934 	drm_puts(p, "\n");
935 }
936 
937 void adreno_show(struct msm_gpu *gpu, struct msm_gpu_state *state,
938 		struct drm_printer *p)
939 {
940 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
941 	int i;
942 
943 	if (IS_ERR_OR_NULL(state))
944 		return;
945 
946 	drm_printf(p, "revision: %u (%"ADRENO_CHIPID_FMT")\n",
947 			adreno_gpu->info->revn,
948 			ADRENO_CHIPID_ARGS(adreno_gpu->chip_id));
949 	/*
950 	 * If this is state collected due to iova fault, so fault related info
951 	 *
952 	 * TTBR0 would not be zero, so this is a good way to distinguish
953 	 */
954 	if (state->fault_info.ttbr0) {
955 		const struct msm_gpu_fault_info *info = &state->fault_info;
956 
957 		drm_puts(p, "fault-info:\n");
958 		drm_printf(p, "  - ttbr0=%.16llx\n", info->ttbr0);
959 		drm_printf(p, "  - iova=%.16lx\n", info->iova);
960 		drm_printf(p, "  - dir=%s\n", info->flags & IOMMU_FAULT_WRITE ? "WRITE" : "READ");
961 		drm_printf(p, "  - type=%s\n", info->type);
962 		drm_printf(p, "  - source=%s\n", info->block);
963 
964 		/* Information extracted from what we think are the current
965 		 * pgtables.  Hopefully the TTBR0 matches what we've extracted
966 		 * from the SMMU registers in smmu_info!
967 		 */
968 		drm_puts(p, "pgtable-fault-info:\n");
969 		drm_printf(p, "  - ttbr0: %.16llx\n", (u64)info->pgtbl_ttbr0);
970 		drm_printf(p, "  - asid: %d\n", info->asid);
971 		drm_printf(p, "  - ptes: %.16llx %.16llx %.16llx %.16llx\n",
972 			   info->ptes[0], info->ptes[1], info->ptes[2], info->ptes[3]);
973 	}
974 
975 	if (state->vm_logs) {
976 		drm_puts(p, "vm-log:\n");
977 		for (i = 0; i < state->nr_vm_logs; i++) {
978 			struct msm_gem_vm_log_entry *e = &state->vm_logs[i];
979 			drm_printf(p, "  - %s:%d: 0x%016llx-0x%016llx\n",
980 				   e->op, e->queue_id, e->iova,
981 				   e->iova + e->range);
982 		}
983 	}
984 
985 	drm_printf(p, "rbbm-status: 0x%08x\n", state->rbbm_status);
986 
987 	drm_puts(p, "ringbuffer:\n");
988 
989 	for (i = 0; i < gpu->nr_rings; i++) {
990 		drm_printf(p, "  - id: %d\n", i);
991 		drm_printf(p, "    iova: 0x%016llx\n", state->ring[i].iova);
992 		drm_printf(p, "    last-fence: %u\n", state->ring[i].seqno);
993 		drm_printf(p, "    retired-fence: %u\n", state->ring[i].fence);
994 		drm_printf(p, "    rptr: %u\n", state->ring[i].rptr);
995 		drm_printf(p, "    wptr: %u\n", state->ring[i].wptr);
996 		drm_printf(p, "    size: %u\n", MSM_GPU_RINGBUFFER_SZ);
997 
998 		adreno_show_object(p, &state->ring[i].data,
999 			state->ring[i].data_size, &state->ring[i].encoded);
1000 	}
1001 
1002 	if (state->bos) {
1003 		drm_puts(p, "bos:\n");
1004 
1005 		for (i = 0; i < state->nr_bos; i++) {
1006 			drm_printf(p, "  - iova: 0x%016llx\n",
1007 				state->bos[i].iova);
1008 			drm_printf(p, "    size: %zd\n", state->bos[i].size);
1009 			drm_printf(p, "    flags: 0x%x\n", state->bos[i].flags);
1010 			drm_printf(p, "    name: %-32s\n", state->bos[i].name);
1011 
1012 			adreno_show_object(p, &state->bos[i].data,
1013 				state->bos[i].size, &state->bos[i].encoded);
1014 		}
1015 	}
1016 
1017 	if (state->nr_registers) {
1018 		drm_puts(p, "registers:\n");
1019 
1020 		for (i = 0; i < state->nr_registers; i++) {
1021 			drm_printf(p, "  - { offset: 0x%04x, value: 0x%08x }\n",
1022 				state->registers[i * 2] << 2,
1023 				state->registers[(i * 2) + 1]);
1024 		}
1025 	}
1026 }
1027 #endif
1028 
1029 /* Dump common gpu status and scratch registers on any hang, to make
1030  * the hangcheck logs more useful.  The scratch registers seem always
1031  * safe to read when GPU has hung (unlike some other regs, depending
1032  * on how the GPU hung), and they are useful to match up to cmdstream
1033  * dumps when debugging hangs:
1034  */
1035 void adreno_dump_info(struct msm_gpu *gpu)
1036 {
1037 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
1038 	int i;
1039 
1040 	printk("revision: %u (%"ADRENO_CHIPID_FMT")\n",
1041 			adreno_gpu->info->revn,
1042 			ADRENO_CHIPID_ARGS(adreno_gpu->chip_id));
1043 
1044 	for (i = 0; i < gpu->nr_rings; i++) {
1045 		struct msm_ringbuffer *ring = gpu->rb[i];
1046 
1047 		printk("rb %d: fence:    %d/%d\n", i,
1048 			ring->memptrs->fence,
1049 			ring->fctx->last_fence);
1050 
1051 		printk("rptr:     %d\n", get_rptr(adreno_gpu, ring));
1052 		printk("rb wptr:  %d\n", get_wptr(ring));
1053 	}
1054 }
1055 
1056 /* would be nice to not have to duplicate the _show() stuff with printk(): */
1057 void adreno_dump(struct msm_gpu *gpu)
1058 {
1059 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
1060 	int i;
1061 
1062 	if (!adreno_gpu->registers)
1063 		return;
1064 
1065 	/* dump these out in a form that can be parsed by demsm: */
1066 	printk("IO:region %s 00000000 00020000\n", gpu->name);
1067 	for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
1068 		uint32_t start = adreno_gpu->registers[i];
1069 		uint32_t end   = adreno_gpu->registers[i+1];
1070 		uint32_t addr;
1071 
1072 		for (addr = start; addr <= end; addr++) {
1073 			uint32_t val = gpu_read(gpu, addr);
1074 			printk("IO:R %08x %08x\n", addr<<2, val);
1075 		}
1076 	}
1077 }
1078 
1079 static uint32_t ring_freewords(struct msm_ringbuffer *ring)
1080 {
1081 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(ring->gpu);
1082 	uint32_t size = MSM_GPU_RINGBUFFER_SZ >> 2;
1083 	/* Use ring->next to calculate free size */
1084 	uint32_t wptr = ring->next - ring->start;
1085 	uint32_t rptr = get_rptr(adreno_gpu, ring);
1086 	return (rptr + (size - 1) - wptr) % size;
1087 }
1088 
1089 void adreno_wait_ring(struct msm_ringbuffer *ring, uint32_t ndwords)
1090 {
1091 	if (spin_until(ring_freewords(ring) >= ndwords))
1092 		DRM_DEV_ERROR(ring->gpu->dev->dev,
1093 			"timeout waiting for space in ringbuffer %d\n",
1094 			ring->id);
1095 }
1096 
1097 static int adreno_get_pwrlevels(struct device *dev,
1098 		struct msm_gpu *gpu)
1099 {
1100 	struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
1101 	unsigned long freq = ULONG_MAX;
1102 	struct dev_pm_opp *opp;
1103 	int ret;
1104 
1105 	gpu->fast_rate = 0;
1106 
1107 	/* devm_pm_opp_of_add_table may error out but will still create an OPP table */
1108 	ret = devm_pm_opp_of_add_table(dev);
1109 	if (ret == -ENODEV) {
1110 		/* Special cases for ancient hw with ancient DT bindings */
1111 		if (adreno_is_a2xx(adreno_gpu)) {
1112 			dev_warn(dev, "Unable to find the OPP table. Falling back to 200 MHz.\n");
1113 			dev_pm_opp_add(dev, 200000000, 0);
1114 		} else if (adreno_is_a320(adreno_gpu)) {
1115 			dev_warn(dev, "Unable to find the OPP table. Falling back to 450 MHz.\n");
1116 			dev_pm_opp_add(dev, 450000000, 0);
1117 		} else {
1118 			DRM_DEV_ERROR(dev, "Unable to find the OPP table\n");
1119 			return -ENODEV;
1120 		}
1121 	} else if (ret) {
1122 		DRM_DEV_ERROR(dev, "Unable to set the OPP table\n");
1123 		return ret;
1124 	}
1125 
1126 	/* Find the fastest defined rate */
1127 	opp = dev_pm_opp_find_freq_floor(dev, &freq);
1128 	if (IS_ERR(opp))
1129 		return PTR_ERR(opp);
1130 
1131 	gpu->fast_rate = freq;
1132 	dev_pm_opp_put(opp);
1133 
1134 	DBG("fast_rate=%u, slow_rate=27000000", gpu->fast_rate);
1135 
1136 	return 0;
1137 }
1138 
1139 int adreno_gpu_ocmem_init(struct device *dev, struct adreno_gpu *adreno_gpu,
1140 			  struct adreno_ocmem *adreno_ocmem)
1141 {
1142 	struct ocmem_buf *ocmem_hdl;
1143 	struct ocmem *ocmem;
1144 
1145 	ocmem = of_get_ocmem(dev);
1146 	if (IS_ERR(ocmem)) {
1147 		if (PTR_ERR(ocmem) == -ENODEV) {
1148 			/*
1149 			 * Return success since either the ocmem property was
1150 			 * not specified in device tree, or ocmem support is
1151 			 * not compiled into the kernel.
1152 			 */
1153 			return 0;
1154 		}
1155 
1156 		return PTR_ERR(ocmem);
1157 	}
1158 
1159 	ocmem_hdl = ocmem_allocate(ocmem, OCMEM_GRAPHICS, adreno_gpu->info->gmem);
1160 	if (IS_ERR(ocmem_hdl))
1161 		return PTR_ERR(ocmem_hdl);
1162 
1163 	adreno_ocmem->ocmem = ocmem;
1164 	adreno_ocmem->base = ocmem_hdl->addr;
1165 	adreno_ocmem->hdl = ocmem_hdl;
1166 
1167 	if (WARN_ON(ocmem_hdl->len != adreno_gpu->info->gmem))
1168 		return -ENOMEM;
1169 
1170 	return 0;
1171 }
1172 
1173 void adreno_gpu_ocmem_cleanup(struct adreno_ocmem *adreno_ocmem)
1174 {
1175 	if (adreno_ocmem && adreno_ocmem->base)
1176 		ocmem_free(adreno_ocmem->ocmem, OCMEM_GRAPHICS,
1177 			   adreno_ocmem->hdl);
1178 }
1179 
1180 int adreno_read_speedbin(struct device *dev, u32 *speedbin)
1181 {
1182 	return nvmem_cell_read_variable_le_u32(dev, "speed_bin", speedbin);
1183 }
1184 
1185 int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
1186 		struct adreno_gpu *adreno_gpu,
1187 		const struct adreno_gpu_funcs *funcs, int nr_rings)
1188 {
1189 	struct device *dev = &pdev->dev;
1190 	struct adreno_platform_config *config = dev->platform_data;
1191 	struct msm_gpu_config adreno_gpu_config  = { 0 };
1192 	struct msm_gpu *gpu = &adreno_gpu->base;
1193 	const char *gpu_name;
1194 	int ret;
1195 
1196 	adreno_gpu->funcs = funcs;
1197 	adreno_gpu->info = config->info;
1198 	adreno_gpu->chip_id = config->chip_id;
1199 
1200 	gpu->allow_relocs = config->info->family < ADRENO_6XX_GEN1;
1201 	gpu->pdev = pdev;
1202 
1203 	/* Only handle the core clock when GMU is not in use (or is absent). */
1204 	if (adreno_has_gmu_wrapper(adreno_gpu) ||
1205 	    adreno_has_rgmu(adreno_gpu) ||
1206 	    adreno_gpu->info->family < ADRENO_6XX_GEN1) {
1207 		/*
1208 		 * This can only be done before devm_pm_opp_of_add_table(), or
1209 		 * dev_pm_opp_set_config() will WARN_ON()
1210 		 */
1211 		if (IS_ERR(devm_clk_get(dev, "core"))) {
1212 			/*
1213 			 * If "core" is absent, go for the legacy clock name.
1214 			 * If we got this far in probing, it's a given one of
1215 			 * them exists.
1216 			 */
1217 			devm_pm_opp_set_clkname(dev, "core_clk");
1218 		} else
1219 			devm_pm_opp_set_clkname(dev, "core");
1220 	}
1221 
1222 	gpu_name = devm_kasprintf(dev, GFP_KERNEL, "%"ADRENO_CHIPID_FMT,
1223 			ADRENO_CHIPID_ARGS(config->chip_id));
1224 	if (!gpu_name)
1225 		return -ENOMEM;
1226 
1227 	adreno_gpu_config.ioname = "kgsl_3d0_reg_memory";
1228 
1229 	adreno_gpu_config.nr_rings = nr_rings;
1230 
1231 	ret = adreno_get_pwrlevels(dev, gpu);
1232 	if (ret)
1233 		return ret;
1234 
1235 	init_completion(&adreno_gpu->fault_coredump_done);
1236 	complete_all(&adreno_gpu->fault_coredump_done);
1237 
1238 	pm_runtime_set_autosuspend_delay(dev,
1239 		adreno_gpu->info->inactive_period);
1240 	pm_runtime_use_autosuspend(dev);
1241 
1242 	return msm_gpu_init(drm, pdev, &adreno_gpu->base, &funcs->base,
1243 			gpu_name, &adreno_gpu_config);
1244 }
1245 
1246 void adreno_gpu_cleanup(struct adreno_gpu *adreno_gpu)
1247 {
1248 	struct msm_gpu *gpu = &adreno_gpu->base;
1249 	struct msm_drm_private *priv = gpu->dev ? gpu->dev->dev_private : NULL;
1250 	unsigned int i;
1251 
1252 	for (i = 0; i < ARRAY_SIZE(adreno_gpu->info->fw); i++)
1253 		release_firmware(adreno_gpu->fw[i]);
1254 
1255 	if (priv && pm_runtime_enabled(&priv->gpu_pdev->dev))
1256 		pm_runtime_disable(&priv->gpu_pdev->dev);
1257 
1258 	msm_gpu_cleanup(&adreno_gpu->base);
1259 }
1260