xref: /freebsd-12.1/sys/dev/ofw/ofwpci.c (revision 151ba793)
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$");
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 #include <sys/rman.h>
36 
37 #include <dev/ofw/openfirm.h>
38 #include <dev/ofw/ofw_pci.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41 #include <dev/ofw/ofwpci.h>
42 
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcib_private.h>
46 
47 #include <machine/bus.h>
48 #include <machine/md_var.h>
49 #include <machine/resource.h>
50 
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 
54 #include "pcib_if.h"
55 
56 /*
57  * If it is necessary to set another value of this for
58  * some platforms it should be set at fdt.h file
59  */
60 #ifndef PCI_MAP_INTR
61 #define	PCI_MAP_INTR	4
62 #endif
63 
64 #define	PCI_INTR_PINS	4
65 
66 /*
67  * bus interface.
68  */
69 static struct resource * ofw_pci_alloc_resource(device_t, device_t,
70     int, int *, rman_res_t, rman_res_t, rman_res_t, u_int);
71 static int ofw_pci_release_resource(device_t, device_t, int, int,
72     struct resource *);
73 static int ofw_pci_activate_resource(device_t, device_t, int, int,
74     struct resource *);
75 static int ofw_pci_deactivate_resource(device_t, device_t, int, int,
76     struct resource *);
77 static int ofw_pci_adjust_resource(device_t, device_t, int,
78     struct resource *, rman_res_t, rman_res_t);
79 
80 #ifdef __powerpc__
81 static bus_space_tag_t ofw_pci_bus_get_bus_tag(device_t, device_t);
82 #endif
83 
84 /*
85  * pcib interface
86  */
87 static int ofw_pci_maxslots(device_t);
88 
89 /*
90  * ofw_bus interface
91  */
92 static phandle_t ofw_pci_get_node(device_t, device_t);
93 
94 /*
95  * local methods
96  */
97 static int ofw_pci_fill_ranges(phandle_t, struct ofw_pci_range *);
98 static struct rman *ofw_pci_get_rman(struct ofw_pci_softc *, int, u_int);
99 
100 /*
101  * Driver methods.
102  */
103 static device_method_t	ofw_pci_methods[] = {
104 
105 	/* Device interface */
106 	DEVMETHOD(device_attach,	ofw_pci_attach),
107 
108 	/* Bus interface */
109 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
110 	DEVMETHOD(bus_read_ivar,	ofw_pci_read_ivar),
111 	DEVMETHOD(bus_write_ivar,	ofw_pci_write_ivar),
112 	DEVMETHOD(bus_setup_intr,	bus_generic_setup_intr),
113 	DEVMETHOD(bus_teardown_intr,	bus_generic_teardown_intr),
114 	DEVMETHOD(bus_alloc_resource,	ofw_pci_alloc_resource),
115 	DEVMETHOD(bus_release_resource,	ofw_pci_release_resource),
116 	DEVMETHOD(bus_activate_resource,	ofw_pci_activate_resource),
117 	DEVMETHOD(bus_deactivate_resource,	ofw_pci_deactivate_resource),
118 	DEVMETHOD(bus_adjust_resource,	ofw_pci_adjust_resource),
119 #ifdef __powerpc__
120 	DEVMETHOD(bus_get_bus_tag,	ofw_pci_bus_get_bus_tag),
121 #endif
122 
123 	/* pcib interface */
124 	DEVMETHOD(pcib_maxslots,	ofw_pci_maxslots),
125 	DEVMETHOD(pcib_route_interrupt,	ofw_pci_route_interrupt),
126 	DEVMETHOD(pcib_request_feature,	pcib_request_feature_allow),
127 
128 	/* ofw_bus interface */
129 	DEVMETHOD(ofw_bus_get_node,	ofw_pci_get_node),
130 
131 	DEVMETHOD_END
132 };
133 
134 DEFINE_CLASS_0(ofw_pci, ofw_pci_driver, ofw_pci_methods, 0);
135 
136 int
ofw_pci_init(device_t dev)137 ofw_pci_init(device_t dev)
138 {
139 	struct ofw_pci_softc *sc;
140 	phandle_t node;
141 	u_int32_t busrange[2];
142 	struct ofw_pci_range *rp;
143 	int i, error;
144 	struct ofw_pci_cell_info *cell_info;
145 
146 	node = ofw_bus_get_node(dev);
147 	sc = device_get_softc(dev);
148 	sc->sc_initialized = 1;
149 	sc->sc_range = NULL;
150 	sc->sc_pci_domain = device_get_unit(dev);
151 
152 	cell_info = (struct ofw_pci_cell_info *)malloc(sizeof(*cell_info),
153 	    M_DEVBUF, M_WAITOK | M_ZERO);
154 
155 	sc->sc_cell_info = cell_info;
156 
157 	if (OF_getencprop(node, "bus-range", busrange, sizeof(busrange)) != 8)
158 		busrange[0] = 0;
159 
160 	sc->sc_dev = dev;
161 	sc->sc_node = node;
162 	sc->sc_bus = busrange[0];
163 
164 	if (sc->sc_quirks & OFW_PCI_QUIRK_RANGES_ON_CHILDREN) {
165 		phandle_t c;
166 		int n, i;
167 
168 		sc->sc_nrange = 0;
169 		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
170 			n = ofw_pci_nranges(c, cell_info);
171 			if (n > 0)
172 				sc->sc_nrange += n;
173 		}
174 		if (sc->sc_nrange == 0) {
175 			error = ENXIO;
176 			goto out;
177 		}
178 		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
179 		    M_DEVBUF, M_WAITOK);
180 		i = 0;
181 		for (c = OF_child(node); c != 0; c = OF_peer(c)) {
182 			n = ofw_pci_fill_ranges(c, &sc->sc_range[i]);
183 			if (n > 0)
184 				i += n;
185 		}
186 		KASSERT(i == sc->sc_nrange, ("range count mismatch"));
187 	} else {
188 		sc->sc_nrange = ofw_pci_nranges(node, cell_info);
189 		if (sc->sc_nrange <= 0) {
190 			device_printf(dev, "could not getranges\n");
191 			error = ENXIO;
192 			goto out;
193 		}
194 		sc->sc_range = malloc(sc->sc_nrange * sizeof(sc->sc_range[0]),
195 		    M_DEVBUF, M_WAITOK);
196 		ofw_pci_fill_ranges(node, sc->sc_range);
197 	}
198 
199 	sc->sc_io_rman.rm_type = RMAN_ARRAY;
200 	sc->sc_io_rman.rm_descr = "PCI I/O Ports";
201 	error = rman_init(&sc->sc_io_rman);
202 	if (error != 0) {
203 		device_printf(dev, "rman_init() failed. error = %d\n", error);
204 		goto out;
205 	}
206 
207 	sc->sc_mem_rman.rm_type = RMAN_ARRAY;
208 	sc->sc_mem_rman.rm_descr = "PCI Non Prefetchable Memory";
209 	error = rman_init(&sc->sc_mem_rman);
210 	if (error != 0) {
211 		device_printf(dev, "rman_init() failed. error = %d\n", error);
212 		goto out;
213 	}
214 
215 	sc->sc_pmem_rman.rm_type = RMAN_ARRAY;
216 	sc->sc_pmem_rman.rm_descr = "PCI Prefetchable Memory";
217 	error = rman_init(&sc->sc_pmem_rman);
218 	if (error != 0) {
219 		device_printf(dev, "rman_init() failed. error = %d\n", error);
220 		goto out;
221 	}
222 
223 	for (i = 0; i < sc->sc_nrange; i++) {
224 		error = 0;
225 		rp = sc->sc_range + i;
226 
227 		if (sc->sc_range_mask & ((uint64_t)1 << i))
228 			continue;
229 		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
230 		case OFW_PCI_PHYS_HI_SPACE_CONFIG:
231 			break;
232 		case OFW_PCI_PHYS_HI_SPACE_IO:
233 			error = rman_manage_region(&sc->sc_io_rman, rp->pci,
234 			    rp->pci + rp->size - 1);
235 			break;
236 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
237 		case OFW_PCI_PHYS_HI_SPACE_MEM64:
238 			if (rp->pci_hi & OFW_PCI_PHYS_HI_PREFETCHABLE) {
239 				sc->sc_have_pmem = 1;
240 				error = rman_manage_region(&sc->sc_pmem_rman,
241 				    rp->pci, rp->pci + rp->size - 1);
242 			} else {
243 				error = rman_manage_region(&sc->sc_mem_rman,
244 				    rp->pci, rp->pci + rp->size - 1);
245 			}
246 			break;
247 		}
248 
249 		if (error != 0) {
250 			device_printf(dev,
251 			    "rman_manage_region(%x, %#jx, %#jx) failed. "
252 			    "error = %d\n", rp->pci_hi &
253 			    OFW_PCI_PHYS_HI_SPACEMASK, rp->pci,
254 			    rp->pci + rp->size - 1, error);
255 			goto out;
256 		}
257 	}
258 
259 	ofw_bus_setup_iinfo(node, &sc->sc_pci_iinfo, sizeof(cell_t));
260 	return (0);
261 
262 out:
263 	free(cell_info, M_DEVBUF);
264 	free(sc->sc_range, M_DEVBUF);
265 	rman_fini(&sc->sc_io_rman);
266 	rman_fini(&sc->sc_mem_rman);
267 	rman_fini(&sc->sc_pmem_rman);
268 
269 	return (error);
270 }
271 
272 int
ofw_pci_attach(device_t dev)273 ofw_pci_attach(device_t dev)
274 {
275 	struct ofw_pci_softc *sc;
276 	int error;
277 
278 	sc = device_get_softc(dev);
279 	if (!sc->sc_initialized) {
280 		error = ofw_pci_init(dev);
281 		if (error != 0)
282 			return (error);
283 	}
284 
285 	device_add_child(dev, "pci", -1);
286 	return (bus_generic_attach(dev));
287 }
288 
289 static int
ofw_pci_maxslots(device_t dev)290 ofw_pci_maxslots(device_t dev)
291 {
292 
293 	return (PCI_SLOTMAX);
294 }
295 
296 int
ofw_pci_route_interrupt(device_t bus,device_t dev,int pin)297 ofw_pci_route_interrupt(device_t bus, device_t dev, int pin)
298 {
299 	struct ofw_pci_softc *sc;
300 	struct ofw_pci_register reg;
301 	uint32_t pintr, mintr[PCI_MAP_INTR];
302 	int intrcells;
303 	phandle_t iparent;
304 
305 	sc = device_get_softc(bus);
306 	pintr = pin;
307 
308 	/* Fabricate imap information in case this isn't an OFW device */
309 	bzero(&reg, sizeof(reg));
310 	reg.phys_hi = (pci_get_bus(dev) << OFW_PCI_PHYS_HI_BUSSHIFT) |
311 	    (pci_get_slot(dev) << OFW_PCI_PHYS_HI_DEVICESHIFT) |
312 	    (pci_get_function(dev) << OFW_PCI_PHYS_HI_FUNCTIONSHIFT);
313 
314 	intrcells = ofw_bus_lookup_imap(ofw_bus_get_node(dev),
315 	    &sc->sc_pci_iinfo, &reg, sizeof(reg), &pintr, sizeof(pintr),
316 	    mintr, sizeof(mintr), &iparent);
317 	if (intrcells != 0) {
318 		pintr = ofw_bus_map_intr(dev, iparent, intrcells, mintr);
319 		return (pintr);
320 	}
321 
322 	/*
323 	 * Maybe it's a real interrupt, not an intpin
324 	 */
325 	if (pin > PCI_INTR_PINS)
326 		return (pin);
327 
328 	device_printf(bus, "could not route pin %d for device %d.%d\n",
329 	    pin, pci_get_slot(dev), pci_get_function(dev));
330 	return (PCI_INVALID_IRQ);
331 }
332 
333 int
ofw_pci_read_ivar(device_t dev,device_t child,int which,uintptr_t * result)334 ofw_pci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
335 {
336 	struct ofw_pci_softc *sc;
337 
338 	sc = device_get_softc(dev);
339 
340 	switch (which) {
341 	case PCIB_IVAR_DOMAIN:
342 		*result = sc->sc_pci_domain;
343 		return (0);
344 	case PCIB_IVAR_BUS:
345 		*result = sc->sc_bus;
346 		return (0);
347 	default:
348 		break;
349 	}
350 
351 	return (ENOENT);
352 }
353 
354 int
ofw_pci_write_ivar(device_t dev,device_t child,int which,uintptr_t value)355 ofw_pci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
356 {
357 	struct ofw_pci_softc *sc;
358 
359 	sc = device_get_softc(dev);
360 
361 	switch (which) {
362 	case PCIB_IVAR_BUS:
363 		sc->sc_bus = value;
364 		return (0);
365 	default:
366 		break;
367 	}
368 
369 	return (ENOENT);
370 }
371 
372 int
ofw_pci_nranges(phandle_t node,struct ofw_pci_cell_info * info)373 ofw_pci_nranges(phandle_t node, struct ofw_pci_cell_info *info)
374 {
375 	ssize_t nbase_ranges;
376 
377 	if (info == NULL)
378 		return (-1);
379 
380 	info->host_address_cells = 1;
381 	info->size_cells = 2;
382 	info->pci_address_cell = 3;
383 
384 	OF_getencprop(OF_parent(node), "#address-cells",
385 	    &(info->host_address_cells), sizeof(info->host_address_cells));
386 	OF_getencprop(node, "#address-cells",
387 	    &(info->pci_address_cell), sizeof(info->pci_address_cell));
388 	OF_getencprop(node, "#size-cells", &(info->size_cells),
389 	    sizeof(info->size_cells));
390 
391 	nbase_ranges = OF_getproplen(node, "ranges");
392 	if (nbase_ranges <= 0)
393 		return (-1);
394 
395 	return (nbase_ranges / sizeof(cell_t) /
396 	    (info->pci_address_cell + info->host_address_cells +
397 	    info->size_cells));
398 }
399 
400 static struct resource *
ofw_pci_alloc_resource(device_t bus,device_t child,int type,int * rid,rman_res_t start,rman_res_t end,rman_res_t count,u_int flags)401 ofw_pci_alloc_resource(device_t bus, device_t child, int type, int *rid,
402     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
403 {
404 	struct ofw_pci_softc *sc;
405 	struct resource *rv;
406 	struct rman *rm;
407 	int needactivate;
408 
409 
410 	needactivate = flags & RF_ACTIVE;
411 	flags &= ~RF_ACTIVE;
412 
413 	sc = device_get_softc(bus);
414 
415 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
416 	if (type ==  PCI_RES_BUS) {
417 		  return (pci_domain_alloc_bus(sc->sc_pci_domain, child, rid,
418 		      start, end, count, flags | needactivate));
419 	}
420 #endif
421 
422 	rm = ofw_pci_get_rman(sc, type, flags);
423 	if (rm == NULL)  {
424 		return (bus_generic_alloc_resource(bus, child, type, rid,
425 		    start, end, count, flags | needactivate));
426 	}
427 
428 	rv = rman_reserve_resource(rm, start, end, count, flags, child);
429 	if (rv == NULL) {
430 		device_printf(bus, "failed to reserve resource for %s\n",
431 		    device_get_nameunit(child));
432 		return (NULL);
433 	}
434 
435 	rman_set_rid(rv, *rid);
436 
437 	if (needactivate) {
438 		if (bus_activate_resource(child, type, *rid, rv) != 0) {
439 			device_printf(bus,
440 			    "failed to activate resource for %s\n",
441 			    device_get_nameunit(child));
442 			rman_release_resource(rv);
443 			return (NULL);
444 		}
445 	}
446 
447 	return (rv);
448 }
449 
450 static int
ofw_pci_release_resource(device_t bus,device_t child,int type,int rid,struct resource * res)451 ofw_pci_release_resource(device_t bus, device_t child, int type, int rid,
452     struct resource *res)
453 {
454 	struct ofw_pci_softc *sc;
455 	struct rman *rm;
456 	int error;
457 
458 	sc = device_get_softc(bus);
459 
460 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
461 	if (type == PCI_RES_BUS)
462 		return (pci_domain_release_bus(sc->sc_pci_domain, child, rid,
463 		    res));
464 #endif
465 
466 	rm = ofw_pci_get_rman(sc, type, rman_get_flags(res));
467 	if (rm == NULL) {
468 		return (bus_generic_release_resource(bus, child, type, rid,
469 		    res));
470 	}
471 	KASSERT(rman_is_region_manager(res, rm), ("rman mismatch"));
472 
473 	if (rman_get_flags(res) & RF_ACTIVE) {
474 		error = bus_deactivate_resource(child, type, rid, res);
475 		if (error != 0)
476 			return (error);
477 	}
478 	return (rman_release_resource(res));
479 }
480 
481 static int
ofw_pci_activate_resource(device_t bus,device_t child,int type,int rid,struct resource * res)482 ofw_pci_activate_resource(device_t bus, device_t child, int type, int rid,
483     struct resource *res)
484 {
485 	struct ofw_pci_softc *sc;
486 	bus_space_handle_t handle;
487 	bus_space_tag_t tag;
488 	struct ofw_pci_range *rp;
489 	vm_paddr_t start;
490 	int space;
491 	int rv;
492 
493 	sc = device_get_softc(bus);
494 
495 	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY) {
496 		return (bus_generic_activate_resource(bus, child, type, rid,
497 		    res));
498 	}
499 
500 	start = (vm_paddr_t)rman_get_start(res);
501 
502 	/*
503 	 * Map this through the ranges list
504 	 */
505 	for (rp = sc->sc_range; rp < sc->sc_range + sc->sc_nrange &&
506 	    rp->pci_hi != 0; rp++) {
507 		if (start < rp->pci || start >= rp->pci + rp->size)
508 			continue;
509 
510 		switch (rp->pci_hi & OFW_PCI_PHYS_HI_SPACEMASK) {
511 		case OFW_PCI_PHYS_HI_SPACE_IO:
512 			space = SYS_RES_IOPORT;
513 			break;
514 		case OFW_PCI_PHYS_HI_SPACE_MEM32:
515 		case OFW_PCI_PHYS_HI_SPACE_MEM64:
516 			space = SYS_RES_MEMORY;
517 			break;
518 		default:
519 			space = -1;
520 			}
521 
522 		if (type == space) {
523 			start += (rp->host - rp->pci);
524 			break;
525 		}
526 	}
527 
528 	if (bootverbose)
529 		printf("ofw_pci mapdev: start %jx, len %jd\n",
530 		    (rman_res_t)start, rman_get_size(res));
531 
532 	tag = BUS_GET_BUS_TAG(child, child);
533 	if (tag == NULL)
534 		return (ENOMEM);
535 
536 	rman_set_bustag(res, tag);
537 	rv = bus_space_map(tag, start,
538 	    rman_get_size(res), 0, &handle);
539 	if (rv != 0)
540 		return (ENOMEM);
541 
542 	rman_set_bushandle(res, handle);
543 	rman_set_virtual(res, (void *)handle); /* XXX  for powerpc only ? */
544 
545 	return (rman_activate_resource(res));
546 }
547 
548 #ifdef __powerpc__
549 static bus_space_tag_t
ofw_pci_bus_get_bus_tag(device_t bus,device_t child)550 ofw_pci_bus_get_bus_tag(device_t bus, device_t child)
551 {
552 
553 	return (&bs_le_tag);
554 }
555 #endif
556 
557 static int
ofw_pci_deactivate_resource(device_t bus,device_t child,int type,int rid,struct resource * res)558 ofw_pci_deactivate_resource(device_t bus, device_t child, int type, int rid,
559     struct resource *res)
560 {
561 	vm_size_t psize;
562 
563 	if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY) {
564 		return (bus_generic_deactivate_resource(bus, child, type, rid,
565 		    res));
566 	}
567 
568 	psize = rman_get_size(res);
569 	pmap_unmapdev((vm_offset_t)rman_get_virtual(res), psize);
570 
571 	return (rman_deactivate_resource(res));
572 }
573 
574 static int
ofw_pci_adjust_resource(device_t bus,device_t child,int type,struct resource * res,rman_res_t start,rman_res_t end)575 ofw_pci_adjust_resource(device_t bus, device_t child, int type,
576     struct resource *res, rman_res_t start, rman_res_t end)
577 {
578 	struct rman *rm;
579 	struct ofw_pci_softc *sc;
580 
581 	sc = device_get_softc(bus);
582 #if defined(NEW_PCIB) && defined(PCI_RES_BUS)
583 	if (type == PCI_RES_BUS)
584 		return (pci_domain_adjust_bus(sc->sc_pci_domain, child, res,
585 		    start, end));
586 #endif
587 
588 	rm = ofw_pci_get_rman(sc, type, rman_get_flags(res));
589 	if (rm == NULL) {
590 		return (bus_generic_adjust_resource(bus, child, type, res,
591 		    start, end));
592 	}
593 	KASSERT(rman_is_region_manager(res, rm), ("rman mismatch"));
594 	KASSERT(!(rman_get_flags(res) & RF_ACTIVE),
595 	    ("active resources cannot be adjusted"));
596 
597 	return (rman_adjust_resource(res, start, end));
598 }
599 
600 static phandle_t
ofw_pci_get_node(device_t bus,device_t dev)601 ofw_pci_get_node(device_t bus, device_t dev)
602 {
603 	struct ofw_pci_softc *sc;
604 
605 	sc = device_get_softc(bus);
606 	/* We only have one child, the PCI bus, which needs our own node. */
607 
608 	return (sc->sc_node);
609 }
610 
611 static int
ofw_pci_fill_ranges(phandle_t node,struct ofw_pci_range * ranges)612 ofw_pci_fill_ranges(phandle_t node, struct ofw_pci_range *ranges)
613 {
614 	int host_address_cells = 1, pci_address_cells = 3, size_cells = 2;
615 	cell_t *base_ranges;
616 	ssize_t nbase_ranges;
617 	int nranges;
618 	int i, j, k;
619 
620 	OF_getencprop(OF_parent(node), "#address-cells", &host_address_cells,
621 	    sizeof(host_address_cells));
622 	OF_getencprop(node, "#address-cells", &pci_address_cells,
623 	    sizeof(pci_address_cells));
624 	OF_getencprop(node, "#size-cells", &size_cells, sizeof(size_cells));
625 
626 	nbase_ranges = OF_getproplen(node, "ranges");
627 	if (nbase_ranges <= 0)
628 		return (-1);
629 	nranges = nbase_ranges / sizeof(cell_t) /
630 	    (pci_address_cells + host_address_cells + size_cells);
631 
632 	base_ranges = malloc(nbase_ranges, M_DEVBUF, M_WAITOK);
633 	OF_getencprop(node, "ranges", base_ranges, nbase_ranges);
634 
635 	for (i = 0, j = 0; i < nranges; i++) {
636 		ranges[i].pci_hi = base_ranges[j++];
637 		ranges[i].pci = 0;
638 		for (k = 0; k < pci_address_cells - 1; k++) {
639 			ranges[i].pci <<= 32;
640 			ranges[i].pci |= base_ranges[j++];
641 		}
642 		ranges[i].host = 0;
643 		for (k = 0; k < host_address_cells; k++) {
644 			ranges[i].host <<= 32;
645 			ranges[i].host |= base_ranges[j++];
646 		}
647 		ranges[i].size = 0;
648 		for (k = 0; k < size_cells; k++) {
649 			ranges[i].size <<= 32;
650 			ranges[i].size |= base_ranges[j++];
651 		}
652 	}
653 
654 	free(base_ranges, M_DEVBUF);
655 	return (nranges);
656 }
657 
658 static struct rman *
ofw_pci_get_rman(struct ofw_pci_softc * sc,int type,u_int flags)659 ofw_pci_get_rman(struct ofw_pci_softc *sc, int type, u_int flags)
660 {
661 
662 	switch (type) {
663 	case SYS_RES_IOPORT:
664 		return (&sc->sc_io_rman);
665 	case SYS_RES_MEMORY:
666 		if (sc->sc_have_pmem  && (flags & RF_PREFETCHABLE))
667 			return (&sc->sc_pmem_rman);
668 		else
669 			return (&sc->sc_mem_rman);
670 	default:
671 		break;
672 	}
673 
674 	return (NULL);
675 }
676