smp_64.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533
  1. /* smp.c: Sparc64 SMP support.
  2. *
  3. * Copyright (C) 1997, 2007, 2008 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/export.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/interrupt.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/fs.h>
  18. #include <linux/seq_file.h>
  19. #include <linux/cache.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/profile.h>
  22. #include <linux/bootmem.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/ftrace.h>
  25. #include <linux/cpu.h>
  26. #include <linux/slab.h>
  27. #include <linux/kgdb.h>
  28. #include <asm/head.h>
  29. #include <asm/ptrace.h>
  30. #include <linux/atomic.h>
  31. #include <asm/tlbflush.h>
  32. #include <asm/mmu_context.h>
  33. #include <asm/cpudata.h>
  34. #include <asm/hvtramp.h>
  35. #include <asm/io.h>
  36. #include <asm/timer.h>
  37. #include <asm/setup.h>
  38. #include <asm/irq.h>
  39. #include <asm/irq_regs.h>
  40. #include <asm/page.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/oplib.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/starfire.h>
  45. #include <asm/tlb.h>
  46. #include <asm/sections.h>
  47. #include <asm/prom.h>
  48. #include <asm/mdesc.h>
  49. #include <asm/ldc.h>
  50. #include <asm/hypervisor.h>
  51. #include <asm/pcr.h>
  52. #include "cpumap.h"
  53. #include "kernel.h"
  54. DEFINE_PER_CPU(cpumask_t, cpu_sibling_map) = CPU_MASK_NONE;
  55. cpumask_t cpu_core_map[NR_CPUS] __read_mostly =
  56. { [0 ... NR_CPUS-1] = CPU_MASK_NONE };
  57. EXPORT_PER_CPU_SYMBOL(cpu_sibling_map);
  58. EXPORT_SYMBOL(cpu_core_map);
  59. static cpumask_t smp_commenced_mask;
  60. void smp_info(struct seq_file *m)
  61. {
  62. int i;
  63. seq_printf(m, "State:\n");
  64. for_each_online_cpu(i)
  65. seq_printf(m, "CPU%d:\t\tonline\n", i);
  66. }
  67. void smp_bogo(struct seq_file *m)
  68. {
  69. int i;
  70. for_each_online_cpu(i)
  71. seq_printf(m,
  72. "Cpu%dClkTck\t: %016lx\n",
  73. i, cpu_data(i).clock_tick);
  74. }
  75. extern void setup_sparc64_timer(void);
  76. static volatile unsigned long callin_flag = 0;
  77. void smp_callin(void)
  78. {
  79. int cpuid = hard_smp_processor_id();
  80. __local_per_cpu_offset = __per_cpu_offset(cpuid);
  81. if (tlb_type == hypervisor)
  82. sun4v_ktsb_register();
  83. __flush_tlb_all();
  84. setup_sparc64_timer();
  85. if (cheetah_pcache_forced_on)
  86. cheetah_enable_pcache();
  87. callin_flag = 1;
  88. __asm__ __volatile__("membar #Sync\n\t"
  89. "flush %%g6" : : : "memory");
  90. /* Clear this or we will die instantly when we
  91. * schedule back to this idler...
  92. */
  93. current_thread_info()->new_child = 0;
  94. /* Attach to the address space of init_task. */
  95. atomic_inc(&init_mm.mm_count);
  96. current->active_mm = &init_mm;
  97. /* inform the notifiers about the new cpu */
  98. notify_cpu_starting(cpuid);
  99. while (!cpumask_test_cpu(cpuid, &smp_commenced_mask))
  100. rmb();
  101. set_cpu_online(cpuid, true);
  102. /* idle thread is expected to have preempt disabled */
  103. preempt_disable();
  104. local_irq_enable();
  105. cpu_startup_entry(CPUHP_ONLINE);
  106. }
  107. void cpu_panic(void)
  108. {
  109. printk("CPU[%d]: Returns from cpu_idle!\n", smp_processor_id());
  110. panic("SMP bolixed\n");
  111. }
  112. /* This tick register synchronization scheme is taken entirely from
  113. * the ia64 port, see arch/ia64/kernel/smpboot.c for details and credit.
  114. *
  115. * The only change I've made is to rework it so that the master
  116. * initiates the synchonization instead of the slave. -DaveM
  117. */
  118. #define MASTER 0
  119. #define SLAVE (SMP_CACHE_BYTES/sizeof(unsigned long))
  120. #define NUM_ROUNDS 64 /* magic value */
  121. #define NUM_ITERS 5 /* likewise */
  122. static DEFINE_RAW_SPINLOCK(itc_sync_lock);
  123. static unsigned long go[SLAVE + 1];
  124. #define DEBUG_TICK_SYNC 0
  125. static inline long get_delta (long *rt, long *master)
  126. {
  127. unsigned long best_t0 = 0, best_t1 = ~0UL, best_tm = 0;
  128. unsigned long tcenter, t0, t1, tm;
  129. unsigned long i;
  130. for (i = 0; i < NUM_ITERS; i++) {
  131. t0 = tick_ops->get_tick();
  132. go[MASTER] = 1;
  133. membar_safe("#StoreLoad");
  134. while (!(tm = go[SLAVE]))
  135. rmb();
  136. go[SLAVE] = 0;
  137. wmb();
  138. t1 = tick_ops->get_tick();
  139. if (t1 - t0 < best_t1 - best_t0)
  140. best_t0 = t0, best_t1 = t1, best_tm = tm;
  141. }
  142. *rt = best_t1 - best_t0;
  143. *master = best_tm - best_t0;
  144. /* average best_t0 and best_t1 without overflow: */
  145. tcenter = (best_t0/2 + best_t1/2);
  146. if (best_t0 % 2 + best_t1 % 2 == 2)
  147. tcenter++;
  148. return tcenter - best_tm;
  149. }
  150. void smp_synchronize_tick_client(void)
  151. {
  152. long i, delta, adj, adjust_latency = 0, done = 0;
  153. unsigned long flags, rt, master_time_stamp;
  154. #if DEBUG_TICK_SYNC
  155. struct {
  156. long rt; /* roundtrip time */
  157. long master; /* master's timestamp */
  158. long diff; /* difference between midpoint and master's timestamp */
  159. long lat; /* estimate of itc adjustment latency */
  160. } t[NUM_ROUNDS];
  161. #endif
  162. go[MASTER] = 1;
  163. while (go[MASTER])
  164. rmb();
  165. local_irq_save(flags);
  166. {
  167. for (i = 0; i < NUM_ROUNDS; i++) {
  168. delta = get_delta(&rt, &master_time_stamp);
  169. if (delta == 0)
  170. done = 1; /* let's lock on to this... */
  171. if (!done) {
  172. if (i > 0) {
  173. adjust_latency += -delta;
  174. adj = -delta + adjust_latency/4;
  175. } else
  176. adj = -delta;
  177. tick_ops->add_tick(adj);
  178. }
  179. #if DEBUG_TICK_SYNC
  180. t[i].rt = rt;
  181. t[i].master = master_time_stamp;
  182. t[i].diff = delta;
  183. t[i].lat = adjust_latency/4;
  184. #endif
  185. }
  186. }
  187. local_irq_restore(flags);
  188. #if DEBUG_TICK_SYNC
  189. for (i = 0; i < NUM_ROUNDS; i++)
  190. printk("rt=%5ld master=%5ld diff=%5ld adjlat=%5ld\n",
  191. t[i].rt, t[i].master, t[i].diff, t[i].lat);
  192. #endif
  193. printk(KERN_INFO "CPU %d: synchronized TICK with master CPU "
  194. "(last diff %ld cycles, maxerr %lu cycles)\n",
  195. smp_processor_id(), delta, rt);
  196. }
  197. static void smp_start_sync_tick_client(int cpu);
  198. static void smp_synchronize_one_tick(int cpu)
  199. {
  200. unsigned long flags, i;
  201. go[MASTER] = 0;
  202. smp_start_sync_tick_client(cpu);
  203. /* wait for client to be ready */
  204. while (!go[MASTER])
  205. rmb();
  206. /* now let the client proceed into his loop */
  207. go[MASTER] = 0;
  208. membar_safe("#StoreLoad");
  209. raw_spin_lock_irqsave(&itc_sync_lock, flags);
  210. {
  211. for (i = 0; i < NUM_ROUNDS*NUM_ITERS; i++) {
  212. while (!go[MASTER])
  213. rmb();
  214. go[MASTER] = 0;
  215. wmb();
  216. go[SLAVE] = tick_ops->get_tick();
  217. membar_safe("#StoreLoad");
  218. }
  219. }
  220. raw_spin_unlock_irqrestore(&itc_sync_lock, flags);
  221. }
  222. #if defined(CONFIG_SUN_LDOMS) && defined(CONFIG_HOTPLUG_CPU)
  223. static void ldom_startcpu_cpuid(unsigned int cpu, unsigned long thread_reg,
  224. void **descrp)
  225. {
  226. extern unsigned long sparc64_ttable_tl0;
  227. extern unsigned long kern_locked_tte_data;
  228. struct hvtramp_descr *hdesc;
  229. unsigned long trampoline_ra;
  230. struct trap_per_cpu *tb;
  231. u64 tte_vaddr, tte_data;
  232. unsigned long hv_err;
  233. int i;
  234. hdesc = kzalloc(sizeof(*hdesc) +
  235. (sizeof(struct hvtramp_mapping) *
  236. num_kernel_image_mappings - 1),
  237. GFP_KERNEL);
  238. if (!hdesc) {
  239. printk(KERN_ERR "ldom_startcpu_cpuid: Cannot allocate "
  240. "hvtramp_descr.\n");
  241. return;
  242. }
  243. *descrp = hdesc;
  244. hdesc->cpu = cpu;
  245. hdesc->num_mappings = num_kernel_image_mappings;
  246. tb = &trap_block[cpu];
  247. hdesc->fault_info_va = (unsigned long) &tb->fault_info;
  248. hdesc->fault_info_pa = kimage_addr_to_ra(&tb->fault_info);
  249. hdesc->thread_reg = thread_reg;
  250. tte_vaddr = (unsigned long) KERNBASE;
  251. tte_data = kern_locked_tte_data;
  252. for (i = 0; i < hdesc->num_mappings; i++) {
  253. hdesc->maps[i].vaddr = tte_vaddr;
  254. hdesc->maps[i].tte = tte_data;
  255. tte_vaddr += 0x400000;
  256. tte_data += 0x400000;
  257. }
  258. trampoline_ra = kimage_addr_to_ra(hv_cpu_startup);
  259. hv_err = sun4v_cpu_start(cpu, trampoline_ra,
  260. kimage_addr_to_ra(&sparc64_ttable_tl0),
  261. __pa(hdesc));
  262. if (hv_err)
  263. printk(KERN_ERR "ldom_startcpu_cpuid: sun4v_cpu_start() "
  264. "gives error %lu\n", hv_err);
  265. }
  266. #endif
  267. extern unsigned long sparc64_cpu_startup;
  268. /* The OBP cpu startup callback truncates the 3rd arg cookie to
  269. * 32-bits (I think) so to be safe we have it read the pointer
  270. * contained here so we work on >4GB machines. -DaveM
  271. */
  272. static struct thread_info *cpu_new_thread = NULL;
  273. static int smp_boot_one_cpu(unsigned int cpu, struct task_struct *idle)
  274. {
  275. unsigned long entry =
  276. (unsigned long)(&sparc64_cpu_startup);
  277. unsigned long cookie =
  278. (unsigned long)(&cpu_new_thread);
  279. void *descr = NULL;
  280. int timeout, ret;
  281. callin_flag = 0;
  282. cpu_new_thread = task_thread_info(idle);
  283. if (tlb_type == hypervisor) {
  284. #if defined(CONFIG_SUN_LDOMS) && defined(CONFIG_HOTPLUG_CPU)
  285. if (ldom_domaining_enabled)
  286. ldom_startcpu_cpuid(cpu,
  287. (unsigned long) cpu_new_thread,
  288. &descr);
  289. else
  290. #endif
  291. prom_startcpu_cpuid(cpu, entry, cookie);
  292. } else {
  293. struct device_node *dp = of_find_node_by_cpuid(cpu);
  294. prom_startcpu(dp->phandle, entry, cookie);
  295. }
  296. for (timeout = 0; timeout < 50000; timeout++) {
  297. if (callin_flag)
  298. break;
  299. udelay(100);
  300. }
  301. if (callin_flag) {
  302. ret = 0;
  303. } else {
  304. printk("Processor %d is stuck.\n", cpu);
  305. ret = -ENODEV;
  306. }
  307. cpu_new_thread = NULL;
  308. kfree(descr);
  309. return ret;
  310. }
  311. static void spitfire_xcall_helper(u64 data0, u64 data1, u64 data2, u64 pstate, unsigned long cpu)
  312. {
  313. u64 result, target;
  314. int stuck, tmp;
  315. if (this_is_starfire) {
  316. /* map to real upaid */
  317. cpu = (((cpu & 0x3c) << 1) |
  318. ((cpu & 0x40) >> 4) |
  319. (cpu & 0x3));
  320. }
  321. target = (cpu << 14) | 0x70;
  322. again:
  323. /* Ok, this is the real Spitfire Errata #54.
  324. * One must read back from a UDB internal register
  325. * after writes to the UDB interrupt dispatch, but
  326. * before the membar Sync for that write.
  327. * So we use the high UDB control register (ASI 0x7f,
  328. * ADDR 0x20) for the dummy read. -DaveM
  329. */
  330. tmp = 0x40;
  331. __asm__ __volatile__(
  332. "wrpr %1, %2, %%pstate\n\t"
  333. "stxa %4, [%0] %3\n\t"
  334. "stxa %5, [%0+%8] %3\n\t"
  335. "add %0, %8, %0\n\t"
  336. "stxa %6, [%0+%8] %3\n\t"
  337. "membar #Sync\n\t"
  338. "stxa %%g0, [%7] %3\n\t"
  339. "membar #Sync\n\t"
  340. "mov 0x20, %%g1\n\t"
  341. "ldxa [%%g1] 0x7f, %%g0\n\t"
  342. "membar #Sync"
  343. : "=r" (tmp)
  344. : "r" (pstate), "i" (PSTATE_IE), "i" (ASI_INTR_W),
  345. "r" (data0), "r" (data1), "r" (data2), "r" (target),
  346. "r" (0x10), "0" (tmp)
  347. : "g1");
  348. /* NOTE: PSTATE_IE is still clear. */
  349. stuck = 100000;
  350. do {
  351. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  352. : "=r" (result)
  353. : "i" (ASI_INTR_DISPATCH_STAT));
  354. if (result == 0) {
  355. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  356. : : "r" (pstate));
  357. return;
  358. }
  359. stuck -= 1;
  360. if (stuck == 0)
  361. break;
  362. } while (result & 0x1);
  363. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  364. : : "r" (pstate));
  365. if (stuck == 0) {
  366. printk("CPU[%d]: mondo stuckage result[%016llx]\n",
  367. smp_processor_id(), result);
  368. } else {
  369. udelay(2);
  370. goto again;
  371. }
  372. }
  373. static void spitfire_xcall_deliver(struct trap_per_cpu *tb, int cnt)
  374. {
  375. u64 *mondo, data0, data1, data2;
  376. u16 *cpu_list;
  377. u64 pstate;
  378. int i;
  379. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  380. cpu_list = __va(tb->cpu_list_pa);
  381. mondo = __va(tb->cpu_mondo_block_pa);
  382. data0 = mondo[0];
  383. data1 = mondo[1];
  384. data2 = mondo[2];
  385. for (i = 0; i < cnt; i++)
  386. spitfire_xcall_helper(data0, data1, data2, pstate, cpu_list[i]);
  387. }
  388. /* Cheetah now allows to send the whole 64-bytes of data in the interrupt
  389. * packet, but we have no use for that. However we do take advantage of
  390. * the new pipelining feature (ie. dispatch to multiple cpus simultaneously).
  391. */
  392. static void cheetah_xcall_deliver(struct trap_per_cpu *tb, int cnt)
  393. {
  394. int nack_busy_id, is_jbus, need_more;
  395. u64 *mondo, pstate, ver, busy_mask;
  396. u16 *cpu_list;
  397. cpu_list = __va(tb->cpu_list_pa);
  398. mondo = __va(tb->cpu_mondo_block_pa);
  399. /* Unfortunately, someone at Sun had the brilliant idea to make the
  400. * busy/nack fields hard-coded by ITID number for this Ultra-III
  401. * derivative processor.
  402. */
  403. __asm__ ("rdpr %%ver, %0" : "=r" (ver));
  404. is_jbus = ((ver >> 32) == __JALAPENO_ID ||
  405. (ver >> 32) == __SERRANO_ID);
  406. __asm__ __volatile__("rdpr %%pstate, %0" : "=r" (pstate));
  407. retry:
  408. need_more = 0;
  409. __asm__ __volatile__("wrpr %0, %1, %%pstate\n\t"
  410. : : "r" (pstate), "i" (PSTATE_IE));
  411. /* Setup the dispatch data registers. */
  412. __asm__ __volatile__("stxa %0, [%3] %6\n\t"
  413. "stxa %1, [%4] %6\n\t"
  414. "stxa %2, [%5] %6\n\t"
  415. "membar #Sync\n\t"
  416. : /* no outputs */
  417. : "r" (mondo[0]), "r" (mondo[1]), "r" (mondo[2]),
  418. "r" (0x40), "r" (0x50), "r" (0x60),
  419. "i" (ASI_INTR_W));
  420. nack_busy_id = 0;
  421. busy_mask = 0;
  422. {
  423. int i;
  424. for (i = 0; i < cnt; i++) {
  425. u64 target, nr;
  426. nr = cpu_list[i];
  427. if (nr == 0xffff)
  428. continue;
  429. target = (nr << 14) | 0x70;
  430. if (is_jbus) {
  431. busy_mask |= (0x1UL << (nr * 2));
  432. } else {
  433. target |= (nack_busy_id << 24);
  434. busy_mask |= (0x1UL <<
  435. (nack_busy_id * 2));
  436. }
  437. __asm__ __volatile__(
  438. "stxa %%g0, [%0] %1\n\t"
  439. "membar #Sync\n\t"
  440. : /* no outputs */
  441. : "r" (target), "i" (ASI_INTR_W));
  442. nack_busy_id++;
  443. if (nack_busy_id == 32) {
  444. need_more = 1;
  445. break;
  446. }
  447. }
  448. }
  449. /* Now, poll for completion. */
  450. {
  451. u64 dispatch_stat, nack_mask;
  452. long stuck;
  453. stuck = 100000 * nack_busy_id;
  454. nack_mask = busy_mask << 1;
  455. do {
  456. __asm__ __volatile__("ldxa [%%g0] %1, %0"
  457. : "=r" (dispatch_stat)
  458. : "i" (ASI_INTR_DISPATCH_STAT));
  459. if (!(dispatch_stat & (busy_mask | nack_mask))) {
  460. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  461. : : "r" (pstate));
  462. if (unlikely(need_more)) {
  463. int i, this_cnt = 0;
  464. for (i = 0; i < cnt; i++) {
  465. if (cpu_list[i] == 0xffff)
  466. continue;
  467. cpu_list[i] = 0xffff;
  468. this_cnt++;
  469. if (this_cnt == 32)
  470. break;
  471. }
  472. goto retry;
  473. }
  474. return;
  475. }
  476. if (!--stuck)
  477. break;
  478. } while (dispatch_stat & busy_mask);
  479. __asm__ __volatile__("wrpr %0, 0x0, %%pstate"
  480. : : "r" (pstate));
  481. if (dispatch_stat & busy_mask) {
  482. /* Busy bits will not clear, continue instead
  483. * of freezing up on this cpu.
  484. */
  485. printk("CPU[%d]: mondo stuckage result[%016llx]\n",
  486. smp_processor_id(), dispatch_stat);
  487. } else {
  488. int i, this_busy_nack = 0;
  489. /* Delay some random time with interrupts enabled
  490. * to prevent deadlock.
  491. */
  492. udelay(2 * nack_busy_id);
  493. /* Clear out the mask bits for cpus which did not
  494. * NACK us.
  495. */
  496. for (i = 0; i < cnt; i++) {
  497. u64 check_mask, nr;
  498. nr = cpu_list[i];
  499. if (nr == 0xffff)
  500. continue;
  501. if (is_jbus)
  502. check_mask = (0x2UL << (2*nr));
  503. else
  504. check_mask = (0x2UL <<
  505. this_busy_nack);
  506. if ((dispatch_stat & check_mask) == 0)
  507. cpu_list[i] = 0xffff;
  508. this_busy_nack += 2;
  509. if (this_busy_nack == 64)
  510. break;
  511. }
  512. goto retry;
  513. }
  514. }
  515. }
  516. /* Multi-cpu list version. */
  517. static void hypervisor_xcall_deliver(struct trap_per_cpu *tb, int cnt)
  518. {
  519. int retries, this_cpu, prev_sent, i, saw_cpu_error;
  520. unsigned long status;
  521. u16 *cpu_list;
  522. this_cpu = smp_processor_id();
  523. cpu_list = __va(tb->cpu_list_pa);
  524. saw_cpu_error = 0;
  525. retries = 0;
  526. prev_sent = 0;
  527. do {
  528. int forward_progress, n_sent;
  529. status = sun4v_cpu_mondo_send(cnt,
  530. tb->cpu_list_pa,
  531. tb->cpu_mondo_block_pa);
  532. /* HV_EOK means all cpus received the xcall, we're done. */
  533. if (likely(status == HV_EOK))
  534. break;
  535. /* First, see if we made any forward progress.
  536. *
  537. * The hypervisor indicates successful sends by setting
  538. * cpu list entries to the value 0xffff.
  539. */
  540. n_sent = 0;
  541. for (i = 0; i < cnt; i++) {
  542. if (likely(cpu_list[i] == 0xffff))
  543. n_sent++;
  544. }
  545. forward_progress = 0;
  546. if (n_sent > prev_sent)
  547. forward_progress = 1;
  548. prev_sent = n_sent;
  549. /* If we get a HV_ECPUERROR, then one or more of the cpus
  550. * in the list are in error state. Use the cpu_state()
  551. * hypervisor call to find out which cpus are in error state.
  552. */
  553. if (unlikely(status == HV_ECPUERROR)) {
  554. for (i = 0; i < cnt; i++) {
  555. long err;
  556. u16 cpu;
  557. cpu = cpu_list[i];
  558. if (cpu == 0xffff)
  559. continue;
  560. err = sun4v_cpu_state(cpu);
  561. if (err == HV_CPU_STATE_ERROR) {
  562. saw_cpu_error = (cpu + 1);
  563. cpu_list[i] = 0xffff;
  564. }
  565. }
  566. } else if (unlikely(status != HV_EWOULDBLOCK))
  567. goto fatal_mondo_error;
  568. /* Don't bother rewriting the CPU list, just leave the
  569. * 0xffff and non-0xffff entries in there and the
  570. * hypervisor will do the right thing.
  571. *
  572. * Only advance timeout state if we didn't make any
  573. * forward progress.
  574. */
  575. if (unlikely(!forward_progress)) {
  576. if (unlikely(++retries > 10000))
  577. goto fatal_mondo_timeout;
  578. /* Delay a little bit to let other cpus catch up
  579. * on their cpu mondo queue work.
  580. */
  581. udelay(2 * cnt);
  582. }
  583. } while (1);
  584. if (unlikely(saw_cpu_error))
  585. goto fatal_mondo_cpu_error;
  586. return;
  587. fatal_mondo_cpu_error:
  588. printk(KERN_CRIT "CPU[%d]: SUN4V mondo cpu error, some target cpus "
  589. "(including %d) were in error state\n",
  590. this_cpu, saw_cpu_error - 1);
  591. return;
  592. fatal_mondo_timeout:
  593. printk(KERN_CRIT "CPU[%d]: SUN4V mondo timeout, no forward "
  594. " progress after %d retries.\n",
  595. this_cpu, retries);
  596. goto dump_cpu_list_and_out;
  597. fatal_mondo_error:
  598. printk(KERN_CRIT "CPU[%d]: Unexpected SUN4V mondo error %lu\n",
  599. this_cpu, status);
  600. printk(KERN_CRIT "CPU[%d]: Args were cnt(%d) cpulist_pa(%lx) "
  601. "mondo_block_pa(%lx)\n",
  602. this_cpu, cnt, tb->cpu_list_pa, tb->cpu_mondo_block_pa);
  603. dump_cpu_list_and_out:
  604. printk(KERN_CRIT "CPU[%d]: CPU list [ ", this_cpu);
  605. for (i = 0; i < cnt; i++)
  606. printk("%u ", cpu_list[i]);
  607. printk("]\n");
  608. }
  609. static void (*xcall_deliver_impl)(struct trap_per_cpu *, int);
  610. static void xcall_deliver(u64 data0, u64 data1, u64 data2, const cpumask_t *mask)
  611. {
  612. struct trap_per_cpu *tb;
  613. int this_cpu, i, cnt;
  614. unsigned long flags;
  615. u16 *cpu_list;
  616. u64 *mondo;
  617. /* We have to do this whole thing with interrupts fully disabled.
  618. * Otherwise if we send an xcall from interrupt context it will
  619. * corrupt both our mondo block and cpu list state.
  620. *
  621. * One consequence of this is that we cannot use timeout mechanisms
  622. * that depend upon interrupts being delivered locally. So, for
  623. * example, we cannot sample jiffies and expect it to advance.
  624. *
  625. * Fortunately, udelay() uses %stick/%tick so we can use that.
  626. */
  627. local_irq_save(flags);
  628. this_cpu = smp_processor_id();
  629. tb = &trap_block[this_cpu];
  630. mondo = __va(tb->cpu_mondo_block_pa);
  631. mondo[0] = data0;
  632. mondo[1] = data1;
  633. mondo[2] = data2;
  634. wmb();
  635. cpu_list = __va(tb->cpu_list_pa);
  636. /* Setup the initial cpu list. */
  637. cnt = 0;
  638. for_each_cpu(i, mask) {
  639. if (i == this_cpu || !cpu_online(i))
  640. continue;
  641. cpu_list[cnt++] = i;
  642. }
  643. if (cnt)
  644. xcall_deliver_impl(tb, cnt);
  645. local_irq_restore(flags);
  646. }
  647. /* Send cross call to all processors mentioned in MASK_P
  648. * except self. Really, there are only two cases currently,
  649. * "cpu_online_mask" and "mm_cpumask(mm)".
  650. */
  651. static void smp_cross_call_masked(unsigned long *func, u32 ctx, u64 data1, u64 data2, const cpumask_t *mask)
  652. {
  653. u64 data0 = (((u64)ctx)<<32 | (((u64)func) & 0xffffffff));
  654. xcall_deliver(data0, data1, data2, mask);
  655. }
  656. /* Send cross call to all processors except self. */
  657. static void smp_cross_call(unsigned long *func, u32 ctx, u64 data1, u64 data2)
  658. {
  659. smp_cross_call_masked(func, ctx, data1, data2, cpu_online_mask);
  660. }
  661. extern unsigned long xcall_sync_tick;
  662. static void smp_start_sync_tick_client(int cpu)
  663. {
  664. xcall_deliver((u64) &xcall_sync_tick, 0, 0,
  665. cpumask_of(cpu));
  666. }
  667. extern unsigned long xcall_call_function;
  668. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  669. {
  670. xcall_deliver((u64) &xcall_call_function, 0, 0, mask);
  671. }
  672. extern unsigned long xcall_call_function_single;
  673. void arch_send_call_function_single_ipi(int cpu)
  674. {
  675. xcall_deliver((u64) &xcall_call_function_single, 0, 0,
  676. cpumask_of(cpu));
  677. }
  678. void __irq_entry smp_call_function_client(int irq, struct pt_regs *regs)
  679. {
  680. clear_softint(1 << irq);
  681. irq_enter();
  682. generic_smp_call_function_interrupt();
  683. irq_exit();
  684. }
  685. void __irq_entry smp_call_function_single_client(int irq, struct pt_regs *regs)
  686. {
  687. clear_softint(1 << irq);
  688. irq_enter();
  689. generic_smp_call_function_single_interrupt();
  690. irq_exit();
  691. }
  692. static void tsb_sync(void *info)
  693. {
  694. struct trap_per_cpu *tp = &trap_block[raw_smp_processor_id()];
  695. struct mm_struct *mm = info;
  696. /* It is not valid to test "current->active_mm == mm" here.
  697. *
  698. * The value of "current" is not changed atomically with
  699. * switch_mm(). But that's OK, we just need to check the
  700. * current cpu's trap block PGD physical address.
  701. */
  702. if (tp->pgd_paddr == __pa(mm->pgd))
  703. tsb_context_switch(mm);
  704. }
  705. void smp_tsb_sync(struct mm_struct *mm)
  706. {
  707. smp_call_function_many(mm_cpumask(mm), tsb_sync, mm, 1);
  708. }
  709. extern unsigned long xcall_flush_tlb_mm;
  710. extern unsigned long xcall_flush_tlb_page;
  711. extern unsigned long xcall_flush_tlb_kernel_range;
  712. extern unsigned long xcall_fetch_glob_regs;
  713. extern unsigned long xcall_fetch_glob_pmu;
  714. extern unsigned long xcall_fetch_glob_pmu_n4;
  715. extern unsigned long xcall_receive_signal;
  716. extern unsigned long xcall_new_mmu_context_version;
  717. #ifdef CONFIG_KGDB
  718. extern unsigned long xcall_kgdb_capture;
  719. #endif
  720. #ifdef DCACHE_ALIASING_POSSIBLE
  721. extern unsigned long xcall_flush_dcache_page_cheetah;
  722. #endif
  723. extern unsigned long xcall_flush_dcache_page_spitfire;
  724. static inline void __local_flush_dcache_page(struct page *page)
  725. {
  726. #ifdef DCACHE_ALIASING_POSSIBLE
  727. __flush_dcache_page(page_address(page),
  728. ((tlb_type == spitfire) &&
  729. page_mapping(page) != NULL));
  730. #else
  731. if (page_mapping(page) != NULL &&
  732. tlb_type == spitfire)
  733. __flush_icache_page(__pa(page_address(page)));
  734. #endif
  735. }
  736. void smp_flush_dcache_page_impl(struct page *page, int cpu)
  737. {
  738. int this_cpu;
  739. if (tlb_type == hypervisor)
  740. return;
  741. #ifdef CONFIG_DEBUG_DCFLUSH
  742. atomic_inc(&dcpage_flushes);
  743. #endif
  744. this_cpu = get_cpu();
  745. if (cpu == this_cpu) {
  746. __local_flush_dcache_page(page);
  747. } else if (cpu_online(cpu)) {
  748. void *pg_addr = page_address(page);
  749. u64 data0 = 0;
  750. if (tlb_type == spitfire) {
  751. data0 = ((u64)&xcall_flush_dcache_page_spitfire);
  752. if (page_mapping(page) != NULL)
  753. data0 |= ((u64)1 << 32);
  754. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  755. #ifdef DCACHE_ALIASING_POSSIBLE
  756. data0 = ((u64)&xcall_flush_dcache_page_cheetah);
  757. #endif
  758. }
  759. if (data0) {
  760. xcall_deliver(data0, __pa(pg_addr),
  761. (u64) pg_addr, cpumask_of(cpu));
  762. #ifdef CONFIG_DEBUG_DCFLUSH
  763. atomic_inc(&dcpage_flushes_xcall);
  764. #endif
  765. }
  766. }
  767. put_cpu();
  768. }
  769. void flush_dcache_page_all(struct mm_struct *mm, struct page *page)
  770. {
  771. void *pg_addr;
  772. u64 data0;
  773. if (tlb_type == hypervisor)
  774. return;
  775. preempt_disable();
  776. #ifdef CONFIG_DEBUG_DCFLUSH
  777. atomic_inc(&dcpage_flushes);
  778. #endif
  779. data0 = 0;
  780. pg_addr = page_address(page);
  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. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  786. #ifdef DCACHE_ALIASING_POSSIBLE
  787. data0 = ((u64)&xcall_flush_dcache_page_cheetah);
  788. #endif
  789. }
  790. if (data0) {
  791. xcall_deliver(data0, __pa(pg_addr),
  792. (u64) pg_addr, cpu_online_mask);
  793. #ifdef CONFIG_DEBUG_DCFLUSH
  794. atomic_inc(&dcpage_flushes_xcall);
  795. #endif
  796. }
  797. __local_flush_dcache_page(page);
  798. preempt_enable();
  799. }
  800. void __irq_entry smp_new_mmu_context_version_client(int irq, struct pt_regs *regs)
  801. {
  802. struct mm_struct *mm;
  803. unsigned long flags;
  804. clear_softint(1 << irq);
  805. /* See if we need to allocate a new TLB context because
  806. * the version of the one we are using is now out of date.
  807. */
  808. mm = current->active_mm;
  809. if (unlikely(!mm || (mm == &init_mm)))
  810. return;
  811. spin_lock_irqsave(&mm->context.lock, flags);
  812. if (unlikely(!CTX_VALID(mm->context)))
  813. get_new_mmu_context(mm);
  814. spin_unlock_irqrestore(&mm->context.lock, flags);
  815. load_secondary_context(mm);
  816. __flush_tlb_mm(CTX_HWBITS(mm->context),
  817. SECONDARY_CONTEXT);
  818. }
  819. void smp_new_mmu_context_version(void)
  820. {
  821. smp_cross_call(&xcall_new_mmu_context_version, 0, 0, 0);
  822. }
  823. #ifdef CONFIG_KGDB
  824. void kgdb_roundup_cpus(unsigned long flags)
  825. {
  826. smp_cross_call(&xcall_kgdb_capture, 0, 0, 0);
  827. }
  828. #endif
  829. void smp_fetch_global_regs(void)
  830. {
  831. smp_cross_call(&xcall_fetch_glob_regs, 0, 0, 0);
  832. }
  833. void smp_fetch_global_pmu(void)
  834. {
  835. if (tlb_type == hypervisor &&
  836. sun4v_chip_type >= SUN4V_CHIP_NIAGARA4)
  837. smp_cross_call(&xcall_fetch_glob_pmu_n4, 0, 0, 0);
  838. else
  839. smp_cross_call(&xcall_fetch_glob_pmu, 0, 0, 0);
  840. }
  841. /* We know that the window frames of the user have been flushed
  842. * to the stack before we get here because all callers of us
  843. * are flush_tlb_*() routines, and these run after flush_cache_*()
  844. * which performs the flushw.
  845. *
  846. * The SMP TLB coherency scheme we use works as follows:
  847. *
  848. * 1) mm->cpu_vm_mask is a bit mask of which cpus an address
  849. * space has (potentially) executed on, this is the heuristic
  850. * we use to avoid doing cross calls.
  851. *
  852. * Also, for flushing from kswapd and also for clones, we
  853. * use cpu_vm_mask as the list of cpus to make run the TLB.
  854. *
  855. * 2) TLB context numbers are shared globally across all processors
  856. * in the system, this allows us to play several games to avoid
  857. * cross calls.
  858. *
  859. * One invariant is that when a cpu switches to a process, and
  860. * that processes tsk->active_mm->cpu_vm_mask does not have the
  861. * current cpu's bit set, that tlb context is flushed locally.
  862. *
  863. * If the address space is non-shared (ie. mm->count == 1) we avoid
  864. * cross calls when we want to flush the currently running process's
  865. * tlb state. This is done by clearing all cpu bits except the current
  866. * processor's in current->mm->cpu_vm_mask and performing the
  867. * flush locally only. This will force any subsequent cpus which run
  868. * this task to flush the context from the local tlb if the process
  869. * migrates to another cpu (again).
  870. *
  871. * 3) For shared address spaces (threads) and swapping we bite the
  872. * bullet for most cases and perform the cross call (but only to
  873. * the cpus listed in cpu_vm_mask).
  874. *
  875. * The performance gain from "optimizing" away the cross call for threads is
  876. * questionable (in theory the big win for threads is the massive sharing of
  877. * address space state across processors).
  878. */
  879. /* This currently is only used by the hugetlb arch pre-fault
  880. * hook on UltraSPARC-III+ and later when changing the pagesize
  881. * bits of the context register for an address space.
  882. */
  883. void smp_flush_tlb_mm(struct mm_struct *mm)
  884. {
  885. u32 ctx = CTX_HWBITS(mm->context);
  886. int cpu = get_cpu();
  887. if (atomic_read(&mm->mm_users) == 1) {
  888. cpumask_copy(mm_cpumask(mm), cpumask_of(cpu));
  889. goto local_flush_and_out;
  890. }
  891. smp_cross_call_masked(&xcall_flush_tlb_mm,
  892. ctx, 0, 0,
  893. mm_cpumask(mm));
  894. local_flush_and_out:
  895. __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
  896. put_cpu();
  897. }
  898. struct tlb_pending_info {
  899. unsigned long ctx;
  900. unsigned long nr;
  901. unsigned long *vaddrs;
  902. };
  903. static void tlb_pending_func(void *info)
  904. {
  905. struct tlb_pending_info *t = info;
  906. __flush_tlb_pending(t->ctx, t->nr, t->vaddrs);
  907. }
  908. void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs)
  909. {
  910. u32 ctx = CTX_HWBITS(mm->context);
  911. struct tlb_pending_info info;
  912. int cpu = get_cpu();
  913. info.ctx = ctx;
  914. info.nr = nr;
  915. info.vaddrs = vaddrs;
  916. if (mm == current->mm && atomic_read(&mm->mm_users) == 1)
  917. cpumask_copy(mm_cpumask(mm), cpumask_of(cpu));
  918. else
  919. smp_call_function_many(mm_cpumask(mm), tlb_pending_func,
  920. &info, 1);
  921. __flush_tlb_pending(ctx, nr, vaddrs);
  922. put_cpu();
  923. }
  924. void smp_flush_tlb_page(struct mm_struct *mm, unsigned long vaddr)
  925. {
  926. unsigned long context = CTX_HWBITS(mm->context);
  927. int cpu = get_cpu();
  928. if (mm == current->mm && atomic_read(&mm->mm_users) == 1)
  929. cpumask_copy(mm_cpumask(mm), cpumask_of(cpu));
  930. else
  931. smp_cross_call_masked(&xcall_flush_tlb_page,
  932. context, vaddr, 0,
  933. mm_cpumask(mm));
  934. __flush_tlb_page(context, vaddr);
  935. put_cpu();
  936. }
  937. void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  938. {
  939. start &= PAGE_MASK;
  940. end = PAGE_ALIGN(end);
  941. if (start != end) {
  942. smp_cross_call(&xcall_flush_tlb_kernel_range,
  943. 0, start, end);
  944. __flush_tlb_kernel_range(start, end);
  945. }
  946. }
  947. /* CPU capture. */
  948. /* #define CAPTURE_DEBUG */
  949. extern unsigned long xcall_capture;
  950. static atomic_t smp_capture_depth = ATOMIC_INIT(0);
  951. static atomic_t smp_capture_registry = ATOMIC_INIT(0);
  952. static unsigned long penguins_are_doing_time;
  953. void smp_capture(void)
  954. {
  955. int result = atomic_add_return(1, &smp_capture_depth);
  956. if (result == 1) {
  957. int ncpus = num_online_cpus();
  958. #ifdef CAPTURE_DEBUG
  959. printk("CPU[%d]: Sending penguins to jail...",
  960. smp_processor_id());
  961. #endif
  962. penguins_are_doing_time = 1;
  963. atomic_inc(&smp_capture_registry);
  964. smp_cross_call(&xcall_capture, 0, 0, 0);
  965. while (atomic_read(&smp_capture_registry) != ncpus)
  966. rmb();
  967. #ifdef CAPTURE_DEBUG
  968. printk("done\n");
  969. #endif
  970. }
  971. }
  972. void smp_release(void)
  973. {
  974. if (atomic_dec_and_test(&smp_capture_depth)) {
  975. #ifdef CAPTURE_DEBUG
  976. printk("CPU[%d]: Giving pardon to "
  977. "imprisoned penguins\n",
  978. smp_processor_id());
  979. #endif
  980. penguins_are_doing_time = 0;
  981. membar_safe("#StoreLoad");
  982. atomic_dec(&smp_capture_registry);
  983. }
  984. }
  985. /* Imprisoned penguins run with %pil == PIL_NORMAL_MAX, but PSTATE_IE
  986. * set, so they can service tlb flush xcalls...
  987. */
  988. extern void prom_world(int);
  989. void __irq_entry smp_penguin_jailcell(int irq, struct pt_regs *regs)
  990. {
  991. clear_softint(1 << irq);
  992. preempt_disable();
  993. __asm__ __volatile__("flushw");
  994. prom_world(1);
  995. atomic_inc(&smp_capture_registry);
  996. membar_safe("#StoreLoad");
  997. while (penguins_are_doing_time)
  998. rmb();
  999. atomic_dec(&smp_capture_registry);
  1000. prom_world(0);
  1001. preempt_enable();
  1002. }
  1003. /* /proc/profile writes can call this, don't __init it please. */
  1004. int setup_profiling_timer(unsigned int multiplier)
  1005. {
  1006. return -EINVAL;
  1007. }
  1008. void __init smp_prepare_cpus(unsigned int max_cpus)
  1009. {
  1010. }
  1011. void smp_prepare_boot_cpu(void)
  1012. {
  1013. }
  1014. void __init smp_setup_processor_id(void)
  1015. {
  1016. if (tlb_type == spitfire)
  1017. xcall_deliver_impl = spitfire_xcall_deliver;
  1018. else if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1019. xcall_deliver_impl = cheetah_xcall_deliver;
  1020. else
  1021. xcall_deliver_impl = hypervisor_xcall_deliver;
  1022. }
  1023. void smp_fill_in_sib_core_maps(void)
  1024. {
  1025. unsigned int i;
  1026. for_each_present_cpu(i) {
  1027. unsigned int j;
  1028. cpumask_clear(&cpu_core_map[i]);
  1029. if (cpu_data(i).core_id == 0) {
  1030. cpumask_set_cpu(i, &cpu_core_map[i]);
  1031. continue;
  1032. }
  1033. for_each_present_cpu(j) {
  1034. if (cpu_data(i).core_id ==
  1035. cpu_data(j).core_id)
  1036. cpumask_set_cpu(j, &cpu_core_map[i]);
  1037. }
  1038. }
  1039. for_each_present_cpu(i) {
  1040. unsigned int j;
  1041. cpumask_clear(&per_cpu(cpu_sibling_map, i));
  1042. if (cpu_data(i).proc_id == -1) {
  1043. cpumask_set_cpu(i, &per_cpu(cpu_sibling_map, i));
  1044. continue;
  1045. }
  1046. for_each_present_cpu(j) {
  1047. if (cpu_data(i).proc_id ==
  1048. cpu_data(j).proc_id)
  1049. cpumask_set_cpu(j, &per_cpu(cpu_sibling_map, i));
  1050. }
  1051. }
  1052. }
  1053. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  1054. {
  1055. int ret = smp_boot_one_cpu(cpu, tidle);
  1056. if (!ret) {
  1057. cpumask_set_cpu(cpu, &smp_commenced_mask);
  1058. while (!cpu_online(cpu))
  1059. mb();
  1060. if (!cpu_online(cpu)) {
  1061. ret = -ENODEV;
  1062. } else {
  1063. /* On SUN4V, writes to %tick and %stick are
  1064. * not allowed.
  1065. */
  1066. if (tlb_type != hypervisor)
  1067. smp_synchronize_one_tick(cpu);
  1068. }
  1069. }
  1070. return ret;
  1071. }
  1072. #ifdef CONFIG_HOTPLUG_CPU
  1073. void cpu_play_dead(void)
  1074. {
  1075. int cpu = smp_processor_id();
  1076. unsigned long pstate;
  1077. idle_task_exit();
  1078. if (tlb_type == hypervisor) {
  1079. struct trap_per_cpu *tb = &trap_block[cpu];
  1080. sun4v_cpu_qconf(HV_CPU_QUEUE_CPU_MONDO,
  1081. tb->cpu_mondo_pa, 0);
  1082. sun4v_cpu_qconf(HV_CPU_QUEUE_DEVICE_MONDO,
  1083. tb->dev_mondo_pa, 0);
  1084. sun4v_cpu_qconf(HV_CPU_QUEUE_RES_ERROR,
  1085. tb->resum_mondo_pa, 0);
  1086. sun4v_cpu_qconf(HV_CPU_QUEUE_NONRES_ERROR,
  1087. tb->nonresum_mondo_pa, 0);
  1088. }
  1089. cpumask_clear_cpu(cpu, &smp_commenced_mask);
  1090. membar_safe("#Sync");
  1091. local_irq_disable();
  1092. __asm__ __volatile__(
  1093. "rdpr %%pstate, %0\n\t"
  1094. "wrpr %0, %1, %%pstate"
  1095. : "=r" (pstate)
  1096. : "i" (PSTATE_IE));
  1097. while (1)
  1098. barrier();
  1099. }
  1100. int __cpu_disable(void)
  1101. {
  1102. int cpu = smp_processor_id();
  1103. cpuinfo_sparc *c;
  1104. int i;
  1105. for_each_cpu(i, &cpu_core_map[cpu])
  1106. cpumask_clear_cpu(cpu, &cpu_core_map[i]);
  1107. cpumask_clear(&cpu_core_map[cpu]);
  1108. for_each_cpu(i, &per_cpu(cpu_sibling_map, cpu))
  1109. cpumask_clear_cpu(cpu, &per_cpu(cpu_sibling_map, i));
  1110. cpumask_clear(&per_cpu(cpu_sibling_map, cpu));
  1111. c = &cpu_data(cpu);
  1112. c->core_id = 0;
  1113. c->proc_id = -1;
  1114. smp_wmb();
  1115. /* Make sure no interrupts point to this cpu. */
  1116. fixup_irqs();
  1117. local_irq_enable();
  1118. mdelay(1);
  1119. local_irq_disable();
  1120. set_cpu_online(cpu, false);
  1121. cpu_map_rebuild();
  1122. return 0;
  1123. }
  1124. void __cpu_die(unsigned int cpu)
  1125. {
  1126. int i;
  1127. for (i = 0; i < 100; i++) {
  1128. smp_rmb();
  1129. if (!cpumask_test_cpu(cpu, &smp_commenced_mask))
  1130. break;
  1131. msleep(100);
  1132. }
  1133. if (cpumask_test_cpu(cpu, &smp_commenced_mask)) {
  1134. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  1135. } else {
  1136. #if defined(CONFIG_SUN_LDOMS)
  1137. unsigned long hv_err;
  1138. int limit = 100;
  1139. do {
  1140. hv_err = sun4v_cpu_stop(cpu);
  1141. if (hv_err == HV_EOK) {
  1142. set_cpu_present(cpu, false);
  1143. break;
  1144. }
  1145. } while (--limit > 0);
  1146. if (limit <= 0) {
  1147. printk(KERN_ERR "sun4v_cpu_stop() fails err=%lu\n",
  1148. hv_err);
  1149. }
  1150. #endif
  1151. }
  1152. }
  1153. #endif
  1154. void __init smp_cpus_done(unsigned int max_cpus)
  1155. {
  1156. }
  1157. void smp_send_reschedule(int cpu)
  1158. {
  1159. if (cpu == smp_processor_id()) {
  1160. WARN_ON_ONCE(preemptible());
  1161. set_softint(1 << PIL_SMP_RECEIVE_SIGNAL);
  1162. } else {
  1163. xcall_deliver((u64) &xcall_receive_signal,
  1164. 0, 0, cpumask_of(cpu));
  1165. }
  1166. }
  1167. void __irq_entry smp_receive_signal_client(int irq, struct pt_regs *regs)
  1168. {
  1169. clear_softint(1 << irq);
  1170. scheduler_ipi();
  1171. }
  1172. /* This is a nop because we capture all other cpus
  1173. * anyways when making the PROM active.
  1174. */
  1175. void smp_send_stop(void)
  1176. {
  1177. }
  1178. /**
  1179. * pcpu_alloc_bootmem - NUMA friendly alloc_bootmem wrapper for percpu
  1180. * @cpu: cpu to allocate for
  1181. * @size: size allocation in bytes
  1182. * @align: alignment
  1183. *
  1184. * Allocate @size bytes aligned at @align for cpu @cpu. This wrapper
  1185. * does the right thing for NUMA regardless of the current
  1186. * configuration.
  1187. *
  1188. * RETURNS:
  1189. * Pointer to the allocated area on success, NULL on failure.
  1190. */
  1191. static void * __init pcpu_alloc_bootmem(unsigned int cpu, size_t size,
  1192. size_t align)
  1193. {
  1194. const unsigned long goal = __pa(MAX_DMA_ADDRESS);
  1195. #ifdef CONFIG_NEED_MULTIPLE_NODES
  1196. int node = cpu_to_node(cpu);
  1197. void *ptr;
  1198. if (!node_online(node) || !NODE_DATA(node)) {
  1199. ptr = __alloc_bootmem(size, align, goal);
  1200. pr_info("cpu %d has no node %d or node-local memory\n",
  1201. cpu, node);
  1202. pr_debug("per cpu data for cpu%d %lu bytes at %016lx\n",
  1203. cpu, size, __pa(ptr));
  1204. } else {
  1205. ptr = __alloc_bootmem_node(NODE_DATA(node),
  1206. size, align, goal);
  1207. pr_debug("per cpu data for cpu%d %lu bytes on node%d at "
  1208. "%016lx\n", cpu, size, node, __pa(ptr));
  1209. }
  1210. return ptr;
  1211. #else
  1212. return __alloc_bootmem(size, align, goal);
  1213. #endif
  1214. }
  1215. static void __init pcpu_free_bootmem(void *ptr, size_t size)
  1216. {
  1217. free_bootmem(__pa(ptr), size);
  1218. }
  1219. static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
  1220. {
  1221. if (cpu_to_node(from) == cpu_to_node(to))
  1222. return LOCAL_DISTANCE;
  1223. else
  1224. return REMOTE_DISTANCE;
  1225. }
  1226. static void __init pcpu_populate_pte(unsigned long addr)
  1227. {
  1228. pgd_t *pgd = pgd_offset_k(addr);
  1229. pud_t *pud;
  1230. pmd_t *pmd;
  1231. if (pgd_none(*pgd)) {
  1232. pud_t *new;
  1233. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1234. pgd_populate(&init_mm, pgd, new);
  1235. }
  1236. pud = pud_offset(pgd, addr);
  1237. if (pud_none(*pud)) {
  1238. pmd_t *new;
  1239. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1240. pud_populate(&init_mm, pud, new);
  1241. }
  1242. pmd = pmd_offset(pud, addr);
  1243. if (!pmd_present(*pmd)) {
  1244. pte_t *new;
  1245. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1246. pmd_populate_kernel(&init_mm, pmd, new);
  1247. }
  1248. }
  1249. void __init setup_per_cpu_areas(void)
  1250. {
  1251. unsigned long delta;
  1252. unsigned int cpu;
  1253. int rc = -EINVAL;
  1254. if (pcpu_chosen_fc != PCPU_FC_PAGE) {
  1255. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  1256. PERCPU_DYNAMIC_RESERVE, 4 << 20,
  1257. pcpu_cpu_distance,
  1258. pcpu_alloc_bootmem,
  1259. pcpu_free_bootmem);
  1260. if (rc)
  1261. pr_warning("PERCPU: %s allocator failed (%d), "
  1262. "falling back to page size\n",
  1263. pcpu_fc_names[pcpu_chosen_fc], rc);
  1264. }
  1265. if (rc < 0)
  1266. rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE,
  1267. pcpu_alloc_bootmem,
  1268. pcpu_free_bootmem,
  1269. pcpu_populate_pte);
  1270. if (rc < 0)
  1271. panic("cannot initialize percpu area (err=%d)", rc);
  1272. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  1273. for_each_possible_cpu(cpu)
  1274. __per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
  1275. /* Setup %g5 for the boot cpu. */
  1276. __local_per_cpu_offset = __per_cpu_offset(smp_processor_id());
  1277. of_fill_in_cpu_data();
  1278. if (tlb_type == hypervisor)
  1279. mdesc_fill_in_cpu_data(cpu_all_mask);
  1280. }