1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * CDX bus public interface
4  *
5  * Copyright (C) 2022-2023, Advanced Micro Devices, Inc.
6  *
7  */
8 
9 #ifndef _CDX_BUS_H_
10 #define _CDX_BUS_H_
11 
12 #include <linux/device.h>
13 #include <linux/list.h>
14 #include <linux/mod_devicetable.h>
15 
16 #define MAX_CDX_DEV_RESOURCES	4
17 #define CDX_CONTROLLER_ID_SHIFT 4
18 #define CDX_BUS_NUM_MASK 0xF
19 
20 /* Forward declaration for CDX controller */
21 struct cdx_controller;
22 
23 enum {
24 	CDX_DEV_RESET_CONF,
25 };
26 
27 struct cdx_device_config {
28 	u8 type;
29 };
30 
31 typedef int (*cdx_scan_cb)(struct cdx_controller *cdx);
32 
33 typedef int (*cdx_dev_configure_cb)(struct cdx_controller *cdx,
34 				    u8 bus_num, u8 dev_num,
35 				    struct cdx_device_config *dev_config);
36 
37 /**
38  * CDX_DEVICE_DRIVER_OVERRIDE - macro used to describe a CDX device with
39  *                              override_only flags.
40  * @vend: the 16 bit CDX Vendor ID
41  * @dev: the 16 bit CDX Device ID
42  * @driver_override: the 32 bit CDX Device override_only
43  *
44  * This macro is used to create a struct cdx_device_id that matches only a
45  * driver_override device.
46  */
47 #define CDX_DEVICE_DRIVER_OVERRIDE(vend, dev, driver_override) \
48 	.vendor = (vend), .device = (dev), .override_only = (driver_override)
49 
50 /**
51  * struct cdx_ops - Callbacks supported by CDX controller.
52  * @scan: scan the devices on the controller
53  * @dev_configure: configuration like reset, master_enable,
54  *		   msi_config etc for a CDX device
55  */
56 struct cdx_ops {
57 	cdx_scan_cb scan;
58 	cdx_dev_configure_cb dev_configure;
59 };
60 
61 /**
62  * struct cdx_controller: CDX controller object
63  * @dev: Linux device associated with the CDX controller.
64  * @priv: private data
65  * @id: Controller ID
66  * @ops: CDX controller ops
67  */
68 struct cdx_controller {
69 	struct device *dev;
70 	void *priv;
71 	u32 id;
72 	struct cdx_ops *ops;
73 };
74 
75 /**
76  * struct cdx_device - CDX device object
77  * @dev: Linux driver model device object
78  * @cdx: CDX controller associated with the device
79  * @vendor: Vendor ID for CDX device
80  * @device: Device ID for CDX device
81  * @bus_num: Bus number for this CDX device
82  * @dev_num: Device number for this device
83  * @res: array of MMIO region entries
84  * @res_attr: resource binary attribute
85  * @res_count: number of valid MMIO regions
86  * @dma_mask: Default DMA mask
87  * @flags: CDX device flags
88  * @req_id: Requestor ID associated with CDX device
89  * @driver_override: driver name to force a match; do not set directly,
90  *                   because core frees it; use driver_set_override() to
91  *                   set or clear it.
92  */
93 struct cdx_device {
94 	struct device dev;
95 	struct cdx_controller *cdx;
96 	u16 vendor;
97 	u16 device;
98 	u8 bus_num;
99 	u8 dev_num;
100 	struct resource res[MAX_CDX_DEV_RESOURCES];
101 	u8 res_count;
102 	u64 dma_mask;
103 	u16 flags;
104 	u32 req_id;
105 	const char *driver_override;
106 };
107 
108 #define to_cdx_device(_dev) \
109 	container_of(_dev, struct cdx_device, dev)
110 
111 /**
112  * struct cdx_driver - CDX device driver
113  * @driver: Generic device driver
114  * @match_id_table: table of supported device matching Ids
115  * @probe: Function called when a device is added
116  * @remove: Function called when a device is removed
117  * @shutdown: Function called at shutdown time to quiesce the device
118  * @reset_prepare: Function called before is reset to notify driver
119  * @reset_done: Function called after reset is complete to notify driver
120  * @driver_managed_dma: Device driver doesn't use kernel DMA API for DMA.
121  *		For most device drivers, no need to care about this flag
122  *		as long as all DMAs are handled through the kernel DMA API.
123  *		For some special ones, for example VFIO drivers, they know
124  *		how to manage the DMA themselves and set this flag so that
125  *		the IOMMU layer will allow them to setup and manage their
126  *		own I/O address space.
127  */
128 struct cdx_driver {
129 	struct device_driver driver;
130 	const struct cdx_device_id *match_id_table;
131 	int (*probe)(struct cdx_device *dev);
132 	int (*remove)(struct cdx_device *dev);
133 	void (*shutdown)(struct cdx_device *dev);
134 	void (*reset_prepare)(struct cdx_device *dev);
135 	void (*reset_done)(struct cdx_device *dev);
136 	bool driver_managed_dma;
137 };
138 
139 #define to_cdx_driver(_drv) \
140 	container_of(_drv, struct cdx_driver, driver)
141 
142 /* Macro to avoid include chaining to get THIS_MODULE */
143 #define cdx_driver_register(drv) \
144 	__cdx_driver_register(drv, THIS_MODULE)
145 
146 /**
147  * __cdx_driver_register - registers a CDX device driver
148  * @cdx_driver: CDX driver to register
149  * @owner: module owner
150  *
151  * Return: -errno on failure, 0 on success.
152  */
153 int __must_check __cdx_driver_register(struct cdx_driver *cdx_driver,
154 				       struct module *owner);
155 
156 /**
157  * cdx_driver_unregister - unregisters a device driver from the
158  * CDX bus.
159  * @cdx_driver: CDX driver to register
160  */
161 void cdx_driver_unregister(struct cdx_driver *cdx_driver);
162 
163 extern struct bus_type cdx_bus_type;
164 
165 /**
166  * cdx_dev_reset - Reset CDX device
167  * @dev: device pointer
168  *
169  * Return: 0 for success, -errno on failure
170  */
171 int cdx_dev_reset(struct device *dev);
172 
173 #endif /* _CDX_BUS_H_ */
174