pci: get IOMMU class
[dpdk.git] / lib / librte_eal / common / include / rte_pci.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2015 Intel Corporation. All rights reserved.
5  *   Copyright 2013-2014 6WIND S.A.
6  *   All rights reserved.
7  *
8  *   Redistribution and use in source and binary forms, with or without
9  *   modification, are permitted provided that the following conditions
10  *   are met:
11  *
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
17  *       distribution.
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.
21  *
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.
33  */
34
35 #ifndef _RTE_PCI_H_
36 #define _RTE_PCI_H_
37
38 /**
39  * @file
40  *
41  * RTE PCI Interface
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <limits.h>
51 #include <errno.h>
52 #include <sys/queue.h>
53 #include <stdint.h>
54 #include <inttypes.h>
55
56 #include <rte_debug.h>
57 #include <rte_interrupts.h>
58 #include <rte_dev.h>
59 #include <rte_bus.h>
60
61 /** Pathname of PCI devices directory. */
62 const char *pci_get_sysfs_path(void);
63
64 /** Formatting string for PCI device identifier: Ex: 0000:00:01.0 */
65 #define PCI_PRI_FMT "%.4" PRIx16 ":%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
66 #define PCI_PRI_STR_SIZE sizeof("XXXXXXXX:XX:XX.X")
67
68 /** Short formatting string, without domain, for PCI device: Ex: 00:01.0 */
69 #define PCI_SHORT_PRI_FMT "%.2" PRIx8 ":%.2" PRIx8 ".%" PRIx8
70
71 /** Nb. of values in PCI device identifier format string. */
72 #define PCI_FMT_NVAL 4
73
74 /** Nb. of values in PCI resource format. */
75 #define PCI_RESOURCE_FMT_NVAL 3
76
77 /** Maximum number of PCI resources. */
78 #define PCI_MAX_RESOURCE 6
79
80 /* Forward declarations */
81 struct rte_pci_device;
82 struct rte_pci_driver;
83
84 /** List of PCI devices */
85 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
86 /** List of PCI drivers */
87 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
88
89 /* PCI Bus iterators */
90 #define FOREACH_DEVICE_ON_PCIBUS(p)     \
91                 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
92
93 #define FOREACH_DRIVER_ON_PCIBUS(p)     \
94                 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
95
96 /**
97  * A structure describing an ID for a PCI driver. Each driver provides a
98  * table of these IDs for each device that it supports.
99  */
100 struct rte_pci_id {
101         uint32_t class_id;            /**< Class ID (class, subclass, pi) or RTE_CLASS_ANY_ID. */
102         uint16_t vendor_id;           /**< Vendor ID or PCI_ANY_ID. */
103         uint16_t device_id;           /**< Device ID or PCI_ANY_ID. */
104         uint16_t subsystem_vendor_id; /**< Subsystem vendor ID or PCI_ANY_ID. */
105         uint16_t subsystem_device_id; /**< Subsystem device ID or PCI_ANY_ID. */
106 };
107
108 /**
109  * A structure describing the location of a PCI device.
110  */
111 struct rte_pci_addr {
112         uint32_t domain;                /**< Device domain */
113         uint8_t bus;                    /**< Device bus */
114         uint8_t devid;                  /**< Device ID */
115         uint8_t function;               /**< Device function. */
116 };
117
118 struct rte_devargs;
119
120 /**
121  * A structure describing a PCI device.
122  */
123 struct rte_pci_device {
124         TAILQ_ENTRY(rte_pci_device) next;       /**< Next probed PCI device. */
125         struct rte_device device;               /**< Inherit core device */
126         struct rte_pci_addr addr;               /**< PCI location. */
127         struct rte_pci_id id;                   /**< PCI ID. */
128         struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
129                                                 /**< PCI Memory Resource */
130         struct rte_intr_handle intr_handle;     /**< Interrupt handle */
131         struct rte_pci_driver *driver;          /**< Associated driver */
132         uint16_t max_vfs;                       /**< sriov enable if not zero */
133         enum rte_kernel_driver kdrv;            /**< Kernel driver passthrough */
134         char name[PCI_PRI_STR_SIZE+1];          /**< PCI location (ASCII) */
135 };
136
137 /**
138  * @internal
139  * Helper macro for drivers that need to convert to struct rte_pci_device.
140  */
141 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
142
143 /** Any PCI device identifier (vendor, device, ...) */
144 #define PCI_ANY_ID (0xffff)
145 #define RTE_CLASS_ANY_ID (0xffffff)
146
147 #ifdef __cplusplus
148 /** C++ macro used to help building up tables of device IDs */
149 #define RTE_PCI_DEVICE(vend, dev) \
150         RTE_CLASS_ANY_ID,         \
151         (vend),                   \
152         (dev),                    \
153         PCI_ANY_ID,               \
154         PCI_ANY_ID
155 #else
156 /** Macro used to help building up tables of device IDs */
157 #define RTE_PCI_DEVICE(vend, dev)          \
158         .class_id = RTE_CLASS_ANY_ID,      \
159         .vendor_id = (vend),               \
160         .device_id = (dev),                \
161         .subsystem_vendor_id = PCI_ANY_ID, \
162         .subsystem_device_id = PCI_ANY_ID
163 #endif
164
165 /**
166  * Initialisation function for the driver called during PCI probing.
167  */
168 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
169
170 /**
171  * Uninitialisation function for the driver called during hotplugging.
172  */
173 typedef int (pci_remove_t)(struct rte_pci_device *);
174
175 /**
176  * A structure describing a PCI driver.
177  */
178 struct rte_pci_driver {
179         TAILQ_ENTRY(rte_pci_driver) next;       /**< Next in list. */
180         struct rte_driver driver;               /**< Inherit core driver. */
181         struct rte_pci_bus *bus;                /**< PCI bus reference. */
182         pci_probe_t *probe;                     /**< Device Probe function. */
183         pci_remove_t *remove;                   /**< Device Remove function. */
184         const struct rte_pci_id *id_table;      /**< ID table, NULL terminated. */
185         uint32_t drv_flags;                     /**< Flags contolling handling of device. */
186 };
187
188 /**
189  * Structure describing the PCI bus
190  */
191 struct rte_pci_bus {
192         struct rte_bus bus;               /**< Inherit the generic class */
193         struct rte_pci_device_list device_list;  /**< List of PCI devices */
194         struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
195 };
196
197 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
198 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
199 /** Device driver supports link state interrupt */
200 #define RTE_PCI_DRV_INTR_LSC    0x0008
201 /** Device driver supports device removal interrupt */
202 #define RTE_PCI_DRV_INTR_RMV 0x0010
203 /** Device driver needs to keep mapped resources if unsupported dev detected */
204 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
205
206 /**
207  * A structure describing a PCI mapping.
208  */
209 struct pci_map {
210         void *addr;
211         char *path;
212         uint64_t offset;
213         uint64_t size;
214         uint64_t phaddr;
215 };
216
217 /**
218  * A structure describing a mapped PCI resource.
219  * For multi-process we need to reproduce all PCI mappings in secondary
220  * processes, so save them in a tailq.
221  */
222 struct mapped_pci_resource {
223         TAILQ_ENTRY(mapped_pci_resource) next;
224
225         struct rte_pci_addr pci_addr;
226         char path[PATH_MAX];
227         int nb_maps;
228         struct pci_map maps[PCI_MAX_RESOURCE];
229 };
230
231 /** mapped pci device list */
232 TAILQ_HEAD(mapped_pci_res_list, mapped_pci_resource);
233
234 /**< Internal use only - Macro used by pci addr parsing functions **/
235 #define GET_PCIADDR_FIELD(in, fd, lim, dlm)                   \
236 do {                                                               \
237         unsigned long val;                                      \
238         char *end;                                              \
239         errno = 0;                                              \
240         val = strtoul((in), &end, 16);                          \
241         if (errno != 0 || end[0] != (dlm) || val > (lim))       \
242                 return -EINVAL;                                 \
243         (fd) = (typeof (fd))val;                                \
244         (in) = end + 1;                                         \
245 } while(0)
246
247 /**
248  * Utility function to produce a PCI Bus-Device-Function value
249  * given a string representation. Assumes that the BDF is provided without
250  * a domain prefix (i.e. domain returned is always 0)
251  *
252  * @param input
253  *      The input string to be parsed. Should have the format XX:XX.X
254  * @param dev_addr
255  *      The PCI Bus-Device-Function address to be returned. Domain will always be
256  *      returned as 0
257  * @return
258  *  0 on success, negative on error.
259  */
260 static inline int
261 eal_parse_pci_BDF(const char *input, struct rte_pci_addr *dev_addr)
262 {
263         dev_addr->domain = 0;
264         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
265         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
266         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
267         return 0;
268 }
269
270 /**
271  * Utility function to produce a PCI Bus-Device-Function value
272  * given a string representation. Assumes that the BDF is provided including
273  * a domain prefix.
274  *
275  * @param input
276  *      The input string to be parsed. Should have the format XXXX:XX:XX.X
277  * @param dev_addr
278  *      The PCI Bus-Device-Function address to be returned
279  * @return
280  *  0 on success, negative on error.
281  */
282 static inline int
283 eal_parse_pci_DomBDF(const char *input, struct rte_pci_addr *dev_addr)
284 {
285         GET_PCIADDR_FIELD(input, dev_addr->domain, UINT16_MAX, ':');
286         GET_PCIADDR_FIELD(input, dev_addr->bus, UINT8_MAX, ':');
287         GET_PCIADDR_FIELD(input, dev_addr->devid, UINT8_MAX, '.');
288         GET_PCIADDR_FIELD(input, dev_addr->function, UINT8_MAX, 0);
289         return 0;
290 }
291 #undef GET_PCIADDR_FIELD
292
293 /**
294  * Utility function to write a pci device name, this device name can later be
295  * used to retrieve the corresponding rte_pci_addr using eal_parse_pci_*
296  * BDF helpers.
297  *
298  * @param addr
299  *      The PCI Bus-Device-Function address
300  * @param output
301  *      The output buffer string
302  * @param size
303  *      The output buffer size
304  */
305 static inline void
306 rte_pci_device_name(const struct rte_pci_addr *addr,
307                 char *output, size_t size)
308 {
309         RTE_VERIFY(size >= PCI_PRI_STR_SIZE);
310         RTE_VERIFY(snprintf(output, size, PCI_PRI_FMT,
311                             addr->domain, addr->bus,
312                             addr->devid, addr->function) >= 0);
313 }
314
315 /* Compare two PCI device addresses. */
316 /**
317  * Utility function to compare two PCI device addresses.
318  *
319  * @param addr
320  *      The PCI Bus-Device-Function address to compare
321  * @param addr2
322  *      The PCI Bus-Device-Function address to compare
323  * @return
324  *      0 on equal PCI address.
325  *      Positive on addr is greater than addr2.
326  *      Negative on addr is less than addr2, or error.
327  */
328 static inline int
329 rte_eal_compare_pci_addr(const struct rte_pci_addr *addr,
330                          const struct rte_pci_addr *addr2)
331 {
332         uint64_t dev_addr, dev_addr2;
333
334         if ((addr == NULL) || (addr2 == NULL))
335                 return -1;
336
337         dev_addr = ((uint64_t)addr->domain << 24) |
338                 (addr->bus << 16) | (addr->devid << 8) | addr->function;
339         dev_addr2 = ((uint64_t)addr2->domain << 24) |
340                 (addr2->bus << 16) | (addr2->devid << 8) | addr2->function;
341
342         if (dev_addr > dev_addr2)
343                 return 1;
344         else if (dev_addr < dev_addr2)
345                 return -1;
346         else
347                 return 0;
348 }
349
350 /**
351  * Scan the content of the PCI bus, and the devices in the devices
352  * list
353  *
354  * @return
355  *  0 on success, negative on error
356  */
357 int rte_pci_scan(void);
358
359 /**
360  * Probe the PCI bus
361  *
362  * @return
363  *   - 0 on success.
364  *   - !0 on error.
365  */
366 int
367 rte_pci_probe(void);
368
369 /*
370  * Match the PCI Driver and Device using the ID Table
371  *
372  * @param pci_drv
373  *      PCI driver from which ID table would be extracted
374  * @param pci_dev
375  *      PCI device to match against the driver
376  * @return
377  *      1 for successful match
378  *      0 for unsuccessful match
379  */
380 int
381 rte_pci_match(const struct rte_pci_driver *pci_drv,
382               const struct rte_pci_device *pci_dev);
383
384
385 /**
386  * Get iommu class of PCI devices on the bus.
387  * And return their preferred iova mapping mode.
388  *
389  * @return
390  *   - enum rte_iova_mode.
391  */
392 enum rte_iova_mode
393 rte_pci_get_iommu_class(void);
394
395 /**
396  * Map the PCI device resources in user space virtual memory address
397  *
398  * Note that driver should not call this function when flag
399  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
400  * you when it's on.
401  *
402  * @param dev
403  *   A pointer to a rte_pci_device structure describing the device
404  *   to use
405  *
406  * @return
407  *   0 on success, negative on error and positive if no driver
408  *   is found for the device.
409  */
410 int rte_pci_map_device(struct rte_pci_device *dev);
411
412 /**
413  * Unmap this device
414  *
415  * @param dev
416  *   A pointer to a rte_pci_device structure describing the device
417  *   to use
418  */
419 void rte_pci_unmap_device(struct rte_pci_device *dev);
420
421 /**
422  * @internal
423  * Map a particular resource from a file.
424  *
425  * @param requested_addr
426  *      The starting address for the new mapping range.
427  * @param fd
428  *      The file descriptor.
429  * @param offset
430  *      The offset for the mapping range.
431  * @param size
432  *      The size for the mapping range.
433  * @param additional_flags
434  *      The additional flags for the mapping range.
435  * @return
436  *   - On success, the function returns a pointer to the mapped area.
437  *   - On error, the value MAP_FAILED is returned.
438  */
439 void *pci_map_resource(void *requested_addr, int fd, off_t offset,
440                 size_t size, int additional_flags);
441
442 /**
443  * @internal
444  * Unmap a particular resource.
445  *
446  * @param requested_addr
447  *      The address for the unmapping range.
448  * @param size
449  *      The size for the unmapping range.
450  */
451 void pci_unmap_resource(void *requested_addr, size_t size);
452
453 /**
454  * Probe the single PCI device.
455  *
456  * Scan the content of the PCI bus, and find the pci device specified by pci
457  * address, then call the probe() function for registered driver that has a
458  * matching entry in its id_table for discovered device.
459  *
460  * @param addr
461  *      The PCI Bus-Device-Function address to probe.
462  * @return
463  *   - 0 on success.
464  *   - Negative on error.
465  */
466 int rte_pci_probe_one(const struct rte_pci_addr *addr);
467
468 /**
469  * Close the single PCI device.
470  *
471  * Scan the content of the PCI bus, and find the pci device specified by pci
472  * address, then call the remove() function for registered driver that has a
473  * matching entry in its id_table for discovered device.
474  *
475  * @param addr
476  *      The PCI Bus-Device-Function address to close.
477  * @return
478  *   - 0 on success.
479  *   - Negative on error.
480  */
481 int rte_pci_detach(const struct rte_pci_addr *addr);
482
483 /**
484  * Dump the content of the PCI bus.
485  *
486  * @param f
487  *   A pointer to a file for output
488  */
489 void rte_pci_dump(FILE *f);
490
491 /**
492  * Register a PCI driver.
493  *
494  * @param driver
495  *   A pointer to a rte_pci_driver structure describing the driver
496  *   to be registered.
497  */
498 void rte_pci_register(struct rte_pci_driver *driver);
499
500 /** Helper for PCI device registration from driver (eth, crypto) instance */
501 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
502 RTE_INIT(pciinitfn_ ##nm); \
503 static void pciinitfn_ ##nm(void) \
504 {\
505         (pci_drv).driver.name = RTE_STR(nm);\
506         rte_pci_register(&pci_drv); \
507 } \
508 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
509
510 /**
511  * Unregister a PCI driver.
512  *
513  * @param driver
514  *   A pointer to a rte_pci_driver structure describing the driver
515  *   to be unregistered.
516  */
517 void rte_pci_unregister(struct rte_pci_driver *driver);
518
519 /**
520  * Read PCI config space.
521  *
522  * @param device
523  *   A pointer to a rte_pci_device structure describing the device
524  *   to use
525  * @param buf
526  *   A data buffer where the bytes should be read into
527  * @param len
528  *   The length of the data buffer.
529  * @param offset
530  *   The offset into PCI config space
531  */
532 int rte_pci_read_config(const struct rte_pci_device *device,
533                 void *buf, size_t len, off_t offset);
534
535 /**
536  * Write PCI config space.
537  *
538  * @param device
539  *   A pointer to a rte_pci_device structure describing the device
540  *   to use
541  * @param buf
542  *   A data buffer containing the bytes should be written
543  * @param len
544  *   The length of the data buffer.
545  * @param offset
546  *   The offset into PCI config space
547  */
548 int rte_pci_write_config(const struct rte_pci_device *device,
549                 const void *buf, size_t len, off_t offset);
550
551 /**
552  * A structure used to access io resources for a pci device.
553  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
554  * of pci ioport api.
555  */
556 struct rte_pci_ioport {
557         struct rte_pci_device *dev;
558         uint64_t base;
559         uint64_t len; /* only filled for memory mapped ports */
560 };
561
562 /**
563  * Initialize a rte_pci_ioport object for a pci device io resource.
564  *
565  * This object is then used to gain access to those io resources (see below).
566  *
567  * @param dev
568  *   A pointer to a rte_pci_device structure describing the device
569  *   to use.
570  * @param bar
571  *   Index of the io pci resource we want to access.
572  * @param p
573  *   The rte_pci_ioport object to be initialized.
574  * @return
575  *  0 on success, negative on error.
576  */
577 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
578                 struct rte_pci_ioport *p);
579
580 /**
581  * Release any resources used in a rte_pci_ioport object.
582  *
583  * @param p
584  *   The rte_pci_ioport object to be uninitialized.
585  * @return
586  *  0 on success, negative on error.
587  */
588 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
589
590 /**
591  * Read from a io pci resource.
592  *
593  * @param p
594  *   The rte_pci_ioport object from which we want to read.
595  * @param data
596  *   A data buffer where the bytes should be read into
597  * @param len
598  *   The length of the data buffer.
599  * @param offset
600  *   The offset into the pci io resource.
601  */
602 void rte_pci_ioport_read(struct rte_pci_ioport *p,
603                 void *data, size_t len, off_t offset);
604
605 /**
606  * Write to a io pci resource.
607  *
608  * @param p
609  *   The rte_pci_ioport object to which we want to write.
610  * @param data
611  *   A data buffer where the bytes should be read into
612  * @param len
613  *   The length of the data buffer.
614  * @param offset
615  *   The offset into the pci io resource.
616  */
617 void rte_pci_ioport_write(struct rte_pci_ioport *p,
618                 const void *data, size_t len, off_t offset);
619
620 #ifdef __cplusplus
621 }
622 #endif
623
624 #endif /* _RTE_PCI_H_ */