1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
6 * Copyright (c) 2014 The Linux Foundation. All rights reserved.
9 #include <linux/ascii85.h>
10 #include <linux/interconnect.h>
11 #include <linux/qcom_scm.h>
12 #include <linux/kernel.h>
13 #include <linux/of_address.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"
24 static bool zap_available = true;
26 static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname,
29 struct device *dev = &gpu->pdev->dev;
30 const struct firmware *fw;
31 const char *signed_fwname = NULL;
32 struct device_node *np, *mem_np;
36 void *mem_region = NULL;
39 if (!IS_ENABLED(CONFIG_ARCH_QCOM)) {
40 zap_available = false;
44 np = of_get_child_by_name(dev->of_node, "zap-shader");
46 zap_available = false;
50 mem_np = of_parse_phandle(np, "memory-region", 0);
53 zap_available = false;
57 ret = of_address_to_resource(mem_np, 0, &r);
65 * Check for a firmware-name property. This is the new scheme
66 * to handle firmware that may be signed with device specific
67 * keys, allowing us to have a different zap fw path for different
70 * If the firmware-name property is found, we bypass the
71 * adreno_request_fw() mechanism, because we don't need to handle
72 * the /lib/firmware/qcom/... vs /lib/firmware/... case.
74 * If the firmware-name property is not found, for backwards
75 * compatibility we fall back to the fwname from the gpulist
78 of_property_read_string_index(np, "firmware-name", 0, &signed_fwname);
80 fwname = signed_fwname;
81 ret = request_firmware_direct(&fw, fwname, gpu->dev->dev);
85 /* Request the MDT file from the default location: */
86 fw = adreno_request_fw(to_adreno_gpu(gpu), fwname);
89 * For new targets, we require the firmware-name property,
90 * if a zap-shader is required, rather than falling back
91 * to a firmware name specified in gpulist.
93 * Because the firmware is signed with a (potentially)
94 * device specific key, having the name come from gpulist
95 * was a bad idea, and is only provided for backwards
96 * compatibility for older targets.
102 DRM_DEV_ERROR(dev, "Unable to load %s\n", fwname);
106 /* Figure out how much memory we need */
107 mem_size = qcom_mdt_get_size(fw);
113 if (mem_size > resource_size(&r)) {
115 "memory region is too small to load the MDT\n");
120 /* Allocate memory for the firmware image */
121 mem_region = memremap(mem_phys, mem_size, MEMREMAP_WC);
128 * Load the rest of the MDT
130 * Note that we could be dealing with two different paths, since
131 * with upstream linux-firmware it would be in a qcom/ subdir..
132 * adreno_request_fw() handles this, but qcom_mdt_load() does
133 * not. But since we've already gotten through adreno_request_fw()
134 * we know which of the two cases it is:
136 if (signed_fwname || (to_adreno_gpu(gpu)->fwloc == FW_LOCATION_LEGACY)) {
137 ret = qcom_mdt_load(dev, fw, fwname, pasid,
138 mem_region, mem_phys, mem_size, NULL);
142 newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname);
144 ret = qcom_mdt_load(dev, fw, newname, pasid,
145 mem_region, mem_phys, mem_size, NULL);
151 /* Send the image to the secure world */
152 ret = qcom_scm_pas_auth_and_reset(pasid);
155 * If the scm call returns -EOPNOTSUPP we assume that this target
156 * doesn't need/support the zap shader so quietly fail
158 if (ret == -EOPNOTSUPP)
159 zap_available = false;
161 DRM_DEV_ERROR(dev, "Unable to authorize the image\n");
165 memunmap(mem_region);
167 release_firmware(fw);
172 int adreno_zap_shader_load(struct msm_gpu *gpu, u32 pasid)
174 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
175 struct platform_device *pdev = gpu->pdev;
177 /* Short cut if we determine the zap shader isn't available/needed */
181 /* We need SCM to be able to load the firmware */
182 if (!qcom_scm_is_available()) {
183 DRM_DEV_ERROR(&pdev->dev, "SCM is not available\n");
184 return -EPROBE_DEFER;
187 return zap_shader_load_mdt(gpu, adreno_gpu->info->zapfw, pasid);
190 void adreno_set_llc_attributes(struct iommu_domain *iommu)
192 iommu_set_pgtable_quirks(iommu, IO_PGTABLE_QUIRK_ARM_OUTER_WBWA);
195 struct msm_gem_address_space *
196 adreno_iommu_create_address_space(struct msm_gpu *gpu,
197 struct platform_device *pdev)
199 struct iommu_domain *iommu;
201 struct msm_gem_address_space *aspace;
204 iommu = iommu_domain_alloc(&platform_bus_type);
208 mmu = msm_iommu_new(&pdev->dev, iommu);
210 iommu_domain_free(iommu);
211 return ERR_CAST(mmu);
215 * Use the aperture start or SZ_16M, whichever is greater. This will
216 * ensure that we align with the allocated pagetable range while still
217 * allowing room in the lower 32 bits for GMEM and whatnot
219 start = max_t(u64, SZ_16M, iommu->geometry.aperture_start);
220 size = iommu->geometry.aperture_end - start + 1;
222 aspace = msm_gem_address_space_create(mmu, "gpu",
223 start & GENMASK_ULL(48, 0), size);
225 if (IS_ERR(aspace) && !IS_ERR(mmu))
226 mmu->funcs->destroy(mmu);
231 int adreno_get_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
232 uint32_t param, uint64_t *value, uint32_t *len)
234 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
236 /* No pointer params yet */
241 case MSM_PARAM_GPU_ID:
242 *value = adreno_gpu->info->revn;
244 case MSM_PARAM_GMEM_SIZE:
245 *value = adreno_gpu->gmem;
247 case MSM_PARAM_GMEM_BASE:
248 *value = !adreno_is_a650_family(adreno_gpu) ? 0x100000 : 0;
250 case MSM_PARAM_CHIP_ID:
251 *value = (uint64_t)adreno_gpu->rev.patchid |
252 ((uint64_t)adreno_gpu->rev.minor << 8) |
253 ((uint64_t)adreno_gpu->rev.major << 16) |
254 ((uint64_t)adreno_gpu->rev.core << 24);
255 if (!adreno_gpu->info->revn)
256 *value |= ((uint64_t) adreno_gpu->speedbin) << 32;
258 case MSM_PARAM_MAX_FREQ:
259 *value = adreno_gpu->base.fast_rate;
261 case MSM_PARAM_TIMESTAMP:
262 if (adreno_gpu->funcs->get_timestamp) {
265 pm_runtime_get_sync(&gpu->pdev->dev);
266 ret = adreno_gpu->funcs->get_timestamp(gpu, value);
267 pm_runtime_put_autosuspend(&gpu->pdev->dev);
272 case MSM_PARAM_PRIORITIES:
273 *value = gpu->nr_rings * NR_SCHED_PRIORITIES;
275 case MSM_PARAM_PP_PGTABLE:
278 case MSM_PARAM_FAULTS:
280 *value = gpu->global_faults + ctx->aspace->faults;
282 *value = gpu->global_faults;
284 case MSM_PARAM_SUSPENDS:
285 *value = gpu->suspend_count;
287 case MSM_PARAM_VA_START:
288 if (ctx->aspace == gpu->aspace)
290 *value = ctx->aspace->va_start;
292 case MSM_PARAM_VA_SIZE:
293 if (ctx->aspace == gpu->aspace)
295 *value = ctx->aspace->va_size;
298 DBG("%s: invalid param: %u", gpu->name, param);
303 int adreno_set_param(struct msm_gpu *gpu, struct msm_file_private *ctx,
304 uint32_t param, uint64_t value, uint32_t len)
308 case MSM_PARAM_CMDLINE:
309 /* kstrdup_quotable_cmdline() limits to PAGE_SIZE, so
310 * that should be a reasonable upper bound
322 case MSM_PARAM_CMDLINE: {
325 str = kmalloc(len + 1, GFP_KERNEL);
329 if (copy_from_user(str, u64_to_user_ptr(value), len)) {
334 /* Ensure string is null terminated: */
337 if (param == MSM_PARAM_COMM) {
340 paramp = &ctx->cmdline;
348 case MSM_PARAM_SYSPROF:
349 if (!capable(CAP_SYS_ADMIN))
351 return msm_file_private_set_sysprof(ctx, gpu, value);
353 DBG("%s: invalid param: %u", gpu->name, param);
358 const struct firmware *
359 adreno_request_fw(struct adreno_gpu *adreno_gpu, const char *fwname)
361 struct drm_device *drm = adreno_gpu->base.dev;
362 const struct firmware *fw = NULL;
366 newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname);
368 return ERR_PTR(-ENOMEM);
371 * Try first to load from qcom/$fwfile using a direct load (to avoid
372 * a potential timeout waiting for usermode helper)
374 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) ||
375 (adreno_gpu->fwloc == FW_LOCATION_NEW)) {
377 ret = request_firmware_direct(&fw, newname, drm->dev);
379 DRM_DEV_INFO(drm->dev, "loaded %s from new location\n",
381 adreno_gpu->fwloc = FW_LOCATION_NEW;
383 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) {
384 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n",
392 * Then try the legacy location without qcom/ prefix
394 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) ||
395 (adreno_gpu->fwloc == FW_LOCATION_LEGACY)) {
397 ret = request_firmware_direct(&fw, fwname, drm->dev);
399 DRM_DEV_INFO(drm->dev, "loaded %s from legacy location\n",
401 adreno_gpu->fwloc = FW_LOCATION_LEGACY;
403 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) {
404 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n",
412 * Finally fall back to request_firmware() for cases where the
413 * usermode helper is needed (I think mainly android)
415 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) ||
416 (adreno_gpu->fwloc == FW_LOCATION_HELPER)) {
418 ret = request_firmware(&fw, newname, drm->dev);
420 DRM_DEV_INFO(drm->dev, "loaded %s with helper\n",
422 adreno_gpu->fwloc = FW_LOCATION_HELPER;
424 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) {
425 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n",
432 DRM_DEV_ERROR(drm->dev, "failed to load %s\n", fwname);
433 fw = ERR_PTR(-ENOENT);
439 int adreno_load_fw(struct adreno_gpu *adreno_gpu)
443 for (i = 0; i < ARRAY_SIZE(adreno_gpu->info->fw); i++) {
444 const struct firmware *fw;
446 if (!adreno_gpu->info->fw[i])
449 /* Skip if the firmware has already been loaded */
450 if (adreno_gpu->fw[i])
453 fw = adreno_request_fw(adreno_gpu, adreno_gpu->info->fw[i]);
457 adreno_gpu->fw[i] = fw;
463 struct drm_gem_object *adreno_fw_create_bo(struct msm_gpu *gpu,
464 const struct firmware *fw, u64 *iova)
466 struct drm_gem_object *bo;
469 ptr = msm_gem_kernel_new(gpu->dev, fw->size - 4,
470 MSM_BO_WC | MSM_BO_GPU_READONLY, gpu->aspace, &bo, iova);
473 return ERR_CAST(ptr);
475 memcpy(ptr, &fw->data[4], fw->size - 4);
477 msm_gem_put_vaddr(bo);
482 int adreno_hw_init(struct msm_gpu *gpu)
484 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
487 VERB("%s", gpu->name);
489 ret = adreno_load_fw(adreno_gpu);
493 for (i = 0; i < gpu->nr_rings; i++) {
494 struct msm_ringbuffer *ring = gpu->rb[i];
499 ring->cur = ring->start;
500 ring->next = ring->start;
501 ring->memptrs->rptr = 0;
503 /* Detect and clean up an impossible fence, ie. if GPU managed
504 * to scribble something invalid, we don't want that to confuse
505 * us into mistakingly believing that submits have completed.
507 if (fence_before(ring->fctx->last_fence, ring->memptrs->fence)) {
508 ring->memptrs->fence = ring->fctx->last_fence;
515 /* Use this helper to read rptr, since a430 doesn't update rptr in memory */
516 static uint32_t get_rptr(struct adreno_gpu *adreno_gpu,
517 struct msm_ringbuffer *ring)
519 struct msm_gpu *gpu = &adreno_gpu->base;
521 return gpu->funcs->get_rptr(gpu, ring);
524 struct msm_ringbuffer *adreno_active_ring(struct msm_gpu *gpu)
529 void adreno_recover(struct msm_gpu *gpu)
531 struct drm_device *dev = gpu->dev;
534 // XXX pm-runtime?? we *need* the device to be off after this
535 // so maybe continuing to call ->pm_suspend/resume() is better?
537 gpu->funcs->pm_suspend(gpu);
538 gpu->funcs->pm_resume(gpu);
540 ret = msm_gpu_hw_init(gpu);
542 DRM_DEV_ERROR(dev->dev, "gpu hw init failed: %d\n", ret);
547 void adreno_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring, u32 reg)
551 /* Copy the shadow to the actual register */
552 ring->cur = ring->next;
555 * Mask wptr value that we calculate to fit in the HW range. This is
556 * to account for the possibility that the last command fit exactly into
557 * the ringbuffer and rb->next hasn't wrapped to zero yet
559 wptr = get_wptr(ring);
561 /* ensure writes to ringbuffer have hit system memory: */
564 gpu_write(gpu, reg, wptr);
567 bool adreno_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring)
569 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
570 uint32_t wptr = get_wptr(ring);
572 /* wait for CP to drain ringbuffer: */
573 if (!spin_until(get_rptr(adreno_gpu, ring) == wptr))
576 /* TODO maybe we need to reset GPU here to recover from hang? */
577 DRM_ERROR("%s: timeout waiting to drain ringbuffer %d rptr/wptr = %X/%X\n",
578 gpu->name, ring->id, get_rptr(adreno_gpu, ring), wptr);
583 int adreno_gpu_state_get(struct msm_gpu *gpu, struct msm_gpu_state *state)
585 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
588 WARN_ON(!mutex_is_locked(&gpu->lock));
590 kref_init(&state->ref);
592 ktime_get_real_ts64(&state->time);
594 for (i = 0; i < gpu->nr_rings; i++) {
597 state->ring[i].fence = gpu->rb[i]->memptrs->fence;
598 state->ring[i].iova = gpu->rb[i]->iova;
599 state->ring[i].seqno = gpu->rb[i]->fctx->last_fence;
600 state->ring[i].rptr = get_rptr(adreno_gpu, gpu->rb[i]);
601 state->ring[i].wptr = get_wptr(gpu->rb[i]);
603 /* Copy at least 'wptr' dwords of the data */
604 size = state->ring[i].wptr;
606 /* After wptr find the last non zero dword to save space */
607 for (j = state->ring[i].wptr; j < MSM_GPU_RINGBUFFER_SZ >> 2; j++)
608 if (gpu->rb[i]->start[j])
612 state->ring[i].data = kvmalloc(size << 2, GFP_KERNEL);
613 if (state->ring[i].data) {
614 memcpy(state->ring[i].data, gpu->rb[i]->start, size << 2);
615 state->ring[i].data_size = size << 2;
620 /* Some targets prefer to collect their own registers */
621 if (!adreno_gpu->registers)
624 /* Count the number of registers */
625 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2)
626 count += adreno_gpu->registers[i + 1] -
627 adreno_gpu->registers[i] + 1;
629 state->registers = kcalloc(count * 2, sizeof(u32), GFP_KERNEL);
630 if (state->registers) {
633 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
634 u32 start = adreno_gpu->registers[i];
635 u32 end = adreno_gpu->registers[i + 1];
638 for (addr = start; addr <= end; addr++) {
639 state->registers[pos++] = addr;
640 state->registers[pos++] = gpu_read(gpu, addr);
644 state->nr_registers = count;
650 void adreno_gpu_state_destroy(struct msm_gpu_state *state)
654 for (i = 0; i < ARRAY_SIZE(state->ring); i++)
655 kvfree(state->ring[i].data);
657 for (i = 0; state->bos && i < state->nr_bos; i++)
658 kvfree(state->bos[i].data);
663 kfree(state->registers);
666 static void adreno_gpu_state_kref_destroy(struct kref *kref)
668 struct msm_gpu_state *state = container_of(kref,
669 struct msm_gpu_state, ref);
671 adreno_gpu_state_destroy(state);
675 int adreno_gpu_state_put(struct msm_gpu_state *state)
677 if (IS_ERR_OR_NULL(state))
680 return kref_put(&state->ref, adreno_gpu_state_kref_destroy);
683 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
685 static char *adreno_gpu_ascii85_encode(u32 *src, size_t len)
688 size_t buf_itr = 0, buffer_size;
689 char out[ASCII85_BUFSZ];
696 l = ascii85_encode_len(len);
699 * Ascii85 outputs either a 5 byte string or a 1 byte string. So we
700 * account for the worst case of 5 bytes per dword plus the 1 for '\0'
702 buffer_size = (l * 5) + 1;
704 buf = kvmalloc(buffer_size, GFP_KERNEL);
708 for (i = 0; i < l; i++)
709 buf_itr += scnprintf(buf + buf_itr, buffer_size - buf_itr, "%s",
710 ascii85_encode(src[i], out));
715 /* len is expected to be in bytes */
716 void adreno_show_object(struct drm_printer *p, void **ptr, int len,
727 * Only dump the non-zero part of the buffer - rarely will
728 * any data completely fill the entire allocated size of
731 for (datalen = 0, i = 0; i < len >> 2; i++)
733 datalen = ((i + 1) << 2);
736 * If we reach here, then the originally captured binary buffer
737 * will be replaced with the ascii85 encoded string
739 *ptr = adreno_gpu_ascii85_encode(buf, datalen);
749 drm_puts(p, " data: !!ascii85 |\n");
757 void adreno_show(struct msm_gpu *gpu, struct msm_gpu_state *state,
758 struct drm_printer *p)
760 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
763 if (IS_ERR_OR_NULL(state))
766 drm_printf(p, "revision: %d (%d.%d.%d.%d)\n",
767 adreno_gpu->info->revn, adreno_gpu->rev.core,
768 adreno_gpu->rev.major, adreno_gpu->rev.minor,
769 adreno_gpu->rev.patchid);
771 * If this is state collected due to iova fault, so fault related info
773 * TTBR0 would not be zero, so this is a good way to distinguish
775 if (state->fault_info.ttbr0) {
776 const struct msm_gpu_fault_info *info = &state->fault_info;
778 drm_puts(p, "fault-info:\n");
779 drm_printf(p, " - ttbr0=%.16llx\n", info->ttbr0);
780 drm_printf(p, " - iova=%.16lx\n", info->iova);
781 drm_printf(p, " - dir=%s\n", info->flags & IOMMU_FAULT_WRITE ? "WRITE" : "READ");
782 drm_printf(p, " - type=%s\n", info->type);
783 drm_printf(p, " - source=%s\n", info->block);
786 drm_printf(p, "rbbm-status: 0x%08x\n", state->rbbm_status);
788 drm_puts(p, "ringbuffer:\n");
790 for (i = 0; i < gpu->nr_rings; i++) {
791 drm_printf(p, " - id: %d\n", i);
792 drm_printf(p, " iova: 0x%016llx\n", state->ring[i].iova);
793 drm_printf(p, " last-fence: %d\n", state->ring[i].seqno);
794 drm_printf(p, " retired-fence: %d\n", state->ring[i].fence);
795 drm_printf(p, " rptr: %d\n", state->ring[i].rptr);
796 drm_printf(p, " wptr: %d\n", state->ring[i].wptr);
797 drm_printf(p, " size: %d\n", MSM_GPU_RINGBUFFER_SZ);
799 adreno_show_object(p, &state->ring[i].data,
800 state->ring[i].data_size, &state->ring[i].encoded);
804 drm_puts(p, "bos:\n");
806 for (i = 0; i < state->nr_bos; i++) {
807 drm_printf(p, " - iova: 0x%016llx\n",
809 drm_printf(p, " size: %zd\n", state->bos[i].size);
811 adreno_show_object(p, &state->bos[i].data,
812 state->bos[i].size, &state->bos[i].encoded);
816 if (state->nr_registers) {
817 drm_puts(p, "registers:\n");
819 for (i = 0; i < state->nr_registers; i++) {
820 drm_printf(p, " - { offset: 0x%04x, value: 0x%08x }\n",
821 state->registers[i * 2] << 2,
822 state->registers[(i * 2) + 1]);
828 /* Dump common gpu status and scratch registers on any hang, to make
829 * the hangcheck logs more useful. The scratch registers seem always
830 * safe to read when GPU has hung (unlike some other regs, depending
831 * on how the GPU hung), and they are useful to match up to cmdstream
832 * dumps when debugging hangs:
834 void adreno_dump_info(struct msm_gpu *gpu)
836 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
839 printk("revision: %d (%d.%d.%d.%d)\n",
840 adreno_gpu->info->revn, adreno_gpu->rev.core,
841 adreno_gpu->rev.major, adreno_gpu->rev.minor,
842 adreno_gpu->rev.patchid);
844 for (i = 0; i < gpu->nr_rings; i++) {
845 struct msm_ringbuffer *ring = gpu->rb[i];
847 printk("rb %d: fence: %d/%d\n", i,
848 ring->memptrs->fence,
849 ring->fctx->last_fence);
851 printk("rptr: %d\n", get_rptr(adreno_gpu, ring));
852 printk("rb wptr: %d\n", get_wptr(ring));
856 /* would be nice to not have to duplicate the _show() stuff with printk(): */
857 void adreno_dump(struct msm_gpu *gpu)
859 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu);
862 if (!adreno_gpu->registers)
865 /* dump these out in a form that can be parsed by demsm: */
866 printk("IO:region %s 00000000 00020000\n", gpu->name);
867 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) {
868 uint32_t start = adreno_gpu->registers[i];
869 uint32_t end = adreno_gpu->registers[i+1];
872 for (addr = start; addr <= end; addr++) {
873 uint32_t val = gpu_read(gpu, addr);
874 printk("IO:R %08x %08x\n", addr<<2, val);
879 static uint32_t ring_freewords(struct msm_ringbuffer *ring)
881 struct adreno_gpu *adreno_gpu = to_adreno_gpu(ring->gpu);
882 uint32_t size = MSM_GPU_RINGBUFFER_SZ >> 2;
883 /* Use ring->next to calculate free size */
884 uint32_t wptr = ring->next - ring->start;
885 uint32_t rptr = get_rptr(adreno_gpu, ring);
886 return (rptr + (size - 1) - wptr) % size;
889 void adreno_wait_ring(struct msm_ringbuffer *ring, uint32_t ndwords)
891 if (spin_until(ring_freewords(ring) >= ndwords))
892 DRM_DEV_ERROR(ring->gpu->dev->dev,
893 "timeout waiting for space in ringbuffer %d\n",
897 /* Get legacy powerlevels from qcom,gpu-pwrlevels and populate the opp table */
898 static int adreno_get_legacy_pwrlevels(struct device *dev)
900 struct device_node *child, *node;
903 node = of_get_compatible_child(dev->of_node, "qcom,gpu-pwrlevels");
905 DRM_DEV_DEBUG(dev, "Could not find the GPU powerlevels\n");
909 for_each_child_of_node(node, child) {
912 ret = of_property_read_u32(child, "qcom,gpu-freq", &val);
917 * Skip the intentionally bogus clock value found at the bottom
918 * of most legacy frequency tables
921 dev_pm_opp_add(dev, val, 0);
929 static void adreno_get_pwrlevels(struct device *dev,
932 unsigned long freq = ULONG_MAX;
933 struct dev_pm_opp *opp;
938 /* You down with OPP? */
939 if (!of_find_property(dev->of_node, "operating-points-v2", NULL))
940 ret = adreno_get_legacy_pwrlevels(dev);
942 ret = devm_pm_opp_of_add_table(dev);
944 DRM_DEV_ERROR(dev, "Unable to set the OPP table\n");
948 /* Find the fastest defined rate */
949 opp = dev_pm_opp_find_freq_floor(dev, &freq);
951 gpu->fast_rate = freq;
956 if (!gpu->fast_rate) {
958 "Could not find a clock rate. Using a reasonable default\n");
959 /* Pick a suitably safe clock speed for any target */
960 gpu->fast_rate = 200000000;
963 DBG("fast_rate=%u, slow_rate=27000000", gpu->fast_rate);
966 int adreno_gpu_ocmem_init(struct device *dev, struct adreno_gpu *adreno_gpu,
967 struct adreno_ocmem *adreno_ocmem)
969 struct ocmem_buf *ocmem_hdl;
972 ocmem = of_get_ocmem(dev);
974 if (PTR_ERR(ocmem) == -ENODEV) {
976 * Return success since either the ocmem property was
977 * not specified in device tree, or ocmem support is
978 * not compiled into the kernel.
983 return PTR_ERR(ocmem);
986 ocmem_hdl = ocmem_allocate(ocmem, OCMEM_GRAPHICS, adreno_gpu->gmem);
987 if (IS_ERR(ocmem_hdl))
988 return PTR_ERR(ocmem_hdl);
990 adreno_ocmem->ocmem = ocmem;
991 adreno_ocmem->base = ocmem_hdl->addr;
992 adreno_ocmem->hdl = ocmem_hdl;
993 adreno_gpu->gmem = ocmem_hdl->len;
998 void adreno_gpu_ocmem_cleanup(struct adreno_ocmem *adreno_ocmem)
1000 if (adreno_ocmem && adreno_ocmem->base)
1001 ocmem_free(adreno_ocmem->ocmem, OCMEM_GRAPHICS,
1005 int adreno_read_speedbin(struct device *dev, u32 *speedbin)
1007 return nvmem_cell_read_variable_le_u32(dev, "speed_bin", speedbin);
1010 int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev,
1011 struct adreno_gpu *adreno_gpu,
1012 const struct adreno_gpu_funcs *funcs, int nr_rings)
1014 struct device *dev = &pdev->dev;
1015 struct adreno_platform_config *config = dev->platform_data;
1016 struct msm_gpu_config adreno_gpu_config = { 0 };
1017 struct msm_gpu *gpu = &adreno_gpu->base;
1018 struct adreno_rev *rev = &config->rev;
1019 const char *gpu_name;
1022 adreno_gpu->funcs = funcs;
1023 adreno_gpu->info = adreno_info(config->rev);
1024 adreno_gpu->gmem = adreno_gpu->info->gmem;
1025 adreno_gpu->revn = adreno_gpu->info->revn;
1026 adreno_gpu->rev = *rev;
1028 if (adreno_read_speedbin(dev, &speedbin) || !speedbin)
1030 adreno_gpu->speedbin = (uint16_t) (0xffff & speedbin);
1032 gpu_name = adreno_gpu->info->name;
1034 gpu_name = devm_kasprintf(dev, GFP_KERNEL, "%d.%d.%d.%d",
1035 rev->core, rev->major, rev->minor,
1041 adreno_gpu_config.ioname = "kgsl_3d0_reg_memory";
1043 adreno_gpu_config.nr_rings = nr_rings;
1045 adreno_get_pwrlevels(dev, gpu);
1047 pm_runtime_set_autosuspend_delay(dev,
1048 adreno_gpu->info->inactive_period);
1049 pm_runtime_use_autosuspend(dev);
1050 pm_runtime_enable(dev);
1052 return msm_gpu_init(drm, pdev, &adreno_gpu->base, &funcs->base,
1053 gpu_name, &adreno_gpu_config);
1056 void adreno_gpu_cleanup(struct adreno_gpu *adreno_gpu)
1058 struct msm_gpu *gpu = &adreno_gpu->base;
1059 struct msm_drm_private *priv = gpu->dev->dev_private;
1062 for (i = 0; i < ARRAY_SIZE(adreno_gpu->info->fw); i++)
1063 release_firmware(adreno_gpu->fw[i]);
1065 if (pm_runtime_enabled(&priv->gpu_pdev->dev))
1066 pm_runtime_disable(&priv->gpu_pdev->dev);
1068 msm_gpu_cleanup(&adreno_gpu->base);