Numa-aware, growable physical memory allocator (pmalloc)
[vpp.git] / src / vlib / pci / pci.h
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  * pci.h: PCI definitions.
17  *
18  * Copyright (c) 2008 Eliot Dresselhaus
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining
21  * a copy of this software and associated documentation files (the
22  * "Software"), to deal in the Software without restriction, including
23  * without limitation the rights to use, copy, modify, merge, publish,
24  * distribute, sublicense, and/or sell copies of the Software, and to
25  * permit persons to whom the Software is furnished to do so, subject to
26  * the following conditions:
27  *
28  * The above copyright notice and this permission notice shall be
29  * included in all copies or substantial portions of the Software.
30  *
31  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
32  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
33  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
34  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
35  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
36  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
37  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38  */
39
40 #ifndef included_vlib_pci_h
41 #define included_vlib_pci_h
42
43 #include <vlib/vlib.h>
44 #include <vlib/pci/pci_config.h>
45
46 /* *INDENT-OFF* */
47 typedef CLIB_PACKED (union
48 {
49   struct
50     {
51       u16 domain;
52       u8 bus;
53       u8 slot: 5;
54       u8 function:3;
55     };
56   u32 as_u32;
57 }) vlib_pci_addr_t;
58 /* *INDENT-ON* */
59
60 typedef struct vlib_pci_device_info
61 {
62   u32 flags;
63 #define VLIB_PCI_DEVICE_INFO_F_NOIOMMU          (1 << 0);
64
65   /* addr */
66   vlib_pci_addr_t addr;
67
68   /* Numa Node */
69   int numa_node;
70
71   /* Device data */
72   u16 device_class;
73   u16 vendor_id;
74   u16 device_id;
75
76   /* Vital Product Data */
77   u8 *product_name;
78   u8 *vpd_r;
79   u8 *vpd_w;
80
81   /* Driver name */
82   u8 *driver_name;
83
84   /* First 64 bytes of configuration space. */
85   union
86   {
87     pci_config_type0_regs_t config0;
88     pci_config_type1_regs_t config1;
89     u8 config_data[256];
90   };
91
92   /* IOMMU Group */
93   int iommu_group;
94
95 } vlib_pci_device_info_t;
96
97 typedef u32 vlib_pci_dev_handle_t;
98
99 vlib_pci_device_info_t *vlib_pci_get_device_info (vlib_main_t * vm,
100                                                   vlib_pci_addr_t * addr,
101                                                   clib_error_t ** error);
102 vlib_pci_addr_t *vlib_pci_get_all_dev_addrs ();
103 vlib_pci_addr_t *vlib_pci_get_addr (vlib_main_t * vm,
104                                     vlib_pci_dev_handle_t h);
105 uword vlib_pci_get_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h);
106 void vlib_pci_set_private_data (vlib_main_t * vm, vlib_pci_dev_handle_t h,
107                                 uword private_data);
108
109 static inline void
110 vlib_pci_free_device_info (vlib_pci_device_info_t * di)
111 {
112   if (!di)
113     return;
114   vec_free (di->product_name);
115   vec_free (di->vpd_r);
116   vec_free (di->vpd_w);
117   vec_free (di->driver_name);
118   clib_mem_free (di);
119 }
120
121 typedef struct
122 {
123   u16 vendor_id, device_id;
124 } pci_device_id_t;
125
126 typedef void (pci_intx_handler_function_t) (vlib_main_t * vm,
127                                             vlib_pci_dev_handle_t handle);
128 typedef void (pci_msix_handler_function_t) (vlib_main_t * vm,
129                                             vlib_pci_dev_handle_t handle,
130                                             u16 line);
131
132 typedef struct _pci_device_registration
133 {
134   /* Driver init function. */
135   clib_error_t *(*init_function) (vlib_main_t * vm,
136                                   vlib_pci_dev_handle_t handle);
137
138   /* Interrupt handler */
139   pci_intx_handler_function_t *interrupt_handler;
140
141   /* List of registrations */
142   struct _pci_device_registration *next_registration;
143
144   /* Vendor/device ids supported by this driver. */
145   pci_device_id_t supported_devices[];
146 } pci_device_registration_t;
147
148 /* Pool of PCI devices. */
149 typedef struct
150 {
151   vlib_main_t *vlib_main;
152   pci_device_registration_t *pci_device_registrations;
153   /* logging */
154   vlib_log_class_t log_default;
155 } vlib_pci_main_t;
156
157 extern vlib_pci_main_t pci_main;
158
159 #define PCI_REGISTER_DEVICE(x,...)                              \
160     __VA_ARGS__ pci_device_registration_t x;                    \
161 static void __vlib_add_pci_device_registration_##x (void)       \
162     __attribute__((__constructor__)) ;                          \
163 static void __vlib_add_pci_device_registration_##x (void)       \
164 {                                                               \
165     vlib_pci_main_t * pm = &pci_main;                           \
166     x.next_registration = pm->pci_device_registrations;         \
167     pm->pci_device_registrations = &x;                          \
168 }                                                               \
169 static void __vlib_rm_pci_device_registration_##x (void)        \
170     __attribute__((__destructor__)) ;                           \
171 static void __vlib_rm_pci_device_registration_##x (void)        \
172 {                                                               \
173     vlib_pci_main_t * pm = &pci_main;                           \
174     VLIB_REMOVE_FROM_LINKED_LIST (pm->pci_device_registrations, \
175                                   &x, next_registration);       \
176 }                                                               \
177 __VA_ARGS__ pci_device_registration_t x
178
179 clib_error_t *vlib_pci_bind_to_uio (vlib_main_t * vm, vlib_pci_addr_t * addr,
180                                     char *uio_driver_name);
181
182 /* Configuration space read/write. */
183 clib_error_t *vlib_pci_read_write_config (vlib_main_t * vm,
184                                           vlib_pci_dev_handle_t handle,
185                                           vlib_read_or_write_t read_or_write,
186                                           uword address, void *data,
187                                           u32 n_bytes);
188
189 #define _(t)                                                            \
190 static inline clib_error_t *                                            \
191 vlib_pci_read_config_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h,     \
192                           uword address, t * data)                      \
193 {                                                                       \
194   return vlib_pci_read_write_config (vm, h, VLIB_READ,address, data,    \
195                                      sizeof (data[0]));                 \
196 }
197
198 _(u32);
199 _(u16);
200 _(u8);
201
202 #undef _
203
204 #define _(t)                                                            \
205 static inline clib_error_t *                                            \
206 vlib_pci_write_config_##t (vlib_main_t *vm, vlib_pci_dev_handle_t h,    \
207                            uword address, t * data)                     \
208 {                                                                       \
209   return vlib_pci_read_write_config (vm, h, VLIB_WRITE,                 \
210                                    address, data, sizeof (data[0]));    \
211 }
212
213 _(u32);
214 _(u16);
215 _(u8);
216
217 #undef _
218
219 static inline clib_error_t *
220 vlib_pci_intr_enable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
221 {
222   u16 command;
223   clib_error_t *err;
224
225   err = vlib_pci_read_config_u16 (vm, h, 4, &command);
226
227   if (err)
228     return err;
229
230   command &= ~PCI_COMMAND_INTX_DISABLE;
231
232   return vlib_pci_write_config_u16 (vm, h, 4, &command);
233 }
234
235 static inline clib_error_t *
236 vlib_pci_intr_disable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
237 {
238   u16 command;
239   clib_error_t *err;
240
241   err = vlib_pci_read_config_u16 (vm, h, 4, &command);
242
243   if (err)
244     return err;
245
246   command |= PCI_COMMAND_INTX_DISABLE;
247
248   return vlib_pci_write_config_u16 (vm, h, 4, &command);
249 }
250
251 static inline clib_error_t *
252 vlib_pci_bus_master_enable (vlib_main_t * vm, vlib_pci_dev_handle_t h)
253 {
254   clib_error_t *err;
255   u16 command;
256
257   /* Set bus master enable (BME) */
258   err = vlib_pci_read_config_u16 (vm, h, 4, &command);
259
260   if (err)
261     return err;
262
263   if (command & PCI_COMMAND_BUS_MASTER)
264     return 0;
265
266   command |= PCI_COMMAND_BUS_MASTER;
267
268   return vlib_pci_write_config_u16 (vm, h, 4, &command);
269 }
270
271 clib_error_t *vlib_pci_device_open (vlib_main_t * vm, vlib_pci_addr_t * addr,
272                                     pci_device_id_t ids[],
273                                     vlib_pci_dev_handle_t * handle);
274 void vlib_pci_device_close (vlib_main_t * vm, vlib_pci_dev_handle_t h);
275 clib_error_t *vlib_pci_map_region (vlib_main_t * vm, vlib_pci_dev_handle_t h,
276                                    u32 resource, void **result);
277 clib_error_t *vlib_pci_map_region_fixed (vlib_main_t * vm,
278                                          vlib_pci_dev_handle_t h,
279                                          u32 resource, u8 * addr,
280                                          void **result);
281 clib_error_t *vlib_pci_register_intx_handler (vlib_main_t * vm,
282                                               vlib_pci_dev_handle_t h,
283                                               pci_intx_handler_function_t *
284                                               intx_handler);
285 clib_error_t *vlib_pci_register_msix_handler (vlib_main_t * vm,
286                                               vlib_pci_dev_handle_t h,
287                                               u32 start, u32 count,
288                                               pci_msix_handler_function_t *
289                                               msix_handler);
290 clib_error_t *vlib_pci_enable_msix_irq (vlib_main_t * vm,
291                                         vlib_pci_dev_handle_t h, u16 start,
292                                         u16 count);
293 clib_error_t *vlib_pci_disable_msix_irq (vlib_main_t * vm,
294                                          vlib_pci_dev_handle_t h, u16 start,
295                                          u16 count);
296 clib_error_t *vlib_pci_map_dma (vlib_main_t * vm, vlib_pci_dev_handle_t h,
297                                 void *ptr);
298
299 int vlib_pci_supports_virtual_addr_dma (vlib_main_t * vm,
300                                         vlib_pci_dev_handle_t h);
301
302 unformat_function_t unformat_vlib_pci_addr;
303 format_function_t format_vlib_pci_addr;
304 format_function_t format_vlib_pci_link_speed;
305 format_function_t format_vlib_pci_vpd;
306
307 #endif /* included_vlib_pci_h */
308
309 /*
310  * fd.io coding-style-patch-verification: ON
311  *
312  * Local Variables:
313  * eval: (c-set-style "gnu")
314  * End:
315  */