1 /* mmu-context.c: MMU context allocation and management
3 * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/sched.h>
13 #include <linux/sched/mm.h>
14 #include <linux/sched/task.h>
16 #include <asm/tlbflush.h>
20 static unsigned long cxn_bitmap
[NR_CXN
/ (sizeof(unsigned long) * 8)];
21 static LIST_HEAD(cxn_owners_lru
);
22 static DEFINE_SPINLOCK(cxn_owners_lock
);
24 int __nongpreldata cxn_pinned
= -1;
27 /*****************************************************************************/
29 * initialise a new context
31 int init_new_context(struct task_struct
*tsk
, struct mm_struct
*mm
)
33 memset(&mm
->context
, 0, sizeof(mm
->context
));
34 INIT_LIST_HEAD(&mm
->context
.id_link
);
35 mm
->context
.itlb_cached_pge
= 0xffffffffUL
;
36 mm
->context
.dtlb_cached_pge
= 0xffffffffUL
;
39 } /* end init_new_context() */
41 /*****************************************************************************/
43 * make sure a kernel MMU context has a CPU context number
44 * - call with cxn_owners_lock held
46 static unsigned get_cxn(mm_context_t
*ctx
)
52 if (!list_empty(&ctx
->id_link
)) {
53 list_move_tail(&ctx
->id_link
, &cxn_owners_lru
);
56 /* find the first unallocated context number
57 * - 0 is reserved for the kernel
59 cxn
= find_next_zero_bit(cxn_bitmap
, NR_CXN
, 1);
61 set_bit(cxn
, cxn_bitmap
);
64 /* none remaining - need to steal someone else's cxn */
66 list_for_each(_p
, &cxn_owners_lru
) {
67 p
= list_entry(_p
, mm_context_t
, id_link
);
68 if (!p
->id_busy
&& p
->id
!= cxn_pinned
)
72 BUG_ON(_p
== &cxn_owners_lru
);
76 list_del_init(&p
->id_link
);
81 list_add_tail(&ctx
->id_link
, &cxn_owners_lru
);
87 /*****************************************************************************/
89 * restore the current TLB miss handler mapped page tables into the MMU context and set up a
90 * mapping for the page directory
92 void change_mm_context(mm_context_t
*old
, mm_context_t
*ctx
, pgd_t
*pgd
)
96 _pgd
= virt_to_phys(pgd
);
98 /* save the state of the outgoing MMU context */
101 asm volatile("movsg scr0,%0" : "=r"(old
->itlb_cached_pge
));
102 asm volatile("movsg dampr4,%0" : "=r"(old
->itlb_ptd_mapping
));
103 asm volatile("movsg scr1,%0" : "=r"(old
->dtlb_cached_pge
));
104 asm volatile("movsg dampr5,%0" : "=r"(old
->dtlb_ptd_mapping
));
106 /* select an MMU context number */
107 spin_lock(&cxn_owners_lock
);
110 spin_unlock(&cxn_owners_lock
);
112 asm volatile("movgs %0,cxnr" : : "r"(ctx
->id
));
114 /* restore the state of the incoming MMU context */
115 asm volatile("movgs %0,scr0" : : "r"(ctx
->itlb_cached_pge
));
116 asm volatile("movgs %0,dampr4" : : "r"(ctx
->itlb_ptd_mapping
));
117 asm volatile("movgs %0,scr1" : : "r"(ctx
->dtlb_cached_pge
));
118 asm volatile("movgs %0,dampr5" : : "r"(ctx
->dtlb_ptd_mapping
));
120 /* map the PGD into uncached virtual memory */
121 asm volatile("movgs %0,ttbr" : : "r"(_pgd
));
122 asm volatile("movgs %0,dampr3"
123 :: "r"(_pgd
| xAMPRx_L
| xAMPRx_M
| xAMPRx_SS_16Kb
|
124 xAMPRx_S
| xAMPRx_C
| xAMPRx_V
));
126 } /* end change_mm_context() */
128 /*****************************************************************************/
130 * finished with an MMU context number
132 void destroy_context(struct mm_struct
*mm
)
134 mm_context_t
*ctx
= &mm
->context
;
136 spin_lock(&cxn_owners_lock
);
138 if (!list_empty(&ctx
->id_link
)) {
139 if (ctx
->id
== cxn_pinned
)
142 list_del_init(&ctx
->id_link
);
143 clear_bit(ctx
->id
, cxn_bitmap
);
144 __flush_tlb_mm(ctx
->id
);
148 spin_unlock(&cxn_owners_lock
);
149 } /* end destroy_context() */
151 /*****************************************************************************/
153 * display the MMU context currently a process is currently using
155 #ifdef CONFIG_PROC_FS
156 char *proc_pid_status_frv_cxnr(struct mm_struct
*mm
, char *buffer
)
158 spin_lock(&cxn_owners_lock
);
159 buffer
+= sprintf(buffer
, "CXNR: %u\n", mm
->context
.id
);
160 spin_unlock(&cxn_owners_lock
);
163 } /* end proc_pid_status_frv_cxnr() */
166 /*****************************************************************************/
168 * (un)pin a process's mm_struct's MMU context ID
170 int cxn_pin_by_pid(pid_t pid
)
172 struct task_struct
*tsk
;
173 struct mm_struct
*mm
= NULL
;
176 /* unpin if pid is zero */
184 /* get a handle on the mm_struct */
185 read_lock(&tasklist_lock
);
186 tsk
= find_task_by_vpid(pid
);
198 read_unlock(&tasklist_lock
);
203 /* make sure it has a CXN and pin it */
204 spin_lock(&cxn_owners_lock
);
205 cxn_pinned
= get_cxn(&mm
->context
);
206 spin_unlock(&cxn_owners_lock
);
210 } /* end cxn_pin_by_pid() */