smpboot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * linux/arch/m32r/kernel/smpboot.c
  3. * orig : i386 2.4.10
  4. *
  5. * M32R SMP booting functions
  6. *
  7. * Copyright (c) 2001, 2002, 2003 Hitoshi Yamamoto
  8. *
  9. * Taken from i386 version.
  10. * (c) 1995 Alan Cox, Building #3 <alan@redhat.com>
  11. * (c) 1998, 1999, 2000 Ingo Molnar <mingo@redhat.com>
  12. *
  13. * Much of the core SMP work is based on previous work by Thomas Radke, to
  14. * whom a great many thanks are extended.
  15. *
  16. * Thanks to Intel for making available several different Pentium,
  17. * Pentium Pro and Pentium-II/Xeon MP machines.
  18. * Original development of Linux SMP code supported by Caldera.
  19. *
  20. * This code is released under the GNU General Public License version 2 or
  21. * later.
  22. *
  23. * Fixes
  24. * Felix Koop : NR_CPUS used properly
  25. * Jose Renau : Handle single CPU case.
  26. * Alan Cox : By repeated request
  27. * 8) - Total BogoMIP report.
  28. * Greg Wright : Fix for kernel stacks panic.
  29. * Erich Boleyn : MP v1.4 and additional changes.
  30. * Matthias Sattler : Changes for 2.1 kernel map.
  31. * Michel Lespinasse : Changes for 2.1 kernel map.
  32. * Michael Chastain : Change trampoline.S to gnu as.
  33. * Alan Cox : Dumb bug: 'B' step PPro's are fine
  34. * Ingo Molnar : Added APIC timers, based on code
  35. * from Jose Renau
  36. * Ingo Molnar : various cleanups and rewrites
  37. * Tigran Aivazian : fixed "0.00 in /proc/uptime on SMP" bug.
  38. * Maciej W. Rozycki : Bits for genuine 82489DX APICs
  39. * Martin J. Bligh : Added support for multi-quad systems
  40. */
  41. #include <linux/module.h>
  42. #include <linux/cpu.h>
  43. #include <linux/init.h>
  44. #include <linux/kernel.h>
  45. #include <linux/mm.h>
  46. #include <linux/sched.h>
  47. #include <linux/err.h>
  48. #include <linux/irq.h>
  49. #include <linux/bootmem.h>
  50. #include <linux/delay.h>
  51. #include <asm/io.h>
  52. #include <asm/pgalloc.h>
  53. #include <asm/tlbflush.h>
  54. #define DEBUG_SMP
  55. #ifdef DEBUG_SMP
  56. #define Dprintk(x...) printk(x)
  57. #else
  58. #define Dprintk(x...)
  59. #endif
  60. extern cpumask_t cpu_initialized;
  61. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  62. /* Data structures and variables */
  63. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  64. /* Processor that is doing the boot up */
  65. static unsigned int bsp_phys_id = -1;
  66. /* Bitmask of physically existing CPUs */
  67. physid_mask_t phys_cpu_present_map;
  68. cpumask_t cpu_bootout_map;
  69. cpumask_t cpu_bootin_map;
  70. static cpumask_t cpu_callin_map;
  71. cpumask_t cpu_callout_map;
  72. EXPORT_SYMBOL(cpu_callout_map);
  73. /* Per CPU bogomips and other parameters */
  74. struct cpuinfo_m32r cpu_data[NR_CPUS] __cacheline_aligned;
  75. static int cpucount;
  76. static cpumask_t smp_commenced_mask;
  77. extern struct {
  78. void * spi;
  79. unsigned short ss;
  80. } stack_start;
  81. /* which physical physical ID maps to which logical CPU number */
  82. static volatile int physid_2_cpu[NR_CPUS];
  83. #define physid_to_cpu(physid) physid_2_cpu[physid]
  84. /* which logical CPU number maps to which physical ID */
  85. volatile int cpu_2_physid[NR_CPUS];
  86. DEFINE_PER_CPU(int, prof_multiplier) = 1;
  87. DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
  88. DEFINE_PER_CPU(int, prof_counter) = 1;
  89. spinlock_t ipi_lock[NR_IPIS];
  90. static unsigned int calibration_result;
  91. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  92. /* Function Prototypes */
  93. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  94. static void init_ipi_lock(void);
  95. static void do_boot_cpu(int);
  96. int start_secondary(void *);
  97. static void smp_callin(void);
  98. static void smp_online(void);
  99. static void show_mp_info(int);
  100. static void smp_store_cpu_info(int);
  101. static void show_cpu_info(int);
  102. int setup_profiling_timer(unsigned int);
  103. static void init_cpu_to_physid(void);
  104. static void map_cpu_to_physid(int, int);
  105. static void unmap_cpu_to_physid(int, int);
  106. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  107. /* Boot up APs Routines : BSP */
  108. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  109. void smp_prepare_boot_cpu(void)
  110. {
  111. bsp_phys_id = hard_smp_processor_id();
  112. physid_set(bsp_phys_id, phys_cpu_present_map);
  113. set_cpu_online(0, true); /* BSP's cpu_id == 0 */
  114. cpumask_set_cpu(0, &cpu_callout_map);
  115. cpumask_set_cpu(0, &cpu_callin_map);
  116. /*
  117. * Initialize the logical to physical CPU number mapping
  118. */
  119. init_cpu_to_physid();
  120. map_cpu_to_physid(0, bsp_phys_id);
  121. current_thread_info()->cpu = 0;
  122. }
  123. /*==========================================================================*
  124. * Name: smp_prepare_cpus (old smp_boot_cpus)
  125. *
  126. * Description: This routine boot up APs.
  127. *
  128. * Born on Date: 2002.02.05
  129. *
  130. * Arguments: NONE
  131. *
  132. * Returns: void (cannot fail)
  133. *
  134. * Modification log:
  135. * Date Who Description
  136. * ---------- --- --------------------------------------------------------
  137. * 2003-06-24 hy modify for linux-2.5.69
  138. *
  139. *==========================================================================*/
  140. void __init smp_prepare_cpus(unsigned int max_cpus)
  141. {
  142. int phys_id;
  143. unsigned long nr_cpu;
  144. nr_cpu = inl(M32R_FPGA_NUM_OF_CPUS_PORTL);
  145. if (nr_cpu > NR_CPUS) {
  146. printk(KERN_INFO "NUM_OF_CPUS reg. value [%ld] > NR_CPU [%d]",
  147. nr_cpu, NR_CPUS);
  148. goto smp_done;
  149. }
  150. for (phys_id = 0 ; phys_id < nr_cpu ; phys_id++)
  151. physid_set(phys_id, phys_cpu_present_map);
  152. #ifndef CONFIG_HOTPLUG_CPU
  153. init_cpu_present(cpu_possible_mask);
  154. #endif
  155. show_mp_info(nr_cpu);
  156. init_ipi_lock();
  157. /*
  158. * Setup boot CPU information
  159. */
  160. smp_store_cpu_info(0); /* Final full version of the data */
  161. /*
  162. * If SMP should be disabled, then really disable it!
  163. */
  164. if (!max_cpus) {
  165. printk(KERN_INFO "SMP mode deactivated by commandline.\n");
  166. goto smp_done;
  167. }
  168. /*
  169. * Now scan the CPU present map and fire up the other CPUs.
  170. */
  171. Dprintk("CPU present map : %lx\n", physids_coerce(phys_cpu_present_map));
  172. for (phys_id = 0 ; phys_id < NR_CPUS ; phys_id++) {
  173. /*
  174. * Don't even attempt to start the boot CPU!
  175. */
  176. if (phys_id == bsp_phys_id)
  177. continue;
  178. if (!physid_isset(phys_id, phys_cpu_present_map))
  179. continue;
  180. if (max_cpus <= cpucount + 1)
  181. continue;
  182. do_boot_cpu(phys_id);
  183. /*
  184. * Make sure we unmap all failed CPUs
  185. */
  186. if (physid_to_cpu(phys_id) == -1) {
  187. physid_clear(phys_id, phys_cpu_present_map);
  188. printk("phys CPU#%d not responding - " \
  189. "cannot use it.\n", phys_id);
  190. }
  191. }
  192. smp_done:
  193. Dprintk("Boot done.\n");
  194. }
  195. /*
  196. * init_ipi_lock : Initialize IPI locks.
  197. */
  198. static void __init init_ipi_lock(void)
  199. {
  200. int ipi;
  201. for (ipi = 0 ; ipi < NR_IPIS ; ipi++)
  202. spin_lock_init(&ipi_lock[ipi]);
  203. }
  204. /*==========================================================================*
  205. * Name: do_boot_cpu
  206. *
  207. * Description: This routine boot up one AP.
  208. *
  209. * Born on Date: 2002.02.05
  210. *
  211. * Arguments: phys_id - Target CPU physical ID
  212. *
  213. * Returns: void (cannot fail)
  214. *
  215. * Modification log:
  216. * Date Who Description
  217. * ---------- --- --------------------------------------------------------
  218. * 2003-06-24 hy modify for linux-2.5.69
  219. *
  220. *==========================================================================*/
  221. static void __init do_boot_cpu(int phys_id)
  222. {
  223. struct task_struct *idle;
  224. unsigned long send_status, boot_status;
  225. int timeout, cpu_id;
  226. cpu_id = ++cpucount;
  227. /*
  228. * We can't use kernel_thread since we must avoid to
  229. * reschedule the child.
  230. */
  231. idle = fork_idle(cpu_id);
  232. if (IS_ERR(idle))
  233. panic("failed fork for CPU#%d.", cpu_id);
  234. idle->thread.lr = (unsigned long)start_secondary;
  235. map_cpu_to_physid(cpu_id, phys_id);
  236. /* So we see what's up */
  237. printk("Booting processor %d/%d\n", phys_id, cpu_id);
  238. stack_start.spi = (void *)idle->thread.sp;
  239. task_thread_info(idle)->cpu = cpu_id;
  240. /*
  241. * Send Startup IPI
  242. * 1.IPI received by CPU#(phys_id).
  243. * 2.CPU#(phys_id) enter startup_AP (arch/m32r/kernel/head.S)
  244. * 3.CPU#(phys_id) enter start_secondary()
  245. */
  246. send_status = 0;
  247. boot_status = 0;
  248. cpumask_set_cpu(phys_id, &cpu_bootout_map);
  249. /* Send Startup IPI */
  250. send_IPI_mask_phys(cpumask_of(phys_id), CPU_BOOT_IPI, 0);
  251. Dprintk("Waiting for send to finish...\n");
  252. timeout = 0;
  253. /* Wait 100[ms] */
  254. do {
  255. Dprintk("+");
  256. udelay(1000);
  257. send_status = !cpumask_test_cpu(phys_id, &cpu_bootin_map);
  258. } while (send_status && (timeout++ < 100));
  259. Dprintk("After Startup.\n");
  260. if (!send_status) {
  261. /*
  262. * allow APs to start initializing.
  263. */
  264. Dprintk("Before Callout %d.\n", cpu_id);
  265. cpumask_set_cpu(cpu_id, &cpu_callout_map);
  266. Dprintk("After Callout %d.\n", cpu_id);
  267. /*
  268. * Wait 5s total for a response
  269. */
  270. for (timeout = 0; timeout < 5000; timeout++) {
  271. if (cpumask_test_cpu(cpu_id, &cpu_callin_map))
  272. break; /* It has booted */
  273. udelay(1000);
  274. }
  275. if (cpumask_test_cpu(cpu_id, &cpu_callin_map)) {
  276. /* number CPUs logically, starting from 1 (BSP is 0) */
  277. Dprintk("OK.\n");
  278. } else {
  279. boot_status = 1;
  280. printk("Not responding.\n");
  281. }
  282. } else
  283. printk("IPI never delivered???\n");
  284. if (send_status || boot_status) {
  285. unmap_cpu_to_physid(cpu_id, phys_id);
  286. cpumask_clear_cpu(cpu_id, &cpu_callout_map);
  287. cpumask_clear_cpu(cpu_id, &cpu_callin_map);
  288. cpumask_clear_cpu(cpu_id, &cpu_initialized);
  289. cpucount--;
  290. }
  291. }
  292. int __cpu_up(unsigned int cpu_id, struct task_struct *tidle)
  293. {
  294. int timeout;
  295. cpumask_set_cpu(cpu_id, &smp_commenced_mask);
  296. /*
  297. * Wait 5s total for a response
  298. */
  299. for (timeout = 0; timeout < 5000; timeout++) {
  300. if (cpu_online(cpu_id))
  301. break;
  302. udelay(1000);
  303. }
  304. if (!cpu_online(cpu_id))
  305. BUG();
  306. return 0;
  307. }
  308. void __init smp_cpus_done(unsigned int max_cpus)
  309. {
  310. int cpu_id, timeout;
  311. unsigned long bogosum = 0;
  312. for (timeout = 0; timeout < 5000; timeout++) {
  313. if (cpumask_equal(&cpu_callin_map, cpu_online_mask))
  314. break;
  315. udelay(1000);
  316. }
  317. if (!cpumask_equal(&cpu_callin_map, cpu_online_mask))
  318. BUG();
  319. for_each_online_cpu(cpu_id)
  320. show_cpu_info(cpu_id);
  321. /*
  322. * Allow the user to impress friends.
  323. */
  324. Dprintk("Before bogomips.\n");
  325. if (cpucount) {
  326. for_each_cpu(cpu_id,cpu_online_mask)
  327. bogosum += cpu_data[cpu_id].loops_per_jiffy;
  328. printk(KERN_INFO "Total of %d processors activated " \
  329. "(%lu.%02lu BogoMIPS).\n", cpucount + 1,
  330. bogosum / (500000 / HZ),
  331. (bogosum / (5000 / HZ)) % 100);
  332. Dprintk("Before bogocount - setting activated=1.\n");
  333. }
  334. }
  335. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  336. /* Activate a secondary processor Routines */
  337. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  338. /*==========================================================================*
  339. * Name: start_secondary
  340. *
  341. * Description: This routine activate a secondary processor.
  342. *
  343. * Born on Date: 2002.02.05
  344. *
  345. * Arguments: *unused - currently unused.
  346. *
  347. * Returns: void (cannot fail)
  348. *
  349. * Modification log:
  350. * Date Who Description
  351. * ---------- --- --------------------------------------------------------
  352. * 2003-06-24 hy modify for linux-2.5.69
  353. *
  354. *==========================================================================*/
  355. int __init start_secondary(void *unused)
  356. {
  357. cpu_init();
  358. preempt_disable();
  359. smp_callin();
  360. while (!cpumask_test_cpu(smp_processor_id(), &smp_commenced_mask))
  361. cpu_relax();
  362. smp_online();
  363. /*
  364. * low-memory mappings have been cleared, flush them from
  365. * the local TLBs too.
  366. */
  367. local_flush_tlb_all();
  368. cpu_startup_entry(CPUHP_ONLINE);
  369. return 0;
  370. }
  371. /*==========================================================================*
  372. * Name: smp_callin
  373. *
  374. * Description: This routine activate a secondary processor.
  375. *
  376. * Born on Date: 2002.02.05
  377. *
  378. * Arguments: NONE
  379. *
  380. * Returns: void (cannot fail)
  381. *
  382. * Modification log:
  383. * Date Who Description
  384. * ---------- --- --------------------------------------------------------
  385. * 2003-06-24 hy modify for linux-2.5.69
  386. *
  387. *==========================================================================*/
  388. static void __init smp_callin(void)
  389. {
  390. int phys_id = hard_smp_processor_id();
  391. int cpu_id = smp_processor_id();
  392. unsigned long timeout;
  393. if (cpumask_test_cpu(cpu_id, &cpu_callin_map)) {
  394. printk("huh, phys CPU#%d, CPU#%d already present??\n",
  395. phys_id, cpu_id);
  396. BUG();
  397. }
  398. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpu_id, phys_id);
  399. /* Waiting 2s total for startup (udelay is not yet working) */
  400. timeout = jiffies + (2 * HZ);
  401. while (time_before(jiffies, timeout)) {
  402. /* Has the boot CPU finished it's STARTUP sequence ? */
  403. if (cpumask_test_cpu(cpu_id, &cpu_callout_map))
  404. break;
  405. cpu_relax();
  406. }
  407. if (!time_before(jiffies, timeout)) {
  408. printk("BUG: CPU#%d started up but did not get a callout!\n",
  409. cpu_id);
  410. BUG();
  411. }
  412. /* Allow the master to continue. */
  413. cpumask_set_cpu(cpu_id, &cpu_callin_map);
  414. }
  415. static void __init smp_online(void)
  416. {
  417. int cpu_id = smp_processor_id();
  418. notify_cpu_starting(cpu_id);
  419. local_irq_enable();
  420. /* Get our bogomips. */
  421. calibrate_delay();
  422. /* Save our processor parameters */
  423. smp_store_cpu_info(cpu_id);
  424. set_cpu_online(cpu_id, true);
  425. }
  426. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  427. /* Boot up CPUs common Routines */
  428. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  429. static void __init show_mp_info(int nr_cpu)
  430. {
  431. int i;
  432. char cpu_model0[17], cpu_model1[17], cpu_ver[9];
  433. strncpy(cpu_model0, (char *)M32R_FPGA_CPU_NAME_ADDR, 16);
  434. strncpy(cpu_model1, (char *)M32R_FPGA_MODEL_ID_ADDR, 16);
  435. strncpy(cpu_ver, (char *)M32R_FPGA_VERSION_ADDR, 8);
  436. cpu_model0[16] = '\0';
  437. for (i = 15 ; i >= 0 ; i--) {
  438. if (cpu_model0[i] != ' ')
  439. break;
  440. cpu_model0[i] = '\0';
  441. }
  442. cpu_model1[16] = '\0';
  443. for (i = 15 ; i >= 0 ; i--) {
  444. if (cpu_model1[i] != ' ')
  445. break;
  446. cpu_model1[i] = '\0';
  447. }
  448. cpu_ver[8] = '\0';
  449. for (i = 7 ; i >= 0 ; i--) {
  450. if (cpu_ver[i] != ' ')
  451. break;
  452. cpu_ver[i] = '\0';
  453. }
  454. printk(KERN_INFO "M32R-mp information\n");
  455. printk(KERN_INFO " On-chip CPUs : %d\n", nr_cpu);
  456. printk(KERN_INFO " CPU model : %s/%s(%s)\n", cpu_model0,
  457. cpu_model1, cpu_ver);
  458. }
  459. /*
  460. * The bootstrap kernel entry code has set these up. Save them for
  461. * a given CPU
  462. */
  463. static void __init smp_store_cpu_info(int cpu_id)
  464. {
  465. struct cpuinfo_m32r *ci = cpu_data + cpu_id;
  466. *ci = boot_cpu_data;
  467. ci->loops_per_jiffy = loops_per_jiffy;
  468. }
  469. static void __init show_cpu_info(int cpu_id)
  470. {
  471. struct cpuinfo_m32r *ci = &cpu_data[cpu_id];
  472. printk("CPU#%d : ", cpu_id);
  473. #define PRINT_CLOCK(name, value) \
  474. printk(name " clock %d.%02dMHz", \
  475. ((value) / 1000000), ((value) % 1000000) / 10000)
  476. PRINT_CLOCK("CPU", (int)ci->cpu_clock);
  477. PRINT_CLOCK(", Bus", (int)ci->bus_clock);
  478. printk(", loops_per_jiffy[%ld]\n", ci->loops_per_jiffy);
  479. }
  480. /*
  481. * the frequency of the profiling timer can be changed
  482. * by writing a multiplier value into /proc/profile.
  483. */
  484. int setup_profiling_timer(unsigned int multiplier)
  485. {
  486. int i;
  487. /*
  488. * Sanity check. [at least 500 APIC cycles should be
  489. * between APIC interrupts as a rule of thumb, to avoid
  490. * irqs flooding us]
  491. */
  492. if ( (!multiplier) || (calibration_result / multiplier < 500))
  493. return -EINVAL;
  494. /*
  495. * Set the new multiplier for each CPU. CPUs don't start using the
  496. * new values until the next timer interrupt in which they do process
  497. * accounting. At that time they also adjust their APIC timers
  498. * accordingly.
  499. */
  500. for_each_possible_cpu(i)
  501. per_cpu(prof_multiplier, i) = multiplier;
  502. return 0;
  503. }
  504. /* Initialize all maps between cpu number and apicids */
  505. static void __init init_cpu_to_physid(void)
  506. {
  507. int i;
  508. for (i = 0 ; i < NR_CPUS ; i++) {
  509. cpu_2_physid[i] = -1;
  510. physid_2_cpu[i] = -1;
  511. }
  512. }
  513. /*
  514. * set up a mapping between cpu and apicid. Uses logical apicids for multiquad,
  515. * else physical apic ids
  516. */
  517. static void __init map_cpu_to_physid(int cpu_id, int phys_id)
  518. {
  519. physid_2_cpu[phys_id] = cpu_id;
  520. cpu_2_physid[cpu_id] = phys_id;
  521. }
  522. /*
  523. * undo a mapping between cpu and apicid. Uses logical apicids for multiquad,
  524. * else physical apic ids
  525. */
  526. static void __init unmap_cpu_to_physid(int cpu_id, int phys_id)
  527. {
  528. physid_2_cpu[phys_id] = -1;
  529. cpu_2_physid[cpu_id] = -1;
  530. }