smp.c 30 KB

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