spu_base.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /*
  2. * Low-level SPU handling
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  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; either version 2, or (at your option)
  11. * any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #undef DEBUG
  23. #include <linux/interrupt.h>
  24. #include <linux/list.h>
  25. #include <linux/module.h>
  26. #include <linux/poll.h>
  27. #include <linux/ptrace.h>
  28. #include <linux/slab.h>
  29. #include <linux/wait.h>
  30. #include <asm/io.h>
  31. #include <asm/prom.h>
  32. #include <linux/mutex.h>
  33. #include <asm/spu.h>
  34. #include <asm/spu_priv1.h>
  35. #include <asm/mmu_context.h>
  36. #include "interrupt.h"
  37. const struct spu_priv1_ops *spu_priv1_ops;
  38. EXPORT_SYMBOL_GPL(spu_priv1_ops);
  39. static int __spu_trap_invalid_dma(struct spu *spu)
  40. {
  41. pr_debug("%s\n", __FUNCTION__);
  42. force_sig(SIGBUS, /* info, */ current);
  43. return 0;
  44. }
  45. static int __spu_trap_dma_align(struct spu *spu)
  46. {
  47. pr_debug("%s\n", __FUNCTION__);
  48. force_sig(SIGBUS, /* info, */ current);
  49. return 0;
  50. }
  51. static int __spu_trap_error(struct spu *spu)
  52. {
  53. pr_debug("%s\n", __FUNCTION__);
  54. force_sig(SIGILL, /* info, */ current);
  55. return 0;
  56. }
  57. static void spu_restart_dma(struct spu *spu)
  58. {
  59. struct spu_priv2 __iomem *priv2 = spu->priv2;
  60. if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
  61. out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
  62. }
  63. static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
  64. {
  65. struct spu_priv2 __iomem *priv2 = spu->priv2;
  66. struct mm_struct *mm = spu->mm;
  67. u64 esid, vsid, llp;
  68. pr_debug("%s\n", __FUNCTION__);
  69. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
  70. /* SLBs are pre-loaded for context switch, so
  71. * we should never get here!
  72. */
  73. printk("%s: invalid access during switch!\n", __func__);
  74. return 1;
  75. }
  76. if (!mm || (REGION_ID(ea) != USER_REGION_ID)) {
  77. /* Future: support kernel segments so that drivers
  78. * can use SPUs.
  79. */
  80. pr_debug("invalid region access at %016lx\n", ea);
  81. return 1;
  82. }
  83. esid = (ea & ESID_MASK) | SLB_ESID_V;
  84. #ifdef CONFIG_HUGETLB_PAGE
  85. if (in_hugepage_area(mm->context, ea))
  86. llp = mmu_psize_defs[mmu_huge_psize].sllp;
  87. else
  88. #endif
  89. llp = mmu_psize_defs[mmu_virtual_psize].sllp;
  90. vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
  91. SLB_VSID_USER | llp;
  92. out_be64(&priv2->slb_index_W, spu->slb_replace);
  93. out_be64(&priv2->slb_vsid_RW, vsid);
  94. out_be64(&priv2->slb_esid_RW, esid);
  95. spu->slb_replace++;
  96. if (spu->slb_replace >= 8)
  97. spu->slb_replace = 0;
  98. spu_restart_dma(spu);
  99. return 0;
  100. }
  101. extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
  102. static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
  103. {
  104. pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
  105. /* Handle kernel space hash faults immediately.
  106. User hash faults need to be deferred to process context. */
  107. if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
  108. && REGION_ID(ea) != USER_REGION_ID
  109. && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
  110. spu_restart_dma(spu);
  111. return 0;
  112. }
  113. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
  114. printk("%s: invalid access during switch!\n", __func__);
  115. return 1;
  116. }
  117. spu->dar = ea;
  118. spu->dsisr = dsisr;
  119. mb();
  120. spu->stop_callback(spu);
  121. return 0;
  122. }
  123. static irqreturn_t
  124. spu_irq_class_0(int irq, void *data, struct pt_regs *regs)
  125. {
  126. struct spu *spu;
  127. spu = data;
  128. spu->class_0_pending = 1;
  129. spu->stop_callback(spu);
  130. return IRQ_HANDLED;
  131. }
  132. int
  133. spu_irq_class_0_bottom(struct spu *spu)
  134. {
  135. unsigned long stat, mask;
  136. spu->class_0_pending = 0;
  137. mask = spu_int_mask_get(spu, 0);
  138. stat = spu_int_stat_get(spu, 0);
  139. stat &= mask;
  140. if (stat & 1) /* invalid MFC DMA */
  141. __spu_trap_invalid_dma(spu);
  142. if (stat & 2) /* invalid DMA alignment */
  143. __spu_trap_dma_align(spu);
  144. if (stat & 4) /* error on SPU */
  145. __spu_trap_error(spu);
  146. spu_int_stat_clear(spu, 0, stat);
  147. return (stat & 0x7) ? -EIO : 0;
  148. }
  149. EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
  150. static irqreturn_t
  151. spu_irq_class_1(int irq, void *data, struct pt_regs *regs)
  152. {
  153. struct spu *spu;
  154. unsigned long stat, mask, dar, dsisr;
  155. spu = data;
  156. /* atomically read & clear class1 status. */
  157. spin_lock(&spu->register_lock);
  158. mask = spu_int_mask_get(spu, 1);
  159. stat = spu_int_stat_get(spu, 1) & mask;
  160. dar = spu_mfc_dar_get(spu);
  161. dsisr = spu_mfc_dsisr_get(spu);
  162. if (stat & 2) /* mapping fault */
  163. spu_mfc_dsisr_set(spu, 0ul);
  164. spu_int_stat_clear(spu, 1, stat);
  165. spin_unlock(&spu->register_lock);
  166. pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
  167. dar, dsisr);
  168. if (stat & 1) /* segment fault */
  169. __spu_trap_data_seg(spu, dar);
  170. if (stat & 2) { /* mapping fault */
  171. __spu_trap_data_map(spu, dar, dsisr);
  172. }
  173. if (stat & 4) /* ls compare & suspend on get */
  174. ;
  175. if (stat & 8) /* ls compare & suspend on put */
  176. ;
  177. return stat ? IRQ_HANDLED : IRQ_NONE;
  178. }
  179. EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
  180. static irqreturn_t
  181. spu_irq_class_2(int irq, void *data, struct pt_regs *regs)
  182. {
  183. struct spu *spu;
  184. unsigned long stat;
  185. unsigned long mask;
  186. spu = data;
  187. spin_lock(&spu->register_lock);
  188. stat = spu_int_stat_get(spu, 2);
  189. mask = spu_int_mask_get(spu, 2);
  190. /* ignore interrupts we're not waiting for */
  191. stat &= mask;
  192. /*
  193. * mailbox interrupts (0x1 and 0x10) are level triggered.
  194. * mask them now before acknowledging.
  195. */
  196. if (stat & 0x11)
  197. spu_int_mask_and(spu, 2, ~(stat & 0x11));
  198. /* acknowledge all interrupts before the callbacks */
  199. spu_int_stat_clear(spu, 2, stat);
  200. spin_unlock(&spu->register_lock);
  201. pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
  202. if (stat & 1) /* PPC core mailbox */
  203. spu->ibox_callback(spu);
  204. if (stat & 2) /* SPU stop-and-signal */
  205. spu->stop_callback(spu);
  206. if (stat & 4) /* SPU halted */
  207. spu->stop_callback(spu);
  208. if (stat & 8) /* DMA tag group complete */
  209. spu->mfc_callback(spu);
  210. if (stat & 0x10) /* SPU mailbox threshold */
  211. spu->wbox_callback(spu);
  212. return stat ? IRQ_HANDLED : IRQ_NONE;
  213. }
  214. static int
  215. spu_request_irqs(struct spu *spu)
  216. {
  217. int ret;
  218. int irq_base;
  219. irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
  220. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", spu->number);
  221. ret = request_irq(irq_base + spu->isrc,
  222. spu_irq_class_0, SA_INTERRUPT, spu->irq_c0, spu);
  223. if (ret)
  224. goto out;
  225. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", spu->number);
  226. ret = request_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc,
  227. spu_irq_class_1, SA_INTERRUPT, spu->irq_c1, spu);
  228. if (ret)
  229. goto out1;
  230. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", spu->number);
  231. ret = request_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc,
  232. spu_irq_class_2, SA_INTERRUPT, spu->irq_c2, spu);
  233. if (ret)
  234. goto out2;
  235. goto out;
  236. out2:
  237. free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
  238. out1:
  239. free_irq(irq_base + spu->isrc, spu);
  240. out:
  241. return ret;
  242. }
  243. static void
  244. spu_free_irqs(struct spu *spu)
  245. {
  246. int irq_base;
  247. irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
  248. free_irq(irq_base + spu->isrc, spu);
  249. free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
  250. free_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, spu);
  251. }
  252. static LIST_HEAD(spu_list);
  253. static DEFINE_MUTEX(spu_mutex);
  254. static void spu_init_channels(struct spu *spu)
  255. {
  256. static const struct {
  257. unsigned channel;
  258. unsigned count;
  259. } zero_list[] = {
  260. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  261. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  262. }, count_list[] = {
  263. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  264. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  265. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  266. };
  267. struct spu_priv2 __iomem *priv2;
  268. int i;
  269. priv2 = spu->priv2;
  270. /* initialize all channel data to zero */
  271. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  272. int count;
  273. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  274. for (count = 0; count < zero_list[i].count; count++)
  275. out_be64(&priv2->spu_chnldata_RW, 0);
  276. }
  277. /* initialize channel counts to meaningful values */
  278. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  279. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  280. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  281. }
  282. }
  283. struct spu *spu_alloc(void)
  284. {
  285. struct spu *spu;
  286. mutex_lock(&spu_mutex);
  287. if (!list_empty(&spu_list)) {
  288. spu = list_entry(spu_list.next, struct spu, list);
  289. list_del_init(&spu->list);
  290. pr_debug("Got SPU %x %d\n", spu->isrc, spu->number);
  291. } else {
  292. pr_debug("No SPU left\n");
  293. spu = NULL;
  294. }
  295. mutex_unlock(&spu_mutex);
  296. if (spu)
  297. spu_init_channels(spu);
  298. return spu;
  299. }
  300. EXPORT_SYMBOL_GPL(spu_alloc);
  301. void spu_free(struct spu *spu)
  302. {
  303. mutex_lock(&spu_mutex);
  304. list_add_tail(&spu->list, &spu_list);
  305. mutex_unlock(&spu_mutex);
  306. }
  307. EXPORT_SYMBOL_GPL(spu_free);
  308. static int spu_handle_mm_fault(struct spu *spu)
  309. {
  310. struct mm_struct *mm = spu->mm;
  311. struct vm_area_struct *vma;
  312. u64 ea, dsisr, is_write;
  313. int ret;
  314. ea = spu->dar;
  315. dsisr = spu->dsisr;
  316. #if 0
  317. if (!IS_VALID_EA(ea)) {
  318. return -EFAULT;
  319. }
  320. #endif /* XXX */
  321. if (mm == NULL) {
  322. return -EFAULT;
  323. }
  324. if (mm->pgd == NULL) {
  325. return -EFAULT;
  326. }
  327. down_read(&mm->mmap_sem);
  328. vma = find_vma(mm, ea);
  329. if (!vma)
  330. goto bad_area;
  331. if (vma->vm_start <= ea)
  332. goto good_area;
  333. if (!(vma->vm_flags & VM_GROWSDOWN))
  334. goto bad_area;
  335. #if 0
  336. if (expand_stack(vma, ea))
  337. goto bad_area;
  338. #endif /* XXX */
  339. good_area:
  340. is_write = dsisr & MFC_DSISR_ACCESS_PUT;
  341. if (is_write) {
  342. if (!(vma->vm_flags & VM_WRITE))
  343. goto bad_area;
  344. } else {
  345. if (dsisr & MFC_DSISR_ACCESS_DENIED)
  346. goto bad_area;
  347. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  348. goto bad_area;
  349. }
  350. ret = 0;
  351. switch (handle_mm_fault(mm, vma, ea, is_write)) {
  352. case VM_FAULT_MINOR:
  353. current->min_flt++;
  354. break;
  355. case VM_FAULT_MAJOR:
  356. current->maj_flt++;
  357. break;
  358. case VM_FAULT_SIGBUS:
  359. ret = -EFAULT;
  360. goto bad_area;
  361. case VM_FAULT_OOM:
  362. ret = -ENOMEM;
  363. goto bad_area;
  364. default:
  365. BUG();
  366. }
  367. up_read(&mm->mmap_sem);
  368. return ret;
  369. bad_area:
  370. up_read(&mm->mmap_sem);
  371. return -EFAULT;
  372. }
  373. int spu_irq_class_1_bottom(struct spu *spu)
  374. {
  375. u64 ea, dsisr, access, error = 0UL;
  376. int ret = 0;
  377. ea = spu->dar;
  378. dsisr = spu->dsisr;
  379. if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
  380. u64 flags;
  381. access = (_PAGE_PRESENT | _PAGE_USER);
  382. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  383. local_irq_save(flags);
  384. if (hash_page(ea, access, 0x300) != 0)
  385. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  386. local_irq_restore(flags);
  387. }
  388. if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
  389. if ((ret = spu_handle_mm_fault(spu)) != 0)
  390. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  391. else
  392. error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
  393. }
  394. spu->dar = 0UL;
  395. spu->dsisr = 0UL;
  396. if (!error) {
  397. spu_restart_dma(spu);
  398. } else {
  399. __spu_trap_invalid_dma(spu);
  400. }
  401. return ret;
  402. }
  403. static int __init find_spu_node_id(struct device_node *spe)
  404. {
  405. unsigned int *id;
  406. struct device_node *cpu;
  407. cpu = spe->parent->parent;
  408. id = (unsigned int *)get_property(cpu, "node-id", NULL);
  409. return id ? *id : 0;
  410. }
  411. static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
  412. const char *prop)
  413. {
  414. static DEFINE_MUTEX(add_spumem_mutex);
  415. struct address_prop {
  416. unsigned long address;
  417. unsigned int len;
  418. } __attribute__((packed)) *p;
  419. int proplen;
  420. unsigned long start_pfn, nr_pages;
  421. struct pglist_data *pgdata;
  422. struct zone *zone;
  423. int ret;
  424. p = (void*)get_property(spe, prop, &proplen);
  425. WARN_ON(proplen != sizeof (*p));
  426. start_pfn = p->address >> PAGE_SHIFT;
  427. nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  428. pgdata = NODE_DATA(spu->nid);
  429. zone = pgdata->node_zones;
  430. /* XXX rethink locking here */
  431. mutex_lock(&add_spumem_mutex);
  432. ret = __add_pages(zone, start_pfn, nr_pages);
  433. mutex_unlock(&add_spumem_mutex);
  434. return ret;
  435. }
  436. static void __iomem * __init map_spe_prop(struct spu *spu,
  437. struct device_node *n, const char *name)
  438. {
  439. struct address_prop {
  440. unsigned long address;
  441. unsigned int len;
  442. } __attribute__((packed)) *prop;
  443. void *p;
  444. int proplen;
  445. void* ret = NULL;
  446. int err = 0;
  447. p = get_property(n, name, &proplen);
  448. if (proplen != sizeof (struct address_prop))
  449. return NULL;
  450. prop = p;
  451. err = cell_spuprop_present(spu, n, name);
  452. if (err && (err != -EEXIST))
  453. goto out;
  454. ret = ioremap(prop->address, prop->len);
  455. out:
  456. return ret;
  457. }
  458. static void spu_unmap(struct spu *spu)
  459. {
  460. iounmap(spu->priv2);
  461. iounmap(spu->priv1);
  462. iounmap(spu->problem);
  463. iounmap((u8 __iomem *)spu->local_store);
  464. }
  465. static int __init spu_map_device(struct spu *spu, struct device_node *node)
  466. {
  467. char *prop;
  468. int ret;
  469. ret = -ENODEV;
  470. prop = get_property(node, "isrc", NULL);
  471. if (!prop)
  472. goto out;
  473. spu->isrc = *(unsigned int *)prop;
  474. spu->name = get_property(node, "name", NULL);
  475. if (!spu->name)
  476. goto out;
  477. prop = get_property(node, "local-store", NULL);
  478. if (!prop)
  479. goto out;
  480. spu->local_store_phys = *(unsigned long *)prop;
  481. /* we use local store as ram, not io memory */
  482. spu->local_store = (void __force *)
  483. map_spe_prop(spu, node, "local-store");
  484. if (!spu->local_store)
  485. goto out;
  486. prop = get_property(node, "problem", NULL);
  487. if (!prop)
  488. goto out_unmap;
  489. spu->problem_phys = *(unsigned long *)prop;
  490. spu->problem= map_spe_prop(spu, node, "problem");
  491. if (!spu->problem)
  492. goto out_unmap;
  493. spu->priv1= map_spe_prop(spu, node, "priv1");
  494. /* priv1 is not available on a hypervisor */
  495. spu->priv2= map_spe_prop(spu, node, "priv2");
  496. if (!spu->priv2)
  497. goto out_unmap;
  498. ret = 0;
  499. goto out;
  500. out_unmap:
  501. spu_unmap(spu);
  502. out:
  503. return ret;
  504. }
  505. struct sysdev_class spu_sysdev_class = {
  506. set_kset_name("spu")
  507. };
  508. static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
  509. {
  510. struct spu *spu = container_of(sysdev, struct spu, sysdev);
  511. return sprintf(buf, "%d\n", spu->isrc);
  512. }
  513. static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
  514. extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
  515. static int spu_create_sysdev(struct spu *spu)
  516. {
  517. int ret;
  518. spu->sysdev.id = spu->number;
  519. spu->sysdev.cls = &spu_sysdev_class;
  520. ret = sysdev_register(&spu->sysdev);
  521. if (ret) {
  522. printk(KERN_ERR "Can't register SPU %d with sysfs\n",
  523. spu->number);
  524. return ret;
  525. }
  526. sysdev_create_file(&spu->sysdev, &attr_isrc);
  527. sysfs_add_device_to_node(&spu->sysdev, spu->nid);
  528. return 0;
  529. }
  530. static void spu_destroy_sysdev(struct spu *spu)
  531. {
  532. sysdev_remove_file(&spu->sysdev, &attr_isrc);
  533. sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
  534. sysdev_unregister(&spu->sysdev);
  535. }
  536. static int __init create_spu(struct device_node *spe)
  537. {
  538. struct spu *spu;
  539. int ret;
  540. static int number;
  541. ret = -ENOMEM;
  542. spu = kzalloc(sizeof (*spu), GFP_KERNEL);
  543. if (!spu)
  544. goto out;
  545. ret = spu_map_device(spu, spe);
  546. if (ret)
  547. goto out_free;
  548. spu->node = find_spu_node_id(spe);
  549. spu->nid = of_node_to_nid(spe);
  550. if (spu->nid == -1)
  551. spu->nid = 0;
  552. spin_lock_init(&spu->register_lock);
  553. spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1));
  554. spu_mfc_sr1_set(spu, 0x33);
  555. mutex_lock(&spu_mutex);
  556. spu->number = number++;
  557. ret = spu_request_irqs(spu);
  558. if (ret)
  559. goto out_unmap;
  560. ret = spu_create_sysdev(spu);
  561. if (ret)
  562. goto out_free_irqs;
  563. list_add(&spu->list, &spu_list);
  564. mutex_unlock(&spu_mutex);
  565. pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
  566. spu->name, spu->isrc, spu->local_store,
  567. spu->problem, spu->priv1, spu->priv2, spu->number);
  568. goto out;
  569. out_free_irqs:
  570. spu_free_irqs(spu);
  571. out_unmap:
  572. mutex_unlock(&spu_mutex);
  573. spu_unmap(spu);
  574. out_free:
  575. kfree(spu);
  576. out:
  577. return ret;
  578. }
  579. static void destroy_spu(struct spu *spu)
  580. {
  581. list_del_init(&spu->list);
  582. spu_destroy_sysdev(spu);
  583. spu_free_irqs(spu);
  584. spu_unmap(spu);
  585. kfree(spu);
  586. }
  587. static void cleanup_spu_base(void)
  588. {
  589. struct spu *spu, *tmp;
  590. mutex_lock(&spu_mutex);
  591. list_for_each_entry_safe(spu, tmp, &spu_list, list)
  592. destroy_spu(spu);
  593. mutex_unlock(&spu_mutex);
  594. sysdev_class_unregister(&spu_sysdev_class);
  595. }
  596. module_exit(cleanup_spu_base);
  597. static int __init init_spu_base(void)
  598. {
  599. struct device_node *node;
  600. int ret;
  601. /* create sysdev class for spus */
  602. ret = sysdev_class_register(&spu_sysdev_class);
  603. if (ret)
  604. return ret;
  605. ret = -ENODEV;
  606. for (node = of_find_node_by_type(NULL, "spe");
  607. node; node = of_find_node_by_type(node, "spe")) {
  608. ret = create_spu(node);
  609. if (ret) {
  610. printk(KERN_WARNING "%s: Error initializing %s\n",
  611. __FUNCTION__, node->name);
  612. cleanup_spu_base();
  613. break;
  614. }
  615. }
  616. /* in some old firmware versions, the spe is called 'spc', so we
  617. look for that as well */
  618. for (node = of_find_node_by_type(NULL, "spc");
  619. node; node = of_find_node_by_type(node, "spc")) {
  620. ret = create_spu(node);
  621. if (ret) {
  622. printk(KERN_WARNING "%s: Error initializing %s\n",
  623. __FUNCTION__, node->name);
  624. cleanup_spu_base();
  625. break;
  626. }
  627. }
  628. return ret;
  629. }
  630. module_init(init_spu_base);
  631. MODULE_LICENSE("GPL");
  632. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");