hypfs_diag0c.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Hypervisor filesystem for Linux on s390
  4. *
  5. * Diag 0C implementation
  6. *
  7. * Copyright IBM Corp. 2014
  8. */
  9. #include <linux/slab.h>
  10. #include <linux/cpu.h>
  11. #include <asm/diag.h>
  12. #include <asm/hypfs.h>
  13. #include "hypfs.h"
  14. #define DBFS_D0C_HDR_VERSION 0
  15. /*
  16. * Execute diagnose 0c in 31 bit mode
  17. */
  18. static void diag0c(struct hypfs_diag0c_entry *entry)
  19. {
  20. diag_stat_inc(DIAG_STAT_X00C);
  21. asm volatile (
  22. " sam31\n"
  23. " diag %0,%0,0x0c\n"
  24. " sam64\n"
  25. : /* no output register */
  26. : "a" (entry)
  27. : "memory");
  28. }
  29. /*
  30. * Get hypfs_diag0c_entry from CPU vector and store diag0c data
  31. */
  32. static void diag0c_fn(void *data)
  33. {
  34. diag0c(((void **) data)[smp_processor_id()]);
  35. }
  36. /*
  37. * Allocate buffer and store diag 0c data
  38. */
  39. static void *diag0c_store(unsigned int *count)
  40. {
  41. struct hypfs_diag0c_data *diag0c_data;
  42. unsigned int cpu_count, cpu, i;
  43. void **cpu_vec;
  44. get_online_cpus();
  45. cpu_count = num_online_cpus();
  46. cpu_vec = kmalloc_array(num_possible_cpus(), sizeof(*cpu_vec),
  47. GFP_KERNEL);
  48. if (!cpu_vec)
  49. goto fail_put_online_cpus;
  50. /* Note: Diag 0c needs 8 byte alignment and real storage */
  51. diag0c_data = kzalloc(sizeof(struct hypfs_diag0c_hdr) +
  52. cpu_count * sizeof(struct hypfs_diag0c_entry),
  53. GFP_KERNEL | GFP_DMA);
  54. if (!diag0c_data)
  55. goto fail_kfree_cpu_vec;
  56. i = 0;
  57. /* Fill CPU vector for each online CPU */
  58. for_each_online_cpu(cpu) {
  59. diag0c_data->entry[i].cpu = cpu;
  60. cpu_vec[cpu] = &diag0c_data->entry[i++];
  61. }
  62. /* Collect data all CPUs */
  63. on_each_cpu(diag0c_fn, cpu_vec, 1);
  64. *count = cpu_count;
  65. kfree(cpu_vec);
  66. put_online_cpus();
  67. return diag0c_data;
  68. fail_kfree_cpu_vec:
  69. kfree(cpu_vec);
  70. fail_put_online_cpus:
  71. put_online_cpus();
  72. return ERR_PTR(-ENOMEM);
  73. }
  74. /*
  75. * Hypfs DBFS callback: Free diag 0c data
  76. */
  77. static void dbfs_diag0c_free(const void *data)
  78. {
  79. kfree(data);
  80. }
  81. /*
  82. * Hypfs DBFS callback: Create diag 0c data
  83. */
  84. static int dbfs_diag0c_create(void **data, void **data_free_ptr, size_t *size)
  85. {
  86. struct hypfs_diag0c_data *diag0c_data;
  87. unsigned int count;
  88. diag0c_data = diag0c_store(&count);
  89. if (IS_ERR(diag0c_data))
  90. return PTR_ERR(diag0c_data);
  91. memset(&diag0c_data->hdr, 0, sizeof(diag0c_data->hdr));
  92. get_tod_clock_ext(diag0c_data->hdr.tod_ext);
  93. diag0c_data->hdr.len = count * sizeof(struct hypfs_diag0c_entry);
  94. diag0c_data->hdr.version = DBFS_D0C_HDR_VERSION;
  95. diag0c_data->hdr.count = count;
  96. *data = diag0c_data;
  97. *data_free_ptr = diag0c_data;
  98. *size = diag0c_data->hdr.len + sizeof(struct hypfs_diag0c_hdr);
  99. return 0;
  100. }
  101. /*
  102. * Hypfs DBFS file structure
  103. */
  104. static struct hypfs_dbfs_file dbfs_file_0c = {
  105. .name = "diag_0c",
  106. .data_create = dbfs_diag0c_create,
  107. .data_free = dbfs_diag0c_free,
  108. };
  109. /*
  110. * Initialize diag 0c interface for z/VM
  111. */
  112. int __init hypfs_diag0c_init(void)
  113. {
  114. if (!MACHINE_IS_VM)
  115. return 0;
  116. return hypfs_dbfs_create_file(&dbfs_file_0c);
  117. }
  118. /*
  119. * Shutdown diag 0c interface for z/VM
  120. */
  121. void hypfs_diag0c_exit(void)
  122. {
  123. if (!MACHINE_IS_VM)
  124. return;
  125. hypfs_dbfs_remove_file(&dbfs_file_0c);
  126. }