lgr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 <[email protected]>
  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. lgr_info->lpar_number = si->lpar_number;
  83. }
  84. /*
  85. * Fill LGR info with 3.2.2 stsi data
  86. */
  87. static void lgr_stsi_3_2_2(struct lgr_info *lgr_info)
  88. {
  89. struct sysinfo_3_2_2 *si = (void *) lgr_page;
  90. int i;
  91. if (stsi(si, 3, 2, 2))
  92. return;
  93. for (i = 0; i < min_t(u8, si->count, VM_LEVEL_MAX); i++) {
  94. cpascii(lgr_info->vm[i].name, si->vm[i].name,
  95. sizeof(si->vm[i].name));
  96. cpascii(lgr_info->vm[i].cpi, si->vm[i].cpi,
  97. sizeof(si->vm[i].cpi));
  98. }
  99. lgr_info->vm_count = si->count;
  100. }
  101. /*
  102. * Fill LGR info with current data
  103. */
  104. static void lgr_info_get(struct lgr_info *lgr_info)
  105. {
  106. int level;
  107. memset(lgr_info, 0, sizeof(*lgr_info));
  108. stfle(lgr_info->stfle_fac_list, ARRAY_SIZE(lgr_info->stfle_fac_list));
  109. level = stsi(NULL, 0, 0, 0);
  110. lgr_info->level = level;
  111. if (level >= 1)
  112. lgr_stsi_1_1_1(lgr_info);
  113. if (level >= 2)
  114. lgr_stsi_2_2_2(lgr_info);
  115. if (level >= 3)
  116. lgr_stsi_3_2_2(lgr_info);
  117. }
  118. /*
  119. * Check if LGR info has changed and if yes log new LGR info to s390dbf
  120. */
  121. void lgr_info_log(void)
  122. {
  123. static DEFINE_SPINLOCK(lgr_info_lock);
  124. unsigned long flags;
  125. if (!spin_trylock_irqsave(&lgr_info_lock, flags))
  126. return;
  127. lgr_info_get(&lgr_info_cur);
  128. if (memcmp(&lgr_info_last, &lgr_info_cur, sizeof(lgr_info_cur)) != 0) {
  129. debug_event(lgr_dbf, 1, &lgr_info_cur, sizeof(lgr_info_cur));
  130. lgr_info_last = lgr_info_cur;
  131. }
  132. spin_unlock_irqrestore(&lgr_info_lock, flags);
  133. }
  134. EXPORT_SYMBOL_GPL(lgr_info_log);
  135. static void lgr_timer_set(void);
  136. /*
  137. * LGR timer callback
  138. */
  139. static void lgr_timer_fn(struct timer_list *unused)
  140. {
  141. lgr_info_log();
  142. lgr_timer_set();
  143. }
  144. static struct timer_list lgr_timer;
  145. /*
  146. * Setup next LGR timer
  147. */
  148. static void lgr_timer_set(void)
  149. {
  150. mod_timer(&lgr_timer, jiffies + msecs_to_jiffies(LGR_TIMER_INTERVAL_SECS * MSEC_PER_SEC));
  151. }
  152. /*
  153. * Initialize LGR: Add s390dbf, write initial lgr_info and setup timer
  154. */
  155. static int __init lgr_init(void)
  156. {
  157. lgr_dbf = debug_register("lgr", 1, 1, sizeof(struct lgr_info));
  158. if (!lgr_dbf)
  159. return -ENOMEM;
  160. debug_register_view(lgr_dbf, &debug_hex_ascii_view);
  161. lgr_info_get(&lgr_info_last);
  162. debug_event(lgr_dbf, 1, &lgr_info_last, sizeof(lgr_info_last));
  163. timer_setup(&lgr_timer, lgr_timer_fn, TIMER_DEFERRABLE);
  164. lgr_timer_set();
  165. return 0;
  166. }
  167. device_initcall(lgr_init);