blob: a15a23efc0e29b07668c884247ee808cc75518d1 [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>
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090028
29/*
30 * This ought to be kept in sync with the powerpc specific do_page_fault
31 * function. Currently, there are a few corner cases that we haven't had
32 * to handle fortunately.
33 */
Ian Munsiee83d0162014-10-08 19:54:50 +110034int copro_handle_mm_fault(struct mm_struct *mm, unsigned long ea,
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090035 unsigned long dsisr, unsigned *flt)
36{
37 struct vm_area_struct *vma;
38 unsigned long is_write;
39 int ret;
40
Jeremy Kerr60ee03192009-02-17 11:44:14 +110041 if (mm == NULL)
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090042 return -EFAULT;
Jeremy Kerr60ee03192009-02-17 11:44:14 +110043
44 if (mm->pgd == NULL)
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090045 return -EFAULT;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090046
47 down_read(&mm->mmap_sem);
Jeremy Kerr60ee03192009-02-17 11:44:14 +110048 ret = -EFAULT;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090049 vma = find_vma(mm, ea);
50 if (!vma)
Jeremy Kerr60ee03192009-02-17 11:44:14 +110051 goto out_unlock;
52
53 if (ea < vma->vm_start) {
54 if (!(vma->vm_flags & VM_GROWSDOWN))
55 goto out_unlock;
56 if (expand_stack(vma, ea))
57 goto out_unlock;
58 }
59
Ian Munsiee83d0162014-10-08 19:54:50 +110060 is_write = dsisr & DSISR_ISSTORE;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090061 if (is_write) {
62 if (!(vma->vm_flags & VM_WRITE))
Jeremy Kerr60ee03192009-02-17 11:44:14 +110063 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090064 } else {
Ian Munsiee83d0162014-10-08 19:54:50 +110065 if (dsisr & DSISR_PROTFAULT)
Jeremy Kerr60ee03192009-02-17 11:44:14 +110066 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090067 if (!(vma->vm_flags & (VM_READ | VM_EXEC)))
Jeremy Kerr60ee03192009-02-17 11:44:14 +110068 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090069 }
Jeremy Kerr60ee03192009-02-17 11:44:14 +110070
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090071 ret = 0;
Linus Torvaldsd06063c2009-04-10 09:01:23 -070072 *flt = handle_mm_fault(mm, vma, ea, is_write ? FAULT_FLAG_WRITE : 0);
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090073 if (unlikely(*flt & VM_FAULT_ERROR)) {
74 if (*flt & VM_FAULT_OOM) {
75 ret = -ENOMEM;
Jeremy Kerr60ee03192009-02-17 11:44:14 +110076 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090077 } else if (*flt & VM_FAULT_SIGBUS) {
78 ret = -EFAULT;
Jeremy Kerr60ee03192009-02-17 11:44:14 +110079 goto out_unlock;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090080 }
81 BUG();
82 }
Jeremy Kerr60ee03192009-02-17 11:44:14 +110083
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090084 if (*flt & VM_FAULT_MAJOR)
85 current->maj_flt++;
86 else
87 current->min_flt++;
Jeremy Kerr60ee03192009-02-17 11:44:14 +110088
89out_unlock:
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090090 up_read(&mm->mmap_sem);
91 return ret;
Jeremy Kerr7cd58e42007-12-20 16:39:59 +090092}
Ian Munsiee83d0162014-10-08 19:54:50 +110093EXPORT_SYMBOL_GPL(copro_handle_mm_fault);
Ian Munsie73d16a62014-10-08 19:54:51 +110094
95int copro_calculate_slb(struct mm_struct *mm, u64 ea, struct copro_slb *slb)
96{
97 u64 vsid;
98 int psize, ssize;
99
100 slb->esid = (ea & ESID_MASK) | SLB_ESID_V;
101
102 switch (REGION_ID(ea)) {
103 case USER_REGION_ID:
104 pr_devel("%s: 0x%llx -- USER_REGION_ID\n", __func__, ea);
105 psize = get_slice_psize(mm, ea);
106 ssize = user_segment_size(ea);
107 vsid = get_vsid(mm->context.id, ea, ssize);
108 break;
109 case VMALLOC_REGION_ID:
110 pr_devel("%s: 0x%llx -- VMALLOC_REGION_ID\n", __func__, ea);
111 if (ea < VMALLOC_END)
112 psize = mmu_vmalloc_psize;
113 else
114 psize = mmu_io_psize;
115 ssize = mmu_kernel_ssize;
116 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
117 break;
118 case KERNEL_REGION_ID:
119 pr_devel("%s: 0x%llx -- KERNEL_REGION_ID\n", __func__, ea);
120 psize = mmu_linear_psize;
121 ssize = mmu_kernel_ssize;
122 vsid = get_kernel_vsid(ea, mmu_kernel_ssize);
123 break;
124 default:
125 pr_debug("%s: invalid region access at %016llx\n", __func__, ea);
126 return 1;
127 }
128
129 vsid = (vsid << slb_vsid_shift(ssize)) | SLB_VSID_USER;
130
131 vsid |= mmu_psize_defs[psize].sllp |
132 ((ssize == MMU_SEGSIZE_1T) ? SLB_VSID_B_1T : 0);
133
134 slb->vsid = vsid;
135
136 return 0;
137}
138EXPORT_SYMBOL_GPL(copro_calculate_slb);