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