smp_64.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568
  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. /* Multi-cpu list version. */
  520. static void hypervisor_xcall_deliver(struct trap_per_cpu *tb, int cnt)
  521. {
  522. int retries, this_cpu, prev_sent, i, saw_cpu_error;
  523. unsigned long status;
  524. u16 *cpu_list;
  525. this_cpu = smp_processor_id();
  526. cpu_list = __va(tb->cpu_list_pa);
  527. saw_cpu_error = 0;
  528. retries = 0;
  529. prev_sent = 0;
  530. do {
  531. int forward_progress, n_sent;
  532. status = sun4v_cpu_mondo_send(cnt,
  533. tb->cpu_list_pa,
  534. tb->cpu_mondo_block_pa);
  535. /* HV_EOK means all cpus received the xcall, we're done. */
  536. if (likely(status == HV_EOK))
  537. break;
  538. /* First, see if we made any forward progress.
  539. *
  540. * The hypervisor indicates successful sends by setting
  541. * cpu list entries to the value 0xffff.
  542. */
  543. n_sent = 0;
  544. for (i = 0; i < cnt; i++) {
  545. if (likely(cpu_list[i] == 0xffff))
  546. n_sent++;
  547. }
  548. forward_progress = 0;
  549. if (n_sent > prev_sent)
  550. forward_progress = 1;
  551. prev_sent = n_sent;
  552. /* If we get a HV_ECPUERROR, then one or more of the cpus
  553. * in the list are in error state. Use the cpu_state()
  554. * hypervisor call to find out which cpus are in error state.
  555. */
  556. if (unlikely(status == HV_ECPUERROR)) {
  557. for (i = 0; i < cnt; i++) {
  558. long err;
  559. u16 cpu;
  560. cpu = cpu_list[i];
  561. if (cpu == 0xffff)
  562. continue;
  563. err = sun4v_cpu_state(cpu);
  564. if (err == HV_CPU_STATE_ERROR) {
  565. saw_cpu_error = (cpu + 1);
  566. cpu_list[i] = 0xffff;
  567. }
  568. }
  569. } else if (unlikely(status != HV_EWOULDBLOCK))
  570. goto fatal_mondo_error;
  571. /* Don't bother rewriting the CPU list, just leave the
  572. * 0xffff and non-0xffff entries in there and the
  573. * hypervisor will do the right thing.
  574. *
  575. * Only advance timeout state if we didn't make any
  576. * forward progress.
  577. */
  578. if (unlikely(!forward_progress)) {
  579. if (unlikely(++retries > 10000))
  580. goto fatal_mondo_timeout;
  581. /* Delay a little bit to let other cpus catch up
  582. * on their cpu mondo queue work.
  583. */
  584. udelay(2 * cnt);
  585. }
  586. } while (1);
  587. if (unlikely(saw_cpu_error))
  588. goto fatal_mondo_cpu_error;
  589. return;
  590. fatal_mondo_cpu_error:
  591. printk(KERN_CRIT "CPU[%d]: SUN4V mondo cpu error, some target cpus "
  592. "(including %d) were in error state\n",
  593. this_cpu, saw_cpu_error - 1);
  594. return;
  595. fatal_mondo_timeout:
  596. printk(KERN_CRIT "CPU[%d]: SUN4V mondo timeout, no forward "
  597. " progress after %d retries.\n",
  598. this_cpu, retries);
  599. goto dump_cpu_list_and_out;
  600. fatal_mondo_error:
  601. printk(KERN_CRIT "CPU[%d]: Unexpected SUN4V mondo error %lu\n",
  602. this_cpu, status);
  603. printk(KERN_CRIT "CPU[%d]: Args were cnt(%d) cpulist_pa(%lx) "
  604. "mondo_block_pa(%lx)\n",
  605. this_cpu, cnt, tb->cpu_list_pa, tb->cpu_mondo_block_pa);
  606. dump_cpu_list_and_out:
  607. printk(KERN_CRIT "CPU[%d]: CPU list [ ", this_cpu);
  608. for (i = 0; i < cnt; i++)
  609. printk("%u ", cpu_list[i]);
  610. printk("]\n");
  611. }
  612. static void (*xcall_deliver_impl)(struct trap_per_cpu *, int);
  613. static void xcall_deliver(u64 data0, u64 data1, u64 data2, const cpumask_t *mask)
  614. {
  615. struct trap_per_cpu *tb;
  616. int this_cpu, i, cnt;
  617. unsigned long flags;
  618. u16 *cpu_list;
  619. u64 *mondo;
  620. /* We have to do this whole thing with interrupts fully disabled.
  621. * Otherwise if we send an xcall from interrupt context it will
  622. * corrupt both our mondo block and cpu list state.
  623. *
  624. * One consequence of this is that we cannot use timeout mechanisms
  625. * that depend upon interrupts being delivered locally. So, for
  626. * example, we cannot sample jiffies and expect it to advance.
  627. *
  628. * Fortunately, udelay() uses %stick/%tick so we can use that.
  629. */
  630. local_irq_save(flags);
  631. this_cpu = smp_processor_id();
  632. tb = &trap_block[this_cpu];
  633. mondo = __va(tb->cpu_mondo_block_pa);
  634. mondo[0] = data0;
  635. mondo[1] = data1;
  636. mondo[2] = data2;
  637. wmb();
  638. cpu_list = __va(tb->cpu_list_pa);
  639. /* Setup the initial cpu list. */
  640. cnt = 0;
  641. for_each_cpu(i, mask) {
  642. if (i == this_cpu || !cpu_online(i))
  643. continue;
  644. cpu_list[cnt++] = i;
  645. }
  646. if (cnt)
  647. xcall_deliver_impl(tb, cnt);
  648. local_irq_restore(flags);
  649. }
  650. /* Send cross call to all processors mentioned in MASK_P
  651. * except self. Really, there are only two cases currently,
  652. * "cpu_online_mask" and "mm_cpumask(mm)".
  653. */
  654. static void smp_cross_call_masked(unsigned long *func, u32 ctx, u64 data1, u64 data2, const cpumask_t *mask)
  655. {
  656. u64 data0 = (((u64)ctx)<<32 | (((u64)func) & 0xffffffff));
  657. xcall_deliver(data0, data1, data2, mask);
  658. }
  659. /* Send cross call to all processors except self. */
  660. static void smp_cross_call(unsigned long *func, u32 ctx, u64 data1, u64 data2)
  661. {
  662. smp_cross_call_masked(func, ctx, data1, data2, cpu_online_mask);
  663. }
  664. extern unsigned long xcall_sync_tick;
  665. static void smp_start_sync_tick_client(int cpu)
  666. {
  667. xcall_deliver((u64) &xcall_sync_tick, 0, 0,
  668. cpumask_of(cpu));
  669. }
  670. extern unsigned long xcall_call_function;
  671. void arch_send_call_function_ipi_mask(const struct cpumask *mask)
  672. {
  673. xcall_deliver((u64) &xcall_call_function, 0, 0, mask);
  674. }
  675. extern unsigned long xcall_call_function_single;
  676. void arch_send_call_function_single_ipi(int cpu)
  677. {
  678. xcall_deliver((u64) &xcall_call_function_single, 0, 0,
  679. cpumask_of(cpu));
  680. }
  681. void __irq_entry smp_call_function_client(int irq, struct pt_regs *regs)
  682. {
  683. clear_softint(1 << irq);
  684. irq_enter();
  685. generic_smp_call_function_interrupt();
  686. irq_exit();
  687. }
  688. void __irq_entry smp_call_function_single_client(int irq, struct pt_regs *regs)
  689. {
  690. clear_softint(1 << irq);
  691. irq_enter();
  692. generic_smp_call_function_single_interrupt();
  693. irq_exit();
  694. }
  695. static void tsb_sync(void *info)
  696. {
  697. struct trap_per_cpu *tp = &trap_block[raw_smp_processor_id()];
  698. struct mm_struct *mm = info;
  699. /* It is not valid to test "current->active_mm == mm" here.
  700. *
  701. * The value of "current" is not changed atomically with
  702. * switch_mm(). But that's OK, we just need to check the
  703. * current cpu's trap block PGD physical address.
  704. */
  705. if (tp->pgd_paddr == __pa(mm->pgd))
  706. tsb_context_switch(mm);
  707. }
  708. void smp_tsb_sync(struct mm_struct *mm)
  709. {
  710. smp_call_function_many(mm_cpumask(mm), tsb_sync, mm, 1);
  711. }
  712. extern unsigned long xcall_flush_tlb_mm;
  713. extern unsigned long xcall_flush_tlb_page;
  714. extern unsigned long xcall_flush_tlb_kernel_range;
  715. extern unsigned long xcall_fetch_glob_regs;
  716. extern unsigned long xcall_fetch_glob_pmu;
  717. extern unsigned long xcall_fetch_glob_pmu_n4;
  718. extern unsigned long xcall_receive_signal;
  719. extern unsigned long xcall_new_mmu_context_version;
  720. #ifdef CONFIG_KGDB
  721. extern unsigned long xcall_kgdb_capture;
  722. #endif
  723. #ifdef DCACHE_ALIASING_POSSIBLE
  724. extern unsigned long xcall_flush_dcache_page_cheetah;
  725. #endif
  726. extern unsigned long xcall_flush_dcache_page_spitfire;
  727. static inline void __local_flush_dcache_page(struct page *page)
  728. {
  729. #ifdef DCACHE_ALIASING_POSSIBLE
  730. __flush_dcache_page(page_address(page),
  731. ((tlb_type == spitfire) &&
  732. page_mapping(page) != NULL));
  733. #else
  734. if (page_mapping(page) != NULL &&
  735. tlb_type == spitfire)
  736. __flush_icache_page(__pa(page_address(page)));
  737. #endif
  738. }
  739. void smp_flush_dcache_page_impl(struct page *page, int cpu)
  740. {
  741. int this_cpu;
  742. if (tlb_type == hypervisor)
  743. return;
  744. #ifdef CONFIG_DEBUG_DCFLUSH
  745. atomic_inc(&dcpage_flushes);
  746. #endif
  747. this_cpu = get_cpu();
  748. if (cpu == this_cpu) {
  749. __local_flush_dcache_page(page);
  750. } else if (cpu_online(cpu)) {
  751. void *pg_addr = page_address(page);
  752. u64 data0 = 0;
  753. if (tlb_type == spitfire) {
  754. data0 = ((u64)&xcall_flush_dcache_page_spitfire);
  755. if (page_mapping(page) != NULL)
  756. data0 |= ((u64)1 << 32);
  757. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  758. #ifdef DCACHE_ALIASING_POSSIBLE
  759. data0 = ((u64)&xcall_flush_dcache_page_cheetah);
  760. #endif
  761. }
  762. if (data0) {
  763. xcall_deliver(data0, __pa(pg_addr),
  764. (u64) pg_addr, cpumask_of(cpu));
  765. #ifdef CONFIG_DEBUG_DCFLUSH
  766. atomic_inc(&dcpage_flushes_xcall);
  767. #endif
  768. }
  769. }
  770. put_cpu();
  771. }
  772. void flush_dcache_page_all(struct mm_struct *mm, struct page *page)
  773. {
  774. void *pg_addr;
  775. u64 data0;
  776. if (tlb_type == hypervisor)
  777. return;
  778. preempt_disable();
  779. #ifdef CONFIG_DEBUG_DCFLUSH
  780. atomic_inc(&dcpage_flushes);
  781. #endif
  782. data0 = 0;
  783. pg_addr = page_address(page);
  784. if (tlb_type == spitfire) {
  785. data0 = ((u64)&xcall_flush_dcache_page_spitfire);
  786. if (page_mapping(page) != NULL)
  787. data0 |= ((u64)1 << 32);
  788. } else if (tlb_type == cheetah || tlb_type == cheetah_plus) {
  789. #ifdef DCACHE_ALIASING_POSSIBLE
  790. data0 = ((u64)&xcall_flush_dcache_page_cheetah);
  791. #endif
  792. }
  793. if (data0) {
  794. xcall_deliver(data0, __pa(pg_addr),
  795. (u64) pg_addr, cpu_online_mask);
  796. #ifdef CONFIG_DEBUG_DCFLUSH
  797. atomic_inc(&dcpage_flushes_xcall);
  798. #endif
  799. }
  800. __local_flush_dcache_page(page);
  801. preempt_enable();
  802. }
  803. void __irq_entry smp_new_mmu_context_version_client(int irq, struct pt_regs *regs)
  804. {
  805. struct mm_struct *mm;
  806. unsigned long flags;
  807. clear_softint(1 << irq);
  808. /* See if we need to allocate a new TLB context because
  809. * the version of the one we are using is now out of date.
  810. */
  811. mm = current->active_mm;
  812. if (unlikely(!mm || (mm == &init_mm)))
  813. return;
  814. spin_lock_irqsave(&mm->context.lock, flags);
  815. if (unlikely(!CTX_VALID(mm->context)))
  816. get_new_mmu_context(mm);
  817. spin_unlock_irqrestore(&mm->context.lock, flags);
  818. load_secondary_context(mm);
  819. __flush_tlb_mm(CTX_HWBITS(mm->context),
  820. SECONDARY_CONTEXT);
  821. }
  822. void smp_new_mmu_context_version(void)
  823. {
  824. smp_cross_call(&xcall_new_mmu_context_version, 0, 0, 0);
  825. }
  826. #ifdef CONFIG_KGDB
  827. void kgdb_roundup_cpus(unsigned long flags)
  828. {
  829. smp_cross_call(&xcall_kgdb_capture, 0, 0, 0);
  830. }
  831. #endif
  832. void smp_fetch_global_regs(void)
  833. {
  834. smp_cross_call(&xcall_fetch_glob_regs, 0, 0, 0);
  835. }
  836. void smp_fetch_global_pmu(void)
  837. {
  838. if (tlb_type == hypervisor &&
  839. sun4v_chip_type >= SUN4V_CHIP_NIAGARA4)
  840. smp_cross_call(&xcall_fetch_glob_pmu_n4, 0, 0, 0);
  841. else
  842. smp_cross_call(&xcall_fetch_glob_pmu, 0, 0, 0);
  843. }
  844. /* We know that the window frames of the user have been flushed
  845. * to the stack before we get here because all callers of us
  846. * are flush_tlb_*() routines, and these run after flush_cache_*()
  847. * which performs the flushw.
  848. *
  849. * The SMP TLB coherency scheme we use works as follows:
  850. *
  851. * 1) mm->cpu_vm_mask is a bit mask of which cpus an address
  852. * space has (potentially) executed on, this is the heuristic
  853. * we use to avoid doing cross calls.
  854. *
  855. * Also, for flushing from kswapd and also for clones, we
  856. * use cpu_vm_mask as the list of cpus to make run the TLB.
  857. *
  858. * 2) TLB context numbers are shared globally across all processors
  859. * in the system, this allows us to play several games to avoid
  860. * cross calls.
  861. *
  862. * One invariant is that when a cpu switches to a process, and
  863. * that processes tsk->active_mm->cpu_vm_mask does not have the
  864. * current cpu's bit set, that tlb context is flushed locally.
  865. *
  866. * If the address space is non-shared (ie. mm->count == 1) we avoid
  867. * cross calls when we want to flush the currently running process's
  868. * tlb state. This is done by clearing all cpu bits except the current
  869. * processor's in current->mm->cpu_vm_mask and performing the
  870. * flush locally only. This will force any subsequent cpus which run
  871. * this task to flush the context from the local tlb if the process
  872. * migrates to another cpu (again).
  873. *
  874. * 3) For shared address spaces (threads) and swapping we bite the
  875. * bullet for most cases and perform the cross call (but only to
  876. * the cpus listed in cpu_vm_mask).
  877. *
  878. * The performance gain from "optimizing" away the cross call for threads is
  879. * questionable (in theory the big win for threads is the massive sharing of
  880. * address space state across processors).
  881. */
  882. /* This currently is only used by the hugetlb arch pre-fault
  883. * hook on UltraSPARC-III+ and later when changing the pagesize
  884. * bits of the context register for an address space.
  885. */
  886. void smp_flush_tlb_mm(struct mm_struct *mm)
  887. {
  888. u32 ctx = CTX_HWBITS(mm->context);
  889. int cpu = get_cpu();
  890. if (atomic_read(&mm->mm_users) == 1) {
  891. cpumask_copy(mm_cpumask(mm), cpumask_of(cpu));
  892. goto local_flush_and_out;
  893. }
  894. smp_cross_call_masked(&xcall_flush_tlb_mm,
  895. ctx, 0, 0,
  896. mm_cpumask(mm));
  897. local_flush_and_out:
  898. __flush_tlb_mm(ctx, SECONDARY_CONTEXT);
  899. put_cpu();
  900. }
  901. struct tlb_pending_info {
  902. unsigned long ctx;
  903. unsigned long nr;
  904. unsigned long *vaddrs;
  905. };
  906. static void tlb_pending_func(void *info)
  907. {
  908. struct tlb_pending_info *t = info;
  909. __flush_tlb_pending(t->ctx, t->nr, t->vaddrs);
  910. }
  911. void smp_flush_tlb_pending(struct mm_struct *mm, unsigned long nr, unsigned long *vaddrs)
  912. {
  913. u32 ctx = CTX_HWBITS(mm->context);
  914. struct tlb_pending_info info;
  915. int cpu = get_cpu();
  916. info.ctx = ctx;
  917. info.nr = nr;
  918. info.vaddrs = vaddrs;
  919. if (mm == current->mm && atomic_read(&mm->mm_users) == 1)
  920. cpumask_copy(mm_cpumask(mm), cpumask_of(cpu));
  921. else
  922. smp_call_function_many(mm_cpumask(mm), tlb_pending_func,
  923. &info, 1);
  924. __flush_tlb_pending(ctx, nr, vaddrs);
  925. put_cpu();
  926. }
  927. void smp_flush_tlb_page(struct mm_struct *mm, unsigned long vaddr)
  928. {
  929. unsigned long context = CTX_HWBITS(mm->context);
  930. int cpu = get_cpu();
  931. if (mm == current->mm && atomic_read(&mm->mm_users) == 1)
  932. cpumask_copy(mm_cpumask(mm), cpumask_of(cpu));
  933. else
  934. smp_cross_call_masked(&xcall_flush_tlb_page,
  935. context, vaddr, 0,
  936. mm_cpumask(mm));
  937. __flush_tlb_page(context, vaddr);
  938. put_cpu();
  939. }
  940. void smp_flush_tlb_kernel_range(unsigned long start, unsigned long end)
  941. {
  942. start &= PAGE_MASK;
  943. end = PAGE_ALIGN(end);
  944. if (start != end) {
  945. smp_cross_call(&xcall_flush_tlb_kernel_range,
  946. 0, start, end);
  947. __flush_tlb_kernel_range(start, end);
  948. }
  949. }
  950. /* CPU capture. */
  951. /* #define CAPTURE_DEBUG */
  952. extern unsigned long xcall_capture;
  953. static atomic_t smp_capture_depth = ATOMIC_INIT(0);
  954. static atomic_t smp_capture_registry = ATOMIC_INIT(0);
  955. static unsigned long penguins_are_doing_time;
  956. void smp_capture(void)
  957. {
  958. int result = atomic_add_return(1, &smp_capture_depth);
  959. if (result == 1) {
  960. int ncpus = num_online_cpus();
  961. #ifdef CAPTURE_DEBUG
  962. printk("CPU[%d]: Sending penguins to jail...",
  963. smp_processor_id());
  964. #endif
  965. penguins_are_doing_time = 1;
  966. atomic_inc(&smp_capture_registry);
  967. smp_cross_call(&xcall_capture, 0, 0, 0);
  968. while (atomic_read(&smp_capture_registry) != ncpus)
  969. rmb();
  970. #ifdef CAPTURE_DEBUG
  971. printk("done\n");
  972. #endif
  973. }
  974. }
  975. void smp_release(void)
  976. {
  977. if (atomic_dec_and_test(&smp_capture_depth)) {
  978. #ifdef CAPTURE_DEBUG
  979. printk("CPU[%d]: Giving pardon to "
  980. "imprisoned penguins\n",
  981. smp_processor_id());
  982. #endif
  983. penguins_are_doing_time = 0;
  984. membar_safe("#StoreLoad");
  985. atomic_dec(&smp_capture_registry);
  986. }
  987. }
  988. /* Imprisoned penguins run with %pil == PIL_NORMAL_MAX, but PSTATE_IE
  989. * set, so they can service tlb flush xcalls...
  990. */
  991. extern void prom_world(int);
  992. void __irq_entry smp_penguin_jailcell(int irq, struct pt_regs *regs)
  993. {
  994. clear_softint(1 << irq);
  995. preempt_disable();
  996. __asm__ __volatile__("flushw");
  997. prom_world(1);
  998. atomic_inc(&smp_capture_registry);
  999. membar_safe("#StoreLoad");
  1000. while (penguins_are_doing_time)
  1001. rmb();
  1002. atomic_dec(&smp_capture_registry);
  1003. prom_world(0);
  1004. preempt_enable();
  1005. }
  1006. /* /proc/profile writes can call this, don't __init it please. */
  1007. int setup_profiling_timer(unsigned int multiplier)
  1008. {
  1009. return -EINVAL;
  1010. }
  1011. void __init smp_prepare_cpus(unsigned int max_cpus)
  1012. {
  1013. }
  1014. void smp_prepare_boot_cpu(void)
  1015. {
  1016. }
  1017. void __init smp_setup_processor_id(void)
  1018. {
  1019. if (tlb_type == spitfire)
  1020. xcall_deliver_impl = spitfire_xcall_deliver;
  1021. else if (tlb_type == cheetah || tlb_type == cheetah_plus)
  1022. xcall_deliver_impl = cheetah_xcall_deliver;
  1023. else
  1024. xcall_deliver_impl = hypervisor_xcall_deliver;
  1025. }
  1026. void smp_fill_in_sib_core_maps(void)
  1027. {
  1028. unsigned int i;
  1029. for_each_present_cpu(i) {
  1030. unsigned int j;
  1031. cpumask_clear(&cpu_core_map[i]);
  1032. if (cpu_data(i).core_id == 0) {
  1033. cpumask_set_cpu(i, &cpu_core_map[i]);
  1034. continue;
  1035. }
  1036. for_each_present_cpu(j) {
  1037. if (cpu_data(i).core_id ==
  1038. cpu_data(j).core_id)
  1039. cpumask_set_cpu(j, &cpu_core_map[i]);
  1040. }
  1041. }
  1042. for_each_present_cpu(i) {
  1043. unsigned int j;
  1044. for_each_present_cpu(j) {
  1045. if (cpu_data(i).sock_id == cpu_data(j).sock_id)
  1046. cpumask_set_cpu(j, &cpu_core_sib_map[i]);
  1047. }
  1048. }
  1049. for_each_present_cpu(i) {
  1050. unsigned int j;
  1051. cpumask_clear(&per_cpu(cpu_sibling_map, i));
  1052. if (cpu_data(i).proc_id == -1) {
  1053. cpumask_set_cpu(i, &per_cpu(cpu_sibling_map, i));
  1054. continue;
  1055. }
  1056. for_each_present_cpu(j) {
  1057. if (cpu_data(i).proc_id ==
  1058. cpu_data(j).proc_id)
  1059. cpumask_set_cpu(j, &per_cpu(cpu_sibling_map, i));
  1060. }
  1061. }
  1062. }
  1063. int __cpu_up(unsigned int cpu, struct task_struct *tidle)
  1064. {
  1065. int ret = smp_boot_one_cpu(cpu, tidle);
  1066. if (!ret) {
  1067. cpumask_set_cpu(cpu, &smp_commenced_mask);
  1068. while (!cpu_online(cpu))
  1069. mb();
  1070. if (!cpu_online(cpu)) {
  1071. ret = -ENODEV;
  1072. } else {
  1073. /* On SUN4V, writes to %tick and %stick are
  1074. * not allowed.
  1075. */
  1076. if (tlb_type != hypervisor)
  1077. smp_synchronize_one_tick(cpu);
  1078. }
  1079. }
  1080. return ret;
  1081. }
  1082. #ifdef CONFIG_HOTPLUG_CPU
  1083. void cpu_play_dead(void)
  1084. {
  1085. int cpu = smp_processor_id();
  1086. unsigned long pstate;
  1087. idle_task_exit();
  1088. if (tlb_type == hypervisor) {
  1089. struct trap_per_cpu *tb = &trap_block[cpu];
  1090. sun4v_cpu_qconf(HV_CPU_QUEUE_CPU_MONDO,
  1091. tb->cpu_mondo_pa, 0);
  1092. sun4v_cpu_qconf(HV_CPU_QUEUE_DEVICE_MONDO,
  1093. tb->dev_mondo_pa, 0);
  1094. sun4v_cpu_qconf(HV_CPU_QUEUE_RES_ERROR,
  1095. tb->resum_mondo_pa, 0);
  1096. sun4v_cpu_qconf(HV_CPU_QUEUE_NONRES_ERROR,
  1097. tb->nonresum_mondo_pa, 0);
  1098. }
  1099. cpumask_clear_cpu(cpu, &smp_commenced_mask);
  1100. membar_safe("#Sync");
  1101. local_irq_disable();
  1102. __asm__ __volatile__(
  1103. "rdpr %%pstate, %0\n\t"
  1104. "wrpr %0, %1, %%pstate"
  1105. : "=r" (pstate)
  1106. : "i" (PSTATE_IE));
  1107. while (1)
  1108. barrier();
  1109. }
  1110. int __cpu_disable(void)
  1111. {
  1112. int cpu = smp_processor_id();
  1113. cpuinfo_sparc *c;
  1114. int i;
  1115. for_each_cpu(i, &cpu_core_map[cpu])
  1116. cpumask_clear_cpu(cpu, &cpu_core_map[i]);
  1117. cpumask_clear(&cpu_core_map[cpu]);
  1118. for_each_cpu(i, &per_cpu(cpu_sibling_map, cpu))
  1119. cpumask_clear_cpu(cpu, &per_cpu(cpu_sibling_map, i));
  1120. cpumask_clear(&per_cpu(cpu_sibling_map, cpu));
  1121. c = &cpu_data(cpu);
  1122. c->core_id = 0;
  1123. c->proc_id = -1;
  1124. smp_wmb();
  1125. /* Make sure no interrupts point to this cpu. */
  1126. fixup_irqs();
  1127. local_irq_enable();
  1128. mdelay(1);
  1129. local_irq_disable();
  1130. set_cpu_online(cpu, false);
  1131. cpu_map_rebuild();
  1132. return 0;
  1133. }
  1134. void __cpu_die(unsigned int cpu)
  1135. {
  1136. int i;
  1137. for (i = 0; i < 100; i++) {
  1138. smp_rmb();
  1139. if (!cpumask_test_cpu(cpu, &smp_commenced_mask))
  1140. break;
  1141. msleep(100);
  1142. }
  1143. if (cpumask_test_cpu(cpu, &smp_commenced_mask)) {
  1144. printk(KERN_ERR "CPU %u didn't die...\n", cpu);
  1145. } else {
  1146. #if defined(CONFIG_SUN_LDOMS)
  1147. unsigned long hv_err;
  1148. int limit = 100;
  1149. do {
  1150. hv_err = sun4v_cpu_stop(cpu);
  1151. if (hv_err == HV_EOK) {
  1152. set_cpu_present(cpu, false);
  1153. break;
  1154. }
  1155. } while (--limit > 0);
  1156. if (limit <= 0) {
  1157. printk(KERN_ERR "sun4v_cpu_stop() fails err=%lu\n",
  1158. hv_err);
  1159. }
  1160. #endif
  1161. }
  1162. }
  1163. #endif
  1164. void __init smp_cpus_done(unsigned int max_cpus)
  1165. {
  1166. }
  1167. void smp_send_reschedule(int cpu)
  1168. {
  1169. if (cpu == smp_processor_id()) {
  1170. WARN_ON_ONCE(preemptible());
  1171. set_softint(1 << PIL_SMP_RECEIVE_SIGNAL);
  1172. } else {
  1173. xcall_deliver((u64) &xcall_receive_signal,
  1174. 0, 0, cpumask_of(cpu));
  1175. }
  1176. }
  1177. void __irq_entry smp_receive_signal_client(int irq, struct pt_regs *regs)
  1178. {
  1179. clear_softint(1 << irq);
  1180. scheduler_ipi();
  1181. }
  1182. static void stop_this_cpu(void *dummy)
  1183. {
  1184. prom_stopself();
  1185. }
  1186. void smp_send_stop(void)
  1187. {
  1188. int cpu;
  1189. if (tlb_type == hypervisor) {
  1190. for_each_online_cpu(cpu) {
  1191. if (cpu == smp_processor_id())
  1192. continue;
  1193. #ifdef CONFIG_SUN_LDOMS
  1194. if (ldom_domaining_enabled) {
  1195. unsigned long hv_err;
  1196. hv_err = sun4v_cpu_stop(cpu);
  1197. if (hv_err)
  1198. printk(KERN_ERR "sun4v_cpu_stop() "
  1199. "failed err=%lu\n", hv_err);
  1200. } else
  1201. #endif
  1202. prom_stopcpu_cpuid(cpu);
  1203. }
  1204. } else
  1205. smp_call_function(stop_this_cpu, NULL, 0);
  1206. }
  1207. /**
  1208. * pcpu_alloc_bootmem - NUMA friendly alloc_bootmem wrapper for percpu
  1209. * @cpu: cpu to allocate for
  1210. * @size: size allocation in bytes
  1211. * @align: alignment
  1212. *
  1213. * Allocate @size bytes aligned at @align for cpu @cpu. This wrapper
  1214. * does the right thing for NUMA regardless of the current
  1215. * configuration.
  1216. *
  1217. * RETURNS:
  1218. * Pointer to the allocated area on success, NULL on failure.
  1219. */
  1220. static void * __init pcpu_alloc_bootmem(unsigned int cpu, size_t size,
  1221. size_t align)
  1222. {
  1223. const unsigned long goal = __pa(MAX_DMA_ADDRESS);
  1224. #ifdef CONFIG_NEED_MULTIPLE_NODES
  1225. int node = cpu_to_node(cpu);
  1226. void *ptr;
  1227. if (!node_online(node) || !NODE_DATA(node)) {
  1228. ptr = __alloc_bootmem(size, align, goal);
  1229. pr_info("cpu %d has no node %d or node-local memory\n",
  1230. cpu, node);
  1231. pr_debug("per cpu data for cpu%d %lu bytes at %016lx\n",
  1232. cpu, size, __pa(ptr));
  1233. } else {
  1234. ptr = __alloc_bootmem_node(NODE_DATA(node),
  1235. size, align, goal);
  1236. pr_debug("per cpu data for cpu%d %lu bytes on node%d at "
  1237. "%016lx\n", cpu, size, node, __pa(ptr));
  1238. }
  1239. return ptr;
  1240. #else
  1241. return __alloc_bootmem(size, align, goal);
  1242. #endif
  1243. }
  1244. static void __init pcpu_free_bootmem(void *ptr, size_t size)
  1245. {
  1246. free_bootmem(__pa(ptr), size);
  1247. }
  1248. static int __init pcpu_cpu_distance(unsigned int from, unsigned int to)
  1249. {
  1250. if (cpu_to_node(from) == cpu_to_node(to))
  1251. return LOCAL_DISTANCE;
  1252. else
  1253. return REMOTE_DISTANCE;
  1254. }
  1255. static void __init pcpu_populate_pte(unsigned long addr)
  1256. {
  1257. pgd_t *pgd = pgd_offset_k(addr);
  1258. pud_t *pud;
  1259. pmd_t *pmd;
  1260. if (pgd_none(*pgd)) {
  1261. pud_t *new;
  1262. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1263. pgd_populate(&init_mm, pgd, new);
  1264. }
  1265. pud = pud_offset(pgd, addr);
  1266. if (pud_none(*pud)) {
  1267. pmd_t *new;
  1268. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1269. pud_populate(&init_mm, pud, new);
  1270. }
  1271. pmd = pmd_offset(pud, addr);
  1272. if (!pmd_present(*pmd)) {
  1273. pte_t *new;
  1274. new = __alloc_bootmem(PAGE_SIZE, PAGE_SIZE, PAGE_SIZE);
  1275. pmd_populate_kernel(&init_mm, pmd, new);
  1276. }
  1277. }
  1278. void __init setup_per_cpu_areas(void)
  1279. {
  1280. unsigned long delta;
  1281. unsigned int cpu;
  1282. int rc = -EINVAL;
  1283. if (pcpu_chosen_fc != PCPU_FC_PAGE) {
  1284. rc = pcpu_embed_first_chunk(PERCPU_MODULE_RESERVE,
  1285. PERCPU_DYNAMIC_RESERVE, 4 << 20,
  1286. pcpu_cpu_distance,
  1287. pcpu_alloc_bootmem,
  1288. pcpu_free_bootmem);
  1289. if (rc)
  1290. pr_warning("PERCPU: %s allocator failed (%d), "
  1291. "falling back to page size\n",
  1292. pcpu_fc_names[pcpu_chosen_fc], rc);
  1293. }
  1294. if (rc < 0)
  1295. rc = pcpu_page_first_chunk(PERCPU_MODULE_RESERVE,
  1296. pcpu_alloc_bootmem,
  1297. pcpu_free_bootmem,
  1298. pcpu_populate_pte);
  1299. if (rc < 0)
  1300. panic("cannot initialize percpu area (err=%d)", rc);
  1301. delta = (unsigned long)pcpu_base_addr - (unsigned long)__per_cpu_start;
  1302. for_each_possible_cpu(cpu)
  1303. __per_cpu_offset(cpu) = delta + pcpu_unit_offsets[cpu];
  1304. /* Setup %g5 for the boot cpu. */
  1305. __local_per_cpu_offset = __per_cpu_offset(smp_processor_id());
  1306. of_fill_in_cpu_data();
  1307. if (tlb_type == hypervisor)
  1308. mdesc_fill_in_cpu_data(cpu_all_mask);
  1309. }