drivers/serial/mpc52xx_uart.c: fix array overindexing check
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / s390 / sysinfo.c
blob0eea90781385784ae4ab838dce905dcaf4942a89
1 /*
2 * drivers/s390/sysinfo.c
4 * Copyright IBM Corp. 2001, 2008
5 * Author(s): Ulrich Weigand (Ulrich.Weigand@de.ibm.com)
6 * Martin Schwidefsky <schwidefsky@de.ibm.com>
7 */
9 #include <linux/kernel.h>
10 #include <linux/mm.h>
11 #include <linux/proc_fs.h>
12 #include <linux/seq_file.h>
13 #include <linux/init.h>
14 #include <linux/delay.h>
15 #include <linux/module.h>
16 #include <asm/ebcdic.h>
17 #include <asm/sysinfo.h>
18 #include <asm/cpcmd.h>
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>
25 static inline int stsi_0(void)
27 int rc = stsi (NULL, 0, 0, 0);
28 return rc == -ENOSYS ? rc : (((unsigned int) rc) >> 28);
31 static int stsi_1_1_1(struct sysinfo_1_1_1 *info, char *page, int len)
33 if (stsi(info, 1, 1, 1) == -ENOSYS)
34 return len;
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')
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 .
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 return len;
81 #if 0 /* Currently unused */
82 static int stsi_1_2_1(struct sysinfo_1_2_1 *info, char *page, int len)
84 if (stsi(info, 1, 2, 1) == -ENOSYS)
85 return len;
87 len += sprintf(page + len, "\n");
88 EBCASC(info->sequence, sizeof(info->sequence));
89 EBCASC(info->plant, sizeof(info->plant));
90 len += sprintf(page + len, "Sequence Code of CPU: %-16.16s\n",
91 info->sequence);
92 len += sprintf(page + len, "Plant of CPU: %-16.16s\n",
93 info->plant);
94 return len;
96 #endif
98 static int stsi_1_2_2(struct sysinfo_1_2_2 *info, char *page, int len)
100 struct sysinfo_1_2_2_extension *ext;
101 int i;
103 if (stsi(info, 1, 2, 2) == -ENOSYS)
104 return len;
105 ext = (struct sysinfo_1_2_2_extension *)
106 ((unsigned long) info + info->acc_offset);
108 len += sprintf(page + len, "\n");
109 len += sprintf(page + len, "CPUs Total: %d\n",
110 info->cpus_total);
111 len += sprintf(page + len, "CPUs Configured: %d\n",
112 info->cpus_configured);
113 len += sprintf(page + len, "CPUs Standby: %d\n",
114 info->cpus_standby);
115 len += sprintf(page + len, "CPUs Reserved: %d\n",
116 info->cpus_reserved);
118 if (info->format == 1) {
120 * Sigh 2. According to the specification the alternate
121 * capability field is a 32 bit floating point number
122 * if the higher order 8 bits are not zero. Printing
123 * a floating point number in the kernel is a no-no,
124 * always print the number as 32 bit unsigned integer.
125 * The user-space needs to know about the strange
126 * encoding of the alternate cpu capability.
128 len += sprintf(page + len, "Capability: %u %u\n",
129 info->capability, ext->alt_capability);
130 for (i = 2; i <= info->cpus_total; i++)
131 len += sprintf(page + len,
132 "Adjustment %02d-way: %u %u\n",
133 i, info->adjustment[i-2],
134 ext->alt_adjustment[i-2]);
136 } else {
137 len += sprintf(page + len, "Capability: %u\n",
138 info->capability);
139 for (i = 2; i <= info->cpus_total; i++)
140 len += sprintf(page + len,
141 "Adjustment %02d-way: %u\n",
142 i, info->adjustment[i-2]);
145 if (info->secondary_capability != 0)
146 len += sprintf(page + len, "Secondary Capability: %d\n",
147 info->secondary_capability);
149 return len;
152 #if 0 /* Currently unused */
153 static int stsi_2_2_1(struct sysinfo_2_2_1 *info, char *page, int len)
155 if (stsi(info, 2, 2, 1) == -ENOSYS)
156 return len;
158 len += sprintf(page + len, "\n");
159 EBCASC (info->sequence, sizeof(info->sequence));
160 EBCASC (info->plant, sizeof(info->plant));
161 len += sprintf(page + len, "Sequence Code of logical CPU: %-16.16s\n",
162 info->sequence);
163 len += sprintf(page + len, "Plant of logical CPU: %-16.16s\n",
164 info->plant);
165 return len;
167 #endif
169 static int stsi_2_2_2(struct sysinfo_2_2_2 *info, char *page, int len)
171 if (stsi(info, 2, 2, 2) == -ENOSYS)
172 return len;
174 EBCASC (info->name, sizeof(info->name));
176 len += sprintf(page + len, "\n");
177 len += sprintf(page + len, "LPAR Number: %d\n",
178 info->lpar_number);
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");
189 len += sprintf(page + len, "LPAR Name: %-8.8s\n",
190 info->name);
192 len += sprintf(page + len, "LPAR Adjustment: %d\n",
193 info->caf);
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;
210 static int stsi_3_2_2(struct sysinfo_3_2_2 *info, char *page, int len)
212 int i;
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);
225 len += sprintf(page + len, "VM%02d Adjustment: %d\n",
226 i, info->vm[i].caf);
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);
237 return len;
241 static int proc_read_sysinfo(char *page, char **start,
242 off_t off, int count,
243 int *eof, void *data)
245 unsigned long info = get_zeroed_page (GFP_KERNEL);
246 int level, len;
248 if (!info)
249 return 0;
251 len = 0;
252 level = stsi_0();
253 if (level >= 1)
254 len = stsi_1_1_1((struct sysinfo_1_1_1 *) info, page, len);
256 if (level >= 1)
257 len = stsi_1_2_2((struct sysinfo_1_2_2 *) info, page, len);
259 if (level >= 2)
260 len = stsi_2_2_2((struct sysinfo_2_2_2 *) info, page, len);
262 if (level >= 3)
263 len = stsi_3_2_2((struct sysinfo_3_2_2 *) info, page, len);
265 free_page (info);
266 return len;
269 static __init int create_proc_sysinfo(void)
271 create_proc_read_entry("sysinfo", 0444, NULL,
272 proc_read_sysinfo, NULL);
273 return 0;
276 __initcall(create_proc_sysinfo);
279 * Service levels interface.
282 static DECLARE_RWSEM(service_level_sem);
283 static LIST_HEAD(service_level_list);
285 int register_service_level(struct service_level *slr)
287 struct service_level *ptr;
289 down_write(&service_level_sem);
290 list_for_each_entry(ptr, &service_level_list, list)
291 if (ptr == slr) {
292 up_write(&service_level_sem);
293 return -EEXIST;
295 list_add_tail(&slr->list, &service_level_list);
296 up_write(&service_level_sem);
297 return 0;
299 EXPORT_SYMBOL(register_service_level);
301 int unregister_service_level(struct service_level *slr)
303 struct service_level *ptr, *next;
304 int rc = -ENOENT;
306 down_write(&service_level_sem);
307 list_for_each_entry_safe(ptr, next, &service_level_list, list) {
308 if (ptr != slr)
309 continue;
310 list_del(&ptr->list);
311 rc = 0;
312 break;
314 up_write(&service_level_sem);
315 return rc;
317 EXPORT_SYMBOL(unregister_service_level);
319 static void *service_level_start(struct seq_file *m, loff_t *pos)
321 down_read(&service_level_sem);
322 return seq_list_start(&service_level_list, *pos);
325 static void *service_level_next(struct seq_file *m, void *p, loff_t *pos)
327 return seq_list_next(p, &service_level_list, pos);
330 static void service_level_stop(struct seq_file *m, void *p)
332 up_read(&service_level_sem);
335 static int service_level_show(struct seq_file *m, void *p)
337 struct service_level *slr;
339 slr = list_entry(p, struct service_level, list);
340 slr->seq_print(m, slr);
341 return 0;
344 static const struct seq_operations service_level_seq_ops = {
345 .start = service_level_start,
346 .next = service_level_next,
347 .stop = service_level_stop,
348 .show = service_level_show
351 static int service_level_open(struct inode *inode, struct file *file)
353 return seq_open(file, &service_level_seq_ops);
356 static const struct file_operations service_level_ops = {
357 .open = service_level_open,
358 .read = seq_read,
359 .llseek = seq_lseek,
360 .release = seq_release
363 static void service_level_vm_print(struct seq_file *m,
364 struct service_level *slr)
366 char *query_buffer, *str;
368 query_buffer = kmalloc(1024, GFP_KERNEL | GFP_DMA);
369 if (!query_buffer)
370 return;
371 cpcmd("QUERY CPLEVEL", query_buffer, 1024, NULL);
372 str = strchr(query_buffer, '\n');
373 if (str)
374 *str = 0;
375 seq_printf(m, "VM: %s\n", query_buffer);
376 kfree(query_buffer);
379 static struct service_level service_level_vm = {
380 .seq_print = service_level_vm_print
383 static __init int create_proc_service_level(void)
385 proc_create("service_levels", 0, NULL, &service_level_ops);
386 if (MACHINE_IS_VM)
387 register_service_level(&service_level_vm);
388 return 0;
391 subsys_initcall(create_proc_service_level);
394 * Bogomips calculation based on cpu capability.
397 int get_cpu_capability(unsigned int *capability)
399 struct sysinfo_1_2_2 *info;
400 int rc;
402 info = (void *) get_zeroed_page(GFP_KERNEL);
403 if (!info)
404 return -ENOMEM;
405 rc = stsi(info, 1, 2, 2);
406 if (rc == -ENOSYS)
407 goto out;
408 rc = 0;
409 *capability = info->capability;
410 out:
411 free_page((unsigned long) info);
412 return rc;
416 * CPU capability might have changed. Therefore recalculate loops_per_jiffy.
418 void s390_adjust_jiffies(void)
420 struct sysinfo_1_2_2 *info;
421 const unsigned int fmil = 0x4b189680; /* 1e7 as 32-bit float. */
422 FP_DECL_S(SA); FP_DECL_S(SB); FP_DECL_S(SR);
423 FP_DECL_EX;
424 unsigned int capability;
426 info = (void *) get_zeroed_page(GFP_KERNEL);
427 if (!info)
428 return;
430 if (stsi(info, 1, 2, 2) != -ENOSYS) {
432 * Major sigh. The cpu capability encoding is "special".
433 * If the first 9 bits of info->capability are 0 then it
434 * is a 32 bit unsigned integer in the range 0 .. 2^23.
435 * If the first 9 bits are != 0 then it is a 32 bit float.
436 * In addition a lower value indicates a proportionally
437 * higher cpu capacity. Bogomips are the other way round.
438 * To get to a halfway suitable number we divide 1e7
439 * by the cpu capability number. Yes, that means a floating
440 * point division .. math-emu here we come :-)
442 FP_UNPACK_SP(SA, &fmil);
443 if ((info->capability >> 23) == 0)
444 FP_FROM_INT_S(SB, info->capability, 32, int);
445 else
446 FP_UNPACK_SP(SB, &info->capability);
447 FP_DIV_S(SR, SA, SB);
448 FP_TO_INT_S(capability, SR, 32, 0);
449 } else
451 * Really old machine without stsi block for basic
452 * cpu information. Report 42.0 bogomips.
454 capability = 42;
455 loops_per_jiffy = capability * (500000/HZ);
456 free_page((unsigned long) info);
460 * calibrate the delay loop
462 void __cpuinit calibrate_delay(void)
464 s390_adjust_jiffies();
465 /* Print the good old Bogomips line .. */
466 printk(KERN_DEBUG "Calibrating delay loop (skipped)... "
467 "%lu.%02lu BogoMIPS preset\n", loops_per_jiffy/(500000/HZ),
468 (loops_per_jiffy/(5000/HZ)) % 100);