Merge with /home/shaggy/git/linus-clean/
[linux-2.6] / arch / sparc64 / prom / p1275.c
1 /* $Id: p1275.c,v 1.22 2001/10/18 09:40:00 davem Exp $
2  * p1275.c: Sun IEEE 1275 PROM low level interface routines
3  *
4  * Copyright (C) 1996,1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
5  */
6
7 #include <linux/kernel.h>
8 #include <linux/init.h>
9 #include <linux/sched.h>
10 #include <linux/smp.h>
11 #include <linux/string.h>
12 #include <linux/spinlock.h>
13
14 #include <asm/openprom.h>
15 #include <asm/oplib.h>
16 #include <asm/system.h>
17 #include <asm/spitfire.h>
18 #include <asm/pstate.h>
19
20 struct {
21         long prom_callback;                     /* 0x00 */
22         void (*prom_cif_handler)(long *);       /* 0x08 */
23         unsigned long prom_cif_stack;           /* 0x10 */
24         unsigned long prom_args [23];           /* 0x18 */
25         char prom_buffer [3000];
26 } p1275buf;
27
28 extern void prom_world(int);
29
30 extern void prom_cif_interface(void);
31 extern void prom_cif_callback(void);
32
33 static inline unsigned long spitfire_get_primary_context(void)
34 {
35         unsigned long ctx;
36
37         __asm__ __volatile__("ldxa      [%1] %2, %0"
38                              : "=r" (ctx)
39                              : "r" (PRIMARY_CONTEXT), "i" (ASI_DMMU));
40         return ctx;
41 }
42
43 /*
44  * This provides SMP safety on the p1275buf. prom_callback() drops this lock
45  * to allow recursuve acquisition.
46  */
47 DEFINE_SPINLOCK(prom_entry_lock);
48
49 long p1275_cmd (char *service, long fmt, ...)
50 {
51         char *p, *q;
52         unsigned long flags;
53         int nargs, nrets, i;
54         va_list list;
55         long attrs, x;
56         
57         p = p1275buf.prom_buffer;
58         BUG_ON((spitfire_get_primary_context() & CTX_NR_MASK) != 0);
59
60         spin_lock_irqsave(&prom_entry_lock, flags);
61
62         p1275buf.prom_args[0] = (unsigned long)p;               /* service */
63         strcpy (p, service);
64         p = (char *)(((long)(strchr (p, 0) + 8)) & ~7);
65         p1275buf.prom_args[1] = nargs = (fmt & 0x0f);           /* nargs */
66         p1275buf.prom_args[2] = nrets = ((fmt & 0xf0) >> 4);    /* nrets */
67         attrs = fmt >> 8;
68         va_start(list, fmt);
69         for (i = 0; i < nargs; i++, attrs >>= 3) {
70                 switch (attrs & 0x7) {
71                 case P1275_ARG_NUMBER:
72                         p1275buf.prom_args[i + 3] =
73                                                 (unsigned)va_arg(list, long);
74                         break;
75                 case P1275_ARG_IN_64B:
76                         p1275buf.prom_args[i + 3] =
77                                 va_arg(list, unsigned long);
78                         break;
79                 case P1275_ARG_IN_STRING:
80                         strcpy (p, va_arg(list, char *));
81                         p1275buf.prom_args[i + 3] = (unsigned long)p;
82                         p = (char *)(((long)(strchr (p, 0) + 8)) & ~7);
83                         break;
84                 case P1275_ARG_OUT_BUF:
85                         (void) va_arg(list, char *);
86                         p1275buf.prom_args[i + 3] = (unsigned long)p;
87                         x = va_arg(list, long);
88                         i++; attrs >>= 3;
89                         p = (char *)(((long)(p + (int)x + 7)) & ~7);
90                         p1275buf.prom_args[i + 3] = x;
91                         break;
92                 case P1275_ARG_IN_BUF:
93                         q = va_arg(list, char *);
94                         p1275buf.prom_args[i + 3] = (unsigned long)p;
95                         x = va_arg(list, long);
96                         i++; attrs >>= 3;
97                         memcpy (p, q, (int)x);
98                         p = (char *)(((long)(p + (int)x + 7)) & ~7);
99                         p1275buf.prom_args[i + 3] = x;
100                         break;
101                 case P1275_ARG_OUT_32B:
102                         (void) va_arg(list, char *);
103                         p1275buf.prom_args[i + 3] = (unsigned long)p;
104                         p += 32;
105                         break;
106                 case P1275_ARG_IN_FUNCTION:
107                         p1275buf.prom_args[i + 3] =
108                                         (unsigned long)prom_cif_callback;
109                         p1275buf.prom_callback = va_arg(list, long);
110                         break;
111                 }
112         }
113         va_end(list);
114
115         prom_world(1);
116         prom_cif_interface();
117         prom_world(0);
118
119         attrs = fmt >> 8;
120         va_start(list, fmt);
121         for (i = 0; i < nargs; i++, attrs >>= 3) {
122                 switch (attrs & 0x7) {
123                 case P1275_ARG_NUMBER:
124                         (void) va_arg(list, long);
125                         break;
126                 case P1275_ARG_IN_STRING:
127                         (void) va_arg(list, char *);
128                         break;
129                 case P1275_ARG_IN_FUNCTION:
130                         (void) va_arg(list, long);
131                         break;
132                 case P1275_ARG_IN_BUF:
133                         (void) va_arg(list, char *);
134                         (void) va_arg(list, long);
135                         i++; attrs >>= 3;
136                         break;
137                 case P1275_ARG_OUT_BUF:
138                         p = va_arg(list, char *);
139                         x = va_arg(list, long);
140                         memcpy (p, (char *)(p1275buf.prom_args[i + 3]), (int)x);
141                         i++; attrs >>= 3;
142                         break;
143                 case P1275_ARG_OUT_32B:
144                         p = va_arg(list, char *);
145                         memcpy (p, (char *)(p1275buf.prom_args[i + 3]), 32);
146                         break;
147                 }
148         }
149         va_end(list);
150         x = p1275buf.prom_args [nargs + 3];
151
152         spin_unlock_irqrestore(&prom_entry_lock, flags);
153
154         return x;
155 }
156
157 void prom_cif_init(void *cif_handler, void *cif_stack)
158 {
159         p1275buf.prom_cif_handler = (void (*)(long *))cif_handler;
160         p1275buf.prom_cif_stack = (unsigned long)cif_stack;
161 }