Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 1 | #ifndef _MM_H |
| 2 | #define _MM_H |
| 3 | |
| 4 | #include <stdbool.h> |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 5 | #include <stdint.h> |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 6 | |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 7 | #include "addr.h" |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 8 | #include "arch_mm.h" |
| 9 | |
| 10 | struct mm_ptable { |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 11 | paddr_t table; |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 12 | uint32_t id; |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 13 | }; |
| 14 | |
| 15 | #define PAGE_SIZE (1 << PAGE_BITS) |
| 16 | |
| 17 | /* The following are arch-independent page mapping modes. */ |
| 18 | #define MM_MODE_R 0x01 /* read */ |
| 19 | #define MM_MODE_W 0x02 /* write */ |
| 20 | #define MM_MODE_X 0x04 /* execute */ |
| 21 | #define MM_MODE_D 0x08 /* device */ |
| 22 | |
| 23 | /* |
| 24 | * This flag indicates that memory allocation must not use locks. This is |
| 25 | * relevant in systems where interlocked operations are only available after |
| 26 | * virtual memory is enabled. |
| 27 | */ |
| 28 | #define MM_MODE_NOSYNC 0x10 |
| 29 | |
| 30 | /* |
| 31 | * This flag indicates that the mapping is intended to be used in a first |
| 32 | * stage translation table, which might have different encodings for the |
| 33 | * attribute bits than the second stage table. |
| 34 | */ |
| 35 | #define MM_MODE_STAGE1 0x20 |
| 36 | |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 37 | /* |
| 38 | * This flag indicates that no TLB invalidations should be issued for the |
| 39 | * changes in the page table. |
| 40 | */ |
| 41 | #define MM_MODE_NOINVALIDATE 0x40 |
| 42 | |
| 43 | bool mm_ptable_init(struct mm_ptable *t, uint32_t id, int mode); |
| 44 | void mm_ptable_dump(struct mm_ptable *t, int mode); |
Andrew Scull | fe636b1 | 2018-07-30 14:15:54 +0100 | [diff] [blame] | 45 | bool mm_ptable_identity_map(struct mm_ptable *t, vaddr_t begin, vaddr_t end, |
| 46 | int mode); |
| 47 | bool mm_ptable_identity_map_page(struct mm_ptable *t, vaddr_t va, int mode); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 48 | bool mm_ptable_unmap(struct mm_ptable *t, vaddr_t begin, vaddr_t end, int mode); |
Wedson Almeida Filho | 2f94ec1 | 2018-07-26 16:00:48 +0100 | [diff] [blame] | 49 | bool mm_ptable_is_mapped(struct mm_ptable *t, vaddr_t addr, int mode); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 50 | void mm_ptable_defrag(struct mm_ptable *t, int mode); |
Wedson Almeida Filho | 84a30a0 | 2018-07-23 20:05:05 +0100 | [diff] [blame] | 51 | bool mm_ptable_unmap_hypervisor(struct mm_ptable *t, int mode); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 52 | |
| 53 | bool mm_init(void); |
Wedson Almeida Filho | 03e767a | 2018-07-30 15:32:03 +0100 | [diff] [blame] | 54 | bool mm_cpu_init(void); |
Andrew Scull | fe636b1 | 2018-07-30 14:15:54 +0100 | [diff] [blame] | 55 | bool mm_identity_map(vaddr_t begin, vaddr_t end, int mode); |
Wedson Almeida Filho | fdf4afc | 2018-07-19 15:45:21 +0100 | [diff] [blame] | 56 | bool mm_unmap(vaddr_t begin, vaddr_t end, int mode); |
| 57 | void mm_defrag(void); |
Wedson Almeida Filho | fed6902 | 2018-07-11 15:39:12 +0100 | [diff] [blame] | 58 | |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 59 | /** |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 60 | * Converts an intermediate physical address to a physical address. Addresses |
| 61 | * are currently identity mapped so this is a simple type convertion. Returns |
| 62 | * true if the address was mapped in the table and the address was converted. |
| 63 | */ |
Andrew Scull | 8dce498 | 2018-08-06 13:02:20 +0100 | [diff] [blame] | 64 | static inline bool mm_ptable_translate_ipa(struct mm_ptable *t, ipaddr_t ipa, |
| 65 | paddr_t *pa) |
Andrew Scull | 265ada9 | 2018-07-30 15:19:01 +0100 | [diff] [blame] | 66 | { |
| 67 | /* TODO: the ptable functions map physical to virtual addresses but they |
| 68 | * should really be mapping to intermediate physical addresses. |
| 69 | * It might be better to have different interfaces to the mm functions? |
| 70 | * This might also mean ipaddr_t should be used when building the VM |
| 71 | * tables too? |
| 72 | * */ |
| 73 | if (mm_ptable_is_mapped(t, va_init(ipa_addr(ipa)), 0)) { |
| 74 | *pa = pa_init(ipa_addr(ipa)); |
| 75 | return true; |
| 76 | } |
| 77 | return false; |
| 78 | } |
| 79 | |
Andrew Scull | 4f170f5 | 2018-07-19 12:58:20 +0100 | [diff] [blame] | 80 | #endif /* _MM_H */ |