smp.c 35 KB

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