The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/mips/atheros/apb.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org>
    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 unmodified, this list of conditions, and the following
   10  *    disclaimer.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  *
   15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25  * SUCH DAMAGE.
   26  */
   27 
   28 #include <sys/cdefs.h>
   29 __FBSDID("$FreeBSD: releng/11.0/sys/mips/atheros/apb.c 299418 2016-05-11 09:42:24Z trasz $");
   30 
   31 #include <sys/param.h>
   32 #include <sys/systm.h>
   33 #include <sys/bus.h>
   34 #include <sys/interrupt.h>
   35 #include <sys/kernel.h>
   36 #include <sys/module.h>
   37 #include <sys/rman.h>
   38 #include <sys/malloc.h>
   39 #include <sys/pcpu.h>
   40 #include <sys/proc.h>
   41 #include <sys/pmc.h>
   42 #include <sys/pmckern.h>
   43 
   44 #include <machine/bus.h>
   45 #include <machine/intr_machdep.h>
   46 
   47 #include <mips/atheros/apbvar.h>
   48 #include <mips/atheros/ar71xxreg.h>
   49 #include <mips/atheros/ar71xx_setup.h>
   50 
   51 #define APB_INTR_PMC    5
   52 
   53 #undef APB_DEBUG
   54 #ifdef APB_DEBUG
   55 #define dprintf printf
   56 #else 
   57 #define dprintf(x, arg...)
   58 #endif  /* APB_DEBUG */
   59 
   60 #define DEVTOAPB(dev)   ((struct apb_ivar *) device_get_ivars(dev))
   61 
   62 static int      apb_activate_resource(device_t, device_t, int, int,
   63                     struct resource *);
   64 static device_t apb_add_child(device_t, u_int, const char *, int);
   65 static struct resource *
   66                 apb_alloc_resource(device_t, device_t, int, int *, rman_res_t,
   67                     rman_res_t, rman_res_t, u_int);
   68 static int      apb_attach(device_t);
   69 static int      apb_deactivate_resource(device_t, device_t, int, int,
   70                     struct resource *);
   71 static struct resource_list *
   72                 apb_get_resource_list(device_t, device_t);
   73 static void     apb_hinted_child(device_t, const char *, int);
   74 static int      apb_filter(void *);
   75 static int      apb_probe(device_t);
   76 static int      apb_release_resource(device_t, device_t, int, int,
   77                     struct resource *);
   78 static int      apb_setup_intr(device_t, device_t, struct resource *, int,
   79                     driver_filter_t *, driver_intr_t *, void *, void **);
   80 static int      apb_teardown_intr(device_t, device_t, struct resource *,
   81                     void *);
   82 
   83 static void 
   84 apb_mask_irq(void *source)
   85 {
   86         unsigned int irq = (unsigned int)source;
   87         uint32_t reg;
   88 
   89         reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
   90         ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq));
   91 
   92 }
   93 
   94 static void 
   95 apb_unmask_irq(void *source)
   96 {
   97         uint32_t reg;
   98         unsigned int irq = (unsigned int)source;
   99 
  100         reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK);
  101         ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq));
  102 }
  103 
  104 static int
  105 apb_probe(device_t dev)
  106 {
  107 
  108         return (BUS_PROBE_NOWILDCARD);
  109 }
  110 
  111 static int
  112 apb_attach(device_t dev)
  113 {
  114         struct apb_softc *sc = device_get_softc(dev);
  115         int rid = 0;
  116 
  117         device_set_desc(dev, "APB Bus bridge");
  118 
  119         sc->apb_mem_rman.rm_type = RMAN_ARRAY;
  120         sc->apb_mem_rman.rm_descr = "APB memory window";
  121 
  122         if (rman_init(&sc->apb_mem_rman) != 0 ||
  123             rman_manage_region(&sc->apb_mem_rman, 
  124                         AR71XX_APB_BASE, 
  125                         AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0)
  126                 panic("apb_attach: failed to set up memory rman");
  127 
  128         sc->apb_irq_rman.rm_type = RMAN_ARRAY;
  129         sc->apb_irq_rman.rm_descr = "APB IRQ";
  130 
  131         if (rman_init(&sc->apb_irq_rman) != 0 ||
  132             rman_manage_region(&sc->apb_irq_rman, 
  133                         APB_IRQ_BASE, APB_IRQ_END) != 0)
  134                 panic("apb_attach: failed to set up IRQ rman");
  135 
  136         if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 
  137             RF_SHAREABLE | RF_ACTIVE)) == NULL) {
  138                 device_printf(dev, "unable to allocate IRQ resource\n");
  139                 return (ENXIO);
  140         }
  141 
  142         if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC, 
  143             apb_filter, NULL, sc, &sc->sc_misc_ih))) {
  144                 device_printf(dev,
  145                     "WARNING: unable to register interrupt handler\n");
  146                 return (ENXIO);
  147         }
  148 
  149         bus_generic_probe(dev);
  150         bus_enumerate_hinted_children(dev);
  151         bus_generic_attach(dev);
  152 
  153         /*
  154          * Unmask performance counter IRQ
  155          */
  156         apb_unmask_irq((void*)APB_INTR_PMC);
  157         sc->sc_intr_counter[APB_INTR_PMC] = mips_intrcnt_create("apb irq5: pmc");
  158 
  159         return (0);
  160 }
  161 
  162 static struct resource *
  163 apb_alloc_resource(device_t bus, device_t child, int type, int *rid,
  164     rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
  165 {
  166         struct apb_softc                *sc = device_get_softc(bus);
  167         struct apb_ivar                 *ivar = device_get_ivars(child);
  168         struct resource                 *rv;
  169         struct resource_list_entry      *rle;
  170         struct rman                     *rm;
  171         int                              isdefault, needactivate, passthrough;
  172 
  173         isdefault = (RMAN_IS_DEFAULT_RANGE(start, end));
  174         needactivate = flags & RF_ACTIVE;
  175         /*
  176          * Pass memory requests to nexus device
  177          */
  178         passthrough = (device_get_parent(child) != bus);
  179         rle = NULL;
  180 
  181         dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %jd, %d)\n",
  182             __func__, bus, child, type, *rid, (void *)(intptr_t)start,
  183             (void *)(intptr_t)end, count, flags);
  184 
  185         if (passthrough)
  186                 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type,
  187                     rid, start, end, count, flags));
  188 
  189         /*
  190          * If this is an allocation of the "default" range for a given RID,
  191          * and we know what the resources for this device are (ie. they aren't
  192          * maintained by a child bus), then work out the start/end values.
  193          */
  194 
  195         if (isdefault) {
  196                 rle = resource_list_find(&ivar->resources, type, *rid);
  197                 if (rle == NULL) {
  198                         return (NULL);
  199                 }
  200 
  201                 if (rle->res != NULL) {
  202                         panic("%s: resource entry is busy", __func__);
  203                 }
  204                 start = rle->start;
  205                 end = rle->end;
  206                 count = rle->count;
  207 
  208                 dprintf("%s: default resource (%p, %p, %ld)\n",
  209                     __func__, (void *)(intptr_t)start,
  210                     (void *)(intptr_t)end, count);
  211         }
  212 
  213         switch (type) {
  214         case SYS_RES_IRQ:
  215                 rm = &sc->apb_irq_rman;
  216                 break;
  217         case SYS_RES_MEMORY:
  218                 rm = &sc->apb_mem_rman;
  219                 break;
  220         default:
  221                 printf("%s: unknown resource type %d\n", __func__, type);
  222                 return (0);
  223         }
  224 
  225         rv = rman_reserve_resource(rm, start, end, count, flags, child);
  226         if (rv == NULL) {
  227                 printf("%s: could not reserve resource\n", __func__);
  228                 return (0);
  229         }
  230 
  231         rman_set_rid(rv, *rid);
  232 
  233         if (needactivate) {
  234                 if (bus_activate_resource(child, type, *rid, rv)) {
  235                         printf("%s: could not activate resource\n", __func__);
  236                         rman_release_resource(rv);
  237                         return (0);
  238                 }
  239         }
  240 
  241         return (rv);
  242 }
  243 
  244 static int
  245 apb_activate_resource(device_t bus, device_t child, int type, int rid,
  246     struct resource *r)
  247 {
  248 
  249         /* XXX: should we mask/unmask IRQ here? */
  250         return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child,
  251                 type, rid, r));
  252 }
  253 
  254 static int
  255 apb_deactivate_resource(device_t bus, device_t child, int type, int rid,
  256     struct resource *r)
  257 {
  258 
  259         /* XXX: should we mask/unmask IRQ here? */
  260         return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child,
  261                 type, rid, r));
  262 }
  263 
  264 static int
  265 apb_release_resource(device_t dev, device_t child, int type,
  266     int rid, struct resource *r)
  267 {
  268         struct resource_list *rl;
  269         struct resource_list_entry *rle;
  270 
  271         rl = apb_get_resource_list(dev, child);
  272         if (rl == NULL)
  273                 return (EINVAL);
  274         rle = resource_list_find(rl, type, rid);
  275         if (rle == NULL)
  276                 return (EINVAL);
  277         rman_release_resource(r);
  278         rle->res = NULL;
  279 
  280         return (0);
  281 }
  282 
  283 static int
  284 apb_setup_intr(device_t bus, device_t child, struct resource *ires,
  285                 int flags, driver_filter_t *filt, driver_intr_t *handler,
  286                 void *arg, void **cookiep)
  287 {
  288         struct apb_softc *sc = device_get_softc(bus);
  289         struct intr_event *event;
  290         int irq, error;
  291 
  292         irq = rman_get_start(ires);
  293 
  294         if (irq > APB_IRQ_END)
  295                 panic("%s: bad irq %d", __func__, irq);
  296 
  297         event = sc->sc_eventstab[irq];
  298         if (event == NULL) {
  299                 error = intr_event_create(&event, (void *)irq, 0, irq, 
  300                     apb_mask_irq, apb_unmask_irq,
  301                     NULL, NULL,
  302                     "apb intr%d:", irq);
  303 
  304                 if (error == 0) {
  305                         sc->sc_eventstab[irq] = event;
  306                         sc->sc_intr_counter[irq] =
  307                             mips_intrcnt_create(event->ie_name);
  308                 }
  309                 else
  310                         return (error);
  311         }
  312 
  313         intr_event_add_handler(event, device_get_nameunit(child), filt,
  314             handler, arg, intr_priority(flags), flags, cookiep);
  315         mips_intrcnt_setname(sc->sc_intr_counter[irq], event->ie_fullname);
  316 
  317         apb_unmask_irq((void*)irq);
  318 
  319         return (0);
  320 }
  321 
  322 static int
  323 apb_teardown_intr(device_t dev, device_t child, struct resource *ires,
  324     void *cookie)
  325 {
  326         struct apb_softc *sc = device_get_softc(dev);
  327         int irq, result;
  328 
  329         irq = rman_get_start(ires);
  330         if (irq > APB_IRQ_END)
  331                 panic("%s: bad irq %d", __func__, irq);
  332 
  333         if (sc->sc_eventstab[irq] == NULL)
  334                 panic("Trying to teardown unoccupied IRQ");
  335 
  336         apb_mask_irq((void*)irq);
  337 
  338         result = intr_event_remove_handler(cookie);
  339         if (!result)
  340                 sc->sc_eventstab[irq] = NULL;
  341 
  342         return (result);
  343 }
  344 
  345 static int
  346 apb_filter(void *arg)
  347 {
  348         struct apb_softc *sc = arg;
  349         struct intr_event *event;
  350         uint32_t reg, irq;
  351         struct thread *td;
  352         struct trapframe *tf;
  353 
  354         reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS);
  355         for (irq = 0; irq < APB_NIRQS; irq++) {
  356                 if (reg & (1 << irq)) {
  357 
  358                         switch (ar71xx_soc) {
  359                         case AR71XX_SOC_AR7240:
  360                         case AR71XX_SOC_AR7241:
  361                         case AR71XX_SOC_AR7242:
  362                         case AR71XX_SOC_AR9330:
  363                         case AR71XX_SOC_AR9331:
  364                         case AR71XX_SOC_AR9341:
  365                         case AR71XX_SOC_AR9342:
  366                         case AR71XX_SOC_AR9344:
  367                         case AR71XX_SOC_QCA9533:
  368                         case AR71XX_SOC_QCA9533_V2:
  369                         case AR71XX_SOC_QCA9556:
  370                         case AR71XX_SOC_QCA9558:
  371                                 /* ACK/clear the given interrupt */
  372                                 ATH_WRITE_REG(AR71XX_MISC_INTR_STATUS,
  373                                     (1 << irq));
  374                                 break;
  375                         default:
  376                                 /* fallthrough */
  377                                 break;
  378                         }
  379 
  380                         event = sc->sc_eventstab[irq];
  381                         /* always count interrupts; spurious or otherwise */
  382                         mips_intrcnt_inc(sc->sc_intr_counter[irq]);
  383                         if (!event || TAILQ_EMPTY(&event->ie_handlers)) {
  384                                 if (irq == APB_INTR_PMC) {
  385                                         td = PCPU_GET(curthread);
  386                                         tf = td->td_intr_frame;
  387 
  388                                         if (pmc_intr)
  389                                                 (*pmc_intr)(PCPU_GET(cpuid), tf);
  390                                         continue;
  391                                 }
  392                                 /* Ignore timer interrupts */
  393                                 if (irq != 0 && irq != 8 && irq != 9 && irq != 10)
  394                                         printf("Stray APB IRQ %d\n", irq);
  395                                 continue;
  396                         }
  397 
  398                         intr_event_handle(event, PCPU_GET(curthread)->td_intr_frame);
  399                 }
  400         }
  401 
  402         return (FILTER_HANDLED);
  403 }
  404 
  405 static void
  406 apb_hinted_child(device_t bus, const char *dname, int dunit)
  407 {
  408         device_t                child;
  409         long                    maddr;
  410         int                     msize;
  411         int                     irq;
  412         int                     result;
  413         int                     mem_hints_count;
  414 
  415         child = BUS_ADD_CHILD(bus, 0, dname, dunit);
  416 
  417         /*
  418          * Set hard-wired resources for hinted child using
  419          * specific RIDs.
  420          */
  421         mem_hints_count = 0;
  422         if (resource_long_value(dname, dunit, "maddr", &maddr) == 0)
  423                 mem_hints_count++;
  424         if (resource_int_value(dname, dunit, "msize", &msize) == 0)
  425                 mem_hints_count++;
  426 
  427         /* check if all info for mem resource has been provided */
  428         if ((mem_hints_count > 0) && (mem_hints_count < 2)) {
  429                 printf("Either maddr or msize hint is missing for %s%d\n",
  430                     dname, dunit);
  431         } else if (mem_hints_count) {
  432                 result = bus_set_resource(child, SYS_RES_MEMORY, 0,
  433                     maddr, msize);
  434                 if (result != 0)
  435                         device_printf(bus, 
  436                             "warning: bus_set_resource() failed\n");
  437         }
  438 
  439         if (resource_int_value(dname, dunit, "irq", &irq) == 0) {
  440                 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1);
  441                 if (result != 0)
  442                         device_printf(bus,
  443                             "warning: bus_set_resource() failed\n");
  444         }
  445 }
  446 
  447 static device_t
  448 apb_add_child(device_t bus, u_int order, const char *name, int unit)
  449 {
  450         device_t                child;
  451         struct apb_ivar *ivar;
  452 
  453         ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO);
  454         resource_list_init(&ivar->resources);
  455 
  456         child = device_add_child_ordered(bus, order, name, unit);
  457         if (child == NULL) {
  458                 printf("Can't add child %s%d ordered\n", name, unit);
  459                 return (0);
  460         }
  461 
  462         device_set_ivars(child, ivar);
  463 
  464         return (child);
  465 }
  466 
  467 /*
  468  * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource
  469  * Provides pointer to resource_list for these routines
  470  */
  471 static struct resource_list *
  472 apb_get_resource_list(device_t dev, device_t child)
  473 {
  474         struct apb_ivar *ivar;
  475 
  476         ivar = device_get_ivars(child);
  477         return (&(ivar->resources));
  478 }
  479 
  480 static int
  481 apb_print_all_resources(device_t dev)
  482 {
  483         struct apb_ivar *ndev = DEVTOAPB(dev);
  484         struct resource_list *rl = &ndev->resources;
  485         int retval = 0;
  486 
  487         if (STAILQ_FIRST(rl))
  488                 retval += printf(" at");
  489 
  490         retval += resource_list_print_type(rl, "mem", SYS_RES_MEMORY, "%#jx");
  491         retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%jd");
  492 
  493         return (retval);
  494 }
  495 
  496 static int
  497 apb_print_child(device_t bus, device_t child)
  498 {
  499         int retval = 0;
  500 
  501         retval += bus_print_child_header(bus, child);
  502         retval += apb_print_all_resources(child);
  503         if (device_get_flags(child))
  504                 retval += printf(" flags %#x", device_get_flags(child));
  505         retval += printf(" on %s\n", device_get_nameunit(bus));
  506 
  507         return (retval);
  508 }
  509 
  510 
  511 static device_method_t apb_methods[] = {
  512         DEVMETHOD(bus_activate_resource,        apb_activate_resource),
  513         DEVMETHOD(bus_add_child,                apb_add_child),
  514         DEVMETHOD(bus_alloc_resource,           apb_alloc_resource),
  515         DEVMETHOD(bus_deactivate_resource,      apb_deactivate_resource),
  516         DEVMETHOD(bus_get_resource_list,        apb_get_resource_list),
  517         DEVMETHOD(bus_hinted_child,             apb_hinted_child),
  518         DEVMETHOD(bus_release_resource,         apb_release_resource),
  519         DEVMETHOD(bus_setup_intr,               apb_setup_intr),
  520         DEVMETHOD(bus_teardown_intr,            apb_teardown_intr),
  521         DEVMETHOD(device_attach,                apb_attach),
  522         DEVMETHOD(device_probe,                 apb_probe),
  523         DEVMETHOD(bus_get_resource,             bus_generic_rl_get_resource),
  524         DEVMETHOD(bus_set_resource,             bus_generic_rl_set_resource),
  525         DEVMETHOD(bus_print_child,              apb_print_child),
  526 
  527         DEVMETHOD_END
  528 };
  529 
  530 static driver_t apb_driver = {
  531         "apb",
  532         apb_methods,
  533         sizeof(struct apb_softc),
  534 };
  535 static devclass_t apb_devclass;
  536 
  537 DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0);

Cache object: 538fe657071850ac3308ef6ff2405270


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.