spu_base.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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 DMA alignment */
  141. __spu_trap_dma_align(spu);
  142. if (stat & 2) /* invalid MFC DMA */
  143. __spu_trap_invalid_dma(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 spu_request_irqs(struct spu *spu)
  215. {
  216. int ret = 0;
  217. if (spu->irqs[0] != NO_IRQ) {
  218. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
  219. spu->number);
  220. ret = request_irq(spu->irqs[0], spu_irq_class_0,
  221. IRQF_DISABLED,
  222. spu->irq_c0, spu);
  223. if (ret)
  224. goto bail0;
  225. }
  226. if (spu->irqs[1] != NO_IRQ) {
  227. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
  228. spu->number);
  229. ret = request_irq(spu->irqs[1], spu_irq_class_1,
  230. IRQF_DISABLED,
  231. spu->irq_c1, spu);
  232. if (ret)
  233. goto bail1;
  234. }
  235. if (spu->irqs[2] != NO_IRQ) {
  236. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
  237. spu->number);
  238. ret = request_irq(spu->irqs[2], spu_irq_class_2,
  239. IRQF_DISABLED,
  240. spu->irq_c2, spu);
  241. if (ret)
  242. goto bail2;
  243. }
  244. return 0;
  245. bail2:
  246. if (spu->irqs[1] != NO_IRQ)
  247. free_irq(spu->irqs[1], spu);
  248. bail1:
  249. if (spu->irqs[0] != NO_IRQ)
  250. free_irq(spu->irqs[0], spu);
  251. bail0:
  252. return ret;
  253. }
  254. static void spu_free_irqs(struct spu *spu)
  255. {
  256. if (spu->irqs[0] != NO_IRQ)
  257. free_irq(spu->irqs[0], spu);
  258. if (spu->irqs[1] != NO_IRQ)
  259. free_irq(spu->irqs[1], spu);
  260. if (spu->irqs[2] != NO_IRQ)
  261. free_irq(spu->irqs[2], spu);
  262. }
  263. static LIST_HEAD(spu_list);
  264. static DEFINE_MUTEX(spu_mutex);
  265. static void spu_init_channels(struct spu *spu)
  266. {
  267. static const struct {
  268. unsigned channel;
  269. unsigned count;
  270. } zero_list[] = {
  271. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  272. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  273. }, count_list[] = {
  274. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  275. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  276. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  277. };
  278. struct spu_priv2 __iomem *priv2;
  279. int i;
  280. priv2 = spu->priv2;
  281. /* initialize all channel data to zero */
  282. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  283. int count;
  284. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  285. for (count = 0; count < zero_list[i].count; count++)
  286. out_be64(&priv2->spu_chnldata_RW, 0);
  287. }
  288. /* initialize channel counts to meaningful values */
  289. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  290. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  291. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  292. }
  293. }
  294. struct spu *spu_alloc(void)
  295. {
  296. struct spu *spu;
  297. mutex_lock(&spu_mutex);
  298. if (!list_empty(&spu_list)) {
  299. spu = list_entry(spu_list.next, struct spu, list);
  300. list_del_init(&spu->list);
  301. pr_debug("Got SPU %x %d\n", spu->isrc, spu->number);
  302. } else {
  303. pr_debug("No SPU left\n");
  304. spu = NULL;
  305. }
  306. mutex_unlock(&spu_mutex);
  307. if (spu)
  308. spu_init_channels(spu);
  309. return spu;
  310. }
  311. EXPORT_SYMBOL_GPL(spu_alloc);
  312. void spu_free(struct spu *spu)
  313. {
  314. mutex_lock(&spu_mutex);
  315. list_add_tail(&spu->list, &spu_list);
  316. mutex_unlock(&spu_mutex);
  317. }
  318. EXPORT_SYMBOL_GPL(spu_free);
  319. static int spu_handle_mm_fault(struct spu *spu)
  320. {
  321. struct mm_struct *mm = spu->mm;
  322. struct vm_area_struct *vma;
  323. u64 ea, dsisr, is_write;
  324. int ret;
  325. ea = spu->dar;
  326. dsisr = spu->dsisr;
  327. #if 0
  328. if (!IS_VALID_EA(ea)) {
  329. return -EFAULT;
  330. }
  331. #endif /* XXX */
  332. if (mm == NULL) {
  333. return -EFAULT;
  334. }
  335. if (mm->pgd == NULL) {
  336. return -EFAULT;
  337. }
  338. down_read(&mm->mmap_sem);
  339. vma = find_vma(mm, ea);
  340. if (!vma)
  341. goto bad_area;
  342. if (vma->vm_start <= ea)
  343. goto good_area;
  344. if (!(vma->vm_flags & VM_GROWSDOWN))
  345. goto bad_area;
  346. #if 0
  347. if (expand_stack(vma, ea))
  348. goto bad_area;
  349. #endif /* XXX */
  350. good_area:
  351. is_write = dsisr & MFC_DSISR_ACCESS_PUT;
  352. if (is_write) {
  353. if (!(vma->vm_flags & VM_WRITE))
  354. goto bad_area;
  355. } else {
  356. if (dsisr & MFC_DSISR_ACCESS_DENIED)
  357. goto bad_area;
  358. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  359. goto bad_area;
  360. }
  361. ret = 0;
  362. switch (handle_mm_fault(mm, vma, ea, is_write)) {
  363. case VM_FAULT_MINOR:
  364. current->min_flt++;
  365. break;
  366. case VM_FAULT_MAJOR:
  367. current->maj_flt++;
  368. break;
  369. case VM_FAULT_SIGBUS:
  370. ret = -EFAULT;
  371. goto bad_area;
  372. case VM_FAULT_OOM:
  373. ret = -ENOMEM;
  374. goto bad_area;
  375. default:
  376. BUG();
  377. }
  378. up_read(&mm->mmap_sem);
  379. return ret;
  380. bad_area:
  381. up_read(&mm->mmap_sem);
  382. return -EFAULT;
  383. }
  384. int spu_irq_class_1_bottom(struct spu *spu)
  385. {
  386. u64 ea, dsisr, access, error = 0UL;
  387. int ret = 0;
  388. ea = spu->dar;
  389. dsisr = spu->dsisr;
  390. if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
  391. u64 flags;
  392. access = (_PAGE_PRESENT | _PAGE_USER);
  393. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  394. local_irq_save(flags);
  395. if (hash_page(ea, access, 0x300) != 0)
  396. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  397. local_irq_restore(flags);
  398. }
  399. if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
  400. if ((ret = spu_handle_mm_fault(spu)) != 0)
  401. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  402. else
  403. error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
  404. }
  405. spu->dar = 0UL;
  406. spu->dsisr = 0UL;
  407. if (!error) {
  408. spu_restart_dma(spu);
  409. } else {
  410. __spu_trap_invalid_dma(spu);
  411. }
  412. return ret;
  413. }
  414. static int __init find_spu_node_id(struct device_node *spe)
  415. {
  416. const unsigned int *id;
  417. struct device_node *cpu;
  418. cpu = spe->parent->parent;
  419. id = get_property(cpu, "node-id", NULL);
  420. return id ? *id : 0;
  421. }
  422. static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
  423. const char *prop)
  424. {
  425. static DEFINE_MUTEX(add_spumem_mutex);
  426. const struct address_prop {
  427. unsigned long address;
  428. unsigned int len;
  429. } __attribute__((packed)) *p;
  430. int proplen;
  431. unsigned long start_pfn, nr_pages;
  432. struct pglist_data *pgdata;
  433. struct zone *zone;
  434. int ret;
  435. p = get_property(spe, prop, &proplen);
  436. WARN_ON(proplen != sizeof (*p));
  437. start_pfn = p->address >> PAGE_SHIFT;
  438. nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  439. pgdata = NODE_DATA(spu->nid);
  440. zone = pgdata->node_zones;
  441. /* XXX rethink locking here */
  442. mutex_lock(&add_spumem_mutex);
  443. ret = __add_pages(zone, start_pfn, nr_pages);
  444. mutex_unlock(&add_spumem_mutex);
  445. return ret;
  446. }
  447. static void __iomem * __init map_spe_prop(struct spu *spu,
  448. struct device_node *n, const char *name)
  449. {
  450. const struct address_prop {
  451. unsigned long address;
  452. unsigned int len;
  453. } __attribute__((packed)) *prop;
  454. const void *p;
  455. int proplen;
  456. void* ret = NULL;
  457. int err = 0;
  458. p = get_property(n, name, &proplen);
  459. if (proplen != sizeof (struct address_prop))
  460. return NULL;
  461. prop = p;
  462. err = cell_spuprop_present(spu, n, name);
  463. if (err && (err != -EEXIST))
  464. goto out;
  465. ret = ioremap(prop->address, prop->len);
  466. out:
  467. return ret;
  468. }
  469. static void spu_unmap(struct spu *spu)
  470. {
  471. iounmap(spu->priv2);
  472. iounmap(spu->priv1);
  473. iounmap(spu->problem);
  474. iounmap((u8 __iomem *)spu->local_store);
  475. }
  476. /* This function shall be abstracted for HV platforms */
  477. static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
  478. {
  479. struct irq_host *host;
  480. unsigned int isrc;
  481. const u32 *tmp;
  482. host = iic_get_irq_host(spu->node);
  483. if (host == NULL)
  484. return -ENODEV;
  485. /* Get the interrupt source from the device-tree */
  486. tmp = get_property(np, "isrc", NULL);
  487. if (!tmp)
  488. return -ENODEV;
  489. spu->isrc = isrc = tmp[0];
  490. /* Now map interrupts of all 3 classes */
  491. spu->irqs[0] = irq_create_mapping(host, 0x00 | isrc);
  492. spu->irqs[1] = irq_create_mapping(host, 0x10 | isrc);
  493. spu->irqs[2] = irq_create_mapping(host, 0x20 | isrc);
  494. /* Right now, we only fail if class 2 failed */
  495. return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
  496. }
  497. static int __init spu_map_device(struct spu *spu, struct device_node *node)
  498. {
  499. const char *prop;
  500. int ret;
  501. ret = -ENODEV;
  502. spu->name = get_property(node, "name", NULL);
  503. if (!spu->name)
  504. goto out;
  505. prop = get_property(node, "local-store", NULL);
  506. if (!prop)
  507. goto out;
  508. spu->local_store_phys = *(unsigned long *)prop;
  509. /* we use local store as ram, not io memory */
  510. spu->local_store = (void __force *)
  511. map_spe_prop(spu, node, "local-store");
  512. if (!spu->local_store)
  513. goto out;
  514. prop = get_property(node, "problem", NULL);
  515. if (!prop)
  516. goto out_unmap;
  517. spu->problem_phys = *(unsigned long *)prop;
  518. spu->problem= map_spe_prop(spu, node, "problem");
  519. if (!spu->problem)
  520. goto out_unmap;
  521. spu->priv1= map_spe_prop(spu, node, "priv1");
  522. /* priv1 is not available on a hypervisor */
  523. spu->priv2= map_spe_prop(spu, node, "priv2");
  524. if (!spu->priv2)
  525. goto out_unmap;
  526. ret = 0;
  527. goto out;
  528. out_unmap:
  529. spu_unmap(spu);
  530. out:
  531. return ret;
  532. }
  533. struct sysdev_class spu_sysdev_class = {
  534. set_kset_name("spu")
  535. };
  536. static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
  537. {
  538. struct spu *spu = container_of(sysdev, struct spu, sysdev);
  539. return sprintf(buf, "%d\n", spu->isrc);
  540. }
  541. static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
  542. extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
  543. static int spu_create_sysdev(struct spu *spu)
  544. {
  545. int ret;
  546. spu->sysdev.id = spu->number;
  547. spu->sysdev.cls = &spu_sysdev_class;
  548. ret = sysdev_register(&spu->sysdev);
  549. if (ret) {
  550. printk(KERN_ERR "Can't register SPU %d with sysfs\n",
  551. spu->number);
  552. return ret;
  553. }
  554. if (spu->isrc != 0)
  555. sysdev_create_file(&spu->sysdev, &attr_isrc);
  556. sysfs_add_device_to_node(&spu->sysdev, spu->nid);
  557. return 0;
  558. }
  559. static void spu_destroy_sysdev(struct spu *spu)
  560. {
  561. sysdev_remove_file(&spu->sysdev, &attr_isrc);
  562. sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
  563. sysdev_unregister(&spu->sysdev);
  564. }
  565. static int __init create_spu(struct device_node *spe)
  566. {
  567. struct spu *spu;
  568. int ret;
  569. static int number;
  570. ret = -ENOMEM;
  571. spu = kzalloc(sizeof (*spu), GFP_KERNEL);
  572. if (!spu)
  573. goto out;
  574. ret = spu_map_device(spu, spe);
  575. if (ret)
  576. goto out_free;
  577. spu->node = find_spu_node_id(spe);
  578. spu->nid = of_node_to_nid(spe);
  579. if (spu->nid == -1)
  580. spu->nid = 0;
  581. ret = spu_map_interrupts(spu, spe);
  582. if (ret)
  583. goto out_unmap;
  584. spin_lock_init(&spu->register_lock);
  585. spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1));
  586. spu_mfc_sr1_set(spu, 0x33);
  587. mutex_lock(&spu_mutex);
  588. spu->number = number++;
  589. ret = spu_request_irqs(spu);
  590. if (ret)
  591. goto out_unmap;
  592. ret = spu_create_sysdev(spu);
  593. if (ret)
  594. goto out_free_irqs;
  595. list_add(&spu->list, &spu_list);
  596. mutex_unlock(&spu_mutex);
  597. pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
  598. spu->name, spu->isrc, spu->local_store,
  599. spu->problem, spu->priv1, spu->priv2, spu->number);
  600. goto out;
  601. out_free_irqs:
  602. spu_free_irqs(spu);
  603. out_unmap:
  604. mutex_unlock(&spu_mutex);
  605. spu_unmap(spu);
  606. out_free:
  607. kfree(spu);
  608. out:
  609. return ret;
  610. }
  611. static void destroy_spu(struct spu *spu)
  612. {
  613. list_del_init(&spu->list);
  614. spu_destroy_sysdev(spu);
  615. spu_free_irqs(spu);
  616. spu_unmap(spu);
  617. kfree(spu);
  618. }
  619. static void cleanup_spu_base(void)
  620. {
  621. struct spu *spu, *tmp;
  622. mutex_lock(&spu_mutex);
  623. list_for_each_entry_safe(spu, tmp, &spu_list, list)
  624. destroy_spu(spu);
  625. mutex_unlock(&spu_mutex);
  626. sysdev_class_unregister(&spu_sysdev_class);
  627. }
  628. module_exit(cleanup_spu_base);
  629. static int __init init_spu_base(void)
  630. {
  631. struct device_node *node;
  632. int ret;
  633. /* create sysdev class for spus */
  634. ret = sysdev_class_register(&spu_sysdev_class);
  635. if (ret)
  636. return ret;
  637. ret = -ENODEV;
  638. for (node = of_find_node_by_type(NULL, "spe");
  639. node; node = of_find_node_by_type(node, "spe")) {
  640. ret = create_spu(node);
  641. if (ret) {
  642. printk(KERN_WARNING "%s: Error initializing %s\n",
  643. __FUNCTION__, node->name);
  644. cleanup_spu_base();
  645. break;
  646. }
  647. }
  648. /* in some old firmware versions, the spe is called 'spc', so we
  649. look for that as well */
  650. for (node = of_find_node_by_type(NULL, "spc");
  651. node; node = of_find_node_by_type(node, "spc")) {
  652. ret = create_spu(node);
  653. if (ret) {
  654. printk(KERN_WARNING "%s: Error initializing %s\n",
  655. __FUNCTION__, node->name);
  656. cleanup_spu_base();
  657. break;
  658. }
  659. }
  660. return ret;
  661. }
  662. module_init(init_spu_base);
  663. MODULE_LICENSE("GPL");
  664. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");