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