spu_manage.c 12 KB

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