spu_base.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  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/ptrace.h>
  27. #include <linux/slab.h>
  28. #include <linux/wait.h>
  29. #include <linux/mm.h>
  30. #include <linux/io.h>
  31. #include <linux/mutex.h>
  32. #include <linux/linux_logo.h>
  33. #include <linux/syscore_ops.h>
  34. #include <asm/spu.h>
  35. #include <asm/spu_priv1.h>
  36. #include <asm/spu_csa.h>
  37. #include <asm/xmon.h>
  38. #include <asm/prom.h>
  39. #include <asm/kexec.h>
  40. const struct spu_management_ops *spu_management_ops;
  41. EXPORT_SYMBOL_GPL(spu_management_ops);
  42. const struct spu_priv1_ops *spu_priv1_ops;
  43. EXPORT_SYMBOL_GPL(spu_priv1_ops);
  44. struct cbe_spu_info cbe_spu_info[MAX_NUMNODES];
  45. EXPORT_SYMBOL_GPL(cbe_spu_info);
  46. /*
  47. * The spufs fault-handling code needs to call force_sig_info to raise signals
  48. * on DMA errors. Export it here to avoid general kernel-wide access to this
  49. * function
  50. */
  51. EXPORT_SYMBOL_GPL(force_sig_info);
  52. /*
  53. * Protects cbe_spu_info and spu->number.
  54. */
  55. static DEFINE_SPINLOCK(spu_lock);
  56. /*
  57. * List of all spus in the system.
  58. *
  59. * This list is iterated by callers from irq context and callers that
  60. * want to sleep. Thus modifications need to be done with both
  61. * spu_full_list_lock and spu_full_list_mutex held, while iterating
  62. * through it requires either of these locks.
  63. *
  64. * In addition spu_full_list_lock protects all assignmens to
  65. * spu->mm.
  66. */
  67. static LIST_HEAD(spu_full_list);
  68. static DEFINE_SPINLOCK(spu_full_list_lock);
  69. static DEFINE_MUTEX(spu_full_list_mutex);
  70. void spu_invalidate_slbs(struct spu *spu)
  71. {
  72. struct spu_priv2 __iomem *priv2 = spu->priv2;
  73. unsigned long flags;
  74. spin_lock_irqsave(&spu->register_lock, flags);
  75. if (spu_mfc_sr1_get(spu) & MFC_STATE1_RELOCATE_MASK)
  76. out_be64(&priv2->slb_invalidate_all_W, 0UL);
  77. spin_unlock_irqrestore(&spu->register_lock, flags);
  78. }
  79. EXPORT_SYMBOL_GPL(spu_invalidate_slbs);
  80. /* This is called by the MM core when a segment size is changed, to
  81. * request a flush of all the SPEs using a given mm
  82. */
  83. void spu_flush_all_slbs(struct mm_struct *mm)
  84. {
  85. struct spu *spu;
  86. unsigned long flags;
  87. spin_lock_irqsave(&spu_full_list_lock, flags);
  88. list_for_each_entry(spu, &spu_full_list, full_list) {
  89. if (spu->mm == mm)
  90. spu_invalidate_slbs(spu);
  91. }
  92. spin_unlock_irqrestore(&spu_full_list_lock, flags);
  93. }
  94. /* The hack below stinks... try to do something better one of
  95. * these days... Does it even work properly with NR_CPUS == 1 ?
  96. */
  97. static inline void mm_needs_global_tlbie(struct mm_struct *mm)
  98. {
  99. int nr = (NR_CPUS > 1) ? NR_CPUS : NR_CPUS + 1;
  100. /* Global TLBIE broadcast required with SPEs. */
  101. bitmap_fill(cpumask_bits(mm_cpumask(mm)), nr);
  102. }
  103. void spu_associate_mm(struct spu *spu, struct mm_struct *mm)
  104. {
  105. unsigned long flags;
  106. spin_lock_irqsave(&spu_full_list_lock, flags);
  107. spu->mm = mm;
  108. spin_unlock_irqrestore(&spu_full_list_lock, flags);
  109. if (mm)
  110. mm_needs_global_tlbie(mm);
  111. }
  112. EXPORT_SYMBOL_GPL(spu_associate_mm);
  113. int spu_64k_pages_available(void)
  114. {
  115. return mmu_psize_defs[MMU_PAGE_64K].shift != 0;
  116. }
  117. EXPORT_SYMBOL_GPL(spu_64k_pages_available);
  118. static void spu_restart_dma(struct spu *spu)
  119. {
  120. struct spu_priv2 __iomem *priv2 = spu->priv2;
  121. if (!test_bit(SPU_CONTEXT_SWITCH_PENDING, &spu->flags))
  122. out_be64(&priv2->mfc_control_RW, MFC_CNTL_RESTART_DMA_COMMAND);
  123. else {
  124. set_bit(SPU_CONTEXT_FAULT_PENDING, &spu->flags);
  125. mb();
  126. }
  127. }
  128. static inline void spu_load_slb(struct spu *spu, int slbe, struct copro_slb *slb)
  129. {
  130. struct spu_priv2 __iomem *priv2 = spu->priv2;
  131. pr_debug("%s: adding SLB[%d] 0x%016llx 0x%016llx\n",
  132. __func__, slbe, slb->vsid, slb->esid);
  133. out_be64(&priv2->slb_index_W, slbe);
  134. /* set invalid before writing vsid */
  135. out_be64(&priv2->slb_esid_RW, 0);
  136. /* now it's safe to write the vsid */
  137. out_be64(&priv2->slb_vsid_RW, slb->vsid);
  138. /* setting the new esid makes the entry valid again */
  139. out_be64(&priv2->slb_esid_RW, slb->esid);
  140. }
  141. static int __spu_trap_data_seg(struct spu *spu, unsigned long ea)
  142. {
  143. struct copro_slb slb;
  144. int ret;
  145. ret = copro_calculate_slb(spu->mm, ea, &slb);
  146. if (ret)
  147. return ret;
  148. spu_load_slb(spu, spu->slb_replace, &slb);
  149. spu->slb_replace++;
  150. if (spu->slb_replace >= 8)
  151. spu->slb_replace = 0;
  152. spu_restart_dma(spu);
  153. spu->stats.slb_flt++;
  154. return 0;
  155. }
  156. extern int hash_page(unsigned long ea, unsigned long access, unsigned long trap); //XXX
  157. static int __spu_trap_data_map(struct spu *spu, unsigned long ea, u64 dsisr)
  158. {
  159. int ret;
  160. pr_debug("%s, %llx, %lx\n", __func__, dsisr, ea);
  161. /*
  162. * Handle kernel space hash faults immediately. User hash
  163. * faults need to be deferred to process context.
  164. */
  165. if ((dsisr & MFC_DSISR_PTE_NOT_FOUND) &&
  166. (REGION_ID(ea) != USER_REGION_ID)) {
  167. spin_unlock(&spu->register_lock);
  168. ret = hash_page(ea, _PAGE_PRESENT, 0x300);
  169. spin_lock(&spu->register_lock);
  170. if (!ret) {
  171. spu_restart_dma(spu);
  172. return 0;
  173. }
  174. }
  175. spu->class_1_dar = ea;
  176. spu->class_1_dsisr = dsisr;
  177. spu->stop_callback(spu, 1);
  178. spu->class_1_dar = 0;
  179. spu->class_1_dsisr = 0;
  180. return 0;
  181. }
  182. static void __spu_kernel_slb(void *addr, struct copro_slb *slb)
  183. {
  184. unsigned long ea = (unsigned long)addr;
  185. u64 llp;
  186. if (REGION_ID(ea) == KERNEL_REGION_ID)
  187. llp = mmu_psize_defs[mmu_linear_psize].sllp;
  188. else
  189. llp = mmu_psize_defs[mmu_virtual_psize].sllp;
  190. slb->vsid = (get_kernel_vsid(ea, MMU_SEGSIZE_256M) << SLB_VSID_SHIFT) |
  191. SLB_VSID_KERNEL | llp;
  192. slb->esid = (ea & ESID_MASK) | SLB_ESID_V;
  193. }
  194. /**
  195. * Given an array of @nr_slbs SLB entries, @slbs, return non-zero if the
  196. * address @new_addr is present.
  197. */
  198. static inline int __slb_present(struct copro_slb *slbs, int nr_slbs,
  199. void *new_addr)
  200. {
  201. unsigned long ea = (unsigned long)new_addr;
  202. int i;
  203. for (i = 0; i < nr_slbs; i++)
  204. if (!((slbs[i].esid ^ ea) & ESID_MASK))
  205. return 1;
  206. return 0;
  207. }
  208. /**
  209. * Setup the SPU kernel SLBs, in preparation for a context save/restore. We
  210. * need to map both the context save area, and the save/restore code.
  211. *
  212. * Because the lscsa and code may cross segment boundaires, we check to see
  213. * if mappings are required for the start and end of each range. We currently
  214. * assume that the mappings are smaller that one segment - if not, something
  215. * is seriously wrong.
  216. */
  217. void spu_setup_kernel_slbs(struct spu *spu, struct spu_lscsa *lscsa,
  218. void *code, int code_size)
  219. {
  220. struct copro_slb slbs[4];
  221. int i, nr_slbs = 0;
  222. /* start and end addresses of both mappings */
  223. void *addrs[] = {
  224. lscsa, (void *)lscsa + sizeof(*lscsa) - 1,
  225. code, code + code_size - 1
  226. };
  227. /* check the set of addresses, and create a new entry in the slbs array
  228. * if there isn't already a SLB for that address */
  229. for (i = 0; i < ARRAY_SIZE(addrs); i++) {
  230. if (__slb_present(slbs, nr_slbs, addrs[i]))
  231. continue;
  232. __spu_kernel_slb(addrs[i], &slbs[nr_slbs]);
  233. nr_slbs++;
  234. }
  235. spin_lock_irq(&spu->register_lock);
  236. /* Add the set of SLBs */
  237. for (i = 0; i < nr_slbs; i++)
  238. spu_load_slb(spu, i, &slbs[i]);
  239. spin_unlock_irq(&spu->register_lock);
  240. }
  241. EXPORT_SYMBOL_GPL(spu_setup_kernel_slbs);
  242. static irqreturn_t
  243. spu_irq_class_0(int irq, void *data)
  244. {
  245. struct spu *spu;
  246. unsigned long stat, mask;
  247. spu = data;
  248. spin_lock(&spu->register_lock);
  249. mask = spu_int_mask_get(spu, 0);
  250. stat = spu_int_stat_get(spu, 0) & mask;
  251. spu->class_0_pending |= stat;
  252. spu->class_0_dar = spu_mfc_dar_get(spu);
  253. spu->stop_callback(spu, 0);
  254. spu->class_0_pending = 0;
  255. spu->class_0_dar = 0;
  256. spu_int_stat_clear(spu, 0, stat);
  257. spin_unlock(&spu->register_lock);
  258. return IRQ_HANDLED;
  259. }
  260. static irqreturn_t
  261. spu_irq_class_1(int irq, void *data)
  262. {
  263. struct spu *spu;
  264. unsigned long stat, mask, dar, dsisr;
  265. spu = data;
  266. /* atomically read & clear class1 status. */
  267. spin_lock(&spu->register_lock);
  268. mask = spu_int_mask_get(spu, 1);
  269. stat = spu_int_stat_get(spu, 1) & mask;
  270. dar = spu_mfc_dar_get(spu);
  271. dsisr = spu_mfc_dsisr_get(spu);
  272. if (stat & CLASS1_STORAGE_FAULT_INTR)
  273. spu_mfc_dsisr_set(spu, 0ul);
  274. spu_int_stat_clear(spu, 1, stat);
  275. pr_debug("%s: %lx %lx %lx %lx\n", __func__, mask, stat,
  276. dar, dsisr);
  277. if (stat & CLASS1_SEGMENT_FAULT_INTR)
  278. __spu_trap_data_seg(spu, dar);
  279. if (stat & CLASS1_STORAGE_FAULT_INTR)
  280. __spu_trap_data_map(spu, dar, dsisr);
  281. if (stat & CLASS1_LS_COMPARE_SUSPEND_ON_GET_INTR)
  282. ;
  283. if (stat & CLASS1_LS_COMPARE_SUSPEND_ON_PUT_INTR)
  284. ;
  285. spu->class_1_dsisr = 0;
  286. spu->class_1_dar = 0;
  287. spin_unlock(&spu->register_lock);
  288. return stat ? IRQ_HANDLED : IRQ_NONE;
  289. }
  290. static irqreturn_t
  291. spu_irq_class_2(int irq, void *data)
  292. {
  293. struct spu *spu;
  294. unsigned long stat;
  295. unsigned long mask;
  296. const int mailbox_intrs =
  297. CLASS2_MAILBOX_THRESHOLD_INTR | CLASS2_MAILBOX_INTR;
  298. spu = data;
  299. spin_lock(&spu->register_lock);
  300. stat = spu_int_stat_get(spu, 2);
  301. mask = spu_int_mask_get(spu, 2);
  302. /* ignore interrupts we're not waiting for */
  303. stat &= mask;
  304. /* mailbox interrupts are level triggered. mask them now before
  305. * acknowledging */
  306. if (stat & mailbox_intrs)
  307. spu_int_mask_and(spu, 2, ~(stat & mailbox_intrs));
  308. /* acknowledge all interrupts before the callbacks */
  309. spu_int_stat_clear(spu, 2, stat);
  310. pr_debug("class 2 interrupt %d, %lx, %lx\n", irq, stat, mask);
  311. if (stat & CLASS2_MAILBOX_INTR)
  312. spu->ibox_callback(spu);
  313. if (stat & CLASS2_SPU_STOP_INTR)
  314. spu->stop_callback(spu, 2);
  315. if (stat & CLASS2_SPU_HALT_INTR)
  316. spu->stop_callback(spu, 2);
  317. if (stat & CLASS2_SPU_DMA_TAG_GROUP_COMPLETE_INTR)
  318. spu->mfc_callback(spu);
  319. if (stat & CLASS2_MAILBOX_THRESHOLD_INTR)
  320. spu->wbox_callback(spu);
  321. spu->stats.class2_intr++;
  322. spin_unlock(&spu->register_lock);
  323. return stat ? IRQ_HANDLED : IRQ_NONE;
  324. }
  325. static int spu_request_irqs(struct spu *spu)
  326. {
  327. int ret = 0;
  328. if (spu->irqs[0] != NO_IRQ) {
  329. snprintf(spu->irq_c0, sizeof (spu->irq_c0), "spe%02d.0",
  330. spu->number);
  331. ret = request_irq(spu->irqs[0], spu_irq_class_0,
  332. 0, spu->irq_c0, spu);
  333. if (ret)
  334. goto bail0;
  335. }
  336. if (spu->irqs[1] != NO_IRQ) {
  337. snprintf(spu->irq_c1, sizeof (spu->irq_c1), "spe%02d.1",
  338. spu->number);
  339. ret = request_irq(spu->irqs[1], spu_irq_class_1,
  340. 0, spu->irq_c1, spu);
  341. if (ret)
  342. goto bail1;
  343. }
  344. if (spu->irqs[2] != NO_IRQ) {
  345. snprintf(spu->irq_c2, sizeof (spu->irq_c2), "spe%02d.2",
  346. spu->number);
  347. ret = request_irq(spu->irqs[2], spu_irq_class_2,
  348. 0, spu->irq_c2, spu);
  349. if (ret)
  350. goto bail2;
  351. }
  352. return 0;
  353. bail2:
  354. if (spu->irqs[1] != NO_IRQ)
  355. free_irq(spu->irqs[1], spu);
  356. bail1:
  357. if (spu->irqs[0] != NO_IRQ)
  358. free_irq(spu->irqs[0], spu);
  359. bail0:
  360. return ret;
  361. }
  362. static void spu_free_irqs(struct spu *spu)
  363. {
  364. if (spu->irqs[0] != NO_IRQ)
  365. free_irq(spu->irqs[0], spu);
  366. if (spu->irqs[1] != NO_IRQ)
  367. free_irq(spu->irqs[1], spu);
  368. if (spu->irqs[2] != NO_IRQ)
  369. free_irq(spu->irqs[2], spu);
  370. }
  371. void spu_init_channels(struct spu *spu)
  372. {
  373. static const struct {
  374. unsigned channel;
  375. unsigned count;
  376. } zero_list[] = {
  377. { 0x00, 1, }, { 0x01, 1, }, { 0x03, 1, }, { 0x04, 1, },
  378. { 0x18, 1, }, { 0x19, 1, }, { 0x1b, 1, }, { 0x1d, 1, },
  379. }, count_list[] = {
  380. { 0x00, 0, }, { 0x03, 0, }, { 0x04, 0, }, { 0x15, 16, },
  381. { 0x17, 1, }, { 0x18, 0, }, { 0x19, 0, }, { 0x1b, 0, },
  382. { 0x1c, 1, }, { 0x1d, 0, }, { 0x1e, 1, },
  383. };
  384. struct spu_priv2 __iomem *priv2;
  385. int i;
  386. priv2 = spu->priv2;
  387. /* initialize all channel data to zero */
  388. for (i = 0; i < ARRAY_SIZE(zero_list); i++) {
  389. int count;
  390. out_be64(&priv2->spu_chnlcntptr_RW, zero_list[i].channel);
  391. for (count = 0; count < zero_list[i].count; count++)
  392. out_be64(&priv2->spu_chnldata_RW, 0);
  393. }
  394. /* initialize channel counts to meaningful values */
  395. for (i = 0; i < ARRAY_SIZE(count_list); i++) {
  396. out_be64(&priv2->spu_chnlcntptr_RW, count_list[i].channel);
  397. out_be64(&priv2->spu_chnlcnt_RW, count_list[i].count);
  398. }
  399. }
  400. EXPORT_SYMBOL_GPL(spu_init_channels);
  401. static struct bus_type spu_subsys = {
  402. .name = "spu",
  403. .dev_name = "spu",
  404. };
  405. int spu_add_dev_attr(struct device_attribute *attr)
  406. {
  407. struct spu *spu;
  408. mutex_lock(&spu_full_list_mutex);
  409. list_for_each_entry(spu, &spu_full_list, full_list)
  410. device_create_file(&spu->dev, attr);
  411. mutex_unlock(&spu_full_list_mutex);
  412. return 0;
  413. }
  414. EXPORT_SYMBOL_GPL(spu_add_dev_attr);
  415. int spu_add_dev_attr_group(struct attribute_group *attrs)
  416. {
  417. struct spu *spu;
  418. int rc = 0;
  419. mutex_lock(&spu_full_list_mutex);
  420. list_for_each_entry(spu, &spu_full_list, full_list) {
  421. rc = sysfs_create_group(&spu->dev.kobj, attrs);
  422. /* we're in trouble here, but try unwinding anyway */
  423. if (rc) {
  424. printk(KERN_ERR "%s: can't create sysfs group '%s'\n",
  425. __func__, attrs->name);
  426. list_for_each_entry_continue_reverse(spu,
  427. &spu_full_list, full_list)
  428. sysfs_remove_group(&spu->dev.kobj, attrs);
  429. break;
  430. }
  431. }
  432. mutex_unlock(&spu_full_list_mutex);
  433. return rc;
  434. }
  435. EXPORT_SYMBOL_GPL(spu_add_dev_attr_group);
  436. void spu_remove_dev_attr(struct device_attribute *attr)
  437. {
  438. struct spu *spu;
  439. mutex_lock(&spu_full_list_mutex);
  440. list_for_each_entry(spu, &spu_full_list, full_list)
  441. device_remove_file(&spu->dev, attr);
  442. mutex_unlock(&spu_full_list_mutex);
  443. }
  444. EXPORT_SYMBOL_GPL(spu_remove_dev_attr);
  445. void spu_remove_dev_attr_group(struct attribute_group *attrs)
  446. {
  447. struct spu *spu;
  448. mutex_lock(&spu_full_list_mutex);
  449. list_for_each_entry(spu, &spu_full_list, full_list)
  450. sysfs_remove_group(&spu->dev.kobj, attrs);
  451. mutex_unlock(&spu_full_list_mutex);
  452. }
  453. EXPORT_SYMBOL_GPL(spu_remove_dev_attr_group);
  454. static int spu_create_dev(struct spu *spu)
  455. {
  456. int ret;
  457. spu->dev.id = spu->number;
  458. spu->dev.bus = &spu_subsys;
  459. ret = device_register(&spu->dev);
  460. if (ret) {
  461. printk(KERN_ERR "Can't register SPU %d with sysfs\n",
  462. spu->number);
  463. return ret;
  464. }
  465. sysfs_add_device_to_node(&spu->dev, spu->node);
  466. return 0;
  467. }
  468. static int __init create_spu(void *data)
  469. {
  470. struct spu *spu;
  471. int ret;
  472. static int number;
  473. unsigned long flags;
  474. ret = -ENOMEM;
  475. spu = kzalloc(sizeof (*spu), GFP_KERNEL);
  476. if (!spu)
  477. goto out;
  478. spu->alloc_state = SPU_FREE;
  479. spin_lock_init(&spu->register_lock);
  480. spin_lock(&spu_lock);
  481. spu->number = number++;
  482. spin_unlock(&spu_lock);
  483. ret = spu_create_spu(spu, data);
  484. if (ret)
  485. goto out_free;
  486. spu_mfc_sdr_setup(spu);
  487. spu_mfc_sr1_set(spu, 0x33);
  488. ret = spu_request_irqs(spu);
  489. if (ret)
  490. goto out_destroy;
  491. ret = spu_create_dev(spu);
  492. if (ret)
  493. goto out_free_irqs;
  494. mutex_lock(&cbe_spu_info[spu->node].list_mutex);
  495. list_add(&spu->cbe_list, &cbe_spu_info[spu->node].spus);
  496. cbe_spu_info[spu->node].n_spus++;
  497. mutex_unlock(&cbe_spu_info[spu->node].list_mutex);
  498. mutex_lock(&spu_full_list_mutex);
  499. spin_lock_irqsave(&spu_full_list_lock, flags);
  500. list_add(&spu->full_list, &spu_full_list);
  501. spin_unlock_irqrestore(&spu_full_list_lock, flags);
  502. mutex_unlock(&spu_full_list_mutex);
  503. spu->stats.util_state = SPU_UTIL_IDLE_LOADED;
  504. spu->stats.tstamp = ktime_get_ns();
  505. INIT_LIST_HEAD(&spu->aff_list);
  506. goto out;
  507. out_free_irqs:
  508. spu_free_irqs(spu);
  509. out_destroy:
  510. spu_destroy_spu(spu);
  511. out_free:
  512. kfree(spu);
  513. out:
  514. return ret;
  515. }
  516. static const char *spu_state_names[] = {
  517. "user", "system", "iowait", "idle"
  518. };
  519. static unsigned long long spu_acct_time(struct spu *spu,
  520. enum spu_utilization_state state)
  521. {
  522. unsigned long long time = spu->stats.times[state];
  523. /*
  524. * If the spu is idle or the context is stopped, utilization
  525. * statistics are not updated. Apply the time delta from the
  526. * last recorded state of the spu.
  527. */
  528. if (spu->stats.util_state == state)
  529. time += ktime_get_ns() - spu->stats.tstamp;
  530. return time / NSEC_PER_MSEC;
  531. }
  532. static ssize_t spu_stat_show(struct device *dev,
  533. struct device_attribute *attr, char *buf)
  534. {
  535. struct spu *spu = container_of(dev, struct spu, dev);
  536. return sprintf(buf, "%s %llu %llu %llu %llu "
  537. "%llu %llu %llu %llu %llu %llu %llu %llu\n",
  538. spu_state_names[spu->stats.util_state],
  539. spu_acct_time(spu, SPU_UTIL_USER),
  540. spu_acct_time(spu, SPU_UTIL_SYSTEM),
  541. spu_acct_time(spu, SPU_UTIL_IOWAIT),
  542. spu_acct_time(spu, SPU_UTIL_IDLE_LOADED),
  543. spu->stats.vol_ctx_switch,
  544. spu->stats.invol_ctx_switch,
  545. spu->stats.slb_flt,
  546. spu->stats.hash_flt,
  547. spu->stats.min_flt,
  548. spu->stats.maj_flt,
  549. spu->stats.class2_intr,
  550. spu->stats.libassist);
  551. }
  552. static DEVICE_ATTR(stat, 0444, spu_stat_show, NULL);
  553. #ifdef CONFIG_KEXEC
  554. struct crash_spu_info {
  555. struct spu *spu;
  556. u32 saved_spu_runcntl_RW;
  557. u32 saved_spu_status_R;
  558. u32 saved_spu_npc_RW;
  559. u64 saved_mfc_sr1_RW;
  560. u64 saved_mfc_dar;
  561. u64 saved_mfc_dsisr;
  562. };
  563. #define CRASH_NUM_SPUS 16 /* Enough for current hardware */
  564. static struct crash_spu_info crash_spu_info[CRASH_NUM_SPUS];
  565. static void crash_kexec_stop_spus(void)
  566. {
  567. struct spu *spu;
  568. int i;
  569. u64 tmp;
  570. for (i = 0; i < CRASH_NUM_SPUS; i++) {
  571. if (!crash_spu_info[i].spu)
  572. continue;
  573. spu = crash_spu_info[i].spu;
  574. crash_spu_info[i].saved_spu_runcntl_RW =
  575. in_be32(&spu->problem->spu_runcntl_RW);
  576. crash_spu_info[i].saved_spu_status_R =
  577. in_be32(&spu->problem->spu_status_R);
  578. crash_spu_info[i].saved_spu_npc_RW =
  579. in_be32(&spu->problem->spu_npc_RW);
  580. crash_spu_info[i].saved_mfc_dar = spu_mfc_dar_get(spu);
  581. crash_spu_info[i].saved_mfc_dsisr = spu_mfc_dsisr_get(spu);
  582. tmp = spu_mfc_sr1_get(spu);
  583. crash_spu_info[i].saved_mfc_sr1_RW = tmp;
  584. tmp &= ~MFC_STATE1_MASTER_RUN_CONTROL_MASK;
  585. spu_mfc_sr1_set(spu, tmp);
  586. __delay(200);
  587. }
  588. }
  589. static void crash_register_spus(struct list_head *list)
  590. {
  591. struct spu *spu;
  592. int ret;
  593. list_for_each_entry(spu, list, full_list) {
  594. if (WARN_ON(spu->number >= CRASH_NUM_SPUS))
  595. continue;
  596. crash_spu_info[spu->number].spu = spu;
  597. }
  598. ret = crash_shutdown_register(&crash_kexec_stop_spus);
  599. if (ret)
  600. printk(KERN_ERR "Could not register SPU crash handler");
  601. }
  602. #else
  603. static inline void crash_register_spus(struct list_head *list)
  604. {
  605. }
  606. #endif
  607. static void spu_shutdown(void)
  608. {
  609. struct spu *spu;
  610. mutex_lock(&spu_full_list_mutex);
  611. list_for_each_entry(spu, &spu_full_list, full_list) {
  612. spu_free_irqs(spu);
  613. spu_destroy_spu(spu);
  614. }
  615. mutex_unlock(&spu_full_list_mutex);
  616. }
  617. static struct syscore_ops spu_syscore_ops = {
  618. .shutdown = spu_shutdown,
  619. };
  620. static int __init init_spu_base(void)
  621. {
  622. int i, ret = 0;
  623. for (i = 0; i < MAX_NUMNODES; i++) {
  624. mutex_init(&cbe_spu_info[i].list_mutex);
  625. INIT_LIST_HEAD(&cbe_spu_info[i].spus);
  626. }
  627. if (!spu_management_ops)
  628. goto out;
  629. /* create system subsystem for spus */
  630. ret = subsys_system_register(&spu_subsys, NULL);
  631. if (ret)
  632. goto out;
  633. ret = spu_enumerate_spus(create_spu);
  634. if (ret < 0) {
  635. printk(KERN_WARNING "%s: Error initializing spus\n",
  636. __func__);
  637. goto out_unregister_subsys;
  638. }
  639. if (ret > 0)
  640. fb_append_extra_logo(&logo_spe_clut224, ret);
  641. mutex_lock(&spu_full_list_mutex);
  642. xmon_register_spus(&spu_full_list);
  643. crash_register_spus(&spu_full_list);
  644. mutex_unlock(&spu_full_list_mutex);
  645. spu_add_dev_attr(&dev_attr_stat);
  646. register_syscore_ops(&spu_syscore_ops);
  647. spu_init_affinity();
  648. return 0;
  649. out_unregister_subsys:
  650. bus_unregister(&spu_subsys);
  651. out:
  652. return ret;
  653. }
  654. module_init(init_spu_base);
  655. MODULE_LICENSE("GPL");
  656. MODULE_AUTHOR("Arnd Bergmann <arndb@de.ibm.com>");