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