vserver 1.9.3
[linux-2.6.git] / arch / i386 / mach-es7000 / es7000plat.c
1 /*
2  * Written by: Garry Forsgren, Unisys Corporation
3  *             Natalie Protasevich, Unisys Corporation
4  * This file contains the code to configure and interface
5  * with Unisys ES7000 series hardware system manager.
6  *
7  * Copyright (c) 2003 Unisys Corporation.  All Rights Reserved.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of version 2 of the GNU General Public License as
11  * published by the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it would be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write the Free Software Foundation, Inc., 59
19  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
20  *
21  * Contact information: Unisys Corporation, Township Line & Union Meeting
22  * Roads-A, Unisys Way, Blue Bell, Pennsylvania, 19424, or:
23  *
24  * http://www.unisys.com
25  */
26
27 #include <linux/module.h>
28 #include <linux/types.h>
29 #include <linux/kernel.h>
30 #include <linux/smp.h>
31 #include <linux/string.h>
32 #include <linux/spinlock.h>
33 #include <linux/errno.h>
34 #include <linux/notifier.h>
35 #include <linux/reboot.h>
36 #include <linux/init.h>
37 #include <linux/acpi.h>
38 #include <asm/io.h>
39 #include <asm/nmi.h>
40 #include <asm/smp.h>
41 #include <asm/apicdef.h>
42 #include "es7000.h"
43
44 /*
45  * ES7000 Globals
46  */
47
48 volatile unsigned long  *psai = NULL;
49 struct mip_reg          *mip_reg;
50 struct mip_reg          *host_reg;
51 int                     mip_port;
52 unsigned long           mip_addr, host_addr;
53
54 #if defined(CONFIG_X86_IO_APIC) && (defined(CONFIG_ACPI_INTERPRETER) || defined(CONFIG_ACPI_BOOT))
55 static unsigned long cycle_irqs = 0;
56 static unsigned long free_irqs = 0;
57 static int gsi_map[MAX_GSI_MAPSIZE] = { [0 ... MAX_GSI_MAPSIZE-1] = -1 };
58
59 /*
60  * GSI override for ES7000 platforms.
61  */
62
63 static int __init
64 es7000_gsi_override(int ioapic, int gsi)
65 {
66         static int newgsi = 0;
67
68         if (gsi_map[gsi] != -1)
69                 gsi = gsi_map[gsi];
70         else if (cycle_irqs ^ free_irqs) {
71                 newgsi = find_next_bit(&cycle_irqs, IOAPIC_GSI_BOUND(0), newgsi);
72                 __set_bit(newgsi, &free_irqs);
73                 gsi_map[gsi] = newgsi;
74                 gsi = newgsi;
75                 newgsi++;
76                 Dprintk("es7000_gsi_override: free_irqs = 0x%lx\n", free_irqs);
77         }
78
79         return gsi;
80 }
81
82 static int __init
83 es7000_rename_gsi(int ioapic, int gsi)
84 {
85         static int initialized = 0;
86         int i;
87
88         /*
89          * These should NEVER be true at this point but we'd rather be
90          * safe than sorry.
91          */
92         if (acpi_disabled || acpi_pci_disabled || acpi_noirq)
93                 return gsi;
94
95         if (ioapic)
96                 return gsi;
97
98         if (!initialized) {
99                 unsigned long tmp_irqs = 0;
100
101                 for (i = 0; i < nr_ioapic_registers[0]; i++)
102                         __set_bit(mp_irqs[i].mpc_srcbusirq, &tmp_irqs);
103
104                 cycle_irqs = (~tmp_irqs & io_apic_irqs & ((1 << IOAPIC_GSI_BOUND(0)) - 1));
105
106                 initialized = 1;
107                 Dprintk("es7000_rename_gsi: cycle_irqs = 0x%lx\n", cycle_irqs);
108         }
109
110         for (i = 0; i < nr_ioapic_registers[0]; i++) {
111                 if (mp_irqs[i].mpc_srcbusirq == gsi) {
112                         if (mp_irqs[i].mpc_dstirq == gsi)
113                                 return gsi;
114                         else
115                                 return es7000_gsi_override(0, gsi);
116                 }
117         }
118
119         return gsi;
120 }
121 #endif // (CONFIG_X86_IO_APIC) && (CONFIG_ACPI_INTERPRETER || CONFIG_ACPI_BOOT)
122
123 /*
124  * Parse the OEM Table
125  */
126
127 int __init
128 parse_unisys_oem (char *oemptr, int oem_entries)
129 {
130         int                     i;
131         int                     success = 0;
132         unsigned char           type, size;
133         unsigned long           val;
134         char                    *tp = NULL;
135         struct psai             *psaip = NULL;
136         struct mip_reg_info     *mi;
137         struct mip_reg          *host, *mip;
138
139         tp = oemptr;
140
141         tp += 8;
142
143         for (i=0; i <= oem_entries; i++) {
144                 type = *tp++;
145                 size = *tp++;
146                 tp -= 2;
147                 switch (type) {
148                 case MIP_REG:
149                         mi = (struct mip_reg_info *)tp;
150                         val = MIP_RD_LO(mi->host_reg);
151                         host_addr = val;
152                         host = (struct mip_reg *)val;
153                         host_reg = __va(host);
154                         val = MIP_RD_LO(mi->mip_reg);
155                         mip_port = MIP_PORT(mi->mip_info);
156                         mip_addr = val;
157                         mip = (struct mip_reg *)val;
158                         mip_reg = __va(mip);
159                         Dprintk("es7000_mipcfg: host_reg = 0x%lx \n",
160                                 (unsigned long)host_reg);
161                         Dprintk("es7000_mipcfg: mip_reg = 0x%lx \n",
162                                 (unsigned long)mip_reg);
163                         success++;
164                         break;
165                 case MIP_PSAI_REG:
166                         psaip = (struct psai *)tp;
167                         if (tp != NULL) {
168                                 if (psaip->addr)
169                                         psai = __va(psaip->addr);
170                                 else
171                                         psai = NULL;
172                                 success++;
173                         }
174                         break;
175                 default:
176                         break;
177                 }
178                 if (i == 6) break;
179                 tp += size;
180         }
181
182         if (success < 2) {
183                 es7000_plat = 0;
184         } else {
185                 printk("\nEnabling ES7000 specific features...\n");
186                 es7000_plat = 1;
187                 platform_rename_gsi = es7000_rename_gsi;
188         }
189         return es7000_plat;
190 }
191
192 int __init
193 find_unisys_acpi_oem_table(unsigned long *oem_addr, int *length)
194 {
195         struct acpi_table_rsdp          *rsdp = NULL;
196         unsigned long                   rsdp_phys = 0;
197         struct acpi_table_header        *header = NULL;
198         int                             i;
199         struct acpi_table_sdt           sdt;
200
201         rsdp_phys = acpi_find_rsdp();
202         rsdp = __va(rsdp_phys);
203         if (rsdp->rsdt_address) {
204                 struct acpi_table_rsdt  *mapped_rsdt = NULL;
205                 sdt.pa = rsdp->rsdt_address;
206
207                 header = (struct acpi_table_header *)
208                         __acpi_map_table(sdt.pa, sizeof(struct acpi_table_header));
209                 if (!header)
210                         return -ENODEV;
211
212                 sdt.count = (header->length - sizeof(struct acpi_table_header)) >> 3;
213                 mapped_rsdt = (struct acpi_table_rsdt *)
214                         __acpi_map_table(sdt.pa, header->length);
215                 if (!mapped_rsdt)
216                         return -ENODEV;
217
218                 header = &mapped_rsdt->header;
219
220                 for (i = 0; i < sdt.count; i++)
221                         sdt.entry[i].pa = (unsigned long) mapped_rsdt->entry[i];
222         };
223         for (i = 0; i < sdt.count; i++) {
224
225                 header = (struct acpi_table_header *)
226                         __acpi_map_table(sdt.entry[i].pa,
227                                 sizeof(struct acpi_table_header));
228                 if (!header)
229                         continue;
230                 if (!strncmp((char *) &header->signature, "OEM1", 4)) {
231                         if (!strncmp((char *) &header->oem_id, "UNISYS", 6)) {
232                                 void *addr;
233                                 struct oem_table *t;
234                                 acpi_table_print(header, sdt.entry[i].pa);
235                                 t = (struct oem_table *) __acpi_map_table(sdt.entry[i].pa, header->length);
236                                 addr = (void *) __acpi_map_table(t->OEMTableAddr, t->OEMTableSize);
237                                 *length = header->length;
238                                 *oem_addr = (unsigned long) addr;
239                                 return 0;
240                         }
241                 }
242         }
243         Dprintk("ES7000: did not find Unisys ACPI OEM table!\n");
244         return -1;
245 }
246
247 static void
248 es7000_spin(int n)
249 {
250         int i = 0;
251
252         while (i++ < n)
253                 rep_nop();
254 }
255
256 static int __init
257 es7000_mip_write(struct mip_reg *mip_reg)
258 {
259         int                     status = 0;
260         int                     spin;
261
262         spin = MIP_SPIN;
263         while (((unsigned long long)host_reg->off_38 &
264                 (unsigned long long)MIP_VALID) != 0) {
265                         if (--spin <= 0) {
266                                 printk("es7000_mip_write: Timeout waiting for Host Valid Flag");
267                                 return -1;
268                         }
269                 es7000_spin(MIP_SPIN);
270         }
271
272         memcpy(host_reg, mip_reg, sizeof(struct mip_reg));
273         outb(1, mip_port);
274
275         spin = MIP_SPIN;
276
277         while (((unsigned long long)mip_reg->off_38 &
278                 (unsigned long long)MIP_VALID) == 0) {
279                 if (--spin <= 0) {
280                         printk("es7000_mip_write: Timeout waiting for MIP Valid Flag");
281                         return -1;
282                 }
283                 es7000_spin(MIP_SPIN);
284         }
285
286         status = ((unsigned long long)mip_reg->off_0 &
287                 (unsigned long long)0xffff0000000000ULL) >> 48;
288         mip_reg->off_38 = ((unsigned long long)mip_reg->off_38 &
289                 (unsigned long long)~MIP_VALID);
290         return status;
291 }
292
293 int
294 es7000_start_cpu(int cpu, unsigned long eip)
295 {
296         unsigned long vect = 0, psaival = 0;
297
298         if (psai == NULL)
299                 return -1;
300
301         vect = ((unsigned long)__pa(eip)/0x1000) << 16;
302         psaival = (0x1000000 | vect | cpu);
303
304         while (*psai & 0x1000000)
305                 ;
306
307         *psai = psaival;
308
309         return 0;
310
311 }
312
313 int
314 es7000_stop_cpu(int cpu)
315 {
316         int startup;
317
318         if (psai == NULL)
319                 return -1;
320
321         startup= (0x1000000 | cpu);
322
323         while ((*psai & 0xff00ffff) != startup)
324                 ;
325
326         startup = (*psai & 0xff0000) >> 16;
327         *psai &= 0xffffff;
328
329         return 0;
330
331 }
332
333 void __init
334 es7000_sw_apic()
335 {
336         if (es7000_plat) {
337                 int mip_status;
338                 struct mip_reg es7000_mip_reg;
339
340                 printk("ES7000: Enabling APIC mode.\n");
341                 memset(&es7000_mip_reg, 0, sizeof(struct mip_reg));
342                 es7000_mip_reg.off_0 = MIP_SW_APIC;
343                 es7000_mip_reg.off_38 = (MIP_VALID);
344                 while ((mip_status = es7000_mip_write(&es7000_mip_reg)) != 0)
345                         printk("es7000_sw_apic: command failed, status = %x\n",
346                                 mip_status);
347                 return;
348         }
349 }