1 /* 2 * Copyright 2023 Red Hat Inc. 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice shall be included in 12 * all copies or substantial portions of the Software. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 20 * OTHER DEALINGS IN THE SOFTWARE. 21 */ 22 #include <rm/rpc.h> 23 24 #include "priv.h" 25 26 #include <core/pci.h> 27 #include <subdev/pci/priv.h> 28 #include <subdev/timer.h> 29 #include <subdev/vfn.h> 30 #include <engine/fifo/chan.h> 31 #include <engine/sec2.h> 32 #include <nvif/log.h> 33 34 #include <nvfw/fw.h> 35 36 #include "nvrm/gsp.h" 37 #include "nvrm/rpcfn.h" 38 #include "nvrm/msgfn.h" 39 #include "nvrm/event.h" 40 #include "nvrm/fifo.h" 41 42 #include <linux/acpi.h> 43 #include <linux/ctype.h> 44 #include <linux/parser.h> 45 46 extern struct dentry *nouveau_debugfs_root; 47 48 static void 49 r535_gsp_msgq_work(struct work_struct *work) 50 { 51 struct nvkm_gsp *gsp = container_of(work, typeof(*gsp), msgq.work); 52 53 mutex_lock(&gsp->cmdq.mutex); 54 if (*gsp->msgq.rptr != *gsp->msgq.wptr) 55 r535_gsp_msg_recv(gsp, 0, 0); 56 mutex_unlock(&gsp->cmdq.mutex); 57 } 58 59 static irqreturn_t 60 r535_gsp_intr(struct nvkm_inth *inth) 61 { 62 struct nvkm_gsp *gsp = container_of(inth, typeof(*gsp), subdev.inth); 63 struct nvkm_subdev *subdev = &gsp->subdev; 64 u32 intr = nvkm_falcon_rd32(&gsp->falcon, 0x0008); 65 u32 inte = nvkm_falcon_rd32(&gsp->falcon, gsp->falcon.func->addr2 + 66 gsp->falcon.func->riscv_irqmask); 67 u32 stat = intr & inte; 68 69 if (!stat) { 70 nvkm_debug(subdev, "inte %08x %08x\n", intr, inte); 71 return IRQ_NONE; 72 } 73 74 if (stat & 0x00000040) { 75 nvkm_falcon_wr32(&gsp->falcon, 0x004, 0x00000040); 76 schedule_work(&gsp->msgq.work); 77 stat &= ~0x00000040; 78 } 79 80 if (stat) { 81 nvkm_error(subdev, "intr %08x\n", stat); 82 nvkm_falcon_wr32(&gsp->falcon, 0x014, stat); 83 nvkm_falcon_wr32(&gsp->falcon, 0x004, stat); 84 } 85 86 nvkm_falcon_intr_retrigger(&gsp->falcon); 87 return IRQ_HANDLED; 88 } 89 90 static bool 91 r535_gsp_xlat_mc_engine_idx(u32 mc_engine_idx, enum nvkm_subdev_type *ptype, int *pinst) 92 { 93 switch (mc_engine_idx) { 94 case MC_ENGINE_IDX_GSP: 95 *ptype = NVKM_SUBDEV_GSP; 96 *pinst = 0; 97 return true; 98 case MC_ENGINE_IDX_DISP: 99 *ptype = NVKM_ENGINE_DISP; 100 *pinst = 0; 101 return true; 102 case MC_ENGINE_IDX_CE0 ... MC_ENGINE_IDX_CE9: 103 *ptype = NVKM_ENGINE_CE; 104 *pinst = mc_engine_idx - MC_ENGINE_IDX_CE0; 105 return true; 106 case MC_ENGINE_IDX_GR0: 107 *ptype = NVKM_ENGINE_GR; 108 *pinst = 0; 109 return true; 110 case MC_ENGINE_IDX_NVDEC0 ... MC_ENGINE_IDX_NVDEC7: 111 *ptype = NVKM_ENGINE_NVDEC; 112 *pinst = mc_engine_idx - MC_ENGINE_IDX_NVDEC0; 113 return true; 114 case MC_ENGINE_IDX_MSENC ... MC_ENGINE_IDX_MSENC2: 115 *ptype = NVKM_ENGINE_NVENC; 116 *pinst = mc_engine_idx - MC_ENGINE_IDX_MSENC; 117 return true; 118 case MC_ENGINE_IDX_NVJPEG0 ... MC_ENGINE_IDX_NVJPEG7: 119 *ptype = NVKM_ENGINE_NVJPG; 120 *pinst = mc_engine_idx - MC_ENGINE_IDX_NVJPEG0; 121 return true; 122 case MC_ENGINE_IDX_OFA0: 123 *ptype = NVKM_ENGINE_OFA; 124 *pinst = 0; 125 return true; 126 default: 127 return false; 128 } 129 } 130 131 static int 132 r535_gsp_intr_get_table(struct nvkm_gsp *gsp) 133 { 134 NV2080_CTRL_INTERNAL_INTR_GET_KERNEL_TABLE_PARAMS *ctrl; 135 const struct nvkm_rm_api *rmapi = gsp->rm->api; 136 int ret = 0; 137 138 ctrl = nvkm_gsp_rm_ctrl_get(&gsp->internal.device.subdevice, 139 NV2080_CTRL_CMD_INTERNAL_INTR_GET_KERNEL_TABLE, sizeof(*ctrl)); 140 if (IS_ERR(ctrl)) 141 return PTR_ERR(ctrl); 142 143 ret = nvkm_gsp_rm_ctrl_push(&gsp->internal.device.subdevice, &ctrl, sizeof(*ctrl)); 144 if (WARN_ON(ret)) { 145 nvkm_gsp_rm_ctrl_done(&gsp->internal.device.subdevice, ctrl); 146 return ret; 147 } 148 149 for (unsigned i = 0; i < ctrl->tableLen; i++) { 150 enum nvkm_subdev_type type; 151 int inst; 152 153 nvkm_debug(&gsp->subdev, 154 "%2d: engineIdx %3d pmcIntrMask %08x stall %08x nonStall %08x\n", i, 155 ctrl->table[i].engineIdx, ctrl->table[i].pmcIntrMask, 156 ctrl->table[i].vectorStall, ctrl->table[i].vectorNonStall); 157 158 if (!rmapi->gsp->xlat_mc_engine_idx(ctrl->table[i].engineIdx, &type, &inst)) 159 continue; 160 161 if (WARN_ON(gsp->intr_nr == ARRAY_SIZE(gsp->intr))) { 162 ret = -ENOSPC; 163 break; 164 } 165 166 gsp->intr[gsp->intr_nr].type = type; 167 gsp->intr[gsp->intr_nr].inst = inst; 168 gsp->intr[gsp->intr_nr].stall = ctrl->table[i].vectorStall; 169 gsp->intr[gsp->intr_nr].nonstall = ctrl->table[i].vectorNonStall; 170 gsp->intr_nr++; 171 } 172 173 nvkm_gsp_rm_ctrl_done(&gsp->internal.device.subdevice, ctrl); 174 return ret; 175 } 176 177 void 178 r535_gsp_get_static_info_fb(struct nvkm_gsp *gsp, 179 const struct NV2080_CTRL_CMD_FB_GET_FB_REGION_INFO_PARAMS *info) 180 { 181 int last_usable = -1; 182 183 for (int i = 0; i < info->numFBRegions; i++) { 184 const NV2080_CTRL_CMD_FB_GET_FB_REGION_FB_REGION_INFO *reg = &info->fbRegion[i]; 185 186 nvkm_debug(&gsp->subdev, "fb region %d: " 187 "%016llx-%016llx rsvd:%016llx perf:%08x comp:%d iso:%d prot:%d\n", i, 188 reg->base, reg->limit, reg->reserved, reg->performance, 189 reg->supportCompressed, reg->supportISO, reg->bProtected); 190 191 if (!reg->reserved && !reg->bProtected) { 192 if (reg->supportCompressed && reg->supportISO && 193 !WARN_ON_ONCE(gsp->fb.region_nr >= ARRAY_SIZE(gsp->fb.region))) { 194 const u64 size = (reg->limit + 1) - reg->base; 195 196 gsp->fb.region[gsp->fb.region_nr].addr = reg->base; 197 gsp->fb.region[gsp->fb.region_nr].size = size; 198 gsp->fb.region_nr++; 199 } 200 201 last_usable = i; 202 } 203 } 204 205 if (last_usable >= 0) { 206 u32 rsvd_base = info->fbRegion[last_usable].limit + 1; 207 208 gsp->fb.rsvd_size = gsp->fb.heap.addr - rsvd_base; 209 } 210 } 211 212 static int 213 r535_gsp_get_static_info(struct nvkm_gsp *gsp) 214 { 215 GspStaticConfigInfo *rpc; 216 217 rpc = nvkm_gsp_rpc_rd(gsp, NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO, sizeof(*rpc)); 218 if (IS_ERR(rpc)) 219 return PTR_ERR(rpc); 220 221 gsp->internal.client.object.client = &gsp->internal.client; 222 gsp->internal.client.object.parent = NULL; 223 gsp->internal.client.object.handle = rpc->hInternalClient; 224 gsp->internal.client.gsp = gsp; 225 226 gsp->internal.device.object.client = &gsp->internal.client; 227 gsp->internal.device.object.parent = &gsp->internal.client.object; 228 gsp->internal.device.object.handle = rpc->hInternalDevice; 229 230 gsp->internal.device.subdevice.client = &gsp->internal.client; 231 gsp->internal.device.subdevice.parent = &gsp->internal.device.object; 232 gsp->internal.device.subdevice.handle = rpc->hInternalSubdevice; 233 234 gsp->bar.rm_bar1_pdb = rpc->bar1PdeBase; 235 gsp->bar.rm_bar2_pdb = rpc->bar2PdeBase; 236 237 r535_gsp_get_static_info_fb(gsp, &rpc->fbRegionInfoParams); 238 239 for (int gpc = 0; gpc < ARRAY_SIZE(rpc->tpcInfo); gpc++) { 240 if (rpc->gpcInfo.gpcMask & BIT(gpc)) { 241 gsp->gr.tpcs += hweight32(rpc->tpcInfo[gpc].tpcMask); 242 gsp->gr.gpcs++; 243 } 244 } 245 246 nvkm_gsp_rpc_done(gsp, rpc); 247 return 0; 248 } 249 250 void 251 nvkm_gsp_mem_dtor(struct nvkm_gsp_mem *mem) 252 { 253 if (mem->data) { 254 /* 255 * Poison the buffer to catch any unexpected access from 256 * GSP-RM if the buffer was prematurely freed. 257 */ 258 memset(mem->data, 0xFF, mem->size); 259 260 dma_free_coherent(mem->dev, mem->size, mem->data, mem->addr); 261 put_device(mem->dev); 262 263 memset(mem, 0, sizeof(*mem)); 264 } 265 } 266 267 /** 268 * nvkm_gsp_mem_ctor - constructor for nvkm_gsp_mem objects 269 * @gsp: gsp pointer 270 * @size: number of bytes to allocate 271 * @mem: nvkm_gsp_mem object to initialize 272 * 273 * Allocates a block of memory for use with GSP. 274 * 275 * This memory block can potentially out-live the driver's remove() callback, 276 * so we take a device reference to ensure its lifetime. The reference is 277 * dropped in the destructor. 278 */ 279 int 280 nvkm_gsp_mem_ctor(struct nvkm_gsp *gsp, size_t size, struct nvkm_gsp_mem *mem) 281 { 282 mem->data = dma_alloc_coherent(gsp->subdev.device->dev, size, &mem->addr, GFP_KERNEL); 283 if (WARN_ON(!mem->data)) 284 return -ENOMEM; 285 286 mem->size = size; 287 mem->dev = get_device(gsp->subdev.device->dev); 288 289 return 0; 290 } 291 292 static int 293 r535_gsp_postinit(struct nvkm_gsp *gsp) 294 { 295 struct nvkm_device *device = gsp->subdev.device; 296 const struct nvkm_rm_api *rmapi = gsp->rm->api; 297 int ret; 298 299 ret = rmapi->gsp->get_static_info(gsp); 300 if (WARN_ON(ret)) 301 return ret; 302 303 INIT_WORK(&gsp->msgq.work, r535_gsp_msgq_work); 304 305 ret = r535_gsp_intr_get_table(gsp); 306 if (WARN_ON(ret)) 307 return ret; 308 309 ret = nvkm_gsp_intr_stall(gsp, gsp->subdev.type, gsp->subdev.inst); 310 if (WARN_ON(ret < 0)) 311 return ret; 312 313 ret = nvkm_inth_add(&device->vfn->intr, ret, NVKM_INTR_PRIO_NORMAL, &gsp->subdev, 314 r535_gsp_intr, &gsp->subdev.inth); 315 if (WARN_ON(ret)) 316 return ret; 317 318 nvkm_inth_allow(&gsp->subdev.inth); 319 nvkm_wr32(device, 0x110004, 0x00000040); 320 321 /* Release the DMA buffers that were needed only for boot and init */ 322 nvkm_gsp_mem_dtor(&gsp->boot.fw); 323 nvkm_gsp_mem_dtor(&gsp->libos); 324 325 return ret; 326 } 327 328 static int 329 r535_gsp_rpc_unloading_guest_driver(struct nvkm_gsp *gsp, bool suspend) 330 { 331 rpc_unloading_guest_driver_v1F_07 *rpc; 332 333 rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_UNLOADING_GUEST_DRIVER, sizeof(*rpc)); 334 if (IS_ERR(rpc)) 335 return PTR_ERR(rpc); 336 337 if (suspend) { 338 rpc->bInPMTransition = 1; 339 rpc->bGc6Entering = 0; 340 rpc->newLevel = NV2080_CTRL_GPU_SET_POWER_STATE_GPU_LEVEL_3; 341 } else { 342 rpc->bInPMTransition = 0; 343 rpc->bGc6Entering = 0; 344 rpc->newLevel = NV2080_CTRL_GPU_SET_POWER_STATE_GPU_LEVEL_0; 345 } 346 347 return nvkm_gsp_rpc_wr(gsp, rpc, NVKM_GSP_RPC_REPLY_RECV); 348 } 349 350 enum registry_type { 351 REGISTRY_TABLE_ENTRY_TYPE_DWORD = 1, /* 32-bit unsigned integer */ 352 REGISTRY_TABLE_ENTRY_TYPE_BINARY = 2, /* Binary blob */ 353 REGISTRY_TABLE_ENTRY_TYPE_STRING = 3, /* Null-terminated string */ 354 }; 355 356 /* An arbitrary limit to the length of a registry key */ 357 #define REGISTRY_MAX_KEY_LENGTH 64 358 359 /** 360 * struct registry_list_entry - linked list member for a registry key/value 361 * @head: list_head struct 362 * @type: dword, binary, or string 363 * @klen: the length of name of the key 364 * @vlen: the length of the value 365 * @key: the key name 366 * @dword: the data, if REGISTRY_TABLE_ENTRY_TYPE_DWORD 367 * @binary: the data, if TYPE_BINARY or TYPE_STRING 368 * 369 * Every registry key/value is represented internally by this struct. 370 * 371 * Type DWORD is a simple 32-bit unsigned integer, and its value is stored in 372 * @dword. 373 * 374 * Types BINARY and STRING are variable-length binary blobs. The only real 375 * difference between BINARY and STRING is that STRING is null-terminated and 376 * is expected to contain only printable characters. 377 * 378 * Note: it is technically possible to have multiple keys with the same name 379 * but different types, but this is not useful since GSP-RM expects keys to 380 * have only one specific type. 381 */ 382 struct registry_list_entry { 383 struct list_head head; 384 enum registry_type type; 385 size_t klen; 386 char key[REGISTRY_MAX_KEY_LENGTH]; 387 size_t vlen; 388 u32 dword; /* TYPE_DWORD */ 389 u8 binary[] __counted_by(vlen); /* TYPE_BINARY or TYPE_STRING */ 390 }; 391 392 /** 393 * add_registry -- adds a registry entry 394 * @gsp: gsp pointer 395 * @key: name of the registry key 396 * @type: type of data 397 * @data: pointer to value 398 * @length: size of data, in bytes 399 * 400 * Adds a registry key/value pair to the registry database. 401 * 402 * This function collects the registry information in a linked list. After 403 * all registry keys have been added, build_registry() is used to create the 404 * RPC data structure. 405 * 406 * registry_rpc_size is a running total of the size of all registry keys. 407 * It's used to avoid an O(n) calculation of the size when the RPC is built. 408 * 409 * Returns 0 on success, or negative error code on error. 410 */ 411 static int add_registry(struct nvkm_gsp *gsp, const char *key, 412 enum registry_type type, const void *data, size_t length) 413 { 414 struct registry_list_entry *reg; 415 const size_t nlen = strnlen(key, REGISTRY_MAX_KEY_LENGTH) + 1; 416 size_t alloc_size; /* extra bytes to alloc for binary or string value */ 417 418 if (nlen > REGISTRY_MAX_KEY_LENGTH) 419 return -EINVAL; 420 421 alloc_size = (type == REGISTRY_TABLE_ENTRY_TYPE_DWORD) ? 0 : length; 422 423 reg = kmalloc(sizeof(*reg) + alloc_size, GFP_KERNEL); 424 if (!reg) 425 return -ENOMEM; 426 427 switch (type) { 428 case REGISTRY_TABLE_ENTRY_TYPE_DWORD: 429 reg->dword = *(const u32 *)(data); 430 break; 431 case REGISTRY_TABLE_ENTRY_TYPE_BINARY: 432 case REGISTRY_TABLE_ENTRY_TYPE_STRING: 433 memcpy(reg->binary, data, alloc_size); 434 break; 435 default: 436 nvkm_error(&gsp->subdev, "unrecognized registry type %u for '%s'\n", 437 type, key); 438 kfree(reg); 439 return -EINVAL; 440 } 441 442 memcpy(reg->key, key, nlen); 443 reg->klen = nlen; 444 reg->vlen = length; 445 reg->type = type; 446 447 list_add_tail(®->head, &gsp->registry_list); 448 gsp->registry_rpc_size += sizeof(PACKED_REGISTRY_ENTRY) + nlen + alloc_size; 449 450 return 0; 451 } 452 453 static int add_registry_num(struct nvkm_gsp *gsp, const char *key, u32 value) 454 { 455 return add_registry(gsp, key, REGISTRY_TABLE_ENTRY_TYPE_DWORD, 456 &value, sizeof(u32)); 457 } 458 459 static int add_registry_string(struct nvkm_gsp *gsp, const char *key, const char *value) 460 { 461 return add_registry(gsp, key, REGISTRY_TABLE_ENTRY_TYPE_STRING, 462 value, strlen(value) + 1); 463 } 464 465 /** 466 * build_registry -- create the registry RPC data 467 * @gsp: gsp pointer 468 * @registry: pointer to the RPC payload to fill 469 * 470 * After all registry key/value pairs have been added, call this function to 471 * build the RPC. 472 * 473 * The registry RPC looks like this: 474 * 475 * +-----------------+ 476 * |NvU32 size; | 477 * |NvU32 numEntries;| 478 * +-----------------+ 479 * +----------------------------------------+ 480 * |PACKED_REGISTRY_ENTRY | 481 * +----------------------------------------+ 482 * |Null-terminated key (string) for entry 0| 483 * +----------------------------------------+ 484 * |Binary/string data value for entry 0 | (only if necessary) 485 * +----------------------------------------+ 486 * 487 * +----------------------------------------+ 488 * |PACKED_REGISTRY_ENTRY | 489 * +----------------------------------------+ 490 * |Null-terminated key (string) for entry 1| 491 * +----------------------------------------+ 492 * |Binary/string data value for entry 1 | (only if necessary) 493 * +----------------------------------------+ 494 * ... (and so on, one copy for each entry) 495 * 496 * 497 * The 'data' field of an entry is either a 32-bit integer (for type DWORD) 498 * or an offset into the PACKED_REGISTRY_TABLE (for types BINARY and STRING). 499 * 500 * All memory allocated by add_registry() is released. 501 */ 502 static void build_registry(struct nvkm_gsp *gsp, PACKED_REGISTRY_TABLE *registry) 503 { 504 struct registry_list_entry *reg, *n; 505 size_t str_offset; 506 unsigned int i = 0; 507 508 registry->numEntries = list_count_nodes(&gsp->registry_list); 509 str_offset = struct_size(registry, entries, registry->numEntries); 510 511 list_for_each_entry_safe(reg, n, &gsp->registry_list, head) { 512 registry->entries[i].type = reg->type; 513 registry->entries[i].length = reg->vlen; 514 515 /* Append the key name to the table */ 516 registry->entries[i].nameOffset = str_offset; 517 memcpy((void *)registry + str_offset, reg->key, reg->klen); 518 str_offset += reg->klen; 519 520 switch (reg->type) { 521 case REGISTRY_TABLE_ENTRY_TYPE_DWORD: 522 registry->entries[i].data = reg->dword; 523 break; 524 case REGISTRY_TABLE_ENTRY_TYPE_BINARY: 525 case REGISTRY_TABLE_ENTRY_TYPE_STRING: 526 /* If the type is binary or string, also append the value */ 527 memcpy((void *)registry + str_offset, reg->binary, reg->vlen); 528 registry->entries[i].data = str_offset; 529 str_offset += reg->vlen; 530 break; 531 default: 532 break; 533 } 534 535 i++; 536 list_del(®->head); 537 kfree(reg); 538 } 539 540 /* Double-check that we calculated the sizes correctly */ 541 WARN_ON(gsp->registry_rpc_size != str_offset); 542 543 registry->size = gsp->registry_rpc_size; 544 } 545 546 /** 547 * clean_registry -- clean up registry memory in case of error 548 * @gsp: gsp pointer 549 * 550 * Call this function to clean up all memory allocated by add_registry() 551 * in case of error and build_registry() is not called. 552 */ 553 static void clean_registry(struct nvkm_gsp *gsp) 554 { 555 struct registry_list_entry *reg, *n; 556 557 list_for_each_entry_safe(reg, n, &gsp->registry_list, head) { 558 list_del(®->head); 559 kfree(reg); 560 } 561 562 gsp->registry_rpc_size = sizeof(PACKED_REGISTRY_TABLE); 563 } 564 565 MODULE_PARM_DESC(NVreg_RegistryDwords, 566 "A semicolon-separated list of key=integer pairs of GSP-RM registry keys"); 567 static char *NVreg_RegistryDwords; 568 module_param(NVreg_RegistryDwords, charp, 0400); 569 570 /* dword only */ 571 struct nv_gsp_registry_entries { 572 const char *name; 573 u32 value; 574 }; 575 576 /* 577 * r535_registry_entries - required registry entries for GSP-RM 578 * 579 * This array lists registry entries that are required for GSP-RM to 580 * function correctly. 581 * 582 * RMSecBusResetEnable - enables PCI secondary bus reset 583 * RMForcePcieConfigSave - forces GSP-RM to preserve PCI configuration 584 * registers on any PCI reset. 585 * RMDevidCheckIgnore - allows GSP-RM to boot even if the PCI dev ID 586 * is not found in the internal product name database. 587 */ 588 static const struct nv_gsp_registry_entries r535_registry_entries[] = { 589 { "RMSecBusResetEnable", 1 }, 590 { "RMForcePcieConfigSave", 1 }, 591 { "RMDevidCheckIgnore", 1 }, 592 }; 593 #define NV_GSP_REG_NUM_ENTRIES ARRAY_SIZE(r535_registry_entries) 594 595 /** 596 * strip - strips all characters in 'reject' from 's' 597 * @s: string to strip 598 * @reject: string of characters to remove 599 * 600 * 's' is modified. 601 * 602 * Returns the length of the new string. 603 */ 604 static size_t strip(char *s, const char *reject) 605 { 606 char *p = s, *p2 = s; 607 size_t length = 0; 608 char c; 609 610 do { 611 while ((c = *p2) && strchr(reject, c)) 612 p2++; 613 614 *p++ = c = *p2++; 615 length++; 616 } while (c); 617 618 return length; 619 } 620 621 /** 622 * r535_gsp_rpc_set_registry - build registry RPC and call GSP-RM 623 * @gsp: gsp pointer 624 * 625 * The GSP-RM registry is a set of key/value pairs that configure some aspects 626 * of GSP-RM. The keys are strings, and the values are 32-bit integers. 627 * 628 * The registry is built from a combination of a static hard-coded list (see 629 * above) and entries passed on the driver's command line. 630 */ 631 static int 632 r535_gsp_rpc_set_registry(struct nvkm_gsp *gsp) 633 { 634 PACKED_REGISTRY_TABLE *rpc; 635 unsigned int i; 636 int ret; 637 638 INIT_LIST_HEAD(&gsp->registry_list); 639 gsp->registry_rpc_size = sizeof(PACKED_REGISTRY_TABLE); 640 641 for (i = 0; i < NV_GSP_REG_NUM_ENTRIES; i++) { 642 ret = add_registry_num(gsp, r535_registry_entries[i].name, 643 r535_registry_entries[i].value); 644 if (ret) 645 goto fail; 646 } 647 648 /* 649 * The NVreg_RegistryDwords parameter is a string of key=value 650 * pairs separated by semicolons. We need to extract and trim each 651 * substring, and then parse the substring to extract the key and 652 * value. 653 */ 654 if (NVreg_RegistryDwords) { 655 char *p = kstrdup(NVreg_RegistryDwords, GFP_KERNEL); 656 char *start, *next = p, *equal; 657 658 if (!p) { 659 ret = -ENOMEM; 660 goto fail; 661 } 662 663 /* Remove any whitespace from the parameter string */ 664 strip(p, " \t\n"); 665 666 while ((start = strsep(&next, ";"))) { 667 long value; 668 669 equal = strchr(start, '='); 670 if (!equal || equal == start || equal[1] == 0) { 671 nvkm_error(&gsp->subdev, 672 "ignoring invalid registry string '%s'\n", 673 start); 674 continue; 675 } 676 677 /* Truncate the key=value string to just key */ 678 *equal = 0; 679 680 ret = kstrtol(equal + 1, 0, &value); 681 if (!ret) { 682 ret = add_registry_num(gsp, start, value); 683 } else { 684 /* Not a number, so treat it as a string */ 685 ret = add_registry_string(gsp, start, equal + 1); 686 } 687 688 if (ret) { 689 nvkm_error(&gsp->subdev, 690 "ignoring invalid registry key/value '%s=%s'\n", 691 start, equal + 1); 692 continue; 693 } 694 } 695 696 kfree(p); 697 } 698 699 rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_SET_REGISTRY, gsp->registry_rpc_size); 700 if (IS_ERR(rpc)) { 701 ret = PTR_ERR(rpc); 702 goto fail; 703 } 704 705 build_registry(gsp, rpc); 706 707 return nvkm_gsp_rpc_wr(gsp, rpc, NVKM_GSP_RPC_REPLY_NOSEQ); 708 709 fail: 710 clean_registry(gsp); 711 return ret; 712 } 713 714 #if defined(CONFIG_ACPI) && defined(CONFIG_X86) 715 void 716 r535_gsp_acpi_caps(acpi_handle handle, CAPS_METHOD_DATA *caps) 717 { 718 const guid_t NVOP_DSM_GUID = 719 GUID_INIT(0xA486D8F8, 0x0BDA, 0x471B, 720 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0); 721 u64 NVOP_DSM_REV = 0x00000100; 722 union acpi_object argv4 = { 723 .buffer.type = ACPI_TYPE_BUFFER, 724 .buffer.length = 4, 725 }, *obj; 726 727 caps->status = 0xffff; 728 729 if (!acpi_check_dsm(handle, &NVOP_DSM_GUID, NVOP_DSM_REV, BIT_ULL(0x1a))) 730 return; 731 732 argv4.buffer.pointer = kmalloc(argv4.buffer.length, GFP_KERNEL); 733 if (!argv4.buffer.pointer) 734 return; 735 736 obj = acpi_evaluate_dsm(handle, &NVOP_DSM_GUID, NVOP_DSM_REV, 0x1a, &argv4); 737 if (!obj) 738 goto done; 739 740 if (obj->type != ACPI_TYPE_BUFFER || 741 obj->buffer.length != 4) 742 goto done; 743 744 caps->status = 0; 745 caps->optimusCaps = *(u32 *)obj->buffer.pointer; 746 747 done: 748 ACPI_FREE(obj); 749 750 kfree(argv4.buffer.pointer); 751 } 752 753 void 754 r535_gsp_acpi_jt(acpi_handle handle, JT_METHOD_DATA *jt) 755 { 756 const guid_t JT_DSM_GUID = 757 GUID_INIT(0xCBECA351L, 0x067B, 0x4924, 758 0x9C, 0xBD, 0xB4, 0x6B, 0x00, 0xB8, 0x6F, 0x34); 759 u64 JT_DSM_REV = 0x00000103; 760 u32 caps; 761 union acpi_object argv4 = { 762 .buffer.type = ACPI_TYPE_BUFFER, 763 .buffer.length = sizeof(caps), 764 }, *obj; 765 766 jt->status = 0xffff; 767 768 argv4.buffer.pointer = kmalloc(argv4.buffer.length, GFP_KERNEL); 769 if (!argv4.buffer.pointer) 770 return; 771 772 obj = acpi_evaluate_dsm(handle, &JT_DSM_GUID, JT_DSM_REV, 0x1, &argv4); 773 if (!obj) 774 goto done; 775 776 if (obj->type != ACPI_TYPE_BUFFER || 777 obj->buffer.length != 4) 778 goto done; 779 780 jt->status = 0; 781 jt->jtCaps = *(u32 *)obj->buffer.pointer; 782 jt->jtRevId = (jt->jtCaps & 0xfff00000) >> 20; 783 jt->bSBIOSCaps = 0; 784 785 done: 786 ACPI_FREE(obj); 787 788 kfree(argv4.buffer.pointer); 789 } 790 791 static void 792 r535_gsp_acpi_mux_id(acpi_handle handle, u32 id, MUX_METHOD_DATA_ELEMENT *mode, 793 MUX_METHOD_DATA_ELEMENT *part) 794 { 795 union acpi_object mux_arg = { ACPI_TYPE_INTEGER }; 796 struct acpi_object_list input = { 1, &mux_arg }; 797 acpi_handle iter = NULL, handle_mux = NULL; 798 acpi_status status; 799 u64 value; 800 int ret; 801 802 mode->status = 0xffff; 803 part->status = 0xffff; 804 805 do { 806 status = acpi_get_next_object(ACPI_TYPE_DEVICE, handle, iter, &iter); 807 if (ACPI_FAILURE(status) || !iter) 808 return; 809 810 ret = acpi_get_local_u64_address(iter, &value); 811 if (ret || value != id) 812 continue; 813 814 handle_mux = iter; 815 } while (!handle_mux); 816 817 if (!handle_mux) 818 return; 819 820 /* I -think- 0 means "acquire" according to nvidia's driver source */ 821 input.pointer->integer.type = ACPI_TYPE_INTEGER; 822 input.pointer->integer.value = 0; 823 824 status = acpi_evaluate_integer(handle_mux, "MXDM", &input, &value); 825 if (ACPI_SUCCESS(status)) { 826 mode->acpiId = id; 827 mode->mode = value; 828 mode->status = 0; 829 } 830 831 status = acpi_evaluate_integer(handle_mux, "MXDS", &input, &value); 832 if (ACPI_SUCCESS(status)) { 833 part->acpiId = id; 834 part->mode = value; 835 part->status = 0; 836 } 837 } 838 839 static void 840 r535_gsp_acpi_mux(acpi_handle handle, DOD_METHOD_DATA *dod, MUX_METHOD_DATA *mux) 841 { 842 mux->tableLen = dod->acpiIdListLen / sizeof(dod->acpiIdList[0]); 843 844 for (int i = 0; i < mux->tableLen; i++) { 845 r535_gsp_acpi_mux_id(handle, dod->acpiIdList[i], &mux->acpiIdMuxModeTable[i], 846 &mux->acpiIdMuxPartTable[i]); 847 } 848 } 849 850 void 851 r535_gsp_acpi_dod(acpi_handle handle, DOD_METHOD_DATA *dod) 852 { 853 acpi_status status; 854 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; 855 union acpi_object *_DOD; 856 857 dod->status = 0xffff; 858 859 status = acpi_evaluate_object(handle, "_DOD", NULL, &output); 860 if (ACPI_FAILURE(status)) 861 return; 862 863 _DOD = output.pointer; 864 865 if (_DOD->type != ACPI_TYPE_PACKAGE || 866 _DOD->package.count > ARRAY_SIZE(dod->acpiIdList)) 867 return; 868 869 for (int i = 0; i < _DOD->package.count; i++) { 870 if (WARN_ON(_DOD->package.elements[i].type != ACPI_TYPE_INTEGER)) 871 return; 872 873 dod->acpiIdList[i] = _DOD->package.elements[i].integer.value; 874 dod->acpiIdListLen += sizeof(dod->acpiIdList[0]); 875 } 876 877 dod->status = 0; 878 kfree(output.pointer); 879 } 880 #endif 881 882 static void 883 r535_gsp_acpi_info(struct nvkm_gsp *gsp, ACPI_METHOD_DATA *acpi) 884 { 885 #if defined(CONFIG_ACPI) && defined(CONFIG_X86) 886 acpi_handle handle = ACPI_HANDLE(gsp->subdev.device->dev); 887 888 if (!handle) 889 return; 890 891 acpi->bValid = 1; 892 893 r535_gsp_acpi_dod(handle, &acpi->dodMethodData); 894 if (acpi->dodMethodData.status == 0) 895 r535_gsp_acpi_mux(handle, &acpi->dodMethodData, &acpi->muxMethodData); 896 897 r535_gsp_acpi_jt(handle, &acpi->jtMethodData); 898 r535_gsp_acpi_caps(handle, &acpi->capsMethodData); 899 #endif 900 } 901 902 static int 903 r535_gsp_set_system_info(struct nvkm_gsp *gsp) 904 { 905 struct nvkm_device *device = gsp->subdev.device; 906 struct nvkm_device_pci *pdev = container_of(device, typeof(*pdev), device); 907 GspSystemInfo *info; 908 909 if (WARN_ON(device->type == NVKM_DEVICE_TEGRA)) 910 return -ENOSYS; 911 912 info = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO, sizeof(*info)); 913 if (IS_ERR(info)) 914 return PTR_ERR(info); 915 916 info->gpuPhysAddr = device->func->resource_addr(device, NVKM_BAR0_PRI); 917 info->gpuPhysFbAddr = device->func->resource_addr(device, NVKM_BAR1_FB); 918 info->gpuPhysInstAddr = device->func->resource_addr(device, NVKM_BAR2_INST); 919 info->nvDomainBusDeviceFunc = pci_dev_id(pdev->pdev); 920 info->maxUserVa = TASK_SIZE; 921 info->pciConfigMirrorBase = device->pci->func->cfg.addr; 922 info->pciConfigMirrorSize = device->pci->func->cfg.size; 923 r535_gsp_acpi_info(gsp, &info->acpiMethodData); 924 925 return nvkm_gsp_rpc_wr(gsp, info, NVKM_GSP_RPC_REPLY_NOSEQ); 926 } 927 928 static int 929 r535_gsp_msg_os_error_log(void *priv, u32 fn, void *repv, u32 repc) 930 { 931 struct nvkm_gsp *gsp = priv; 932 struct nvkm_subdev *subdev = &gsp->subdev; 933 rpc_os_error_log_v17_00 *msg = repv; 934 935 if (WARN_ON(repc < sizeof(*msg))) 936 return -EINVAL; 937 938 nvkm_error(subdev, "Xid:%d %s\n", msg->exceptType, msg->errString); 939 return 0; 940 } 941 942 static int 943 r535_gsp_msg_mmu_fault_queued(void *priv, u32 fn, void *repv, u32 repc) 944 { 945 struct nvkm_gsp *gsp = priv; 946 struct nvkm_subdev *subdev = &gsp->subdev; 947 948 WARN_ON(repc != 0); 949 950 nvkm_error(subdev, "mmu fault queued\n"); 951 return 0; 952 } 953 954 static int 955 r535_gsp_msg_post_event(void *priv, u32 fn, void *repv, u32 repc) 956 { 957 struct nvkm_gsp *gsp = priv; 958 struct nvkm_gsp_client *client; 959 struct nvkm_subdev *subdev = &gsp->subdev; 960 rpc_post_event_v17_00 *msg = repv; 961 962 if (WARN_ON(repc < sizeof(*msg))) 963 return -EINVAL; 964 if (WARN_ON(repc != sizeof(*msg) + msg->eventDataSize)) 965 return -EINVAL; 966 967 nvkm_debug(subdev, "event: %08x %08x %d %08x %08x %d %d\n", 968 msg->hClient, msg->hEvent, msg->notifyIndex, msg->data, 969 msg->status, msg->eventDataSize, msg->bNotifyList); 970 971 mutex_lock(&gsp->client_id.mutex); 972 client = idr_find(&gsp->client_id.idr, msg->hClient & 0xffff); 973 if (client) { 974 struct nvkm_gsp_event *event; 975 bool handled = false; 976 977 list_for_each_entry(event, &client->events, head) { 978 if (event->object.handle == msg->hEvent) { 979 event->func(event, msg->eventData, msg->eventDataSize); 980 handled = true; 981 } 982 } 983 984 if (!handled) { 985 nvkm_error(subdev, "event: cid 0x%08x event 0x%08x not found!\n", 986 msg->hClient, msg->hEvent); 987 } 988 } else { 989 nvkm_error(subdev, "event: cid 0x%08x not found!\n", msg->hClient); 990 } 991 mutex_unlock(&gsp->client_id.mutex); 992 return 0; 993 } 994 995 /** 996 * r535_gsp_msg_run_cpu_sequencer() -- process I/O commands from the GSP 997 * @priv: gsp pointer 998 * @fn: function number (ignored) 999 * @repv: pointer to libos print RPC 1000 * @repc: message size 1001 * 1002 * The GSP sequencer is a list of I/O commands that the GSP can send to 1003 * the driver to perform for various purposes. The most common usage is to 1004 * perform a special mid-initialization reset. 1005 */ 1006 static int 1007 r535_gsp_msg_run_cpu_sequencer(void *priv, u32 fn, void *repv, u32 repc) 1008 { 1009 struct nvkm_gsp *gsp = priv; 1010 struct nvkm_subdev *subdev = &gsp->subdev; 1011 struct nvkm_device *device = subdev->device; 1012 rpc_run_cpu_sequencer_v17_00 *seq = repv; 1013 int ptr = 0, ret; 1014 1015 nvkm_debug(subdev, "seq: %08x %08x\n", seq->bufferSizeDWord, seq->cmdIndex); 1016 1017 while (ptr < seq->cmdIndex) { 1018 GSP_SEQUENCER_BUFFER_CMD *cmd = (void *)&seq->commandBuffer[ptr]; 1019 1020 ptr += 1; 1021 ptr += GSP_SEQUENCER_PAYLOAD_SIZE_DWORDS(cmd->opCode); 1022 1023 switch (cmd->opCode) { 1024 case GSP_SEQ_BUF_OPCODE_REG_WRITE: { 1025 u32 addr = cmd->payload.regWrite.addr; 1026 u32 data = cmd->payload.regWrite.val; 1027 1028 nvkm_trace(subdev, "seq wr32 %06x %08x\n", addr, data); 1029 nvkm_wr32(device, addr, data); 1030 } 1031 break; 1032 case GSP_SEQ_BUF_OPCODE_REG_MODIFY: { 1033 u32 addr = cmd->payload.regModify.addr; 1034 u32 mask = cmd->payload.regModify.mask; 1035 u32 data = cmd->payload.regModify.val; 1036 1037 nvkm_trace(subdev, "seq mask %06x %08x %08x\n", addr, mask, data); 1038 nvkm_mask(device, addr, mask, data); 1039 } 1040 break; 1041 case GSP_SEQ_BUF_OPCODE_REG_POLL: { 1042 u32 addr = cmd->payload.regPoll.addr; 1043 u32 mask = cmd->payload.regPoll.mask; 1044 u32 data = cmd->payload.regPoll.val; 1045 u32 usec = cmd->payload.regPoll.timeout ?: 4000000; 1046 //u32 error = cmd->payload.regPoll.error; 1047 1048 nvkm_trace(subdev, "seq poll %06x %08x %08x %d\n", addr, mask, data, usec); 1049 nvkm_rd32(device, addr); 1050 nvkm_usec(device, usec, 1051 if ((nvkm_rd32(device, addr) & mask) == data) 1052 break; 1053 ); 1054 } 1055 break; 1056 case GSP_SEQ_BUF_OPCODE_DELAY_US: { 1057 u32 usec = cmd->payload.delayUs.val; 1058 1059 nvkm_trace(subdev, "seq usec %d\n", usec); 1060 udelay(usec); 1061 } 1062 break; 1063 case GSP_SEQ_BUF_OPCODE_REG_STORE: { 1064 u32 addr = cmd->payload.regStore.addr; 1065 u32 slot = cmd->payload.regStore.index; 1066 1067 seq->regSaveArea[slot] = nvkm_rd32(device, addr); 1068 nvkm_trace(subdev, "seq save %08x -> %d: %08x\n", addr, slot, 1069 seq->regSaveArea[slot]); 1070 } 1071 break; 1072 case GSP_SEQ_BUF_OPCODE_CORE_RESET: 1073 nvkm_trace(subdev, "seq core reset\n"); 1074 nvkm_falcon_reset(&gsp->falcon); 1075 nvkm_falcon_mask(&gsp->falcon, 0x624, 0x00000080, 0x00000080); 1076 nvkm_falcon_wr32(&gsp->falcon, 0x10c, 0x00000000); 1077 break; 1078 case GSP_SEQ_BUF_OPCODE_CORE_START: 1079 nvkm_trace(subdev, "seq core start\n"); 1080 if (nvkm_falcon_rd32(&gsp->falcon, 0x100) & 0x00000040) 1081 nvkm_falcon_wr32(&gsp->falcon, 0x130, 0x00000002); 1082 else 1083 nvkm_falcon_wr32(&gsp->falcon, 0x100, 0x00000002); 1084 break; 1085 case GSP_SEQ_BUF_OPCODE_CORE_WAIT_FOR_HALT: 1086 nvkm_trace(subdev, "seq core wait halt\n"); 1087 nvkm_msec(device, 2000, 1088 if (nvkm_falcon_rd32(&gsp->falcon, 0x100) & 0x00000010) 1089 break; 1090 ); 1091 break; 1092 case GSP_SEQ_BUF_OPCODE_CORE_RESUME: { 1093 struct nvkm_sec2 *sec2 = device->sec2; 1094 u32 mbox0; 1095 1096 nvkm_trace(subdev, "seq core resume\n"); 1097 1098 ret = gsp->func->reset(gsp); 1099 if (WARN_ON(ret)) 1100 return ret; 1101 1102 nvkm_falcon_wr32(&gsp->falcon, 0x040, lower_32_bits(gsp->libos.addr)); 1103 nvkm_falcon_wr32(&gsp->falcon, 0x044, upper_32_bits(gsp->libos.addr)); 1104 1105 nvkm_falcon_start(&sec2->falcon); 1106 1107 if (nvkm_msec(device, 2000, 1108 if (nvkm_rd32(device, 0x1180f8) & 0x04000000) 1109 break; 1110 ) < 0) 1111 return -ETIMEDOUT; 1112 1113 mbox0 = nvkm_falcon_rd32(&sec2->falcon, 0x040); 1114 if (WARN_ON(mbox0)) { 1115 nvkm_error(&gsp->subdev, "seq core resume sec2: 0x%x\n", mbox0); 1116 return -EIO; 1117 } 1118 1119 nvkm_falcon_wr32(&gsp->falcon, 0x080, gsp->boot.app_version); 1120 1121 if (WARN_ON(!nvkm_falcon_riscv_active(&gsp->falcon))) 1122 return -EIO; 1123 } 1124 break; 1125 default: 1126 nvkm_error(subdev, "unknown sequencer opcode %08x\n", cmd->opCode); 1127 return -EINVAL; 1128 } 1129 } 1130 1131 return 0; 1132 } 1133 1134 static int 1135 r535_gsp_shared_init(struct nvkm_gsp *gsp) 1136 { 1137 struct { 1138 msgqTxHeader tx; 1139 msgqRxHeader rx; 1140 } *cmdq, *msgq; 1141 int ret, i; 1142 1143 gsp->shm.cmdq.size = 0x40000; 1144 gsp->shm.msgq.size = 0x40000; 1145 1146 gsp->shm.ptes.nr = (gsp->shm.cmdq.size + gsp->shm.msgq.size) >> GSP_PAGE_SHIFT; 1147 gsp->shm.ptes.nr += DIV_ROUND_UP(gsp->shm.ptes.nr * sizeof(u64), GSP_PAGE_SIZE); 1148 gsp->shm.ptes.size = ALIGN(gsp->shm.ptes.nr * sizeof(u64), GSP_PAGE_SIZE); 1149 1150 ret = nvkm_gsp_mem_ctor(gsp, gsp->shm.ptes.size + 1151 gsp->shm.cmdq.size + 1152 gsp->shm.msgq.size, 1153 &gsp->shm.mem); 1154 if (ret) 1155 return ret; 1156 1157 gsp->shm.ptes.ptr = gsp->shm.mem.data; 1158 gsp->shm.cmdq.ptr = (u8 *)gsp->shm.ptes.ptr + gsp->shm.ptes.size; 1159 gsp->shm.msgq.ptr = (u8 *)gsp->shm.cmdq.ptr + gsp->shm.cmdq.size; 1160 1161 for (i = 0; i < gsp->shm.ptes.nr; i++) 1162 gsp->shm.ptes.ptr[i] = gsp->shm.mem.addr + (i << GSP_PAGE_SHIFT); 1163 1164 cmdq = gsp->shm.cmdq.ptr; 1165 cmdq->tx.version = 0; 1166 cmdq->tx.size = gsp->shm.cmdq.size; 1167 cmdq->tx.entryOff = GSP_PAGE_SIZE; 1168 cmdq->tx.msgSize = GSP_PAGE_SIZE; 1169 cmdq->tx.msgCount = (cmdq->tx.size - cmdq->tx.entryOff) / cmdq->tx.msgSize; 1170 cmdq->tx.writePtr = 0; 1171 cmdq->tx.flags = 1; 1172 cmdq->tx.rxHdrOff = offsetof(typeof(*cmdq), rx.readPtr); 1173 1174 msgq = gsp->shm.msgq.ptr; 1175 1176 gsp->cmdq.cnt = cmdq->tx.msgCount; 1177 gsp->cmdq.wptr = &cmdq->tx.writePtr; 1178 gsp->cmdq.rptr = &msgq->rx.readPtr; 1179 gsp->msgq.cnt = cmdq->tx.msgCount; 1180 gsp->msgq.wptr = &msgq->tx.writePtr; 1181 gsp->msgq.rptr = &cmdq->rx.readPtr; 1182 return 0; 1183 } 1184 1185 static void 1186 r535_gsp_set_rmargs(struct nvkm_gsp *gsp, bool resume) 1187 { 1188 GSP_ARGUMENTS_CACHED *args = gsp->rmargs.data; 1189 1190 args->messageQueueInitArguments.sharedMemPhysAddr = gsp->shm.mem.addr; 1191 args->messageQueueInitArguments.pageTableEntryCount = gsp->shm.ptes.nr; 1192 args->messageQueueInitArguments.cmdQueueOffset = 1193 (u8 *)gsp->shm.cmdq.ptr - (u8 *)gsp->shm.mem.data; 1194 args->messageQueueInitArguments.statQueueOffset = 1195 (u8 *)gsp->shm.msgq.ptr - (u8 *)gsp->shm.mem.data; 1196 1197 if (!resume) { 1198 args->srInitArguments.oldLevel = 0; 1199 args->srInitArguments.flags = 0; 1200 args->srInitArguments.bInPMTransition = 0; 1201 } else { 1202 args->srInitArguments.oldLevel = NV2080_CTRL_GPU_SET_POWER_STATE_GPU_LEVEL_3; 1203 args->srInitArguments.flags = 0; 1204 args->srInitArguments.bInPMTransition = 1; 1205 } 1206 } 1207 1208 static int 1209 r535_gsp_rmargs_init(struct nvkm_gsp *gsp, bool resume) 1210 { 1211 int ret; 1212 1213 if (!resume) { 1214 ret = r535_gsp_shared_init(gsp); 1215 if (ret) 1216 return ret; 1217 1218 ret = nvkm_gsp_mem_ctor(gsp, 0x1000, &gsp->rmargs); 1219 if (ret) 1220 return ret; 1221 } 1222 1223 gsp->rm->api->gsp->set_rmargs(gsp, resume); 1224 return 0; 1225 } 1226 1227 #ifdef CONFIG_DEBUG_FS 1228 1229 /* 1230 * If GSP-RM load fails, then the GSP nvkm object will be deleted, the logging 1231 * debugfs entries will be deleted, and it will not be possible to debug the 1232 * load failure. The keep_gsp_logging parameter tells Nouveau to copy the 1233 * logging buffers to new debugfs entries, and these entries are retained 1234 * until the driver unloads. 1235 */ 1236 static bool keep_gsp_logging; 1237 module_param(keep_gsp_logging, bool, 0444); 1238 MODULE_PARM_DESC(keep_gsp_logging, 1239 "Migrate the GSP-RM logging debugfs entries upon exit"); 1240 1241 /* 1242 * GSP-RM uses a pseudo-class mechanism to define of a variety of per-"engine" 1243 * data structures, and each engine has a "class ID" genererated by a 1244 * pre-processor. This is the class ID for the PMU. 1245 */ 1246 #define NV_GSP_MSG_EVENT_UCODE_LIBOS_CLASS_PMU 0xf3d722 1247 1248 /** 1249 * struct rpc_ucode_libos_print_v1e_08 - RPC payload for libos print buffers 1250 * @ucode_eng_desc: the engine descriptor 1251 * @libos_print_buf_size: the size of the libos_print_buf[] 1252 * @libos_print_buf: the actual buffer 1253 * 1254 * The engine descriptor is divided into 31:8 "class ID" and 7:0 "instance 1255 * ID". We only care about messages from PMU. 1256 */ 1257 struct rpc_ucode_libos_print_v1e_08 { 1258 u32 ucode_eng_desc; 1259 u32 libos_print_buf_size; 1260 u8 libos_print_buf[]; 1261 }; 1262 1263 /** 1264 * r535_gsp_msg_libos_print - capture log message from the PMU 1265 * @priv: gsp pointer 1266 * @fn: function number (ignored) 1267 * @repv: pointer to libos print RPC 1268 * @repc: message size 1269 * 1270 * Called when we receive a UCODE_LIBOS_PRINT event RPC from GSP-RM. This RPC 1271 * contains the contents of the libos print buffer from PMU. It is typically 1272 * only written to when PMU encounters an error. 1273 * 1274 * Technically this RPC can be used to pass print buffers from any number of 1275 * GSP-RM engines, but we only expect to receive them for the PMU. 1276 * 1277 * For the PMU, the buffer is 4K in size and the RPC always contains the full 1278 * contents. 1279 */ 1280 static int 1281 r535_gsp_msg_libos_print(void *priv, u32 fn, void *repv, u32 repc) 1282 { 1283 struct nvkm_gsp *gsp = priv; 1284 struct nvkm_subdev *subdev = &gsp->subdev; 1285 struct rpc_ucode_libos_print_v1e_08 *rpc = repv; 1286 unsigned int class = rpc->ucode_eng_desc >> 8; 1287 1288 nvkm_debug(subdev, "received libos print from class 0x%x for %u bytes\n", 1289 class, rpc->libos_print_buf_size); 1290 1291 if (class != NV_GSP_MSG_EVENT_UCODE_LIBOS_CLASS_PMU) { 1292 nvkm_warn(subdev, 1293 "received libos print from unknown class 0x%x\n", 1294 class); 1295 return -ENOMSG; 1296 } 1297 1298 if (rpc->libos_print_buf_size > GSP_PAGE_SIZE) { 1299 nvkm_error(subdev, "libos print is too large (%u bytes)\n", 1300 rpc->libos_print_buf_size); 1301 return -E2BIG; 1302 } 1303 1304 memcpy(gsp->blob_pmu.data, rpc->libos_print_buf, rpc->libos_print_buf_size); 1305 1306 return 0; 1307 } 1308 1309 /** 1310 * create_debugfs - create a blob debugfs entry 1311 * @gsp: gsp pointer 1312 * @name: name of this dentry 1313 * @blob: blob wrapper 1314 * 1315 * Creates a debugfs entry for a logging buffer with the name 'name'. 1316 */ 1317 static struct dentry *create_debugfs(struct nvkm_gsp *gsp, const char *name, 1318 struct debugfs_blob_wrapper *blob) 1319 { 1320 struct dentry *dent; 1321 1322 dent = debugfs_create_blob(name, 0444, gsp->debugfs.parent, blob); 1323 if (IS_ERR(dent)) { 1324 nvkm_error(&gsp->subdev, 1325 "failed to create %s debugfs entry\n", name); 1326 return NULL; 1327 } 1328 1329 /* 1330 * For some reason, debugfs_create_blob doesn't set the size of the 1331 * dentry, so do that here. See [1] 1332 * 1333 * [1] https://lore.kernel.org/r/linux-fsdevel/20240207200619.3354549-1-ttabi@nvidia.com/ 1334 */ 1335 i_size_write(d_inode(dent), blob->size); 1336 1337 return dent; 1338 } 1339 1340 /** 1341 * r535_gsp_libos_debugfs_init - create logging debugfs entries 1342 * @gsp: gsp pointer 1343 * 1344 * Create the debugfs entries. This exposes the log buffers to userspace so 1345 * that an external tool can parse it. 1346 * 1347 * The 'logpmu' contains exception dumps from the PMU. It is written via an 1348 * RPC sent from GSP-RM and must be only 4KB. We create it here because it's 1349 * only useful if there is a debugfs entry to expose it. If we get the PMU 1350 * logging RPC and there is no debugfs entry, the RPC is just ignored. 1351 * 1352 * The blob_init, blob_rm, and blob_pmu objects can't be transient 1353 * because debugfs_create_blob doesn't copy them. 1354 * 1355 * NOTE: OpenRM loads the logging elf image and prints the log messages 1356 * in real-time. We may add that capability in the future, but that 1357 * requires loading ELF images that are not distributed with the driver and 1358 * adding the parsing code to Nouveau. 1359 * 1360 * Ideally, this should be part of nouveau_debugfs_init(), but that function 1361 * is called too late. We really want to create these debugfs entries before 1362 * r535_gsp_booter_load() is called, so that if GSP-RM fails to initialize, 1363 * there could still be a log to capture. 1364 */ 1365 static void 1366 r535_gsp_libos_debugfs_init(struct nvkm_gsp *gsp) 1367 { 1368 struct device *dev = gsp->subdev.device->dev; 1369 1370 /* Create a new debugfs directory with a name unique to this GPU. */ 1371 gsp->debugfs.parent = debugfs_create_dir(dev_name(dev), nouveau_debugfs_root); 1372 if (IS_ERR(gsp->debugfs.parent)) { 1373 nvkm_error(&gsp->subdev, 1374 "failed to create %s debugfs root\n", dev_name(dev)); 1375 return; 1376 } 1377 1378 gsp->blob_init.data = gsp->loginit.data; 1379 gsp->blob_init.size = gsp->loginit.size; 1380 gsp->blob_intr.data = gsp->logintr.data; 1381 gsp->blob_intr.size = gsp->logintr.size; 1382 gsp->blob_rm.data = gsp->logrm.data; 1383 gsp->blob_rm.size = gsp->logrm.size; 1384 1385 gsp->debugfs.init = create_debugfs(gsp, "loginit", &gsp->blob_init); 1386 if (!gsp->debugfs.init) 1387 goto error; 1388 1389 gsp->debugfs.intr = create_debugfs(gsp, "logintr", &gsp->blob_intr); 1390 if (!gsp->debugfs.intr) 1391 goto error; 1392 1393 gsp->debugfs.rm = create_debugfs(gsp, "logrm", &gsp->blob_rm); 1394 if (!gsp->debugfs.rm) 1395 goto error; 1396 1397 /* 1398 * Since the PMU buffer is copied from an RPC, it doesn't need to be 1399 * a DMA buffer. 1400 */ 1401 gsp->blob_pmu.size = GSP_PAGE_SIZE; 1402 gsp->blob_pmu.data = kzalloc(gsp->blob_pmu.size, GFP_KERNEL); 1403 if (!gsp->blob_pmu.data) 1404 goto error; 1405 1406 gsp->debugfs.pmu = create_debugfs(gsp, "logpmu", &gsp->blob_pmu); 1407 if (!gsp->debugfs.pmu) { 1408 kfree(gsp->blob_pmu.data); 1409 goto error; 1410 } 1411 1412 i_size_write(d_inode(gsp->debugfs.init), gsp->blob_init.size); 1413 i_size_write(d_inode(gsp->debugfs.intr), gsp->blob_intr.size); 1414 i_size_write(d_inode(gsp->debugfs.rm), gsp->blob_rm.size); 1415 i_size_write(d_inode(gsp->debugfs.pmu), gsp->blob_pmu.size); 1416 1417 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT, 1418 r535_gsp_msg_libos_print, gsp); 1419 1420 nvkm_debug(&gsp->subdev, "created debugfs GSP-RM logging entries\n"); 1421 1422 if (keep_gsp_logging) { 1423 nvkm_info(&gsp->subdev, 1424 "logging buffers will be retained on failure\n"); 1425 } 1426 1427 return; 1428 1429 error: 1430 debugfs_remove(gsp->debugfs.parent); 1431 gsp->debugfs.parent = NULL; 1432 } 1433 1434 #endif 1435 1436 static inline u64 1437 r535_gsp_libos_id8(const char *name) 1438 { 1439 u64 id = 0; 1440 1441 for (int i = 0; i < sizeof(id) && *name; i++, name++) 1442 id = (id << 8) | *name; 1443 1444 return id; 1445 } 1446 1447 /** 1448 * create_pte_array() - creates a PTE array of a physically contiguous buffer 1449 * @ptes: pointer to the array 1450 * @addr: base address of physically contiguous buffer (GSP_PAGE_SIZE aligned) 1451 * @size: size of the buffer 1452 * 1453 * GSP-RM sometimes expects physically-contiguous buffers to have an array of 1454 * "PTEs" for each page in that buffer. Although in theory that allows for 1455 * the buffer to be physically discontiguous, GSP-RM does not currently 1456 * support that. 1457 * 1458 * In this case, the PTEs are DMA addresses of each page of the buffer. Since 1459 * the buffer is physically contiguous, calculating all the PTEs is simple 1460 * math. 1461 * 1462 * See memdescGetPhysAddrsForGpu() 1463 */ 1464 static void create_pte_array(u64 *ptes, dma_addr_t addr, size_t size) 1465 { 1466 unsigned int num_pages = DIV_ROUND_UP_ULL(size, GSP_PAGE_SIZE); 1467 unsigned int i; 1468 1469 for (i = 0; i < num_pages; i++) 1470 ptes[i] = (u64)addr + (i << GSP_PAGE_SHIFT); 1471 } 1472 1473 /** 1474 * r535_gsp_libos_init() -- create the libos arguments structure 1475 * @gsp: gsp pointer 1476 * 1477 * The logging buffers are byte queues that contain encoded printf-like 1478 * messages from GSP-RM. They need to be decoded by a special application 1479 * that can parse the buffers. 1480 * 1481 * The 'loginit' buffer contains logs from early GSP-RM init and 1482 * exception dumps. The 'logrm' buffer contains the subsequent logs. Both are 1483 * written to directly by GSP-RM and can be any multiple of GSP_PAGE_SIZE. 1484 * 1485 * The physical address map for the log buffer is stored in the buffer 1486 * itself, starting with offset 1. Offset 0 contains the "put" pointer (pp). 1487 * Initially, pp is equal to 0. If the buffer has valid logging data in it, 1488 * then pp points to index into the buffer where the next logging entry will 1489 * be written. Therefore, the logging data is valid if: 1490 * 1 <= pp < sizeof(buffer)/sizeof(u64) 1491 * 1492 * The GSP only understands 4K pages (GSP_PAGE_SIZE), so even if the kernel is 1493 * configured for a larger page size (e.g. 64K pages), we need to give 1494 * the GSP an array of 4K pages. Fortunately, since the buffer is 1495 * physically contiguous, it's simple math to calculate the addresses. 1496 * 1497 * The buffers must be a multiple of GSP_PAGE_SIZE. GSP-RM also currently 1498 * ignores the @kind field for LOGINIT, LOGINTR, and LOGRM, but expects the 1499 * buffers to be physically contiguous anyway. 1500 * 1501 * The memory allocated for the arguments must remain until the GSP sends the 1502 * init_done RPC. 1503 * 1504 * See _kgspInitLibosLoggingStructures (allocates memory for buffers) 1505 * See kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array) 1506 */ 1507 static int 1508 r535_gsp_libos_init(struct nvkm_gsp *gsp) 1509 { 1510 LibosMemoryRegionInitArgument *args; 1511 int ret; 1512 1513 ret = nvkm_gsp_mem_ctor(gsp, 0x1000, &gsp->libos); 1514 if (ret) 1515 return ret; 1516 1517 args = gsp->libos.data; 1518 1519 ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->loginit); 1520 if (ret) 1521 return ret; 1522 1523 args[0].id8 = r535_gsp_libos_id8("LOGINIT"); 1524 args[0].pa = gsp->loginit.addr; 1525 args[0].size = gsp->loginit.size; 1526 args[0].kind = LIBOS_MEMORY_REGION_CONTIGUOUS; 1527 args[0].loc = LIBOS_MEMORY_REGION_LOC_SYSMEM; 1528 create_pte_array(gsp->loginit.data + sizeof(u64), gsp->loginit.addr, gsp->loginit.size); 1529 1530 ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->logintr); 1531 if (ret) 1532 return ret; 1533 1534 args[1].id8 = r535_gsp_libos_id8("LOGINTR"); 1535 args[1].pa = gsp->logintr.addr; 1536 args[1].size = gsp->logintr.size; 1537 args[1].kind = LIBOS_MEMORY_REGION_CONTIGUOUS; 1538 args[1].loc = LIBOS_MEMORY_REGION_LOC_SYSMEM; 1539 create_pte_array(gsp->logintr.data + sizeof(u64), gsp->logintr.addr, gsp->logintr.size); 1540 1541 ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->logrm); 1542 if (ret) 1543 return ret; 1544 1545 args[2].id8 = r535_gsp_libos_id8("LOGRM"); 1546 args[2].pa = gsp->logrm.addr; 1547 args[2].size = gsp->logrm.size; 1548 args[2].kind = LIBOS_MEMORY_REGION_CONTIGUOUS; 1549 args[2].loc = LIBOS_MEMORY_REGION_LOC_SYSMEM; 1550 create_pte_array(gsp->logrm.data + sizeof(u64), gsp->logrm.addr, gsp->logrm.size); 1551 1552 ret = r535_gsp_rmargs_init(gsp, false); 1553 if (ret) 1554 return ret; 1555 1556 args[3].id8 = r535_gsp_libos_id8("RMARGS"); 1557 args[3].pa = gsp->rmargs.addr; 1558 args[3].size = gsp->rmargs.size; 1559 args[3].kind = LIBOS_MEMORY_REGION_CONTIGUOUS; 1560 args[3].loc = LIBOS_MEMORY_REGION_LOC_SYSMEM; 1561 1562 #ifdef CONFIG_DEBUG_FS 1563 r535_gsp_libos_debugfs_init(gsp); 1564 #endif 1565 1566 return 0; 1567 } 1568 1569 void 1570 nvkm_gsp_sg_free(struct nvkm_device *device, struct sg_table *sgt) 1571 { 1572 struct scatterlist *sgl; 1573 int i; 1574 1575 dma_unmap_sgtable(device->dev, sgt, DMA_BIDIRECTIONAL, 0); 1576 1577 for_each_sgtable_sg(sgt, sgl, i) { 1578 struct page *page = sg_page(sgl); 1579 1580 __free_page(page); 1581 } 1582 1583 sg_free_table(sgt); 1584 } 1585 1586 int 1587 nvkm_gsp_sg(struct nvkm_device *device, u64 size, struct sg_table *sgt) 1588 { 1589 const u64 pages = DIV_ROUND_UP(size, PAGE_SIZE); 1590 struct scatterlist *sgl; 1591 int ret, i; 1592 1593 ret = sg_alloc_table(sgt, pages, GFP_KERNEL); 1594 if (ret) 1595 return ret; 1596 1597 for_each_sgtable_sg(sgt, sgl, i) { 1598 struct page *page = alloc_page(GFP_KERNEL); 1599 1600 if (!page) { 1601 nvkm_gsp_sg_free(device, sgt); 1602 return -ENOMEM; 1603 } 1604 1605 sg_set_page(sgl, page, PAGE_SIZE, 0); 1606 } 1607 1608 ret = dma_map_sgtable(device->dev, sgt, DMA_BIDIRECTIONAL, 0); 1609 if (ret) 1610 nvkm_gsp_sg_free(device, sgt); 1611 1612 return ret; 1613 } 1614 1615 static void 1616 nvkm_gsp_radix3_dtor(struct nvkm_gsp *gsp, struct nvkm_gsp_radix3 *rx3) 1617 { 1618 nvkm_gsp_sg_free(gsp->subdev.device, &rx3->lvl2); 1619 nvkm_gsp_mem_dtor(&rx3->lvl1); 1620 nvkm_gsp_mem_dtor(&rx3->lvl0); 1621 } 1622 1623 /** 1624 * nvkm_gsp_radix3_sg - build a radix3 table from a S/G list 1625 * @gsp: gsp pointer 1626 * @sgt: S/G list to traverse 1627 * @size: size of the image, in bytes 1628 * @rx3: radix3 array to update 1629 * 1630 * The GSP uses a three-level page table, called radix3, to map the firmware. 1631 * Each 64-bit "pointer" in the table is either the bus address of an entry in 1632 * the next table (for levels 0 and 1) or the bus address of the next page in 1633 * the GSP firmware image itself. 1634 * 1635 * Level 0 contains a single entry in one page that points to the first page 1636 * of level 1. 1637 * 1638 * Level 1, since it's also only one page in size, contains up to 512 entries, 1639 * one for each page in Level 2. 1640 * 1641 * Level 2 can be up to 512 pages in size, and each of those entries points to 1642 * the next page of the firmware image. Since there can be up to 512*512 1643 * pages, that limits the size of the firmware to 512*512*GSP_PAGE_SIZE = 1GB. 1644 * 1645 * Internally, the GSP has its window into system memory, but the base 1646 * physical address of the aperture is not 0. In fact, it varies depending on 1647 * the GPU architecture. Since the GPU is a PCI device, this window is 1648 * accessed via DMA and is therefore bound by IOMMU translation. The end 1649 * result is that GSP-RM must translate the bus addresses in the table to GSP 1650 * physical addresses. All this should happen transparently. 1651 * 1652 * Returns 0 on success, or negative error code 1653 * 1654 * See kgspCreateRadix3_IMPL 1655 */ 1656 static int 1657 nvkm_gsp_radix3_sg(struct nvkm_gsp *gsp, struct sg_table *sgt, u64 size, 1658 struct nvkm_gsp_radix3 *rx3) 1659 { 1660 struct sg_dma_page_iter sg_dma_iter; 1661 struct scatterlist *sg; 1662 size_t bufsize; 1663 u64 *pte; 1664 int ret, i, page_idx = 0; 1665 1666 ret = nvkm_gsp_mem_ctor(gsp, GSP_PAGE_SIZE, &rx3->lvl0); 1667 if (ret) 1668 return ret; 1669 1670 ret = nvkm_gsp_mem_ctor(gsp, GSP_PAGE_SIZE, &rx3->lvl1); 1671 if (ret) 1672 goto lvl1_fail; 1673 1674 // Allocate level 2 1675 bufsize = ALIGN((size / GSP_PAGE_SIZE) * sizeof(u64), GSP_PAGE_SIZE); 1676 ret = nvkm_gsp_sg(gsp->subdev.device, bufsize, &rx3->lvl2); 1677 if (ret) 1678 goto lvl2_fail; 1679 1680 // Write the bus address of level 1 to level 0 1681 pte = rx3->lvl0.data; 1682 *pte = rx3->lvl1.addr; 1683 1684 // Write the bus address of each page in level 2 to level 1 1685 pte = rx3->lvl1.data; 1686 for_each_sgtable_dma_page(&rx3->lvl2, &sg_dma_iter, 0) 1687 *pte++ = sg_page_iter_dma_address(&sg_dma_iter); 1688 1689 // Finally, write the bus address of each page in sgt to level 2 1690 for_each_sgtable_sg(&rx3->lvl2, sg, i) { 1691 void *sgl_end; 1692 1693 pte = sg_virt(sg); 1694 sgl_end = (void *)pte + sg->length; 1695 1696 for_each_sgtable_dma_page(sgt, &sg_dma_iter, page_idx) { 1697 *pte++ = sg_page_iter_dma_address(&sg_dma_iter); 1698 page_idx++; 1699 1700 // Go to the next scatterlist for level 2 if we've reached the end 1701 if ((void *)pte >= sgl_end) 1702 break; 1703 } 1704 } 1705 1706 if (ret) { 1707 lvl2_fail: 1708 nvkm_gsp_mem_dtor(&rx3->lvl1); 1709 lvl1_fail: 1710 nvkm_gsp_mem_dtor(&rx3->lvl0); 1711 } 1712 1713 return ret; 1714 } 1715 1716 static u32 1717 r535_gsp_sr_data_size(struct nvkm_gsp *gsp) 1718 { 1719 GspFwWprMeta *meta = gsp->wpr_meta.data; 1720 1721 return meta->gspFwWprEnd - meta->gspFwWprStart; 1722 } 1723 1724 int 1725 r535_gsp_fini(struct nvkm_gsp *gsp, enum nvkm_suspend_state suspend) 1726 { 1727 struct nvkm_rm *rm = gsp->rm; 1728 int ret; 1729 1730 if (suspend) { 1731 u32 len = rm->api->gsp->sr_data_size(gsp); 1732 GspFwSRMeta *sr; 1733 1734 ret = nvkm_gsp_sg(gsp->subdev.device, len, &gsp->sr.sgt); 1735 if (ret) 1736 return ret; 1737 1738 ret = nvkm_gsp_radix3_sg(gsp, &gsp->sr.sgt, len, &gsp->sr.radix3); 1739 if (ret) 1740 return ret; 1741 1742 ret = nvkm_gsp_mem_ctor(gsp, sizeof(*sr), &gsp->sr.meta); 1743 if (ret) 1744 return ret; 1745 1746 sr = gsp->sr.meta.data; 1747 sr->magic = GSP_FW_SR_META_MAGIC; 1748 sr->revision = GSP_FW_SR_META_REVISION; 1749 sr->sysmemAddrOfSuspendResumeData = gsp->sr.radix3.lvl0.addr; 1750 sr->sizeOfSuspendResumeData = len; 1751 1752 ret = rm->api->fbsr->suspend(gsp); 1753 if (ret) { 1754 nvkm_gsp_mem_dtor(&gsp->sr.meta); 1755 nvkm_gsp_radix3_dtor(gsp, &gsp->sr.radix3); 1756 nvkm_gsp_sg_free(gsp->subdev.device, &gsp->sr.sgt); 1757 return ret; 1758 } 1759 1760 /* 1761 * TODO: Debug the GSP firmware / RPC handling to find out why 1762 * without this Turing (but none of the other architectures) 1763 * ends up resetting all channels after resume. 1764 * Additionally, runtime suspend on other architectures quickly 1765 * becomes unreliable without this sleep. If you're experiencing 1766 * issues with runtime suspend, try bumping this delay up and 1767 * sending a patch if it fixes your GPU. 1768 */ 1769 msleep(200); 1770 } 1771 1772 ret = r535_gsp_rpc_unloading_guest_driver(gsp, suspend); 1773 if (WARN_ON(ret)) 1774 return ret; 1775 1776 nvkm_msec(gsp->subdev.device, 2000, 1777 if (nvkm_falcon_rd32(&gsp->falcon, 0x040) == 0x80000000) 1778 break; 1779 ); 1780 1781 gsp->running = false; 1782 return 0; 1783 } 1784 1785 int 1786 r535_gsp_init(struct nvkm_gsp *gsp) 1787 { 1788 int ret; 1789 1790 nvkm_falcon_wr32(&gsp->falcon, 0x080, gsp->boot.app_version); 1791 1792 if (WARN_ON(!nvkm_falcon_riscv_active(&gsp->falcon))) 1793 return -EIO; 1794 1795 ret = r535_gsp_rpc_poll(gsp, NV_VGPU_MSG_EVENT_GSP_INIT_DONE); 1796 if (ret) 1797 goto done; 1798 1799 gsp->running = true; 1800 1801 done: 1802 if (gsp->sr.meta.data) { 1803 gsp->rm->api->fbsr->resume(gsp); 1804 1805 nvkm_gsp_mem_dtor(&gsp->sr.meta); 1806 nvkm_gsp_radix3_dtor(gsp, &gsp->sr.radix3); 1807 nvkm_gsp_sg_free(gsp->subdev.device, &gsp->sr.sgt); 1808 return ret; 1809 } 1810 1811 if (ret == 0) 1812 ret = r535_gsp_postinit(gsp); 1813 1814 return ret; 1815 } 1816 1817 static int 1818 r535_gsp_rm_boot_ctor(struct nvkm_gsp *gsp) 1819 { 1820 const struct firmware *fw = gsp->fws.bl; 1821 const struct nvfw_bin_hdr *hdr; 1822 RM_RISCV_UCODE_DESC *desc; 1823 int ret; 1824 1825 ret = nvkm_gsp_fwsec_sb_ctor(gsp); 1826 if (ret) 1827 return ret; 1828 1829 hdr = nvfw_bin_hdr(&gsp->subdev, fw->data); 1830 desc = (void *)fw->data + hdr->header_offset; 1831 1832 ret = nvkm_gsp_mem_ctor(gsp, hdr->data_size, &gsp->boot.fw); 1833 if (ret) 1834 goto dtor_fwsec; 1835 1836 memcpy(gsp->boot.fw.data, fw->data + hdr->data_offset, hdr->data_size); 1837 1838 gsp->boot.code_offset = desc->monitorCodeOffset; 1839 gsp->boot.data_offset = desc->monitorDataOffset; 1840 gsp->boot.manifest_offset = desc->manifestOffset; 1841 gsp->boot.app_version = desc->appVersion; 1842 return 0; 1843 dtor_fwsec: 1844 nvkm_gsp_fwsec_sb_dtor(gsp); 1845 return ret; 1846 } 1847 1848 static const struct nvkm_firmware_func 1849 r535_gsp_fw = { 1850 .type = NVKM_FIRMWARE_IMG_SGT, 1851 }; 1852 1853 static int 1854 r535_gsp_elf_section(struct nvkm_gsp *gsp, const char *name, const u8 **pdata, u64 *psize) 1855 { 1856 const u8 *img = gsp->fws.rm->data; 1857 const struct elf64_hdr *ehdr = (const struct elf64_hdr *)img; 1858 const struct elf64_shdr *shdr = (const struct elf64_shdr *)&img[ehdr->e_shoff]; 1859 const char *names = &img[shdr[ehdr->e_shstrndx].sh_offset]; 1860 1861 for (int i = 0; i < ehdr->e_shnum; i++, shdr++) { 1862 if (!strcmp(&names[shdr->sh_name], name)) { 1863 *pdata = &img[shdr->sh_offset]; 1864 *psize = shdr->sh_size; 1865 return 0; 1866 } 1867 } 1868 1869 nvkm_error(&gsp->subdev, "section '%s' not found\n", name); 1870 return -ENOENT; 1871 } 1872 1873 #ifdef CONFIG_DEBUG_FS 1874 1875 struct r535_gsp_log { 1876 struct nvif_log log; 1877 1878 /* 1879 * Logging buffers in debugfs. The wrapper objects need to remain 1880 * in memory until the dentry is deleted. 1881 */ 1882 struct dentry *debugfs_logging_dir; 1883 struct debugfs_blob_wrapper blob_init; 1884 struct debugfs_blob_wrapper blob_intr; 1885 struct debugfs_blob_wrapper blob_rm; 1886 struct debugfs_blob_wrapper blob_pmu; 1887 }; 1888 1889 /** 1890 * r535_debugfs_shutdown - delete GSP-RM logging buffers for one GPU 1891 * @_log: nvif_log struct for this GPU 1892 * 1893 * Called when the driver is shutting down, to clean up the retained GSP-RM 1894 * logging buffers. 1895 */ 1896 static void r535_debugfs_shutdown(struct nvif_log *_log) 1897 { 1898 struct r535_gsp_log *log = container_of(_log, struct r535_gsp_log, log); 1899 1900 debugfs_remove(log->debugfs_logging_dir); 1901 1902 kfree(log->blob_init.data); 1903 kfree(log->blob_intr.data); 1904 kfree(log->blob_rm.data); 1905 kfree(log->blob_pmu.data); 1906 1907 /* We also need to delete the list object */ 1908 kfree(log); 1909 } 1910 1911 /** 1912 * is_empty - return true if the logging buffer was never written to 1913 * @b: blob wrapper with ->data field pointing to logging buffer 1914 * 1915 * The first 64-bit field of loginit, and logintr, and logrm is the 'put' 1916 * pointer, and it is initialized to 0. It's a dword-based index into the 1917 * circular buffer, indicating where the next printf write will be made. 1918 * 1919 * If the pointer is still 0 when GSP-RM is shut down, that means that the 1920 * buffer was never written to, so it can be ignored. 1921 * 1922 * This test also works for logpmu, even though it doesn't have a put pointer. 1923 */ 1924 static bool is_empty(const struct debugfs_blob_wrapper *b) 1925 { 1926 u64 *put = b->data; 1927 1928 return put ? (*put == 0) : true; 1929 } 1930 1931 /** 1932 * r535_gsp_copy_log - preserve the logging buffers in a blob 1933 * @parent: the top-level dentry for this GPU 1934 * @name: name of debugfs entry to create 1935 * @s: original wrapper object to copy from 1936 * @t: new wrapper object to copy to 1937 * 1938 * When GSP shuts down, the nvkm_gsp object and all its memory is deleted. 1939 * To preserve the logging buffers, the buffers need to be copied, but only 1940 * if they actually have data. 1941 */ 1942 static int r535_gsp_copy_log(struct dentry *parent, 1943 const char *name, 1944 const struct debugfs_blob_wrapper *s, 1945 struct debugfs_blob_wrapper *t) 1946 { 1947 struct dentry *dent; 1948 void *p; 1949 1950 if (is_empty(s)) 1951 return 0; 1952 1953 /* The original buffers will be deleted */ 1954 p = kmemdup(s->data, s->size, GFP_KERNEL); 1955 if (!p) 1956 return -ENOMEM; 1957 1958 t->data = p; 1959 t->size = s->size; 1960 1961 dent = debugfs_create_blob(name, 0444, parent, t); 1962 if (IS_ERR(dent)) { 1963 kfree(p); 1964 memset(t, 0, sizeof(*t)); 1965 return PTR_ERR(dent); 1966 } 1967 1968 i_size_write(d_inode(dent), t->size); 1969 1970 return 0; 1971 } 1972 1973 /** 1974 * r535_gsp_retain_logging - copy logging buffers to new debugfs root 1975 * @gsp: gsp pointer 1976 * 1977 * If keep_gsp_logging is enabled, then we want to preserve the GSP-RM logging 1978 * buffers and their debugfs entries, but all those objects would normally 1979 * deleted if GSP-RM fails to load. 1980 * 1981 * To preserve the logging buffers, we need to: 1982 * 1983 * 1) Allocate new buffers and copy the logs into them, so that the original 1984 * DMA buffers can be released. 1985 * 1986 * 2) Preserve the directories. We don't need to save single dentries because 1987 * we're going to delete the parent when the 1988 * 1989 * If anything fails in this process, then all the dentries need to be 1990 * deleted. We don't need to deallocate the original logging buffers because 1991 * the caller will do that regardless. 1992 */ 1993 static void r535_gsp_retain_logging(struct nvkm_gsp *gsp) 1994 { 1995 struct device *dev = gsp->subdev.device->dev; 1996 struct r535_gsp_log *log = NULL; 1997 int ret; 1998 1999 if (!keep_gsp_logging || !gsp->debugfs.parent) { 2000 /* Nothing to do */ 2001 goto exit; 2002 } 2003 2004 /* Check to make sure at least one buffer has data. */ 2005 if (is_empty(&gsp->blob_init) && is_empty(&gsp->blob_intr) && 2006 is_empty(&gsp->blob_rm) && is_empty(&gsp->blob_rm)) { 2007 nvkm_warn(&gsp->subdev, "all logging buffers are empty\n"); 2008 goto exit; 2009 } 2010 2011 log = kzalloc_obj(*log); 2012 if (!log) 2013 goto error; 2014 2015 /* 2016 * Since the nvkm_gsp object is going away, the debugfs_blob_wrapper 2017 * objects are also being deleted, which means the dentries will no 2018 * longer be valid. Delete the existing entries so that we can create 2019 * new ones with the same name. 2020 */ 2021 debugfs_remove(gsp->debugfs.init); 2022 debugfs_remove(gsp->debugfs.intr); 2023 debugfs_remove(gsp->debugfs.rm); 2024 debugfs_remove(gsp->debugfs.pmu); 2025 2026 ret = r535_gsp_copy_log(gsp->debugfs.parent, "loginit", &gsp->blob_init, &log->blob_init); 2027 if (ret) 2028 goto error; 2029 2030 ret = r535_gsp_copy_log(gsp->debugfs.parent, "logintr", &gsp->blob_intr, &log->blob_intr); 2031 if (ret) 2032 goto error; 2033 2034 ret = r535_gsp_copy_log(gsp->debugfs.parent, "logrm", &gsp->blob_rm, &log->blob_rm); 2035 if (ret) 2036 goto error; 2037 2038 ret = r535_gsp_copy_log(gsp->debugfs.parent, "logpmu", &gsp->blob_pmu, &log->blob_pmu); 2039 if (ret) 2040 goto error; 2041 2042 /* The nvkm_gsp object is going away, so save the dentry */ 2043 log->debugfs_logging_dir = gsp->debugfs.parent; 2044 2045 log->log.shutdown = r535_debugfs_shutdown; 2046 list_add(&log->log.entry, &gsp_logs.head); 2047 2048 nvkm_warn(&gsp->subdev, 2049 "logging buffers migrated to /sys/kernel/debug/nouveau/%s\n", 2050 dev_name(dev)); 2051 2052 return; 2053 2054 error: 2055 nvkm_warn(&gsp->subdev, "failed to migrate logging buffers\n"); 2056 2057 exit: 2058 debugfs_remove(gsp->debugfs.parent); 2059 2060 if (log) { 2061 kfree(log->blob_init.data); 2062 kfree(log->blob_intr.data); 2063 kfree(log->blob_rm.data); 2064 kfree(log->blob_pmu.data); 2065 kfree(log); 2066 } 2067 } 2068 2069 #endif 2070 2071 /** 2072 * r535_gsp_libos_debugfs_fini - cleanup/retain log buffers on shutdown 2073 * @gsp: gsp pointer 2074 * 2075 * If the log buffers are exposed via debugfs, the data for those entries 2076 * needs to be cleaned up when the GSP device shuts down. 2077 */ 2078 static void 2079 r535_gsp_libos_debugfs_fini(struct nvkm_gsp __maybe_unused *gsp) 2080 { 2081 #ifdef CONFIG_DEBUG_FS 2082 r535_gsp_retain_logging(gsp); 2083 2084 /* 2085 * Unlike the other buffers, the PMU blob is a kmalloc'd buffer that 2086 * exists only if the debugfs entries were created. 2087 */ 2088 kfree(gsp->blob_pmu.data); 2089 gsp->blob_pmu.data = NULL; 2090 #endif 2091 } 2092 2093 void 2094 r535_gsp_dtor(struct nvkm_gsp *gsp) 2095 { 2096 idr_destroy(&gsp->client_id.idr); 2097 mutex_destroy(&gsp->client_id.mutex); 2098 2099 nvkm_gsp_radix3_dtor(gsp, &gsp->radix3); 2100 nvkm_gsp_mem_dtor(&gsp->sig); 2101 nvkm_firmware_dtor(&gsp->fw); 2102 2103 nvkm_falcon_fw_dtor(&gsp->booter.unload); 2104 nvkm_falcon_fw_dtor(&gsp->booter.load); 2105 2106 nvkm_gsp_mem_dtor(&gsp->fmc.args); 2107 kfree(gsp->fmc.sig); 2108 kfree(gsp->fmc.pkey); 2109 kfree(gsp->fmc.hash); 2110 nvkm_gsp_mem_dtor(&gsp->fmc.fw); 2111 2112 mutex_destroy(&gsp->msgq.mutex); 2113 mutex_destroy(&gsp->cmdq.mutex); 2114 2115 nvkm_gsp_dtor_fws(gsp); 2116 nvkm_gsp_fwsec_sb_dtor(gsp); 2117 2118 nvkm_gsp_mem_dtor(&gsp->rmargs); 2119 nvkm_gsp_mem_dtor(&gsp->wpr_meta); 2120 nvkm_gsp_mem_dtor(&gsp->shm.mem); 2121 2122 r535_gsp_libos_debugfs_fini(gsp); 2123 2124 nvkm_gsp_mem_dtor(&gsp->loginit); 2125 nvkm_gsp_mem_dtor(&gsp->logintr); 2126 nvkm_gsp_mem_dtor(&gsp->logrm); 2127 } 2128 2129 static void 2130 r535_gsp_drop_send_user_shared_data(struct nvkm_gsp *gsp) 2131 { 2132 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_GSP_SEND_USER_SHARED_DATA, NULL, NULL); 2133 } 2134 2135 int 2136 r535_gsp_oneinit(struct nvkm_gsp *gsp) 2137 { 2138 struct nvkm_device *device = gsp->subdev.device; 2139 const struct nvkm_rm_api *rmapi = gsp->rm->api; 2140 const u8 *data; 2141 u64 size; 2142 int ret; 2143 2144 mutex_init(&gsp->cmdq.mutex); 2145 mutex_init(&gsp->msgq.mutex); 2146 2147 /* Load GSP firmware from ELF image into DMA-accessible memory. */ 2148 ret = r535_gsp_elf_section(gsp, ".fwimage", &data, &size); 2149 if (ret) 2150 return ret; 2151 2152 ret = nvkm_firmware_ctor(&r535_gsp_fw, "gsp-rm", device, data, size, &gsp->fw); 2153 if (ret) 2154 return ret; 2155 2156 /* Load relevant signature from ELF image. */ 2157 ret = r535_gsp_elf_section(gsp, gsp->func->sig_section, &data, &size); 2158 if (ret) 2159 return ret; 2160 2161 ret = nvkm_gsp_mem_ctor(gsp, ALIGN(size, 256), &gsp->sig); 2162 if (ret) 2163 return ret; 2164 2165 memcpy(gsp->sig.data, data, size); 2166 2167 /* Build radix3 page table for ELF image. */ 2168 ret = nvkm_gsp_radix3_sg(gsp, &gsp->fw.mem.sgt, gsp->fw.len, &gsp->radix3); 2169 if (ret) 2170 return ret; 2171 2172 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER, 2173 r535_gsp_msg_run_cpu_sequencer, gsp); 2174 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_POST_EVENT, r535_gsp_msg_post_event, gsp); 2175 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_RC_TRIGGERED, rmapi->fifo->rc_triggered, gsp); 2176 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED, 2177 r535_gsp_msg_mmu_fault_queued, gsp); 2178 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_OS_ERROR_LOG, r535_gsp_msg_os_error_log, gsp); 2179 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_PERF_BRIDGELESS_INFO_UPDATE, NULL, NULL); 2180 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT, NULL, NULL); 2181 if (rmapi->gsp->drop_send_user_shared_data) 2182 rmapi->gsp->drop_send_user_shared_data(gsp); 2183 if (rmapi->gsp->drop_post_nocat_record) 2184 rmapi->gsp->drop_post_nocat_record(gsp); 2185 2186 ret = r535_gsp_rm_boot_ctor(gsp); 2187 if (ret) 2188 return ret; 2189 2190 /* Release FW images - we've copied them to DMA buffers now. */ 2191 nvkm_gsp_dtor_fws(gsp); 2192 2193 ret = r535_gsp_libos_init(gsp); 2194 if (WARN_ON(ret)) 2195 return ret; 2196 2197 ret = rmapi->gsp->set_system_info(gsp); 2198 if (WARN_ON(ret)) 2199 return ret; 2200 2201 ret = r535_gsp_rpc_set_registry(gsp); 2202 if (WARN_ON(ret)) 2203 return ret; 2204 2205 mutex_init(&gsp->client_id.mutex); 2206 idr_init(&gsp->client_id.idr); 2207 return 0; 2208 } 2209 2210 const struct nvkm_rm_api_gsp 2211 r535_gsp = { 2212 .set_rmargs = r535_gsp_set_rmargs, 2213 .set_system_info = r535_gsp_set_system_info, 2214 .get_static_info = r535_gsp_get_static_info, 2215 .xlat_mc_engine_idx = r535_gsp_xlat_mc_engine_idx, 2216 .drop_send_user_shared_data = r535_gsp_drop_send_user_shared_data, 2217 .sr_data_size = r535_gsp_sr_data_size, 2218 }; 2219