2 * MMU context allocation for 64-bit kernels.
4 * Copyright (C) 2004 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/string.h>
17 #include <linux/types.h>
19 #include <linux/spinlock.h>
20 #include <linux/idr.h>
21 #include <linux/export.h>
22 #include <linux/gfp.h>
23 #include <linux/slab.h>
25 #include <asm/mmu_context.h>
27 #ifdef CONFIG_PPC_ICSWX
29 * The processor and its L2 cache cause the icswx instruction to
30 * generate a COP_REQ transaction on PowerBus. The transaction has
31 * no address, and the processor does not perform an MMU access
32 * to authenticate the transaction. The command portion of the
33 * PowerBus COP_REQ transaction includes the LPAR_ID (LPID) and
34 * the coprocessor Process ID (PID), which the coprocessor compares
35 * to the authorized LPID and PID held in the coprocessor, to determine
36 * if the process is authorized to generate the transaction.
37 * The data of the COP_REQ transaction is 128-byte or less and is
38 * placed in cacheable memory on a 128-byte cache line boundary.
40 * The task to use a coprocessor should use use_cop() to allocate
41 * a coprocessor PID before executing icswx instruction. use_cop()
42 * also enables the coprocessor context switching. Drop_cop() is
43 * used to free the coprocessor PID.
46 * Host Fabric Interface (HFI) is a PowerPC network coprocessor.
47 * Each HFI have multiple windows. Each HFI window serves as a
48 * network device sending to and receiving from HFI network.
49 * HFI immediate send function uses icswx instruction. The immediate
50 * send function allows small (single cache-line) packets be sent
51 * without using the regular HFI send FIFO and doorbell, which are
52 * much slower than immediate send.
54 * For each task intending to use HFI immediate send, the HFI driver
55 * calls use_cop() to obtain a coprocessor PID for the task.
56 * The HFI driver then allocate a free HFI window and save the
57 * coprocessor PID to the HFI window to allow the task to use the
60 * The HFI driver repeatedly creates immediate send packets and
61 * issues icswx instruction to send data through the HFI window.
62 * The HFI compares the coprocessor PID in the CPU PID register
63 * to the PID held in the HFI window to determine if the transaction
66 * When the task to release the HFI window, the HFI driver calls
67 * drop_cop() to release the coprocessor PID.
70 #define COP_PID_NONE 0
71 #define COP_PID_MIN (COP_PID_NONE + 1)
72 #define COP_PID_MAX (0xFFFF)
74 static DEFINE_SPINLOCK(mmu_context_acop_lock
);
75 static DEFINE_IDA(cop_ida
);
77 void switch_cop(struct mm_struct
*next
)
79 mtspr(SPRN_PID
, next
->context
.cop_pid
);
80 mtspr(SPRN_ACOP
, next
->context
.acop
);
83 static int new_cop_pid(struct ida
*ida
, int min_id
, int max_id
,
90 if (!ida_pre_get(ida
, GFP_KERNEL
))
94 err
= ida_get_new_above(ida
, min_id
, &index
);
102 if (index
> max_id
) {
104 ida_remove(ida
, index
);
112 static void sync_cop(void *arg
)
114 struct mm_struct
*mm
= arg
;
116 if (mm
== current
->active_mm
)
117 switch_cop(current
->active_mm
);
121 * Start using a coprocessor.
122 * @acop: mask of coprocessor to be used.
123 * @mm: The mm the coprocessor to associate with. Most likely current mm.
125 * Return a positive PID if successful. Negative errno otherwise.
126 * The returned PID will be fed to the coprocessor to determine if an
127 * icswx transaction is authenticated.
129 int use_cop(unsigned long acop
, struct mm_struct
*mm
)
133 if (!cpu_has_feature(CPU_FTR_ICSWX
))
139 /* The page_table_lock ensures mm_users won't change under us */
140 spin_lock(&mm
->page_table_lock
);
141 spin_lock(mm
->context
.cop_lockp
);
143 if (mm
->context
.cop_pid
== COP_PID_NONE
) {
144 ret
= new_cop_pid(&cop_ida
, COP_PID_MIN
, COP_PID_MAX
,
145 &mmu_context_acop_lock
);
149 mm
->context
.cop_pid
= ret
;
151 mm
->context
.acop
|= acop
;
156 * If this is a threaded process then there might be other threads
157 * running. We need to send an IPI to force them to pick up any
158 * change in PID and ACOP.
160 if (atomic_read(&mm
->mm_users
) > 1)
161 smp_call_function(sync_cop
, mm
, 1);
163 ret
= mm
->context
.cop_pid
;
166 spin_unlock(mm
->context
.cop_lockp
);
167 spin_unlock(&mm
->page_table_lock
);
171 EXPORT_SYMBOL_GPL(use_cop
);
174 * Stop using a coprocessor.
175 * @acop: mask of coprocessor to be stopped.
176 * @mm: The mm the coprocessor associated with.
178 void drop_cop(unsigned long acop
, struct mm_struct
*mm
)
180 int free_pid
= COP_PID_NONE
;
182 if (!cpu_has_feature(CPU_FTR_ICSWX
))
185 if (WARN_ON_ONCE(!mm
))
188 /* The page_table_lock ensures mm_users won't change under us */
189 spin_lock(&mm
->page_table_lock
);
190 spin_lock(mm
->context
.cop_lockp
);
192 mm
->context
.acop
&= ~acop
;
194 if ((!mm
->context
.acop
) && (mm
->context
.cop_pid
!= COP_PID_NONE
)) {
195 free_pid
= mm
->context
.cop_pid
;
196 mm
->context
.cop_pid
= COP_PID_NONE
;
202 * If this is a threaded process then there might be other threads
203 * running. We need to send an IPI to force them to pick up any
204 * change in PID and ACOP.
206 if (atomic_read(&mm
->mm_users
) > 1)
207 smp_call_function(sync_cop
, mm
, 1);
209 if (free_pid
!= COP_PID_NONE
) {
210 spin_lock(&mmu_context_acop_lock
);
211 ida_remove(&cop_ida
, free_pid
);
212 spin_unlock(&mmu_context_acop_lock
);
215 spin_unlock(mm
->context
.cop_lockp
);
216 spin_unlock(&mm
->page_table_lock
);
218 EXPORT_SYMBOL_GPL(drop_cop
);
220 #endif /* CONFIG_PPC_ICSWX */
222 static DEFINE_SPINLOCK(mmu_context_lock
);
223 static DEFINE_IDA(mmu_context_ida
);
226 * The proto-VSID space has 2^35 - 1 segments available for user mappings.
227 * Each segment contains 2^28 bytes. Each context maps 2^44 bytes,
228 * so we can support 2^19-1 contexts (19 == 35 + 28 - 44).
230 #define MAX_CONTEXT ((1UL << 19) - 1)
232 int __init_new_context(void)
238 if (!ida_pre_get(&mmu_context_ida
, GFP_KERNEL
))
241 spin_lock(&mmu_context_lock
);
242 err
= ida_get_new_above(&mmu_context_ida
, 1, &index
);
243 spin_unlock(&mmu_context_lock
);
250 if (index
> MAX_CONTEXT
) {
251 spin_lock(&mmu_context_lock
);
252 ida_remove(&mmu_context_ida
, index
);
253 spin_unlock(&mmu_context_lock
);
259 EXPORT_SYMBOL_GPL(__init_new_context
);
261 int init_new_context(struct task_struct
*tsk
, struct mm_struct
*mm
)
265 index
= __init_new_context();
269 /* The old code would re-promote on fork, we don't do that
270 * when using slices as it could cause problem promoting slices
271 * that have been forced down to 4K
273 if (slice_mm_new_context(mm
))
274 slice_set_user_psize(mm
, mmu_virtual_psize
);
275 subpage_prot_init_new_context(mm
);
276 mm
->context
.id
= index
;
277 #ifdef CONFIG_PPC_ICSWX
278 mm
->context
.cop_lockp
= kmalloc(sizeof(spinlock_t
), GFP_KERNEL
);
279 if (!mm
->context
.cop_lockp
) {
280 __destroy_context(index
);
281 subpage_prot_free(mm
);
282 mm
->context
.id
= MMU_NO_CONTEXT
;
285 spin_lock_init(mm
->context
.cop_lockp
);
286 #endif /* CONFIG_PPC_ICSWX */
291 void __destroy_context(int context_id
)
293 spin_lock(&mmu_context_lock
);
294 ida_remove(&mmu_context_ida
, context_id
);
295 spin_unlock(&mmu_context_lock
);
297 EXPORT_SYMBOL_GPL(__destroy_context
);
299 void destroy_context(struct mm_struct
*mm
)
301 #ifdef CONFIG_PPC_ICSWX
302 drop_cop(mm
->context
.acop
, mm
);
303 kfree(mm
->context
.cop_lockp
);
304 mm
->context
.cop_lockp
= NULL
;
305 #endif /* CONFIG_PPC_ICSWX */
306 __destroy_context(mm
->context
.id
);
307 subpage_prot_free(mm
);
308 mm
->context
.id
= MMU_NO_CONTEXT
;