xref: /freebsd/sys/dev/bhnd/siba/siba_bhndb.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
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 	BHND_DEVICE_QUIRK_END
75 };
76 
77 static struct bhnd_device bridge_devs[] = {
78 	BHND_DEVICE(BCM, PCI, NULL, bridge_quirks),
79 	BHND_DEVICE_END
80 };
81 
82 static int
83 siba_bhndb_probe(device_t dev)
84 {
85 	const struct bhnd_chipid	*cid;
86 	int				 error;
87 
88 	/* Defer to default probe implementation */
89 	if ((error = siba_probe(dev)) > 0)
90 		return (error);
91 
92 	/* Check bus type */
93 	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
94 	if (cid->chip_type != BHND_CHIPTYPE_SIBA)
95 		return (ENXIO);
96 
97 	/* Set device description */
98 	bhnd_set_default_bus_desc(dev, cid);
99 
100 	return (error);
101 }
102 
103 static int
104 siba_bhndb_attach(device_t dev)
105 {
106 	struct siba_softc	*sc;
107 	int			 error;
108 
109 	sc = device_get_softc(dev);
110 
111 	/* Perform initial attach and enumerate our children. */
112 	if ((error = siba_attach(dev)))
113 		goto failed;
114 
115 	/* Apply attach/resume workarounds before any child drivers attach */
116 	if ((error = siba_bhndb_wars_hwup(sc)))
117 		goto failed;
118 
119 	/* Delegate remainder to standard bhnd method implementation */
120 	if ((error = bhnd_generic_attach(dev)))
121 		goto failed;
122 
123 	return (0);
124 
125 failed:
126 	device_delete_children(dev);
127 	return (error);
128 }
129 
130 static int
131 siba_bhndb_resume(device_t dev)
132 {
133 	struct siba_softc	*sc;
134 	int			 error;
135 
136 	sc = device_get_softc(dev);
137 
138 	/* Apply attach/resume work-arounds */
139 	if ((error = siba_bhndb_wars_hwup(sc)))
140 		return (error);
141 
142 	/* Call our superclass' implementation */
143 	return (siba_resume(dev));
144 }
145 
146 /* Suspend all references to the device's cfg register blocks */
147 static void
148 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) {
149 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
150 		if (dinfo->cfg[i] == NULL)
151 			continue;
152 
153 		BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
154 		    SYS_RES_MEMORY, dinfo->cfg[i]->res);
155 	}
156 }
157 
158 static int
159 siba_bhndb_suspend_child(device_t dev, device_t child)
160 {
161 	struct siba_devinfo	*dinfo;
162 	int			 error;
163 
164 	if (device_get_parent(child) != dev)
165 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
166 
167 	dinfo = device_get_ivars(child);
168 
169 	/* Suspend the child */
170 	if ((error = bhnd_generic_br_suspend_child(dev, child)))
171 		return (error);
172 
173 	/* Suspend resource references to the child's config registers */
174 	siba_bhndb_suspend_cfgblocks(dev, dinfo);
175 
176 	return (0);
177 }
178 
179 static int
180 siba_bhndb_resume_child(device_t dev, device_t child)
181 {
182 	struct siba_devinfo	*dinfo;
183 	int			 error;
184 
185 	if (device_get_parent(child) != dev)
186 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
187 
188 	if (!device_is_suspended(child))
189 		return (EBUSY);
190 
191 	dinfo = device_get_ivars(child);
192 
193 	/* Resume all resource references to the child's config registers */
194 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
195 		if (dinfo->cfg[i] == NULL)
196 			continue;
197 
198 		error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
199 		    SYS_RES_MEMORY, dinfo->cfg[i]->res);
200 		if (error) {
201 			siba_bhndb_suspend_cfgblocks(dev, dinfo);
202 			return (error);
203 		}
204 	}
205 
206 	/* Resume the child */
207 	if ((error = bhnd_generic_br_resume_child(dev, child))) {
208 		siba_bhndb_suspend_cfgblocks(dev, dinfo);
209 		return (error);
210 	}
211 
212 	return (0);
213 }
214 
215 /* Work-around implementation for SIBA_QUIRK_PCIE_D11_SB_TIMEOUT */
216 static int
217 siba_bhndb_wars_pcie_clear_d11_timeout(struct siba_softc *sc)
218 {
219 	struct siba_devinfo	*dinfo;
220 	device_t		 hostb_dev;
221 	device_t		 d11;
222 	uint32_t		 imcfg;
223 
224 	/* Only applies when bridged by PCIe */
225 	if ((hostb_dev = bhnd_find_hostb_device(sc->dev)) == NULL)
226 		return (ENXIO);
227 
228 	if (bhnd_get_class(hostb_dev) != BHND_DEVCLASS_PCIE)
229 		return (0);
230 
231 	/* Only applies if there's a D11 core */
232 	d11 = bhnd_match_child(sc->dev, &(struct bhnd_core_match) {
233 		BHND_MATCH_CORE(BHND_MFGID_BCM, BHND_COREID_D11),
234 		BHND_MATCH_CORE_UNIT(0)
235 	});
236 	if (d11 == NULL)
237 		return (0);
238 
239 	/* Clear initiator timeout in D11's CFG0 block */
240 	dinfo = device_get_ivars(d11);
241 	KASSERT(dinfo->cfg[0] != NULL, ("missing core config mapping"));
242 
243 	imcfg = bhnd_bus_read_4(dinfo->cfg[0], SIBA_CFG0_IMCONFIGLOW);
244 	imcfg &= ~SIBA_IMCL_RTO_MASK;
245 
246 	bhnd_bus_write_4(dinfo->cfg[0], SIBA_CFG0_IMCONFIGLOW, imcfg);
247 
248 	return (0);
249 }
250 
251 /**
252  * Apply any hardware workarounds that are required upon attach or resume
253  * of the bus.
254  */
255 static int
256 siba_bhndb_wars_hwup(struct siba_softc *sc)
257 {
258 	device_t		 hostb_dev;
259 	uint32_t		 quirks;
260 	int			 error;
261 
262 	if ((hostb_dev = bhnd_find_hostb_device(sc->dev)) == NULL)
263 		return (ENXIO);
264 
265 	quirks = bhnd_device_quirks(hostb_dev, bridge_devs,
266 	    sizeof(bridge_devs[0]));
267 
268 	if (quirks & SIBA_QUIRK_PCIE_D11_SB_TIMEOUT) {
269 		if ((error = siba_bhndb_wars_pcie_clear_d11_timeout(sc)))
270 			return (error);
271 	}
272 
273 	return (0);
274 }
275 
276 static device_method_t siba_bhndb_methods[] = {
277 	/* Device interface */
278 	DEVMETHOD(device_probe,			siba_bhndb_probe),
279 	DEVMETHOD(device_attach,		siba_bhndb_attach),
280 	DEVMETHOD(device_resume,		siba_bhndb_resume),
281 
282 	/* Bus interface */
283 	DEVMETHOD(bus_suspend_child,		siba_bhndb_suspend_child),
284 	DEVMETHOD(bus_resume_child,		siba_bhndb_resume_child),
285 
286 	DEVMETHOD_END
287 };
288 
289 DEFINE_CLASS_2(bhnd, siba_bhndb_driver, siba_bhndb_methods,
290     sizeof(struct siba_softc), bhnd_bhndb_driver, siba_driver);
291 
292 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, bhnd_devclass, NULL, NULL);
293 
294 MODULE_VERSION(siba_bhndb, 1);
295 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1);
296 MODULE_DEPEND(siba_bhndb, bhnd, 1, 1, 1);
297 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1);
298