10aeed3e9SJustin Hibbits /*-
20aeed3e9SJustin Hibbits * Copyright (c) 2012 Semihalf.
30aeed3e9SJustin Hibbits * All rights reserved.
40aeed3e9SJustin Hibbits *
50aeed3e9SJustin Hibbits * Redistribution and use in source and binary forms, with or without
60aeed3e9SJustin Hibbits * modification, are permitted provided that the following conditions
70aeed3e9SJustin Hibbits * are met:
80aeed3e9SJustin Hibbits * 1. Redistributions of source code must retain the above copyright
90aeed3e9SJustin Hibbits * notice, this list of conditions and the following disclaimer.
100aeed3e9SJustin Hibbits * 2. Redistributions in binary form must reproduce the above copyright
110aeed3e9SJustin Hibbits * notice, this list of conditions and the following disclaimer in the
120aeed3e9SJustin Hibbits * documentation and/or other materials provided with the distribution.
130aeed3e9SJustin Hibbits *
140aeed3e9SJustin Hibbits * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
150aeed3e9SJustin Hibbits * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
160aeed3e9SJustin Hibbits * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
170aeed3e9SJustin Hibbits * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
180aeed3e9SJustin Hibbits * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
190aeed3e9SJustin Hibbits * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
200aeed3e9SJustin Hibbits * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
210aeed3e9SJustin Hibbits * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
220aeed3e9SJustin Hibbits * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
230aeed3e9SJustin Hibbits * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
240aeed3e9SJustin Hibbits * SUCH DAMAGE.
250aeed3e9SJustin Hibbits */
260aeed3e9SJustin Hibbits
270aeed3e9SJustin Hibbits #include "opt_platform.h"
28fdafd315SWarner Losh
290aeed3e9SJustin Hibbits #include <sys/param.h>
300aeed3e9SJustin Hibbits #include <sys/systm.h>
310aeed3e9SJustin Hibbits #include <sys/kernel.h>
320aeed3e9SJustin Hibbits #include <sys/bus.h>
330aeed3e9SJustin Hibbits #include <sys/lock.h>
340aeed3e9SJustin Hibbits #include <sys/module.h>
350aeed3e9SJustin Hibbits #include <sys/mutex.h>
360aeed3e9SJustin Hibbits #include <sys/proc.h>
370aeed3e9SJustin Hibbits #include <sys/pcpu.h>
380aeed3e9SJustin Hibbits #include <sys/sched.h>
390aeed3e9SJustin Hibbits
400aeed3e9SJustin Hibbits #include <machine/bus.h>
410aeed3e9SJustin Hibbits #include <machine/tlb.h>
420aeed3e9SJustin Hibbits
430aeed3e9SJustin Hibbits #include <dev/ofw/ofw_bus.h>
440aeed3e9SJustin Hibbits #include <dev/ofw/ofw_bus_subr.h>
450aeed3e9SJustin Hibbits
460aeed3e9SJustin Hibbits #include <powerpc/mpc85xx/mpc85xx.h>
470aeed3e9SJustin Hibbits
480aeed3e9SJustin Hibbits #include "bman.h"
490aeed3e9SJustin Hibbits #include "portals.h"
500aeed3e9SJustin Hibbits
510aeed3e9SJustin Hibbits t_Handle bman_portal_setup(struct bman_softc *);
520aeed3e9SJustin Hibbits
530aeed3e9SJustin Hibbits struct dpaa_portals_softc *bp_sc;
540aeed3e9SJustin Hibbits
550aeed3e9SJustin Hibbits int
bman_portals_attach(device_t dev)560aeed3e9SJustin Hibbits bman_portals_attach(device_t dev)
570aeed3e9SJustin Hibbits {
580aeed3e9SJustin Hibbits struct dpaa_portals_softc *sc;
590aeed3e9SJustin Hibbits
600aeed3e9SJustin Hibbits sc = bp_sc = device_get_softc(dev);
610aeed3e9SJustin Hibbits
620aeed3e9SJustin Hibbits /* Map bman portal to physical address space */
630aeed3e9SJustin Hibbits if (law_enable(OCP85XX_TGTIF_BMAN, sc->sc_dp_pa, sc->sc_dp_size)) {
640aeed3e9SJustin Hibbits bman_portals_detach(dev);
650aeed3e9SJustin Hibbits return (ENXIO);
660aeed3e9SJustin Hibbits }
670aeed3e9SJustin Hibbits /* Set portal properties for XX_VirtToPhys() */
680aeed3e9SJustin Hibbits XX_PortalSetInfo(dev);
690aeed3e9SJustin Hibbits
70*18250ec6SJohn Baldwin bus_attach_children(dev);
71*18250ec6SJohn Baldwin return (0);
720aeed3e9SJustin Hibbits }
730aeed3e9SJustin Hibbits
740aeed3e9SJustin Hibbits int
bman_portals_detach(device_t dev)750aeed3e9SJustin Hibbits bman_portals_detach(device_t dev)
760aeed3e9SJustin Hibbits {
770aeed3e9SJustin Hibbits struct dpaa_portals_softc *sc;
780aeed3e9SJustin Hibbits int i;
790aeed3e9SJustin Hibbits
800aeed3e9SJustin Hibbits bp_sc = NULL;
810aeed3e9SJustin Hibbits sc = device_get_softc(dev);
820aeed3e9SJustin Hibbits
830aeed3e9SJustin Hibbits for (i = 0; i < ARRAY_SIZE(sc->sc_dp); i++) {
840aeed3e9SJustin Hibbits if (sc->sc_dp[i].dp_ph != NULL) {
850aeed3e9SJustin Hibbits thread_lock(curthread);
860aeed3e9SJustin Hibbits sched_bind(curthread, i);
870aeed3e9SJustin Hibbits thread_unlock(curthread);
880aeed3e9SJustin Hibbits
890aeed3e9SJustin Hibbits BM_PORTAL_Free(sc->sc_dp[i].dp_ph);
900aeed3e9SJustin Hibbits
910aeed3e9SJustin Hibbits thread_lock(curthread);
920aeed3e9SJustin Hibbits sched_unbind(curthread);
930aeed3e9SJustin Hibbits thread_unlock(curthread);
940aeed3e9SJustin Hibbits }
950aeed3e9SJustin Hibbits
960aeed3e9SJustin Hibbits if (sc->sc_dp[i].dp_ires != NULL) {
97f77405e3SJustin Hibbits XX_DeallocIntr((uintptr_t)sc->sc_dp[i].dp_ires);
980aeed3e9SJustin Hibbits bus_release_resource(dev, SYS_RES_IRQ,
990aeed3e9SJustin Hibbits sc->sc_dp[i].dp_irid, sc->sc_dp[i].dp_ires);
1000aeed3e9SJustin Hibbits }
1010aeed3e9SJustin Hibbits }
1020aeed3e9SJustin Hibbits for (i = 0; i < ARRAY_SIZE(sc->sc_rres); i++) {
1030aeed3e9SJustin Hibbits if (sc->sc_rres[i] != NULL)
1040aeed3e9SJustin Hibbits bus_release_resource(dev, SYS_RES_MEMORY,
1050aeed3e9SJustin Hibbits sc->sc_rrid[i],
1060aeed3e9SJustin Hibbits sc->sc_rres[i]);
1070aeed3e9SJustin Hibbits }
1080aeed3e9SJustin Hibbits
1090aeed3e9SJustin Hibbits return (0);
1100aeed3e9SJustin Hibbits }
1110aeed3e9SJustin Hibbits
1120aeed3e9SJustin Hibbits t_Handle
bman_portal_setup(struct bman_softc * bsc)1130aeed3e9SJustin Hibbits bman_portal_setup(struct bman_softc *bsc)
1140aeed3e9SJustin Hibbits {
1150aeed3e9SJustin Hibbits struct dpaa_portals_softc *sc;
1160aeed3e9SJustin Hibbits t_BmPortalParam bpp;
1170aeed3e9SJustin Hibbits t_Handle portal;
118f77405e3SJustin Hibbits unsigned int cpu;
119f77405e3SJustin Hibbits uintptr_t p;
1200aeed3e9SJustin Hibbits
1210aeed3e9SJustin Hibbits /* Return NULL if we're not ready or while detach */
1220aeed3e9SJustin Hibbits if (bp_sc == NULL)
1230aeed3e9SJustin Hibbits return (NULL);
1240aeed3e9SJustin Hibbits
1250aeed3e9SJustin Hibbits sc = bp_sc;
1260aeed3e9SJustin Hibbits
1270aeed3e9SJustin Hibbits sched_pin();
1280aeed3e9SJustin Hibbits portal = NULL;
1290aeed3e9SJustin Hibbits cpu = PCPU_GET(cpuid);
1300aeed3e9SJustin Hibbits
1310aeed3e9SJustin Hibbits /* Check if portal is ready */
132f77405e3SJustin Hibbits while (atomic_cmpset_acq_ptr((uintptr_t *)&sc->sc_dp[cpu].dp_ph,
1330aeed3e9SJustin Hibbits 0, -1) == 0) {
134f77405e3SJustin Hibbits p = atomic_load_acq_ptr((uintptr_t *)&sc->sc_dp[cpu].dp_ph);
1350aeed3e9SJustin Hibbits
1360aeed3e9SJustin Hibbits /* Return if portal is already initialized */
1370aeed3e9SJustin Hibbits if (p != 0 && p != -1) {
1380aeed3e9SJustin Hibbits sched_unpin();
1390aeed3e9SJustin Hibbits return ((t_Handle)p);
1400aeed3e9SJustin Hibbits }
1410aeed3e9SJustin Hibbits
1420aeed3e9SJustin Hibbits /* Not inititialized and "owned" by another thread */
1431029dab6SMitchell Horne sched_relinquish(curthread);
1440aeed3e9SJustin Hibbits }
1450aeed3e9SJustin Hibbits
1460aeed3e9SJustin Hibbits /* Map portal registers */
1470aeed3e9SJustin Hibbits dpaa_portal_map_registers(sc);
1480aeed3e9SJustin Hibbits
1490aeed3e9SJustin Hibbits /* Configure and initialize portal */
1500aeed3e9SJustin Hibbits bpp.ceBaseAddress = rman_get_bushandle(sc->sc_rres[0]);
1510aeed3e9SJustin Hibbits bpp.ciBaseAddress = rman_get_bushandle(sc->sc_rres[1]);
1520aeed3e9SJustin Hibbits bpp.h_Bm = bsc->sc_bh;
1530aeed3e9SJustin Hibbits bpp.swPortalId = cpu;
154f77405e3SJustin Hibbits bpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires;
1550aeed3e9SJustin Hibbits
1560aeed3e9SJustin Hibbits portal = BM_PORTAL_Config(&bpp);
1570aeed3e9SJustin Hibbits if (portal == NULL)
1580aeed3e9SJustin Hibbits goto err;
1590aeed3e9SJustin Hibbits
1600aeed3e9SJustin Hibbits if (BM_PORTAL_Init(portal) != E_OK)
1610aeed3e9SJustin Hibbits goto err;
1620aeed3e9SJustin Hibbits
163f77405e3SJustin Hibbits atomic_store_rel_ptr((uintptr_t *)&sc->sc_dp[cpu].dp_ph, (uintptr_t)portal);
1640aeed3e9SJustin Hibbits
1650aeed3e9SJustin Hibbits sched_unpin();
1660aeed3e9SJustin Hibbits
1670aeed3e9SJustin Hibbits return (portal);
1680aeed3e9SJustin Hibbits
1690aeed3e9SJustin Hibbits err:
1700aeed3e9SJustin Hibbits if (portal != NULL)
1710aeed3e9SJustin Hibbits BM_PORTAL_Free(portal);
1720aeed3e9SJustin Hibbits
173f77405e3SJustin Hibbits atomic_store_rel_ptr((uintptr_t *)&sc->sc_dp[cpu].dp_ph, 0);
1740aeed3e9SJustin Hibbits sched_unpin();
1750aeed3e9SJustin Hibbits
1760aeed3e9SJustin Hibbits return (NULL);
1770aeed3e9SJustin Hibbits }
178