3 * Procedures for interfacing to the RTAS on CHRP machines.
5 * Peter Bergner, IBM March 2001.
6 * Copyright (C) 2001 IBM.
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.
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/spinlock.h>
23 #include <asm/semaphore.h>
24 #include <asm/machdep.h>
27 #include <asm/system.h>
28 #include <asm/abs_addr.h>
31 struct proc_dir_entry *rtas_proc_dir; /* /proc/ppc64/rtas dir */
32 struct flash_block_list_header rtas_firmware_flash_list = {0, 0};
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.)
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.
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.
55 struct rtas_t rtas = {
56 .lock = SPIN_LOCK_UNLOCKED
59 extern unsigned long reloc_offset(void);
61 spinlock_t rtas_data_buf_lock = SPIN_LOCK_UNLOCKED;
62 char rtas_data_buf[RTAS_DATA_BUF_SIZE];
65 phys_call_rtas(int token, int nargs, int nret, ...)
68 unsigned long offset = reloc_offset();
69 struct rtas_args *rtas = PTRRELOC(&(get_paca()->xRtas));
75 rtas->rets = (rtas_arg_t *)PTRRELOC(&(rtas->args[nargs]));
78 for (i = 0; i < nargs; i++)
79 rtas->args[i] = (rtas_arg_t)LONG_LSW(va_arg(list, ulong));
86 phys_call_rtas_display_status(char c)
88 unsigned long offset = reloc_offset();
89 struct rtas_args *rtas = PTRRELOC(&(get_paca()->xRtas));
94 rtas->rets = (rtas_arg_t *)PTRRELOC(&(rtas->args[1]));
95 rtas->args[0] = (int)c;
101 call_rtas_display_status(char c)
103 struct rtas_args *rtas = &(get_paca()->xRtas);
108 rtas->rets = (rtas_arg_t *)&(rtas->args[1]);
109 rtas->args[0] = (int)c;
111 enter_rtas((void *)__pa((unsigned long)rtas));
116 rtas_token(const char *service)
119 if (rtas.dev == NULL) {
120 PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
121 return RTAS_UNKNOWN_SERVICE;
123 tokp = (int *) get_property(rtas.dev, service, NULL);
124 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
129 rtas_call(int token, int nargs, int nret,
130 unsigned long *outputs, ...)
135 struct rtas_args *rtas_args = &(get_paca()->xRtas);
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)
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]);
156 for (i = 0; i < nret; ++i)
157 rtas_args->rets[i] = 0;
159 #if 0 /* Gotta do something different here, use global lock for now... */
160 spin_lock_irqsave(&rtas_args->lock, s);
162 spin_lock_irqsave(&rtas.lock, s);
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);
171 spin_unlock_irqrestore(&rtas.lock, s);
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]);
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);
184 #define FLASH_BLOCK_LIST_VERSION (1UL)
186 rtas_flash_firmware(void)
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;
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");
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.
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");
212 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
213 /* Update the block_list in place. */
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;
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);
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 */
235 printk(KERN_ALERT "FLASH: success\n");
238 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
241 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
244 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
247 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
252 void rtas_flash_bypass_warning(void)
254 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
255 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
260 rtas_restart(char *cmd)
262 if (rtas_firmware_flash_list.next)
263 rtas_flash_firmware();
265 printk("RTAS system-reboot returned %ld\n",
266 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
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));
284 if (rtas_firmware_flash_list.next)
285 rtas_flash_bypass_warning();