1 /*
2  * Copyright (c) 2020 Intel Corporation
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #ifndef ZEPHYR_INCLUDE_DRIVERS_INTEL_VTD_H_
7 #define ZEPHYR_INCLUDE_DRIVERS_INTEL_VTD_H_
8 
9 #include <drivers/pcie/msi.h>
10 
11 typedef int (*vtd_alloc_entries_f)(const struct device *dev,
12 				   uint8_t n_entries);
13 
14 typedef uint32_t (*vtd_remap_msi_f)(const struct device *dev,
15 				    msi_vector_t *vector);
16 
17 typedef int (*vtd_remap_f)(const struct device *dev,
18 			   msi_vector_t *vector);
19 
20 struct vtd_driver_api {
21 	vtd_alloc_entries_f allocate_entries;
22 	vtd_remap_msi_f remap_msi;
23 	vtd_remap_f remap;
24 };
25 
26 /**
27  * @brief Allocate contiguous IRTEs
28  *
29  * @param dev Pointer to the device structure for the driver instance
30  * @param n_entries How many IRTE to allocate
31  *
32  * Note: It will try to allocate all, or it will fail.
33  *
34  * @return The first allocated IRTE index, or -EBUSY on failure
35  */
vtd_allocate_entries(const struct device * dev,uint8_t n_entries)36 static inline int vtd_allocate_entries(const struct device *dev,
37 				       uint8_t n_entries)
38 {
39 	const struct vtd_driver_api *api =
40 		(const struct vtd_driver_api *)dev->api;
41 
42 	return api->allocate_entries(dev, n_entries);
43 }
44 
45 /**
46  * @brief Generate the MSI Message Address data for the given vector
47  *
48  * @param dev Pointer to the device structure for the driver instance
49  * @param vector A valid allocated MSI vector
50  *
51  * @return The MSI Message Address value
52  */
vtd_remap_msi(const struct device * dev,msi_vector_t * vector)53 static inline uint32_t vtd_remap_msi(const struct device *dev,
54 				     msi_vector_t *vector)
55 {
56 	const struct vtd_driver_api *api =
57 		(const struct vtd_driver_api *)dev->api;
58 
59 	return api->remap_msi(dev, vector);
60 }
61 
62 /**
63  * @brief Remap the given vector
64  *
65  * @param dev Pointer to the device structure for the driver instance
66  * @param vector A valid allocated MSI vector
67  *
68  * @return 0 on success, a negative errno otherwise
69  */
vtd_remap(const struct device * dev,msi_vector_t * vector)70 static inline int vtd_remap(const struct device *dev,
71 			    msi_vector_t *vector)
72 {
73 	const struct vtd_driver_api *api =
74 		(const struct vtd_driver_api *)dev->api;
75 
76 	return api->remap(dev, vector);
77 }
78 
79 
80 #endif /* ZEPHYR_INCLUDE_DRIVERS_INTEL_VTD_H_ */
81