fault.c 5.0 KB

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