blob: d75fc436574649a078ee83409e71dca0bfaee4f8 [file] [log] [blame]
Thomas Gleixner5dfa3c22019-05-29 16:57:56 -07001/* SPDX-License-Identifier: GPL-2.0-only */
Hollis Blancharde2174022007-12-03 15:30:24 -06002
3#ifndef __KVM_IODEV_H__
4#define __KVM_IODEV_H__
5
Avi Kivityedf88412007-12-16 11:02:48 +02006#include <linux/kvm_types.h>
Andre Przywaraaf669ac2015-03-26 14:39:29 +00007#include <linux/errno.h>
Hollis Blancharde2174022007-12-03 15:30:24 -06008
Gregory Haskinsd76685c42009-06-01 12:54:50 -04009struct kvm_io_device;
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +000010struct kvm_vcpu;
Gregory Haskinsd76685c42009-06-01 12:54:50 -040011
Michael S. Tsirkin69fa2d72009-06-29 22:24:07 +030012/**
13 * kvm_io_device_ops are called under kvm slots_lock.
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030014 * 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. Tsirkin69fa2d72009-06-29 22:24:07 +030016 **/
Gregory Haskinsd76685c42009-06-01 12:54:50 -040017struct kvm_io_device_ops {
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +000018 int (*read)(struct kvm_vcpu *vcpu,
19 struct kvm_io_device *this,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030020 gpa_t addr,
21 int len,
22 void *val);
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +000023 int (*write)(struct kvm_vcpu *vcpu,
24 struct kvm_io_device *this,
Hollis Blancharde2174022007-12-03 15:30:24 -060025 gpa_t addr,
26 int len,
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030027 const void *val);
Hollis Blancharde2174022007-12-03 15:30:24 -060028 void (*destructor)(struct kvm_io_device *this);
Hollis Blancharde2174022007-12-03 15:30:24 -060029};
30
Gregory Haskinsd76685c42009-06-01 12:54:50 -040031
32struct kvm_io_device {
33 const struct kvm_io_device_ops *ops;
34};
35
36static 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 Nikolaeve32edf42015-03-26 14:39:28 +000042static inline int kvm_iodevice_read(struct kvm_vcpu *vcpu,
43 struct kvm_io_device *dev, gpa_t addr,
44 int l, void *v)
Hollis Blancharde2174022007-12-03 15:30:24 -060045{
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +000046 return dev->ops->read ? dev->ops->read(vcpu, dev, addr, l, v)
47 : -EOPNOTSUPP;
Hollis Blancharde2174022007-12-03 15:30:24 -060048}
49
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +000050static 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 Blancharde2174022007-12-03 15:30:24 -060053{
Nikolay Nikolaeve32edf42015-03-26 14:39:28 +000054 return dev->ops->write ? dev->ops->write(vcpu, dev, addr, l, v)
55 : -EOPNOTSUPP;
Hollis Blancharde2174022007-12-03 15:30:24 -060056}
57
58static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
59{
Gregory Haskinsd76685c42009-06-01 12:54:50 -040060 if (dev->ops->destructor)
61 dev->ops->destructor(dev);
Hollis Blancharde2174022007-12-03 15:30:24 -060062}
63
64#endif /* __KVM_IODEV_H__ */