Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
89e1f7d4 AW |
2 | /* |
3 | * Copyright (C) 2012 Red Hat, Inc. All rights reserved. | |
4 | * Author: Alex Williamson <alex.williamson@redhat.com> | |
5 | * | |
89e1f7d4 AW |
6 | * Derived from original vfio: |
7 | * Copyright 2010 Cisco Systems, Inc. All rights reserved. | |
8 | * Author: Tom Lyon, pugs@cisco.com | |
9 | */ | |
10 | ||
80c7e8cc AW |
11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
12 | ||
89e1f7d4 AW |
13 | #include <linux/device.h> |
14 | #include <linux/eventfd.h> | |
8b27ee60 | 15 | #include <linux/file.h> |
89e1f7d4 AW |
16 | #include <linux/interrupt.h> |
17 | #include <linux/iommu.h> | |
18 | #include <linux/module.h> | |
19 | #include <linux/mutex.h> | |
20 | #include <linux/notifier.h> | |
21 | #include <linux/pci.h> | |
22 | #include <linux/pm_runtime.h> | |
23 | #include <linux/slab.h> | |
24 | #include <linux/types.h> | |
25 | #include <linux/uaccess.h> | |
26 | #include <linux/vfio.h> | |
ecaa1f6a | 27 | #include <linux/vgaarb.h> |
0e714d27 | 28 | #include <linux/nospec.h> |
abafbc55 | 29 | #include <linux/sched/mm.h> |
89e1f7d4 AW |
30 | |
31 | #include "vfio_pci_private.h" | |
32 | ||
33 | #define DRIVER_VERSION "0.2" | |
34 | #define DRIVER_AUTHOR "Alex Williamson <alex.williamson@redhat.com>" | |
35 | #define DRIVER_DESC "VFIO PCI - User Level meta-driver" | |
36 | ||
80c7e8cc AW |
37 | static char ids[1024] __initdata; |
38 | module_param_string(ids, ids, sizeof(ids), 0); | |
39 | MODULE_PARM_DESC(ids, "Initial PCI IDs to add to the vfio driver, format is \"vendor:device[:subvendor[:subdevice[:class[:class_mask]]]]\" and multiple comma separated entries can be specified"); | |
40 | ||
89e1f7d4 AW |
41 | static bool nointxmask; |
42 | module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR); | |
43 | MODULE_PARM_DESC(nointxmask, | |
44 | "Disable support for PCI 2.3 style INTx masking. If this resolves problems for specific devices, report lspci -vvvxxx to linux-pci@vger.kernel.org so the device can be fixed automatically via the broken_intx_masking flag."); | |
45 | ||
88c0dead AW |
46 | #ifdef CONFIG_VFIO_PCI_VGA |
47 | static bool disable_vga; | |
48 | module_param(disable_vga, bool, S_IRUGO); | |
49 | MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci"); | |
50 | #endif | |
51 | ||
6eb70187 AW |
52 | static bool disable_idle_d3; |
53 | module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR); | |
54 | MODULE_PARM_DESC(disable_idle_d3, | |
55 | "Disable using the PCI D3 low power state for idle, unused devices"); | |
56 | ||
137e5531 AW |
57 | static bool enable_sriov; |
58 | #ifdef CONFIG_PCI_IOV | |
59 | module_param(enable_sriov, bool, 0644); | |
60 | MODULE_PARM_DESC(enable_sriov, "Enable support for SR-IOV configuration. Enabling SR-IOV on a PF typically requires support of the userspace PF driver, enabling VFs without such support may result in non-functional VFs or PF."); | |
61 | #endif | |
62 | ||
1f97970e GC |
63 | static bool disable_denylist; |
64 | module_param(disable_denylist, bool, 0444); | |
65 | MODULE_PARM_DESC(disable_denylist, "Disable use of device denylist. Disabling the denylist allows binding to devices with known errata that may lead to exploitable stability or security issues when accessed by untrusted users."); | |
66 | ||
88c0dead AW |
67 | static inline bool vfio_vga_disabled(void) |
68 | { | |
69 | #ifdef CONFIG_VFIO_PCI_VGA | |
70 | return disable_vga; | |
71 | #else | |
72 | return true; | |
73 | #endif | |
74 | } | |
75 | ||
1f97970e GC |
76 | static bool vfio_pci_dev_in_denylist(struct pci_dev *pdev) |
77 | { | |
50173329 GC |
78 | switch (pdev->vendor) { |
79 | case PCI_VENDOR_ID_INTEL: | |
80 | switch (pdev->device) { | |
81 | case PCI_DEVICE_ID_INTEL_QAT_C3XXX: | |
82 | case PCI_DEVICE_ID_INTEL_QAT_C3XXX_VF: | |
83 | case PCI_DEVICE_ID_INTEL_QAT_C62X: | |
84 | case PCI_DEVICE_ID_INTEL_QAT_C62X_VF: | |
85 | case PCI_DEVICE_ID_INTEL_QAT_DH895XCC: | |
86 | case PCI_DEVICE_ID_INTEL_QAT_DH895XCC_VF: | |
87 | return true; | |
88 | default: | |
89 | return false; | |
90 | } | |
91 | } | |
92 | ||
1f97970e GC |
93 | return false; |
94 | } | |
95 | ||
96 | static bool vfio_pci_is_denylisted(struct pci_dev *pdev) | |
97 | { | |
98 | if (!vfio_pci_dev_in_denylist(pdev)) | |
99 | return false; | |
100 | ||
101 | if (disable_denylist) { | |
102 | pci_warn(pdev, | |
103 | "device denylist disabled - allowing device %04x:%04x.\n", | |
104 | pdev->vendor, pdev->device); | |
105 | return false; | |
106 | } | |
107 | ||
108 | pci_warn(pdev, "%04x:%04x exists in vfio-pci device denylist, driver probing disallowed.\n", | |
109 | pdev->vendor, pdev->device); | |
110 | ||
111 | return true; | |
112 | } | |
113 | ||
ecaa1f6a AW |
114 | /* |
115 | * Our VGA arbiter participation is limited since we don't know anything | |
116 | * about the device itself. However, if the device is the only VGA device | |
117 | * downstream of a bridge and VFIO VGA support is disabled, then we can | |
118 | * safely return legacy VGA IO and memory as not decoded since the user | |
119 | * has no way to get to it and routing can be disabled externally at the | |
120 | * bridge. | |
121 | */ | |
122 | static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga) | |
123 | { | |
124 | struct vfio_pci_device *vdev = opaque; | |
125 | struct pci_dev *tmp = NULL, *pdev = vdev->pdev; | |
126 | unsigned char max_busnr; | |
127 | unsigned int decodes; | |
128 | ||
129 | if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus)) | |
130 | return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | | |
131 | VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; | |
132 | ||
133 | max_busnr = pci_bus_max_busnr(pdev->bus); | |
134 | decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; | |
135 | ||
136 | while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) { | |
137 | if (tmp == pdev || | |
138 | pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) || | |
139 | pci_is_root_bus(tmp->bus)) | |
140 | continue; | |
141 | ||
142 | if (tmp->bus->number >= pdev->bus->number && | |
143 | tmp->bus->number <= max_busnr) { | |
144 | pci_dev_put(tmp); | |
145 | decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM; | |
146 | break; | |
147 | } | |
148 | } | |
149 | ||
150 | return decodes; | |
151 | } | |
152 | ||
153 | static inline bool vfio_pci_is_vga(struct pci_dev *pdev) | |
154 | { | |
155 | return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA; | |
156 | } | |
157 | ||
05f0c03f YX |
158 | static void vfio_pci_probe_mmaps(struct vfio_pci_device *vdev) |
159 | { | |
160 | struct resource *res; | |
c9c13ba4 | 161 | int i; |
05f0c03f YX |
162 | struct vfio_pci_dummy_resource *dummy_res; |
163 | ||
c9c13ba4 DE |
164 | for (i = 0; i < PCI_STD_NUM_BARS; i++) { |
165 | int bar = i + PCI_STD_RESOURCES; | |
166 | ||
167 | res = &vdev->pdev->resource[bar]; | |
05f0c03f YX |
168 | |
169 | if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP)) | |
170 | goto no_mmap; | |
171 | ||
172 | if (!(res->flags & IORESOURCE_MEM)) | |
173 | goto no_mmap; | |
174 | ||
175 | /* | |
176 | * The PCI core shouldn't set up a resource with a | |
177 | * type but zero size. But there may be bugs that | |
178 | * cause us to do that. | |
179 | */ | |
180 | if (!resource_size(res)) | |
181 | goto no_mmap; | |
182 | ||
183 | if (resource_size(res) >= PAGE_SIZE) { | |
184 | vdev->bar_mmap_supported[bar] = true; | |
185 | continue; | |
186 | } | |
187 | ||
188 | if (!(res->start & ~PAGE_MASK)) { | |
189 | /* | |
190 | * Add a dummy resource to reserve the remainder | |
191 | * of the exclusive page in case that hot-add | |
192 | * device's bar is assigned into it. | |
193 | */ | |
194 | dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL); | |
195 | if (dummy_res == NULL) | |
196 | goto no_mmap; | |
197 | ||
198 | dummy_res->resource.name = "vfio sub-page reserved"; | |
199 | dummy_res->resource.start = res->end + 1; | |
200 | dummy_res->resource.end = res->start + PAGE_SIZE - 1; | |
201 | dummy_res->resource.flags = res->flags; | |
202 | if (request_resource(res->parent, | |
203 | &dummy_res->resource)) { | |
204 | kfree(dummy_res); | |
205 | goto no_mmap; | |
206 | } | |
207 | dummy_res->index = bar; | |
208 | list_add(&dummy_res->res_next, | |
209 | &vdev->dummy_resources_list); | |
210 | vdev->bar_mmap_supported[bar] = true; | |
211 | continue; | |
212 | } | |
213 | /* | |
214 | * Here we don't handle the case when the BAR is not page | |
215 | * aligned because we can't expect the BAR will be | |
216 | * assigned into the same location in a page in guest | |
217 | * when we passthrough the BAR. And it's hard to access | |
218 | * this BAR in userspace because we have no way to get | |
219 | * the BAR's location in a page. | |
220 | */ | |
221 | no_mmap: | |
222 | vdev->bar_mmap_supported[bar] = false; | |
223 | } | |
224 | } | |
225 | ||
bc4fba77 | 226 | static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev); |
f572a960 | 227 | static void vfio_pci_disable(struct vfio_pci_device *vdev); |
abafbc55 | 228 | static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data); |
bc4fba77 | 229 | |
45074405 AW |
230 | /* |
231 | * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND | |
232 | * _and_ the ability detect when the device is asserting INTx via PCI_STATUS. | |
233 | * If a device implements the former but not the latter we would typically | |
234 | * expect broken_intx_masking be set and require an exclusive interrupt. | |
235 | * However since we do have control of the device's ability to assert INTx, | |
236 | * we can instead pretend that the device does not implement INTx, virtualizing | |
237 | * the pin register to report zero and maintaining DisINTx set on the host. | |
238 | */ | |
239 | static bool vfio_pci_nointx(struct pci_dev *pdev) | |
240 | { | |
241 | switch (pdev->vendor) { | |
242 | case PCI_VENDOR_ID_INTEL: | |
243 | switch (pdev->device) { | |
7d57e5e9 | 244 | /* All i40e (XL710/X710/XXV710) 10/20/25/40GbE NICs */ |
45074405 AW |
245 | case 0x1572: |
246 | case 0x1574: | |
247 | case 0x1580 ... 0x1581: | |
7d57e5e9 | 248 | case 0x1583 ... 0x158b: |
45074405 | 249 | case 0x37d0 ... 0x37d2: |
bf3551e1 AW |
250 | /* X550 */ |
251 | case 0x1563: | |
45074405 AW |
252 | return true; |
253 | default: | |
254 | return false; | |
255 | } | |
256 | } | |
257 | ||
258 | return false; | |
259 | } | |
260 | ||
51ef3a00 AW |
261 | static void vfio_pci_probe_power_state(struct vfio_pci_device *vdev) |
262 | { | |
263 | struct pci_dev *pdev = vdev->pdev; | |
264 | u16 pmcsr; | |
265 | ||
266 | if (!pdev->pm_cap) | |
267 | return; | |
268 | ||
269 | pci_read_config_word(pdev, pdev->pm_cap + PCI_PM_CTRL, &pmcsr); | |
270 | ||
271 | vdev->needs_pm_restore = !(pmcsr & PCI_PM_CTRL_NO_SOFT_RESET); | |
272 | } | |
273 | ||
274 | /* | |
275 | * pci_set_power_state() wrapper handling devices which perform a soft reset on | |
276 | * D3->D0 transition. Save state prior to D0/1/2->D3, stash it on the vdev, | |
277 | * restore when returned to D0. Saved separately from pci_saved_state for use | |
278 | * by PM capability emulation and separately from pci_dev internal saved state | |
279 | * to avoid it being overwritten and consumed around other resets. | |
280 | */ | |
281 | int vfio_pci_set_power_state(struct vfio_pci_device *vdev, pci_power_t state) | |
282 | { | |
283 | struct pci_dev *pdev = vdev->pdev; | |
284 | bool needs_restore = false, needs_save = false; | |
285 | int ret; | |
286 | ||
287 | if (vdev->needs_pm_restore) { | |
288 | if (pdev->current_state < PCI_D3hot && state >= PCI_D3hot) { | |
289 | pci_save_state(pdev); | |
290 | needs_save = true; | |
291 | } | |
292 | ||
293 | if (pdev->current_state >= PCI_D3hot && state <= PCI_D0) | |
294 | needs_restore = true; | |
295 | } | |
296 | ||
297 | ret = pci_set_power_state(pdev, state); | |
298 | ||
299 | if (!ret) { | |
300 | /* D3 might be unsupported via quirk, skip unless in D3 */ | |
301 | if (needs_save && pdev->current_state >= PCI_D3hot) { | |
302 | vdev->pm_save = pci_store_saved_state(pdev); | |
303 | } else if (needs_restore) { | |
304 | pci_load_and_free_saved_state(pdev, &vdev->pm_save); | |
305 | pci_restore_state(pdev); | |
306 | } | |
307 | } | |
308 | ||
309 | return ret; | |
310 | } | |
311 | ||
89e1f7d4 AW |
312 | static int vfio_pci_enable(struct vfio_pci_device *vdev) |
313 | { | |
314 | struct pci_dev *pdev = vdev->pdev; | |
315 | int ret; | |
316 | u16 cmd; | |
317 | u8 msix_pos; | |
318 | ||
51ef3a00 | 319 | vfio_pci_set_power_state(vdev, PCI_D0); |
6eb70187 | 320 | |
9c22e660 AW |
321 | /* Don't allow our initial saved state to include busmaster */ |
322 | pci_clear_master(pdev); | |
323 | ||
9a92c509 AW |
324 | ret = pci_enable_device(pdev); |
325 | if (ret) | |
326 | return ret; | |
327 | ||
9f478035 AW |
328 | /* If reset fails because of the device lock, fail this path entirely */ |
329 | ret = pci_try_reset_function(pdev); | |
330 | if (ret == -EAGAIN) { | |
331 | pci_disable_device(pdev); | |
332 | return ret; | |
333 | } | |
334 | ||
335 | vdev->reset_works = !ret; | |
89e1f7d4 AW |
336 | pci_save_state(pdev); |
337 | vdev->pci_saved_state = pci_store_saved_state(pdev); | |
338 | if (!vdev->pci_saved_state) | |
a88a7b3e | 339 | pci_dbg(pdev, "%s: Couldn't store saved state\n", __func__); |
89e1f7d4 | 340 | |
45074405 AW |
341 | if (likely(!nointxmask)) { |
342 | if (vfio_pci_nointx(pdev)) { | |
a88a7b3e | 343 | pci_info(pdev, "Masking broken INTx support\n"); |
45074405 AW |
344 | vdev->nointx = true; |
345 | pci_intx(pdev, 0); | |
346 | } else | |
347 | vdev->pci_2_3 = pci_intx_mask_supported(pdev); | |
9a92c509 | 348 | } |
89e1f7d4 | 349 | |
89e1f7d4 AW |
350 | pci_read_config_word(pdev, PCI_COMMAND, &cmd); |
351 | if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) { | |
352 | cmd &= ~PCI_COMMAND_INTX_DISABLE; | |
353 | pci_write_config_word(pdev, PCI_COMMAND, cmd); | |
354 | } | |
355 | ||
45074405 AW |
356 | ret = vfio_config_init(vdev); |
357 | if (ret) { | |
358 | kfree(vdev->pci_saved_state); | |
359 | vdev->pci_saved_state = NULL; | |
360 | pci_disable_device(pdev); | |
361 | return ret; | |
362 | } | |
363 | ||
a9047f24 | 364 | msix_pos = pdev->msix_cap; |
89e1f7d4 AW |
365 | if (msix_pos) { |
366 | u16 flags; | |
367 | u32 table; | |
368 | ||
369 | pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags); | |
370 | pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table); | |
371 | ||
508d1aa6 BH |
372 | vdev->msix_bar = table & PCI_MSIX_TABLE_BIR; |
373 | vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET; | |
89e1f7d4 AW |
374 | vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16; |
375 | } else | |
376 | vdev->msix_bar = 0xFF; | |
377 | ||
ecaa1f6a | 378 | if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev)) |
84237a82 | 379 | vdev->has_vga = true; |
84237a82 | 380 | |
f572a960 AW |
381 | if (vfio_pci_is_vga(pdev) && |
382 | pdev->vendor == PCI_VENDOR_ID_INTEL && | |
383 | IS_ENABLED(CONFIG_VFIO_PCI_IGD)) { | |
384 | ret = vfio_pci_igd_init(vdev); | |
e4eccb85 | 385 | if (ret && ret != -ENODEV) { |
a88a7b3e | 386 | pci_warn(pdev, "Failed to setup Intel IGD regions\n"); |
7f928917 AK |
387 | goto disable_exit; |
388 | } | |
389 | } | |
390 | ||
05f0c03f YX |
391 | vfio_pci_probe_mmaps(vdev); |
392 | ||
9a92c509 | 393 | return 0; |
7f928917 AK |
394 | |
395 | disable_exit: | |
396 | vfio_pci_disable(vdev); | |
397 | return ret; | |
89e1f7d4 AW |
398 | } |
399 | ||
400 | static void vfio_pci_disable(struct vfio_pci_device *vdev) | |
401 | { | |
2007722a | 402 | struct pci_dev *pdev = vdev->pdev; |
05f0c03f | 403 | struct vfio_pci_dummy_resource *dummy_res, *tmp; |
30656177 | 404 | struct vfio_pci_ioeventfd *ioeventfd, *ioeventfd_tmp; |
28541d41 | 405 | int i, bar; |
89e1f7d4 | 406 | |
9c22e660 AW |
407 | /* Stop the device from further DMA */ |
408 | pci_clear_master(pdev); | |
89e1f7d4 AW |
409 | |
410 | vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE | | |
411 | VFIO_IRQ_SET_ACTION_TRIGGER, | |
412 | vdev->irq_type, 0, 0, NULL); | |
413 | ||
30656177 AW |
414 | /* Device closed, don't need mutex here */ |
415 | list_for_each_entry_safe(ioeventfd, ioeventfd_tmp, | |
416 | &vdev->ioeventfds_list, next) { | |
417 | vfio_virqfd_disable(&ioeventfd->virqfd); | |
418 | list_del(&ioeventfd->next); | |
419 | kfree(ioeventfd); | |
420 | } | |
421 | vdev->ioeventfds_nr = 0; | |
422 | ||
89e1f7d4 AW |
423 | vdev->virq_disabled = false; |
424 | ||
28541d41 AW |
425 | for (i = 0; i < vdev->num_regions; i++) |
426 | vdev->region[i].ops->release(vdev, &vdev->region[i]); | |
427 | ||
428 | vdev->num_regions = 0; | |
429 | kfree(vdev->region); | |
430 | vdev->region = NULL; /* don't krealloc a freed pointer */ | |
431 | ||
89e1f7d4 AW |
432 | vfio_config_free(vdev); |
433 | ||
c9c13ba4 DE |
434 | for (i = 0; i < PCI_STD_NUM_BARS; i++) { |
435 | bar = i + PCI_STD_RESOURCES; | |
89e1f7d4 AW |
436 | if (!vdev->barmap[bar]) |
437 | continue; | |
2007722a AW |
438 | pci_iounmap(pdev, vdev->barmap[bar]); |
439 | pci_release_selected_regions(pdev, 1 << bar); | |
89e1f7d4 AW |
440 | vdev->barmap[bar] = NULL; |
441 | } | |
2007722a | 442 | |
05f0c03f YX |
443 | list_for_each_entry_safe(dummy_res, tmp, |
444 | &vdev->dummy_resources_list, res_next) { | |
445 | list_del(&dummy_res->res_next); | |
446 | release_resource(&dummy_res->resource); | |
447 | kfree(dummy_res); | |
448 | } | |
449 | ||
bc4fba77 AW |
450 | vdev->needs_reset = true; |
451 | ||
2007722a AW |
452 | /* |
453 | * If we have saved state, restore it. If we can reset the device, | |
454 | * even better. Resetting with current state seems better than | |
455 | * nothing, but saving and restoring current state without reset | |
456 | * is just busy work. | |
457 | */ | |
458 | if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) { | |
a88a7b3e | 459 | pci_info(pdev, "%s: Couldn't reload saved state\n", __func__); |
2007722a AW |
460 | |
461 | if (!vdev->reset_works) | |
9c22e660 | 462 | goto out; |
2007722a AW |
463 | |
464 | pci_save_state(pdev); | |
465 | } | |
466 | ||
467 | /* | |
468 | * Disable INTx and MSI, presumably to avoid spurious interrupts | |
469 | * during reset. Stolen from pci_reset_function() | |
470 | */ | |
471 | pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE); | |
472 | ||
d24cdbfd | 473 | /* |
92c80268 | 474 | * Try to get the locks ourselves to prevent a deadlock. The |
475 | * success of this is dependent on being able to lock the device, | |
476 | * which is not always possible. | |
477 | * We can not use the "try" reset interface here, which will | |
478 | * overwrite the previously restored configuration information. | |
d24cdbfd | 479 | */ |
92c80268 | 480 | if (vdev->reset_works && pci_cfg_access_trylock(pdev)) { |
481 | if (device_trylock(&pdev->dev)) { | |
482 | if (!__pci_reset_function_locked(pdev)) | |
483 | vdev->needs_reset = false; | |
484 | device_unlock(&pdev->dev); | |
485 | } | |
486 | pci_cfg_access_unlock(pdev); | |
487 | } | |
2007722a AW |
488 | |
489 | pci_restore_state(pdev); | |
9c22e660 AW |
490 | out: |
491 | pci_disable_device(pdev); | |
bc4fba77 AW |
492 | |
493 | vfio_pci_try_bus_reset(vdev); | |
6eb70187 AW |
494 | |
495 | if (!disable_idle_d3) | |
51ef3a00 | 496 | vfio_pci_set_power_state(vdev, PCI_D3hot); |
89e1f7d4 AW |
497 | } |
498 | ||
cc20d799 AW |
499 | static struct pci_driver vfio_pci_driver; |
500 | ||
07d47b42 | 501 | static struct vfio_pci_device *get_pf_vdev(struct vfio_pci_device *vdev) |
cc20d799 AW |
502 | { |
503 | struct pci_dev *physfn = pci_physfn(vdev->pdev); | |
07d47b42 | 504 | struct vfio_device *pf_dev; |
cc20d799 AW |
505 | |
506 | if (!vdev->pdev->is_virtfn) | |
507 | return NULL; | |
508 | ||
07d47b42 JG |
509 | pf_dev = vfio_device_get_from_dev(&physfn->dev); |
510 | if (!pf_dev) | |
cc20d799 AW |
511 | return NULL; |
512 | ||
513 | if (pci_dev_driver(physfn) != &vfio_pci_driver) { | |
07d47b42 | 514 | vfio_device_put(pf_dev); |
cc20d799 AW |
515 | return NULL; |
516 | } | |
517 | ||
07d47b42 | 518 | return container_of(pf_dev, struct vfio_pci_device, vdev); |
cc20d799 AW |
519 | } |
520 | ||
521 | static void vfio_pci_vf_token_user_add(struct vfio_pci_device *vdev, int val) | |
522 | { | |
07d47b42 | 523 | struct vfio_pci_device *pf_vdev = get_pf_vdev(vdev); |
cc20d799 AW |
524 | |
525 | if (!pf_vdev) | |
526 | return; | |
527 | ||
528 | mutex_lock(&pf_vdev->vf_token->lock); | |
529 | pf_vdev->vf_token->users += val; | |
530 | WARN_ON(pf_vdev->vf_token->users < 0); | |
531 | mutex_unlock(&pf_vdev->vf_token->lock); | |
532 | ||
07d47b42 | 533 | vfio_device_put(&pf_vdev->vdev); |
cc20d799 AW |
534 | } |
535 | ||
6df62c5b | 536 | static void vfio_pci_release(struct vfio_device *core_vdev) |
89e1f7d4 | 537 | { |
6df62c5b JG |
538 | struct vfio_pci_device *vdev = |
539 | container_of(core_vdev, struct vfio_pci_device, vdev); | |
89e1f7d4 | 540 | |
e309df5b | 541 | mutex_lock(&vdev->reflck->lock); |
61d79256 AW |
542 | |
543 | if (!(--vdev->refcnt)) { | |
cc20d799 | 544 | vfio_pci_vf_token_user_add(vdev, -1); |
1b69be5e | 545 | vfio_spapr_pci_eeh_release(vdev->pdev); |
89e1f7d4 | 546 | vfio_pci_disable(vdev); |
924b51ab | 547 | |
b872d064 | 548 | mutex_lock(&vdev->igate); |
5c5866c5 | 549 | if (vdev->err_trigger) { |
1518ac27 | 550 | eventfd_ctx_put(vdev->err_trigger); |
5c5866c5 AW |
551 | vdev->err_trigger = NULL; |
552 | } | |
553 | if (vdev->req_trigger) { | |
1518ac27 | 554 | eventfd_ctx_put(vdev->req_trigger); |
5c5866c5 AW |
555 | vdev->req_trigger = NULL; |
556 | } | |
b872d064 | 557 | mutex_unlock(&vdev->igate); |
1b69be5e | 558 | } |
89e1f7d4 | 559 | |
e309df5b | 560 | mutex_unlock(&vdev->reflck->lock); |
89e1f7d4 AW |
561 | } |
562 | ||
6df62c5b | 563 | static int vfio_pci_open(struct vfio_device *core_vdev) |
89e1f7d4 | 564 | { |
6df62c5b JG |
565 | struct vfio_pci_device *vdev = |
566 | container_of(core_vdev, struct vfio_pci_device, vdev); | |
61d79256 | 567 | int ret = 0; |
89e1f7d4 | 568 | |
e309df5b | 569 | mutex_lock(&vdev->reflck->lock); |
61d79256 AW |
570 | |
571 | if (!vdev->refcnt) { | |
1b69be5e GS |
572 | ret = vfio_pci_enable(vdev); |
573 | if (ret) | |
574 | goto error; | |
575 | ||
9b936c96 | 576 | vfio_spapr_pci_eeh_open(vdev->pdev); |
cc20d799 | 577 | vfio_pci_vf_token_user_add(vdev, 1); |
89e1f7d4 | 578 | } |
61d79256 | 579 | vdev->refcnt++; |
1b69be5e | 580 | error: |
e309df5b | 581 | mutex_unlock(&vdev->reflck->lock); |
1b69be5e | 582 | return ret; |
89e1f7d4 AW |
583 | } |
584 | ||
585 | static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type) | |
586 | { | |
587 | if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) { | |
588 | u8 pin; | |
db04264f AW |
589 | |
590 | if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) || | |
591 | vdev->nointx || vdev->pdev->is_virtfn) | |
592 | return 0; | |
593 | ||
89e1f7d4 | 594 | pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin); |
89e1f7d4 | 595 | |
db04264f | 596 | return pin ? 1 : 0; |
89e1f7d4 AW |
597 | } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) { |
598 | u8 pos; | |
599 | u16 flags; | |
600 | ||
a9047f24 | 601 | pos = vdev->pdev->msi_cap; |
89e1f7d4 AW |
602 | if (pos) { |
603 | pci_read_config_word(vdev->pdev, | |
604 | pos + PCI_MSI_FLAGS, &flags); | |
fd49c81f | 605 | return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1); |
89e1f7d4 AW |
606 | } |
607 | } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) { | |
608 | u8 pos; | |
609 | u16 flags; | |
610 | ||
a9047f24 | 611 | pos = vdev->pdev->msix_cap; |
89e1f7d4 AW |
612 | if (pos) { |
613 | pci_read_config_word(vdev->pdev, | |
614 | pos + PCI_MSIX_FLAGS, &flags); | |
615 | ||
616 | return (flags & PCI_MSIX_FLAGS_QSIZE) + 1; | |
617 | } | |
6140a8f5 | 618 | } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) { |
dad9f897 VMP |
619 | if (pci_is_pcie(vdev->pdev)) |
620 | return 1; | |
6140a8f5 AW |
621 | } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) { |
622 | return 1; | |
623 | } | |
89e1f7d4 AW |
624 | |
625 | return 0; | |
626 | } | |
627 | ||
8b27ee60 AW |
628 | static int vfio_pci_count_devs(struct pci_dev *pdev, void *data) |
629 | { | |
630 | (*(int *)data)++; | |
631 | return 0; | |
632 | } | |
633 | ||
634 | struct vfio_pci_fill_info { | |
635 | int max; | |
636 | int cur; | |
637 | struct vfio_pci_dependent_device *devices; | |
638 | }; | |
639 | ||
640 | static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data) | |
641 | { | |
642 | struct vfio_pci_fill_info *fill = data; | |
643 | struct iommu_group *iommu_group; | |
644 | ||
645 | if (fill->cur == fill->max) | |
646 | return -EAGAIN; /* Something changed, try again */ | |
647 | ||
648 | iommu_group = iommu_group_get(&pdev->dev); | |
649 | if (!iommu_group) | |
650 | return -EPERM; /* Cannot reset non-isolated devices */ | |
651 | ||
652 | fill->devices[fill->cur].group_id = iommu_group_id(iommu_group); | |
653 | fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus); | |
654 | fill->devices[fill->cur].bus = pdev->bus->number; | |
655 | fill->devices[fill->cur].devfn = pdev->devfn; | |
656 | fill->cur++; | |
657 | iommu_group_put(iommu_group); | |
658 | return 0; | |
659 | } | |
660 | ||
661 | struct vfio_pci_group_entry { | |
662 | struct vfio_group *group; | |
663 | int id; | |
664 | }; | |
665 | ||
666 | struct vfio_pci_group_info { | |
667 | int count; | |
668 | struct vfio_pci_group_entry *groups; | |
669 | }; | |
670 | ||
671 | static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data) | |
672 | { | |
673 | struct vfio_pci_group_info *info = data; | |
674 | struct iommu_group *group; | |
675 | int id, i; | |
676 | ||
677 | group = iommu_group_get(&pdev->dev); | |
678 | if (!group) | |
679 | return -EPERM; | |
680 | ||
681 | id = iommu_group_id(group); | |
682 | ||
683 | for (i = 0; i < info->count; i++) | |
684 | if (info->groups[i].id == id) | |
685 | break; | |
686 | ||
687 | iommu_group_put(group); | |
688 | ||
689 | return (i == info->count) ? -EINVAL : 0; | |
690 | } | |
691 | ||
692 | static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot) | |
693 | { | |
694 | for (; pdev; pdev = pdev->bus->self) | |
695 | if (pdev->bus == slot->bus) | |
696 | return (pdev->slot == slot); | |
697 | return false; | |
698 | } | |
699 | ||
700 | struct vfio_pci_walk_info { | |
701 | int (*fn)(struct pci_dev *, void *data); | |
702 | void *data; | |
703 | struct pci_dev *pdev; | |
704 | bool slot; | |
705 | int ret; | |
706 | }; | |
707 | ||
708 | static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data) | |
709 | { | |
710 | struct vfio_pci_walk_info *walk = data; | |
711 | ||
712 | if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot)) | |
713 | walk->ret = walk->fn(pdev, walk->data); | |
714 | ||
715 | return walk->ret; | |
716 | } | |
717 | ||
718 | static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev, | |
719 | int (*fn)(struct pci_dev *, | |
720 | void *data), void *data, | |
721 | bool slot) | |
722 | { | |
723 | struct vfio_pci_walk_info walk = { | |
724 | .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0, | |
725 | }; | |
726 | ||
727 | pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk); | |
728 | ||
729 | return walk.ret; | |
730 | } | |
731 | ||
a32295c6 AK |
732 | static int msix_mmappable_cap(struct vfio_pci_device *vdev, |
733 | struct vfio_info_cap *caps) | |
188ad9d6 | 734 | { |
a32295c6 AK |
735 | struct vfio_info_cap_header header = { |
736 | .id = VFIO_REGION_INFO_CAP_MSIX_MAPPABLE, | |
737 | .version = 1 | |
738 | }; | |
28541d41 | 739 | |
a32295c6 | 740 | return vfio_info_add_capability(caps, &header, sizeof(header)); |
28541d41 AW |
741 | } |
742 | ||
743 | int vfio_pci_register_dev_region(struct vfio_pci_device *vdev, | |
744 | unsigned int type, unsigned int subtype, | |
745 | const struct vfio_pci_regops *ops, | |
746 | size_t size, u32 flags, void *data) | |
747 | { | |
748 | struct vfio_pci_region *region; | |
749 | ||
750 | region = krealloc(vdev->region, | |
751 | (vdev->num_regions + 1) * sizeof(*region), | |
752 | GFP_KERNEL); | |
753 | if (!region) | |
754 | return -ENOMEM; | |
755 | ||
756 | vdev->region = region; | |
757 | vdev->region[vdev->num_regions].type = type; | |
758 | vdev->region[vdev->num_regions].subtype = subtype; | |
759 | vdev->region[vdev->num_regions].ops = ops; | |
760 | vdev->region[vdev->num_regions].size = size; | |
761 | vdev->region[vdev->num_regions].flags = flags; | |
762 | vdev->region[vdev->num_regions].data = data; | |
763 | ||
764 | vdev->num_regions++; | |
765 | ||
766 | return 0; | |
767 | } | |
768 | ||
abafbc55 | 769 | struct vfio_devices { |
07d47b42 | 770 | struct vfio_pci_device **devices; |
abafbc55 AW |
771 | int cur_index; |
772 | int max_index; | |
773 | }; | |
774 | ||
6df62c5b | 775 | static long vfio_pci_ioctl(struct vfio_device *core_vdev, |
89e1f7d4 AW |
776 | unsigned int cmd, unsigned long arg) |
777 | { | |
6df62c5b JG |
778 | struct vfio_pci_device *vdev = |
779 | container_of(core_vdev, struct vfio_pci_device, vdev); | |
89e1f7d4 AW |
780 | unsigned long minsz; |
781 | ||
782 | if (cmd == VFIO_DEVICE_GET_INFO) { | |
783 | struct vfio_device_info info; | |
e6b817d4 MR |
784 | struct vfio_info_cap caps = { .buf = NULL, .size = 0 }; |
785 | unsigned long capsz; | |
b9abef43 | 786 | int ret; |
89e1f7d4 AW |
787 | |
788 | minsz = offsetofend(struct vfio_device_info, num_irqs); | |
789 | ||
e6b817d4 MR |
790 | /* For backward compatibility, cannot require this */ |
791 | capsz = offsetofend(struct vfio_iommu_type1_info, cap_offset); | |
792 | ||
89e1f7d4 AW |
793 | if (copy_from_user(&info, (void __user *)arg, minsz)) |
794 | return -EFAULT; | |
795 | ||
796 | if (info.argsz < minsz) | |
797 | return -EINVAL; | |
798 | ||
e6b817d4 MR |
799 | if (info.argsz >= capsz) { |
800 | minsz = capsz; | |
801 | info.cap_offset = 0; | |
802 | } | |
803 | ||
89e1f7d4 AW |
804 | info.flags = VFIO_DEVICE_FLAGS_PCI; |
805 | ||
806 | if (vdev->reset_works) | |
807 | info.flags |= VFIO_DEVICE_FLAGS_RESET; | |
808 | ||
28541d41 | 809 | info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions; |
89e1f7d4 AW |
810 | info.num_irqs = VFIO_PCI_NUM_IRQS; |
811 | ||
b9abef43 MG |
812 | ret = vfio_pci_info_zdev_add_caps(vdev, &caps); |
813 | if (ret && ret != -ENODEV) { | |
814 | pci_warn(vdev->pdev, "Failed to setup zPCI info capabilities\n"); | |
815 | return ret; | |
e6b817d4 MR |
816 | } |
817 | ||
818 | if (caps.size) { | |
819 | info.flags |= VFIO_DEVICE_FLAGS_CAPS; | |
820 | if (info.argsz < sizeof(info) + caps.size) { | |
821 | info.argsz = sizeof(info) + caps.size; | |
822 | } else { | |
823 | vfio_info_cap_shift(&caps, sizeof(info)); | |
824 | if (copy_to_user((void __user *)arg + | |
825 | sizeof(info), caps.buf, | |
826 | caps.size)) { | |
827 | kfree(caps.buf); | |
828 | return -EFAULT; | |
829 | } | |
830 | info.cap_offset = sizeof(info); | |
831 | } | |
832 | ||
833 | kfree(caps.buf); | |
834 | } | |
835 | ||
8160c4e4 MT |
836 | return copy_to_user((void __user *)arg, &info, minsz) ? |
837 | -EFAULT : 0; | |
89e1f7d4 AW |
838 | |
839 | } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) { | |
840 | struct pci_dev *pdev = vdev->pdev; | |
841 | struct vfio_region_info info; | |
188ad9d6 | 842 | struct vfio_info_cap caps = { .buf = NULL, .size = 0 }; |
28541d41 | 843 | int i, ret; |
89e1f7d4 AW |
844 | |
845 | minsz = offsetofend(struct vfio_region_info, offset); | |
846 | ||
847 | if (copy_from_user(&info, (void __user *)arg, minsz)) | |
848 | return -EFAULT; | |
849 | ||
850 | if (info.argsz < minsz) | |
851 | return -EINVAL; | |
852 | ||
853 | switch (info.index) { | |
854 | case VFIO_PCI_CONFIG_REGION_INDEX: | |
855 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); | |
856 | info.size = pdev->cfg_size; | |
857 | info.flags = VFIO_REGION_INFO_FLAG_READ | | |
858 | VFIO_REGION_INFO_FLAG_WRITE; | |
859 | break; | |
860 | case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: | |
861 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); | |
862 | info.size = pci_resource_len(pdev, info.index); | |
863 | if (!info.size) { | |
864 | info.flags = 0; | |
865 | break; | |
866 | } | |
867 | ||
868 | info.flags = VFIO_REGION_INFO_FLAG_READ | | |
869 | VFIO_REGION_INFO_FLAG_WRITE; | |
05f0c03f | 870 | if (vdev->bar_mmap_supported[info.index]) { |
89e1f7d4 | 871 | info.flags |= VFIO_REGION_INFO_FLAG_MMAP; |
188ad9d6 | 872 | if (info.index == vdev->msix_bar) { |
a32295c6 | 873 | ret = msix_mmappable_cap(vdev, &caps); |
188ad9d6 AW |
874 | if (ret) |
875 | return ret; | |
876 | } | |
877 | } | |
878 | ||
89e1f7d4 AW |
879 | break; |
880 | case VFIO_PCI_ROM_REGION_INDEX: | |
881 | { | |
882 | void __iomem *io; | |
883 | size_t size; | |
abafbc55 | 884 | u16 cmd; |
89e1f7d4 AW |
885 | |
886 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); | |
887 | info.flags = 0; | |
888 | ||
889 | /* Report the BAR size, not the ROM size */ | |
890 | info.size = pci_resource_len(pdev, info.index); | |
a13b6459 AW |
891 | if (!info.size) { |
892 | /* Shadow ROMs appear as PCI option ROMs */ | |
893 | if (pdev->resource[PCI_ROM_RESOURCE].flags & | |
894 | IORESOURCE_ROM_SHADOW) | |
895 | info.size = 0x20000; | |
896 | else | |
897 | break; | |
898 | } | |
89e1f7d4 | 899 | |
0cfd027b EA |
900 | /* |
901 | * Is it really there? Enable memory decode for | |
902 | * implicit access in pci_map_rom(). | |
903 | */ | |
abafbc55 | 904 | cmd = vfio_pci_memory_lock_and_enable(vdev); |
89e1f7d4 | 905 | io = pci_map_rom(pdev, &size); |
0cfd027b EA |
906 | if (io) { |
907 | info.flags = VFIO_REGION_INFO_FLAG_READ; | |
908 | pci_unmap_rom(pdev, io); | |
909 | } else { | |
89e1f7d4 | 910 | info.size = 0; |
89e1f7d4 | 911 | } |
abafbc55 | 912 | vfio_pci_memory_unlock_and_restore(vdev, cmd); |
89e1f7d4 | 913 | |
89e1f7d4 AW |
914 | break; |
915 | } | |
84237a82 AW |
916 | case VFIO_PCI_VGA_REGION_INDEX: |
917 | if (!vdev->has_vga) | |
918 | return -EINVAL; | |
919 | ||
920 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); | |
921 | info.size = 0xc0000; | |
922 | info.flags = VFIO_REGION_INFO_FLAG_READ | | |
923 | VFIO_REGION_INFO_FLAG_WRITE; | |
924 | ||
925 | break; | |
89e1f7d4 | 926 | default: |
c535d345 | 927 | { |
dda01f78 AW |
928 | struct vfio_region_info_cap_type cap_type = { |
929 | .header.id = VFIO_REGION_INFO_CAP_TYPE, | |
930 | .header.version = 1 }; | |
c535d345 | 931 | |
28541d41 AW |
932 | if (info.index >= |
933 | VFIO_PCI_NUM_REGIONS + vdev->num_regions) | |
934 | return -EINVAL; | |
0e714d27 GS |
935 | info.index = array_index_nospec(info.index, |
936 | VFIO_PCI_NUM_REGIONS + | |
937 | vdev->num_regions); | |
28541d41 AW |
938 | |
939 | i = info.index - VFIO_PCI_NUM_REGIONS; | |
940 | ||
941 | info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index); | |
942 | info.size = vdev->region[i].size; | |
943 | info.flags = vdev->region[i].flags; | |
944 | ||
c535d345 KW |
945 | cap_type.type = vdev->region[i].type; |
946 | cap_type.subtype = vdev->region[i].subtype; | |
947 | ||
dda01f78 AW |
948 | ret = vfio_info_add_capability(&caps, &cap_type.header, |
949 | sizeof(cap_type)); | |
28541d41 AW |
950 | if (ret) |
951 | return ret; | |
c535d345 | 952 | |
c2c0f1cd AK |
953 | if (vdev->region[i].ops->add_capability) { |
954 | ret = vdev->region[i].ops->add_capability(vdev, | |
955 | &vdev->region[i], &caps); | |
956 | if (ret) | |
957 | return ret; | |
958 | } | |
c535d345 | 959 | } |
89e1f7d4 AW |
960 | } |
961 | ||
188ad9d6 AW |
962 | if (caps.size) { |
963 | info.flags |= VFIO_REGION_INFO_FLAG_CAPS; | |
964 | if (info.argsz < sizeof(info) + caps.size) { | |
965 | info.argsz = sizeof(info) + caps.size; | |
966 | info.cap_offset = 0; | |
967 | } else { | |
968 | vfio_info_cap_shift(&caps, sizeof(info)); | |
c4aec310 DC |
969 | if (copy_to_user((void __user *)arg + |
970 | sizeof(info), caps.buf, | |
971 | caps.size)) { | |
188ad9d6 | 972 | kfree(caps.buf); |
c4aec310 | 973 | return -EFAULT; |
188ad9d6 AW |
974 | } |
975 | info.cap_offset = sizeof(info); | |
976 | } | |
977 | ||
978 | kfree(caps.buf); | |
89e1f7d4 AW |
979 | } |
980 | ||
8160c4e4 MT |
981 | return copy_to_user((void __user *)arg, &info, minsz) ? |
982 | -EFAULT : 0; | |
89e1f7d4 AW |
983 | |
984 | } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) { | |
985 | struct vfio_irq_info info; | |
986 | ||
987 | minsz = offsetofend(struct vfio_irq_info, count); | |
988 | ||
989 | if (copy_from_user(&info, (void __user *)arg, minsz)) | |
990 | return -EFAULT; | |
991 | ||
992 | if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS) | |
993 | return -EINVAL; | |
994 | ||
dad9f897 VMP |
995 | switch (info.index) { |
996 | case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX: | |
6140a8f5 | 997 | case VFIO_PCI_REQ_IRQ_INDEX: |
dad9f897 VMP |
998 | break; |
999 | case VFIO_PCI_ERR_IRQ_INDEX: | |
1000 | if (pci_is_pcie(vdev->pdev)) | |
1001 | break; | |
df561f66 | 1002 | fallthrough; |
dad9f897 VMP |
1003 | default: |
1004 | return -EINVAL; | |
1005 | } | |
1006 | ||
89e1f7d4 AW |
1007 | info.flags = VFIO_IRQ_INFO_EVENTFD; |
1008 | ||
1009 | info.count = vfio_pci_get_irq_count(vdev, info.index); | |
1010 | ||
1011 | if (info.index == VFIO_PCI_INTX_IRQ_INDEX) | |
1012 | info.flags |= (VFIO_IRQ_INFO_MASKABLE | | |
1013 | VFIO_IRQ_INFO_AUTOMASKED); | |
1014 | else | |
1015 | info.flags |= VFIO_IRQ_INFO_NORESIZE; | |
1016 | ||
8160c4e4 MT |
1017 | return copy_to_user((void __user *)arg, &info, minsz) ? |
1018 | -EFAULT : 0; | |
89e1f7d4 AW |
1019 | |
1020 | } else if (cmd == VFIO_DEVICE_SET_IRQS) { | |
1021 | struct vfio_irq_set hdr; | |
1022 | u8 *data = NULL; | |
05692d70 | 1023 | int max, ret = 0; |
ef198aaa | 1024 | size_t data_size = 0; |
89e1f7d4 AW |
1025 | |
1026 | minsz = offsetofend(struct vfio_irq_set, count); | |
1027 | ||
1028 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) | |
1029 | return -EFAULT; | |
1030 | ||
05692d70 | 1031 | max = vfio_pci_get_irq_count(vdev, hdr.index); |
89e1f7d4 | 1032 | |
ef198aaa KW |
1033 | ret = vfio_set_irqs_validate_and_prepare(&hdr, max, |
1034 | VFIO_PCI_NUM_IRQS, &data_size); | |
1035 | if (ret) | |
1036 | return ret; | |
89e1f7d4 | 1037 | |
ef198aaa | 1038 | if (data_size) { |
3a1f7041 | 1039 | data = memdup_user((void __user *)(arg + minsz), |
ef198aaa | 1040 | data_size); |
3a1f7041 FW |
1041 | if (IS_ERR(data)) |
1042 | return PTR_ERR(data); | |
89e1f7d4 AW |
1043 | } |
1044 | ||
1045 | mutex_lock(&vdev->igate); | |
1046 | ||
1047 | ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index, | |
1048 | hdr.start, hdr.count, data); | |
1049 | ||
1050 | mutex_unlock(&vdev->igate); | |
1051 | kfree(data); | |
1052 | ||
1053 | return ret; | |
1054 | ||
8b27ee60 | 1055 | } else if (cmd == VFIO_DEVICE_RESET) { |
abafbc55 AW |
1056 | int ret; |
1057 | ||
1058 | if (!vdev->reset_works) | |
1059 | return -EINVAL; | |
1060 | ||
1061 | vfio_pci_zap_and_down_write_memory_lock(vdev); | |
1062 | ret = pci_try_reset_function(vdev->pdev); | |
1063 | up_write(&vdev->memory_lock); | |
1064 | ||
1065 | return ret; | |
89e1f7d4 | 1066 | |
8b27ee60 AW |
1067 | } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) { |
1068 | struct vfio_pci_hot_reset_info hdr; | |
1069 | struct vfio_pci_fill_info fill = { 0 }; | |
1070 | struct vfio_pci_dependent_device *devices = NULL; | |
1071 | bool slot = false; | |
1072 | int ret = 0; | |
1073 | ||
1074 | minsz = offsetofend(struct vfio_pci_hot_reset_info, count); | |
1075 | ||
1076 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) | |
1077 | return -EFAULT; | |
1078 | ||
1079 | if (hdr.argsz < minsz) | |
1080 | return -EINVAL; | |
1081 | ||
1082 | hdr.flags = 0; | |
1083 | ||
1084 | /* Can we do a slot or bus reset or neither? */ | |
1085 | if (!pci_probe_reset_slot(vdev->pdev->slot)) | |
1086 | slot = true; | |
1087 | else if (pci_probe_reset_bus(vdev->pdev->bus)) | |
1088 | return -ENODEV; | |
1089 | ||
1090 | /* How many devices are affected? */ | |
1091 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, | |
1092 | vfio_pci_count_devs, | |
1093 | &fill.max, slot); | |
1094 | if (ret) | |
1095 | return ret; | |
1096 | ||
1097 | WARN_ON(!fill.max); /* Should always be at least one */ | |
1098 | ||
1099 | /* | |
1100 | * If there's enough space, fill it now, otherwise return | |
1101 | * -ENOSPC and the number of devices affected. | |
1102 | */ | |
1103 | if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) { | |
1104 | ret = -ENOSPC; | |
1105 | hdr.count = fill.max; | |
1106 | goto reset_info_exit; | |
1107 | } | |
1108 | ||
1109 | devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL); | |
1110 | if (!devices) | |
1111 | return -ENOMEM; | |
1112 | ||
1113 | fill.devices = devices; | |
1114 | ||
1115 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, | |
1116 | vfio_pci_fill_devs, | |
1117 | &fill, slot); | |
1118 | ||
1119 | /* | |
1120 | * If a device was removed between counting and filling, | |
1121 | * we may come up short of fill.max. If a device was | |
1122 | * added, we'll have a return of -EAGAIN above. | |
1123 | */ | |
1124 | if (!ret) | |
1125 | hdr.count = fill.cur; | |
1126 | ||
1127 | reset_info_exit: | |
1128 | if (copy_to_user((void __user *)arg, &hdr, minsz)) | |
1129 | ret = -EFAULT; | |
1130 | ||
1131 | if (!ret) { | |
1132 | if (copy_to_user((void __user *)(arg + minsz), devices, | |
1133 | hdr.count * sizeof(*devices))) | |
1134 | ret = -EFAULT; | |
1135 | } | |
1136 | ||
1137 | kfree(devices); | |
1138 | return ret; | |
1139 | ||
1140 | } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) { | |
1141 | struct vfio_pci_hot_reset hdr; | |
1142 | int32_t *group_fds; | |
1143 | struct vfio_pci_group_entry *groups; | |
1144 | struct vfio_pci_group_info info; | |
abafbc55 | 1145 | struct vfio_devices devs = { .cur_index = 0 }; |
8b27ee60 | 1146 | bool slot = false; |
abafbc55 | 1147 | int i, group_idx, mem_idx = 0, count = 0, ret = 0; |
8b27ee60 AW |
1148 | |
1149 | minsz = offsetofend(struct vfio_pci_hot_reset, count); | |
1150 | ||
1151 | if (copy_from_user(&hdr, (void __user *)arg, minsz)) | |
1152 | return -EFAULT; | |
1153 | ||
1154 | if (hdr.argsz < minsz || hdr.flags) | |
1155 | return -EINVAL; | |
1156 | ||
1157 | /* Can we do a slot or bus reset or neither? */ | |
1158 | if (!pci_probe_reset_slot(vdev->pdev->slot)) | |
1159 | slot = true; | |
1160 | else if (pci_probe_reset_bus(vdev->pdev->bus)) | |
1161 | return -ENODEV; | |
1162 | ||
1163 | /* | |
1164 | * We can't let userspace give us an arbitrarily large | |
1165 | * buffer to copy, so verify how many we think there | |
1166 | * could be. Note groups can have multiple devices so | |
1167 | * one group per device is the max. | |
1168 | */ | |
1169 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, | |
1170 | vfio_pci_count_devs, | |
1171 | &count, slot); | |
1172 | if (ret) | |
1173 | return ret; | |
1174 | ||
1175 | /* Somewhere between 1 and count is OK */ | |
1176 | if (!hdr.count || hdr.count > count) | |
1177 | return -EINVAL; | |
1178 | ||
1179 | group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL); | |
1180 | groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL); | |
1181 | if (!group_fds || !groups) { | |
1182 | kfree(group_fds); | |
1183 | kfree(groups); | |
1184 | return -ENOMEM; | |
1185 | } | |
1186 | ||
1187 | if (copy_from_user(group_fds, (void __user *)(arg + minsz), | |
1188 | hdr.count * sizeof(*group_fds))) { | |
1189 | kfree(group_fds); | |
1190 | kfree(groups); | |
1191 | return -EFAULT; | |
1192 | } | |
1193 | ||
1194 | /* | |
1195 | * For each group_fd, get the group through the vfio external | |
1196 | * user interface and store the group and iommu ID. This | |
1197 | * ensures the group is held across the reset. | |
1198 | */ | |
abafbc55 | 1199 | for (group_idx = 0; group_idx < hdr.count; group_idx++) { |
8b27ee60 | 1200 | struct vfio_group *group; |
abafbc55 | 1201 | struct fd f = fdget(group_fds[group_idx]); |
8b27ee60 AW |
1202 | if (!f.file) { |
1203 | ret = -EBADF; | |
1204 | break; | |
1205 | } | |
1206 | ||
1207 | group = vfio_group_get_external_user(f.file); | |
1208 | fdput(f); | |
1209 | if (IS_ERR(group)) { | |
1210 | ret = PTR_ERR(group); | |
1211 | break; | |
1212 | } | |
1213 | ||
abafbc55 AW |
1214 | groups[group_idx].group = group; |
1215 | groups[group_idx].id = | |
1216 | vfio_external_user_iommu_id(group); | |
8b27ee60 AW |
1217 | } |
1218 | ||
1219 | kfree(group_fds); | |
1220 | ||
1221 | /* release reference to groups on error */ | |
1222 | if (ret) | |
1223 | goto hot_reset_release; | |
1224 | ||
1225 | info.count = hdr.count; | |
1226 | info.groups = groups; | |
1227 | ||
1228 | /* | |
1229 | * Test whether all the affected devices are contained | |
1230 | * by the set of groups provided by the user. | |
1231 | */ | |
1232 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, | |
1233 | vfio_pci_validate_devs, | |
1234 | &info, slot); | |
abafbc55 AW |
1235 | if (ret) |
1236 | goto hot_reset_release; | |
1237 | ||
1238 | devs.max_index = count; | |
1239 | devs.devices = kcalloc(count, sizeof(struct vfio_device *), | |
1240 | GFP_KERNEL); | |
1241 | if (!devs.devices) { | |
1242 | ret = -ENOMEM; | |
1243 | goto hot_reset_release; | |
1244 | } | |
1245 | ||
1246 | /* | |
1247 | * We need to get memory_lock for each device, but devices | |
c1e8d7c6 | 1248 | * can share mmap_lock, therefore we need to zap and hold |
abafbc55 AW |
1249 | * the vma_lock for each device, and only then get each |
1250 | * memory_lock. | |
1251 | */ | |
1252 | ret = vfio_pci_for_each_slot_or_bus(vdev->pdev, | |
1253 | vfio_pci_try_zap_and_vma_lock_cb, | |
1254 | &devs, slot); | |
1255 | if (ret) | |
1256 | goto hot_reset_release; | |
1257 | ||
1258 | for (; mem_idx < devs.cur_index; mem_idx++) { | |
07d47b42 | 1259 | struct vfio_pci_device *tmp = devs.devices[mem_idx]; |
abafbc55 AW |
1260 | |
1261 | ret = down_write_trylock(&tmp->memory_lock); | |
1262 | if (!ret) { | |
1263 | ret = -EBUSY; | |
1264 | goto hot_reset_release; | |
1265 | } | |
1266 | mutex_unlock(&tmp->vma_lock); | |
1267 | } | |
1268 | ||
1269 | /* User has access, do the reset */ | |
1270 | ret = pci_reset_bus(vdev->pdev); | |
8b27ee60 AW |
1271 | |
1272 | hot_reset_release: | |
abafbc55 | 1273 | for (i = 0; i < devs.cur_index; i++) { |
07d47b42 | 1274 | struct vfio_pci_device *tmp = devs.devices[i]; |
abafbc55 AW |
1275 | |
1276 | if (i < mem_idx) | |
1277 | up_write(&tmp->memory_lock); | |
1278 | else | |
1279 | mutex_unlock(&tmp->vma_lock); | |
07d47b42 | 1280 | vfio_device_put(&tmp->vdev); |
abafbc55 AW |
1281 | } |
1282 | kfree(devs.devices); | |
1283 | ||
1284 | for (group_idx--; group_idx >= 0; group_idx--) | |
1285 | vfio_group_put_external_user(groups[group_idx].group); | |
8b27ee60 AW |
1286 | |
1287 | kfree(groups); | |
1288 | return ret; | |
30656177 AW |
1289 | } else if (cmd == VFIO_DEVICE_IOEVENTFD) { |
1290 | struct vfio_device_ioeventfd ioeventfd; | |
1291 | int count; | |
1292 | ||
1293 | minsz = offsetofend(struct vfio_device_ioeventfd, fd); | |
1294 | ||
1295 | if (copy_from_user(&ioeventfd, (void __user *)arg, minsz)) | |
1296 | return -EFAULT; | |
1297 | ||
1298 | if (ioeventfd.argsz < minsz) | |
1299 | return -EINVAL; | |
1300 | ||
1301 | if (ioeventfd.flags & ~VFIO_DEVICE_IOEVENTFD_SIZE_MASK) | |
1302 | return -EINVAL; | |
1303 | ||
1304 | count = ioeventfd.flags & VFIO_DEVICE_IOEVENTFD_SIZE_MASK; | |
1305 | ||
1306 | if (hweight8(count) != 1 || ioeventfd.fd < -1) | |
1307 | return -EINVAL; | |
1308 | ||
1309 | return vfio_pci_ioeventfd(vdev, ioeventfd.offset, | |
1310 | ioeventfd.data, count, ioeventfd.fd); | |
43eeeecc AW |
1311 | } else if (cmd == VFIO_DEVICE_FEATURE) { |
1312 | struct vfio_device_feature feature; | |
1313 | uuid_t uuid; | |
1314 | ||
1315 | minsz = offsetofend(struct vfio_device_feature, flags); | |
1316 | ||
1317 | if (copy_from_user(&feature, (void __user *)arg, minsz)) | |
1318 | return -EFAULT; | |
1319 | ||
1320 | if (feature.argsz < minsz) | |
1321 | return -EINVAL; | |
1322 | ||
1323 | /* Check unknown flags */ | |
1324 | if (feature.flags & ~(VFIO_DEVICE_FEATURE_MASK | | |
1325 | VFIO_DEVICE_FEATURE_SET | | |
1326 | VFIO_DEVICE_FEATURE_GET | | |
1327 | VFIO_DEVICE_FEATURE_PROBE)) | |
1328 | return -EINVAL; | |
1329 | ||
1330 | /* GET & SET are mutually exclusive except with PROBE */ | |
1331 | if (!(feature.flags & VFIO_DEVICE_FEATURE_PROBE) && | |
1332 | (feature.flags & VFIO_DEVICE_FEATURE_SET) && | |
1333 | (feature.flags & VFIO_DEVICE_FEATURE_GET)) | |
1334 | return -EINVAL; | |
1335 | ||
1336 | switch (feature.flags & VFIO_DEVICE_FEATURE_MASK) { | |
1337 | case VFIO_DEVICE_FEATURE_PCI_VF_TOKEN: | |
1338 | if (!vdev->vf_token) | |
1339 | return -ENOTTY; | |
1340 | ||
1341 | /* | |
1342 | * We do not support GET of the VF Token UUID as this | |
1343 | * could expose the token of the previous device user. | |
1344 | */ | |
1345 | if (feature.flags & VFIO_DEVICE_FEATURE_GET) | |
1346 | return -EINVAL; | |
1347 | ||
1348 | if (feature.flags & VFIO_DEVICE_FEATURE_PROBE) | |
1349 | return 0; | |
1350 | ||
1351 | /* Don't SET unless told to do so */ | |
1352 | if (!(feature.flags & VFIO_DEVICE_FEATURE_SET)) | |
1353 | return -EINVAL; | |
1354 | ||
1355 | if (feature.argsz < minsz + sizeof(uuid)) | |
1356 | return -EINVAL; | |
1357 | ||
1358 | if (copy_from_user(&uuid, (void __user *)(arg + minsz), | |
1359 | sizeof(uuid))) | |
1360 | return -EFAULT; | |
1361 | ||
1362 | mutex_lock(&vdev->vf_token->lock); | |
1363 | uuid_copy(&vdev->vf_token->uuid, &uuid); | |
1364 | mutex_unlock(&vdev->vf_token->lock); | |
1365 | ||
1366 | return 0; | |
1367 | default: | |
1368 | return -ENOTTY; | |
1369 | } | |
8b27ee60 AW |
1370 | } |
1371 | ||
89e1f7d4 AW |
1372 | return -ENOTTY; |
1373 | } | |
1374 | ||
6df62c5b | 1375 | static ssize_t vfio_pci_rw(struct vfio_pci_device *vdev, char __user *buf, |
5b279a11 | 1376 | size_t count, loff_t *ppos, bool iswrite) |
89e1f7d4 AW |
1377 | { |
1378 | unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); | |
89e1f7d4 | 1379 | |
28541d41 | 1380 | if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) |
89e1f7d4 AW |
1381 | return -EINVAL; |
1382 | ||
5b279a11 AW |
1383 | switch (index) { |
1384 | case VFIO_PCI_CONFIG_REGION_INDEX: | |
906ee99d AW |
1385 | return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite); |
1386 | ||
5b279a11 AW |
1387 | case VFIO_PCI_ROM_REGION_INDEX: |
1388 | if (iswrite) | |
1389 | return -EINVAL; | |
906ee99d | 1390 | return vfio_pci_bar_rw(vdev, buf, count, ppos, false); |
89e1f7d4 | 1391 | |
5b279a11 | 1392 | case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX: |
906ee99d | 1393 | return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite); |
84237a82 AW |
1394 | |
1395 | case VFIO_PCI_VGA_REGION_INDEX: | |
1396 | return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite); | |
28541d41 AW |
1397 | default: |
1398 | index -= VFIO_PCI_NUM_REGIONS; | |
1399 | return vdev->region[index].ops->rw(vdev, buf, | |
1400 | count, ppos, iswrite); | |
5b279a11 AW |
1401 | } |
1402 | ||
89e1f7d4 AW |
1403 | return -EINVAL; |
1404 | } | |
1405 | ||
6df62c5b | 1406 | static ssize_t vfio_pci_read(struct vfio_device *core_vdev, char __user *buf, |
5b279a11 AW |
1407 | size_t count, loff_t *ppos) |
1408 | { | |
6df62c5b JG |
1409 | struct vfio_pci_device *vdev = |
1410 | container_of(core_vdev, struct vfio_pci_device, vdev); | |
1411 | ||
906ee99d AW |
1412 | if (!count) |
1413 | return 0; | |
1414 | ||
6df62c5b | 1415 | return vfio_pci_rw(vdev, buf, count, ppos, false); |
5b279a11 AW |
1416 | } |
1417 | ||
6df62c5b | 1418 | static ssize_t vfio_pci_write(struct vfio_device *core_vdev, const char __user *buf, |
89e1f7d4 AW |
1419 | size_t count, loff_t *ppos) |
1420 | { | |
6df62c5b JG |
1421 | struct vfio_pci_device *vdev = |
1422 | container_of(core_vdev, struct vfio_pci_device, vdev); | |
1423 | ||
906ee99d AW |
1424 | if (!count) |
1425 | return 0; | |
1426 | ||
6df62c5b | 1427 | return vfio_pci_rw(vdev, (char __user *)buf, count, ppos, true); |
89e1f7d4 AW |
1428 | } |
1429 | ||
abafbc55 AW |
1430 | /* Return 1 on zap and vma_lock acquired, 0 on contention (only with @try) */ |
1431 | static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try) | |
1432 | { | |
1433 | struct vfio_pci_mmap_vma *mmap_vma, *tmp; | |
1434 | ||
1435 | /* | |
1436 | * Lock ordering: | |
c1e8d7c6 | 1437 | * vma_lock is nested under mmap_lock for vm_ops callback paths. |
abafbc55 AW |
1438 | * The memory_lock semaphore is used by both code paths calling |
1439 | * into this function to zap vmas and the vm_ops.fault callback | |
1440 | * to protect the memory enable state of the device. | |
1441 | * | |
c1e8d7c6 | 1442 | * When zapping vmas we need to maintain the mmap_lock => vma_lock |
abafbc55 | 1443 | * ordering, which requires using vma_lock to walk vma_list to |
c1e8d7c6 | 1444 | * acquire an mm, then dropping vma_lock to get the mmap_lock and |
abafbc55 AW |
1445 | * reacquiring vma_lock. This logic is derived from similar |
1446 | * requirements in uverbs_user_mmap_disassociate(). | |
1447 | * | |
c1e8d7c6 | 1448 | * mmap_lock must always be the top-level lock when it is taken. |
abafbc55 | 1449 | * Therefore we can only hold the memory_lock write lock when |
c1e8d7c6 | 1450 | * vma_list is empty, as we'd need to take mmap_lock to clear |
abafbc55 AW |
1451 | * entries. vma_list can only be guaranteed empty when holding |
1452 | * vma_lock, thus memory_lock is nested under vma_lock. | |
1453 | * | |
1454 | * This enables the vm_ops.fault callback to acquire vma_lock, | |
1455 | * followed by memory_lock read lock, while already holding | |
c1e8d7c6 | 1456 | * mmap_lock without risk of deadlock. |
abafbc55 AW |
1457 | */ |
1458 | while (1) { | |
1459 | struct mm_struct *mm = NULL; | |
1460 | ||
1461 | if (try) { | |
1462 | if (!mutex_trylock(&vdev->vma_lock)) | |
1463 | return 0; | |
1464 | } else { | |
1465 | mutex_lock(&vdev->vma_lock); | |
1466 | } | |
1467 | while (!list_empty(&vdev->vma_list)) { | |
1468 | mmap_vma = list_first_entry(&vdev->vma_list, | |
1469 | struct vfio_pci_mmap_vma, | |
1470 | vma_next); | |
1471 | mm = mmap_vma->vma->vm_mm; | |
1472 | if (mmget_not_zero(mm)) | |
1473 | break; | |
1474 | ||
1475 | list_del(&mmap_vma->vma_next); | |
1476 | kfree(mmap_vma); | |
1477 | mm = NULL; | |
1478 | } | |
1479 | if (!mm) | |
1480 | return 1; | |
1481 | mutex_unlock(&vdev->vma_lock); | |
1482 | ||
1483 | if (try) { | |
89154dd5 | 1484 | if (!mmap_read_trylock(mm)) { |
abafbc55 AW |
1485 | mmput(mm); |
1486 | return 0; | |
1487 | } | |
1488 | } else { | |
89154dd5 | 1489 | mmap_read_lock(mm); |
abafbc55 | 1490 | } |
4d45e75a JH |
1491 | if (try) { |
1492 | if (!mutex_trylock(&vdev->vma_lock)) { | |
1493 | mmap_read_unlock(mm); | |
1494 | mmput(mm); | |
1495 | return 0; | |
abafbc55 | 1496 | } |
4d45e75a JH |
1497 | } else { |
1498 | mutex_lock(&vdev->vma_lock); | |
1499 | } | |
1500 | list_for_each_entry_safe(mmap_vma, tmp, | |
1501 | &vdev->vma_list, vma_next) { | |
1502 | struct vm_area_struct *vma = mmap_vma->vma; | |
abafbc55 | 1503 | |
4d45e75a JH |
1504 | if (vma->vm_mm != mm) |
1505 | continue; | |
abafbc55 | 1506 | |
4d45e75a JH |
1507 | list_del(&mmap_vma->vma_next); |
1508 | kfree(mmap_vma); | |
abafbc55 | 1509 | |
4d45e75a JH |
1510 | zap_vma_ptes(vma, vma->vm_start, |
1511 | vma->vm_end - vma->vm_start); | |
abafbc55 | 1512 | } |
4d45e75a | 1513 | mutex_unlock(&vdev->vma_lock); |
89154dd5 | 1514 | mmap_read_unlock(mm); |
abafbc55 AW |
1515 | mmput(mm); |
1516 | } | |
1517 | } | |
1518 | ||
1519 | void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device *vdev) | |
1520 | { | |
1521 | vfio_pci_zap_and_vma_lock(vdev, false); | |
1522 | down_write(&vdev->memory_lock); | |
1523 | mutex_unlock(&vdev->vma_lock); | |
1524 | } | |
1525 | ||
1526 | u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev) | |
1527 | { | |
1528 | u16 cmd; | |
1529 | ||
1530 | down_write(&vdev->memory_lock); | |
1531 | pci_read_config_word(vdev->pdev, PCI_COMMAND, &cmd); | |
1532 | if (!(cmd & PCI_COMMAND_MEMORY)) | |
1533 | pci_write_config_word(vdev->pdev, PCI_COMMAND, | |
1534 | cmd | PCI_COMMAND_MEMORY); | |
1535 | ||
1536 | return cmd; | |
1537 | } | |
1538 | ||
1539 | void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev, u16 cmd) | |
1540 | { | |
1541 | pci_write_config_word(vdev->pdev, PCI_COMMAND, cmd); | |
1542 | up_write(&vdev->memory_lock); | |
1543 | } | |
1544 | ||
1545 | /* Caller holds vma_lock */ | |
1546 | static int __vfio_pci_add_vma(struct vfio_pci_device *vdev, | |
1547 | struct vm_area_struct *vma) | |
11c4cd07 AW |
1548 | { |
1549 | struct vfio_pci_mmap_vma *mmap_vma; | |
1550 | ||
1551 | mmap_vma = kmalloc(sizeof(*mmap_vma), GFP_KERNEL); | |
1552 | if (!mmap_vma) | |
1553 | return -ENOMEM; | |
1554 | ||
1555 | mmap_vma->vma = vma; | |
11c4cd07 | 1556 | list_add(&mmap_vma->vma_next, &vdev->vma_list); |
11c4cd07 AW |
1557 | |
1558 | return 0; | |
1559 | } | |
1560 | ||
1561 | /* | |
1562 | * Zap mmaps on open so that we can fault them in on access and therefore | |
1563 | * our vma_list only tracks mappings accessed since last zap. | |
1564 | */ | |
1565 | static void vfio_pci_mmap_open(struct vm_area_struct *vma) | |
1566 | { | |
1567 | zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start); | |
1568 | } | |
1569 | ||
1570 | static void vfio_pci_mmap_close(struct vm_area_struct *vma) | |
1571 | { | |
1572 | struct vfio_pci_device *vdev = vma->vm_private_data; | |
1573 | struct vfio_pci_mmap_vma *mmap_vma; | |
1574 | ||
1575 | mutex_lock(&vdev->vma_lock); | |
1576 | list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) { | |
1577 | if (mmap_vma->vma == vma) { | |
1578 | list_del(&mmap_vma->vma_next); | |
1579 | kfree(mmap_vma); | |
1580 | break; | |
1581 | } | |
1582 | } | |
1583 | mutex_unlock(&vdev->vma_lock); | |
1584 | } | |
1585 | ||
1586 | static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf) | |
1587 | { | |
1588 | struct vm_area_struct *vma = vmf->vma; | |
1589 | struct vfio_pci_device *vdev = vma->vm_private_data; | |
abafbc55 AW |
1590 | vm_fault_t ret = VM_FAULT_NOPAGE; |
1591 | ||
1592 | mutex_lock(&vdev->vma_lock); | |
1593 | down_read(&vdev->memory_lock); | |
1594 | ||
1595 | if (!__vfio_pci_memory_enabled(vdev)) { | |
1596 | ret = VM_FAULT_SIGBUS; | |
1597 | mutex_unlock(&vdev->vma_lock); | |
1598 | goto up_out; | |
1599 | } | |
11c4cd07 | 1600 | |
abafbc55 AW |
1601 | if (__vfio_pci_add_vma(vdev, vma)) { |
1602 | ret = VM_FAULT_OOM; | |
1603 | mutex_unlock(&vdev->vma_lock); | |
1604 | goto up_out; | |
1605 | } | |
1606 | ||
1607 | mutex_unlock(&vdev->vma_lock); | |
11c4cd07 | 1608 | |
7b06a56d JG |
1609 | if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff, |
1610 | vma->vm_end - vma->vm_start, vma->vm_page_prot)) | |
abafbc55 | 1611 | ret = VM_FAULT_SIGBUS; |
11c4cd07 | 1612 | |
abafbc55 AW |
1613 | up_out: |
1614 | up_read(&vdev->memory_lock); | |
1615 | return ret; | |
11c4cd07 AW |
1616 | } |
1617 | ||
1618 | static const struct vm_operations_struct vfio_pci_mmap_ops = { | |
1619 | .open = vfio_pci_mmap_open, | |
1620 | .close = vfio_pci_mmap_close, | |
1621 | .fault = vfio_pci_mmap_fault, | |
1622 | }; | |
1623 | ||
6df62c5b | 1624 | static int vfio_pci_mmap(struct vfio_device *core_vdev, struct vm_area_struct *vma) |
89e1f7d4 | 1625 | { |
6df62c5b JG |
1626 | struct vfio_pci_device *vdev = |
1627 | container_of(core_vdev, struct vfio_pci_device, vdev); | |
89e1f7d4 AW |
1628 | struct pci_dev *pdev = vdev->pdev; |
1629 | unsigned int index; | |
34002f54 | 1630 | u64 phys_len, req_len, pgoff, req_start; |
89e1f7d4 AW |
1631 | int ret; |
1632 | ||
1633 | index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT); | |
1634 | ||
90929078 CE |
1635 | if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions) |
1636 | return -EINVAL; | |
89e1f7d4 AW |
1637 | if (vma->vm_end < vma->vm_start) |
1638 | return -EINVAL; | |
1639 | if ((vma->vm_flags & VM_SHARED) == 0) | |
1640 | return -EINVAL; | |
a15b1883 AK |
1641 | if (index >= VFIO_PCI_NUM_REGIONS) { |
1642 | int regnum = index - VFIO_PCI_NUM_REGIONS; | |
1643 | struct vfio_pci_region *region = vdev->region + regnum; | |
1644 | ||
90929078 | 1645 | if (region->ops && region->ops->mmap && |
a15b1883 AK |
1646 | (region->flags & VFIO_REGION_INFO_FLAG_MMAP)) |
1647 | return region->ops->mmap(vdev, region, vma); | |
1648 | return -EINVAL; | |
1649 | } | |
89e1f7d4 AW |
1650 | if (index >= VFIO_PCI_ROM_REGION_INDEX) |
1651 | return -EINVAL; | |
05f0c03f | 1652 | if (!vdev->bar_mmap_supported[index]) |
89e1f7d4 AW |
1653 | return -EINVAL; |
1654 | ||
05f0c03f | 1655 | phys_len = PAGE_ALIGN(pci_resource_len(pdev, index)); |
89e1f7d4 AW |
1656 | req_len = vma->vm_end - vma->vm_start; |
1657 | pgoff = vma->vm_pgoff & | |
1658 | ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1); | |
1659 | req_start = pgoff << PAGE_SHIFT; | |
1660 | ||
05f0c03f | 1661 | if (req_start + req_len > phys_len) |
89e1f7d4 AW |
1662 | return -EINVAL; |
1663 | ||
89e1f7d4 AW |
1664 | /* |
1665 | * Even though we don't make use of the barmap for the mmap, | |
1666 | * we need to request the region and the barmap tracks that. | |
1667 | */ | |
1668 | if (!vdev->barmap[index]) { | |
1669 | ret = pci_request_selected_regions(pdev, | |
1670 | 1 << index, "vfio-pci"); | |
1671 | if (ret) | |
1672 | return ret; | |
1673 | ||
1674 | vdev->barmap[index] = pci_iomap(pdev, index, 0); | |
e19f32da AY |
1675 | if (!vdev->barmap[index]) { |
1676 | pci_release_selected_regions(pdev, 1 << index); | |
1677 | return -ENOMEM; | |
1678 | } | |
89e1f7d4 AW |
1679 | } |
1680 | ||
1681 | vma->vm_private_data = vdev; | |
89e1f7d4 | 1682 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); |
34002f54 | 1683 | vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff; |
89e1f7d4 | 1684 | |
11c4cd07 AW |
1685 | /* |
1686 | * See remap_pfn_range(), called from vfio_pci_fault() but we can't | |
1687 | * change vm_flags within the fault handler. Set them now. | |
1688 | */ | |
1689 | vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP; | |
1690 | vma->vm_ops = &vfio_pci_mmap_ops; | |
1691 | ||
1692 | return 0; | |
89e1f7d4 AW |
1693 | } |
1694 | ||
6df62c5b | 1695 | static void vfio_pci_request(struct vfio_device *core_vdev, unsigned int count) |
6140a8f5 | 1696 | { |
6df62c5b JG |
1697 | struct vfio_pci_device *vdev = |
1698 | container_of(core_vdev, struct vfio_pci_device, vdev); | |
a88a7b3e | 1699 | struct pci_dev *pdev = vdev->pdev; |
6140a8f5 AW |
1700 | |
1701 | mutex_lock(&vdev->igate); | |
1702 | ||
1703 | if (vdev->req_trigger) { | |
5f55d2ae | 1704 | if (!(count % 10)) |
a88a7b3e | 1705 | pci_notice_ratelimited(pdev, |
5f55d2ae AW |
1706 | "Relaying device request to user (#%u)\n", |
1707 | count); | |
6140a8f5 | 1708 | eventfd_signal(vdev->req_trigger, 1); |
5f55d2ae | 1709 | } else if (count == 0) { |
a88a7b3e | 1710 | pci_warn(pdev, |
5f55d2ae | 1711 | "No device request channel registered, blocked until released by user\n"); |
6140a8f5 AW |
1712 | } |
1713 | ||
1714 | mutex_unlock(&vdev->igate); | |
1715 | } | |
1716 | ||
cc20d799 AW |
1717 | static int vfio_pci_validate_vf_token(struct vfio_pci_device *vdev, |
1718 | bool vf_token, uuid_t *uuid) | |
1719 | { | |
1720 | /* | |
1721 | * There's always some degree of trust or collaboration between SR-IOV | |
1722 | * PF and VFs, even if just that the PF hosts the SR-IOV capability and | |
1723 | * can disrupt VFs with a reset, but often the PF has more explicit | |
1724 | * access to deny service to the VF or access data passed through the | |
1725 | * VF. We therefore require an opt-in via a shared VF token (UUID) to | |
1726 | * represent this trust. This both prevents that a VF driver might | |
1727 | * assume the PF driver is a trusted, in-kernel driver, and also that | |
1728 | * a PF driver might be replaced with a rogue driver, unknown to in-use | |
1729 | * VF drivers. | |
1730 | * | |
1731 | * Therefore when presented with a VF, if the PF is a vfio device and | |
1732 | * it is bound to the vfio-pci driver, the user needs to provide a VF | |
1733 | * token to access the device, in the form of appending a vf_token to | |
1734 | * the device name, for example: | |
1735 | * | |
1736 | * "0000:04:10.0 vf_token=bd8d9d2b-5a5f-4f5a-a211-f591514ba1f3" | |
1737 | * | |
1738 | * When presented with a PF which has VFs in use, the user must also | |
1739 | * provide the current VF token to prove collaboration with existing | |
1740 | * VF users. If VFs are not in use, the VF token provided for the PF | |
1741 | * device will act to set the VF token. | |
1742 | * | |
1743 | * If the VF token is provided but unused, an error is generated. | |
1744 | */ | |
1745 | if (!vdev->pdev->is_virtfn && !vdev->vf_token && !vf_token) | |
1746 | return 0; /* No VF token provided or required */ | |
1747 | ||
1748 | if (vdev->pdev->is_virtfn) { | |
07d47b42 | 1749 | struct vfio_pci_device *pf_vdev = get_pf_vdev(vdev); |
cc20d799 AW |
1750 | bool match; |
1751 | ||
1752 | if (!pf_vdev) { | |
1753 | if (!vf_token) | |
1754 | return 0; /* PF is not vfio-pci, no VF token */ | |
1755 | ||
1756 | pci_info_ratelimited(vdev->pdev, | |
1757 | "VF token incorrectly provided, PF not bound to vfio-pci\n"); | |
1758 | return -EINVAL; | |
1759 | } | |
1760 | ||
1761 | if (!vf_token) { | |
07d47b42 | 1762 | vfio_device_put(&pf_vdev->vdev); |
cc20d799 AW |
1763 | pci_info_ratelimited(vdev->pdev, |
1764 | "VF token required to access device\n"); | |
1765 | return -EACCES; | |
1766 | } | |
1767 | ||
1768 | mutex_lock(&pf_vdev->vf_token->lock); | |
1769 | match = uuid_equal(uuid, &pf_vdev->vf_token->uuid); | |
1770 | mutex_unlock(&pf_vdev->vf_token->lock); | |
1771 | ||
07d47b42 | 1772 | vfio_device_put(&pf_vdev->vdev); |
cc20d799 AW |
1773 | |
1774 | if (!match) { | |
1775 | pci_info_ratelimited(vdev->pdev, | |
1776 | "Incorrect VF token provided for device\n"); | |
1777 | return -EACCES; | |
1778 | } | |
1779 | } else if (vdev->vf_token) { | |
1780 | mutex_lock(&vdev->vf_token->lock); | |
1781 | if (vdev->vf_token->users) { | |
1782 | if (!vf_token) { | |
1783 | mutex_unlock(&vdev->vf_token->lock); | |
1784 | pci_info_ratelimited(vdev->pdev, | |
1785 | "VF token required to access device\n"); | |
1786 | return -EACCES; | |
1787 | } | |
1788 | ||
1789 | if (!uuid_equal(uuid, &vdev->vf_token->uuid)) { | |
1790 | mutex_unlock(&vdev->vf_token->lock); | |
1791 | pci_info_ratelimited(vdev->pdev, | |
1792 | "Incorrect VF token provided for device\n"); | |
1793 | return -EACCES; | |
1794 | } | |
1795 | } else if (vf_token) { | |
1796 | uuid_copy(&vdev->vf_token->uuid, uuid); | |
1797 | } | |
1798 | ||
1799 | mutex_unlock(&vdev->vf_token->lock); | |
1800 | } else if (vf_token) { | |
1801 | pci_info_ratelimited(vdev->pdev, | |
1802 | "VF token incorrectly provided, not a PF or VF\n"); | |
1803 | return -EINVAL; | |
1804 | } | |
1805 | ||
1806 | return 0; | |
1807 | } | |
1808 | ||
1809 | #define VF_TOKEN_ARG "vf_token=" | |
1810 | ||
6df62c5b | 1811 | static int vfio_pci_match(struct vfio_device *core_vdev, char *buf) |
467c084f | 1812 | { |
6df62c5b JG |
1813 | struct vfio_pci_device *vdev = |
1814 | container_of(core_vdev, struct vfio_pci_device, vdev); | |
cc20d799 AW |
1815 | bool vf_token = false; |
1816 | uuid_t uuid; | |
1817 | int ret; | |
1818 | ||
1819 | if (strncmp(pci_name(vdev->pdev), buf, strlen(pci_name(vdev->pdev)))) | |
1820 | return 0; /* No match */ | |
1821 | ||
1822 | if (strlen(buf) > strlen(pci_name(vdev->pdev))) { | |
1823 | buf += strlen(pci_name(vdev->pdev)); | |
1824 | ||
1825 | if (*buf != ' ') | |
1826 | return 0; /* No match: non-whitespace after name */ | |
1827 | ||
1828 | while (*buf) { | |
1829 | if (*buf == ' ') { | |
1830 | buf++; | |
1831 | continue; | |
1832 | } | |
1833 | ||
1834 | if (!vf_token && !strncmp(buf, VF_TOKEN_ARG, | |
1835 | strlen(VF_TOKEN_ARG))) { | |
1836 | buf += strlen(VF_TOKEN_ARG); | |
1837 | ||
1838 | if (strlen(buf) < UUID_STRING_LEN) | |
1839 | return -EINVAL; | |
1840 | ||
1841 | ret = uuid_parse(buf, &uuid); | |
1842 | if (ret) | |
1843 | return ret; | |
467c084f | 1844 | |
cc20d799 AW |
1845 | vf_token = true; |
1846 | buf += UUID_STRING_LEN; | |
1847 | } else { | |
1848 | /* Unknown/duplicate option */ | |
1849 | return -EINVAL; | |
1850 | } | |
1851 | } | |
1852 | } | |
1853 | ||
1854 | ret = vfio_pci_validate_vf_token(vdev, vf_token, &uuid); | |
1855 | if (ret) | |
1856 | return ret; | |
1857 | ||
1858 | return 1; /* Match */ | |
467c084f AW |
1859 | } |
1860 | ||
89e1f7d4 AW |
1861 | static const struct vfio_device_ops vfio_pci_ops = { |
1862 | .name = "vfio-pci", | |
1863 | .open = vfio_pci_open, | |
1864 | .release = vfio_pci_release, | |
1865 | .ioctl = vfio_pci_ioctl, | |
1866 | .read = vfio_pci_read, | |
1867 | .write = vfio_pci_write, | |
1868 | .mmap = vfio_pci_mmap, | |
6140a8f5 | 1869 | .request = vfio_pci_request, |
467c084f | 1870 | .match = vfio_pci_match, |
89e1f7d4 AW |
1871 | }; |
1872 | ||
e309df5b AW |
1873 | static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev); |
1874 | static void vfio_pci_reflck_put(struct vfio_pci_reflck *reflck); | |
137e5531 AW |
1875 | |
1876 | static int vfio_pci_bus_notifier(struct notifier_block *nb, | |
1877 | unsigned long action, void *data) | |
1878 | { | |
1879 | struct vfio_pci_device *vdev = container_of(nb, | |
1880 | struct vfio_pci_device, nb); | |
1881 | struct device *dev = data; | |
1882 | struct pci_dev *pdev = to_pci_dev(dev); | |
1883 | struct pci_dev *physfn = pci_physfn(pdev); | |
1884 | ||
1885 | if (action == BUS_NOTIFY_ADD_DEVICE && | |
1886 | pdev->is_virtfn && physfn == vdev->pdev) { | |
1887 | pci_info(vdev->pdev, "Captured SR-IOV VF %s driver_override\n", | |
1888 | pci_name(pdev)); | |
1889 | pdev->driver_override = kasprintf(GFP_KERNEL, "%s", | |
1890 | vfio_pci_ops.name); | |
1891 | } else if (action == BUS_NOTIFY_BOUND_DRIVER && | |
1892 | pdev->is_virtfn && physfn == vdev->pdev) { | |
1893 | struct pci_driver *drv = pci_dev_driver(pdev); | |
1894 | ||
1895 | if (drv && drv != &vfio_pci_driver) | |
1896 | pci_warn(vdev->pdev, | |
1897 | "VF %s bound to driver %s while PF bound to vfio-pci\n", | |
1898 | pci_name(pdev), drv->name); | |
1899 | } | |
1900 | ||
1901 | return 0; | |
1902 | } | |
e309df5b | 1903 | |
61e90817 JG |
1904 | static int vfio_pci_vf_init(struct vfio_pci_device *vdev) |
1905 | { | |
1906 | struct pci_dev *pdev = vdev->pdev; | |
1907 | int ret; | |
1908 | ||
1909 | if (!pdev->is_physfn) | |
1910 | return 0; | |
1911 | ||
1912 | vdev->vf_token = kzalloc(sizeof(*vdev->vf_token), GFP_KERNEL); | |
1913 | if (!vdev->vf_token) | |
1914 | return -ENOMEM; | |
1915 | ||
1916 | mutex_init(&vdev->vf_token->lock); | |
1917 | uuid_gen(&vdev->vf_token->uuid); | |
1918 | ||
1919 | vdev->nb.notifier_call = vfio_pci_bus_notifier; | |
1920 | ret = bus_register_notifier(&pci_bus_type, &vdev->nb); | |
1921 | if (ret) { | |
1922 | kfree(vdev->vf_token); | |
1923 | return ret; | |
1924 | } | |
1925 | return 0; | |
1926 | } | |
1927 | ||
1928 | static void vfio_pci_vf_uninit(struct vfio_pci_device *vdev) | |
1929 | { | |
1930 | if (!vdev->vf_token) | |
1931 | return; | |
1932 | ||
1933 | bus_unregister_notifier(&pci_bus_type, &vdev->nb); | |
1934 | WARN_ON(vdev->vf_token->users); | |
1935 | mutex_destroy(&vdev->vf_token->lock); | |
1936 | kfree(vdev->vf_token); | |
1937 | } | |
1938 | ||
1939 | static int vfio_pci_vga_init(struct vfio_pci_device *vdev) | |
1940 | { | |
1941 | struct pci_dev *pdev = vdev->pdev; | |
1942 | int ret; | |
1943 | ||
1944 | if (!vfio_pci_is_vga(pdev)) | |
1945 | return 0; | |
1946 | ||
1947 | ret = vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode); | |
1948 | if (ret) | |
1949 | return ret; | |
1950 | vga_set_legacy_decoding(pdev, vfio_pci_set_vga_decode(vdev, false)); | |
1951 | return 0; | |
1952 | } | |
1953 | ||
1954 | static void vfio_pci_vga_uninit(struct vfio_pci_device *vdev) | |
1955 | { | |
1956 | struct pci_dev *pdev = vdev->pdev; | |
1957 | ||
1958 | if (!vfio_pci_is_vga(pdev)) | |
1959 | return; | |
1960 | vga_client_register(pdev, NULL, NULL, NULL); | |
1961 | vga_set_legacy_decoding(pdev, VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM | | |
1962 | VGA_RSRC_LEGACY_IO | | |
1963 | VGA_RSRC_LEGACY_MEM); | |
1964 | } | |
1965 | ||
89e1f7d4 AW |
1966 | static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
1967 | { | |
89e1f7d4 AW |
1968 | struct vfio_pci_device *vdev; |
1969 | struct iommu_group *group; | |
1970 | int ret; | |
1971 | ||
1f97970e GC |
1972 | if (vfio_pci_is_denylisted(pdev)) |
1973 | return -EINVAL; | |
1974 | ||
7c2e211f | 1975 | if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL) |
89e1f7d4 AW |
1976 | return -EINVAL; |
1977 | ||
0dd0e297 | 1978 | /* |
137e5531 AW |
1979 | * Prevent binding to PFs with VFs enabled, the VFs might be in use |
1980 | * by the host or other users. We cannot capture the VFs if they | |
1981 | * already exist, nor can we track VF users. Disabling SR-IOV here | |
1982 | * would initiate removing the VFs, which would unbind the driver, | |
1983 | * which is prone to blocking if that VF is also in use by vfio-pci. | |
1984 | * Just reject these PFs and let the user sort it out. | |
0dd0e297 AW |
1985 | */ |
1986 | if (pci_num_vf(pdev)) { | |
1987 | pci_warn(pdev, "Cannot bind to PF with SR-IOV enabled\n"); | |
1988 | return -EBUSY; | |
1989 | } | |
1990 | ||
03a76b60 | 1991 | group = vfio_iommu_group_get(&pdev->dev); |
89e1f7d4 AW |
1992 | if (!group) |
1993 | return -EINVAL; | |
1994 | ||
1995 | vdev = kzalloc(sizeof(*vdev), GFP_KERNEL); | |
1996 | if (!vdev) { | |
b66574a3 AW |
1997 | ret = -ENOMEM; |
1998 | goto out_group_put; | |
89e1f7d4 AW |
1999 | } |
2000 | ||
1e04ec14 | 2001 | vfio_init_group_dev(&vdev->vdev, &pdev->dev, &vfio_pci_ops); |
89e1f7d4 AW |
2002 | vdev->pdev = pdev; |
2003 | vdev->irq_type = VFIO_PCI_NUM_IRQS; | |
2004 | mutex_init(&vdev->igate); | |
2005 | spin_lock_init(&vdev->irqlock); | |
30656177 | 2006 | mutex_init(&vdev->ioeventfds_lock); |
16b8fe4c | 2007 | INIT_LIST_HEAD(&vdev->dummy_resources_list); |
30656177 | 2008 | INIT_LIST_HEAD(&vdev->ioeventfds_list); |
11c4cd07 AW |
2009 | mutex_init(&vdev->vma_lock); |
2010 | INIT_LIST_HEAD(&vdev->vma_list); | |
abafbc55 | 2011 | init_rwsem(&vdev->memory_lock); |
89e1f7d4 | 2012 | |
e309df5b | 2013 | ret = vfio_pci_reflck_attach(vdev); |
b66574a3 AW |
2014 | if (ret) |
2015 | goto out_free; | |
61e90817 | 2016 | ret = vfio_pci_vf_init(vdev); |
b66574a3 | 2017 | if (ret) |
61e90817 JG |
2018 | goto out_reflck; |
2019 | ret = vfio_pci_vga_init(vdev); | |
2020 | if (ret) | |
2021 | goto out_vf; | |
ecaa1f6a | 2022 | |
51ef3a00 AW |
2023 | vfio_pci_probe_power_state(vdev); |
2024 | ||
6eb70187 AW |
2025 | if (!disable_idle_d3) { |
2026 | /* | |
2027 | * pci-core sets the device power state to an unknown value at | |
2028 | * bootup and after being removed from a driver. The only | |
2029 | * transition it allows from this unknown state is to D0, which | |
2030 | * typically happens when a driver calls pci_enable_device(). | |
2031 | * We're not ready to enable the device yet, but we do want to | |
2032 | * be able to get to D3. Therefore first do a D0 transition | |
2033 | * before going to D3. | |
2034 | */ | |
51ef3a00 AW |
2035 | vfio_pci_set_power_state(vdev, PCI_D0); |
2036 | vfio_pci_set_power_state(vdev, PCI_D3hot); | |
6eb70187 AW |
2037 | } |
2038 | ||
6b018e20 | 2039 | ret = vfio_register_group_dev(&vdev->vdev); |
4aeec398 JG |
2040 | if (ret) |
2041 | goto out_power; | |
6b018e20 | 2042 | dev_set_drvdata(&pdev->dev, vdev); |
4aeec398 | 2043 | return 0; |
b66574a3 | 2044 | |
4aeec398 JG |
2045 | out_power: |
2046 | if (!disable_idle_d3) | |
2047 | vfio_pci_set_power_state(vdev, PCI_D0); | |
61e90817 JG |
2048 | out_vf: |
2049 | vfio_pci_vf_uninit(vdev); | |
b66574a3 AW |
2050 | out_reflck: |
2051 | vfio_pci_reflck_put(vdev->reflck); | |
b66574a3 | 2052 | out_free: |
4aeec398 | 2053 | kfree(vdev->pm_save); |
b66574a3 AW |
2054 | kfree(vdev); |
2055 | out_group_put: | |
2056 | vfio_iommu_group_put(group, &pdev->dev); | |
2057 | return ret; | |
89e1f7d4 AW |
2058 | } |
2059 | ||
2060 | static void vfio_pci_remove(struct pci_dev *pdev) | |
2061 | { | |
6b018e20 | 2062 | struct vfio_pci_device *vdev = dev_get_drvdata(&pdev->dev); |
89e1f7d4 | 2063 | |
137e5531 AW |
2064 | pci_disable_sriov(pdev); |
2065 | ||
6b018e20 | 2066 | vfio_unregister_group_dev(&vdev->vdev); |
137e5531 | 2067 | |
61e90817 | 2068 | vfio_pci_vf_uninit(vdev); |
e309df5b | 2069 | vfio_pci_reflck_put(vdev->reflck); |
61e90817 | 2070 | vfio_pci_vga_uninit(vdev); |
e309df5b | 2071 | |
03a76b60 | 2072 | vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev); |
51ef3a00 AW |
2073 | |
2074 | if (!disable_idle_d3) | |
2075 | vfio_pci_set_power_state(vdev, PCI_D0); | |
2076 | ||
61e90817 JG |
2077 | mutex_destroy(&vdev->ioeventfds_lock); |
2078 | kfree(vdev->region); | |
51ef3a00 | 2079 | kfree(vdev->pm_save); |
ecaa1f6a | 2080 | kfree(vdev); |
89e1f7d4 AW |
2081 | } |
2082 | ||
dad9f897 VMP |
2083 | static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev, |
2084 | pci_channel_state_t state) | |
2085 | { | |
2086 | struct vfio_pci_device *vdev; | |
2087 | struct vfio_device *device; | |
2088 | ||
2089 | device = vfio_device_get_from_dev(&pdev->dev); | |
2090 | if (device == NULL) | |
2091 | return PCI_ERS_RESULT_DISCONNECT; | |
2092 | ||
07d47b42 | 2093 | vdev = container_of(device, struct vfio_pci_device, vdev); |
dad9f897 | 2094 | |
3be3a074 AW |
2095 | mutex_lock(&vdev->igate); |
2096 | ||
dad9f897 VMP |
2097 | if (vdev->err_trigger) |
2098 | eventfd_signal(vdev->err_trigger, 1); | |
2099 | ||
3be3a074 AW |
2100 | mutex_unlock(&vdev->igate); |
2101 | ||
dad9f897 VMP |
2102 | vfio_device_put(device); |
2103 | ||
2104 | return PCI_ERS_RESULT_CAN_RECOVER; | |
2105 | } | |
2106 | ||
137e5531 AW |
2107 | static int vfio_pci_sriov_configure(struct pci_dev *pdev, int nr_virtfn) |
2108 | { | |
137e5531 AW |
2109 | struct vfio_device *device; |
2110 | int ret = 0; | |
2111 | ||
2112 | might_sleep(); | |
2113 | ||
2114 | if (!enable_sriov) | |
2115 | return -ENOENT; | |
2116 | ||
2117 | device = vfio_device_get_from_dev(&pdev->dev); | |
2118 | if (!device) | |
2119 | return -ENODEV; | |
2120 | ||
137e5531 AW |
2121 | if (nr_virtfn == 0) |
2122 | pci_disable_sriov(pdev); | |
2123 | else | |
2124 | ret = pci_enable_sriov(pdev, nr_virtfn); | |
2125 | ||
2126 | vfio_device_put(device); | |
2127 | ||
2128 | return ret < 0 ? ret : nr_virtfn; | |
2129 | } | |
2130 | ||
7d10f4e0 | 2131 | static const struct pci_error_handlers vfio_err_handlers = { |
dad9f897 VMP |
2132 | .error_detected = vfio_pci_aer_err_detected, |
2133 | }; | |
2134 | ||
89e1f7d4 | 2135 | static struct pci_driver vfio_pci_driver = { |
137e5531 AW |
2136 | .name = "vfio-pci", |
2137 | .id_table = NULL, /* only dynamic ids */ | |
2138 | .probe = vfio_pci_probe, | |
2139 | .remove = vfio_pci_remove, | |
2140 | .sriov_configure = vfio_pci_sriov_configure, | |
2141 | .err_handler = &vfio_err_handlers, | |
89e1f7d4 AW |
2142 | }; |
2143 | ||
e309df5b AW |
2144 | static DEFINE_MUTEX(reflck_lock); |
2145 | ||
2146 | static struct vfio_pci_reflck *vfio_pci_reflck_alloc(void) | |
2147 | { | |
2148 | struct vfio_pci_reflck *reflck; | |
2149 | ||
2150 | reflck = kzalloc(sizeof(*reflck), GFP_KERNEL); | |
2151 | if (!reflck) | |
2152 | return ERR_PTR(-ENOMEM); | |
2153 | ||
2154 | kref_init(&reflck->kref); | |
2155 | mutex_init(&reflck->lock); | |
2156 | ||
2157 | return reflck; | |
2158 | } | |
2159 | ||
2160 | static void vfio_pci_reflck_get(struct vfio_pci_reflck *reflck) | |
2161 | { | |
2162 | kref_get(&reflck->kref); | |
2163 | } | |
2164 | ||
2165 | static int vfio_pci_reflck_find(struct pci_dev *pdev, void *data) | |
2166 | { | |
2167 | struct vfio_pci_reflck **preflck = data; | |
2168 | struct vfio_device *device; | |
2169 | struct vfio_pci_device *vdev; | |
2170 | ||
2171 | device = vfio_device_get_from_dev(&pdev->dev); | |
2172 | if (!device) | |
2173 | return 0; | |
2174 | ||
2175 | if (pci_dev_driver(pdev) != &vfio_pci_driver) { | |
2176 | vfio_device_put(device); | |
2177 | return 0; | |
2178 | } | |
2179 | ||
07d47b42 | 2180 | vdev = container_of(device, struct vfio_pci_device, vdev); |
e309df5b AW |
2181 | |
2182 | if (vdev->reflck) { | |
2183 | vfio_pci_reflck_get(vdev->reflck); | |
2184 | *preflck = vdev->reflck; | |
2185 | vfio_device_put(device); | |
2186 | return 1; | |
2187 | } | |
2188 | ||
2189 | vfio_device_put(device); | |
2190 | return 0; | |
2191 | } | |
2192 | ||
2193 | static int vfio_pci_reflck_attach(struct vfio_pci_device *vdev) | |
2194 | { | |
2195 | bool slot = !pci_probe_reset_slot(vdev->pdev->slot); | |
2196 | ||
2197 | mutex_lock(&reflck_lock); | |
2198 | ||
2199 | if (pci_is_root_bus(vdev->pdev->bus) || | |
2200 | vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_reflck_find, | |
2201 | &vdev->reflck, slot) <= 0) | |
2202 | vdev->reflck = vfio_pci_reflck_alloc(); | |
2203 | ||
2204 | mutex_unlock(&reflck_lock); | |
2205 | ||
2206 | return PTR_ERR_OR_ZERO(vdev->reflck); | |
2207 | } | |
2208 | ||
2209 | static void vfio_pci_reflck_release(struct kref *kref) | |
2210 | { | |
2211 | struct vfio_pci_reflck *reflck = container_of(kref, | |
2212 | struct vfio_pci_reflck, | |
2213 | kref); | |
2214 | ||
2215 | kfree(reflck); | |
2216 | mutex_unlock(&reflck_lock); | |
2217 | } | |
2218 | ||
2219 | static void vfio_pci_reflck_put(struct vfio_pci_reflck *reflck) | |
2220 | { | |
2221 | kref_put_mutex(&reflck->kref, vfio_pci_reflck_release, &reflck_lock); | |
2222 | } | |
2223 | ||
e309df5b | 2224 | static int vfio_pci_get_unused_devs(struct pci_dev *pdev, void *data) |
bc4fba77 | 2225 | { |
93899a67 | 2226 | struct vfio_devices *devs = data; |
20f30017 | 2227 | struct vfio_device *device; |
e309df5b | 2228 | struct vfio_pci_device *vdev; |
bc4fba77 | 2229 | |
93899a67 AW |
2230 | if (devs->cur_index == devs->max_index) |
2231 | return -ENOSPC; | |
bc4fba77 | 2232 | |
20f30017 AW |
2233 | device = vfio_device_get_from_dev(&pdev->dev); |
2234 | if (!device) | |
93899a67 | 2235 | return -EINVAL; |
bc4fba77 | 2236 | |
20f30017 AW |
2237 | if (pci_dev_driver(pdev) != &vfio_pci_driver) { |
2238 | vfio_device_put(device); | |
2239 | return -EBUSY; | |
2240 | } | |
2241 | ||
07d47b42 | 2242 | vdev = container_of(device, struct vfio_pci_device, vdev); |
e309df5b AW |
2243 | |
2244 | /* Fault if the device is not unused */ | |
2245 | if (vdev->refcnt) { | |
2246 | vfio_device_put(device); | |
2247 | return -EBUSY; | |
2248 | } | |
2249 | ||
07d47b42 | 2250 | devs->devices[devs->cur_index++] = vdev; |
bc4fba77 AW |
2251 | return 0; |
2252 | } | |
2253 | ||
abafbc55 AW |
2254 | static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data) |
2255 | { | |
2256 | struct vfio_devices *devs = data; | |
2257 | struct vfio_device *device; | |
2258 | struct vfio_pci_device *vdev; | |
2259 | ||
2260 | if (devs->cur_index == devs->max_index) | |
2261 | return -ENOSPC; | |
2262 | ||
2263 | device = vfio_device_get_from_dev(&pdev->dev); | |
2264 | if (!device) | |
2265 | return -EINVAL; | |
2266 | ||
2267 | if (pci_dev_driver(pdev) != &vfio_pci_driver) { | |
2268 | vfio_device_put(device); | |
2269 | return -EBUSY; | |
2270 | } | |
2271 | ||
07d47b42 | 2272 | vdev = container_of(device, struct vfio_pci_device, vdev); |
abafbc55 AW |
2273 | |
2274 | /* | |
2275 | * Locking multiple devices is prone to deadlock, runaway and | |
2276 | * unwind if we hit contention. | |
2277 | */ | |
2278 | if (!vfio_pci_zap_and_vma_lock(vdev, true)) { | |
2279 | vfio_device_put(device); | |
2280 | return -EBUSY; | |
2281 | } | |
2282 | ||
07d47b42 | 2283 | devs->devices[devs->cur_index++] = vdev; |
abafbc55 AW |
2284 | return 0; |
2285 | } | |
2286 | ||
bc4fba77 | 2287 | /* |
e309df5b AW |
2288 | * If a bus or slot reset is available for the provided device and: |
2289 | * - All of the devices affected by that bus or slot reset are unused | |
2290 | * (!refcnt) | |
2291 | * - At least one of the affected devices is marked dirty via | |
2292 | * needs_reset (such as by lack of FLR support) | |
2293 | * Then attempt to perform that bus or slot reset. Callers are required | |
2294 | * to hold vdev->reflck->lock, protecting the bus/slot reset group from | |
2295 | * concurrent opens. A vfio_device reference is acquired for each device | |
2296 | * to prevent unbinds during the reset operation. | |
93899a67 AW |
2297 | * |
2298 | * NB: vfio-core considers a group to be viable even if some devices are | |
2299 | * bound to drivers like pci-stub or pcieport. Here we require all devices | |
2300 | * to be bound to vfio_pci since that's the only way we can be sure they | |
2301 | * stay put. | |
bc4fba77 AW |
2302 | */ |
2303 | static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev) | |
2304 | { | |
93899a67 AW |
2305 | struct vfio_devices devs = { .cur_index = 0 }; |
2306 | int i = 0, ret = -EINVAL; | |
e309df5b | 2307 | bool slot = false; |
93899a67 | 2308 | struct vfio_pci_device *tmp; |
bc4fba77 AW |
2309 | |
2310 | if (!pci_probe_reset_slot(vdev->pdev->slot)) | |
2311 | slot = true; | |
2312 | else if (pci_probe_reset_bus(vdev->pdev->bus)) | |
2313 | return; | |
2314 | ||
93899a67 AW |
2315 | if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs, |
2316 | &i, slot) || !i) | |
bc4fba77 AW |
2317 | return; |
2318 | ||
93899a67 AW |
2319 | devs.max_index = i; |
2320 | devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL); | |
2321 | if (!devs.devices) | |
bc4fba77 AW |
2322 | return; |
2323 | ||
93899a67 | 2324 | if (vfio_pci_for_each_slot_or_bus(vdev->pdev, |
e309df5b AW |
2325 | vfio_pci_get_unused_devs, |
2326 | &devs, slot)) | |
93899a67 AW |
2327 | goto put_devs; |
2328 | ||
e309df5b | 2329 | /* Does at least one need a reset? */ |
93899a67 | 2330 | for (i = 0; i < devs.cur_index; i++) { |
07d47b42 | 2331 | tmp = devs.devices[i]; |
e309df5b AW |
2332 | if (tmp->needs_reset) { |
2333 | ret = pci_reset_bus(vdev->pdev); | |
2334 | break; | |
2335 | } | |
93899a67 AW |
2336 | } |
2337 | ||
93899a67 AW |
2338 | put_devs: |
2339 | for (i = 0; i < devs.cur_index; i++) { | |
07d47b42 | 2340 | tmp = devs.devices[i]; |
e309df5b AW |
2341 | |
2342 | /* | |
2343 | * If reset was successful, affected devices no longer need | |
2344 | * a reset and we should return all the collateral devices | |
2345 | * to low power. If not successful, we either didn't reset | |
2346 | * the bus or timed out waiting for it, so let's not touch | |
2347 | * the power state. | |
2348 | */ | |
2349 | if (!ret) { | |
93899a67 | 2350 | tmp->needs_reset = false; |
6eb70187 | 2351 | |
e309df5b | 2352 | if (tmp != vdev && !disable_idle_d3) |
51ef3a00 | 2353 | vfio_pci_set_power_state(tmp, PCI_D3hot); |
e309df5b | 2354 | } |
6eb70187 | 2355 | |
07d47b42 | 2356 | vfio_device_put(&tmp->vdev); |
93899a67 AW |
2357 | } |
2358 | ||
2359 | kfree(devs.devices); | |
bc4fba77 AW |
2360 | } |
2361 | ||
89e1f7d4 AW |
2362 | static void __exit vfio_pci_cleanup(void) |
2363 | { | |
2364 | pci_unregister_driver(&vfio_pci_driver); | |
89e1f7d4 AW |
2365 | vfio_pci_uninit_perm_bits(); |
2366 | } | |
2367 | ||
80c7e8cc AW |
2368 | static void __init vfio_pci_fill_ids(void) |
2369 | { | |
2370 | char *p, *id; | |
2371 | int rc; | |
2372 | ||
2373 | /* no ids passed actually */ | |
2374 | if (ids[0] == '\0') | |
2375 | return; | |
2376 | ||
2377 | /* add ids specified in the module parameter */ | |
2378 | p = ids; | |
2379 | while ((id = strsep(&p, ","))) { | |
2380 | unsigned int vendor, device, subvendor = PCI_ANY_ID, | |
2381 | subdevice = PCI_ANY_ID, class = 0, class_mask = 0; | |
2382 | int fields; | |
2383 | ||
2384 | if (!strlen(id)) | |
2385 | continue; | |
2386 | ||
2387 | fields = sscanf(id, "%x:%x:%x:%x:%x:%x", | |
2388 | &vendor, &device, &subvendor, &subdevice, | |
2389 | &class, &class_mask); | |
2390 | ||
2391 | if (fields < 2) { | |
2392 | pr_warn("invalid id string \"%s\"\n", id); | |
2393 | continue; | |
2394 | } | |
2395 | ||
2396 | rc = pci_add_dynid(&vfio_pci_driver, vendor, device, | |
2397 | subvendor, subdevice, class, class_mask, 0); | |
2398 | if (rc) | |
426b046b | 2399 | pr_warn("failed to add dynamic id [%04x:%04x[%04x:%04x]] class %#08x/%08x (%d)\n", |
80c7e8cc AW |
2400 | vendor, device, subvendor, subdevice, |
2401 | class, class_mask, rc); | |
2402 | else | |
426b046b | 2403 | pr_info("add [%04x:%04x[%04x:%04x]] class %#08x/%08x\n", |
80c7e8cc AW |
2404 | vendor, device, subvendor, subdevice, |
2405 | class, class_mask); | |
2406 | } | |
2407 | } | |
2408 | ||
89e1f7d4 AW |
2409 | static int __init vfio_pci_init(void) |
2410 | { | |
2411 | int ret; | |
2412 | ||
fbc9d371 | 2413 | /* Allocate shared config space permission data used by all devices */ |
89e1f7d4 AW |
2414 | ret = vfio_pci_init_perm_bits(); |
2415 | if (ret) | |
2416 | return ret; | |
2417 | ||
89e1f7d4 AW |
2418 | /* Register and scan for devices */ |
2419 | ret = pci_register_driver(&vfio_pci_driver); | |
2420 | if (ret) | |
2421 | goto out_driver; | |
2422 | ||
80c7e8cc AW |
2423 | vfio_pci_fill_ids(); |
2424 | ||
1f97970e GC |
2425 | if (disable_denylist) |
2426 | pr_warn("device denylist disabled.\n"); | |
2427 | ||
89e1f7d4 AW |
2428 | return 0; |
2429 | ||
89e1f7d4 AW |
2430 | out_driver: |
2431 | vfio_pci_uninit_perm_bits(); | |
2432 | return ret; | |
2433 | } | |
2434 | ||
2435 | module_init(vfio_pci_init); | |
2436 | module_exit(vfio_pci_cleanup); | |
2437 | ||
2438 | MODULE_VERSION(DRIVER_VERSION); | |
2439 | MODULE_LICENSE("GPL v2"); | |
2440 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
2441 | MODULE_DESCRIPTION(DRIVER_DESC); |