iodev.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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, see <http://www.gnu.org/licenses/>.
  13. */
  14. #ifndef __KVM_IODEV_H__
  15. #define __KVM_IODEV_H__
  16. #include <linux/kvm_types.h>
  17. #include <linux/errno.h>
  18. struct kvm_io_device;
  19. struct kvm_vcpu;
  20. /**
  21. * kvm_io_device_ops are called under kvm slots_lock.
  22. * read and write handlers return 0 if the transaction has been handled,
  23. * or non-zero to have it passed to the next device.
  24. **/
  25. struct kvm_io_device_ops {
  26. int (*read)(struct kvm_vcpu *vcpu,
  27. struct kvm_io_device *this,
  28. gpa_t addr,
  29. int len,
  30. void *val);
  31. int (*write)(struct kvm_vcpu *vcpu,
  32. struct kvm_io_device *this,
  33. gpa_t addr,
  34. int len,
  35. const void *val);
  36. void (*destructor)(struct kvm_io_device *this);
  37. };
  38. struct kvm_io_device {
  39. const struct kvm_io_device_ops *ops;
  40. };
  41. static inline void kvm_iodevice_init(struct kvm_io_device *dev,
  42. const struct kvm_io_device_ops *ops)
  43. {
  44. dev->ops = ops;
  45. }
  46. static inline int kvm_iodevice_read(struct kvm_vcpu *vcpu,
  47. struct kvm_io_device *dev, gpa_t addr,
  48. int l, void *v)
  49. {
  50. return dev->ops->read ? dev->ops->read(vcpu, dev, addr, l, v)
  51. : -EOPNOTSUPP;
  52. }
  53. static inline int kvm_iodevice_write(struct kvm_vcpu *vcpu,
  54. struct kvm_io_device *dev, gpa_t addr,
  55. int l, const void *v)
  56. {
  57. return dev->ops->write ? dev->ops->write(vcpu, dev, addr, l, v)
  58. : -EOPNOTSUPP;
  59. }
  60. static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
  61. {
  62. if (dev->ops->destructor)
  63. dev->ops->destructor(dev);
  64. }
  65. #endif /* __KVM_IODEV_H__ */