1139735Simp/*- 2129198Scognet * Copyright 1998 Massachusetts Institute of Technology 3129198Scognet * 4129198Scognet * Permission to use, copy, modify, and distribute this software and 5129198Scognet * its documentation for any purpose and without fee is hereby 6129198Scognet * granted, provided that both the above copyright notice and this 7129198Scognet * permission notice appear in all copies, that both the above 8129198Scognet * copyright notice and this permission notice appear in all 9129198Scognet * supporting documentation, and that the name of M.I.T. not be used 10129198Scognet * in advertising or publicity pertaining to distribution of the 11129198Scognet * software without specific, written prior permission. M.I.T. makes 12129198Scognet * no representations about the suitability of this software for any 13129198Scognet * purpose. It is provided "as is" without express or implied 14129198Scognet * warranty. 15182934Sraj * 16129198Scognet * THIS SOFTWARE IS PROVIDED BY M.I.T. ``AS IS''. M.I.T. DISCLAIMS 17129198Scognet * ALL EXPRESS OR IMPLIED WARRANTIES WITH REGARD TO THIS SOFTWARE, 18129198Scognet * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19129198Scognet * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 20129198Scognet * SHALL M.I.T. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21129198Scognet * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22129198Scognet * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 23129198Scognet * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 24129198Scognet * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25129198Scognet * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 26129198Scognet * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27129198Scognet * SUCH DAMAGE. 28129198Scognet * 29129198Scognet */ 30129198Scognet 31129198Scognet/* 32129198Scognet * This code implements a `root nexus' for Arm Architecture 33129198Scognet * machines. The function of the root nexus is to serve as an 34129198Scognet * attachment point for both processors and buses, and to manage 35129198Scognet * resources which are common to all of them. In particular, 36129198Scognet * this code implements the core resource managers for interrupt 37129198Scognet * requests, DMA requests (which rightfully should be a part of the 38129198Scognet * ISA code but it's easier to do it here for now), I/O port addresses, 39129198Scognet * and I/O memory address space. 40129198Scognet */ 41129198Scognet 42276032Sandrew#include "opt_platform.h" 43276032Sandrew 44129198Scognet#include <sys/cdefs.h> 45129198Scognet__FBSDID("$FreeBSD: stable/11/sys/arm/arm/nexus.c 308382 2016-11-06 18:40:12Z gonzo $"); 46129198Scognet 47129198Scognet#include <sys/param.h> 48129198Scognet#include <sys/systm.h> 49129198Scognet#include <sys/bus.h> 50129198Scognet#include <sys/kernel.h> 51129198Scognet#include <sys/malloc.h> 52129198Scognet#include <sys/module.h> 53129198Scognet#include <machine/bus.h> 54129198Scognet#include <sys/rman.h> 55129198Scognet#include <sys/interrupt.h> 56129198Scognet 57129198Scognet#include <machine/vmparam.h> 58129198Scognet#include <machine/pcb.h> 59129198Scognet#include <vm/vm.h> 60129198Scognet#include <vm/pmap.h> 61129198Scognet 62129198Scognet#include <machine/resource.h> 63129198Scognet#include <machine/intr.h> 64129198Scognet 65257702Snwhitehorn#ifdef FDT 66257702Snwhitehorn#include <machine/fdt.h> 67308333Smmel#include <dev/ofw/ofw_bus_subr.h> 68260281Snwhitehorn#include "ofw_bus_if.h" 69261513Snwhitehorn#endif 70261513Snwhitehorn 71129198Scognetstatic MALLOC_DEFINE(M_NEXUSDEV, "nexusdev", "Nexus device"); 72129198Scognet 73129198Scognetstruct nexus_device { 74129198Scognet struct resource_list nx_resources; 75129198Scognet}; 76129198Scognet 77129198Scognet#define DEVTONX(dev) ((struct nexus_device *)device_get_ivars(dev)) 78129198Scognet 79129198Scognetstatic struct rman mem_rman; 80129198Scognet 81129198Scognetstatic int nexus_probe(device_t); 82129198Scognetstatic int nexus_attach(device_t); 83129198Scognetstatic int nexus_print_child(device_t, device_t); 84212413Savgstatic device_t nexus_add_child(device_t, u_int, const char *, int); 85129198Scognetstatic struct resource *nexus_alloc_resource(device_t, device_t, int, int *, 86294883Sjhibbits rman_res_t, rman_res_t, rman_res_t, u_int); 87129198Scognetstatic int nexus_activate_resource(device_t, device_t, int, int, 88182934Sraj struct resource *); 89295755Szbbstatic bus_space_tag_t nexus_get_bus_tag(device_t, device_t); 90298068Sandrew#ifdef INTRNG 91289529Sian#ifdef SMP 92289529Sianstatic int nexus_bind_intr(device_t, device_t, struct resource *, int); 93289529Sian#endif 94289529Sian#endif 95260161Szbbstatic int nexus_config_intr(device_t dev, int irq, enum intr_trigger trig, 96260161Szbb enum intr_polarity pol); 97298068Sandrew#ifdef INTRNG 98289529Sianstatic int nexus_describe_intr(device_t dev, device_t child, 99289529Sian struct resource *irq, void *cookie, const char *descr); 100289529Sian#endif 101257702Snwhitehornstatic int nexus_deactivate_resource(device_t, device_t, int, int, 102257702Snwhitehorn struct resource *); 103271484Sjhbstatic int nexus_release_resource(device_t, device_t, int, int, 104271484Sjhb struct resource *); 105257702Snwhitehorn 106182934Srajstatic int nexus_setup_intr(device_t dev, device_t child, struct resource *res, 107182934Sraj int flags, driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep); 108182934Srajstatic int nexus_teardown_intr(device_t, device_t, struct resource *, void *); 109147166Scognet 110260281Snwhitehorn#ifdef FDT 111260281Snwhitehornstatic int nexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, 112261351Snwhitehorn int icells, pcell_t *intr); 113260281Snwhitehorn#endif 114260281Snwhitehorn 115129198Scognetstatic device_method_t nexus_methods[] = { 116129198Scognet /* Device interface */ 117129198Scognet DEVMETHOD(device_probe, nexus_probe), 118129198Scognet DEVMETHOD(device_attach, nexus_attach), 119129198Scognet /* Bus interface */ 120129198Scognet DEVMETHOD(bus_print_child, nexus_print_child), 121129198Scognet DEVMETHOD(bus_add_child, nexus_add_child), 122129198Scognet DEVMETHOD(bus_alloc_resource, nexus_alloc_resource), 123129198Scognet DEVMETHOD(bus_activate_resource, nexus_activate_resource), 124260161Szbb DEVMETHOD(bus_config_intr, nexus_config_intr), 125257702Snwhitehorn DEVMETHOD(bus_deactivate_resource, nexus_deactivate_resource), 126271484Sjhb DEVMETHOD(bus_release_resource, nexus_release_resource), 127129198Scognet DEVMETHOD(bus_setup_intr, nexus_setup_intr), 128147166Scognet DEVMETHOD(bus_teardown_intr, nexus_teardown_intr), 129295755Szbb DEVMETHOD(bus_get_bus_tag, nexus_get_bus_tag), 130298068Sandrew#ifdef INTRNG 131289529Sian DEVMETHOD(bus_describe_intr, nexus_describe_intr), 132289529Sian#ifdef SMP 133289529Sian DEVMETHOD(bus_bind_intr, nexus_bind_intr), 134289529Sian#endif 135289529Sian#endif 136260281Snwhitehorn#ifdef FDT 137260281Snwhitehorn DEVMETHOD(ofw_bus_map_intr, nexus_ofw_map_intr), 138260281Snwhitehorn#endif 139129198Scognet { 0, 0 } 140129198Scognet}; 141129198Scognet 142257702Snwhitehornstatic devclass_t nexus_devclass; 143129198Scognetstatic driver_t nexus_driver = { 144129198Scognet "nexus", 145129198Scognet nexus_methods, 146129198Scognet 1 /* no softc */ 147129198Scognet}; 148283366SandrewEARLY_DRIVER_MODULE(nexus, root, nexus_driver, nexus_devclass, 0, 0, 149269606Sian BUS_PASS_BUS + BUS_PASS_ORDER_EARLY); 150129198Scognet 151129198Scognetstatic int 152129198Scognetnexus_probe(device_t dev) 153129198Scognet{ 154209129Sraj 155129198Scognet device_quiet(dev); /* suppress attach message for neatness */ 156182934Sraj 157209129Sraj return (BUS_PROBE_DEFAULT); 158129198Scognet} 159129198Scognet 160129198Scognetstatic int 161129198Scognetnexus_attach(device_t dev) 162129198Scognet{ 163182934Sraj 164209232Sraj mem_rman.rm_start = 0; 165296250Sjhibbits mem_rman.rm_end = BUS_SPACE_MAXADDR; 166209232Sraj mem_rman.rm_type = RMAN_ARRAY; 167209232Sraj mem_rman.rm_descr = "I/O memory addresses"; 168296250Sjhibbits if (rman_init(&mem_rman) || 169296250Sjhibbits rman_manage_region(&mem_rman, 0, BUS_SPACE_MAXADDR)) 170209232Sraj panic("nexus_probe mem_rman"); 171209232Sraj 172129198Scognet /* 173129198Scognet * First, deal with the children we know about already 174129198Scognet */ 175129198Scognet bus_generic_probe(dev); 176129198Scognet bus_generic_attach(dev); 177182934Sraj 178182934Sraj return (0); 179129198Scognet} 180129198Scognet 181129198Scognetstatic int 182129198Scognetnexus_print_child(device_t bus, device_t child) 183129198Scognet{ 184129198Scognet int retval = 0; 185182934Sraj 186129198Scognet retval += bus_print_child_header(bus, child); 187235907Sgber retval += printf("\n"); 188182934Sraj 189129198Scognet return (retval); 190129198Scognet} 191129198Scognet 192129198Scognetstatic device_t 193212413Savgnexus_add_child(device_t bus, u_int order, const char *name, int unit) 194129198Scognet{ 195182934Sraj device_t child; 196129198Scognet struct nexus_device *ndev; 197182934Sraj 198129198Scognet ndev = malloc(sizeof(struct nexus_device), M_NEXUSDEV, M_NOWAIT|M_ZERO); 199129198Scognet if (!ndev) 200182934Sraj return (0); 201129198Scognet resource_list_init(&ndev->nx_resources); 202129198Scognet 203129198Scognet child = device_add_child_ordered(bus, order, name, unit); 204182934Sraj 205129198Scognet /* should we free this in nexus_child_detached? */ 206129198Scognet device_set_ivars(child, ndev); 207182934Sraj 208182934Sraj return (child); 209129198Scognet} 210129198Scognet 211129198Scognet 212129198Scognet/* 213129198Scognet * Allocate a resource on behalf of child. NB: child is usually going to be a 214129198Scognet * child of one of our descendants, not a direct child of nexus0. 215129198Scognet * (Exceptions include footbridge.) 216129198Scognet */ 217129198Scognetstatic struct resource * 218129198Scognetnexus_alloc_resource(device_t bus, device_t child, int type, int *rid, 219294883Sjhibbits rman_res_t start, rman_res_t end, rman_res_t count, u_int flags) 220129198Scognet{ 221129198Scognet struct resource *rv; 222129198Scognet struct rman *rm; 223129198Scognet int needactivate = flags & RF_ACTIVE; 224129198Scognet 225271484Sjhb flags &= ~RF_ACTIVE; 226271484Sjhb 227129198Scognet switch (type) { 228129198Scognet case SYS_RES_MEMORY: 229257702Snwhitehorn case SYS_RES_IOPORT: 230129198Scognet rm = &mem_rman; 231129198Scognet break; 232182934Sraj 233129198Scognet default: 234271484Sjhb return (NULL); 235129198Scognet } 236129198Scognet 237129198Scognet rv = rman_reserve_resource(rm, start, end, count, flags, child); 238298055Spfg if (rv == NULL) 239271484Sjhb return (NULL); 240129198Scognet 241157891Simp rman_set_rid(rv, *rid); 242182934Sraj 243129198Scognet if (needactivate) { 244129198Scognet if (bus_activate_resource(child, type, *rid, rv)) { 245129198Scognet rman_release_resource(rv); 246182934Sraj return (0); 247129198Scognet } 248129198Scognet } 249182934Sraj 250182934Sraj return (rv); 251129198Scognet} 252129198Scognet 253257702Snwhitehornstatic int 254271484Sjhbnexus_release_resource(device_t bus, device_t child, int type, int rid, 255271484Sjhb struct resource *res) 256271484Sjhb{ 257271484Sjhb int error; 258283366Sandrew 259271484Sjhb if (rman_get_flags(res) & RF_ACTIVE) { 260271484Sjhb error = bus_deactivate_resource(child, type, rid, res); 261271484Sjhb if (error) 262271484Sjhb return (error); 263271484Sjhb } 264271484Sjhb return (rman_release_resource(res)); 265271484Sjhb} 266271484Sjhb 267295755Szbbstatic bus_space_tag_t 268295755Szbbnexus_get_bus_tag(device_t bus __unused, device_t child __unused) 269295755Szbb{ 270295755Szbb 271295755Szbb#ifdef FDT 272295755Szbb return(fdtbus_bs_tag); 273295755Szbb#else 274295755Szbb return((void *)1); 275295755Szbb#endif 276295755Szbb} 277295755Szbb 278271484Sjhbstatic int 279260161Szbbnexus_config_intr(device_t dev, int irq, enum intr_trigger trig, 280260161Szbb enum intr_polarity pol) 281260161Szbb{ 282260161Szbb int ret = ENODEV; 283260161Szbb 284298068Sandrew#ifdef INTRNG 285297539Sskra device_printf(dev, "bus_config_intr is obsolete and not supported!\n"); 286297539Sskra ret = EOPNOTSUPP; 287289529Sian#else 288260161Szbb if (arm_config_irq) 289260161Szbb ret = (*arm_config_irq)(irq, trig, pol); 290289529Sian#endif 291260161Szbb return (ret); 292260161Szbb} 293260161Szbb 294260161Szbbstatic int 295257702Snwhitehornnexus_setup_intr(device_t dev, device_t child, struct resource *res, int flags, 296257702Snwhitehorn driver_filter_t *filt, driver_intr_t *intr, void *arg, void **cookiep) 297257702Snwhitehorn{ 298298068Sandrew#ifndef INTRNG 299257702Snwhitehorn int irq; 300297539Sskra#endif 301129198Scognet 302257702Snwhitehorn if ((rman_get_flags(res) & RF_SHAREABLE) == 0) 303257702Snwhitehorn flags |= INTR_EXCL; 304257702Snwhitehorn 305298068Sandrew#ifdef INTRNG 306297539Sskra return(intr_setup_irq(child, res, filt, intr, arg, flags, cookiep)); 307289529Sian#else 308297539Sskra for (irq = rman_get_start(res); irq <= rman_get_end(res); irq++) { 309257702Snwhitehorn arm_setup_irqhandler(device_get_nameunit(child), 310257702Snwhitehorn filt, intr, arg, irq, flags, cookiep); 311257702Snwhitehorn arm_unmask_irq(irq); 312257702Snwhitehorn } 313257702Snwhitehorn return (0); 314297539Sskra#endif 315257702Snwhitehorn} 316257702Snwhitehorn 317129198Scognetstatic int 318257702Snwhitehornnexus_teardown_intr(device_t dev, device_t child, struct resource *r, void *ih) 319257702Snwhitehorn{ 320257702Snwhitehorn 321298068Sandrew#ifdef INTRNG 322297539Sskra return (intr_teardown_irq(child, r, ih)); 323289529Sian#else 324257702Snwhitehorn return (arm_remove_irqhandler(rman_get_start(r), ih)); 325289529Sian#endif 326257702Snwhitehorn} 327257702Snwhitehorn 328298068Sandrew#ifdef INTRNG 329289529Sianstatic int 330289529Siannexus_describe_intr(device_t dev, device_t child, struct resource *irq, 331289529Sian void *cookie, const char *descr) 332289529Sian{ 333257702Snwhitehorn 334297539Sskra return (intr_describe_irq(child, irq, cookie, descr)); 335289529Sian} 336289529Sian 337289529Sian#ifdef SMP 338257702Snwhitehornstatic int 339289529Siannexus_bind_intr(device_t dev, device_t child, struct resource *irq, int cpu) 340289529Sian{ 341289529Sian 342297539Sskra return (intr_bind_irq(child, irq, cpu)); 343289529Sian} 344289529Sian#endif 345289529Sian#endif 346289529Sian 347289529Sianstatic int 348129198Scognetnexus_activate_resource(device_t bus, device_t child, int type, int rid, 349182934Sraj struct resource *r) 350129198Scognet{ 351260283Sian int err; 352260283Sian bus_addr_t paddr; 353260283Sian bus_size_t psize; 354260283Sian bus_space_handle_t vaddr; 355260283Sian 356260283Sian if ((err = rman_activate_resource(r)) != 0) 357260283Sian return (err); 358260283Sian 359129198Scognet /* 360129198Scognet * If this is a memory resource, map it into the kernel. 361129198Scognet */ 362257702Snwhitehorn if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 363260283Sian paddr = (bus_addr_t)rman_get_start(r); 364260283Sian psize = (bus_size_t)rman_get_size(r); 365257702Snwhitehorn#ifdef FDT 366260283Sian err = bus_space_map(fdtbus_bs_tag, paddr, psize, 0, &vaddr); 367260283Sian if (err != 0) { 368260283Sian rman_deactivate_resource(r); 369260283Sian return (err); 370260283Sian } 371257702Snwhitehorn rman_set_bustag(r, fdtbus_bs_tag); 372257702Snwhitehorn#else 373260283Sian vaddr = (bus_space_handle_t)pmap_mapdev((vm_offset_t)paddr, 374260283Sian (vm_size_t)psize); 375260283Sian if (vaddr == 0) { 376260283Sian rman_deactivate_resource(r); 377260283Sian return (ENOMEM); 378260283Sian } 379257702Snwhitehorn rman_set_bustag(r, (void *)1); 380257702Snwhitehorn#endif 381260283Sian rman_set_virtual(r, (void *)vaddr); 382260283Sian rman_set_bushandle(r, vaddr); 383308333Smmel return (0); 384308333Smmel } else if (type == SYS_RES_IRQ) { 385308333Smmel#ifdef INTRNG 386308382Sgonzo err = intr_activate_irq(child, r); 387308382Sgonzo if (err != 0) { 388308382Sgonzo rman_deactivate_resource(r); 389308382Sgonzo return (err); 390308382Sgonzo } 391308333Smmel#endif 392129198Scognet } 393260283Sian return (0); 394129198Scognet} 395129198Scognet 396257702Snwhitehornstatic int 397257702Snwhitehornnexus_deactivate_resource(device_t bus, device_t child, int type, int rid, 398260283Sian struct resource *r) 399257702Snwhitehorn{ 400260283Sian bus_size_t psize; 401260283Sian bus_space_handle_t vaddr; 402257702Snwhitehorn 403308333Smmel if (type == SYS_RES_MEMORY || type == SYS_RES_IOPORT) { 404308333Smmel psize = (bus_size_t)rman_get_size(r); 405308333Smmel vaddr = rman_get_bushandle(r); 406260283Sian 407308333Smmel if (vaddr != 0) { 408260283Sian#ifdef FDT 409308333Smmel bus_space_unmap(fdtbus_bs_tag, vaddr, psize); 410260283Sian#else 411308333Smmel pmap_unmapdev((vm_offset_t)vaddr, (vm_size_t)psize); 412260283Sian#endif 413308333Smmel rman_set_virtual(r, NULL); 414308333Smmel rman_set_bushandle(r, 0); 415308333Smmel } 416308333Smmel } else if (type == SYS_RES_IRQ) { 417308333Smmel#ifdef INTRNG 418308333Smmel intr_deactivate_irq(child, r); 419308333Smmel#endif 420260283Sian } 421260283Sian 422260283Sian return (rman_deactivate_resource(r)); 423257702Snwhitehorn} 424257702Snwhitehorn 425260281Snwhitehorn#ifdef FDT 426260281Snwhitehornstatic int 427261351Snwhitehornnexus_ofw_map_intr(device_t dev, device_t child, phandle_t iparent, int icells, 428261351Snwhitehorn pcell_t *intr) 429260281Snwhitehorn{ 430308333Smmel#ifndef INTRNG 431308333Smmel return (intr_fdt_map_irq(iparent, intr, icells)); 432308333Smmel#else 433308333Smmel u_int irq; 434308333Smmel struct intr_map_data_fdt *fdt_data; 435308333Smmel size_t len; 436260281Snwhitehorn 437308333Smmel len = sizeof(*fdt_data) + icells * sizeof(pcell_t); 438308333Smmel fdt_data = (struct intr_map_data_fdt *)intr_alloc_map_data( 439308333Smmel INTR_MAP_DATA_FDT, len, M_WAITOK | M_ZERO); 440308333Smmel fdt_data->iparent = iparent; 441308333Smmel fdt_data->ncells = icells; 442308333Smmel memcpy(fdt_data->cells, intr, icells * sizeof(pcell_t)); 443308333Smmel irq = intr_map_irq(NULL, iparent, (struct intr_map_data *)fdt_data); 444308333Smmel return (irq); 445308333Smmel#endif /* INTRNG */ 446260281Snwhitehorn} 447308333Smmel#endif /* FDT */ 448308333Smmel 449