smp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. * Copyright (C) 2009,2010,2011 Imagination Technologies Ltd.
  3. *
  4. * Copyright (C) 2002 ARM Limited, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/atomic.h>
  11. #include <linux/completion.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/sched.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/cache.h>
  18. #include <linux/profile.h>
  19. #include <linux/errno.h>
  20. #include <linux/mm.h>
  21. #include <linux/err.h>
  22. #include <linux/cpu.h>
  23. #include <linux/smp.h>
  24. #include <linux/seq_file.h>
  25. #include <linux/irq.h>
  26. #include <linux/bootmem.h>
  27. #include <asm/cacheflush.h>
  28. #include <asm/cachepart.h>
  29. #include <asm/core_reg.h>
  30. #include <asm/cpu.h>
  31. #include <asm/global_lock.h>
  32. #include <asm/metag_mem.h>
  33. #include <asm/mmu_context.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/pgalloc.h>
  36. #include <asm/processor.h>
  37. #include <asm/setup.h>
  38. #include <asm/tlbflush.h>
  39. #include <asm/hwthread.h>
  40. #include <asm/traps.h>
  41. #define SYSC_DCPART(n) (SYSC_DCPART0 + SYSC_xCPARTn_STRIDE * (n))
  42. #define SYSC_ICPART(n) (SYSC_ICPART0 + SYSC_xCPARTn_STRIDE * (n))
  43. DECLARE_PER_CPU(PTBI, pTBI);
  44. void *secondary_data_stack;
  45. /*
  46. * structures for inter-processor calls
  47. * - A collection of single bit ipi messages.
  48. */
  49. struct ipi_data {
  50. spinlock_t lock;
  51. unsigned long ipi_count;
  52. unsigned long bits;
  53. };
  54. static DEFINE_PER_CPU(struct ipi_data, ipi_data) = {
  55. .lock = __SPIN_LOCK_UNLOCKED(ipi_data.lock),
  56. };
  57. static DEFINE_SPINLOCK(boot_lock);
  58. static DECLARE_COMPLETION(cpu_running);
  59. /*
  60. * "thread" is assumed to be a valid Meta hardware thread ID.
  61. */
  62. static int boot_secondary(unsigned int thread, struct task_struct *idle)
  63. {
  64. u32 val;
  65. /*
  66. * set synchronisation state between this boot processor
  67. * and the secondary one
  68. */
  69. spin_lock(&boot_lock);
  70. core_reg_write(TXUPC_ID, 0, thread, (unsigned int)secondary_startup);
  71. core_reg_write(TXUPC_ID, 1, thread, 0);
  72. /*
  73. * Give the thread privilege (PSTAT) and clear potentially problematic
  74. * bits in the process (namely ISTAT, CBMarker, CBMarkerI, LSM_STEP).
  75. */
  76. core_reg_write(TXUCT_ID, TXSTATUS_REGNUM, thread, TXSTATUS_PSTAT_BIT);
  77. /* Clear the minim enable bit. */
  78. val = core_reg_read(TXUCT_ID, TXPRIVEXT_REGNUM, thread);
  79. core_reg_write(TXUCT_ID, TXPRIVEXT_REGNUM, thread, val & ~0x80);
  80. /*
  81. * set the ThreadEnable bit (0x1) in the TXENABLE register
  82. * for the specified thread - off it goes!
  83. */
  84. val = core_reg_read(TXUCT_ID, TXENABLE_REGNUM, thread);
  85. core_reg_write(TXUCT_ID, TXENABLE_REGNUM, thread, val | 0x1);
  86. /*
  87. * now the secondary core is starting up let it run its
  88. * calibrations, then wait for it to finish
  89. */
  90. spin_unlock(&boot_lock);
  91. return 0;
  92. }
  93. /**
  94. * describe_cachepart_change: describe a change to cache partitions.
  95. * @thread: Hardware thread number.
  96. * @label: Label of cache type, e.g. "dcache" or "icache".
  97. * @sz: Total size of the cache.
  98. * @old: Old cache partition configuration (*CPART* register).
  99. * @new: New cache partition configuration (*CPART* register).
  100. *
  101. * If the cache partition has changed, prints a message to the log describing
  102. * those changes.
  103. */
  104. static void describe_cachepart_change(unsigned int thread, const char *label,
  105. unsigned int sz, unsigned int old,
  106. unsigned int new)
  107. {
  108. unsigned int lor1, land1, gor1, gand1;
  109. unsigned int lor2, land2, gor2, gand2;
  110. unsigned int diff = old ^ new;
  111. if (!diff)
  112. return;
  113. pr_info("Thread %d: %s partition changed:", thread, label);
  114. if (diff & (SYSC_xCPARTL_OR_BITS | SYSC_xCPARTL_AND_BITS)) {
  115. lor1 = (old & SYSC_xCPARTL_OR_BITS) >> SYSC_xCPARTL_OR_S;
  116. lor2 = (new & SYSC_xCPARTL_OR_BITS) >> SYSC_xCPARTL_OR_S;
  117. land1 = (old & SYSC_xCPARTL_AND_BITS) >> SYSC_xCPARTL_AND_S;
  118. land2 = (new & SYSC_xCPARTL_AND_BITS) >> SYSC_xCPARTL_AND_S;
  119. pr_cont(" L:%#x+%#x->%#x+%#x",
  120. (lor1 * sz) >> 4,
  121. ((land1 + 1) * sz) >> 4,
  122. (lor2 * sz) >> 4,
  123. ((land2 + 1) * sz) >> 4);
  124. }
  125. if (diff & (SYSC_xCPARTG_OR_BITS | SYSC_xCPARTG_AND_BITS)) {
  126. gor1 = (old & SYSC_xCPARTG_OR_BITS) >> SYSC_xCPARTG_OR_S;
  127. gor2 = (new & SYSC_xCPARTG_OR_BITS) >> SYSC_xCPARTG_OR_S;
  128. gand1 = (old & SYSC_xCPARTG_AND_BITS) >> SYSC_xCPARTG_AND_S;
  129. gand2 = (new & SYSC_xCPARTG_AND_BITS) >> SYSC_xCPARTG_AND_S;
  130. pr_cont(" G:%#x+%#x->%#x+%#x",
  131. (gor1 * sz) >> 4,
  132. ((gand1 + 1) * sz) >> 4,
  133. (gor2 * sz) >> 4,
  134. ((gand2 + 1) * sz) >> 4);
  135. }
  136. if (diff & SYSC_CWRMODE_BIT)
  137. pr_cont(" %sWR",
  138. (new & SYSC_CWRMODE_BIT) ? "+" : "-");
  139. if (diff & SYSC_DCPART_GCON_BIT)
  140. pr_cont(" %sGCOn",
  141. (new & SYSC_DCPART_GCON_BIT) ? "+" : "-");
  142. pr_cont("\n");
  143. }
  144. /**
  145. * setup_smp_cache: ensure cache coherency for new SMP thread.
  146. * @thread: New hardware thread number.
  147. *
  148. * Ensures that coherency is enabled and that the threads share the same cache
  149. * partitions.
  150. */
  151. static void setup_smp_cache(unsigned int thread)
  152. {
  153. unsigned int this_thread, lflags;
  154. unsigned int dcsz, dcpart_this, dcpart_old, dcpart_new;
  155. unsigned int icsz, icpart_old, icpart_new;
  156. /*
  157. * Copy over the current thread's cache partition configuration to the
  158. * new thread so that they share cache partitions.
  159. */
  160. __global_lock2(lflags);
  161. this_thread = hard_processor_id();
  162. /* Share dcache partition */
  163. dcpart_this = metag_in32(SYSC_DCPART(this_thread));
  164. dcpart_old = metag_in32(SYSC_DCPART(thread));
  165. dcpart_new = dcpart_this;
  166. #if PAGE_OFFSET < LINGLOBAL_BASE
  167. /*
  168. * For the local data cache to be coherent the threads must also have
  169. * GCOn enabled.
  170. */
  171. dcpart_new |= SYSC_DCPART_GCON_BIT;
  172. metag_out32(dcpart_new, SYSC_DCPART(this_thread));
  173. #endif
  174. metag_out32(dcpart_new, SYSC_DCPART(thread));
  175. /* Share icache partition too */
  176. icpart_new = metag_in32(SYSC_ICPART(this_thread));
  177. icpart_old = metag_in32(SYSC_ICPART(thread));
  178. metag_out32(icpart_new, SYSC_ICPART(thread));
  179. __global_unlock2(lflags);
  180. /*
  181. * Log if the cache partitions were altered so the user is aware of any
  182. * potential unintentional cache wastage.
  183. */
  184. dcsz = get_dcache_size();
  185. icsz = get_dcache_size();
  186. describe_cachepart_change(this_thread, "dcache", dcsz,
  187. dcpart_this, dcpart_new);
  188. describe_cachepart_change(thread, "dcache", dcsz,
  189. dcpart_old, dcpart_new);
  190. describe_cachepart_change(thread, "icache", icsz,
  191. icpart_old, icpart_new);
  192. }
  193. int __cpu_up(unsigned int cpu, struct task_struct *idle)
  194. {
  195. unsigned int thread = cpu_2_hwthread_id[cpu];
  196. int ret;
  197. load_pgd(swapper_pg_dir, thread);
  198. flush_tlb_all();
  199. setup_smp_cache(thread);
  200. /*
  201. * Tell the secondary CPU where to find its idle thread's stack.
  202. */
  203. secondary_data_stack = task_stack_page(idle);
  204. wmb();
  205. /*
  206. * Now bring the CPU into our world.
  207. */
  208. ret = boot_secondary(thread, idle);
  209. if (ret == 0) {
  210. /*
  211. * CPU was successfully started, wait for it
  212. * to come online or time out.
  213. */
  214. wait_for_completion_timeout(&cpu_running,
  215. msecs_to_jiffies(1000));
  216. if (!cpu_online(cpu))
  217. ret = -EIO;
  218. }
  219. secondary_data_stack = NULL;
  220. if (ret) {
  221. pr_crit("CPU%u: processor failed to boot\n", cpu);
  222. /*
  223. * FIXME: We need to clean up the new idle thread. --rmk
  224. */
  225. }
  226. return ret;
  227. }
  228. #ifdef CONFIG_HOTPLUG_CPU
  229. static DECLARE_COMPLETION(cpu_killed);
  230. /*
  231. * __cpu_disable runs on the processor to be shutdown.
  232. */
  233. int __cpu_disable(void)
  234. {
  235. unsigned int cpu = smp_processor_id();
  236. /*
  237. * Take this CPU offline. Once we clear this, we can't return,
  238. * and we must not schedule until we're ready to give up the cpu.
  239. */
  240. set_cpu_online(cpu, false);
  241. /*
  242. * OK - migrate IRQs away from this CPU
  243. */
  244. migrate_irqs();
  245. /*
  246. * Flush user cache and TLB mappings, and then remove this CPU
  247. * from the vm mask set of all processes.
  248. */
  249. flush_cache_all();
  250. local_flush_tlb_all();
  251. clear_tasks_mm_cpumask(cpu);
  252. return 0;
  253. }
  254. /*
  255. * called on the thread which is asking for a CPU to be shutdown -
  256. * waits until shutdown has completed, or it is timed out.
  257. */
  258. void __cpu_die(unsigned int cpu)
  259. {
  260. if (!wait_for_completion_timeout(&cpu_killed, msecs_to_jiffies(1)))
  261. pr_err("CPU%u: unable to kill\n", cpu);
  262. }
  263. /*
  264. * Called from the idle thread for the CPU which has been shutdown.
  265. *
  266. * Note that we do not return from this function. If this cpu is
  267. * brought online again it will need to run secondary_startup().
  268. */
  269. void cpu_die(void)
  270. {
  271. local_irq_disable();
  272. idle_task_exit();
  273. complete(&cpu_killed);
  274. asm ("XOR TXENABLE, D0Re0,D0Re0\n");
  275. }
  276. #endif /* CONFIG_HOTPLUG_CPU */
  277. /*
  278. * Called by both boot and secondaries to move global data into
  279. * per-processor storage.
  280. */
  281. void smp_store_cpu_info(unsigned int cpuid)
  282. {
  283. struct cpuinfo_metag *cpu_info = &per_cpu(cpu_data, cpuid);
  284. cpu_info->loops_per_jiffy = loops_per_jiffy;
  285. }
  286. /*
  287. * This is the secondary CPU boot entry. We're using this CPUs
  288. * idle thread stack and the global page tables.
  289. */
  290. asmlinkage void secondary_start_kernel(void)
  291. {
  292. struct mm_struct *mm = &init_mm;
  293. unsigned int cpu = smp_processor_id();
  294. /*
  295. * All kernel threads share the same mm context; grab a
  296. * reference and switch to it.
  297. */
  298. atomic_inc(&mm->mm_users);
  299. atomic_inc(&mm->mm_count);
  300. current->active_mm = mm;
  301. cpumask_set_cpu(cpu, mm_cpumask(mm));
  302. enter_lazy_tlb(mm, current);
  303. local_flush_tlb_all();
  304. /*
  305. * TODO: Some day it might be useful for each Linux CPU to
  306. * have its own TBI structure. That would allow each Linux CPU
  307. * to run different interrupt handlers for the same IRQ
  308. * number.
  309. *
  310. * For now, simply copying the pointer to the boot CPU's TBI
  311. * structure is sufficient because we always want to run the
  312. * same interrupt handler whatever CPU takes the interrupt.
  313. */
  314. per_cpu(pTBI, cpu) = __TBI(TBID_ISTAT_BIT);
  315. if (!per_cpu(pTBI, cpu))
  316. panic("No TBI found!");
  317. per_cpu_trap_init(cpu);
  318. preempt_disable();
  319. setup_priv();
  320. notify_cpu_starting(cpu);
  321. pr_info("CPU%u (thread %u): Booted secondary processor\n",
  322. cpu, cpu_2_hwthread_id[cpu]);
  323. calibrate_delay();
  324. smp_store_cpu_info(cpu);
  325. /*
  326. * OK, now it's safe to let the boot CPU continue
  327. */
  328. set_cpu_online(cpu, true);
  329. complete(&cpu_running);
  330. /*
  331. * Enable local interrupts.
  332. */
  333. tbi_startup_interrupt(TBID_SIGNUM_TRT);
  334. local_irq_enable();
  335. /*
  336. * OK, it's off to the idle thread for us
  337. */
  338. cpu_startup_entry(CPUHP_ONLINE);
  339. }
  340. void __init smp_cpus_done(unsigned int max_cpus)
  341. {
  342. int cpu;
  343. unsigned long bogosum = 0;
  344. for_each_online_cpu(cpu)
  345. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  346. pr_info("SMP: Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  347. num_online_cpus(),
  348. bogosum / (500000/HZ),
  349. (bogosum / (5000/HZ)) % 100);
  350. }
  351. void __init smp_prepare_cpus(unsigned int max_cpus)
  352. {
  353. unsigned int cpu = smp_processor_id();
  354. init_new_context(current, &init_mm);
  355. current_thread_info()->cpu = cpu;
  356. smp_store_cpu_info(cpu);
  357. init_cpu_present(cpu_possible_mask);
  358. }
  359. void __init smp_prepare_boot_cpu(void)
  360. {
  361. unsigned int cpu = smp_processor_id();
  362. per_cpu(pTBI, cpu) = __TBI(TBID_ISTAT_BIT);
  363. if (!per_cpu(pTBI, cpu))
  364. panic("No TBI found!");
  365. }
  366. static void smp_cross_call(cpumask_t callmap, enum ipi_msg_type msg);
  367. static void send_ipi_message(const struct cpumask *mask, enum ipi_msg_type msg)
  368. {
  369. unsigned long flags;
  370. unsigned int cpu;
  371. cpumask_t map;
  372. cpumask_clear(&map);
  373. local_irq_save(flags);
  374. for_each_cpu(cpu, mask) {
  375. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  376. spin_lock(&ipi->lock);
  377. /*
  378. * KICK interrupts are queued in hardware so we'll get
  379. * multiple interrupts if we call smp_cross_call()
  380. * multiple times for one msg. The problem is that we
  381. * only have one bit for each message - we can't queue
  382. * them in software.
  383. *
  384. * The first time through ipi_handler() we'll clear
  385. * the msg bit, having done all the work. But when we
  386. * return we'll get _another_ interrupt (and another,
  387. * and another until we've handled all the queued
  388. * KICKs). Running ipi_handler() when there's no work
  389. * to do is bad because that's how kick handler
  390. * chaining detects who the KICK was intended for.
  391. * See arch/metag/kernel/kick.c for more details.
  392. *
  393. * So only add 'cpu' to 'map' if we haven't already
  394. * queued a KICK interrupt for 'msg'.
  395. */
  396. if (!(ipi->bits & (1 << msg))) {
  397. ipi->bits |= 1 << msg;
  398. cpumask_set_cpu(cpu, &map);
  399. }
  400. spin_unlock(&ipi->lock);
  401. }
  402. /*
  403. * Call the platform specific cross-CPU call function.
  404. */
  405. smp_cross_call(map, msg);
  406. local_irq_restore(flags);
  407. }
  408. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  409. {
  410. send_ipi_message(mask, IPI_CALL_FUNC);
  411. }
  412. void arch_send_call_function_single_ipi(int cpu)
  413. {
  414. send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
  415. }
  416. void show_ipi_list(struct seq_file *p)
  417. {
  418. unsigned int cpu;
  419. seq_puts(p, "IPI:");
  420. for_each_present_cpu(cpu)
  421. seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
  422. seq_putc(p, '\n');
  423. }
  424. static DEFINE_SPINLOCK(stop_lock);
  425. /*
  426. * Main handler for inter-processor interrupts
  427. *
  428. * For Meta, the ipimask now only identifies a single
  429. * category of IPI (Bit 1 IPIs have been replaced by a
  430. * different mechanism):
  431. *
  432. * Bit 0 - Inter-processor function call
  433. */
  434. static int do_IPI(void)
  435. {
  436. unsigned int cpu = smp_processor_id();
  437. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  438. unsigned long msgs, nextmsg;
  439. int handled = 0;
  440. ipi->ipi_count++;
  441. spin_lock(&ipi->lock);
  442. msgs = ipi->bits;
  443. nextmsg = msgs & -msgs;
  444. ipi->bits &= ~nextmsg;
  445. spin_unlock(&ipi->lock);
  446. if (nextmsg) {
  447. handled = 1;
  448. nextmsg = ffz(~nextmsg);
  449. switch (nextmsg) {
  450. case IPI_RESCHEDULE:
  451. scheduler_ipi();
  452. break;
  453. case IPI_CALL_FUNC:
  454. generic_smp_call_function_interrupt();
  455. break;
  456. default:
  457. pr_crit("CPU%u: Unknown IPI message 0x%lx\n",
  458. cpu, nextmsg);
  459. break;
  460. }
  461. }
  462. return handled;
  463. }
  464. void smp_send_reschedule(int cpu)
  465. {
  466. send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
  467. }
  468. static void stop_this_cpu(void *data)
  469. {
  470. unsigned int cpu = smp_processor_id();
  471. if (system_state == SYSTEM_BOOTING ||
  472. system_state == SYSTEM_RUNNING) {
  473. spin_lock(&stop_lock);
  474. pr_crit("CPU%u: stopping\n", cpu);
  475. dump_stack();
  476. spin_unlock(&stop_lock);
  477. }
  478. set_cpu_online(cpu, false);
  479. local_irq_disable();
  480. hard_processor_halt(HALT_OK);
  481. }
  482. void smp_send_stop(void)
  483. {
  484. smp_call_function(stop_this_cpu, NULL, 0);
  485. }
  486. /*
  487. * not supported here
  488. */
  489. int setup_profiling_timer(unsigned int multiplier)
  490. {
  491. return -EINVAL;
  492. }
  493. /*
  494. * We use KICKs for inter-processor interrupts.
  495. *
  496. * For every CPU in "callmap" the IPI data must already have been
  497. * stored in that CPU's "ipi_data" member prior to calling this
  498. * function.
  499. */
  500. static void kick_raise_softirq(cpumask_t callmap, unsigned int irq)
  501. {
  502. int cpu;
  503. for_each_cpu(cpu, &callmap) {
  504. unsigned int thread;
  505. thread = cpu_2_hwthread_id[cpu];
  506. BUG_ON(thread == BAD_HWTHREAD_ID);
  507. metag_out32(1, T0KICKI + (thread * TnXKICK_STRIDE));
  508. }
  509. }
  510. static TBIRES ipi_handler(TBIRES State, int SigNum, int Triggers,
  511. int Inst, PTBI pTBI, int *handled)
  512. {
  513. *handled = do_IPI();
  514. return State;
  515. }
  516. static struct kick_irq_handler ipi_irq = {
  517. .func = ipi_handler,
  518. };
  519. static void smp_cross_call(cpumask_t callmap, enum ipi_msg_type msg)
  520. {
  521. kick_raise_softirq(callmap, 1);
  522. }
  523. static inline unsigned int get_core_count(void)
  524. {
  525. int i;
  526. unsigned int ret = 0;
  527. for (i = 0; i < CONFIG_NR_CPUS; i++) {
  528. if (core_reg_read(TXUCT_ID, TXENABLE_REGNUM, i))
  529. ret++;
  530. }
  531. return ret;
  532. }
  533. /*
  534. * Initialise the CPU possible map early - this describes the CPUs
  535. * which may be present or become present in the system.
  536. */
  537. void __init smp_init_cpus(void)
  538. {
  539. unsigned int i, ncores = get_core_count();
  540. /* If no hwthread_map early param was set use default mapping */
  541. for (i = 0; i < NR_CPUS; i++)
  542. if (cpu_2_hwthread_id[i] == BAD_HWTHREAD_ID) {
  543. cpu_2_hwthread_id[i] = i;
  544. hwthread_id_2_cpu[i] = i;
  545. }
  546. for (i = 0; i < ncores; i++)
  547. set_cpu_possible(i, true);
  548. kick_register_func(&ipi_irq);
  549. }