fault.c 4.3 KB

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