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