summaryrefslogtreecommitdiff
path: root/wl1271/platforms/os/linux/src/osmemapi.c
blob: 85a775359dfbe597a3257d51a675abd345f71b08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
 * osmemapi.c
 *
 * Copyright(c) 1998 - 2009 Texas Instruments. All rights reserved.      
 * All rights reserved.                                                  
 *                                                                       
 * Redistribution and use in source and binary forms, with or without    
 * modification, are permitted provided that the following conditions    
 * are met:                                                              
 *                                                                       
 *  * Redistributions of source code must retain the above copyright     
 *    notice, this list of conditions and the following disclaimer.      
 *  * Redistributions in binary form must reproduce the above copyright  
 *    notice, this list of conditions and the following disclaimer in    
 *    the documentation and/or other materials provided with the         
 *    distribution.                                                      
 *  * Neither the name Texas Instruments nor the names of its            
 *    contributors may be used to endorse or promote products derived    
 *    from this software without specific prior written permission.      
 *                                                                       
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


/*
 * src/osmemapi.c
 *
 */

#include "arch_ti.h"

#include <linux/stddef.h>
#include <linux/string.h>
#include <linux/time.h>
#include <linux/timer.h>

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/vmalloc.h>
#include <linux/string.h>
#include <linux/delay.h>
#include <linux/time.h>
#include <linux/list.h>

#include "osApi.h"
#include "tidef.h"
#include "WlanDrvIf.h"

typedef void (*os_free)(void *);
struct os_mem_block
{
	struct list_head blk_list;
	os_free f_free;
	__u32 size;
	__u32 signature;
};
#define MEM_BLOCK_START  (('m'<<24) | ('e'<<16) | ('m'<<8) | 's')
#define MEM_BLOCK_END    (('m'<<24) | ('e'<<16) | ('m'<<8) | 'e')

/****************************************************************************************
 *                        																*
 *						OS Memory API													*       
 *																						*
 ****************************************************************************************/

/****************************************************************************************
 *                        os_memoryAlloc()                                 
 ****************************************************************************************
DESCRIPTION:    Allocates resident (nonpaged) system-space memory.

ARGUMENTS:		OsContext	- our adapter context.
				Size		- Specifies the size, in bytes, to be allocated.

RETURN:			Pointer to the allocated memory.
				NULL if there is insufficient memory available.

NOTES:         	With the call to vmalloc it is assumed that this function will
				never be called in an interrupt context. vmalloc has the potential to
				sleep the caller while waiting for memory to become available.

*****************************************************************************************/
void*
os_memoryAlloc(
        TI_HANDLE OsContext,
        TI_UINT32 Size
        )
{
	struct os_mem_block *blk;
	__u32 total_size = Size + sizeof(struct os_mem_block) + sizeof(__u32);

#ifdef TI_MEM_ALLOC_TRACE
	os_printf("MTT:%s:%d ::os_memoryAlloc(0x%p, %lu) : %lu\n",__FUNCTION__, __LINE__,OsContext,Size,total_size);
#endif
	/* 
	Memory optimization issue. Allocate up to 2 pages (8k) from the SLAB
	    allocator (2^n), otherwise allocate from virtual pool.
	If full Async mode is used, allow up to 6 pages (24k) for DMA-able
	  memory, so the TxCtrlBlk table can be transacted over DMA.
	*/
#ifdef FULL_ASYNC_MODE
	if (total_size < 6 * 4096)
#else
	if (total_size < 2 * 4096)
#endif
	{
		if (in_atomic())
		{
			blk = kmalloc(total_size, GFP_ATOMIC);
		}
		else
		{
			blk = kmalloc(total_size, GFP_KERNEL);
		}
		if (!blk)
		{
			printk("%s: NULL\n",__func__);	
			return NULL;
		}
		blk->f_free = (os_free)kfree;
	}
	else
	{
		/* We expect that the big allocations should be made outside
		     the interrupt, otherwise fail
		*/
		if (in_interrupt()) {
			printk("%s: NULL\n",__func__);
			return NULL;
		}
	        blk = vmalloc(total_size);
	        if (!blk) {
			printk("%s: NULL\n",__func__);
			return NULL;
		}
		blk->f_free = (os_free)vfree;
	}

	os_profile (OsContext, 4, total_size);

	/*list_add(&blk->blk_list, &drv->mem_blocks);*/
	blk->size = Size;
	blk->signature = MEM_BLOCK_START;
	*(__u32 *)((unsigned char *)blk + total_size - sizeof(__u32)) = MEM_BLOCK_END;
	return (void *)((char *)blk + sizeof(struct os_mem_block));
}


