lgr.c 4.5 KB

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