Linux 4.1.18
[linux/fpc-iii.git] / arch / powerpc / mm / icswx_pid.c
blob91e30eb7d054e9cbb0ddf161df6dc27b3177beed
1 /*
2 * ICSWX and ACOP/PID Management
4 * Copyright (C) 2011 Anton Blanchard, IBM Corp. <anton@samba.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
13 #include <linux/sched.h>
14 #include <linux/kernel.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/mm.h>
18 #include <linux/spinlock.h>
19 #include <linux/idr.h>
20 #include <linux/module.h>
21 #include "icswx.h"
23 #define COP_PID_MIN (COP_PID_NONE + 1)
24 #define COP_PID_MAX (0xFFFF)
26 static DEFINE_SPINLOCK(mmu_context_acop_lock);
27 static DEFINE_IDA(cop_ida);
29 static int new_cop_pid(struct ida *ida, int min_id, int max_id,
30 spinlock_t *lock)
32 int index;
33 int err;
35 again:
36 if (!ida_pre_get(ida, GFP_KERNEL))
37 return -ENOMEM;
39 spin_lock(lock);
40 err = ida_get_new_above(ida, min_id, &index);
41 spin_unlock(lock);
43 if (err == -EAGAIN)
44 goto again;
45 else if (err)
46 return err;
48 if (index > max_id) {
49 spin_lock(lock);
50 ida_remove(ida, index);
51 spin_unlock(lock);
52 return -ENOMEM;
55 return index;
58 int get_cop_pid(struct mm_struct *mm)
60 int pid;
62 if (mm->context.cop_pid == COP_PID_NONE) {
63 pid = new_cop_pid(&cop_ida, COP_PID_MIN, COP_PID_MAX,
64 &mmu_context_acop_lock);
65 if (pid >= 0)
66 mm->context.cop_pid = pid;
68 return mm->context.cop_pid;
71 int disable_cop_pid(struct mm_struct *mm)
73 int free_pid = COP_PID_NONE;
75 if ((!mm->context.acop) && (mm->context.cop_pid != COP_PID_NONE)) {
76 free_pid = mm->context.cop_pid;
77 mm->context.cop_pid = COP_PID_NONE;
79 return free_pid;
82 void free_cop_pid(int free_pid)
84 spin_lock(&mmu_context_acop_lock);
85 ida_remove(&cop_ida, free_pid);
86 spin_unlock(&mmu_context_acop_lock);