smp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924
  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 <asm/atomic.h>
  38. #include <asm/irq.h>
  39. #include <asm/page.h>
  40. #include <asm/pgtable.h>
  41. #include <asm/sections.h>
  42. #include <asm/io.h>
  43. #include <asm/prom.h>
  44. #include <asm/smp.h>
  45. #include <asm/machdep.h>
  46. #include <asm/pmac_feature.h>
  47. #include <asm/time.h>
  48. #include <asm/mpic.h>
  49. #include <asm/cacheflush.h>
  50. #include <asm/keylargo.h>
  51. #include <asm/pmac_low_i2c.h>
  52. #include <asm/pmac_pfunc.h>
  53. #define DEBUG
  54. #ifdef DEBUG
  55. #define DBG(fmt...) udbg_printf(fmt)
  56. #else
  57. #define DBG(fmt...)
  58. #endif
  59. extern void __secondary_start_pmac_0(void);
  60. extern int pmac_pfunc_base_install(void);
  61. #ifdef CONFIG_PPC32
  62. /* Sync flag for HW tb sync */
  63. static volatile int sec_tb_reset = 0;
  64. /*
  65. * Powersurge (old powermac SMP) support.
  66. */
  67. /* Addresses for powersurge registers */
  68. #define HAMMERHEAD_BASE 0xf8000000
  69. #define HHEAD_CONFIG 0x90
  70. #define HHEAD_SEC_INTR 0xc0
  71. /* register for interrupting the primary processor on the powersurge */
  72. /* N.B. this is actually the ethernet ROM! */
  73. #define PSURGE_PRI_INTR 0xf3019000
  74. /* register for storing the start address for the secondary processor */
  75. /* N.B. this is the PCI config space address register for the 1st bridge */
  76. #define PSURGE_START 0xf2800000
  77. /* Daystar/XLR8 4-CPU card */
  78. #define PSURGE_QUAD_REG_ADDR 0xf8800000
  79. #define PSURGE_QUAD_IRQ_SET 0
  80. #define PSURGE_QUAD_IRQ_CLR 1
  81. #define PSURGE_QUAD_IRQ_PRIMARY 2
  82. #define PSURGE_QUAD_CKSTOP_CTL 3
  83. #define PSURGE_QUAD_PRIMARY_ARB 4
  84. #define PSURGE_QUAD_BOARD_ID 6
  85. #define PSURGE_QUAD_WHICH_CPU 7
  86. #define PSURGE_QUAD_CKSTOP_RDBK 8
  87. #define PSURGE_QUAD_RESET_CTL 11
  88. #define PSURGE_QUAD_OUT(r, v) (out_8(quad_base + ((r) << 4) + 4, (v)))
  89. #define PSURGE_QUAD_IN(r) (in_8(quad_base + ((r) << 4) + 4) & 0x0f)
  90. #define PSURGE_QUAD_BIS(r, v) (PSURGE_QUAD_OUT((r), PSURGE_QUAD_IN(r) | (v)))
  91. #define PSURGE_QUAD_BIC(r, v) (PSURGE_QUAD_OUT((r), PSURGE_QUAD_IN(r) & ~(v)))
  92. /* virtual addresses for the above */
  93. static volatile u8 __iomem *hhead_base;
  94. static volatile u8 __iomem *quad_base;
  95. static volatile u32 __iomem *psurge_pri_intr;
  96. static volatile u8 __iomem *psurge_sec_intr;
  97. static volatile u32 __iomem *psurge_start;
  98. /* values for psurge_type */
  99. #define PSURGE_NONE -1
  100. #define PSURGE_DUAL 0
  101. #define PSURGE_QUAD_OKEE 1
  102. #define PSURGE_QUAD_COTTON 2
  103. #define PSURGE_QUAD_ICEGRASS 3
  104. /* what sort of powersurge board we have */
  105. static int psurge_type = PSURGE_NONE;
  106. /*
  107. * Set and clear IPIs for powersurge.
  108. */
  109. static inline void psurge_set_ipi(int cpu)
  110. {
  111. if (psurge_type == PSURGE_NONE)
  112. return;
  113. if (cpu == 0)
  114. in_be32(psurge_pri_intr);
  115. else if (psurge_type == PSURGE_DUAL)
  116. out_8(psurge_sec_intr, 0);
  117. else
  118. PSURGE_QUAD_OUT(PSURGE_QUAD_IRQ_SET, 1 << cpu);
  119. }
  120. static inline void psurge_clr_ipi(int cpu)
  121. {
  122. if (cpu > 0) {
  123. switch(psurge_type) {
  124. case PSURGE_DUAL:
  125. out_8(psurge_sec_intr, ~0);
  126. case PSURGE_NONE:
  127. break;
  128. default:
  129. PSURGE_QUAD_OUT(PSURGE_QUAD_IRQ_CLR, 1 << cpu);
  130. }
  131. }
  132. }
  133. /*
  134. * On powersurge (old SMP powermac architecture) we don't have
  135. * separate IPIs for separate messages like openpic does. Instead
  136. * we have a bitmap for each processor, where a 1 bit means that
  137. * the corresponding message is pending for that processor.
  138. * Ideally each cpu's entry would be in a different cache line.
  139. * -- paulus.
  140. */
  141. static unsigned long psurge_smp_message[NR_CPUS];
  142. void psurge_smp_message_recv(void)
  143. {
  144. int cpu = smp_processor_id();
  145. int msg;
  146. /* clear interrupt */
  147. psurge_clr_ipi(cpu);
  148. if (num_online_cpus() < 2)
  149. return;
  150. /* make sure there is a message there */
  151. for (msg = 0; msg < 4; msg++)
  152. if (test_and_clear_bit(msg, &psurge_smp_message[cpu]))
  153. smp_message_recv(msg);
  154. }
  155. irqreturn_t psurge_primary_intr(int irq, void *d)
  156. {
  157. psurge_smp_message_recv();
  158. return IRQ_HANDLED;
  159. }
  160. static void smp_psurge_message_pass(int target, int msg)
  161. {
  162. int i;
  163. if (num_online_cpus() < 2)
  164. return;
  165. for_each_online_cpu(i) {
  166. if (target == MSG_ALL
  167. || (target == MSG_ALL_BUT_SELF && i != smp_processor_id())
  168. || target == i) {
  169. set_bit(msg, &psurge_smp_message[i]);
  170. psurge_set_ipi(i);
  171. }
  172. }
  173. }
  174. /*
  175. * Determine a quad card presence. We read the board ID register, we
  176. * force the data bus to change to something else, and we read it again.
  177. * It it's stable, then the register probably exist (ugh !)
  178. */
  179. static int __init psurge_quad_probe(void)
  180. {
  181. int type;
  182. unsigned int i;
  183. type = PSURGE_QUAD_IN(PSURGE_QUAD_BOARD_ID);
  184. if (type < PSURGE_QUAD_OKEE || type > PSURGE_QUAD_ICEGRASS
  185. || type != PSURGE_QUAD_IN(PSURGE_QUAD_BOARD_ID))
  186. return PSURGE_DUAL;
  187. /* looks OK, try a slightly more rigorous test */
  188. /* bogus is not necessarily cacheline-aligned,
  189. though I don't suppose that really matters. -- paulus */
  190. for (i = 0; i < 100; i++) {
  191. volatile u32 bogus[8];
  192. bogus[(0+i)%8] = 0x00000000;
  193. bogus[(1+i)%8] = 0x55555555;
  194. bogus[(2+i)%8] = 0xFFFFFFFF;
  195. bogus[(3+i)%8] = 0xAAAAAAAA;
  196. bogus[(4+i)%8] = 0x33333333;
  197. bogus[(5+i)%8] = 0xCCCCCCCC;
  198. bogus[(6+i)%8] = 0xCCCCCCCC;
  199. bogus[(7+i)%8] = 0x33333333;
  200. wmb();
  201. asm volatile("dcbf 0,%0" : : "r" (bogus) : "memory");
  202. mb();
  203. if (type != PSURGE_QUAD_IN(PSURGE_QUAD_BOARD_ID))
  204. return PSURGE_DUAL;
  205. }
  206. return type;
  207. }
  208. static void __init psurge_quad_init(void)
  209. {
  210. int procbits;
  211. if (ppc_md.progress) ppc_md.progress("psurge_quad_init", 0x351);
  212. procbits = ~PSURGE_QUAD_IN(PSURGE_QUAD_WHICH_CPU);
  213. if (psurge_type == PSURGE_QUAD_ICEGRASS)
  214. PSURGE_QUAD_BIS(PSURGE_QUAD_RESET_CTL, procbits);
  215. else
  216. PSURGE_QUAD_BIC(PSURGE_QUAD_CKSTOP_CTL, procbits);
  217. mdelay(33);
  218. out_8(psurge_sec_intr, ~0);
  219. PSURGE_QUAD_OUT(PSURGE_QUAD_IRQ_CLR, procbits);
  220. PSURGE_QUAD_BIS(PSURGE_QUAD_RESET_CTL, procbits);
  221. if (psurge_type != PSURGE_QUAD_ICEGRASS)
  222. PSURGE_QUAD_BIS(PSURGE_QUAD_CKSTOP_CTL, procbits);
  223. PSURGE_QUAD_BIC(PSURGE_QUAD_PRIMARY_ARB, procbits);
  224. mdelay(33);
  225. PSURGE_QUAD_BIC(PSURGE_QUAD_RESET_CTL, procbits);
  226. mdelay(33);
  227. PSURGE_QUAD_BIS(PSURGE_QUAD_PRIMARY_ARB, procbits);
  228. mdelay(33);
  229. }
  230. static int __init smp_psurge_probe(void)
  231. {
  232. int i, ncpus;
  233. struct device_node *dn;
  234. /* We don't do SMP on the PPC601 -- paulus */
  235. if (PVR_VER(mfspr(SPRN_PVR)) == 1)
  236. return 1;
  237. /*
  238. * The powersurge cpu board can be used in the generation
  239. * of powermacs that have a socket for an upgradeable cpu card,
  240. * including the 7500, 8500, 9500, 9600.
  241. * The device tree doesn't tell you if you have 2 cpus because
  242. * OF doesn't know anything about the 2nd processor.
  243. * Instead we look for magic bits in magic registers,
  244. * in the hammerhead memory controller in the case of the
  245. * dual-cpu powersurge board. -- paulus.
  246. */
  247. dn = of_find_node_by_name(NULL, "hammerhead");
  248. if (dn == NULL)
  249. return 1;
  250. of_node_put(dn);
  251. hhead_base = ioremap(HAMMERHEAD_BASE, 0x800);
  252. quad_base = ioremap(PSURGE_QUAD_REG_ADDR, 1024);
  253. psurge_sec_intr = hhead_base + HHEAD_SEC_INTR;
  254. psurge_type = psurge_quad_probe();
  255. if (psurge_type != PSURGE_DUAL) {
  256. psurge_quad_init();
  257. /* All released cards using this HW design have 4 CPUs */
  258. ncpus = 4;
  259. } else {
  260. iounmap(quad_base);
  261. if ((in_8(hhead_base + HHEAD_CONFIG) & 0x02) == 0) {
  262. /* not a dual-cpu card */
  263. iounmap(hhead_base);
  264. psurge_type = PSURGE_NONE;
  265. return 1;
  266. }
  267. ncpus = 2;
  268. }
  269. psurge_start = ioremap(PSURGE_START, 4);
  270. psurge_pri_intr = ioremap(PSURGE_PRI_INTR, 4);
  271. /*
  272. * This is necessary because OF doesn't know about the
  273. * secondary cpu(s), and thus there aren't nodes in the
  274. * device tree for them, and smp_setup_cpu_maps hasn't
  275. * set their bits in cpu_possible_map and cpu_present_map.
  276. */
  277. if (ncpus > NR_CPUS)
  278. ncpus = NR_CPUS;
  279. for (i = 1; i < ncpus ; ++i) {
  280. cpu_set(i, cpu_present_map);
  281. cpu_set(i, cpu_possible_map);
  282. set_hard_smp_processor_id(i, i);
  283. }
  284. if (ppc_md.progress) ppc_md.progress("smp_psurge_probe - done", 0x352);
  285. return ncpus;
  286. }
  287. static void __init smp_psurge_kick_cpu(int nr)
  288. {
  289. unsigned long start = __pa(__secondary_start_pmac_0) + nr * 8;
  290. unsigned long a;
  291. int i;
  292. /* may need to flush here if secondary bats aren't setup */
  293. for (a = KERNELBASE; a < KERNELBASE + 0x800000; a += 32)
  294. asm volatile("dcbf 0,%0" : : "r" (a) : "memory");
  295. asm volatile("sync");
  296. if (ppc_md.progress) ppc_md.progress("smp_psurge_kick_cpu", 0x353);
  297. out_be32(psurge_start, start);
  298. mb();
  299. psurge_set_ipi(nr);
  300. /*
  301. * We can't use udelay here because the timebase is now frozen.
  302. */
  303. for (i = 0; i < 2000; ++i)
  304. barrier();
  305. psurge_clr_ipi(nr);
  306. if (ppc_md.progress) ppc_md.progress("smp_psurge_kick_cpu - done", 0x354);
  307. }
  308. /*
  309. * With the dual-cpu powersurge board, the decrementers and timebases
  310. * of both cpus are frozen after the secondary cpu is started up,
  311. * until we give the secondary cpu another interrupt. This routine
  312. * uses this to get the timebases synchronized.
  313. * -- paulus.
  314. */
  315. static void __init psurge_dual_sync_tb(int cpu_nr)
  316. {
  317. int t;
  318. set_dec(tb_ticks_per_jiffy);
  319. /* XXX fixme */
  320. set_tb(0, 0);
  321. if (cpu_nr > 0) {
  322. mb();
  323. sec_tb_reset = 1;
  324. return;
  325. }
  326. /* wait for the secondary to have reset its TB before proceeding */
  327. for (t = 10000000; t > 0 && !sec_tb_reset; --t)
  328. ;
  329. /* now interrupt the secondary, starting both TBs */
  330. psurge_set_ipi(1);
  331. }
  332. static struct irqaction psurge_irqaction = {
  333. .handler = psurge_primary_intr,
  334. .flags = IRQF_DISABLED,
  335. .mask = CPU_MASK_NONE,
  336. .name = "primary IPI",
  337. };
  338. static void __init smp_psurge_setup_cpu(int cpu_nr)
  339. {
  340. if (cpu_nr == 0) {
  341. /* If we failed to start the second CPU, we should still
  342. * send it an IPI to start the timebase & DEC or we might
  343. * have them stuck.
  344. */
  345. if (num_online_cpus() < 2) {
  346. if (psurge_type == PSURGE_DUAL)
  347. psurge_set_ipi(1);
  348. return;
  349. }
  350. /* reset the entry point so if we get another intr we won't
  351. * try to startup again */
  352. out_be32(psurge_start, 0x100);
  353. if (setup_irq(30, &psurge_irqaction))
  354. printk(KERN_ERR "Couldn't get primary IPI interrupt");
  355. }
  356. if (psurge_type == PSURGE_DUAL)
  357. psurge_dual_sync_tb(cpu_nr);
  358. }
  359. void __init smp_psurge_take_timebase(void)
  360. {
  361. /* Dummy implementation */
  362. }
  363. void __init smp_psurge_give_timebase(void)
  364. {
  365. /* Dummy implementation */
  366. }
  367. /* PowerSurge-style Macs */
  368. struct smp_ops_t psurge_smp_ops = {
  369. .message_pass = smp_psurge_message_pass,
  370. .probe = smp_psurge_probe,
  371. .kick_cpu = smp_psurge_kick_cpu,
  372. .setup_cpu = smp_psurge_setup_cpu,
  373. .give_timebase = smp_psurge_give_timebase,
  374. .take_timebase = smp_psurge_take_timebase,
  375. };
  376. #endif /* CONFIG_PPC32 - actually powersurge support */
  377. /*
  378. * Core 99 and later support
  379. */
  380. static void (*pmac_tb_freeze)(int freeze);
  381. static u64 timebase;
  382. static int tb_req;
  383. static void smp_core99_give_timebase(void)
  384. {
  385. unsigned long flags;
  386. local_irq_save(flags);
  387. while(!tb_req)
  388. barrier();
  389. tb_req = 0;
  390. (*pmac_tb_freeze)(1);
  391. mb();
  392. timebase = get_tb();
  393. mb();
  394. while (timebase)
  395. barrier();
  396. mb();
  397. (*pmac_tb_freeze)(0);
  398. mb();
  399. local_irq_restore(flags);
  400. }
  401. static void __devinit smp_core99_take_timebase(void)
  402. {
  403. unsigned long flags;
  404. local_irq_save(flags);
  405. tb_req = 1;
  406. mb();
  407. while (!timebase)
  408. barrier();
  409. mb();
  410. set_tb(timebase >> 32, timebase & 0xffffffff);
  411. timebase = 0;
  412. mb();
  413. set_dec(tb_ticks_per_jiffy/2);
  414. local_irq_restore(flags);
  415. }
  416. #ifdef CONFIG_PPC64
  417. /*
  418. * G5s enable/disable the timebase via an i2c-connected clock chip.
  419. */
  420. static struct pmac_i2c_bus *pmac_tb_clock_chip_host;
  421. static u8 pmac_tb_pulsar_addr;
  422. static void smp_core99_cypress_tb_freeze(int freeze)
  423. {
  424. u8 data;
  425. int rc;
  426. /* Strangely, the device-tree says address is 0xd2, but darwin
  427. * accesses 0xd0 ...
  428. */
  429. pmac_i2c_setmode(pmac_tb_clock_chip_host,
  430. pmac_i2c_mode_combined);
  431. rc = pmac_i2c_xfer(pmac_tb_clock_chip_host,
  432. 0xd0 | pmac_i2c_read,
  433. 1, 0x81, &data, 1);
  434. if (rc != 0)
  435. goto bail;
  436. data = (data & 0xf3) | (freeze ? 0x00 : 0x0c);
  437. pmac_i2c_setmode(pmac_tb_clock_chip_host, pmac_i2c_mode_stdsub);
  438. rc = pmac_i2c_xfer(pmac_tb_clock_chip_host,
  439. 0xd0 | pmac_i2c_write,
  440. 1, 0x81, &data, 1);
  441. bail:
  442. if (rc != 0) {
  443. printk("Cypress Timebase %s rc: %d\n",
  444. freeze ? "freeze" : "unfreeze", rc);
  445. panic("Timebase freeze failed !\n");
  446. }
  447. }
  448. static void smp_core99_pulsar_tb_freeze(int freeze)
  449. {
  450. u8 data;
  451. int rc;
  452. pmac_i2c_setmode(pmac_tb_clock_chip_host,
  453. pmac_i2c_mode_combined);
  454. rc = pmac_i2c_xfer(pmac_tb_clock_chip_host,
  455. pmac_tb_pulsar_addr | pmac_i2c_read,
  456. 1, 0x2e, &data, 1);
  457. if (rc != 0)
  458. goto bail;
  459. data = (data & 0x88) | (freeze ? 0x11 : 0x22);
  460. pmac_i2c_setmode(pmac_tb_clock_chip_host, pmac_i2c_mode_stdsub);
  461. rc = pmac_i2c_xfer(pmac_tb_clock_chip_host,
  462. pmac_tb_pulsar_addr | pmac_i2c_write,
  463. 1, 0x2e, &data, 1);
  464. bail:
  465. if (rc != 0) {
  466. printk(KERN_ERR "Pulsar Timebase %s rc: %d\n",
  467. freeze ? "freeze" : "unfreeze", rc);
  468. panic("Timebase freeze failed !\n");
  469. }
  470. }
  471. static void __init smp_core99_setup_i2c_hwsync(int ncpus)
  472. {
  473. struct device_node *cc = NULL;
  474. struct device_node *p;
  475. const char *name = NULL;
  476. const u32 *reg;
  477. int ok;
  478. /* Look for the clock chip */
  479. while ((cc = of_find_node_by_name(cc, "i2c-hwclock")) != NULL) {
  480. p = of_get_parent(cc);
  481. ok = p && of_device_is_compatible(p, "uni-n-i2c");
  482. of_node_put(p);
  483. if (!ok)
  484. continue;
  485. pmac_tb_clock_chip_host = pmac_i2c_find_bus(cc);
  486. if (pmac_tb_clock_chip_host == NULL)
  487. continue;
  488. reg = of_get_property(cc, "reg", NULL);
  489. if (reg == NULL)
  490. continue;
  491. switch (*reg) {
  492. case 0xd2:
  493. if (of_device_is_compatible(cc,"pulsar-legacy-slewing")) {
  494. pmac_tb_freeze = smp_core99_pulsar_tb_freeze;
  495. pmac_tb_pulsar_addr = 0xd2;
  496. name = "Pulsar";
  497. } else if (of_device_is_compatible(cc, "cy28508")) {
  498. pmac_tb_freeze = smp_core99_cypress_tb_freeze;
  499. name = "Cypress";
  500. }
  501. break;
  502. case 0xd4:
  503. pmac_tb_freeze = smp_core99_pulsar_tb_freeze;
  504. pmac_tb_pulsar_addr = 0xd4;
  505. name = "Pulsar";
  506. break;
  507. }
  508. if (pmac_tb_freeze != NULL)
  509. break;
  510. }
  511. if (pmac_tb_freeze != NULL) {
  512. /* Open i2c bus for synchronous access */
  513. if (pmac_i2c_open(pmac_tb_clock_chip_host, 1)) {
  514. printk(KERN_ERR "Failed top open i2c bus for clock"
  515. " sync, fallback to software sync !\n");
  516. goto no_i2c_sync;
  517. }
  518. printk(KERN_INFO "Processor timebase sync using %s i2c clock\n",
  519. name);
  520. return;
  521. }
  522. no_i2c_sync:
  523. pmac_tb_freeze = NULL;
  524. pmac_tb_clock_chip_host = NULL;
  525. }
  526. /*
  527. * Newer G5s uses a platform function
  528. */
  529. static void smp_core99_pfunc_tb_freeze(int freeze)
  530. {
  531. struct device_node *cpus;
  532. struct pmf_args args;
  533. cpus = of_find_node_by_path("/cpus");
  534. BUG_ON(cpus == NULL);
  535. args.count = 1;
  536. args.u[0].v = !freeze;
  537. pmf_call_function(cpus, "cpu-timebase", &args);
  538. of_node_put(cpus);
  539. }
  540. #else /* CONFIG_PPC64 */
  541. /*
  542. * SMP G4 use a GPIO to enable/disable the timebase.
  543. */
  544. static unsigned int core99_tb_gpio; /* Timebase freeze GPIO */
  545. static void smp_core99_gpio_tb_freeze(int freeze)
  546. {
  547. if (freeze)
  548. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, core99_tb_gpio, 4);
  549. else
  550. pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, core99_tb_gpio, 0);
  551. pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, core99_tb_gpio, 0);
  552. }
  553. #endif /* !CONFIG_PPC64 */
  554. /* L2 and L3 cache settings to pass from CPU0 to CPU1 on G4 cpus */
  555. volatile static long int core99_l2_cache;
  556. volatile static long int core99_l3_cache;
  557. static void __devinit core99_init_caches(int cpu)
  558. {
  559. #ifndef CONFIG_PPC64
  560. if (!cpu_has_feature(CPU_FTR_L2CR))
  561. return;
  562. if (cpu == 0) {
  563. core99_l2_cache = _get_L2CR();
  564. printk("CPU0: L2CR is %lx\n", core99_l2_cache);
  565. } else {
  566. printk("CPU%d: L2CR was %lx\n", cpu, _get_L2CR());
  567. _set_L2CR(0);
  568. _set_L2CR(core99_l2_cache);
  569. printk("CPU%d: L2CR set to %lx\n", cpu, core99_l2_cache);
  570. }
  571. if (!cpu_has_feature(CPU_FTR_L3CR))
  572. return;
  573. if (cpu == 0){
  574. core99_l3_cache = _get_L3CR();
  575. printk("CPU0: L3CR is %lx\n", core99_l3_cache);
  576. } else {
  577. printk("CPU%d: L3CR was %lx\n", cpu, _get_L3CR());
  578. _set_L3CR(0);
  579. _set_L3CR(core99_l3_cache);
  580. printk("CPU%d: L3CR set to %lx\n", cpu, core99_l3_cache);
  581. }
  582. #endif /* !CONFIG_PPC64 */
  583. }
  584. static void __init smp_core99_setup(int ncpus)
  585. {
  586. #ifdef CONFIG_PPC64
  587. /* i2c based HW sync on some G5s */
  588. if (machine_is_compatible("PowerMac7,2") ||
  589. machine_is_compatible("PowerMac7,3") ||
  590. machine_is_compatible("RackMac3,1"))
  591. smp_core99_setup_i2c_hwsync(ncpus);
  592. /* pfunc based HW sync on recent G5s */
  593. if (pmac_tb_freeze == NULL) {
  594. struct device_node *cpus =
  595. of_find_node_by_path("/cpus");
  596. if (cpus &&
  597. of_get_property(cpus, "platform-cpu-timebase", NULL)) {
  598. pmac_tb_freeze = smp_core99_pfunc_tb_freeze;
  599. printk(KERN_INFO "Processor timebase sync using"
  600. " platform function\n");
  601. }
  602. }
  603. #else /* CONFIG_PPC64 */
  604. /* GPIO based HW sync on ppc32 Core99 */
  605. if (pmac_tb_freeze == NULL && !machine_is_compatible("MacRISC4")) {
  606. struct device_node *cpu;
  607. const u32 *tbprop = NULL;
  608. core99_tb_gpio = KL_GPIO_TB_ENABLE; /* default value */
  609. cpu = of_find_node_by_type(NULL, "cpu");
  610. if (cpu != NULL) {
  611. tbprop = of_get_property(cpu, "timebase-enable", NULL);
  612. if (tbprop)
  613. core99_tb_gpio = *tbprop;
  614. of_node_put(cpu);
  615. }
  616. pmac_tb_freeze = smp_core99_gpio_tb_freeze;
  617. printk(KERN_INFO "Processor timebase sync using"
  618. " GPIO 0x%02x\n", core99_tb_gpio);
  619. }
  620. #endif /* CONFIG_PPC64 */
  621. /* No timebase sync, fallback to software */
  622. if (pmac_tb_freeze == NULL) {
  623. smp_ops->give_timebase = smp_generic_give_timebase;
  624. smp_ops->take_timebase = smp_generic_take_timebase;
  625. printk(KERN_INFO "Processor timebase sync using software\n");
  626. }
  627. #ifndef CONFIG_PPC64
  628. {
  629. int i;
  630. /* XXX should get this from reg properties */
  631. for (i = 1; i < ncpus; ++i)
  632. smp_hw_index[i] = i;
  633. }
  634. #endif
  635. /* 32 bits SMP can't NAP */
  636. if (!machine_is_compatible("MacRISC4"))
  637. powersave_nap = 0;
  638. }
  639. static int __init smp_core99_probe(void)
  640. {
  641. struct device_node *cpus;
  642. int ncpus = 0;
  643. if (ppc_md.progress) ppc_md.progress("smp_core99_probe", 0x345);
  644. /* Count CPUs in the device-tree */
  645. for (cpus = NULL; (cpus = of_find_node_by_type(cpus, "cpu")) != NULL;)
  646. ++ncpus;
  647. printk(KERN_INFO "PowerMac SMP probe found %d cpus\n", ncpus);
  648. /* Nothing more to do if less than 2 of them */
  649. if (ncpus <= 1)
  650. return 1;
  651. /* We need to perform some early initialisations before we can start
  652. * setting up SMP as we are running before initcalls
  653. */
  654. pmac_pfunc_base_install();
  655. pmac_i2c_init();
  656. /* Setup various bits like timebase sync method, ability to nap, ... */
  657. smp_core99_setup(ncpus);
  658. /* Install IPIs */
  659. mpic_request_ipis();
  660. /* Collect l2cr and l3cr values from CPU 0 */
  661. core99_init_caches(0);
  662. return ncpus;
  663. }
  664. static void __devinit smp_core99_kick_cpu(int nr)
  665. {
  666. unsigned int save_vector;
  667. unsigned long target, flags;
  668. volatile unsigned int *vector
  669. = ((volatile unsigned int *)(KERNELBASE+0x100));
  670. if (nr < 0 || nr > 3)
  671. return;
  672. if (ppc_md.progress)
  673. ppc_md.progress("smp_core99_kick_cpu", 0x346);
  674. local_irq_save(flags);
  675. /* Save reset vector */
  676. save_vector = *vector;
  677. /* Setup fake reset vector that does
  678. * b __secondary_start_pmac_0 + nr*8 - KERNELBASE
  679. */
  680. target = (unsigned long) __secondary_start_pmac_0 + nr * 8;
  681. create_branch((unsigned long)vector, target, BRANCH_SET_LINK);
  682. /* Put some life in our friend */
  683. pmac_call_feature(PMAC_FTR_RESET_CPU, NULL, nr, 0);
  684. /* FIXME: We wait a bit for the CPU to take the exception, I should
  685. * instead wait for the entry code to set something for me. Well,
  686. * ideally, all that crap will be done in prom.c and the CPU left
  687. * in a RAM-based wait loop like CHRP.
  688. */
  689. mdelay(1);
  690. /* Restore our exception vector */
  691. *vector = save_vector;
  692. flush_icache_range((unsigned long) vector, (unsigned long) vector + 4);
  693. local_irq_restore(flags);
  694. if (ppc_md.progress) ppc_md.progress("smp_core99_kick_cpu done", 0x347);
  695. }
  696. static void __devinit smp_core99_setup_cpu(int cpu_nr)
  697. {
  698. /* Setup L2/L3 */
  699. if (cpu_nr != 0)
  700. core99_init_caches(cpu_nr);
  701. /* Setup openpic */
  702. mpic_setup_this_cpu();
  703. if (cpu_nr == 0) {
  704. #ifdef CONFIG_PPC64
  705. extern void g5_phy_disable_cpu1(void);
  706. /* Close i2c bus if it was used for tb sync */
  707. if (pmac_tb_clock_chip_host) {
  708. pmac_i2c_close(pmac_tb_clock_chip_host);
  709. pmac_tb_clock_chip_host = NULL;
  710. }
  711. /* If we didn't start the second CPU, we must take
  712. * it off the bus
  713. */
  714. if (machine_is_compatible("MacRISC4") &&
  715. num_online_cpus() < 2)
  716. g5_phy_disable_cpu1();
  717. #endif /* CONFIG_PPC64 */
  718. if (ppc_md.progress)
  719. ppc_md.progress("core99_setup_cpu 0 done", 0x349);
  720. }
  721. }
  722. #if defined(CONFIG_HOTPLUG_CPU) && defined(CONFIG_PPC32)
  723. int smp_core99_cpu_disable(void)
  724. {
  725. cpu_clear(smp_processor_id(), cpu_online_map);
  726. /* XXX reset cpu affinity here */
  727. mpic_cpu_set_priority(0xf);
  728. asm volatile("mtdec %0" : : "r" (0x7fffffff));
  729. mb();
  730. udelay(20);
  731. asm volatile("mtdec %0" : : "r" (0x7fffffff));
  732. return 0;
  733. }
  734. extern void low_cpu_die(void) __attribute__((noreturn)); /* in sleep.S */
  735. static int cpu_dead[NR_CPUS];
  736. void cpu_die(void)
  737. {
  738. local_irq_disable();
  739. cpu_dead[smp_processor_id()] = 1;
  740. mb();
  741. low_cpu_die();
  742. }
  743. void smp_core99_cpu_die(unsigned int cpu)
  744. {
  745. int timeout;
  746. timeout = 1000;
  747. while (!cpu_dead[cpu]) {
  748. if (--timeout == 0) {
  749. printk("CPU %u refused to die!\n", cpu);
  750. break;
  751. }
  752. msleep(1);
  753. }
  754. cpu_dead[cpu] = 0;
  755. }
  756. #endif /* CONFIG_HOTPLUG_CPU && CONFIG_PP32 */
  757. /* Core99 Macs (dual G4s and G5s) */
  758. struct smp_ops_t core99_smp_ops = {
  759. .message_pass = smp_mpic_message_pass,
  760. .probe = smp_core99_probe,
  761. .kick_cpu = smp_core99_kick_cpu,
  762. .setup_cpu = smp_core99_setup_cpu,
  763. .give_timebase = smp_core99_give_timebase,
  764. .take_timebase = smp_core99_take_timebase,
  765. #if defined(CONFIG_HOTPLUG_CPU)
  766. # if defined(CONFIG_PPC32)
  767. .cpu_disable = smp_core99_cpu_disable,
  768. .cpu_die = smp_core99_cpu_die,
  769. # endif
  770. # if defined(CONFIG_PPC64)
  771. .cpu_disable = generic_cpu_disable,
  772. .cpu_die = generic_cpu_die,
  773. /* intentionally do *NOT* assign cpu_enable,
  774. * the generic code will use kick_cpu then! */
  775. # endif
  776. #endif
  777. };