xref: /illumos-gate/usr/src/boot/i386/libi386/biospci.c (revision 22028508)
1fe20e172SToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3eee59048SToomas Soome  * Copyright (c) 2016 Netflix, Inc
4199767f8SToomas Soome  * All rights reserved.
5199767f8SToomas Soome  *
6199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
7199767f8SToomas Soome  * modification, are permitted provided that the following conditions
8199767f8SToomas Soome  * are met:
9199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
10199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
11199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
12199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
13199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
14199767f8SToomas Soome  *
15199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25199767f8SToomas Soome  * SUCH DAMAGE.
26199767f8SToomas Soome  */
27199767f8SToomas Soome 
28199767f8SToomas Soome #include <sys/cdefs.h>
29199767f8SToomas Soome 
30199767f8SToomas Soome /*
31199767f8SToomas Soome  * PnP enumerator using the PCI BIOS.
32199767f8SToomas Soome  */
33199767f8SToomas Soome 
34199767f8SToomas Soome #include <stand.h>
35199767f8SToomas Soome #include <machine/stdarg.h>
36a3907569SToomas Soome #include <machine/cpufunc.h>
37199767f8SToomas Soome #include <bootstrap.h>
38199767f8SToomas Soome #include <isapnp.h>
39199767f8SToomas Soome #include <btxv86.h>
40199767f8SToomas Soome #include "libi386.h"
41eee59048SToomas Soome #include "ficl.h"
42199767f8SToomas Soome 
43199767f8SToomas Soome /*
44199767f8SToomas Soome  * Stupid PCI BIOS interface doesn't let you simply enumerate everything
45199767f8SToomas Soome  * that's there, instead you have to ask it if it has something.
46199767f8SToomas Soome  *
47199767f8SToomas Soome  * So we have to scan by class code, subclass code and sometimes programming
48199767f8SToomas Soome  * interface.
49199767f8SToomas Soome  */
50199767f8SToomas Soome 
51fe20e172SToomas Soome struct pci_progif
52199767f8SToomas Soome {
53fe20e172SToomas Soome 	int		pi_code;
54fe20e172SToomas Soome 	const char	*pi_name;
55199767f8SToomas Soome };
56199767f8SToomas Soome 
57199767f8SToomas Soome static struct pci_progif progif_null[] = {
58fe20e172SToomas Soome 	{0x0,	NULL},
59fe20e172SToomas Soome 	{-1,	NULL}
60199767f8SToomas Soome };
61199767f8SToomas Soome 
62199767f8SToomas Soome static struct pci_progif progif_display[] = {
63fe20e172SToomas Soome 	{0x0,	"VGA"},
64fe20e172SToomas Soome 	{0x1,	"8514"},
65fe20e172SToomas Soome 	{-1,	NULL}
66199767f8SToomas Soome };
67199767f8SToomas Soome 
68199767f8SToomas Soome static struct pci_progif progif_ide[] = {
69fe20e172SToomas Soome 	{0x00,	NULL},
70fe20e172SToomas Soome 	{0x01,	NULL},
71fe20e172SToomas Soome 	{0x02,	NULL},
72fe20e172SToomas Soome 	{0x03,	NULL},
73fe20e172SToomas Soome 	{0x04,	NULL},
74fe20e172SToomas Soome 	{0x05,	NULL},
75fe20e172SToomas Soome 	{0x06,	NULL},
76fe20e172SToomas Soome 	{0x07,	NULL},
77fe20e172SToomas Soome 	{0x08,	NULL},
78fe20e172SToomas Soome 	{0x09,	NULL},
79fe20e172SToomas Soome 	{0x0a,	NULL},
80fe20e172SToomas Soome 	{0x0b,	NULL},
81fe20e172SToomas Soome 	{0x0c,	NULL},
82fe20e172SToomas Soome 	{0x0d,	NULL},
83fe20e172SToomas Soome 	{0x0e,	NULL},
84fe20e172SToomas Soome 	{0x0f,	NULL},
85fe20e172SToomas Soome 	{0x80,	NULL},
86fe20e172SToomas Soome 	{0x81,	NULL},
87fe20e172SToomas Soome 	{0x82,	NULL},
88fe20e172SToomas Soome 	{0x83,	NULL},
89fe20e172SToomas Soome 	{0x84,	NULL},
90fe20e172SToomas Soome 	{0x85,	NULL},
91fe20e172SToomas Soome 	{0x86,	NULL},
92fe20e172SToomas Soome 	{0x87,	NULL},
93fe20e172SToomas Soome 	{0x88,	NULL},
94fe20e172SToomas Soome 	{0x89,	NULL},
95fe20e172SToomas Soome 	{0x8a,	NULL},
96fe20e172SToomas Soome 	{0x8b,	NULL},
97fe20e172SToomas Soome 	{0x8c,	NULL},
98fe20e172SToomas Soome 	{0x8d,	NULL},
99fe20e172SToomas Soome 	{0x8e,	NULL},
100fe20e172SToomas Soome 	{0x8f,	NULL},
101fe20e172SToomas Soome 	{-1,	NULL}
102199767f8SToomas Soome };
103199767f8SToomas Soome 
104199767f8SToomas Soome static struct pci_progif progif_serial[] = {
105fe20e172SToomas Soome 	{0x0,	"8250"},
106fe20e172SToomas Soome 	{0x1,	"16450"},
107fe20e172SToomas Soome 	{0x2,	"16550"},
108fe20e172SToomas Soome 	{-1,	NULL}
109199767f8SToomas Soome };
110199767f8SToomas Soome 
111199767f8SToomas Soome static struct pci_progif progif_parallel[] = {
112fe20e172SToomas Soome 	{0x0,	"Standard"},
113fe20e172SToomas Soome 	{0x1,	"Bidirectional"},
114fe20e172SToomas Soome 	{0x2,	"ECP"},
115fe20e172SToomas Soome 	{-1,	NULL}
116199767f8SToomas Soome };
117199767f8SToomas Soome 
118199767f8SToomas Soome static struct pci_progif progif_firewire[] = {
119fe20e172SToomas Soome 	{0x10,	"OHCI"},
120fe20e172SToomas Soome 	{-1,	NULL}
121199767f8SToomas Soome };
122199767f8SToomas Soome 
123fe20e172SToomas Soome struct pci_subclass
124199767f8SToomas Soome {
125fe20e172SToomas Soome 	int			ps_subclass;
126fe20e172SToomas Soome 	const char		*ps_name;
127fe20e172SToomas Soome 	/* if set, use for programming interface value(s) */
128fe20e172SToomas Soome 	struct pci_progif	*ps_progif;
129199767f8SToomas Soome };
130199767f8SToomas Soome 
131199767f8SToomas Soome static struct pci_subclass subclass_old[] = {
132fe20e172SToomas Soome 	{0x0,	"Old non-VGA",		progif_null},
133fe20e172SToomas Soome 	{0x1,	"Old VGA",		progif_null},
134fe20e172SToomas Soome 	{-1,	NULL,			NULL}
135199767f8SToomas Soome };
136199767f8SToomas Soome 
137199767f8SToomas Soome static struct pci_subclass subclass_mass[] = {
138fe20e172SToomas Soome 	{0x0,	"SCSI",			progif_null},
139fe20e172SToomas Soome 	{0x1,	"IDE",			progif_ide},
140fe20e172SToomas Soome 	{0x2,	"Floppy disk",		progif_null},
141fe20e172SToomas Soome 	{0x3,	"IPI",			progif_null},
142fe20e172SToomas Soome 	{0x4,	"RAID",			progif_null},
143fe20e172SToomas Soome 	{0x80,	"mass storage",		progif_null},
144fe20e172SToomas Soome 	{-1,	NULL,			NULL}
145199767f8SToomas Soome };
146199767f8SToomas Soome 
147199767f8SToomas Soome static struct pci_subclass subclass_net[] = {
148fe20e172SToomas Soome 	{0x0,	"Ethernet",		progif_null},
149fe20e172SToomas Soome 	{0x1,	"Token ring",		progif_null},
150fe20e172SToomas Soome 	{0x2,	"FDDI",			progif_null},
151fe20e172SToomas Soome 	{0x3,	"ATM",			progif_null},
152fe20e172SToomas Soome 	{0x80,	"network",		progif_null},
153fe20e172SToomas Soome 	{-1,	NULL,			NULL}
154199767f8SToomas Soome };
155199767f8SToomas Soome 
156199767f8SToomas Soome static struct pci_subclass subclass_display[] = {
157fe20e172SToomas Soome 	{0x0,	NULL,			progif_display},
158fe20e172SToomas Soome 	{0x1,	"XGA",			progif_null},
159fe20e172SToomas Soome 	{0x80,	"other",		progif_null},
160fe20e172SToomas Soome 	{-1,	NULL,			NULL}
161199767f8SToomas Soome };
162199767f8SToomas Soome 
163199767f8SToomas Soome static struct pci_subclass subclass_comms[] = {
164fe20e172SToomas Soome 	{0x0,	"serial",		progif_serial},
165fe20e172SToomas Soome 	{0x1,	"parallel",		progif_parallel},
166fe20e172SToomas Soome 	{0x80,	"communications",	progif_null},
167fe20e172SToomas Soome 	{-1,	NULL,			NULL}
168199767f8SToomas Soome };
169199767f8SToomas Soome 
170199767f8SToomas Soome static struct pci_subclass subclass_serial[] = {
171fe20e172SToomas Soome 	{0x0,	"FireWire",		progif_firewire},
172fe20e172SToomas Soome 	{0x1,	"ACCESS.bus",		progif_null},
173fe20e172SToomas Soome 	{0x2,	"SSA",			progif_null},
174fe20e172SToomas Soome 	{0x3,	"USB",			progif_null},
175fe20e172SToomas Soome 	{0x4,	"Fibrechannel",		progif_null},
176fe20e172SToomas Soome 	{-1,	NULL,			NULL}
177199767f8SToomas Soome };
178199767f8SToomas Soome 
179199767f8SToomas Soome static struct pci_class
180199767f8SToomas Soome {
181fe20e172SToomas Soome 	int			pc_class;
182fe20e172SToomas Soome 	const char		*pc_name;
183fe20e172SToomas Soome 	struct pci_subclass	*pc_subclass;
184199767f8SToomas Soome } pci_classes[] = {
185fe20e172SToomas Soome 	{0x0,	"device",	subclass_old},
186fe20e172SToomas Soome 	{0x1,	"controller",	subclass_mass},
187fe20e172SToomas Soome 	{0x2,	"controller",	subclass_net},
188fe20e172SToomas Soome 	{0x3,	"display",	subclass_display},
189fe20e172SToomas Soome 	{0x7,	"controller",	subclass_comms},
190fe20e172SToomas Soome 	{0xc,	"controller",	subclass_serial},
191fe20e172SToomas Soome 	{-1,	NULL,		NULL}
192199767f8SToomas Soome };
193199767f8SToomas Soome 
194199767f8SToomas Soome 
195199767f8SToomas Soome static void	biospci_enumerate(void);
196fe20e172SToomas Soome static void	biospci_addinfo(int, struct pci_class *, struct pci_subclass *,
197fe20e172SToomas Soome     struct pci_progif *);
198199767f8SToomas Soome 
199199767f8SToomas Soome struct pnphandler biospcihandler =
200199767f8SToomas Soome {
201fe20e172SToomas Soome 	"PCI BIOS",
202fe20e172SToomas Soome 	biospci_enumerate
203199767f8SToomas Soome };
204199767f8SToomas Soome 
205fe20e172SToomas Soome #define	PCI_BIOS_PRESENT	0xb101
206fe20e172SToomas Soome #define	FIND_PCI_DEVICE		0xb102
207fe20e172SToomas Soome #define	FIND_PCI_CLASS_CODE	0xb103
208fe20e172SToomas Soome #define	GENERATE_SPECIAL_CYCLE	0xb106
209fe20e172SToomas Soome #define	READ_CONFIG_BYTE	0xb108
210fe20e172SToomas Soome #define	READ_CONFIG_WORD	0xb109
211fe20e172SToomas Soome #define	READ_CONFIG_DWORD	0xb10a
212fe20e172SToomas Soome #define	WRITE_CONFIG_BYTE	0xb10b
213fe20e172SToomas Soome #define	WRITE_CONFIG_WORD	0xb10c
214fe20e172SToomas Soome #define	WRITE_CONFIG_DWORD	0xb10d
215fe20e172SToomas Soome #define	GET_IRQ_ROUTING_OPTIONS	0xb10e
216fe20e172SToomas Soome #define	SET_PCI_IRQ		0xb10f
217199767f8SToomas Soome 
218fe20e172SToomas Soome #define	PCI_INT			0x1a
219199767f8SToomas Soome 
220fe20e172SToomas Soome #define	PCI_SIGNATURE		0x20494350	/* AKA "PCI " */
221199767f8SToomas Soome 
222199767f8SToomas Soome void
biospci_detect(void)223199767f8SToomas Soome biospci_detect(void)
224199767f8SToomas Soome {
225fe20e172SToomas Soome 	uint16_t version, hwcap, maxbus;
226fe20e172SToomas Soome 	char buf[24];
227fe20e172SToomas Soome 
228fe20e172SToomas Soome 	/* Find the PCI BIOS */
229fe20e172SToomas Soome 	v86.ctl = V86_FLAGS;
230fe20e172SToomas Soome 	v86.addr = PCI_INT;
231fe20e172SToomas Soome 	v86.eax = PCI_BIOS_PRESENT;
232fe20e172SToomas Soome 	v86.edi = 0x0;
233fe20e172SToomas Soome 	v86int();
234fe20e172SToomas Soome 
235fe20e172SToomas Soome 	/* Check for OK response */
236fe20e172SToomas Soome 	if (V86_CY(v86.efl) || ((v86.eax & 0xff00) != 0) ||
237fe20e172SToomas Soome 	    (v86.edx != PCI_SIGNATURE))
238fe20e172SToomas Soome 		return;
239fe20e172SToomas Soome 
240fe20e172SToomas Soome 	version = v86.ebx & 0xffff;
241fe20e172SToomas Soome 	hwcap = v86.eax & 0xff;
242fe20e172SToomas Soome 	maxbus = v86.ecx & 0xff;
243199767f8SToomas Soome #if 0
244fe20e172SToomas Soome 	printf("PCI BIOS %d.%d%s%s maxbus %d\n",
245fe20e172SToomas Soome 	    bcd2bin((version >> 8) & 0xf), bcd2bin(version & 0xf),
246fe20e172SToomas Soome 	    (hwcap & 1) ? " config1" : "", (hwcap & 2) ? " config2" : "",
247fe20e172SToomas Soome 	    maxbus);
248199767f8SToomas Soome #endif
249fe20e172SToomas Soome 	sprintf(buf, "%d", bcd2bin((version >> 8) & 0xf));
250fe20e172SToomas Soome 	setenv("pcibios.major", buf, 1);
251fe20e172SToomas Soome 	sprintf(buf, "%d", bcd2bin(version & 0xf));
252fe20e172SToomas Soome 	setenv("pcibios.minor", buf, 1);
253fe20e172SToomas Soome 	sprintf(buf, "%d", !!(hwcap & 1));
254fe20e172SToomas Soome 	setenv("pcibios.config1", buf, 1);
255fe20e172SToomas Soome 	sprintf(buf, "%d", !!(hwcap & 2));
256fe20e172SToomas Soome 	setenv("pcibios.config2", buf, 1);
257fe20e172SToomas Soome 	sprintf(buf, "%d", maxbus);
258fe20e172SToomas Soome 	setenv("pcibios.maxbus", buf, 1);
259199767f8SToomas Soome }
260199767f8SToomas Soome 
261199767f8SToomas Soome static void
biospci_enumerate(void)262199767f8SToomas Soome biospci_enumerate(void)
263199767f8SToomas Soome {
264fe20e172SToomas Soome 	int			device_index, err;
265fe20e172SToomas Soome 	uint32_t		locator, devid;
266fe20e172SToomas Soome 	struct pci_class	*pc;
267fe20e172SToomas Soome 	struct pci_subclass	*psc;
268fe20e172SToomas Soome 	struct pci_progif	*ppi;
269fe20e172SToomas Soome 
270fe20e172SToomas Soome 	/* Iterate over known classes */
271fe20e172SToomas Soome 	for (pc = pci_classes; pc->pc_class >= 0; pc++) {
272fe20e172SToomas Soome 		/* Iterate over subclasses */
273fe20e172SToomas Soome 		for (psc = pc->pc_subclass; psc->ps_subclass >= 0; psc++) {
274fe20e172SToomas Soome 			/* Iterate over programming interfaces */
275fe20e172SToomas Soome 			for (ppi = psc->ps_progif; ppi->pi_code >= 0; ppi++) {
276fe20e172SToomas Soome 
277fe20e172SToomas Soome 				/* Scan for matches */
278fe20e172SToomas Soome 				for (device_index = 0; ; device_index++) {
279fe20e172SToomas Soome 					/* Look for a match */
280fe20e172SToomas Soome 					err = biospci_find_devclass(
281fe20e172SToomas Soome 					    (pc->pc_class << 16) +
282fe20e172SToomas Soome 					    (psc->ps_subclass << 8) +
283fe20e172SToomas Soome 					    ppi->pi_code,
284fe20e172SToomas Soome 					    device_index, &locator);
285fe20e172SToomas Soome 					if (err != 0)
286fe20e172SToomas Soome 						break;
287fe20e172SToomas Soome 
288fe20e172SToomas Soome 					/*
289fe20e172SToomas Soome 					 * Read the device identifier from
290fe20e172SToomas Soome 					 * the nominated device
291fe20e172SToomas Soome 					 */
292fe20e172SToomas Soome 					err = biospci_read_config(locator,
293fe20e172SToomas Soome 					    0, 2, &devid);
294fe20e172SToomas Soome 					if (err != 0)
295fe20e172SToomas Soome 						break;
296fe20e172SToomas Soome 
297fe20e172SToomas Soome 					/*
298fe20e172SToomas Soome 					 * We have the device ID, create a PnP
299fe20e172SToomas Soome 					 * object and save everything
300fe20e172SToomas Soome 					 */
301fe20e172SToomas Soome 					biospci_addinfo(devid, pc, psc, ppi);
302fe20e172SToomas Soome 				}
303fe20e172SToomas Soome 			}
304199767f8SToomas Soome 		}
305199767f8SToomas Soome 	}
306199767f8SToomas Soome }
307199767f8SToomas Soome 
308199767f8SToomas Soome static void
biospci_addinfo(int devid,struct pci_class * pc,struct pci_subclass * psc,struct pci_progif * ppi)309fe20e172SToomas Soome biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc,
310fe20e172SToomas Soome     struct pci_progif *ppi)
311199767f8SToomas Soome {
312fe20e172SToomas Soome 	struct pnpinfo	*pi;
313fe20e172SToomas Soome 	char		desc[80];
314fe20e172SToomas Soome 
315fe20e172SToomas Soome 
316fe20e172SToomas Soome 	/* build the description */
317fe20e172SToomas Soome 	desc[0] = 0;
318fe20e172SToomas Soome 	if (ppi->pi_name != NULL) {
319fe20e172SToomas Soome 		strcat(desc, ppi->pi_name);
320fe20e172SToomas Soome 		strcat(desc, " ");
321fe20e172SToomas Soome 	}
322fe20e172SToomas Soome 	if (psc->ps_name != NULL) {
323fe20e172SToomas Soome 		strcat(desc, psc->ps_name);
324fe20e172SToomas Soome 		strcat(desc, " ");
325fe20e172SToomas Soome 	}
326fe20e172SToomas Soome 	if (pc->pc_name != NULL)
327fe20e172SToomas Soome 		strcat(desc, pc->pc_name);
328fe20e172SToomas Soome 
329fe20e172SToomas Soome 	pi = pnp_allocinfo();
330fe20e172SToomas Soome 	pi->pi_desc = strdup(desc);
331fe20e172SToomas Soome 	sprintf(desc, "0x%08x", devid);
332fe20e172SToomas Soome 	pnp_addident(pi, desc);
333fe20e172SToomas Soome 	pnp_addinfo(pi);
334199767f8SToomas Soome }
335199767f8SToomas Soome 
336199767f8SToomas Soome int
biospci_find_devclass(uint32_t class,int index,uint32_t * locator)337199767f8SToomas Soome biospci_find_devclass(uint32_t class, int index, uint32_t *locator)
338199767f8SToomas Soome {
339199767f8SToomas Soome 	v86.ctl = V86_FLAGS;
340199767f8SToomas Soome 	v86.addr = PCI_INT;
341199767f8SToomas Soome 	v86.eax = FIND_PCI_CLASS_CODE;
342199767f8SToomas Soome 	v86.ecx = class;
343199767f8SToomas Soome 	v86.esi = index;
344199767f8SToomas Soome 	v86int();
345199767f8SToomas Soome 
346fe20e172SToomas Soome 	/* error */
347199767f8SToomas Soome 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
348199767f8SToomas Soome 		return (-1);
349199767f8SToomas Soome 
350199767f8SToomas Soome 	*locator = v86.ebx;
351199767f8SToomas Soome 	return (0);
352199767f8SToomas Soome }
353199767f8SToomas Soome 
354eee59048SToomas Soome static int
biospci_find_device(uint32_t devid,int index,uint32_t * locator)355199767f8SToomas Soome biospci_find_device(uint32_t devid, int index, uint32_t *locator)
356199767f8SToomas Soome {
357199767f8SToomas Soome 	v86.ctl = V86_FLAGS;
358199767f8SToomas Soome 	v86.addr = PCI_INT;
359199767f8SToomas Soome 	v86.eax = FIND_PCI_DEVICE;
360199767f8SToomas Soome 	v86.edx = devid & 0xffff;		/* EDX - Vendor ID */
361199767f8SToomas Soome 	v86.ecx = (devid >> 16) & 0xffff;	/* ECX - Device ID */
362199767f8SToomas Soome 	v86.esi = index;
363199767f8SToomas Soome 	v86int();
364199767f8SToomas Soome 
365fe20e172SToomas Soome 	/* error */
366199767f8SToomas Soome 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
367199767f8SToomas Soome 		return (-1);
368199767f8SToomas Soome 
369199767f8SToomas Soome 	*locator = v86.ebx;
370199767f8SToomas Soome 	return (0);
371199767f8SToomas Soome }
372199767f8SToomas Soome /*
373199767f8SToomas Soome  * Configuration space access methods.
374199767f8SToomas Soome  * width = 0(byte), 1(word) or 2(dword).
375199767f8SToomas Soome  */
376199767f8SToomas Soome int
biospci_write_config(uint32_t locator,int offset,int width,uint32_t val)377199767f8SToomas Soome biospci_write_config(uint32_t locator, int offset, int width, uint32_t val)
378199767f8SToomas Soome {
379199767f8SToomas Soome 	v86.ctl = V86_FLAGS;
380199767f8SToomas Soome 	v86.addr = PCI_INT;
381199767f8SToomas Soome 	v86.eax = WRITE_CONFIG_BYTE + width;
382199767f8SToomas Soome 	v86.ebx = locator;
383199767f8SToomas Soome 	v86.edi = offset;
384199767f8SToomas Soome 	v86.ecx = val;
385199767f8SToomas Soome 	v86int();
386199767f8SToomas Soome 
387fe20e172SToomas Soome 	/* error */
388199767f8SToomas Soome 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
389199767f8SToomas Soome 		return (-1);
390199767f8SToomas Soome 
391fe20e172SToomas Soome 	return (0);
392199767f8SToomas Soome }
393199767f8SToomas Soome 
394199767f8SToomas Soome int
biospci_read_config(uint32_t locator,int offset,int width,uint32_t * val)395199767f8SToomas Soome biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val)
396199767f8SToomas Soome {
397199767f8SToomas Soome 	v86.ctl = V86_FLAGS;
398199767f8SToomas Soome 	v86.addr = PCI_INT;
399199767f8SToomas Soome 	v86.eax = READ_CONFIG_BYTE + width;
400199767f8SToomas Soome 	v86.ebx = locator;
401199767f8SToomas Soome 	v86.edi = offset;
402199767f8SToomas Soome 	v86int();
403199767f8SToomas Soome 
404fe20e172SToomas Soome 	/* error */
405199767f8SToomas Soome 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
406199767f8SToomas Soome 		return (-1);
407199767f8SToomas Soome 
408199767f8SToomas Soome 	*val = v86.ecx;
409199767f8SToomas Soome 	return (0);
410199767f8SToomas Soome }
411199767f8SToomas Soome 
412199767f8SToomas Soome uint32_t
biospci_locator(int8_t bus,uint8_t device,uint8_t function)413199767f8SToomas Soome biospci_locator(int8_t bus, uint8_t device, uint8_t function)
414199767f8SToomas Soome {
415199767f8SToomas Soome 
416199767f8SToomas Soome 	return ((bus << 8) | ((device & 0x1f) << 3) | (function & 0x7));
417199767f8SToomas Soome }
418199767f8SToomas Soome 
419199767f8SToomas Soome /*
420199767f8SToomas Soome  * Counts the number of instances of devid we have in the system, as least as
421199767f8SToomas Soome  * far as the PCI BIOS is able to tell.
422199767f8SToomas Soome  */
423eee59048SToomas Soome static int
biospci_count_device_type(uint32_t devid)424199767f8SToomas Soome biospci_count_device_type(uint32_t devid)
425199767f8SToomas Soome {
426199767f8SToomas Soome 	int i;
427199767f8SToomas Soome 
428199767f8SToomas Soome 	for (i = 0; 1; i++) {
429199767f8SToomas Soome 		v86.ctl = V86_FLAGS;
430199767f8SToomas Soome 		v86.addr = PCI_INT;
431199767f8SToomas Soome 		v86.eax = FIND_PCI_DEVICE;
432199767f8SToomas Soome 		v86.edx = devid & 0xffff;		/* EDX - Vendor ID */
433199767f8SToomas Soome 		v86.ecx = (devid >> 16) & 0xffff;	/* ECX - Device ID */
434199767f8SToomas Soome 		v86.esi = i;
435199767f8SToomas Soome 		v86int();
436199767f8SToomas Soome 		if (V86_CY(v86.efl) || (v86.eax & 0xff00))
437199767f8SToomas Soome 			break;
438199767f8SToomas Soome 
439199767f8SToomas Soome 	}
440fe20e172SToomas Soome 	return (i);
441199767f8SToomas Soome }
442eee59048SToomas Soome 
443eee59048SToomas Soome /*
444eee59048SToomas Soome  * pcibios-device-count (devid -- count)
445eee59048SToomas Soome  *
446eee59048SToomas Soome  * Returns the PCI BIOS' count of how many devices matching devid are
447eee59048SToomas Soome  * in the system. devid is the 32-bit vendor + device.
448eee59048SToomas Soome  */
449eee59048SToomas Soome static void
ficlPciBiosCountDevices(ficlVm * pVM)450eee59048SToomas Soome ficlPciBiosCountDevices(ficlVm *pVM)
451eee59048SToomas Soome {
452eee59048SToomas Soome 	uint32_t devid;
453eee59048SToomas Soome 	int i;
454eee59048SToomas Soome 
455eee59048SToomas Soome 	FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 1, 1);
456eee59048SToomas Soome 
457eee59048SToomas Soome 	devid = ficlStackPopInteger(ficlVmGetDataStack(pVM));
458eee59048SToomas Soome 
459eee59048SToomas Soome 	i = biospci_count_device_type(devid);
460eee59048SToomas Soome 
461eee59048SToomas Soome 	ficlStackPushInteger(ficlVmGetDataStack(pVM), i);
462eee59048SToomas Soome }
463eee59048SToomas Soome 
464eee59048SToomas Soome /*
465eee59048SToomas Soome  * pcibios-write-config (locator offset width value -- )
466eee59048SToomas Soome  *
467eee59048SToomas Soome  * Writes the specified config register.
468eee59048SToomas Soome  * Locator is bus << 8 | device << 3 | fuction
469eee59048SToomas Soome  * offset is the pci config register
470eee59048SToomas Soome  * width is 0 for byte, 1 for word, 2 for dword
471eee59048SToomas Soome  * value is the value to write
472eee59048SToomas Soome  */
473eee59048SToomas Soome static void
ficlPciBiosWriteConfig(ficlVm * pVM)474eee59048SToomas Soome ficlPciBiosWriteConfig(ficlVm *pVM)
475eee59048SToomas Soome {
476eee59048SToomas Soome 	uint32_t value, width, offset, locator;
477eee59048SToomas Soome 
478eee59048SToomas Soome 	FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 4, 0);
479eee59048SToomas Soome 
480eee59048SToomas Soome 	value = ficlStackPopInteger(ficlVmGetDataStack(pVM));
481eee59048SToomas Soome 	width = ficlStackPopInteger(ficlVmGetDataStack(pVM));
482eee59048SToomas Soome 	offset = ficlStackPopInteger(ficlVmGetDataStack(pVM));
483eee59048SToomas Soome 	locator = ficlStackPopInteger(ficlVmGetDataStack(pVM));
484eee59048SToomas Soome 
485eee59048SToomas Soome 	biospci_write_config(locator, offset, width, value);
486eee59048SToomas Soome }
487eee59048SToomas Soome 
488eee59048SToomas Soome /*
489eee59048SToomas Soome  * pcibios-read-config (locator offset width -- value)
490eee59048SToomas Soome  *
491eee59048SToomas Soome  * Reads the specified config register.
492eee59048SToomas Soome  * Locator is bus << 8 | device << 3 | fuction
493eee59048SToomas Soome  * offset is the pci config register
494eee59048SToomas Soome  * width is 0 for byte, 1 for word, 2 for dword
495eee59048SToomas Soome  * value is the value to read from the register
496eee59048SToomas Soome  */
497eee59048SToomas Soome static void
ficlPciBiosReadConfig(ficlVm * pVM)498eee59048SToomas Soome ficlPciBiosReadConfig(ficlVm *pVM)
499eee59048SToomas Soome {
500eee59048SToomas Soome 	uint32_t value, width, offset, locator;
501eee59048SToomas Soome 
502eee59048SToomas Soome 	FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 3, 1);
503eee59048SToomas Soome 
504eee59048SToomas Soome 	width = ficlStackPopInteger(ficlVmGetDataStack(pVM));
505eee59048SToomas Soome 	offset = ficlStackPopInteger(ficlVmGetDataStack(pVM));
506eee59048SToomas Soome 	locator = ficlStackPopInteger(ficlVmGetDataStack(pVM));
507eee59048SToomas Soome 
508fe20e172SToomas Soome 	value = 0;
509fe20e172SToomas Soome 	(void) biospci_read_config(locator, offset, width, &value);
510eee59048SToomas Soome 
511eee59048SToomas Soome 	ficlStackPushInteger(ficlVmGetDataStack(pVM), value);
512eee59048SToomas Soome }
513eee59048SToomas Soome 
514eee59048SToomas Soome /*
515eee59048SToomas Soome  * pcibios-find-devclass (class index -- locator)
516eee59048SToomas Soome  *
517eee59048SToomas Soome  * Finds the index'th instance of class in the pci tree.
518eee59048SToomas Soome  * must be an exact match.
519eee59048SToomas Soome  * class is the class to search for.
520eee59048SToomas Soome  * index 0..N (set to 0, increment until error)
521eee59048SToomas Soome  *
522eee59048SToomas Soome  * Locator is bus << 8 | device << 3 | fuction (or -1 on error)
523eee59048SToomas Soome  */
524eee59048SToomas Soome static void
ficlPciBiosFindDevclass(ficlVm * pVM)525eee59048SToomas Soome ficlPciBiosFindDevclass(ficlVm *pVM)
526eee59048SToomas Soome {
527eee59048SToomas Soome 	uint32_t index, class, locator;
528eee59048SToomas Soome 
529eee59048SToomas Soome 	FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 2, 1);
530eee59048SToomas Soome 
531eee59048SToomas Soome 	index = ficlStackPopInteger(ficlVmGetDataStack(pVM));
532eee59048SToomas Soome 	class = ficlStackPopInteger(ficlVmGetDataStack(pVM));
533eee59048SToomas Soome 
534eee59048SToomas Soome 	if (biospci_find_devclass(class, index, &locator))
535eee59048SToomas Soome 		locator = 0xffffffff;
536eee59048SToomas Soome 
537eee59048SToomas Soome 	ficlStackPushInteger(ficlVmGetDataStack(pVM), locator);
538eee59048SToomas Soome }
539eee59048SToomas Soome 
540eee59048SToomas Soome /*
541eee59048SToomas Soome  * pcibios-find-device(devid index -- locator)
542eee59048SToomas Soome  *
543eee59048SToomas Soome  * Finds the index'th instance of devid in the pci tree.
544eee59048SToomas Soome  * must be an exact match.
545eee59048SToomas Soome  * class is the class to search for.
546eee59048SToomas Soome  * index 0..N (set to 0, increment until error)
547eee59048SToomas Soome  *
548eee59048SToomas Soome  * Locator is bus << 8 | device << 3 | fuction (or -1 on error)
549eee59048SToomas Soome  */
550eee59048SToomas Soome static void
ficlPciBiosFindDevice(ficlVm * pVM)551eee59048SToomas Soome ficlPciBiosFindDevice(ficlVm *pVM)
552eee59048SToomas Soome {
553eee59048SToomas Soome 	uint32_t index, devid, locator;
554eee59048SToomas Soome 
555eee59048SToomas Soome 	FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 2, 1);
556eee59048SToomas Soome 
557eee59048SToomas Soome 	index = ficlStackPopInteger(ficlVmGetDataStack(pVM));
558eee59048SToomas Soome 	devid = ficlStackPopInteger(ficlVmGetDataStack(pVM));
559eee59048SToomas Soome 
560eee59048SToomas Soome 	if (biospci_find_device(devid, index, &locator))
561eee59048SToomas Soome 		locator = 0xffffffff;
562eee59048SToomas Soome 
563eee59048SToomas Soome 	ficlStackPushInteger(ficlVmGetDataStack(pVM), locator);
564eee59048SToomas Soome }
565eee59048SToomas Soome 
566eee59048SToomas Soome /*
567eee59048SToomas Soome  * pcibios-locator(bus device function -- locator)
568eee59048SToomas Soome  *
569eee59048SToomas Soome  * converts bus, device, function to locator.
570eee59048SToomas Soome  *
571eee59048SToomas Soome  * Locator is bus << 8 | device << 3 | fuction
572eee59048SToomas Soome  */
573eee59048SToomas Soome static void
ficlPciBiosLocator(ficlVm * pVM)574eee59048SToomas Soome ficlPciBiosLocator(ficlVm *pVM)
575eee59048SToomas Soome {
576eee59048SToomas Soome 	uint32_t bus, device, function, locator;
577eee59048SToomas Soome 
578eee59048SToomas Soome 	FICL_STACK_CHECK(ficlVmGetDataStack(pVM), 3, 1);
579eee59048SToomas Soome 
580eee59048SToomas Soome 	function = ficlStackPopInteger(ficlVmGetDataStack(pVM));
581eee59048SToomas Soome 	device = ficlStackPopInteger(ficlVmGetDataStack(pVM));
582eee59048SToomas Soome 	bus = ficlStackPopInteger(ficlVmGetDataStack(pVM));
583eee59048SToomas Soome 
584eee59048SToomas Soome 	locator = biospci_locator(bus, device, function);
585eee59048SToomas Soome 
586eee59048SToomas Soome 	ficlStackPushInteger(ficlVmGetDataStack(pVM), locator);
587eee59048SToomas Soome }
588eee59048SToomas Soome 
589eee59048SToomas Soome /*
590eee59048SToomas Soome  * Glue function to add the appropriate forth words to access pci bios
591eee59048SToomas Soome  * functionality.
592eee59048SToomas Soome  */
593eee59048SToomas Soome static void
ficlCompilePciBios(ficlSystem * pSys)594eee59048SToomas Soome ficlCompilePciBios(ficlSystem *pSys)
595eee59048SToomas Soome {
596eee59048SToomas Soome 	ficlDictionary *dp = ficlSystemGetDictionary(pSys);
597eee59048SToomas Soome 
598eee59048SToomas Soome 	FICL_SYSTEM_ASSERT(pSys, dp);
599eee59048SToomas Soome 
600eee59048SToomas Soome 	ficlDictionarySetPrimitive(dp, "pcibios-device-count",
601eee59048SToomas Soome 	    ficlPciBiosCountDevices, FICL_WORD_DEFAULT);
602eee59048SToomas Soome 	ficlDictionarySetPrimitive(dp, "pcibios-read-config",
603eee59048SToomas Soome 	    ficlPciBiosReadConfig, FICL_WORD_DEFAULT);
604eee59048SToomas Soome 	ficlDictionarySetPrimitive(dp, "pcibios-write-config",
605eee59048SToomas Soome 	    ficlPciBiosWriteConfig, FICL_WORD_DEFAULT);
606eee59048SToomas Soome 	ficlDictionarySetPrimitive(dp, "pcibios-find-devclass",
607eee59048SToomas Soome 	    ficlPciBiosFindDevclass, FICL_WORD_DEFAULT);
608eee59048SToomas Soome 	ficlDictionarySetPrimitive(dp, "pcibios-find-device",
609eee59048SToomas Soome 	    ficlPciBiosFindDevice, FICL_WORD_DEFAULT);
610eee59048SToomas Soome 	ficlDictionarySetPrimitive(dp, "pcibios-locator", ficlPciBiosLocator,
611eee59048SToomas Soome 	    FICL_WORD_DEFAULT);
612eee59048SToomas Soome }
613eee59048SToomas Soome 
614eee59048SToomas Soome FICL_COMPILE_SET(ficlCompilePciBios);
615