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