mem: introduce memzone freeing
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_ivshmem.c
index 9ed7bad..589019b 100644 (file)
@@ -1,13 +1,13 @@
 /*-
  *   BSD LICENSE
- * 
+ *
  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
  *   All rights reserved.
- * 
+ *
  *   Redistribution and use in source and binary forms, with or without
  *   modification, are permitted provided that the following conditions
  *   are met:
- * 
+ *
  *     * Redistributions of source code must retain the above copyright
  *       notice, this list of conditions and the following disclaimer.
  *     * Redistributions in binary form must reproduce the above copyright
@@ -17,7 +17,7 @@
  *     * Neither the name of Intel Corporation nor the names of its
  *       contributors may be used to endorse or promote products derived
  *       from this software without specific prior written permission.
- * 
+ *
  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
@@ -50,9 +50,9 @@
 #include <rte_errno.h>
 #include <rte_ring.h>
 #include <rte_mempool.h>
+#include <rte_malloc.h>
 #include <rte_common.h>
 #include <rte_ivshmem.h>
-#include <rte_tailq_elem.h>
 
 #include "eal_internal_cfg.h"
 #include "eal_private.h"
@@ -61,7 +61,6 @@
 #define PCI_DEVICE_ID_IVSHMEM 0x1110
 
 #define IVSHMEM_MAGIC 0x0BADC0DE
-#define IVSHMEM_METADATA_SIZE 0x1000
 
 #define IVSHMEM_RESOURCE_PATH "/sys/bus/pci/devices/%04x:%02x:%02x.%x/resource2"
 #define IVSHMEM_CONFIG_PATH "/var/run/.%s_ivshmem_config"
@@ -101,7 +100,7 @@ static int memseg_idx;
 static int pagesz;
 
 /* Tailq heads to add rings to */
-TAILQ_HEAD(rte_ring_list, rte_ring);
+TAILQ_HEAD(rte_ring_list, rte_tailq_entry);
 
 /*
  * Utility functions
@@ -364,7 +363,7 @@ read_metadata(char * path, int path_len, int fd, uint64_t flen)
                                sizeof(struct rte_ivshmem_metadata_entry));
 
                /* copy path */
-               rte_snprintf(ivshmem_config->segment[idx].path, path_len, "%s", path);
+               snprintf(ivshmem_config->segment[idx].path, path_len, "%s", path);
 
                idx++;
        }
@@ -469,7 +468,7 @@ create_shared_config(void)
        int fd;
 
        /* build ivshmem config file path */
