2 * Copyright (C) 2001-2003 by NBMK Encryption Technologies.
5 * NBMK Encryption Technologies provides no support of any kind for
6 * this software. Questions or concerns about it may be addressed to
7 * the members of the relevant open-source community at
8 * <tech-crypto@netbsd.org>.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the following
19 * disclaimer in the documentation and/or other materials provided
20 * with the distribution.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 /*****************************************************************************
36 * @(#) userPool.c 1.10@(#)
37 *****************************************************************************/
39 /*****************************************************************************/
40 /** @file userPool.c *
41 * @brief Memory Services - Cross-Platform *
43 * This file implements the kernel onlu portions of the user pool allocation *
44 * and management service for the NSP2000 devices. *
48 * Obtains the large allocated space. It allocates the large pool *
49 * with n8_GetLargeAllocation (platform-specific). *
51 * Purges the allocation map and deallocates the large allocation space.*
53 *****************************************************************************/
55 /*****************************************************************************
57 * 05/07/03 brr Modified user pools to take advantage of the support for
58 * multiple memory banks.
59 * 04/23/03 brr Removed redundant parameter from n8_FreeLargeAllocation.
60 * 04/21/03 brr Added support for multiple memory banks.
61 * 07/15/02 brr Do not take userPoolSem in userPoolAlloc if not initialzed.
62 * 07/11/02 brr Do not fail userPoolInit if banks is 0.
63 * 07/02/02 brr Added userPoolStats.
64 * 06/26/02 brr Remove bank parameter from calls to N8_PhysToVirt.
65 * 06/24/02 brr Original version.
66 ****************************************************************************/
67 /** @defgroup NSP2000Driver Memory Allocation Services - Cross-Platform.
71 #include "n8_OS_intf.h"
73 #include "n8_driver_parms.h"
74 #include "n8_malloc_common.h"
75 #include "n8_memory.h"
78 static int userPoolBanks
= 0;
79 static ATOMICLOCK_t userPoolSem
;
81 static unsigned long userPoolBase
[DEF_USER_POOL_BANKS
];
82 static n8_UserPoolState_t userPoolState
[DEF_USER_POOL_BANKS
];
83 static int userPoolID
[DEF_USER_POOL_BANKS
];
85 /*****************************************************************************
87 *****************************************************************************/
88 /** @ingroup NSP2000Driver
89 * @brief Displays the current state of the user pool.
91 * This function displays the current state of the user pool.
98 * See return section for error information.
99 *****************************************************************************/
101 void userPoolDisplay(void)
105 if (userPoolBanks
== 0)
107 N8_PRINT( "userPool: <************* USER POOL UNINITIALIZED ***********>\n");
111 N8_PRINT( "userPool: <***************** USER POOL MAP *****************>\n");
113 N8_PRINT( "userPool: banks = %d \n", userPoolBanks
);
115 for (ctr
= 0; ctr
< userPoolBanks
; ctr
++)
117 N8_PRINT( "userPool: <*************** USER POOL BANK #%d ***************>\n", ctr
);
119 N8_PRINT( "userPool: base address = %x, bank state = %d, session ID = %d, \n",
120 (int)(userPoolBase
[ctr
]),
121 userPoolState
[ctr
], userPoolID
[ctr
]);
123 if (userPoolState
[ctr
] != POOL_FREE
)
125 n8_memoryDisplay(N8_MEMBANK_USERPOOL
+ ctr
);
129 N8_PRINT( "userPool: <********* USER POOL ALLOCATION MAP END **********>\n");
133 /*****************************************************************************
135 *****************************************************************************/
136 /** @ingroup NSP2000Driver
137 * @brief Returns the number of allocated user pools.
139 * This function returns the number of allocated user pools.
146 * See return section for error information.
147 *****************************************************************************/
149 int userPoolCount(void)
151 return(userPoolBanks
);
154 /*****************************************************************************
156 *****************************************************************************/
157 /** @ingroup NSP2000Driver
158 * @brief Allocate and setup management for user pool allocations.
160 * This routine should be called when a driver is loaded and initialized.
162 * @param bank RO: The number of user pool banks to allocate
163 * @param size RO: Specifies the desired allocation size(MB) per bank
164 * @param granularity RO: The size of the smallest allocation.
172 * See return section for error information.
173 *****************************************************************************/
175 void userPoolInit(unsigned int banks
,
177 unsigned long granularity
)
181 /* Initialize each bank */
182 for (ctr
= 0; ctr
< banks
; ctr
++)
184 userPoolBase
[ctr
] = (unsigned long)n8_memoryInit(N8_MEMBANK_USERPOOL
+ ctr
,
185 size
* N8_ONE_MEGABYTE
,
187 if (!userPoolBase
[ctr
])
193 /* Setup user pool globals */
195 N8_AtomicLockInit(userPoolSem
);
200 /*****************************************************************************
202 *****************************************************************************/
203 /** @ingroup NSP2000Driver
204 * @brief Release memory resources allocated for user pools.
206 * This routine deallocates and releases the large space allocated for the
217 * See return section for error information.
218 *****************************************************************************/
220 void userPoolRelease(void)
224 N8_AtomicLockDel(userPoolSem
);
225 for (ctr
= 0; ctr
< userPoolBanks
; ctr
++)
227 n8_memoryRelease(N8_MEMBANK_USERPOOL
+ctr
);
228 userPoolBase
[ctr
] = 0;
235 /*****************************************************************************
237 *****************************************************************************/
238 /** @ingroup NSP2000Driver
239 * @brief Allocate a user pool for a process.
241 * This routine should be called when a process opens the device
246 * NULL pointer if process failed
247 * non-NULL void physical pointer to the base of the user pool
250 * See return section for error information.
251 *****************************************************************************/
253 unsigned long userPoolAlloc(int sessID
)
256 unsigned long addr
= 0;
257 MemBankCtl_t
*memBankCtl_p
;
259 N8_AtomicLock(userPoolSem
);
261 for (ctr
= 0; ctr
< userPoolBanks
; ctr
++)
263 if (userPoolState
[ctr
] == POOL_FREE
)
265 userPoolState
[ctr
] = POOL_ALLOCATED
;
266 userPoolID
[ctr
] = sessID
;
267 addr
= userPoolBase
[ctr
];
269 /* Reset memory control variables for new allocation */
270 memBankCtl_p
= memBankCtl_gp
[N8_MEMBANK_USERPOOL
+ctr
];
271 memBankCtl_p
->maxAllocated
= 0;
272 memBankCtl_p
->failedAllocs
= 0;
276 N8_AtomicUnlock(userPoolSem
);
282 /*****************************************************************************
284 *****************************************************************************/
285 /** @ingroup NSP2000Driver
286 * @brief Deallocate a user pool for a process.
288 * This routine should be called when a process exits
290 * @param sessID RO: The session ID of the exiting process.
297 * See return section for error information.
300 * It is assume the caller has disabled interrupts to ensure there is no
301 * conflict with the ISR accessing the currAllocated value.
302 *****************************************************************************/
304 void userPoolFree(int sessID
)
307 MemBankCtl_t
*memBankCtl_p
;
309 for (ctr
= 0; ctr
< userPoolBanks
; ctr
++)
311 if (userPoolID
[ctr
] == sessID
)
314 /* If there is memory still allocated, the command blocks on the */
315 /* queue may still be pointing to memory in this pool. Thus it */
316 /* cannot be marked as free until everything has been returned. */
317 n8_pfreeSessID(N8_MEMBANK_USERPOOL
+ctr
, sessID
);
319 memBankCtl_p
= memBankCtl_gp
[N8_MEMBANK_USERPOOL
+ctr
];
320 if (n8_atomic_read(memBankCtl_p
->curAllocated
))
322 userPoolState
[ctr
] = POOL_DELETED
;
326 userPoolState
[ctr
] = POOL_FREE
;
334 /*****************************************************************************
336 *****************************************************************************/
337 /** @ingroup NSP2000Driver
338 * @brief Mark a user pool as free
340 * This routine should be called after the last buffer has been
343 * @param addr RO: The physical address of the user pool.
350 * See return section for error information.
352 *****************************************************************************/
354 void userPoolFreePool(int bankIndex
)
357 /* Validate bank index */
358 if ((bankIndex
>= N8_MEMBANK_USERPOOL
) &&
359 (bankIndex
<=N8_MEMBANK_USERPOOL
+userPoolBanks
))
361 /* Only mark the bank as free once all the buffers have been freed */
362 if (n8_atomic_read(memBankCtl_gp
[bankIndex
]->curAllocated
) == 0)
364 userPoolState
[bankIndex
- N8_MEMBANK_USERPOOL
] = POOL_FREE
;
370 /*****************************************************************************
372 *****************************************************************************/
373 /** @ingroup NSP2000Driver
374 * @brief Returns the current state of the user pool.
376 * This function return the current state of the user pool.
383 * See return section for error information.
384 *****************************************************************************/
386 void userPoolStats(MemStats_t
*memStatsPtr
)
389 MemBankCtl_t
*memBankCtl_p
;
391 for (ctr
= 0; ctr
< userPoolBanks
; ctr
++)
394 if (userPoolState
[ctr
] != POOL_FREE
)
396 memBankCtl_p
= memBankCtl_gp
[N8_MEMBANK_USERPOOL
+ctr
];
397 memStatsPtr
->sessionID
= userPoolID
[ctr
];
398 memStatsPtr
->curAllocated
= n8_atomic_read(memBankCtl_p
->curAllocated
);
399 memStatsPtr
->maxAllocated
= memBankCtl_p
->maxAllocated
;
400 memStatsPtr
->maxEntries
= memBankCtl_p
->maxEntries
;
401 memStatsPtr
->failedAllocs
= memBankCtl_p
->failedAllocs
;
406 memStatsPtr
->sessionID
= 0;