Rev 877 | Rev 894 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 877 | Rev 883 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Jakub Jermar |
2 | * Copyright (C) 2005 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <arch/mm/tlb.h> |
29 | #include <arch/mm/tlb.h> |
30 | #include <mm/tlb.h> |
30 | #include <mm/tlb.h> |
31 | #include <arch/mm/frame.h> |
31 | #include <arch/mm/frame.h> |
32 | #include <arch/mm/page.h> |
32 | #include <arch/mm/page.h> |
33 | #include <arch/mm/mmu.h> |
33 | #include <arch/mm/mmu.h> |
34 | #include <mm/asid.h> |
34 | #include <mm/asid.h> |
35 | #include <print.h> |
35 | #include <print.h> |
36 | #include <arch/types.h> |
36 | #include <arch/types.h> |
37 | #include <typedefs.h> |
37 | #include <typedefs.h> |
38 | #include <config.h> |
38 | #include <config.h> |
39 | #include <arch/trap/trap.h> |
39 | #include <arch/trap/trap.h> |
40 | #include <panic.h> |
40 | #include <panic.h> |
41 | #include <arch/asm.h> |
41 | #include <arch/asm.h> |
42 | #include <symtab.h> |
42 | #include <symtab.h> |
- | 43 | #include <arch/drivers/fb.h> |
|
43 | 44 | ||
44 | char *context_encoding[] = { |
45 | char *context_encoding[] = { |
45 | "Primary", |
46 | "Primary", |
46 | "Secondary", |
47 | "Secondary", |
47 | "Nucleus", |
48 | "Nucleus", |
48 | "Reserved" |
49 | "Reserved" |
49 | }; |
50 | }; |
50 | 51 | ||
51 | /** Initialize ITLB and DTLB. |
52 | /** Initialize ITLB and DTLB. |
52 | * |
53 | * |
53 | * The goal of this function is to disable MMU |
54 | * The goal of this function is to disable MMU |
54 | * so that both TLBs can be purged and new |
55 | * so that both TLBs can be purged and new |
55 | * kernel 4M locked entry can be installed. |
56 | * kernel 4M locked entry can be installed. |
56 | * After TLB is initialized, MMU is enabled |
57 | * After TLB is initialized, MMU is enabled |
57 | * again. |
58 | * again. |
58 | * |
59 | * |
59 | * Switching MMU off imposes the requirement for |
60 | * Switching MMU off imposes the requirement for |
60 | * the kernel to run in identity mapped environment. |
61 | * the kernel to run in identity mapped environment. |
61 | */ |
62 | */ |
62 | void tlb_arch_init(void) |
63 | void tlb_arch_init(void) |
63 | { |
64 | { |
64 | tlb_tag_access_reg_t tag; |
65 | tlb_tag_access_reg_t tag; |
65 | tlb_data_t data; |
66 | tlb_data_t data; |
66 | frame_address_t fr; |
67 | frame_address_t fr; |
67 | page_address_t pg; |
68 | page_address_t pg; |
68 | 69 | ||
69 | fr.address = config.base; |
70 | fr.address = config.base; |
70 | pg.address = config.base; |
71 | pg.address = config.base; |
71 | 72 | ||
72 | immu_disable(); |
73 | immu_disable(); |
73 | dmmu_disable(); |
74 | dmmu_disable(); |
74 | 75 | ||
75 | /* |
76 | /* |
76 | * We do identity mapping of 4M-page at 4M. |
77 | * We do identity mapping of 4M-page at 4M. |
77 | */ |
78 | */ |
78 | tag.value = ASID_KERNEL; |
79 | tag.value = ASID_KERNEL; |
79 | tag.vpn = pg.vpn; |
80 | tag.vpn = pg.vpn; |
80 | 81 | ||
81 | itlb_tag_access_write(tag.value); |
82 | itlb_tag_access_write(tag.value); |
82 | dtlb_tag_access_write(tag.value); |
83 | dtlb_tag_access_write(tag.value); |
83 | 84 | ||
84 | data.value = 0; |
85 | data.value = 0; |
85 | data.v = true; |
86 | data.v = true; |
86 | data.size = PAGESIZE_4M; |
87 | data.size = PAGESIZE_4M; |
87 | data.pfn = fr.pfn; |
88 | data.pfn = fr.pfn; |
88 | data.l = true; |
89 | data.l = true; |
89 | data.cp = 1; |
90 | data.cp = 1; |
90 | data.cv = 1; |
91 | data.cv = 1; |
91 | data.p = true; |
92 | data.p = true; |
92 | data.w = true; |
93 | data.w = true; |
93 | data.g = true; |
94 | data.g = true; |
94 | 95 | ||
95 | itlb_data_in_write(data.value); |
96 | itlb_data_in_write(data.value); |
96 | dtlb_data_in_write(data.value); |
97 | dtlb_data_in_write(data.value); |
97 | 98 | ||
98 | /* |
99 | /* |
99 | * Register window traps can occur before MMU is enabled again. |
100 | * Register window traps can occur before MMU is enabled again. |
100 | * This ensures that any such traps will be handled from |
101 | * This ensures that any such traps will be handled from |
101 | * kernel identity mapped trap handler. |
102 | * kernel identity mapped trap handler. |
102 | */ |
103 | */ |
103 | trap_switch_trap_table(); |
104 | trap_switch_trap_table(); |
104 | 105 | ||
105 | tlb_invalidate_all(); |
106 | tlb_invalidate_all(); |
106 | 107 | ||
107 | dmmu_enable(); |
108 | dmmu_enable(); |
108 | immu_enable(); |
109 | immu_enable(); |
109 | 110 | ||
110 | /* |
111 | /* |
111 | * Quick hack: map frame buffer |
112 | * Quick hack: map frame buffer |
112 | */ |
113 | */ |
113 | fr.address = 0x1C901000000ULL; |
114 | fr.address = FB_PHYS_ADDRESS; |
114 | pg.address = 0xc0000000; |
115 | pg.address = FB_VIRT_ADDRESS; |
115 | 116 | ||
116 | tag.value = ASID_KERNEL; |
117 | tag.value = ASID_KERNEL; |
117 | tag.vpn = pg.vpn; |
118 | tag.vpn = pg.vpn; |
118 | 119 | ||
119 | dtlb_tag_access_write(tag.value); |
120 | dtlb_tag_access_write(tag.value); |
120 | 121 | ||
121 | data.value = 0; |
122 | data.value = 0; |
122 | data.v = true; |
123 | data.v = true; |
123 | data.size = PAGESIZE_4M; |
124 | data.size = PAGESIZE_4M; |
124 | data.pfn = fr.pfn; |
125 | data.pfn = fr.pfn; |
125 | data.l = true; |
126 | data.l = true; |
126 | data.cp = 0; |
127 | data.cp = 0; |
127 | data.cv = 0; |
128 | data.cv = 0; |
128 | data.p = true; |
129 | data.p = true; |
129 | data.w = true; |
130 | data.w = true; |
130 | data.g = true; |
131 | data.g = true; |
131 | 132 | ||
132 | dtlb_data_in_write(data.value); |
133 | dtlb_data_in_write(data.value); |
133 | - | ||
134 | } |
134 | } |
135 | 135 | ||
136 | /** ITLB miss handler. */ |
136 | /** ITLB miss handler. */ |
137 | void fast_instruction_access_mmu_miss(void) |
137 | void fast_instruction_access_mmu_miss(void) |
138 | { |
138 | { |
139 | panic("%s\n", __FUNCTION__); |
139 | panic("%s\n", __FUNCTION__); |
140 | } |
140 | } |
141 | 141 | ||
142 | /** DTLB miss handler. */ |
142 | /** DTLB miss handler. */ |
143 | void fast_data_access_mmu_miss(void) |
143 | void fast_data_access_mmu_miss(void) |
144 | { |
144 | { |
145 | tlb_tag_access_reg_t tag; |
145 | tlb_tag_access_reg_t tag; |
146 | tlb_data_t data; |
146 | tlb_data_t data; |
147 | __address tpc; |
147 | __address tpc; |
148 | char *tpc_str; |
148 | char *tpc_str; |
149 | 149 | ||
150 | tag.value = dtlb_tag_access_read(); |
150 | tag.value = dtlb_tag_access_read(); |
151 | if (tag.context != ASID_KERNEL || tag.vpn == 0) { |
151 | if (tag.context != ASID_KERNEL || tag.vpn == 0) { |
152 | tpc = tpc_read(); |
152 | tpc = tpc_read(); |
153 | tpc_str = get_symtab_entry(tpc); |
153 | tpc_str = get_symtab_entry(tpc); |
154 | 154 | ||
155 | printf("Faulting page: %P, ASID=%d\n", tag.vpn * PAGE_SIZE, tag.context); |
155 | printf("Faulting page: %P, ASID=%d\n", tag.vpn * PAGE_SIZE, tag.context); |
156 | printf("TPC=%P, (%s)\n", tpc, tpc_str ? tpc_str : "?"); |
156 | printf("TPC=%P, (%s)\n", tpc, tpc_str ? tpc_str : "?"); |
157 | panic("%s\n", __FUNCTION__); |
157 | panic("%s\n", __FUNCTION__); |
158 | } |
158 | } |
159 | 159 | ||
160 | /* |
160 | /* |
161 | * Identity map piece of faulting kernel address space. |
161 | * Identity map piece of faulting kernel address space. |
162 | */ |
162 | */ |
163 | data.value = 0; |
163 | data.value = 0; |
164 | data.v = true; |
164 | data.v = true; |
165 | data.size = PAGESIZE_8K; |
165 | data.size = PAGESIZE_8K; |
166 | data.pfn = tag.vpn; |
166 | data.pfn = tag.vpn; |
167 | data.l = false; |
167 | data.l = false; |
168 | data.cp = 1; |
168 | data.cp = 1; |
169 | data.cv = 1; |
169 | data.cv = 1; |
170 | data.p = true; |
170 | data.p = true; |
171 | data.w = true; |
171 | data.w = true; |
172 | data.g = true; |
172 | data.g = true; |
173 | 173 | ||
174 | dtlb_data_in_write(data.value); |
174 | dtlb_data_in_write(data.value); |
175 | } |
175 | } |
176 | 176 | ||
177 | /** DTLB protection fault handler. */ |
177 | /** DTLB protection fault handler. */ |
178 | void fast_data_access_protection(void) |
178 | void fast_data_access_protection(void) |
179 | { |
179 | { |
180 | panic("%s\n", __FUNCTION__); |
180 | panic("%s\n", __FUNCTION__); |
181 | } |
181 | } |
182 | 182 | ||
183 | /** Print contents of both TLBs. */ |
183 | /** Print contents of both TLBs. */ |
184 | void tlb_print(void) |
184 | void tlb_print(void) |
185 | { |
185 | { |
186 | int i; |
186 | int i; |
187 | tlb_data_t d; |
187 | tlb_data_t d; |
188 | tlb_tag_read_reg_t t; |
188 | tlb_tag_read_reg_t t; |
189 | 189 | ||
190 | printf("I-TLB contents:\n"); |
190 | printf("I-TLB contents:\n"); |
191 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
191 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
192 | d.value = itlb_data_access_read(i); |
192 | d.value = itlb_data_access_read(i); |
193 | t.value = itlb_tag_read_read(i); |
193 | t.value = itlb_tag_read_read(i); |
194 | 194 | ||
195 | printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", |
195 | printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", |
196 | i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g); |
196 | i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g); |
197 | } |
197 | } |
198 | 198 | ||
199 | printf("D-TLB contents:\n"); |
199 | printf("D-TLB contents:\n"); |
200 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
200 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
201 | d.value = dtlb_data_access_read(i); |
201 | d.value = dtlb_data_access_read(i); |
202 | t.value = dtlb_tag_read_read(i); |
202 | t.value = dtlb_tag_read_read(i); |
203 | 203 | ||
204 | printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", |
204 | printf("%d: vpn=%Q, context=%d, v=%d, size=%d, nfo=%d, ie=%d, soft2=%X, diag=%X, pfn=%X, soft=%X, l=%d, cp=%d, cv=%d, e=%d, p=%d, w=%d, g=%d\n", |
205 | i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g); |
205 | i, t.vpn, t.context, d.v, d.size, d.nfo, d.ie, d.soft2, d.diag, d.pfn, d.soft, d.l, d.cp, d.cv, d.e, d.p, d.w, d.g); |
206 | } |
206 | } |
207 | 207 | ||
208 | } |
208 | } |
209 | 209 | ||
210 | /** Invalidate all unlocked ITLB and DTLB entries. */ |
210 | /** Invalidate all unlocked ITLB and DTLB entries. */ |
211 | void tlb_invalidate_all(void) |
211 | void tlb_invalidate_all(void) |
212 | { |
212 | { |
213 | int i; |
213 | int i; |
214 | tlb_data_t d; |
214 | tlb_data_t d; |
215 | tlb_tag_read_reg_t t; |
215 | tlb_tag_read_reg_t t; |
216 | 216 | ||
217 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
217 | for (i = 0; i < ITLB_ENTRY_COUNT; i++) { |
218 | d.value = itlb_data_access_read(i); |
218 | d.value = itlb_data_access_read(i); |
219 | if (!d.l) { |
219 | if (!d.l) { |
220 | t.value = itlb_tag_read_read(i); |
220 | t.value = itlb_tag_read_read(i); |
221 | d.v = false; |
221 | d.v = false; |
222 | itlb_tag_access_write(t.value); |
222 | itlb_tag_access_write(t.value); |
223 | itlb_data_access_write(i, d.value); |
223 | itlb_data_access_write(i, d.value); |
224 | } |
224 | } |
225 | } |
225 | } |
226 | 226 | ||
227 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
227 | for (i = 0; i < DTLB_ENTRY_COUNT; i++) { |
228 | d.value = dtlb_data_access_read(i); |
228 | d.value = dtlb_data_access_read(i); |
229 | if (!d.l) { |
229 | if (!d.l) { |
230 | t.value = dtlb_tag_read_read(i); |
230 | t.value = dtlb_tag_read_read(i); |
231 | d.v = false; |
231 | d.v = false; |
232 | dtlb_tag_access_write(t.value); |
232 | dtlb_tag_access_write(t.value); |
233 | dtlb_data_access_write(i, d.value); |
233 | dtlb_data_access_write(i, d.value); |
234 | } |
234 | } |
235 | } |
235 | } |
236 | 236 | ||
237 | } |
237 | } |
238 | 238 | ||
239 | /** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context). |
239 | /** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context). |
240 | * |
240 | * |
241 | * @param asid Address Space ID. |
241 | * @param asid Address Space ID. |
242 | */ |
242 | */ |
243 | void tlb_invalidate_asid(asid_t asid) |
243 | void tlb_invalidate_asid(asid_t asid) |
244 | { |
244 | { |
245 | /* TODO: write asid to some Context register and encode the register in second parameter below. */ |
245 | /* TODO: write asid to some Context register and encode the register in second parameter below. */ |
246 | itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0); |
246 | itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0); |
247 | dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0); |
247 | dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0); |
248 | } |
248 | } |
249 | 249 | ||
250 | /** Invalidate all ITLB and DTLB entries for specified page range in specified address space. |
250 | /** Invalidate all ITLB and DTLB entries for specified page range in specified address space. |
251 | * |
251 | * |
252 | * @param asid Address Space ID. |
252 | * @param asid Address Space ID. |
253 | * @param page First page which to sweep out from ITLB and DTLB. |
253 | * @param page First page which to sweep out from ITLB and DTLB. |
254 | * @param cnt Number of ITLB and DTLB entries to invalidate. |
254 | * @param cnt Number of ITLB and DTLB entries to invalidate. |
255 | */ |
255 | */ |
256 | void tlb_invalidate_pages(asid_t asid, __address page, count_t cnt) |
256 | void tlb_invalidate_pages(asid_t asid, __address page, count_t cnt) |
257 | { |
257 | { |
258 | int i; |
258 | int i; |
259 | 259 | ||
260 | for (i = 0; i < cnt; i++) { |
260 | for (i = 0; i < cnt; i++) { |
261 | /* TODO: write asid to some Context register and encode the register in second parameter below. */ |
261 | /* TODO: write asid to some Context register and encode the register in second parameter below. */ |
262 | itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE); |
262 | itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE); |
263 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE); |
263 | dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE); |
264 | } |
264 | } |
265 | } |
265 | } |
266 | 266 |