1 /*- 2 * Copyright 1998 Massachusetts Institute of Technology 3 * 4 * Permission to use, copy, modify, and distribute this software and 5 * its documentation for any purpose and without fee is hereby 6 * granted, provided that both the above copyright notice and this 7 * permission notice appear in all copies, that both the above 8 * copyright notice and this permission notice appear in all 9 * supporting documentation, and that the name of M.I.T. not be used 10 * in advertising or publicity pertaining to distribution of the 11 * software without specific, written prior permission. M.I.T. makes 12 * no representations about the suitability of this software for any 13 * purpose. It is provided "as is" without express or implied 14 * warranty. 15 * 16 * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17 * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20 * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 /* 32 * This code implements a `root nexus' for Arm Architecture 33 * machines. The function of the root nexus is to serve as an 34 * attachment point for both processors and buses, and to manage 35 * resources which are common to all of them. In particular, 36 * this code implements the core resource managers for interrupt 37 * requests and I/O memory address space. 38 */ 39 40 #include "opt_platform.h" 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/bus.h> 45 #include <sys/interrupt.h> 46 #include <sys/kernel.h> 47 #include <sys/malloc.h> 48 #include <sys/module.h> 49 #include <sys/rman.h> 50 51 #include <vm/vm.h> 52 #include <vm/pmap.h> 53 54 #include <machine/bus.h> 55 #include <machine/pcb.h> 56 #include <machine/intr.h> 57 #include <machine/resource.h> 58 #include <machine/vmparam.h> 59 60 #include <arm/arm/nexusvar.h> 61 62 #ifdef FDT 63 #include <machine/fdt.h> 64 #include <dev/ofw/ofw_bus_subr.h> 65 #include "ofw_bus_if.h" 66 #endif 67 68 static MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device"); 69 70 struct nexus_device { 71 struct resource_list nx_resources; 72 }; 73 74 #define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev)) 75 76 static struct rman mem_rman; 77 static struct rman irq_rman; 78 79 static device_probe_t nexus_probe; 80 static device_attach_t nexus_attach; 81 82 static bus_add_child_t nexus_add_child; 83 static bus_print_child_t nexus_print_child; 84 85 static bus_activate_resource_t nexus_activate_resource; 86 static bus_adjust_resource_t nexus_adjust_resource; 87 static bus_alloc_resource_t nexus_alloc_resource; 88 static bus_deactivate_resource_t nexus_deactivate_resource; 89 static bus_release_resource_t nexus_release_resource; 90 91 #ifdef SMP 92 static bus_bind_intr_t nexus_bind_intr; 93 #endif 94 static bus_config_intr_t nexus_config_intr; 95 static bus_describe_intr_t nexus_describe_intr; 96 static bus_setup_intr_t nexus_setup_intr; 97 static bus_teardown_intr_t nexus_teardown_intr; 98 99 static bus_get_bus_tag_t nexus_get_bus_tag; 100 static bus_get_dma_tag_t nexus_get_dma_tag; 101 102 #ifdef FDT 103 static ofw_bus_map_intr_t nexus_ofw_map_intr; 104 #endif 105 106 /* 107 * Normally NULL (which results in defaults which are handled in 108 * busdma_machdep), platform init code can use nexus_set_dma_tag() to set this 109 * to a tag that will be inherited by all busses and devices on the platform. 110 */ 111 static bus_dma_tag_t nexus_dma_tag; 112 113 static device_method_t nexus_methods[] = { 114 /* Device interface */ 115 DEVMETHOD(device_probe, nexus_probe), 116 DEVMETHOD(device_attach, nexus_attach), 117 118 /* Bus interface */ 119 DEVMETHOD(bus_add_child, nexus_add_child), 120 DEVMETHOD(bus_print_child, nexus_print_child), 121 DEVMETHOD(bus_activate_resource, nexus_activate_resource), 122 DEVMETHOD(bus_adjust_resource, nexus_adjust_resource), 123 DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 124 DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 125 DEVMETHOD(bus_release_resource, nexus_release_resource), 126 #ifdef SMP 127 DEVMETHOD(bus_bind_intr, nexus_bind_intr), 128 #endif 129 DEVMETHOD(bus_config_intr, nexus_config_intr), 130 DEVMETHOD(bus_describe_intr, nexus_describe_intr), 131 DEVMETHOD(bus_setup_intr, nexus_setup_intr), 132 DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 133 DEVMETHOD(bus_get_bus_tag, nexus_get_bus_tag), 134 DEVMETHOD(bus_get_dma_tag, nexus_get_dma_tag), 135 #ifdef FDT 136 /* ofw_bus interface */ 137 DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr), 138 #endif 139 DEVMETHOD_END 140 }; 141 142 static driver_t nexus_driver = { 143 "nexus", 144 nexus_methods, 145 1 /* no softc */ 146 }; 147 148 EARLY_DRIVER_MODULE(nexus, root, nexus_driver, 0, 0, 149 BUS_PASS_BUS + BUS_PASS_ORDER_EARLY); 150 151 static int 152 nexus_probe(device_t dev) 153 { 154 155 device_quiet(dev); /* suppress attach message for neatness */ 156 157 return (BUS_PROBE_DEFAULT); 158 } 159 160 static int 161 nexus_attach(device_t dev) 162 { 163 164 mem_rman.rm_start = 0; 165 mem_rman.rm_end = BUS_SPACE_MAXADDR; 166 mem_rman.rm_type = RMAN_ARRAY; 167 mem_rman.rm_descr = "I/O memory addresses"; 168 if (rman_init(&mem_rman) || 169 rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR)) 170 panic("nexus_probe mem_rman"); 171 irq_rman.rm_start = 0; 172 irq_rman.rm_end = ~0; 173 irq_rman.rm_type = RMAN_ARRAY; 174 irq_rman.rm_descr = "Interrupts"; 175 if (rman_init(&irq_rman) || rman_manage_region(&irq_rman, 0, ~0)) 176 panic("nexus_attach irq_rman"); 177 178 /* First, add ofwbus0. */ 179 device_add_child(dev, "ofwbus", 0); 180 181 /* 182 * Next, deal with the children we know about already. 183 */ 184 bus_generic_probe(dev); 185 bus_generic_attach(dev); 186 187 return (0); 188 } 189 190 static int 191 nexus_print_child(device_t bus, device_t child) 192 { 193 int retval = 0; 194 195 retval += bus_print_child_header(bus, child); 196 retval += printf("\n"); 197 198 return (retval); 199 } 200 201 static device_t 202 nexus_add_child(device_t bus, u_int order, const char *name, int unit) 203 { 204 device_t child; 205 struct nexus_device *ndev; 206 207 ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO); 208 if (!ndev) 209 return (0); 210 resource_list_init(&ndev->nx_resources); 211 212 child = device_add_child_ordered(bus, order, name, unit); 213 214 device_set_ivars(child, ndev); 215 216 return (child); 217 } 218 219 /* 220 * Allocate a resource on behalf of child. NB: child is usually going to be a 221 * child of one of our descendants, not a direct child of nexus0. 222 */ 223 static struct resource * 224 nexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 225 rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 226 { 227 struct resource *rv; 228 struct rman *rm; 229 int needactivate = flags & RF_ACTIVE; 230 231 flags &= ~RF_ACTIVE; 232 233 switch (type) { 234 case SYS_RES_IRQ: 235 rm = &irq_rman; 236 break; 237 238 case SYS_RES_MEMORY: 239 case SYS_RES_IOPORT: 240 rm = &mem_rman; 241 break; 242 243 default: 244 return (NULL); 245 } 246 247 rv = rman_reserve_resource(rm, start, end, count, flags, child); 248 if (rv == NULL) 249 return (NULL); 250 251 rman_set_rid(rv, *rid); 252 253 if (needactivate) { 254 if (bus_activate_resource(child, type, *rid, rv)) { 255 rman_release_resource(rv); 256 return (NULL); 257 } 258 } 259 260 return (rv); 261 } 262 263 static int 264 nexus_adjust_resource(device_t bus __unused, device_t child __unused, int type, 265 struct resource *r, rman_res_t start, rman_res_t end) 266 { 267 struct rman *rm; 268 269 switch (type) { 270 case SYS_RES_IRQ: 271 rm = &irq_rman; 272 break; 273 case SYS_RES_MEMORY: 274 rm = &mem_rman; 275 break; 276 default: 277 return (EINVAL); 278 } 279 if (rman_is_region_manager(r, rm) == 0) 280 return (EINVAL); 281 return (rman_adjust_resource(r, start, end)); 282 } 283 284 static int 285 nexus_release_resource(device_t bus, device_t child, int type, int rid, 286 struct resource *res) 287 { 288 int error; 289 290 if (rman_get_flags(res) & RF_ACTIVE) { 291 error = bus_deactivate_resource(child, type, rid, res); 292 if (error) 293 return (error); 294 } 295 return (rman_release_resource(res)); 296 } 297 298 static bus_space_tag_t 299 nexus_get_bus_tag(device_t bus __unused, device_t child __unused) 300 { 301 302 #ifdef FDT 303 return (fdtbus_bs_tag); 304 #else 305 return ((void *)1); 306 #endif 307 } 308 309 static bus_dma_tag_t 310 nexus_get_dma_tag(device_t dev, device_t child) 311 { 312 313 return (nexus_dma_tag); 314 } 315 316 void 317 nexus_set_dma_tag(bus_dma_tag_t tag) 318 { 319 320 nexus_dma_tag = tag; 321 } 322 323 static int 324 nexus_config_intr(device_t dev, int irq, enum intr_trigger trig, 325 enum intr_polarity pol) 326 { 327 int ret = ENODEV; 328 329 device_printf(dev, "bus_config_intr is obsolete and not supported!\n"); 330 ret = EOPNOTSUPP; 331 return (ret); 332 } 333 334 static int 335 nexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, 336 driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep) 337 { 338 339 if ((rman_get_flags(res) & RF_SHAREABLE) == 0) 340 flags |= INTR_EXCL; 341 342 return (intr_setup_irq(child, res, filt, intr, arg, flags, cookiep)); 343 } 344 345 static int 346 nexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih) 347 { 348 349 return (intr_teardown_irq(child, r, ih)); 350 } 351 352 static int 353 nexus_describe_intr(device_t dev, device_t child, struct resource *irq, 354 void *cookie, const char *descr) 355 { 356 357 return (intr_describe_irq(child, irq, cookie, descr)); 358 } 359 360 #ifdef SMP 361 static int 362 nexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu) 363 { 364 365 return (intr_bind_irq(child, irq, cpu)); 366 } 367 #endif 368 369 static int 370 nexus_activate_resource(device_t bus, device_t child, int type, int rid, 371 struct resource *r) 372 { 373 int err; 374 bus_addr_t paddr; 375 bus_size_t psize; 376 bus_space_handle_t vaddr; 377 378 if ((err = rman_activate_resource(r)) != 0) 379 return (err); 380 381 /* 382 * If this is a memory resource, map it into the kernel. 383 */ 384 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 385 paddr = (bus_addr_t)rman_get_start(r); 386 psize = (bus_size_t)rman_get_size(r); 387 #ifdef FDT 388 err = bus_space_map(fdtbus_bs_tag, paddr, psize, 0, &vaddr); 389 if (err != 0) { 390 rman_deactivate_resource(r); 391 return (err); 392 } 393 rman_set_bustag(r, fdtbus_bs_tag); 394 #else 395 vaddr = (bus_space_handle_t)pmap_mapdev((vm_paddr_t)paddr, 396 (vm_size_t)psize); 397 if (vaddr == 0) { 398 rman_deactivate_resource(r); 399 return (ENOMEM); 400 } 401 rman_set_bustag(r, (void *)1); 402 #endif 403 rman_set_virtual(r, (void *)vaddr); 404 rman_set_bushandle(r, vaddr); 405 return (0); 406 } else if (type == SYS_RES_IRQ) { 407 err = intr_activate_irq(child, r); 408 if (err != 0) { 409 rman_deactivate_resource(r); 410 return (err); 411 } 412 } 413 return (0); 414 } 415 416 static int 417 nexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 418 struct resource *r) 419 { 420 bus_size_t psize; 421 bus_space_handle_t vaddr; 422 423 if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 424 psize = (bus_size_t)rman_get_size(r); 425 vaddr = rman_get_bushandle(r); 426 427 if (vaddr != 0) { 428 #ifdef FDT 429 bus_space_unmap(fdtbus_bs_tag, vaddr, psize); 430 #else 431 pmap_unmapdev((void *)vaddr, (vm_size_t)psize); 432 #endif 433 rman_set_virtual(r, NULL); 434 rman_set_bushandle(r, 0); 435 } 436 } else if (type == SYS_RES_IRQ) { 437 intr_deactivate_irq(child, r); 438 } 439 440 return (rman_deactivate_resource(r)); 441 } 442 443 #ifdef FDT 444 static int 445 nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells, 446 pcell_t *intr) 447 { 448 u_int irq; 449 struct intr_map_data_fdt *fdt_data; 450 size_t len; 451 452 len = sizeof(*fdt_data) + icells * sizeof(pcell_t); 453 fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data( 454 INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO); 455 fdt_data->iparent = iparent; 456 fdt_data->ncells = icells; 457 memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t)); 458 irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data); 459 return (irq); 460 } 461 #endif /* FDT */ 462