smpboot.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  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/config.h>
  42. #include <linux/init.h>
  43. #include <linux/mm.h>
  44. #include <linux/smp_lock.h>
  45. #include <linux/irq.h>
  46. #include <linux/bootmem.h>
  47. #include <linux/delay.h>
  48. #include <asm/io.h>
  49. #include <asm/pgalloc.h>
  50. #include <asm/tlbflush.h>
  51. #define DEBUG_SMP
  52. #ifdef DEBUG_SMP
  53. #define Dprintk(x...) printk(x)
  54. #else
  55. #define Dprintk(x...)
  56. #endif
  57. extern cpumask_t cpu_initialized;
  58. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  59. /* Data structures and variables */
  60. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  61. /* Processor that is doing the boot up */
  62. static unsigned int bsp_phys_id = -1;
  63. /* Bitmask of physically existing CPUs */
  64. physid_mask_t phys_cpu_present_map;
  65. /* Bitmask of currently online CPUs */
  66. cpumask_t cpu_online_map;
  67. cpumask_t cpu_bootout_map;
  68. cpumask_t cpu_bootin_map;
  69. cpumask_t cpu_callout_map;
  70. static cpumask_t cpu_callin_map;
  71. /* Per CPU bogomips and other parameters */
  72. struct cpuinfo_m32r cpu_data[NR_CPUS] __cacheline_aligned;
  73. static int cpucount;
  74. static cpumask_t smp_commenced_mask;
  75. extern struct {
  76. void * spi;
  77. unsigned short ss;
  78. } stack_start;
  79. /* which physical physical ID maps to which logical CPU number */
  80. static volatile int physid_2_cpu[NR_CPUS];
  81. /* which logical CPU number maps to which physical ID */
  82. volatile int cpu_2_physid[NR_CPUS];
  83. DEFINE_PER_CPU(int, prof_multiplier) = 1;
  84. DEFINE_PER_CPU(int, prof_old_multiplier) = 1;
  85. DEFINE_PER_CPU(int, prof_counter) = 1;
  86. spinlock_t ipi_lock[NR_IPIS];
  87. static unsigned int calibration_result;
  88. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  89. /* Function Prototypes */
  90. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  91. void smp_prepare_boot_cpu(void);
  92. void smp_prepare_cpus(unsigned int);
  93. static void smp_tune_scheduling(void);
  94. static void init_ipi_lock(void);
  95. static void do_boot_cpu(int);
  96. int __cpu_up(unsigned int);
  97. void smp_cpus_done(unsigned int);
  98. int start_secondary(void *);
  99. static void smp_callin(void);
  100. static void smp_online(void);
  101. static void show_mp_info(int);
  102. static void smp_store_cpu_info(int);
  103. static void show_cpu_info(int);
  104. int setup_profiling_timer(unsigned int);
  105. static void init_cpu_to_physid(void);
  106. static void map_cpu_to_physid(int, int);
  107. static void unmap_cpu_to_physid(int, int);
  108. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  109. /* Boot up APs Routins : BSP */
  110. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  111. void __devinit smp_prepare_boot_cpu(void)
  112. {
  113. bsp_phys_id = hard_smp_processor_id();
  114. physid_set(bsp_phys_id, phys_cpu_present_map);
  115. cpu_set(0, cpu_online_map); /* BSP's cpu_id == 0 */
  116. cpu_set(0, cpu_callout_map);
  117. cpu_set(0, cpu_callin_map);
  118. /*
  119. * Initialize the logical to physical CPU number mapping
  120. */
  121. init_cpu_to_physid();
  122. map_cpu_to_physid(0, bsp_phys_id);
  123. current_thread_info()->cpu = 0;
  124. }
  125. /*==========================================================================*
  126. * Name: smp_prepare_cpus (old smp_boot_cpus)
  127. *
  128. * Description: This routine boot up APs.
  129. *
  130. * Born on Date: 2002.02.05
  131. *
  132. * Arguments: NONE
  133. *
  134. * Returns: void (cannot fail)
  135. *
  136. * Modification log:
  137. * Date Who Description
  138. * ---------- --- --------------------------------------------------------
  139. * 2003-06-24 hy modify for linux-2.5.69
  140. *
  141. *==========================================================================*/
  142. void __init smp_prepare_cpus(unsigned int max_cpus)
  143. {
  144. int phys_id;
  145. unsigned long nr_cpu;
  146. nr_cpu = inl(M32R_FPGA_NUM_OF_CPUS_PORTL);
  147. if (nr_cpu > NR_CPUS) {
  148. printk(KERN_INFO "NUM_OF_CPUS reg. value [%ld] > NR_CPU [%d]",
  149. nr_cpu, NR_CPUS);
  150. goto smp_done;
  151. }
  152. for (phys_id = 0 ; phys_id < nr_cpu ; phys_id++)
  153. physid_set(phys_id, phys_cpu_present_map);
  154. show_mp_info(nr_cpu);
  155. init_ipi_lock();
  156. /*
  157. * Setup boot CPU information
  158. */
  159. smp_store_cpu_info(0); /* Final full version of the data */
  160. smp_tune_scheduling();
  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 >= 0) && (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. static void __init smp_tune_scheduling(void)
  196. {
  197. /* Nothing to do. */
  198. }
  199. /*
  200. * init_ipi_lock : Initialize IPI locks.
  201. */
  202. static void __init init_ipi_lock(void)
  203. {
  204. int ipi;
  205. for (ipi = 0 ; ipi < NR_IPIS ; ipi++)
  206. spin_lock_init(&ipi_lock[ipi]);
  207. }
  208. /*==========================================================================*
  209. * Name: do_boot_cpu
  210. *
  211. * Description: This routine boot up one AP.
  212. *
  213. * Born on Date: 2002.02.05
  214. *
  215. * Arguments: phys_id - Target CPU physical ID
  216. *
  217. * Returns: void (cannot fail)
  218. *
  219. * Modification log:
  220. * Date Who Description
  221. * ---------- --- --------------------------------------------------------
  222. * 2003-06-24 hy modify for linux-2.5.69
  223. *
  224. *==========================================================================*/
  225. static void __init do_boot_cpu(int phys_id)
  226. {
  227. struct task_struct *idle;
  228. unsigned long send_status, boot_status;
  229. int timeout, cpu_id;
  230. cpu_id = ++cpucount;
  231. /*
  232. * We can't use kernel_thread since we must avoid to
  233. * reschedule the child.
  234. */
  235. idle = fork_idle(cpu_id);
  236. if (IS_ERR(idle))
  237. panic("failed fork for CPU#%d.", cpu_id);
  238. idle->thread.lr = (unsigned long)start_secondary;
  239. map_cpu_to_physid(cpu_id, phys_id);
  240. /* So we see what's up */
  241. printk("Booting processor %d/%d\n", phys_id, cpu_id);
  242. stack_start.spi = (void *)idle->thread.sp;
  243. idle->thread_info->cpu = cpu_id;
  244. /*
  245. * Send Startup IPI
  246. * 1.IPI received by CPU#(phys_id).
  247. * 2.CPU#(phys_id) enter startup_AP (arch/m32r/kernel/head.S)
  248. * 3.CPU#(phys_id) enter start_secondary()
  249. */
  250. send_status = 0;
  251. boot_status = 0;
  252. cpu_set(phys_id, cpu_bootout_map);
  253. /* Send Startup IPI */
  254. send_IPI_mask_phys(cpumask_of_cpu(phys_id), CPU_BOOT_IPI, 0);
  255. Dprintk("Waiting for send to finish...\n");
  256. timeout = 0;
  257. /* Wait 100[ms] */
  258. do {
  259. Dprintk("+");
  260. udelay(1000);
  261. send_status = !cpu_isset(phys_id, cpu_bootin_map);
  262. } while (send_status && (timeout++ < 100));
  263. Dprintk("After Startup.\n");
  264. if (!send_status) {
  265. /*
  266. * allow APs to start initializing.
  267. */
  268. Dprintk("Before Callout %d.\n", cpu_id);
  269. cpu_set(cpu_id, cpu_callout_map);
  270. Dprintk("After Callout %d.\n", cpu_id);
  271. /*
  272. * Wait 5s total for a response
  273. */
  274. for (timeout = 0; timeout < 5000; timeout++) {
  275. if (cpu_isset(cpu_id, cpu_callin_map))
  276. break; /* It has booted */
  277. udelay(1000);
  278. }
  279. if (cpu_isset(cpu_id, cpu_callin_map)) {
  280. /* number CPUs logically, starting from 1 (BSP is 0) */
  281. Dprintk("OK.\n");
  282. } else {
  283. boot_status = 1;
  284. printk("Not responding.\n");
  285. }
  286. } else
  287. printk("IPI never delivered???\n");
  288. if (send_status || boot_status) {
  289. unmap_cpu_to_physid(cpu_id, phys_id);
  290. cpu_clear(cpu_id, cpu_callout_map);
  291. cpu_clear(cpu_id, cpu_callin_map);
  292. cpu_clear(cpu_id, cpu_initialized);
  293. cpucount--;
  294. }
  295. }
  296. int __devinit __cpu_up(unsigned int cpu_id)
  297. {
  298. int timeout;
  299. cpu_set(cpu_id, smp_commenced_mask);
  300. /*
  301. * Wait 5s total for a response
  302. */
  303. for (timeout = 0; timeout < 5000; timeout++) {
  304. if (cpu_isset(cpu_id, cpu_online_map))
  305. break;
  306. udelay(1000);
  307. }
  308. if (!cpu_isset(cpu_id, cpu_online_map))
  309. BUG();
  310. return 0;
  311. }
  312. void __init smp_cpus_done(unsigned int max_cpus)
  313. {
  314. int cpu_id, timeout;
  315. unsigned long bogosum = 0;
  316. for (timeout = 0; timeout < 5000; timeout++) {
  317. if (cpus_equal(cpu_callin_map, cpu_online_map))
  318. break;
  319. udelay(1000);
  320. }
  321. if (!cpus_equal(cpu_callin_map, cpu_online_map))
  322. BUG();
  323. for (cpu_id = 0 ; cpu_id < num_online_cpus() ; cpu_id++)
  324. show_cpu_info(cpu_id);
  325. /*
  326. * Allow the user to impress friends.
  327. */
  328. Dprintk("Before bogomips.\n");
  329. if (cpucount) {
  330. for_each_cpu_mask(cpu_id, cpu_online_map)
  331. bogosum += cpu_data[cpu_id].loops_per_jiffy;
  332. printk(KERN_INFO "Total of %d processors activated " \
  333. "(%lu.%02lu BogoMIPS).\n", cpucount + 1,
  334. bogosum / (500000 / HZ),
  335. (bogosum / (5000 / HZ)) % 100);
  336. Dprintk("Before bogocount - setting activated=1.\n");
  337. }
  338. }
  339. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  340. /* Activate a secondary processor Routins */
  341. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  342. /*==========================================================================*
  343. * Name: start_secondary
  344. *
  345. * Description: This routine activate a secondary processor.
  346. *
  347. * Born on Date: 2002.02.05
  348. *
  349. * Arguments: *unused - currently unused.
  350. *
  351. * Returns: void (cannot fail)
  352. *
  353. * Modification log:
  354. * Date Who Description
  355. * ---------- --- --------------------------------------------------------
  356. * 2003-06-24 hy modify for linux-2.5.69
  357. *
  358. *==========================================================================*/
  359. int __init start_secondary(void *unused)
  360. {
  361. cpu_init();
  362. smp_callin();
  363. while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
  364. cpu_relax();
  365. smp_online();
  366. /*
  367. * low-memory mappings have been cleared, flush them from
  368. * the local TLBs too.
  369. */
  370. local_flush_tlb_all();
  371. cpu_idle();
  372. return 0;
  373. }
  374. /*==========================================================================*
  375. * Name: smp_callin
  376. *
  377. * Description: This routine activate a secondary processor.
  378. *
  379. * Born on Date: 2002.02.05
  380. *
  381. * Arguments: NONE
  382. *
  383. * Returns: void (cannot fail)
  384. *
  385. * Modification log:
  386. * Date Who Description
  387. * ---------- --- --------------------------------------------------------
  388. * 2003-06-24 hy modify for linux-2.5.69
  389. *
  390. *==========================================================================*/
  391. static void __init smp_callin(void)
  392. {
  393. int phys_id = hard_smp_processor_id();
  394. int cpu_id = smp_processor_id();
  395. unsigned long timeout;
  396. if (cpu_isset(cpu_id, cpu_callin_map)) {
  397. printk("huh, phys CPU#%d, CPU#%d already present??\n",
  398. phys_id, cpu_id);
  399. BUG();
  400. }
  401. Dprintk("CPU#%d (phys ID: %d) waiting for CALLOUT\n", cpu_id, phys_id);
  402. /* Waiting 2s total for startup (udelay is not yet working) */
  403. timeout = jiffies + (2 * HZ);
  404. while (time_before(jiffies, timeout)) {
  405. /* Has the boot CPU finished it's STARTUP sequence ? */
  406. if (cpu_isset(cpu_id, cpu_callout_map))
  407. break;
  408. cpu_relax();
  409. }
  410. if (!time_before(jiffies, timeout)) {
  411. printk("BUG: CPU#%d started up but did not get a callout!\n",
  412. cpu_id);
  413. BUG();
  414. }
  415. /* Allow the master to continue. */
  416. cpu_set(cpu_id, cpu_callin_map);
  417. }
  418. static void __init smp_online(void)
  419. {
  420. int cpu_id = smp_processor_id();
  421. local_irq_enable();
  422. /* Get our bogomips. */
  423. calibrate_delay();
  424. /* Save our processor parameters */
  425. smp_store_cpu_info(cpu_id);
  426. cpu_set(cpu_id, cpu_online_map);
  427. }
  428. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  429. /* Boot up CPUs common Routins */
  430. /*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*/
  431. static void __init show_mp_info(int nr_cpu)
  432. {
  433. int i;
  434. char cpu_model0[17], cpu_model1[17], cpu_ver[9];
  435. strncpy(cpu_model0, (char *)M32R_FPGA_CPU_NAME_ADDR, 16);
  436. strncpy(cpu_model1, (char *)M32R_FPGA_MODEL_ID_ADDR, 16);
  437. strncpy(cpu_ver, (char *)M32R_FPGA_VERSION_ADDR, 8);
  438. cpu_model0[16] = '\0';
  439. for (i = 15 ; i >= 0 ; i--) {
  440. if (cpu_model0[i] != ' ')
  441. break;
  442. cpu_model0[i] = '\0';
  443. }
  444. cpu_model1[16] = '\0';
  445. for (i = 15 ; i >= 0 ; i--) {
  446. if (cpu_model1[i] != ' ')
  447. break;
  448. cpu_model1[i] = '\0';
  449. }
  450. cpu_ver[8] = '\0';
  451. for (i = 7 ; i >= 0 ; i--) {
  452. if (cpu_ver[i] != ' ')
  453. break;
  454. cpu_ver[i] = '\0';
  455. }
  456. printk(KERN_INFO "M32R-mp information\n");
  457. printk(KERN_INFO " On-chip CPUs : %d\n", nr_cpu);
  458. printk(KERN_INFO " CPU model : %s/%s(%s)\n", cpu_model0,
  459. cpu_model1, cpu_ver);
  460. }
  461. /*
  462. * The bootstrap kernel entry code has set these up. Save them for
  463. * a given CPU
  464. */
  465. static void __init smp_store_cpu_info(int cpu_id)
  466. {
  467. struct cpuinfo_m32r *ci = cpu_data + cpu_id;
  468. *ci = boot_cpu_data;
  469. ci->loops_per_jiffy = loops_per_jiffy;
  470. }
  471. static void __init show_cpu_info(int cpu_id)
  472. {
  473. struct cpuinfo_m32r *ci = &cpu_data[cpu_id];
  474. printk("CPU#%d : ", cpu_id);
  475. #define PRINT_CLOCK(name, value) \
  476. printk(name " clock %d.%02dMHz", \
  477. ((value) / 1000000), ((value) % 1000000) / 10000)
  478. PRINT_CLOCK("CPU", (int)ci->cpu_clock);
  479. PRINT_CLOCK(", Bus", (int)ci->bus_clock);
  480. printk(", loops_per_jiffy[%ld]\n", ci->loops_per_jiffy);
  481. }
  482. /*
  483. * the frequency of the profiling timer can be changed
  484. * by writing a multiplier value into /proc/profile.
  485. */
  486. int setup_profiling_timer(unsigned int multiplier)
  487. {
  488. int i;
  489. /*
  490. * Sanity check. [at least 500 APIC cycles should be
  491. * between APIC interrupts as a rule of thumb, to avoid
  492. * irqs flooding us]
  493. */
  494. if ( (!multiplier) || (calibration_result / multiplier < 500))
  495. return -EINVAL;
  496. /*
  497. * Set the new multiplier for each CPU. CPUs don't start using the
  498. * new values until the next timer interrupt in which they do process
  499. * accounting. At that time they also adjust their APIC timers
  500. * accordingly.
  501. */
  502. for (i = 0; i < NR_CPUS; ++i)
  503. per_cpu(prof_multiplier, i) = multiplier;
  504. return 0;
  505. }
  506. /* Initialize all maps between cpu number and apicids */
  507. static void __init init_cpu_to_physid(void)
  508. {
  509. int i;
  510. for (i = 0 ; i < NR_CPUS ; i++) {
  511. cpu_2_physid[i] = -1;
  512. physid_2_cpu[i] = -1;
  513. }
  514. }
  515. /*
  516. * set up a mapping between cpu and apicid. Uses logical apicids for multiquad,
  517. * else physical apic ids
  518. */
  519. static void __init map_cpu_to_physid(int cpu_id, int phys_id)
  520. {
  521. physid_2_cpu[phys_id] = cpu_id;
  522. cpu_2_physid[cpu_id] = phys_id;
  523. }
  524. /*
  525. * undo a mapping between cpu and apicid. Uses logical apicids for multiquad,
  526. * else physical apic ids
  527. */
  528. static void __init unmap_cpu_to_physid(int cpu_id, int phys_id)
  529. {
  530. physid_2_cpu[phys_id] = -1;
  531. cpu_2_physid[cpu_id] = -1;
  532. }