dpaa: BMan/QMan/FMan portal integration and bus drivers

Add the FreeBSD bus driver wrappers that bridge NCSW to the kernel
device tree. BMan and QMan portal drivers handle multi-CPU
initialization with explicit CPU binding (sched_bind unavailable
during attach). FMan driver configures the frame manager hardware,
reserved-memory regions (PFDR/FQD/FBPR via no-map DT nodes), and
MURAM allocation.

Portal common infrastructure provides shared init and cache-inhibited
mapping for both BMan and QMan portals.
This commit is contained in:
Tomaz Zaman 2026-03-04 21:16:51 +01:00 • committed by Maurice Walker
parent ba1e85edb1
commit 89ce353b40
16 changed files with 3392 additions and 62 deletions

View file

@ -89,3 +89,25 @@ contrib/ncsw/user/env/xx_arm64.c optional dpaa \
no-depend compile-with "${DPAA_COMPILE_CMD}"
contrib/ncsw/user/env/core_arm64.c optional dpaa \
no-depend compile-with "${DPAA_COMPILE_CMD}"
# FreeBSD Wrappers
dev/dpaa/portals_common.c optional dpaa \
no-depend compile-with "${DPAA_COMPILE_CMD}"
dev/dpaa/bman_portals.c optional dpaa fdt \
no-depend compile-with "${DPAA_COMPILE_CMD}"
dev/dpaa/bman.c optional dpaa \
no-depend compile-with "${DPAA_COMPILE_CMD}"
dev/dpaa/bman_fdt.c optional dpaa fdt \
no-depend compile-with "${DPAA_COMPILE_CMD}"
dev/dpaa/qman_portals.c optional dpaa fdt \
no-depend compile-with "${DPAA_COMPILE_CMD}"
dev/dpaa/qman.c optional dpaa \
no-depend compile-with "${DPAA_COMPILE_CMD}"
dev/dpaa/qman_fdt.c optional dpaa fdt \
no-depend compile-with "${DPAA_COMPILE_CMD}"
dev/dpaa/fman.c optional dpaa fdt \
no-depend compile-with "${DPAA_COMPILE_CMD}"
dev/dpaa/fman_chardev.c optional dpaa fdt \
no-depend compile-with "${FMCD_COMPILE_CMD}"
dev/dpaa/fman_fdt.c optional dpaa fdt \
no-depend compile-with "${DPAA_COMPILE_CMD}"

View file

