ofw_pci.c revision 259516
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 259516 2013-12-17 15:11:24Z 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);
74static int		ofw_pci_adjust_resource(device_t bus, device_t child,
75			    int type, struct resource *res, u_long start,
76			    u_long end);
77
78/*
79 * pcib interface.
80 */
81static int		ofw_pci_maxslots(device_t);
82static int		ofw_pci_route_interrupt(device_t, device_t, int);
83
84/*
85 * ofw_bus interface
86 */
87static phandle_t ofw_pci_get_node(device_t bus, device_t dev);
88
89/*
90 * local methods
91 */
92
93static int ofw_pci_nranges(phandle_t node);
94static int ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges);
95
96/*
97 * Driver methods.
98 */
99static device_method_t	ofw_pci_methods[] = {
100	/* Device interface */
101	DEVMETHOD(device_attach,	ofw_pci_attach),
102
103	/* Bus interface */
104	DEVMETHOD(bus_print_child,	bus_generic_print_child),
105	DEVMETHOD(bus_read_ivar,	ofw_pci_read_ivar),
106	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
107	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
108	DEVMETHOD(bus_alloc_resource,	ofw_pci_alloc_resource),
109	DEVMETHOD(bus_release_resource,	ofw_pci_release_resource),
110	DEVMETHOD(bus_activate_resource,	ofw_pci_activate_resource),
111	DEVMETHOD(bus_deactivate_resource,	ofw_pci_deactivate_resource),
112	DEVMETHOD(bus_adjust_resource,	ofw_pci_adjust_resource),
113
114	/* pcib interface */
115	DEVMETHOD(pcib_maxslots,	ofw_pci_maxslots),
116	DEVMETHOD(pcib_route_interrupt,	ofw_pci_route_interrupt),
117
118	/* ofw_bus interface */
119	DEVMETHOD(ofw_bus_get_node,     ofw_pci_get_node),
120
121	DEVMETHOD_END
122};
123
124DEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0);
125
126int
127ofw_pci_init(device_t dev)
128{
129	struct		ofw_pci_softc *sc;
130	phandle_t	node;
131	u_int32_t	busrange[2];
132	struct		ofw_pci_range *rp;
133	int		error;
134
135	node = ofw_bus_get_node(dev);
136	sc = device_get_softc(dev);
137	sc->sc_initialized = 1;
138
139	if (OF_getprop(node, "reg", &sc->sc_pcir, sizeof(sc->sc_pcir)) == -1)
140		return (ENXIO);
141
142	if (OF_getprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
143		busrange[0] = 0;
144
145	sc->sc_dev = dev;
146	sc->sc_node = node;
147	sc->sc_bus = busrange[0];
148
149	if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) {
150		phandle_t c;
151		int n, i;
152
153		sc->sc_nrange = 0;
154		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
155			n = ofw_pci_nranges(c);
156			if (n > 0)
157				sc->sc_nrange += n;
158		}
159		if (sc->sc_nrange == 0)
160			return (ENXIO);
161		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
162		    M_DEVBUF, M_WAITOK);
163		i = 0;
164		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
165			n = ofw_pci_fill_ranges(c, &sc->sc_range[i]);
166			if (n > 0)
167				i += n;
168		}
169		KASSERT(i == sc->sc_nrange, ("range count mismatch"));
170	} else {
171		sc->sc_nrange = ofw_pci_nranges(node);
172		if (sc->sc_nrange <= 0) {
173			device_printf(dev, "could not get ranges\n");
174			return (ENXIO);
175		}
176		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
177		    M_DEVBUF, M_WAITOK);
178		ofw_pci_fill_ranges(node, sc->sc_range);
179	}
180
181	sc->sc_io_rman.rm_type = RMAN_ARRAY;
182	sc->sc_io_rman.rm_descr = "PCI I/O Ports";
183	error = rman_init(&sc->sc_io_rman);
184	if (error) {
185		device_printf(dev, "rman_init() failed. error = %d\n", error);
186		return (error);
187	}
188
189	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
190	sc->sc_mem_rman.rm_descr = "PCI Memory";
191	error = rman_init(&sc->sc_mem_rman);
192	if (error) {
193		device_printf(dev, "rman_init() failed. error = %d\n", error);
194		return (error);
195	}
196
197	for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
198	       rp->pci_hi != 0; rp++) {
199		error = 0;
200
201		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
202		case OFW_PCI_PHYS_HI_SPACE_CONFIG:
203			break;
204		case OFW_PCI_PHYS_HI_SPACE_IO:
205			error = rman_manage_region(&sc->sc_io_rman, rp->pci,
206			    rp->pci + rp->size - 1);
207			break;
208		case OFW_PCI_PHYS_HI_SPACE_MEM32:
209		case OFW_PCI_PHYS_HI_SPACE_MEM64:
210			error = rman_manage_region(&sc->sc_mem_rman, rp->pci,
211			    rp->pci + rp->size - 1);
212			break;
213		}
214
215		if (error) {
216			device_printf(dev,
217			    "rman_manage_region(%x, %#jx, %#jx) failed. "
218			    "error = %d\n", rp->pci_hi &
219			    OFW_PCI_PHYS_HI_SPACEMASK, rp->pci,
220			    rp->pci + rp->size - 1, error);
221			return (error);
222		}
223	}
224
225	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
226
227	return (error);
228}
229
230int
231ofw_pci_attach(device_t dev)
232{
233	struct ofw_pci_softc *sc;
234	int error;
235
236	sc = device_get_softc(dev);
237	if (!sc->sc_initialized) {
238		error = ofw_pci_init(dev);
239		if (error)
240			return (error);
241	}
242
243	device_add_child(dev, "pci", device_get_unit(dev));
244	return (bus_generic_attach(dev));
245}
246
247static int
248ofw_pci_maxslots(device_t dev)
249{
250
251	return (PCI_SLOTMAX);
252}
253
254static int
255ofw_pci_route_interrupt(device_t bus, device_t dev, int pin)
256{
257	struct ofw_pci_softc *sc;
258	struct ofw_pci_register reg;
259	uint32_t pintr, mintr[2];
260	int intrcells;
261	phandle_t iparent;
262
263	sc = device_get_softc(bus);
264	pintr = pin;
265
266	/* Fabricate imap information in case this isn't an OFW device */
267	bzero(&reg, sizeof(reg));
268	reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) |
269	    (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) |
270	    (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT);
271
272	intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev),
273	    &sc->sc_pci_iinfo, &reg, sizeof(reg), &pintr, sizeof(pintr),
274	    mintr, sizeof(mintr), &iparent);
275	if (intrcells) {
276		pintr = ofw_bus_map_intr(dev, iparent, mintr[0]);
277		if (intrcells == 2)
278			ofw_bus_config_intr(dev, pintr, mintr[1]);
279		return (pintr);
280	}
281
282	/* Maybe it's a real interrupt, not an intpin */
283	if (pin > 4)
284		return (pin);
285
286	device_printf(bus, "could not route pin %d for device %d.%d\n",
287	    pin, pci_get_slot(dev), pci_get_function(dev));
288	return (PCI_INVALID_IRQ);
289}
290
291static int
292ofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
293{
294	struct	ofw_pci_softc *sc;
295
296	sc = device_get_softc(dev);
297
298	switch (which) {
299	case PCIB_IVAR_DOMAIN:
300		*result = device_get_unit(dev);
301		return (0);
302	case PCIB_IVAR_BUS:
303		*result = sc->sc_bus;
304		return (0);
305	}
306
307	return (ENOENT);
308}
309
310static struct resource *
311ofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
312    u_long start, u_long end, u_long count, u_int flags)
313{
314	struct			ofw_pci_softc *sc;
315	struct			resource *rv;
316	struct			rman *rm;
317	int			needactivate;
318
319	needactivate = flags & RF_ACTIVE;
320	flags &= ~RF_ACTIVE;
321
322	sc = device_get_softc(bus);
323
324	switch (type) {
325	case SYS_RES_MEMORY:
326		rm = &sc->sc_mem_rman;
327		break;
328
329	case SYS_RES_IOPORT:
330		rm = &sc->sc_io_rman;
331		break;
332
333	case SYS_RES_IRQ:
334		return (bus_alloc_resource(bus, type, rid, start, end, count,
335		    flags));
336
337	default:
338		device_printf(bus, "unknown resource request from %s\n",
339		    device_get_nameunit(child));
340		return (NULL);
341	}
342
343	rv = rman_reserve_resource(rm, start, end, count, flags, child);
344	if (rv == NULL) {
345		device_printf(bus, "failed to reserve resource for %s\n",
346		    device_get_nameunit(child));
347		return (NULL);
348	}
349
350	rman_set_rid(rv, *rid);
351
352	if (needactivate) {
353		if (bus_activate_resource(child, type, *rid, rv) != 0) {
354			device_printf(bus,
355			    "failed to activate resource for %s\n",
356			    device_get_nameunit(child));
357			rman_release_resource(rv);
358			return (NULL);
359		}
360	}
361
362	return (rv);
363}
364
365static int
366ofw_pci_release_resource(device_t bus, device_t child, int type, int rid,
367    struct resource *res)
368{
369	if (rman_get_flags(res) & RF_ACTIVE) {
370		int error = bus_deactivate_resource(child, type, rid, res);
371		if (error)
372			return error;
373	}
374
375	return (rman_release_resource(res));
376}
377
378static int
379ofw_pci_activate_resource(device_t bus, device_t child, int type, int rid,
380    struct resource *res)
381{
382	struct ofw_pci_softc *sc;
383	void	*p;
384
385	sc = device_get_softc(bus);
386
387	if (type == SYS_RES_IRQ) {
388		return (bus_activate_resource(bus, type, rid, res));
389	}
390	if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) {
391		struct ofw_pci_range *rp;
392		vm_offset_t start;
393		int space;
394
395		start = (vm_offset_t)rman_get_start(res);
396
397		/*
398		 * Map this through the ranges list
399		 */
400		for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
401		       rp->pci_hi != 0; rp++) {
402			if (start < rp->pci || start >= rp->pci + rp->size)
403				continue;
404
405			switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
406			case OFW_PCI_PHYS_HI_SPACE_IO:
407				space = SYS_RES_IOPORT;
408				break;
409			case OFW_PCI_PHYS_HI_SPACE_MEM32:
410			case OFW_PCI_PHYS_HI_SPACE_MEM64:
411				space = SYS_RES_MEMORY;
412				break;
413			default:
414				space = -1;
415			}
416
417			if (type == space) {
418				start += (rp->host - rp->pci);
419				break;
420			}
421		}
422
423		if (bootverbose)
424			printf("ofw_pci mapdev: start %zx, len %ld\n", start,
425			    rman_get_size(res));
426
427		p = pmap_mapdev(start, (vm_size_t)rman_get_size(res));
428		if (p == NULL)
429			return (ENOMEM);
430
431		rman_set_virtual(res, p);
432		rman_set_bustag(res, &bs_le_tag);
433		rman_set_bushandle(res, (u_long)p);
434	}
435
436	return (rman_activate_resource(res));
437}
438
439static int
440ofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid,
441    struct resource *res)
442{
443	/*
444	 * If this is a memory resource, unmap it.
445	 */
446	if ((type == SYS_RES_MEMORY) || (type == SYS_RES_IOPORT)) {
447		u_int32_t psize;
448
449		psize = rman_get_size(res);
450		pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
451	}
452
453	return (rman_deactivate_resource(res));
454}
455
456static int
457ofw_pci_adjust_resource(device_t bus, device_t child, int type,
458    struct resource *res, u_long start, u_long end)
459{
460	struct rman *rm = NULL;
461	struct ofw_pci_softc *sc = device_get_softc(bus);
462
463	KASSERT(!(rman_get_flags(res) & RF_ACTIVE),
464	    ("active resources cannot be adjusted"));
465	if (rman_get_flags(res) & RF_ACTIVE)
466		return (EINVAL);
467
468	switch (type) {
469	case SYS_RES_MEMORY:
470		rm = &sc->sc_mem_rman;
471		break;
472	case SYS_RES_IOPORT:
473		rm = &sc->sc_io_rman;
474		break;
475	default:
476		return (ENXIO);
477	}
478
479	if (!rman_is_region_manager(res, rm))
480		return (EINVAL);
481
482	return (rman_adjust_resource(res, start, end));
483}
484
485
486static phandle_t
487ofw_pci_get_node(device_t bus, device_t dev)
488{
489	struct ofw_pci_softc *sc;
490
491	sc = device_get_softc(bus);
492	/* We only have one child, the PCI bus, which needs our own node. */
493
494	return (sc->sc_node);
495}
496
497static int
498ofw_pci_nranges(phandle_t node)
499{
500	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
501	ssize_t nbase_ranges;
502
503	OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
504	    sizeof(host_address_cells));
505	OF_getprop(node, "#address-cells", &pci_address_cells,
506	    sizeof(pci_address_cells));
507	OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
508
509	nbase_ranges = OF_getproplen(node, "ranges");
510	if (nbase_ranges <= 0)
511		return (-1);
512
513	return (nbase_ranges / sizeof(cell_t) /
514	    (pci_address_cells + host_address_cells + size_cells));
515}
516
517static int
518ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges)
519{
520	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
521	cell_t *base_ranges;
522	ssize_t nbase_ranges;
523	int nranges;
524	int i, j, k;
525
526	OF_getprop(OF_parent(node), "#address-cells", &host_address_cells,
527	    sizeof(host_address_cells));
528	OF_getprop(node, "#address-cells", &pci_address_cells,
529	    sizeof(pci_address_cells));
530	OF_getprop(node, "#size-cells", &size_cells, sizeof(size_cells));
531
532	nbase_ranges = OF_getproplen(node, "ranges");
533	if (nbase_ranges <= 0)
534		return (-1);
535	nranges = nbase_ranges / sizeof(cell_t) /
536	    (pci_address_cells + host_address_cells + size_cells);
537
538	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
539	OF_getprop(node, "ranges", base_ranges, nbase_ranges);
540
541	for (i = 0, j = 0; i < nranges; i++) {
542		ranges[i].pci_hi = base_ranges[j++];
543		ranges[i].pci = 0;
544		for (k = 0; k < pci_address_cells - 1; k++) {
545			ranges[i].pci <<= 32;
546			ranges[i].pci |= base_ranges[j++];
547		}
548		ranges[i].host = 0;
549		for (k = 0; k < host_address_cells; k++) {
550			ranges[i].host <<= 32;
551			ranges[i].host |= base_ranges[j++];
552		}
553		ranges[i].size = 0;
554		for (k = 0; k < size_cells; k++) {
555			ranges[i].size <<= 32;
556			ranges[i].size |= base_ranges[j++];
557		}
558	}
559
560	free(base_ranges, M_DEVBUF);
561	return (nranges);
562}
563
564