spu_manage.c 13 KB

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