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

Cache object: 5dd00fb6bd39226308bc9d2b8c48b8c1


[ 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.