smp_64.c 36 KB

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