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
26 #include <linux/notifier.h>
27 #include <linux/spinlock.h>
28 #include <linux/cpumask.h>
29 #include <linux/threads.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/reboot.h>
33 #include <linux/string.h>
34 #include <linux/types.h>
35 #include <linux/errno.h>
36 #include <linux/acpi.h>
37 #include <linux/init.h>
38 #include <linux/smp.h>
40 #include <asm/apicdef.h>
41 #include <asm/atomic.h>
42 #include <asm/fixmap.h>
43 #include <asm/mpspec.h>
44 #include <asm/setup.h>
56 #define ES7000_CLASSIC 1
57 #define ES7000_ZORRO 2
60 #define MIP_PSAI_REG 4
63 #define MIP_SPIN 0xf0000
64 #define MIP_VALID 0x0100000000000000ULL
65 #define MIP_SW_APIC 0x1020b
67 #define MIP_PORT(val) ((val >> 32) & 0xffff)
69 #define MIP_RD_LO(val) (val & 0xffffffff)
72 unsigned long long off_0x00;
73 unsigned long long off_0x08;
74 unsigned long long off_0x10;
75 unsigned long long off_0x18;
76 unsigned long long off_0x20;
77 unsigned long long off_0x28;
78 unsigned long long off_0x30;
79 unsigned long long off_0x38;
83 unsigned long long mip_info;
84 unsigned long long delivery_info;
85 unsigned long long host_reg;
86 unsigned long long mip_reg;
90 unsigned long long entry_type;
91 unsigned long long addr;
92 unsigned long long bep_addr;
96 struct es7000_oem_table {
97 struct acpi_table_header Header;
107 static volatile unsigned long *psai = NULL;
108 static struct mip_reg *mip_reg;
109 static struct mip_reg *host_reg;
111 static unsigned long mip_addr, host_addr;
116 * GSI override for ES7000 platforms.
119 static unsigned int base;
122 es7000_rename_gsi(int ioapic, int gsi)
124 if (es7000_plat == ES7000_ZORRO)
129 for (i = 0; i < nr_ioapics; i++)
130 base += nr_ioapic_registers[i];
133 if (!ioapic && (gsi < 16))
139 static int wakeup_secondary_cpu_via_mip(int cpu, unsigned long eip)
141 unsigned long vect = 0, psaival = 0;
146 vect = ((unsigned long)__pa(eip)/0x1000) << 16;
147 psaival = (0x1000000 | vect | cpu);
149 while (*psai & 0x1000000)
157 static int __init es7000_update_genapic(void)
159 apic->wakeup_cpu = wakeup_secondary_cpu_via_mip;
162 if (boot_cpu_data.x86 == 6 &&
163 (boot_cpu_data.x86_model >= 7 || boot_cpu_data.x86_model <= 11)) {
164 es7000_update_genapic_to_cluster();
165 apic->wait_for_init_deassert = NULL;
166 apic->wakeup_cpu = wakeup_secondary_cpu_via_mip;
172 static void __init setup_unisys(void)
175 * Determine the generation of the ES7000 currently running.
177 * es7000_plat = 1 if the machine is a 5xx ES7000 box
178 * es7000_plat = 2 if the machine is a x86_64 ES7000 box
181 if (!(boot_cpu_data.x86 <= 15 && boot_cpu_data.x86_model <= 2))
182 es7000_plat = ES7000_ZORRO;
184 es7000_plat = ES7000_CLASSIC;
185 ioapic_renumber_irq = es7000_rename_gsi;
187 x86_quirks->update_genapic = es7000_update_genapic;
191 * Parse the OEM Table:
193 static int __init parse_unisys_oem(char *oemptr)
197 unsigned char type, size;
200 struct psai *psaip = NULL;
201 struct mip_reg_info *mi;
202 struct mip_reg *host, *mip;
208 for (i = 0; i <= 6; i++) {
214 mi = (struct mip_reg_info *)tp;
215 val = MIP_RD_LO(mi->host_reg);
217 host = (struct mip_reg *)val;
218 host_reg = __va(host);
219 val = MIP_RD_LO(mi->mip_reg);
220 mip_port = MIP_PORT(mi->mip_info);
222 mip = (struct mip_reg *)val;
224 pr_debug("es7000_mipcfg: host_reg = 0x%lx \n",
225 (unsigned long)host_reg);
226 pr_debug("es7000_mipcfg: mip_reg = 0x%lx \n",
227 (unsigned long)mip_reg);
231 psaip = (struct psai *)tp;
234 psai = __va(psaip->addr);
247 es7000_plat = NON_UNISYS;
256 static unsigned long oem_addrX;
257 static unsigned long oem_size;
259 static int __init find_unisys_acpi_oem_table(unsigned long *oem_addr)
261 struct acpi_table_header *header = NULL;
265 while (ACPI_SUCCESS(acpi_get_table_with_size("OEM1", i++, &header, &tbl_size))) {
266 if (!memcmp((char *) &header->oem_id, "UNISYS", 6)) {
267 struct es7000_oem_table *t = (void *)header;
269 oem_addrX = t->OEMTableAddr;
270 oem_size = t->OEMTableSize;
271 early_acpi_os_unmap_memory(header, tbl_size);
273 *oem_addr = (unsigned long)__acpi_map_table(oem_addrX,
277 early_acpi_os_unmap_memory(header, tbl_size);
282 static void __init unmap_unisys_acpi_oem_table(unsigned long oem_addr)
287 __acpi_unmap_table((char *)oem_addr, oem_size);
291 static void es7000_spin(int n)
300 es7000_mip_write(struct mip_reg *mip_reg)
306 while ((host_reg->off_0x38 & MIP_VALID) != 0) {
308 printk("es7000_mip_write: Timeout waiting for Host Valid Flag");
311 es7000_spin(MIP_SPIN);
314 memcpy(host_reg, mip_reg, sizeof(struct mip_reg));
319 while ((mip_reg->off_0x38 & MIP_VALID) == 0) {
321 printk("es7000_mip_write: Timeout waiting for MIP Valid Flag");
324 es7000_spin(MIP_SPIN);
327 status = (mip_reg->off_0x00 & 0xffff0000000000ULL) >> 48;
328 mip_reg->off_0x38 &= ~MIP_VALID;
333 static void __init es7000_enable_apic_mode(void)
335 struct mip_reg es7000_mip_reg;
341 printk("ES7000: Enabling APIC mode.\n");
342 memset(&es7000_mip_reg, 0, sizeof(struct mip_reg));
343 es7000_mip_reg.off_0x00 = MIP_SW_APIC;
344 es7000_mip_reg.off_0x38 = MIP_VALID;
346 while ((mip_status = es7000_mip_write(&es7000_mip_reg)) != 0) {
347 printk("es7000_enable_apic_mode: command failed, status = %x\n",
352 static void es7000_vector_allocation_domain(int cpu, cpumask_t *retmask)
354 /* Careful. Some cpus do not strictly honor the set of cpus
355 * specified in the interrupt destination when using lowest
356 * priority interrupt delivery mode.
358 * In particular there was a hyperthreading cpu observed to
359 * deliver interrupts to the wrong hyperthread when only one
360 * hyperthread was specified in the interrupt desitination.
362 *retmask = (cpumask_t){ { [0] = APIC_ALL_CPUS, } };
366 static void es7000_wait_for_init_deassert(atomic_t *deassert)
368 #ifndef CONFIG_ES7000_CLUSTERED_APIC
369 while (!atomic_read(deassert))
375 static unsigned int es7000_get_apic_id(unsigned long x)
377 return (x >> 24) & 0xFF;
381 static int es7000_check_dsdt(void)
383 struct acpi_table_header header;
385 if (ACPI_SUCCESS(acpi_get_table_header(ACPI_SIG_DSDT, 0, &header)) &&
386 !strncmp(header.oem_id, "UNISYS", 6))
392 static void es7000_send_IPI_mask(const struct cpumask *mask, int vector)
394 default_send_IPI_mask_sequence_phys(mask, vector);
397 static void es7000_send_IPI_allbutself(int vector)
399 default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
402 static void es7000_send_IPI_all(int vector)
404 es7000_send_IPI_mask(cpu_online_mask, vector);
407 static int es7000_apic_id_registered(void)
412 static const cpumask_t *target_cpus_cluster(void)
414 return &CPU_MASK_ALL;
417 static const cpumask_t *es7000_target_cpus(void)
419 return &cpumask_of_cpu(smp_processor_id());
423 es7000_check_apicid_used(physid_mask_t bitmap, int apicid)
427 static unsigned long es7000_check_apicid_present(int bit)
429 return physid_isset(bit, phys_cpu_present_map);
432 static unsigned long calculate_ldr(int cpu)
434 unsigned long id = per_cpu(x86_bios_cpu_apicid, cpu);
436 return SET_APIC_LOGICAL_ID(id);
440 * Set up the logical destination ID.
442 * Intel recommends to set DFR, LdR and TPR before enabling
443 * an APIC. See e.g. "AP-388 82489DX User's Manual" (Intel
444 * document number 292116). So here it goes...
446 static void es7000_init_apic_ldr_cluster(void)
449 int cpu = smp_processor_id();
451 apic_write(APIC_DFR, APIC_DFR_CLUSTER);
452 val = calculate_ldr(cpu);
453 apic_write(APIC_LDR, val);
456 static void es7000_init_apic_ldr(void)
459 int cpu = smp_processor_id();
461 apic_write(APIC_DFR, APIC_DFR_FLAT);
462 val = calculate_ldr(cpu);
463 apic_write(APIC_LDR, val);
466 static void es7000_setup_apic_routing(void)
468 int apic = per_cpu(x86_bios_cpu_apicid, smp_processor_id());
469 printk("Enabling APIC mode: %s. Using %d I/O APICs, target cpus %lx\n",
470 (apic_version[apic] == 0x14) ?
471 "Physical Cluster" : "Logical Cluster",
472 nr_ioapics, cpus_addr(*es7000_target_cpus())[0]);
475 static int es7000_apicid_to_node(int logical_apicid)
481 static int es7000_cpu_present_to_apicid(int mps_cpu)
484 return boot_cpu_physical_apicid;
485 else if (mps_cpu < nr_cpu_ids)
486 return per_cpu(x86_bios_cpu_apicid, mps_cpu);
491 static physid_mask_t es7000_apicid_to_cpu_present(int phys_apicid)
496 mask = physid_mask_of_physid(id);
502 /* Mapping from cpu number to logical apicid */
503 static int es7000_cpu_to_logical_apicid(int cpu)
506 if (cpu >= nr_cpu_ids)
508 return cpu_2_logical_apicid[cpu];
510 return logical_smp_processor_id();
514 static physid_mask_t es7000_ioapic_phys_id_map(physid_mask_t phys_map)
516 /* For clustered we don't have a good way to do this yet - hack */
517 return physids_promote(0xff);
520 static int es7000_check_phys_apicid_present(int cpu_physical_apicid)
522 boot_cpu_physical_apicid = read_apic_id();
527 es7000_cpu_mask_to_apicid_cluster(const struct cpumask *cpumask)
534 num_bits_set = cpumask_weight(cpumask);
535 /* Return id to all */
536 if (num_bits_set == nr_cpu_ids)
539 * The cpus in the mask must all be on the apic cluster. If are not
540 * on the same apicid cluster return default value of target_cpus():
542 cpu = cpumask_first(cpumask);
543 apicid = es7000_cpu_to_logical_apicid(cpu);
545 while (cpus_found < num_bits_set) {
546 if (cpumask_test_cpu(cpu, cpumask)) {
547 int new_apicid = es7000_cpu_to_logical_apicid(cpu);
549 if (APIC_CLUSTER(apicid) != APIC_CLUSTER(new_apicid)) {
550 printk("%s: Not a valid mask!\n", __func__);
562 static unsigned int es7000_cpu_mask_to_apicid(const cpumask_t *cpumask)
569 num_bits_set = cpus_weight(*cpumask);
570 /* Return id to all */
571 if (num_bits_set == nr_cpu_ids)
572 return es7000_cpu_to_logical_apicid(0);
574 * The cpus in the mask must all be on the apic cluster. If are not
575 * on the same apicid cluster return default value of target_cpus():
577 cpu = first_cpu(*cpumask);
578 apicid = es7000_cpu_to_logical_apicid(cpu);
579 while (cpus_found < num_bits_set) {
580 if (cpu_isset(cpu, *cpumask)) {
581 int new_apicid = es7000_cpu_to_logical_apicid(cpu);
583 if (APIC_CLUSTER(apicid) != APIC_CLUSTER(new_apicid)) {
584 printk("%s: Not a valid mask!\n", __func__);
586 return es7000_cpu_to_logical_apicid(0);
597 es7000_cpu_mask_to_apicid_and(const struct cpumask *inmask,
598 const struct cpumask *andmask)
600 int apicid = es7000_cpu_to_logical_apicid(0);
601 cpumask_var_t cpumask;
603 if (!alloc_cpumask_var(&cpumask, GFP_ATOMIC))
606 cpumask_and(cpumask, inmask, andmask);
607 cpumask_and(cpumask, cpumask, cpu_online_mask);
608 apicid = es7000_cpu_mask_to_apicid(cpumask);
610 free_cpumask_var(cpumask);
615 static int es7000_phys_pkg_id(int cpuid_apic, int index_msb)
617 return cpuid_apic >> index_msb;
620 void __init es7000_update_genapic_to_cluster(void)
622 apic->target_cpus = target_cpus_cluster;
623 apic->irq_delivery_mode = dest_LowestPrio;
624 /* logical delivery broadcast to all procs: */
625 apic->irq_dest_mode = 1;
627 apic->init_apic_ldr = es7000_init_apic_ldr_cluster;
629 apic->cpu_mask_to_apicid = es7000_cpu_mask_to_apicid_cluster;
632 static int probe_es7000(void)
634 /* probed later in mptable/ACPI hooks */
639 es7000_mps_oem_check(struct mpc_table *mpc, char *oem, char *productid)
642 struct mpc_oemtable *oem_table =
643 (struct mpc_oemtable *)mpc->oemptr;
645 if (!strncmp(oem, "UNISYS", 6))
646 return parse_unisys_oem((char *)oem_table);
652 /* Hook from generic ACPI tables.c */
653 static int __init es7000_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
655 unsigned long oem_addr = 0;
659 /* check dsdt at first to avoid clear fix_map for oem_addr */
660 check_dsdt = es7000_check_dsdt();
662 if (!find_unisys_acpi_oem_table(&oem_addr)) {
664 ret = parse_unisys_oem((char *)oem_addr);
670 * we need to unmap it
672 unmap_unisys_acpi_oem_table(oem_addr);
677 static int __init es7000_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
684 struct genapic apic_es7000 = {
687 .probe = probe_es7000,
688 .acpi_madt_oem_check = es7000_acpi_madt_oem_check,
689 .apic_id_registered = es7000_apic_id_registered,
691 .irq_delivery_mode = dest_Fixed,
692 /* phys delivery to target CPUs: */
695 .target_cpus = es7000_target_cpus,
698 .check_apicid_used = es7000_check_apicid_used,
699 .check_apicid_present = es7000_check_apicid_present,
701 .vector_allocation_domain = es7000_vector_allocation_domain,
702 .init_apic_ldr = es7000_init_apic_ldr,
704 .ioapic_phys_id_map = es7000_ioapic_phys_id_map,
705 .setup_apic_routing = es7000_setup_apic_routing,
706 .multi_timer_check = NULL,
707 .apicid_to_node = es7000_apicid_to_node,
708 .cpu_to_logical_apicid = es7000_cpu_to_logical_apicid,
709 .cpu_present_to_apicid = es7000_cpu_present_to_apicid,
710 .apicid_to_cpu_present = es7000_apicid_to_cpu_present,
711 .setup_portio_remap = NULL,
712 .check_phys_apicid_present = es7000_check_phys_apicid_present,
713 .enable_apic_mode = es7000_enable_apic_mode,
714 .phys_pkg_id = es7000_phys_pkg_id,
715 .mps_oem_check = es7000_mps_oem_check,
717 .get_apic_id = es7000_get_apic_id,
719 .apic_id_mask = 0xFF << 24,
721 .cpu_mask_to_apicid = es7000_cpu_mask_to_apicid,
722 .cpu_mask_to_apicid_and = es7000_cpu_mask_to_apicid_and,
724 .send_IPI_mask = es7000_send_IPI_mask,
725 .send_IPI_mask_allbutself = NULL,
726 .send_IPI_allbutself = es7000_send_IPI_allbutself,
727 .send_IPI_all = es7000_send_IPI_all,
728 .send_IPI_self = default_send_IPI_self,
732 .trampoline_phys_low = 0x467,
733 .trampoline_phys_high = 0x469,
735 .wait_for_init_deassert = es7000_wait_for_init_deassert,
737 /* Nothing to do for most platforms, since cleared by the INIT cycle: */
738 .smp_callin_clear_local_apic = NULL,
739 .store_NMI_vector = NULL,
740 .inquire_remote_apic = default_inquire_remote_apic,
742 .read = native_apic_mem_read,
743 .write = native_apic_mem_write,
744 .icr_read = native_apic_icr_read,
745 .icr_write = native_apic_icr_write,
746 .wait_icr_idle = native_apic_wait_icr_idle,
747 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,