xref: /linux/tools/testing/selftests/vfio/lib/drivers/nv_falcon/nv_falcon.c (revision a625b2a387628df94e385faf5c81bf252f304ed9)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
4  */
5 #include <stdint.h>
6 #include <strings.h>
7 #include <unistd.h>
8 #include <stdbool.h>
9 #include <string.h>
10 #include <time.h>
11 
12 #include <linux/errno.h>
13 #include <linux/io.h>
14 #include <linux/pci_ids.h>
15 
16 #include <libvfio.h>
17 
18 #include "hw.h"
19 
20 struct gpu_device {
21 	enum gpu_arch arch;
22 	void *bar0;
23 	bool is_memory_clear_supported;
24 	const struct falcon *falcon;
25 	u32 pmc_enable_mask;
26 	bool fsp_dma_enabled;
27 
28 	/* Pending memcpy parameters, set by memcpy_start() */
29 	u64 memcpy_src;
30 	u64 memcpy_dst;
31 	u64 memcpy_size;
32 };
33 
to_gpu_device(struct vfio_pci_device * device)34 static inline struct gpu_device *to_gpu_device(struct vfio_pci_device *device)
35 {
36 	return device->driver.region.vaddr;
37 }
38 
nv_gpu_arch_lookup(u32 pmc_boot_0)39 static enum gpu_arch nv_gpu_arch_lookup(u32 pmc_boot_0)
40 {
41 	u32 arch = (pmc_boot_0 >> 24) & 0x1f;
42 
43 	switch (arch) {
44 	case 0x0e:
45 	case 0x0f:
46 	case 0x10:
47 		return GPU_ARCH_KEPLER;
48 	case 0x11:
49 		return GPU_ARCH_MAXWELL_GEN1;
50 	case 0x12:
51 		return GPU_ARCH_MAXWELL_GEN2;
52 	case 0x13:
53 		/* P100 (impl 0) uses PMC reset; P4/P40 use engine reset */
54 		if (((pmc_boot_0 >> 20) & 0xf) == 0)
55 			return GPU_ARCH_PASCAL;
56 		return GPU_ARCH_PASCAL_10X;
57 	case 0x14:
58 		return GPU_ARCH_VOLTA;
59 	case 0x16:
60 		return GPU_ARCH_TURING;
61 	case 0x17:
62 		return GPU_ARCH_AMPERE;
63 	case 0x18:
64 		return GPU_ARCH_HOPPER;
65 	case 0x19:
66 		return GPU_ARCH_ADA;
67 	default:
68 		return GPU_ARCH_UNKNOWN;
69 	}
70 }
71 
gpu_read32(struct gpu_device * gpu,u32 offset)72 static inline u32 gpu_read32(struct gpu_device *gpu, u32 offset)
73 {
74 	return readl(gpu->bar0 + offset);
75 }
76 
gpu_write32(struct gpu_device * gpu,u32 offset,u32 value)77 static inline void gpu_write32(struct gpu_device *gpu, u32 offset, u32 value)
78 {
79 	writel(value, gpu->bar0 + offset);
80 }
81 
get_elapsed_ms(struct timespec * start)82 static u64 get_elapsed_ms(struct timespec *start)
83 {
84 	struct timespec now;
85 
86 	clock_gettime(CLOCK_MONOTONIC, &now);
87 
88 	return (now.tv_sec - start->tv_sec) * 1000
89 	       + (now.tv_nsec - start->tv_nsec) / 1000000;
90 }
91 
gpu_poll_register(struct vfio_pci_device * device,const char * name,u32 offset,u32 expected,u32 mask,u32 timeout_ms)92 static int gpu_poll_register(struct vfio_pci_device *device,
93 			     const char *name, u32 offset,
94 			     u32 expected, u32 mask, u32 timeout_ms)
95 {
96 	struct gpu_device *gpu = to_gpu_device(device);
97 	struct timespec start;
98 	u64 elapsed_ms;
99 	u32 value;
100 
101 	clock_gettime(CLOCK_MONOTONIC, &start);
102 
103 	for (;;) {
104 		value = gpu_read32(gpu, offset);
105 		if ((value & mask) == expected)
106 			return 0;
107 
108 		elapsed_ms = get_elapsed_ms(&start);
109 
110 		if (elapsed_ms >= timeout_ms)
111 			break;
112 
113 		usleep(1000);
114 	}
115 
116 	dev_err(device,
117 		"Timeout polling %s (0x%x): value=0x%x expected=0x%x mask=0x%x after %lu ms\n",
118 		name, offset, value, expected, mask, elapsed_ms);
119 	return -ETIMEDOUT;
120 }
121 
fsp_poll_queue(struct vfio_pci_device * device,const char * name,u32 head_reg,u32 tail_reg,bool wait_empty,u32 timeout_ms)122 static int fsp_poll_queue(struct vfio_pci_device *device, const char *name,
123 			  u32 head_reg, u32 tail_reg, bool wait_empty,
124 			  u32 timeout_ms)
125 {
126 	struct gpu_device *gpu = to_gpu_device(device);
127 	struct timespec start;
128 	u64 elapsed_ms;
129 	u32 head, tail;
130 
131 	clock_gettime(CLOCK_MONOTONIC, &start);
132 
133 	for (;;) {
134 		head = gpu_read32(gpu, head_reg);
135 		tail = gpu_read32(gpu, tail_reg);
136 		if (wait_empty ? (head == tail) : (head != tail))
137 			return 0;
138 
139 		elapsed_ms = get_elapsed_ms(&start);
140 
141 		if (elapsed_ms >= timeout_ms)
142 			break;
143 
144 		usleep(1000);
145 	}
146 
147 	dev_err(device,
148 		"Timeout polling %s: head=0x%x tail=0x%x wait_empty=%d after %lu ms\n",
149 		name, head, tail, wait_empty, elapsed_ms);
150 	return -ETIMEDOUT;
151 }
152 
fsp_emem_write(struct vfio_pci_device * device,u32 offset,const u32 * data,u32 count)153 static void fsp_emem_write(struct vfio_pci_device *device, u32 offset,
154 			   const u32 *data, u32 count)
155 {
156 	struct gpu_device *gpu = to_gpu_device(device);
157 	u32 i;
158 
159 	/* Configure port with auto-increment for read and write */
160 	gpu_write32(gpu, NV_FSP_EMEM_PORT2_CTRL,
161 		    offset | NV_FALCON_EMEMC_AINCR | NV_FALCON_EMEMC_AINCW);
162 
163 	for (i = 0; i < count; i++)
164 		gpu_write32(gpu, NV_FSP_EMEM_PORT2_DATA, data[i]);
165 }
166 
fsp_emem_read(struct vfio_pci_device * device,u32 offset,u32 * data,u32 count)167 static void fsp_emem_read(struct vfio_pci_device *device, u32 offset,
168 			  u32 *data, u32 count)
169 {
170 	struct gpu_device *gpu = to_gpu_device(device);
171 	u32 i;
172 
173 	/* Configure port with auto-increment for read and write */
174 	gpu_write32(gpu, NV_FSP_EMEM_PORT2_CTRL,
175 		    offset | NV_FALCON_EMEMC_AINCR | NV_FALCON_EMEMC_AINCW);
176 
177 	for (i = 0; i < count; i++)
178 		data[i] = gpu_read32(gpu, NV_FSP_EMEM_PORT2_DATA);
179 }
180 
fsp_rpc_send_data(struct vfio_pci_device * device,const u32 * data,u32 count)181 static int fsp_rpc_send_data(struct vfio_pci_device *device, const u32 *data,
182 			     u32 count)
183 {
184 	struct gpu_device *gpu = to_gpu_device(device);
185 	int ret;
186 
187 	ret = fsp_poll_queue(device, "fsp_cmd_queue_empty",
188 			     NV_FSP_QUEUE_HEAD, NV_FSP_QUEUE_TAIL, true, 1000);
189 	if (ret)
190 		return ret;
191 
192 	fsp_emem_write(device, NV_FSP_RPC_EMEM_BASE, data, count);
193 
194 	/* Update queue head/tail to signal data is ready */
195 	gpu_write32(gpu, NV_FSP_QUEUE_TAIL,
196 		    NV_FSP_RPC_EMEM_BASE + (count - 1) * 4);
197 	gpu_write32(gpu, NV_FSP_QUEUE_HEAD, NV_FSP_RPC_EMEM_BASE);
198 
199 	return ret;
200 }
201 
fsp_rpc_receive_data(struct vfio_pci_device * device,u32 * data,u32 max_count,u32 timeout_ms)202 static int fsp_rpc_receive_data(struct vfio_pci_device *device, u32 *data,
203 				u32 max_count, u32 timeout_ms)
204 {
205 	struct gpu_device *gpu = to_gpu_device(device);
206 	u32 head, tail;
207 	u32 msg_size_words;
208 	int ret;
209 
210 	ret = fsp_poll_queue(device, "fsp_msg_queue_ready",
211 			     NV_FSP_MSG_QUEUE_HEAD, NV_FSP_MSG_QUEUE_TAIL,
212 			     false, timeout_ms);
213 	if (ret)
214 		return ret;
215 
216 	head = gpu_read32(gpu, NV_FSP_MSG_QUEUE_HEAD);
217 	tail = gpu_read32(gpu, NV_FSP_MSG_QUEUE_TAIL);
218 
219 	msg_size_words = (tail - head + 4) / 4;
220 	if (msg_size_words > max_count)
221 		msg_size_words = max_count;
222 
223 	fsp_emem_read(device, NV_FSP_RPC_EMEM_BASE, data, msg_size_words);
224 
225 	/* Reset message queue tail to acknowledge receipt */
226 	gpu_write32(gpu, NV_FSP_MSG_QUEUE_TAIL, head);
227 
228 	return msg_size_words;
229 }
230 
fsp_reset_rpc_state(struct vfio_pci_device * device)231 static void fsp_reset_rpc_state(struct vfio_pci_device *device)
232 {
233 	struct gpu_device *gpu = to_gpu_device(device);
234 	u32 head, tail;
235 
236 	head = gpu_read32(gpu, NV_FSP_QUEUE_HEAD);
237 	tail = gpu_read32(gpu, NV_FSP_QUEUE_TAIL);
238 
239 	if (head == tail) {
240 		head = gpu_read32(gpu, NV_FSP_MSG_QUEUE_HEAD);
241 		tail = gpu_read32(gpu, NV_FSP_MSG_QUEUE_TAIL);
242 		if (head == tail)
243 			return;
244 	}
245 
246 	/* Best-effort drain; timeout is expected if no pending message. */
247 	fsp_poll_queue(device, "fsp_msg_queue_drain",
248 		       NV_FSP_MSG_QUEUE_HEAD, NV_FSP_MSG_QUEUE_TAIL,
249 		       false, 5000);
250 
251 	gpu_write32(gpu, NV_FSP_QUEUE_TAIL, NV_FSP_RPC_EMEM_BASE);
252 	gpu_write32(gpu, NV_FSP_QUEUE_HEAD, NV_FSP_RPC_EMEM_BASE);
253 	gpu_write32(gpu, NV_FSP_MSG_QUEUE_TAIL, NV_FSP_RPC_EMEM_BASE);
254 	gpu_write32(gpu, NV_FSP_MSG_QUEUE_HEAD, NV_FSP_RPC_EMEM_BASE);
255 }
256 
mctp_header_build(u8 seid,u8 seq,bool som,bool eom)257 static inline u32 mctp_header_build(u8 seid, u8 seq, bool som, bool eom)
258 {
259 	u32 hdr = 0;
260 
261 	hdr |= (seid & NV_MCTP_HDR_SEID_MASK) << NV_MCTP_HDR_SEID_SHIFT;
262 	hdr |= (seq & NV_MCTP_HDR_SEQ_MASK) << NV_MCTP_HDR_SEQ_SHIFT;
263 	if (som)
264 		hdr |= NV_MCTP_HDR_SOM_BIT;
265 	if (eom)
266 		hdr |= NV_MCTP_HDR_EOM_BIT;
267 
268 	return hdr;
269 }
270 
mctp_msg_header_build(u8 nvdm_type)271 static inline u32 mctp_msg_header_build(u8 nvdm_type)
272 {
273 	u32 hdr = 0;
274 
275 	hdr |= (NV_MCTP_MSG_TYPE_VENDOR_DEFINED & NV_MCTP_MSG_TYPE_MASK)
276 		<< NV_MCTP_MSG_TYPE_SHIFT;
277 	hdr |= (NV_MCTP_MSG_VENDOR_ID_NVIDIA & NV_MCTP_MSG_VENDOR_ID_MASK)
278 		<< NV_MCTP_MSG_VENDOR_ID_SHIFT;
279 	hdr |= (nvdm_type & NV_MCTP_MSG_NVDM_TYPE_MASK)
280 		<< NV_MCTP_MSG_NVDM_TYPE_SHIFT;
281 
282 	return hdr;
283 }
284 
mctp_msg_header_get_nvdm_type(u32 hdr)285 static inline u8 mctp_msg_header_get_nvdm_type(u32 hdr)
286 {
287 	return (hdr >> NV_MCTP_MSG_NVDM_TYPE_SHIFT) &
288 	       NV_MCTP_MSG_NVDM_TYPE_MASK;
289 }
290 
fsp_rpc_send_cmd(struct vfio_pci_device * device,u8 nvdm_type,const u32 * data,u32 data_count,u32 timeout_ms)291 static int fsp_rpc_send_cmd(struct vfio_pci_device *device, u8 nvdm_type,
292 			    const u32 *data, u32 data_count, u32 timeout_ms)
293 {
294 	u32 max_packet_words = NV_FSP_RPC_MAX_PACKET_SIZE / 4;
295 	u32 packet[256];
296 	u32 resp_buf[256];
297 	u32 total_words;
298 	int resp_words;
299 	u8 resp_nvdm_type;
300 	int ret;
301 
302 	total_words = 2 + data_count;
303 	if (total_words > max_packet_words)
304 		return -EINVAL;
305 
306 	packet[0] = mctp_header_build(0, 0, true, true);
307 	packet[1] = mctp_msg_header_build(nvdm_type);
308 
309 	if (data_count > 0)
310 		memcpy(&packet[2], data, data_count * sizeof(u32));
311 
312 	ret = fsp_rpc_send_data(device, packet, total_words);
313 	if (ret)
314 		return ret;
315 
316 	resp_words = fsp_rpc_receive_data(device, resp_buf, 256, timeout_ms);
317 	if (resp_words < 0)
318 		return resp_words;
319 
320 	if (resp_words < NV_FSP_RPC_MIN_RESPONSE_WORDS)
321 		return -EPROTO;
322 
323 	resp_nvdm_type = mctp_msg_header_get_nvdm_type(resp_buf[1]);
324 	if (resp_nvdm_type != NV_NVDM_TYPE_RESPONSE)
325 		return -EPROTO;
326 
327 	if (resp_buf[3] != nvdm_type)
328 		return -EPROTO;
329 
330 	if (resp_buf[4] != 0)
331 		return -resp_buf[4];
332 
333 	return 0;
334 }
335 
fsp_init(struct vfio_pci_device * device)336 static int fsp_init(struct vfio_pci_device *device)
337 {
338 	int ret;
339 
340 	ret = gpu_poll_register(device, "fsp_boot_complete",
341 				NV_FSP_BOOT_COMPLETE_OFFSET,
342 				NV_FSP_BOOT_COMPLETE_SUCCESS, 0xffffffff, 5000);
343 	if (ret)
344 		return ret;
345 
346 	fsp_reset_rpc_state(device);
347 	return ret;
348 }
349 
fsp_fbdma_enable(struct vfio_pci_device * device)350 static int fsp_fbdma_enable(struct vfio_pci_device *device)
351 {
352 	struct gpu_device *gpu = to_gpu_device(device);
353 	u32 cmd_data = NV_FBDMA_SUBCMD_ENABLE;
354 	int ret = 0;
355 
356 	if (gpu->fsp_dma_enabled)
357 		return ret;
358 
359 	ret = fsp_rpc_send_cmd(device, NV_NVDM_TYPE_FBDMA, &cmd_data, 1, 5000);
360 	if (ret)
361 		return ret;
362 
363 	gpu->fsp_dma_enabled = true;
364 	return ret;
365 }
366 
fsp_check_ofa_dma_support(struct vfio_pci_device * device)367 static bool fsp_check_ofa_dma_support(struct vfio_pci_device *device)
368 {
369 	struct gpu_device *gpu = to_gpu_device(device);
370 	u32 val = gpu_read32(gpu, NV_OFA_DMA_SUPPORT_CHECK_REG);
371 
372 	return (val >> 16) != 0xbadf;
373 }
374 
size_to_dma_encoding(u64 size)375 static u32 size_to_dma_encoding(u64 size)
376 {
377 	VFIO_ASSERT_LE(size, NV_FALCON_DMA_MAX_TRANSFER_SIZE);
378 	VFIO_ASSERT_GE(size, NV_FALCON_DMA_MIN_TRANSFER_SIZE);
379 	VFIO_ASSERT_EQ(size & (size - 1), 0, "size must be power-of-2\n");
380 
381 	return ffs(size) - 3;
382 }
383 
falcon_dmem_port_configure(struct vfio_pci_device * device,u32 offset,bool auto_inc_read,bool auto_inc_write)384 static void falcon_dmem_port_configure(struct vfio_pci_device *device,
385 				       u32 offset, bool auto_inc_read,
386 				       bool auto_inc_write)
387 {
388 	struct gpu_device *gpu = to_gpu_device(device);
389 	const struct falcon *falcon = gpu->falcon;
390 	u32 memc_value = offset;
391 
392 	/* Set auto-increment flags */
393 	if (auto_inc_read)
394 		memc_value |= NV_PPWR_FALCON_DMEMC_AINCR_TRUE;
395 	if (auto_inc_write)
396 		memc_value |= NV_PPWR_FALCON_DMEMC_AINCW_TRUE;
397 
398 	gpu_write32(gpu, falcon->dmem_control_reg, memc_value);
399 }
400 
falcon_select_core_falcon(struct vfio_pci_device * device)401 static void falcon_select_core_falcon(struct vfio_pci_device *device)
402 {
403 	struct gpu_device *gpu = to_gpu_device(device);
404 	const struct falcon *falcon = gpu->falcon;
405 	u32 core_select_reg = falcon->base_page + NV_FALCON_CORE_SELECT_OFFSET;
406 	u32 core_select;
407 
408 	core_select = gpu_read32(gpu, core_select_reg);
409 
410 	/* Clear bits 4:5 to select falcon core (not RISCV) */
411 	core_select &= ~NV_FALCON_CORE_SELECT_MASK;
412 
413 	gpu_write32(gpu, core_select_reg, core_select);
414 }
415 
falcon_enable(struct vfio_pci_device * device)416 static int falcon_enable(struct vfio_pci_device *device)
417 {
418 	struct gpu_device *gpu = to_gpu_device(device);
419 	const struct falcon *falcon = gpu->falcon;
420 	u32 mailbox_test_reg;
421 	u32 mailbox_val;
422 
423 	if (falcon->no_outside_reset)
424 		return 0;
425 
426 	/* Ada-specific: Check if falcon needs reset before enable */
427 	if (gpu->arch == GPU_ARCH_ADA) {
428 		mailbox_test_reg = falcon->base_page +
429 				   NV_FALCON_MAILBOX_TEST_OFFSET;
430 		mailbox_val = gpu_read32(gpu, mailbox_test_reg);
431 		if (mailbox_val == NV_FALCON_MAILBOX_RESET_MAGIC)
432 			gpu_write32(gpu, falcon->engine_reset, 1);
433 	}
434 
435 	/* Enable the falcon based on control method */
436 	if (gpu->pmc_enable_mask != 0) {
437 		u32 pmc_enable;
438 
439 		/* Enable via PMC_ENABLE register */
440 		pmc_enable = gpu_read32(gpu, NV_PMC_ENABLE);
441 		gpu_write32(gpu, NV_PMC_ENABLE,
442 			    pmc_enable | gpu->pmc_enable_mask);
443 	} else {
444 		/* Enable by deasserting engine reset */
445 		gpu_write32(gpu, falcon->engine_reset, 0);
446 	}
447 
448 	if (gpu->arch < GPU_ARCH_HOPPER) {
449 		falcon_select_core_falcon(device);
450 
451 		/* Wait for DMACTL to be ready (bits 1:2 should be 0) */
452 		return gpu_poll_register(device, "falcon_dmactl",
453 					 falcon->dmactl, 0,
454 					 NV_FALCON_DMACTL_READY_MASK, 1000);
455 	}
456 
457 	return 0;
458 }
459 
falcon_disable(struct vfio_pci_device * device)460 static void falcon_disable(struct vfio_pci_device *device)
461 {
462 	struct gpu_device *gpu = to_gpu_device(device);
463 	const struct falcon *falcon = gpu->falcon;
464 	u32 pmc_enable;
465 
466 	if (falcon->no_outside_reset)
467 		return;
468 
469 	if (gpu->pmc_enable_mask != 0) {
470 		/* Disable via PMC_ENABLE */
471 		pmc_enable = gpu_read32(gpu, NV_PMC_ENABLE);
472 		gpu_write32(gpu, NV_PMC_ENABLE,
473 			    pmc_enable & ~gpu->pmc_enable_mask);
474 	} else {
475 		/* Disable by asserting engine reset */
476 		gpu_write32(gpu, falcon->engine_reset, 1);
477 	}
478 }
479 
falcon_reset(struct vfio_pci_device * device)480 static int falcon_reset(struct vfio_pci_device *device)
481 {
482 	falcon_disable(device);
483 
484 	return falcon_enable(device);
485 }
486 
nv_falcon_dma_init(struct vfio_pci_device * device)487 static int nv_falcon_dma_init(struct vfio_pci_device *device)
488 {
489 	struct gpu_device *gpu = to_gpu_device(device);
490 	const struct falcon *falcon;
491 	u32 transcfg;
492 	u32 dmactl;
493 	u32 ctl;
494 	int ret = 0;
495 
496 	falcon = gpu->falcon;
497 
498 	vfio_pci_cmd_set(device, PCI_COMMAND_MASTER);
499 
500 	if (gpu->arch >= GPU_ARCH_HOPPER) {
501 		ret = fsp_init(device);
502 		if (ret) {
503 			dev_err(device, "Failed to init FSP: %d\n", ret);
504 			return ret;
505 		}
506 
507 		ret = fsp_fbdma_enable(device);
508 		if (ret) {
509 			dev_err(device,
510 				"Failed to enable FSP FBDMA: %d\n", ret);
511 			return ret;
512 		}
513 
514 		if (!fsp_check_ofa_dma_support(device)) {
515 			dev_err(device,
516 				"OFA DMA not supported with current firmware\n");
517 			return -EOPNOTSUPP;
518 		}
519 	}
520 
521 	if (gpu->is_memory_clear_supported) {
522 		/* For Turing+, wait for boot to complete first */
523 		if (gpu->arch >= GPU_ARCH_TURING) {
524 			/* Wait for boot complete - Hopper+ uses FSP register */
525 			if (gpu->arch >= GPU_ARCH_HOPPER) {
526 				ret = gpu_poll_register(device,
527 					"fsp_boot_complete",
528 					NV_FSP_BOOT_COMPLETE_OFFSET,
529 					NV_FSP_BOOT_COMPLETE_SUCCESS,
530 					0xffffffff, 5000);
531 			} else {
532 				ret = gpu_poll_register(device,
533 					"boot_complete",
534 					NV_BOOT_COMPLETE_OFFSET,
535 					NV_BOOT_COMPLETE_SUCCESS,
536 					0xffffffff, 5000);
537 			}
538 			if (ret)
539 				return ret;
540 
541 			ret = gpu_poll_register(device,
542 				"memory_clear_finished",
543 				NV_MEM_CLEAR_OFFSET, 0x1, 0xffffffff, 5000);
544 			if (ret)
545 				return ret;
546 		}
547 	}
548 
549 	ret = falcon_reset(device);
550 	if (ret)
551 		return ret;
552 
553 	falcon_dmem_port_configure(device, 0, false, false);
554 
555 	transcfg = gpu_read32(gpu, falcon->fbif_transcfg);
556 	transcfg &= ~NV_FBIF_TRANSCFG_TARGET_MASK;
557 	transcfg |= NV_FBIF_TRANSCFG_SYSMEM_DEFAULT;
558 	gpu_write32(gpu, falcon->fbif_transcfg, transcfg);
559 
560 	gpu_write32(gpu, falcon->fbif_ctl2, 0x1);
561 
562 	ctl = gpu_read32(gpu, falcon->fbif_ctl);
563 	ctl |= NV_FBIF_CTL_ALLOW_PHYS_MODE | NV_FBIF_CTL_ALLOW_FULL_PHYS_MODE;
564 	gpu_write32(gpu, falcon->fbif_ctl, ctl);
565 
566 	dmactl = gpu_read32(gpu, falcon->dmactl);
567 	dmactl &= ~NV_FALCON_DMACTL_DMEM_SCRUBBING;
568 	gpu_write32(gpu, falcon->dmactl, dmactl);
569 
570 	return ret;
571 }
572 
nv_falcon_dma(struct vfio_pci_device * device,u64 address,u64 size,bool write)573 static int nv_falcon_dma(struct vfio_pci_device *device,
574 			 u64 address, u64 size,
575 			 bool write)
576 {
577 	struct gpu_device *gpu = to_gpu_device(device);
578 	const struct falcon *falcon = gpu->falcon;
579 	u32 dma_cmd;
580 	int ret;
581 
582 	gpu_write32(gpu, NV_GPU_DMA_ADDR_TOP_BITS_REG,
583 		    (address >> 47) & 0x1ffff);
584 	gpu_write32(gpu, falcon->base_page + NV_FALCON_DMA_ADDR_HIGH_OFFSET,
585 		    (address >> 40) & 0x7f);
586 	gpu_write32(gpu, falcon->base_page + NV_FALCON_DMA_ADDR_LOW_OFFSET,
587 		    (address >> 8) & 0xffffffff);
588 	gpu_write32(gpu, falcon->base_page + NV_FALCON_DMA_BLOCK_OFFSET,
589 		    address & 0xff);
590 	gpu_write32(gpu, falcon->base_page + NV_FALCON_DMA_MEM_OFFSET, 0);
591 
592 	dma_cmd = size_to_dma_encoding(size) << NV_FALCON_DMA_CMD_SIZE_SHIFT;
593 
594 	/* Set direction: write (DMEM->mem) or read (mem->DMEM) */
595 	if (write)
596 		dma_cmd |= NV_FALCON_DMA_CMD_WRITE_BIT;
597 
598 	gpu_write32(gpu, falcon->base_page + NV_FALCON_DMA_CMD_OFFSET, dma_cmd);
599 
600 	ret = gpu_poll_register(device, "dma_done",
601 				falcon->base_page + NV_FALCON_DMA_CMD_OFFSET,
602 				NV_FALCON_DMA_CMD_DONE_BIT,
603 				NV_FALCON_DMA_CMD_DONE_BIT, 1000);
604 	if (ret)
605 		dev_err(device, "Failed DMA %s (addr=0x%lx, size=%lu)\n",
606 			write ? "write" : "read", address, size);
607 
608 	return ret;
609 }
610 
nv_falcon_memcpy_chunk(struct vfio_pci_device * device,iova_t src,iova_t dst,u64 size)611 static int nv_falcon_memcpy_chunk(struct vfio_pci_device *device,
612 				  iova_t src, iova_t dst, u64 size)
613 {
614 	int ret;
615 
616 	ret = nv_falcon_dma(device, src, size, false);
617 	if (ret)
618 		return ret;
619 
620 	return nv_falcon_dma(device, dst, size, true);
621 }
622 
nv_falcon_probe(struct vfio_pci_device * device)623 static int nv_falcon_probe(struct vfio_pci_device *device)
624 {
625 	enum gpu_arch gpu_arch;
626 	u32 pmc_boot_0;
627 	void *bar0;
628 	int i;
629 
630 	if (vfio_pci_config_readw(device, PCI_VENDOR_ID) !=
631 	    PCI_VENDOR_ID_NVIDIA)
632 		return -ENODEV;
633 
634 	if (vfio_pci_config_readw(device, PCI_CLASS_DEVICE) >> 8 !=
635 	    PCI_BASE_CLASS_DISPLAY)
636 		return -ENODEV;
637 
638 	/* Get BAR0 pointer for reading GPU registers */
639 	bar0 = device->bars[0].vaddr;
640 	if (!bar0)
641 		return -ENODEV;
642 
643 	/* Read PMC_BOOT_0 register from BAR0 to identify GPU */
644 	pmc_boot_0 = readl(bar0 + NV_PMC_BOOT_0);
645 
646 	/* Look up GPU architecture to verify this is a supported GPU */
647 	gpu_arch = nv_gpu_arch_lookup(pmc_boot_0);
648 	if (gpu_arch == GPU_ARCH_UNKNOWN) {
649 		dev_err(device,
650 			"Unsupported GPU architecture for PMC_BOOT_0: 0x%x\n",
651 			pmc_boot_0);
652 		return -ENODEV;
653 	}
654 
655 	/* Check verified GPU map */
656 	for (i = 0; i < VERIFIED_GPU_MAP_SIZE; i++) {
657 		if (verified_gpu_map[i] == pmc_boot_0)
658 			return 0;
659 	}
660 
661 	dev_info(device,
662 		 "Unvalidated GPU: PMC_BOOT_0: 0x%x, possibly not supported\n",
663 		 pmc_boot_0);
664 
665 	return 0;
666 }
667 
nv_falcon_init(struct vfio_pci_device * device)668 static void nv_falcon_init(struct vfio_pci_device *device)
669 {
670 	struct gpu_device *gpu = to_gpu_device(device);
671 	const struct gpu_properties *props;
672 	u32 pmc_boot_0;
673 	int ret;
674 
675 	VFIO_ASSERT_GE(device->driver.region.size, sizeof(*gpu));
676 
677 	/* Read PMC_BOOT_0 register from BAR0 to identify GPU */
678 	pmc_boot_0 = readl(device->bars[0].vaddr + NV_PMC_BOOT_0);
679 
680 	/* Look up GPU architecture */
681 	gpu->arch = nv_gpu_arch_lookup(pmc_boot_0);
682 
683 	props = &gpu_properties_map[gpu->arch];
684 
685 	/* Populate GPU structure */
686 	gpu->bar0 = device->bars[0].vaddr;
687 	gpu->is_memory_clear_supported = props->memory_clear_supported;
688 	gpu->falcon = &falcon_map[props->falcon_type];
689 	gpu->pmc_enable_mask = props->pmc_enable_mask;
690 
691 	/* Initialize falcon for DMA */
692 	ret = nv_falcon_dma_init(device);
693 	VFIO_ASSERT_EQ(ret, 0, "Failed to initialize falcon DMA: %d\n", ret);
694 
695 	device->driver.max_memcpy_size = NV_FALCON_DMA_MAX_TRANSFER_SIZE;
696 	device->driver.max_memcpy_count = NV_FALCON_DMA_MAX_TRANSFER_COUNT;
697 }
698 
nv_falcon_remove(struct vfio_pci_device * device)699 static void nv_falcon_remove(struct vfio_pci_device *device)
700 {
701 	falcon_disable(device);
702 	vfio_pci_cmd_clear(device, PCI_COMMAND_MASTER);
703 }
704 
705 /*
706  * Falcon DMA can only process one transfer at a time,
707  * so the actual work is deferred to memcpy_wait() to conform to the
708  * memcpy_start()/memcpy_wait() contract.
709  */
nv_falcon_memcpy_start(struct vfio_pci_device * device,iova_t src,iova_t dst,u64 size,u64 count)710 static void nv_falcon_memcpy_start(struct vfio_pci_device *device,
711 				   iova_t src, iova_t dst, u64 size, u64 count)
712 {
713 	struct gpu_device *gpu = to_gpu_device(device);
714 
715 	VFIO_ASSERT_EQ(count, 1);
716 	VFIO_ASSERT_EQ(size & (NV_FALCON_DMA_MIN_TRANSFER_SIZE - 1), 0,
717 		       "size 0x%lx must be %u-byte aligned\n",
718 		       (unsigned long)size, NV_FALCON_DMA_MIN_TRANSFER_SIZE);
719 
720 	gpu->memcpy_src = src;
721 	gpu->memcpy_dst = dst;
722 	gpu->memcpy_size = size;
723 }
724 
725 /*
726  * Return the largest power-of-2 bytes we can transfer from @addr
727  * without crossing a DMA block boundary.
728  */
dma_block_remain(u64 addr)729 static u64 dma_block_remain(u64 addr)
730 {
731 	u64 offset = addr & (NV_FALCON_DMA_BLOCK_SIZE - 1);
732 
733 	if (!offset)
734 		return NV_FALCON_DMA_BLOCK_SIZE;
735 
736 	/* Lowest set bit of the offset is the largest aligned chunk */
737 	return 1ULL << (ffs(offset) - 1);
738 }
739 
rounddown_pow_of_two(u64 x)740 static u64 rounddown_pow_of_two(u64 x)
741 {
742 	return 1ULL << (63 - __builtin_clzll(x));
743 }
744 
nv_falcon_memcpy_wait(struct vfio_pci_device * device)745 static int nv_falcon_memcpy_wait(struct vfio_pci_device *device)
746 {
747 	struct gpu_device *gpu = to_gpu_device(device);
748 	iova_t src = gpu->memcpy_src;
749 	iova_t dst = gpu->memcpy_dst;
750 	u64 remaining = gpu->memcpy_size;
751 	int ret = 0;
752 
753 	/*
754 	 * Falcon DMA supports power-of-2 transfer sizes in [4, 256] and
755 	 * cannot cross 256-byte block boundaries.  Decompose the request
756 	 * into the largest valid chunk at each step.
757 	 */
758 	while (remaining) {
759 		u64 chunk = rounddown_pow_of_two(remaining);
760 
761 		chunk = min(chunk, dma_block_remain(src));
762 		chunk = min(chunk, dma_block_remain(dst));
763 
764 		ret = nv_falcon_memcpy_chunk(device, src, dst, chunk);
765 		if (ret)
766 			break;
767 
768 		src += chunk;
769 		dst += chunk;
770 		remaining -= chunk;
771 	}
772 
773 	return ret;
774 }
775 
776 const struct vfio_pci_driver_ops nv_falcon_ops = {
777 	.name = "nv_falcon",
778 	.probe = nv_falcon_probe,
779 	.init = nv_falcon_init,
780 	.remove = nv_falcon_remove,
781 	.memcpy_start = nv_falcon_memcpy_start,
782 	.memcpy_wait = nv_falcon_memcpy_wait,
783 };
784