ofw_pci.c revision 230993
1/*-
2 * Copyright (c) 2011 Nathan Whitehorn
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 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD: head/sys/powerpc/ofw/ofw_pci.c 230993 2012-02-04 19:54:13Z nwhitehorn $");
29#include <sys/param.h>
30#include <sys/systm.h>
31#include <sys/module.h>
32#include <sys/bus.h>
33#include <sys/conf.h>
34#include <sys/kernel.h>
35
36#include <dev/ofw/openfirm.h>
37#include <dev/ofw/ofw_pci.h>
38#include <dev/ofw/ofw_bus.h>
39#include <dev/ofw/ofw_bus_subr.h>
40
41#include <dev/pci/pcivar.h>
42#include <dev/pci/pcireg.h>
43
44#include <machine/bus.h>
45#include <machine/intr_machdep.h>
46#include <machine/md_var.h>
47#include <machine/pio.h>
48#include <machine/resource.h>
49
50#include <sys/rman.h>
51
52#include <vm/vm.h>
53#include <vm/pmap.h>
54
55#include <powerpc/ofw/ofw_pci.h>
56
57#include "pcib_if.h"
58
59/*
60 * Bus interface.
61 */
62static int		ofw_pci_read_ivar(device_t, device_t, int,
63			    uintptr_t *);
64static struct		resource * ofw_pci_alloc_resource(device_t bus,
65			    device_t child, int type, int *rid, u_long start,
66			    u_long end, u_long count, u_int flags);
67static int		ofw_pci_release_resource(device_t bus, device_t child,
68    			    int type, int rid, struct resource *res);
69static int		ofw_pci_activate_resource(device_t bus, device_t child,
70			    int type, int rid, struct resource *res);
71static int		ofw_pci_deactivate_resource(device_t bus,
72    			    device_t child, int type, int rid,
73    			    struct resource *res);
74
75/*
76 * pcib interface.
77 */
78static int		ofw_pci_maxslots(device_t);
79static int		ofw_pci_route_interrupt(device_t, device_t, int);
80
81/*
82 * ofw_bus interface
83 */
84static phandle_t ofw_pci_get_node(device_t bus, device_t dev);
85
86/*
87 * local methods
88 */
89
90static int ofw_pci_nranges(phandle_t node);
91static int ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges);
92
93/*
94 * Driver methods.
95 */
96static device_method_t	ofw_pci_methods[] = {
97	/* Device interface */
98	DEVMETHOD(device_attach,	ofw_pci_attach),
99
100	/* Bus interface */
101	DEVMETHOD(bus_print_child,	bus_generic_print_child),
102	DEVMETHOD(bus_read_ivar,	ofw_pci_read_ivar),
103	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
104	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
105	DEVMETHOD(bus_alloc_resource,	ofw_pci_alloc_resource),
106	DEVMETHOD(bus_release_resource,	ofw_pci_release_resource),
107	DEVMETHOD(bus_activate_resource,	ofw_pci_activate_resource),
108	DEVMETHOD(bus_deactivate_resource,	ofw_pci_deactivate_resource),
109
110	/* pcib interface */
111	DEVMETHOD(pcib_maxslots,	ofw_pci_maxslots),
112	DEVMETHOD(pcib_route_interrupt,	ofw_pci_route_interrupt),
113
114	/* ofw_bus interface */
115	DEVMETHOD(ofw_bus_get_node,     ofw_pci_get_node),
116
117	DEVMETHOD_END
118};
119
120DEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0);
121
122int
123ofw_pci_attach(device_t dev)
124{
125	struct		ofw_pci_softc *sc;
126	phandle_t	node;
127	u_int32_t	busrange[2];
128	struct		ofw_pci_range *rp;
129	int		error;
130
131	node = ofw_bus_get_node(dev);
132	sc = device_get_softc(dev);
133
134	if (OF_getprop(node, "reg", &sc->sc_pcir, sizeof(sc->sc_pcir)) == -1)
135		return (ENXIO);
136
137	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
138		busrange[0] = 0;
139
140	sc->sc_dev = dev;
141	sc->sc_node = node;
142	sc->sc_bus = busrange[0];
143
144	if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) {
145		phandle_t c;
146		int n, i;
147
148		sc->sc_nrange = 0;
149		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
150			n = ofw_pci_nranges(c);
151			if (n > 0)
152				sc->sc_nrange += n;
153		}
154		if (sc->sc_nrange == 0)
155			return (ENXIO);
156		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
157		    M_DEVBUF, M_WAITOK);
158		i = 0;
159		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
160			n = ofw_pci_fill_ranges(c, &sc->sc_range[i]);
161			if (n > 0)
162				i += n;
163		}
164		KASSERT(i == sc->sc_nrange, ("range count mismatch"));
165	} else {
166		sc->sc_nrange = ofw_pci_nranges(node);
167		if (sc->sc_nrange <= 0) {
168			device_printf(dev, "could not get ranges\n");
169			return (ENXIO);
170		}
171		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
172		    M_DEVBUF, M_WAITOK);
173		ofw_pci_fill_ranges(node, sc->sc_range);
174	}
175
176	sc->sc_io_rman.rm_type = RMAN_ARRAY;
177	sc->sc_io_rman.rm_descr = "PCI I/O Ports";
178	error = rman_init(&sc->sc_io_rman);
179	if (error) {
180		device_printf(dev, "rman_init() failed. error = %d\n", error);
181		return (error);
182	}
183
184	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
185	sc->sc_mem_rman.rm_descr = "PCI Memory";
186	error = rman_init(&sc->sc_mem_rman);
187	if (error) {
188		device_printf(dev, "rman_init() failed. error = %d\n", error);
189		return (error);
190	}
191
192	for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
193	       rp->pci_hi != 0; rp++) {
194		error = 0;
195
196		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
197		case OFW_PCI_PHYS_HI_SPACE_CONFIG:
198			break;
199		case OFW_PCI_PHYS_HI_SPACE_IO:
200			error = rman_manage_region(&sc->sc_io_rman, rp->pci,
201			    rp->pci + rp->size - 1);
202			break;
203		case OFW_PCI_PHYS_HI_SPACE_MEM32:
204		case OFW_PCI_PHYS_HI_SPACE_MEM64:
205			error = rman_manage_region(&sc->sc_mem_rman, rp->pci,
206			    rp->pci + rp->size - 1);
207			break;
208		}
209
210		if (error) {
211			device_printf(dev,
212			    "rman_manage_region(%x, %#jx, %#jx) failed. "
213			    "error = %d\n", rp->pci_hi &
214			    OFW_PCI_PHYS_HI_SPACEMASK, rp->pci,
215			    rp->pci + rp->size - 1, error);
216			panic("AHOY");
217			return (error);
218		}
219	}
220
221	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
222
223	device_add_child(dev, "pci", device_get_unit(dev));
224	return (bus_generic_attach(dev));
225}
226
227static int
228ofw_pci_maxslots(device_t dev)
229{
230
231	return (PCI_SLOTMAX);
232}
233
234static int
235ofw_pci_route_interrupt(device_t bus, device_t dev, int pin)
236{
237	struct ofw_pci_softc *sc;
238	struct ofw_pci_register reg;
239	uint32_t pintr, mintr;
240	phandle_t iparent;
241	uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
242
243	sc = device_get_softc(bus);
244	pintr = pin;
245	if (ofw_bus_lookup_imap(ofw_bus_get_node(dev), &sc->sc_pci_iinfo, &reg,
246	    sizeof(reg), &pintr, sizeof(pintr), &mintr, sizeof(mintr),
247	    &iparent, maskbuf))
248		return (MAP_IRQ(iparent, mintr));
249
250	/* Maybe it's a real interrupt, not an intpin */
251	if (pin > 4)
252		return (pin);
253
254	device_printf(bus, "could not route pin %d for device %d.%d\n",
255	    pin, pci_get_slot(dev), pci_get_function(dev));
256	return (PCI_INVALID_IRQ);
257}
258
259static int
260ofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
261{
262	struct	ofw_pci_softc *sc;
263
264	sc = device_get_softc(dev);
265
266	switch (which) {
267	case PCIB_IVAR_DOMAIN:
268		*result = device_get_unit(dev);
269		return (0);
270	case PCIB_IVAR_BUS:
271		*result = sc->sc_bus;
272		return (0);
273	}
274
275	return (ENOENT);
276}
277
278static struct resource *
279ofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
280    u_long start, u_long end, u_long count, u_int flags)
281{
282	struct			ofw_pci_softc *sc;
283	struct			resource *rv;
284	struct			rman *rm;
285	int			needactivate;
286
287	needactivate = flags & RF_ACTIVE;
288	flags &= ~RF_ACTIVE;
289
290	sc = device_get_softc(bus);
291
292	switch (type) {
293	case SYS_RES_MEMORY:
294		rm = &sc->sc_mem_rman;
295		break;
296
297	case SYS_RES_IOPORT:
298		rm = &sc->sc_io_rman;
299		break;
300
301	case SYS_RES_IRQ:
302		return (bus_alloc_resource(bus, type, rid, start, end, count,
303		    flags));
304
305	default:
306		device_printf(bus, "unknown resource request from %s\n",
307		    device_get_nameunit(child));
308		return (NULL);
309	}
310
311	rv = rman_reserve_resource(rm, start, end, count, flags, child);
312	if (rv == NULL) {
313		device_printf(bus, "failed to reserve resource for %s\n",
314		    device_get_nameunit(child));
315		return (NULL);
316	}
317
318	rman_set_rid(rv, *rid);
319
320	if (needactivate) {
321		if (bus_activate_resource(child, type, *rid, rv) != 0) {
322			device_printf(bus,
323			    "failed to activate resource for %s\n",
324			    device_get_nameunit(child));
325			rman_release_resource(rv);
326			return (NULL);
327		}
328	}
329
330	return (rv);
331}
332
333static int
334ofw_pci_release_resource(device_t bus, device_t child, int type, int rid,
335    struct resource *res)
336{
337	if (rman_get_flags(res) & RF_ACTIVE) {
338		int error = bus_deactivate_resource(child, type, rid, res);
339		if (error)
340			return error;
341	}
342
343	return (rman_release_resource(res));
344}
345
346static int
347ofw_pci_activate_resource(device_t bus, device_t child, int type, int rid,
348    struct resource *res)
349{
350	struct ofw_pci_softc *sc;
351	void	*p;
352
353	sc = device_get_softc(bus);
354
355	if (type == SYS_RES_IRQ) {
356		return (bus_activate_resource(bus, type, rid, res));
357	}
358	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
359		struct ofw_pci_range *rp;
360		vm_offset_t start;
361		int space;
362
363		start = (vm_offset_t)rman_get_start(res);
364
365		/*
366		 * Map this through the ranges list
367		 */
368		for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
369		       rp->pci_hi != 0; rp++) {
370			if (start < rp->pci || start >= rp->pci + rp->size)
371				continue;
372
373			switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
374			case OFW_PCI_PHYS_HI_SPACE_IO:
375				space = SYS_RES_IOPORT;
376				break;
377			case OFW_PCI_PHYS_HI_SPACE_MEM32:
378			case OFW_PCI_PHYS_HI_SPACE_MEM64:
379				space = SYS_RES_MEMORY;
380				break;
381			default:
382				space = -1;
383			}
384
385			if (type == space) {
386				start += (rp->host - rp->pci);
387				break;
388			}
389		}
390
391		if (bootverbose)
392			printf("ofw_pci mapdev: start %zx, len %ld\n", start,
393			    rman_get_size(res));
394
395		p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
396		if (p == NULL)
397			return (ENOMEM);
398
399		rman_set_virtual(res, p);
400		rman_set_bustag(res, &bs_le_tag);
401		rman_set_bushandle(res, (u_long)p);
402	}
403
404	return (rman_activate_resource(res));
405}
406
407static int
408ofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid,
409    struct resource *res)
410{
411	/*
412	 * If this is a memory resource, unmap it.
413	 */
414	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
415		u_int32_t psize;
416
417		psize = rman_get_size(res);
418		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
419	}
420
421	return (rman_deactivate_resource(res));
422}
423
424static phandle_t
425ofw_pci_get_node(device_t bus, device_t dev)
426{
427	struct ofw_pci_softc *sc;
428
429	sc = device_get_softc(bus);
430	/* We only have one child, the PCI bus, which needs our own node. */
431
432	return (sc->sc_node);
433}
434
435static int
436ofw_pci_nranges(phandle_t node)
437{
438	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
439	ssize_t nbase_ranges;
440
441	OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
442	    sizeof(host_address_cells));
443	OF_getprop(node, "#address-cells", &pci_address_cells,
444	    sizeof(pci_address_cells));
445	OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
446
447	nbase_ranges = OF_getproplen(node, "ranges");
448	if (nbase_ranges <= 0)
449		return (-1);
450
451	return (nbase_ranges / sizeof(cell_t) /
452	    (pci_address_cells + host_address_cells + size_cells));
453}
454
455static int
456ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges)
457{
458	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
459	cell_t *base_ranges;
460	ssize_t nbase_ranges;
461	int nranges;
462	int i, j, k;
463
464	OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
465	    sizeof(host_address_cells));
466	OF_getprop(node, "#address-cells", &pci_address_cells,
467	    sizeof(pci_address_cells));
468	OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
469
470	nbase_ranges = OF_getproplen(node, "ranges");
471	if (nbase_ranges <= 0)
472		return (-1);
473	nranges = nbase_ranges / sizeof(cell_t) /
474	    (pci_address_cells + host_address_cells + size_cells);
475
476	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
477	OF_getprop(node, "ranges", base_ranges, nbase_ranges);
478
479	for (i = 0, j = 0; i < nranges; i++) {
480		ranges[i].pci_hi = base_ranges[j++];
481		ranges[i].pci = 0;
482		for (k = 0; k < pci_address_cells - 1; k++) {
483			ranges[i].pci <<= 32;
484			ranges[i].pci |= base_ranges[j++];
485		}
486		ranges[i].host = 0;
487		for (k = 0; k < host_address_cells; k++) {
488			ranges[i].host <<= 32;
489			ranges[i].host |= base_ranges[j++];
490		}
491		ranges[i].size = 0;
492		for (k = 0; k < size_cells; k++) {
493			ranges[i].size <<= 32;
494			ranges[i].size |= base_ranges[j++];
495		}
496	}
497
498	free(base_ranges, M_DEVBUF);
499	return (nranges);
500}
501
502