blob: d5752f45ff180915362ed716640e5aff134e584b [file] [log] [blame]
Andrew Scull18834872018-10-12 11:48:09 +01001/*
2 * Copyright 2018 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Andrew Scullfbc938a2018-08-20 14:09:28 +010017#pragma once
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010018
Andrew Scull4e5f8142018-10-12 14:37:19 +010019#include <assert.h>
20#include <stdalign.h>
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010021#include <stdbool.h>
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010022#include <stdint.h>
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010023
Andrew Scull18c78fc2018-08-20 12:57:41 +010024#include "hf/arch/mm.h"
25
26#include "hf/addr.h"
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010027
Andrew Scull4e5f8142018-10-12 14:37:19 +010028#define PAGE_SIZE (1 << PAGE_BITS)
29#define MM_PTE_PER_PAGE (PAGE_SIZE / sizeof(pte_t))
30
31struct mm_page_table {
32 alignas(PAGE_SIZE) pte_t entries[MM_PTE_PER_PAGE];
33};
34static_assert(sizeof(struct mm_page_table) == PAGE_SIZE,
35 "A page table must take exactly one page.");
36static_assert(alignof(struct mm_page_table) == PAGE_SIZE,
37 "A page table must be page aligned.");
38
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010039struct mm_ptable {
Andrew Scull265ada92018-07-30 15:19:01 +010040 paddr_t table;
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010041};
42
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010043/* The following are arch-independent page mapping modes. */
44#define MM_MODE_R 0x01 /* read */
45#define MM_MODE_W 0x02 /* write */
46#define MM_MODE_X 0x04 /* execute */
47#define MM_MODE_D 0x08 /* device */
48
Andrew Walbranc3910f72018-11-27 14:24:36 +000049/**
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010050 * This flag indicates that memory allocation must not use locks. This is
51 * relevant in systems where interlocked operations are only available after
52 * virtual memory is enabled.
53 */
54#define MM_MODE_NOSYNC 0x10
55
Andrew Walbranc3910f72018-11-27 14:24:36 +000056/**
Wedson Almeida Filhofed69022018-07-11 15:39:12 +010057 * This flag indicates that the mapping is intended to be used in a first
58 * stage translation table, which might have different encodings for the
59 * attribute bits than the second stage table.
60 */
61#define MM_MODE_STAGE1 0x20
62
Andrew Walbranc3910f72018-11-27 14:24:36 +000063/**
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010064 * This flag indicates that no TLB invalidations should be issued for the
65 * changes in the page table.
66 */
67#define MM_MODE_NOINVALIDATE 0x40
68
Andrew Scull8c3a63a2018-09-20 13:38:34 +010069bool mm_ptable_init(struct mm_ptable *t, int mode);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010070void mm_ptable_dump(struct mm_ptable *t, int mode);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010071void mm_ptable_defrag(struct mm_ptable *t, int mode);
Wedson Almeida Filho84a30a02018-07-23 20:05:05 +010072bool mm_ptable_unmap_hypervisor(struct mm_ptable *t, int mode);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010073
Andrew Scull80871322018-08-06 12:04:09 +010074bool mm_vm_identity_map(struct mm_ptable *t, paddr_t begin, paddr_t end,
75 int mode, ipaddr_t *ipa);
Andrew Scull80871322018-08-06 12:04:09 +010076bool mm_vm_unmap(struct mm_ptable *t, paddr_t begin, paddr_t end, int mode);
77bool mm_vm_is_mapped(struct mm_ptable *t, ipaddr_t ipa, int mode);
78bool mm_vm_translate(struct mm_ptable *t, ipaddr_t ipa, paddr_t *pa);
79
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010080bool mm_init(void);
Wedson Almeida Filho03e767a2018-07-30 15:32:03 +010081bool mm_cpu_init(void);
Andrew Scull80871322018-08-06 12:04:09 +010082void *mm_identity_map(paddr_t begin, paddr_t end, int mode);
83bool mm_unmap(paddr_t begin, paddr_t end, int mode);
Wedson Almeida Filhofdf4afc2018-07-19 15:45:21 +010084void mm_defrag(void);