diff options
author | Sam Nelson | 2016-09-09 23:15:05 -0500 |
---|---|---|
committer | Sam Nelson | 2016-09-14 13:19:21 -0500 |
commit | 3de2f7d6fb455f504b5958bdb39a8f15584cf441 (patch) | |
tree | f9110a72f8245ac083df91e84a590d2fdb3362e3 | |
parent | 5c1f177b8d5802227dfe7e86fc026620e1e10189 (diff) | |
download | mpm-transport-3de2f7d6fb455f504b5958bdb39a8f15584cf441.tar.gz mpm-transport-3de2f7d6fb455f504b5958bdb39a8f15584cf441.tar.xz mpm-transport-3de2f7d6fb455f504b5958bdb39a8f15584cf441.zip |
mpm_transport: Update to adapt to new changes in dspmem driver
Then updated dspmem driver now removes dev-tree entries and adds sysfs
entries.
Currently using the sysfs entries to find index.
Signed-off-by: Sam Nelson <sam.nelson@ti.com>
-rwxr-xr-x | src/transport/sharedmem/mpm_transport_sharedmem.c | 234 |
1 files changed, 152 insertions, 82 deletions
diff --git a/src/transport/sharedmem/mpm_transport_sharedmem.c b/src/transport/sharedmem/mpm_transport_sharedmem.c index 63d5fb8..a76a200 100755 --- a/src/transport/sharedmem/mpm_transport_sharedmem.c +++ b/src/transport/sharedmem/mpm_transport_sharedmem.c | |||
@@ -44,7 +44,11 @@ | |||
44 | #include "uio_module_drv.h" | 44 | #include "uio_module_drv.h" |
45 | 45 | ||
46 | #define MAX_DEVICE_NAME_LEN 32 | 46 | #define MAX_DEVICE_NAME_LEN 32 |
47 | #define MAX_FILE_NAME_LENGTH 128 | 47 | #define MAX_FILE_NAME_LENGTH 256 |
48 | #define MAX_PARAM_VAL_LENGTH 32 | ||
49 | #define HEXA_DECIMAL_BASE 16 | ||
50 | |||
51 | #define SYSFS_ENTRY_PREFIX /sys/class/misc | ||
48 | 52 | ||
49 | static int | 53 | static int |
50 | mpm_transport_get_mem_details (mpm_transport_cfg_t *sp, | 54 | mpm_transport_get_mem_details (mpm_transport_cfg_t *sp, |
@@ -78,17 +82,41 @@ mpm_transport_get_mem_details (mpm_transport_cfg_t *sp, | |||
78 | laddr, size, sp->name); | 82 | laddr, size, sp->name); |
79 | return -1; | 83 | return -1; |
80 | } | 84 | } |
85 | |||
86 | /* Function finds the mapping index based on the starting address */ | ||
87 | static int mpm_transport_find_map_index(mpm_transport_sharedmem_t *td, int fd_index, uint64_t global_start_addr) { | ||
88 | |||
89 | int k; | ||
90 | |||
91 | for (k = 0; k < td->fd_mem_block.num_mem_blocks[fd_index]; k++) { | ||
92 | if(((uint32_t)global_start_addr >= td->fd_mem_block.mem_block[fd_index][k].base) && | ||
93 | ((uint32_t)global_start_addr < (td->fd_mem_block.mem_block[fd_index][k].base | ||
94 | + td->fd_mem_block.mem_block[fd_index][k].length))) { | ||
95 | #ifdef DEBUG | ||
96 | mpm_printf(1, "DEBUG: Success map_index %d, global_start_addr 0x%x: fd_index %d\n", | ||
97 | k, (uint32_t)global_start_addr, fd_index); | ||
98 | #endif | ||
99 | return k; | ||
100 | } | ||
101 | } | ||
102 | return -1; | ||
103 | |||
104 | } | ||
105 | |||
81 | int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *ocfg) | 106 | int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t *ocfg) |
82 | { | 107 | { |
83 | int i, j, k; | 108 | int i, j, k; |
84 | mpm_transport_sharedmem_t *td; | 109 | mpm_transport_sharedmem_t *td; |
85 | int fd; | 110 | int fd; |
86 | char filename[MAX_FILE_NAME_LENGTH]; | 111 | char filename[MAX_FILE_NAME_LENGTH]; |
112 | char hexstring[MAX_PARAM_VAL_LENGTH]; | ||
87 | off_t fileSize; | 113 | off_t fileSize; |
88 | struct stat statbuf; | 114 | struct stat statbuf; |
89 | char *scratch; | 115 | char *scratch; |
90 | char *devname; | 116 | char *devname; |
91 | int fd_index; | 117 | int fd_index; |
118 | int map_index; | ||
119 | int ret; | ||
92 | 120 | ||
93 | td = calloc(1, sizeof(mpm_transport_sharedmem_t)); | 121 | td = calloc(1, sizeof(mpm_transport_sharedmem_t)); |
94 | if (!td) { | 122 | if (!td) { |
@@ -98,30 +126,22 @@ int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t * | |||
98 | td->fd_mem_block.num_fds = 0; | 126 | td->fd_mem_block.num_fds = 0; |
99 | for (i = 0; i < sp->num_mmap; i++) { | 127 | for (i = 0; i < sp->num_mmap; i++) { |
100 | /* check if fd for same device name is already opened */ | 128 | /* check if fd for same device name is already opened */ |
101 | for (j = 0; j < i; j++) { | 129 | for (j = 0; j < td->fd_mem_block.num_fds; j++) { |
102 | #ifdef DEBUG | 130 | #ifdef DEBUG |
103 | mpm_printf(1, "DEBUG: string compare %s : %s\n", | 131 | mpm_printf(1, "DEBUG: string compare i:%d, j:%d, %s : %s\n", |
104 | sp->mmap[i].devicename, sp->mmap[j].devicename); | 132 | i, j, sp->mmap[i].devicename, sp->mmap[j].devicename); |
105 | #endif | 133 | #endif |
106 | if (!strncmp(sp->mmap[i].devicename, sp->mmap[j].devicename, MAX_DEVICE_NAME_LEN)) { | 134 | if (!strncmp(sp->mmap[i].devicename, sp->mmap[j].devicename, MAX_DEVICE_NAME_LEN)) { |
107 | td->fd_index[i] = td->fd_index[j]; | 135 | td->fd_index[i] = td->fd_index[j]; |
108 | fd_index = td->fd_index[i]; | 136 | fd_index = td->fd_index[i]; |
109 | for (k = 0; k < td->fd_mem_block.num_mem_blocks[fd_index]; k++) { | 137 | map_index = mpm_transport_find_map_index(td, fd_index, sp->mmap[i].global_addr); |
110 | if((sp->mmap[i].global_addr >= td->fd_mem_block.mem_block[fd_index][k].base) && | 138 | if ( map_index < 0 ) { |
111 | (sp->mmap[i].global_addr < td->fd_mem_block.mem_block[fd_index][k].base | 139 | mpm_printf(1, "Could not find map index for address %llx error map_index %d\n", |
112 | + td->fd_mem_block.mem_block[fd_index][k].length)) { | 140 | sp->mmap[i].global_addr, map_index); |
113 | td->map_index[i] = k; | 141 | return MPM_TRANSPORT_SHM_OPEN_ERR_MAP_INDEX; |
114 | #ifdef DEBUG | 142 | |
115 | mpm_printf(1, "DEBUG: Success mmap entry %d: fd_index %d, map_index %d\n", | ||
116 | i, fd_index, k); | ||
117 | #endif | ||
118 | break; | ||
119 | } | ||
120 | } | ||
121 | if(k >= td->fd_mem_block.num_mem_blocks[fd_index]) { | ||
122 | mpm_printf(1, "Could not find map index for address %x\n", | ||
123 | sp->mmap[i].global_addr); | ||
124 | } | 143 | } |
144 | td->map_index[i] = map_index; | ||
125 | break; | 145 | break; |
126 | } | 146 | } |
127 | } | 147 | } |
@@ -137,76 +157,126 @@ int mpm_transport_sharedmem_open(mpm_transport_cfg_t *sp, mpm_transport_open_t * | |||
137 | } | 157 | } |
138 | td->fd_index[i] = td->fd_mem_block.num_fds; | 158 | td->fd_index[i] = td->fd_mem_block.num_fds; |
139 | devname = strrchr(sp->mmap[i].devicename, '/')+1; | 159 | devname = strrchr(sp->mmap[i].devicename, '/')+1; |
140 | /* Check device tree and record the index of the mmap */ | 160 | |
141 | snprintf(filename, MAX_FILE_NAME_LENGTH, "/proc/device-tree/soc/%s/reg", devname); | 161 | k = 0; |
142 | if ((fd = open (filename, O_RDONLY)) == -1) | 162 | /* Check first if there are sysfs entries for the memory */ |
143 | { | 163 | snprintf(filename, MAX_FILE_NAME_LENGTH, |
144 | mpm_printf(1, "Failed to open \"%s\" err=%s\n", | 164 | "SYSFS_ENTRY_PREFIX""/%s/memory%d/addr", devname, k); |
145 | filename, strerror(errno)); | 165 | |
146 | return MPM_TRANSPORT_SHM_OPEN_ERR_MEM_FILE_OPEN; | 166 | if ((fd = open (filename, O_RDONLY)) != -1) { |
147 | } | 167 | |
148 | if (fstat (fd, &statbuf) == -1) | 168 | /* Found sysfs entry : Read memory information */ |
149 | { | 169 | do { |
150 | mpm_printf (1, "Failed to find size of %s (%s)\n", | 170 | ret = read(fd, hexstring, MAX_PARAM_VAL_LENGTH); |
151 | filename, strerror(errno)); | 171 | if ( ret <= 0 ) { |
152 | return MPM_TRANSPORT_SHM_OPEN_ERR_FIND_FILE_SIZE; | 172 | mpm_printf (1, "Failed to read %d bytes from %s: ret %d\n", |
153 | } | 173 | (int) MAX_PARAM_VAL_LENGTH, filename, ret); |
154 | fileSize = statbuf.st_size; | 174 | return MPM_TRANSPORT_SHM_OPEN_ERR_READ; |
155 | td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] = fileSize / sizeof(mem_block_t); | 175 | } |
156 | scratch = malloc (sizeof(mem_block_t) * td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]); | 176 | close(fd); |
157 | if (!td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] || ! scratch) | 177 | /* And populate mem_block base information */ |
158 | { | 178 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base |
159 | mpm_printf (1, "Failed to malloc size blocks for %d\n", td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]); | 179 | = (uint32_t)strtoul(hexstring, NULL, HEXA_DECIMAL_BASE); |
160 | return MPM_TRANSPORT_SHM_OPEN_ERR_MALLOC; | ||
161 | } | ||
162 | if ( read (fd, scratch, fileSize) != fileSize) | ||
163 | { | ||
164 | mpm_printf (1, "Failed to read %d bytes from %s\n", | ||
165 | (int)fileSize, filename); | ||
166 | return MPM_TRANSPORT_SHM_OPEN_ERR_READ; | ||
167 | } | ||
168 | close (fd); | ||
169 | if ( td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] > MPM_MAX_MEM_ENTRIES) { | ||
170 | mpm_printf (1, "Number of blocks %d exceeds max %d\n", | ||
171 | td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds], MPM_MAX_MEM_ENTRIES ); | ||
172 | return MPM_TRANSPORT_SHM_OPEN_ERR_BLOCKS_EXCEED_MAX; | ||
173 | } | ||
174 | for (k = 0; k < td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]; k++) | ||
175 | { | ||
176 | /* Swizzle from big endian in proc */ | ||
177 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base = | ||
178 | (scratch[k*8 + 0] << 24) | | ||
179 | (scratch[k*8 + 1] << 16) | | ||
180 | (scratch[k*8 + 2] << 8) | | ||
181 | (scratch[k*8 + 3] ); | ||
182 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length = | ||
183 | (scratch[k*8 + 4] << 24) | | ||
184 | (scratch[k*8 + 5] << 16) | | ||
185 | (scratch[k*8 + 6] << 8) | | ||
186 | (scratch[k*8 + 7] ); | ||
187 | #ifdef DEBUG | 180 | #ifdef DEBUG |
188 | mpm_printf(1, " DEBUG: Index %d: Base %x, length %x\n", k, td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base, | 181 | mpm_printf(1, "Recording addr 0x%x\n", |
189 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length); | 182 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base); |
190 | #endif | 183 | #endif |
191 | } | 184 | snprintf(filename, MAX_FILE_NAME_LENGTH, |
192 | for (k = 0; k < td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]; k++) | 185 | "SYSFS_ENTRY_PREFIX""/%s/memory%d/size", |
193 | { | 186 | devname, k); |
194 | if((sp->mmap[i].global_addr >= td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base) && | 187 | fd = open(filename, O_RDONLY); |
195 | (sp->mmap[i].global_addr < td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base | 188 | if ( fd == -1 ) { |
196 | + td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length)) { | 189 | mpm_printf(1, "Failed to open \"%s\" err=%s\n", |
197 | td->map_index[i] = k; | 190 | filename, strerror(errno)); |
198 | mpm_printf(1, "Success mmap entry %d: fd_index %d, map_index %d\n", | 191 | return MPM_TRANSPORT_SHM_OPEN_ERR_MEM_FILE_OPEN; |
199 | i,td->fd_mem_block.num_fds, k); | 192 | } |
200 | break; | 193 | |
194 | ret = read(fd, hexstring, MAX_PARAM_VAL_LENGTH); | ||
195 | if ( ret <= 0 ) { | ||
196 | mpm_printf (1, "Failed to read %d bytes from %s: ret %d\n", | ||
197 | (int) MAX_PARAM_VAL_LENGTH, filename, ret); | ||
198 | return MPM_TRANSPORT_SHM_OPEN_ERR_READ; | ||
199 | } | ||
200 | close(fd); | ||
201 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length | ||
202 | = (uint32_t)strtoul(hexstring, NULL, HEXA_DECIMAL_BASE); | ||
203 | #ifdef DEBUG | ||
204 | mpm_printf(1, "Recording length 0x%x\n", | ||
205 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length); | ||
206 | #endif | ||
207 | k++; | ||
208 | snprintf(filename, MAX_FILE_NAME_LENGTH, | ||
209 | "SYSFS_ENTRY_PREFIX""/%s/memory%d/addr", | ||
210 | devname, k); | ||
211 | } while ((fd = open(filename, O_RDONLY)) != -1 ); | ||
212 | } else { | ||
213 | /* Check device tree and record the index of the mmap */ | ||
214 | snprintf(filename, MAX_FILE_NAME_LENGTH, "/proc/device-tree/soc/%s/reg", devname); | ||
215 | |||
216 | if ((fd = open(filename, O_RDONLY)) == -1) | ||
217 | { | ||
218 | mpm_printf(1, "Failed to open \"%s\" err=%s\n", | ||
219 | filename, strerror(errno)); | ||
220 | return MPM_TRANSPORT_SHM_OPEN_ERR_MEM_FILE_OPEN; | ||
221 | } | ||
222 | if (fstat (fd, &statbuf) == -1) | ||
223 | { | ||
224 | mpm_printf (1, "Failed to find size of %s (%s)\n", | ||
225 | filename, strerror(errno)); | ||
226 | return MPM_TRANSPORT_SHM_OPEN_ERR_FIND_FILE_SIZE; | ||
227 | } | ||
228 | fileSize = statbuf.st_size; | ||
229 | td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] = fileSize / sizeof(mem_block_t); | ||
230 | scratch = malloc (sizeof(mem_block_t) * td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]); | ||
231 | if (!td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] || !scratch) | ||
232 | { | ||
233 | mpm_printf (1, "Failed to malloc size blocks for %d\n", | ||
234 | td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]); | ||
235 | return MPM_TRANSPORT_SHM_OPEN_ERR_MALLOC; | ||
236 | } | ||
237 | if ( read (fd, scratch, fileSize) != fileSize) | ||
238 | { | ||
239 | mpm_printf (1, "Failed to read %d bytes from %s\n", | ||
240 | (int)fileSize, filename); | ||
241 | return MPM_TRANSPORT_SHM_OPEN_ERR_READ; | ||
201 | } | 242 | } |
243 | close (fd); | ||
244 | if ( td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds] > MPM_MAX_MEM_ENTRIES) { | ||
245 | mpm_printf (1, "Number of blocks %d exceeds max %d\n", | ||
246 | td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds], MPM_MAX_MEM_ENTRIES ); | ||
247 | return MPM_TRANSPORT_SHM_OPEN_ERR_BLOCKS_EXCEED_MAX; | ||
248 | } | ||
249 | for (k = 0; k < td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]; k++) | ||
250 | { | ||
251 | /* Swizzle from big endian in proc */ | ||
252 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base = | ||
253 | (scratch[k*8 + 0] << 24) | | ||
254 | (scratch[k*8 + 1] << 16) | | ||
255 | (scratch[k*8 + 2] << 8) | | ||
256 | (scratch[k*8 + 3] ); | ||
257 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length = | ||
258 | (scratch[k*8 + 4] << 24) | | ||
259 | (scratch[k*8 + 5] << 16) | | ||
260 | (scratch[k*8 + 6] << 8) | | ||
261 | (scratch[k*8 + 7] ); | ||
262 | #ifdef DEBUG | ||
263 | mpm_printf(1, " DEBUG: Index %d: Base %x, length %x\n", k, | ||
264 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].base, | ||
265 | td->fd_mem_block.mem_block[td->fd_mem_block.num_fds][k].length); | ||
266 | #endif | ||
267 | } | ||
268 | free (scratch); | ||
202 | } | 269 | } |
203 | if(k >= td->fd_mem_block.num_mem_blocks[td->fd_mem_block.num_fds]) { | 270 | /* Now find the matching map section */ |
204 | mpm_printf(1, "Could not find map index for address %x\n", | 271 | map_index = mpm_transport_find_map_index(td, td->fd_mem_block.num_fds, |
205 | sp->mmap[i].global_addr); | 272 | sp->mmap[i].global_addr); |
273 | if ( map_index < 0 ) { | ||
274 | mpm_printf(1, "Could not find map index for address 0x%llx error: map_index %d\n", | ||
275 | sp->mmap[i].global_addr, map_index); | ||
206 | return MPM_TRANSPORT_SHM_OPEN_ERR_MAP_INDEX; | 276 | return MPM_TRANSPORT_SHM_OPEN_ERR_MAP_INDEX; |
207 | } | 277 | } |
208 | 278 | td->map_index[i] = map_index; | |
209 | free (scratch); | 279 | |
210 | td->fd_mem_block.num_fds++; | 280 | td->fd_mem_block.num_fds++; |
211 | if ( td->fd_mem_block.num_fds >= MPM_MAX_NUM_FDS ) { | 281 | if ( td->fd_mem_block.num_fds >= MPM_MAX_NUM_FDS ) { |
212 | mpm_printf (1, "Number of fds %d exceeds max fds %d\n", | 282 | mpm_printf (1, "Number of fds %d exceeds max fds %d\n", |