xref: /freebsd/sys/dev/bhnd/siba/siba_bhndb.c (revision ec65e4f8d0654361df5e97d4de3518edebf76b46)
1 /*-
2  * Copyright (c) 2015 Landon Fuller <landon@landonf.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13  *    redistribution must be conditioned upon including a substantially
14  *    similar Disclaimer requirement for further binary redistribution.
15  *
16  * NO WARRANTY
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27  * THE POSSIBILITY OF SUCH DAMAGES.
28  */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 
38 #include <dev/bhnd/bhnd_ids.h>
39 #include <dev/bhnd/bhndb/bhndbvar.h>
40 #include <dev/bhnd/bhndb/bhndb_hwdata.h>
41 
42 #include "sibavar.h"
43 
44 /*
45  * Supports attachment of siba(4) bus devices via a bhndb bridge.
46  */
47 
48 //
49 // TODO: PCI rev < 6 interrupt handling
50 //
51 // On early PCI cores (rev < 6) interrupt masking is handled via interconnect
52 // configuration registers (SBINTVEC), rather than the PCI_INT_MASK
53 // config register.
54 //
55 // On those devices, we should handle interrupts locally using SBINTVEC, rather
56 // than delegating to our parent bhndb device.
57 //
58 
59 static int
60 siba_bhndb_probe(device_t dev)
61 {
62 	const struct bhnd_chipid *cid;
63 
64 	/* Check bus type */
65 	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
66 	if (cid->chip_type != BHND_CHIPTYPE_SIBA)
67 		return (ENXIO);
68 
69 	/* Delegate to default probe implementation */
70 	return (siba_probe(dev));
71 }
72 
73 static int
74 siba_bhndb_attach(device_t dev)
75 {
76 	const struct bhnd_chipid	*chipid;
77 	int				 error;
78 
79 	/* Enumerate our children. */
80 	chipid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
81 	if ((error = siba_add_children(dev, chipid)))
82 		return (error);
83 
84 	/* Initialize full bridge configuration */
85 	error = BHNDB_INIT_FULL_CONFIG(device_get_parent(dev), dev,
86 	    bhndb_siba_priority_table);
87 	if (error)
88 		return (error);
89 
90 	/* Call our superclass' implementation */
91 	return (siba_attach(dev));
92 }
93 
94 /* Suspend all references to the device's cfg register blocks */
95 static void
96 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) {
97 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
98 		if (dinfo->cfg[i] == NULL)
99 			continue;
100 
101 		BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
102 		    SYS_RES_MEMORY, dinfo->cfg[i]->res);
103 	}
104 }
105 
106 static int
107 siba_bhndb_suspend_child(device_t dev, device_t child)
108 {
109 	struct siba_devinfo	*dinfo;
110 	int			 error;
111 
112 	if (device_get_parent(child) != dev)
113 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
114 
115 	dinfo = device_get_ivars(child);
116 
117 	/* Suspend the child */
118 	if ((error = bhnd_generic_br_suspend_child(dev, child)))
119 		return (error);
120 
121 	/* Suspend resource references to the child's config registers */
122 	siba_bhndb_suspend_cfgblocks(dev, dinfo);
123 
124 	return (0);
125 }
126 
127 static int
128 siba_bhndb_resume_child(device_t dev, device_t child)
129 {
130 	struct siba_devinfo	*dinfo;
131 	int			 error;
132 
133 	if (device_get_parent(child) != dev)
134 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
135 
136 	if (!device_is_suspended(child))
137 		return (EBUSY);
138 
139 	dinfo = device_get_ivars(child);
140 
141 	/* Resume all resource references to the child's config registers */
142 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
143 		if (dinfo->cfg[i] == NULL)
144 			continue;
145 
146 		error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
147 		    SYS_RES_MEMORY, dinfo->cfg[i]->res);
148 		if (error) {
149 			siba_bhndb_suspend_cfgblocks(dev, dinfo);
150 			return (error);
151 		}
152 	}
153 
154 	/* Resume the child */
155 	if ((error = bhnd_generic_br_resume_child(dev, child))) {
156 		siba_bhndb_suspend_cfgblocks(dev, dinfo);
157 		return (error);
158 	}
159 
160 	return (0);
161 }
162 
163 static device_method_t siba_bhndb_methods[] = {
164 	/* Device interface */
165 	DEVMETHOD(device_probe,			siba_bhndb_probe),
166 	DEVMETHOD(device_attach,		siba_bhndb_attach),
167 
168 	/* Bus interface */
169 	DEVMETHOD(bus_suspend_child,		siba_bhndb_suspend_child),
170 	DEVMETHOD(bus_resume_child,		siba_bhndb_resume_child),
171 
172 	DEVMETHOD_END
173 };
174 
175 DEFINE_CLASS_1(bhnd, siba_bhndb_driver, siba_bhndb_methods,
176     sizeof(struct siba_softc), siba_driver);
177 
178 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, bhnd_devclass, NULL, NULL);
179 
180 MODULE_VERSION(siba_bhndb, 1);
181 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1);
182 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1);