platmcpm.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (c) 2013-2014 Linaro Ltd.
  3. * Copyright (c) 2013-2014 Hisilicon Limited.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/smp.h>
  11. #include <linux/delay.h>
  12. #include <linux/io.h>
  13. #include <linux/memblock.h>
  14. #include <linux/of_address.h>
  15. #include <asm/cputype.h>
  16. #include <asm/cp15.h>
  17. #include <asm/cacheflush.h>
  18. #include <asm/smp.h>
  19. #include <asm/smp_plat.h>
  20. #include "core.h"
  21. /* bits definition in SC_CPU_RESET_REQ[x]/SC_CPU_RESET_DREQ[x]
  22. * 1 -- unreset; 0 -- reset
  23. */
  24. #define CORE_RESET_BIT(x) (1 << x)
  25. #define NEON_RESET_BIT(x) (1 << (x + 4))
  26. #define CORE_DEBUG_RESET_BIT(x) (1 << (x + 9))
  27. #define CLUSTER_L2_RESET_BIT (1 << 8)
  28. #define CLUSTER_DEBUG_RESET_BIT (1 << 13)
  29. /*
  30. * bits definition in SC_CPU_RESET_STATUS[x]
  31. * 1 -- reset status; 0 -- unreset status
  32. */
  33. #define CORE_RESET_STATUS(x) (1 << x)
  34. #define NEON_RESET_STATUS(x) (1 << (x + 4))
  35. #define CORE_DEBUG_RESET_STATUS(x) (1 << (x + 9))
  36. #define CLUSTER_L2_RESET_STATUS (1 << 8)
  37. #define CLUSTER_DEBUG_RESET_STATUS (1 << 13)
  38. #define CORE_WFI_STATUS(x) (1 << (x + 16))
  39. #define CORE_WFE_STATUS(x) (1 << (x + 20))
  40. #define CORE_DEBUG_ACK(x) (1 << (x + 24))
  41. #define SC_CPU_RESET_REQ(x) (0x520 + (x << 3)) /* reset */
  42. #define SC_CPU_RESET_DREQ(x) (0x524 + (x << 3)) /* unreset */
  43. #define SC_CPU_RESET_STATUS(x) (0x1520 + (x << 3))
  44. #define FAB_SF_MODE 0x0c
  45. #define FAB_SF_INVLD 0x10
  46. /* bits definition in FB_SF_INVLD */
  47. #define FB_SF_INVLD_START (1 << 8)
  48. #define HIP04_MAX_CLUSTERS 4
  49. #define HIP04_MAX_CPUS_PER_CLUSTER 4
  50. #define POLL_MSEC 10
  51. #define TIMEOUT_MSEC 1000
  52. static void __iomem *sysctrl, *fabric;
  53. static int hip04_cpu_table[HIP04_MAX_CLUSTERS][HIP04_MAX_CPUS_PER_CLUSTER];
  54. static DEFINE_SPINLOCK(boot_lock);
  55. static u32 fabric_phys_addr;
  56. /*
  57. * [0]: bootwrapper physical address
  58. * [1]: bootwrapper size
  59. * [2]: relocation address
  60. * [3]: relocation size
  61. */
  62. static u32 hip04_boot_method[4];
  63. static bool hip04_cluster_is_down(unsigned int cluster)
  64. {
  65. int i;
  66. for (i = 0; i < HIP04_MAX_CPUS_PER_CLUSTER; i++)
  67. if (hip04_cpu_table[cluster][i])
  68. return false;
  69. return true;
  70. }
  71. static void hip04_set_snoop_filter(unsigned int cluster, unsigned int on)
  72. {
  73. unsigned long data;
  74. if (!fabric)
  75. BUG();
  76. data = readl_relaxed(fabric + FAB_SF_MODE);
  77. if (on)
  78. data |= 1 << cluster;
  79. else
  80. data &= ~(1 << cluster);
  81. writel_relaxed(data, fabric + FAB_SF_MODE);
  82. do {
  83. cpu_relax();
  84. } while (data != readl_relaxed(fabric + FAB_SF_MODE));
  85. }
  86. static int hip04_boot_secondary(unsigned int l_cpu, struct task_struct *idle)
  87. {
  88. unsigned int mpidr, cpu, cluster;
  89. unsigned long data;
  90. void __iomem *sys_dreq, *sys_status;
  91. mpidr = cpu_logical_map(l_cpu);
  92. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  93. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  94. if (!sysctrl)
  95. return -ENODEV;
  96. if (cluster >= HIP04_MAX_CLUSTERS || cpu >= HIP04_MAX_CPUS_PER_CLUSTER)
  97. return -EINVAL;
  98. spin_lock_irq(&boot_lock);
  99. if (hip04_cpu_table[cluster][cpu])
  100. goto out;
  101. sys_dreq = sysctrl + SC_CPU_RESET_DREQ(cluster);
  102. sys_status = sysctrl + SC_CPU_RESET_STATUS(cluster);
  103. if (hip04_cluster_is_down(cluster)) {
  104. data = CLUSTER_DEBUG_RESET_BIT;
  105. writel_relaxed(data, sys_dreq);
  106. do {
  107. cpu_relax();
  108. data = readl_relaxed(sys_status);
  109. } while (data & CLUSTER_DEBUG_RESET_STATUS);
  110. hip04_set_snoop_filter(cluster, 1);
  111. }
  112. data = CORE_RESET_BIT(cpu) | NEON_RESET_BIT(cpu) | \
  113. CORE_DEBUG_RESET_BIT(cpu);
  114. writel_relaxed(data, sys_dreq);
  115. do {
  116. cpu_relax();
  117. } while (data == readl_relaxed(sys_status));
  118. /*
  119. * We may fail to power up core again without this delay.
  120. * It's not mentioned in document. It's found by test.
  121. */
  122. udelay(20);
  123. arch_send_wakeup_ipi_mask(cpumask_of(l_cpu));
  124. out:
  125. hip04_cpu_table[cluster][cpu]++;
  126. spin_unlock_irq(&boot_lock);
  127. return 0;
  128. }
  129. #ifdef CONFIG_HOTPLUG_CPU
  130. static void hip04_cpu_die(unsigned int l_cpu)
  131. {
  132. unsigned int mpidr, cpu, cluster;
  133. bool last_man;
  134. mpidr = cpu_logical_map(l_cpu);
  135. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  136. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  137. spin_lock(&boot_lock);
  138. hip04_cpu_table[cluster][cpu]--;
  139. if (hip04_cpu_table[cluster][cpu] == 1) {
  140. /* A power_up request went ahead of us. */
  141. spin_unlock(&boot_lock);
  142. return;
  143. } else if (hip04_cpu_table[cluster][cpu] > 1) {
  144. pr_err("Cluster %d CPU%d boots multiple times\n", cluster, cpu);
  145. BUG();
  146. }
  147. last_man = hip04_cluster_is_down(cluster);
  148. spin_unlock(&boot_lock);
  149. if (last_man) {
  150. /* Since it's Cortex A15, disable L2 prefetching. */
  151. asm volatile(
  152. "mcr p15, 1, %0, c15, c0, 3 \n\t"
  153. "isb \n\t"
  154. "dsb "
  155. : : "r" (0x400) );
  156. v7_exit_coherency_flush(all);
  157. } else {
  158. v7_exit_coherency_flush(louis);
  159. }
  160. for (;;)
  161. wfi();
  162. }
  163. static int hip04_cpu_kill(unsigned int l_cpu)
  164. {
  165. unsigned int mpidr, cpu, cluster;
  166. unsigned int data, tries, count;
  167. mpidr = cpu_logical_map(l_cpu);
  168. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  169. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  170. BUG_ON(cluster >= HIP04_MAX_CLUSTERS ||
  171. cpu >= HIP04_MAX_CPUS_PER_CLUSTER);
  172. count = TIMEOUT_MSEC / POLL_MSEC;
  173. spin_lock_irq(&boot_lock);
  174. for (tries = 0; tries < count; tries++) {
  175. if (hip04_cpu_table[cluster][cpu])
  176. goto err;
  177. cpu_relax();
  178. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  179. if (data & CORE_WFI_STATUS(cpu))
  180. break;
  181. spin_unlock_irq(&boot_lock);
  182. /* Wait for clean L2 when the whole cluster is down. */
  183. msleep(POLL_MSEC);
  184. spin_lock_irq(&boot_lock);
  185. }
  186. if (tries >= count)
  187. goto err;
  188. data = CORE_RESET_BIT(cpu) | NEON_RESET_BIT(cpu) | \
  189. CORE_DEBUG_RESET_BIT(cpu);
  190. writel_relaxed(data, sysctrl + SC_CPU_RESET_REQ(cluster));
  191. for (tries = 0; tries < count; tries++) {
  192. cpu_relax();
  193. data = readl_relaxed(sysctrl + SC_CPU_RESET_STATUS(cluster));
  194. if (data & CORE_RESET_STATUS(cpu))
  195. break;
  196. }
  197. if (tries >= count)
  198. goto err;
  199. if (hip04_cluster_is_down(cluster))
  200. hip04_set_snoop_filter(cluster, 0);
  201. spin_unlock_irq(&boot_lock);
  202. return 1;
  203. err:
  204. spin_unlock_irq(&boot_lock);
  205. return 0;
  206. }
  207. #endif
  208. static const struct smp_operations hip04_smp_ops __initconst = {
  209. .smp_boot_secondary = hip04_boot_secondary,
  210. #ifdef CONFIG_HOTPLUG_CPU
  211. .cpu_die = hip04_cpu_die,
  212. .cpu_kill = hip04_cpu_kill,
  213. #endif
  214. };
  215. static bool __init hip04_cpu_table_init(void)
  216. {
  217. unsigned int mpidr, cpu, cluster;
  218. mpidr = read_cpuid_mpidr();
  219. cpu = MPIDR_AFFINITY_LEVEL(mpidr, 0);
  220. cluster = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  221. if (cluster >= HIP04_MAX_CLUSTERS ||
  222. cpu >= HIP04_MAX_CPUS_PER_CLUSTER) {
  223. pr_err("%s: boot CPU is out of bound!\n", __func__);
  224. return false;
  225. }
  226. hip04_set_snoop_filter(cluster, 1);
  227. hip04_cpu_table[cluster][cpu] = 1;
  228. return true;
  229. }
  230. static int __init hip04_smp_init(void)
  231. {
  232. struct device_node *np, *np_sctl, *np_fab;
  233. struct resource fab_res;
  234. void __iomem *relocation;
  235. int ret = -ENODEV;
  236. np = of_find_compatible_node(NULL, NULL, "hisilicon,hip04-bootwrapper");
  237. if (!np)
  238. goto err;
  239. ret = of_property_read_u32_array(np, "boot-method",
  240. &hip04_boot_method[0], 4);
  241. if (ret)
  242. goto err;
  243. np_sctl = of_find_compatible_node(NULL, NULL, "hisilicon,sysctrl");
  244. if (!np_sctl)
  245. goto err;
  246. np_fab = of_find_compatible_node(NULL, NULL, "hisilicon,hip04-fabric");
  247. if (!np_fab)
  248. goto err;
  249. ret = memblock_reserve(hip04_boot_method[0], hip04_boot_method[1]);
  250. if (ret)
  251. goto err;
  252. relocation = ioremap(hip04_boot_method[2], hip04_boot_method[3]);
  253. if (!relocation) {
  254. pr_err("failed to map relocation space\n");
  255. ret = -ENOMEM;
  256. goto err_reloc;
  257. }
  258. sysctrl = of_iomap(np_sctl, 0);
  259. if (!sysctrl) {
  260. pr_err("failed to get sysctrl base\n");
  261. ret = -ENOMEM;
  262. goto err_sysctrl;
  263. }
  264. ret = of_address_to_resource(np_fab, 0, &fab_res);
  265. if (ret) {
  266. pr_err("failed to get fabric base phys\n");
  267. goto err_fabric;
  268. }
  269. fabric_phys_addr = fab_res.start;
  270. sync_cache_w(&fabric_phys_addr);
  271. fabric = of_iomap(np_fab, 0);
  272. if (!fabric) {
  273. pr_err("failed to get fabric base\n");
  274. ret = -ENOMEM;
  275. goto err_fabric;
  276. }
  277. if (!hip04_cpu_table_init()) {
  278. ret = -EINVAL;
  279. goto err_table;
  280. }
  281. /*
  282. * Fill the instruction address that is used after secondary core
  283. * out of reset.
  284. */
  285. writel_relaxed(hip04_boot_method[0], relocation);
  286. writel_relaxed(0xa5a5a5a5, relocation + 4); /* magic number */
  287. writel_relaxed(virt_to_phys(secondary_startup), relocation + 8);
  288. writel_relaxed(0, relocation + 12);
  289. iounmap(relocation);
  290. smp_set_ops(&hip04_smp_ops);
  291. return ret;
  292. err_table:
  293. iounmap(fabric);
  294. err_fabric:
  295. iounmap(sysctrl);
  296. err_sysctrl:
  297. iounmap(relocation);
  298. err_reloc:
  299. memblock_free(hip04_boot_method[0], hip04_boot_method[1]);
  300. err:
  301. return ret;
  302. }
  303. early_initcall(hip04_smp_init);