2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2016 Netflix, Inc
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
32 * PnP enumerator using the PCI BIOS.
36 #include <machine/stdarg.h>
37 #include <bootstrap.h>
46 * Stupid PCI BIOS interface doesn't let you simply enumerate everything
47 * that's there, instead you have to ask it if it has something.
49 * So we have to scan by class code, subclass code and sometimes programming
59 static struct pci_progif progif_null[] = {
64 static struct pci_progif progif_display[] = {
70 static struct pci_progif progif_ide[] = {
106 static struct pci_progif progif_serial[] = {
113 static struct pci_progif progif_parallel[] = {
115 {0x1, "Bidirectional"},
120 static struct pci_progif progif_firewire[] = {
129 struct pci_progif *ps_progif; /* if set, use for programming interface value(s) */
132 static struct pci_subclass subclass_old[] = {
133 {0x0, "Old non-VGA", progif_null},
134 {0x1, "Old VGA", progif_null},
138 static struct pci_subclass subclass_mass[] = {
139 {0x0, "SCSI", progif_null},
140 {0x1, "IDE", progif_ide},
141 {0x2, "Floppy disk", progif_null},
142 {0x3, "IPI", progif_null},
143 {0x4, "RAID", progif_null},
144 {0x80, "mass storage", progif_null},
148 static struct pci_subclass subclass_net[] = {
149 {0x0, "Ethernet", progif_null},
150 {0x1, "Token ring", progif_null},
151 {0x2, "FDDI", progif_null},
152 {0x3, "ATM", progif_null},
153 {0x80, "network", progif_null},
157 static struct pci_subclass subclass_display[] = {
158 {0x0, NULL, progif_display},
159 {0x1, "XGA", progif_null},
160 {0x80, "other", progif_null},
164 static struct pci_subclass subclass_comms[] = {
165 {0x0, "serial", progif_serial},
166 {0x1, "parallel", progif_parallel},
167 {0x80, "communications", progif_null},
171 static struct pci_subclass subclass_serial[] = {
172 {0x0, "FireWire", progif_firewire},
173 {0x1, "ACCESS.bus", progif_null},
174 {0x2, "SSA", progif_null},
175 {0x3, "USB", progif_null},
176 {0x4, "Fibrechannel", progif_null},
180 static struct pci_class
184 struct pci_subclass *pc_subclass;
186 {0x0, "device", subclass_old},
187 {0x1, "controller", subclass_mass},
188 {0x2, "controller", subclass_net},
189 {0x3, "display", subclass_display},
190 {0x7, "controller", subclass_comms},
191 {0xc, "controller", subclass_serial},
195 static void biospci_enumerate(void);
196 static void biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi);
198 struct pnphandler biospcihandler =
203 static int biospci_version;
205 #define PCI_BIOS_PRESENT 0xb101
206 #define FIND_PCI_DEVICE 0xb102
207 #define FIND_PCI_CLASS_CODE 0xb103
208 #define GENERATE_SPECIAL_CYCLE 0xb106
209 #define READ_CONFIG_BYTE 0xb108
210 #define READ_CONFIG_WORD 0xb109
211 #define READ_CONFIG_DWORD 0xb10a
212 #define WRITE_CONFIG_BYTE 0xb10b
213 #define WRITE_CONFIG_WORD 0xb10c
214 #define WRITE_CONFIG_DWORD 0xb10d
215 #define GET_IRQ_ROUTING_OPTIONS 0xb10e
216 #define SET_PCI_IRQ 0xb10f
220 #define PCI_SIGNATURE 0x20494350 /* AKA "PCI " */
225 uint16_t version, hwcap, maxbus;
228 /* Find the PCI BIOS */
231 v86.eax = PCI_BIOS_PRESENT;
235 /* Check for OK response */
236 if (V86_CY(v86.efl) || ((v86.eax & 0xff00) != 0) ||
237 (v86.edx != PCI_SIGNATURE))
240 version = v86.ebx & 0xffff;
241 hwcap = v86.eax & 0xff;
242 maxbus = v86.ecx & 0xff;
244 printf("PCI BIOS %d.%d%s%s maxbus %d\n",
245 bcd2bin((version >> 8) & 0xf), bcd2bin(version & 0xf),
246 (hwcap & 1) ? " config1" : "", (hwcap & 2) ? " config2" : "",
249 sprintf(buf, "%d", bcd2bin((version >> 8) & 0xf));
250 setenv("pcibios.major", buf, 1);
251 sprintf(buf, "%d", bcd2bin(version & 0xf));
252 setenv("pcibios.minor", buf, 1);
253 sprintf(buf, "%d", !!(hwcap & 1));
254 setenv("pcibios.config1", buf, 1);
255 sprintf(buf, "%d", !!(hwcap & 2));
256 setenv("pcibios.config2", buf, 1);
257 sprintf(buf, "%d", maxbus);
258 setenv("pcibios.maxbus", buf, 1);
259 biospci_version = bcd2bin((version >> 8) & 0xf) * 10 + bcd2bin(version & 0xf);
263 biospci_enumerate(void)
265 int device_index, err;
266 uint32_t locator, devid;
267 struct pci_class *pc;
268 struct pci_subclass *psc;
269 struct pci_progif *ppi;
271 /* Iterate over known classes */
272 for (pc = pci_classes; pc->pc_class >= 0; pc++) {
273 /* Iterate over subclasses */
274 for (psc = pc->pc_subclass; psc->ps_subclass >= 0; psc++) {
275 /* Iterate over programming interfaces */
276 for (ppi = psc->ps_progif; ppi->pi_code >= 0; ppi++) {
278 /* Scan for matches */
279 for (device_index = 0; ; device_index++) {
280 /* Look for a match */
281 err = biospci_find_devclass((pc->pc_class << 16)
282 + (psc->ps_subclass << 8) + ppi->pi_code,
283 device_index, &locator);
287 /* Read the device identifier from the nominated device */
288 err = biospci_read_config(locator, 0, 2, &devid);
292 /* We have the device ID, create a PnP object and save everything */
293 biospci_addinfo(devid, pc, psc, ppi);
301 biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi)
307 /* build the description */
309 if (ppi->pi_name != NULL) {
310 strcat(desc, ppi->pi_name);
313 if (psc->ps_name != NULL) {
314 strcat(desc, psc->ps_name);
317 if (pc->pc_name != NULL)
318 strcat(desc, pc->pc_name);
320 pi = pnp_allocinfo();
321 pi->pi_desc = strdup(desc);
322 sprintf(desc,"0x%08x", devid);
323 pnp_addident(pi, desc);
328 biospci_find_devclass(uint32_t class, int index, uint32_t *locator)
332 v86.eax = FIND_PCI_CLASS_CODE;
338 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
346 biospci_find_device(uint32_t devid, int index, uint32_t *locator)
350 v86.eax = FIND_PCI_DEVICE;
351 v86.edx = devid & 0xffff; /* EDX - Vendor ID */
352 v86.ecx = (devid >> 16) & 0xffff; /* ECX - Device ID */
357 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
364 * Configuration space access methods.
365 * width = 0(byte), 1(word) or 2(dword).
368 biospci_write_config(uint32_t locator, int offset, int width, uint32_t val)
372 v86.eax = WRITE_CONFIG_BYTE + width;
379 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
386 biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val)
390 v86.eax = READ_CONFIG_BYTE + width;
396 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
404 biospci_locator(int8_t bus, uint8_t device, uint8_t function)
407 return ((bus << 8) | ((device & 0x1f) << 3) | (function & 0x7));
411 * Counts the number of instances of devid we have in the system, as least as
412 * far as the PCI BIOS is able to tell.
415 biospci_count_device_type(uint32_t devid)
419 for (i = 0; 1; i++) {
422 v86.eax = FIND_PCI_DEVICE;
423 v86.edx = devid & 0xffff; /* EDX - Vendor ID */
424 v86.ecx = (devid >> 16) & 0xffff; /* ECX - Device ID */
427 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
436 * pcibios-device-count (devid -- count)
438 * Returns the PCI BIOS' count of how many devices matching devid are in the system.
439 * devid is the 32-bit vendor + device.
442 ficlPciBiosCountDevices(FICL_VM *pVM)
447 devid = stackPopINT(pVM->pStack);
449 i = biospci_count_device_type(devid);
451 stackPushINT(pVM->pStack, i);
455 * pcibios-write-config (locator offset width value -- )
457 * Writes the specified config register.
458 * Locator is bus << 8 | device << 3 | fuction
459 * offset is the pci config register
460 * width is 0 for byte, 1 for word, 2 for dword
461 * value is the value to write
464 ficlPciBiosWriteConfig(FICL_VM *pVM)
466 uint32_t value, width, offset, locator;
468 value = stackPopINT(pVM->pStack);
469 width = stackPopINT(pVM->pStack);
470 offset = stackPopINT(pVM->pStack);
471 locator = stackPopINT(pVM->pStack);
473 biospci_write_config(locator, offset, width, value);
477 * pcibios-read-config (locator offset width -- value)
479 * Reads the specified config register.
480 * Locator is bus << 8 | device << 3 | fuction
481 * offset is the pci config register
482 * width is 0 for byte, 1 for word, 2 for dword
483 * value is the value to read from the register
486 ficlPciBiosReadConfig(FICL_VM *pVM)
488 uint32_t value, width, offset, locator;
490 width = stackPopINT(pVM->pStack);
491 offset = stackPopINT(pVM->pStack);
492 locator = stackPopINT(pVM->pStack);
494 biospci_read_config(locator, offset, width, &value);
496 stackPushINT(pVM->pStack, value);
500 * pcibios-find-devclass (class index -- locator)
502 * Finds the index'th instance of class in the pci tree.
503 * must be an exact match.
504 * class is the class to search for.
505 * index 0..N (set to 0, increment until error)
507 * Locator is bus << 8 | device << 3 | fuction (or -1 on error)
510 ficlPciBiosFindDevclass(FICL_VM *pVM)
512 uint32_t index, class, locator;
514 index = stackPopINT(pVM->pStack);
515 class = stackPopINT(pVM->pStack);
517 if (biospci_find_devclass(class, index, &locator))
518 locator = 0xffffffff;
520 stackPushINT(pVM->pStack, locator);
524 * pcibios-find-device(devid index -- locator)
526 * Finds the index'th instance of devid in the pci tree.
527 * must be an exact match.
528 * class is the class to search for.
529 * index 0..N (set to 0, increment until error)
531 * Locator is bus << 8 | device << 3 | fuction (or -1 on error)
534 ficlPciBiosFindDevice(FICL_VM *pVM)
536 uint32_t index, devid, locator;
538 index = stackPopINT(pVM->pStack);
539 devid = stackPopINT(pVM->pStack);
541 if (biospci_find_device(devid, index, &locator))
542 locator = 0xffffffff;
544 stackPushINT(pVM->pStack, locator);
548 * pcibios-find-device(bus device function -- locator)
550 * converts bus, device, function to locator.
552 * Locator is bus << 8 | device << 3 | fuction
555 ficlPciBiosLocator(FICL_VM *pVM)
557 uint32_t bus, device, function, locator;
559 function = stackPopINT(pVM->pStack);
560 device = stackPopINT(pVM->pStack);
561 bus = stackPopINT(pVM->pStack);
563 locator = biospci_locator(bus, device, function);
565 stackPushINT(pVM->pStack, locator);
569 * Glue function to add the appropriate forth words to access pci bios
572 static void ficlCompilePciBios(FICL_SYSTEM *pSys)
574 FICL_DICT *dp = pSys->dp;
577 dictAppendWord(dp, "pcibios-device-count", ficlPciBiosCountDevices, FW_DEFAULT);
578 dictAppendWord(dp, "pcibios-read-config", ficlPciBiosReadConfig, FW_DEFAULT);
579 dictAppendWord(dp, "pcibios-write-config", ficlPciBiosWriteConfig, FW_DEFAULT);
580 dictAppendWord(dp, "pcibios-find-devclass", ficlPciBiosFindDevclass, FW_DEFAULT);
581 dictAppendWord(dp, "pcibios-find-device", ficlPciBiosFindDevice, FW_DEFAULT);
582 dictAppendWord(dp, "pcibios-locator", ficlPciBiosLocator, FW_DEFAULT);
584 ficlSetEnv(pSys, "pcibios-version", biospci_version);
587 FICL_COMPILE_SET(ficlCompilePciBios);