/****************************************************************************************
 *                        os_memoryCAlloc()                                 
 ****************************************************************************************
DESCRIPTION:    Allocates an array in memory with elements initialized to 0.

ARGUMENTS:		OsContext	-	our adapter context.
				Number		-	Number of elements
				Size		-	Length in bytes of each element

RETURN:			None

NOTES:         	
*****************************************************************************************/
void*
os_memoryCAlloc(
        TI_HANDLE OsContext,
        TI_UINT32 Number,
        TI_UINT32 Size
        )
{
	void* pAllocatedMem;
	TI_UINT32 MemSize;

#ifdef TI_MEM_ALLOC_TRACE
	os_printf("MTT:%s:%d ::os_memoryCAlloc(0x%p, %lu, %lu) : %lu\n",__FUNCTION__,__LINE__,OsContext,Number,Size,Number*Size);
#endif
	MemSize = Number * Size;

	pAllocatedMem = os_memoryAlloc(OsContext, MemSize);

	if (!pAllocatedMem)
		return NULL;

	memset(pAllocatedMem,0,MemSize);

	return pAllocatedMem;
}



/****************************************************************************************
 *                        os_memoryFree()                                 
 ****************************************************************************************
DESCRIPTION:    This function releases a block of memory previously allocated with the
				os_memoryAlloc function.


ARGUMENTS:		OsContext	-	our adapter context.
				pMemPtr		-	Pointer to the base virtual address of the allocated memory.
								This address was returned by the os_memoryAlloc function.
				Size		-	Specifies the size, in bytes, of the memory block to be released.
								This parameter must be identical to the Length that was passed to
								os_memoryAlloc.

RETURN:			None

NOTES:         	
*****************************************************************************************/
void
os_memoryFree(
        TI_HANDLE OsContext,
        void* pMemPtr,
        TI_UINT32 Size
        )
{
	struct os_mem_block *blk;

	if (!pMemPtr) {
		printk("%s: NULL\n",__func__); 
		return;
	}
	blk = (struct os_mem_block *)((char *)pMemPtr - sizeof(struct os_mem_block));

#ifdef TI_MEM_ALLOC_TRACE
	os_printf("MTT:%s:%d ::os_memoryFree(0x%p, 0x%p, %lu) : %d\n",__FUNCTION__,__LINE__,OsContext,pMemPtr,Size,-Size);
#endif
	if (blk->signature != MEM_BLOCK_START)
	{
		printk("\n\n%s: memory block signature is incorrect - 0x%x\n\n\n",
			__FUNCTION__, blk->signature);
		return;
	}
	*(char *)(&blk->signature) = '~';
	if (*(__u32 *)((unsigned char *)blk + blk->size + sizeof(struct os_mem_block))
		!= MEM_BLOCK_END)
	{
		printk("\n\n%s: memory block corruption. Size=%u\n\n\n",
			__FUNCTION__, blk->size);
	}

	os_profile (OsContext, 5, blk->size + sizeof(struct os_mem_block) + sizeof(__u32));
	blk->f_free(blk);
}


/****************************************************************************************
 *                        os_memorySet()                                 
 ****************************************************************************************
DESCRIPTION:    This function fills a block of memory with given value.

ARGUMENTS:		OsContext	- our adapter context.
				pMemPtr		- Specifies the base address of a block of memory
				Value		- Specifies the value to set
				Length		- Specifies the size, in bytes, to copy.

RETURN:			None

NOTES:       
*****************************************************************************************/
void
os_memorySet(
    TI_HANDLE OsContext,
    void* pMemPtr,
    TI_INT32 Value,
    TI_UINT32 Length
    )
{
	if (!pMemPtr) {
		printk("%s: NULL\n",__func__);
		return;
	}
	memset(pMemPtr,Value,Length);
}

/****************************************************************************************
 *                        _os_memoryAlloc4HwDma()                                 
 ****************************************************************************************
DESCRIPTION:    Allocates resident (nonpaged) system-space memory for DMA operations.

ARGUMENTS:		OsContext	- our adapter context.
				Size		- Specifies the size, in bytes, to be allocated.

RETURN:			Pointer to the allocated memory.
				NULL if there is insufficient memory available.

NOTES:         		

*****************************************************************************************/
void*
os_memoryAlloc4HwDma(
    TI_HANDLE pOsContext,
    TI_UINT32 Size
    )
{
	struct os_mem_block *blk;
	__u32 total_size = Size + sizeof(struct os_mem_block) + sizeof(__u32);
	/* 
	if the size is greater than 2 pages then we cant allocate the memory
	    through kmalloc so the function fails
	*/
	if (Size < 2 * OS_PAGE_SIZE)
	{
		blk = kmalloc(total_size, GFP_ATOMIC|GFP_DMA);
		if (!blk) {
			printk("%s: NULL\n",__func__);
			return NULL;
		}
		blk->f_free = (os_free)kfree;
	}
	else
	{
		printk("\n\n%s: memory cant be allocated-Size = %d\n\n\n",
			__FUNCTION__, Size);
		return NULL;
	}
	blk->size = Size;
	blk->signature = MEM_BLOCK_START;
	*(__u32 *)((unsigned char *)blk + total_size - sizeof(__u32)) = MEM_BLOCK_END;

	return (void *)((char *)blk + sizeof(struct os_mem_block));
}

