copro_fault.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * CoProcessor (SPU/AFU) mm fault handler
  3. *
  4. * (C) Copyright IBM Deutschland Entwicklung GmbH 2007
  5. *
  6. * Author: Arnd Bergmann <arndb@de.ibm.com>
  7. * Author: Jeremy Kerr <jk@ozlabs.org>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. */
  23. #include <linux/sched.h>
  24. #include <linux/mm.h>
  25. #include <linux/export.h>
  26. #include <asm/reg.h>
  27. #include <asm/copro.h>
  28. #include <asm/spu.h>
  29. #include <misc/cxl-base.h>
  30. /*
  31. * This ought to be kept in sync with the powerpc specific do_page_fault
  32. * function. Currently, there are a few corner cases that we haven't had
  33. * to handle fortunately.
  34. */
  35. int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea,
  36. unsigned long dsisr, vm_fault_t *flt)
  37. {
  38. struct vm_area_struct *vma;
  39. unsigned long is_write;
  40. int ret;
  41. if (mm == NULL)
  42. return -EFAULT;
  43. if (mm->pgd == NULL)
  44. return -EFAULT;
  45. down_read(&mm->mmap_sem);
  46. ret = -EFAULT;
  47. vma = find_vma(mm, ea);
  48. if (!vma)
  49. goto out_unlock;
  50. if (ea < vma->vm_start) {
  51. if (!(vma->vm_flags & VM_GROWSDOWN))
  52. goto out_unlock;
  53. if (expand_stack(vma, ea))
  54. goto out_unlock;
  55. }
  56. is_write = dsisr & DSISR_ISSTORE;
  57. if (is_write) {
  58. if (!(vma->vm_flags & VM_WRITE))
  59. goto out_unlock;
  60. } else {
  61. if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
  62. goto out_unlock;
  63. /*
  64. * PROT_NONE is covered by the VMA check above.
  65. * and hash should get a NOHPTE fault instead of
  66. * a PROTFAULT in case fixup is needed for things
  67. * like autonuma.
  68. */
  69. if (!radix_enabled())
  70. WARN_ON_ONCE(dsisr & DSISR_PROTFAULT);
  71. }
  72. ret = 0;
  73. *flt = handle_mm_fault(vma, ea, is_write ? FAULT_FLAG_WRITE : 0);
  74. if (unlikely(*flt & VM_FAULT_ERROR)) {
  75. if (*flt & VM_FAULT_OOM) {
  76. ret = -ENOMEM;
  77. goto out_unlock;
  78. } else if (*flt & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) {
  79. ret = -EFAULT;
  80. goto out_unlock;
  81. }
  82. BUG();
  83. }
  84. if (*flt & VM_FAULT_MAJOR)
  85. current->maj_flt++;
  86. else
  87. current->min_flt++;
  88. out_unlock:
  89. up_read(&mm->mmap_sem);
  90. return ret;
  91. }
  92. EXPORT_SYMBOL_GPL(copro_handle_mm_fault);
  93. int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb)
  94. {
  95. u64 vsid, vsidkey;
  96. int psize, ssize;
  97. switch (REGION_ID(ea)) {
  98. case USER_REGION_ID:
  99. pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea);
  100. if (mm == NULL)
  101. return 1;
  102. psize = get_slice_psize(mm, ea);
  103. ssize = user_segment_size(ea);
  104. vsid = get_user_vsid(&mm->context, ea, ssize);
  105. vsidkey = SLB_VSID_USER;
  106. break;
  107. case VMALLOC_REGION_ID:
  108. pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea);
  109. if (ea < VMALLOC_END)
  110. psize = mmu_vmalloc_psize;
  111. else
  112. psize = mmu_io_psize;
  113. ssize = mmu_kernel_ssize;
  114. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  115. vsidkey = SLB_VSID_KERNEL;
  116. break;
  117. case KERNEL_REGION_ID:
  118. pr_devel("%s: 0x%llx -- KERNEL_REGION_ID\n", __func__, ea);
  119. psize = mmu_linear_psize;
  120. ssize = mmu_kernel_ssize;
  121. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  122. vsidkey = SLB_VSID_KERNEL;
  123. break;
  124. default:
  125. pr_debug("%s: invalid region access at %016llx\n", __func__, ea);
  126. return 1;
  127. }
  128. /* Bad address */
  129. if (!vsid)
  130. return 1;
  131. vsid = (vsid << slb_vsid_shift(ssize)) | vsidkey;
  132. vsid |= mmu_psize_defs[psize].sllp |
  133. ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0);
  134. slb->esid = (ea & (ssize == MMU_SEGSIZE_1T ? ESID_MASK_1T : ESID_MASK)) | SLB_ESID_V;
  135. slb->vsid = vsid;
  136. return 0;
  137. }
  138. EXPORT_SYMBOL_GPL(copro_calculate_slb);
  139. void copro_flush_all_slbs(struct mm_struct *mm)
  140. {
  141. #ifdef CONFIG_SPU_BASE
  142. spu_flush_all_slbs(mm);
  143. #endif
  144. cxl_slbia(mm);
  145. }
  146. EXPORT_SYMBOL_GPL(copro_flush_all_slbs);