smp_64.c 39 KB

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