xref: /linux-6.15/include/linux/mdev.h (revision 15fcc44b)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
27b96953bSKirti Wankhede /*
37b96953bSKirti Wankhede  * Mediated device definition
47b96953bSKirti Wankhede  *
57b96953bSKirti Wankhede  * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
67b96953bSKirti Wankhede  *     Author: Neo Jia <[email protected]>
77b96953bSKirti Wankhede  *             Kirti Wankhede <[email protected]>
87b96953bSKirti Wankhede  */
97b96953bSKirti Wankhede 
107b96953bSKirti Wankhede #ifndef MDEV_H
117b96953bSKirti Wankhede #define MDEV_H
127b96953bSKirti Wankhede 
13417fd5bfSJason Gunthorpe struct mdev_type;
14417fd5bfSJason Gunthorpe 
152a3d15f2SJason Gunthorpe struct mdev_device {
162a3d15f2SJason Gunthorpe 	struct device dev;
172a3d15f2SJason Gunthorpe 	guid_t uuid;
182a3d15f2SJason Gunthorpe 	void *driver_data;
192a3d15f2SJason Gunthorpe 	struct list_head next;
20417fd5bfSJason Gunthorpe 	struct mdev_type *type;
212a3d15f2SJason Gunthorpe 	struct device *iommu_device;
222a3d15f2SJason Gunthorpe 	bool active;
232a3d15f2SJason Gunthorpe };
242a3d15f2SJason Gunthorpe 
252a3d15f2SJason Gunthorpe static inline struct mdev_device *to_mdev_device(struct device *dev)
262a3d15f2SJason Gunthorpe {
272a3d15f2SJason Gunthorpe 	return container_of(dev, struct mdev_device, dev);
282a3d15f2SJason Gunthorpe }
297b96953bSKirti Wankhede 
308ac13175SLu Baolu /*
318ac13175SLu Baolu  * Called by the parent device driver to set the device which represents
328ac13175SLu Baolu  * this mdev in iommu protection scope. By default, the iommu device is
338ac13175SLu Baolu  * NULL, that indicates using vendor defined isolation.
348ac13175SLu Baolu  *
358ac13175SLu Baolu  * @dev: the mediated device that iommu will isolate.
368ac13175SLu Baolu  * @iommu_device: a pci device which represents the iommu for @dev.
378ac13175SLu Baolu  */
382a3d15f2SJason Gunthorpe static inline void mdev_set_iommu_device(struct mdev_device *mdev,
392a3d15f2SJason Gunthorpe 					 struct device *iommu_device)
402a3d15f2SJason Gunthorpe {
412a3d15f2SJason Gunthorpe 	mdev->iommu_device = iommu_device;
422a3d15f2SJason Gunthorpe }
438ac13175SLu Baolu 
442a3d15f2SJason Gunthorpe static inline struct device *mdev_get_iommu_device(struct mdev_device *mdev)
452a3d15f2SJason Gunthorpe {
462a3d15f2SJason Gunthorpe 	return mdev->iommu_device;
472a3d15f2SJason Gunthorpe }
488ac13175SLu Baolu 
49*15fcc44bSJason Gunthorpe unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
50*15fcc44bSJason Gunthorpe unsigned int mtype_get_type_group_id(struct kobject *mtype_kobj);
51*15fcc44bSJason Gunthorpe 
527b96953bSKirti Wankhede /**
5342930553SAlex Williamson  * struct mdev_parent_ops - Structure to be registered for each parent device to
547b96953bSKirti Wankhede  * register the device to mdev module.
557b96953bSKirti Wankhede  *
567b96953bSKirti Wankhede  * @owner:		The module owner.
577b96953bSKirti Wankhede  * @dev_attr_groups:	Attributes of the parent device.
587b96953bSKirti Wankhede  * @mdev_attr_groups:	Attributes of the mediated device.
597b96953bSKirti Wankhede  * @supported_type_groups: Attributes to define supported types. It is mandatory
607b96953bSKirti Wankhede  *			to provide supported types.
617b96953bSKirti Wankhede  * @create:		Called to allocate basic resources in parent device's
627b96953bSKirti Wankhede  *			driver for a particular mediated device. It is
637b96953bSKirti Wankhede  *			mandatory to provide create ops.
647b96953bSKirti Wankhede  *			@kobj: kobject of type for which 'create' is called.
657b96953bSKirti Wankhede  *			@mdev: mdev_device structure on of mediated device
667b96953bSKirti Wankhede  *			      that is being created
677b96953bSKirti Wankhede  *			Returns integer: success (0) or error (< 0)
68df54714fSRandy Dunlap  * @remove:		Called to free resources in parent device's driver for
697b96953bSKirti Wankhede  *			a mediated device. It is mandatory to provide 'remove'
707b96953bSKirti Wankhede  *			ops.
717b96953bSKirti Wankhede  *			@mdev: mdev_device device structure which is being
727b96953bSKirti Wankhede  *			       destroyed
737b96953bSKirti Wankhede  *			Returns integer: success (0) or error (< 0)
747b96953bSKirti Wankhede  * @open:		Open mediated device.
757b96953bSKirti Wankhede  *			@mdev: mediated device.
767b96953bSKirti Wankhede  *			Returns integer: success (0) or error (< 0)
777b96953bSKirti Wankhede  * @release:		release mediated device
787b96953bSKirti Wankhede  *			@mdev: mediated device.
797b96953bSKirti Wankhede  * @read:		Read emulation callback
807b96953bSKirti Wankhede  *			@mdev: mediated device structure
817b96953bSKirti Wankhede  *			@buf: read buffer
827b96953bSKirti Wankhede  *			@count: number of bytes to read
837b96953bSKirti Wankhede  *			@ppos: address.
847b96953bSKirti Wankhede  *			Retuns number on bytes read on success or error.
857b96953bSKirti Wankhede  * @write:		Write emulation callback
867b96953bSKirti Wankhede  *			@mdev: mediated device structure
877b96953bSKirti Wankhede  *			@buf: write buffer
887b96953bSKirti Wankhede  *			@count: number of bytes to be written
897b96953bSKirti Wankhede  *			@ppos: address.
907b96953bSKirti Wankhede  *			Retuns number on bytes written on success or error.
917b96953bSKirti Wankhede  * @ioctl:		IOCTL callback
927b96953bSKirti Wankhede  *			@mdev: mediated device structure
937b96953bSKirti Wankhede  *			@cmd: ioctl command
947b96953bSKirti Wankhede  *			@arg: arguments to ioctl
957b96953bSKirti Wankhede  * @mmap:		mmap callback
967b96953bSKirti Wankhede  *			@mdev: mediated device structure
977b96953bSKirti Wankhede  *			@vma: vma structure
98a15ac665SEric Farman  * @request:		request callback to release device
99a15ac665SEric Farman  *			@mdev: mediated device structure
100a15ac665SEric Farman  *			@count: request sequence number
1017b96953bSKirti Wankhede  * Parent device that support mediated device should be registered with mdev
10242930553SAlex Williamson  * module with mdev_parent_ops structure.
1037b96953bSKirti Wankhede  **/
10442930553SAlex Williamson struct mdev_parent_ops {
1057b96953bSKirti Wankhede 	struct module   *owner;
1067b96953bSKirti Wankhede 	const struct attribute_group **dev_attr_groups;
1077b96953bSKirti Wankhede 	const struct attribute_group **mdev_attr_groups;
1087b96953bSKirti Wankhede 	struct attribute_group **supported_type_groups;
1097b96953bSKirti Wankhede 
1107b96953bSKirti Wankhede 	int     (*create)(struct kobject *kobj, struct mdev_device *mdev);
1117b96953bSKirti Wankhede 	int     (*remove)(struct mdev_device *mdev);
1127b96953bSKirti Wankhede 	int     (*open)(struct mdev_device *mdev);
1137b96953bSKirti Wankhede 	void    (*release)(struct mdev_device *mdev);
1147b96953bSKirti Wankhede 	ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
1157b96953bSKirti Wankhede 			size_t count, loff_t *ppos);
1167b96953bSKirti Wankhede 	ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
1177b96953bSKirti Wankhede 			 size_t count, loff_t *ppos);
118c6ef7fd4SPaul Gortmaker 	long	(*ioctl)(struct mdev_device *mdev, unsigned int cmd,
1197b96953bSKirti Wankhede 			 unsigned long arg);
1207b96953bSKirti Wankhede 	int	(*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
121a15ac665SEric Farman 	void	(*request)(struct mdev_device *mdev, unsigned int count);
1227b96953bSKirti Wankhede };
1237b96953bSKirti Wankhede 
1247b96953bSKirti Wankhede /* interface for exporting mdev supported type attributes */
1257b96953bSKirti Wankhede struct mdev_type_attribute {
1267b96953bSKirti Wankhede 	struct attribute attr;
1277b96953bSKirti Wankhede 	ssize_t (*show)(struct kobject *kobj, struct device *dev, char *buf);
1287b96953bSKirti Wankhede 	ssize_t (*store)(struct kobject *kobj, struct device *dev,
1297b96953bSKirti Wankhede 			 const char *buf, size_t count);
1307b96953bSKirti Wankhede };
1317b96953bSKirti Wankhede 
1327b96953bSKirti Wankhede #define MDEV_TYPE_ATTR(_name, _mode, _show, _store)		\
1337b96953bSKirti Wankhede struct mdev_type_attribute mdev_type_attr_##_name =		\
1347b96953bSKirti Wankhede 	__ATTR(_name, _mode, _show, _store)
1357b96953bSKirti Wankhede #define MDEV_TYPE_ATTR_RW(_name) \
1367b96953bSKirti Wankhede 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
1377b96953bSKirti Wankhede #define MDEV_TYPE_ATTR_RO(_name) \
1387b96953bSKirti Wankhede 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
1397b96953bSKirti Wankhede #define MDEV_TYPE_ATTR_WO(_name) \
1407b96953bSKirti Wankhede 	struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
1417b96953bSKirti Wankhede 
1427b96953bSKirti Wankhede /**
1437b96953bSKirti Wankhede  * struct mdev_driver - Mediated device driver
1447b96953bSKirti Wankhede  * @probe: called when new device created
1457b96953bSKirti Wankhede  * @remove: called when device removed
1467b96953bSKirti Wankhede  * @driver: device driver structure
1477b96953bSKirti Wankhede  *
1487b96953bSKirti Wankhede  **/
1497b96953bSKirti Wankhede struct mdev_driver {
1502a3d15f2SJason Gunthorpe 	int (*probe)(struct mdev_device *dev);
1512a3d15f2SJason Gunthorpe 	void (*remove)(struct mdev_device *dev);
1527b96953bSKirti Wankhede 	struct device_driver driver;
1537b96953bSKirti Wankhede };
1547b96953bSKirti Wankhede 
1552a3d15f2SJason Gunthorpe static inline void *mdev_get_drvdata(struct mdev_device *mdev)
1562a3d15f2SJason Gunthorpe {
1572a3d15f2SJason Gunthorpe 	return mdev->driver_data;
1582a3d15f2SJason Gunthorpe }
1592a3d15f2SJason Gunthorpe static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
1602a3d15f2SJason Gunthorpe {
1612a3d15f2SJason Gunthorpe 	mdev->driver_data = data;
1622a3d15f2SJason Gunthorpe }
1632a3d15f2SJason Gunthorpe static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
1642a3d15f2SJason Gunthorpe {
1652a3d15f2SJason Gunthorpe 	return &mdev->uuid;
1662a3d15f2SJason Gunthorpe }
1677b96953bSKirti Wankhede 
1687b96953bSKirti Wankhede extern struct bus_type mdev_bus_type;
1697b96953bSKirti Wankhede 
17050732af3SParav Pandit int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
17150732af3SParav Pandit void mdev_unregister_device(struct device *dev);
1727b96953bSKirti Wankhede 
17391b9969dSJason Gunthorpe int mdev_register_driver(struct mdev_driver *drv);
17450732af3SParav Pandit void mdev_unregister_driver(struct mdev_driver *drv);
1757b96953bSKirti Wankhede 
17650732af3SParav Pandit struct device *mdev_parent_dev(struct mdev_device *mdev);
1772a3d15f2SJason Gunthorpe static inline struct device *mdev_dev(struct mdev_device *mdev)
1782a3d15f2SJason Gunthorpe {
1792a3d15f2SJason Gunthorpe 	return &mdev->dev;
1802a3d15f2SJason Gunthorpe }
1812a3d15f2SJason Gunthorpe static inline struct mdev_device *mdev_from_dev(struct device *dev)
1822a3d15f2SJason Gunthorpe {
1832a3d15f2SJason Gunthorpe 	return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
1842a3d15f2SJason Gunthorpe }
1859372e6feSAlex Williamson 
1867b96953bSKirti Wankhede #endif /* MDEV_H */
187