1 // Copyright 2017 The Fuchsia Authors
2 // Copyright (c) 2014 Travis Geiselbrecht
3 //
4 // Use of this source code is governed by a MIT-style
5 // license that can be found in the LICENSE file or at
6 // https://opensource.org/licenses/MIT
7 
8 #pragma once
9 
10 #include <arch/defines.h>
11 
12 // The kernel physmap is a region of the kernel where all of useful physical memory
13 // is mapped in one large chunk. It's up to the individual architecture to decide
14 // how much to map but it's usually a fairly large chunk at the base of the kernel
15 // address space.
16 
17 #define PHYSMAP_BASE (KERNEL_ASPACE_BASE)
18 #define PHYSMAP_SIZE (ARCH_PHYSMAP_SIZE)
19 #define PHYSMAP_BASE_PHYS (0)
20 
21 #ifndef __ASSEMBLER__
22 
23 #include <arch.h>
24 #include <assert.h>
25 #include <inttypes.h>
26 #include <vm/vm.h>
27 #include <zircon/compiler.h>
28 
29 __BEGIN_CDECLS
30 
31 // check to see if an address is in the physmap virtually and physically
is_physmap_addr(const void * addr)32 static inline bool is_physmap_addr(const void* addr) {
33     return ((uintptr_t)addr >= PHYSMAP_BASE &&
34             (uintptr_t)addr - PHYSMAP_BASE < PHYSMAP_SIZE);
35 }
36 
is_physmap_phys_addr(paddr_t pa)37 static inline bool is_physmap_phys_addr(paddr_t pa) {
38     return (
39 #if PHYSMAP_BASE_PHYS != 0
40         pa >= PHYSMAP_BASE_PHYS &&
41 #endif
42         pa - PHYSMAP_BASE_PHYS < PHYSMAP_SIZE);
43 }
44 
45 // physical to virtual, returning pointer in the big kernel map
paddr_to_physmap(paddr_t pa)46 static inline void* paddr_to_physmap(paddr_t pa) {
47     DEBUG_ASSERT_MSG(is_physmap_phys_addr(pa), "paddr %#" PRIxPTR "\n", pa);
48 
49     return (void*)(pa - PHYSMAP_BASE_PHYS + PHYSMAP_BASE);
50 }
51 
52 // given a pointer into the physmap, reverse back to a physical address
physmap_to_paddr(const void * addr)53 static inline paddr_t physmap_to_paddr(const void* addr) {
54     DEBUG_ASSERT_MSG(is_physmap_addr(addr), "vaddr %p\n", addr);
55 
56     return (uintptr_t)addr - PHYSMAP_BASE + PHYSMAP_BASE_PHYS;
57 }
58 
59 __END_CDECLS
60 
61 #endif // !__ASSEMBLER__
62