17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5bd87be88Ssethg  * Common Development and Distribution License (the "License").
6bd87be88Ssethg  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2205f867c3Sgs  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*cd0d4b40SRobert Mustacchi  * Copyright 2022 Oxide Computer Company
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * Determine the PCI configuration mechanism recommended by the BIOS.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
337c478bd9Sstevel@tonic-gate #include <sys/pci_impl.h>
347c478bd9Sstevel@tonic-gate #include <sys/ddi_subrdefs.h>
357c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
367c478bd9Sstevel@tonic-gate #include <sys/psw.h>
377c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
387c478bd9Sstevel@tonic-gate #include <sys/errno.h>
397c478bd9Sstevel@tonic-gate #include <sys/pci.h>
407c478bd9Sstevel@tonic-gate #include <sys/pci_cfgspace.h>
417c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
42c88420b3Sdmick #include <sys/pci_cfgspace_impl.h>
43c88420b3Sdmick #include <sys/mutex.h>
44*cd0d4b40SRobert Mustacchi #include <sys/plat/pci_prd.h>
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate extern int pci_boot_debug;
47*cd0d4b40SRobert Mustacchi extern int pci_boot_maxbus;
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Interface routines
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate void pci_enumerate(int);
537c478bd9Sstevel@tonic-gate void pci_setup_tree(void);
547c478bd9Sstevel@tonic-gate void pci_reprogram(void);
55*cd0d4b40SRobert Mustacchi dev_info_t *pci_boot_bus_to_dip(uint32_t);
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate static struct modlmisc modlmisc = {
58613b2871SRichard Bean 	&mod_miscops, "PCI BIOS interface"
597c478bd9Sstevel@tonic-gate };
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
627c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlmisc, NULL
637c478bd9Sstevel@tonic-gate };
647c478bd9Sstevel@tonic-gate 
65*cd0d4b40SRobert Mustacchi static pci_prd_upcalls_t pci_upcalls = {
66*cd0d4b40SRobert Mustacchi 	.pru_bus2dip_f = pci_boot_bus_to_dip
67*cd0d4b40SRobert Mustacchi };
68*cd0d4b40SRobert Mustacchi 
697c478bd9Sstevel@tonic-gate int
_init(void)707c478bd9Sstevel@tonic-gate _init(void)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	int	err;
737c478bd9Sstevel@tonic-gate 
74*cd0d4b40SRobert Mustacchi 	if ((err = pci_prd_init(&pci_upcalls)) != 0) {
75*cd0d4b40SRobert Mustacchi 		return (err);
76*cd0d4b40SRobert Mustacchi 	}
77*cd0d4b40SRobert Mustacchi 
78*cd0d4b40SRobert Mustacchi 	if ((err = mod_install(&modlinkage)) != 0) {
79*cd0d4b40SRobert Mustacchi 		pci_prd_fini();
807c478bd9Sstevel@tonic-gate 		return (err);
81*cd0d4b40SRobert Mustacchi 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	impl_bus_add_probe(pci_enumerate);
847c478bd9Sstevel@tonic-gate 	return (0);
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate int
_fini(void)887c478bd9Sstevel@tonic-gate _fini(void)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	int	err;
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 	if ((err = mod_remove(&modlinkage)) != 0)
937c478bd9Sstevel@tonic-gate 		return (err);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	impl_bus_delete_probe(pci_enumerate);
96*cd0d4b40SRobert Mustacchi 	pci_prd_fini();
977c478bd9Sstevel@tonic-gate 	return (0);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1017c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate 
106b1f176e8Sjg 
1077c478bd9Sstevel@tonic-gate /*
108c88420b3Sdmick  * This function is invoked twice: first time, with reprogram=0 to
1097c478bd9Sstevel@tonic-gate  * set up the PCI portion of the device tree. The second time is
1107c478bd9Sstevel@tonic-gate  * for reprogramming devices not set up by the BIOS.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate void
pci_enumerate(int reprogram)1137c478bd9Sstevel@tonic-gate pci_enumerate(int reprogram)
1147c478bd9Sstevel@tonic-gate {
115bd87be88Ssethg 	extern void add_pci_fixes(void);
116bd87be88Ssethg 	extern void undo_pci_fixes(void);
117bd87be88Ssethg 
118*cd0d4b40SRobert Mustacchi 	/*
119*cd0d4b40SRobert Mustacchi 	 * On our first pass through here actually determine what the maximum
120*cd0d4b40SRobert Mustacchi 	 * bus that we should use is.
121*cd0d4b40SRobert Mustacchi 	 */
122*cd0d4b40SRobert Mustacchi 	if (reprogram == 0) {
123*cd0d4b40SRobert Mustacchi 		pci_boot_maxbus = pci_prd_max_bus();
124*cd0d4b40SRobert Mustacchi 	}
125*cd0d4b40SRobert Mustacchi 
126bd87be88Ssethg 	add_pci_fixes();
127bd87be88Ssethg 
1287c478bd9Sstevel@tonic-gate 	if (reprogram) {
1297c478bd9Sstevel@tonic-gate 		pci_reprogram();
130bd87be88Ssethg 		undo_pci_fixes();
1317c478bd9Sstevel@tonic-gate 		return;
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	/* setup device tree */
1357c478bd9Sstevel@tonic-gate 	pci_setup_tree();
136bd87be88Ssethg 	undo_pci_fixes();
1377c478bd9Sstevel@tonic-gate }
138