xref: /OK3568_Linux_fs/kernel/include/linux/mdev.h (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Mediated device definition
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6*4882a593Smuzhiyun  *     Author: Neo Jia <cjia@nvidia.com>
7*4882a593Smuzhiyun  *             Kirti Wankhede <kwankhede@nvidia.com>
8*4882a593Smuzhiyun  */
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #ifndef MDEV_H
11*4882a593Smuzhiyun #define MDEV_H
12*4882a593Smuzhiyun 
13*4882a593Smuzhiyun struct mdev_device;
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * Called by the parent device driver to set the device which represents
17*4882a593Smuzhiyun  * this mdev in iommu protection scope. By default, the iommu device is
18*4882a593Smuzhiyun  * NULL, that indicates using vendor defined isolation.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  * @dev: the mediated device that iommu will isolate.
21*4882a593Smuzhiyun  * @iommu_device: a pci device which represents the iommu for @dev.
22*4882a593Smuzhiyun  *
23*4882a593Smuzhiyun  * Return 0 for success, otherwise negative error value.
24*4882a593Smuzhiyun  */
25*4882a593Smuzhiyun int mdev_set_iommu_device(struct device *dev, struct device *iommu_device);
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun struct device *mdev_get_iommu_device(struct device *dev);
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /**
30*4882a593Smuzhiyun  * struct mdev_parent_ops - Structure to be registered for each parent device to
31*4882a593Smuzhiyun  * register the device to mdev module.
32*4882a593Smuzhiyun  *
33*4882a593Smuzhiyun  * @owner:		The module owner.
34*4882a593Smuzhiyun  * @dev_attr_groups:	Attributes of the parent device.
35*4882a593Smuzhiyun  * @mdev_attr_groups:	Attributes of the mediated device.
36*4882a593Smuzhiyun  * @supported_type_groups: Attributes to define supported types. It is mandatory
37*4882a593Smuzhiyun  *			to provide supported types.
38*4882a593Smuzhiyun  * @create:		Called to allocate basic resources in parent device's
39*4882a593Smuzhiyun  *			driver for a particular mediated device. It is
40*4882a593Smuzhiyun  *			mandatory to provide create ops.
41*4882a593Smuzhiyun  *			@kobj: kobject of type for which 'create' is called.
42*4882a593Smuzhiyun  *			@mdev: mdev_device structure on of mediated device
43*4882a593Smuzhiyun  *			      that is being created
44*4882a593Smuzhiyun  *			Returns integer: success (0) or error (< 0)
45*4882a593Smuzhiyun  * @remove:		Called to free resources in parent device's driver for a
46*4882a593Smuzhiyun  *			a mediated device. It is mandatory to provide 'remove'
47*4882a593Smuzhiyun  *			ops.
48*4882a593Smuzhiyun  *			@mdev: mdev_device device structure which is being
49*4882a593Smuzhiyun  *			       destroyed
50*4882a593Smuzhiyun  *			Returns integer: success (0) or error (< 0)
51*4882a593Smuzhiyun  * @open:		Open mediated device.
52*4882a593Smuzhiyun  *			@mdev: mediated device.
53*4882a593Smuzhiyun  *			Returns integer: success (0) or error (< 0)
54*4882a593Smuzhiyun  * @release:		release mediated device
55*4882a593Smuzhiyun  *			@mdev: mediated device.
56*4882a593Smuzhiyun  * @read:		Read emulation callback
57*4882a593Smuzhiyun  *			@mdev: mediated device structure
58*4882a593Smuzhiyun  *			@buf: read buffer
59*4882a593Smuzhiyun  *			@count: number of bytes to read
60*4882a593Smuzhiyun  *			@ppos: address.
61*4882a593Smuzhiyun  *			Retuns number on bytes read on success or error.
62*4882a593Smuzhiyun  * @write:		Write emulation callback
63*4882a593Smuzhiyun  *			@mdev: mediated device structure
64*4882a593Smuzhiyun  *			@buf: write buffer
65*4882a593Smuzhiyun  *			@count: number of bytes to be written
66*4882a593Smuzhiyun  *			@ppos: address.
67*4882a593Smuzhiyun  *			Retuns number on bytes written on success or error.
68*4882a593Smuzhiyun  * @ioctl:		IOCTL callback
69*4882a593Smuzhiyun  *			@mdev: mediated device structure
70*4882a593Smuzhiyun  *			@cmd: ioctl command
71*4882a593Smuzhiyun  *			@arg: arguments to ioctl
72*4882a593Smuzhiyun  * @mmap:		mmap callback
73*4882a593Smuzhiyun  *			@mdev: mediated device structure
74*4882a593Smuzhiyun  *			@vma: vma structure
75*4882a593Smuzhiyun  * Parent device that support mediated device should be registered with mdev
76*4882a593Smuzhiyun  * module with mdev_parent_ops structure.
77*4882a593Smuzhiyun  **/
78*4882a593Smuzhiyun struct mdev_parent_ops {
79*4882a593Smuzhiyun 	struct module   *owner;
80*4882a593Smuzhiyun 	const struct attribute_group **dev_attr_groups;
81*4882a593Smuzhiyun 	const struct attribute_group **mdev_attr_groups;
82*4882a593Smuzhiyun 	struct attribute_group **supported_type_groups;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	int     (*create)(struct kobject *kobj, struct mdev_device *mdev);
85*4882a593Smuzhiyun 	int     (*remove)(struct mdev_device *mdev);
86*4882a593Smuzhiyun 	int     (*open)(struct mdev_device *mdev);
87*4882a593Smuzhiyun 	void    (*release)(struct mdev_device *mdev);
88*4882a593Smuzhiyun 	ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
89*4882a593Smuzhiyun 			size_t count, loff_t *ppos);
90*4882a593Smuzhiyun 	ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
91*4882a593Smuzhiyun 			 size_t count, loff_t *ppos);
92*4882a593Smuzhiyun 	long	(*ioctl)(struct mdev_device *mdev, unsigned int cmd,
93*4882a593Smuzhiyun 			 unsigned long arg);
94*4882a593Smuzhiyun 	int	(*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
95*4882a593Smuzhiyun };
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun /* interface for exporting mdev supported type attributes */
98*4882a593Smuzhiyun struct mdev_type_attribute {
99*4882a593Smuzhiyun 	struct attribute attr;
100*4882a593Smuzhiyun 	ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
101*4882a593Smuzhiyun 	ssize_t (*store)(struct kobject *kobj, struct device *dev,
102*4882a593Smuzhiyun 			 const char *buf, size_t count);
103*4882a593Smuzhiyun };
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
106*4882a593Smuzhiyun struct mdev_type_attribute mdev_type_attr_##_name =		\
107*4882a593Smuzhiyun 	__ATTR(_name, _mode, _show, _store)
108*4882a593Smuzhiyun #define MDEV_TYPE_ATTR_RW(_name) \
109*4882a593Smuzhiyun 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
110*4882a593Smuzhiyun #define MDEV_TYPE_ATTR_RO(_name) \
111*4882a593Smuzhiyun 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
112*4882a593Smuzhiyun #define MDEV_TYPE_ATTR_WO(_name) \
113*4882a593Smuzhiyun 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
114*4882a593Smuzhiyun 
115*4882a593Smuzhiyun /**
116*4882a593Smuzhiyun  * struct mdev_driver - Mediated device driver
117*4882a593Smuzhiyun  * @name: driver name
118*4882a593Smuzhiyun  * @probe: called when new device created
119*4882a593Smuzhiyun  * @remove: called when device removed
120*4882a593Smuzhiyun  * @driver: device driver structure
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  **/
123*4882a593Smuzhiyun struct mdev_driver {
124*4882a593Smuzhiyun 	const char *name;
125*4882a593Smuzhiyun 	int  (*probe)(struct device *dev);
126*4882a593Smuzhiyun 	void (*remove)(struct device *dev);
127*4882a593Smuzhiyun 	struct device_driver driver;
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun #define to_mdev_driver(drv)	container_of(drv, struct mdev_driver, driver)
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun void *mdev_get_drvdata(struct mdev_device *mdev);
133*4882a593Smuzhiyun void mdev_set_drvdata(struct mdev_device *mdev, void *data);
134*4882a593Smuzhiyun const guid_t *mdev_uuid(struct mdev_device *mdev);
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun extern struct bus_type mdev_bus_type;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
139*4882a593Smuzhiyun void mdev_unregister_device(struct device *dev);
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun int mdev_register_driver(struct mdev_driver *drv, struct module *owner);
142*4882a593Smuzhiyun void mdev_unregister_driver(struct mdev_driver *drv);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun struct device *mdev_parent_dev(struct mdev_device *mdev);
145*4882a593Smuzhiyun struct device *mdev_dev(struct mdev_device *mdev);
146*4882a593Smuzhiyun struct mdev_device *mdev_from_dev(struct device *dev);
147*4882a593Smuzhiyun 
148*4882a593Smuzhiyun #endif /* MDEV_H */
149