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