New upstream version 18.11-rc1
[deb_dpdk.git] / drivers / common / dpaax / dpaax_iova_table.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2018 NXP
3  */
4
5 #include <rte_memory.h>
6
7 #include "dpaax_iova_table.h"
8 #include "dpaax_logs.h"
9
10 /* Global dpaax logger identifier */
11 int dpaax_logger;
12
13 /* Global table reference */
14 struct dpaax_iova_table *dpaax_iova_table_p;
15
16 static int dpaax_handle_memevents(void);
17
18 /* A structure representing the device-tree node available in /proc/device-tree.
19  */
20 struct reg_node {
21         phys_addr_t addr;
22         size_t len;
23 };
24
25 /* A ntohll equivalent routine
26  * XXX: This is only applicable for 64 bit environment.
27  */
28 static void
29 rotate_8(unsigned char *arr)
30 {
31         uint32_t temp;
32         uint32_t *first_half;
33         uint32_t *second_half;
34
35         first_half = (uint32_t *)(arr);
36         second_half = (uint32_t *)(arr + 4);
37
38         temp = *first_half;
39         *first_half = *second_half;
40         *second_half = temp;
41
42         *first_half = ntohl(*first_half);
43         *second_half = ntohl(*second_half);
44 }
45
46 /* read_memory_nodes
47  * Memory layout for DPAAx platforms (LS1043, LS1046, LS1088, LS2088, LX2160)
48  * are populated by Uboot and available in device tree:
49  * /proc/device-tree/memory@<address>/reg <= register.
50  * Entries are of the form:
51  *  (<8 byte start addr><8 byte length>)(..more similar blocks of start,len>)..
52  *
53  * @param count
54  *    OUT populate number of entries found in memory node
55  * @return
56  *    Pointer to array of reg_node elements, count size
57  */
58 static struct reg_node *
59 read_memory_node(unsigned int *count)
60 {
61         int fd, ret, i;
62         unsigned int j;
63         glob_t result = {0};
64         struct stat statbuf = {0};
65         char file_data[MEM_NODE_FILE_LEN];
66         struct reg_node *nodes = NULL;
67
68         *count = 0;
69
70         ret = glob(MEM_NODE_PATH_GLOB, 0, NULL, &result);
71         if (ret != 0) {
72                 DPAAX_DEBUG("Unable to glob device-tree memory node: (%s)(%d)",
73                             MEM_NODE_PATH_GLOB, ret);
74                 goto out;
75         }
76
77         if (result.gl_pathc != 1) {
78                 /* Either more than one memory@<addr> node found, or none.
79                  * In either case, cannot work ahead.
80                  */
81                 DPAAX_DEBUG("Found (%zu) entries in device-tree. Not supported!",
82                             result.gl_pathc);
83                 goto out;
84         }
85
86         DPAAX_DEBUG("Opening and parsing device-tree node: (%s)",
87                     result.gl_pathv[0]);
88         fd = open(result.gl_pathv[0], O_RDONLY);
89         if (fd < 0) {
90                 DPAAX_DEBUG("Unable to open the device-tree node: (%s)(fd=%d)",
91                             MEM_NODE_PATH_GLOB, fd);
92                 goto cleanup;
93         }
94
95         /* Stat to get the file size */
96         ret = fstat(fd, &statbuf);
97         if (ret != 0) {
98                 DPAAX_DEBUG("Unable to get device-tree memory node size.");
99                 goto cleanup;
100         }
101
102         DPAAX_DEBUG("Size of device-tree mem node: %lu", statbuf.st_size);
103         if (statbuf.st_size > MEM_NODE_FILE_LEN) {
104                 DPAAX_DEBUG("More memory nodes available than assumed.");
105                 DPAAX_DEBUG("System may not work properly!");
106         }
107
108         ret = read(fd, file_data, statbuf.st_size > MEM_NODE_FILE_LEN ?
109                                   MEM_NODE_FILE_LEN : statbuf.st_size);
110         if (ret <= 0) {
111                 DPAAX_DEBUG("Unable to read device-tree memory node: (%d)",
112                             ret);
113                 goto cleanup;
114         }
115
116         /* The reg node should be multiple of 16 bytes, 8 bytes each for addr
117          * and len.
118          */
119         *count = (statbuf.st_size / 16);
120         if ((*count) <= 0 || (statbuf.st_size % 16 != 0)) {
121                 DPAAX_DEBUG("Invalid memory node values or count. (size=%lu)",
122                             statbuf.st_size);
123                 goto cleanup;
124         }
125
126         /* each entry is of 16 bytes, and size/16 is total count of entries */
127         nodes = malloc(sizeof(struct reg_node) * (*count));
128         if (!nodes) {
129                 DPAAX_DEBUG("Failure in allocating working memory.");
130                 goto cleanup;
131         }
132         memset(nodes, 0, sizeof(struct reg_node) * (*count));
133
134         for (i = 0, j = 0; i < (statbuf.st_size) && j < (*count); i += 16, j++) {
135                 memcpy(&nodes[j], file_data + i, 16);
136                 /* Rotate (ntohl) each 8 byte entry */
137                 rotate_8((unsigned char *)(&(nodes[j].addr)));
138                 rotate_8((unsigned char *)(&(nodes[j].len)));
139         }
140
141         DPAAX_DEBUG("Device-tree memory node data:");
142         do {
143                 DPAAX_DEBUG("\n    %08" PRIx64 " %08zu", nodes[j].addr, nodes[j].len);
144         } while (--j);
145
146 cleanup:
147         close(fd);
148         globfree(&result);
149 out:
150         return nodes;
151 }
152
153 int
154 dpaax_iova_table_populate(void)
155 {
156         int ret;
157         unsigned int i, node_count;
158         size_t tot_memory_size, total_table_size;
159         struct reg_node *nodes;
160         struct dpaax_iovat_element *entry;
161
162         /* dpaax_iova_table_p is a singleton - only one instance should be
163          * created.
164          */
165         if (dpaax_iova_table_p) {
166                 DPAAX_DEBUG("Multiple allocation attempt for IOVA Table (%p)",
167                             dpaax_iova_table_p);
168                 /* This can be an error case as well - some path not cleaning
169                  * up table - but, for now, it is assumed that if IOVA Table
170                  * pointer is valid, table is allocated.
171                  */
172                 return 0;
173         }
174
175         nodes = read_memory_node(&node_count);
176         if (nodes == NULL) {
177                 DPAAX_WARN("PA->VA translation not available;");
178                 DPAAX_WARN("Expect performance impact.");
179                 return -1;
180         }
181
182         tot_memory_size = 0;
183         for (i = 0; i < node_count; i++)
184                 tot_memory_size += nodes[i].len;
185
186         DPAAX_DEBUG("Total available PA memory size: %zu", tot_memory_size);
187
188         /* Total table size = meta data + tot_memory_size/8 */
189         total_table_size = sizeof(struct dpaax_iova_table) +
190                            (sizeof(struct dpaax_iovat_element) * node_count) +
191                            ((tot_memory_size / DPAAX_MEM_SPLIT) * sizeof(uint64_t));
192
193         /* TODO: This memory doesn't need to shared but needs to be always
194          * pinned to RAM (no swap out) - using hugepage rather than malloc
195          */
196         dpaax_iova_table_p = rte_zmalloc(NULL, total_table_size, 0);
197         if (dpaax_iova_table_p == NULL) {
198                 DPAAX_WARN("Unable to allocate memory for PA->VA Table;");
199                 DPAAX_WARN("PA->VA translation not available;");
200                 DPAAX_WARN("Expect performance impact.");
201                 free(nodes);
202                 return -1;
203         }
204
205         /* Initialize table */
206         dpaax_iova_table_p->count = node_count;
207         entry = dpaax_iova_table_p->entries;
208
209         DPAAX_DEBUG("IOVA Table entries: (entry start = %p)", (void *)entry);
210         DPAAX_DEBUG("\t(entry),(start),(len),(next)");
211
212         for (i = 0; i < node_count; i++) {
213                 /* dpaax_iova_table_p
214                  * |   dpaax_iova_table_p->entries
215                  * |      |
216                  * |      |
217                  * V      V
218                  * +------+------+-------+---+----------+---------+---
219                  * |iova_ |entry | entry |   | pages    | pages   |
220                  * |table | 1    |  2    |...| entry 1  | entry2  |
221                  * +-----'+.-----+-------+---+;---------+;--------+---
222                  *         \      \          /          /
223                  *          `~~~~~~|~~~~~>pages        /
224                  *                  \                 /
225                  *                   `~~~~~~~~~~~>pages
226                  */
227                 entry[i].start = nodes[i].addr;
228                 entry[i].len = nodes[i].len;
229                 if (i > 0)
230                         entry[i].pages = entry[i-1].pages +
231                                 ((entry[i-1].len/DPAAX_MEM_SPLIT));
232                 else
233                         entry[i].pages = (uint64_t *)((unsigned char *)entry +
234                                          (sizeof(struct dpaax_iovat_element) *
235                                          node_count));
236
237                 DPAAX_DEBUG("\t(%u),(%8"PRIx64"),(%8zu),(%8p)",
238                             i, entry[i].start, entry[i].len, entry[i].pages);
239         }
240
241         /* Release memory associated with nodes array - not required now */
242         free(nodes);
243
244         DPAAX_DEBUG("Adding mem-event handler\n");
245         ret = dpaax_handle_memevents();
246         if (ret) {
247                 DPAAX_ERR("Unable to add mem-event handler");
248                 DPAAX_WARN("Cases with non-buffer pool mem won't work!");
249         }
250
251         return 0;
252 }
253
254 void
255 dpaax_iova_table_depopulate(void)
256 {
257         if (dpaax_iova_table_p == NULL)
258                 return;
259
260         rte_free(dpaax_iova_table_p->entries);
261         dpaax_iova_table_p = NULL;
262
263         DPAAX_DEBUG("IOVA Table cleanedup");
264 }
265
266 int
267 dpaax_iova_table_update(phys_addr_t paddr, void *vaddr, size_t length)
268 {
269         int found = 0;
270         unsigned int i;
271         size_t req_length = length, e_offset;
272         struct dpaax_iovat_element *entry;
273         uintptr_t align_vaddr;
274         phys_addr_t align_paddr;
275
276         if (unlikely(dpaax_iova_table_p == NULL))
277                 return -1;
278
279         align_paddr = paddr & DPAAX_MEM_SPLIT_MASK;
280         align_vaddr = ((uintptr_t)vaddr & DPAAX_MEM_SPLIT_MASK);
281
282         /* Check if paddr is available in table */
283         entry = dpaax_iova_table_p->entries;
284         for (i = 0; i < dpaax_iova_table_p->count; i++) {
285                 if (align_paddr < entry[i].start) {
286                         /* Address lower than start, but not found in previous
287                          * iteration shouldn't exist.
288                          */
289                         DPAAX_ERR("Add: Incorrect entry for PA->VA Table"
290                                   "(%"PRIu64")", paddr);
291                         DPAAX_ERR("Add: Lowest address: %"PRIu64"",
292                                   entry[i].start);
293                         return -1;
294                 }
295
296                 if (align_paddr > (entry[i].start + entry[i].len))
297                         continue;
298
299                 /* align_paddr >= start && align_paddr < (start + len) */
300                 found = 1;
301
302                 do {
303                         e_offset = ((align_paddr - entry[i].start) / DPAAX_MEM_SPLIT);
304                         /* TODO: Whatif something already exists at this
305                          * location - is that an error? For now, ignoring the
306                          * case.
307                          */
308                         entry[i].pages[e_offset] = align_vaddr;
309                         DPAAX_DEBUG("Added: vaddr=%zu for Phy:%"PRIu64" at %zu"
310                                     " remaining len %zu", align_vaddr,
311                                     align_paddr, e_offset, req_length);
312
313                         /* Incoming request can be larger than the
314                          * DPAAX_MEM_SPLIT size - in which case, multiple
315                          * entries in entry->pages[] are filled up.
316                          */
317                         if (req_length <= DPAAX_MEM_SPLIT)
318                                 break;
319                         align_paddr += DPAAX_MEM_SPLIT;
320                         align_vaddr += DPAAX_MEM_SPLIT;
321                         req_length -= DPAAX_MEM_SPLIT;
322                 } while (1);
323
324                 break;
325         }
326
327         if (!found) {
328                 /* There might be case where the incoming physical address is
329                  * beyond the address discovered in the memory node of
330                  * device-tree. Specially if some malloc'd area is used by EAL
331                  * and the memevent handlers passes that across. But, this is
332                  * not necessarily an error.
333                  */
334                 DPAAX_DEBUG("Add: Unable to find slot for vaddr:(%p),"
335                             " phy(%"PRIu64")",
336                             vaddr, paddr);
337                 return -1;
338         }
339
340         DPAAX_DEBUG("Add: Found slot at (%"PRIu64")[(%zu)] for vaddr:(%p),"
341                     " phy(%"PRIu64"), len(%zu)", entry[i].start, e_offset,
342                     vaddr, paddr, length);
343         return 0;
344 }
345
346 /* dpaax_iova_table_dump
347  * Dump the table, with its entries, on screen. Only works in Debug Mode
348  * Not for weak hearted - the tables can get quite large
349  */
350 void
351 dpaax_iova_table_dump(void)
352 {
353         unsigned int i, j;
354         struct dpaax_iovat_element *entry;
355
356         /* In case DEBUG is not enabled, some 'if' conditions might misbehave
357          * as they have nothing else in them  except a DPAAX_DEBUG() which if
358          * tuned out would leave 'if' naked.
359          */
360         if (rte_log_get_global_level() < RTE_LOG_DEBUG) {
361                 DPAAX_ERR("Set log level to Debug for PA->Table dump!");
362                 return;
363         }
364
365         DPAAX_DEBUG(" === Start of PA->VA Translation Table ===");
366         if (dpaax_iova_table_p == NULL)
367                 DPAAX_DEBUG("\tNULL");
368
369         entry = dpaax_iova_table_p->entries;
370         for (i = 0; i < dpaax_iova_table_p->count; i++) {
371                 DPAAX_DEBUG("\t(%16i),(%16"PRIu64"),(%16zu),(%16p)",
372                             i, entry[i].start, entry[i].len, entry[i].pages);
373                 DPAAX_DEBUG("\t\t          (PA),          (VA)");
374                 for (j = 0; j < (entry->len/DPAAX_MEM_SPLIT); j++) {
375                         if (entry[i].pages[j] == 0)
376                                 continue;
377                         DPAAX_DEBUG("\t\t(%16"PRIx64"),(%16"PRIx64")",
378                                     (entry[i].start + (j * sizeof(uint64_t))),
379                                     entry[i].pages[j]);
380                 }
381         }
382         DPAAX_DEBUG(" === End of PA->VA Translation Table ===");
383 }
384
385 static void
386 dpaax_memevent_cb(enum rte_mem_event type, const void *addr, size_t len,
387                   void *arg __rte_unused)
388 {
389         struct rte_memseg_list *msl;
390         struct rte_memseg *ms;
391         size_t cur_len = 0, map_len = 0;
392         phys_addr_t phys_addr;
393         void *virt_addr;
394         int ret;
395
396         DPAAX_DEBUG("Called with addr=%p, len=%zu", addr, len);
397
398         msl = rte_mem_virt2memseg_list(addr);
399
400         while (cur_len < len) {
401                 const void *va = RTE_PTR_ADD(addr, cur_len);
402
403                 ms = rte_mem_virt2memseg(va, msl);
404                 phys_addr = rte_mem_virt2phy(ms->addr);
405                 virt_addr = ms->addr;
406                 map_len = ms->len;
407
408                 DPAAX_DEBUG("Request for %s, va=%p, virt_addr=%p,"
409                             "iova=%"PRIu64", map_len=%zu",
410                             type == RTE_MEM_EVENT_ALLOC ?
411                             "alloc" : "dealloc",
412                             va, virt_addr, phys_addr, map_len);
413
414                 if (type == RTE_MEM_EVENT_ALLOC)
415                         ret = dpaax_iova_table_update(phys_addr, virt_addr,
416                                                       map_len);
417                 else
418                         /* In case of mem_events for MEM_EVENT_FREE, complete
419                          * hugepage is released and its PA entry is set to 0.
420                          */
421                         ret = dpaax_iova_table_update(phys_addr, 0, map_len);
422
423                 if (ret != 0) {
424                         DPAAX_DEBUG("PA-Table entry update failed. "
425                                     "Map=%d, addr=%p, len=%zu, err:(%d)",
426                                     type, va, map_len, ret);
427                         return;
428                 }
429
430                 cur_len += map_len;
431         }
432 }
433
434 static int
435 dpaax_memevent_walk_memsegs(const struct rte_memseg_list *msl __rte_unused,
436                             const struct rte_memseg *ms, size_t len,
437                             void *arg __rte_unused)
438 {
439         DPAAX_DEBUG("Walking for %p (pa=%"PRIu64") and len %zu",
440                     ms->addr, ms->phys_addr, len);
441         dpaax_iova_table_update(rte_mem_virt2phy(ms->addr), ms->addr, len);
442         return 0;
443 }
444
445 static int
446 dpaax_handle_memevents(void)
447 {
448         /* First, walk through all memsegs and pin them, before installing
449          * handler. This assures that all memseg which have already been
450          * identified/allocated by EAL, are already part of PA->VA Table. This
451          * is especially for cases where application allocates memory before
452          * the EAL or this is an externally allocated memory passed to EAL.
453          */
454         rte_memseg_contig_walk_thread_unsafe(dpaax_memevent_walk_memsegs, NULL);
455
456         return rte_mem_event_callback_register("dpaax_memevents_cb",
457                                                dpaax_memevent_cb, NULL);
458 }
459
460 RTE_INIT(dpaax_log)
461 {
462         dpaax_logger = rte_log_register("pmd.common.dpaax");
463         if (dpaax_logger >= 0)
464                 rte_log_set_level(dpaax_logger, RTE_LOG_ERR);
465 }