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>
19 #include <linux/slab.h>
24 #include <asm/semaphore.h>
25 #include <asm/machdep.h>
28 #include <asm/param.h>
29 #include <asm/system.h>
30 #include <asm/abs_addr.h>
32 #include <asm/uaccess.h>
34 struct proc_dir_entry *rtas_proc_dir; /* /proc/ppc64/rtas dir */
35 struct flash_block_list_header rtas_firmware_flash_list = {0, 0};
36 struct errinjct_token ei_token_list[MAX_ERRINJCT_TOKENS];
39 * prom_init() is called very early on, before the kernel text
40 * and data have been mapped to KERNELBASE. At this point the code
41 * is running at whatever address it has been loaded at, so
42 * references to extern and static variables must be relocated
43 * explicitly. The procedure reloc_offset() returns the address
44 * we're currently running at minus the address we were linked at.
45 * (Note that strings count as static variables.)
47 * Because OF may have mapped I/O devices into the area starting at
48 * KERNELBASE, particularly on CHRP machines, we can't safely call
49 * OF once the kernel has been mapped to KERNELBASE. Therefore all
50 * OF calls should be done within prom_init(), and prom_init()
51 * and all routines called within it must be careful to relocate
52 * references as necessary.
54 * Note that the bss is cleared *after* prom_init runs, so we have
55 * to make sure that any static or extern variables it accesses
56 * are put in the data segment.
59 struct rtas_t rtas = {
60 .lock = SPIN_LOCK_UNLOCKED
63 extern unsigned long reloc_offset(void);
65 spinlock_t rtas_data_buf_lock = SPIN_LOCK_UNLOCKED;
66 char rtas_data_buf[RTAS_DATA_BUF_SIZE]__page_aligned;
69 phys_call_rtas(int token, int nargs, int nret, ...)
72 unsigned long offset = reloc_offset();
73 struct rtas_args *rtas = PTRRELOC(&(get_paca()->xRtas));
79 rtas->rets = (rtas_arg_t *)PTRRELOC(&(rtas->args[nargs]));
82 for (i = 0; i < nargs; i++)
83 rtas->args[i] = (rtas_arg_t)LONG_LSW(va_arg(list, ulong));
90 phys_call_rtas_display_status(char c)
92 unsigned long offset = reloc_offset();
93 struct rtas_args *rtas = PTRRELOC(&(get_paca()->xRtas));
98 rtas->rets = (rtas_arg_t *)PTRRELOC(&(rtas->args[1]));
99 rtas->args[0] = (int)c;
105 call_rtas_display_status(char c)
107 struct rtas_args *rtas = &(get_paca()->xRtas);
112 rtas->rets = (rtas_arg_t *)&(rtas->args[1]);
113 rtas->args[0] = (int)c;
115 enter_rtas((void *)__pa((unsigned long)rtas));
120 rtas_token(const char *service)
123 if (rtas.dev == NULL) {
124 PPCDBG(PPCDBG_RTAS,"\tNo rtas device in device-tree...\n");
125 return RTAS_UNKNOWN_SERVICE;
127 tokp = (int *) get_property(rtas.dev, service, NULL);
128 return tokp ? *tokp : RTAS_UNKNOWN_SERVICE;
133 rtas_call(int token, int nargs, int nret,
134 unsigned long *outputs, ...)
139 struct rtas_args *rtas_args = &(get_paca()->xRtas);
141 PPCDBG(PPCDBG_RTAS, "Entering rtas_call\n");
142 PPCDBG(PPCDBG_RTAS, "\ttoken = 0x%x\n", token);
143 PPCDBG(PPCDBG_RTAS, "\tnargs = %d\n", nargs);
144 PPCDBG(PPCDBG_RTAS, "\tnret = %d\n", nret);
145 PPCDBG(PPCDBG_RTAS, "\t&outputs = 0x%lx\n", outputs);
146 if (token == RTAS_UNKNOWN_SERVICE)
149 rtas_args->token = token;
150 rtas_args->nargs = nargs;
151 rtas_args->nret = nret;
152 rtas_args->rets = (rtas_arg_t *)&(rtas_args->args[nargs]);
153 va_start(list, outputs);
154 for (i = 0; i < nargs; ++i) {
155 rtas_args->args[i] = (rtas_arg_t)LONG_LSW(va_arg(list, ulong));
156 PPCDBG(PPCDBG_RTAS, "\tnarg[%d] = 0x%lx\n", i, rtas_args->args[i]);
160 for (i = 0; i < nret; ++i)
161 rtas_args->rets[i] = 0;
163 #if 0 /* Gotta do something different here, use global lock for now... */
164 spin_lock_irqsave(&rtas_args->lock, s);
166 spin_lock_irqsave(&rtas.lock, s);
168 PPCDBG(PPCDBG_RTAS, "\tentering rtas with 0x%lx\n",
169 (void *)__pa((unsigned long)rtas_args));
170 enter_rtas((void *)__pa((unsigned long)rtas_args));
171 PPCDBG(PPCDBG_RTAS, "\treturned from rtas ...\n");
172 #if 0 /* Gotta do something different here, use global lock for now... */
173 spin_unlock_irqrestore(&rtas_args->lock, s);
175 spin_unlock_irqrestore(&rtas.lock, s);
177 ifppcdebug(PPCDBG_RTAS) {
178 for(i=0; i < nret ;i++)
179 udbg_printf("\tnret[%d] = 0x%lx\n", i, (ulong)rtas_args->rets[i]);
182 if (nret > 1 && outputs != NULL)
183 for (i = 0; i < nret-1; ++i)
184 outputs[i] = rtas_args->rets[i+1];
185 return (ulong)((nret > 0) ? rtas_args->rets[0] : 0);
188 /* Given an RTAS status code of 990n compute the hinted delay of 10^n
189 * (last digit) milliseconds. For now we bound at n=3 (1 sec).
192 rtas_extended_busy_delay_time(int status)
194 int order = status - 9900;
198 order = 0; /* RTC depends on this for -2 clock busy */
200 order = 3; /* bound */
202 /* Use microseconds for reasonable accuracy */
203 for (ms = 1000; order > 0; order--)
205 return ms / (1000000/HZ); /* round down is fine */
208 #define FLASH_BLOCK_LIST_VERSION (1UL)
210 rtas_flash_firmware(void)
212 unsigned long image_size;
213 struct flash_block_list *f, *next, *flist;
214 unsigned long rtas_block_list;
215 int i, status, update_token;
217 update_token = rtas_token("ibm,update-flash-64-and-reboot");
218 if (update_token == RTAS_UNKNOWN_SERVICE) {
219 printk(KERN_ALERT "FLASH: ibm,update-flash-64-and-reboot is not available -- not a service partition?\n");
220 printk(KERN_ALERT "FLASH: firmware will not be flashed\n");
224 /* NOTE: the "first" block list is a global var with no data
225 * blocks in the kernel data segment. We do this because
226 * we want to ensure this block_list addr is under 4GB.
228 rtas_firmware_flash_list.num_blocks = 0;
229 flist = (struct flash_block_list *)&rtas_firmware_flash_list;
230 rtas_block_list = virt_to_absolute((unsigned long)flist);
232 if (rtas_block_list >= 4UL*1024*1024*1024) {
233 printk(KERN_ALERT "FLASH: kernel bug...flash list header addr above 4GB\n");
237 printk(KERN_ALERT "FLASH: preparing saved firmware image for flash\n");
238 /* Update the block_list in place. */
240 for (f = flist; f; f = next) {
241 /* Translate data addrs to absolute */
242 for (i = 0; i < f->num_blocks; i++) {
243 f->blocks[i].data = (char *)virt_to_absolute((unsigned long)f->blocks[i].data);
244 image_size += f->blocks[i].length;
247 /* Don't translate final NULL pointer */
249 f->next = (struct flash_block_list *)virt_to_absolute((unsigned long)f->next);
252 /* make num_blocks into the version/length field */
253 f->num_blocks = (FLASH_BLOCK_LIST_VERSION << 56) | ((f->num_blocks+1)*16);
256 printk(KERN_ALERT "FLASH: flash image is %ld bytes\n", image_size);
257 printk(KERN_ALERT "FLASH: performing flash and reboot\n");
258 ppc_md.progress("Flashing \n", 0x0);
259 ppc_md.progress("Please Wait... ", 0x0);
260 printk(KERN_ALERT "FLASH: this will take several minutes. Do not power off!\n");
261 status = rtas_call(update_token, 1, 1, NULL, rtas_block_list);
262 switch (status) { /* should only get "bad" status */
264 printk(KERN_ALERT "FLASH: success\n");
267 printk(KERN_ALERT "FLASH: hardware error. Firmware may not be not flashed\n");
270 printk(KERN_ALERT "FLASH: image is corrupt or not correct for this platform. Firmware not flashed\n");
273 printk(KERN_ALERT "FLASH: flash failed when partially complete. System may not reboot\n");
276 printk(KERN_ALERT "FLASH: unknown flash return code %d\n", status);
281 void rtas_flash_bypass_warning(void)
283 printk(KERN_ALERT "FLASH: firmware flash requires a reboot\n");
284 printk(KERN_ALERT "FLASH: the firmware image will NOT be flashed\n");
289 rtas_restart(char *cmd)
291 if (rtas_firmware_flash_list.next)
292 rtas_flash_firmware();
294 printk("RTAS system-reboot returned %ld\n",
295 rtas_call(rtas_token("system-reboot"), 0, 1, NULL));
302 if (rtas_firmware_flash_list.next)
303 rtas_flash_bypass_warning();
304 /* allow power on only with power button press */
305 printk("RTAS power-off returned %ld\n",
306 rtas_call(rtas_token("power-off"), 2, 1, NULL,0xffffffff,0xffffffff));
313 if (rtas_firmware_flash_list.next)
314 rtas_flash_bypass_warning();
319 rtas_errinjct_open(void)
325 /* The rc and open_token values are backwards due to a misprint in
327 open_token = rtas_call(rtas_token("ibm,open-errinjct"), 0, 2, (void *) &ret);
331 printk(KERN_WARNING "error: ibm,open-errinjct failed (%d)\n", rc);
339 rtas_errinjct(unsigned int open_token, char * ei_token, char * in_workspace)
341 struct errinjct_token * ei;
342 int rtas_ei_token = -1;
347 for (i = 0; i < MAX_ERRINJCT_TOKENS && ei->name; i++) {
348 if (strcmp(ei_token, ei->name) == 0) {
349 rtas_ei_token = ei->value;
354 if (rtas_ei_token == -1) {
358 spin_lock(&rtas_data_buf_lock);
361 memcpy(rtas_data_buf, in_workspace, RTAS_DATA_BUF_SIZE);
363 rc = rtas_call(rtas_token("ibm,errinjct"), 3, 1, NULL, rtas_ei_token,
364 open_token, __pa(rtas_data_buf));
366 spin_unlock(&rtas_data_buf_lock);
372 rtas_errinjct_close(unsigned int open_token)
376 rc = rtas_call(rtas_token("ibm,close-errinjct"), 1, 1, NULL, open_token);
378 printk(KERN_WARNING "error: ibm,close-errinjct failed (%d)\n", rc);
385 #ifndef CONFIG_PPC_ISERIES
386 static int __init rtas_errinjct_init(void)
394 token_array = (char *) get_property(rtas.dev, "ibm,errinjct-tokens",
396 /* if token is not found, then we fall through loop */
397 end_array = token_array + array_len;
398 for (i = 0, j = 0; i < MAX_ERRINJCT_TOKENS && token_array < end_array; i++) {
400 len = strnlen(token_array, ERRINJCT_TOKEN_LEN) + 1;
401 ei_token_list[i].name = (char *) kmalloc(len, GFP_KERNEL);
402 if (!ei_token_list[i].name) {
403 printk(KERN_WARNING "error: kmalloc failed\n");
407 strcpy(ei_token_list[i].name, token_array);
410 ei_token_list[i].value = *(int *)token_array;
411 token_array += sizeof(int);
413 for (; i < MAX_ERRINJCT_TOKENS; i++) {
414 ei_token_list[i].name = 0;
415 ei_token_list[i].value = 0;
420 __initcall(rtas_errinjct_init);