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 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/malloc.h> 35 #include <sys/bus.h> 36 #include <sys/cpu.h> 37 #include <machine/bus.h> 38 #include <machine/intr_machdep.h> 39 40 #include <dev/ofw/openfirm.h> 41 #include <dev/ofw/ofw_bus.h> 42 #include <dev/ofw/ofw_bus_subr.h> 43 44 #include <powerpc/pseries/plpar_iommu.h> 45 46 #include "iommu_if.h" 47 48 static int vdevice_probe(device_t); 49 static int vdevice_attach(device_t); 50 static const struct ofw_bus_devinfo *vdevice_get_devinfo(device_t dev, 51 device_t child); 52 static int vdevice_print_child(device_t dev, device_t child); 53 static struct resource_list *vdevice_get_resource_list(device_t, device_t); 54 static bus_dma_tag_t vdevice_get_dma_tag(device_t dev, device_t child); 55 56 /* 57 * VDevice devinfo 58 */ 59 struct vdevice_devinfo { 60 struct ofw_bus_devinfo mdi_obdinfo; 61 struct resource_list mdi_resources; 62 bus_dma_tag_t mdi_dma_tag; 63 }; 64 65 static device_method_t vdevice_methods[] = { 66 /* Device interface */ 67 DEVMETHOD(device_probe, vdevice_probe), 68 DEVMETHOD(device_attach, vdevice_attach), 69 70 /* Bus interface */ 71 DEVMETHOD(bus_add_child, bus_generic_add_child), 72 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 73 DEVMETHOD(bus_print_child, vdevice_print_child), 74 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 75 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 76 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 77 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 78 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 79 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 80 DEVMETHOD(bus_get_resource_list, vdevice_get_resource_list), 81 82 /* ofw_bus interface */ 83 DEVMETHOD(ofw_bus_get_devinfo, vdevice_get_devinfo), 84 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 85 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 86 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 87 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 88 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 89 90 /* IOMMU interface */ 91 DEVMETHOD(bus_get_dma_tag, vdevice_get_dma_tag), 92 DEVMETHOD(iommu_map, phyp_iommu_map), 93 DEVMETHOD(iommu_unmap, phyp_iommu_unmap), 94 95 DEVMETHOD_END 96 }; 97 98 static driver_t vdevice_driver = { 99 "vdevice", 100 vdevice_methods, 101 0 102 }; 103 104 static devclass_t vdevice_devclass; 105 106 DRIVER_MODULE(vdevice, nexus, vdevice_driver, vdevice_devclass, 0, 0); 107 108 static int 109 vdevice_probe(device_t dev) 110 { 111 const char *name; 112 113 name = ofw_bus_get_name(dev); 114 115 if (name == NULL || strcmp(name, "vdevice") != 0) 116 return (ENXIO); 117 118 if (!ofw_bus_is_compatible(dev, "IBM,vdevice")) 119 return (ENXIO); 120 121 device_set_desc(dev, "POWER Hypervisor Virtual Device Root"); 122 123 return (0); 124 } 125 126 static int 127 vdevice_attach(device_t dev) 128 { 129 phandle_t root, child; 130 device_t cdev; 131 int icells, i, nintr, *intr; 132 phandle_t iparent; 133 struct vdevice_devinfo *dinfo; 134 135 root = ofw_bus_get_node(dev); 136 137 for (child = OF_child(root); child != 0; child = OF_peer(child)) { 138 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); 139 140 if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, 141 child) != 0) { 142 free(dinfo, M_DEVBUF); 143 continue; 144 } 145 resource_list_init(&dinfo->mdi_resources); 146 147 if (OF_searchprop(child, "#interrupt-cells", &icells, 148 sizeof(icells)) <= 0) 149 icells = 2; 150 if (OF_getprop(child, "interrupt-parent", &iparent, 151 sizeof(iparent)) <= 0) 152 iparent = -1; 153 nintr = OF_getprop_alloc(child, "interrupts", sizeof(*intr), 154 (void **)&intr); 155 if (nintr > 0) { 156 for (i = 0; i < nintr; i += icells) { 157 u_int irq = intr[i]; 158 if (iparent != -1) 159 irq = ofw_bus_map_intr(dev, iparent, 160 intr[i]); 161 162 resource_list_add(&dinfo->mdi_resources, 163 SYS_RES_IRQ, i, irq, irq, i); 164 } 165 } 166 167 cdev = device_add_child(dev, NULL, -1); 168 if (cdev == NULL) { 169 device_printf(dev, "<%s>: device_add_child failed\n", 170 dinfo->mdi_obdinfo.obd_name); 171 ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo); 172 free(dinfo, M_DEVBUF); 173 continue; 174 } 175 device_set_ivars(cdev, dinfo); 176 } 177 178 return (bus_generic_attach(dev)); 179 } 180 181 static const struct ofw_bus_devinfo * 182 vdevice_get_devinfo(device_t dev, device_t child) 183 { 184 return (device_get_ivars(child)); 185 } 186 187 static int 188 vdevice_print_child(device_t dev, device_t child) 189 { 190 struct vdevice_devinfo *dinfo; 191 struct resource_list *rl; 192 int retval = 0; 193 194 dinfo = device_get_ivars(child); 195 rl = &dinfo->mdi_resources; 196 197 retval += bus_print_child_header(dev, child); 198 199 retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 200 201 retval += bus_print_child_footer(dev, child); 202 203 return (retval); 204 } 205 206 static struct resource_list * 207 vdevice_get_resource_list (device_t dev, device_t child) 208 { 209 struct vdevice_devinfo *dinfo; 210 211 dinfo = device_get_ivars(child); 212 return (&dinfo->mdi_resources); 213 } 214 215 static bus_dma_tag_t 216 vdevice_get_dma_tag(device_t dev, device_t child) 217 { 218 struct vdevice_devinfo *dinfo; 219 while (child != NULL && device_get_parent(child) != dev) 220 child = device_get_parent(child); 221 dinfo = device_get_ivars(child); 222 223 if (dinfo->mdi_dma_tag == NULL) { 224 bus_dma_tag_create(bus_get_dma_tag(dev), 225 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 226 NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED, 227 BUS_SPACE_MAXSIZE, 0, NULL, NULL, &dinfo->mdi_dma_tag); 228 phyp_iommu_set_dma_tag(dev, child, dinfo->mdi_dma_tag); 229 } 230 231 return (dinfo->mdi_dma_tag); 232 } 233 234