New upstream version 18.08
[deb_dpdk.git] / lib / librte_eal / common / include / rte_bus.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright 2016 NXP
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of NXP nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef _RTE_BUS_H_
34 #define _RTE_BUS_H_
35
36 /**
37  * @file
38  *
39  * DPDK device bus interface
40  *
41  * This file exposes API and interfaces for bus abstraction
42  * over the devices and drivers in EAL.
43  */
44
45 #ifdef __cplusplus
46 extern "C" {
47 #endif
48
49 #include <stdio.h>
50 #include <sys/queue.h>
51
52 #include <rte_log.h>
53 #include <rte_dev.h>
54
55 /** Double linked list of buses */
56 TAILQ_HEAD(rte_bus_list, rte_bus);
57
58
59 /**
60  * IOVA mapping mode.
61  *
62  * IOVA mapping mode is iommu programming mode of a device.
63  * That device (for example: IOMMU backed DMA device) based
64  * on rte_iova_mode will generate physical or virtual address.
65  *
66  */
67 enum rte_iova_mode {
68         RTE_IOVA_DC = 0,        /* Don't care mode */
69         RTE_IOVA_PA = (1 << 0), /* DMA using physical address */
70         RTE_IOVA_VA = (1 << 1)  /* DMA using virtual address */
71 };
72
73 /**
74  * Bus specific scan for devices attached on the bus.
75  * For each bus object, the scan would be responsible for finding devices and
76  * adding them to its private device list.
77  *
78  * A bus should mandatorily implement this method.
79  *
80  * @return
81  *      0 for successful scan
82  *      <0 for unsuccessful scan with error value
83  */
84 typedef int (*rte_bus_scan_t)(void);
85
86 /**
87  * Implementation specific probe function which is responsible for linking
88  * devices on that bus with applicable drivers.
89  *
90  * This is called while iterating over each registered bus.
91  *
92  * @return
93  *      0 for successful probe
94  *      !0 for any error while probing
95  */
96 typedef int (*rte_bus_probe_t)(void);
97
98 /**
99  * Device iterator to find a device on a bus.
100  *
101  * This function returns an rte_device if one of those held by the bus
102  * matches the data passed as parameter.
103  *
104  * If the comparison function returns zero this function should stop iterating
105  * over any more devices. To continue a search the device of a previous search
106  * can be passed via the start parameter.
107  *
108  * @param cmp
109  *      Comparison function.
110  *
111  * @param data
112  *      Data to compare each device against.
113  *
114  * @param start
115  *      starting point for the iteration
116  *
117  * @return
118  *      The first device matching the data, NULL if none exists.
119  */
120 typedef struct rte_device *
121 (*rte_bus_find_device_t)(const struct rte_device *start, rte_dev_cmp_t cmp,
122                          const void *data);
123
124 /**
125  * Implementation specific probe function which is responsible for linking
126  * devices on that bus with applicable drivers.
127  *
128  * @param dev
129  *      Device pointer that was returned by a previous call to find_device.
130  *
131  * @return
132  *      0 on success.
133  *      !0 on error.
134  */
135 typedef int (*rte_bus_plug_t)(struct rte_device *dev);
136
137 /**
138  * Implementation specific remove function which is responsible for unlinking
139  * devices on that bus from assigned driver.
140  *
141  * @param dev
142  *      Device pointer that was returned by a previous call to find_device.
143  *
144  * @return
145  *      0 on success.
146  *      !0 on error.
147  */
148 typedef int (*rte_bus_unplug_t)(struct rte_device *dev);
149
150 /**
151  * Bus specific parsing function.
152  * Validates the syntax used in the textual representation of a device,
153  * If the syntax is valid and ``addr`` is not NULL, writes the bus-specific
154  * device representation to ``addr``.
155  *
156  * @param[in] name
157  *      device textual description
158  *
159  * @param[out] addr
160  *      device information location address, into which parsed info
161  *      should be written. If NULL, nothing should be written, which
162  *      is not an error.
163  *
164  * @return
165  *      0 if parsing was successful.
166  *      !0 for any error.
167  */
168 typedef int (*rte_bus_parse_t)(const char *name, void *addr);
169
170 /**
171  * Bus scan policies
172  */
173 enum rte_bus_scan_mode {
174         RTE_BUS_SCAN_UNDEFINED,
175         RTE_BUS_SCAN_WHITELIST,
176         RTE_BUS_SCAN_BLACKLIST,
177 };
178
179 /**
180  * A structure used to configure bus operations.
181  */
182 struct rte_bus_conf {
183         enum rte_bus_scan_mode scan_mode; /**< Scan policy. */
184 };
185
186
187 /**
188  * Get common iommu class of the all the devices on the bus. The bus may
189  * check that those devices are attached to iommu driver.
190  * If no devices are attached to the bus. The bus may return with don't care
191  * (_DC) value.
192  * Otherwise, The bus will return appropriate _pa or _va iova mode.
193  *
194  * @return
195  *      enum rte_iova_mode value.
196  */
197 typedef enum rte_iova_mode (*rte_bus_get_iommu_class_t)(void);
198
199
200 /**
201  * A structure describing a generic bus.
202  */
203 struct rte_bus {
204         TAILQ_ENTRY(rte_bus) next;   /**< Next bus object in linked list */
205         const char *name;            /**< Name of the bus */
206         rte_bus_scan_t scan;         /**< Scan for devices attached to bus */
207         rte_bus_probe_t probe;       /**< Probe devices on bus */
208         rte_bus_find_device_t find_device; /**< Find a device on the bus */
209         rte_bus_plug_t plug;         /**< Probe single device for drivers */
210         rte_bus_unplug_t unplug;     /**< Remove single device from driver */
211         rte_bus_parse_t parse;       /**< Parse a device name */
212         struct rte_bus_conf conf;    /**< Bus configuration */
213         rte_bus_get_iommu_class_t get_iommu_class; /**< Get iommu class */
214         rte_dev_iterate_t dev_iterate; /**< Device iterator. */
215 };
216
217 /**
218  * Register a Bus handler.
219  *
220  * @param bus
221  *   A pointer to a rte_bus structure describing the bus
222  *   to be registered.
223  */
224 void rte_bus_register(struct rte_bus *bus);
225
226 /**
227  * Unregister a Bus handler.
228  *
229  * @param bus
230  *   A pointer to a rte_bus structure describing the bus
231  *   to be unregistered.
232  */
233 void rte_bus_unregister(struct rte_bus *bus);
234
235 /**
236  * Scan all the buses.
237  *
238  * @return
239  *   0 in case of success in scanning all buses
240  *  !0 in case of failure to scan
241  */
242 int rte_bus_scan(void);
243
244 /**
245  * For each device on the buses, perform a driver 'match' and call the
246  * driver-specific probe for device initialization.
247  *
248  * @return
249  *       0 for successful match/probe
250  *      !0 otherwise
251  */
252 int rte_bus_probe(void);
253
254 /**
255  * Dump information of all the buses registered with EAL.
256  *
257  * @param f
258  *       A valid and open output stream handle
259  */
260 void rte_bus_dump(FILE *f);
261
262 /**
263  * Bus comparison function.
264  *
265  * @param bus
266  *      Bus under test.
267  *
268  * @param data
269  *      Data to compare against.
270  *
271  * @return
272  *      0 if the bus matches the data.
273  *      !0 if the bus does not match.
274  *      <0 if ordering is possible and the bus is lower than the data.
275  *      >0 if ordering is possible and the bus is greater than the data.
276  */
277 typedef int (*rte_bus_cmp_t)(const struct rte_bus *bus, const void *data);
278
279 /**
280  * Bus iterator to find a particular bus.
281  *
282  * This function compares each registered bus to find one that matches
283  * the data passed as parameter.
284  *
285  * If the comparison function returns zero this function will stop iterating
286  * over any more buses. To continue a search the bus of a previous search can
287  * be passed via the start parameter.
288  *
289  * @param start
290  *      Starting point for the iteration.
291  *
292  * @param cmp
293  *      Comparison function.
294  *
295  * @param data
296  *       Data to pass to comparison function.
297  *
298  * @return
299  *       A pointer to a rte_bus structure or NULL in case no bus matches
300  */
301 struct rte_bus *rte_bus_find(const struct rte_bus *start, rte_bus_cmp_t cmp,
302                              const void *data);
303
304 /**
305  * Find the registered bus for a particular device.
306  */
307 struct rte_bus *rte_bus_find_by_device(const struct rte_device *dev);
308
309 /**
310  * Find the registered bus for a given name.
311  */
312 struct rte_bus *rte_bus_find_by_name(const char *busname);
313
314
315 /**
316  * Get the common iommu class of devices bound on to buses available in the
317  * system. The default mode is PA.
318  *
319  * @return
320  *     enum rte_iova_mode value.
321  */
322 enum rte_iova_mode rte_bus_get_iommu_class(void);
323
324 /**
325  * Helper for Bus registration.
326  * The constructor has higher priority than PMD constructors.
327  */
328 #define RTE_REGISTER_BUS(nm, bus) \
329 RTE_INIT_PRIO(businitfn_ ##nm, BUS) \
330 {\
331         (bus).name = RTE_STR(nm);\
332         rte_bus_register(&bus); \
333 }
334
335 #ifdef __cplusplus
336 }
337 #endif
338
339 #endif /* _RTE_BUS_H */