1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2015 Intel Corporation
14 #include <rte_bus_pci.h>
15 #include <rte_tailq.h>
17 #include <rte_malloc.h>
21 static struct rte_tailq_elem rte_uio_tailq = {
22 .name = "UIO_RESOURCE_LIST",
24 EAL_REGISTER_TAILQ(rte_uio_tailq)
27 pci_uio_map_secondary(struct rte_pci_device *dev)
30 struct mapped_pci_resource *uio_res;
31 struct mapped_pci_res_list *uio_res_list =
32 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
34 TAILQ_FOREACH(uio_res, uio_res_list, next) {
36 /* skip this element if it doesn't match our PCI address */
37 if (rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
40 for (i = 0; i != uio_res->nb_maps; i++) {
42 * open devname, to mmap it
44 fd = open(uio_res->maps[i].path, O_RDWR);
46 RTE_LOG(ERR, EAL, "Cannot open %s: %s\n",
47 uio_res->maps[i].path, strerror(errno));
51 void *mapaddr = pci_map_resource(uio_res->maps[i].addr,
52 fd, (off_t)uio_res->maps[i].offset,
53 (size_t)uio_res->maps[i].size, 0);
55 /* fd is not needed in secondary process, close it */
57 if (mapaddr != uio_res->maps[i].addr) {
59 "Cannot mmap device resource file %s to address: %p\n",
60 uio_res->maps[i].path,
61 uio_res->maps[i].addr);
62 if (mapaddr != NULL) {
63 /* unmap addrs correctly mapped */
64 for (j = 0; j < i; j++)
66 uio_res->maps[j].addr,
67 (size_t)uio_res->maps[j].size);
68 /* unmap addr wrongly mapped */
69 pci_unmap_resource(mapaddr,
70 (size_t)uio_res->maps[i].size);
74 dev->mem_resource[i].addr = mapaddr;
79 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
83 /* map the PCI resource of a PCI device in virtual memory */
85 pci_uio_map_resource(struct rte_pci_device *dev)
87 int i, map_idx = 0, ret;
89 struct mapped_pci_resource *uio_res = NULL;
90 struct mapped_pci_res_list *uio_res_list =
91 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
93 if (rte_intr_fd_set(dev->intr_handle, -1))
96 if (rte_intr_dev_fd_set(dev->intr_handle, -1))
99 /* secondary processes - use already recorded details */
100 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
101 return pci_uio_map_secondary(dev);
103 /* allocate uio resource */
104 ret = pci_uio_alloc_resource(dev, &uio_res);
109 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
111 phaddr = dev->mem_resource[i].phys_addr;
115 ret = pci_uio_map_resource_by_index(dev, i,
123 uio_res->nb_maps = map_idx;
125 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
129 for (i = 0; i < map_idx; i++) {
130 pci_unmap_resource(uio_res->maps[i].addr,
131 (size_t)uio_res->maps[i].size);
132 rte_free(uio_res->maps[i].path);
134 pci_uio_free_resource(dev, uio_res);
139 pci_uio_unmap(struct mapped_pci_resource *uio_res)
146 for (i = 0; i != uio_res->nb_maps; i++) {
147 pci_unmap_resource(uio_res->maps[i].addr,
148 (size_t)uio_res->maps[i].size);
149 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
150 rte_free(uio_res->maps[i].path);
154 /* remap the PCI resource of a PCI device in anonymous virtual memory */
156 pci_uio_remap_resource(struct rte_pci_device *dev)
165 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
167 if (dev->mem_resource[i].phys_addr == 0)
169 map_address = mmap(dev->mem_resource[i].addr,
170 (size_t)dev->mem_resource[i].len,
171 PROT_READ | PROT_WRITE,
172 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
173 if (map_address == MAP_FAILED) {
175 "Cannot remap resource for device %s\n",
180 "Successful remap resource for device %s\n",
187 static struct mapped_pci_resource *
188 pci_uio_find_resource(struct rte_pci_device *dev)
190 struct mapped_pci_resource *uio_res;
191 struct mapped_pci_res_list *uio_res_list =
192 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
197 TAILQ_FOREACH(uio_res, uio_res_list, next) {
199 /* skip this element if it doesn't match our PCI address */
200 if (!rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
206 /* unmap the PCI resource of a PCI device in virtual memory */
208 pci_uio_unmap_resource(struct rte_pci_device *dev)
210 struct mapped_pci_resource *uio_res;
211 struct mapped_pci_res_list *uio_res_list =
212 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
218 /* find an entry for the device */
219 uio_res = pci_uio_find_resource(dev);
223 /* secondary processes - just free maps */
224 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
225 return pci_uio_unmap(uio_res);
227 TAILQ_REMOVE(uio_res_list, uio_res, next);
229 /* unmap all resources */
230 pci_uio_unmap(uio_res);
232 /* free uio resource */
235 /* close fd if in primary process */
236 if (rte_intr_fd_get(dev->intr_handle) >= 0)
237 close(rte_intr_fd_get(dev->intr_handle));
238 uio_cfg_fd = rte_intr_dev_fd_get(dev->intr_handle);
239 if (uio_cfg_fd >= 0) {
241 rte_intr_dev_fd_set(dev->intr_handle, -1);
244 rte_intr_fd_set(dev->intr_handle, -1);
245 rte_intr_type_set(dev->intr_handle, RTE_INTR_HANDLE_UNKNOWN);