smp.c 26 KB

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