GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / vfio / pci / vfio_pci.c
1 /*
2  * Copyright (C) 2012 Red Hat, Inc.  All rights reserved.
3  *     Author: Alex Williamson <alex.williamson@redhat.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Derived from original vfio:
10  * Copyright 2010 Cisco Systems, Inc.  All rights reserved.
11  * Author: Tom Lyon, pugs@cisco.com
12  */
13
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16 #include <linux/device.h>
17 #include <linux/eventfd.h>
18 #include <linux/file.h>
19 #include <linux/interrupt.h>
20 #include <linux/iommu.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/notifier.h>
24 #include <linux/pci.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/slab.h>
27 #include <linux/types.h>
28 #include <linux/uaccess.h>
29 #include <linux/vfio.h>
30 #include <linux/vgaarb.h>
31 #include <linux/nospec.h>
32 #include <linux/sched/mm.h>
33
34 #include "vfio_pci_private.h"
35
36 #define DRIVER_VERSION  "0.2"
37 #define DRIVER_AUTHOR   "Alex Williamson <alex.williamson@redhat.com>"
38 #define DRIVER_DESC     "VFIO PCI - User Level meta-driver"
39
40 static char ids[1024] __initdata;
41 module_param_string(ids, ids, sizeof(ids), 0);
42 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");
43
44 static bool nointxmask;
45 module_param_named(nointxmask, nointxmask, bool, S_IRUGO | S_IWUSR);
46 MODULE_PARM_DESC(nointxmask,
47                   "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.");
48
49 #ifdef CONFIG_VFIO_PCI_VGA
50 static bool disable_vga;
51 module_param(disable_vga, bool, S_IRUGO);
52 MODULE_PARM_DESC(disable_vga, "Disable VGA resource access through vfio-pci");
53 #endif
54
55 static bool disable_idle_d3;
56 module_param(disable_idle_d3, bool, S_IRUGO | S_IWUSR);
57 MODULE_PARM_DESC(disable_idle_d3,
58                  "Disable using the PCI D3 low power state for idle, unused devices");
59
60 static DEFINE_MUTEX(driver_lock);
61
62 static inline bool vfio_vga_disabled(void)
63 {
64 #ifdef CONFIG_VFIO_PCI_VGA
65         return disable_vga;
66 #else
67         return true;
68 #endif
69 }
70
71 /*
72  * Our VGA arbiter participation is limited since we don't know anything
73  * about the device itself.  However, if the device is the only VGA device
74  * downstream of a bridge and VFIO VGA support is disabled, then we can
75  * safely return legacy VGA IO and memory as not decoded since the user
76  * has no way to get to it and routing can be disabled externally at the
77  * bridge.
78  */
79 static unsigned int vfio_pci_set_vga_decode(void *opaque, bool single_vga)
80 {
81         struct vfio_pci_device *vdev = opaque;
82         struct pci_dev *tmp = NULL, *pdev = vdev->pdev;
83         unsigned char max_busnr;
84         unsigned int decodes;
85
86         if (single_vga || !vfio_vga_disabled() || pci_is_root_bus(pdev->bus))
87                 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
88                        VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
89
90         max_busnr = pci_bus_max_busnr(pdev->bus);
91         decodes = VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
92
93         while ((tmp = pci_get_class(PCI_CLASS_DISPLAY_VGA << 8, tmp)) != NULL) {
94                 if (tmp == pdev ||
95                     pci_domain_nr(tmp->bus) != pci_domain_nr(pdev->bus) ||
96                     pci_is_root_bus(tmp->bus))
97                         continue;
98
99                 if (tmp->bus->number >= pdev->bus->number &&
100                     tmp->bus->number <= max_busnr) {
101                         pci_dev_put(tmp);
102                         decodes |= VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM;
103                         break;
104                 }
105         }
106
107         return decodes;
108 }
109
110 static inline bool vfio_pci_is_vga(struct pci_dev *pdev)
111 {
112         return (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
113 }
114
115 static void vfio_pci_probe_mmaps(struct vfio_pci_device *vdev)
116 {
117         struct resource *res;
118         int bar;
119         struct vfio_pci_dummy_resource *dummy_res;
120
121         for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
122                 res = vdev->pdev->resource + bar;
123
124                 if (!IS_ENABLED(CONFIG_VFIO_PCI_MMAP))
125                         goto no_mmap;
126
127                 if (!(res->flags & IORESOURCE_MEM))
128                         goto no_mmap;
129
130                 /*
131                  * The PCI core shouldn't set up a resource with a
132                  * type but zero size. But there may be bugs that
133                  * cause us to do that.
134                  */
135                 if (!resource_size(res))
136                         goto no_mmap;
137
138                 if (resource_size(res) >= PAGE_SIZE) {
139                         vdev->bar_mmap_supported[bar] = true;
140                         continue;
141                 }
142
143                 if (!(res->start & ~PAGE_MASK)) {
144                         /*
145                          * Add a dummy resource to reserve the remainder
146                          * of the exclusive page in case that hot-add
147                          * device's bar is assigned into it.
148                          */
149                         dummy_res = kzalloc(sizeof(*dummy_res), GFP_KERNEL);
150                         if (dummy_res == NULL)
151                                 goto no_mmap;
152
153                         dummy_res->resource.name = "vfio sub-page reserved";
154                         dummy_res->resource.start = res->end + 1;
155                         dummy_res->resource.end = res->start + PAGE_SIZE - 1;
156                         dummy_res->resource.flags = res->flags;
157                         if (request_resource(res->parent,
158                                                 &dummy_res->resource)) {
159                                 kfree(dummy_res);
160                                 goto no_mmap;
161                         }
162                         dummy_res->index = bar;
163                         list_add(&dummy_res->res_next,
164                                         &vdev->dummy_resources_list);
165                         vdev->bar_mmap_supported[bar] = true;
166                         continue;
167                 }
168                 /*
169                  * Here we don't handle the case when the BAR is not page
170                  * aligned because we can't expect the BAR will be
171                  * assigned into the same location in a page in guest
172                  * when we passthrough the BAR. And it's hard to access
173                  * this BAR in userspace because we have no way to get
174                  * the BAR's location in a page.
175                  */
176 no_mmap:
177                 vdev->bar_mmap_supported[bar] = false;
178         }
179 }
180
181 static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev);
182 static void vfio_pci_disable(struct vfio_pci_device *vdev);
183 static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data);
184
185 /*
186  * INTx masking requires the ability to disable INTx signaling via PCI_COMMAND
187  * _and_ the ability detect when the device is asserting INTx via PCI_STATUS.
188  * If a device implements the former but not the latter we would typically
189  * expect broken_intx_masking be set and require an exclusive interrupt.
190  * However since we do have control of the device's ability to assert INTx,
191  * we can instead pretend that the device does not implement INTx, virtualizing
192  * the pin register to report zero and maintaining DisINTx set on the host.
193  */
194 static bool vfio_pci_nointx(struct pci_dev *pdev)
195 {
196         switch (pdev->vendor) {
197         case PCI_VENDOR_ID_INTEL:
198                 switch (pdev->device) {
199                 /* All i40e (XL710/X710/XXV710) 10/20/25/40GbE NICs */
200                 case 0x1572:
201                 case 0x1574:
202                 case 0x1580 ... 0x1581:
203                 case 0x1583 ... 0x158b:
204                 case 0x37d0 ... 0x37d2:
205                         return true;
206                 default:
207                         return false;
208                 }
209         }
210
211         return false;
212 }
213
214 static int vfio_pci_enable(struct vfio_pci_device *vdev)
215 {
216         struct pci_dev *pdev = vdev->pdev;
217         int ret;
218         u16 cmd;
219         u8 msix_pos;
220
221         pci_set_power_state(pdev, PCI_D0);
222
223         /* Don't allow our initial saved state to include busmaster */
224         pci_clear_master(pdev);
225
226         ret = pci_enable_device(pdev);
227         if (ret)
228                 return ret;
229
230         /* If reset fails because of the device lock, fail this path entirely */
231         ret = pci_try_reset_function(pdev);
232         if (ret == -EAGAIN) {
233                 pci_disable_device(pdev);
234                 return ret;
235         }
236
237         vdev->reset_works = !ret;
238         pci_save_state(pdev);
239         vdev->pci_saved_state = pci_store_saved_state(pdev);
240         if (!vdev->pci_saved_state)
241                 pr_debug("%s: Couldn't store %s saved state\n",
242                          __func__, dev_name(&pdev->dev));
243
244         if (likely(!nointxmask)) {
245                 if (vfio_pci_nointx(pdev)) {
246                         dev_info(&pdev->dev, "Masking broken INTx support\n");
247                         vdev->nointx = true;
248                         pci_intx(pdev, 0);
249                 } else
250                         vdev->pci_2_3 = pci_intx_mask_supported(pdev);
251         }
252
253         pci_read_config_word(pdev, PCI_COMMAND, &cmd);
254         if (vdev->pci_2_3 && (cmd & PCI_COMMAND_INTX_DISABLE)) {
255                 cmd &= ~PCI_COMMAND_INTX_DISABLE;
256                 pci_write_config_word(pdev, PCI_COMMAND, cmd);
257         }
258
259         ret = vfio_config_init(vdev);
260         if (ret) {
261                 kfree(vdev->pci_saved_state);
262                 vdev->pci_saved_state = NULL;
263                 pci_disable_device(pdev);
264                 return ret;
265         }
266
267         msix_pos = pdev->msix_cap;
268         if (msix_pos) {
269                 u16 flags;
270                 u32 table;
271
272                 pci_read_config_word(pdev, msix_pos + PCI_MSIX_FLAGS, &flags);
273                 pci_read_config_dword(pdev, msix_pos + PCI_MSIX_TABLE, &table);
274
275                 vdev->msix_bar = table & PCI_MSIX_TABLE_BIR;
276                 vdev->msix_offset = table & PCI_MSIX_TABLE_OFFSET;
277                 vdev->msix_size = ((flags & PCI_MSIX_FLAGS_QSIZE) + 1) * 16;
278         } else
279                 vdev->msix_bar = 0xFF;
280
281         if (!vfio_vga_disabled() && vfio_pci_is_vga(pdev))
282                 vdev->has_vga = true;
283
284
285         if (vfio_pci_is_vga(pdev) &&
286             pdev->vendor == PCI_VENDOR_ID_INTEL &&
287             IS_ENABLED(CONFIG_VFIO_PCI_IGD)) {
288                 ret = vfio_pci_igd_init(vdev);
289                 if (ret) {
290                         dev_warn(&vdev->pdev->dev,
291                                  "Failed to setup Intel IGD regions\n");
292                         vfio_pci_disable(vdev);
293                         return ret;
294                 }
295         }
296
297         vfio_pci_probe_mmaps(vdev);
298
299         return 0;
300 }
301
302 static void vfio_pci_disable(struct vfio_pci_device *vdev)
303 {
304         struct pci_dev *pdev = vdev->pdev;
305         struct vfio_pci_dummy_resource *dummy_res, *tmp;
306         int i, bar;
307
308         /* Stop the device from further DMA */
309         pci_clear_master(pdev);
310
311         vfio_pci_set_irqs_ioctl(vdev, VFIO_IRQ_SET_DATA_NONE |
312                                 VFIO_IRQ_SET_ACTION_TRIGGER,
313                                 vdev->irq_type, 0, 0, NULL);
314
315         vdev->virq_disabled = false;
316
317         for (i = 0; i < vdev->num_regions; i++)
318                 vdev->region[i].ops->release(vdev, &vdev->region[i]);
319
320         vdev->num_regions = 0;
321         kfree(vdev->region);
322         vdev->region = NULL; /* don't krealloc a freed pointer */
323
324         vfio_config_free(vdev);
325
326         for (bar = PCI_STD_RESOURCES; bar <= PCI_STD_RESOURCE_END; bar++) {
327                 if (!vdev->barmap[bar])
328                         continue;
329                 pci_iounmap(pdev, vdev->barmap[bar]);
330                 pci_release_selected_regions(pdev, 1 << bar);
331                 vdev->barmap[bar] = NULL;
332         }
333
334         list_for_each_entry_safe(dummy_res, tmp,
335                                  &vdev->dummy_resources_list, res_next) {
336                 list_del(&dummy_res->res_next);
337                 release_resource(&dummy_res->resource);
338                 kfree(dummy_res);
339         }
340
341         vdev->needs_reset = true;
342
343         /*
344          * If we have saved state, restore it.  If we can reset the device,
345          * even better.  Resetting with current state seems better than
346          * nothing, but saving and restoring current state without reset
347          * is just busy work.
348          */
349         if (pci_load_and_free_saved_state(pdev, &vdev->pci_saved_state)) {
350                 pr_info("%s: Couldn't reload %s saved state\n",
351                         __func__, dev_name(&pdev->dev));
352
353                 if (!vdev->reset_works)
354                         goto out;
355
356                 pci_save_state(pdev);
357         }
358
359         /*
360          * Disable INTx and MSI, presumably to avoid spurious interrupts
361          * during reset.  Stolen from pci_reset_function()
362          */
363         pci_write_config_word(pdev, PCI_COMMAND, PCI_COMMAND_INTX_DISABLE);
364
365         /*
366          * Try to get the locks ourselves to prevent a deadlock. The
367          * success of this is dependent on being able to lock the device,
368          * which is not always possible.
369          * We can not use the "try" reset interface here, which will
370          * overwrite the previously restored configuration information.
371          */
372         if (vdev->reset_works && pci_cfg_access_trylock(pdev)) {
373                 if (device_trylock(&pdev->dev)) {
374                         if (!__pci_reset_function_locked(pdev))
375                                 vdev->needs_reset = false;
376                         device_unlock(&pdev->dev);
377                 }
378                 pci_cfg_access_unlock(pdev);
379         }
380
381         pci_restore_state(pdev);
382 out:
383         pci_disable_device(pdev);
384
385         vfio_pci_try_bus_reset(vdev);
386
387         if (!disable_idle_d3)
388                 pci_set_power_state(pdev, PCI_D3hot);
389 }
390
391 static void vfio_pci_release(void *device_data)
392 {
393         struct vfio_pci_device *vdev = device_data;
394
395         mutex_lock(&driver_lock);
396
397         if (!(--vdev->refcnt)) {
398                 vfio_spapr_pci_eeh_release(vdev->pdev);
399                 vfio_pci_disable(vdev);
400                 mutex_lock(&vdev->igate);
401                 if (vdev->err_trigger) {
402                         eventfd_ctx_put(vdev->err_trigger);
403                         vdev->err_trigger = NULL;
404                 }
405                 mutex_unlock(&vdev->igate);
406
407                 mutex_lock(&vdev->igate);
408                 if (vdev->req_trigger) {
409                         eventfd_ctx_put(vdev->req_trigger);
410                         vdev->req_trigger = NULL;
411                 }
412                 mutex_unlock(&vdev->igate);
413         }
414
415         mutex_unlock(&driver_lock);
416
417         module_put(THIS_MODULE);
418 }
419
420 static int vfio_pci_open(void *device_data)
421 {
422         struct vfio_pci_device *vdev = device_data;
423         int ret = 0;
424
425         if (!try_module_get(THIS_MODULE))
426                 return -ENODEV;
427
428         mutex_lock(&driver_lock);
429
430         if (!vdev->refcnt) {
431                 ret = vfio_pci_enable(vdev);
432                 if (ret)
433                         goto error;
434
435                 vfio_spapr_pci_eeh_open(vdev->pdev);
436         }
437         vdev->refcnt++;
438 error:
439         mutex_unlock(&driver_lock);
440         if (ret)
441                 module_put(THIS_MODULE);
442         return ret;
443 }
444
445 static int vfio_pci_get_irq_count(struct vfio_pci_device *vdev, int irq_type)
446 {
447         if (irq_type == VFIO_PCI_INTX_IRQ_INDEX) {
448                 u8 pin;
449
450                 if (!IS_ENABLED(CONFIG_VFIO_PCI_INTX) ||
451                     vdev->nointx || vdev->pdev->is_virtfn)
452                         return 0;
453
454                 pci_read_config_byte(vdev->pdev, PCI_INTERRUPT_PIN, &pin);
455
456                 return pin ? 1 : 0;
457         } else if (irq_type == VFIO_PCI_MSI_IRQ_INDEX) {
458                 u8 pos;
459                 u16 flags;
460
461                 pos = vdev->pdev->msi_cap;
462                 if (pos) {
463                         pci_read_config_word(vdev->pdev,
464                                              pos + PCI_MSI_FLAGS, &flags);
465                         return 1 << ((flags & PCI_MSI_FLAGS_QMASK) >> 1);
466                 }
467         } else if (irq_type == VFIO_PCI_MSIX_IRQ_INDEX) {
468                 u8 pos;
469                 u16 flags;
470
471                 pos = vdev->pdev->msix_cap;
472                 if (pos) {
473                         pci_read_config_word(vdev->pdev,
474                                              pos + PCI_MSIX_FLAGS, &flags);
475
476                         return (flags & PCI_MSIX_FLAGS_QSIZE) + 1;
477                 }
478         } else if (irq_type == VFIO_PCI_ERR_IRQ_INDEX) {
479                 if (pci_is_pcie(vdev->pdev))
480                         return 1;
481         } else if (irq_type == VFIO_PCI_REQ_IRQ_INDEX) {
482                 return 1;
483         }
484
485         return 0;
486 }
487
488 static int vfio_pci_count_devs(struct pci_dev *pdev, void *data)
489 {
490         (*(int *)data)++;
491         return 0;
492 }
493
494 struct vfio_pci_fill_info {
495         int max;
496         int cur;
497         struct vfio_pci_dependent_device *devices;
498 };
499
500 static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data)
501 {
502         struct vfio_pci_fill_info *fill = data;
503         struct iommu_group *iommu_group;
504
505         if (fill->cur == fill->max)
506                 return -EAGAIN; /* Something changed, try again */
507
508         iommu_group = iommu_group_get(&pdev->dev);
509         if (!iommu_group)
510                 return -EPERM; /* Cannot reset non-isolated devices */
511
512         fill->devices[fill->cur].group_id = iommu_group_id(iommu_group);
513         fill->devices[fill->cur].segment = pci_domain_nr(pdev->bus);
514         fill->devices[fill->cur].bus = pdev->bus->number;
515         fill->devices[fill->cur].devfn = pdev->devfn;
516         fill->cur++;
517         iommu_group_put(iommu_group);
518         return 0;
519 }
520
521 struct vfio_pci_group_entry {
522         struct vfio_group *group;
523         int id;
524 };
525
526 struct vfio_pci_group_info {
527         int count;
528         struct vfio_pci_group_entry *groups;
529 };
530
531 static int vfio_pci_validate_devs(struct pci_dev *pdev, void *data)
532 {
533         struct vfio_pci_group_info *info = data;
534         struct iommu_group *group;
535         int id, i;
536
537         group = iommu_group_get(&pdev->dev);
538         if (!group)
539                 return -EPERM;
540
541         id = iommu_group_id(group);
542
543         for (i = 0; i < info->count; i++)
544                 if (info->groups[i].id == id)
545                         break;
546
547         iommu_group_put(group);
548
549         return (i == info->count) ? -EINVAL : 0;
550 }
551
552 static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
553 {
554         for (; pdev; pdev = pdev->bus->self)
555                 if (pdev->bus == slot->bus)
556                         return (pdev->slot == slot);
557         return false;
558 }
559
560 struct vfio_pci_walk_info {
561         int (*fn)(struct pci_dev *, void *data);
562         void *data;
563         struct pci_dev *pdev;
564         bool slot;
565         int ret;
566 };
567
568 static int vfio_pci_walk_wrapper(struct pci_dev *pdev, void *data)
569 {
570         struct vfio_pci_walk_info *walk = data;
571
572         if (!walk->slot || vfio_pci_dev_below_slot(pdev, walk->pdev->slot))
573                 walk->ret = walk->fn(pdev, walk->data);
574
575         return walk->ret;
576 }
577
578 static int vfio_pci_for_each_slot_or_bus(struct pci_dev *pdev,
579                                          int (*fn)(struct pci_dev *,
580                                                    void *data), void *data,
581                                          bool slot)
582 {
583         struct vfio_pci_walk_info walk = {
584                 .fn = fn, .data = data, .pdev = pdev, .slot = slot, .ret = 0,
585         };
586
587         pci_walk_bus(pdev->bus, vfio_pci_walk_wrapper, &walk);
588
589         return walk.ret;
590 }
591
592 static int msix_sparse_mmap_cap(struct vfio_pci_device *vdev,
593                                 struct vfio_info_cap *caps)
594 {
595         struct vfio_region_info_cap_sparse_mmap *sparse;
596         size_t end, size;
597         int nr_areas = 2, i = 0, ret;
598
599         end = pci_resource_len(vdev->pdev, vdev->msix_bar);
600
601         /* If MSI-X table is aligned to the start or end, only one area */
602         if (((vdev->msix_offset & PAGE_MASK) == 0) ||
603             (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) >= end))
604                 nr_areas = 1;
605
606         size = sizeof(*sparse) + (nr_areas * sizeof(*sparse->areas));
607
608         sparse = kzalloc(size, GFP_KERNEL);
609         if (!sparse)
610                 return -ENOMEM;
611
612         sparse->nr_areas = nr_areas;
613
614         if (vdev->msix_offset & PAGE_MASK) {
615                 sparse->areas[i].offset = 0;
616                 sparse->areas[i].size = vdev->msix_offset & PAGE_MASK;
617                 i++;
618         }
619
620         if (PAGE_ALIGN(vdev->msix_offset + vdev->msix_size) < end) {
621                 sparse->areas[i].offset = PAGE_ALIGN(vdev->msix_offset +
622                                                      vdev->msix_size);
623                 sparse->areas[i].size = end - sparse->areas[i].offset;
624                 i++;
625         }
626
627         ret = vfio_info_add_capability(caps, VFIO_REGION_INFO_CAP_SPARSE_MMAP,
628                                        sparse);
629         kfree(sparse);
630
631         return ret;
632 }
633
634 int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
635                                  unsigned int type, unsigned int subtype,
636                                  const struct vfio_pci_regops *ops,
637                                  size_t size, u32 flags, void *data)
638 {
639         struct vfio_pci_region *region;
640
641         region = krealloc(vdev->region,
642                           (vdev->num_regions + 1) * sizeof(*region),
643                           GFP_KERNEL);
644         if (!region)
645                 return -ENOMEM;
646
647         vdev->region = region;
648         vdev->region[vdev->num_regions].type = type;
649         vdev->region[vdev->num_regions].subtype = subtype;
650         vdev->region[vdev->num_regions].ops = ops;
651         vdev->region[vdev->num_regions].size = size;
652         vdev->region[vdev->num_regions].flags = flags;
653         vdev->region[vdev->num_regions].data = data;
654
655         vdev->num_regions++;
656
657         return 0;
658 }
659
660 struct vfio_devices {
661         struct vfio_device **devices;
662         int cur_index;
663         int max_index;
664 };
665
666 static long vfio_pci_ioctl(void *device_data,
667                            unsigned int cmd, unsigned long arg)
668 {
669         struct vfio_pci_device *vdev = device_data;
670         unsigned long minsz;
671
672         if (cmd == VFIO_DEVICE_GET_INFO) {
673                 struct vfio_device_info info;
674
675                 minsz = offsetofend(struct vfio_device_info, num_irqs);
676
677                 if (copy_from_user(&info, (void __user *)arg, minsz))
678                         return -EFAULT;
679
680                 if (info.argsz < minsz)
681                         return -EINVAL;
682
683                 info.flags = VFIO_DEVICE_FLAGS_PCI;
684
685                 if (vdev->reset_works)
686                         info.flags |= VFIO_DEVICE_FLAGS_RESET;
687
688                 info.num_regions = VFIO_PCI_NUM_REGIONS + vdev->num_regions;
689                 info.num_irqs = VFIO_PCI_NUM_IRQS;
690
691                 return copy_to_user((void __user *)arg, &info, minsz) ?
692                         -EFAULT : 0;
693
694         } else if (cmd == VFIO_DEVICE_GET_REGION_INFO) {
695                 struct pci_dev *pdev = vdev->pdev;
696                 struct vfio_region_info info;
697                 struct vfio_info_cap caps = { .buf = NULL, .size = 0 };
698                 int i, ret;
699
700                 minsz = offsetofend(struct vfio_region_info, offset);
701
702                 if (copy_from_user(&info, (void __user *)arg, minsz))
703                         return -EFAULT;
704
705                 if (info.argsz < minsz)
706                         return -EINVAL;
707
708                 switch (info.index) {
709                 case VFIO_PCI_CONFIG_REGION_INDEX:
710                         info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
711                         info.size = pdev->cfg_size;
712                         info.flags = VFIO_REGION_INFO_FLAG_READ |
713                                      VFIO_REGION_INFO_FLAG_WRITE;
714                         break;
715                 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
716                         info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
717                         info.size = pci_resource_len(pdev, info.index);
718                         if (!info.size) {
719                                 info.flags = 0;
720                                 break;
721                         }
722
723                         info.flags = VFIO_REGION_INFO_FLAG_READ |
724                                      VFIO_REGION_INFO_FLAG_WRITE;
725                         if (vdev->bar_mmap_supported[info.index]) {
726                                 info.flags |= VFIO_REGION_INFO_FLAG_MMAP;
727                                 if (info.index == vdev->msix_bar) {
728                                         ret = msix_sparse_mmap_cap(vdev, &caps);
729                                         if (ret)
730                                                 return ret;
731                                 }
732                         }
733
734                         break;
735                 case VFIO_PCI_ROM_REGION_INDEX:
736                 {
737                         void __iomem *io;
738                         size_t size;
739                         u16 cmd;
740
741                         info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
742                         info.flags = 0;
743
744                         /* Report the BAR size, not the ROM size */
745                         info.size = pci_resource_len(pdev, info.index);
746                         if (!info.size) {
747                                 /* Shadow ROMs appear as PCI option ROMs */
748                                 if (pdev->resource[PCI_ROM_RESOURCE].flags &
749                                                         IORESOURCE_ROM_SHADOW)
750                                         info.size = 0x20000;
751                                 else
752                                         break;
753                         }
754
755                         /*
756                          * Is it really there?  Enable memory decode for
757                          * implicit access in pci_map_rom().
758                          */
759                         cmd = vfio_pci_memory_lock_and_enable(vdev);
760                         io = pci_map_rom(pdev, &size);
761                         if (io) {
762                                 info.flags = VFIO_REGION_INFO_FLAG_READ;
763                                 pci_unmap_rom(pdev, io);
764                         } else {
765                                 info.size = 0;
766                         }
767                         vfio_pci_memory_unlock_and_restore(vdev, cmd);
768
769                         break;
770                 }
771                 case VFIO_PCI_VGA_REGION_INDEX:
772                         if (!vdev->has_vga)
773                                 return -EINVAL;
774
775                         info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
776                         info.size = 0xc0000;
777                         info.flags = VFIO_REGION_INFO_FLAG_READ |
778                                      VFIO_REGION_INFO_FLAG_WRITE;
779
780                         break;
781                 default:
782                 {
783                         struct vfio_region_info_cap_type cap_type;
784
785                         if (info.index >=
786                             VFIO_PCI_NUM_REGIONS + vdev->num_regions)
787                                 return -EINVAL;
788                         info.index = array_index_nospec(info.index,
789                                                         VFIO_PCI_NUM_REGIONS +
790                                                         vdev->num_regions);
791
792                         i = info.index - VFIO_PCI_NUM_REGIONS;
793
794                         info.offset = VFIO_PCI_INDEX_TO_OFFSET(info.index);
795                         info.size = vdev->region[i].size;
796                         info.flags = vdev->region[i].flags;
797
798                         cap_type.type = vdev->region[i].type;
799                         cap_type.subtype = vdev->region[i].subtype;
800
801                         ret = vfio_info_add_capability(&caps,
802                                                       VFIO_REGION_INFO_CAP_TYPE,
803                                                       &cap_type);
804                         if (ret)
805                                 return ret;
806
807                 }
808                 }
809
810                 if (caps.size) {
811                         info.flags |= VFIO_REGION_INFO_FLAG_CAPS;
812                         if (info.argsz < sizeof(info) + caps.size) {
813                                 info.argsz = sizeof(info) + caps.size;
814                                 info.cap_offset = 0;
815                         } else {
816                                 vfio_info_cap_shift(&caps, sizeof(info));
817                                 if (copy_to_user((void __user *)arg +
818                                                   sizeof(info), caps.buf,
819                                                   caps.size)) {
820                                         kfree(caps.buf);
821                                         return -EFAULT;
822                                 }
823                                 info.cap_offset = sizeof(info);
824                         }
825
826                         kfree(caps.buf);
827                 }
828
829                 return copy_to_user((void __user *)arg, &info, minsz) ?
830                         -EFAULT : 0;
831
832         } else if (cmd == VFIO_DEVICE_GET_IRQ_INFO) {
833                 struct vfio_irq_info info;
834
835                 minsz = offsetofend(struct vfio_irq_info, count);
836
837                 if (copy_from_user(&info, (void __user *)arg, minsz))
838                         return -EFAULT;
839
840                 if (info.argsz < minsz || info.index >= VFIO_PCI_NUM_IRQS)
841                         return -EINVAL;
842
843                 switch (info.index) {
844                 case VFIO_PCI_INTX_IRQ_INDEX ... VFIO_PCI_MSIX_IRQ_INDEX:
845                 case VFIO_PCI_REQ_IRQ_INDEX:
846                         break;
847                 case VFIO_PCI_ERR_IRQ_INDEX:
848                         if (pci_is_pcie(vdev->pdev))
849                                 break;
850                 /* pass thru to return error */
851                 default:
852                         return -EINVAL;
853                 }
854
855                 info.flags = VFIO_IRQ_INFO_EVENTFD;
856
857                 info.count = vfio_pci_get_irq_count(vdev, info.index);
858
859                 if (info.index == VFIO_PCI_INTX_IRQ_INDEX)
860                         info.flags |= (VFIO_IRQ_INFO_MASKABLE |
861                                        VFIO_IRQ_INFO_AUTOMASKED);
862                 else
863                         info.flags |= VFIO_IRQ_INFO_NORESIZE;
864
865                 return copy_to_user((void __user *)arg, &info, minsz) ?
866                         -EFAULT : 0;
867
868         } else if (cmd == VFIO_DEVICE_SET_IRQS) {
869                 struct vfio_irq_set hdr;
870                 u8 *data = NULL;
871                 int max, ret = 0;
872                 size_t data_size = 0;
873
874                 minsz = offsetofend(struct vfio_irq_set, count);
875
876                 if (copy_from_user(&hdr, (void __user *)arg, minsz))
877                         return -EFAULT;
878
879                 max = vfio_pci_get_irq_count(vdev, hdr.index);
880
881                 ret = vfio_set_irqs_validate_and_prepare(&hdr, max,
882                                                  VFIO_PCI_NUM_IRQS, &data_size);
883                 if (ret)
884                         return ret;
885
886                 if (data_size) {
887                         data = memdup_user((void __user *)(arg + minsz),
888                                             data_size);
889                         if (IS_ERR(data))
890                                 return PTR_ERR(data);
891                 }
892
893                 mutex_lock(&vdev->igate);
894
895                 ret = vfio_pci_set_irqs_ioctl(vdev, hdr.flags, hdr.index,
896                                               hdr.start, hdr.count, data);
897
898                 mutex_unlock(&vdev->igate);
899                 kfree(data);
900
901                 return ret;
902
903         } else if (cmd == VFIO_DEVICE_RESET) {
904                 int ret;
905
906                 if (!vdev->reset_works)
907                         return -EINVAL;
908
909                 vfio_pci_zap_and_down_write_memory_lock(vdev);
910                 ret = pci_try_reset_function(vdev->pdev);
911                 up_write(&vdev->memory_lock);
912
913                 return ret;
914
915         } else if (cmd == VFIO_DEVICE_GET_PCI_HOT_RESET_INFO) {
916                 struct vfio_pci_hot_reset_info hdr;
917                 struct vfio_pci_fill_info fill = { 0 };
918                 struct vfio_pci_dependent_device *devices = NULL;
919                 bool slot = false;
920                 int ret = 0;
921
922                 minsz = offsetofend(struct vfio_pci_hot_reset_info, count);
923
924                 if (copy_from_user(&hdr, (void __user *)arg, minsz))
925                         return -EFAULT;
926
927                 if (hdr.argsz < minsz)
928                         return -EINVAL;
929
930                 hdr.flags = 0;
931
932                 /* Can we do a slot or bus reset or neither? */
933                 if (!pci_probe_reset_slot(vdev->pdev->slot))
934                         slot = true;
935                 else if (pci_probe_reset_bus(vdev->pdev->bus))
936                         return -ENODEV;
937
938                 /* How many devices are affected? */
939                 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
940                                                     vfio_pci_count_devs,
941                                                     &fill.max, slot);
942                 if (ret)
943                         return ret;
944
945                 WARN_ON(!fill.max); /* Should always be at least one */
946
947                 /*
948                  * If there's enough space, fill it now, otherwise return
949                  * -ENOSPC and the number of devices affected.
950                  */
951                 if (hdr.argsz < sizeof(hdr) + (fill.max * sizeof(*devices))) {
952                         ret = -ENOSPC;
953                         hdr.count = fill.max;
954                         goto reset_info_exit;
955                 }
956
957                 devices = kcalloc(fill.max, sizeof(*devices), GFP_KERNEL);
958                 if (!devices)
959                         return -ENOMEM;
960
961                 fill.devices = devices;
962
963                 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
964                                                     vfio_pci_fill_devs,
965                                                     &fill, slot);
966
967                 /*
968                  * If a device was removed between counting and filling,
969                  * we may come up short of fill.max.  If a device was
970                  * added, we'll have a return of -EAGAIN above.
971                  */
972                 if (!ret)
973                         hdr.count = fill.cur;
974
975 reset_info_exit:
976                 if (copy_to_user((void __user *)arg, &hdr, minsz))
977                         ret = -EFAULT;
978
979                 if (!ret) {
980                         if (copy_to_user((void __user *)(arg + minsz), devices,
981                                          hdr.count * sizeof(*devices)))
982                                 ret = -EFAULT;
983                 }
984
985                 kfree(devices);
986                 return ret;
987
988         } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
989                 struct vfio_pci_hot_reset hdr;
990                 int32_t *group_fds;
991                 struct vfio_pci_group_entry *groups;
992                 struct vfio_pci_group_info info;
993                 struct vfio_devices devs = { .cur_index = 0 };
994                 bool slot = false;
995                 int i, group_idx, mem_idx = 0, count = 0, ret = 0;
996
997                 minsz = offsetofend(struct vfio_pci_hot_reset, count);
998
999                 if (copy_from_user(&hdr, (void __user *)arg, minsz))
1000                         return -EFAULT;
1001
1002                 if (hdr.argsz < minsz || hdr.flags)
1003                         return -EINVAL;
1004
1005                 /* Can we do a slot or bus reset or neither? */
1006                 if (!pci_probe_reset_slot(vdev->pdev->slot))
1007                         slot = true;
1008                 else if (pci_probe_reset_bus(vdev->pdev->bus))
1009                         return -ENODEV;
1010
1011                 /*
1012                  * We can't let userspace give us an arbitrarily large
1013                  * buffer to copy, so verify how many we think there
1014                  * could be.  Note groups can have multiple devices so
1015                  * one group per device is the max.
1016                  */
1017                 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
1018                                                     vfio_pci_count_devs,
1019                                                     &count, slot);
1020                 if (ret)
1021                         return ret;
1022
1023                 /* Somewhere between 1 and count is OK */
1024                 if (!hdr.count || hdr.count > count)
1025                         return -EINVAL;
1026
1027                 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
1028                 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);
1029                 if (!group_fds || !groups) {
1030                         kfree(group_fds);
1031                         kfree(groups);
1032                         return -ENOMEM;
1033                 }
1034
1035                 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
1036                                    hdr.count * sizeof(*group_fds))) {
1037                         kfree(group_fds);
1038                         kfree(groups);
1039                         return -EFAULT;
1040                 }
1041
1042                 /*
1043                  * For each group_fd, get the group through the vfio external
1044                  * user interface and store the group and iommu ID.  This
1045                  * ensures the group is held across the reset.
1046                  */
1047                 for (group_idx = 0; group_idx < hdr.count; group_idx++) {
1048                         struct vfio_group *group;
1049                         struct fd f = fdget(group_fds[group_idx]);
1050                         if (!f.file) {
1051                                 ret = -EBADF;
1052                                 break;
1053                         }
1054
1055                         group = vfio_group_get_external_user(f.file);
1056                         fdput(f);
1057                         if (IS_ERR(group)) {
1058                                 ret = PTR_ERR(group);
1059                                 break;
1060                         }
1061
1062                         groups[group_idx].group = group;
1063                         groups[group_idx].id =
1064                                         vfio_external_user_iommu_id(group);
1065                 }
1066
1067                 kfree(group_fds);
1068
1069                 /* release reference to groups on error */
1070                 if (ret)
1071                         goto hot_reset_release;
1072
1073                 info.count = hdr.count;
1074                 info.groups = groups;
1075
1076                 /*
1077                  * Test whether all the affected devices are contained
1078                  * by the set of groups provided by the user.
1079                  */
1080                 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
1081                                                     vfio_pci_validate_devs,
1082                                                     &info, slot);
1083
1084                 if (ret)
1085                         goto hot_reset_release;
1086
1087                 devs.max_index = count;
1088                 devs.devices = kcalloc(count, sizeof(struct vfio_device *),
1089                                        GFP_KERNEL);
1090                 if (!devs.devices) {
1091                         ret = -ENOMEM;
1092                         goto hot_reset_release;
1093                 }
1094
1095                 /*
1096                  * We need to get memory_lock for each device, but devices
1097                  * can share mmap_sem, therefore we need to zap and hold
1098                  * the vma_lock for each device, and only then get each
1099                  * memory_lock.
1100                  */
1101                 ret = vfio_pci_for_each_slot_or_bus(vdev->pdev,
1102                                             vfio_pci_try_zap_and_vma_lock_cb,
1103                                             &devs, slot);
1104                 if (ret)
1105                         goto hot_reset_release;
1106
1107                 for (; mem_idx < devs.cur_index; mem_idx++) {
1108                         struct vfio_pci_device *tmp;
1109
1110                         tmp = vfio_device_data(devs.devices[mem_idx]);
1111
1112                         ret = down_write_trylock(&tmp->memory_lock);
1113                         if (!ret) {
1114                                 ret = -EBUSY;
1115                                 goto hot_reset_release;
1116                         }
1117                         mutex_unlock(&tmp->vma_lock);
1118                 }
1119
1120                 /* User has access, do the reset */
1121                 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1122                                 pci_try_reset_bus(vdev->pdev->bus);
1123
1124 hot_reset_release:
1125                 for (i = 0; i < devs.cur_index; i++) {
1126                         struct vfio_device *device;
1127                         struct vfio_pci_device *tmp;
1128
1129                         device = devs.devices[i];
1130                         tmp = vfio_device_data(device);
1131
1132                         if (i < mem_idx)
1133                                 up_write(&tmp->memory_lock);
1134                         else
1135                                 mutex_unlock(&tmp->vma_lock);
1136                         vfio_device_put(device);
1137                 }
1138                 kfree(devs.devices);
1139
1140                 for (group_idx--; group_idx >= 0; group_idx--)
1141                         vfio_group_put_external_user(groups[group_idx].group);
1142
1143                 kfree(groups);
1144                 return ret;
1145         }
1146
1147         return -ENOTTY;
1148 }
1149
1150 static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
1151                            size_t count, loff_t *ppos, bool iswrite)
1152 {
1153         unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
1154         struct vfio_pci_device *vdev = device_data;
1155
1156         if (index >= VFIO_PCI_NUM_REGIONS + vdev->num_regions)
1157                 return -EINVAL;
1158
1159         switch (index) {
1160         case VFIO_PCI_CONFIG_REGION_INDEX:
1161                 return vfio_pci_config_rw(vdev, buf, count, ppos, iswrite);
1162
1163         case VFIO_PCI_ROM_REGION_INDEX:
1164                 if (iswrite)
1165                         return -EINVAL;
1166                 return vfio_pci_bar_rw(vdev, buf, count, ppos, false);
1167
1168         case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
1169                 return vfio_pci_bar_rw(vdev, buf, count, ppos, iswrite);
1170
1171         case VFIO_PCI_VGA_REGION_INDEX:
1172                 return vfio_pci_vga_rw(vdev, buf, count, ppos, iswrite);
1173         default:
1174                 index -= VFIO_PCI_NUM_REGIONS;
1175                 return vdev->region[index].ops->rw(vdev, buf,
1176                                                    count, ppos, iswrite);
1177         }
1178
1179         return -EINVAL;
1180 }
1181
1182 static ssize_t vfio_pci_read(void *device_data, char __user *buf,
1183                              size_t count, loff_t *ppos)
1184 {
1185         if (!count)
1186                 return 0;
1187
1188         return vfio_pci_rw(device_data, buf, count, ppos, false);
1189 }
1190
1191 static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
1192                               size_t count, loff_t *ppos)
1193 {
1194         if (!count)
1195                 return 0;
1196
1197         return vfio_pci_rw(device_data, (char __user *)buf, count, ppos, true);
1198 }
1199
1200 /* Return 1 on zap and vma_lock acquired, 0 on contention (only with @try) */
1201 static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try)
1202 {
1203         struct vfio_pci_mmap_vma *mmap_vma, *tmp;
1204
1205         /*
1206          * Lock ordering:
1207          * vma_lock is nested under mmap_sem for vm_ops callback paths.
1208          * The memory_lock semaphore is used by both code paths calling
1209          * into this function to zap vmas and the vm_ops.fault callback
1210          * to protect the memory enable state of the device.
1211          *
1212          * When zapping vmas we need to maintain the mmap_sem => vma_lock
1213          * ordering, which requires using vma_lock to walk vma_list to
1214          * acquire an mm, then dropping vma_lock to get the mmap_sem and
1215          * reacquiring vma_lock.  This logic is derived from similar
1216          * requirements in uverbs_user_mmap_disassociate().
1217          *
1218          * mmap_sem must always be the top-level lock when it is taken.
1219          * Therefore we can only hold the memory_lock write lock when
1220          * vma_list is empty, as we'd need to take mmap_sem to clear
1221          * entries.  vma_list can only be guaranteed empty when holding
1222          * vma_lock, thus memory_lock is nested under vma_lock.
1223          *
1224          * This enables the vm_ops.fault callback to acquire vma_lock,
1225          * followed by memory_lock read lock, while already holding
1226          * mmap_sem without risk of deadlock.
1227          */
1228         while (1) {
1229                 struct mm_struct *mm = NULL;
1230
1231                 if (try) {
1232                         if (!mutex_trylock(&vdev->vma_lock))
1233                                 return 0;
1234                 } else {
1235                         mutex_lock(&vdev->vma_lock);
1236                 }
1237                 while (!list_empty(&vdev->vma_list)) {
1238                         mmap_vma = list_first_entry(&vdev->vma_list,
1239                                                     struct vfio_pci_mmap_vma,
1240                                                     vma_next);
1241                         mm = mmap_vma->vma->vm_mm;
1242                         if (mmget_not_zero(mm))
1243                                 break;
1244
1245                         list_del(&mmap_vma->vma_next);
1246                         kfree(mmap_vma);
1247                         mm = NULL;
1248                 }
1249                 if (!mm)
1250                         return 1;
1251                 mutex_unlock(&vdev->vma_lock);
1252
1253                 if (try) {
1254                         if (!down_read_trylock(&mm->mmap_sem)) {
1255                                 mmput(mm);
1256                                 return 0;
1257                         }
1258                 } else {
1259                         down_read(&mm->mmap_sem);
1260                 }
1261                 if (mmget_still_valid(mm)) {
1262                         if (try) {
1263                                 if (!mutex_trylock(&vdev->vma_lock)) {
1264                                         up_read(&mm->mmap_sem);
1265                                         mmput(mm);
1266                                         return 0;
1267                                 }
1268                         } else {
1269                                 mutex_lock(&vdev->vma_lock);
1270                         }
1271                         list_for_each_entry_safe(mmap_vma, tmp,
1272                                                  &vdev->vma_list, vma_next) {
1273                                 struct vm_area_struct *vma = mmap_vma->vma;
1274
1275                                 if (vma->vm_mm != mm)
1276                                         continue;
1277
1278                                 list_del(&mmap_vma->vma_next);
1279                                 kfree(mmap_vma);
1280
1281                                 zap_vma_ptes(vma, vma->vm_start,
1282                                              vma->vm_end - vma->vm_start);
1283                         }
1284                         mutex_unlock(&vdev->vma_lock);
1285                 }
1286                 up_read(&mm->mmap_sem);
1287                 mmput(mm);
1288         }
1289 }
1290
1291 void vfio_pci_zap_and_down_write_memory_lock(struct vfio_pci_device *vdev)
1292 {
1293         vfio_pci_zap_and_vma_lock(vdev, false);
1294         down_write(&vdev->memory_lock);
1295         mutex_unlock(&vdev->vma_lock);
1296 }
1297
1298 u16 vfio_pci_memory_lock_and_enable(struct vfio_pci_device *vdev)
1299 {
1300         u16 cmd;
1301
1302         down_write(&vdev->memory_lock);
1303         pci_read_config_word(vdev->pdev, PCI_COMMAND, &cmd);
1304         if (!(cmd & PCI_COMMAND_MEMORY))
1305                 pci_write_config_word(vdev->pdev, PCI_COMMAND,
1306                                       cmd | PCI_COMMAND_MEMORY);
1307
1308         return cmd;
1309 }
1310
1311 void vfio_pci_memory_unlock_and_restore(struct vfio_pci_device *vdev, u16 cmd)
1312 {
1313         pci_write_config_word(vdev->pdev, PCI_COMMAND, cmd);
1314         up_write(&vdev->memory_lock);
1315 }
1316
1317 /* Caller holds vma_lock */
1318 static int __vfio_pci_add_vma(struct vfio_pci_device *vdev,
1319                               struct vm_area_struct *vma)
1320 {
1321         struct vfio_pci_mmap_vma *mmap_vma;
1322
1323         mmap_vma = kmalloc(sizeof(*mmap_vma), GFP_KERNEL);
1324         if (!mmap_vma)
1325                 return -ENOMEM;
1326
1327         mmap_vma->vma = vma;
1328         list_add(&mmap_vma->vma_next, &vdev->vma_list);
1329
1330         return 0;
1331 }
1332
1333 /*
1334  * Zap mmaps on open so that we can fault them in on access and therefore
1335  * our vma_list only tracks mappings accessed since last zap.
1336  */
1337 static void vfio_pci_mmap_open(struct vm_area_struct *vma)
1338 {
1339         zap_vma_ptes(vma, vma->vm_start, vma->vm_end - vma->vm_start);
1340 }
1341
1342 static void vfio_pci_mmap_close(struct vm_area_struct *vma)
1343 {
1344         struct vfio_pci_device *vdev = vma->vm_private_data;
1345         struct vfio_pci_mmap_vma *mmap_vma;
1346
1347         mutex_lock(&vdev->vma_lock);
1348         list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) {
1349                 if (mmap_vma->vma == vma) {
1350                         list_del(&mmap_vma->vma_next);
1351                         kfree(mmap_vma);
1352                         break;
1353                 }
1354         }
1355         mutex_unlock(&vdev->vma_lock);
1356 }
1357
1358 static int vfio_pci_mmap_fault(struct vm_fault *vmf)
1359 {
1360         struct vm_area_struct *vma = vmf->vma;
1361         struct vfio_pci_device *vdev = vma->vm_private_data;
1362         int ret = VM_FAULT_NOPAGE;
1363
1364         mutex_lock(&vdev->vma_lock);
1365         down_read(&vdev->memory_lock);
1366
1367         if (!__vfio_pci_memory_enabled(vdev)) {
1368                 ret = VM_FAULT_SIGBUS;
1369                 mutex_unlock(&vdev->vma_lock);
1370                 goto up_out;
1371         }
1372
1373         if (__vfio_pci_add_vma(vdev, vma)) {
1374                 ret = VM_FAULT_OOM;
1375                 mutex_unlock(&vdev->vma_lock);
1376                 goto up_out;
1377         }
1378
1379         mutex_unlock(&vdev->vma_lock);
1380
1381         if (io_remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
1382                                vma->vm_end - vma->vm_start, vma->vm_page_prot))
1383                 ret = VM_FAULT_SIGBUS;
1384
1385 up_out:
1386         up_read(&vdev->memory_lock);
1387         return ret;
1388 }
1389
1390 static const struct vm_operations_struct vfio_pci_mmap_ops = {
1391         .open = vfio_pci_mmap_open,
1392         .close = vfio_pci_mmap_close,
1393         .fault = vfio_pci_mmap_fault,
1394 };
1395
1396 static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
1397 {
1398         struct vfio_pci_device *vdev = device_data;
1399         struct pci_dev *pdev = vdev->pdev;
1400         unsigned int index;
1401         u64 phys_len, req_len, pgoff, req_start;
1402         int ret;
1403
1404         index = vma->vm_pgoff >> (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT);
1405
1406         if (vma->vm_end < vma->vm_start)
1407                 return -EINVAL;
1408         if ((vma->vm_flags & VM_SHARED) == 0)
1409                 return -EINVAL;
1410         if (index >= VFIO_PCI_ROM_REGION_INDEX)
1411                 return -EINVAL;
1412         if (!vdev->bar_mmap_supported[index])
1413                 return -EINVAL;
1414
1415         phys_len = PAGE_ALIGN(pci_resource_len(pdev, index));
1416         req_len = vma->vm_end - vma->vm_start;
1417         pgoff = vma->vm_pgoff &
1418                 ((1U << (VFIO_PCI_OFFSET_SHIFT - PAGE_SHIFT)) - 1);
1419         req_start = pgoff << PAGE_SHIFT;
1420
1421         if (req_start + req_len > phys_len)
1422                 return -EINVAL;
1423
1424         if (index == vdev->msix_bar) {
1425                 /*
1426                  * Disallow mmaps overlapping the MSI-X table; users don't
1427                  * get to touch this directly.  We could find somewhere
1428                  * else to map the overlap, but page granularity is only
1429                  * a recommendation, not a requirement, so the user needs
1430                  * to know which bits are real.  Requiring them to mmap
1431                  * around the table makes that clear.
1432                  */
1433
1434                 /* If neither entirely above nor below, then it overlaps */
1435                 if (!(req_start >= vdev->msix_offset + vdev->msix_size ||
1436                       req_start + req_len <= vdev->msix_offset))
1437                         return -EINVAL;
1438         }
1439
1440         /*
1441          * Even though we don't make use of the barmap for the mmap,
1442          * we need to request the region and the barmap tracks that.
1443          */
1444         if (!vdev->barmap[index]) {
1445                 ret = pci_request_selected_regions(pdev,
1446                                                    1 << index, "vfio-pci");
1447                 if (ret)
1448                         return ret;
1449
1450                 vdev->barmap[index] = pci_iomap(pdev, index, 0);
1451                 if (!vdev->barmap[index]) {
1452                         pci_release_selected_regions(pdev, 1 << index);
1453                         return -ENOMEM;
1454                 }
1455         }
1456
1457         vma->vm_private_data = vdev;
1458         vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1459         vma->vm_pgoff = (pci_resource_start(pdev, index) >> PAGE_SHIFT) + pgoff;
1460
1461         /*
1462          * See remap_pfn_range(), called from vfio_pci_fault() but we can't
1463          * change vm_flags within the fault handler.  Set them now.
1464          */
1465         vma->vm_flags |= VM_IO | VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP;
1466         vma->vm_ops = &vfio_pci_mmap_ops;
1467
1468         return 0;
1469 }
1470
1471 static void vfio_pci_request(void *device_data, unsigned int count)
1472 {
1473         struct vfio_pci_device *vdev = device_data;
1474
1475         mutex_lock(&vdev->igate);
1476
1477         if (vdev->req_trigger) {
1478                 if (!(count % 10))
1479                         dev_notice_ratelimited(&vdev->pdev->dev,
1480                                 "Relaying device request to user (#%u)\n",
1481                                 count);
1482                 eventfd_signal(vdev->req_trigger, 1);
1483         } else if (count == 0) {
1484                 dev_warn(&vdev->pdev->dev,
1485                         "No device request channel registered, blocked until released by user\n");
1486         }
1487
1488         mutex_unlock(&vdev->igate);
1489 }
1490
1491 static const struct vfio_device_ops vfio_pci_ops = {
1492         .name           = "vfio-pci",
1493         .open           = vfio_pci_open,
1494         .release        = vfio_pci_release,
1495         .ioctl          = vfio_pci_ioctl,
1496         .read           = vfio_pci_read,
1497         .write          = vfio_pci_write,
1498         .mmap           = vfio_pci_mmap,
1499         .request        = vfio_pci_request,
1500 };
1501
1502 static int vfio_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1503 {
1504         struct vfio_pci_device *vdev;
1505         struct iommu_group *group;
1506         int ret;
1507
1508         if (pdev->hdr_type != PCI_HEADER_TYPE_NORMAL)
1509                 return -EINVAL;
1510
1511         group = vfio_iommu_group_get(&pdev->dev);
1512         if (!group)
1513                 return -EINVAL;
1514
1515         vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
1516         if (!vdev) {
1517                 vfio_iommu_group_put(group, &pdev->dev);
1518                 return -ENOMEM;
1519         }
1520
1521         vdev->pdev = pdev;
1522         vdev->irq_type = VFIO_PCI_NUM_IRQS;
1523         mutex_init(&vdev->igate);
1524         spin_lock_init(&vdev->irqlock);
1525         INIT_LIST_HEAD(&vdev->dummy_resources_list);
1526         mutex_init(&vdev->vma_lock);
1527         INIT_LIST_HEAD(&vdev->vma_list);
1528         init_rwsem(&vdev->memory_lock);
1529
1530         ret = vfio_add_group_dev(&pdev->dev, &vfio_pci_ops, vdev);
1531         if (ret) {
1532                 vfio_iommu_group_put(group, &pdev->dev);
1533                 kfree(vdev);
1534                 return ret;
1535         }
1536
1537         if (vfio_pci_is_vga(pdev)) {
1538                 vga_client_register(pdev, vdev, NULL, vfio_pci_set_vga_decode);
1539                 vga_set_legacy_decoding(pdev,
1540                                         vfio_pci_set_vga_decode(vdev, false));
1541         }
1542
1543         if (!disable_idle_d3) {
1544                 /*
1545                  * pci-core sets the device power state to an unknown value at
1546                  * bootup and after being removed from a driver.  The only
1547                  * transition it allows from this unknown state is to D0, which
1548                  * typically happens when a driver calls pci_enable_device().
1549                  * We're not ready to enable the device yet, but we do want to
1550                  * be able to get to D3.  Therefore first do a D0 transition
1551                  * before going to D3.
1552                  */
1553                 pci_set_power_state(pdev, PCI_D0);
1554                 pci_set_power_state(pdev, PCI_D3hot);
1555         }
1556
1557         return ret;
1558 }
1559
1560 static void vfio_pci_remove(struct pci_dev *pdev)
1561 {
1562         struct vfio_pci_device *vdev;
1563
1564         vdev = vfio_del_group_dev(&pdev->dev);
1565         if (!vdev)
1566                 return;
1567
1568         vfio_iommu_group_put(pdev->dev.iommu_group, &pdev->dev);
1569         kfree(vdev->region);
1570         kfree(vdev);
1571
1572         if (vfio_pci_is_vga(pdev)) {
1573                 vga_client_register(pdev, NULL, NULL, NULL);
1574                 vga_set_legacy_decoding(pdev,
1575                                 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM |
1576                                 VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM);
1577         }
1578
1579         if (!disable_idle_d3)
1580                 pci_set_power_state(pdev, PCI_D0);
1581 }
1582
1583 static pci_ers_result_t vfio_pci_aer_err_detected(struct pci_dev *pdev,
1584                                                   pci_channel_state_t state)
1585 {
1586         struct vfio_pci_device *vdev;
1587         struct vfio_device *device;
1588
1589         device = vfio_device_get_from_dev(&pdev->dev);
1590         if (device == NULL)
1591                 return PCI_ERS_RESULT_DISCONNECT;
1592
1593         vdev = vfio_device_data(device);
1594         if (vdev == NULL) {
1595                 vfio_device_put(device);
1596                 return PCI_ERS_RESULT_DISCONNECT;
1597         }
1598
1599         mutex_lock(&vdev->igate);
1600
1601         if (vdev->err_trigger)
1602                 eventfd_signal(vdev->err_trigger, 1);
1603
1604         mutex_unlock(&vdev->igate);
1605
1606         vfio_device_put(device);
1607
1608         return PCI_ERS_RESULT_CAN_RECOVER;
1609 }
1610
1611 static const struct pci_error_handlers vfio_err_handlers = {
1612         .error_detected = vfio_pci_aer_err_detected,
1613 };
1614
1615 static struct pci_driver vfio_pci_driver = {
1616         .name           = "vfio-pci",
1617         .id_table       = NULL, /* only dynamic ids */
1618         .probe          = vfio_pci_probe,
1619         .remove         = vfio_pci_remove,
1620         .err_handler    = &vfio_err_handlers,
1621 };
1622
1623 static int vfio_pci_get_devs(struct pci_dev *pdev, void *data)
1624 {
1625         struct vfio_devices *devs = data;
1626         struct vfio_device *device;
1627
1628         if (devs->cur_index == devs->max_index)
1629                 return -ENOSPC;
1630
1631         device = vfio_device_get_from_dev(&pdev->dev);
1632         if (!device)
1633                 return -EINVAL;
1634
1635         if (pci_dev_driver(pdev) != &vfio_pci_driver) {
1636                 vfio_device_put(device);
1637                 return -EBUSY;
1638         }
1639
1640         devs->devices[devs->cur_index++] = device;
1641         return 0;
1642 }
1643
1644 static int vfio_pci_try_zap_and_vma_lock_cb(struct pci_dev *pdev, void *data)
1645 {
1646         struct vfio_devices *devs = data;
1647         struct vfio_device *device;
1648         struct vfio_pci_device *vdev;
1649
1650         if (devs->cur_index == devs->max_index)
1651                 return -ENOSPC;
1652
1653         device = vfio_device_get_from_dev(&pdev->dev);
1654         if (!device)
1655                 return -EINVAL;
1656
1657         if (pci_dev_driver(pdev) != &vfio_pci_driver) {
1658                 vfio_device_put(device);
1659                 return -EBUSY;
1660         }
1661
1662         vdev = vfio_device_data(device);
1663
1664         /*
1665          * Locking multiple devices is prone to deadlock, runaway and
1666          * unwind if we hit contention.
1667          */
1668         if (!vfio_pci_zap_and_vma_lock(vdev, true)) {
1669                 vfio_device_put(device);
1670                 return -EBUSY;
1671         }
1672
1673         devs->devices[devs->cur_index++] = device;
1674         return 0;
1675 }
1676
1677 /*
1678  * Attempt to do a bus/slot reset if there are devices affected by a reset for
1679  * this device that are needs_reset and all of the affected devices are unused
1680  * (!refcnt).  Callers are required to hold driver_lock when calling this to
1681  * prevent device opens and concurrent bus reset attempts.  We prevent device
1682  * unbinds by acquiring and holding a reference to the vfio_device.
1683  *
1684  * NB: vfio-core considers a group to be viable even if some devices are
1685  * bound to drivers like pci-stub or pcieport.  Here we require all devices
1686  * to be bound to vfio_pci since that's the only way we can be sure they
1687  * stay put.
1688  */
1689 static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
1690 {
1691         struct vfio_devices devs = { .cur_index = 0 };
1692         int i = 0, ret = -EINVAL;
1693         bool needs_reset = false, slot = false;
1694         struct vfio_pci_device *tmp;
1695
1696         if (!pci_probe_reset_slot(vdev->pdev->slot))
1697                 slot = true;
1698         else if (pci_probe_reset_bus(vdev->pdev->bus))
1699                 return;
1700
1701         if (vfio_pci_for_each_slot_or_bus(vdev->pdev, vfio_pci_count_devs,
1702                                           &i, slot) || !i)
1703                 return;
1704
1705         devs.max_index = i;
1706         devs.devices = kcalloc(i, sizeof(struct vfio_device *), GFP_KERNEL);
1707         if (!devs.devices)
1708                 return;
1709
1710         if (vfio_pci_for_each_slot_or_bus(vdev->pdev,
1711                                           vfio_pci_get_devs, &devs, slot))
1712                 goto put_devs;
1713
1714         for (i = 0; i < devs.cur_index; i++) {
1715                 tmp = vfio_device_data(devs.devices[i]);
1716                 if (tmp->needs_reset)
1717                         needs_reset = true;
1718                 if (tmp->refcnt)
1719                         goto put_devs;
1720         }
1721
1722         if (needs_reset)
1723                 ret = slot ? pci_try_reset_slot(vdev->pdev->slot) :
1724                              pci_try_reset_bus(vdev->pdev->bus);
1725
1726 put_devs:
1727         for (i = 0; i < devs.cur_index; i++) {
1728                 tmp = vfio_device_data(devs.devices[i]);
1729                 if (!ret)
1730                         tmp->needs_reset = false;
1731
1732                 if (!tmp->refcnt && !disable_idle_d3)
1733                         pci_set_power_state(tmp->pdev, PCI_D3hot);
1734
1735                 vfio_device_put(devs.devices[i]);
1736         }
1737
1738         kfree(devs.devices);
1739 }
1740
1741 static void __exit vfio_pci_cleanup(void)
1742 {
1743         pci_unregister_driver(&vfio_pci_driver);
1744         vfio_pci_uninit_perm_bits();
1745 }
1746
1747 static void __init vfio_pci_fill_ids(void)
1748 {
1749         char *p, *id;
1750         int rc;
1751
1752         /* no ids passed actually */
1753         if (ids[0] == '\0')
1754                 return;
1755
1756         /* add ids specified in the module parameter */
1757         p = ids;
1758         while ((id = strsep(&p, ","))) {
1759                 unsigned int vendor, device, subvendor = PCI_ANY_ID,
1760                         subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
1761                 int fields;
1762
1763                 if (!strlen(id))
1764                         continue;
1765
1766                 fields = sscanf(id, "%x:%x:%x:%x:%x:%x",
1767                                 &vendor, &device, &subvendor, &subdevice,
1768                                 &class, &class_mask);
1769
1770                 if (fields < 2) {
1771                         pr_warn("invalid id string \"%s\"\n", id);
1772                         continue;
1773                 }
1774
1775                 rc = pci_add_dynid(&vfio_pci_driver, vendor, device,
1776                                    subvendor, subdevice, class, class_mask, 0);
1777                 if (rc)
1778                         pr_warn("failed to add dynamic id [%04x:%04x[%04x:%04x]] class %#08x/%08x (%d)\n",
1779                                 vendor, device, subvendor, subdevice,
1780                                 class, class_mask, rc);
1781                 else
1782                         pr_info("add [%04x:%04x[%04x:%04x]] class %#08x/%08x\n",
1783                                 vendor, device, subvendor, subdevice,
1784                                 class, class_mask);
1785         }
1786 }
1787
1788 static int __init vfio_pci_init(void)
1789 {
1790         int ret;
1791
1792         /* Allocate shared config space permision data used by all devices */
1793         ret = vfio_pci_init_perm_bits();
1794         if (ret)
1795                 return ret;
1796
1797         /* Register and scan for devices */
1798         ret = pci_register_driver(&vfio_pci_driver);
1799         if (ret)
1800                 goto out_driver;
1801
1802         vfio_pci_fill_ids();
1803
1804         return 0;
1805
1806 out_driver:
1807         vfio_pci_uninit_perm_bits();
1808         return ret;
1809 }
1810
1811 module_init(vfio_pci_init);
1812 module_exit(vfio_pci_cleanup);
1813
1814 MODULE_VERSION(DRIVER_VERSION);
1815 MODULE_LICENSE("GPL v2");
1816 MODULE_AUTHOR(DRIVER_AUTHOR);
1817 MODULE_DESCRIPTION(DRIVER_DESC);