Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 1 | /* |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 2 | * CoProcessor (SPU/AFU) mm fault handler |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 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> |
Paul Gortmaker | 4b16f8e | 2011-07-22 18:24:23 -0400 | [diff] [blame] | 25 | #include <linux/export.h> |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 26 | #include <asm/reg.h> |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 27 | #include <asm/copro.h> |
Ian Munsie | be3ebfe | 2014-10-08 19:54:52 +1100 | [diff] [blame^] | 28 | #include <asm/spu.h> |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 29 | |
| 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 | */ |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 35 | int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea, |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 36 | unsigned long dsisr, unsigned *flt) |
| 37 | { |
| 38 | struct vm_area_struct *vma; |
| 39 | unsigned long is_write; |
| 40 | int ret; |
| 41 | |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 42 | if (mm == NULL) |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 43 | return -EFAULT; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 44 | |
| 45 | if (mm->pgd == NULL) |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 46 | return -EFAULT; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 47 | |
| 48 | down_read(&mm->mmap_sem); |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 49 | ret = -EFAULT; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 50 | vma = find_vma(mm, ea); |
| 51 | if (!vma) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 52 | goto out_unlock; |
| 53 | |
| 54 | if (ea < vma->vm_start) { |
| 55 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 56 | goto out_unlock; |
| 57 | if (expand_stack(vma, ea)) |
| 58 | goto out_unlock; |
| 59 | } |
| 60 | |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 61 | is_write = dsisr & DSISR_ISSTORE; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 62 | if (is_write) { |
| 63 | if (!(vma->vm_flags & VM_WRITE)) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 64 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 65 | } else { |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 66 | if (dsisr & DSISR_PROTFAULT) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 67 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 68 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 69 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 70 | } |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 71 | |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 72 | ret = 0; |
Linus Torvalds | d06063c | 2009-04-10 09:01:23 -0700 | [diff] [blame] | 73 | *flt = handle_mm_fault(mm, vma, ea, is_write ? FAULT_FLAG_WRITE : 0); |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 74 | if (unlikely(*flt & VM_FAULT_ERROR)) { |
| 75 | if (*flt & VM_FAULT_OOM) { |
| 76 | ret = -ENOMEM; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 77 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 78 | } else if (*flt & VM_FAULT_SIGBUS) { |
| 79 | ret = -EFAULT; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 80 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 81 | } |
| 82 | BUG(); |
| 83 | } |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 84 | |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 85 | if (*flt & VM_FAULT_MAJOR) |
| 86 | current->maj_flt++; |
| 87 | else |
| 88 | current->min_flt++; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 89 | |
| 90 | out_unlock: |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 91 | up_read(&mm->mmap_sem); |
| 92 | return ret; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 93 | } |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 94 | EXPORT_SYMBOL_GPL(copro_handle_mm_fault); |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 95 | |
| 96 | int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb) |
| 97 | { |
| 98 | u64 vsid; |
| 99 | int psize, ssize; |
| 100 | |
| 101 | slb->esid = (ea & ESID_MASK) | SLB_ESID_V; |
| 102 | |
| 103 | switch (REGION_ID(ea)) { |
| 104 | case USER_REGION_ID: |
| 105 | pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea); |
| 106 | psize = get_slice_psize(mm, ea); |
| 107 | ssize = user_segment_size(ea); |
| 108 | vsid = get_vsid(mm->context.id, ea, ssize); |
| 109 | break; |
| 110 | case VMALLOC_REGION_ID: |
| 111 | pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea); |
| 112 | if (ea < VMALLOC_END) |
| 113 | psize = mmu_vmalloc_psize; |
| 114 | else |
| 115 | psize = mmu_io_psize; |
| 116 | ssize = mmu_kernel_ssize; |
| 117 | vsid = get_kernel_vsid(ea, mmu_kernel_ssize); |
| 118 | break; |
| 119 | case KERNEL_REGION_ID: |
| 120 | pr_devel("%s: 0x%llx -- KERNEL_REGION_ID\n", __func__, ea); |
| 121 | psize = mmu_linear_psize; |
| 122 | ssize = mmu_kernel_ssize; |
| 123 | vsid = get_kernel_vsid(ea, mmu_kernel_ssize); |
| 124 | break; |
| 125 | default: |
| 126 | pr_debug("%s: invalid region access at %016llx\n", __func__, ea); |
| 127 | return 1; |
| 128 | } |
| 129 | |
| 130 | vsid = (vsid << slb_vsid_shift(ssize)) | SLB_VSID_USER; |
| 131 | |
| 132 | vsid |= mmu_psize_defs[psize].sllp | |
| 133 | ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0); |
| 134 | |
| 135 | slb->vsid = vsid; |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | EXPORT_SYMBOL_GPL(copro_calculate_slb); |
Ian Munsie | be3ebfe | 2014-10-08 19:54:52 +1100 | [diff] [blame^] | 140 | |
| 141 | void copro_flush_all_slbs(struct mm_struct *mm) |
| 142 | { |
| 143 | #ifdef CONFIG_SPU_BASE |
| 144 | spu_flush_all_slbs(mm); |
| 145 | #endif |
| 146 | } |
| 147 | EXPORT_SYMBOL_GPL(copro_flush_all_slbs); |