smp.c 29 KB

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