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