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