@ -36,12 +36,25 @@
#include <sys/pcpu.h>
#include <sys/rman.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <sys/malloc.h>
#include <machine/resource.h>
#ifdef __powerpc__
#include <machine/tlb.h>
#endif
#ifdef __aarch64__
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif
#include "bman.h"
#define FBPR_ENTRY_SIZE 64 /* matches NCSW bm.h */
static struct bman_softc *bman_sc;
static t_Handle bman_pool_by_bpid[BM_MAX_NUM_OF_POOLS];
extern t_Handle bman_portal_setup(struct bman_softc *bsc);
@ -74,6 +87,7 @@ bman_exception(t_Handle h_App, e_BmExceptions exception)
device_printf(sc->sc_dev, "BMAN Exception: %s.\n", message);
}
int
bman_attach(device_t dev)
{
@ -114,12 +128,47 @@ bman_attach(device_t dev)
bp.partBpidBase = 0;
bp.partNumOfPools = BM_MAX_NUM_OF_POOLS;
#ifdef __aarch64__
/*
* Parse DT memory-region phandle for FBPR, matching Linux.
* DT: memory-region = <&bman_fbpr>;
* bman-fbpr: size=0x1000000 (16MB)
*/
{
extern void *qbman_alloc_reserved_mem(device_t, phandle_t,
uint32_t *, const char *);
phandle_t node, mem_node;
pcell_t mem_handle;
node = ofw_bus_get_node(dev);
if (node > 0 && OF_getencprop(node, "memory-region",
(void *)&mem_handle, sizeof(mem_handle)) ==
sizeof(mem_handle)) {
mem_node = OF_node_from_xref(mem_handle);
if (mem_node > 0) {
bp.p_FbprBase =
qbman_alloc_reserved_mem(dev,
mem_node, &bp.fbprSize, "FBPR");
if (bp.p_FbprBase != NULL)
bp.totalNumOfBuffers =
bp.fbprSize / FBPR_ENTRY_SIZE * 8;
}
} else {
device_printf(dev,
"no memory-region property, using defaults\n");
}
}
#endif
device_printf(dev, "CCSR va=%#lx totalBufs=%u\n",
(unsigned long)bp.baseAddress, bp.totalNumOfBuffers);
sc->sc_bh = BM_Config(&bp);
if (sc->sc_bh == NULL)
goto err;
/* Warn if there is less than 5% free FPBR's in pool */
error = BM_ConfigFbprThreshold(sc->sc_bh, (BMAN_MAX_BUFFERS / 8) / 20);
/* Warn if there is less than 5% free FBPR's in pool */
error = BM_ConfigFbprThreshold(sc->sc_bh, (bp.totalNumOfBuffers / 8) / 20);
if (error != E_OK)
goto err;
@ -134,6 +183,18 @@ bman_attach(device_t dev)
device_printf(dev, "Hardware version: %d.%d.\n",
rev.majorRev, rev.minorRev);
#ifdef __aarch64__
/*
* Initialize BMan portals for all CPUs from boot CPU.
* ARM64: pmap_mapdev creates global kernel VA, no sched_bind needed.
*/
for (int cpu = 0; cpu < mp_ncpus; cpu++) {
if (bman_portal_init_cpu(sc, cpu) == NULL)
device_printf(dev,
"could not setup BMan portal on CPU %d\n", cpu);
}
#endif
return (0);
err:
@ -255,6 +316,7 @@ bman_pool_create(uint8_t *bpid, uint16_t bufferSize, uint16_t maxBuffers,
*bpid = BM_POOL_GetId(pool);
sc->sc_bpool_cpu[*bpid] = PCPU_GET(cpuid);
bman_pool_by_bpid[*bpid] = pool;
sched_unpin();
@ -275,6 +337,7 @@ bman_pool_destroy(t_Handle pool)
struct bman_softc *sc;
sc = bman_sc;
bman_pool_by_bpid[BM_POOL_GetId(pool)] = NULL;
thread_lock(curthread);
sched_bind(curthread, sc->sc_bpool_cpu[BM_POOL_GetId(pool)]);
thread_unlock(curthread);
@ -363,3 +426,12 @@ bman_count(t_Handle pool)
return (BM_POOL_GetCounter(pool, e_BM_POOL_COUNTERS_CONTENT));
}
t_Handle
bman_pool_for_bpid(uint8_t bpid)
{
if (bpid >= BM_MAX_NUM_OF_POOLS)
return (NULL);
return (bman_pool_by_bpid[bpid]);
}

View file

@ -35,8 +35,8 @@
* BMAN Configuration
*/
/* Maximum number of buffers in all BMAN pools */
#define BMAN_MAX_BUFFERS 4096
/* Maximum number of buffers in all BMAN pools (FBPR ring entries) */
#define BMAN_MAX_BUFFERS 65536
/*
* Portal definitions
@ -189,6 +189,7 @@ int bman_put_buffer(t_Handle pool, void *buffer);
* @returns Number of free buffers in pool.
*/
uint32_t bman_count(t_Handle pool);
t_Handle bman_pool_for_bpid(uint8_t bpid);
/*
* Bus i/f
@ -199,4 +200,8 @@ int bman_suspend(device_t dev);
int bman_resume(device_t dev);
int bman_shutdown(device_t dev);
#ifdef __aarch64__
t_Handle bman_portal_init_cpu(struct bman_softc *, int);
#endif
#endif /* BMAN_H */

View file

@ -34,6 +34,8 @@
#include <sys/smp.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
@ -103,7 +105,10 @@ static driver_t bm_portals_driver = {
};
EARLY_DRIVER_MODULE(bman_portals, ofwbus, bm_portals_driver, 0, 0,
BUS_PASS_BUS);
BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
/* On ARM64, portal bus nodes are children of soc (simplebus), not ofwbus */
EARLY_DRIVER_MODULE(bman_portals, simplebus, bm_portals_driver, 0, 0,
BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
static void
get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep)
@ -147,7 +152,7 @@ bman_portal_find_cpu(int cpu)
return (node);
for (node = OF_child(node); node != 0; node = OF_peer(node)) {
if (OF_getprop(node, "reg", &reg, sizeof(reg)) <= 0)
if (OF_getencprop(node, "reg", &reg, sizeof(reg)) <= 0)
continue;
if (reg == cpu)
return (node);
@ -184,14 +189,16 @@ bman_portals_fdt_attach(device_t dev)
continue;
}
/* Checkout related cpu */
if (OF_getprop(child, "cpu-handle", (void *)&cpu,
if (OF_getencprop(child, "cpu-handle", (void *)&cpu,
sizeof(cpu)) <= 0) {
cpu = bman_portal_find_cpu(cpus);
if (cpu <= 0)
/* No cpu-handle (ARM64): find CPU node directly */
cpu_node = bman_portal_find_cpu(cpus);
if (cpu_node <= 0)
continue;
} else {
/* cpu-handle is an ihandle (PowerPC) */
cpu_node = OF_instance_to_package(cpu);
}
/* Acquire cpu number */
cpu_node = OF_instance_to_package(cpu);
if (OF_getencprop(cpu_node, "reg", &cpu_num, sizeof(cpu_num)) <= 0) {
device_printf(dev, "Could not retrieve CPU number.\n");
return (ENXIO);

View file

@ -38,13 +38,16 @@
#include <sys/sched.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#ifdef __powerpc__
#include <machine/tlb.h>
#include <powerpc/mpc85xx/mpc85xx.h>
#endif
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <powerpc/mpc85xx/mpc85xx.h>
#include "bman.h"
#include "portals.h"
@ -55,15 +58,16 @@ struct dpaa_portals_softc *bp_sc;
int
bman_portals_attach(device_t dev)
{
struct dpaa_portals_softc *sc;
bp_sc = device_get_softc(dev);
sc = bp_sc = device_get_softc(dev);
/* Map bman portal to physical address space */
#ifdef __powerpc__
struct dpaa_portals_softc *sc = bp_sc;
/* Map bman portal to physical address space (PowerPC LAW) */
if (law_enable(OCP85XX_TGTIF_BMAN, sc->sc_dp_pa, sc->sc_dp_size)) {
bman_portals_detach(dev);
return (ENXIO);
}
#endif
/* Set portal properties for XX_VirtToPhys() */
XX_PortalSetInfo(dev);
@ -146,8 +150,8 @@ bman_portal_setup(struct bman_softc *bsc)
dpaa_portal_map_registers(sc);
/* Configure and initialize portal */
bpp.ceBaseAddress = rman_get_bushandle(sc->sc_rres[0]);
bpp.ciBaseAddress = rman_get_bushandle(sc->sc_rres[1]);
bpp.ceBaseAddress = sc->sc_dp[cpu].dp_ce_va;
bpp.ciBaseAddress = sc->sc_dp[cpu].dp_ci_va;
bpp.h_Bm = bsc->sc_bh;
bpp.swPortalId = cpu;
bpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires;
@ -174,3 +178,54 @@ err:
return (NULL);
}
#ifdef __aarch64__
/*
* Initialize a BMan portal for a specific CPU from any CPU context.
* Unlike bman_portal_setup() which uses PCPU_GET(cpuid), this takes
* an explicit CPU parameter. On ARM64, pmap_mapdev creates global
* kernel VA, so no sched_bind/migration is needed.
*
* BMan has no SDEST configuration (confirmed from Linux reference).
*/
t_Handle
bman_portal_init_cpu(struct bman_softc *bsc, int cpu)
{
struct dpaa_portals_softc *sc;
t_BmPortalParam bpp;
t_Handle portal;
if (bp_sc == NULL)
return (NULL);
sc = bp_sc;
/* Skip if already initialized */
if (sc->sc_dp[cpu].dp_ph != NULL)
return (sc->sc_dp[cpu].dp_ph);
/* Map CE/CI registers (global kernel VA, accessible from any CPU) */
dpaa_portal_map_registers_cpu(sc, cpu);
if (sc->sc_dp[cpu].dp_ce_va == 0 || sc->sc_dp[cpu].dp_ci_va == 0)
return (NULL);
memset(&bpp, 0, sizeof(bpp));
bpp.ceBaseAddress = sc->sc_dp[cpu].dp_ce_va;
bpp.ciBaseAddress = sc->sc_dp[cpu].dp_ci_va;
bpp.h_Bm = bsc->sc_bh;
bpp.swPortalId = cpu;
bpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires;
portal = BM_PORTAL_Config(&bpp);
if (portal == NULL)
return (NULL);
if (BM_PORTAL_Init(portal) != E_OK) {
BM_PORTAL_Free(portal);
return (NULL);
}
sc->sc_dp[cpu].dp_ph = portal;
return (portal);
}
#endif

View file

@ -38,19 +38,33 @@
#include <dev/ofw/ofw_bus_subr.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include "opt_platform.h"
#include "opt_dpaa.h"
#include <contrib/ncsw/inc/Peripherals/fm_ext.h>
#include <contrib/ncsw/inc/Peripherals/fm_muram_ext.h>
#include <contrib/ncsw/inc/Peripherals/fm_pcd_ext.h>
#include <contrib/ncsw/inc/ncsw_ext.h>
#include <contrib/ncsw/inc/net_ext.h>
#include <contrib/ncsw/integrations/fman_ucode.h>
#include "fman.h"
#include "fman_chardev.h"
#include <dev/dpaa/qman.h>
/* NCSW internal headers — needed for hot-adding HC to existing PCD */
#include <contrib/ncsw/Peripherals/FM/Pcd/fm_pcd.h>
#include <contrib/ncsw/Peripherals/FM/inc/fm_hc.h>
static MALLOC_DEFINE(M_FMAN, "fman", "fman devices information");
/**
* @group FMan private defines.
* @{
@ -62,7 +76,11 @@ enum fman_irq_enum {
enum fman_mu_ram_map {
FMAN_MURAM_OFF = 0x0,
FMAN_MURAM_SIZE = 0x28000
#ifdef __aarch64__
FMAN_MURAM_SIZE = 0x60000 /* LS1046A FManv3: 384KB */
#else
FMAN_MURAM_SIZE = 0x28000 /* P5020 FManv2: 160KB */
#endif
};
struct fman_config {
@ -81,11 +99,13 @@ struct fman_config {
*/
/**
* Frame Manager firmware.
* We use the same firmware for both P3041 and P2041 devices.
* On ARM64 (LS1046A), U-Boot preloads FManv3 microcode into IRAM,
* so we skip embedding firmware. On PowerPC, use the P3041 image.
*/
#ifndef __aarch64__
const uint32_t fman_firmware[] = FMAN_UC_IMG;
const uint32_t fman_firmware_size = sizeof(fman_firmware);
#endif
int
fman_activate_resource(device_t bus, device_t child, int type, int rid,
struct resource *res)
@ -260,6 +280,110 @@ fman_fill_ranges(phandle_t node, struct simplebus_softc *sc)
return (sc->nranges);
}
static void
fman_pcd_exception_callback(t_Handle app_handle,
e_FmPcdExceptions exception)
{
struct fman_softc *sc;
sc = app_handle;
device_printf(sc->sc_base.dev, "PCD exception %d occurred.\n",
exception);
}
static void
fman_pcd_id_exception_callback(t_Handle app_handle,
e_FmPcdExceptions exception, uint16_t index)
{
struct fman_softc *sc;
sc = app_handle;
device_printf(sc->sc_base.dev,
"PCD indexed exception %d, index %u.\n", exception, index);
}
/*
* Host Command (HC) port parameters.
*
* The HC port uses OH port cell-index 0x2 (the first offline port),
* which is reserved in the Mono Gateway DTS with status="disabled"
* so dpaa_oh doesn't claim it. FMANv3 requires portId=0 for HC.
*/
#define FMAN_HC_OH_REG_OFFSET 0x82000 /* OH port 0x2 register offset */
#define FMAN_HC_PORT_ID 0 /* MUST be 0 for FMANv3 */
#define FMAN_HC_OH_CELL_INDEX 0x02 /* DT cell-index for QMan channel */
#define FMAN_HC_LIODN_BASE 0 /* ARM64 SMMU handles LIODN */
#define FMAN_HC_FQ_WQ 1 /* Work queue priority */
/*
* Byte-swap every uint32_t in a buffer.
*
* FMan is big-endian and reads HC frame data via DMA with no hardware
* swap (DMA_NO_SWAP). On PPC (big-endian), native uint32_t values
* are already in the correct byte order. On ARM64 (little-endian),
* every uint32_t must be byte-swapped so FMan reads correct values.
*
* The Linux NXP SDK does exactly this in QmEnqueueCB (before enqueue)
* and qm_tx_conf_dqrr_cb (after confirmation) in lnxwrp_fm_port.c.
*/
static __inline void
fman_hc_bswap_frame(void *buf, uint32_t len)
{
#if _BYTE_ORDER == _LITTLE_ENDIAN
uint32_t *p = (uint32_t *)buf;
uint32_t i;
for (i = 0; i < len / 4; i++)
p[i] = __builtin_bswap32(p[i]);
#endif
}
/*
* QMan enqueue callback for the HC port.
* Called by NCSW HC code (hc.c EnQFrm) to send command frames
* through QMan to the FMan HC offline port.
*/
static t_Error
fman_hc_qm_enqueue(t_Handle h_arg, void *p_fd)
{
struct fman_softc *sc;
t_DpaaFD *fd = (t_DpaaFD *)p_fd;
sc = h_arg;
/* Byte-swap HC frame data for FMan big-endian DMA */
fman_hc_bswap_frame(DPAA_FD_GET_ADDR(fd), DPAA_FD_GET_LENGTH(fd));
return (qman_fqr_enqueue(sc->hc_tx_fqr, 0, fd));
}
/*
* QMan receive callback for HC confirmation frames.
*
* FMan sends HC command completions on the confirmation FQ. QMan
* delivers them here; we forward to FM_PCD_HcTxConf() which clears
* the "enqueued" flag in hc.c, unblocking the spinning EnQFrm().
*
* The HC frame data was byte-swapped before enqueue (for FMan BE DMA).
* Swap it back to native byte order so FmHcTxConf can read fields
* like commandSequence in native format.
*/
static e_RxStoreResponse
fman_hc_conf_callback(t_Handle app, t_Handle qm_fqr, t_Handle qm_portal,
uint32_t fqid_offset, t_DpaaFD *frame)
{
struct fman_softc *sc;
sc = app;
/* Byte-swap HC frame data back to native byte order */
fman_hc_bswap_frame(DPAA_FD_GET_ADDR(frame),
DPAA_FD_GET_LENGTH(frame));
FM_PCD_HcTxConf(sc->pcd_handle, frame);
return (e_RX_STORE_RESPONSE_CONTINUE);
}
static t_Handle
fman_init(struct fman_softc *sc, struct fman_config *cfg)
{
@ -271,6 +395,8 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg)
uint16_t clock;
uint32_t tmp, mod;
memset(&fm_params, 0, sizeof(fm_params));
/* MURAM configuration */
muram_handle = FM_MURAM_ConfigAndInit(cfg->mem_base_addr +
FMAN_MURAM_OFF, FMAN_MURAM_SIZE);
@ -281,6 +407,12 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg)
}
sc->muram_handle = muram_handle;
/*
* MURAM stays as Device-nGnRnE (the default bus resource mapping),
* matching Linux devm_ioremap(). All MURAM accesses must use
* WRITE_UINT32/GET_UINT32/IOMemSet32 — never plain memset/memcpy.
*/
/* Fill in FM configuration */
fm_params.fmId = cfg->fm_id;
/* XXX we support only one partition thus each fman has master id */
@ -307,8 +439,22 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg)
fm_params.irq = cfg->irq_num;
fm_params.errIrq = cfg->err_irq_num;
#ifdef __aarch64__
/* U-Boot preloads FMan microcode into IRAM; skip firmware loading */
fm_params.firmware.size = 0;
fm_params.firmware.p_Code = NULL;
#else
fm_params.firmware.size = fman_firmware_size;
fm_params.firmware.p_Code = (uint32_t*)fman_firmware;
#endif
#if (DPAA_VERSION >= 11)
/* FManv3 Virtual Storage Profiles — master owns all 64 entries.
* VSP register block at FMan base + 0xdc000 (from fm_common.h FM_MM_SP). */
fm_params.partVSPBase = 0;
fm_params.partNumOfVSPs = FM_VSP_MAX_NUM_OF_ENTRIES;
fm_params.vspBaseAddr = cfg->mem_base_addr + 0xdc000;
#endif
fm_handle = FM_Config(&fm_params);
if (fm_handle == NULL) {
@ -317,7 +463,12 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg)
goto err;
}
#ifdef __aarch64__
/* Don't reset FMan — would wipe U-Boot's pre-loaded microcode */
FM_ConfigResetOnInit(fm_handle, FALSE);
#else
FM_ConfigResetOnInit(fm_handle, TRUE);
#endif
error = FM_Init(fm_handle);
if (error != E_OK) {
@ -334,6 +485,100 @@ fman_init(struct fman_softc *sc, struct fman_config *cfg)
device_printf(cfg->fman_device, "Hardware version: %d.%d.\n",
revision_info.majorRev, revision_info.minorRev);
{
t_FmCtrlCodeRevisionInfo ucode_rev;
if (FM_GetFmanCtrlCodeRevision(fm_handle, &ucode_rev) == E_OK)
device_printf(cfg->fman_device,
"Microcode: package %u, version %u.%u%s\n",
ucode_rev.packageRev,
ucode_rev.majorRev, ucode_rev.minorRev,
ucode_rev.packageRev >= 209 ? " (CDX/ASK)" :
ucode_rev.packageRev == 106 ? " (IP offload)" :
" (unknown)");
}
/*
* Initialize PCD (Parse-Classify-Distribute) with Parser +
* KeyGen + CC for RSS/multi-queue RX distribution and
* classification support.
*
* CC is enabled here so CDX can use it for hash tables.
* Host Command (HC) is NOT enabled here because QMan portals
* aren't ready during early FMan attach. CDX calls
* fman_reinit_pcd_with_hc() later to hot-add HC to this PCD
* without rebuilding it — preserving existing dtsec schemes.
*/
{
t_FmPcdParams pcd_params;
t_FmPcdNetEnvParams netenv_params;
sc->pcd_handle = NULL;
sc->netenv_handle = NULL;
sc->hc_tx_fqr = NULL;
sc->hc_err_fqr = NULL;
sc->hc_conf_fqr = NULL;
memset(&pcd_params, 0, sizeof(pcd_params));
pcd_params.h_Fm = fm_handle;
pcd_params.prsSupport = TRUE;
pcd_params.ccSupport = TRUE;
pcd_params.kgSupport = TRUE;
pcd_params.plcrSupport = TRUE;
pcd_params.numOfSchemes = FM_PCD_KG_NUM_OF_SCHEMES;
pcd_params.f_Exception = fman_pcd_exception_callback;
pcd_params.f_ExceptionId = fman_pcd_id_exception_callback;
pcd_params.h_App = sc;
sc->pcd_handle = FM_PCD_Config(&pcd_params);
if (sc->pcd_handle == NULL) {
device_printf(cfg->fman_device,
"couldn't configure PCD\n");
goto skip_pcd;
}
FM_PCD_ConfigPlcrNumOfSharedProfiles(sc->pcd_handle, 16);
error = FM_PCD_Init(sc->pcd_handle);
if (error != E_OK) {
device_printf(cfg->fman_device,
"couldn't init PCD: %d\n", error);
FM_PCD_Free(sc->pcd_handle);
sc->pcd_handle = NULL;
goto skip_pcd;
}
error = FM_PCD_Enable(sc->pcd_handle);
if (error != E_OK) {
device_printf(cfg->fman_device,
"couldn't enable PCD: %d\n", error);
FM_PCD_Free(sc->pcd_handle);
sc->pcd_handle = NULL;
goto skip_pcd;
}
/*
* Network environment: IPv4/IPv6 + TCP/UDP distinction.
* This tells PCD which protocol layers to recognize.
*/
memset(&netenv_params, 0, sizeof(netenv_params));
netenv_params.numOfDistinctionUnits = 2;
netenv_params.units[0].hdrs[0].hdr = HEADER_TYPE_IPv4;
netenv_params.units[0].hdrs[1].hdr = HEADER_TYPE_IPv6;
netenv_params.units[1].hdrs[0].hdr = HEADER_TYPE_TCP;
netenv_params.units[1].hdrs[1].hdr = HEADER_TYPE_UDP;
sc->netenv_handle = FM_PCD_NetEnvCharacteristicsSet(
sc->pcd_handle, &netenv_params);
if (sc->netenv_handle == NULL)
device_printf(cfg->fman_device,
"couldn't set NetEnv characteristics\n");
device_printf(cfg->fman_device,
"PCD initialized: Parser + KeyGen\n");
skip_pcd:
;
}
/* Initialize the simplebus part of things */
simplebus_init(sc->sc_base.dev, 0);
@ -352,6 +597,7 @@ err2:
FM_Free(fm_handle);
err:
FM_MURAM_Free(muram_handle);
sc->muram_handle = NULL;
return (NULL);
}
@ -361,7 +607,7 @@ fman_exception_callback(t_Handle app_handle, e_FmExceptions exception)
struct fman_softc *sc;
sc = app_handle;
device_printf(sc->sc_base.dev, "FMan exception occurred.\n");
device_printf(sc->sc_base.dev, "FMan exception %d\n", exception);
}
static void
@ -371,8 +617,11 @@ fman_error_callback(t_Handle app_handle, e_FmPortType port_type,
struct fman_softc *sc;
sc = app_handle;
device_printf(sc->sc_base.dev, "FMan error occurred.\n");
device_printf(sc->sc_base.dev,
"FMan error: portType=%d portId=%u addr=0x%llx tnum=%u liodn=%u\n",
port_type, port_id, (unsigned long long)addr, tnum, liodn);
}
/** @} */
@ -411,6 +660,34 @@ fman_get_bushandle(device_t dev, vm_offset_t *fm_base)
return (0);
}
int
fman_get_pcd_handle(device_t dev, t_Handle *pcdh)
{
struct fman_softc *sc = device_get_softc(dev);
*pcdh = sc->pcd_handle;
return (0);
}
int
fman_get_netenv_handle(device_t dev, t_Handle *netenvh)
{
struct fman_softc *sc = device_get_softc(dev);
*netenvh = sc->netenv_handle;
return (0);
}
struct fmcd_softc *
fman_get_fmcd(device_t dev)
{
struct fman_softc *sc = device_get_softc(dev);
return (sc->fmcd);
}
int
fman_attach(device_t dev)
{
@ -475,7 +752,20 @@ fman_attach(device_t dev)
goto err;
}
return (bus_generic_attach(dev));
/* Initialize FMD chardev interface for userspace PCD control */
if (sc->pcd_handle != NULL) {
sc->fmcd = malloc(sizeof(struct fmcd_softc), M_FMAN,
M_WAITOK | M_ZERO);
if (fmcd_init(dev, sc->fmcd) != 0) {
free(sc->fmcd, M_FMAN);
sc->fmcd = NULL;
device_printf(dev,
"fmcd: chardev init failed (non-fatal)\n");
}
}
bus_attach_children(dev);
return (0);
err:
fman_detach(dev);
@ -489,12 +779,29 @@ fman_detach(device_t dev)
sc = device_get_softc(dev);
if (sc->muram_handle) {
FM_MURAM_Free(sc->muram_handle);
if (sc->fmcd) {
fmcd_destroy(sc->fmcd);
free(sc->fmcd, M_FMAN);
sc->fmcd = NULL;
}
if (sc->netenv_handle) {
FM_PCD_NetEnvCharacteristicsDelete(sc->netenv_handle);
sc->netenv_handle = NULL;
}
if (sc->pcd_handle) {
FM_PCD_Disable(sc->pcd_handle);
FM_PCD_Free(sc->pcd_handle);
sc->pcd_handle = NULL;
}
if (sc->fm_handle) {
FM_Free(sc->fm_handle);
sc->fm_handle = NULL;
}
if (sc->muram_handle) {
FM_MURAM_Free(sc->muram_handle);
sc->muram_handle = NULL;
}
if (sc->mem_res) {
@ -507,7 +814,7 @@ fman_detach(device_t dev)
sc->irq_res);
}
if (sc->irq_res) {
if (sc->err_irq_res) {
bus_release_resource(dev, SYS_RES_IRQ, sc->err_irq_rid,
sc->err_irq_res);
}
@ -540,8 +847,15 @@ int
fman_qman_channel_id(device_t dev, int port)
{
struct fman_softc *sc;
#ifdef __aarch64__
/* FMan v3 / Major >= 6 (LS1046A): 2x 10G + 6x 1G + 6x OH ports.
* Order matches Linux fman_get_qman_channel_id() for Major >= 6. */
int qman_port_id[] = {0x30, 0x31, 0x28, 0x29, 0x2a, 0x2b,
0x2c, 0x2d, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
#else
int qman_port_id[] = {0x31, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e,
0x2f, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
#endif
int i;
sc = device_get_softc(dev);
@ -553,4 +867,152 @@ fman_qman_channel_id(device_t dev, int port)
return (0);
}
/*
* Hot-add Host Command port to existing PCD for advanced offload.
*
* Called by CDX module init (after QMan portals are ready) to add HC
* support to the existing PCD. Unlike the previous approach of
* destroying and rebuilding PCD (which invalidated dtsec schemes and
* caused FM_FD_ERR_NO_SCHEME), this preserves the PCD, all existing
* KeyGen schemes, and dtsec port bindings.
*
* Sequence:
* 1. Create QMan FQRs for HC TX/confirmation/error
* 2. Disable PCD (brief pause — schemes survive)
* 3. Create HC port via FmHcConfigAndInit
* 4. Set p_FmPcd->h_Hc to enable HC in the PCD
* 5. FM_PCD_SetAdvancedOffloadSupport (requires HC + PCD disabled)
* 6. Re-enable PCD
*
* Returns 0 on success, errno on failure.
*/
int
fman_reinit_pcd_with_hc(device_t dev)
{
struct fman_softc *sc;
t_FmPcd *p_FmPcd;
t_FmHcParams hc_params;
t_Handle hc_handle;
uint32_t hc_qman_chan;
t_Handle fqr;
t_Error error;
sc = device_get_softc(dev);
if (sc->pcd_handle == NULL) {
device_printf(dev, "PCD not initialized\n");
return (ENXIO);
}
/* Resolve QMan channel for HC offline port (cell-index 0x2) */
hc_qman_chan = fman_qman_channel_id(dev, FMAN_HC_OH_CELL_INDEX);
if (hc_qman_chan == 0) {
device_printf(dev,
"couldn't get QMan channel for HC port\n");
return (ENXIO);
}
/* TX FQR — HC enqueues command frames here for FMan */
fqr = qman_fqr_create(1, hc_qman_chan, FMAN_HC_FQ_WQ,
false, 0, false, false, true, false, 0, 0, 0);
if (fqr == NULL) {
device_printf(dev, "couldn't create HC TX FQR\n");
return (ENOMEM);
}
sc->hc_tx_fqr = fqr;
/* Confirmation FQR — FMan sends HC completions here */
fqr = qman_fqr_create(1, e_QM_FQ_CHANNEL_POOL1,
FMAN_HC_FQ_WQ, false, 0, false, false, true, false,
0, 0, 0);
if (fqr == NULL) {
device_printf(dev, "couldn't create HC confirmation FQR\n");
return (ENOMEM);
}
sc->hc_conf_fqr = fqr;
/*
* Register callback for HC confirmation frames.
* FMan sends command completions on confFqid; without this
* callback, they hit QMan's dummy handler and EnQFrm() in
* hc.c spins until timeout.
*/
error = qman_fqr_register_cb(sc->hc_conf_fqr,
fman_hc_conf_callback, sc);
if (error != E_OK) {
device_printf(dev,
"couldn't register HC confirmation callback: %d\n",
error);
return (ENXIO);
}
/* Error FQR — FMan sends HC errors here */
fqr = qman_fqr_create(1, e_QM_FQ_CHANNEL_POOL1,
FMAN_HC_FQ_WQ, false, 0, false, false, true, false,
0, 0, 0);
if (fqr == NULL) {
device_printf(dev, "couldn't create HC error FQR\n");
return (ENOMEM);
}
sc->hc_err_fqr = fqr;
/*
* Temporarily disable PCD to add HC and enable advanced
* offload. Existing KeyGen schemes and port bindings survive
* the Disable/Enable cycle — only the PRS/KG/PLCR hardware
* engines are paused.
*/
error = FM_PCD_Disable(sc->pcd_handle);
if (error != E_OK) {
device_printf(dev, "couldn't disable PCD: %d\n", error);
return (ENXIO);
}
/* Create HC port and attach to existing PCD */
memset(&hc_params, 0, sizeof(hc_params));
hc_params.h_Fm = sc->fm_handle;
hc_params.h_FmPcd = sc->pcd_handle;
hc_params.params.portBaseAddr = rman_get_bushandle(sc->mem_res) +
FMAN_HC_OH_REG_OFFSET;
hc_params.params.portId = FMAN_HC_PORT_ID;
hc_params.params.liodnBase = FMAN_HC_LIODN_BASE;
hc_params.params.errFqid =
qman_fqr_get_base_fqid(sc->hc_err_fqr);
hc_params.params.confFqid =
qman_fqr_get_base_fqid(sc->hc_conf_fqr);
hc_params.params.qmChannel = hc_qman_chan;
hc_params.params.f_QmEnqueue = fman_hc_qm_enqueue;
hc_params.params.h_QmArg = sc;
hc_handle = FmHcConfigAndInit(&hc_params);
if (hc_handle == NULL) {
device_printf(dev, "couldn't create HC port\n");
FM_PCD_Enable(sc->pcd_handle);
return (ENXIO);
}
/* Wire HC into the existing PCD */
p_FmPcd = (t_FmPcd *)sc->pcd_handle;
p_FmPcd->h_Hc = hc_handle;
/* Enable advanced offload (requires HC + PCD disabled) */
error = FM_PCD_SetAdvancedOffloadSupport(sc->pcd_handle);
if (error != E_OK) {
device_printf(dev,
"couldn't set advanced offload support: %d\n", error);
/* Non-fatal — HC still works, just no offload flag */
}
/* Re-enable PCD — resumes Parser/KeyGen/CC processing */
error = FM_PCD_Enable(sc->pcd_handle);
if (error != E_OK) {
device_printf(dev, "couldn't re-enable PCD: %d\n", error);
return (ENXIO);
}
device_printf(dev, "PCD upgraded: HC port added, "
"existing schemes preserved\n");
return (0);
}
/** @} */

View file

@ -27,8 +27,11 @@
#ifndef FMAN_H_
#define FMAN_H_
#include <sys/rman.h>
#include <dev/fdt/simplebus.h>
struct fmcd_softc; /* Forward declaration */
/**
* FMan driver instance data.
*/
@ -46,6 +49,16 @@ struct fman_softc {
t_Handle fm_handle;
t_Handle muram_handle;
t_Handle pcd_handle;
t_Handle netenv_handle;
/* Host Command port state (for advanced offload) */
t_Handle hc_tx_fqr; /* TX FQR for HC enqueue */
t_Handle hc_err_fqr; /* Error FQR for HC port */
t_Handle hc_conf_fqr; /* Confirmation FQR for HC port */
/* FMD chardev interface (for userspace PCD control via fmlib/FMC) */
struct fmcd_softc *fmcd;
};
@ -73,5 +86,9 @@ uint32_t fman_get_clock(struct fman_softc *sc);
int fman_get_handle(device_t dev, t_Handle *fmh);
int fman_get_muram_handle(device_t dev, t_Handle *muramh);
int fman_get_bushandle(device_t dev, vm_offset_t *fm_base);
int fman_get_pcd_handle(device_t dev, t_Handle *pcdh);
int fman_get_netenv_handle(device_t dev, t_Handle *netenvh);
int fman_reinit_pcd_with_hc(device_t dev);
struct fmcd_softc *fman_get_fmcd(device_t dev);
#endif /* FMAN_H_ */

1777
sys/dev/dpaa/fman_chardev.c Normal file

File diff suppressed because it is too large Load diff

155
sys/dev/dpaa/fman_chardev.h Normal file
View file

@ -0,0 +1,155 @@
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2026 Mono Technologies Inc.
* All rights reserved.
*
* FMan character device interface — exposes NCSW PCD API to userspace.
*
* Ioctl command numbers and data structures are ABI-compatible with the
* Linux NXP SDK FMD driver so that the same fmlib/FMC userspace tools
* work unchanged.
*
* Handle management: NCSW returns opaque kernel pointers (t_Handle).
* Userspace receives opaque integer handle IDs via a kernel handle
* table — kernel pointers are never exposed. The ioc structs have
* identical binary layouts to the NCSW structs (with a trailing
* void *id field); the kernel translates handle IDs to/from kernel
* pointers at the ioctl boundary.
*
* The ioctl struct definitions come from the fmlib ioctl headers
* (fm_pcd_ioctls.h, fm_port_ioctls.h) which are included directly
* by fman_chardev.c via the FMCD_COMPILE_CMD include paths.
* This header only exposes kernel-private definitions.
*/
#ifndef FMAN_CHARDEV_H_
#define FMAN_CHARDEV_H_
/*
* Device minor number layout.
* For FMan instance 0:
* minor 0 = /dev/fm0 (FM management)
* minor 1 = /dev/fm0-pcd (PCD operations)
* minor 2 = /dev/fm0-port-oh0 (OH port, cell-index 3)
* minor 3 = /dev/fm0-port-oh1 (OH port, cell-index 4)
* minor 8-15 = /dev/fm0-port-rx0..rx7 (RX ports, fmlib convention:
* 0-5 = 1G, 6-7 = 10G)
*/
#define FMD_MINOR_FM 0
#define FMD_MINOR_PCD 1
#define FMD_MINOR_OH_BASE 2
#define FMD_MINOR_RX_BASE 8 /* after 6 OH slots */
#define FMD_MINOR_MAX 16 /* 8 + 8 RX port slots */
/* ------------------------------------------------------------------ */
/* Kernel-only: chardev state and functions */
/* ------------------------------------------------------------------ */
#ifdef _KERNEL
#include <sys/conf.h>
#include <sys/sx.h>
#include <contrib/ncsw/inc/ncsw_ext.h>
/*
* PCD handle table — maps opaque integer IDs (exposed to userspace)
* to kernel pointers (used internally by NCSW).
*
* Handle ID encoding (uint64_t, same width as void* on arm64):
* bits [6:0] = slot index (0..FMCD_MAX_HANDLES-1)
* bits [31:7] = generation counter (invalidates stale handles)
* bits [63:32] = 0 (ensures ID never resembles a kernel pointer)
*
* Value 0 is reserved as "invalid/NULL handle". handle_gen starts
* at 1 so slot 0's first ID is (1 << 7) | 0 = 128, not 0.
*/
enum fmcd_handle_type {
FMCD_HDL_NONE = 0,
FMCD_HDL_NET_ENV,
FMCD_HDL_KG_SCHEME,
FMCD_HDL_CC_ROOT,
FMCD_HDL_MATCH_TABLE,
FMCD_HDL_HASH_TABLE,
FMCD_HDL_MANIP,
FMCD_HDL_PLCR_PROFILE,
FMCD_HDL_FRM_REPLIC,
};
struct fmcd_handle_entry {
t_Handle ptr; /* kernel pointer (NULL = free slot) */
enum fmcd_handle_type type;
};
#define FMCD_MAX_HANDLES 128
#define FMCD_HANDLE_MASK 0x7f /* slot index in lower 7 bits */
#define FMCD_HANDLE_SHIFT 7 /* generation counter above */
/*
* Per-FMan chardev state.
*/
struct fmcd_softc {
device_t fman_dev; /* parent fman device */
struct cdev *cdev_fm; /* /dev/fm0 */
struct cdev *cdev_pcd; /* /dev/fm0-pcd */
struct cdev *cdev_port_rx[8]; /* /dev/fm0-port-rx[0-7] */
struct cdev *cdev_port_oh[6]; /* /dev/fm0-port-oh[0-5] */
t_Handle fm_handle; /* NCSW FM handle */
t_Handle pcd_handle; /* NCSW PCD handle */
/* Port handle registry — indexed by fmlib convention:
* 0-5 = 1G MACs (cell-index 0-5), 6-7 = 10G MACs (cell-index 8-9).
* fmcd_cell_to_rxidx() in fman_chardev.c converts DT cell-index. */
struct sx port_lock;
t_Handle rx_port_handles[8];
t_Handle rx_port_schemes[8]; /* dtsec KG scheme handles */
t_Handle *rx_port_scheme_ptrs[8]; /* ptr to dtsec sc_scheme */
uint32_t rx_port_fqids[8]; /* dtsec default RX FQIDs */
t_Handle oh_port_handles[6];
/* PCD handle table — protected by port_lock */
struct fmcd_handle_entry handles[FMCD_MAX_HANDLES];
uint32_t handle_gen; /* generation counter */
bool pcd_transferred; /* CDX has taken PCD ownership */
};
/*
* Chardev lifecycle.
*/
int fmcd_init(device_t fman_dev, struct fmcd_softc *sc);
void fmcd_destroy(struct fmcd_softc *sc);
/*
* Port registration (called by dtsec and dpaa_oh during attach).
*/
void fmcd_register_rx_port(struct fmcd_softc *sc, int index,
t_Handle h_port, uint32_t dflt_fqid);
void fmcd_register_oh_port(struct fmcd_softc *sc, int index,
t_Handle h_port);
uint32_t fmcd_get_rx_dflt_fqid(struct fmcd_softc *sc, int index);
/*
* Register dtsec's KG scheme handle for PCD teardown.
* p_scheme points to dtsec_softc->sc_scheme so the chardev can
* clear it when userspace takes over PCD management.
*/
void fmcd_register_rx_port_scheme(struct fmcd_softc *sc, int index,
t_Handle h_scheme, t_Handle *p_scheme);
/*
* Mark PCD resources as transferred to CDX. After this call,
* fmcd_pcd_close() will not attempt to delete NCSW PCD objects.
*/
void fmcd_pcd_mark_transferred(struct fmcd_softc *sc);
/*
* Resolve a handle ID to a kernel pointer (read-only, does not free).
* Used by CDX to translate userspace handle IDs to NCSW pointers.
*/
t_Handle fmcd_handle_resolve(struct fmcd_softc *sc, uint64_t id,
enum fmcd_handle_type expected_type);
#endif /* _KERNEL */
#endif /* FMAN_CHARDEV_H_ */

View file

@ -31,6 +31,7 @@
#include <sys/bus.h>
#include <sys/module.h>
#include <dev/extres/clk/clk.h>
#include <dev/fdt/simplebus.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
@ -62,8 +63,11 @@ static device_method_t fman_methods[] = {
DEFINE_CLASS_1(fman, fman_driver, fman_methods,
sizeof(struct fman_softc), simplebus_driver);
/* Probe after GIC (BUS_PASS_INTERRUPT + BUS_PASS_ORDER_MIDDLE = 45)
* so interrupt allocation works on the first attempt.
* Also after clockgen (BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE) for clk_get_freq(). */
EARLY_DRIVER_MODULE(fman, simplebus, fman_driver, 0, 0,
BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
static int
@ -87,18 +91,29 @@ fman_get_clock(struct fman_softc *sc)
device_t dev;
phandle_t node;
pcell_t fman_clock;
clk_t clk;
uint64_t freq;
dev = sc->sc_base.dev;
node = ofw_bus_get_node(dev);
/* Try static clock-frequency property first (PowerPC style) */
if ((OF_getprop(node, "clock-frequency", &fman_clock,
sizeof(fman_clock)) <= 0) || (fman_clock == 0)) {
device_printf(dev, "could not acquire correct frequency "
"from DTS\n");
sizeof(fman_clock)) > 0) && (fman_clock != 0))
return ((uint32_t)fman_clock);
return (0);
/* Fall back to clock framework (ARM64 style) */
if (clk_get_by_ofw_index(dev, node, 0, &clk) == 0) {
if (clk_enable(clk) == 0 &&
clk_get_freq(clk, &freq) == 0 && freq != 0) {
device_printf(dev, "FMan clock: %ju Hz\n",
(uintmax_t)freq);
return ((uint32_t)freq);
}
clk_release(clk);
}
return ((uint32_t)fman_clock);
device_printf(dev, "could not acquire correct frequency from DTS\n");
return (0);
}

View file

@ -35,6 +35,8 @@ typedef struct dpaa_portal {
vm_paddr_t dp_ci_pa; /* portal's CI area PA */
uint32_t dp_ce_size; /* portal's CE area size */
uint32_t dp_ci_size; /* portal's CI area size */
vm_offset_t dp_ce_va; /* per-CPU CE virtual addr */
vm_offset_t dp_ci_va; /* per-CPU CI virtual addr */
uintptr_t dp_intr_num; /* portal's intr. number */
} dpaa_portal_t;
@ -60,3 +62,6 @@ int qman_portals_detach(device_t);
int dpaa_portal_alloc_res(device_t, struct dpaa_portals_devinfo *, int);
void dpaa_portal_map_registers(struct dpaa_portals_softc *);
#ifdef __aarch64__
void dpaa_portal_map_registers_cpu(struct dpaa_portals_softc *, int);
#endif

View file

@ -39,7 +39,12 @@
#include <vm/pmap.h>
#include <machine/resource.h>
#ifdef __powerpc__
#include <machine/tlb.h>
#endif
#ifdef __aarch64__
#include <machine/pmap.h>
#endif
#include <contrib/ncsw/inc/error_ext.h>
#include <contrib/ncsw/inc/xx_ext.h>
@ -70,6 +75,11 @@ dpaa_portal_alloc_res(device_t dev, struct dpaa_portals_devinfo *di, int cpu)
/* Cache enabled area */
rle = resource_list_find(res, SYS_RES_MEMORY, 0);
sc->sc_rrid[0] = 0;
device_printf(dev, "CE alloc: pa=%#lx-%#lx size=%#lx dp_pa=%#lx\n",
(unsigned long)(rle->start + sc->sc_dp_pa),
(unsigned long)(rle->end + sc->sc_dp_pa),
(unsigned long)rle->count,
(unsigned long)sc->sc_dp_pa);
sc->sc_rres[0] = bus_alloc_resource(dev,
SYS_RES_MEMORY, &sc->sc_rrid[0], rle->start + sc->sc_dp_pa,
rle->end + sc->sc_dp_pa, rle->count, RF_ACTIVE);
@ -78,11 +88,37 @@ dpaa_portal_alloc_res(device_t dev, struct dpaa_portals_devinfo *di, int cpu)
"Could not allocate cache enabled memory.\n");
return (ENXIO);
}
device_printf(dev, "CE mapped: va=%#lx\n",
(unsigned long)rman_get_bushandle(sc->sc_rres[0]));
#ifdef __powerpc__
tlb1_set_entry(rman_get_bushandle(sc->sc_rres[0]),
rle->start + sc->sc_dp_pa, rle->count, _TLB_ENTRY_MEM);
#endif
#ifdef __aarch64__
/*
* ARM64: The CE portal region must be Normal Non-Cacheable.
* Linux maps this as MEMREMAP_WC (pgprot_writecombine =
* Normal-NC) on ARM64. Stores go directly to the portal
* hardware without cache involvement. dc zva works on
* Normal-NC (zeros written as burst), dc cvac is a no-op.
*/
pmap_change_attr(
(vm_offset_t)rman_get_bushandle(sc->sc_rres[0]),
rle->count, VM_MEMATTR_UNCACHEABLE);
device_printf(dev, "CE pa_verify: va=%#lx -> pa=%#lx "
"(expected %#lx)\n",
(unsigned long)rman_get_bushandle(sc->sc_rres[0]),
(unsigned long)pmap_kextract(
(vm_offset_t)rman_get_bushandle(sc->sc_rres[0])),
(unsigned long)(rle->start + sc->sc_dp_pa));
#endif
/* Cache inhibited area */
rle = resource_list_find(res, SYS_RES_MEMORY, 1);
sc->sc_rrid[1] = 1;
device_printf(dev, "CI alloc: pa=%#lx-%#lx size=%#lx\n",
(unsigned long)(rle->start + sc->sc_dp_pa),
(unsigned long)(rle->end + sc->sc_dp_pa),
(unsigned long)rle->count);
sc->sc_rres[1] = bus_alloc_resource(dev,
SYS_RES_MEMORY, &sc->sc_rrid[1], rle->start + sc->sc_dp_pa,
rle->end + sc->sc_dp_pa, rle->count, RF_ACTIVE);
@ -93,8 +129,16 @@ dpaa_portal_alloc_res(device_t dev, struct dpaa_portals_devinfo *di, int cpu)
sc->sc_rrid[0], sc->sc_rres[0]);
return (ENXIO);
}
device_printf(dev, "CI mapped: va=%#lx\n",
(unsigned long)rman_get_bushandle(sc->sc_rres[1]));
#ifdef __powerpc__
tlb1_set_entry(rman_get_bushandle(sc->sc_rres[1]),
rle->start + sc->sc_dp_pa, rle->count, _TLB_ENTRY_IO);
#endif
sc->sc_dp[cpu].dp_ce_va =
rman_get_bushandle(sc->sc_rres[0]);
sc->sc_dp[cpu].dp_ci_va =
rman_get_bushandle(sc->sc_rres[1]);
sc->sc_dp[cpu].dp_regs_mapped = 1;
}
/* Acquire portal's CE_PA and CI_PA */
@ -153,15 +197,64 @@ dpaa_portal_map_registers(struct dpaa_portals_softc *sc)
if (sc->sc_dp[cpu].dp_regs_mapped)
goto out;
#ifdef __powerpc__
/* PowerPC: remap the shared VA to this CPU's portal PA via TLB */
tlb1_set_entry(rman_get_bushandle(sc->sc_rres[0]),
sc->sc_dp[cpu].dp_ce_pa, sc->sc_dp[cpu].dp_ce_size,
_TLB_ENTRY_MEM);
tlb1_set_entry(rman_get_bushandle(sc->sc_rres[1]),
sc->sc_dp[cpu].dp_ci_pa, sc->sc_dp[cpu].dp_ci_size,
_TLB_ENTRY_IO);
sc->sc_dp[cpu].dp_ce_va = rman_get_bushandle(sc->sc_rres[0]);
sc->sc_dp[cpu].dp_ci_va = rman_get_bushandle(sc->sc_rres[1]);
#endif
#ifdef __aarch64__
/*
* ARM64: Each CPU needs its own VA→PA mapping because we can't
* remap a shared VA per-CPU like PowerPC does with TLB1 entries.
* Map each portal's CE as Normal Non-Cacheable, CI as Device.
*/
if (sc->sc_dp[cpu].dp_ce_pa == 0) {
printf("dpaa_portal_map_registers: cpu %u has no portal allocated\n", cpu);
goto out;
}
sc->sc_dp[cpu].dp_ce_va = (vm_offset_t)pmap_mapdev_attr(
sc->sc_dp[cpu].dp_ce_pa, sc->sc_dp[cpu].dp_ce_size,
VM_MEMATTR_UNCACHEABLE);
sc->sc_dp[cpu].dp_ci_va = (vm_offset_t)pmap_mapdev(
sc->sc_dp[cpu].dp_ci_pa, sc->sc_dp[cpu].dp_ci_size);
#endif
sc->sc_dp[cpu].dp_regs_mapped = 1;
out:
sched_unpin();
}
#ifdef __aarch64__
/*
* Map portal CE/CI registers for a specific CPU.
* Unlike dpaa_portal_map_registers() which uses PCPU_GET(cpuid),
* this takes an explicit CPU parameter so it can be called from
* any CPU context during early boot (before smp_started=1).
* pmap_mapdev_attr/pmap_mapdev create global kernel VA mappings.
*/
void
dpaa_portal_map_registers_cpu(struct dpaa_portals_softc *sc, int cpu)
{
if (sc->sc_dp[cpu].dp_regs_mapped)
return;
if (sc->sc_dp[cpu].dp_ce_pa == 0) {
printf("dpaa_portal_map_registers_cpu: "
"cpu %d has no portal allocated\n", cpu);
return;
}
sc->sc_dp[cpu].dp_ce_va = (vm_offset_t)pmap_mapdev_attr(
sc->sc_dp[cpu].dp_ce_pa, sc->sc_dp[cpu].dp_ce_size,
VM_MEMATTR_UNCACHEABLE);
sc->sc_dp[cpu].dp_ci_va = (vm_offset_t)pmap_mapdev(
sc->sc_dp[cpu].dp_ci_pa, sc->sc_dp[cpu].dp_ci_size);
sc->sc_dp[cpu].dp_regs_mapped = 1;
}
#endif

