spu_manage.c 13 KB

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