2 * Copyright (c) 2011-2012 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
40 #include <sys/sched.h>
42 #include <machine/tlb.h>
46 static struct bman_softc *bman_sc;
48 extern t_Handle bman_portal_setup(struct bman_softc *bsc);
51 bman_exception(t_Handle h_App, e_BmExceptions exception)
53 struct bman_softc *sc;
59 case e_BM_EX_INVALID_COMMAND:
60 message = "Invalid Command Verb";
62 case e_BM_EX_FBPR_THRESHOLD:
63 message = "FBPR pool exhaused. Consider increasing "
66 case e_BM_EX_SINGLE_ECC:
67 message = "Single bit ECC error";
69 case e_BM_EX_MULTI_ECC:
70 message = "Multi bit ECC error";
73 message = "Unknown error";
76 device_printf(sc->sc_dev, "BMAN Exception: %s.\n", message);
80 bman_attach(device_t dev)
82 struct bman_softc *sc;
87 sc = device_get_softc(dev);
91 /* Check if MallocSmart allocator is ready */
92 if (XX_MallocSmartInit() != E_OK)
95 /* Allocate resources */
97 sc->sc_rres = bus_alloc_resource_anywhere(dev, SYS_RES_MEMORY,
98 &sc->sc_rrid, BMAN_CCSR_SIZE, RF_ACTIVE);
99 if (sc->sc_rres == NULL)
103 sc->sc_ires = bus_alloc_resource_any(sc->sc_dev, SYS_RES_IRQ,
104 &sc->sc_irid, RF_ACTIVE | RF_SHAREABLE);
105 if (sc->sc_ires == NULL)
108 /* Initialize BMAN */
109 memset(&bp, 0, sizeof(bp));
110 bp.guestId = NCSW_MASTER_ID;
111 bp.baseAddress = rman_get_bushandle(sc->sc_rres);
112 bp.totalNumOfBuffers = BMAN_MAX_BUFFERS;
113 bp.f_Exception = bman_exception;
115 bp.errIrq = (uintptr_t)sc->sc_ires;
117 bp.partNumOfPools = BM_MAX_NUM_OF_POOLS;
119 sc->sc_bh = BM_Config(&bp);
120 if (sc->sc_bh == NULL)
123 /* Warn if there is less than 5% free FPBR's in pool */
124 error = BM_ConfigFbprThreshold(sc->sc_bh, (BMAN_MAX_BUFFERS / 8) / 20);
128 error = BM_Init(sc->sc_bh);
132 error = BM_GetRevision(sc->sc_bh, &rev);
136 device_printf(dev, "Hardware version: %d.%d.\n",
137 rev.majorRev, rev.minorRev);
147 bman_detach(device_t dev)
149 struct bman_softc *sc;
151 sc = device_get_softc(dev);
153 if (sc->sc_bh != NULL)
156 if (sc->sc_ires != NULL)
157 bus_release_resource(dev, SYS_RES_IRQ,
158 sc->sc_irid, sc->sc_ires);
160 if (sc->sc_rres != NULL)
161 bus_release_resource(dev, SYS_RES_MEMORY,
162 sc->sc_rrid, sc->sc_rres);
168 bman_suspend(device_t dev)
175 bman_resume(device_t dev)
182 bman_shutdown(device_t dev)
193 bman_pool_create(uint8_t *bpid, uint16_t bufferSize, uint16_t maxBuffers,
194 uint16_t minBuffers, uint16_t allocBuffers, t_GetBufFunction *f_GetBuf,
195 t_PutBufFunction *f_PutBuf, uint32_t dep_sw_entry, uint32_t dep_sw_exit,
196 uint32_t dep_hw_entry, uint32_t dep_hw_exit,
197 t_BmDepletionCallback *f_Depletion, t_Handle h_BufferPool,
198 t_PhysToVirt *f_PhysToVirt, t_VirtToPhys *f_VirtToPhys)
200 uint32_t thresholds[MAX_DEPLETION_THRESHOLDS];
201 struct bman_softc *sc;
202 t_Handle pool, portal;
211 portal = bman_portal_setup(sc);
215 memset(&bpp, 0, sizeof(bpp));
216 bpp.h_Bm = sc->sc_bh;
217 bpp.h_BmPortal = portal;
218 bpp.h_App = h_BufferPool;
219 bpp.numOfBuffers = allocBuffers;
221 bpp.bufferPoolInfo.h_BufferPool = h_BufferPool;
222 bpp.bufferPoolInfo.f_GetBuf = f_GetBuf;
223 bpp.bufferPoolInfo.f_PutBuf = f_PutBuf;
224 bpp.bufferPoolInfo.f_PhysToVirt = f_PhysToVirt;
225 bpp.bufferPoolInfo.f_VirtToPhys = f_VirtToPhys;
226 bpp.bufferPoolInfo.bufferSize = bufferSize;
228 pool = BM_POOL_Config(&bpp);
233 * Buffer context must be disabled on FreeBSD
234 * as it could cause memory corruption.
236 BM_POOL_ConfigBuffContextMode(pool, 0);
238 if (minBuffers != 0 || maxBuffers != 0) {
239 error = BM_POOL_ConfigStockpile(pool, maxBuffers, minBuffers);
244 if (f_Depletion != NULL) {
245 thresholds[BM_POOL_DEP_THRESH_SW_ENTRY] = dep_sw_entry;
246 thresholds[BM_POOL_DEP_THRESH_SW_EXIT] = dep_sw_exit;
247 thresholds[BM_POOL_DEP_THRESH_HW_ENTRY] = dep_hw_entry;
248 thresholds[BM_POOL_DEP_THRESH_HW_EXIT] = dep_hw_exit;
249 error = BM_POOL_ConfigDepletion(pool, f_Depletion, thresholds);
254 error = BM_POOL_Init(pool);
258 *bpid = BM_POOL_GetId(pool);
259 sc->sc_bpool_cpu[*bpid] = PCPU_GET(cpuid);
275 bman_pool_destroy(t_Handle pool)
277 struct bman_softc *sc;
280 thread_lock(curthread);
281 sched_bind(curthread, sc->sc_bpool_cpu[BM_POOL_GetId(pool)]);
282 thread_unlock(curthread);
286 thread_lock(curthread);
287 sched_unbind(curthread);
288 thread_unlock(curthread);
294 bman_pool_fill(t_Handle pool, uint16_t nbufs)
296 struct bman_softc *sc;
303 portal = bman_portal_setup(sc);
304 if (portal == NULL) {
309 error = BM_POOL_FillBufs(pool, portal, nbufs);
313 return ((error == E_OK) ? 0 : EIO);
317 bman_get_buffer(t_Handle pool)
319 struct bman_softc *sc;
326 portal = bman_portal_setup(sc);
327 if (portal == NULL) {
332 buffer = BM_POOL_GetBuf(pool, portal);
340 bman_put_buffer(t_Handle pool, void *buffer)
342 struct bman_softc *sc;
349 portal = bman_portal_setup(sc);
350 if (portal == NULL) {
355 error = BM_POOL_PutBuf(pool, portal, buffer);
359 return ((error == E_OK) ? 0 : EIO);
363 bman_count(t_Handle pool)
366 return (BM_POOL_GetCounter(pool, e_BM_POOL_COUNTERS_CONTENT));