smp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. /*
  2. * linux/arch/alpha/kernel/smp.c
  3. *
  4. * 2001-07-09 Phil Ezolt (Phillip.Ezolt@compaq.com)
  5. * Renamed modified smp_call_function to smp_call_function_on_cpu()
  6. * Created an function that conforms to the old calling convention
  7. * of smp_call_function().
  8. *
  9. * This is helpful for DCPI.
  10. *
  11. */
  12. #include <linux/errno.h>
  13. #include <linux/kernel.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/module.h>
  16. #include <linux/sched.h>
  17. #include <linux/mm.h>
  18. #include <linux/threads.h>
  19. #include <linux/smp.h>
  20. #include <linux/smp_lock.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/init.h>
  23. #include <linux/delay.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/irq.h>
  26. #include <linux/cache.h>
  27. #include <linux/profile.h>
  28. #include <linux/bitops.h>
  29. #include <asm/hwrpb.h>
  30. #include <asm/ptrace.h>
  31. #include <asm/atomic.h>
  32. #include <asm/io.h>
  33. #include <asm/irq.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/pgalloc.h>
  36. #include <asm/mmu_context.h>
  37. #include <asm/tlbflush.h>
  38. #include "proto.h"
  39. #include "irq_impl.h"
  40. #define DEBUG_SMP 0
  41. #if DEBUG_SMP
  42. #define DBGS(args) printk args
  43. #else
  44. #define DBGS(args)
  45. #endif
  46. /* A collection of per-processor data. */
  47. struct cpuinfo_alpha cpu_data[NR_CPUS];
  48. EXPORT_SYMBOL(cpu_data);
  49. /* A collection of single bit ipi messages. */
  50. static struct {
  51. unsigned long bits ____cacheline_aligned;
  52. } ipi_data[NR_CPUS] __cacheline_aligned;
  53. enum ipi_message_type {
  54. IPI_RESCHEDULE,
  55. IPI_CALL_FUNC,
  56. IPI_CPU_STOP,
  57. };
  58. /* Set to a secondary's cpuid when it comes online. */
  59. static int smp_secondary_alive __initdata = 0;
  60. /* Which cpus ids came online. */
  61. cpumask_t cpu_online_map;
  62. EXPORT_SYMBOL(cpu_online_map);
  63. int smp_num_probed; /* Internal processor count */
  64. int smp_num_cpus = 1; /* Number that came online. */
  65. EXPORT_SYMBOL(smp_num_cpus);
  66. extern void calibrate_delay(void);
  67. /*
  68. * Called by both boot and secondaries to move global data into
  69. * per-processor storage.
  70. */
  71. static inline void __init
  72. smp_store_cpu_info(int cpuid)
  73. {
  74. cpu_data[cpuid].loops_per_jiffy = loops_per_jiffy;
  75. cpu_data[cpuid].last_asn = ASN_FIRST_VERSION;
  76. cpu_data[cpuid].need_new_asn = 0;
  77. cpu_data[cpuid].asn_lock = 0;
  78. }
  79. /*
  80. * Ideally sets up per-cpu profiling hooks. Doesn't do much now...
  81. */
  82. static inline void __init
  83. smp_setup_percpu_timer(int cpuid)
  84. {
  85. cpu_data[cpuid].prof_counter = 1;
  86. cpu_data[cpuid].prof_multiplier = 1;
  87. }
  88. static void __init
  89. wait_boot_cpu_to_stop(int cpuid)
  90. {
  91. unsigned long stop = jiffies + 10*HZ;
  92. while (time_before(jiffies, stop)) {
  93. if (!smp_secondary_alive)
  94. return;
  95. barrier();
  96. }
  97. printk("wait_boot_cpu_to_stop: FAILED on CPU %d, hanging now\n", cpuid);
  98. for (;;)
  99. barrier();
  100. }
  101. /*
  102. * Where secondaries begin a life of C.
  103. */
  104. void __init
  105. smp_callin(void)
  106. {
  107. int cpuid = hard_smp_processor_id();
  108. if (cpu_test_and_set(cpuid, cpu_online_map)) {
  109. printk("??, cpu 0x%x already present??\n", cpuid);
  110. BUG();
  111. }
  112. /* Turn on machine checks. */
  113. wrmces(7);
  114. /* Set trap vectors. */
  115. trap_init();
  116. /* Set interrupt vector. */
  117. wrent(entInt, 0);
  118. /* Get our local ticker going. */
  119. smp_setup_percpu_timer(cpuid);
  120. /* Call platform-specific callin, if specified */
  121. if (alpha_mv.smp_callin) alpha_mv.smp_callin();
  122. /* All kernel threads share the same mm context. */
  123. atomic_inc(&init_mm.mm_count);
  124. current->active_mm = &init_mm;
  125. /* Must have completely accurate bogos. */
  126. local_irq_enable();
  127. /* Wait boot CPU to stop with irq enabled before running
  128. calibrate_delay. */
  129. wait_boot_cpu_to_stop(cpuid);
  130. mb();
  131. calibrate_delay();
  132. smp_store_cpu_info(cpuid);
  133. /* Allow master to continue only after we written loops_per_jiffy. */
  134. wmb();
  135. smp_secondary_alive = 1;
  136. DBGS(("smp_callin: commencing CPU %d current %p active_mm %p\n",
  137. cpuid, current, current->active_mm));
  138. /* Do nothing. */
  139. cpu_idle();
  140. }
  141. /* Wait until hwrpb->txrdy is clear for cpu. Return -1 on timeout. */
  142. static int __init
  143. wait_for_txrdy (unsigned long cpumask)
  144. {
  145. unsigned long timeout;
  146. if (!(hwrpb->txrdy & cpumask))
  147. return 0;
  148. timeout = jiffies + 10*HZ;
  149. while (time_before(jiffies, timeout)) {
  150. if (!(hwrpb->txrdy & cpumask))
  151. return 0;
  152. udelay(10);
  153. barrier();
  154. }
  155. return -1;
  156. }
  157. /*
  158. * Send a message to a secondary's console. "START" is one such
  159. * interesting message. ;-)
  160. */
  161. static void __init
  162. send_secondary_console_msg(char *str, int cpuid)
  163. {
  164. struct percpu_struct *cpu;
  165. register char *cp1, *cp2;
  166. unsigned long cpumask;
  167. size_t len;
  168. cpu = (struct percpu_struct *)
  169. ((char*)hwrpb
  170. + hwrpb->processor_offset
  171. + cpuid * hwrpb->processor_size);
  172. cpumask = (1UL << cpuid);
  173. if (wait_for_txrdy(cpumask))
  174. goto timeout;
  175. cp2 = str;
  176. len = strlen(cp2);
  177. *(unsigned int *)&cpu->ipc_buffer[0] = len;
  178. cp1 = (char *) &cpu->ipc_buffer[1];
  179. memcpy(cp1, cp2, len);
  180. /* atomic test and set */
  181. wmb();
  182. set_bit(cpuid, &hwrpb->rxrdy);
  183. if (wait_for_txrdy(cpumask))
  184. goto timeout;
  185. return;
  186. timeout:
  187. printk("Processor %x not ready\n", cpuid);
  188. }
  189. /*
  190. * A secondary console wants to send a message. Receive it.
  191. */
  192. static void
  193. recv_secondary_console_msg(void)
  194. {
  195. int mycpu, i, cnt;
  196. unsigned long txrdy = hwrpb->txrdy;
  197. char *cp1, *cp2, buf[80];
  198. struct percpu_struct *cpu;
  199. DBGS(("recv_secondary_console_msg: TXRDY 0x%lx.\n", txrdy));
  200. mycpu = hard_smp_processor_id();
  201. for (i = 0; i < NR_CPUS; i++) {
  202. if (!(txrdy & (1UL << i)))
  203. continue;
  204. DBGS(("recv_secondary_console_msg: "
  205. "TXRDY contains CPU %d.\n", i));
  206. cpu = (struct percpu_struct *)
  207. ((char*)hwrpb
  208. + hwrpb->processor_offset
  209. + i * hwrpb->processor_size);
  210. DBGS(("recv_secondary_console_msg: on %d from %d"
  211. " HALT_REASON 0x%lx FLAGS 0x%lx\n",
  212. mycpu, i, cpu->halt_reason, cpu->flags));
  213. cnt = cpu->ipc_buffer[0] >> 32;
  214. if (cnt <= 0 || cnt >= 80)
  215. strcpy(buf, "<<< BOGUS MSG >>>");
  216. else {
  217. cp1 = (char *) &cpu->ipc_buffer[11];
  218. cp2 = buf;
  219. strcpy(cp2, cp1);
  220. while ((cp2 = strchr(cp2, '\r')) != 0) {
  221. *cp2 = ' ';
  222. if (cp2[1] == '\n')
  223. cp2[1] = ' ';
  224. }
  225. }
  226. DBGS((KERN_INFO "recv_secondary_console_msg: on %d "
  227. "message is '%s'\n", mycpu, buf));
  228. }
  229. hwrpb->txrdy = 0;
  230. }
  231. /*
  232. * Convince the console to have a secondary cpu begin execution.
  233. */
  234. static int __init
  235. secondary_cpu_start(int cpuid, struct task_struct *idle)
  236. {
  237. struct percpu_struct *cpu;
  238. struct pcb_struct *hwpcb, *ipcb;
  239. unsigned long timeout;
  240. cpu = (struct percpu_struct *)
  241. ((char*)hwrpb
  242. + hwrpb->processor_offset
  243. + cpuid * hwrpb->processor_size);
  244. hwpcb = (struct pcb_struct *) cpu->hwpcb;
  245. ipcb = &task_thread_info(idle)->pcb;
  246. /* Initialize the CPU's HWPCB to something just good enough for
  247. us to get started. Immediately after starting, we'll swpctx
  248. to the target idle task's pcb. Reuse the stack in the mean
  249. time. Precalculate the target PCBB. */
  250. hwpcb->ksp = (unsigned long)ipcb + sizeof(union thread_union) - 16;
  251. hwpcb->usp = 0;
  252. hwpcb->ptbr = ipcb->ptbr;
  253. hwpcb->pcc = 0;
  254. hwpcb->asn = 0;
  255. hwpcb->unique = virt_to_phys(ipcb);
  256. hwpcb->flags = ipcb->flags;
  257. hwpcb->res1 = hwpcb->res2 = 0;
  258. #if 0
  259. DBGS(("KSP 0x%lx PTBR 0x%lx VPTBR 0x%lx UNIQUE 0x%lx\n",
  260. hwpcb->ksp, hwpcb->ptbr, hwrpb->vptb, hwpcb->unique));
  261. #endif
  262. DBGS(("Starting secondary cpu %d: state 0x%lx pal_flags 0x%lx\n",
  263. cpuid, idle->state, ipcb->flags));
  264. /* Setup HWRPB fields that SRM uses to activate secondary CPU */
  265. hwrpb->CPU_restart = __smp_callin;
  266. hwrpb->CPU_restart_data = (unsigned long) __smp_callin;
  267. /* Recalculate and update the HWRPB checksum */
  268. hwrpb_update_checksum(hwrpb);
  269. /*
  270. * Send a "start" command to the specified processor.
  271. */
  272. /* SRM III 3.4.1.3 */
  273. cpu->flags |= 0x22; /* turn on Context Valid and Restart Capable */
  274. cpu->flags &= ~1; /* turn off Bootstrap In Progress */
  275. wmb();
  276. send_secondary_console_msg("START\r\n", cpuid);
  277. /* Wait 10 seconds for an ACK from the console. */
  278. timeout = jiffies + 10*HZ;
  279. while (time_before(jiffies, timeout)) {
  280. if (cpu->flags & 1)
  281. goto started;
  282. udelay(10);
  283. barrier();
  284. }
  285. printk(KERN_ERR "SMP: Processor %d failed to start.\n", cpuid);
  286. return -1;
  287. started:
  288. DBGS(("secondary_cpu_start: SUCCESS for CPU %d!!!\n", cpuid));
  289. return 0;
  290. }
  291. /*
  292. * Bring one cpu online.
  293. */
  294. static int __init
  295. smp_boot_one_cpu(int cpuid)
  296. {
  297. struct task_struct *idle;
  298. unsigned long timeout;
  299. /* Cook up an idler for this guy. Note that the address we
  300. give to kernel_thread is irrelevant -- it's going to start
  301. where HWRPB.CPU_restart says to start. But this gets all
  302. the other task-y sort of data structures set up like we
  303. wish. We can't use kernel_thread since we must avoid
  304. rescheduling the child. */
  305. idle = fork_idle(cpuid);
  306. if (IS_ERR(idle))
  307. panic("failed fork for CPU %d", cpuid);
  308. DBGS(("smp_boot_one_cpu: CPU %d state 0x%lx flags 0x%lx\n",
  309. cpuid, idle->state, idle->flags));
  310. /* Signal the secondary to wait a moment. */
  311. smp_secondary_alive = -1;
  312. /* Whirrr, whirrr, whirrrrrrrrr... */
  313. if (secondary_cpu_start(cpuid, idle))
  314. return -1;
  315. /* Notify the secondary CPU it can run calibrate_delay. */
  316. mb();
  317. smp_secondary_alive = 0;
  318. /* We've been acked by the console; wait one second for
  319. the task to start up for real. */
  320. timeout = jiffies + 1*HZ;
  321. while (time_before(jiffies, timeout)) {
  322. if (smp_secondary_alive == 1)
  323. goto alive;
  324. udelay(10);
  325. barrier();
  326. }
  327. /* We failed to boot the CPU. */
  328. printk(KERN_ERR "SMP: Processor %d is stuck.\n", cpuid);
  329. return -1;
  330. alive:
  331. /* Another "Red Snapper". */
  332. return 0;
  333. }
  334. /*
  335. * Called from setup_arch. Detect an SMP system and which processors
  336. * are present.
  337. */
  338. void __init
  339. setup_smp(void)
  340. {
  341. struct percpu_struct *cpubase, *cpu;
  342. unsigned long i;
  343. if (boot_cpuid != 0) {
  344. printk(KERN_WARNING "SMP: Booting off cpu %d instead of 0?\n",
  345. boot_cpuid);
  346. }
  347. if (hwrpb->nr_processors > 1) {
  348. int boot_cpu_palrev;
  349. DBGS(("setup_smp: nr_processors %ld\n",
  350. hwrpb->nr_processors));
  351. cpubase = (struct percpu_struct *)
  352. ((char*)hwrpb + hwrpb->processor_offset);
  353. boot_cpu_palrev = cpubase->pal_revision;
  354. for (i = 0; i < hwrpb->nr_processors; i++) {
  355. cpu = (struct percpu_struct *)
  356. ((char *)cpubase + i*hwrpb->processor_size);
  357. if ((cpu->flags & 0x1cc) == 0x1cc) {
  358. smp_num_probed++;
  359. /* Assume here that "whami" == index */
  360. cpu_set(i, cpu_present_map);
  361. cpu->pal_revision = boot_cpu_palrev;
  362. }
  363. DBGS(("setup_smp: CPU %d: flags 0x%lx type 0x%lx\n",
  364. i, cpu->flags, cpu->type));
  365. DBGS(("setup_smp: CPU %d: PAL rev 0x%lx\n",
  366. i, cpu->pal_revision));
  367. }
  368. } else {
  369. smp_num_probed = 1;
  370. }
  371. printk(KERN_INFO "SMP: %d CPUs probed -- cpu_present_map = %lx\n",
  372. smp_num_probed, cpu_present_map.bits[0]);
  373. }
  374. /*
  375. * Called by smp_init prepare the secondaries
  376. */
  377. void __init
  378. smp_prepare_cpus(unsigned int max_cpus)
  379. {
  380. /* Take care of some initial bookkeeping. */
  381. memset(ipi_data, 0, sizeof(ipi_data));
  382. current_thread_info()->cpu = boot_cpuid;
  383. smp_store_cpu_info(boot_cpuid);
  384. smp_setup_percpu_timer(boot_cpuid);
  385. /* Nothing to do on a UP box, or when told not to. */
  386. if (smp_num_probed == 1 || max_cpus == 0) {
  387. cpu_present_map = cpumask_of_cpu(boot_cpuid);
  388. printk(KERN_INFO "SMP mode deactivated.\n");
  389. return;
  390. }
  391. printk(KERN_INFO "SMP starting up secondaries.\n");
  392. smp_num_cpus = smp_num_probed;
  393. }
  394. void __devinit
  395. smp_prepare_boot_cpu(void)
  396. {
  397. }
  398. int __devinit
  399. __cpu_up(unsigned int cpu)
  400. {
  401. smp_boot_one_cpu(cpu);
  402. return cpu_online(cpu) ? 0 : -ENOSYS;
  403. }
  404. void __init
  405. smp_cpus_done(unsigned int max_cpus)
  406. {
  407. int cpu;
  408. unsigned long bogosum = 0;
  409. for(cpu = 0; cpu < NR_CPUS; cpu++)
  410. if (cpu_online(cpu))
  411. bogosum += cpu_data[cpu].loops_per_jiffy;
  412. printk(KERN_INFO "SMP: Total of %d processors activated "
  413. "(%lu.%02lu BogoMIPS).\n",
  414. num_online_cpus(),
  415. (bogosum + 2500) / (500000/HZ),
  416. ((bogosum + 2500) / (5000/HZ)) % 100);
  417. }
  418. void
  419. smp_percpu_timer_interrupt(struct pt_regs *regs)
  420. {
  421. struct pt_regs *old_regs;
  422. int cpu = smp_processor_id();
  423. unsigned long user = user_mode(regs);
  424. struct cpuinfo_alpha *data = &cpu_data[cpu];
  425. old_regs = set_irq_regs(regs);
  426. /* Record kernel PC. */
  427. profile_tick(CPU_PROFILING);
  428. if (!--data->prof_counter) {
  429. /* We need to make like a normal interrupt -- otherwise
  430. timer interrupts ignore the global interrupt lock,
  431. which would be a Bad Thing. */
  432. irq_enter();
  433. update_process_times(user);
  434. data->prof_counter = data->prof_multiplier;
  435. irq_exit();
  436. }
  437. set_irq_regs(old_regs);
  438. }
  439. int __init
  440. setup_profiling_timer(unsigned int multiplier)
  441. {
  442. return -EINVAL;
  443. }
  444. static void
  445. send_ipi_message(cpumask_t to_whom, enum ipi_message_type operation)
  446. {
  447. int i;
  448. mb();
  449. for_each_cpu_mask(i, to_whom)
  450. set_bit(operation, &ipi_data[i].bits);
  451. mb();
  452. for_each_cpu_mask(i, to_whom)
  453. wripir(i);
  454. }
  455. /* Structure and data for smp_call_function. This is designed to
  456. minimize static memory requirements. Plus it looks cleaner. */
  457. struct smp_call_struct {
  458. void (*func) (void *info);
  459. void *info;
  460. long wait;
  461. atomic_t unstarted_count;
  462. atomic_t unfinished_count;
  463. };
  464. static struct smp_call_struct *smp_call_function_data;
  465. /* Atomicly drop data into a shared pointer. The pointer is free if
  466. it is initially locked. If retry, spin until free. */
  467. static int
  468. pointer_lock (void *lock, void *data, int retry)
  469. {
  470. void *old, *tmp;
  471. mb();
  472. again:
  473. /* Compare and swap with zero. */
  474. asm volatile (
  475. "1: ldq_l %0,%1\n"
  476. " mov %3,%2\n"
  477. " bne %0,2f\n"
  478. " stq_c %2,%1\n"
  479. " beq %2,1b\n"
  480. "2:"
  481. : "=&r"(old), "=m"(*(void **)lock), "=&r"(tmp)
  482. : "r"(data)
  483. : "memory");
  484. if (old == 0)
  485. return 0;
  486. if (! retry)
  487. return -EBUSY;
  488. while (*(void **)lock)
  489. barrier();
  490. goto again;
  491. }
  492. void
  493. handle_ipi(struct pt_regs *regs)
  494. {
  495. int this_cpu = smp_processor_id();
  496. unsigned long *pending_ipis = &ipi_data[this_cpu].bits;
  497. unsigned long ops;
  498. #if 0
  499. DBGS(("handle_ipi: on CPU %d ops 0x%lx PC 0x%lx\n",
  500. this_cpu, *pending_ipis, regs->pc));
  501. #endif
  502. mb(); /* Order interrupt and bit testing. */
  503. while ((ops = xchg(pending_ipis, 0)) != 0) {
  504. mb(); /* Order bit clearing and data access. */
  505. do {
  506. unsigned long which;
  507. which = ops & -ops;
  508. ops &= ~which;
  509. which = __ffs(which);
  510. switch (which) {
  511. case IPI_RESCHEDULE:
  512. /* Reschedule callback. Everything to be done
  513. is done by the interrupt return path. */
  514. break;
  515. case IPI_CALL_FUNC:
  516. {
  517. struct smp_call_struct *data;
  518. void (*func)(void *info);
  519. void *info;
  520. int wait;
  521. data = smp_call_function_data;
  522. func = data->func;
  523. info = data->info;
  524. wait = data->wait;
  525. /* Notify the sending CPU that the data has been
  526. received, and execution is about to begin. */
  527. mb();
  528. atomic_dec (&data->unstarted_count);
  529. /* At this point the structure may be gone unless
  530. wait is true. */
  531. (*func)(info);
  532. /* Notify the sending CPU that the task is done. */
  533. mb();
  534. if (wait) atomic_dec (&data->unfinished_count);
  535. break;
  536. }
  537. case IPI_CPU_STOP:
  538. halt();
  539. default:
  540. printk(KERN_CRIT "Unknown IPI on CPU %d: %lu\n",
  541. this_cpu, which);
  542. break;
  543. }
  544. } while (ops);
  545. mb(); /* Order data access and bit testing. */
  546. }
  547. cpu_data[this_cpu].ipi_count++;
  548. if (hwrpb->txrdy)
  549. recv_secondary_console_msg();
  550. }
  551. void
  552. smp_send_reschedule(int cpu)
  553. {
  554. #ifdef DEBUG_IPI_MSG
  555. if (cpu == hard_smp_processor_id())
  556. printk(KERN_WARNING
  557. "smp_send_reschedule: Sending IPI to self.\n");
  558. #endif
  559. send_ipi_message(cpumask_of_cpu(cpu), IPI_RESCHEDULE);
  560. }
  561. void
  562. smp_send_stop(void)
  563. {
  564. cpumask_t to_whom = cpu_possible_map;
  565. cpu_clear(smp_processor_id(), to_whom);
  566. #ifdef DEBUG_IPI_MSG
  567. if (hard_smp_processor_id() != boot_cpu_id)
  568. printk(KERN_WARNING "smp_send_stop: Not on boot cpu.\n");
  569. #endif
  570. send_ipi_message(to_whom, IPI_CPU_STOP);
  571. }
  572. /*
  573. * Run a function on all other CPUs.
  574. * <func> The function to run. This must be fast and non-blocking.
  575. * <info> An arbitrary pointer to pass to the function.
  576. * <retry> If true, keep retrying until ready.
  577. * <wait> If true, wait until function has completed on other CPUs.
  578. * [RETURNS] 0 on success, else a negative status code.
  579. *
  580. * Does not return until remote CPUs are nearly ready to execute <func>
  581. * or are or have executed.
  582. * You must not call this function with disabled interrupts or from a
  583. * hardware interrupt handler or from a bottom half handler.
  584. */
  585. int
  586. smp_call_function_on_cpu (void (*func) (void *info), void *info, int retry,
  587. int wait, cpumask_t to_whom)
  588. {
  589. struct smp_call_struct data;
  590. unsigned long timeout;
  591. int num_cpus_to_call;
  592. /* Can deadlock when called with interrupts disabled */
  593. WARN_ON(irqs_disabled());
  594. data.func = func;
  595. data.info = info;
  596. data.wait = wait;
  597. cpu_clear(smp_processor_id(), to_whom);
  598. num_cpus_to_call = cpus_weight(to_whom);
  599. atomic_set(&data.unstarted_count, num_cpus_to_call);
  600. atomic_set(&data.unfinished_count, num_cpus_to_call);
  601. /* Acquire the smp_call_function_data mutex. */
  602. if (pointer_lock(&smp_call_function_data, &data, retry))
  603. return -EBUSY;
  604. /* Send a message to the requested CPUs. */
  605. send_ipi_message(to_whom, IPI_CALL_FUNC);
  606. /* Wait for a minimal response. */
  607. timeout = jiffies + HZ;
  608. while (atomic_read (&data.unstarted_count) > 0
  609. && time_before (jiffies, timeout))
  610. barrier();
  611. /* If there's no response yet, log a message but allow a longer
  612. * timeout period -- if we get a response this time, log
  613. * a message saying when we got it..
  614. */
  615. if (atomic_read(&data.unstarted_count) > 0) {
  616. long start_time = jiffies;
  617. printk(KERN_ERR "%s: initial timeout -- trying long wait\n",
  618. __FUNCTION__);
  619. timeout = jiffies + 30 * HZ;
  620. while (atomic_read(&data.unstarted_count) > 0
  621. && time_before(jiffies, timeout))
  622. barrier();
  623. if (atomic_read(&data.unstarted_count) <= 0) {
  624. long delta = jiffies - start_time;
  625. printk(KERN_ERR
  626. "%s: response %ld.%ld seconds into long wait\n",
  627. __FUNCTION__, delta / HZ,
  628. (100 * (delta - ((delta / HZ) * HZ))) / HZ);
  629. }
  630. }
  631. /* We either got one or timed out -- clear the lock. */
  632. mb();
  633. smp_call_function_data = NULL;
  634. /*
  635. * If after both the initial and long timeout periods we still don't
  636. * have a response, something is very wrong...
  637. */
  638. BUG_ON(atomic_read (&data.unstarted_count) > 0);
  639. /* Wait for a complete response, if needed. */
  640. if (wait) {
  641. while (atomic_read (&data.unfinished_count) > 0)
  642. barrier();
  643. }
  644. return 0;
  645. }
  646. EXPORT_SYMBOL(smp_call_function_on_cpu);
  647. int
  648. smp_call_function (void (*func) (void *info), void *info, int retry, int wait)
  649. {
  650. return smp_call_function_on_cpu (func, info, retry, wait,
  651. cpu_online_map);
  652. }
  653. EXPORT_SYMBOL(smp_call_function);
  654. static void
  655. ipi_imb(void *ignored)
  656. {
  657. imb();
  658. }
  659. void
  660. smp_imb(void)
  661. {
  662. /* Must wait other processors to flush their icache before continue. */
  663. if (on_each_cpu(ipi_imb, NULL, 1, 1))
  664. printk(KERN_CRIT "smp_imb: timed out\n");
  665. }
  666. EXPORT_SYMBOL(smp_imb);
  667. static void
  668. ipi_flush_tlb_all(void *ignored)
  669. {
  670. tbia();
  671. }
  672. void
  673. flush_tlb_all(void)
  674. {
  675. /* Although we don't have any data to pass, we do want to
  676. synchronize with the other processors. */
  677. if (on_each_cpu(ipi_flush_tlb_all, NULL, 1, 1)) {
  678. printk(KERN_CRIT "flush_tlb_all: timed out\n");
  679. }
  680. }
  681. #define asn_locked() (cpu_data[smp_processor_id()].asn_lock)
  682. static void
  683. ipi_flush_tlb_mm(void *x)
  684. {
  685. struct mm_struct *mm = (struct mm_struct *) x;
  686. if (mm == current->active_mm && !asn_locked())
  687. flush_tlb_current(mm);
  688. else
  689. flush_tlb_other(mm);
  690. }
  691. void
  692. flush_tlb_mm(struct mm_struct *mm)
  693. {
  694. preempt_disable();
  695. if (mm == current->active_mm) {
  696. flush_tlb_current(mm);
  697. if (atomic_read(&mm->mm_users) <= 1) {
  698. int cpu, this_cpu = smp_processor_id();
  699. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  700. if (!cpu_online(cpu) || cpu == this_cpu)
  701. continue;
  702. if (mm->context[cpu])
  703. mm->context[cpu] = 0;
  704. }
  705. preempt_enable();
  706. return;
  707. }
  708. }
  709. if (smp_call_function(ipi_flush_tlb_mm, mm, 1, 1)) {
  710. printk(KERN_CRIT "flush_tlb_mm: timed out\n");
  711. }
  712. preempt_enable();
  713. }
  714. EXPORT_SYMBOL(flush_tlb_mm);
  715. struct flush_tlb_page_struct {
  716. struct vm_area_struct *vma;
  717. struct mm_struct *mm;
  718. unsigned long addr;
  719. };
  720. static void
  721. ipi_flush_tlb_page(void *x)
  722. {
  723. struct flush_tlb_page_struct *data = (struct flush_tlb_page_struct *)x;
  724. struct mm_struct * mm = data->mm;
  725. if (mm == current->active_mm && !asn_locked())
  726. flush_tlb_current_page(mm, data->vma, data->addr);
  727. else
  728. flush_tlb_other(mm);
  729. }
  730. void
  731. flush_tlb_page(struct vm_area_struct *vma, unsigned long addr)
  732. {
  733. struct flush_tlb_page_struct data;
  734. struct mm_struct *mm = vma->vm_mm;
  735. preempt_disable();
  736. if (mm == current->active_mm) {
  737. flush_tlb_current_page(mm, vma, addr);
  738. if (atomic_read(&mm->mm_users) <= 1) {
  739. int cpu, this_cpu = smp_processor_id();
  740. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  741. if (!cpu_online(cpu) || cpu == this_cpu)
  742. continue;
  743. if (mm->context[cpu])
  744. mm->context[cpu] = 0;
  745. }
  746. preempt_enable();
  747. return;
  748. }
  749. }
  750. data.vma = vma;
  751. data.mm = mm;
  752. data.addr = addr;
  753. if (smp_call_function(ipi_flush_tlb_page, &data, 1, 1)) {
  754. printk(KERN_CRIT "flush_tlb_page: timed out\n");
  755. }
  756. preempt_enable();
  757. }
  758. EXPORT_SYMBOL(flush_tlb_page);
  759. void
  760. flush_tlb_range(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  761. {
  762. /* On the Alpha we always flush the whole user tlb. */
  763. flush_tlb_mm(vma->vm_mm);
  764. }
  765. EXPORT_SYMBOL(flush_tlb_range);
  766. static void
  767. ipi_flush_icache_page(void *x)
  768. {
  769. struct mm_struct *mm = (struct mm_struct *) x;
  770. if (mm == current->active_mm && !asn_locked())
  771. __load_new_mm_context(mm);
  772. else
  773. flush_tlb_other(mm);
  774. }
  775. void
  776. flush_icache_user_range(struct vm_area_struct *vma, struct page *page,
  777. unsigned long addr, int len)
  778. {
  779. struct mm_struct *mm = vma->vm_mm;
  780. if ((vma->vm_flags & VM_EXEC) == 0)
  781. return;
  782. preempt_disable();
  783. if (mm == current->active_mm) {
  784. __load_new_mm_context(mm);
  785. if (atomic_read(&mm->mm_users) <= 1) {
  786. int cpu, this_cpu = smp_processor_id();
  787. for (cpu = 0; cpu < NR_CPUS; cpu++) {
  788. if (!cpu_online(cpu) || cpu == this_cpu)
  789. continue;
  790. if (mm->context[cpu])
  791. mm->context[cpu] = 0;
  792. }
  793. preempt_enable();
  794. return;
  795. }
  796. }
  797. if (smp_call_function(ipi_flush_icache_page, mm, 1, 1)) {
  798. printk(KERN_CRIT "flush_icache_page: timed out\n");
  799. }
  800. preempt_enable();
  801. }