lgr.c 4.5 KB

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