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