2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Copyright (c) 2016 Netflix, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * PnP enumerator using the PCI BIOS.
37 #include <machine/stdarg.h>
38 #include <bootstrap.h>
44 * Stupid PCI BIOS interface doesn't let you simply enumerate everything
45 * that's there, instead you have to ask it if it has something.
47 * So we have to scan by class code, subclass code and sometimes programming
57 static struct pci_progif progif_null[] = {
62 static struct pci_progif progif_display[] = {
68 static struct pci_progif progif_ide[] = {
104 static struct pci_progif progif_serial[] = {
111 static struct pci_progif progif_parallel[] = {
113 {0x1, "Bidirectional"},
118 static struct pci_progif progif_firewire[] = {
127 struct pci_progif *ps_progif; /* if set, use for programming interface value(s) */
130 static struct pci_subclass subclass_old[] = {
131 {0x0, "Old non-VGA", progif_null},
132 {0x1, "Old VGA", progif_null},
136 static struct pci_subclass subclass_mass[] = {
137 {0x0, "SCSI", progif_null},
138 {0x1, "IDE", progif_ide},
139 {0x2, "Floppy disk", progif_null},
140 {0x3, "IPI", progif_null},
141 {0x4, "RAID", progif_null},
142 {0x80, "mass storage", progif_null},
146 static struct pci_subclass subclass_net[] = {
147 {0x0, "Ethernet", progif_null},
148 {0x1, "Token ring", progif_null},
149 {0x2, "FDDI", progif_null},
150 {0x3, "ATM", progif_null},
151 {0x80, "network", progif_null},
155 static struct pci_subclass subclass_display[] = {
156 {0x0, NULL, progif_display},
157 {0x1, "XGA", progif_null},
158 {0x80, "other", progif_null},
162 static struct pci_subclass subclass_comms[] = {
163 {0x0, "serial", progif_serial},
164 {0x1, "parallel", progif_parallel},
165 {0x80, "communications", progif_null},
169 static struct pci_subclass subclass_serial[] = {
170 {0x0, "FireWire", progif_firewire},
171 {0x1, "ACCESS.bus", progif_null},
172 {0x2, "SSA", progif_null},
173 {0x3, "USB", progif_null},
174 {0x4, "Fibrechannel", progif_null},
178 static struct pci_class
182 struct pci_subclass *pc_subclass;
184 {0x0, "device", subclass_old},
185 {0x1, "controller", subclass_mass},
186 {0x2, "controller", subclass_net},
187 {0x3, "display", subclass_display},
188 {0x7, "controller", subclass_comms},
189 {0xc, "controller", subclass_serial},
193 static void biospci_enumerate(void);
194 static void biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi);
196 struct pnphandler biospcihandler =
201 static int biospci_version;
203 #define PCI_BIOS_PRESENT 0xb101
204 #define FIND_PCI_DEVICE 0xb102
205 #define FIND_PCI_CLASS_CODE 0xb103
206 #define GENERATE_SPECIAL_CYCLE 0xb106
207 #define READ_CONFIG_BYTE 0xb108
208 #define READ_CONFIG_WORD 0xb109
209 #define READ_CONFIG_DWORD 0xb10a
210 #define WRITE_CONFIG_BYTE 0xb10b
211 #define WRITE_CONFIG_WORD 0xb10c
212 #define WRITE_CONFIG_DWORD 0xb10d
213 #define GET_IRQ_ROUTING_OPTIONS 0xb10e
214 #define SET_PCI_IRQ 0xb10f
218 #define PCI_SIGNATURE 0x20494350 /* AKA "PCI " */
223 uint16_t version, hwcap, maxbus;
226 /* Find the PCI BIOS */
229 v86.eax = PCI_BIOS_PRESENT;
233 /* Check for OK response */
234 if (V86_CY(v86.efl) || ((v86.eax & 0xff00) != 0) ||
235 (v86.edx != PCI_SIGNATURE))
238 version = v86.ebx & 0xffff;
239 hwcap = v86.eax & 0xff;
240 maxbus = v86.ecx & 0xff;
242 printf("PCI BIOS %d.%d%s%s maxbus %d\n",
243 bcd2bin((version >> 8) & 0xf), bcd2bin(version & 0xf),
244 (hwcap & 1) ? " config1" : "", (hwcap & 2) ? " config2" : "",
247 sprintf(buf, "%d", bcd2bin((version >> 8) & 0xf));
248 setenv("pcibios.major", buf, 1);
249 sprintf(buf, "%d", bcd2bin(version & 0xf));
250 setenv("pcibios.minor", buf, 1);
251 sprintf(buf, "%d", !!(hwcap & 1));
252 setenv("pcibios.config1", buf, 1);
253 sprintf(buf, "%d", !!(hwcap & 2));
254 setenv("pcibios.config2", buf, 1);
255 sprintf(buf, "%d", maxbus);
256 setenv("pcibios.maxbus", buf, 1);
257 biospci_version = bcd2bin((version >> 8) & 0xf) * 10 + bcd2bin(version & 0xf);
261 biospci_enumerate(void)
263 int device_index, err;
264 uint32_t locator, devid;
265 struct pci_class *pc;
266 struct pci_subclass *psc;
267 struct pci_progif *ppi;
269 /* Iterate over known classes */
270 for (pc = pci_classes; pc->pc_class >= 0; pc++) {
271 /* Iterate over subclasses */
272 for (psc = pc->pc_subclass; psc->ps_subclass >= 0; psc++) {
273 /* Iterate over programming interfaces */
274 for (ppi = psc->ps_progif; ppi->pi_code >= 0; ppi++) {
276 /* Scan for matches */
277 for (device_index = 0; ; device_index++) {
278 /* Look for a match */
279 err = biospci_find_devclass((pc->pc_class << 16)
280 + (psc->ps_subclass << 8) + ppi->pi_code,
281 device_index, &locator);
285 /* Read the device identifier from the nominated device */
286 err = biospci_read_config(locator, 0, BIOSPCI_32BITS, &devid);
290 /* We have the device ID, create a PnP object and save everything */
291 biospci_addinfo(devid, pc, psc, ppi);
299 biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi)
305 /* build the description */
307 if (ppi->pi_name != NULL) {
308 strcat(desc, ppi->pi_name);
311 if (psc->ps_name != NULL) {
312 strcat(desc, psc->ps_name);
315 if (pc->pc_name != NULL)
316 strcat(desc, pc->pc_name);
318 pi = pnp_allocinfo();
319 pi->pi_desc = strdup(desc);
320 sprintf(desc,"0x%08x", devid);
321 pnp_addident(pi, desc);
326 biospci_find_devclass(uint32_t class, int index, uint32_t *locator)
330 v86.eax = FIND_PCI_CLASS_CODE;
336 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
344 biospci_find_device(uint32_t devid, int index, uint32_t *locator)
348 v86.eax = FIND_PCI_DEVICE;
349 v86.edx = devid & 0xffff; /* EDX - Vendor ID */
350 v86.ecx = (devid >> 16) & 0xffff; /* ECX - Device ID */
355 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
362 * Configuration space access methods.
363 * width = 0(byte), 1(word) or 2(dword).
366 biospci_write_config(uint32_t locator, int offset, int width, uint32_t val)
370 v86.eax = WRITE_CONFIG_BYTE + width;
377 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
384 biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val)
388 v86.eax = READ_CONFIG_BYTE + width;
394 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
402 biospci_locator(int8_t bus, uint8_t device, uint8_t function)
405 return ((bus << 8) | ((device & 0x1f) << 3) | (function & 0x7));