xref: /illumos-gate/usr/src/uts/intel/io/pci/pci_autoconfig.c (revision dd72704bd9e794056c558153663c739e2012d721)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  * Copyright 2022 Oxide Computer Company
25  */
26 
27 /*
28  * Determine the PCI configuration mechanism recommended by the BIOS.
29  */
30 
31 #include <sys/types.h>
32 #include <sys/sunddi.h>
33 #include <sys/pci_impl.h>
34 #include <sys/ddi_subrdefs.h>
35 #include <sys/bootconf.h>
36 #include <sys/psw.h>
37 #include <sys/modctl.h>
38 #include <sys/errno.h>
39 #include <sys/pci.h>
40 #include <sys/pci_cfgspace.h>
41 #include <sys/reboot.h>
42 #include <sys/pci_cfgspace_impl.h>
43 #include <sys/mutex.h>
44 #include <sys/plat/pci_prd.h>
45 
46 extern int pci_boot_debug;
47 extern int pci_boot_maxbus;
48 
49 /*
50  * Interface routines
51  */
52 void pci_enumerate(int);
53 void pci_setup_tree(void);
54 void pci_reprogram(void);
55 dev_info_t *pci_boot_bus_to_dip(uint32_t);
56 
57 static struct modlmisc modlmisc = {
58 	&mod_miscops, "PCI BIOS interface"
59 };
60 
61 static struct modlinkage modlinkage = {
62 	MODREV_1, (void *)&modlmisc, NULL
63 };
64 
65 static pci_prd_upcalls_t pci_upcalls = {
66 	.pru_bus2dip_f = pci_boot_bus_to_dip
67 };
68 
69 int
70 _init(void)
71 {
72 	int	err;
73 
74 	if ((err = pci_prd_init(&pci_upcalls)) != 0) {
75 		return (err);
76 	}
77 
78 	if ((err = mod_install(&modlinkage)) != 0) {
79 		pci_prd_fini();
80 		return (err);
81 	}
82 
83 	impl_bus_add_probe(pci_enumerate);
84 	return (0);
85 }
86 
87 int
88 _fini(void)
89 {
90 	int	err;
91 
92 	if ((err = mod_remove(&modlinkage)) != 0)
93 		return (err);
94 
95 	impl_bus_delete_probe(pci_enumerate);
96 	pci_prd_fini();
97 	return (0);
98 }
99 
100 int
101 _info(struct modinfo *modinfop)
102 {
103 	return (mod_info(&modlinkage, modinfop));
104 }
105 
106 
107 /*
108  * This function is invoked twice: first time, with reprogram=0 to
109  * set up the PCI portion of the device tree. The second time is
110  * for reprogramming devices not set up by the BIOS.
111  */
112 void
113 pci_enumerate(int reprogram)
114 {
115 	extern void add_pci_fixes(void);
116 	extern void undo_pci_fixes(void);
117 
118 	/*
119 	 * On our first pass through here actually determine what the maximum
120 	 * bus that we should use is.
121 	 */
122 	if (reprogram == 0) {
123 		pci_boot_maxbus = pci_prd_max_bus();
124 	}
125 
126 	add_pci_fixes();
127 
128 	if (reprogram) {
129 		pci_reprogram();
130 		undo_pci_fixes();
131 		return;
132 	}
133 
134 	/* setup device tree */
135 	pci_setup_tree();
136 	undo_pci_fixes();
137 }
138