pmac_smp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. #undef DEBUG
  25. #include <linux/config.h>
  26. #include <linux/kernel.h>
  27. #include <linux/sched.h>
  28. #include <linux/smp.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/kernel_stat.h>
  32. #include <linux/init.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/errno.h>
  35. #include <linux/irq.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/cacheflush.h>
  49. #include <asm/keylargo.h>
  50. #include <asm/pmac_low_i2c.h>
  51. #include "mpic.h"
  52. #ifdef DEBUG
  53. #define DBG(fmt...) udbg_printf(fmt)
  54. #else
  55. #define DBG(fmt...)
  56. #endif
  57. extern void pmac_secondary_start_1(void);
  58. extern void pmac_secondary_start_2(void);
  59. extern void pmac_secondary_start_3(void);
  60. extern struct smp_ops_t *smp_ops;
  61. static void (*pmac_tb_freeze)(int freeze);
  62. static struct device_node *pmac_tb_clock_chip_host;
  63. static u8 pmac_tb_pulsar_addr;
  64. static DEFINE_SPINLOCK(timebase_lock);
  65. static unsigned long timebase;
  66. static void smp_core99_cypress_tb_freeze(int freeze)
  67. {
  68. u8 data;
  69. int rc;
  70. /* Strangely, the device-tree says address is 0xd2, but darwin
  71. * accesses 0xd0 ...
  72. */
  73. pmac_low_i2c_setmode(pmac_tb_clock_chip_host, pmac_low_i2c_mode_combined);
  74. rc = pmac_low_i2c_xfer(pmac_tb_clock_chip_host,
  75. 0xd0 | pmac_low_i2c_read,
  76. 0x81, &data, 1);
  77. if (rc != 0)
  78. goto bail;
  79. data = (data & 0xf3) | (freeze ? 0x00 : 0x0c);
  80. pmac_low_i2c_setmode(pmac_tb_clock_chip_host, pmac_low_i2c_mode_stdsub);
  81. rc = pmac_low_i2c_xfer(pmac_tb_clock_chip_host,
  82. 0xd0 | pmac_low_i2c_write,
  83. 0x81, &data, 1);
  84. bail:
  85. if (rc != 0) {
  86. printk("Cypress Timebase %s rc: %d\n",
  87. freeze ? "freeze" : "unfreeze", rc);
  88. panic("Timebase freeze failed !\n");
  89. }
  90. }
  91. static void smp_core99_pulsar_tb_freeze(int freeze)
  92. {
  93. u8 data;
  94. int rc;
  95. pmac_low_i2c_setmode(pmac_tb_clock_chip_host, pmac_low_i2c_mode_combined);
  96. rc = pmac_low_i2c_xfer(pmac_tb_clock_chip_host,
  97. pmac_tb_pulsar_addr | pmac_low_i2c_read,
  98. 0x2e, &data, 1);
  99. if (rc != 0)
  100. goto bail;
  101. data = (data & 0x88) | (freeze ? 0x11 : 0x22);
  102. pmac_low_i2c_setmode(pmac_tb_clock_chip_host, pmac_low_i2c_mode_stdsub);
  103. rc = pmac_low_i2c_xfer(pmac_tb_clock_chip_host,
  104. pmac_tb_pulsar_addr | pmac_low_i2c_write,
  105. 0x2e, &data, 1);
  106. bail:
  107. if (rc != 0) {
  108. printk(KERN_ERR "Pulsar Timebase %s rc: %d\n",
  109. freeze ? "freeze" : "unfreeze", rc);
  110. panic("Timebase freeze failed !\n");
  111. }
  112. }
  113. static void smp_core99_give_timebase(void)
  114. {
  115. /* Open i2c bus for synchronous access */
  116. if (pmac_low_i2c_open(pmac_tb_clock_chip_host, 0))
  117. panic("Can't open i2c for TB sync !\n");
  118. spin_lock(&timebase_lock);
  119. (*pmac_tb_freeze)(1);
  120. mb();
  121. timebase = get_tb();
  122. spin_unlock(&timebase_lock);
  123. while (timebase)
  124. barrier();
  125. spin_lock(&timebase_lock);
  126. (*pmac_tb_freeze)(0);
  127. spin_unlock(&timebase_lock);
  128. /* Close i2c bus */
  129. pmac_low_i2c_close(pmac_tb_clock_chip_host);
  130. }
  131. static void __devinit smp_core99_take_timebase(void)
  132. {
  133. while (!timebase)
  134. barrier();
  135. spin_lock(&timebase_lock);
  136. set_tb(timebase >> 32, timebase & 0xffffffff);
  137. timebase = 0;
  138. spin_unlock(&timebase_lock);
  139. }
  140. static int __init smp_core99_probe(void)
  141. {
  142. struct device_node *cpus;
  143. struct device_node *cc;
  144. int ncpus = 0;
  145. /* Maybe use systemconfiguration here ? */
  146. if (ppc_md.progress) ppc_md.progress("smp_core99_probe", 0x345);
  147. /* Count CPUs in the device-tree */
  148. for (cpus = NULL; (cpus = of_find_node_by_type(cpus, "cpu")) != NULL;)
  149. ++ncpus;
  150. printk(KERN_INFO "PowerMac SMP probe found %d cpus\n", ncpus);
  151. /* Nothing more to do if less than 2 of them */
  152. if (ncpus <= 1)
  153. return 1;
  154. /* HW sync only on these platforms */
  155. if (!machine_is_compatible("PowerMac7,2") &&
  156. !machine_is_compatible("PowerMac7,3") &&
  157. !machine_is_compatible("RackMac3,1"))
  158. goto nohwsync;
  159. /* Look for the clock chip */
  160. for (cc = NULL; (cc = of_find_node_by_name(cc, "i2c-hwclock")) != NULL;) {
  161. struct device_node *p = of_get_parent(cc);
  162. u32 *reg;
  163. int ok;
  164. ok = p && device_is_compatible(p, "uni-n-i2c");
  165. if (!ok)
  166. goto next;
  167. reg = (u32 *)get_property(cc, "reg", NULL);
  168. if (reg == NULL)
  169. goto next;
  170. switch (*reg) {
  171. case 0xd2:
  172. if (device_is_compatible(cc, "pulsar-legacy-slewing")) {
  173. pmac_tb_freeze = smp_core99_pulsar_tb_freeze;
  174. pmac_tb_pulsar_addr = 0xd2;
  175. printk(KERN_INFO "Timebase clock is Pulsar chip\n");
  176. } else if (device_is_compatible(cc, "cy28508")) {
  177. pmac_tb_freeze = smp_core99_cypress_tb_freeze;
  178. printk(KERN_INFO "Timebase clock is Cypress chip\n");
  179. }
  180. break;
  181. case 0xd4:
  182. pmac_tb_freeze = smp_core99_pulsar_tb_freeze;
  183. pmac_tb_pulsar_addr = 0xd4;
  184. printk(KERN_INFO "Timebase clock is Pulsar chip\n");
  185. break;
  186. }
  187. if (pmac_tb_freeze != NULL) {
  188. pmac_tb_clock_chip_host = p;
  189. smp_ops->give_timebase = smp_core99_give_timebase;
  190. smp_ops->take_timebase = smp_core99_take_timebase;
  191. of_node_put(cc);
  192. of_node_put(p);
  193. break;
  194. }
  195. next:
  196. of_node_put(p);
  197. }
  198. nohwsync:
  199. mpic_request_ipis();
  200. return ncpus;
  201. }
  202. static void __init smp_core99_kick_cpu(int nr)
  203. {
  204. int save_vector, j;
  205. unsigned long new_vector;
  206. unsigned long flags;
  207. volatile unsigned int *vector
  208. = ((volatile unsigned int *)(KERNELBASE+0x100));
  209. if (nr < 1 || nr > 3)
  210. return;
  211. if (ppc_md.progress) ppc_md.progress("smp_core99_kick_cpu", 0x346);
  212. local_irq_save(flags);
  213. local_irq_disable();
  214. /* Save reset vector */
  215. save_vector = *vector;
  216. /* Setup fake reset vector that does
  217. * b .pmac_secondary_start - KERNELBASE
  218. */
  219. switch(nr) {
  220. case 1:
  221. new_vector = (unsigned long)pmac_secondary_start_1;
  222. break;
  223. case 2:
  224. new_vector = (unsigned long)pmac_secondary_start_2;
  225. break;
  226. case 3:
  227. default:
  228. new_vector = (unsigned long)pmac_secondary_start_3;
  229. break;
  230. }
  231. *vector = 0x48000002 + (new_vector - KERNELBASE);
  232. /* flush data cache and inval instruction cache */
  233. flush_icache_range((unsigned long) vector, (unsigned long) vector + 4);
  234. /* Put some life in our friend */
  235. pmac_call_feature(PMAC_FTR_RESET_CPU, NULL, nr, 0);
  236. paca[nr].cpu_start = 1;
  237. /* FIXME: We wait a bit for the CPU to take the exception, I should
  238. * instead wait for the entry code to set something for me. Well,
  239. * ideally, all that crap will be done in prom.c and the CPU left
  240. * in a RAM-based wait loop like CHRP.
  241. */
  242. for (j = 1; j < 1000000; j++)
  243. mb();
  244. /* Restore our exception vector */
  245. *vector = save_vector;
  246. flush_icache_range((unsigned long) vector, (unsigned long) vector + 4);
  247. local_irq_restore(flags);
  248. if (ppc_md.progress) ppc_md.progress("smp_core99_kick_cpu done", 0x347);
  249. }
  250. static void __init smp_core99_setup_cpu(int cpu_nr)
  251. {
  252. /* Setup MPIC */
  253. mpic_setup_this_cpu();
  254. if (cpu_nr == 0) {
  255. extern void g5_phy_disable_cpu1(void);
  256. /* If we didn't start the second CPU, we must take
  257. * it off the bus
  258. */
  259. if (num_online_cpus() < 2)
  260. g5_phy_disable_cpu1();
  261. if (ppc_md.progress) ppc_md.progress("smp_core99_setup_cpu 0 done", 0x349);
  262. }
  263. }
  264. struct smp_ops_t core99_smp_ops __pmacdata = {
  265. .message_pass = smp_mpic_message_pass,
  266. .probe = smp_core99_probe,
  267. .kick_cpu = smp_core99_kick_cpu,
  268. .setup_cpu = smp_core99_setup_cpu,
  269. .give_timebase = smp_generic_give_timebase,
  270. .take_timebase = smp_generic_take_timebase,
  271. };
  272. void __init pmac_setup_smp(void)
  273. {
  274. smp_ops = &core99_smp_ops;
  275. #ifdef CONFIG_HOTPLUG_CPU
  276. smp_ops->cpu_enable = generic_cpu_enable;
  277. smp_ops->cpu_disable = generic_cpu_disable;
  278. smp_ops->cpu_die = generic_cpu_die;
  279. #endif
  280. }