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