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