lgr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/init.h>
  8. #include <linux/export.h>
  9. #include <linux/timer.h>
  10. #include <linux/slab.h>
  11. #include <asm/facility.h>
  12. #include <asm/sysinfo.h>
  13. #include <asm/ebcdic.h>
  14. #include <asm/debug.h>
  15. #include <asm/ipl.h>
  16. #define LGR_TIMER_INTERVAL_SECS (30 * 60)
  17. #define VM_LEVEL_MAX 2 /* Maximum is 8, but we only record two levels */
  18. /*
  19. * LGR info: Contains stfle and stsi data
  20. */
  21. struct lgr_info {
  22. /* Bit field with facility information: 4 DWORDs are stored */
  23. u64 stfle_fac_list[4];
  24. /* Level of system (1 = CEC, 2 = LPAR, 3 = z/VM */
  25. u32 level;
  26. /* Level 1: CEC info (stsi 1.1.1) */
  27. char manufacturer[16];
  28. char type[4];
  29. char sequence[16];
  30. char plant[4];
  31. char model[16];
  32. /* Level 2: LPAR info (stsi 2.2.2) */
  33. u16 lpar_number;
  34. char name[8];
  35. /* Level 3: VM info (stsi 3.2.2) */
  36. u8 vm_count;
  37. struct {
  38. char name[8];
  39. char cpi[16];
  40. } vm[VM_LEVEL_MAX];
  41. } __packed __aligned(8);
  42. /*
  43. * LGR globals
  44. */
  45. static char lgr_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  46. static struct lgr_info lgr_info_last;
  47. static struct lgr_info lgr_info_cur;
  48. static struct debug_info *lgr_dbf;
  49. /*
  50. * Copy buffer and then convert it to ASCII
  51. */
  52. static void cpascii(char *dst, char *src, int size)
  53. {
  54. memcpy(dst, src, size);
  55. EBCASC(dst, size);
  56. }
  57. /*
  58. * Fill LGR info with 1.1.1 stsi data
  59. */
  60. static void lgr_stsi_1_1_1(struct lgr_info *lgr_info)
  61. {
  62. struct sysinfo_1_1_1 *si = (void *) lgr_page;
  63. if (stsi(si, 1, 1, 1))
  64. return;
  65. cpascii(lgr_info->manufacturer, si->manufacturer,
  66. sizeof(si->manufacturer));
  67. cpascii(lgr_info->type, si->type, sizeof(si->type));
  68. cpascii(lgr_info->model, si->model, sizeof(si->model));
  69. cpascii(lgr_info->sequence, si->sequence, sizeof(si->sequence));
  70. cpascii(lgr_info->plant, si->plant, sizeof(si->plant));
  71. }
  72. /*
  73. * Fill LGR info with 2.2.2 stsi data
  74. */
  75. static void lgr_stsi_2_2_2(struct lgr_info *lgr_info)
  76. {
  77. struct sysinfo_2_2_2 *si = (void *) lgr_page;
  78. if (stsi(si, 2, 2, 2))
  79. return;
  80. cpascii(lgr_info->name, si->name, sizeof(si->name));
  81. memcpy(&lgr_info->lpar_number, &si->lpar_number,
  82. sizeof(lgr_info->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(unsigned long ignored)
  140. {
  141. lgr_info_log();
  142. lgr_timer_set();
  143. }
  144. static struct timer_list lgr_timer =
  145. TIMER_DEFERRED_INITIALIZER(lgr_timer_fn, 0, 0);
  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. lgr_timer_set();
  165. return 0;
  166. }
  167. device_initcall(lgr_init);