xref: /freebsd/sys/dev/dpaa/bman.h (revision 783d3ff6d7fae619db8a7990b8a6387de0c677b5)
1 /*-
2  * Copyright (c) 2011-2012 Semihalf.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #ifndef _BMAN_H
28 #define _BMAN_H
29 
30 #include <machine/vmparam.h>
31 
32 #include <contrib/ncsw/inc/Peripherals/bm_ext.h>
33 
34 /*
35  * BMAN Configuration
36  */
37 
38 /* Maximum number of buffers in all BMAN pools */
39 #define BMAN_MAX_BUFFERS	4096
40 
41 /*
42  * Portal definitions
43  */
44 #define BMAN_CE_PA(base)	(base)
45 #define BMAN_CI_PA(base)	((base) + 0x100000)
46 
47 #define BMAN_PORTAL_CE_PA(base, n)	\
48     (BMAN_CE_PA(base) + ((n) * BMAN_PORTAL_CE_SIZE))
49 #define BMAN_PORTAL_CI_PA(base, n)	\
50     (BMAN_CI_PA(base) + ((n) * BMAN_PORTAL_CI_SIZE))
51 
52 #define BMAN_CCSR_SIZE		0x1000
53 
54 struct bman_softc {
55 	device_t	sc_dev;			/* device handle */
56 	int		sc_rrid;		/* register rid */
57 	struct resource	*sc_rres;		/* register resource */
58 	int		sc_irid;		/* interrupt rid */
59 	struct resource	*sc_ires;		/* interrupt resource */
60 
61 	bool		sc_regs_mapped[MAXCPU];	/* register mapping status */
62 
63 	t_Handle	sc_bh;			/* BMAN handle */
64 	t_Handle	sc_bph[MAXCPU];		/* BMAN portal handles */
65 	vm_paddr_t	sc_bp_pa;		/* BMAN portals PA */
66 	unsigned int	sc_bpool_cpu[BM_MAX_NUM_OF_POOLS];
67 };
68 
69 /*
70  * External API
71  */
72 
73 /*
74  * @brief Function to create BMAN pool.
75  *
76  * @param bpid		The pointer to variable where Buffer Pool ID will be
77  *			stored.
78  *
79  * @param bufferSize	The size of buffers in newly created pool.
80  *
81  * @param maxBuffers	The maximum number of buffers in software stockpile.
82  *			Set to 0 if software stockpile should not be created.
83  *
84  * @param minBuffers	The minimum number of buffers in software stockpile.
85  *			Set to 0 if software stockpile should not be created.
86  *
87  * @param allocBuffers	The number of buffers to preallocate during pool
88  *			creation.
89  *
90  * @param f_GetBuf	The buffer allocating function. Called only by
91  *			bman_pool_create() and bman_pool_fill().
92  *
93  * @param f_PutBuf	The buffer freeing function. Called only by
94  *			bman_pool_destroy().
95  *
96  * @param dep_sw_entry	The software portal depletion entry threshold.
97  *			Set to 0 if depletion should not be signaled on
98  *			software portal.
99  *
100  * @param dep_sw_exit	The software portal depletion exit threshold.
101  *			Set to 0 if depletion should not be signaled on
102  *			software portal.
103  *
104  * @param dep_hw_entry	The hardware portal depletion entry threshold.
105  *			Set to 0 if depletion should not be signaled on
106  *			hardware portal.
107  *
108  * @param dep_hw_exit	The hardware portal depletion exit threshold.
109  *			Set to 0 if depletion should not be signaled on
110  *			hardware portal.
111  *
112  * @param f_Depletion	The software portal depletion notification function.
113  *			Set to NULL if depletion notification is not used.
114  *
115  * @param h_BufferPool	The user provided buffer pool context passed to
116  *			f_GetBuf, f_PutBuf and f_Depletion functions.
117  *
118  * @param f_PhysToVirt	The PA to VA translation function. Set to NULL if
119  *			default	one should be used.
120  *
121  * @param f_VirtToPhys	The VA to PA translation function. Set to NULL if
122  *			default one should be used.
123  *
124  * @returns		Handle to newly created BMAN pool or NULL on error.
125  *
126  * @cautions		If pool uses software stockpile, all accesses to given
127  *			pool must be protected by lock. Even if only hardware
128  *			portal depletion notification is used, the caller must
129  *			provide valid @p f_Depletion function.
130  */
131 t_Handle bman_pool_create(uint8_t *bpid, uint16_t bufferSize,
132     uint16_t maxBuffers, uint16_t minBuffers, uint16_t allocBuffers,
133     t_GetBufFunction *f_GetBuf, t_PutBufFunction *f_PutBuf,
134     uint32_t dep_sw_entry, uint32_t dep_sw_exit, uint32_t dep_hw_entry,
135     uint32_t dep_hw_exit, t_BmDepletionCallback *f_Depletion,
136     t_Handle h_BufferPool, t_PhysToVirt *f_PhysToVirt,
137     t_VirtToPhys *f_VirtToPhys);
138 
139 /*
140  * @brief Fill pool with buffers.
141  *
142  * The bman_pool_fill() function fills the BMAN pool with buffers. The buffers
143  * are allocated through f_GetBuf function (see bman_pool_create() description).
144  *
145  * @param pool		The BMAN pool handle.
146  * @param nbufs		The number of buffers to allocate. To maximize
147  *			performance this value should be multiple of 8.
148  *
149  * @returns		Zero on success or error code on failure.
150  */
151 int bman_pool_fill(t_Handle pool, uint16_t nbufs);
152 
153 /*
154  * @brief Destroy pool.
155  *
156  * The bman_pool_destroy() function destroys the BMAN pool. Buffers for pool
157  * are free through f_PutBuf function (see bman_pool_create() description).
158  *
159  * @param pool		The BMAN pool handle.
160  *
161  * @returns		Zero on success or error code on failure.
162  */
163 int bman_pool_destroy(t_Handle pool);
164 
165 /*
166  * @brief Get a buffer from BMAN pool.
167  *
168  * @param pool		The BMAN pool handle.
169  *
170  * @returns		Pointer to the buffer or NULL if pool is empty.
171  */
172 void *bman_get_buffer(t_Handle pool);
173 
174 /*
175  * @brief Put a buffer to BMAN pool.
176  *
177  * @param pool		The BMAN pool handle.
178  * @param buffer	The pointer to buffer.
179  *
180  * @returns		Zero on success or error code on failure.
181  */
182 int bman_put_buffer(t_Handle pool, void *buffer);
183 
184 /*
185  * @brief Count free buffers in given pool.
186  *
187  * @param pool		The BMAN pool handle.
188  *
189  * @returns		Number of free buffers in pool.
190  */
191 uint32_t bman_count(t_Handle pool);
192 
193 /*
194  * Bus i/f
195  */
196 int bman_attach(device_t dev);
197 int bman_detach(device_t dev);
198 int bman_suspend(device_t dev);
199 int bman_resume(device_t dev);
200 int bman_shutdown(device_t dev);
201 
202 #endif /* BMAN_H */
203