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: stable/11/sys/dev/bhnd/siba/siba_bhndb.c 331044 2018-03-16 02:44:18Z eadler $");
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
61static int	siba_bhndb_wars_hwup(struct siba_softc *sc);
62
63/* Bridge-specific core device quirks */
64enum {
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
71static 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
77static struct bhnd_device bridge_devs[] = {
78	BHND_DEVICE(BCM, PCI, NULL, bridge_quirks),
79	BHND_DEVICE_END
80};
81
82static int
83siba_bhndb_probe(device_t dev)
84{
85	const struct bhnd_chipid *cid;
86
87	/* Check bus type */
88	cid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
89	if (cid->chip_type != BHND_CHIPTYPE_SIBA)
90		return (ENXIO);
91
92	/* Delegate to default probe implementation */
93	return (siba_probe(dev));
94}
95
96static int
97siba_bhndb_attach(device_t dev)
98{
99	struct siba_softc		*sc;
100	const struct bhnd_chipid	*chipid;
101	int				 error;
102
103	sc = device_get_softc(dev);
104
105	/* Enumerate our children. */
106	chipid = BHNDB_GET_CHIPID(device_get_parent(dev), dev);
107	if ((error = siba_add_children(dev, chipid)))
108		return (error);
109
110	/* Initialize full bridge configuration */
111	error = BHNDB_INIT_FULL_CONFIG(device_get_parent(dev), dev,
112	    bhndb_siba_priority_table);
113	if (error)
114		return (error);
115
116	/* Ask our parent bridge to find the corresponding bridge core */
117	sc->hostb_dev = BHNDB_FIND_HOSTB_DEVICE(device_get_parent(dev), dev);
118
119	/* Call our superclass' implementation */
120	if ((error = siba_attach(dev)))
121		return (error);
122
123	/* Apply attach/resume work-arounds */
124	if ((error = siba_bhndb_wars_hwup(sc)))
125		return (error);
126
127	return (0);
128}
129
130static int
131siba_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 */
147static void
148siba_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
158static int
159siba_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
179static int
180siba_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 */
216static int
217siba_bhndb_wars_pcie_clear_d11_timeout(struct siba_softc *sc)
218{
219	struct siba_devinfo	*dinfo;
220	device_t		 d11;
221	uint32_t		 imcfg;
222
223	/* Only applies when bridged by PCIe */
224	if (bhnd_get_class(sc->hostb_dev) != BHND_DEVCLASS_PCIE)
225		return (0);
226
227	/* Only applies if there's a D11 core */
228	d11 = bhnd_match_child(sc->dev, &(struct bhnd_core_match) {
229		BHND_MATCH_CORE(BHND_MFGID_BCM, BHND_COREID_D11),
230		BHND_MATCH_CORE_UNIT(0)
231	});
232	if (d11 == NULL)
233		return (0);
234
235	/* Clear initiator timeout in D11's CFG0 block */
236	dinfo = device_get_ivars(d11);
237	KASSERT(dinfo->cfg[0] != NULL, ("missing core config mapping"));
238
239	imcfg = bhnd_bus_read_4(dinfo->cfg[0], SIBA_CFG0_IMCONFIGLOW);
240	imcfg &= ~SIBA_IMCL_RTO_MASK;
241
242	bhnd_bus_write_4(dinfo->cfg[0], SIBA_CFG0_IMCONFIGLOW, imcfg);
243
244	return (0);
245}
246
247/**
248 * Apply any hardware workarounds that are required upon attach or resume
249 * of the bus.
250 */
251static int
252siba_bhndb_wars_hwup(struct siba_softc *sc)
253{
254	uint32_t		 quirks;
255	int			 error;
256
257	quirks = bhnd_device_quirks(sc->hostb_dev, bridge_devs,
258	    sizeof(bridge_devs[0]));
259
260	if (quirks & SIBA_QUIRK_PCIE_D11_SB_TIMEOUT) {
261		if ((error = siba_bhndb_wars_pcie_clear_d11_timeout(sc)))
262			return (error);
263	}
264
265	return (0);
266}
267
268
269static device_method_t siba_bhndb_methods[] = {
270	/* Device interface */
271	DEVMETHOD(device_probe,			siba_bhndb_probe),
272	DEVMETHOD(device_attach,		siba_bhndb_attach),
273	DEVMETHOD(device_resume,		siba_bhndb_resume),
274
275	/* Bus interface */
276	DEVMETHOD(bus_suspend_child,		siba_bhndb_suspend_child),
277	DEVMETHOD(bus_resume_child,		siba_bhndb_resume_child),
278
279	DEVMETHOD_END
280};
281
282DEFINE_CLASS_2(bhnd, siba_bhndb_driver, siba_bhndb_methods,
283    sizeof(struct siba_softc), bhnd_bhndb_driver, siba_driver);
284
285DRIVER_MODULE(siba_bhndb, bhndb, siba_bhndb_driver, bhnd_devclass, NULL, NULL);
286
287MODULE_VERSION(siba_bhndb, 1);
288MODULE_DEPEND(siba_bhndb, siba, 1, 1, 1);
289MODULE_DEPEND(siba_bhndb, bhnd, 1, 1, 1);
290MODULE_DEPEND(siba_bhndb, bhndb, 1, 1, 1);
291