smp.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  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/irq_regs.h>
  32. #include <asm/page.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/oplib.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/timer.h>
  37. #include <asm/starfire.h>
  38. #include <asm/tlb.h>
  39. #include <asm/sections.h>
  40. #include <asm/prom.h>
  41. extern void calibrate_delay(void);
  42. /* Please don't make this stuff initdata!!! --DaveM */
  43. static unsigned char boot_cpu_id;
  44. cpumask_t cpu_online_map __read_mostly = CPU_MASK_NONE;
  45. cpumask_t phys_cpu_present_map __read_mostly = CPU_MASK_NONE;
  46. cpumask_t cpu_sibling_map[NR_CPUS] __read_mostly =
  47. { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
  48. static cpumask_t smp_commenced_mask;
  49. static cpumask_t cpu_callout_map;
  50. void smp_info(struct seq_file *m)
  51. {
  52. int i;
  53. seq_printf(m, "State:\n");
  54. for_each_online_cpu(i)
  55. seq_printf(m, "CPU%d:\t\tonline\n", i);
  56. }
  57. void smp_bogo(struct seq_file *m)
  58. {
  59. int i;
  60. for_each_online_cpu(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. struct device_node *dp;
  71. int def;
  72. /* multiplier and counter set by
  73. smp_setup_percpu_timer() */
  74. cpu_data(id).udelay_val = loops_per_jiffy;
  75. cpu_find_by_mid(id, &dp);
  76. cpu_data(id).clock_tick =
  77. of_getintprop_default(dp, "clock-frequency", 0);
  78. def = ((tlb_type == hypervisor) ? (8 * 1024) : (16 * 1024));
  79. cpu_data(id).dcache_size =
  80. of_getintprop_default(dp, "dcache-size", def);
  81. def = 32;
  82. cpu_data(id).dcache_line_size =
  83. of_getintprop_default(dp, "dcache-line-size", def);
  84. def = 16 * 1024;
  85. cpu_data(id).icache_size =
  86. of_getintprop_default(dp, "icache-size", def);
  87. def = 32;
  88. cpu_data(id).icache_line_size =
  89. of_getintprop_default(dp, "icache-line-size", def);
  90. def = ((tlb_type == hypervisor) ?
  91. (3 * 1024 * 1024) :
  92. (4 * 1024 * 1024));
  93. cpu_data(id).ecache_size =
  94. of_getintprop_default(dp, "ecache-size", def);
  95. def = 64;
  96. cpu_data(id).ecache_line_size =
  97. of_getintprop_default(dp, "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. struct device_node *dp;
  280. cpu_find_by_mid(cpu, &dp);
  281. prom_startcpu(dp->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 void hypervisor_xcall_deliver(u64 data0, u64 data1, u64 data2, cpumask_t mask)
  464. {
  465. struct trap_per_cpu *tb;
  466. u16 *cpu_list;
  467. u64 *mondo;
  468. cpumask_t error_mask;
  469. unsigned long flags, status;
  470. int cnt, retries, this_cpu, prev_sent, i;
  471. /* We have to do this whole thing with interrupts fully disabled.
  472. * Otherwise if we send an xcall from interrupt context it will
  473. * corrupt both our mondo block and cpu list state.
  474. *
  475. * One consequence of this is that we cannot use timeout mechanisms
  476. * that depend upon interrupts being delivered locally. So, for
  477. * example, we cannot sample jiffies and expect it to advance.
  478. *
  479. * Fortunately, udelay() uses %stick/%tick so we can use that.
  480. */
  481. local_irq_save(flags);
  482. this_cpu = smp_processor_id();
  483. tb = &trap_block[this_cpu];
  484. mondo = __va(tb->cpu_mondo_block_pa);
  485. mondo[0] = data0;
  486. mondo[1] = data1;
  487. mondo[2] = data2;
  488. wmb();
  489. cpu_list = __va(tb->cpu_list_pa);
  490. /* Setup the initial cpu list. */
  491. cnt = 0;
  492. for_each_cpu_mask(i, mask)
  493. cpu_list[cnt++] = i;
  494. cpus_clear(error_mask);
  495. retries = 0;
  496. prev_sent = 0;
  497. do {
  498. int forward_progress, n_sent;
  499. status = sun4v_cpu_mondo_send(cnt,
  500. tb->cpu_list_pa,
  501. tb->cpu_mondo_block_pa);
  502. /* HV_EOK means all cpus received the xcall, we're done. */
  503. if (likely(status == HV_EOK))
  504. break;
  505. /* First, see if we made any forward progress.
  506. *
  507. * The hypervisor indicates successful sends by setting
  508. * cpu list entries to the value 0xffff.
  509. */
  510. n_sent = 0;
  511. for (i = 0; i < cnt; i++) {
  512. if (likely(cpu_list[i] == 0xffff))
  513. n_sent++;
  514. }
  515. forward_progress = 0;
  516. if (n_sent > prev_sent)
  517. forward_progress = 1;
  518. prev_sent = n_sent;
  519. /* If we get a HV_ECPUERROR, then one or more of the cpus
  520. * in the list are in error state. Use the cpu_state()
  521. * hypervisor call to find out which cpus are in error state.
  522. */
  523. if (unlikely(status == HV_ECPUERROR)) {
  524. for (i = 0; i < cnt; i++) {
  525. long err;
  526. u16 cpu;
  527. cpu = cpu_list[i];
  528. if (cpu == 0xffff)
  529. continue;
  530. err = sun4v_cpu_state(cpu);
  531. if (err >= 0 &&
  532. err == HV_CPU_STATE_ERROR) {
  533. cpu_list[i] = 0xffff;
  534. cpu_set(cpu, error_mask);
  535. }
  536. }
  537. } else if (unlikely(status != HV_EWOULDBLOCK))
  538. goto fatal_mondo_error;
  539. /* Don't bother rewriting the CPU list, just leave the
  540. * 0xffff and non-0xffff entries in there and the
  541. * hypervisor will do the right thing.
  542. *
  543. * Only advance timeout state if we didn't make any
  544. * forward progress.
  545. */
  546. if (unlikely(!forward_progress)) {
  547. if (unlikely(++retries > 10000))
  548. goto fatal_mondo_timeout;
  549. /* Delay a little bit to let other cpus catch up
  550. * on their cpu mondo queue work.
  551. */
  552. udelay(2 * cnt);
  553. }
  554. } while (1);
  555. local_irq_restore(flags);
  556. if (unlikely(!cpus_empty(error_mask)))
  557. goto fatal_mondo_cpu_error;
  558. return;
  559. fatal_mondo_cpu_error:
  560. printk(KERN_CRIT "CPU[%d]: SUN4V mondo cpu error, some target cpus "
  561. "were in error state\n",
  562. this_cpu);
  563. printk(KERN_CRIT "CPU[%d]: Error mask [ ", this_cpu);
  564. for_each_cpu_mask(i, error_mask)
  565. printk("%d ", i);
  566. printk("]\n");
  567. return;
  568. fatal_mondo_timeout:
  569. local_irq_restore(flags);
  570. printk(KERN_CRIT "CPU[%d]: SUN4V mondo timeout, no forward "
  571. " progress after %d retries.\n",
  572. this_cpu, retries);
  573. goto dump_cpu_list_and_out;
  574. fatal_mondo_error:
  575. local_irq_restore(flags);
  576. printk(KERN_CRIT "CPU[%d]: Unexpected SUN4V mondo error %lu\n",
  577. this_cpu, status);
  578. printk(KERN_CRIT "CPU[%d]: Args were cnt(%d) cpulist_pa(%lx) "
  579. "mondo_block_pa(%lx)\n",
  580. this_cpu, cnt, tb->cpu_list_pa, tb->cpu_mondo_block_pa);
  581. dump_cpu_list_and_out:
  582. printk(KERN_CRIT "CPU[%d]: CPU list [ ", this_cpu);
  583. for (i = 0; i < cnt; i++)
  584. printk("%u ", cpu_list[i]);
  585. printk("]\n");
  586. }
  587. /* Send cross call to all processors mentioned in MASK
  588. * except self.
  589. */
  590. static void smp_cross_call_masked(unsigned long *func, u32 ctx, u64 data1, u64 data2, cpumask_t mask)
  591. {
  592. u64 data0 = (((u64)ctx)<<32 | (((u64)func) & 0xffffffff));
  593. int this_cpu = get_cpu();
  594. cpus_and(mask, mask, cpu_online_map);
  595. cpu_clear(this_cpu, mask);
  596. if (tlb_type == spitfire)
  597. spitfire_xcall_deliver(data0, data1, data2, mask);
  598. else if (tlb_type == cheetah || tlb_type == cheetah_plus)
  599. cheetah_xcall_deliver(data0, data1, data2, mask);
  600. else
  601. hypervisor_xcall_deliver(data0, data1, data2, mask);
  602. /* NOTE: Caller runs local copy on master. */
  603. put_cpu();
  604. }
  605. extern unsigned long xcall_sync_tick;
  606. static void smp_start_sync_tick_client(int cpu)
  607. {
  608. cpumask_t mask = cpumask_of_cpu(cpu);
  609. smp_cross_call_masked(&xcall_sync_tick,
  610. 0, 0, 0, mask);
  611. }
  612. /* Send cross call to all processors except self. */
  613. #define smp_cross_call(func, ctx, data1, data2) \
  614. smp_cross_call_masked(func, ctx, data1, data2, cpu_online_map)
  615. struct call_data_struct {
  616. void (*func) (void *info);
  617. void *info;
  618. atomic_t finished;
  619. int wait;
  620. };
  621. static __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
  622. static struct call_data_struct *call_data;
  623. extern unsigned long xcall_call_function;
  624. /**
  625. * smp_call_function(): Run a function on all other CPUs.
  626. * @func: The function to run. This must be fast and non-blocking.
  627. * @info: An arbitrary pointer to pass to the function.
  628. * @nonatomic: currently unused.
  629. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  630. *
  631. * Returns 0 on success, else a negative status code. Does not return until
  632. * remote CPUs are nearly ready to execute <<func>> or are or have executed.
  633. *
  634. * You must not call this function with disabled interrupts or from a
  635. * hardware interrupt handler or from a bottom half handler.
  636. */
  637. static int smp_call_function_mask(void (*func)(void *info), void *info,
  638. int nonatomic, int wait, cpumask_t mask)
  639. {
  640. struct call_data_struct data;
  641. int cpus;
  642. /* Can deadlock when called with interrupts disabled */
  643. WARN_ON(irqs_disabled());
  644. data.func = func;
  645. data.info = info;
  646. atomic_set(&data.finished, 0);
  647. data.wait = wait;
  648. spin_lock(&call_lock);
  649. cpu_clear(smp_processor_id(), mask);
  650. cpus = cpus_weight(mask);
  651. if (!cpus)
  652. goto out_unlock;
  653. call_data = &data;
  654. mb();
  655. smp_cross_call_masked(&xcall_call_function, 0, 0, 0, mask);
  656. /* Wait for response */
  657. while (atomic_read(&data.finished) != cpus)
  658. cpu_relax();
  659. out_unlock:
  660. spin_unlock(&call_lock);
  661. return 0;
  662. }
  663. int smp_call_function(void (*func)(void *info), void *info,
  664. int nonatomic, int wait)
  665. {
  666. return smp_call_function_mask(func, info, nonatomic, wait,
  667. cpu_online_map);
  668. }
  669. void smp_call_function_client(int irq, struct pt_regs *regs)
  670. {
  671. void (*func) (void *info) = call_data->func;
  672. void *info = call_data->info;
  673. clear_softint(1 << irq);
  674. if (call_data->wait) {
  675. /* let initiator proceed only after completion */
  676. func(info);
  677. atomic_inc(&call_data->finished);
  678. } else {
  679. /* let initiator proceed after getting data */
  680. atomic_inc(&call_data->finished);
  681. func(info);
  682. }
  683. }
  684. static void tsb_sync(void *info)
  685. {
  686. struct trap_per_cpu *tp = &trap_block[raw_smp_processor_id()];
  687. struct mm_struct *mm = info;
  688. /* It is not valid to test "currrent->active_mm == mm" here.
  689. *
  690. * The value of "current" is not changed atomically with
  691. * switch_mm(). But that's OK, we just need to check the
  692. * current cpu's trap block PGD physical address.
  693. */
  694. if (tp->pgd_paddr == __pa(mm->pgd))
  695. tsb_context_switch(mm);
  696. }
  697. void smp_tsb_sync(struct mm_struct *mm)
  698. {
  699. smp_call_function_mask(tsb_sync, mm, 0, 1, mm->cpu_vm_mask);
  700. }
  701. extern unsigned long xcall_flush_tlb_mm;
  702. extern unsigned long xcall_flush_tlb_pending;
  703. extern unsigned long xcall_flush_tlb_kernel_range;
  704. extern unsigned long xcall_report_regs;
  705. extern unsigned long xcall_receive_signal;
  706. extern unsigned long xcall_new_mmu_context_version;
  707. #ifdef DCACHE_ALIASING_POSSIBLE
  708. extern unsigned long xcall_flush_dcache_page_cheetah;
  709. #endif
  710. extern unsigned long xcall_flush_dcache_page_spitfire;
  711. #ifdef CONFIG_DEBUG_DCFLUSH
  712. extern atomic_t dcpage_flushes;
  713. extern atomic_t dcpage_flushes_xcall;
  714. #endif
  715. static __inline__ void __local_flush_dcache_page(struct page *page)
  716. {
  717. #ifdef DCACHE_ALIASING_POSSIBLE
  718. __flush_dcache_page(page_address(page),
  719. ((tlb_type == spitfire) &&
  720. page_mapping(page) != NULL));
  721. #else
  722. if (page_mapping(page) != NULL &&
  723. tlb_type == spitfire)
  724. __flush_icache_page(__pa(page_address(page)));
  725. #endif
  726. }
  727. void smp_flush_dcache_page_impl(struct page *page, int cpu)
  728. {
  729. cpumask_t mask = cpumask_of_cpu(cpu);
  730. int this_cpu;
  731. if (tlb_type == hypervisor)
  732. return;
  733. #ifdef CONFIG_DEBUG_DCFLUSH
  734. atomic_inc(&dcpage_flushes);
  735. #endif
  736. this_cpu = get_cpu();
  737. if (cpu == this_cpu) {
  738. __local_flush_dcache_page(page);
  739. } else if (cpu_online(cpu)) {
  740. void *pg_addr = page_address(page);
  741. u64 data0;
  742. if (tlb_type == spitfire) {
  743. data0 =
  744. ((u64)&xcall_flush_dcache_page_spitfire);
  745. if (page_mapping(page) != NULL)
  746. data0 |= ((u64)1 << 32);
  747. spitfire_xcall_deliver(data0,
  748. __pa(pg_addr),
  749. (u64) pg_addr,
  750. mask);
  751. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  752. #ifdef DCACHE_ALIASING_POSSIBLE
  753. data0 =
  754. ((u64)&xcall_flush_dcache_page_cheetah);
  755. cheetah_xcall_deliver(data0,
  756. __pa(pg_addr),
  757. 0, mask);
  758. #endif
  759. }
  760. #ifdef CONFIG_DEBUG_DCFLUSH
  761. atomic_inc(&dcpage_flushes_xcall);
  762. #endif
  763. }
  764. put_cpu();
  765. }
  766. void flush_dcache_page_all(struct mm_struct *mm, struct page *page)
  767. {
  768. void *pg_addr = page_address(page);
  769. cpumask_t mask = cpu_online_map;
  770. u64 data0;
  771. int this_cpu;
  772. if (tlb_type == hypervisor)
  773. return;
  774. this_cpu = get_cpu();
  775. cpu_clear(this_cpu, mask);
  776. #ifdef CONFIG_DEBUG_DCFLUSH
  777. atomic_inc(&dcpage_flushes);
  778. #endif
  779. if (cpus_empty(mask))
  780. goto flush_self;
  781. if (tlb_type == spitfire) {
  782. data0 = ((u64)&xcall_flush_dcache_page_spitfire);
  783. if (page_mapping(page) != NULL)
  784. data0 |= ((u64)1 << 32);
  785. spitfire_xcall_deliver(data0,
  786. __pa(pg_addr),
  787. (u64) pg_addr,
  788. mask);
  789. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  790. #ifdef DCACHE_ALIASING_POSSIBLE
  791. data0 = ((u64)&xcall_flush_dcache_page_cheetah);
  792. cheetah_xcall_deliver(data0,
  793. __pa(pg_addr),
  794. 0, mask);
  795. #endif
  796. }
  797. #ifdef CONFIG_DEBUG_DCFLUSH
  798. atomic_inc(&dcpage_flushes_xcall);
  799. #endif
  800. flush_self:
  801. __local_flush_dcache_page(page);
  802. put_cpu();
  803. }
  804. static void __smp_receive_signal_mask(cpumask_t mask)
  805. {
  806. smp_cross_call_masked(&xcall_receive_signal, 0, 0, 0, mask);
  807. }
  808. void smp_receive_signal(int cpu)
  809. {
  810. cpumask_t mask = cpumask_of_cpu(cpu);
  811. if (cpu_online(cpu))
  812. __smp_receive_signal_mask(mask);
  813. }
  814. void smp_receive_signal_client(int irq, struct pt_regs *regs)
  815. {
  816. clear_softint(1 << irq);
  817. }
  818. void smp_new_mmu_context_version_client(int irq, struct pt_regs *regs)
  819. {
  820. struct mm_struct *mm;
  821. unsigned long flags;
  822. clear_softint(1 << irq);
  823. /* See if we need to allocate a new TLB context because
  824. * the version of the one we are using is now out of date.
  825. */
  826. mm = current->active_mm;
  827. if (unlikely(!mm || (mm == &init_mm)))
  828. return;
  829. spin_lock_irqsave(&mm->context.lock, flags);
  830. if (unlikely(!CTX_VALID(mm->context)))
  831. get_new_mmu_context(mm);
  832. spin_unlock_irqrestore(&mm->context.lock, flags);
  833. load_secondary_context(mm);
  834. __flush_tlb_mm(CTX_HWBITS(mm->context),
  835. SECONDARY_CONTEXT);
  836. }
  837. void smp_new_mmu_context_version(void)
  838. {
  839. smp_cross_call(&xcall_new_mmu_context_version, 0, 0, 0);
  840. }
  841. void smp_report_regs(void)
  842. {
  843. smp_cross_call(&xcall_report_regs, 0, 0, 0);
  844. }
  845. /* We know that the window frames of the user have been flushed
  846. * to the stack before we get here because all callers of us
  847. * are flush_tlb_*() routines, and these run after flush_cache_*()
  848. * which performs the flushw.
  849. *
  850. * The SMP TLB coherency scheme we use works as follows:
  851. *
  852. * 1) mm->cpu_vm_mask is a bit mask of which cpus an address
  853. * space has (potentially) executed on, this is the heuristic
  854. * we use to avoid doing cross calls.
  855. *
  856. * Also, for flushing from kswapd and also for clones, we
  857. * use cpu_vm_mask as the list of cpus to make run the TLB.
  858. *
  859. * 2) TLB context numbers are shared globally across all processors
  860. * in the system, this allows us to play several games to avoid
  861. * cross calls.
  862. *
  863. * One invariant is that when a cpu switches to a process, and
  864. * that processes tsk->active_mm->cpu_vm_mask does not have the
  865. * current cpu's bit set, that tlb context is flushed locally.
  866. *
  867. * If the address space is non-shared (ie. mm->count == 1) we avoid
  868. * cross calls when we want to flush the currently running process's
  869. * tlb state. This is done by clearing all cpu bits except the current
  870. * processor's in current->active_mm->cpu_vm_mask and performing the
  871. * flush locally only. This will force any subsequent cpus which run
  872. * this task to flush the context from the local tlb if the process
  873. * migrates to another cpu (again).
  874. *
  875. * 3) For shared address spaces (threads) and swapping we bite the
  876. * bullet for most cases and perform the cross call (but only to
  877. * the cpus listed in cpu_vm_mask).
  878. *
  879. * The performance gain from "optimizing" away the cross call for threads is
  880. * questionable (in theory the big win for threads is the massive sharing of
  881. * address space state across processors).
  882. */
  883. /* This currently is only used by the hugetlb arch pre-fault
  884. * hook on UltraSPARC-III+ and later when changing the pagesize
  885. * bits of the context register for an address space.
  886. */
  887. void smp_flush_tlb_mm(struct mm_struct *mm)
  888. {
  889. u32 ctx = CTX_HWBITS(mm->context);
  890. int cpu = get_cpu();
  891. if (atomic_read(&mm->mm_users) == 1) {
  892. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  893. goto local_flush_and_out;
  894. }
  895. smp_cross_call_masked(&xcall_flush_tlb_mm,
  896. ctx, 0, 0,
  897. mm->cpu_vm_mask);
  898. local_flush_and_out:
  899. __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
  900. put_cpu();
  901. }
  902. void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs)
  903. {
  904. u32 ctx = CTX_HWBITS(mm->context);
  905. int cpu = get_cpu();
  906. if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1)
  907. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  908. else
  909. smp_cross_call_masked(&xcall_flush_tlb_pending,
  910. ctx, nr, (unsigned long) vaddrs,
  911. mm->cpu_vm_mask);
  912. __flush_tlb_pending(ctx, nr, vaddrs);
  913. put_cpu();
  914. }
  915. void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  916. {
  917. start &= PAGE_MASK;
  918. end = PAGE_ALIGN(end);
  919. if (start != end) {
  920. smp_cross_call(&xcall_flush_tlb_kernel_range,
  921. 0, start, end);
  922. __flush_tlb_kernel_range(start, end);
  923. }
  924. }
  925. /* CPU capture. */
  926. /* #define CAPTURE_DEBUG */
  927. extern unsigned long xcall_capture;
  928. static atomic_t smp_capture_depth = ATOMIC_INIT(0);
  929. static atomic_t smp_capture_registry = ATOMIC_INIT(0);
  930. static unsigned long penguins_are_doing_time;
  931. void smp_capture(void)
  932. {
  933. int result = atomic_add_ret(1, &smp_capture_depth);
  934. if (result == 1) {
  935. int ncpus = num_online_cpus();
  936. #ifdef CAPTURE_DEBUG
  937. printk("CPU[%d]: Sending penguins to jail...",
  938. smp_processor_id());
  939. #endif
  940. penguins_are_doing_time = 1;
  941. membar_storestore_loadstore();
  942. atomic_inc(&smp_capture_registry);
  943. smp_cross_call(&xcall_capture, 0, 0, 0);
  944. while (atomic_read(&smp_capture_registry) != ncpus)
  945. rmb();
  946. #ifdef CAPTURE_DEBUG
  947. printk("done\n");
  948. #endif
  949. }
  950. }
  951. void smp_release(void)
  952. {
  953. if (atomic_dec_and_test(&smp_capture_depth)) {
  954. #ifdef CAPTURE_DEBUG
  955. printk("CPU[%d]: Giving pardon to "
  956. "imprisoned penguins\n",
  957. smp_processor_id());
  958. #endif
  959. penguins_are_doing_time = 0;
  960. membar_storeload_storestore();
  961. atomic_dec(&smp_capture_registry);
  962. }
  963. }
  964. /* Imprisoned penguins run with %pil == 15, but PSTATE_IE set, so they
  965. * can service tlb flush xcalls...
  966. */
  967. extern void prom_world(int);
  968. void smp_penguin_jailcell(int irq, struct pt_regs *regs)
  969. {
  970. clear_softint(1 << irq);
  971. preempt_disable();
  972. __asm__ __volatile__("flushw");
  973. prom_world(1);
  974. atomic_inc(&smp_capture_registry);
  975. membar_storeload_storestore();
  976. while (penguins_are_doing_time)
  977. rmb();
  978. atomic_dec(&smp_capture_registry);
  979. prom_world(0);
  980. preempt_enable();
  981. }
  982. #define prof_multiplier(__cpu) cpu_data(__cpu).multiplier
  983. #define prof_counter(__cpu) cpu_data(__cpu).counter
  984. void smp_percpu_timer_interrupt(struct pt_regs *regs)
  985. {
  986. unsigned long compare, tick, pstate;
  987. int cpu = smp_processor_id();
  988. int user = user_mode(regs);
  989. struct pt_regs *old_regs;
  990. /*
  991. * Check for level 14 softint.
  992. */
  993. {
  994. unsigned long tick_mask = tick_ops->softint_mask;
  995. if (!(get_softint() & tick_mask)) {
  996. extern void handler_irq(int, struct pt_regs *);
  997. handler_irq(14, regs);
  998. return;
  999. }
  1000. clear_softint(tick_mask);
  1001. }
  1002. old_regs = set_irq_regs(regs);
  1003. do {
  1004. profile_tick(CPU_PROFILING);
  1005. if (!--prof_counter(cpu)) {
  1006. irq_enter();
  1007. if (cpu == boot_cpu_id) {
  1008. kstat_this_cpu.irqs[0]++;
  1009. timer_tick_interrupt(regs);
  1010. }
  1011. update_process_times(user);
  1012. irq_exit();
  1013. prof_counter(cpu) = prof_multiplier(cpu);
  1014. }
  1015. /* Guarantee that the following sequences execute
  1016. * uninterrupted.
  1017. */
  1018. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  1019. "wrpr %0, %1, %%pstate"
  1020. : "=r" (pstate)
  1021. : "i" (PSTATE_IE));
  1022. compare = tick_ops->add_compare(current_tick_offset);
  1023. tick = tick_ops->get_tick();
  1024. /* Restore PSTATE_IE. */
  1025. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  1026. : /* no outputs */
  1027. : "r" (pstate));
  1028. } while (time_after_eq(tick, compare));
  1029. set_irq_regs(old_regs);
  1030. }
  1031. static void __init smp_setup_percpu_timer(void)
  1032. {
  1033. int cpu = smp_processor_id();
  1034. unsigned long pstate;
  1035. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  1036. /* Guarantee that the following sequences execute
  1037. * uninterrupted.
  1038. */
  1039. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  1040. "wrpr %0, %1, %%pstate"
  1041. : "=r" (pstate)
  1042. : "i" (PSTATE_IE));
  1043. tick_ops->init_tick(current_tick_offset);
  1044. /* Restore PSTATE_IE. */
  1045. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  1046. : /* no outputs */
  1047. : "r" (pstate));
  1048. }
  1049. void __init smp_tick_init(void)
  1050. {
  1051. boot_cpu_id = hard_smp_processor_id();
  1052. current_tick_offset = timer_tick_offset;
  1053. prof_counter(boot_cpu_id) = prof_multiplier(boot_cpu_id) = 1;
  1054. }
  1055. /* /proc/profile writes can call this, don't __init it please. */
  1056. static DEFINE_SPINLOCK(prof_setup_lock);
  1057. int setup_profiling_timer(unsigned int multiplier)
  1058. {
  1059. unsigned long flags;
  1060. int i;
  1061. if ((!multiplier) || (timer_tick_offset / multiplier) < 1000)
  1062. return -EINVAL;
  1063. spin_lock_irqsave(&prof_setup_lock, flags);
  1064. for_each_possible_cpu(i)
  1065. prof_multiplier(i) = multiplier;
  1066. current_tick_offset = (timer_tick_offset / multiplier);
  1067. spin_unlock_irqrestore(&prof_setup_lock, flags);
  1068. return 0;
  1069. }
  1070. static void __init smp_tune_scheduling(void)
  1071. {
  1072. struct device_node *dp;
  1073. int instance;
  1074. unsigned int def, smallest = ~0U;
  1075. def = ((tlb_type == hypervisor) ?
  1076. (3 * 1024 * 1024) :
  1077. (4 * 1024 * 1024));
  1078. instance = 0;
  1079. while (!cpu_find_by_instance(instance, &dp, NULL)) {
  1080. unsigned int val;
  1081. val = of_getintprop_default(dp, "ecache-size", def);
  1082. if (val < smallest)
  1083. smallest = val;
  1084. instance++;
  1085. }
  1086. /* Any value less than 256K is nonsense. */
  1087. if (smallest < (256U * 1024U))
  1088. smallest = 256 * 1024;
  1089. max_cache_size = smallest;
  1090. if (smallest < 1U * 1024U * 1024U)
  1091. printk(KERN_INFO "Using max_cache_size of %uKB\n",
  1092. smallest / 1024U);
  1093. else
  1094. printk(KERN_INFO "Using max_cache_size of %uMB\n",
  1095. smallest / 1024U / 1024U);
  1096. }
  1097. /* Constrain the number of cpus to max_cpus. */
  1098. void __init smp_prepare_cpus(unsigned int max_cpus)
  1099. {
  1100. int i;
  1101. if (num_possible_cpus() > max_cpus) {
  1102. int instance, mid;
  1103. instance = 0;
  1104. while (!cpu_find_by_instance(instance, NULL, &mid)) {
  1105. if (mid != boot_cpu_id) {
  1106. cpu_clear(mid, phys_cpu_present_map);
  1107. cpu_clear(mid, cpu_present_map);
  1108. if (num_possible_cpus() <= max_cpus)
  1109. break;
  1110. }
  1111. instance++;
  1112. }
  1113. }
  1114. for_each_possible_cpu(i) {
  1115. if (tlb_type == hypervisor) {
  1116. int j;
  1117. /* XXX get this mapping from machine description */
  1118. for_each_possible_cpu(j) {
  1119. if ((j >> 2) == (i >> 2))
  1120. cpu_set(j, cpu_sibling_map[i]);
  1121. }
  1122. } else {
  1123. cpu_set(i, cpu_sibling_map[i]);
  1124. }
  1125. }
  1126. smp_store_cpu_info(boot_cpu_id);
  1127. smp_tune_scheduling();
  1128. }
  1129. /* Set this up early so that things like the scheduler can init
  1130. * properly. We use the same cpu mask for both the present and
  1131. * possible cpu map.
  1132. */
  1133. void __init smp_setup_cpu_possible_map(void)
  1134. {
  1135. int instance, mid;
  1136. instance = 0;
  1137. while (!cpu_find_by_instance(instance, NULL, &mid)) {
  1138. if (mid < NR_CPUS) {
  1139. cpu_set(mid, phys_cpu_present_map);
  1140. cpu_set(mid, cpu_present_map);
  1141. }
  1142. instance++;
  1143. }
  1144. }
  1145. void __devinit smp_prepare_boot_cpu(void)
  1146. {
  1147. }
  1148. int __cpuinit __cpu_up(unsigned int cpu)
  1149. {
  1150. int ret = smp_boot_one_cpu(cpu);
  1151. if (!ret) {
  1152. cpu_set(cpu, smp_commenced_mask);
  1153. while (!cpu_isset(cpu, cpu_online_map))
  1154. mb();
  1155. if (!cpu_isset(cpu, cpu_online_map)) {
  1156. ret = -ENODEV;
  1157. } else {
  1158. /* On SUN4V, writes to %tick and %stick are
  1159. * not allowed.
  1160. */
  1161. if (tlb_type != hypervisor)
  1162. smp_synchronize_one_tick(cpu);
  1163. }
  1164. }
  1165. return ret;
  1166. }
  1167. void __init smp_cpus_done(unsigned int max_cpus)
  1168. {
  1169. unsigned long bogosum = 0;
  1170. int i;
  1171. for_each_online_cpu(i)
  1172. bogosum += cpu_data(i).udelay_val;
  1173. printk("Total of %ld processors activated "
  1174. "(%lu.%02lu BogoMIPS).\n",
  1175. (long) num_online_cpus(),
  1176. bogosum/(500000/HZ),
  1177. (bogosum/(5000/HZ))%100);
  1178. }
  1179. void smp_send_reschedule(int cpu)
  1180. {
  1181. smp_receive_signal(cpu);
  1182. }
  1183. /* This is a nop because we capture all other cpus
  1184. * anyways when making the PROM active.
  1185. */
  1186. void smp_send_stop(void)
  1187. {
  1188. }
  1189. unsigned long __per_cpu_base __read_mostly;
  1190. unsigned long __per_cpu_shift __read_mostly;
  1191. EXPORT_SYMBOL(__per_cpu_base);
  1192. EXPORT_SYMBOL(__per_cpu_shift);
  1193. void __init setup_per_cpu_areas(void)
  1194. {
  1195. unsigned long goal, size, i;
  1196. char *ptr;
  1197. /* Copy section for each CPU (we discard the original) */
  1198. goal = PERCPU_ENOUGH_ROOM;
  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. }