common/dpaax: add library for PA/VA translation table
[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_ERR("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_ERR("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_ERR("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_ERR("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_WARN("More memory nodes available than assumed.");
105                 DPAAX_WARN("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_ERR("Unable to read device-tree memory node: (%d)", ret);
112                 goto cleanup;
113         }
114
115         /* The reg node should be multiple of 16 bytes, 8 bytes each for addr
116          * and len.
117          */
118         *count = (statbuf.st_size / 16);
119         if ((*count) <= 0 || (statbuf.st_size % 16 != 0)) {
120                 DPAAX_ERR("Invalid memory node values or count. (size=%lu)",
121                           statbuf.st_size);
122                 goto cleanup;
123         }
124
125         /* each entry is of 16 bytes, and size/16 is total count of entries */
126         nodes = malloc(sizeof(struct reg_node) * (*count));
127         if (!nodes) {
128                 DPAAX_ERR("Failure in allocating working memory.");
129                 goto cleanup;
130         }
131         memset(nodes, 0, sizeof(struct reg_node) * (*count));
132
133         for (i = 0, j = 0; i < (statbuf.st_size) && j < (*count); i += 16, j++) {
134                 memcpy(&nodes[j], file_data + i, 16);
135                 /* Rotate (ntohl) each 8 byte entry */
136                 rotate_8((unsigned char *)(&(nodes[j].addr)));
137                 rotate_8((unsigned char *)(&(nodes[j].len)));
138         }
139
140         DPAAX_DEBUG("Device-tree memory node data:");
141         do {
142                 DPAAX_DEBUG("\n    %08" PRIx64 " %08zu", nodes[j].addr, nodes[j].len);
143         } while (--j);
144
145 cleanup:
146         close(fd);
147         globfree(&result);
148 out:
149         return nodes;
150 }
151
152 int
153 dpaax_iova_table_populate(void)
154 {
155         int ret;
156         unsigned int i, node_count;
157         size_t tot_memory_size, total_table_size;
158         struct reg_node *nodes;
159         struct dpaax_iovat_element *entry;
160
161         /* dpaax_iova_table_p is a singleton - only one instance should be
162          * created.
163          */
164         if (dpaax_iova_table_p) {
165                 DPAAX_DEBUG("Multiple allocation attempt for IOVA Table (%p)",
166                             dpaax_iova_table_p);
167                 /* This can be an error case as well - some path not cleaning
168                  * up table - but, for now, it is assumed that if IOVA Table
169                  * pointer is valid, table is allocated.
170                  */
171                 return 0;
172         }
173
174         nodes = read_memory_node(&node_count);
175         if (nodes == NULL || node_count <= 0) {
176                 DPAAX_WARN("PA->VA translation not available;");
177                 DPAAX_WARN("Expect performance impact.");
178                 return -1;
179         }
180
181         tot_memory_size = 0;
182         for (i = 0; i < node_count; i++)
183                 tot_memory_size += nodes[i].len;
184
185         DPAAX_DEBUG("Total available PA memory size: %zu", tot_memory_size);
186
187         /* Total table size = meta data + tot_memory_size/8 */
188         total_table_size = sizeof(struct dpaax_iova_table) +
189                            (sizeof(struct dpaax_iovat_element) * node_count) +
190                            ((tot_memory_size / DPAAX_MEM_SPLIT) * sizeof(uint64_t));
191
192         /* TODO: This memory doesn't need to shared but needs to be always
193          * pinned to RAM (no swap out) - using hugepage rather than malloc
194          */
195         dpaax_iova_table_p = rte_zmalloc(NULL, total_table_size, 0);
196         if (dpaax_iova_table_p == NULL) {
197                 DPAAX_WARN("Unable to allocate memory for PA->VA Table;");
198                 DPAAX_WARN("PA->VA translation not available;");
199                 DPAAX_WARN("Expect performance impact.");
200                 free(nodes);
201                 return -1;
202         }
203
204         /* Initialize table */
205         dpaax_iova_table_p->count = node_count;
206         entry = dpaax_iova_table_p->entries;
207
208         DPAAX_DEBUG("IOVA Table entries: (entry start = %p)", (void *)entry);
209         DPAAX_DEBUG("\t(entry),(start),(len),(next)");
210
211         for (i = 0; i < node_count; i++) {
212                 /* dpaax_iova_table_p
213                  * |   dpaax_iova_table_p->entries
214                  * |      |
215                  * |      |
216                  * V      V
217                  * +------+------+-------+---+----------+---------+---
218                  * |iova_ |entry | entry |   | pages    | pages   |
219                  * |table | 1    |  2    |...| entry 1  | entry2  |
220                  * +-----'+.-----+-------+---+;---------+;--------+---
221                  *         \      \          /          /
222                  *          `~~~~~~|~~~~~>pages        /
223                  *                  \                 /
224                  *                   `~~~~~~~~~~~>pages
225                  */
226                 entry[i].start = nodes[i].addr;
227                 entry[i].len = nodes[i].len;
228                 if (i > 0)
229                         entry[i].pages = entry[i-1].pages +
230                                 ((entry[i-1].len/DPAAX_MEM_SPLIT));
231                 else
232                         entry[i].pages = (uint64_t *)((unsigned char *)entry +
233                                          (sizeof(struct dpaax_iovat_element) *
234                                          node_count));
235
236                 DPAAX_DEBUG("\t(%u),(%8"PRIx64"),(%8zu),(%8p)",
237                             i, entry[i].start, entry[i].len, entry[i].pages);
238         }
239
240         /* Release memory associated with nodes array - not required now */
241         free(nodes);
242
243         DPAAX_DEBUG("Adding mem-event handler\n");
244         ret = dpaax_handle_memevents();
245         if (ret) {
246                 DPAAX_ERR("Unable to add mem-event handler");
247                 DPAAX_WARN("Cases with non-buffer pool mem won't work!");
248         }
249
250         return 0;
251 }
252
253 void
254 dpaax_iova_table_depopulate(void)
255 {
256         if (dpaax_iova_table_p == NULL)
257                 return;
258
259         rte_free(dpaax_iova_table_p->entries);
260         dpaax_iova_table_p = NULL;
261
262         DPAAX_DEBUG("IOVA Table cleanedup");
263 }
264
265 int
266 dpaax_iova_table_update(phys_addr_t paddr, void *vaddr, size_t length)
267 {
268         int found = 0;
269         unsigned int i;
270         size_t req_length = length, e_offset;
271         struct dpaax_iovat_element *entry;
272         uintptr_t align_vaddr;
273         phys_addr_t align_paddr;
274
275         align_paddr = paddr & DPAAX_MEM_SPLIT_MASK;
276         align_vaddr = ((uintptr_t)vaddr & DPAAX_MEM_SPLIT_MASK);
277
278         /* Check if paddr is available in table */
279         entry = dpaax_iova_table_p->entries;
280         for (i = 0; i < dpaax_iova_table_p->count; i++) {
281                 if (align_paddr < entry[i].start) {
282                         /* Address lower than start, but not found in previous
283                          * iteration shouldn't exist.
284                          */
285                         DPAAX_ERR("Add: Incorrect entry for PA->VA Table"
286                                   "(%"PRIu64")", paddr);
287                         DPAAX_ERR("Add: Lowest address: %"PRIu64"",
288                                   entry[i].start);
289                         return -1;
290                 }
291
292                 if (align_paddr > (entry[i].start + entry[i].len))
293                         continue;
294
295                 /* align_paddr >= start && align_paddr < (start + len) */
296                 found = 1;
297
298                 do {
299                         e_offset = ((align_paddr - entry[i].start) / DPAAX_MEM_SPLIT);
300                         /* TODO: Whatif something already exists at this
301                          * location - is that an error? For now, ignoring the
302                          * case.
303                          */
304                         entry[i].pages[e_offset] = align_vaddr;
305                         DPAAX_DEBUG("Added: vaddr=%zu for Phy:%"PRIu64" at %zu"
306                                     " remaining len %zu", align_vaddr,
307                                     align_paddr, e_offset, req_length);
308
309                         /* Incoming request can be larger than the
310                          * DPAAX_MEM_SPLIT size - in which case, multiple
311                          * entries in entry->pages[] are filled up.
312                          */
313                         if (req_length <= DPAAX_MEM_SPLIT)
314                                 break;
315                         align_paddr += DPAAX_MEM_SPLIT;
316                         align_vaddr += DPAAX_MEM_SPLIT;
317                         req_length -= DPAAX_MEM_SPLIT;
318                 } while (1);
319
320                 break;
321         }
322
323         if (!found) {
324                 /* There might be case where the incoming physical address is
325                  * beyond the address discovered in the memory node of
326                  * device-tree. Specially if some malloc'd area is used by EAL
327                  * and the memevent handlers passes that across. But, this is
328                  * not necessarily an error.
329                  */
330                 DPAAX_DEBUG("Add: Unable to find slot for vaddr:(%p),"
331                             " phy(%"PRIu64")",
332                             vaddr, paddr);
333                 return -1;
334         }
335
336         DPAAX_DEBUG("Add: Found slot at (%"PRIu64")[(%zu)] for vaddr:(%p),"
337                     " phy(%"PRIu64"), len(%zu)", entry[i].start, e_offset,
338                     vaddr, paddr, length);
339         return 0;
340 }
341
342 /* dpaax_iova_table_dump
343  * Dump the table, with its entries, on screen. Only works in Debug Mode
344  * Not for weak hearted - the tables can get quite large
345  */
346 void
347 dpaax_iova_table_dump(void)
348 {
349         unsigned int i, j;
350         struct dpaax_iovat_element *entry;
351
352         /* In case DEBUG is not enabled, some 'if' conditions might misbehave
353          * as they have nothing else in them  except a DPAAX_DEBUG() which if
354          * tuned out would leave 'if' naked.
355          */
356         if (rte_log_get_global_level() < RTE_LOG_DEBUG) {
357                 DPAAX_ERR("Set log level to Debug for PA->Table dump!");
358                 return;
359         }
360
361         DPAAX_DEBUG(" === Start of PA->VA Translation Table ===");
362         if (dpaax_iova_table_p == NULL)
363                 DPAAX_DEBUG("\tNULL");
364
365         entry = dpaax_iova_table_p->entries;
366         for (i = 0; i < dpaax_iova_table_p->count; i++) {
367                 DPAAX_DEBUG("\t(%16i),(%16"PRIu64"),(%16zu),(%16p)",
368                             i, entry[i].start, entry[i].len, entry[i].pages);
369                 DPAAX_DEBUG("\t\t          (PA),          (VA)");
370                 for (j = 0; j < (entry->len/DPAAX_MEM_SPLIT); j++) {
371                         if (entry[i].pages[j] == 0)
372                                 continue;
373                         DPAAX_DEBUG("\t\t(%16"PRIx64"),(%16"PRIx64")",
374                                     (entry[i].start + (j * sizeof(uint64_t))),
375                                     entry[i].pages[j]);
376                 }
377         }
378         DPAAX_DEBUG(" === End of PA->VA Translation Table ===");
379 }
380
381 static void
382 dpaax_memevent_cb(enum rte_mem_event type, const void *addr, size_t len,
383                   void *arg __rte_unused)
384 {
385         struct rte_memseg_list *msl;
386         struct rte_memseg *ms;
387         size_t cur_len = 0, map_len = 0;
388         phys_addr_t phys_addr;
389         void *virt_addr;
390         int ret;
391
392         DPAAX_DEBUG("Called with addr=%p, len=%zu", addr, len);
393
394         msl = rte_mem_virt2memseg_list(addr);
395
396         while (cur_len < len) {
397                 const void *va = RTE_PTR_ADD(addr, cur_len);
398
399                 ms = rte_mem_virt2memseg(va, msl);
400                 phys_addr = rte_mem_virt2phy(ms->addr);
401                 virt_addr = ms->addr;
402                 map_len = ms->len;
403
404                 DPAAX_DEBUG("Request for %s, va=%p, virt_addr=%p,"
405                             "iova=%"PRIu64", map_len=%zu",
406                             type == RTE_MEM_EVENT_ALLOC ?
407                             "alloc" : "dealloc",
408                             va, virt_addr, phys_addr, map_len);
409
410                 if (type == RTE_MEM_EVENT_ALLOC)
411                         ret = dpaax_iova_table_update(phys_addr, virt_addr,
412                                                       map_len);
413                 else
414                         /* In case of mem_events for MEM_EVENT_FREE, complete
415                          * hugepage is released and its PA entry is set to 0.
416                          */
417                         ret = dpaax_iova_table_update(phys_addr, 0, map_len);
418
419                 if (ret != 0) {
420                         DPAAX_ERR("PA-Table entry update failed. "
421                                   "Map=%d, addr=%p, len=%zu, err:(%d)",
422                                   type, va, map_len, ret);
423                         return;
424                 }
425
426                 cur_len += map_len;
427         }
428 }
429
430 static int
431 dpaax_memevent_walk_memsegs(const struct rte_memseg_list *msl __rte_unused,
432                             const struct rte_memseg *ms, size_t len,
433                             void *arg __rte_unused)
434 {
435         DPAAX_DEBUG("Walking for %p (pa=%"PRIu64") and len %zu",
436                     ms->addr, ms->phys_addr, len);
437         dpaax_iova_table_update(rte_mem_virt2phy(ms->addr), ms->addr, len);
438         return 0;
439 }
440
441 static int
442 dpaax_handle_memevents(void)
443 {
444         /* First, walk through all memsegs and pin them, before installing
445          * handler. This assures that all memseg which have already been
446          * identified/allocated by EAL, are already part of PA->VA Table. This
447          * is especially for cases where application allocates memory before
448          * the EAL or this is an externally allocated memory passed to EAL.
449          */
450         rte_memseg_contig_walk_thread_unsafe(dpaax_memevent_walk_memsegs, NULL);
451
452         return rte_mem_event_callback_register("dpaax_memevents_cb",
453                                                dpaax_memevent_cb, NULL);
454 }
455
456 RTE_INIT(dpaax_log)
457 {
458         dpaax_logger = rte_log_register("pmd.common.dpaax");
459         if (dpaax_logger >= 0)
460                 rte_log_set_level(dpaax_logger, RTE_LOG_NOTICE);
461 }