smp.c 34 KB

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