1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
11 #include <sys/queue.h>
14 #include <rte_errno.h>
15 #include <rte_interrupts.h>
18 #include <rte_per_lcore.h>
19 #include <rte_memory.h>
21 #include <rte_string_fns.h>
22 #include <rte_common.h>
26 static inline const char *
27 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
33 /* empty string is an error though strtoul() returns 0 */
38 val = strtoul(in, &end, 16);
39 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
40 errno = errno ? errno : EINVAL;
48 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
50 const char *in = input;
53 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
56 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
59 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
66 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
68 const char *in = input;
73 val = strtoul(in, &end, 16);
74 if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
76 dev_addr->domain = (uint16_t)val;
78 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
81 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
84 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
91 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
93 return pci_bdf_parse(input, dev_addr);
97 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
99 return pci_dbdf_parse(input, dev_addr);
103 rte_pci_device_name(const struct rte_pci_addr *addr,
104 char *output, size_t size)
106 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
107 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
108 addr->domain, addr->bus,
109 addr->devid, addr->function) >= 0);
113 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
114 const struct rte_pci_addr *addr2)
116 return rte_pci_addr_cmp(addr, addr2);
120 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
121 const struct rte_pci_addr *addr2)
123 uint64_t dev_addr, dev_addr2;
125 if ((addr == NULL) || (addr2 == NULL))
128 dev_addr = ((uint64_t)addr->domain << 24) |
129 (addr->bus << 16) | (addr->devid << 8) | addr->function;
130 dev_addr2 = ((uint64_t)addr2->domain << 24) |
131 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
133 if (dev_addr > dev_addr2)
135 else if (dev_addr < dev_addr2)
142 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
144 if (pci_bdf_parse(str, addr) == 0 ||
145 pci_dbdf_parse(str, addr) == 0)
151 /* map a particular resource from a file */
153 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
154 int additional_flags)
158 /* Map the PCI memory resource of device */
159 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
160 MAP_SHARED | additional_flags, fd, offset);
161 if (mapaddr == MAP_FAILED) {
163 "%s(): cannot mmap(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
164 __func__, fd, requested_addr, size,
165 (unsigned long long)offset,
166 strerror(errno), mapaddr);
168 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
173 /* unmap a particular resource */
175 pci_unmap_resource(void *requested_addr, size_t size)
177 if (requested_addr == NULL)
180 /* Unmap the PCI memory resource of device */
181 if (munmap(requested_addr, size)) {
182 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, %#zx): %s\n",
183 __func__, requested_addr, size,
186 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",