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