Merge commit 'remotes/tip/x86/paravirt' into x86/untangle2
[pandora-kernel.git] / arch / x86 / mach-voyager / voyager_smp.c
1 /* -*- mode: c; c-basic-offset: 8 -*- */
2
3 /* Copyright (C) 1999,2001
4  *
5  * Author: J.E.J.Bottomley@HansenPartnership.com
6  *
7  * This file provides all the same external entries as smp.c but uses
8  * the voyager hal to provide the functionality
9  */
10 #include <linux/cpu.h>
11 #include <linux/module.h>
12 #include <linux/mm.h>
13 #include <linux/kernel_stat.h>
14 #include <linux/delay.h>
15 #include <linux/mc146818rtc.h>
16 #include <linux/cache.h>
17 #include <linux/interrupt.h>
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/bootmem.h>
21 #include <linux/completion.h>
22 #include <asm/desc.h>
23 #include <asm/voyager.h>
24 #include <asm/vic.h>
25 #include <asm/mtrr.h>
26 #include <asm/pgalloc.h>
27 #include <asm/tlbflush.h>
28 #include <asm/arch_hooks.h>
29 #include <asm/trampoline.h>
30
31 /* TLB state -- visible externally, indexed physically */
32 DEFINE_PER_CPU_SHARED_ALIGNED(struct tlb_state, cpu_tlbstate) = { &init_mm, 0 };
33
34 /* CPU IRQ affinity -- set to all ones initially */
35 static unsigned long cpu_irq_affinity[NR_CPUS] __cacheline_aligned =
36         {[0 ... NR_CPUS-1]  = ~0UL };
37
38 /* per CPU data structure (for /proc/cpuinfo et al), visible externally
39  * indexed physically */
40 DEFINE_PER_CPU_SHARED_ALIGNED(struct cpuinfo_x86, cpu_info);
41 EXPORT_PER_CPU_SYMBOL(cpu_info);
42
43 /* physical ID of the CPU used to boot the system */
44 unsigned char boot_cpu_id;
45
46 /* The memory line addresses for the Quad CPIs */
47 struct voyager_qic_cpi *voyager_quad_cpi_addr[NR_CPUS] __cacheline_aligned;
48
49 /* The masks for the Extended VIC processors, filled in by cat_init */
50 __u32 voyager_extended_vic_processors = 0;
51
52 /* Masks for the extended Quad processors which cannot be VIC booted */
53 __u32 voyager_allowed_boot_processors = 0;
54
55 /* The mask for the Quad Processors (both extended and non-extended) */
56 __u32 voyager_quad_processors = 0;
57
58 /* Total count of live CPUs, used in process.c to display
59  * the CPU information and in irq.c for the per CPU irq
60  * activity count.  Finally exported by i386_ksyms.c */
61 static int voyager_extended_cpus = 1;
62
63 /* Used for the invalidate map that's also checked in the spinlock */
64 static volatile unsigned long smp_invalidate_needed;
65
66 /* Bitmask of CPUs present in the system - exported by i386_syms.c, used
67  * by scheduler but indexed physically */
68 cpumask_t phys_cpu_present_map = CPU_MASK_NONE;
69
70 /* The internal functions */
71 static void send_CPI(__u32 cpuset, __u8 cpi);
72 static void ack_CPI(__u8 cpi);
73 static int ack_QIC_CPI(__u8 cpi);
74 static void ack_special_QIC_CPI(__u8 cpi);
75 static void ack_VIC_CPI(__u8 cpi);
76 static void send_CPI_allbutself(__u8 cpi);
77 static void mask_vic_irq(unsigned int irq);
78 static void unmask_vic_irq(unsigned int irq);
79 static unsigned int startup_vic_irq(unsigned int irq);
80 static void enable_local_vic_irq(unsigned int irq);
81 static void disable_local_vic_irq(unsigned int irq);
82 static void before_handle_vic_irq(unsigned int irq);
83 static void after_handle_vic_irq(unsigned int irq);
84 static void set_vic_irq_affinity(unsigned int irq, const struct cpumask *mask);
85 static void ack_vic_irq(unsigned int irq);
86 static void vic_enable_cpi(void);
87 static void do_boot_cpu(__u8 cpuid);
88 static void do_quad_bootstrap(void);
89 static void initialize_secondary(void);
90
91 int hard_smp_processor_id(void);
92 int safe_smp_processor_id(void);
93
94 /* Inline functions */
95 static inline void send_one_QIC_CPI(__u8 cpu, __u8 cpi)
96 {
97         voyager_quad_cpi_addr[cpu]->qic_cpi[cpi].cpi =
98             (smp_processor_id() << 16) + cpi;
99 }
100
101 static inline void send_QIC_CPI(__u32 cpuset, __u8 cpi)
102 {
103         int cpu;
104
105         for_each_online_cpu(cpu) {
106                 if (cpuset & (1 << cpu)) {
107 #ifdef VOYAGER_DEBUG
108                         if (!cpu_online(cpu))
109                                 VDEBUG(("CPU%d sending cpi %d to CPU%d not in "
110                                         "cpu_online_map\n",
111                                         hard_smp_processor_id(), cpi, cpu));
112 #endif
113                         send_one_QIC_CPI(cpu, cpi - QIC_CPI_OFFSET);
114                 }
115         }
116 }
117
118 static inline void wrapper_smp_local_timer_interrupt(void)
119 {
120         irq_enter();
121         smp_local_timer_interrupt();
122         irq_exit();
123 }
124
125 static inline void send_one_CPI(__u8 cpu, __u8 cpi)
126 {
127         if (voyager_quad_processors & (1 << cpu))
128                 send_one_QIC_CPI(cpu, cpi - QIC_CPI_OFFSET);
129         else
130                 send_CPI(1 << cpu, cpi);
131 }
132
133 static inline void send_CPI_allbutself(__u8 cpi)
134 {
135         __u8 cpu = smp_processor_id();
136         __u32 mask = cpus_addr(cpu_online_map)[0] & ~(1 << cpu);
137         send_CPI(mask, cpi);
138 }
139
140 static inline int is_cpu_quad(void)
141 {
142         __u8 cpumask = inb(VIC_PROC_WHO_AM_I);
143         return ((cpumask & QUAD_IDENTIFIER) == QUAD_IDENTIFIER);
144 }
145
146 static inline int is_cpu_extended(void)
147 {
148         __u8 cpu = hard_smp_processor_id();
149
150         return (voyager_extended_vic_processors & (1 << cpu));
151 }
152
153 static inline int is_cpu_vic_boot(void)
154 {
155         __u8 cpu = hard_smp_processor_id();
156
157         return (voyager_extended_vic_processors
158                 & voyager_allowed_boot_processors & (1 << cpu));
159 }
160
161 static inline void ack_CPI(__u8 cpi)
162 {
163         switch (cpi) {
164         case VIC_CPU_BOOT_CPI:
165                 if (is_cpu_quad() && !is_cpu_vic_boot())
166                         ack_QIC_CPI(cpi);
167                 else
168                         ack_VIC_CPI(cpi);
169                 break;
170         case VIC_SYS_INT:
171         case VIC_CMN_INT:
172                 /* These are slightly strange.  Even on the Quad card,
173                  * They are vectored as VIC CPIs */
174                 if (is_cpu_quad())
175                         ack_special_QIC_CPI(cpi);
176                 else
177                         ack_VIC_CPI(cpi);
178                 break;
179         default:
180                 printk("VOYAGER ERROR: CPI%d is in common CPI code\n", cpi);
181                 break;
182         }
183 }
184
185 /* local variables */
186
187 /* The VIC IRQ descriptors -- these look almost identical to the
188  * 8259 IRQs except that masks and things must be kept per processor
189  */
190 static struct irq_chip vic_chip = {
191         .name = "VIC",
192         .startup = startup_vic_irq,
193         .mask = mask_vic_irq,
194         .unmask = unmask_vic_irq,
195         .set_affinity = set_vic_irq_affinity,
196 };
197
198 /* used to count up as CPUs are brought on line (starts at 0) */
199 static int cpucount = 0;
200
201 /* The per cpu profile stuff - used in smp_local_timer_interrupt */
202 static DEFINE_PER_CPU(int, prof_multiplier) = 1;
203 static DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
204 static DEFINE_PER_CPU(int, prof_counter) = 1;
205
206 /* the map used to check if a CPU has booted */
207 static __u32 cpu_booted_map;
208
209 /* the synchronize flag used to hold all secondary CPUs spinning in
210  * a tight loop until the boot sequence is ready for them */
211 static cpumask_t smp_commenced_mask = CPU_MASK_NONE;
212
213 /* This is for the new dynamic CPU boot code */
214
215 /* The per processor IRQ masks (these are usually kept in sync) */
216 static __u16 vic_irq_mask[NR_CPUS] __cacheline_aligned;
217
218 /* the list of IRQs to be enabled by the VIC_ENABLE_IRQ_CPI */
219 static __u16 vic_irq_enable_mask[NR_CPUS] __cacheline_aligned = { 0 };
220
221 /* Lock for enable/disable of VIC interrupts */
222 static __cacheline_aligned DEFINE_SPINLOCK(vic_irq_lock);
223
224 /* The boot processor is correctly set up in PC mode when it
225  * comes up, but the secondaries need their master/slave 8259
226  * pairs initializing correctly */
227
228 /* Interrupt counters (per cpu) and total - used to try to
229  * even up the interrupt handling routines */
230 static long vic_intr_total = 0;
231 static long vic_intr_count[NR_CPUS] __cacheline_aligned = { 0 };
232 static unsigned long vic_tick[NR_CPUS] __cacheline_aligned = { 0 };
233
234 /* Since we can only use CPI0, we fake all the other CPIs */
235 static unsigned long vic_cpi_mailbox[NR_CPUS] __cacheline_aligned;
236
237 /* debugging routine to read the isr of the cpu's pic */
238 static inline __u16 vic_read_isr(void)
239 {
240         __u16 isr;
241
242         outb(0x0b, 0xa0);
243         isr = inb(0xa0) << 8;
244         outb(0x0b, 0x20);
245         isr |= inb(0x20);
246
247         return isr;
248 }
249
250 static __init void qic_setup(void)
251 {
252         if (!is_cpu_quad()) {
253                 /* not a quad, no setup */
254                 return;
255         }
256         outb(QIC_DEFAULT_MASK0, QIC_MASK_REGISTER0);
257         outb(QIC_CPI_ENABLE, QIC_MASK_REGISTER1);
258
259         if (is_cpu_extended()) {
260                 /* the QIC duplicate of the VIC base register */
261                 outb(VIC_DEFAULT_CPI_BASE, QIC_VIC_CPI_BASE_REGISTER);
262                 outb(QIC_DEFAULT_CPI_BASE, QIC_CPI_BASE_REGISTER);
263
264                 /* FIXME: should set up the QIC timer and memory parity
265                  * error vectors here */
266         }
267 }
268
269 static __init void vic_setup_pic(void)
270 {
271         outb(1, VIC_REDIRECT_REGISTER_1);
272         /* clear the claim registers for dynamic routing */
273         outb(0, VIC_CLAIM_REGISTER_0);
274         outb(0, VIC_CLAIM_REGISTER_1);
275
276         outb(0, VIC_PRIORITY_REGISTER);
277         /* Set the Primary and Secondary Microchannel vector
278          * bases to be the same as the ordinary interrupts
279          *
280          * FIXME: This would be more efficient using separate
281          * vectors. */
282         outb(FIRST_EXTERNAL_VECTOR, VIC_PRIMARY_MC_BASE);
283         outb(FIRST_EXTERNAL_VECTOR, VIC_SECONDARY_MC_BASE);
284         /* Now initiallise the master PIC belonging to this CPU by
285          * sending the four ICWs */
286
287         /* ICW1: level triggered, ICW4 needed */
288         outb(0x19, 0x20);
289
290         /* ICW2: vector base */
291         outb(FIRST_EXTERNAL_VECTOR, 0x21);
292
293         /* ICW3: slave at line 2 */
294         outb(0x04, 0x21);
295
296         /* ICW4: 8086 mode */
297         outb(0x01, 0x21);
298
299         /* now the same for the slave PIC */
300
301         /* ICW1: level trigger, ICW4 needed */
302         outb(0x19, 0xA0);
303
304         /* ICW2: slave vector base */
305         outb(FIRST_EXTERNAL_VECTOR + 8, 0xA1);
306
307         /* ICW3: slave ID */
308         outb(0x02, 0xA1);
309
310         /* ICW4: 8086 mode */
311         outb(0x01, 0xA1);
312 }
313
314 static void do_quad_bootstrap(void)
315 {
316         if (is_cpu_quad() && is_cpu_vic_boot()) {
317                 int i;
318                 unsigned long flags;
319                 __u8 cpuid = hard_smp_processor_id();
320
321                 local_irq_save(flags);
322
323                 for (i = 0; i < 4; i++) {
324                         /* FIXME: this would be >>3 &0x7 on the 32 way */
325                         if (((cpuid >> 2) & 0x03) == i)
326                                 /* don't lower our own mask! */
327                                 continue;
328
329                         /* masquerade as local Quad CPU */
330                         outb(QIC_CPUID_ENABLE | i, QIC_PROCESSOR_ID);
331                         /* enable the startup CPI */
332                         outb(QIC_BOOT_CPI_MASK, QIC_MASK_REGISTER1);
333                         /* restore cpu id */
334                         outb(0, QIC_PROCESSOR_ID);
335                 }
336                 local_irq_restore(flags);
337         }
338 }
339
340 void prefill_possible_map(void)
341 {
342         /* This is empty on voyager because we need a much
343          * earlier detection which is done in find_smp_config */
344 }
345
346 /* Set up all the basic stuff: read the SMP config and make all the
347  * SMP information reflect only the boot cpu.  All others will be
348  * brought on-line later. */
349 void __init find_smp_config(void)
350 {
351         int i;
352
353         boot_cpu_id = hard_smp_processor_id();
354
355         printk("VOYAGER SMP: Boot cpu is %d\n", boot_cpu_id);
356
357         /* initialize the CPU structures (moved from smp_boot_cpus) */
358         for (i = 0; i < nr_cpu_ids; i++)
359                 cpu_irq_affinity[i] = ~0;
360         cpu_online_map = cpumask_of_cpu(boot_cpu_id);
361
362         /* The boot CPU must be extended */
363         voyager_extended_vic_processors = 1 << boot_cpu_id;
364         /* initially, all of the first 8 CPUs can boot */
365         voyager_allowed_boot_processors = 0xff;
366         /* set up everything for just this CPU, we can alter
367          * this as we start the other CPUs later */
368         /* now get the CPU disposition from the extended CMOS */
369         cpus_addr(phys_cpu_present_map)[0] =
370             voyager_extended_cmos_read(VOYAGER_PROCESSOR_PRESENT_MASK);
371         cpus_addr(phys_cpu_present_map)[0] |=
372             voyager_extended_cmos_read(VOYAGER_PROCESSOR_PRESENT_MASK + 1) << 8;
373         cpus_addr(phys_cpu_present_map)[0] |=
374             voyager_extended_cmos_read(VOYAGER_PROCESSOR_PRESENT_MASK +
375                                        2) << 16;
376         cpus_addr(phys_cpu_present_map)[0] |=
377             voyager_extended_cmos_read(VOYAGER_PROCESSOR_PRESENT_MASK +
378                                        3) << 24;
379         init_cpu_possible(&phys_cpu_present_map);
380         printk("VOYAGER SMP: phys_cpu_present_map = 0x%lx\n",
381                cpus_addr(phys_cpu_present_map)[0]);
382         /* Here we set up the VIC to enable SMP */
383         /* enable the CPIs by writing the base vector to their register */
384         outb(VIC_DEFAULT_CPI_BASE, VIC_CPI_BASE_REGISTER);
385         outb(1, VIC_REDIRECT_REGISTER_1);
386         /* set the claim registers for static routing --- Boot CPU gets
387          * all interrupts untill all other CPUs started */
388         outb(0xff, VIC_CLAIM_REGISTER_0);
389         outb(0xff, VIC_CLAIM_REGISTER_1);
390         /* Set the Primary and Secondary Microchannel vector
391          * bases to be the same as the ordinary interrupts
392          *
393          * FIXME: This would be more efficient using separate
394          * vectors. */
395         outb(FIRST_EXTERNAL_VECTOR, VIC_PRIMARY_MC_BASE);
396         outb(FIRST_EXTERNAL_VECTOR, VIC_SECONDARY_MC_BASE);
397
398         /* Finally tell the firmware that we're driving */
399         outb(inb(VOYAGER_SUS_IN_CONTROL_PORT) | VOYAGER_IN_CONTROL_FLAG,
400              VOYAGER_SUS_IN_CONTROL_PORT);
401
402         current_thread_info()->cpu = boot_cpu_id;
403         percpu_write(cpu_number, boot_cpu_id);
404 }
405
406 /*
407  *      The bootstrap kernel entry code has set these up. Save them
408  *      for a given CPU, id is physical */
409 void __init smp_store_cpu_info(int id)
410 {
411         struct cpuinfo_x86 *c = &cpu_data(id);
412
413         *c = boot_cpu_data;
414         c->cpu_index = id;
415
416         identify_secondary_cpu(c);
417 }
418
419 /* Routine initially called when a non-boot CPU is brought online */
420 static void __init start_secondary(void *unused)
421 {
422         __u8 cpuid = hard_smp_processor_id();
423
424         cpu_init();
425
426         /* OK, we're in the routine */
427         ack_CPI(VIC_CPU_BOOT_CPI);
428
429         /* setup the 8259 master slave pair belonging to this CPU ---
430          * we won't actually receive any until the boot CPU
431          * relinquishes it's static routing mask */
432         vic_setup_pic();
433
434         qic_setup();
435
436         if (is_cpu_quad() && !is_cpu_vic_boot()) {
437                 /* clear the boot CPI */
438                 __u8 dummy;
439
440                 dummy =
441                     voyager_quad_cpi_addr[cpuid]->qic_cpi[VIC_CPU_BOOT_CPI].cpi;
442                 printk("read dummy %d\n", dummy);
443         }
444
445         /* lower the mask to receive CPIs */
446         vic_enable_cpi();
447
448         VDEBUG(("VOYAGER SMP: CPU%d, stack at about %p\n", cpuid, &cpuid));
449
450         notify_cpu_starting(cpuid);
451
452         /* enable interrupts */
453         local_irq_enable();
454
455         /* get our bogomips */
456         calibrate_delay();
457
458         /* save our processor parameters */
459         smp_store_cpu_info(cpuid);
460
461         /* if we're a quad, we may need to bootstrap other CPUs */
462         do_quad_bootstrap();
463
464         /* FIXME: this is rather a poor hack to prevent the CPU
465          * activating softirqs while it's supposed to be waiting for
466          * permission to proceed.  Without this, the new per CPU stuff
467          * in the softirqs will fail */
468         local_irq_disable();
469         cpu_set(cpuid, cpu_callin_map);
470
471         /* signal that we're done */
472         cpu_booted_map = 1;
473
474         while (!cpu_isset(cpuid, smp_commenced_mask))
475                 rep_nop();
476         local_irq_enable();
477
478         local_flush_tlb();
479
480         cpu_set(cpuid, cpu_online_map);
481         wmb();
482         cpu_idle();
483 }
484
485 /* Routine to kick start the given CPU and wait for it to report ready
486  * (or timeout in startup).  When this routine returns, the requested
487  * CPU is either fully running and configured or known to be dead.
488  *
489  * We call this routine sequentially 1 CPU at a time, so no need for
490  * locking */
491
492 static void __init do_boot_cpu(__u8 cpu)
493 {
494         struct task_struct *idle;
495         int timeout;
496         unsigned long flags;
497         int quad_boot = (1 << cpu) & voyager_quad_processors
498             & ~(voyager_extended_vic_processors
499                 & voyager_allowed_boot_processors);
500
501         /* This is the format of the CPI IDT gate (in real mode) which
502          * we're hijacking to boot the CPU */
503         union IDTFormat {
504                 struct seg {
505                         __u16 Offset;
506                         __u16 Segment;
507                 } idt;
508                 __u32 val;
509         } hijack_source;
510
511         __u32 *hijack_vector;
512         __u32 start_phys_address = setup_trampoline();
513
514         /* There's a clever trick to this: The linux trampoline is
515          * compiled to begin at absolute location zero, so make the
516          * address zero but have the data segment selector compensate
517          * for the actual address */
518         hijack_source.idt.Offset = start_phys_address & 0x000F;
519         hijack_source.idt.Segment = (start_phys_address >> 4) & 0xFFFF;
520
521         cpucount++;
522         alternatives_smp_switch(1);
523
524         idle = fork_idle(cpu);
525         if (IS_ERR(idle))
526                 panic("failed fork for CPU%d", cpu);
527         idle->thread.ip = (unsigned long)start_secondary;
528         /* init_tasks (in sched.c) is indexed logically */
529         stack_start.sp = (void *)idle->thread.sp;
530
531         per_cpu(current_task, cpu) = idle;
532         early_gdt_descr.address = (unsigned long)get_cpu_gdt_table(cpu);
533         irq_ctx_init(cpu);
534
535         /* Note: Don't modify initial ss override */
536         VDEBUG(("VOYAGER SMP: Booting CPU%d at 0x%lx[%x:%x], stack %p\n", cpu,
537                 (unsigned long)hijack_source.val, hijack_source.idt.Segment,
538                 hijack_source.idt.Offset, stack_start.sp));
539
540         /* init lowmem identity mapping */
541         clone_pgd_range(swapper_pg_dir, swapper_pg_dir + KERNEL_PGD_BOUNDARY,
542                         min_t(unsigned long, KERNEL_PGD_PTRS, KERNEL_PGD_BOUNDARY));
543         flush_tlb_all();
544
545         if (quad_boot) {
546                 printk("CPU %d: non extended Quad boot\n", cpu);
547                 hijack_vector =
548                     (__u32 *)
549                     phys_to_virt((VIC_CPU_BOOT_CPI + QIC_DEFAULT_CPI_BASE) * 4);
550                 *hijack_vector = hijack_source.val;
551         } else {
552                 printk("CPU%d: extended VIC boot\n", cpu);
553                 hijack_vector =
554                     (__u32 *)
555                     phys_to_virt((VIC_CPU_BOOT_CPI + VIC_DEFAULT_CPI_BASE) * 4);
556                 *hijack_vector = hijack_source.val;
557                 /* VIC errata, may also receive interrupt at this address */
558                 hijack_vector =
559                     (__u32 *)
560                     phys_to_virt((VIC_CPU_BOOT_ERRATA_CPI +
561                                   VIC_DEFAULT_CPI_BASE) * 4);
562                 *hijack_vector = hijack_source.val;
563         }
564         /* All non-boot CPUs start with interrupts fully masked.  Need
565          * to lower the mask of the CPI we're about to send.  We do
566          * this in the VIC by masquerading as the processor we're
567          * about to boot and lowering its interrupt mask */
568         local_irq_save(flags);
569         if (quad_boot) {
570                 send_one_QIC_CPI(cpu, VIC_CPU_BOOT_CPI);
571         } else {
572                 outb(VIC_CPU_MASQUERADE_ENABLE | cpu, VIC_PROCESSOR_ID);
573                 /* here we're altering registers belonging to `cpu' */
574
575                 outb(VIC_BOOT_INTERRUPT_MASK, 0x21);
576                 /* now go back to our original identity */
577                 outb(boot_cpu_id, VIC_PROCESSOR_ID);
578
579                 /* and boot the CPU */
580
581                 send_CPI((1 << cpu), VIC_CPU_BOOT_CPI);
582         }
583         cpu_booted_map = 0;
584         local_irq_restore(flags);
585
586         /* now wait for it to become ready (or timeout) */
587         for (timeout = 0; timeout < 50000; timeout++) {
588                 if (cpu_booted_map)
589                         break;
590                 udelay(100);
591         }
592         /* reset the page table */
593         zap_low_mappings();
594
595         if (cpu_booted_map) {
596                 VDEBUG(("CPU%d: Booted successfully, back in CPU %d\n",
597                         cpu, smp_processor_id()));
598
599                 printk("CPU%d: ", cpu);
600                 print_cpu_info(&cpu_data(cpu));
601                 wmb();
602                 cpu_set(cpu, cpu_callout_map);
603                 cpu_set(cpu, cpu_present_map);
604         } else {
605                 printk("CPU%d FAILED TO BOOT: ", cpu);
606                 if (*
607                     ((volatile unsigned char *)phys_to_virt(start_phys_address))
608                     == 0xA5)
609                         printk("Stuck.\n");
610                 else
611                         printk("Not responding.\n");
612
613                 cpucount--;
614         }
615 }
616
617 void __init smp_boot_cpus(void)
618 {
619         int i;
620
621         /* CAT BUS initialisation must be done after the memory */
622         /* FIXME: The L4 has a catbus too, it just needs to be
623          * accessed in a totally different way */
624         if (voyager_level == 5) {
625                 voyager_cat_init();
626
627                 /* now that the cat has probed the Voyager System Bus, sanity
628                  * check the cpu map */
629                 if (((voyager_quad_processors | voyager_extended_vic_processors)
630                      & cpus_addr(phys_cpu_present_map)[0]) !=
631                     cpus_addr(phys_cpu_present_map)[0]) {
632                         /* should panic */
633                         printk("\n\n***WARNING*** "
634                                "Sanity check of CPU present map FAILED\n");
635                 }
636         } else if (voyager_level == 4)
637                 voyager_extended_vic_processors =
638                     cpus_addr(phys_cpu_present_map)[0];
639
640         /* this sets up the idle task to run on the current cpu */
641         voyager_extended_cpus = 1;
642         /* Remove the global_irq_holder setting, it triggers a BUG() on
643          * schedule at the moment */
644         //global_irq_holder = boot_cpu_id;
645
646         /* FIXME: Need to do something about this but currently only works
647          * on CPUs with a tsc which none of mine have.
648          smp_tune_scheduling();
649          */
650         smp_store_cpu_info(boot_cpu_id);
651         /* setup the jump vector */
652         initial_code = (unsigned long)initialize_secondary;
653         printk("CPU%d: ", boot_cpu_id);
654         print_cpu_info(&cpu_data(boot_cpu_id));
655
656         if (is_cpu_quad()) {
657                 /* booting on a Quad CPU */
658                 printk("VOYAGER SMP: Boot CPU is Quad\n");
659                 qic_setup();
660                 do_quad_bootstrap();
661         }
662
663         /* enable our own CPIs */
664         vic_enable_cpi();
665
666         cpu_set(boot_cpu_id, cpu_online_map);
667         cpu_set(boot_cpu_id, cpu_callout_map);
668
669         /* loop over all the extended VIC CPUs and boot them.  The
670          * Quad CPUs must be bootstrapped by their extended VIC cpu */
671         for (i = 0; i < nr_cpu_ids; i++) {
672                 if (i == boot_cpu_id || !cpu_isset(i, phys_cpu_present_map))
673                         continue;
674                 do_boot_cpu(i);
675                 /* This udelay seems to be needed for the Quad boots
676                  * don't remove unless you know what you're doing */
677                 udelay(1000);
678         }
679         /* we could compute the total bogomips here, but why bother?,
680          * Code added from smpboot.c */
681         {
682                 unsigned long bogosum = 0;
683
684                 for_each_online_cpu(i)
685                         bogosum += cpu_data(i).loops_per_jiffy;
686                 printk(KERN_INFO "Total of %d processors activated "
687                        "(%lu.%02lu BogoMIPS).\n",
688                        cpucount + 1, bogosum / (500000 / HZ),
689                        (bogosum / (5000 / HZ)) % 100);
690         }
691         voyager_extended_cpus = hweight32(voyager_extended_vic_processors);
692         printk("VOYAGER: Extended (interrupt handling CPUs): "
693                "%d, non-extended: %d\n", voyager_extended_cpus,
694                num_booting_cpus() - voyager_extended_cpus);
695         /* that's it, switch to symmetric mode */
696         outb(0, VIC_PRIORITY_REGISTER);
697         outb(0, VIC_CLAIM_REGISTER_0);
698         outb(0, VIC_CLAIM_REGISTER_1);
699
700         VDEBUG(("VOYAGER SMP: Booted with %d CPUs\n", num_booting_cpus()));
701 }
702
703 /* Reload the secondary CPUs task structure (this function does not
704  * return ) */
705 static void __init initialize_secondary(void)
706 {
707 #if 0
708         // AC kernels only
709         set_current(hard_get_current());
710 #endif
711
712         /*
713          * We don't actually need to load the full TSS,
714          * basically just the stack pointer and the eip.
715          */
716
717         asm volatile ("movl %0,%%esp\n\t"
718                       "jmp *%1"::"r" (current->thread.sp),
719                       "r"(current->thread.ip));
720 }
721
722 /* handle a Voyager SYS_INT -- If we don't, the base board will
723  * panic the system.
724  *
725  * System interrupts occur because some problem was detected on the
726  * various busses.  To find out what you have to probe all the
727  * hardware via the CAT bus.  FIXME: At the moment we do nothing. */
728 void smp_vic_sys_interrupt(struct pt_regs *regs)
729 {
730         ack_CPI(VIC_SYS_INT);
731         printk("Voyager SYSTEM INTERRUPT\n");
732 }
733
734 /* Handle a voyager CMN_INT; These interrupts occur either because of
735  * a system status change or because a single bit memory error
736  * occurred.  FIXME: At the moment, ignore all this. */
737 void smp_vic_cmn_interrupt(struct pt_regs *regs)
738 {
739         static __u8 in_cmn_int = 0;
740         static DEFINE_SPINLOCK(cmn_int_lock);
741
742         /* common ints are broadcast, so make sure we only do this once */
743         _raw_spin_lock(&cmn_int_lock);
744         if (in_cmn_int)
745                 goto unlock_end;
746
747         in_cmn_int++;
748         _raw_spin_unlock(&cmn_int_lock);
749
750         VDEBUG(("Voyager COMMON INTERRUPT\n"));
751
752         if (voyager_level == 5)
753                 voyager_cat_do_common_interrupt();
754
755         _raw_spin_lock(&cmn_int_lock);
756         in_cmn_int = 0;
757       unlock_end:
758         _raw_spin_unlock(&cmn_int_lock);
759         ack_CPI(VIC_CMN_INT);
760 }
761
762 /*
763  * Reschedule call back. Nothing to do, all the work is done
764  * automatically when we return from the interrupt.  */
765 static void smp_reschedule_interrupt(void)
766 {
767         /* do nothing */
768 }
769
770 static struct mm_struct *flush_mm;
771 static unsigned long flush_va;
772 static DEFINE_SPINLOCK(tlbstate_lock);
773
774 /*
775  * We cannot call mmdrop() because we are in interrupt context,
776  * instead update mm->cpu_vm_mask.
777  *
778  * We need to reload %cr3 since the page tables may be going
779  * away from under us..
780  */
781 static inline void voyager_leave_mm(unsigned long cpu)
782 {
783         if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_OK)
784                 BUG();
785         cpu_clear(cpu, per_cpu(cpu_tlbstate, cpu).active_mm->cpu_vm_mask);
786         load_cr3(swapper_pg_dir);
787 }
788
789 /*
790  * Invalidate call-back
791  */
792 static void smp_invalidate_interrupt(void)
793 {
794         __u8 cpu = smp_processor_id();
795
796         if (!test_bit(cpu, &smp_invalidate_needed))
797                 return;
798         /* This will flood messages.  Don't uncomment unless you see
799          * Problems with cross cpu invalidation
800          VDEBUG(("VOYAGER SMP: CPU%d received INVALIDATE_CPI\n",
801          smp_processor_id()));
802          */
803
804         if (flush_mm == per_cpu(cpu_tlbstate, cpu).active_mm) {
805                 if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_OK) {
806                         if (flush_va == TLB_FLUSH_ALL)
807                                 local_flush_tlb();
808                         else
809                                 __flush_tlb_one(flush_va);
810                 } else
811                         voyager_leave_mm(cpu);
812         }
813         smp_mb__before_clear_bit();
814         clear_bit(cpu, &smp_invalidate_needed);
815         smp_mb__after_clear_bit();
816 }
817
818 /* All the new flush operations for 2.4 */
819
820 /* This routine is called with a physical cpu mask */
821 static void
822 voyager_flush_tlb_others(unsigned long cpumask, struct mm_struct *mm,
823                          unsigned long va)
824 {
825         int stuck = 50000;
826
827         if (!cpumask)
828                 BUG();
829         if ((cpumask & cpus_addr(cpu_online_map)[0]) != cpumask)
830                 BUG();
831         if (cpumask & (1 << smp_processor_id()))
832                 BUG();
833         if (!mm)
834                 BUG();
835
836         spin_lock(&tlbstate_lock);
837
838         flush_mm = mm;
839         flush_va = va;
840         atomic_set_mask(cpumask, &smp_invalidate_needed);
841         /*
842          * We have to send the CPI only to
843          * CPUs affected.
844          */
845         send_CPI(cpumask, VIC_INVALIDATE_CPI);
846
847         while (smp_invalidate_needed) {
848                 mb();
849                 if (--stuck == 0) {
850                         printk("***WARNING*** Stuck doing invalidate CPI "
851                                "(CPU%d)\n", smp_processor_id());
852                         break;
853                 }
854         }
855
856         /* Uncomment only to debug invalidation problems
857            VDEBUG(("VOYAGER SMP: Completed invalidate CPI (CPU%d)\n", cpu));
858          */
859
860         flush_mm = NULL;
861         flush_va = 0;
862         spin_unlock(&tlbstate_lock);
863 }
864
865 void flush_tlb_current_task(void)
866 {
867         struct mm_struct *mm = current->mm;
868         unsigned long cpu_mask;
869
870         preempt_disable();
871
872         cpu_mask = cpus_addr(mm->cpu_vm_mask)[0] & ~(1 << smp_processor_id());
873         local_flush_tlb();
874         if (cpu_mask)
875                 voyager_flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
876
877         preempt_enable();
878 }
879
880 void flush_tlb_mm(struct mm_struct *mm)
881 {
882         unsigned long cpu_mask;
883
884         preempt_disable();
885
886         cpu_mask = cpus_addr(mm->cpu_vm_mask)[0] & ~(1 << smp_processor_id());
887
888         if (current->active_mm == mm) {
889                 if (current->mm)
890                         local_flush_tlb();
891                 else
892                         voyager_leave_mm(smp_processor_id());
893         }
894         if (cpu_mask)
895                 voyager_flush_tlb_others(cpu_mask, mm, TLB_FLUSH_ALL);
896
897         preempt_enable();
898 }
899
900 void flush_tlb_page(struct vm_area_struct *vma, unsigned long va)
901 {
902         struct mm_struct *mm = vma->vm_mm;
903         unsigned long cpu_mask;
904
905         preempt_disable();
906
907         cpu_mask = cpus_addr(mm->cpu_vm_mask)[0] & ~(1 << smp_processor_id());
908         if (current->active_mm == mm) {
909                 if (current->mm)
910                         __flush_tlb_one(va);
911                 else
912                         voyager_leave_mm(smp_processor_id());
913         }
914
915         if (cpu_mask)
916                 voyager_flush_tlb_others(cpu_mask, mm, va);
917
918         preempt_enable();
919 }
920
921 EXPORT_SYMBOL(flush_tlb_page);
922
923 /* enable the requested IRQs */
924 static void smp_enable_irq_interrupt(void)
925 {
926         __u8 irq;
927         __u8 cpu = get_cpu();
928
929         VDEBUG(("VOYAGER SMP: CPU%d enabling irq mask 0x%x\n", cpu,
930                 vic_irq_enable_mask[cpu]));
931
932         spin_lock(&vic_irq_lock);
933         for (irq = 0; irq < 16; irq++) {
934                 if (vic_irq_enable_mask[cpu] & (1 << irq))
935                         enable_local_vic_irq(irq);
936         }
937         vic_irq_enable_mask[cpu] = 0;
938         spin_unlock(&vic_irq_lock);
939
940         put_cpu_no_resched();
941 }
942
943 /*
944  *      CPU halt call-back
945  */
946 static void smp_stop_cpu_function(void *dummy)
947 {
948         VDEBUG(("VOYAGER SMP: CPU%d is STOPPING\n", smp_processor_id()));
949         cpu_clear(smp_processor_id(), cpu_online_map);
950         local_irq_disable();
951         for (;;)
952                 halt();
953 }
954
955 /* execute a thread on a new CPU.  The function to be called must be
956  * previously set up.  This is used to schedule a function for
957  * execution on all CPUs - set up the function then broadcast a
958  * function_interrupt CPI to come here on each CPU */
959 static void smp_call_function_interrupt(void)
960 {
961         irq_enter();
962         generic_smp_call_function_interrupt();
963         __get_cpu_var(irq_stat).irq_call_count++;
964         irq_exit();
965 }
966
967 static void smp_call_function_single_interrupt(void)
968 {
969         irq_enter();
970         generic_smp_call_function_single_interrupt();
971         __get_cpu_var(irq_stat).irq_call_count++;
972         irq_exit();
973 }
974
975 /* Sorry about the name.  In an APIC based system, the APICs
976  * themselves are programmed to send a timer interrupt.  This is used
977  * by linux to reschedule the processor.  Voyager doesn't have this,
978  * so we use the system clock to interrupt one processor, which in
979  * turn, broadcasts a timer CPI to all the others --- we receive that
980  * CPI here.  We don't use this actually for counting so losing
981  * ticks doesn't matter
982  *
983  * FIXME: For those CPUs which actually have a local APIC, we could
984  * try to use it to trigger this interrupt instead of having to
985  * broadcast the timer tick.  Unfortunately, all my pentium DYADs have
986  * no local APIC, so I can't do this
987  *
988  * This function is currently a placeholder and is unused in the code */
989 void smp_apic_timer_interrupt(struct pt_regs *regs)
990 {
991         struct pt_regs *old_regs = set_irq_regs(regs);
992         wrapper_smp_local_timer_interrupt();
993         set_irq_regs(old_regs);
994 }
995
996 /* All of the QUAD interrupt GATES */
997 void smp_qic_timer_interrupt(struct pt_regs *regs)
998 {
999         struct pt_regs *old_regs = set_irq_regs(regs);
1000         ack_QIC_CPI(QIC_TIMER_CPI);
1001         wrapper_smp_local_timer_interrupt();
1002         set_irq_regs(old_regs);
1003 }
1004
1005 void smp_qic_invalidate_interrupt(struct pt_regs *regs)
1006 {
1007         ack_QIC_CPI(QIC_INVALIDATE_CPI);
1008         smp_invalidate_interrupt();
1009 }
1010
1011 void smp_qic_reschedule_interrupt(struct pt_regs *regs)
1012 {
1013         ack_QIC_CPI(QIC_RESCHEDULE_CPI);
1014         smp_reschedule_interrupt();
1015 }
1016
1017 void smp_qic_enable_irq_interrupt(struct pt_regs *regs)
1018 {
1019         ack_QIC_CPI(QIC_ENABLE_IRQ_CPI);
1020         smp_enable_irq_interrupt();
1021 }
1022
1023 void smp_qic_call_function_interrupt(struct pt_regs *regs)
1024 {
1025         ack_QIC_CPI(QIC_CALL_FUNCTION_CPI);
1026         smp_call_function_interrupt();
1027 }
1028
1029 void smp_qic_call_function_single_interrupt(struct pt_regs *regs)
1030 {
1031         ack_QIC_CPI(QIC_CALL_FUNCTION_SINGLE_CPI);
1032         smp_call_function_single_interrupt();
1033 }
1034
1035 void smp_vic_cpi_interrupt(struct pt_regs *regs)
1036 {
1037         struct pt_regs *old_regs = set_irq_regs(regs);
1038         __u8 cpu = smp_processor_id();
1039
1040         if (is_cpu_quad())
1041                 ack_QIC_CPI(VIC_CPI_LEVEL0);
1042         else
1043                 ack_VIC_CPI(VIC_CPI_LEVEL0);
1044
1045         if (test_and_clear_bit(VIC_TIMER_CPI, &vic_cpi_mailbox[cpu]))
1046                 wrapper_smp_local_timer_interrupt();
1047         if (test_and_clear_bit(VIC_INVALIDATE_CPI, &vic_cpi_mailbox[cpu]))
1048                 smp_invalidate_interrupt();
1049         if (test_and_clear_bit(VIC_RESCHEDULE_CPI, &vic_cpi_mailbox[cpu]))
1050                 smp_reschedule_interrupt();
1051         if (test_and_clear_bit(VIC_ENABLE_IRQ_CPI, &vic_cpi_mailbox[cpu]))
1052                 smp_enable_irq_interrupt();
1053         if (test_and_clear_bit(VIC_CALL_FUNCTION_CPI, &vic_cpi_mailbox[cpu]))
1054                 smp_call_function_interrupt();
1055         if (test_and_clear_bit(VIC_CALL_FUNCTION_SINGLE_CPI, &vic_cpi_mailbox[cpu]))
1056                 smp_call_function_single_interrupt();
1057         set_irq_regs(old_regs);
1058 }
1059
1060 static void do_flush_tlb_all(void *info)
1061 {
1062         unsigned long cpu = smp_processor_id();
1063
1064         __flush_tlb_all();
1065         if (per_cpu(cpu_tlbstate, cpu).state == TLBSTATE_LAZY)
1066                 voyager_leave_mm(cpu);
1067 }
1068
1069 /* flush the TLB of every active CPU in the system */
1070 void flush_tlb_all(void)
1071 {
1072         on_each_cpu(do_flush_tlb_all, 0, 1);
1073 }
1074
1075 /* send a reschedule CPI to one CPU by physical CPU number*/
1076 static void voyager_smp_send_reschedule(int cpu)
1077 {
1078         send_one_CPI(cpu, VIC_RESCHEDULE_CPI);
1079 }
1080
1081 int hard_smp_processor_id(void)
1082 {
1083         __u8 i;
1084         __u8 cpumask = inb(VIC_PROC_WHO_AM_I);
1085         if ((cpumask & QUAD_IDENTIFIER) == QUAD_IDENTIFIER)
1086                 return cpumask & 0x1F;
1087
1088         for (i = 0; i < 8; i++) {
1089                 if (cpumask & (1 << i))
1090                         return i;
1091         }
1092         printk("** WARNING ** Illegal cpuid returned by VIC: %d", cpumask);
1093         return 0;
1094 }
1095
1096 int safe_smp_processor_id(void)
1097 {
1098         return hard_smp_processor_id();
1099 }
1100
1101 /* broadcast a halt to all other CPUs */
1102 static void voyager_smp_send_stop(void)
1103 {
1104         smp_call_function(smp_stop_cpu_function, NULL, 1);
1105 }
1106
1107 /* this function is triggered in time.c when a clock tick fires
1108  * we need to re-broadcast the tick to all CPUs */
1109 void smp_vic_timer_interrupt(void)
1110 {
1111         send_CPI_allbutself(VIC_TIMER_CPI);
1112         smp_local_timer_interrupt();
1113 }
1114
1115 /* local (per CPU) timer interrupt.  It does both profiling and
1116  * process statistics/rescheduling.
1117  *
1118  * We do profiling in every local tick, statistics/rescheduling
1119  * happen only every 'profiling multiplier' ticks. The default
1120  * multiplier is 1 and it can be changed by writing the new multiplier
1121  * value into /proc/profile.
1122  */
1123 void smp_local_timer_interrupt(void)
1124 {
1125         int cpu = smp_processor_id();
1126         long weight;
1127
1128         profile_tick(CPU_PROFILING);
1129         if (--per_cpu(prof_counter, cpu) <= 0) {
1130                 /*
1131                  * The multiplier may have changed since the last time we got
1132                  * to this point as a result of the user writing to
1133                  * /proc/profile. In this case we need to adjust the APIC
1134                  * timer accordingly.
1135                  *
1136                  * Interrupts are already masked off at this point.
1137                  */
1138                 per_cpu(prof_counter, cpu) = per_cpu(prof_multiplier, cpu);
1139                 if (per_cpu(prof_counter, cpu) !=
1140                     per_cpu(prof_old_multiplier, cpu)) {
1141                         /* FIXME: need to update the vic timer tick here */
1142                         per_cpu(prof_old_multiplier, cpu) =
1143                             per_cpu(prof_counter, cpu);
1144                 }
1145
1146                 update_process_times(user_mode_vm(get_irq_regs()));
1147         }
1148
1149         if (((1 << cpu) & voyager_extended_vic_processors) == 0)
1150                 /* only extended VIC processors participate in
1151                  * interrupt distribution */
1152                 return;
1153
1154         /*
1155          * We take the 'long' return path, and there every subsystem
1156          * grabs the appropriate locks (kernel lock/ irq lock).
1157          *
1158          * we might want to decouple profiling from the 'long path',
1159          * and do the profiling totally in assembly.
1160          *
1161          * Currently this isn't too much of an issue (performance wise),
1162          * we can take more than 100K local irqs per second on a 100 MHz P5.
1163          */
1164
1165         if ((++vic_tick[cpu] & 0x7) != 0)
1166                 return;
1167         /* get here every 16 ticks (about every 1/6 of a second) */
1168
1169         /* Change our priority to give someone else a chance at getting
1170          * the IRQ. The algorithm goes like this:
1171          *
1172          * In the VIC, the dynamically routed interrupt is always
1173          * handled by the lowest priority eligible (i.e. receiving
1174          * interrupts) CPU.  If >1 eligible CPUs are equal lowest, the
1175          * lowest processor number gets it.
1176          *
1177          * The priority of a CPU is controlled by a special per-CPU
1178          * VIC priority register which is 3 bits wide 0 being lowest
1179          * and 7 highest priority..
1180          *
1181          * Therefore we subtract the average number of interrupts from
1182          * the number we've fielded.  If this number is negative, we
1183          * lower the activity count and if it is positive, we raise
1184          * it.
1185          *
1186          * I'm afraid this still leads to odd looking interrupt counts:
1187          * the totals are all roughly equal, but the individual ones
1188          * look rather skewed.
1189          *
1190          * FIXME: This algorithm is total crap when mixed with SMP
1191          * affinity code since we now try to even up the interrupt
1192          * counts when an affinity binding is keeping them on a
1193          * particular CPU*/
1194         weight = (vic_intr_count[cpu] * voyager_extended_cpus
1195                   - vic_intr_total) >> 4;
1196         weight += 4;
1197         if (weight > 7)
1198                 weight = 7;
1199         if (weight < 0)
1200                 weight = 0;
1201
1202         outb((__u8) weight, VIC_PRIORITY_REGISTER);
1203
1204 #ifdef VOYAGER_DEBUG
1205         if ((vic_tick[cpu] & 0xFFF) == 0) {
1206                 /* print this message roughly every 25 secs */
1207                 printk("VOYAGER SMP: vic_tick[%d] = %lu, weight = %ld\n",
1208                        cpu, vic_tick[cpu], weight);
1209         }
1210 #endif
1211 }
1212
1213 /* setup the profiling timer */
1214 int setup_profiling_timer(unsigned int multiplier)
1215 {
1216         int i;
1217
1218         if ((!multiplier))
1219                 return -EINVAL;
1220
1221         /*
1222          * Set the new multiplier for each CPU. CPUs don't start using the
1223          * new values until the next timer interrupt in which they do process
1224          * accounting.
1225          */
1226         for (i = 0; i < nr_cpu_ids; ++i)
1227                 per_cpu(prof_multiplier, i) = multiplier;
1228
1229         return 0;
1230 }
1231
1232 /* This is a bit of a mess, but forced on us by the genirq changes
1233  * there's no genirq handler that really does what voyager wants
1234  * so hack it up with the simple IRQ handler */
1235 static void handle_vic_irq(unsigned int irq, struct irq_desc *desc)
1236 {
1237         before_handle_vic_irq(irq);
1238         handle_simple_irq(irq, desc);
1239         after_handle_vic_irq(irq);
1240 }
1241
1242 /*  The CPIs are handled in the per cpu 8259s, so they must be
1243  *  enabled to be received: FIX: enabling the CPIs in the early
1244  *  boot sequence interferes with bug checking; enable them later
1245  *  on in smp_init */
1246 #define VIC_SET_GATE(cpi, vector) \
1247         set_intr_gate((cpi) + VIC_DEFAULT_CPI_BASE, (vector))
1248 #define QIC_SET_GATE(cpi, vector) \
1249         set_intr_gate((cpi) + QIC_DEFAULT_CPI_BASE, (vector))
1250
1251 void __init voyager_smp_intr_init(void)
1252 {
1253         int i;
1254
1255         /* initialize the per cpu irq mask to all disabled */
1256         for (i = 0; i < nr_cpu_ids; i++)
1257                 vic_irq_mask[i] = 0xFFFF;
1258
1259         VIC_SET_GATE(VIC_CPI_LEVEL0, vic_cpi_interrupt);
1260
1261         VIC_SET_GATE(VIC_SYS_INT, vic_sys_interrupt);
1262         VIC_SET_GATE(VIC_CMN_INT, vic_cmn_interrupt);
1263
1264         QIC_SET_GATE(QIC_TIMER_CPI, qic_timer_interrupt);
1265         QIC_SET_GATE(QIC_INVALIDATE_CPI, qic_invalidate_interrupt);
1266         QIC_SET_GATE(QIC_RESCHEDULE_CPI, qic_reschedule_interrupt);
1267         QIC_SET_GATE(QIC_ENABLE_IRQ_CPI, qic_enable_irq_interrupt);
1268         QIC_SET_GATE(QIC_CALL_FUNCTION_CPI, qic_call_function_interrupt);
1269
1270         /* now put the VIC descriptor into the first 48 IRQs
1271          *
1272          * This is for later: first 16 correspond to PC IRQs; next 16
1273          * are Primary MC IRQs and final 16 are Secondary MC IRQs */
1274         for (i = 0; i < 48; i++)
1275                 set_irq_chip_and_handler(i, &vic_chip, handle_vic_irq);
1276 }
1277
1278 /* send a CPI at level cpi to a set of cpus in cpuset (set 1 bit per
1279  * processor to receive CPI */
1280 static void send_CPI(__u32 cpuset, __u8 cpi)
1281 {
1282         int cpu;
1283         __u32 quad_cpuset = (cpuset & voyager_quad_processors);
1284
1285         if (cpi < VIC_START_FAKE_CPI) {
1286                 /* fake CPI are only used for booting, so send to the
1287                  * extended quads as well---Quads must be VIC booted */
1288                 outb((__u8) (cpuset), VIC_CPI_Registers[cpi]);
1289                 return;
1290         }
1291         if (quad_cpuset)
1292                 send_QIC_CPI(quad_cpuset, cpi);
1293         cpuset &= ~quad_cpuset;
1294         cpuset &= 0xff;         /* only first 8 CPUs vaild for VIC CPI */
1295         if (cpuset == 0)
1296                 return;
1297         for_each_online_cpu(cpu) {
1298                 if (cpuset & (1 << cpu))
1299                         set_bit(cpi, &vic_cpi_mailbox[cpu]);
1300         }
1301         if (cpuset)
1302                 outb((__u8) cpuset, VIC_CPI_Registers[VIC_CPI_LEVEL0]);
1303 }
1304
1305 /* Acknowledge receipt of CPI in the QIC, clear in QIC hardware and
1306  * set the cache line to shared by reading it.
1307  *
1308  * DON'T make this inline otherwise the cache line read will be
1309  * optimised away
1310  * */
1311 static int ack_QIC_CPI(__u8 cpi)
1312 {
1313         __u8 cpu = hard_smp_processor_id();
1314
1315         cpi &= 7;
1316
1317         outb(1 << cpi, QIC_INTERRUPT_CLEAR1);
1318         return voyager_quad_cpi_addr[cpu]->qic_cpi[cpi].cpi;
1319 }
1320
1321 static void ack_special_QIC_CPI(__u8 cpi)
1322 {
1323         switch (cpi) {
1324         case VIC_CMN_INT:
1325                 outb(QIC_CMN_INT, QIC_INTERRUPT_CLEAR0);
1326                 break;
1327         case VIC_SYS_INT:
1328                 outb(QIC_SYS_INT, QIC_INTERRUPT_CLEAR0);
1329                 break;
1330         }
1331         /* also clear at the VIC, just in case (nop for non-extended proc) */
1332         ack_VIC_CPI(cpi);
1333 }
1334
1335 /* Acknowledge receipt of CPI in the VIC (essentially an EOI) */
1336 static void ack_VIC_CPI(__u8 cpi)
1337 {
1338 #ifdef VOYAGER_DEBUG
1339         unsigned long flags;
1340         __u16 isr;
1341         __u8 cpu = smp_processor_id();
1342
1343         local_irq_save(flags);
1344         isr = vic_read_isr();
1345         if ((isr & (1 << (cpi & 7))) == 0) {
1346                 printk("VOYAGER SMP: CPU%d lost CPI%d\n", cpu, cpi);
1347         }
1348 #endif
1349         /* send specific EOI; the two system interrupts have
1350          * bit 4 set for a separate vector but behave as the
1351          * corresponding 3 bit intr */
1352         outb_p(0x60 | (cpi & 7), 0x20);
1353
1354 #ifdef VOYAGER_DEBUG
1355         if ((vic_read_isr() & (1 << (cpi & 7))) != 0) {
1356                 printk("VOYAGER SMP: CPU%d still asserting CPI%d\n", cpu, cpi);
1357         }
1358         local_irq_restore(flags);
1359 #endif
1360 }
1361
1362 /* cribbed with thanks from irq.c */
1363 #define __byte(x,y)     (((unsigned char *)&(y))[x])
1364 #define cached_21(cpu)  (__byte(0,vic_irq_mask[cpu]))
1365 #define cached_A1(cpu)  (__byte(1,vic_irq_mask[cpu]))
1366
1367 static unsigned int startup_vic_irq(unsigned int irq)
1368 {
1369         unmask_vic_irq(irq);
1370
1371         return 0;
1372 }
1373
1374 /* The enable and disable routines.  This is where we run into
1375  * conflicting architectural philosophy.  Fundamentally, the voyager
1376  * architecture does not expect to have to disable interrupts globally
1377  * (the IRQ controllers belong to each CPU).  The processor masquerade
1378  * which is used to start the system shouldn't be used in a running OS
1379  * since it will cause great confusion if two separate CPUs drive to
1380  * the same IRQ controller (I know, I've tried it).
1381  *
1382  * The solution is a variant on the NCR lazy SPL design:
1383  *
1384  * 1) To disable an interrupt, do nothing (other than set the
1385  *    IRQ_DISABLED flag).  This dares the interrupt actually to arrive.
1386  *
1387  * 2) If the interrupt dares to come in, raise the local mask against
1388  *    it (this will result in all the CPU masks being raised
1389  *    eventually).
1390  *
1391  * 3) To enable the interrupt, lower the mask on the local CPU and
1392  *    broadcast an Interrupt enable CPI which causes all other CPUs to
1393  *    adjust their masks accordingly.  */
1394
1395 static void unmask_vic_irq(unsigned int irq)
1396 {
1397         /* linux doesn't to processor-irq affinity, so enable on
1398          * all CPUs we know about */
1399         int cpu = smp_processor_id(), real_cpu;
1400         __u16 mask = (1 << irq);
1401         __u32 processorList = 0;
1402         unsigned long flags;
1403
1404         VDEBUG(("VOYAGER: unmask_vic_irq(%d) CPU%d affinity 0x%lx\n",
1405                 irq, cpu, cpu_irq_affinity[cpu]));
1406         spin_lock_irqsave(&vic_irq_lock, flags);
1407         for_each_online_cpu(real_cpu) {
1408                 if (!(voyager_extended_vic_processors & (1 << real_cpu)))
1409                         continue;
1410                 if (!(cpu_irq_affinity[real_cpu] & mask)) {
1411                         /* irq has no affinity for this CPU, ignore */
1412                         continue;
1413                 }
1414                 if (real_cpu == cpu) {
1415                         enable_local_vic_irq(irq);
1416                 } else if (vic_irq_mask[real_cpu] & mask) {
1417                         vic_irq_enable_mask[real_cpu] |= mask;
1418                         processorList |= (1 << real_cpu);
1419                 }
1420         }
1421         spin_unlock_irqrestore(&vic_irq_lock, flags);
1422         if (processorList)
1423                 send_CPI(processorList, VIC_ENABLE_IRQ_CPI);
1424 }
1425
1426 static void mask_vic_irq(unsigned int irq)
1427 {
1428         /* lazy disable, do nothing */
1429 }
1430
1431 static void enable_local_vic_irq(unsigned int irq)
1432 {
1433         __u8 cpu = smp_processor_id();
1434         __u16 mask = ~(1 << irq);
1435         __u16 old_mask = vic_irq_mask[cpu];
1436
1437         vic_irq_mask[cpu] &= mask;
1438         if (vic_irq_mask[cpu] == old_mask)
1439                 return;
1440
1441         VDEBUG(("VOYAGER DEBUG: Enabling irq %d in hardware on CPU %d\n",
1442                 irq, cpu));
1443
1444         if (irq & 8) {
1445                 outb_p(cached_A1(cpu), 0xA1);
1446                 (void)inb_p(0xA1);
1447         } else {
1448                 outb_p(cached_21(cpu), 0x21);
1449                 (void)inb_p(0x21);
1450         }
1451 }
1452
1453 static void disable_local_vic_irq(unsigned int irq)
1454 {
1455         __u8 cpu = smp_processor_id();
1456         __u16 mask = (1 << irq);
1457         __u16 old_mask = vic_irq_mask[cpu];
1458
1459         if (irq == 7)
1460                 return;
1461
1462         vic_irq_mask[cpu] |= mask;
1463         if (old_mask == vic_irq_mask[cpu])
1464                 return;
1465
1466         VDEBUG(("VOYAGER DEBUG: Disabling irq %d in hardware on CPU %d\n",
1467                 irq, cpu));
1468
1469         if (irq & 8) {
1470                 outb_p(cached_A1(cpu), 0xA1);
1471                 (void)inb_p(0xA1);
1472         } else {
1473                 outb_p(cached_21(cpu), 0x21);
1474                 (void)inb_p(0x21);
1475         }
1476 }
1477
1478 /* The VIC is level triggered, so the ack can only be issued after the
1479  * interrupt completes.  However, we do Voyager lazy interrupt
1480  * handling here: It is an extremely expensive operation to mask an
1481  * interrupt in the vic, so we merely set a flag (IRQ_DISABLED).  If
1482  * this interrupt actually comes in, then we mask and ack here to push
1483  * the interrupt off to another CPU */
1484 static void before_handle_vic_irq(unsigned int irq)
1485 {
1486         irq_desc_t *desc = irq_to_desc(irq);
1487         __u8 cpu = smp_processor_id();
1488
1489         _raw_spin_lock(&vic_irq_lock);
1490         vic_intr_total++;
1491         vic_intr_count[cpu]++;
1492
1493         if (!(cpu_irq_affinity[cpu] & (1 << irq))) {
1494                 /* The irq is not in our affinity mask, push it off
1495                  * onto another CPU */
1496                 VDEBUG(("VOYAGER DEBUG: affinity triggered disable of irq %d "
1497                         "on cpu %d\n", irq, cpu));
1498                 disable_local_vic_irq(irq);
1499                 /* set IRQ_INPROGRESS to prevent the handler in irq.c from
1500                  * actually calling the interrupt routine */
1501                 desc->status |= IRQ_REPLAY | IRQ_INPROGRESS;
1502         } else if (desc->status & IRQ_DISABLED) {
1503                 /* Damn, the interrupt actually arrived, do the lazy
1504                  * disable thing. The interrupt routine in irq.c will
1505                  * not handle a IRQ_DISABLED interrupt, so nothing more
1506                  * need be done here */
1507                 VDEBUG(("VOYAGER DEBUG: lazy disable of irq %d on CPU %d\n",
1508                         irq, cpu));
1509                 disable_local_vic_irq(irq);
1510                 desc->status |= IRQ_REPLAY;
1511         } else {
1512                 desc->status &= ~IRQ_REPLAY;
1513         }
1514
1515         _raw_spin_unlock(&vic_irq_lock);
1516 }
1517
1518 /* Finish the VIC interrupt: basically mask */
1519 static void after_handle_vic_irq(unsigned int irq)
1520 {
1521         irq_desc_t *desc = irq_to_desc(irq);
1522
1523         _raw_spin_lock(&vic_irq_lock);
1524         {
1525                 unsigned int status = desc->status & ~IRQ_INPROGRESS;
1526 #ifdef VOYAGER_DEBUG
1527                 __u16 isr;
1528 #endif
1529
1530                 desc->status = status;
1531                 if ((status & IRQ_DISABLED))
1532                         disable_local_vic_irq(irq);
1533 #ifdef VOYAGER_DEBUG
1534                 /* DEBUG: before we ack, check what's in progress */
1535                 isr = vic_read_isr();
1536                 if ((isr & (1 << irq) && !(status & IRQ_REPLAY)) == 0) {
1537                         int i;
1538                         __u8 cpu = smp_processor_id();
1539                         __u8 real_cpu;
1540                         int mask;       /* Um... initialize me??? --RR */
1541
1542                         printk("VOYAGER SMP: CPU%d lost interrupt %d\n",
1543                                cpu, irq);
1544                         for_each_possible_cpu(real_cpu, mask) {
1545
1546                                 outb(VIC_CPU_MASQUERADE_ENABLE | real_cpu,
1547                                      VIC_PROCESSOR_ID);
1548                                 isr = vic_read_isr();
1549                                 if (isr & (1 << irq)) {
1550                                         printk
1551                                             ("VOYAGER SMP: CPU%d ack irq %d\n",
1552                                              real_cpu, irq);
1553                                         ack_vic_irq(irq);
1554                                 }
1555                                 outb(cpu, VIC_PROCESSOR_ID);
1556                         }
1557                 }
1558 #endif /* VOYAGER_DEBUG */
1559                 /* as soon as we ack, the interrupt is eligible for
1560                  * receipt by another CPU so everything must be in
1561                  * order here  */
1562                 ack_vic_irq(irq);
1563                 if (status & IRQ_REPLAY) {
1564                         /* replay is set if we disable the interrupt
1565                          * in the before_handle_vic_irq() routine, so
1566                          * clear the in progress bit here to allow the
1567                          * next CPU to handle this correctly */
1568                         desc->status &= ~(IRQ_REPLAY | IRQ_INPROGRESS);
1569                 }
1570 #ifdef VOYAGER_DEBUG
1571                 isr = vic_read_isr();
1572                 if ((isr & (1 << irq)) != 0)
1573                         printk("VOYAGER SMP: after_handle_vic_irq() after "
1574                                "ack irq=%d, isr=0x%x\n", irq, isr);
1575 #endif /* VOYAGER_DEBUG */
1576         }
1577         _raw_spin_unlock(&vic_irq_lock);
1578
1579         /* All code after this point is out of the main path - the IRQ
1580          * may be intercepted by another CPU if reasserted */
1581 }
1582
1583 /* Linux processor - interrupt affinity manipulations.
1584  *
1585  * For each processor, we maintain a 32 bit irq affinity mask.
1586  * Initially it is set to all 1's so every processor accepts every
1587  * interrupt.  In this call, we change the processor's affinity mask:
1588  *
1589  * Change from enable to disable:
1590  *
1591  * If the interrupt ever comes in to the processor, we will disable it
1592  * and ack it to push it off to another CPU, so just accept the mask here.
1593  *
1594  * Change from disable to enable:
1595  *
1596  * change the mask and then do an interrupt enable CPI to re-enable on
1597  * the selected processors */
1598
1599 void set_vic_irq_affinity(unsigned int irq, const struct cpumask *mask)
1600 {
1601         /* Only extended processors handle interrupts */
1602         unsigned long real_mask;
1603         unsigned long irq_mask = 1 << irq;
1604         int cpu;
1605
1606         real_mask = cpus_addr(*mask)[0] & voyager_extended_vic_processors;
1607
1608         if (cpus_addr(*mask)[0] == 0)
1609                 /* can't have no CPUs to accept the interrupt -- extremely
1610                  * bad things will happen */
1611                 return;
1612
1613         if (irq == 0)
1614                 /* can't change the affinity of the timer IRQ.  This
1615                  * is due to the constraint in the voyager
1616                  * architecture that the CPI also comes in on and IRQ
1617                  * line and we have chosen IRQ0 for this.  If you
1618                  * raise the mask on this interrupt, the processor
1619                  * will no-longer be able to accept VIC CPIs */
1620                 return;
1621
1622         if (irq >= 32)
1623                 /* You can only have 32 interrupts in a voyager system
1624                  * (and 32 only if you have a secondary microchannel
1625                  * bus) */
1626                 return;
1627
1628         for_each_online_cpu(cpu) {
1629                 unsigned long cpu_mask = 1 << cpu;
1630
1631                 if (cpu_mask & real_mask) {
1632                         /* enable the interrupt for this cpu */
1633                         cpu_irq_affinity[cpu] |= irq_mask;
1634                 } else {
1635                         /* disable the interrupt for this cpu */
1636                         cpu_irq_affinity[cpu] &= ~irq_mask;
1637                 }
1638         }
1639         /* this is magic, we now have the correct affinity maps, so
1640          * enable the interrupt.  This will send an enable CPI to
1641          * those CPUs who need to enable it in their local masks,
1642          * causing them to correct for the new affinity . If the
1643          * interrupt is currently globally disabled, it will simply be
1644          * disabled again as it comes in (voyager lazy disable).  If
1645          * the affinity map is tightened to disable the interrupt on a
1646          * cpu, it will be pushed off when it comes in */
1647         unmask_vic_irq(irq);
1648 }
1649
1650 static void ack_vic_irq(unsigned int irq)
1651 {
1652         if (irq & 8) {
1653                 outb(0x62, 0x20);       /* Specific EOI to cascade */
1654                 outb(0x60 | (irq & 7), 0xA0);
1655         } else {
1656                 outb(0x60 | (irq & 7), 0x20);
1657         }
1658 }
1659
1660 /* enable the CPIs.  In the VIC, the CPIs are delivered by the 8259
1661  * but are not vectored by it.  This means that the 8259 mask must be
1662  * lowered to receive them */
1663 static __init void vic_enable_cpi(void)
1664 {
1665         __u8 cpu = smp_processor_id();
1666
1667         /* just take a copy of the current mask (nop for boot cpu) */
1668         vic_irq_mask[cpu] = vic_irq_mask[boot_cpu_id];
1669
1670         enable_local_vic_irq(VIC_CPI_LEVEL0);
1671         enable_local_vic_irq(VIC_CPI_LEVEL1);
1672         /* for sys int and cmn int */
1673         enable_local_vic_irq(7);
1674
1675         if (is_cpu_quad()) {
1676                 outb(QIC_DEFAULT_MASK0, QIC_MASK_REGISTER0);
1677                 outb(QIC_CPI_ENABLE, QIC_MASK_REGISTER1);
1678                 VDEBUG(("VOYAGER SMP: QIC ENABLE CPI: CPU%d: MASK 0x%x\n",
1679                         cpu, QIC_CPI_ENABLE));
1680         }
1681
1682         VDEBUG(("VOYAGER SMP: ENABLE CPI: CPU%d: MASK 0x%x\n",
1683                 cpu, vic_irq_mask[cpu]));
1684 }
1685
1686 void voyager_smp_dump()
1687 {
1688         int old_cpu = smp_processor_id(), cpu;
1689
1690         /* dump the interrupt masks of each processor */
1691         for_each_online_cpu(cpu) {
1692                 __u16 imr, isr, irr;
1693                 unsigned long flags;
1694
1695                 local_irq_save(flags);
1696                 outb(VIC_CPU_MASQUERADE_ENABLE | cpu, VIC_PROCESSOR_ID);
1697                 imr = (inb(0xa1) << 8) | inb(0x21);
1698                 outb(0x0a, 0xa0);
1699                 irr = inb(0xa0) << 8;
1700                 outb(0x0a, 0x20);
1701                 irr |= inb(0x20);
1702                 outb(0x0b, 0xa0);
1703                 isr = inb(0xa0) << 8;
1704                 outb(0x0b, 0x20);
1705                 isr |= inb(0x20);
1706                 outb(old_cpu, VIC_PROCESSOR_ID);
1707                 local_irq_restore(flags);
1708                 printk("\tCPU%d: mask=0x%x, IMR=0x%x, IRR=0x%x, ISR=0x%x\n",
1709                        cpu, vic_irq_mask[cpu], imr, irr, isr);
1710 #if 0
1711                 /* These lines are put in to try to unstick an un ack'd irq */
1712                 if (isr != 0) {
1713                         int irq;
1714                         for (irq = 0; irq < 16; irq++) {
1715                                 if (isr & (1 << irq)) {
1716                                         printk("\tCPU%d: ack irq %d\n",
1717                                                cpu, irq);
1718                                         local_irq_save(flags);
1719                                         outb(VIC_CPU_MASQUERADE_ENABLE | cpu,
1720                                              VIC_PROCESSOR_ID);
1721                                         ack_vic_irq(irq);
1722                                         outb(old_cpu, VIC_PROCESSOR_ID);
1723                                         local_irq_restore(flags);
1724                                 }
1725                         }
1726                 }
1727 #endif
1728         }
1729 }
1730
1731 void smp_voyager_power_off(void *dummy)
1732 {
1733         if (smp_processor_id() == boot_cpu_id)
1734                 voyager_power_off();
1735         else
1736                 smp_stop_cpu_function(NULL);
1737 }
1738
1739 static void __init voyager_smp_prepare_cpus(unsigned int max_cpus)
1740 {
1741         /* FIXME: ignore max_cpus for now */
1742         smp_boot_cpus();
1743 }
1744
1745 static void __cpuinit voyager_smp_prepare_boot_cpu(void)
1746 {
1747         int cpu = smp_processor_id();
1748         switch_to_new_gdt(cpu);
1749
1750         cpu_set(cpu, cpu_online_map);
1751         cpu_set(cpu, cpu_callout_map);
1752         cpu_set(cpu, cpu_possible_map);
1753         cpu_set(cpu, cpu_present_map);
1754
1755 }
1756
1757 static int __cpuinit voyager_cpu_up(unsigned int cpu)
1758 {
1759         /* This only works at boot for x86.  See "rewrite" above. */
1760         if (cpu_isset(cpu, smp_commenced_mask))
1761                 return -ENOSYS;
1762
1763         /* In case one didn't come up */
1764         if (!cpu_isset(cpu, cpu_callin_map))
1765                 return -EIO;
1766         /* Unleash the CPU! */
1767         cpu_set(cpu, smp_commenced_mask);
1768         while (!cpu_online(cpu))
1769                 mb();
1770         return 0;
1771 }
1772
1773 static void __init voyager_smp_cpus_done(unsigned int max_cpus)
1774 {
1775         zap_low_mappings();
1776 }
1777
1778 void __init smp_setup_processor_id(void)
1779 {
1780         current_thread_info()->cpu = hard_smp_processor_id();
1781 }
1782
1783 static void voyager_send_call_func(const struct cpumask *callmask)
1784 {
1785         __u32 mask = cpus_addr(*callmask)[0] & ~(1 << smp_processor_id());
1786         send_CPI(mask, VIC_CALL_FUNCTION_CPI);
1787 }
1788
1789 static void voyager_send_call_func_single(int cpu)
1790 {
1791         send_CPI(1 << cpu, VIC_CALL_FUNCTION_SINGLE_CPI);
1792 }
1793
1794 struct smp_ops smp_ops = {
1795         .smp_prepare_boot_cpu = voyager_smp_prepare_boot_cpu,
1796         .smp_prepare_cpus = voyager_smp_prepare_cpus,
1797         .cpu_up = voyager_cpu_up,
1798         .smp_cpus_done = voyager_smp_cpus_done,
1799
1800         .smp_send_stop = voyager_smp_send_stop,
1801         .smp_send_reschedule = voyager_smp_send_reschedule,
1802
1803         .send_call_func_ipi = voyager_send_call_func,
1804         .send_call_func_single_ipi = voyager_send_call_func_single,
1805 };