View file

@ -25,6 +25,8 @@
*/
#include <sys/cdefs.h>
#include "opt_dpaa.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
@ -37,16 +39,33 @@
#include <sys/rman.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <sys/malloc.h>
#include <machine/bus.h>
#include <machine/resource.h>
#ifdef __powerpc__
#include <machine/tlb.h>
#endif
#ifdef __aarch64__
#include <vm/vm.h>
#include <vm/pmap.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#endif
#include "bman.h"
#include "qman.h"
#include "portals.h"
#ifdef QMAN_CEETM_SUPPORT
#include "qman_ceetm.h"
#endif
#define FQD_ENTRY_SIZE 64 /* matches NCSW qm.h */
extern struct dpaa_portals_softc *qp_sc;
static struct qman_softc *qman_sc;
struct qman_softc *qman_sc;
extern t_Handle qman_portal_setup(struct qman_softc *qsc);
@ -126,11 +145,28 @@ e_RxStoreResponse
qman_received_frame_callback(t_Handle app, t_Handle qm_fqr, t_Handle qm_portal,
uint32_t fqid_offset, t_DpaaFD *frame)
{
struct qman_softc *sc;
static volatile int dflt_cb_count = 0;
t_Handle pool;
int n = ++dflt_cb_count;
sc = app;
device_printf(sc->sc_dev, "dummy callback for received frame.\n");
/*
* This callback fires for frames dequeued from FQs that have no
* registered callback (e.g., FQIDs created by FMC PCD schemes).
* When fqr==NULL, fqid_offset is the raw FQID, not an offset.
*
* Return the buffer to its BMan pool to prevent pool depletion.
*/
pool = bman_pool_for_bpid(frame->bpid);
if (pool != NULL) {
bman_put_buffer(pool, DPAA_FD_GET_ADDR(frame));
} else if (n <= 50 || (n % 500) == 0) {
printf("qman: DEFAULT CB #%d: fqr=%p fqid=%u bpid=%u "
"status=0x%08x len=%u addr=%p [NO POOL]\n",
n, qm_fqr, fqid_offset, frame->bpid,
DPAA_FD_GET_STATUS(frame),
DPAA_FD_GET_LENGTH(frame),
DPAA_FD_GET_ADDR(frame));
}
return (e_RX_STORE_RESPONSE_CONTINUE);
}
@ -144,14 +180,114 @@ qman_rejected_frame_callback(t_Handle app, t_Handle qm_fqr, t_Handle qm_portal,
uint32_t fqid_offset, t_DpaaFD *frame,
t_QmRejectedFrameInfo *qm_rejected_frame_info)
{
static int ern_count;
struct qman_softc *sc;
sc = app;
device_printf(sc->sc_dev, "dummy callback for rejected frame.\n");
if (ern_count++ < 20)
device_printf(sc->sc_dev,
"ERN: rejected frame fqid_off=%u rc=%d\n",
fqid_offset,
qm_rejected_frame_info ?
qm_rejected_frame_info->rejectionCode : -1);
return (e_RX_STORE_RESPONSE_CONTINUE);
}
/*
* Map a DT reserved-memory region for QBMan use.
*
* If the reserved-memory node has a 'reg' property (fixed physical address),
* the region is already excluded from phys_avail[] by early boot code
* (fdt_foreach_reserved_mem + no-map). We map it with pmap_mapdev_attr()
* as write-back cacheable — LS1046A CCI-400 provides HW coherency.
*
* Falls back to contigmalloc for nodes with only 'size' (no fixed address).
*
* Returns mapped VA, or NULL on failure. *out_size receives the size.
*/
#ifdef __aarch64__
MALLOC_DECLARE(M_NETCOMMSW);
void *qbman_alloc_reserved_mem(device_t dev, phandle_t mem_node,
uint32_t *out_size, const char *name);
void *
qbman_alloc_reserved_mem(device_t dev, phandle_t mem_node,
uint32_t *out_size, const char *name)
{
pcell_t reg[4]; /* addr_hi, addr_lo, size_hi, size_lo */
pcell_t cells[2];
uint64_t pa, size, alignment;
void *va;
/*
* Prefer fixed physical address from 'reg' property.
* DT reserved-memory with no-map + reg is excluded from phys_avail[]
* at early boot, so this memory is never touched by the VM system.
*/
if (OF_getencprop(mem_node, "reg", reg, sizeof(reg)) ==
sizeof(reg)) {
pa = ((uint64_t)reg[0] << 32) | reg[1];
size = ((uint64_t)reg[2] << 32) | reg[3];
if (size == 0 || size > 64 * 1024 * 1024) {
device_printf(dev, "%s: invalid reg size %#jx\n",
name, (uintmax_t)size);
return (NULL);
}
va = pmap_mapdev_attr(pa, size, VM_MEMATTR_WRITE_BACK);
if (va == NULL) {
device_printf(dev,
"%s: pmap_mapdev_attr(%#jx, %#jx) failed\n",
name, (uintmax_t)pa, (uintmax_t)size);
return (NULL);
}
memset(va, 0, size);
*out_size = (uint32_t)size;
device_printf(dev,
"%s: %uMB at pa=%#jx va=%p (no-map reserved)\n",
name, (unsigned)(size >> 20), (uintmax_t)pa, va);
return (va);
}
/*
* Fallback: dynamic allocation from 'size' property.
*/
if (OF_getencprop(mem_node, "size", cells, sizeof(cells)) <= 0) {
device_printf(dev, "%s: no 'reg' or 'size' property\n", name);
return (NULL);
}
size = ((uint64_t)cells[0] << 32) | cells[1];
if (OF_getencprop(mem_node, "alignment", cells, sizeof(cells)) <= 0)
alignment = size;
else
alignment = ((uint64_t)cells[0] << 32) | cells[1];
if (size == 0 || size > 64 * 1024 * 1024) {
device_printf(dev, "%s: invalid size %#jx\n",
name, (uintmax_t)size);
return (NULL);
}
va = contigmalloc(size, M_NETCOMMSW, M_WAITOK | M_ZERO,
0, 0xFFFFFFFFULL, alignment, 0);
if (va == NULL) {
device_printf(dev, "%s: contigmalloc(%#jx, align=%#jx) failed\n",
name, (uintmax_t)size, (uintmax_t)alignment);
return (NULL);
}
*out_size = (uint32_t)size;
device_printf(dev, "%s: %uMB at %p (contigmalloc, align=%#jx)\n",
name, (unsigned)(size >> 20), va, (uintmax_t)alignment);
return (va);
}
#endif
int
qman_attach(device_t dev)
{
@ -191,9 +327,12 @@ qman_attach(device_t dev)
if (qp_sc == NULL)
goto err;
#ifndef __aarch64__
dpaa_portal_map_registers(qp_sc);
#endif
/* Initialize QMan */
memset(&qp, 0, sizeof(qp));
qp.guestId = NCSW_MASTER_ID;
qp.baseAddress = rman_get_bushandle(sc->sc_rres);
qp.swPortalsBaseAddress = rman_get_bushandle(qp_sc->sc_rres[0]);
@ -209,6 +348,48 @@ qman_attach(device_t dev)
qp.partCgsBase = 0;
qp.partNumOfCgs = 0;
#ifdef __aarch64__
/*
* Parse DT memory-region phandles to allocate FQD and PFDR memory
* from reserved-memory nodes, matching Linux's qbman_init_private_mem().
* DT: memory-region = <&qman_fqd &qman_pfdr>;
* qman-fqd: size=0x800000 (8MB) → 131072 FQIDs
* qman-pfdr: size=0x2000000 (32MB) → 524288 PFDRs
*/
{
phandle_t node, mem_node;
pcell_t mem_handles[2];
node = ofw_bus_get_node(dev);
if (node > 0 && OF_getencprop(node, "memory-region",
(void *)mem_handles, sizeof(mem_handles)) ==
sizeof(mem_handles)) {
/* Index 0 = FQD, Index 1 = PFDR */
mem_node = OF_node_from_xref(mem_handles[0]);
if (mem_node > 0) {
qp.p_FqdBase = qbman_alloc_reserved_mem(dev,
mem_node, &qp.fqdSize, "FQD");
if (qp.p_FqdBase != NULL) {
qp.totalNumOfFqids =
qp.fqdSize / FQD_ENTRY_SIZE;
qp.partNumOfFqids =
qp.totalNumOfFqids;
}
}
/* Index 1 = PFDR */
mem_node = OF_node_from_xref(mem_handles[1]);
if (mem_node > 0) {
qp.p_PfdrBase =
qbman_alloc_reserved_mem(dev,
mem_node, &qp.pfdrSize, "PFDR");
}
} else {
device_printf(dev,
"no memory-region property, using defaults\n");
}
}
#endif
sc->sc_qh = QM_Config(&qp);
if (sc->sc_qh == NULL) {
device_printf(dev, "could not be configured\n");
@ -230,9 +411,34 @@ qman_attach(device_t dev)
device_printf(dev, "Hardware version: %d.%d.\n",
rev.majorRev, rev.minorRev);
#ifndef __aarch64__
qman_portal_setup(sc);
#endif
sched_unpin();
qman_portal_setup(sc);
#ifdef __aarch64__
/*
* Initialize portals for all CPUs from boot CPU.
* ARM64: pmap_mapdev creates global kernel VA, no sched_bind needed.
* SDEST configured per-portal via QM_PORTAL_ConfigStash.
* All portals use NAPI-style interrupt handling (FILTER + taskqueue).
*/
for (int cpu = 0; cpu < mp_ncpus; cpu++) {
if (qman_portal_init_cpu(sc, cpu) == NULL)
device_printf(dev,
"could not setup QMan portal on CPU %d\n", cpu);
else
device_printf(dev,
"QMan portal %d initialized (SDEST=%d)\n",
cpu, cpu / 2);
}
#ifdef QMAN_CEETM_SUPPORT
/* Initialize CEETM if DT node is present */
qman_ceetm_init();
#endif
#endif
return (0);
@ -360,7 +566,73 @@ qman_fqr_create(uint32_t fqids_num, e_QmFQChannel channel, uint8_t wq,
goto err;
}
sc->sc_fqr_cpu[QM_FQR_GetFqid(fqrh)] = PCPU_GET(cpuid);
if (QM_FQR_GetFqid(fqrh) < QMAN_MAX_FQIDS)
sc->sc_fqr_cpu[QM_FQR_GetFqid(fqrh)] = PCPU_GET(cpuid);
sched_unpin();
return (fqrh);
err:
sched_unpin();
return (NULL);
}
t_Handle
qman_fqr_create_ctx(uint32_t fqids_num, e_QmFQChannel channel, uint8_t wq,
bool force_fqid, uint32_t fqid_or_align, bool prefer_in_cache,
t_QmContextA *p_context_a, t_QmContextB *p_context_b)
{
struct qman_softc *sc;
t_QmFqrParams fqr;
t_Handle fqrh, portal;
sc = qman_sc;
sched_pin();
portal = qman_portal_setup(sc);
if (portal == NULL) {
device_printf(sc->sc_dev, "could not setup QMan portal\n");
goto err;
}
fqr.h_Qm = sc->sc_qh;
fqr.h_QmPortal = portal;
fqr.initParked = FALSE;
fqr.holdActive = FALSE;
fqr.preferInCache = prefer_in_cache;
fqr.useContextAForStash = FALSE;
fqr.p_ContextA = p_context_a;
fqr.p_ContextB = p_context_b;
fqr.channel = channel;
fqr.wq = wq;
fqr.shadowMode = FALSE;
fqr.numOfFqids = fqids_num;
fqr.useForce = force_fqid;
if (force_fqid)
fqr.qs.frcQ.fqid = fqid_or_align;
else
fqr.qs.nonFrcQs.align = fqid_or_align;
fqr.congestionAvoidanceEnable = FALSE;
fqr.congestionAvoidanceParams.h_QmCg = 0;
fqr.congestionAvoidanceParams.overheadAccountingLength = 0;
fqr.congestionAvoidanceParams.fqTailDropThreshold = 0;
fqrh = QM_FQR_Create(&fqr);
if (fqrh == NULL) {
device_printf(sc->sc_dev,
"could not create Frame Queue Range (ctx)\n");
goto err;
}
if (QM_FQR_GetFqid(fqrh) < QMAN_MAX_FQIDS)
sc->sc_fqr_cpu[QM_FQR_GetFqid(fqrh)] = PCPU_GET(cpuid);
sched_unpin();
@ -379,9 +651,11 @@ qman_fqr_free(t_Handle fqr)
t_Error error;
sc = qman_sc;
thread_lock(curthread);
sched_bind(curthread, sc->sc_fqr_cpu[QM_FQR_GetFqid(fqr)]);
thread_unlock(curthread);
if (QM_FQR_GetFqid(fqr) < QMAN_MAX_FQIDS) {
thread_lock(curthread);
sched_bind(curthread, sc->sc_fqr_cpu[QM_FQR_GetFqid(fqr)]);
thread_unlock(curthread);
}
error = QM_FQR_Free(fqr);
@ -443,6 +717,30 @@ qman_fqr_enqueue(t_Handle fqr, uint32_t fqid_off, t_DpaaFD *frame)
return (error);
}
t_Error
qman_enqueue_fqid(uint32_t fqid, t_DpaaFD *frame)
{
struct qman_softc *sc;
t_Error error;
t_Handle portal;
sc = qman_sc;
sched_pin();
portal = qman_portal_setup(sc);
if (portal == NULL) {
device_printf(sc->sc_dev, "could not setup QMan portal\n");
sched_unpin();
return (E_NOT_SUPPORTED);
}
error = QM_PORTAL_EnqueueFqid(portal, fqid, frame);
sched_unpin();
return (error);
}
uint32_t
qman_fqr_get_counter(t_Handle fqr, uint32_t fqid_off,
e_QmFqrCounters counter)
@ -545,7 +843,64 @@ qman_poll(e_QmPortalPollSource source)
}
/*
* TODO: add polling and/or congestion support.
* QMan diagnostic counters — expose QM_GetCounter to dtsec driver.
*/
uint32_t
qman_get_pfdr_in_use(void)
{
struct qman_softc *sc;
sc = qman_sc;
if (sc == NULL || sc->sc_qh == NULL)
return (0);
return (QM_GetCounter(sc->sc_qh, e_QM_COUNTERS_PFDR_IN_USE));
}
uint32_t
qman_get_sfdr_in_use(void)
{
struct qman_softc *sc;
sc = qman_sc;
if (sc == NULL || sc->sc_qh == NULL)
return (0);
return (QM_GetCounter(sc->sc_qh, e_QM_COUNTERS_SFDR_IN_USE));
}
#ifdef __aarch64__
void
qman_ccsr_diag(void)
{
struct qman_softc *sc;
uint32_t err_isr, ecsr, ecir, pfdr_fpc, sfdr_in_use;
uint32_t idle_stat, dcp0_cfg, dcp0_dlm_avg;
uint32_t dcp_dd_ihrsr, dcp_dd_hasr;
sc = qman_sc;
if (sc == NULL || sc->sc_qh == NULL)
return;
QM_ErrorDiag(sc->sc_qh, &err_isr, &ecsr, &ecir, &pfdr_fpc,
&sfdr_in_use, &idle_stat, &dcp0_cfg, &dcp0_dlm_avg,
&dcp_dd_ihrsr, &dcp_dd_hasr);
printf("qman: CCSR err_isr=0x%08x ecsr=0x%08x ecir=0x%08x\n",
err_isr, ecsr, ecir);
if (ecir) {
printf("qman: ecir: %s portal %u fqid %u\n",
(ecir & 0x20000000) ? "DCP" : "SWP",
(ecir >> 24) & 0x1f,
ecir & 0x00ffffff);
}
printf("qman: pfdr_free=%u sfdr_in_use=%u idle=0x%08x\n",
pfdr_fpc, sfdr_in_use, idle_stat);
printf("qman: dcp0: cfg=0x%08x dlm_avg=0x%08x\n",
dcp0_cfg, dcp0_dlm_avg);
printf("qman: dcp_dd: ihrsr=0x%08x hasr=0x%08x\n",
dcp_dd_ihrsr, dcp_dd_hasr);
}
#endif
/** @} */

