smp.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436
  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 __cacheline_aligned_in_smp DEFINE_SPINLOCK(call_lock);
  619. static struct call_data_struct *call_data;
  620. extern unsigned long xcall_call_function;
  621. /**
  622. * smp_call_function(): Run a function on all other CPUs.
  623. * @func: The function to run. This must be fast and non-blocking.
  624. * @info: An arbitrary pointer to pass to the function.
  625. * @nonatomic: currently unused.
  626. * @wait: If true, wait (atomically) until function has completed on other CPUs.
  627. *
  628. * Returns 0 on success, else a negative status code. Does not return until
  629. * remote CPUs are nearly ready to execute <<func>> or are or have executed.
  630. *
  631. * You must not call this function with disabled interrupts or from a
  632. * hardware interrupt handler or from a bottom half handler.
  633. */
  634. static int smp_call_function_mask(void (*func)(void *info), void *info,
  635. int nonatomic, int wait, cpumask_t mask)
  636. {
  637. struct call_data_struct data;
  638. int cpus;
  639. /* Can deadlock when called with interrupts disabled */
  640. WARN_ON(irqs_disabled());
  641. data.func = func;
  642. data.info = info;
  643. atomic_set(&data.finished, 0);
  644. data.wait = wait;
  645. spin_lock(&call_lock);
  646. cpu_clear(smp_processor_id(), mask);
  647. cpus = cpus_weight(mask);
  648. if (!cpus)
  649. goto out_unlock;
  650. call_data = &data;
  651. mb();
  652. smp_cross_call_masked(&xcall_call_function, 0, 0, 0, mask);
  653. /* Wait for response */
  654. while (atomic_read(&data.finished) != cpus)
  655. cpu_relax();
  656. out_unlock:
  657. spin_unlock(&call_lock);
  658. return 0;
  659. }
  660. int smp_call_function(void (*func)(void *info), void *info,
  661. int nonatomic, int wait)
  662. {
  663. return smp_call_function_mask(func, info, nonatomic, wait,
  664. cpu_online_map);
  665. }
  666. void smp_call_function_client(int irq, struct pt_regs *regs)
  667. {
  668. void (*func) (void *info) = call_data->func;
  669. void *info = call_data->info;
  670. clear_softint(1 << irq);
  671. if (call_data->wait) {
  672. /* let initiator proceed only after completion */
  673. func(info);
  674. atomic_inc(&call_data->finished);
  675. } else {
  676. /* let initiator proceed after getting data */
  677. atomic_inc(&call_data->finished);
  678. func(info);
  679. }
  680. }
  681. static void tsb_sync(void *info)
  682. {
  683. struct trap_per_cpu *tp = &trap_block[raw_smp_processor_id()];
  684. struct mm_struct *mm = info;
  685. /* It is not valid to test "currrent->active_mm == mm" here.
  686. *
  687. * The value of "current" is not changed atomically with
  688. * switch_mm(). But that's OK, we just need to check the
  689. * current cpu's trap block PGD physical address.
  690. */
  691. if (tp->pgd_paddr == __pa(mm->pgd))
  692. tsb_context_switch(mm);
  693. }
  694. void smp_tsb_sync(struct mm_struct *mm)
  695. {
  696. smp_call_function_mask(tsb_sync, mm, 0, 1, mm->cpu_vm_mask);
  697. }
  698. extern unsigned long xcall_flush_tlb_mm;
  699. extern unsigned long xcall_flush_tlb_pending;
  700. extern unsigned long xcall_flush_tlb_kernel_range;
  701. extern unsigned long xcall_report_regs;
  702. extern unsigned long xcall_receive_signal;
  703. extern unsigned long xcall_new_mmu_context_version;
  704. #ifdef DCACHE_ALIASING_POSSIBLE
  705. extern unsigned long xcall_flush_dcache_page_cheetah;
  706. #endif
  707. extern unsigned long xcall_flush_dcache_page_spitfire;
  708. #ifdef CONFIG_DEBUG_DCFLUSH
  709. extern atomic_t dcpage_flushes;
  710. extern atomic_t dcpage_flushes_xcall;
  711. #endif
  712. static __inline__ void __local_flush_dcache_page(struct page *page)
  713. {
  714. #ifdef DCACHE_ALIASING_POSSIBLE
  715. __flush_dcache_page(page_address(page),
  716. ((tlb_type == spitfire) &&
  717. page_mapping(page) != NULL));
  718. #else
  719. if (page_mapping(page) != NULL &&
  720. tlb_type == spitfire)
  721. __flush_icache_page(__pa(page_address(page)));
  722. #endif
  723. }
  724. void smp_flush_dcache_page_impl(struct page *page, int cpu)
  725. {
  726. cpumask_t mask = cpumask_of_cpu(cpu);
  727. int this_cpu;
  728. if (tlb_type == hypervisor)
  729. return;
  730. #ifdef CONFIG_DEBUG_DCFLUSH
  731. atomic_inc(&dcpage_flushes);
  732. #endif
  733. this_cpu = get_cpu();
  734. if (cpu == this_cpu) {
  735. __local_flush_dcache_page(page);
  736. } else if (cpu_online(cpu)) {
  737. void *pg_addr = page_address(page);
  738. u64 data0;
  739. if (tlb_type == spitfire) {
  740. data0 =
  741. ((u64)&xcall_flush_dcache_page_spitfire);
  742. if (page_mapping(page) != NULL)
  743. data0 |= ((u64)1 << 32);
  744. spitfire_xcall_deliver(data0,
  745. __pa(pg_addr),
  746. (u64) pg_addr,
  747. mask);
  748. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  749. #ifdef DCACHE_ALIASING_POSSIBLE
  750. data0 =
  751. ((u64)&xcall_flush_dcache_page_cheetah);
  752. cheetah_xcall_deliver(data0,
  753. __pa(pg_addr),
  754. 0, mask);
  755. #endif
  756. }
  757. #ifdef CONFIG_DEBUG_DCFLUSH
  758. atomic_inc(&dcpage_flushes_xcall);
  759. #endif
  760. }
  761. put_cpu();
  762. }
  763. void flush_dcache_page_all(struct mm_struct *mm, struct page *page)
  764. {
  765. void *pg_addr = page_address(page);
  766. cpumask_t mask = cpu_online_map;
  767. u64 data0;
  768. int this_cpu;
  769. if (tlb_type == hypervisor)
  770. return;
  771. this_cpu = get_cpu();
  772. cpu_clear(this_cpu, mask);
  773. #ifdef CONFIG_DEBUG_DCFLUSH
  774. atomic_inc(&dcpage_flushes);
  775. #endif
  776. if (cpus_empty(mask))
  777. goto flush_self;
  778. if (tlb_type == spitfire) {
  779. data0 = ((u64)&xcall_flush_dcache_page_spitfire);
  780. if (page_mapping(page) != NULL)
  781. data0 |= ((u64)1 << 32);
  782. spitfire_xcall_deliver(data0,
  783. __pa(pg_addr),
  784. (u64) pg_addr,
  785. mask);
  786. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  787. #ifdef DCACHE_ALIASING_POSSIBLE
  788. data0 = ((u64)&xcall_flush_dcache_page_cheetah);
  789. cheetah_xcall_deliver(data0,
  790. __pa(pg_addr),
  791. 0, mask);
  792. #endif
  793. }
  794. #ifdef CONFIG_DEBUG_DCFLUSH
  795. atomic_inc(&dcpage_flushes_xcall);
  796. #endif
  797. flush_self:
  798. __local_flush_dcache_page(page);
  799. put_cpu();
  800. }
  801. static void __smp_receive_signal_mask(cpumask_t mask)
  802. {
  803. smp_cross_call_masked(&xcall_receive_signal, 0, 0, 0, mask);
  804. }
  805. void smp_receive_signal(int cpu)
  806. {
  807. cpumask_t mask = cpumask_of_cpu(cpu);
  808. if (cpu_online(cpu))
  809. __smp_receive_signal_mask(mask);
  810. }
  811. void smp_receive_signal_client(int irq, struct pt_regs *regs)
  812. {
  813. clear_softint(1 << irq);
  814. }
  815. void smp_new_mmu_context_version_client(int irq, struct pt_regs *regs)
  816. {
  817. struct mm_struct *mm;
  818. unsigned long flags;
  819. clear_softint(1 << irq);
  820. /* See if we need to allocate a new TLB context because
  821. * the version of the one we are using is now out of date.
  822. */
  823. mm = current->active_mm;
  824. if (unlikely(!mm || (mm == &init_mm)))
  825. return;
  826. spin_lock_irqsave(&mm->context.lock, flags);
  827. if (unlikely(!CTX_VALID(mm->context)))
  828. get_new_mmu_context(mm);
  829. spin_unlock_irqrestore(&mm->context.lock, flags);
  830. load_secondary_context(mm);
  831. __flush_tlb_mm(CTX_HWBITS(mm->context),
  832. SECONDARY_CONTEXT);
  833. }
  834. void smp_new_mmu_context_version(void)
  835. {
  836. smp_cross_call(&xcall_new_mmu_context_version, 0, 0, 0);
  837. }
  838. void smp_report_regs(void)
  839. {
  840. smp_cross_call(&xcall_report_regs, 0, 0, 0);
  841. }
  842. /* We know that the window frames of the user have been flushed
  843. * to the stack before we get here because all callers of us
  844. * are flush_tlb_*() routines, and these run after flush_cache_*()
  845. * which performs the flushw.
  846. *
  847. * The SMP TLB coherency scheme we use works as follows:
  848. *
  849. * 1) mm->cpu_vm_mask is a bit mask of which cpus an address
  850. * space has (potentially) executed on, this is the heuristic
  851. * we use to avoid doing cross calls.
  852. *
  853. * Also, for flushing from kswapd and also for clones, we
  854. * use cpu_vm_mask as the list of cpus to make run the TLB.
  855. *
  856. * 2) TLB context numbers are shared globally across all processors
  857. * in the system, this allows us to play several games to avoid
  858. * cross calls.
  859. *
  860. * One invariant is that when a cpu switches to a process, and
  861. * that processes tsk->active_mm->cpu_vm_mask does not have the
  862. * current cpu's bit set, that tlb context is flushed locally.
  863. *
  864. * If the address space is non-shared (ie. mm->count == 1) we avoid
  865. * cross calls when we want to flush the currently running process's
  866. * tlb state. This is done by clearing all cpu bits except the current
  867. * processor's in current->active_mm->cpu_vm_mask and performing the
  868. * flush locally only. This will force any subsequent cpus which run
  869. * this task to flush the context from the local tlb if the process
  870. * migrates to another cpu (again).
  871. *
  872. * 3) For shared address spaces (threads) and swapping we bite the
  873. * bullet for most cases and perform the cross call (but only to
  874. * the cpus listed in cpu_vm_mask).
  875. *
  876. * The performance gain from "optimizing" away the cross call for threads is
  877. * questionable (in theory the big win for threads is the massive sharing of
  878. * address space state across processors).
  879. */
  880. /* This currently is only used by the hugetlb arch pre-fault
  881. * hook on UltraSPARC-III+ and later when changing the pagesize
  882. * bits of the context register for an address space.
  883. */
  884. void smp_flush_tlb_mm(struct mm_struct *mm)
  885. {
  886. u32 ctx = CTX_HWBITS(mm->context);
  887. int cpu = get_cpu();
  888. if (atomic_read(&mm->mm_users) == 1) {
  889. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  890. goto local_flush_and_out;
  891. }
  892. smp_cross_call_masked(&xcall_flush_tlb_mm,
  893. ctx, 0, 0,
  894. mm->cpu_vm_mask);
  895. local_flush_and_out:
  896. __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
  897. put_cpu();
  898. }
  899. void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs)
  900. {
  901. u32 ctx = CTX_HWBITS(mm->context);
  902. int cpu = get_cpu();
  903. if (mm == current->active_mm && atomic_read(&mm->mm_users) == 1)
  904. mm->cpu_vm_mask = cpumask_of_cpu(cpu);
  905. else
  906. smp_cross_call_masked(&xcall_flush_tlb_pending,
  907. ctx, nr, (unsigned long) vaddrs,
  908. mm->cpu_vm_mask);
  909. __flush_tlb_pending(ctx, nr, vaddrs);
  910. put_cpu();
  911. }
  912. void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  913. {
  914. start &= PAGE_MASK;
  915. end = PAGE_ALIGN(end);
  916. if (start != end) {
  917. smp_cross_call(&xcall_flush_tlb_kernel_range,
  918. 0, start, end);
  919. __flush_tlb_kernel_range(start, end);
  920. }
  921. }
  922. /* CPU capture. */
  923. /* #define CAPTURE_DEBUG */
  924. extern unsigned long xcall_capture;
  925. static atomic_t smp_capture_depth = ATOMIC_INIT(0);
  926. static atomic_t smp_capture_registry = ATOMIC_INIT(0);
  927. static unsigned long penguins_are_doing_time;
  928. void smp_capture(void)
  929. {
  930. int result = atomic_add_ret(1, &smp_capture_depth);
  931. if (result == 1) {
  932. int ncpus = num_online_cpus();
  933. #ifdef CAPTURE_DEBUG
  934. printk("CPU[%d]: Sending penguins to jail...",
  935. smp_processor_id());
  936. #endif
  937. penguins_are_doing_time = 1;
  938. membar_storestore_loadstore();
  939. atomic_inc(&smp_capture_registry);
  940. smp_cross_call(&xcall_capture, 0, 0, 0);
  941. while (atomic_read(&smp_capture_registry) != ncpus)
  942. rmb();
  943. #ifdef CAPTURE_DEBUG
  944. printk("done\n");
  945. #endif
  946. }
  947. }
  948. void smp_release(void)
  949. {
  950. if (atomic_dec_and_test(&smp_capture_depth)) {
  951. #ifdef CAPTURE_DEBUG
  952. printk("CPU[%d]: Giving pardon to "
  953. "imprisoned penguins\n",
  954. smp_processor_id());
  955. #endif
  956. penguins_are_doing_time = 0;
  957. membar_storeload_storestore();
  958. atomic_dec(&smp_capture_registry);
  959. }
  960. }
  961. /* Imprisoned penguins run with %pil == 15, but PSTATE_IE set, so they
  962. * can service tlb flush xcalls...
  963. */
  964. extern void prom_world(int);
  965. void smp_penguin_jailcell(int irq, struct pt_regs *regs)
  966. {
  967. clear_softint(1 << irq);
  968. preempt_disable();
  969. __asm__ __volatile__("flushw");
  970. prom_world(1);
  971. atomic_inc(&smp_capture_registry);
  972. membar_storeload_storestore();
  973. while (penguins_are_doing_time)
  974. rmb();
  975. atomic_dec(&smp_capture_registry);
  976. prom_world(0);
  977. preempt_enable();
  978. }
  979. #define prof_multiplier(__cpu) cpu_data(__cpu).multiplier
  980. #define prof_counter(__cpu) cpu_data(__cpu).counter
  981. void smp_percpu_timer_interrupt(struct pt_regs *regs)
  982. {
  983. unsigned long compare, tick, pstate;
  984. int cpu = smp_processor_id();
  985. int user = user_mode(regs);
  986. /*
  987. * Check for level 14 softint.
  988. */
  989. {
  990. unsigned long tick_mask = tick_ops->softint_mask;
  991. if (!(get_softint() & tick_mask)) {
  992. extern void handler_irq(int, struct pt_regs *);
  993. handler_irq(14, regs);
  994. return;
  995. }
  996. clear_softint(tick_mask);
  997. }
  998. do {
  999. profile_tick(CPU_PROFILING, regs);
  1000. if (!--prof_counter(cpu)) {
  1001. irq_enter();
  1002. if (cpu == boot_cpu_id) {
  1003. kstat_this_cpu.irqs[0]++;
  1004. timer_tick_interrupt(regs);
  1005. }
  1006. update_process_times(user);
  1007. irq_exit();
  1008. prof_counter(cpu) = prof_multiplier(cpu);
  1009. }
  1010. /* Guarantee that the following sequences execute
  1011. * uninterrupted.
  1012. */
  1013. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  1014. "wrpr %0, %1, %%pstate"
  1015. : "=r" (pstate)
  1016. : "i" (PSTATE_IE));
  1017. compare = tick_ops->add_compare(current_tick_offset);
  1018. tick = tick_ops->get_tick();
  1019. /* Restore PSTATE_IE. */
  1020. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  1021. : /* no outputs */
  1022. : "r" (pstate));
  1023. } while (time_after_eq(tick, compare));
  1024. }
  1025. static void __init smp_setup_percpu_timer(void)
  1026. {
  1027. int cpu = smp_processor_id();
  1028. unsigned long pstate;
  1029. prof_counter(cpu) = prof_multiplier(cpu) = 1;
  1030. /* Guarantee that the following sequences execute
  1031. * uninterrupted.
  1032. */
  1033. __asm__ __volatile__("rdpr %%pstate, %0\n\t"
  1034. "wrpr %0, %1, %%pstate"
  1035. : "=r" (pstate)
  1036. : "i" (PSTATE_IE));
  1037. tick_ops->init_tick(current_tick_offset);
  1038. /* Restore PSTATE_IE. */
  1039. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  1040. : /* no outputs */
  1041. : "r" (pstate));
  1042. }
  1043. void __init smp_tick_init(void)
  1044. {
  1045. boot_cpu_id = hard_smp_processor_id();
  1046. current_tick_offset = timer_tick_offset;
  1047. cpu_set(boot_cpu_id, cpu_online_map);
  1048. prof_counter(boot_cpu_id) = prof_multiplier(boot_cpu_id) = 1;
  1049. }
  1050. /* /proc/profile writes can call this, don't __init it please. */
  1051. static DEFINE_SPINLOCK(prof_setup_lock);
  1052. int setup_profiling_timer(unsigned int multiplier)
  1053. {
  1054. unsigned long flags;
  1055. int i;
  1056. if ((!multiplier) || (timer_tick_offset / multiplier) < 1000)
  1057. return -EINVAL;
  1058. spin_lock_irqsave(&prof_setup_lock, flags);
  1059. for_each_possible_cpu(i)
  1060. prof_multiplier(i) = multiplier;
  1061. current_tick_offset = (timer_tick_offset / multiplier);
  1062. spin_unlock_irqrestore(&prof_setup_lock, flags);
  1063. return 0;
  1064. }
  1065. /* Constrain the number of cpus to max_cpus. */
  1066. void __init smp_prepare_cpus(unsigned int max_cpus)
  1067. {
  1068. int i;
  1069. if (num_possible_cpus() > max_cpus) {
  1070. int instance, mid;
  1071. instance = 0;
  1072. while (!cpu_find_by_instance(instance, NULL, &mid)) {
  1073. if (mid != boot_cpu_id) {
  1074. cpu_clear(mid, phys_cpu_present_map);
  1075. cpu_clear(mid, cpu_present_map);
  1076. if (num_possible_cpus() <= max_cpus)
  1077. break;
  1078. }
  1079. instance++;
  1080. }
  1081. }
  1082. for_each_possible_cpu(i) {
  1083. if (tlb_type == hypervisor) {
  1084. int j;
  1085. /* XXX get this mapping from machine description */
  1086. for_each_possible_cpu(j) {
  1087. if ((j >> 2) == (i >> 2))
  1088. cpu_set(j, cpu_sibling_map[i]);
  1089. }
  1090. } else {
  1091. cpu_set(i, cpu_sibling_map[i]);
  1092. }
  1093. }
  1094. smp_store_cpu_info(boot_cpu_id);
  1095. }
  1096. /* Set this up early so that things like the scheduler can init
  1097. * properly. We use the same cpu mask for both the present and
  1098. * possible cpu map.
  1099. */
  1100. void __init smp_setup_cpu_possible_map(void)
  1101. {
  1102. int instance, mid;
  1103. instance = 0;
  1104. while (!cpu_find_by_instance(instance, NULL, &mid)) {
  1105. if (mid < NR_CPUS) {
  1106. cpu_set(mid, phys_cpu_present_map);
  1107. cpu_set(mid, cpu_present_map);
  1108. }
  1109. instance++;
  1110. }
  1111. }
  1112. void __devinit smp_prepare_boot_cpu(void)
  1113. {
  1114. int cpu = hard_smp_processor_id();
  1115. if (cpu >= NR_CPUS) {
  1116. prom_printf("Serious problem, boot cpu id >= NR_CPUS\n");
  1117. prom_halt();
  1118. }
  1119. current_thread_info()->cpu = cpu;
  1120. __local_per_cpu_offset = __per_cpu_offset(cpu);
  1121. cpu_set(smp_processor_id(), cpu_online_map);
  1122. cpu_set(smp_processor_id(), phys_cpu_present_map);
  1123. }
  1124. int __devinit __cpu_up(unsigned int cpu)
  1125. {
  1126. int ret = smp_boot_one_cpu(cpu);
  1127. if (!ret) {
  1128. cpu_set(cpu, smp_commenced_mask);
  1129. while (!cpu_isset(cpu, cpu_online_map))
  1130. mb();
  1131. if (!cpu_isset(cpu, cpu_online_map)) {
  1132. ret = -ENODEV;
  1133. } else {
  1134. /* On SUN4V, writes to %tick and %stick are
  1135. * not allowed.
  1136. */
  1137. if (tlb_type != hypervisor)
  1138. smp_synchronize_one_tick(cpu);
  1139. }
  1140. }
  1141. return ret;
  1142. }
  1143. void __init smp_cpus_done(unsigned int max_cpus)
  1144. {
  1145. unsigned long bogosum = 0;
  1146. int i;
  1147. for_each_online_cpu(i)
  1148. bogosum += cpu_data(i).udelay_val;
  1149. printk("Total of %ld processors activated "
  1150. "(%lu.%02lu BogoMIPS).\n",
  1151. (long) num_online_cpus(),
  1152. bogosum/(500000/HZ),
  1153. (bogosum/(5000/HZ))%100);
  1154. }
  1155. void smp_send_reschedule(int cpu)
  1156. {
  1157. smp_receive_signal(cpu);
  1158. }
  1159. /* This is a nop because we capture all other cpus
  1160. * anyways when making the PROM active.
  1161. */
  1162. void smp_send_stop(void)
  1163. {
  1164. }
  1165. unsigned long __per_cpu_base __read_mostly;
  1166. unsigned long __per_cpu_shift __read_mostly;
  1167. EXPORT_SYMBOL(__per_cpu_base);
  1168. EXPORT_SYMBOL(__per_cpu_shift);
  1169. void __init setup_per_cpu_areas(void)
  1170. {
  1171. unsigned long goal, size, i;
  1172. char *ptr;
  1173. /* Copy section for each CPU (we discard the original) */
  1174. goal = ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES);
  1175. #ifdef CONFIG_MODULES
  1176. if (goal < PERCPU_ENOUGH_ROOM)
  1177. goal = PERCPU_ENOUGH_ROOM;
  1178. #endif
  1179. __per_cpu_shift = 0;
  1180. for (size = 1UL; size < goal; size <<= 1UL)
  1181. __per_cpu_shift++;
  1182. ptr = alloc_bootmem(size * NR_CPUS);
  1183. __per_cpu_base = ptr - __per_cpu_start;
  1184. for (i = 0; i < NR_CPUS; i++, ptr += size)
  1185. memcpy(ptr, __per_cpu_start, __per_cpu_end - __per_cpu_start);
  1186. }