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) 233 { 234 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 235 236 switch (param) { 237 case MSM_PARAM_GPU_ID: 238 *value = adreno_gpu->info->revn; 239 return 0; 240 case MSM_PARAM_GMEM_SIZE: 241 *value = adreno_gpu->gmem; 242 return 0; 243 case MSM_PARAM_GMEM_BASE: 244 *value = !adreno_is_a650_family(adreno_gpu) ? 0x100000 : 0; 245 return 0; 246 case MSM_PARAM_CHIP_ID: 247 *value = (uint64_t)adreno_gpu->rev.patchid | 248 ((uint64_t)adreno_gpu->rev.minor << 8) | 249 ((uint64_t)adreno_gpu->rev.major << 16) | 250 ((uint64_t)adreno_gpu->rev.core << 24); 251 if (!adreno_gpu->info->revn) 252 *value |= ((uint64_t) adreno_gpu->speedbin) << 32; 253 return 0; 254 case MSM_PARAM_MAX_FREQ: 255 *value = adreno_gpu->base.fast_rate; 256 return 0; 257 case MSM_PARAM_TIMESTAMP: 258 if (adreno_gpu->funcs->get_timestamp) { 259 int ret; 260 261 pm_runtime_get_sync(&gpu->pdev->dev); 262 ret = adreno_gpu->funcs->get_timestamp(gpu, value); 263 pm_runtime_put_autosuspend(&gpu->pdev->dev); 264 265 return ret; 266 } 267 return -EINVAL; 268 case MSM_PARAM_PRIORITIES: 269 *value = gpu->nr_rings * NR_SCHED_PRIORITIES; 270 return 0; 271 case MSM_PARAM_PP_PGTABLE: 272 *value = 0; 273 return 0; 274 case MSM_PARAM_FAULTS: 275 *value = gpu->global_faults + ctx->aspace->faults; 276 return 0; 277 case MSM_PARAM_SUSPENDS: 278 *value = gpu->suspend_count; 279 return 0; 280 default: 281 DBG("%s: invalid param: %u", gpu->name, param); 282 return -EINVAL; 283 } 284 } 285 286 int adreno_set_param(struct msm_gpu *gpu, struct msm_file_private *ctx, 287 uint32_t param, uint64_t value) 288 { 289 switch (param) { 290 case MSM_PARAM_SYSPROF: 291 if (!capable(CAP_SYS_ADMIN)) 292 return -EPERM; 293 return msm_file_private_set_sysprof(ctx, gpu, value); 294 default: 295 DBG("%s: invalid param: %u", gpu->name, param); 296 return -EINVAL; 297 } 298 } 299 300 const struct firmware * 301 adreno_request_fw(struct adreno_gpu *adreno_gpu, const char *fwname) 302 { 303 struct drm_device *drm = adreno_gpu->base.dev; 304 const struct firmware *fw = NULL; 305 char *newname; 306 int ret; 307 308 newname = kasprintf(GFP_KERNEL, "qcom/%s", fwname); 309 if (!newname) 310 return ERR_PTR(-ENOMEM); 311 312 /* 313 * Try first to load from qcom/$fwfile using a direct load (to avoid 314 * a potential timeout waiting for usermode helper) 315 */ 316 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) || 317 (adreno_gpu->fwloc == FW_LOCATION_NEW)) { 318 319 ret = request_firmware_direct(&fw, newname, drm->dev); 320 if (!ret) { 321 DRM_DEV_INFO(drm->dev, "loaded %s from new location\n", 322 newname); 323 adreno_gpu->fwloc = FW_LOCATION_NEW; 324 goto out; 325 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) { 326 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n", 327 newname, ret); 328 fw = ERR_PTR(ret); 329 goto out; 330 } 331 } 332 333 /* 334 * Then try the legacy location without qcom/ prefix 335 */ 336 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) || 337 (adreno_gpu->fwloc == FW_LOCATION_LEGACY)) { 338 339 ret = request_firmware_direct(&fw, fwname, drm->dev); 340 if (!ret) { 341 DRM_DEV_INFO(drm->dev, "loaded %s from legacy location\n", 342 newname); 343 adreno_gpu->fwloc = FW_LOCATION_LEGACY; 344 goto out; 345 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) { 346 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n", 347 fwname, ret); 348 fw = ERR_PTR(ret); 349 goto out; 350 } 351 } 352 353 /* 354 * Finally fall back to request_firmware() for cases where the 355 * usermode helper is needed (I think mainly android) 356 */ 357 if ((adreno_gpu->fwloc == FW_LOCATION_UNKNOWN) || 358 (adreno_gpu->fwloc == FW_LOCATION_HELPER)) { 359 360 ret = request_firmware(&fw, newname, drm->dev); 361 if (!ret) { 362 DRM_DEV_INFO(drm->dev, "loaded %s with helper\n", 363 newname); 364 adreno_gpu->fwloc = FW_LOCATION_HELPER; 365 goto out; 366 } else if (adreno_gpu->fwloc != FW_LOCATION_UNKNOWN) { 367 DRM_DEV_ERROR(drm->dev, "failed to load %s: %d\n", 368 newname, ret); 369 fw = ERR_PTR(ret); 370 goto out; 371 } 372 } 373 374 DRM_DEV_ERROR(drm->dev, "failed to load %s\n", fwname); 375 fw = ERR_PTR(-ENOENT); 376 out: 377 kfree(newname); 378 return fw; 379 } 380 381 int adreno_load_fw(struct adreno_gpu *adreno_gpu) 382 { 383 int i; 384 385 for (i = 0; i < ARRAY_SIZE(adreno_gpu->info->fw); i++) { 386 const struct firmware *fw; 387 388 if (!adreno_gpu->info->fw[i]) 389 continue; 390 391 /* Skip if the firmware has already been loaded */ 392 if (adreno_gpu->fw[i]) 393 continue; 394 395 fw = adreno_request_fw(adreno_gpu, adreno_gpu->info->fw[i]); 396 if (IS_ERR(fw)) 397 return PTR_ERR(fw); 398 399 adreno_gpu->fw[i] = fw; 400 } 401 402 return 0; 403 } 404 405 struct drm_gem_object *adreno_fw_create_bo(struct msm_gpu *gpu, 406 const struct firmware *fw, u64 *iova) 407 { 408 struct drm_gem_object *bo; 409 void *ptr; 410 411 ptr = msm_gem_kernel_new(gpu->dev, fw->size - 4, 412 MSM_BO_WC | MSM_BO_GPU_READONLY, gpu->aspace, &bo, iova); 413 414 if (IS_ERR(ptr)) 415 return ERR_CAST(ptr); 416 417 memcpy(ptr, &fw->data[4], fw->size - 4); 418 419 msm_gem_put_vaddr(bo); 420 421 return bo; 422 } 423 424 int adreno_hw_init(struct msm_gpu *gpu) 425 { 426 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 427 int ret, i; 428 429 VERB("%s", gpu->name); 430 431 ret = adreno_load_fw(adreno_gpu); 432 if (ret) 433 return ret; 434 435 for (i = 0; i < gpu->nr_rings; i++) { 436 struct msm_ringbuffer *ring = gpu->rb[i]; 437 438 if (!ring) 439 continue; 440 441 ring->cur = ring->start; 442 ring->next = ring->start; 443 444 /* reset completed fence seqno: */ 445 ring->memptrs->fence = ring->fctx->completed_fence; 446 ring->memptrs->rptr = 0; 447 } 448 449 return 0; 450 } 451 452 /* Use this helper to read rptr, since a430 doesn't update rptr in memory */ 453 static uint32_t get_rptr(struct adreno_gpu *adreno_gpu, 454 struct msm_ringbuffer *ring) 455 { 456 struct msm_gpu *gpu = &adreno_gpu->base; 457 458 return gpu->funcs->get_rptr(gpu, ring); 459 } 460 461 struct msm_ringbuffer *adreno_active_ring(struct msm_gpu *gpu) 462 { 463 return gpu->rb[0]; 464 } 465 466 void adreno_recover(struct msm_gpu *gpu) 467 { 468 struct drm_device *dev = gpu->dev; 469 int ret; 470 471 // XXX pm-runtime?? we *need* the device to be off after this 472 // so maybe continuing to call ->pm_suspend/resume() is better? 473 474 gpu->funcs->pm_suspend(gpu); 475 gpu->funcs->pm_resume(gpu); 476 477 ret = msm_gpu_hw_init(gpu); 478 if (ret) { 479 DRM_DEV_ERROR(dev->dev, "gpu hw init failed: %d\n", ret); 480 /* hmm, oh well? */ 481 } 482 } 483 484 void adreno_flush(struct msm_gpu *gpu, struct msm_ringbuffer *ring, u32 reg) 485 { 486 uint32_t wptr; 487 488 /* Copy the shadow to the actual register */ 489 ring->cur = ring->next; 490 491 /* 492 * Mask wptr value that we calculate to fit in the HW range. This is 493 * to account for the possibility that the last command fit exactly into 494 * the ringbuffer and rb->next hasn't wrapped to zero yet 495 */ 496 wptr = get_wptr(ring); 497 498 /* ensure writes to ringbuffer have hit system memory: */ 499 mb(); 500 501 gpu_write(gpu, reg, wptr); 502 } 503 504 bool adreno_idle(struct msm_gpu *gpu, struct msm_ringbuffer *ring) 505 { 506 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 507 uint32_t wptr = get_wptr(ring); 508 509 /* wait for CP to drain ringbuffer: */ 510 if (!spin_until(get_rptr(adreno_gpu, ring) == wptr)) 511 return true; 512 513 /* TODO maybe we need to reset GPU here to recover from hang? */ 514 DRM_ERROR("%s: timeout waiting to drain ringbuffer %d rptr/wptr = %X/%X\n", 515 gpu->name, ring->id, get_rptr(adreno_gpu, ring), wptr); 516 517 return false; 518 } 519 520 int adreno_gpu_state_get(struct msm_gpu *gpu, struct msm_gpu_state *state) 521 { 522 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 523 int i, count = 0; 524 525 WARN_ON(!mutex_is_locked(&gpu->lock)); 526 527 kref_init(&state->ref); 528 529 ktime_get_real_ts64(&state->time); 530 531 for (i = 0; i < gpu->nr_rings; i++) { 532 int size = 0, j; 533 534 state->ring[i].fence = gpu->rb[i]->memptrs->fence; 535 state->ring[i].iova = gpu->rb[i]->iova; 536 state->ring[i].seqno = gpu->rb[i]->seqno; 537 state->ring[i].rptr = get_rptr(adreno_gpu, gpu->rb[i]); 538 state->ring[i].wptr = get_wptr(gpu->rb[i]); 539 540 /* Copy at least 'wptr' dwords of the data */ 541 size = state->ring[i].wptr; 542 543 /* After wptr find the last non zero dword to save space */ 544 for (j = state->ring[i].wptr; j < MSM_GPU_RINGBUFFER_SZ >> 2; j++) 545 if (gpu->rb[i]->start[j]) 546 size = j + 1; 547 548 if (size) { 549 state->ring[i].data = kvmalloc(size << 2, GFP_KERNEL); 550 if (state->ring[i].data) { 551 memcpy(state->ring[i].data, gpu->rb[i]->start, size << 2); 552 state->ring[i].data_size = size << 2; 553 } 554 } 555 } 556 557 /* Some targets prefer to collect their own registers */ 558 if (!adreno_gpu->registers) 559 return 0; 560 561 /* Count the number of registers */ 562 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) 563 count += adreno_gpu->registers[i + 1] - 564 adreno_gpu->registers[i] + 1; 565 566 state->registers = kcalloc(count * 2, sizeof(u32), GFP_KERNEL); 567 if (state->registers) { 568 int pos = 0; 569 570 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) { 571 u32 start = adreno_gpu->registers[i]; 572 u32 end = adreno_gpu->registers[i + 1]; 573 u32 addr; 574 575 for (addr = start; addr <= end; addr++) { 576 state->registers[pos++] = addr; 577 state->registers[pos++] = gpu_read(gpu, addr); 578 } 579 } 580 581 state->nr_registers = count; 582 } 583 584 return 0; 585 } 586 587 void adreno_gpu_state_destroy(struct msm_gpu_state *state) 588 { 589 int i; 590 591 for (i = 0; i < ARRAY_SIZE(state->ring); i++) 592 kvfree(state->ring[i].data); 593 594 for (i = 0; state->bos && i < state->nr_bos; i++) 595 kvfree(state->bos[i].data); 596 597 kfree(state->bos); 598 kfree(state->comm); 599 kfree(state->cmd); 600 kfree(state->registers); 601 } 602 603 static void adreno_gpu_state_kref_destroy(struct kref *kref) 604 { 605 struct msm_gpu_state *state = container_of(kref, 606 struct msm_gpu_state, ref); 607 608 adreno_gpu_state_destroy(state); 609 kfree(state); 610 } 611 612 int adreno_gpu_state_put(struct msm_gpu_state *state) 613 { 614 if (IS_ERR_OR_NULL(state)) 615 return 1; 616 617 return kref_put(&state->ref, adreno_gpu_state_kref_destroy); 618 } 619 620 #if defined(CONFIG_DEBUG_FS) || defined(CONFIG_DEV_COREDUMP) 621 622 static char *adreno_gpu_ascii85_encode(u32 *src, size_t len) 623 { 624 void *buf; 625 size_t buf_itr = 0, buffer_size; 626 char out[ASCII85_BUFSZ]; 627 long l; 628 int i; 629 630 if (!src || !len) 631 return NULL; 632 633 l = ascii85_encode_len(len); 634 635 /* 636 * Ascii85 outputs either a 5 byte string or a 1 byte string. So we 637 * account for the worst case of 5 bytes per dword plus the 1 for '\0' 638 */ 639 buffer_size = (l * 5) + 1; 640 641 buf = kvmalloc(buffer_size, GFP_KERNEL); 642 if (!buf) 643 return NULL; 644 645 for (i = 0; i < l; i++) 646 buf_itr += scnprintf(buf + buf_itr, buffer_size - buf_itr, "%s", 647 ascii85_encode(src[i], out)); 648 649 return buf; 650 } 651 652 /* len is expected to be in bytes */ 653 void adreno_show_object(struct drm_printer *p, void **ptr, int len, 654 bool *encoded) 655 { 656 if (!*ptr || !len) 657 return; 658 659 if (!*encoded) { 660 long datalen, i; 661 u32 *buf = *ptr; 662 663 /* 664 * Only dump the non-zero part of the buffer - rarely will 665 * any data completely fill the entire allocated size of 666 * the buffer. 667 */ 668 for (datalen = 0, i = 0; i < len >> 2; i++) 669 if (buf[i]) 670 datalen = ((i + 1) << 2); 671 672 /* 673 * If we reach here, then the originally captured binary buffer 674 * will be replaced with the ascii85 encoded string 675 */ 676 *ptr = adreno_gpu_ascii85_encode(buf, datalen); 677 678 kvfree(buf); 679 680 *encoded = true; 681 } 682 683 if (!*ptr) 684 return; 685 686 drm_puts(p, " data: !!ascii85 |\n"); 687 drm_puts(p, " "); 688 689 drm_puts(p, *ptr); 690 691 drm_puts(p, "\n"); 692 } 693 694 void adreno_show(struct msm_gpu *gpu, struct msm_gpu_state *state, 695 struct drm_printer *p) 696 { 697 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 698 int i; 699 700 if (IS_ERR_OR_NULL(state)) 701 return; 702 703 drm_printf(p, "revision: %d (%d.%d.%d.%d)\n", 704 adreno_gpu->info->revn, adreno_gpu->rev.core, 705 adreno_gpu->rev.major, adreno_gpu->rev.minor, 706 adreno_gpu->rev.patchid); 707 /* 708 * If this is state collected due to iova fault, so fault related info 709 * 710 * TTBR0 would not be zero, so this is a good way to distinguish 711 */ 712 if (state->fault_info.ttbr0) { 713 const struct msm_gpu_fault_info *info = &state->fault_info; 714 715 drm_puts(p, "fault-info:\n"); 716 drm_printf(p, " - ttbr0=%.16llx\n", info->ttbr0); 717 drm_printf(p, " - iova=%.16lx\n", info->iova); 718 drm_printf(p, " - dir=%s\n", info->flags & IOMMU_FAULT_WRITE ? "WRITE" : "READ"); 719 drm_printf(p, " - type=%s\n", info->type); 720 drm_printf(p, " - source=%s\n", info->block); 721 } 722 723 drm_printf(p, "rbbm-status: 0x%08x\n", state->rbbm_status); 724 725 drm_puts(p, "ringbuffer:\n"); 726 727 for (i = 0; i < gpu->nr_rings; i++) { 728 drm_printf(p, " - id: %d\n", i); 729 drm_printf(p, " iova: 0x%016llx\n", state->ring[i].iova); 730 drm_printf(p, " last-fence: %d\n", state->ring[i].seqno); 731 drm_printf(p, " retired-fence: %d\n", state->ring[i].fence); 732 drm_printf(p, " rptr: %d\n", state->ring[i].rptr); 733 drm_printf(p, " wptr: %d\n", state->ring[i].wptr); 734 drm_printf(p, " size: %d\n", MSM_GPU_RINGBUFFER_SZ); 735 736 adreno_show_object(p, &state->ring[i].data, 737 state->ring[i].data_size, &state->ring[i].encoded); 738 } 739 740 if (state->bos) { 741 drm_puts(p, "bos:\n"); 742 743 for (i = 0; i < state->nr_bos; i++) { 744 drm_printf(p, " - iova: 0x%016llx\n", 745 state->bos[i].iova); 746 drm_printf(p, " size: %zd\n", state->bos[i].size); 747 748 adreno_show_object(p, &state->bos[i].data, 749 state->bos[i].size, &state->bos[i].encoded); 750 } 751 } 752 753 if (state->nr_registers) { 754 drm_puts(p, "registers:\n"); 755 756 for (i = 0; i < state->nr_registers; i++) { 757 drm_printf(p, " - { offset: 0x%04x, value: 0x%08x }\n", 758 state->registers[i * 2] << 2, 759 state->registers[(i * 2) + 1]); 760 } 761 } 762 } 763 #endif 764 765 /* Dump common gpu status and scratch registers on any hang, to make 766 * the hangcheck logs more useful. The scratch registers seem always 767 * safe to read when GPU has hung (unlike some other regs, depending 768 * on how the GPU hung), and they are useful to match up to cmdstream 769 * dumps when debugging hangs: 770 */ 771 void adreno_dump_info(struct msm_gpu *gpu) 772 { 773 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 774 int i; 775 776 printk("revision: %d (%d.%d.%d.%d)\n", 777 adreno_gpu->info->revn, adreno_gpu->rev.core, 778 adreno_gpu->rev.major, adreno_gpu->rev.minor, 779 adreno_gpu->rev.patchid); 780 781 for (i = 0; i < gpu->nr_rings; i++) { 782 struct msm_ringbuffer *ring = gpu->rb[i]; 783 784 printk("rb %d: fence: %d/%d\n", i, 785 ring->memptrs->fence, 786 ring->seqno); 787 788 printk("rptr: %d\n", get_rptr(adreno_gpu, ring)); 789 printk("rb wptr: %d\n", get_wptr(ring)); 790 } 791 } 792 793 /* would be nice to not have to duplicate the _show() stuff with printk(): */ 794 void adreno_dump(struct msm_gpu *gpu) 795 { 796 struct adreno_gpu *adreno_gpu = to_adreno_gpu(gpu); 797 int i; 798 799 if (!adreno_gpu->registers) 800 return; 801 802 /* dump these out in a form that can be parsed by demsm: */ 803 printk("IO:region %s 00000000 00020000\n", gpu->name); 804 for (i = 0; adreno_gpu->registers[i] != ~0; i += 2) { 805 uint32_t start = adreno_gpu->registers[i]; 806 uint32_t end = adreno_gpu->registers[i+1]; 807 uint32_t addr; 808 809 for (addr = start; addr <= end; addr++) { 810 uint32_t val = gpu_read(gpu, addr); 811 printk("IO:R %08x %08x\n", addr<<2, val); 812 } 813 } 814 } 815 816 static uint32_t ring_freewords(struct msm_ringbuffer *ring) 817 { 818 struct adreno_gpu *adreno_gpu = to_adreno_gpu(ring->gpu); 819 uint32_t size = MSM_GPU_RINGBUFFER_SZ >> 2; 820 /* Use ring->next to calculate free size */ 821 uint32_t wptr = ring->next - ring->start; 822 uint32_t rptr = get_rptr(adreno_gpu, ring); 823 return (rptr + (size - 1) - wptr) % size; 824 } 825 826 void adreno_wait_ring(struct msm_ringbuffer *ring, uint32_t ndwords) 827 { 828 if (spin_until(ring_freewords(ring) >= ndwords)) 829 DRM_DEV_ERROR(ring->gpu->dev->dev, 830 "timeout waiting for space in ringbuffer %d\n", 831 ring->id); 832 } 833 834 /* Get legacy powerlevels from qcom,gpu-pwrlevels and populate the opp table */ 835 static int adreno_get_legacy_pwrlevels(struct device *dev) 836 { 837 struct device_node *child, *node; 838 int ret; 839 840 node = of_get_compatible_child(dev->of_node, "qcom,gpu-pwrlevels"); 841 if (!node) { 842 DRM_DEV_DEBUG(dev, "Could not find the GPU powerlevels\n"); 843 return -ENXIO; 844 } 845 846 for_each_child_of_node(node, child) { 847 unsigned int val; 848 849 ret = of_property_read_u32(child, "qcom,gpu-freq", &val); 850 if (ret) 851 continue; 852 853 /* 854 * Skip the intentionally bogus clock value found at the bottom 855 * of most legacy frequency tables 856 */ 857 if (val != 27000000) 858 dev_pm_opp_add(dev, val, 0); 859 } 860 861 of_node_put(node); 862 863 return 0; 864 } 865 866 static void adreno_get_pwrlevels(struct device *dev, 867 struct msm_gpu *gpu) 868 { 869 unsigned long freq = ULONG_MAX; 870 struct dev_pm_opp *opp; 871 int ret; 872 873 gpu->fast_rate = 0; 874 875 /* You down with OPP? */ 876 if (!of_find_property(dev->of_node, "operating-points-v2", NULL)) 877 ret = adreno_get_legacy_pwrlevels(dev); 878 else { 879 ret = devm_pm_opp_of_add_table(dev); 880 if (ret) 881 DRM_DEV_ERROR(dev, "Unable to set the OPP table\n"); 882 } 883 884 if (!ret) { 885 /* Find the fastest defined rate */ 886 opp = dev_pm_opp_find_freq_floor(dev, &freq); 887 if (!IS_ERR(opp)) { 888 gpu->fast_rate = freq; 889 dev_pm_opp_put(opp); 890 } 891 } 892 893 if (!gpu->fast_rate) { 894 dev_warn(dev, 895 "Could not find a clock rate. Using a reasonable default\n"); 896 /* Pick a suitably safe clock speed for any target */ 897 gpu->fast_rate = 200000000; 898 } 899 900 DBG("fast_rate=%u, slow_rate=27000000", gpu->fast_rate); 901 } 902 903 int adreno_gpu_ocmem_init(struct device *dev, struct adreno_gpu *adreno_gpu, 904 struct adreno_ocmem *adreno_ocmem) 905 { 906 struct ocmem_buf *ocmem_hdl; 907 struct ocmem *ocmem; 908 909 ocmem = of_get_ocmem(dev); 910 if (IS_ERR(ocmem)) { 911 if (PTR_ERR(ocmem) == -ENODEV) { 912 /* 913 * Return success since either the ocmem property was 914 * not specified in device tree, or ocmem support is 915 * not compiled into the kernel. 916 */ 917 return 0; 918 } 919 920 return PTR_ERR(ocmem); 921 } 922 923 ocmem_hdl = ocmem_allocate(ocmem, OCMEM_GRAPHICS, adreno_gpu->gmem); 924 if (IS_ERR(ocmem_hdl)) 925 return PTR_ERR(ocmem_hdl); 926 927 adreno_ocmem->ocmem = ocmem; 928 adreno_ocmem->base = ocmem_hdl->addr; 929 adreno_ocmem->hdl = ocmem_hdl; 930 adreno_gpu->gmem = ocmem_hdl->len; 931 932 return 0; 933 } 934 935 void adreno_gpu_ocmem_cleanup(struct adreno_ocmem *adreno_ocmem) 936 { 937 if (adreno_ocmem && adreno_ocmem->base) 938 ocmem_free(adreno_ocmem->ocmem, OCMEM_GRAPHICS, 939 adreno_ocmem->hdl); 940 } 941 942 int adreno_read_speedbin(struct device *dev, u32 *speedbin) 943 { 944 return nvmem_cell_read_variable_le_u32(dev, "speed_bin", speedbin); 945 } 946 947 int adreno_gpu_init(struct drm_device *drm, struct platform_device *pdev, 948 struct adreno_gpu *adreno_gpu, 949 const struct adreno_gpu_funcs *funcs, int nr_rings) 950 { 951 struct device *dev = &pdev->dev; 952 struct adreno_platform_config *config = dev->platform_data; 953 struct msm_gpu_config adreno_gpu_config = { 0 }; 954 struct msm_gpu *gpu = &adreno_gpu->base; 955 struct adreno_rev *rev = &config->rev; 956 const char *gpu_name; 957 u32 speedbin; 958 959 adreno_gpu->funcs = funcs; 960 adreno_gpu->info = adreno_info(config->rev); 961 adreno_gpu->gmem = adreno_gpu->info->gmem; 962 adreno_gpu->revn = adreno_gpu->info->revn; 963 adreno_gpu->rev = *rev; 964 965 if (adreno_read_speedbin(dev, &speedbin) || !speedbin) 966 speedbin = 0xffff; 967 adreno_gpu->speedbin = (uint16_t) (0xffff & speedbin); 968 969 gpu_name = adreno_gpu->info->name; 970 if (!gpu_name) { 971 gpu_name = devm_kasprintf(dev, GFP_KERNEL, "%d.%d.%d.%d", 972 rev->core, rev->major, rev->minor, 973 rev->patchid); 974 if (!gpu_name) 975 return -ENOMEM; 976 } 977 978 adreno_gpu_config.ioname = "kgsl_3d0_reg_memory"; 979 980 adreno_gpu_config.nr_rings = nr_rings; 981 982 adreno_get_pwrlevels(dev, gpu); 983 984 pm_runtime_set_autosuspend_delay(dev, 985 adreno_gpu->info->inactive_period); 986 pm_runtime_use_autosuspend(dev); 987 pm_runtime_enable(dev); 988 989 return msm_gpu_init(drm, pdev, &adreno_gpu->base, &funcs->base, 990 gpu_name, &adreno_gpu_config); 991 } 992 993 void adreno_gpu_cleanup(struct adreno_gpu *adreno_gpu) 994 { 995 struct msm_gpu *gpu = &adreno_gpu->base; 996 struct msm_drm_private *priv = gpu->dev->dev_private; 997 unsigned int i; 998 999 for (i = 0; i < ARRAY_SIZE(adreno_gpu->info->fw); i++) 1000 release_firmware(adreno_gpu->fw[i]); 1001 1002 pm_runtime_disable(&priv->gpu_pdev->dev); 1003 1004 msm_gpu_cleanup(&adreno_gpu->base); 1005 } 1006