smp.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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. /*
  230. * __cpu_disable runs on the processor to be shutdown.
  231. */
  232. int __cpu_disable(void)
  233. {
  234. unsigned int cpu = smp_processor_id();
  235. /*
  236. * Take this CPU offline. Once we clear this, we can't return,
  237. * and we must not schedule until we're ready to give up the cpu.
  238. */
  239. set_cpu_online(cpu, false);
  240. /*
  241. * OK - migrate IRQs away from this CPU
  242. */
  243. migrate_irqs();
  244. /*
  245. * Flush user cache and TLB mappings, and then remove this CPU
  246. * from the vm mask set of all processes.
  247. */
  248. flush_cache_all();
  249. local_flush_tlb_all();
  250. clear_tasks_mm_cpumask(cpu);
  251. return 0;
  252. }
  253. /*
  254. * called on the thread which is asking for a CPU to be shutdown -
  255. * waits until shutdown has completed, or it is timed out.
  256. */
  257. void __cpu_die(unsigned int cpu)
  258. {
  259. if (!cpu_wait_death(cpu, 1))
  260. pr_err("CPU%u: unable to kill\n", cpu);
  261. }
  262. /*
  263. * Called from the idle thread for the CPU which has been shutdown.
  264. *
  265. * Note that we do not return from this function. If this cpu is
  266. * brought online again it will need to run secondary_startup().
  267. */
  268. void cpu_die(void)
  269. {
  270. local_irq_disable();
  271. idle_task_exit();
  272. irq_ctx_exit(smp_processor_id());
  273. (void)cpu_report_death();
  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. irq_ctx_init(cpu);
  319. preempt_disable();
  320. setup_priv();
  321. notify_cpu_starting(cpu);
  322. pr_info("CPU%u (thread %u): Booted secondary processor\n",
  323. cpu, cpu_2_hwthread_id[cpu]);
  324. calibrate_delay();
  325. smp_store_cpu_info(cpu);
  326. /*
  327. * OK, now it's safe to let the boot CPU continue
  328. */
  329. set_cpu_online(cpu, true);
  330. complete(&cpu_running);
  331. /*
  332. * Enable local interrupts.
  333. */
  334. tbi_startup_interrupt(TBID_SIGNUM_TRT);
  335. local_irq_enable();
  336. /*
  337. * OK, it's off to the idle thread for us
  338. */
  339. cpu_startup_entry(CPUHP_AP_ONLINE_IDLE);
  340. }
  341. void __init smp_cpus_done(unsigned int max_cpus)
  342. {
  343. int cpu;
  344. unsigned long bogosum = 0;
  345. for_each_online_cpu(cpu)
  346. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  347. pr_info("SMP: Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  348. num_online_cpus(),
  349. bogosum / (500000/HZ),
  350. (bogosum / (5000/HZ)) % 100);
  351. }
  352. void __init smp_prepare_cpus(unsigned int max_cpus)
  353. {
  354. unsigned int cpu = smp_processor_id();
  355. init_new_context(current, &init_mm);
  356. current_thread_info()->cpu = cpu;
  357. smp_store_cpu_info(cpu);
  358. init_cpu_present(cpu_possible_mask);
  359. }
  360. void __init smp_prepare_boot_cpu(void)
  361. {
  362. unsigned int cpu = smp_processor_id();
  363. per_cpu(pTBI, cpu) = __TBI(TBID_ISTAT_BIT);
  364. if (!per_cpu(pTBI, cpu))
  365. panic("No TBI found!");
  366. }
  367. static void smp_cross_call(cpumask_t callmap, enum ipi_msg_type msg);
  368. static void send_ipi_message(const struct cpumask *mask, enum ipi_msg_type msg)
  369. {
  370. unsigned long flags;
  371. unsigned int cpu;
  372. cpumask_t map;
  373. cpumask_clear(&map);
  374. local_irq_save(flags);
  375. for_each_cpu(cpu, mask) {
  376. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  377. spin_lock(&ipi->lock);
  378. /*
  379. * KICK interrupts are queued in hardware so we'll get
  380. * multiple interrupts if we call smp_cross_call()
  381. * multiple times for one msg. The problem is that we
  382. * only have one bit for each message - we can't queue
  383. * them in software.
  384. *
  385. * The first time through ipi_handler() we'll clear
  386. * the msg bit, having done all the work. But when we
  387. * return we'll get _another_ interrupt (and another,
  388. * and another until we've handled all the queued
  389. * KICKs). Running ipi_handler() when there's no work
  390. * to do is bad because that's how kick handler
  391. * chaining detects who the KICK was intended for.
  392. * See arch/metag/kernel/kick.c for more details.
  393. *
  394. * So only add 'cpu' to 'map' if we haven't already
  395. * queued a KICK interrupt for 'msg'.
  396. */
  397. if (!(ipi->bits & (1 << msg))) {
  398. ipi->bits |= 1 << msg;
  399. cpumask_set_cpu(cpu, &map);
  400. }
  401. spin_unlock(&ipi->lock);
  402. }
  403. /*
  404. * Call the platform specific cross-CPU call function.
  405. */
  406. smp_cross_call(map, msg);
  407. local_irq_restore(flags);
  408. }
  409. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  410. {
  411. send_ipi_message(mask, IPI_CALL_FUNC);
  412. }
  413. void arch_send_call_function_single_ipi(int cpu)
  414. {
  415. send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
  416. }
  417. void show_ipi_list(struct seq_file *p)
  418. {
  419. unsigned int cpu;
  420. seq_puts(p, "IPI:");
  421. for_each_present_cpu(cpu)
  422. seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
  423. seq_putc(p, '\n');
  424. }
  425. static DEFINE_SPINLOCK(stop_lock);
  426. /*
  427. * Main handler for inter-processor interrupts
  428. *
  429. * For Meta, the ipimask now only identifies a single
  430. * category of IPI (Bit 1 IPIs have been replaced by a
  431. * different mechanism):
  432. *
  433. * Bit 0 - Inter-processor function call
  434. */
  435. static int do_IPI(void)
  436. {
  437. unsigned int cpu = smp_processor_id();
  438. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  439. unsigned long msgs, nextmsg;
  440. int handled = 0;
  441. ipi->ipi_count++;
  442. spin_lock(&ipi->lock);
  443. msgs = ipi->bits;
  444. nextmsg = msgs & -msgs;
  445. ipi->bits &= ~nextmsg;
  446. spin_unlock(&ipi->lock);
  447. if (nextmsg) {
  448. handled = 1;
  449. nextmsg = ffz(~nextmsg);
  450. switch (nextmsg) {
  451. case IPI_RESCHEDULE:
  452. scheduler_ipi();
  453. break;
  454. case IPI_CALL_FUNC:
  455. generic_smp_call_function_interrupt();
  456. break;
  457. default:
  458. pr_crit("CPU%u: Unknown IPI message 0x%lx\n",
  459. cpu, nextmsg);
  460. break;
  461. }
  462. }
  463. return handled;
  464. }
  465. void smp_send_reschedule(int cpu)
  466. {
  467. send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
  468. }
  469. static void stop_this_cpu(void *data)
  470. {
  471. unsigned int cpu = smp_processor_id();
  472. if (system_state == SYSTEM_BOOTING ||
  473. system_state == SYSTEM_RUNNING) {
  474. spin_lock(&stop_lock);
  475. pr_crit("CPU%u: stopping\n", cpu);
  476. dump_stack();
  477. spin_unlock(&stop_lock);
  478. }
  479. set_cpu_online(cpu, false);
  480. local_irq_disable();
  481. hard_processor_halt(HALT_OK);
  482. }
  483. void smp_send_stop(void)
  484. {
  485. smp_call_function(stop_this_cpu, NULL, 0);
  486. }
  487. /*
  488. * not supported here
  489. */
  490. int setup_profiling_timer(unsigned int multiplier)
  491. {
  492. return -EINVAL;
  493. }
  494. /*
  495. * We use KICKs for inter-processor interrupts.
  496. *
  497. * For every CPU in "callmap" the IPI data must already have been
  498. * stored in that CPU's "ipi_data" member prior to calling this
  499. * function.
  500. */
  501. static void kick_raise_softirq(cpumask_t callmap, unsigned int irq)
  502. {
  503. int cpu;
  504. for_each_cpu(cpu, &callmap) {
  505. unsigned int thread;
  506. thread = cpu_2_hwthread_id[cpu];
  507. BUG_ON(thread == BAD_HWTHREAD_ID);
  508. metag_out32(1, T0KICKI + (thread * TnXKICK_STRIDE));
  509. }
  510. }
  511. static TBIRES ipi_handler(TBIRES State, int SigNum, int Triggers,
  512. int Inst, PTBI pTBI, int *handled)
  513. {
  514. *handled = do_IPI();
  515. return State;
  516. }
  517. static struct kick_irq_handler ipi_irq = {
  518. .func = ipi_handler,
  519. };
  520. static void smp_cross_call(cpumask_t callmap, enum ipi_msg_type msg)
  521. {
  522. kick_raise_softirq(callmap, 1);
  523. }
  524. static inline unsigned int get_core_count(void)
  525. {
  526. int i;
  527. unsigned int ret = 0;
  528. for (i = 0; i < CONFIG_NR_CPUS; i++) {
  529. if (core_reg_read(TXUCT_ID, TXENABLE_REGNUM, i))
  530. ret++;
  531. }
  532. return ret;
  533. }
  534. /*
  535. * Initialise the CPU possible map early - this describes the CPUs
  536. * which may be present or become present in the system.
  537. */
  538. void __init smp_init_cpus(void)
  539. {
  540. unsigned int i, ncores = get_core_count();
  541. /* If no hwthread_map early param was set use default mapping */
  542. for (i = 0; i < NR_CPUS; i++)
  543. if (cpu_2_hwthread_id[i] == BAD_HWTHREAD_ID) {
  544. cpu_2_hwthread_id[i] = i;
  545. hwthread_id_2_cpu[i] = i;
  546. }
  547. for (i = 0; i < ncores; i++)
  548. set_cpu_possible(i, true);
  549. kick_register_func(&ipi_irq);
  550. }