1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
32
33#include <linux/notifier.h>
34#include <linux/spinlock.h>
35#include <linux/cpumask.h>
36#include <linux/threads.h>
37#include <linux/kernel.h>
38#include <linux/module.h>
39#include <linux/reboot.h>
40#include <linux/string.h>
41#include <linux/types.h>
42#include <linux/errno.h>
43#include <linux/acpi.h>
44#include <linux/init.h>
45#include <linux/gfp.h>
46#include <linux/nmi.h>
47#include <linux/smp.h>
48#include <linux/io.h>
49
50#include <asm/apicdef.h>
51#include <linux/atomic.h>
52#include <asm/fixmap.h>
53#include <asm/mpspec.h>
54#include <asm/setup.h>
55#include <asm/apic.h>
56#include <asm/ipi.h>
57
58
59
60
61
62#define NON_UNISYS 0
63#define ES7000_CLASSIC 1
64#define ES7000_ZORRO 2
65
66#define MIP_REG 1
67#define MIP_PSAI_REG 4
68
69#define MIP_BUSY 1
70#define MIP_SPIN 0xf0000
71#define MIP_VALID 0x0100000000000000ULL
72#define MIP_SW_APIC 0x1020b
73
74#define MIP_PORT(val) ((val >> 32) & 0xffff)
75
76#define MIP_RD_LO(val) (val & 0xffffffff)
77
78struct mip_reg {
79 unsigned long long off_0x00;
80 unsigned long long off_0x08;
81 unsigned long long off_0x10;
82 unsigned long long off_0x18;
83 unsigned long long off_0x20;
84 unsigned long long off_0x28;
85 unsigned long long off_0x30;
86 unsigned long long off_0x38;
87};
88
89struct mip_reg_info {
90 unsigned long long mip_info;
91 unsigned long long delivery_info;
92 unsigned long long host_reg;
93 unsigned long long mip_reg;
94};
95
96struct psai {
97 unsigned long long entry_type;
98 unsigned long long addr;
99 unsigned long long bep_addr;
100};
101
102#ifdef CONFIG_ACPI
103
104struct es7000_oem_table {
105 struct acpi_table_header Header;
106 u32 OEMTableAddr;
107 u32 OEMTableSize;
108};
109
110static unsigned long oem_addrX;
111static unsigned long oem_size;
112
113#endif
114
115
116
117
118
119static volatile unsigned long *psai;
120static struct mip_reg *mip_reg;
121static struct mip_reg *host_reg;
122static int mip_port;
123static unsigned long mip_addr;
124static unsigned long host_addr;
125
126int es7000_plat;
127
128
129
130
131
132
133static int __cpuinit wakeup_secondary_cpu_via_mip(int cpu, unsigned long eip)
134{
135 unsigned long vect = 0, psaival = 0;
136
137 if (psai == NULL)
138 return -1;
139
140 vect = ((unsigned long)__pa(eip)/0x1000) << 16;
141 psaival = (0x1000000 | vect | cpu);
142
143 while (*psai & 0x1000000)
144 ;
145
146 *psai = psaival;
147
148 return 0;
149}
150
151static int es7000_apic_is_cluster(void)
152{
153
154 if (boot_cpu_data.x86 == 6 &&
155 (boot_cpu_data.x86_model >= 7 && boot_cpu_data.x86_model <= 11))
156 return 1;
157
158 return 0;
159}
160
161static void setup_unisys(void)
162{
163
164
165
166
167
168
169
170 if (!(boot_cpu_data.x86 <= 15 && boot_cpu_data.x86_model <= 2))
171 es7000_plat = ES7000_ZORRO;
172 else
173 es7000_plat = ES7000_CLASSIC;
174}
175
176
177
178
179static int parse_unisys_oem(char *oemptr)
180{
181 int i;
182 int success = 0;
183 unsigned char type, size;
184 unsigned long val;
185 char *tp = NULL;
186 struct psai *psaip = NULL;
187 struct mip_reg_info *mi;
188 struct mip_reg *host, *mip;
189
190 tp = oemptr;
191
192 tp += 8;
193
194 for (i = 0; i <= 6; i++) {
195 type = *tp++;
196 size = *tp++;
197 tp -= 2;
198 switch (type) {
199 case MIP_REG:
200 mi = (struct mip_reg_info *)tp;
201 val = MIP_RD_LO(mi->host_reg);
202 host_addr = val;
203 host = (struct mip_reg *)val;
204 host_reg = __va(host);
205 val = MIP_RD_LO(mi->mip_reg);
206 mip_port = MIP_PORT(mi->mip_info);
207 mip_addr = val;
208 mip = (struct mip_reg *)val;
209 mip_reg = __va(mip);
210 pr_debug("host_reg = 0x%lx\n",
211 (unsigned long)host_reg);
212 pr_debug("mip_reg = 0x%lx\n",
213 (unsigned long)mip_reg);
214 success++;
215 break;
216 case MIP_PSAI_REG:
217 psaip = (struct psai *)tp;
218 if (tp != NULL) {
219 if (psaip->addr)
220 psai = __va(psaip->addr);
221 else
222 psai = NULL;
223 success++;
224 }
225 break;
226 default:
227 break;
228 }
229 tp += size;
230 }
231
232 if (success < 2)
233 es7000_plat = NON_UNISYS;
234 else
235 setup_unisys();
236
237 return es7000_plat;
238}
239
240#ifdef CONFIG_ACPI
241static int __init find_unisys_acpi_oem_table(unsigned long *oem_addr)
242{
243 struct acpi_table_header *header = NULL;
244 struct es7000_oem_table *table;
245 acpi_size tbl_size;
246 acpi_status ret;
247 int i = 0;
248
249 for (;;) {
250 ret = acpi_get_table_with_size("OEM1", i++, &header, &tbl_size);
251 if (!ACPI_SUCCESS(ret))
252 return -1;
253
254 if (!memcmp((char *) &header->oem_id, "UNISYS", 6))
255 break;
256
257 early_acpi_os_unmap_memory(header, tbl_size);
258 }
259
260 table = (void *)header;
261
262 oem_addrX = table->OEMTableAddr;
263 oem_size = table->OEMTableSize;
264
265 early_acpi_os_unmap_memory(header, tbl_size);
266
267 *oem_addr = (unsigned long)__acpi_map_table(oem_addrX, oem_size);
268
269 return 0;
270}
271
272static void __init unmap_unisys_acpi_oem_table(unsigned long oem_addr)
273{
274 if (!oem_addr)
275 return;
276
277 __acpi_unmap_table((char *)oem_addr, oem_size);
278}
279
280static int es7000_check_dsdt(void)
281{
282 struct acpi_table_header header;
283
284 if (ACPI_SUCCESS(acpi_get_table_header(ACPI_SIG_DSDT, 0, &header)) &&
285 !strncmp(header.oem_id, "UNISYS", 6))
286 return 1;
287 return 0;
288}
289
290static int es7000_acpi_ret;
291
292
293static int __init es7000_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
294{
295 unsigned long oem_addr = 0;
296 int check_dsdt;
297 int ret = 0;
298
299
300 check_dsdt = es7000_check_dsdt();
301
302 if (!find_unisys_acpi_oem_table(&oem_addr)) {
303 if (check_dsdt) {
304 ret = parse_unisys_oem((char *)oem_addr);
305 } else {
306 setup_unisys();
307 ret = 1;
308 }
309
310
311
312 unmap_unisys_acpi_oem_table(oem_addr);
313 }
314
315 es7000_acpi_ret = ret;
316
317 return ret && !es7000_apic_is_cluster();
318}
319
320static int es7000_acpi_madt_oem_check_cluster(char *oem_id, char *oem_table_id)
321{
322 int ret = es7000_acpi_ret;
323
324 return ret && es7000_apic_is_cluster();
325}
326
327#else
328static int es7000_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
329{
330 return 0;
331}
332
333static int es7000_acpi_madt_oem_check_cluster(char *oem_id, char *oem_table_id)
334{
335 return 0;
336}
337#endif
338
339static void es7000_spin(int n)
340{
341 int i = 0;
342
343 while (i++ < n)
344 rep_nop();
345}
346
347static int es7000_mip_write(struct mip_reg *mip_reg)
348{
349 int status = 0;
350 int spin;
351
352 spin = MIP_SPIN;
353 while ((host_reg->off_0x38 & MIP_VALID) != 0) {
354 if (--spin <= 0) {
355 WARN(1, "Timeout waiting for Host Valid Flag\n");
356 return -1;
357 }
358 es7000_spin(MIP_SPIN);
359 }
360
361 memcpy(host_reg, mip_reg, sizeof(struct mip_reg));
362 outb(1, mip_port);
363
364 spin = MIP_SPIN;
365
366 while ((mip_reg->off_0x38 & MIP_VALID) == 0) {
367 if (--spin <= 0) {
368 WARN(1, "Timeout waiting for MIP Valid Flag\n");
369 return -1;
370 }
371 es7000_spin(MIP_SPIN);
372 }
373
374 status = (mip_reg->off_0x00 & 0xffff0000000000ULL) >> 48;
375 mip_reg->off_0x38 &= ~MIP_VALID;
376
377 return status;
378}
379
380static void es7000_enable_apic_mode(void)
381{
382 struct mip_reg es7000_mip_reg;
383 int mip_status;
384
385 if (!es7000_plat)
386 return;
387
388 pr_info("Enabling APIC mode.\n");
389 memset(&es7000_mip_reg, 0, sizeof(struct mip_reg));
390 es7000_mip_reg.off_0x00 = MIP_SW_APIC;
391 es7000_mip_reg.off_0x38 = MIP_VALID;
392
393 while ((mip_status = es7000_mip_write(&es7000_mip_reg)) != 0)
394 WARN(1, "Command failed, status = %x\n", mip_status);
395}
396
397static void es7000_wait_for_init_deassert(atomic_t *deassert)
398{
399 while (!atomic_read(deassert))
400 cpu_relax();
401}
402
403static unsigned int es7000_get_apic_id(unsigned long x)
404{
405 return (x >> 24) & 0xFF;
406}
407
408static void es7000_send_IPI_mask(const struct cpumask *mask, int vector)
409{
410 default_send_IPI_mask_sequence_phys(mask, vector);
411}
412
413static void es7000_send_IPI_allbutself(int vector)
414{
415 default_send_IPI_mask_allbutself_phys(cpu_online_mask, vector);
416}
417
418static void es7000_send_IPI_all(int vector)
419{
420 es7000_send_IPI_mask(cpu_online_mask, vector);
421}
422
423static int es7000_apic_id_registered(void)
424{
425 return 1;
426}
427
428static const struct cpumask *target_cpus_cluster(void)
429{
430 return cpu_all_mask;
431}
432
433static const struct cpumask *es7000_target_cpus(void)
434{
435 return cpumask_of(smp_processor_id());
436}
437
438static unsigned long es7000_check_apicid_used(physid_mask_t *map, int apicid)
439{
440 return 0;
441}
442
443static unsigned long es7000_check_apicid_present(int bit)
444{
445 return physid_isset(bit, phys_cpu_present_map);
446}
447
448static int es7000_early_logical_apicid(int cpu)
449{
450
451 return early_per_cpu(x86_bios_cpu_apicid, cpu);
452}
453
454static unsigned long calculate_ldr(int cpu)
455{
456 unsigned long id = per_cpu(x86_bios_cpu_apicid, cpu);
457
458 return SET_APIC_LOGICAL_ID(id);
459}
460
461
462
463
464
465
466
467
468static void es7000_init_apic_ldr_cluster(void)
469{
470 unsigned long val;
471 int cpu = smp_processor_id();
472
473 apic_write(APIC_DFR, APIC_DFR_CLUSTER);
474 val = calculate_ldr(cpu);
475 apic_write(APIC_LDR, val);
476}
477
478static void es7000_init_apic_ldr(void)
479{
480 unsigned long val;
481 int cpu = smp_processor_id();
482
483 apic_write(APIC_DFR, APIC_DFR_FLAT);
484 val = calculate_ldr(cpu);
485 apic_write(APIC_LDR, val);
486}
487
488static void es7000_setup_apic_routing(void)
489{
490 int apic = per_cpu(x86_bios_cpu_apicid, smp_processor_id());
491
492 pr_info("Enabling APIC mode: %s. Using %d I/O APICs, target cpus %lx\n",
493 (apic_version[apic] == 0x14) ?
494 "Physical Cluster" : "Logical Cluster",
495 nr_ioapics, cpumask_bits(es7000_target_cpus())[0]);
496}
497
498static int es7000_cpu_present_to_apicid(int mps_cpu)
499{
500 if (!mps_cpu)
501 return boot_cpu_physical_apicid;
502 else if (mps_cpu < nr_cpu_ids)
503 return per_cpu(x86_bios_cpu_apicid, mps_cpu);
504 else
505 return BAD_APICID;
506}
507
508static int cpu_id;
509
510static void es7000_apicid_to_cpu_present(int phys_apicid, physid_mask_t *retmap)
511{
512 physid_set_mask_of_physid(cpu_id, retmap);
513 ++cpu_id;
514}
515
516static void es7000_ioapic_phys_id_map(physid_mask_t *phys_map, physid_mask_t *retmap)
517{
518
519 physids_promote(0xFFL, retmap);
520}
521
522static int es7000_check_phys_apicid_present(int cpu_physical_apicid)
523{
524 boot_cpu_physical_apicid = read_apic_id();
525 return 1;
526}
527
528static inline int
529es7000_cpu_mask_to_apicid(const struct cpumask *cpumask, unsigned int *dest_id)
530{
531 unsigned int round = 0;
532 unsigned int cpu, uninitialized_var(apicid);
533
534
535
536
537 for_each_cpu_and(cpu, cpumask, cpu_online_mask) {
538 int new_apicid = early_per_cpu(x86_cpu_to_logical_apicid, cpu);
539
540 if (round && APIC_CLUSTER(apicid) != APIC_CLUSTER(new_apicid)) {
541 WARN(1, "Not a valid mask!");
542
543 return -EINVAL;
544 }
545 apicid |= new_apicid;
546 round++;
547 }
548 if (!round)
549 return -EINVAL;
550 *dest_id = apicid;
551 return 0;
552}
553
554static int
555es7000_cpu_mask_to_apicid_and(const struct cpumask *inmask,
556 const struct cpumask *andmask,
557 unsigned int *apicid)
558{
559 cpumask_var_t cpumask;
560 *apicid = early_per_cpu(x86_cpu_to_logical_apicid, 0);
561
562 if (!alloc_cpumask_var(&cpumask, GFP_ATOMIC))
563 return 0;
564
565 cpumask_and(cpumask, inmask, andmask);
566 es7000_cpu_mask_to_apicid(cpumask, apicid);
567
568 free_cpumask_var(cpumask);
569
570 return 0;
571}
572
573static int es7000_phys_pkg_id(int cpuid_apic, int index_msb)
574{
575 return cpuid_apic >> index_msb;
576}
577
578static int probe_es7000(void)
579{
580
581 return 0;
582}
583
584static int es7000_mps_ret;
585static int es7000_mps_oem_check(struct mpc_table *mpc, char *oem,
586 char *productid)
587{
588 int ret = 0;
589
590 if (mpc->oemptr) {
591 struct mpc_oemtable *oem_table =
592 (struct mpc_oemtable *)mpc->oemptr;
593
594 if (!strncmp(oem, "UNISYS", 6))
595 ret = parse_unisys_oem((char *)oem_table);
596 }
597
598 es7000_mps_ret = ret;
599
600 return ret && !es7000_apic_is_cluster();
601}
602
603static int es7000_mps_oem_check_cluster(struct mpc_table *mpc, char *oem,
604 char *productid)
605{
606 int ret = es7000_mps_ret;
607
608 return ret && es7000_apic_is_cluster();
609}
610
611
612static struct apic __refdata apic_es7000_cluster = {
613
614 .name = "es7000",
615 .probe = probe_es7000,
616 .acpi_madt_oem_check = es7000_acpi_madt_oem_check_cluster,
617 .apic_id_valid = default_apic_id_valid,
618 .apic_id_registered = es7000_apic_id_registered,
619
620 .irq_delivery_mode = dest_LowestPrio,
621
622 .irq_dest_mode = 1,
623
624 .target_cpus = target_cpus_cluster,
625 .disable_esr = 1,
626 .dest_logical = 0,
627 .check_apicid_used = es7000_check_apicid_used,
628 .check_apicid_present = es7000_check_apicid_present,
629
630 .vector_allocation_domain = flat_vector_allocation_domain,
631 .init_apic_ldr = es7000_init_apic_ldr_cluster,
632
633 .ioapic_phys_id_map = es7000_ioapic_phys_id_map,
634 .setup_apic_routing = es7000_setup_apic_routing,
635 .multi_timer_check = NULL,
636 .cpu_present_to_apicid = es7000_cpu_present_to_apicid,
637 .apicid_to_cpu_present = es7000_apicid_to_cpu_present,
638 .setup_portio_remap = NULL,
639 .check_phys_apicid_present = es7000_check_phys_apicid_present,
640 .enable_apic_mode = es7000_enable_apic_mode,
641 .phys_pkg_id = es7000_phys_pkg_id,
642 .mps_oem_check = es7000_mps_oem_check_cluster,
643
644 .get_apic_id = es7000_get_apic_id,
645 .set_apic_id = NULL,
646 .apic_id_mask = 0xFF << 24,
647
648 .cpu_mask_to_apicid_and = es7000_cpu_mask_to_apicid_and,
649
650 .send_IPI_mask = es7000_send_IPI_mask,
651 .send_IPI_mask_allbutself = NULL,
652 .send_IPI_allbutself = es7000_send_IPI_allbutself,
653 .send_IPI_all = es7000_send_IPI_all,
654 .send_IPI_self = default_send_IPI_self,
655
656 .wakeup_secondary_cpu = wakeup_secondary_cpu_via_mip,
657
658 .trampoline_phys_low = 0x467,
659 .trampoline_phys_high = 0x469,
660
661 .wait_for_init_deassert = NULL,
662
663
664 .smp_callin_clear_local_apic = NULL,
665 .inquire_remote_apic = default_inquire_remote_apic,
666
667 .read = native_apic_mem_read,
668 .write = native_apic_mem_write,
669 .eoi_write = native_apic_mem_write,
670 .icr_read = native_apic_icr_read,
671 .icr_write = native_apic_icr_write,
672 .wait_icr_idle = native_apic_wait_icr_idle,
673 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
674
675 .x86_32_early_logical_apicid = es7000_early_logical_apicid,
676};
677
678static struct apic __refdata apic_es7000 = {
679
680 .name = "es7000",
681 .probe = probe_es7000,
682 .acpi_madt_oem_check = es7000_acpi_madt_oem_check,
683 .apic_id_valid = default_apic_id_valid,
684 .apic_id_registered = es7000_apic_id_registered,
685
686 .irq_delivery_mode = dest_Fixed,
687
688 .irq_dest_mode = 0,
689
690 .target_cpus = es7000_target_cpus,
691 .disable_esr = 1,
692 .dest_logical = 0,
693 .check_apicid_used = es7000_check_apicid_used,
694 .check_apicid_present = es7000_check_apicid_present,
695
696 .vector_allocation_domain = flat_vector_allocation_domain,
697 .init_apic_ldr = es7000_init_apic_ldr,
698
699 .ioapic_phys_id_map = es7000_ioapic_phys_id_map,
700 .setup_apic_routing = es7000_setup_apic_routing,
701 .multi_timer_check = NULL,
702 .cpu_present_to_apicid = es7000_cpu_present_to_apicid,
703 .apicid_to_cpu_present = es7000_apicid_to_cpu_present,
704 .setup_portio_remap = NULL,
705 .check_phys_apicid_present = es7000_check_phys_apicid_present,
706 .enable_apic_mode = es7000_enable_apic_mode,
707 .phys_pkg_id = es7000_phys_pkg_id,
708 .mps_oem_check = es7000_mps_oem_check,
709
710 .get_apic_id = es7000_get_apic_id,
711 .set_apic_id = NULL,
712 .apic_id_mask = 0xFF << 24,
713
714 .cpu_mask_to_apicid_and = es7000_cpu_mask_to_apicid_and,
715
716 .send_IPI_mask = es7000_send_IPI_mask,
717 .send_IPI_mask_allbutself = NULL,
718 .send_IPI_allbutself = es7000_send_IPI_allbutself,
719 .send_IPI_all = es7000_send_IPI_all,
720 .send_IPI_self = default_send_IPI_self,
721
722 .trampoline_phys_low = 0x467,
723 .trampoline_phys_high = 0x469,
724
725 .wait_for_init_deassert = es7000_wait_for_init_deassert,
726
727
728 .smp_callin_clear_local_apic = NULL,
729 .inquire_remote_apic = default_inquire_remote_apic,
730
731 .read = native_apic_mem_read,
732 .write = native_apic_mem_write,
733 .eoi_write = native_apic_mem_write,
734 .icr_read = native_apic_icr_read,
735 .icr_write = native_apic_icr_write,
736 .wait_icr_idle = native_apic_wait_icr_idle,
737 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
738
739 .x86_32_early_logical_apicid = es7000_early_logical_apicid,
740};
741
742
743
744
745
746apic_drivers(apic_es7000, apic_es7000_cluster);
747