lgr.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 char lgr_page[PAGE_SIZE] __aligned(PAGE_SIZE);
  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. * Copy buffer and then convert it to ASCII
  50. */
  51. static void cpascii(char *dst, char *src, int size)
  52. {
  53. memcpy(dst, src, size);
  54. EBCASC(dst, size);
  55. }
  56. /*
  57. * Fill LGR info with 1.1.1 stsi data
  58. */
  59. static void lgr_stsi_1_1_1(struct lgr_info *lgr_info)
  60. {
  61. struct sysinfo_1_1_1 *si = (void *) lgr_page;
  62. if (stsi(si, 1, 1, 1))
  63. return;
  64. cpascii(lgr_info->manufacturer, si->manufacturer,
  65. sizeof(si->manufacturer));
  66. cpascii(lgr_info->type, si->type, sizeof(si->type));
  67. cpascii(lgr_info->model, si->model, sizeof(si->model));
  68. cpascii(lgr_info->sequence, si->sequence, sizeof(si->sequence));
  69. cpascii(lgr_info->plant, si->plant, sizeof(si->plant));
  70. }
  71. /*
  72. * Fill LGR info with 2.2.2 stsi data
  73. */
  74. static void lgr_stsi_2_2_2(struct lgr_info *lgr_info)
  75. {
  76. struct sysinfo_2_2_2 *si = (void *) lgr_page;
  77. if (stsi(si, 2, 2, 2))
  78. return;
  79. cpascii(lgr_info->name, si->name, sizeof(si->name));
  80. memcpy(&lgr_info->lpar_number, &si->lpar_number,
  81. sizeof(lgr_info->lpar_number));
  82. }
  83. /*
  84. * Fill LGR info with 3.2.2 stsi data
  85. */
  86. static void lgr_stsi_3_2_2(struct lgr_info *lgr_info)
  87. {
  88. struct sysinfo_3_2_2 *si = (void *) lgr_page;
  89. int i;
  90. if (stsi(si, 3, 2, 2))
  91. return;
  92. for (i = 0; i < min_t(u8, si->count, VM_LEVEL_MAX); i++) {
  93. cpascii(lgr_info->vm[i].name, si->vm[i].name,
  94. sizeof(si->vm[i].name));
  95. cpascii(lgr_info->vm[i].cpi, si->vm[i].cpi,
  96. sizeof(si->vm[i].cpi));
  97. }
  98. lgr_info->vm_count = si->count;
  99. }
  100. /*
  101. * Fill LGR info with current data
  102. */
  103. static void lgr_info_get(struct lgr_info *lgr_info)
  104. {
  105. int level;
  106. memset(lgr_info, 0, sizeof(*lgr_info));
  107. stfle(lgr_info->stfle_fac_list, ARRAY_SIZE(lgr_info->stfle_fac_list));
  108. level = stsi(NULL, 0, 0, 0);
  109. lgr_info->level = level;
  110. if (level >= 1)
  111. lgr_stsi_1_1_1(lgr_info);
  112. if (level >= 2)
  113. lgr_stsi_2_2_2(lgr_info);
  114. if (level >= 3)
  115. lgr_stsi_3_2_2(lgr_info);
  116. }
  117. /*
  118. * Check if LGR info has changed and if yes log new LGR info to s390dbf
  119. */
  120. void lgr_info_log(void)
  121. {
  122. static DEFINE_SPINLOCK(lgr_info_lock);
  123. unsigned long flags;
  124. if (!spin_trylock_irqsave(&lgr_info_lock, flags))
  125. return;
  126. lgr_info_get(&lgr_info_cur);
  127. if (memcmp(&lgr_info_last, &lgr_info_cur, sizeof(lgr_info_cur)) != 0) {
  128. debug_event(lgr_dbf, 1, &lgr_info_cur, sizeof(lgr_info_cur));
  129. lgr_info_last = lgr_info_cur;
  130. }
  131. spin_unlock_irqrestore(&lgr_info_lock, flags);
  132. }
  133. EXPORT_SYMBOL_GPL(lgr_info_log);
  134. static void lgr_timer_set(void);
  135. /*
  136. * LGR timer callback
  137. */
  138. static void lgr_timer_fn(unsigned long ignored)
  139. {
  140. lgr_info_log();
  141. lgr_timer_set();
  142. }
  143. static struct timer_list lgr_timer =
  144. TIMER_DEFERRED_INITIALIZER(lgr_timer_fn, 0, 0);
  145. /*
  146. * Setup next LGR timer
  147. */
  148. static void lgr_timer_set(void)
  149. {
  150. mod_timer(&lgr_timer, jiffies + LGR_TIMER_INTERVAL_SECS * HZ);
  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. lgr_timer_set();
  164. return 0;
  165. }
  166. module_init(lgr_init);