1 /*- 2 * Copyright (c) 2011-2012 Semihalf. 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 "opt_platform.h" 28 #include <sys/cdefs.h> 29 __FBSDID("$FreeBSD$"); 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/module.h> 36 #include <sys/smp.h> 37 38 #include <machine/bus.h> 39 40 #include <dev/ofw/ofw_bus.h> 41 #include <dev/ofw/ofw_bus_subr.h> 42 #include <dev/ofw/ofw_subr.h> 43 44 #include "qman.h" 45 #include "portals.h" 46 47 #define FQMAN_DEVSTR "Freescale Queue Manager" 48 49 static int qman_fdt_probe(device_t); 50 51 static device_method_t qman_methods[] = { 52 /* Device interface */ 53 DEVMETHOD(device_probe, qman_fdt_probe), 54 DEVMETHOD(device_attach, qman_attach), 55 DEVMETHOD(device_detach, qman_detach), 56 57 DEVMETHOD(device_suspend, qman_suspend), 58 DEVMETHOD(device_resume, qman_resume), 59 DEVMETHOD(device_shutdown, qman_shutdown), 60 61 { 0, 0 } 62 }; 63 64 static driver_t qman_driver = { 65 "qman", 66 qman_methods, 67 sizeof(struct qman_softc), 68 }; 69 70 EARLY_DRIVER_MODULE(qman, simplebus, qman_driver, 0, 0, BUS_PASS_SUPPORTDEV); 71 72 static int 73 qman_fdt_probe(device_t dev) 74 { 75 76 if (!ofw_bus_is_compatible(dev, "fsl,qman")) 77 return (ENXIO); 78 79 device_set_desc(dev, FQMAN_DEVSTR); 80 81 return (BUS_PROBE_DEFAULT); 82 } 83 84 /* 85 * QMAN Portals 86 */ 87 #define QMAN_PORT_DEVSTR "Freescale Queue Manager - Portals" 88 89 static device_probe_t qman_portals_fdt_probe; 90 static device_attach_t qman_portals_fdt_attach; 91 92 static device_method_t qm_portals_methods[] = { 93 /* Device interface */ 94 DEVMETHOD(device_probe, qman_portals_fdt_probe), 95 DEVMETHOD(device_attach, qman_portals_fdt_attach), 96 DEVMETHOD(device_detach, qman_portals_detach), 97 98 { 0, 0 } 99 }; 100 101 static driver_t qm_portals_driver = { 102 "qman-portals", 103 qm_portals_methods, 104 sizeof(struct dpaa_portals_softc), 105 }; 106 107 EARLY_DRIVER_MODULE(qman_portals, ofwbus, qm_portals_driver, 0, 0, 108 BUS_PASS_BUS); 109 110 static void 111 get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep) 112 { 113 114 *addrp = 2; 115 *sizep = 1; 116 OF_getencprop(node, "#address-cells", addrp, sizeof(*addrp)); 117 OF_getencprop(node, "#size-cells", sizep, sizeof(*sizep)); 118 } 119 120 static int 121 qman_portals_fdt_probe(device_t dev) 122 { 123 phandle_t node; 124 125 if (ofw_bus_is_compatible(dev, "simple-bus")) { 126 node = ofw_bus_get_node(dev); 127 for (node = OF_child(node); node > 0; node = OF_peer(node)) { 128 if (ofw_bus_node_is_compatible(node, "fsl,qman-portal")) 129 break; 130 } 131 if (node <= 0) 132 return (ENXIO); 133 } else if (!ofw_bus_is_compatible(dev, "fsl,qman-portals")) 134 return (ENXIO); 135 136 device_set_desc(dev, QMAN_PORT_DEVSTR); 137 138 return (BUS_PROBE_DEFAULT); 139 } 140 141 static phandle_t 142 qman_portal_find_cpu(int cpu) 143 { 144 phandle_t node; 145 pcell_t reg; 146 147 node = OF_finddevice("/cpus"); 148 if (node == -1) 149 return (-1); 150 151 for (node = OF_child(node); node != 0; node = OF_peer(node)) { 152 if (OF_getprop(node, "reg", ®, sizeof(reg)) <= 0) 153 continue; 154 if (reg == cpu) 155 return (node); 156 } 157 return (-1); 158 } 159 160 static int 161 qman_portals_fdt_attach(device_t dev) 162 { 163 struct dpaa_portals_softc *sc; 164 phandle_t node, child, cpu_node; 165 vm_paddr_t portal_pa, portal_par_pa; 166 vm_size_t portal_size; 167 uint32_t addr, paddr, size; 168 ihandle_t cpu; 169 int cpu_num, cpus, intr_rid; 170 struct dpaa_portals_devinfo di; 171 struct ofw_bus_devinfo ofw_di = {}; 172 cell_t *range; 173 int nrange; 174 int i; 175 176 cpus = 0; 177 sc = device_get_softc(dev); 178 sc->sc_dev = dev; 179 180 node = ofw_bus_get_node(dev); 181 182 /* Get this node's range */ 183 get_addr_props(ofw_bus_get_node(device_get_parent(dev)), &paddr, &size); 184 get_addr_props(node, &addr, &size); 185 186 nrange = OF_getencprop_alloc_multi(node, "ranges", 187 sizeof(*range), (void **)&range); 188 if (nrange < addr + paddr + size) 189 return (ENXIO); 190 portal_pa = portal_par_pa = 0; 191 portal_size = 0; 192 for (i = 0; i < addr; i++) { 193 portal_pa <<= 32; 194 portal_pa |= range[i]; 195 } 196 for (; i < paddr + addr; i++) { 197 portal_par_pa <<= 32; 198 portal_par_pa |= range[i]; 199 } 200 portal_pa += portal_par_pa; 201 for (; i < size + paddr + addr; i++) { 202 portal_size = (uintmax_t)portal_size << 32; 203 portal_size |= range[i]; 204 } 205 OF_prop_free(range); 206 sc->sc_dp_size = portal_size; 207 sc->sc_dp_pa = portal_pa; 208 209 /* Find portals tied to CPUs */ 210 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 211 if (cpus >= mp_ncpus) 212 break; 213 if (!ofw_bus_node_is_compatible(child, "fsl,qman-portal")) { 214 continue; 215 } 216 /* Checkout related cpu */ 217 if (OF_getprop(child, "cpu-handle", (void *)&cpu, 218 sizeof(cpu)) <= 0) { 219 cpu = qman_portal_find_cpu(cpus); 220 if (cpu <= 0) 221 continue; 222 } 223 /* Acquire cpu number */ 224 cpu_node = OF_instance_to_package(cpu); 225 if (OF_getencprop(cpu_node, "reg", &cpu_num, sizeof(cpu_num)) <= 0) { 226 device_printf(dev, "Could not retrieve CPU number.\n"); 227 return (ENXIO); 228 } 229 230 cpus++; 231 232 if (ofw_bus_gen_setup_devinfo(&ofw_di, child) != 0) { 233 device_printf(dev, "could not set up devinfo\n"); 234 continue; 235 } 236 237 resource_list_init(&di.di_res); 238 if (ofw_bus_reg_to_rl(dev, child, addr, size, &di.di_res)) { 239 device_printf(dev, "%s: could not process 'reg' " 240 "property\n", ofw_di.obd_name); 241 ofw_bus_gen_destroy_devinfo(&ofw_di); 242 continue; 243 } 244 if (ofw_bus_intr_to_rl(dev, child, &di.di_res, &intr_rid)) { 245 device_printf(dev, "%s: could not process " 246 "'interrupts' property\n", ofw_di.obd_name); 247 resource_list_free(&di.di_res); 248 ofw_bus_gen_destroy_devinfo(&ofw_di); 249 continue; 250 } 251 di.di_intr_rid = intr_rid; 252 253 if (dpaa_portal_alloc_res(dev, &di, cpu_num)) 254 goto err; 255 } 256 257 ofw_bus_gen_destroy_devinfo(&ofw_di); 258 259 return (qman_portals_attach(dev)); 260 err: 261 resource_list_free(&di.di_res); 262 ofw_bus_gen_destroy_devinfo(&ofw_di); 263 qman_portals_detach(dev); 264 return (ENXIO); 265 } 266