powerpc/mm/hash64: Map all the kernel regions in the same 0xc range
[linux-2.6-block.git] / arch / powerpc / mm / copro_fault.c
CommitLineData
7cd58e43 1/*
e83d0169 2 * CoProcessor (SPU/AFU) mm fault handler
7cd58e43
JK
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>
4b16f8e2 25#include <linux/export.h>
e83d0169 26#include <asm/reg.h>
73d16a6e 27#include <asm/copro.h>
be3ebfe8 28#include <asm/spu.h>
ec249dd8 29#include <misc/cxl-base.h>
7cd58e43
JK
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 */
e83d0169 36int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea,
50a7ca3c 37 unsigned long dsisr, vm_fault_t *flt)
7cd58e43
JK
38{
39 struct vm_area_struct *vma;
40 unsigned long is_write;
41 int ret;
42
60ee0319 43 if (mm == NULL)
7cd58e43 44 return -EFAULT;
60ee0319
JK
45
46 if (mm->pgd == NULL)
7cd58e43 47 return -EFAULT;
7cd58e43
JK
48
49 down_read(&mm->mmap_sem);
60ee0319 50 ret = -EFAULT;
7cd58e43
JK
51 vma = find_vma(mm, ea);
52 if (!vma)
60ee0319
JK
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
e83d0169 62 is_write = dsisr & DSISR_ISSTORE;
7cd58e43
JK
63 if (is_write) {
64 if (!(vma->vm_flags & VM_WRITE))
60ee0319 65 goto out_unlock;
7cd58e43 66 } else {
7cd58e43 67 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
60ee0319 68 goto out_unlock;
842915f5 69 /*
18061c17
AK
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.
842915f5 74 */
18061c17
AK
75 if (!radix_enabled())
76 WARN_ON_ONCE(dsisr & DSISR_PROTFAULT);
7cd58e43 77 }
60ee0319 78
7cd58e43 79 ret = 0;
dcddffd4 80 *flt = handle_mm_fault(vma, ea, is_write ? FAULT_FLAG_WRITE : 0);
7cd58e43
JK
81 if (unlikely(*flt & VM_FAULT_ERROR)) {
82 if (*flt & VM_FAULT_OOM) {
83 ret = -ENOMEM;
60ee0319 84 goto out_unlock;
33692f27 85 } else if (*flt & (VM_FAULT_SIGBUS | VM_FAULT_SIGSEGV)) {
7cd58e43 86 ret = -EFAULT;
60ee0319 87 goto out_unlock;
7cd58e43
JK
88 }
89 BUG();
90 }
60ee0319 91
7cd58e43
JK
92 if (*flt & VM_FAULT_MAJOR)
93 current->maj_flt++;
94 else
95 current->min_flt++;
7cd58e43 96
60ee0319 97out_unlock:
7cd58e43 98 up_read(&mm->mmap_sem);
60ee0319 99 return ret;
7cd58e43 100}
e83d0169 101EXPORT_SYMBOL_GPL(copro_handle_mm_fault);
73d16a6e
IM
102
103int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb)
104{
85a97da9 105 u64 vsid, vsidkey;
73d16a6e
IM
106 int psize, ssize;
107
0034d395 108 switch (get_region_id(ea)) {
73d16a6e
IM
109 case USER_REGION_ID:
110 pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea);
d2cf909c
FB
111 if (mm == NULL)
112 return 1;
73d16a6e
IM
113 psize = get_slice_psize(mm, ea);
114 ssize = user_segment_size(ea);
f384796c 115 vsid = get_user_vsid(&mm->context, ea, ssize);
85a97da9 116 vsidkey = SLB_VSID_USER;
73d16a6e
IM
117 break;
118 case VMALLOC_REGION_ID:
119 pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea);
0034d395
AK
120 psize = mmu_vmalloc_psize;
121 ssize = mmu_kernel_ssize;
122 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
123 vsidkey = SLB_VSID_KERNEL;
124 break;
125 case IO_REGION_ID:
126 pr_devel("%s: 0x%llx -- IO_REGION_ID\n", __func__, ea);
127 psize = mmu_io_psize;
73d16a6e
IM
128 ssize = mmu_kernel_ssize;
129 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
85a97da9 130 vsidkey = SLB_VSID_KERNEL;
73d16a6e
IM
131 break;
132 case KERNEL_REGION_ID:
133 pr_devel("%s: 0x%llx -- KERNEL_REGION_ID\n", __func__, ea);
134 psize = mmu_linear_psize;
135 ssize = mmu_kernel_ssize;
136 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
85a97da9 137 vsidkey = SLB_VSID_KERNEL;
73d16a6e
IM
138 break;
139 default:
140 pr_debug("%s: invalid region access at %016llx\n", __func__, ea);
141 return 1;
142 }
64168f42
AK
143 /* Bad address */
144 if (!vsid)
145 return 1;
73d16a6e 146
85a97da9 147 vsid = (vsid << slb_vsid_shift(ssize)) | vsidkey;
73d16a6e
IM
148
149 vsid |= mmu_psize_defs[psize].sllp |
150 ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0);
151
03f54397 152 slb->esid = (ea & (ssize == MMU_SEGSIZE_1T ? ESID_MASK_1T : ESID_MASK)) | SLB_ESID_V;
73d16a6e
IM
153 slb->vsid = vsid;
154
155 return 0;
156}
157EXPORT_SYMBOL_GPL(copro_calculate_slb);
be3ebfe8
IM
158
159void copro_flush_all_slbs(struct mm_struct *mm)
160{
161#ifdef CONFIG_SPU_BASE
162 spu_flush_all_slbs(mm);
163#endif
4c6d9acc 164 cxl_slbia(mm);
be3ebfe8
IM
165}
166EXPORT_SYMBOL_GPL(copro_flush_all_slbs);