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