xref: /illumos-gate/usr/src/cmd/bhyve/common/pci_passthru.c (revision 5c4a5fe16715fb423db76577a6883b5bbecdbe45)
1*5c4a5fe1SAndy Fiddaman /*-
2*5c4a5fe1SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
3*5c4a5fe1SAndy Fiddaman  *
4*5c4a5fe1SAndy Fiddaman  * Copyright (c) 2011 NetApp, Inc.
5*5c4a5fe1SAndy Fiddaman  * All rights reserved.
6*5c4a5fe1SAndy Fiddaman  *
7*5c4a5fe1SAndy Fiddaman  * Redistribution and use in source and binary forms, with or without
8*5c4a5fe1SAndy Fiddaman  * modification, are permitted provided that the following conditions
9*5c4a5fe1SAndy Fiddaman  * are met:
10*5c4a5fe1SAndy Fiddaman  * 1. Redistributions of source code must retain the above copyright
11*5c4a5fe1SAndy Fiddaman  *    notice, this list of conditions and the following disclaimer.
12*5c4a5fe1SAndy Fiddaman  * 2. Redistributions in binary form must reproduce the above copyright
13*5c4a5fe1SAndy Fiddaman  *    notice, this list of conditions and the following disclaimer in the
14*5c4a5fe1SAndy Fiddaman  *    documentation and/or other materials provided with the distribution.
15*5c4a5fe1SAndy Fiddaman  *
16*5c4a5fe1SAndy Fiddaman  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17*5c4a5fe1SAndy Fiddaman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*5c4a5fe1SAndy Fiddaman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*5c4a5fe1SAndy Fiddaman  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20*5c4a5fe1SAndy Fiddaman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*5c4a5fe1SAndy Fiddaman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*5c4a5fe1SAndy Fiddaman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*5c4a5fe1SAndy Fiddaman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*5c4a5fe1SAndy Fiddaman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*5c4a5fe1SAndy Fiddaman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*5c4a5fe1SAndy Fiddaman  * SUCH DAMAGE.
27*5c4a5fe1SAndy Fiddaman  */
28*5c4a5fe1SAndy Fiddaman 
29*5c4a5fe1SAndy Fiddaman 
30*5c4a5fe1SAndy Fiddaman #include <sys/param.h>
31*5c4a5fe1SAndy Fiddaman #include <sys/types.h>
32*5c4a5fe1SAndy Fiddaman #include <sys/mman.h>
33*5c4a5fe1SAndy Fiddaman #include <sys/pciio.h>
34*5c4a5fe1SAndy Fiddaman #include <sys/ioctl.h>
35*5c4a5fe1SAndy Fiddaman #include <sys/stat.h>
36*5c4a5fe1SAndy Fiddaman 
37*5c4a5fe1SAndy Fiddaman #include <sys/pci.h>
38*5c4a5fe1SAndy Fiddaman 
39*5c4a5fe1SAndy Fiddaman #include <dev/io/iodev.h>
40*5c4a5fe1SAndy Fiddaman #include <dev/pci/pcireg.h>
41*5c4a5fe1SAndy Fiddaman 
42*5c4a5fe1SAndy Fiddaman #include <machine/iodev.h>
43*5c4a5fe1SAndy Fiddaman 
44*5c4a5fe1SAndy Fiddaman #include <stdio.h>
45*5c4a5fe1SAndy Fiddaman #include <stdlib.h>
46*5c4a5fe1SAndy Fiddaman #include <string.h>
47*5c4a5fe1SAndy Fiddaman #include <err.h>
48*5c4a5fe1SAndy Fiddaman #include <errno.h>
49*5c4a5fe1SAndy Fiddaman #include <fcntl.h>
50*5c4a5fe1SAndy Fiddaman #include <sysexits.h>
51*5c4a5fe1SAndy Fiddaman #include <unistd.h>
52*5c4a5fe1SAndy Fiddaman 
53*5c4a5fe1SAndy Fiddaman #include <machine/vmm.h>
54*5c4a5fe1SAndy Fiddaman #include <vmmapi.h>
55*5c4a5fe1SAndy Fiddaman #include <sys/ppt_dev.h>
56*5c4a5fe1SAndy Fiddaman 
57*5c4a5fe1SAndy Fiddaman #include "config.h"
58*5c4a5fe1SAndy Fiddaman #include "debug.h"
59*5c4a5fe1SAndy Fiddaman #include "pci_passthru.h"
60*5c4a5fe1SAndy Fiddaman #include "mem.h"
61*5c4a5fe1SAndy Fiddaman 
62*5c4a5fe1SAndy Fiddaman #define	LEGACY_SUPPORT	1
63*5c4a5fe1SAndy Fiddaman 
64*5c4a5fe1SAndy Fiddaman #define MSIX_TABLE_COUNT(ctrl) (((ctrl) & PCIM_MSIXCTRL_TABLE_SIZE) + 1)
65*5c4a5fe1SAndy Fiddaman #define MSIX_CAPLEN 12
66*5c4a5fe1SAndy Fiddaman 
67*5c4a5fe1SAndy Fiddaman struct passthru_softc {
68*5c4a5fe1SAndy Fiddaman 	struct pci_devinst *psc_pi;
69*5c4a5fe1SAndy Fiddaman 	/* ROM is handled like a BAR */
70*5c4a5fe1SAndy Fiddaman 	struct pcibar psc_bar[PCI_BARMAX_WITH_ROM + 1];
71*5c4a5fe1SAndy Fiddaman 	struct {
72*5c4a5fe1SAndy Fiddaman 		int		capoff;
73*5c4a5fe1SAndy Fiddaman 		int		msgctrl;
74*5c4a5fe1SAndy Fiddaman 		int		emulated;
75*5c4a5fe1SAndy Fiddaman 	} psc_msi;
76*5c4a5fe1SAndy Fiddaman 	struct {
77*5c4a5fe1SAndy Fiddaman 		int		capoff;
78*5c4a5fe1SAndy Fiddaman 	} psc_msix;
79*5c4a5fe1SAndy Fiddaman 	int pptfd;
80*5c4a5fe1SAndy Fiddaman 	int msi_limit;
81*5c4a5fe1SAndy Fiddaman 	int msix_limit;
82*5c4a5fe1SAndy Fiddaman 
83*5c4a5fe1SAndy Fiddaman 	cfgread_handler psc_pcir_rhandler[PCI_REGMAX + 1];
84*5c4a5fe1SAndy Fiddaman 	cfgwrite_handler psc_pcir_whandler[PCI_REGMAX + 1];
85*5c4a5fe1SAndy Fiddaman };
86*5c4a5fe1SAndy Fiddaman 
87*5c4a5fe1SAndy Fiddaman static int
msi_caplen(int msgctrl)88*5c4a5fe1SAndy Fiddaman msi_caplen(int msgctrl)
89*5c4a5fe1SAndy Fiddaman {
90*5c4a5fe1SAndy Fiddaman 	int len;
91*5c4a5fe1SAndy Fiddaman 
92*5c4a5fe1SAndy Fiddaman 	len = 10;		/* minimum length of msi capability */
93*5c4a5fe1SAndy Fiddaman 
94*5c4a5fe1SAndy Fiddaman 	if (msgctrl & PCIM_MSICTRL_64BIT)
95*5c4a5fe1SAndy Fiddaman 		len += 4;
96*5c4a5fe1SAndy Fiddaman 
97*5c4a5fe1SAndy Fiddaman #if 0
98*5c4a5fe1SAndy Fiddaman 	/*
99*5c4a5fe1SAndy Fiddaman 	 * Ignore the 'mask' and 'pending' bits in the MSI capability.
100*5c4a5fe1SAndy Fiddaman 	 * We'll let the guest manipulate them directly.
101*5c4a5fe1SAndy Fiddaman 	 */
102*5c4a5fe1SAndy Fiddaman 	if (msgctrl & PCIM_MSICTRL_VECTOR)
103*5c4a5fe1SAndy Fiddaman 		len += 10;
104*5c4a5fe1SAndy Fiddaman #endif
105*5c4a5fe1SAndy Fiddaman 
106*5c4a5fe1SAndy Fiddaman 	return (len);
107*5c4a5fe1SAndy Fiddaman }
108*5c4a5fe1SAndy Fiddaman 
109*5c4a5fe1SAndy Fiddaman static uint32_t
passthru_read_config(const struct passthru_softc * sc,long reg,int width)110*5c4a5fe1SAndy Fiddaman passthru_read_config(const struct passthru_softc *sc, long reg, int width)
111*5c4a5fe1SAndy Fiddaman {
112*5c4a5fe1SAndy Fiddaman 	struct ppt_cfg_io pi;
113*5c4a5fe1SAndy Fiddaman 
114*5c4a5fe1SAndy Fiddaman 	pi.pci_off = reg;
115*5c4a5fe1SAndy Fiddaman 	pi.pci_width = width;
116*5c4a5fe1SAndy Fiddaman 
117*5c4a5fe1SAndy Fiddaman 	if (ioctl(sc->pptfd, PPT_CFG_READ, &pi) != 0) {
118*5c4a5fe1SAndy Fiddaman 		return (0);
119*5c4a5fe1SAndy Fiddaman 	}
120*5c4a5fe1SAndy Fiddaman 	return (pi.pci_data);
121*5c4a5fe1SAndy Fiddaman }
122*5c4a5fe1SAndy Fiddaman 
123*5c4a5fe1SAndy Fiddaman static void
passthru_write_config(const struct passthru_softc * sc,long reg,int width,uint32_t data)124*5c4a5fe1SAndy Fiddaman passthru_write_config(const struct passthru_softc *sc, long reg, int width,
125*5c4a5fe1SAndy Fiddaman     uint32_t data)
126*5c4a5fe1SAndy Fiddaman {
127*5c4a5fe1SAndy Fiddaman 	struct ppt_cfg_io pi;
128*5c4a5fe1SAndy Fiddaman 
129*5c4a5fe1SAndy Fiddaman 	pi.pci_off = reg;
130*5c4a5fe1SAndy Fiddaman 	pi.pci_width = width;
131*5c4a5fe1SAndy Fiddaman 	pi.pci_data = data;
132*5c4a5fe1SAndy Fiddaman 
133*5c4a5fe1SAndy Fiddaman 	(void) ioctl(sc->pptfd, PPT_CFG_WRITE, &pi);
134*5c4a5fe1SAndy Fiddaman }
135*5c4a5fe1SAndy Fiddaman 
136*5c4a5fe1SAndy Fiddaman static int
passthru_get_bar(struct passthru_softc * sc,int bar,enum pcibar_type * type,uint64_t * base,uint64_t * size)137*5c4a5fe1SAndy Fiddaman passthru_get_bar(struct passthru_softc *sc, int bar, enum pcibar_type *type,
138*5c4a5fe1SAndy Fiddaman     uint64_t *base, uint64_t *size)
139*5c4a5fe1SAndy Fiddaman {
140*5c4a5fe1SAndy Fiddaman 	struct ppt_bar_query pb;
141*5c4a5fe1SAndy Fiddaman 
142*5c4a5fe1SAndy Fiddaman 	pb.pbq_baridx = bar;
143*5c4a5fe1SAndy Fiddaman 
144*5c4a5fe1SAndy Fiddaman 	if (ioctl(sc->pptfd, PPT_BAR_QUERY, &pb) != 0) {
145*5c4a5fe1SAndy Fiddaman 		return (-1);
146*5c4a5fe1SAndy Fiddaman 	}
147*5c4a5fe1SAndy Fiddaman 
148*5c4a5fe1SAndy Fiddaman 	switch (pb.pbq_type) {
149*5c4a5fe1SAndy Fiddaman 	case PCI_ADDR_IO:
150*5c4a5fe1SAndy Fiddaman 		*type = PCIBAR_IO;
151*5c4a5fe1SAndy Fiddaman 		break;
152*5c4a5fe1SAndy Fiddaman 	case PCI_ADDR_MEM32:
153*5c4a5fe1SAndy Fiddaman 		*type = PCIBAR_MEM32;
154*5c4a5fe1SAndy Fiddaman 		break;
155*5c4a5fe1SAndy Fiddaman 	case PCI_ADDR_MEM64:
156*5c4a5fe1SAndy Fiddaman 		*type = PCIBAR_MEM64;
157*5c4a5fe1SAndy Fiddaman 		break;
158*5c4a5fe1SAndy Fiddaman 	default:
159*5c4a5fe1SAndy Fiddaman 		err(1, "unrecognized BAR type: %u\n", pb.pbq_type);
160*5c4a5fe1SAndy Fiddaman 		break;
161*5c4a5fe1SAndy Fiddaman 	}
162*5c4a5fe1SAndy Fiddaman 
163*5c4a5fe1SAndy Fiddaman 	*base = pb.pbq_base;
164*5c4a5fe1SAndy Fiddaman 	*size = pb.pbq_size;
165*5c4a5fe1SAndy Fiddaman 	return (0);
166*5c4a5fe1SAndy Fiddaman }
167*5c4a5fe1SAndy Fiddaman 
168*5c4a5fe1SAndy Fiddaman static int
passthru_dev_open(const char * path,int * pptfdp)169*5c4a5fe1SAndy Fiddaman passthru_dev_open(const char *path, int *pptfdp)
170*5c4a5fe1SAndy Fiddaman {
171*5c4a5fe1SAndy Fiddaman 	int pptfd;
172*5c4a5fe1SAndy Fiddaman 
173*5c4a5fe1SAndy Fiddaman 	if ((pptfd = open(path, O_RDWR)) < 0) {
174*5c4a5fe1SAndy Fiddaman 		return (errno);
175*5c4a5fe1SAndy Fiddaman 	}
176*5c4a5fe1SAndy Fiddaman 
177*5c4a5fe1SAndy Fiddaman 	/* XXX: verify fd with ioctl? */
178*5c4a5fe1SAndy Fiddaman 	*pptfdp = pptfd;
179*5c4a5fe1SAndy Fiddaman 	return (0);
180*5c4a5fe1SAndy Fiddaman }
181*5c4a5fe1SAndy Fiddaman 
182*5c4a5fe1SAndy Fiddaman #ifdef LEGACY_SUPPORT
183*5c4a5fe1SAndy Fiddaman static int
passthru_add_msicap(struct pci_devinst * pi,int msgnum,int nextptr)184*5c4a5fe1SAndy Fiddaman passthru_add_msicap(struct pci_devinst *pi, int msgnum, int nextptr)
185*5c4a5fe1SAndy Fiddaman {
186*5c4a5fe1SAndy Fiddaman 	int capoff;
187*5c4a5fe1SAndy Fiddaman 	struct msicap msicap;
188*5c4a5fe1SAndy Fiddaman 	u_char *capdata;
189*5c4a5fe1SAndy Fiddaman 
190*5c4a5fe1SAndy Fiddaman 	pci_populate_msicap(&msicap, msgnum, nextptr);
191*5c4a5fe1SAndy Fiddaman 
192*5c4a5fe1SAndy Fiddaman 	/*
193*5c4a5fe1SAndy Fiddaman 	 * XXX
194*5c4a5fe1SAndy Fiddaman 	 * Copy the msi capability structure in the last 16 bytes of the
195*5c4a5fe1SAndy Fiddaman 	 * config space. This is wrong because it could shadow something
196*5c4a5fe1SAndy Fiddaman 	 * useful to the device.
197*5c4a5fe1SAndy Fiddaman 	 */
198*5c4a5fe1SAndy Fiddaman 	capoff = 256 - roundup(sizeof(msicap), 4);
199*5c4a5fe1SAndy Fiddaman 	capdata = (u_char *)&msicap;
200*5c4a5fe1SAndy Fiddaman 	for (size_t i = 0; i < sizeof(msicap); i++)
201*5c4a5fe1SAndy Fiddaman 		pci_set_cfgdata8(pi, capoff + i, capdata[i]);
202*5c4a5fe1SAndy Fiddaman 
203*5c4a5fe1SAndy Fiddaman 	return (capoff);
204*5c4a5fe1SAndy Fiddaman }
205*5c4a5fe1SAndy Fiddaman #endif	/* LEGACY_SUPPORT */
206*5c4a5fe1SAndy Fiddaman 
207*5c4a5fe1SAndy Fiddaman static void
passthru_intr_limit(struct passthru_softc * sc,struct msixcap * msixcap)208*5c4a5fe1SAndy Fiddaman passthru_intr_limit(struct passthru_softc *sc, struct msixcap *msixcap)
209*5c4a5fe1SAndy Fiddaman {
210*5c4a5fe1SAndy Fiddaman 	struct pci_devinst *pi = sc->psc_pi;
211*5c4a5fe1SAndy Fiddaman 	int off;
212*5c4a5fe1SAndy Fiddaman 
213*5c4a5fe1SAndy Fiddaman 	/* Reduce the number of MSI vectors if higher than OS limit */
214*5c4a5fe1SAndy Fiddaman 	if ((off = sc->psc_msi.capoff) != 0 && sc->msi_limit != -1) {
215*5c4a5fe1SAndy Fiddaman 		int msi_limit, mmc;
216*5c4a5fe1SAndy Fiddaman 
217*5c4a5fe1SAndy Fiddaman 		msi_limit =
218*5c4a5fe1SAndy Fiddaman 		    sc->msi_limit > 16 ? PCIM_MSICTRL_MMC_32 :
219*5c4a5fe1SAndy Fiddaman 		    sc->msi_limit > 8 ? PCIM_MSICTRL_MMC_16 :
220*5c4a5fe1SAndy Fiddaman 		    sc->msi_limit > 4 ? PCIM_MSICTRL_MMC_8 :
221*5c4a5fe1SAndy Fiddaman 		    sc->msi_limit > 2 ? PCIM_MSICTRL_MMC_4 :
222*5c4a5fe1SAndy Fiddaman 		    sc->msi_limit > 1 ? PCIM_MSICTRL_MMC_2 :
223*5c4a5fe1SAndy Fiddaman 		    PCIM_MSICTRL_MMC_1;
224*5c4a5fe1SAndy Fiddaman 		mmc = sc->psc_msi.msgctrl & PCIM_MSICTRL_MMC_MASK;
225*5c4a5fe1SAndy Fiddaman 
226*5c4a5fe1SAndy Fiddaman 		if (mmc > msi_limit) {
227*5c4a5fe1SAndy Fiddaman 			sc->psc_msi.msgctrl &= ~PCIM_MSICTRL_MMC_MASK;
228*5c4a5fe1SAndy Fiddaman 			sc->psc_msi.msgctrl |= msi_limit;
229*5c4a5fe1SAndy Fiddaman 			pci_set_cfgdata16(pi, off + 2, sc->psc_msi.msgctrl);
230*5c4a5fe1SAndy Fiddaman 		}
231*5c4a5fe1SAndy Fiddaman 	}
232*5c4a5fe1SAndy Fiddaman 
233*5c4a5fe1SAndy Fiddaman 	/* Reduce the number of MSI-X vectors if higher than OS limit */
234*5c4a5fe1SAndy Fiddaman 	if ((off = sc->psc_msix.capoff) != 0 && sc->msix_limit != -1) {
235*5c4a5fe1SAndy Fiddaman 		if (MSIX_TABLE_COUNT(msixcap->msgctrl) > sc->msix_limit) {
236*5c4a5fe1SAndy Fiddaman 			msixcap->msgctrl &= ~PCIM_MSIXCTRL_TABLE_SIZE;
237*5c4a5fe1SAndy Fiddaman 			msixcap->msgctrl |= sc->msix_limit - 1;
238*5c4a5fe1SAndy Fiddaman 			pci_set_cfgdata16(pi, off + 2, msixcap->msgctrl);
239*5c4a5fe1SAndy Fiddaman 		}
240*5c4a5fe1SAndy Fiddaman 	}
241*5c4a5fe1SAndy Fiddaman }
242*5c4a5fe1SAndy Fiddaman 
243*5c4a5fe1SAndy Fiddaman static int
cfginitmsi(struct passthru_softc * sc)244*5c4a5fe1SAndy Fiddaman cfginitmsi(struct passthru_softc *sc)
245*5c4a5fe1SAndy Fiddaman {
246*5c4a5fe1SAndy Fiddaman 	int i, ptr, capptr, cap, sts, caplen, table_size;
247*5c4a5fe1SAndy Fiddaman 	uint32_t u32;
248*5c4a5fe1SAndy Fiddaman 	struct pci_devinst *pi = sc->psc_pi;
249*5c4a5fe1SAndy Fiddaman 	struct msixcap msixcap;
250*5c4a5fe1SAndy Fiddaman 	char *msixcap_ptr;
251*5c4a5fe1SAndy Fiddaman 
252*5c4a5fe1SAndy Fiddaman 	/*
253*5c4a5fe1SAndy Fiddaman 	 * Parse the capabilities and cache the location of the MSI
254*5c4a5fe1SAndy Fiddaman 	 * and MSI-X capabilities.
255*5c4a5fe1SAndy Fiddaman 	 */
256*5c4a5fe1SAndy Fiddaman 	sts = passthru_read_config(sc, PCIR_STATUS, 2);
257*5c4a5fe1SAndy Fiddaman 	if (sts & PCIM_STATUS_CAPPRESENT) {
258*5c4a5fe1SAndy Fiddaman 		ptr = passthru_read_config(sc, PCIR_CAP_PTR, 1);
259*5c4a5fe1SAndy Fiddaman 		while (ptr != 0 && ptr != 0xff) {
260*5c4a5fe1SAndy Fiddaman 			cap = passthru_read_config(sc, ptr + PCICAP_ID, 1);
261*5c4a5fe1SAndy Fiddaman 			if (cap == PCIY_MSI) {
262*5c4a5fe1SAndy Fiddaman 				/*
263*5c4a5fe1SAndy Fiddaman 				 * Copy the MSI capability into the config
264*5c4a5fe1SAndy Fiddaman 				 * space of the emulated pci device
265*5c4a5fe1SAndy Fiddaman 				 */
266*5c4a5fe1SAndy Fiddaman 				sc->psc_msi.capoff = ptr;
267*5c4a5fe1SAndy Fiddaman 				sc->psc_msi.msgctrl = passthru_read_config(sc,
268*5c4a5fe1SAndy Fiddaman 				    ptr + 2, 2);
269*5c4a5fe1SAndy Fiddaman 				sc->psc_msi.emulated = 0;
270*5c4a5fe1SAndy Fiddaman 				caplen = msi_caplen(sc->psc_msi.msgctrl);
271*5c4a5fe1SAndy Fiddaman 				capptr = ptr;
272*5c4a5fe1SAndy Fiddaman 				while (caplen > 0) {
273*5c4a5fe1SAndy Fiddaman 					u32 = passthru_read_config(sc,
274*5c4a5fe1SAndy Fiddaman 					    capptr, 4);
275*5c4a5fe1SAndy Fiddaman 					pci_set_cfgdata32(pi, capptr, u32);
276*5c4a5fe1SAndy Fiddaman 					caplen -= 4;
277*5c4a5fe1SAndy Fiddaman 					capptr += 4;
278*5c4a5fe1SAndy Fiddaman 				}
279*5c4a5fe1SAndy Fiddaman 			} else if (cap == PCIY_MSIX) {
280*5c4a5fe1SAndy Fiddaman 				/*
281*5c4a5fe1SAndy Fiddaman 				 * Copy the MSI-X capability
282*5c4a5fe1SAndy Fiddaman 				 */
283*5c4a5fe1SAndy Fiddaman 				sc->psc_msix.capoff = ptr;
284*5c4a5fe1SAndy Fiddaman 				caplen = 12;
285*5c4a5fe1SAndy Fiddaman 				msixcap_ptr = (char *)&msixcap;
286*5c4a5fe1SAndy Fiddaman 				capptr = ptr;
287*5c4a5fe1SAndy Fiddaman 				while (caplen > 0) {
288*5c4a5fe1SAndy Fiddaman 					u32 = passthru_read_config(sc,
289*5c4a5fe1SAndy Fiddaman 					    capptr, 4);
290*5c4a5fe1SAndy Fiddaman 					memcpy(msixcap_ptr, &u32, 4);
291*5c4a5fe1SAndy Fiddaman 					pci_set_cfgdata32(pi, capptr, u32);
292*5c4a5fe1SAndy Fiddaman 					caplen -= 4;
293*5c4a5fe1SAndy Fiddaman 					capptr += 4;
294*5c4a5fe1SAndy Fiddaman 					msixcap_ptr += 4;
295*5c4a5fe1SAndy Fiddaman 				}
296*5c4a5fe1SAndy Fiddaman 			}
297*5c4a5fe1SAndy Fiddaman 			ptr = passthru_read_config(sc, ptr + PCICAP_NEXTPTR, 1);
298*5c4a5fe1SAndy Fiddaman 		}
299*5c4a5fe1SAndy Fiddaman 	}
300*5c4a5fe1SAndy Fiddaman 
301*5c4a5fe1SAndy Fiddaman 	passthru_intr_limit(sc, &msixcap);
302*5c4a5fe1SAndy Fiddaman 
303*5c4a5fe1SAndy Fiddaman 	if (sc->psc_msix.capoff != 0) {
304*5c4a5fe1SAndy Fiddaman 		pi->pi_msix.pba_bar =
305*5c4a5fe1SAndy Fiddaman 		    msixcap.pba_info & PCIM_MSIX_BIR_MASK;
306*5c4a5fe1SAndy Fiddaman 		pi->pi_msix.pba_offset =
307*5c4a5fe1SAndy Fiddaman 		    msixcap.pba_info & ~PCIM_MSIX_BIR_MASK;
308*5c4a5fe1SAndy Fiddaman 		pi->pi_msix.table_bar =
309*5c4a5fe1SAndy Fiddaman 		    msixcap.table_info & PCIM_MSIX_BIR_MASK;
310*5c4a5fe1SAndy Fiddaman 		pi->pi_msix.table_offset =
311*5c4a5fe1SAndy Fiddaman 		    msixcap.table_info & ~PCIM_MSIX_BIR_MASK;
312*5c4a5fe1SAndy Fiddaman 		pi->pi_msix.table_count = MSIX_TABLE_COUNT(msixcap.msgctrl);
313*5c4a5fe1SAndy Fiddaman 		pi->pi_msix.pba_size = PBA_SIZE(pi->pi_msix.table_count);
314*5c4a5fe1SAndy Fiddaman 
315*5c4a5fe1SAndy Fiddaman 		/* Allocate the emulated MSI-X table array */
316*5c4a5fe1SAndy Fiddaman 		table_size = pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
317*5c4a5fe1SAndy Fiddaman 		pi->pi_msix.table = calloc(1, table_size);
318*5c4a5fe1SAndy Fiddaman 
319*5c4a5fe1SAndy Fiddaman 		/* Mask all table entries */
320*5c4a5fe1SAndy Fiddaman 		for (i = 0; i < pi->pi_msix.table_count; i++) {
321*5c4a5fe1SAndy Fiddaman 			pi->pi_msix.table[i].vector_control |=
322*5c4a5fe1SAndy Fiddaman 						PCIM_MSIX_VCTRL_MASK;
323*5c4a5fe1SAndy Fiddaman 		}
324*5c4a5fe1SAndy Fiddaman 	}
325*5c4a5fe1SAndy Fiddaman 
326*5c4a5fe1SAndy Fiddaman #ifdef LEGACY_SUPPORT
327*5c4a5fe1SAndy Fiddaman 	/*
328*5c4a5fe1SAndy Fiddaman 	 * If the passthrough device does not support MSI then craft a
329*5c4a5fe1SAndy Fiddaman 	 * MSI capability for it. We link the new MSI capability at the
330*5c4a5fe1SAndy Fiddaman 	 * head of the list of capabilities.
331*5c4a5fe1SAndy Fiddaman 	 */
332*5c4a5fe1SAndy Fiddaman 	if ((sts & PCIM_STATUS_CAPPRESENT) != 0 && sc->psc_msi.capoff == 0) {
333*5c4a5fe1SAndy Fiddaman 		int origptr, msiptr;
334*5c4a5fe1SAndy Fiddaman 		origptr = passthru_read_config(sc, PCIR_CAP_PTR, 1);
335*5c4a5fe1SAndy Fiddaman 		msiptr = passthru_add_msicap(pi, 1, origptr);
336*5c4a5fe1SAndy Fiddaman 		sc->psc_msi.capoff = msiptr;
337*5c4a5fe1SAndy Fiddaman 		sc->psc_msi.msgctrl = pci_get_cfgdata16(pi, msiptr + 2);
338*5c4a5fe1SAndy Fiddaman 		sc->psc_msi.emulated = 1;
339*5c4a5fe1SAndy Fiddaman 		pci_set_cfgdata8(pi, PCIR_CAP_PTR, msiptr);
340*5c4a5fe1SAndy Fiddaman 	}
341*5c4a5fe1SAndy Fiddaman #endif
342*5c4a5fe1SAndy Fiddaman 
343*5c4a5fe1SAndy Fiddaman 	/* Make sure one of the capabilities is present */
344*5c4a5fe1SAndy Fiddaman 	if (sc->psc_msi.capoff == 0 && sc->psc_msix.capoff == 0)
345*5c4a5fe1SAndy Fiddaman 		return (-1);
346*5c4a5fe1SAndy Fiddaman 	else
347*5c4a5fe1SAndy Fiddaman 		return (0);
348*5c4a5fe1SAndy Fiddaman }
349*5c4a5fe1SAndy Fiddaman 
350*5c4a5fe1SAndy Fiddaman static uint64_t
msix_table_read(struct passthru_softc * sc,uint64_t offset,int size)351*5c4a5fe1SAndy Fiddaman msix_table_read(struct passthru_softc *sc, uint64_t offset, int size)
352*5c4a5fe1SAndy Fiddaman {
353*5c4a5fe1SAndy Fiddaman 	struct pci_devinst *pi;
354*5c4a5fe1SAndy Fiddaman 	struct msix_table_entry *entry;
355*5c4a5fe1SAndy Fiddaman 	uint8_t *src8;
356*5c4a5fe1SAndy Fiddaman 	uint16_t *src16;
357*5c4a5fe1SAndy Fiddaman 	uint32_t *src32;
358*5c4a5fe1SAndy Fiddaman 	uint64_t *src64;
359*5c4a5fe1SAndy Fiddaman 	uint64_t data;
360*5c4a5fe1SAndy Fiddaman 	size_t entry_offset;
361*5c4a5fe1SAndy Fiddaman 	uint32_t table_offset;
362*5c4a5fe1SAndy Fiddaman 	int index, table_count;
363*5c4a5fe1SAndy Fiddaman 
364*5c4a5fe1SAndy Fiddaman 	pi = sc->psc_pi;
365*5c4a5fe1SAndy Fiddaman 
366*5c4a5fe1SAndy Fiddaman 	table_offset = pi->pi_msix.table_offset;
367*5c4a5fe1SAndy Fiddaman 	table_count = pi->pi_msix.table_count;
368*5c4a5fe1SAndy Fiddaman 	if (offset < table_offset ||
369*5c4a5fe1SAndy Fiddaman 	    offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) {
370*5c4a5fe1SAndy Fiddaman 		switch (size) {
371*5c4a5fe1SAndy Fiddaman 		case 1:
372*5c4a5fe1SAndy Fiddaman 			src8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset);
373*5c4a5fe1SAndy Fiddaman 			data = *src8;
374*5c4a5fe1SAndy Fiddaman 			break;
375*5c4a5fe1SAndy Fiddaman 		case 2:
376*5c4a5fe1SAndy Fiddaman 			src16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset);
377*5c4a5fe1SAndy Fiddaman 			data = *src16;
378*5c4a5fe1SAndy Fiddaman 			break;
379*5c4a5fe1SAndy Fiddaman 		case 4:
380*5c4a5fe1SAndy Fiddaman 			src32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset);
381*5c4a5fe1SAndy Fiddaman 			data = *src32;
382*5c4a5fe1SAndy Fiddaman 			break;
383*5c4a5fe1SAndy Fiddaman 		case 8:
384*5c4a5fe1SAndy Fiddaman 			src64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset);
385*5c4a5fe1SAndy Fiddaman 			data = *src64;
386*5c4a5fe1SAndy Fiddaman 			break;
387*5c4a5fe1SAndy Fiddaman 		default:
388*5c4a5fe1SAndy Fiddaman 			return (-1);
389*5c4a5fe1SAndy Fiddaman 		}
390*5c4a5fe1SAndy Fiddaman 		return (data);
391*5c4a5fe1SAndy Fiddaman 	}
392*5c4a5fe1SAndy Fiddaman 
393*5c4a5fe1SAndy Fiddaman 	offset -= table_offset;
394*5c4a5fe1SAndy Fiddaman 	index = offset / MSIX_TABLE_ENTRY_SIZE;
395*5c4a5fe1SAndy Fiddaman 	assert(index < table_count);
396*5c4a5fe1SAndy Fiddaman 
397*5c4a5fe1SAndy Fiddaman 	entry = &pi->pi_msix.table[index];
398*5c4a5fe1SAndy Fiddaman 	entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
399*5c4a5fe1SAndy Fiddaman 
400*5c4a5fe1SAndy Fiddaman 	switch (size) {
401*5c4a5fe1SAndy Fiddaman 	case 1:
402*5c4a5fe1SAndy Fiddaman 		src8 = (uint8_t *)((uint8_t *)entry + entry_offset);
403*5c4a5fe1SAndy Fiddaman 		data = *src8;
404*5c4a5fe1SAndy Fiddaman 		break;
405*5c4a5fe1SAndy Fiddaman 	case 2:
406*5c4a5fe1SAndy Fiddaman 		src16 = (uint16_t *)((uint8_t *)entry + entry_offset);
407*5c4a5fe1SAndy Fiddaman 		data = *src16;
408*5c4a5fe1SAndy Fiddaman 		break;
409*5c4a5fe1SAndy Fiddaman 	case 4:
410*5c4a5fe1SAndy Fiddaman 		src32 = (uint32_t *)((uint8_t *)entry + entry_offset);
411*5c4a5fe1SAndy Fiddaman 		data = *src32;
412*5c4a5fe1SAndy Fiddaman 		break;
413*5c4a5fe1SAndy Fiddaman 	case 8:
414*5c4a5fe1SAndy Fiddaman 		src64 = (uint64_t *)((uint8_t *)entry + entry_offset);
415*5c4a5fe1SAndy Fiddaman 		data = *src64;
416*5c4a5fe1SAndy Fiddaman 		break;
417*5c4a5fe1SAndy Fiddaman 	default:
418*5c4a5fe1SAndy Fiddaman 		return (-1);
419*5c4a5fe1SAndy Fiddaman 	}
420*5c4a5fe1SAndy Fiddaman 
421*5c4a5fe1SAndy Fiddaman 	return (data);
422*5c4a5fe1SAndy Fiddaman }
423*5c4a5fe1SAndy Fiddaman 
424*5c4a5fe1SAndy Fiddaman static void
msix_table_write(struct vmctx * ctx,struct passthru_softc * sc,uint64_t offset,int size,uint64_t data)425*5c4a5fe1SAndy Fiddaman msix_table_write(struct vmctx *ctx, struct passthru_softc *sc,
426*5c4a5fe1SAndy Fiddaman 		 uint64_t offset, int size, uint64_t data)
427*5c4a5fe1SAndy Fiddaman {
428*5c4a5fe1SAndy Fiddaman 	struct pci_devinst *pi;
429*5c4a5fe1SAndy Fiddaman 	struct msix_table_entry *entry;
430*5c4a5fe1SAndy Fiddaman 	uint8_t *dest8;
431*5c4a5fe1SAndy Fiddaman 	uint16_t *dest16;
432*5c4a5fe1SAndy Fiddaman 	uint32_t *dest32;
433*5c4a5fe1SAndy Fiddaman 	uint64_t *dest64;
434*5c4a5fe1SAndy Fiddaman 	size_t entry_offset;
435*5c4a5fe1SAndy Fiddaman 	uint32_t table_offset, vector_control;
436*5c4a5fe1SAndy Fiddaman 	int index, table_count;
437*5c4a5fe1SAndy Fiddaman 
438*5c4a5fe1SAndy Fiddaman 	pi = sc->psc_pi;
439*5c4a5fe1SAndy Fiddaman 
440*5c4a5fe1SAndy Fiddaman 	table_offset = pi->pi_msix.table_offset;
441*5c4a5fe1SAndy Fiddaman 	table_count = pi->pi_msix.table_count;
442*5c4a5fe1SAndy Fiddaman 	if (offset < table_offset ||
443*5c4a5fe1SAndy Fiddaman 	    offset >= table_offset + table_count * MSIX_TABLE_ENTRY_SIZE) {
444*5c4a5fe1SAndy Fiddaman 		switch (size) {
445*5c4a5fe1SAndy Fiddaman 		case 1:
446*5c4a5fe1SAndy Fiddaman 			dest8 = (uint8_t *)(pi->pi_msix.mapped_addr + offset);
447*5c4a5fe1SAndy Fiddaman 			*dest8 = data;
448*5c4a5fe1SAndy Fiddaman 			break;
449*5c4a5fe1SAndy Fiddaman 		case 2:
450*5c4a5fe1SAndy Fiddaman 			dest16 = (uint16_t *)(pi->pi_msix.mapped_addr + offset);
451*5c4a5fe1SAndy Fiddaman 			*dest16 = data;
452*5c4a5fe1SAndy Fiddaman 			break;
453*5c4a5fe1SAndy Fiddaman 		case 4:
454*5c4a5fe1SAndy Fiddaman 			dest32 = (uint32_t *)(pi->pi_msix.mapped_addr + offset);
455*5c4a5fe1SAndy Fiddaman 			*dest32 = data;
456*5c4a5fe1SAndy Fiddaman 			break;
457*5c4a5fe1SAndy Fiddaman 		case 8:
458*5c4a5fe1SAndy Fiddaman 			dest64 = (uint64_t *)(pi->pi_msix.mapped_addr + offset);
459*5c4a5fe1SAndy Fiddaman 			*dest64 = data;
460*5c4a5fe1SAndy Fiddaman 			break;
461*5c4a5fe1SAndy Fiddaman 		}
462*5c4a5fe1SAndy Fiddaman 		return;
463*5c4a5fe1SAndy Fiddaman 	}
464*5c4a5fe1SAndy Fiddaman 
465*5c4a5fe1SAndy Fiddaman 	offset -= table_offset;
466*5c4a5fe1SAndy Fiddaman 	index = offset / MSIX_TABLE_ENTRY_SIZE;
467*5c4a5fe1SAndy Fiddaman 	assert(index < table_count);
468*5c4a5fe1SAndy Fiddaman 
469*5c4a5fe1SAndy Fiddaman 	entry = &pi->pi_msix.table[index];
470*5c4a5fe1SAndy Fiddaman 	entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
471*5c4a5fe1SAndy Fiddaman 
472*5c4a5fe1SAndy Fiddaman 	/* Only 4 byte naturally-aligned writes are supported */
473*5c4a5fe1SAndy Fiddaman 	assert(size == 4);
474*5c4a5fe1SAndy Fiddaman 	assert(entry_offset % 4 == 0);
475*5c4a5fe1SAndy Fiddaman 
476*5c4a5fe1SAndy Fiddaman 	vector_control = entry->vector_control;
477*5c4a5fe1SAndy Fiddaman 	dest32 = (uint32_t *)((uint8_t *)entry + entry_offset);
478*5c4a5fe1SAndy Fiddaman 	*dest32 = data;
479*5c4a5fe1SAndy Fiddaman 	/* If MSI-X hasn't been enabled, do nothing */
480*5c4a5fe1SAndy Fiddaman 	if (pi->pi_msix.enabled) {
481*5c4a5fe1SAndy Fiddaman 		/* If the entry is masked, don't set it up */
482*5c4a5fe1SAndy Fiddaman 		if ((entry->vector_control & PCIM_MSIX_VCTRL_MASK) == 0 ||
483*5c4a5fe1SAndy Fiddaman 		    (vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
484*5c4a5fe1SAndy Fiddaman 			(void) vm_setup_pptdev_msix(ctx, sc->pptfd,
485*5c4a5fe1SAndy Fiddaman 			    index, entry->addr, entry->msg_data,
486*5c4a5fe1SAndy Fiddaman 			    entry->vector_control);
487*5c4a5fe1SAndy Fiddaman 		}
488*5c4a5fe1SAndy Fiddaman 	}
489*5c4a5fe1SAndy Fiddaman }
490*5c4a5fe1SAndy Fiddaman 
491*5c4a5fe1SAndy Fiddaman static int
init_msix_table(struct vmctx * ctx __unused,struct passthru_softc * sc)492*5c4a5fe1SAndy Fiddaman init_msix_table(struct vmctx *ctx __unused, struct passthru_softc *sc)
493*5c4a5fe1SAndy Fiddaman {
494*5c4a5fe1SAndy Fiddaman 	struct pci_devinst *pi = sc->psc_pi;
495*5c4a5fe1SAndy Fiddaman 	uint32_t table_size, table_offset;
496*5c4a5fe1SAndy Fiddaman 	int i;
497*5c4a5fe1SAndy Fiddaman 
498*5c4a5fe1SAndy Fiddaman 	i = pci_msix_table_bar(pi);
499*5c4a5fe1SAndy Fiddaman 	assert(i >= 0);
500*5c4a5fe1SAndy Fiddaman 
501*5c4a5fe1SAndy Fiddaman         /*
502*5c4a5fe1SAndy Fiddaman          * Map the region of the BAR containing the MSI-X table.  This is
503*5c4a5fe1SAndy Fiddaman          * necessary for two reasons:
504*5c4a5fe1SAndy Fiddaman          * 1. The PBA may reside in the first or last page containing the MSI-X
505*5c4a5fe1SAndy Fiddaman          *    table.
506*5c4a5fe1SAndy Fiddaman          * 2. While PCI devices are not supposed to use the page(s) containing
507*5c4a5fe1SAndy Fiddaman          *    the MSI-X table for other purposes, some do in practice.
508*5c4a5fe1SAndy Fiddaman          */
509*5c4a5fe1SAndy Fiddaman 
510*5c4a5fe1SAndy Fiddaman 	/*
511*5c4a5fe1SAndy Fiddaman 	 * Mapping pptfd provides access to the BAR containing the MSI-X
512*5c4a5fe1SAndy Fiddaman 	 * table. See ppt_devmap() in usr/src/uts/intel/io/vmm/io/ppt.c
513*5c4a5fe1SAndy Fiddaman 	 *
514*5c4a5fe1SAndy Fiddaman 	 * This maps the whole BAR and then mprotect(PROT_NONE) is used below
515*5c4a5fe1SAndy Fiddaman 	 * to prevent access to pages that don't contain the MSI-X table.
516*5c4a5fe1SAndy Fiddaman 	 * When porting this, it was tempting to just map the MSI-X table pages
517*5c4a5fe1SAndy Fiddaman 	 * but that would mean updating everywhere that assumes that
518*5c4a5fe1SAndy Fiddaman 	 * pi->pi_msix.mapped_addr points to the start of the BAR. For now,
519*5c4a5fe1SAndy Fiddaman 	 * keep closer to upstream.
520*5c4a5fe1SAndy Fiddaman 	 */
521*5c4a5fe1SAndy Fiddaman 	pi->pi_msix.mapped_size = sc->psc_bar[i].size;
522*5c4a5fe1SAndy Fiddaman 	pi->pi_msix.mapped_addr = (uint8_t *)mmap(NULL, pi->pi_msix.mapped_size,
523*5c4a5fe1SAndy Fiddaman 	    PROT_READ | PROT_WRITE, MAP_SHARED, sc->pptfd, 0);
524*5c4a5fe1SAndy Fiddaman 	if (pi->pi_msix.mapped_addr == MAP_FAILED) {
525*5c4a5fe1SAndy Fiddaman 		warn("Failed to map MSI-X table BAR on %d", sc->pptfd);
526*5c4a5fe1SAndy Fiddaman 		return (-1);
527*5c4a5fe1SAndy Fiddaman 	}
528*5c4a5fe1SAndy Fiddaman 
529*5c4a5fe1SAndy Fiddaman 	table_offset = rounddown2(pi->pi_msix.table_offset, 4096);
530*5c4a5fe1SAndy Fiddaman 
531*5c4a5fe1SAndy Fiddaman 	table_size = pi->pi_msix.table_offset - table_offset;
532*5c4a5fe1SAndy Fiddaman 	table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
533*5c4a5fe1SAndy Fiddaman 	table_size = roundup2(table_size, 4096);
534*5c4a5fe1SAndy Fiddaman 
535*5c4a5fe1SAndy Fiddaman 	/*
536*5c4a5fe1SAndy Fiddaman 	 * Unmap any pages not containing the table, we do not need to emulate
537*5c4a5fe1SAndy Fiddaman 	 * accesses to them.  Avoid releasing address space to help ensure that
538*5c4a5fe1SAndy Fiddaman 	 * a buggy out-of-bounds access causes a crash.
539*5c4a5fe1SAndy Fiddaman 	 */
540*5c4a5fe1SAndy Fiddaman 	if (table_offset != 0)
541*5c4a5fe1SAndy Fiddaman 		if (mprotect((caddr_t)pi->pi_msix.mapped_addr, table_offset,
542*5c4a5fe1SAndy Fiddaman 		    PROT_NONE) != 0)
543*5c4a5fe1SAndy Fiddaman 			warn("Failed to unmap MSI-X table BAR region");
544*5c4a5fe1SAndy Fiddaman 	if (table_offset + table_size != pi->pi_msix.mapped_size)
545*5c4a5fe1SAndy Fiddaman 		if (mprotect((caddr_t)
546*5c4a5fe1SAndy Fiddaman 		    pi->pi_msix.mapped_addr + table_offset + table_size,
547*5c4a5fe1SAndy Fiddaman 		    pi->pi_msix.mapped_size - (table_offset + table_size),
548*5c4a5fe1SAndy Fiddaman 		    PROT_NONE) != 0)
549*5c4a5fe1SAndy Fiddaman 			warn("Failed to unmap MSI-X table BAR region");
550*5c4a5fe1SAndy Fiddaman 
551*5c4a5fe1SAndy Fiddaman 	return (0);
552*5c4a5fe1SAndy Fiddaman }
553*5c4a5fe1SAndy Fiddaman 
554*5c4a5fe1SAndy Fiddaman static int
cfginitbar(struct vmctx * ctx __unused,struct passthru_softc * sc)555*5c4a5fe1SAndy Fiddaman cfginitbar(struct vmctx *ctx __unused, struct passthru_softc *sc)
556*5c4a5fe1SAndy Fiddaman {
557*5c4a5fe1SAndy Fiddaman 	struct pci_devinst *pi = sc->psc_pi;
558*5c4a5fe1SAndy Fiddaman 	uint_t i;
559*5c4a5fe1SAndy Fiddaman 
560*5c4a5fe1SAndy Fiddaman 	/*
561*5c4a5fe1SAndy Fiddaman 	 * Initialize BAR registers
562*5c4a5fe1SAndy Fiddaman 	 */
563*5c4a5fe1SAndy Fiddaman 	for (i = 0; i <= PCI_BARMAX; i++) {
564*5c4a5fe1SAndy Fiddaman 		enum pcibar_type bartype;
565*5c4a5fe1SAndy Fiddaman 		uint64_t base, size;
566*5c4a5fe1SAndy Fiddaman 		int error;
567*5c4a5fe1SAndy Fiddaman 
568*5c4a5fe1SAndy Fiddaman 		if (passthru_get_bar(sc, i, &bartype, &base, &size) != 0) {
569*5c4a5fe1SAndy Fiddaman 			continue;
570*5c4a5fe1SAndy Fiddaman 		}
571*5c4a5fe1SAndy Fiddaman 
572*5c4a5fe1SAndy Fiddaman 		if (bartype != PCIBAR_IO) {
573*5c4a5fe1SAndy Fiddaman 			if (((base | size) & PAGE_MASK) != 0) {
574*5c4a5fe1SAndy Fiddaman 				warnx("passthru device %d BAR %d: "
575*5c4a5fe1SAndy Fiddaman 				    "base %#lx or size %#lx not page aligned\n",
576*5c4a5fe1SAndy Fiddaman 				    sc->pptfd, i, base, size);
577*5c4a5fe1SAndy Fiddaman 				return (-1);
578*5c4a5fe1SAndy Fiddaman 			}
579*5c4a5fe1SAndy Fiddaman 		}
580*5c4a5fe1SAndy Fiddaman 
581*5c4a5fe1SAndy Fiddaman 		/* Cache information about the "real" BAR */
582*5c4a5fe1SAndy Fiddaman 		sc->psc_bar[i].type = bartype;
583*5c4a5fe1SAndy Fiddaman 		sc->psc_bar[i].size = size;
584*5c4a5fe1SAndy Fiddaman 		sc->psc_bar[i].addr = base;
585*5c4a5fe1SAndy Fiddaman 		sc->psc_bar[i].lobits = 0;
586*5c4a5fe1SAndy Fiddaman 
587*5c4a5fe1SAndy Fiddaman 		/* Allocate the BAR in the guest I/O or MMIO space */
588*5c4a5fe1SAndy Fiddaman 		error = pci_emul_alloc_bar(pi, i, bartype, size);
589*5c4a5fe1SAndy Fiddaman 		if (error)
590*5c4a5fe1SAndy Fiddaman 			return (-1);
591*5c4a5fe1SAndy Fiddaman 
592*5c4a5fe1SAndy Fiddaman 		/* Use same lobits as physical bar */
593*5c4a5fe1SAndy Fiddaman 		uint8_t lobits = passthru_read_config(sc, PCIR_BAR(i), 0x01);
594*5c4a5fe1SAndy Fiddaman 		if (bartype == PCIBAR_MEM32 || bartype == PCIBAR_MEM64) {
595*5c4a5fe1SAndy Fiddaman 			lobits &= ~PCIM_BAR_MEM_BASE;
596*5c4a5fe1SAndy Fiddaman 		} else {
597*5c4a5fe1SAndy Fiddaman 			lobits &= ~PCIM_BAR_IO_BASE;
598*5c4a5fe1SAndy Fiddaman 		}
599*5c4a5fe1SAndy Fiddaman 		sc->psc_bar[i].lobits = lobits;
600*5c4a5fe1SAndy Fiddaman 		pi->pi_bar[i].lobits = lobits;
601*5c4a5fe1SAndy Fiddaman 
602*5c4a5fe1SAndy Fiddaman 		/*
603*5c4a5fe1SAndy Fiddaman 		 * 64-bit BAR takes up two slots so skip the next one.
604*5c4a5fe1SAndy Fiddaman 		 */
605*5c4a5fe1SAndy Fiddaman 		if (bartype == PCIBAR_MEM64) {
606*5c4a5fe1SAndy Fiddaman 			i++;
607*5c4a5fe1SAndy Fiddaman 			assert(i <= PCI_BARMAX);
608*5c4a5fe1SAndy Fiddaman 			sc->psc_bar[i].type = PCIBAR_MEMHI64;
609*5c4a5fe1SAndy Fiddaman 		}
610*5c4a5fe1SAndy Fiddaman 	}
611*5c4a5fe1SAndy Fiddaman 	return (0);
612*5c4a5fe1SAndy Fiddaman }
613*5c4a5fe1SAndy Fiddaman 
614*5c4a5fe1SAndy Fiddaman static int
cfginit(struct vmctx * ctx,struct passthru_softc * sc)615*5c4a5fe1SAndy Fiddaman cfginit(struct vmctx *ctx, struct passthru_softc *sc)
616*5c4a5fe1SAndy Fiddaman {
617*5c4a5fe1SAndy Fiddaman 	int error;
618*5c4a5fe1SAndy Fiddaman 	struct pci_devinst *pi = sc->psc_pi;
619*5c4a5fe1SAndy Fiddaman 	uint16_t cmd;
620*5c4a5fe1SAndy Fiddaman 	uint8_t intline, intpin;
621*5c4a5fe1SAndy Fiddaman 
622*5c4a5fe1SAndy Fiddaman 	/*
623*5c4a5fe1SAndy Fiddaman 	 * Copy physical PCI header to virtual config space.  COMMAND,
624*5c4a5fe1SAndy Fiddaman 	 * INTLINE and INTPIN shouldn't be aligned with their physical value
625*5c4a5fe1SAndy Fiddaman 	 * and they are already set by pci_emul_init().
626*5c4a5fe1SAndy Fiddaman 	 */
627*5c4a5fe1SAndy Fiddaman 	cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
628*5c4a5fe1SAndy Fiddaman 	intline = pci_get_cfgdata8(pi, PCIR_INTLINE);
629*5c4a5fe1SAndy Fiddaman 	intpin = pci_get_cfgdata8(pi, PCIR_INTPIN);
630*5c4a5fe1SAndy Fiddaman 	for (int i = 0; i <= PCIR_MAXLAT; i += 4) {
631*5c4a5fe1SAndy Fiddaman #ifdef	__FreeBSD__
632*5c4a5fe1SAndy Fiddaman 		pci_set_cfgdata32(pi, i, read_config(&sc->psc_sel, i, 4));
633*5c4a5fe1SAndy Fiddaman #else
634*5c4a5fe1SAndy Fiddaman 		pci_set_cfgdata32(pi, i, passthru_read_config(sc, i, 4));
635*5c4a5fe1SAndy Fiddaman #endif
636*5c4a5fe1SAndy Fiddaman 	}
637*5c4a5fe1SAndy Fiddaman 
638*5c4a5fe1SAndy Fiddaman 	pci_set_cfgdata16(pi, PCIR_COMMAND, cmd);
639*5c4a5fe1SAndy Fiddaman 	pci_set_cfgdata8(pi, PCIR_INTLINE, intline);
640*5c4a5fe1SAndy Fiddaman 	pci_set_cfgdata8(pi, PCIR_INTPIN, intpin);
641*5c4a5fe1SAndy Fiddaman 
642*5c4a5fe1SAndy Fiddaman 	if (cfginitmsi(sc) != 0) {
643*5c4a5fe1SAndy Fiddaman 		warnx("failed to initialize MSI for PCI %d", sc->pptfd);
644*5c4a5fe1SAndy Fiddaman 		return (-1);
645*5c4a5fe1SAndy Fiddaman 	}
646*5c4a5fe1SAndy Fiddaman 
647*5c4a5fe1SAndy Fiddaman 	if (cfginitbar(ctx, sc) != 0) {
648*5c4a5fe1SAndy Fiddaman 		warnx("failed to initialize BARs for PCI %d", sc->pptfd);
649*5c4a5fe1SAndy Fiddaman 		return (-1);
650*5c4a5fe1SAndy Fiddaman 	}
651*5c4a5fe1SAndy Fiddaman 
652*5c4a5fe1SAndy Fiddaman 	if (pci_msix_table_bar(pi) >= 0) {
653*5c4a5fe1SAndy Fiddaman 		error = init_msix_table(ctx, sc);
654*5c4a5fe1SAndy Fiddaman 		if (error != 0) {
655*5c4a5fe1SAndy Fiddaman 			warnx("failed to initialize MSI-X table for PCI %d",
656*5c4a5fe1SAndy Fiddaman 			    sc->pptfd);
657*5c4a5fe1SAndy Fiddaman 			goto done;
658*5c4a5fe1SAndy Fiddaman 		}
659*5c4a5fe1SAndy Fiddaman 	}
660*5c4a5fe1SAndy Fiddaman 
661*5c4a5fe1SAndy Fiddaman 	/* Emulate most PCI header register. */
662*5c4a5fe1SAndy Fiddaman 	if ((error = set_pcir_handler(sc, 0, PCIR_MAXLAT + 1,
663*5c4a5fe1SAndy Fiddaman 	    passthru_cfgread_emulate, passthru_cfgwrite_emulate)) != 0)
664*5c4a5fe1SAndy Fiddaman 		goto done;
665*5c4a5fe1SAndy Fiddaman 
666*5c4a5fe1SAndy Fiddaman 	/* Allow access to the physical status register. */
667*5c4a5fe1SAndy Fiddaman 	if ((error = set_pcir_handler(sc, PCIR_COMMAND, 0x04, NULL, NULL)) != 0)
668*5c4a5fe1SAndy Fiddaman 		goto done;
669*5c4a5fe1SAndy Fiddaman 
670*5c4a5fe1SAndy Fiddaman 	error = 0;				/* success */
671*5c4a5fe1SAndy Fiddaman done:
672*5c4a5fe1SAndy Fiddaman 	return (error);
673*5c4a5fe1SAndy Fiddaman }
674*5c4a5fe1SAndy Fiddaman 
675*5c4a5fe1SAndy Fiddaman int
set_pcir_handler(struct passthru_softc * sc,int reg,int len,cfgread_handler rhandler,cfgwrite_handler whandler)676*5c4a5fe1SAndy Fiddaman set_pcir_handler(struct passthru_softc *sc, int reg, int len,
677*5c4a5fe1SAndy Fiddaman     cfgread_handler rhandler, cfgwrite_handler whandler)
678*5c4a5fe1SAndy Fiddaman {
679*5c4a5fe1SAndy Fiddaman 	if (reg > PCI_REGMAX || reg + len > PCI_REGMAX + 1)
680*5c4a5fe1SAndy Fiddaman 		return (-1);
681*5c4a5fe1SAndy Fiddaman 
682*5c4a5fe1SAndy Fiddaman 	for (int i = reg; i < reg + len; ++i) {
683*5c4a5fe1SAndy Fiddaman 		assert(sc->psc_pcir_rhandler[i] == NULL || rhandler == NULL);
684*5c4a5fe1SAndy Fiddaman 		assert(sc->psc_pcir_whandler[i] == NULL || whandler == NULL);
685*5c4a5fe1SAndy Fiddaman 		sc->psc_pcir_rhandler[i] = rhandler;
686*5c4a5fe1SAndy Fiddaman 		sc->psc_pcir_whandler[i] = whandler;
687*5c4a5fe1SAndy Fiddaman 	}
688*5c4a5fe1SAndy Fiddaman 
689*5c4a5fe1SAndy Fiddaman 	return (0);
690*5c4a5fe1SAndy Fiddaman }
691*5c4a5fe1SAndy Fiddaman 
692*5c4a5fe1SAndy Fiddaman static int
passthru_legacy_config(nvlist_t * nvl,const char * opt)693*5c4a5fe1SAndy Fiddaman passthru_legacy_config(nvlist_t *nvl, const char *opt)
694*5c4a5fe1SAndy Fiddaman {
695*5c4a5fe1SAndy Fiddaman 	char *config, *name, *tofree, *value;
696*5c4a5fe1SAndy Fiddaman 
697*5c4a5fe1SAndy Fiddaman 	if (opt == NULL)
698*5c4a5fe1SAndy Fiddaman 		return (0);
699*5c4a5fe1SAndy Fiddaman 
700*5c4a5fe1SAndy Fiddaman 	config = tofree = strdup(opt);
701*5c4a5fe1SAndy Fiddaman 	while ((name = strsep(&config, ",")) != NULL) {
702*5c4a5fe1SAndy Fiddaman 		value = strchr(name, '=');
703*5c4a5fe1SAndy Fiddaman 		if (value != NULL) {
704*5c4a5fe1SAndy Fiddaman 			*value++ = '\0';
705*5c4a5fe1SAndy Fiddaman 			set_config_value_node(nvl, name, value);
706*5c4a5fe1SAndy Fiddaman 		} else {
707*5c4a5fe1SAndy Fiddaman 			if (strncmp(name, "/dev/ppt", 8) != 0) {
708*5c4a5fe1SAndy Fiddaman 				EPRINTLN("passthru: invalid path \"%s\"", name);
709*5c4a5fe1SAndy Fiddaman 				free(tofree);
710*5c4a5fe1SAndy Fiddaman 				return (-1);
711*5c4a5fe1SAndy Fiddaman 			}
712*5c4a5fe1SAndy Fiddaman 			set_config_value_node(nvl, "path", name);
713*5c4a5fe1SAndy Fiddaman 		}
714*5c4a5fe1SAndy Fiddaman 	}
715*5c4a5fe1SAndy Fiddaman 	free(tofree);
716*5c4a5fe1SAndy Fiddaman 	return (0);
717*5c4a5fe1SAndy Fiddaman }
718*5c4a5fe1SAndy Fiddaman 
719*5c4a5fe1SAndy Fiddaman static int
passthru_init_rom(struct vmctx * const ctx __unused,struct passthru_softc * const sc,const char * const romfile)720*5c4a5fe1SAndy Fiddaman passthru_init_rom(struct vmctx *const ctx __unused,
721*5c4a5fe1SAndy Fiddaman     struct passthru_softc *const sc, const char *const romfile)
722*5c4a5fe1SAndy Fiddaman {
723*5c4a5fe1SAndy Fiddaman 	if (romfile == NULL) {
724*5c4a5fe1SAndy Fiddaman 		return (0);
725*5c4a5fe1SAndy Fiddaman 	}
726*5c4a5fe1SAndy Fiddaman 
727*5c4a5fe1SAndy Fiddaman 	const int fd = open(romfile, O_RDONLY);
728*5c4a5fe1SAndy Fiddaman 	if (fd < 0) {
729*5c4a5fe1SAndy Fiddaman 		warnx("%s: can't open romfile \"%s\"", __func__, romfile);
730*5c4a5fe1SAndy Fiddaman 		return (-1);
731*5c4a5fe1SAndy Fiddaman 	}
732*5c4a5fe1SAndy Fiddaman 
733*5c4a5fe1SAndy Fiddaman 	struct stat sbuf;
734*5c4a5fe1SAndy Fiddaman 	if (fstat(fd, &sbuf) < 0) {
735*5c4a5fe1SAndy Fiddaman 		warnx("%s: can't fstat romfile \"%s\"", __func__, romfile);
736*5c4a5fe1SAndy Fiddaman 		close(fd);
737*5c4a5fe1SAndy Fiddaman 		return (-1);
738*5c4a5fe1SAndy Fiddaman 	}
739*5c4a5fe1SAndy Fiddaman 	const uint64_t rom_size = sbuf.st_size;
740*5c4a5fe1SAndy Fiddaman 
741*5c4a5fe1SAndy Fiddaman 	void *const rom_data = mmap(NULL, rom_size, PROT_READ, MAP_SHARED, fd,
742*5c4a5fe1SAndy Fiddaman 	    0);
743*5c4a5fe1SAndy Fiddaman 	if (rom_data == MAP_FAILED) {
744*5c4a5fe1SAndy Fiddaman 		warnx("%s: unable to mmap romfile \"%s\" (%d)", __func__,
745*5c4a5fe1SAndy Fiddaman 		    romfile, errno);
746*5c4a5fe1SAndy Fiddaman 		close(fd);
747*5c4a5fe1SAndy Fiddaman 		return (-1);
748*5c4a5fe1SAndy Fiddaman 	}
749*5c4a5fe1SAndy Fiddaman 
750*5c4a5fe1SAndy Fiddaman 	void *rom_addr;
751*5c4a5fe1SAndy Fiddaman 	int error = pci_emul_alloc_rom(sc->psc_pi, rom_size, &rom_addr);
752*5c4a5fe1SAndy Fiddaman 	if (error) {
753*5c4a5fe1SAndy Fiddaman 		warnx("%s: failed to alloc rom segment", __func__);
754*5c4a5fe1SAndy Fiddaman 		munmap(rom_data, rom_size);
755*5c4a5fe1SAndy Fiddaman 		close(fd);
756*5c4a5fe1SAndy Fiddaman 		return (error);
757*5c4a5fe1SAndy Fiddaman 	}
758*5c4a5fe1SAndy Fiddaman 	memcpy(rom_addr, rom_data, rom_size);
759*5c4a5fe1SAndy Fiddaman 
760*5c4a5fe1SAndy Fiddaman 	sc->psc_bar[PCI_ROM_IDX].type = PCIBAR_ROM;
761*5c4a5fe1SAndy Fiddaman 	sc->psc_bar[PCI_ROM_IDX].addr = (uint64_t)rom_addr;
762*5c4a5fe1SAndy Fiddaman 	sc->psc_bar[PCI_ROM_IDX].size = rom_size;
763*5c4a5fe1SAndy Fiddaman 
764*5c4a5fe1SAndy Fiddaman 	munmap(rom_data, rom_size);
765*5c4a5fe1SAndy Fiddaman 	close(fd);
766*5c4a5fe1SAndy Fiddaman 
767*5c4a5fe1SAndy Fiddaman  	return (0);
768*5c4a5fe1SAndy Fiddaman  }
769*5c4a5fe1SAndy Fiddaman 
770*5c4a5fe1SAndy Fiddaman static int
passthru_init(struct pci_devinst * pi,nvlist_t * nvl)771*5c4a5fe1SAndy Fiddaman passthru_init(struct pci_devinst *pi, nvlist_t *nvl)
772*5c4a5fe1SAndy Fiddaman {
773*5c4a5fe1SAndy Fiddaman 	int error, memflags, pptfd;
774*5c4a5fe1SAndy Fiddaman 	struct passthru_softc *sc;
775*5c4a5fe1SAndy Fiddaman 	const char *path;
776*5c4a5fe1SAndy Fiddaman 	struct vmctx *ctx = pi->pi_vmctx;
777*5c4a5fe1SAndy Fiddaman 
778*5c4a5fe1SAndy Fiddaman 	pptfd = -1;
779*5c4a5fe1SAndy Fiddaman 	sc = NULL;
780*5c4a5fe1SAndy Fiddaman 	error = 1;
781*5c4a5fe1SAndy Fiddaman 
782*5c4a5fe1SAndy Fiddaman 	memflags = vm_get_memflags(ctx);
783*5c4a5fe1SAndy Fiddaman 	if (!(memflags & VM_MEM_F_WIRED)) {
784*5c4a5fe1SAndy Fiddaman 		warnx("passthru requires guest memory to be wired");
785*5c4a5fe1SAndy Fiddaman 		goto done;
786*5c4a5fe1SAndy Fiddaman 	}
787*5c4a5fe1SAndy Fiddaman 
788*5c4a5fe1SAndy Fiddaman 	path = get_config_value_node(nvl, "path");
789*5c4a5fe1SAndy Fiddaman 	if (path == NULL || passthru_dev_open(path, &pptfd) != 0) {
790*5c4a5fe1SAndy Fiddaman 		warnx("invalid passthru options");
791*5c4a5fe1SAndy Fiddaman 		goto done;
792*5c4a5fe1SAndy Fiddaman 	}
793*5c4a5fe1SAndy Fiddaman 
794*5c4a5fe1SAndy Fiddaman 	if (vm_assign_pptdev(ctx, pptfd) != 0) {
795*5c4a5fe1SAndy Fiddaman 		warnx("PCI device at %d is not using the ppt driver", pptfd);
796*5c4a5fe1SAndy Fiddaman 		goto done;
797*5c4a5fe1SAndy Fiddaman 	}
798*5c4a5fe1SAndy Fiddaman 
799*5c4a5fe1SAndy Fiddaman 	sc = calloc(1, sizeof(struct passthru_softc));
800*5c4a5fe1SAndy Fiddaman 
801*5c4a5fe1SAndy Fiddaman 	pi->pi_arg = sc;
802*5c4a5fe1SAndy Fiddaman 	sc->psc_pi = pi;
803*5c4a5fe1SAndy Fiddaman 	sc->pptfd = pptfd;
804*5c4a5fe1SAndy Fiddaman 
805*5c4a5fe1SAndy Fiddaman 	if ((error = vm_get_pptdev_limits(ctx, pptfd, &sc->msi_limit,
806*5c4a5fe1SAndy Fiddaman 	    &sc->msix_limit)) != 0)
807*5c4a5fe1SAndy Fiddaman 		goto done;
808*5c4a5fe1SAndy Fiddaman 
809*5c4a5fe1SAndy Fiddaman #ifndef	__FreeBSD__
810*5c4a5fe1SAndy Fiddaman 	/*
811*5c4a5fe1SAndy Fiddaman 	 * If this function uses legacy interrupt messages, then request one for
812*5c4a5fe1SAndy Fiddaman 	 * the guest in case drivers expect to see it. Note that nothing in the
813*5c4a5fe1SAndy Fiddaman 	 * hypervisor is currently wired up do deliver such an interrupt should
814*5c4a5fe1SAndy Fiddaman 	 * the guest actually rely upon it.
815*5c4a5fe1SAndy Fiddaman 	 */
816*5c4a5fe1SAndy Fiddaman 	uint8_t intpin = passthru_read_config(sc, PCIR_INTPIN, 1);
817*5c4a5fe1SAndy Fiddaman 	if (intpin > 0 && intpin < 5)
818*5c4a5fe1SAndy Fiddaman 		pci_lintr_request(sc->psc_pi);
819*5c4a5fe1SAndy Fiddaman #endif
820*5c4a5fe1SAndy Fiddaman 
821*5c4a5fe1SAndy Fiddaman 	/* initialize config space */
822*5c4a5fe1SAndy Fiddaman 	if ((error = cfginit(ctx, sc)) != 0)
823*5c4a5fe1SAndy Fiddaman 		goto done;
824*5c4a5fe1SAndy Fiddaman 
825*5c4a5fe1SAndy Fiddaman 	/* initialize ROM */
826*5c4a5fe1SAndy Fiddaman 	if ((error = passthru_init_rom(ctx, sc,
827*5c4a5fe1SAndy Fiddaman 	    get_config_value_node(nvl, "rom"))) != 0) {
828*5c4a5fe1SAndy Fiddaman 		goto done;
829*5c4a5fe1SAndy Fiddaman 	}
830*5c4a5fe1SAndy Fiddaman 
831*5c4a5fe1SAndy Fiddaman done:
832*5c4a5fe1SAndy Fiddaman 	if (error) {
833*5c4a5fe1SAndy Fiddaman 		free(sc);
834*5c4a5fe1SAndy Fiddaman 		if (pptfd != -1)
835*5c4a5fe1SAndy Fiddaman 			vm_unassign_pptdev(ctx, pptfd);
836*5c4a5fe1SAndy Fiddaman 	}
837*5c4a5fe1SAndy Fiddaman 	return (error);
838*5c4a5fe1SAndy Fiddaman }
839*5c4a5fe1SAndy Fiddaman 
840*5c4a5fe1SAndy Fiddaman static int
msicap_access(struct passthru_softc * sc,int coff)841*5c4a5fe1SAndy Fiddaman msicap_access(struct passthru_softc *sc, int coff)
842*5c4a5fe1SAndy Fiddaman {
843*5c4a5fe1SAndy Fiddaman 	int caplen;
844*5c4a5fe1SAndy Fiddaman 
845*5c4a5fe1SAndy Fiddaman 	if (sc->psc_msi.capoff == 0)
846*5c4a5fe1SAndy Fiddaman 		return (0);
847*5c4a5fe1SAndy Fiddaman 
848*5c4a5fe1SAndy Fiddaman 	caplen = msi_caplen(sc->psc_msi.msgctrl);
849*5c4a5fe1SAndy Fiddaman 
850*5c4a5fe1SAndy Fiddaman 	if (coff >= sc->psc_msi.capoff && coff < sc->psc_msi.capoff + caplen)
851*5c4a5fe1SAndy Fiddaman 		return (1);
852*5c4a5fe1SAndy Fiddaman 	else
853*5c4a5fe1SAndy Fiddaman 		return (0);
854*5c4a5fe1SAndy Fiddaman }
855*5c4a5fe1SAndy Fiddaman 
856*5c4a5fe1SAndy Fiddaman static int
msixcap_access(struct passthru_softc * sc,int coff)857*5c4a5fe1SAndy Fiddaman msixcap_access(struct passthru_softc *sc, int coff)
858*5c4a5fe1SAndy Fiddaman {
859*5c4a5fe1SAndy Fiddaman 	if (sc->psc_msix.capoff == 0)
860*5c4a5fe1SAndy Fiddaman 		return (0);
861*5c4a5fe1SAndy Fiddaman 
862*5c4a5fe1SAndy Fiddaman 	return (coff >= sc->psc_msix.capoff &&
863*5c4a5fe1SAndy Fiddaman 	        coff < sc->psc_msix.capoff + MSIX_CAPLEN);
864*5c4a5fe1SAndy Fiddaman }
865*5c4a5fe1SAndy Fiddaman 
866*5c4a5fe1SAndy Fiddaman static int
passthru_cfgread_default(struct passthru_softc * sc,struct pci_devinst * pi __unused,int coff,int bytes,uint32_t * rv)867*5c4a5fe1SAndy Fiddaman passthru_cfgread_default(struct passthru_softc *sc,
868*5c4a5fe1SAndy Fiddaman     struct pci_devinst *pi __unused, int coff, int bytes, uint32_t *rv)
869*5c4a5fe1SAndy Fiddaman {
870*5c4a5fe1SAndy Fiddaman 	/*
871*5c4a5fe1SAndy Fiddaman 	 * MSI capability is emulated.
872*5c4a5fe1SAndy Fiddaman 	 */
873*5c4a5fe1SAndy Fiddaman 	if (msicap_access(sc, coff) || msixcap_access(sc, coff))
874*5c4a5fe1SAndy Fiddaman 		return (-1);
875*5c4a5fe1SAndy Fiddaman 
876*5c4a5fe1SAndy Fiddaman 	/*
877*5c4a5fe1SAndy Fiddaman 	 * MSI-X is also emulated since a limit on interrupts may be imposed by
878*5c4a5fe1SAndy Fiddaman 	 * the OS, altering the perceived register state.
879*5c4a5fe1SAndy Fiddaman 	 */
880*5c4a5fe1SAndy Fiddaman 	if (msixcap_access(sc, coff))
881*5c4a5fe1SAndy Fiddaman 		return (-1);
882*5c4a5fe1SAndy Fiddaman 
883*5c4a5fe1SAndy Fiddaman 	/*
884*5c4a5fe1SAndy Fiddaman 	 * Emulate the command register.  If a single read reads both the
885*5c4a5fe1SAndy Fiddaman 	 * command and status registers, read the status register from the
886*5c4a5fe1SAndy Fiddaman 	 * device's config space.
887*5c4a5fe1SAndy Fiddaman 	 */
888*5c4a5fe1SAndy Fiddaman 	if (coff == PCIR_COMMAND) {
889*5c4a5fe1SAndy Fiddaman 		if (bytes <= 2)
890*5c4a5fe1SAndy Fiddaman 			return (-1);
891*5c4a5fe1SAndy Fiddaman 		*rv = passthru_read_config(sc, PCIR_STATUS, 2) << 16 |
892*5c4a5fe1SAndy Fiddaman 		    pci_get_cfgdata16(pi, PCIR_COMMAND);
893*5c4a5fe1SAndy Fiddaman 		return (0);
894*5c4a5fe1SAndy Fiddaman 	}
895*5c4a5fe1SAndy Fiddaman 
896*5c4a5fe1SAndy Fiddaman 	/* Everything else just read from the device's config space */
897*5c4a5fe1SAndy Fiddaman 	*rv = passthru_read_config(sc, coff, bytes);
898*5c4a5fe1SAndy Fiddaman 
899*5c4a5fe1SAndy Fiddaman 	return (0);
900*5c4a5fe1SAndy Fiddaman }
901*5c4a5fe1SAndy Fiddaman 
902*5c4a5fe1SAndy Fiddaman int
passthru_cfgread_emulate(struct passthru_softc * sc __unused,struct pci_devinst * pi __unused,int coff __unused,int bytes __unused,uint32_t * rv __unused)903*5c4a5fe1SAndy Fiddaman passthru_cfgread_emulate(struct passthru_softc *sc __unused,
904*5c4a5fe1SAndy Fiddaman     struct pci_devinst *pi __unused, int coff __unused, int bytes __unused,
905*5c4a5fe1SAndy Fiddaman     uint32_t *rv __unused)
906*5c4a5fe1SAndy Fiddaman {
907*5c4a5fe1SAndy Fiddaman 	return (-1);
908*5c4a5fe1SAndy Fiddaman }
909*5c4a5fe1SAndy Fiddaman 
910*5c4a5fe1SAndy Fiddaman static int
passthru_cfgread(struct pci_devinst * pi,int coff,int bytes,uint32_t * rv)911*5c4a5fe1SAndy Fiddaman passthru_cfgread(struct pci_devinst *pi, int coff, int bytes, uint32_t *rv)
912*5c4a5fe1SAndy Fiddaman {
913*5c4a5fe1SAndy Fiddaman 	struct passthru_softc *sc;
914*5c4a5fe1SAndy Fiddaman 
915*5c4a5fe1SAndy Fiddaman 	sc = pi->pi_arg;
916*5c4a5fe1SAndy Fiddaman 
917*5c4a5fe1SAndy Fiddaman 	if (sc->psc_pcir_rhandler[coff] != NULL)
918*5c4a5fe1SAndy Fiddaman 		return (sc->psc_pcir_rhandler[coff](sc, pi, coff, bytes, rv));
919*5c4a5fe1SAndy Fiddaman 
920*5c4a5fe1SAndy Fiddaman 	return (passthru_cfgread_default(sc, pi, coff, bytes, rv));
921*5c4a5fe1SAndy Fiddaman }
922*5c4a5fe1SAndy Fiddaman 
923*5c4a5fe1SAndy Fiddaman static int
passthru_cfgwrite_default(struct passthru_softc * sc,struct pci_devinst * pi,int coff,int bytes,uint32_t val)924*5c4a5fe1SAndy Fiddaman passthru_cfgwrite_default(struct passthru_softc *sc, struct pci_devinst *pi,
925*5c4a5fe1SAndy Fiddaman     int coff, int bytes, uint32_t val)
926*5c4a5fe1SAndy Fiddaman {
927*5c4a5fe1SAndy Fiddaman 	int error, msix_table_entries, i;
928*5c4a5fe1SAndy Fiddaman 	uint16_t cmd_old;
929*5c4a5fe1SAndy Fiddaman 	struct vmctx *ctx = pi->pi_vmctx;
930*5c4a5fe1SAndy Fiddaman 
931*5c4a5fe1SAndy Fiddaman 	/*
932*5c4a5fe1SAndy Fiddaman 	 * MSI capability is emulated
933*5c4a5fe1SAndy Fiddaman 	 */
934*5c4a5fe1SAndy Fiddaman 	if (msicap_access(sc, coff)) {
935*5c4a5fe1SAndy Fiddaman 		pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msi.capoff,
936*5c4a5fe1SAndy Fiddaman 		    PCIY_MSI);
937*5c4a5fe1SAndy Fiddaman 		error = vm_setup_pptdev_msi(ctx, sc->pptfd,
938*5c4a5fe1SAndy Fiddaman 		    pi->pi_msi.addr, pi->pi_msi.msg_data, pi->pi_msi.maxmsgnum);
939*5c4a5fe1SAndy Fiddaman 		if (error != 0)
940*5c4a5fe1SAndy Fiddaman 			err(1, "vm_setup_pptdev_msi");
941*5c4a5fe1SAndy Fiddaman 		return (0);
942*5c4a5fe1SAndy Fiddaman 	}
943*5c4a5fe1SAndy Fiddaman 
944*5c4a5fe1SAndy Fiddaman 	if (msixcap_access(sc, coff)) {
945*5c4a5fe1SAndy Fiddaman 		pci_emul_capwrite(pi, coff, bytes, val, sc->psc_msix.capoff,
946*5c4a5fe1SAndy Fiddaman 		    PCIY_MSIX);
947*5c4a5fe1SAndy Fiddaman 		if (pi->pi_msix.enabled) {
948*5c4a5fe1SAndy Fiddaman 			msix_table_entries = pi->pi_msix.table_count;
949*5c4a5fe1SAndy Fiddaman 			for (i = 0; i < msix_table_entries; i++) {
950*5c4a5fe1SAndy Fiddaman 				error = vm_setup_pptdev_msix(ctx,
951*5c4a5fe1SAndy Fiddaman 				    sc->pptfd, i,
952*5c4a5fe1SAndy Fiddaman 				    pi->pi_msix.table[i].addr,
953*5c4a5fe1SAndy Fiddaman 				    pi->pi_msix.table[i].msg_data,
954*5c4a5fe1SAndy Fiddaman 				    pi->pi_msix.table[i].vector_control);
955*5c4a5fe1SAndy Fiddaman 
956*5c4a5fe1SAndy Fiddaman 				if (error)
957*5c4a5fe1SAndy Fiddaman 					err(1, "vm_setup_pptdev_msix");
958*5c4a5fe1SAndy Fiddaman 			}
959*5c4a5fe1SAndy Fiddaman 		} else {
960*5c4a5fe1SAndy Fiddaman 			error = vm_disable_pptdev_msix(ctx, sc->pptfd);
961*5c4a5fe1SAndy Fiddaman 			if (error)
962*5c4a5fe1SAndy Fiddaman 				err(1, "vm_disable_pptdev_msix");
963*5c4a5fe1SAndy Fiddaman 		}
964*5c4a5fe1SAndy Fiddaman 		return (0);
965*5c4a5fe1SAndy Fiddaman 	}
966*5c4a5fe1SAndy Fiddaman 
967*5c4a5fe1SAndy Fiddaman 	/*
968*5c4a5fe1SAndy Fiddaman 	 * The command register is emulated, but the status register
969*5c4a5fe1SAndy Fiddaman 	 * is passed through.
970*5c4a5fe1SAndy Fiddaman 	 */
971*5c4a5fe1SAndy Fiddaman 	if (coff == PCIR_COMMAND) {
972*5c4a5fe1SAndy Fiddaman 		if (bytes <= 2)
973*5c4a5fe1SAndy Fiddaman 			return (-1);
974*5c4a5fe1SAndy Fiddaman 
975*5c4a5fe1SAndy Fiddaman 		/* Update the physical status register. */
976*5c4a5fe1SAndy Fiddaman 		passthru_write_config(sc, PCIR_STATUS, 2, val >> 16);
977*5c4a5fe1SAndy Fiddaman 
978*5c4a5fe1SAndy Fiddaman 		/* Update the virtual command register. */
979*5c4a5fe1SAndy Fiddaman 		cmd_old = pci_get_cfgdata16(pi, PCIR_COMMAND);
980*5c4a5fe1SAndy Fiddaman 		pci_set_cfgdata16(pi, PCIR_COMMAND, val & 0xffff);
981*5c4a5fe1SAndy Fiddaman 		pci_emul_cmd_changed(pi, cmd_old);
982*5c4a5fe1SAndy Fiddaman 		return (0);
983*5c4a5fe1SAndy Fiddaman 	}
984*5c4a5fe1SAndy Fiddaman 
985*5c4a5fe1SAndy Fiddaman 	passthru_write_config(sc, coff, bytes, val);
986*5c4a5fe1SAndy Fiddaman 
987*5c4a5fe1SAndy Fiddaman 	return (0);
988*5c4a5fe1SAndy Fiddaman }
989*5c4a5fe1SAndy Fiddaman 
990*5c4a5fe1SAndy Fiddaman int
passthru_cfgwrite_emulate(struct passthru_softc * sc __unused,struct pci_devinst * pi __unused,int coff __unused,int bytes __unused,uint32_t val __unused)991*5c4a5fe1SAndy Fiddaman passthru_cfgwrite_emulate(struct passthru_softc *sc __unused,
992*5c4a5fe1SAndy Fiddaman     struct pci_devinst *pi __unused, int coff __unused, int bytes __unused,
993*5c4a5fe1SAndy Fiddaman     uint32_t val __unused)
994*5c4a5fe1SAndy Fiddaman {
995*5c4a5fe1SAndy Fiddaman 	return (-1);
996*5c4a5fe1SAndy Fiddaman }
997*5c4a5fe1SAndy Fiddaman 
998*5c4a5fe1SAndy Fiddaman static int
passthru_cfgwrite(struct pci_devinst * pi,int coff,int bytes,uint32_t val)999*5c4a5fe1SAndy Fiddaman passthru_cfgwrite(struct pci_devinst *pi, int coff, int bytes, uint32_t val)
1000*5c4a5fe1SAndy Fiddaman {
1001*5c4a5fe1SAndy Fiddaman 	struct passthru_softc *sc;
1002*5c4a5fe1SAndy Fiddaman 
1003*5c4a5fe1SAndy Fiddaman 	sc = pi->pi_arg;
1004*5c4a5fe1SAndy Fiddaman 
1005*5c4a5fe1SAndy Fiddaman 	if (sc->psc_pcir_whandler[coff] != NULL)
1006*5c4a5fe1SAndy Fiddaman 		return (sc->psc_pcir_whandler[coff](sc, pi, coff, bytes, val));
1007*5c4a5fe1SAndy Fiddaman 
1008*5c4a5fe1SAndy Fiddaman 	return (passthru_cfgwrite_default(sc, pi, coff, bytes, val));
1009*5c4a5fe1SAndy Fiddaman }
1010*5c4a5fe1SAndy Fiddaman 
1011*5c4a5fe1SAndy Fiddaman static void
passthru_write(struct pci_devinst * pi,int baridx,uint64_t offset,int size,uint64_t value)1012*5c4a5fe1SAndy Fiddaman passthru_write(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
1013*5c4a5fe1SAndy Fiddaman     uint64_t value)
1014*5c4a5fe1SAndy Fiddaman {
1015*5c4a5fe1SAndy Fiddaman 	struct passthru_softc *sc = pi->pi_arg;
1016*5c4a5fe1SAndy Fiddaman 	struct vmctx *ctx = pi->pi_vmctx;
1017*5c4a5fe1SAndy Fiddaman 
1018*5c4a5fe1SAndy Fiddaman 	if (baridx == pci_msix_table_bar(pi)) {
1019*5c4a5fe1SAndy Fiddaman 		msix_table_write(ctx, sc, offset, size, value);
1020*5c4a5fe1SAndy Fiddaman 	} else {
1021*5c4a5fe1SAndy Fiddaman 		struct ppt_bar_io pbi;
1022*5c4a5fe1SAndy Fiddaman 
1023*5c4a5fe1SAndy Fiddaman 		assert(pi->pi_bar[baridx].type == PCIBAR_IO);
1024*5c4a5fe1SAndy Fiddaman 
1025*5c4a5fe1SAndy Fiddaman 		pbi.pbi_bar = baridx;
1026*5c4a5fe1SAndy Fiddaman 		pbi.pbi_width = size;
1027*5c4a5fe1SAndy Fiddaman 		pbi.pbi_off = offset;
1028*5c4a5fe1SAndy Fiddaman 		pbi.pbi_data = value;
1029*5c4a5fe1SAndy Fiddaman 		(void) ioctl(sc->pptfd, PPT_BAR_WRITE, &pbi);
1030*5c4a5fe1SAndy Fiddaman 	}
1031*5c4a5fe1SAndy Fiddaman }
1032*5c4a5fe1SAndy Fiddaman 
1033*5c4a5fe1SAndy Fiddaman static uint64_t
passthru_read(struct pci_devinst * pi,int baridx,uint64_t offset,int size)1034*5c4a5fe1SAndy Fiddaman passthru_read(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
1035*5c4a5fe1SAndy Fiddaman {
1036*5c4a5fe1SAndy Fiddaman 	struct passthru_softc *sc = pi->pi_arg;
1037*5c4a5fe1SAndy Fiddaman 	uint64_t val;
1038*5c4a5fe1SAndy Fiddaman 
1039*5c4a5fe1SAndy Fiddaman 	if (baridx == pci_msix_table_bar(pi)) {
1040*5c4a5fe1SAndy Fiddaman 		val = msix_table_read(sc, offset, size);
1041*5c4a5fe1SAndy Fiddaman 	} else {
1042*5c4a5fe1SAndy Fiddaman 		struct ppt_bar_io pbi;
1043*5c4a5fe1SAndy Fiddaman 
1044*5c4a5fe1SAndy Fiddaman 		assert(pi->pi_bar[baridx].type == PCIBAR_IO);
1045*5c4a5fe1SAndy Fiddaman 
1046*5c4a5fe1SAndy Fiddaman 		pbi.pbi_bar = baridx;
1047*5c4a5fe1SAndy Fiddaman 		pbi.pbi_width = size;
1048*5c4a5fe1SAndy Fiddaman 		pbi.pbi_off = offset;
1049*5c4a5fe1SAndy Fiddaman 		if (ioctl(sc->pptfd, PPT_BAR_READ, &pbi) == 0) {
1050*5c4a5fe1SAndy Fiddaman 			val = pbi.pbi_data;
1051*5c4a5fe1SAndy Fiddaman 		} else {
1052*5c4a5fe1SAndy Fiddaman 			val = 0;
1053*5c4a5fe1SAndy Fiddaman 		}
1054*5c4a5fe1SAndy Fiddaman 	}
1055*5c4a5fe1SAndy Fiddaman 
1056*5c4a5fe1SAndy Fiddaman 	return (val);
1057*5c4a5fe1SAndy Fiddaman }
1058*5c4a5fe1SAndy Fiddaman 
1059*5c4a5fe1SAndy Fiddaman static void
passthru_msix_addr(struct vmctx * ctx,struct pci_devinst * pi,int baridx,int enabled,uint64_t address)1060*5c4a5fe1SAndy Fiddaman passthru_msix_addr(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
1061*5c4a5fe1SAndy Fiddaman 		   int enabled, uint64_t address)
1062*5c4a5fe1SAndy Fiddaman {
1063*5c4a5fe1SAndy Fiddaman 	struct passthru_softc *sc;
1064*5c4a5fe1SAndy Fiddaman 	size_t remaining;
1065*5c4a5fe1SAndy Fiddaman 	uint32_t table_size, table_offset;
1066*5c4a5fe1SAndy Fiddaman 
1067*5c4a5fe1SAndy Fiddaman 	sc = pi->pi_arg;
1068*5c4a5fe1SAndy Fiddaman 	table_offset = rounddown2(pi->pi_msix.table_offset, 4096);
1069*5c4a5fe1SAndy Fiddaman 	if (table_offset > 0) {
1070*5c4a5fe1SAndy Fiddaman 		if (!enabled) {
1071*5c4a5fe1SAndy Fiddaman 			if (vm_unmap_pptdev_mmio(ctx, sc->pptfd, address,
1072*5c4a5fe1SAndy Fiddaman 			    table_offset) != 0)
1073*5c4a5fe1SAndy Fiddaman 				warnx("pci_passthru: unmap_pptdev_mmio failed");
1074*5c4a5fe1SAndy Fiddaman 		} else {
1075*5c4a5fe1SAndy Fiddaman 			if (vm_map_pptdev_mmio(ctx, sc->pptfd, address,
1076*5c4a5fe1SAndy Fiddaman 			    table_offset, sc->psc_bar[baridx].addr) != 0)
1077*5c4a5fe1SAndy Fiddaman 				warnx("pci_passthru: map_pptdev_mmio failed");
1078*5c4a5fe1SAndy Fiddaman 		}
1079*5c4a5fe1SAndy Fiddaman 	}
1080*5c4a5fe1SAndy Fiddaman 	table_size = pi->pi_msix.table_offset - table_offset;
1081*5c4a5fe1SAndy Fiddaman 	table_size += pi->pi_msix.table_count * MSIX_TABLE_ENTRY_SIZE;
1082*5c4a5fe1SAndy Fiddaman 	table_size = roundup2(table_size, 4096);
1083*5c4a5fe1SAndy Fiddaman 	remaining = pi->pi_bar[baridx].size - table_offset - table_size;
1084*5c4a5fe1SAndy Fiddaman 	if (remaining > 0) {
1085*5c4a5fe1SAndy Fiddaman 		address += table_offset + table_size;
1086*5c4a5fe1SAndy Fiddaman 		if (!enabled) {
1087*5c4a5fe1SAndy Fiddaman 			if (vm_unmap_pptdev_mmio(ctx, sc->pptfd, address,
1088*5c4a5fe1SAndy Fiddaman 			    remaining) != 0)
1089*5c4a5fe1SAndy Fiddaman 				warnx("pci_passthru: unmap_pptdev_mmio failed");
1090*5c4a5fe1SAndy Fiddaman 		} else {
1091*5c4a5fe1SAndy Fiddaman 			if (vm_map_pptdev_mmio(ctx, sc->pptfd, address,
1092*5c4a5fe1SAndy Fiddaman 			    remaining, sc->psc_bar[baridx].addr +
1093*5c4a5fe1SAndy Fiddaman 			    table_offset + table_size) != 0)
1094*5c4a5fe1SAndy Fiddaman 				warnx("pci_passthru: map_pptdev_mmio failed");
1095*5c4a5fe1SAndy Fiddaman 		}
1096*5c4a5fe1SAndy Fiddaman 	}
1097*5c4a5fe1SAndy Fiddaman }
1098*5c4a5fe1SAndy Fiddaman 
1099*5c4a5fe1SAndy Fiddaman static void
passthru_mmio_addr(struct vmctx * ctx,struct pci_devinst * pi,int baridx,int enabled,uint64_t address)1100*5c4a5fe1SAndy Fiddaman passthru_mmio_addr(struct vmctx *ctx, struct pci_devinst *pi, int baridx,
1101*5c4a5fe1SAndy Fiddaman 		   int enabled, uint64_t address)
1102*5c4a5fe1SAndy Fiddaman {
1103*5c4a5fe1SAndy Fiddaman 	struct passthru_softc *sc;
1104*5c4a5fe1SAndy Fiddaman 
1105*5c4a5fe1SAndy Fiddaman 	sc = pi->pi_arg;
1106*5c4a5fe1SAndy Fiddaman 	if (!enabled) {
1107*5c4a5fe1SAndy Fiddaman 		if (vm_unmap_pptdev_mmio(ctx, sc->pptfd, address,
1108*5c4a5fe1SAndy Fiddaman 		    sc->psc_bar[baridx].size) != 0)
1109*5c4a5fe1SAndy Fiddaman 			warnx("pci_passthru: unmap_pptdev_mmio failed");
1110*5c4a5fe1SAndy Fiddaman 	} else {
1111*5c4a5fe1SAndy Fiddaman 		if (vm_map_pptdev_mmio(ctx, sc->pptfd, address,
1112*5c4a5fe1SAndy Fiddaman 		    sc->psc_bar[baridx].size, sc->psc_bar[baridx].addr) != 0)
1113*5c4a5fe1SAndy Fiddaman 			warnx("pci_passthru: map_pptdev_mmio failed");
1114*5c4a5fe1SAndy Fiddaman 	}
1115*5c4a5fe1SAndy Fiddaman }
1116*5c4a5fe1SAndy Fiddaman 
1117*5c4a5fe1SAndy Fiddaman static void
passthru_addr_rom(struct pci_devinst * const pi,const int idx,const int enabled)1118*5c4a5fe1SAndy Fiddaman passthru_addr_rom(struct pci_devinst *const pi, const int idx,
1119*5c4a5fe1SAndy Fiddaman     const int enabled)
1120*5c4a5fe1SAndy Fiddaman {
1121*5c4a5fe1SAndy Fiddaman 	const uint64_t addr = pi->pi_bar[idx].addr;
1122*5c4a5fe1SAndy Fiddaman 	const uint64_t size = pi->pi_bar[idx].size;
1123*5c4a5fe1SAndy Fiddaman 
1124*5c4a5fe1SAndy Fiddaman 	if (!enabled) {
1125*5c4a5fe1SAndy Fiddaman 		if (vm_munmap_memseg(pi->pi_vmctx, addr, size) != 0) {
1126*5c4a5fe1SAndy Fiddaman 			errx(4, "%s: munmap_memseg @ [%016lx - %016lx] failed",
1127*5c4a5fe1SAndy Fiddaman 			    __func__, addr, addr + size);
1128*5c4a5fe1SAndy Fiddaman 		}
1129*5c4a5fe1SAndy Fiddaman 
1130*5c4a5fe1SAndy Fiddaman 	} else {
1131*5c4a5fe1SAndy Fiddaman 		if (vm_mmap_memseg(pi->pi_vmctx, addr, VM_PCIROM,
1132*5c4a5fe1SAndy Fiddaman 			pi->pi_romoffset, size, PROT_READ | PROT_EXEC) != 0) {
1133*5c4a5fe1SAndy Fiddaman 			errx(4, "%s: mmap_memseg @ [%016lx - %016lx]  failed",
1134*5c4a5fe1SAndy Fiddaman 			    __func__, addr, addr + size);
1135*5c4a5fe1SAndy Fiddaman 		}
1136*5c4a5fe1SAndy Fiddaman 	}
1137*5c4a5fe1SAndy Fiddaman }
1138*5c4a5fe1SAndy Fiddaman 
1139*5c4a5fe1SAndy Fiddaman static void
passthru_addr(struct pci_devinst * pi,int baridx,int enabled,uint64_t address)1140*5c4a5fe1SAndy Fiddaman passthru_addr(struct pci_devinst *pi, int baridx,
1141*5c4a5fe1SAndy Fiddaman     int enabled, uint64_t address)
1142*5c4a5fe1SAndy Fiddaman {
1143*5c4a5fe1SAndy Fiddaman 	struct vmctx *ctx = pi->pi_vmctx;
1144*5c4a5fe1SAndy Fiddaman 
1145*5c4a5fe1SAndy Fiddaman 	switch (pi->pi_bar[baridx].type) {
1146*5c4a5fe1SAndy Fiddaman 	case PCIBAR_IO:
1147*5c4a5fe1SAndy Fiddaman 		/* IO BARs are emulated */
1148*5c4a5fe1SAndy Fiddaman 		break;
1149*5c4a5fe1SAndy Fiddaman 	case PCIBAR_ROM:
1150*5c4a5fe1SAndy Fiddaman 		passthru_addr_rom(pi, baridx, enabled);
1151*5c4a5fe1SAndy Fiddaman 		break;
1152*5c4a5fe1SAndy Fiddaman 	case PCIBAR_MEM32:
1153*5c4a5fe1SAndy Fiddaman 	case PCIBAR_MEM64:
1154*5c4a5fe1SAndy Fiddaman 		if (baridx == pci_msix_table_bar(pi))
1155*5c4a5fe1SAndy Fiddaman 			passthru_msix_addr(ctx, pi, baridx, enabled, address);
1156*5c4a5fe1SAndy Fiddaman 		else
1157*5c4a5fe1SAndy Fiddaman 			passthru_mmio_addr(ctx, pi, baridx, enabled, address);
1158*5c4a5fe1SAndy Fiddaman 		break;
1159*5c4a5fe1SAndy Fiddaman 	default:
1160*5c4a5fe1SAndy Fiddaman 		errx(4, "%s: invalid BAR type %d", __func__,
1161*5c4a5fe1SAndy Fiddaman 		    pi->pi_bar[baridx].type);
1162*5c4a5fe1SAndy Fiddaman 	}
1163*5c4a5fe1SAndy Fiddaman }
1164*5c4a5fe1SAndy Fiddaman 
1165*5c4a5fe1SAndy Fiddaman static const struct pci_devemu passthru = {
1166*5c4a5fe1SAndy Fiddaman 	.pe_emu		= "passthru",
1167*5c4a5fe1SAndy Fiddaman 	.pe_init	= passthru_init,
1168*5c4a5fe1SAndy Fiddaman 	.pe_legacy_config = passthru_legacy_config,
1169*5c4a5fe1SAndy Fiddaman 	.pe_cfgwrite	= passthru_cfgwrite,
1170*5c4a5fe1SAndy Fiddaman 	.pe_cfgread	= passthru_cfgread,
1171*5c4a5fe1SAndy Fiddaman 	.pe_barwrite 	= passthru_write,
1172*5c4a5fe1SAndy Fiddaman 	.pe_barread    	= passthru_read,
1173*5c4a5fe1SAndy Fiddaman 	.pe_baraddr	= passthru_addr,
1174*5c4a5fe1SAndy Fiddaman };
1175*5c4a5fe1SAndy Fiddaman PCI_EMUL_SET(passthru);
1176*5c4a5fe1SAndy Fiddaman 
1177*5c4a5fe1SAndy Fiddaman /*
1178*5c4a5fe1SAndy Fiddaman  * This isn't the right place for these functions which, on FreeBSD, can
1179*5c4a5fe1SAndy Fiddaman  * read or write from arbitrary devices. They are not supported on illumos;
1180*5c4a5fe1SAndy Fiddaman  * not least because bhyve is generally run in a non-global zone which doesn't
1181*5c4a5fe1SAndy Fiddaman  * have access to the devinfo tree.
1182*5c4a5fe1SAndy Fiddaman  */
1183*5c4a5fe1SAndy Fiddaman uint32_t
pci_host_read_config(const struct pcisel * sel __unused,long reg __unused,int width __unused)1184*5c4a5fe1SAndy Fiddaman pci_host_read_config(const struct pcisel *sel __unused, long reg __unused,
1185*5c4a5fe1SAndy Fiddaman     int width __unused)
1186*5c4a5fe1SAndy Fiddaman {
1187*5c4a5fe1SAndy Fiddaman 	return (-1);
1188*5c4a5fe1SAndy Fiddaman }
1189*5c4a5fe1SAndy Fiddaman 
1190*5c4a5fe1SAndy Fiddaman void
pci_host_write_config(const struct pcisel * sel __unused,long reg __unused,int width __unused,uint32_t data __unused)1191*5c4a5fe1SAndy Fiddaman pci_host_write_config(const struct pcisel *sel __unused, long reg __unused,
1192*5c4a5fe1SAndy Fiddaman     int width __unused, uint32_t data __unused)
1193*5c4a5fe1SAndy Fiddaman {
1194*5c4a5fe1SAndy Fiddaman        errx(4, "pci_host_write_config() unimplemented on illumos");
1195*5c4a5fe1SAndy Fiddaman }
1196