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/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 bool zap_available = true; 25 26 static int zap_shader_load_mdt(struct msm_gpu *gpu, const char *fwname, 27 u32 pasid) 28 { 29 struct device *dev = &gpu->pdev->dev; 30 const struct firmware *fw; 31 const char *signed_fwname = NULL; 32 struct device_node *np, *mem_np; 33 struct resource r; 34 phys_addr_t mem_phys; 35 ssize_t mem_size; 36 void *mem_region = NULL; 37 int ret; 38 39 if (!IS_ENABLED(CONFIG_ARCH_QCOM)) { 40 zap_available = false; 41 return -EINVAL; 42 } 43 44 np = of_get_child_by_name(dev->of_node, "zap-shader"); 45 if (!np) { 46 zap_available = false; 47 return -ENODEV; 48 } 49 50 mem_np = of_parse_phandle(np, "memory-region", 0); 51 of_node_put(np); 52 if (!mem_np) { 53 zap_available = false; 54 return -EINVAL; 55 } 56 57 ret = of_address_to_resource(mem_np, 0, &r); 58 of_node_put(mem_np); 59 if (ret) 60 return ret; 61 62 mem_phys = r.start; 63 64 /* 65 * Check for a firmware-name property. This is the new scheme 66 * to handle firmware that may be signed with device specific 67 * keys, allowing us to have a different zap fw path for different 68 * devices. 69 * 70 * If the firmware-name property is found, we bypass the 71 * adreno_request_fw() mechanism, because we don't need to handle 72 * the /lib/firmware/qcom/... vs /lib/firmware/... case. 73 * 74 * If the firmware-name property is not found, for backwards 75 * compatibility we fall back to the fwname from the gpulist 76 * table. 77 */ 78 of_property_read_string_index(np, "firmware-name", 0, &signed_fwname); 79 if (signed_fwname) { 80 fwname = signed_fwname; 81 ret = request_firmware_direct(&fw, fwname, gpu->dev->dev); 82 if (ret) 83 fw = ERR_PTR(ret); 84 } else if (fwname) { 85 /* Request the MDT file from the default location: */ 86 fw = adreno_request_fw(to_adreno_gpu(gpu), fwname); 87 } else { 88 /* 89 * For new targets, we require the firmware-name property, 90 * if a zap-shader is required, rather than falling back 91 * to a firmware name specified in gpulist. 92 * 93 * Because the firmware is signed with a (potentially) 94 * device specific key, having the name come from gpulist 95 * was a bad idea, and is only provided for backwards 96 * compatibility for older targets. 97 */ 98 return -ENODEV; 99 } 100 101 if (IS_ERR(fw)) { 102 DRM_DEV_ERROR(dev, "Unable to load %s\n", fwname); 103 return PTR_ERR(fw); 104 } 105 106 /* Figure out how much memory we need */ 107 mem_size = qcom_mdt_get_size(fw); 108 if (mem_size < 0) { 109 ret = mem_size; 110 goto out; 111 } 112 113 if (mem_size > resource_size(&r)) { 114 DRM_DEV_ERROR(dev, 115 "memory region is too small to load the MDT\n"); 116 ret = -E2BIG; 117 goto out; 118 } 119 120 /* Allocate memory for the firmware image */ 121 mem_region = memremap(mem_phys, mem_size, MEMREMAP_WC); 122 if (!mem_region) { 123 ret = -ENOMEM; 124 goto out; 125 } 126 127 /* 128 * Load the rest of the MDT 129 * 130 * Note that we could be dealing with two different paths, since 131 * with upstream linux-firmware it would be in a qcom/ subdir.. 132 * adreno_request_fw() handles this, but qcom_mdt_load() does 133 * not. But since we've already gotten through adreno_request_fw() 134 * we know which of the two cases it is: 135 */ 136 if (signed_fwname || (to_adreno_gpu(gpu)->fwloc == FW_LOCATION_LEGACY)) { 137 ret = qcom_mdt_load(dev, fw, fwname, pasid, 138 mem_region, mem_phys, mem_size, NULL); 139 } else { 140 char *newname; 141 142 newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname); 143 144 ret = qcom_mdt_load(dev, fw, newname, pasid, 145 mem_region, mem_phys, mem_size, NULL); 146 kfree(newname); 147 } 148 if (ret) 149 goto out; 150 151 /* Send the image to the secure world */ 152 ret = qcom_scm_pas_auth_and_reset(pasid); 153 154 /* 155 * If the scm call returns -EOPNOTSUPP we assume that this target 156 * doesn't need/support the zap shader so quietly fail 157 */ 158 if (ret == -EOPNOTSUPP) 159 zap_available = false; 160 else if (ret) 161 DRM_DEV_ERROR(dev, "Unable to authorize the image\n"); 162 163 out: 164 if (mem_region) 165 memunmap(mem_region); 166 167 release_firmware(fw); 168 169 return ret; 170 } 171 172 int adreno_zap_shader_load(struct msm_gpu *gpu, u32 pasid) 173 { 174 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 175 struct platform_device *pdev = gpu->pdev; 176 177 /* Short cut if we determine the zap shader isn't available/needed */ 178 if (!zap_available) 179 return -ENODEV; 180 181 /* We need SCM to be able to load the firmware */ 182 if (!qcom_scm_is_available()) { 183 DRM_DEV_ERROR(&pdev->dev, "SCM is not available\n"); 184 return -EPROBE_DEFER; 185 } 186 187 return zap_shader_load_mdt(gpu, adreno_gpu->info->zapfw, pasid); 188 } 189 190 void adreno_set_llc_attributes(struct iommu_domain *iommu) 191 { 192 iommu_set_pgtable_quirks(iommu, IO_PGTABLE_QUIRK_ARM_OUTER_WBWA); 193 } 194 195 struct msm_gem_address_space * 196 adreno_iommu_create_address_space(struct msm_gpu *gpu, 197 struct platform_device *pdev) 198 { 199 struct iommu_domain *iommu; 200 struct msm_mmu *mmu; 201 struct msm_gem_address_space *aspace; 202 u64 start, size; 203 204 iommu = iommu_domain_alloc(&platform_bus_type); 205 if (!iommu) 206 return NULL; 207 208 mmu = msm_iommu_new(&pdev->dev, iommu); 209 if (IS_ERR(mmu)) { 210 iommu_domain_free(iommu); 211 return ERR_CAST(mmu); 212 } 213 214 /* 215 * Use the aperture start or SZ_16M, whichever is greater. This will 216 * ensure that we align with the allocated pagetable range while still 217 * allowing room in the lower 32 bits for GMEM and whatnot 218 */ 219 start = max_t(u64, SZ_16M, iommu->geometry.aperture_start); 220 size = iommu->geometry.aperture_end - start + 1; 221 222 aspace = msm_gem_address_space_create(mmu, "gpu", 223 start & GENMASK_ULL(48, 0), size); 224 225 if (IS_ERR(aspace) && !IS_ERR(mmu)) 226 mmu->funcs->destroy(mmu); 227 228 return aspace; 229 } 230 231 int adreno_get_param(struct msm_gpu *gpu, struct msm_file_private *ctx, 232 uint32_t param, uint64_t *value, uint32_t *len) 233 { 234 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 235 236 /* No pointer params yet */ 237 if (*len != 0) 238 return -EINVAL; 239 240 switch (param) { 241 case MSM_PARAM_GPU_ID: 242 *value = adreno_gpu->info->revn; 243 return 0; 244 case MSM_PARAM_GMEM_SIZE: 245 *value = adreno_gpu->gmem; 246 return 0; 247 case MSM_PARAM_GMEM_BASE: 248 *value = !adreno_is_a650_family(adreno_gpu) ? 0x100000 : 0; 249 return 0; 250 case MSM_PARAM_CHIP_ID: 251 *value = (uint64_t)adreno_gpu->rev.patchid | 252 ((uint64_t)adreno_gpu->rev.minor << 8) | 253 ((uint64_t)adreno_gpu->rev.major << 16) | 254 ((uint64_t)adreno_gpu->rev.core << 24); 255 if (!adreno_gpu->info->revn) 256 *value |= ((uint64_t) adreno_gpu->speedbin) << 32; 257 return 0; 258 case MSM_PARAM_MAX_FREQ: 259 *value = adreno_gpu->base.fast_rate; 260 return 0; 261 case MSM_PARAM_TIMESTAMP: 262 if (adreno_gpu->funcs->get_timestamp) { 263 int ret; 264 265 pm_runtime_get_sync(&gpu->pdev->dev); 266 ret = adreno_gpu->funcs->get_timestamp(gpu, value); 267 pm_runtime_put_autosuspend(&gpu->pdev->dev); 268 269 return ret; 270 } 271 return -EINVAL; 272 case MSM_PARAM_PRIORITIES: 273 *value = gpu->nr_rings * NR_SCHED_PRIORITIES; 274 return 0; 275 case MSM_PARAM_PP_PGTABLE: 276 *value = 0; 277 return 0; 278 case MSM_PARAM_FAULTS: 279 if (ctx->aspace) 280 *value = gpu->global_faults + ctx->aspace->faults; 281 else 282 *value = gpu->global_faults; 283 return 0; 284 case MSM_PARAM_SUSPENDS: 285 *value = gpu->suspend_count; 286 return 0; 287 case MSM_PARAM_VA_START: 288 if (ctx->aspace == gpu->aspace) 289 return -EINVAL; 290 *value = ctx->aspace->va_start; 291 return 0; 292 case MSM_PARAM_VA_SIZE: 293 if (ctx->aspace == gpu->aspace) 294 return -EINVAL; 295 *value = ctx->aspace->va_size; 296 return 0; 297 default: 298 DBG("%s: invalid param: %u", gpu->name, param); 299 return -EINVAL; 300 } 301 } 302 303 int adreno_set_param(struct msm_gpu *gpu, struct msm_file_private *ctx, 304 uint32_t param, uint64_t value, uint32_t len) 305 { 306 switch (param) { 307 case MSM_PARAM_COMM: 308 case MSM_PARAM_CMDLINE: 309 /* kstrdup_quotable_cmdline() limits to PAGE_SIZE, so 310 * that should be a reasonable upper bound 311 */ 312 if (len > PAGE_SIZE) 313 return -EINVAL; 314 break; 315 default: 316 if (len != 0) 317 return -EINVAL; 318 } 319 320 switch (param) { 321 case MSM_PARAM_COMM: 322 case MSM_PARAM_CMDLINE: { 323 char *str, **paramp; 324 325 str = kmalloc(len + 1, GFP_KERNEL); 326 if (!str) 327 return -ENOMEM; 328 329 if (copy_from_user(str, u64_to_user_ptr(value), len)) { 330 kfree(str); 331 return -EFAULT; 332 } 333 334 /* Ensure string is null terminated: */ 335 str[len] = '\0'; 336 337 if (param == MSM_PARAM_COMM) { 338 paramp = &ctx->comm; 339 } else { 340 paramp = &ctx->cmdline; 341 } 342 343 kfree(*paramp); 344 *paramp = str; 345 346 return 0; 347 } 348 case MSM_PARAM_SYSPROF: 349 if (!capable(CAP_SYS_ADMIN)) 350 return -EPERM; 351 return msm_file_private_set_sysprof(ctx, gpu, value); 352 default: 353 DBG("%s: invalid param: %u", gpu->name, param); 354 return -EINVAL; 355 } 356 } 357 358 const struct firmware * 359 adreno_request_fw(struct adreno_gpu *adreno_gpu, const char *fwname) 360 { 361 struct drm_device *drm = adreno_gpu->base.dev; 362 const struct firmware *fw = NULL; 363 char *newname; 364 int ret; 365 366 newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname); 367 if (!newname) 368 return ERR_PTR(-ENOMEM); 369 370 /* 371 * Try first to load from qcom/$fwfile using a direct load (to avoid 372 * a potential timeout waiting for usermode helper) 373 */ 374 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) || 375 (adreno_gpu->fwloc == FW_LOCATION_NEW)) { 376 377 ret = request_firmware_direct(&fw, newname, drm->dev); 378 if (!ret) { 379 DRM_DEV_INFO(drm->dev, "loaded %s from new location\n", 380 newname); 381 adreno_gpu->fwloc = FW_LOCATION_NEW; 382 goto out; 383 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) { 384 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n", 385 newname, ret); 386 fw = ERR_PTR(ret); 387 goto out; 388 } 389 } 390 391 /* 392 * Then try the legacy location without qcom/ prefix 393 */ 394 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) || 395 (adreno_gpu->fwloc == FW_LOCATION_LEGACY)) { 396 397 ret = request_firmware_direct(&fw, fwname, drm->dev); 398 if (!ret) { 399 DRM_DEV_INFO(drm->dev, "loaded %s from legacy location\n", 400 newname); 401 adreno_gpu->fwloc = FW_LOCATION_LEGACY; 402 goto out; 403 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) { 404 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n", 405 fwname, ret); 406 fw = ERR_PTR(ret); 407 goto out; 408 } 409 } 410 411 /* 412 * Finally fall back to request_firmware() for cases where the 413 * usermode helper is needed (I think mainly android) 414 */ 415 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) || 416 (adreno_gpu->fwloc == FW_LOCATION_HELPER)) { 417 418 ret = request_firmware(&fw, newname, drm->dev); 419 if (!ret) { 420 DRM_DEV_INFO(drm->dev, "loaded %s with helper\n", 421 newname); 422 adreno_gpu->fwloc = FW_LOCATION_HELPER; 423 goto out; 424 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) { 425 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n", 426 newname, ret); 427 fw = ERR_PTR(ret); 428 goto out; 429 } 430 } 431 432 DRM_DEV_ERROR(drm->dev, "failed to load %s\n", fwname); 433 fw = ERR_PTR(-ENOENT); 434 out: 435 kfree(newname); 436 return fw; 437 } 438 439 int adreno_load_fw(struct adreno_gpu *adreno_gpu) 440 { 441 int i; 442 443 for (i = 0; i < ARRAY_SIZE(adreno_gpu->info->fw); i++) { 444 const struct firmware *fw; 445 446 if (!adreno_gpu->info->fw[i]) 447 continue; 448 449 /* Skip if the firmware has already been loaded */ 450 if (adreno_gpu->fw[i]) 451 continue; 452 453 fw = adreno_request_fw(adreno_gpu, adreno_gpu->info->fw[i]); 454 if (IS_ERR(fw)) 455 return PTR_ERR(fw); 456 457 adreno_gpu->fw[i] = fw; 458 } 459 460 return 0; 461 } 462 463 struct drm_gem_object *adreno_fw_create_bo(struct msm_gpu *gpu, 464 const struct firmware *fw, u64 *iova) 465 { 466 struct drm_gem_object *bo; 467 void *ptr; 468 469 ptr = msm_gem_kernel_new(gpu->dev, fw->size - 4, 470 MSM_BO_WC | MSM_BO_GPU_READONLY, gpu->aspace, &bo, iova); 471 472 if (IS_ERR(ptr)) 473 return ERR_CAST(ptr); 474 475 memcpy(ptr, &fw->data[4], fw->size - 4); 476 477 msm_gem_put_vaddr(bo); 478 479 return bo; 480 } 481 482 int adreno_hw_init(struct msm_gpu *gpu) 483 { 484 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 485 int ret, i; 486 487 VERB("%s", gpu->name); 488 489 ret = adreno_load_fw(adreno_gpu); 490 if (ret) 491 return ret; 492 493 for (i = 0; i < gpu->nr_rings; i++) { 494 struct msm_ringbuffer *ring = gpu->rb[i]; 495 496 if (!ring) 497 continue; 498 499 ring->cur = ring->start; 500 ring->next = ring->start; 501 502 /* reset completed fence seqno: */ 503 ring->memptrs->fence = ring->fctx->completed_fence; 504 ring->memptrs->rptr = 0; 505 } 506 507 return 0; 508 } 509 510 /* Use this helper to read rptr, since a430 doesn't update rptr in memory */ 511 static uint32_t get_rptr(struct adreno_gpu *adreno_gpu, 512 struct msm_ringbuffer *ring) 513 { 514 struct msm_gpu *gpu = &adreno_gpu->base; 515 516 return gpu->funcs->get_rptr(gpu, ring); 517 } 518 519 struct msm_ringbuffer *adreno_active_ring(struct msm_gpu *gpu) 520 { 521 return gpu->rb[0]; 522 } 523 524 void adreno_recover(struct msm_gpu *gpu) 525 { 526 struct drm_device *dev = gpu->dev; 527 int ret; 528 529 // XXX pm-runtime?? we *need* the device to be off after this 530 // so maybe continuing to call ->pm_suspend/resume() is better? 531 532 gpu->funcs->pm_suspend(gpu); 533 gpu->funcs->pm_resume(gpu); 534 535 ret = msm_gpu_hw_init(gpu); 536 if (ret) { 537 DRM_DEV_ERROR(dev->dev, "gpu hw init failed: %d\n", ret); 538 /* hmm, oh well? */ 539 } 540 } 541 542 void adreno_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring, u32 reg) 543 { 544 uint32_t wptr; 545 546 /* Copy the shadow to the actual register */ 547 ring->cur = ring->next; 548 549 /* 550 * Mask wptr value that we calculate to fit in the HW range. This is 551 * to account for the possibility that the last command fit exactly into 552 * the ringbuffer and rb->next hasn't wrapped to zero yet 553 */ 554 wptr = get_wptr(ring); 555 556 /* ensure writes to ringbuffer have hit system memory: */ 557 mb(); 558 559 gpu_write(gpu, reg, wptr); 560 } 561 562 bool adreno_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring) 563 { 564 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 565 uint32_t wptr = get_wptr(ring); 566 567 /* wait for CP to drain ringbuffer: */ 568 if (!spin_until(get_rptr(adreno_gpu, ring) == wptr)) 569 return true; 570 571 /* TODO maybe we need to reset GPU here to recover from hang? */ 572 DRM_ERROR("%s: timeout waiting to drain ringbuffer %d rptr/wptr = %X/%X\n", 573 gpu->name, ring->id, get_rptr(adreno_gpu, ring), wptr); 574 575 return false; 576 } 577 578 int adreno_gpu_state_get(struct msm_gpu *gpu, struct msm_gpu_state *state) 579 { 580 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 581 int i, count = 0; 582 583 WARN_ON(!mutex_is_locked(&gpu->lock)); 584 585 kref_init(&state->ref); 586 587 ktime_get_real_ts64(&state->time); 588 589 for (i = 0; i < gpu->nr_rings; i++) { 590 int size = 0, j; 591 592 state->ring[i].fence = gpu->rb[i]->memptrs->fence; 593 state->ring[i].iova = gpu->rb[i]->iova; 594 state->ring[i].seqno = gpu->rb[i]->fctx->last_fence; 595 state->ring[i].rptr = get_rptr(adreno_gpu, gpu->rb[i]); 596 state->ring[i].wptr = get_wptr(gpu->rb[i]); 597 598 /* Copy at least 'wptr' dwords of the data */ 599 size = state->ring[i].wptr; 600 601 /* After wptr find the last non zero dword to save space */ 602 for (j = state->ring[i].wptr; j < MSM_GPU_RINGBUFFER_SZ >> 2; j++) 603 if (gpu->rb[i]->start[j]) 604 size = j + 1; 605 606 if (size) { 607 state->ring[i].data = kvmalloc(size << 2, GFP_KERNEL); 608 if (state->ring[i].data) { 609 memcpy(state->ring[i].data, gpu->rb[i]->start, size << 2); 610 state->ring[i].data_size = size << 2; 611 } 612 } 613 } 614 615 /* Some targets prefer to collect their own registers */ 616 if (!adreno_gpu->registers) 617 return 0; 618 619 /* Count the number of registers */ 620 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) 621 count += adreno_gpu->registers[i + 1] - 622 adreno_gpu->registers[i] + 1; 623 624 state->registers = kcalloc(count * 2, sizeof(u32), GFP_KERNEL); 625 if (state->registers) { 626 int pos = 0; 627 628 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) { 629 u32 start = adreno_gpu->registers[i]; 630 u32 end = adreno_gpu->registers[i + 1]; 631 u32 addr; 632 633 for (addr = start; addr <= end; addr++) { 634 state->registers[pos++] = addr; 635 state->registers[pos++] = gpu_read(gpu, addr); 636 } 637 } 638 639 state->nr_registers = count; 640 } 641 642 return 0; 643 } 644 645 void adreno_gpu_state_destroy(struct msm_gpu_state *state) 646 { 647 int i; 648 649 for (i = 0; i < ARRAY_SIZE(state->ring); i++) 650 kvfree(state->ring[i].data); 651 652 for (i = 0; state->bos && i < state->nr_bos; i++) 653 kvfree(state->bos[i].data); 654 655 kfree(state->bos); 656 kfree(state->comm); 657 kfree(state->cmd); 658 kfree(state->registers); 659 } 660 661 static void adreno_gpu_state_kref_destroy(struct kref *kref) 662 { 663 struct msm_gpu_state *state = container_of(kref, 664 struct msm_gpu_state, ref); 665 666 adreno_gpu_state_destroy(state); 667 kfree(state); 668 } 669 670 int adreno_gpu_state_put(struct msm_gpu_state *state) 671 { 672 if (IS_ERR_OR_NULL(state)) 673 return 1; 674 675 return kref_put(&state->ref, adreno_gpu_state_kref_destroy); 676 } 677 678 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP) 679 680 static char *adreno_gpu_ascii85_encode(u32 *src, size_t len) 681 { 682 void *buf; 683 size_t buf_itr = 0, buffer_size; 684 char out[ASCII85_BUFSZ]; 685 long l; 686 int i; 687 688 if (!src || !len) 689 return NULL; 690 691 l = ascii85_encode_len(len); 692 693 /* 694 * Ascii85 outputs either a 5 byte string or a 1 byte string. So we 695 * account for the worst case of 5 bytes per dword plus the 1 for '\0' 696 */ 697 buffer_size = (l * 5) + 1; 698 699 buf = kvmalloc(buffer_size, GFP_KERNEL); 700 if (!buf) 701 return NULL; 702 703 for (i = 0; i < l; i++) 704 buf_itr += scnprintf(buf + buf_itr, buffer_size - buf_itr, "%s", 705 ascii85_encode(src[i], out)); 706 707 return buf; 708 } 709 710 /* len is expected to be in bytes */ 711 void adreno_show_object(struct drm_printer *p, void **ptr, int len, 712 bool *encoded) 713 { 714 if (!*ptr || !len) 715 return; 716 717 if (!*encoded) { 718 long datalen, i; 719 u32 *buf = *ptr; 720 721 /* 722 * Only dump the non-zero part of the buffer - rarely will 723 * any data completely fill the entire allocated size of 724 * the buffer. 725 */ 726 for (datalen = 0, i = 0; i < len >> 2; i++) 727 if (buf[i]) 728 datalen = ((i + 1) << 2); 729 730 /* 731 * If we reach here, then the originally captured binary buffer 732 * will be replaced with the ascii85 encoded string 733 */ 734 *ptr = adreno_gpu_ascii85_encode(buf, datalen); 735 736 kvfree(buf); 737 738 *encoded = true; 739 } 740 741 if (!*ptr) 742 return; 743 744 drm_puts(p, " data: !!ascii85 |\n"); 745 drm_puts(p, " "); 746 747 drm_puts(p, *ptr); 748 749 drm_puts(p, "\n"); 750 } 751 752 void adreno_show(struct msm_gpu *gpu, struct msm_gpu_state *state, 753 struct drm_printer *p) 754 { 755 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 756 int i; 757 758 if (IS_ERR_OR_NULL(state)) 759 return; 760 761 drm_printf(p, "revision: %d (%d.%d.%d.%d)\n", 762 adreno_gpu->info->revn, adreno_gpu->rev.core, 763 adreno_gpu->rev.major, adreno_gpu->rev.minor, 764 adreno_gpu->rev.patchid); 765 /* 766 * If this is state collected due to iova fault, so fault related info 767 * 768 * TTBR0 would not be zero, so this is a good way to distinguish 769 */ 770 if (state->fault_info.ttbr0) { 771 const struct msm_gpu_fault_info *info = &state->fault_info; 772 773 drm_puts(p, "fault-info:\n"); 774 drm_printf(p, " - ttbr0=%.16llx\n", info->ttbr0); 775 drm_printf(p, " - iova=%.16lx\n", info->iova); 776 drm_printf(p, " - dir=%s\n", info->flags & IOMMU_FAULT_WRITE ? "WRITE" : "READ"); 777 drm_printf(p, " - type=%s\n", info->type); 778 drm_printf(p, " - source=%s\n", info->block); 779 } 780 781 drm_printf(p, "rbbm-status: 0x%08x\n", state->rbbm_status); 782 783 drm_puts(p, "ringbuffer:\n"); 784 785 for (i = 0; i < gpu->nr_rings; i++) { 786 drm_printf(p, " - id: %d\n", i); 787 drm_printf(p, " iova: 0x%016llx\n", state->ring[i].iova); 788 drm_printf(p, " last-fence: %d\n", state->ring[i].seqno); 789 drm_printf(p, " retired-fence: %d\n", state->ring[i].fence); 790 drm_printf(p, " rptr: %d\n", state->ring[i].rptr); 791 drm_printf(p, " wptr: %d\n", state->ring[i].wptr); 792 drm_printf(p, " size: %d\n", MSM_GPU_RINGBUFFER_SZ); 793 794 adreno_show_object(p, &state->ring[i].data, 795 state->ring[i].data_size, &state->ring[i].encoded); 796 } 797 798 if (state->bos) { 799 drm_puts(p, "bos:\n"); 800 801 for (i = 0; i < state->nr_bos; i++) { 802 drm_printf(p, " - iova: 0x%016llx\n", 803 state->bos[i].iova); 804 drm_printf(p, " size: %zd\n", state->bos[i].size); 805 806 adreno_show_object(p, &state->bos[i].data, 807 state->bos[i].size, &state->bos[i].encoded); 808 } 809 } 810 811 if (state->nr_registers) { 812 drm_puts(p, "registers:\n"); 813 814 for (i = 0; i < state->nr_registers; i++) { 815 drm_printf(p, " - { offset: 0x%04x, value: 0x%08x }\n", 816 state->registers[i * 2] << 2, 817 state->registers[(i * 2) + 1]); 818 } 819 } 820 } 821 #endif 822 823 /* Dump common gpu status and scratch registers on any hang, to make 824 * the hangcheck logs more useful. The scratch registers seem always 825 * safe to read when GPU has hung (unlike some other regs, depending 826 * on how the GPU hung), and they are useful to match up to cmdstream 827 * dumps when debugging hangs: 828 */ 829 void adreno_dump_info(struct msm_gpu *gpu) 830 { 831 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 832 int i; 833 834 printk("revision: %d (%d.%d.%d.%d)\n", 835 adreno_gpu->info->revn, adreno_gpu->rev.core, 836 adreno_gpu->rev.major, adreno_gpu->rev.minor, 837 adreno_gpu->rev.patchid); 838 839 for (i = 0; i < gpu->nr_rings; i++) { 840 struct msm_ringbuffer *ring = gpu->rb[i]; 841 842 printk("rb %d: fence: %d/%d\n", i, 843 ring->memptrs->fence, 844 ring->fctx->last_fence); 845 846 printk("rptr: %d\n", get_rptr(adreno_gpu, ring)); 847 printk("rb wptr: %d\n", get_wptr(ring)); 848 } 849 } 850 851 /* would be nice to not have to duplicate the _show() stuff with printk(): */ 852 void adreno_dump(struct msm_gpu *gpu) 853 { 854 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 855 int i; 856 857 if (!adreno_gpu->registers) 858 return; 859 860 /* dump these out in a form that can be parsed by demsm: */ 861 printk("IO:region %s 00000000 00020000\n", gpu->name); 862 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) { 863 uint32_t start = adreno_gpu->registers[i]; 864 uint32_t end = adreno_gpu->registers[i+1]; 865 uint32_t addr; 866 867 for (addr = start; addr <= end; addr++) { 868 uint32_t val = gpu_read(gpu, addr); 869 printk("IO:R %08x %08x\n", addr<<2, val); 870 } 871 } 872 } 873 874 static uint32_t ring_freewords(struct msm_ringbuffer *ring) 875 { 876 struct adreno_gpu *adreno_gpu = to_adreno_gpu(ring->gpu); 877 uint32_t size = MSM_GPU_RINGBUFFER_SZ >> 2; 878 /* Use ring->next to calculate free size */ 879 uint32_t wptr = ring->next - ring->start; 880 uint32_t rptr = get_rptr(adreno_gpu, ring); 881 return (rptr + (size - 1) - wptr) % size; 882 } 883 884 void adreno_wait_ring(struct msm_ringbuffer *ring, uint32_t ndwords) 885 { 886 if (spin_until(ring_freewords(ring) >= ndwords)) 887 DRM_DEV_ERROR(ring->gpu->dev->dev, 888 "timeout waiting for space in ringbuffer %d\n", 889 ring->id); 890 } 891 892 /* Get legacy powerlevels from qcom,gpu-pwrlevels and populate the opp table */ 893 static int adreno_get_legacy_pwrlevels(struct device *dev) 894 { 895 struct device_node *child, *node; 896 int ret; 897 898 node = of_get_compatible_child(dev->of_node, "qcom,gpu-pwrlevels"); 899 if (!node) { 900 DRM_DEV_DEBUG(dev, "Could not find the GPU powerlevels\n"); 901 return -ENXIO; 902 } 903 904 for_each_child_of_node(node, child) { 905 unsigned int val; 906 907 ret = of_property_read_u32(child, "qcom,gpu-freq", &val); 908 if (ret) 909 continue; 910 911 /* 912 * Skip the intentionally bogus clock value found at the bottom 913 * of most legacy frequency tables 914 */ 915 if (val != 27000000) 916 dev_pm_opp_add(dev, val, 0); 917 } 918 919 of_node_put(node); 920 921 return 0; 922 } 923 924 static void adreno_get_pwrlevels(struct device *dev, 925 struct msm_gpu *gpu) 926 { 927 unsigned long freq = ULONG_MAX; 928 struct dev_pm_opp *opp; 929 int ret; 930 931 gpu->fast_rate = 0; 932 933 /* You down with OPP? */ 934 if (!of_find_property(dev->of_node, "operating-points-v2", NULL)) 935 ret = adreno_get_legacy_pwrlevels(dev); 936 else { 937 ret = devm_pm_opp_of_add_table(dev); 938 if (ret) 939 DRM_DEV_ERROR(dev, "Unable to set the OPP table\n"); 940 } 941 942 if (!ret) { 943 /* Find the fastest defined rate */ 944 opp = dev_pm_opp_find_freq_floor(dev, &freq); 945 if (!IS_ERR(opp)) { 946 gpu->fast_rate = freq; 947 dev_pm_opp_put(opp); 948 } 949 } 950 951 if (!gpu->fast_rate) { 952 dev_warn(dev, 953 "Could not find a clock rate. Using a reasonable default\n"); 954 /* Pick a suitably safe clock speed for any target */ 955 gpu->fast_rate = 200000000; 956 } 957 958 DBG("fast_rate=%u, slow_rate=27000000", gpu->fast_rate); 959 } 960 961 int adreno_gpu_ocmem_init(struct device *dev, struct adreno_gpu *adreno_gpu, 962 struct adreno_ocmem *adreno_ocmem) 963 { 964 struct ocmem_buf *ocmem_hdl; 965 struct ocmem *ocmem; 966 967 ocmem = of_get_ocmem(dev); 968 if (IS_ERR(ocmem)) { 969 if (PTR_ERR(ocmem) == -ENODEV) { 970 /* 971 * Return success since either the ocmem property was 972 * not specified in device tree, or ocmem support is 973 * not compiled into the kernel. 974 */ 975 return 0; 976 } 977 978 return PTR_ERR(ocmem); 979 } 980 981 ocmem_hdl = ocmem_allocate(ocmem, OCMEM_GRAPHICS, adreno_gpu->gmem); 982 if (IS_ERR(ocmem_hdl)) 983 return PTR_ERR(ocmem_hdl); 984 985 adreno_ocmem->ocmem = ocmem; 986 adreno_ocmem->base = ocmem_hdl->addr; 987 adreno_ocmem->hdl = ocmem_hdl; 988 adreno_gpu->gmem = ocmem_hdl->len; 989 990 return 0; 991 } 992 993 void adreno_gpu_ocmem_cleanup(struct adreno_ocmem *adreno_ocmem) 994 { 995 if (adreno_ocmem && adreno_ocmem->base) 996 ocmem_free(adreno_ocmem->ocmem, OCMEM_GRAPHICS, 997 adreno_ocmem->hdl); 998 } 999 1000 int adreno_read_speedbin(struct device *dev, u32 *speedbin) 1001 { 1002 return nvmem_cell_read_variable_le_u32(dev, "speed_bin", speedbin); 1003 } 1004 1005 int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev, 1006 struct adreno_gpu *adreno_gpu, 1007 const struct adreno_gpu_funcs *funcs, int nr_rings) 1008 { 1009 struct device *dev = &pdev->dev; 1010 struct adreno_platform_config *config = dev->platform_data; 1011 struct msm_gpu_config adreno_gpu_config = { 0 }; 1012 struct msm_gpu *gpu = &adreno_gpu->base; 1013 struct adreno_rev *rev = &config->rev; 1014 const char *gpu_name; 1015 u32 speedbin; 1016 1017 adreno_gpu->funcs = funcs; 1018 adreno_gpu->info = adreno_info(config->rev); 1019 adreno_gpu->gmem = adreno_gpu->info->gmem; 1020 adreno_gpu->revn = adreno_gpu->info->revn; 1021 adreno_gpu->rev = *rev; 1022 1023 if (adreno_read_speedbin(dev, &speedbin) || !speedbin) 1024 speedbin = 0xffff; 1025 adreno_gpu->speedbin = (uint16_t) (0xffff & speedbin); 1026 1027 gpu_name = adreno_gpu->info->name; 1028 if (!gpu_name) { 1029 gpu_name = devm_kasprintf(dev, GFP_KERNEL, "%d.%d.%d.%d", 1030 rev->core, rev->major, rev->minor, 1031 rev->patchid); 1032 if (!gpu_name) 1033 return -ENOMEM; 1034 } 1035 1036 adreno_gpu_config.ioname = "kgsl_3d0_reg_memory"; 1037 1038 adreno_gpu_config.nr_rings = nr_rings; 1039 1040 adreno_get_pwrlevels(dev, gpu); 1041 1042 pm_runtime_set_autosuspend_delay(dev, 1043 adreno_gpu->info->inactive_period); 1044 pm_runtime_use_autosuspend(dev); 1045 pm_runtime_enable(dev); 1046 1047 return msm_gpu_init(drm, pdev, &adreno_gpu->base, &funcs->base, 1048 gpu_name, &adreno_gpu_config); 1049 } 1050 1051 void adreno_gpu_cleanup(struct adreno_gpu *adreno_gpu) 1052 { 1053 struct msm_gpu *gpu = &adreno_gpu->base; 1054 struct msm_drm_private *priv = gpu->dev->dev_private; 1055 unsigned int i; 1056 1057 for (i = 0; i < ARRAY_SIZE(adreno_gpu->info->fw); i++) 1058 release_firmware(adreno_gpu->fw[i]); 1059 1060 pm_runtime_disable(&priv->gpu_pdev->dev); 1061 1062 msm_gpu_cleanup(&adreno_gpu->base); 1063 } 1064