bus/pci: enable write combining during mapping
[dpdk.git] / drivers / bus / pci / rte_bus_pci.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2015 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5
6 #ifndef _RTE_BUS_PCI_H_
7 #define _RTE_BUS_PCI_H_
8
9 /**
10  * @file
11  *
12  * RTE PCI Bus Interface
13  */
14
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <limits.h>
22 #include <errno.h>
23 #include <sys/queue.h>
24 #include <stdint.h>
25 #include <inttypes.h>
26
27 #include <rte_debug.h>
28 #include <rte_interrupts.h>
29 #include <rte_dev.h>
30 #include <rte_bus.h>
31 #include <rte_pci.h>
32
33 /** Pathname of PCI devices directory. */
34 const char *rte_pci_get_sysfs_path(void);
35
36 /* Forward declarations */
37 struct rte_pci_device;
38 struct rte_pci_driver;
39
40 /** List of PCI devices */
41 TAILQ_HEAD(rte_pci_device_list, rte_pci_device);
42 /** List of PCI drivers */
43 TAILQ_HEAD(rte_pci_driver_list, rte_pci_driver);
44
45 /* PCI Bus iterators */
46 #define FOREACH_DEVICE_ON_PCIBUS(p)     \
47                 TAILQ_FOREACH(p, &(rte_pci_bus.device_list), next)
48
49 #define FOREACH_DRIVER_ON_PCIBUS(p)     \
50                 TAILQ_FOREACH(p, &(rte_pci_bus.driver_list), next)
51
52 struct rte_devargs;
53
54 /**
55  * A structure describing a PCI device.
56  */
57 struct rte_pci_device {
58         TAILQ_ENTRY(rte_pci_device) next;   /**< Next probed PCI device. */
59         struct rte_device device;           /**< Inherit core device */
60         struct rte_pci_addr addr;           /**< PCI location. */
61         struct rte_pci_id id;               /**< PCI ID. */
62         struct rte_mem_resource mem_resource[PCI_MAX_RESOURCE];
63                                             /**< PCI Memory Resource */
64         struct rte_intr_handle intr_handle; /**< Interrupt handle */
65         struct rte_pci_driver *driver;      /**< Associated driver */
66         uint16_t max_vfs;                   /**< sriov enable if not zero */
67         enum rte_kernel_driver kdrv;        /**< Kernel driver passthrough */
68         char name[PCI_PRI_STR_SIZE+1];      /**< PCI location (ASCII) */
69 };
70
71 /**
72  * @internal
73  * Helper macro for drivers that need to convert to struct rte_pci_device.
74  */
75 #define RTE_DEV_TO_PCI(ptr) container_of(ptr, struct rte_pci_device, device)
76
77 #define RTE_DEV_TO_PCI_CONST(ptr) \
78         container_of(ptr, const struct rte_pci_device, device)
79
80 #define RTE_ETH_DEV_TO_PCI(eth_dev)     RTE_DEV_TO_PCI((eth_dev)->device)
81
82 /** Any PCI device identifier (vendor, device, ...) */
83 #define PCI_ANY_ID (0xffff)
84 #define RTE_CLASS_ANY_ID (0xffffff)
85
86 #ifdef __cplusplus
87 /** C++ macro used to help building up tables of device IDs */
88 #define RTE_PCI_DEVICE(vend, dev) \
89         RTE_CLASS_ANY_ID,         \
90         (vend),                   \
91         (dev),                    \
92         PCI_ANY_ID,               \
93         PCI_ANY_ID
94 #else
95 /** Macro used to help building up tables of device IDs */
96 #define RTE_PCI_DEVICE(vend, dev)          \
97         .class_id = RTE_CLASS_ANY_ID,      \
98         .vendor_id = (vend),               \
99         .device_id = (dev),                \
100         .subsystem_vendor_id = PCI_ANY_ID, \
101         .subsystem_device_id = PCI_ANY_ID
102 #endif
103
104 /**
105  * Initialisation function for the driver called during PCI probing.
106  */
107 typedef int (pci_probe_t)(struct rte_pci_driver *, struct rte_pci_device *);
108
109 /**
110  * Uninitialisation function for the driver called during hotplugging.
111  */
112 typedef int (pci_remove_t)(struct rte_pci_device *);
113
114 /**
115  * A structure describing a PCI driver.
116  */
117 struct rte_pci_driver {
118         TAILQ_ENTRY(rte_pci_driver) next;  /**< Next in list. */
119         struct rte_driver driver;          /**< Inherit core driver. */
120         struct rte_pci_bus *bus;           /**< PCI bus reference. */
121         pci_probe_t *probe;                /**< Device Probe function. */
122         pci_remove_t *remove;              /**< Device Remove function. */
123         const struct rte_pci_id *id_table; /**< ID table, NULL terminated. */
124         uint32_t drv_flags;                /**< Flags contolling handling of device. */
125 };
126
127 /**
128  * Structure describing the PCI bus
129  */
130 struct rte_pci_bus {
131         struct rte_bus bus;               /**< Inherit the generic class */
132         struct rte_pci_device_list device_list;  /**< List of PCI devices */
133         struct rte_pci_driver_list driver_list;  /**< List of PCI drivers */
134 };
135
136 /** Device needs PCI BAR mapping (done with either IGB_UIO or VFIO) */
137 #define RTE_PCI_DRV_NEED_MAPPING 0x0001
138 /** Device needs PCI BAR mapping with enabled write combining (wc) */
139 #define RTE_PCI_DRV_WC_ACTIVATE 0x0002
140 /** Device driver supports link state interrupt */
141 #define RTE_PCI_DRV_INTR_LSC    0x0008
142 /** Device driver supports device removal interrupt */
143 #define RTE_PCI_DRV_INTR_RMV 0x0010
144 /** Device driver needs to keep mapped resources if unsupported dev detected */
145 #define RTE_PCI_DRV_KEEP_MAPPED_RES 0x0020
146 /** Device driver supports IOVA as VA */
147 #define RTE_PCI_DRV_IOVA_AS_VA 0X0040
148
149 /**
150  * Map the PCI device resources in user space virtual memory address
151  *
152  * Note that driver should not call this function when flag
153  * RTE_PCI_DRV_NEED_MAPPING is set, as EAL will do that for
154  * you when it's on.
155  *
156  * @param dev
157  *   A pointer to a rte_pci_device structure describing the device
158  *   to use
159  *
160  * @return
161  *   0 on success, negative on error and positive if no driver
162  *   is found for the device.
163  */
164 int rte_pci_map_device(struct rte_pci_device *dev);
165
166 /**
167  * Unmap this device
168  *
169  * @param dev
170  *   A pointer to a rte_pci_device structure describing the device
171  *   to use
172  */
173 void rte_pci_unmap_device(struct rte_pci_device *dev);
174
175 /**
176  * Dump the content of the PCI bus.
177  *
178  * @param f
179  *   A pointer to a file for output
180  */
181 void rte_pci_dump(FILE *f);
182
183 /**
184  * Register a PCI driver.
185  *
186  * @param driver
187  *   A pointer to a rte_pci_driver structure describing the driver
188  *   to be registered.
189  */
190 void rte_pci_register(struct rte_pci_driver *driver);
191
192 /** Helper for PCI device registration from driver (eth, crypto) instance */
193 #define RTE_PMD_REGISTER_PCI(nm, pci_drv) \
194 RTE_INIT(pciinitfn_ ##nm); \
195 static void pciinitfn_ ##nm(void) \
196 {\
197         (pci_drv).driver.name = RTE_STR(nm);\
198         rte_pci_register(&pci_drv); \
199 } \
200 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
201
202 /**
203  * Unregister a PCI driver.
204  *
205  * @param driver
206  *   A pointer to a rte_pci_driver structure describing the driver
207  *   to be unregistered.
208  */
209 void rte_pci_unregister(struct rte_pci_driver *driver);
210
211 /**
212  * Read PCI config space.
213  *
214  * @param device
215  *   A pointer to a rte_pci_device structure describing the device
216  *   to use
217  * @param buf
218  *   A data buffer where the bytes should be read into
219  * @param len
220  *   The length of the data buffer.
221  * @param offset
222  *   The offset into PCI config space
223  */
224 int rte_pci_read_config(const struct rte_pci_device *device,
225                 void *buf, size_t len, off_t offset);
226
227 /**
228  * Write PCI config space.
229  *
230  * @param device
231  *   A pointer to a rte_pci_device structure describing the device
232  *   to use
233  * @param buf
234  *   A data buffer containing the bytes should be written
235  * @param len
236  *   The length of the data buffer.
237  * @param offset
238  *   The offset into PCI config space
239  */
240 int rte_pci_write_config(const struct rte_pci_device *device,
241                 const void *buf, size_t len, off_t offset);
242
243 /**
244  * A structure used to access io resources for a pci device.
245  * rte_pci_ioport is arch, os, driver specific, and should not be used outside
246  * of pci ioport api.
247  */
248 struct rte_pci_ioport {
249         struct rte_pci_device *dev;
250         uint64_t base;
251         uint64_t len; /* only filled for memory mapped ports */
252 };
253
254 /**
255  * Initialize a rte_pci_ioport object for a pci device io resource.
256  *
257  * This object is then used to gain access to those io resources (see below).
258  *
259  * @param dev
260  *   A pointer to a rte_pci_device structure describing the device
261  *   to use.
262  * @param bar
263  *   Index of the io pci resource we want to access.
264  * @param p
265  *   The rte_pci_ioport object to be initialized.
266  * @return
267  *  0 on success, negative on error.
268  */
269 int rte_pci_ioport_map(struct rte_pci_device *dev, int bar,
270                 struct rte_pci_ioport *p);
271
272 /**
273  * Release any resources used in a rte_pci_ioport object.
274  *
275  * @param p
276  *   The rte_pci_ioport object to be uninitialized.
277  * @return
278  *  0 on success, negative on error.
279  */
280 int rte_pci_ioport_unmap(struct rte_pci_ioport *p);
281
282 /**
283  * Read from a io pci resource.
284  *
285  * @param p
286  *   The rte_pci_ioport object from which we want to read.
287  * @param data
288  *   A data buffer where the bytes should be read into
289  * @param len
290  *   The length of the data buffer.
291  * @param offset
292  *   The offset into the pci io resource.
293  */
294 void rte_pci_ioport_read(struct rte_pci_ioport *p,
295                 void *data, size_t len, off_t offset);
296
297 /**
298  * Write to a io pci resource.
299  *
300  * @param p
301  *   The rte_pci_ioport object to which we want to write.
302  * @param data
303  *   A data buffer where the bytes should be read into
304  * @param len
305  *   The length of the data buffer.
306  * @param offset
307  *   The offset into the pci io resource.
308  */
309 void rte_pci_ioport_write(struct rte_pci_ioport *p,
310                 const void *data, size_t len, off_t offset);
311
312 #ifdef __cplusplus
313 }
314 #endif
315
316 #endif /* _RTE_BUS_PCI_H_ */