1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 // Software and any modification/derivatives thereof.
18 // No right, ownership, or interest to MStar Software and any
19 // modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 // supplied together with third party`s software and the use of MStar
23 // Software may require additional licenses from third parties.
24 // Therefore, you hereby agree it is your sole responsibility to separately
25 // obtain any and all third party right and license necessary for your use of
26 // such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 // MStar`s confidential information and you agree to keep MStar`s
30 // confidential information in strictest confidence and not disclose to any
31 // third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 // kind. Any warranties are hereby expressly disclaimed by MStar, including
35 // without limitation, any warranties of merchantability, non-infringement of
36 // intellectual property rights, fitness for a particular purpose, error free
37 // and in conformity with any international standard. You agree to waive any
38 // claim against MStar for any loss, damage, cost or expense that you may
39 // incur related to your use of MStar Software.
40 // In no event shall MStar be liable for any direct, indirect, incidental or
41 // consequential damages, including without limitation, lost of profit or
42 // revenues, lost or damage of data, and unauthorized system use.
43 // You agree that this Section 4 shall still apply without being affected
44 // even if MStar Software has been modified by MStar in accordance with your
45 // request or instruction for your use, except otherwise agreed by both
46 // parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 // services in relation with MStar Software to you for your use of
50 // MStar Software in conjunction with your or your customer`s product
51 // ("Services").
52 // You understand and agree that, except otherwise agreed by both parties in
53 // writing, Services are provided on an "AS IS" basis and the warranty
54 // disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 // or otherwise:
58 // (a) conferring any license or right to use MStar name, trademark, service
59 // mark, symbol or any other identification;
60 // (b) obligating MStar or any of its affiliates to furnish any person,
61 // including without limitation, you and your customers, any assistance
62 // of any kind whatsoever, or any information; or
63 // (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 // of Taiwan, R.O.C., excluding its conflict of law rules.
67 // Any and all dispute arising out hereof or related hereto shall be finally
68 // settled by arbitration referred to the Chinese Arbitration Association,
69 // Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 // Rules of the Association by three (3) arbitrators appointed in accordance
71 // with the said Rules.
72 // The place of arbitration shall be in Taipei, Taiwan and the language shall
73 // be English.
74 // The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78
79 //=============================================================================
80 //#include <MsCommon.h> // NUSED
81 //#include <cyg/hal/hal_if.h> // NUSED
82 //#include <cyg/hal/hal_arch.h> // NUSED
83 #include "include/drvConfig.h"
84 //#include "include/drvPorts.h" // NUSED
85 #include "include/drvKernel.h"
86 #include "include/drvBitops.h"
87
88 #define NOCACHE_MEMORY_SIZE SZ_128K //SZ_64K, Kaiserin
89 //#define NOCACHE_MEMORY_REGION MEM_REGION_8M
90 //#define NOCACHE_REGION_NUM 3
91 //#define NOCACHE_MIN_ALIGN 32
92 //#define NCMEM_BUFFER_POOLS 4
93
94 //extern int BaseOfFreeMemory; // NUSED
95 //extern int TopOfFreeMemory; // NUSED
96 //extern int BaseOfHeap; // NUSED
97 //extern int TopOfHeap; // NUSED
98
99 //#define NCM_DEBUG
100
101 #ifdef NCM_DEBUG
102 #define NCM_Dbg(x,arg...) diag_printf(x,##arg)
103 #else
104 #define NCM_Dbg(x,...)
105 #endif
106
107 struct ncmem_pool_tag { /* the pool */
108 U32 base_addr;
109 size_t pages_per_pool;
110 U32 *bitmap;
111 };
112 static struct ncmem_pool_tag ncmem_pool;
113 int ncmem_mapsize;
114 U32 ncmem_addr_init;
115 U32 ncmem_addr;
116 U32 ncmem_base;
117
118 /*******************************************************************************
119 * Routine name : ncmem_free_page
120 * returns : none
121 * Created by : Peter Liao
122 * Date created : 2004/01/25
123 * Description : Create a non-cacheable and non-bufferable page memory pool via
124 * CPU memoryt region declaration API. Page momery pool is splited
125 * by the size of page memory. Each page memory is aligned to
126 * page_size bytes.
127 * Notes :
128 *******************************************************************************/
init_cache_memory(void)129 void init_cache_memory(void)
130 {
131 int mapsize;
132 U32 alloc_addr;
133
134
135 //ncmem_addr=(U32)KSEG02KSEG1(Usb_AllocateNonCachedMemory(NOCACHE_MEMORY_SIZE + PAGE_SIZE));
136 alloc_addr = (U32)Usb_AllocateNonCachedMemory(NOCACHE_MEMORY_SIZE + PAGE_SIZE);
137 diag_printf("[NCM]The allocted address is 0x%8lx\n", alloc_addr);
138 ncmem_addr=(U32)KSEG02KSEG1(alloc_addr);
139 ncmem_addr_init = ncmem_addr;
140
141 USB_ASSERT(ncmem_addr, "Allocate ncmem buffer fail\n");
142 ncmem_base = (ncmem_addr + PAGE_SIZE)&(~(PAGE_SIZE-1));
143 NCM_Dbg("The base address is 0x%8lX\n", ncmem_base);
144
145 // Initialize non-cached memory pool information
146 ncmem_pool.pages_per_pool = (NOCACHE_MEMORY_SIZE/PAGE_SIZE);
147 mapsize = ncmem_pool.pages_per_pool;
148 mapsize = (mapsize + BITS_PER_LONG - 1) / BITS_PER_LONG;
149 mapsize *= sizeof (U32);
150 ncmem_mapsize = mapsize;
151 ncmem_pool.base_addr =(U32) KSEG02KSEG1(ncmem_base); // convert cached address to uncached
152 // ncmem_pool.bitmap = (U32 *)LIB_MemoryAllocate(mapsize, MALLOC_CACHED_ADDR);
153 ncmem_pool.bitmap = (U32*)Usb_AllocateNonCachedMemory(mapsize);
154 memset(ncmem_pool.bitmap, ((U32)-1), mapsize);//Bitmap: 0-used 1-unused
155
156 NCM_Dbg("Base addr is 0x%08lX\n",ncmem_pool.base_addr);
157 NCM_Dbg("Top addr is 0x%08lX\n",ncmem_pool.base_addr+NOCACHE_MEMORY_SIZE-1);
158 NCM_Dbg("Pages per pool is %d\n",ncmem_pool.pages_per_pool);
159 NCM_Dbg("Bit map base addr is %p\n",ncmem_pool.bitmap);
160 }
161
162 /*******************************************************************************
163 * Routine name : ncmem_alloc_page
164 * returns : 0 - pointer to allocated virtual address
165 * dma_addr : pointer to allocated physical address
166 * Created by : Peter Liao
167 * Date created : 2004/01/25
168 * Description : It will allocate a new non-cacheable and non-bufferable memory
169 * (size = page_size) from non-cacheable page memory pool for callee
170 * PAGE_SIZE is defined in pci_mem.h file
171 * Notes :
172 *******************************************************************************/
ncmem_alloc_page(dma_addr_t * dma_addr)173 void *ncmem_alloc_page(dma_addr_t *dma_addr)
174 {
175 U32 i;
176 int map, page;
177 size_t offset;
178 void *retval;
179 U32 flags;
180
181 osapi_spin_lock_irqsave (&pool->lock, flags);
182 /* only cachable accesses here ... */
183 for (map = 0, i = 0;
184 i < ncmem_pool.pages_per_pool;
185 i += BITS_PER_LONG, map++)
186 {
187 if (ncmem_pool.bitmap[map] == 0)
188 continue;
189 page = ms_find_1st_zero (~ ncmem_pool.bitmap [map]);
190 if ((i + page) < ncmem_pool.pages_per_pool)
191 {
192 ms_clear_bit ( page, &ncmem_pool.bitmap [map],U32);
193 offset = (BITS_PER_LONG * map) + page;
194 offset *= PAGE_SIZE;
195 goto ready;
196 }
197 }
198
199 NCM_Dbg("ERROR: No enough non-cached memory space !!!\n");
200 retval = 0;
201 goto done;
202
203 ready:
204 retval = (void*) (offset + (size_t) (ncmem_pool.base_addr));
205 *dma_addr = (dma_addr_t)USB_VA2PA((U32)retval); // Convert to physical address for DMA
206 done:
207 osapi_spin_unlock_irqrestore (&pool->lock, flags);
208 NCM_Dbg("The allocated addr is %p, bit_map[%d] is 0x%08lX\n",retval,map,(U32)(ncmem_pool.bitmap[map]));
209 return retval;
210 }
211
212 /*******************************************************************************
213 * Routine name : ncmem_free_page
214 * returns : none
215 * addr : allocated virtual base address
216 * Created by : Peter Liao
217 * Date created : 2004/01/25
218 * Description : It will de-allocate/release the allocated non-cacheable memory
219 * page to non-cacheable page memory pool.
220 * Notes :
221 *******************************************************************************/
ncmem_free_page(U32 addr)222 void ncmem_free_page(U32 addr)
223 {
224 U32 flags;
225 int map, page;
226
227 page = addr - ncmem_pool.base_addr;
228 page /= PAGE_SIZE;
229 map = page / BITS_PER_LONG;
230 page %= BITS_PER_LONG;
231 osapi_spin_lock_irqsave (NULL, flags);
232 ms_set_bit (page, &(ncmem_pool.bitmap[map]), U32);
233 NCM_Dbg("Free Block: bitmap[%d] is 0x%08lX\n",map,(U32)ncmem_pool.bitmap[map]);
234 osapi_spin_unlock_irqrestore (&pool->lock, flags);
235 }
236 /*******************************************************************************
237 * Routine name : ncmem_alloc
238 * returns : 0 - pointer to allocated virtual address
239 * dma_addr : pointer to allocated physical address
240 * Created by : Peter Liao
241 * Date created : 2004/02/10
242 * Description : It will allocate a new non-cacheable and non-bufferable memory
243 * (size = user specific) from non-cacheable page memory pool for callee
244 * Therefore, it may waste up memory space if the size is not mutiply
245 * of page size. Callee should call this function before calling
246 * ncmem_alloc_page because of continuous issue.
247 * Notes :
248 *******************************************************************************/
ncmem_alloc(dma_addr_t * dma_addr,U32 size)249 void *ncmem_alloc(dma_addr_t *dma_addr, U32 size)
250 {
251 U32 flags;
252 U32 i;
253 U32 map, page;
254 U32 num_of_pages;
255 size_t offset;
256 void *retval;
257
258 //If the size is not multiply of PAGE_SIZE, just allocate a PAGE for remaining space
259 if ( size%PAGE_SIZE != 0)
260 size = size/PAGE_SIZE +1;
261 else
262 size = size/PAGE_SIZE;
263 num_of_pages = size/PAGE_SIZE;
264 osapi_spin_lock_irqsave (&pool->lock, flags);
265 /* only cachable accesses here ... */
266 for (map = 0, i = 0;
267 i < ncmem_pool.pages_per_pool;
268 i += BITS_PER_LONG, map++)
269 {
270 if (ncmem_pool.bitmap[map] == 0)
271 continue;
272 page = ms_find_1st_zero (~ ncmem_pool.bitmap [map]);
273 if ((i + page) < ncmem_pool.pages_per_pool)
274 {
275 offset = (BITS_PER_LONG * map) + page;
276 offset *= PAGE_SIZE;
277 goto ready;
278 }
279 }
280 NCM_Dbg("ERROR: No enough non-cached memory space !!!\n");
281 retval = 0;
282 goto done;
283
284 ready:
285 //Allocate enough pages for required memory space
286 for(i=0;(i < num_of_pages);i++)
287 {
288 if ( page >= BITS_PER_LONG )
289 {
290 page = 0;
291 map++;
292 }
293 ms_clear_bit ( page++, &ncmem_pool.bitmap [map],U32);
294 }
295 retval = (void*) (offset + (size_t) (ncmem_pool.base_addr));
296 *dma_addr = (dma_addr_t)USB_VA2PA((U32)retval);
297 done:
298 osapi_spin_unlock_irqrestore (&pool->lock, flags);
299 NCM_Dbg("The allocated addr = %p, size = %ld pages = %ld, end of bitmap[%ld]=0x%08lX\n",retval ,size, num_of_pages, map,(U32)ncmem_pool.bitmap[map]);
300 return retval;
301 }
302
303 /*******************************************************************************
304 * Routine name : ncmem_free
305 * returns : none
306 * addr : allocated virtual base address
307 * size : allocated size
308 * Created by : Peter Liao
309 * Date created : 2004/02/10
310 * Description : It will de-allocate/release the allocated non-cacheable memory
311 * space groupged by pages.
312 * Notes :
313 *******************************************************************************/
ncmem_free(U32 addr,U32 size)314 void ncmem_free(U32 addr,U32 size)
315 {
316 U32 flags;
317 int i;
318 int map, page, num_of_pages;
319
320 if ( size%PAGE_SIZE != 0)
321 size = size/PAGE_SIZE +1;
322 else
323 size = size/PAGE_SIZE;
324 num_of_pages = size/PAGE_SIZE;
325
326 page = ncmem_pool.base_addr - addr;
327 page /= PAGE_SIZE;
328 map = page / BITS_PER_LONG;
329 page %= BITS_PER_LONG;
330 osapi_spin_lock_irqsave (&pool->lock, flags);
331 for(i=0;(i < num_of_pages);i++)
332 {
333 if ( page >= BITS_PER_LONG )
334 {
335 page = 0;
336 map++;
337 }
338 ms_set_bit (page, &(ncmem_pool.bitmap[map]), U32);
339 }
340 NCM_Dbg("Free non-cacheable memory : size = %ld = %d pages, end of bitmap[%d]=0x%08lX\n",size, num_of_pages, map,(U32)ncmem_pool.bitmap[map]);
341 osapi_spin_unlock_irqrestore (&pool->lock, flags);
342 }
343
destroy_NC_mem(void)344 void destroy_NC_mem(void)
345 {
346 #if 0
347 LIB_MemoryFree((void*) ncmem_base);
348 #else
349
350 //MsOS_FreeMemory((void*)CYGARC_CACHED_ADDRESS( ncmem_addr),gs32NonCachedPoolID_MIU0 );
351 //Usb_FreeNonCachedMemory((void*)CYGARC_CACHED_ADDRESS( ncmem_addr));
352 {
353 int i;
354 for (i=0; i<ncmem_mapsize/sizeof(U32); i++)
355 diag_printf("[dNc] bitmap[%2d]: %08x\n", i, ncmem_pool.bitmap[i]);
356 }
357 Usb_FreeNonCachedMemory((void*)( ncmem_pool.bitmap));
358 Usb_FreeNonCachedMemory((void*)( ncmem_addr_init));
359 diag_printf("[USB] destroy NC mem %x\n", ncmem_addr_init);
360
361 #endif
362 }
363
Chip_Flush_Memory(void)364 inline void Chip_Flush_Memory(void)
365 {
366 MsOS_FlushMemory();
367 }
Chip_Read_Memory(void)368 inline void Chip_Read_Memory(void)
369 {
370 MsOS_ReadMemory();
371 }
372
373