x86, es7000: clean up
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / x86 / kernel / es7000_32.c
blobc7cc9776ccae149c8fea00731e631e35dca6e13f
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.
7 * Copyright (c) 2003 Unisys Corporation. All Rights Reserved.
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.
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.
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.
21 * Contact information: Unisys Corporation, Township Line & Union Meeting
22 * Roads-A, Unisys Way, Blue Bell, Pennsylvania, 19424, or:
24 * http://www.unisys.com
27 #include <linux/notifier.h>
28 #include <linux/spinlock.h>
29 #include <linux/cpumask.h>
30 #include <linux/threads.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/reboot.h>
34 #include <linux/string.h>
35 #include <linux/types.h>
36 #include <linux/errno.h>
37 #include <linux/acpi.h>
38 #include <linux/init.h>
39 #include <linux/smp.h>
41 #include <asm/apicdef.h>
42 #include <asm/atomic.h>
43 #include <asm/fixmap.h>
44 #include <asm/mpspec.h>
45 #include <asm/setup.h>
46 #include <asm/apic.h>
47 #include <asm/ipi.h>
48 #include <asm/nmi.h>
49 #include <asm/smp.h>
50 #include <asm/io.h>
53 * ES7000 chipsets
56 #define NON_UNISYS 0
57 #define ES7000_CLASSIC 1
58 #define ES7000_ZORRO 2
60 #define MIP_REG 1
61 #define MIP_PSAI_REG 4
63 #define MIP_BUSY 1
64 #define MIP_SPIN 0xf0000
65 #define MIP_VALID 0x0100000000000000ULL
67 #define MIP_PORT(val) ((val >> 32) & 0xffff)
69 #define MIP_RD_LO(val) (val & 0xffffffff)
71 struct mip_reg_info {
72 unsigned long long mip_info;
73 unsigned long long delivery_info;
74 unsigned long long host_reg;
75 unsigned long long mip_reg;
78 struct part_info {
79 unsigned char type;
80 unsigned char length;
81 unsigned char part_id;
82 unsigned char apic_mode;
83 unsigned long snum;
84 char ptype[16];
85 char sname[64];
86 char pname[64];
89 struct psai {
90 unsigned long long entry_type;
91 unsigned long long addr;
92 unsigned long long bep_addr;
95 struct es7000_mem_info {
96 unsigned char type;
97 unsigned char length;
98 unsigned char resv[6];
99 unsigned long long start;
100 unsigned long long size;
103 struct es7000_oem_table {
104 unsigned long long hdr;
105 struct mip_reg_info mip;
106 struct part_info pif;
107 struct es7000_mem_info shm;
108 struct psai psai;
111 #ifdef CONFIG_ACPI
113 struct oem_table {
114 struct acpi_table_header Header;
115 u32 OEMTableAddr;
116 u32 OEMTableSize;
119 extern int find_unisys_acpi_oem_table(unsigned long *oem_addr);
120 extern void unmap_unisys_acpi_oem_table(unsigned long oem_addr);
121 #endif
123 struct mip_reg {
124 unsigned long long off_0x00;
125 unsigned long long off_0x08;
126 unsigned long long off_0x10;
127 unsigned long long off_0x18;
128 unsigned long long off_0x20;
129 unsigned long long off_0x28;
130 unsigned long long off_0x30;
131 unsigned long long off_0x38;
134 #define MIP_SW_APIC 0x1020b
135 #define MIP_FUNC(VALUE) (VALUE & 0xff)
137 #define APIC_DFR_VALUE_CLUSTER (APIC_DFR_CLUSTER)
138 #define INT_DELIVERY_MODE_CLUSTER (dest_LowestPrio)
139 #define INT_DEST_MODE_CLUSTER (1) /* logical delivery broadcast to all procs */
141 #define APIC_DFR_VALUE (APIC_DFR_FLAT)
143 extern void es7000_enable_apic_mode(void);
144 extern int parse_unisys_oem (char *oemptr);
145 extern int find_unisys_acpi_oem_table(unsigned long *oem_addr);
146 extern void unmap_unisys_acpi_oem_table(unsigned long oem_addr);
147 extern void setup_unisys(void);
149 #define apicid_cluster(apicid) (apicid & 0xF0)
152 * ES7000 Globals
155 static volatile unsigned long *psai = NULL;
156 static struct mip_reg *mip_reg;
157 static struct mip_reg *host_reg;
158 static int mip_port;
159 static unsigned long mip_addr, host_addr;
161 int es7000_plat;
164 * GSI override for ES7000 platforms.
167 static unsigned int base;
169 static int
170 es7000_rename_gsi(int ioapic, int gsi)
172 if (es7000_plat == ES7000_ZORRO)
173 return gsi;
175 if (!base) {
176 int i;
177 for (i = 0; i < nr_ioapics; i++)
178 base += nr_ioapic_registers[i];
181 if (!ioapic && (gsi < 16))
182 gsi += base;
184 return gsi;
187 static int wakeup_secondary_cpu_via_mip(int cpu, unsigned long eip)
189 unsigned long vect = 0, psaival = 0;
191 if (psai == NULL)
192 return -1;
194 vect = ((unsigned long)__pa(eip)/0x1000) << 16;
195 psaival = (0x1000000 | vect | cpu);
197 while (*psai & 0x1000000)
200 *psai = psaival;
202 return 0;
205 static int __init es7000_update_genapic(void)
207 apic->wakeup_cpu = wakeup_secondary_cpu_via_mip;
209 /* MPENTIUMIII */
210 if (boot_cpu_data.x86 == 6 &&
211 (boot_cpu_data.x86_model >= 7 || boot_cpu_data.x86_model <= 11)) {
212 es7000_update_genapic_to_cluster();
213 apic->wait_for_init_deassert = NULL;
214 apic->wakeup_cpu = wakeup_secondary_cpu_via_mip;
217 return 0;
220 void __init setup_unisys(void)
223 * Determine the generation of the ES7000 currently running.
225 * es7000_plat = 1 if the machine is a 5xx ES7000 box
226 * es7000_plat = 2 if the machine is a x86_64 ES7000 box
229 if (!(boot_cpu_data.x86 <= 15 && boot_cpu_data.x86_model <= 2))
230 es7000_plat = ES7000_ZORRO;
231 else
232 es7000_plat = ES7000_CLASSIC;
233 ioapic_renumber_irq = es7000_rename_gsi;
235 x86_quirks->update_genapic = es7000_update_genapic;
239 * Parse the OEM Table
242 int __init parse_unisys_oem (char *oemptr)
244 int i;
245 int success = 0;
246 unsigned char type, size;
247 unsigned long val;
248 char *tp = NULL;
249 struct psai *psaip = NULL;
250 struct mip_reg_info *mi;
251 struct mip_reg *host, *mip;
253 tp = oemptr;
255 tp += 8;
257 for (i=0; i <= 6; i++) {
258 type = *tp++;
259 size = *tp++;
260 tp -= 2;
261 switch (type) {
262 case MIP_REG:
263 mi = (struct mip_reg_info *)tp;
264 val = MIP_RD_LO(mi->host_reg);
265 host_addr = val;
266 host = (struct mip_reg *)val;
267 host_reg = __va(host);
268 val = MIP_RD_LO(mi->mip_reg);
269 mip_port = MIP_PORT(mi->mip_info);
270 mip_addr = val;
271 mip = (struct mip_reg *)val;
272 mip_reg = __va(mip);
273 pr_debug("es7000_mipcfg: host_reg = 0x%lx \n",
274 (unsigned long)host_reg);
275 pr_debug("es7000_mipcfg: mip_reg = 0x%lx \n",
276 (unsigned long)mip_reg);
277 success++;
278 break;
279 case MIP_PSAI_REG:
280 psaip = (struct psai *)tp;
281 if (tp != NULL) {
282 if (psaip->addr)
283 psai = __va(psaip->addr);
284 else
285 psai = NULL;
286 success++;
288 break;
289 default:
290 break;
292 tp += size;
295 if (success < 2) {
296 es7000_plat = NON_UNISYS;
297 } else
298 setup_unisys();
300 return es7000_plat;
303 #ifdef CONFIG_ACPI
305 static unsigned long oem_addrX;
306 static unsigned long oem_size;
308 int __init find_unisys_acpi_oem_table(unsigned long *oem_addr)
310 struct acpi_table_header *header = NULL;
311 int i = 0;
312 acpi_size tbl_size;
314 while (ACPI_SUCCESS(acpi_get_table_with_size("OEM1", i++, &header, &tbl_size))) {
315 if (!memcmp((char *) &header->oem_id, "UNISYS", 6)) {
316 struct oem_table *t = (struct oem_table *)header;
318 oem_addrX = t->OEMTableAddr;
319 oem_size = t->OEMTableSize;
320 early_acpi_os_unmap_memory(header, tbl_size);
322 *oem_addr = (unsigned long)__acpi_map_table(oem_addrX,
323 oem_size);
324 return 0;
326 early_acpi_os_unmap_memory(header, tbl_size);
328 return -1;
331 void __init unmap_unisys_acpi_oem_table(unsigned long oem_addr)
333 if (!oem_addr)
334 return;
336 __acpi_unmap_table((char *)oem_addr, oem_size);
338 #endif
340 static void es7000_spin(int n)
342 int i = 0;
344 while (i++ < n)
345 rep_nop();
348 static int __init
349 es7000_mip_write(struct mip_reg *mip_reg)
351 int status = 0;
352 int spin;
354 spin = MIP_SPIN;
355 while ((host_reg->off_0x38 & MIP_VALID) != 0) {
356 if (--spin <= 0) {
357 printk("es7000_mip_write: Timeout waiting for Host Valid Flag");
358 return -1;
360 es7000_spin(MIP_SPIN);
363 memcpy(host_reg, mip_reg, sizeof(struct mip_reg));
364 outb(1, mip_port);
366 spin = MIP_SPIN;
368 while ((mip_reg->off_0x38 & MIP_VALID) == 0) {
369 if (--spin <= 0) {
370 printk("es7000_mip_write: Timeout waiting for MIP Valid Flag");
371 return -1;
373 es7000_spin(MIP_SPIN);
376 status = (mip_reg->off_0x00 & 0xffff0000000000ULL) >> 48;
377 mip_reg->off_0x38 &= ~MIP_VALID;
379 return status;
382 void __init es7000_enable_apic_mode(void)
384 struct mip_reg es7000_mip_reg;
385 int mip_status;
387 if (!es7000_plat)
388 return;
390 printk("ES7000: Enabling APIC mode.\n");
391 memset(&es7000_mip_reg, 0, sizeof(struct mip_reg));
392 es7000_mip_reg.off_0x00 = MIP_SW_APIC;
393 es7000_mip_reg.off_0x38 = MIP_VALID;
395 while ((mip_status = es7000_mip_write(&es7000_mip_reg)) != 0) {
396 printk("es7000_enable_apic_mode: command failed, status = %x\n",
397 mip_status);
401 static void es7000_vector_allocation_domain(int cpu, cpumask_t *retmask)
403 /* Careful. Some cpus do not strictly honor the set of cpus
404 * specified in the interrupt destination when using lowest
405 * priority interrupt delivery mode.
407 * In particular there was a hyperthreading cpu observed to
408 * deliver interrupts to the wrong hyperthread when only one
409 * hyperthread was specified in the interrupt desitination.
411 *retmask = (cpumask_t){ { [0] = APIC_ALL_CPUS, } };
415 static void es7000_wait_for_init_deassert(atomic_t *deassert)
417 #ifndef CONFIG_ES7000_CLUSTERED_APIC
418 while (!atomic_read(deassert))
419 cpu_relax();
420 #endif
421 return;
424 static unsigned int es7000_get_apic_id(unsigned long x)
426 return (x >> 24) & 0xFF;
429 #ifdef CONFIG_ACPI
430 static int es7000_check_dsdt(void)
432 struct acpi_table_header header;
434 if (ACPI_SUCCESS(acpi_get_table_header(ACPI_SIG_DSDT, 0, &header)) &&
435 !strncmp(header.oem_id, "UNISYS", 6))
436 return 1;
437 return 0;
439 #endif
441 static void es7000_send_IPI_mask(const struct cpumask *mask, int vector)
443 default_send_IPI_mask_sequence_phys(mask, vector);
446 static void es7000_send_IPI_allbutself(int vector)
448 default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
451 static void es7000_send_IPI_all(int vector)
453 es7000_send_IPI_mask(cpu_online_mask, vector);
456 static int es7000_apic_id_registered(void)
458 return 1;
461 static const cpumask_t *target_cpus_cluster(void)
463 return &CPU_MASK_ALL;
466 static const cpumask_t *es7000_target_cpus(void)
468 return &cpumask_of_cpu(smp_processor_id());
471 static unsigned long
472 es7000_check_apicid_used(physid_mask_t bitmap, int apicid)
474 return 0;
476 static unsigned long es7000_check_apicid_present(int bit)
478 return physid_isset(bit, phys_cpu_present_map);
481 static unsigned long calculate_ldr(int cpu)
483 unsigned long id = per_cpu(x86_bios_cpu_apicid, cpu);
485 return SET_APIC_LOGICAL_ID(id);
489 * Set up the logical destination ID.
491 * Intel recommends to set DFR, LdR and TPR before enabling
492 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
493 * document number 292116). So here it goes...
495 static void es7000_init_apic_ldr_cluster(void)
497 unsigned long val;
498 int cpu = smp_processor_id();
500 apic_write(APIC_DFR, APIC_DFR_VALUE_CLUSTER);
501 val = calculate_ldr(cpu);
502 apic_write(APIC_LDR, val);
505 static void es7000_init_apic_ldr(void)
507 unsigned long val;
508 int cpu = smp_processor_id();
510 apic_write(APIC_DFR, APIC_DFR_VALUE);
511 val = calculate_ldr(cpu);
512 apic_write(APIC_LDR, val);
515 static void es7000_setup_apic_routing(void)
517 int apic = per_cpu(x86_bios_cpu_apicid, smp_processor_id());
518 printk("Enabling APIC mode: %s. Using %d I/O APICs, target cpus %lx\n",
519 (apic_version[apic] == 0x14) ?
520 "Physical Cluster" : "Logical Cluster",
521 nr_ioapics, cpus_addr(*es7000_target_cpus())[0]);
524 static int es7000_apicid_to_node(int logical_apicid)
526 return 0;
530 static int es7000_cpu_present_to_apicid(int mps_cpu)
532 if (!mps_cpu)
533 return boot_cpu_physical_apicid;
534 else if (mps_cpu < nr_cpu_ids)
535 return per_cpu(x86_bios_cpu_apicid, mps_cpu);
536 else
537 return BAD_APICID;
540 static physid_mask_t es7000_apicid_to_cpu_present(int phys_apicid)
542 static int id = 0;
543 physid_mask_t mask;
545 mask = physid_mask_of_physid(id);
546 ++id;
548 return mask;
551 /* Mapping from cpu number to logical apicid */
552 static int es7000_cpu_to_logical_apicid(int cpu)
554 #ifdef CONFIG_SMP
555 if (cpu >= nr_cpu_ids)
556 return BAD_APICID;
557 return cpu_2_logical_apicid[cpu];
558 #else
559 return logical_smp_processor_id();
560 #endif
563 static physid_mask_t es7000_ioapic_phys_id_map(physid_mask_t phys_map)
565 /* For clustered we don't have a good way to do this yet - hack */
566 return physids_promote(0xff);
569 static int es7000_check_phys_apicid_present(int cpu_physical_apicid)
571 boot_cpu_physical_apicid = read_apic_id();
572 return 1;
575 static unsigned int
576 es7000_cpu_mask_to_apicid_cluster(const struct cpumask *cpumask)
578 int cpus_found = 0;
579 int num_bits_set;
580 int apicid;
581 int cpu;
583 num_bits_set = cpumask_weight(cpumask);
584 /* Return id to all */
585 if (num_bits_set == nr_cpu_ids)
586 return 0xFF;
588 * The cpus in the mask must all be on the apic cluster. If are not
589 * on the same apicid cluster return default value of target_cpus():
591 cpu = cpumask_first(cpumask);
592 apicid = es7000_cpu_to_logical_apicid(cpu);
594 while (cpus_found < num_bits_set) {
595 if (cpumask_test_cpu(cpu, cpumask)) {
596 int new_apicid = es7000_cpu_to_logical_apicid(cpu);
598 if (apicid_cluster(apicid) !=
599 apicid_cluster(new_apicid)) {
600 printk ("%s: Not a valid mask!\n", __func__);
602 return 0xFF;
604 apicid = new_apicid;
605 cpus_found++;
607 cpu++;
609 return apicid;
612 static unsigned int es7000_cpu_mask_to_apicid(const cpumask_t *cpumask)
614 int cpus_found = 0;
615 int num_bits_set;
616 int apicid;
617 int cpu;
619 num_bits_set = cpus_weight(*cpumask);
620 /* Return id to all */
621 if (num_bits_set == nr_cpu_ids)
622 return es7000_cpu_to_logical_apicid(0);
624 * The cpus in the mask must all be on the apic cluster. If are not
625 * on the same apicid cluster return default value of target_cpus():
627 cpu = first_cpu(*cpumask);
628 apicid = es7000_cpu_to_logical_apicid(cpu);
629 while (cpus_found < num_bits_set) {
630 if (cpu_isset(cpu, *cpumask)) {
631 int new_apicid = es7000_cpu_to_logical_apicid(cpu);
633 if (apicid_cluster(apicid) !=
634 apicid_cluster(new_apicid)) {
635 printk ("%s: Not a valid mask!\n", __func__);
637 return es7000_cpu_to_logical_apicid(0);
639 apicid = new_apicid;
640 cpus_found++;
642 cpu++;
644 return apicid;
647 static unsigned int
648 es7000_cpu_mask_to_apicid_and(const struct cpumask *inmask,
649 const struct cpumask *andmask)
651 int apicid = es7000_cpu_to_logical_apicid(0);
652 cpumask_var_t cpumask;
654 if (!alloc_cpumask_var(&cpumask, GFP_ATOMIC))
655 return apicid;
657 cpumask_and(cpumask, inmask, andmask);
658 cpumask_and(cpumask, cpumask, cpu_online_mask);
659 apicid = es7000_cpu_mask_to_apicid(cpumask);
661 free_cpumask_var(cpumask);
663 return apicid;
666 static int es7000_phys_pkg_id(int cpuid_apic, int index_msb)
668 return cpuid_apic >> index_msb;
671 void __init es7000_update_genapic_to_cluster(void)
673 apic->target_cpus = target_cpus_cluster;
674 apic->irq_delivery_mode = INT_DELIVERY_MODE_CLUSTER;
675 apic->irq_dest_mode = INT_DEST_MODE_CLUSTER;
677 apic->init_apic_ldr = es7000_init_apic_ldr_cluster;
679 apic->cpu_mask_to_apicid = es7000_cpu_mask_to_apicid_cluster;
682 static int probe_es7000(void)
684 /* probed later in mptable/ACPI hooks */
685 return 0;
688 static __init int
689 es7000_mps_oem_check(struct mpc_table *mpc, char *oem, char *productid)
691 if (mpc->oemptr) {
692 struct mpc_oemtable *oem_table =
693 (struct mpc_oemtable *)mpc->oemptr;
695 if (!strncmp(oem, "UNISYS", 6))
696 return parse_unisys_oem((char *)oem_table);
698 return 0;
701 #ifdef CONFIG_ACPI
702 /* Hook from generic ACPI tables.c */
703 static int __init es7000_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
705 unsigned long oem_addr = 0;
706 int check_dsdt;
707 int ret = 0;
709 /* check dsdt at first to avoid clear fix_map for oem_addr */
710 check_dsdt = es7000_check_dsdt();
712 if (!find_unisys_acpi_oem_table(&oem_addr)) {
713 if (check_dsdt)
714 ret = parse_unisys_oem((char *)oem_addr);
715 else {
716 setup_unisys();
717 ret = 1;
720 * we need to unmap it
722 unmap_unisys_acpi_oem_table(oem_addr);
724 return ret;
726 #else
727 static int __init es7000_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
729 return 0;
731 #endif
734 struct genapic apic_es7000 = {
736 .name = "es7000",
737 .probe = probe_es7000,
738 .acpi_madt_oem_check = es7000_acpi_madt_oem_check,
739 .apic_id_registered = es7000_apic_id_registered,
741 .irq_delivery_mode = dest_Fixed,
742 /* phys delivery to target CPUs: */
743 .irq_dest_mode = 0,
745 .target_cpus = es7000_target_cpus,
746 .disable_esr = 1,
747 .dest_logical = 0,
748 .check_apicid_used = es7000_check_apicid_used,
749 .check_apicid_present = es7000_check_apicid_present,
751 .vector_allocation_domain = es7000_vector_allocation_domain,
752 .init_apic_ldr = es7000_init_apic_ldr,
754 .ioapic_phys_id_map = es7000_ioapic_phys_id_map,
755 .setup_apic_routing = es7000_setup_apic_routing,
756 .multi_timer_check = NULL,
757 .apicid_to_node = es7000_apicid_to_node,
758 .cpu_to_logical_apicid = es7000_cpu_to_logical_apicid,
759 .cpu_present_to_apicid = es7000_cpu_present_to_apicid,
760 .apicid_to_cpu_present = es7000_apicid_to_cpu_present,
761 .setup_portio_remap = NULL,
762 .check_phys_apicid_present = es7000_check_phys_apicid_present,
763 .enable_apic_mode = es7000_enable_apic_mode,
764 .phys_pkg_id = es7000_phys_pkg_id,
765 .mps_oem_check = es7000_mps_oem_check,
767 .get_apic_id = es7000_get_apic_id,
768 .set_apic_id = NULL,
769 .apic_id_mask = 0xFF << 24,
771 .cpu_mask_to_apicid = es7000_cpu_mask_to_apicid,
772 .cpu_mask_to_apicid_and = es7000_cpu_mask_to_apicid_and,
774 .send_IPI_mask = es7000_send_IPI_mask,
775 .send_IPI_mask_allbutself = NULL,
776 .send_IPI_allbutself = es7000_send_IPI_allbutself,
777 .send_IPI_all = es7000_send_IPI_all,
778 .send_IPI_self = default_send_IPI_self,
780 .wakeup_cpu = NULL,
782 .trampoline_phys_low = 0x467,
783 .trampoline_phys_high = 0x469,
785 .wait_for_init_deassert = es7000_wait_for_init_deassert,
787 /* Nothing to do for most platforms, since cleared by the INIT cycle: */
788 .smp_callin_clear_local_apic = NULL,
789 .store_NMI_vector = NULL,
790 .inquire_remote_apic = default_inquire_remote_apic,
792 .read = native_apic_mem_read,
793 .write = native_apic_mem_write,
794 .icr_read = native_apic_icr_read,
795 .icr_write = native_apic_icr_write,
796 .wait_icr_idle = native_apic_wait_icr_idle,
797 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,