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> |
Michael Neuling | ec249dd | 2015-05-27 16:07:16 +1000 | [diff] [blame] | 29 | #include <misc/cxl-base.h> |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * This ought to be kept in sync with the powerpc specific do_page_fault |
| 33 | * function. Currently, there are a few corner cases that we haven't had |
| 34 | * to handle fortunately. |
| 35 | */ |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 36 | int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea, |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 37 | unsigned long dsisr, unsigned *flt) |
| 38 | { |
| 39 | struct vm_area_struct *vma; |
| 40 | unsigned long is_write; |
| 41 | int ret; |
| 42 | |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 43 | if (mm == NULL) |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 44 | return -EFAULT; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 45 | |
| 46 | if (mm->pgd == NULL) |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 47 | return -EFAULT; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 48 | |
| 49 | down_read(&mm->mmap_sem); |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 50 | ret = -EFAULT; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 51 | vma = find_vma(mm, ea); |
| 52 | if (!vma) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 53 | goto out_unlock; |
| 54 | |
| 55 | if (ea < vma->vm_start) { |
| 56 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 57 | goto out_unlock; |
| 58 | if (expand_stack(vma, ea)) |
| 59 | goto out_unlock; |
| 60 | } |
| 61 | |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 62 | is_write = dsisr & DSISR_ISSTORE; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 63 | if (is_write) { |
| 64 | if (!(vma->vm_flags & VM_WRITE)) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 65 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 66 | } else { |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 67 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 68 | goto out_unlock; |
Mel Gorman | 842915f | 2015-02-12 14:58:25 -0800 | [diff] [blame] | 69 | /* |
Aneesh Kumar K.V | 18061c1 | 2017-01-30 21:42:59 +0530 | [diff] [blame] | 70 | * PROT_NONE is covered by the VMA check above. |
| 71 | * and hash should get a NOHPTE fault instead of |
| 72 | * a PROTFAULT in case fixup is needed for things |
| 73 | * like autonuma. |
Mel Gorman | 842915f | 2015-02-12 14:58:25 -0800 | [diff] [blame] | 74 | */ |
Aneesh Kumar K.V | 18061c1 | 2017-01-30 21:42:59 +0530 | [diff] [blame] | 75 | if (!radix_enabled()) |
| 76 | WARN_ON_ONCE(dsisr & DSISR_PROTFAULT); |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 77 | } |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 78 | |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 79 | ret = 0; |
Kirill A. Shutemov | dcddffd | 2016-07-26 15:25:18 -0700 | [diff] [blame] | 80 | *flt = handle_mm_fault(vma, ea, is_write ? FAULT_FLAG_WRITE : 0); |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 81 | if (unlikely(*flt & VM_FAULT_ERROR)) { |
| 82 | if (*flt & VM_FAULT_OOM) { |
| 83 | ret = -ENOMEM; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 84 | goto out_unlock; |
Linus Torvalds | 33692f2 | 2015-01-29 10:51:32 -0800 | [diff] [blame] | 85 | } else if (*flt & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) { |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 86 | ret = -EFAULT; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 87 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 88 | } |
| 89 | BUG(); |
| 90 | } |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 91 | |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 92 | if (*flt & VM_FAULT_MAJOR) |
| 93 | current->maj_flt++; |
| 94 | else |
| 95 | current->min_flt++; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 96 | |
| 97 | out_unlock: |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 98 | up_read(&mm->mmap_sem); |
| 99 | return ret; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 100 | } |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 101 | EXPORT_SYMBOL_GPL(copro_handle_mm_fault); |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 102 | |
| 103 | int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb) |
| 104 | { |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 105 | u64 vsid, vsidkey; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 106 | int psize, ssize; |
| 107 | |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 108 | switch (REGION_ID(ea)) { |
| 109 | case USER_REGION_ID: |
| 110 | pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea); |
Frederic Barrat | d2cf909 | 2016-06-17 18:53:28 +0200 | [diff] [blame] | 111 | if (mm == NULL) |
| 112 | return 1; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 113 | psize = get_slice_psize(mm, ea); |
| 114 | ssize = user_segment_size(ea); |
| 115 | vsid = get_vsid(mm->context.id, ea, ssize); |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 116 | vsidkey = SLB_VSID_USER; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 117 | break; |
| 118 | case VMALLOC_REGION_ID: |
| 119 | pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea); |
| 120 | if (ea < VMALLOC_END) |
| 121 | psize = mmu_vmalloc_psize; |
| 122 | else |
| 123 | psize = mmu_io_psize; |
| 124 | ssize = mmu_kernel_ssize; |
| 125 | vsid = get_kernel_vsid(ea, mmu_kernel_ssize); |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 126 | vsidkey = SLB_VSID_KERNEL; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 127 | break; |
| 128 | case KERNEL_REGION_ID: |
| 129 | pr_devel("%s: 0x%llx -- KERNEL_REGION_ID\n", __func__, ea); |
| 130 | psize = mmu_linear_psize; |
| 131 | ssize = mmu_kernel_ssize; |
| 132 | vsid = get_kernel_vsid(ea, mmu_kernel_ssize); |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 133 | vsidkey = SLB_VSID_KERNEL; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 134 | break; |
| 135 | default: |
| 136 | pr_debug("%s: invalid region access at %016llx\n", __func__, ea); |
| 137 | return 1; |
| 138 | } |
Aneesh Kumar K.V | 64168f4 | 2016-11-15 20:36:06 +0530 | [diff] [blame] | 139 | /* Bad address */ |
| 140 | if (!vsid) |
| 141 | return 1; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 142 | |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 143 | vsid = (vsid << slb_vsid_shift(ssize)) | vsidkey; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 144 | |
| 145 | vsid |= mmu_psize_defs[psize].sllp | |
| 146 | ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0); |
| 147 | |
Ian Munsie | 03f5439 | 2014-10-28 14:25:29 +1100 | [diff] [blame] | 148 | slb->esid = (ea & (ssize == MMU_SEGSIZE_1T ? ESID_MASK_1T : ESID_MASK)) | SLB_ESID_V; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 149 | slb->vsid = vsid; |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | EXPORT_SYMBOL_GPL(copro_calculate_slb); |
Ian Munsie | be3ebfe | 2014-10-08 19:54:52 +1100 | [diff] [blame] | 154 | |
| 155 | void copro_flush_all_slbs(struct mm_struct *mm) |
| 156 | { |
| 157 | #ifdef CONFIG_SPU_BASE |
| 158 | spu_flush_all_slbs(mm); |
| 159 | #endif |
Ian Munsie | 4c6d9ac | 2014-10-08 19:55:00 +1100 | [diff] [blame] | 160 | cxl_slbia(mm); |
Ian Munsie | be3ebfe | 2014-10-08 19:54:52 +1100 | [diff] [blame] | 161 | } |
| 162 | EXPORT_SYMBOL_GPL(copro_flush_all_slbs); |