spu_manage.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * spu management operations for of based platforms
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. * Copyright 2006 Sony Corp.
  6. * (C) Copyright 2007 TOSHIBA CORPORATION
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. #include <linux/interrupt.h>
  22. #include <linux/list.h>
  23. #include <linux/module.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/slab.h>
  26. #include <linux/wait.h>
  27. #include <linux/mm.h>
  28. #include <linux/io.h>
  29. #include <linux/mutex.h>
  30. #include <linux/device.h>
  31. #include <asm/spu.h>
  32. #include <asm/spu_priv1.h>
  33. #include <asm/firmware.h>
  34. #include <asm/prom.h>
  35. #include "interrupt.h"
  36. struct device_node *spu_devnode(struct spu *spu)
  37. {
  38. return spu->devnode;
  39. }
  40. EXPORT_SYMBOL_GPL(spu_devnode);
  41. static u64 __init find_spu_unit_number(struct device_node *spe)
  42. {
  43. const unsigned int *prop;
  44. int proplen;
  45. prop = of_get_property(spe, "unit-id", &proplen);
  46. if (proplen == 4)
  47. return (u64)*prop;
  48. prop = of_get_property(spe, "reg", &proplen);
  49. if (proplen == 4)
  50. return (u64)*prop;
  51. return 0;
  52. }
  53. static void spu_unmap(struct spu *spu)
  54. {
  55. if (!firmware_has_feature(FW_FEATURE_LPAR))
  56. iounmap(spu->priv1);
  57. iounmap(spu->priv2);
  58. iounmap(spu->problem);
  59. iounmap((__force u8 __iomem *)spu->local_store);
  60. }
  61. static int __init spu_map_interrupts_old(struct spu *spu,
  62. struct device_node *np)
  63. {
  64. unsigned int isrc;
  65. const u32 *tmp;
  66. int nid;
  67. /* Get the interrupt source unit from the device-tree */
  68. tmp = of_get_property(np, "isrc", NULL);
  69. if (!tmp)
  70. return -ENODEV;
  71. isrc = tmp[0];
  72. tmp = of_get_property(np->parent->parent, "node-id", NULL);
  73. if (!tmp) {
  74. printk(KERN_WARNING "%s: can't find node-id\n", __FUNCTION__);
  75. nid = spu->node;
  76. } else
  77. nid = tmp[0];
  78. /* Add the node number */
  79. isrc |= nid << IIC_IRQ_NODE_SHIFT;
  80. /* Now map interrupts of all 3 classes */
  81. spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
  82. spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
  83. spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
  84. /* Right now, we only fail if class 2 failed */
  85. return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
  86. }
  87. static void __iomem * __init spu_map_prop_old(struct spu *spu,
  88. struct device_node *n,
  89. const char *name)
  90. {
  91. const struct address_prop {
  92. unsigned long address;
  93. unsigned int len;
  94. } __attribute__((packed)) *prop;
  95. int proplen;
  96. prop = of_get_property(n, name, &proplen);
  97. if (prop == NULL || proplen != sizeof (struct address_prop))
  98. return NULL;
  99. return ioremap(prop->address, prop->len);
  100. }
  101. static int __init spu_map_device_old(struct spu *spu)
  102. {
  103. struct device_node *node = spu->devnode;
  104. const char *prop;
  105. int ret;
  106. ret = -ENODEV;
  107. spu->name = of_get_property(node, "name", NULL);
  108. if (!spu->name)
  109. goto out;
  110. prop = of_get_property(node, "local-store", NULL);
  111. if (!prop)
  112. goto out;
  113. spu->local_store_phys = *(unsigned long *)prop;
  114. /* we use local store as ram, not io memory */
  115. spu->local_store = (void __force *)
  116. spu_map_prop_old(spu, node, "local-store");
  117. if (!spu->local_store)
  118. goto out;
  119. prop = of_get_property(node, "problem", NULL);
  120. if (!prop)
  121. goto out_unmap;
  122. spu->problem_phys = *(unsigned long *)prop;
  123. spu->problem = spu_map_prop_old(spu, node, "problem");
  124. if (!spu->problem)
  125. goto out_unmap;
  126. spu->priv2 = spu_map_prop_old(spu, node, "priv2");
  127. if (!spu->priv2)
  128. goto out_unmap;
  129. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  130. spu->priv1 = spu_map_prop_old(spu, node, "priv1");
  131. if (!spu->priv1)
  132. goto out_unmap;
  133. }
  134. ret = 0;
  135. goto out;
  136. out_unmap:
  137. spu_unmap(spu);
  138. out:
  139. return ret;
  140. }
  141. static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
  142. {
  143. struct of_irq oirq;
  144. int ret;
  145. int i;
  146. for (i=0; i < 3; i++) {
  147. ret = of_irq_map_one(np, i, &oirq);
  148. if (ret) {
  149. pr_debug("spu_new: failed to get irq %d\n", i);
  150. goto err;
  151. }
  152. ret = -EINVAL;
  153. pr_debug(" irq %d no 0x%x on %s\n", i, oirq.specifier[0],
  154. oirq.controller->full_name);
  155. spu->irqs[i] = irq_create_of_mapping(oirq.controller,
  156. oirq.specifier, oirq.size);
  157. if (spu->irqs[i] == NO_IRQ) {
  158. pr_debug("spu_new: failed to map it !\n");
  159. goto err;
  160. }
  161. }
  162. return 0;
  163. err:
  164. pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
  165. spu->name);
  166. for (; i >= 0; i--) {
  167. if (spu->irqs[i] != NO_IRQ)
  168. irq_dispose_mapping(spu->irqs[i]);
  169. }
  170. return ret;
  171. }
  172. static int spu_map_resource(struct spu *spu, int nr,
  173. void __iomem** virt, unsigned long *phys)
  174. {
  175. struct device_node *np = spu->devnode;
  176. struct resource resource = { };
  177. unsigned long len;
  178. int ret;
  179. ret = of_address_to_resource(np, nr, &resource);
  180. if (ret)
  181. return ret;
  182. if (phys)
  183. *phys = resource.start;
  184. len = resource.end - resource.start + 1;
  185. *virt = ioremap(resource.start, len);
  186. if (!*virt)
  187. return -EINVAL;
  188. return 0;
  189. }
  190. static int __init spu_map_device(struct spu *spu)
  191. {
  192. struct device_node *np = spu->devnode;
  193. int ret = -ENODEV;
  194. spu->name = of_get_property(np, "name", NULL);
  195. if (!spu->name)
  196. goto out;
  197. ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
  198. &spu->local_store_phys);
  199. if (ret) {
  200. pr_debug("spu_new: failed to map %s resource 0\n",
  201. np->full_name);
  202. goto out;
  203. }
  204. ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
  205. &spu->problem_phys);
  206. if (ret) {
  207. pr_debug("spu_new: failed to map %s resource 1\n",
  208. np->full_name);
  209. goto out_unmap;
  210. }
  211. ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
  212. if (ret) {
  213. pr_debug("spu_new: failed to map %s resource 2\n",
  214. np->full_name);
  215. goto out_unmap;
  216. }
  217. if (!firmware_has_feature(FW_FEATURE_LPAR))
  218. ret = spu_map_resource(spu, 3,
  219. (void __iomem**)&spu->priv1, NULL);
  220. if (ret) {
  221. pr_debug("spu_new: failed to map %s resource 3\n",
  222. np->full_name);
  223. goto out_unmap;
  224. }
  225. pr_debug("spu_new: %s maps:\n", np->full_name);
  226. pr_debug(" local store : 0x%016lx -> 0x%p\n",
  227. spu->local_store_phys, spu->local_store);
  228. pr_debug(" problem state : 0x%016lx -> 0x%p\n",
  229. spu->problem_phys, spu->problem);
  230. pr_debug(" priv2 : 0x%p\n", spu->priv2);
  231. pr_debug(" priv1 : 0x%p\n", spu->priv1);
  232. return 0;
  233. out_unmap:
  234. spu_unmap(spu);
  235. out:
  236. pr_debug("failed to map spe %s: %d\n", spu->name, ret);
  237. return ret;
  238. }
  239. static int __init of_enumerate_spus(int (*fn)(void *data))
  240. {
  241. int ret;
  242. struct device_node *node;
  243. ret = -ENODEV;
  244. for (node = of_find_node_by_type(NULL, "spe");
  245. node; node = of_find_node_by_type(node, "spe")) {
  246. ret = fn(node);
  247. if (ret) {
  248. printk(KERN_WARNING "%s: Error initializing %s\n",
  249. __FUNCTION__, node->name);
  250. break;
  251. }
  252. }
  253. return ret;
  254. }
  255. static int __init of_create_spu(struct spu *spu, void *data)
  256. {
  257. int ret;
  258. struct device_node *spe = (struct device_node *)data;
  259. static int legacy_map = 0, legacy_irq = 0;
  260. spu->devnode = of_node_get(spe);
  261. spu->spe_id = find_spu_unit_number(spe);
  262. spu->node = of_node_to_nid(spe);
  263. if (spu->node >= MAX_NUMNODES) {
  264. printk(KERN_WARNING "SPE %s on node %d ignored,"
  265. " node number too big\n", spe->full_name, spu->node);
  266. printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
  267. ret = -ENODEV;
  268. goto out;
  269. }
  270. ret = spu_map_device(spu);
  271. if (ret) {
  272. if (!legacy_map) {
  273. legacy_map = 1;
  274. printk(KERN_WARNING "%s: Legacy device tree found, "
  275. "trying to map old style\n", __FUNCTION__);
  276. }
  277. ret = spu_map_device_old(spu);
  278. if (ret) {
  279. printk(KERN_ERR "Unable to map %s\n",
  280. spu->name);
  281. goto out;
  282. }
  283. }
  284. ret = spu_map_interrupts(spu, spe);
  285. if (ret) {
  286. if (!legacy_irq) {
  287. legacy_irq = 1;
  288. printk(KERN_WARNING "%s: Legacy device tree found, "
  289. "trying old style irq\n", __FUNCTION__);
  290. }
  291. ret = spu_map_interrupts_old(spu, spe);
  292. if (ret) {
  293. printk(KERN_ERR "%s: could not map interrupts",
  294. spu->name);
  295. goto out_unmap;
  296. }
  297. }
  298. pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
  299. spu->local_store, spu->problem, spu->priv1,
  300. spu->priv2, spu->number);
  301. goto out;
  302. out_unmap:
  303. spu_unmap(spu);
  304. out:
  305. return ret;
  306. }
  307. static int of_destroy_spu(struct spu *spu)
  308. {
  309. spu_unmap(spu);
  310. of_node_put(spu->devnode);
  311. return 0;
  312. }
  313. const struct spu_management_ops spu_management_of_ops = {
  314. .enumerate_spus = of_enumerate_spus,
  315. .create_spu = of_create_spu,
  316. .destroy_spu = of_destroy_spu,
  317. };