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 <soc/qcom/ocmem.h>
18 #include "adreno_gpu.h"
23 static bool zap_available
= true;
25 static int zap_shader_load_mdt(struct msm_gpu
*gpu
, const char *fwname
,
28 struct device
*dev
= &gpu
->pdev
->dev
;
29 const struct firmware
*fw
;
30 const char *signed_fwname
= NULL
;
31 struct device_node
*np
, *mem_np
;
35 void *mem_region
= NULL
;
38 if (!IS_ENABLED(CONFIG_ARCH_QCOM
)) {
39 zap_available
= false;
43 np
= of_get_child_by_name(dev
->of_node
, "zap-shader");
45 zap_available
= false;
49 mem_np
= of_parse_phandle(np
, "memory-region", 0);
52 zap_available
= false;
56 ret
= of_address_to_resource(mem_np
, 0, &r
);
64 * Check for a firmware-name property. This is the new scheme
65 * to handle firmware that may be signed with device specific
66 * keys, allowing us to have a different zap fw path for different
69 * If the firmware-name property is found, we bypass the
70 * adreno_request_fw() mechanism, because we don't need to handle
71 * the /lib/firmware/qcom/... vs /lib/firmware/... case.
73 * If the firmware-name property is not found, for backwards
74 * compatibility we fall back to the fwname from the gpulist
77 of_property_read_string_index(np
, "firmware-name", 0, &signed_fwname
);
79 fwname
= signed_fwname
;
80 ret
= request_firmware_direct(&fw
, fwname
, gpu
->dev
->dev
);
84 /* Request the MDT file from the default location: */
85 fw
= adreno_request_fw(to_adreno_gpu(gpu
), fwname
);
88 * For new targets, we require the firmware-name property,
89 * if a zap-shader is required, rather than falling back
90 * to a firmware name specified in gpulist.
92 * Because the firmware is signed with a (potentially)
93 * device specific key, having the name come from gpulist
94 * was a bad idea, and is only provided for backwards
95 * compatibility for older targets.
101 DRM_DEV_ERROR(dev
, "Unable to load %s\n", fwname
);
105 /* Figure out how much memory we need */
106 mem_size
= qcom_mdt_get_size(fw
);
112 if (mem_size
> resource_size(&r
)) {
114 "memory region is too small to load the MDT\n");
119 /* Allocate memory for the firmware image */
120 mem_region
= memremap(mem_phys
, mem_size
, MEMREMAP_WC
);
127 * Load the rest of the MDT
129 * Note that we could be dealing with two different paths, since
130 * with upstream linux-firmware it would be in a qcom/ subdir..
131 * adreno_request_fw() handles this, but qcom_mdt_load() does
132 * not. But since we've already gotten through adreno_request_fw()
133 * we know which of the two cases it is:
135 if (signed_fwname
|| (to_adreno_gpu(gpu
)->fwloc
== FW_LOCATION_LEGACY
)) {
136 ret
= qcom_mdt_load(dev
, fw
, fwname
, pasid
,
137 mem_region
, mem_phys
, mem_size
, NULL
);
141 newname
= kasprintf(GFP_KERNEL
, "qcom/%s", fwname
);
143 ret
= qcom_mdt_load(dev
, fw
, newname
, pasid
,
144 mem_region
, mem_phys
, mem_size
, NULL
);
150 /* Send the image to the secure world */
151 ret
= qcom_scm_pas_auth_and_reset(pasid
);
154 * If the scm call returns -EOPNOTSUPP we assume that this target
155 * doesn't need/support the zap shader so quietly fail
157 if (ret
== -EOPNOTSUPP
)
158 zap_available
= false;
160 DRM_DEV_ERROR(dev
, "Unable to authorize the image\n");
164 memunmap(mem_region
);
166 release_firmware(fw
);
171 int adreno_zap_shader_load(struct msm_gpu
*gpu
, u32 pasid
)
173 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
174 struct platform_device
*pdev
= gpu
->pdev
;
176 /* Short cut if we determine the zap shader isn't available/needed */
180 /* We need SCM to be able to load the firmware */
181 if (!qcom_scm_is_available()) {
182 DRM_DEV_ERROR(&pdev
->dev
, "SCM is not available\n");
183 return -EPROBE_DEFER
;
186 return zap_shader_load_mdt(gpu
, adreno_gpu
->info
->zapfw
, pasid
);
189 struct msm_gem_address_space
*
190 adreno_iommu_create_address_space(struct msm_gpu
*gpu
,
191 struct platform_device
*pdev
)
193 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
194 struct iommu_domain
*iommu
;
196 struct msm_gem_address_space
*aspace
;
199 iommu
= iommu_domain_alloc(&platform_bus_type
);
204 if (adreno_is_a6xx(adreno_gpu
)) {
205 struct a6xx_gpu
*a6xx_gpu
= to_a6xx_gpu(adreno_gpu
);
206 struct io_pgtable_domain_attr pgtbl_cfg
;
208 * This allows GPU to set the bus attributes required to use system
209 * cache on behalf of the iommu page table walker.
211 if (!IS_ERR(a6xx_gpu
->htw_llc_slice
)) {
212 pgtbl_cfg
.quirks
= IO_PGTABLE_QUIRK_ARM_OUTER_WBWA
;
213 iommu_domain_set_attr(iommu
, DOMAIN_ATTR_IO_PGTABLE_CFG
, &pgtbl_cfg
);
217 mmu
= msm_iommu_new(&pdev
->dev
, iommu
);
219 iommu_domain_free(iommu
);
220 return ERR_CAST(mmu
);
224 * Use the aperture start or SZ_16M, whichever is greater. This will
225 * ensure that we align with the allocated pagetable range while still
226 * allowing room in the lower 32 bits for GMEM and whatnot
228 start
= max_t(u64
, SZ_16M
, iommu
->geometry
.aperture_start
);
229 size
= iommu
->geometry
.aperture_end
- start
+ 1;
231 aspace
= msm_gem_address_space_create(mmu
, "gpu",
232 start
& GENMASK_ULL(48, 0), size
);
234 if (IS_ERR(aspace
) && !IS_ERR(mmu
))
235 mmu
->funcs
->destroy(mmu
);
240 int adreno_get_param(struct msm_gpu
*gpu
, uint32_t param
, uint64_t *value
)
242 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
245 case MSM_PARAM_GPU_ID
:
246 *value
= adreno_gpu
->info
->revn
;
248 case MSM_PARAM_GMEM_SIZE
:
249 *value
= adreno_gpu
->gmem
;
251 case MSM_PARAM_GMEM_BASE
:
252 *value
= !adreno_is_a650(adreno_gpu
) ? 0x100000 : 0;
254 case MSM_PARAM_CHIP_ID
:
255 *value
= adreno_gpu
->rev
.patchid
|
256 (adreno_gpu
->rev
.minor
<< 8) |
257 (adreno_gpu
->rev
.major
<< 16) |
258 (adreno_gpu
->rev
.core
<< 24);
260 case MSM_PARAM_MAX_FREQ
:
261 *value
= adreno_gpu
->base
.fast_rate
;
263 case MSM_PARAM_TIMESTAMP
:
264 if (adreno_gpu
->funcs
->get_timestamp
) {
267 pm_runtime_get_sync(&gpu
->pdev
->dev
);
268 ret
= adreno_gpu
->funcs
->get_timestamp(gpu
, value
);
269 pm_runtime_put_autosuspend(&gpu
->pdev
->dev
);
274 case MSM_PARAM_NR_RINGS
:
275 *value
= gpu
->nr_rings
;
277 case MSM_PARAM_PP_PGTABLE
:
280 case MSM_PARAM_FAULTS
:
281 *value
= gpu
->global_faults
;
284 DBG("%s: invalid param: %u", gpu
->name
, param
);
289 const struct firmware
*
290 adreno_request_fw(struct adreno_gpu
*adreno_gpu
, const char *fwname
)
292 struct drm_device
*drm
= adreno_gpu
->base
.dev
;
293 const struct firmware
*fw
= NULL
;
297 newname
= kasprintf(GFP_KERNEL
, "qcom/%s", fwname
);
299 return ERR_PTR(-ENOMEM
);
302 * Try first to load from qcom/$fwfile using a direct load (to avoid
303 * a potential timeout waiting for usermode helper)
305 if ((adreno_gpu
->fwloc
== FW_LOCATION_UNKNOWN
) ||
306 (adreno_gpu
->fwloc
== FW_LOCATION_NEW
)) {
308 ret
= request_firmware_direct(&fw
, newname
, drm
->dev
);
310 DRM_DEV_INFO(drm
->dev
, "loaded %s from new location\n",
312 adreno_gpu
->fwloc
= FW_LOCATION_NEW
;
314 } else if (adreno_gpu
->fwloc
!= FW_LOCATION_UNKNOWN
) {
315 DRM_DEV_ERROR(drm
->dev
, "failed to load %s: %d\n",
323 * Then try the legacy location without qcom/ prefix
325 if ((adreno_gpu
->fwloc
== FW_LOCATION_UNKNOWN
) ||
326 (adreno_gpu
->fwloc
== FW_LOCATION_LEGACY
)) {
328 ret
= request_firmware_direct(&fw
, fwname
, drm
->dev
);
330 DRM_DEV_INFO(drm
->dev
, "loaded %s from legacy location\n",
332 adreno_gpu
->fwloc
= FW_LOCATION_LEGACY
;
334 } else if (adreno_gpu
->fwloc
!= FW_LOCATION_UNKNOWN
) {
335 DRM_DEV_ERROR(drm
->dev
, "failed to load %s: %d\n",
343 * Finally fall back to request_firmware() for cases where the
344 * usermode helper is needed (I think mainly android)
346 if ((adreno_gpu
->fwloc
== FW_LOCATION_UNKNOWN
) ||
347 (adreno_gpu
->fwloc
== FW_LOCATION_HELPER
)) {
349 ret
= request_firmware(&fw
, newname
, drm
->dev
);
351 DRM_DEV_INFO(drm
->dev
, "loaded %s with helper\n",
353 adreno_gpu
->fwloc
= FW_LOCATION_HELPER
;
355 } else if (adreno_gpu
->fwloc
!= FW_LOCATION_UNKNOWN
) {
356 DRM_DEV_ERROR(drm
->dev
, "failed to load %s: %d\n",
363 DRM_DEV_ERROR(drm
->dev
, "failed to load %s\n", fwname
);
364 fw
= ERR_PTR(-ENOENT
);
370 int adreno_load_fw(struct adreno_gpu
*adreno_gpu
)
374 for (i
= 0; i
< ARRAY_SIZE(adreno_gpu
->info
->fw
); i
++) {
375 const struct firmware
*fw
;
377 if (!adreno_gpu
->info
->fw
[i
])
380 /* Skip if the firmware has already been loaded */
381 if (adreno_gpu
->fw
[i
])
384 fw
= adreno_request_fw(adreno_gpu
, adreno_gpu
->info
->fw
[i
]);
388 adreno_gpu
->fw
[i
] = fw
;
394 struct drm_gem_object
*adreno_fw_create_bo(struct msm_gpu
*gpu
,
395 const struct firmware
*fw
, u64
*iova
)
397 struct drm_gem_object
*bo
;
400 ptr
= msm_gem_kernel_new_locked(gpu
->dev
, fw
->size
- 4,
401 MSM_BO_UNCACHED
| MSM_BO_GPU_READONLY
, gpu
->aspace
, &bo
, iova
);
404 return ERR_CAST(ptr
);
406 memcpy(ptr
, &fw
->data
[4], fw
->size
- 4);
408 msm_gem_put_vaddr(bo
);
413 int adreno_hw_init(struct msm_gpu
*gpu
)
415 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
418 DBG("%s", gpu
->name
);
420 ret
= adreno_load_fw(adreno_gpu
);
424 for (i
= 0; i
< gpu
->nr_rings
; i
++) {
425 struct msm_ringbuffer
*ring
= gpu
->rb
[i
];
430 ring
->cur
= ring
->start
;
431 ring
->next
= ring
->start
;
433 /* reset completed fence seqno: */
434 ring
->memptrs
->fence
= ring
->fctx
->completed_fence
;
435 ring
->memptrs
->rptr
= 0;
441 /* Use this helper to read rptr, since a430 doesn't update rptr in memory */
442 static uint32_t get_rptr(struct adreno_gpu
*adreno_gpu
,
443 struct msm_ringbuffer
*ring
)
445 struct msm_gpu
*gpu
= &adreno_gpu
->base
;
447 return gpu
->funcs
->get_rptr(gpu
, ring
);
450 struct msm_ringbuffer
*adreno_active_ring(struct msm_gpu
*gpu
)
455 void adreno_recover(struct msm_gpu
*gpu
)
457 struct drm_device
*dev
= gpu
->dev
;
460 // XXX pm-runtime?? we *need* the device to be off after this
461 // so maybe continuing to call ->pm_suspend/resume() is better?
463 gpu
->funcs
->pm_suspend(gpu
);
464 gpu
->funcs
->pm_resume(gpu
);
466 ret
= msm_gpu_hw_init(gpu
);
468 DRM_DEV_ERROR(dev
->dev
, "gpu hw init failed: %d\n", ret
);
473 void adreno_flush(struct msm_gpu
*gpu
, struct msm_ringbuffer
*ring
, u32 reg
)
477 /* Copy the shadow to the actual register */
478 ring
->cur
= ring
->next
;
481 * Mask wptr value that we calculate to fit in the HW range. This is
482 * to account for the possibility that the last command fit exactly into
483 * the ringbuffer and rb->next hasn't wrapped to zero yet
485 wptr
= get_wptr(ring
);
487 /* ensure writes to ringbuffer have hit system memory: */
490 gpu_write(gpu
, reg
, wptr
);
493 bool adreno_idle(struct msm_gpu
*gpu
, struct msm_ringbuffer
*ring
)
495 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
496 uint32_t wptr
= get_wptr(ring
);
498 /* wait for CP to drain ringbuffer: */
499 if (!spin_until(get_rptr(adreno_gpu
, ring
) == wptr
))
502 /* TODO maybe we need to reset GPU here to recover from hang? */
503 DRM_ERROR("%s: timeout waiting to drain ringbuffer %d rptr/wptr = %X/%X\n",
504 gpu
->name
, ring
->id
, get_rptr(adreno_gpu
, ring
), wptr
);
509 int adreno_gpu_state_get(struct msm_gpu
*gpu
, struct msm_gpu_state
*state
)
511 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
514 kref_init(&state
->ref
);
516 ktime_get_real_ts64(&state
->time
);
518 for (i
= 0; i
< gpu
->nr_rings
; i
++) {
521 state
->ring
[i
].fence
= gpu
->rb
[i
]->memptrs
->fence
;
522 state
->ring
[i
].iova
= gpu
->rb
[i
]->iova
;
523 state
->ring
[i
].seqno
= gpu
->rb
[i
]->seqno
;
524 state
->ring
[i
].rptr
= get_rptr(adreno_gpu
, gpu
->rb
[i
]);
525 state
->ring
[i
].wptr
= get_wptr(gpu
->rb
[i
]);
527 /* Copy at least 'wptr' dwords of the data */
528 size
= state
->ring
[i
].wptr
;
530 /* After wptr find the last non zero dword to save space */
531 for (j
= state
->ring
[i
].wptr
; j
< MSM_GPU_RINGBUFFER_SZ
>> 2; j
++)
532 if (gpu
->rb
[i
]->start
[j
])
536 state
->ring
[i
].data
= kvmalloc(size
<< 2, GFP_KERNEL
);
537 if (state
->ring
[i
].data
) {
538 memcpy(state
->ring
[i
].data
, gpu
->rb
[i
]->start
, size
<< 2);
539 state
->ring
[i
].data_size
= size
<< 2;
544 /* Some targets prefer to collect their own registers */
545 if (!adreno_gpu
->registers
)
548 /* Count the number of registers */
549 for (i
= 0; adreno_gpu
->registers
[i
] != ~0; i
+= 2)
550 count
+= adreno_gpu
->registers
[i
+ 1] -
551 adreno_gpu
->registers
[i
] + 1;
553 state
->registers
= kcalloc(count
* 2, sizeof(u32
), GFP_KERNEL
);
554 if (state
->registers
) {
557 for (i
= 0; adreno_gpu
->registers
[i
] != ~0; i
+= 2) {
558 u32 start
= adreno_gpu
->registers
[i
];
559 u32 end
= adreno_gpu
->registers
[i
+ 1];
562 for (addr
= start
; addr
<= end
; addr
++) {
563 state
->registers
[pos
++] = addr
;
564 state
->registers
[pos
++] = gpu_read(gpu
, addr
);
568 state
->nr_registers
= count
;
574 void adreno_gpu_state_destroy(struct msm_gpu_state
*state
)
578 for (i
= 0; i
< ARRAY_SIZE(state
->ring
); i
++)
579 kvfree(state
->ring
[i
].data
);
581 for (i
= 0; state
->bos
&& i
< state
->nr_bos
; i
++)
582 kvfree(state
->bos
[i
].data
);
587 kfree(state
->registers
);
590 static void adreno_gpu_state_kref_destroy(struct kref
*kref
)
592 struct msm_gpu_state
*state
= container_of(kref
,
593 struct msm_gpu_state
, ref
);
595 adreno_gpu_state_destroy(state
);
599 int adreno_gpu_state_put(struct msm_gpu_state
*state
)
601 if (IS_ERR_OR_NULL(state
))
604 return kref_put(&state
->ref
, adreno_gpu_state_kref_destroy
);
607 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP)
609 static char *adreno_gpu_ascii85_encode(u32
*src
, size_t len
)
612 size_t buf_itr
= 0, buffer_size
;
613 char out
[ASCII85_BUFSZ
];
620 l
= ascii85_encode_len(len
);
623 * Ascii85 outputs either a 5 byte string or a 1 byte string. So we
624 * account for the worst case of 5 bytes per dword plus the 1 for '\0'
626 buffer_size
= (l
* 5) + 1;
628 buf
= kvmalloc(buffer_size
, GFP_KERNEL
);
632 for (i
= 0; i
< l
; i
++)
633 buf_itr
+= scnprintf(buf
+ buf_itr
, buffer_size
- buf_itr
, "%s",
634 ascii85_encode(src
[i
], out
));
639 /* len is expected to be in bytes */
640 static void adreno_show_object(struct drm_printer
*p
, void **ptr
, int len
,
651 * Only dump the non-zero part of the buffer - rarely will
652 * any data completely fill the entire allocated size of
655 for (datalen
= 0, i
= 0; i
< len
>> 2; i
++)
657 datalen
= ((i
+ 1) << 2);
660 * If we reach here, then the originally captured binary buffer
661 * will be replaced with the ascii85 encoded string
663 *ptr
= adreno_gpu_ascii85_encode(buf
, datalen
);
673 drm_puts(p
, " data: !!ascii85 |\n");
681 void adreno_show(struct msm_gpu
*gpu
, struct msm_gpu_state
*state
,
682 struct drm_printer
*p
)
684 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
687 if (IS_ERR_OR_NULL(state
))
690 drm_printf(p
, "revision: %d (%d.%d.%d.%d)\n",
691 adreno_gpu
->info
->revn
, adreno_gpu
->rev
.core
,
692 adreno_gpu
->rev
.major
, adreno_gpu
->rev
.minor
,
693 adreno_gpu
->rev
.patchid
);
695 drm_printf(p
, "rbbm-status: 0x%08x\n", state
->rbbm_status
);
697 drm_puts(p
, "ringbuffer:\n");
699 for (i
= 0; i
< gpu
->nr_rings
; i
++) {
700 drm_printf(p
, " - id: %d\n", i
);
701 drm_printf(p
, " iova: 0x%016llx\n", state
->ring
[i
].iova
);
702 drm_printf(p
, " last-fence: %d\n", state
->ring
[i
].seqno
);
703 drm_printf(p
, " retired-fence: %d\n", state
->ring
[i
].fence
);
704 drm_printf(p
, " rptr: %d\n", state
->ring
[i
].rptr
);
705 drm_printf(p
, " wptr: %d\n", state
->ring
[i
].wptr
);
706 drm_printf(p
, " size: %d\n", MSM_GPU_RINGBUFFER_SZ
);
708 adreno_show_object(p
, &state
->ring
[i
].data
,
709 state
->ring
[i
].data_size
, &state
->ring
[i
].encoded
);
713 drm_puts(p
, "bos:\n");
715 for (i
= 0; i
< state
->nr_bos
; i
++) {
716 drm_printf(p
, " - iova: 0x%016llx\n",
718 drm_printf(p
, " size: %zd\n", state
->bos
[i
].size
);
720 adreno_show_object(p
, &state
->bos
[i
].data
,
721 state
->bos
[i
].size
, &state
->bos
[i
].encoded
);
725 if (state
->nr_registers
) {
726 drm_puts(p
, "registers:\n");
728 for (i
= 0; i
< state
->nr_registers
; i
++) {
729 drm_printf(p
, " - { offset: 0x%04x, value: 0x%08x }\n",
730 state
->registers
[i
* 2] << 2,
731 state
->registers
[(i
* 2) + 1]);
737 /* Dump common gpu status and scratch registers on any hang, to make
738 * the hangcheck logs more useful. The scratch registers seem always
739 * safe to read when GPU has hung (unlike some other regs, depending
740 * on how the GPU hung), and they are useful to match up to cmdstream
741 * dumps when debugging hangs:
743 void adreno_dump_info(struct msm_gpu
*gpu
)
745 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
748 printk("revision: %d (%d.%d.%d.%d)\n",
749 adreno_gpu
->info
->revn
, adreno_gpu
->rev
.core
,
750 adreno_gpu
->rev
.major
, adreno_gpu
->rev
.minor
,
751 adreno_gpu
->rev
.patchid
);
753 for (i
= 0; i
< gpu
->nr_rings
; i
++) {
754 struct msm_ringbuffer
*ring
= gpu
->rb
[i
];
756 printk("rb %d: fence: %d/%d\n", i
,
757 ring
->memptrs
->fence
,
760 printk("rptr: %d\n", get_rptr(adreno_gpu
, ring
));
761 printk("rb wptr: %d\n", get_wptr(ring
));
765 /* would be nice to not have to duplicate the _show() stuff with printk(): */
766 void adreno_dump(struct msm_gpu
*gpu
)
768 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(gpu
);
771 if (!adreno_gpu
->registers
)
774 /* dump these out in a form that can be parsed by demsm: */
775 printk("IO:region %s 00000000 00020000\n", gpu
->name
);
776 for (i
= 0; adreno_gpu
->registers
[i
] != ~0; i
+= 2) {
777 uint32_t start
= adreno_gpu
->registers
[i
];
778 uint32_t end
= adreno_gpu
->registers
[i
+1];
781 for (addr
= start
; addr
<= end
; addr
++) {
782 uint32_t val
= gpu_read(gpu
, addr
);
783 printk("IO:R %08x %08x\n", addr
<<2, val
);
788 static uint32_t ring_freewords(struct msm_ringbuffer
*ring
)
790 struct adreno_gpu
*adreno_gpu
= to_adreno_gpu(ring
->gpu
);
791 uint32_t size
= MSM_GPU_RINGBUFFER_SZ
>> 2;
792 /* Use ring->next to calculate free size */
793 uint32_t wptr
= ring
->next
- ring
->start
;
794 uint32_t rptr
= get_rptr(adreno_gpu
, ring
);
795 return (rptr
+ (size
- 1) - wptr
) % size
;
798 void adreno_wait_ring(struct msm_ringbuffer
*ring
, uint32_t ndwords
)
800 if (spin_until(ring_freewords(ring
) >= ndwords
))
801 DRM_DEV_ERROR(ring
->gpu
->dev
->dev
,
802 "timeout waiting for space in ringbuffer %d\n",
806 /* Get legacy powerlevels from qcom,gpu-pwrlevels and populate the opp table */
807 static int adreno_get_legacy_pwrlevels(struct device
*dev
)
809 struct device_node
*child
, *node
;
812 node
= of_get_compatible_child(dev
->of_node
, "qcom,gpu-pwrlevels");
814 DRM_DEV_DEBUG(dev
, "Could not find the GPU powerlevels\n");
818 for_each_child_of_node(node
, child
) {
821 ret
= of_property_read_u32(child
, "qcom,gpu-freq", &val
);
826 * Skip the intentionally bogus clock value found at the bottom
827 * of most legacy frequency tables
830 dev_pm_opp_add(dev
, val
, 0);
838 static void adreno_get_pwrlevels(struct device
*dev
,
841 unsigned long freq
= ULONG_MAX
;
842 struct dev_pm_opp
*opp
;
847 /* You down with OPP? */
848 if (!of_find_property(dev
->of_node
, "operating-points-v2", NULL
))
849 ret
= adreno_get_legacy_pwrlevels(dev
);
851 ret
= dev_pm_opp_of_add_table(dev
);
853 DRM_DEV_ERROR(dev
, "Unable to set the OPP table\n");
857 /* Find the fastest defined rate */
858 opp
= dev_pm_opp_find_freq_floor(dev
, &freq
);
860 gpu
->fast_rate
= freq
;
865 if (!gpu
->fast_rate
) {
867 "Could not find a clock rate. Using a reasonable default\n");
868 /* Pick a suitably safe clock speed for any target */
869 gpu
->fast_rate
= 200000000;
872 DBG("fast_rate=%u, slow_rate=27000000", gpu
->fast_rate
);
875 int adreno_gpu_ocmem_init(struct device
*dev
, struct adreno_gpu
*adreno_gpu
,
876 struct adreno_ocmem
*adreno_ocmem
)
878 struct ocmem_buf
*ocmem_hdl
;
881 ocmem
= of_get_ocmem(dev
);
883 if (PTR_ERR(ocmem
) == -ENODEV
) {
885 * Return success since either the ocmem property was
886 * not specified in device tree, or ocmem support is
887 * not compiled into the kernel.
892 return PTR_ERR(ocmem
);
895 ocmem_hdl
= ocmem_allocate(ocmem
, OCMEM_GRAPHICS
, adreno_gpu
->gmem
);
896 if (IS_ERR(ocmem_hdl
))
897 return PTR_ERR(ocmem_hdl
);
899 adreno_ocmem
->ocmem
= ocmem
;
900 adreno_ocmem
->base
= ocmem_hdl
->addr
;
901 adreno_ocmem
->hdl
= ocmem_hdl
;
902 adreno_gpu
->gmem
= ocmem_hdl
->len
;
907 void adreno_gpu_ocmem_cleanup(struct adreno_ocmem
*adreno_ocmem
)
909 if (adreno_ocmem
&& adreno_ocmem
->base
)
910 ocmem_free(adreno_ocmem
->ocmem
, OCMEM_GRAPHICS
,
914 int adreno_gpu_init(struct drm_device
*drm
, struct platform_device
*pdev
,
915 struct adreno_gpu
*adreno_gpu
,
916 const struct adreno_gpu_funcs
*funcs
, int nr_rings
)
918 struct device
*dev
= &pdev
->dev
;
919 struct adreno_platform_config
*config
= dev
->platform_data
;
920 struct msm_gpu_config adreno_gpu_config
= { 0 };
921 struct msm_gpu
*gpu
= &adreno_gpu
->base
;
923 adreno_gpu
->funcs
= funcs
;
924 adreno_gpu
->info
= adreno_info(config
->rev
);
925 adreno_gpu
->gmem
= adreno_gpu
->info
->gmem
;
926 adreno_gpu
->revn
= adreno_gpu
->info
->revn
;
927 adreno_gpu
->rev
= config
->rev
;
929 adreno_gpu_config
.ioname
= "kgsl_3d0_reg_memory";
931 adreno_gpu_config
.nr_rings
= nr_rings
;
933 adreno_get_pwrlevels(dev
, gpu
);
935 pm_runtime_set_autosuspend_delay(dev
,
936 adreno_gpu
->info
->inactive_period
);
937 pm_runtime_use_autosuspend(dev
);
938 pm_runtime_enable(dev
);
940 return msm_gpu_init(drm
, pdev
, &adreno_gpu
->base
, &funcs
->base
,
941 adreno_gpu
->info
->name
, &adreno_gpu_config
);
944 void adreno_gpu_cleanup(struct adreno_gpu
*adreno_gpu
)
946 struct msm_gpu
*gpu
= &adreno_gpu
->base
;
947 struct msm_drm_private
*priv
= gpu
->dev
->dev_private
;
950 for (i
= 0; i
< ARRAY_SIZE(adreno_gpu
->info
->fw
); i
++)
951 release_firmware(adreno_gpu
->fw
[i
]);
953 pm_runtime_disable(&priv
->gpu_pdev
->dev
);
955 msm_gpu_cleanup(&adreno_gpu
->base
);
957 icc_put(gpu
->icc_path
);
958 icc_put(gpu
->ocmem_icc_path
);