Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License as published by |
| 4 | * the Free Software Foundation; either version 2 of the License. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * You should have received a copy of the GNU General Public License |
| 12 | * along with this program; if not, write to the Free Software |
| 13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 14 | */ |
| 15 | |
| 16 | #ifndef __KVM_IODEV_H__ |
| 17 | #define __KVM_IODEV_H__ |
| 18 | |
Avi Kivity | edf8841 | 2007-12-16 11:02:48 +0200 | [diff] [blame] | 19 | #include <linux/kvm_types.h> |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 20 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 21 | struct kvm_io_device; |
| 22 | |
Michael S. Tsirkin | 69fa2d7 | 2009-06-29 22:24:07 +0300 | [diff] [blame] | 23 | /** |
| 24 | * kvm_io_device_ops are called under kvm slots_lock. |
| 25 | **/ |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 26 | struct kvm_io_device_ops { |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 27 | void (*read)(struct kvm_io_device *this, |
| 28 | gpa_t addr, |
| 29 | int len, |
| 30 | void *val); |
| 31 | void (*write)(struct kvm_io_device *this, |
| 32 | gpa_t addr, |
| 33 | int len, |
| 34 | const void *val); |
Laurent Vivier | 9276049 | 2008-05-30 16:05:53 +0200 | [diff] [blame] | 35 | int (*in_range)(struct kvm_io_device *this, gpa_t addr, int len, |
| 36 | int is_write); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 37 | void (*destructor)(struct kvm_io_device *this); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 38 | }; |
| 39 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 40 | |
| 41 | struct kvm_io_device { |
| 42 | const struct kvm_io_device_ops *ops; |
| 43 | }; |
| 44 | |
| 45 | static inline void kvm_iodevice_init(struct kvm_io_device *dev, |
| 46 | const struct kvm_io_device_ops *ops) |
| 47 | { |
| 48 | dev->ops = ops; |
| 49 | } |
| 50 | |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 51 | static inline void kvm_iodevice_read(struct kvm_io_device *dev, |
| 52 | gpa_t addr, |
| 53 | int len, |
| 54 | void *val) |
| 55 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 56 | dev->ops->read(dev, addr, len, val); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | static inline void kvm_iodevice_write(struct kvm_io_device *dev, |
| 60 | gpa_t addr, |
| 61 | int len, |
| 62 | const void *val) |
| 63 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 64 | dev->ops->write(dev, addr, len, val); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 65 | } |
| 66 | |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 67 | static inline int kvm_iodevice_in_range(struct kvm_io_device *dev, |
| 68 | gpa_t addr, int len, int is_write) |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 69 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 70 | return dev->ops->in_range(dev, addr, len, is_write); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static inline void kvm_iodevice_destructor(struct kvm_io_device *dev) |
| 74 | { |
Gregory Haskins | d76685c | 2009-06-01 12:54:50 -0400 | [diff] [blame] | 75 | if (dev->ops->destructor) |
| 76 | dev->ops->destructor(dev); |
Hollis Blanchard | e217402 | 2007-12-03 15:30:24 -0600 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | #endif /* __KVM_IODEV_H__ */ |