2 * Linux Guest Relocation (LGR) detection
4 * Copyright IBM Corp. 2012
5 * Author(s): Michael Holzheu <holzheu@linux.vnet.ibm.com>
8 #include <linux/module.h>
9 #include <linux/timer.h>
10 #include <linux/slab.h>
11 #include <asm/sysinfo.h>
12 #include <asm/ebcdic.h>
13 #include <asm/debug.h>
16 #define LGR_TIMER_INTERVAL_SECS (30 * 60)
17 #define VM_LEVEL_MAX 2 /* Maximum is 8, but we only record two levels */
20 * LGR info: Contains stfle and stsi data
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 */
27 /* Level 1: CEC info (stsi 1.1.1) */
28 char manufacturer[16];
33 /* Level 2: LPAR info (stsi 2.2.2) */
36 /* Level 3: VM info (stsi 3.2.2) */
42 } __packed __aligned(8);
47 static void *lgr_page;
48 static struct lgr_info lgr_info_last;
49 static struct lgr_info lgr_info_cur;
50 static struct debug_info *lgr_dbf;
53 * Return number of valid stsi levels
55 static inline int stsi_0(void)
57 int rc = stsi(NULL, 0, 0, 0);
59 return rc == -ENOSYS ? rc : (((unsigned int) rc) >> 28);
63 * Copy buffer and then convert it to ASCII
65 static void cpascii(char *dst, char *src, int size)
67 memcpy(dst, src, size);
72 * Fill LGR info with 1.1.1 stsi data
74 static void lgr_stsi_1_1_1(struct lgr_info *lgr_info)
76 struct sysinfo_1_1_1 *si = lgr_page;
78 if (stsi(si, 1, 1, 1) == -ENOSYS)
80 cpascii(lgr_info->manufacturer, si->manufacturer,
81 sizeof(si->manufacturer));
82 cpascii(lgr_info->type, si->type, sizeof(si->type));
83 cpascii(lgr_info->model, si->model, sizeof(si->model));
84 cpascii(lgr_info->sequence, si->sequence, sizeof(si->sequence));
85 cpascii(lgr_info->plant, si->plant, sizeof(si->plant));
89 * Fill LGR info with 2.2.2 stsi data
91 static void lgr_stsi_2_2_2(struct lgr_info *lgr_info)
93 struct sysinfo_2_2_2 *si = lgr_page;
95 if (stsi(si, 2, 2, 2) == -ENOSYS)
97 cpascii(lgr_info->name, si->name, sizeof(si->name));
98 memcpy(&lgr_info->lpar_number, &si->lpar_number,
99 sizeof(lgr_info->lpar_number));
103 * Fill LGR info with 3.2.2 stsi data
105 static void lgr_stsi_3_2_2(struct lgr_info *lgr_info)
107 struct sysinfo_3_2_2 *si = lgr_page;
110 if (stsi(si, 3, 2, 2) == -ENOSYS)
112 for (i = 0; i < min_t(u8, si->count, VM_LEVEL_MAX); i++) {
113 cpascii(lgr_info->vm[i].name, si->vm[i].name,
114 sizeof(si->vm[i].name));
115 cpascii(lgr_info->vm[i].cpi, si->vm[i].cpi,
116 sizeof(si->vm[i].cpi));
118 lgr_info->vm_count = si->count;
122 * Fill LGR info with current data
124 static void lgr_info_get(struct lgr_info *lgr_info)
126 memset(lgr_info, 0, sizeof(*lgr_info));
127 stfle(lgr_info->stfle_fac_list, ARRAY_SIZE(lgr_info->stfle_fac_list));
128 lgr_info->level = stsi_0();
129 if (lgr_info->level == -ENOSYS)
131 if (lgr_info->level >= 1)
132 lgr_stsi_1_1_1(lgr_info);
133 if (lgr_info->level >= 2)
134 lgr_stsi_2_2_2(lgr_info);
135 if (lgr_info->level >= 3)
136 lgr_stsi_3_2_2(lgr_info);
140 * Check if LGR info has changed and if yes log new LGR info to s390dbf
142 void lgr_info_log(void)
144 static DEFINE_SPINLOCK(lgr_info_lock);
147 if (!spin_trylock_irqsave(&lgr_info_lock, flags))
149 lgr_info_get(&lgr_info_cur);
150 if (memcmp(&lgr_info_last, &lgr_info_cur, sizeof(lgr_info_cur)) != 0) {
151 debug_event(lgr_dbf, 1, &lgr_info_cur, sizeof(lgr_info_cur));
152 lgr_info_last = lgr_info_cur;
154 spin_unlock_irqrestore(&lgr_info_lock, flags);
156 EXPORT_SYMBOL_GPL(lgr_info_log);
158 static void lgr_timer_set(void);
163 static void lgr_timer_fn(unsigned long ignored)
169 static struct timer_list lgr_timer =
170 TIMER_DEFERRED_INITIALIZER(lgr_timer_fn, 0, 0);
173 * Setup next LGR timer
175 static void lgr_timer_set(void)
177 mod_timer(&lgr_timer, jiffies + LGR_TIMER_INTERVAL_SECS * HZ);
181 * Initialize LGR: Add s390dbf, write initial lgr_info and setup timer
183 static int __init lgr_init(void)
185 lgr_page = (void *) __get_free_pages(GFP_KERNEL, 0);
188 lgr_dbf = debug_register("lgr", 1, 1, sizeof(struct lgr_info));
190 free_page((unsigned long) lgr_page);
193 debug_register_view(lgr_dbf, &debug_hex_ascii_view);
194 lgr_info_get(&lgr_info_last);
195 debug_event(lgr_dbf, 1, &lgr_info_last, sizeof(lgr_info_last));
199 module_init(lgr_init);