iodev.h 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef __KVM_IODEV_H__
  3. #define __KVM_IODEV_H__
  4. #include <linux/kvm_types.h>
  5. #include <linux/errno.h>
  6. struct kvm_io_device;
  7. struct kvm_vcpu;
  8. /**
  9. * kvm_io_device_ops are called under kvm slots_lock.
  10. * read and write handlers return 0 if the transaction has been handled,
  11. * or non-zero to have it passed to the next device.
  12. **/
  13. struct kvm_io_device_ops {
  14. int (*read)(struct kvm_vcpu *vcpu,
  15. struct kvm_io_device *this,
  16. gpa_t addr,
  17. int len,
  18. void *val);
  19. int (*write)(struct kvm_vcpu *vcpu,
  20. struct kvm_io_device *this,
  21. gpa_t addr,
  22. int len,
  23. const void *val);
  24. void (*destructor)(struct kvm_io_device *this);
  25. };
  26. struct kvm_io_device {
  27. const struct kvm_io_device_ops *ops;
  28. };
  29. static inline void kvm_iodevice_init(struct kvm_io_device *dev,
  30. const struct kvm_io_device_ops *ops)
  31. {
  32. dev->ops = ops;
  33. }
  34. static inline int kvm_iodevice_read(struct kvm_vcpu *vcpu,
  35. struct kvm_io_device *dev, gpa_t addr,
  36. int l, void *v)
  37. {
  38. return dev->ops->read ? dev->ops->read(vcpu, dev, addr, l, v)
  39. : -EOPNOTSUPP;
  40. }
  41. static inline int kvm_iodevice_write(struct kvm_vcpu *vcpu,
  42. struct kvm_io_device *dev, gpa_t addr,
  43. int l, const void *v)
  44. {
  45. return dev->ops->write ? dev->ops->write(vcpu, dev, addr, l, v)
  46. : -EOPNOTSUPP;
  47. }
  48. static inline void kvm_iodevice_destructor(struct kvm_io_device *dev)
  49. {
  50. if (dev->ops->destructor)
  51. dev->ops->destructor(dev);
  52. }
  53. #endif /* __KVM_IODEV_H__ */