spu_manage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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. unsigned int n = 0;
  244. ret = -ENODEV;
  245. for (node = of_find_node_by_type(NULL, "spe");
  246. node; node = of_find_node_by_type(node, "spe")) {
  247. ret = fn(node);
  248. if (ret) {
  249. printk(KERN_WARNING "%s: Error initializing %s\n",
  250. __FUNCTION__, node->name);
  251. break;
  252. }
  253. n++;
  254. }
  255. return ret ? ret : n;
  256. }
  257. static int __init of_create_spu(struct spu *spu, void *data)
  258. {
  259. int ret;
  260. struct device_node *spe = (struct device_node *)data;
  261. static int legacy_map = 0, legacy_irq = 0;
  262. spu->devnode = of_node_get(spe);
  263. spu->spe_id = find_spu_unit_number(spe);
  264. spu->node = of_node_to_nid(spe);
  265. if (spu->node >= MAX_NUMNODES) {
  266. printk(KERN_WARNING "SPE %s on node %d ignored,"
  267. " node number too big\n", spe->full_name, spu->node);
  268. printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
  269. ret = -ENODEV;
  270. goto out;
  271. }
  272. ret = spu_map_device(spu);
  273. if (ret) {
  274. if (!legacy_map) {
  275. legacy_map = 1;
  276. printk(KERN_WARNING "%s: Legacy device tree found, "
  277. "trying to map old style\n", __FUNCTION__);
  278. }
  279. ret = spu_map_device_old(spu);
  280. if (ret) {
  281. printk(KERN_ERR "Unable to map %s\n",
  282. spu->name);
  283. goto out;
  284. }
  285. }
  286. ret = spu_map_interrupts(spu, spe);
  287. if (ret) {
  288. if (!legacy_irq) {
  289. legacy_irq = 1;
  290. printk(KERN_WARNING "%s: Legacy device tree found, "
  291. "trying old style irq\n", __FUNCTION__);
  292. }
  293. ret = spu_map_interrupts_old(spu, spe);
  294. if (ret) {
  295. printk(KERN_ERR "%s: could not map interrupts",
  296. spu->name);
  297. goto out_unmap;
  298. }
  299. }
  300. pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
  301. spu->local_store, spu->problem, spu->priv1,
  302. spu->priv2, spu->number);
  303. goto out;
  304. out_unmap:
  305. spu_unmap(spu);
  306. out:
  307. return ret;
  308. }
  309. static int of_destroy_spu(struct spu *spu)
  310. {
  311. spu_unmap(spu);
  312. of_node_put(spu->devnode);
  313. return 0;
  314. }
  315. /* Hardcoded affinity idxs for qs20 */
  316. #define QS20_SPES_PER_BE 8
  317. static int qs20_reg_idxs[QS20_SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
  318. static int qs20_reg_memory[QS20_SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
  319. static struct spu *spu_lookup_reg(int node, u32 reg)
  320. {
  321. struct spu *spu;
  322. u32 *spu_reg;
  323. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  324. spu_reg = (u32*)of_get_property(spu_devnode(spu), "reg", NULL);
  325. if (*spu_reg == reg)
  326. return spu;
  327. }
  328. return NULL;
  329. }
  330. static void init_affinity_qs20_harcoded(void)
  331. {
  332. int node, i;
  333. struct spu *last_spu, *spu;
  334. u32 reg;
  335. for (node = 0; node < MAX_NUMNODES; node++) {
  336. last_spu = NULL;
  337. for (i = 0; i < QS20_SPES_PER_BE; i++) {
  338. reg = qs20_reg_idxs[i];
  339. spu = spu_lookup_reg(node, reg);
  340. if (!spu)
  341. continue;
  342. spu->has_mem_affinity = qs20_reg_memory[reg];
  343. if (last_spu)
  344. list_add_tail(&spu->aff_list,
  345. &last_spu->aff_list);
  346. last_spu = spu;
  347. }
  348. }
  349. }
  350. static int of_has_vicinity(void)
  351. {
  352. struct spu* spu;
  353. spu = list_first_entry(&cbe_spu_info[0].spus, struct spu, cbe_list);
  354. return of_find_property(spu_devnode(spu), "vicinity", NULL) != NULL;
  355. }
  356. static struct spu *devnode_spu(int cbe, struct device_node *dn)
  357. {
  358. struct spu *spu;
  359. list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
  360. if (spu_devnode(spu) == dn)
  361. return spu;
  362. return NULL;
  363. }
  364. static struct spu *
  365. neighbour_spu(int cbe, struct device_node *target, struct device_node *avoid)
  366. {
  367. struct spu *spu;
  368. struct device_node *spu_dn;
  369. const phandle *vic_handles;
  370. int lenp, i;
  371. list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
  372. spu_dn = spu_devnode(spu);
  373. if (spu_dn == avoid)
  374. continue;
  375. vic_handles = of_get_property(spu_dn, "vicinity", &lenp);
  376. for (i=0; i < (lenp / sizeof(phandle)); i++) {
  377. if (vic_handles[i] == target->linux_phandle)
  378. return spu;
  379. }
  380. }
  381. return NULL;
  382. }
  383. static void init_affinity_node(int cbe)
  384. {
  385. struct spu *spu, *last_spu;
  386. struct device_node *vic_dn, *last_spu_dn;
  387. phandle avoid_ph;
  388. const phandle *vic_handles;
  389. const char *name;
  390. int lenp, i, added;
  391. last_spu = list_first_entry(&cbe_spu_info[cbe].spus, struct spu,
  392. cbe_list);
  393. avoid_ph = 0;
  394. for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
  395. last_spu_dn = spu_devnode(last_spu);
  396. vic_handles = of_get_property(last_spu_dn, "vicinity", &lenp);
  397. /*
  398. * Walk through each phandle in vicinity property of the spu
  399. * (tipically two vicinity phandles per spe node)
  400. */
  401. for (i = 0; i < (lenp / sizeof(phandle)); i++) {
  402. if (vic_handles[i] == avoid_ph)
  403. continue;
  404. vic_dn = of_find_node_by_phandle(vic_handles[i]);
  405. if (!vic_dn)
  406. continue;
  407. /* a neighbour might be spe, mic-tm, or bif0 */
  408. name = of_get_property(vic_dn, "name", NULL);
  409. if (!name)
  410. continue;
  411. if (strcmp(name, "spe") == 0) {
  412. spu = devnode_spu(cbe, vic_dn);
  413. avoid_ph = last_spu_dn->linux_phandle;
  414. } else {
  415. /*
  416. * "mic-tm" and "bif0" nodes do not have
  417. * vicinity property. So we need to find the
  418. * spe which has vic_dn as neighbour, but
  419. * skipping the one we came from (last_spu_dn)
  420. */
  421. spu = neighbour_spu(cbe, vic_dn, last_spu_dn);
  422. if (!spu)
  423. continue;
  424. if (!strcmp(name, "mic-tm")) {
  425. last_spu->has_mem_affinity = 1;
  426. spu->has_mem_affinity = 1;
  427. }
  428. avoid_ph = vic_dn->linux_phandle;
  429. }
  430. list_add_tail(&spu->aff_list, &last_spu->aff_list);
  431. last_spu = spu;
  432. break;
  433. }
  434. }
  435. }
  436. static void init_affinity_fw(void)
  437. {
  438. int cbe;
  439. for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
  440. init_affinity_node(cbe);
  441. }
  442. static int __init init_affinity(void)
  443. {
  444. if (of_has_vicinity()) {
  445. init_affinity_fw();
  446. } else {
  447. long root = of_get_flat_dt_root();
  448. if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0"))
  449. init_affinity_qs20_harcoded();
  450. else
  451. printk("No affinity configuration found");
  452. }
  453. return 0;
  454. }
  455. const struct spu_management_ops spu_management_of_ops = {
  456. .enumerate_spus = of_enumerate_spus,
  457. .create_spu = of_create_spu,
  458. .destroy_spu = of_destroy_spu,
  459. .init_affinity = init_affinity,
  460. };