Thomas Gleixner | de6cc65 | 2019-05-27 08:55:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 2 | /* |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 3 | * CoProcessor (SPU/AFU) mm fault handler |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 4 | * |
| 5 | * (C) Copyright IBM Deutschland Entwicklung GmbH 2007 |
| 6 | * |
| 7 | * Author: Arnd Bergmann <arndb@de.ibm.com> |
| 8 | * Author: Jeremy Kerr <jk@ozlabs.org> |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 9 | */ |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/mm.h> |
Paul Gortmaker | 4b16f8e | 2011-07-22 18:24:23 -0400 | [diff] [blame] | 12 | #include <linux/export.h> |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 13 | #include <asm/reg.h> |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 14 | #include <asm/copro.h> |
Ian Munsie | be3ebfe | 2014-10-08 19:54:52 +1100 | [diff] [blame] | 15 | #include <asm/spu.h> |
Michael Neuling | ec249dd | 2015-05-27 16:07:16 +1000 | [diff] [blame] | 16 | #include <misc/cxl-base.h> |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 17 | |
| 18 | /* |
| 19 | * This ought to be kept in sync with the powerpc specific do_page_fault |
| 20 | * function. Currently, there are a few corner cases that we haven't had |
| 21 | * to handle fortunately. |
| 22 | */ |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 23 | int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea, |
Souptick Joarder | 50a7ca3 | 2018-08-17 15:44:47 -0700 | [diff] [blame] | 24 | unsigned long dsisr, vm_fault_t *flt) |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 25 | { |
| 26 | struct vm_area_struct *vma; |
| 27 | unsigned long is_write; |
| 28 | int ret; |
| 29 | |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 30 | if (mm == NULL) |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 31 | return -EFAULT; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 32 | |
| 33 | if (mm->pgd == NULL) |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 34 | return -EFAULT; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 35 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 36 | mmap_read_lock(mm); |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 37 | ret = -EFAULT; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 38 | vma = find_vma(mm, ea); |
| 39 | if (!vma) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 40 | goto out_unlock; |
| 41 | |
| 42 | if (ea < vma->vm_start) { |
| 43 | if (!(vma->vm_flags & VM_GROWSDOWN)) |
| 44 | goto out_unlock; |
| 45 | if (expand_stack(vma, ea)) |
| 46 | goto out_unlock; |
| 47 | } |
| 48 | |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 49 | is_write = dsisr & DSISR_ISSTORE; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 50 | if (is_write) { |
| 51 | if (!(vma->vm_flags & VM_WRITE)) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 52 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 53 | } else { |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 54 | if (!(vma->vm_flags & (VM_READ | VM_EXEC))) |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 55 | goto out_unlock; |
Mel Gorman | 842915f | 2015-02-12 14:58:25 -0800 | [diff] [blame] | 56 | /* |
Aneesh Kumar K.V | 18061c1 | 2017-01-30 21:42:59 +0530 | [diff] [blame] | 57 | * PROT_NONE is covered by the VMA check above. |
| 58 | * and hash should get a NOHPTE fault instead of |
| 59 | * a PROTFAULT in case fixup is needed for things |
| 60 | * like autonuma. |
Mel Gorman | 842915f | 2015-02-12 14:58:25 -0800 | [diff] [blame] | 61 | */ |
Aneesh Kumar K.V | 18061c1 | 2017-01-30 21:42:59 +0530 | [diff] [blame] | 62 | if (!radix_enabled()) |
| 63 | WARN_ON_ONCE(dsisr & DSISR_PROTFAULT); |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 64 | } |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 65 | |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 66 | ret = 0; |
Peter Xu | bce617e | 2020-08-11 18:37:44 -0700 | [diff] [blame^] | 67 | *flt = handle_mm_fault(vma, ea, is_write ? FAULT_FLAG_WRITE : 0, NULL); |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 68 | if (unlikely(*flt & VM_FAULT_ERROR)) { |
| 69 | if (*flt & VM_FAULT_OOM) { |
| 70 | ret = -ENOMEM; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 71 | goto out_unlock; |
Linus Torvalds | 33692f2 | 2015-01-29 10:51:32 -0800 | [diff] [blame] | 72 | } else if (*flt & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) { |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 73 | ret = -EFAULT; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 74 | goto out_unlock; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 75 | } |
| 76 | BUG(); |
| 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 | if (*flt & VM_FAULT_MAJOR) |
| 80 | current->maj_flt++; |
| 81 | else |
| 82 | current->min_flt++; |
Jeremy Kerr | 60ee0319 | 2009-02-17 11:44:14 +1100 | [diff] [blame] | 83 | |
| 84 | out_unlock: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 85 | mmap_read_unlock(mm); |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 86 | return ret; |
Jeremy Kerr | 7cd58e4 | 2007-12-20 16:39:59 +0900 | [diff] [blame] | 87 | } |
Ian Munsie | e83d016 | 2014-10-08 19:54:50 +1100 | [diff] [blame] | 88 | EXPORT_SYMBOL_GPL(copro_handle_mm_fault); |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 89 | |
| 90 | int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb) |
| 91 | { |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 92 | u64 vsid, vsidkey; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 93 | int psize, ssize; |
| 94 | |
Aneesh Kumar K.V | 0034d39 | 2019-04-17 18:29:14 +0530 | [diff] [blame] | 95 | switch (get_region_id(ea)) { |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 96 | case USER_REGION_ID: |
| 97 | pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea); |
Frederic Barrat | d2cf909 | 2016-06-17 18:53:28 +0200 | [diff] [blame] | 98 | if (mm == NULL) |
| 99 | return 1; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 100 | psize = get_slice_psize(mm, ea); |
| 101 | ssize = user_segment_size(ea); |
Aneesh Kumar K.V | f384796c | 2018-03-26 15:34:48 +0530 | [diff] [blame] | 102 | vsid = get_user_vsid(&mm->context, ea, ssize); |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 103 | vsidkey = SLB_VSID_USER; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 104 | break; |
| 105 | case VMALLOC_REGION_ID: |
| 106 | pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea); |
Aneesh Kumar K.V | 0034d39 | 2019-04-17 18:29:14 +0530 | [diff] [blame] | 107 | psize = mmu_vmalloc_psize; |
| 108 | ssize = mmu_kernel_ssize; |
| 109 | vsid = get_kernel_vsid(ea, mmu_kernel_ssize); |
| 110 | vsidkey = SLB_VSID_KERNEL; |
| 111 | break; |
| 112 | case IO_REGION_ID: |
| 113 | pr_devel("%s: 0x%llx -- IO_REGION_ID\n", __func__, ea); |
| 114 | psize = mmu_io_psize; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 115 | ssize = mmu_kernel_ssize; |
| 116 | vsid = get_kernel_vsid(ea, mmu_kernel_ssize); |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 117 | vsidkey = SLB_VSID_KERNEL; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 118 | break; |
Aneesh Kumar K.V | 5f53d28 | 2019-04-17 18:29:19 +0530 | [diff] [blame] | 119 | case LINEAR_MAP_REGION_ID: |
| 120 | pr_devel("%s: 0x%llx -- LINEAR_MAP_REGION_ID\n", __func__, ea); |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 121 | psize = mmu_linear_psize; |
| 122 | ssize = mmu_kernel_ssize; |
| 123 | vsid = get_kernel_vsid(ea, mmu_kernel_ssize); |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 124 | vsidkey = SLB_VSID_KERNEL; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 125 | break; |
| 126 | default: |
| 127 | pr_debug("%s: invalid region access at %016llx\n", __func__, ea); |
| 128 | return 1; |
| 129 | } |
Aneesh Kumar K.V | 64168f4 | 2016-11-15 20:36:06 +0530 | [diff] [blame] | 130 | /* Bad address */ |
| 131 | if (!vsid) |
| 132 | return 1; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 133 | |
Michael Neuling | 85a97da | 2015-05-27 16:06:55 +1000 | [diff] [blame] | 134 | vsid = (vsid << slb_vsid_shift(ssize)) | vsidkey; |
Ian Munsie | 73d16a6 | 2014-10-08 19:54:51 +1100 | [diff] [blame] | 135 | |
| 136 | vsid |= mmu_psize_defs[psize].sllp | |
| 137 | ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0); |
| 138 | |
Ian Munsie | 03f5439 | 2014-10-28 14:25:29 +1100 | [diff] [blame] | 139 | 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] | 140 | slb->vsid = vsid; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | EXPORT_SYMBOL_GPL(copro_calculate_slb); |
Ian Munsie | be3ebfe | 2014-10-08 19:54:52 +1100 | [diff] [blame] | 145 | |
| 146 | void copro_flush_all_slbs(struct mm_struct *mm) |
| 147 | { |
| 148 | #ifdef CONFIG_SPU_BASE |
| 149 | spu_flush_all_slbs(mm); |
| 150 | #endif |
Ian Munsie | 4c6d9ac | 2014-10-08 19:55:00 +1100 | [diff] [blame] | 151 | cxl_slbia(mm); |
Ian Munsie | be3ebfe | 2014-10-08 19:54:52 +1100 | [diff] [blame] | 152 | } |
| 153 | EXPORT_SYMBOL_GPL(copro_flush_all_slbs); |