blob: 0f9939e693df6b8422a7b87301f7dcd250816c34 [file] [log] [blame]
Jeremy Kerr7cd58e42007-12-20 16:39:59 +09001/*
Ian Munsiee83d0162014-10-08 19:54:50 +11002 * CoProcessor (SPU/AFU) mm fault handler
Jeremy Kerr7cd58e42007-12-20 16:39:59 +09003 *
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 Gortmaker4b16f8e2011-07-22 18:24:23 -040025#include <linux/export.h>
Ian Munsiee83d0162014-10-08 19:54:50 +110026#include <asm/reg.h>
Ian Munsie73d16a62014-10-08 19:54:51 +110027#include <asm/copro.h>
Ian Munsiebe3ebfe2014-10-08 19:54:52 +110028#include <asm/spu.h>
Ian Munsie4c6d9ac2014-10-08 19:55:00 +110029#include <misc/cxl.h>
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090030
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 Munsiee83d0162014-10-08 19:54:50 +110036int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea,
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090037 unsigned long dsisr, unsigned *flt)
38{
39 struct vm_area_struct *vma;
40 unsigned long is_write;
41 int ret;
42
Jeremy Kerr60ee0312009-02-17 11:44:14 +110043 if (mm == NULL)
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090044 return -EFAULT;
Jeremy Kerr60ee0312009-02-17 11:44:14 +110045
46 if (mm->pgd == NULL)
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090047 return -EFAULT;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090048
49 down_read(&mm->mmap_sem);
Jeremy Kerr60ee0312009-02-17 11:44:14 +110050 ret = -EFAULT;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090051 vma = find_vma(mm, ea);
52 if (!vma)
Jeremy Kerr60ee0312009-02-17 11:44:14 +110053 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 Munsiee83d0162014-10-08 19:54:50 +110062 is_write = dsisr & DSISR_ISSTORE;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090063 if (is_write) {
64 if (!(vma->vm_flags & VM_WRITE))
Jeremy Kerr60ee0312009-02-17 11:44:14 +110065 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090066 } else {
Ian Munsiee83d0162014-10-08 19:54:50 +110067 if (dsisr & DSISR_PROTFAULT)
Jeremy Kerr60ee0312009-02-17 11:44:14 +110068 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090069 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
Jeremy Kerr60ee0312009-02-17 11:44:14 +110070 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090071 }
Jeremy Kerr60ee0312009-02-17 11:44:14 +110072
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090073 ret = 0;
Linus Torvaldsd06063c2009-04-10 09:01:23 -070074 *flt = handle_mm_fault(mm, vma, ea, is_write ? FAULT_FLAG_WRITE : 0);
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090075 if (unlikely(*flt & VM_FAULT_ERROR)) {
76 if (*flt & VM_FAULT_OOM) {
77 ret = -ENOMEM;
Jeremy Kerr60ee0312009-02-17 11:44:14 +110078 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090079 } else if (*flt & VM_FAULT_SIGBUS) {
80 ret = -EFAULT;
Jeremy Kerr60ee0312009-02-17 11:44:14 +110081 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090082 }
83 BUG();
84 }
Jeremy Kerr60ee0312009-02-17 11:44:14 +110085
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090086 if (*flt & VM_FAULT_MAJOR)
87 current->maj_flt++;
88 else
89 current->min_flt++;
Jeremy Kerr60ee0312009-02-17 11:44:14 +110090
91out_unlock:
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090092 up_read(&mm->mmap_sem);
93 return ret;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090094}
Ian Munsiee83d0162014-10-08 19:54:50 +110095EXPORT_SYMBOL_GPL(copro_handle_mm_fault);
Ian Munsie73d16a62014-10-08 19:54:51 +110096
97int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb)
98{
99 u64 vsid;
100 int psize, ssize;
101
102 slb->esid = (ea & ESID_MASK) | SLB_ESID_V;
103
104 switch (REGION_ID(ea)) {
105 case USER_REGION_ID:
106 pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea);
107 psize = get_slice_psize(mm, ea);
108 ssize = user_segment_size(ea);
109 vsid = get_vsid(mm->context.id, ea, ssize);
110 break;
111 case VMALLOC_REGION_ID:
112 pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea);
113 if (ea < VMALLOC_END)
114 psize = mmu_vmalloc_psize;
115 else
116 psize = mmu_io_psize;
117 ssize = mmu_kernel_ssize;
118 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
119 break;
120 case KERNEL_REGION_ID:
121 pr_devel("%s: 0x%llx -- KERNEL_REGION_ID\n", __func__, ea);
122 psize = mmu_linear_psize;
123 ssize = mmu_kernel_ssize;
124 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
125 break;
126 default:
127 pr_debug("%s: invalid region access at %016llx\n", __func__, ea);
128 return 1;
129 }
130
131 vsid = (vsid << slb_vsid_shift(ssize)) | SLB_VSID_USER;
132
133 vsid |= mmu_psize_defs[psize].sllp |
134 ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0);
135
136 slb->vsid = vsid;
137
138 return 0;
139}
140EXPORT_SYMBOL_GPL(copro_calculate_slb);
Ian Munsiebe3ebfe2014-10-08 19:54:52 +1100141
142void copro_flush_all_slbs(struct mm_struct *mm)
143{
144#ifdef CONFIG_SPU_BASE
145 spu_flush_all_slbs(mm);
146#endif
Ian Munsie4c6d9ac2014-10-08 19:55:00 +1100147 cxl_slbia(mm);
Ian Munsiebe3ebfe2014-10-08 19:54:52 +1100148}
149EXPORT_SYMBOL_GPL(copro_flush_all_slbs);