spu_base.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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/pci.h>
  27. #include <linux/poll.h>
  28. #include <linux/ptrace.h>
  29. #include <linux/slab.h>
  30. #include <linux/wait.h>
  31. #include <asm/firmware.h>
  32. #include <asm/io.h>
  33. #include <asm/prom.h>
  34. #include <linux/mutex.h>
  35. #include <asm/spu.h>
  36. #include <asm/spu_priv1.h>
  37. #include <asm/mmu_context.h>
  38. #include "interrupt.h"
  39. const struct spu_priv1_ops *spu_priv1_ops;
  40. EXPORT_SYMBOL_GPL(spu_priv1_ops);
  41. static int __spu_trap_invalid_dma(struct spu *spu)
  42. {
  43. pr_debug("%s\n", __FUNCTION__);
  44. spu->dma_callback(spu, SPE_EVENT_INVALID_DMA);
  45. return 0;
  46. }
  47. static int __spu_trap_dma_align(struct spu *spu)
  48. {
  49. pr_debug("%s\n", __FUNCTION__);
  50. spu->dma_callback(spu, SPE_EVENT_DMA_ALIGNMENT);
  51. return 0;
  52. }
  53. static int __spu_trap_error(struct spu *spu)
  54. {
  55. pr_debug("%s\n", __FUNCTION__);
  56. spu->dma_callback(spu, SPE_EVENT_SPE_ERROR);
  57. return 0;
  58. }
  59. static void spu_restart_dma(struct spu *spu)
  60. {
  61. struct spu_priv2 __iomem *priv2 = spu->priv2;
  62. if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
  63. out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
  64. }
  65. static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
  66. {
  67. struct spu_priv2 __iomem *priv2 = spu->priv2;
  68. struct mm_struct *mm = spu->mm;
  69. u64 esid, vsid, llp;
  70. pr_debug("%s\n", __FUNCTION__);
  71. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
  72. /* SLBs are pre-loaded for context switch, so
  73. * we should never get here!
  74. */
  75. printk("%s: invalid access during switch!\n", __func__);
  76. return 1;
  77. }
  78. if (!mm || (REGION_ID(ea) != USER_REGION_ID)) {
  79. /* Future: support kernel segments so that drivers
  80. * can use SPUs.
  81. */
  82. pr_debug("invalid region access at %016lx\n", ea);
  83. return 1;
  84. }
  85. esid = (ea & ESID_MASK) | SLB_ESID_V;
  86. #ifdef CONFIG_HUGETLB_PAGE
  87. if (in_hugepage_area(mm->context, ea))
  88. llp = mmu_psize_defs[mmu_huge_psize].sllp;
  89. else
  90. #endif
  91. llp = mmu_psize_defs[mmu_virtual_psize].sllp;
  92. vsid = (get_vsid(mm->context.id, ea) << SLB_VSID_SHIFT) |
  93. SLB_VSID_USER | llp;
  94. out_be64(&priv2->slb_index_W, spu->slb_replace);
  95. out_be64(&priv2->slb_vsid_RW, vsid);
  96. out_be64(&priv2->slb_esid_RW, esid);
  97. spu->slb_replace++;
  98. if (spu->slb_replace >= 8)
  99. spu->slb_replace = 0;
  100. spu_restart_dma(spu);
  101. return 0;
  102. }
  103. extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
  104. static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
  105. {
  106. pr_debug("%s, %lx, %lx\n", __FUNCTION__, dsisr, ea);
  107. /* Handle kernel space hash faults immediately.
  108. User hash faults need to be deferred to process context. */
  109. if ((dsisr & MFC_DSISR_PTE_NOT_FOUND)
  110. && REGION_ID(ea) != USER_REGION_ID
  111. && hash_page(ea, _PAGE_PRESENT, 0x300) == 0) {
  112. spu_restart_dma(spu);
  113. return 0;
  114. }
  115. if (test_bit(SPU_CONTEXT_SWITCH_ACTIVE, &spu->flags)) {
  116. printk("%s: invalid access during switch!\n", __func__);
  117. return 1;
  118. }
  119. spu->dar = ea;
  120. spu->dsisr = dsisr;
  121. mb();
  122. spu->stop_callback(spu);
  123. return 0;
  124. }
  125. static irqreturn_t
  126. spu_irq_class_0(int irq, void *data)
  127. {
  128. struct spu *spu;
  129. spu = data;
  130. spu->class_0_pending = 1;
  131. spu->stop_callback(spu);
  132. return IRQ_HANDLED;
  133. }
  134. int
  135. spu_irq_class_0_bottom(struct spu *spu)
  136. {
  137. unsigned long stat, mask;
  138. spu->class_0_pending = 0;
  139. mask = spu_int_mask_get(spu, 0);
  140. stat = spu_int_stat_get(spu, 0);
  141. stat &= mask;
  142. if (stat & 1) /* invalid DMA alignment */
  143. __spu_trap_dma_align(spu);
  144. if (stat & 2) /* invalid MFC DMA */
  145. __spu_trap_invalid_dma(spu);
  146. if (stat & 4) /* error on SPU */
  147. __spu_trap_error(spu);
  148. spu_int_stat_clear(spu, 0, stat);
  149. return (stat & 0x7) ? -EIO : 0;
  150. }
  151. EXPORT_SYMBOL_GPL(spu_irq_class_0_bottom);
  152. static irqreturn_t
  153. spu_irq_class_1(int irq, void *data)
  154. {
  155. struct spu *spu;
  156. unsigned long stat, mask, dar, dsisr;
  157. spu = data;
  158. /* atomically read & clear class1 status. */
  159. spin_lock(&spu->register_lock);
  160. mask = spu_int_mask_get(spu, 1);
  161. stat = spu_int_stat_get(spu, 1) & mask;
  162. dar = spu_mfc_dar_get(spu);
  163. dsisr = spu_mfc_dsisr_get(spu);
  164. if (stat & 2) /* mapping fault */
  165. spu_mfc_dsisr_set(spu, 0ul);
  166. spu_int_stat_clear(spu, 1, stat);
  167. spin_unlock(&spu->register_lock);
  168. pr_debug("%s: %lx %lx %lx %lx\n", __FUNCTION__, mask, stat,
  169. dar, dsisr);
  170. if (stat & 1) /* segment fault */
  171. __spu_trap_data_seg(spu, dar);
  172. if (stat & 2) { /* mapping fault */
  173. __spu_trap_data_map(spu, dar, dsisr);
  174. }
  175. if (stat & 4) /* ls compare & suspend on get */
  176. ;
  177. if (stat & 8) /* ls compare & suspend on put */
  178. ;
  179. return stat ? IRQ_HANDLED : IRQ_NONE;
  180. }
  181. EXPORT_SYMBOL_GPL(spu_irq_class_1_bottom);
  182. static irqreturn_t
  183. spu_irq_class_2(int irq, void *data)
  184. {
  185. struct spu *spu;
  186. unsigned long stat;
  187. unsigned long mask;
  188. spu = data;
  189. spin_lock(&spu->register_lock);
  190. stat = spu_int_stat_get(spu, 2);
  191. mask = spu_int_mask_get(spu, 2);
  192. /* ignore interrupts we're not waiting for */
  193. stat &= mask;
  194. /*
  195. * mailbox interrupts (0x1 and 0x10) are level triggered.
  196. * mask them now before acknowledging.
  197. */
  198. if (stat & 0x11)
  199. spu_int_mask_and(spu, 2, ~(stat & 0x11));
  200. /* acknowledge all interrupts before the callbacks */
  201. spu_int_stat_clear(spu, 2, stat);
  202. spin_unlock(&spu->register_lock);
  203. pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
  204. if (stat & 1) /* PPC core mailbox */
  205. spu->ibox_callback(spu);
  206. if (stat & 2) /* SPU stop-and-signal */
  207. spu->stop_callback(spu);
  208. if (stat & 4) /* SPU halted */
  209. spu->stop_callback(spu);
  210. if (stat & 8) /* DMA tag group complete */
  211. spu->mfc_callback(spu);
  212. if (stat & 0x10) /* SPU mailbox threshold */
  213. spu->wbox_callback(spu);
  214. return stat ? IRQ_HANDLED : IRQ_NONE;
  215. }
  216. static int spu_request_irqs(struct spu *spu)
  217. {
  218. int ret = 0;
  219. if (spu->irqs[0] != NO_IRQ) {
  220. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
  221. spu->number);
  222. ret = request_irq(spu->irqs[0], spu_irq_class_0,
  223. IRQF_DISABLED,
  224. spu->irq_c0, spu);
  225. if (ret)
  226. goto bail0;
  227. }
  228. if (spu->irqs[1] != NO_IRQ) {
  229. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
  230. spu->number);
  231. ret = request_irq(spu->irqs[1], spu_irq_class_1,
  232. IRQF_DISABLED,
  233. spu->irq_c1, spu);
  234. if (ret)
  235. goto bail1;
  236. }
  237. if (spu->irqs[2] != NO_IRQ) {
  238. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
  239. spu->number);
  240. ret = request_irq(spu->irqs[2], spu_irq_class_2,
  241. IRQF_DISABLED,
  242. spu->irq_c2, spu);
  243. if (ret)
  244. goto bail2;
  245. }
  246. return 0;
  247. bail2:
  248. if (spu->irqs[1] != NO_IRQ)
  249. free_irq(spu->irqs[1], spu);
  250. bail1:
  251. if (spu->irqs[0] != NO_IRQ)
  252. free_irq(spu->irqs[0], spu);
  253. bail0:
  254. return ret;
  255. }
  256. static void spu_free_irqs(struct spu *spu)
  257. {
  258. if (spu->irqs[0] != NO_IRQ)
  259. free_irq(spu->irqs[0], spu);
  260. if (spu->irqs[1] != NO_IRQ)
  261. free_irq(spu->irqs[1], spu);
  262. if (spu->irqs[2] != NO_IRQ)
  263. free_irq(spu->irqs[2], spu);
  264. }
  265. static struct list_head spu_list[MAX_NUMNODES];
  266. static DEFINE_MUTEX(spu_mutex);
  267. static void spu_init_channels(struct spu *spu)
  268. {
  269. static const struct {
  270. unsigned channel;
  271. unsigned count;
  272. } zero_list[] = {
  273. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  274. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  275. }, count_list[] = {
  276. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  277. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  278. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  279. };
  280. struct spu_priv2 __iomem *priv2;
  281. int i;
  282. priv2 = spu->priv2;
  283. /* initialize all channel data to zero */
  284. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  285. int count;
  286. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  287. for (count = 0; count < zero_list[i].count; count++)
  288. out_be64(&priv2->spu_chnldata_RW, 0);
  289. }
  290. /* initialize channel counts to meaningful values */
  291. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  292. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  293. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  294. }
  295. }
  296. struct spu *spu_alloc_node(int node)
  297. {
  298. struct spu *spu = NULL;
  299. mutex_lock(&spu_mutex);
  300. if (!list_empty(&spu_list[node])) {
  301. spu = list_entry(spu_list[node].next, struct spu, list);
  302. list_del_init(&spu->list);
  303. pr_debug("Got SPU %d %d\n", spu->number, spu->node);
  304. spu_init_channels(spu);
  305. }
  306. mutex_unlock(&spu_mutex);
  307. return spu;
  308. }
  309. EXPORT_SYMBOL_GPL(spu_alloc_node);
  310. struct spu *spu_alloc(void)
  311. {
  312. struct spu *spu = NULL;
  313. int node;
  314. for (node = 0; node < MAX_NUMNODES; node++) {
  315. spu = spu_alloc_node(node);
  316. if (spu)
  317. break;
  318. }
  319. return spu;
  320. }
  321. void spu_free(struct spu *spu)
  322. {
  323. mutex_lock(&spu_mutex);
  324. list_add_tail(&spu->list, &spu_list[spu->node]);
  325. mutex_unlock(&spu_mutex);
  326. }
  327. EXPORT_SYMBOL_GPL(spu_free);
  328. static int spu_handle_mm_fault(struct spu *spu)
  329. {
  330. struct mm_struct *mm = spu->mm;
  331. struct vm_area_struct *vma;
  332. u64 ea, dsisr, is_write;
  333. int ret;
  334. ea = spu->dar;
  335. dsisr = spu->dsisr;
  336. #if 0
  337. if (!IS_VALID_EA(ea)) {
  338. return -EFAULT;
  339. }
  340. #endif /* XXX */
  341. if (mm == NULL) {
  342. return -EFAULT;
  343. }
  344. if (mm->pgd == NULL) {
  345. return -EFAULT;
  346. }
  347. down_read(&mm->mmap_sem);
  348. vma = find_vma(mm, ea);
  349. if (!vma)
  350. goto bad_area;
  351. if (vma->vm_start <= ea)
  352. goto good_area;
  353. if (!(vma->vm_flags & VM_GROWSDOWN))
  354. goto bad_area;
  355. #if 0
  356. if (expand_stack(vma, ea))
  357. goto bad_area;
  358. #endif /* XXX */
  359. good_area:
  360. is_write = dsisr & MFC_DSISR_ACCESS_PUT;
  361. if (is_write) {
  362. if (!(vma->vm_flags & VM_WRITE))
  363. goto bad_area;
  364. } else {
  365. if (dsisr & MFC_DSISR_ACCESS_DENIED)
  366. goto bad_area;
  367. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  368. goto bad_area;
  369. }
  370. ret = 0;
  371. switch (handle_mm_fault(mm, vma, ea, is_write)) {
  372. case VM_FAULT_MINOR:
  373. current->min_flt++;
  374. break;
  375. case VM_FAULT_MAJOR:
  376. current->maj_flt++;
  377. break;
  378. case VM_FAULT_SIGBUS:
  379. ret = -EFAULT;
  380. goto bad_area;
  381. case VM_FAULT_OOM:
  382. ret = -ENOMEM;
  383. goto bad_area;
  384. default:
  385. BUG();
  386. }
  387. up_read(&mm->mmap_sem);
  388. return ret;
  389. bad_area:
  390. up_read(&mm->mmap_sem);
  391. return -EFAULT;
  392. }
  393. int spu_irq_class_1_bottom(struct spu *spu)
  394. {
  395. u64 ea, dsisr, access, error = 0UL;
  396. int ret = 0;
  397. ea = spu->dar;
  398. dsisr = spu->dsisr;
  399. if (dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)) {
  400. u64 flags;
  401. access = (_PAGE_PRESENT | _PAGE_USER);
  402. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  403. local_irq_save(flags);
  404. if (hash_page(ea, access, 0x300) != 0)
  405. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  406. local_irq_restore(flags);
  407. }
  408. if (error & CLASS1_ENABLE_STORAGE_FAULT_INTR) {
  409. if ((ret = spu_handle_mm_fault(spu)) != 0)
  410. error |= CLASS1_ENABLE_STORAGE_FAULT_INTR;
  411. else
  412. error &= ~CLASS1_ENABLE_STORAGE_FAULT_INTR;
  413. }
  414. spu->dar = 0UL;
  415. spu->dsisr = 0UL;
  416. if (!error) {
  417. spu_restart_dma(spu);
  418. } else {
  419. __spu_trap_invalid_dma(spu);
  420. }
  421. return ret;
  422. }
  423. static int __init find_spu_node_id(struct device_node *spe)
  424. {
  425. const unsigned int *id;
  426. struct device_node *cpu;
  427. cpu = spe->parent->parent;
  428. id = get_property(cpu, "node-id", NULL);
  429. return id ? *id : 0;
  430. }
  431. static int __init cell_spuprop_present(struct spu *spu, struct device_node *spe,
  432. const char *prop)
  433. {
  434. static DEFINE_MUTEX(add_spumem_mutex);
  435. const struct address_prop {
  436. unsigned long address;
  437. unsigned int len;
  438. } __attribute__((packed)) *p;
  439. int proplen;
  440. unsigned long start_pfn, nr_pages;
  441. struct pglist_data *pgdata;
  442. struct zone *zone;
  443. int ret;
  444. p = get_property(spe, prop, &proplen);
  445. WARN_ON(proplen != sizeof (*p));
  446. start_pfn = p->address >> PAGE_SHIFT;
  447. nr_pages = ((unsigned long)p->len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  448. pgdata = NODE_DATA(spu->nid);
  449. zone = pgdata->node_zones;
  450. /* XXX rethink locking here */
  451. mutex_lock(&add_spumem_mutex);
  452. ret = __add_pages(zone, start_pfn, nr_pages);
  453. mutex_unlock(&add_spumem_mutex);
  454. return ret;
  455. }
  456. static void __iomem * __init map_spe_prop(struct spu *spu,
  457. struct device_node *n, const char *name)
  458. {
  459. const struct address_prop {
  460. unsigned long address;
  461. unsigned int len;
  462. } __attribute__((packed)) *prop;
  463. const void *p;
  464. int proplen;
  465. void __iomem *ret = NULL;
  466. int err = 0;
  467. p = get_property(n, name, &proplen);
  468. if (proplen != sizeof (struct address_prop))
  469. return NULL;
  470. prop = p;
  471. err = cell_spuprop_present(spu, n, name);
  472. if (err && (err != -EEXIST))
  473. goto out;
  474. ret = ioremap(prop->address, prop->len);
  475. out:
  476. return ret;
  477. }
  478. static void spu_unmap(struct spu *spu)
  479. {
  480. iounmap(spu->priv2);
  481. iounmap(spu->priv1);
  482. iounmap(spu->problem);
  483. iounmap((__force u8 __iomem *)spu->local_store);
  484. }
  485. /* This function shall be abstracted for HV platforms */
  486. static int __init spu_map_interrupts_old(struct spu *spu, struct device_node *np)
  487. {
  488. unsigned int isrc;
  489. const u32 *tmp;
  490. /* Get the interrupt source unit from the device-tree */
  491. tmp = get_property(np, "isrc", NULL);
  492. if (!tmp)
  493. return -ENODEV;
  494. isrc = tmp[0];
  495. /* Add the node number */
  496. isrc |= spu->node << IIC_IRQ_NODE_SHIFT;
  497. /* Now map interrupts of all 3 classes */
  498. spu->irqs[0] = irq_create_mapping(NULL, IIC_IRQ_CLASS_0 | isrc);
  499. spu->irqs[1] = irq_create_mapping(NULL, IIC_IRQ_CLASS_1 | isrc);
  500. spu->irqs[2] = irq_create_mapping(NULL, IIC_IRQ_CLASS_2 | isrc);
  501. /* Right now, we only fail if class 2 failed */
  502. return spu->irqs[2] == NO_IRQ ? -EINVAL : 0;
  503. }
  504. static int __init spu_map_device_old(struct spu *spu, struct device_node *node)
  505. {
  506. const char *prop;
  507. int ret;
  508. ret = -ENODEV;
  509. spu->name = get_property(node, "name", NULL);
  510. if (!spu->name)
  511. goto out;
  512. prop = get_property(node, "local-store", NULL);
  513. if (!prop)
  514. goto out;
  515. spu->local_store_phys = *(unsigned long *)prop;
  516. /* we use local store as ram, not io memory */
  517. spu->local_store = (void __force *)
  518. map_spe_prop(spu, node, "local-store");
  519. if (!spu->local_store)
  520. goto out;
  521. prop = get_property(node, "problem", NULL);
  522. if (!prop)
  523. goto out_unmap;
  524. spu->problem_phys = *(unsigned long *)prop;
  525. spu->problem= map_spe_prop(spu, node, "problem");
  526. if (!spu->problem)
  527. goto out_unmap;
  528. spu->priv1= map_spe_prop(spu, node, "priv1");
  529. /* priv1 is not available on a hypervisor */
  530. spu->priv2= map_spe_prop(spu, node, "priv2");
  531. if (!spu->priv2)
  532. goto out_unmap;
  533. ret = 0;
  534. goto out;
  535. out_unmap:
  536. spu_unmap(spu);
  537. out:
  538. return ret;
  539. }
  540. static int __init spu_map_interrupts(struct spu *spu, struct device_node *np)
  541. {
  542. struct of_irq oirq;
  543. int ret;
  544. int i;
  545. for (i=0; i < 3; i++) {
  546. ret = of_irq_map_one(np, i, &oirq);
  547. if (ret)
  548. goto err;
  549. ret = -EINVAL;
  550. spu->irqs[i] = irq_create_of_mapping(oirq.controller,
  551. oirq.specifier, oirq.size);
  552. if (spu->irqs[i] == NO_IRQ)
  553. goto err;
  554. }
  555. return 0;
  556. err:
  557. pr_debug("failed to map irq %x for spu %s\n", *oirq.specifier, spu->name);
  558. for (; i >= 0; i--) {
  559. if (spu->irqs[i] != NO_IRQ)
  560. irq_dispose_mapping(spu->irqs[i]);
  561. }
  562. return ret;
  563. }
  564. static int spu_map_resource(struct device_node *node, int nr,
  565. void __iomem** virt, unsigned long *phys)
  566. {
  567. struct resource resource = { };
  568. int ret;
  569. ret = of_address_to_resource(node, 0, &resource);
  570. if (ret)
  571. goto out;
  572. if (phys)
  573. *phys = resource.start;
  574. *virt = ioremap(resource.start, resource.end - resource.start);
  575. if (!*virt)
  576. ret = -EINVAL;
  577. out:
  578. return ret;
  579. }
  580. static int __init spu_map_device(struct spu *spu, struct device_node *node)
  581. {
  582. int ret = -ENODEV;
  583. spu->name = get_property(node, "name", NULL);
  584. if (!spu->name)
  585. goto out;
  586. ret = spu_map_resource(node, 0, (void __iomem**)&spu->local_store,
  587. &spu->local_store_phys);
  588. if (ret)
  589. goto out;
  590. ret = spu_map_resource(node, 1, (void __iomem**)&spu->problem,
  591. &spu->problem_phys);
  592. if (ret)
  593. goto out_unmap;
  594. ret = spu_map_resource(node, 2, (void __iomem**)&spu->priv2,
  595. NULL);
  596. if (ret)
  597. goto out_unmap;
  598. if (!firmware_has_feature(FW_FEATURE_LPAR))
  599. ret = spu_map_resource(node, 3, (void __iomem**)&spu->priv1,
  600. NULL);
  601. if (ret)
  602. goto out_unmap;
  603. return 0;
  604. out_unmap:
  605. spu_unmap(spu);
  606. out:
  607. pr_debug("failed to map spe %s: %d\n", spu->name, ret);
  608. return ret;
  609. }
  610. struct sysdev_class spu_sysdev_class = {
  611. set_kset_name("spu")
  612. };
  613. static int spu_create_sysdev(struct spu *spu)
  614. {
  615. int ret;
  616. spu->sysdev.id = spu->number;
  617. spu->sysdev.cls = &spu_sysdev_class;
  618. ret = sysdev_register(&spu->sysdev);
  619. if (ret) {
  620. printk(KERN_ERR "Can't register SPU %d with sysfs\n",
  621. spu->number);
  622. return ret;
  623. }
  624. sysfs_add_device_to_node(&spu->sysdev, spu->nid);
  625. return 0;
  626. }
  627. static void spu_destroy_sysdev(struct spu *spu)
  628. {
  629. sysfs_remove_device_from_node(&spu->sysdev, spu->nid);
  630. sysdev_unregister(&spu->sysdev);
  631. }
  632. static int __init create_spu(struct device_node *spe)
  633. {
  634. struct spu *spu;
  635. int ret;
  636. static int number;
  637. ret = -ENOMEM;
  638. spu = kzalloc(sizeof (*spu), GFP_KERNEL);
  639. if (!spu)
  640. goto out;
  641. spu->node = find_spu_node_id(spe);
  642. if (spu->node >= MAX_NUMNODES) {
  643. printk(KERN_WARNING "SPE %s on node %d ignored,"
  644. " node number too big\n", spe->full_name, spu->node);
  645. printk(KERN_WARNING "Check if CONFIG_NUMA is enabled.\n");
  646. return -ENODEV;
  647. }
  648. spu->nid = of_node_to_nid(spe);
  649. if (spu->nid == -1)
  650. spu->nid = 0;
  651. ret = spu_map_device(spu, spe);
  652. /* try old method */
  653. if (ret)
  654. ret = spu_map_device_old(spu, spe);
  655. if (ret)
  656. goto out_free;
  657. ret = spu_map_interrupts(spu, spe);
  658. if (ret)
  659. ret = spu_map_interrupts_old(spu, spe);
  660. if (ret)
  661. goto out_unmap;
  662. spin_lock_init(&spu->register_lock);
  663. spu_mfc_sdr_setup(spu);
  664. spu_mfc_sr1_set(spu, 0x33);
  665. mutex_lock(&spu_mutex);
  666. spu->number = number++;
  667. ret = spu_request_irqs(spu);
  668. if (ret)
  669. goto out_unlock;
  670. ret = spu_create_sysdev(spu);
  671. if (ret)
  672. goto out_free_irqs;
  673. list_add(&spu->list, &spu_list[spu->node]);
  674. mutex_unlock(&spu_mutex);
  675. pr_debug(KERN_DEBUG "Using SPE %s %p %p %p %p %d\n",
  676. spu->name, spu->local_store,
  677. spu->problem, spu->priv1, spu->priv2, spu->number);
  678. goto out;
  679. out_free_irqs:
  680. spu_free_irqs(spu);
  681. out_unlock:
  682. mutex_unlock(&spu_mutex);
  683. out_unmap:
  684. spu_unmap(spu);
  685. out_free:
  686. kfree(spu);
  687. out:
  688. return ret;
  689. }
  690. static void destroy_spu(struct spu *spu)
  691. {
  692. list_del_init(&spu->list);
  693. spu_destroy_sysdev(spu);
  694. spu_free_irqs(spu);
  695. spu_unmap(spu);
  696. kfree(spu);
  697. }
  698. static void cleanup_spu_base(void)
  699. {
  700. struct spu *spu, *tmp;
  701. int node;
  702. mutex_lock(&spu_mutex);
  703. for (node = 0; node < MAX_NUMNODES; node++) {
  704. list_for_each_entry_safe(spu, tmp, &spu_list[node], list)
  705. destroy_spu(spu);
  706. }
  707. mutex_unlock(&spu_mutex);
  708. sysdev_class_unregister(&spu_sysdev_class);
  709. }
  710. module_exit(cleanup_spu_base);
  711. static int __init init_spu_base(void)
  712. {
  713. struct device_node *node;
  714. int i, ret;
  715. /* create sysdev class for spus */
  716. ret = sysdev_class_register(&spu_sysdev_class);
  717. if (ret)
  718. return ret;
  719. for (i = 0; i < MAX_NUMNODES; i++)
  720. INIT_LIST_HEAD(&spu_list[i]);
  721. ret = -ENODEV;
  722. for (node = of_find_node_by_type(NULL, "spe");
  723. node; node = of_find_node_by_type(node, "spe")) {
  724. ret = create_spu(node);
  725. if (ret) {
  726. printk(KERN_WARNING "%s: Error initializing %s\n",
  727. __FUNCTION__, node->name);
  728. cleanup_spu_base();
  729. break;
  730. }
  731. }
  732. return ret;
  733. }
  734. module_init(init_spu_base);
  735. MODULE_LICENSE("GPL");
  736. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");