xref: /freebsd-src/sys/dev/dpaa/qman_portals.c (revision 18250ec6c089c0c50cbd9fd87d78e03ff89916df)
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 "qman.h"
490aeed3e9SJustin Hibbits #include "portals.h"
500aeed3e9SJustin Hibbits 
510aeed3e9SJustin Hibbits extern e_RxStoreResponse qman_received_frame_callback(t_Handle, t_Handle,
520aeed3e9SJustin Hibbits     t_Handle, uint32_t, t_DpaaFD *);
530aeed3e9SJustin Hibbits extern e_RxStoreResponse qman_rejected_frame_callback(t_Handle, t_Handle,
540aeed3e9SJustin Hibbits     t_Handle, uint32_t, t_DpaaFD *, t_QmRejectedFrameInfo *);
550aeed3e9SJustin Hibbits 
560aeed3e9SJustin Hibbits t_Handle qman_portal_setup(struct qman_softc *);
570aeed3e9SJustin Hibbits 
580aeed3e9SJustin Hibbits struct dpaa_portals_softc *qp_sc;
590aeed3e9SJustin Hibbits 
600aeed3e9SJustin Hibbits int
610aeed3e9SJustin Hibbits qman_portals_attach(device_t dev)
620aeed3e9SJustin Hibbits {
630aeed3e9SJustin Hibbits 	struct dpaa_portals_softc *sc;
640aeed3e9SJustin Hibbits 
650aeed3e9SJustin Hibbits 	sc = qp_sc = device_get_softc(dev);
660aeed3e9SJustin Hibbits 
670aeed3e9SJustin Hibbits 	/* Map bman portal to physical address space */
680aeed3e9SJustin Hibbits 	if (law_enable(OCP85XX_TGTIF_QMAN, sc->sc_dp_pa, sc->sc_dp_size)) {
690aeed3e9SJustin Hibbits 		qman_portals_detach(dev);
700aeed3e9SJustin Hibbits 		return (ENXIO);
710aeed3e9SJustin Hibbits 	}
720aeed3e9SJustin Hibbits 	/* Set portal properties for XX_VirtToPhys() */
730aeed3e9SJustin Hibbits 	XX_PortalSetInfo(dev);
740aeed3e9SJustin Hibbits 
75*18250ec6SJohn Baldwin 	bus_attach_children(dev);
76*18250ec6SJohn Baldwin 	return (0);
770aeed3e9SJustin Hibbits }
780aeed3e9SJustin Hibbits 
790aeed3e9SJustin Hibbits int
800aeed3e9SJustin Hibbits qman_portals_detach(device_t dev)
810aeed3e9SJustin Hibbits {
820aeed3e9SJustin Hibbits 	struct dpaa_portals_softc *sc;
830aeed3e9SJustin Hibbits 	int i;
840aeed3e9SJustin Hibbits 
850aeed3e9SJustin Hibbits 	qp_sc = NULL;
860aeed3e9SJustin Hibbits 	sc = device_get_softc(dev);
870aeed3e9SJustin Hibbits 
880aeed3e9SJustin Hibbits 	for (i = 0; i < ARRAY_SIZE(sc->sc_dp); i++) {
890aeed3e9SJustin Hibbits 		if (sc->sc_dp[i].dp_ph != NULL) {
900aeed3e9SJustin Hibbits 			thread_lock(curthread);
910aeed3e9SJustin Hibbits 			sched_bind(curthread, i);
920aeed3e9SJustin Hibbits 			thread_unlock(curthread);
930aeed3e9SJustin Hibbits 
940aeed3e9SJustin Hibbits 			QM_PORTAL_Free(sc->sc_dp[i].dp_ph);
950aeed3e9SJustin Hibbits 
960aeed3e9SJustin Hibbits 			thread_lock(curthread);
970aeed3e9SJustin Hibbits 			sched_unbind(curthread);
980aeed3e9SJustin Hibbits 			thread_unlock(curthread);
990aeed3e9SJustin Hibbits 		}
1000aeed3e9SJustin Hibbits 
1010aeed3e9SJustin Hibbits 		if (sc->sc_dp[i].dp_ires != NULL) {
102f77405e3SJustin Hibbits 			XX_DeallocIntr((uintptr_t)sc->sc_dp[i].dp_ires);
1030aeed3e9SJustin Hibbits 			bus_release_resource(dev, SYS_RES_IRQ,
1040aeed3e9SJustin Hibbits 			    sc->sc_dp[i].dp_irid, sc->sc_dp[i].dp_ires);
1050aeed3e9SJustin Hibbits 		}
1060aeed3e9SJustin Hibbits 	}
1070aeed3e9SJustin Hibbits 	for (i = 0; i < ARRAY_SIZE(sc->sc_rres); i++) {
1080aeed3e9SJustin Hibbits 		if (sc->sc_rres[i] != NULL)
1090aeed3e9SJustin Hibbits 			bus_release_resource(dev, SYS_RES_MEMORY,
1100aeed3e9SJustin Hibbits 			    sc->sc_rrid[i],
1110aeed3e9SJustin Hibbits 			    sc->sc_rres[i]);
1120aeed3e9SJustin Hibbits 	}
1130aeed3e9SJustin Hibbits 
1140aeed3e9SJustin Hibbits 	return (0);
1150aeed3e9SJustin Hibbits }
1160aeed3e9SJustin Hibbits 
1170aeed3e9SJustin Hibbits t_Handle
1180aeed3e9SJustin Hibbits qman_portal_setup(struct qman_softc *qsc)
1190aeed3e9SJustin Hibbits {
1200aeed3e9SJustin Hibbits 	struct dpaa_portals_softc *sc;
1210aeed3e9SJustin Hibbits 	t_QmPortalParam qpp;
122f77405e3SJustin Hibbits 	unsigned int cpu;
123f77405e3SJustin Hibbits 	uintptr_t p;
1240aeed3e9SJustin Hibbits 	t_Handle portal;
1250aeed3e9SJustin Hibbits 
1260aeed3e9SJustin Hibbits 	/* Return NULL if we're not ready or while detach */
1270aeed3e9SJustin Hibbits 	if (qp_sc == NULL)
1280aeed3e9SJustin Hibbits 		return (NULL);
1290aeed3e9SJustin Hibbits 
1300aeed3e9SJustin Hibbits 	sc = qp_sc;
1310aeed3e9SJustin Hibbits 
1320aeed3e9SJustin Hibbits 	sched_pin();
1330aeed3e9SJustin Hibbits 	portal = NULL;
1340aeed3e9SJustin Hibbits 	cpu = PCPU_GET(cpuid);
1350aeed3e9SJustin Hibbits 
1360aeed3e9SJustin Hibbits 	/* Check if portal is ready */
137f77405e3SJustin Hibbits 	while (atomic_cmpset_acq_ptr((uintptr_t *)&sc->sc_dp[cpu].dp_ph,
1380aeed3e9SJustin Hibbits 	    0, -1) == 0) {
139f77405e3SJustin Hibbits 		p = atomic_load_acq_ptr((uintptr_t *)&sc->sc_dp[cpu].dp_ph);
1400aeed3e9SJustin Hibbits 
1410aeed3e9SJustin Hibbits 		/* Return if portal is already initialized */
1420aeed3e9SJustin Hibbits 		if (p != 0 && p != -1) {
1430aeed3e9SJustin Hibbits 			sched_unpin();
1440aeed3e9SJustin Hibbits 			return ((t_Handle)p);
1450aeed3e9SJustin Hibbits 		}
1460aeed3e9SJustin Hibbits 
1470aeed3e9SJustin Hibbits 		/* Not inititialized and "owned" by another thread */
1481029dab6SMitchell Horne 		sched_relinquish(curthread);
1490aeed3e9SJustin Hibbits 	}
1500aeed3e9SJustin Hibbits 
1510aeed3e9SJustin Hibbits 	/* Map portal registers */
1520aeed3e9SJustin Hibbits 	dpaa_portal_map_registers(sc);
1530aeed3e9SJustin Hibbits 
1540aeed3e9SJustin Hibbits 	/* Configure and initialize portal */
1550aeed3e9SJustin Hibbits 	qpp.ceBaseAddress = rman_get_bushandle(sc->sc_rres[0]);
1560aeed3e9SJustin Hibbits 	qpp.ciBaseAddress = rman_get_bushandle(sc->sc_rres[1]);
1570aeed3e9SJustin Hibbits 	qpp.h_Qm = qsc->sc_qh;
1580aeed3e9SJustin Hibbits 	qpp.swPortalId = cpu;
159f77405e3SJustin Hibbits 	qpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires;
1600aeed3e9SJustin Hibbits 	qpp.fdLiodnOffset = 0;
1610aeed3e9SJustin Hibbits 	qpp.f_DfltFrame = qman_received_frame_callback;
1620aeed3e9SJustin Hibbits 	qpp.f_RejectedFrame = qman_rejected_frame_callback;
1630aeed3e9SJustin Hibbits 	qpp.h_App = qsc;
1640aeed3e9SJustin Hibbits 
1650aeed3e9SJustin Hibbits 	portal = QM_PORTAL_Config(&qpp);
1660aeed3e9SJustin Hibbits 	if (portal == NULL)
1670aeed3e9SJustin Hibbits 		goto err;
1680aeed3e9SJustin Hibbits 
1690aeed3e9SJustin Hibbits 	if (QM_PORTAL_Init(portal) != E_OK)
1700aeed3e9SJustin Hibbits 		goto err;
1710aeed3e9SJustin Hibbits 
1720aeed3e9SJustin Hibbits 	if (QM_PORTAL_AddPoolChannel(portal, QMAN_COMMON_POOL_CHANNEL) != E_OK)
1730aeed3e9SJustin Hibbits 		goto err;
1740aeed3e9SJustin Hibbits 
175f77405e3SJustin Hibbits 	atomic_store_rel_ptr((uintptr_t *)&sc->sc_dp[cpu].dp_ph,
176f77405e3SJustin Hibbits 	    (uintptr_t)portal);
1770aeed3e9SJustin Hibbits 	sched_unpin();
1780aeed3e9SJustin Hibbits 
1790aeed3e9SJustin Hibbits 	return (portal);
1800aeed3e9SJustin Hibbits 
1810aeed3e9SJustin Hibbits err:
1820aeed3e9SJustin Hibbits 	if (portal != NULL)
1830aeed3e9SJustin Hibbits 		QM_PORTAL_Free(portal);
1840aeed3e9SJustin Hibbits 
1850aeed3e9SJustin Hibbits 	atomic_store_rel_32((uint32_t *)&sc->sc_dp[cpu].dp_ph, 0);
1860aeed3e9SJustin Hibbits 	sched_unpin();
1870aeed3e9SJustin Hibbits 
1880aeed3e9SJustin Hibbits 	return (NULL);
1890aeed3e9SJustin Hibbits }
190