cbe_regs.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * cbe_regs.c
  4. *
  5. * Accessor routines for the various MMIO register blocks of the CBE
  6. *
  7. * (c) 2006 Benjamin Herrenschmidt <benh@kernel.crashing.org>, IBM Corp.
  8. */
  9. #include <linux/percpu.h>
  10. #include <linux/types.h>
  11. #include <linux/export.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/pgtable.h>
  15. #include <asm/io.h>
  16. #include <asm/ptrace.h>
  17. #include <asm/cell-regs.h>
  18. /*
  19. * Current implementation uses "cpu" nodes. We build our own mapping
  20. * array of cpu numbers to cpu nodes locally for now to allow interrupt
  21. * time code to have a fast path rather than call of_get_cpu_node(). If
  22. * we implement cpu hotplug, we'll have to install an appropriate notifier
  23. * in order to release references to the cpu going away
  24. */
  25. static struct cbe_regs_map
  26. {
  27. struct device_node *cpu_node;
  28. struct device_node *be_node;
  29. struct cbe_pmd_regs __iomem *pmd_regs;
  30. struct cbe_iic_regs __iomem *iic_regs;
  31. struct cbe_mic_tm_regs __iomem *mic_tm_regs;
  32. struct cbe_pmd_shadow_regs pmd_shadow_regs;
  33. } cbe_regs_maps[MAX_CBE];
  34. static int cbe_regs_map_count;
  35. static struct cbe_thread_map
  36. {
  37. struct device_node *cpu_node;
  38. struct device_node *be_node;
  39. struct cbe_regs_map *regs;
  40. unsigned int thread_id;
  41. unsigned int cbe_id;
  42. } cbe_thread_map[NR_CPUS];
  43. static cpumask_t cbe_local_mask[MAX_CBE] = { [0 ... MAX_CBE-1] = {CPU_BITS_NONE} };
  44. static cpumask_t cbe_first_online_cpu = { CPU_BITS_NONE };
  45. static struct cbe_regs_map *cbe_find_map(struct device_node *np)
  46. {
  47. int i;
  48. struct device_node *tmp_np;
  49. if (!of_node_is_type(np, "spe")) {
  50. for (i = 0; i < cbe_regs_map_count; i++)
  51. if (cbe_regs_maps[i].cpu_node == np ||
  52. cbe_regs_maps[i].be_node == np)
  53. return &cbe_regs_maps[i];
  54. return NULL;
  55. }
  56. if (np->data)
  57. return np->data;
  58. /* walk up path until cpu or be node was found */
  59. tmp_np = np;
  60. do {
  61. tmp_np = tmp_np->parent;
  62. /* on a correct devicetree we wont get up to root */
  63. BUG_ON(!tmp_np);
  64. } while (!of_node_is_type(tmp_np, "cpu") ||
  65. !of_node_is_type(tmp_np, "be"));
  66. np->data = cbe_find_map(tmp_np);
  67. return np->data;
  68. }
  69. struct cbe_pmd_regs __iomem *cbe_get_pmd_regs(struct device_node *np)
  70. {
  71. struct cbe_regs_map *map = cbe_find_map(np);
  72. if (map == NULL)
  73. return NULL;
  74. return map->pmd_regs;
  75. }
  76. EXPORT_SYMBOL_GPL(cbe_get_pmd_regs);
  77. struct cbe_pmd_regs __iomem *cbe_get_cpu_pmd_regs(int cpu)
  78. {
  79. struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
  80. if (map == NULL)
  81. return NULL;
  82. return map->pmd_regs;
  83. }
  84. EXPORT_SYMBOL_GPL(cbe_get_cpu_pmd_regs);
  85. struct cbe_pmd_shadow_regs *cbe_get_pmd_shadow_regs(struct device_node *np)
  86. {
  87. struct cbe_regs_map *map = cbe_find_map(np);
  88. if (map == NULL)
  89. return NULL;
  90. return &map->pmd_shadow_regs;
  91. }
  92. struct cbe_pmd_shadow_regs *cbe_get_cpu_pmd_shadow_regs(int cpu)
  93. {
  94. struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
  95. if (map == NULL)
  96. return NULL;
  97. return &map->pmd_shadow_regs;
  98. }
  99. struct cbe_iic_regs __iomem *cbe_get_iic_regs(struct device_node *np)
  100. {
  101. struct cbe_regs_map *map = cbe_find_map(np);
  102. if (map == NULL)
  103. return NULL;
  104. return map->iic_regs;
  105. }
  106. struct cbe_iic_regs __iomem *cbe_get_cpu_iic_regs(int cpu)
  107. {
  108. struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
  109. if (map == NULL)
  110. return NULL;
  111. return map->iic_regs;
  112. }
  113. struct cbe_mic_tm_regs __iomem *cbe_get_mic_tm_regs(struct device_node *np)
  114. {
  115. struct cbe_regs_map *map = cbe_find_map(np);
  116. if (map == NULL)
  117. return NULL;
  118. return map->mic_tm_regs;
  119. }
  120. struct cbe_mic_tm_regs __iomem *cbe_get_cpu_mic_tm_regs(int cpu)
  121. {
  122. struct cbe_regs_map *map = cbe_thread_map[cpu].regs;
  123. if (map == NULL)
  124. return NULL;
  125. return map->mic_tm_regs;
  126. }
  127. EXPORT_SYMBOL_GPL(cbe_get_cpu_mic_tm_regs);
  128. u32 cbe_get_hw_thread_id(int cpu)
  129. {
  130. return cbe_thread_map[cpu].thread_id;
  131. }
  132. EXPORT_SYMBOL_GPL(cbe_get_hw_thread_id);
  133. u32 cbe_cpu_to_node(int cpu)
  134. {
  135. return cbe_thread_map[cpu].cbe_id;
  136. }
  137. EXPORT_SYMBOL_GPL(cbe_cpu_to_node);
  138. u32 cbe_node_to_cpu(int node)
  139. {
  140. return cpumask_first(&cbe_local_mask[node]);
  141. }
  142. EXPORT_SYMBOL_GPL(cbe_node_to_cpu);
  143. static struct device_node *__init cbe_get_be_node(int cpu_id)
  144. {
  145. struct device_node *np;
  146. for_each_node_by_type (np, "be") {
  147. int len,i;
  148. const phandle *cpu_handle;
  149. cpu_handle = of_get_property(np, "cpus", &len);
  150. /*
  151. * the CAB SLOF tree is non compliant, so we just assume
  152. * there is only one node
  153. */
  154. if (WARN_ON_ONCE(!cpu_handle))
  155. return np;
  156. for (i = 0; i < len; i++) {
  157. struct device_node *ch_np = of_find_node_by_phandle(cpu_handle[i]);
  158. struct device_node *ci_np = of_get_cpu_node(cpu_id, NULL);
  159. of_node_put(ch_np);
  160. of_node_put(ci_np);
  161. if (ch_np == ci_np)
  162. return np;
  163. }
  164. }
  165. return NULL;
  166. }
  167. static void __init cbe_fill_regs_map(struct cbe_regs_map *map)
  168. {
  169. if(map->be_node) {
  170. struct device_node *be, *np, *parent_np;
  171. be = map->be_node;
  172. for_each_node_by_type(np, "pervasive") {
  173. parent_np = of_get_parent(np);
  174. if (parent_np == be)
  175. map->pmd_regs = of_iomap(np, 0);
  176. of_node_put(parent_np);
  177. }
  178. for_each_node_by_type(np, "CBEA-Internal-Interrupt-Controller") {
  179. parent_np = of_get_parent(np);
  180. if (parent_np == be)
  181. map->iic_regs = of_iomap(np, 2);
  182. of_node_put(parent_np);
  183. }
  184. for_each_node_by_type(np, "mic-tm") {
  185. parent_np = of_get_parent(np);
  186. if (parent_np == be)
  187. map->mic_tm_regs = of_iomap(np, 0);
  188. of_node_put(parent_np);
  189. }
  190. } else {
  191. struct device_node *cpu;
  192. /* That hack must die die die ! */
  193. const struct address_prop {
  194. unsigned long address;
  195. unsigned int len;
  196. } __attribute__((packed)) *prop;
  197. cpu = map->cpu_node;
  198. prop = of_get_property(cpu, "pervasive", NULL);
  199. if (prop != NULL)
  200. map->pmd_regs = ioremap(prop->address, prop->len);
  201. prop = of_get_property(cpu, "iic", NULL);
  202. if (prop != NULL)
  203. map->iic_regs = ioremap(prop->address, prop->len);
  204. prop = of_get_property(cpu, "mic-tm", NULL);
  205. if (prop != NULL)
  206. map->mic_tm_regs = ioremap(prop->address, prop->len);
  207. }
  208. }
  209. void __init cbe_regs_init(void)
  210. {
  211. int i;
  212. unsigned int thread_id;
  213. struct device_node *cpu;
  214. /* Build local fast map of CPUs */
  215. for_each_possible_cpu(i) {
  216. cbe_thread_map[i].cpu_node = of_get_cpu_node(i, &thread_id);
  217. cbe_thread_map[i].be_node = cbe_get_be_node(i);
  218. cbe_thread_map[i].thread_id = thread_id;
  219. }
  220. /* Find maps for each device tree CPU */
  221. for_each_node_by_type(cpu, "cpu") {
  222. struct cbe_regs_map *map;
  223. unsigned int cbe_id;
  224. cbe_id = cbe_regs_map_count++;
  225. map = &cbe_regs_maps[cbe_id];
  226. if (cbe_regs_map_count > MAX_CBE) {
  227. printk(KERN_ERR "cbe_regs: More BE chips than supported"
  228. "!\n");
  229. cbe_regs_map_count--;
  230. of_node_put(cpu);
  231. return;
  232. }
  233. of_node_put(map->cpu_node);
  234. map->cpu_node = of_node_get(cpu);
  235. for_each_possible_cpu(i) {
  236. struct cbe_thread_map *thread = &cbe_thread_map[i];
  237. if (thread->cpu_node == cpu) {
  238. thread->regs = map;
  239. thread->cbe_id = cbe_id;
  240. map->be_node = thread->be_node;
  241. cpumask_set_cpu(i, &cbe_local_mask[cbe_id]);
  242. if(thread->thread_id == 0)
  243. cpumask_set_cpu(i, &cbe_first_online_cpu);
  244. }
  245. }
  246. cbe_fill_regs_map(map);
  247. }
  248. }