[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[2a1410d] | 29 | /** @addtogroup mips32mm
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[edebc15c] | 35 | #include <macros.h>
|
---|
[f761f1eb] | 36 | #include <arch/mm/frame.h>
|
---|
[edebc15c] | 37 | #include <arch/mm/tlb.h>
|
---|
[6c296a9] | 38 | #include <interrupt.h>
|
---|
[f761f1eb] | 39 | #include <mm/frame.h>
|
---|
[edebc15c] | 40 | #include <mm/asid.h>
|
---|
[84dd253] | 41 | #include <config.h>
|
---|
[edebc15c] | 42 | #include <arch/drivers/msim.h>
|
---|
| 43 | #include <print.h>
|
---|
| 44 |
|
---|
[b76e51d] | 45 | #define ZERO_PAGE_MASK TLB_PAGE_MASK_256K
|
---|
| 46 | #define ZERO_FRAMES 2048
|
---|
| 47 | #define ZERO_PAGE_WIDTH 18 /* 256K */
|
---|
| 48 | #define ZERO_PAGE_SIZE (1 << ZERO_PAGE_WIDTH)
|
---|
| 49 | #define ZERO_PAGE_ASID ASID_INVALID
|
---|
| 50 | #define ZERO_PAGE_TLBI 0
|
---|
| 51 | #define ZERO_PAGE_ADDR 0
|
---|
| 52 | #define ZERO_PAGE_OFFSET (ZERO_PAGE_SIZE / sizeof(uint32_t) - 1)
|
---|
| 53 | #define ZERO_PAGE_VALUE (((volatile uint32_t *) ZERO_PAGE_ADDR)[ZERO_PAGE_OFFSET])
|
---|
[edebc15c] | 54 |
|
---|
[b76e51d] | 55 | #define ZERO_PAGE_VALUE_KSEG1(frame) \
|
---|
| 56 | (((volatile uint32_t *) (0xa0000000 + (frame << ZERO_PAGE_WIDTH)))[ZERO_PAGE_OFFSET])
|
---|
[8480714] | 57 |
|
---|
[b76e51d] | 58 | #define MAX_REGIONS 32
|
---|
[edebc15c] | 59 |
|
---|
| 60 | typedef struct {
|
---|
| 61 | pfn_t start;
|
---|
| 62 | pfn_t count;
|
---|
| 63 | } phys_region_t;
|
---|
| 64 |
|
---|
[98000fb] | 65 | static size_t phys_regions_count = 0;
|
---|
[edebc15c] | 66 | static phys_region_t phys_regions[MAX_REGIONS];
|
---|
| 67 |
|
---|
| 68 | /** Check whether frame is available
|
---|
| 69 | *
|
---|
| 70 | * Returns true if given frame is generally available for use.
|
---|
| 71 | * Returns false if given frame is used for physical memory
|
---|
| 72 | * mapped devices and cannot be used.
|
---|
| 73 | *
|
---|
| 74 | */
|
---|
| 75 | static bool frame_available(pfn_t frame)
|
---|
| 76 | {
|
---|
[e94f730] | 77 | #ifdef MACHINE_msim
|
---|
[edebc15c] | 78 | /* MSIM device (dprinter) */
|
---|
| 79 | if (frame == (KA2PA(MSIM_VIDEORAM) >> ZERO_PAGE_WIDTH))
|
---|
| 80 | return false;
|
---|
| 81 |
|
---|
| 82 | /* MSIM device (dkeyboard) */
|
---|
| 83 | if (frame == (KA2PA(MSIM_KBD_ADDRESS) >> ZERO_PAGE_WIDTH))
|
---|
| 84 | return false;
|
---|
[8480714] | 85 | #endif
|
---|
[b76e51d] | 86 |
|
---|
[e94f730] | 87 | #if defined(MACHINE_lgxemul) || defined(MACHINE_bgxemul)
|
---|
[8480714] | 88 | /* gxemul devices */
|
---|
| 89 | if (overlaps(frame << ZERO_PAGE_WIDTH, ZERO_PAGE_SIZE,
|
---|
[933cadf] | 90 | 0x10000000, MiB2SIZE(256)))
|
---|
[8480714] | 91 | return false;
|
---|
| 92 | #endif
|
---|
[edebc15c] | 93 |
|
---|
| 94 | return true;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 |
|
---|
| 98 | /** Check whether frame is safe to write
|
---|
| 99 | *
|
---|
| 100 | * Returns true if given frame is safe for read/write test.
|
---|
| 101 | * Returns false if given frame should not be touched.
|
---|
| 102 | *
|
---|
| 103 | */
|
---|
| 104 | static bool frame_safe(pfn_t frame)
|
---|
| 105 | {
|
---|
| 106 | /* Kernel structures */
|
---|
| 107 | if ((frame << ZERO_PAGE_WIDTH) < KA2PA(config.base))
|
---|
| 108 | return false;
|
---|
| 109 |
|
---|
| 110 | /* Kernel */
|
---|
| 111 | if (overlaps(frame << ZERO_PAGE_WIDTH, ZERO_PAGE_SIZE,
|
---|
| 112 | KA2PA(config.base), config.kernel_size))
|
---|
| 113 | return false;
|
---|
| 114 |
|
---|
| 115 | /* Kernel stack */
|
---|
| 116 | if (overlaps(frame << ZERO_PAGE_WIDTH, ZERO_PAGE_SIZE,
|
---|
| 117 | KA2PA(config.stack_base), config.stack_size))
|
---|
| 118 | return false;
|
---|
| 119 |
|
---|
| 120 | /* Init tasks */
|
---|
| 121 | bool safe = true;
|
---|
[98000fb] | 122 | size_t i;
|
---|
[edebc15c] | 123 | for (i = 0; i < init.cnt; i++)
|
---|
| 124 | if (overlaps(frame << ZERO_PAGE_WIDTH, ZERO_PAGE_SIZE,
|
---|
| 125 | KA2PA(init.tasks[i].addr), init.tasks[i].size)) {
|
---|
| 126 | safe = false;
|
---|
| 127 | break;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | return safe;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[42f60375] | 133 | static void frame_add_region(pfn_t start_frame, pfn_t end_frame, bool low)
|
---|
[edebc15c] | 134 | {
|
---|
[42f60375] | 135 | if (end_frame <= start_frame)
|
---|
| 136 | return;
|
---|
| 137 |
|
---|
| 138 | uintptr_t base = start_frame << ZERO_PAGE_WIDTH;
|
---|
| 139 | size_t size = (end_frame - start_frame) << ZERO_PAGE_WIDTH;
|
---|
| 140 |
|
---|
| 141 | if (!frame_adjust_zone_bounds(low, &base, &size))
|
---|
| 142 | return;
|
---|
| 143 |
|
---|
| 144 | pfn_t first = ADDR2PFN(base);
|
---|
| 145 | size_t count = SIZE2FRAMES(size);
|
---|
| 146 | pfn_t conf_frame;
|
---|
| 147 |
|
---|
| 148 | if (low) {
|
---|
[edebc15c] | 149 | /* Interrupt vector frame is blacklisted */
|
---|
[c32a6f37] | 150 | if (first == 0)
|
---|
[edebc15c] | 151 | conf_frame = 1;
|
---|
| 152 | else
|
---|
| 153 | conf_frame = first;
|
---|
[42f60375] | 154 | zone_create(first, count, conf_frame,
|
---|
| 155 | ZONE_AVAILABLE | ZONE_LOWMEM);
|
---|
| 156 | } else {
|
---|
| 157 | conf_frame = zone_external_conf_alloc(count);
|
---|
[7852625] | 158 | if (conf_frame != 0)
|
---|
| 159 | zone_create(first, count, conf_frame,
|
---|
| 160 | ZONE_AVAILABLE | ZONE_HIGHMEM);
|
---|
[42f60375] | 161 | }
|
---|
[7852625] | 162 |
|
---|
[42f60375] | 163 | if (phys_regions_count < MAX_REGIONS) {
|
---|
| 164 | phys_regions[phys_regions_count].start = first;
|
---|
| 165 | phys_regions[phys_regions_count].count = count;
|
---|
| 166 | phys_regions_count++;
|
---|
[edebc15c] | 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[f761f1eb] | 170 |
|
---|
[939dfd7] | 171 | /** Create memory zones
|
---|
| 172 | *
|
---|
[c32a6f37] | 173 | * Walk through available 256 KB chunks of physical
|
---|
[edebc15c] | 174 | * memory and create zones.
|
---|
| 175 | *
|
---|
[6c296a9] | 176 | * Note: It is assumed that the TLB is not yet being
|
---|
| 177 | * used in any way, thus there is no interference.
|
---|
| 178 | *
|
---|
[939dfd7] | 179 | */
|
---|
[ddcc8a0] | 180 | void frame_low_arch_init(void)
|
---|
[f761f1eb] | 181 | {
|
---|
[6c296a9] | 182 | ipl_t ipl = interrupts_disable();
|
---|
[edebc15c] | 183 |
|
---|
[6c296a9] | 184 | /* Clear and initialize TLB */
|
---|
| 185 | cp0_pagemask_write(ZERO_PAGE_MASK);
|
---|
| 186 | cp0_entry_lo0_write(0);
|
---|
| 187 | cp0_entry_lo1_write(0);
|
---|
| 188 | cp0_entry_hi_write(0);
|
---|
| 189 |
|
---|
[98000fb] | 190 | size_t i;
|
---|
[6c296a9] | 191 | for (i = 0; i < TLB_ENTRY_COUNT; i++) {
|
---|
| 192 | cp0_index_write(i);
|
---|
| 193 | tlbwi();
|
---|
| 194 | }
|
---|
[edebc15c] | 195 |
|
---|
| 196 | pfn_t start_frame = 0;
|
---|
| 197 | pfn_t frame;
|
---|
| 198 | bool avail = true;
|
---|
| 199 |
|
---|
| 200 | /* Walk through all 1 MB frames */
|
---|
| 201 | for (frame = 0; frame < ZERO_FRAMES; frame++) {
|
---|
| 202 | if (!frame_available(frame))
|
---|
| 203 | avail = false;
|
---|
| 204 | else {
|
---|
| 205 | if (frame_safe(frame)) {
|
---|
| 206 | entry_lo_t lo0;
|
---|
| 207 | entry_lo_t lo1;
|
---|
| 208 | entry_hi_t hi;
|
---|
| 209 | tlb_prepare_entry_lo(&lo0, false, true, true, false, frame << (ZERO_PAGE_WIDTH - 12));
|
---|
| 210 | tlb_prepare_entry_lo(&lo1, false, false, false, false, 0);
|
---|
| 211 | tlb_prepare_entry_hi(&hi, ZERO_PAGE_ASID, ZERO_PAGE_ADDR);
|
---|
| 212 |
|
---|
[6c296a9] | 213 | cp0_pagemask_write(ZERO_PAGE_MASK);
|
---|
[edebc15c] | 214 | cp0_entry_lo0_write(lo0.value);
|
---|
| 215 | cp0_entry_lo1_write(lo1.value);
|
---|
| 216 | cp0_entry_hi_write(hi.value);
|
---|
[6c296a9] | 217 | cp0_index_write(ZERO_PAGE_TLBI);
|
---|
[edebc15c] | 218 | tlbwi();
|
---|
| 219 |
|
---|
| 220 | ZERO_PAGE_VALUE = 0;
|
---|
| 221 | if (ZERO_PAGE_VALUE != 0)
|
---|
| 222 | avail = false;
|
---|
| 223 | else {
|
---|
| 224 | ZERO_PAGE_VALUE = 0xdeadbeef;
|
---|
| 225 | if (ZERO_PAGE_VALUE != 0xdeadbeef)
|
---|
| 226 | avail = false;
|
---|
[2a1410d] | 227 | #if defined(MACHINE_lgxemul) || defined(MACHINE_bgxemul)
|
---|
[8480714] | 228 | else {
|
---|
| 229 | ZERO_PAGE_VALUE_KSEG1(frame) = 0xaabbccdd;
|
---|
| 230 | if (ZERO_PAGE_VALUE_KSEG1(frame) != 0xaabbccdd)
|
---|
| 231 | avail = false;
|
---|
| 232 | }
|
---|
| 233 | #endif
|
---|
[edebc15c] | 234 | }
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | if (!avail) {
|
---|
[42f60375] | 239 | frame_add_region(start_frame, frame, true);
|
---|
[edebc15c] | 240 | start_frame = frame + 1;
|
---|
| 241 | avail = true;
|
---|
| 242 | }
|
---|
[085d973] | 243 | }
|
---|
[edebc15c] | 244 |
|
---|
[42f60375] | 245 | frame_add_region(start_frame, frame, true);
|
---|
[edebc15c] | 246 |
|
---|
[6c296a9] | 247 | /* Blacklist interrupt vector frame */
|
---|
| 248 | frame_mark_unavailable(0, 1);
|
---|
| 249 |
|
---|
| 250 | /* Cleanup */
|
---|
| 251 | cp0_pagemask_write(ZERO_PAGE_MASK);
|
---|
| 252 | cp0_entry_lo0_write(0);
|
---|
| 253 | cp0_entry_lo1_write(0);
|
---|
| 254 | cp0_entry_hi_write(0);
|
---|
[edebc15c] | 255 | cp0_index_write(ZERO_PAGE_TLBI);
|
---|
| 256 | tlbwi();
|
---|
| 257 |
|
---|
[6c296a9] | 258 | interrupts_restore(ipl);
|
---|
[edebc15c] | 259 | }
|
---|
| 260 |
|
---|
[ddcc8a0] | 261 | void frame_high_arch_init(void)
|
---|
| 262 | {
|
---|
| 263 | }
|
---|
[edebc15c] | 264 |
|
---|
| 265 | void physmem_print(void)
|
---|
| 266 | {
|
---|
[ccb426c] | 267 | printf("[base ] [size ]\n");
|
---|
[edebc15c] | 268 |
|
---|
[98000fb] | 269 | size_t i;
|
---|
[edebc15c] | 270 | for (i = 0; i < phys_regions_count; i++) {
|
---|
[0d387d2] | 271 | printf("%#010x %10u\n",
|
---|
[edebc15c] | 272 | PFN2ADDR(phys_regions[i].start), PFN2ADDR(phys_regions[i].count));
|
---|
| 273 | }
|
---|
[f761f1eb] | 274 | }
|
---|
[b45c443] | 275 |
|
---|
[a6dd361] | 276 | /** @}
|
---|
[b45c443] | 277 | */
|
---|