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 * This file and its contents are supplied under the terms of the
30*5c4a5fe1SAndy Fiddaman * Common Development and Distribution License ("CDDL"), version 1.0.
31*5c4a5fe1SAndy Fiddaman * You may only use this file in accordance with the terms of version
32*5c4a5fe1SAndy Fiddaman * 1.0 of the CDDL.
33*5c4a5fe1SAndy Fiddaman *
34*5c4a5fe1SAndy Fiddaman * A full copy of the text of the CDDL should have accompanied this
35*5c4a5fe1SAndy Fiddaman * source. A copy of the CDDL is also available via the Internet at
36*5c4a5fe1SAndy Fiddaman * http://www.illumos.org/license/CDDL.
37*5c4a5fe1SAndy Fiddaman *
38*5c4a5fe1SAndy Fiddaman * Copyright 2014 Pluribus Networks Inc.
39*5c4a5fe1SAndy Fiddaman * Copyright 2018 Joyent, Inc.
40*5c4a5fe1SAndy Fiddaman */
41*5c4a5fe1SAndy Fiddaman
42*5c4a5fe1SAndy Fiddaman
43*5c4a5fe1SAndy Fiddaman #include <sys/param.h>
44*5c4a5fe1SAndy Fiddaman #include <sys/linker_set.h>
45*5c4a5fe1SAndy Fiddaman #include <sys/mman.h>
46*5c4a5fe1SAndy Fiddaman
47*5c4a5fe1SAndy Fiddaman #include <ctype.h>
48*5c4a5fe1SAndy Fiddaman #include <err.h>
49*5c4a5fe1SAndy Fiddaman #include <errno.h>
50*5c4a5fe1SAndy Fiddaman #include <pthread.h>
51*5c4a5fe1SAndy Fiddaman #include <stdio.h>
52*5c4a5fe1SAndy Fiddaman #include <stdlib.h>
53*5c4a5fe1SAndy Fiddaman #include <string.h>
54*5c4a5fe1SAndy Fiddaman #include <strings.h>
55*5c4a5fe1SAndy Fiddaman #include <assert.h>
56*5c4a5fe1SAndy Fiddaman #include <stdbool.h>
57*5c4a5fe1SAndy Fiddaman #include <sysexits.h>
58*5c4a5fe1SAndy Fiddaman
59*5c4a5fe1SAndy Fiddaman #include <machine/vmm.h>
60*5c4a5fe1SAndy Fiddaman #include <vmmapi.h>
61*5c4a5fe1SAndy Fiddaman
62*5c4a5fe1SAndy Fiddaman #include "acpi.h"
63*5c4a5fe1SAndy Fiddaman #include "bhyverun.h"
64*5c4a5fe1SAndy Fiddaman #include "bootrom.h"
65*5c4a5fe1SAndy Fiddaman #include "config.h"
66*5c4a5fe1SAndy Fiddaman #include "debug.h"
67*5c4a5fe1SAndy Fiddaman #include "inout.h"
68*5c4a5fe1SAndy Fiddaman #include "ioapic.h"
69*5c4a5fe1SAndy Fiddaman #include "mem.h"
70*5c4a5fe1SAndy Fiddaman #include "pci_emul.h"
71*5c4a5fe1SAndy Fiddaman #include "pci_irq.h"
72*5c4a5fe1SAndy Fiddaman #include "pci_lpc.h"
73*5c4a5fe1SAndy Fiddaman #include "pci_passthru.h"
74*5c4a5fe1SAndy Fiddaman #include "qemu_fwcfg.h"
75*5c4a5fe1SAndy Fiddaman
76*5c4a5fe1SAndy Fiddaman #define CONF1_ADDR_PORT 0x0cf8
77*5c4a5fe1SAndy Fiddaman #define CONF1_DATA_PORT 0x0cfc
78*5c4a5fe1SAndy Fiddaman
79*5c4a5fe1SAndy Fiddaman #define CONF1_ENABLE 0x80000000ul
80*5c4a5fe1SAndy Fiddaman
81*5c4a5fe1SAndy Fiddaman #define MAXBUSES (PCI_BUSMAX + 1)
82*5c4a5fe1SAndy Fiddaman #define MAXSLOTS (PCI_SLOTMAX + 1)
83*5c4a5fe1SAndy Fiddaman #define MAXFUNCS (PCI_FUNCMAX + 1)
84*5c4a5fe1SAndy Fiddaman
85*5c4a5fe1SAndy Fiddaman #define GB (1024 * 1024 * 1024UL)
86*5c4a5fe1SAndy Fiddaman
87*5c4a5fe1SAndy Fiddaman struct funcinfo {
88*5c4a5fe1SAndy Fiddaman nvlist_t *fi_config;
89*5c4a5fe1SAndy Fiddaman struct pci_devemu *fi_pde;
90*5c4a5fe1SAndy Fiddaman struct pci_devinst *fi_devi;
91*5c4a5fe1SAndy Fiddaman };
92*5c4a5fe1SAndy Fiddaman
93*5c4a5fe1SAndy Fiddaman struct intxinfo {
94*5c4a5fe1SAndy Fiddaman int ii_count;
95*5c4a5fe1SAndy Fiddaman int ii_pirq_pin;
96*5c4a5fe1SAndy Fiddaman int ii_ioapic_irq;
97*5c4a5fe1SAndy Fiddaman };
98*5c4a5fe1SAndy Fiddaman
99*5c4a5fe1SAndy Fiddaman struct slotinfo {
100*5c4a5fe1SAndy Fiddaman struct intxinfo si_intpins[4];
101*5c4a5fe1SAndy Fiddaman struct funcinfo si_funcs[MAXFUNCS];
102*5c4a5fe1SAndy Fiddaman };
103*5c4a5fe1SAndy Fiddaman
104*5c4a5fe1SAndy Fiddaman struct businfo {
105*5c4a5fe1SAndy Fiddaman uint16_t iobase, iolimit; /* I/O window */
106*5c4a5fe1SAndy Fiddaman uint32_t membase32, memlimit32; /* mmio window below 4GB */
107*5c4a5fe1SAndy Fiddaman uint64_t membase64, memlimit64; /* mmio window above 4GB */
108*5c4a5fe1SAndy Fiddaman struct slotinfo slotinfo[MAXSLOTS];
109*5c4a5fe1SAndy Fiddaman };
110*5c4a5fe1SAndy Fiddaman
111*5c4a5fe1SAndy Fiddaman static struct businfo *pci_businfo[MAXBUSES];
112*5c4a5fe1SAndy Fiddaman
113*5c4a5fe1SAndy Fiddaman SET_DECLARE(pci_devemu_set, struct pci_devemu);
114*5c4a5fe1SAndy Fiddaman
115*5c4a5fe1SAndy Fiddaman static uint64_t pci_emul_iobase;
116*5c4a5fe1SAndy Fiddaman static uint8_t *pci_emul_rombase;
117*5c4a5fe1SAndy Fiddaman static uint64_t pci_emul_romoffset;
118*5c4a5fe1SAndy Fiddaman static uint8_t *pci_emul_romlim;
119*5c4a5fe1SAndy Fiddaman static uint64_t pci_emul_membase32;
120*5c4a5fe1SAndy Fiddaman static uint64_t pci_emul_membase64;
121*5c4a5fe1SAndy Fiddaman static uint64_t pci_emul_memlim64;
122*5c4a5fe1SAndy Fiddaman
123*5c4a5fe1SAndy Fiddaman struct pci_bar_allocation {
124*5c4a5fe1SAndy Fiddaman TAILQ_ENTRY(pci_bar_allocation) chain;
125*5c4a5fe1SAndy Fiddaman struct pci_devinst *pdi;
126*5c4a5fe1SAndy Fiddaman int idx;
127*5c4a5fe1SAndy Fiddaman enum pcibar_type type;
128*5c4a5fe1SAndy Fiddaman uint64_t size;
129*5c4a5fe1SAndy Fiddaman };
130*5c4a5fe1SAndy Fiddaman
131*5c4a5fe1SAndy Fiddaman static TAILQ_HEAD(pci_bar_list, pci_bar_allocation) pci_bars =
132*5c4a5fe1SAndy Fiddaman TAILQ_HEAD_INITIALIZER(pci_bars);
133*5c4a5fe1SAndy Fiddaman
134*5c4a5fe1SAndy Fiddaman struct boot_device {
135*5c4a5fe1SAndy Fiddaman TAILQ_ENTRY(boot_device) boot_device_chain;
136*5c4a5fe1SAndy Fiddaman struct pci_devinst *pdi;
137*5c4a5fe1SAndy Fiddaman int bootindex;
138*5c4a5fe1SAndy Fiddaman };
139*5c4a5fe1SAndy Fiddaman static TAILQ_HEAD(boot_list, boot_device) boot_devices = TAILQ_HEAD_INITIALIZER(
140*5c4a5fe1SAndy Fiddaman boot_devices);
141*5c4a5fe1SAndy Fiddaman
142*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_IOBASE 0x2000
143*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_IOLIMIT 0x10000
144*5c4a5fe1SAndy Fiddaman
145*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_ROMSIZE 0x10000000
146*5c4a5fe1SAndy Fiddaman
147*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_ECFG_BASE 0xE0000000 /* 3.5GB */
148*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_ECFG_SIZE (MAXBUSES * 1024 * 1024) /* 1MB per bus */
149*5c4a5fe1SAndy Fiddaman SYSRES_MEM(PCI_EMUL_ECFG_BASE, PCI_EMUL_ECFG_SIZE);
150*5c4a5fe1SAndy Fiddaman
151*5c4a5fe1SAndy Fiddaman /*
152*5c4a5fe1SAndy Fiddaman * OVMF always uses 0xC0000000 as base address for 32 bit PCI MMIO. Don't
153*5c4a5fe1SAndy Fiddaman * change this address without changing it in OVMF.
154*5c4a5fe1SAndy Fiddaman */
155*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_MEMBASE32 0xC0000000
156*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_MEMLIMIT32 PCI_EMUL_ECFG_BASE
157*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_MEMSIZE64 (32*GB)
158*5c4a5fe1SAndy Fiddaman
159*5c4a5fe1SAndy Fiddaman static struct pci_devemu *pci_emul_finddev(const char *name);
160*5c4a5fe1SAndy Fiddaman static void pci_lintr_route(struct pci_devinst *pi);
161*5c4a5fe1SAndy Fiddaman static void pci_lintr_update(struct pci_devinst *pi);
162*5c4a5fe1SAndy Fiddaman static void pci_cfgrw(int in, int bus, int slot, int func, int coff,
163*5c4a5fe1SAndy Fiddaman int bytes, uint32_t *val);
164*5c4a5fe1SAndy Fiddaman
165*5c4a5fe1SAndy Fiddaman static __inline void
CFGWRITE(struct pci_devinst * pi,int coff,uint32_t val,int bytes)166*5c4a5fe1SAndy Fiddaman CFGWRITE(struct pci_devinst *pi, int coff, uint32_t val, int bytes)
167*5c4a5fe1SAndy Fiddaman {
168*5c4a5fe1SAndy Fiddaman
169*5c4a5fe1SAndy Fiddaman if (bytes == 1)
170*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, coff, val);
171*5c4a5fe1SAndy Fiddaman else if (bytes == 2)
172*5c4a5fe1SAndy Fiddaman pci_set_cfgdata16(pi, coff, val);
173*5c4a5fe1SAndy Fiddaman else
174*5c4a5fe1SAndy Fiddaman pci_set_cfgdata32(pi, coff, val);
175*5c4a5fe1SAndy Fiddaman }
176*5c4a5fe1SAndy Fiddaman
177*5c4a5fe1SAndy Fiddaman static __inline uint32_t
CFGREAD(struct pci_devinst * pi,int coff,int bytes)178*5c4a5fe1SAndy Fiddaman CFGREAD(struct pci_devinst *pi, int coff, int bytes)
179*5c4a5fe1SAndy Fiddaman {
180*5c4a5fe1SAndy Fiddaman
181*5c4a5fe1SAndy Fiddaman if (bytes == 1)
182*5c4a5fe1SAndy Fiddaman return (pci_get_cfgdata8(pi, coff));
183*5c4a5fe1SAndy Fiddaman else if (bytes == 2)
184*5c4a5fe1SAndy Fiddaman return (pci_get_cfgdata16(pi, coff));
185*5c4a5fe1SAndy Fiddaman else
186*5c4a5fe1SAndy Fiddaman return (pci_get_cfgdata32(pi, coff));
187*5c4a5fe1SAndy Fiddaman }
188*5c4a5fe1SAndy Fiddaman
189*5c4a5fe1SAndy Fiddaman static int
is_pcir_bar(int coff)190*5c4a5fe1SAndy Fiddaman is_pcir_bar(int coff)
191*5c4a5fe1SAndy Fiddaman {
192*5c4a5fe1SAndy Fiddaman return (coff >= PCIR_BAR(0) && coff < PCIR_BAR(PCI_BARMAX + 1));
193*5c4a5fe1SAndy Fiddaman }
194*5c4a5fe1SAndy Fiddaman
195*5c4a5fe1SAndy Fiddaman static int
is_pcir_bios(int coff)196*5c4a5fe1SAndy Fiddaman is_pcir_bios(int coff)
197*5c4a5fe1SAndy Fiddaman {
198*5c4a5fe1SAndy Fiddaman return (coff >= PCIR_BIOS && coff < PCIR_BIOS + 4);
199*5c4a5fe1SAndy Fiddaman }
200*5c4a5fe1SAndy Fiddaman
201*5c4a5fe1SAndy Fiddaman /*
202*5c4a5fe1SAndy Fiddaman * I/O access
203*5c4a5fe1SAndy Fiddaman */
204*5c4a5fe1SAndy Fiddaman
205*5c4a5fe1SAndy Fiddaman /*
206*5c4a5fe1SAndy Fiddaman * Slot options are in the form:
207*5c4a5fe1SAndy Fiddaman *
208*5c4a5fe1SAndy Fiddaman * <bus>:<slot>:<func>,<emul>[,<config>]
209*5c4a5fe1SAndy Fiddaman * <slot>[:<func>],<emul>[,<config>]
210*5c4a5fe1SAndy Fiddaman *
211*5c4a5fe1SAndy Fiddaman * slot is 0..31
212*5c4a5fe1SAndy Fiddaman * func is 0..7
213*5c4a5fe1SAndy Fiddaman * emul is a string describing the type of PCI device e.g. virtio-net
214*5c4a5fe1SAndy Fiddaman * config is an optional string, depending on the device, that can be
215*5c4a5fe1SAndy Fiddaman * used for configuration.
216*5c4a5fe1SAndy Fiddaman * Examples are:
217*5c4a5fe1SAndy Fiddaman * 1,virtio-net,tap0
218*5c4a5fe1SAndy Fiddaman * 3:0,dummy
219*5c4a5fe1SAndy Fiddaman */
220*5c4a5fe1SAndy Fiddaman static void
pci_parse_slot_usage(char * aopt)221*5c4a5fe1SAndy Fiddaman pci_parse_slot_usage(char *aopt)
222*5c4a5fe1SAndy Fiddaman {
223*5c4a5fe1SAndy Fiddaman
224*5c4a5fe1SAndy Fiddaman EPRINTLN("Invalid PCI slot info field \"%s\"", aopt);
225*5c4a5fe1SAndy Fiddaman }
226*5c4a5fe1SAndy Fiddaman
227*5c4a5fe1SAndy Fiddaman /*
228*5c4a5fe1SAndy Fiddaman * Helper function to parse a list of comma-separated options where
229*5c4a5fe1SAndy Fiddaman * each option is formatted as "name[=value]". If no value is
230*5c4a5fe1SAndy Fiddaman * provided, the option is treated as a boolean and is given a value
231*5c4a5fe1SAndy Fiddaman * of true.
232*5c4a5fe1SAndy Fiddaman */
233*5c4a5fe1SAndy Fiddaman int
pci_parse_legacy_config(nvlist_t * nvl,const char * opt)234*5c4a5fe1SAndy Fiddaman pci_parse_legacy_config(nvlist_t *nvl, const char *opt)
235*5c4a5fe1SAndy Fiddaman {
236*5c4a5fe1SAndy Fiddaman char *config, *name, *tofree, *value;
237*5c4a5fe1SAndy Fiddaman
238*5c4a5fe1SAndy Fiddaman if (opt == NULL)
239*5c4a5fe1SAndy Fiddaman return (0);
240*5c4a5fe1SAndy Fiddaman
241*5c4a5fe1SAndy Fiddaman config = tofree = strdup(opt);
242*5c4a5fe1SAndy Fiddaman while ((name = strsep(&config, ",")) != NULL) {
243*5c4a5fe1SAndy Fiddaman value = strchr(name, '=');
244*5c4a5fe1SAndy Fiddaman if (value != NULL) {
245*5c4a5fe1SAndy Fiddaman *value = '\0';
246*5c4a5fe1SAndy Fiddaman value++;
247*5c4a5fe1SAndy Fiddaman set_config_value_node(nvl, name, value);
248*5c4a5fe1SAndy Fiddaman } else
249*5c4a5fe1SAndy Fiddaman set_config_bool_node(nvl, name, true);
250*5c4a5fe1SAndy Fiddaman }
251*5c4a5fe1SAndy Fiddaman free(tofree);
252*5c4a5fe1SAndy Fiddaman return (0);
253*5c4a5fe1SAndy Fiddaman }
254*5c4a5fe1SAndy Fiddaman
255*5c4a5fe1SAndy Fiddaman /*
256*5c4a5fe1SAndy Fiddaman * PCI device configuration is stored in MIBs that encode the device's
257*5c4a5fe1SAndy Fiddaman * location:
258*5c4a5fe1SAndy Fiddaman *
259*5c4a5fe1SAndy Fiddaman * pci.<bus>.<slot>.<func>
260*5c4a5fe1SAndy Fiddaman *
261*5c4a5fe1SAndy Fiddaman * Where "bus", "slot", and "func" are all decimal values without
262*5c4a5fe1SAndy Fiddaman * leading zeroes. Each valid device must have a "device" node which
263*5c4a5fe1SAndy Fiddaman * identifies the driver model of the device.
264*5c4a5fe1SAndy Fiddaman *
265*5c4a5fe1SAndy Fiddaman * Device backends can provide a parser for the "config" string. If
266*5c4a5fe1SAndy Fiddaman * a custom parser is not provided, pci_parse_legacy_config() is used
267*5c4a5fe1SAndy Fiddaman * to parse the string.
268*5c4a5fe1SAndy Fiddaman */
269*5c4a5fe1SAndy Fiddaman int
pci_parse_slot(char * opt)270*5c4a5fe1SAndy Fiddaman pci_parse_slot(char *opt)
271*5c4a5fe1SAndy Fiddaman {
272*5c4a5fe1SAndy Fiddaman char node_name[sizeof("pci.XXX.XX.X")];
273*5c4a5fe1SAndy Fiddaman struct pci_devemu *pde;
274*5c4a5fe1SAndy Fiddaman char *emul, *config, *str, *cp;
275*5c4a5fe1SAndy Fiddaman int error, bnum, snum, fnum;
276*5c4a5fe1SAndy Fiddaman nvlist_t *nvl;
277*5c4a5fe1SAndy Fiddaman
278*5c4a5fe1SAndy Fiddaman error = -1;
279*5c4a5fe1SAndy Fiddaman str = strdup(opt);
280*5c4a5fe1SAndy Fiddaman
281*5c4a5fe1SAndy Fiddaman emul = config = NULL;
282*5c4a5fe1SAndy Fiddaman if ((cp = strchr(str, ',')) != NULL) {
283*5c4a5fe1SAndy Fiddaman *cp = '\0';
284*5c4a5fe1SAndy Fiddaman emul = cp + 1;
285*5c4a5fe1SAndy Fiddaman if ((cp = strchr(emul, ',')) != NULL) {
286*5c4a5fe1SAndy Fiddaman *cp = '\0';
287*5c4a5fe1SAndy Fiddaman config = cp + 1;
288*5c4a5fe1SAndy Fiddaman }
289*5c4a5fe1SAndy Fiddaman } else {
290*5c4a5fe1SAndy Fiddaman pci_parse_slot_usage(opt);
291*5c4a5fe1SAndy Fiddaman goto done;
292*5c4a5fe1SAndy Fiddaman }
293*5c4a5fe1SAndy Fiddaman
294*5c4a5fe1SAndy Fiddaman /* <bus>:<slot>:<func> */
295*5c4a5fe1SAndy Fiddaman if (sscanf(str, "%d:%d:%d", &bnum, &snum, &fnum) != 3) {
296*5c4a5fe1SAndy Fiddaman bnum = 0;
297*5c4a5fe1SAndy Fiddaman /* <slot>:<func> */
298*5c4a5fe1SAndy Fiddaman if (sscanf(str, "%d:%d", &snum, &fnum) != 2) {
299*5c4a5fe1SAndy Fiddaman fnum = 0;
300*5c4a5fe1SAndy Fiddaman /* <slot> */
301*5c4a5fe1SAndy Fiddaman if (sscanf(str, "%d", &snum) != 1) {
302*5c4a5fe1SAndy Fiddaman snum = -1;
303*5c4a5fe1SAndy Fiddaman }
304*5c4a5fe1SAndy Fiddaman }
305*5c4a5fe1SAndy Fiddaman }
306*5c4a5fe1SAndy Fiddaman
307*5c4a5fe1SAndy Fiddaman if (bnum < 0 || bnum >= MAXBUSES || snum < 0 || snum >= MAXSLOTS ||
308*5c4a5fe1SAndy Fiddaman fnum < 0 || fnum >= MAXFUNCS) {
309*5c4a5fe1SAndy Fiddaman pci_parse_slot_usage(opt);
310*5c4a5fe1SAndy Fiddaman goto done;
311*5c4a5fe1SAndy Fiddaman }
312*5c4a5fe1SAndy Fiddaman
313*5c4a5fe1SAndy Fiddaman pde = pci_emul_finddev(emul);
314*5c4a5fe1SAndy Fiddaman if (pde == NULL) {
315*5c4a5fe1SAndy Fiddaman EPRINTLN("pci slot %d:%d:%d: unknown device \"%s\"", bnum, snum,
316*5c4a5fe1SAndy Fiddaman fnum, emul);
317*5c4a5fe1SAndy Fiddaman goto done;
318*5c4a5fe1SAndy Fiddaman }
319*5c4a5fe1SAndy Fiddaman
320*5c4a5fe1SAndy Fiddaman snprintf(node_name, sizeof(node_name), "pci.%d.%d.%d", bnum, snum,
321*5c4a5fe1SAndy Fiddaman fnum);
322*5c4a5fe1SAndy Fiddaman nvl = find_config_node(node_name);
323*5c4a5fe1SAndy Fiddaman if (nvl != NULL) {
324*5c4a5fe1SAndy Fiddaman EPRINTLN("pci slot %d:%d:%d already occupied!", bnum, snum,
325*5c4a5fe1SAndy Fiddaman fnum);
326*5c4a5fe1SAndy Fiddaman goto done;
327*5c4a5fe1SAndy Fiddaman }
328*5c4a5fe1SAndy Fiddaman nvl = create_config_node(node_name);
329*5c4a5fe1SAndy Fiddaman if (pde->pe_alias != NULL)
330*5c4a5fe1SAndy Fiddaman set_config_value_node(nvl, "device", pde->pe_alias);
331*5c4a5fe1SAndy Fiddaman else
332*5c4a5fe1SAndy Fiddaman set_config_value_node(nvl, "device", pde->pe_emu);
333*5c4a5fe1SAndy Fiddaman
334*5c4a5fe1SAndy Fiddaman if (pde->pe_legacy_config != NULL)
335*5c4a5fe1SAndy Fiddaman error = pde->pe_legacy_config(nvl, config);
336*5c4a5fe1SAndy Fiddaman else
337*5c4a5fe1SAndy Fiddaman error = pci_parse_legacy_config(nvl, config);
338*5c4a5fe1SAndy Fiddaman done:
339*5c4a5fe1SAndy Fiddaman free(str);
340*5c4a5fe1SAndy Fiddaman return (error);
341*5c4a5fe1SAndy Fiddaman }
342*5c4a5fe1SAndy Fiddaman
343*5c4a5fe1SAndy Fiddaman void
pci_print_supported_devices(void)344*5c4a5fe1SAndy Fiddaman pci_print_supported_devices(void)
345*5c4a5fe1SAndy Fiddaman {
346*5c4a5fe1SAndy Fiddaman struct pci_devemu **pdpp, *pdp;
347*5c4a5fe1SAndy Fiddaman
348*5c4a5fe1SAndy Fiddaman SET_FOREACH(pdpp, pci_devemu_set) {
349*5c4a5fe1SAndy Fiddaman pdp = *pdpp;
350*5c4a5fe1SAndy Fiddaman printf("%s\n", pdp->pe_emu);
351*5c4a5fe1SAndy Fiddaman }
352*5c4a5fe1SAndy Fiddaman }
353*5c4a5fe1SAndy Fiddaman
354*5c4a5fe1SAndy Fiddaman uint32_t
pci_config_read_reg(const struct pcisel * const host_sel,nvlist_t * nvl,const uint32_t reg,const uint8_t size,const uint32_t def)355*5c4a5fe1SAndy Fiddaman pci_config_read_reg(const struct pcisel *const host_sel, nvlist_t *nvl,
356*5c4a5fe1SAndy Fiddaman const uint32_t reg, const uint8_t size, const uint32_t def)
357*5c4a5fe1SAndy Fiddaman {
358*5c4a5fe1SAndy Fiddaman const char *config;
359*5c4a5fe1SAndy Fiddaman const nvlist_t *pci_regs;
360*5c4a5fe1SAndy Fiddaman
361*5c4a5fe1SAndy Fiddaman assert(size == 1 || size == 2 || size == 4);
362*5c4a5fe1SAndy Fiddaman
363*5c4a5fe1SAndy Fiddaman pci_regs = find_relative_config_node(nvl, "pcireg");
364*5c4a5fe1SAndy Fiddaman if (pci_regs == NULL) {
365*5c4a5fe1SAndy Fiddaman return def;
366*5c4a5fe1SAndy Fiddaman }
367*5c4a5fe1SAndy Fiddaman
368*5c4a5fe1SAndy Fiddaman switch (reg) {
369*5c4a5fe1SAndy Fiddaman case PCIR_DEVICE:
370*5c4a5fe1SAndy Fiddaman config = get_config_value_node(pci_regs, "device");
371*5c4a5fe1SAndy Fiddaman break;
372*5c4a5fe1SAndy Fiddaman case PCIR_VENDOR:
373*5c4a5fe1SAndy Fiddaman config = get_config_value_node(pci_regs, "vendor");
374*5c4a5fe1SAndy Fiddaman break;
375*5c4a5fe1SAndy Fiddaman case PCIR_REVID:
376*5c4a5fe1SAndy Fiddaman config = get_config_value_node(pci_regs, "revid");
377*5c4a5fe1SAndy Fiddaman break;
378*5c4a5fe1SAndy Fiddaman case PCIR_SUBVEND_0:
379*5c4a5fe1SAndy Fiddaman config = get_config_value_node(pci_regs, "subvendor");
380*5c4a5fe1SAndy Fiddaman break;
381*5c4a5fe1SAndy Fiddaman case PCIR_SUBDEV_0:
382*5c4a5fe1SAndy Fiddaman config = get_config_value_node(pci_regs, "subdevice");
383*5c4a5fe1SAndy Fiddaman break;
384*5c4a5fe1SAndy Fiddaman default:
385*5c4a5fe1SAndy Fiddaman return (-1);
386*5c4a5fe1SAndy Fiddaman }
387*5c4a5fe1SAndy Fiddaman
388*5c4a5fe1SAndy Fiddaman if (config == NULL) {
389*5c4a5fe1SAndy Fiddaman return def;
390*5c4a5fe1SAndy Fiddaman } else if (host_sel != NULL && strcmp(config, "host") == 0) {
391*5c4a5fe1SAndy Fiddaman return pci_host_read_config(host_sel, reg, size);
392*5c4a5fe1SAndy Fiddaman } else {
393*5c4a5fe1SAndy Fiddaman return strtol(config, NULL, 16);
394*5c4a5fe1SAndy Fiddaman }
395*5c4a5fe1SAndy Fiddaman }
396*5c4a5fe1SAndy Fiddaman
397*5c4a5fe1SAndy Fiddaman static int
pci_valid_pba_offset(struct pci_devinst * pi,uint64_t offset)398*5c4a5fe1SAndy Fiddaman pci_valid_pba_offset(struct pci_devinst *pi, uint64_t offset)
399*5c4a5fe1SAndy Fiddaman {
400*5c4a5fe1SAndy Fiddaman
401*5c4a5fe1SAndy Fiddaman if (offset < pi->pi_msix.pba_offset)
402*5c4a5fe1SAndy Fiddaman return (0);
403*5c4a5fe1SAndy Fiddaman
404*5c4a5fe1SAndy Fiddaman if (offset >= pi->pi_msix.pba_offset + pi->pi_msix.pba_size) {
405*5c4a5fe1SAndy Fiddaman return (0);
406*5c4a5fe1SAndy Fiddaman }
407*5c4a5fe1SAndy Fiddaman
408*5c4a5fe1SAndy Fiddaman return (1);
409*5c4a5fe1SAndy Fiddaman }
410*5c4a5fe1SAndy Fiddaman
411*5c4a5fe1SAndy Fiddaman int
pci_emul_msix_twrite(struct pci_devinst * pi,uint64_t offset,int size,uint64_t value)412*5c4a5fe1SAndy Fiddaman pci_emul_msix_twrite(struct pci_devinst *pi, uint64_t offset, int size,
413*5c4a5fe1SAndy Fiddaman uint64_t value)
414*5c4a5fe1SAndy Fiddaman {
415*5c4a5fe1SAndy Fiddaman int msix_entry_offset;
416*5c4a5fe1SAndy Fiddaman int tab_index;
417*5c4a5fe1SAndy Fiddaman char *dest;
418*5c4a5fe1SAndy Fiddaman
419*5c4a5fe1SAndy Fiddaman /* support only 4 or 8 byte writes */
420*5c4a5fe1SAndy Fiddaman if (size != 4 && size != 8)
421*5c4a5fe1SAndy Fiddaman return (-1);
422*5c4a5fe1SAndy Fiddaman
423*5c4a5fe1SAndy Fiddaman /*
424*5c4a5fe1SAndy Fiddaman * Return if table index is beyond what device supports
425*5c4a5fe1SAndy Fiddaman */
426*5c4a5fe1SAndy Fiddaman tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
427*5c4a5fe1SAndy Fiddaman if (tab_index >= pi->pi_msix.table_count)
428*5c4a5fe1SAndy Fiddaman return (-1);
429*5c4a5fe1SAndy Fiddaman
430*5c4a5fe1SAndy Fiddaman msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
431*5c4a5fe1SAndy Fiddaman
432*5c4a5fe1SAndy Fiddaman /* support only aligned writes */
433*5c4a5fe1SAndy Fiddaman if ((msix_entry_offset % size) != 0)
434*5c4a5fe1SAndy Fiddaman return (-1);
435*5c4a5fe1SAndy Fiddaman
436*5c4a5fe1SAndy Fiddaman dest = (char *)(pi->pi_msix.table + tab_index);
437*5c4a5fe1SAndy Fiddaman dest += msix_entry_offset;
438*5c4a5fe1SAndy Fiddaman
439*5c4a5fe1SAndy Fiddaman if (size == 4)
440*5c4a5fe1SAndy Fiddaman *((uint32_t *)dest) = value;
441*5c4a5fe1SAndy Fiddaman else
442*5c4a5fe1SAndy Fiddaman *((uint64_t *)dest) = value;
443*5c4a5fe1SAndy Fiddaman
444*5c4a5fe1SAndy Fiddaman return (0);
445*5c4a5fe1SAndy Fiddaman }
446*5c4a5fe1SAndy Fiddaman
447*5c4a5fe1SAndy Fiddaman uint64_t
pci_emul_msix_tread(struct pci_devinst * pi,uint64_t offset,int size)448*5c4a5fe1SAndy Fiddaman pci_emul_msix_tread(struct pci_devinst *pi, uint64_t offset, int size)
449*5c4a5fe1SAndy Fiddaman {
450*5c4a5fe1SAndy Fiddaman char *dest;
451*5c4a5fe1SAndy Fiddaman int msix_entry_offset;
452*5c4a5fe1SAndy Fiddaman int tab_index;
453*5c4a5fe1SAndy Fiddaman uint64_t retval = ~0;
454*5c4a5fe1SAndy Fiddaman
455*5c4a5fe1SAndy Fiddaman /*
456*5c4a5fe1SAndy Fiddaman * The PCI standard only allows 4 and 8 byte accesses to the MSI-X
457*5c4a5fe1SAndy Fiddaman * table but we also allow 1 byte access to accommodate reads from
458*5c4a5fe1SAndy Fiddaman * ddb.
459*5c4a5fe1SAndy Fiddaman */
460*5c4a5fe1SAndy Fiddaman if (size != 1 && size != 4 && size != 8)
461*5c4a5fe1SAndy Fiddaman return (retval);
462*5c4a5fe1SAndy Fiddaman
463*5c4a5fe1SAndy Fiddaman msix_entry_offset = offset % MSIX_TABLE_ENTRY_SIZE;
464*5c4a5fe1SAndy Fiddaman
465*5c4a5fe1SAndy Fiddaman /* support only aligned reads */
466*5c4a5fe1SAndy Fiddaman if ((msix_entry_offset % size) != 0) {
467*5c4a5fe1SAndy Fiddaman return (retval);
468*5c4a5fe1SAndy Fiddaman }
469*5c4a5fe1SAndy Fiddaman
470*5c4a5fe1SAndy Fiddaman tab_index = offset / MSIX_TABLE_ENTRY_SIZE;
471*5c4a5fe1SAndy Fiddaman
472*5c4a5fe1SAndy Fiddaman if (tab_index < pi->pi_msix.table_count) {
473*5c4a5fe1SAndy Fiddaman /* valid MSI-X Table access */
474*5c4a5fe1SAndy Fiddaman dest = (char *)(pi->pi_msix.table + tab_index);
475*5c4a5fe1SAndy Fiddaman dest += msix_entry_offset;
476*5c4a5fe1SAndy Fiddaman
477*5c4a5fe1SAndy Fiddaman if (size == 1)
478*5c4a5fe1SAndy Fiddaman retval = *((uint8_t *)dest);
479*5c4a5fe1SAndy Fiddaman else if (size == 4)
480*5c4a5fe1SAndy Fiddaman retval = *((uint32_t *)dest);
481*5c4a5fe1SAndy Fiddaman else
482*5c4a5fe1SAndy Fiddaman retval = *((uint64_t *)dest);
483*5c4a5fe1SAndy Fiddaman } else if (pci_valid_pba_offset(pi, offset)) {
484*5c4a5fe1SAndy Fiddaman /* return 0 for PBA access */
485*5c4a5fe1SAndy Fiddaman retval = 0;
486*5c4a5fe1SAndy Fiddaman }
487*5c4a5fe1SAndy Fiddaman
488*5c4a5fe1SAndy Fiddaman return (retval);
489*5c4a5fe1SAndy Fiddaman }
490*5c4a5fe1SAndy Fiddaman
491*5c4a5fe1SAndy Fiddaman int
pci_msix_table_bar(struct pci_devinst * pi)492*5c4a5fe1SAndy Fiddaman pci_msix_table_bar(struct pci_devinst *pi)
493*5c4a5fe1SAndy Fiddaman {
494*5c4a5fe1SAndy Fiddaman
495*5c4a5fe1SAndy Fiddaman if (pi->pi_msix.table != NULL)
496*5c4a5fe1SAndy Fiddaman return (pi->pi_msix.table_bar);
497*5c4a5fe1SAndy Fiddaman else
498*5c4a5fe1SAndy Fiddaman return (-1);
499*5c4a5fe1SAndy Fiddaman }
500*5c4a5fe1SAndy Fiddaman
501*5c4a5fe1SAndy Fiddaman int
pci_msix_pba_bar(struct pci_devinst * pi)502*5c4a5fe1SAndy Fiddaman pci_msix_pba_bar(struct pci_devinst *pi)
503*5c4a5fe1SAndy Fiddaman {
504*5c4a5fe1SAndy Fiddaman
505*5c4a5fe1SAndy Fiddaman if (pi->pi_msix.table != NULL)
506*5c4a5fe1SAndy Fiddaman return (pi->pi_msix.pba_bar);
507*5c4a5fe1SAndy Fiddaman else
508*5c4a5fe1SAndy Fiddaman return (-1);
509*5c4a5fe1SAndy Fiddaman }
510*5c4a5fe1SAndy Fiddaman
511*5c4a5fe1SAndy Fiddaman static int
pci_emul_io_handler(struct vmctx * ctx __unused,int in,int port,int bytes,uint32_t * eax,void * arg)512*5c4a5fe1SAndy Fiddaman pci_emul_io_handler(struct vmctx *ctx __unused, int in, int port,
513*5c4a5fe1SAndy Fiddaman int bytes, uint32_t *eax, void *arg)
514*5c4a5fe1SAndy Fiddaman {
515*5c4a5fe1SAndy Fiddaman struct pci_devinst *pdi = arg;
516*5c4a5fe1SAndy Fiddaman struct pci_devemu *pe = pdi->pi_d;
517*5c4a5fe1SAndy Fiddaman uint64_t offset;
518*5c4a5fe1SAndy Fiddaman int i;
519*5c4a5fe1SAndy Fiddaman
520*5c4a5fe1SAndy Fiddaman assert(port >= 0);
521*5c4a5fe1SAndy Fiddaman
522*5c4a5fe1SAndy Fiddaman for (i = 0; i <= PCI_BARMAX; i++) {
523*5c4a5fe1SAndy Fiddaman if (pdi->pi_bar[i].type == PCIBAR_IO &&
524*5c4a5fe1SAndy Fiddaman (uint64_t)port >= pdi->pi_bar[i].addr &&
525*5c4a5fe1SAndy Fiddaman (uint64_t)port + bytes <=
526*5c4a5fe1SAndy Fiddaman pdi->pi_bar[i].addr + pdi->pi_bar[i].size) {
527*5c4a5fe1SAndy Fiddaman offset = port - pdi->pi_bar[i].addr;
528*5c4a5fe1SAndy Fiddaman if (in)
529*5c4a5fe1SAndy Fiddaman *eax = (*pe->pe_barread)(pdi, i,
530*5c4a5fe1SAndy Fiddaman offset, bytes);
531*5c4a5fe1SAndy Fiddaman else
532*5c4a5fe1SAndy Fiddaman (*pe->pe_barwrite)(pdi, i, offset,
533*5c4a5fe1SAndy Fiddaman bytes, *eax);
534*5c4a5fe1SAndy Fiddaman return (0);
535*5c4a5fe1SAndy Fiddaman }
536*5c4a5fe1SAndy Fiddaman }
537*5c4a5fe1SAndy Fiddaman return (-1);
538*5c4a5fe1SAndy Fiddaman }
539*5c4a5fe1SAndy Fiddaman
540*5c4a5fe1SAndy Fiddaman static int
pci_emul_mem_handler(struct vcpu * vcpu __unused,int dir,uint64_t addr,int size,uint64_t * val,void * arg1,long arg2)541*5c4a5fe1SAndy Fiddaman pci_emul_mem_handler(struct vcpu *vcpu __unused, int dir,
542*5c4a5fe1SAndy Fiddaman uint64_t addr, int size, uint64_t *val, void *arg1, long arg2)
543*5c4a5fe1SAndy Fiddaman {
544*5c4a5fe1SAndy Fiddaman struct pci_devinst *pdi = arg1;
545*5c4a5fe1SAndy Fiddaman struct pci_devemu *pe = pdi->pi_d;
546*5c4a5fe1SAndy Fiddaman uint64_t offset;
547*5c4a5fe1SAndy Fiddaman int bidx = (int)arg2;
548*5c4a5fe1SAndy Fiddaman
549*5c4a5fe1SAndy Fiddaman assert(bidx <= PCI_BARMAX);
550*5c4a5fe1SAndy Fiddaman assert(pdi->pi_bar[bidx].type == PCIBAR_MEM32 ||
551*5c4a5fe1SAndy Fiddaman pdi->pi_bar[bidx].type == PCIBAR_MEM64);
552*5c4a5fe1SAndy Fiddaman assert(addr >= pdi->pi_bar[bidx].addr &&
553*5c4a5fe1SAndy Fiddaman addr + size <= pdi->pi_bar[bidx].addr + pdi->pi_bar[bidx].size);
554*5c4a5fe1SAndy Fiddaman
555*5c4a5fe1SAndy Fiddaman offset = addr - pdi->pi_bar[bidx].addr;
556*5c4a5fe1SAndy Fiddaman
557*5c4a5fe1SAndy Fiddaman if (dir == MEM_F_WRITE) {
558*5c4a5fe1SAndy Fiddaman if (size == 8) {
559*5c4a5fe1SAndy Fiddaman (*pe->pe_barwrite)(pdi, bidx, offset,
560*5c4a5fe1SAndy Fiddaman 4, *val & 0xffffffff);
561*5c4a5fe1SAndy Fiddaman (*pe->pe_barwrite)(pdi, bidx, offset + 4,
562*5c4a5fe1SAndy Fiddaman 4, *val >> 32);
563*5c4a5fe1SAndy Fiddaman } else {
564*5c4a5fe1SAndy Fiddaman (*pe->pe_barwrite)(pdi, bidx, offset,
565*5c4a5fe1SAndy Fiddaman size, *val);
566*5c4a5fe1SAndy Fiddaman }
567*5c4a5fe1SAndy Fiddaman } else {
568*5c4a5fe1SAndy Fiddaman if (size == 8) {
569*5c4a5fe1SAndy Fiddaman *val = (*pe->pe_barread)(pdi, bidx,
570*5c4a5fe1SAndy Fiddaman offset, 4);
571*5c4a5fe1SAndy Fiddaman *val |= (*pe->pe_barread)(pdi, bidx,
572*5c4a5fe1SAndy Fiddaman offset + 4, 4) << 32;
573*5c4a5fe1SAndy Fiddaman } else {
574*5c4a5fe1SAndy Fiddaman *val = (*pe->pe_barread)(pdi, bidx,
575*5c4a5fe1SAndy Fiddaman offset, size);
576*5c4a5fe1SAndy Fiddaman }
577*5c4a5fe1SAndy Fiddaman }
578*5c4a5fe1SAndy Fiddaman
579*5c4a5fe1SAndy Fiddaman return (0);
580*5c4a5fe1SAndy Fiddaman }
581*5c4a5fe1SAndy Fiddaman
582*5c4a5fe1SAndy Fiddaman
583*5c4a5fe1SAndy Fiddaman static int
pci_emul_alloc_resource(uint64_t * baseptr,uint64_t limit,uint64_t size,uint64_t * addr)584*5c4a5fe1SAndy Fiddaman pci_emul_alloc_resource(uint64_t *baseptr, uint64_t limit, uint64_t size,
585*5c4a5fe1SAndy Fiddaman uint64_t *addr)
586*5c4a5fe1SAndy Fiddaman {
587*5c4a5fe1SAndy Fiddaman uint64_t base;
588*5c4a5fe1SAndy Fiddaman
589*5c4a5fe1SAndy Fiddaman assert((size & (size - 1)) == 0); /* must be a power of 2 */
590*5c4a5fe1SAndy Fiddaman
591*5c4a5fe1SAndy Fiddaman base = roundup2(*baseptr, size);
592*5c4a5fe1SAndy Fiddaman
593*5c4a5fe1SAndy Fiddaman if (base + size <= limit) {
594*5c4a5fe1SAndy Fiddaman *addr = base;
595*5c4a5fe1SAndy Fiddaman *baseptr = base + size;
596*5c4a5fe1SAndy Fiddaman return (0);
597*5c4a5fe1SAndy Fiddaman } else
598*5c4a5fe1SAndy Fiddaman return (-1);
599*5c4a5fe1SAndy Fiddaman }
600*5c4a5fe1SAndy Fiddaman
601*5c4a5fe1SAndy Fiddaman /*
602*5c4a5fe1SAndy Fiddaman * Register (or unregister) the MMIO or I/O region associated with the BAR
603*5c4a5fe1SAndy Fiddaman * register 'idx' of an emulated pci device.
604*5c4a5fe1SAndy Fiddaman */
605*5c4a5fe1SAndy Fiddaman static void
modify_bar_registration(struct pci_devinst * pi,int idx,int registration)606*5c4a5fe1SAndy Fiddaman modify_bar_registration(struct pci_devinst *pi, int idx, int registration)
607*5c4a5fe1SAndy Fiddaman {
608*5c4a5fe1SAndy Fiddaman struct pci_devemu *pe;
609*5c4a5fe1SAndy Fiddaman int error;
610*5c4a5fe1SAndy Fiddaman struct inout_port iop;
611*5c4a5fe1SAndy Fiddaman struct mem_range mr;
612*5c4a5fe1SAndy Fiddaman
613*5c4a5fe1SAndy Fiddaman pe = pi->pi_d;
614*5c4a5fe1SAndy Fiddaman switch (pi->pi_bar[idx].type) {
615*5c4a5fe1SAndy Fiddaman case PCIBAR_IO:
616*5c4a5fe1SAndy Fiddaman bzero(&iop, sizeof(struct inout_port));
617*5c4a5fe1SAndy Fiddaman iop.name = pi->pi_name;
618*5c4a5fe1SAndy Fiddaman iop.port = pi->pi_bar[idx].addr;
619*5c4a5fe1SAndy Fiddaman iop.size = pi->pi_bar[idx].size;
620*5c4a5fe1SAndy Fiddaman if (registration) {
621*5c4a5fe1SAndy Fiddaman iop.flags = IOPORT_F_INOUT;
622*5c4a5fe1SAndy Fiddaman iop.handler = pci_emul_io_handler;
623*5c4a5fe1SAndy Fiddaman iop.arg = pi;
624*5c4a5fe1SAndy Fiddaman error = register_inout(&iop);
625*5c4a5fe1SAndy Fiddaman } else
626*5c4a5fe1SAndy Fiddaman error = unregister_inout(&iop);
627*5c4a5fe1SAndy Fiddaman break;
628*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM32:
629*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM64:
630*5c4a5fe1SAndy Fiddaman bzero(&mr, sizeof(struct mem_range));
631*5c4a5fe1SAndy Fiddaman mr.name = pi->pi_name;
632*5c4a5fe1SAndy Fiddaman mr.base = pi->pi_bar[idx].addr;
633*5c4a5fe1SAndy Fiddaman mr.size = pi->pi_bar[idx].size;
634*5c4a5fe1SAndy Fiddaman if (registration) {
635*5c4a5fe1SAndy Fiddaman mr.flags = MEM_F_RW;
636*5c4a5fe1SAndy Fiddaman mr.handler = pci_emul_mem_handler;
637*5c4a5fe1SAndy Fiddaman mr.arg1 = pi;
638*5c4a5fe1SAndy Fiddaman mr.arg2 = idx;
639*5c4a5fe1SAndy Fiddaman error = register_mem(&mr);
640*5c4a5fe1SAndy Fiddaman } else
641*5c4a5fe1SAndy Fiddaman error = unregister_mem(&mr);
642*5c4a5fe1SAndy Fiddaman break;
643*5c4a5fe1SAndy Fiddaman case PCIBAR_ROM:
644*5c4a5fe1SAndy Fiddaman error = 0;
645*5c4a5fe1SAndy Fiddaman break;
646*5c4a5fe1SAndy Fiddaman default:
647*5c4a5fe1SAndy Fiddaman error = EINVAL;
648*5c4a5fe1SAndy Fiddaman break;
649*5c4a5fe1SAndy Fiddaman }
650*5c4a5fe1SAndy Fiddaman assert(error == 0);
651*5c4a5fe1SAndy Fiddaman
652*5c4a5fe1SAndy Fiddaman if (pe->pe_baraddr != NULL)
653*5c4a5fe1SAndy Fiddaman (*pe->pe_baraddr)(pi, idx, registration, pi->pi_bar[idx].addr);
654*5c4a5fe1SAndy Fiddaman }
655*5c4a5fe1SAndy Fiddaman
656*5c4a5fe1SAndy Fiddaman static void
unregister_bar(struct pci_devinst * pi,int idx)657*5c4a5fe1SAndy Fiddaman unregister_bar(struct pci_devinst *pi, int idx)
658*5c4a5fe1SAndy Fiddaman {
659*5c4a5fe1SAndy Fiddaman
660*5c4a5fe1SAndy Fiddaman modify_bar_registration(pi, idx, 0);
661*5c4a5fe1SAndy Fiddaman }
662*5c4a5fe1SAndy Fiddaman
663*5c4a5fe1SAndy Fiddaman static void
register_bar(struct pci_devinst * pi,int idx)664*5c4a5fe1SAndy Fiddaman register_bar(struct pci_devinst *pi, int idx)
665*5c4a5fe1SAndy Fiddaman {
666*5c4a5fe1SAndy Fiddaman
667*5c4a5fe1SAndy Fiddaman modify_bar_registration(pi, idx, 1);
668*5c4a5fe1SAndy Fiddaman }
669*5c4a5fe1SAndy Fiddaman
670*5c4a5fe1SAndy Fiddaman /* Is the ROM enabled for the emulated pci device? */
671*5c4a5fe1SAndy Fiddaman static int
romen(struct pci_devinst * pi)672*5c4a5fe1SAndy Fiddaman romen(struct pci_devinst *pi)
673*5c4a5fe1SAndy Fiddaman {
674*5c4a5fe1SAndy Fiddaman return (pi->pi_bar[PCI_ROM_IDX].lobits & PCIM_BIOS_ENABLE) ==
675*5c4a5fe1SAndy Fiddaman PCIM_BIOS_ENABLE;
676*5c4a5fe1SAndy Fiddaman }
677*5c4a5fe1SAndy Fiddaman
678*5c4a5fe1SAndy Fiddaman /* Are we decoding i/o port accesses for the emulated pci device? */
679*5c4a5fe1SAndy Fiddaman static int
porten(struct pci_devinst * pi)680*5c4a5fe1SAndy Fiddaman porten(struct pci_devinst *pi)
681*5c4a5fe1SAndy Fiddaman {
682*5c4a5fe1SAndy Fiddaman uint16_t cmd;
683*5c4a5fe1SAndy Fiddaman
684*5c4a5fe1SAndy Fiddaman cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
685*5c4a5fe1SAndy Fiddaman
686*5c4a5fe1SAndy Fiddaman return (cmd & PCIM_CMD_PORTEN);
687*5c4a5fe1SAndy Fiddaman }
688*5c4a5fe1SAndy Fiddaman
689*5c4a5fe1SAndy Fiddaman /* Are we decoding memory accesses for the emulated pci device? */
690*5c4a5fe1SAndy Fiddaman static int
memen(struct pci_devinst * pi)691*5c4a5fe1SAndy Fiddaman memen(struct pci_devinst *pi)
692*5c4a5fe1SAndy Fiddaman {
693*5c4a5fe1SAndy Fiddaman uint16_t cmd;
694*5c4a5fe1SAndy Fiddaman
695*5c4a5fe1SAndy Fiddaman cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
696*5c4a5fe1SAndy Fiddaman
697*5c4a5fe1SAndy Fiddaman return (cmd & PCIM_CMD_MEMEN);
698*5c4a5fe1SAndy Fiddaman }
699*5c4a5fe1SAndy Fiddaman
700*5c4a5fe1SAndy Fiddaman /*
701*5c4a5fe1SAndy Fiddaman * Update the MMIO or I/O address that is decoded by the BAR register.
702*5c4a5fe1SAndy Fiddaman *
703*5c4a5fe1SAndy Fiddaman * If the pci device has enabled the address space decoding then intercept
704*5c4a5fe1SAndy Fiddaman * the address range decoded by the BAR register.
705*5c4a5fe1SAndy Fiddaman */
706*5c4a5fe1SAndy Fiddaman static void
update_bar_address(struct pci_devinst * pi,uint64_t addr,int idx,int type)707*5c4a5fe1SAndy Fiddaman update_bar_address(struct pci_devinst *pi, uint64_t addr, int idx, int type)
708*5c4a5fe1SAndy Fiddaman {
709*5c4a5fe1SAndy Fiddaman int decode;
710*5c4a5fe1SAndy Fiddaman
711*5c4a5fe1SAndy Fiddaman if (pi->pi_bar[idx].type == PCIBAR_IO)
712*5c4a5fe1SAndy Fiddaman decode = porten(pi);
713*5c4a5fe1SAndy Fiddaman else
714*5c4a5fe1SAndy Fiddaman decode = memen(pi);
715*5c4a5fe1SAndy Fiddaman
716*5c4a5fe1SAndy Fiddaman if (decode)
717*5c4a5fe1SAndy Fiddaman unregister_bar(pi, idx);
718*5c4a5fe1SAndy Fiddaman
719*5c4a5fe1SAndy Fiddaman switch (type) {
720*5c4a5fe1SAndy Fiddaman case PCIBAR_IO:
721*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM32:
722*5c4a5fe1SAndy Fiddaman pi->pi_bar[idx].addr = addr;
723*5c4a5fe1SAndy Fiddaman break;
724*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM64:
725*5c4a5fe1SAndy Fiddaman pi->pi_bar[idx].addr &= ~0xffffffffUL;
726*5c4a5fe1SAndy Fiddaman pi->pi_bar[idx].addr |= addr;
727*5c4a5fe1SAndy Fiddaman break;
728*5c4a5fe1SAndy Fiddaman case PCIBAR_MEMHI64:
729*5c4a5fe1SAndy Fiddaman pi->pi_bar[idx].addr &= 0xffffffff;
730*5c4a5fe1SAndy Fiddaman pi->pi_bar[idx].addr |= addr;
731*5c4a5fe1SAndy Fiddaman break;
732*5c4a5fe1SAndy Fiddaman default:
733*5c4a5fe1SAndy Fiddaman assert(0);
734*5c4a5fe1SAndy Fiddaman }
735*5c4a5fe1SAndy Fiddaman
736*5c4a5fe1SAndy Fiddaman if (decode)
737*5c4a5fe1SAndy Fiddaman register_bar(pi, idx);
738*5c4a5fe1SAndy Fiddaman }
739*5c4a5fe1SAndy Fiddaman
740*5c4a5fe1SAndy Fiddaman int
pci_emul_alloc_bar(struct pci_devinst * pdi,int idx,enum pcibar_type type,uint64_t size)741*5c4a5fe1SAndy Fiddaman pci_emul_alloc_bar(struct pci_devinst *pdi, int idx, enum pcibar_type type,
742*5c4a5fe1SAndy Fiddaman uint64_t size)
743*5c4a5fe1SAndy Fiddaman {
744*5c4a5fe1SAndy Fiddaman assert((type == PCIBAR_ROM) || (idx >= 0 && idx <= PCI_BARMAX));
745*5c4a5fe1SAndy Fiddaman assert((type != PCIBAR_ROM) || (idx == PCI_ROM_IDX));
746*5c4a5fe1SAndy Fiddaman
747*5c4a5fe1SAndy Fiddaman if ((size & (size - 1)) != 0)
748*5c4a5fe1SAndy Fiddaman size = 1UL << flsl(size); /* round up to a power of 2 */
749*5c4a5fe1SAndy Fiddaman
750*5c4a5fe1SAndy Fiddaman /* Enforce minimum BAR sizes required by the PCI standard */
751*5c4a5fe1SAndy Fiddaman if (type == PCIBAR_IO) {
752*5c4a5fe1SAndy Fiddaman if (size < 4)
753*5c4a5fe1SAndy Fiddaman size = 4;
754*5c4a5fe1SAndy Fiddaman } else if (type == PCIBAR_ROM) {
755*5c4a5fe1SAndy Fiddaman if (size < ~PCIM_BIOS_ADDR_MASK + 1)
756*5c4a5fe1SAndy Fiddaman size = ~PCIM_BIOS_ADDR_MASK + 1;
757*5c4a5fe1SAndy Fiddaman } else {
758*5c4a5fe1SAndy Fiddaman if (size < 16)
759*5c4a5fe1SAndy Fiddaman size = 16;
760*5c4a5fe1SAndy Fiddaman }
761*5c4a5fe1SAndy Fiddaman
762*5c4a5fe1SAndy Fiddaman /*
763*5c4a5fe1SAndy Fiddaman * To reduce fragmentation of the MMIO space, we allocate the BARs by
764*5c4a5fe1SAndy Fiddaman * size. Therefore, don't allocate the BAR yet. We create a list of all
765*5c4a5fe1SAndy Fiddaman * BAR allocation which is sorted by BAR size. When all PCI devices are
766*5c4a5fe1SAndy Fiddaman * initialized, we will assign an address to the BARs.
767*5c4a5fe1SAndy Fiddaman */
768*5c4a5fe1SAndy Fiddaman
769*5c4a5fe1SAndy Fiddaman /* create a new list entry */
770*5c4a5fe1SAndy Fiddaman struct pci_bar_allocation *const new_bar = malloc(sizeof(*new_bar));
771*5c4a5fe1SAndy Fiddaman memset(new_bar, 0, sizeof(*new_bar));
772*5c4a5fe1SAndy Fiddaman new_bar->pdi = pdi;
773*5c4a5fe1SAndy Fiddaman new_bar->idx = idx;
774*5c4a5fe1SAndy Fiddaman new_bar->type = type;
775*5c4a5fe1SAndy Fiddaman new_bar->size = size;
776*5c4a5fe1SAndy Fiddaman
777*5c4a5fe1SAndy Fiddaman /*
778*5c4a5fe1SAndy Fiddaman * Search for a BAR which size is lower than the size of our newly
779*5c4a5fe1SAndy Fiddaman * allocated BAR.
780*5c4a5fe1SAndy Fiddaman */
781*5c4a5fe1SAndy Fiddaman struct pci_bar_allocation *bar = NULL;
782*5c4a5fe1SAndy Fiddaman TAILQ_FOREACH(bar, &pci_bars, chain) {
783*5c4a5fe1SAndy Fiddaman if (bar->size < size) {
784*5c4a5fe1SAndy Fiddaman break;
785*5c4a5fe1SAndy Fiddaman }
786*5c4a5fe1SAndy Fiddaman }
787*5c4a5fe1SAndy Fiddaman
788*5c4a5fe1SAndy Fiddaman if (bar == NULL) {
789*5c4a5fe1SAndy Fiddaman /*
790*5c4a5fe1SAndy Fiddaman * Either the list is empty or new BAR is the smallest BAR of
791*5c4a5fe1SAndy Fiddaman * the list. Append it to the end of our list.
792*5c4a5fe1SAndy Fiddaman */
793*5c4a5fe1SAndy Fiddaman TAILQ_INSERT_TAIL(&pci_bars, new_bar, chain);
794*5c4a5fe1SAndy Fiddaman } else {
795*5c4a5fe1SAndy Fiddaman /*
796*5c4a5fe1SAndy Fiddaman * The found BAR is smaller than our new BAR. For that reason,
797*5c4a5fe1SAndy Fiddaman * insert our new BAR before the found BAR.
798*5c4a5fe1SAndy Fiddaman */
799*5c4a5fe1SAndy Fiddaman TAILQ_INSERT_BEFORE(bar, new_bar, chain);
800*5c4a5fe1SAndy Fiddaman }
801*5c4a5fe1SAndy Fiddaman
802*5c4a5fe1SAndy Fiddaman #ifdef __FreeBSD__
803*5c4a5fe1SAndy Fiddaman /*
804*5c4a5fe1SAndy Fiddaman * Enable PCI BARs only if we don't have a boot ROM, i.e., bhyveload was
805*5c4a5fe1SAndy Fiddaman * used to load the initial guest image. Otherwise, we rely on the boot
806*5c4a5fe1SAndy Fiddaman * ROM to handle this.
807*5c4a5fe1SAndy Fiddaman */
808*5c4a5fe1SAndy Fiddaman if (!get_config_bool_default("pci.enable_bars", !bootrom_boot()))
809*5c4a5fe1SAndy Fiddaman return (0);
810*5c4a5fe1SAndy Fiddaman #else
811*5c4a5fe1SAndy Fiddaman /*
812*5c4a5fe1SAndy Fiddaman * Enable PCI BARs unless specifically requested not to. Bootroms
813*5c4a5fe1SAndy Fiddaman * generally used in illumos do not perform PCI BAR enumeration
814*5c4a5fe1SAndy Fiddaman * themselves and so need the BARs enabling here.
815*5c4a5fe1SAndy Fiddaman */
816*5c4a5fe1SAndy Fiddaman if (!get_config_bool_default("pci.enable_bars", true))
817*5c4a5fe1SAndy Fiddaman return (0);
818*5c4a5fe1SAndy Fiddaman #endif
819*5c4a5fe1SAndy Fiddaman
820*5c4a5fe1SAndy Fiddaman /*
821*5c4a5fe1SAndy Fiddaman * pci_passthru devices synchronize their physical and virtual command
822*5c4a5fe1SAndy Fiddaman * register on init. For that reason, the virtual cmd reg should be
823*5c4a5fe1SAndy Fiddaman * updated as early as possible.
824*5c4a5fe1SAndy Fiddaman */
825*5c4a5fe1SAndy Fiddaman uint16_t enbit = 0;
826*5c4a5fe1SAndy Fiddaman switch (type) {
827*5c4a5fe1SAndy Fiddaman case PCIBAR_IO:
828*5c4a5fe1SAndy Fiddaman enbit = PCIM_CMD_PORTEN;
829*5c4a5fe1SAndy Fiddaman break;
830*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM64:
831*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM32:
832*5c4a5fe1SAndy Fiddaman enbit = PCIM_CMD_MEMEN;
833*5c4a5fe1SAndy Fiddaman break;
834*5c4a5fe1SAndy Fiddaman default:
835*5c4a5fe1SAndy Fiddaman enbit = 0;
836*5c4a5fe1SAndy Fiddaman break;
837*5c4a5fe1SAndy Fiddaman }
838*5c4a5fe1SAndy Fiddaman
839*5c4a5fe1SAndy Fiddaman const uint16_t cmd = pci_get_cfgdata16(pdi, PCIR_COMMAND);
840*5c4a5fe1SAndy Fiddaman pci_set_cfgdata16(pdi, PCIR_COMMAND, cmd | enbit);
841*5c4a5fe1SAndy Fiddaman
842*5c4a5fe1SAndy Fiddaman return (0);
843*5c4a5fe1SAndy Fiddaman }
844*5c4a5fe1SAndy Fiddaman
845*5c4a5fe1SAndy Fiddaman static int
pci_emul_assign_bar(struct pci_devinst * const pdi,const int idx,const enum pcibar_type type,const uint64_t size)846*5c4a5fe1SAndy Fiddaman pci_emul_assign_bar(struct pci_devinst *const pdi, const int idx,
847*5c4a5fe1SAndy Fiddaman const enum pcibar_type type, const uint64_t size)
848*5c4a5fe1SAndy Fiddaman {
849*5c4a5fe1SAndy Fiddaman int error;
850*5c4a5fe1SAndy Fiddaman uint64_t *baseptr, limit, addr, mask, lobits, bar;
851*5c4a5fe1SAndy Fiddaman
852*5c4a5fe1SAndy Fiddaman switch (type) {
853*5c4a5fe1SAndy Fiddaman case PCIBAR_NONE:
854*5c4a5fe1SAndy Fiddaman baseptr = NULL;
855*5c4a5fe1SAndy Fiddaman addr = mask = lobits = 0;
856*5c4a5fe1SAndy Fiddaman break;
857*5c4a5fe1SAndy Fiddaman case PCIBAR_IO:
858*5c4a5fe1SAndy Fiddaman baseptr = &pci_emul_iobase;
859*5c4a5fe1SAndy Fiddaman limit = PCI_EMUL_IOLIMIT;
860*5c4a5fe1SAndy Fiddaman mask = PCIM_BAR_IO_BASE;
861*5c4a5fe1SAndy Fiddaman lobits = PCIM_BAR_IO_SPACE;
862*5c4a5fe1SAndy Fiddaman break;
863*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM64:
864*5c4a5fe1SAndy Fiddaman /*
865*5c4a5fe1SAndy Fiddaman * XXX
866*5c4a5fe1SAndy Fiddaman * Some drivers do not work well if the 64-bit BAR is allocated
867*5c4a5fe1SAndy Fiddaman * above 4GB. Allow for this by allocating small requests under
868*5c4a5fe1SAndy Fiddaman * 4GB unless then allocation size is larger than some arbitrary
869*5c4a5fe1SAndy Fiddaman * number (128MB currently).
870*5c4a5fe1SAndy Fiddaman */
871*5c4a5fe1SAndy Fiddaman if (size > 128 * 1024 * 1024) {
872*5c4a5fe1SAndy Fiddaman baseptr = &pci_emul_membase64;
873*5c4a5fe1SAndy Fiddaman limit = pci_emul_memlim64;
874*5c4a5fe1SAndy Fiddaman mask = PCIM_BAR_MEM_BASE;
875*5c4a5fe1SAndy Fiddaman lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64 |
876*5c4a5fe1SAndy Fiddaman PCIM_BAR_MEM_PREFETCH;
877*5c4a5fe1SAndy Fiddaman } else {
878*5c4a5fe1SAndy Fiddaman baseptr = &pci_emul_membase32;
879*5c4a5fe1SAndy Fiddaman limit = PCI_EMUL_MEMLIMIT32;
880*5c4a5fe1SAndy Fiddaman mask = PCIM_BAR_MEM_BASE;
881*5c4a5fe1SAndy Fiddaman lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_64;
882*5c4a5fe1SAndy Fiddaman }
883*5c4a5fe1SAndy Fiddaman break;
884*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM32:
885*5c4a5fe1SAndy Fiddaman baseptr = &pci_emul_membase32;
886*5c4a5fe1SAndy Fiddaman limit = PCI_EMUL_MEMLIMIT32;
887*5c4a5fe1SAndy Fiddaman mask = PCIM_BAR_MEM_BASE;
888*5c4a5fe1SAndy Fiddaman lobits = PCIM_BAR_MEM_SPACE | PCIM_BAR_MEM_32;
889*5c4a5fe1SAndy Fiddaman break;
890*5c4a5fe1SAndy Fiddaman case PCIBAR_ROM:
891*5c4a5fe1SAndy Fiddaman /* do not claim memory for ROM. OVMF will do it for us. */
892*5c4a5fe1SAndy Fiddaman baseptr = NULL;
893*5c4a5fe1SAndy Fiddaman limit = 0;
894*5c4a5fe1SAndy Fiddaman mask = PCIM_BIOS_ADDR_MASK;
895*5c4a5fe1SAndy Fiddaman lobits = 0;
896*5c4a5fe1SAndy Fiddaman break;
897*5c4a5fe1SAndy Fiddaman default:
898*5c4a5fe1SAndy Fiddaman printf("pci_emul_alloc_base: invalid bar type %d\n", type);
899*5c4a5fe1SAndy Fiddaman #ifdef FreeBSD
900*5c4a5fe1SAndy Fiddaman assert(0);
901*5c4a5fe1SAndy Fiddaman #else
902*5c4a5fe1SAndy Fiddaman abort();
903*5c4a5fe1SAndy Fiddaman #endif
904*5c4a5fe1SAndy Fiddaman }
905*5c4a5fe1SAndy Fiddaman
906*5c4a5fe1SAndy Fiddaman if (baseptr != NULL) {
907*5c4a5fe1SAndy Fiddaman error = pci_emul_alloc_resource(baseptr, limit, size, &addr);
908*5c4a5fe1SAndy Fiddaman if (error != 0)
909*5c4a5fe1SAndy Fiddaman return (error);
910*5c4a5fe1SAndy Fiddaman } else {
911*5c4a5fe1SAndy Fiddaman addr = 0;
912*5c4a5fe1SAndy Fiddaman }
913*5c4a5fe1SAndy Fiddaman
914*5c4a5fe1SAndy Fiddaman pdi->pi_bar[idx].type = type;
915*5c4a5fe1SAndy Fiddaman pdi->pi_bar[idx].addr = addr;
916*5c4a5fe1SAndy Fiddaman pdi->pi_bar[idx].size = size;
917*5c4a5fe1SAndy Fiddaman /*
918*5c4a5fe1SAndy Fiddaman * passthru devices are using same lobits as physical device they set
919*5c4a5fe1SAndy Fiddaman * this property
920*5c4a5fe1SAndy Fiddaman */
921*5c4a5fe1SAndy Fiddaman if (pdi->pi_bar[idx].lobits != 0) {
922*5c4a5fe1SAndy Fiddaman lobits = pdi->pi_bar[idx].lobits;
923*5c4a5fe1SAndy Fiddaman } else {
924*5c4a5fe1SAndy Fiddaman pdi->pi_bar[idx].lobits = lobits;
925*5c4a5fe1SAndy Fiddaman }
926*5c4a5fe1SAndy Fiddaman
927*5c4a5fe1SAndy Fiddaman /* Initialize the BAR register in config space */
928*5c4a5fe1SAndy Fiddaman bar = (addr & mask) | lobits;
929*5c4a5fe1SAndy Fiddaman pci_set_cfgdata32(pdi, PCIR_BAR(idx), bar);
930*5c4a5fe1SAndy Fiddaman
931*5c4a5fe1SAndy Fiddaman if (type == PCIBAR_MEM64) {
932*5c4a5fe1SAndy Fiddaman assert(idx + 1 <= PCI_BARMAX);
933*5c4a5fe1SAndy Fiddaman pdi->pi_bar[idx + 1].type = PCIBAR_MEMHI64;
934*5c4a5fe1SAndy Fiddaman pci_set_cfgdata32(pdi, PCIR_BAR(idx + 1), bar >> 32);
935*5c4a5fe1SAndy Fiddaman }
936*5c4a5fe1SAndy Fiddaman
937*5c4a5fe1SAndy Fiddaman switch (type) {
938*5c4a5fe1SAndy Fiddaman case PCIBAR_IO:
939*5c4a5fe1SAndy Fiddaman if (porten(pdi))
940*5c4a5fe1SAndy Fiddaman register_bar(pdi, idx);
941*5c4a5fe1SAndy Fiddaman break;
942*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM32:
943*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM64:
944*5c4a5fe1SAndy Fiddaman case PCIBAR_MEMHI64:
945*5c4a5fe1SAndy Fiddaman if (memen(pdi))
946*5c4a5fe1SAndy Fiddaman register_bar(pdi, idx);
947*5c4a5fe1SAndy Fiddaman break;
948*5c4a5fe1SAndy Fiddaman default:
949*5c4a5fe1SAndy Fiddaman break;
950*5c4a5fe1SAndy Fiddaman }
951*5c4a5fe1SAndy Fiddaman
952*5c4a5fe1SAndy Fiddaman return (0);
953*5c4a5fe1SAndy Fiddaman }
954*5c4a5fe1SAndy Fiddaman
955*5c4a5fe1SAndy Fiddaman int
pci_emul_alloc_rom(struct pci_devinst * const pdi,const uint64_t size,void ** const addr)956*5c4a5fe1SAndy Fiddaman pci_emul_alloc_rom(struct pci_devinst *const pdi, const uint64_t size,
957*5c4a5fe1SAndy Fiddaman void **const addr)
958*5c4a5fe1SAndy Fiddaman {
959*5c4a5fe1SAndy Fiddaman /* allocate ROM space once on first call */
960*5c4a5fe1SAndy Fiddaman if (pci_emul_rombase == 0) {
961*5c4a5fe1SAndy Fiddaman pci_emul_rombase = vm_create_devmem(pdi->pi_vmctx, VM_PCIROM,
962*5c4a5fe1SAndy Fiddaman "pcirom", PCI_EMUL_ROMSIZE);
963*5c4a5fe1SAndy Fiddaman if (pci_emul_rombase == MAP_FAILED) {
964*5c4a5fe1SAndy Fiddaman warnx("%s: failed to create rom segment", __func__);
965*5c4a5fe1SAndy Fiddaman return (-1);
966*5c4a5fe1SAndy Fiddaman }
967*5c4a5fe1SAndy Fiddaman pci_emul_romlim = pci_emul_rombase + PCI_EMUL_ROMSIZE;
968*5c4a5fe1SAndy Fiddaman pci_emul_romoffset = 0;
969*5c4a5fe1SAndy Fiddaman }
970*5c4a5fe1SAndy Fiddaman
971*5c4a5fe1SAndy Fiddaman /* ROM size should be a power of 2 and greater than 2 KB */
972*5c4a5fe1SAndy Fiddaman const uint64_t rom_size = MAX(1UL << flsl(size),
973*5c4a5fe1SAndy Fiddaman ~PCIM_BIOS_ADDR_MASK + 1);
974*5c4a5fe1SAndy Fiddaman
975*5c4a5fe1SAndy Fiddaman /* check if ROM fits into ROM space */
976*5c4a5fe1SAndy Fiddaman if (pci_emul_romoffset + rom_size > PCI_EMUL_ROMSIZE) {
977*5c4a5fe1SAndy Fiddaman warnx("%s: no space left in rom segment:", __func__);
978*5c4a5fe1SAndy Fiddaman warnx("%16lu bytes left",
979*5c4a5fe1SAndy Fiddaman PCI_EMUL_ROMSIZE - pci_emul_romoffset);
980*5c4a5fe1SAndy Fiddaman warnx("%16lu bytes required by %d/%d/%d", rom_size, pdi->pi_bus,
981*5c4a5fe1SAndy Fiddaman pdi->pi_slot, pdi->pi_func);
982*5c4a5fe1SAndy Fiddaman return (-1);
983*5c4a5fe1SAndy Fiddaman }
984*5c4a5fe1SAndy Fiddaman
985*5c4a5fe1SAndy Fiddaman /* allocate ROM BAR */
986*5c4a5fe1SAndy Fiddaman const int error = pci_emul_alloc_bar(pdi, PCI_ROM_IDX, PCIBAR_ROM,
987*5c4a5fe1SAndy Fiddaman rom_size);
988*5c4a5fe1SAndy Fiddaman if (error)
989*5c4a5fe1SAndy Fiddaman return error;
990*5c4a5fe1SAndy Fiddaman
991*5c4a5fe1SAndy Fiddaman /* return address */
992*5c4a5fe1SAndy Fiddaman *addr = pci_emul_rombase + pci_emul_romoffset;
993*5c4a5fe1SAndy Fiddaman
994*5c4a5fe1SAndy Fiddaman /* save offset into ROM Space */
995*5c4a5fe1SAndy Fiddaman pdi->pi_romoffset = pci_emul_romoffset;
996*5c4a5fe1SAndy Fiddaman
997*5c4a5fe1SAndy Fiddaman /* increase offset for next ROM */
998*5c4a5fe1SAndy Fiddaman pci_emul_romoffset += rom_size;
999*5c4a5fe1SAndy Fiddaman
1000*5c4a5fe1SAndy Fiddaman return (0);
1001*5c4a5fe1SAndy Fiddaman }
1002*5c4a5fe1SAndy Fiddaman
1003*5c4a5fe1SAndy Fiddaman int
pci_emul_add_boot_device(struct pci_devinst * pi,int bootindex)1004*5c4a5fe1SAndy Fiddaman pci_emul_add_boot_device(struct pci_devinst *pi, int bootindex)
1005*5c4a5fe1SAndy Fiddaman {
1006*5c4a5fe1SAndy Fiddaman struct boot_device *new_device, *device;
1007*5c4a5fe1SAndy Fiddaman
1008*5c4a5fe1SAndy Fiddaman /* don't permit a negative bootindex */
1009*5c4a5fe1SAndy Fiddaman if (bootindex < 0) {
1010*5c4a5fe1SAndy Fiddaman errx(4, "Invalid bootindex %d for %s", bootindex, pi->pi_name);
1011*5c4a5fe1SAndy Fiddaman }
1012*5c4a5fe1SAndy Fiddaman
1013*5c4a5fe1SAndy Fiddaman /* alloc new boot device */
1014*5c4a5fe1SAndy Fiddaman new_device = calloc(1, sizeof(struct boot_device));
1015*5c4a5fe1SAndy Fiddaman if (new_device == NULL) {
1016*5c4a5fe1SAndy Fiddaman return (ENOMEM);
1017*5c4a5fe1SAndy Fiddaman }
1018*5c4a5fe1SAndy Fiddaman new_device->pdi = pi;
1019*5c4a5fe1SAndy Fiddaman new_device->bootindex = bootindex;
1020*5c4a5fe1SAndy Fiddaman
1021*5c4a5fe1SAndy Fiddaman /* search for boot device with higher boot index */
1022*5c4a5fe1SAndy Fiddaman TAILQ_FOREACH(device, &boot_devices, boot_device_chain) {
1023*5c4a5fe1SAndy Fiddaman if (device->bootindex == bootindex) {
1024*5c4a5fe1SAndy Fiddaman errx(4,
1025*5c4a5fe1SAndy Fiddaman "Could not set bootindex %d for %s. Bootindex already occupied by %s",
1026*5c4a5fe1SAndy Fiddaman bootindex, pi->pi_name, device->pdi->pi_name);
1027*5c4a5fe1SAndy Fiddaman } else if (device->bootindex > bootindex) {
1028*5c4a5fe1SAndy Fiddaman break;
1029*5c4a5fe1SAndy Fiddaman }
1030*5c4a5fe1SAndy Fiddaman }
1031*5c4a5fe1SAndy Fiddaman
1032*5c4a5fe1SAndy Fiddaman /* add boot device to queue */
1033*5c4a5fe1SAndy Fiddaman if (device == NULL) {
1034*5c4a5fe1SAndy Fiddaman TAILQ_INSERT_TAIL(&boot_devices, new_device, boot_device_chain);
1035*5c4a5fe1SAndy Fiddaman } else {
1036*5c4a5fe1SAndy Fiddaman TAILQ_INSERT_BEFORE(device, new_device, boot_device_chain);
1037*5c4a5fe1SAndy Fiddaman }
1038*5c4a5fe1SAndy Fiddaman
1039*5c4a5fe1SAndy Fiddaman return (0);
1040*5c4a5fe1SAndy Fiddaman }
1041*5c4a5fe1SAndy Fiddaman
1042*5c4a5fe1SAndy Fiddaman #define CAP_START_OFFSET 0x40
1043*5c4a5fe1SAndy Fiddaman static int
pci_emul_add_capability(struct pci_devinst * pi,u_char * capdata,int caplen)1044*5c4a5fe1SAndy Fiddaman pci_emul_add_capability(struct pci_devinst *pi, u_char *capdata, int caplen)
1045*5c4a5fe1SAndy Fiddaman {
1046*5c4a5fe1SAndy Fiddaman int i, capoff, reallen;
1047*5c4a5fe1SAndy Fiddaman uint16_t sts;
1048*5c4a5fe1SAndy Fiddaman
1049*5c4a5fe1SAndy Fiddaman assert(caplen > 0);
1050*5c4a5fe1SAndy Fiddaman
1051*5c4a5fe1SAndy Fiddaman reallen = roundup2(caplen, 4); /* dword aligned */
1052*5c4a5fe1SAndy Fiddaman
1053*5c4a5fe1SAndy Fiddaman sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1054*5c4a5fe1SAndy Fiddaman if ((sts & PCIM_STATUS_CAPPRESENT) == 0)
1055*5c4a5fe1SAndy Fiddaman capoff = CAP_START_OFFSET;
1056*5c4a5fe1SAndy Fiddaman else
1057*5c4a5fe1SAndy Fiddaman capoff = pi->pi_capend + 1;
1058*5c4a5fe1SAndy Fiddaman
1059*5c4a5fe1SAndy Fiddaman /* Check if we have enough space */
1060*5c4a5fe1SAndy Fiddaman if (capoff + reallen > PCI_REGMAX + 1)
1061*5c4a5fe1SAndy Fiddaman return (-1);
1062*5c4a5fe1SAndy Fiddaman
1063*5c4a5fe1SAndy Fiddaman /* Set the previous capability pointer */
1064*5c4a5fe1SAndy Fiddaman if ((sts & PCIM_STATUS_CAPPRESENT) == 0) {
1065*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, PCIR_CAP_PTR, capoff);
1066*5c4a5fe1SAndy Fiddaman pci_set_cfgdata16(pi, PCIR_STATUS, sts|PCIM_STATUS_CAPPRESENT);
1067*5c4a5fe1SAndy Fiddaman } else
1068*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, pi->pi_prevcap + 1, capoff);
1069*5c4a5fe1SAndy Fiddaman
1070*5c4a5fe1SAndy Fiddaman /* Copy the capability */
1071*5c4a5fe1SAndy Fiddaman for (i = 0; i < caplen; i++)
1072*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, capoff + i, capdata[i]);
1073*5c4a5fe1SAndy Fiddaman
1074*5c4a5fe1SAndy Fiddaman /* Set the next capability pointer */
1075*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, capoff + 1, 0);
1076*5c4a5fe1SAndy Fiddaman
1077*5c4a5fe1SAndy Fiddaman pi->pi_prevcap = capoff;
1078*5c4a5fe1SAndy Fiddaman pi->pi_capend = capoff + reallen - 1;
1079*5c4a5fe1SAndy Fiddaman return (0);
1080*5c4a5fe1SAndy Fiddaman }
1081*5c4a5fe1SAndy Fiddaman
1082*5c4a5fe1SAndy Fiddaman static struct pci_devemu *
pci_emul_finddev(const char * name)1083*5c4a5fe1SAndy Fiddaman pci_emul_finddev(const char *name)
1084*5c4a5fe1SAndy Fiddaman {
1085*5c4a5fe1SAndy Fiddaman struct pci_devemu **pdpp, *pdp;
1086*5c4a5fe1SAndy Fiddaman
1087*5c4a5fe1SAndy Fiddaman SET_FOREACH(pdpp, pci_devemu_set) {
1088*5c4a5fe1SAndy Fiddaman pdp = *pdpp;
1089*5c4a5fe1SAndy Fiddaman if (!strcmp(pdp->pe_emu, name)) {
1090*5c4a5fe1SAndy Fiddaman return (pdp);
1091*5c4a5fe1SAndy Fiddaman }
1092*5c4a5fe1SAndy Fiddaman }
1093*5c4a5fe1SAndy Fiddaman
1094*5c4a5fe1SAndy Fiddaman return (NULL);
1095*5c4a5fe1SAndy Fiddaman }
1096*5c4a5fe1SAndy Fiddaman
1097*5c4a5fe1SAndy Fiddaman static int
pci_emul_init(struct vmctx * ctx,struct pci_devemu * pde,int bus,int slot,int func,struct funcinfo * fi)1098*5c4a5fe1SAndy Fiddaman pci_emul_init(struct vmctx *ctx, struct pci_devemu *pde, int bus, int slot,
1099*5c4a5fe1SAndy Fiddaman int func, struct funcinfo *fi)
1100*5c4a5fe1SAndy Fiddaman {
1101*5c4a5fe1SAndy Fiddaman struct pci_devinst *pdi;
1102*5c4a5fe1SAndy Fiddaman int err;
1103*5c4a5fe1SAndy Fiddaman
1104*5c4a5fe1SAndy Fiddaman pdi = calloc(1, sizeof(struct pci_devinst));
1105*5c4a5fe1SAndy Fiddaman
1106*5c4a5fe1SAndy Fiddaman pdi->pi_vmctx = ctx;
1107*5c4a5fe1SAndy Fiddaman pdi->pi_bus = bus;
1108*5c4a5fe1SAndy Fiddaman pdi->pi_slot = slot;
1109*5c4a5fe1SAndy Fiddaman pdi->pi_func = func;
1110*5c4a5fe1SAndy Fiddaman pthread_mutex_init(&pdi->pi_lintr.lock, NULL);
1111*5c4a5fe1SAndy Fiddaman pdi->pi_lintr.pin = 0;
1112*5c4a5fe1SAndy Fiddaman pdi->pi_lintr.state = IDLE;
1113*5c4a5fe1SAndy Fiddaman pdi->pi_lintr.pirq_pin = 0;
1114*5c4a5fe1SAndy Fiddaman pdi->pi_lintr.ioapic_irq = 0;
1115*5c4a5fe1SAndy Fiddaman pdi->pi_d = pde;
1116*5c4a5fe1SAndy Fiddaman snprintf(pdi->pi_name, PI_NAMESZ, "%s@pci.%d.%d.%d", pde->pe_emu, bus,
1117*5c4a5fe1SAndy Fiddaman slot, func);
1118*5c4a5fe1SAndy Fiddaman
1119*5c4a5fe1SAndy Fiddaman /* Disable legacy interrupts */
1120*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
1121*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);
1122*5c4a5fe1SAndy Fiddaman
1123*5c4a5fe1SAndy Fiddaman #ifdef __FreeBSD__
1124*5c4a5fe1SAndy Fiddaman if (get_config_bool_default("pci.enable_bars", !bootrom_boot()))
1125*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_BUSMASTEREN);
1126*5c4a5fe1SAndy Fiddaman #else
1127*5c4a5fe1SAndy Fiddaman if (get_config_bool_default("pci.enable_bars", true))
1128*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pdi, PCIR_COMMAND, PCIM_CMD_BUSMASTEREN);
1129*5c4a5fe1SAndy Fiddaman #endif
1130*5c4a5fe1SAndy Fiddaman
1131*5c4a5fe1SAndy Fiddaman err = (*pde->pe_init)(pdi, fi->fi_config);
1132*5c4a5fe1SAndy Fiddaman if (err == 0)
1133*5c4a5fe1SAndy Fiddaman fi->fi_devi = pdi;
1134*5c4a5fe1SAndy Fiddaman else
1135*5c4a5fe1SAndy Fiddaman free(pdi);
1136*5c4a5fe1SAndy Fiddaman
1137*5c4a5fe1SAndy Fiddaman return (err);
1138*5c4a5fe1SAndy Fiddaman }
1139*5c4a5fe1SAndy Fiddaman
1140*5c4a5fe1SAndy Fiddaman void
pci_populate_msicap(struct msicap * msicap,int msgnum,int nextptr)1141*5c4a5fe1SAndy Fiddaman pci_populate_msicap(struct msicap *msicap, int msgnum, int nextptr)
1142*5c4a5fe1SAndy Fiddaman {
1143*5c4a5fe1SAndy Fiddaman int mmc;
1144*5c4a5fe1SAndy Fiddaman
1145*5c4a5fe1SAndy Fiddaman /* Number of msi messages must be a power of 2 between 1 and 32 */
1146*5c4a5fe1SAndy Fiddaman assert((msgnum & (msgnum - 1)) == 0 && msgnum >= 1 && msgnum <= 32);
1147*5c4a5fe1SAndy Fiddaman mmc = ffs(msgnum) - 1;
1148*5c4a5fe1SAndy Fiddaman
1149*5c4a5fe1SAndy Fiddaman bzero(msicap, sizeof(struct msicap));
1150*5c4a5fe1SAndy Fiddaman msicap->capid = PCIY_MSI;
1151*5c4a5fe1SAndy Fiddaman msicap->nextptr = nextptr;
1152*5c4a5fe1SAndy Fiddaman msicap->msgctrl = PCIM_MSICTRL_64BIT | (mmc << 1);
1153*5c4a5fe1SAndy Fiddaman }
1154*5c4a5fe1SAndy Fiddaman
1155*5c4a5fe1SAndy Fiddaman int
pci_emul_add_msicap(struct pci_devinst * pi,int msgnum)1156*5c4a5fe1SAndy Fiddaman pci_emul_add_msicap(struct pci_devinst *pi, int msgnum)
1157*5c4a5fe1SAndy Fiddaman {
1158*5c4a5fe1SAndy Fiddaman struct msicap msicap;
1159*5c4a5fe1SAndy Fiddaman
1160*5c4a5fe1SAndy Fiddaman pci_populate_msicap(&msicap, msgnum, 0);
1161*5c4a5fe1SAndy Fiddaman
1162*5c4a5fe1SAndy Fiddaman return (pci_emul_add_capability(pi, (u_char *)&msicap, sizeof(msicap)));
1163*5c4a5fe1SAndy Fiddaman }
1164*5c4a5fe1SAndy Fiddaman
1165*5c4a5fe1SAndy Fiddaman static void
pci_populate_msixcap(struct msixcap * msixcap,int msgnum,int barnum,uint32_t msix_tab_size)1166*5c4a5fe1SAndy Fiddaman pci_populate_msixcap(struct msixcap *msixcap, int msgnum, int barnum,
1167*5c4a5fe1SAndy Fiddaman uint32_t msix_tab_size)
1168*5c4a5fe1SAndy Fiddaman {
1169*5c4a5fe1SAndy Fiddaman
1170*5c4a5fe1SAndy Fiddaman assert(msix_tab_size % 4096 == 0);
1171*5c4a5fe1SAndy Fiddaman
1172*5c4a5fe1SAndy Fiddaman bzero(msixcap, sizeof(struct msixcap));
1173*5c4a5fe1SAndy Fiddaman msixcap->capid = PCIY_MSIX;
1174*5c4a5fe1SAndy Fiddaman
1175*5c4a5fe1SAndy Fiddaman /*
1176*5c4a5fe1SAndy Fiddaman * Message Control Register, all fields set to
1177*5c4a5fe1SAndy Fiddaman * zero except for the Table Size.
1178*5c4a5fe1SAndy Fiddaman * Note: Table size N is encoded as N-1
1179*5c4a5fe1SAndy Fiddaman */
1180*5c4a5fe1SAndy Fiddaman msixcap->msgctrl = msgnum - 1;
1181*5c4a5fe1SAndy Fiddaman
1182*5c4a5fe1SAndy Fiddaman /*
1183*5c4a5fe1SAndy Fiddaman * MSI-X BAR setup:
1184*5c4a5fe1SAndy Fiddaman * - MSI-X table start at offset 0
1185*5c4a5fe1SAndy Fiddaman * - PBA table starts at a 4K aligned offset after the MSI-X table
1186*5c4a5fe1SAndy Fiddaman */
1187*5c4a5fe1SAndy Fiddaman msixcap->table_info = barnum & PCIM_MSIX_BIR_MASK;
1188*5c4a5fe1SAndy Fiddaman msixcap->pba_info = msix_tab_size | (barnum & PCIM_MSIX_BIR_MASK);
1189*5c4a5fe1SAndy Fiddaman }
1190*5c4a5fe1SAndy Fiddaman
1191*5c4a5fe1SAndy Fiddaman static void
pci_msix_table_init(struct pci_devinst * pi,int table_entries)1192*5c4a5fe1SAndy Fiddaman pci_msix_table_init(struct pci_devinst *pi, int table_entries)
1193*5c4a5fe1SAndy Fiddaman {
1194*5c4a5fe1SAndy Fiddaman int i, table_size;
1195*5c4a5fe1SAndy Fiddaman
1196*5c4a5fe1SAndy Fiddaman assert(table_entries > 0);
1197*5c4a5fe1SAndy Fiddaman assert(table_entries <= MAX_MSIX_TABLE_ENTRIES);
1198*5c4a5fe1SAndy Fiddaman
1199*5c4a5fe1SAndy Fiddaman table_size = table_entries * MSIX_TABLE_ENTRY_SIZE;
1200*5c4a5fe1SAndy Fiddaman pi->pi_msix.table = calloc(1, table_size);
1201*5c4a5fe1SAndy Fiddaman
1202*5c4a5fe1SAndy Fiddaman /* set mask bit of vector control register */
1203*5c4a5fe1SAndy Fiddaman for (i = 0; i < table_entries; i++)
1204*5c4a5fe1SAndy Fiddaman pi->pi_msix.table[i].vector_control |= PCIM_MSIX_VCTRL_MASK;
1205*5c4a5fe1SAndy Fiddaman }
1206*5c4a5fe1SAndy Fiddaman
1207*5c4a5fe1SAndy Fiddaman int
pci_emul_add_msixcap(struct pci_devinst * pi,int msgnum,int barnum)1208*5c4a5fe1SAndy Fiddaman pci_emul_add_msixcap(struct pci_devinst *pi, int msgnum, int barnum)
1209*5c4a5fe1SAndy Fiddaman {
1210*5c4a5fe1SAndy Fiddaman uint32_t tab_size;
1211*5c4a5fe1SAndy Fiddaman struct msixcap msixcap;
1212*5c4a5fe1SAndy Fiddaman
1213*5c4a5fe1SAndy Fiddaman assert(msgnum >= 1 && msgnum <= MAX_MSIX_TABLE_ENTRIES);
1214*5c4a5fe1SAndy Fiddaman assert(barnum >= 0 && barnum <= PCIR_MAX_BAR_0);
1215*5c4a5fe1SAndy Fiddaman
1216*5c4a5fe1SAndy Fiddaman tab_size = msgnum * MSIX_TABLE_ENTRY_SIZE;
1217*5c4a5fe1SAndy Fiddaman
1218*5c4a5fe1SAndy Fiddaman /* Align table size to nearest 4K */
1219*5c4a5fe1SAndy Fiddaman tab_size = roundup2(tab_size, 4096);
1220*5c4a5fe1SAndy Fiddaman
1221*5c4a5fe1SAndy Fiddaman pi->pi_msix.table_bar = barnum;
1222*5c4a5fe1SAndy Fiddaman pi->pi_msix.pba_bar = barnum;
1223*5c4a5fe1SAndy Fiddaman pi->pi_msix.table_offset = 0;
1224*5c4a5fe1SAndy Fiddaman pi->pi_msix.table_count = msgnum;
1225*5c4a5fe1SAndy Fiddaman pi->pi_msix.pba_offset = tab_size;
1226*5c4a5fe1SAndy Fiddaman pi->pi_msix.pba_size = PBA_SIZE(msgnum);
1227*5c4a5fe1SAndy Fiddaman
1228*5c4a5fe1SAndy Fiddaman pci_msix_table_init(pi, msgnum);
1229*5c4a5fe1SAndy Fiddaman
1230*5c4a5fe1SAndy Fiddaman pci_populate_msixcap(&msixcap, msgnum, barnum, tab_size);
1231*5c4a5fe1SAndy Fiddaman
1232*5c4a5fe1SAndy Fiddaman /* allocate memory for MSI-X Table and PBA */
1233*5c4a5fe1SAndy Fiddaman pci_emul_alloc_bar(pi, barnum, PCIBAR_MEM32,
1234*5c4a5fe1SAndy Fiddaman tab_size + pi->pi_msix.pba_size);
1235*5c4a5fe1SAndy Fiddaman
1236*5c4a5fe1SAndy Fiddaman return (pci_emul_add_capability(pi, (u_char *)&msixcap,
1237*5c4a5fe1SAndy Fiddaman sizeof(msixcap)));
1238*5c4a5fe1SAndy Fiddaman }
1239*5c4a5fe1SAndy Fiddaman
1240*5c4a5fe1SAndy Fiddaman static void
msixcap_cfgwrite(struct pci_devinst * pi,int capoff,int offset,int bytes,uint32_t val)1241*5c4a5fe1SAndy Fiddaman msixcap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
1242*5c4a5fe1SAndy Fiddaman int bytes, uint32_t val)
1243*5c4a5fe1SAndy Fiddaman {
1244*5c4a5fe1SAndy Fiddaman uint16_t msgctrl, rwmask;
1245*5c4a5fe1SAndy Fiddaman int off;
1246*5c4a5fe1SAndy Fiddaman
1247*5c4a5fe1SAndy Fiddaman off = offset - capoff;
1248*5c4a5fe1SAndy Fiddaman /* Message Control Register */
1249*5c4a5fe1SAndy Fiddaman if (off == 2 && bytes == 2) {
1250*5c4a5fe1SAndy Fiddaman rwmask = PCIM_MSIXCTRL_MSIX_ENABLE | PCIM_MSIXCTRL_FUNCTION_MASK;
1251*5c4a5fe1SAndy Fiddaman msgctrl = pci_get_cfgdata16(pi, offset);
1252*5c4a5fe1SAndy Fiddaman msgctrl &= ~rwmask;
1253*5c4a5fe1SAndy Fiddaman msgctrl |= val & rwmask;
1254*5c4a5fe1SAndy Fiddaman val = msgctrl;
1255*5c4a5fe1SAndy Fiddaman
1256*5c4a5fe1SAndy Fiddaman pi->pi_msix.enabled = val & PCIM_MSIXCTRL_MSIX_ENABLE;
1257*5c4a5fe1SAndy Fiddaman pi->pi_msix.function_mask = val & PCIM_MSIXCTRL_FUNCTION_MASK;
1258*5c4a5fe1SAndy Fiddaman pci_lintr_update(pi);
1259*5c4a5fe1SAndy Fiddaman }
1260*5c4a5fe1SAndy Fiddaman
1261*5c4a5fe1SAndy Fiddaman CFGWRITE(pi, offset, val, bytes);
1262*5c4a5fe1SAndy Fiddaman }
1263*5c4a5fe1SAndy Fiddaman
1264*5c4a5fe1SAndy Fiddaman static void
msicap_cfgwrite(struct pci_devinst * pi,int capoff,int offset,int bytes,uint32_t val)1265*5c4a5fe1SAndy Fiddaman msicap_cfgwrite(struct pci_devinst *pi, int capoff, int offset,
1266*5c4a5fe1SAndy Fiddaman int bytes, uint32_t val)
1267*5c4a5fe1SAndy Fiddaman {
1268*5c4a5fe1SAndy Fiddaman uint16_t msgctrl, rwmask, msgdata, mme;
1269*5c4a5fe1SAndy Fiddaman uint32_t addrlo;
1270*5c4a5fe1SAndy Fiddaman
1271*5c4a5fe1SAndy Fiddaman /*
1272*5c4a5fe1SAndy Fiddaman * If guest is writing to the message control register make sure
1273*5c4a5fe1SAndy Fiddaman * we do not overwrite read-only fields.
1274*5c4a5fe1SAndy Fiddaman */
1275*5c4a5fe1SAndy Fiddaman if ((offset - capoff) == 2 && bytes == 2) {
1276*5c4a5fe1SAndy Fiddaman rwmask = PCIM_MSICTRL_MME_MASK | PCIM_MSICTRL_MSI_ENABLE;
1277*5c4a5fe1SAndy Fiddaman msgctrl = pci_get_cfgdata16(pi, offset);
1278*5c4a5fe1SAndy Fiddaman msgctrl &= ~rwmask;
1279*5c4a5fe1SAndy Fiddaman msgctrl |= val & rwmask;
1280*5c4a5fe1SAndy Fiddaman val = msgctrl;
1281*5c4a5fe1SAndy Fiddaman }
1282*5c4a5fe1SAndy Fiddaman CFGWRITE(pi, offset, val, bytes);
1283*5c4a5fe1SAndy Fiddaman
1284*5c4a5fe1SAndy Fiddaman msgctrl = pci_get_cfgdata16(pi, capoff + 2);
1285*5c4a5fe1SAndy Fiddaman addrlo = pci_get_cfgdata32(pi, capoff + 4);
1286*5c4a5fe1SAndy Fiddaman if (msgctrl & PCIM_MSICTRL_64BIT)
1287*5c4a5fe1SAndy Fiddaman msgdata = pci_get_cfgdata16(pi, capoff + 12);
1288*5c4a5fe1SAndy Fiddaman else
1289*5c4a5fe1SAndy Fiddaman msgdata = pci_get_cfgdata16(pi, capoff + 8);
1290*5c4a5fe1SAndy Fiddaman
1291*5c4a5fe1SAndy Fiddaman mme = msgctrl & PCIM_MSICTRL_MME_MASK;
1292*5c4a5fe1SAndy Fiddaman pi->pi_msi.enabled = msgctrl & PCIM_MSICTRL_MSI_ENABLE ? 1 : 0;
1293*5c4a5fe1SAndy Fiddaman if (pi->pi_msi.enabled) {
1294*5c4a5fe1SAndy Fiddaman pi->pi_msi.addr = addrlo;
1295*5c4a5fe1SAndy Fiddaman pi->pi_msi.msg_data = msgdata;
1296*5c4a5fe1SAndy Fiddaman pi->pi_msi.maxmsgnum = 1 << (mme >> 4);
1297*5c4a5fe1SAndy Fiddaman } else {
1298*5c4a5fe1SAndy Fiddaman pi->pi_msi.maxmsgnum = 0;
1299*5c4a5fe1SAndy Fiddaman }
1300*5c4a5fe1SAndy Fiddaman pci_lintr_update(pi);
1301*5c4a5fe1SAndy Fiddaman }
1302*5c4a5fe1SAndy Fiddaman
1303*5c4a5fe1SAndy Fiddaman static void
pciecap_cfgwrite(struct pci_devinst * pi,int capoff __unused,int offset,int bytes,uint32_t val)1304*5c4a5fe1SAndy Fiddaman pciecap_cfgwrite(struct pci_devinst *pi, int capoff __unused, int offset,
1305*5c4a5fe1SAndy Fiddaman int bytes, uint32_t val)
1306*5c4a5fe1SAndy Fiddaman {
1307*5c4a5fe1SAndy Fiddaman
1308*5c4a5fe1SAndy Fiddaman /* XXX don't write to the readonly parts */
1309*5c4a5fe1SAndy Fiddaman CFGWRITE(pi, offset, val, bytes);
1310*5c4a5fe1SAndy Fiddaman }
1311*5c4a5fe1SAndy Fiddaman
1312*5c4a5fe1SAndy Fiddaman #define PCIECAP_VERSION 0x2
1313*5c4a5fe1SAndy Fiddaman int
pci_emul_add_pciecap(struct pci_devinst * pi,int type)1314*5c4a5fe1SAndy Fiddaman pci_emul_add_pciecap(struct pci_devinst *pi, int type)
1315*5c4a5fe1SAndy Fiddaman {
1316*5c4a5fe1SAndy Fiddaman int err;
1317*5c4a5fe1SAndy Fiddaman struct pciecap pciecap;
1318*5c4a5fe1SAndy Fiddaman
1319*5c4a5fe1SAndy Fiddaman bzero(&pciecap, sizeof(pciecap));
1320*5c4a5fe1SAndy Fiddaman
1321*5c4a5fe1SAndy Fiddaman /*
1322*5c4a5fe1SAndy Fiddaman * Use the integrated endpoint type for endpoints on a root complex bus.
1323*5c4a5fe1SAndy Fiddaman *
1324*5c4a5fe1SAndy Fiddaman * NB: bhyve currently only supports a single PCI bus that is the root
1325*5c4a5fe1SAndy Fiddaman * complex bus, so all endpoints are integrated.
1326*5c4a5fe1SAndy Fiddaman */
1327*5c4a5fe1SAndy Fiddaman if ((type == PCIEM_TYPE_ENDPOINT) && (pi->pi_bus == 0))
1328*5c4a5fe1SAndy Fiddaman type = PCIEM_TYPE_ROOT_INT_EP;
1329*5c4a5fe1SAndy Fiddaman
1330*5c4a5fe1SAndy Fiddaman pciecap.capid = PCIY_EXPRESS;
1331*5c4a5fe1SAndy Fiddaman pciecap.pcie_capabilities = PCIECAP_VERSION | type;
1332*5c4a5fe1SAndy Fiddaman if (type != PCIEM_TYPE_ROOT_INT_EP) {
1333*5c4a5fe1SAndy Fiddaman pciecap.link_capabilities = 0x411; /* gen1, x1 */
1334*5c4a5fe1SAndy Fiddaman pciecap.link_status = 0x11; /* gen1, x1 */
1335*5c4a5fe1SAndy Fiddaman }
1336*5c4a5fe1SAndy Fiddaman
1337*5c4a5fe1SAndy Fiddaman err = pci_emul_add_capability(pi, (u_char *)&pciecap, sizeof(pciecap));
1338*5c4a5fe1SAndy Fiddaman return (err);
1339*5c4a5fe1SAndy Fiddaman }
1340*5c4a5fe1SAndy Fiddaman
1341*5c4a5fe1SAndy Fiddaman /*
1342*5c4a5fe1SAndy Fiddaman * This function assumes that 'coff' is in the capabilities region of the
1343*5c4a5fe1SAndy Fiddaman * config space. A capoff parameter of zero will force a search for the
1344*5c4a5fe1SAndy Fiddaman * offset and type.
1345*5c4a5fe1SAndy Fiddaman */
1346*5c4a5fe1SAndy Fiddaman void
pci_emul_capwrite(struct pci_devinst * pi,int offset,int bytes,uint32_t val,uint8_t capoff,int capid)1347*5c4a5fe1SAndy Fiddaman pci_emul_capwrite(struct pci_devinst *pi, int offset, int bytes, uint32_t val,
1348*5c4a5fe1SAndy Fiddaman uint8_t capoff, int capid)
1349*5c4a5fe1SAndy Fiddaman {
1350*5c4a5fe1SAndy Fiddaman uint8_t nextoff;
1351*5c4a5fe1SAndy Fiddaman
1352*5c4a5fe1SAndy Fiddaman /* Do not allow un-aligned writes */
1353*5c4a5fe1SAndy Fiddaman if ((offset & (bytes - 1)) != 0)
1354*5c4a5fe1SAndy Fiddaman return;
1355*5c4a5fe1SAndy Fiddaman
1356*5c4a5fe1SAndy Fiddaman if (capoff == 0) {
1357*5c4a5fe1SAndy Fiddaman /* Find the capability that we want to update */
1358*5c4a5fe1SAndy Fiddaman capoff = CAP_START_OFFSET;
1359*5c4a5fe1SAndy Fiddaman while (1) {
1360*5c4a5fe1SAndy Fiddaman nextoff = pci_get_cfgdata8(pi, capoff + 1);
1361*5c4a5fe1SAndy Fiddaman if (nextoff == 0)
1362*5c4a5fe1SAndy Fiddaman break;
1363*5c4a5fe1SAndy Fiddaman if (offset >= capoff && offset < nextoff)
1364*5c4a5fe1SAndy Fiddaman break;
1365*5c4a5fe1SAndy Fiddaman
1366*5c4a5fe1SAndy Fiddaman capoff = nextoff;
1367*5c4a5fe1SAndy Fiddaman }
1368*5c4a5fe1SAndy Fiddaman assert(offset >= capoff);
1369*5c4a5fe1SAndy Fiddaman capid = pci_get_cfgdata8(pi, capoff);
1370*5c4a5fe1SAndy Fiddaman }
1371*5c4a5fe1SAndy Fiddaman
1372*5c4a5fe1SAndy Fiddaman /*
1373*5c4a5fe1SAndy Fiddaman * Capability ID and Next Capability Pointer are readonly.
1374*5c4a5fe1SAndy Fiddaman * However, some o/s's do 4-byte writes that include these.
1375*5c4a5fe1SAndy Fiddaman * For this case, trim the write back to 2 bytes and adjust
1376*5c4a5fe1SAndy Fiddaman * the data.
1377*5c4a5fe1SAndy Fiddaman */
1378*5c4a5fe1SAndy Fiddaman if (offset == capoff || offset == capoff + 1) {
1379*5c4a5fe1SAndy Fiddaman if (offset == capoff && bytes == 4) {
1380*5c4a5fe1SAndy Fiddaman bytes = 2;
1381*5c4a5fe1SAndy Fiddaman offset += 2;
1382*5c4a5fe1SAndy Fiddaman val >>= 16;
1383*5c4a5fe1SAndy Fiddaman } else
1384*5c4a5fe1SAndy Fiddaman return;
1385*5c4a5fe1SAndy Fiddaman }
1386*5c4a5fe1SAndy Fiddaman
1387*5c4a5fe1SAndy Fiddaman switch (capid) {
1388*5c4a5fe1SAndy Fiddaman case PCIY_MSI:
1389*5c4a5fe1SAndy Fiddaman msicap_cfgwrite(pi, capoff, offset, bytes, val);
1390*5c4a5fe1SAndy Fiddaman break;
1391*5c4a5fe1SAndy Fiddaman case PCIY_MSIX:
1392*5c4a5fe1SAndy Fiddaman msixcap_cfgwrite(pi, capoff, offset, bytes, val);
1393*5c4a5fe1SAndy Fiddaman break;
1394*5c4a5fe1SAndy Fiddaman case PCIY_EXPRESS:
1395*5c4a5fe1SAndy Fiddaman pciecap_cfgwrite(pi, capoff, offset, bytes, val);
1396*5c4a5fe1SAndy Fiddaman break;
1397*5c4a5fe1SAndy Fiddaman default:
1398*5c4a5fe1SAndy Fiddaman break;
1399*5c4a5fe1SAndy Fiddaman }
1400*5c4a5fe1SAndy Fiddaman }
1401*5c4a5fe1SAndy Fiddaman
1402*5c4a5fe1SAndy Fiddaman static int
pci_emul_iscap(struct pci_devinst * pi,int offset)1403*5c4a5fe1SAndy Fiddaman pci_emul_iscap(struct pci_devinst *pi, int offset)
1404*5c4a5fe1SAndy Fiddaman {
1405*5c4a5fe1SAndy Fiddaman uint16_t sts;
1406*5c4a5fe1SAndy Fiddaman
1407*5c4a5fe1SAndy Fiddaman sts = pci_get_cfgdata16(pi, PCIR_STATUS);
1408*5c4a5fe1SAndy Fiddaman if ((sts & PCIM_STATUS_CAPPRESENT) != 0) {
1409*5c4a5fe1SAndy Fiddaman if (offset >= CAP_START_OFFSET && offset <= pi->pi_capend)
1410*5c4a5fe1SAndy Fiddaman return (1);
1411*5c4a5fe1SAndy Fiddaman }
1412*5c4a5fe1SAndy Fiddaman return (0);
1413*5c4a5fe1SAndy Fiddaman }
1414*5c4a5fe1SAndy Fiddaman
1415*5c4a5fe1SAndy Fiddaman static int
pci_emul_fallback_handler(struct vcpu * vcpu __unused,int dir,uint64_t addr __unused,int size __unused,uint64_t * val,void * arg1 __unused,long arg2 __unused)1416*5c4a5fe1SAndy Fiddaman pci_emul_fallback_handler(struct vcpu *vcpu __unused, int dir,
1417*5c4a5fe1SAndy Fiddaman uint64_t addr __unused, int size __unused, uint64_t *val,
1418*5c4a5fe1SAndy Fiddaman void *arg1 __unused, long arg2 __unused)
1419*5c4a5fe1SAndy Fiddaman {
1420*5c4a5fe1SAndy Fiddaman /*
1421*5c4a5fe1SAndy Fiddaman * Ignore writes; return 0xff's for reads. The mem read code
1422*5c4a5fe1SAndy Fiddaman * will take care of truncating to the correct size.
1423*5c4a5fe1SAndy Fiddaman */
1424*5c4a5fe1SAndy Fiddaman if (dir == MEM_F_READ) {
1425*5c4a5fe1SAndy Fiddaman *val = 0xffffffffffffffff;
1426*5c4a5fe1SAndy Fiddaman }
1427*5c4a5fe1SAndy Fiddaman
1428*5c4a5fe1SAndy Fiddaman return (0);
1429*5c4a5fe1SAndy Fiddaman }
1430*5c4a5fe1SAndy Fiddaman
1431*5c4a5fe1SAndy Fiddaman static int
pci_emul_ecfg_handler(struct vcpu * vcpu __unused,int dir,uint64_t addr,int bytes,uint64_t * val,void * arg1 __unused,long arg2 __unused)1432*5c4a5fe1SAndy Fiddaman pci_emul_ecfg_handler(struct vcpu *vcpu __unused, int dir, uint64_t addr,
1433*5c4a5fe1SAndy Fiddaman int bytes, uint64_t *val, void *arg1 __unused, long arg2 __unused)
1434*5c4a5fe1SAndy Fiddaman {
1435*5c4a5fe1SAndy Fiddaman int bus, slot, func, coff, in;
1436*5c4a5fe1SAndy Fiddaman
1437*5c4a5fe1SAndy Fiddaman coff = addr & 0xfff;
1438*5c4a5fe1SAndy Fiddaman func = (addr >> 12) & 0x7;
1439*5c4a5fe1SAndy Fiddaman slot = (addr >> 15) & 0x1f;
1440*5c4a5fe1SAndy Fiddaman bus = (addr >> 20) & 0xff;
1441*5c4a5fe1SAndy Fiddaman in = (dir == MEM_F_READ);
1442*5c4a5fe1SAndy Fiddaman if (in)
1443*5c4a5fe1SAndy Fiddaman *val = ~0UL;
1444*5c4a5fe1SAndy Fiddaman pci_cfgrw(in, bus, slot, func, coff, bytes, (uint32_t *)val);
1445*5c4a5fe1SAndy Fiddaman return (0);
1446*5c4a5fe1SAndy Fiddaman }
1447*5c4a5fe1SAndy Fiddaman
1448*5c4a5fe1SAndy Fiddaman uint64_t
pci_ecfg_base(void)1449*5c4a5fe1SAndy Fiddaman pci_ecfg_base(void)
1450*5c4a5fe1SAndy Fiddaman {
1451*5c4a5fe1SAndy Fiddaman
1452*5c4a5fe1SAndy Fiddaman return (PCI_EMUL_ECFG_BASE);
1453*5c4a5fe1SAndy Fiddaman }
1454*5c4a5fe1SAndy Fiddaman
1455*5c4a5fe1SAndy Fiddaman static int
init_bootorder(void)1456*5c4a5fe1SAndy Fiddaman init_bootorder(void)
1457*5c4a5fe1SAndy Fiddaman {
1458*5c4a5fe1SAndy Fiddaman struct boot_device *device;
1459*5c4a5fe1SAndy Fiddaman FILE *fp;
1460*5c4a5fe1SAndy Fiddaman char *bootorder;
1461*5c4a5fe1SAndy Fiddaman size_t bootorder_len;
1462*5c4a5fe1SAndy Fiddaman
1463*5c4a5fe1SAndy Fiddaman if (TAILQ_EMPTY(&boot_devices))
1464*5c4a5fe1SAndy Fiddaman return (0);
1465*5c4a5fe1SAndy Fiddaman
1466*5c4a5fe1SAndy Fiddaman fp = open_memstream(&bootorder, &bootorder_len);
1467*5c4a5fe1SAndy Fiddaman TAILQ_FOREACH(device, &boot_devices, boot_device_chain) {
1468*5c4a5fe1SAndy Fiddaman fprintf(fp, "/pci@i0cf8/pci@%d,%d\n",
1469*5c4a5fe1SAndy Fiddaman device->pdi->pi_slot, device->pdi->pi_func);
1470*5c4a5fe1SAndy Fiddaman }
1471*5c4a5fe1SAndy Fiddaman fclose(fp);
1472*5c4a5fe1SAndy Fiddaman
1473*5c4a5fe1SAndy Fiddaman return (qemu_fwcfg_add_file("bootorder", bootorder_len, bootorder));
1474*5c4a5fe1SAndy Fiddaman }
1475*5c4a5fe1SAndy Fiddaman
1476*5c4a5fe1SAndy Fiddaman #define BUSIO_ROUNDUP 32
1477*5c4a5fe1SAndy Fiddaman #define BUSMEM32_ROUNDUP (1024 * 1024)
1478*5c4a5fe1SAndy Fiddaman #define BUSMEM64_ROUNDUP (512 * 1024 * 1024)
1479*5c4a5fe1SAndy Fiddaman
1480*5c4a5fe1SAndy Fiddaman int
init_pci(struct vmctx * ctx)1481*5c4a5fe1SAndy Fiddaman init_pci(struct vmctx *ctx)
1482*5c4a5fe1SAndy Fiddaman {
1483*5c4a5fe1SAndy Fiddaman char node_name[sizeof("pci.XXX.XX.X")];
1484*5c4a5fe1SAndy Fiddaman struct mem_range mr;
1485*5c4a5fe1SAndy Fiddaman struct pci_devemu *pde;
1486*5c4a5fe1SAndy Fiddaman struct businfo *bi;
1487*5c4a5fe1SAndy Fiddaman struct slotinfo *si;
1488*5c4a5fe1SAndy Fiddaman struct funcinfo *fi;
1489*5c4a5fe1SAndy Fiddaman nvlist_t *nvl;
1490*5c4a5fe1SAndy Fiddaman const char *emul;
1491*5c4a5fe1SAndy Fiddaman size_t lowmem;
1492*5c4a5fe1SAndy Fiddaman int bus, slot, func;
1493*5c4a5fe1SAndy Fiddaman int error;
1494*5c4a5fe1SAndy Fiddaman
1495*5c4a5fe1SAndy Fiddaman if (vm_get_lowmem_limit(ctx) > PCI_EMUL_MEMBASE32)
1496*5c4a5fe1SAndy Fiddaman errx(EX_OSERR, "Invalid lowmem limit");
1497*5c4a5fe1SAndy Fiddaman
1498*5c4a5fe1SAndy Fiddaman pci_emul_iobase = PCI_EMUL_IOBASE;
1499*5c4a5fe1SAndy Fiddaman pci_emul_membase32 = PCI_EMUL_MEMBASE32;
1500*5c4a5fe1SAndy Fiddaman
1501*5c4a5fe1SAndy Fiddaman pci_emul_membase64 = vm_get_highmem_base(ctx) +
1502*5c4a5fe1SAndy Fiddaman vm_get_highmem_size(ctx);
1503*5c4a5fe1SAndy Fiddaman pci_emul_membase64 = roundup2(pci_emul_membase64, PCI_EMUL_MEMSIZE64);
1504*5c4a5fe1SAndy Fiddaman pci_emul_memlim64 = pci_emul_membase64 + PCI_EMUL_MEMSIZE64;
1505*5c4a5fe1SAndy Fiddaman
1506*5c4a5fe1SAndy Fiddaman TAILQ_INIT(&boot_devices);
1507*5c4a5fe1SAndy Fiddaman
1508*5c4a5fe1SAndy Fiddaman for (bus = 0; bus < MAXBUSES; bus++) {
1509*5c4a5fe1SAndy Fiddaman snprintf(node_name, sizeof(node_name), "pci.%d", bus);
1510*5c4a5fe1SAndy Fiddaman nvl = find_config_node(node_name);
1511*5c4a5fe1SAndy Fiddaman if (nvl == NULL)
1512*5c4a5fe1SAndy Fiddaman continue;
1513*5c4a5fe1SAndy Fiddaman pci_businfo[bus] = calloc(1, sizeof(struct businfo));
1514*5c4a5fe1SAndy Fiddaman bi = pci_businfo[bus];
1515*5c4a5fe1SAndy Fiddaman
1516*5c4a5fe1SAndy Fiddaman /*
1517*5c4a5fe1SAndy Fiddaman * Keep track of the i/o and memory resources allocated to
1518*5c4a5fe1SAndy Fiddaman * this bus.
1519*5c4a5fe1SAndy Fiddaman */
1520*5c4a5fe1SAndy Fiddaman bi->iobase = pci_emul_iobase;
1521*5c4a5fe1SAndy Fiddaman bi->membase32 = pci_emul_membase32;
1522*5c4a5fe1SAndy Fiddaman bi->membase64 = pci_emul_membase64;
1523*5c4a5fe1SAndy Fiddaman
1524*5c4a5fe1SAndy Fiddaman /* first run: init devices */
1525*5c4a5fe1SAndy Fiddaman for (slot = 0; slot < MAXSLOTS; slot++) {
1526*5c4a5fe1SAndy Fiddaman si = &bi->slotinfo[slot];
1527*5c4a5fe1SAndy Fiddaman for (func = 0; func < MAXFUNCS; func++) {
1528*5c4a5fe1SAndy Fiddaman fi = &si->si_funcs[func];
1529*5c4a5fe1SAndy Fiddaman snprintf(node_name, sizeof(node_name),
1530*5c4a5fe1SAndy Fiddaman "pci.%d.%d.%d", bus, slot, func);
1531*5c4a5fe1SAndy Fiddaman nvl = find_config_node(node_name);
1532*5c4a5fe1SAndy Fiddaman if (nvl == NULL)
1533*5c4a5fe1SAndy Fiddaman continue;
1534*5c4a5fe1SAndy Fiddaman
1535*5c4a5fe1SAndy Fiddaman fi->fi_config = nvl;
1536*5c4a5fe1SAndy Fiddaman emul = get_config_value_node(nvl, "device");
1537*5c4a5fe1SAndy Fiddaman if (emul == NULL) {
1538*5c4a5fe1SAndy Fiddaman EPRINTLN("pci slot %d:%d:%d: missing "
1539*5c4a5fe1SAndy Fiddaman "\"device\" value", bus, slot, func);
1540*5c4a5fe1SAndy Fiddaman return (EINVAL);
1541*5c4a5fe1SAndy Fiddaman }
1542*5c4a5fe1SAndy Fiddaman pde = pci_emul_finddev(emul);
1543*5c4a5fe1SAndy Fiddaman if (pde == NULL) {
1544*5c4a5fe1SAndy Fiddaman EPRINTLN("pci slot %d:%d:%d: unknown "
1545*5c4a5fe1SAndy Fiddaman "device \"%s\"", bus, slot, func,
1546*5c4a5fe1SAndy Fiddaman emul);
1547*5c4a5fe1SAndy Fiddaman return (EINVAL);
1548*5c4a5fe1SAndy Fiddaman }
1549*5c4a5fe1SAndy Fiddaman if (pde->pe_alias != NULL) {
1550*5c4a5fe1SAndy Fiddaman EPRINTLN("pci slot %d:%d:%d: legacy "
1551*5c4a5fe1SAndy Fiddaman "device \"%s\", use \"%s\" instead",
1552*5c4a5fe1SAndy Fiddaman bus, slot, func, emul,
1553*5c4a5fe1SAndy Fiddaman pde->pe_alias);
1554*5c4a5fe1SAndy Fiddaman return (EINVAL);
1555*5c4a5fe1SAndy Fiddaman }
1556*5c4a5fe1SAndy Fiddaman fi->fi_pde = pde;
1557*5c4a5fe1SAndy Fiddaman error = pci_emul_init(ctx, pde, bus, slot,
1558*5c4a5fe1SAndy Fiddaman func, fi);
1559*5c4a5fe1SAndy Fiddaman if (error)
1560*5c4a5fe1SAndy Fiddaman return (error);
1561*5c4a5fe1SAndy Fiddaman }
1562*5c4a5fe1SAndy Fiddaman }
1563*5c4a5fe1SAndy Fiddaman
1564*5c4a5fe1SAndy Fiddaman /* second run: assign BARs and free list */
1565*5c4a5fe1SAndy Fiddaman struct pci_bar_allocation *bar;
1566*5c4a5fe1SAndy Fiddaman struct pci_bar_allocation *bar_tmp;
1567*5c4a5fe1SAndy Fiddaman TAILQ_FOREACH_SAFE(bar, &pci_bars, chain, bar_tmp) {
1568*5c4a5fe1SAndy Fiddaman pci_emul_assign_bar(bar->pdi, bar->idx, bar->type,
1569*5c4a5fe1SAndy Fiddaman bar->size);
1570*5c4a5fe1SAndy Fiddaman free(bar);
1571*5c4a5fe1SAndy Fiddaman }
1572*5c4a5fe1SAndy Fiddaman TAILQ_INIT(&pci_bars);
1573*5c4a5fe1SAndy Fiddaman
1574*5c4a5fe1SAndy Fiddaman /*
1575*5c4a5fe1SAndy Fiddaman * Add some slop to the I/O and memory resources decoded by
1576*5c4a5fe1SAndy Fiddaman * this bus to give a guest some flexibility if it wants to
1577*5c4a5fe1SAndy Fiddaman * reprogram the BARs.
1578*5c4a5fe1SAndy Fiddaman */
1579*5c4a5fe1SAndy Fiddaman pci_emul_iobase += BUSIO_ROUNDUP;
1580*5c4a5fe1SAndy Fiddaman pci_emul_iobase = roundup2(pci_emul_iobase, BUSIO_ROUNDUP);
1581*5c4a5fe1SAndy Fiddaman bi->iolimit = pci_emul_iobase;
1582*5c4a5fe1SAndy Fiddaman
1583*5c4a5fe1SAndy Fiddaman pci_emul_membase32 += BUSMEM32_ROUNDUP;
1584*5c4a5fe1SAndy Fiddaman pci_emul_membase32 = roundup2(pci_emul_membase32,
1585*5c4a5fe1SAndy Fiddaman BUSMEM32_ROUNDUP);
1586*5c4a5fe1SAndy Fiddaman bi->memlimit32 = pci_emul_membase32;
1587*5c4a5fe1SAndy Fiddaman
1588*5c4a5fe1SAndy Fiddaman pci_emul_membase64 += BUSMEM64_ROUNDUP;
1589*5c4a5fe1SAndy Fiddaman pci_emul_membase64 = roundup2(pci_emul_membase64,
1590*5c4a5fe1SAndy Fiddaman BUSMEM64_ROUNDUP);
1591*5c4a5fe1SAndy Fiddaman bi->memlimit64 = pci_emul_membase64;
1592*5c4a5fe1SAndy Fiddaman }
1593*5c4a5fe1SAndy Fiddaman
1594*5c4a5fe1SAndy Fiddaman /*
1595*5c4a5fe1SAndy Fiddaman * PCI backends are initialized before routing INTx interrupts
1596*5c4a5fe1SAndy Fiddaman * so that LPC devices are able to reserve ISA IRQs before
1597*5c4a5fe1SAndy Fiddaman * routing PIRQ pins.
1598*5c4a5fe1SAndy Fiddaman */
1599*5c4a5fe1SAndy Fiddaman for (bus = 0; bus < MAXBUSES; bus++) {
1600*5c4a5fe1SAndy Fiddaman if ((bi = pci_businfo[bus]) == NULL)
1601*5c4a5fe1SAndy Fiddaman continue;
1602*5c4a5fe1SAndy Fiddaman
1603*5c4a5fe1SAndy Fiddaman for (slot = 0; slot < MAXSLOTS; slot++) {
1604*5c4a5fe1SAndy Fiddaman si = &bi->slotinfo[slot];
1605*5c4a5fe1SAndy Fiddaman for (func = 0; func < MAXFUNCS; func++) {
1606*5c4a5fe1SAndy Fiddaman fi = &si->si_funcs[func];
1607*5c4a5fe1SAndy Fiddaman if (fi->fi_devi == NULL)
1608*5c4a5fe1SAndy Fiddaman continue;
1609*5c4a5fe1SAndy Fiddaman pci_lintr_route(fi->fi_devi);
1610*5c4a5fe1SAndy Fiddaman }
1611*5c4a5fe1SAndy Fiddaman }
1612*5c4a5fe1SAndy Fiddaman }
1613*5c4a5fe1SAndy Fiddaman lpc_pirq_routed();
1614*5c4a5fe1SAndy Fiddaman
1615*5c4a5fe1SAndy Fiddaman if ((error = init_bootorder()) != 0) {
1616*5c4a5fe1SAndy Fiddaman warnx("%s: Unable to init bootorder", __func__);
1617*5c4a5fe1SAndy Fiddaman return (error);
1618*5c4a5fe1SAndy Fiddaman }
1619*5c4a5fe1SAndy Fiddaman
1620*5c4a5fe1SAndy Fiddaman /*
1621*5c4a5fe1SAndy Fiddaman * The guest physical memory map looks like the following:
1622*5c4a5fe1SAndy Fiddaman * [0, lowmem) guest system memory
1623*5c4a5fe1SAndy Fiddaman * [lowmem, 0xC0000000) memory hole (may be absent)
1624*5c4a5fe1SAndy Fiddaman * [0xC0000000, 0xE0000000) PCI hole (32-bit BAR allocation)
1625*5c4a5fe1SAndy Fiddaman * [0xE0000000, 0xF0000000) PCI extended config window
1626*5c4a5fe1SAndy Fiddaman * [0xF0000000, 4GB) LAPIC, IOAPIC, HPET, firmware
1627*5c4a5fe1SAndy Fiddaman * [4GB, 4GB + highmem)
1628*5c4a5fe1SAndy Fiddaman */
1629*5c4a5fe1SAndy Fiddaman
1630*5c4a5fe1SAndy Fiddaman /*
1631*5c4a5fe1SAndy Fiddaman * Accesses to memory addresses that are not allocated to system
1632*5c4a5fe1SAndy Fiddaman * memory or PCI devices return 0xff's.
1633*5c4a5fe1SAndy Fiddaman */
1634*5c4a5fe1SAndy Fiddaman lowmem = vm_get_lowmem_size(ctx);
1635*5c4a5fe1SAndy Fiddaman bzero(&mr, sizeof(struct mem_range));
1636*5c4a5fe1SAndy Fiddaman mr.name = "PCI hole";
1637*5c4a5fe1SAndy Fiddaman mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1638*5c4a5fe1SAndy Fiddaman mr.base = lowmem;
1639*5c4a5fe1SAndy Fiddaman mr.size = (4ULL * 1024 * 1024 * 1024) - lowmem;
1640*5c4a5fe1SAndy Fiddaman mr.handler = pci_emul_fallback_handler;
1641*5c4a5fe1SAndy Fiddaman error = register_mem_fallback(&mr);
1642*5c4a5fe1SAndy Fiddaman assert(error == 0);
1643*5c4a5fe1SAndy Fiddaman
1644*5c4a5fe1SAndy Fiddaman /* PCI extended config space */
1645*5c4a5fe1SAndy Fiddaman bzero(&mr, sizeof(struct mem_range));
1646*5c4a5fe1SAndy Fiddaman mr.name = "PCI ECFG";
1647*5c4a5fe1SAndy Fiddaman mr.flags = MEM_F_RW | MEM_F_IMMUTABLE;
1648*5c4a5fe1SAndy Fiddaman mr.base = PCI_EMUL_ECFG_BASE;
1649*5c4a5fe1SAndy Fiddaman mr.size = PCI_EMUL_ECFG_SIZE;
1650*5c4a5fe1SAndy Fiddaman mr.handler = pci_emul_ecfg_handler;
1651*5c4a5fe1SAndy Fiddaman error = register_mem(&mr);
1652*5c4a5fe1SAndy Fiddaman assert(error == 0);
1653*5c4a5fe1SAndy Fiddaman
1654*5c4a5fe1SAndy Fiddaman return (0);
1655*5c4a5fe1SAndy Fiddaman }
1656*5c4a5fe1SAndy Fiddaman
1657*5c4a5fe1SAndy Fiddaman static void
pci_apic_prt_entry(int bus __unused,int slot,int pin,int pirq_pin __unused,int ioapic_irq,void * arg __unused)1658*5c4a5fe1SAndy Fiddaman pci_apic_prt_entry(int bus __unused, int slot, int pin, int pirq_pin __unused,
1659*5c4a5fe1SAndy Fiddaman int ioapic_irq, void *arg __unused)
1660*5c4a5fe1SAndy Fiddaman {
1661*5c4a5fe1SAndy Fiddaman
1662*5c4a5fe1SAndy Fiddaman dsdt_line(" Package ()");
1663*5c4a5fe1SAndy Fiddaman dsdt_line(" {");
1664*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1665*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%02X,", pin - 1);
1666*5c4a5fe1SAndy Fiddaman dsdt_line(" Zero,");
1667*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%X", ioapic_irq);
1668*5c4a5fe1SAndy Fiddaman dsdt_line(" },");
1669*5c4a5fe1SAndy Fiddaman }
1670*5c4a5fe1SAndy Fiddaman
1671*5c4a5fe1SAndy Fiddaman static void
pci_pirq_prt_entry(int bus __unused,int slot,int pin,int pirq_pin,int ioapic_irq __unused,void * arg __unused)1672*5c4a5fe1SAndy Fiddaman pci_pirq_prt_entry(int bus __unused, int slot, int pin, int pirq_pin,
1673*5c4a5fe1SAndy Fiddaman int ioapic_irq __unused, void *arg __unused)
1674*5c4a5fe1SAndy Fiddaman {
1675*5c4a5fe1SAndy Fiddaman char *name;
1676*5c4a5fe1SAndy Fiddaman
1677*5c4a5fe1SAndy Fiddaman name = lpc_pirq_name(pirq_pin);
1678*5c4a5fe1SAndy Fiddaman if (name == NULL)
1679*5c4a5fe1SAndy Fiddaman return;
1680*5c4a5fe1SAndy Fiddaman dsdt_line(" Package ()");
1681*5c4a5fe1SAndy Fiddaman dsdt_line(" {");
1682*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%X,", slot << 16 | 0xffff);
1683*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%02X,", pin - 1);
1684*5c4a5fe1SAndy Fiddaman dsdt_line(" %s,", name);
1685*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x00");
1686*5c4a5fe1SAndy Fiddaman dsdt_line(" },");
1687*5c4a5fe1SAndy Fiddaman free(name);
1688*5c4a5fe1SAndy Fiddaman }
1689*5c4a5fe1SAndy Fiddaman
1690*5c4a5fe1SAndy Fiddaman /*
1691*5c4a5fe1SAndy Fiddaman * A bhyve virtual machine has a flat PCI hierarchy with a root port
1692*5c4a5fe1SAndy Fiddaman * corresponding to each PCI bus.
1693*5c4a5fe1SAndy Fiddaman */
1694*5c4a5fe1SAndy Fiddaman static void
pci_bus_write_dsdt(int bus)1695*5c4a5fe1SAndy Fiddaman pci_bus_write_dsdt(int bus)
1696*5c4a5fe1SAndy Fiddaman {
1697*5c4a5fe1SAndy Fiddaman struct businfo *bi;
1698*5c4a5fe1SAndy Fiddaman struct slotinfo *si;
1699*5c4a5fe1SAndy Fiddaman struct pci_devinst *pi;
1700*5c4a5fe1SAndy Fiddaman int count, func, slot;
1701*5c4a5fe1SAndy Fiddaman
1702*5c4a5fe1SAndy Fiddaman /*
1703*5c4a5fe1SAndy Fiddaman * If there are no devices on this 'bus' then just return.
1704*5c4a5fe1SAndy Fiddaman */
1705*5c4a5fe1SAndy Fiddaman if ((bi = pci_businfo[bus]) == NULL) {
1706*5c4a5fe1SAndy Fiddaman /*
1707*5c4a5fe1SAndy Fiddaman * Bus 0 is special because it decodes the I/O ports used
1708*5c4a5fe1SAndy Fiddaman * for PCI config space access even if there are no devices
1709*5c4a5fe1SAndy Fiddaman * on it.
1710*5c4a5fe1SAndy Fiddaman */
1711*5c4a5fe1SAndy Fiddaman if (bus != 0)
1712*5c4a5fe1SAndy Fiddaman return;
1713*5c4a5fe1SAndy Fiddaman }
1714*5c4a5fe1SAndy Fiddaman
1715*5c4a5fe1SAndy Fiddaman dsdt_line(" Device (PC%02X)", bus);
1716*5c4a5fe1SAndy Fiddaman dsdt_line(" {");
1717*5c4a5fe1SAndy Fiddaman dsdt_line(" Name (_HID, EisaId (\"PNP0A03\"))");
1718*5c4a5fe1SAndy Fiddaman
1719*5c4a5fe1SAndy Fiddaman dsdt_line(" Method (_BBN, 0, NotSerialized)");
1720*5c4a5fe1SAndy Fiddaman dsdt_line(" {");
1721*5c4a5fe1SAndy Fiddaman dsdt_line(" Return (0x%08X)", bus);
1722*5c4a5fe1SAndy Fiddaman dsdt_line(" }");
1723*5c4a5fe1SAndy Fiddaman dsdt_line(" Name (_CRS, ResourceTemplate ()");
1724*5c4a5fe1SAndy Fiddaman dsdt_line(" {");
1725*5c4a5fe1SAndy Fiddaman dsdt_line(" WordBusNumber (ResourceProducer, MinFixed, "
1726*5c4a5fe1SAndy Fiddaman "MaxFixed, PosDecode,");
1727*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Granularity");
1728*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%04X, // Range Minimum", bus);
1729*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%04X, // Range Maximum", bus);
1730*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Translation Offset");
1731*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0001, // Length");
1732*5c4a5fe1SAndy Fiddaman dsdt_line(" ,, )");
1733*5c4a5fe1SAndy Fiddaman
1734*5c4a5fe1SAndy Fiddaman if (bus == 0) {
1735*5c4a5fe1SAndy Fiddaman dsdt_indent(3);
1736*5c4a5fe1SAndy Fiddaman dsdt_fixed_ioport(0xCF8, 8);
1737*5c4a5fe1SAndy Fiddaman dsdt_unindent(3);
1738*5c4a5fe1SAndy Fiddaman
1739*5c4a5fe1SAndy Fiddaman dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1740*5c4a5fe1SAndy Fiddaman "PosDecode, EntireRange,");
1741*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Granularity");
1742*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Range Minimum");
1743*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0CF7, // Range Maximum");
1744*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Translation Offset");
1745*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0CF8, // Length");
1746*5c4a5fe1SAndy Fiddaman dsdt_line(" ,, , TypeStatic)");
1747*5c4a5fe1SAndy Fiddaman
1748*5c4a5fe1SAndy Fiddaman dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1749*5c4a5fe1SAndy Fiddaman "PosDecode, EntireRange,");
1750*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Granularity");
1751*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0D00, // Range Minimum");
1752*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%04X, // Range Maximum",
1753*5c4a5fe1SAndy Fiddaman PCI_EMUL_IOBASE - 1);
1754*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Translation Offset");
1755*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%04X, // Length",
1756*5c4a5fe1SAndy Fiddaman PCI_EMUL_IOBASE - 0x0D00);
1757*5c4a5fe1SAndy Fiddaman dsdt_line(" ,, , TypeStatic)");
1758*5c4a5fe1SAndy Fiddaman
1759*5c4a5fe1SAndy Fiddaman if (bi == NULL) {
1760*5c4a5fe1SAndy Fiddaman dsdt_line(" })");
1761*5c4a5fe1SAndy Fiddaman goto done;
1762*5c4a5fe1SAndy Fiddaman }
1763*5c4a5fe1SAndy Fiddaman }
1764*5c4a5fe1SAndy Fiddaman assert(bi != NULL);
1765*5c4a5fe1SAndy Fiddaman
1766*5c4a5fe1SAndy Fiddaman /* i/o window */
1767*5c4a5fe1SAndy Fiddaman dsdt_line(" WordIO (ResourceProducer, MinFixed, MaxFixed, "
1768*5c4a5fe1SAndy Fiddaman "PosDecode, EntireRange,");
1769*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Granularity");
1770*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%04X, // Range Minimum", bi->iobase);
1771*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%04X, // Range Maximum",
1772*5c4a5fe1SAndy Fiddaman bi->iolimit - 1);
1773*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000, // Translation Offset");
1774*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%04X, // Length",
1775*5c4a5fe1SAndy Fiddaman bi->iolimit - bi->iobase);
1776*5c4a5fe1SAndy Fiddaman dsdt_line(" ,, , TypeStatic)");
1777*5c4a5fe1SAndy Fiddaman
1778*5c4a5fe1SAndy Fiddaman /* mmio window (32-bit) */
1779*5c4a5fe1SAndy Fiddaman dsdt_line(" DWordMemory (ResourceProducer, PosDecode, "
1780*5c4a5fe1SAndy Fiddaman "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1781*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x00000000, // Granularity");
1782*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%08X, // Range Minimum\n", bi->membase32);
1783*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%08X, // Range Maximum\n",
1784*5c4a5fe1SAndy Fiddaman bi->memlimit32 - 1);
1785*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x00000000, // Translation Offset");
1786*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%08X, // Length\n",
1787*5c4a5fe1SAndy Fiddaman bi->memlimit32 - bi->membase32);
1788*5c4a5fe1SAndy Fiddaman dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1789*5c4a5fe1SAndy Fiddaman
1790*5c4a5fe1SAndy Fiddaman /* mmio window (64-bit) */
1791*5c4a5fe1SAndy Fiddaman dsdt_line(" QWordMemory (ResourceProducer, PosDecode, "
1792*5c4a5fe1SAndy Fiddaman "MinFixed, MaxFixed, NonCacheable, ReadWrite,");
1793*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000000000000000, // Granularity");
1794*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%016lX, // Range Minimum\n", bi->membase64);
1795*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%016lX, // Range Maximum\n",
1796*5c4a5fe1SAndy Fiddaman bi->memlimit64 - 1);
1797*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x0000000000000000, // Translation Offset");
1798*5c4a5fe1SAndy Fiddaman dsdt_line(" 0x%016lX, // Length\n",
1799*5c4a5fe1SAndy Fiddaman bi->memlimit64 - bi->membase64);
1800*5c4a5fe1SAndy Fiddaman dsdt_line(" ,, , AddressRangeMemory, TypeStatic)");
1801*5c4a5fe1SAndy Fiddaman dsdt_line(" })");
1802*5c4a5fe1SAndy Fiddaman
1803*5c4a5fe1SAndy Fiddaman count = pci_count_lintr(bus);
1804*5c4a5fe1SAndy Fiddaman if (count != 0) {
1805*5c4a5fe1SAndy Fiddaman dsdt_indent(2);
1806*5c4a5fe1SAndy Fiddaman dsdt_line("Name (PPRT, Package ()");
1807*5c4a5fe1SAndy Fiddaman dsdt_line("{");
1808*5c4a5fe1SAndy Fiddaman pci_walk_lintr(bus, pci_pirq_prt_entry, NULL);
1809*5c4a5fe1SAndy Fiddaman dsdt_line("})");
1810*5c4a5fe1SAndy Fiddaman dsdt_line("Name (APRT, Package ()");
1811*5c4a5fe1SAndy Fiddaman dsdt_line("{");
1812*5c4a5fe1SAndy Fiddaman pci_walk_lintr(bus, pci_apic_prt_entry, NULL);
1813*5c4a5fe1SAndy Fiddaman dsdt_line("})");
1814*5c4a5fe1SAndy Fiddaman dsdt_line("Method (_PRT, 0, NotSerialized)");
1815*5c4a5fe1SAndy Fiddaman dsdt_line("{");
1816*5c4a5fe1SAndy Fiddaman dsdt_line(" If (PICM)");
1817*5c4a5fe1SAndy Fiddaman dsdt_line(" {");
1818*5c4a5fe1SAndy Fiddaman dsdt_line(" Return (APRT)");
1819*5c4a5fe1SAndy Fiddaman dsdt_line(" }");
1820*5c4a5fe1SAndy Fiddaman dsdt_line(" Else");
1821*5c4a5fe1SAndy Fiddaman dsdt_line(" {");
1822*5c4a5fe1SAndy Fiddaman dsdt_line(" Return (PPRT)");
1823*5c4a5fe1SAndy Fiddaman dsdt_line(" }");
1824*5c4a5fe1SAndy Fiddaman dsdt_line("}");
1825*5c4a5fe1SAndy Fiddaman dsdt_unindent(2);
1826*5c4a5fe1SAndy Fiddaman }
1827*5c4a5fe1SAndy Fiddaman
1828*5c4a5fe1SAndy Fiddaman dsdt_indent(2);
1829*5c4a5fe1SAndy Fiddaman for (slot = 0; slot < MAXSLOTS; slot++) {
1830*5c4a5fe1SAndy Fiddaman si = &bi->slotinfo[slot];
1831*5c4a5fe1SAndy Fiddaman for (func = 0; func < MAXFUNCS; func++) {
1832*5c4a5fe1SAndy Fiddaman pi = si->si_funcs[func].fi_devi;
1833*5c4a5fe1SAndy Fiddaman if (pi != NULL && pi->pi_d->pe_write_dsdt != NULL)
1834*5c4a5fe1SAndy Fiddaman pi->pi_d->pe_write_dsdt(pi);
1835*5c4a5fe1SAndy Fiddaman }
1836*5c4a5fe1SAndy Fiddaman }
1837*5c4a5fe1SAndy Fiddaman dsdt_unindent(2);
1838*5c4a5fe1SAndy Fiddaman done:
1839*5c4a5fe1SAndy Fiddaman dsdt_line(" }");
1840*5c4a5fe1SAndy Fiddaman }
1841*5c4a5fe1SAndy Fiddaman
1842*5c4a5fe1SAndy Fiddaman void
pci_write_dsdt(void)1843*5c4a5fe1SAndy Fiddaman pci_write_dsdt(void)
1844*5c4a5fe1SAndy Fiddaman {
1845*5c4a5fe1SAndy Fiddaman int bus;
1846*5c4a5fe1SAndy Fiddaman
1847*5c4a5fe1SAndy Fiddaman dsdt_indent(1);
1848*5c4a5fe1SAndy Fiddaman dsdt_line("Name (PICM, 0x00)");
1849*5c4a5fe1SAndy Fiddaman dsdt_line("Method (_PIC, 1, NotSerialized)");
1850*5c4a5fe1SAndy Fiddaman dsdt_line("{");
1851*5c4a5fe1SAndy Fiddaman dsdt_line(" Store (Arg0, PICM)");
1852*5c4a5fe1SAndy Fiddaman dsdt_line("}");
1853*5c4a5fe1SAndy Fiddaman dsdt_line("");
1854*5c4a5fe1SAndy Fiddaman dsdt_line("Scope (_SB)");
1855*5c4a5fe1SAndy Fiddaman dsdt_line("{");
1856*5c4a5fe1SAndy Fiddaman for (bus = 0; bus < MAXBUSES; bus++)
1857*5c4a5fe1SAndy Fiddaman pci_bus_write_dsdt(bus);
1858*5c4a5fe1SAndy Fiddaman dsdt_line("}");
1859*5c4a5fe1SAndy Fiddaman dsdt_unindent(1);
1860*5c4a5fe1SAndy Fiddaman }
1861*5c4a5fe1SAndy Fiddaman
1862*5c4a5fe1SAndy Fiddaman int
pci_bus_configured(int bus)1863*5c4a5fe1SAndy Fiddaman pci_bus_configured(int bus)
1864*5c4a5fe1SAndy Fiddaman {
1865*5c4a5fe1SAndy Fiddaman assert(bus >= 0 && bus < MAXBUSES);
1866*5c4a5fe1SAndy Fiddaman return (pci_businfo[bus] != NULL);
1867*5c4a5fe1SAndy Fiddaman }
1868*5c4a5fe1SAndy Fiddaman
1869*5c4a5fe1SAndy Fiddaman int
pci_msi_enabled(struct pci_devinst * pi)1870*5c4a5fe1SAndy Fiddaman pci_msi_enabled(struct pci_devinst *pi)
1871*5c4a5fe1SAndy Fiddaman {
1872*5c4a5fe1SAndy Fiddaman return (pi->pi_msi.enabled);
1873*5c4a5fe1SAndy Fiddaman }
1874*5c4a5fe1SAndy Fiddaman
1875*5c4a5fe1SAndy Fiddaman int
pci_msi_maxmsgnum(struct pci_devinst * pi)1876*5c4a5fe1SAndy Fiddaman pci_msi_maxmsgnum(struct pci_devinst *pi)
1877*5c4a5fe1SAndy Fiddaman {
1878*5c4a5fe1SAndy Fiddaman if (pi->pi_msi.enabled)
1879*5c4a5fe1SAndy Fiddaman return (pi->pi_msi.maxmsgnum);
1880*5c4a5fe1SAndy Fiddaman else
1881*5c4a5fe1SAndy Fiddaman return (0);
1882*5c4a5fe1SAndy Fiddaman }
1883*5c4a5fe1SAndy Fiddaman
1884*5c4a5fe1SAndy Fiddaman int
pci_msix_enabled(struct pci_devinst * pi)1885*5c4a5fe1SAndy Fiddaman pci_msix_enabled(struct pci_devinst *pi)
1886*5c4a5fe1SAndy Fiddaman {
1887*5c4a5fe1SAndy Fiddaman
1888*5c4a5fe1SAndy Fiddaman return (pi->pi_msix.enabled && !pi->pi_msi.enabled);
1889*5c4a5fe1SAndy Fiddaman }
1890*5c4a5fe1SAndy Fiddaman
1891*5c4a5fe1SAndy Fiddaman void
pci_generate_msix(struct pci_devinst * pi,int index)1892*5c4a5fe1SAndy Fiddaman pci_generate_msix(struct pci_devinst *pi, int index)
1893*5c4a5fe1SAndy Fiddaman {
1894*5c4a5fe1SAndy Fiddaman struct msix_table_entry *mte;
1895*5c4a5fe1SAndy Fiddaman
1896*5c4a5fe1SAndy Fiddaman if (!pci_msix_enabled(pi))
1897*5c4a5fe1SAndy Fiddaman return;
1898*5c4a5fe1SAndy Fiddaman
1899*5c4a5fe1SAndy Fiddaman if (pi->pi_msix.function_mask)
1900*5c4a5fe1SAndy Fiddaman return;
1901*5c4a5fe1SAndy Fiddaman
1902*5c4a5fe1SAndy Fiddaman if (index >= pi->pi_msix.table_count)
1903*5c4a5fe1SAndy Fiddaman return;
1904*5c4a5fe1SAndy Fiddaman
1905*5c4a5fe1SAndy Fiddaman mte = &pi->pi_msix.table[index];
1906*5c4a5fe1SAndy Fiddaman if ((mte->vector_control & PCIM_MSIX_VCTRL_MASK) == 0) {
1907*5c4a5fe1SAndy Fiddaman /* XXX Set PBA bit if interrupt is disabled */
1908*5c4a5fe1SAndy Fiddaman vm_lapic_msi(pi->pi_vmctx, mte->addr, mte->msg_data);
1909*5c4a5fe1SAndy Fiddaman }
1910*5c4a5fe1SAndy Fiddaman }
1911*5c4a5fe1SAndy Fiddaman
1912*5c4a5fe1SAndy Fiddaman void
pci_generate_msi(struct pci_devinst * pi,int index)1913*5c4a5fe1SAndy Fiddaman pci_generate_msi(struct pci_devinst *pi, int index)
1914*5c4a5fe1SAndy Fiddaman {
1915*5c4a5fe1SAndy Fiddaman
1916*5c4a5fe1SAndy Fiddaman if (pci_msi_enabled(pi) && index < pci_msi_maxmsgnum(pi)) {
1917*5c4a5fe1SAndy Fiddaman vm_lapic_msi(pi->pi_vmctx, pi->pi_msi.addr,
1918*5c4a5fe1SAndy Fiddaman pi->pi_msi.msg_data + index);
1919*5c4a5fe1SAndy Fiddaman }
1920*5c4a5fe1SAndy Fiddaman }
1921*5c4a5fe1SAndy Fiddaman
1922*5c4a5fe1SAndy Fiddaman static bool
pci_lintr_permitted(struct pci_devinst * pi)1923*5c4a5fe1SAndy Fiddaman pci_lintr_permitted(struct pci_devinst *pi)
1924*5c4a5fe1SAndy Fiddaman {
1925*5c4a5fe1SAndy Fiddaman uint16_t cmd;
1926*5c4a5fe1SAndy Fiddaman
1927*5c4a5fe1SAndy Fiddaman cmd = pci_get_cfgdata16(pi, PCIR_COMMAND);
1928*5c4a5fe1SAndy Fiddaman return (!(pi->pi_msi.enabled || pi->pi_msix.enabled ||
1929*5c4a5fe1SAndy Fiddaman (cmd & PCIM_CMD_INTxDIS)));
1930*5c4a5fe1SAndy Fiddaman }
1931*5c4a5fe1SAndy Fiddaman
1932*5c4a5fe1SAndy Fiddaman void
pci_lintr_request(struct pci_devinst * pi)1933*5c4a5fe1SAndy Fiddaman pci_lintr_request(struct pci_devinst *pi)
1934*5c4a5fe1SAndy Fiddaman {
1935*5c4a5fe1SAndy Fiddaman struct businfo *bi;
1936*5c4a5fe1SAndy Fiddaman struct slotinfo *si;
1937*5c4a5fe1SAndy Fiddaman int bestpin, bestcount, pin;
1938*5c4a5fe1SAndy Fiddaman
1939*5c4a5fe1SAndy Fiddaman bi = pci_businfo[pi->pi_bus];
1940*5c4a5fe1SAndy Fiddaman assert(bi != NULL);
1941*5c4a5fe1SAndy Fiddaman
1942*5c4a5fe1SAndy Fiddaman /*
1943*5c4a5fe1SAndy Fiddaman * Just allocate a pin from our slot. The pin will be
1944*5c4a5fe1SAndy Fiddaman * assigned IRQs later when interrupts are routed.
1945*5c4a5fe1SAndy Fiddaman */
1946*5c4a5fe1SAndy Fiddaman si = &bi->slotinfo[pi->pi_slot];
1947*5c4a5fe1SAndy Fiddaman bestpin = 0;
1948*5c4a5fe1SAndy Fiddaman bestcount = si->si_intpins[0].ii_count;
1949*5c4a5fe1SAndy Fiddaman for (pin = 1; pin < 4; pin++) {
1950*5c4a5fe1SAndy Fiddaman if (si->si_intpins[pin].ii_count < bestcount) {
1951*5c4a5fe1SAndy Fiddaman bestpin = pin;
1952*5c4a5fe1SAndy Fiddaman bestcount = si->si_intpins[pin].ii_count;
1953*5c4a5fe1SAndy Fiddaman }
1954*5c4a5fe1SAndy Fiddaman }
1955*5c4a5fe1SAndy Fiddaman
1956*5c4a5fe1SAndy Fiddaman si->si_intpins[bestpin].ii_count++;
1957*5c4a5fe1SAndy Fiddaman pi->pi_lintr.pin = bestpin + 1;
1958*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, PCIR_INTPIN, bestpin + 1);
1959*5c4a5fe1SAndy Fiddaman }
1960*5c4a5fe1SAndy Fiddaman
1961*5c4a5fe1SAndy Fiddaman static void
pci_lintr_route(struct pci_devinst * pi)1962*5c4a5fe1SAndy Fiddaman pci_lintr_route(struct pci_devinst *pi)
1963*5c4a5fe1SAndy Fiddaman {
1964*5c4a5fe1SAndy Fiddaman struct businfo *bi;
1965*5c4a5fe1SAndy Fiddaman struct intxinfo *ii;
1966*5c4a5fe1SAndy Fiddaman
1967*5c4a5fe1SAndy Fiddaman if (pi->pi_lintr.pin == 0)
1968*5c4a5fe1SAndy Fiddaman return;
1969*5c4a5fe1SAndy Fiddaman
1970*5c4a5fe1SAndy Fiddaman bi = pci_businfo[pi->pi_bus];
1971*5c4a5fe1SAndy Fiddaman assert(bi != NULL);
1972*5c4a5fe1SAndy Fiddaman ii = &bi->slotinfo[pi->pi_slot].si_intpins[pi->pi_lintr.pin - 1];
1973*5c4a5fe1SAndy Fiddaman
1974*5c4a5fe1SAndy Fiddaman /*
1975*5c4a5fe1SAndy Fiddaman * Attempt to allocate an I/O APIC pin for this intpin if one
1976*5c4a5fe1SAndy Fiddaman * is not yet assigned.
1977*5c4a5fe1SAndy Fiddaman */
1978*5c4a5fe1SAndy Fiddaman if (ii->ii_ioapic_irq == 0)
1979*5c4a5fe1SAndy Fiddaman ii->ii_ioapic_irq = ioapic_pci_alloc_irq(pi);
1980*5c4a5fe1SAndy Fiddaman assert(ii->ii_ioapic_irq > 0);
1981*5c4a5fe1SAndy Fiddaman
1982*5c4a5fe1SAndy Fiddaman /*
1983*5c4a5fe1SAndy Fiddaman * Attempt to allocate a PIRQ pin for this intpin if one is
1984*5c4a5fe1SAndy Fiddaman * not yet assigned.
1985*5c4a5fe1SAndy Fiddaman */
1986*5c4a5fe1SAndy Fiddaman if (ii->ii_pirq_pin == 0)
1987*5c4a5fe1SAndy Fiddaman ii->ii_pirq_pin = pirq_alloc_pin(pi);
1988*5c4a5fe1SAndy Fiddaman assert(ii->ii_pirq_pin > 0);
1989*5c4a5fe1SAndy Fiddaman
1990*5c4a5fe1SAndy Fiddaman pi->pi_lintr.ioapic_irq = ii->ii_ioapic_irq;
1991*5c4a5fe1SAndy Fiddaman pi->pi_lintr.pirq_pin = ii->ii_pirq_pin;
1992*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, PCIR_INTLINE, pirq_irq(ii->ii_pirq_pin));
1993*5c4a5fe1SAndy Fiddaman }
1994*5c4a5fe1SAndy Fiddaman
1995*5c4a5fe1SAndy Fiddaman void
pci_lintr_assert(struct pci_devinst * pi)1996*5c4a5fe1SAndy Fiddaman pci_lintr_assert(struct pci_devinst *pi)
1997*5c4a5fe1SAndy Fiddaman {
1998*5c4a5fe1SAndy Fiddaman
1999*5c4a5fe1SAndy Fiddaman assert(pi->pi_lintr.pin > 0);
2000*5c4a5fe1SAndy Fiddaman
2001*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&pi->pi_lintr.lock);
2002*5c4a5fe1SAndy Fiddaman if (pi->pi_lintr.state == IDLE) {
2003*5c4a5fe1SAndy Fiddaman if (pci_lintr_permitted(pi)) {
2004*5c4a5fe1SAndy Fiddaman pi->pi_lintr.state = ASSERTED;
2005*5c4a5fe1SAndy Fiddaman pci_irq_assert(pi);
2006*5c4a5fe1SAndy Fiddaman } else
2007*5c4a5fe1SAndy Fiddaman pi->pi_lintr.state = PENDING;
2008*5c4a5fe1SAndy Fiddaman }
2009*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&pi->pi_lintr.lock);
2010*5c4a5fe1SAndy Fiddaman }
2011*5c4a5fe1SAndy Fiddaman
2012*5c4a5fe1SAndy Fiddaman void
pci_lintr_deassert(struct pci_devinst * pi)2013*5c4a5fe1SAndy Fiddaman pci_lintr_deassert(struct pci_devinst *pi)
2014*5c4a5fe1SAndy Fiddaman {
2015*5c4a5fe1SAndy Fiddaman
2016*5c4a5fe1SAndy Fiddaman assert(pi->pi_lintr.pin > 0);
2017*5c4a5fe1SAndy Fiddaman
2018*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&pi->pi_lintr.lock);
2019*5c4a5fe1SAndy Fiddaman if (pi->pi_lintr.state == ASSERTED) {
2020*5c4a5fe1SAndy Fiddaman pi->pi_lintr.state = IDLE;
2021*5c4a5fe1SAndy Fiddaman pci_irq_deassert(pi);
2022*5c4a5fe1SAndy Fiddaman } else if (pi->pi_lintr.state == PENDING)
2023*5c4a5fe1SAndy Fiddaman pi->pi_lintr.state = IDLE;
2024*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&pi->pi_lintr.lock);
2025*5c4a5fe1SAndy Fiddaman }
2026*5c4a5fe1SAndy Fiddaman
2027*5c4a5fe1SAndy Fiddaman static void
pci_lintr_update(struct pci_devinst * pi)2028*5c4a5fe1SAndy Fiddaman pci_lintr_update(struct pci_devinst *pi)
2029*5c4a5fe1SAndy Fiddaman {
2030*5c4a5fe1SAndy Fiddaman
2031*5c4a5fe1SAndy Fiddaman pthread_mutex_lock(&pi->pi_lintr.lock);
2032*5c4a5fe1SAndy Fiddaman if (pi->pi_lintr.state == ASSERTED && !pci_lintr_permitted(pi)) {
2033*5c4a5fe1SAndy Fiddaman pci_irq_deassert(pi);
2034*5c4a5fe1SAndy Fiddaman pi->pi_lintr.state = PENDING;
2035*5c4a5fe1SAndy Fiddaman } else if (pi->pi_lintr.state == PENDING && pci_lintr_permitted(pi)) {
2036*5c4a5fe1SAndy Fiddaman pi->pi_lintr.state = ASSERTED;
2037*5c4a5fe1SAndy Fiddaman pci_irq_assert(pi);
2038*5c4a5fe1SAndy Fiddaman }
2039*5c4a5fe1SAndy Fiddaman pthread_mutex_unlock(&pi->pi_lintr.lock);
2040*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
2041*5c4a5fe1SAndy Fiddaman if (pi->pi_d->pe_lintrupdate != NULL) {
2042*5c4a5fe1SAndy Fiddaman pi->pi_d->pe_lintrupdate(pi);
2043*5c4a5fe1SAndy Fiddaman }
2044*5c4a5fe1SAndy Fiddaman #endif /* __FreeBSD__ */
2045*5c4a5fe1SAndy Fiddaman }
2046*5c4a5fe1SAndy Fiddaman
2047*5c4a5fe1SAndy Fiddaman int
pci_count_lintr(int bus)2048*5c4a5fe1SAndy Fiddaman pci_count_lintr(int bus)
2049*5c4a5fe1SAndy Fiddaman {
2050*5c4a5fe1SAndy Fiddaman int count, slot, pin;
2051*5c4a5fe1SAndy Fiddaman struct slotinfo *slotinfo;
2052*5c4a5fe1SAndy Fiddaman
2053*5c4a5fe1SAndy Fiddaman count = 0;
2054*5c4a5fe1SAndy Fiddaman if (pci_businfo[bus] != NULL) {
2055*5c4a5fe1SAndy Fiddaman for (slot = 0; slot < MAXSLOTS; slot++) {
2056*5c4a5fe1SAndy Fiddaman slotinfo = &pci_businfo[bus]->slotinfo[slot];
2057*5c4a5fe1SAndy Fiddaman for (pin = 0; pin < 4; pin++) {
2058*5c4a5fe1SAndy Fiddaman if (slotinfo->si_intpins[pin].ii_count != 0)
2059*5c4a5fe1SAndy Fiddaman count++;
2060*5c4a5fe1SAndy Fiddaman }
2061*5c4a5fe1SAndy Fiddaman }
2062*5c4a5fe1SAndy Fiddaman }
2063*5c4a5fe1SAndy Fiddaman return (count);
2064*5c4a5fe1SAndy Fiddaman }
2065*5c4a5fe1SAndy Fiddaman
2066*5c4a5fe1SAndy Fiddaman void
pci_walk_lintr(int bus,pci_lintr_cb cb,void * arg)2067*5c4a5fe1SAndy Fiddaman pci_walk_lintr(int bus, pci_lintr_cb cb, void *arg)
2068*5c4a5fe1SAndy Fiddaman {
2069*5c4a5fe1SAndy Fiddaman struct businfo *bi;
2070*5c4a5fe1SAndy Fiddaman struct slotinfo *si;
2071*5c4a5fe1SAndy Fiddaman struct intxinfo *ii;
2072*5c4a5fe1SAndy Fiddaman int slot, pin;
2073*5c4a5fe1SAndy Fiddaman
2074*5c4a5fe1SAndy Fiddaman if ((bi = pci_businfo[bus]) == NULL)
2075*5c4a5fe1SAndy Fiddaman return;
2076*5c4a5fe1SAndy Fiddaman
2077*5c4a5fe1SAndy Fiddaman for (slot = 0; slot < MAXSLOTS; slot++) {
2078*5c4a5fe1SAndy Fiddaman si = &bi->slotinfo[slot];
2079*5c4a5fe1SAndy Fiddaman for (pin = 0; pin < 4; pin++) {
2080*5c4a5fe1SAndy Fiddaman ii = &si->si_intpins[pin];
2081*5c4a5fe1SAndy Fiddaman if (ii->ii_count != 0)
2082*5c4a5fe1SAndy Fiddaman cb(bus, slot, pin + 1, ii->ii_pirq_pin,
2083*5c4a5fe1SAndy Fiddaman ii->ii_ioapic_irq, arg);
2084*5c4a5fe1SAndy Fiddaman }
2085*5c4a5fe1SAndy Fiddaman }
2086*5c4a5fe1SAndy Fiddaman }
2087*5c4a5fe1SAndy Fiddaman
2088*5c4a5fe1SAndy Fiddaman /*
2089*5c4a5fe1SAndy Fiddaman * Return 1 if the emulated device in 'slot' is a multi-function device.
2090*5c4a5fe1SAndy Fiddaman * Return 0 otherwise.
2091*5c4a5fe1SAndy Fiddaman */
2092*5c4a5fe1SAndy Fiddaman static int
pci_emul_is_mfdev(int bus,int slot)2093*5c4a5fe1SAndy Fiddaman pci_emul_is_mfdev(int bus, int slot)
2094*5c4a5fe1SAndy Fiddaman {
2095*5c4a5fe1SAndy Fiddaman struct businfo *bi;
2096*5c4a5fe1SAndy Fiddaman struct slotinfo *si;
2097*5c4a5fe1SAndy Fiddaman int f, numfuncs;
2098*5c4a5fe1SAndy Fiddaman
2099*5c4a5fe1SAndy Fiddaman numfuncs = 0;
2100*5c4a5fe1SAndy Fiddaman if ((bi = pci_businfo[bus]) != NULL) {
2101*5c4a5fe1SAndy Fiddaman si = &bi->slotinfo[slot];
2102*5c4a5fe1SAndy Fiddaman for (f = 0; f < MAXFUNCS; f++) {
2103*5c4a5fe1SAndy Fiddaman if (si->si_funcs[f].fi_devi != NULL) {
2104*5c4a5fe1SAndy Fiddaman numfuncs++;
2105*5c4a5fe1SAndy Fiddaman }
2106*5c4a5fe1SAndy Fiddaman }
2107*5c4a5fe1SAndy Fiddaman }
2108*5c4a5fe1SAndy Fiddaman return (numfuncs > 1);
2109*5c4a5fe1SAndy Fiddaman }
2110*5c4a5fe1SAndy Fiddaman
2111*5c4a5fe1SAndy Fiddaman /*
2112*5c4a5fe1SAndy Fiddaman * Ensure that the PCIM_MFDEV bit is properly set (or unset) depending on
2113*5c4a5fe1SAndy Fiddaman * whether or not is a multi-function being emulated in the pci 'slot'.
2114*5c4a5fe1SAndy Fiddaman */
2115*5c4a5fe1SAndy Fiddaman static void
pci_emul_hdrtype_fixup(int bus,int slot,int off,int bytes,uint32_t * rv)2116*5c4a5fe1SAndy Fiddaman pci_emul_hdrtype_fixup(int bus, int slot, int off, int bytes, uint32_t *rv)
2117*5c4a5fe1SAndy Fiddaman {
2118*5c4a5fe1SAndy Fiddaman int mfdev;
2119*5c4a5fe1SAndy Fiddaman
2120*5c4a5fe1SAndy Fiddaman if (off <= PCIR_HDRTYPE && off + bytes > PCIR_HDRTYPE) {
2121*5c4a5fe1SAndy Fiddaman mfdev = pci_emul_is_mfdev(bus, slot);
2122*5c4a5fe1SAndy Fiddaman switch (bytes) {
2123*5c4a5fe1SAndy Fiddaman case 1:
2124*5c4a5fe1SAndy Fiddaman case 2:
2125*5c4a5fe1SAndy Fiddaman *rv &= ~PCIM_MFDEV;
2126*5c4a5fe1SAndy Fiddaman if (mfdev) {
2127*5c4a5fe1SAndy Fiddaman *rv |= PCIM_MFDEV;
2128*5c4a5fe1SAndy Fiddaman }
2129*5c4a5fe1SAndy Fiddaman break;
2130*5c4a5fe1SAndy Fiddaman case 4:
2131*5c4a5fe1SAndy Fiddaman *rv &= ~(PCIM_MFDEV << 16);
2132*5c4a5fe1SAndy Fiddaman if (mfdev) {
2133*5c4a5fe1SAndy Fiddaman *rv |= (PCIM_MFDEV << 16);
2134*5c4a5fe1SAndy Fiddaman }
2135*5c4a5fe1SAndy Fiddaman break;
2136*5c4a5fe1SAndy Fiddaman }
2137*5c4a5fe1SAndy Fiddaman }
2138*5c4a5fe1SAndy Fiddaman }
2139*5c4a5fe1SAndy Fiddaman
2140*5c4a5fe1SAndy Fiddaman /*
2141*5c4a5fe1SAndy Fiddaman * Update device state in response to changes to the PCI command
2142*5c4a5fe1SAndy Fiddaman * register.
2143*5c4a5fe1SAndy Fiddaman */
2144*5c4a5fe1SAndy Fiddaman void
pci_emul_cmd_changed(struct pci_devinst * pi,uint16_t old)2145*5c4a5fe1SAndy Fiddaman pci_emul_cmd_changed(struct pci_devinst *pi, uint16_t old)
2146*5c4a5fe1SAndy Fiddaman {
2147*5c4a5fe1SAndy Fiddaman int i;
2148*5c4a5fe1SAndy Fiddaman uint16_t changed, new;
2149*5c4a5fe1SAndy Fiddaman
2150*5c4a5fe1SAndy Fiddaman new = pci_get_cfgdata16(pi, PCIR_COMMAND);
2151*5c4a5fe1SAndy Fiddaman changed = old ^ new;
2152*5c4a5fe1SAndy Fiddaman
2153*5c4a5fe1SAndy Fiddaman /*
2154*5c4a5fe1SAndy Fiddaman * If the MMIO or I/O address space decoding has changed then
2155*5c4a5fe1SAndy Fiddaman * register/unregister all BARs that decode that address space.
2156*5c4a5fe1SAndy Fiddaman */
2157*5c4a5fe1SAndy Fiddaman for (i = 0; i <= PCI_BARMAX_WITH_ROM; i++) {
2158*5c4a5fe1SAndy Fiddaman switch (pi->pi_bar[i].type) {
2159*5c4a5fe1SAndy Fiddaman case PCIBAR_NONE:
2160*5c4a5fe1SAndy Fiddaman case PCIBAR_MEMHI64:
2161*5c4a5fe1SAndy Fiddaman break;
2162*5c4a5fe1SAndy Fiddaman case PCIBAR_IO:
2163*5c4a5fe1SAndy Fiddaman /* I/O address space decoding changed? */
2164*5c4a5fe1SAndy Fiddaman if (changed & PCIM_CMD_PORTEN) {
2165*5c4a5fe1SAndy Fiddaman if (new & PCIM_CMD_PORTEN)
2166*5c4a5fe1SAndy Fiddaman register_bar(pi, i);
2167*5c4a5fe1SAndy Fiddaman else
2168*5c4a5fe1SAndy Fiddaman unregister_bar(pi, i);
2169*5c4a5fe1SAndy Fiddaman }
2170*5c4a5fe1SAndy Fiddaman break;
2171*5c4a5fe1SAndy Fiddaman case PCIBAR_ROM:
2172*5c4a5fe1SAndy Fiddaman /* skip (un-)register of ROM if it disabled */
2173*5c4a5fe1SAndy Fiddaman if (!romen(pi))
2174*5c4a5fe1SAndy Fiddaman break;
2175*5c4a5fe1SAndy Fiddaman /* fallthrough */
2176*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM32:
2177*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM64:
2178*5c4a5fe1SAndy Fiddaman /* MMIO address space decoding changed? */
2179*5c4a5fe1SAndy Fiddaman if (changed & PCIM_CMD_MEMEN) {
2180*5c4a5fe1SAndy Fiddaman if (new & PCIM_CMD_MEMEN)
2181*5c4a5fe1SAndy Fiddaman register_bar(pi, i);
2182*5c4a5fe1SAndy Fiddaman else
2183*5c4a5fe1SAndy Fiddaman unregister_bar(pi, i);
2184*5c4a5fe1SAndy Fiddaman }
2185*5c4a5fe1SAndy Fiddaman break;
2186*5c4a5fe1SAndy Fiddaman default:
2187*5c4a5fe1SAndy Fiddaman assert(0);
2188*5c4a5fe1SAndy Fiddaman }
2189*5c4a5fe1SAndy Fiddaman }
2190*5c4a5fe1SAndy Fiddaman
2191*5c4a5fe1SAndy Fiddaman /*
2192*5c4a5fe1SAndy Fiddaman * If INTx has been unmasked and is pending, assert the
2193*5c4a5fe1SAndy Fiddaman * interrupt.
2194*5c4a5fe1SAndy Fiddaman */
2195*5c4a5fe1SAndy Fiddaman pci_lintr_update(pi);
2196*5c4a5fe1SAndy Fiddaman }
2197*5c4a5fe1SAndy Fiddaman
2198*5c4a5fe1SAndy Fiddaman static void
pci_emul_cmdsts_write(struct pci_devinst * pi,int coff,uint32_t new,int bytes)2199*5c4a5fe1SAndy Fiddaman pci_emul_cmdsts_write(struct pci_devinst *pi, int coff, uint32_t new, int bytes)
2200*5c4a5fe1SAndy Fiddaman {
2201*5c4a5fe1SAndy Fiddaman int rshift;
2202*5c4a5fe1SAndy Fiddaman uint32_t cmd, old, readonly;
2203*5c4a5fe1SAndy Fiddaman
2204*5c4a5fe1SAndy Fiddaman cmd = pci_get_cfgdata16(pi, PCIR_COMMAND); /* stash old value */
2205*5c4a5fe1SAndy Fiddaman
2206*5c4a5fe1SAndy Fiddaman /*
2207*5c4a5fe1SAndy Fiddaman * From PCI Local Bus Specification 3.0 sections 6.2.2 and 6.2.3.
2208*5c4a5fe1SAndy Fiddaman *
2209*5c4a5fe1SAndy Fiddaman * XXX Bits 8, 11, 12, 13, 14 and 15 in the status register are
2210*5c4a5fe1SAndy Fiddaman * 'write 1 to clear'. However these bits are not set to '1' by
2211*5c4a5fe1SAndy Fiddaman * any device emulation so it is simpler to treat them as readonly.
2212*5c4a5fe1SAndy Fiddaman */
2213*5c4a5fe1SAndy Fiddaman rshift = (coff & 0x3) * 8;
2214*5c4a5fe1SAndy Fiddaman readonly = 0xFFFFF880 >> rshift;
2215*5c4a5fe1SAndy Fiddaman
2216*5c4a5fe1SAndy Fiddaman old = CFGREAD(pi, coff, bytes);
2217*5c4a5fe1SAndy Fiddaman new &= ~readonly;
2218*5c4a5fe1SAndy Fiddaman new |= (old & readonly);
2219*5c4a5fe1SAndy Fiddaman CFGWRITE(pi, coff, new, bytes); /* update config */
2220*5c4a5fe1SAndy Fiddaman
2221*5c4a5fe1SAndy Fiddaman pci_emul_cmd_changed(pi, cmd);
2222*5c4a5fe1SAndy Fiddaman }
2223*5c4a5fe1SAndy Fiddaman
2224*5c4a5fe1SAndy Fiddaman static void
pci_cfgrw(int in,int bus,int slot,int func,int coff,int bytes,uint32_t * valp)2225*5c4a5fe1SAndy Fiddaman pci_cfgrw(int in, int bus, int slot, int func, int coff, int bytes,
2226*5c4a5fe1SAndy Fiddaman uint32_t *valp)
2227*5c4a5fe1SAndy Fiddaman {
2228*5c4a5fe1SAndy Fiddaman struct businfo *bi;
2229*5c4a5fe1SAndy Fiddaman struct slotinfo *si;
2230*5c4a5fe1SAndy Fiddaman struct pci_devinst *pi;
2231*5c4a5fe1SAndy Fiddaman struct pci_devemu *pe;
2232*5c4a5fe1SAndy Fiddaman int idx, needcfg;
2233*5c4a5fe1SAndy Fiddaman uint64_t addr, mask;
2234*5c4a5fe1SAndy Fiddaman uint64_t bar = 0;
2235*5c4a5fe1SAndy Fiddaman
2236*5c4a5fe1SAndy Fiddaman if ((bi = pci_businfo[bus]) != NULL) {
2237*5c4a5fe1SAndy Fiddaman si = &bi->slotinfo[slot];
2238*5c4a5fe1SAndy Fiddaman pi = si->si_funcs[func].fi_devi;
2239*5c4a5fe1SAndy Fiddaman } else
2240*5c4a5fe1SAndy Fiddaman pi = NULL;
2241*5c4a5fe1SAndy Fiddaman
2242*5c4a5fe1SAndy Fiddaman /*
2243*5c4a5fe1SAndy Fiddaman * Just return if there is no device at this slot:func or if the
2244*5c4a5fe1SAndy Fiddaman * guest is doing an un-aligned access.
2245*5c4a5fe1SAndy Fiddaman */
2246*5c4a5fe1SAndy Fiddaman if (pi == NULL || (bytes != 1 && bytes != 2 && bytes != 4) ||
2247*5c4a5fe1SAndy Fiddaman (coff & (bytes - 1)) != 0) {
2248*5c4a5fe1SAndy Fiddaman if (in)
2249*5c4a5fe1SAndy Fiddaman *valp = 0xffffffff;
2250*5c4a5fe1SAndy Fiddaman return;
2251*5c4a5fe1SAndy Fiddaman }
2252*5c4a5fe1SAndy Fiddaman
2253*5c4a5fe1SAndy Fiddaman /*
2254*5c4a5fe1SAndy Fiddaman * Ignore all writes beyond the standard config space and return all
2255*5c4a5fe1SAndy Fiddaman * ones on reads.
2256*5c4a5fe1SAndy Fiddaman */
2257*5c4a5fe1SAndy Fiddaman if (coff >= PCI_REGMAX + 1) {
2258*5c4a5fe1SAndy Fiddaman if (in) {
2259*5c4a5fe1SAndy Fiddaman *valp = 0xffffffff;
2260*5c4a5fe1SAndy Fiddaman /*
2261*5c4a5fe1SAndy Fiddaman * Extended capabilities begin at offset 256 in config
2262*5c4a5fe1SAndy Fiddaman * space. Absence of extended capabilities is signaled
2263*5c4a5fe1SAndy Fiddaman * with all 0s in the extended capability header at
2264*5c4a5fe1SAndy Fiddaman * offset 256.
2265*5c4a5fe1SAndy Fiddaman */
2266*5c4a5fe1SAndy Fiddaman if (coff <= PCI_REGMAX + 4)
2267*5c4a5fe1SAndy Fiddaman *valp = 0x00000000;
2268*5c4a5fe1SAndy Fiddaman }
2269*5c4a5fe1SAndy Fiddaman return;
2270*5c4a5fe1SAndy Fiddaman }
2271*5c4a5fe1SAndy Fiddaman
2272*5c4a5fe1SAndy Fiddaman pe = pi->pi_d;
2273*5c4a5fe1SAndy Fiddaman
2274*5c4a5fe1SAndy Fiddaman /*
2275*5c4a5fe1SAndy Fiddaman * Config read
2276*5c4a5fe1SAndy Fiddaman */
2277*5c4a5fe1SAndy Fiddaman if (in) {
2278*5c4a5fe1SAndy Fiddaman /* Let the device emulation override the default handler */
2279*5c4a5fe1SAndy Fiddaman if (pe->pe_cfgread != NULL) {
2280*5c4a5fe1SAndy Fiddaman needcfg = pe->pe_cfgread(pi, coff, bytes, valp);
2281*5c4a5fe1SAndy Fiddaman } else {
2282*5c4a5fe1SAndy Fiddaman needcfg = 1;
2283*5c4a5fe1SAndy Fiddaman }
2284*5c4a5fe1SAndy Fiddaman
2285*5c4a5fe1SAndy Fiddaman if (needcfg)
2286*5c4a5fe1SAndy Fiddaman *valp = CFGREAD(pi, coff, bytes);
2287*5c4a5fe1SAndy Fiddaman
2288*5c4a5fe1SAndy Fiddaman pci_emul_hdrtype_fixup(bus, slot, coff, bytes, valp);
2289*5c4a5fe1SAndy Fiddaman } else {
2290*5c4a5fe1SAndy Fiddaman /* Let the device emulation override the default handler */
2291*5c4a5fe1SAndy Fiddaman if (pe->pe_cfgwrite != NULL &&
2292*5c4a5fe1SAndy Fiddaman (*pe->pe_cfgwrite)(pi, coff, bytes, *valp) == 0)
2293*5c4a5fe1SAndy Fiddaman return;
2294*5c4a5fe1SAndy Fiddaman
2295*5c4a5fe1SAndy Fiddaman /*
2296*5c4a5fe1SAndy Fiddaman * Special handling for write to BAR and ROM registers
2297*5c4a5fe1SAndy Fiddaman */
2298*5c4a5fe1SAndy Fiddaman if (is_pcir_bar(coff) || is_pcir_bios(coff)) {
2299*5c4a5fe1SAndy Fiddaman /*
2300*5c4a5fe1SAndy Fiddaman * Ignore writes to BAR registers that are not
2301*5c4a5fe1SAndy Fiddaman * 4-byte aligned.
2302*5c4a5fe1SAndy Fiddaman */
2303*5c4a5fe1SAndy Fiddaman if (bytes != 4 || (coff & 0x3) != 0)
2304*5c4a5fe1SAndy Fiddaman return;
2305*5c4a5fe1SAndy Fiddaman
2306*5c4a5fe1SAndy Fiddaman if (is_pcir_bar(coff)) {
2307*5c4a5fe1SAndy Fiddaman idx = (coff - PCIR_BAR(0)) / 4;
2308*5c4a5fe1SAndy Fiddaman } else if (is_pcir_bios(coff)) {
2309*5c4a5fe1SAndy Fiddaman idx = PCI_ROM_IDX;
2310*5c4a5fe1SAndy Fiddaman } else {
2311*5c4a5fe1SAndy Fiddaman errx(4, "%s: invalid BAR offset %d", __func__,
2312*5c4a5fe1SAndy Fiddaman coff);
2313*5c4a5fe1SAndy Fiddaman }
2314*5c4a5fe1SAndy Fiddaman
2315*5c4a5fe1SAndy Fiddaman mask = ~(pi->pi_bar[idx].size - 1);
2316*5c4a5fe1SAndy Fiddaman switch (pi->pi_bar[idx].type) {
2317*5c4a5fe1SAndy Fiddaman case PCIBAR_NONE:
2318*5c4a5fe1SAndy Fiddaman pi->pi_bar[idx].addr = bar = 0;
2319*5c4a5fe1SAndy Fiddaman break;
2320*5c4a5fe1SAndy Fiddaman case PCIBAR_IO:
2321*5c4a5fe1SAndy Fiddaman addr = *valp & mask;
2322*5c4a5fe1SAndy Fiddaman addr &= 0xffff;
2323*5c4a5fe1SAndy Fiddaman bar = addr | pi->pi_bar[idx].lobits;
2324*5c4a5fe1SAndy Fiddaman /*
2325*5c4a5fe1SAndy Fiddaman * Register the new BAR value for interception
2326*5c4a5fe1SAndy Fiddaman */
2327*5c4a5fe1SAndy Fiddaman if (addr != pi->pi_bar[idx].addr) {
2328*5c4a5fe1SAndy Fiddaman update_bar_address(pi, addr, idx,
2329*5c4a5fe1SAndy Fiddaman PCIBAR_IO);
2330*5c4a5fe1SAndy Fiddaman }
2331*5c4a5fe1SAndy Fiddaman break;
2332*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM32:
2333*5c4a5fe1SAndy Fiddaman addr = bar = *valp & mask;
2334*5c4a5fe1SAndy Fiddaman bar |= pi->pi_bar[idx].lobits;
2335*5c4a5fe1SAndy Fiddaman if (addr != pi->pi_bar[idx].addr) {
2336*5c4a5fe1SAndy Fiddaman update_bar_address(pi, addr, idx,
2337*5c4a5fe1SAndy Fiddaman PCIBAR_MEM32);
2338*5c4a5fe1SAndy Fiddaman }
2339*5c4a5fe1SAndy Fiddaman break;
2340*5c4a5fe1SAndy Fiddaman case PCIBAR_MEM64:
2341*5c4a5fe1SAndy Fiddaman addr = bar = *valp & mask;
2342*5c4a5fe1SAndy Fiddaman bar |= pi->pi_bar[idx].lobits;
2343*5c4a5fe1SAndy Fiddaman if (addr != (uint32_t)pi->pi_bar[idx].addr) {
2344*5c4a5fe1SAndy Fiddaman update_bar_address(pi, addr, idx,
2345*5c4a5fe1SAndy Fiddaman PCIBAR_MEM64);
2346*5c4a5fe1SAndy Fiddaman }
2347*5c4a5fe1SAndy Fiddaman break;
2348*5c4a5fe1SAndy Fiddaman case PCIBAR_MEMHI64:
2349*5c4a5fe1SAndy Fiddaman mask = ~(pi->pi_bar[idx - 1].size - 1);
2350*5c4a5fe1SAndy Fiddaman addr = ((uint64_t)*valp << 32) & mask;
2351*5c4a5fe1SAndy Fiddaman bar = addr >> 32;
2352*5c4a5fe1SAndy Fiddaman if (bar != pi->pi_bar[idx - 1].addr >> 32) {
2353*5c4a5fe1SAndy Fiddaman update_bar_address(pi, addr, idx - 1,
2354*5c4a5fe1SAndy Fiddaman PCIBAR_MEMHI64);
2355*5c4a5fe1SAndy Fiddaman }
2356*5c4a5fe1SAndy Fiddaman break;
2357*5c4a5fe1SAndy Fiddaman case PCIBAR_ROM:
2358*5c4a5fe1SAndy Fiddaman addr = bar = *valp & mask;
2359*5c4a5fe1SAndy Fiddaman if (memen(pi) && romen(pi)) {
2360*5c4a5fe1SAndy Fiddaman unregister_bar(pi, idx);
2361*5c4a5fe1SAndy Fiddaman }
2362*5c4a5fe1SAndy Fiddaman pi->pi_bar[idx].addr = addr;
2363*5c4a5fe1SAndy Fiddaman pi->pi_bar[idx].lobits = *valp &
2364*5c4a5fe1SAndy Fiddaman PCIM_BIOS_ENABLE;
2365*5c4a5fe1SAndy Fiddaman /* romen could have changed it value */
2366*5c4a5fe1SAndy Fiddaman if (memen(pi) && romen(pi)) {
2367*5c4a5fe1SAndy Fiddaman register_bar(pi, idx);
2368*5c4a5fe1SAndy Fiddaman }
2369*5c4a5fe1SAndy Fiddaman bar |= pi->pi_bar[idx].lobits;
2370*5c4a5fe1SAndy Fiddaman break;
2371*5c4a5fe1SAndy Fiddaman default:
2372*5c4a5fe1SAndy Fiddaman assert(0);
2373*5c4a5fe1SAndy Fiddaman }
2374*5c4a5fe1SAndy Fiddaman pci_set_cfgdata32(pi, coff, bar);
2375*5c4a5fe1SAndy Fiddaman
2376*5c4a5fe1SAndy Fiddaman } else if (pci_emul_iscap(pi, coff)) {
2377*5c4a5fe1SAndy Fiddaman pci_emul_capwrite(pi, coff, bytes, *valp, 0, 0);
2378*5c4a5fe1SAndy Fiddaman } else if (coff >= PCIR_COMMAND && coff < PCIR_REVID) {
2379*5c4a5fe1SAndy Fiddaman pci_emul_cmdsts_write(pi, coff, *valp, bytes);
2380*5c4a5fe1SAndy Fiddaman } else {
2381*5c4a5fe1SAndy Fiddaman CFGWRITE(pi, coff, *valp, bytes);
2382*5c4a5fe1SAndy Fiddaman }
2383*5c4a5fe1SAndy Fiddaman }
2384*5c4a5fe1SAndy Fiddaman }
2385*5c4a5fe1SAndy Fiddaman
2386*5c4a5fe1SAndy Fiddaman static int cfgenable, cfgbus, cfgslot, cfgfunc, cfgoff;
2387*5c4a5fe1SAndy Fiddaman
2388*5c4a5fe1SAndy Fiddaman static int
pci_emul_cfgaddr(struct vmctx * ctx __unused,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)2389*5c4a5fe1SAndy Fiddaman pci_emul_cfgaddr(struct vmctx *ctx __unused, int in,
2390*5c4a5fe1SAndy Fiddaman int port __unused, int bytes, uint32_t *eax, void *arg __unused)
2391*5c4a5fe1SAndy Fiddaman {
2392*5c4a5fe1SAndy Fiddaman uint32_t x;
2393*5c4a5fe1SAndy Fiddaman
2394*5c4a5fe1SAndy Fiddaman if (bytes != 4) {
2395*5c4a5fe1SAndy Fiddaman if (in)
2396*5c4a5fe1SAndy Fiddaman *eax = (bytes == 2) ? 0xffff : 0xff;
2397*5c4a5fe1SAndy Fiddaman return (0);
2398*5c4a5fe1SAndy Fiddaman }
2399*5c4a5fe1SAndy Fiddaman
2400*5c4a5fe1SAndy Fiddaman if (in) {
2401*5c4a5fe1SAndy Fiddaman x = (cfgbus << 16) | (cfgslot << 11) | (cfgfunc << 8) | cfgoff;
2402*5c4a5fe1SAndy Fiddaman if (cfgenable)
2403*5c4a5fe1SAndy Fiddaman x |= CONF1_ENABLE;
2404*5c4a5fe1SAndy Fiddaman *eax = x;
2405*5c4a5fe1SAndy Fiddaman } else {
2406*5c4a5fe1SAndy Fiddaman x = *eax;
2407*5c4a5fe1SAndy Fiddaman cfgenable = (x & CONF1_ENABLE) == CONF1_ENABLE;
2408*5c4a5fe1SAndy Fiddaman cfgoff = (x & PCI_REGMAX) & ~0x03;
2409*5c4a5fe1SAndy Fiddaman cfgfunc = (x >> 8) & PCI_FUNCMAX;
2410*5c4a5fe1SAndy Fiddaman cfgslot = (x >> 11) & PCI_SLOTMAX;
2411*5c4a5fe1SAndy Fiddaman cfgbus = (x >> 16) & PCI_BUSMAX;
2412*5c4a5fe1SAndy Fiddaman }
2413*5c4a5fe1SAndy Fiddaman
2414*5c4a5fe1SAndy Fiddaman return (0);
2415*5c4a5fe1SAndy Fiddaman }
2416*5c4a5fe1SAndy Fiddaman INOUT_PORT(pci_cfgaddr, CONF1_ADDR_PORT, IOPORT_F_INOUT, pci_emul_cfgaddr);
2417*5c4a5fe1SAndy Fiddaman
2418*5c4a5fe1SAndy Fiddaman static int
pci_emul_cfgdata(struct vmctx * ctx __unused,int in,int port,int bytes,uint32_t * eax,void * arg __unused)2419*5c4a5fe1SAndy Fiddaman pci_emul_cfgdata(struct vmctx *ctx __unused, int in, int port,
2420*5c4a5fe1SAndy Fiddaman int bytes, uint32_t *eax, void *arg __unused)
2421*5c4a5fe1SAndy Fiddaman {
2422*5c4a5fe1SAndy Fiddaman int coff;
2423*5c4a5fe1SAndy Fiddaman
2424*5c4a5fe1SAndy Fiddaman assert(bytes == 1 || bytes == 2 || bytes == 4);
2425*5c4a5fe1SAndy Fiddaman
2426*5c4a5fe1SAndy Fiddaman coff = cfgoff + (port - CONF1_DATA_PORT);
2427*5c4a5fe1SAndy Fiddaman if (cfgenable) {
2428*5c4a5fe1SAndy Fiddaman pci_cfgrw(in, cfgbus, cfgslot, cfgfunc, coff, bytes, eax);
2429*5c4a5fe1SAndy Fiddaman } else {
2430*5c4a5fe1SAndy Fiddaman /* Ignore accesses to cfgdata if not enabled by cfgaddr */
2431*5c4a5fe1SAndy Fiddaman if (in)
2432*5c4a5fe1SAndy Fiddaman *eax = 0xffffffff;
2433*5c4a5fe1SAndy Fiddaman }
2434*5c4a5fe1SAndy Fiddaman return (0);
2435*5c4a5fe1SAndy Fiddaman }
2436*5c4a5fe1SAndy Fiddaman
2437*5c4a5fe1SAndy Fiddaman INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+0, IOPORT_F_INOUT, pci_emul_cfgdata);
2438*5c4a5fe1SAndy Fiddaman INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+1, IOPORT_F_INOUT, pci_emul_cfgdata);
2439*5c4a5fe1SAndy Fiddaman INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+2, IOPORT_F_INOUT, pci_emul_cfgdata);
2440*5c4a5fe1SAndy Fiddaman INOUT_PORT(pci_cfgdata, CONF1_DATA_PORT+3, IOPORT_F_INOUT, pci_emul_cfgdata);
2441*5c4a5fe1SAndy Fiddaman
2442*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_TEST
2443*5c4a5fe1SAndy Fiddaman #ifdef PCI_EMUL_TEST
2444*5c4a5fe1SAndy Fiddaman /*
2445*5c4a5fe1SAndy Fiddaman * Define a dummy test device
2446*5c4a5fe1SAndy Fiddaman */
2447*5c4a5fe1SAndy Fiddaman #define DIOSZ 8
2448*5c4a5fe1SAndy Fiddaman #define DMEMSZ 4096
2449*5c4a5fe1SAndy Fiddaman struct pci_emul_dsoftc {
2450*5c4a5fe1SAndy Fiddaman uint8_t ioregs[DIOSZ];
2451*5c4a5fe1SAndy Fiddaman uint8_t memregs[2][DMEMSZ];
2452*5c4a5fe1SAndy Fiddaman };
2453*5c4a5fe1SAndy Fiddaman
2454*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_MSI_MSGS 4
2455*5c4a5fe1SAndy Fiddaman #define PCI_EMUL_MSIX_MSGS 16
2456*5c4a5fe1SAndy Fiddaman
2457*5c4a5fe1SAndy Fiddaman static int
pci_emul_dinit(struct pci_devinst * pi,nvlist_t * nvl __unused)2458*5c4a5fe1SAndy Fiddaman pci_emul_dinit(struct pci_devinst *pi, nvlist_t *nvl __unused)
2459*5c4a5fe1SAndy Fiddaman {
2460*5c4a5fe1SAndy Fiddaman int error;
2461*5c4a5fe1SAndy Fiddaman struct pci_emul_dsoftc *sc;
2462*5c4a5fe1SAndy Fiddaman
2463*5c4a5fe1SAndy Fiddaman sc = calloc(1, sizeof(struct pci_emul_dsoftc));
2464*5c4a5fe1SAndy Fiddaman
2465*5c4a5fe1SAndy Fiddaman pi->pi_arg = sc;
2466*5c4a5fe1SAndy Fiddaman
2467*5c4a5fe1SAndy Fiddaman pci_set_cfgdata16(pi, PCIR_DEVICE, 0x0001);
2468*5c4a5fe1SAndy Fiddaman pci_set_cfgdata16(pi, PCIR_VENDOR, 0x10DD);
2469*5c4a5fe1SAndy Fiddaman pci_set_cfgdata8(pi, PCIR_CLASS, 0x02);
2470*5c4a5fe1SAndy Fiddaman
2471*5c4a5fe1SAndy Fiddaman error = pci_emul_add_msicap(pi, PCI_EMUL_MSI_MSGS);
2472*5c4a5fe1SAndy Fiddaman assert(error == 0);
2473*5c4a5fe1SAndy Fiddaman
2474*5c4a5fe1SAndy Fiddaman error = pci_emul_alloc_bar(pi, 0, PCIBAR_IO, DIOSZ);
2475*5c4a5fe1SAndy Fiddaman assert(error == 0);
2476*5c4a5fe1SAndy Fiddaman
2477*5c4a5fe1SAndy Fiddaman error = pci_emul_alloc_bar(pi, 1, PCIBAR_MEM32, DMEMSZ);
2478*5c4a5fe1SAndy Fiddaman assert(error == 0);
2479*5c4a5fe1SAndy Fiddaman
2480*5c4a5fe1SAndy Fiddaman error = pci_emul_alloc_bar(pi, 2, PCIBAR_MEM32, DMEMSZ);
2481*5c4a5fe1SAndy Fiddaman assert(error == 0);
2482*5c4a5fe1SAndy Fiddaman
2483*5c4a5fe1SAndy Fiddaman return (0);
2484*5c4a5fe1SAndy Fiddaman }
2485*5c4a5fe1SAndy Fiddaman
2486*5c4a5fe1SAndy Fiddaman static void
pci_emul_diow(struct pci_devinst * pi,int baridx,uint64_t offset,int size,uint64_t value)2487*5c4a5fe1SAndy Fiddaman pci_emul_diow(struct pci_devinst *pi, int baridx, uint64_t offset, int size,
2488*5c4a5fe1SAndy Fiddaman uint64_t value)
2489*5c4a5fe1SAndy Fiddaman {
2490*5c4a5fe1SAndy Fiddaman int i;
2491*5c4a5fe1SAndy Fiddaman struct pci_emul_dsoftc *sc = pi->pi_arg;
2492*5c4a5fe1SAndy Fiddaman
2493*5c4a5fe1SAndy Fiddaman if (baridx == 0) {
2494*5c4a5fe1SAndy Fiddaman if (offset + size > DIOSZ) {
2495*5c4a5fe1SAndy Fiddaman printf("diow: iow too large, offset %ld size %d\n",
2496*5c4a5fe1SAndy Fiddaman offset, size);
2497*5c4a5fe1SAndy Fiddaman return;
2498*5c4a5fe1SAndy Fiddaman }
2499*5c4a5fe1SAndy Fiddaman
2500*5c4a5fe1SAndy Fiddaman if (size == 1) {
2501*5c4a5fe1SAndy Fiddaman sc->ioregs[offset] = value & 0xff;
2502*5c4a5fe1SAndy Fiddaman } else if (size == 2) {
2503*5c4a5fe1SAndy Fiddaman *(uint16_t *)&sc->ioregs[offset] = value & 0xffff;
2504*5c4a5fe1SAndy Fiddaman } else if (size == 4) {
2505*5c4a5fe1SAndy Fiddaman *(uint32_t *)&sc->ioregs[offset] = value;
2506*5c4a5fe1SAndy Fiddaman } else {
2507*5c4a5fe1SAndy Fiddaman printf("diow: iow unknown size %d\n", size);
2508*5c4a5fe1SAndy Fiddaman }
2509*5c4a5fe1SAndy Fiddaman
2510*5c4a5fe1SAndy Fiddaman /*
2511*5c4a5fe1SAndy Fiddaman * Special magic value to generate an interrupt
2512*5c4a5fe1SAndy Fiddaman */
2513*5c4a5fe1SAndy Fiddaman if (offset == 4 && size == 4 && pci_msi_enabled(pi))
2514*5c4a5fe1SAndy Fiddaman pci_generate_msi(pi, value % pci_msi_maxmsgnum(pi));
2515*5c4a5fe1SAndy Fiddaman
2516*5c4a5fe1SAndy Fiddaman if (value == 0xabcdef) {
2517*5c4a5fe1SAndy Fiddaman for (i = 0; i < pci_msi_maxmsgnum(pi); i++)
2518*5c4a5fe1SAndy Fiddaman pci_generate_msi(pi, i);
2519*5c4a5fe1SAndy Fiddaman }
2520*5c4a5fe1SAndy Fiddaman }
2521*5c4a5fe1SAndy Fiddaman
2522*5c4a5fe1SAndy Fiddaman if (baridx == 1 || baridx == 2) {
2523*5c4a5fe1SAndy Fiddaman if (offset + size > DMEMSZ) {
2524*5c4a5fe1SAndy Fiddaman printf("diow: memw too large, offset %ld size %d\n",
2525*5c4a5fe1SAndy Fiddaman offset, size);
2526*5c4a5fe1SAndy Fiddaman return;
2527*5c4a5fe1SAndy Fiddaman }
2528*5c4a5fe1SAndy Fiddaman
2529*5c4a5fe1SAndy Fiddaman i = baridx - 1; /* 'memregs' index */
2530*5c4a5fe1SAndy Fiddaman
2531*5c4a5fe1SAndy Fiddaman if (size == 1) {
2532*5c4a5fe1SAndy Fiddaman sc->memregs[i][offset] = value;
2533*5c4a5fe1SAndy Fiddaman } else if (size == 2) {
2534*5c4a5fe1SAndy Fiddaman *(uint16_t *)&sc->memregs[i][offset] = value;
2535*5c4a5fe1SAndy Fiddaman } else if (size == 4) {
2536*5c4a5fe1SAndy Fiddaman *(uint32_t *)&sc->memregs[i][offset] = value;
2537*5c4a5fe1SAndy Fiddaman } else if (size == 8) {
2538*5c4a5fe1SAndy Fiddaman *(uint64_t *)&sc->memregs[i][offset] = value;
2539*5c4a5fe1SAndy Fiddaman } else {
2540*5c4a5fe1SAndy Fiddaman printf("diow: memw unknown size %d\n", size);
2541*5c4a5fe1SAndy Fiddaman }
2542*5c4a5fe1SAndy Fiddaman
2543*5c4a5fe1SAndy Fiddaman /*
2544*5c4a5fe1SAndy Fiddaman * magic interrupt ??
2545*5c4a5fe1SAndy Fiddaman */
2546*5c4a5fe1SAndy Fiddaman }
2547*5c4a5fe1SAndy Fiddaman
2548*5c4a5fe1SAndy Fiddaman if (baridx > 2 || baridx < 0) {
2549*5c4a5fe1SAndy Fiddaman printf("diow: unknown bar idx %d\n", baridx);
2550*5c4a5fe1SAndy Fiddaman }
2551*5c4a5fe1SAndy Fiddaman }
2552*5c4a5fe1SAndy Fiddaman
2553*5c4a5fe1SAndy Fiddaman static uint64_t
pci_emul_dior(struct pci_devinst * pi,int baridx,uint64_t offset,int size)2554*5c4a5fe1SAndy Fiddaman pci_emul_dior(struct pci_devinst *pi, int baridx, uint64_t offset, int size)
2555*5c4a5fe1SAndy Fiddaman {
2556*5c4a5fe1SAndy Fiddaman struct pci_emul_dsoftc *sc = pi->pi_arg;
2557*5c4a5fe1SAndy Fiddaman uint32_t value;
2558*5c4a5fe1SAndy Fiddaman int i;
2559*5c4a5fe1SAndy Fiddaman
2560*5c4a5fe1SAndy Fiddaman value = 0;
2561*5c4a5fe1SAndy Fiddaman if (baridx == 0) {
2562*5c4a5fe1SAndy Fiddaman if (offset + size > DIOSZ) {
2563*5c4a5fe1SAndy Fiddaman printf("dior: ior too large, offset %ld size %d\n",
2564*5c4a5fe1SAndy Fiddaman offset, size);
2565*5c4a5fe1SAndy Fiddaman return (0);
2566*5c4a5fe1SAndy Fiddaman }
2567*5c4a5fe1SAndy Fiddaman
2568*5c4a5fe1SAndy Fiddaman value = 0;
2569*5c4a5fe1SAndy Fiddaman if (size == 1) {
2570*5c4a5fe1SAndy Fiddaman value = sc->ioregs[offset];
2571*5c4a5fe1SAndy Fiddaman } else if (size == 2) {
2572*5c4a5fe1SAndy Fiddaman value = *(uint16_t *) &sc->ioregs[offset];
2573*5c4a5fe1SAndy Fiddaman } else if (size == 4) {
2574*5c4a5fe1SAndy Fiddaman value = *(uint32_t *) &sc->ioregs[offset];
2575*5c4a5fe1SAndy Fiddaman } else {
2576*5c4a5fe1SAndy Fiddaman printf("dior: ior unknown size %d\n", size);
2577*5c4a5fe1SAndy Fiddaman }
2578*5c4a5fe1SAndy Fiddaman }
2579*5c4a5fe1SAndy Fiddaman
2580*5c4a5fe1SAndy Fiddaman if (baridx == 1 || baridx == 2) {
2581*5c4a5fe1SAndy Fiddaman if (offset + size > DMEMSZ) {
2582*5c4a5fe1SAndy Fiddaman printf("dior: memr too large, offset %ld size %d\n",
2583*5c4a5fe1SAndy Fiddaman offset, size);
2584*5c4a5fe1SAndy Fiddaman return (0);
2585*5c4a5fe1SAndy Fiddaman }
2586*5c4a5fe1SAndy Fiddaman
2587*5c4a5fe1SAndy Fiddaman i = baridx - 1; /* 'memregs' index */
2588*5c4a5fe1SAndy Fiddaman
2589*5c4a5fe1SAndy Fiddaman if (size == 1) {
2590*5c4a5fe1SAndy Fiddaman value = sc->memregs[i][offset];
2591*5c4a5fe1SAndy Fiddaman } else if (size == 2) {
2592*5c4a5fe1SAndy Fiddaman value = *(uint16_t *) &sc->memregs[i][offset];
2593*5c4a5fe1SAndy Fiddaman } else if (size == 4) {
2594*5c4a5fe1SAndy Fiddaman value = *(uint32_t *) &sc->memregs[i][offset];
2595*5c4a5fe1SAndy Fiddaman } else if (size == 8) {
2596*5c4a5fe1SAndy Fiddaman value = *(uint64_t *) &sc->memregs[i][offset];
2597*5c4a5fe1SAndy Fiddaman } else {
2598*5c4a5fe1SAndy Fiddaman printf("dior: ior unknown size %d\n", size);
2599*5c4a5fe1SAndy Fiddaman }
2600*5c4a5fe1SAndy Fiddaman }
2601*5c4a5fe1SAndy Fiddaman
2602*5c4a5fe1SAndy Fiddaman
2603*5c4a5fe1SAndy Fiddaman if (baridx > 2 || baridx < 0) {
2604*5c4a5fe1SAndy Fiddaman printf("dior: unknown bar idx %d\n", baridx);
2605*5c4a5fe1SAndy Fiddaman return (0);
2606*5c4a5fe1SAndy Fiddaman }
2607*5c4a5fe1SAndy Fiddaman
2608*5c4a5fe1SAndy Fiddaman return (value);
2609*5c4a5fe1SAndy Fiddaman }
2610*5c4a5fe1SAndy Fiddaman
2611*5c4a5fe1SAndy Fiddaman static const struct pci_devemu pci_dummy = {
2612*5c4a5fe1SAndy Fiddaman .pe_emu = "dummy",
2613*5c4a5fe1SAndy Fiddaman .pe_init = pci_emul_dinit,
2614*5c4a5fe1SAndy Fiddaman .pe_barwrite = pci_emul_diow,
2615*5c4a5fe1SAndy Fiddaman .pe_barread = pci_emul_dior,
2616*5c4a5fe1SAndy Fiddaman };
2617*5c4a5fe1SAndy Fiddaman PCI_EMUL_SET(pci_dummy);
2618*5c4a5fe1SAndy Fiddaman
2619*5c4a5fe1SAndy Fiddaman #endif /* PCI_EMUL_TEST */
2620