xref: /freebsd/sys/dev/bhnd/siba/siba_bhndb.c (revision d96700a6da2afa88607fbd7405ade439424d10d9)
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 #include <sys/systm.h>
38 
39 #include <dev/bhnd/bhnd_ids.h>
40 #include <dev/bhnd/bhndb/bhndbvar.h>
41 #include <dev/bhnd/bhndb/bhndb_hwdata.h>
42 
43 #include "sibareg.h"
44 #include "sibavar.h"
45 
46 /*
47  * Supports attachment of siba(4) bus devices via a bhndb bridge.
48  */
49 
50 //
51 // TODO: PCI rev < 6 interrupt handling
52 //
53 // On early PCI cores (rev < 6) interrupt masking is handled via interconnect
54 // configuration registers (SBINTVEC), rather than the PCI_INT_MASK
55 // config register.
56 //
57 // On those devices, we should handle interrupts locally using SBINTVEC, rather
58 // than delegating to our parent bhndb device.
59 //
60 
61 static int	siba_bhndb_wars_hwup(struct siba_softc *sc);
62 
63 /* Bridge-specific core device quirks */
64 enum {
65 	/** When PCIe-bridged, the D11 core's initiator request
66 	 *  timeout must be disabled to prevent D11 from entering a
67 	 *  RESP_TIMEOUT error state. */
68 	SIBA_QUIRK_PCIE_D11_SB_TIMEOUT	= (1<<0)
69 };
70 
71 static struct bhnd_device_quirk bridge_quirks[] = {
72 	BHND_CHIP_QUIRK(4311, HWREV_EQ(2), SIBA_QUIRK_PCIE_D11_SB_TIMEOUT),
73 	BHND_CHIP_QUIRK(4312, HWREV_EQ(0), SIBA_QUIRK_PCIE_D11_SB_TIMEOUT),
74 };
75 
76 static struct bhnd_device bridge_devs[] = {
77 	BHND_DEVICE(BCM, PCI, NULL, bridge_quirks),
78 };
79 
80 static int
81 siba_bhndb_probe(device_t dev)
82 {
83 	const struct bhnd_chipid	*cid;
84 	int				 error;
85 
86 	/* Defer to default probe implementation */
87 	if ((error = siba_probe(dev)) > 0)
88 		return (error);
89 
90 	/* Check bus type */
91 	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
92 	if (cid->chip_type != BHND_CHIPTYPE_SIBA)
93 		return (ENXIO);
94 
95 	/* Set device description */
96 	bhnd_set_default_bus_desc(dev, cid);
97 
98 	return (error);
99 }
100 
101 static int
102 siba_bhndb_attach(device_t dev)
103 {
104 	struct siba_softc		*sc;
105 	const struct bhnd_chipid	*chipid;
106 	int				 error;
107 
108 	sc = device_get_softc(dev);
109 
110 	/* Enumerate our children. */
111 	chipid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
112 	if ((error = siba_add_children(dev, chipid)))
113 		return (error);
114 
115 	/* Initialize full bridge configuration */
116 	error = BHNDB_INIT_FULL_CONFIG(device_get_parent(dev), dev,
117 	    bhndb_siba_priority_table);
118 	if (error)
119 		return (error);
120 
121 	/* Ask our parent bridge to find the corresponding bridge core */
122 	sc->hostb_dev = BHNDB_FIND_HOSTB_DEVICE(device_get_parent(dev), dev);
123 
124 	/* Call our superclass' implementation */
125 	if ((error = siba_attach(dev)))
126 		return (error);
127 
128 	/* Apply attach/resume work-arounds */
129 	if ((error = siba_bhndb_wars_hwup(sc)))
130 		return (error);
131 
132 	return (0);
133 }
134 
135 static int
136 siba_bhndb_resume(device_t dev)
137 {
138 	struct siba_softc	*sc;
139 	int			 error;
140 
141 	sc = device_get_softc(dev);
142 
143 	/* Apply attach/resume work-arounds */
144 	if ((error = siba_bhndb_wars_hwup(sc)))
145 		return (error);
146 
147 	/* Call our superclass' implementation */
148 	return (siba_resume(dev));
149 }
150 
151 /* Suspend all references to the device's cfg register blocks */
152 static void
153 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) {
154 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
155 		if (dinfo->cfg[i] == NULL)
156 			continue;
157 
158 		BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
159 		    SYS_RES_MEMORY, dinfo->cfg[i]->res);
160 	}
161 }
162 
163 static int
164 siba_bhndb_suspend_child(device_t dev, device_t child)
165 {
166 	struct siba_devinfo	*dinfo;
167 	int			 error;
168 
169 	if (device_get_parent(child) != dev)
170 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
171 
172 	dinfo = device_get_ivars(child);
173 
174 	/* Suspend the child */
175 	if ((error = bhnd_generic_br_suspend_child(dev, child)))
176 		return (error);
177 
178 	/* Suspend resource references to the child's config registers */
179 	siba_bhndb_suspend_cfgblocks(dev, dinfo);
180 
181 	return (0);
182 }
183 
184 static int
185 siba_bhndb_resume_child(device_t dev, device_t child)
186 {
187 	struct siba_devinfo	*dinfo;
188 	int			 error;
189 
190 	if (device_get_parent(child) != dev)
191 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
192 
193 	if (!device_is_suspended(child))
194 		return (EBUSY);
195 
196 	dinfo = device_get_ivars(child);
197 
198 	/* Resume all resource references to the child's config registers */
199 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
200 		if (dinfo->cfg[i] == NULL)
201 			continue;
202 
203 		error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
204 		    SYS_RES_MEMORY, dinfo->cfg[i]->res);
205 		if (error) {
206 			siba_bhndb_suspend_cfgblocks(dev, dinfo);
207 			return (error);
208 		}
209 	}
210 
211 	/* Resume the child */
212 	if ((error = bhnd_generic_br_resume_child(dev, child))) {
213 		siba_bhndb_suspend_cfgblocks(dev, dinfo);
214 		return (error);
215 	}
216 
217 	return (0);
218 }
219 
220 /* Work-around implementation for SIBA_QUIRK_PCIE_D11_SB_TIMEOUT */
221 static int
222 siba_bhndb_wars_pcie_clear_d11_timeout(struct siba_softc *sc)
223 {
224 	struct siba_devinfo	*dinfo;
225 	device_t		 d11;
226 	uint32_t		 imcfg;
227 
228 	/* Only applies when bridged by PCIe */
229 	if (bhnd_get_class(sc->hostb_dev) != BHND_DEVCLASS_PCIE)
230 		return (0);
231 
232 	/* Only applies if there's a D11 core */
233 	d11 = bhnd_match_child(sc->dev, &(struct bhnd_core_match) {
234 		BHND_MATCH_CORE(BHND_MFGID_BCM, BHND_COREID_D11),
235 		BHND_MATCH_CORE_UNIT(0)
236 	});
237 	if (d11 == NULL)
238 		return (0);
239 
240 	/* Clear initiator timeout in D11's CFG0 block */
241 	dinfo = device_get_ivars(d11);
242 	KASSERT(dinfo->cfg[0] != NULL, ("missing core config mapping"));
243 
244 	imcfg = bhnd_bus_read_4(dinfo->cfg[0], SIBA_CFG0_IMCONFIGLOW);
245 	imcfg &= ~SIBA_IMCL_RTO_MASK;
246 
247 	bhnd_bus_write_4(dinfo->cfg[0], SIBA_CFG0_IMCONFIGLOW, imcfg);
248 
249 	return (0);
250 }
251 
252 /**
253  * Apply any hardware workarounds that are required upon attach or resume
254  * of the bus.
255  */
256 static int
257 siba_bhndb_wars_hwup(struct siba_softc *sc)
258 {
259 	uint32_t		 quirks;
260 	int			 error;
261 
262 	quirks = bhnd_device_quirks(sc->hostb_dev, bridge_devs,
263 	    sizeof(bridge_devs[0]));
264 
265 	if (quirks & SIBA_QUIRK_PCIE_D11_SB_TIMEOUT) {
266 		if ((error = siba_bhndb_wars_pcie_clear_d11_timeout(sc)))
267 			return (error);
268 	}
269 
270 	return (0);
271 }
272 
273 
274 static device_method_t siba_bhndb_methods[] = {
275 	/* Device interface */
276 	DEVMETHOD(device_probe,			siba_bhndb_probe),
277 	DEVMETHOD(device_attach,		siba_bhndb_attach),
278 	DEVMETHOD(device_resume,		siba_bhndb_resume),
279 
280 	/* Bus interface */
281 	DEVMETHOD(bus_suspend_child,		siba_bhndb_suspend_child),
282 	DEVMETHOD(bus_resume_child,		siba_bhndb_resume_child),
283 
284 	DEVMETHOD_END
285 };
286 
287 DEFINE_CLASS_2(bhnd, siba_bhndb_driver, siba_bhndb_methods,
288     sizeof(struct siba_softc), bhnd_bhndb_driver, siba_driver);
289 
290 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, bhnd_devclass, NULL, NULL);
291 
292 MODULE_VERSION(siba_bhndb, 1);
293 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1);
294 MODULE_DEPEND(siba_bhndb, bhnd, 1, 1, 1);
295 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1);
296