4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5 * Copyright 2013-2014 6WIND S.A.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
18 * * Neither the name of Intel Corporation nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include <sys/queue.h>
43 #include <rte_errno.h>
44 #include <rte_interrupts.h>
47 #include <rte_per_lcore.h>
48 #include <rte_memory.h>
49 #include <rte_memzone.h>
51 #include <rte_string_fns.h>
52 #include <rte_common.h>
56 static inline const char *
57 get_u8_pciaddr_field(const char *in, void *_u8, char dlm)
64 val = strtoul(in, &end, 16);
65 if (errno != 0 || end[0] != dlm || val > UINT8_MAX) {
66 errno = errno ? errno : EINVAL;
74 pci_bdf_parse(const char *input, struct rte_pci_addr *dev_addr)
76 const char *in = input;
79 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
82 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
85 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
92 pci_dbdf_parse(const char *input, struct rte_pci_addr *dev_addr)
94 const char *in = input;
99 val = strtoul(in, &end, 16);
100 if (errno != 0 || end[0] != ':' || val > UINT16_MAX)
102 dev_addr->domain = (uint16_t)val;
104 in = get_u8_pciaddr_field(in, &dev_addr->bus, ':');
107 in = get_u8_pciaddr_field(in, &dev_addr->devid, '.');
110 in = get_u8_pciaddr_field(in, &dev_addr->function, '\0');
117 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
119 return pci_bdf_parse(input, dev_addr);
123 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
125 return pci_dbdf_parse(input, dev_addr);
129 rte_pci_device_name(const struct rte_pci_addr *addr,
130 char *output, size_t size)
132 RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
133 RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
134 addr->domain, addr->bus,
135 addr->devid, addr->function) >= 0);
139 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
140 const struct rte_pci_addr *addr2)
142 return rte_pci_addr_cmp(addr, addr2);
146 rte_pci_addr_cmp(const struct rte_pci_addr *addr,
147 const struct rte_pci_addr *addr2)
149 uint64_t dev_addr, dev_addr2;
151 if ((addr == NULL) || (addr2 == NULL))
154 dev_addr = ((uint64_t)addr->domain << 24) |
155 (addr->bus << 16) | (addr->devid << 8) | addr->function;
156 dev_addr2 = ((uint64_t)addr2->domain << 24) |
157 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
159 if (dev_addr > dev_addr2)
161 else if (dev_addr < dev_addr2)
168 rte_pci_addr_parse(const char *str, struct rte_pci_addr *addr)
170 if (pci_bdf_parse(str, addr) == 0 ||
171 pci_dbdf_parse(str, addr) == 0)
177 /* map a particular resource from a file */
179 pci_map_resource(void *requested_addr, int fd, off_t offset, size_t size,
180 int additional_flags)
184 /* Map the PCI memory resource of device */
185 mapaddr = mmap(requested_addr, size, PROT_READ | PROT_WRITE,
186 MAP_SHARED | additional_flags, fd, offset);
187 if (mapaddr == MAP_FAILED) {
188 RTE_LOG(ERR, EAL, "%s(): cannot mmap(%d, %p, 0x%lx, 0x%lx): %s (%p)\n",
189 __func__, fd, requested_addr,
190 (unsigned long)size, (unsigned long)offset,
191 strerror(errno), mapaddr);
193 RTE_LOG(DEBUG, EAL, " PCI memory mapped at %p\n", mapaddr);
198 /* unmap a particular resource */
200 pci_unmap_resource(void *requested_addr, size_t size)
202 if (requested_addr == NULL)
205 /* Unmap the PCI memory resource of device */
206 if (munmap(requested_addr, size)) {
207 RTE_LOG(ERR, EAL, "%s(): cannot munmap(%p, 0x%lx): %s\n",
208 __func__, requested_addr, (unsigned long)size,
211 RTE_LOG(DEBUG, EAL, " PCI memory unmapped at %p\n",