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