Import 2.1.116pre2
[davej-history.git] / arch / i386 / kernel / smp.c
blobd230574ed6a8b30639df5dfe7cb68fc8e4b02621
1 /*
2 * Intel MP v1.1/v1.4 specification support routines for multi-pentium
3 * hosts.
5 * (c) 1995 Alan Cox, CymruNET Ltd <alan@cymru.net>
6 * Supported by Caldera http://www.caldera.com.
7 * Much of the core SMP work is based on previous work by Thomas Radke, to
8 * whom a great many thanks are extended.
10 * Thanks to Intel for making available several different Pentium and
11 * Pentium Pro MP machines.
13 * This code is released under the GNU public license version 2 or
14 * later.
16 * Fixes
17 * Felix Koop : NR_CPUS used properly
18 * Jose Renau : Handle single CPU case.
19 * Alan Cox : By repeated request 8) - Total BogoMIP report.
20 * Greg Wright : Fix for kernel stacks panic.
21 * Erich Boleyn : MP v1.4 and additional changes.
22 * Matthias Sattler : Changes for 2.1 kernel map.
23 * Michel Lespinasse : Changes for 2.1 kernel map.
24 * Michael Chastain : Change trampoline.S to gnu as.
25 * Alan Cox : Dumb bug: 'B' step PPro's are fine
26 * Ingo Molnar : Added APIC timers, based on code
27 * from Jose Renau
28 * Alan Cox : Added EBDA scanning
31 #include <linux/config.h>
32 #include <linux/kernel.h>
33 #include <linux/string.h>
34 #include <linux/timer.h>
35 #include <linux/sched.h>
36 #include <linux/mm.h>
37 #include <linux/kernel_stat.h>
38 #include <linux/delay.h>
39 #include <linux/mc146818rtc.h>
40 #include <asm/i82489.h>
41 #include <linux/smp.h>
42 #include <linux/smp_lock.h>
43 #include <linux/interrupt.h>
44 #include <linux/init.h>
45 #include <asm/pgtable.h>
46 #include <asm/bitops.h>
47 #include <asm/pgtable.h>
48 #include <asm/smp.h>
49 #include <asm/io.h>
51 #ifdef CONFIG_MTRR
52 # include <asm/mtrr.h>
53 #endif
55 #define __KERNEL_SYSCALLS__
56 #include <linux/unistd.h>
58 #include "irq.h"
60 extern unsigned long start_kernel, _etext;
61 extern void update_one_process( struct task_struct *p,
62 unsigned long ticks, unsigned long user,
63 unsigned long system, int cpu);
65 * Some notes on processor bugs:
67 * Pentium and Pentium Pro (and all CPUs) have bugs. The Linux issues
68 * for SMP are handled as follows.
70 * Pentium Pro
71 * Occasional delivery of 'spurious interrupt' as trap #16. This
72 * is very rare. The kernel logs the event and recovers
74 * Pentium
75 * There is a marginal case where REP MOVS on 100MHz SMP
76 * machines with B stepping processors can fail. XXX should provide
77 * an L1cache=Writethrough or L1cache=off option.
79 * B stepping CPUs may hang. There are hardware work arounds
80 * for this. We warn about it in case your board doesnt have the work
81 * arounds. Basically thats so I can tell anyone with a B stepping
82 * CPU and SMP problems "tough".
84 * Specific items [From Pentium Processor Specification Update]
86 * 1AP. Linux doesn't use remote read
87 * 2AP. Linux doesn't trust APIC errors
88 * 3AP. We work around this
89 * 4AP. Linux never generated 3 interrupts of the same priority
90 * to cause a lost local interrupt.
91 * 5AP. Remote read is never used
92 * 9AP. XXX NEED TO CHECK WE HANDLE THIS XXX
93 * 10AP. XXX NEED TO CHECK WE HANDLE THIS XXX
94 * 11AP. Linux reads the APIC between writes to avoid this, as per
95 * the documentation. Make sure you preserve this as it affects
96 * the C stepping chips too.
98 * If this sounds worrying believe me these bugs are ___RARE___ and
99 * there's about nothing of note with C stepping upwards.
103 /* Kernel spinlock */
104 spinlock_t kernel_flag = SPIN_LOCK_UNLOCKED;
107 * Why isn't this somewhere standard ??
109 * Maybe because this procedure is horribly buggy, and does
110 * not deserve to live. Think about signedness issues for five
111 * seconds to see why. - Linus
114 extern __inline int max(int a,int b)
116 if(a>b)
117 return a;
118 return b;
121 static int smp_b_stepping = 0; /* Set if we find a B stepping CPU */
123 static int max_cpus = -1; /* Setup configured maximum number of CPUs to activate */
124 int smp_found_config=0; /* Have we found an SMP box */
126 unsigned long cpu_present_map = 0; /* Bitmask of existing CPUs */
127 int smp_num_cpus = 1; /* Total count of live CPUs */
128 int smp_threads_ready=0; /* Set when the idlers are all forked */
129 volatile int cpu_number_map[NR_CPUS]; /* which CPU maps to which logical number */
130 volatile int __cpu_logical_map[NR_CPUS]; /* which logical number maps to which CPU */
131 volatile unsigned long cpu_callin_map[NR_CPUS] = {0,}; /* We always use 0 the rest is ready for parallel delivery */
132 volatile unsigned long smp_invalidate_needed; /* Used for the invalidate map that's also checked in the spinlock */
133 volatile unsigned long kstack_ptr; /* Stack vector for booting CPUs */
134 struct cpuinfo_x86 cpu_data[NR_CPUS]; /* Per CPU bogomips and other parameters */
135 static unsigned int num_processors = 1; /* Internal processor count */
136 unsigned long mp_ioapic_addr = 0xFEC00000; /* Address of the I/O apic (not yet used) */
137 unsigned char boot_cpu_id = 0; /* Processor that is doing the boot up */
138 static int smp_activated = 0; /* Tripped once we need to start cross invalidating */
139 int apic_version[NR_CPUS]; /* APIC version number */
140 static volatile int smp_commenced=0; /* Tripped when we start scheduling */
141 unsigned long apic_retval; /* Just debugging the assembler.. */
143 static volatile unsigned char smp_cpu_in_msg[NR_CPUS]; /* True if this processor is sending an IPI */
145 volatile unsigned long kernel_counter=0; /* Number of times the processor holds the lock */
146 volatile unsigned long syscall_count=0; /* Number of times the processor holds the syscall lock */
148 volatile unsigned long ipi_count; /* Number of IPIs delivered */
150 volatile unsigned long smp_proc_in_lock[NR_CPUS] = {0,};/* for computing process time */
151 volatile int smp_process_available=0;
153 const char lk_lockmsg[] = "lock from interrupt context at %p\n";
155 int mp_bus_id_to_type [MAX_MP_BUSSES] = { -1, };
156 extern int mp_irq_entries;
157 extern struct mpc_config_intsrc mp_irqs [MAX_IRQ_SOURCES];
158 extern int mpc_default_type;
159 int mp_bus_id_to_pci_bus [MAX_MP_BUSSES] = { -1, };
160 int mp_current_pci_id = 0;
161 unsigned long mp_lapic_addr = 0;
163 /* #define SMP_DEBUG */
165 #ifdef SMP_DEBUG
166 #define SMP_PRINTK(x) printk x
167 #else
168 #define SMP_PRINTK(x)
169 #endif
172 * Setup routine for controlling SMP activation
174 * Command-line option of "nosmp" or "maxcpus=0" will disable SMP
175 * activation entirely (the MPS table probe still happens, though).
177 * Command-line option of "maxcpus=<NUM>", where <NUM> is an integer
178 * greater than 0, limits the maximum number of CPUs activated in
179 * SMP mode to <NUM>.
182 __initfunc(void smp_setup(char *str, int *ints))
184 if (ints && ints[0] > 0)
185 max_cpus = ints[1];
186 else
187 max_cpus = 0;
190 void ack_APIC_irq (void)
192 /* Clear the IPI */
194 /* Dummy read */
195 apic_read(APIC_SPIV);
197 /* Docs say use 0 for future compatibility */
198 apic_write(APIC_EOI, 0);
202 * Checksum an MP configuration block.
205 static int mpf_checksum(unsigned char *mp, int len)
207 int sum=0;
208 while(len--)
209 sum+=*mp++;
210 return sum&0xFF;
214 * Processor encoding in an MP configuration block
217 static char *mpc_family(int family,int model)
219 static char n[32];
220 static char *model_defs[]=
222 "80486DX","80486DX",
223 "80486SX","80486DX/2 or 80487",
224 "80486SL","Intel5X2(tm)",
225 "Unknown","Unknown",
226 "80486DX/4"
228 if(family==0x6)
229 return("Pentium(tm) Pro");
230 if(family==0x5)
231 return("Pentium(tm)");
232 if(family==0x0F && model==0x0F)
233 return("Special controller");
234 if(family==0x04 && model<9)
235 return model_defs[model];
236 sprintf(n,"Unknown CPU [%d:%d]",family, model);
237 return n;
241 * Read the MPC
244 __initfunc(static int smp_read_mpc(struct mp_config_table *mpc))
246 char str[16];
247 int count=sizeof(*mpc);
248 int apics=0;
249 unsigned char *mpt=((unsigned char *)mpc)+count;
251 if(memcmp(mpc->mpc_signature,MPC_SIGNATURE,4))
253 printk("Bad signature [%c%c%c%c].\n",
254 mpc->mpc_signature[0],
255 mpc->mpc_signature[1],
256 mpc->mpc_signature[2],
257 mpc->mpc_signature[3]);
258 return 1;
260 if(mpf_checksum((unsigned char *)mpc,mpc->mpc_length))
262 printk("Checksum error.\n");
263 return 1;
265 if(mpc->mpc_spec!=0x01 && mpc->mpc_spec!=0x04)
267 printk("Bad Config Table version (%d)!!\n",mpc->mpc_spec);
268 return 1;
270 memcpy(str,mpc->mpc_oem,8);
271 str[8]=0;
272 memcpy(ioapic_OEM_ID,str,9);
273 printk("OEM ID: %s ",str);
275 memcpy(str,mpc->mpc_productid,12);
276 str[12]=0;
277 memcpy(ioapic_Product_ID,str,13);
278 printk("Product ID: %s ",str);
280 printk("APIC at: 0x%lX\n",mpc->mpc_lapic);
282 /* save the local APIC address, it might be non-default */
283 mp_lapic_addr = mpc->mpc_lapic;
286 * Now process the configuration blocks.
289 while(count<mpc->mpc_length)
291 switch(*mpt)
293 case MP_PROCESSOR:
295 struct mpc_config_processor *m=
296 (struct mpc_config_processor *)mpt;
297 if(m->mpc_cpuflag&CPU_ENABLED)
299 printk("Processor #%d %s APIC version %d\n",
300 m->mpc_apicid,
301 mpc_family((m->mpc_cpufeature&
302 CPU_FAMILY_MASK)>>8,
303 (m->mpc_cpufeature&
304 CPU_MODEL_MASK)>>4),
305 m->mpc_apicver);
306 #ifdef SMP_DEBUG
307 if(m->mpc_featureflag&(1<<0))
308 printk(" Floating point unit present.\n");
309 if(m->mpc_featureflag&(1<<7))
310 printk(" Machine Exception supported.\n");
311 if(m->mpc_featureflag&(1<<8))
312 printk(" 64 bit compare & exchange supported.\n");
313 if(m->mpc_featureflag&(1<<9))
314 printk(" Internal APIC present.\n");
315 #endif
316 if(m->mpc_cpuflag&CPU_BOOTPROCESSOR)
318 SMP_PRINTK((" Bootup CPU\n"));
319 boot_cpu_id=m->mpc_apicid;
321 else /* Boot CPU already counted */
322 num_processors++;
324 if(m->mpc_apicid>NR_CPUS)
325 printk("Processor #%d unused. (Max %d processors).\n",m->mpc_apicid, NR_CPUS);
326 else
328 cpu_present_map|=(1<<m->mpc_apicid);
329 apic_version[m->mpc_apicid]=m->mpc_apicver;
332 mpt+=sizeof(*m);
333 count+=sizeof(*m);
334 break;
336 case MP_BUS:
338 struct mpc_config_bus *m=
339 (struct mpc_config_bus *)mpt;
340 memcpy(str,m->mpc_bustype,6);
341 str[6]=0;
342 SMP_PRINTK(("Bus #%d is %s\n",
343 m->mpc_busid,
344 str));
345 if ((strncmp(m->mpc_bustype,"ISA",3) == 0) ||
346 (strncmp(m->mpc_bustype,"EISA",4) == 0))
347 mp_bus_id_to_type[m->mpc_busid] =
348 MP_BUS_ISA;
349 else
350 if (strncmp(m->mpc_bustype,"PCI",3) == 0) {
351 mp_bus_id_to_type[m->mpc_busid] =
352 MP_BUS_PCI;
353 mp_bus_id_to_pci_bus[m->mpc_busid] =
354 mp_current_pci_id;
355 mp_current_pci_id++;
357 mpt+=sizeof(*m);
358 count+=sizeof(*m);
359 break;
361 case MP_IOAPIC:
363 struct mpc_config_ioapic *m=
364 (struct mpc_config_ioapic *)mpt;
365 if(m->mpc_flags&MPC_APIC_USABLE)
367 apics++;
368 printk("I/O APIC #%d Version %d at 0x%lX.\n",
369 m->mpc_apicid,m->mpc_apicver,
370 m->mpc_apicaddr);
371 mp_ioapic_addr = m->mpc_apicaddr;
373 mpt+=sizeof(*m);
374 count+=sizeof(*m);
375 break;
377 case MP_INTSRC:
379 struct mpc_config_intsrc *m=
380 (struct mpc_config_intsrc *)mpt;
382 mp_irqs [mp_irq_entries] = *m;
383 if (++mp_irq_entries == MAX_IRQ_SOURCES) {
384 printk("Max irq sources exceeded!!\n");
385 printk("Skipping remaining sources.\n");
386 --mp_irq_entries;
389 mpt+=sizeof(*m);
390 count+=sizeof(*m);
391 break;
393 case MP_LINTSRC:
395 struct mpc_config_intlocal *m=
396 (struct mpc_config_intlocal *)mpt;
397 mpt+=sizeof(*m);
398 count+=sizeof(*m);
399 break;
403 if(apics>1)
404 printk("Warning: Multiple APICs not supported.\n");
405 return num_processors;
409 * Scan the memory blocks for an SMP configuration block.
412 __initfunc(int smp_scan_config(unsigned long base, unsigned long length))
414 unsigned long *bp=phys_to_virt(base);
415 struct intel_mp_floating *mpf;
417 SMP_PRINTK(("Scan SMP from %p for %ld bytes.\n",
418 bp,length));
419 if(sizeof(*mpf)!=16)
420 printk("Error: MPF size\n");
422 while(length>0)
424 if(*bp==SMP_MAGIC_IDENT)
426 mpf=(struct intel_mp_floating *)bp;
427 if(mpf->mpf_length==1 &&
428 !mpf_checksum((unsigned char *)bp,16) &&
429 (mpf->mpf_specification == 1
430 || mpf->mpf_specification == 4) )
432 printk("Intel MultiProcessor Specification v1.%d\n", mpf->mpf_specification);
433 if(mpf->mpf_feature2&(1<<7))
434 printk(" IMCR and PIC compatibility mode.\n");
435 else
436 printk(" Virtual Wire compatibility mode.\n");
437 smp_found_config=1;
439 * Now see if we need to read further.
441 if(mpf->mpf_feature1!=0)
443 unsigned long cfg;
445 /* local APIC has default address */
446 mp_lapic_addr = 0xFEE00000;
448 * We need to know what the local
449 * APIC id of the boot CPU is!
454 * HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK HACK
456 * It's not just a crazy hack. ;-)
459 * Standard page mapping
460 * functions don't work yet.
461 * We know that page 0 is not
462 * used. Steal it for now!
465 cfg=pg0[0];
466 pg0[0] = (mp_lapic_addr | 7);
467 local_flush_tlb();
469 boot_cpu_id = GET_APIC_ID(*((volatile unsigned long *) APIC_ID));
472 * Give it back
475 pg0[0]= cfg;
476 local_flush_tlb();
480 * END OF HACK END OF HACK END OF HACK END OF HACK END OF HACK
484 * 2 CPUs, numbered 0 & 1.
486 cpu_present_map=3;
487 num_processors=2;
488 printk("I/O APIC at 0xFEC00000.\n");
491 * Save the default type number, we
492 * need it later to set the IO-APIC
493 * up properly:
495 mpc_default_type = mpf->mpf_feature1;
497 printk("Bus #0 is ");
499 switch(mpf->mpf_feature1)
501 case 1:
502 case 5:
503 printk("ISA\n");
504 break;
505 case 2:
506 printk("EISA with no IRQ8 chaining\n");
507 break;
508 case 6:
509 case 3:
510 printk("EISA\n");
511 break;
512 case 4:
513 case 7:
514 printk("MCA\n");
515 break;
516 case 0:
517 break;
518 default:
519 printk("???\nUnknown standard configuration %d\n",
520 mpf->mpf_feature1);
521 return 1;
523 if(mpf->mpf_feature1>4)
525 printk("Bus #1 is PCI\n");
528 * Set local APIC version to
529 * the integrated form.
530 * It's initialized to zero
531 * otherwise, representing
532 * a discrete 82489DX.
534 apic_version[0] = 0x10;
535 apic_version[1] = 0x10;
538 * Read the physical hardware table.
539 * Anything here will override the
540 * defaults.
542 if(mpf->mpf_physptr)
543 smp_read_mpc((void *)mpf->mpf_physptr);
545 __cpu_logical_map[0] = boot_cpu_id;
546 global_irq_holder = boot_cpu_id;
547 current->processor = boot_cpu_id;
549 printk("Processors: %d\n", num_processors);
551 * Only use the first configuration found.
553 return 1;
556 bp+=4;
557 length-=16;
560 return 0;
564 * Trampoline 80x86 program as an array.
567 extern unsigned char trampoline_data [];
568 extern unsigned char trampoline_end [];
569 static unsigned char *trampoline_base;
572 * Currently trivial. Write the real->protected mode
573 * bootstrap into the page concerned. The caller
574 * has made sure it's suitably aligned.
577 __initfunc(static unsigned long setup_trampoline(void))
579 memcpy(trampoline_base, trampoline_data, trampoline_end - trampoline_data);
580 return virt_to_phys(trampoline_base);
584 * We are called very early to get the low memory for the
585 * SMP bootup trampoline page.
587 __initfunc(unsigned long smp_alloc_memory(unsigned long mem_base))
589 if (virt_to_phys((void *)mem_base) >= 0x9F000)
590 panic("smp_alloc_memory: Insufficient low memory for kernel trampoline 0x%lx.", mem_base);
591 trampoline_base = (void *)mem_base;
592 return mem_base + PAGE_SIZE;
596 * The bootstrap kernel entry code has set these up. Save them for
597 * a given CPU
600 __initfunc(void smp_store_cpu_info(int id))
602 struct cpuinfo_x86 *c=&cpu_data[id];
604 *c = boot_cpu_data;
605 c->pte_quick = 0;
606 c->pgd_quick = 0;
607 c->pgtable_cache_sz = 0;
608 identify_cpu(c);
610 * Mask B, Pentium, but not Pentium MMX
612 if (c->x86_vendor == X86_VENDOR_INTEL &&
613 c->x86 == 5 &&
614 c->x86_mask >= 1 && c->x86_mask <= 4 &&
615 c->x86_model <= 3)
616 smp_b_stepping=1; /* Remember we have B step Pentia with bugs */
620 * Architecture specific routine called by the kernel just before init is
621 * fired off. This allows the BP to have everything in order [we hope].
622 * At the end of this all the APs will hit the system scheduling and off
623 * we go. Each AP will load the system gdt's and jump through the kernel
624 * init into idle(). At this point the scheduler will one day take over
625 * and give them jobs to do. smp_callin is a standard routine
626 * we use to track CPUs as they power up.
629 __initfunc(void smp_commence(void))
632 * Lets the callins below out of their loop.
634 SMP_PRINTK(("Setting commenced=1, go go go\n"));
635 smp_commenced=1;
638 __initfunc(void enable_local_APIC(void))
640 unsigned long value;
642 value = apic_read(APIC_SPIV);
643 value |= (1<<8); /* Enable APIC (bit==1) */
644 value &= ~(1<<9); /* Enable focus processor (bit==0) */
645 value |= 0xff; /* Set spurious IRQ vector to 0xff */
646 apic_write(APIC_SPIV,value);
648 value = apic_read(APIC_TASKPRI);
649 value &= ~APIC_TPRI_MASK; /* Set Task Priority to 'accept all' */
650 apic_write(APIC_TASKPRI,value);
652 udelay(100); /* B safe */
653 ack_APIC_irq();
654 udelay(100);
657 __initfunc(unsigned long init_smp_mappings(unsigned long memory_start))
659 unsigned long apic_phys, ioapic_phys;
661 memory_start = PAGE_ALIGN(memory_start);
662 if (smp_found_config) {
663 apic_phys = mp_lapic_addr;
664 ioapic_phys = mp_ioapic_addr;
665 } else {
667 * set up a fake all zeroes page to simulate the
668 * local APIC and another one for the IO-APIC. We
669 * could use the real zero-page, but it's safer
670 * this way if some buggy code writes to this page ...
672 apic_phys = __pa(memory_start);
673 ioapic_phys = __pa(memory_start+PAGE_SIZE);
674 memset((void *)memory_start, 0, 2*PAGE_SIZE);
675 memory_start += 2*PAGE_SIZE;
678 set_fixmap(FIX_APIC_BASE,apic_phys);
679 set_fixmap(FIX_IO_APIC_BASE,ioapic_phys);
681 printk("mapped APIC to %08lx (%08lx)\n", APIC_BASE, apic_phys);
682 printk("mapped IOAPIC to %08lx (%08lx)\n", fix_to_virt(FIX_IO_APIC_BASE), ioapic_phys);
684 return memory_start;
687 __initfunc(void smp_callin(void))
689 extern void calibrate_delay(void);
690 int cpuid=GET_APIC_ID(apic_read(APIC_ID));
693 * Activate our APIC
696 SMP_PRINTK(("CALLIN %d %d\n",hard_smp_processor_id(), smp_processor_id()));
697 enable_local_APIC();
700 * Set up our APIC timer.
702 setup_APIC_clock();
704 sti();
706 * Get our bogomips.
708 calibrate_delay();
709 SMP_PRINTK(("Stack at about %p\n",&cpuid));
712 * Save our processor parameters
714 smp_store_cpu_info(cpuid);
717 * Allow the master to continue.
719 set_bit(cpuid, (unsigned long *)&cpu_callin_map[0]);
722 int cpucount = 0;
724 extern int cpu_idle(void * unused);
727 * Activate a secondary processor.
729 __initfunc(int start_secondary(void *unused))
731 #ifdef CONFIG_MTRR
732 /* Must be done before calibration delay is computed */
733 mtrr_init_secondary_cpu ();
734 #endif
735 smp_callin();
736 while (!smp_commenced)
737 barrier();
738 return cpu_idle(NULL);
742 * Everything has been set up for the secondary
743 * CPUs - they just need to reload everything
744 * from the task structure
746 __initfunc(void initialize_secondary(void))
748 struct thread_struct * p = &current->tss;
751 * We don't actually need to load the full TSS,
752 * basically just the stack pointer and the eip.
754 * Get the scheduler lock, because we're going
755 * to release it as part of the "reschedule" return.
757 spin_lock(&scheduler_lock);
758 asm volatile("lldt %%ax": :"a" (p->ldt));
759 asm volatile("ltr %%ax": :"a" (p->tr));
760 asm volatile(
761 "movl %0,%%esp\n\t"
762 "jmp *%1"
764 :"r" (p->esp),"r" (p->eip));
767 extern struct {
768 void * esp;
769 unsigned short ss;
770 } stack_start;
772 __initfunc(static void do_boot_cpu(int i))
774 unsigned long cfg;
775 pgd_t maincfg;
776 struct task_struct *idle;
777 unsigned long send_status, accept_status;
778 int timeout, num_starts, j;
779 unsigned long start_eip;
782 * We need an idle process for each processor.
785 kernel_thread(start_secondary, NULL, CLONE_PID);
786 cpucount++;
788 idle = task[cpucount];
789 if (!idle)
790 panic("No idle process for CPU %d", i);
792 idle->processor = i;
793 __cpu_logical_map[cpucount] = i;
794 cpu_number_map[i] = cpucount;
796 /* start_eip had better be page-aligned! */
797 start_eip = setup_trampoline();
799 printk("Booting processor %d eip %lx\n", i, start_eip); /* So we see what's up */
800 stack_start.esp = (void *) (1024 + PAGE_SIZE + (char *)idle);
803 * This grunge runs the startup process for
804 * the targeted processor.
807 SMP_PRINTK(("Setting warm reset code and vector.\n"));
809 CMOS_WRITE(0xa, 0xf);
810 local_flush_tlb();
811 SMP_PRINTK(("1.\n"));
812 *((volatile unsigned short *) phys_to_virt(0x469)) = start_eip >> 4;
813 SMP_PRINTK(("2.\n"));
814 *((volatile unsigned short *) phys_to_virt(0x467)) = start_eip & 0xf;
815 SMP_PRINTK(("3.\n"));
817 maincfg=swapper_pg_dir[0];
818 ((unsigned long *)swapper_pg_dir)[0]=0x102007;
821 * Be paranoid about clearing APIC errors.
824 if ( apic_version[i] & 0xF0 )
826 apic_write(APIC_ESR, 0);
827 accept_status = (apic_read(APIC_ESR) & 0xEF);
831 * Status is now clean
834 send_status = 0;
835 accept_status = 0;
838 * Starting actual IPI sequence...
841 SMP_PRINTK(("Asserting INIT.\n"));
844 * Turn INIT on
847 cfg=apic_read(APIC_ICR2);
848 cfg&=0x00FFFFFF;
849 apic_write(APIC_ICR2, cfg|SET_APIC_DEST_FIELD(i)); /* Target chip */
850 cfg=apic_read(APIC_ICR);
851 cfg&=~0xCDFFF; /* Clear bits */
852 cfg |= (APIC_DEST_FIELD | APIC_DEST_LEVELTRIG
853 | APIC_DEST_ASSERT | APIC_DEST_DM_INIT);
854 apic_write(APIC_ICR, cfg); /* Send IPI */
856 udelay(200);
857 SMP_PRINTK(("Deasserting INIT.\n"));
859 cfg=apic_read(APIC_ICR2);
860 cfg&=0x00FFFFFF;
861 apic_write(APIC_ICR2, cfg|SET_APIC_DEST_FIELD(i)); /* Target chip */
862 cfg=apic_read(APIC_ICR);
863 cfg&=~0xCDFFF; /* Clear bits */
864 cfg |= (APIC_DEST_FIELD | APIC_DEST_LEVELTRIG
865 | APIC_DEST_DM_INIT);
866 apic_write(APIC_ICR, cfg); /* Send IPI */
869 * Should we send STARTUP IPIs ?
871 * Determine this based on the APIC version.
872 * If we don't have an integrated APIC, don't
873 * send the STARTUP IPIs.
876 if ( apic_version[i] & 0xF0 )
877 num_starts = 2;
878 else
879 num_starts = 0;
882 * Run STARTUP IPI loop.
885 for (j = 1; !(send_status || accept_status)
886 && (j <= num_starts) ; j++)
888 SMP_PRINTK(("Sending STARTUP #%d.\n",j));
889 apic_write(APIC_ESR, 0);
890 SMP_PRINTK(("After apic_write.\n"));
893 * STARTUP IPI
896 cfg=apic_read(APIC_ICR2);
897 cfg&=0x00FFFFFF;
898 apic_write(APIC_ICR2, cfg|SET_APIC_DEST_FIELD(i)); /* Target chip */
899 cfg=apic_read(APIC_ICR);
900 cfg&=~0xCDFFF; /* Clear bits */
901 cfg |= (APIC_DEST_FIELD
902 | APIC_DEST_DM_STARTUP
903 | (start_eip >> 12)); /* Boot on the stack */
904 SMP_PRINTK(("Before start apic_write.\n"));
905 apic_write(APIC_ICR, cfg); /* Kick the second */
907 SMP_PRINTK(("Startup point 1.\n"));
908 timeout = 0;
909 do {
910 SMP_PRINTK(("Sleeping.\n")); mdelay(1000);
911 udelay(10);
912 } while ( (send_status = (apic_read(APIC_ICR) & 0x1000))
913 && (timeout++ < 1000));
914 udelay(200);
915 accept_status = (apic_read(APIC_ESR) & 0xEF);
917 SMP_PRINTK(("After Startup.\n"));
919 if (send_status) /* APIC never delivered?? */
920 printk("APIC never delivered???\n");
921 if (accept_status) /* Send accept error */
922 printk("APIC delivery error (%lx).\n", accept_status);
924 if( !(send_status || accept_status) )
926 for(timeout=0;timeout<50000;timeout++)
928 if(cpu_callin_map[0]&(1<<i))
929 break; /* It has booted */
930 udelay(100); /* Wait 5s total for a response */
932 if(cpu_callin_map[0]&(1<<i))
934 /* number CPUs logically, starting from 1 (BSP is 0) */
935 #if 0
936 cpu_number_map[i] = cpucount;
937 __cpu_logical_map[cpucount] = i;
938 #endif
939 printk("OK.\n");
940 printk("CPU%d: ", i);
941 print_cpu_info(&cpu_data[i]);
943 else
945 if(*((volatile unsigned char *)phys_to_virt(8192))==0xA5)
946 printk("Stuck ??\n");
947 else
948 printk("Not responding.\n");
950 SMP_PRINTK(("CPU has booted.\n"));
952 else
954 __cpu_logical_map[cpucount] = -1;
955 cpu_number_map[i] = -1;
956 cpucount--;
959 swapper_pg_dir[0]=maincfg;
960 local_flush_tlb();
962 /* mark "stuck" area as not stuck */
963 *((volatile unsigned long *)phys_to_virt(8192)) = 0;
966 unsigned int prof_multiplier[NR_CPUS];
967 unsigned int prof_counter[NR_CPUS];
970 * Cycle through the processors sending APIC IPIs to boot each.
973 __initfunc(void smp_boot_cpus(void))
975 int i;
976 unsigned long cfg;
978 #ifdef CONFIG_MTRR
979 /* Must be done before other processors booted */
980 mtrr_init_boot_cpu ();
981 #endif
983 * Initialize the logical to physical CPU number mapping
984 * and the per-CPU profiling counter/multiplier
987 for (i = 0; i < NR_CPUS; i++) {
988 cpu_number_map[i] = -1;
989 prof_counter[i] = 1;
990 prof_multiplier[i] = 1;
994 * Setup boot CPU information
997 smp_store_cpu_info(boot_cpu_id); /* Final full version of the data */
998 printk("CPU%d: ", boot_cpu_id);
999 print_cpu_info(&cpu_data[boot_cpu_id]);
1001 cpu_present_map |= (1 << hard_smp_processor_id());
1002 cpu_number_map[boot_cpu_id] = 0;
1005 * If we don't conform to the Intel MPS standard, get out
1006 * of here now!
1009 if (!smp_found_config)
1011 printk(KERN_NOTICE "SMP motherboard not detected. Using dummy APIC emulation.\n");
1012 io_apic_irqs = 0;
1013 goto smp_done;
1017 * If SMP should be disabled, then really disable it!
1020 if (!max_cpus)
1022 smp_found_config = 0;
1023 printk(KERN_INFO "SMP mode deactivated, forcing use of dummy APIC emulation.\n");
1026 #ifdef SMP_DEBUG
1028 int reg;
1031 * This is to verify that we're looking at
1032 * a real local APIC. Check these against
1033 * your board if the CPUs aren't getting
1034 * started for no apparent reason.
1037 reg = apic_read(APIC_VERSION);
1038 SMP_PRINTK(("Getting VERSION: %x\n", reg));
1040 apic_write(APIC_VERSION, 0);
1041 reg = apic_read(APIC_VERSION);
1042 SMP_PRINTK(("Getting VERSION: %x\n", reg));
1045 * The two version reads above should print the same
1046 * NON-ZERO!!! numbers. If the second one is zero,
1047 * there is a problem with the APIC write/read
1048 * definitions.
1050 * The next two are just to see if we have sane values.
1051 * They're only really relevant if we're in Virtual Wire
1052 * compatibility mode, but most boxes are anymore.
1056 reg = apic_read(APIC_LVT0);
1057 SMP_PRINTK(("Getting LVT0: %x\n", reg));
1059 reg = apic_read(APIC_LVT1);
1060 SMP_PRINTK(("Getting LVT1: %x\n", reg));
1062 #endif
1064 enable_local_APIC();
1067 * Set up our local APIC timer:
1069 setup_APIC_clock ();
1072 * Now scan the CPU present map and fire up the other CPUs.
1075 SMP_PRINTK(("CPU map: %lx\n", cpu_present_map));
1077 for(i=0;i<NR_CPUS;i++)
1080 * Don't even attempt to start the boot CPU!
1082 if (i == boot_cpu_id)
1083 continue;
1085 if ((cpu_present_map & (1 << i))
1086 && (max_cpus < 0 || max_cpus > cpucount+1))
1088 do_boot_cpu(i);
1092 * Make sure we unmap all failed CPUs
1095 if (cpu_number_map[i] == -1 && (cpu_present_map & (1 << i))) {
1096 printk("CPU #%d not responding. Removing from cpu_present_map.\n",i);
1097 cpu_present_map &= ~(1 << i);
1102 * Cleanup possible dangling ends...
1106 * Install writable page 0 entry.
1109 cfg = pg0[0];
1110 pg0[0] = 3; /* writeable, present, addr 0 */
1111 local_flush_tlb();
1114 * Paranoid: Set warm reset code and vector here back
1115 * to default values.
1118 CMOS_WRITE(0, 0xf);
1120 *((volatile long *) phys_to_virt(0x467)) = 0;
1123 * Restore old page 0 entry.
1126 pg0[0] = cfg;
1127 local_flush_tlb();
1130 * Allow the user to impress friends.
1133 SMP_PRINTK(("Before bogomips.\n"));
1134 if(cpucount==0)
1136 printk(KERN_ERR "Error: only one processor found.\n");
1137 cpu_present_map=(1<<hard_smp_processor_id());
1139 else
1141 unsigned long bogosum=0;
1142 for(i=0;i<32;i++)
1144 if(cpu_present_map&(1<<i))
1145 bogosum+=cpu_data[i].loops_per_sec;
1147 printk(KERN_INFO "Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
1148 cpucount+1,
1149 (bogosum+2500)/500000,
1150 ((bogosum+2500)/5000)%100);
1151 SMP_PRINTK(("Before bogocount - setting activated=1.\n"));
1152 smp_activated=1;
1153 smp_num_cpus=cpucount+1;
1155 if(smp_b_stepping)
1156 printk(KERN_WARNING "WARNING: SMP operation may be unreliable with B stepping processors.\n");
1157 SMP_PRINTK(("Boot done.\n"));
1160 * Here we can be sure that there is an IO-APIC in the system. Let's
1161 * go and set it up:
1163 setup_IO_APIC();
1165 smp_done:
1169 void send_IPI (int dest, int vector)
1171 unsigned long cfg;
1172 unsigned long flags;
1174 __save_flags(flags);
1175 __cli();
1178 * prepare target chip field
1181 cfg = apic_read(APIC_ICR2) & 0x00FFFFFF;
1182 apic_write(APIC_ICR2, cfg|SET_APIC_DEST_FIELD(dest));
1184 cfg = apic_read(APIC_ICR);
1185 cfg &= ~0xFDFFF;
1186 cfg |= APIC_DEST_FIELD|APIC_DEST_DM_FIXED|vector;
1187 cfg |= dest;
1190 * Send the IPI. The write to APIC_ICR fires this off.
1193 apic_write(APIC_ICR, cfg);
1194 __restore_flags(flags);
1198 * A non wait message cannot pass data or CPU source info. This current setup
1199 * is only safe because the kernel lock owner is the only person who can send
1200 * a message.
1202 * Wrapping this whole block in a spinlock is not the safe answer either. A
1203 * processor may get stuck with IRQs off waiting to send a message and thus
1204 * not replying to the person spinning for a reply.
1206 * In the end flush tlb ought to be the NMI and a very short function
1207 * (to avoid the old IDE disk problems), and other messages sent with IRQs
1208 * enabled in a civilised fashion. That will also boost performance.
1211 void smp_message_pass(int target, int msg, unsigned long data, int wait)
1213 unsigned long cfg;
1214 unsigned long dest = 0;
1215 unsigned long target_map;
1216 int p=smp_processor_id();
1217 int irq;
1218 int ct=0;
1221 * During boot up send no messages
1224 if(!smp_activated || !smp_commenced)
1225 return;
1229 * Skip the reschedule if we are waiting to clear a
1230 * message at this time. The reschedule cannot wait
1231 * but is not critical.
1234 switch (msg) {
1235 case MSG_RESCHEDULE:
1236 irq = 0x30;
1237 if (smp_cpu_in_msg[p])
1238 return;
1239 break;
1241 case MSG_INVALIDATE_TLB:
1242 /* make this a NMI some day */
1243 irq = 0x31;
1244 break;
1246 case MSG_STOP_CPU:
1247 irq = 0x40;
1248 break;
1250 case MSG_MTRR_CHANGE:
1251 irq = 0x50;
1252 break;
1254 default:
1255 printk("Unknown SMP message %d\n", msg);
1256 return;
1260 * Sanity check we don't re-enter this across CPUs. Only the kernel
1261 * lock holder may send messages. For a STOP_CPU we are bringing the
1262 * entire box to the fastest halt we can. A reschedule carries
1263 * no data and can occur during a flush. Guess what panic
1264 * I got to notice this bug.
1268 * We are busy.
1271 smp_cpu_in_msg[p]++;
1273 /* printk("SMP message pass #%d to %d of %d\n",
1274 p, msg, target);*/
1277 * Wait for the APIC to become ready - this should never occur. It's
1278 * a debugging check really.
1281 while (ct<1000)
1283 cfg=apic_read(APIC_ICR);
1284 if(!(cfg&(1<<12)))
1285 break;
1286 ct++;
1287 udelay(10);
1291 * Just pray... there is nothing more we can do
1294 if(ct==1000)
1295 printk("CPU #%d: previous IPI still not cleared after 10mS\n", p);
1298 * Set the target requirement
1301 if(target==MSG_ALL_BUT_SELF)
1303 dest=APIC_DEST_ALLBUT;
1304 target_map=cpu_present_map;
1305 cpu_callin_map[0]=(1<<p);
1307 else if(target==MSG_ALL)
1309 dest=APIC_DEST_ALLINC;
1310 target_map=cpu_present_map;
1311 cpu_callin_map[0]=0;
1313 else
1315 dest=0;
1316 target_map=(1<<target);
1317 cpu_callin_map[0]=0;
1321 * Program the APIC to deliver the IPI
1324 send_IPI(dest,irq);
1327 * Spin waiting for completion
1330 switch(wait)
1332 int stuck;
1333 case 1:
1334 stuck = 50000000;
1335 while(cpu_callin_map[0]!=target_map) {
1336 --stuck;
1337 if (!stuck) {
1338 printk("stuck on target_map IPI wait\n");
1339 break;
1342 break;
1343 case 2:
1344 stuck = 50000000;
1345 /* Wait for invalidate map to clear */
1346 while (smp_invalidate_needed) {
1347 /* Take care of "crossing" invalidates */
1348 if (test_bit(p, &smp_invalidate_needed))
1349 clear_bit(p, &smp_invalidate_needed);
1350 --stuck;
1351 if (!stuck) {
1352 printk("stuck on smp_invalidate_needed IPI wait (CPU#%d)\n",p);
1353 break;
1356 break;
1360 * Record our completion
1363 smp_cpu_in_msg[p]--;
1367 * This is fraught with deadlocks. Linus does a flush tlb at a whim
1368 * even with IRQs off. We have to avoid a pair of crossing flushes
1369 * or we are doomed. See the notes about smp_message_pass.
1372 void smp_flush_tlb(void)
1374 unsigned long flags;
1376 /* printk("SMI-");*/
1379 * The assignment is safe because it's volatile so the compiler cannot reorder it,
1380 * because the i586 has strict memory ordering and because only the kernel lock holder
1381 * may issue a tlb flush. If you break any one of those three change this to an atomic
1382 * bus locked or.
1385 smp_invalidate_needed=cpu_present_map;
1388 * Processors spinning on the lock will see this IRQ late. The smp_invalidate_needed map will
1389 * ensure they don't do a spurious flush tlb or miss one.
1392 __save_flags(flags);
1393 __cli();
1394 smp_message_pass(MSG_ALL_BUT_SELF, MSG_INVALIDATE_TLB, 0L, 2);
1397 * Flush the local TLB
1400 local_flush_tlb();
1402 __restore_flags(flags);
1405 * Completed.
1408 /* printk("SMID\n");*/
1412 void smp_send_reschedule(int cpu)
1414 unsigned long flags;
1416 __save_flags(flags);
1417 __cli();
1418 smp_message_pass(cpu, MSG_RESCHEDULE, 0L, 0);
1419 __restore_flags(flags);
1423 * Local timer interrupt handler. It does both profiling and
1424 * process statistics/rescheduling.
1426 * We do profiling in every local tick, statistics/rescheduling
1427 * happen only every 'profiling multiplier' ticks. The default
1428 * multiplier is 1 and it can be changed by writing the new multiplier
1429 * value into /proc/profile.
1432 void smp_local_timer_interrupt(struct pt_regs * regs)
1434 int cpu = smp_processor_id();
1437 * The profiling function is SMP safe. (nothing can mess
1438 * around with "current", and the profiling counters are
1439 * updated with atomic operations). This is especially
1440 * useful with a profiling multiplier != 1
1442 if (!user_mode(regs))
1443 x86_do_profile (regs->eip);
1445 if (!--prof_counter[cpu]) {
1446 int user=0,system=0;
1447 struct task_struct * p = current;
1450 * After doing the above, we need to make like
1451 * a normal interrupt - otherwise timer interrupts
1452 * ignore the global interrupt lock, which is the
1453 * WrongThing (tm) to do.
1456 if (user_mode(regs))
1457 user=1;
1458 else
1459 system=1;
1461 irq_enter(cpu, 0);
1462 if (p->pid) {
1463 update_one_process(p, 1, user, system, cpu);
1465 p->counter -= 1;
1466 if (p->counter < 0) {
1467 p->counter = 0;
1468 p->need_resched = 1;
1470 if (p->priority < DEF_PRIORITY) {
1471 kstat.cpu_nice += user;
1472 kstat.per_cpu_nice[cpu] += user;
1473 } else {
1474 kstat.cpu_user += user;
1475 kstat.per_cpu_user[cpu] += user;
1478 kstat.cpu_system += system;
1479 kstat.per_cpu_system[cpu] += system;
1482 prof_counter[cpu]=prof_multiplier[cpu];
1483 irq_exit(cpu, 0);
1487 * We take the 'long' return path, and there every subsystem
1488 * grabs the apropriate locks (kernel lock/ irq lock).
1490 * we might want to decouple profiling from the 'long path',
1491 * and do the profiling totally in assembly.
1493 * Currently this isn't too much of an issue (performance wise),
1494 * we can take more than 100K local irqs per second on a 100 MHz P5.
1499 * Local APIC timer interrupt. This is the most natural way for doing
1500 * local interrupts, but local timer interrupts can be emulated by
1501 * broadcast interrupts too. [in case the hw doesnt support APIC timers]
1503 * [ if a single-CPU system runs an SMP kernel then we call the local
1504 * interrupt as well. Thus we cannot inline the local irq ... ]
1506 void smp_apic_timer_interrupt(struct pt_regs * regs)
1509 * NOTE! We'd better ACK the irq immediately,
1510 * because timer handling can be slow, and we
1511 * want to be able to accept NMI tlb invalidates
1512 * during this time.
1514 ack_APIC_irq();
1515 smp_local_timer_interrupt(regs);
1519 * Reschedule call back. Nothing to do,
1520 * all the work is done automatically when
1521 * we return from the interrupt.
1523 asmlinkage void smp_reschedule_interrupt(void)
1525 ack_APIC_irq();
1529 * Invalidate call-back
1531 asmlinkage void smp_invalidate_interrupt(void)
1533 if (test_and_clear_bit(smp_processor_id(), &smp_invalidate_needed))
1534 local_flush_tlb();
1536 ack_APIC_irq ();
1540 * CPU halt call-back
1542 asmlinkage void smp_stop_cpu_interrupt(void)
1544 if (cpu_data[smp_processor_id()].hlt_works_ok)
1545 for(;;) __asm__("hlt");
1546 for (;;) ;
1549 void (*mtrr_hook) (void) = NULL;
1551 asmlinkage void smp_mtrr_interrupt(void)
1553 ack_APIC_irq ();
1554 if (mtrr_hook) (*mtrr_hook) ();
1558 * This interrupt should _never_ happen with our APIC/SMP architecture
1560 asmlinkage void smp_spurious_interrupt(void)
1562 /* ack_APIC_irq (); see sw-dev-man vol 3, chapter 7.4.13.5 */
1563 printk("spurious APIC interrupt, ayiee, should never happen.\n");
1567 * This part sets up the APIC 32 bit clock in LVTT1, with HZ interrupts
1568 * per second. We assume that the caller has already set up the local
1569 * APIC.
1571 * The APIC timer is not exactly sync with the external timer chip, it
1572 * closely follows bus clocks.
1575 #define RDTSC(x) __asm__ __volatile__ ( "rdtsc" \
1576 :"=a" (((unsigned long*)&x)[0]), \
1577 "=d" (((unsigned long*)&x)[1]))
1580 * The timer chip is already set up at HZ interrupts per second here,
1581 * but we do not accept timer interrupts yet. We only allow the BP
1582 * to calibrate.
1584 __initfunc(static unsigned int get_8254_timer_count (void))
1586 unsigned int count;
1588 outb_p(0x00, 0x43);
1589 count = inb_p(0x40);
1590 count |= inb_p(0x40) << 8;
1592 return count;
1596 * This function sets up the local APIC timer, with a timeout of
1597 * 'clocks' APIC bus clock. During calibration we actually call
1598 * this function twice, once with a bogus timeout value, second
1599 * time for real. The other (noncalibrating) CPUs call this
1600 * function only once, with the real value.
1602 * We are strictly in irqs off mode here, as we do not want to
1603 * get an APIC interrupt go off accidentally.
1605 * We do reads before writes even if unnecessary, to get around the
1606 * APIC double write bug.
1609 #define APIC_DIVISOR 16
1611 void setup_APIC_timer (unsigned int clocks)
1613 unsigned long lvtt1_value;
1614 unsigned int tmp_value;
1617 * Unfortunately the local APIC timer cannot be set up into NMI
1618 * mode. With the IO APIC we can re-route the external timer
1619 * interrupt and broadcast it as an NMI to all CPUs, so no pain.
1621 * NOTE: this trap vector (0x41) and the gate in
1622 * BUILD_SMP_TIMER_INTERRUPT should be the same ;)
1624 tmp_value = apic_read(APIC_LVTT);
1625 lvtt1_value = APIC_LVT_TIMER_PERIODIC | 0x41;
1626 apic_write(APIC_LVTT , lvtt1_value);
1629 * Divide PICLK by 16
1631 tmp_value = apic_read(APIC_TDCR);
1632 apic_write(APIC_TDCR , (tmp_value & ~APIC_TDR_DIV_1 )
1633 | APIC_TDR_DIV_16);
1635 tmp_value = apic_read(APIC_TMICT);
1636 apic_write(APIC_TMICT, clocks/APIC_DIVISOR);
1639 __initfunc(void wait_8254_wraparound (void))
1641 unsigned int curr_count, prev_count=~0;
1642 int delta;
1644 curr_count = get_8254_timer_count();
1646 do {
1647 prev_count = curr_count;
1648 curr_count = get_8254_timer_count();
1649 delta = curr_count-prev_count;
1652 * This limit for delta seems arbitrary, but it isn't, it's
1653 * slightly above the level of error a buggy Mercury/Neptune
1654 * chipset timer can cause.
1657 } while (delta<300);
1661 * In this function we calibrate APIC bus clocks to the external
1662 * timer. Unfortunately we cannot use jiffies and the timer irq
1663 * to calibrate, since some later bootup code depends on getting
1664 * the first irq? Ugh.
1666 * We want to do the calibration only once since we
1667 * want to have local timer irqs syncron. CPUs connected
1668 * by the same APIC bus have the very same bus frequency.
1669 * And we want to have irqs off anyways, no accidental
1670 * APIC irq that way.
1673 __initfunc(int calibrate_APIC_clock (void))
1675 unsigned long long t1,t2;
1676 long tt1,tt2;
1677 long calibration_result;
1678 int i;
1680 printk("calibrating APIC timer ... ");
1683 * Put whatever arbitrary (but long enough) timeout
1684 * value into the APIC clock, we just want to get the
1685 * counter running for calibration.
1687 setup_APIC_timer(1000000000);
1690 * The timer chip counts down to zero. Let's wait
1691 * for a wraparound to start exact measurement:
1692 * (the current tick might have been already half done)
1695 wait_8254_wraparound ();
1698 * We wrapped around just now. Let's start:
1700 RDTSC(t1);
1701 tt1=apic_read(APIC_TMCCT);
1703 #define LOOPS (HZ/10)
1705 * Let's wait LOOPS wraprounds:
1707 for (i=0; i<LOOPS; i++)
1708 wait_8254_wraparound ();
1710 tt2=apic_read(APIC_TMCCT);
1711 RDTSC(t2);
1714 * The APIC bus clock counter is 32 bits only, it
1715 * might have overflown, but note that we use signed
1716 * longs, thus no extra care needed.
1718 * underflown to be exact, as the timer counts down ;)
1721 calibration_result = (tt1-tt2)*APIC_DIVISOR/LOOPS;
1723 SMP_PRINTK(("\n..... %ld CPU clocks in 1 timer chip tick.",
1724 (unsigned long)(t2-t1)/LOOPS));
1726 SMP_PRINTK(("\n..... %ld APIC bus clocks in 1 timer chip tick.",
1727 calibration_result));
1730 printk("\n..... CPU clock speed is %ld.%04ld MHz.\n",
1731 ((long)(t2-t1)/LOOPS)/(1000000/HZ),
1732 ((long)(t2-t1)/LOOPS)%(1000000/HZ) );
1734 printk("..... APIC bus clock speed is %ld.%04ld MHz.\n",
1735 calibration_result/(1000000/HZ),
1736 calibration_result%(1000000/HZ) );
1737 #undef LOOPS
1739 return calibration_result;
1742 static unsigned int calibration_result;
1744 __initfunc(void setup_APIC_clock (void))
1746 unsigned long flags;
1748 static volatile int calibration_lock;
1750 save_flags(flags);
1751 cli();
1753 SMP_PRINTK(("setup_APIC_clock() called.\n"));
1756 * [ setup_APIC_clock() is called from all CPUs, but we want
1757 * to do this part of the setup only once ... and it fits
1758 * here best ]
1760 if (!test_and_set_bit(0,&calibration_lock)) {
1762 calibration_result=calibrate_APIC_clock();
1764 * Signal completion to the other CPU[s]:
1766 calibration_lock = 3;
1768 } else {
1770 * Other CPU is calibrating, wait for finish:
1772 SMP_PRINTK(("waiting for other CPU calibrating APIC ... "));
1773 while (calibration_lock == 1);
1774 SMP_PRINTK(("done, continuing.\n"));
1778 * Now set up the timer for real.
1781 setup_APIC_timer (calibration_result);
1784 * We ACK the APIC, just in case there is something pending.
1787 ack_APIC_irq ();
1790 restore_flags(flags);
1794 * the frequency of the profiling timer can be changed
1795 * by writing a multiplier value into /proc/profile.
1797 * usually you want to run this on all CPUs ;)
1799 int setup_profiling_timer (unsigned int multiplier)
1801 int cpu = smp_processor_id();
1802 unsigned long flags;
1805 * Sanity check. [at least 500 APIC cycles should be
1806 * between APIC interrupts as a rule of thumb, to avoid
1807 * irqs flooding us]
1809 if ( (!multiplier) || (calibration_result/multiplier < 500))
1810 return -EINVAL;
1812 save_flags(flags);
1813 cli();
1814 setup_APIC_timer (calibration_result/multiplier);
1815 prof_multiplier[cpu]=multiplier;
1816 restore_flags(flags);
1818 return 0;
1821 #undef APIC_DIVISOR