smp.c 31 KB

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