Thomas Gleixner | 5dfa3c2 | 2019-05-29 16:57:56 -0700 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 2 | |
| 3 | #ifndef __KVM_IODEV_H__ |
| 4 | #define __KVM_IODEV_H__ |
| 5 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 6 | #include <linux/kvm_types.h> |
Andre Przywara | af669ac | 2015-03-26 14:39:29 +0000 | [diff] [blame] | 7 | #include <linux/errno.h> |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 8 | |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 9 | struct kvm_io_device; |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 10 | struct kvm_vcpu; |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 11 | |
Michael S. Tsirkin | 69fa2d7 | 2009-06-29 22:24:07 +0300 | [diff] [blame] | 12 | /** |
| 13 | * kvm_io_device_ops are called under kvm slots_lock. |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 14 | * read and write handlers return 0 if the transaction has been handled, |
| 15 | * or non-zero to have it passed to the next device. |
Michael S. Tsirkin | 69fa2d7 | 2009-06-29 22:24:07 +0300 | [diff] [blame] | 16 | **/ |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 17 | struct kvm_io_device_ops { |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 18 | int (*read)(struct kvm_vcpu *vcpu, |
| 19 | struct kvm_io_device *this, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 20 | gpa_t addr, |
| 21 | int len, |
| 22 | void *val); |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 23 | int (*write)(struct kvm_vcpu *vcpu, |
| 24 | struct kvm_io_device *this, |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 25 | gpa_t addr, |
| 26 | int len, |
Michael S. Tsirkin | bda9020 | 2009-06-29 22:24:32 +0300 | [diff] [blame] | 27 | const void *val); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 28 | void (*destructor)(struct kvm_io_device *this); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 29 | }; |
| 30 | |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 31 | |
| 32 | struct kvm_io_device { |
| 33 | const struct kvm_io_device_ops *ops; |
| 34 | }; |
| 35 | |
| 36 | static inline void kvm_iodevice_init(struct kvm_io_device *dev, |
| 37 | const struct kvm_io_device_ops *ops) |
| 38 | { |
| 39 | dev->ops = ops; |
| 40 | } |
| 41 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 42 | static inline int kvm_iodevice_read(struct kvm_vcpu *vcpu, |
| 43 | struct kvm_io_device *dev, gpa_t addr, |
| 44 | int l, void *v) |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 45 | { |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 46 | return dev->ops->read ? dev->ops->read(vcpu, dev, addr, l, v) |
| 47 | : -EOPNOTSUPP; |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 48 | } |
| 49 | |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 50 | static inline int kvm_iodevice_write(struct kvm_vcpu *vcpu, |
| 51 | struct kvm_io_device *dev, gpa_t addr, |
| 52 | int l, const void *v) |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 53 | { |
Nikolay Nikolaev | e32edf4 | 2015-03-26 14:39:28 +0000 | [diff] [blame] | 54 | return dev->ops->write ? dev->ops->write(vcpu, dev, addr, l, v) |
| 55 | : -EOPNOTSUPP; |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static inline void kvm_iodevice_destructor(struct kvm_io_device *dev) |
| 59 | { |
Gregory Haskins | d76685c4 | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 60 | if (dev->ops->destructor) |
| 61 | dev->ops->destructor(dev); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | #endif /* __KVM_IODEV_H__ */ |