xref: /freebsd/stand/i386/libi386/biospci.c (revision cfd6422a5217410fbd66f7a7a8a64d9d85e61229)
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Copyright (c) 2016 Netflix, Inc.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * PnP enumerator using the PCI BIOS.
34  */
35 
36 #include <stand.h>
37 #include <machine/stdarg.h>
38 #include <bootstrap.h>
39 #include <isapnp.h>
40 #include <btxv86.h>
41 #include "libi386.h"
42 
43 /*
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.
46  *
47  * So we have to scan by class code, subclass code and sometimes programming
48  * interface.
49  */
50 
51 struct pci_progif
52 {
53     int		pi_code;
54     const char	*pi_name;
55 };
56 
57 static struct pci_progif progif_null[] = {
58     {0x0,	NULL},
59     {-1,	NULL}
60 };
61 
62 static struct pci_progif progif_display[] = {
63     {0x0,	"VGA"},
64     {0x1,	"8514"},
65     {-1,	NULL}
66 };
67 
68 static struct pci_progif progif_ide[] = {
69     {0x00,	NULL},
70     {0x01,	NULL},
71     {0x02,	NULL},
72     {0x03,	NULL},
73     {0x04,	NULL},
74     {0x05,	NULL},
75     {0x06,	NULL},
76     {0x07,	NULL},
77     {0x08,	NULL},
78     {0x09,	NULL},
79     {0x0a,	NULL},
80     {0x0b,	NULL},
81     {0x0c,	NULL},
82     {0x0d,	NULL},
83     {0x0e,	NULL},
84     {0x0f,	NULL},
85     {0x80,	NULL},
86     {0x81,	NULL},
87     {0x82,	NULL},
88     {0x83,	NULL},
89     {0x84,	NULL},
90     {0x85,	NULL},
91     {0x86,	NULL},
92     {0x87,	NULL},
93     {0x88,	NULL},
94     {0x89,	NULL},
95     {0x8a,	NULL},
96     {0x8b,	NULL},
97     {0x8c,	NULL},
98     {0x8d,	NULL},
99     {0x8e,	NULL},
100     {0x8f,	NULL},
101     {-1,	NULL}
102 };
103 
104 static struct pci_progif progif_serial[] = {
105     {0x0,	"8250"},
106     {0x1,	"16450"},
107     {0x2,	"16550"},
108     {-1,	NULL}
109 };
110 
111 static struct pci_progif progif_parallel[] = {
112     {0x0,	"Standard"},
113     {0x1,	"Bidirectional"},
114     {0x2,	"ECP"},
115     {-1,	NULL}
116 };
117 
118 static struct pci_progif progif_firewire[] = {
119     {0x10,	"OHCI"},
120     {-1,	NULL}
121 };
122 
123 struct pci_subclass
124 {
125     int			ps_subclass;
126     const char		*ps_name;
127     struct pci_progif	*ps_progif;	/* if set, use for programming interface value(s) */
128 };
129 
130 static struct pci_subclass subclass_old[] = {
131     {0x0,	"Old non-VGA",		progif_null},
132     {0x1,	"Old VGA",		progif_null},
133     {-1,	NULL,			NULL}
134 };
135 
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},
143     {-1,	NULL,			NULL}
144 };
145 
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},
152     {-1,	NULL,			NULL}
153 };
154 
155 static struct pci_subclass subclass_display[] = {
156     {0x0,	NULL,			progif_display},
157     {0x1,	"XGA",			progif_null},
158     {0x80,	"other",		progif_null},
159     {-1,	NULL,			NULL}
160 };
161 
162 static struct pci_subclass subclass_comms[] = {
163     {0x0,	"serial",		progif_serial},
164     {0x1,	"parallel",		progif_parallel},
165     {0x80,	"communications",	progif_null},
166     {-1,	NULL,			NULL}
167 };
168 
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},
175     {-1,	NULL,			NULL}
176 };
177 
178 static struct pci_class
179 {
180     int			pc_class;
181     const char		*pc_name;
182     struct pci_subclass	*pc_subclass;
183 } pci_classes[] = {
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},
190     {-1,	NULL,		NULL}
191 };
192 
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);
195 
196 struct pnphandler biospcihandler =
197 {
198     "PCI BIOS",
199     biospci_enumerate
200 };
201 static int biospci_version;
202 
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
215 
216 #define PCI_INT			0x1a
217 
218 #define PCI_SIGNATURE		0x20494350	/* AKA "PCI " */
219 
220 void
221 biospci_detect(void)
222 {
223     uint16_t version, hwcap, maxbus;
224     char buf[24];
225 
226     /* Find the PCI BIOS */
227     v86.ctl = V86_FLAGS;
228     v86.addr = PCI_INT;
229     v86.eax = PCI_BIOS_PRESENT;
230     v86.edi = 0x0;
231     v86int();
232 
233     /* Check for OK response */
234     if (V86_CY(v86.efl) || ((v86.eax & 0xff00) != 0) ||
235 	(v86.edx != PCI_SIGNATURE))
236 	return;
237 
238     version = v86.ebx & 0xffff;
239     hwcap = v86.eax & 0xff;
240     maxbus = v86.ecx & 0xff;
241 #if 0
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" : "",
245 	   maxbus);
246 #endif
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);
258 }
259 
260 static void
261 biospci_enumerate(void)
262 {
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;
268 
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++) {
275 
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);
282 		    if (err != 0)
283 			break;
284 
285 		    /* Read the device identifier from the nominated device */
286 		    err = biospci_read_config(locator, 0, BIOSPCI_32BITS, &devid);
287 		    if (err != 0)
288 			break;
289 
290 		    /* We have the device ID, create a PnP object and save everything */
291 		    biospci_addinfo(devid, pc, psc, ppi);
292 		}
293 	    }
294 	}
295     }
296 }
297 
298 static void
299 biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi)
300 {
301     struct pnpinfo	*pi;
302     char		desc[80];
303 
304 
305     /* build the description */
306     desc[0] = 0;
307     if (ppi->pi_name != NULL) {
308 	strcat(desc, ppi->pi_name);
309 	strcat(desc, " ");
310     }
311     if (psc->ps_name != NULL) {
312 	strcat(desc, psc->ps_name);
313 	strcat(desc, " ");
314     }
315     if (pc->pc_name != NULL)
316 	strcat(desc, pc->pc_name);
317 
318     pi = pnp_allocinfo();
319     pi->pi_desc = strdup(desc);
320     sprintf(desc,"0x%08x", devid);
321     pnp_addident(pi, desc);
322     pnp_addinfo(pi);
323 }
324 
325 int
326 biospci_find_devclass(uint32_t class, int index, uint32_t *locator)
327 {
328 	v86.ctl = V86_FLAGS;
329 	v86.addr = PCI_INT;
330 	v86.eax = FIND_PCI_CLASS_CODE;
331 	v86.ecx = class;
332 	v86.esi = index;
333 	v86int();
334 
335 	 /* error */
336 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
337 		return (-1);
338 
339 	*locator = v86.ebx;
340 	return (0);
341 }
342 
343 static int
344 biospci_find_device(uint32_t devid, int index, uint32_t *locator)
345 {
346 	v86.ctl = V86_FLAGS;
347 	v86.addr = PCI_INT;
348 	v86.eax = FIND_PCI_DEVICE;
349 	v86.edx = devid & 0xffff;		/* EDX - Vendor ID */
350 	v86.ecx = (devid >> 16) & 0xffff;	/* ECX - Device ID */
351 	v86.esi = index;
352 	v86int();
353 
354 	 /* error */
355 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
356 		return (-1);
357 
358 	*locator = v86.ebx;
359 	return (0);
360 }
361 /*
362  * Configuration space access methods.
363  * width = 0(byte), 1(word) or 2(dword).
364  */
365 int
366 biospci_write_config(uint32_t locator, int offset, int width, uint32_t val)
367 {
368 	v86.ctl = V86_FLAGS;
369 	v86.addr = PCI_INT;
370 	v86.eax = WRITE_CONFIG_BYTE + width;
371 	v86.ebx = locator;
372 	v86.edi = offset;
373 	v86.ecx = val;
374 	v86int();
375 
376 	 /* error */
377 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
378 		return (-1);
379 
380 	return(0);
381 }
382 
383 int
384 biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val)
385 {
386 	v86.ctl = V86_FLAGS;
387 	v86.addr = PCI_INT;
388 	v86.eax = READ_CONFIG_BYTE + width;
389 	v86.ebx = locator;
390 	v86.edi = offset;
391 	v86int();
392 
393 	 /* error */
394 	if (V86_CY(v86.efl) || (v86.eax & 0xff00))
395 		return (-1);
396 
397 	*val = v86.ecx;
398 	return (0);
399 }
400 
401 uint32_t
402 biospci_locator(int8_t bus, uint8_t device, uint8_t function)
403 {
404 
405 	return ((bus << 8) | ((device & 0x1f) << 3) | (function & 0x7));
406 }
407