xref: /freebsd/stand/i386/libi386/biospci.c (revision 63a938566d524836885917d95bd491aa4400b181)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2016 Netflix, Inc
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  * PnP enumerator using the PCI BIOS.
33  */
34 
35 #include <stand.h>
36 #include <machine/stdarg.h>
37 #include <bootstrap.h>
38 #include <isapnp.h>
39 #include <btxv86.h>
40 #include "libi386.h"
41 
42 /*
43  * Stupid PCI BIOS interface doesn't let you simply enumerate everything
44  * that's there, instead you have to ask it if it has something.
45  *
46  * So we have to scan by class code, subclass code and sometimes programming
47  * interface.
48  */
49 
50 struct pci_progif
51 {
52     int		pi_code;
53     const char	*pi_name;
54 };
55 
56 static struct pci_progif progif_null[] = {
57     {0x0,	NULL},
58     {-1,	NULL}
59 };
60 
61 static struct pci_progif progif_display[] = {
62     {0x0,	"VGA"},
63     {0x1,	"8514"},
64     {-1,	NULL}
65 };
66 
67 static struct pci_progif progif_ide[] = {
68     {0x00,	NULL},
69     {0x01,	NULL},
70     {0x02,	NULL},
71     {0x03,	NULL},
72     {0x04,	NULL},
73     {0x05,	NULL},
74     {0x06,	NULL},
75     {0x07,	NULL},
76     {0x08,	NULL},
77     {0x09,	NULL},
78     {0x0a,	NULL},
79     {0x0b,	NULL},
80     {0x0c,	NULL},
81     {0x0d,	NULL},
82     {0x0e,	NULL},
83     {0x0f,	NULL},
84     {0x80,	NULL},
85     {0x81,	NULL},
86     {0x82,	NULL},
87     {0x83,	NULL},
88     {0x84,	NULL},
89     {0x85,	NULL},
90     {0x86,	NULL},
91     {0x87,	NULL},
92     {0x88,	NULL},
93     {0x89,	NULL},
94     {0x8a,	NULL},
95     {0x8b,	NULL},
96     {0x8c,	NULL},
97     {0x8d,	NULL},
98     {0x8e,	NULL},
99     {0x8f,	NULL},
100     {-1,	NULL}
101 };
102 
103 static struct pci_progif progif_serial[] = {
104     {0x0,	"8250"},
105     {0x1,	"16450"},
106     {0x2,	"16550"},
107     {-1,	NULL}
108 };
109 
110 static struct pci_progif progif_parallel[] = {
111     {0x0,	"Standard"},
112     {0x1,	"Bidirectional"},
113     {0x2,	"ECP"},
114     {-1,	NULL}
115 };
116 
117 static struct pci_progif progif_firewire[] = {
118     {0x10,	"OHCI"},
119     {-1,	NULL}
120 };
121 
122 struct pci_subclass
123 {
124     int			ps_subclass;
125     const char		*ps_name;
126     struct pci_progif	*ps_progif;	/* if set, use for programming interface value(s) */
127 };
128 
129 static struct pci_subclass subclass_old[] = {
130     {0x0,	"Old non-VGA",		progif_null},
131     {0x1,	"Old VGA",		progif_null},
132     {-1,	NULL,			NULL}
133 };
134 
135 static struct pci_subclass subclass_mass[] = {
136     {0x0,	"SCSI",			progif_null},
137     {0x1,	"IDE",			progif_ide},
138     {0x2,	"Floppy disk",		progif_null},
139     {0x3,	"IPI",			progif_null},
140     {0x4,	"RAID",			progif_null},
141     {0x80,	"mass storage",		progif_null},
142     {-1,	NULL,			NULL}
143 };
144 
145 static struct pci_subclass subclass_net[] = {
146     {0x0,	"Ethernet",		progif_null},
147     {0x1,	"Token ring",		progif_null},
148     {0x2,	"FDDI",			progif_null},
149     {0x3,	"ATM",			progif_null},
150     {0x80,	"network",		progif_null},
151     {-1,	NULL,			NULL}
152 };
153 
154 static struct pci_subclass subclass_display[] = {
155     {0x0,	NULL,			progif_display},
156     {0x1,	"XGA",			progif_null},
157     {0x80,	"other",		progif_null},
158     {-1,	NULL,			NULL}
159 };
160 
161 static struct pci_subclass subclass_comms[] = {
162     {0x0,	"serial",		progif_serial},
163     {0x1,	"parallel",		progif_parallel},
164     {0x80,	"communications",	progif_null},
165     {-1,	NULL,			NULL}
166 };
167 
168 static struct pci_subclass subclass_serial[] = {
169     {0x0,	"FireWire",		progif_firewire},
170     {0x1,	"ACCESS.bus",		progif_null},
171     {0x2,	"SSA",			progif_null},
172     {0x3,	"USB",			progif_null},
173     {0x4,	"Fibrechannel",		progif_null},
174     {-1,	NULL,			NULL}
175 };
176 
177 static struct pci_class
178 {
179     int			pc_class;
180     const char		*pc_name;
181     struct pci_subclass	*pc_subclass;
182 } pci_classes[] = {
183     {0x0,	"device",	subclass_old},
184     {0x1,	"controller",	subclass_mass},
185     {0x2,	"controller",	subclass_net},
186     {0x3,	"display",	subclass_display},
187     {0x7,	"controller",	subclass_comms},
188     {0xc,	"controller",	subclass_serial},
189     {-1,	NULL,		NULL}
190 };
191 
192 static void	biospci_enumerate(void);
193 static void	biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi);
194 
195 struct pnphandler biospcihandler =
196 {
197     "PCI BIOS",
198     biospci_enumerate
199 };
200 static int biospci_version;
201 
202 #define PCI_BIOS_PRESENT	0xb101
203 #define FIND_PCI_DEVICE		0xb102
204 #define FIND_PCI_CLASS_CODE	0xb103
205 #define GENERATE_SPECIAL_CYCLE	0xb106
206 #define READ_CONFIG_BYTE	0xb108
207 #define READ_CONFIG_WORD	0xb109
208 #define READ_CONFIG_DWORD	0xb10a
209 #define WRITE_CONFIG_BYTE	0xb10b
210 #define WRITE_CONFIG_WORD	0xb10c
211 #define WRITE_CONFIG_DWORD	0xb10d
212 #define GET_IRQ_ROUTING_OPTIONS	0xb10e
213 #define SET_PCI_IRQ		0xb10f
214 
215 #define PCI_INT			0x1a
216 
217 #define PCI_SIGNATURE		0x20494350	/* AKA "PCI " */
218 
219 void
220 biospci_detect(void)
221 {
222     uint16_t version, hwcap, maxbus;
223     char buf[24];
224 
225     /* Find the PCI BIOS */
226     v86.ctl = V86_FLAGS;
227     v86.addr = PCI_INT;
228     v86.eax = PCI_BIOS_PRESENT;
229     v86.edi = 0x0;
230     v86int();
231 
232     /* Check for OK response */
233     if (V86_CY(v86.efl) || ((v86.eax & 0xff00) != 0) ||
234 	(v86.edx != PCI_SIGNATURE))
235 	return;
236 
237     version = v86.ebx & 0xffff;
238     hwcap = v86.eax & 0xff;
239     maxbus = v86.ecx & 0xff;
240 #if 0
241     printf("PCI BIOS %d.%d%s%s maxbus %d\n",
242 	   bcd2bin((version >> 8) & 0xf), bcd2bin(version & 0xf),
243 	   (hwcap & 1) ? " config1" : "", (hwcap & 2) ? " config2" : "",
244 	   maxbus);
245 #endif
246     sprintf(buf, "%d", bcd2bin((version >> 8) & 0xf));
247     setenv("pcibios.major", buf, 1);
248     sprintf(buf, "%d", bcd2bin(version & 0xf));
249     setenv("pcibios.minor", buf, 1);
250     sprintf(buf, "%d", !!(hwcap & 1));
251     setenv("pcibios.config1", buf, 1);
252     sprintf(buf, "%d", !!(hwcap & 2));
253     setenv("pcibios.config2", buf, 1);
254     sprintf(buf, "%d", maxbus);
255     setenv("pcibios.maxbus", buf, 1);
256     biospci_version = bcd2bin((version >> 8) & 0xf) * 10 + bcd2bin(version & 0xf);
257 }
258 
259 static void
260 biospci_enumerate(void)
261 {
262     int			device_index, err;
263     uint32_t		locator, devid;
264     struct pci_class	*pc;
265     struct pci_subclass *psc;
266     struct pci_progif	*ppi;
267 
268     /* Iterate over known classes */
269     for (pc = pci_classes; pc->pc_class >= 0; pc++) {
270 	/* Iterate over subclasses */
271 	for (psc = pc->pc_subclass; psc->ps_subclass >= 0; psc++) {
272 	    /* Iterate over programming interfaces */
273 	    for (ppi = psc->ps_progif; ppi->pi_code >= 0; ppi++) {
274 
275 		/* Scan for matches */
276 		for (device_index = 0; ; device_index++) {
277 		    /* Look for a match */
278 		    err = biospci_find_devclass((pc->pc_class << 16)
279 			+ (psc->ps_subclass << 8) + ppi->pi_code,
280 			device_index, &locator);
281 		    if (err != 0)
282 			break;
283 
284 		    /* Read the device identifier from the nominated device */
285 		    err = biospci_read_config(locator, 0, BIOSPCI_32BITS, &devid);
286 		    if (err != 0)
287 			break;
288 
289 		    /* We have the device ID, create a PnP object and save everything */
290 		    biospci_addinfo(devid, pc, psc, ppi);
291 		}
292 	    }
293 	}
294     }
295 }
296 
297 static void
298 biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi)
299 {
300     struct pnpinfo	*pi;
301     char		desc[80];
302 
303 
304     /* build the description */
305     desc[0] = 0;
306     if (ppi->pi_name != NULL) {
307 	strcat(desc, ppi->pi_name);
308 	strcat(desc, " ");
309     }
310     if (psc->ps_name != NULL) {
311 	strcat(desc, psc->ps_name);
312 	strcat(desc, " ");
313     }
314     if (pc->pc_name != NULL)
315 	strcat(desc, pc->pc_name);
316 
317     pi = pnp_allocinfo();
318     pi->pi_desc = strdup(desc);
319     sprintf(desc,"0x%08x", devid);
320     pnp_addident(pi, desc);
321     pnp_addinfo(pi);
322 }
323 
324 int
325 biospci_find_devclass(uint32_t class, int index, uint32_t *locator)
326 {
327 	v86.ctl = V86_FLAGS;
328 	v86.addr = PCI_INT;
329 	v86.eax = FIND_PCI_CLASS_CODE;
330 	v86.ecx = class;
331 	v86.esi = index;
332 	v86int();
333 
334 	 /* error */
335 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
336 		return (-1);
337 
338 	*locator = v86.ebx;
339 	return (0);
340 }
341 
342 static int
343 biospci_find_device(uint32_t devid, int index, uint32_t *locator)
344 {
345 	v86.ctl = V86_FLAGS;
346 	v86.addr = PCI_INT;
347 	v86.eax = FIND_PCI_DEVICE;
348 	v86.edx = devid & 0xffff;		/* EDX - Vendor ID */
349 	v86.ecx = (devid >> 16) & 0xffff;	/* ECX - Device ID */
350 	v86.esi = index;
351 	v86int();
352 
353 	 /* error */
354 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
355 		return (-1);
356 
357 	*locator = v86.ebx;
358 	return (0);
359 }
360 /*
361  * Configuration space access methods.
362  * width = 0(byte), 1(word) or 2(dword).
363  */
364 int
365 biospci_write_config(uint32_t locator, int offset, int width, uint32_t val)
366 {
367 	v86.ctl = V86_FLAGS;
368 	v86.addr = PCI_INT;
369 	v86.eax = WRITE_CONFIG_BYTE + width;
370 	v86.ebx = locator;
371 	v86.edi = offset;
372 	v86.ecx = val;
373 	v86int();
374 
375 	 /* error */
376 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
377 		return (-1);
378 
379 	return(0);
380 }
381 
382 int
383 biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val)
384 {
385 	v86.ctl = V86_FLAGS;
386 	v86.addr = PCI_INT;
387 	v86.eax = READ_CONFIG_BYTE + width;
388 	v86.ebx = locator;
389 	v86.edi = offset;
390 	v86int();
391 
392 	 /* error */
393 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
394 		return (-1);
395 
396 	*val = v86.ecx;
397 	return (0);
398 }
399 
400 uint32_t
401 biospci_locator(int8_t bus, uint8_t device, uint8_t function)
402 {
403 
404 	return ((bus << 8) | ((device & 0x1f) << 3) | (function & 0x7));
405 }
406