2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/queue.h>
36 #include <sys/kernel.h>
38 #include <sys/_pthreadtypes.h>
40 #include <dev/pci/pcireg.h>
44 #define PCI_BARMAX PCIR_MAX_BAR_0 /* BAR registers in a Type 0 header */
49 struct vm_snapshot_meta;
52 char *pe_emu; /* Name of device emulation */
54 /* instance creation */
55 int (*pe_init)(struct vmctx *, struct pci_devinst *,
57 int (*pe_legacy_config)(nvlist_t *, const char *);
60 /* ACPI DSDT enumeration */
61 void (*pe_write_dsdt)(struct pci_devinst *);
63 /* config space read/write callbacks */
64 int (*pe_cfgwrite)(struct vmctx *ctx, int vcpu,
65 struct pci_devinst *pi, int offset,
66 int bytes, uint32_t val);
67 int (*pe_cfgread)(struct vmctx *ctx, int vcpu,
68 struct pci_devinst *pi, int offset,
69 int bytes, uint32_t *retval);
71 /* BAR read/write callbacks */
72 void (*pe_barwrite)(struct vmctx *ctx, int vcpu,
73 struct pci_devinst *pi, int baridx,
74 uint64_t offset, int size, uint64_t value);
75 uint64_t (*pe_barread)(struct vmctx *ctx, int vcpu,
76 struct pci_devinst *pi, int baridx,
77 uint64_t offset, int size);
79 void (*pe_baraddr)(struct vmctx *ctx, struct pci_devinst *pi,
80 int baridx, int enabled, uint64_t address);
82 /* Save/restore device state */
83 int (*pe_snapshot)(struct vm_snapshot_meta *meta);
84 int (*pe_pause)(struct vmctx *ctx, struct pci_devinst *pi);
85 int (*pe_resume)(struct vmctx *ctx, struct pci_devinst *pi);
88 #define PCI_EMUL_SET(x) DATA_SET(pci_devemu_set, x);
99 enum pcibar_type type; /* io or memory */
107 struct msix_table_entry {
110 uint32_t vector_control;
114 * In case the structure is modified to hold extra information, use a define
115 * for the size that should be emulated.
117 #define MSIX_TABLE_ENTRY_SIZE 16
118 #define MAX_MSIX_TABLE_ENTRIES 2048
119 #define PBA_SIZE(msgnum) (roundup2((msgnum), 64) / 8)
128 struct pci_devemu *pi_d;
129 struct vmctx *pi_vmctx;
130 uint8_t pi_bus, pi_slot, pi_func;
131 char pi_name[PI_NAMESZ];
138 enum lintr_stat state;
141 pthread_mutex_t lock;
155 uint32_t table_offset;
160 struct msix_table_entry *table; /* allocated at runtime */
161 uint8_t *mapped_addr;
165 void *pi_arg; /* devemu-private data */
167 u_char pi_cfgdata[PCI_REGMAX + 1];
168 struct pcibar pi_bar[PCI_BARMAX + 1];
179 static_assert(sizeof(struct msicap) == 14, "compile-time assertion failed");
185 uint32_t table_info; /* bar index and offset within it */
186 uint32_t pba_info; /* bar index and offset within it */
188 static_assert(sizeof(struct msixcap) == 12, "compile-time assertion failed");
193 uint16_t pcie_capabilities;
195 uint32_t dev_capabilities; /* all devices */
196 uint16_t dev_control;
199 uint32_t link_capabilities; /* devices with links */
200 uint16_t link_control;
201 uint16_t link_status;
203 uint32_t slot_capabilities; /* ports with slots */
204 uint16_t slot_control;
205 uint16_t slot_status;
207 uint16_t root_control; /* root ports */
208 uint16_t root_capabilities;
209 uint32_t root_status;
211 uint32_t dev_capabilities2; /* all devices */
212 uint16_t dev_control2;
213 uint16_t dev_status2;
215 uint32_t link_capabilities2; /* devices with links */
216 uint16_t link_control2;
217 uint16_t link_status2;
219 uint32_t slot_capabilities2; /* ports with slots */
220 uint16_t slot_control2;
221 uint16_t slot_status2;
223 static_assert(sizeof(struct pciecap) == 60, "compile-time assertion failed");
225 typedef void (*pci_lintr_cb)(int b, int s, int pin, int pirq_pin,
226 int ioapic_irq, void *arg);
228 int init_pci(struct vmctx *ctx);
229 void pci_callback(void);
230 int pci_emul_alloc_bar(struct pci_devinst *pdi, int idx,
231 enum pcibar_type type, uint64_t size);
232 int pci_emul_add_msicap(struct pci_devinst *pi, int msgnum);
233 int pci_emul_add_pciecap(struct pci_devinst *pi, int pcie_device_type);
234 void pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes,
235 uint32_t val, uint8_t capoff, int capid);
236 void pci_emul_cmd_changed(struct pci_devinst *pi, uint16_t old);
237 void pci_generate_msi(struct pci_devinst *pi, int msgnum);
238 void pci_generate_msix(struct pci_devinst *pi, int msgnum);
239 void pci_lintr_assert(struct pci_devinst *pi);
240 void pci_lintr_deassert(struct pci_devinst *pi);
241 void pci_lintr_request(struct pci_devinst *pi);
242 int pci_msi_enabled(struct pci_devinst *pi);
243 int pci_msix_enabled(struct pci_devinst *pi);
244 int pci_msix_table_bar(struct pci_devinst *pi);
245 int pci_msix_pba_bar(struct pci_devinst *pi);
246 int pci_msi_maxmsgnum(struct pci_devinst *pi);
247 int pci_parse_legacy_config(nvlist_t *nvl, const char *opt);
248 int pci_parse_slot(char *opt);
249 void pci_print_supported_devices();
250 void pci_populate_msicap(struct msicap *cap, int msgs, int nextptr);
251 int pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum);
252 int pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
254 uint64_t pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size);
255 int pci_count_lintr(int bus);
256 void pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg);
257 void pci_write_dsdt(void);
258 uint64_t pci_ecfg_base(void);
259 int pci_bus_configured(int bus);
260 #ifdef BHYVE_SNAPSHOT
261 int pci_snapshot(struct vm_snapshot_meta *meta);
262 int pci_pause(struct vmctx *ctx, const char *dev_name);
263 int pci_resume(struct vmctx *ctx, const char *dev_name);
267 pci_set_cfgdata8(struct pci_devinst *pi, int offset, uint8_t val)
269 assert(offset <= PCI_REGMAX);
270 *(uint8_t *)(pi->pi_cfgdata + offset) = val;
274 pci_set_cfgdata16(struct pci_devinst *pi, int offset, uint16_t val)
276 assert(offset <= (PCI_REGMAX - 1) && (offset & 1) == 0);
277 *(uint16_t *)(pi->pi_cfgdata + offset) = val;
281 pci_set_cfgdata32(struct pci_devinst *pi, int offset, uint32_t val)
283 assert(offset <= (PCI_REGMAX - 3) && (offset & 3) == 0);
284 *(uint32_t *)(pi->pi_cfgdata + offset) = val;
287 static __inline uint8_t
288 pci_get_cfgdata8(struct pci_devinst *pi, int offset)
290 assert(offset <= PCI_REGMAX);
291 return (*(uint8_t *)(pi->pi_cfgdata + offset));
294 static __inline uint16_t
295 pci_get_cfgdata16(struct pci_devinst *pi, int offset)
297 assert(offset <= (PCI_REGMAX - 1) && (offset & 1) == 0);
298 return (*(uint16_t *)(pi->pi_cfgdata + offset));
301 static __inline uint32_t
302 pci_get_cfgdata32(struct pci_devinst *pi, int offset)
304 assert(offset <= (PCI_REGMAX - 3) && (offset & 3) == 0);
305 return (*(uint32_t *)(pi->pi_cfgdata + offset));
308 #endif /* _PCI_EMUL_H_ */