source: mainline/kernel/arch/sparc64/src/mm/tlb.c@ a5f76758

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a5f76758 was 11675207, checked in by jermar <jermar@…>, 17 years ago

Move everything to kernel/.

  • Property mode set to 100644
File size: 7.4 KB
RevLine 
[0d04024]1/*
2 * Copyright (C) 2005 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
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
15 * derived from this software without specific prior written permission.
16 *
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
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
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
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
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[b45c443]29 /** @addtogroup sparc64mm
30 * @{
31 */
32/** @file
33 */
34
[0d04024]35#include <arch/mm/tlb.h>
36#include <mm/tlb.h>
[0cfc4d38]37#include <arch/mm/frame.h>
38#include <arch/mm/page.h>
39#include <arch/mm/mmu.h>
[68656282]40#include <mm/asid.h>
[0d04024]41#include <print.h>
[dbb6886]42#include <arch/types.h>
43#include <typedefs.h>
[0cfc4d38]44#include <config.h>
[49b6d32]45#include <arch/trap/trap.h>
[008029d]46#include <panic.h>
[b6fba84]47#include <arch/asm.h>
48#include <symtab.h>
[02f441c0]49
[7cb53f62]50#include <arch/drivers/fb.h>
[30ab05f]51#include <arch/drivers/i8042.h>
[b6fba84]52
53char *context_encoding[] = {
54 "Primary",
55 "Secondary",
56 "Nucleus",
57 "Reserved"
58};
[0d04024]59
[0cfc4d38]60/** Initialize ITLB and DTLB.
61 *
62 * The goal of this function is to disable MMU
63 * so that both TLBs can be purged and new
64 * kernel 4M locked entry can be installed.
65 * After TLB is initialized, MMU is enabled
66 * again.
[10c071e]67 *
68 * Switching MMU off imposes the requirement for
69 * the kernel to run in identity mapped environment.
[0cfc4d38]70 */
[0d04024]71void tlb_arch_init(void)
72{
[0cfc4d38]73 tlb_tag_access_reg_t tag;
74 tlb_data_t data;
75 frame_address_t fr;
76 page_address_t pg;
77
78 fr.address = config.base;
79 pg.address = config.base;
[031e264]80
[0cfc4d38]81 immu_disable();
82 dmmu_disable();
[7d6ec87]83
84 /*
85 * Demap everything, especially OpenFirmware.
86 */
87 itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
88 dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
[0cfc4d38]89
90 /*
[0e4dd7b]91 * We do identity mapping of 4M-page at 4M.
[0cfc4d38]92 */
[68656282]93 tag.value = ASID_KERNEL;
[0cfc4d38]94 tag.vpn = pg.vpn;
95
96 itlb_tag_access_write(tag.value);
97 dtlb_tag_access_write(tag.value);
98
99 data.value = 0;
100 data.v = true;
101 data.size = PAGESIZE_4M;
102 data.pfn = fr.pfn;
103 data.l = true;
104 data.cp = 1;
105 data.cv = 1;
106 data.p = true;
107 data.w = true;
108 data.g = true;
109
110 itlb_data_in_write(data.value);
111 dtlb_data_in_write(data.value);
112
[10c071e]113 /*
114 * Register window traps can occur before MMU is enabled again.
115 * This ensures that any such traps will be handled from
116 * kernel identity mapped trap handler.
117 */
118 trap_switch_trap_table();
119
[0cfc4d38]120 tlb_invalidate_all();
121
122 dmmu_enable();
123 immu_enable();
[97f1691]124}
[b6fba84]125
[97f1691]126/** Insert privileged mapping into DMMU TLB.
127 *
128 * @param page Virtual page address.
129 * @param frame Physical frame address.
130 * @param pagesize Page size.
131 * @param locked True for permanent mappings, false otherwise.
132 * @param cacheable True if the mapping is cacheable, false otherwise.
133 */
[7f1c620]134void dtlb_insert_mapping(uintptr_t page, uintptr_t frame, int pagesize, bool locked, bool cacheable)
[97f1691]135{
136 tlb_tag_access_reg_t tag;
137 tlb_data_t data;
138 page_address_t pg;
139 frame_address_t fr;
[b6fba84]140
[97f1691]141 pg.address = page;
142 fr.address = frame;
[02f441c0]143
144 tag.value = ASID_KERNEL;
145 tag.vpn = pg.vpn;
146
147 dtlb_tag_access_write(tag.value);
148
149 data.value = 0;
150 data.v = true;
[97f1691]151 data.size = pagesize;
[02f441c0]152 data.pfn = fr.pfn;
[97f1691]153 data.l = locked;
154 data.cp = cacheable;
155 data.cv = cacheable;
[02f441c0]156 data.p = true;
157 data.w = true;
158 data.g = true;
159
160 dtlb_data_in_write(data.value);
[0d04024]161}
162
[008029d]163/** ITLB miss handler. */
164void fast_instruction_access_mmu_miss(void)
165{
166 panic("%s\n", __FUNCTION__);
167}
168
169/** DTLB miss handler. */
170void fast_data_access_mmu_miss(void)
171{
[68656282]172 tlb_tag_access_reg_t tag;
[7f1c620]173 uintptr_t tpc;
[b6fba84]174 char *tpc_str;
[7cb53f62]175
[68656282]176 tag.value = dtlb_tag_access_read();
177 if (tag.context != ASID_KERNEL || tag.vpn == 0) {
178 tpc = tpc_read();
179 tpc_str = get_symtab_entry(tpc);
180
[cf85e24c]181 printf("Faulting page: %p, ASID=%d\n", tag.vpn * PAGE_SIZE, tag.context);
182 printf("TPC=%p, (%s)\n", tpc, tpc_str ? tpc_str : "?");
[68656282]183 panic("%s\n", __FUNCTION__);
184 }
185
186 /*
187 * Identity map piece of faulting kernel address space.
188 */
[97f1691]189 dtlb_insert_mapping(tag.vpn * PAGE_SIZE, tag.vpn * FRAME_SIZE, PAGESIZE_8K, false, true);
[008029d]190}
191
192/** DTLB protection fault handler. */
193void fast_data_access_protection(void)
194{
195 panic("%s\n", __FUNCTION__);
196}
197
[0d04024]198/** Print contents of both TLBs. */
199void tlb_print(void)
200{
201 int i;
202 tlb_data_t d;
203 tlb_tag_read_reg_t t;
204
205 printf("I-TLB contents:\n");
206 for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
207 d.value = itlb_data_access_read(i);
[c52ed6b]208 t.value = itlb_tag_read_read(i);
[0d04024]209
[fbf7b4c]210 printf("%d: vpn=%#llx, 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",
[dbb6886]211 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);
[0d04024]212 }
213
214 printf("D-TLB contents:\n");
215 for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
216 d.value = dtlb_data_access_read(i);
[c52ed6b]217 t.value = dtlb_tag_read_read(i);
[0d04024]218
[fbf7b4c]219 printf("%d: vpn=%#llx, 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",
[dbb6886]220 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);
[0d04024]221 }
222
223}
[dbb6886]224
225/** Invalidate all unlocked ITLB and DTLB entries. */
226void tlb_invalidate_all(void)
227{
228 int i;
229 tlb_data_t d;
230 tlb_tag_read_reg_t t;
231
232 for (i = 0; i < ITLB_ENTRY_COUNT; i++) {
233 d.value = itlb_data_access_read(i);
234 if (!d.l) {
235 t.value = itlb_tag_read_read(i);
236 d.v = false;
237 itlb_tag_access_write(t.value);
238 itlb_data_access_write(i, d.value);
239 }
240 }
241
242 for (i = 0; i < DTLB_ENTRY_COUNT; i++) {
243 d.value = dtlb_data_access_read(i);
244 if (!d.l) {
245 t.value = dtlb_tag_read_read(i);
246 d.v = false;
247 dtlb_tag_access_write(t.value);
248 dtlb_data_access_write(i, d.value);
249 }
250 }
251
252}
253
254/** Invalidate all ITLB and DTLB entries that belong to specified ASID (Context).
255 *
256 * @param asid Address Space ID.
257 */
258void tlb_invalidate_asid(asid_t asid)
259{
260 /* TODO: write asid to some Context register and encode the register in second parameter below. */
261 itlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
262 dtlb_demap(TLB_DEMAP_CONTEXT, TLB_DEMAP_NUCLEUS, 0);
263}
264
[4512d7e]265/** Invalidate all ITLB and DTLB entries for specified page range in specified address space.
[dbb6886]266 *
267 * @param asid Address Space ID.
[4512d7e]268 * @param page First page which to sweep out from ITLB and DTLB.
269 * @param cnt Number of ITLB and DTLB entries to invalidate.
[dbb6886]270 */
[7f1c620]271void tlb_invalidate_pages(asid_t asid, uintptr_t page, count_t cnt)
[dbb6886]272{
[4512d7e]273 int i;
274
275 for (i = 0; i < cnt; i++) {
276 /* TODO: write asid to some Context register and encode the register in second parameter below. */
277 itlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE);
278 dtlb_demap(TLB_DEMAP_PAGE, TLB_DEMAP_NUCLEUS, page + i * PAGE_SIZE);
279 }
[dbb6886]280}
[b45c443]281
282 /** @}
283 */
284
Note: See TracBrowser for help on using the repository browser.