xref: /freebsd/sys/dev/bhnd/siba/siba_bhndb.c (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * Copyright (c) 2015-2016 Landon Fuller <landon@landonf.org>
3  * Copyright (c) 2017 The FreeBSD Foundation
4  * All rights reserved.
5  *
6  * Portions of this software were developed by Landon Fuller
7  * under sponsorship from the FreeBSD Foundation.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer,
14  *    without modification.
15  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
16  *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
17  *    redistribution must be conditioned upon including a substantially
18  *    similar Disclaimer requirement for further binary redistribution.
19  *
20  * NO WARRANTY
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
24  * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
25  * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
26  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
29  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGES.
32  */
33 
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36 
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/bus.h>
40 #include <sys/module.h>
41 #include <sys/systm.h>
42 
43 #include <dev/bhnd/bhnd_ids.h>
44 #include <dev/bhnd/bhndb/bhndbvar.h>
45 #include <dev/bhnd/bhndb/bhndb_hwdata.h>
46 
47 #include "sibareg.h"
48 #include "sibavar.h"
49 
50 /*
51  * Supports attachment of siba(4) bus devices via a bhndb bridge.
52  */
53 
54 struct siba_bhndb_softc;
55 
56 static int	siba_bhndb_wars_hwup(struct siba_bhndb_softc *sc);
57 
58 /* siba_bhndb per-instance state */
59 struct siba_bhndb_softc {
60 	struct siba_softc	siba;	/**< common siba per-instance state */
61 	uint32_t		quirks;	/**< bus-level quirks */
62 };
63 
64 /* siba_bhndb quirks */
65 enum {
66 	/** When PCIe-bridged, the D11 core's initiator request
67 	 *  timeout must be disabled to prevent D11 from entering a
68 	 *  RESP_TIMEOUT error state. */
69 	SIBA_QUIRK_PCIE_D11_SB_TIMEOUT	= (1<<0)
70 };
71 
72 /* Bus-level quirks when bridged via a PCI host bridge core */
73 static struct bhnd_device_quirk pci_bridge_quirks[] = {
74 	BHND_DEVICE_QUIRK_END
75 };
76 
77 /* Bus-level quirks when bridged via a PCIe host bridge core */
78 static struct bhnd_device_quirk pcie_bridge_quirks[] = {
79 	BHND_CHIP_QUIRK (4311, HWREV_EQ(2),	SIBA_QUIRK_PCIE_D11_SB_TIMEOUT),
80 	BHND_CHIP_QUIRK (4312, HWREV_ANY,	SIBA_QUIRK_PCIE_D11_SB_TIMEOUT),
81 	BHND_DEVICE_QUIRK_END
82 };
83 
84 /* Bus-level quirks specific to a particular host bridge core */
85 static struct bhnd_device bridge_devs[] = {
86 	BHND_DEVICE(BCM, PCI,	NULL, pci_bridge_quirks),
87 	BHND_DEVICE(BCM, PCIE,	NULL, pcie_bridge_quirks),
88 	BHND_DEVICE_END
89 };
90 
91 static int
92 siba_bhndb_probe(device_t dev)
93 {
94 	const struct bhnd_chipid	*cid;
95 	int				 error;
96 
97 	/* Defer to default probe implementation */
98 	if ((error = siba_probe(dev)) > 0)
99 		return (error);
100 
101 	/* Check bus type */
102 	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
103 	if (cid->chip_type != BHND_CHIPTYPE_SIBA)
104 		return (ENXIO);
105 
106 	/* Set device description */
107 	bhnd_set_default_bus_desc(dev, cid);
108 
109 	return (error);
110 }
111 
112 static int
113 siba_bhndb_attach(device_t dev)
114 {
115 	struct siba_bhndb_softc	*sc;
116 	device_t		 hostb;
117 	int			 error;
118 
119 	sc = device_get_softc(dev);
120 	sc->quirks = 0;
121 
122 	/* Perform initial attach and enumerate our children. */
123 	if ((error = siba_attach(dev)))
124 		return (error);
125 
126 	/* Fetch bus-level quirks required by the host bridge core */
127 	if ((hostb = bhnd_bus_find_hostb_device(dev)) != NULL) {
128 		sc->quirks |= bhnd_device_quirks(hostb, bridge_devs,
129 		    sizeof(bridge_devs[0]));
130 	}
131 
132 	/* Apply attach/resume workarounds before any child drivers attach */
133 	if ((error = siba_bhndb_wars_hwup(sc)))
134 		goto failed;
135 
136 	/* Delegate remainder to standard bhnd method implementation */
137 	if ((error = bhnd_generic_attach(dev)))
138 		goto failed;
139 
140 	return (0);
141 
142 failed:
143 	siba_detach(dev);
144 	return (error);
145 }
146 
147 static int
148 siba_bhndb_resume(device_t dev)
149 {
150 	struct siba_bhndb_softc	*sc;
151 	int			 error;
152 
153 	sc = device_get_softc(dev);
154 
155 	/* Apply attach/resume work-arounds */
156 	if ((error = siba_bhndb_wars_hwup(sc)))
157 		return (error);
158 
159 	/* Call our superclass' implementation */
160 	return (siba_resume(dev));
161 }
162 
163 /* Suspend all references to the device's cfg register blocks */
164 static void
165 siba_bhndb_suspend_cfgblocks(device_t dev, struct siba_devinfo *dinfo) {
166 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
167 		if (dinfo->cfg_res[i] == NULL)
168 			continue;
169 
170 		BHNDB_SUSPEND_RESOURCE(device_get_parent(dev), dev,
171 		    SYS_RES_MEMORY, dinfo->cfg_res[i]->res);
172 	}
173 }
174 
175 static int
176 siba_bhndb_suspend_child(device_t dev, device_t child)
177 {
178 	struct siba_devinfo	*dinfo;
179 	int			 error;
180 
181 	if (device_get_parent(child) != dev)
182 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
183 
184 	dinfo = device_get_ivars(child);
185 
186 	/* Suspend the child */
187 	if ((error = bhnd_generic_br_suspend_child(dev, child)))
188 		return (error);
189 
190 	/* Suspend resource references to the child's config registers */
191 	siba_bhndb_suspend_cfgblocks(dev, dinfo);
192 
193 	return (0);
194 }
195 
196 static int
197 siba_bhndb_resume_child(device_t dev, device_t child)
198 {
199 	struct siba_devinfo	*dinfo;
200 	int			 error;
201 
202 	if (device_get_parent(child) != dev)
203 		BUS_SUSPEND_CHILD(device_get_parent(dev), child);
204 
205 	if (!device_is_suspended(child))
206 		return (EBUSY);
207 
208 	dinfo = device_get_ivars(child);
209 
210 	/* Resume all resource references to the child's config registers */
211 	for (u_int i = 0; i < dinfo->core_id.num_cfg_blocks; i++) {
212 		if (dinfo->cfg_res[i] == NULL)
213 			continue;
214 
215 		error = BHNDB_RESUME_RESOURCE(device_get_parent(dev), dev,
216 		    SYS_RES_MEMORY, dinfo->cfg_res[i]->res);
217 		if (error) {
218 			siba_bhndb_suspend_cfgblocks(dev, dinfo);
219 			return (error);
220 		}
221 	}
222 
223 	/* Resume the child */
224 	if ((error = bhnd_generic_br_resume_child(dev, child))) {
225 		siba_bhndb_suspend_cfgblocks(dev, dinfo);
226 		return (error);
227 	}
228 
229 	return (0);
230 }
231 
232 /* Work-around implementation for SIBA_QUIRK_PCIE_D11_SB_TIMEOUT */
233 static int
234 siba_bhndb_wars_pcie_clear_d11_timeout(struct siba_bhndb_softc *sc)
235 {
236 	struct siba_devinfo	*dinfo;
237 	device_t		 d11;
238 	uint32_t		 imcfg;
239 
240 	/* Only applicable if there's a D11 core */
241 	d11 = bhnd_bus_match_child(sc->siba.dev, &(struct bhnd_core_match) {
242 		BHND_MATCH_CORE(BHND_MFGID_BCM, BHND_COREID_D11),
243 		BHND_MATCH_CORE_UNIT(0)
244 	});
245 	if (d11 == NULL)
246 		return (0);
247 
248 	/* Clear initiator timeout in D11's CFG0 block */
249 	dinfo = device_get_ivars(d11);
250 	KASSERT(dinfo->cfg_res[0] != NULL, ("missing core config mapping"));
251 
252 	imcfg = bhnd_bus_read_4(dinfo->cfg_res[0], SIBA_CFG0_IMCONFIGLOW);
253 	imcfg &= ~(SIBA_IMCL_RTO_MASK|SIBA_IMCL_STO_MASK);
254 
255 	bhnd_bus_write_4(dinfo->cfg_res[0], SIBA_CFG0_IMCONFIGLOW, imcfg);
256 
257 	return (0);
258 }
259 
260 /**
261  * Apply any hardware workarounds that are required upon attach or resume
262  * of the bus.
263  */
264 static int
265 siba_bhndb_wars_hwup(struct siba_bhndb_softc *sc)
266 {
267 	int error;
268 
269 	if (sc->quirks & SIBA_QUIRK_PCIE_D11_SB_TIMEOUT) {
270 		if ((error = siba_bhndb_wars_pcie_clear_d11_timeout(sc)))
271 			return (error);
272 	}
273 
274 	return (0);
275 }
276 
277 static device_method_t siba_bhndb_methods[] = {
278 	/* Device interface */
279 	DEVMETHOD(device_probe,			siba_bhndb_probe),
280 	DEVMETHOD(device_attach,		siba_bhndb_attach),
281 	DEVMETHOD(device_resume,		siba_bhndb_resume),
282 
283 	/* Bus interface */
284 	DEVMETHOD(bus_suspend_child,		siba_bhndb_suspend_child),
285 	DEVMETHOD(bus_resume_child,		siba_bhndb_resume_child),
286 
287 	DEVMETHOD_END
288 };
289 
290 DEFINE_CLASS_2(bhnd, siba_bhndb_driver, siba_bhndb_methods,
291     sizeof(struct siba_softc), bhnd_bhndb_driver, siba_driver);
292 
293 DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, NULL, NULL);
294 
295 MODULE_VERSION(siba_bhndb, 1);
296 MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1);
297 MODULE_DEPEND(siba_bhndb, bhnd, 1, 1, 1);
298 MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1);
299