blob: 81c3f75b73308c08e578aa8948930565ae1944d0 [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>
11#include <linux/sched.h>
12#include <linux/pid.h>
13#include <linux/mm.h>
14#include <linux/moduleparam.h>
15
16#undef MODULE_PARAM_PREFIX
17#define MODULE_PARAM_PREFIX "cxl" "."
18#include <asm/current.h>
19#include <asm/copro.h>
20#include <asm/mmu.h>
21
22#include "cxl.h"
Ian Munsie9bcf28c2015-01-09 20:34:36 +110023#include "trace.h"
Ian Munsief204e0b2014-10-08 19:55:02 +110024
Ian Munsieeb01d4c2014-10-28 14:25:30 +110025static bool sste_matches(struct cxl_sste *sste, struct copro_slb *slb)
26{
27 return ((sste->vsid_data == cpu_to_be64(slb->vsid)) &&
28 (sste->esid_data == cpu_to_be64(slb->esid)));
29}
30
31/*
32 * This finds a free SSTE for the given SLB, or returns NULL if it's already in
33 * the segment table.
34 */
Ian Munsieb03a7f52014-10-28 14:25:28 +110035static struct cxl_sste* find_free_sste(struct cxl_context *ctx,
36 struct copro_slb *slb)
Ian Munsief204e0b2014-10-08 19:55:02 +110037{
Ian Munsieeb01d4c2014-10-28 14:25:30 +110038 struct cxl_sste *primary, *sste, *ret = NULL;
Ian Munsieb03a7f52014-10-28 14:25:28 +110039 unsigned int mask = (ctx->sst_size >> 7) - 1; /* SSTP0[SegTableSize] */
Ian Munsie5100a9d2014-10-28 14:25:27 +110040 unsigned int entry;
Ian Munsieb03a7f52014-10-28 14:25:28 +110041 unsigned int hash;
Ian Munsief204e0b2014-10-08 19:55:02 +110042
Ian Munsieb03a7f52014-10-28 14:25:28 +110043 if (slb->vsid & SLB_VSID_B_1T)
44 hash = (slb->esid >> SID_SHIFT_1T) & mask;
45 else /* 256M */
46 hash = (slb->esid >> SID_SHIFT) & mask;
47
48 primary = ctx->sstp + (hash << 3);
49
50 for (entry = 0, sste = primary; entry < 8; entry++, sste++) {
Ian Munsieeb01d4c2014-10-28 14:25:30 +110051 if (!ret && !(be64_to_cpu(sste->esid_data) & SLB_ESID_V))
52 ret = sste;
53 if (sste_matches(sste, slb))
54 return NULL;
Ian Munsief204e0b2014-10-08 19:55:02 +110055 }
Ian Munsieeb01d4c2014-10-28 14:25:30 +110056 if (ret)
57 return ret;
Ian Munsieb03a7f52014-10-28 14:25:28 +110058
Ian Munsief204e0b2014-10-08 19:55:02 +110059 /* Nothing free, select an entry to cast out */
Ian Munsieeb01d4c2014-10-28 14:25:30 +110060 ret = primary + ctx->sst_lru;
Ian Munsieb03a7f52014-10-28 14:25:28 +110061 ctx->sst_lru = (ctx->sst_lru + 1) & 0x7;
Ian Munsief204e0b2014-10-08 19:55:02 +110062
Ian Munsieeb01d4c2014-10-28 14:25:30 +110063 return ret;
Ian Munsief204e0b2014-10-08 19:55:02 +110064}
65
66static void cxl_load_segment(struct cxl_context *ctx, struct copro_slb *slb)
67{
68 /* mask is the group index, we search primary and secondary here. */
Ian Munsief204e0b2014-10-08 19:55:02 +110069 struct cxl_sste *sste;
Ian Munsief204e0b2014-10-08 19:55:02 +110070 unsigned long flags;
71
Ian Munsief204e0b2014-10-08 19:55:02 +110072 spin_lock_irqsave(&ctx->sste_lock, flags);
Ian Munsieb03a7f52014-10-28 14:25:28 +110073 sste = find_free_sste(ctx, slb);
Ian Munsieeb01d4c2014-10-28 14:25:30 +110074 if (!sste)
75 goto out_unlock;
Ian Munsief204e0b2014-10-08 19:55:02 +110076
77 pr_devel("CXL Populating SST[%li]: %#llx %#llx\n",
78 sste - ctx->sstp, slb->vsid, slb->esid);
Ian Munsie9bcf28c2015-01-09 20:34:36 +110079 trace_cxl_ste_write(ctx, sste - ctx->sstp, slb->esid, slb->vsid);
Ian Munsief204e0b2014-10-08 19:55:02 +110080
81 sste->vsid_data = cpu_to_be64(slb->vsid);
82 sste->esid_data = cpu_to_be64(slb->esid);
Ian Munsieeb01d4c2014-10-28 14:25:30 +110083out_unlock:
Ian Munsief204e0b2014-10-08 19:55:02 +110084 spin_unlock_irqrestore(&ctx->sste_lock, flags);
85}
86
87static int cxl_fault_segment(struct cxl_context *ctx, struct mm_struct *mm,
88 u64 ea)
89{
90 struct copro_slb slb = {0,0};
91 int rc;
92
93 if (!(rc = copro_calculate_slb(mm, ea, &slb))) {
94 cxl_load_segment(ctx, &slb);
95 }
96
97 return rc;
98}
99
100static void cxl_ack_ae(struct cxl_context *ctx)
101{
102 unsigned long flags;
103
104 cxl_ack_irq(ctx, CXL_PSL_TFC_An_AE, 0);
105
106 spin_lock_irqsave(&ctx->lock, flags);
107 ctx->pending_fault = true;
108 ctx->fault_addr = ctx->dar;
109 ctx->fault_dsisr = ctx->dsisr;
110 spin_unlock_irqrestore(&ctx->lock, flags);
111
112 wake_up_all(&ctx->wq);
113}
114
115static int cxl_handle_segment_miss(struct cxl_context *ctx,
116 struct mm_struct *mm, u64 ea)
117{
118 int rc;
119
120 pr_devel("CXL interrupt: Segment fault pe: %i ea: %#llx\n", ctx->pe, ea);
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100121 trace_cxl_ste_miss(ctx, ea);
Ian Munsief204e0b2014-10-08 19:55:02 +1100122
123 if ((rc = cxl_fault_segment(ctx, mm, ea)))
124 cxl_ack_ae(ctx);
125 else {
126
127 mb(); /* Order seg table write to TFC MMIO write */
128 cxl_ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
129 }
130
131 return IRQ_HANDLED;
132}
133
134static void cxl_handle_page_fault(struct cxl_context *ctx,
135 struct mm_struct *mm, u64 dsisr, u64 dar)
136{
137 unsigned flt = 0;
138 int result;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530139 unsigned long access, flags, inv_flags = 0;
Ian Munsief204e0b2014-10-08 19:55:02 +1100140
Ian Munsie9bcf28c2015-01-09 20:34:36 +1100141 trace_cxl_pte_miss(ctx, dsisr, dar);
142
Ian Munsief204e0b2014-10-08 19:55:02 +1100143 if ((result = copro_handle_mm_fault(mm, dar, dsisr, &flt))) {
144 pr_devel("copro_handle_mm_fault failed: %#x\n", result);
145 return cxl_ack_ae(ctx);
146 }
147
148 /*
149 * update_mmu_cache() will not have loaded the hash since current->trap
150 * is not a 0x400 or 0x300, so just call hash_page_mm() here.
151 */
152 access = _PAGE_PRESENT;
153 if (dsisr & CXL_PSL_DSISR_An_S)
154 access |= _PAGE_RW;
155 if ((!ctx->kernel) || ~(dar & (1ULL << 63)))
156 access |= _PAGE_USER;
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530157
158 if (dsisr & DSISR_NOHPTE)
159 inv_flags |= HPTE_NOHPTE_UPDATE;
160
Ian Munsief204e0b2014-10-08 19:55:02 +1100161 local_irq_save(flags);
Aneesh Kumar K.Vaefa5682014-12-04 11:00:14 +0530162 hash_page_mm(mm, dar, access, 0x300, inv_flags);
Ian Munsief204e0b2014-10-08 19:55:02 +1100163 local_irq_restore(flags);
164
165 pr_devel("Page fault successfully handled for pe: %i!\n", ctx->pe);
166 cxl_ack_irq(ctx, CXL_PSL_TFC_An_R, 0);
167}
168
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530169/*
170 * Returns the mm_struct corresponding to the context ctx via ctx->pid
171 * In case the task has exited we use the task group leader accessible
172 * via ctx->glpid to find the next task in the thread group that has a
173 * valid mm_struct associated with it. If a task with valid mm_struct
174 * is found the ctx->pid is updated to use the task struct for subsequent
175 * translations. In case no valid mm_struct is found in the task group to
176 * service the fault a NULL is returned.
177 */
178static struct mm_struct *get_mem_context(struct cxl_context *ctx)
179{
180 struct task_struct *task = NULL;
181 struct mm_struct *mm = NULL;
182 struct pid *old_pid = ctx->pid;
183
184 if (old_pid == NULL) {
185 pr_warn("%s: Invalid context for pe=%d\n",
186 __func__, ctx->pe);
187 return NULL;
188 }
189
190 task = get_pid_task(old_pid, PIDTYPE_PID);
191
192 /*
193 * pid_alive may look racy but this saves us from costly
194 * get_task_mm when the task is a zombie. In worst case
195 * we may think a task is alive, which is about to die
196 * but get_task_mm will return NULL.
197 */
198 if (task != NULL && pid_alive(task))
199 mm = get_task_mm(task);
200
201 /* release the task struct that was taken earlier */
202 if (task)
203 put_task_struct(task);
204 else
205 pr_devel("%s: Context owning pid=%i for pe=%i dead\n",
206 __func__, pid_nr(old_pid), ctx->pe);
207
208 /*
209 * If we couldn't find the mm context then use the group
210 * leader to iterate over the task group and find a task
211 * that gives us mm_struct.
212 */
213 if (unlikely(mm == NULL && ctx->glpid != NULL)) {
214
215 rcu_read_lock();
216 task = pid_task(ctx->glpid, PIDTYPE_PID);
217 if (task)
218 do {
219 mm = get_task_mm(task);
220 if (mm) {
221 ctx->pid = get_task_pid(task,
222 PIDTYPE_PID);
223 break;
224 }
225 task = next_thread(task);
226 } while (task && !thread_group_leader(task));
227 rcu_read_unlock();
228
229 /* check if we switched pid */
230 if (ctx->pid != old_pid) {
231 if (mm)
232 pr_devel("%s:pe=%i switch pid %i->%i\n",
233 __func__, ctx->pe, pid_nr(old_pid),
234 pid_nr(ctx->pid));
235 else
236 pr_devel("%s:Cannot find mm for pid=%i\n",
237 __func__, pid_nr(old_pid));
238
239 /* drop the reference to older pid */
240 put_pid(old_pid);
241 }
242 }
243
244 return mm;
245}
246
247
248
Ian Munsief204e0b2014-10-08 19:55:02 +1100249void cxl_handle_fault(struct work_struct *fault_work)
250{
251 struct cxl_context *ctx =
252 container_of(fault_work, struct cxl_context, fault_work);
253 u64 dsisr = ctx->dsisr;
254 u64 dar = ctx->dar;
Michael Neulinga6b07d82015-05-27 16:07:11 +1000255 struct mm_struct *mm = NULL;
Ian Munsief204e0b2014-10-08 19:55:02 +1100256
257 if (cxl_p2n_read(ctx->afu, CXL_PSL_DSISR_An) != dsisr ||
258 cxl_p2n_read(ctx->afu, CXL_PSL_DAR_An) != dar ||
259 cxl_p2n_read(ctx->afu, CXL_PSL_PEHandle_An) != ctx->pe) {
260 /* Most likely explanation is harmless - a dedicated process
261 * has detached and these were cleared by the PSL purge, but
262 * warn about it just in case */
263 dev_notice(&ctx->afu->dev, "cxl_handle_fault: Translation fault regs changed\n");
264 return;
265 }
266
Ian Munsie13da7042014-12-08 19:17:58 +1100267 /* Early return if the context is being / has been detached */
268 if (ctx->status == CLOSED) {
269 cxl_ack_ae(ctx);
270 return;
271 }
272
Ian Munsief204e0b2014-10-08 19:55:02 +1100273 pr_devel("CXL BOTTOM HALF handling fault for afu pe: %i. "
274 "DSISR: %#llx DAR: %#llx\n", ctx->pe, dsisr, dar);
275
Michael Neulinga6b07d82015-05-27 16:07:11 +1000276 if (!ctx->kernel) {
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530277
278 mm = get_mem_context(ctx);
279 /* indicates all the thread in task group have exited */
280 if (mm == NULL) {
281 pr_devel("%s: unable to get mm for pe=%d pid=%i\n",
282 __func__, ctx->pe, pid_nr(ctx->pid));
Michael Neulinga6b07d82015-05-27 16:07:11 +1000283 cxl_ack_ae(ctx);
284 return;
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530285 } else {
286 pr_devel("Handling page fault for pe=%d pid=%i\n",
287 ctx->pe, pid_nr(ctx->pid));
Michael Neulinga6b07d82015-05-27 16:07:11 +1000288 }
Ian Munsief204e0b2014-10-08 19:55:02 +1100289 }
290
291 if (dsisr & CXL_PSL_DSISR_An_DS)
292 cxl_handle_segment_miss(ctx, mm, dar);
293 else if (dsisr & CXL_PSL_DSISR_An_DM)
294 cxl_handle_page_fault(ctx, mm, dsisr, dar);
295 else
296 WARN(1, "cxl_handle_fault has nothing to handle\n");
297
Michael Neulinga6b07d82015-05-27 16:07:11 +1000298 if (mm)
299 mmput(mm);
Ian Munsief204e0b2014-10-08 19:55:02 +1100300}
301
302static void cxl_prefault_one(struct cxl_context *ctx, u64 ea)
303{
Ian Munsief204e0b2014-10-08 19:55:02 +1100304 struct mm_struct *mm;
305
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530306 mm = get_mem_context(ctx);
307 if (mm == NULL) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100308 pr_devel("cxl_prefault_one unable to get mm %i\n",
309 pid_nr(ctx->pid));
Ian Munsief204e0b2014-10-08 19:55:02 +1100310 return;
311 }
312
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530313 cxl_fault_segment(ctx, mm, ea);
Ian Munsief204e0b2014-10-08 19:55:02 +1100314
315 mmput(mm);
Ian Munsief204e0b2014-10-08 19:55:02 +1100316}
317
318static u64 next_segment(u64 ea, u64 vsid)
319{
320 if (vsid & SLB_VSID_B_1T)
321 ea |= (1ULL << 40) - 1;
322 else
323 ea |= (1ULL << 28) - 1;
324
325 return ea + 1;
326}
327
328static void cxl_prefault_vma(struct cxl_context *ctx)
329{
330 u64 ea, last_esid = 0;
331 struct copro_slb slb;
332 struct vm_area_struct *vma;
333 int rc;
Ian Munsief204e0b2014-10-08 19:55:02 +1100334 struct mm_struct *mm;
335
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530336 mm = get_mem_context(ctx);
337 if (mm == NULL) {
Ian Munsief204e0b2014-10-08 19:55:02 +1100338 pr_devel("cxl_prefault_vm unable to get mm %i\n",
339 pid_nr(ctx->pid));
Vaibhav Jain7b8ad492015-11-24 16:26:18 +0530340 return;
Ian Munsief204e0b2014-10-08 19:55:02 +1100341 }
342
343 down_read(&mm->mmap_sem);
344 for (vma = mm->mmap; vma; vma = vma->vm_next) {
345 for (ea = vma->vm_start; ea < vma->vm_end;
346 ea = next_segment(ea, slb.vsid)) {
347 rc = copro_calculate_slb(mm, ea, &slb);
348 if (rc)
349 continue;
350
351 if (last_esid == slb.esid)
352 continue;
353
354 cxl_load_segment(ctx, &slb);
355 last_esid = slb.esid;
356 }
357 }
358 up_read(&mm->mmap_sem);
359
360 mmput(mm);
Ian Munsief204e0b2014-10-08 19:55:02 +1100361}
362
363void cxl_prefault(struct cxl_context *ctx, u64 wed)
364{
365 switch (ctx->afu->prefault_mode) {
366 case CXL_PREFAULT_WED:
367 cxl_prefault_one(ctx, wed);
368 break;
369 case CXL_PREFAULT_ALL:
370 cxl_prefault_vma(ctx);
371 break;
372 default:
373 break;
374 }
375}