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