smp.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * SMP support for power macintosh.
  3. *
  4. * We support both the old "powersurge" SMP architecture
  5. * and the current Core99 (G4 PowerMac) machines.
  6. *
  7. * Note that we don't support the very first rev. of
  8. * Apple/DayStar 2 CPUs board, the one with the funky
  9. * watchdog. Hopefully, none of these should be there except
  10. * maybe internally to Apple. I should probably still add some
  11. * code to detect this card though and disable SMP. --BenH.
  12. *
  13. * Support Macintosh G4 SMP by Troy Benjegerdes (hozer@drgw.net)
  14. * and Ben Herrenschmidt <benh@kernel.crashing.org>.
  15. *
  16. * Support for DayStar quad CPU cards
  17. * Copyright (C) XLR8, Inc. 1994-2000
  18. *
  19. * This program is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU General Public License
  21. * as published by the Free Software Foundation; either version
  22. * 2 of the License, or (at your option) any later version.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/smp.h>
  27. #include <linux/interrupt.h>
  28. #include <linux/kernel_stat.h>
  29. #include <linux/delay.h>
  30. #include <linux/init.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/errno.h>
  33. #include <linux/hardirq.h>
  34. #include <linux/cpu.h>
  35. #include <linux/compiler.h>
  36. #include <asm/ptrace.h>
  37. #include <linux/atomic.h>
  38. #include <asm/code-patching.h>
  39. #include <asm/irq.h>
  40. #include <asm/page.h>
  41. #include <asm/pgtable.h>
  42. #include <asm/sections.h>
  43. #include <asm/io.h>
  44. #include <asm/prom.h>
  45. #include <asm/smp.h>
  46. #include <asm/machdep.h>
  47. #include <asm/pmac_feature.h>
  48. #include <asm/time.h>
  49. #include <asm/mpic.h>
  50. #include <asm/cacheflush.h>
  51. #include <asm/keylargo.h>
  52. #include <asm/pmac_low_i2c.h>
  53. #include <asm/pmac_pfunc.h>
  54. #include "pmac.h"
  55. #undef DEBUG
  56. #ifdef DEBUG
  57. #define DBG(fmt...) udbg_printf(fmt)
  58. #else
  59. #define DBG(fmt...)
  60. #endif
  61. extern void __secondary_start_pmac_0(void);
  62. extern int pmac_pfunc_base_install(void);
  63. static void (*pmac_tb_freeze)(int freeze);
  64. static u64 timebase;
  65. static int tb_req;
  66. #ifdef CONFIG_PPC_PMAC32_PSURGE
  67. /*
  68. * Powersurge (old powermac SMP) support.
  69. */
  70. /* Addresses for powersurge registers */
  71. #define HAMMERHEAD_BASE 0xf8000000
  72. #define HHEAD_CONFIG 0x90
  73. #define HHEAD_SEC_INTR 0xc0
  74. /* register for interrupting the primary processor on the powersurge */
  75. /* N.B. this is actually the ethernet ROM! */
  76. #define PSURGE_PRI_INTR 0xf3019000
  77. /* register for storing the start address for the secondary processor */
  78. /* N.B. this is the PCI config space address register for the 1st bridge */
  79. #define PSURGE_START 0xf2800000
  80. /* Daystar/XLR8 4-CPU card */
  81. #define PSURGE_QUAD_REG_ADDR 0xf8800000
  82. #define PSURGE_QUAD_IRQ_SET 0
  83. #define PSURGE_QUAD_IRQ_CLR 1
  84. #define PSURGE_QUAD_IRQ_PRIMARY 2
  85. #define PSURGE_QUAD_CKSTOP_CTL 3
  86. #define PSURGE_QUAD_PRIMARY_ARB 4
  87. #define PSURGE_QUAD_BOARD_ID 6
  88. #define PSURGE_QUAD_WHICH_CPU 7
  89. #define PSURGE_QUAD_CKSTOP_RDBK 8
  90. #define PSURGE_QUAD_RESET_CTL 11
  91. #define PSURGE_QUAD_OUT(r, v) (out_8(quad_base + ((r) << 4) + 4, (v)))
  92. #define PSURGE_QUAD_IN(r) (in_8(quad_base + ((r) << 4) + 4) & 0x0f)
  93. #define PSURGE_QUAD_BIS(r, v) (PSURGE_QUAD_OUT((r), PSURGE_QUAD_IN(r) | (v)))
  94. #define PSURGE_QUAD_BIC(r, v) (PSURGE_QUAD_OUT((r), PSURGE_QUAD_IN(r) & ~(v)))
  95. /* virtual addresses for the above */
  96. static volatile u8 __iomem *hhead_base;
  97. static volatile u8 __iomem *quad_base;
  98. static volatile u32 __iomem *psurge_pri_intr;
  99. static volatile u8 __iomem *psurge_sec_intr;
  100. static volatile u32 __iomem *psurge_start;
  101. /* values for psurge_type */
  102. #define PSURGE_NONE -1
  103. #define PSURGE_DUAL 0
  104. #define PSURGE_QUAD_OKEE 1
  105. #define PSURGE_QUAD_COTTON 2
  106. #define PSURGE_QUAD_ICEGRASS 3
  107. /* what sort of powersurge board we have */
  108. static int psurge_type = PSURGE_NONE;
  109. /* irq for secondary cpus to report */
  110. static struct irq_host *psurge_host;
  111. int psurge_secondary_virq;
  112. /*
  113. * Set and clear IPIs for powersurge.
  114. */
  115. static inline void psurge_set_ipi(int cpu)
  116. {
  117. if (psurge_type == PSURGE_NONE)
  118. return;
  119. if (cpu == 0)
  120. in_be32(psurge_pri_intr);
  121. else if (psurge_type == PSURGE_DUAL)
  122. out_8(psurge_sec_intr, 0);
  123. else
  124. PSURGE_QUAD_OUT(PSURGE_QUAD_IRQ_SET, 1 << cpu);
  125. }
  126. static inline void psurge_clr_ipi(int cpu)
  127. {
  128. if (cpu > 0) {
  129. switch(psurge_type) {
  130. case PSURGE_DUAL:
  131. out_8(psurge_sec_intr, ~0);
  132. case PSURGE_NONE:
  133. break;
  134. default:
  135. PSURGE_QUAD_OUT(PSURGE_QUAD_IRQ_CLR, 1 << cpu);
  136. }
  137. }
  138. }
  139. /*
  140. * On powersurge (old SMP powermac architecture) we don't have
  141. * separate IPIs for separate messages like openpic does. Instead
  142. * use the generic demux helpers
  143. * -- paulus.
  144. */
  145. static irqreturn_t psurge_ipi_intr(int irq, void *d)
  146. {
  147. psurge_clr_ipi(smp_processor_id());
  148. smp_ipi_demux();
  149. return IRQ_HANDLED;
  150. }
  151. static void smp_psurge_cause_ipi(int cpu, unsigned long data)
  152. {
  153. psurge_set_ipi(cpu);
  154. }
  155. static int psurge_host_map(struct irq_host *h, unsigned int virq,
  156. irq_hw_number_t hw)
  157. {
  158. irq_set_chip_and_handler(virq, &dummy_irq_chip, handle_percpu_irq);
  159. return 0;
  160. }
  161. struct irq_host_ops psurge_host_ops = {
  162. .map = psurge_host_map,
  163. };
  164. static int psurge_secondary_ipi_init(void)
  165. {
  166. int rc = -ENOMEM;
  167. psurge_host = irq_alloc_host(NULL, IRQ_HOST_MAP_NOMAP, 0,
  168. &psurge_host_ops, 0);
  169. if (psurge_host)
  170. psurge_secondary_virq = irq_create_direct_mapping(psurge_host);
  171. if (psurge_secondary_virq)
  172. rc = request_irq(psurge_secondary_virq, psurge_ipi_intr,
  173. IRQF_DISABLED|IRQF_PERCPU, "IPI", NULL);
  174. if (rc)
  175. pr_err("Failed to setup secondary cpu IPI\n");
  176. return rc;
  177. }
  178. /*
  179. * Determine a quad card presence. We read the board ID register, we
  180. * force the data bus to change to something else, and we read it again.
  181. * It it's stable, then the register probably exist (ugh !)
  182. */
  183. static int __init psurge_quad_probe(void)
  184. {
  185. int type;
  186. unsigned int i;
  187. type = PSURGE_QUAD_IN(PSURGE_QUAD_BOARD_ID);
  188. if (type < PSURGE_QUAD_OKEE || type > PSURGE_QUAD_ICEGRASS
  189. || type != PSURGE_QUAD_IN(PSURGE_QUAD_BOARD_ID))
  190. return PSURGE_DUAL;
  191. /* looks OK, try a slightly more rigorous test */
  192. /* bogus is not necessarily cacheline-aligned,
  193. though I don't suppose that really matters. -- paulus */
  194. for (i = 0; i < 100; i++) {
  195. volatile u32 bogus[8];
  196. bogus[(0+i)%8] = 0x00000000;
  197. bogus[(1+i)%8] = 0x55555555;
  198. bogus[(2+i)%8] = 0xFFFFFFFF;
  199. bogus[(3+i)%8] = 0xAAAAAAAA;
  200. bogus[(4+i)%8] = 0x33333333;
  201. bogus[(5+i)%8] = 0xCCCCCCCC;
  202. bogus[(6+i)%8] = 0xCCCCCCCC;
  203. bogus[(7+i)%8] = 0x33333333;
  204. wmb();
  205. asm volatile("dcbf 0,%0" : : "r" (bogus) : "memory");
  206. mb();
  207. if (type != PSURGE_QUAD_IN(PSURGE_QUAD_BOARD_ID))
  208. return PSURGE_DUAL;
  209. }
  210. return type;
  211. }
  212. static void __init psurge_quad_init(void)
  213. {
  214. int procbits;
  215. if (ppc_md.progress) ppc_md.progress("psurge_quad_init", 0x351);
  216. procbits = ~PSURGE_QUAD_IN(PSURGE_QUAD_WHICH_CPU);
  217. if (psurge_type == PSURGE_QUAD_ICEGRASS)
  218. PSURGE_QUAD_BIS(PSURGE_QUAD_RESET_CTL, procbits);
  219. else
  220. PSURGE_QUAD_BIC(PSURGE_QUAD_CKSTOP_CTL, procbits);
  221. mdelay(33);
  222. out_8(psurge_sec_intr, ~0);
  223. PSURGE_QUAD_OUT(PSURGE_QUAD_IRQ_CLR, procbits);
  224. PSURGE_QUAD_BIS(PSURGE_QUAD_RESET_CTL, procbits);
  225. if (psurge_type != PSURGE_QUAD_ICEGRASS)
  226. PSURGE_QUAD_BIS(PSURGE_QUAD_CKSTOP_CTL, procbits);
  227. PSURGE_QUAD_BIC(PSURGE_QUAD_PRIMARY_ARB, procbits);
  228. mdelay(33);
  229. PSURGE_QUAD_BIC(PSURGE_QUAD_RESET_CTL, procbits);
  230. mdelay(33);
  231. PSURGE_QUAD_BIS(PSURGE_QUAD_PRIMARY_ARB, procbits);
  232. mdelay(33);
  233. }
  234. static int __init smp_psurge_probe(void)
  235. {
  236. int i, ncpus;
  237. struct device_node *dn;
  238. /* We don't do SMP on the PPC601 -- paulus */
  239. if (PVR_VER(mfspr(SPRN_PVR)) == 1)
  240. return 1;
  241. /*
  242. * The powersurge cpu board can be used in the generation
  243. * of powermacs that have a socket for an upgradeable cpu card,
  244. * including the 7500, 8500, 9500, 9600.
  245. * The device tree doesn't tell you if you have 2 cpus because
  246. * OF doesn't know anything about the 2nd processor.
  247. * Instead we look for magic bits in magic registers,
  248. * in the hammerhead memory controller in the case of the
  249. * dual-cpu powersurge board. -- paulus.
  250. */
  251. dn = of_find_node_by_name(NULL, "hammerhead");
  252. if (dn == NULL)
  253. return 1;
  254. of_node_put(dn);
  255. hhead_base = ioremap(HAMMERHEAD_BASE, 0x800);
  256. quad_base = ioremap(PSURGE_QUAD_REG_ADDR, 1024);
  257. psurge_sec_intr = hhead_base + HHEAD_SEC_INTR;
  258. psurge_type = psurge_quad_probe();
  259. if (psurge_type != PSURGE_DUAL) {
  260. psurge_quad_init();
  261. /* All released cards using this HW design have 4 CPUs */
  262. ncpus = 4;
  263. /* No sure how timebase sync works on those, let's use SW */
  264. smp_ops->give_timebase = smp_generic_give_timebase;
  265. smp_ops->take_timebase = smp_generic_take_timebase;
  266. } else {
  267. iounmap(quad_base);
  268. if ((in_8(hhead_base + HHEAD_CONFIG) & 0x02) == 0) {
  269. /* not a dual-cpu card */
  270. iounmap(hhead_base);
  271. psurge_type = PSURGE_NONE;
  272. return 1;
  273. }
  274. ncpus = 2;
  275. }
  276. if (psurge_secondary_ipi_init())
  277. return 1;
  278. psurge_start = ioremap(PSURGE_START, 4);
  279. psurge_pri_intr = ioremap(PSURGE_PRI_INTR, 4);
  280. /* This is necessary because OF doesn't know about the
  281. * secondary cpu(s), and thus there aren't nodes in the
  282. * device tree for them, and smp_setup_cpu_maps hasn't
  283. * set their bits in cpu_present_mask.
  284. */
  285. if (ncpus > NR_CPUS)
  286. ncpus = NR_CPUS;
  287. for (i = 1; i < ncpus ; ++i)
  288. set_cpu_present(i, true);
  289. if (ppc_md.progress) ppc_md.progress("smp_psurge_probe - done", 0x352);
  290. return ncpus;
  291. }
  292. static int __init smp_psurge_kick_cpu(int nr)
  293. {
  294. unsigned long start = __pa(__secondary_start_pmac_0) + nr * 8;
  295. unsigned long a, flags;
  296. int i, j;
  297. /* Defining this here is evil ... but I prefer hiding that
  298. * crap to avoid giving people ideas that they can do the
  299. * same.
  300. */
  301. extern volatile unsigned int cpu_callin_map[NR_CPUS];
  302. /* may need to flush here if secondary bats aren't setup */
  303. for (a = KERNELBASE; a < KERNELBASE + 0x800000; a += 32)
  304. asm volatile("dcbf 0,%0" : : "r" (a) : "memory");
  305. asm volatile("sync");
  306. if (ppc_md.progress) ppc_md.progress("smp_psurge_kick_cpu", 0x353);
  307. /* This is going to freeze the timeebase, we disable interrupts */
  308. local_irq_save(flags);
  309. out_be32(psurge_start, start);
  310. mb();
  311. psurge_set_ipi(nr);
  312. /*
  313. * We can't use udelay here because the timebase is now frozen.
  314. */
  315. for (i = 0; i < 2000; ++i)
  316. asm volatile("nop" : : : "memory");
  317. psurge_clr_ipi(nr);
  318. /*
  319. * Also, because the timebase is frozen, we must not return to the
  320. * caller which will try to do udelay's etc... Instead, we wait -here-
  321. * for the CPU to callin.
  322. */
  323. for (i = 0; i < 100000 && !cpu_callin_map[nr]; ++i) {
  324. for (j = 1; j < 10000; j++)
  325. asm volatile("nop" : : : "memory");
  326. asm volatile("sync" : : : "memory");
  327. }
  328. if (!cpu_callin_map[nr])
  329. goto stuck;
  330. /* And we do the TB sync here too for standard dual CPU cards */
  331. if (psurge_type == PSURGE_DUAL) {
  332. while(!tb_req)
  333. barrier();
  334. tb_req = 0;
  335. mb();
  336. timebase = get_tb();
  337. mb();
  338. while (timebase)
  339. barrier();
  340. mb();
  341. }
  342. stuck:
  343. /* now interrupt the secondary, restarting both TBs */
  344. if (psurge_type == PSURGE_DUAL)
  345. psurge_set_ipi(1);
  346. if (ppc_md.progress) ppc_md.progress("smp_psurge_kick_cpu - done", 0x354);
  347. return 0;
  348. }
  349. static struct irqaction psurge_irqaction = {
  350. .handler = psurge_ipi_intr,
  351. .flags = IRQF_DISABLED|IRQF_PERCPU,
  352. .name = "primary IPI",
  353. };
  354. static void __init smp_psurge_setup_cpu(int cpu_nr)
  355. {
  356. if (cpu_nr != 0)
  357. return;
  358. /* reset the entry point so if we get another intr we won't
  359. * try to startup again */
  360. out_be32(psurge_start, 0x100);
  361. if (setup_irq(irq_create_mapping(NULL, 30), &psurge_irqaction))
  362. printk(KERN_ERR "Couldn't get primary IPI interrupt");
  363. }
  364. void __init smp_psurge_take_timebase(void)
  365. {
  366. if (psurge_type != PSURGE_DUAL)
  367. return;
  368. tb_req = 1;
  369. mb();
  370. while (!timebase)
  371. barrier();
  372. mb();
  373. set_tb(timebase >> 32, timebase & 0xffffffff);
  374. timebase = 0;
  375. mb();
  376. set_dec(tb_ticks_per_jiffy/2);
  377. }
  378. void __init smp_psurge_give_timebase(void)
  379. {
  380. /* Nothing to do here */
  381. }
  382. /* PowerSurge-style Macs */
  383. struct smp_ops_t psurge_smp_ops = {
  384. .message_pass = NULL, /* Use smp_muxed_ipi_message_pass */
  385. .cause_ipi = smp_psurge_cause_ipi,
  386. .probe = smp_psurge_probe,
  387. .kick_cpu = smp_psurge_kick_cpu,
  388. .setup_cpu = smp_psurge_setup_cpu,
  389. .give_timebase = smp_psurge_give_timebase,
  390. .take_timebase = smp_psurge_take_timebase,
  391. };
  392. #endif /* CONFIG_PPC_PMAC32_PSURGE */
  393. /*
  394. * Core 99 and later support
  395. */
  396. static void smp_core99_give_timebase(void)
  397. {
  398. unsigned long flags;
  399. local_irq_save(flags);
  400. while(!tb_req)
  401. barrier();
  402. tb_req = 0;
  403. (*pmac_tb_freeze)(1);
  404. mb();
  405. timebase = get_tb();
  406. mb();
  407. while (timebase)
  408. barrier();
  409. mb();
  410. (*pmac_tb_freeze)(0);
  411. mb();
  412. local_irq_restore(flags);
  413. }
  414. static void __devinit smp_core99_take_timebase(void)
  415. {
  416. unsigned long flags;
  417. local_irq_save(flags);
  418. tb_req = 1;
  419. mb();
  420. while (!timebase)
  421. barrier();
  422. mb();
  423. set_tb(timebase >> 32, timebase & 0xffffffff);
  424. timebase = 0;
  425. mb();
  426. local_irq_restore(flags);
  427. }
  428. #ifdef CONFIG_PPC64
  429. /*
  430. * G5s enable/disable the timebase via an i2c-connected clock chip.
  431. */
  432. static struct pmac_i2c_bus *pmac_tb_clock_chip_host;
  433. static u8 pmac_tb_pulsar_addr;
  434. static void smp_core99_cypress_tb_freeze(int freeze)
  435. {
  436. u8 data;
  437. int rc;
  438. /* Strangely, the device-tree says address is 0xd2, but darwin
  439. * accesses 0xd0 ...
  440. */
  441. pmac_i2c_setmode(pmac_tb_clock_chip_host,
  442. pmac_i2c_mode_combined);
  443. rc = pmac_i2c_xfer(pmac_tb_clock_chip_host,
  444. 0xd0 | pmac_i2c_read,
  445. 1, 0x81, &data, 1);
  446. if (rc != 0)
  447. goto bail;
  448. data = (data & 0xf3) | (freeze ? 0x00 : 0x0c);
  449. pmac_i2c_setmode(pmac_tb_clock_chip_host, pmac_i2c_mode_stdsub);
  450. rc = pmac_i2c_xfer(pmac_tb_clock_chip_host,
  451. 0xd0 | pmac_i2c_write,
  452. 1, 0x81, &data, 1);
  453. bail:
  454. if (rc != 0) {
  455. printk("Cypress Timebase %s rc: %d\n",
  456. freeze ? "freeze" : "unfreeze", rc);
  457. panic("Timebase freeze failed !\n");
  458. }
  459. }
  460. static void smp_core99_pulsar_tb_freeze(int freeze)
  461. {
  462. u8 data;
  463. int rc;
  464. pmac_i2c_setmode(pmac_tb_clock_chip_host,
  465. pmac_i2c_mode_combined);
  466. rc = pmac_i2c_xfer(pmac_tb_clock_chip_host,
  467. pmac_tb_pulsar_addr | pmac_i2c_read,
  468. 1, 0x2e, &data, 1);
  469. if (rc != 0)
  470. goto bail;
  471. data = (data & 0x88) | (freeze ? 0x11 : 0x22);
  472. pmac_i2c_setmode(pmac_tb_clock_chip_host, pmac_i2c_mode_stdsub);
  473. rc = pmac_i2c_xfer(pmac_tb_clock_chip_host,
  474. pmac_tb_pulsar_addr | pmac_i2c_write,
  475. 1, 0x2e, &data, 1);
  476. bail:
  477. if (rc != 0) {
  478. printk(KERN_ERR "Pulsar Timebase %s rc: %d\n",
  479. freeze ? "freeze" : "unfreeze", rc);
  480. panic("Timebase freeze failed !\n");
  481. }
  482. }
  483. static void __init smp_core99_setup_i2c_hwsync(int ncpus)
  484. {
  485. struct device_node *cc = NULL;
  486. struct device_node *p;
  487. const char *name = NULL;
  488. const u32 *reg;
  489. int ok;
  490. /* Look for the clock chip */
  491. while ((cc = of_find_node_by_name(cc, "i2c-hwclock")) != NULL) {
  492. p = of_get_parent(cc);
  493. ok = p && of_device_is_compatible(p, "uni-n-i2c");
  494. of_node_put(p);
  495. if (!ok)
  496. continue;
  497. pmac_tb_clock_chip_host = pmac_i2c_find_bus(cc);
  498. if (pmac_tb_clock_chip_host == NULL)
  499. continue;
  500. reg = of_get_property(cc, "reg", NULL);
  501. if (reg == NULL)
  502. continue;
  503. switch (*reg) {
  504. case 0xd2:
  505. if (of_device_is_compatible(cc,"pulsar-legacy-slewing")) {
  506. pmac_tb_freeze = smp_core99_pulsar_tb_freeze;
  507. pmac_tb_pulsar_addr = 0xd2;
  508. name = "Pulsar";
  509. } else if (of_device_is_compatible(cc, "cy28508")) {
  510. pmac_tb_freeze = smp_core99_cypress_tb_freeze;
  511. name = "Cypress";
  512. }
  513. break;
  514. case 0xd4:
  515. pmac_tb_freeze = smp_core99_pulsar_tb_freeze;
  516. pmac_tb_pulsar_addr = 0xd4;
  517. name = "Pulsar";
  518. break;
  519. }
  520. if (pmac_tb_freeze != NULL)
  521. break;
  522. }
  523. if (pmac_tb_freeze != NULL) {
  524. /* Open i2c bus for synchronous access */
  525. if (pmac_i2c_open(pmac_tb_clock_chip_host, 1)) {
  526. printk(KERN_ERR "Failed top open i2c bus for clock"
  527. " sync, fallback to software sync !\n");
  528. goto no_i2c_sync;
  529. }
  530. printk(KERN_INFO "Processor timebase sync using %s i2c clock\n",
  531. name);
  532. return;
  533. }
  534. no_i2c_sync:
  535. pmac_tb_freeze = NULL;
  536. pmac_tb_clock_chip_host = NULL;
  537. }
  538. /*
  539. * Newer G5s uses a platform function
  540. */
  541. static void smp_core99_pfunc_tb_freeze(int freeze)
  542. {
  543. struct device_node *cpus;
  544. struct pmf_args args;
  545. cpus = of_find_node_by_path("/cpus");
  546. BUG_ON(cpus == NULL);
  547. args.count = 1;
  548. args.u[0].v = !freeze;
  549. pmf_call_function(cpus, "cpu-timebase", &args);
  550. of_node_put(cpus);
  551. }
  552. #else /* CONFIG_PPC64 */
  553. /*
  554. * SMP G4 use a GPIO to enable/disable the timebase.
  555. */
  556. static unsigned int core99_tb_gpio; /* Timebase freeze GPIO */
  557. static void smp_core99_gpio_tb_freeze(int freeze)
  558. {
  559. if (freeze)
  560. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, core99_tb_gpio, 4);
  561. else
  562. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, core99_tb_gpio, 0);
  563. pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, core99_tb_gpio, 0);
  564. }
  565. #endif /* !CONFIG_PPC64 */
  566. /* L2 and L3 cache settings to pass from CPU0 to CPU1 on G4 cpus */
  567. volatile static long int core99_l2_cache;
  568. volatile static long int core99_l3_cache;
  569. static void __devinit core99_init_caches(int cpu)
  570. {
  571. #ifndef CONFIG_PPC64
  572. if (!cpu_has_feature(CPU_FTR_L2CR))
  573. return;
  574. if (cpu == 0) {
  575. core99_l2_cache = _get_L2CR();
  576. printk("CPU0: L2CR is %lx\n", core99_l2_cache);
  577. } else {
  578. printk("CPU%d: L2CR was %lx\n", cpu, _get_L2CR());
  579. _set_L2CR(0);
  580. _set_L2CR(core99_l2_cache);
  581. printk("CPU%d: L2CR set to %lx\n", cpu, core99_l2_cache);
  582. }
  583. if (!cpu_has_feature(CPU_FTR_L3CR))
  584. return;
  585. if (cpu == 0){
  586. core99_l3_cache = _get_L3CR();
  587. printk("CPU0: L3CR is %lx\n", core99_l3_cache);
  588. } else {
  589. printk("CPU%d: L3CR was %lx\n", cpu, _get_L3CR());
  590. _set_L3CR(0);
  591. _set_L3CR(core99_l3_cache);
  592. printk("CPU%d: L3CR set to %lx\n", cpu, core99_l3_cache);
  593. }
  594. #endif /* !CONFIG_PPC64 */
  595. }
  596. static void __init smp_core99_setup(int ncpus)
  597. {
  598. #ifdef CONFIG_PPC64
  599. /* i2c based HW sync on some G5s */
  600. if (of_machine_is_compatible("PowerMac7,2") ||
  601. of_machine_is_compatible("PowerMac7,3") ||
  602. of_machine_is_compatible("RackMac3,1"))
  603. smp_core99_setup_i2c_hwsync(ncpus);
  604. /* pfunc based HW sync on recent G5s */
  605. if (pmac_tb_freeze == NULL) {
  606. struct device_node *cpus =
  607. of_find_node_by_path("/cpus");
  608. if (cpus &&
  609. of_get_property(cpus, "platform-cpu-timebase", NULL)) {
  610. pmac_tb_freeze = smp_core99_pfunc_tb_freeze;
  611. printk(KERN_INFO "Processor timebase sync using"
  612. " platform function\n");
  613. }
  614. }
  615. #else /* CONFIG_PPC64 */
  616. /* GPIO based HW sync on ppc32 Core99 */
  617. if (pmac_tb_freeze == NULL && !of_machine_is_compatible("MacRISC4")) {
  618. struct device_node *cpu;
  619. const u32 *tbprop = NULL;
  620. core99_tb_gpio = KL_GPIO_TB_ENABLE; /* default value */
  621. cpu = of_find_node_by_type(NULL, "cpu");
  622. if (cpu != NULL) {
  623. tbprop = of_get_property(cpu, "timebase-enable", NULL);
  624. if (tbprop)
  625. core99_tb_gpio = *tbprop;
  626. of_node_put(cpu);
  627. }
  628. pmac_tb_freeze = smp_core99_gpio_tb_freeze;
  629. printk(KERN_INFO "Processor timebase sync using"
  630. " GPIO 0x%02x\n", core99_tb_gpio);
  631. }
  632. #endif /* CONFIG_PPC64 */
  633. /* No timebase sync, fallback to software */
  634. if (pmac_tb_freeze == NULL) {
  635. smp_ops->give_timebase = smp_generic_give_timebase;
  636. smp_ops->take_timebase = smp_generic_take_timebase;
  637. printk(KERN_INFO "Processor timebase sync using software\n");
  638. }
  639. #ifndef CONFIG_PPC64
  640. {
  641. int i;
  642. /* XXX should get this from reg properties */
  643. for (i = 1; i < ncpus; ++i)
  644. set_hard_smp_processor_id(i, i);
  645. }
  646. #endif
  647. /* 32 bits SMP can't NAP */
  648. if (!of_machine_is_compatible("MacRISC4"))
  649. powersave_nap = 0;
  650. }
  651. static int __init smp_core99_probe(void)
  652. {
  653. struct device_node *cpus;
  654. int ncpus = 0;
  655. if (ppc_md.progress) ppc_md.progress("smp_core99_probe", 0x345);
  656. /* Count CPUs in the device-tree */
  657. for (cpus = NULL; (cpus = of_find_node_by_type(cpus, "cpu")) != NULL;)
  658. ++ncpus;
  659. printk(KERN_INFO "PowerMac SMP probe found %d cpus\n", ncpus);
  660. /* Nothing more to do if less than 2 of them */
  661. if (ncpus <= 1)
  662. return 1;
  663. /* We need to perform some early initialisations before we can start
  664. * setting up SMP as we are running before initcalls
  665. */
  666. pmac_pfunc_base_install();
  667. pmac_i2c_init();
  668. /* Setup various bits like timebase sync method, ability to nap, ... */
  669. smp_core99_setup(ncpus);
  670. /* Install IPIs */
  671. mpic_request_ipis();
  672. /* Collect l2cr and l3cr values from CPU 0 */
  673. core99_init_caches(0);
  674. return ncpus;
  675. }
  676. static int __devinit smp_core99_kick_cpu(int nr)
  677. {
  678. unsigned int save_vector;
  679. unsigned long target, flags;
  680. unsigned int *vector = (unsigned int *)(PAGE_OFFSET+0x100);
  681. if (nr < 0 || nr > 3)
  682. return -ENOENT;
  683. if (ppc_md.progress)
  684. ppc_md.progress("smp_core99_kick_cpu", 0x346);
  685. local_irq_save(flags);
  686. /* Save reset vector */
  687. save_vector = *vector;
  688. /* Setup fake reset vector that does
  689. * b __secondary_start_pmac_0 + nr*8
  690. */
  691. target = (unsigned long) __secondary_start_pmac_0 + nr * 8;
  692. patch_branch(vector, target, BRANCH_SET_LINK);
  693. /* Put some life in our friend */
  694. pmac_call_feature(PMAC_FTR_RESET_CPU, NULL, nr, 0);
  695. /* FIXME: We wait a bit for the CPU to take the exception, I should
  696. * instead wait for the entry code to set something for me. Well,
  697. * ideally, all that crap will be done in prom.c and the CPU left
  698. * in a RAM-based wait loop like CHRP.
  699. */
  700. mdelay(1);
  701. /* Restore our exception vector */
  702. *vector = save_vector;
  703. flush_icache_range((unsigned long) vector, (unsigned long) vector + 4);
  704. local_irq_restore(flags);
  705. if (ppc_md.progress) ppc_md.progress("smp_core99_kick_cpu done", 0x347);
  706. return 0;
  707. }
  708. static void __devinit smp_core99_setup_cpu(int cpu_nr)
  709. {
  710. /* Setup L2/L3 */
  711. if (cpu_nr != 0)
  712. core99_init_caches(cpu_nr);
  713. /* Setup openpic */
  714. mpic_setup_this_cpu();
  715. }
  716. #ifdef CONFIG_PPC64
  717. #ifdef CONFIG_HOTPLUG_CPU
  718. static int smp_core99_cpu_notify(struct notifier_block *self,
  719. unsigned long action, void *hcpu)
  720. {
  721. int rc;
  722. switch(action) {
  723. case CPU_UP_PREPARE:
  724. case CPU_UP_PREPARE_FROZEN:
  725. /* Open i2c bus if it was used for tb sync */
  726. if (pmac_tb_clock_chip_host) {
  727. rc = pmac_i2c_open(pmac_tb_clock_chip_host, 1);
  728. if (rc) {
  729. pr_err("Failed to open i2c bus for time sync\n");
  730. return notifier_from_errno(rc);
  731. }
  732. }
  733. break;
  734. case CPU_ONLINE:
  735. case CPU_UP_CANCELED:
  736. /* Close i2c bus if it was used for tb sync */
  737. if (pmac_tb_clock_chip_host)
  738. pmac_i2c_close(pmac_tb_clock_chip_host);
  739. break;
  740. default:
  741. break;
  742. }
  743. return NOTIFY_OK;
  744. }
  745. static struct notifier_block __cpuinitdata smp_core99_cpu_nb = {
  746. .notifier_call = smp_core99_cpu_notify,
  747. };
  748. #endif /* CONFIG_HOTPLUG_CPU */
  749. static void __init smp_core99_bringup_done(void)
  750. {
  751. extern void g5_phy_disable_cpu1(void);
  752. /* Close i2c bus if it was used for tb sync */
  753. if (pmac_tb_clock_chip_host)
  754. pmac_i2c_close(pmac_tb_clock_chip_host);
  755. /* If we didn't start the second CPU, we must take
  756. * it off the bus.
  757. */
  758. if (of_machine_is_compatible("MacRISC4") &&
  759. num_online_cpus() < 2) {
  760. set_cpu_present(1, false);
  761. g5_phy_disable_cpu1();
  762. }
  763. #ifdef CONFIG_HOTPLUG_CPU
  764. register_cpu_notifier(&smp_core99_cpu_nb);
  765. #endif
  766. if (ppc_md.progress)
  767. ppc_md.progress("smp_core99_bringup_done", 0x349);
  768. }
  769. #endif /* CONFIG_PPC64 */
  770. #ifdef CONFIG_HOTPLUG_CPU
  771. static int smp_core99_cpu_disable(void)
  772. {
  773. int rc = generic_cpu_disable();
  774. if (rc)
  775. return rc;
  776. mpic_cpu_set_priority(0xf);
  777. return 0;
  778. }
  779. #ifdef CONFIG_PPC32
  780. static void pmac_cpu_die(void)
  781. {
  782. int cpu = smp_processor_id();
  783. local_irq_disable();
  784. idle_task_exit();
  785. pr_debug("CPU%d offline\n", cpu);
  786. generic_set_cpu_dead(cpu);
  787. smp_wmb();
  788. mb();
  789. low_cpu_die();
  790. }
  791. #else /* CONFIG_PPC32 */
  792. static void pmac_cpu_die(void)
  793. {
  794. int cpu = smp_processor_id();
  795. local_irq_disable();
  796. idle_task_exit();
  797. /*
  798. * turn off as much as possible, we'll be
  799. * kicked out as this will only be invoked
  800. * on core99 platforms for now ...
  801. */
  802. printk(KERN_INFO "CPU#%d offline\n", cpu);
  803. generic_set_cpu_dead(cpu);
  804. smp_wmb();
  805. /*
  806. * Re-enable interrupts. The NAP code needs to enable them
  807. * anyways, do it now so we deal with the case where one already
  808. * happened while soft-disabled.
  809. * We shouldn't get any external interrupts, only decrementer, and the
  810. * decrementer handler is safe for use on offline CPUs
  811. */
  812. local_irq_enable();
  813. while (1) {
  814. /* let's not take timer interrupts too often ... */
  815. set_dec(0x7fffffff);
  816. /* Enter NAP mode */
  817. power4_idle();
  818. }
  819. }
  820. #endif /* else CONFIG_PPC32 */
  821. #endif /* CONFIG_HOTPLUG_CPU */
  822. /* Core99 Macs (dual G4s and G5s) */
  823. struct smp_ops_t core99_smp_ops = {
  824. .message_pass = smp_mpic_message_pass,
  825. .probe = smp_core99_probe,
  826. #ifdef CONFIG_PPC64
  827. .bringup_done = smp_core99_bringup_done,
  828. #endif
  829. .kick_cpu = smp_core99_kick_cpu,
  830. .setup_cpu = smp_core99_setup_cpu,
  831. .give_timebase = smp_core99_give_timebase,
  832. .take_timebase = smp_core99_take_timebase,
  833. #if defined(CONFIG_HOTPLUG_CPU)
  834. .cpu_disable = smp_core99_cpu_disable,
  835. .cpu_die = generic_cpu_die,
  836. #endif
  837. };
  838. void __init pmac_setup_smp(void)
  839. {
  840. struct device_node *np;
  841. /* Check for Core99 */
  842. np = of_find_node_by_name(NULL, "uni-n");
  843. if (!np)
  844. np = of_find_node_by_name(NULL, "u3");
  845. if (!np)
  846. np = of_find_node_by_name(NULL, "u4");
  847. if (np) {
  848. of_node_put(np);
  849. smp_ops = &core99_smp_ops;
  850. }
  851. #ifdef CONFIG_PPC_PMAC32_PSURGE
  852. else {
  853. /* We have to set bits in cpu_possible_mask here since the
  854. * secondary CPU(s) aren't in the device tree. Various
  855. * things won't be initialized for CPUs not in the possible
  856. * map, so we really need to fix it up here.
  857. */
  858. int cpu;
  859. for (cpu = 1; cpu < 4 && cpu < NR_CPUS; ++cpu)
  860. set_cpu_possible(cpu, true);
  861. smp_ops = &psurge_smp_ops;
  862. }
  863. #endif /* CONFIG_PPC_PMAC32_PSURGE */
  864. #ifdef CONFIG_HOTPLUG_CPU
  865. ppc_md.cpu_die = pmac_cpu_die;
  866. #endif
  867. }