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"
28*fdafd315SWarner 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/proc.h>
340aeed3e9SJustin Hibbits #include <sys/pcpu.h>
350aeed3e9SJustin Hibbits #include <sys/rman.h>
360aeed3e9SJustin Hibbits #include <sys/sched.h>
370aeed3e9SJustin Hibbits
380aeed3e9SJustin Hibbits #include <vm/vm.h>
390aeed3e9SJustin Hibbits #include <vm/pmap.h>
400aeed3e9SJustin Hibbits
410aeed3e9SJustin Hibbits #include <machine/resource.h>
420aeed3e9SJustin Hibbits #include <machine/tlb.h>
430aeed3e9SJustin Hibbits
440aeed3e9SJustin Hibbits #include <contrib/ncsw/inc/error_ext.h>
450aeed3e9SJustin Hibbits #include <contrib/ncsw/inc/xx_ext.h>
460aeed3e9SJustin Hibbits
470aeed3e9SJustin Hibbits #include "portals.h"
480aeed3e9SJustin Hibbits
490aeed3e9SJustin Hibbits
500aeed3e9SJustin Hibbits int
dpaa_portal_alloc_res(device_t dev,struct dpaa_portals_devinfo * di,int cpu)510aeed3e9SJustin Hibbits dpaa_portal_alloc_res(device_t dev, struct dpaa_portals_devinfo *di, int cpu)
520aeed3e9SJustin Hibbits {
530aeed3e9SJustin Hibbits struct dpaa_portals_softc *sc = device_get_softc(dev);
540aeed3e9SJustin Hibbits struct resource_list_entry *rle;
550aeed3e9SJustin Hibbits int err;
560aeed3e9SJustin Hibbits struct resource_list *res;
570aeed3e9SJustin Hibbits
580aeed3e9SJustin Hibbits /* Check if MallocSmart allocator is ready */
590aeed3e9SJustin Hibbits if (XX_MallocSmartInit() != E_OK)
600aeed3e9SJustin Hibbits return (ENXIO);
610aeed3e9SJustin Hibbits
620aeed3e9SJustin Hibbits res = &di->di_res;
630aeed3e9SJustin Hibbits
640aeed3e9SJustin Hibbits /*
650aeed3e9SJustin Hibbits * Allocate memory.
660aeed3e9SJustin Hibbits * Reserve only one pair of CE/CI virtual memory regions
670aeed3e9SJustin Hibbits * for all CPUs, in order to save the space.
680aeed3e9SJustin Hibbits */
690aeed3e9SJustin Hibbits if (sc->sc_rres[0] == NULL) {
700aeed3e9SJustin Hibbits /* Cache enabled area */
710aeed3e9SJustin Hibbits rle = resource_list_find(res, SYS_RES_MEMORY, 0);
720aeed3e9SJustin Hibbits sc->sc_rrid[0] = 0;
730aeed3e9SJustin Hibbits sc->sc_rres[0] = bus_alloc_resource(dev,
740aeed3e9SJustin Hibbits SYS_RES_MEMORY, &sc->sc_rrid[0], rle->start + sc->sc_dp_pa,
750aeed3e9SJustin Hibbits rle->end + sc->sc_dp_pa, rle->count, RF_ACTIVE);
760aeed3e9SJustin Hibbits if (sc->sc_rres[0] == NULL) {
770e9f21dcSJustin Hibbits device_printf(dev,
780e9f21dcSJustin Hibbits "Could not allocate cache enabled memory.\n");
790aeed3e9SJustin Hibbits return (ENXIO);
800aeed3e9SJustin Hibbits }
810e9f21dcSJustin Hibbits tlb1_set_entry(rman_get_bushandle(sc->sc_rres[0]),
820e9f21dcSJustin Hibbits rle->start + sc->sc_dp_pa, rle->count, _TLB_ENTRY_MEM);
830aeed3e9SJustin Hibbits /* Cache inhibited area */
840aeed3e9SJustin Hibbits rle = resource_list_find(res, SYS_RES_MEMORY, 1);
850aeed3e9SJustin Hibbits sc->sc_rrid[1] = 1;
860aeed3e9SJustin Hibbits sc->sc_rres[1] = bus_alloc_resource(dev,
870aeed3e9SJustin Hibbits SYS_RES_MEMORY, &sc->sc_rrid[1], rle->start + sc->sc_dp_pa,
880aeed3e9SJustin Hibbits rle->end + sc->sc_dp_pa, rle->count, RF_ACTIVE);
890aeed3e9SJustin Hibbits if (sc->sc_rres[1] == NULL) {
900e9f21dcSJustin Hibbits device_printf(dev,
910e9f21dcSJustin Hibbits "Could not allocate cache inhibited memory.\n");
920aeed3e9SJustin Hibbits bus_release_resource(dev, SYS_RES_MEMORY,
930aeed3e9SJustin Hibbits sc->sc_rrid[0], sc->sc_rres[0]);
940aeed3e9SJustin Hibbits return (ENXIO);
950aeed3e9SJustin Hibbits }
960e9f21dcSJustin Hibbits tlb1_set_entry(rman_get_bushandle(sc->sc_rres[1]),
970e9f21dcSJustin Hibbits rle->start + sc->sc_dp_pa, rle->count, _TLB_ENTRY_IO);
980e9f21dcSJustin Hibbits sc->sc_dp[cpu].dp_regs_mapped = 1;
990aeed3e9SJustin Hibbits }
1000aeed3e9SJustin Hibbits /* Acquire portal's CE_PA and CI_PA */
1010aeed3e9SJustin Hibbits rle = resource_list_find(res, SYS_RES_MEMORY, 0);
1020aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ce_pa = rle->start + sc->sc_dp_pa;
1030aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ce_size = rle->count;
1040aeed3e9SJustin Hibbits rle = resource_list_find(res, SYS_RES_MEMORY, 1);
1050aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ci_pa = rle->start + sc->sc_dp_pa;
1060aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ci_size = rle->count;
1070aeed3e9SJustin Hibbits
1080aeed3e9SJustin Hibbits /* Allocate interrupts */
1090aeed3e9SJustin Hibbits rle = resource_list_find(res, SYS_RES_IRQ, 0);
1100aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_irid = 0;
1110aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ires = bus_alloc_resource(dev,
1120aeed3e9SJustin Hibbits SYS_RES_IRQ, &sc->sc_dp[cpu].dp_irid, rle->start, rle->end,
1130aeed3e9SJustin Hibbits rle->count, RF_ACTIVE);
1140aeed3e9SJustin Hibbits /* Save interrupt number for later use */
1150aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_intr_num = rle->start;
1160aeed3e9SJustin Hibbits
1170aeed3e9SJustin Hibbits if (sc->sc_dp[cpu].dp_ires == NULL) {
1180aeed3e9SJustin Hibbits device_printf(dev, "Could not allocate irq.\n");
1190aeed3e9SJustin Hibbits return (ENXIO);
1200aeed3e9SJustin Hibbits }
121b9931c07SBrandon Bergren err = XX_PreallocAndBindIntr(dev, (uintptr_t)sc->sc_dp[cpu].dp_ires, cpu);
1220aeed3e9SJustin Hibbits
1230aeed3e9SJustin Hibbits if (err != E_OK) {
1240aeed3e9SJustin Hibbits device_printf(dev, "Could not prealloc and bind interrupt\n");
1250aeed3e9SJustin Hibbits bus_release_resource(dev, SYS_RES_IRQ,
1260aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_irid, sc->sc_dp[cpu].dp_ires);
1270aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ires = NULL;
1280aeed3e9SJustin Hibbits return (ENXIO);
1290aeed3e9SJustin Hibbits }
1300aeed3e9SJustin Hibbits
1310aeed3e9SJustin Hibbits #if 0
1320aeed3e9SJustin Hibbits err = bus_generic_config_intr(dev, rle->start, di->di_intr_trig,
1330aeed3e9SJustin Hibbits di->di_intr_pol);
1340aeed3e9SJustin Hibbits if (err != 0) {
1350aeed3e9SJustin Hibbits device_printf(dev, "Could not configure interrupt\n");
1360aeed3e9SJustin Hibbits bus_release_resource(dev, SYS_RES_IRQ,
1370aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_irid, sc->sc_dp[cpu].dp_ires);
1380aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ires = NULL;
1390aeed3e9SJustin Hibbits return (err);
1400aeed3e9SJustin Hibbits }
1410aeed3e9SJustin Hibbits #endif
1420aeed3e9SJustin Hibbits
1430aeed3e9SJustin Hibbits return (0);
1440aeed3e9SJustin Hibbits }
1450aeed3e9SJustin Hibbits
1460aeed3e9SJustin Hibbits void
dpaa_portal_map_registers(struct dpaa_portals_softc * sc)1470aeed3e9SJustin Hibbits dpaa_portal_map_registers(struct dpaa_portals_softc *sc)
1480aeed3e9SJustin Hibbits {
1490aeed3e9SJustin Hibbits unsigned int cpu;
1500aeed3e9SJustin Hibbits
1510aeed3e9SJustin Hibbits sched_pin();
1520aeed3e9SJustin Hibbits cpu = PCPU_GET(cpuid);
1530aeed3e9SJustin Hibbits if (sc->sc_dp[cpu].dp_regs_mapped)
1540aeed3e9SJustin Hibbits goto out;
1550aeed3e9SJustin Hibbits
1560aeed3e9SJustin Hibbits tlb1_set_entry(rman_get_bushandle(sc->sc_rres[0]),
1570aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ce_pa, sc->sc_dp[cpu].dp_ce_size,
1580aeed3e9SJustin Hibbits _TLB_ENTRY_MEM);
1590aeed3e9SJustin Hibbits tlb1_set_entry(rman_get_bushandle(sc->sc_rres[1]),
1600aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_ci_pa, sc->sc_dp[cpu].dp_ci_size,
1610aeed3e9SJustin Hibbits _TLB_ENTRY_IO);
1620aeed3e9SJustin Hibbits
1630aeed3e9SJustin Hibbits sc->sc_dp[cpu].dp_regs_mapped = 1;
1640aeed3e9SJustin Hibbits
1650aeed3e9SJustin Hibbits out:
1660aeed3e9SJustin Hibbits sched_unpin();
1670aeed3e9SJustin Hibbits }
168