vserver 1.9.3
[linux-2.6.git] / arch / ia64 / sn / kernel / sn2 / prominfo_proc.c
1 /*
2  * This file is subject to the terms and conditions of the GNU General Public
3  * License.  See the file "COPYING" in the main directory of this archive
4  * for more details.
5  *
6  * Copyright (C) 1999,2001-2004 Silicon Graphics, Inc.  All Rights Reserved.
7  *
8  * Module to export the system's Firmware Interface Tables, including
9  * PROM revision numbers and banners, in /proc
10  */
11 #include <linux/config.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/proc_fs.h>
15 #include <asm/system.h>
16 #include <asm/io.h>
17 #include <asm/sn/sn_sal.h>
18 #include <asm/sn/sn_cpuid.h>
19 #include <asm/sn/sn2/addrs.h>
20
21 MODULE_DESCRIPTION("PROM version reporting for /proc");
22 MODULE_AUTHOR("Chad Talbott");
23 MODULE_LICENSE("GPL");
24
25 /* Standard Intel FIT entry types */
26 #define FIT_ENTRY_FIT_HEADER    0x00    /* FIT header entry */
27 #define FIT_ENTRY_PAL_B         0x01    /* PAL_B entry */
28 /* Entries 0x02 through 0x0D reserved by Intel */
29 #define FIT_ENTRY_PAL_A_PROC    0x0E    /* Processor-specific PAL_A entry */
30 #define FIT_ENTRY_PAL_A         0x0F    /* PAL_A entry, same as... */
31 #define FIT_ENTRY_PAL_A_GEN     0x0F    /* ...Generic PAL_A entry */
32 #define FIT_ENTRY_UNUSED        0x7F    /* Unused (reserved by Intel?) */
33 /* OEM-defined entries range from 0x10 to 0x7E. */
34 #define FIT_ENTRY_SAL_A         0x10    /* SAL_A entry */
35 #define FIT_ENTRY_SAL_B         0x11    /* SAL_B entry */
36 #define FIT_ENTRY_SALRUNTIME    0x12    /* SAL runtime entry */
37 #define FIT_ENTRY_EFI           0x1F    /* EFI entry */
38 #define FIT_ENTRY_FPSWA         0x20    /* embedded fpswa entry */
39 #define FIT_ENTRY_VMLINUX       0x21    /* embedded vmlinux entry */
40
41 #define FIT_MAJOR_SHIFT (32 + 8)
42 #define FIT_MAJOR_MASK  ((1 << 8) - 1)
43 #define FIT_MINOR_SHIFT 32
44 #define FIT_MINOR_MASK  ((1 << 8) - 1)
45
46 #define FIT_MAJOR(q)    \
47         ((unsigned) ((q) >> FIT_MAJOR_SHIFT) & FIT_MAJOR_MASK)
48 #define FIT_MINOR(q)    \
49         ((unsigned) ((q) >> FIT_MINOR_SHIFT) & FIT_MINOR_MASK)
50
51 #define FIT_TYPE_SHIFT  (32 + 16)
52 #define FIT_TYPE_MASK   ((1 << 7) - 1)
53
54 #define FIT_TYPE(q)     \
55         ((unsigned) ((q) >> FIT_TYPE_SHIFT) & FIT_TYPE_MASK)
56
57 struct fit_type_map_t {
58         unsigned char   type;
59         const char      *name;
60 };
61
62 static const struct fit_type_map_t fit_entry_types[] = {
63         { FIT_ENTRY_FIT_HEADER, "FIT Header" },
64         { FIT_ENTRY_PAL_A_GEN,  "Generic PAL_A" },
65         { FIT_ENTRY_PAL_A_PROC, "Processor-specific PAL_A" },
66         { FIT_ENTRY_PAL_A,      "PAL_A" },
67         { FIT_ENTRY_PAL_B,      "PAL_B" },
68         { FIT_ENTRY_SAL_A,      "SAL_A" },
69         { FIT_ENTRY_SAL_B,      "SAL_B" },
70         { FIT_ENTRY_SALRUNTIME, "SAL runtime" },
71         { FIT_ENTRY_EFI,        "EFI" },
72         { FIT_ENTRY_VMLINUX,    "Embedded Linux" },
73         { FIT_ENTRY_FPSWA,      "Embedded FPSWA" },
74         { FIT_ENTRY_UNUSED,     "Unused" },
75         { 0xff,                 "Error" },
76 };
77
78 static const char *
79 fit_type_name(unsigned char type)
80 {
81         struct fit_type_map_t const*mapp;
82
83         for (mapp = fit_entry_types; mapp->type != 0xff; mapp++)
84                 if (type == mapp->type)
85                         return mapp->name;
86
87         if ((type > FIT_ENTRY_PAL_A) && (type < FIT_ENTRY_UNUSED))
88                 return "OEM type";
89         if ((type > FIT_ENTRY_PAL_B) && (type < FIT_ENTRY_PAL_A))
90                 return "Reserved";
91
92         return "Unknown type";
93 }
94
95
96 /* ============ BEGIN temp til old PROMs are no longer supported =============
97  *
98  * The OS should not make direct access to the PROM flash memory. Access to
99  * this region must be serialized with a PROM lock. If SAL on one cpu is
100  * updating the FLASH error log at the same time another cpu is accessing the
101  * PROM, data corruption will occur.
102  *
103  * To solve the problem, all flash PROM access has been moved to SAL. Because
104  * not all systems will have instant PROM updates, we need to support a new OS
105  * running on a system with old PROMs.
106  *
107  * This code should be deleted after 1 OS/PROM release has occurred & the OS
108  * no longer supports downrev PROMs. (PROM support should be in the 3.50
109  * PROMs).
110  */
111 #define SUPPORT_OLD_PROMS
112 #ifdef SUPPORT_OLD_PROMS
113
114
115 #define FIT_SIGNATURE           0x2020205f5449465ful
116
117 /* Sub-regions determined by bits in Node Offset */
118 #define LB_PROM_SPACE           0x0000000700000000ul /* Local LB PROM */
119
120 /* Offset of PROM banner pointers in SAL A and SAL B */
121 #define SAL_A_BANNER_OFFSET     (1 * 16)
122 #define SAL_B_BANNER_OFFSET     (3 * 16)
123
124 /* Architected IA64 firmware space */
125 #define FW_BASE                 0x00000000FF000000
126 #define FW_TOP                  0x0000000100000000
127
128 static unsigned long
129 convert_fw_addr(nasid_t nasid, unsigned long addr)
130 {
131         /* snag just the node-relative offset */
132         addr &= ~0ul >> (63-35);
133         /* the pointer to SAL A is relative to IA-64 compatibility
134          * space.  However, the PROM is mapped at a different offset
135          * in MMR space (both local and global)
136          */
137         addr += 0x700000000;
138         return GLOBAL_MMR_ADDR(nasid, addr);
139 }
140
141 static int
142 valid_fw_addr(unsigned long addr)
143 {
144         addr &= ~(1ul << 63); /* Clear cached/uncached bit */
145         return (addr >= FW_BASE && addr < FW_TOP);
146 }
147
148 static unsigned long *
149 lookup_fit(int nasid)
150 {
151         unsigned long *fitp;
152         unsigned long fit_paddr;
153         unsigned long *fit_vaddr;
154
155         fitp = (void *)GLOBAL_MMR_ADDR(nasid, LB_PROM_SPACE - 32);
156         fit_paddr = readq(fitp);
157         fit_vaddr = (unsigned long *) convert_fw_addr(nasid, fit_paddr);
158         return fit_vaddr;
159 }
160 #endif /* SUPPORT_OLD_PROMS */
161 /* ============ END temp til old PROMs are no longer supported ============= */
162
163 static int
164 get_fit_entry(unsigned long nasid, int index, unsigned long *fentry,
165               char *banner, int banlen)
166 {
167         int ret;
168
169         ret = ia64_sn_get_fit_compt(nasid, index, fentry, banner, banlen);
170
171 #ifdef SUPPORT_OLD_PROMS
172         /* The following is hack is temporary until PROMs are updated */
173         if (ret == SALRET_NOT_IMPLEMENTED) {
174                 unsigned long *fitadr = lookup_fit(nasid);
175                 int nentries;
176
177                 if (readq(fitadr) != FIT_SIGNATURE) {
178                         printk(KERN_WARNING "Unrecognized FIT signature");
179                         return -2;
180                 }
181
182                 nentries = (unsigned int) (readq(fitadr + 1) & 0xffffff);
183                 if (index >= nentries)
184                         return -2;
185
186                 fentry[0] = readq(fitadr + 2 * index);
187                 fentry[1] = readq(fitadr + 2 * index + 1);
188                 ret = 0;
189
190                 if (banner && FIT_TYPE(fentry[1]) == FIT_ENTRY_SAL_A) {
191                         unsigned long i, qw, *bwp, *qwp;
192
193                         banner[0] = '\0';
194                         qw = fentry[0]; /* Address of SAL A */
195                         if (!valid_fw_addr(qw))
196                                 return 0;
197
198                         qw += SAL_A_BANNER_OFFSET;
199                         qw = convert_fw_addr(nasid, qw);
200
201                         qw = readq(qw);                 /* Address of banner */
202                         if (!valid_fw_addr(qw))
203                                 return 0;
204                         qw = convert_fw_addr(nasid, qw);
205                         qwp = (unsigned long *) qw;
206                         bwp = (unsigned long *) banner;
207                         for (i=0; i<banlen/8; i++)
208                                 bwp[i] = qwp[i];
209                 }
210         }
211 #endif /* SUPPORT_OLD_PROMS */
212         return ret;
213 }
214
215
216 /*
217  * These two routines display the FIT table for each node.
218  */
219 static int
220 dump_fit_entry(char *page, unsigned long *fentry)
221 {
222         unsigned type;
223
224         type = FIT_TYPE(fentry[1]);
225         return sprintf(page, "%02x %-25s %x.%02x %016lx %u\n",
226                        type,
227                        fit_type_name(type),
228                        FIT_MAJOR(fentry[1]), FIT_MINOR(fentry[1]),
229                        fentry[0],
230                        /* mult by sixteen to get size in bytes */
231                        (unsigned)(fentry[1] & 0xffffff) * 16);
232 }
233
234
235 /*
236  * We assume that the fit table will be small enough that we can print
237  * the whole thing into one page.  (This is true for our default 16kB
238  * pages -- each entry is about 60 chars wide when printed.)  I read
239  * somewhere that the maximum size of the FIT is 128 entries, so we're
240  * OK except for 4kB pages (and no one is going to do that on SN
241  * anyway).
242  */
243 static int
244 dump_fit(char *page, unsigned long nasid)
245 {
246         unsigned long fentry[2];
247         int index;
248         char *p;
249
250         p = page;
251         for (index=0;;index++) {
252                 BUG_ON(index * 60 > PAGE_SIZE);
253                 if (get_fit_entry(nasid, index, fentry, NULL, 0))
254                         break;
255                 p += dump_fit_entry(p, fentry);
256         }
257
258         return p - page;
259 }
260
261 static int
262 dump_version(char *page, unsigned long nasid)
263 {
264         unsigned long fentry[2];
265         char banner[128];
266         int index;
267         int len;
268
269         for (index = 0; ; index++) {
270                 if (get_fit_entry(nasid, index, fentry, banner,
271                                   sizeof(banner)))
272                         return 0;
273                 if (FIT_TYPE(fentry[1]) == FIT_ENTRY_SAL_A)
274                         break;
275         }
276
277         len = sprintf(page, "%x.%02x\n", FIT_MAJOR(fentry[1]),
278                       FIT_MINOR(fentry[1]));
279         page += len;
280
281         if (banner[0])
282                 len += snprintf(page, PAGE_SIZE-len, "%s\n", banner);
283
284         return len;
285 }
286
287 /* same as in proc_misc.c */
288 static int
289 proc_calc_metrics(char *page, char **start, off_t off, int count, int *eof,
290                   int len)
291 {
292         if (len <= off+count) *eof = 1;
293         *start = page + off;
294         len -= off;
295         if (len>count) len = count;
296         if (len<0) len = 0;
297         return len;
298 }
299
300 static int
301 read_version_entry(char *page, char **start, off_t off, int count, int *eof,
302                    void *data)
303 {
304         int len = 0;
305
306         /* data holds the NASID of the node */
307         len = dump_version(page, (unsigned long)data);
308         len = proc_calc_metrics(page, start, off, count, eof, len);
309         return len;
310 }
311
312 static int
313 read_fit_entry(char *page, char **start, off_t off, int count, int *eof,
314                void *data)
315 {
316         int len = 0;
317
318         /* data holds the NASID of the node */
319         len = dump_fit(page, (unsigned long)data);
320         len = proc_calc_metrics(page, start, off, count, eof, len);
321
322         return len;
323 }
324
325 /* module entry points */
326 int __init prominfo_init(void);
327 void __exit prominfo_exit(void);
328
329 module_init(prominfo_init);
330 module_exit(prominfo_exit);
331
332 static struct proc_dir_entry **proc_entries;
333 static struct proc_dir_entry *sgi_prominfo_entry;
334
335 #define NODE_NAME_LEN 11
336
337 int __init
338 prominfo_init(void)
339 {
340         struct proc_dir_entry **entp;
341         struct proc_dir_entry *p;
342         cnodeid_t cnodeid;
343         unsigned long nasid;
344         char name[NODE_NAME_LEN];
345
346         if (!ia64_platform_is("sn2"))
347                 return 0;
348
349         proc_entries = kmalloc(numnodes * sizeof(struct proc_dir_entry *),
350                                GFP_KERNEL);
351
352         sgi_prominfo_entry = proc_mkdir("sgi_prominfo", NULL);
353
354         for (cnodeid = 0, entp = proc_entries;
355              cnodeid < numnodes;
356              cnodeid++, entp++) {
357                 sprintf(name, "node%d", cnodeid);
358                 *entp = proc_mkdir(name, sgi_prominfo_entry);
359                 nasid = cnodeid_to_nasid(cnodeid);
360                 p = create_proc_read_entry(
361                         "fit", 0, *entp, read_fit_entry,
362                         (void *)nasid);
363                 if (p)
364                         p->owner = THIS_MODULE;
365                 p = create_proc_read_entry(
366                         "version", 0, *entp, read_version_entry,
367                         (void *)nasid);
368                 if (p)
369                         p->owner = THIS_MODULE;
370         }
371
372         return 0;
373 }
374
375 void __exit
376 prominfo_exit(void)
377 {
378         struct proc_dir_entry **entp;
379         unsigned cnodeid;
380         char name[NODE_NAME_LEN];
381
382         for (cnodeid = 0, entp = proc_entries;
383              cnodeid < numnodes;
384              cnodeid++, entp++) {
385                 remove_proc_entry("fit", *entp);
386                 remove_proc_entry("version", *entp);
387                 sprintf(name, "node%d", cnodeid);
388                 remove_proc_entry(name, sgi_prominfo_entry);
389         }
390         remove_proc_entry("sgi_prominfo", NULL);
391         kfree(proc_entries);
392 }