spu_manage.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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/export.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/wait.h>
  26. #include <linux/mm.h>
  27. #include <linux/io.h>
  28. #include <linux/mutex.h>
  29. #include <linux/device.h>
  30. #include <asm/spu.h>
  31. #include <asm/spu_priv1.h>
  32. #include <asm/firmware.h>
  33. #include <asm/prom.h>
  34. #include "spufs/spufs.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. /* new device trees should provide the physical-id attribute */
  46. prop = of_get_property(spe, "physical-id", &proplen);
  47. if (proplen == 4)
  48. return (u64)*prop;
  49. /* celleb device tree provides the unit-id */
  50. prop = of_get_property(spe, "unit-id", &proplen);
  51. if (proplen == 4)
  52. return (u64)*prop;
  53. /* legacy device trees provide the id in the reg attribute */
  54. prop = of_get_property(spe, "reg", &proplen);
  55. if (proplen == 4)
  56. return (u64)*prop;
  57. return 0;
  58. }
  59. static void spu_unmap(struct spu *spu)
  60. {
  61. if (!firmware_has_feature(FW_FEATURE_LPAR))
  62. iounmap(spu->priv1);
  63. iounmap(spu->priv2);
  64. iounmap(spu->problem);
  65. iounmap((__force u8 __iomem *)spu->local_store);
  66. }
  67. static int __init spu_map_interrupts_old(struct spu *spu,
  68. struct device_node *np)
  69. {
  70. unsigned int isrc;
  71. const u32 *tmp;
  72. int nid;
  73. /* Get the interrupt source unit from the device-tree */
  74. tmp = of_get_property(np, "isrc", NULL);
  75. if (!tmp)
  76. return -ENODEV;
  77. isrc = tmp[0];
  78. tmp = of_get_property(np->parent->parent, "node-id", NULL);
  79. if (!tmp) {
  80. printk(KERN_WARNING "%s: can't find node-id\n", __func__);
  81. nid = spu->node;
  82. } else
  83. nid = tmp[0];
  84. /* Add the node number */
  85. isrc |= nid << IIC_IRQ_NODE_SHIFT;
  86. /* Now map interrupts of all 3 classes */
  87. spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
  88. spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
  89. spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
  90. /* Right now, we only fail if class 2 failed */
  91. return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
  92. }
  93. static void __iomem * __init spu_map_prop_old(struct spu *spu,
  94. struct device_node *n,
  95. const char *name)
  96. {
  97. const struct address_prop {
  98. unsigned long address;
  99. unsigned int len;
  100. } __attribute__((packed)) *prop;
  101. int proplen;
  102. prop = of_get_property(n, name, &proplen);
  103. if (prop == NULL || proplen != sizeof (struct address_prop))
  104. return NULL;
  105. return ioremap(prop->address, prop->len);
  106. }
  107. static int __init spu_map_device_old(struct spu *spu)
  108. {
  109. struct device_node *node = spu->devnode;
  110. const char *prop;
  111. int ret;
  112. ret = -ENODEV;
  113. spu->name = of_get_property(node, "name", NULL);
  114. if (!spu->name)
  115. goto out;
  116. prop = of_get_property(node, "local-store", NULL);
  117. if (!prop)
  118. goto out;
  119. spu->local_store_phys = *(unsigned long *)prop;
  120. /* we use local store as ram, not io memory */
  121. spu->local_store = (void __force *)
  122. spu_map_prop_old(spu, node, "local-store");
  123. if (!spu->local_store)
  124. goto out;
  125. prop = of_get_property(node, "problem", NULL);
  126. if (!prop)
  127. goto out_unmap;
  128. spu->problem_phys = *(unsigned long *)prop;
  129. spu->problem = spu_map_prop_old(spu, node, "problem");
  130. if (!spu->problem)
  131. goto out_unmap;
  132. spu->priv2 = spu_map_prop_old(spu, node, "priv2");
  133. if (!spu->priv2)
  134. goto out_unmap;
  135. if (!firmware_has_feature(FW_FEATURE_LPAR)) {
  136. spu->priv1 = spu_map_prop_old(spu, node, "priv1");
  137. if (!spu->priv1)
  138. goto out_unmap;
  139. }
  140. ret = 0;
  141. goto out;
  142. out_unmap:
  143. spu_unmap(spu);
  144. out:
  145. return ret;
  146. }
  147. static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
  148. {
  149. struct of_phandle_args oirq;
  150. int ret;
  151. int i;
  152. for (i=0; i < 3; i++) {
  153. ret = of_irq_parse_one(np, i, &oirq);
  154. if (ret) {
  155. pr_debug("spu_new: failed to get irq %d\n", i);
  156. goto err;
  157. }
  158. ret = -EINVAL;
  159. pr_debug(" irq %d no 0x%x on %s\n", i, oirq.args[0],
  160. oirq.np->full_name);
  161. spu->irqs[i] = irq_create_of_mapping(&oirq);
  162. if (spu->irqs[i] == NO_IRQ) {
  163. pr_debug("spu_new: failed to map it !\n");
  164. goto err;
  165. }
  166. }
  167. return 0;
  168. err:
  169. pr_debug("failed to map irq %x for spu %s\n", *oirq.args,
  170. spu->name);
  171. for (; i >= 0; i--) {
  172. if (spu->irqs[i] != NO_IRQ)
  173. irq_dispose_mapping(spu->irqs[i]);
  174. }
  175. return ret;
  176. }
  177. static int spu_map_resource(struct spu *spu, int nr,
  178. void __iomem** virt, unsigned long *phys)
  179. {
  180. struct device_node *np = spu->devnode;
  181. struct resource resource = { };
  182. unsigned long len;
  183. int ret;
  184. ret = of_address_to_resource(np, nr, &resource);
  185. if (ret)
  186. return ret;
  187. if (phys)
  188. *phys = resource.start;
  189. len = resource_size(&resource);
  190. *virt = ioremap(resource.start, len);
  191. if (!*virt)
  192. return -EINVAL;
  193. return 0;
  194. }
  195. static int __init spu_map_device(struct spu *spu)
  196. {
  197. struct device_node *np = spu->devnode;
  198. int ret = -ENODEV;
  199. spu->name = of_get_property(np, "name", NULL);
  200. if (!spu->name)
  201. goto out;
  202. ret = spu_map_resource(spu, 0, (void __iomem**)&spu->local_store,
  203. &spu->local_store_phys);
  204. if (ret) {
  205. pr_debug("spu_new: failed to map %s resource 0\n",
  206. np->full_name);
  207. goto out;
  208. }
  209. ret = spu_map_resource(spu, 1, (void __iomem**)&spu->problem,
  210. &spu->problem_phys);
  211. if (ret) {
  212. pr_debug("spu_new: failed to map %s resource 1\n",
  213. np->full_name);
  214. goto out_unmap;
  215. }
  216. ret = spu_map_resource(spu, 2, (void __iomem**)&spu->priv2, NULL);
  217. if (ret) {
  218. pr_debug("spu_new: failed to map %s resource 2\n",
  219. np->full_name);
  220. goto out_unmap;
  221. }
  222. if (!firmware_has_feature(FW_FEATURE_LPAR))
  223. ret = spu_map_resource(spu, 3,
  224. (void __iomem**)&spu->priv1, NULL);
  225. if (ret) {
  226. pr_debug("spu_new: failed to map %s resource 3\n",
  227. np->full_name);
  228. goto out_unmap;
  229. }
  230. pr_debug("spu_new: %s maps:\n", np->full_name);
  231. pr_debug(" local store : 0x%016lx -> 0x%p\n",
  232. spu->local_store_phys, spu->local_store);
  233. pr_debug(" problem state : 0x%016lx -> 0x%p\n",
  234. spu->problem_phys, spu->problem);
  235. pr_debug(" priv2 : 0x%p\n", spu->priv2);
  236. pr_debug(" priv1 : 0x%p\n", spu->priv1);
  237. return 0;
  238. out_unmap:
  239. spu_unmap(spu);
  240. out:
  241. pr_debug("failed to map spe %s: %d\n", spu->name, ret);
  242. return ret;
  243. }
  244. static int __init of_enumerate_spus(int (*fn)(void *data))
  245. {
  246. int ret;
  247. struct device_node *node;
  248. unsigned int n = 0;
  249. ret = -ENODEV;
  250. for (node = of_find_node_by_type(NULL, "spe");
  251. node; node = of_find_node_by_type(node, "spe")) {
  252. ret = fn(node);
  253. if (ret) {
  254. printk(KERN_WARNING "%s: Error initializing %s\n",
  255. __func__, node->name);
  256. break;
  257. }
  258. n++;
  259. }
  260. return ret ? ret : n;
  261. }
  262. static int __init of_create_spu(struct spu *spu, void *data)
  263. {
  264. int ret;
  265. struct device_node *spe = (struct device_node *)data;
  266. static int legacy_map = 0, legacy_irq = 0;
  267. spu->devnode = of_node_get(spe);
  268. spu->spe_id = find_spu_unit_number(spe);
  269. spu->node = of_node_to_nid(spe);
  270. if (spu->node >= MAX_NUMNODES) {
  271. printk(KERN_WARNING "SPE %s on node %d ignored,"
  272. " node number too big\n", spe->full_name, spu->node);
  273. printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
  274. ret = -ENODEV;
  275. goto out;
  276. }
  277. ret = spu_map_device(spu);
  278. if (ret) {
  279. if (!legacy_map) {
  280. legacy_map = 1;
  281. printk(KERN_WARNING "%s: Legacy device tree found, "
  282. "trying to map old style\n", __func__);
  283. }
  284. ret = spu_map_device_old(spu);
  285. if (ret) {
  286. printk(KERN_ERR "Unable to map %s\n",
  287. spu->name);
  288. goto out;
  289. }
  290. }
  291. ret = spu_map_interrupts(spu, spe);
  292. if (ret) {
  293. if (!legacy_irq) {
  294. legacy_irq = 1;
  295. printk(KERN_WARNING "%s: Legacy device tree found, "
  296. "trying old style irq\n", __func__);
  297. }
  298. ret = spu_map_interrupts_old(spu, spe);
  299. if (ret) {
  300. printk(KERN_ERR "%s: could not map interrupts\n",
  301. spu->name);
  302. goto out_unmap;
  303. }
  304. }
  305. pr_debug("Using SPE %s %p %p %p %p %d\n", spu->name,
  306. spu->local_store, spu->problem, spu->priv1,
  307. spu->priv2, spu->number);
  308. goto out;
  309. out_unmap:
  310. spu_unmap(spu);
  311. out:
  312. return ret;
  313. }
  314. static int of_destroy_spu(struct spu *spu)
  315. {
  316. spu_unmap(spu);
  317. of_node_put(spu->devnode);
  318. return 0;
  319. }
  320. static void enable_spu_by_master_run(struct spu_context *ctx)
  321. {
  322. ctx->ops->master_start(ctx);
  323. }
  324. static void disable_spu_by_master_run(struct spu_context *ctx)
  325. {
  326. ctx->ops->master_stop(ctx);
  327. }
  328. /* Hardcoded affinity idxs for qs20 */
  329. #define QS20_SPES_PER_BE 8
  330. static int qs20_reg_idxs[QS20_SPES_PER_BE] = { 0, 2, 4, 6, 7, 5, 3, 1 };
  331. static int qs20_reg_memory[QS20_SPES_PER_BE] = { 1, 1, 0, 0, 0, 0, 0, 0 };
  332. static struct spu *spu_lookup_reg(int node, u32 reg)
  333. {
  334. struct spu *spu;
  335. const u32 *spu_reg;
  336. list_for_each_entry(spu, &cbe_spu_info[node].spus, cbe_list) {
  337. spu_reg = of_get_property(spu_devnode(spu), "reg", NULL);
  338. if (*spu_reg == reg)
  339. return spu;
  340. }
  341. return NULL;
  342. }
  343. static void init_affinity_qs20_harcoded(void)
  344. {
  345. int node, i;
  346. struct spu *last_spu, *spu;
  347. u32 reg;
  348. for (node = 0; node < MAX_NUMNODES; node++) {
  349. last_spu = NULL;
  350. for (i = 0; i < QS20_SPES_PER_BE; i++) {
  351. reg = qs20_reg_idxs[i];
  352. spu = spu_lookup_reg(node, reg);
  353. if (!spu)
  354. continue;
  355. spu->has_mem_affinity = qs20_reg_memory[reg];
  356. if (last_spu)
  357. list_add_tail(&spu->aff_list,
  358. &last_spu->aff_list);
  359. last_spu = spu;
  360. }
  361. }
  362. }
  363. static int of_has_vicinity(void)
  364. {
  365. struct device_node *dn;
  366. for_each_node_by_type(dn, "spe") {
  367. if (of_find_property(dn, "vicinity", NULL)) {
  368. of_node_put(dn);
  369. return 1;
  370. }
  371. }
  372. return 0;
  373. }
  374. static struct spu *devnode_spu(int cbe, struct device_node *dn)
  375. {
  376. struct spu *spu;
  377. list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list)
  378. if (spu_devnode(spu) == dn)
  379. return spu;
  380. return NULL;
  381. }
  382. static struct spu *
  383. neighbour_spu(int cbe, struct device_node *target, struct device_node *avoid)
  384. {
  385. struct spu *spu;
  386. struct device_node *spu_dn;
  387. const phandle *vic_handles;
  388. int lenp, i;
  389. list_for_each_entry(spu, &cbe_spu_info[cbe].spus, cbe_list) {
  390. spu_dn = spu_devnode(spu);
  391. if (spu_dn == avoid)
  392. continue;
  393. vic_handles = of_get_property(spu_dn, "vicinity", &lenp);
  394. for (i=0; i < (lenp / sizeof(phandle)); i++) {
  395. if (vic_handles[i] == target->phandle)
  396. return spu;
  397. }
  398. }
  399. return NULL;
  400. }
  401. static void init_affinity_node(int cbe)
  402. {
  403. struct spu *spu, *last_spu;
  404. struct device_node *vic_dn, *last_spu_dn;
  405. phandle avoid_ph;
  406. const phandle *vic_handles;
  407. const char *name;
  408. int lenp, i, added;
  409. last_spu = list_first_entry(&cbe_spu_info[cbe].spus, struct spu,
  410. cbe_list);
  411. avoid_ph = 0;
  412. for (added = 1; added < cbe_spu_info[cbe].n_spus; added++) {
  413. last_spu_dn = spu_devnode(last_spu);
  414. vic_handles = of_get_property(last_spu_dn, "vicinity", &lenp);
  415. /*
  416. * Walk through each phandle in vicinity property of the spu
  417. * (tipically two vicinity phandles per spe node)
  418. */
  419. for (i = 0; i < (lenp / sizeof(phandle)); i++) {
  420. if (vic_handles[i] == avoid_ph)
  421. continue;
  422. vic_dn = of_find_node_by_phandle(vic_handles[i]);
  423. if (!vic_dn)
  424. continue;
  425. /* a neighbour might be spe, mic-tm, or bif0 */
  426. name = of_get_property(vic_dn, "name", NULL);
  427. if (!name)
  428. continue;
  429. if (strcmp(name, "spe") == 0) {
  430. spu = devnode_spu(cbe, vic_dn);
  431. avoid_ph = last_spu_dn->phandle;
  432. } else {
  433. /*
  434. * "mic-tm" and "bif0" nodes do not have
  435. * vicinity property. So we need to find the
  436. * spe which has vic_dn as neighbour, but
  437. * skipping the one we came from (last_spu_dn)
  438. */
  439. spu = neighbour_spu(cbe, vic_dn, last_spu_dn);
  440. if (!spu)
  441. continue;
  442. if (!strcmp(name, "mic-tm")) {
  443. last_spu->has_mem_affinity = 1;
  444. spu->has_mem_affinity = 1;
  445. }
  446. avoid_ph = vic_dn->phandle;
  447. }
  448. list_add_tail(&spu->aff_list, &last_spu->aff_list);
  449. last_spu = spu;
  450. break;
  451. }
  452. }
  453. }
  454. static void init_affinity_fw(void)
  455. {
  456. int cbe;
  457. for (cbe = 0; cbe < MAX_NUMNODES; cbe++)
  458. init_affinity_node(cbe);
  459. }
  460. static int __init init_affinity(void)
  461. {
  462. if (of_has_vicinity()) {
  463. init_affinity_fw();
  464. } else {
  465. long root = of_get_flat_dt_root();
  466. if (of_flat_dt_is_compatible(root, "IBM,CPBW-1.0"))
  467. init_affinity_qs20_harcoded();
  468. else
  469. printk("No affinity configuration found\n");
  470. }
  471. return 0;
  472. }
  473. const struct spu_management_ops spu_management_of_ops = {
  474. .enumerate_spus = of_enumerate_spus,
  475. .create_spu = of_create_spu,
  476. .destroy_spu = of_destroy_spu,
  477. .enable_spu = enable_spu_by_master_run,
  478. .disable_spu = disable_spu_by_master_run,
  479. .init_affinity = init_affinity,
  480. };