blob: e6f8f05446bef5569f0cde3e9a9f6fdcaba144c6 [file] [log] [blame]
Ian Munsief204e0b2014-10-08 19:55:02 +11001/*
2 * Copyright 2014 IBM Corp.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 */
9
10#include <linux/workqueue.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010011#include <linux/sched/signal.h>
Ingo Molnar6e84f312017-02-08 18:51:29 +010012#include <linux/sched/mm.h>
Ian Munsief204e0b2014-10-08 19:55:02 +110013#include <linux/pid.h>
14#include <linux/mm.h>
15#include <linux/moduleparam.h>
16
17#undef MODULE_PARAM_PREFIX
18#define MODULE_PARAM_PREFIX "cxl" "."
19#include <asm/current.h>
20#include <asm/copro.h>
21#include <asm/mmu.h>
22
23#include "cxl.h"
Ian Munsie9bcf28c2015-01-09 20:34:36 +110024#include "trace.h"
Ian Munsief204e0b2014-10-08 19:55:02 +110025
Ian Munsieeb01d4c2014-10-28 14:25:30 +110026static bool sste_matches(struct cxl_sste *sste, struct copro_slb *slb)
27{
28 return ((sste->vsid_data == cpu_to_be64(slb->vsid)) &&
29 (sste->esid_data == cpu_to_be64(slb->esid)));
30}
31
32/*
33 * This finds a free SSTE for the given SLB, or returns NULL if it's already in
34 * the segment table.
35 */
Ian Munsieb03a7f52014-10-28 14:25:28 +110036static struct cxl_sste* find_free_sste(struct cxl_context *ctx,
37 struct copro_slb *slb)
Ian Munsief204e0b2014-10-08 19:55:02 +110038{
Ian Munsieeb01d4c2014-10-28 14:25:30 +110039 struct cxl_sste *primary, *sste, *ret = NULL;
Ian Munsieb03a7f52014-10-28 14:25:28 +110040 unsigned int mask = (ctx->sst_size >> 7) - 1; /* SSTP0[SegTableSize] */
Ian Munsie5100a9d2014-10-28 14:25:27 +110041 unsigned int entry;
Ian Munsieb03a7f52014-10-28 14:25:28 +110042 unsigned int hash;
Ian Munsief204e0b2014-10-08 19:55:02 +110043
Ian Munsieb03a7f52014-10-28 14:25:28 +110044 if (slb->vsid & SLB_VSID_B_1T)
45 hash = (slb->esid >> SID_SHIFT_1T) & mask;
46 else /* 256M */
47 hash = (slb->esid >> SID_SHIFT) & mask;
48
49 primary = ctx->sstp + (hash << 3);
50
51 for (entry = 0, sste = primary; entry < 8; entry++, sste++) {
Ian Munsieeb01d4c2014-10-28 14:25:30 +110052 if (!ret && !(be64_to_cpu(sste->esid_data) & SLB_ESID_V))
53 ret = sste;
54 if (sste_matches(sste, slb))
55 return NULL;
Ian Munsief204e0b2014-10-08 19:55:02 +110056 }
Ian Munsieeb01d4c2014-10-28 14:25:30 +110057 if (ret)
58 return ret;
Ian Munsieb03a7f52014-10-28 14:25:28 +110059
Ian Munsief204e0b2014-10-08 19:55:02 +110060 /* Nothing free, select an entry to cast out */
Ian Munsieeb01d4c2014-10-28 14:25:30 +110061 ret = primary + ctx->sst_lru;
Ian Munsieb03a7f52014-10-28 14:25:28 +110062 ctx->sst_lru = (ctx->sst_lru + 1) & 0x7;
Ian Munsief204e0b2014-10-08 19:55:02 +110063
Ian Munsieeb01d4c2014-10-28 14:25:30 +110064 return ret;
Ian Munsief204e0b2014-10-08 19:55:02 +110065}
66
67static void cxl_load_segment(struct cxl_context *ctx, struct copro_slb *slb)
68{
69 /* mask is the group index, we search primary and secondary here. */
Ian Munsief204e0b2014-10-08 19:55:02 +110070 struct cxl_sste *sste;
Ian Munsief204e0b2014-10-08 19:55:02 +110071 unsigned long flags;
72
Ian Munsief204e0b2014-10-08 19:55:02 +110073 spin_lock_irqsave(&ctx->sste_lock, flags);
Ian Munsieb03a7f52014-10-28 14:25:28 +110074 sste = find_free_sste(ctx, slb);
Ian Munsieeb01d4c2014-10-28 14:25:30 +110075 if (!sste)
76 goto out_unlock;
Ian Munsief204e0b2014-10-08 19:55:02 +110077
78 pr_devel("CXL Populating SST[%li]: %#llx %#llx\n",
79 sste - ctx->sstp, slb->vsid, slb->esid);
Ian Munsie9bcf28c2015-01-09 20:34:36 +110080 trace_cxl_ste_write(ctx, sste - ctx->sstp, slb->esid, slb->vsid);
Ian Munsief204e0b2014-10-08 19:55:02 +110081
82 sste->vsid_data = cpu_to_be64(slb->vsid);
83 sste->esid_data = cpu_to_be64(slb->esid);
Ian Munsieeb01d4c2014-10-28 14:25:30 +110084out_unlock:
Ian Munsief204e0b2014-10-08 19:55:02 +110085 spin_unlock_irqrestore(&ctx->sste_lock, flags);
86}
87
88static int cxl_fault_segment(struct cxl_context *ctx, struct mm_struct *mm,
89 u64 ea)
90{
91 struct copro_slb slb = {0,0};
92 int rc;
93
94 if (!(rc = copro_calculate_slb(mm, ea, &slb))) {
95 cxl_load_segment(ctx, &slb);
96 }
97
98 return rc;
99}
100
101static void cxl_ack_ae(struct cxl_context *ctx)
102{
103 unsigned long flags;
104
Frederic Barrat5be587b2016-03-04 12:26:28 +0100105 cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_AE, 0);
Ian Munsief204e0b2014-10-08 19:55:02 +1100106
107 spin_lock_irqsave(&ctx->lock, flags);
108 ctx->pending_fault = true;
109 ctx->fault_addr = ctx->dar;
110 ctx->fault_dsisr = ctx->dsisr;
111 spin_unlock_irqrestore(&ctx->lock, flags);
112
113 wake_up_all(&ctx->wq);
114}
115
116static int cxl_handle_segment_miss(struct cxl_context *ctx,
117 struct mm_struct *mm, u64 ea)
118{
119 int rc;
120
121 pr_devel("CXL interrupt: Segment fault pe: %i ea: %#llx\n", ctx->pe, ea);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100122 trace_cxl_ste_miss(ctx, ea);
Ian Munsief204e0b2014-10-08 19:55:02 +1100123
124 if ((rc = cxl_fault_segment(ctx, mm, ea)))
125 cxl_ack_ae(ctx);
126 else {
127
128 mb(); /* Order seg table write to TFC MMIO write */
Frederic Barrat5be587b2016-03-04 12:26:28 +0100129 cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
Ian Munsief204e0b2014-10-08 19:55:02 +1100130 }
131
132 return IRQ_HANDLED;
133}
134
135static void cxl_handle_page_fault(struct cxl_context *ctx,
136 struct mm_struct *mm, u64 dsisr, u64 dar)
137{
138 unsigned flt = 0;
139 int result;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530140 unsigned long access, flags, inv_flags = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100141
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100142 trace_cxl_pte_miss(ctx, dsisr, dar);
143
Ian Munsief204e0b2014-10-08 19:55:02 +1100144 if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
145 pr_devel("copro_handle_mm_fault failed: %#x\n", result);
146 return cxl_ack_ae(ctx);
147 }
148
149 /*
150 * update_mmu_cache() will not have loaded the hash since current->trap
151 * is not a 0x400 or 0x300, so just call hash_page_mm() here.
152 */
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +1000153 access = _PAGE_PRESENT | _PAGE_READ;
Ian Munsief204e0b2014-10-08 19:55:02 +1100154 if (dsisr & CXL_PSL_DSISR_An_S)
Aneesh Kumar K.Vc7d54842016-04-29 23:25:30 +1000155 access |= _PAGE_WRITE;
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +1000156
157 access |= _PAGE_PRIVILEGED;
Aneesh Kumar K.V3b1dbfa2016-04-20 03:59:47 -0400158 if ((!ctx->kernel) || (REGION_ID(dar) == USER_REGION_ID))
Aneesh Kumar K.Vac29c642016-04-29 23:25:34 +1000159 access &= ~_PAGE_PRIVILEGED;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530160
161 if (dsisr & DSISR_NOHPTE)
162 inv_flags |= HPTE_NOHPTE_UPDATE;
163
Ian Munsief204e0b2014-10-08 19:55:02 +1100164 local_irq_save(flags);
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530165 hash_page_mm(mm, dar, access, 0x300, inv_flags);
Ian Munsief204e0b2014-10-08 19:55:02 +1100166 local_irq_restore(flags);
167
168 pr_devel("Page fault successfully handled for pe: %i!\n", ctx->pe);
Frederic Barrat5be587b2016-03-04 12:26:28 +0100169 cxl_ops->ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
Ian Munsief204e0b2014-10-08 19:55:02 +1100170}
171
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530172/*
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200173 * Returns the mm_struct corresponding to the context ctx.
174 * mm_users == 0, the context may be in the process of being closed.
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530175 */
176static struct mm_struct *get_mem_context(struct cxl_context *ctx)
177{
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200178 if (ctx->mm == NULL)
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530179 return NULL;
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530180
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200181 if (!atomic_inc_not_zero(&ctx->mm->mm_users))
182 return NULL;
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530183
Christophe Lombard6dd2d232017-04-07 16:11:55 +0200184 return ctx->mm;
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530185}
186
187
188
Ian Munsief204e0b2014-10-08 19:55:02 +1100189void cxl_handle_fault(struct work_struct *fault_work)
190{
191 struct cxl_context *ctx =
192 container_of(fault_work, struct cxl_context, fault_work);
193 u64 dsisr = ctx->dsisr;
194 u64 dar = ctx->dar;
Michael Neulinga6b07d82015-05-27 16:07:11 +1000195 struct mm_struct *mm = NULL;
Ian Munsief204e0b2014-10-08 19:55:02 +1100196
Frederic Barratea2d1f92016-03-04 12:26:30 +0100197 if (cpu_has_feature(CPU_FTR_HVMODE)) {
198 if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
199 cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
200 cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
201 /* Most likely explanation is harmless - a dedicated
202 * process has detached and these were cleared by the
203 * PSL purge, but warn about it just in case
204 */
205 dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
206 return;
207 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100208 }
209
Ian Munsie13da7042014-12-08 19:17:58 +1100210 /* Early return if the context is being / has been detached */
211 if (ctx->status == CLOSED) {
212 cxl_ack_ae(ctx);
213 return;
214 }
215
Ian Munsief204e0b2014-10-08 19:55:02 +1100216 pr_devel("CXL BOTTOM HALF handling fault for afu pe: %i. "
217 "DSISR: %#llx DAR: %#llx\n", ctx->pe, dsisr, dar);
218
Michael Neulinga6b07d82015-05-27 16:07:11 +1000219 if (!ctx->kernel) {
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530220
221 mm = get_mem_context(ctx);
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530222 if (mm == NULL) {
223 pr_devel("%s: unable to get mm for pe=%d pid=%i\n",
224 __func__, ctx->pe, pid_nr(ctx->pid));
Michael Neulinga6b07d82015-05-27 16:07:11 +1000225 cxl_ack_ae(ctx);
226 return;
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530227 } else {
228 pr_devel("Handling page fault for pe=%d pid=%i\n",
229 ctx->pe, pid_nr(ctx->pid));
Michael Neulinga6b07d82015-05-27 16:07:11 +1000230 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100231 }
232
233 if (dsisr & CXL_PSL_DSISR_An_DS)
234 cxl_handle_segment_miss(ctx, mm, dar);
235 else if (dsisr & CXL_PSL_DSISR_An_DM)
236 cxl_handle_page_fault(ctx, mm, dsisr, dar);
237 else
238 WARN(1, "cxl_handle_fault has nothing to handle\n");
239
Michael Neulinga6b07d82015-05-27 16:07:11 +1000240 if (mm)
241 mmput(mm);
Ian Munsief204e0b2014-10-08 19:55:02 +1100242}
243
244static void cxl_prefault_one(struct cxl_context *ctx, u64 ea)
245{
Ian Munsief204e0b2014-10-08 19:55:02 +1100246 struct mm_struct *mm;
247
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530248 mm = get_mem_context(ctx);
249 if (mm == NULL) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100250 pr_devel("cxl_prefault_one unable to get mm %i\n",
251 pid_nr(ctx->pid));
Ian Munsief204e0b2014-10-08 19:55:02 +1100252 return;
253 }
254
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530255 cxl_fault_segment(ctx, mm, ea);
Ian Munsief204e0b2014-10-08 19:55:02 +1100256
257 mmput(mm);
Ian Munsief204e0b2014-10-08 19:55:02 +1100258}
259
260static u64 next_segment(u64 ea, u64 vsid)
261{
262 if (vsid & SLB_VSID_B_1T)
263 ea |= (1ULL << 40) - 1;
264 else
265 ea |= (1ULL << 28) - 1;
266
267 return ea + 1;
268}
269
270static void cxl_prefault_vma(struct cxl_context *ctx)
271{
272 u64 ea, last_esid = 0;
273 struct copro_slb slb;
274 struct vm_area_struct *vma;
275 int rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100276 struct mm_struct *mm;
277
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530278 mm = get_mem_context(ctx);
279 if (mm == NULL) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100280 pr_devel("cxl_prefault_vm unable to get mm %i\n",
281 pid_nr(ctx->pid));
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530282 return;
Ian Munsief204e0b2014-10-08 19:55:02 +1100283 }
284
285 down_read(&mm->mmap_sem);
286 for (vma = mm->mmap; vma; vma = vma->vm_next) {
287 for (ea = vma->vm_start; ea < vma->vm_end;
288 ea = next_segment(ea, slb.vsid)) {
289 rc = copro_calculate_slb(mm, ea, &slb);
290 if (rc)
291 continue;
292
293 if (last_esid == slb.esid)
294 continue;
295
296 cxl_load_segment(ctx, &slb);
297 last_esid = slb.esid;
298 }
299 }
300 up_read(&mm->mmap_sem);
301
302 mmput(mm);
Ian Munsief204e0b2014-10-08 19:55:02 +1100303}
304
305void cxl_prefault(struct cxl_context *ctx, u64 wed)
306{
307 switch (ctx->afu->prefault_mode) {
308 case CXL_PREFAULT_WED:
309 cxl_prefault_one(ctx, wed);
310 break;
311 case CXL_PREFAULT_ALL:
312 cxl_prefault_vma(ctx);
313 break;
314 default:
315 break;
316 }
317}