xref: /illumos-gate/usr/src/cmd/bhyve/common/pci_hostbridge.c (revision 5c4a5fe16715fb423db76577a6883b5bbecdbe45)
1*5c4a5fe1SAndy Fiddaman /*-
2*5c4a5fe1SAndy Fiddaman  * SPDX-License-Identifier: BSD-2-Clause
3*5c4a5fe1SAndy Fiddaman  *
4*5c4a5fe1SAndy Fiddaman  * Copyright (c) 2011 NetApp, Inc.
5*5c4a5fe1SAndy Fiddaman  * Copyright (c) 2018 Joyent, Inc.
6*5c4a5fe1SAndy Fiddaman  * Copyright 2021 Oxide Computer Company
7*5c4a5fe1SAndy Fiddaman  * All rights reserved.
8*5c4a5fe1SAndy Fiddaman  *
9*5c4a5fe1SAndy Fiddaman  * Redistribution and use in source and binary forms, with or without
10*5c4a5fe1SAndy Fiddaman  * modification, are permitted provided that the following conditions
11*5c4a5fe1SAndy Fiddaman  * are met:
12*5c4a5fe1SAndy Fiddaman  * 1. Redistributions of source code must retain the above copyright
13*5c4a5fe1SAndy Fiddaman  *    notice, this list of conditions and the following disclaimer.
14*5c4a5fe1SAndy Fiddaman  * 2. Redistributions in binary form must reproduce the above copyright
15*5c4a5fe1SAndy Fiddaman  *    notice, this list of conditions and the following disclaimer in the
16*5c4a5fe1SAndy Fiddaman  *    documentation and/or other materials provided with the distribution.
17*5c4a5fe1SAndy Fiddaman  *
18*5c4a5fe1SAndy Fiddaman  * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
19*5c4a5fe1SAndy Fiddaman  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*5c4a5fe1SAndy Fiddaman  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*5c4a5fe1SAndy Fiddaman  * ARE DISCLAIMED.  IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
22*5c4a5fe1SAndy Fiddaman  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*5c4a5fe1SAndy Fiddaman  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*5c4a5fe1SAndy Fiddaman  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*5c4a5fe1SAndy Fiddaman  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*5c4a5fe1SAndy Fiddaman  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*5c4a5fe1SAndy Fiddaman  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*5c4a5fe1SAndy Fiddaman  * SUCH DAMAGE.
29*5c4a5fe1SAndy Fiddaman  */
30*5c4a5fe1SAndy Fiddaman 
31*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
32*5c4a5fe1SAndy Fiddaman #include <errno.h>
33*5c4a5fe1SAndy Fiddaman #include <stdlib.h>
34*5c4a5fe1SAndy Fiddaman #include <stdio.h>
35*5c4a5fe1SAndy Fiddaman #include <strings.h>
36*5c4a5fe1SAndy Fiddaman #include <err.h>
37*5c4a5fe1SAndy Fiddaman #endif
38*5c4a5fe1SAndy Fiddaman 
39*5c4a5fe1SAndy Fiddaman #include <stdlib.h>
40*5c4a5fe1SAndy Fiddaman 
41*5c4a5fe1SAndy Fiddaman #include "config.h"
42*5c4a5fe1SAndy Fiddaman #include "pci_emul.h"
43*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
44*5c4a5fe1SAndy Fiddaman #include "bhyverun.h"
45*5c4a5fe1SAndy Fiddaman #endif
46*5c4a5fe1SAndy Fiddaman 
47*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
48*5c4a5fe1SAndy Fiddaman static struct pci_hostbridge_model {
49*5c4a5fe1SAndy Fiddaman 	const char	*phm_model;
50*5c4a5fe1SAndy Fiddaman 	uint16_t	phm_vendor;
51*5c4a5fe1SAndy Fiddaman 	uint16_t	phm_device;
52*5c4a5fe1SAndy Fiddaman } pci_hb_models[] = {
53*5c4a5fe1SAndy Fiddaman 	{ "amd",	0x1022, 0x7432 }, /* AMD/made-up */
54*5c4a5fe1SAndy Fiddaman 	{ "netapp",	0x1275, 0x1275 }, /* NetApp/NetApp */
55*5c4a5fe1SAndy Fiddaman 	{ "i440fx",	0x8086, 0x1237 }, /* Intel/82441 */
56*5c4a5fe1SAndy Fiddaman 	{ "q35",	0x8086, 0x29b0 }, /* Intel/Q35 HB */
57*5c4a5fe1SAndy Fiddaman };
58*5c4a5fe1SAndy Fiddaman 
59*5c4a5fe1SAndy Fiddaman #define	NUM_HB_MODELS	(sizeof (pci_hb_models) / sizeof (pci_hb_models[0]))
60*5c4a5fe1SAndy Fiddaman #endif
61*5c4a5fe1SAndy Fiddaman 
62*5c4a5fe1SAndy Fiddaman static int
pci_hostbridge_init(struct pci_devinst * pi,nvlist_t * nvl)63*5c4a5fe1SAndy Fiddaman pci_hostbridge_init(struct pci_devinst *pi, nvlist_t *nvl)
64*5c4a5fe1SAndy Fiddaman {
65*5c4a5fe1SAndy Fiddaman 	const char *value;
66*5c4a5fe1SAndy Fiddaman 	u_int vendor, device;
67*5c4a5fe1SAndy Fiddaman 
68*5c4a5fe1SAndy Fiddaman #ifdef __FreeBSD__
69*5c4a5fe1SAndy Fiddaman 	vendor = 0x1275;	/* NetApp */
70*5c4a5fe1SAndy Fiddaman 	device = 0x1275;	/* NetApp */
71*5c4a5fe1SAndy Fiddaman #else
72*5c4a5fe1SAndy Fiddaman 	vendor = device = 0;
73*5c4a5fe1SAndy Fiddaman #endif
74*5c4a5fe1SAndy Fiddaman 
75*5c4a5fe1SAndy Fiddaman 	value = get_config_value_node(nvl, "vendor");
76*5c4a5fe1SAndy Fiddaman 	if (value != NULL)
77*5c4a5fe1SAndy Fiddaman 		vendor = strtol(value, NULL, 0);
78*5c4a5fe1SAndy Fiddaman 	else
79*5c4a5fe1SAndy Fiddaman 		vendor = pci_config_read_reg(NULL, nvl, PCIR_VENDOR, 2, vendor);
80*5c4a5fe1SAndy Fiddaman 	value = get_config_value_node(nvl, "devid");
81*5c4a5fe1SAndy Fiddaman 	if (value != NULL)
82*5c4a5fe1SAndy Fiddaman 		device = strtol(value, NULL, 0);
83*5c4a5fe1SAndy Fiddaman 	else
84*5c4a5fe1SAndy Fiddaman 		device = pci_config_read_reg(NULL, nvl, PCIR_DEVICE, 2, device);
85*5c4a5fe1SAndy Fiddaman 
86*5c4a5fe1SAndy Fiddaman #ifndef __FreeBSD__
87*5c4a5fe1SAndy Fiddaman 	const char *model = get_config_value_node(nvl, "model");
88*5c4a5fe1SAndy Fiddaman 
89*5c4a5fe1SAndy Fiddaman 	if (model != NULL && (vendor != 0 || device != 0)) {
90*5c4a5fe1SAndy Fiddaman 		warnx("pci_hostbridge: cannot specify model and vendor/device");
91*5c4a5fe1SAndy Fiddaman 		return (-1);
92*5c4a5fe1SAndy Fiddaman 	} else if ((vendor != 0 && device == 0) ||
93*5c4a5fe1SAndy Fiddaman 	    (vendor == 0 && device != 0)) {
94*5c4a5fe1SAndy Fiddaman 		warnx("pci_hostbridge: must specify both vendor and "
95*5c4a5fe1SAndy Fiddaman 		    "device for custom hostbridge");
96*5c4a5fe1SAndy Fiddaman 		return (-1);
97*5c4a5fe1SAndy Fiddaman 	}
98*5c4a5fe1SAndy Fiddaman 	if (model == NULL && vendor == 0 && device == 0)
99*5c4a5fe1SAndy Fiddaman 		model = "netapp";
100*5c4a5fe1SAndy Fiddaman 
101*5c4a5fe1SAndy Fiddaman 	if (model != NULL) {
102*5c4a5fe1SAndy Fiddaman 		for (uint_t i = 0; i < NUM_HB_MODELS; i++) {
103*5c4a5fe1SAndy Fiddaman 			if (strcmp(model, pci_hb_models[i].phm_model) != 0)
104*5c4a5fe1SAndy Fiddaman 				continue;
105*5c4a5fe1SAndy Fiddaman 
106*5c4a5fe1SAndy Fiddaman 			/* found a model match */
107*5c4a5fe1SAndy Fiddaman 			vendor = pci_hb_models[i].phm_vendor;
108*5c4a5fe1SAndy Fiddaman 			device = pci_hb_models[i].phm_device;
109*5c4a5fe1SAndy Fiddaman 			break;
110*5c4a5fe1SAndy Fiddaman 		}
111*5c4a5fe1SAndy Fiddaman 		if (vendor == 0) {
112*5c4a5fe1SAndy Fiddaman 			warnx("pci_hostbridge: invalid model '%s'", model);
113*5c4a5fe1SAndy Fiddaman 			return (-1);
114*5c4a5fe1SAndy Fiddaman 		}
115*5c4a5fe1SAndy Fiddaman 	}
116*5c4a5fe1SAndy Fiddaman 
117*5c4a5fe1SAndy Fiddaman 	/* Both i440fx and Q35 chipsets feature the concept of Programmable
118*5c4a5fe1SAndy Fiddaman 	 * Address Memory (PAM), where certain physical address ranges can be
119*5c4a5fe1SAndy Fiddaman 	 * configured to direct reads/writes to either DRAM, or to the PCI MMIO
120*5c4a5fe1SAndy Fiddaman 	 * space instead.  At boot, they default to bypassing DRAM, so in order
121*5c4a5fe1SAndy Fiddaman 	 * to cheaply paper over our lack of emulation, the memory in PAM0
122*5c4a5fe1SAndy Fiddaman 	 * (0xf0000-0xfffff, the System BIOS segment) should be zeroed.
123*5c4a5fe1SAndy Fiddaman 	 *
124*5c4a5fe1SAndy Fiddaman 	 * If this emulation is expanded in the future to truly support PAM
125*5c4a5fe1SAndy Fiddaman 	 * behavior, this hack can be removed.
126*5c4a5fe1SAndy Fiddaman 	 */
127*5c4a5fe1SAndy Fiddaman 	if (vendor == 0x8086 && (device == 0x1237 || device == 0x29b0)) {
128*5c4a5fe1SAndy Fiddaman 		const uintptr_t start = 0xf0000;
129*5c4a5fe1SAndy Fiddaman 		const size_t len = 0x10000;
130*5c4a5fe1SAndy Fiddaman 		void *system_bios_region = paddr_guest2host(pi->pi_vmctx,
131*5c4a5fe1SAndy Fiddaman 		    start, len);
132*5c4a5fe1SAndy Fiddaman 		assert(system_bios_region != NULL);
133*5c4a5fe1SAndy Fiddaman 		bzero(system_bios_region, len);
134*5c4a5fe1SAndy Fiddaman 	}
135*5c4a5fe1SAndy Fiddaman #endif /* !__FreeBSD__ */
136*5c4a5fe1SAndy Fiddaman 
137*5c4a5fe1SAndy Fiddaman 	/* config space */
138*5c4a5fe1SAndy Fiddaman 	pci_set_cfgdata16(pi, PCIR_VENDOR, vendor);
139*5c4a5fe1SAndy Fiddaman 	pci_set_cfgdata16(pi, PCIR_DEVICE, device);
140*5c4a5fe1SAndy Fiddaman 	pci_set_cfgdata8(pi, PCIR_HDRTYPE, PCIM_HDRTYPE_NORMAL);
141*5c4a5fe1SAndy Fiddaman 	pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_BRIDGE);
142*5c4a5fe1SAndy Fiddaman 	pci_set_cfgdata8(pi, PCIR_SUBCLASS, PCIS_BRIDGE_HOST);
143*5c4a5fe1SAndy Fiddaman 
144*5c4a5fe1SAndy Fiddaman 	pci_emul_add_pciecap(pi, PCIEM_TYPE_ROOT_PORT);
145*5c4a5fe1SAndy Fiddaman 
146*5c4a5fe1SAndy Fiddaman 	return (0);
147*5c4a5fe1SAndy Fiddaman }
148*5c4a5fe1SAndy Fiddaman 
149*5c4a5fe1SAndy Fiddaman static int
pci_amd_hostbridge_legacy_config(nvlist_t * nvl,const char * opts __unused)150*5c4a5fe1SAndy Fiddaman pci_amd_hostbridge_legacy_config(nvlist_t *nvl, const char *opts __unused)
151*5c4a5fe1SAndy Fiddaman {
152*5c4a5fe1SAndy Fiddaman 	nvlist_t *pci_regs;
153*5c4a5fe1SAndy Fiddaman 
154*5c4a5fe1SAndy Fiddaman 	pci_regs = create_relative_config_node(nvl, "pcireg");
155*5c4a5fe1SAndy Fiddaman 	if (pci_regs == NULL) {
156*5c4a5fe1SAndy Fiddaman 		warnx("amd_hostbridge: failed to create pciregs node");
157*5c4a5fe1SAndy Fiddaman 		return (-1);
158*5c4a5fe1SAndy Fiddaman 	}
159*5c4a5fe1SAndy Fiddaman 	set_config_value_node(pci_regs, "vendor", "0x1022");	/* AMD */
160*5c4a5fe1SAndy Fiddaman 	set_config_value_node(pci_regs, "device", "0x7432");	/* made up */
161*5c4a5fe1SAndy Fiddaman 
162*5c4a5fe1SAndy Fiddaman 	return (0);
163*5c4a5fe1SAndy Fiddaman }
164*5c4a5fe1SAndy Fiddaman 
165*5c4a5fe1SAndy Fiddaman static const struct pci_devemu pci_de_amd_hostbridge = {
166*5c4a5fe1SAndy Fiddaman 	.pe_emu = "amd_hostbridge",
167*5c4a5fe1SAndy Fiddaman 	.pe_legacy_config = pci_amd_hostbridge_legacy_config,
168*5c4a5fe1SAndy Fiddaman 	.pe_alias = "hostbridge",
169*5c4a5fe1SAndy Fiddaman };
170*5c4a5fe1SAndy Fiddaman PCI_EMUL_SET(pci_de_amd_hostbridge);
171*5c4a5fe1SAndy Fiddaman 
172*5c4a5fe1SAndy Fiddaman static const struct pci_devemu pci_de_hostbridge = {
173*5c4a5fe1SAndy Fiddaman 	.pe_emu = "hostbridge",
174*5c4a5fe1SAndy Fiddaman 	.pe_init = pci_hostbridge_init,
175*5c4a5fe1SAndy Fiddaman };
176*5c4a5fe1SAndy Fiddaman PCI_EMUL_SET(pci_de_hostbridge);
177