debugfs.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2023, HiSilicon Ltd.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/debugfs.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/vfio.h>
  9. #include "vfio.h"
  10. static struct dentry *vfio_debugfs_root;
  11. static int vfio_device_state_read(struct seq_file *seq, void *data)
  12. {
  13. struct device *vf_dev = seq->private;
  14. struct vfio_device *vdev = container_of(vf_dev,
  15. struct vfio_device, device);
  16. enum vfio_device_mig_state state;
  17. int ret;
  18. BUILD_BUG_ON(VFIO_DEVICE_STATE_NR !=
  19. VFIO_DEVICE_STATE_PRE_COPY_P2P + 1);
  20. ret = vdev->mig_ops->migration_get_state(vdev, &state);
  21. if (ret)
  22. return -EINVAL;
  23. switch (state) {
  24. case VFIO_DEVICE_STATE_ERROR:
  25. seq_puts(seq, "ERROR\n");
  26. break;
  27. case VFIO_DEVICE_STATE_STOP:
  28. seq_puts(seq, "STOP\n");
  29. break;
  30. case VFIO_DEVICE_STATE_RUNNING:
  31. seq_puts(seq, "RUNNING\n");
  32. break;
  33. case VFIO_DEVICE_STATE_STOP_COPY:
  34. seq_puts(seq, "STOP_COPY\n");
  35. break;
  36. case VFIO_DEVICE_STATE_RESUMING:
  37. seq_puts(seq, "RESUMING\n");
  38. break;
  39. case VFIO_DEVICE_STATE_RUNNING_P2P:
  40. seq_puts(seq, "RUNNING_P2P\n");
  41. break;
  42. case VFIO_DEVICE_STATE_PRE_COPY:
  43. seq_puts(seq, "PRE_COPY\n");
  44. break;
  45. case VFIO_DEVICE_STATE_PRE_COPY_P2P:
  46. seq_puts(seq, "PRE_COPY_P2P\n");
  47. break;
  48. default:
  49. seq_puts(seq, "Invalid\n");
  50. }
  51. return 0;
  52. }
  53. static int vfio_device_features_read(struct seq_file *seq, void *data)
  54. {
  55. struct device *vf_dev = seq->private;
  56. struct vfio_device *vdev = container_of(vf_dev, struct vfio_device, device);
  57. if (vdev->migration_flags & VFIO_MIGRATION_STOP_COPY)
  58. seq_puts(seq, "stop-copy\n");
  59. if (vdev->migration_flags & VFIO_MIGRATION_P2P)
  60. seq_puts(seq, "p2p\n");
  61. if (vdev->migration_flags & VFIO_MIGRATION_PRE_COPY)
  62. seq_puts(seq, "pre-copy\n");
  63. if (vdev->log_ops)
  64. seq_puts(seq, "dirty-tracking\n");
  65. return 0;
  66. }
  67. void vfio_device_debugfs_init(struct vfio_device *vdev)
  68. {
  69. struct device *dev = &vdev->device;
  70. vdev->debug_root = debugfs_create_dir(dev_name(vdev->dev),
  71. vfio_debugfs_root);
  72. if (vdev->mig_ops) {
  73. struct dentry *vfio_dev_migration = NULL;
  74. vfio_dev_migration = debugfs_create_dir("migration",
  75. vdev->debug_root);
  76. debugfs_create_devm_seqfile(dev, "state", vfio_dev_migration,
  77. vfio_device_state_read);
  78. debugfs_create_devm_seqfile(dev, "features", vfio_dev_migration,
  79. vfio_device_features_read);
  80. }
  81. }
  82. void vfio_device_debugfs_exit(struct vfio_device *vdev)
  83. {
  84. debugfs_remove_recursive(vdev->debug_root);
  85. }
  86. void vfio_debugfs_create_root(void)
  87. {
  88. vfio_debugfs_root = debugfs_create_dir("vfio", NULL);
  89. }
  90. void vfio_debugfs_remove_root(void)
  91. {
  92. debugfs_remove_recursive(vfio_debugfs_root);
  93. vfio_debugfs_root = NULL;
  94. }