1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation.
3 * Copyright 2013-2014 6WIND S.A.
12 #include <sys/queue.h>
15 #include <rte_errno.h>
16 #include <rte_interrupts.h>
19 #include <rte_per_lcore.h>
20 #include <rte_memory.h>
22 #include <rte_string_fns.h>
23 #include <rte_common.h>
27 static inline const char *
28 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
35 val = strtoul(in, &end, 16);
36 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
37 errno = errno ? errno : EINVAL;
45 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
47 const char *in = input;
50 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
53 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
56 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
63 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
65 const char *in = input;
70 val = strtoul(in, &end, 16);
71 if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
73 dev_addr->domain = (uint16_t)val;
75 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
78 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
81 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
88 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
90 return pci_bdf_parse(input, dev_addr);
94 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
96 return pci_dbdf_parse(input, dev_addr);
100 rte_pci_device_name(const struct rte_pci_addr *addr,
101 char *output, size_t size)
103 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
104 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
105 addr->domain, addr->bus,
106 addr->devid, addr->function) >= 0);
110 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
111 const struct rte_pci_addr *addr2)
113 return rte_pci_addr_cmp(addr, addr2);
117 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
118 const struct rte_pci_addr *addr2)
120 uint64_t dev_addr, dev_addr2;
122 if ((addr == NULL) || (addr2 == NULL))
125 dev_addr = ((uint64_t)addr->domain << 24) |
126 (addr->bus << 16) | (addr->devid << 8) | addr->function;
127 dev_addr2 = ((uint64_t)addr2->domain << 24) |
128 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
130 if (dev_addr > dev_addr2)
132 else if (dev_addr < dev_addr2)
139 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
141 if (pci_bdf_parse(str, addr) == 0 ||
142 pci_dbdf_parse(str, addr) == 0)
148 /* map a particular resource from a file */
150 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
151 int additional_flags)
155 /* Map the PCI memory resource of device */
156 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
157 MAP_SHARED | additional_flags, fd, offset);
158 if (mapaddr == MAP_FAILED) {
159 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
160 __func__, fd, requested_addr,
161 (unsigned long)size, (unsigned long)offset,
162 strerror(errno), mapaddr);
164 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
169 /* unmap a particular resource */
171 pci_unmap_resource(void *requested_addr, size_t size)
173 if (requested_addr == NULL)
176 /* Unmap the PCI memory resource of device */
177 if (munmap(requested_addr, size)) {
178 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
179 __func__, requested_addr, (unsigned long)size,
182 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",