/****************************************************************************************
 *                        _os_memory4HwDmaFree()                                 
 ****************************************************************************************
DESCRIPTION:    This function releases a block of memory previously allocated with the
				_os_memoryAlloc4HwDma function.


ARGUMENTS:		OsContext	-	our adapter context.
				pMemPtr		-	Pointer to the base virtual address of the allocated memory.
								This address was returned by the os_memoryAlloc function.
				Size		-	Specifies the size, in bytes, of the memory block to be released.
								This parameter must be identical to the Length that was passed to
								os_memoryAlloc.

RETURN:			None

NOTES:         	
*****************************************************************************************/
void
os_memory4HwDmaFree(
    TI_HANDLE pOsContext,
    void* pMem_ptr,
    TI_UINT32 Size
    )
{
	struct os_mem_block *blk;

	if (!pMem_ptr) {
		printk("%s: NULL\n",__func__);
		return;
	}
	blk = (struct os_mem_block *)((char *)pMem_ptr - sizeof(struct os_mem_block));

	if (blk->signature != MEM_BLOCK_START)
	{
		printk("\n\n%s: memory block signature is incorrect - 0x%x\n\n\n",
			__FUNCTION__, blk->signature);
		return;
	}
	*(char *)(&blk->signature) = '~';
	if (*(__u32 *)((unsigned char *)blk + blk->size + sizeof(struct os_mem_block))
		!= MEM_BLOCK_END)
	{
		printk("\n\n%s: memory block corruption. Size=%u\n\n\n",
			__FUNCTION__, blk->size);
	}

	blk->f_free(blk);
}

/****************************************************************************************
 *                        os_memoryZero()                                 
 ****************************************************************************************
DESCRIPTION:    This function fills a block of memory with 0s.

ARGUMENTS:		OsContext	- our adapter context.
				pMemPtr		- Specifies the base address of a block of memory
				Length		- Specifies how many bytes to fill with 0s.

RETURN:			None

NOTES:  
*****************************************************************************************/
void
os_memoryZero(
    TI_HANDLE OsContext,
    void* pMemPtr,
    TI_UINT32 Length
    )
{
	if (!pMemPtr) {
		printk("%s: NULL\n",__func__);
		return;
	}
	memset(pMemPtr,0,Length);
}


/****************************************************************************************
 *                        os_memoryCopy()                                 
 ****************************************************************************************
DESCRIPTION:    This function copies a specified number of bytes from one caller-supplied
				location to another.

ARGUMENTS:		OsContext	- our adapter context.
				pDstPtr		- Destination buffer
				pSrcPtr		- Source buffer
				Size		- Specifies the size, in bytes, to copy.

RETURN:			None

NOTES:       
*****************************************************************************************/
void
os_memoryCopy(
    TI_HANDLE OsContext,
    void* pDstPtr,
    void* pSrcPtr,
    TI_UINT32 Size
    )
{

	memcpy(pDstPtr,pSrcPtr,Size);
}

/****************************************************************************************
 *                        os_memoryCompare()                                 
 ****************************************************************************************
DESCRIPTION:    Compare characters in two buffers.

ARGUMENTS:		OsContext	- our adapter context.
				Buf1		- First buffer
				Buf2		- Second buffer
				Count		- Number of characters

RETURN:			The return value indicates the relationship between the buffers:
                < 0 Buf1 less than Buf2
                0 Buf1 identical to Buf2
                > 0 Buf1 greater than Buf2

NOTES:         	
*****************************************************************************************/
TI_INT32
os_memoryCompare(
        TI_HANDLE OsContext,
        TI_UINT8* Buf1,
        TI_UINT8* Buf2,
        TI_INT32 Count
        )
{
	return memcmp(Buf1, Buf2, Count);
}




/****************************************************************************************
 *                        os_memoryCopyFromUser()                                 
 ****************************************************************************************
DESCRIPTION:    This function copies a specified number of bytes from one caller-supplied
				location to another. source buffer is in USER-MODE address space

ARGUMENTS:		OsContext	- our adapter context.
				pDstPtr		- Destination buffer
				pSrcPtr		- Source buffer
				Size		- Specifies the size, in bytes, to copy.

RETURN:			None

NOTES:       
*****************************************************************************************/
int
os_memoryCopyFromUser(
    TI_HANDLE OsContext,
    void* pDstPtr,
    void* pSrcPtr,
    TI_UINT32 Size
    )
{
	return copy_from_user(pDstPtr,pSrcPtr,Size);
}

/****************************************************************************************
 *                        os_memoryCopyToUser()                                 
 ****************************************************************************************
DESCRIPTION:    This function copies a specified number of bytes from one caller-supplied
				location to another. desination buffer is in USER-MODE address space

ARGUMENTS:		OsContext	- our adapter context.
				pDstPtr		- Destination buffer
				pSrcPtr		- Source buffer
				Size		- Specifies the size, in bytes, to copy.

RETURN:			None

NOTES:       
*****************************************************************************************/
int 
os_memoryCopyToUser(
    TI_HANDLE OsContext,
    void* pDstPtr,
    void* pSrcPtr,
    TI_UINT32 Size
    )
{
	return copy_to_user(pDstPtr,pSrcPtr,Size);
}