smp_64.c 40 KB

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