lgr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Linux Guest Relocation (LGR) detection
  4. *
  5. * Copyright IBM Corp. 2012
  6. * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/export.h>
  10. #include <linux/timer.h>
  11. #include <linux/slab.h>
  12. #include <asm/facility.h>
  13. #include <asm/sysinfo.h>
  14. #include <asm/ebcdic.h>
  15. #include <asm/debug.h>
  16. #include <asm/ipl.h>
  17. #define LGR_TIMER_INTERVAL_SECS (30 * 60)
  18. #define VM_LEVEL_MAX 2 /* Maximum is 8, but we only record two levels */
  19. /*
  20. * LGR info: Contains stfle and stsi data
  21. */
  22. struct lgr_info {
  23. /* Bit field with facility information: 4 DWORDs are stored */
  24. u64 stfle_fac_list[4];
  25. /* Level of system (1 = CEC, 2 = LPAR, 3 = z/VM */
  26. u32 level;
  27. /* Level 1: CEC info (stsi 1.1.1) */
  28. char manufacturer[16];
  29. char type[4];
  30. char sequence[16];
  31. char plant[4];
  32. char model[16];
  33. /* Level 2: LPAR info (stsi 2.2.2) */
  34. u16 lpar_number;
  35. char name[8];
  36. /* Level 3: VM info (stsi 3.2.2) */
  37. u8 vm_count;
  38. struct {
  39. char name[8];
  40. char cpi[16];
  41. } vm[VM_LEVEL_MAX];
  42. } __packed __aligned(8);
  43. /*
  44. * LGR globals
  45. */
  46. static char lgr_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  47. static struct lgr_info lgr_info_last;
  48. static struct lgr_info lgr_info_cur;
  49. static struct debug_info *lgr_dbf;
  50. /*
  51. * Copy buffer and then convert it to ASCII
  52. */
  53. static void cpascii(char *dst, char *src, int size)
  54. {
  55. memcpy(dst, src, size);
  56. EBCASC(dst, size);
  57. }
  58. /*
  59. * Fill LGR info with 1.1.1 stsi data
  60. */
  61. static void lgr_stsi_1_1_1(struct lgr_info *lgr_info)
  62. {
  63. struct sysinfo_1_1_1 *si = (void *) lgr_page;
  64. if (stsi(si, 1, 1, 1))
  65. return;
  66. cpascii(lgr_info->manufacturer, si->manufacturer,
  67. sizeof(si->manufacturer));
  68. cpascii(lgr_info->type, si->type, sizeof(si->type));
  69. cpascii(lgr_info->model, si->model, sizeof(si->model));
  70. cpascii(lgr_info->sequence, si->sequence, sizeof(si->sequence));
  71. cpascii(lgr_info->plant, si->plant, sizeof(si->plant));
  72. }
  73. /*
  74. * Fill LGR info with 2.2.2 stsi data
  75. */
  76. static void lgr_stsi_2_2_2(struct lgr_info *lgr_info)
  77. {
  78. struct sysinfo_2_2_2 *si = (void *) lgr_page;
  79. if (stsi(si, 2, 2, 2))
  80. return;
  81. cpascii(lgr_info->name, si->name, sizeof(si->name));
  82. memcpy(&lgr_info->lpar_number, &si->lpar_number,
  83. sizeof(lgr_info->lpar_number));
  84. }
  85. /*
  86. * Fill LGR info with 3.2.2 stsi data
  87. */
  88. static void lgr_stsi_3_2_2(struct lgr_info *lgr_info)
  89. {
  90. struct sysinfo_3_2_2 *si = (void *) lgr_page;
  91. int i;
  92. if (stsi(si, 3, 2, 2))
  93. return;
  94. for (i = 0; i < min_t(u8, si->count, VM_LEVEL_MAX); i++) {
  95. cpascii(lgr_info->vm[i].name, si->vm[i].name,
  96. sizeof(si->vm[i].name));
  97. cpascii(lgr_info->vm[i].cpi, si->vm[i].cpi,
  98. sizeof(si->vm[i].cpi));
  99. }
  100. lgr_info->vm_count = si->count;
  101. }
  102. /*
  103. * Fill LGR info with current data
  104. */
  105. static void lgr_info_get(struct lgr_info *lgr_info)
  106. {
  107. int level;
  108. memset(lgr_info, 0, sizeof(*lgr_info));
  109. stfle(lgr_info->stfle_fac_list, ARRAY_SIZE(lgr_info->stfle_fac_list));
  110. level = stsi(NULL, 0, 0, 0);
  111. lgr_info->level = level;
  112. if (level >= 1)
  113. lgr_stsi_1_1_1(lgr_info);
  114. if (level >= 2)
  115. lgr_stsi_2_2_2(lgr_info);
  116. if (level >= 3)
  117. lgr_stsi_3_2_2(lgr_info);
  118. }
  119. /*
  120. * Check if LGR info has changed and if yes log new LGR info to s390dbf
  121. */
  122. void lgr_info_log(void)
  123. {
  124. static DEFINE_SPINLOCK(lgr_info_lock);
  125. unsigned long flags;
  126. if (!spin_trylock_irqsave(&lgr_info_lock, flags))
  127. return;
  128. lgr_info_get(&lgr_info_cur);
  129. if (memcmp(&lgr_info_last, &lgr_info_cur, sizeof(lgr_info_cur)) != 0) {
  130. debug_event(lgr_dbf, 1, &lgr_info_cur, sizeof(lgr_info_cur));
  131. lgr_info_last = lgr_info_cur;
  132. }
  133. spin_unlock_irqrestore(&lgr_info_lock, flags);
  134. }
  135. EXPORT_SYMBOL_GPL(lgr_info_log);
  136. static void lgr_timer_set(void);
  137. /*
  138. * LGR timer callback
  139. */
  140. static void lgr_timer_fn(struct timer_list *unused)
  141. {
  142. lgr_info_log();
  143. lgr_timer_set();
  144. }
  145. static struct timer_list lgr_timer;
  146. /*
  147. * Setup next LGR timer
  148. */
  149. static void lgr_timer_set(void)
  150. {
  151. mod_timer(&lgr_timer, jiffies + LGR_TIMER_INTERVAL_SECS * HZ);
  152. }
  153. /*
  154. * Initialize LGR: Add s390dbf, write initial lgr_info and setup timer
  155. */
  156. static int __init lgr_init(void)
  157. {
  158. lgr_dbf = debug_register("lgr", 1, 1, sizeof(struct lgr_info));
  159. if (!lgr_dbf)
  160. return -ENOMEM;
  161. debug_register_view(lgr_dbf, &debug_hex_ascii_view);
  162. lgr_info_get(&lgr_info_last);
  163. debug_event(lgr_dbf, 1, &lgr_info_last, sizeof(lgr_info_last));
  164. timer_setup(&lgr_timer, lgr_timer_fn, TIMER_DEFERRABLE);
  165. lgr_timer_set();
  166. return 0;
  167. }
  168. device_initcall(lgr_init);