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)
34 val = strtoul(in, &end, 16);
35 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
36 errno = errno ? errno : EINVAL;
44 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
46 const char *in = input;
49 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
52 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
55 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
62 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
64 const char *in = input;
69 val = strtoul(in, &end, 16);
70 if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
72 dev_addr->domain = (uint16_t)val;
74 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
77 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
80 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
87 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
89 return pci_bdf_parse(input, dev_addr);
93 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
95 return pci_dbdf_parse(input, dev_addr);
99 rte_pci_device_name(const struct rte_pci_addr *addr,
100 char *output, size_t size)
102 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
103 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
104 addr->domain, addr->bus,
105 addr->devid, addr->function) >= 0);
109 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
110 const struct rte_pci_addr *addr2)
112 return rte_pci_addr_cmp(addr, addr2);
116 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
117 const struct rte_pci_addr *addr2)
119 uint64_t dev_addr, dev_addr2;
121 if ((addr == NULL) || (addr2 == NULL))
124 dev_addr = ((uint64_t)addr->domain << 24) |
125 (addr->bus << 16) | (addr->devid << 8) | addr->function;
126 dev_addr2 = ((uint64_t)addr2->domain << 24) |
127 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
129 if (dev_addr > dev_addr2)
131 else if (dev_addr < dev_addr2)
138 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
140 if (pci_bdf_parse(str, addr) == 0 ||
141 pci_dbdf_parse(str, addr) == 0)
147 /* map a particular resource from a file */
149 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
150 int additional_flags)
154 /* Map the PCI memory resource of device */
155 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
156 MAP_SHARED | additional_flags, fd, offset);
157 if (mapaddr == MAP_FAILED) {
159 "%s(): cannot mmap(%d, %p, 0x%zx, 0x%llx): %s (%p)\n",
160 __func__, fd, requested_addr, size,
161 (unsigned long 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, %#zx): %s\n",
179 __func__, requested_addr, size,
182 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",