smp.c 22 KB

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