[8b6aa39] | 1 | /*
|
---|
| 2 | * Copyright (c) 2016 Martin Decky
|
---|
| 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 |
|
---|
[c5429fe] | 29 | /** @addtogroup kernel_riscv64_mm
|
---|
[8b6aa39] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | #include <mm/frame.h>
|
---|
[ccc362a1] | 34 | #include <arch/boot/boot.h>
|
---|
[8b6aa39] | 35 | #include <arch/mm/frame.h>
|
---|
[ccc362a1] | 36 | #include <arch/drivers/ucb.h>
|
---|
[8b6aa39] | 37 | #include <mm/as.h>
|
---|
| 38 | #include <config.h>
|
---|
| 39 | #include <panic.h>
|
---|
| 40 | #include <debug.h>
|
---|
| 41 | #include <align.h>
|
---|
| 42 | #include <macros.h>
|
---|
| 43 |
|
---|
[ccc362a1] | 44 | uintptr_t physmem_start;
|
---|
| 45 | uintptr_t htif_frame;
|
---|
| 46 | uintptr_t pt_frame;
|
---|
| 47 | memmap_t memmap;
|
---|
[8b6aa39] | 48 |
|
---|
| 49 | void physmem_print(void)
|
---|
| 50 | {
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[ccc362a1] | 53 | static void frame_common_arch_init(bool low)
|
---|
| 54 | {
|
---|
| 55 | pfn_t minconf =
|
---|
| 56 | max3(ADDR2PFN(physmem_start), htif_frame + 1, pt_frame + 1);
|
---|
[a35b458] | 57 |
|
---|
[ccc362a1] | 58 | for (size_t i = 0; i < memmap.cnt; i++) {
|
---|
| 59 | /* To be safe, make the available zone possibly smaller */
|
---|
| 60 | uintptr_t base = ALIGN_UP((uintptr_t) memmap.zones[i].start,
|
---|
| 61 | FRAME_SIZE);
|
---|
| 62 | size_t size = ALIGN_DOWN(memmap.zones[i].size -
|
---|
| 63 | (base - ((uintptr_t) memmap.zones[i].start)), FRAME_SIZE);
|
---|
[a35b458] | 64 |
|
---|
[ccc362a1] | 65 | if (!frame_adjust_zone_bounds(low, &base, &size))
|
---|
| 66 | return;
|
---|
[a35b458] | 67 |
|
---|
[ccc362a1] | 68 | pfn_t pfn = ADDR2PFN(base);
|
---|
| 69 | size_t count = SIZE2FRAMES(size);
|
---|
| 70 | pfn_t conf;
|
---|
[a35b458] | 71 |
|
---|
[ccc362a1] | 72 | if (low) {
|
---|
| 73 | if ((minconf < pfn) || (minconf >= pfn + count))
|
---|
| 74 | conf = pfn;
|
---|
| 75 | else
|
---|
| 76 | conf = minconf;
|
---|
[a35b458] | 77 |
|
---|
[ccc362a1] | 78 | zone_create(pfn, count, conf,
|
---|
| 79 | ZONE_AVAILABLE | ZONE_LOWMEM);
|
---|
| 80 | } else {
|
---|
| 81 | conf = zone_external_conf_alloc(count);
|
---|
| 82 | if (conf != 0)
|
---|
| 83 | zone_create(pfn, count, conf,
|
---|
| 84 | ZONE_AVAILABLE | ZONE_HIGHMEM);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
[8b6aa39] | 88 |
|
---|
| 89 | void frame_low_arch_init(void)
|
---|
| 90 | {
|
---|
[ccc362a1] | 91 | frame_common_arch_init(true);
|
---|
[a35b458] | 92 |
|
---|
[ccc362a1] | 93 | frame_mark_unavailable(htif_frame, 1);
|
---|
| 94 | frame_mark_unavailable(pt_frame, 1);
|
---|
[8b6aa39] | 95 | }
|
---|
| 96 |
|
---|
| 97 | void frame_high_arch_init(void)
|
---|
| 98 | {
|
---|
[ccc362a1] | 99 | frame_common_arch_init(false);
|
---|
[8b6aa39] | 100 | }
|
---|
| 101 |
|
---|
| 102 | /** @}
|
---|
| 103 | */
|
---|