1 /*- 2 * Copyright 1998 Massachusetts Institute of Technology 3 * Copyright 2001 by Thomas Moestl <tmm@FreeBSD.org>. 4 * Copyright 2006 by Marius Strobl <marius@FreeBSD.org>. 5 * All rights reserved. 6 * 7 * Permission to use, copy, modify, and distribute this software and 8 * its documentation for any purpose and without fee is hereby 9 * granted, provided that both the above copyright notice and this 10 * permission notice appear in all copies, that both the above 11 * copyright notice and this permission notice appear in all 12 * supporting documentation, and that the name of M.I.T. not be used 13 * in advertising or publicity pertaining to distribution of the 14 * software without specific, written prior permission. M.I.T. makes 15 * no representations about the suitability of this software for any 16 * purpose. It is provided "as is" without express or implied 17 * warranty. 18 * 19 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 20 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 21 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 22 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 23 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 29 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * from: FreeBSD: src/sys/i386/i386/nexus.c,v 1.43 2001/02/09 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/bus.h> 41 #include <sys/kernel.h> 42 #include <sys/malloc.h> 43 #include <sys/module.h> 44 #include <sys/pcpu.h> 45 #include <sys/rman.h> 46 #ifdef INTRNG 47 #include <sys/intr.h> 48 #endif 49 50 #include <vm/vm.h> 51 #include <vm/pmap.h> 52 53 #include <dev/ofw/ofw_bus.h> 54 #include <dev/ofw/ofw_bus_subr.h> 55 #include <dev/ofw/openfirm.h> 56 #include <dev/fdt/simplebus.h> 57 58 #include <machine/bus.h> 59 #include <machine/resource.h> 60 61 /* 62 * The ofwbus (which is a pseudo-bus actually) iterates over the nodes that 63 * hang from the Open Firmware root node and adds them as devices to this bus 64 * (except some special nodes which are excluded) so that drivers can be 65 * attached to them. 66 * 67 */ 68 69 struct ofwbus_softc { 70 struct simplebus_softc simplebus_sc; 71 struct rman sc_intr_rman; 72 struct rman sc_mem_rman; 73 }; 74 75 #ifndef __aarch64__ 76 static device_identify_t ofwbus_identify; 77 #endif 78 static device_probe_t ofwbus_probe; 79 static device_attach_t ofwbus_attach; 80 static bus_alloc_resource_t ofwbus_alloc_resource; 81 static bus_adjust_resource_t ofwbus_adjust_resource; 82 static bus_release_resource_t ofwbus_release_resource; 83 #ifdef INTRNG 84 static bus_map_intr_t ofwbus_map_intr; 85 #endif 86 87 static device_method_t ofwbus_methods[] = { 88 /* Device interface */ 89 #ifndef __aarch64__ 90 DEVMETHOD(device_identify, ofwbus_identify), 91 #endif 92 DEVMETHOD(device_probe, ofwbus_probe), 93 DEVMETHOD(device_attach, ofwbus_attach), 94 95 /* Bus interface */ 96 DEVMETHOD(bus_alloc_resource, ofwbus_alloc_resource), 97 DEVMETHOD(bus_adjust_resource, ofwbus_adjust_resource), 98 DEVMETHOD(bus_release_resource, ofwbus_release_resource), 99 #ifdef INTRNG 100 DEVMETHOD(bus_map_intr, ofwbus_map_intr), 101 #endif 102 103 DEVMETHOD_END 104 }; 105 106 DEFINE_CLASS_1(ofwbus, ofwbus_driver, ofwbus_methods, 107 sizeof(struct ofwbus_softc), simplebus_driver); 108 static devclass_t ofwbus_devclass; 109 EARLY_DRIVER_MODULE(ofwbus, nexus, ofwbus_driver, ofwbus_devclass, 0, 0, 110 BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE); 111 MODULE_VERSION(ofwbus, 1); 112 113 #ifndef __aarch64__ 114 static void 115 ofwbus_identify(driver_t *driver, device_t parent) 116 { 117 118 /* Check if Open Firmware has been instantiated */ 119 if (OF_peer(0) == 0) 120 return; 121 122 if (device_find_child(parent, "ofwbus", -1) == NULL) 123 BUS_ADD_CHILD(parent, 0, "ofwbus", -1); 124 } 125 #endif 126 127 static int 128 ofwbus_probe(device_t dev) 129 { 130 131 #ifdef __aarch64__ 132 if (OF_peer(0) == 0) 133 return (ENXIO); 134 #endif 135 136 device_set_desc(dev, "Open Firmware Device Tree"); 137 return (BUS_PROBE_NOWILDCARD); 138 } 139 140 static int 141 ofwbus_attach(device_t dev) 142 { 143 struct ofwbus_softc *sc; 144 phandle_t node; 145 struct ofw_bus_devinfo obd; 146 147 sc = device_get_softc(dev); 148 149 node = OF_peer(0); 150 151 /* 152 * If no Open Firmware, bail early 153 */ 154 if (node == -1) 155 return (ENXIO); 156 157 /* 158 * ofwbus bus starts on unamed node in FDT, so we cannot make 159 * ofw_bus_devinfo from it. Pass node to simplebus_init directly. 160 */ 161 simplebus_init(dev, node); 162 sc->sc_intr_rman.rm_type = RMAN_ARRAY; 163 sc->sc_intr_rman.rm_descr = "Interrupts"; 164 sc->sc_mem_rman.rm_type = RMAN_ARRAY; 165 sc->sc_mem_rman.rm_descr = "Device Memory"; 166 if (rman_init(&sc->sc_intr_rman) != 0 || 167 rman_init(&sc->sc_mem_rman) != 0 || 168 rman_manage_region(&sc->sc_intr_rman, 0, ~0) != 0 || 169 rman_manage_region(&sc->sc_mem_rman, 0, BUS_SPACE_MAXADDR) != 0) 170 panic("%s: failed to set up rmans.", __func__); 171 172 /* 173 * Allow devices to identify. 174 */ 175 bus_generic_probe(dev); 176 177 /* 178 * Now walk the OFW tree and attach top-level devices. 179 */ 180 for (node = OF_child(node); node > 0; node = OF_peer(node)) { 181 if (ofw_bus_gen_setup_devinfo(&obd, node) != 0) 182 continue; 183 simplebus_add_device(dev, node, 0, NULL, -1, NULL); 184 } 185 return (bus_generic_attach(dev)); 186 } 187 188 static struct resource * 189 ofwbus_alloc_resource(device_t bus, device_t child, int type, int *rid, 190 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 191 { 192 struct ofwbus_softc *sc; 193 struct rman *rm; 194 struct resource *rv; 195 struct resource_list_entry *rle; 196 int isdefault, passthrough; 197 198 isdefault = RMAN_IS_DEFAULT_RANGE(start, end); 199 passthrough = (device_get_parent(child) != bus); 200 sc = device_get_softc(bus); 201 rle = NULL; 202 if (!passthrough && isdefault) { 203 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), 204 type, *rid); 205 if (rle == NULL) { 206 if (bootverbose) 207 device_printf(bus, "no default resources for " 208 "rid = %d, type = %d\n", *rid, type); 209 return (NULL); 210 } 211 start = rle->start; 212 count = ummax(count, rle->count); 213 end = ummax(rle->end, start + count - 1); 214 } 215 216 switch (type) { 217 case SYS_RES_IRQ: 218 rm = &sc->sc_intr_rman; 219 break; 220 case SYS_RES_MEMORY: 221 rm = &sc->sc_mem_rman; 222 break; 223 default: 224 return (NULL); 225 } 226 227 rv = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 228 child); 229 if (rv == NULL) 230 return (NULL); 231 rman_set_rid(rv, *rid); 232 233 if ((flags & RF_ACTIVE) != 0 && bus_activate_resource(child, type, 234 *rid, rv) != 0) { 235 rman_release_resource(rv); 236 return (NULL); 237 } 238 239 if (!passthrough && rle != NULL) { 240 rle->res = rv; 241 rle->start = rman_get_start(rv); 242 rle->end = rman_get_end(rv); 243 rle->count = rle->end - rle->start + 1; 244 } 245 246 return (rv); 247 } 248 249 static int 250 ofwbus_adjust_resource(device_t bus, device_t child __unused, int type, 251 struct resource *r, rman_res_t start, rman_res_t end) 252 { 253 struct ofwbus_softc *sc; 254 struct rman *rm; 255 device_t ofwbus; 256 257 ofwbus = bus; 258 while (strcmp(device_get_name(device_get_parent(ofwbus)), "root") != 0) 259 ofwbus = device_get_parent(ofwbus); 260 sc = device_get_softc(ofwbus); 261 switch (type) { 262 case SYS_RES_IRQ: 263 rm = &sc->sc_intr_rman; 264 break; 265 case SYS_RES_MEMORY: 266 rm = &sc->sc_mem_rman; 267 break; 268 default: 269 return (EINVAL); 270 } 271 if (rm == NULL) 272 return (ENXIO); 273 if (rman_is_region_manager(r, rm) == 0) 274 return (EINVAL); 275 return (rman_adjust_resource(r, start, end)); 276 } 277 278 static int 279 ofwbus_release_resource(device_t bus, device_t child, int type, 280 int rid, struct resource *r) 281 { 282 struct resource_list_entry *rle; 283 int passthrough; 284 int error; 285 286 passthrough = (device_get_parent(child) != bus); 287 if (!passthrough) { 288 /* Clean resource list entry */ 289 rle = resource_list_find(BUS_GET_RESOURCE_LIST(bus, child), 290 type, rid); 291 if (rle != NULL) 292 rle->res = NULL; 293 } 294 295 if ((rman_get_flags(r) & RF_ACTIVE) != 0) { 296 error = bus_deactivate_resource(child, type, rid, r); 297 if (error) 298 return (error); 299 } 300 return (rman_release_resource(r)); 301 } 302 303 #ifdef INTRNG 304 static void 305 ofwbus_destruct_map_data(struct intr_map_data *map_data) 306 { 307 struct intr_map_data_fdt *fdt_map_data; 308 309 KASSERT(map_data->type == INTR_MAP_DATA_FDT, 310 ("%s: bad map_data type %d", __func__, map_data->type)); 311 312 fdt_map_data = (struct intr_map_data_fdt *)map_data; 313 OF_prop_free(fdt_map_data->cells); 314 free(fdt_map_data, M_OFWPROP); 315 } 316 317 static int 318 ofwbus_map_intr(device_t bus, device_t child, int *rid, rman_res_t *start, 319 rman_res_t *end, rman_res_t *count, struct intr_map_data **imd) 320 { 321 phandle_t iparent, node; 322 pcell_t *cells; 323 int ncells, rv; 324 u_int irq; 325 struct intr_map_data_fdt *fdt_data; 326 327 node = ofw_bus_get_node(child); 328 rv = ofw_bus_intr_by_rid(child, node, *rid, &iparent, &ncells, &cells); 329 if (rv != 0) 330 return (rv); 331 332 fdt_data = malloc(sizeof(*fdt_data), M_OFWPROP, M_WAITOK | M_ZERO); 333 fdt_data->hdr.type = INTR_MAP_DATA_FDT; 334 fdt_data->hdr.destruct = ofwbus_destruct_map_data; 335 fdt_data->iparent = iparent; 336 fdt_data->ncells = ncells; 337 fdt_data->cells = cells; 338 rv = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data, 339 &irq); 340 if (rv != 0) { 341 ofwbus_destruct_map_data((struct intr_map_data *)fdt_data); 342 return (rv); 343 } 344 345 *start = irq; 346 *end = irq; 347 *count = 1; 348 *imd = (struct intr_map_data *)fdt_data; 349 return (0); 350 } 351 #endif 352