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);
54 /* fd is not needed in slave process, close it */
56 if (mapaddr != uio_res->maps[i].addr) {
58 "Cannot mmap device resource file %s to address: %p\n",
59 uio_res->maps[i].path,
60 uio_res->maps[i].addr);
61 if (mapaddr != MAP_FAILED) {
62 /* unmap addrs correctly mapped */
63 for (j = 0; j < i; j++)
65 uio_res->maps[j].addr,
66 (size_t)uio_res->maps[j].size);
67 /* unmap addr wrongly mapped */
68 pci_unmap_resource(mapaddr,
69 (size_t)uio_res->maps[i].size);
77 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
81 /* map the PCI resource of a PCI device in virtual memory */
83 pci_uio_map_resource(struct rte_pci_device *dev)
85 int i, map_idx = 0, ret;
87 struct mapped_pci_resource *uio_res = NULL;
88 struct mapped_pci_res_list *uio_res_list =
89 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
91 dev->intr_handle.fd = -1;
92 dev->intr_handle.uio_cfg_fd = -1;
94 /* secondary processes - use already recorded details */
95 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
96 return pci_uio_map_secondary(dev);
98 /* allocate uio resource */
99 ret = pci_uio_alloc_resource(dev, &uio_res);
104 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
106 phaddr = dev->mem_resource[i].phys_addr;
110 ret = pci_uio_map_resource_by_index(dev, i,
118 uio_res->nb_maps = map_idx;
120 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
124 for (i = 0; i < map_idx; i++) {
125 pci_unmap_resource(uio_res->maps[i].addr,
126 (size_t)uio_res->maps[i].size);
127 rte_free(uio_res->maps[i].path);
129 pci_uio_free_resource(dev, uio_res);
134 pci_uio_unmap(struct mapped_pci_resource *uio_res)
141 for (i = 0; i != uio_res->nb_maps; i++) {
142 pci_unmap_resource(uio_res->maps[i].addr,
143 (size_t)uio_res->maps[i].size);
144 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
145 rte_free(uio_res->maps[i].path);
149 /* remap the PCI resource of a PCI device in anonymous virtual memory */
151 pci_uio_remap_resource(struct rte_pci_device *dev)
160 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
162 if (dev->mem_resource[i].phys_addr == 0)
164 map_address = mmap(dev->mem_resource[i].addr,
165 (size_t)dev->mem_resource[i].len,
166 PROT_READ | PROT_WRITE,
167 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
168 if (map_address == MAP_FAILED) {
170 "Cannot remap resource for device %s\n",
175 "Successful remap resource for device %s\n",
182 static struct mapped_pci_resource *
183 pci_uio_find_resource(struct rte_pci_device *dev)
185 struct mapped_pci_resource *uio_res;
186 struct mapped_pci_res_list *uio_res_list =
187 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
192 TAILQ_FOREACH(uio_res, uio_res_list, next) {
194 /* skip this element if it doesn't match our PCI address */
195 if (!rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
201 /* unmap the PCI resource of a PCI device in virtual memory */
203 pci_uio_unmap_resource(struct rte_pci_device *dev)
205 struct mapped_pci_resource *uio_res;
206 struct mapped_pci_res_list *uio_res_list =
207 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
212 /* find an entry for the device */
213 uio_res = pci_uio_find_resource(dev);
217 /* secondary processes - just free maps */
218 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
219 return pci_uio_unmap(uio_res);
221 TAILQ_REMOVE(uio_res_list, uio_res, next);
223 /* unmap all resources */
224 pci_uio_unmap(uio_res);
226 /* free uio resource */
229 /* close fd if in primary process */
230 close(dev->intr_handle.fd);
231 if (dev->intr_handle.uio_cfg_fd >= 0) {
232 close(dev->intr_handle.uio_cfg_fd);
233 dev->intr_handle.uio_cfg_fd = -1;
236 dev->intr_handle.fd = -1;
237 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;