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