copro_fault.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, unsigned *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. * protfault should only happen due to us
  65. * mapping a region readonly temporarily. PROT_NONE
  66. * is also covered by the VMA check above.
  67. */
  68. WARN_ON_ONCE(dsisr & DSISR_PROTFAULT);
  69. }
  70. ret = 0;
  71. *flt = handle_mm_fault(mm, vma, ea, is_write ? FAULT_FLAG_WRITE : 0);
  72. if (unlikely(*flt & VM_FAULT_ERROR)) {
  73. if (*flt & VM_FAULT_OOM) {
  74. ret = -ENOMEM;
  75. goto out_unlock;
  76. } else if (*flt & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) {
  77. ret = -EFAULT;
  78. goto out_unlock;
  79. }
  80. BUG();
  81. }
  82. if (*flt & VM_FAULT_MAJOR)
  83. current->maj_flt++;
  84. else
  85. current->min_flt++;
  86. out_unlock:
  87. up_read(&mm->mmap_sem);
  88. return ret;
  89. }
  90. EXPORT_SYMBOL_GPL(copro_handle_mm_fault);
  91. int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb)
  92. {
  93. u64 vsid, vsidkey;
  94. int psize, ssize;
  95. switch (REGION_ID(ea)) {
  96. case USER_REGION_ID:
  97. pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea);
  98. psize = get_slice_psize(mm, ea);
  99. ssize = user_segment_size(ea);
  100. vsid = get_vsid(mm->context.id, ea, ssize);
  101. vsidkey = SLB_VSID_USER;
  102. break;
  103. case VMALLOC_REGION_ID:
  104. pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea);
  105. if (ea < VMALLOC_END)
  106. psize = mmu_vmalloc_psize;
  107. else
  108. psize = mmu_io_psize;
  109. ssize = mmu_kernel_ssize;
  110. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  111. vsidkey = SLB_VSID_KERNEL;
  112. break;
  113. case KERNEL_REGION_ID:
  114. pr_devel("%s: 0x%llx -- KERNEL_REGION_ID\n", __func__, ea);
  115. psize = mmu_linear_psize;
  116. ssize = mmu_kernel_ssize;
  117. vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
  118. vsidkey = SLB_VSID_KERNEL;
  119. break;
  120. default:
  121. pr_debug("%s: invalid region access at %016llx\n", __func__, ea);
  122. return 1;
  123. }
  124. vsid = (vsid << slb_vsid_shift(ssize)) | vsidkey;
  125. vsid |= mmu_psize_defs[psize].sllp |
  126. ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0);
  127. slb->esid = (ea & (ssize == MMU_SEGSIZE_1T ? ESID_MASK_1T : ESID_MASK)) | SLB_ESID_V;
  128. slb->vsid = vsid;
  129. return 0;
  130. }
  131. EXPORT_SYMBOL_GPL(copro_calculate_slb);
  132. void copro_flush_all_slbs(struct mm_struct *mm)
  133. {
  134. #ifdef CONFIG_SPU_BASE
  135. spu_flush_all_slbs(mm);
  136. #endif
  137. cxl_slbia(mm);
  138. }
  139. EXPORT_SYMBOL_GPL(copro_flush_all_slbs);