Disintegrate asm/system.h for S390
[sfrench/cifs-2.6.git] / arch / s390 / kernel / lgr.c
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
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>
14 #include <asm/ipl.h>
15
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 /*
20  * LGR info: Contains stfle and stsi data
21  */
22 struct lgr_info {
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 */
26         u32 level;
27         /* Level 1: CEC info (stsi 1.1.1) */
28         char manufacturer[16];
29         char type[4];
30         char sequence[16];
31         char plant[4];
32         char model[16];
33         /* Level 2: LPAR info (stsi 2.2.2) */
34         u16 lpar_number;
35         char name[8];
36         /* Level 3: VM info (stsi 3.2.2) */
37         u8 vm_count;
38         struct {
39                 char name[8];
40                 char cpi[16];
41         } vm[VM_LEVEL_MAX];
42 } __packed __aligned(8);
43
44 /*
45  * LGR globals
46  */
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;
51
52 /*
53  * Return number of valid stsi levels
54  */
55 static inline int stsi_0(void)
56 {
57         int rc = stsi(NULL, 0, 0, 0);
58
59         return rc == -ENOSYS ? rc : (((unsigned int) rc) >> 28);
60 }
61
62 /*
63  * Copy buffer and then convert it to ASCII
64  */
65 static void cpascii(char *dst, char *src, int size)
66 {
67         memcpy(dst, src, size);
68         EBCASC(dst, size);
69 }
70
71 /*
72  * Fill LGR info with 1.1.1 stsi data
73  */
74 static void lgr_stsi_1_1_1(struct lgr_info *lgr_info)
75 {
76         struct sysinfo_1_1_1 *si = lgr_page;
77
78         if (stsi(si, 1, 1, 1) == -ENOSYS)
79                 return;
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));
86 }
87
88 /*
89  * Fill LGR info with 2.2.2 stsi data
90  */
91 static void lgr_stsi_2_2_2(struct lgr_info *lgr_info)
92 {
93         struct sysinfo_2_2_2 *si = lgr_page;
94
95         if (stsi(si, 2, 2, 2) == -ENOSYS)
96                 return;
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));
100 }
101
102 /*
103  * Fill LGR info with 3.2.2 stsi data
104  */
105 static void lgr_stsi_3_2_2(struct lgr_info *lgr_info)
106 {
107         struct sysinfo_3_2_2 *si = lgr_page;
108         int i;
109
110         if (stsi(si, 3, 2, 2) == -ENOSYS)
111                 return;
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));
117         }
118         lgr_info->vm_count = si->count;
119 }
120
121 /*
122  * Fill LGR info with current data
123  */
124 static void lgr_info_get(struct lgr_info *lgr_info)
125 {
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)
130                 return;
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);
137 }
138
139 /*
140  * Check if LGR info has changed and if yes log new LGR info to s390dbf
141  */
142 void lgr_info_log(void)
143 {
144         static DEFINE_SPINLOCK(lgr_info_lock);
145         unsigned long flags;
146
147         if (!spin_trylock_irqsave(&lgr_info_lock, flags))
148                 return;
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;
153         }
154         spin_unlock_irqrestore(&lgr_info_lock, flags);
155 }
156 EXPORT_SYMBOL_GPL(lgr_info_log);
157
158 static void lgr_timer_set(void);
159
160 /*
161  * LGR timer callback
162  */
163 static void lgr_timer_fn(unsigned long ignored)
164 {
165         lgr_info_log();
166         lgr_timer_set();
167 }
168
169 static struct timer_list lgr_timer =
170         TIMER_DEFERRED_INITIALIZER(lgr_timer_fn, 0, 0);
171
172 /*
173  * Setup next LGR timer
174  */
175 static void lgr_timer_set(void)
176 {
177         mod_timer(&lgr_timer, jiffies + LGR_TIMER_INTERVAL_SECS * HZ);
178 }
179
180 /*
181  * Initialize LGR: Add s390dbf, write initial lgr_info and setup timer
182  */
183 static int __init lgr_init(void)
184 {
185         lgr_page = (void *) __get_free_pages(GFP_KERNEL, 0);
186         if (!lgr_page)
187                 return -ENOMEM;
188         lgr_dbf = debug_register("lgr", 1, 1, sizeof(struct lgr_info));
189         if (!lgr_dbf) {
190                 free_page((unsigned long) lgr_page);
191                 return -ENOMEM;
192         }
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));
196         lgr_timer_set();
197         return 0;
198 }
199 module_init(lgr_init);