-       rte_snprintf(path, sizeof(path), IVSHMEM_CONFIG_PATH,
+       snprintf(path, sizeof(path), IVSHMEM_CONFIG_PATH,
                        internal_config.hugefile_prefix);
 
        fd = open(path, O_CREAT | O_RDWR, 0600);
@@ -520,7 +519,7 @@ open_shared_config(void)
        int fd;
 
        /* build ivshmem config file path */
-       rte_snprintf(path, sizeof(path), IVSHMEM_CONFIG_PATH,
+       snprintf(path, sizeof(path), IVSHMEM_CONFIG_PATH,
                        internal_config.hugefile_prefix);
 
        fd = open(path, O_RDONLY);
@@ -586,7 +585,7 @@ static inline int
 map_all_segments(void)
 {
        struct ivshmem_segment ms_tbl[RTE_MAX_MEMSEG];
-       struct ivshmem_pci_device * pci_dev; 
+       struct ivshmem_pci_device * pci_dev;
        struct rte_mem_config * mcfg;
        struct ivshmem_segment * seg;
        int fd, fd_zero;
@@ -726,15 +725,6 @@ map_all_segments(void)
                 * expect memsegs to be empty */
                memcpy(&mcfg->memseg[i], &ms,
                                sizeof(struct rte_memseg));
-               memcpy(&mcfg->free_memseg[i], &ms,
-                               sizeof(struct rte_memseg));
-
-
-               /* adjust the free_memseg so that there's no free space left */
-               mcfg->free_memseg[i].ioremap_addr += mcfg->free_memseg[i].len;
-               mcfg->free_memseg[i].phys_addr += mcfg->free_memseg[i].len;
-               mcfg->free_memseg[i].addr_64 += mcfg->free_memseg[i].len;
-               mcfg->free_memseg[i].len = 0;
 
                close(fd);
 
@@ -754,6 +744,7 @@ rte_eal_ivshmem_obj_init(void)
        struct ivshmem_segment * seg;
        struct rte_memzone * mz;
        struct rte_ring * r;
+       struct rte_tailq_entry *te;
        unsigned i, ms, idx;
        uint64_t offset;
 
@@ -763,8 +754,8 @@ rte_eal_ivshmem_obj_init(void)
                return 0;
 
        /* check that we have an initialised ring tail queue */
-       if ((ring_list =
-            RTE_TAILQ_LOOKUP_BY_IDX(RTE_TAILQ_RING, rte_ring_list)) == NULL) {
+       ring_list = RTE_TAILQ_LOOKUP(RTE_TAILQ_RING_NAME, rte_ring_list);
+       if (ring_list == NULL) {
                RTE_LOG(ERR, EAL, "No rte_ring tailq found!\n");
                return -1;
        }
@@ -777,12 +768,12 @@ rte_eal_ivshmem_obj_init(void)
                seg = &ivshmem_config->segment[i];
 
                /* add memzone */
-               if (mcfg->memzone_idx == RTE_MAX_MEMZONE) {
+               if (mcfg->memzone_cnt == RTE_MAX_MEMZONE) {
                        RTE_LOG(ERR, EAL, "No more memory zones available!\n");
                        return -1;
                }
 
-               idx = mcfg->memzone_idx;
+               idx = mcfg->memzone_cnt;
 
                RTE_LOG(DEBUG, EAL, "Found memzone: '%s' at %p (len 0x%" PRIx64 ")\n",
                                seg->entry.mz.name, seg->entry.mz.addr, seg->entry.mz.len);
@@ -805,11 +796,13 @@ rte_eal_ivshmem_obj_init(void)
                        }
                }
 
-               mcfg->memzone_idx++;
+               mcfg->memzone_cnt++;
        }
 
+       rte_rwlock_write_lock(RTE_EAL_TAILQ_RWLOCK);
+
        /* find rings */
-       for (i = 0; i < mcfg->memzone_idx; i++) {
+       for (i = 0; i < mcfg->memzone_cnt; i++) {
                mz = &mcfg->memzone[i];
 
                /* check if memzone has a ring prefix */
@@ -819,10 +812,19 @@ rte_eal_ivshmem_obj_init(void)
 
                r = (struct rte_ring*) (mz->addr_64);
 
-               TAILQ_INSERT_TAIL(ring_list, r, next);
+               te = rte_zmalloc("RING_TAILQ_ENTRY", sizeof(*te), 0);
+               if (te == NULL) {
+                       RTE_LOG(ERR, EAL, "Cannot allocate ring tailq entry!\n");
+                       return -1;
+               }
+
+               te->data = (void *) r;
+
+               TAILQ_INSERT_TAIL(ring_list, te, next);
 
                RTE_LOG(DEBUG, EAL, "Found ring: '%s' at %p\n", r->name, mz->addr);
        }
+       rte_rwlock_write_unlock(RTE_EAL_TAILQ_RWLOCK);
 
 #ifdef RTE_LIBRTE_IVSHMEM_DEBUG
        rte_memzone_dump(stdout);
@@ -843,7 +845,7 @@ int rte_eal_ivshmem_init(void)
        /* initialize everything to 0 */
        memset(path, 0, sizeof(path));
        ivshmem_config = NULL;
-       
+
        pagesz = getpagesize();
 
        RTE_LOG(DEBUG, EAL, "Searching for IVSHMEM devices...\n");
@@ -869,7 +871,7 @@ int rte_eal_ivshmem_init(void)
                                        continue;
 
                                /* construct pci device path */
-                               rte_snprintf(path, sizeof(path), IVSHMEM_RESOURCE_PATH,
+                               snprintf(path, sizeof(path), IVSHMEM_RESOURCE_PATH,
                                                dev->addr.domain, dev->addr.bus, dev->addr.devid,
                                                dev->addr.function);
 
@@ -916,9 +918,9 @@ int rte_eal_ivshmem_init(void)
                                                        dev->addr.bus, dev->addr.devid, dev->addr.function);
 
                                        ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].ioremap_addr = res->phys_addr;
-                                       rte_snprintf(ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].path,
+                                       snprintf(ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].path,
                                                        sizeof(ivshmem_config->pci_devs[ivshmem_config->pci_devs_idx].path),
-                                                       path);
+                                                       "%s", path);
 
                                        ivshmem_config->pci_devs_idx++;
                                }