blob: d24bc44e361ee20b654960cc7784176670705da6 [file] [log] [blame]
Adrian Bunkb00dc832008-05-19 16:52:27 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * misc.c: Miscellaneous prom functions that don't belong
3 * anywhere else.
4 *
5 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
6 * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/types.h>
10#include <linux/kernel.h>
11#include <linux/sched.h>
12#include <linux/interrupt.h>
13#include <linux/delay.h>
Sam Ravnborg917c3662009-01-08 16:58:20 -080014#include <linux/module.h>
15
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/openprom.h>
17#include <asm/oplib.h>
18#include <asm/system.h>
David S. Millerb3e13fb2007-07-12 15:55:55 -070019#include <asm/ldc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
David S. Miller22d6a1c2007-05-25 00:37:12 -070021int prom_service_exists(const char *service_name)
22{
David S. Miller25edd692010-08-23 23:10:57 -070023 unsigned long args[5];
David S. Miller22d6a1c2007-05-25 00:37:12 -070024
David S. Miller25edd692010-08-23 23:10:57 -070025 args[0] = (unsigned long) "test";
26 args[1] = 1;
27 args[2] = 1;
28 args[3] = (unsigned long) service_name;
29 args[4] = (unsigned long) -1;
30
31 p1275_cmd_direct(args);
32
33 if (args[4])
David S. Miller22d6a1c2007-05-25 00:37:12 -070034 return 0;
35 return 1;
36}
37
38void prom_sun4v_guest_soft_state(void)
39{
40 const char *svc = "SUNW,soft-state-supported";
David S. Miller25edd692010-08-23 23:10:57 -070041 unsigned long args[3];
David S. Miller22d6a1c2007-05-25 00:37:12 -070042
43 if (!prom_service_exists(svc))
44 return;
David S. Miller25edd692010-08-23 23:10:57 -070045 args[0] = (unsigned long) svc;
46 args[1] = 0;
47 args[2] = 0;
48 p1275_cmd_direct(args);
David S. Miller22d6a1c2007-05-25 00:37:12 -070049}
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051/* Reset and reboot the machine with the command 'bcommand'. */
David S. Millerbff06d52005-09-22 20:11:33 -070052void prom_reboot(const char *bcommand)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
David S. Miller25edd692010-08-23 23:10:57 -070054 unsigned long args[4];
55
David S. Millerb3e13fb2007-07-12 15:55:55 -070056#ifdef CONFIG_SUN_LDOMS
57 if (ldom_domaining_enabled)
58 ldom_reboot(bcommand);
59#endif
David S. Miller25edd692010-08-23 23:10:57 -070060 args[0] = (unsigned long) "boot";
61 args[1] = 1;
62 args[2] = 0;
63 args[3] = (unsigned long) bcommand;
64
65 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68/* Forth evaluate the expression contained in 'fstring'. */
David S. Millerbff06d52005-09-22 20:11:33 -070069void prom_feval(const char *fstring)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
David S. Miller25edd692010-08-23 23:10:57 -070071 unsigned long args[5];
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (!fstring || fstring[0] == 0)
74 return;
David S. Miller25edd692010-08-23 23:10:57 -070075 args[0] = (unsigned long) "interpret";
76 args[1] = 1;
77 args[2] = 1;
78 args[3] = (unsigned long) fstring;
79 args[4] = (unsigned long) -1;
80
81 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
Sam Ravnborg917c3662009-01-08 16:58:20 -080083EXPORT_SYMBOL(prom_feval);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#ifdef CONFIG_SMP
86extern void smp_capture(void);
87extern void smp_release(void);
88#endif
89
90/* Drop into the prom, with the chance to continue with the 'go'
91 * prom command.
92 */
93void prom_cmdline(void)
94{
David S. Miller25edd692010-08-23 23:10:57 -070095 unsigned long args[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 unsigned long flags;
97
98 local_irq_save(flags);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#ifdef CONFIG_SMP
101 smp_capture();
102#endif
103
David S. Miller25edd692010-08-23 23:10:57 -0700104 args[0] = (unsigned long) "enter";
105 args[1] = 0;
106 args[2] = 0;
107
108 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110#ifdef CONFIG_SMP
111 smp_release();
112#endif
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 local_irq_restore(flags);
115}
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117/* Drop into the prom, but completely terminate the program.
118 * No chance of continuing.
119 */
David S. Millerbd4352c2009-09-04 03:38:54 -0700120void notrace prom_halt(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
David S. Miller25edd692010-08-23 23:10:57 -0700122 unsigned long args[3];
123
David S. Miller4f0234f2007-07-13 16:03:42 -0700124#ifdef CONFIG_SUN_LDOMS
125 if (ldom_domaining_enabled)
126 ldom_power_off();
127#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128again:
David S. Miller25edd692010-08-23 23:10:57 -0700129 args[0] = (unsigned long) "exit";
130 args[1] = 0;
131 args[2] = 0;
132 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 goto again; /* PROM is out to get me -DaveM */
134}
135
136void prom_halt_power_off(void)
137{
David S. Miller25edd692010-08-23 23:10:57 -0700138 unsigned long args[3];
139
David S. Miller4f0234f2007-07-13 16:03:42 -0700140#ifdef CONFIG_SUN_LDOMS
141 if (ldom_domaining_enabled)
142 ldom_power_off();
143#endif
David S. Miller25edd692010-08-23 23:10:57 -0700144 args[0] = (unsigned long) "SUNW,power-off";
145 args[1] = 0;
146 args[2] = 0;
147 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 /* if nothing else helps, we just halt */
150 prom_halt();
151}
152
153/* Set prom sync handler to call function 'funcp'. */
154void prom_setcallback(callback_func_t funcp)
155{
David S. Miller25edd692010-08-23 23:10:57 -0700156 unsigned long args[5];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!funcp)
158 return;
David S. Miller25edd692010-08-23 23:10:57 -0700159 args[0] = (unsigned long) "set-callback";
160 args[1] = 1;
161 args[2] = 1;
162 args[3] = (unsigned long) funcp;
163 args[4] = (unsigned long) -1;
164 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167/* Get the idprom and stuff it into buffer 'idbuf'. Returns the
168 * format type. 'num_bytes' is the number of bytes that your idbuf
169 * has space for. Returns 0xff on error.
170 */
171unsigned char prom_get_idprom(char *idbuf, int num_bytes)
172{
173 int len;
174
175 len = prom_getproplen(prom_root_node, "idprom");
176 if ((len >num_bytes) || (len == -1))
177 return 0xff;
178 if (!prom_getproperty(prom_root_node, "idprom", idbuf, num_bytes))
179 return idbuf[0];
180
181 return 0xff;
182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184int prom_get_mmu_ihandle(void)
185{
Andres Salomon8d125562010-10-08 14:18:11 -0700186 phandle node;
187 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
David S. Millerbff06d52005-09-22 20:11:33 -0700189 if (prom_mmu_ihandle_cache != 0)
190 return prom_mmu_ihandle_cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
David S. Millerbff06d52005-09-22 20:11:33 -0700192 node = prom_finddevice(prom_chosen_path);
193 ret = prom_getint(node, prom_mmu_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (ret == -1 || ret == 0)
David S. Millerbff06d52005-09-22 20:11:33 -0700195 prom_mmu_ihandle_cache = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 else
David S. Millerbff06d52005-09-22 20:11:33 -0700197 prom_mmu_ihandle_cache = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 return ret;
200}
201
202static int prom_get_memory_ihandle(void)
203{
204 static int memory_ihandle_cache;
Andres Salomon8d125562010-10-08 14:18:11 -0700205 phandle node;
206 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 if (memory_ihandle_cache != 0)
209 return memory_ihandle_cache;
210
211 node = prom_finddevice("/chosen");
212 ret = prom_getint(node, "memory");
213 if (ret == -1 || ret == 0)
214 memory_ihandle_cache = -1;
215 else
216 memory_ihandle_cache = ret;
217
218 return ret;
219}
220
221/* Load explicit I/D TLB entries. */
David S. Miller25edd692010-08-23 23:10:57 -0700222static long tlb_load(const char *type, unsigned long index,
223 unsigned long tte_data, unsigned long vaddr)
224{
225 unsigned long args[9];
226
227 args[0] = (unsigned long) prom_callmethod_name;
228 args[1] = 5;
229 args[2] = 1;
230 args[3] = (unsigned long) type;
231 args[4] = (unsigned int) prom_get_mmu_ihandle();
232 args[5] = vaddr;
233 args[6] = tte_data;
234 args[7] = index;
235 args[8] = (unsigned long) -1;
236
237 p1275_cmd_direct(args);
238
239 return (long) args[8];
240}
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242long prom_itlb_load(unsigned long index,
243 unsigned long tte_data,
244 unsigned long vaddr)
245{
David S. Miller25edd692010-08-23 23:10:57 -0700246 return tlb_load("SUNW,itlb-load", index, tte_data, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249long prom_dtlb_load(unsigned long index,
250 unsigned long tte_data,
251 unsigned long vaddr)
252{
David S. Miller25edd692010-08-23 23:10:57 -0700253 return tlb_load("SUNW,dtlb-load", index, tte_data, vaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254}
255
256int prom_map(int mode, unsigned long size,
257 unsigned long vaddr, unsigned long paddr)
258{
David S. Miller25edd692010-08-23 23:10:57 -0700259 unsigned long args[11];
260 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
David S. Miller25edd692010-08-23 23:10:57 -0700262 args[0] = (unsigned long) prom_callmethod_name;
263 args[1] = 7;
264 args[2] = 1;
265 args[3] = (unsigned long) prom_map_name;
266 args[4] = (unsigned int) prom_get_mmu_ihandle();
267 args[5] = (unsigned int) mode;
268 args[6] = size;
269 args[7] = vaddr;
270 args[8] = 0;
271 args[9] = paddr;
272 args[10] = (unsigned long) -1;
273
274 p1275_cmd_direct(args);
275
276 ret = (int) args[10];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (ret == 0)
278 ret = -1;
279 return ret;
280}
281
282void prom_unmap(unsigned long size, unsigned long vaddr)
283{
David S. Miller25edd692010-08-23 23:10:57 -0700284 unsigned long args[7];
285
286 args[0] = (unsigned long) prom_callmethod_name;
287 args[1] = 4;
288 args[2] = 0;
289 args[3] = (unsigned long) prom_unmap_name;
290 args[4] = (unsigned int) prom_get_mmu_ihandle();
291 args[5] = size;
292 args[6] = vaddr;
293
294 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
297/* Set aside physical memory which is not touched or modified
298 * across soft resets.
299 */
David S. Miller25edd692010-08-23 23:10:57 -0700300int prom_retain(const char *name, unsigned long size,
301 unsigned long align, unsigned long *paddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
David S. Miller25edd692010-08-23 23:10:57 -0700303 unsigned long args[11];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
David S. Miller25edd692010-08-23 23:10:57 -0700305 args[0] = (unsigned long) prom_callmethod_name;
306 args[1] = 5;
307 args[2] = 3;
308 args[3] = (unsigned long) "SUNW,retain";
309 args[4] = (unsigned int) prom_get_memory_ihandle();
310 args[5] = align;
311 args[6] = size;
312 args[7] = (unsigned long) name;
313 args[8] = (unsigned long) -1;
314 args[9] = (unsigned long) -1;
315 args[10] = (unsigned long) -1;
316
317 p1275_cmd_direct(args);
318
319 if (args[8])
320 return (int) args[8];
321
322 /* Next we get "phys_high" then "phys_low". On 64-bit
323 * the phys_high cell is don't care since the phys_low
324 * cell has the full value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 */
David S. Miller25edd692010-08-23 23:10:57 -0700326 *paddr = args[10];
327
328 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
331/* Get "Unumber" string for the SIMM at the given
332 * memory address. Usually this will be of the form
333 * "Uxxxx" where xxxx is a decimal number which is
334 * etched into the motherboard next to the SIMM slot
335 * in question.
336 */
337int prom_getunumber(int syndrome_code,
338 unsigned long phys_addr,
339 char *buf, int buflen)
340{
David S. Miller25edd692010-08-23 23:10:57 -0700341 unsigned long args[12];
342
343 args[0] = (unsigned long) prom_callmethod_name;
344 args[1] = 7;
345 args[2] = 2;
346 args[3] = (unsigned long) "SUNW,get-unumber";
347 args[4] = (unsigned int) prom_get_memory_ihandle();
348 args[5] = buflen;
349 args[6] = (unsigned long) buf;
350 args[7] = 0;
351 args[8] = phys_addr;
352 args[9] = (unsigned int) syndrome_code;
353 args[10] = (unsigned long) -1;
354 args[11] = (unsigned long) -1;
355
356 p1275_cmd_direct(args);
357
358 return (int) args[10];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
361/* Power management extensions. */
362void prom_sleepself(void)
363{
David S. Miller25edd692010-08-23 23:10:57 -0700364 unsigned long args[3];
365
366 args[0] = (unsigned long) "SUNW,sleep-self";
367 args[1] = 0;
368 args[2] = 0;
369 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
372int prom_sleepsystem(void)
373{
David S. Miller25edd692010-08-23 23:10:57 -0700374 unsigned long args[4];
375
376 args[0] = (unsigned long) "SUNW,sleep-system";
377 args[1] = 0;
378 args[2] = 1;
379 args[3] = (unsigned long) -1;
380 p1275_cmd_direct(args);
381
382 return (int) args[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385int prom_wakeupsystem(void)
386{
David S. Miller25edd692010-08-23 23:10:57 -0700387 unsigned long args[4];
388
389 args[0] = (unsigned long) "SUNW,wakeup-system";
390 args[1] = 0;
391 args[2] = 1;
392 args[3] = (unsigned long) -1;
393 p1275_cmd_direct(args);
394
395 return (int) args[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396}
397
398#ifdef CONFIG_SMP
David S. Miller7890f792006-02-15 02:26:54 -0800399void prom_startcpu(int cpunode, unsigned long pc, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
David S. Miller25edd692010-08-23 23:10:57 -0700401 unsigned long args[6];
402
403 args[0] = (unsigned long) "SUNW,start-cpu";
404 args[1] = 3;
405 args[2] = 0;
406 args[3] = (unsigned int) cpunode;
407 args[4] = pc;
408 args[5] = arg;
409 p1275_cmd_direct(args);
David S. Miller7890f792006-02-15 02:26:54 -0800410}
411
412void prom_startcpu_cpuid(int cpuid, unsigned long pc, unsigned long arg)
413{
David S. Miller25edd692010-08-23 23:10:57 -0700414 unsigned long args[6];
415
416 args[0] = (unsigned long) "SUNW,start-cpu-by-cpuid";
417 args[1] = 3;
418 args[2] = 0;
419 args[3] = (unsigned int) cpuid;
420 args[4] = pc;
421 args[5] = arg;
422 p1275_cmd_direct(args);
David S. Miller7890f792006-02-15 02:26:54 -0800423}
424
425void prom_stopcpu_cpuid(int cpuid)
426{
David S. Miller25edd692010-08-23 23:10:57 -0700427 unsigned long args[4];
428
429 args[0] = (unsigned long) "SUNW,stop-cpu-by-cpuid";
430 args[1] = 1;
431 args[2] = 0;
432 args[3] = (unsigned int) cpuid;
433 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436void prom_stopself(void)
437{
David S. Miller25edd692010-08-23 23:10:57 -0700438 unsigned long args[3];
439
440 args[0] = (unsigned long) "SUNW,stop-self";
441 args[1] = 0;
442 args[2] = 0;
443 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446void prom_idleself(void)
447{
David S. Miller25edd692010-08-23 23:10:57 -0700448 unsigned long args[3];
449
450 args[0] = (unsigned long) "SUNW,idle-self";
451 args[1] = 0;
452 args[2] = 0;
453 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
456void prom_resumecpu(int cpunode)
457{
David S. Miller25edd692010-08-23 23:10:57 -0700458 unsigned long args[4];
459
460 args[0] = (unsigned long) "SUNW,resume-cpu";
461 args[1] = 1;
462 args[2] = 0;
463 args[3] = (unsigned int) cpunode;
464 p1275_cmd_direct(args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466#endif