2 * Copyright 2006 John-Mark Gurney.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * Support for the Hypervisor PCI bus.
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/module.h>
42 #include <sys/endian.h>
44 #include <dev/pci/pcireg.h>
45 #include <dev/pci/pcivar.h>
47 #include <machine/bus.h>
49 #include <machine/hypervisorvar.h>
50 #include <machine/hv_api.h>
52 #include <dev/ofw/ofw_bus.h>
53 #include <dev/ofw/ofw_bus_subr.h>
54 #include <dev/ofw/ofw_pci.h>
55 #include <dev/ofw/openfirm.h>
56 #include <sparc64/pci/ofw_pci.h>
58 #include <machine/hv_pcivar.h>
59 #include <machine/hviommu.h>
60 #include <machine/vmparam.h>
61 #include <machine/tlb.h>
62 #include <machine/nexusvar.h>
67 * XXX - should get this through the bus, but Sun overloaded the reg OFW
68 * property, so there isn't normal resources associated w/ this device.
70 extern struct bus_space_tag nexus_bustag;
74 static device_probe_t hvpci_probe;
75 static device_attach_t hvpci_attach;
76 static bus_read_ivar_t hvpci_read_ivar;
77 static bus_write_ivar_t hvpci_write_ivar;
78 static bus_alloc_resource_t hvpci_alloc_resource;
79 static bus_activate_resource_t hvpci_activate_resource;
80 static bus_deactivate_resource_t hvpci_deactivate_resource;
81 static bus_release_resource_t hvpci_release_resource;
82 static bus_get_dma_tag_t hvpci_get_dma_tag;
83 static pcib_maxslots_t hvpci_maxslots;
84 static pcib_read_config_t hvpci_read_config;
85 static pcib_write_config_t hvpci_write_config;
86 static pcib_route_interrupt_t hvpci_route_interrupt;
87 static ofw_bus_get_node_t hvpci_get_node;
89 static device_method_t hv_pcib_methods[] = {
90 /* Device interface */
91 DEVMETHOD(device_probe, hvpci_probe),
92 DEVMETHOD(device_attach, hvpci_attach),
93 DEVMETHOD(device_shutdown, bus_generic_shutdown),
94 DEVMETHOD(device_suspend, bus_generic_suspend),
95 DEVMETHOD(device_resume, bus_generic_resume),
98 DEVMETHOD(bus_print_child, bus_generic_print_child),
99 DEVMETHOD(bus_read_ivar, hvpci_read_ivar),
100 DEVMETHOD(bus_write_ivar, hvpci_write_ivar),
101 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr),
102 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr),
103 DEVMETHOD(bus_alloc_resource, hvpci_alloc_resource),
104 DEVMETHOD(bus_activate_resource, hvpci_activate_resource),
105 DEVMETHOD(bus_deactivate_resource, hvpci_deactivate_resource),
106 DEVMETHOD(bus_release_resource, hvpci_release_resource),
107 DEVMETHOD(bus_get_dma_tag, hvpci_get_dma_tag),
110 DEVMETHOD(pcib_maxslots, hvpci_maxslots),
111 DEVMETHOD(pcib_read_config, hvpci_read_config),
112 DEVMETHOD(pcib_write_config, hvpci_write_config),
113 DEVMETHOD(pcib_route_interrupt, hvpci_route_interrupt),
115 /* ofw_bus interface */
116 DEVMETHOD(ofw_bus_get_node, hvpci_get_node),
121 static driver_t hvpci_driver = {
124 sizeof(struct hvpci_softc),
127 static devclass_t hvpci_devclass;
129 DRIVER_MODULE(hvpci, nexus, hvpci_driver, hvpci_devclass, 0, 0);
132 hvpci_probe(device_t dev)
135 if (strcmp(ofw_bus_get_name(dev), "pci") != 0)
138 device_set_desc(dev, "Hypervisor PCI Bridge");
143 hvpci_attach(device_t dev)
145 struct ofw_pci_ranges *range;
147 struct hvpci_softc *sc;
148 struct hviommu *himp;
149 bus_space_tag_t *btp;
156 sc = device_get_softc(dev);
158 node = ofw_bus_get_node(dev);
160 panic("%s: ofw_bus_get_node failed.", __func__);
164 /* Setup the root bus number for this bus */
165 n = OF_getprop(node, "bus-range", &br[0], sizeof br);
167 panic("%s: could not get bus-range", __func__);
169 panic("%s: broken bus-range (%d)", __func__, n);
170 sc->hs_busnum = br[0];
172 /* Setup the HyperVisor devhandle for this bus */
173 sc->hs_devhandle = nexus_get_devhandle(dev);
175 /* Pull in the ra addresses out of OFW */
176 nrange = OF_getprop_alloc(node, "ranges", sizeof *range,
179 /* Initialize memory and I/O rmans. */
180 for (i = 0; i < nrange; i++) {
181 /* XXX - from sun4v/io/px/px_lib4v.c: px_ranges_phi_mask */
182 #define PHYS_MASK ((1ll << (28 + 32)) - 1)
183 switch (OFW_PCI_RANGE_CS(&range[i])) {
185 rmanp = &sc->hs_pci_io_rman;
186 rmanp->rm_descr = "HyperVisor PCI I/O Ports";
187 btp = &sc->hs_pci_iot;
188 sc->hs_pci_ioh = OFW_PCI_RANGE_PHYS(&range[i]) &
190 type = PCI_IO_BUS_SPACE;
192 printf("io handle: %#lx\n", sc->hs_pci_ioh);
196 case OFW_PCI_CS_MEM32:
197 rmanp = &sc->hs_pci_mem_rman;
198 rmanp->rm_descr = "HyperVisor PCI Memory";
199 btp = &sc->hs_pci_memt;
200 sc->hs_pci_memh = OFW_PCI_RANGE_PHYS(&range[i]) &
202 type = PCI_MEMORY_BUS_SPACE;
205 case OFW_PCI_CS_MEM64:
209 panic("%s: unknown range type: %d", __func__,
210 OFW_PCI_RANGE_CS(&range[i]));
212 rmanp->rm_type = RMAN_ARRAY;
213 if (rman_init(rmanp) != 0 || rman_manage_region(rmanp, 0,
214 OFW_PCI_RANGE_SIZE(&range[i])) != 0)
215 panic("%s: failed to set up rman type: %d", __func__,
216 OFW_PCI_RANGE_CS(&range[i]));
218 *btp = (bus_space_tag_t)malloc(sizeof **btp, M_DEVBUF,
220 (*btp)->bst_parent = &nexus_bustag;
221 (*btp)->bst_type = type;
223 free(range, M_OFWPROP);
225 nrange = OF_getprop_alloc(node, "virtual-dma", sizeof *dvma,
227 KASSERT(nrange == 2, ("virtual-dma propery invalid"));
229 /* Setup bus_dma_tag */
230 himp = hviommu_init(sc->hs_devhandle, dvma[0], dvma[1]);
231 sc->hs_dmatag.dt_cookie = himp;
232 sc->hs_dmatag.dt_mt = &hviommu_dma_methods;
233 sc->hs_dmatag.dt_lowaddr = ~0;
234 sc->hs_dmatag.dt_highaddr = ~0;
235 sc->hs_dmatag.dt_boundary = BUS_SPACE_MAXADDR_32BIT + 1;
237 free(dvma, M_OFWPROP);
240 ofw_bus_setup_iinfo(node, &sc->hs_pci_iinfo, sizeof(ofw_pci_intr_t));
242 device_add_child(dev, "pci", -1);
244 return (bus_generic_attach(dev));
248 hvpci_maxslots(device_t dev)
254 #define HVPCI_BDF(b, d, f) \
255 ((b & 0xff) << 16) | ((d & 0x1f) << 11) | ((f & 0x7) << 8)
257 hvpci_read_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
260 struct hvpci_softc *sc;
265 sc = device_get_softc(dev);
267 r = hv_pci_config_get(sc->hs_devhandle, HVPCI_BDF(bus, slot, func),
268 reg, width, (pci_cfg_data_t *)&data);
274 if (ret == 0 && reg == PCIR_INTLINE)
275 ret = PCI_INVALID_IRQ;
293 hvpci_write_config(device_t dev, u_int bus, u_int slot, u_int func, u_int reg,
294 uint32_t val, int width)
296 struct hvpci_softc *sc;
297 pci_cfg_data_t data = { 0 };
300 sc = device_get_softc(dev);
303 data.qw = (uint8_t)val;
306 data.qw = (uint16_t)(val & 0xffff);
309 data.qw = (uint32_t)val;
312 panic("unsupported width: %d", width);
315 r = hv_pci_config_put(sc->hs_devhandle, HVPCI_BDF(bus, slot, func),
316 reg, width, (pci_cfg_data_t)data);
319 printf("put failed with: %ld\n", r);
323 hvpci_route_interrupt(device_t bridge, device_t dev, int pin)
325 struct hvpci_softc *sc;
326 struct ofw_pci_register reg;
328 ofw_pci_intr_t pintr, mintr;
330 uint8_t maskbuf[sizeof(reg) + sizeof(pintr)];
332 sc = device_get_softc(bridge);
333 node = ofw_bus_get_node(dev);
335 obli = ofw_bus_lookup_imap(node, &sc->hs_pci_iinfo, ®, sizeof(reg),
336 &pintr, sizeof(pintr), &mintr, sizeof(mintr), maskbuf);
337 device_printf(dev, "called hvpci_route_intr: %d, got: mintr: %#x\n",
342 panic("pin %d not found in imap of %s", pin, device_get_nameunit(bridge));
346 hvpci_get_node(device_t bus, device_t dev)
348 struct hvpci_softc *sc;
350 sc = device_get_softc(bus);
352 return (sc->hs_node);
356 hvpci_read_ivar(device_t dev, device_t child, int which, uintptr_t *result)
358 struct hvpci_softc *sc;
360 sc = device_get_softc(dev);
363 case PCIB_IVAR_DOMAIN:
367 *result = sc->hs_busnum;
375 hvpci_write_ivar(device_t dev, device_t child, int which, uintptr_t value)
377 struct hvpci_softc *sc;
379 sc = device_get_softc(dev);
381 case PCIB_IVAR_DOMAIN:
384 sc->hs_busnum = value;
391 static struct resource *
392 hvpci_alloc_resource(device_t bus, device_t child, int type, int *rid,
393 u_long start, u_long end, u_long count, u_int flags)
395 struct hvpci_softc *sc;
399 bus_space_handle_t bh;
402 sc = device_get_softc(bus);
404 needactivate = flags & RF_ACTIVE;
409 return BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
410 rid, start, end, count, flags);
414 rm = &sc->hs_pci_mem_rman;
415 bt = sc->hs_pci_memt;
416 bh = sc->hs_pci_memh;
421 printf("alloc: start: %#lx, end: %#lx, count: %#lx\n", start, end, count);
423 rm = &sc->hs_pci_io_rman;
432 rv = rman_reserve_resource(rm, start, end, count, flags, child);
436 bh += rman_get_start(rv);
437 rman_set_bustag(rv, bt);
438 rman_set_bushandle(rv, bh);
441 if (bus_activate_resource(child, type, *rid, rv)) {
442 rman_release_resource(rv);
451 hvpci_activate_resource(device_t bus, device_t child, int type, int rid,
456 if (type == SYS_RES_MEMORY) {
457 /* XXX - we may still need to set the IE bit on the mapping */
458 p = (void *)TLB_PHYS_TO_DIRECT(rman_get_bushandle(r));
459 rman_set_virtual(r, p);
461 return (rman_activate_resource(r));
465 hvpci_deactivate_resource(device_t bus, device_t child, int type, int rid,
473 hvpci_release_resource(device_t bus, device_t child, int type, int rid,
481 hvpci_get_dma_tag(device_t bus, device_t child)
483 struct hvpci_softc *sc;
485 sc = device_get_softc(bus);
487 return &sc->hs_dmatag;