vdevice.c revision 255927
1255643Snwhitehorn/*- 2255643Snwhitehorn * Copyright (c) 2011 Nathan Whitehorn 3255643Snwhitehorn * All rights reserved. 4255643Snwhitehorn * 5255643Snwhitehorn * Redistribution and use in source and binary forms, with or without 6255643Snwhitehorn * modification, are permitted provided that the following conditions 7255643Snwhitehorn * are met: 8255643Snwhitehorn * 1. Redistributions of source code must retain the above copyright 9255643Snwhitehorn * notice, this list of conditions and the following disclaimer. 10255643Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright 11255643Snwhitehorn * notice, this list of conditions and the following disclaimer in the 12255643Snwhitehorn * documentation and/or other materials provided with the distribution. 13255643Snwhitehorn * 14255643Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15255643Snwhitehorn * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16255643Snwhitehorn * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17255643Snwhitehorn * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18255643Snwhitehorn * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19255643Snwhitehorn * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20255643Snwhitehorn * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21255643Snwhitehorn * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22255643Snwhitehorn * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23255643Snwhitehorn * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24255643Snwhitehorn * SUCH DAMAGE. 25255643Snwhitehorn */ 26255643Snwhitehorn 27255643Snwhitehorn#include <sys/cdefs.h> 28255643Snwhitehorn__FBSDID("$FreeBSD: head/sys/powerpc/pseries/vdevice.c 255927 2013-09-28 15:46:03Z nwhitehorn $"); 29255643Snwhitehorn 30255643Snwhitehorn#include <sys/param.h> 31255643Snwhitehorn#include <sys/systm.h> 32255643Snwhitehorn#include <sys/kernel.h> 33255643Snwhitehorn#include <sys/module.h> 34255643Snwhitehorn#include <sys/malloc.h> 35255643Snwhitehorn#include <sys/bus.h> 36255643Snwhitehorn#include <sys/cpu.h> 37255643Snwhitehorn#include <machine/bus.h> 38255643Snwhitehorn#include <machine/intr_machdep.h> 39255643Snwhitehorn 40255643Snwhitehorn#include <dev/ofw/openfirm.h> 41255643Snwhitehorn#include <dev/ofw/ofw_bus.h> 42255643Snwhitehorn#include <dev/ofw/ofw_bus_subr.h> 43255643Snwhitehorn 44255927Snwhitehorn#include <powerpc/pseries/plpar_iommu.h> 45255927Snwhitehorn 46255927Snwhitehorn#include "iommu_if.h" 47255927Snwhitehorn 48255643Snwhitehornstatic int vdevice_probe(device_t); 49255643Snwhitehornstatic int vdevice_attach(device_t); 50255643Snwhitehornstatic const struct ofw_bus_devinfo *vdevice_get_devinfo(device_t dev, 51255643Snwhitehorn device_t child); 52255643Snwhitehornstatic int vdevice_print_child(device_t dev, device_t child); 53255927Snwhitehornstatic struct resource_list *vdevice_get_resource_list(device_t, device_t); 54255927Snwhitehornstatic bus_dma_tag_t vdevice_get_dma_tag(device_t dev, device_t child); 55255643Snwhitehorn 56255643Snwhitehorn/* 57255643Snwhitehorn * VDevice devinfo 58255643Snwhitehorn */ 59255643Snwhitehornstruct vdevice_devinfo { 60255643Snwhitehorn struct ofw_bus_devinfo mdi_obdinfo; 61255643Snwhitehorn struct resource_list mdi_resources; 62255927Snwhitehorn bus_dma_tag_t mdi_dma_tag; 63255643Snwhitehorn}; 64255643Snwhitehorn 65255643Snwhitehornstatic device_method_t vdevice_methods[] = { 66255643Snwhitehorn /* Device interface */ 67255643Snwhitehorn DEVMETHOD(device_probe, vdevice_probe), 68255643Snwhitehorn DEVMETHOD(device_attach, vdevice_attach), 69255643Snwhitehorn 70255643Snwhitehorn /* Bus interface */ 71255643Snwhitehorn DEVMETHOD(bus_add_child, bus_generic_add_child), 72255643Snwhitehorn DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 73255643Snwhitehorn DEVMETHOD(bus_print_child, vdevice_print_child), 74255643Snwhitehorn DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 75255643Snwhitehorn DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 76255643Snwhitehorn DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 77255643Snwhitehorn DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 78255643Snwhitehorn DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 79255643Snwhitehorn DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 80255643Snwhitehorn DEVMETHOD(bus_get_resource_list, vdevice_get_resource_list), 81255643Snwhitehorn 82255643Snwhitehorn /* ofw_bus interface */ 83255643Snwhitehorn DEVMETHOD(ofw_bus_get_devinfo, vdevice_get_devinfo), 84255643Snwhitehorn DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 85255643Snwhitehorn DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 86255643Snwhitehorn DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 87255643Snwhitehorn DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 88255643Snwhitehorn DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 89255643Snwhitehorn 90255927Snwhitehorn /* IOMMU interface */ 91255927Snwhitehorn DEVMETHOD(bus_get_dma_tag, vdevice_get_dma_tag), 92255927Snwhitehorn DEVMETHOD(iommu_map, phyp_iommu_map), 93255927Snwhitehorn DEVMETHOD(iommu_unmap, phyp_iommu_unmap), 94255927Snwhitehorn 95255643Snwhitehorn DEVMETHOD_END 96255643Snwhitehorn}; 97255643Snwhitehorn 98255643Snwhitehornstatic driver_t vdevice_driver = { 99255643Snwhitehorn "vdevice", 100255643Snwhitehorn vdevice_methods, 101255643Snwhitehorn 0 102255643Snwhitehorn}; 103255643Snwhitehorn 104255643Snwhitehornstatic devclass_t vdevice_devclass; 105255643Snwhitehorn 106255643SnwhitehornDRIVER_MODULE(vdevice, nexus, vdevice_driver, vdevice_devclass, 0, 0); 107255643Snwhitehorn 108255643Snwhitehornstatic int 109255643Snwhitehornvdevice_probe(device_t dev) 110255643Snwhitehorn{ 111255643Snwhitehorn const char *name; 112255643Snwhitehorn 113255643Snwhitehorn name = ofw_bus_get_name(dev); 114255643Snwhitehorn 115255643Snwhitehorn if (name == NULL || strcmp(name, "vdevice") != 0) 116255643Snwhitehorn return (ENXIO); 117255643Snwhitehorn 118255643Snwhitehorn if (!ofw_bus_is_compatible(dev, "IBM,vdevice")) 119255643Snwhitehorn return (ENXIO); 120255643Snwhitehorn 121255643Snwhitehorn device_set_desc(dev, "POWER Hypervisor Virtual Device Root"); 122255643Snwhitehorn 123255643Snwhitehorn return (0); 124255643Snwhitehorn} 125255643Snwhitehorn 126255643Snwhitehornstatic int 127255643Snwhitehornvdevice_attach(device_t dev) 128255643Snwhitehorn{ 129255643Snwhitehorn phandle_t root, child; 130255643Snwhitehorn device_t cdev; 131255643Snwhitehorn int icells, i, nintr, *intr; 132255643Snwhitehorn phandle_t iparent; 133255643Snwhitehorn struct vdevice_devinfo *dinfo; 134255643Snwhitehorn 135255643Snwhitehorn root = ofw_bus_get_node(dev); 136255643Snwhitehorn 137255643Snwhitehorn for (child = OF_child(root); child != 0; child = OF_peer(child)) { 138255643Snwhitehorn dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); 139255643Snwhitehorn 140255643Snwhitehorn if (ofw_bus_gen_setup_devinfo(&dinfo->mdi_obdinfo, 141255643Snwhitehorn child) != 0) { 142255643Snwhitehorn free(dinfo, M_DEVBUF); 143255643Snwhitehorn continue; 144255643Snwhitehorn } 145255643Snwhitehorn resource_list_init(&dinfo->mdi_resources); 146255643Snwhitehorn 147255643Snwhitehorn if (OF_searchprop(child, "#interrupt-cells", &icells, 148255643Snwhitehorn sizeof(icells)) <= 0) 149255643Snwhitehorn icells = 2; 150255643Snwhitehorn if (OF_getprop(child, "interrupt-parent", &iparent, 151255643Snwhitehorn sizeof(iparent)) <= 0) 152255643Snwhitehorn iparent = -1; 153255643Snwhitehorn nintr = OF_getprop_alloc(child, "interrupts", sizeof(*intr), 154255643Snwhitehorn (void **)&intr); 155255643Snwhitehorn if (nintr > 0) { 156255643Snwhitehorn for (i = 0; i < nintr; i += icells) { 157255643Snwhitehorn u_int irq = intr[i]; 158255643Snwhitehorn if (iparent != -1) 159255643Snwhitehorn irq = MAP_IRQ(iparent, intr[i]); 160255643Snwhitehorn 161255643Snwhitehorn resource_list_add(&dinfo->mdi_resources, 162255643Snwhitehorn SYS_RES_IRQ, i, irq, irq, i); 163255643Snwhitehorn } 164255643Snwhitehorn } 165255643Snwhitehorn 166255643Snwhitehorn cdev = device_add_child(dev, NULL, -1); 167255643Snwhitehorn if (cdev == NULL) { 168255643Snwhitehorn device_printf(dev, "<%s>: device_add_child failed\n", 169255643Snwhitehorn dinfo->mdi_obdinfo.obd_name); 170255643Snwhitehorn ofw_bus_gen_destroy_devinfo(&dinfo->mdi_obdinfo); 171255643Snwhitehorn free(dinfo, M_DEVBUF); 172255643Snwhitehorn continue; 173255643Snwhitehorn } 174255643Snwhitehorn device_set_ivars(cdev, dinfo); 175255643Snwhitehorn } 176255643Snwhitehorn 177255643Snwhitehorn return (bus_generic_attach(dev)); 178255643Snwhitehorn} 179255643Snwhitehorn 180255643Snwhitehornstatic const struct ofw_bus_devinfo * 181255643Snwhitehornvdevice_get_devinfo(device_t dev, device_t child) 182255643Snwhitehorn{ 183255643Snwhitehorn return (device_get_ivars(child)); 184255643Snwhitehorn} 185255643Snwhitehorn 186255643Snwhitehornstatic int 187255643Snwhitehornvdevice_print_child(device_t dev, device_t child) 188255643Snwhitehorn{ 189255643Snwhitehorn struct vdevice_devinfo *dinfo; 190255643Snwhitehorn struct resource_list *rl; 191255643Snwhitehorn int retval = 0; 192255643Snwhitehorn 193255643Snwhitehorn dinfo = device_get_ivars(child); 194255643Snwhitehorn rl = &dinfo->mdi_resources; 195255643Snwhitehorn 196255643Snwhitehorn retval += bus_print_child_header(dev, child); 197255643Snwhitehorn 198255643Snwhitehorn retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 199255643Snwhitehorn 200255643Snwhitehorn retval += bus_print_child_footer(dev, child); 201255643Snwhitehorn 202255643Snwhitehorn return (retval); 203255643Snwhitehorn} 204255643Snwhitehorn 205255643Snwhitehornstatic struct resource_list * 206255643Snwhitehornvdevice_get_resource_list (device_t dev, device_t child) 207255643Snwhitehorn{ 208255643Snwhitehorn struct vdevice_devinfo *dinfo; 209255643Snwhitehorn 210255643Snwhitehorn dinfo = device_get_ivars(child); 211255643Snwhitehorn return (&dinfo->mdi_resources); 212255643Snwhitehorn} 213255643Snwhitehorn 214255927Snwhitehornstatic bus_dma_tag_t 215255927Snwhitehornvdevice_get_dma_tag(device_t dev, device_t child) 216255927Snwhitehorn{ 217255927Snwhitehorn struct vdevice_devinfo *dinfo; 218255927Snwhitehorn while (child != NULL && device_get_parent(child) != dev) 219255927Snwhitehorn child = device_get_parent(child); 220255927Snwhitehorn dinfo = device_get_ivars(child); 221255927Snwhitehorn 222255927Snwhitehorn if (dinfo->mdi_dma_tag == NULL) { 223255927Snwhitehorn bus_dma_tag_create(bus_get_dma_tag(dev), 224255927Snwhitehorn 1, 0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, 225255927Snwhitehorn NULL, NULL, BUS_SPACE_MAXSIZE, BUS_SPACE_UNRESTRICTED, 226255927Snwhitehorn BUS_SPACE_MAXSIZE, 0, NULL, NULL, &dinfo->mdi_dma_tag); 227255927Snwhitehorn phyp_iommu_set_dma_tag(dev, child, dinfo->mdi_dma_tag); 228255927Snwhitehorn } 229255927Snwhitehorn 230255927Snwhitehorn return (dinfo->mdi_dma_tag); 231255927Snwhitehorn} 232255927Snwhitehorn 233