smp.c 29 KB

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