copro_fault.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * CoProcessor (SPU/AFU) mm fault handler
  4. *
  5. * (C) Copyright IBM Deutschland Entwicklung GmbH 2007
  6. *
  7. * Author: Arnd Bergmann <[email protected]>
  8. * Author: Jeremy Kerr <[email protected]>
  9. */
  10. #include <linux/sched.h>
  11. #include <linux/mm.h>
  12. #include <linux/export.h>
  13. #include <asm/reg.h>
  14. #include <asm/copro.h>
  15. #include <asm/spu.h>
  16. #include <misc/cxl-base.h>
  17. /*
  18. * This ought to be kept in sync with the powerpc specific do_page_fault
  19. * function. Currently, there are a few corner cases that we haven't had
  20. * to handle fortunately.
  21. */
  22. int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea,
  23. unsigned long dsisr, vm_fault_t *flt)
  24. {
  25. struct vm_area_struct *vma;
  26. unsigned long is_write;
  27. int ret;
  28. if (mm == NULL)
  29. return -EFAULT;
  30. if (mm->pgd == NULL)
  31. return -EFAULT;
  32. vma = lock_mm_and_find_vma(mm, ea, NULL);
  33. if (!vma)
  34. return -EFAULT;
  35. ret = -EFAULT;
  36. is_write = dsisr & DSISR_ISSTORE;
  37. if (is_write) {
  38. if (!(vma->vm_flags & VM_WRITE))
  39. goto out_unlock;
  40. } else {
  41. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  42. goto out_unlock;
  43. /*
  44. * PROT_NONE is covered by the VMA check above.
  45. * and hash should get a NOHPTE fault instead of
  46. * a PROTFAULT in case fixup is needed for things
  47. * like autonuma.
  48. */
  49. if (!radix_enabled())
  50. WARN_ON_ONCE(dsisr & DSISR_PROTFAULT);
  51. }
  52. ret = 0;
  53. *flt = handle_mm_fault(vma, ea, is_write ? FAULT_FLAG_WRITE : 0, NULL);
  54. /* The fault is fully completed (including releasing mmap lock) */
  55. if (*flt & VM_FAULT_COMPLETED)
  56. return 0;
  57. if (unlikely(*flt & VM_FAULT_ERROR)) {
  58. if (*flt & VM_FAULT_OOM) {
  59. ret = -ENOMEM;
  60. goto out_unlock;
  61. } else if (*flt & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) {
  62. ret = -EFAULT;
  63. goto out_unlock;
  64. }
  65. BUG();
  66. }
  67. out_unlock:
  68. mmap_read_unlock(mm);
  69. return ret;
  70. }
  71. EXPORT_SYMBOL_GPL(copro_handle_mm_fault);
  72. #ifdef CONFIG_PPC_64S_HASH_MMU
  73. int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb)
  74. {
  75. u64 vsid, vsidkey;
  76. int psize, ssize;
  77. switch (get_region_id(ea)) {
  78. case USER_REGION_ID:
  79. pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea);
  80. if (mm == NULL)
  81. return 1;
  82. psize = get_slice_psize(mm, ea);
  83. ssize = user_segment_size(ea);
  84. vsid = get_user_vsid(&mm->context, ea, ssize);
  85. vsidkey = SLB_VSID_USER;
  86. break;
  87. case VMALLOC_REGION_ID:
  88. pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea);
  89. psize = mmu_vmalloc_psize;
  90. ssize = mmu_kernel_ssize;
  91. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  92. vsidkey = SLB_VSID_KERNEL;
  93. break;
  94. case IO_REGION_ID:
  95. pr_devel("%s: 0x%llx -- IO_REGION_ID\n", __func__, ea);
  96. psize = mmu_io_psize;
  97. ssize = mmu_kernel_ssize;
  98. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  99. vsidkey = SLB_VSID_KERNEL;
  100. break;
  101. case LINEAR_MAP_REGION_ID:
  102. pr_devel("%s: 0x%llx -- LINEAR_MAP_REGION_ID\n", __func__, ea);
  103. psize = mmu_linear_psize;
  104. ssize = mmu_kernel_ssize;
  105. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  106. vsidkey = SLB_VSID_KERNEL;
  107. break;
  108. default:
  109. pr_debug("%s: invalid region access at %016llx\n", __func__, ea);
  110. return 1;
  111. }
  112. /* Bad address */
  113. if (!vsid)
  114. return 1;
  115. vsid = (vsid << slb_vsid_shift(ssize)) | vsidkey;
  116. vsid |= mmu_psize_defs[psize].sllp |
  117. ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0);
  118. slb->esid = (ea & (ssize == MMU_SEGSIZE_1T ? ESID_MASK_1T : ESID_MASK)) | SLB_ESID_V;
  119. slb->vsid = vsid;
  120. return 0;
  121. }
  122. EXPORT_SYMBOL_GPL(copro_calculate_slb);
  123. void copro_flush_all_slbs(struct mm_struct *mm)
  124. {
  125. #ifdef CONFIG_SPU_BASE
  126. spu_flush_all_slbs(mm);
  127. #endif
  128. cxl_slbia(mm);
  129. }
  130. EXPORT_SYMBOL_GPL(copro_flush_all_slbs);
  131. #endif