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);
73 dev->mem_resource[i].addr = mapaddr;
78 RTE_LOG(ERR, EAL, "Cannot find resource for device\n");
82 /* map the PCI resource of a PCI device in virtual memory */
84 pci_uio_map_resource(struct rte_pci_device *dev)
86 int i, map_idx = 0, ret;
88 struct mapped_pci_resource *uio_res = NULL;
89 struct mapped_pci_res_list *uio_res_list =
90 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
92 dev->intr_handle.fd = -1;
93 dev->intr_handle.uio_cfg_fd = -1;
95 /* secondary processes - use already recorded details */
96 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
97 return pci_uio_map_secondary(dev);
99 /* allocate uio resource */
100 ret = pci_uio_alloc_resource(dev, &uio_res);
105 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
107 phaddr = dev->mem_resource[i].phys_addr;
111 ret = pci_uio_map_resource_by_index(dev, i,
119 uio_res->nb_maps = map_idx;
121 TAILQ_INSERT_TAIL(uio_res_list, uio_res, next);
125 for (i = 0; i < map_idx; i++) {
126 pci_unmap_resource(uio_res->maps[i].addr,
127 (size_t)uio_res->maps[i].size);
128 rte_free(uio_res->maps[i].path);
130 pci_uio_free_resource(dev, uio_res);
135 pci_uio_unmap(struct mapped_pci_resource *uio_res)
142 for (i = 0; i != uio_res->nb_maps; i++) {
143 pci_unmap_resource(uio_res->maps[i].addr,
144 (size_t)uio_res->maps[i].size);
145 if (rte_eal_process_type() == RTE_PROC_PRIMARY)
146 rte_free(uio_res->maps[i].path);
150 /* remap the PCI resource of a PCI device in anonymous virtual memory */
152 pci_uio_remap_resource(struct rte_pci_device *dev)
161 for (i = 0; i != PCI_MAX_RESOURCE; i++) {
163 if (dev->mem_resource[i].phys_addr == 0)
165 map_address = mmap(dev->mem_resource[i].addr,
166 (size_t)dev->mem_resource[i].len,
167 PROT_READ | PROT_WRITE,
168 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
169 if (map_address == MAP_FAILED) {
171 "Cannot remap resource for device %s\n",
176 "Successful remap resource for device %s\n",
183 static struct mapped_pci_resource *
184 pci_uio_find_resource(struct rte_pci_device *dev)
186 struct mapped_pci_resource *uio_res;
187 struct mapped_pci_res_list *uio_res_list =
188 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
193 TAILQ_FOREACH(uio_res, uio_res_list, next) {
195 /* skip this element if it doesn't match our PCI address */
196 if (!rte_pci_addr_cmp(&uio_res->pci_addr, &dev->addr))
202 /* unmap the PCI resource of a PCI device in virtual memory */
204 pci_uio_unmap_resource(struct rte_pci_device *dev)
206 struct mapped_pci_resource *uio_res;
207 struct mapped_pci_res_list *uio_res_list =
208 RTE_TAILQ_CAST(rte_uio_tailq.head, mapped_pci_res_list);
213 /* find an entry for the device */
214 uio_res = pci_uio_find_resource(dev);
218 /* secondary processes - just free maps */
219 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
220 return pci_uio_unmap(uio_res);
222 TAILQ_REMOVE(uio_res_list, uio_res, next);
224 /* unmap all resources */
225 pci_uio_unmap(uio_res);
227 /* free uio resource */
230 /* close fd if in primary process */
231 close(dev->intr_handle.fd);
232 if (dev->intr_handle.uio_cfg_fd >= 0) {
233 close(dev->intr_handle.uio_cfg_fd);
234 dev->intr_handle.uio_cfg_fd = -1;
237 dev->intr_handle.fd = -1;
238 dev->intr_handle.type = RTE_INTR_HANDLE_UNKNOWN;