]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/mdev.h
KVM: arm64: Fix PMU probe ordering
[mirror_ubuntu-jammy-kernel.git] / include / linux / mdev.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
7b96953b
KW
2/*
3 * Mediated device definition
4 *
5 * Copyright (c) 2016, NVIDIA CORPORATION. All rights reserved.
6 * Author: Neo Jia <cjia@nvidia.com>
7 * Kirti Wankhede <kwankhede@nvidia.com>
7b96953b
KW
8 */
9
10#ifndef MDEV_H
11#define MDEV_H
12
417fd5bf
JG
13struct mdev_type;
14
2a3d15f2
JG
15struct mdev_device {
16 struct device dev;
2a3d15f2
JG
17 guid_t uuid;
18 void *driver_data;
19 struct list_head next;
417fd5bf 20 struct mdev_type *type;
2a3d15f2
JG
21 struct device *iommu_device;
22 bool active;
23};
24
25static inline struct mdev_device *to_mdev_device(struct device *dev)
26{
27 return container_of(dev, struct mdev_device, dev);
28}
7b96953b 29
8ac13175
LB
30/*
31 * Called by the parent device driver to set the device which represents
32 * this mdev in iommu protection scope. By default, the iommu device is
33 * NULL, that indicates using vendor defined isolation.
34 *
35 * @dev: the mediated device that iommu will isolate.
36 * @iommu_device: a pci device which represents the iommu for @dev.
8ac13175 37 */
2a3d15f2
JG
38static inline void mdev_set_iommu_device(struct mdev_device *mdev,
39 struct device *iommu_device)
40{
41 mdev->iommu_device = iommu_device;
42}
8ac13175 43
2a3d15f2
JG
44static inline struct device *mdev_get_iommu_device(struct mdev_device *mdev)
45{
46 return mdev->iommu_device;
47}
8ac13175 48
15fcc44b 49unsigned int mdev_get_type_group_id(struct mdev_device *mdev);
9169cff1
JG
50unsigned int mtype_get_type_group_id(struct mdev_type *mtype);
51struct device *mtype_get_parent_dev(struct mdev_type *mtype);
15fcc44b 52
7b96953b 53/**
42930553 54 * struct mdev_parent_ops - Structure to be registered for each parent device to
7b96953b
KW
55 * register the device to mdev module.
56 *
57 * @owner: The module owner.
88a21f26 58 * @device_driver: Which device driver to probe() on newly created devices
7b96953b
KW
59 * @dev_attr_groups: Attributes of the parent device.
60 * @mdev_attr_groups: Attributes of the mediated device.
61 * @supported_type_groups: Attributes to define supported types. It is mandatory
62 * to provide supported types.
63 * @create: Called to allocate basic resources in parent device's
64 * driver for a particular mediated device. It is
65 * mandatory to provide create ops.
7b96953b
KW
66 * @mdev: mdev_device structure on of mediated device
67 * that is being created
68 * Returns integer: success (0) or error (< 0)
df54714f 69 * @remove: Called to free resources in parent device's driver for
7b96953b
KW
70 * a mediated device. It is mandatory to provide 'remove'
71 * ops.
72 * @mdev: mdev_device device structure which is being
73 * destroyed
74 * Returns integer: success (0) or error (< 0)
7b96953b
KW
75 * @read: Read emulation callback
76 * @mdev: mediated device structure
77 * @buf: read buffer
78 * @count: number of bytes to read
79 * @ppos: address.
80 * Retuns number on bytes read on success or error.
81 * @write: Write emulation callback
82 * @mdev: mediated device structure
83 * @buf: write buffer
84 * @count: number of bytes to be written
85 * @ppos: address.
86 * Retuns number on bytes written on success or error.
87 * @ioctl: IOCTL callback
88 * @mdev: mediated device structure
89 * @cmd: ioctl command
90 * @arg: arguments to ioctl
91 * @mmap: mmap callback
92 * @mdev: mediated device structure
93 * @vma: vma structure
a15ac665
EF
94 * @request: request callback to release device
95 * @mdev: mediated device structure
96 * @count: request sequence number
7b96953b 97 * Parent device that support mediated device should be registered with mdev
42930553 98 * module with mdev_parent_ops structure.
7b96953b 99 **/
42930553 100struct mdev_parent_ops {
7b96953b 101 struct module *owner;
88a21f26 102 struct mdev_driver *device_driver;
7b96953b
KW
103 const struct attribute_group **dev_attr_groups;
104 const struct attribute_group **mdev_attr_groups;
105 struct attribute_group **supported_type_groups;
106
c2ef2f50 107 int (*create)(struct mdev_device *mdev);
7b96953b 108 int (*remove)(struct mdev_device *mdev);
2fd585f4
JG
109 int (*open_device)(struct mdev_device *mdev);
110 void (*close_device)(struct mdev_device *mdev);
7b96953b
KW
111 ssize_t (*read)(struct mdev_device *mdev, char __user *buf,
112 size_t count, loff_t *ppos);
113 ssize_t (*write)(struct mdev_device *mdev, const char __user *buf,
114 size_t count, loff_t *ppos);
c6ef7fd4 115 long (*ioctl)(struct mdev_device *mdev, unsigned int cmd,
7b96953b
KW
116 unsigned long arg);
117 int (*mmap)(struct mdev_device *mdev, struct vm_area_struct *vma);
a15ac665 118 void (*request)(struct mdev_device *mdev, unsigned int count);
7b96953b
KW
119};
120
121/* interface for exporting mdev supported type attributes */
122struct mdev_type_attribute {
123 struct attribute attr;
9169cff1
JG
124 ssize_t (*show)(struct mdev_type *mtype,
125 struct mdev_type_attribute *attr, char *buf);
126 ssize_t (*store)(struct mdev_type *mtype,
127 struct mdev_type_attribute *attr, const char *buf,
128 size_t count);
7b96953b
KW
129};
130
131#define MDEV_TYPE_ATTR(_name, _mode, _show, _store) \
132struct mdev_type_attribute mdev_type_attr_##_name = \
133 __ATTR(_name, _mode, _show, _store)
134#define MDEV_TYPE_ATTR_RW(_name) \
135 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RW(_name)
136#define MDEV_TYPE_ATTR_RO(_name) \
137 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_RO(_name)
138#define MDEV_TYPE_ATTR_WO(_name) \
139 struct mdev_type_attribute mdev_type_attr_##_name = __ATTR_WO(_name)
140
141/**
142 * struct mdev_driver - Mediated device driver
7b96953b
KW
143 * @probe: called when new device created
144 * @remove: called when device removed
145 * @driver: device driver structure
146 *
147 **/
148struct mdev_driver {
2a3d15f2
JG
149 int (*probe)(struct mdev_device *dev);
150 void (*remove)(struct mdev_device *dev);
7b96953b
KW
151 struct device_driver driver;
152};
153
2a3d15f2
JG
154static inline void *mdev_get_drvdata(struct mdev_device *mdev)
155{
156 return mdev->driver_data;
157}
158static inline void mdev_set_drvdata(struct mdev_device *mdev, void *data)
159{
160 mdev->driver_data = data;
161}
162static inline const guid_t *mdev_uuid(struct mdev_device *mdev)
163{
164 return &mdev->uuid;
165}
7b96953b
KW
166
167extern struct bus_type mdev_bus_type;
168
50732af3
PP
169int mdev_register_device(struct device *dev, const struct mdev_parent_ops *ops);
170void mdev_unregister_device(struct device *dev);
7b96953b 171
91b9969d 172int mdev_register_driver(struct mdev_driver *drv);
50732af3 173void mdev_unregister_driver(struct mdev_driver *drv);
7b96953b 174
50732af3 175struct device *mdev_parent_dev(struct mdev_device *mdev);
2a3d15f2
JG
176static inline struct device *mdev_dev(struct mdev_device *mdev)
177{
178 return &mdev->dev;
179}
180static inline struct mdev_device *mdev_from_dev(struct device *dev)
181{
182 return dev->bus == &mdev_bus_type ? to_mdev_device(dev) : NULL;
183}
9372e6fe 184
7b96953b 185#endif /* MDEV_H */