blob: 0710bda5ae2c2e103e266e806dbb066fca320a16 [file] [log] [blame]
Alex Williamson89e1f7d2012-07-31 08:16:24 -06001/*
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#include <linux/mutex.h>
15#include <linux/pci.h>
Feng Wu6d7425f2015-09-18 22:29:50 +080016#include <linux/irqbypass.h>
Alex Williamson28541d42016-02-22 16:02:39 -070017#include <linux/types.h>
Alex Williamson89e1f7d2012-07-31 08:16:24 -060018
19#ifndef VFIO_PCI_PRIVATE_H
20#define VFIO_PCI_PRIVATE_H
21
22#define VFIO_PCI_OFFSET_SHIFT 40
23
24#define VFIO_PCI_OFFSET_TO_INDEX(off) (off >> VFIO_PCI_OFFSET_SHIFT)
25#define VFIO_PCI_INDEX_TO_OFFSET(index) ((u64)(index) << VFIO_PCI_OFFSET_SHIFT)
26#define VFIO_PCI_OFFSET_MASK (((u64)(1) << VFIO_PCI_OFFSET_SHIFT) - 1)
27
28struct vfio_pci_irq_ctx {
29 struct eventfd_ctx *trigger;
30 struct virqfd *unmask;
31 struct virqfd *mask;
32 char *name;
33 bool masked;
Feng Wu6d7425f2015-09-18 22:29:50 +080034 struct irq_bypass_producer producer;
Alex Williamson89e1f7d2012-07-31 08:16:24 -060035};
36
Alex Williamson28541d42016-02-22 16:02:39 -070037struct vfio_pci_device;
38struct vfio_pci_region;
39
40struct vfio_pci_regops {
41 size_t (*rw)(struct vfio_pci_device *vdev, char __user *buf,
42 size_t count, loff_t *ppos, bool iswrite);
43 void (*release)(struct vfio_pci_device *vdev,
44 struct vfio_pci_region *region);
45};
46
47struct vfio_pci_region {
48 u32 type;
49 u32 subtype;
50 const struct vfio_pci_regops *ops;
51 void *data;
52 size_t size;
53 u32 flags;
54};
55
Alex Williamson89e1f7d2012-07-31 08:16:24 -060056struct vfio_pci_device {
57 struct pci_dev *pdev;
58 void __iomem *barmap[PCI_STD_RESOURCE_END + 1];
59 u8 *pci_config_map;
60 u8 *vconfig;
61 struct perm_bits *msi_perm;
62 spinlock_t irqlock;
63 struct mutex igate;
64 struct msix_entry *msix;
65 struct vfio_pci_irq_ctx *ctx;
66 int num_ctx;
67 int irq_type;
Alex Williamson28541d42016-02-22 16:02:39 -070068 int num_regions;
69 struct vfio_pci_region *region;
Alex Williamson89e1f7d2012-07-31 08:16:24 -060070 u8 msi_qmax;
71 u8 msix_bar;
72 u16 msix_size;
73 u32 msix_offset;
74 u32 rbar[7];
75 bool pci_2_3;
76 bool virq_disabled;
77 bool reset_works;
78 bool extended_caps;
79 bool bardirty;
Alex Williamson84237a82013-02-18 10:11:13 -070080 bool has_vga;
Alex Williamsonbc4fba72014-08-07 11:12:07 -060081 bool needs_reset;
Alex Williamson89e1f7d2012-07-31 08:16:24 -060082 struct pci_saved_state *pci_saved_state;
Alex Williamson61d79252014-08-07 11:12:04 -060083 int refcnt;
Vijay Mohan Pandarathildad9f892013-03-11 09:31:22 -060084 struct eventfd_ctx *err_trigger;
Alex Williamson6140a8f2015-02-06 15:05:08 -070085 struct eventfd_ctx *req_trigger;
Alex Williamson89e1f7d2012-07-31 08:16:24 -060086};
87
88#define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
89#define is_msi(vdev) (vdev->irq_type == VFIO_PCI_MSI_IRQ_INDEX)
90#define is_msix(vdev) (vdev->irq_type == VFIO_PCI_MSIX_IRQ_INDEX)
91#define is_irq_none(vdev) (!(is_intx(vdev) || is_msi(vdev) || is_msix(vdev)))
92#define irq_is(vdev, type) (vdev->irq_type == type)
93
94extern void vfio_pci_intx_mask(struct vfio_pci_device *vdev);
95extern void vfio_pci_intx_unmask(struct vfio_pci_device *vdev);
96
97extern int vfio_pci_set_irqs_ioctl(struct vfio_pci_device *vdev,
98 uint32_t flags, unsigned index,
99 unsigned start, unsigned count, void *data);
100
Alex Williamson906ee992013-02-14 14:02:12 -0700101extern ssize_t vfio_pci_config_rw(struct vfio_pci_device *vdev,
102 char __user *buf, size_t count,
103 loff_t *ppos, bool iswrite);
104
105extern ssize_t vfio_pci_bar_rw(struct vfio_pci_device *vdev, char __user *buf,
106 size_t count, loff_t *ppos, bool iswrite);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600107
Alex Williamson84237a82013-02-18 10:11:13 -0700108extern ssize_t vfio_pci_vga_rw(struct vfio_pci_device *vdev, char __user *buf,
109 size_t count, loff_t *ppos, bool iswrite);
110
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600111extern int vfio_pci_init_perm_bits(void);
112extern void vfio_pci_uninit_perm_bits(void);
113
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600114extern int vfio_config_init(struct vfio_pci_device *vdev);
115extern void vfio_config_free(struct vfio_pci_device *vdev);
Alex Williamson28541d42016-02-22 16:02:39 -0700116
117extern int vfio_pci_register_dev_region(struct vfio_pci_device *vdev,
118 unsigned int type, unsigned int subtype,
119 const struct vfio_pci_regops *ops,
120 size_t size, u32 flags, void *data);
Alex Williamson89e1f7d2012-07-31 08:16:24 -0600121#endif /* VFIO_PCI_PRIVATE_H */