xref: /linux/drivers/gpu/drm/nouveau/nvkm/subdev/gsp/r535.c (revision 2c1ed907520c50326b8f604907a8478b27881a2e)
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 "priv.h"
23 
24 #include <core/pci.h>
25 #include <subdev/timer.h>
26 #include <subdev/vfn.h>
27 #include <engine/fifo/chan.h>
28 #include <engine/sec2.h>
29 #include <nvif/log.h>
30 
31 #include <nvfw/fw.h>
32 
33 #include <nvrm/nvtypes.h>
34 #include <nvrm/535.113.01/common/sdk/nvidia/inc/class/cl0000.h>
35 #include <nvrm/535.113.01/common/sdk/nvidia/inc/class/cl0005.h>
36 #include <nvrm/535.113.01/common/sdk/nvidia/inc/class/cl0080.h>
37 #include <nvrm/535.113.01/common/sdk/nvidia/inc/class/cl2080.h>
38 #include <nvrm/535.113.01/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080event.h>
39 #include <nvrm/535.113.01/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080gpu.h>
40 #include <nvrm/535.113.01/common/sdk/nvidia/inc/ctrl/ctrl2080/ctrl2080internal.h>
41 #include <nvrm/535.113.01/common/sdk/nvidia/inc/nvos.h>
42 #include <nvrm/535.113.01/common/shared/msgq/inc/msgq/msgq_priv.h>
43 #include <nvrm/535.113.01/common/uproc/os/common/include/libos_init_args.h>
44 #include <nvrm/535.113.01/nvidia/arch/nvalloc/common/inc/gsp/gsp_fw_sr_meta.h>
45 #include <nvrm/535.113.01/nvidia/arch/nvalloc/common/inc/gsp/gsp_fw_wpr_meta.h>
46 #include <nvrm/535.113.01/nvidia/arch/nvalloc/common/inc/rmRiscvUcode.h>
47 #include <nvrm/535.113.01/nvidia/arch/nvalloc/common/inc/rmgspseq.h>
48 #include <nvrm/535.113.01/nvidia/generated/g_allclasses.h>
49 #include <nvrm/535.113.01/nvidia/generated/g_os_nvoc.h>
50 #include <nvrm/535.113.01/nvidia/generated/g_rpc-structures.h>
51 #include <nvrm/535.113.01/nvidia/inc/kernel/gpu/gsp/gsp_fw_heap.h>
52 #include <nvrm/535.113.01/nvidia/inc/kernel/gpu/gsp/gsp_init_args.h>
53 #include <nvrm/535.113.01/nvidia/inc/kernel/gpu/gsp/gsp_static_config.h>
54 #include <nvrm/535.113.01/nvidia/inc/kernel/gpu/intr/engine_idx.h>
55 #include <nvrm/535.113.01/nvidia/kernel/inc/vgpu/rpc_global_enums.h>
56 
57 #include <linux/acpi.h>
58 #include <linux/ctype.h>
59 #include <linux/parser.h>
60 
61 extern struct dentry *nouveau_debugfs_root;
62 
63 #define GSP_MSG_MIN_SIZE GSP_PAGE_SIZE
64 #define GSP_MSG_MAX_SIZE GSP_PAGE_MIN_SIZE * 16
65 
66 struct r535_gsp_msg {
67 	u8 auth_tag_buffer[16];
68 	u8 aad_buffer[16];
69 	u32 checksum;
70 	u32 sequence;
71 	u32 elem_count;
72 	u32 pad;
73 	u8  data[];
74 };
75 
76 #define GSP_MSG_HDR_SIZE offsetof(struct r535_gsp_msg, data)
77 
78 static int
r535_rpc_status_to_errno(uint32_t rpc_status)79 r535_rpc_status_to_errno(uint32_t rpc_status)
80 {
81 	switch (rpc_status) {
82 	case 0x55: /* NV_ERR_NOT_READY */
83 	case 0x66: /* NV_ERR_TIMEOUT_RETRY */
84 		return -EBUSY;
85 	case 0x51: /* NV_ERR_NO_MEMORY */
86 		return -ENOMEM;
87 	default:
88 		return -EINVAL;
89 	}
90 }
91 
92 static void *
r535_gsp_msgq_wait(struct nvkm_gsp * gsp,u32 repc,u32 * prepc,int * ptime)93 r535_gsp_msgq_wait(struct nvkm_gsp *gsp, u32 repc, u32 *prepc, int *ptime)
94 {
95 	struct r535_gsp_msg *mqe;
96 	u32 size, rptr = *gsp->msgq.rptr;
97 	int used;
98 	u8 *msg;
99 	u32 len;
100 
101 	size = DIV_ROUND_UP(GSP_MSG_HDR_SIZE + repc, GSP_PAGE_SIZE);
102 	if (WARN_ON(!size || size >= gsp->msgq.cnt))
103 		return ERR_PTR(-EINVAL);
104 
105 	do {
106 		u32 wptr = *gsp->msgq.wptr;
107 
108 		used = wptr + gsp->msgq.cnt - rptr;
109 		if (used >= gsp->msgq.cnt)
110 			used -= gsp->msgq.cnt;
111 		if (used >= size)
112 			break;
113 
114 		usleep_range(1, 2);
115 	} while (--(*ptime));
116 
117 	if (WARN_ON(!*ptime))
118 		return ERR_PTR(-ETIMEDOUT);
119 
120 	mqe = (void *)((u8 *)gsp->shm.msgq.ptr + 0x1000 + rptr * 0x1000);
121 
122 	if (prepc) {
123 		*prepc = (used * GSP_PAGE_SIZE) - sizeof(*mqe);
124 		return mqe->data;
125 	}
126 
127 	size = ALIGN(repc + GSP_MSG_HDR_SIZE, GSP_PAGE_SIZE);
128 
129 	msg = kvmalloc(repc, GFP_KERNEL);
130 	if (!msg)
131 		return ERR_PTR(-ENOMEM);
132 
133 	len = ((gsp->msgq.cnt - rptr) * GSP_PAGE_SIZE) - sizeof(*mqe);
134 	len = min_t(u32, repc, len);
135 	memcpy(msg, mqe->data, len);
136 
137 	repc -= len;
138 
139 	if (repc) {
140 		mqe = (void *)((u8 *)gsp->shm.msgq.ptr + 0x1000 + 0 * 0x1000);
141 		memcpy(msg + len, mqe, repc);
142 	}
143 
144 	rptr = (rptr + DIV_ROUND_UP(size, GSP_PAGE_SIZE)) % gsp->msgq.cnt;
145 
146 	mb();
147 	(*gsp->msgq.rptr) = rptr;
148 	return msg;
149 }
150 
151 static void *
r535_gsp_msgq_recv(struct nvkm_gsp * gsp,u32 repc,int * ptime)152 r535_gsp_msgq_recv(struct nvkm_gsp *gsp, u32 repc, int *ptime)
153 {
154 	return r535_gsp_msgq_wait(gsp, repc, NULL, ptime);
155 }
156 
157 static int
r535_gsp_cmdq_push(struct nvkm_gsp * gsp,void * argv)158 r535_gsp_cmdq_push(struct nvkm_gsp *gsp, void *argv)
159 {
160 	struct r535_gsp_msg *cmd = container_of(argv, typeof(*cmd), data);
161 	struct r535_gsp_msg *cqe;
162 	u32 argc = cmd->checksum;
163 	u64 *ptr = (void *)cmd;
164 	u64 *end;
165 	u64 csum = 0;
166 	int free, time = 1000000;
167 	u32 wptr, size, step;
168 	u32 off = 0;
169 
170 	argc = ALIGN(GSP_MSG_HDR_SIZE + argc, GSP_PAGE_SIZE);
171 
172 	end = (u64 *)((char *)ptr + argc);
173 	cmd->pad = 0;
174 	cmd->checksum = 0;
175 	cmd->sequence = gsp->cmdq.seq++;
176 	cmd->elem_count = DIV_ROUND_UP(argc, 0x1000);
177 
178 	while (ptr < end)
179 		csum ^= *ptr++;
180 
181 	cmd->checksum = upper_32_bits(csum) ^ lower_32_bits(csum);
182 
183 	wptr = *gsp->cmdq.wptr;
184 	do {
185 		do {
186 			free = *gsp->cmdq.rptr + gsp->cmdq.cnt - wptr - 1;
187 			if (free >= gsp->cmdq.cnt)
188 				free -= gsp->cmdq.cnt;
189 			if (free >= 1)
190 				break;
191 
192 			usleep_range(1, 2);
193 		} while(--time);
194 
195 		if (WARN_ON(!time)) {
196 			kvfree(cmd);
197 			return -ETIMEDOUT;
198 		}
199 
200 		cqe = (void *)((u8 *)gsp->shm.cmdq.ptr + 0x1000 + wptr * 0x1000);
201 		step = min_t(u32, free, (gsp->cmdq.cnt - wptr));
202 		size = min_t(u32, argc, step * GSP_PAGE_SIZE);
203 
204 		memcpy(cqe, (u8 *)cmd + off, size);
205 
206 		wptr += DIV_ROUND_UP(size, 0x1000);
207 		if (wptr == gsp->cmdq.cnt)
208 			wptr = 0;
209 
210 		off  += size;
211 		argc -= size;
212 	} while(argc);
213 
214 	nvkm_trace(&gsp->subdev, "cmdq: wptr %d\n", wptr);
215 	wmb();
216 	(*gsp->cmdq.wptr) = wptr;
217 	mb();
218 
219 	nvkm_falcon_wr32(&gsp->falcon, 0xc00, 0x00000000);
220 
221 	kvfree(cmd);
222 	return 0;
223 }
224 
225 static void *
r535_gsp_cmdq_get(struct nvkm_gsp * gsp,u32 argc)226 r535_gsp_cmdq_get(struct nvkm_gsp *gsp, u32 argc)
227 {
228 	struct r535_gsp_msg *cmd;
229 	u32 size = GSP_MSG_HDR_SIZE + argc;
230 
231 	size = ALIGN(size, GSP_MSG_MIN_SIZE);
232 	cmd = kvzalloc(size, GFP_KERNEL);
233 	if (!cmd)
234 		return ERR_PTR(-ENOMEM);
235 
236 	cmd->checksum = argc;
237 	return cmd->data;
238 }
239 
240 struct nvfw_gsp_rpc {
241 	u32 header_version;
242 	u32 signature;
243 	u32 length;
244 	u32 function;
245 	u32 rpc_result;
246 	u32 rpc_result_private;
247 	u32 sequence;
248 	union {
249 		u32 spare;
250 		u32 cpuRmGfid;
251 	};
252 	u8  data[];
253 };
254 
255 static void
r535_gsp_msg_done(struct nvkm_gsp * gsp,struct nvfw_gsp_rpc * msg)256 r535_gsp_msg_done(struct nvkm_gsp *gsp, struct nvfw_gsp_rpc *msg)
257 {
258 	kvfree(msg);
259 }
260 
261 static void
r535_gsp_msg_dump(struct nvkm_gsp * gsp,struct nvfw_gsp_rpc * msg,int lvl)262 r535_gsp_msg_dump(struct nvkm_gsp *gsp, struct nvfw_gsp_rpc *msg, int lvl)
263 {
264 	if (gsp->subdev.debug >= lvl) {
265 		nvkm_printk__(&gsp->subdev, lvl, info,
266 			      "msg fn:%d len:0x%x/0x%zx res:0x%x resp:0x%x\n",
267 			      msg->function, msg->length, msg->length - sizeof(*msg),
268 			      msg->rpc_result, msg->rpc_result_private);
269 		print_hex_dump(KERN_INFO, "msg: ", DUMP_PREFIX_OFFSET, 16, 1,
270 			       msg->data, msg->length - sizeof(*msg), true);
271 	}
272 }
273 
274 static struct nvfw_gsp_rpc *
r535_gsp_msg_recv(struct nvkm_gsp * gsp,int fn,u32 repc)275 r535_gsp_msg_recv(struct nvkm_gsp *gsp, int fn, u32 repc)
276 {
277 	struct nvkm_subdev *subdev = &gsp->subdev;
278 	struct nvfw_gsp_rpc *msg;
279 	int time = 4000000, i;
280 	u32 size;
281 
282 retry:
283 	msg = r535_gsp_msgq_wait(gsp, sizeof(*msg), &size, &time);
284 	if (IS_ERR_OR_NULL(msg))
285 		return msg;
286 
287 	msg = r535_gsp_msgq_recv(gsp, msg->length, &time);
288 	if (IS_ERR_OR_NULL(msg))
289 		return msg;
290 
291 	if (msg->rpc_result) {
292 		r535_gsp_msg_dump(gsp, msg, NV_DBG_ERROR);
293 		r535_gsp_msg_done(gsp, msg);
294 		return ERR_PTR(-EINVAL);
295 	}
296 
297 	r535_gsp_msg_dump(gsp, msg, NV_DBG_TRACE);
298 
299 	if (fn && msg->function == fn) {
300 		if (repc) {
301 			if (msg->length < sizeof(*msg) + repc) {
302 				nvkm_error(subdev, "msg len %d < %zd\n",
303 					   msg->length, sizeof(*msg) + repc);
304 				r535_gsp_msg_dump(gsp, msg, NV_DBG_ERROR);
305 				r535_gsp_msg_done(gsp, msg);
306 				return ERR_PTR(-EIO);
307 			}
308 
309 			return msg;
310 		}
311 
312 		r535_gsp_msg_done(gsp, msg);
313 		return NULL;
314 	}
315 
316 	for (i = 0; i < gsp->msgq.ntfy_nr; i++) {
317 		struct nvkm_gsp_msgq_ntfy *ntfy = &gsp->msgq.ntfy[i];
318 
319 		if (ntfy->fn == msg->function) {
320 			if (ntfy->func)
321 				ntfy->func(ntfy->priv, ntfy->fn, msg->data, msg->length - sizeof(*msg));
322 			break;
323 		}
324 	}
325 
326 	if (i == gsp->msgq.ntfy_nr)
327 		r535_gsp_msg_dump(gsp, msg, NV_DBG_WARN);
328 
329 	r535_gsp_msg_done(gsp, msg);
330 	if (fn)
331 		goto retry;
332 
333 	if (*gsp->msgq.rptr != *gsp->msgq.wptr)
334 		goto retry;
335 
336 	return NULL;
337 }
338 
339 static int
r535_gsp_msg_ntfy_add(struct nvkm_gsp * gsp,u32 fn,nvkm_gsp_msg_ntfy_func func,void * priv)340 r535_gsp_msg_ntfy_add(struct nvkm_gsp *gsp, u32 fn, nvkm_gsp_msg_ntfy_func func, void *priv)
341 {
342 	int ret = 0;
343 
344 	mutex_lock(&gsp->msgq.mutex);
345 	if (WARN_ON(gsp->msgq.ntfy_nr >= ARRAY_SIZE(gsp->msgq.ntfy))) {
346 		ret = -ENOSPC;
347 	} else {
348 		gsp->msgq.ntfy[gsp->msgq.ntfy_nr].fn = fn;
349 		gsp->msgq.ntfy[gsp->msgq.ntfy_nr].func = func;
350 		gsp->msgq.ntfy[gsp->msgq.ntfy_nr].priv = priv;
351 		gsp->msgq.ntfy_nr++;
352 	}
353 	mutex_unlock(&gsp->msgq.mutex);
354 	return ret;
355 }
356 
357 static int
r535_gsp_rpc_poll(struct nvkm_gsp * gsp,u32 fn)358 r535_gsp_rpc_poll(struct nvkm_gsp *gsp, u32 fn)
359 {
360 	void *repv;
361 
362 	mutex_lock(&gsp->cmdq.mutex);
363 	repv = r535_gsp_msg_recv(gsp, fn, 0);
364 	mutex_unlock(&gsp->cmdq.mutex);
365 	if (IS_ERR(repv))
366 		return PTR_ERR(repv);
367 
368 	return 0;
369 }
370 
371 static void *
r535_gsp_rpc_send(struct nvkm_gsp * gsp,void * argv,bool wait,u32 repc)372 r535_gsp_rpc_send(struct nvkm_gsp *gsp, void *argv, bool wait, u32 repc)
373 {
374 	struct nvfw_gsp_rpc *rpc = container_of(argv, typeof(*rpc), data);
375 	struct nvfw_gsp_rpc *msg;
376 	u32 fn = rpc->function;
377 	void *repv = NULL;
378 	int ret;
379 
380 	if (gsp->subdev.debug >= NV_DBG_TRACE) {
381 		nvkm_trace(&gsp->subdev, "rpc fn:%d len:0x%x/0x%zx\n", rpc->function,
382 			   rpc->length, rpc->length - sizeof(*rpc));
383 		print_hex_dump(KERN_INFO, "rpc: ", DUMP_PREFIX_OFFSET, 16, 1,
384 			       rpc->data, rpc->length - sizeof(*rpc), true);
385 	}
386 
387 	ret = r535_gsp_cmdq_push(gsp, rpc);
388 	if (ret)
389 		return ERR_PTR(ret);
390 
391 	if (wait) {
392 		msg = r535_gsp_msg_recv(gsp, fn, repc);
393 		if (!IS_ERR_OR_NULL(msg))
394 			repv = msg->data;
395 		else
396 			repv = msg;
397 	}
398 
399 	return repv;
400 }
401 
402 static void
r535_gsp_event_dtor(struct nvkm_gsp_event * event)403 r535_gsp_event_dtor(struct nvkm_gsp_event *event)
404 {
405 	struct nvkm_gsp_device *device = event->device;
406 	struct nvkm_gsp_client *client = device->object.client;
407 	struct nvkm_gsp *gsp = client->gsp;
408 
409 	mutex_lock(&gsp->client_id.mutex);
410 	if (event->func) {
411 		list_del(&event->head);
412 		event->func = NULL;
413 	}
414 	mutex_unlock(&gsp->client_id.mutex);
415 
416 	nvkm_gsp_rm_free(&event->object);
417 	event->device = NULL;
418 }
419 
420 static int
r535_gsp_device_event_get(struct nvkm_gsp_event * event)421 r535_gsp_device_event_get(struct nvkm_gsp_event *event)
422 {
423 	struct nvkm_gsp_device *device = event->device;
424 	NV2080_CTRL_EVENT_SET_NOTIFICATION_PARAMS *ctrl;
425 
426 	ctrl = nvkm_gsp_rm_ctrl_get(&device->subdevice,
427 				    NV2080_CTRL_CMD_EVENT_SET_NOTIFICATION, sizeof(*ctrl));
428 	if (IS_ERR(ctrl))
429 		return PTR_ERR(ctrl);
430 
431 	ctrl->event = event->id;
432 	ctrl->action = NV2080_CTRL_EVENT_SET_NOTIFICATION_ACTION_REPEAT;
433 	return nvkm_gsp_rm_ctrl_wr(&device->subdevice, ctrl);
434 }
435 
436 static int
r535_gsp_device_event_ctor(struct nvkm_gsp_device * device,u32 handle,u32 id,nvkm_gsp_event_func func,struct nvkm_gsp_event * event)437 r535_gsp_device_event_ctor(struct nvkm_gsp_device *device, u32 handle, u32 id,
438 			   nvkm_gsp_event_func func, struct nvkm_gsp_event *event)
439 {
440 	struct nvkm_gsp_client *client = device->object.client;
441 	struct nvkm_gsp *gsp = client->gsp;
442 	NV0005_ALLOC_PARAMETERS *args;
443 	int ret;
444 
445 	args = nvkm_gsp_rm_alloc_get(&device->subdevice, handle,
446 				     NV01_EVENT_KERNEL_CALLBACK_EX, sizeof(*args),
447 				     &event->object);
448 	if (IS_ERR(args))
449 		return PTR_ERR(args);
450 
451 	args->hParentClient = client->object.handle;
452 	args->hSrcResource = 0;
453 	args->hClass = NV01_EVENT_KERNEL_CALLBACK_EX;
454 	args->notifyIndex = NV01_EVENT_CLIENT_RM | id;
455 	args->data = NULL;
456 
457 	ret = nvkm_gsp_rm_alloc_wr(&event->object, args);
458 	if (ret)
459 		return ret;
460 
461 	event->device = device;
462 	event->id = id;
463 
464 	ret = r535_gsp_device_event_get(event);
465 	if (ret) {
466 		nvkm_gsp_event_dtor(event);
467 		return ret;
468 	}
469 
470 	mutex_lock(&gsp->client_id.mutex);
471 	event->func = func;
472 	list_add(&event->head, &client->events);
473 	mutex_unlock(&gsp->client_id.mutex);
474 	return 0;
475 }
476 
477 static void
r535_gsp_device_dtor(struct nvkm_gsp_device * device)478 r535_gsp_device_dtor(struct nvkm_gsp_device *device)
479 {
480 	nvkm_gsp_rm_free(&device->subdevice);
481 	nvkm_gsp_rm_free(&device->object);
482 }
483 
484 static int
r535_gsp_subdevice_ctor(struct nvkm_gsp_device * device)485 r535_gsp_subdevice_ctor(struct nvkm_gsp_device *device)
486 {
487 	NV2080_ALLOC_PARAMETERS *args;
488 
489 	return nvkm_gsp_rm_alloc(&device->object, 0x5d1d0000, NV20_SUBDEVICE_0, sizeof(*args),
490 				 &device->subdevice);
491 }
492 
493 static int
r535_gsp_device_ctor(struct nvkm_gsp_client * client,struct nvkm_gsp_device * device)494 r535_gsp_device_ctor(struct nvkm_gsp_client *client, struct nvkm_gsp_device *device)
495 {
496 	NV0080_ALLOC_PARAMETERS *args;
497 	int ret;
498 
499 	args = nvkm_gsp_rm_alloc_get(&client->object, 0xde1d0000, NV01_DEVICE_0, sizeof(*args),
500 				     &device->object);
501 	if (IS_ERR(args))
502 		return PTR_ERR(args);
503 
504 	args->hClientShare = client->object.handle;
505 
506 	ret = nvkm_gsp_rm_alloc_wr(&device->object, args);
507 	if (ret)
508 		return ret;
509 
510 	ret = r535_gsp_subdevice_ctor(device);
511 	if (ret)
512 		nvkm_gsp_rm_free(&device->object);
513 
514 	return ret;
515 }
516 
517 static void
r535_gsp_client_dtor(struct nvkm_gsp_client * client)518 r535_gsp_client_dtor(struct nvkm_gsp_client *client)
519 {
520 	struct nvkm_gsp *gsp = client->gsp;
521 
522 	nvkm_gsp_rm_free(&client->object);
523 
524 	mutex_lock(&gsp->client_id.mutex);
525 	idr_remove(&gsp->client_id.idr, client->object.handle & 0xffff);
526 	mutex_unlock(&gsp->client_id.mutex);
527 
528 	client->gsp = NULL;
529 }
530 
531 static int
r535_gsp_client_ctor(struct nvkm_gsp * gsp,struct nvkm_gsp_client * client)532 r535_gsp_client_ctor(struct nvkm_gsp *gsp, struct nvkm_gsp_client *client)
533 {
534 	NV0000_ALLOC_PARAMETERS *args;
535 	int ret;
536 
537 	mutex_lock(&gsp->client_id.mutex);
538 	ret = idr_alloc(&gsp->client_id.idr, client, 0, 0xffff + 1, GFP_KERNEL);
539 	mutex_unlock(&gsp->client_id.mutex);
540 	if (ret < 0)
541 		return ret;
542 
543 	client->gsp = gsp;
544 	client->object.client = client;
545 	INIT_LIST_HEAD(&client->events);
546 
547 	args = nvkm_gsp_rm_alloc_get(&client->object, 0xc1d00000 | ret, NV01_ROOT, sizeof(*args),
548 				     &client->object);
549 	if (IS_ERR(args)) {
550 		r535_gsp_client_dtor(client);
551 		return ret;
552 	}
553 
554 	args->hClient = client->object.handle;
555 	args->processID = ~0;
556 
557 	ret = nvkm_gsp_rm_alloc_wr(&client->object, args);
558 	if (ret) {
559 		r535_gsp_client_dtor(client);
560 		return ret;
561 	}
562 
563 	return 0;
564 }
565 
566 static int
r535_gsp_rpc_rm_free(struct nvkm_gsp_object * object)567 r535_gsp_rpc_rm_free(struct nvkm_gsp_object *object)
568 {
569 	struct nvkm_gsp_client *client = object->client;
570 	struct nvkm_gsp *gsp = client->gsp;
571 	rpc_free_v03_00 *rpc;
572 
573 	nvkm_debug(&gsp->subdev, "cli:0x%08x obj:0x%08x free\n",
574 		   client->object.handle, object->handle);
575 
576 	rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_FREE, sizeof(*rpc));
577 	if (WARN_ON(IS_ERR_OR_NULL(rpc)))
578 		return -EIO;
579 
580 	rpc->params.hRoot = client->object.handle;
581 	rpc->params.hObjectParent = 0;
582 	rpc->params.hObjectOld = object->handle;
583 	return nvkm_gsp_rpc_wr(gsp, rpc, true);
584 }
585 
586 static void
r535_gsp_rpc_rm_alloc_done(struct nvkm_gsp_object * object,void * repv)587 r535_gsp_rpc_rm_alloc_done(struct nvkm_gsp_object *object, void *repv)
588 {
589 	rpc_gsp_rm_alloc_v03_00 *rpc = container_of(repv, typeof(*rpc), params);
590 
591 	nvkm_gsp_rpc_done(object->client->gsp, rpc);
592 }
593 
594 static void *
r535_gsp_rpc_rm_alloc_push(struct nvkm_gsp_object * object,void * argv,u32 repc)595 r535_gsp_rpc_rm_alloc_push(struct nvkm_gsp_object *object, void *argv, u32 repc)
596 {
597 	rpc_gsp_rm_alloc_v03_00 *rpc = container_of(argv, typeof(*rpc), params);
598 	struct nvkm_gsp *gsp = object->client->gsp;
599 	void *ret;
600 
601 	rpc = nvkm_gsp_rpc_push(gsp, rpc, true, sizeof(*rpc) + repc);
602 	if (IS_ERR_OR_NULL(rpc))
603 		return rpc;
604 
605 	if (rpc->status) {
606 		ret = ERR_PTR(r535_rpc_status_to_errno(rpc->status));
607 		if (PTR_ERR(ret) != -EAGAIN && PTR_ERR(ret) != -EBUSY)
608 			nvkm_error(&gsp->subdev, "RM_ALLOC: 0x%x\n", rpc->status);
609 	} else {
610 		ret = repc ? rpc->params : NULL;
611 	}
612 
613 	nvkm_gsp_rpc_done(gsp, rpc);
614 
615 	return ret;
616 }
617 
618 static void *
r535_gsp_rpc_rm_alloc_get(struct nvkm_gsp_object * object,u32 oclass,u32 argc)619 r535_gsp_rpc_rm_alloc_get(struct nvkm_gsp_object *object, u32 oclass, u32 argc)
620 {
621 	struct nvkm_gsp_client *client = object->client;
622 	struct nvkm_gsp *gsp = client->gsp;
623 	rpc_gsp_rm_alloc_v03_00 *rpc;
624 
625 	nvkm_debug(&gsp->subdev, "cli:0x%08x obj:0x%08x new obj:0x%08x cls:0x%08x argc:%d\n",
626 		   client->object.handle, object->parent->handle, object->handle, oclass, argc);
627 
628 	rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_GSP_RM_ALLOC, sizeof(*rpc) + argc);
629 	if (IS_ERR(rpc))
630 		return rpc;
631 
632 	rpc->hClient = client->object.handle;
633 	rpc->hParent = object->parent->handle;
634 	rpc->hObject = object->handle;
635 	rpc->hClass = oclass;
636 	rpc->status = 0;
637 	rpc->paramsSize = argc;
638 	return rpc->params;
639 }
640 
641 static void
r535_gsp_rpc_rm_ctrl_done(struct nvkm_gsp_object * object,void * repv)642 r535_gsp_rpc_rm_ctrl_done(struct nvkm_gsp_object *object, void *repv)
643 {
644 	rpc_gsp_rm_control_v03_00 *rpc = container_of(repv, typeof(*rpc), params);
645 
646 	if (!repv)
647 		return;
648 	nvkm_gsp_rpc_done(object->client->gsp, rpc);
649 }
650 
651 static int
r535_gsp_rpc_rm_ctrl_push(struct nvkm_gsp_object * object,void ** argv,u32 repc)652 r535_gsp_rpc_rm_ctrl_push(struct nvkm_gsp_object *object, void **argv, u32 repc)
653 {
654 	rpc_gsp_rm_control_v03_00 *rpc = container_of((*argv), typeof(*rpc), params);
655 	struct nvkm_gsp *gsp = object->client->gsp;
656 	int ret = 0;
657 
658 	rpc = nvkm_gsp_rpc_push(gsp, rpc, true, repc);
659 	if (IS_ERR_OR_NULL(rpc)) {
660 		*argv = NULL;
661 		return PTR_ERR(rpc);
662 	}
663 
664 	if (rpc->status) {
665 		ret = r535_rpc_status_to_errno(rpc->status);
666 		if (ret != -EAGAIN && ret != -EBUSY)
667 			nvkm_error(&gsp->subdev, "cli:0x%08x obj:0x%08x ctrl cmd:0x%08x failed: 0x%08x\n",
668 				   object->client->object.handle, object->handle, rpc->cmd, rpc->status);
669 	}
670 
671 	if (repc)
672 		*argv = rpc->params;
673 	else
674 		nvkm_gsp_rpc_done(gsp, rpc);
675 
676 	return ret;
677 }
678 
679 static void *
r535_gsp_rpc_rm_ctrl_get(struct nvkm_gsp_object * object,u32 cmd,u32 argc)680 r535_gsp_rpc_rm_ctrl_get(struct nvkm_gsp_object *object, u32 cmd, u32 argc)
681 {
682 	struct nvkm_gsp_client *client = object->client;
683 	struct nvkm_gsp *gsp = client->gsp;
684 	rpc_gsp_rm_control_v03_00 *rpc;
685 
686 	nvkm_debug(&gsp->subdev, "cli:0x%08x obj:0x%08x ctrl cmd:0x%08x argc:%d\n",
687 		   client->object.handle, object->handle, cmd, argc);
688 
689 	rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_GSP_RM_CONTROL, sizeof(*rpc) + argc);
690 	if (IS_ERR(rpc))
691 		return rpc;
692 
693 	rpc->hClient    = client->object.handle;
694 	rpc->hObject    = object->handle;
695 	rpc->cmd	= cmd;
696 	rpc->status     = 0;
697 	rpc->paramsSize = argc;
698 	return rpc->params;
699 }
700 
701 static void
r535_gsp_rpc_done(struct nvkm_gsp * gsp,void * repv)702 r535_gsp_rpc_done(struct nvkm_gsp *gsp, void *repv)
703 {
704 	struct nvfw_gsp_rpc *rpc = container_of(repv, typeof(*rpc), data);
705 
706 	r535_gsp_msg_done(gsp, rpc);
707 }
708 
709 static void *
r535_gsp_rpc_get(struct nvkm_gsp * gsp,u32 fn,u32 argc)710 r535_gsp_rpc_get(struct nvkm_gsp *gsp, u32 fn, u32 argc)
711 {
712 	struct nvfw_gsp_rpc *rpc;
713 
714 	rpc = r535_gsp_cmdq_get(gsp, ALIGN(sizeof(*rpc) + argc, sizeof(u64)));
715 	if (IS_ERR(rpc))
716 		return ERR_CAST(rpc);
717 
718 	rpc->header_version = 0x03000000;
719 	rpc->signature = ('C' << 24) | ('P' << 16) | ('R' << 8) | 'V';
720 	rpc->function = fn;
721 	rpc->rpc_result = 0xffffffff;
722 	rpc->rpc_result_private = 0xffffffff;
723 	rpc->length = sizeof(*rpc) + argc;
724 	return rpc->data;
725 }
726 
727 static void *
r535_gsp_rpc_push(struct nvkm_gsp * gsp,void * argv,bool wait,u32 repc)728 r535_gsp_rpc_push(struct nvkm_gsp *gsp, void *argv, bool wait, u32 repc)
729 {
730 	struct nvfw_gsp_rpc *rpc = container_of(argv, typeof(*rpc), data);
731 	struct r535_gsp_msg *cmd = container_of((void *)rpc, typeof(*cmd), data);
732 	const u32 max_msg_size = (16 * 0x1000) - sizeof(struct r535_gsp_msg);
733 	const u32 max_rpc_size = max_msg_size - sizeof(*rpc);
734 	u32 rpc_size = rpc->length - sizeof(*rpc);
735 	void *repv;
736 
737 	mutex_lock(&gsp->cmdq.mutex);
738 	if (rpc_size > max_rpc_size) {
739 		const u32 fn = rpc->function;
740 
741 		/* Adjust length, and send initial RPC. */
742 		rpc->length = sizeof(*rpc) + max_rpc_size;
743 		cmd->checksum = rpc->length;
744 
745 		repv = r535_gsp_rpc_send(gsp, argv, false, 0);
746 		if (IS_ERR(repv))
747 			goto done;
748 
749 		argv += max_rpc_size;
750 		rpc_size -= max_rpc_size;
751 
752 		/* Remaining chunks sent as CONTINUATION_RECORD RPCs. */
753 		while (rpc_size) {
754 			u32 size = min(rpc_size, max_rpc_size);
755 			void *next;
756 
757 			next = r535_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_CONTINUATION_RECORD, size);
758 			if (IS_ERR(next)) {
759 				repv = next;
760 				goto done;
761 			}
762 
763 			memcpy(next, argv, size);
764 
765 			repv = r535_gsp_rpc_send(gsp, next, false, 0);
766 			if (IS_ERR(repv))
767 				goto done;
768 
769 			argv += size;
770 			rpc_size -= size;
771 		}
772 
773 		/* Wait for reply. */
774 		if (wait) {
775 			rpc = r535_gsp_msg_recv(gsp, fn, repc);
776 			if (!IS_ERR_OR_NULL(rpc))
777 				repv = rpc->data;
778 			else
779 				repv = rpc;
780 		} else {
781 			repv = NULL;
782 		}
783 	} else {
784 		repv = r535_gsp_rpc_send(gsp, argv, wait, repc);
785 	}
786 
787 done:
788 	mutex_unlock(&gsp->cmdq.mutex);
789 	return repv;
790 }
791 
792 const struct nvkm_gsp_rm
793 r535_gsp_rm = {
794 	.rpc_get = r535_gsp_rpc_get,
795 	.rpc_push = r535_gsp_rpc_push,
796 	.rpc_done = r535_gsp_rpc_done,
797 
798 	.rm_ctrl_get = r535_gsp_rpc_rm_ctrl_get,
799 	.rm_ctrl_push = r535_gsp_rpc_rm_ctrl_push,
800 	.rm_ctrl_done = r535_gsp_rpc_rm_ctrl_done,
801 
802 	.rm_alloc_get = r535_gsp_rpc_rm_alloc_get,
803 	.rm_alloc_push = r535_gsp_rpc_rm_alloc_push,
804 	.rm_alloc_done = r535_gsp_rpc_rm_alloc_done,
805 
806 	.rm_free = r535_gsp_rpc_rm_free,
807 
808 	.client_ctor = r535_gsp_client_ctor,
809 	.client_dtor = r535_gsp_client_dtor,
810 
811 	.device_ctor = r535_gsp_device_ctor,
812 	.device_dtor = r535_gsp_device_dtor,
813 
814 	.event_ctor = r535_gsp_device_event_ctor,
815 	.event_dtor = r535_gsp_event_dtor,
816 };
817 
818 static void
r535_gsp_msgq_work(struct work_struct * work)819 r535_gsp_msgq_work(struct work_struct *work)
820 {
821 	struct nvkm_gsp *gsp = container_of(work, typeof(*gsp), msgq.work);
822 
823 	mutex_lock(&gsp->cmdq.mutex);
824 	if (*gsp->msgq.rptr != *gsp->msgq.wptr)
825 		r535_gsp_msg_recv(gsp, 0, 0);
826 	mutex_unlock(&gsp->cmdq.mutex);
827 }
828 
829 static irqreturn_t
r535_gsp_intr(struct nvkm_inth * inth)830 r535_gsp_intr(struct nvkm_inth *inth)
831 {
832 	struct nvkm_gsp *gsp = container_of(inth, typeof(*gsp), subdev.inth);
833 	struct nvkm_subdev *subdev = &gsp->subdev;
834 	u32 intr = nvkm_falcon_rd32(&gsp->falcon, 0x0008);
835 	u32 inte = nvkm_falcon_rd32(&gsp->falcon, gsp->falcon.func->addr2 +
836 						  gsp->falcon.func->riscv_irqmask);
837 	u32 stat = intr & inte;
838 
839 	if (!stat) {
840 		nvkm_debug(subdev, "inte %08x %08x\n", intr, inte);
841 		return IRQ_NONE;
842 	}
843 
844 	if (stat & 0x00000040) {
845 		nvkm_falcon_wr32(&gsp->falcon, 0x004, 0x00000040);
846 		schedule_work(&gsp->msgq.work);
847 		stat &= ~0x00000040;
848 	}
849 
850 	if (stat) {
851 		nvkm_error(subdev, "intr %08x\n", stat);
852 		nvkm_falcon_wr32(&gsp->falcon, 0x014, stat);
853 		nvkm_falcon_wr32(&gsp->falcon, 0x004, stat);
854 	}
855 
856 	nvkm_falcon_intr_retrigger(&gsp->falcon);
857 	return IRQ_HANDLED;
858 }
859 
860 static int
r535_gsp_intr_get_table(struct nvkm_gsp * gsp)861 r535_gsp_intr_get_table(struct nvkm_gsp *gsp)
862 {
863 	NV2080_CTRL_INTERNAL_INTR_GET_KERNEL_TABLE_PARAMS *ctrl;
864 	int ret = 0;
865 
866 	ctrl = nvkm_gsp_rm_ctrl_get(&gsp->internal.device.subdevice,
867 				    NV2080_CTRL_CMD_INTERNAL_INTR_GET_KERNEL_TABLE, sizeof(*ctrl));
868 	if (IS_ERR(ctrl))
869 		return PTR_ERR(ctrl);
870 
871 	ret = nvkm_gsp_rm_ctrl_push(&gsp->internal.device.subdevice, &ctrl, sizeof(*ctrl));
872 	if (WARN_ON(ret)) {
873 		nvkm_gsp_rm_ctrl_done(&gsp->internal.device.subdevice, ctrl);
874 		return ret;
875 	}
876 
877 	for (unsigned i = 0; i < ctrl->tableLen; i++) {
878 		enum nvkm_subdev_type type;
879 		int inst;
880 
881 		nvkm_debug(&gsp->subdev,
882 			   "%2d: engineIdx %3d pmcIntrMask %08x stall %08x nonStall %08x\n", i,
883 			   ctrl->table[i].engineIdx, ctrl->table[i].pmcIntrMask,
884 			   ctrl->table[i].vectorStall, ctrl->table[i].vectorNonStall);
885 
886 		switch (ctrl->table[i].engineIdx) {
887 		case MC_ENGINE_IDX_GSP:
888 			type = NVKM_SUBDEV_GSP;
889 			inst = 0;
890 			break;
891 		case MC_ENGINE_IDX_DISP:
892 			type = NVKM_ENGINE_DISP;
893 			inst = 0;
894 			break;
895 		case MC_ENGINE_IDX_CE0 ... MC_ENGINE_IDX_CE9:
896 			type = NVKM_ENGINE_CE;
897 			inst = ctrl->table[i].engineIdx - MC_ENGINE_IDX_CE0;
898 			break;
899 		case MC_ENGINE_IDX_GR0:
900 			type = NVKM_ENGINE_GR;
901 			inst = 0;
902 			break;
903 		case MC_ENGINE_IDX_NVDEC0 ... MC_ENGINE_IDX_NVDEC7:
904 			type = NVKM_ENGINE_NVDEC;
905 			inst = ctrl->table[i].engineIdx - MC_ENGINE_IDX_NVDEC0;
906 			break;
907 		case MC_ENGINE_IDX_MSENC ... MC_ENGINE_IDX_MSENC2:
908 			type = NVKM_ENGINE_NVENC;
909 			inst = ctrl->table[i].engineIdx - MC_ENGINE_IDX_MSENC;
910 			break;
911 		case MC_ENGINE_IDX_NVJPEG0 ... MC_ENGINE_IDX_NVJPEG7:
912 			type = NVKM_ENGINE_NVJPG;
913 			inst = ctrl->table[i].engineIdx - MC_ENGINE_IDX_NVJPEG0;
914 			break;
915 		case MC_ENGINE_IDX_OFA0:
916 			type = NVKM_ENGINE_OFA;
917 			inst = 0;
918 			break;
919 		default:
920 			continue;
921 		}
922 
923 		if (WARN_ON(gsp->intr_nr == ARRAY_SIZE(gsp->intr))) {
924 			ret = -ENOSPC;
925 			break;
926 		}
927 
928 		gsp->intr[gsp->intr_nr].type = type;
929 		gsp->intr[gsp->intr_nr].inst = inst;
930 		gsp->intr[gsp->intr_nr].stall = ctrl->table[i].vectorStall;
931 		gsp->intr[gsp->intr_nr].nonstall = ctrl->table[i].vectorNonStall;
932 		gsp->intr_nr++;
933 	}
934 
935 	nvkm_gsp_rm_ctrl_done(&gsp->internal.device.subdevice, ctrl);
936 	return ret;
937 }
938 
939 static int
r535_gsp_rpc_get_gsp_static_info(struct nvkm_gsp * gsp)940 r535_gsp_rpc_get_gsp_static_info(struct nvkm_gsp *gsp)
941 {
942 	GspStaticConfigInfo *rpc;
943 	int last_usable = -1;
944 
945 	rpc = nvkm_gsp_rpc_rd(gsp, NV_VGPU_MSG_FUNCTION_GET_GSP_STATIC_INFO, sizeof(*rpc));
946 	if (IS_ERR(rpc))
947 		return PTR_ERR(rpc);
948 
949 	gsp->internal.client.object.client = &gsp->internal.client;
950 	gsp->internal.client.object.parent = NULL;
951 	gsp->internal.client.object.handle = rpc->hInternalClient;
952 	gsp->internal.client.gsp = gsp;
953 
954 	gsp->internal.device.object.client = &gsp->internal.client;
955 	gsp->internal.device.object.parent = &gsp->internal.client.object;
956 	gsp->internal.device.object.handle = rpc->hInternalDevice;
957 
958 	gsp->internal.device.subdevice.client = &gsp->internal.client;
959 	gsp->internal.device.subdevice.parent = &gsp->internal.device.object;
960 	gsp->internal.device.subdevice.handle = rpc->hInternalSubdevice;
961 
962 	gsp->bar.rm_bar1_pdb = rpc->bar1PdeBase;
963 	gsp->bar.rm_bar2_pdb = rpc->bar2PdeBase;
964 
965 	for (int i = 0; i < rpc->fbRegionInfoParams.numFBRegions; i++) {
966 		NV2080_CTRL_CMD_FB_GET_FB_REGION_FB_REGION_INFO *reg =
967 			&rpc->fbRegionInfoParams.fbRegion[i];
968 
969 		nvkm_debug(&gsp->subdev, "fb region %d: "
970 			   "%016llx-%016llx rsvd:%016llx perf:%08x comp:%d iso:%d prot:%d\n", i,
971 			   reg->base, reg->limit, reg->reserved, reg->performance,
972 			   reg->supportCompressed, reg->supportISO, reg->bProtected);
973 
974 		if (!reg->reserved && !reg->bProtected) {
975 			if (reg->supportCompressed && reg->supportISO &&
976 			    !WARN_ON_ONCE(gsp->fb.region_nr >= ARRAY_SIZE(gsp->fb.region))) {
977 					const u64 size = (reg->limit + 1) - reg->base;
978 
979 					gsp->fb.region[gsp->fb.region_nr].addr = reg->base;
980 					gsp->fb.region[gsp->fb.region_nr].size = size;
981 					gsp->fb.region_nr++;
982 			}
983 
984 			last_usable = i;
985 		}
986 	}
987 
988 	if (last_usable >= 0) {
989 		u32 rsvd_base = rpc->fbRegionInfoParams.fbRegion[last_usable].limit + 1;
990 
991 		gsp->fb.rsvd_size = gsp->fb.heap.addr - rsvd_base;
992 	}
993 
994 	for (int gpc = 0; gpc < ARRAY_SIZE(rpc->tpcInfo); gpc++) {
995 		if (rpc->gpcInfo.gpcMask & BIT(gpc)) {
996 			gsp->gr.tpcs += hweight32(rpc->tpcInfo[gpc].tpcMask);
997 			gsp->gr.gpcs++;
998 		}
999 	}
1000 
1001 	nvkm_gsp_rpc_done(gsp, rpc);
1002 	return 0;
1003 }
1004 
1005 static void
nvkm_gsp_mem_dtor(struct nvkm_gsp_mem * mem)1006 nvkm_gsp_mem_dtor(struct nvkm_gsp_mem *mem)
1007 {
1008 	if (mem->data) {
1009 		/*
1010 		 * Poison the buffer to catch any unexpected access from
1011 		 * GSP-RM if the buffer was prematurely freed.
1012 		 */
1013 		memset(mem->data, 0xFF, mem->size);
1014 
1015 		dma_free_coherent(mem->dev, mem->size, mem->data, mem->addr);
1016 		put_device(mem->dev);
1017 
1018 		memset(mem, 0, sizeof(*mem));
1019 	}
1020 }
1021 
1022 /**
1023  * nvkm_gsp_mem_ctor - constructor for nvkm_gsp_mem objects
1024  * @gsp: gsp pointer
1025  * @size: number of bytes to allocate
1026  * @mem: nvkm_gsp_mem object to initialize
1027  *
1028  * Allocates a block of memory for use with GSP.
1029  *
1030  * This memory block can potentially out-live the driver's remove() callback,
1031  * so we take a device reference to ensure its lifetime. The reference is
1032  * dropped in the destructor.
1033  */
1034 static int
nvkm_gsp_mem_ctor(struct nvkm_gsp * gsp,size_t size,struct nvkm_gsp_mem * mem)1035 nvkm_gsp_mem_ctor(struct nvkm_gsp *gsp, size_t size, struct nvkm_gsp_mem *mem)
1036 {
1037 	mem->data = dma_alloc_coherent(gsp->subdev.device->dev, size, &mem->addr, GFP_KERNEL);
1038 	if (WARN_ON(!mem->data))
1039 		return -ENOMEM;
1040 
1041 	mem->size = size;
1042 	mem->dev = get_device(gsp->subdev.device->dev);
1043 
1044 	return 0;
1045 }
1046 
1047 static int
r535_gsp_postinit(struct nvkm_gsp * gsp)1048 r535_gsp_postinit(struct nvkm_gsp *gsp)
1049 {
1050 	struct nvkm_device *device = gsp->subdev.device;
1051 	int ret;
1052 
1053 	ret = r535_gsp_rpc_get_gsp_static_info(gsp);
1054 	if (WARN_ON(ret))
1055 		return ret;
1056 
1057 	INIT_WORK(&gsp->msgq.work, r535_gsp_msgq_work);
1058 
1059 	ret = r535_gsp_intr_get_table(gsp);
1060 	if (WARN_ON(ret))
1061 		return ret;
1062 
1063 	ret = nvkm_gsp_intr_stall(gsp, gsp->subdev.type, gsp->subdev.inst);
1064 	if (WARN_ON(ret < 0))
1065 		return ret;
1066 
1067 	ret = nvkm_inth_add(&device->vfn->intr, ret, NVKM_INTR_PRIO_NORMAL, &gsp->subdev,
1068 			    r535_gsp_intr, &gsp->subdev.inth);
1069 	if (WARN_ON(ret))
1070 		return ret;
1071 
1072 	nvkm_inth_allow(&gsp->subdev.inth);
1073 	nvkm_wr32(device, 0x110004, 0x00000040);
1074 
1075 	/* Release the DMA buffers that were needed only for boot and init */
1076 	nvkm_gsp_mem_dtor(&gsp->boot.fw);
1077 	nvkm_gsp_mem_dtor(&gsp->libos);
1078 
1079 	return ret;
1080 }
1081 
1082 static int
r535_gsp_rpc_unloading_guest_driver(struct nvkm_gsp * gsp,bool suspend)1083 r535_gsp_rpc_unloading_guest_driver(struct nvkm_gsp *gsp, bool suspend)
1084 {
1085 	rpc_unloading_guest_driver_v1F_07 *rpc;
1086 
1087 	rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_UNLOADING_GUEST_DRIVER, sizeof(*rpc));
1088 	if (IS_ERR(rpc))
1089 		return PTR_ERR(rpc);
1090 
1091 	if (suspend) {
1092 		rpc->bInPMTransition = 1;
1093 		rpc->bGc6Entering = 0;
1094 		rpc->newLevel = NV2080_CTRL_GPU_SET_POWER_STATE_GPU_LEVEL_3;
1095 	} else {
1096 		rpc->bInPMTransition = 0;
1097 		rpc->bGc6Entering = 0;
1098 		rpc->newLevel = NV2080_CTRL_GPU_SET_POWER_STATE_GPU_LEVEL_0;
1099 	}
1100 
1101 	return nvkm_gsp_rpc_wr(gsp, rpc, true);
1102 }
1103 
1104 enum registry_type {
1105 	REGISTRY_TABLE_ENTRY_TYPE_DWORD  = 1, /* 32-bit unsigned integer */
1106 	REGISTRY_TABLE_ENTRY_TYPE_BINARY = 2, /* Binary blob */
1107 	REGISTRY_TABLE_ENTRY_TYPE_STRING = 3, /* Null-terminated string */
1108 };
1109 
1110 /* An arbitrary limit to the length of a registry key */
1111 #define REGISTRY_MAX_KEY_LENGTH		64
1112 
1113 /**
1114  * registry_list_entry - linked list member for a registry key/value
1115  * @head: list_head struct
1116  * @type: dword, binary, or string
1117  * @klen: the length of name of the key
1118  * @vlen: the length of the value
1119  * @key: the key name
1120  * @dword: the data, if REGISTRY_TABLE_ENTRY_TYPE_DWORD
1121  * @binary: the data, if TYPE_BINARY or TYPE_STRING
1122  *
1123  * Every registry key/value is represented internally by this struct.
1124  *
1125  * Type DWORD is a simple 32-bit unsigned integer, and its value is stored in
1126  * @dword.
1127  *
1128  * Types BINARY and STRING are variable-length binary blobs.  The only real
1129  * difference between BINARY and STRING is that STRING is null-terminated and
1130  * is expected to contain only printable characters.
1131  *
1132  * Note: it is technically possible to have multiple keys with the same name
1133  * but different types, but this is not useful since GSP-RM expects keys to
1134  * have only one specific type.
1135  */
1136 struct registry_list_entry {
1137 	struct list_head head;
1138 	enum registry_type type;
1139 	size_t klen;
1140 	char key[REGISTRY_MAX_KEY_LENGTH];
1141 	size_t vlen;
1142 	u32 dword;			/* TYPE_DWORD */
1143 	u8 binary[] __counted_by(vlen);	/* TYPE_BINARY or TYPE_STRING */
1144 };
1145 
1146 /**
1147  * add_registry -- adds a registry entry
1148  * @gsp: gsp pointer
1149  * @key: name of the registry key
1150  * @type: type of data
1151  * @data: pointer to value
1152  * @length: size of data, in bytes
1153  *
1154  * Adds a registry key/value pair to the registry database.
1155  *
1156  * This function collects the registry information in a linked list.  After
1157  * all registry keys have been added, build_registry() is used to create the
1158  * RPC data structure.
1159  *
1160  * registry_rpc_size is a running total of the size of all registry keys.
1161  * It's used to avoid an O(n) calculation of the size when the RPC is built.
1162  *
1163  * Returns 0 on success, or negative error code on error.
1164  */
add_registry(struct nvkm_gsp * gsp,const char * key,enum registry_type type,const void * data,size_t length)1165 static int add_registry(struct nvkm_gsp *gsp, const char *key,
1166 			enum registry_type type, const void *data, size_t length)
1167 {
1168 	struct registry_list_entry *reg;
1169 	const size_t nlen = strnlen(key, REGISTRY_MAX_KEY_LENGTH) + 1;
1170 	size_t alloc_size; /* extra bytes to alloc for binary or string value */
1171 
1172 	if (nlen > REGISTRY_MAX_KEY_LENGTH)
1173 		return -EINVAL;
1174 
1175 	alloc_size = (type == REGISTRY_TABLE_ENTRY_TYPE_DWORD) ? 0 : length;
1176 
1177 	reg = kmalloc(sizeof(*reg) + alloc_size, GFP_KERNEL);
1178 	if (!reg)
1179 		return -ENOMEM;
1180 
1181 	switch (type) {
1182 	case REGISTRY_TABLE_ENTRY_TYPE_DWORD:
1183 		reg->dword = *(const u32 *)(data);
1184 		break;
1185 	case REGISTRY_TABLE_ENTRY_TYPE_BINARY:
1186 	case REGISTRY_TABLE_ENTRY_TYPE_STRING:
1187 		memcpy(reg->binary, data, alloc_size);
1188 		break;
1189 	default:
1190 		nvkm_error(&gsp->subdev, "unrecognized registry type %u for '%s'\n",
1191 			   type, key);
1192 		kfree(reg);
1193 		return -EINVAL;
1194 	}
1195 
1196 	memcpy(reg->key, key, nlen);
1197 	reg->klen = nlen;
1198 	reg->vlen = length;
1199 	reg->type = type;
1200 
1201 	list_add_tail(&reg->head, &gsp->registry_list);
1202 	gsp->registry_rpc_size += sizeof(PACKED_REGISTRY_ENTRY) + nlen + alloc_size;
1203 
1204 	return 0;
1205 }
1206 
add_registry_num(struct nvkm_gsp * gsp,const char * key,u32 value)1207 static int add_registry_num(struct nvkm_gsp *gsp, const char *key, u32 value)
1208 {
1209 	return add_registry(gsp, key, REGISTRY_TABLE_ENTRY_TYPE_DWORD,
1210 			    &value, sizeof(u32));
1211 }
1212 
add_registry_string(struct nvkm_gsp * gsp,const char * key,const char * value)1213 static int add_registry_string(struct nvkm_gsp *gsp, const char *key, const char *value)
1214 {
1215 	return add_registry(gsp, key, REGISTRY_TABLE_ENTRY_TYPE_STRING,
1216 			    value, strlen(value) + 1);
1217 }
1218 
1219 /**
1220  * build_registry -- create the registry RPC data
1221  * @gsp: gsp pointer
1222  * @registry: pointer to the RPC payload to fill
1223  *
1224  * After all registry key/value pairs have been added, call this function to
1225  * build the RPC.
1226  *
1227  * The registry RPC looks like this:
1228  *
1229  * +-----------------+
1230  * |NvU32 size;      |
1231  * |NvU32 numEntries;|
1232  * +-----------------+
1233  * +----------------------------------------+
1234  * |PACKED_REGISTRY_ENTRY                   |
1235  * +----------------------------------------+
1236  * |Null-terminated key (string) for entry 0|
1237  * +----------------------------------------+
1238  * |Binary/string data value for entry 0    | (only if necessary)
1239  * +----------------------------------------+
1240  *
1241  * +----------------------------------------+
1242  * |PACKED_REGISTRY_ENTRY                   |
1243  * +----------------------------------------+
1244  * |Null-terminated key (string) for entry 1|
1245  * +----------------------------------------+
1246  * |Binary/string data value for entry 1    | (only if necessary)
1247  * +----------------------------------------+
1248  * ... (and so on, one copy for each entry)
1249  *
1250  *
1251  * The 'data' field of an entry is either a 32-bit integer (for type DWORD)
1252  * or an offset into the PACKED_REGISTRY_TABLE (for types BINARY and STRING).
1253  *
1254  * All memory allocated by add_registry() is released.
1255  */
build_registry(struct nvkm_gsp * gsp,PACKED_REGISTRY_TABLE * registry)1256 static void build_registry(struct nvkm_gsp *gsp, PACKED_REGISTRY_TABLE *registry)
1257 {
1258 	struct registry_list_entry *reg, *n;
1259 	size_t str_offset;
1260 	unsigned int i = 0;
1261 
1262 	registry->numEntries = list_count_nodes(&gsp->registry_list);
1263 	str_offset = struct_size(registry, entries, registry->numEntries);
1264 
1265 	list_for_each_entry_safe(reg, n, &gsp->registry_list, head) {
1266 		registry->entries[i].type = reg->type;
1267 		registry->entries[i].length = reg->vlen;
1268 
1269 		/* Append the key name to the table */
1270 		registry->entries[i].nameOffset = str_offset;
1271 		memcpy((void *)registry + str_offset, reg->key, reg->klen);
1272 		str_offset += reg->klen;
1273 
1274 		switch (reg->type) {
1275 		case REGISTRY_TABLE_ENTRY_TYPE_DWORD:
1276 			registry->entries[i].data = reg->dword;
1277 			break;
1278 		case REGISTRY_TABLE_ENTRY_TYPE_BINARY:
1279 		case REGISTRY_TABLE_ENTRY_TYPE_STRING:
1280 			/* If the type is binary or string, also append the value */
1281 			memcpy((void *)registry + str_offset, reg->binary, reg->vlen);
1282 			registry->entries[i].data = str_offset;
1283 			str_offset += reg->vlen;
1284 			break;
1285 		default:
1286 			break;
1287 		}
1288 
1289 		i++;
1290 		list_del(&reg->head);
1291 		kfree(reg);
1292 	}
1293 
1294 	/* Double-check that we calculated the sizes correctly */
1295 	WARN_ON(gsp->registry_rpc_size != str_offset);
1296 
1297 	registry->size = gsp->registry_rpc_size;
1298 }
1299 
1300 /**
1301  * clean_registry -- clean up registry memory in case of error
1302  * @gsp: gsp pointer
1303  *
1304  * Call this function to clean up all memory allocated by add_registry()
1305  * in case of error and build_registry() is not called.
1306  */
clean_registry(struct nvkm_gsp * gsp)1307 static void clean_registry(struct nvkm_gsp *gsp)
1308 {
1309 	struct registry_list_entry *reg, *n;
1310 
1311 	list_for_each_entry_safe(reg, n, &gsp->registry_list, head) {
1312 		list_del(&reg->head);
1313 		kfree(reg);
1314 	}
1315 
1316 	gsp->registry_rpc_size = sizeof(PACKED_REGISTRY_TABLE);
1317 }
1318 
1319 MODULE_PARM_DESC(NVreg_RegistryDwords,
1320 		 "A semicolon-separated list of key=integer pairs of GSP-RM registry keys");
1321 static char *NVreg_RegistryDwords;
1322 module_param(NVreg_RegistryDwords, charp, 0400);
1323 
1324 /* dword only */
1325 struct nv_gsp_registry_entries {
1326 	const char *name;
1327 	u32 value;
1328 };
1329 
1330 /**
1331  * r535_registry_entries - required registry entries for GSP-RM
1332  *
1333  * This array lists registry entries that are required for GSP-RM to
1334  * function correctly.
1335  *
1336  * RMSecBusResetEnable - enables PCI secondary bus reset
1337  * RMForcePcieConfigSave - forces GSP-RM to preserve PCI configuration
1338  *   registers on any PCI reset.
1339  */
1340 static const struct nv_gsp_registry_entries r535_registry_entries[] = {
1341 	{ "RMSecBusResetEnable", 1 },
1342 	{ "RMForcePcieConfigSave", 1 },
1343 };
1344 #define NV_GSP_REG_NUM_ENTRIES ARRAY_SIZE(r535_registry_entries)
1345 
1346 /**
1347  * strip - strips all characters in 'reject' from 's'
1348  * @s: string to strip
1349  * @reject: string of characters to remove
1350  *
1351  * 's' is modified.
1352  *
1353  * Returns the length of the new string.
1354  */
strip(char * s,const char * reject)1355 static size_t strip(char *s, const char *reject)
1356 {
1357 	char *p = s, *p2 = s;
1358 	size_t length = 0;
1359 	char c;
1360 
1361 	do {
1362 		while ((c = *p2) && strchr(reject, c))
1363 			p2++;
1364 
1365 		*p++ = c = *p2++;
1366 		length++;
1367 	} while (c);
1368 
1369 	return length;
1370 }
1371 
1372 /**
1373  * r535_gsp_rpc_set_registry - build registry RPC and call GSP-RM
1374  * @gsp: gsp pointer
1375  *
1376  * The GSP-RM registry is a set of key/value pairs that configure some aspects
1377  * of GSP-RM. The keys are strings, and the values are 32-bit integers.
1378  *
1379  * The registry is built from a combination of a static hard-coded list (see
1380  * above) and entries passed on the driver's command line.
1381  */
1382 static int
r535_gsp_rpc_set_registry(struct nvkm_gsp * gsp)1383 r535_gsp_rpc_set_registry(struct nvkm_gsp *gsp)
1384 {
1385 	PACKED_REGISTRY_TABLE *rpc;
1386 	unsigned int i;
1387 	int ret;
1388 
1389 	INIT_LIST_HEAD(&gsp->registry_list);
1390 	gsp->registry_rpc_size = sizeof(PACKED_REGISTRY_TABLE);
1391 
1392 	for (i = 0; i < NV_GSP_REG_NUM_ENTRIES; i++) {
1393 		ret = add_registry_num(gsp, r535_registry_entries[i].name,
1394 				       r535_registry_entries[i].value);
1395 		if (ret)
1396 			goto fail;
1397 	}
1398 
1399 	/*
1400 	 * The NVreg_RegistryDwords parameter is a string of key=value
1401 	 * pairs separated by semicolons. We need to extract and trim each
1402 	 * substring, and then parse the substring to extract the key and
1403 	 * value.
1404 	 */
1405 	if (NVreg_RegistryDwords) {
1406 		char *p = kstrdup(NVreg_RegistryDwords, GFP_KERNEL);
1407 		char *start, *next = p, *equal;
1408 
1409 		if (!p) {
1410 			ret = -ENOMEM;
1411 			goto fail;
1412 		}
1413 
1414 		/* Remove any whitespace from the parameter string */
1415 		strip(p, " \t\n");
1416 
1417 		while ((start = strsep(&next, ";"))) {
1418 			long value;
1419 
1420 			equal = strchr(start, '=');
1421 			if (!equal || equal == start || equal[1] == 0) {
1422 				nvkm_error(&gsp->subdev,
1423 					   "ignoring invalid registry string '%s'\n",
1424 					   start);
1425 				continue;
1426 			}
1427 
1428 			/* Truncate the key=value string to just key */
1429 			*equal = 0;
1430 
1431 			ret = kstrtol(equal + 1, 0, &value);
1432 			if (!ret) {
1433 				ret = add_registry_num(gsp, start, value);
1434 			} else {
1435 				/* Not a number, so treat it as a string */
1436 				ret = add_registry_string(gsp, start, equal + 1);
1437 			}
1438 
1439 			if (ret) {
1440 				nvkm_error(&gsp->subdev,
1441 					   "ignoring invalid registry key/value '%s=%s'\n",
1442 					   start, equal + 1);
1443 				continue;
1444 			}
1445 		}
1446 
1447 		kfree(p);
1448 	}
1449 
1450 	rpc = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_SET_REGISTRY, gsp->registry_rpc_size);
1451 	if (IS_ERR(rpc)) {
1452 		ret = PTR_ERR(rpc);
1453 		goto fail;
1454 	}
1455 
1456 	build_registry(gsp, rpc);
1457 
1458 	return nvkm_gsp_rpc_wr(gsp, rpc, false);
1459 
1460 fail:
1461 	clean_registry(gsp);
1462 	return ret;
1463 }
1464 
1465 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
1466 static void
r535_gsp_acpi_caps(acpi_handle handle,CAPS_METHOD_DATA * caps)1467 r535_gsp_acpi_caps(acpi_handle handle, CAPS_METHOD_DATA *caps)
1468 {
1469 	const guid_t NVOP_DSM_GUID =
1470 		GUID_INIT(0xA486D8F8, 0x0BDA, 0x471B,
1471 			  0xA7, 0x2B, 0x60, 0x42, 0xA6, 0xB5, 0xBE, 0xE0);
1472 	u64 NVOP_DSM_REV = 0x00000100;
1473 	union acpi_object argv4 = {
1474 		.buffer.type    = ACPI_TYPE_BUFFER,
1475 		.buffer.length  = 4,
1476 		.buffer.pointer = kmalloc(argv4.buffer.length, GFP_KERNEL),
1477 	}, *obj;
1478 
1479 	caps->status = 0xffff;
1480 
1481 	if (!acpi_check_dsm(handle, &NVOP_DSM_GUID, NVOP_DSM_REV, BIT_ULL(0x1a)))
1482 		return;
1483 
1484 	obj = acpi_evaluate_dsm(handle, &NVOP_DSM_GUID, NVOP_DSM_REV, 0x1a, &argv4);
1485 	if (!obj)
1486 		return;
1487 
1488 	if (WARN_ON(obj->type != ACPI_TYPE_BUFFER) ||
1489 	    WARN_ON(obj->buffer.length != 4))
1490 		return;
1491 
1492 	caps->status = 0;
1493 	caps->optimusCaps = *(u32 *)obj->buffer.pointer;
1494 
1495 	ACPI_FREE(obj);
1496 
1497 	kfree(argv4.buffer.pointer);
1498 }
1499 
1500 static void
r535_gsp_acpi_jt(acpi_handle handle,JT_METHOD_DATA * jt)1501 r535_gsp_acpi_jt(acpi_handle handle, JT_METHOD_DATA *jt)
1502 {
1503 	const guid_t JT_DSM_GUID =
1504 		GUID_INIT(0xCBECA351L, 0x067B, 0x4924,
1505 			  0x9C, 0xBD, 0xB4, 0x6B, 0x00, 0xB8, 0x6F, 0x34);
1506 	u64 JT_DSM_REV = 0x00000103;
1507 	u32 caps;
1508 	union acpi_object argv4 = {
1509 		.buffer.type    = ACPI_TYPE_BUFFER,
1510 		.buffer.length  = sizeof(caps),
1511 		.buffer.pointer = kmalloc(argv4.buffer.length, GFP_KERNEL),
1512 	}, *obj;
1513 
1514 	jt->status = 0xffff;
1515 
1516 	obj = acpi_evaluate_dsm(handle, &JT_DSM_GUID, JT_DSM_REV, 0x1, &argv4);
1517 	if (!obj)
1518 		return;
1519 
1520 	if (WARN_ON(obj->type != ACPI_TYPE_BUFFER) ||
1521 	    WARN_ON(obj->buffer.length != 4))
1522 		return;
1523 
1524 	jt->status = 0;
1525 	jt->jtCaps = *(u32 *)obj->buffer.pointer;
1526 	jt->jtRevId = (jt->jtCaps & 0xfff00000) >> 20;
1527 	jt->bSBIOSCaps = 0;
1528 
1529 	ACPI_FREE(obj);
1530 
1531 	kfree(argv4.buffer.pointer);
1532 }
1533 
1534 static void
r535_gsp_acpi_mux_id(acpi_handle handle,u32 id,MUX_METHOD_DATA_ELEMENT * mode,MUX_METHOD_DATA_ELEMENT * part)1535 r535_gsp_acpi_mux_id(acpi_handle handle, u32 id, MUX_METHOD_DATA_ELEMENT *mode,
1536 						 MUX_METHOD_DATA_ELEMENT *part)
1537 {
1538 	union acpi_object mux_arg = { ACPI_TYPE_INTEGER };
1539 	struct acpi_object_list input = { 1, &mux_arg };
1540 	acpi_handle iter = NULL, handle_mux = NULL;
1541 	acpi_status status;
1542 	unsigned long long value;
1543 
1544 	mode->status = 0xffff;
1545 	part->status = 0xffff;
1546 
1547 	do {
1548 		status = acpi_get_next_object(ACPI_TYPE_DEVICE, handle, iter, &iter);
1549 		if (ACPI_FAILURE(status) || !iter)
1550 			return;
1551 
1552 		status = acpi_evaluate_integer(iter, "_ADR", NULL, &value);
1553 		if (ACPI_FAILURE(status) || value != id)
1554 			continue;
1555 
1556 		handle_mux = iter;
1557 	} while (!handle_mux);
1558 
1559 	if (!handle_mux)
1560 		return;
1561 
1562 	/* I -think- 0 means "acquire" according to nvidia's driver source */
1563 	input.pointer->integer.type = ACPI_TYPE_INTEGER;
1564 	input.pointer->integer.value = 0;
1565 
1566 	status = acpi_evaluate_integer(handle_mux, "MXDM", &input, &value);
1567 	if (ACPI_SUCCESS(status)) {
1568 		mode->acpiId = id;
1569 		mode->mode   = value;
1570 		mode->status = 0;
1571 	}
1572 
1573 	status = acpi_evaluate_integer(handle_mux, "MXDS", &input, &value);
1574 	if (ACPI_SUCCESS(status)) {
1575 		part->acpiId = id;
1576 		part->mode   = value;
1577 		part->status = 0;
1578 	}
1579 }
1580 
1581 static void
r535_gsp_acpi_mux(acpi_handle handle,DOD_METHOD_DATA * dod,MUX_METHOD_DATA * mux)1582 r535_gsp_acpi_mux(acpi_handle handle, DOD_METHOD_DATA *dod, MUX_METHOD_DATA *mux)
1583 {
1584 	mux->tableLen = dod->acpiIdListLen / sizeof(dod->acpiIdList[0]);
1585 
1586 	for (int i = 0; i < mux->tableLen; i++) {
1587 		r535_gsp_acpi_mux_id(handle, dod->acpiIdList[i], &mux->acpiIdMuxModeTable[i],
1588 								 &mux->acpiIdMuxPartTable[i]);
1589 	}
1590 }
1591 
1592 static void
r535_gsp_acpi_dod(acpi_handle handle,DOD_METHOD_DATA * dod)1593 r535_gsp_acpi_dod(acpi_handle handle, DOD_METHOD_DATA *dod)
1594 {
1595 	acpi_status status;
1596 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1597 	union acpi_object *_DOD;
1598 
1599 	dod->status = 0xffff;
1600 
1601 	status = acpi_evaluate_object(handle, "_DOD", NULL, &output);
1602 	if (ACPI_FAILURE(status))
1603 		return;
1604 
1605 	_DOD = output.pointer;
1606 
1607 	if (WARN_ON(_DOD->type != ACPI_TYPE_PACKAGE) ||
1608 	    WARN_ON(_DOD->package.count > ARRAY_SIZE(dod->acpiIdList)))
1609 		return;
1610 
1611 	for (int i = 0; i < _DOD->package.count; i++) {
1612 		if (WARN_ON(_DOD->package.elements[i].type != ACPI_TYPE_INTEGER))
1613 			return;
1614 
1615 		dod->acpiIdList[i] = _DOD->package.elements[i].integer.value;
1616 		dod->acpiIdListLen += sizeof(dod->acpiIdList[0]);
1617 	}
1618 
1619 	dod->status = 0;
1620 	kfree(output.pointer);
1621 }
1622 #endif
1623 
1624 static void
r535_gsp_acpi_info(struct nvkm_gsp * gsp,ACPI_METHOD_DATA * acpi)1625 r535_gsp_acpi_info(struct nvkm_gsp *gsp, ACPI_METHOD_DATA *acpi)
1626 {
1627 #if defined(CONFIG_ACPI) && defined(CONFIG_X86)
1628 	acpi_handle handle = ACPI_HANDLE(gsp->subdev.device->dev);
1629 
1630 	if (!handle)
1631 		return;
1632 
1633 	acpi->bValid = 1;
1634 
1635 	r535_gsp_acpi_dod(handle, &acpi->dodMethodData);
1636 	if (acpi->dodMethodData.status == 0)
1637 		r535_gsp_acpi_mux(handle, &acpi->dodMethodData, &acpi->muxMethodData);
1638 
1639 	r535_gsp_acpi_jt(handle, &acpi->jtMethodData);
1640 	r535_gsp_acpi_caps(handle, &acpi->capsMethodData);
1641 #endif
1642 }
1643 
1644 static int
r535_gsp_rpc_set_system_info(struct nvkm_gsp * gsp)1645 r535_gsp_rpc_set_system_info(struct nvkm_gsp *gsp)
1646 {
1647 	struct nvkm_device *device = gsp->subdev.device;
1648 	struct nvkm_device_pci *pdev = container_of(device, typeof(*pdev), device);
1649 	GspSystemInfo *info;
1650 
1651 	if (WARN_ON(device->type == NVKM_DEVICE_TEGRA))
1652 		return -ENOSYS;
1653 
1654 	info = nvkm_gsp_rpc_get(gsp, NV_VGPU_MSG_FUNCTION_GSP_SET_SYSTEM_INFO, sizeof(*info));
1655 	if (IS_ERR(info))
1656 		return PTR_ERR(info);
1657 
1658 	info->gpuPhysAddr = device->func->resource_addr(device, 0);
1659 	info->gpuPhysFbAddr = device->func->resource_addr(device, 1);
1660 	info->gpuPhysInstAddr = device->func->resource_addr(device, 3);
1661 	info->nvDomainBusDeviceFunc = pci_dev_id(pdev->pdev);
1662 	info->maxUserVa = TASK_SIZE;
1663 	info->pciConfigMirrorBase = 0x088000;
1664 	info->pciConfigMirrorSize = 0x001000;
1665 	r535_gsp_acpi_info(gsp, &info->acpiMethodData);
1666 
1667 	return nvkm_gsp_rpc_wr(gsp, info, false);
1668 }
1669 
1670 static int
r535_gsp_msg_os_error_log(void * priv,u32 fn,void * repv,u32 repc)1671 r535_gsp_msg_os_error_log(void *priv, u32 fn, void *repv, u32 repc)
1672 {
1673 	struct nvkm_gsp *gsp = priv;
1674 	struct nvkm_subdev *subdev = &gsp->subdev;
1675 	rpc_os_error_log_v17_00 *msg = repv;
1676 
1677 	if (WARN_ON(repc < sizeof(*msg)))
1678 		return -EINVAL;
1679 
1680 	nvkm_error(subdev, "Xid:%d %s\n", msg->exceptType, msg->errString);
1681 	return 0;
1682 }
1683 
1684 static int
r535_gsp_msg_rc_triggered(void * priv,u32 fn,void * repv,u32 repc)1685 r535_gsp_msg_rc_triggered(void *priv, u32 fn, void *repv, u32 repc)
1686 {
1687 	rpc_rc_triggered_v17_02 *msg = repv;
1688 	struct nvkm_gsp *gsp = priv;
1689 	struct nvkm_subdev *subdev = &gsp->subdev;
1690 	struct nvkm_chan *chan;
1691 	unsigned long flags;
1692 
1693 	if (WARN_ON(repc < sizeof(*msg)))
1694 		return -EINVAL;
1695 
1696 	nvkm_error(subdev, "rc engn:%08x chid:%d type:%d scope:%d part:%d\n",
1697 		   msg->nv2080EngineType, msg->chid, msg->exceptType, msg->scope,
1698 		   msg->partitionAttributionId);
1699 
1700 	chan = nvkm_chan_get_chid(&subdev->device->fifo->engine, msg->chid / 8, &flags);
1701 	if (!chan) {
1702 		nvkm_error(subdev, "rc chid:%d not found!\n", msg->chid);
1703 		return 0;
1704 	}
1705 
1706 	nvkm_chan_error(chan, false);
1707 	nvkm_chan_put(&chan, flags);
1708 	return 0;
1709 }
1710 
1711 static int
r535_gsp_msg_mmu_fault_queued(void * priv,u32 fn,void * repv,u32 repc)1712 r535_gsp_msg_mmu_fault_queued(void *priv, u32 fn, void *repv, u32 repc)
1713 {
1714 	struct nvkm_gsp *gsp = priv;
1715 	struct nvkm_subdev *subdev = &gsp->subdev;
1716 
1717 	WARN_ON(repc != 0);
1718 
1719 	nvkm_error(subdev, "mmu fault queued\n");
1720 	return 0;
1721 }
1722 
1723 static int
r535_gsp_msg_post_event(void * priv,u32 fn,void * repv,u32 repc)1724 r535_gsp_msg_post_event(void *priv, u32 fn, void *repv, u32 repc)
1725 {
1726 	struct nvkm_gsp *gsp = priv;
1727 	struct nvkm_gsp_client *client;
1728 	struct nvkm_subdev *subdev = &gsp->subdev;
1729 	rpc_post_event_v17_00 *msg = repv;
1730 
1731 	if (WARN_ON(repc < sizeof(*msg)))
1732 		return -EINVAL;
1733 	if (WARN_ON(repc != sizeof(*msg) + msg->eventDataSize))
1734 		return -EINVAL;
1735 
1736 	nvkm_debug(subdev, "event: %08x %08x %d %08x %08x %d %d\n",
1737 		   msg->hClient, msg->hEvent, msg->notifyIndex, msg->data,
1738 		   msg->status, msg->eventDataSize, msg->bNotifyList);
1739 
1740 	mutex_lock(&gsp->client_id.mutex);
1741 	client = idr_find(&gsp->client_id.idr, msg->hClient & 0xffff);
1742 	if (client) {
1743 		struct nvkm_gsp_event *event;
1744 		bool handled = false;
1745 
1746 		list_for_each_entry(event, &client->events, head) {
1747 			if (event->object.handle == msg->hEvent) {
1748 				event->func(event, msg->eventData, msg->eventDataSize);
1749 				handled = true;
1750 			}
1751 		}
1752 
1753 		if (!handled) {
1754 			nvkm_error(subdev, "event: cid 0x%08x event 0x%08x not found!\n",
1755 				   msg->hClient, msg->hEvent);
1756 		}
1757 	} else {
1758 		nvkm_error(subdev, "event: cid 0x%08x not found!\n", msg->hClient);
1759 	}
1760 	mutex_unlock(&gsp->client_id.mutex);
1761 	return 0;
1762 }
1763 
1764 /**
1765  * r535_gsp_msg_run_cpu_sequencer() -- process I/O commands from the GSP
1766  * @priv: gsp pointer
1767  * @fn: function number (ignored)
1768  * @repv: pointer to libos print RPC
1769  * @repc: message size
1770  *
1771  * The GSP sequencer is a list of I/O commands that the GSP can send to
1772  * the driver to perform for various purposes.  The most common usage is to
1773  * perform a special mid-initialization reset.
1774  */
1775 static int
r535_gsp_msg_run_cpu_sequencer(void * priv,u32 fn,void * repv,u32 repc)1776 r535_gsp_msg_run_cpu_sequencer(void *priv, u32 fn, void *repv, u32 repc)
1777 {
1778 	struct nvkm_gsp *gsp = priv;
1779 	struct nvkm_subdev *subdev = &gsp->subdev;
1780 	struct nvkm_device *device = subdev->device;
1781 	rpc_run_cpu_sequencer_v17_00 *seq = repv;
1782 	int ptr = 0, ret;
1783 
1784 	nvkm_debug(subdev, "seq: %08x %08x\n", seq->bufferSizeDWord, seq->cmdIndex);
1785 
1786 	while (ptr < seq->cmdIndex) {
1787 		GSP_SEQUENCER_BUFFER_CMD *cmd = (void *)&seq->commandBuffer[ptr];
1788 
1789 		ptr += 1;
1790 		ptr += GSP_SEQUENCER_PAYLOAD_SIZE_DWORDS(cmd->opCode);
1791 
1792 		switch (cmd->opCode) {
1793 		case GSP_SEQ_BUF_OPCODE_REG_WRITE: {
1794 			u32 addr = cmd->payload.regWrite.addr;
1795 			u32 data = cmd->payload.regWrite.val;
1796 
1797 			nvkm_trace(subdev, "seq wr32 %06x %08x\n", addr, data);
1798 			nvkm_wr32(device, addr, data);
1799 		}
1800 			break;
1801 		case GSP_SEQ_BUF_OPCODE_REG_MODIFY: {
1802 			u32 addr = cmd->payload.regModify.addr;
1803 			u32 mask = cmd->payload.regModify.mask;
1804 			u32 data = cmd->payload.regModify.val;
1805 
1806 			nvkm_trace(subdev, "seq mask %06x %08x %08x\n", addr, mask, data);
1807 			nvkm_mask(device, addr, mask, data);
1808 		}
1809 			break;
1810 		case GSP_SEQ_BUF_OPCODE_REG_POLL: {
1811 			u32 addr = cmd->payload.regPoll.addr;
1812 			u32 mask = cmd->payload.regPoll.mask;
1813 			u32 data = cmd->payload.regPoll.val;
1814 			u32 usec = cmd->payload.regPoll.timeout ?: 4000000;
1815 			//u32 error = cmd->payload.regPoll.error;
1816 
1817 			nvkm_trace(subdev, "seq poll %06x %08x %08x %d\n", addr, mask, data, usec);
1818 			nvkm_rd32(device, addr);
1819 			nvkm_usec(device, usec,
1820 				if ((nvkm_rd32(device, addr) & mask) == data)
1821 					break;
1822 			);
1823 		}
1824 			break;
1825 		case GSP_SEQ_BUF_OPCODE_DELAY_US: {
1826 			u32 usec = cmd->payload.delayUs.val;
1827 
1828 			nvkm_trace(subdev, "seq usec %d\n", usec);
1829 			udelay(usec);
1830 		}
1831 			break;
1832 		case GSP_SEQ_BUF_OPCODE_REG_STORE: {
1833 			u32 addr = cmd->payload.regStore.addr;
1834 			u32 slot = cmd->payload.regStore.index;
1835 
1836 			seq->regSaveArea[slot] = nvkm_rd32(device, addr);
1837 			nvkm_trace(subdev, "seq save %08x -> %d: %08x\n", addr, slot,
1838 				   seq->regSaveArea[slot]);
1839 		}
1840 			break;
1841 		case GSP_SEQ_BUF_OPCODE_CORE_RESET:
1842 			nvkm_trace(subdev, "seq core reset\n");
1843 			nvkm_falcon_reset(&gsp->falcon);
1844 			nvkm_falcon_mask(&gsp->falcon, 0x624, 0x00000080, 0x00000080);
1845 			nvkm_falcon_wr32(&gsp->falcon, 0x10c, 0x00000000);
1846 			break;
1847 		case GSP_SEQ_BUF_OPCODE_CORE_START:
1848 			nvkm_trace(subdev, "seq core start\n");
1849 			if (nvkm_falcon_rd32(&gsp->falcon, 0x100) & 0x00000040)
1850 				nvkm_falcon_wr32(&gsp->falcon, 0x130, 0x00000002);
1851 			else
1852 				nvkm_falcon_wr32(&gsp->falcon, 0x100, 0x00000002);
1853 			break;
1854 		case GSP_SEQ_BUF_OPCODE_CORE_WAIT_FOR_HALT:
1855 			nvkm_trace(subdev, "seq core wait halt\n");
1856 			nvkm_msec(device, 2000,
1857 				if (nvkm_falcon_rd32(&gsp->falcon, 0x100) & 0x00000010)
1858 					break;
1859 			);
1860 			break;
1861 		case GSP_SEQ_BUF_OPCODE_CORE_RESUME: {
1862 			struct nvkm_sec2 *sec2 = device->sec2;
1863 			u32 mbox0;
1864 
1865 			nvkm_trace(subdev, "seq core resume\n");
1866 
1867 			ret = gsp->func->reset(gsp);
1868 			if (WARN_ON(ret))
1869 				return ret;
1870 
1871 			nvkm_falcon_wr32(&gsp->falcon, 0x040, lower_32_bits(gsp->libos.addr));
1872 			nvkm_falcon_wr32(&gsp->falcon, 0x044, upper_32_bits(gsp->libos.addr));
1873 
1874 			nvkm_falcon_start(&sec2->falcon);
1875 
1876 			if (nvkm_msec(device, 2000,
1877 				if (nvkm_rd32(device, 0x1180f8) & 0x04000000)
1878 					break;
1879 			) < 0)
1880 				return -ETIMEDOUT;
1881 
1882 			mbox0 = nvkm_falcon_rd32(&sec2->falcon, 0x040);
1883 			if (WARN_ON(mbox0)) {
1884 				nvkm_error(&gsp->subdev, "seq core resume sec2: 0x%x\n", mbox0);
1885 				return -EIO;
1886 			}
1887 
1888 			nvkm_falcon_wr32(&gsp->falcon, 0x080, gsp->boot.app_version);
1889 
1890 			if (WARN_ON(!nvkm_falcon_riscv_active(&gsp->falcon)))
1891 				return -EIO;
1892 		}
1893 			break;
1894 		default:
1895 			nvkm_error(subdev, "unknown sequencer opcode %08x\n", cmd->opCode);
1896 			return -EINVAL;
1897 		}
1898 	}
1899 
1900 	return 0;
1901 }
1902 
1903 static int
r535_gsp_booter_unload(struct nvkm_gsp * gsp,u32 mbox0,u32 mbox1)1904 r535_gsp_booter_unload(struct nvkm_gsp *gsp, u32 mbox0, u32 mbox1)
1905 {
1906 	struct nvkm_subdev *subdev = &gsp->subdev;
1907 	struct nvkm_device *device = subdev->device;
1908 	u32 wpr2_hi;
1909 	int ret;
1910 
1911 	wpr2_hi = nvkm_rd32(device, 0x1fa828);
1912 	if (!wpr2_hi) {
1913 		nvkm_debug(subdev, "WPR2 not set - skipping booter unload\n");
1914 		return 0;
1915 	}
1916 
1917 	ret = nvkm_falcon_fw_boot(&gsp->booter.unload, &gsp->subdev, true, &mbox0, &mbox1, 0, 0);
1918 	if (WARN_ON(ret))
1919 		return ret;
1920 
1921 	wpr2_hi = nvkm_rd32(device, 0x1fa828);
1922 	if (WARN_ON(wpr2_hi))
1923 		return -EIO;
1924 
1925 	return 0;
1926 }
1927 
1928 static int
r535_gsp_booter_load(struct nvkm_gsp * gsp,u32 mbox0,u32 mbox1)1929 r535_gsp_booter_load(struct nvkm_gsp *gsp, u32 mbox0, u32 mbox1)
1930 {
1931 	int ret;
1932 
1933 	ret = nvkm_falcon_fw_boot(&gsp->booter.load, &gsp->subdev, true, &mbox0, &mbox1, 0, 0);
1934 	if (ret)
1935 		return ret;
1936 
1937 	nvkm_falcon_wr32(&gsp->falcon, 0x080, gsp->boot.app_version);
1938 
1939 	if (WARN_ON(!nvkm_falcon_riscv_active(&gsp->falcon)))
1940 		return -EIO;
1941 
1942 	return 0;
1943 }
1944 
1945 static int
r535_gsp_wpr_meta_init(struct nvkm_gsp * gsp)1946 r535_gsp_wpr_meta_init(struct nvkm_gsp *gsp)
1947 {
1948 	GspFwWprMeta *meta;
1949 	int ret;
1950 
1951 	ret = nvkm_gsp_mem_ctor(gsp, 0x1000, &gsp->wpr_meta);
1952 	if (ret)
1953 		return ret;
1954 
1955 	meta = gsp->wpr_meta.data;
1956 
1957 	meta->magic = GSP_FW_WPR_META_MAGIC;
1958 	meta->revision = GSP_FW_WPR_META_REVISION;
1959 
1960 	meta->sysmemAddrOfRadix3Elf = gsp->radix3.lvl0.addr;
1961 	meta->sizeOfRadix3Elf = gsp->fb.wpr2.elf.size;
1962 
1963 	meta->sysmemAddrOfBootloader = gsp->boot.fw.addr;
1964 	meta->sizeOfBootloader = gsp->boot.fw.size;
1965 	meta->bootloaderCodeOffset = gsp->boot.code_offset;
1966 	meta->bootloaderDataOffset = gsp->boot.data_offset;
1967 	meta->bootloaderManifestOffset = gsp->boot.manifest_offset;
1968 
1969 	meta->sysmemAddrOfSignature = gsp->sig.addr;
1970 	meta->sizeOfSignature = gsp->sig.size;
1971 
1972 	meta->gspFwRsvdStart = gsp->fb.heap.addr;
1973 	meta->nonWprHeapOffset = gsp->fb.heap.addr;
1974 	meta->nonWprHeapSize = gsp->fb.heap.size;
1975 	meta->gspFwWprStart = gsp->fb.wpr2.addr;
1976 	meta->gspFwHeapOffset = gsp->fb.wpr2.heap.addr;
1977 	meta->gspFwHeapSize = gsp->fb.wpr2.heap.size;
1978 	meta->gspFwOffset = gsp->fb.wpr2.elf.addr;
1979 	meta->bootBinOffset = gsp->fb.wpr2.boot.addr;
1980 	meta->frtsOffset = gsp->fb.wpr2.frts.addr;
1981 	meta->frtsSize = gsp->fb.wpr2.frts.size;
1982 	meta->gspFwWprEnd = ALIGN_DOWN(gsp->fb.bios.vga_workspace.addr, 0x20000);
1983 	meta->fbSize = gsp->fb.size;
1984 	meta->vgaWorkspaceOffset = gsp->fb.bios.vga_workspace.addr;
1985 	meta->vgaWorkspaceSize = gsp->fb.bios.vga_workspace.size;
1986 	meta->bootCount = 0;
1987 	meta->partitionRpcAddr = 0;
1988 	meta->partitionRpcRequestOffset = 0;
1989 	meta->partitionRpcReplyOffset = 0;
1990 	meta->verified = 0;
1991 	return 0;
1992 }
1993 
1994 static int
r535_gsp_shared_init(struct nvkm_gsp * gsp)1995 r535_gsp_shared_init(struct nvkm_gsp *gsp)
1996 {
1997 	struct {
1998 		msgqTxHeader tx;
1999 		msgqRxHeader rx;
2000 	} *cmdq, *msgq;
2001 	int ret, i;
2002 
2003 	gsp->shm.cmdq.size = 0x40000;
2004 	gsp->shm.msgq.size = 0x40000;
2005 
2006 	gsp->shm.ptes.nr  = (gsp->shm.cmdq.size + gsp->shm.msgq.size) >> GSP_PAGE_SHIFT;
2007 	gsp->shm.ptes.nr += DIV_ROUND_UP(gsp->shm.ptes.nr * sizeof(u64), GSP_PAGE_SIZE);
2008 	gsp->shm.ptes.size = ALIGN(gsp->shm.ptes.nr * sizeof(u64), GSP_PAGE_SIZE);
2009 
2010 	ret = nvkm_gsp_mem_ctor(gsp, gsp->shm.ptes.size +
2011 				     gsp->shm.cmdq.size +
2012 				     gsp->shm.msgq.size,
2013 				&gsp->shm.mem);
2014 	if (ret)
2015 		return ret;
2016 
2017 	gsp->shm.ptes.ptr = gsp->shm.mem.data;
2018 	gsp->shm.cmdq.ptr = (u8 *)gsp->shm.ptes.ptr + gsp->shm.ptes.size;
2019 	gsp->shm.msgq.ptr = (u8 *)gsp->shm.cmdq.ptr + gsp->shm.cmdq.size;
2020 
2021 	for (i = 0; i < gsp->shm.ptes.nr; i++)
2022 		gsp->shm.ptes.ptr[i] = gsp->shm.mem.addr + (i << GSP_PAGE_SHIFT);
2023 
2024 	cmdq = gsp->shm.cmdq.ptr;
2025 	cmdq->tx.version = 0;
2026 	cmdq->tx.size = gsp->shm.cmdq.size;
2027 	cmdq->tx.entryOff = GSP_PAGE_SIZE;
2028 	cmdq->tx.msgSize = GSP_PAGE_SIZE;
2029 	cmdq->tx.msgCount = (cmdq->tx.size - cmdq->tx.entryOff) / cmdq->tx.msgSize;
2030 	cmdq->tx.writePtr = 0;
2031 	cmdq->tx.flags = 1;
2032 	cmdq->tx.rxHdrOff = offsetof(typeof(*cmdq), rx.readPtr);
2033 
2034 	msgq = gsp->shm.msgq.ptr;
2035 
2036 	gsp->cmdq.cnt = cmdq->tx.msgCount;
2037 	gsp->cmdq.wptr = &cmdq->tx.writePtr;
2038 	gsp->cmdq.rptr = &msgq->rx.readPtr;
2039 	gsp->msgq.cnt = cmdq->tx.msgCount;
2040 	gsp->msgq.wptr = &msgq->tx.writePtr;
2041 	gsp->msgq.rptr = &cmdq->rx.readPtr;
2042 	return 0;
2043 }
2044 
2045 static int
r535_gsp_rmargs_init(struct nvkm_gsp * gsp,bool resume)2046 r535_gsp_rmargs_init(struct nvkm_gsp *gsp, bool resume)
2047 {
2048 	GSP_ARGUMENTS_CACHED *args;
2049 	int ret;
2050 
2051 	if (!resume) {
2052 		ret = r535_gsp_shared_init(gsp);
2053 		if (ret)
2054 			return ret;
2055 
2056 		ret = nvkm_gsp_mem_ctor(gsp, 0x1000, &gsp->rmargs);
2057 		if (ret)
2058 			return ret;
2059 	}
2060 
2061 	args = gsp->rmargs.data;
2062 	args->messageQueueInitArguments.sharedMemPhysAddr = gsp->shm.mem.addr;
2063 	args->messageQueueInitArguments.pageTableEntryCount = gsp->shm.ptes.nr;
2064 	args->messageQueueInitArguments.cmdQueueOffset =
2065 		(u8 *)gsp->shm.cmdq.ptr - (u8 *)gsp->shm.mem.data;
2066 	args->messageQueueInitArguments.statQueueOffset =
2067 		(u8 *)gsp->shm.msgq.ptr - (u8 *)gsp->shm.mem.data;
2068 
2069 	if (!resume) {
2070 		args->srInitArguments.oldLevel = 0;
2071 		args->srInitArguments.flags = 0;
2072 		args->srInitArguments.bInPMTransition = 0;
2073 	} else {
2074 		args->srInitArguments.oldLevel = NV2080_CTRL_GPU_SET_POWER_STATE_GPU_LEVEL_3;
2075 		args->srInitArguments.flags = 0;
2076 		args->srInitArguments.bInPMTransition = 1;
2077 	}
2078 
2079 	return 0;
2080 }
2081 
2082 #ifdef CONFIG_DEBUG_FS
2083 
2084 /*
2085  * If GSP-RM load fails, then the GSP nvkm object will be deleted, the logging
2086  * debugfs entries will be deleted, and it will not be possible to debug the
2087  * load failure. The keep_gsp_logging parameter tells Nouveau to copy the
2088  * logging buffers to new debugfs entries, and these entries are retained
2089  * until the driver unloads.
2090  */
2091 static bool keep_gsp_logging;
2092 module_param(keep_gsp_logging, bool, 0444);
2093 MODULE_PARM_DESC(keep_gsp_logging,
2094 		 "Migrate the GSP-RM logging debugfs entries upon exit");
2095 
2096 /*
2097  * GSP-RM uses a pseudo-class mechanism to define of a variety of per-"engine"
2098  * data structures, and each engine has a "class ID" genererated by a
2099  * pre-processor. This is the class ID for the PMU.
2100  */
2101 #define NV_GSP_MSG_EVENT_UCODE_LIBOS_CLASS_PMU		0xf3d722
2102 
2103 /**
2104  * rpc_ucode_libos_print_v1E_08 - RPC payload for libos print buffers
2105  * @ucode_eng_desc: the engine descriptor
2106  * @libos_print_buf_size: the size of the libos_print_buf[]
2107  * @libos_print_buf: the actual buffer
2108  *
2109  * The engine descriptor is divided into 31:8 "class ID" and 7:0 "instance
2110  * ID". We only care about messages from PMU.
2111  */
2112 struct rpc_ucode_libos_print_v1e_08 {
2113 	u32 ucode_eng_desc;
2114 	u32 libos_print_buf_size;
2115 	u8 libos_print_buf[];
2116 };
2117 
2118 /**
2119  * r535_gsp_msg_libos_print - capture log message from the PMU
2120  * @priv: gsp pointer
2121  * @fn: function number (ignored)
2122  * @repv: pointer to libos print RPC
2123  * @repc: message size
2124  *
2125  * Called when we receive a UCODE_LIBOS_PRINT event RPC from GSP-RM. This RPC
2126  * contains the contents of the libos print buffer from PMU. It is typically
2127  * only written to when PMU encounters an error.
2128  *
2129  * Technically this RPC can be used to pass print buffers from any number of
2130  * GSP-RM engines, but we only expect to receive them for the PMU.
2131  *
2132  * For the PMU, the buffer is 4K in size and the RPC always contains the full
2133  * contents.
2134  */
2135 static int
r535_gsp_msg_libos_print(void * priv,u32 fn,void * repv,u32 repc)2136 r535_gsp_msg_libos_print(void *priv, u32 fn, void *repv, u32 repc)
2137 {
2138 	struct nvkm_gsp *gsp = priv;
2139 	struct nvkm_subdev *subdev = &gsp->subdev;
2140 	struct rpc_ucode_libos_print_v1e_08 *rpc = repv;
2141 	unsigned int class = rpc->ucode_eng_desc >> 8;
2142 
2143 	nvkm_debug(subdev, "received libos print from class 0x%x for %u bytes\n",
2144 		   class, rpc->libos_print_buf_size);
2145 
2146 	if (class != NV_GSP_MSG_EVENT_UCODE_LIBOS_CLASS_PMU) {
2147 		nvkm_warn(subdev,
2148 			  "received libos print from unknown class 0x%x\n",
2149 			  class);
2150 		return -ENOMSG;
2151 	}
2152 
2153 	if (rpc->libos_print_buf_size > GSP_PAGE_SIZE) {
2154 		nvkm_error(subdev, "libos print is too large (%u bytes)\n",
2155 			   rpc->libos_print_buf_size);
2156 		return -E2BIG;
2157 	}
2158 
2159 	memcpy(gsp->blob_pmu.data, rpc->libos_print_buf, rpc->libos_print_buf_size);
2160 
2161 	return 0;
2162 }
2163 
2164 /**
2165  * create_debufgs - create a blob debugfs entry
2166  * @gsp: gsp pointer
2167  * @name: name of this dentry
2168  * @blob: blob wrapper
2169  *
2170  * Creates a debugfs entry for a logging buffer with the name 'name'.
2171  */
create_debugfs(struct nvkm_gsp * gsp,const char * name,struct debugfs_blob_wrapper * blob)2172 static struct dentry *create_debugfs(struct nvkm_gsp *gsp, const char *name,
2173 				     struct debugfs_blob_wrapper *blob)
2174 {
2175 	struct dentry *dent;
2176 
2177 	dent = debugfs_create_blob(name, 0444, gsp->debugfs.parent, blob);
2178 	if (IS_ERR(dent)) {
2179 		nvkm_error(&gsp->subdev,
2180 			   "failed to create %s debugfs entry\n", name);
2181 		return NULL;
2182 	}
2183 
2184 	/*
2185 	 * For some reason, debugfs_create_blob doesn't set the size of the
2186 	 * dentry, so do that here.  See [1]
2187 	 *
2188 	 * [1] https://lore.kernel.org/r/linux-fsdevel/20240207200619.3354549-1-ttabi@nvidia.com/
2189 	 */
2190 	i_size_write(d_inode(dent), blob->size);
2191 
2192 	return dent;
2193 }
2194 
2195 /**
2196  * r535_gsp_libos_debugfs_init - create logging debugfs entries
2197  * @gsp: gsp pointer
2198  *
2199  * Create the debugfs entries. This exposes the log buffers to userspace so
2200  * that an external tool can parse it.
2201  *
2202  * The 'logpmu' contains exception dumps from the PMU. It is written via an
2203  * RPC sent from GSP-RM and must be only 4KB. We create it here because it's
2204  * only useful if there is a debugfs entry to expose it. If we get the PMU
2205  * logging RPC and there is no debugfs entry, the RPC is just ignored.
2206  *
2207  * The blob_init, blob_rm, and blob_pmu objects can't be transient
2208  * because debugfs_create_blob doesn't copy them.
2209  *
2210  * NOTE: OpenRM loads the logging elf image and prints the log messages
2211  * in real-time. We may add that capability in the future, but that
2212  * requires loading ELF images that are not distributed with the driver and
2213  * adding the parsing code to Nouveau.
2214  *
2215  * Ideally, this should be part of nouveau_debugfs_init(), but that function
2216  * is called too late. We really want to create these debugfs entries before
2217  * r535_gsp_booter_load() is called, so that if GSP-RM fails to initialize,
2218  * there could still be a log to capture.
2219  */
2220 static void
r535_gsp_libos_debugfs_init(struct nvkm_gsp * gsp)2221 r535_gsp_libos_debugfs_init(struct nvkm_gsp *gsp)
2222 {
2223 	struct device *dev = gsp->subdev.device->dev;
2224 
2225 	/* Create a new debugfs directory with a name unique to this GPU. */
2226 	gsp->debugfs.parent = debugfs_create_dir(dev_name(dev), nouveau_debugfs_root);
2227 	if (IS_ERR(gsp->debugfs.parent)) {
2228 		nvkm_error(&gsp->subdev,
2229 			   "failed to create %s debugfs root\n", dev_name(dev));
2230 		return;
2231 	}
2232 
2233 	gsp->blob_init.data = gsp->loginit.data;
2234 	gsp->blob_init.size = gsp->loginit.size;
2235 	gsp->blob_intr.data = gsp->logintr.data;
2236 	gsp->blob_intr.size = gsp->logintr.size;
2237 	gsp->blob_rm.data = gsp->logrm.data;
2238 	gsp->blob_rm.size = gsp->logrm.size;
2239 
2240 	gsp->debugfs.init = create_debugfs(gsp, "loginit", &gsp->blob_init);
2241 	if (!gsp->debugfs.init)
2242 		goto error;
2243 
2244 	gsp->debugfs.intr = create_debugfs(gsp, "logintr", &gsp->blob_intr);
2245 	if (!gsp->debugfs.intr)
2246 		goto error;
2247 
2248 	gsp->debugfs.rm = create_debugfs(gsp, "logrm", &gsp->blob_rm);
2249 	if (!gsp->debugfs.rm)
2250 		goto error;
2251 
2252 	/*
2253 	 * Since the PMU buffer is copied from an RPC, it doesn't need to be
2254 	 * a DMA buffer.
2255 	 */
2256 	gsp->blob_pmu.size = GSP_PAGE_SIZE;
2257 	gsp->blob_pmu.data = kzalloc(gsp->blob_pmu.size, GFP_KERNEL);
2258 	if (!gsp->blob_pmu.data)
2259 		goto error;
2260 
2261 	gsp->debugfs.pmu = create_debugfs(gsp, "logpmu", &gsp->blob_pmu);
2262 	if (!gsp->debugfs.pmu) {
2263 		kfree(gsp->blob_pmu.data);
2264 		goto error;
2265 	}
2266 
2267 	i_size_write(d_inode(gsp->debugfs.init), gsp->blob_init.size);
2268 	i_size_write(d_inode(gsp->debugfs.intr), gsp->blob_intr.size);
2269 	i_size_write(d_inode(gsp->debugfs.rm), gsp->blob_rm.size);
2270 	i_size_write(d_inode(gsp->debugfs.pmu), gsp->blob_pmu.size);
2271 
2272 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT,
2273 			      r535_gsp_msg_libos_print, gsp);
2274 
2275 	nvkm_debug(&gsp->subdev, "created debugfs GSP-RM logging entries\n");
2276 
2277 	if (keep_gsp_logging) {
2278 		nvkm_info(&gsp->subdev,
2279 			  "logging buffers will be retained on failure\n");
2280 	}
2281 
2282 	return;
2283 
2284 error:
2285 	debugfs_remove(gsp->debugfs.parent);
2286 	gsp->debugfs.parent = NULL;
2287 }
2288 
2289 #endif
2290 
2291 static inline u64
r535_gsp_libos_id8(const char * name)2292 r535_gsp_libos_id8(const char *name)
2293 {
2294 	u64 id = 0;
2295 
2296 	for (int i = 0; i < sizeof(id) && *name; i++, name++)
2297 		id = (id << 8) | *name;
2298 
2299 	return id;
2300 }
2301 
2302 /**
2303  * create_pte_array() - creates a PTE array of a physically contiguous buffer
2304  * @ptes: pointer to the array
2305  * @addr: base address of physically contiguous buffer (GSP_PAGE_SIZE aligned)
2306  * @size: size of the buffer
2307  *
2308  * GSP-RM sometimes expects physically-contiguous buffers to have an array of
2309  * "PTEs" for each page in that buffer.  Although in theory that allows for
2310  * the buffer to be physically discontiguous, GSP-RM does not currently
2311  * support that.
2312  *
2313  * In this case, the PTEs are DMA addresses of each page of the buffer.  Since
2314  * the buffer is physically contiguous, calculating all the PTEs is simple
2315  * math.
2316  *
2317  * See memdescGetPhysAddrsForGpu()
2318  */
create_pte_array(u64 * ptes,dma_addr_t addr,size_t size)2319 static void create_pte_array(u64 *ptes, dma_addr_t addr, size_t size)
2320 {
2321 	unsigned int num_pages = DIV_ROUND_UP_ULL(size, GSP_PAGE_SIZE);
2322 	unsigned int i;
2323 
2324 	for (i = 0; i < num_pages; i++)
2325 		ptes[i] = (u64)addr + (i << GSP_PAGE_SHIFT);
2326 }
2327 
2328 /**
2329  * r535_gsp_libos_init() -- create the libos arguments structure
2330  * @gsp: gsp pointer
2331  *
2332  * The logging buffers are byte queues that contain encoded printf-like
2333  * messages from GSP-RM.  They need to be decoded by a special application
2334  * that can parse the buffers.
2335  *
2336  * The 'loginit' buffer contains logs from early GSP-RM init and
2337  * exception dumps.  The 'logrm' buffer contains the subsequent logs. Both are
2338  * written to directly by GSP-RM and can be any multiple of GSP_PAGE_SIZE.
2339  *
2340  * The physical address map for the log buffer is stored in the buffer
2341  * itself, starting with offset 1. Offset 0 contains the "put" pointer (pp).
2342  * Initially, pp is equal to 0. If the buffer has valid logging data in it,
2343  * then pp points to index into the buffer where the next logging entry will
2344  * be written. Therefore, the logging data is valid if:
2345  *   1 <= pp < sizeof(buffer)/sizeof(u64)
2346  *
2347  * The GSP only understands 4K pages (GSP_PAGE_SIZE), so even if the kernel is
2348  * configured for a larger page size (e.g. 64K pages), we need to give
2349  * the GSP an array of 4K pages. Fortunately, since the buffer is
2350  * physically contiguous, it's simple math to calculate the addresses.
2351  *
2352  * The buffers must be a multiple of GSP_PAGE_SIZE.  GSP-RM also currently
2353  * ignores the @kind field for LOGINIT, LOGINTR, and LOGRM, but expects the
2354  * buffers to be physically contiguous anyway.
2355  *
2356  * The memory allocated for the arguments must remain until the GSP sends the
2357  * init_done RPC.
2358  *
2359  * See _kgspInitLibosLoggingStructures (allocates memory for buffers)
2360  * See kgspSetupLibosInitArgs_IMPL (creates pLibosInitArgs[] array)
2361  */
2362 static int
r535_gsp_libos_init(struct nvkm_gsp * gsp)2363 r535_gsp_libos_init(struct nvkm_gsp *gsp)
2364 {
2365 	LibosMemoryRegionInitArgument *args;
2366 	int ret;
2367 
2368 	ret = nvkm_gsp_mem_ctor(gsp, 0x1000, &gsp->libos);
2369 	if (ret)
2370 		return ret;
2371 
2372 	args = gsp->libos.data;
2373 
2374 	ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->loginit);
2375 	if (ret)
2376 		return ret;
2377 
2378 	args[0].id8  = r535_gsp_libos_id8("LOGINIT");
2379 	args[0].pa   = gsp->loginit.addr;
2380 	args[0].size = gsp->loginit.size;
2381 	args[0].kind = LIBOS_MEMORY_REGION_CONTIGUOUS;
2382 	args[0].loc  = LIBOS_MEMORY_REGION_LOC_SYSMEM;
2383 	create_pte_array(gsp->loginit.data + sizeof(u64), gsp->loginit.addr, gsp->loginit.size);
2384 
2385 	ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->logintr);
2386 	if (ret)
2387 		return ret;
2388 
2389 	args[1].id8  = r535_gsp_libos_id8("LOGINTR");
2390 	args[1].pa   = gsp->logintr.addr;
2391 	args[1].size = gsp->logintr.size;
2392 	args[1].kind = LIBOS_MEMORY_REGION_CONTIGUOUS;
2393 	args[1].loc  = LIBOS_MEMORY_REGION_LOC_SYSMEM;
2394 	create_pte_array(gsp->logintr.data + sizeof(u64), gsp->logintr.addr, gsp->logintr.size);
2395 
2396 	ret = nvkm_gsp_mem_ctor(gsp, 0x10000, &gsp->logrm);
2397 	if (ret)
2398 		return ret;
2399 
2400 	args[2].id8  = r535_gsp_libos_id8("LOGRM");
2401 	args[2].pa   = gsp->logrm.addr;
2402 	args[2].size = gsp->logrm.size;
2403 	args[2].kind = LIBOS_MEMORY_REGION_CONTIGUOUS;
2404 	args[2].loc  = LIBOS_MEMORY_REGION_LOC_SYSMEM;
2405 	create_pte_array(gsp->logrm.data + sizeof(u64), gsp->logrm.addr, gsp->logrm.size);
2406 
2407 	ret = r535_gsp_rmargs_init(gsp, false);
2408 	if (ret)
2409 		return ret;
2410 
2411 	args[3].id8  = r535_gsp_libos_id8("RMARGS");
2412 	args[3].pa   = gsp->rmargs.addr;
2413 	args[3].size = gsp->rmargs.size;
2414 	args[3].kind = LIBOS_MEMORY_REGION_CONTIGUOUS;
2415 	args[3].loc  = LIBOS_MEMORY_REGION_LOC_SYSMEM;
2416 
2417 #ifdef CONFIG_DEBUG_FS
2418 	r535_gsp_libos_debugfs_init(gsp);
2419 #endif
2420 
2421 	return 0;
2422 }
2423 
2424 void
nvkm_gsp_sg_free(struct nvkm_device * device,struct sg_table * sgt)2425 nvkm_gsp_sg_free(struct nvkm_device *device, struct sg_table *sgt)
2426 {
2427 	struct scatterlist *sgl;
2428 	int i;
2429 
2430 	dma_unmap_sgtable(device->dev, sgt, DMA_BIDIRECTIONAL, 0);
2431 
2432 	for_each_sgtable_sg(sgt, sgl, i) {
2433 		struct page *page = sg_page(sgl);
2434 
2435 		__free_page(page);
2436 	}
2437 
2438 	sg_free_table(sgt);
2439 }
2440 
2441 int
nvkm_gsp_sg(struct nvkm_device * device,u64 size,struct sg_table * sgt)2442 nvkm_gsp_sg(struct nvkm_device *device, u64 size, struct sg_table *sgt)
2443 {
2444 	const u64 pages = DIV_ROUND_UP(size, PAGE_SIZE);
2445 	struct scatterlist *sgl;
2446 	int ret, i;
2447 
2448 	ret = sg_alloc_table(sgt, pages, GFP_KERNEL);
2449 	if (ret)
2450 		return ret;
2451 
2452 	for_each_sgtable_sg(sgt, sgl, i) {
2453 		struct page *page = alloc_page(GFP_KERNEL);
2454 
2455 		if (!page) {
2456 			nvkm_gsp_sg_free(device, sgt);
2457 			return -ENOMEM;
2458 		}
2459 
2460 		sg_set_page(sgl, page, PAGE_SIZE, 0);
2461 	}
2462 
2463 	ret = dma_map_sgtable(device->dev, sgt, DMA_BIDIRECTIONAL, 0);
2464 	if (ret)
2465 		nvkm_gsp_sg_free(device, sgt);
2466 
2467 	return ret;
2468 }
2469 
2470 static void
nvkm_gsp_radix3_dtor(struct nvkm_gsp * gsp,struct nvkm_gsp_radix3 * rx3)2471 nvkm_gsp_radix3_dtor(struct nvkm_gsp *gsp, struct nvkm_gsp_radix3 *rx3)
2472 {
2473 	nvkm_gsp_sg_free(gsp->subdev.device, &rx3->lvl2);
2474 	nvkm_gsp_mem_dtor(&rx3->lvl1);
2475 	nvkm_gsp_mem_dtor(&rx3->lvl0);
2476 }
2477 
2478 /**
2479  * nvkm_gsp_radix3_sg - build a radix3 table from a S/G list
2480  * @gsp: gsp pointer
2481  * @sgt: S/G list to traverse
2482  * @size: size of the image, in bytes
2483  * @rx3: radix3 array to update
2484  *
2485  * The GSP uses a three-level page table, called radix3, to map the firmware.
2486  * Each 64-bit "pointer" in the table is either the bus address of an entry in
2487  * the next table (for levels 0 and 1) or the bus address of the next page in
2488  * the GSP firmware image itself.
2489  *
2490  * Level 0 contains a single entry in one page that points to the first page
2491  * of level 1.
2492  *
2493  * Level 1, since it's also only one page in size, contains up to 512 entries,
2494  * one for each page in Level 2.
2495  *
2496  * Level 2 can be up to 512 pages in size, and each of those entries points to
2497  * the next page of the firmware image.  Since there can be up to 512*512
2498  * pages, that limits the size of the firmware to 512*512*GSP_PAGE_SIZE = 1GB.
2499  *
2500  * Internally, the GSP has its window into system memory, but the base
2501  * physical address of the aperture is not 0.  In fact, it varies depending on
2502  * the GPU architecture.  Since the GPU is a PCI device, this window is
2503  * accessed via DMA and is therefore bound by IOMMU translation.  The end
2504  * result is that GSP-RM must translate the bus addresses in the table to GSP
2505  * physical addresses.  All this should happen transparently.
2506  *
2507  * Returns 0 on success, or negative error code
2508  *
2509  * See kgspCreateRadix3_IMPL
2510  */
2511 static int
nvkm_gsp_radix3_sg(struct nvkm_gsp * gsp,struct sg_table * sgt,u64 size,struct nvkm_gsp_radix3 * rx3)2512 nvkm_gsp_radix3_sg(struct nvkm_gsp *gsp, struct sg_table *sgt, u64 size,
2513 		   struct nvkm_gsp_radix3 *rx3)
2514 {
2515 	struct sg_dma_page_iter sg_dma_iter;
2516 	struct scatterlist *sg;
2517 	size_t bufsize;
2518 	u64 *pte;
2519 	int ret, i, page_idx = 0;
2520 
2521 	ret = nvkm_gsp_mem_ctor(gsp, GSP_PAGE_SIZE, &rx3->lvl0);
2522 	if (ret)
2523 		return ret;
2524 
2525 	ret = nvkm_gsp_mem_ctor(gsp, GSP_PAGE_SIZE, &rx3->lvl1);
2526 	if (ret)
2527 		goto lvl1_fail;
2528 
2529 	// Allocate level 2
2530 	bufsize = ALIGN((size / GSP_PAGE_SIZE) * sizeof(u64), GSP_PAGE_SIZE);
2531 	ret = nvkm_gsp_sg(gsp->subdev.device, bufsize, &rx3->lvl2);
2532 	if (ret)
2533 		goto lvl2_fail;
2534 
2535 	// Write the bus address of level 1 to level 0
2536 	pte = rx3->lvl0.data;
2537 	*pte = rx3->lvl1.addr;
2538 
2539 	// Write the bus address of each page in level 2 to level 1
2540 	pte = rx3->lvl1.data;
2541 	for_each_sgtable_dma_page(&rx3->lvl2, &sg_dma_iter, 0)
2542 		*pte++ = sg_page_iter_dma_address(&sg_dma_iter);
2543 
2544 	// Finally, write the bus address of each page in sgt to level 2
2545 	for_each_sgtable_sg(&rx3->lvl2, sg, i) {
2546 		void *sgl_end;
2547 
2548 		pte = sg_virt(sg);
2549 		sgl_end = (void *)pte + sg->length;
2550 
2551 		for_each_sgtable_dma_page(sgt, &sg_dma_iter, page_idx) {
2552 			*pte++ = sg_page_iter_dma_address(&sg_dma_iter);
2553 			page_idx++;
2554 
2555 			// Go to the next scatterlist for level 2 if we've reached the end
2556 			if ((void *)pte >= sgl_end)
2557 				break;
2558 		}
2559 	}
2560 
2561 	if (ret) {
2562 lvl2_fail:
2563 		nvkm_gsp_mem_dtor(&rx3->lvl1);
2564 lvl1_fail:
2565 		nvkm_gsp_mem_dtor(&rx3->lvl0);
2566 	}
2567 
2568 	return ret;
2569 }
2570 
2571 int
r535_gsp_fini(struct nvkm_gsp * gsp,bool suspend)2572 r535_gsp_fini(struct nvkm_gsp *gsp, bool suspend)
2573 {
2574 	u32 mbox0 = 0xff, mbox1 = 0xff;
2575 	int ret;
2576 
2577 	if (!gsp->running)
2578 		return 0;
2579 
2580 	if (suspend) {
2581 		GspFwWprMeta *meta = gsp->wpr_meta.data;
2582 		u64 len = meta->gspFwWprEnd - meta->gspFwWprStart;
2583 		GspFwSRMeta *sr;
2584 
2585 		ret = nvkm_gsp_sg(gsp->subdev.device, len, &gsp->sr.sgt);
2586 		if (ret)
2587 			return ret;
2588 
2589 		ret = nvkm_gsp_radix3_sg(gsp, &gsp->sr.sgt, len, &gsp->sr.radix3);
2590 		if (ret)
2591 			return ret;
2592 
2593 		ret = nvkm_gsp_mem_ctor(gsp, sizeof(*sr), &gsp->sr.meta);
2594 		if (ret)
2595 			return ret;
2596 
2597 		sr = gsp->sr.meta.data;
2598 		sr->magic = GSP_FW_SR_META_MAGIC;
2599 		sr->revision = GSP_FW_SR_META_REVISION;
2600 		sr->sysmemAddrOfSuspendResumeData = gsp->sr.radix3.lvl0.addr;
2601 		sr->sizeOfSuspendResumeData = len;
2602 
2603 		mbox0 = lower_32_bits(gsp->sr.meta.addr);
2604 		mbox1 = upper_32_bits(gsp->sr.meta.addr);
2605 	}
2606 
2607 	ret = r535_gsp_rpc_unloading_guest_driver(gsp, suspend);
2608 	if (WARN_ON(ret))
2609 		return ret;
2610 
2611 	nvkm_msec(gsp->subdev.device, 2000,
2612 		if (nvkm_falcon_rd32(&gsp->falcon, 0x040) & 0x80000000)
2613 			break;
2614 	);
2615 
2616 	nvkm_falcon_reset(&gsp->falcon);
2617 
2618 	ret = nvkm_gsp_fwsec_sb(gsp);
2619 	WARN_ON(ret);
2620 
2621 	ret = r535_gsp_booter_unload(gsp, mbox0, mbox1);
2622 	WARN_ON(ret);
2623 
2624 	gsp->running = false;
2625 	return 0;
2626 }
2627 
2628 int
r535_gsp_init(struct nvkm_gsp * gsp)2629 r535_gsp_init(struct nvkm_gsp *gsp)
2630 {
2631 	u32 mbox0, mbox1;
2632 	int ret;
2633 
2634 	if (!gsp->sr.meta.data) {
2635 		mbox0 = lower_32_bits(gsp->wpr_meta.addr);
2636 		mbox1 = upper_32_bits(gsp->wpr_meta.addr);
2637 	} else {
2638 		r535_gsp_rmargs_init(gsp, true);
2639 
2640 		mbox0 = lower_32_bits(gsp->sr.meta.addr);
2641 		mbox1 = upper_32_bits(gsp->sr.meta.addr);
2642 	}
2643 
2644 	/* Execute booter to handle (eventually...) booting GSP-RM. */
2645 	ret = r535_gsp_booter_load(gsp, mbox0, mbox1);
2646 	if (WARN_ON(ret))
2647 		goto done;
2648 
2649 	ret = r535_gsp_rpc_poll(gsp, NV_VGPU_MSG_EVENT_GSP_INIT_DONE);
2650 	if (ret)
2651 		goto done;
2652 
2653 	gsp->running = true;
2654 
2655 done:
2656 	if (gsp->sr.meta.data) {
2657 		nvkm_gsp_mem_dtor(&gsp->sr.meta);
2658 		nvkm_gsp_radix3_dtor(gsp, &gsp->sr.radix3);
2659 		nvkm_gsp_sg_free(gsp->subdev.device, &gsp->sr.sgt);
2660 		return ret;
2661 	}
2662 
2663 	if (ret == 0)
2664 		ret = r535_gsp_postinit(gsp);
2665 
2666 	return ret;
2667 }
2668 
2669 static int
r535_gsp_rm_boot_ctor(struct nvkm_gsp * gsp)2670 r535_gsp_rm_boot_ctor(struct nvkm_gsp *gsp)
2671 {
2672 	const struct firmware *fw = gsp->fws.bl;
2673 	const struct nvfw_bin_hdr *hdr;
2674 	RM_RISCV_UCODE_DESC *desc;
2675 	int ret;
2676 
2677 	hdr = nvfw_bin_hdr(&gsp->subdev, fw->data);
2678 	desc = (void *)fw->data + hdr->header_offset;
2679 
2680 	ret = nvkm_gsp_mem_ctor(gsp, hdr->data_size, &gsp->boot.fw);
2681 	if (ret)
2682 		return ret;
2683 
2684 	memcpy(gsp->boot.fw.data, fw->data + hdr->data_offset, hdr->data_size);
2685 
2686 	gsp->boot.code_offset = desc->monitorCodeOffset;
2687 	gsp->boot.data_offset = desc->monitorDataOffset;
2688 	gsp->boot.manifest_offset = desc->manifestOffset;
2689 	gsp->boot.app_version = desc->appVersion;
2690 	return 0;
2691 }
2692 
2693 static const struct nvkm_firmware_func
2694 r535_gsp_fw = {
2695 	.type = NVKM_FIRMWARE_IMG_SGT,
2696 };
2697 
2698 static int
r535_gsp_elf_section(struct nvkm_gsp * gsp,const char * name,const u8 ** pdata,u64 * psize)2699 r535_gsp_elf_section(struct nvkm_gsp *gsp, const char *name, const u8 **pdata, u64 *psize)
2700 {
2701 	const u8 *img = gsp->fws.rm->data;
2702 	const struct elf64_hdr *ehdr = (const struct elf64_hdr *)img;
2703 	const struct elf64_shdr *shdr = (const struct elf64_shdr *)&img[ehdr->e_shoff];
2704 	const char *names = &img[shdr[ehdr->e_shstrndx].sh_offset];
2705 
2706 	for (int i = 0; i < ehdr->e_shnum; i++, shdr++) {
2707 		if (!strcmp(&names[shdr->sh_name], name)) {
2708 			*pdata = &img[shdr->sh_offset];
2709 			*psize = shdr->sh_size;
2710 			return 0;
2711 		}
2712 	}
2713 
2714 	nvkm_error(&gsp->subdev, "section '%s' not found\n", name);
2715 	return -ENOENT;
2716 }
2717 
2718 static void
r535_gsp_dtor_fws(struct nvkm_gsp * gsp)2719 r535_gsp_dtor_fws(struct nvkm_gsp *gsp)
2720 {
2721 	nvkm_firmware_put(gsp->fws.bl);
2722 	gsp->fws.bl = NULL;
2723 	nvkm_firmware_put(gsp->fws.booter.unload);
2724 	gsp->fws.booter.unload = NULL;
2725 	nvkm_firmware_put(gsp->fws.booter.load);
2726 	gsp->fws.booter.load = NULL;
2727 	nvkm_firmware_put(gsp->fws.rm);
2728 	gsp->fws.rm = NULL;
2729 }
2730 
2731 #ifdef CONFIG_DEBUG_FS
2732 
2733 struct r535_gsp_log {
2734 	struct nvif_log log;
2735 
2736 	/*
2737 	 * Logging buffers in debugfs. The wrapper objects need to remain
2738 	 * in memory until the dentry is deleted.
2739 	 */
2740 	struct dentry *debugfs_logging_dir;
2741 	struct debugfs_blob_wrapper blob_init;
2742 	struct debugfs_blob_wrapper blob_intr;
2743 	struct debugfs_blob_wrapper blob_rm;
2744 	struct debugfs_blob_wrapper blob_pmu;
2745 };
2746 
2747 /**
2748  * r535_debugfs_shutdown - delete GSP-RM logging buffers for one GPU
2749  * @_log: nvif_log struct for this GPU
2750  *
2751  * Called when the driver is shutting down, to clean up the retained GSP-RM
2752  * logging buffers.
2753  */
r535_debugfs_shutdown(struct nvif_log * _log)2754 static void r535_debugfs_shutdown(struct nvif_log *_log)
2755 {
2756 	struct r535_gsp_log *log = container_of(_log, struct r535_gsp_log, log);
2757 
2758 	debugfs_remove(log->debugfs_logging_dir);
2759 
2760 	kfree(log->blob_init.data);
2761 	kfree(log->blob_intr.data);
2762 	kfree(log->blob_rm.data);
2763 	kfree(log->blob_pmu.data);
2764 
2765 	/* We also need to delete the list object */
2766 	kfree(log);
2767 }
2768 
2769 /**
2770  * is_empty - return true if the logging buffer was never written to
2771  * @b: blob wrapper with ->data field pointing to logging buffer
2772  *
2773  * The first 64-bit field of loginit, and logintr, and logrm is the 'put'
2774  * pointer, and it is initialized to 0. It's a dword-based index into the
2775  * circular buffer, indicating where the next printf write will be made.
2776  *
2777  * If the pointer is still 0 when GSP-RM is shut down, that means that the
2778  * buffer was never written to, so it can be ignored.
2779  *
2780  * This test also works for logpmu, even though it doesn't have a put pointer.
2781  */
is_empty(const struct debugfs_blob_wrapper * b)2782 static bool is_empty(const struct debugfs_blob_wrapper *b)
2783 {
2784 	u64 *put = b->data;
2785 
2786 	return put ? (*put == 0) : true;
2787 }
2788 
2789 /**
2790  * r535_gsp_copy_log - preserve the logging buffers in a blob
2791  *
2792  * When GSP shuts down, the nvkm_gsp object and all its memory is deleted.
2793  * To preserve the logging buffers, the buffers need to be copied, but only
2794  * if they actually have data.
2795  */
r535_gsp_copy_log(struct dentry * parent,const char * name,const struct debugfs_blob_wrapper * s,struct debugfs_blob_wrapper * t)2796 static int r535_gsp_copy_log(struct dentry *parent,
2797 			     const char *name,
2798 			     const struct debugfs_blob_wrapper *s,
2799 			     struct debugfs_blob_wrapper *t)
2800 {
2801 	struct dentry *dent;
2802 	void *p;
2803 
2804 	if (is_empty(s))
2805 		return 0;
2806 
2807 	/* The original buffers will be deleted */
2808 	p = kmemdup(s->data, s->size, GFP_KERNEL);
2809 	if (!p)
2810 		return -ENOMEM;
2811 
2812 	t->data = p;
2813 	t->size = s->size;
2814 
2815 	dent = debugfs_create_blob(name, 0444, parent, t);
2816 	if (IS_ERR(dent)) {
2817 		kfree(p);
2818 		memset(t, 0, sizeof(*t));
2819 		return PTR_ERR(dent);
2820 	}
2821 
2822 	i_size_write(d_inode(dent), t->size);
2823 
2824 	return 0;
2825 }
2826 
2827 /**
2828  * r535_gsp_retain_logging - copy logging buffers to new debugfs root
2829  * @gsp: gsp pointer
2830  *
2831  * If keep_gsp_logging is enabled, then we want to preserve the GSP-RM logging
2832  * buffers and their debugfs entries, but all those objects would normally
2833  * deleted if GSP-RM fails to load.
2834  *
2835  * To preserve the logging buffers, we need to:
2836  *
2837  * 1) Allocate new buffers and copy the logs into them, so that the original
2838  * DMA buffers can be released.
2839  *
2840  * 2) Preserve the directories.  We don't need to save single dentries because
2841  * we're going to delete the parent when the
2842  *
2843  * If anything fails in this process, then all the dentries need to be
2844  * deleted.  We don't need to deallocate the original logging buffers because
2845  * the caller will do that regardless.
2846  */
r535_gsp_retain_logging(struct nvkm_gsp * gsp)2847 static void r535_gsp_retain_logging(struct nvkm_gsp *gsp)
2848 {
2849 	struct device *dev = gsp->subdev.device->dev;
2850 	struct r535_gsp_log *log = NULL;
2851 	int ret;
2852 
2853 	if (!keep_gsp_logging || !gsp->debugfs.parent) {
2854 		/* Nothing to do */
2855 		goto exit;
2856 	}
2857 
2858 	/* Check to make sure at least one buffer has data. */
2859 	if (is_empty(&gsp->blob_init) && is_empty(&gsp->blob_intr) &&
2860 	    is_empty(&gsp->blob_rm) && is_empty(&gsp->blob_rm)) {
2861 		nvkm_warn(&gsp->subdev, "all logging buffers are empty\n");
2862 		goto exit;
2863 	}
2864 
2865 	log = kzalloc(sizeof(*log), GFP_KERNEL);
2866 	if (!log)
2867 		goto error;
2868 
2869 	/*
2870 	 * Since the nvkm_gsp object is going away, the debugfs_blob_wrapper
2871 	 * objects are also being deleted, which means the dentries will no
2872 	 * longer be valid.  Delete the existing entries so that we can create
2873 	 * new ones with the same name.
2874 	 */
2875 	debugfs_remove(gsp->debugfs.init);
2876 	debugfs_remove(gsp->debugfs.intr);
2877 	debugfs_remove(gsp->debugfs.rm);
2878 	debugfs_remove(gsp->debugfs.pmu);
2879 
2880 	ret = r535_gsp_copy_log(gsp->debugfs.parent, "loginit", &gsp->blob_init, &log->blob_init);
2881 	if (ret)
2882 		goto error;
2883 
2884 	ret = r535_gsp_copy_log(gsp->debugfs.parent, "logintr", &gsp->blob_intr, &log->blob_intr);
2885 	if (ret)
2886 		goto error;
2887 
2888 	ret = r535_gsp_copy_log(gsp->debugfs.parent, "logrm", &gsp->blob_rm, &log->blob_rm);
2889 	if (ret)
2890 		goto error;
2891 
2892 	ret = r535_gsp_copy_log(gsp->debugfs.parent, "logpmu", &gsp->blob_pmu, &log->blob_pmu);
2893 	if (ret)
2894 		goto error;
2895 
2896 	/* The nvkm_gsp object is going away, so save the dentry */
2897 	log->debugfs_logging_dir = gsp->debugfs.parent;
2898 
2899 	log->log.shutdown = r535_debugfs_shutdown;
2900 	list_add(&log->log.entry, &gsp_logs.head);
2901 
2902 	nvkm_warn(&gsp->subdev,
2903 		  "logging buffers migrated to /sys/kernel/debug/nouveau/%s\n",
2904 		  dev_name(dev));
2905 
2906 	return;
2907 
2908 error:
2909 	nvkm_warn(&gsp->subdev, "failed to migrate logging buffers\n");
2910 
2911 exit:
2912 	debugfs_remove(gsp->debugfs.parent);
2913 
2914 	if (log) {
2915 		kfree(log->blob_init.data);
2916 		kfree(log->blob_intr.data);
2917 		kfree(log->blob_rm.data);
2918 		kfree(log->blob_pmu.data);
2919 		kfree(log);
2920 	}
2921 }
2922 
2923 #endif
2924 
2925 /**
2926  * r535_gsp_libos_debugfs_fini - cleanup/retain log buffers on shutdown
2927  * @gsp: gsp pointer
2928  *
2929  * If the log buffers are exposed via debugfs, the data for those entries
2930  * needs to be cleaned up when the GSP device shuts down.
2931  */
2932 static void
r535_gsp_libos_debugfs_fini(struct nvkm_gsp __maybe_unused * gsp)2933 r535_gsp_libos_debugfs_fini(struct nvkm_gsp __maybe_unused *gsp)
2934 {
2935 #ifdef CONFIG_DEBUG_FS
2936 	r535_gsp_retain_logging(gsp);
2937 
2938 	/*
2939 	 * Unlike the other buffers, the PMU blob is a kmalloc'd buffer that
2940 	 * exists only if the debugfs entries were created.
2941 	 */
2942 	kfree(gsp->blob_pmu.data);
2943 	gsp->blob_pmu.data = NULL;
2944 #endif
2945 }
2946 
2947 void
r535_gsp_dtor(struct nvkm_gsp * gsp)2948 r535_gsp_dtor(struct nvkm_gsp *gsp)
2949 {
2950 	idr_destroy(&gsp->client_id.idr);
2951 	mutex_destroy(&gsp->client_id.mutex);
2952 
2953 	nvkm_gsp_radix3_dtor(gsp, &gsp->radix3);
2954 	nvkm_gsp_mem_dtor(&gsp->sig);
2955 	nvkm_firmware_dtor(&gsp->fw);
2956 
2957 	nvkm_falcon_fw_dtor(&gsp->booter.unload);
2958 	nvkm_falcon_fw_dtor(&gsp->booter.load);
2959 
2960 	mutex_destroy(&gsp->msgq.mutex);
2961 	mutex_destroy(&gsp->cmdq.mutex);
2962 
2963 	r535_gsp_dtor_fws(gsp);
2964 
2965 	nvkm_gsp_mem_dtor(&gsp->rmargs);
2966 	nvkm_gsp_mem_dtor(&gsp->wpr_meta);
2967 	nvkm_gsp_mem_dtor(&gsp->shm.mem);
2968 
2969 	r535_gsp_libos_debugfs_fini(gsp);
2970 
2971 	nvkm_gsp_mem_dtor(&gsp->loginit);
2972 	nvkm_gsp_mem_dtor(&gsp->logintr);
2973 	nvkm_gsp_mem_dtor(&gsp->logrm);
2974 }
2975 
2976 int
r535_gsp_oneinit(struct nvkm_gsp * gsp)2977 r535_gsp_oneinit(struct nvkm_gsp *gsp)
2978 {
2979 	struct nvkm_device *device = gsp->subdev.device;
2980 	const u8 *data;
2981 	u64 size;
2982 	int ret;
2983 
2984 	mutex_init(&gsp->cmdq.mutex);
2985 	mutex_init(&gsp->msgq.mutex);
2986 
2987 	ret = gsp->func->booter.ctor(gsp, "booter-load", gsp->fws.booter.load,
2988 				     &device->sec2->falcon, &gsp->booter.load);
2989 	if (ret)
2990 		return ret;
2991 
2992 	ret = gsp->func->booter.ctor(gsp, "booter-unload", gsp->fws.booter.unload,
2993 				     &device->sec2->falcon, &gsp->booter.unload);
2994 	if (ret)
2995 		return ret;
2996 
2997 	/* Load GSP firmware from ELF image into DMA-accessible memory. */
2998 	ret = r535_gsp_elf_section(gsp, ".fwimage", &data, &size);
2999 	if (ret)
3000 		return ret;
3001 
3002 	ret = nvkm_firmware_ctor(&r535_gsp_fw, "gsp-rm", device, data, size, &gsp->fw);
3003 	if (ret)
3004 		return ret;
3005 
3006 	/* Load relevant signature from ELF image. */
3007 	ret = r535_gsp_elf_section(gsp, gsp->func->sig_section, &data, &size);
3008 	if (ret)
3009 		return ret;
3010 
3011 	ret = nvkm_gsp_mem_ctor(gsp, ALIGN(size, 256), &gsp->sig);
3012 	if (ret)
3013 		return ret;
3014 
3015 	memcpy(gsp->sig.data, data, size);
3016 
3017 	/* Build radix3 page table for ELF image. */
3018 	ret = nvkm_gsp_radix3_sg(gsp, &gsp->fw.mem.sgt, gsp->fw.len, &gsp->radix3);
3019 	if (ret)
3020 		return ret;
3021 
3022 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_GSP_RUN_CPU_SEQUENCER,
3023 			      r535_gsp_msg_run_cpu_sequencer, gsp);
3024 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_POST_EVENT, r535_gsp_msg_post_event, gsp);
3025 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_RC_TRIGGERED,
3026 			      r535_gsp_msg_rc_triggered, gsp);
3027 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_MMU_FAULT_QUEUED,
3028 			      r535_gsp_msg_mmu_fault_queued, gsp);
3029 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_OS_ERROR_LOG, r535_gsp_msg_os_error_log, gsp);
3030 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_PERF_BRIDGELESS_INFO_UPDATE, NULL, NULL);
3031 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_UCODE_LIBOS_PRINT, NULL, NULL);
3032 	r535_gsp_msg_ntfy_add(gsp, NV_VGPU_MSG_EVENT_GSP_SEND_USER_SHARED_DATA, NULL, NULL);
3033 	ret = r535_gsp_rm_boot_ctor(gsp);
3034 	if (ret)
3035 		return ret;
3036 
3037 	/* Release FW images - we've copied them to DMA buffers now. */
3038 	r535_gsp_dtor_fws(gsp);
3039 
3040 	/* Calculate FB layout. */
3041 	gsp->fb.wpr2.frts.size = 0x100000;
3042 	gsp->fb.wpr2.frts.addr = ALIGN_DOWN(gsp->fb.bios.addr, 0x20000) - gsp->fb.wpr2.frts.size;
3043 
3044 	gsp->fb.wpr2.boot.size = gsp->boot.fw.size;
3045 	gsp->fb.wpr2.boot.addr = ALIGN_DOWN(gsp->fb.wpr2.frts.addr - gsp->fb.wpr2.boot.size, 0x1000);
3046 
3047 	gsp->fb.wpr2.elf.size = gsp->fw.len;
3048 	gsp->fb.wpr2.elf.addr = ALIGN_DOWN(gsp->fb.wpr2.boot.addr - gsp->fb.wpr2.elf.size, 0x10000);
3049 
3050 	{
3051 		u32 fb_size_gb = DIV_ROUND_UP_ULL(gsp->fb.size, 1 << 30);
3052 
3053 		gsp->fb.wpr2.heap.size =
3054 			gsp->func->wpr_heap.os_carveout_size +
3055 			gsp->func->wpr_heap.base_size +
3056 			ALIGN(GSP_FW_HEAP_PARAM_SIZE_PER_GB_FB * fb_size_gb, 1 << 20) +
3057 			ALIGN(GSP_FW_HEAP_PARAM_CLIENT_ALLOC_SIZE, 1 << 20);
3058 
3059 		gsp->fb.wpr2.heap.size = max(gsp->fb.wpr2.heap.size, gsp->func->wpr_heap.min_size);
3060 	}
3061 
3062 	gsp->fb.wpr2.heap.addr = ALIGN_DOWN(gsp->fb.wpr2.elf.addr - gsp->fb.wpr2.heap.size, 0x100000);
3063 	gsp->fb.wpr2.heap.size = ALIGN_DOWN(gsp->fb.wpr2.elf.addr - gsp->fb.wpr2.heap.addr, 0x100000);
3064 
3065 	gsp->fb.wpr2.addr = ALIGN_DOWN(gsp->fb.wpr2.heap.addr - sizeof(GspFwWprMeta), 0x100000);
3066 	gsp->fb.wpr2.size = gsp->fb.wpr2.frts.addr + gsp->fb.wpr2.frts.size - gsp->fb.wpr2.addr;
3067 
3068 	gsp->fb.heap.size = 0x100000;
3069 	gsp->fb.heap.addr = gsp->fb.wpr2.addr - gsp->fb.heap.size;
3070 
3071 	ret = nvkm_gsp_fwsec_frts(gsp);
3072 	if (WARN_ON(ret))
3073 		return ret;
3074 
3075 	ret = r535_gsp_libos_init(gsp);
3076 	if (WARN_ON(ret))
3077 		return ret;
3078 
3079 	ret = r535_gsp_wpr_meta_init(gsp);
3080 	if (WARN_ON(ret))
3081 		return ret;
3082 
3083 	ret = r535_gsp_rpc_set_system_info(gsp);
3084 	if (WARN_ON(ret))
3085 		return ret;
3086 
3087 	ret = r535_gsp_rpc_set_registry(gsp);
3088 	if (WARN_ON(ret))
3089 		return ret;
3090 
3091 	/* Reset GSP into RISC-V mode. */
3092 	ret = gsp->func->reset(gsp);
3093 	if (WARN_ON(ret))
3094 		return ret;
3095 
3096 	nvkm_falcon_wr32(&gsp->falcon, 0x040, lower_32_bits(gsp->libos.addr));
3097 	nvkm_falcon_wr32(&gsp->falcon, 0x044, upper_32_bits(gsp->libos.addr));
3098 
3099 	mutex_init(&gsp->client_id.mutex);
3100 	idr_init(&gsp->client_id.idr);
3101 	return 0;
3102 }
3103 
3104 static int
r535_gsp_load_fw(struct nvkm_gsp * gsp,const char * name,const char * ver,const struct firmware ** pfw)3105 r535_gsp_load_fw(struct nvkm_gsp *gsp, const char *name, const char *ver,
3106 		 const struct firmware **pfw)
3107 {
3108 	char fwname[64];
3109 
3110 	snprintf(fwname, sizeof(fwname), "gsp/%s-%s", name, ver);
3111 	return nvkm_firmware_get(&gsp->subdev, fwname, 0, pfw);
3112 }
3113 
3114 int
r535_gsp_load(struct nvkm_gsp * gsp,int ver,const struct nvkm_gsp_fwif * fwif)3115 r535_gsp_load(struct nvkm_gsp *gsp, int ver, const struct nvkm_gsp_fwif *fwif)
3116 {
3117 	struct nvkm_subdev *subdev = &gsp->subdev;
3118 	int ret;
3119 	bool enable_gsp = fwif->enable;
3120 
3121 #if IS_ENABLED(CONFIG_DRM_NOUVEAU_GSP_DEFAULT)
3122 	enable_gsp = true;
3123 #endif
3124 	if (!nvkm_boolopt(subdev->device->cfgopt, "NvGspRm", enable_gsp))
3125 		return -EINVAL;
3126 
3127 	if ((ret = r535_gsp_load_fw(gsp, "gsp", fwif->ver, &gsp->fws.rm)) ||
3128 	    (ret = r535_gsp_load_fw(gsp, "booter_load", fwif->ver, &gsp->fws.booter.load)) ||
3129 	    (ret = r535_gsp_load_fw(gsp, "booter_unload", fwif->ver, &gsp->fws.booter.unload)) ||
3130 	    (ret = r535_gsp_load_fw(gsp, "bootloader", fwif->ver, &gsp->fws.bl))) {
3131 		r535_gsp_dtor_fws(gsp);
3132 		return ret;
3133 	}
3134 
3135 	return 0;
3136 }
3137 
3138 #define NVKM_GSP_FIRMWARE(chip)                                  \
3139 MODULE_FIRMWARE("nvidia/"#chip"/gsp/booter_load-535.113.01.bin");   \
3140 MODULE_FIRMWARE("nvidia/"#chip"/gsp/booter_unload-535.113.01.bin"); \
3141 MODULE_FIRMWARE("nvidia/"#chip"/gsp/bootloader-535.113.01.bin");    \
3142 MODULE_FIRMWARE("nvidia/"#chip"/gsp/gsp-535.113.01.bin")
3143 
3144 NVKM_GSP_FIRMWARE(tu102);
3145 NVKM_GSP_FIRMWARE(tu104);
3146 NVKM_GSP_FIRMWARE(tu106);
3147 
3148 NVKM_GSP_FIRMWARE(tu116);
3149 NVKM_GSP_FIRMWARE(tu117);
3150 
3151 NVKM_GSP_FIRMWARE(ga100);
3152 
3153 NVKM_GSP_FIRMWARE(ga102);
3154 NVKM_GSP_FIRMWARE(ga103);
3155 NVKM_GSP_FIRMWARE(ga104);
3156 NVKM_GSP_FIRMWARE(ga106);
3157 NVKM_GSP_FIRMWARE(ga107);
3158 
3159 NVKM_GSP_FIRMWARE(ad102);
3160 NVKM_GSP_FIRMWARE(ad103);
3161 NVKM_GSP_FIRMWARE(ad104);
3162 NVKM_GSP_FIRMWARE(ad106);
3163 NVKM_GSP_FIRMWARE(ad107);
3164