ofw_pci.c revision 292789
1193323Sed/*-
2193323Sed * Copyright (c) 1999, 2000 Matthew R. Green
3193323Sed * Copyright (c) 2001 - 2003 by Thomas Moestl <tmm@FreeBSD.org>
4193323Sed * Copyright (c) 2005 - 2015 by Marius Strobl <marius@FreeBSD.org>
5193323Sed * All rights reserved.
6193323Sed *
7193323Sed * Redistribution and use in source and binary forms, with or without
8193323Sed * modification, are permitted provided that the following conditions
9193323Sed * are met:
10193323Sed * 1. Redistributions of source code must retain the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer.
12193323Sed * 2. Redistributions in binary form must reproduce the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer in the
14193323Sed *    documentation and/or other materials provided with the distribution.
15193323Sed * 3. The name of the author may not be used to endorse or promote products
16193323Sed *    derived from this software without specific prior written permission.
17193323Sed *
18193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19193323Sed * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20193323Sed * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21193323Sed * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22193323Sed * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23193323Sed * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24193323Sed * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25193323Sed * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26193323Sed * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27193323Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28193323Sed * SUCH DAMAGE.
29193323Sed *
30193323Sed *	from: NetBSD: psycho.c,v 1.35 2001/09/10 16:17:06 eeh Exp
31193323Sed */
32193323Sed
33198090Srdivacky#include <sys/cdefs.h>
34198090Srdivacky__FBSDID("$FreeBSD: stable/10/sys/sparc64/pci/ofw_pci.c 292789 2015-12-27 19:37:47Z marius $");
35193323Sed
36193323Sed#include "opt_ofw_pci.h"
37193323Sed
38193323Sed#include <sys/param.h>
39193323Sed#include <sys/systm.h>
40193323Sed#include <sys/bus.h>
41193323Sed#include <sys/kernel.h>
42193323Sed#include <sys/rman.h>
43193323Sed
44193323Sed#include <dev/ofw/ofw_bus.h>
45193323Sed#include <dev/ofw/ofw_pci.h>
46193323Sed#include <dev/ofw/openfirm.h>
47193323Sed
48193323Sed#include <dev/pci/pcireg.h>
49193323Sed#include <dev/pci/pcivar.h>
50193323Sed
51193323Sed#include <machine/asi.h>
52193323Sed#include <machine/bus.h>
53193323Sed#include <machine/bus_private.h>
54193323Sed#include <machine/cpufunc.h>
55193323Sed#include <machine/fsr.h>
56193323Sed#include <machine/resource.h>
57193323Sed
58193323Sed#include <sparc64/pci/ofw_pci.h>
59193323Sed
60193323Sed/* XXX */
61193323Sedextern struct bus_space_tag nexus_bustag;
62193323Sed
63193323Sedint
64193323Sedofw_pci_attach_common(device_t dev, bus_dma_tag_t dmat, u_long iosize,
65193323Sed    u_long memsize)
66193323Sed{
67193323Sed	struct ofw_pci_softc *sc;
68193323Sed	struct ofw_pci_ranges *range;
69193323Sed	phandle_t node;
70193323Sed	uint32_t prop_array[2];
71193323Sed	u_int i, j, nrange;
72193323Sed
73193323Sed	sc = device_get_softc(dev);
74193323Sed	node = ofw_bus_get_node(dev);
75198090Srdivacky	sc->sc_node = node;
76198090Srdivacky	sc->sc_pci_dmat = dmat;
77198090Srdivacky
78198090Srdivacky	/* Initialize memory and I/O rmans. */
79198090Srdivacky	sc->sc_pci_io_rman.rm_type = RMAN_ARRAY;
80198090Srdivacky	sc->sc_pci_io_rman.rm_descr = "PCI I/O Ports";
81193323Sed	if (rman_init(&sc->sc_pci_io_rman) != 0 ||
82193323Sed	    rman_manage_region(&sc->sc_pci_io_rman, 0, iosize) != 0) {
83193323Sed		device_printf(dev, "failed to set up I/O rman\n");
84193323Sed		return (ENXIO);
85193323Sed	}
86193323Sed	sc->sc_pci_mem_rman.rm_type = RMAN_ARRAY;
87193323Sed	sc->sc_pci_mem_rman.rm_descr = "PCI Memory";
88193323Sed	if (rman_init(&sc->sc_pci_mem_rman) != 0 ||
89193323Sed	    rman_manage_region(&sc->sc_pci_mem_rman, 0, memsize) != 0) {
90193323Sed		device_printf(dev, "failed to set up memory rman\n");
91193323Sed		return (ENXIO);
92193323Sed	}
93193323Sed
94193323Sed	/*
95193323Sed	 * Find the addresses of the various bus spaces.  The physical
96193323Sed	 * start addresses of the ranges are the configuration, I/O and
97193323Sed	 * memory handles.  There should not be multiple ones of one kind.
98193323Sed	 */
99193323Sed	nrange = OF_getprop_alloc(node, "ranges", sizeof(*range),
100193323Sed	    (void **)&range);
101193323Sed	for (i = 0; i < nrange; i++) {
102193323Sed		j = OFW_PCI_RANGE_CS(&range[i]);
103193323Sed		if (sc->sc_pci_bh[j] != 0) {
104193323Sed			device_printf(dev, "duplicate range for space %d\n",
105193323Sed			    j);
106193323Sed			free(range, M_OFWPROP);
107193323Sed			return (EINVAL);
108193323Sed		}
109198090Srdivacky		sc->sc_pci_bh[j] = OFW_PCI_RANGE_PHYS(&range[i]);
110193323Sed	}
111193323Sed	free(range, M_OFWPROP);
112193323Sed
113193323Sed	/*
114193323Sed	 * Make sure that the expected ranges are actually present.
115193323Sed	 * The OFW_PCI_CS_MEM64 one is not currently used.
116193323Sed	 */
117193323Sed	if (sc->sc_pci_bh[OFW_PCI_CS_CONFIG] == 0) {
118193323Sed		device_printf(dev, "missing CONFIG range\n");
119193323Sed		return (ENXIO);
120193323Sed	}
121193323Sed	if (sc->sc_pci_bh[OFW_PCI_CS_IO] == 0) {
122193323Sed		device_printf(dev, "missing IO range\n");
123198090Srdivacky		return (ENXIO);
124198090Srdivacky	}
125193323Sed	if (sc->sc_pci_bh[OFW_PCI_CS_MEM32] == 0) {
126193323Sed		device_printf(dev, "missing MEM32 range\n");
127193323Sed		return (ENXIO);
128193323Sed	}
129193323Sed
130193323Sed	/* Allocate our tags. */
131193323Sed	sc->sc_pci_iot = sparc64_alloc_bus_tag(NULL, &nexus_bustag,
132193323Sed	    PCI_IO_BUS_SPACE, NULL);
133193323Sed	if (sc->sc_pci_iot == NULL) {
134193323Sed		device_printf(dev, "could not allocate PCI I/O tag\n");
135193323Sed		return (ENXIO);
136193323Sed	}
137193323Sed	sc->sc_pci_cfgt = sparc64_alloc_bus_tag(NULL, &nexus_bustag,
138193323Sed	    PCI_CONFIG_BUS_SPACE, NULL);
139193323Sed	if (sc->sc_pci_cfgt == NULL) {
140198090Srdivacky		device_printf(dev,
141198090Srdivacky		    "could not allocate PCI configuration space tag\n");
142193323Sed		return (ENXIO);
143193323Sed	}
144193323Sed
145193323Sed	/*
146193323Sed	 * Get the bus range from the firmware.
147193323Sed	 */
148193323Sed	i = OF_getprop(node, "bus-range", (void *)prop_array,
149193323Sed	    sizeof(prop_array));
150198090Srdivacky	if (i == -1) {
151193323Sed		device_printf(dev, "could not get bus-range\n");
152193323Sed		return (ENXIO);
153193323Sed	}
154193323Sed	if (i != sizeof(prop_array)) {
155193323Sed		device_printf(dev, "broken bus-range (%d)", i);
156193323Sed		return (EINVAL);
157193323Sed	}
158193323Sed	sc->sc_pci_secbus = prop_array[0];
159193323Sed	sc->sc_pci_subbus = prop_array[1];
160193323Sed	if (bootverbose != 0)
161193323Sed		device_printf(dev, "bus range %u to %u; PCI bus %d\n",
162193323Sed		    sc->sc_pci_secbus, sc->sc_pci_subbus, sc->sc_pci_secbus);
163193323Sed
164193323Sed	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(ofw_pci_intr_t));
165193323Sed
166193323Sed	return (0);
167193323Sed}
168193323Sed
169193323Seduint32_t
170193323Sedofw_pci_read_config_common(device_t dev, u_int regmax, u_long offset,
171193323Sed    u_int bus, u_int slot, u_int func, u_int reg, int width)
172193323Sed{
173193323Sed	struct ofw_pci_softc *sc;
174193323Sed	bus_space_handle_t bh;
175193323Sed	uint32_t r, wrd;
176193323Sed	int i;
177193323Sed	uint16_t shrt;
178193323Sed	uint8_t byte;
179193323Sed
180193323Sed	sc = device_get_softc(dev);
181193323Sed	if (bus < sc->sc_pci_secbus || bus > sc->sc_pci_subbus ||
182193323Sed	    slot > PCI_SLOTMAX || func > PCI_FUNCMAX || reg > regmax)
183193323Sed		return (-1);
184193323Sed
185193323Sed	bh = sc->sc_pci_bh[OFW_PCI_CS_CONFIG];
186193323Sed	switch (width) {
187193323Sed	case 1:
188193323Sed		i = bus_space_peek_1(sc->sc_pci_cfgt, bh, offset, &byte);
189193323Sed		r = byte;
190193323Sed		break;
191193323Sed	case 2:
192193323Sed		i = bus_space_peek_2(sc->sc_pci_cfgt, bh, offset, &shrt);
193193323Sed		r = shrt;
194193323Sed		break;
195193323Sed	case 4:
196193323Sed		i = bus_space_peek_4(sc->sc_pci_cfgt, bh, offset, &wrd);
197193323Sed		r = wrd;
198193323Sed		break;
199193323Sed	default:
200193323Sed		panic("%s: bad width %d", __func__, width);
201193323Sed		/* NOTREACHED */
202193323Sed	}
203193323Sed
204193323Sed	if (i) {
205193323Sed#ifdef OFW_PCI_DEBUG
206193323Sed		printf("%s: read data error reading: %d.%d.%d: 0x%x\n",
207193323Sed		    __func__, bus, slot, func, reg);
208193323Sed#endif
209193323Sed		r = -1;
210	}
211	return (r);
212}
213
214void
215ofw_pci_write_config_common(device_t dev, u_int regmax, u_long offset,
216    u_int bus, u_int slot, u_int func, u_int reg, uint32_t val, int width)
217{
218	struct ofw_pci_softc *sc;
219	bus_space_handle_t bh;
220
221	sc = device_get_softc(dev);
222	if (bus < sc->sc_pci_secbus || bus > sc->sc_pci_subbus ||
223	    slot > PCI_SLOTMAX || func > PCI_FUNCMAX || reg > regmax)
224		return;
225
226	bh = sc->sc_pci_bh[OFW_PCI_CS_CONFIG];
227	switch (width) {
228	case 1:
229		bus_space_write_1(sc->sc_pci_cfgt, bh, offset, val);
230		break;
231	case 2:
232		bus_space_write_2(sc->sc_pci_cfgt, bh, offset, val);
233		break;
234	case 4:
235		bus_space_write_4(sc->sc_pci_cfgt, bh, offset, val);
236		break;
237	default:
238		panic("%s: bad width %d", __func__, width);
239		/* NOTREACHED */
240	}
241}
242
243ofw_pci_intr_t
244ofw_pci_route_interrupt_common(device_t bridge, device_t dev, int pin)
245{
246	struct ofw_pci_softc *sc;
247	struct ofw_pci_register reg;
248	ofw_pci_intr_t pintr, mintr;
249
250	sc = device_get_softc(bridge);
251	pintr = pin;
252	if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo,
253	    &reg, sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
254	    NULL) != 0)
255		return (mintr);
256	return (PCI_INVALID_IRQ);
257}
258
259void
260ofw_pci_dmamap_sync_stst_order_common(void)
261{
262	static u_char buf[VIS_BLOCKSIZE] __aligned(VIS_BLOCKSIZE);
263	register_t reg, s;
264
265	s = intr_disable();
266	reg = rd(fprs);
267	wr(fprs, reg | FPRS_FEF, 0);
268	__asm __volatile("stda %%f0, [%0] %1"
269	    : : "r" (buf), "n" (ASI_BLK_COMMIT_S));
270	membar(Sync);
271	wr(fprs, reg, 0);
272	intr_restore(s);
273}
274
275int
276ofw_pci_read_ivar(device_t dev, device_t child __unused, int which,
277    uintptr_t *result)
278{
279	struct ofw_pci_softc *sc;
280
281	switch (which) {
282	case PCIB_IVAR_DOMAIN:
283		*result = device_get_unit(dev);
284		return (0);
285	case PCIB_IVAR_BUS:
286		sc = device_get_softc(dev);
287		*result = sc->sc_pci_secbus;
288		return (0);
289	}
290	return (ENOENT);
291}
292
293struct resource *
294ofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
295    u_long start, u_long end, u_long count, u_int flags)
296{
297	struct ofw_pci_softc *sc;
298	struct resource *rv;
299	struct rman *rm;
300
301	sc = device_get_softc(bus);
302	switch (type) {
303	case SYS_RES_IRQ:
304		/*
305		 * XXX: Don't accept blank ranges for now, only single
306		 * interrupts.  The other case should not happen with
307		 * the MI PCI code ...
308		 * XXX: This may return a resource that is out of the
309		 * range that was specified.  Is this correct ...?
310		 */
311		if (start != end)
312			panic("%s: XXX: interrupt range", __func__);
313		return (bus_generic_alloc_resource(bus, child, type, rid,
314		    start, end, count, flags));
315	case SYS_RES_MEMORY:
316		rm = &sc->sc_pci_mem_rman;
317		break;
318	case SYS_RES_IOPORT:
319		rm = &sc->sc_pci_io_rman;
320		break;
321	default:
322		return (NULL);
323	}
324
325	rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE,
326	    child);
327	if (rv == NULL)
328		return (NULL);
329	rman_set_rid(rv, *rid);
330
331	if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type,
332	    *rid, rv) != 0) {
333		rman_release_resource(rv);
334		return (NULL);
335	}
336	return (rv);
337}
338
339int
340ofw_pci_activate_resource(device_t bus, device_t child, int type, int rid,
341    struct resource *r)
342{
343	struct ofw_pci_softc *sc;
344	struct bus_space_tag *tag;
345
346	sc = device_get_softc(bus);
347	switch (type) {
348	case SYS_RES_IRQ:
349		return (bus_generic_activate_resource(bus, child, type, rid,
350		    r));
351	case SYS_RES_MEMORY:
352		tag = sparc64_alloc_bus_tag(r, &nexus_bustag,
353		    PCI_MEMORY_BUS_SPACE, NULL);
354		if (tag == NULL)
355			return (ENOMEM);
356		rman_set_bustag(r, tag);
357		rman_set_bushandle(r, sc->sc_pci_bh[OFW_PCI_CS_MEM32] +
358		    rman_get_start(r));
359		break;
360	case SYS_RES_IOPORT:
361		rman_set_bustag(r, sc->sc_pci_iot);
362		rman_set_bushandle(r, sc->sc_pci_bh[OFW_PCI_CS_IO] +
363		    rman_get_start(r));
364		break;
365	}
366	return (rman_activate_resource(r));
367}
368
369int
370ofw_pci_adjust_resource(device_t bus, device_t child, int type,
371    struct resource *r, u_long start, u_long end)
372{
373	struct ofw_pci_softc *sc;
374	struct rman *rm;
375
376	sc = device_get_softc(bus);
377	switch (type) {
378	case SYS_RES_IRQ:
379		return (bus_generic_adjust_resource(bus, child, type, r,
380		    start, end));
381	case SYS_RES_MEMORY:
382		rm = &sc->sc_pci_mem_rman;
383		break;
384	case SYS_RES_IOPORT:
385		rm = &sc->sc_pci_io_rman;
386		break;
387	default:
388		return (EINVAL);
389	}
390	if (rman_is_region_manager(r, rm) == 0)
391		return (EINVAL);
392	return (rman_adjust_resource(r, start, end));
393}
394
395bus_dma_tag_t
396ofw_pci_get_dma_tag(device_t bus, device_t child __unused)
397{
398	struct ofw_pci_softc *sc;
399
400	sc = device_get_softc(bus);
401	return (sc->sc_pci_dmat);
402}
403
404phandle_t
405ofw_pci_get_node(device_t bus, device_t child __unused)
406{
407	struct ofw_pci_softc *sc;
408
409	sc = device_get_softc(bus);
410	/* We only have one child, the PCI bus, which needs our own node. */
411	return (sc->sc_node);
412}
413