copro_fault.c 3.6 KB

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