fault.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include <linux/sched.h>
  23. #include <linux/mm.h>
  24. #include <linux/module.h>
  25. #include <asm/spu.h>
  26. #include <asm/spu_csa.h>
  27. #include "spufs.h"
  28. /**
  29. * Handle an SPE event, depending on context SPU_CREATE_EVENTS_ENABLED flag.
  30. *
  31. * If the context was created with events, we just set the return event.
  32. * Otherwise, send an appropriate signal to the process.
  33. */
  34. static void spufs_handle_event(struct spu_context *ctx,
  35. unsigned long ea, int type)
  36. {
  37. siginfo_t info;
  38. if (ctx->flags & SPU_CREATE_EVENTS_ENABLED) {
  39. ctx->event_return |= type;
  40. wake_up_all(&ctx->stop_wq);
  41. return;
  42. }
  43. memset(&info, 0, sizeof(info));
  44. switch (type) {
  45. case SPE_EVENT_INVALID_DMA:
  46. info.si_signo = SIGBUS;
  47. info.si_code = BUS_OBJERR;
  48. break;
  49. case SPE_EVENT_SPE_DATA_STORAGE:
  50. info.si_signo = SIGSEGV;
  51. info.si_addr = (void __user *)ea;
  52. info.si_code = SEGV_ACCERR;
  53. ctx->ops->restart_dma(ctx);
  54. break;
  55. case SPE_EVENT_DMA_ALIGNMENT:
  56. info.si_signo = SIGBUS;
  57. /* DAR isn't set for an alignment fault :( */
  58. info.si_code = BUS_ADRALN;
  59. break;
  60. case SPE_EVENT_SPE_ERROR:
  61. info.si_signo = SIGILL;
  62. info.si_addr = (void __user *)(unsigned long)
  63. ctx->ops->npc_read(ctx) - 4;
  64. info.si_code = ILL_ILLOPC;
  65. break;
  66. }
  67. if (info.si_signo)
  68. force_sig_info(info.si_signo, &info, current);
  69. }
  70. int spufs_handle_class0(struct spu_context *ctx)
  71. {
  72. unsigned long stat = ctx->csa.class_0_pending & CLASS0_INTR_MASK;
  73. if (likely(!stat))
  74. return 0;
  75. if (stat & CLASS0_DMA_ALIGNMENT_INTR)
  76. spufs_handle_event(ctx, ctx->csa.class_0_dar,
  77. SPE_EVENT_DMA_ALIGNMENT);
  78. if (stat & CLASS0_INVALID_DMA_COMMAND_INTR)
  79. spufs_handle_event(ctx, ctx->csa.class_0_dar,
  80. SPE_EVENT_INVALID_DMA);
  81. if (stat & CLASS0_SPU_ERROR_INTR)
  82. spufs_handle_event(ctx, ctx->csa.class_0_dar,
  83. SPE_EVENT_SPE_ERROR);
  84. ctx->csa.class_0_pending = 0;
  85. return -EIO;
  86. }
  87. /*
  88. * bottom half handler for page faults, we can't do this from
  89. * interrupt context, since we might need to sleep.
  90. * we also need to give up the mutex so we can get scheduled
  91. * out while waiting for the backing store.
  92. *
  93. * TODO: try calling hash_page from the interrupt handler first
  94. * in order to speed up the easy case.
  95. */
  96. int spufs_handle_class1(struct spu_context *ctx)
  97. {
  98. u64 ea, dsisr, access;
  99. unsigned long flags;
  100. unsigned flt = 0;
  101. int ret;
  102. /*
  103. * dar and dsisr get passed from the registers
  104. * to the spu_context, to this function, but not
  105. * back to the spu if it gets scheduled again.
  106. *
  107. * if we don't handle the fault for a saved context
  108. * in time, we can still expect to get the same fault
  109. * the immediately after the context restore.
  110. */
  111. ea = ctx->csa.class_1_dar;
  112. dsisr = ctx->csa.class_1_dsisr;
  113. if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
  114. return 0;
  115. spuctx_switch_state(ctx, SPU_UTIL_IOWAIT);
  116. pr_debug("ctx %p: ea %016llx, dsisr %016llx state %d\n", ctx, ea,
  117. dsisr, ctx->state);
  118. ctx->stats.hash_flt++;
  119. if (ctx->state == SPU_STATE_RUNNABLE)
  120. ctx->spu->stats.hash_flt++;
  121. /* we must not hold the lock when entering spu_handle_mm_fault */
  122. spu_release(ctx);
  123. access = (_PAGE_PRESENT | _PAGE_USER);
  124. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  125. local_irq_save(flags);
  126. ret = hash_page(ea, access, 0x300);
  127. local_irq_restore(flags);
  128. /* hashing failed, so try the actual fault handler */
  129. if (ret)
  130. ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt);
  131. /*
  132. * This is nasty: we need the state_mutex for all the bookkeeping even
  133. * if the syscall was interrupted by a signal. ewww.
  134. */
  135. mutex_lock(&ctx->state_mutex);
  136. /*
  137. * Clear dsisr under ctxt lock after handling the fault, so that
  138. * time slicing will not preempt the context while the page fault
  139. * handler is running. Context switch code removes mappings.
  140. */
  141. ctx->csa.class_1_dar = ctx->csa.class_1_dsisr = 0;
  142. /*
  143. * If we handled the fault successfully and are in runnable
  144. * state, restart the DMA.
  145. * In case of unhandled error report the problem to user space.
  146. */
  147. if (!ret) {
  148. if (flt & VM_FAULT_MAJOR)
  149. ctx->stats.maj_flt++;
  150. else
  151. ctx->stats.min_flt++;
  152. if (ctx->state == SPU_STATE_RUNNABLE) {
  153. if (flt & VM_FAULT_MAJOR)
  154. ctx->spu->stats.maj_flt++;
  155. else
  156. ctx->spu->stats.min_flt++;
  157. }
  158. if (ctx->spu)
  159. ctx->ops->restart_dma(ctx);
  160. } else
  161. spufs_handle_event(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
  162. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  163. return ret;
  164. }