View file

@ -39,7 +39,7 @@
/**
* Maximum number of frame queues in all QMans.
*/
#define QMAN_MAX_FQIDS 16
#define QMAN_MAX_FQIDS 4096
/**
* Pool channel common to all software portals.
@ -85,6 +85,8 @@ struct qman_softc {
* @group QMan bus interface
* @{
*/
extern struct qman_softc *qman_sc;
int qman_attach(device_t dev);
int qman_detach(device_t dev);
int qman_suspend(device_t dev);
@ -185,6 +187,16 @@ t_Error qman_fqr_register_cb(t_Handle fqr, t_QmReceivedFrameCallback *callback,
*/
t_Error qman_fqr_enqueue(t_Handle fqr, uint32_t fqid_off, t_DpaaFD *frame);
/**
* Enqueue a frame to a specific FQID (bypassing FQR).
* Used for CEETM frame queues managed by CEETM hardware.
*
* @param fqid Target Frame Queue ID.
* @param frame A frame to be enqueued.
* @return E_OK on success; error code otherwise.
*/
t_Error qman_enqueue_fqid(uint32_t fqid, t_DpaaFD *frame);
/**
* Get one of the FQR counter's value.
*
@ -239,6 +251,39 @@ e_RxStoreResponse qman_rejected_frame_callback(t_Handle app, t_Handle qm_fqr,
t_Handle qm_portal, uint32_t fqid_offset, t_DpaaFD *frame,
t_QmRejectedFrameInfo *qm_rejected_frame_info);
/**
* Create Frame Queue Range with explicit contextA/B.
*
* Same as qman_fqr_create() but allows setting raw FQD contextA and
* contextB fields. Used by CDX to create forwarding TX FQs with
* OVFQ/EBD bits set for hardware flow offload.
*
* @param fqids_num Number of frame queues in the range.
* @param channel Dedicated channel (e.g. FMan TX port).
* @param wq Work Queue Number within the channel.
* @param force_fqid If TRUE, force allocation of specific FQID.
* @param fqid_or_align FQID if force_fqid, alignment otherwise.
* @param prefer_in_cache If TRUE, prefer QMan internal cache.
* @param p_context_a Raw contextA (8 bytes), or NULL.
* @param p_context_b Raw contextB (4 bytes), or NULL.
* @return NCSW FQR handle, or NULL on failure.
*/
t_Handle qman_fqr_create_ctx(uint32_t fqids_num, e_QmFQChannel channel,
uint8_t wq, bool force_fqid, uint32_t fqid_or_align,
bool prefer_in_cache, t_QmContextA *p_context_a,
t_QmContextB *p_context_b);
uint32_t qman_get_pfdr_in_use(void);
uint32_t qman_get_sfdr_in_use(void);
#ifdef __aarch64__
t_Handle qman_portal_init_cpu(struct qman_softc *, int);
void qman_portal_dqrr_diag(void);
void qman_ccsr_diag(void);
struct mbuf;
void qman_rx_defer(struct mbuf *);
#endif
/** @} */
#endif /* QMAN_H */

View file

@ -103,7 +103,10 @@ static driver_t qm_portals_driver = {
};
EARLY_DRIVER_MODULE(qman_portals, ofwbus, qm_portals_driver, 0, 0,
BUS_PASS_BUS);
BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
/* On ARM64, portal bus nodes are children of soc (simplebus), not ofwbus */
EARLY_DRIVER_MODULE(qman_portals, simplebus, qm_portals_driver, 0, 0,
BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LAST);
static void
get_addr_props(phandle_t node, uint32_t *addrp, uint32_t *sizep)
@ -147,7 +150,7 @@ qman_portal_find_cpu(int cpu)
return (-1);
for (node = OF_child(node); node != 0; node = OF_peer(node)) {
if (OF_getprop(node, "reg", &reg, sizeof(reg)) <= 0)
if (OF_getencprop(node, "reg", &reg, sizeof(reg)) <= 0)
continue;
if (reg == cpu)
return (node);
@ -212,14 +215,16 @@ qman_portals_fdt_attach(device_t dev)
continue;
}
/* Checkout related cpu */
if (OF_getprop(child, "cpu-handle", (void *)&cpu,
if (OF_getencprop(child, "cpu-handle", (void *)&cpu,
sizeof(cpu)) <= 0) {
cpu = qman_portal_find_cpu(cpus);
if (cpu <= 0)
/* No cpu-handle (ARM64): find CPU node directly */
cpu_node = qman_portal_find_cpu(cpus);
if (cpu_node <= 0)
continue;
} else {
/* cpu-handle is an ihandle (PowerPC) */
cpu_node = OF_instance_to_package(cpu);
}
/* Acquire cpu number */
cpu_node = OF_instance_to_package(cpu);
if (OF_getencprop(cpu_node, "reg", &cpu_num, sizeof(cpu_num)) <= 0) {
device_printf(dev, "Could not retrieve CPU number.\n");
return (ENXIO);

View file

@ -36,15 +36,19 @@
#include <sys/proc.h>
#include <sys/pcpu.h>
#include <sys/sched.h>
#include <sys/smp.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>
#ifdef __powerpc__
#include <machine/tlb.h>
#include <powerpc/mpc85xx/mpc85xx.h>
#endif
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <powerpc/mpc85xx/mpc85xx.h>
#include "qman.h"
#include "portals.h"
@ -60,15 +64,16 @@ struct dpaa_portals_softc *qp_sc;
int
qman_portals_attach(device_t dev)
{
struct dpaa_portals_softc *sc;
qp_sc = device_get_softc(dev);
sc = qp_sc = device_get_softc(dev);
/* Map bman portal to physical address space */
#ifdef __powerpc__
struct dpaa_portals_softc *sc = qp_sc;
/* Map qman portal to physical address space (PowerPC LAW) */
if (law_enable(OCP85XX_TGTIF_QMAN, sc->sc_dp_pa, sc->sc_dp_size)) {
qman_portals_detach(dev);
return (ENXIO);
}
#endif
/* Set portal properties for XX_VirtToPhys() */
XX_PortalSetInfo(dev);
@ -128,6 +133,19 @@ qman_portal_setup(struct qman_softc *qsc)
sc = qp_sc;
#ifdef __aarch64__
/*
* On ARM64, all portals are pre-initialized with NAPI-style
* interrupt handling via qman_portal_init_cpu() in qman_attach().
* Just return the portal for the current CPU.
*/
sched_pin();
cpu = PCPU_GET(cpuid);
portal = sc->sc_dp[cpu].dp_ph;
sched_unpin();
return (portal);
#endif
sched_pin();
portal = NULL;
cpu = PCPU_GET(cpuid);
@ -150,9 +168,14 @@ qman_portal_setup(struct qman_softc *qsc)
/* Map portal registers */
dpaa_portal_map_registers(sc);
/* Bail if portal mapping failed (e.g. no portal allocated for this CPU) */
if (sc->sc_dp[cpu].dp_ce_va == 0 || sc->sc_dp[cpu].dp_ci_va == 0)
goto err;
/* Configure and initialize portal */
qpp.ceBaseAddress = rman_get_bushandle(sc->sc_rres[0]);
qpp.ciBaseAddress = rman_get_bushandle(sc->sc_rres[1]);
qpp.ceBaseAddress = sc->sc_dp[cpu].dp_ce_va;
qpp.ciBaseAddress = sc->sc_dp[cpu].dp_ci_va;
qpp.h_Qm = qsc->sc_qh;
qpp.swPortalId = cpu;
qpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires;
@ -186,3 +209,220 @@ err:
return (NULL);
}
#ifdef __aarch64__
#include <sys/taskqueue.h>
#include <sys/epoch.h>
#include <sys/mbuf.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
/* Per-portal NAPI state */
static struct {
struct task task;
struct taskqueue *tq;
struct mbufq rxq; /* deferred RX mbufs */
} qman_napi[MAXCPU];
/* Declared in xx_arm64.c */
extern void XX_ConfigPortalNapi(uintptr_t irq, struct taskqueue *tq,
struct task *task);
extern void XX_SetPortalNapiHandle(uintptr_t irq, t_Handle portal);
/*
* Enqueue an mbuf for deferred delivery after QM_PORTAL_Poll returns.
* Called from the DQRR RX callback (inside NCSW_PLOCK) to avoid
* re-entering the portal via if_input -> TCP -> TX -> QM_FQR_Enqueue.
*/
void
qman_rx_defer(struct mbuf *m)
{
int cpu = PCPU_GET(cpuid);
if (mbufq_enqueue(&qman_napi[cpu].rxq, m) != 0) {
static volatile uint32_t defer_drop_cnt;
uint32_t n = atomic_fetchadd_32(&defer_drop_cnt, 1) + 1;
if (n <= 5)
printf("qman: rx_defer queue full on CPU %d (#%u),"
" dropping\n", cpu, n);
m_freem(m);
}
}
/*
* NAPI-style poll task for QMan portals (FreeBSD equivalent of Linux
* dpaa_eth_poll). Runs in a per-CPU taskqueue thread at PI_NET,
* scheduled by the FILTER interrupt handler via XX_PortalFilter.
*
* Drains the entire DQRR (max 16 entries) and MR in one shot via
* QM_PORTAL_Poll, then delivers deferred RX mbufs outside NCSW_PLOCK
* to prevent nested portal enqueue from the TX path.
*/
static void
qman_portal_poll_task(void *arg, int pending)
{
t_Handle portal = arg;
struct mbuf *m;
int cpu = PCPU_GET(cpuid);
QM_PORTAL_Poll(portal, e_QM_PORTAL_POLL_SOURCE_BOTH);
QM_PORTAL_Uninhibit(portal);
/* Deliver deferred RX mbufs outside NCSW_PLOCK */
while ((m = mbufq_dequeue(&qman_napi[cpu].rxq)) != NULL)
if_input(m->m_pkthdr.rcvif, m);
}
/*
* Initialize a QMan portal for a specific CPU with NAPI-style
* interrupt handling. Like Linux's qman_create_affine_portal +
* dpaa_eth_add_channel:
*
* 1. Create a per-CPU taskqueue pinned to the portal's CPU
* 2. Configure the portal interrupt for FILTER mode (via
* XX_ConfigPortalNapi) — the FILTER handler inhibits the
* portal and enqueues the taskqueue task
* 3. Initialize the NCSW portal (Config + Stash + Init)
* 4. Subscribe to the pool channel so QMan distributes RX
* frames across all portals (round-robin)
*
* On ARM64, pmap_mapdev creates global kernel VA, so no
* sched_bind/migration is needed.
*/
t_Handle
qman_portal_init_cpu(struct qman_softc *qsc, int cpu)
{
struct dpaa_portals_softc *sc;
t_QmPortalParam qpp;
t_QmPortalStashParam stash;
t_Handle portal;
cpuset_t cpumask;
if (qp_sc == NULL)
return (NULL);
sc = qp_sc;
/* Skip if already initialized */
if (sc->sc_dp[cpu].dp_ph != NULL)
return (sc->sc_dp[cpu].dp_ph);
/* Map CE/CI registers (global kernel VA, accessible from any CPU) */
dpaa_portal_map_registers_cpu(sc, cpu);
if (sc->sc_dp[cpu].dp_ce_va == 0 || sc->sc_dp[cpu].dp_ci_va == 0)
return (NULL);
if (sc->sc_dp[cpu].dp_ires == NULL) {
printf("qman: portal %d has no IRQ resource\n", cpu);
return (NULL);
}
/* Init deferred RX mbuf queue (unlimited — DQRR ring is the natural cap) */
mbufq_init(&qman_napi[cpu].rxq, 0);
/*
* Create per-CPU taskqueue for NAPI-style polling.
* The task arg is set to the portal handle after Init.
*/
CPU_ZERO(&cpumask);
CPU_SET(cpu, &cpumask);
NET_TASK_INIT(&qman_napi[cpu].task, 0, qman_portal_poll_task, NULL);
qman_napi[cpu].tq = taskqueue_create_fast("qman_poll", M_WAITOK,
taskqueue_thread_enqueue, &qman_napi[cpu].tq);
taskqueue_start_threads_cpuset(&qman_napi[cpu].tq, 1, PI_NET,
&cpumask, "qman%d", cpu);
/*
* Configure NAPI mode before QM_PORTAL_Init — Init calls
* XX_SetIntr internally, which checks for NAPI mode and
* registers a FILTER handler instead of an ithread handler.
*/
XX_ConfigPortalNapi((uintptr_t)sc->sc_dp[cpu].dp_ires,
qman_napi[cpu].tq, &qman_napi[cpu].task);
/* Configure portal */
memset(&qpp, 0, sizeof(qpp));
qpp.ceBaseAddress = sc->sc_dp[cpu].dp_ce_va;
qpp.ciBaseAddress = sc->sc_dp[cpu].dp_ci_va;
qpp.h_Qm = qsc->sc_qh;
qpp.swPortalId = cpu;
qpp.irq = (uintptr_t)sc->sc_dp[cpu].dp_ires;
qpp.fdLiodnOffset = 0;
qpp.f_DfltFrame = qman_received_frame_callback;
qpp.f_RejectedFrame = qman_rejected_frame_callback;
qpp.h_App = qsc;
portal = QM_PORTAL_Config(&qpp);
if (portal == NULL)
return (NULL);
/*
* Configure stash destination (SDEST) for this CPU.
* Linux: qman_set_sdest(channel, cpu_idx / 2) for QMan v3+.
* Each pair of cores shares one Stash Request Queue (SRQ) in
* the CoreNet fabric. LS1046A: SRQ0=CPU0-1, SRQ1=CPU2-3.
*/
memset(&stash, 0, sizeof(stash));
stash.stashDestQueue = cpu / 2;
QM_PORTAL_ConfigStash(portal, &stash);
if (QM_PORTAL_Init(portal) != E_OK) {
QM_PORTAL_Free(portal);
return (NULL);
}
/*
* Set portal handle for the FILTER handler and task.
* The FILTER handler uses this to call QM_PORTAL_Inhibit.
* The task function receives it as the arg parameter.
*/
XX_SetPortalNapiHandle((uintptr_t)sc->sc_dp[cpu].dp_ires, portal);
qman_napi[cpu].task.ta_context = portal;
/*
* Subscribe to pool channel — all portals get RX frames.
* QMan hardware distributes frames round-robin across all
* portals subscribed to the pool channel, matching Linux's
* dpaa_eth_add_channel() which calls qman_p_static_dequeue_add
* for every CPU's portal.
*
* RX FQs are on per-CPU dedicated channels (RSS), so pool
* channel round-robin no longer causes TCP reordering. Pool
* channel is still needed for TX confirm, CDX dist FQs, and
* CAAM QI response FQs.
*/
QM_PORTAL_AddPoolChannel(portal, QMAN_COMMON_POOL_CHANNEL);
sc->sc_dp[cpu].dp_ph = portal;
return (portal);
}
void
qman_portal_dqrr_diag(void)
{
struct dpaa_portals_softc *sc;
t_Handle portal;
uint8_t sw_pi, sw_ci, sw_fill, hw_pi, hw_ci;
uint32_t isr, ier, iir;
int cpu;
if (qp_sc == NULL)
return;
sc = qp_sc;
for (cpu = 0; cpu < mp_ncpus; cpu++) {
portal = sc->sc_dp[cpu].dp_ph;
if (portal == NULL)
continue;
QM_PORTAL_DqrrDiag(portal, &sw_pi, &sw_ci, &sw_fill,
&hw_pi, &hw_ci);
QM_PORTAL_IsrDiag(portal, &isr, &ier, &iir);
printf("qman: portal%d: dqrr sw_pi=%u sw_ci=%u fill=%u "
"hw_pi=%u hw_ci=%u isr=0x%05x ier=0x%05x iir=%u\n",
cpu, sw_pi, sw_ci, sw_fill, hw_pi, hw_ci,
isr, ier, iir);
}
}
#endif