]> git.openfabrics.org - ~shefty/rdma-dev.git/blob - arch/s390/kernel/sysinfo.c
a91274b4eb8fdc3296a24566ffba85f05b6b1139
[~shefty/rdma-dev.git] / arch / s390 / kernel / sysinfo.c
1 /*
2  *  Copyright IBM Corp. 2001, 2009
3  *  Author(s): Ulrich Weigand <Ulrich.Weigand@de.ibm.com>,
4  *             Martin Schwidefsky <schwidefsky@de.ibm.com>,
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/mm.h>
9 #include <linux/proc_fs.h>
10 #include <linux/seq_file.h>
11 #include <linux/init.h>
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/slab.h>
15 #include <asm/ebcdic.h>
16 #include <asm/sysinfo.h>
17 #include <asm/cpcmd.h>
18
19 /* Sigh, math-emu. Don't ask. */
20 #include <asm/sfp-util.h>
21 #include <math-emu/soft-fp.h>
22 #include <math-emu/single.h>
23
24 static inline int stsi_0(void)
25 {
26         int rc = stsi(NULL, 0, 0, 0);
27         return rc == -ENOSYS ? rc : (((unsigned int) rc) >> 28);
28 }
29
30 static int stsi_1_1_1(struct sysinfo_1_1_1 *info, char *page, int len)
31 {
32         if (stsi(info, 1, 1, 1) == -ENOSYS)
33                 return len;
34
35         EBCASC(info->manufacturer, sizeof(info->manufacturer));
36         EBCASC(info->type, sizeof(info->type));
37         EBCASC(info->model, sizeof(info->model));
38         EBCASC(info->sequence, sizeof(info->sequence));
39         EBCASC(info->plant, sizeof(info->plant));
40         EBCASC(info->model_capacity, sizeof(info->model_capacity));
41         EBCASC(info->model_perm_cap, sizeof(info->model_perm_cap));
42         EBCASC(info->model_temp_cap, sizeof(info->model_temp_cap));
43         len += sprintf(page + len, "Manufacturer:         %-16.16s\n",
44                        info->manufacturer);
45         len += sprintf(page + len, "Type:                 %-4.4s\n",
46                        info->type);
47         if (info->model[0] != '\0')
48                 /*
49                  * Sigh: the model field has been renamed with System z9
50                  * to model_capacity and a new model field has been added
51                  * after the plant field. To avoid confusing older programs
52                  * the "Model:" prints "model_capacity model" or just
53                  * "model_capacity" if the model string is empty .
54                  */
55                 len += sprintf(page + len,
56                                "Model:                %-16.16s %-16.16s\n",
57                                info->model_capacity, info->model);
58         else
59                 len += sprintf(page + len, "Model:                %-16.16s\n",
60                                info->model_capacity);
61         len += sprintf(page + len, "Sequence Code:        %-16.16s\n",
62                        info->sequence);
63         len += sprintf(page + len, "Plant:                %-4.4s\n",
64                        info->plant);
65         len += sprintf(page + len, "Model Capacity:       %-16.16s %08u\n",
66                        info->model_capacity, *(u32 *) info->model_cap_rating);
67         if (info->model_perm_cap[0] != '\0')
68                 len += sprintf(page + len,
69                                "Model Perm. Capacity: %-16.16s %08u\n",
70                                info->model_perm_cap,
71                                *(u32 *) info->model_perm_cap_rating);
72         if (info->model_temp_cap[0] != '\0')
73                 len += sprintf(page + len,
74                                "Model Temp. Capacity: %-16.16s %08u\n",
75                                info->model_temp_cap,
76                                *(u32 *) info->model_temp_cap_rating);
77         if (info->cai) {
78                 len += sprintf(page + len,
79                                "Capacity Adj. Ind.:   %d\n",
80                                info->cai);
81                 len += sprintf(page + len, "Capacity Ch. Reason:  %d\n",
82                                info->ccr);
83         }
84         return len;
85 }
86
87 static int stsi_1_2_2(struct sysinfo_1_2_2 *info, char *page, int len)
88 {
89         struct sysinfo_1_2_2_extension *ext;
90         int i;
91
92         if (stsi(info, 1, 2, 2) == -ENOSYS)
93                 return len;
94         ext = (struct sysinfo_1_2_2_extension *)
95                 ((unsigned long) info + info->acc_offset);
96
97         len += sprintf(page + len, "\n");
98         len += sprintf(page + len, "CPUs Total:           %d\n",
99                        info->cpus_total);
100         len += sprintf(page + len, "CPUs Configured:      %d\n",
101                        info->cpus_configured);
102         len += sprintf(page + len, "CPUs Standby:         %d\n",
103                        info->cpus_standby);
104         len += sprintf(page + len, "CPUs Reserved:        %d\n",
105                        info->cpus_reserved);
106
107         if (info->format == 1) {
108                 /*
109                  * Sigh 2. According to the specification the alternate
110                  * capability field is a 32 bit floating point number
111                  * if the higher order 8 bits are not zero. Printing
112                  * a floating point number in the kernel is a no-no,
113                  * always print the number as 32 bit unsigned integer.
114                  * The user-space needs to know about the strange
115                  * encoding of the alternate cpu capability.
116                  */
117                 len += sprintf(page + len, "Capability:           %u %u\n",
118                                info->capability, ext->alt_capability);
119                 for (i = 2; i <= info->cpus_total; i++)
120                         len += sprintf(page + len,
121                                        "Adjustment %02d-way:    %u %u\n",
122                                        i, info->adjustment[i-2],
123                                        ext->alt_adjustment[i-2]);
124
125         } else {
126                 len += sprintf(page + len, "Capability:           %u\n",
127                                info->capability);
128                 for (i = 2; i <= info->cpus_total; i++)
129                         len += sprintf(page + len,
130                                        "Adjustment %02d-way:    %u\n",
131                                        i, info->adjustment[i-2]);
132         }
133
134         if (info->secondary_capability != 0)
135                 len += sprintf(page + len, "Secondary Capability: %d\n",
136                                info->secondary_capability);
137         return len;
138 }
139
140 static int stsi_2_2_2(struct sysinfo_2_2_2 *info, char *page, int len)
141 {
142         if (stsi(info, 2, 2, 2) == -ENOSYS)
143                 return len;
144
145         EBCASC(info->name, sizeof(info->name));
146
147         len += sprintf(page + len, "\n");
148         len += sprintf(page + len, "LPAR Number:          %d\n",
149                        info->lpar_number);
150
151         len += sprintf(page + len, "LPAR Characteristics: ");
152         if (info->characteristics & LPAR_CHAR_DEDICATED)
153                 len += sprintf(page + len, "Dedicated ");
154         if (info->characteristics & LPAR_CHAR_SHARED)
155                 len += sprintf(page + len, "Shared ");
156         if (info->characteristics & LPAR_CHAR_LIMITED)
157                 len += sprintf(page + len, "Limited ");
158         len += sprintf(page + len, "\n");
159
160         len += sprintf(page + len, "LPAR Name:            %-8.8s\n",
161                        info->name);
162
163         len += sprintf(page + len, "LPAR Adjustment:      %d\n",
164                        info->caf);
165
166         len += sprintf(page + len, "LPAR CPUs Total:      %d\n",
167                        info->cpus_total);
168         len += sprintf(page + len, "LPAR CPUs Configured: %d\n",
169                        info->cpus_configured);
170         len += sprintf(page + len, "LPAR CPUs Standby:    %d\n",
171                        info->cpus_standby);
172         len += sprintf(page + len, "LPAR CPUs Reserved:   %d\n",
173                        info->cpus_reserved);
174         len += sprintf(page + len, "LPAR CPUs Dedicated:  %d\n",
175                        info->cpus_dedicated);
176         len += sprintf(page + len, "LPAR CPUs Shared:     %d\n",
177                        info->cpus_shared);
178         return len;
179 }
180
181 static int stsi_3_2_2(struct sysinfo_3_2_2 *info, char *page, int len)
182 {
183         int i;
184
185         if (stsi(info, 3, 2, 2) == -ENOSYS)
186                 return len;
187         for (i = 0; i < info->count; i++) {
188                 EBCASC(info->vm[i].name, sizeof(info->vm[i].name));
189                 EBCASC(info->vm[i].cpi, sizeof(info->vm[i].cpi));
190                 len += sprintf(page + len, "\n");
191                 len += sprintf(page + len, "VM%02d Name:            %-8.8s\n",
192                                i, info->vm[i].name);
193                 len += sprintf(page + len, "VM%02d Control Program: %-16.16s\n",
194                                i, info->vm[i].cpi);
195
196                 len += sprintf(page + len, "VM%02d Adjustment:      %d\n",
197                                i, info->vm[i].caf);
198
199                 len += sprintf(page + len, "VM%02d CPUs Total:      %d\n",
200                                i, info->vm[i].cpus_total);
201                 len += sprintf(page + len, "VM%02d CPUs Configured: %d\n",
202                                i, info->vm[i].cpus_configured);
203                 len += sprintf(page + len, "VM%02d CPUs Standby:    %d\n",
204                                i, info->vm[i].cpus_standby);
205                 len += sprintf(page + len, "VM%02d CPUs Reserved:   %d\n",
206                                i, info->vm[i].cpus_reserved);
207         }
208         return len;
209 }
210
211 static int proc_read_sysinfo(char *page, char **start,
212                              off_t off, int count,
213                              int *eof, void *data)
214 {
215         unsigned long info = get_zeroed_page(GFP_KERNEL);
216         int level, len;
217
218         if (!info)
219                 return 0;
220
221         len = 0;
222         level = stsi_0();
223         if (level >= 1)
224                 len = stsi_1_1_1((struct sysinfo_1_1_1 *) info, page, len);
225
226         if (level >= 1)
227                 len = stsi_1_2_2((struct sysinfo_1_2_2 *) info, page, len);
228
229         if (level >= 2)
230                 len = stsi_2_2_2((struct sysinfo_2_2_2 *) info, page, len);
231
232         if (level >= 3)
233                 len = stsi_3_2_2((struct sysinfo_3_2_2 *) info, page, len);
234
235         free_page(info);
236         return len;
237 }
238
239 static __init int create_proc_sysinfo(void)
240 {
241         create_proc_read_entry("sysinfo", 0444, NULL,
242                                proc_read_sysinfo, NULL);
243         return 0;
244 }
245 device_initcall(create_proc_sysinfo);
246
247 /*
248  * Service levels interface.
249  */
250
251 static DECLARE_RWSEM(service_level_sem);
252 static LIST_HEAD(service_level_list);
253
254 int register_service_level(struct service_level *slr)
255 {
256         struct service_level *ptr;
257
258         down_write(&service_level_sem);
259         list_for_each_entry(ptr, &service_level_list, list)
260                 if (ptr == slr) {
261                         up_write(&service_level_sem);
262                         return -EEXIST;
263                 }
264         list_add_tail(&slr->list, &service_level_list);
265         up_write(&service_level_sem);
266         return 0;
267 }
268 EXPORT_SYMBOL(register_service_level);
269
270 int unregister_service_level(struct service_level *slr)
271 {
272         struct service_level *ptr, *next;
273         int rc = -ENOENT;
274
275         down_write(&service_level_sem);
276         list_for_each_entry_safe(ptr, next, &service_level_list, list) {
277                 if (ptr != slr)
278                         continue;
279                 list_del(&ptr->list);
280                 rc = 0;
281                 break;
282         }
283         up_write(&service_level_sem);
284         return rc;
285 }
286 EXPORT_SYMBOL(unregister_service_level);
287
288 static void *service_level_start(struct seq_file *m, loff_t *pos)
289 {
290         down_read(&service_level_sem);
291         return seq_list_start(&service_level_list, *pos);
292 }
293
294 static void *service_level_next(struct seq_file *m, void *p, loff_t *pos)
295 {
296         return seq_list_next(p, &service_level_list, pos);
297 }
298
299 static void service_level_stop(struct seq_file *m, void *p)
300 {
301         up_read(&service_level_sem);
302 }
303
304 static int service_level_show(struct seq_file *m, void *p)
305 {
306         struct service_level *slr;
307
308         slr = list_entry(p, struct service_level, list);
309         slr->seq_print(m, slr);
310         return 0;
311 }
312
313 static const struct seq_operations service_level_seq_ops = {
314         .start          = service_level_start,
315         .next           = service_level_next,
316         .stop           = service_level_stop,
317         .show           = service_level_show
318 };
319
320 static int service_level_open(struct inode *inode, struct file *file)
321 {
322         return seq_open(file, &service_level_seq_ops);
323 }
324
325 static const struct file_operations service_level_ops = {
326         .open           = service_level_open,
327         .read           = seq_read,
328         .llseek         = seq_lseek,
329         .release        = seq_release
330 };
331
332 static void service_level_vm_print(struct seq_file *m,
333                                    struct service_level *slr)
334 {
335         char *query_buffer, *str;
336
337         query_buffer = kmalloc(1024, GFP_KERNEL | GFP_DMA);
338         if (!query_buffer)
339                 return;
340         cpcmd("QUERY CPLEVEL", query_buffer, 1024, NULL);
341         str = strchr(query_buffer, '\n');
342         if (str)
343                 *str = 0;
344         seq_printf(m, "VM: %s\n", query_buffer);
345         kfree(query_buffer);
346 }
347
348 static struct service_level service_level_vm = {
349         .seq_print = service_level_vm_print
350 };
351
352 static __init int create_proc_service_level(void)
353 {
354         proc_create("service_levels", 0, NULL, &service_level_ops);
355         if (MACHINE_IS_VM)
356                 register_service_level(&service_level_vm);
357         return 0;
358 }
359 subsys_initcall(create_proc_service_level);
360
361 /*
362  * Bogomips calculation based on cpu capability.
363  */
364 int get_cpu_capability(unsigned int *capability)
365 {
366         struct sysinfo_1_2_2 *info;
367         int rc;
368
369         info = (void *) get_zeroed_page(GFP_KERNEL);
370         if (!info)
371                 return -ENOMEM;
372         rc = stsi(info, 1, 2, 2);
373         if (rc == -ENOSYS)
374                 goto out;
375         rc = 0;
376         *capability = info->capability;
377 out:
378         free_page((unsigned long) info);
379         return rc;
380 }
381
382 /*
383  * CPU capability might have changed. Therefore recalculate loops_per_jiffy.
384  */
385 void s390_adjust_jiffies(void)
386 {
387         struct sysinfo_1_2_2 *info;
388         const unsigned int fmil = 0x4b189680;   /* 1e7 as 32-bit float. */
389         FP_DECL_S(SA); FP_DECL_S(SB); FP_DECL_S(SR);
390         FP_DECL_EX;
391         unsigned int capability;
392
393         info = (void *) get_zeroed_page(GFP_KERNEL);
394         if (!info)
395                 return;
396
397         if (stsi(info, 1, 2, 2) != -ENOSYS) {
398                 /*
399                  * Major sigh. The cpu capability encoding is "special".
400                  * If the first 9 bits of info->capability are 0 then it
401                  * is a 32 bit unsigned integer in the range 0 .. 2^23.
402                  * If the first 9 bits are != 0 then it is a 32 bit float.
403                  * In addition a lower value indicates a proportionally
404                  * higher cpu capacity. Bogomips are the other way round.
405                  * To get to a halfway suitable number we divide 1e7
406                  * by the cpu capability number. Yes, that means a floating
407                  * point division .. math-emu here we come :-)
408                  */
409                 FP_UNPACK_SP(SA, &fmil);
410                 if ((info->capability >> 23) == 0)
411                         FP_FROM_INT_S(SB, info->capability, 32, int);
412                 else
413                         FP_UNPACK_SP(SB, &info->capability);
414                 FP_DIV_S(SR, SA, SB);
415                 FP_TO_INT_S(capability, SR, 32, 0);
416         } else
417                 /*
418                  * Really old machine without stsi block for basic
419                  * cpu information. Report 42.0 bogomips.
420                  */
421                 capability = 42;
422         loops_per_jiffy = capability * (500000/HZ);
423         free_page((unsigned long) info);
424 }
425
426 /*
427  * calibrate the delay loop
428  */
429 void __cpuinit calibrate_delay(void)
430 {
431         s390_adjust_jiffies();
432         /* Print the good old Bogomips line .. */
433         printk(KERN_DEBUG "Calibrating delay loop (skipped)... "
434                "%lu.%02lu BogoMIPS preset\n", loops_per_jiffy/(500000/HZ),
435                (loops_per_jiffy/(5000/HZ)) % 100);
436 }