]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - stand/i386/libi386/biospci.c
MFC r325834,r325997,326502: Move sys/boot to stand/
[FreeBSD/FreeBSD.git] / stand / i386 / libi386 / biospci.c
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 #ifdef BOOT_FORTH
42 #include "ficl.h"
43 #endif
44
45 /*
46  * Stupid PCI BIOS interface doesn't let you simply enumerate everything
47  * that's there, instead you have to ask it if it has something.
48  *
49  * So we have to scan by class code, subclass code and sometimes programming
50  * interface.
51  */
52
53 struct pci_progif 
54 {
55     int         pi_code;
56     const char  *pi_name;
57 };
58
59 static struct pci_progif progif_null[] = {
60     {0x0,       NULL},
61     {-1,        NULL}
62 };
63
64 static struct pci_progif progif_display[] = {
65     {0x0,       "VGA"},
66     {0x1,       "8514"},
67     {-1,        NULL}
68 };
69
70 static struct pci_progif progif_ide[] = {
71     {0x00,      NULL},
72     {0x01,      NULL},
73     {0x02,      NULL},
74     {0x03,      NULL},
75     {0x04,      NULL},
76     {0x05,      NULL},
77     {0x06,      NULL},
78     {0x07,      NULL},
79     {0x08,      NULL},
80     {0x09,      NULL},
81     {0x0a,      NULL},
82     {0x0b,      NULL},
83     {0x0c,      NULL},
84     {0x0d,      NULL},
85     {0x0e,      NULL},
86     {0x0f,      NULL},
87     {0x80,      NULL},
88     {0x81,      NULL},
89     {0x82,      NULL},
90     {0x83,      NULL},
91     {0x84,      NULL},
92     {0x85,      NULL},
93     {0x86,      NULL},
94     {0x87,      NULL},
95     {0x88,      NULL},
96     {0x89,      NULL},
97     {0x8a,      NULL},
98     {0x8b,      NULL},
99     {0x8c,      NULL},
100     {0x8d,      NULL},
101     {0x8e,      NULL},
102     {0x8f,      NULL},
103     {-1,        NULL}
104 };
105
106 static struct pci_progif progif_serial[] = {
107     {0x0,       "8250"},
108     {0x1,       "16450"},
109     {0x2,       "16550"},
110     {-1,        NULL}
111 };
112
113 static struct pci_progif progif_parallel[] = {
114     {0x0,       "Standard"},
115     {0x1,       "Bidirectional"},
116     {0x2,       "ECP"},
117     {-1,        NULL}
118 };
119
120 static struct pci_progif progif_firewire[] = {
121     {0x10,      "OHCI"},
122     {-1,        NULL}
123 };
124
125 struct pci_subclass 
126 {
127     int                 ps_subclass;
128     const char          *ps_name;
129     struct pci_progif   *ps_progif;     /* if set, use for programming interface value(s) */
130 };
131
132 static struct pci_subclass subclass_old[] = {
133     {0x0,       "Old non-VGA",          progif_null},
134     {0x1,       "Old VGA",              progif_null},
135     {-1,        NULL,                   NULL}
136 };
137
138 static struct pci_subclass subclass_mass[] = {
139     {0x0,       "SCSI",                 progif_null},
140     {0x1,       "IDE",                  progif_ide},
141     {0x2,       "Floppy disk",          progif_null},
142     {0x3,       "IPI",                  progif_null},
143     {0x4,       "RAID",                 progif_null},
144     {0x80,      "mass storage",         progif_null},
145     {-1,        NULL,                   NULL}
146 };
147
148 static struct pci_subclass subclass_net[] = {
149     {0x0,       "Ethernet",             progif_null},
150     {0x1,       "Token ring",           progif_null},
151     {0x2,       "FDDI",                 progif_null},
152     {0x3,       "ATM",                  progif_null},
153     {0x80,      "network",              progif_null},
154     {-1,        NULL,                   NULL}
155 };
156
157 static struct pci_subclass subclass_display[] = {
158     {0x0,       NULL,                   progif_display},
159     {0x1,       "XGA",                  progif_null},
160     {0x80,      "other",                progif_null},
161     {-1,        NULL,                   NULL}
162 };
163
164 static struct pci_subclass subclass_comms[] = {
165     {0x0,       "serial",               progif_serial},
166     {0x1,       "parallel",             progif_parallel},
167     {0x80,      "communications",       progif_null},
168     {-1,        NULL,                   NULL}
169 };
170
171 static struct pci_subclass subclass_serial[] = {
172     {0x0,       "FireWire",             progif_firewire},
173     {0x1,       "ACCESS.bus",           progif_null},
174     {0x2,       "SSA",                  progif_null},
175     {0x3,       "USB",                  progif_null},
176     {0x4,       "Fibrechannel",         progif_null},
177     {-1,        NULL,                   NULL}
178 };
179
180 static struct pci_class
181 {
182     int                 pc_class;
183     const char          *pc_name;
184     struct pci_subclass *pc_subclass;
185 } pci_classes[] = {
186     {0x0,       "device",       subclass_old},
187     {0x1,       "controller",   subclass_mass},
188     {0x2,       "controller",   subclass_net},
189     {0x3,       "display",      subclass_display},
190     {0x7,       "controller",   subclass_comms},
191     {0xc,       "controller",   subclass_serial},
192     {-1,        NULL,           NULL}
193 };
194
195 static void     biospci_enumerate(void);
196 static void     biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi);
197
198 struct pnphandler biospcihandler =
199 {
200     "PCI BIOS",
201     biospci_enumerate
202 };
203 static int biospci_version;
204
205 #define PCI_BIOS_PRESENT        0xb101
206 #define FIND_PCI_DEVICE         0xb102
207 #define FIND_PCI_CLASS_CODE     0xb103
208 #define GENERATE_SPECIAL_CYCLE  0xb106
209 #define READ_CONFIG_BYTE        0xb108
210 #define READ_CONFIG_WORD        0xb109
211 #define READ_CONFIG_DWORD       0xb10a
212 #define WRITE_CONFIG_BYTE       0xb10b
213 #define WRITE_CONFIG_WORD       0xb10c
214 #define WRITE_CONFIG_DWORD      0xb10d
215 #define GET_IRQ_ROUTING_OPTIONS 0xb10e
216 #define SET_PCI_IRQ             0xb10f
217
218 #define PCI_INT                 0x1a
219
220 #define PCI_SIGNATURE           0x20494350      /* AKA "PCI " */
221
222 void
223 biospci_detect(void)
224 {
225     uint16_t version, hwcap, maxbus;
226     char buf[24];
227
228     /* Find the PCI BIOS */
229     v86.ctl = V86_FLAGS;
230     v86.addr = PCI_INT;
231     v86.eax = PCI_BIOS_PRESENT;
232     v86.edi = 0x0;
233     v86int();
234
235     /* Check for OK response */
236     if (V86_CY(v86.efl) || ((v86.eax & 0xff00) != 0) ||
237         (v86.edx != PCI_SIGNATURE))
238         return;
239
240     version = v86.ebx & 0xffff;
241     hwcap = v86.eax & 0xff;
242     maxbus = v86.ecx & 0xff;
243 #if 0
244     printf("PCI BIOS %d.%d%s%s maxbus %d\n", 
245            bcd2bin((version >> 8) & 0xf), bcd2bin(version & 0xf),
246            (hwcap & 1) ? " config1" : "", (hwcap & 2) ? " config2" : "",
247            maxbus);
248 #endif
249     sprintf(buf, "%d", bcd2bin((version >> 8) & 0xf));
250     setenv("pcibios.major", buf, 1);
251     sprintf(buf, "%d", bcd2bin(version & 0xf));
252     setenv("pcibios.minor", buf, 1);
253     sprintf(buf, "%d", !!(hwcap & 1));
254     setenv("pcibios.config1", buf, 1);
255     sprintf(buf, "%d", !!(hwcap & 2));
256     setenv("pcibios.config2", buf, 1);
257     sprintf(buf, "%d", maxbus);
258     setenv("pcibios.maxbus", buf, 1);
259     biospci_version = bcd2bin((version >> 8) & 0xf) * 10 + bcd2bin(version & 0xf);
260 }
261
262 static void
263 biospci_enumerate(void)
264 {
265     int                 device_index, err;
266     uint32_t            locator, devid;
267     struct pci_class    *pc;
268     struct pci_subclass *psc;
269     struct pci_progif   *ppi;
270
271     /* Iterate over known classes */
272     for (pc = pci_classes; pc->pc_class >= 0; pc++) {
273         /* Iterate over subclasses */
274         for (psc = pc->pc_subclass; psc->ps_subclass >= 0; psc++) {
275             /* Iterate over programming interfaces */
276             for (ppi = psc->ps_progif; ppi->pi_code >= 0; ppi++) {
277
278                 /* Scan for matches */
279                 for (device_index = 0; ; device_index++) {
280                     /* Look for a match */
281                     err = biospci_find_devclass((pc->pc_class << 16)
282                         + (psc->ps_subclass << 8) + ppi->pi_code,
283                         device_index, &locator);
284                     if (err != 0)
285                         break;
286
287                     /* Read the device identifier from the nominated device */
288                     err = biospci_read_config(locator, 0, 2, &devid);
289                     if (err != 0)
290                         break;
291                     
292                     /* We have the device ID, create a PnP object and save everything */
293                     biospci_addinfo(devid, pc, psc, ppi);
294                 }
295             }
296         }
297     }
298 }
299
300 static void
301 biospci_addinfo(int devid, struct pci_class *pc, struct pci_subclass *psc, struct pci_progif *ppi) 
302 {
303     struct pnpinfo      *pi;
304     char                desc[80];
305     
306     
307     /* build the description */
308     desc[0] = 0;
309     if (ppi->pi_name != NULL) {
310         strcat(desc, ppi->pi_name);
311         strcat(desc, " ");
312     }
313     if (psc->ps_name != NULL) {
314         strcat(desc, psc->ps_name);
315         strcat(desc, " ");
316     }
317     if (pc->pc_name != NULL)
318         strcat(desc, pc->pc_name);
319
320     pi = pnp_allocinfo();
321     pi->pi_desc = strdup(desc);
322     sprintf(desc,"0x%08x", devid);
323     pnp_addident(pi, desc);
324     pnp_addinfo(pi);
325 }
326
327 int
328 biospci_find_devclass(uint32_t class, int index, uint32_t *locator)
329 {
330         v86.ctl = V86_FLAGS;
331         v86.addr = PCI_INT;
332         v86.eax = FIND_PCI_CLASS_CODE;
333         v86.ecx = class;
334         v86.esi = index;
335         v86int();
336
337          /* error */
338         if (V86_CY(v86.efl) || (v86.eax & 0xff00))
339                 return (-1);
340
341         *locator = v86.ebx;
342         return (0);
343 }
344
345 static int
346 biospci_find_device(uint32_t devid, int index, uint32_t *locator)
347 {
348         v86.ctl = V86_FLAGS;
349         v86.addr = PCI_INT;
350         v86.eax = FIND_PCI_DEVICE;
351         v86.edx = devid & 0xffff;               /* EDX - Vendor ID */
352         v86.ecx = (devid >> 16) & 0xffff;       /* ECX - Device ID */
353         v86.esi = index;
354         v86int();
355
356          /* error */
357         if (V86_CY(v86.efl) || (v86.eax & 0xff00))
358                 return (-1);
359
360         *locator = v86.ebx;
361         return (0);
362 }
363 /*
364  * Configuration space access methods.
365  * width = 0(byte), 1(word) or 2(dword).
366  */
367 int
368 biospci_write_config(uint32_t locator, int offset, int width, uint32_t val)
369 {
370         v86.ctl = V86_FLAGS;
371         v86.addr = PCI_INT;
372         v86.eax = WRITE_CONFIG_BYTE + width;
373         v86.ebx = locator;
374         v86.edi = offset;
375         v86.ecx = val;
376         v86int();
377
378          /* error */
379         if (V86_CY(v86.efl) || (v86.eax & 0xff00))
380                 return (-1);
381
382         return(0);
383 }
384
385 int
386 biospci_read_config(uint32_t locator, int offset, int width, uint32_t *val)
387 {
388         v86.ctl = V86_FLAGS;
389         v86.addr = PCI_INT;
390         v86.eax = READ_CONFIG_BYTE + width;
391         v86.ebx = locator;
392         v86.edi = offset;
393         v86int();
394
395          /* error */
396         if (V86_CY(v86.efl) || (v86.eax & 0xff00))
397                 return (-1);
398
399         *val = v86.ecx;
400         return (0);
401 }
402
403 uint32_t
404 biospci_locator(int8_t bus, uint8_t device, uint8_t function)
405 {
406
407         return ((bus << 8) | ((device & 0x1f) << 3) | (function & 0x7));
408 }
409
410 /*
411  * Counts the number of instances of devid we have in the system, as least as
412  * far as the PCI BIOS is able to tell.
413  */
414 static int
415 biospci_count_device_type(uint32_t devid)
416 {
417         int i;
418
419         for (i = 0; 1; i++) {
420                 v86.ctl = V86_FLAGS;
421                 v86.addr = PCI_INT;
422                 v86.eax = FIND_PCI_DEVICE;
423                 v86.edx = devid & 0xffff;               /* EDX - Vendor ID */
424                 v86.ecx = (devid >> 16) & 0xffff;       /* ECX - Device ID */
425                 v86.esi = i;
426                 v86int();
427                 if (V86_CY(v86.efl) || (v86.eax & 0xff00))
428                         break;
429
430         }
431         return i;
432 }
433
434 #ifdef BOOT_FORTH
435 /*
436  * pcibios-device-count (devid -- count)
437  *
438  * Returns the PCI BIOS' count of how many devices matching devid are in the system.
439  * devid is the 32-bit vendor + device.
440  */
441 static void
442 ficlPciBiosCountDevices(FICL_VM *pVM)
443 {
444     uint32_t devid;
445     int i;
446
447     devid = stackPopINT(pVM->pStack);
448
449     i = biospci_count_device_type(devid);
450
451     stackPushINT(pVM->pStack, i);
452 }
453
454 /*
455  * pcibios-write-config (locator offset width value -- )
456  *
457  * Writes the specified config register.
458  * Locator is bus << 8 | device << 3 | fuction
459  * offset is the pci config register
460  * width is 0 for byte, 1 for word, 2 for dword
461  * value is the value to write
462  */
463 static void
464 ficlPciBiosWriteConfig(FICL_VM *pVM)
465 {
466     uint32_t value, width, offset, locator;
467
468     value = stackPopINT(pVM->pStack);
469     width = stackPopINT(pVM->pStack);
470     offset = stackPopINT(pVM->pStack);
471     locator = stackPopINT(pVM->pStack);
472
473     biospci_write_config(locator, offset, width, value);
474 }
475
476 /*
477  * pcibios-read-config (locator offset width -- value)
478  *
479  * Reads the specified config register.
480  * Locator is bus << 8 | device << 3 | fuction
481  * offset is the pci config register
482  * width is 0 for byte, 1 for word, 2 for dword
483  * value is the value to read from the register
484  */
485 static void
486 ficlPciBiosReadConfig(FICL_VM *pVM)
487 {
488     uint32_t value, width, offset, locator;
489
490     width = stackPopINT(pVM->pStack);
491     offset = stackPopINT(pVM->pStack);
492     locator = stackPopINT(pVM->pStack);
493
494     biospci_read_config(locator, offset, width, &value);
495
496     stackPushINT(pVM->pStack, value);
497 }
498
499 /*
500  * pcibios-find-devclass (class index -- locator)
501  *
502  * Finds the index'th instance of class in the pci tree.
503  * must be an exact match.
504  * class is the class to search for.
505  * index 0..N (set to 0, increment until error)
506  *
507  * Locator is bus << 8 | device << 3 | fuction (or -1 on error)
508  */
509 static void
510 ficlPciBiosFindDevclass(FICL_VM *pVM)
511 {
512     uint32_t index, class, locator;
513
514     index = stackPopINT(pVM->pStack);
515     class = stackPopINT(pVM->pStack);
516
517     if (biospci_find_devclass(class, index, &locator))
518         locator = 0xffffffff;
519
520     stackPushINT(pVM->pStack, locator);
521 }
522
523 /*
524  * pcibios-find-device(devid index -- locator)
525  *
526  * Finds the index'th instance of devid in the pci tree.
527  * must be an exact match.
528  * class is the class to search for.
529  * index 0..N (set to 0, increment until error)
530  *
531  * Locator is bus << 8 | device << 3 | fuction (or -1 on error)
532  */
533 static void
534 ficlPciBiosFindDevice(FICL_VM *pVM)
535 {
536     uint32_t index, devid, locator;
537
538     index = stackPopINT(pVM->pStack);
539     devid = stackPopINT(pVM->pStack);
540
541     if (biospci_find_device(devid, index, &locator))
542         locator = 0xffffffff;
543
544     stackPushINT(pVM->pStack, locator);
545 }
546
547 /*
548  * pcibios-find-device(bus device function -- locator)
549  *
550  * converts bus, device, function to locator.
551  *
552  * Locator is bus << 8 | device << 3 | fuction
553  */
554 static void
555 ficlPciBiosLocator(FICL_VM *pVM)
556 {
557     uint32_t bus, device, function, locator;
558
559     function = stackPopINT(pVM->pStack);
560     device = stackPopINT(pVM->pStack);
561     bus = stackPopINT(pVM->pStack);
562
563     locator = biospci_locator(bus, device, function);
564
565     stackPushINT(pVM->pStack, locator);
566 }
567
568 /*
569  * Glue function to add the appropriate forth words to access pci bios
570  * functionality.
571  */
572 static void ficlCompilePciBios(FICL_SYSTEM *pSys)
573 {
574     FICL_DICT *dp = pSys->dp;
575     assert (dp);
576
577     dictAppendWord(dp, "pcibios-device-count", ficlPciBiosCountDevices, FW_DEFAULT);
578     dictAppendWord(dp, "pcibios-read-config", ficlPciBiosReadConfig, FW_DEFAULT);
579     dictAppendWord(dp, "pcibios-write-config", ficlPciBiosWriteConfig, FW_DEFAULT);
580     dictAppendWord(dp, "pcibios-find-devclass", ficlPciBiosFindDevclass, FW_DEFAULT);
581     dictAppendWord(dp, "pcibios-find-device", ficlPciBiosFindDevice, FW_DEFAULT);
582     dictAppendWord(dp, "pcibios-locator", ficlPciBiosLocator, FW_DEFAULT);
583
584     ficlSetEnv(pSys, "pcibios-version", biospci_version);
585 }
586
587 FICL_COMPILE_SET(ficlCompilePciBios);
588 #endif