cleanup
[linux-2.4.21-pre4.git] / arch / ppc64 / kernel / rtas.c
1 /*
2  *
3  * Procedures for interfacing to the RTAS on CHRP machines.
4  *
5  * Peter Bergner, IBM   March 2001.
6  * Copyright (C) 2001 IBM.
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13
14 #include <stdarg.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/spinlock.h>
18 #include <linux/fs.h>
19
20 #include <asm/init.h>
21 #include <asm/prom.h>
22 #include <asm/rtas.h>
23 #include <asm/semaphore.h>
24 #include <asm/machdep.h>
25 #include <asm/paca.h>
26 #include <asm/page.h>
27 #include <asm/system.h>
28 #include <asm/abs_addr.h>
29 #include <asm/udbg.h>
30
31 struct proc_dir_entry *rtas_proc_dir;   /* /proc/ppc64/rtas dir */
32 struct flash_block_list_header rtas_firmware_flash_list = {0, 0};
33
34 /*
35  * prom_init() is called very early on, before the kernel text
36  * and data have been mapped to KERNELBASE.  At this point the code
37  * is running at whatever address it has been loaded at, so
38  * references to extern and static variables must be relocated
39  * explicitly.  The procedure reloc_offset() returns the address
40  * we're currently running at minus the address we were linked at.
41  * (Note that strings count as static variables.)
42  *
43  * Because OF may have mapped I/O devices into the area starting at
44  * KERNELBASE, particularly on CHRP machines, we can't safely call
45  * OF once the kernel has been mapped to KERNELBASE.  Therefore all
46  * OF calls should be done within prom_init(), and prom_init()
47  * and all routines called within it must be careful to relocate
48  * references as necessary.
49  *
50  * Note that the bss is cleared *after* prom_init runs, so we have
51  * to make sure that any static or extern variables it accesses
52  * are put in the data segment.
53  */
54
55 struct rtas_t rtas = { 
56         .lock = SPIN_LOCK_UNLOCKED
57 };
58
59 extern unsigned long reloc_offset(void);
60
61 spinlock_t rtas_data_buf_lock = SPIN_LOCK_UNLOCKED;
62 char rtas_data_buf[RTAS_DATA_BUF_SIZE];
63
64 void
65 phys_call_rtas(int token, int nargs, int nret, ...)
66 {
67         va_list list;
68         unsigned long offset = reloc_offset();
69         struct rtas_args *rtas = PTRRELOC(&(get_paca()->xRtas));
70         int i;
71
72         rtas->token = token;
73         rtas->nargs = nargs;
74         rtas->nret  = nret;
75         rtas->rets  = (rtas_arg_t *)PTRRELOC(&(rtas->args[nargs]));
76
77         va_start(list, nret);
78         for (i = 0; i < nargs; i++)
79           rtas->args[i] = (rtas_arg_t)LONG_LSW(va_arg(list, ulong));
80         va_end(list);
81
82         enter_rtas(rtas);       
83 }
84
85 void
86 phys_call_rtas_display_status(char c)
87 {
88         unsigned long offset = reloc_offset();
89         struct rtas_args *rtas = PTRRELOC(&(get_paca()->xRtas));
90
91         rtas->token = 10;
92         rtas->nargs = 1;
93         rtas->nret  = 1;
94         rtas->rets  = (rtas_arg_t *)PTRRELOC(&(rtas->args[1]));
95         rtas->args[0] = (int)c;
96
97         enter_rtas(rtas);       
98 }
99
100 void
101 call_rtas_display_status(char c)
102 {
103         struct rtas_args *rtas = &(get_paca()->xRtas);
104
105         rtas->token = 10;
106         rtas->nargs = 1;
107         rtas->nret  = 1;
108         rtas->rets  = (rtas_arg_t *)&(rtas->args[1]);
109         rtas->args[0] = (int)c;
110
111         enter_rtas((void *)__pa((unsigned long)rtas));  
112 }
113
114 __openfirmware
115 int
116 rtas_token(const char *service)
117 {
118         int *tokp;
119         if (rtas.dev == NULL) {
120                 PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
121                 return RTAS_UNKNOWN_SERVICE;
122         }
123         tokp = (int *) get_property(rtas.dev, service, NULL);
124         return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
125 }
126
127 __openfirmware
128 long
129 rtas_call(int token, int nargs, int nret,
130           unsigned long *outputs, ...)
131 {
132         va_list list;
133         int i;
134         unsigned long s;
135         struct rtas_args *rtas_args = &(get_paca()->xRtas);
136
137         PPCDBG(PPCDBG_RTAS, "Entering rtas_call\n");
138         PPCDBG(PPCDBG_RTAS, "\ttoken    = 0x%x\n", token);
139         PPCDBG(PPCDBG_RTAS, "\tnargs    = %d\n", nargs);
140         PPCDBG(PPCDBG_RTAS, "\tnret     = %d\n", nret);
141         PPCDBG(PPCDBG_RTAS, "\t&outputs = 0x%lx\n", outputs);
142         if (token == RTAS_UNKNOWN_SERVICE)
143                 return -1;
144
145         rtas_args->token = token;
146         rtas_args->nargs = nargs;
147         rtas_args->nret  = nret;
148         rtas_args->rets  = (rtas_arg_t *)&(rtas_args->args[nargs]);
149         va_start(list, outputs);
150         for (i = 0; i < nargs; ++i) {
151                 rtas_args->args[i] = (rtas_arg_t)LONG_LSW(va_arg(list, ulong));
152                 PPCDBG(PPCDBG_RTAS, "\tnarg[%d] = 0x%lx\n", i, rtas_args->args[i]);
153         }
154         va_end(list);
155
156         for (i = 0; i < nret; ++i)
157           rtas_args->rets[i] = 0;
158
159 #if 0   /* Gotta do something different here, use global lock for now... */
160         spin_lock_irqsave(&rtas_args->lock, s);
161 #else
162         spin_lock_irqsave(&rtas.lock, s);
163 #endif
164         PPCDBG(PPCDBG_RTAS, "\tentering rtas with 0x%lx\n",
165                 (void *)__pa((unsigned long)rtas_args));
166         enter_rtas((void *)__pa((unsigned long)rtas_args));
167         PPCDBG(PPCDBG_RTAS, "\treturned from rtas ...\n");
168 #if 0   /* Gotta do something different here, use global lock for now... */
169         spin_unlock_irqrestore(&rtas_args->lock, s);
170 #else
171         spin_unlock_irqrestore(&rtas.lock, s);
172 #endif
173         ifppcdebug(PPCDBG_RTAS) {
174                 for(i=0; i < nret ;i++)
175                         udbg_printf("\tnret[%d] = 0x%lx\n", i, (ulong)rtas_args->rets[i]);
176         }
177
178         if (nret > 1 && outputs != NULL)
179                 for (i = 0; i < nret-1; ++i)
180                         outputs[i] = rtas_args->rets[i+1];
181         return (ulong)((nret > 0) ? rtas_args->rets[0] : 0);
182 }
183
184 #define FLASH_BLOCK_LIST_VERSION (1UL)
185 static void
186 rtas_flash_firmware(void)
187 {
188         unsigned long image_size;
189         struct flash_block_list *f, *next, *flist;
190         unsigned long rtas_block_list;
191         int i, status, update_token;
192
193         update_token = rtas_token("ibm,update-flash-64-and-reboot");
194         if (update_token == RTAS_UNKNOWN_SERVICE) {
195                 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
196                 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
197                 return;
198         }
199
200         /* NOTE: the "first" block list is a global var with no data
201          * blocks in the kernel data segment.  We do this because
202          * we want to ensure this block_list addr is under 4GB.
203          */
204         rtas_firmware_flash_list.num_blocks = 0;
205         flist = (struct flash_block_list *)&rtas_firmware_flash_list;
206         rtas_block_list = virt_to_absolute((unsigned long)flist);
207         if (rtas_block_list >= (4UL << 20)) {
208                 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
209                 return;
210         }
211
212         printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
213         /* Update the block_list in place. */
214         image_size = 0;
215         for (f = flist; f; f = next) {
216                 /* Translate data addrs to absolute */
217                 for (i = 0; i < f->num_blocks; i++) {
218                         f->blocks[i].data = (char *)virt_to_absolute((unsigned long)f->blocks[i].data);
219                         image_size += f->blocks[i].length;
220                 }
221                 next = f->next;
222                 f->next = (struct flash_block_list *)virt_to_absolute((unsigned long)f->next);
223                 /* make num_blocks into the version/length field */
224                 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
225         }
226
227         printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
228         printk(KERN_ALERT "FLASH: performing flash and reboot\n");
229         ppc_md.progress("Flashing        \n", 0x0);
230         ppc_md.progress("Please Wait...  ", 0x0);
231         printk(KERN_ALERT "FLASH: this will take several minutes.  Do not power off!\n");
232         status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
233         switch (status) {       /* should only get "bad" status */
234             case 0:
235                 printk(KERN_ALERT "FLASH: success\n");
236                 break;
237             case -1:
238                 printk(KERN_ALERT "FLASH: hardware error.  Firmware may not be not flashed\n");
239                 break;
240             case -3:
241                 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform.  Firmware not flashed\n");
242                 break;
243             case -4:
244                 printk(KERN_ALERT "FLASH: flash failed when partially complete.  System may not reboot\n");
245                 break;
246             default:
247                 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
248                 break;
249         }
250 }
251
252 void rtas_flash_bypass_warning(void)
253 {
254         printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
255         printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
256 }
257
258
259 void __chrp
260 rtas_restart(char *cmd)
261 {
262         if (rtas_firmware_flash_list.next)
263                 rtas_flash_firmware();
264
265         printk("RTAS system-reboot returned %ld\n",
266                rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
267         for (;;);
268 }
269
270 void __chrp
271 rtas_power_off(void)
272 {
273         if (rtas_firmware_flash_list.next)
274                 rtas_flash_bypass_warning();
275         /* allow power on only with power button press */
276         printk("RTAS power-off returned %ld\n",
277                rtas_call(rtas_token("power-off"), 2, 1, NULL,0xffffffff,0xffffffff));
278         for (;;);
279 }
280
281 void __chrp
282 rtas_halt(void)
283 {
284         if (rtas_firmware_flash_list.next)
285                 rtas_flash_bypass_warning();
286         rtas_power_off();
287 }