smp_64.c 38 KB

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