Path: blob/main/sys/contrib/ncsw/Peripherals/BM/bman_low.c
48378 views
/******************************************************************************12� 1995-2003, 2004, 2005-2011 Freescale Semiconductor, Inc.3All rights reserved.45This is proprietary source code of Freescale Semiconductor Inc.,6and its use is subject to the NetComm Device Drivers EULA.7The copyright notice above does not evidence any actual or intended8publication of such source code.910ALTERNATIVELY, redistribution and use in source and binary forms, with11or without modification, are permitted provided that the following12conditions are met:13* Redistributions of source code must retain the above copyright14notice, this list of conditions and the following disclaimer.15* Redistributions in binary form must reproduce the above copyright16notice, this list of conditions and the following disclaimer in the17documentation and/or other materials provided with the distribution.18* Neither the name of Freescale Semiconductor nor the19names of its contributors may be used to endorse or promote products20derived from this software without specific prior written permission.2122THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY23EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED24WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE25DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY26DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES27(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;28LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND29ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT30(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*3334**************************************************************************/35/******************************************************************************36@File bman_low.c3738@Description BM low-level implementation39*//***************************************************************************/40#include <sys/cdefs.h>41#include <sys/types.h>42#include <machine/atomic.h>4344#include "std_ext.h"45#include "core_ext.h"46#include "xx_ext.h"47#include "error_ext.h"4849#include "bman_private.h"505152/***************************/53/* Portal register assists */54/***************************/5556/* Cache-inhibited register offsets */57#define REG_RCR_PI_CINH 0x000058#define REG_RCR_CI_CINH 0x000459#define REG_RCR_ITR 0x000860#define REG_CFG 0x010061#define REG_SCN(n) (0x0200 + ((n) << 2))62#define REG_ISR 0x0e0063#define REG_IER 0x0e0464#define REG_ISDR 0x0e0865#define REG_IIR 0x0e0c6667/* Cache-enabled register offsets */68#define CL_CR 0x000069#define CL_RR0 0x010070#define CL_RR1 0x014071#define CL_RCR 0x100072#define CL_RCR_PI_CENA 0x300073#define CL_RCR_CI_CENA 0x31007475/* The h/w design requires mappings to be size-aligned so that "add"s can be76* reduced to "or"s. The primitives below do the same for s/w. */7778static __inline__ void *ptr_ADD(void *a, uintptr_t b)79{80return (void *)((uintptr_t)a + b);81}8283/* Bitwise-OR two pointers */84static __inline__ void *ptr_OR(void *a, uintptr_t b)85{86return (void *)((uintptr_t)a | b);87}8889/* Cache-inhibited register access */90static __inline__ uint32_t __bm_in(struct bm_addr *bm, uintptr_t offset)91{92uint32_t *tmp = (uint32_t *)ptr_ADD(bm->addr_ci, offset);93return GET_UINT32(*tmp);94}95static __inline__ void __bm_out(struct bm_addr *bm, uintptr_t offset, uint32_t val)96{97uint32_t *tmp = (uint32_t *)ptr_ADD(bm->addr_ci, offset);98WRITE_UINT32(*tmp, val);99}100#define bm_in(reg) __bm_in(&portal->addr, REG_##reg)101#define bm_out(reg, val) __bm_out(&portal->addr, REG_##reg, val)102103/* Convert 'n' cachelines to a pointer value for bitwise OR */104#define bm_cl(n) (void *)((n) << 6)105106/* Cache-enabled (index) register access */107static __inline__ void __bm_cl_touch_ro(struct bm_addr *bm, uintptr_t offset)108{109dcbt_ro(ptr_ADD(bm->addr_ce, offset));110}111static __inline__ void __bm_cl_touch_rw(struct bm_addr *bm, uintptr_t offset)112{113dcbt_rw(ptr_ADD(bm->addr_ce, offset));114}115static __inline__ uint32_t __bm_cl_in(struct bm_addr *bm, uintptr_t offset)116{117uint32_t *tmp = (uint32_t *)ptr_ADD(bm->addr_ce, offset);118return GET_UINT32(*tmp);119}120static __inline__ void __bm_cl_out(struct bm_addr *bm, uintptr_t offset, uint32_t val)121{122uint32_t *tmp = (uint32_t *)ptr_ADD(bm->addr_ce, offset);123WRITE_UINT32(*tmp, val);124dcbf(tmp);125}126static __inline__ void __bm_cl_invalidate(struct bm_addr *bm, uintptr_t offset)127{128dcbi(ptr_ADD(bm->addr_ce, offset));129}130#define bm_cl_touch_ro(reg) __bm_cl_touch_ro(&portal->addr, CL_##reg##_CENA)131#define bm_cl_touch_rw(reg) __bm_cl_touch_rw(&portal->addr, CL_##reg##_CENA)132#define bm_cl_in(reg) __bm_cl_in(&portal->addr, CL_##reg##_CENA)133#define bm_cl_out(reg, val) __bm_cl_out(&portal->addr, CL_##reg##_CENA, val)134#define bm_cl_invalidate(reg) __bm_cl_invalidate(&portal->addr, CL_##reg##_CENA)135136/* Cyclic helper for rings. TODO: once we are able to do fine-grain perf137* analysis, look at using the "extra" bit in the ring index registers to avoid138* cyclic issues. */139static __inline__ uint8_t cyc_diff(uint8_t ringsize, uint8_t first, uint8_t last)140{141/* 'first' is included, 'last' is excluded */142if (first <= last)143return (uint8_t)(last - first);144return (uint8_t)(ringsize + last - first);145}146147/* --------------- */148/* --- RCR API --- */149150/* It's safer to code in terms of the 'rcr' object than the 'portal' object,151* because the latter runs the risk of copy-n-paste errors from other code where152* we could manipulate some other structure within 'portal'. */153/* #define RCR_API_START() register struct bm_rcr *rcr = &portal->rcr */154155/* Bit-wise logic to wrap a ring pointer by clearing the "carry bit" */156#define RCR_CARRYCLEAR(p) \157(void *)((uintptr_t)(p) & (~(uintptr_t)(BM_RCR_SIZE << 6)))158159/* Bit-wise logic to convert a ring pointer to a ring index */160static __inline__ uint8_t RCR_PTR2IDX(struct bm_rcr_entry *e)161{162return (uint8_t)(((uintptr_t)e >> 6) & (BM_RCR_SIZE - 1));163}164165/* Increment the 'cursor' ring pointer, taking 'vbit' into account */166static __inline__ void RCR_INC(struct bm_rcr *rcr)167{168/* NB: this is odd-looking, but experiments show that it generates169* fast code with essentially no branching overheads. We increment to170* the next RCR pointer and handle overflow and 'vbit'. */171struct bm_rcr_entry *partial = rcr->cursor + 1;172rcr->cursor = RCR_CARRYCLEAR(partial);173if (partial != rcr->cursor)174rcr->vbit ^= BM_RCR_VERB_VBIT;175}176177t_Error bm_rcr_init(struct bm_portal *portal,178e_BmPortalProduceMode pmode,179e_BmPortalRcrConsumeMode cmode)180{181register struct bm_rcr *rcr = &portal->rcr;182uint32_t cfg;183uint8_t pi;184185rcr->ring = ptr_ADD(portal->addr.addr_ce, CL_RCR);186rcr->ci = (uint8_t)(bm_in(RCR_CI_CINH) & (BM_RCR_SIZE - 1));187pi = (uint8_t)(bm_in(RCR_PI_CINH) & (BM_RCR_SIZE - 1));188rcr->cursor = rcr->ring + pi;189rcr->vbit = (uint8_t)((bm_in(RCR_PI_CINH) & BM_RCR_SIZE) ? BM_RCR_VERB_VBIT : 0);190rcr->available = (uint8_t)(BM_RCR_SIZE - 1 - cyc_diff(BM_RCR_SIZE, rcr->ci, pi));191rcr->ithresh = (uint8_t)bm_in(RCR_ITR);192#ifdef BM_CHECKING193rcr->busy = 0;194rcr->pmode = pmode;195rcr->cmode = cmode;196#else197UNUSED(cmode);198#endif /* BM_CHECKING */199cfg = (bm_in(CFG) & 0xffffffe0) | (pmode & 0x3); /* BCSP_CFG::RPM */200bm_out(CFG, cfg);201return 0;202}203204void bm_rcr_finish(struct bm_portal *portal)205{206register struct bm_rcr *rcr = &portal->rcr;207uint8_t pi = (uint8_t)(bm_in(RCR_PI_CINH) & (BM_RCR_SIZE - 1));208uint8_t ci = (uint8_t)(bm_in(RCR_CI_CINH) & (BM_RCR_SIZE - 1));209ASSERT_COND(!rcr->busy);210if (pi != RCR_PTR2IDX(rcr->cursor))211REPORT_ERROR(WARNING, E_INVALID_STATE, ("losing uncommitted RCR entries"));212if (ci != rcr->ci)213REPORT_ERROR(WARNING, E_INVALID_STATE, ("missing existing RCR completions"));214if (rcr->ci != RCR_PTR2IDX(rcr->cursor))215REPORT_ERROR(WARNING, E_INVALID_STATE, ("RCR destroyed unquiesced"));216}217218struct bm_rcr_entry *bm_rcr_start(struct bm_portal *portal)219{220register struct bm_rcr *rcr = &portal->rcr;221ASSERT_COND(!rcr->busy);222if (!rcr->available)223return NULL;224#ifdef BM_CHECKING225rcr->busy = 1;226#endif /* BM_CHECKING */227dcbz_64(rcr->cursor);228return rcr->cursor;229}230231void bm_rcr_abort(struct bm_portal *portal)232{233register struct bm_rcr *rcr = &portal->rcr;234ASSERT_COND(rcr->busy);235#ifdef BM_CHECKING236rcr->busy = 0;237#else238UNUSED(rcr);239#endif /* BM_CHECKING */240}241242struct bm_rcr_entry *bm_rcr_pend_and_next(struct bm_portal *portal, uint8_t myverb)243{244register struct bm_rcr *rcr = &portal->rcr;245ASSERT_COND(rcr->busy);246ASSERT_COND(rcr->pmode != e_BmPortalPVB);247if (rcr->available == 1)248return NULL;249rcr->cursor->__dont_write_directly__verb = (uint8_t)(myverb | rcr->vbit);250dcbf_64(rcr->cursor);251RCR_INC(rcr);252rcr->available--;253dcbz_64(rcr->cursor);254return rcr->cursor;255}256257void bm_rcr_pci_commit(struct bm_portal *portal, uint8_t myverb)258{259register struct bm_rcr *rcr = &portal->rcr;260ASSERT_COND(rcr->busy);261ASSERT_COND(rcr->pmode == e_BmPortalPCI);262rcr->cursor->__dont_write_directly__verb = (uint8_t)(myverb | rcr->vbit);263RCR_INC(rcr);264rcr->available--;265mb();266bm_out(RCR_PI_CINH, RCR_PTR2IDX(rcr->cursor));267#ifdef BM_CHECKING268rcr->busy = 0;269#endif /* BM_CHECKING */270}271272void bm_rcr_pce_prefetch(struct bm_portal *portal)273{274ASSERT_COND(((struct bm_rcr *)&portal->rcr)->pmode == e_BmPortalPCE);275bm_cl_invalidate(RCR_PI);276bm_cl_touch_rw(RCR_PI);277}278279void bm_rcr_pce_commit(struct bm_portal *portal, uint8_t myverb)280{281register struct bm_rcr *rcr = &portal->rcr;282ASSERT_COND(rcr->busy);283ASSERT_COND(rcr->pmode == e_BmPortalPCE);284rcr->cursor->__dont_write_directly__verb = (uint8_t)(myverb | rcr->vbit);285RCR_INC(rcr);286rcr->available--;287wmb();288bm_cl_out(RCR_PI, RCR_PTR2IDX(rcr->cursor));289#ifdef BM_CHECKING290rcr->busy = 0;291#endif /* BM_CHECKING */292}293294void bm_rcr_pvb_commit(struct bm_portal *portal, uint8_t myverb)295{296register struct bm_rcr *rcr = &portal->rcr;297struct bm_rcr_entry *rcursor;298ASSERT_COND(rcr->busy);299ASSERT_COND(rcr->pmode == e_BmPortalPVB);300rmb();301rcursor = rcr->cursor;302rcursor->__dont_write_directly__verb = (uint8_t)(myverb | rcr->vbit);303dcbf_64(rcursor);304RCR_INC(rcr);305rcr->available--;306#ifdef BM_CHECKING307rcr->busy = 0;308#endif /* BM_CHECKING */309}310311312uint8_t bm_rcr_cci_update(struct bm_portal *portal)313{314register struct bm_rcr *rcr = &portal->rcr;315uint8_t diff, old_ci = rcr->ci;316ASSERT_COND(rcr->cmode == e_BmPortalRcrCCI);317rcr->ci = (uint8_t)(bm_in(RCR_CI_CINH) & (BM_RCR_SIZE - 1));318diff = cyc_diff(BM_RCR_SIZE, old_ci, rcr->ci);319rcr->available += diff;320return diff;321}322323324void bm_rcr_cce_prefetch(struct bm_portal *portal)325{326ASSERT_COND(((struct bm_rcr *)&portal->rcr)->cmode == e_BmPortalRcrCCE);327bm_cl_touch_ro(RCR_CI);328}329330331uint8_t bm_rcr_cce_update(struct bm_portal *portal)332{333register struct bm_rcr *rcr = &portal->rcr;334uint8_t diff, old_ci = rcr->ci;335ASSERT_COND(rcr->cmode == e_BmPortalRcrCCE);336rcr->ci = (uint8_t)(bm_cl_in(RCR_CI) & (BM_RCR_SIZE - 1));337bm_cl_invalidate(RCR_CI);338diff = cyc_diff(BM_RCR_SIZE, old_ci, rcr->ci);339rcr->available += diff;340return diff;341}342343344uint8_t bm_rcr_get_ithresh(struct bm_portal *portal)345{346register struct bm_rcr *rcr = &portal->rcr;347return rcr->ithresh;348}349350351void bm_rcr_set_ithresh(struct bm_portal *portal, uint8_t ithresh)352{353register struct bm_rcr *rcr = &portal->rcr;354rcr->ithresh = ithresh;355bm_out(RCR_ITR, ithresh);356}357358359uint8_t bm_rcr_get_avail(struct bm_portal *portal)360{361register struct bm_rcr *rcr = &portal->rcr;362return rcr->available;363}364365366uint8_t bm_rcr_get_fill(struct bm_portal *portal)367{368register struct bm_rcr *rcr = &portal->rcr;369return (uint8_t)(BM_RCR_SIZE - 1 - rcr->available);370}371372373/* ------------------------------ */374/* --- Management command API --- */375376/* It's safer to code in terms of the 'mc' object than the 'portal' object,377* because the latter runs the risk of copy-n-paste errors from other code where378* we could manipulate some other structure within 'portal'. */379/* #define MC_API_START() register struct bm_mc *mc = &portal->mc */380381382t_Error bm_mc_init(struct bm_portal *portal)383{384register struct bm_mc *mc = &portal->mc;385mc->cr = ptr_ADD(portal->addr.addr_ce, CL_CR);386mc->rr = ptr_ADD(portal->addr.addr_ce, CL_RR0);387mc->rridx = (uint8_t)((mc->cr->__dont_write_directly__verb & BM_MCC_VERB_VBIT) ?3880 : 1);389mc->vbit = (uint8_t)(mc->rridx ? BM_MCC_VERB_VBIT : 0);390#ifdef BM_CHECKING391mc->state = mc_idle;392#endif /* BM_CHECKING */393return 0;394}395396397void bm_mc_finish(struct bm_portal *portal)398{399register struct bm_mc *mc = &portal->mc;400ASSERT_COND(mc->state == mc_idle);401#ifdef BM_CHECKING402if (mc->state != mc_idle)403REPORT_ERROR(WARNING, E_INVALID_STATE, ("Losing incomplete MC command"));404#else405UNUSED(mc);406#endif /* BM_CHECKING */407}408409410struct bm_mc_command *bm_mc_start(struct bm_portal *portal)411{412register struct bm_mc *mc = &portal->mc;413ASSERT_COND(mc->state == mc_idle);414#ifdef BM_CHECKING415mc->state = mc_user;416#endif /* BM_CHECKING */417dcbz_64(mc->cr);418return mc->cr;419}420421422void bm_mc_abort(struct bm_portal *portal)423{424register struct bm_mc *mc = &portal->mc;425ASSERT_COND(mc->state == mc_user);426#ifdef BM_CHECKING427mc->state = mc_idle;428#else429UNUSED(mc);430#endif /* BM_CHECKING */431}432433434void bm_mc_commit(struct bm_portal *portal, uint8_t myverb)435{436register struct bm_mc *mc = &portal->mc;437ASSERT_COND(mc->state == mc_user);438rmb();439mc->cr->__dont_write_directly__verb = (uint8_t)(myverb | mc->vbit);440dcbf_64(mc->cr);441dcbit_ro(mc->rr + mc->rridx);442#ifdef BM_CHECKING443mc->state = mc_hw;444#endif /* BM_CHECKING */445}446447448struct bm_mc_result *bm_mc_result(struct bm_portal *portal)449{450register struct bm_mc *mc = &portal->mc;451struct bm_mc_result *rr = mc->rr + mc->rridx;452ASSERT_COND(mc->state == mc_hw);453/* The inactive response register's verb byte always returns zero until454* its command is submitted and completed. This includes the valid-bit,455* in case you were wondering... */456if (!rr->verb) {457dcbit_ro(rr);458return NULL;459}460mc->rridx ^= 1;461mc->vbit ^= BM_MCC_VERB_VBIT;462#ifdef BM_CHECKING463mc->state = mc_idle;464#endif /* BM_CHECKING */465return rr;466}467468/* ------------------------------------- */469/* --- Portal interrupt register API --- */470471#define SCN_REG(bpid) REG_SCN((bpid) / 32)472#define SCN_BIT(bpid) (0x80000000 >> (bpid & 31))473void bm_isr_bscn_mask(struct bm_portal *portal, uint8_t bpid, int enable)474{475uint32_t val;476ASSERT_COND(bpid < BM_MAX_NUM_OF_POOLS);477/* REG_SCN for bpid=0..31, REG_SCN+4 for bpid=32..63 */478val = __bm_in(&portal->addr, SCN_REG(bpid));479if (enable)480val |= SCN_BIT(bpid);481else482val &= ~SCN_BIT(bpid);483__bm_out(&portal->addr, SCN_REG(bpid), val);484}485486487uint32_t __bm_isr_read(struct bm_portal *portal, enum bm_isr_reg n)488{489return __bm_in(&portal->addr, REG_ISR + (n << 2));490}491492493void __bm_isr_write(struct bm_portal *portal, enum bm_isr_reg n, uint32_t val)494{495__bm_out(&portal->addr, REG_ISR + (n << 2), val);496}497498499500