smp.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226
  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. /* This currently is only used by the hugetlb arch pre-fault
  712. * hook on UltraSPARC-III+ and later when changing the pagesize
  713. * bits of the context register for an address space.
  714. */
  715. void smp_flush_tlb_mm(struct mm_struct *mm)
  716. {
  717. u32 ctx = CTX_HWBITS(mm->context);
  718. int cpu = get_cpu();
  719. if (atomic_read(&mm->mm_users) == 1) {
  720. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  721. goto local_flush_and_out;
  722. }
  723. smp_cross_call_masked(&xcall_flush_tlb_mm,
  724. ctx, 0, 0,
  725. mm->cpu_vm_mask);
  726. local_flush_and_out:
  727. __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
  728. put_cpu();
  729. }
  730. void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs)
  731. {
  732. u32 ctx = CTX_HWBITS(mm->context);
  733. int cpu = get_cpu();
  734. if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1)
  735. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  736. else
  737. smp_cross_call_masked(&xcall_flush_tlb_pending,
  738. ctx, nr, (unsigned long) vaddrs,
  739. mm->cpu_vm_mask);
  740. __flush_tlb_pending(ctx, nr, vaddrs);
  741. put_cpu();
  742. }
  743. void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  744. {
  745. start &= PAGE_MASK;
  746. end = PAGE_ALIGN(end);
  747. if (start != end) {
  748. smp_cross_call(&xcall_flush_tlb_kernel_range,
  749. 0, start, end);
  750. __flush_tlb_kernel_range(start, end);
  751. }
  752. }
  753. /* CPU capture. */
  754. /* #define CAPTURE_DEBUG */
  755. extern unsigned long xcall_capture;
  756. static atomic_t smp_capture_depth = ATOMIC_INIT(0);
  757. static atomic_t smp_capture_registry = ATOMIC_INIT(0);
  758. static unsigned long penguins_are_doing_time;
  759. void smp_capture(void)
  760. {
  761. int result = atomic_add_ret(1, &smp_capture_depth);
  762. if (result == 1) {
  763. int ncpus = num_online_cpus();
  764. #ifdef CAPTURE_DEBUG
  765. printk("CPU[%d]: Sending penguins to jail...",
  766. smp_processor_id());
  767. #endif
  768. penguins_are_doing_time = 1;
  769. membar_storestore_loadstore();
  770. atomic_inc(&smp_capture_registry);
  771. smp_cross_call(&xcall_capture, 0, 0, 0);
  772. while (atomic_read(&smp_capture_registry) != ncpus)
  773. rmb();
  774. #ifdef CAPTURE_DEBUG
  775. printk("done\n");
  776. #endif
  777. }
  778. }
  779. void smp_release(void)
  780. {
  781. if (atomic_dec_and_test(&smp_capture_depth)) {
  782. #ifdef CAPTURE_DEBUG
  783. printk("CPU[%d]: Giving pardon to "
  784. "imprisoned penguins\n",
  785. smp_processor_id());
  786. #endif
  787. penguins_are_doing_time = 0;
  788. membar_storeload_storestore();
  789. atomic_dec(&smp_capture_registry);
  790. }
  791. }
  792. /* Imprisoned penguins run with %pil == 15, but PSTATE_IE set, so they
  793. * can service tlb flush xcalls...
  794. */
  795. extern void prom_world(int);
  796. extern void save_alternate_globals(unsigned long *);
  797. extern void restore_alternate_globals(unsigned long *);
  798. void smp_penguin_jailcell(int irq, struct pt_regs *regs)
  799. {
  800. unsigned long global_save[24];
  801. clear_softint(1 << irq);
  802. preempt_disable();
  803. __asm__ __volatile__("flushw");
  804. save_alternate_globals(global_save);
  805. prom_world(1);
  806. atomic_inc(&smp_capture_registry);
  807. membar_storeload_storestore();
  808. while (penguins_are_doing_time)
  809. rmb();
  810. restore_alternate_globals(global_save);
  811. atomic_dec(&smp_capture_registry);
  812. prom_world(0);
  813. preempt_enable();
  814. }
  815. #define prof_multiplier(__cpu) cpu_data(__cpu).multiplier
  816. #define prof_counter(__cpu) cpu_data(__cpu).counter
  817. void smp_percpu_timer_interrupt(struct pt_regs *regs)
  818. {
  819. unsigned long compare, tick, pstate;
  820. int cpu = smp_processor_id();
  821. int user = user_mode(regs);
  822. /*
  823. * Check for level 14 softint.
  824. */
  825. {
  826. unsigned long tick_mask = tick_ops->softint_mask;
  827. if (!(get_softint() & tick_mask)) {
  828. extern void handler_irq(int, struct pt_regs *);
  829. handler_irq(14, regs);
  830. return;
  831. }
  832. clear_softint(tick_mask);
  833. }
  834. do {
  835. profile_tick(CPU_PROFILING, regs);
  836. if (!--prof_counter(cpu)) {
  837. irq_enter();
  838. if (cpu == boot_cpu_id) {
  839. kstat_this_cpu.irqs[0]++;
  840. timer_tick_interrupt(regs);
  841. }
  842. update_process_times(user);
  843. irq_exit();
  844. prof_counter(cpu) = prof_multiplier(cpu);
  845. }
  846. /* Guarantee that the following sequences execute
  847. * uninterrupted.
  848. */
  849. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  850. "wrpr %0, %1, %%pstate"
  851. : "=r" (pstate)
  852. : "i" (PSTATE_IE));
  853. compare = tick_ops->add_compare(current_tick_offset);
  854. tick = tick_ops->get_tick();
  855. /* Restore PSTATE_IE. */
  856. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  857. : /* no outputs */
  858. : "r" (pstate));
  859. } while (time_after_eq(tick, compare));
  860. }
  861. static void __init smp_setup_percpu_timer(void)
  862. {
  863. int cpu = smp_processor_id();
  864. unsigned long pstate;
  865. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  866. /* Guarantee that the following sequences execute
  867. * uninterrupted.
  868. */
  869. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  870. "wrpr %0, %1, %%pstate"
  871. : "=r" (pstate)
  872. : "i" (PSTATE_IE));
  873. tick_ops->init_tick(current_tick_offset);
  874. /* Restore PSTATE_IE. */
  875. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  876. : /* no outputs */
  877. : "r" (pstate));
  878. }
  879. void __init smp_tick_init(void)
  880. {
  881. boot_cpu_id = hard_smp_processor_id();
  882. current_tick_offset = timer_tick_offset;
  883. cpu_set(boot_cpu_id, cpu_online_map);
  884. prof_counter(boot_cpu_id) = prof_multiplier(boot_cpu_id) = 1;
  885. }
  886. /* /proc/profile writes can call this, don't __init it please. */
  887. static DEFINE_SPINLOCK(prof_setup_lock);
  888. int setup_profiling_timer(unsigned int multiplier)
  889. {
  890. unsigned long flags;
  891. int i;
  892. if ((!multiplier) || (timer_tick_offset / multiplier) < 1000)
  893. return -EINVAL;
  894. spin_lock_irqsave(&prof_setup_lock, flags);
  895. for (i = 0; i < NR_CPUS; i++)
  896. prof_multiplier(i) = multiplier;
  897. current_tick_offset = (timer_tick_offset / multiplier);
  898. spin_unlock_irqrestore(&prof_setup_lock, flags);
  899. return 0;
  900. }
  901. void __init smp_prepare_cpus(unsigned int max_cpus)
  902. {
  903. int instance, mid;
  904. instance = 0;
  905. while (!cpu_find_by_instance(instance, NULL, &mid)) {
  906. if (mid < max_cpus)
  907. cpu_set(mid, phys_cpu_present_map);
  908. instance++;
  909. }
  910. if (num_possible_cpus() > max_cpus) {
  911. instance = 0;
  912. while (!cpu_find_by_instance(instance, NULL, &mid)) {
  913. if (mid != boot_cpu_id) {
  914. cpu_clear(mid, phys_cpu_present_map);
  915. if (num_possible_cpus() <= max_cpus)
  916. break;
  917. }
  918. instance++;
  919. }
  920. }
  921. smp_store_cpu_info(boot_cpu_id);
  922. }
  923. void __devinit smp_prepare_boot_cpu(void)
  924. {
  925. if (hard_smp_processor_id() >= NR_CPUS) {
  926. prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
  927. prom_halt();
  928. }
  929. current_thread_info()->cpu = hard_smp_processor_id();
  930. cpu_set(smp_processor_id(), cpu_online_map);
  931. cpu_set(smp_processor_id(), phys_cpu_present_map);
  932. }
  933. int __devinit __cpu_up(unsigned int cpu)
  934. {
  935. int ret = smp_boot_one_cpu(cpu);
  936. if (!ret) {
  937. cpu_set(cpu, smp_commenced_mask);
  938. while (!cpu_isset(cpu, cpu_online_map))
  939. mb();
  940. if (!cpu_isset(cpu, cpu_online_map)) {
  941. ret = -ENODEV;
  942. } else {
  943. smp_synchronize_one_tick(cpu);
  944. }
  945. }
  946. return ret;
  947. }
  948. void __init smp_cpus_done(unsigned int max_cpus)
  949. {
  950. unsigned long bogosum = 0;
  951. int i;
  952. for (i = 0; i < NR_CPUS; i++) {
  953. if (cpu_online(i))
  954. bogosum += cpu_data(i).udelay_val;
  955. }
  956. printk("Total of %ld processors activated "
  957. "(%lu.%02lu BogoMIPS).\n",
  958. (long) num_online_cpus(),
  959. bogosum/(500000/HZ),
  960. (bogosum/(5000/HZ))%100);
  961. }
  962. /* This needn't do anything as we do not sleep the cpu
  963. * inside of the idler task, so an interrupt is not needed
  964. * to get a clean fast response.
  965. *
  966. * XXX Reverify this assumption... -DaveM
  967. *
  968. * Addendum: We do want it to do something for the signal
  969. * delivery case, we detect that by just seeing
  970. * if we are trying to send this to an idler or not.
  971. */
  972. void smp_send_reschedule(int cpu)
  973. {
  974. if (cpu_data(cpu).idle_volume == 0)
  975. smp_receive_signal(cpu);
  976. }
  977. /* This is a nop because we capture all other cpus
  978. * anyways when making the PROM active.
  979. */
  980. void smp_send_stop(void)
  981. {
  982. }
  983. unsigned long __per_cpu_base __read_mostly;
  984. unsigned long __per_cpu_shift __read_mostly;
  985. EXPORT_SYMBOL(__per_cpu_base);
  986. EXPORT_SYMBOL(__per_cpu_shift);
  987. void __init setup_per_cpu_areas(void)
  988. {
  989. unsigned long goal, size, i;
  990. char *ptr;
  991. /* Created by linker magic */
  992. extern char __per_cpu_start[], __per_cpu_end[];
  993. /* Copy section for each CPU (we discard the original) */
  994. goal = ALIGN(__per_cpu_end - __per_cpu_start, PAGE_SIZE);
  995. #ifdef CONFIG_MODULES
  996. if (goal < PERCPU_ENOUGH_ROOM)
  997. goal = PERCPU_ENOUGH_ROOM;
  998. #endif
  999. __per_cpu_shift = 0;
  1000. for (size = 1UL; size < goal; size <<= 1UL)
  1001. __per_cpu_shift++;
  1002. /* Make sure the resulting __per_cpu_base value
  1003. * will fit in the 43-bit sign extended IMMU
  1004. * TSB register.
  1005. */
  1006. ptr = __alloc_bootmem(size * NR_CPUS, PAGE_SIZE,
  1007. (unsigned long) __per_cpu_start);
  1008. __per_cpu_base = ptr - __per_cpu_start;
  1009. if ((__per_cpu_shift < PAGE_SHIFT) ||
  1010. (__per_cpu_base & ~PAGE_MASK) ||
  1011. (__per_cpu_base != (((long) __per_cpu_base << 20) >> 20))) {
  1012. prom_printf("PER_CPU: Invalid layout, "
  1013. "ptr[%p] shift[%lx] base[%lx]\n",
  1014. ptr, __per_cpu_shift, __per_cpu_base);
  1015. prom_halt();
  1016. }
  1017. for (i = 0; i < NR_CPUS; i++, ptr += size)
  1018. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  1019. /* Finally, load in the boot cpu's base value.
  1020. * We abuse the IMMU TSB register for trap handler
  1021. * entry and exit loading of %g5. That is why it
  1022. * has to be page aligned.
  1023. */
  1024. cpu_setup_percpu_base(hard_smp_processor_id());
  1025. }