fault.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.dar, SPE_EVENT_DMA_ALIGNMENT);
  77. if (stat & CLASS0_INVALID_DMA_COMMAND_INTR)
  78. spufs_handle_event(ctx, ctx->csa.dar, SPE_EVENT_INVALID_DMA);
  79. if (stat & CLASS0_SPU_ERROR_INTR)
  80. spufs_handle_event(ctx, ctx->csa.dar, SPE_EVENT_SPE_ERROR);
  81. return -EIO;
  82. }
  83. /*
  84. * bottom half handler for page faults, we can't do this from
  85. * interrupt context, since we might need to sleep.
  86. * we also need to give up the mutex so we can get scheduled
  87. * out while waiting for the backing store.
  88. *
  89. * TODO: try calling hash_page from the interrupt handler first
  90. * in order to speed up the easy case.
  91. */
  92. int spufs_handle_class1(struct spu_context *ctx)
  93. {
  94. u64 ea, dsisr, access;
  95. unsigned long flags;
  96. unsigned flt = 0;
  97. int ret;
  98. /*
  99. * dar and dsisr get passed from the registers
  100. * to the spu_context, to this function, but not
  101. * back to the spu if it gets scheduled again.
  102. *
  103. * if we don't handle the fault for a saved context
  104. * in time, we can still expect to get the same fault
  105. * the immediately after the context restore.
  106. */
  107. ea = ctx->csa.dar;
  108. dsisr = ctx->csa.dsisr;
  109. if (!(dsisr & (MFC_DSISR_PTE_NOT_FOUND | MFC_DSISR_ACCESS_DENIED)))
  110. return 0;
  111. spuctx_switch_state(ctx, SPU_UTIL_IOWAIT);
  112. pr_debug("ctx %p: ea %016lx, dsisr %016lx state %d\n", ctx, ea,
  113. dsisr, ctx->state);
  114. ctx->stats.hash_flt++;
  115. if (ctx->state == SPU_STATE_RUNNABLE)
  116. ctx->spu->stats.hash_flt++;
  117. /* we must not hold the lock when entering spu_handle_mm_fault */
  118. spu_release(ctx);
  119. access = (_PAGE_PRESENT | _PAGE_USER);
  120. access |= (dsisr & MFC_DSISR_ACCESS_PUT) ? _PAGE_RW : 0UL;
  121. local_irq_save(flags);
  122. ret = hash_page(ea, access, 0x300);
  123. local_irq_restore(flags);
  124. /* hashing failed, so try the actual fault handler */
  125. if (ret)
  126. ret = spu_handle_mm_fault(current->mm, ea, dsisr, &flt);
  127. /*
  128. * This is nasty: we need the state_mutex for all the bookkeeping even
  129. * if the syscall was interrupted by a signal. ewww.
  130. */
  131. mutex_lock(&ctx->state_mutex);
  132. /*
  133. * Clear dsisr under ctxt lock after handling the fault, so that
  134. * time slicing will not preempt the context while the page fault
  135. * handler is running. Context switch code removes mappings.
  136. */
  137. ctx->csa.dar = ctx->csa.dsisr = 0;
  138. /*
  139. * If we handled the fault successfully and are in runnable
  140. * state, restart the DMA.
  141. * In case of unhandled error report the problem to user space.
  142. */
  143. if (!ret) {
  144. if (flt & VM_FAULT_MAJOR)
  145. ctx->stats.maj_flt++;
  146. else
  147. ctx->stats.min_flt++;
  148. if (ctx->state == SPU_STATE_RUNNABLE) {
  149. if (flt & VM_FAULT_MAJOR)
  150. ctx->spu->stats.maj_flt++;
  151. else
  152. ctx->spu->stats.min_flt++;
  153. }
  154. if (ctx->spu)
  155. ctx->ops->restart_dma(ctx);
  156. } else
  157. spufs_handle_event(ctx, ea, SPE_EVENT_SPE_DATA_STORAGE);
  158. spuctx_switch_state(ctx, SPU_UTIL_SYSTEM);
  159. return ret;
  160. }