xref: /dpdk/drivers/bus/platform/bus_platform_driver.h (revision 719834a6849e1daf4a70ff7742bbcc3ae7e25607)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2023 Marvell.
3  */
4 
5 #ifndef BUS_PLATFORM_DRIVER_H
6 #define BUS_PLATFORM_DRIVER_H
7 
8 /**
9  * @file
10  * Platform bus interface.
11  */
12 
13 #include <stddef.h>
14 #include <stdint.h>
15 
16 #include <dev_driver.h>
17 #include <rte_common.h>
18 #include <rte_dev.h>
19 #include <rte_os.h>
20 #include <rte_vfio.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 /* Forward declarations */
27 struct rte_platform_bus;
28 struct rte_platform_device;
29 struct rte_platform_driver;
30 
31 /**
32  * Initialization function for the driver called during platform device probing.
33  *
34  * @param pdev
35  *   Pointer to the platform device.
36  * @return
37  *   0 on success, negative value otherwise.
38  */
39 typedef int (rte_platform_probe_t)(struct rte_platform_device *pdev);
40 
41 /**
42  * Removal function for the driver called during platform device removal.
43  *
44  * @param pdev
45  *   Pointer to the platform device.
46  * @return
47  *   0 on success, negative value otherwise.
48  */
49 typedef int (rte_platform_remove_t)(struct rte_platform_device *pdev);
50 
51 /**
52  * Driver specific DMA mapping.
53  *
54  * @param pdev
55  *   Pointer to the platform device.
56  * @param addr
57  *   Starting virtual address of memory to be mapped.
58  * @param iova
59  *   Starting IOVA address of memory to be mapped.
60  * @param len
61  *   Length of memory segment being mapped.
62  * @return
63  *   - 0 on success, negative value and rte_errno is set otherwise.
64  */
65 typedef int (rte_platform_dma_map_t)(struct rte_platform_device *pdev, void *addr, uint64_t iova,
66 				     size_t len);
67 
68 /**
69  * Driver specific DMA unmapping.
70  *
71  * @param pdev
72  *   Pointer to the platform device.
73  * @param addr
74  *   Starting virtual address of memory to be mapped.
75  * @param iova
76  *   Starting IOVA address of memory to be mapped.
77  * @param len
78  *   Length of memory segment being mapped.
79  * @return
80  *   - 0 on success, negative value and rte_errno is set otherwise.
81  */
82 typedef int (rte_platform_dma_unmap_t)(struct rte_platform_device *pdev, void *addr, uint64_t iova,
83 				       size_t len);
84 
85 /**
86  * A structure describing a platform device resource.
87  */
88 struct rte_platform_resource {
89 	char *name; /**< Resource name specified via reg-names prop in device-tree */
90 	struct rte_mem_resource mem; /**< Memory resource */
91 };
92 
93 /**
94  * A structure describing a platform device.
95  */
96 struct rte_platform_device {
97 	RTE_TAILQ_ENTRY(rte_platform_device) next; /**< Next attached platform device */
98 	struct rte_device device; /**< Core device */
99 	struct rte_platform_driver *driver; /**< Matching device driver */
100 	char name[RTE_DEV_NAME_MAX_LEN]; /**< Device name */
101 	unsigned int num_resource; /**< Number of device resources */
102 	struct rte_platform_resource *resource; /**< Device resources */
103 	int dev_fd; /**< VFIO device fd */
104 };
105 
106 /**
107  * A structure describing a platform device driver.
108  */
109 struct rte_platform_driver {
110 	RTE_TAILQ_ENTRY(rte_platform_driver) next; /**< Next available platform driver */
111 	struct rte_driver driver; /**< Core driver */
112 	rte_platform_probe_t *probe;  /**< Device probe function */
113 	rte_platform_remove_t *remove; /**< Device remove function */
114 	rte_platform_dma_map_t *dma_map; /**< Device DMA map function */
115 	rte_platform_dma_unmap_t *dma_unmap; /**< Device DMA unmap function */
116 	uint32_t drv_flags; /**< Driver flags RTE_PLATFORM_DRV_* */
117 };
118 
119 /** Device driver needs IOVA as VA and cannot work with IOVA as PA */
120 #define RTE_PLATFORM_DRV_NEED_IOVA_AS_VA 0x0001
121 
122 /**
123  * @internal
124  * Helper macros used to convert core device to platform device.
125  */
126 #define RTE_DEV_TO_PLATFORM_DEV(ptr) \
127 	container_of(ptr, struct rte_platform_device, device)
128 
129 #define RTE_DEV_TO_PLATFORM_DEV_CONST(ptr) \
130 	container_of(ptr, const struct rte_platform_device, device)
131 
132 /** Helper for platform driver registration. */
133 #define RTE_PMD_REGISTER_PLATFORM(nm, platform_drv) \
134 static const char *pdrvinit_ ## nm ## _alias; \
135 RTE_INIT(pdrvinitfn_ ##nm) \
136 { \
137 	(platform_drv).driver.name = RTE_STR(nm); \
138 	(platform_drv).driver.alias = pdrvinit_ ## nm ## _alias; \
139 	rte_platform_register(&(platform_drv)); \
140 } \
141 RTE_PMD_EXPORT_NAME(nm, __COUNTER__)
142 
143 /** Helper for setting platform driver alias. */
144 #define RTE_PMD_REGISTER_ALIAS(nm, alias) \
145 static const char *pdrvinit_ ## nm ## _alias = RTE_STR(alias)
146 
147 #ifdef VFIO_PRESENT
148 
149 /**
150  * Register a platform device driver.
151  *
152  * @warning
153  * @b EXPERIMENTAL: this API may change without prior notice.
154  *
155  * @param pdrv
156  *   A pointer to a rte_platform_driver structure describing driver to be registered.
157  */
158 __rte_internal
159 void rte_platform_register(struct rte_platform_driver *pdrv);
160 
161 /**
162  * Unregister a platform device driver.
163  *
164  * @warning
165  * @b EXPERIMENTAL: this API may change without prior notice.
166  *
167  * @param pdrv
168  *   A pointer to a rte_platform_driver structure describing driver to be unregistered.
169  */
170 __rte_internal
171 void rte_platform_unregister(struct rte_platform_driver *pdrv);
172 
173 #else
174 
175 __rte_internal
176 static inline void
177 rte_platform_register(struct rte_platform_driver *pdrv __rte_unused)
178 {
179 }
180 
181 __rte_internal
182 static inline void
183 rte_platform_unregister(struct rte_platform_driver *pdrv __rte_unused)
184 {
185 }
186 
187 #endif /* VFIO_PRESENT */
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 
193 #endif /* BUS_PLATFORM_DRIVER_H */
194