smp.c 23 KB

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