smp.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. /* SMP support routines.
  2. *
  3. * Copyright (C) 2006-2008 Panasonic Corporation
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/interrupt.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/init.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/cpumask.h>
  20. #include <linux/err.h>
  21. #include <linux/kernel.h>
  22. #include <linux/delay.h>
  23. #include <linux/sched.h>
  24. #include <linux/profile.h>
  25. #include <linux/smp.h>
  26. #include <asm/tlbflush.h>
  27. #include <asm/system.h>
  28. #include <asm/bitops.h>
  29. #include <asm/processor.h>
  30. #include <asm/bug.h>
  31. #include <asm/exceptions.h>
  32. #include <asm/hardirq.h>
  33. #include <asm/fpu.h>
  34. #include <asm/mmu_context.h>
  35. #include <asm/thread_info.h>
  36. #include <asm/cpu-regs.h>
  37. #include <asm/intctl-regs.h>
  38. #include "internal.h"
  39. #ifdef CONFIG_HOTPLUG_CPU
  40. #include <linux/cpu.h>
  41. #include <asm/cacheflush.h>
  42. static unsigned long sleep_mode[NR_CPUS];
  43. static void run_sleep_cpu(unsigned int cpu);
  44. static void run_wakeup_cpu(unsigned int cpu);
  45. #endif /* CONFIG_HOTPLUG_CPU */
  46. /*
  47. * Debug Message function
  48. */
  49. #undef DEBUG_SMP
  50. #ifdef DEBUG_SMP
  51. #define Dprintk(fmt, ...) printk(KERN_DEBUG fmt, ##__VA_ARGS__)
  52. #else
  53. #define Dprintk(fmt, ...) no_printk(KERN_DEBUG fmt, ##__VA_ARGS__)
  54. #endif
  55. /* timeout value in msec for smp_nmi_call_function. zero is no timeout. */
  56. #define CALL_FUNCTION_NMI_IPI_TIMEOUT 0
  57. /*
  58. * Structure and data for smp_nmi_call_function().
  59. */
  60. struct nmi_call_data_struct {
  61. smp_call_func_t func;
  62. void *info;
  63. cpumask_t started;
  64. cpumask_t finished;
  65. int wait;
  66. char size_alignment[0]
  67. __attribute__ ((__aligned__(SMP_CACHE_BYTES)));
  68. } __attribute__ ((__aligned__(SMP_CACHE_BYTES)));
  69. static DEFINE_SPINLOCK(smp_nmi_call_lock);
  70. static struct nmi_call_data_struct *nmi_call_data;
  71. /*
  72. * Data structures and variables
  73. */
  74. static cpumask_t cpu_callin_map; /* Bitmask of callin CPUs */
  75. static cpumask_t cpu_callout_map; /* Bitmask of callout CPUs */
  76. cpumask_t cpu_boot_map; /* Bitmask of boot APs */
  77. unsigned long start_stack[NR_CPUS - 1];
  78. /*
  79. * Per CPU parameters
  80. */
  81. struct mn10300_cpuinfo cpu_data[NR_CPUS] __cacheline_aligned;
  82. static int cpucount; /* The count of boot CPUs */
  83. static cpumask_t smp_commenced_mask;
  84. cpumask_t cpu_initialized __initdata = CPU_MASK_NONE;
  85. /*
  86. * Function Prototypes
  87. */
  88. static int do_boot_cpu(int);
  89. static void smp_show_cpu_info(int cpu_id);
  90. static void smp_callin(void);
  91. static void smp_online(void);
  92. static void smp_store_cpu_info(int);
  93. static void smp_cpu_init(void);
  94. static void smp_tune_scheduling(void);
  95. static void send_IPI_mask(const cpumask_t *cpumask, int irq);
  96. static void init_ipi(void);
  97. /*
  98. * IPI Initialization interrupt definitions
  99. */
  100. static void mn10300_ipi_disable(unsigned int irq);
  101. static void mn10300_ipi_enable(unsigned int irq);
  102. static void mn10300_ipi_ack(unsigned int irq);
  103. static void mn10300_ipi_nop(unsigned int irq);
  104. static struct irq_chip mn10300_ipi_type = {
  105. .name = "cpu_ipi",
  106. .disable = mn10300_ipi_disable,
  107. .enable = mn10300_ipi_enable,
  108. .ack = mn10300_ipi_ack,
  109. .eoi = mn10300_ipi_nop
  110. };
  111. static irqreturn_t smp_reschedule_interrupt(int irq, void *dev_id);
  112. static irqreturn_t smp_call_function_interrupt(int irq, void *dev_id);
  113. static irqreturn_t smp_ipi_timer_interrupt(int irq, void *dev_id);
  114. static struct irqaction reschedule_ipi = {
  115. .handler = smp_reschedule_interrupt,
  116. .name = "smp reschedule IPI"
  117. };
  118. static struct irqaction call_function_ipi = {
  119. .handler = smp_call_function_interrupt,
  120. .name = "smp call function IPI"
  121. };
  122. static struct irqaction local_timer_ipi = {
  123. .handler = smp_ipi_timer_interrupt,
  124. .flags = IRQF_DISABLED,
  125. .name = "smp local timer IPI"
  126. };
  127. /**
  128. * init_ipi - Initialise the IPI mechanism
  129. */
  130. static void init_ipi(void)
  131. {
  132. unsigned long flags;
  133. u16 tmp16;
  134. /* set up the reschedule IPI */
  135. set_irq_chip_and_handler(RESCHEDULE_IPI,
  136. &mn10300_ipi_type, handle_percpu_irq);
  137. setup_irq(RESCHEDULE_IPI, &reschedule_ipi);
  138. set_intr_level(RESCHEDULE_IPI, RESCHEDULE_GxICR_LV);
  139. mn10300_ipi_enable(RESCHEDULE_IPI);
  140. /* set up the call function IPI */
  141. set_irq_chip_and_handler(CALL_FUNC_SINGLE_IPI,
  142. &mn10300_ipi_type, handle_percpu_irq);
  143. setup_irq(CALL_FUNC_SINGLE_IPI, &call_function_ipi);
  144. set_intr_level(CALL_FUNC_SINGLE_IPI, CALL_FUNCTION_GxICR_LV);
  145. mn10300_ipi_enable(CALL_FUNC_SINGLE_IPI);
  146. /* set up the local timer IPI */
  147. set_irq_chip_and_handler(LOCAL_TIMER_IPI,
  148. &mn10300_ipi_type, handle_percpu_irq);
  149. setup_irq(LOCAL_TIMER_IPI, &local_timer_ipi);
  150. set_intr_level(LOCAL_TIMER_IPI, LOCAL_TIMER_GxICR_LV);
  151. mn10300_ipi_enable(LOCAL_TIMER_IPI);
  152. #ifdef CONFIG_MN10300_CACHE_ENABLED
  153. /* set up the cache flush IPI */
  154. flags = arch_local_cli_save();
  155. __set_intr_stub(NUM2EXCEP_IRQ_LEVEL(FLUSH_CACHE_GxICR_LV),
  156. mn10300_low_ipi_handler);
  157. GxICR(FLUSH_CACHE_IPI) = FLUSH_CACHE_GxICR_LV | GxICR_DETECT;
  158. mn10300_ipi_enable(FLUSH_CACHE_IPI);
  159. arch_local_irq_restore(flags);
  160. #endif
  161. /* set up the NMI call function IPI */
  162. flags = arch_local_cli_save();
  163. GxICR(CALL_FUNCTION_NMI_IPI) = GxICR_NMI | GxICR_ENABLE | GxICR_DETECT;
  164. tmp16 = GxICR(CALL_FUNCTION_NMI_IPI);
  165. arch_local_irq_restore(flags);
  166. /* set up the SMP boot IPI */
  167. flags = arch_local_cli_save();
  168. __set_intr_stub(NUM2EXCEP_IRQ_LEVEL(SMP_BOOT_GxICR_LV),
  169. mn10300_low_ipi_handler);
  170. arch_local_irq_restore(flags);
  171. }
  172. /**
  173. * mn10300_ipi_shutdown - Shut down handling of an IPI
  174. * @irq: The IPI to be shut down.
  175. */
  176. static void mn10300_ipi_shutdown(unsigned int irq)
  177. {
  178. unsigned long flags;
  179. u16 tmp;
  180. flags = arch_local_cli_save();
  181. tmp = GxICR(irq);
  182. GxICR(irq) = (tmp & GxICR_LEVEL) | GxICR_DETECT;
  183. tmp = GxICR(irq);
  184. arch_local_irq_restore(flags);
  185. }
  186. /**
  187. * mn10300_ipi_enable - Enable an IPI
  188. * @irq: The IPI to be enabled.
  189. */
  190. static void mn10300_ipi_enable(unsigned int irq)
  191. {
  192. unsigned long flags;
  193. u16 tmp;
  194. flags = arch_local_cli_save();
  195. tmp = GxICR(irq);
  196. GxICR(irq) = (tmp & GxICR_LEVEL) | GxICR_ENABLE;
  197. tmp = GxICR(irq);
  198. arch_local_irq_restore(flags);
  199. }
  200. /**
  201. * mn10300_ipi_disable - Disable an IPI
  202. * @irq: The IPI to be disabled.
  203. */
  204. static void mn10300_ipi_disable(unsigned int irq)
  205. {
  206. unsigned long flags;
  207. u16 tmp;
  208. flags = arch_local_cli_save();
  209. tmp = GxICR(irq);
  210. GxICR(irq) = tmp & GxICR_LEVEL;
  211. tmp = GxICR(irq);
  212. arch_local_irq_restore(flags);
  213. }
  214. /**
  215. * mn10300_ipi_ack - Acknowledge an IPI interrupt in the PIC
  216. * @irq: The IPI to be acknowledged.
  217. *
  218. * Clear the interrupt detection flag for the IPI on the appropriate interrupt
  219. * channel in the PIC.
  220. */
  221. static void mn10300_ipi_ack(unsigned int irq)
  222. {
  223. unsigned long flags;
  224. u16 tmp;
  225. flags = arch_local_cli_save();
  226. GxICR_u8(irq) = GxICR_DETECT;
  227. tmp = GxICR(irq);
  228. arch_local_irq_restore(flags);
  229. }
  230. /**
  231. * mn10300_ipi_nop - Dummy IPI action
  232. * @irq: The IPI to be acted upon.
  233. */
  234. static void mn10300_ipi_nop(unsigned int irq)
  235. {
  236. }
  237. /**
  238. * send_IPI_mask - Send IPIs to all CPUs in list
  239. * @cpumask: The list of CPUs to target.
  240. * @irq: The IPI request to be sent.
  241. *
  242. * Send the specified IPI to all the CPUs in the list, not waiting for them to
  243. * finish before returning. The caller is responsible for synchronisation if
  244. * that is needed.
  245. */
  246. static void send_IPI_mask(const cpumask_t *cpumask, int irq)
  247. {
  248. int i;
  249. u16 tmp;
  250. for (i = 0; i < NR_CPUS; i++) {
  251. if (cpu_isset(i, *cpumask)) {
  252. /* send IPI */
  253. tmp = CROSS_GxICR(irq, i);
  254. CROSS_GxICR(irq, i) =
  255. tmp | GxICR_REQUEST | GxICR_DETECT;
  256. tmp = CROSS_GxICR(irq, i); /* flush write buffer */
  257. }
  258. }
  259. }
  260. /**
  261. * send_IPI_self - Send an IPI to this CPU.
  262. * @irq: The IPI request to be sent.
  263. *
  264. * Send the specified IPI to the current CPU.
  265. */
  266. void send_IPI_self(int irq)
  267. {
  268. send_IPI_mask(cpumask_of(smp_processor_id()), irq);
  269. }
  270. /**
  271. * send_IPI_allbutself - Send IPIs to all the other CPUs.
  272. * @irq: The IPI request to be sent.
  273. *
  274. * Send the specified IPI to all CPUs in the system barring the current one,
  275. * not waiting for them to finish before returning. The caller is responsible
  276. * for synchronisation if that is needed.
  277. */
  278. void send_IPI_allbutself(int irq)
  279. {
  280. cpumask_t cpumask;
  281. cpumask = cpu_online_map;
  282. cpu_clear(smp_processor_id(), cpumask);
  283. send_IPI_mask(&cpumask, irq);
  284. }
  285. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  286. {
  287. BUG();
  288. /*send_IPI_mask(mask, CALL_FUNCTION_IPI);*/
  289. }
  290. void arch_send_call_function_single_ipi(int cpu)
  291. {
  292. send_IPI_mask(cpumask_of(cpu), CALL_FUNC_SINGLE_IPI);
  293. }
  294. /**
  295. * smp_send_reschedule - Send reschedule IPI to a CPU
  296. * @cpu: The CPU to target.
  297. */
  298. void smp_send_reschedule(int cpu)
  299. {
  300. send_IPI_mask(cpumask_of(cpu), RESCHEDULE_IPI);
  301. }
  302. /**
  303. * smp_nmi_call_function - Send a call function NMI IPI to all CPUs
  304. * @func: The function to ask to be run.
  305. * @info: The context data to pass to that function.
  306. * @wait: If true, wait (atomically) until function is run on all CPUs.
  307. *
  308. * Send a non-maskable request to all CPUs in the system, requesting them to
  309. * run the specified function with the given context data, and, potentially, to
  310. * wait for completion of that function on all CPUs.
  311. *
  312. * Returns 0 if successful, -ETIMEDOUT if we were asked to wait, but hit the
  313. * timeout.
  314. */
  315. int smp_nmi_call_function(smp_call_func_t func, void *info, int wait)
  316. {
  317. struct nmi_call_data_struct data;
  318. unsigned long flags;
  319. unsigned int cnt;
  320. int cpus, ret = 0;
  321. cpus = num_online_cpus() - 1;
  322. if (cpus < 1)
  323. return 0;
  324. data.func = func;
  325. data.info = info;
  326. data.started = cpu_online_map;
  327. cpu_clear(smp_processor_id(), data.started);
  328. data.wait = wait;
  329. if (wait)
  330. data.finished = data.started;
  331. spin_lock_irqsave(&smp_nmi_call_lock, flags);
  332. nmi_call_data = &data;
  333. smp_mb();
  334. /* Send a message to all other CPUs and wait for them to respond */
  335. send_IPI_allbutself(CALL_FUNCTION_NMI_IPI);
  336. /* Wait for response */
  337. if (CALL_FUNCTION_NMI_IPI_TIMEOUT > 0) {
  338. for (cnt = 0;
  339. cnt < CALL_FUNCTION_NMI_IPI_TIMEOUT &&
  340. !cpus_empty(data.started);
  341. cnt++)
  342. mdelay(1);
  343. if (wait && cnt < CALL_FUNCTION_NMI_IPI_TIMEOUT) {
  344. for (cnt = 0;
  345. cnt < CALL_FUNCTION_NMI_IPI_TIMEOUT &&
  346. !cpus_empty(data.finished);
  347. cnt++)
  348. mdelay(1);
  349. }
  350. if (cnt >= CALL_FUNCTION_NMI_IPI_TIMEOUT)
  351. ret = -ETIMEDOUT;
  352. } else {
  353. /* If timeout value is zero, wait until cpumask has been
  354. * cleared */
  355. while (!cpus_empty(data.started))
  356. barrier();
  357. if (wait)
  358. while (!cpus_empty(data.finished))
  359. barrier();
  360. }
  361. spin_unlock_irqrestore(&smp_nmi_call_lock, flags);
  362. return ret;
  363. }
  364. /**
  365. * stop_this_cpu - Callback to stop a CPU.
  366. * @unused: Callback context (ignored).
  367. */
  368. void stop_this_cpu(void *unused)
  369. {
  370. static volatile int stopflag;
  371. unsigned long flags;
  372. #ifdef CONFIG_GDBSTUB
  373. /* In case of single stepping smp_send_stop by other CPU,
  374. * clear procindebug to avoid deadlock.
  375. */
  376. atomic_set(&procindebug[smp_processor_id()], 0);
  377. #endif /* CONFIG_GDBSTUB */
  378. flags = arch_local_cli_save();
  379. cpu_clear(smp_processor_id(), cpu_online_map);
  380. while (!stopflag)
  381. cpu_relax();
  382. cpu_set(smp_processor_id(), cpu_online_map);
  383. arch_local_irq_restore(flags);
  384. }
  385. /**
  386. * smp_send_stop - Send a stop request to all CPUs.
  387. */
  388. void smp_send_stop(void)
  389. {
  390. smp_nmi_call_function(stop_this_cpu, NULL, 0);
  391. }
  392. /**
  393. * smp_reschedule_interrupt - Reschedule IPI handler
  394. * @irq: The interrupt number.
  395. * @dev_id: The device ID.
  396. *
  397. * We need do nothing here, since the scheduling will be effected on our way
  398. * back through entry.S.
  399. *
  400. * Returns IRQ_HANDLED to indicate we handled the interrupt successfully.
  401. */
  402. static irqreturn_t smp_reschedule_interrupt(int irq, void *dev_id)
  403. {
  404. /* do nothing */
  405. return IRQ_HANDLED;
  406. }
  407. /**
  408. * smp_call_function_interrupt - Call function IPI handler
  409. * @irq: The interrupt number.
  410. * @dev_id: The device ID.
  411. *
  412. * Returns IRQ_HANDLED to indicate we handled the interrupt successfully.
  413. */
  414. static irqreturn_t smp_call_function_interrupt(int irq, void *dev_id)
  415. {
  416. /* generic_smp_call_function_interrupt(); */
  417. generic_smp_call_function_single_interrupt();
  418. return IRQ_HANDLED;
  419. }
  420. /**
  421. * smp_nmi_call_function_interrupt - Non-maskable call function IPI handler
  422. */
  423. void smp_nmi_call_function_interrupt(void)
  424. {
  425. smp_call_func_t func = nmi_call_data->func;
  426. void *info = nmi_call_data->info;
  427. int wait = nmi_call_data->wait;
  428. /* Notify the initiating CPU that I've grabbed the data and am about to
  429. * execute the function
  430. */
  431. smp_mb();
  432. cpu_clear(smp_processor_id(), nmi_call_data->started);
  433. (*func)(info);
  434. if (wait) {
  435. smp_mb();
  436. cpu_clear(smp_processor_id(), nmi_call_data->finished);
  437. }
  438. }
  439. /**
  440. * smp_ipi_timer_interrupt - Local timer IPI handler
  441. * @irq: The interrupt number.
  442. * @dev_id: The device ID.
  443. *
  444. * Returns IRQ_HANDLED to indicate we handled the interrupt successfully.
  445. */
  446. static irqreturn_t smp_ipi_timer_interrupt(int irq, void *dev_id)
  447. {
  448. return local_timer_interrupt();
  449. }
  450. void __init smp_init_cpus(void)
  451. {
  452. int i;
  453. for (i = 0; i < NR_CPUS; i++) {
  454. set_cpu_possible(i, true);
  455. set_cpu_present(i, true);
  456. }
  457. }
  458. /**
  459. * smp_cpu_init - Initialise AP in start_secondary.
  460. *
  461. * For this Application Processor, set up init_mm, initialise FPU and set
  462. * interrupt level 0-6 setting.
  463. */
  464. static void __init smp_cpu_init(void)
  465. {
  466. unsigned long flags;
  467. int cpu_id = smp_processor_id();
  468. u16 tmp16;
  469. if (test_and_set_bit(cpu_id, &cpu_initialized)) {
  470. printk(KERN_WARNING "CPU#%d already initialized!\n", cpu_id);
  471. for (;;)
  472. local_irq_enable();
  473. }
  474. printk(KERN_INFO "Initializing CPU#%d\n", cpu_id);
  475. atomic_inc(&init_mm.mm_count);
  476. current->active_mm = &init_mm;
  477. BUG_ON(current->mm);
  478. enter_lazy_tlb(&init_mm, current);
  479. /* Force FPU initialization */
  480. clear_using_fpu(current);
  481. GxICR(CALL_FUNC_SINGLE_IPI) = CALL_FUNCTION_GxICR_LV | GxICR_DETECT;
  482. mn10300_ipi_enable(CALL_FUNC_SINGLE_IPI);
  483. GxICR(LOCAL_TIMER_IPI) = LOCAL_TIMER_GxICR_LV | GxICR_DETECT;
  484. mn10300_ipi_enable(LOCAL_TIMER_IPI);
  485. GxICR(RESCHEDULE_IPI) = RESCHEDULE_GxICR_LV | GxICR_DETECT;
  486. mn10300_ipi_enable(RESCHEDULE_IPI);
  487. #ifdef CONFIG_MN10300_CACHE_ENABLED
  488. GxICR(FLUSH_CACHE_IPI) = FLUSH_CACHE_GxICR_LV | GxICR_DETECT;
  489. mn10300_ipi_enable(FLUSH_CACHE_IPI);
  490. #endif
  491. mn10300_ipi_shutdown(SMP_BOOT_IRQ);
  492. /* Set up the non-maskable call function IPI */
  493. flags = arch_local_cli_save();
  494. GxICR(CALL_FUNCTION_NMI_IPI) = GxICR_NMI | GxICR_ENABLE | GxICR_DETECT;
  495. tmp16 = GxICR(CALL_FUNCTION_NMI_IPI);
  496. arch_local_irq_restore(flags);
  497. }
  498. /**
  499. * smp_prepare_cpu_init - Initialise CPU in startup_secondary
  500. *
  501. * Set interrupt level 0-6 setting and init ICR of gdbstub.
  502. */
  503. void smp_prepare_cpu_init(void)
  504. {
  505. int loop;
  506. /* Set the interrupt vector registers */
  507. IVAR0 = EXCEP_IRQ_LEVEL0;
  508. IVAR1 = EXCEP_IRQ_LEVEL1;
  509. IVAR2 = EXCEP_IRQ_LEVEL2;
  510. IVAR3 = EXCEP_IRQ_LEVEL3;
  511. IVAR4 = EXCEP_IRQ_LEVEL4;
  512. IVAR5 = EXCEP_IRQ_LEVEL5;
  513. IVAR6 = EXCEP_IRQ_LEVEL6;
  514. /* Disable all interrupts and set to priority 6 (lowest) */
  515. for (loop = 0; loop < GxICR_NUM_IRQS; loop++)
  516. GxICR(loop) = GxICR_LEVEL_6 | GxICR_DETECT;
  517. #ifdef CONFIG_GDBSTUB
  518. /* initialise GDB-stub */
  519. do {
  520. unsigned long flags;
  521. u16 tmp16;
  522. flags = arch_local_cli_save();
  523. GxICR(GDB_NMI_IPI) = GxICR_NMI | GxICR_ENABLE | GxICR_DETECT;
  524. tmp16 = GxICR(GDB_NMI_IPI);
  525. arch_local_irq_restore(flags);
  526. } while (0);
  527. #endif
  528. }
  529. /**
  530. * start_secondary - Activate a secondary CPU (AP)
  531. * @unused: Thread parameter (ignored).
  532. */
  533. int __init start_secondary(void *unused)
  534. {
  535. smp_cpu_init();
  536. smp_callin();
  537. while (!cpu_isset(smp_processor_id(), smp_commenced_mask))
  538. cpu_relax();
  539. local_flush_tlb();
  540. preempt_disable();
  541. smp_online();
  542. cpu_idle();
  543. return 0;
  544. }
  545. /**
  546. * smp_prepare_cpus - Boot up secondary CPUs (APs)
  547. * @max_cpus: Maximum number of CPUs to boot.
  548. *
  549. * Call do_boot_cpu, and boot up APs.
  550. */
  551. void __init smp_prepare_cpus(unsigned int max_cpus)
  552. {
  553. int phy_id;
  554. /* Setup boot CPU information */
  555. smp_store_cpu_info(0);
  556. smp_tune_scheduling();
  557. init_ipi();
  558. /* If SMP should be disabled, then finish */
  559. if (max_cpus == 0) {
  560. printk(KERN_INFO "SMP mode deactivated.\n");
  561. goto smp_done;
  562. }
  563. /* Boot secondary CPUs (for which phy_id > 0) */
  564. for (phy_id = 0; phy_id < NR_CPUS; phy_id++) {
  565. /* Don't boot primary CPU */
  566. if (max_cpus <= cpucount + 1)
  567. continue;
  568. if (phy_id != 0)
  569. do_boot_cpu(phy_id);
  570. set_cpu_possible(phy_id, true);
  571. smp_show_cpu_info(phy_id);
  572. }
  573. smp_done:
  574. Dprintk("Boot done.\n");
  575. }
  576. /**
  577. * smp_store_cpu_info - Save a CPU's information
  578. * @cpu: The CPU to save for.
  579. *
  580. * Save boot_cpu_data and jiffy for the specified CPU.
  581. */
  582. static void __init smp_store_cpu_info(int cpu)
  583. {
  584. struct mn10300_cpuinfo *ci = &cpu_data[cpu];
  585. *ci = boot_cpu_data;
  586. ci->loops_per_jiffy = loops_per_jiffy;
  587. ci->type = CPUREV;
  588. }
  589. /**
  590. * smp_tune_scheduling - Set time slice value
  591. *
  592. * Nothing to do here.
  593. */
  594. static void __init smp_tune_scheduling(void)
  595. {
  596. }
  597. /**
  598. * do_boot_cpu: Boot up one CPU
  599. * @phy_id: Physical ID of CPU to boot.
  600. *
  601. * Send an IPI to a secondary CPU to boot it. Returns 0 on success, 1
  602. * otherwise.
  603. */
  604. static int __init do_boot_cpu(int phy_id)
  605. {
  606. struct task_struct *idle;
  607. unsigned long send_status, callin_status;
  608. int timeout, cpu_id;
  609. send_status = GxICR_REQUEST;
  610. callin_status = 0;
  611. timeout = 0;
  612. cpu_id = phy_id;
  613. cpucount++;
  614. /* Create idle thread for this CPU */
  615. idle = fork_idle(cpu_id);
  616. if (IS_ERR(idle))
  617. panic("Failed fork for CPU#%d.", cpu_id);
  618. idle->thread.pc = (unsigned long)start_secondary;
  619. printk(KERN_NOTICE "Booting CPU#%d\n", cpu_id);
  620. start_stack[cpu_id - 1] = idle->thread.sp;
  621. task_thread_info(idle)->cpu = cpu_id;
  622. /* Send boot IPI to AP */
  623. send_IPI_mask(cpumask_of(phy_id), SMP_BOOT_IRQ);
  624. Dprintk("Waiting for send to finish...\n");
  625. /* Wait for AP's IPI receive in 100[ms] */
  626. do {
  627. udelay(1000);
  628. send_status =
  629. CROSS_GxICR(SMP_BOOT_IRQ, phy_id) & GxICR_REQUEST;
  630. } while (send_status == GxICR_REQUEST && timeout++ < 100);
  631. Dprintk("Waiting for cpu_callin_map.\n");
  632. if (send_status == 0) {
  633. /* Allow AP to start initializing */
  634. cpu_set(cpu_id, cpu_callout_map);
  635. /* Wait for setting cpu_callin_map */
  636. timeout = 0;
  637. do {
  638. udelay(1000);
  639. callin_status = cpu_isset(cpu_id, cpu_callin_map);
  640. } while (callin_status == 0 && timeout++ < 5000);
  641. if (callin_status == 0)
  642. Dprintk("Not responding.\n");
  643. } else {
  644. printk(KERN_WARNING "IPI not delivered.\n");
  645. }
  646. if (send_status == GxICR_REQUEST || callin_status == 0) {
  647. cpu_clear(cpu_id, cpu_callout_map);
  648. cpu_clear(cpu_id, cpu_callin_map);
  649. cpu_clear(cpu_id, cpu_initialized);
  650. cpucount--;
  651. return 1;
  652. }
  653. return 0;
  654. }
  655. /**
  656. * smp_show_cpu_info - Show SMP CPU information
  657. * @cpu: The CPU of interest.
  658. */
  659. static void __init smp_show_cpu_info(int cpu)
  660. {
  661. struct mn10300_cpuinfo *ci = &cpu_data[cpu];
  662. printk(KERN_INFO
  663. "CPU#%d : ioclk speed: %lu.%02luMHz : bogomips : %lu.%02lu\n",
  664. cpu,
  665. MN10300_IOCLK / 1000000,
  666. (MN10300_IOCLK / 10000) % 100,
  667. ci->loops_per_jiffy / (500000 / HZ),
  668. (ci->loops_per_jiffy / (5000 / HZ)) % 100);
  669. }
  670. /**
  671. * smp_callin - Set cpu_callin_map of the current CPU ID
  672. */
  673. static void __init smp_callin(void)
  674. {
  675. unsigned long timeout;
  676. int cpu;
  677. cpu = smp_processor_id();
  678. timeout = jiffies + (2 * HZ);
  679. if (cpu_isset(cpu, cpu_callin_map)) {
  680. printk(KERN_ERR "CPU#%d already present.\n", cpu);
  681. BUG();
  682. }
  683. Dprintk("CPU#%d waiting for CALLOUT\n", cpu);
  684. /* Wait for AP startup 2s total */
  685. while (time_before(jiffies, timeout)) {
  686. if (cpu_isset(cpu, cpu_callout_map))
  687. break;
  688. cpu_relax();
  689. }
  690. if (!time_before(jiffies, timeout)) {
  691. printk(KERN_ERR
  692. "BUG: CPU#%d started up but did not get a callout!\n",
  693. cpu);
  694. BUG();
  695. }
  696. #ifdef CONFIG_CALIBRATE_DELAY
  697. calibrate_delay(); /* Get our bogomips */
  698. #endif
  699. /* Save our processor parameters */
  700. smp_store_cpu_info(cpu);
  701. /* Allow the boot processor to continue */
  702. cpu_set(cpu, cpu_callin_map);
  703. }
  704. /**
  705. * smp_online - Set cpu_online_map
  706. */
  707. static void __init smp_online(void)
  708. {
  709. int cpu;
  710. cpu = smp_processor_id();
  711. local_irq_enable();
  712. cpu_set(cpu, cpu_online_map);
  713. smp_wmb();
  714. }
  715. /**
  716. * smp_cpus_done -
  717. * @max_cpus: Maximum CPU count.
  718. *
  719. * Do nothing.
  720. */
  721. void __init smp_cpus_done(unsigned int max_cpus)
  722. {
  723. }
  724. /*
  725. * smp_prepare_boot_cpu - Set up stuff for the boot processor.
  726. *
  727. * Set up the cpu_online_map, cpu_callout_map and cpu_callin_map of the boot
  728. * processor (CPU 0).
  729. */
  730. void __devinit smp_prepare_boot_cpu(void)
  731. {
  732. cpu_set(0, cpu_callout_map);
  733. cpu_set(0, cpu_callin_map);
  734. current_thread_info()->cpu = 0;
  735. }
  736. /*
  737. * initialize_secondary - Initialise a secondary CPU (Application Processor).
  738. *
  739. * Set SP register and jump to thread's PC address.
  740. */
  741. void initialize_secondary(void)
  742. {
  743. asm volatile (
  744. "mov %0,sp \n"
  745. "jmp (%1) \n"
  746. :
  747. : "a"(current->thread.sp), "a"(current->thread.pc));
  748. }
  749. /**
  750. * __cpu_up - Set smp_commenced_mask for the nominated CPU
  751. * @cpu: The target CPU.
  752. */
  753. int __devinit __cpu_up(unsigned int cpu)
  754. {
  755. int timeout;
  756. #ifdef CONFIG_HOTPLUG_CPU
  757. if (num_online_cpus() == 1)
  758. disable_hlt();
  759. if (sleep_mode[cpu])
  760. run_wakeup_cpu(cpu);
  761. #endif /* CONFIG_HOTPLUG_CPU */
  762. cpu_set(cpu, smp_commenced_mask);
  763. /* Wait 5s total for a response */
  764. for (timeout = 0 ; timeout < 5000 ; timeout++) {
  765. if (cpu_isset(cpu, cpu_online_map))
  766. break;
  767. udelay(1000);
  768. }
  769. BUG_ON(!cpu_isset(cpu, cpu_online_map));
  770. return 0;
  771. }
  772. /**
  773. * setup_profiling_timer - Set up the profiling timer
  774. * @multiplier - The frequency multiplier to use
  775. *
  776. * The frequency of the profiling timer can be changed by writing a multiplier
  777. * value into /proc/profile.
  778. */
  779. int setup_profiling_timer(unsigned int multiplier)
  780. {
  781. return -EINVAL;
  782. }
  783. /*
  784. * CPU hotplug routines
  785. */
  786. #ifdef CONFIG_HOTPLUG_CPU
  787. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  788. static int __init topology_init(void)
  789. {
  790. int cpu, ret;
  791. for_each_cpu(cpu) {
  792. ret = register_cpu(&per_cpu(cpu_devices, cpu), cpu, NULL);
  793. if (ret)
  794. printk(KERN_WARNING
  795. "topology_init: register_cpu %d failed (%d)\n",
  796. cpu, ret);
  797. }
  798. return 0;
  799. }
  800. subsys_initcall(topology_init);
  801. int __cpu_disable(void)
  802. {
  803. int cpu = smp_processor_id();
  804. if (cpu == 0)
  805. return -EBUSY;
  806. migrate_irqs();
  807. cpu_clear(cpu, current->active_mm->cpu_vm_mask);
  808. return 0;
  809. }
  810. void __cpu_die(unsigned int cpu)
  811. {
  812. run_sleep_cpu(cpu);
  813. if (num_online_cpus() == 1)
  814. enable_hlt();
  815. }
  816. #ifdef CONFIG_MN10300_CACHE_ENABLED
  817. static inline void hotplug_cpu_disable_cache(void)
  818. {
  819. int tmp;
  820. asm volatile(
  821. " movhu (%1),%0 \n"
  822. " and %2,%0 \n"
  823. " movhu %0,(%1) \n"
  824. "1: movhu (%1),%0 \n"
  825. " btst %3,%0 \n"
  826. " bne 1b \n"
  827. : "=&r"(tmp)
  828. : "a"(&CHCTR),
  829. "i"(~(CHCTR_ICEN | CHCTR_DCEN)),
  830. "i"(CHCTR_ICBUSY | CHCTR_DCBUSY)
  831. : "memory", "cc");
  832. }
  833. static inline void hotplug_cpu_enable_cache(void)
  834. {
  835. int tmp;
  836. asm volatile(
  837. "movhu (%1),%0 \n"
  838. "or %2,%0 \n"
  839. "movhu %0,(%1) \n"
  840. : "=&r"(tmp)
  841. : "a"(&CHCTR),
  842. "i"(CHCTR_ICEN | CHCTR_DCEN)
  843. : "memory", "cc");
  844. }
  845. static inline void hotplug_cpu_invalidate_cache(void)
  846. {
  847. int tmp;
  848. asm volatile (
  849. "movhu (%1),%0 \n"
  850. "or %2,%0 \n"
  851. "movhu %0,(%1) \n"
  852. : "=&r"(tmp)
  853. : "a"(&CHCTR),
  854. "i"(CHCTR_ICINV | CHCTR_DCINV)
  855. : "cc");
  856. }
  857. #else /* CONFIG_MN10300_CACHE_ENABLED */
  858. #define hotplug_cpu_disable_cache() do {} while (0)
  859. #define hotplug_cpu_enable_cache() do {} while (0)
  860. #define hotplug_cpu_invalidate_cache() do {} while (0)
  861. #endif /* CONFIG_MN10300_CACHE_ENABLED */
  862. /**
  863. * hotplug_cpu_nmi_call_function - Call a function on other CPUs for hotplug
  864. * @cpumask: List of target CPUs.
  865. * @func: The function to call on those CPUs.
  866. * @info: The context data for the function to be called.
  867. * @wait: Whether to wait for the calls to complete.
  868. *
  869. * Non-maskably call a function on another CPU for hotplug purposes.
  870. *
  871. * This function must be called with maskable interrupts disabled.
  872. */
  873. static int hotplug_cpu_nmi_call_function(cpumask_t cpumask,
  874. smp_call_func_t func, void *info,
  875. int wait)
  876. {
  877. /*
  878. * The address and the size of nmi_call_func_mask_data
  879. * need to be aligned on L1_CACHE_BYTES.
  880. */
  881. static struct nmi_call_data_struct nmi_call_func_mask_data
  882. __cacheline_aligned;
  883. unsigned long start, end;
  884. start = (unsigned long)&nmi_call_func_mask_data;
  885. end = start + sizeof(struct nmi_call_data_struct);
  886. nmi_call_func_mask_data.func = func;
  887. nmi_call_func_mask_data.info = info;
  888. nmi_call_func_mask_data.started = cpumask;
  889. nmi_call_func_mask_data.wait = wait;
  890. if (wait)
  891. nmi_call_func_mask_data.finished = cpumask;
  892. spin_lock(&smp_nmi_call_lock);
  893. nmi_call_data = &nmi_call_func_mask_data;
  894. mn10300_local_dcache_flush_range(start, end);
  895. smp_wmb();
  896. send_IPI_mask(cpumask, CALL_FUNCTION_NMI_IPI);
  897. do {
  898. mn10300_local_dcache_inv_range(start, end);
  899. barrier();
  900. } while (!cpus_empty(nmi_call_func_mask_data.started));
  901. if (wait) {
  902. do {
  903. mn10300_local_dcache_inv_range(start, end);
  904. barrier();
  905. } while (!cpus_empty(nmi_call_func_mask_data.finished));
  906. }
  907. spin_unlock(&smp_nmi_call_lock);
  908. return 0;
  909. }
  910. static void restart_wakeup_cpu(void)
  911. {
  912. unsigned int cpu = smp_processor_id();
  913. cpu_set(cpu, cpu_callin_map);
  914. local_flush_tlb();
  915. cpu_set(cpu, cpu_online_map);
  916. smp_wmb();
  917. }
  918. static void prepare_sleep_cpu(void *unused)
  919. {
  920. sleep_mode[smp_processor_id()] = 1;
  921. smp_mb();
  922. mn10300_local_dcache_flush_inv();
  923. hotplug_cpu_disable_cache();
  924. hotplug_cpu_invalidate_cache();
  925. }
  926. /* when this function called, IE=0, NMID=0. */
  927. static void sleep_cpu(void *unused)
  928. {
  929. unsigned int cpu_id = smp_processor_id();
  930. /*
  931. * CALL_FUNCTION_NMI_IPI for wakeup_cpu() shall not be requested,
  932. * before this cpu goes in SLEEP mode.
  933. */
  934. do {
  935. smp_mb();
  936. __sleep_cpu();
  937. } while (sleep_mode[cpu_id]);
  938. restart_wakeup_cpu();
  939. }
  940. static void run_sleep_cpu(unsigned int cpu)
  941. {
  942. unsigned long flags;
  943. cpumask_t cpumask = cpumask_of(cpu);
  944. flags = arch_local_cli_save();
  945. hotplug_cpu_nmi_call_function(cpumask, prepare_sleep_cpu, NULL, 1);
  946. hotplug_cpu_nmi_call_function(cpumask, sleep_cpu, NULL, 0);
  947. udelay(1); /* delay for the cpu to sleep. */
  948. arch_local_irq_restore(flags);
  949. }
  950. static void wakeup_cpu(void)
  951. {
  952. hotplug_cpu_invalidate_cache();
  953. hotplug_cpu_enable_cache();
  954. smp_mb();
  955. sleep_mode[smp_processor_id()] = 0;
  956. }
  957. static void run_wakeup_cpu(unsigned int cpu)
  958. {
  959. unsigned long flags;
  960. flags = arch_local_cli_save();
  961. #if NR_CPUS == 2
  962. mn10300_local_dcache_flush_inv();
  963. #else
  964. /*
  965. * Before waking up the cpu,
  966. * all online cpus should stop and flush D-Cache for global data.
  967. */
  968. #error not support NR_CPUS > 2, when CONFIG_HOTPLUG_CPU=y.
  969. #endif
  970. hotplug_cpu_nmi_call_function(cpumask_of(cpu), wakeup_cpu, NULL, 1);
  971. arch_local_irq_restore(flags);
  972. }
  973. #endif /* CONFIG_HOTPLUG_CPU */