spu_manage.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 = get_property(spe, "unit-id", &proplen);
  46. if (proplen == 4)
  47. return (u64)*prop;
  48. prop = get_property(spe, "reg", &proplen);
  49. if (proplen == 4)
  50. return (u64)*prop;
  51. return 0;
  52. }
  53. static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
  54. const char *prop)
  55. {
  56. const struct address_prop {
  57. unsigned long address;
  58. unsigned int len;
  59. } __attribute__((packed)) *p;
  60. int proplen;
  61. unsigned long start_pfn, nr_pages;
  62. struct pglist_data *pgdata;
  63. struct zone *zone;
  64. int ret;
  65. p = get_property(spe, prop, &proplen);
  66. WARN_ON(proplen != sizeof (*p));
  67. start_pfn = p->address >> PAGE_SHIFT;
  68. nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  69. pgdata = NODE_DATA(spu->node);
  70. zone = pgdata->node_zones;
  71. ret = __add_pages(zone, start_pfn, nr_pages);
  72. return ret;
  73. }
  74. static void __iomem * __init map_spe_prop(struct spu *spu,
  75. struct device_node *n, const char *name)
  76. {
  77. const struct address_prop {
  78. unsigned long address;
  79. unsigned int len;
  80. } __attribute__((packed)) *prop;
  81. const void *p;
  82. int proplen;
  83. void __iomem *ret = NULL;
  84. int err = 0;
  85. p = get_property(n, name, &proplen);
  86. if (proplen != sizeof (struct address_prop))
  87. return NULL;
  88. prop = p;
  89. err = cell_spuprop_present(spu, n, name);
  90. if (err && (err != -EEXIST))
  91. goto out;
  92. ret = ioremap(prop->address, prop->len);
  93. out:
  94. return ret;
  95. }
  96. static void spu_unmap(struct spu *spu)
  97. {
  98. if (!firmware_has_feature(FW_FEATURE_LPAR))
  99. iounmap(spu->priv1);
  100. iounmap(spu->priv2);
  101. iounmap(spu->problem);
  102. iounmap((__force u8 __iomem *)spu->local_store);
  103. }
  104. static int __init spu_map_interrupts_old(struct spu *spu,
  105. struct device_node *np)
  106. {
  107. unsigned int isrc;
  108. const u32 *tmp;
  109. int nid;
  110. /* Get the interrupt source unit from the device-tree */
  111. tmp = get_property(np, "isrc", NULL);
  112. if (!tmp)
  113. return -ENODEV;
  114. isrc = tmp[0];
  115. tmp = get_property(np->parent->parent, "node-id", NULL);
  116. if (!tmp) {
  117. printk(KERN_WARNING "%s: can't find node-id\n", __FUNCTION__);
  118. nid = spu->node;
  119. } else
  120. nid = tmp[0];
  121. /* Add the node number */
  122. isrc |= nid << IIC_IRQ_NODE_SHIFT;
  123. /* Now map interrupts of all 3 classes */
  124. spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
  125. spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
  126. spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
  127. /* Right now, we only fail if class 2 failed */
  128. return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
  129. }
  130. static int __init spu_map_device_old(struct spu *spu)
  131. {
  132. struct device_node *node = spu->devnode;
  133. const char *prop;
  134. int ret;
  135. ret = -ENODEV;
  136. spu->name = get_property(node, "name", NULL);
  137. if (!spu->name)
  138. goto out;
  139. prop = get_property(node, "local-store", NULL);
  140. if (!prop)
  141. goto out;
  142. spu->local_store_phys = *(unsigned long *)prop;
  143. /* we use local store as ram, not io memory */
  144. spu->local_store = (void __force *)
  145. map_spe_prop(spu, node, "local-store");
  146. if (!spu->local_store)
  147. goto out;
  148. prop = get_property(node, "problem", NULL);
  149. if (!prop)
  150. goto out_unmap;
  151. spu->problem_phys = *(unsigned long *)prop;
  152. spu->problem = map_spe_prop(spu, node, "problem");
  153. if (!spu->problem)
  154. goto out_unmap;
  155. spu->priv2 = map_spe_prop(spu, node, "priv2");
  156. if (!spu->priv2)
  157. goto out_unmap;
  158. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  159. spu->priv1 = map_spe_prop(spu, node, "priv1");
  160. if (!spu->priv1)
  161. goto out_unmap;
  162. }
  163. ret = 0;
  164. goto out;
  165. out_unmap:
  166. spu_unmap(spu);
  167. out:
  168. return ret;
  169. }
  170. static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
  171. {
  172. struct of_irq oirq;
  173. int ret;
  174. int i;
  175. for (i=0; i < 3; i++) {
  176. ret = of_irq_map_one(np, i, &oirq);
  177. if (ret) {
  178. pr_debug("spu_new: failed to get irq %d\n", i);
  179. goto err;
  180. }
  181. ret = -EINVAL;
  182. pr_debug(" irq %d no 0x%x on %s\n", i, oirq.specifier[0],
  183. oirq.controller->full_name);
  184. spu->irqs[i] = irq_create_of_mapping(oirq.controller,
  185. oirq.specifier, oirq.size);
  186. if (spu->irqs[i] == NO_IRQ) {
  187. pr_debug("spu_new: failed to map it !\n");
  188. goto err;
  189. }
  190. }
  191. return 0;
  192. err:
  193. pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier,
  194. spu->name);
  195. for (; i >= 0; i--) {
  196. if (spu->irqs[i] != NO_IRQ)
  197. irq_dispose_mapping(spu->irqs[i]);
  198. }
  199. return ret;
  200. }
  201. static int spu_map_resource(struct spu *spu, int nr,
  202. void __iomem** virt, unsigned long *phys)
  203. {
  204. struct device_node *np = spu->devnode;
  205. unsigned long start_pfn, nr_pages;
  206. struct pglist_data *pgdata;
  207. struct zone *zone;
  208. struct resource resource = { };
  209. unsigned long len;
  210. int ret;
  211. ret = of_address_to_resource(np, nr, &resource);
  212. if (ret)
  213. goto out;
  214. if (phys)
  215. *phys = resource.start;
  216. len = resource.end - resource.start + 1;
  217. *virt = ioremap(resource.start, len);
  218. if (!*virt)
  219. ret = -EINVAL;
  220. start_pfn = resource.start >> PAGE_SHIFT;
  221. nr_pages = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  222. pgdata = NODE_DATA(spu->node);
  223. zone = pgdata->node_zones;
  224. ret = __add_pages(zone, start_pfn, nr_pages);
  225. out:
  226. return ret;
  227. }
  228. static int __init spu_map_device(struct spu *spu)
  229. {
  230. struct device_node *np = spu->devnode;
  231. int ret = -ENODEV;
  232. spu->name = get_property(np, "name", NULL);
  233. if (!spu->name)
  234. goto out;
  235. ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
  236. &spu->local_store_phys);
  237. if (ret) {
  238. pr_debug("spu_new: failed to map %s resource 0\n",
  239. np->full_name);
  240. goto out;
  241. }
  242. ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
  243. &spu->problem_phys);
  244. if (ret) {
  245. pr_debug("spu_new: failed to map %s resource 1\n",
  246. np->full_name);
  247. goto out_unmap;
  248. }
  249. ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
  250. if (ret) {
  251. pr_debug("spu_new: failed to map %s resource 2\n",
  252. np->full_name);
  253. goto out_unmap;
  254. }
  255. if (!firmware_has_feature(FW_FEATURE_LPAR))
  256. ret = spu_map_resource(spu, 3,
  257. (void __iomem**)&spu->priv1, NULL);
  258. if (ret) {
  259. pr_debug("spu_new: failed to map %s resource 3\n",
  260. np->full_name);
  261. goto out_unmap;
  262. }
  263. pr_debug("spu_new: %s maps:\n", np->full_name);
  264. pr_debug(" local store : 0x%016lx -> 0x%p\n",
  265. spu->local_store_phys, spu->local_store);
  266. pr_debug(" problem state : 0x%016lx -> 0x%p\n",
  267. spu->problem_phys, spu->problem);
  268. pr_debug(" priv2 : 0x%p\n", spu->priv2);
  269. pr_debug(" priv1 : 0x%p\n", spu->priv1);
  270. return 0;
  271. out_unmap:
  272. spu_unmap(spu);
  273. out:
  274. pr_debug("failed to map spe %s: %d\n", spu->name, ret);
  275. return ret;
  276. }
  277. static int __init of_enumerate_spus(int (*fn)(void *data))
  278. {
  279. int ret;
  280. struct device_node *node;
  281. ret = -ENODEV;
  282. for (node = of_find_node_by_type(NULL, "spe");
  283. node; node = of_find_node_by_type(node, "spe")) {
  284. ret = fn(node);
  285. if (ret) {
  286. printk(KERN_WARNING "%s: Error initializing %s\n",
  287. __FUNCTION__, node->name);
  288. break;
  289. }
  290. }
  291. return ret;
  292. }
  293. static int __init of_create_spu(struct spu *spu, void *data)
  294. {
  295. int ret;
  296. struct device_node *spe = (struct device_node *)data;
  297. static int legacy_map = 0, legacy_irq = 0;
  298. spu->devnode = of_node_get(spe);
  299. spu->spe_id = find_spu_unit_number(spe);
  300. spu->node = of_node_to_nid(spe);
  301. if (spu->node >= MAX_NUMNODES) {
  302. printk(KERN_WARNING "SPE %s on node %d ignored,"
  303. " node number too big\n", spe->full_name, spu->node);
  304. printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
  305. ret = -ENODEV;
  306. goto out;
  307. }
  308. ret = spu_map_device(spu);
  309. if (ret) {
  310. if (!legacy_map) {
  311. legacy_map = 1;
  312. printk(KERN_WARNING "%s: Legacy device tree found, "
  313. "trying to map old style\n", __FUNCTION__);
  314. }
  315. ret = spu_map_device_old(spu);
  316. if (ret) {
  317. printk(KERN_ERR "Unable to map %s\n",
  318. spu->name);
  319. goto out;
  320. }
  321. }
  322. ret = spu_map_interrupts(spu, spe);
  323. if (ret) {
  324. if (!legacy_irq) {
  325. legacy_irq = 1;
  326. printk(KERN_WARNING "%s: Legacy device tree found, "
  327. "trying old style irq\n", __FUNCTION__);
  328. }
  329. ret = spu_map_interrupts_old(spu, spe);
  330. if (ret) {
  331. printk(KERN_ERR "%s: could not map interrupts",
  332. spu->name);
  333. goto out_unmap;
  334. }
  335. }
  336. pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
  337. spu->local_store, spu->problem, spu->priv1,
  338. spu->priv2, spu->number);
  339. goto out;
  340. out_unmap:
  341. spu_unmap(spu);
  342. out:
  343. return ret;
  344. }
  345. static int of_destroy_spu(struct spu *spu)
  346. {
  347. spu_unmap(spu);
  348. of_node_put(spu->devnode);
  349. return 0;
  350. }
  351. const struct spu_management_ops spu_management_of_ops = {
  352. .enumerate_spus = of_enumerate_spus,
  353. .create_spu = of_create_spu,
  354. .destroy_spu = of_destroy_spu,
  355. };