spu_base.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  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. if (spu->stop_callback)
  121. spu->stop_callback(spu);
  122. return 0;
  123. }
  124. static int __spu_trap_mailbox(struct spu *spu)
  125. {
  126. if (spu->ibox_callback)
  127. spu->ibox_callback(spu);
  128. /* atomically disable SPU mailbox interrupts */
  129. spin_lock(&spu->register_lock);
  130. spu_int_mask_and(spu, 2, ~0x1);
  131. spin_unlock(&spu->register_lock);
  132. return 0;
  133. }
  134. static int __spu_trap_stop(struct spu *spu)
  135. {
  136. pr_debug("%s\n", __FUNCTION__);
  137. spu->stop_code = in_be32(&spu->problem->spu_status_R);
  138. if (spu->stop_callback)
  139. spu->stop_callback(spu);
  140. return 0;
  141. }
  142. static int __spu_trap_halt(struct spu *spu)
  143. {
  144. pr_debug("%s\n", __FUNCTION__);
  145. spu->stop_code = in_be32(&spu->problem->spu_status_R);
  146. if (spu->stop_callback)
  147. spu->stop_callback(spu);
  148. return 0;
  149. }
  150. static int __spu_trap_tag_group(struct spu *spu)
  151. {
  152. pr_debug("%s\n", __FUNCTION__);
  153. spu->mfc_callback(spu);
  154. return 0;
  155. }
  156. static int __spu_trap_spubox(struct spu *spu)
  157. {
  158. if (spu->wbox_callback)
  159. spu->wbox_callback(spu);
  160. /* atomically disable SPU mailbox interrupts */
  161. spin_lock(&spu->register_lock);
  162. spu_int_mask_and(spu, 2, ~0x10);
  163. spin_unlock(&spu->register_lock);
  164. return 0;
  165. }
  166. static irqreturn_t
  167. spu_irq_class_0(int irq, void *data, struct pt_regs *regs)
  168. {
  169. struct spu *spu;
  170. spu = data;
  171. spu->class_0_pending = 1;
  172. if (spu->stop_callback)
  173. spu->stop_callback(spu);
  174. return IRQ_HANDLED;
  175. }
  176. int
  177. spu_irq_class_0_bottom(struct spu *spu)
  178. {
  179. unsigned long stat, mask;
  180. spu->class_0_pending = 0;
  181. mask = spu_int_mask_get(spu, 0);
  182. stat = spu_int_stat_get(spu, 0);
  183. stat &= mask;
  184. if (stat & 1) /* invalid MFC DMA */
  185. __spu_trap_invalid_dma(spu);
  186. if (stat & 2) /* invalid DMA alignment */
  187. __spu_trap_dma_align(spu);
  188. if (stat & 4) /* error on SPU */
  189. __spu_trap_error(spu);
  190. spu_int_stat_clear(spu, 0, stat);
  191. return (stat & 0x7) ? -EIO : 0;
  192. }
  193. EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
  194. static irqreturn_t
  195. spu_irq_class_1(int irq, void *data, struct pt_regs *regs)
  196. {
  197. struct spu *spu;
  198. unsigned long stat, mask, dar, dsisr;
  199. spu = data;
  200. /* atomically read & clear class1 status. */
  201. spin_lock(&spu->register_lock);
  202. mask = spu_int_mask_get(spu, 1);
  203. stat = spu_int_stat_get(spu, 1) & mask;
  204. dar = spu_mfc_dar_get(spu);
  205. dsisr = spu_mfc_dsisr_get(spu);
  206. if (stat & 2) /* mapping fault */
  207. spu_mfc_dsisr_set(spu, 0ul);
  208. spu_int_stat_clear(spu, 1, stat);
  209. spin_unlock(&spu->register_lock);
  210. pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
  211. dar, dsisr);
  212. if (stat & 1) /* segment fault */
  213. __spu_trap_data_seg(spu, dar);
  214. if (stat & 2) { /* mapping fault */
  215. __spu_trap_data_map(spu, dar, dsisr);
  216. }
  217. if (stat & 4) /* ls compare & suspend on get */
  218. ;
  219. if (stat & 8) /* ls compare & suspend on put */
  220. ;
  221. return stat ? IRQ_HANDLED : IRQ_NONE;
  222. }
  223. EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
  224. static irqreturn_t
  225. spu_irq_class_2(int irq, void *data, struct pt_regs *regs)
  226. {
  227. struct spu *spu;
  228. unsigned long stat;
  229. unsigned long mask;
  230. spu = data;
  231. stat = spu_int_stat_get(spu, 2);
  232. mask = spu_int_mask_get(spu, 2);
  233. pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
  234. stat &= mask;
  235. if (stat & 1) /* PPC core mailbox */
  236. __spu_trap_mailbox(spu);
  237. if (stat & 2) /* SPU stop-and-signal */
  238. __spu_trap_stop(spu);
  239. if (stat & 4) /* SPU halted */
  240. __spu_trap_halt(spu);
  241. if (stat & 8) /* DMA tag group complete */
  242. __spu_trap_tag_group(spu);
  243. if (stat & 0x10) /* SPU mailbox threshold */
  244. __spu_trap_spubox(spu);
  245. spu_int_stat_clear(spu, 2, stat);
  246. return stat ? IRQ_HANDLED : IRQ_NONE;
  247. }
  248. static int
  249. spu_request_irqs(struct spu *spu)
  250. {
  251. int ret;
  252. int irq_base;
  253. irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
  254. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0", spu->number);
  255. ret = request_irq(irq_base + spu->isrc,
  256. spu_irq_class_0, SA_INTERRUPT, spu->irq_c0, spu);
  257. if (ret)
  258. goto out;
  259. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1", spu->number);
  260. ret = request_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc,
  261. spu_irq_class_1, SA_INTERRUPT, spu->irq_c1, spu);
  262. if (ret)
  263. goto out1;
  264. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2", spu->number);
  265. ret = request_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc,
  266. spu_irq_class_2, SA_INTERRUPT, spu->irq_c2, spu);
  267. if (ret)
  268. goto out2;
  269. goto out;
  270. out2:
  271. free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
  272. out1:
  273. free_irq(irq_base + spu->isrc, spu);
  274. out:
  275. return ret;
  276. }
  277. static void
  278. spu_free_irqs(struct spu *spu)
  279. {
  280. int irq_base;
  281. irq_base = IIC_NODE_STRIDE * spu->node + IIC_SPE_OFFSET;
  282. free_irq(irq_base + spu->isrc, spu);
  283. free_irq(irq_base + IIC_CLASS_STRIDE + spu->isrc, spu);
  284. free_irq(irq_base + 2*IIC_CLASS_STRIDE + spu->isrc, spu);
  285. }
  286. static LIST_HEAD(spu_list);
  287. static DEFINE_MUTEX(spu_mutex);
  288. static void spu_init_channels(struct spu *spu)
  289. {
  290. static const struct {
  291. unsigned channel;
  292. unsigned count;
  293. } zero_list[] = {
  294. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  295. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  296. }, count_list[] = {
  297. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  298. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  299. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  300. };
  301. struct spu_priv2 __iomem *priv2;
  302. int i;
  303. priv2 = spu->priv2;
  304. /* initialize all channel data to zero */
  305. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  306. int count;
  307. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  308. for (count = 0; count < zero_list[i].count; count++)
  309. out_be64(&priv2->spu_chnldata_RW, 0);
  310. }
  311. /* initialize channel counts to meaningful values */
  312. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  313. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  314. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  315. }
  316. }
  317. struct spu *spu_alloc(void)
  318. {
  319. struct spu *spu;
  320. mutex_lock(&spu_mutex);
  321. if (!list_empty(&spu_list)) {
  322. spu = list_entry(spu_list.next, struct spu, list);
  323. list_del_init(&spu->list);
  324. pr_debug("Got SPU %x %d\n", spu->isrc, spu->number);
  325. } else {
  326. pr_debug("No SPU left\n");
  327. spu = NULL;
  328. }
  329. mutex_unlock(&spu_mutex);
  330. if (spu)
  331. spu_init_channels(spu);
  332. return spu;
  333. }
  334. EXPORT_SYMBOL_GPL(spu_alloc);
  335. void spu_free(struct spu *spu)
  336. {
  337. mutex_lock(&spu_mutex);
  338. list_add_tail(&spu->list, &spu_list);
  339. mutex_unlock(&spu_mutex);
  340. }
  341. EXPORT_SYMBOL_GPL(spu_free);
  342. static int spu_handle_mm_fault(struct spu *spu)
  343. {
  344. struct mm_struct *mm = spu->mm;
  345. struct vm_area_struct *vma;
  346. u64 ea, dsisr, is_write;
  347. int ret;
  348. ea = spu->dar;
  349. dsisr = spu->dsisr;
  350. #if 0
  351. if (!IS_VALID_EA(ea)) {
  352. return -EFAULT;
  353. }
  354. #endif /* XXX */
  355. if (mm == NULL) {
  356. return -EFAULT;
  357. }
  358. if (mm->pgd == NULL) {
  359. return -EFAULT;
  360. }
  361. down_read(&mm->mmap_sem);
  362. vma = find_vma(mm, ea);
  363. if (!vma)
  364. goto bad_area;
  365. if (vma->vm_start <= ea)
  366. goto good_area;
  367. if (!(vma->vm_flags & VM_GROWSDOWN))
  368. goto bad_area;
  369. #if 0
  370. if (expand_stack(vma, ea))
  371. goto bad_area;
  372. #endif /* XXX */
  373. good_area:
  374. is_write = dsisr & MFC_DSISR_ACCESS_PUT;
  375. if (is_write) {
  376. if (!(vma->vm_flags & VM_WRITE))
  377. goto bad_area;
  378. } else {
  379. if (dsisr & MFC_DSISR_ACCESS_DENIED)
  380. goto bad_area;
  381. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  382. goto bad_area;
  383. }
  384. ret = 0;
  385. switch (handle_mm_fault(mm, vma, ea, is_write)) {
  386. case VM_FAULT_MINOR:
  387. current->min_flt++;
  388. break;
  389. case VM_FAULT_MAJOR:
  390. current->maj_flt++;
  391. break;
  392. case VM_FAULT_SIGBUS:
  393. ret = -EFAULT;
  394. goto bad_area;
  395. case VM_FAULT_OOM:
  396. ret = -ENOMEM;
  397. goto bad_area;
  398. default:
  399. BUG();
  400. }
  401. up_read(&mm->mmap_sem);
  402. return ret;
  403. bad_area:
  404. up_read(&mm->mmap_sem);
  405. return -EFAULT;
  406. }
  407. int spu_irq_class_1_bottom(struct spu *spu)
  408. {
  409. u64 ea, dsisr, access, error = 0UL;
  410. int ret = 0;
  411. ea = spu->dar;
  412. dsisr = spu->dsisr;
  413. if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
  414. u64 flags;
  415. access = (_PAGE_PRESENT | _PAGE_USER);
  416. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  417. local_irq_save(flags);
  418. if (hash_page(ea, access, 0x300) != 0)
  419. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  420. local_irq_restore(flags);
  421. }
  422. if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
  423. if ((ret = spu_handle_mm_fault(spu)) != 0)
  424. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  425. else
  426. error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
  427. }
  428. spu->dar = 0UL;
  429. spu->dsisr = 0UL;
  430. if (!error) {
  431. spu_restart_dma(spu);
  432. } else {
  433. __spu_trap_invalid_dma(spu);
  434. }
  435. return ret;
  436. }
  437. void spu_irq_setaffinity(struct spu *spu, int cpu)
  438. {
  439. u64 target = iic_get_target_id(cpu);
  440. u64 route = target << 48 | target << 32 | target << 16;
  441. spu_int_route_set(spu, route);
  442. }
  443. EXPORT_SYMBOL_GPL(spu_irq_setaffinity);
  444. static int __init find_spu_node_id(struct device_node *spe)
  445. {
  446. unsigned int *id;
  447. struct device_node *cpu;
  448. cpu = spe->parent->parent;
  449. id = (unsigned int *)get_property(cpu, "node-id", NULL);
  450. return id ? *id : 0;
  451. }
  452. static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
  453. const char *prop)
  454. {
  455. static DEFINE_MUTEX(add_spumem_mutex);
  456. struct address_prop {
  457. unsigned long address;
  458. unsigned int len;
  459. } __attribute__((packed)) *p;
  460. int proplen;
  461. unsigned long start_pfn, nr_pages;
  462. struct pglist_data *pgdata;
  463. struct zone *zone;
  464. int ret;
  465. p = (void*)get_property(spe, prop, &proplen);
  466. WARN_ON(proplen != sizeof (*p));
  467. start_pfn = p->address >> PAGE_SHIFT;
  468. nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  469. pgdata = NODE_DATA(spu->nid);
  470. zone = pgdata->node_zones;
  471. /* XXX rethink locking here */
  472. mutex_lock(&add_spumem_mutex);
  473. ret = __add_pages(zone, start_pfn, nr_pages);
  474. mutex_unlock(&add_spumem_mutex);
  475. return ret;
  476. }
  477. static void __iomem * __init map_spe_prop(struct spu *spu,
  478. struct device_node *n, const char *name)
  479. {
  480. struct address_prop {
  481. unsigned long address;
  482. unsigned int len;
  483. } __attribute__((packed)) *prop;
  484. void *p;
  485. int proplen;
  486. void* ret = NULL;
  487. int err = 0;
  488. p = get_property(n, name, &proplen);
  489. if (proplen != sizeof (struct address_prop))
  490. return NULL;
  491. prop = p;
  492. err = cell_spuprop_present(spu, n, name);
  493. if (err && (err != -EEXIST))
  494. goto out;
  495. ret = ioremap(prop->address, prop->len);
  496. out:
  497. return ret;
  498. }
  499. static void spu_unmap(struct spu *spu)
  500. {
  501. iounmap(spu->priv2);
  502. iounmap(spu->priv1);
  503. iounmap(spu->problem);
  504. iounmap((u8 __iomem *)spu->local_store);
  505. }
  506. static int __init spu_map_device(struct spu *spu, struct device_node *node)
  507. {
  508. char *prop;
  509. int ret;
  510. ret = -ENODEV;
  511. prop = get_property(node, "isrc", NULL);
  512. if (!prop)
  513. goto out;
  514. spu->isrc = *(unsigned int *)prop;
  515. spu->name = get_property(node, "name", NULL);
  516. if (!spu->name)
  517. goto out;
  518. prop = get_property(node, "local-store", NULL);
  519. if (!prop)
  520. goto out;
  521. spu->local_store_phys = *(unsigned long *)prop;
  522. /* we use local store as ram, not io memory */
  523. spu->local_store = (void __force *)
  524. map_spe_prop(spu, node, "local-store");
  525. if (!spu->local_store)
  526. goto out;
  527. prop = get_property(node, "problem", NULL);
  528. if (!prop)
  529. goto out_unmap;
  530. spu->problem_phys = *(unsigned long *)prop;
  531. spu->problem= map_spe_prop(spu, node, "problem");
  532. if (!spu->problem)
  533. goto out_unmap;
  534. spu->priv1= map_spe_prop(spu, node, "priv1");
  535. /* priv1 is not available on a hypervisor */
  536. spu->priv2= map_spe_prop(spu, node, "priv2");
  537. if (!spu->priv2)
  538. goto out_unmap;
  539. ret = 0;
  540. goto out;
  541. out_unmap:
  542. spu_unmap(spu);
  543. out:
  544. return ret;
  545. }
  546. struct sysdev_class spu_sysdev_class = {
  547. set_kset_name("spu")
  548. };
  549. static ssize_t spu_show_isrc(struct sys_device *sysdev, char *buf)
  550. {
  551. struct spu *spu = container_of(sysdev, struct spu, sysdev);
  552. return sprintf(buf, "%d\n", spu->isrc);
  553. }
  554. static SYSDEV_ATTR(isrc, 0400, spu_show_isrc, NULL);
  555. extern int attach_sysdev_to_node(struct sys_device *dev, int nid);
  556. static int spu_create_sysdev(struct spu *spu)
  557. {
  558. int ret;
  559. spu->sysdev.id = spu->number;
  560. spu->sysdev.cls = &spu_sysdev_class;
  561. ret = sysdev_register(&spu->sysdev);
  562. if (ret) {
  563. printk(KERN_ERR "Can't register SPU %d with sysfs\n",
  564. spu->number);
  565. return ret;
  566. }
  567. sysdev_create_file(&spu->sysdev, &attr_isrc);
  568. sysfs_add_device_to_node(&spu->sysdev, spu->nid);
  569. return 0;
  570. }
  571. static void spu_destroy_sysdev(struct spu *spu)
  572. {
  573. sysdev_remove_file(&spu->sysdev, &attr_isrc);
  574. sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
  575. sysdev_unregister(&spu->sysdev);
  576. }
  577. static int __init create_spu(struct device_node *spe)
  578. {
  579. struct spu *spu;
  580. int ret;
  581. static int number;
  582. ret = -ENOMEM;
  583. spu = kzalloc(sizeof (*spu), GFP_KERNEL);
  584. if (!spu)
  585. goto out;
  586. ret = spu_map_device(spu, spe);
  587. if (ret)
  588. goto out_free;
  589. spu->node = find_spu_node_id(spe);
  590. spu->nid = of_node_to_nid(spe);
  591. if (spu->nid == -1)
  592. spu->nid = 0;
  593. spin_lock_init(&spu->register_lock);
  594. spu_mfc_sdr_set(spu, mfspr(SPRN_SDR1));
  595. spu_mfc_sr1_set(spu, 0x33);
  596. mutex_lock(&spu_mutex);
  597. spu->number = number++;
  598. ret = spu_request_irqs(spu);
  599. if (ret)
  600. goto out_unmap;
  601. ret = spu_create_sysdev(spu);
  602. if (ret)
  603. goto out_free_irqs;
  604. list_add(&spu->list, &spu_list);
  605. mutex_unlock(&spu_mutex);
  606. pr_debug(KERN_DEBUG "Using SPE %s %02x %p %p %p %p %d\n",
  607. spu->name, spu->isrc, spu->local_store,
  608. spu->problem, spu->priv1, spu->priv2, spu->number);
  609. goto out;
  610. out_free_irqs:
  611. spu_free_irqs(spu);
  612. out_unmap:
  613. mutex_unlock(&spu_mutex);
  614. spu_unmap(spu);
  615. out_free:
  616. kfree(spu);
  617. out:
  618. return ret;
  619. }
  620. static void destroy_spu(struct spu *spu)
  621. {
  622. list_del_init(&spu->list);
  623. spu_destroy_sysdev(spu);
  624. spu_free_irqs(spu);
  625. spu_unmap(spu);
  626. kfree(spu);
  627. }
  628. static void cleanup_spu_base(void)
  629. {
  630. struct spu *spu, *tmp;
  631. mutex_lock(&spu_mutex);
  632. list_for_each_entry_safe(spu, tmp, &spu_list, list)
  633. destroy_spu(spu);
  634. mutex_unlock(&spu_mutex);
  635. sysdev_class_unregister(&spu_sysdev_class);
  636. }
  637. module_exit(cleanup_spu_base);
  638. static int __init init_spu_base(void)
  639. {
  640. struct device_node *node;
  641. int ret;
  642. /* create sysdev class for spus */
  643. ret = sysdev_class_register(&spu_sysdev_class);
  644. if (ret)
  645. return ret;
  646. ret = -ENODEV;
  647. for (node = of_find_node_by_type(NULL, "spe");
  648. node; node = of_find_node_by_type(node, "spe")) {
  649. ret = create_spu(node);
  650. if (ret) {
  651. printk(KERN_WARNING "%s: Error initializing %s\n",
  652. __FUNCTION__, node->name);
  653. cleanup_spu_base();
  654. break;
  655. }
  656. }
  657. /* in some old firmware versions, the spe is called 'spc', so we
  658. look for that as well */
  659. for (node = of_find_node_by_type(NULL, "spc");
  660. node; node = of_find_node_by_type(node, "spc")) {
  661. ret = create_spu(node);
  662. if (ret) {
  663. printk(KERN_WARNING "%s: Error initializing %s\n",
  664. __FUNCTION__, node->name);
  665. cleanup_spu_base();
  666. break;
  667. }
  668. }
  669. return ret;
  670. }
  671. module_init(init_spu_base);
  672. MODULE_LICENSE("GPL");
  673. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");