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
r535_gsp_msgq_work(struct work_struct * work)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
r535_gsp_intr(struct nvkm_inth * inth)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
r535_gsp_xlat_mc_engine_idx(u32 mc_engine_idx,enum nvkm_subdev_type * ptype,int * pinst)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
r535_gsp_intr_get_table(struct nvkm_gsp * gsp)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
r535_gsp_get_static_info_fb(struct nvkm_gsp * gsp,const struct NV2080_CTRL_CMD_FB_GET_FB_REGION_INFO_PARAMS * info)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
r535_gsp_get_static_info(struct nvkm_gsp * gsp)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
nvkm_gsp_mem_dtor(struct nvkm_gsp_mem * mem)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
nvkm_gsp_mem_ctor(struct nvkm_gsp * gsp,size_t size,struct nvkm_gsp_mem * mem)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
r535_gsp_postinit(struct nvkm_gsp * gsp)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
r535_gsp_rpc_unloading_guest_driver(struct nvkm_gsp * gsp,bool suspend)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 */
add_registry(struct nvkm_gsp * gsp,const char * key,enum registry_type type,const void * data,size_t length)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
add_registry_num(struct nvkm_gsp * gsp,const char * key,u32 value)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
add_registry_string(struct nvkm_gsp * gsp,const char * key,const char * value)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 */
build_registry(struct nvkm_gsp * gsp,PACKED_REGISTRY_TABLE * registry)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 */
clean_registry(struct nvkm_gsp * gsp)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 */
586 static const struct nv_gsp_registry_entries r535_registry_entries[] = {
587 { "RMSecBusResetEnable", 1 },
588 { "RMForcePcieConfigSave", 1 },
589 };
590 #define NV_GSP_REG_NUM_ENTRIES ARRAY_SIZE(r535_registry_entries)
591
592 /**
593 * strip - strips all characters in 'reject' from 's'
594 * @s: string to strip
595 * @reject: string of characters to remove
596 *
597 * 's' is modified.
598 *
599 * Returns the length of the new string.
600 */
strip(char * s,const char * reject)601 static size_t strip(char *s, const char *reject)
602 {
603 char *p = s, *p2 = s;
604 size_t length = 0;
605 char c;
606
607 do {
608 while ((c = *p2) && strchr(reject, c))
609 p2++;
610
611 *p++ = c = *p2++;
612 length++;
613 } while (c);
614
615 return length;
616 }
617
618 /**
619 * r535_gsp_rpc_set_registry - build registry RPC and call GSP-RM
620 * @gsp: gsp pointer
621 *
622 * The GSP-RM registry is a set of key/value pairs that configure some aspects
623 * of GSP-RM. The keys are strings, and the values are 32-bit integers.
624 *
625 * The registry is built from a combination of a static hard-coded list (see
626 * above) and entries passed on the driver's command line.
627 */
628 static int
r535_gsp_rpc_set_registry(struct nvkm_gsp * gsp)629 r535_gsp_rpc_set_registry(struct nvkm_gsp *gsp)
630 {
631 PACKED_REGISTRY_TABLE *rpc;
632 unsigned int i;
633 int ret;
634
635 INIT_LIST_HEAD(&gsp->registry_list);
636 gsp->registry_rpc_size = sizeof(PACKED_REGISTRY_TABLE);
637
638 for (i = 0; i < NV_GSP_REG_NUM_ENTRIES; i++) {
639 ret = add_registry_num(gsp, r535_registry_entries[i].name,
640 r535_registry_entries[i].value);
641 if (ret)
642 goto fail;
643 }
644
645 /*
646 * The NVreg_RegistryDwords parameter is a string of key=value
647 * pairs separated by semicolons. We need to extract and trim each
648 * substring, and then parse the substring to extract the key and
649 * value.
650 */
651 if (NVreg_RegistryDwords) {
652 char *p = kstrdup(NVreg_RegistryDwords, GFP_KERNEL);
653 char *start, *next = p, *equal;
654
655 if (!p) {
656 ret = -ENOMEM;
657 goto fail;
658 }
659
660 /* Remove any whitespace from the parameter string */
661 strip(p, " \t\n");
662
663 while ((start = strsep(&next, ";"))) {
664 long value;
665
666 equal = strchr(start, '=');
667 if (!equal || equal == start || equal[1] == 0) {
668 nvkm_error(&gsp->subdev,
669 "ignoring invalid registry string '%s'\n",
670 start);
671 continue;
672 }
673
674 /* Truncate the key=value string to just key */
675 *equal = 0;
676
677 ret = kstrtol(equal + 1, 0, &value);
678 if (!ret) {
679 ret = add_registry_num(gsp, start, value);
680 } else {
681 /* Not a number, so treat it as a string */
682 ret = add_registry_string(gsp, start, equal + 1);
683 }
684
685 if (ret) {
686 nvkm_error(&gsp->subdev,
687 "ignoring invalid registry key/value '%s=%s'\n",
688 start, equal + 1);
689 continue;
690 }
691 }
692
693 kfree(p);
694 }
695
696 rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_SET_REGISTRY, gsp->registry_rpc_size);
697 if (IS_ERR(rpc)) {
698 ret = PTR_ERR(rpc);
699 goto fail;
700 }
701
702 build_registry(gsp, rpc);
703
704 return nvkm_gsp_rpc_wr(gsp, rpc, NVKM_GSP_RPC_REPLY_NOWAIT);
705
706 fail:
707 clean_registry(gsp);
708 return ret;
709 }
710
711 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
712 void
r535_gsp_acpi_caps(acpi_handle handle,CAPS_METHOD_DATA * caps)713 r535_gsp_acpi_caps(acpi_handle handle, CAPS_METHOD_DATA *caps)
714 {
715 const guid_t NVOP_DSM_GUID =
716 GUID_INIT(0xA486D8F8, 0x0BDA, 0x471B,
717 0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0);
718 u64 NVOP_DSM_REV = 0x00000100;
719 union acpi_object argv4 = {
720 .buffer.type = ACPI_TYPE_BUFFER,
721 .buffer.length = 4,
722 }, *obj;
723
724 caps->status = 0xffff;
725
726 if (!acpi_check_dsm(handle, &NVOP_DSM_GUID, NVOP_DSM_REV, BIT_ULL(0x1a)))
727 return;
728
729 argv4.buffer.pointer = kmalloc(argv4.buffer.length, GFP_KERNEL);
730 if (!argv4.buffer.pointer)
731 return;
732
733 obj = acpi_evaluate_dsm(handle, &NVOP_DSM_GUID, NVOP_DSM_REV, 0x1a, &argv4);
734 if (!obj)
735 goto done;
736
737 if (WARN_ON(obj->type != ACPI_TYPE_BUFFER) ||
738 WARN_ON(obj->buffer.length != 4))
739 goto done;
740
741 caps->status = 0;
742 caps->optimusCaps = *(u32 *)obj->buffer.pointer;
743
744 done:
745 ACPI_FREE(obj);
746
747 kfree(argv4.buffer.pointer);
748 }
749
750 void
r535_gsp_acpi_jt(acpi_handle handle,JT_METHOD_DATA * jt)751 r535_gsp_acpi_jt(acpi_handle handle, JT_METHOD_DATA *jt)
752 {
753 const guid_t JT_DSM_GUID =
754 GUID_INIT(0xCBECA351L, 0x067B, 0x4924,
755 0x9C, 0xBD, 0xB4, 0x6B, 0x00, 0xB8, 0x6F, 0x34);
756 u64 JT_DSM_REV = 0x00000103;
757 u32 caps;
758 union acpi_object argv4 = {
759 .buffer.type = ACPI_TYPE_BUFFER,
760 .buffer.length = sizeof(caps),
761 }, *obj;
762
763 jt->status = 0xffff;
764
765 argv4.buffer.pointer = kmalloc(argv4.buffer.length, GFP_KERNEL);
766 if (!argv4.buffer.pointer)
767 return;
768
769 obj = acpi_evaluate_dsm(handle, &JT_DSM_GUID, JT_DSM_REV, 0x1, &argv4);
770 if (!obj)
771 goto done;
772
773 if (WARN_ON(obj->type != ACPI_TYPE_BUFFER) ||
774 WARN_ON(obj->buffer.length != 4))
775 goto done;
776
777 jt->status = 0;
778 jt->jtCaps = *(u32 *)obj->buffer.pointer;
779 jt->jtRevId = (jt->jtCaps & 0xfff00000) >> 20;
780 jt->bSBIOSCaps = 0;
781
782 done:
783 ACPI_FREE(obj);
784
785 kfree(argv4.buffer.pointer);
786 }
787
788 static void
r535_gsp_acpi_mux_id(acpi_handle handle,u32 id,MUX_METHOD_DATA_ELEMENT * mode,MUX_METHOD_DATA_ELEMENT * part)789 r535_gsp_acpi_mux_id(acpi_handle handle, u32 id, MUX_METHOD_DATA_ELEMENT *mode,
790 MUX_METHOD_DATA_ELEMENT *part)
791 {
792 union acpi_object mux_arg = { ACPI_TYPE_INTEGER };
793 struct acpi_object_list input = { 1, &mux_arg };
794 acpi_handle iter = NULL, handle_mux = NULL;
795 acpi_status status;
796 unsigned long long value;
797
798 mode->status = 0xffff;
799 part->status = 0xffff;
800
801 do {
802 status = acpi_get_next_object(ACPI_TYPE_DEVICE, handle, iter, &iter);
803 if (ACPI_FAILURE(status) || !iter)
804 return;
805
806 status = acpi_evaluate_integer(iter, "_ADR", NULL, &value);
807 if (ACPI_FAILURE(status) || value != id)
808 continue;
809
810 handle_mux = iter;
811 } while (!handle_mux);
812
813 if (!handle_mux)
814 return;
815
816 /* I -think- 0 means "acquire" according to nvidia's driver source */
817 input.pointer->integer.type = ACPI_TYPE_INTEGER;
818 input.pointer->integer.value = 0;
819
820 status = acpi_evaluate_integer(handle_mux, "MXDM", &input, &value);
821 if (ACPI_SUCCESS(status)) {
822 mode->acpiId = id;
823 mode->mode = value;
824 mode->status = 0;
825 }
826
827 status = acpi_evaluate_integer(handle_mux, "MXDS", &input, &value);
828 if (ACPI_SUCCESS(status)) {
829 part->acpiId = id;
830 part->mode = value;
831 part->status = 0;
832 }
833 }
834
835 static void
r535_gsp_acpi_mux(acpi_handle handle,DOD_METHOD_DATA * dod,MUX_METHOD_DATA * mux)836 r535_gsp_acpi_mux(acpi_handle handle, DOD_METHOD_DATA *dod, MUX_METHOD_DATA *mux)
837 {
838 mux->tableLen = dod->acpiIdListLen / sizeof(dod->acpiIdList[0]);
839
840 for (int i = 0; i < mux->tableLen; i++) {
841 r535_gsp_acpi_mux_id(handle, dod->acpiIdList[i], &mux->acpiIdMuxModeTable[i],
842 &mux->acpiIdMuxPartTable[i]);
843 }
844 }
845
846 void
r535_gsp_acpi_dod(acpi_handle handle,DOD_METHOD_DATA * dod)847 r535_gsp_acpi_dod(acpi_handle handle, DOD_METHOD_DATA *dod)
848 {
849 acpi_status status;
850 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
851 union acpi_object *_DOD;
852
853 dod->status = 0xffff;
854
855 status = acpi_evaluate_object(handle, "_DOD", NULL, &output);
856 if (ACPI_FAILURE(status))
857 return;
858
859 _DOD = output.pointer;
860
861 if (WARN_ON(_DOD->type != ACPI_TYPE_PACKAGE) ||
862 WARN_ON(_DOD->package.count > ARRAY_SIZE(dod->acpiIdList)))
863 return;
864
865 for (int i = 0; i < _DOD->package.count; i++) {
866 if (WARN_ON(_DOD->package.elements[i].type != ACPI_TYPE_INTEGER))
867 return;
868
869 dod->acpiIdList[i] = _DOD->package.elements[i].integer.value;
870 dod->acpiIdListLen += sizeof(dod->acpiIdList[0]);
871 }
872
873 dod->status = 0;
874 kfree(output.pointer);
875 }
876 #endif
877
878 static void
r535_gsp_acpi_info(struct nvkm_gsp * gsp,ACPI_METHOD_DATA * acpi)879 r535_gsp_acpi_info(struct nvkm_gsp *gsp, ACPI_METHOD_DATA *acpi)
880 {
881 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
882 acpi_handle handle = ACPI_HANDLE(gsp->subdev.device->dev);
883
884 if (!handle)
885 return;
886
887 acpi->bValid = 1;
888
889 r535_gsp_acpi_dod(handle, &acpi->dodMethodData);
890 if (acpi->dodMethodData.status == 0)
891 r535_gsp_acpi_mux(handle, &acpi->dodMethodData, &acpi->muxMethodData);
892
893 r535_gsp_acpi_jt(handle, &acpi->jtMethodData);
894 r535_gsp_acpi_caps(handle, &acpi->capsMethodData);
895 #endif
896 }
897
898 static int
r535_gsp_set_system_info(struct nvkm_gsp * gsp)899 r535_gsp_set_system_info(struct nvkm_gsp *gsp)
900 {
901 struct nvkm_device *device = gsp->subdev.device;
902 struct nvkm_device_pci *pdev = container_of(device, typeof(*pdev), device);
903 GspSystemInfo *info;
904
905 if (WARN_ON(device->type == NVKM_DEVICE_TEGRA))
906 return -ENOSYS;
907
908 info = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO, sizeof(*info));
909 if (IS_ERR(info))
910 return PTR_ERR(info);
911
912 info->gpuPhysAddr = device->func->resource_addr(device, NVKM_BAR0_PRI);
913 info->gpuPhysFbAddr = device->func->resource_addr(device, NVKM_BAR1_FB);
914 info->gpuPhysInstAddr = device->func->resource_addr(device, NVKM_BAR2_INST);
915 info->nvDomainBusDeviceFunc = pci_dev_id(pdev->pdev);
916 info->maxUserVa = TASK_SIZE;
917 info->pciConfigMirrorBase = device->pci->func->cfg.addr;
918 info->pciConfigMirrorSize = device->pci->func->cfg.size;
919 r535_gsp_acpi_info(gsp, &info->acpiMethodData);
920
921 return nvkm_gsp_rpc_wr(gsp, info, NVKM_GSP_RPC_REPLY_NOWAIT);
922 }
923
924 static int
r535_gsp_msg_os_error_log(void * priv,u32 fn,void * repv,u32 repc)925 r535_gsp_msg_os_error_log(void *priv, u32 fn, void *repv, u32 repc)
926 {
927 struct nvkm_gsp *gsp = priv;
928 struct nvkm_subdev *subdev = &gsp->subdev;
929 rpc_os_error_log_v17_00 *msg = repv;
930
931 if (WARN_ON(repc < sizeof(*msg)))
932 return -EINVAL;
933
934 nvkm_error(subdev, "Xid:%d %s\n", msg->exceptType, msg->errString);
935 return 0;
936 }
937
938 static int
r535_gsp_msg_mmu_fault_queued(void * priv,u32 fn,void * repv,u32 repc)939 r535_gsp_msg_mmu_fault_queued(void *priv, u32 fn, void *repv, u32 repc)
940 {
941 struct nvkm_gsp *gsp = priv;
942 struct nvkm_subdev *subdev = &gsp->subdev;
943
944 WARN_ON(repc != 0);
945
946 nvkm_error(subdev, "mmu fault queued\n");
947 return 0;
948 }
949
950 static int
r535_gsp_msg_post_event(void * priv,u32 fn,void * repv,u32 repc)951 r535_gsp_msg_post_event(void *priv, u32 fn, void *repv, u32 repc)
952 {
953 struct nvkm_gsp *gsp = priv;
954 struct nvkm_gsp_client *client;
955 struct nvkm_subdev *subdev = &gsp->subdev;
956 rpc_post_event_v17_00 *msg = repv;
957
958 if (WARN_ON(repc < sizeof(*msg)))
959 return -EINVAL;
960 if (WARN_ON(repc != sizeof(*msg) + msg->eventDataSize))
961 return -EINVAL;
962
963 nvkm_debug(subdev, "event: %08x %08x %d %08x %08x %d %d\n",
964 msg->hClient, msg->hEvent, msg->notifyIndex, msg->data,
965 msg->status, msg->eventDataSize, msg->bNotifyList);
966
967 mutex_lock(&gsp->client_id.mutex);
968 client = idr_find(&gsp->client_id.idr, msg->hClient & 0xffff);
969 if (client) {
970 struct nvkm_gsp_event *event;
971 bool handled = false;
972
973 list_for_each_entry(event, &client->events, head) {
974 if (event->object.handle == msg->hEvent) {
975 event->func(event, msg->eventData, msg->eventDataSize);
976 handled = true;
977 }
978 }
979
980 if (!handled) {
981 nvkm_error(subdev, "event: cid 0x%08x event 0x%08x not found!\n",
982 msg->hClient, msg->hEvent);
983 }
984 } else {
985 nvkm_error(subdev, "event: cid 0x%08x not found!\n", msg->hClient);
986 }
987 mutex_unlock(&gsp->client_id.mutex);
988 return 0;
989 }
990
991 /**
992 * r535_gsp_msg_run_cpu_sequencer() -- process I/O commands from the GSP
993 * @priv: gsp pointer
994 * @fn: function number (ignored)
995 * @repv: pointer to libos print RPC
996 * @repc: message size
997 *
998 * The GSP sequencer is a list of I/O commands that the GSP can send to
999 * the driver to perform for various purposes. The most common usage is to
1000 * perform a special mid-initialization reset.
1001 */
1002 static int
r535_gsp_msg_run_cpu_sequencer(void * priv,u32 fn,void * repv,u32 repc)1003 r535_gsp_msg_run_cpu_sequencer(void *priv, u32 fn, void *repv, u32 repc)
1004 {
1005 struct nvkm_gsp *gsp = priv;
1006 struct nvkm_subdev *subdev = &gsp->subdev;
1007 struct nvkm_device *device = subdev->device;
1008 rpc_run_cpu_sequencer_v17_00 *seq = repv;
1009 int ptr = 0, ret;
1010
1011 nvkm_debug(subdev, "seq: %08x %08x\n", seq->bufferSizeDWord, seq->cmdIndex);
1012
1013 while (ptr < seq->cmdIndex) {
1014 GSP_SEQUENCER_BUFFER_CMD *cmd = (void *)&seq->commandBuffer[ptr];
1015
1016 ptr += 1;
1017 ptr += GSP_SEQUENCER_PAYLOAD_SIZE_DWORDS(cmd->opCode);
1018
1019 switch (cmd->opCode) {
1020 case GSP_SEQ_BUF_OPCODE_REG_WRITE: {
1021 u32 addr = cmd->payload.regWrite.addr;
1022 u32 data = cmd->payload.regWrite.val;
1023
1024 nvkm_trace(subdev, "seq wr32 %06x %08x\n", addr, data);
1025 nvkm_wr32(device, addr, data);
1026 }
1027 break;
1028 case GSP_SEQ_BUF_OPCODE_REG_MODIFY: {
1029 u32 addr = cmd->payload.regModify.addr;
1030 u32 mask = cmd->payload.regModify.mask;
1031 u32 data = cmd->payload.regModify.val;
1032
1033 nvkm_trace(subdev, "seq mask %06x %08x %08x\n", addr, mask, data);
1034 nvkm_mask(device, addr, mask, data);
1035 }
1036 break;
1037 case GSP_SEQ_BUF_OPCODE_REG_POLL: {
1038 u32 addr = cmd->payload.regPoll.addr;
1039 u32 mask = cmd->payload.regPoll.mask;
1040 u32 data = cmd->payload.regPoll.val;
1041 u32 usec = cmd->payload.regPoll.timeout ?: 4000000;
1042 //u32 error = cmd->payload.regPoll.error;
1043
1044 nvkm_trace(subdev, "seq poll %06x %08x %08x %d\n", addr, mask, data, usec);
1045 nvkm_rd32(device, addr);
1046 nvkm_usec(device, usec,
1047 if ((nvkm_rd32(device, addr) & mask) == data)
1048 break;
1049 );
1050 }
1051 break;
1052 case GSP_SEQ_BUF_OPCODE_DELAY_US: {
1053 u32 usec = cmd->payload.delayUs.val;
1054
1055 nvkm_trace(subdev, "seq usec %d\n", usec);
1056 udelay(usec);
1057 }
1058 break;
1059 case GSP_SEQ_BUF_OPCODE_REG_STORE: {
1060 u32 addr = cmd->payload.regStore.addr;
1061 u32 slot = cmd->payload.regStore.index;
1062
1063 seq->regSaveArea[slot] = nvkm_rd32(device, addr);
1064 nvkm_trace(subdev, "seq save %08x -> %d: %08x\n", addr, slot,
1065 seq->regSaveArea[slot]);
1066 }
1067 break;
1068 case GSP_SEQ_BUF_OPCODE_CORE_RESET:
1069 nvkm_trace(subdev, "seq core reset\n");
1070 nvkm_falcon_reset(&gsp->falcon);
1071 nvkm_falcon_mask(&gsp->falcon, 0x624, 0x00000080, 0x00000080);
1072 nvkm_falcon_wr32(&gsp->falcon, 0x10c, 0x00000000);
1073 break;
1074 case GSP_SEQ_BUF_OPCODE_CORE_START:
1075 nvkm_trace(subdev, "seq core start\n");
1076 if (nvkm_falcon_rd32(&gsp->falcon, 0x100) & 0x00000040)
1077 nvkm_falcon_wr32(&gsp->falcon, 0x130, 0x00000002);
1078 else
1079 nvkm_falcon_wr32(&gsp->falcon, 0x100, 0x00000002);
1080 break;
1081 case GSP_SEQ_BUF_OPCODE_CORE_WAIT_FOR_HALT:
1082 nvkm_trace(subdev, "seq core wait halt\n");
1083 nvkm_msec(device, 2000,
1084 if (nvkm_falcon_rd32(&gsp->falcon, 0x100) & 0x00000010)
1085 break;
1086 );
1087 break;
1088 case GSP_SEQ_BUF_OPCODE_CORE_RESUME: {
1089 struct nvkm_sec2 *sec2 = device->sec2;
1090 u32 mbox0;
1091
1092 nvkm_trace(subdev, "seq core resume\n");
1093
1094 ret = gsp->func->reset(gsp);
1095 if (WARN_ON(ret))
1096 return ret;
1097
1098 nvkm_falcon_wr32(&gsp->falcon, 0x040, lower_32_bits(gsp->libos.addr));
1099 nvkm_falcon_wr32(&gsp->falcon, 0x044, upper_32_bits(gsp->libos.addr));
1100
1101 nvkm_falcon_start(&sec2->falcon);
1102
1103 if (nvkm_msec(device, 2000,
1104 if (nvkm_rd32(device, 0x1180f8) & 0x04000000)
1105 break;
1106 ) < 0)
1107 return -ETIMEDOUT;
1108
1109 mbox0 = nvkm_falcon_rd32(&sec2->falcon, 0x040);
1110 if (WARN_ON(mbox0)) {
1111 nvkm_error(&gsp->subdev, "seq core resume sec2: 0x%x\n", mbox0);
1112 return -EIO;
1113 }
1114
1115 nvkm_falcon_wr32(&gsp->falcon, 0x080, gsp->boot.app_version);
1116
1117 if (WARN_ON(!nvkm_falcon_riscv_active(&gsp->falcon)))
1118 return -EIO;
1119 }
1120 break;
1121 default:
1122 nvkm_error(subdev, "unknown sequencer opcode %08x\n", cmd->opCode);
1123 return -EINVAL;
1124 }
1125 }
1126
1127 return 0;
1128 }
1129
1130 static int
r535_gsp_shared_init(struct nvkm_gsp * gsp)1131 r535_gsp_shared_init(struct nvkm_gsp *gsp)
1132 {
1133 struct {
1134 msgqTxHeader tx;
1135 msgqRxHeader rx;
1136 } *cmdq, *msgq;
1137 int ret, i;
1138
1139 gsp->shm.cmdq.size = 0x40000;
1140 gsp->shm.msgq.size = 0x40000;
1141
1142 gsp->shm.ptes.nr = (gsp->shm.cmdq.size + gsp->shm.msgq.size) >> GSP_PAGE_SHIFT;
1143 gsp->shm.ptes.nr += DIV_ROUND_UP(gsp->shm.ptes.nr * sizeof(u64), GSP_PAGE_SIZE);
1144 gsp->shm.ptes.size = ALIGN(gsp->shm.ptes.nr * sizeof(u64), GSP_PAGE_SIZE);
1145
1146 ret = nvkm_gsp_mem_ctor(gsp, gsp->shm.ptes.size +
1147 gsp->shm.cmdq.size +
1148 gsp->shm.msgq.size,
1149 &gsp->shm.mem);
1150 if (ret)
1151 return ret;
1152
1153 gsp->shm.ptes.ptr = gsp->shm.mem.data;
1154 gsp->shm.cmdq.ptr = (u8 *)gsp->shm.ptes.ptr + gsp->shm.ptes.size;
1155 gsp->shm.msgq.ptr = (u8 *)gsp->shm.cmdq.ptr + gsp->shm.cmdq.size;
1156
1157 for (i = 0; i < gsp->shm.ptes.nr; i++)
1158 gsp->shm.ptes.ptr[i] = gsp->shm.mem.addr + (i << GSP_PAGE_SHIFT);
1159
1160 cmdq = gsp->shm.cmdq.ptr;
1161 cmdq->tx.version = 0;
1162 cmdq->tx.size = gsp->shm.cmdq.size;
1163 cmdq->tx.entryOff = GSP_PAGE_SIZE;
1164 cmdq->tx.msgSize = GSP_PAGE_SIZE;
1165 cmdq->tx.msgCount = (cmdq->tx.size - cmdq->tx.entryOff) / cmdq->tx.msgSize;
1166 cmdq->tx.writePtr = 0;
1167 cmdq->tx.flags = 1;
1168 cmdq->tx.rxHdrOff = offsetof(typeof(*cmdq), rx.readPtr);
1169
1170 msgq = gsp->shm.msgq.ptr;
1171
1172 gsp->cmdq.cnt = cmdq->tx.msgCount;
1173 gsp->cmdq.wptr = &cmdq->tx.writePtr;
1174 gsp->cmdq.rptr = &msgq->rx.readPtr;
1175 gsp->msgq.cnt = cmdq->tx.msgCount;
1176 gsp->msgq.wptr = &msgq->tx.writePtr;
1177 gsp->msgq.rptr = &cmdq->rx.readPtr;
1178 return 0;
1179 }
1180
1181 static void
r535_gsp_set_rmargs(struct nvkm_gsp * gsp,bool resume)1182 r535_gsp_set_rmargs(struct nvkm_gsp *gsp, bool resume)
1183 {
1184 GSP_ARGUMENTS_CACHED *args = gsp->rmargs.data;
1185
1186 args->messageQueueInitArguments.sharedMemPhysAddr = gsp->shm.mem.addr;
1187 args->messageQueueInitArguments.pageTableEntryCount = gsp->shm.ptes.nr;
1188 args->messageQueueInitArguments.cmdQueueOffset =
1189 (u8 *)gsp->shm.cmdq.ptr - (u8 *)gsp->shm.mem.data;
1190 args->messageQueueInitArguments.statQueueOffset =
1191 (u8 *)gsp->shm.msgq.ptr - (u8 *)gsp->shm.mem.data;
1192
1193 if (!resume) {
1194 args->srInitArguments.oldLevel = 0;
1195 args->srInitArguments.flags = 0;
1196 args->srInitArguments.bInPMTransition = 0;
1197 } else {
1198 args->srInitArguments.oldLevel = NV2080_CTRL_GPU_SET_POWER_STATE_GPU_LEVEL_3;
1199 args->srInitArguments.flags = 0;
1200 args->srInitArguments.bInPMTransition = 1;
1201 }
1202 }
1203
1204 static int
r535_gsp_rmargs_init(struct nvkm_gsp * gsp,bool resume)1205 r535_gsp_rmargs_init(struct nvkm_gsp *gsp, bool resume)
1206 {
1207 int ret;
1208
1209 if (!resume) {
1210 ret = r535_gsp_shared_init(gsp);
1211 if (ret)
1212 return ret;
1213
1214 ret = nvkm_gsp_mem_ctor(gsp, 0x1000, &gsp->rmargs);
1215 if (ret)
1216 return ret;
1217 }
1218
1219 gsp->rm->api->gsp->set_rmargs(gsp, resume);
1220 return 0;
1221 }
1222
1223 #ifdef CONFIG_DEBUG_FS
1224
1225 /*
1226 * If GSP-RM load fails, then the GSP nvkm object will be deleted, the logging
1227 * debugfs entries will be deleted, and it will not be possible to debug the
1228 * load failure. The keep_gsp_logging parameter tells Nouveau to copy the
1229 * logging buffers to new debugfs entries, and these entries are retained
1230 * until the driver unloads.
1231 */
1232 static bool keep_gsp_logging;
1233 module_param(keep_gsp_logging, bool, 0444);
1234 MODULE_PARM_DESC(keep_gsp_logging,
1235 "Migrate the GSP-RM logging debugfs entries upon exit");
1236
1237 /*
1238 * GSP-RM uses a pseudo-class mechanism to define of a variety of per-"engine"
1239 * data structures, and each engine has a "class ID" genererated by a
1240 * pre-processor. This is the class ID for the PMU.
1241 */
1242 #define NV_GSP_MSG_EVENT_UCODE_LIBOS_CLASS_PMU 0xf3d722
1243
1244 /**
1245 * struct rpc_ucode_libos_print_v1e_08 - RPC payload for libos print buffers
1246 * @ucode_eng_desc: the engine descriptor
1247 * @libos_print_buf_size: the size of the libos_print_buf[]
1248 * @libos_print_buf: the actual buffer
1249 *
1250 * The engine descriptor is divided into 31:8 "class ID" and 7:0 "instance
1251 * ID". We only care about messages from PMU.
1252 */
1253 struct rpc_ucode_libos_print_v1e_08 {
1254 u32 ucode_eng_desc;
1255 u32 libos_print_buf_size;
1256 u8 libos_print_buf[];
1257 };
1258
1259 /**
1260 * r535_gsp_msg_libos_print - capture log message from the PMU
1261 * @priv: gsp pointer
1262 * @fn: function number (ignored)
1263 * @repv: pointer to libos print RPC
1264 * @repc: message size
1265 *
1266 * Called when we receive a UCODE_LIBOS_PRINT event RPC from GSP-RM. This RPC
1267 * contains the contents of the libos print buffer from PMU. It is typically
1268 * only written to when PMU encounters an error.
1269 *
1270 * Technically this RPC can be used to pass print buffers from any number of
1271 * GSP-RM engines, but we only expect to receive them for the PMU.
1272 *
1273 * For the PMU, the buffer is 4K in size and the RPC always contains the full
1274 * contents.
1275 */
1276 static int
r535_gsp_msg_libos_print(void * priv,u32 fn,void * repv,u32 repc)1277 r535_gsp_msg_libos_print(void *priv, u32 fn, void *repv, u32 repc)
1278 {
1279 struct nvkm_gsp *gsp = priv;
1280 struct nvkm_subdev *subdev = &gsp->subdev;
1281 struct rpc_ucode_libos_print_v1e_08 *rpc = repv;
1282 unsigned int class = rpc->ucode_eng_desc >> 8;
1283
1284 nvkm_debug(subdev, "received libos print from class 0x%x for %u bytes\n",
1285 class, rpc->libos_print_buf_size);
1286
1287 if (class != NV_GSP_MSG_EVENT_UCODE_LIBOS_CLASS_PMU) {
1288 nvkm_warn(subdev,
1289 "received libos print from unknown class 0x%x\n",
1290 class);
1291 return -ENOMSG;
1292 }
1293
1294 if (rpc->libos_print_buf_size > GSP_PAGE_SIZE) {
1295 nvkm_error(subdev, "libos print is too large (%u bytes)\n",
1296 rpc->libos_print_buf_size);
1297 return -E2BIG;
1298 }
1299
1300 memcpy(gsp->blob_pmu.data, rpc->libos_print_buf, rpc->libos_print_buf_size);
1301
1302 return 0;
1303 }
1304
1305 /**
1306 * create_debugfs - create a blob debugfs entry
1307 * @gsp: gsp pointer
1308 * @name: name of this dentry
1309 * @blob: blob wrapper
1310 *
1311 * Creates a debugfs entry for a logging buffer with the name 'name'.
1312 */
create_debugfs(struct nvkm_gsp * gsp,const char * name,struct debugfs_blob_wrapper * blob)1313 static struct dentry *create_debugfs(struct nvkm_gsp *gsp, const char *name,
1314 struct debugfs_blob_wrapper *blob)
1315 {
1316 struct dentry *dent;
1317
1318 dent = debugfs_create_blob(name, 0444, gsp->debugfs.parent, blob);
1319 if (IS_ERR(dent)) {
1320 nvkm_error(&gsp->subdev,
1321 "failed to create %s debugfs entry\n", name);
1322 return NULL;
1323 }
1324
1325 /*
1326 * For some reason, debugfs_create_blob doesn't set the size of the
1327 * dentry, so do that here. See [1]
1328 *
1329 * [1] https://lore.kernel.org/r/linux-fsdevel/20240207200619.3354549-1-ttabi@nvidia.com/
1330 */
1331 i_size_write(d_inode(dent), blob->size);
1332
1333 return dent;
1334 }
1335
1336 /**
1337 * r535_gsp_libos_debugfs_init - create logging debugfs entries
1338 * @gsp: gsp pointer
1339 *
1340 * Create the debugfs entries. This exposes the log buffers to userspace so
1341 * that an external tool can parse it.
1342 *
1343 * The 'logpmu' contains exception dumps from the PMU. It is written via an
1344 * RPC sent from GSP-RM and must be only 4KB. We create it here because it's
1345 * only useful if there is a debugfs entry to expose it. If we get the PMU
1346 * logging RPC and there is no debugfs entry, the RPC is just ignored.
1347 *
1348 * The blob_init, blob_rm, and blob_pmu objects can't be transient
1349 * because debugfs_create_blob doesn't copy them.
1350 *
1351 * NOTE: OpenRM loads the logging elf image and prints the log messages
1352 * in real-time. We may add that capability in the future, but that
1353 * requires loading ELF images that are not distributed with the driver and
1354 * adding the parsing code to Nouveau.
1355 *
1356 * Ideally, this should be part of nouveau_debugfs_init(), but that function
1357 * is called too late. We really want to create these debugfs entries before
1358 * r535_gsp_booter_load() is called, so that if GSP-RM fails to initialize,
1359 * there could still be a log to capture.
1360 */
1361 static void
r535_gsp_libos_debugfs_init(struct nvkm_gsp * gsp)1362 r535_gsp_libos_debugfs_init(struct nvkm_gsp *gsp)
1363 {
1364 struct device *dev = gsp->subdev.device->dev;
1365
1366 /* Create a new debugfs directory with a name unique to this GPU. */
1367 gsp->debugfs.parent = debugfs_create_dir(dev_name(dev), nouveau_debugfs_root);
1368 if (IS_ERR(gsp->debugfs.parent)) {
1369 nvkm_error(&gsp->subdev,
1370 "failed to create %s debugfs root\n", dev_name(dev));
1371 return;
1372 }
1373
1374 gsp->blob_init.data = gsp->loginit.data;
1375 gsp->blob_init.size = gsp->loginit.size;
1376 gsp->blob_intr.data = gsp->logintr.data;
1377 gsp->blob_intr.size = gsp->logintr.size;
1378 gsp->blob_rm.data = gsp->logrm.data;
1379 gsp->blob_rm.size = gsp->logrm.size;
1380
1381 gsp->debugfs.init = create_debugfs(gsp, "loginit", &gsp->blob_init);
1382 if (!gsp->debugfs.init)
1383 goto error;
1384
1385 gsp->debugfs.intr = create_debugfs(gsp, "logintr", &gsp->blob_intr);
1386 if (!gsp->debugfs.intr)
1387 goto error;
1388
1389 gsp->debugfs.rm = create_debugfs(gsp, "logrm", &gsp->blob_rm);
1390 if (!gsp->debugfs.rm)
1391 goto error;
1392
1393 /*
1394 * Since the PMU buffer is copied from an RPC, it doesn't need to be
1395 * a DMA buffer.
1396 */
1397 gsp->blob_pmu.size = GSP_PAGE_SIZE;
1398 gsp->blob_pmu.data = kzalloc(gsp->blob_pmu.size, GFP_KERNEL);
1399 if (!gsp->blob_pmu.data)
1400 goto error;
1401
1402 gsp->debugfs.pmu = create_debugfs(gsp, "logpmu", &gsp->blob_pmu);
1403 if (!gsp->debugfs.pmu) {
1404 kfree(gsp->blob_pmu.data);
1405 goto error;
1406 }
1407
1408 i_size_write(d_inode(gsp->debugfs.init), gsp->blob_init.size);
1409 i_size_write(d_inode(gsp->debugfs.intr), gsp->blob_intr.size);
1410 i_size_write(d_inode(gsp->debugfs.rm), gsp->blob_rm.size);
1411 i_size_write(d_inode(gsp->debugfs.pmu), gsp->blob_pmu.size);
1412
1413 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT,
1414 r535_gsp_msg_libos_print, gsp);
1415
1416 nvkm_debug(&gsp->subdev, "created debugfs GSP-RM logging entries\n");
1417
1418 if (keep_gsp_logging) {
1419 nvkm_info(&gsp->subdev,
1420 "logging buffers will be retained on failure\n");
1421 }
1422
1423 return;
1424
1425 error:
1426 debugfs_remove(gsp->debugfs.parent);
1427 gsp->debugfs.parent = NULL;
1428 }
1429
1430 #endif
1431
1432 static inline u64
r535_gsp_libos_id8(const char * name)1433 r535_gsp_libos_id8(const char *name)
1434 {
1435 u64 id = 0;
1436
1437 for (int i = 0; i < sizeof(id) && *name; i++, name++)
1438 id = (id << 8) | *name;
1439
1440 return id;
1441 }
1442
1443 /**
1444 * create_pte_array() - creates a PTE array of a physically contiguous buffer
1445 * @ptes: pointer to the array
1446 * @addr: base address of physically contiguous buffer (GSP_PAGE_SIZE aligned)
1447 * @size: size of the buffer
1448 *
1449 * GSP-RM sometimes expects physically-contiguous buffers to have an array of
1450 * "PTEs" for each page in that buffer. Although in theory that allows for
1451 * the buffer to be physically discontiguous, GSP-RM does not currently
1452 * support that.
1453 *
1454 * In this case, the PTEs are DMA addresses of each page of the buffer. Since
1455 * the buffer is physically contiguous, calculating all the PTEs is simple
1456 * math.
1457 *
1458 * See memdescGetPhysAddrsForGpu()
1459 */
create_pte_array(u64 * ptes,dma_addr_t addr,size_t size)1460 static void create_pte_array(u64 *ptes, dma_addr_t addr, size_t size)
1461 {
1462 unsigned int num_pages = DIV_ROUND_UP_ULL(size, GSP_PAGE_SIZE);
1463 unsigned int i;
1464
1465 for (i = 0; i < num_pages; i++)
1466 ptes[i] = (u64)addr + (i << GSP_PAGE_SHIFT);
1467 }
1468
1469 /**
1470 * r535_gsp_libos_init() -- create the libos arguments structure
1471 * @gsp: gsp pointer
1472 *
1473 * The logging buffers are byte queues that contain encoded printf-like
1474 * messages from GSP-RM. They need to be decoded by a special application
1475 * that can parse the buffers.
1476 *
1477 * The 'loginit' buffer contains logs from early GSP-RM init and
1478 * exception dumps. The 'logrm' buffer contains the subsequent logs. Both are
1479 * written to directly by GSP-RM and can be any multiple of GSP_PAGE_SIZE.
1480 *
1481 * The physical address map for the log buffer is stored in the buffer
1482 * itself, starting with offset 1. Offset 0 contains the "put" pointer (pp).
1483 * Initially, pp is equal to 0. If the buffer has valid logging data in it,
1484 * then pp points to index into the buffer where the next logging entry will
1485 * be written. Therefore, the logging data is valid if:
1486 * 1 <= pp < sizeof(buffer)/sizeof(u64)
1487 *
1488 * The GSP only understands 4K pages (GSP_PAGE_SIZE), so even if the kernel is
1489 * configured for a larger page size (e.g. 64K pages), we need to give
1490 * the GSP an array of 4K pages. Fortunately, since the buffer is
1491 * physically contiguous, it's simple math to calculate the addresses.
1492 *
1493 * The buffers must be a multiple of GSP_PAGE_SIZE. GSP-RM also currently
1494 * ignores the @kind field for LOGINIT, LOGINTR, and LOGRM, but expects the
1495 * buffers to be physically contiguous anyway.
1496 *
1497 * The memory allocated for the arguments must remain until the GSP sends the
1498 * init_done RPC.
1499 *
1500 * See _kgspInitLibosLoggingStructures (allocates memory for buffers)
1501 * See kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array)
1502 */
1503 static int
r535_gsp_libos_init(struct nvkm_gsp * gsp)1504 r535_gsp_libos_init(struct nvkm_gsp *gsp)
1505 {
1506 LibosMemoryRegionInitArgument *args;
1507 int ret;
1508
1509 ret = nvkm_gsp_mem_ctor(gsp, 0x1000, &gsp->libos);
1510 if (ret)
1511 return ret;
1512
1513 args = gsp->libos.data;
1514
1515 ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->loginit);
1516 if (ret)
1517 return ret;
1518
1519 args[0].id8 = r535_gsp_libos_id8("LOGINIT");
1520 args[0].pa = gsp->loginit.addr;
1521 args[0].size = gsp->loginit.size;
1522 args[0].kind = LIBOS_MEMORY_REGION_CONTIGUOUS;
1523 args[0].loc = LIBOS_MEMORY_REGION_LOC_SYSMEM;
1524 create_pte_array(gsp->loginit.data + sizeof(u64), gsp->loginit.addr, gsp->loginit.size);
1525
1526 ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->logintr);
1527 if (ret)
1528 return ret;
1529
1530 args[1].id8 = r535_gsp_libos_id8("LOGINTR");
1531 args[1].pa = gsp->logintr.addr;
1532 args[1].size = gsp->logintr.size;
1533 args[1].kind = LIBOS_MEMORY_REGION_CONTIGUOUS;
1534 args[1].loc = LIBOS_MEMORY_REGION_LOC_SYSMEM;
1535 create_pte_array(gsp->logintr.data + sizeof(u64), gsp->logintr.addr, gsp->logintr.size);
1536
1537 ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->logrm);
1538 if (ret)
1539 return ret;
1540
1541 args[2].id8 = r535_gsp_libos_id8("LOGRM");
1542 args[2].pa = gsp->logrm.addr;
1543 args[2].size = gsp->logrm.size;
1544 args[2].kind = LIBOS_MEMORY_REGION_CONTIGUOUS;
1545 args[2].loc = LIBOS_MEMORY_REGION_LOC_SYSMEM;
1546 create_pte_array(gsp->logrm.data + sizeof(u64), gsp->logrm.addr, gsp->logrm.size);
1547
1548 ret = r535_gsp_rmargs_init(gsp, false);
1549 if (ret)
1550 return ret;
1551
1552 args[3].id8 = r535_gsp_libos_id8("RMARGS");
1553 args[3].pa = gsp->rmargs.addr;
1554 args[3].size = gsp->rmargs.size;
1555 args[3].kind = LIBOS_MEMORY_REGION_CONTIGUOUS;
1556 args[3].loc = LIBOS_MEMORY_REGION_LOC_SYSMEM;
1557
1558 #ifdef CONFIG_DEBUG_FS
1559 r535_gsp_libos_debugfs_init(gsp);
1560 #endif
1561
1562 return 0;
1563 }
1564
1565 void
nvkm_gsp_sg_free(struct nvkm_device * device,struct sg_table * sgt)1566 nvkm_gsp_sg_free(struct nvkm_device *device, struct sg_table *sgt)
1567 {
1568 struct scatterlist *sgl;
1569 int i;
1570
1571 dma_unmap_sgtable(device->dev, sgt, DMA_BIDIRECTIONAL, 0);
1572
1573 for_each_sgtable_sg(sgt, sgl, i) {
1574 struct page *page = sg_page(sgl);
1575
1576 __free_page(page);
1577 }
1578
1579 sg_free_table(sgt);
1580 }
1581
1582 int
nvkm_gsp_sg(struct nvkm_device * device,u64 size,struct sg_table * sgt)1583 nvkm_gsp_sg(struct nvkm_device *device, u64 size, struct sg_table *sgt)
1584 {
1585 const u64 pages = DIV_ROUND_UP(size, PAGE_SIZE);
1586 struct scatterlist *sgl;
1587 int ret, i;
1588
1589 ret = sg_alloc_table(sgt, pages, GFP_KERNEL);
1590 if (ret)
1591 return ret;
1592
1593 for_each_sgtable_sg(sgt, sgl, i) {
1594 struct page *page = alloc_page(GFP_KERNEL);
1595
1596 if (!page) {
1597 nvkm_gsp_sg_free(device, sgt);
1598 return -ENOMEM;
1599 }
1600
1601 sg_set_page(sgl, page, PAGE_SIZE, 0);
1602 }
1603
1604 ret = dma_map_sgtable(device->dev, sgt, DMA_BIDIRECTIONAL, 0);
1605 if (ret)
1606 nvkm_gsp_sg_free(device, sgt);
1607
1608 return ret;
1609 }
1610
1611 static void
nvkm_gsp_radix3_dtor(struct nvkm_gsp * gsp,struct nvkm_gsp_radix3 * rx3)1612 nvkm_gsp_radix3_dtor(struct nvkm_gsp *gsp, struct nvkm_gsp_radix3 *rx3)
1613 {
1614 nvkm_gsp_sg_free(gsp->subdev.device, &rx3->lvl2);
1615 nvkm_gsp_mem_dtor(&rx3->lvl1);
1616 nvkm_gsp_mem_dtor(&rx3->lvl0);
1617 }
1618
1619 /**
1620 * nvkm_gsp_radix3_sg - build a radix3 table from a S/G list
1621 * @gsp: gsp pointer
1622 * @sgt: S/G list to traverse
1623 * @size: size of the image, in bytes
1624 * @rx3: radix3 array to update
1625 *
1626 * The GSP uses a three-level page table, called radix3, to map the firmware.
1627 * Each 64-bit "pointer" in the table is either the bus address of an entry in
1628 * the next table (for levels 0 and 1) or the bus address of the next page in
1629 * the GSP firmware image itself.
1630 *
1631 * Level 0 contains a single entry in one page that points to the first page
1632 * of level 1.
1633 *
1634 * Level 1, since it's also only one page in size, contains up to 512 entries,
1635 * one for each page in Level 2.
1636 *
1637 * Level 2 can be up to 512 pages in size, and each of those entries points to
1638 * the next page of the firmware image. Since there can be up to 512*512
1639 * pages, that limits the size of the firmware to 512*512*GSP_PAGE_SIZE = 1GB.
1640 *
1641 * Internally, the GSP has its window into system memory, but the base
1642 * physical address of the aperture is not 0. In fact, it varies depending on
1643 * the GPU architecture. Since the GPU is a PCI device, this window is
1644 * accessed via DMA and is therefore bound by IOMMU translation. The end
1645 * result is that GSP-RM must translate the bus addresses in the table to GSP
1646 * physical addresses. All this should happen transparently.
1647 *
1648 * Returns 0 on success, or negative error code
1649 *
1650 * See kgspCreateRadix3_IMPL
1651 */
1652 static int
nvkm_gsp_radix3_sg(struct nvkm_gsp * gsp,struct sg_table * sgt,u64 size,struct nvkm_gsp_radix3 * rx3)1653 nvkm_gsp_radix3_sg(struct nvkm_gsp *gsp, struct sg_table *sgt, u64 size,
1654 struct nvkm_gsp_radix3 *rx3)
1655 {
1656 struct sg_dma_page_iter sg_dma_iter;
1657 struct scatterlist *sg;
1658 size_t bufsize;
1659 u64 *pte;
1660 int ret, i, page_idx = 0;
1661
1662 ret = nvkm_gsp_mem_ctor(gsp, GSP_PAGE_SIZE, &rx3->lvl0);
1663 if (ret)
1664 return ret;
1665
1666 ret = nvkm_gsp_mem_ctor(gsp, GSP_PAGE_SIZE, &rx3->lvl1);
1667 if (ret)
1668 goto lvl1_fail;
1669
1670 // Allocate level 2
1671 bufsize = ALIGN((size / GSP_PAGE_SIZE) * sizeof(u64), GSP_PAGE_SIZE);
1672 ret = nvkm_gsp_sg(gsp->subdev.device, bufsize, &rx3->lvl2);
1673 if (ret)
1674 goto lvl2_fail;
1675
1676 // Write the bus address of level 1 to level 0
1677 pte = rx3->lvl0.data;
1678 *pte = rx3->lvl1.addr;
1679
1680 // Write the bus address of each page in level 2 to level 1
1681 pte = rx3->lvl1.data;
1682 for_each_sgtable_dma_page(&rx3->lvl2, &sg_dma_iter, 0)
1683 *pte++ = sg_page_iter_dma_address(&sg_dma_iter);
1684
1685 // Finally, write the bus address of each page in sgt to level 2
1686 for_each_sgtable_sg(&rx3->lvl2, sg, i) {
1687 void *sgl_end;
1688
1689 pte = sg_virt(sg);
1690 sgl_end = (void *)pte + sg->length;
1691
1692 for_each_sgtable_dma_page(sgt, &sg_dma_iter, page_idx) {
1693 *pte++ = sg_page_iter_dma_address(&sg_dma_iter);
1694 page_idx++;
1695
1696 // Go to the next scatterlist for level 2 if we've reached the end
1697 if ((void *)pte >= sgl_end)
1698 break;
1699 }
1700 }
1701
1702 if (ret) {
1703 lvl2_fail:
1704 nvkm_gsp_mem_dtor(&rx3->lvl1);
1705 lvl1_fail:
1706 nvkm_gsp_mem_dtor(&rx3->lvl0);
1707 }
1708
1709 return ret;
1710 }
1711
1712 static u32
r535_gsp_sr_data_size(struct nvkm_gsp * gsp)1713 r535_gsp_sr_data_size(struct nvkm_gsp *gsp)
1714 {
1715 GspFwWprMeta *meta = gsp->wpr_meta.data;
1716
1717 return meta->gspFwWprEnd - meta->gspFwWprStart;
1718 }
1719
1720 int
r535_gsp_fini(struct nvkm_gsp * gsp,bool suspend)1721 r535_gsp_fini(struct nvkm_gsp *gsp, bool suspend)
1722 {
1723 struct nvkm_rm *rm = gsp->rm;
1724 int ret;
1725
1726 if (suspend) {
1727 u32 len = rm->api->gsp->sr_data_size(gsp);
1728 GspFwSRMeta *sr;
1729
1730 ret = nvkm_gsp_sg(gsp->subdev.device, len, &gsp->sr.sgt);
1731 if (ret)
1732 return ret;
1733
1734 ret = nvkm_gsp_radix3_sg(gsp, &gsp->sr.sgt, len, &gsp->sr.radix3);
1735 if (ret)
1736 return ret;
1737
1738 ret = nvkm_gsp_mem_ctor(gsp, sizeof(*sr), &gsp->sr.meta);
1739 if (ret)
1740 return ret;
1741
1742 sr = gsp->sr.meta.data;
1743 sr->magic = GSP_FW_SR_META_MAGIC;
1744 sr->revision = GSP_FW_SR_META_REVISION;
1745 sr->sysmemAddrOfSuspendResumeData = gsp->sr.radix3.lvl0.addr;
1746 sr->sizeOfSuspendResumeData = len;
1747
1748 ret = rm->api->fbsr->suspend(gsp);
1749 if (ret) {
1750 nvkm_gsp_mem_dtor(&gsp->sr.meta);
1751 nvkm_gsp_radix3_dtor(gsp, &gsp->sr.radix3);
1752 nvkm_gsp_sg_free(gsp->subdev.device, &gsp->sr.sgt);
1753 return ret;
1754 }
1755
1756 /*
1757 * TODO: Debug the GSP firmware / RPC handling to find out why
1758 * without this Turing (but none of the other architectures)
1759 * ends up resetting all channels after resume.
1760 */
1761 msleep(50);
1762 }
1763
1764 ret = r535_gsp_rpc_unloading_guest_driver(gsp, suspend);
1765 if (WARN_ON(ret))
1766 return ret;
1767
1768 nvkm_msec(gsp->subdev.device, 2000,
1769 if (nvkm_falcon_rd32(&gsp->falcon, 0x040) == 0x80000000)
1770 break;
1771 );
1772
1773 gsp->running = false;
1774 return 0;
1775 }
1776
1777 int
r535_gsp_init(struct nvkm_gsp * gsp)1778 r535_gsp_init(struct nvkm_gsp *gsp)
1779 {
1780 int ret;
1781
1782 nvkm_falcon_wr32(&gsp->falcon, 0x080, gsp->boot.app_version);
1783
1784 if (WARN_ON(!nvkm_falcon_riscv_active(&gsp->falcon)))
1785 return -EIO;
1786
1787 ret = r535_gsp_rpc_poll(gsp, NV_VGPU_MSG_EVENT_GSP_INIT_DONE);
1788 if (ret)
1789 goto done;
1790
1791 gsp->running = true;
1792
1793 done:
1794 if (gsp->sr.meta.data) {
1795 gsp->rm->api->fbsr->resume(gsp);
1796
1797 nvkm_gsp_mem_dtor(&gsp->sr.meta);
1798 nvkm_gsp_radix3_dtor(gsp, &gsp->sr.radix3);
1799 nvkm_gsp_sg_free(gsp->subdev.device, &gsp->sr.sgt);
1800 return ret;
1801 }
1802
1803 if (ret == 0)
1804 ret = r535_gsp_postinit(gsp);
1805
1806 return ret;
1807 }
1808
1809 static int
r535_gsp_rm_boot_ctor(struct nvkm_gsp * gsp)1810 r535_gsp_rm_boot_ctor(struct nvkm_gsp *gsp)
1811 {
1812 const struct firmware *fw = gsp->fws.bl;
1813 const struct nvfw_bin_hdr *hdr;
1814 RM_RISCV_UCODE_DESC *desc;
1815 int ret;
1816
1817 hdr = nvfw_bin_hdr(&gsp->subdev, fw->data);
1818 desc = (void *)fw->data + hdr->header_offset;
1819
1820 ret = nvkm_gsp_mem_ctor(gsp, hdr->data_size, &gsp->boot.fw);
1821 if (ret)
1822 return ret;
1823
1824 memcpy(gsp->boot.fw.data, fw->data + hdr->data_offset, hdr->data_size);
1825
1826 gsp->boot.code_offset = desc->monitorCodeOffset;
1827 gsp->boot.data_offset = desc->monitorDataOffset;
1828 gsp->boot.manifest_offset = desc->manifestOffset;
1829 gsp->boot.app_version = desc->appVersion;
1830 return 0;
1831 }
1832
1833 static const struct nvkm_firmware_func
1834 r535_gsp_fw = {
1835 .type = NVKM_FIRMWARE_IMG_SGT,
1836 };
1837
1838 static int
r535_gsp_elf_section(struct nvkm_gsp * gsp,const char * name,const u8 ** pdata,u64 * psize)1839 r535_gsp_elf_section(struct nvkm_gsp *gsp, const char *name, const u8 **pdata, u64 *psize)
1840 {
1841 const u8 *img = gsp->fws.rm->data;
1842 const struct elf64_hdr *ehdr = (const struct elf64_hdr *)img;
1843 const struct elf64_shdr *shdr = (const struct elf64_shdr *)&img[ehdr->e_shoff];
1844 const char *names = &img[shdr[ehdr->e_shstrndx].sh_offset];
1845
1846 for (int i = 0; i < ehdr->e_shnum; i++, shdr++) {
1847 if (!strcmp(&names[shdr->sh_name], name)) {
1848 *pdata = &img[shdr->sh_offset];
1849 *psize = shdr->sh_size;
1850 return 0;
1851 }
1852 }
1853
1854 nvkm_error(&gsp->subdev, "section '%s' not found\n", name);
1855 return -ENOENT;
1856 }
1857
1858 #ifdef CONFIG_DEBUG_FS
1859
1860 struct r535_gsp_log {
1861 struct nvif_log log;
1862
1863 /*
1864 * Logging buffers in debugfs. The wrapper objects need to remain
1865 * in memory until the dentry is deleted.
1866 */
1867 struct dentry *debugfs_logging_dir;
1868 struct debugfs_blob_wrapper blob_init;
1869 struct debugfs_blob_wrapper blob_intr;
1870 struct debugfs_blob_wrapper blob_rm;
1871 struct debugfs_blob_wrapper blob_pmu;
1872 };
1873
1874 /**
1875 * r535_debugfs_shutdown - delete GSP-RM logging buffers for one GPU
1876 * @_log: nvif_log struct for this GPU
1877 *
1878 * Called when the driver is shutting down, to clean up the retained GSP-RM
1879 * logging buffers.
1880 */
r535_debugfs_shutdown(struct nvif_log * _log)1881 static void r535_debugfs_shutdown(struct nvif_log *_log)
1882 {
1883 struct r535_gsp_log *log = container_of(_log, struct r535_gsp_log, log);
1884
1885 debugfs_remove(log->debugfs_logging_dir);
1886
1887 kfree(log->blob_init.data);
1888 kfree(log->blob_intr.data);
1889 kfree(log->blob_rm.data);
1890 kfree(log->blob_pmu.data);
1891
1892 /* We also need to delete the list object */
1893 kfree(log);
1894 }
1895
1896 /**
1897 * is_empty - return true if the logging buffer was never written to
1898 * @b: blob wrapper with ->data field pointing to logging buffer
1899 *
1900 * The first 64-bit field of loginit, and logintr, and logrm is the 'put'
1901 * pointer, and it is initialized to 0. It's a dword-based index into the
1902 * circular buffer, indicating where the next printf write will be made.
1903 *
1904 * If the pointer is still 0 when GSP-RM is shut down, that means that the
1905 * buffer was never written to, so it can be ignored.
1906 *
1907 * This test also works for logpmu, even though it doesn't have a put pointer.
1908 */
is_empty(const struct debugfs_blob_wrapper * b)1909 static bool is_empty(const struct debugfs_blob_wrapper *b)
1910 {
1911 u64 *put = b->data;
1912
1913 return put ? (*put == 0) : true;
1914 }
1915
1916 /**
1917 * r535_gsp_copy_log - preserve the logging buffers in a blob
1918 * @parent: the top-level dentry for this GPU
1919 * @name: name of debugfs entry to create
1920 * @s: original wrapper object to copy from
1921 * @t: new wrapper object to copy to
1922 *
1923 * When GSP shuts down, the nvkm_gsp object and all its memory is deleted.
1924 * To preserve the logging buffers, the buffers need to be copied, but only
1925 * if they actually have data.
1926 */
r535_gsp_copy_log(struct dentry * parent,const char * name,const struct debugfs_blob_wrapper * s,struct debugfs_blob_wrapper * t)1927 static int r535_gsp_copy_log(struct dentry *parent,
1928 const char *name,
1929 const struct debugfs_blob_wrapper *s,
1930 struct debugfs_blob_wrapper *t)
1931 {
1932 struct dentry *dent;
1933 void *p;
1934
1935 if (is_empty(s))
1936 return 0;
1937
1938 /* The original buffers will be deleted */
1939 p = kmemdup(s->data, s->size, GFP_KERNEL);
1940 if (!p)
1941 return -ENOMEM;
1942
1943 t->data = p;
1944 t->size = s->size;
1945
1946 dent = debugfs_create_blob(name, 0444, parent, t);
1947 if (IS_ERR(dent)) {
1948 kfree(p);
1949 memset(t, 0, sizeof(*t));
1950 return PTR_ERR(dent);
1951 }
1952
1953 i_size_write(d_inode(dent), t->size);
1954
1955 return 0;
1956 }
1957
1958 /**
1959 * r535_gsp_retain_logging - copy logging buffers to new debugfs root
1960 * @gsp: gsp pointer
1961 *
1962 * If keep_gsp_logging is enabled, then we want to preserve the GSP-RM logging
1963 * buffers and their debugfs entries, but all those objects would normally
1964 * deleted if GSP-RM fails to load.
1965 *
1966 * To preserve the logging buffers, we need to:
1967 *
1968 * 1) Allocate new buffers and copy the logs into them, so that the original
1969 * DMA buffers can be released.
1970 *
1971 * 2) Preserve the directories. We don't need to save single dentries because
1972 * we're going to delete the parent when the
1973 *
1974 * If anything fails in this process, then all the dentries need to be
1975 * deleted. We don't need to deallocate the original logging buffers because
1976 * the caller will do that regardless.
1977 */
r535_gsp_retain_logging(struct nvkm_gsp * gsp)1978 static void r535_gsp_retain_logging(struct nvkm_gsp *gsp)
1979 {
1980 struct device *dev = gsp->subdev.device->dev;
1981 struct r535_gsp_log *log = NULL;
1982 int ret;
1983
1984 if (!keep_gsp_logging || !gsp->debugfs.parent) {
1985 /* Nothing to do */
1986 goto exit;
1987 }
1988
1989 /* Check to make sure at least one buffer has data. */
1990 if (is_empty(&gsp->blob_init) && is_empty(&gsp->blob_intr) &&
1991 is_empty(&gsp->blob_rm) && is_empty(&gsp->blob_rm)) {
1992 nvkm_warn(&gsp->subdev, "all logging buffers are empty\n");
1993 goto exit;
1994 }
1995
1996 log = kzalloc(sizeof(*log), GFP_KERNEL);
1997 if (!log)
1998 goto error;
1999
2000 /*
2001 * Since the nvkm_gsp object is going away, the debugfs_blob_wrapper
2002 * objects are also being deleted, which means the dentries will no
2003 * longer be valid. Delete the existing entries so that we can create
2004 * new ones with the same name.
2005 */
2006 debugfs_remove(gsp->debugfs.init);
2007 debugfs_remove(gsp->debugfs.intr);
2008 debugfs_remove(gsp->debugfs.rm);
2009 debugfs_remove(gsp->debugfs.pmu);
2010
2011 ret = r535_gsp_copy_log(gsp->debugfs.parent, "loginit", &gsp->blob_init, &log->blob_init);
2012 if (ret)
2013 goto error;
2014
2015 ret = r535_gsp_copy_log(gsp->debugfs.parent, "logintr", &gsp->blob_intr, &log->blob_intr);
2016 if (ret)
2017 goto error;
2018
2019 ret = r535_gsp_copy_log(gsp->debugfs.parent, "logrm", &gsp->blob_rm, &log->blob_rm);
2020 if (ret)
2021 goto error;
2022
2023 ret = r535_gsp_copy_log(gsp->debugfs.parent, "logpmu", &gsp->blob_pmu, &log->blob_pmu);
2024 if (ret)
2025 goto error;
2026
2027 /* The nvkm_gsp object is going away, so save the dentry */
2028 log->debugfs_logging_dir = gsp->debugfs.parent;
2029
2030 log->log.shutdown = r535_debugfs_shutdown;
2031 list_add(&log->log.entry, &gsp_logs.head);
2032
2033 nvkm_warn(&gsp->subdev,
2034 "logging buffers migrated to /sys/kernel/debug/nouveau/%s\n",
2035 dev_name(dev));
2036
2037 return;
2038
2039 error:
2040 nvkm_warn(&gsp->subdev, "failed to migrate logging buffers\n");
2041
2042 exit:
2043 debugfs_remove(gsp->debugfs.parent);
2044
2045 if (log) {
2046 kfree(log->blob_init.data);
2047 kfree(log->blob_intr.data);
2048 kfree(log->blob_rm.data);
2049 kfree(log->blob_pmu.data);
2050 kfree(log);
2051 }
2052 }
2053
2054 #endif
2055
2056 /**
2057 * r535_gsp_libos_debugfs_fini - cleanup/retain log buffers on shutdown
2058 * @gsp: gsp pointer
2059 *
2060 * If the log buffers are exposed via debugfs, the data for those entries
2061 * needs to be cleaned up when the GSP device shuts down.
2062 */
2063 static void
r535_gsp_libos_debugfs_fini(struct nvkm_gsp __maybe_unused * gsp)2064 r535_gsp_libos_debugfs_fini(struct nvkm_gsp __maybe_unused *gsp)
2065 {
2066 #ifdef CONFIG_DEBUG_FS
2067 r535_gsp_retain_logging(gsp);
2068
2069 /*
2070 * Unlike the other buffers, the PMU blob is a kmalloc'd buffer that
2071 * exists only if the debugfs entries were created.
2072 */
2073 kfree(gsp->blob_pmu.data);
2074 gsp->blob_pmu.data = NULL;
2075 #endif
2076 }
2077
2078 void
r535_gsp_dtor(struct nvkm_gsp * gsp)2079 r535_gsp_dtor(struct nvkm_gsp *gsp)
2080 {
2081 idr_destroy(&gsp->client_id.idr);
2082 mutex_destroy(&gsp->client_id.mutex);
2083
2084 nvkm_gsp_radix3_dtor(gsp, &gsp->radix3);
2085 nvkm_gsp_mem_dtor(&gsp->sig);
2086 nvkm_firmware_dtor(&gsp->fw);
2087
2088 nvkm_falcon_fw_dtor(&gsp->booter.unload);
2089 nvkm_falcon_fw_dtor(&gsp->booter.load);
2090
2091 nvkm_gsp_mem_dtor(&gsp->fmc.args);
2092 kfree(gsp->fmc.sig);
2093 kfree(gsp->fmc.pkey);
2094 kfree(gsp->fmc.hash);
2095 nvkm_gsp_mem_dtor(&gsp->fmc.fw);
2096
2097 mutex_destroy(&gsp->msgq.mutex);
2098 mutex_destroy(&gsp->cmdq.mutex);
2099
2100 nvkm_gsp_dtor_fws(gsp);
2101
2102 nvkm_gsp_mem_dtor(&gsp->rmargs);
2103 nvkm_gsp_mem_dtor(&gsp->wpr_meta);
2104 nvkm_gsp_mem_dtor(&gsp->shm.mem);
2105
2106 r535_gsp_libos_debugfs_fini(gsp);
2107
2108 nvkm_gsp_mem_dtor(&gsp->loginit);
2109 nvkm_gsp_mem_dtor(&gsp->logintr);
2110 nvkm_gsp_mem_dtor(&gsp->logrm);
2111 }
2112
2113 static void
r535_gsp_drop_send_user_shared_data(struct nvkm_gsp * gsp)2114 r535_gsp_drop_send_user_shared_data(struct nvkm_gsp *gsp)
2115 {
2116 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_GSP_SEND_USER_SHARED_DATA, NULL, NULL);
2117 }
2118
2119 int
r535_gsp_oneinit(struct nvkm_gsp * gsp)2120 r535_gsp_oneinit(struct nvkm_gsp *gsp)
2121 {
2122 struct nvkm_device *device = gsp->subdev.device;
2123 const struct nvkm_rm_api *rmapi = gsp->rm->api;
2124 const u8 *data;
2125 u64 size;
2126 int ret;
2127
2128 mutex_init(&gsp->cmdq.mutex);
2129 mutex_init(&gsp->msgq.mutex);
2130
2131 /* Load GSP firmware from ELF image into DMA-accessible memory. */
2132 ret = r535_gsp_elf_section(gsp, ".fwimage", &data, &size);
2133 if (ret)
2134 return ret;
2135
2136 ret = nvkm_firmware_ctor(&r535_gsp_fw, "gsp-rm", device, data, size, &gsp->fw);
2137 if (ret)
2138 return ret;
2139
2140 /* Load relevant signature from ELF image. */
2141 ret = r535_gsp_elf_section(gsp, gsp->func->sig_section, &data, &size);
2142 if (ret)
2143 return ret;
2144
2145 ret = nvkm_gsp_mem_ctor(gsp, ALIGN(size, 256), &gsp->sig);
2146 if (ret)
2147 return ret;
2148
2149 memcpy(gsp->sig.data, data, size);
2150
2151 /* Build radix3 page table for ELF image. */
2152 ret = nvkm_gsp_radix3_sg(gsp, &gsp->fw.mem.sgt, gsp->fw.len, &gsp->radix3);
2153 if (ret)
2154 return ret;
2155
2156 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER,
2157 r535_gsp_msg_run_cpu_sequencer, gsp);
2158 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_POST_EVENT, r535_gsp_msg_post_event, gsp);
2159 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_RC_TRIGGERED, rmapi->fifo->rc_triggered, gsp);
2160 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED,
2161 r535_gsp_msg_mmu_fault_queued, gsp);
2162 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_OS_ERROR_LOG, r535_gsp_msg_os_error_log, gsp);
2163 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_PERF_BRIDGELESS_INFO_UPDATE, NULL, NULL);
2164 r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT, NULL, NULL);
2165 if (rmapi->gsp->drop_send_user_shared_data)
2166 rmapi->gsp->drop_send_user_shared_data(gsp);
2167 if (rmapi->gsp->drop_post_nocat_record)
2168 rmapi->gsp->drop_post_nocat_record(gsp);
2169
2170 ret = r535_gsp_rm_boot_ctor(gsp);
2171 if (ret)
2172 return ret;
2173
2174 /* Release FW images - we've copied them to DMA buffers now. */
2175 nvkm_gsp_dtor_fws(gsp);
2176
2177 ret = r535_gsp_libos_init(gsp);
2178 if (WARN_ON(ret))
2179 return ret;
2180
2181 ret = rmapi->gsp->set_system_info(gsp);
2182 if (WARN_ON(ret))
2183 return ret;
2184
2185 ret = r535_gsp_rpc_set_registry(gsp);
2186 if (WARN_ON(ret))
2187 return ret;
2188
2189 mutex_init(&gsp->client_id.mutex);
2190 idr_init(&gsp->client_id.idr);
2191 return 0;
2192 }
2193
2194 const struct nvkm_rm_api_gsp
2195 r535_gsp = {
2196 .set_rmargs = r535_gsp_set_rmargs,
2197 .set_system_info = r535_gsp_set_system_info,
2198 .get_static_info = r535_gsp_get_static_info,
2199 .xlat_mc_engine_idx = r535_gsp_xlat_mc_engine_idx,
2200 .drop_send_user_shared_data = r535_gsp_drop_send_user_shared_data,
2201 .sr_data_size = r535_gsp_sr_data_size,
2202 };
2203