smp.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247
  1. /* smp.c: Sparc64 SMP support.
  2. *
  3. * Copyright (C) 1997 David S. Miller (davem@caip.rutgers.edu)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/kernel.h>
  7. #include <linux/sched.h>
  8. #include <linux/mm.h>
  9. #include <linux/pagemap.h>
  10. #include <linux/threads.h>
  11. #include <linux/smp.h>
  12. #include <linux/smp_lock.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/kernel_stat.h>
  15. #include <linux/delay.h>
  16. #include <linux/init.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/fs.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/cache.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/profile.h>
  23. #include <linux/bootmem.h>
  24. #include <asm/head.h>
  25. #include <asm/ptrace.h>
  26. #include <asm/atomic.h>
  27. #include <asm/tlbflush.h>
  28. #include <asm/mmu_context.h>
  29. #include <asm/cpudata.h>
  30. #include <asm/irq.h>
  31. #include <asm/page.h>
  32. #include <asm/pgtable.h>
  33. #include <asm/oplib.h>
  34. #include <asm/uaccess.h>
  35. #include <asm/timer.h>
  36. #include <asm/starfire.h>
  37. #include <asm/tlb.h>
  38. extern int linux_num_cpus;
  39. extern void calibrate_delay(void);
  40. /* Please don't make this stuff initdata!!! --DaveM */
  41. static unsigned char boot_cpu_id;
  42. cpumask_t cpu_online_map __read_mostly = CPU_MASK_NONE;
  43. cpumask_t phys_cpu_present_map __read_mostly = CPU_MASK_NONE;
  44. static cpumask_t smp_commenced_mask;
  45. static cpumask_t cpu_callout_map;
  46. void smp_info(struct seq_file *m)
  47. {
  48. int i;
  49. seq_printf(m, "State:\n");
  50. for (i = 0; i < NR_CPUS; i++) {
  51. if (cpu_online(i))
  52. seq_printf(m,
  53. "CPU%d:\t\tonline\n", i);
  54. }
  55. }
  56. void smp_bogo(struct seq_file *m)
  57. {
  58. int i;
  59. for (i = 0; i < NR_CPUS; i++)
  60. if (cpu_online(i))
  61. seq_printf(m,
  62. "Cpu%dBogo\t: %lu.%02lu\n"
  63. "Cpu%dClkTck\t: %016lx\n",
  64. i, cpu_data(i).udelay_val / (500000/HZ),
  65. (cpu_data(i).udelay_val / (5000/HZ)) % 100,
  66. i, cpu_data(i).clock_tick);
  67. }
  68. void __init smp_store_cpu_info(int id)
  69. {
  70. int cpu_node;
  71. /* multiplier and counter set by
  72. smp_setup_percpu_timer() */
  73. cpu_data(id).udelay_val = loops_per_jiffy;
  74. cpu_find_by_mid(id, &cpu_node);
  75. cpu_data(id).clock_tick = prom_getintdefault(cpu_node,
  76. "clock-frequency", 0);
  77. cpu_data(id).pgcache_size = 0;
  78. cpu_data(id).pte_cache[0] = NULL;
  79. cpu_data(id).pte_cache[1] = NULL;
  80. cpu_data(id).pgd_cache = NULL;
  81. cpu_data(id).idle_volume = 1;
  82. }
  83. static void smp_setup_percpu_timer(void);
  84. static volatile unsigned long callin_flag = 0;
  85. extern void inherit_locked_prom_mappings(int save_p);
  86. static inline void cpu_setup_percpu_base(unsigned long cpu_id)
  87. {
  88. __asm__ __volatile__("mov %0, %%g5\n\t"
  89. "stxa %0, [%1] %2\n\t"
  90. "membar #Sync"
  91. : /* no outputs */
  92. : "r" (__per_cpu_offset(cpu_id)),
  93. "r" (TSB_REG), "i" (ASI_IMMU));
  94. }
  95. void __init smp_callin(void)
  96. {
  97. int cpuid = hard_smp_processor_id();
  98. inherit_locked_prom_mappings(0);
  99. __flush_tlb_all();
  100. cpu_setup_percpu_base(cpuid);
  101. smp_setup_percpu_timer();
  102. if (cheetah_pcache_forced_on)
  103. cheetah_enable_pcache();
  104. local_irq_enable();
  105. calibrate_delay();
  106. smp_store_cpu_info(cpuid);
  107. callin_flag = 1;
  108. __asm__ __volatile__("membar #Sync\n\t"
  109. "flush %%g6" : : : "memory");
  110. /* Clear this or we will die instantly when we
  111. * schedule back to this idler...
  112. */
  113. current_thread_info()->new_child = 0;
  114. /* Attach to the address space of init_task. */
  115. atomic_inc(&init_mm.mm_count);
  116. current->active_mm = &init_mm;
  117. while (!cpu_isset(cpuid, smp_commenced_mask))
  118. rmb();
  119. cpu_set(cpuid, cpu_online_map);
  120. }
  121. void cpu_panic(void)
  122. {
  123. printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
  124. panic("SMP bolixed\n");
  125. }
  126. static unsigned long current_tick_offset __read_mostly;
  127. /* This tick register synchronization scheme is taken entirely from
  128. * the ia64 port, see arch/ia64/kernel/smpboot.c for details and credit.
  129. *
  130. * The only change I've made is to rework it so that the master
  131. * initiates the synchonization instead of the slave. -DaveM
  132. */
  133. #define MASTER 0
  134. #define SLAVE (SMP_CACHE_BYTES/sizeof(unsigned long))
  135. #define NUM_ROUNDS 64 /* magic value */
  136. #define NUM_ITERS 5 /* likewise */
  137. static DEFINE_SPINLOCK(itc_sync_lock);
  138. static unsigned long go[SLAVE + 1];
  139. #define DEBUG_TICK_SYNC 0
  140. static inline long get_delta (long *rt, long *master)
  141. {
  142. unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
  143. unsigned long tcenter, t0, t1, tm;
  144. unsigned long i;
  145. for (i = 0; i < NUM_ITERS; i++) {
  146. t0 = tick_ops->get_tick();
  147. go[MASTER] = 1;
  148. membar_storeload();
  149. while (!(tm = go[SLAVE]))
  150. rmb();
  151. go[SLAVE] = 0;
  152. wmb();
  153. t1 = tick_ops->get_tick();
  154. if (t1 - t0 < best_t1 - best_t0)
  155. best_t0 = t0, best_t1 = t1, best_tm = tm;
  156. }
  157. *rt = best_t1 - best_t0;
  158. *master = best_tm - best_t0;
  159. /* average best_t0 and best_t1 without overflow: */
  160. tcenter = (best_t0/2 + best_t1/2);
  161. if (best_t0 % 2 + best_t1 % 2 == 2)
  162. tcenter++;
  163. return tcenter - best_tm;
  164. }
  165. void smp_synchronize_tick_client(void)
  166. {
  167. long i, delta, adj, adjust_latency = 0, done = 0;
  168. unsigned long flags, rt, master_time_stamp, bound;
  169. #if DEBUG_TICK_SYNC
  170. struct {
  171. long rt; /* roundtrip time */
  172. long master; /* master's timestamp */
  173. long diff; /* difference between midpoint and master's timestamp */
  174. long lat; /* estimate of itc adjustment latency */
  175. } t[NUM_ROUNDS];
  176. #endif
  177. go[MASTER] = 1;
  178. while (go[MASTER])
  179. rmb();
  180. local_irq_save(flags);
  181. {
  182. for (i = 0; i < NUM_ROUNDS; i++) {
  183. delta = get_delta(&rt, &master_time_stamp);
  184. if (delta == 0) {
  185. done = 1; /* let's lock on to this... */
  186. bound = rt;
  187. }
  188. if (!done) {
  189. if (i > 0) {
  190. adjust_latency += -delta;
  191. adj = -delta + adjust_latency/4;
  192. } else
  193. adj = -delta;
  194. tick_ops->add_tick(adj, current_tick_offset);
  195. }
  196. #if DEBUG_TICK_SYNC
  197. t[i].rt = rt;
  198. t[i].master = master_time_stamp;
  199. t[i].diff = delta;
  200. t[i].lat = adjust_latency/4;
  201. #endif
  202. }
  203. }
  204. local_irq_restore(flags);
  205. #if DEBUG_TICK_SYNC
  206. for (i = 0; i < NUM_ROUNDS; i++)
  207. printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
  208. t[i].rt, t[i].master, t[i].diff, t[i].lat);
  209. #endif
  210. printk(KERN_INFO "CPU %d: synchronized TICK with master CPU (last diff %ld cycles,"
  211. "maxerr %lu cycles)\n", smp_processor_id(), delta, rt);
  212. }
  213. static void smp_start_sync_tick_client(int cpu);
  214. static void smp_synchronize_one_tick(int cpu)
  215. {
  216. unsigned long flags, i;
  217. go[MASTER] = 0;
  218. smp_start_sync_tick_client(cpu);
  219. /* wait for client to be ready */
  220. while (!go[MASTER])
  221. rmb();
  222. /* now let the client proceed into his loop */
  223. go[MASTER] = 0;
  224. membar_storeload();
  225. spin_lock_irqsave(&itc_sync_lock, flags);
  226. {
  227. for (i = 0; i < NUM_ROUNDS*NUM_ITERS; i++) {
  228. while (!go[MASTER])
  229. rmb();
  230. go[MASTER] = 0;
  231. wmb();
  232. go[SLAVE] = tick_ops->get_tick();
  233. membar_storeload();
  234. }
  235. }
  236. spin_unlock_irqrestore(&itc_sync_lock, flags);
  237. }
  238. extern unsigned long sparc64_cpu_startup;
  239. /* The OBP cpu startup callback truncates the 3rd arg cookie to
  240. * 32-bits (I think) so to be safe we have it read the pointer
  241. * contained here so we work on >4GB machines. -DaveM
  242. */
  243. static struct thread_info *cpu_new_thread = NULL;
  244. static int __devinit smp_boot_one_cpu(unsigned int cpu)
  245. {
  246. unsigned long entry =
  247. (unsigned long)(&sparc64_cpu_startup);
  248. unsigned long cookie =
  249. (unsigned long)(&cpu_new_thread);
  250. struct task_struct *p;
  251. int timeout, ret, cpu_node;
  252. p = fork_idle(cpu);
  253. callin_flag = 0;
  254. cpu_new_thread = p->thread_info;
  255. cpu_set(cpu, cpu_callout_map);
  256. cpu_find_by_mid(cpu, &cpu_node);
  257. prom_startcpu(cpu_node, entry, cookie);
  258. for (timeout = 0; timeout < 5000000; timeout++) {
  259. if (callin_flag)
  260. break;
  261. udelay(100);
  262. }
  263. if (callin_flag) {
  264. ret = 0;
  265. } else {
  266. printk("Processor %d is stuck.\n", cpu);
  267. cpu_clear(cpu, cpu_callout_map);
  268. ret = -ENODEV;
  269. }
  270. cpu_new_thread = NULL;
  271. return ret;
  272. }
  273. static void spitfire_xcall_helper(u64 data0, u64 data1, u64 data2, u64 pstate, unsigned long cpu)
  274. {
  275. u64 result, target;
  276. int stuck, tmp;
  277. if (this_is_starfire) {
  278. /* map to real upaid */
  279. cpu = (((cpu & 0x3c) << 1) |
  280. ((cpu & 0x40) >> 4) |
  281. (cpu & 0x3));
  282. }
  283. target = (cpu << 14) | 0x70;
  284. again:
  285. /* Ok, this is the real Spitfire Errata #54.
  286. * One must read back from a UDB internal register
  287. * after writes to the UDB interrupt dispatch, but
  288. * before the membar Sync for that write.
  289. * So we use the high UDB control register (ASI 0x7f,
  290. * ADDR 0x20) for the dummy read. -DaveM
  291. */
  292. tmp = 0x40;
  293. __asm__ __volatile__(
  294. "wrpr %1, %2, %%pstate\n\t"
  295. "stxa %4, [%0] %3\n\t"
  296. "stxa %5, [%0+%8] %3\n\t"
  297. "add %0, %8, %0\n\t"
  298. "stxa %6, [%0+%8] %3\n\t"
  299. "membar #Sync\n\t"
  300. "stxa %%g0, [%7] %3\n\t"
  301. "membar #Sync\n\t"
  302. "mov 0x20, %%g1\n\t"
  303. "ldxa [%%g1] 0x7f, %%g0\n\t"
  304. "membar #Sync"
  305. : "=r" (tmp)
  306. : "r" (pstate), "i" (PSTATE_IE), "i" (ASI_INTR_W),
  307. "r" (data0), "r" (data1), "r" (data2), "r" (target),
  308. "r" (0x10), "0" (tmp)
  309. : "g1");
  310. /* NOTE: PSTATE_IE is still clear. */
  311. stuck = 100000;
  312. do {
  313. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  314. : "=r" (result)
  315. : "i" (ASI_INTR_DISPATCH_STAT));
  316. if (result == 0) {
  317. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  318. : : "r" (pstate));
  319. return;
  320. }
  321. stuck -= 1;
  322. if (stuck == 0)
  323. break;
  324. } while (result & 0x1);
  325. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  326. : : "r" (pstate));
  327. if (stuck == 0) {
  328. printk("CPU[%d]: mondo stuckage result[%016lx]\n",
  329. smp_processor_id(), result);
  330. } else {
  331. udelay(2);
  332. goto again;
  333. }
  334. }
  335. static __inline__ void spitfire_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
  336. {
  337. u64 pstate;
  338. int i;
  339. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  340. for_each_cpu_mask(i, mask)
  341. spitfire_xcall_helper(data0, data1, data2, pstate, i);
  342. }
  343. /* Cheetah now allows to send the whole 64-bytes of data in the interrupt
  344. * packet, but we have no use for that. However we do take advantage of
  345. * the new pipelining feature (ie. dispatch to multiple cpus simultaneously).
  346. */
  347. static void cheetah_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
  348. {
  349. u64 pstate, ver;
  350. int nack_busy_id, is_jalapeno;
  351. if (cpus_empty(mask))
  352. return;
  353. /* Unfortunately, someone at Sun had the brilliant idea to make the
  354. * busy/nack fields hard-coded by ITID number for this Ultra-III
  355. * derivative processor.
  356. */
  357. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  358. is_jalapeno = ((ver >> 32) == 0x003e0016);
  359. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  360. retry:
  361. __asm__ __volatile__("wrpr %0, %1, %%pstate\n\t"
  362. : : "r" (pstate), "i" (PSTATE_IE));
  363. /* Setup the dispatch data registers. */
  364. __asm__ __volatile__("stxa %0, [%3] %6\n\t"
  365. "stxa %1, [%4] %6\n\t"
  366. "stxa %2, [%5] %6\n\t"
  367. "membar #Sync\n\t"
  368. : /* no outputs */
  369. : "r" (data0), "r" (data1), "r" (data2),
  370. "r" (0x40), "r" (0x50), "r" (0x60),
  371. "i" (ASI_INTR_W));
  372. nack_busy_id = 0;
  373. {
  374. int i;
  375. for_each_cpu_mask(i, mask) {
  376. u64 target = (i << 14) | 0x70;
  377. if (!is_jalapeno)
  378. target |= (nack_busy_id << 24);
  379. __asm__ __volatile__(
  380. "stxa %%g0, [%0] %1\n\t"
  381. "membar #Sync\n\t"
  382. : /* no outputs */
  383. : "r" (target), "i" (ASI_INTR_W));
  384. nack_busy_id++;
  385. }
  386. }
  387. /* Now, poll for completion. */
  388. {
  389. u64 dispatch_stat;
  390. long stuck;
  391. stuck = 100000 * nack_busy_id;
  392. do {
  393. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  394. : "=r" (dispatch_stat)
  395. : "i" (ASI_INTR_DISPATCH_STAT));
  396. if (dispatch_stat == 0UL) {
  397. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  398. : : "r" (pstate));
  399. return;
  400. }
  401. if (!--stuck)
  402. break;
  403. } while (dispatch_stat & 0x5555555555555555UL);
  404. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  405. : : "r" (pstate));
  406. if ((dispatch_stat & ~(0x5555555555555555UL)) == 0) {
  407. /* Busy bits will not clear, continue instead
  408. * of freezing up on this cpu.
  409. */
  410. printk("CPU[%d]: mondo stuckage result[%016lx]\n",
  411. smp_processor_id(), dispatch_stat);
  412. } else {
  413. int i, this_busy_nack = 0;
  414. /* Delay some random time with interrupts enabled
  415. * to prevent deadlock.
  416. */
  417. udelay(2 * nack_busy_id);
  418. /* Clear out the mask bits for cpus which did not
  419. * NACK us.
  420. */
  421. for_each_cpu_mask(i, mask) {
  422. u64 check_mask;
  423. if (is_jalapeno)
  424. check_mask = (0x2UL << (2*i));
  425. else
  426. check_mask = (0x2UL <<
  427. this_busy_nack);
  428. if ((dispatch_stat & check_mask) == 0)
  429. cpu_clear(i, mask);
  430. this_busy_nack += 2;
  431. }
  432. goto retry;
  433. }
  434. }
  435. }
  436. /* Send cross call to all processors mentioned in MASK
  437. * except self.
  438. */
  439. static void smp_cross_call_masked(unsigned long *func, u32 ctx, u64 data1, u64 data2, cpumask_t mask)
  440. {
  441. u64 data0 = (((u64)ctx)<<32 | (((u64)func) & 0xffffffff));
  442. int this_cpu = get_cpu();
  443. cpus_and(mask, mask, cpu_online_map);
  444. cpu_clear(this_cpu, mask);
  445. if (tlb_type == spitfire)
  446. spitfire_xcall_deliver(data0, data1, data2, mask);
  447. else
  448. cheetah_xcall_deliver(data0, data1, data2, mask);
  449. /* NOTE: Caller runs local copy on master. */
  450. put_cpu();
  451. }
  452. extern unsigned long xcall_sync_tick;
  453. static void smp_start_sync_tick_client(int cpu)
  454. {
  455. cpumask_t mask = cpumask_of_cpu(cpu);
  456. smp_cross_call_masked(&xcall_sync_tick,
  457. 0, 0, 0, mask);
  458. }
  459. /* Send cross call to all processors except self. */
  460. #define smp_cross_call(func, ctx, data1, data2) \
  461. smp_cross_call_masked(func, ctx, data1, data2, cpu_online_map)
  462. struct call_data_struct {
  463. void (*func) (void *info);
  464. void *info;
  465. atomic_t finished;
  466. int wait;
  467. };
  468. static DEFINE_SPINLOCK(call_lock);
  469. static struct call_data_struct *call_data;
  470. extern unsigned long xcall_call_function;
  471. /*
  472. * You must not call this function with disabled interrupts or from a
  473. * hardware interrupt handler or from a bottom half handler.
  474. */
  475. int smp_call_function(void (*func)(void *info), void *info,
  476. int nonatomic, int wait)
  477. {
  478. struct call_data_struct data;
  479. int cpus = num_online_cpus() - 1;
  480. long timeout;
  481. if (!cpus)
  482. return 0;
  483. /* Can deadlock when called with interrupts disabled */
  484. WARN_ON(irqs_disabled());
  485. data.func = func;
  486. data.info = info;
  487. atomic_set(&data.finished, 0);
  488. data.wait = wait;
  489. spin_lock(&call_lock);
  490. call_data = &data;
  491. smp_cross_call(&xcall_call_function, 0, 0, 0);
  492. /*
  493. * Wait for other cpus to complete function or at
  494. * least snap the call data.
  495. */
  496. timeout = 1000000;
  497. while (atomic_read(&data.finished) != cpus) {
  498. if (--timeout <= 0)
  499. goto out_timeout;
  500. barrier();
  501. udelay(1);
  502. }
  503. spin_unlock(&call_lock);
  504. return 0;
  505. out_timeout:
  506. spin_unlock(&call_lock);
  507. printk("XCALL: Remote cpus not responding, ncpus=%ld finished=%ld\n",
  508. (long) num_online_cpus() - 1L,
  509. (long) atomic_read(&data.finished));
  510. return 0;
  511. }
  512. void smp_call_function_client(int irq, struct pt_regs *regs)
  513. {
  514. void (*func) (void *info) = call_data->func;
  515. void *info = call_data->info;
  516. clear_softint(1 << irq);
  517. if (call_data->wait) {
  518. /* let initiator proceed only after completion */
  519. func(info);
  520. atomic_inc(&call_data->finished);
  521. } else {
  522. /* let initiator proceed after getting data */
  523. atomic_inc(&call_data->finished);
  524. func(info);
  525. }
  526. }
  527. extern unsigned long xcall_flush_tlb_mm;
  528. extern unsigned long xcall_flush_tlb_pending;
  529. extern unsigned long xcall_flush_tlb_kernel_range;
  530. extern unsigned long xcall_flush_tlb_all_spitfire;
  531. extern unsigned long xcall_flush_tlb_all_cheetah;
  532. extern unsigned long xcall_report_regs;
  533. extern unsigned long xcall_receive_signal;
  534. #ifdef DCACHE_ALIASING_POSSIBLE
  535. extern unsigned long xcall_flush_dcache_page_cheetah;
  536. #endif
  537. extern unsigned long xcall_flush_dcache_page_spitfire;
  538. #ifdef CONFIG_DEBUG_DCFLUSH
  539. extern atomic_t dcpage_flushes;
  540. extern atomic_t dcpage_flushes_xcall;
  541. #endif
  542. static __inline__ void __local_flush_dcache_page(struct page *page)
  543. {
  544. #ifdef DCACHE_ALIASING_POSSIBLE
  545. __flush_dcache_page(page_address(page),
  546. ((tlb_type == spitfire) &&
  547. page_mapping(page) != NULL));
  548. #else
  549. if (page_mapping(page) != NULL &&
  550. tlb_type == spitfire)
  551. __flush_icache_page(__pa(page_address(page)));
  552. #endif
  553. }
  554. void smp_flush_dcache_page_impl(struct page *page, int cpu)
  555. {
  556. cpumask_t mask = cpumask_of_cpu(cpu);
  557. int this_cpu = get_cpu();
  558. #ifdef CONFIG_DEBUG_DCFLUSH
  559. atomic_inc(&dcpage_flushes);
  560. #endif
  561. if (cpu == this_cpu) {
  562. __local_flush_dcache_page(page);
  563. } else if (cpu_online(cpu)) {
  564. void *pg_addr = page_address(page);
  565. u64 data0;
  566. if (tlb_type == spitfire) {
  567. data0 =
  568. ((u64)&xcall_flush_dcache_page_spitfire);
  569. if (page_mapping(page) != NULL)
  570. data0 |= ((u64)1 << 32);
  571. spitfire_xcall_deliver(data0,
  572. __pa(pg_addr),
  573. (u64) pg_addr,
  574. mask);
  575. } else {
  576. #ifdef DCACHE_ALIASING_POSSIBLE
  577. data0 =
  578. ((u64)&xcall_flush_dcache_page_cheetah);
  579. cheetah_xcall_deliver(data0,
  580. __pa(pg_addr),
  581. 0, mask);
  582. #endif
  583. }
  584. #ifdef CONFIG_DEBUG_DCFLUSH
  585. atomic_inc(&dcpage_flushes_xcall);
  586. #endif
  587. }
  588. put_cpu();
  589. }
  590. void flush_dcache_page_all(struct mm_struct *mm, struct page *page)
  591. {
  592. void *pg_addr = page_address(page);
  593. cpumask_t mask = cpu_online_map;
  594. u64 data0;
  595. int this_cpu = get_cpu();
  596. cpu_clear(this_cpu, mask);
  597. #ifdef CONFIG_DEBUG_DCFLUSH
  598. atomic_inc(&dcpage_flushes);
  599. #endif
  600. if (cpus_empty(mask))
  601. goto flush_self;
  602. if (tlb_type == spitfire) {
  603. data0 = ((u64)&xcall_flush_dcache_page_spitfire);
  604. if (page_mapping(page) != NULL)
  605. data0 |= ((u64)1 << 32);
  606. spitfire_xcall_deliver(data0,
  607. __pa(pg_addr),
  608. (u64) pg_addr,
  609. mask);
  610. } else {
  611. #ifdef DCACHE_ALIASING_POSSIBLE
  612. data0 = ((u64)&xcall_flush_dcache_page_cheetah);
  613. cheetah_xcall_deliver(data0,
  614. __pa(pg_addr),
  615. 0, mask);
  616. #endif
  617. }
  618. #ifdef CONFIG_DEBUG_DCFLUSH
  619. atomic_inc(&dcpage_flushes_xcall);
  620. #endif
  621. flush_self:
  622. __local_flush_dcache_page(page);
  623. put_cpu();
  624. }
  625. void smp_receive_signal(int cpu)
  626. {
  627. cpumask_t mask = cpumask_of_cpu(cpu);
  628. if (cpu_online(cpu)) {
  629. u64 data0 = (((u64)&xcall_receive_signal) & 0xffffffff);
  630. if (tlb_type == spitfire)
  631. spitfire_xcall_deliver(data0, 0, 0, mask);
  632. else
  633. cheetah_xcall_deliver(data0, 0, 0, mask);
  634. }
  635. }
  636. void smp_receive_signal_client(int irq, struct pt_regs *regs)
  637. {
  638. /* Just return, rtrap takes care of the rest. */
  639. clear_softint(1 << irq);
  640. }
  641. void smp_report_regs(void)
  642. {
  643. smp_cross_call(&xcall_report_regs, 0, 0, 0);
  644. }
  645. void smp_flush_tlb_all(void)
  646. {
  647. if (tlb_type == spitfire)
  648. smp_cross_call(&xcall_flush_tlb_all_spitfire, 0, 0, 0);
  649. else
  650. smp_cross_call(&xcall_flush_tlb_all_cheetah, 0, 0, 0);
  651. __flush_tlb_all();
  652. }
  653. /* We know that the window frames of the user have been flushed
  654. * to the stack before we get here because all callers of us
  655. * are flush_tlb_*() routines, and these run after flush_cache_*()
  656. * which performs the flushw.
  657. *
  658. * The SMP TLB coherency scheme we use works as follows:
  659. *
  660. * 1) mm->cpu_vm_mask is a bit mask of which cpus an address
  661. * space has (potentially) executed on, this is the heuristic
  662. * we use to avoid doing cross calls.
  663. *
  664. * Also, for flushing from kswapd and also for clones, we
  665. * use cpu_vm_mask as the list of cpus to make run the TLB.
  666. *
  667. * 2) TLB context numbers are shared globally across all processors
  668. * in the system, this allows us to play several games to avoid
  669. * cross calls.
  670. *
  671. * One invariant is that when a cpu switches to a process, and
  672. * that processes tsk->active_mm->cpu_vm_mask does not have the
  673. * current cpu's bit set, that tlb context is flushed locally.
  674. *
  675. * If the address space is non-shared (ie. mm->count == 1) we avoid
  676. * cross calls when we want to flush the currently running process's
  677. * tlb state. This is done by clearing all cpu bits except the current
  678. * processor's in current->active_mm->cpu_vm_mask and performing the
  679. * flush locally only. This will force any subsequent cpus which run
  680. * this task to flush the context from the local tlb if the process
  681. * migrates to another cpu (again).
  682. *
  683. * 3) For shared address spaces (threads) and swapping we bite the
  684. * bullet for most cases and perform the cross call (but only to
  685. * the cpus listed in cpu_vm_mask).
  686. *
  687. * The performance gain from "optimizing" away the cross call for threads is
  688. * questionable (in theory the big win for threads is the massive sharing of
  689. * address space state across processors).
  690. */
  691. void smp_flush_tlb_mm(struct mm_struct *mm)
  692. {
  693. /*
  694. * This code is called from two places, dup_mmap and exit_mmap. In the
  695. * former case, we really need a flush. In the later case, the callers
  696. * are single threaded exec_mmap (really need a flush), multithreaded
  697. * exec_mmap case (do not need to flush, since the caller gets a new
  698. * context via activate_mm), and all other callers of mmput() whence
  699. * the flush can be optimized since the associated threads are dead and
  700. * the mm is being torn down (__exit_mm and other mmput callers) or the
  701. * owning thread is dissociating itself from the mm. The
  702. * (atomic_read(&mm->mm_users) == 0) check ensures real work is done
  703. * for single thread exec and dup_mmap cases. An alternate check might
  704. * have been (current->mm != mm).
  705. * Kanoj Sarcar
  706. */
  707. if (atomic_read(&mm->mm_users) == 0)
  708. return;
  709. {
  710. u32 ctx = CTX_HWBITS(mm->context);
  711. int cpu = get_cpu();
  712. if (atomic_read(&mm->mm_users) == 1) {
  713. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  714. goto local_flush_and_out;
  715. }
  716. smp_cross_call_masked(&xcall_flush_tlb_mm,
  717. ctx, 0, 0,
  718. mm->cpu_vm_mask);
  719. local_flush_and_out:
  720. __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
  721. put_cpu();
  722. }
  723. }
  724. void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs)
  725. {
  726. u32 ctx = CTX_HWBITS(mm->context);
  727. int cpu = get_cpu();
  728. if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1) {
  729. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  730. goto local_flush_and_out;
  731. } else {
  732. /* This optimization is not valid. Normally
  733. * we will be holding the page_table_lock, but
  734. * there is an exception which is copy_page_range()
  735. * when forking. The lock is held during the individual
  736. * page table updates in the parent, but not at the
  737. * top level, which is where we are invoked.
  738. */
  739. if (0) {
  740. cpumask_t this_cpu_mask = cpumask_of_cpu(cpu);
  741. /* By virtue of running under the mm->page_table_lock,
  742. * and mmu_context.h:switch_mm doing the same, the
  743. * following operation is safe.
  744. */
  745. if (cpus_equal(mm->cpu_vm_mask, this_cpu_mask))
  746. goto local_flush_and_out;
  747. }
  748. }
  749. smp_cross_call_masked(&xcall_flush_tlb_pending,
  750. ctx, nr, (unsigned long) vaddrs,
  751. mm->cpu_vm_mask);
  752. local_flush_and_out:
  753. __flush_tlb_pending(ctx, nr, vaddrs);
  754. put_cpu();
  755. }
  756. void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  757. {
  758. start &= PAGE_MASK;
  759. end = PAGE_ALIGN(end);
  760. if (start != end) {
  761. smp_cross_call(&xcall_flush_tlb_kernel_range,
  762. 0, start, end);
  763. __flush_tlb_kernel_range(start, end);
  764. }
  765. }
  766. /* CPU capture. */
  767. /* #define CAPTURE_DEBUG */
  768. extern unsigned long xcall_capture;
  769. static atomic_t smp_capture_depth = ATOMIC_INIT(0);
  770. static atomic_t smp_capture_registry = ATOMIC_INIT(0);
  771. static unsigned long penguins_are_doing_time;
  772. void smp_capture(void)
  773. {
  774. int result = atomic_add_ret(1, &smp_capture_depth);
  775. if (result == 1) {
  776. int ncpus = num_online_cpus();
  777. #ifdef CAPTURE_DEBUG
  778. printk("CPU[%d]: Sending penguins to jail...",
  779. smp_processor_id());
  780. #endif
  781. penguins_are_doing_time = 1;
  782. membar_storestore_loadstore();
  783. atomic_inc(&smp_capture_registry);
  784. smp_cross_call(&xcall_capture, 0, 0, 0);
  785. while (atomic_read(&smp_capture_registry) != ncpus)
  786. rmb();
  787. #ifdef CAPTURE_DEBUG
  788. printk("done\n");
  789. #endif
  790. }
  791. }
  792. void smp_release(void)
  793. {
  794. if (atomic_dec_and_test(&smp_capture_depth)) {
  795. #ifdef CAPTURE_DEBUG
  796. printk("CPU[%d]: Giving pardon to "
  797. "imprisoned penguins\n",
  798. smp_processor_id());
  799. #endif
  800. penguins_are_doing_time = 0;
  801. membar_storeload_storestore();
  802. atomic_dec(&smp_capture_registry);
  803. }
  804. }
  805. /* Imprisoned penguins run with %pil == 15, but PSTATE_IE set, so they
  806. * can service tlb flush xcalls...
  807. */
  808. extern void prom_world(int);
  809. extern void save_alternate_globals(unsigned long *);
  810. extern void restore_alternate_globals(unsigned long *);
  811. void smp_penguin_jailcell(int irq, struct pt_regs *regs)
  812. {
  813. unsigned long global_save[24];
  814. clear_softint(1 << irq);
  815. preempt_disable();
  816. __asm__ __volatile__("flushw");
  817. save_alternate_globals(global_save);
  818. prom_world(1);
  819. atomic_inc(&smp_capture_registry);
  820. membar_storeload_storestore();
  821. while (penguins_are_doing_time)
  822. rmb();
  823. restore_alternate_globals(global_save);
  824. atomic_dec(&smp_capture_registry);
  825. prom_world(0);
  826. preempt_enable();
  827. }
  828. extern unsigned long xcall_promstop;
  829. void smp_promstop_others(void)
  830. {
  831. smp_cross_call(&xcall_promstop, 0, 0, 0);
  832. }
  833. #define prof_multiplier(__cpu) cpu_data(__cpu).multiplier
  834. #define prof_counter(__cpu) cpu_data(__cpu).counter
  835. void smp_percpu_timer_interrupt(struct pt_regs *regs)
  836. {
  837. unsigned long compare, tick, pstate;
  838. int cpu = smp_processor_id();
  839. int user = user_mode(regs);
  840. /*
  841. * Check for level 14 softint.
  842. */
  843. {
  844. unsigned long tick_mask = tick_ops->softint_mask;
  845. if (!(get_softint() & tick_mask)) {
  846. extern void handler_irq(int, struct pt_regs *);
  847. handler_irq(14, regs);
  848. return;
  849. }
  850. clear_softint(tick_mask);
  851. }
  852. do {
  853. profile_tick(CPU_PROFILING, regs);
  854. if (!--prof_counter(cpu)) {
  855. irq_enter();
  856. if (cpu == boot_cpu_id) {
  857. kstat_this_cpu.irqs[0]++;
  858. timer_tick_interrupt(regs);
  859. }
  860. update_process_times(user);
  861. irq_exit();
  862. prof_counter(cpu) = prof_multiplier(cpu);
  863. }
  864. /* Guarantee that the following sequences execute
  865. * uninterrupted.
  866. */
  867. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  868. "wrpr %0, %1, %%pstate"
  869. : "=r" (pstate)
  870. : "i" (PSTATE_IE));
  871. compare = tick_ops->add_compare(current_tick_offset);
  872. tick = tick_ops->get_tick();
  873. /* Restore PSTATE_IE. */
  874. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  875. : /* no outputs */
  876. : "r" (pstate));
  877. } while (time_after_eq(tick, compare));
  878. }
  879. static void __init smp_setup_percpu_timer(void)
  880. {
  881. int cpu = smp_processor_id();
  882. unsigned long pstate;
  883. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  884. /* Guarantee that the following sequences execute
  885. * uninterrupted.
  886. */
  887. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  888. "wrpr %0, %1, %%pstate"
  889. : "=r" (pstate)
  890. : "i" (PSTATE_IE));
  891. tick_ops->init_tick(current_tick_offset);
  892. /* Restore PSTATE_IE. */
  893. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  894. : /* no outputs */
  895. : "r" (pstate));
  896. }
  897. void __init smp_tick_init(void)
  898. {
  899. boot_cpu_id = hard_smp_processor_id();
  900. current_tick_offset = timer_tick_offset;
  901. cpu_set(boot_cpu_id, cpu_online_map);
  902. prof_counter(boot_cpu_id) = prof_multiplier(boot_cpu_id) = 1;
  903. }
  904. /* /proc/profile writes can call this, don't __init it please. */
  905. static DEFINE_SPINLOCK(prof_setup_lock);
  906. int setup_profiling_timer(unsigned int multiplier)
  907. {
  908. unsigned long flags;
  909. int i;
  910. if ((!multiplier) || (timer_tick_offset / multiplier) < 1000)
  911. return -EINVAL;
  912. spin_lock_irqsave(&prof_setup_lock, flags);
  913. for (i = 0; i < NR_CPUS; i++)
  914. prof_multiplier(i) = multiplier;
  915. current_tick_offset = (timer_tick_offset / multiplier);
  916. spin_unlock_irqrestore(&prof_setup_lock, flags);
  917. return 0;
  918. }
  919. void __init smp_prepare_cpus(unsigned int max_cpus)
  920. {
  921. int instance, mid;
  922. instance = 0;
  923. while (!cpu_find_by_instance(instance, NULL, &mid)) {
  924. if (mid < max_cpus)
  925. cpu_set(mid, phys_cpu_present_map);
  926. instance++;
  927. }
  928. if (num_possible_cpus() > max_cpus) {
  929. instance = 0;
  930. while (!cpu_find_by_instance(instance, NULL, &mid)) {
  931. if (mid != boot_cpu_id) {
  932. cpu_clear(mid, phys_cpu_present_map);
  933. if (num_possible_cpus() <= max_cpus)
  934. break;
  935. }
  936. instance++;
  937. }
  938. }
  939. smp_store_cpu_info(boot_cpu_id);
  940. }
  941. void __devinit smp_prepare_boot_cpu(void)
  942. {
  943. if (hard_smp_processor_id() >= NR_CPUS) {
  944. prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
  945. prom_halt();
  946. }
  947. current_thread_info()->cpu = hard_smp_processor_id();
  948. cpu_set(smp_processor_id(), cpu_online_map);
  949. cpu_set(smp_processor_id(), phys_cpu_present_map);
  950. }
  951. int __devinit __cpu_up(unsigned int cpu)
  952. {
  953. int ret = smp_boot_one_cpu(cpu);
  954. if (!ret) {
  955. cpu_set(cpu, smp_commenced_mask);
  956. while (!cpu_isset(cpu, cpu_online_map))
  957. mb();
  958. if (!cpu_isset(cpu, cpu_online_map)) {
  959. ret = -ENODEV;
  960. } else {
  961. smp_synchronize_one_tick(cpu);
  962. }
  963. }
  964. return ret;
  965. }
  966. void __init smp_cpus_done(unsigned int max_cpus)
  967. {
  968. unsigned long bogosum = 0;
  969. int i;
  970. for (i = 0; i < NR_CPUS; i++) {
  971. if (cpu_online(i))
  972. bogosum += cpu_data(i).udelay_val;
  973. }
  974. printk("Total of %ld processors activated "
  975. "(%lu.%02lu BogoMIPS).\n",
  976. (long) num_online_cpus(),
  977. bogosum/(500000/HZ),
  978. (bogosum/(5000/HZ))%100);
  979. }
  980. /* This needn't do anything as we do not sleep the cpu
  981. * inside of the idler task, so an interrupt is not needed
  982. * to get a clean fast response.
  983. *
  984. * XXX Reverify this assumption... -DaveM
  985. *
  986. * Addendum: We do want it to do something for the signal
  987. * delivery case, we detect that by just seeing
  988. * if we are trying to send this to an idler or not.
  989. */
  990. void smp_send_reschedule(int cpu)
  991. {
  992. if (cpu_data(cpu).idle_volume == 0)
  993. smp_receive_signal(cpu);
  994. }
  995. /* This is a nop because we capture all other cpus
  996. * anyways when making the PROM active.
  997. */
  998. void smp_send_stop(void)
  999. {
  1000. }
  1001. unsigned long __per_cpu_base __read_mostly;
  1002. unsigned long __per_cpu_shift __read_mostly;
  1003. EXPORT_SYMBOL(__per_cpu_base);
  1004. EXPORT_SYMBOL(__per_cpu_shift);
  1005. void __init setup_per_cpu_areas(void)
  1006. {
  1007. unsigned long goal, size, i;
  1008. char *ptr;
  1009. /* Created by linker magic */
  1010. extern char __per_cpu_start[], __per_cpu_end[];
  1011. /* Copy section for each CPU (we discard the original) */
  1012. goal = ALIGN(__per_cpu_end - __per_cpu_start, PAGE_SIZE);
  1013. #ifdef CONFIG_MODULES
  1014. if (goal < PERCPU_ENOUGH_ROOM)
  1015. goal = PERCPU_ENOUGH_ROOM;
  1016. #endif
  1017. __per_cpu_shift = 0;
  1018. for (size = 1UL; size < goal; size <<= 1UL)
  1019. __per_cpu_shift++;
  1020. /* Make sure the resulting __per_cpu_base value
  1021. * will fit in the 43-bit sign extended IMMU
  1022. * TSB register.
  1023. */
  1024. ptr = __alloc_bootmem(size * NR_CPUS, PAGE_SIZE,
  1025. (unsigned long) __per_cpu_start);
  1026. __per_cpu_base = ptr - __per_cpu_start;
  1027. if ((__per_cpu_shift < PAGE_SHIFT) ||
  1028. (__per_cpu_base & ~PAGE_MASK) ||
  1029. (__per_cpu_base != (((long) __per_cpu_base << 20) >> 20))) {
  1030. prom_printf("PER_CPU: Invalid layout, "
  1031. "ptr[%p] shift[%lx] base[%lx]\n",
  1032. ptr, __per_cpu_shift, __per_cpu_base);
  1033. prom_halt();
  1034. }
  1035. for (i = 0; i < NR_CPUS; i++, ptr += size)
  1036. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  1037. /* Finally, load in the boot cpu's base value.
  1038. * We abuse the IMMU TSB register for trap handler
  1039. * entry and exit loading of %g5. That is why it
  1040. * has to be page aligned.
  1041. */
  1042. cpu_setup_percpu_base(hard_smp_processor_id());
  1043. }