smp.c 31 KB

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