smpboot.c 16 KB

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