smp.c 15 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. /*
  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. (void)cpu_report_death();
  273. asm ("XOR TXENABLE, D0Re0,D0Re0\n");
  274. }
  275. #endif /* CONFIG_HOTPLUG_CPU */
  276. /*
  277. * Called by both boot and secondaries to move global data into
  278. * per-processor storage.
  279. */
  280. void smp_store_cpu_info(unsigned int cpuid)
  281. {
  282. struct cpuinfo_metag *cpu_info = &per_cpu(cpu_data, cpuid);
  283. cpu_info->loops_per_jiffy = loops_per_jiffy;
  284. }
  285. /*
  286. * This is the secondary CPU boot entry. We're using this CPUs
  287. * idle thread stack and the global page tables.
  288. */
  289. asmlinkage void secondary_start_kernel(void)
  290. {
  291. struct mm_struct *mm = &init_mm;
  292. unsigned int cpu = smp_processor_id();
  293. /*
  294. * All kernel threads share the same mm context; grab a
  295. * reference and switch to it.
  296. */
  297. atomic_inc(&mm->mm_users);
  298. atomic_inc(&mm->mm_count);
  299. current->active_mm = mm;
  300. cpumask_set_cpu(cpu, mm_cpumask(mm));
  301. enter_lazy_tlb(mm, current);
  302. local_flush_tlb_all();
  303. /*
  304. * TODO: Some day it might be useful for each Linux CPU to
  305. * have its own TBI structure. That would allow each Linux CPU
  306. * to run different interrupt handlers for the same IRQ
  307. * number.
  308. *
  309. * For now, simply copying the pointer to the boot CPU's TBI
  310. * structure is sufficient because we always want to run the
  311. * same interrupt handler whatever CPU takes the interrupt.
  312. */
  313. per_cpu(pTBI, cpu) = __TBI(TBID_ISTAT_BIT);
  314. if (!per_cpu(pTBI, cpu))
  315. panic("No TBI found!");
  316. per_cpu_trap_init(cpu);
  317. preempt_disable();
  318. setup_priv();
  319. notify_cpu_starting(cpu);
  320. pr_info("CPU%u (thread %u): Booted secondary processor\n",
  321. cpu, cpu_2_hwthread_id[cpu]);
  322. calibrate_delay();
  323. smp_store_cpu_info(cpu);
  324. /*
  325. * OK, now it's safe to let the boot CPU continue
  326. */
  327. set_cpu_online(cpu, true);
  328. complete(&cpu_running);
  329. /*
  330. * Enable local interrupts.
  331. */
  332. tbi_startup_interrupt(TBID_SIGNUM_TRT);
  333. local_irq_enable();
  334. /*
  335. * OK, it's off to the idle thread for us
  336. */
  337. cpu_startup_entry(CPUHP_ONLINE);
  338. }
  339. void __init smp_cpus_done(unsigned int max_cpus)
  340. {
  341. int cpu;
  342. unsigned long bogosum = 0;
  343. for_each_online_cpu(cpu)
  344. bogosum += per_cpu(cpu_data, cpu).loops_per_jiffy;
  345. pr_info("SMP: Total of %d processors activated (%lu.%02lu BogoMIPS).\n",
  346. num_online_cpus(),
  347. bogosum / (500000/HZ),
  348. (bogosum / (5000/HZ)) % 100);
  349. }
  350. void __init smp_prepare_cpus(unsigned int max_cpus)
  351. {
  352. unsigned int cpu = smp_processor_id();
  353. init_new_context(current, &init_mm);
  354. current_thread_info()->cpu = cpu;
  355. smp_store_cpu_info(cpu);
  356. init_cpu_present(cpu_possible_mask);
  357. }
  358. void __init smp_prepare_boot_cpu(void)
  359. {
  360. unsigned int cpu = smp_processor_id();
  361. per_cpu(pTBI, cpu) = __TBI(TBID_ISTAT_BIT);
  362. if (!per_cpu(pTBI, cpu))
  363. panic("No TBI found!");
  364. }
  365. static void smp_cross_call(cpumask_t callmap, enum ipi_msg_type msg);
  366. static void send_ipi_message(const struct cpumask *mask, enum ipi_msg_type msg)
  367. {
  368. unsigned long flags;
  369. unsigned int cpu;
  370. cpumask_t map;
  371. cpumask_clear(&map);
  372. local_irq_save(flags);
  373. for_each_cpu(cpu, mask) {
  374. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  375. spin_lock(&ipi->lock);
  376. /*
  377. * KICK interrupts are queued in hardware so we'll get
  378. * multiple interrupts if we call smp_cross_call()
  379. * multiple times for one msg. The problem is that we
  380. * only have one bit for each message - we can't queue
  381. * them in software.
  382. *
  383. * The first time through ipi_handler() we'll clear
  384. * the msg bit, having done all the work. But when we
  385. * return we'll get _another_ interrupt (and another,
  386. * and another until we've handled all the queued
  387. * KICKs). Running ipi_handler() when there's no work
  388. * to do is bad because that's how kick handler
  389. * chaining detects who the KICK was intended for.
  390. * See arch/metag/kernel/kick.c for more details.
  391. *
  392. * So only add 'cpu' to 'map' if we haven't already
  393. * queued a KICK interrupt for 'msg'.
  394. */
  395. if (!(ipi->bits & (1 << msg))) {
  396. ipi->bits |= 1 << msg;
  397. cpumask_set_cpu(cpu, &map);
  398. }
  399. spin_unlock(&ipi->lock);
  400. }
  401. /*
  402. * Call the platform specific cross-CPU call function.
  403. */
  404. smp_cross_call(map, msg);
  405. local_irq_restore(flags);
  406. }
  407. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  408. {
  409. send_ipi_message(mask, IPI_CALL_FUNC);
  410. }
  411. void arch_send_call_function_single_ipi(int cpu)
  412. {
  413. send_ipi_message(cpumask_of(cpu), IPI_CALL_FUNC);
  414. }
  415. void show_ipi_list(struct seq_file *p)
  416. {
  417. unsigned int cpu;
  418. seq_puts(p, "IPI:");
  419. for_each_present_cpu(cpu)
  420. seq_printf(p, " %10lu", per_cpu(ipi_data, cpu).ipi_count);
  421. seq_putc(p, '\n');
  422. }
  423. static DEFINE_SPINLOCK(stop_lock);
  424. /*
  425. * Main handler for inter-processor interrupts
  426. *
  427. * For Meta, the ipimask now only identifies a single
  428. * category of IPI (Bit 1 IPIs have been replaced by a
  429. * different mechanism):
  430. *
  431. * Bit 0 - Inter-processor function call
  432. */
  433. static int do_IPI(void)
  434. {
  435. unsigned int cpu = smp_processor_id();
  436. struct ipi_data *ipi = &per_cpu(ipi_data, cpu);
  437. unsigned long msgs, nextmsg;
  438. int handled = 0;
  439. ipi->ipi_count++;
  440. spin_lock(&ipi->lock);
  441. msgs = ipi->bits;
  442. nextmsg = msgs & -msgs;
  443. ipi->bits &= ~nextmsg;
  444. spin_unlock(&ipi->lock);
  445. if (nextmsg) {
  446. handled = 1;
  447. nextmsg = ffz(~nextmsg);
  448. switch (nextmsg) {
  449. case IPI_RESCHEDULE:
  450. scheduler_ipi();
  451. break;
  452. case IPI_CALL_FUNC:
  453. generic_smp_call_function_interrupt();
  454. break;
  455. default:
  456. pr_crit("CPU%u: Unknown IPI message 0x%lx\n",
  457. cpu, nextmsg);
  458. break;
  459. }
  460. }
  461. return handled;
  462. }
  463. void smp_send_reschedule(int cpu)
  464. {
  465. send_ipi_message(cpumask_of(cpu), IPI_RESCHEDULE);
  466. }
  467. static void stop_this_cpu(void *data)
  468. {
  469. unsigned int cpu = smp_processor_id();
  470. if (system_state == SYSTEM_BOOTING ||
  471. system_state == SYSTEM_RUNNING) {
  472. spin_lock(&stop_lock);
  473. pr_crit("CPU%u: stopping\n", cpu);
  474. dump_stack();
  475. spin_unlock(&stop_lock);
  476. }
  477. set_cpu_online(cpu, false);
  478. local_irq_disable();
  479. hard_processor_halt(HALT_OK);
  480. }
  481. void smp_send_stop(void)
  482. {
  483. smp_call_function(stop_this_cpu, NULL, 0);
  484. }
  485. /*
  486. * not supported here
  487. */
  488. int setup_profiling_timer(unsigned int multiplier)
  489. {
  490. return -EINVAL;
  491. }
  492. /*
  493. * We use KICKs for inter-processor interrupts.
  494. *
  495. * For every CPU in "callmap" the IPI data must already have been
  496. * stored in that CPU's "ipi_data" member prior to calling this
  497. * function.
  498. */
  499. static void kick_raise_softirq(cpumask_t callmap, unsigned int irq)
  500. {
  501. int cpu;
  502. for_each_cpu(cpu, &callmap) {
  503. unsigned int thread;
  504. thread = cpu_2_hwthread_id[cpu];
  505. BUG_ON(thread == BAD_HWTHREAD_ID);
  506. metag_out32(1, T0KICKI + (thread * TnXKICK_STRIDE));
  507. }
  508. }
  509. static TBIRES ipi_handler(TBIRES State, int SigNum, int Triggers,
  510. int Inst, PTBI pTBI, int *handled)
  511. {
  512. *handled = do_IPI();
  513. return State;
  514. }
  515. static struct kick_irq_handler ipi_irq = {
  516. .func = ipi_handler,
  517. };
  518. static void smp_cross_call(cpumask_t callmap, enum ipi_msg_type msg)
  519. {
  520. kick_raise_softirq(callmap, 1);
  521. }
  522. static inline unsigned int get_core_count(void)
  523. {
  524. int i;
  525. unsigned int ret = 0;
  526. for (i = 0; i < CONFIG_NR_CPUS; i++) {
  527. if (core_reg_read(TXUCT_ID, TXENABLE_REGNUM, i))
  528. ret++;
  529. }
  530. return ret;
  531. }
  532. /*
  533. * Initialise the CPU possible map early - this describes the CPUs
  534. * which may be present or become present in the system.
  535. */
  536. void __init smp_init_cpus(void)
  537. {
  538. unsigned int i, ncores = get_core_count();
  539. /* If no hwthread_map early param was set use default mapping */
  540. for (i = 0; i < NR_CPUS; i++)
  541. if (cpu_2_hwthread_id[i] == BAD_HWTHREAD_ID) {
  542. cpu_2_hwthread_id[i] = i;
  543. hwthread_id_2_cpu[i] = i;
  544. }
  545. for (i = 0; i < ncores; i++)
  546. set_cpu_possible(i, true);
  547. kick_register_func(&ipi_irq);
  548. }