2 /******************************************************************************
3 * FILE netapi_vm.c
4 * PURPOSE: Memory allocator for NETAPI and related utilities
5 * -- using MSMC for descriptors/buffers (current), use CMA (future)
6 ******************************************************************************
7 * FILE NAME: netapi_vm.c
8 *
9 * DESCRIPTION: Memory allocator for netapi
10 * This is only a permanent memory allocator.
11 *
12 * REVISION HISTORY:
13 *
14 * Copyright (c) Texas Instruments Incorporated 2010-2011
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 *
20 * Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 *
23 * Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the
26 * distribution.
27 *
28 * Neither the name of Texas Instruments Incorporated nor the names of
29 * its contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 *
44 */
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <string.h>
49 #include <sys/types.h>
50 #include <sys/stat.h>
51 #include <fcntl.h>
52 #include <sys/mman.h>
53 #include <errno.h>
54 #include <unistd.h>
56 #include <ti/drv/nwal/nwal.h>
57 #include "netapi_vm.h"
61 /***********************RAW MEMORY ALLOCATION & TRANSLATION*************************/
62 /* Macro to align x to y */
63 #define align(x,y) ((x + y) & (~y))
65 uint8_t *netapi_VM_mem_start_phy = (uint8_t*)0;
66 uint8_t *netapi_VM_mem_start = (uint8_t*)0;
68 static uint8_t *netapi_VM_mem_end = (uint8_t*)0;
69 static uint8_t *netapi_VM_mem_alloc_ptr = (uint8_t*)0;
70 static uint32_t netapi_VM_mem_size = 0;
74 /* File descriptor for /dev/mem */
75 static int dev_mem_fd;
77 nwal_Bool_t netapi_VM_memAllocInit
78 (
79 uint8_t *addr, /* Physical address */
80 uint32_t size /* Size of block */
81 )
82 {
83 void *map_base;
85 if((dev_mem_fd = open("/dev/mem", (O_RDWR | O_SYNC))) == -1)
86 {
87 printf(">netapi_VM_memAllocInit: Failed to open \"dev/mem\" err=%s\n",
88 strerror(errno));
89 return nwal_FALSE;
90 }
92 map_base = netapi_VM_memMap ((void *)addr, size);
94 if (!map_base)
95 {
96 printf(">netapi_VM_memAllocInit: Failed to mmap addr (0x%x)", addr);
97 return nwal_FALSE;
98 }
100 printf(">netapi_VM_memAllocInit: Phy Addr %x Memory (%d bytes) mapped at address %p.\n", addr,size, map_base);
102 netapi_VM_mem_alloc_ptr = netapi_VM_mem_start = map_base;
103 netapi_VM_mem_size = size;
104 netapi_VM_mem_end = netapi_VM_mem_start + netapi_VM_mem_size;
105 netapi_VM_mem_start_phy = addr;
106 return nwal_TRUE;
107 }
109 void* netapi_VM_memAlloc
110 (
111 uint32_t size,
112 uint32_t align
113 )
114 {
115 uint32_t key;
116 uint8_t *alloc_ptr;
117 void *p_block =NULL;
119 Osal_stubCsEnter();
120 alloc_ptr = (uint8_t*)align((uint32_t)netapi_VM_mem_alloc_ptr, align);
121 if ((alloc_ptr + size) < netapi_VM_mem_end)
122 {
123 p_block =(void *)alloc_ptr;
124 netapi_VM_mem_alloc_ptr = alloc_ptr + size;
125 Osal_stubCsExit(key);
126 memset (p_block, 0, size);
127 }
128 else
129 {
130 Osal_stubCsExit(key);
131 }
132 return p_block;
133 }\r
134 uint32_t xtraLogs=0;
135 /* Api to map the give physical address to virtual memory space */
136 void *netapi_VM_memMap
137 (
138 void *addr, /* Physical address */
139 uint32_t size /* Size of block */
140 )
141 {
142 void *map_base,*virt_addr,*tmpAddr;
143 uint32_t page_sz;
144 long retval;\r
145 uint32_t mask = (size-1);\r
146 uint32_t offset;
148 retval = sysconf(_SC_PAGE_SIZE);
149 if (retval == -1)
150 {
151 printf(">netapi_VM_memMap: Failed to get page size err=%s\n",
152 strerror(errno));
153 return (void *)0;
154 }
156 page_sz = (uint32_t)retval;
158 if (size%page_sz)
159 {
160 printf(">netapi_VM_memMap: error: block size not aligned to page size\n");
161 return (void *)0;
162 }
164 if ((uint32_t)addr%page_sz)
165 {
166 printf(">netapi_VM_memMap: error: addr not aligned to page size\n");
167 return (void *)0;
168 }
170 map_base = mmap(0, size, (PROT_READ|PROT_WRITE), MAP_SHARED, dev_mem_fd, (off_t)addr & ~mask);
171 if(map_base == (void *) -1)
172 {
173 printf(">netapi_VM_memMap: Failed to mmap \"dev/mem\" err=%s\n",
174 strerror(errno));
175 return (void *)0;
176 }
177 virt_addr = map_base + ((off_t)addr & mask);
178 if(xtraLogs)
179 {
180 printf(">netapi_VM_memMap:Memory mapped Begin Address 0x%x Read Value: 0x%x.\n", virt_addr,*((unsigned long *)virt_addr));
181 // offset = size/(sizeof(unsigned long));
182 // tmpAddr = (unsigned long *)virt_addr + offset-1;
183 tmpAddr = (uint8_t *)virt_addr + 0x6800c;
184 printf("netapi_VM_memMap:Memory mapped End Address 0x%x Read Value: 0x%x.\n", (unsigned long *)tmpAddr ,*((unsigned long *)tmpAddr));
185 *((unsigned long *)tmpAddr) = 0x1234;
186 printf("netapi_VM_memMap:Memory mapped End Address 0x%x Write Value: 0x%x.\n", (unsigned long *)tmpAddr ,*((unsigned long *)tmpAddr));
188 }
189 return(virt_addr);
190 }
192 /***************************************************************/
193 /*************** Memory Initilaization**************************/
194 /***************************************************************/
195 /* for now use msmc */
196 /* Total Permanent memory required in NWAL test
197 * for Packet buffers & descriptor buffers
198 */
199 #define NETAPI_PERM_MEM_SZ (TUNE_NETAPI_PERM_MEM_SZ)
201 /* Physical address map & size for various subsystems */
202 #define QMSS_CFG_BASE_ADDR CSL_QM_SS_CFG_QUE_PEEK_REGS
203 #define QMSS_CFG_BLK_SZ (1*1024*1024)
204 #define QMSS_DATA_BASE_ADDR 0x44020000
205 #define QMSS_DATA_BLK_SZ (0x60000)
206 #define SRIO_CFG_BASE_ADDR CSL_SRIO_CONFIG_REGS
207 #define SRIO_CFG_BLK_SZ (132*1024)
208 #define PASS_CFG_BASE_ADDR CSL_PA_SS_CFG_REGS
209 #define PASS_CFG_BLK_SZ (1*1024*1024)
211 #define MSMC_SRAM_BASE_ADDR CSL_MSMC_SRAM_REGS
213 /* Global variables to hold virtual address of various subsystems */
214 void *netapi_VM_qmssCfgVaddr;
215 void *netapi_VM_qmssDataVaddr;
216 void *netapi_VM_srioCfgVaddr;
217 void *netapi_VM_passCfgVaddr;
219 /* also for our descriptor area */
220 unsigned char *netapi_VM_QMemLocalDescRam=NULL;
221 unsigned char *netapi_VM_QMemGlobalDescRam=NULL;
224 /*************************************************
225 * setup VM memory
226 ************************************************/
227 int netapi_VM_memory_setup(void)
228 {
229 /* (1) big chunck of memory out of MSMC -> todo, get from CMA */
230 if (netapi_VM_memAllocInit((uint8_t*)MSMC_SRAM_BASE_ADDR,
231 NETAPI_PERM_MEM_SZ) == nwal_FALSE) {
232 printf(">netapi ERROR: netapi_V_MmemAllocInit failed\n");
233 return (-1);
234 }
237 /* (2) Create virtual memory maps for peripherals */
238 /* (2a) QMSS CFG Regs */
239 netapi_VM_qmssCfgVaddr = netapi_VM_memMap((void*)QMSS_CFG_BASE_ADDR,
240 QMSS_CFG_BLK_SZ);
241 if (!netapi_VM_qmssCfgVaddr)
242 {
243 printf(">netapi ERROR: Failed to map QMSS CFG registers\n");
244 return (-1);
245 }
246 printf("netapi> QMSS_CFG_BASE_ADDR:0x%x Memory mapped at address %p.\n",(void*)QMSS_CFG_BASE_ADDR, netapi_VM_qmssCfgVaddr);
248 /* (2b) QMSS DATA Regs */
249 netapi_VM_qmssDataVaddr = netapi_VM_memMap((void*)QMSS_DATA_BASE_ADDR,
250 QMSS_DATA_BLK_SZ);
251 if (!netapi_VM_qmssDataVaddr)
252 {
253 printf(">netapi ERROR: Failed to map QMSS DATA registers\n");
254 return (-1);
255 }
256 printf(">netapi QMSS_DATA_BASE_ADDR:0x%x Memory mapped at address %p.\n",(void*)QMSS_DATA_BASE_ADDR, netapi_VM_qmssDataVaddr);
258 /* (2c) SRIO CFG Regs */
259 netapi_VM_srioCfgVaddr = netapi_VM_memMap((void*)SRIO_CFG_BASE_ADDR,
260 SRIO_CFG_BLK_SZ);
261 if (!netapi_VM_srioCfgVaddr)
262 {
263 printf(">netapi ERROR: Failed to map SRIO CFG registers\n");
264 return (-1);
265 }
266 printf(">netapi SRIO_CFG_BASE_ADDR:0x%x Memory mapped at address %p.\n",(void*)SRIO_CFG_BASE_ADDR, netapi_VM_srioCfgVaddr);
268 /* (2d) PASS CFG Regs */
269 netapi_VM_passCfgVaddr = netapi_VM_memMap((void*)PASS_CFG_BASE_ADDR,
270 PASS_CFG_BLK_SZ);
271 if (!netapi_VM_passCfgVaddr)
272 {
273 printf(">netapi ERROR: Failed to map PASS CFG registers\n");
274 return (-1);
275 }
276 printf(">netapi PASS_CFG_BASE_ADDR:0x%x Memory mapped at address %p.\n",(void*)PASS_CFG_BASE_ADDR, netapi_VM_passCfgVaddr);
278 /* (2e) SA ?? */
279 /*to do */
281 /* (2f) Timer */
282 t64_memmap(dev_mem_fd);
284 /* (3) Allocate 2 QM regions from continguous chunk above */
285 netapi_VM_QMemGlobalDescRam = (void *)netapi_VM_memAlloc((TUNE_NETAPI_NUM_GLOBAL_DESC *
286 TUNE_NETAPI_DESC_SIZE),
287 128);
288 netapi_VM_QMemLocalDescRam = (void *)netapi_VM_memAlloc((TUNE_NETAPI_NUM_LOCAL_DESC *
289 TUNE_NETAPI_DESC_SIZE),
290 128);
291 printf(">netapi local desc region=%x global desc region=%x\n", netapi_VM_QMemLocalDescRam,netapi_VM_QMemGlobalDescRam);
293 return 1;
295 }