smp_64.c 35 KB

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