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 <sys/types.h>
11 #include <vm/page.h>
12 #include <zircon/compiler.h>
13 #include <zircon/types.h>
14 
15 // physical allocator
16 typedef struct pmm_arena_info {
17     char name[16];
18 
19     uint flags;
20     uint priority;
21 
22     paddr_t base;
23     size_t size;
24 } pmm_arena_info_t;
25 
26 #define PMM_ARENA_FLAG_LO_MEM (0x1) // this arena is contained within architecturally-defined 'low memory'
27 
28 // Add a pre-filled memory arena to the physical allocator.
29 // The arena data will be copied.
30 zx_status_t pmm_add_arena(const pmm_arena_info_t* arena) __NONNULL((1));
31 
32 // flags for allocation routines below
33 #define PMM_ALLOC_FLAG_ANY (0x0)    // no restrictions on which arena to allocate from
34 #define PMM_ALLOC_FLAG_LO_MEM (0x1) // allocate only from arenas marked LO_MEM
35 
36 // Allocate count pages of physical memory, adding to the tail of the passed list.
37 // The list must be initialized.
38 zx_status_t pmm_alloc_pages(size_t count, uint alloc_flags, list_node* list) __NONNULL((3));
39 
40 // Allocate a single page of physical memory.
41 zx_status_t pmm_alloc_page(uint alloc_flags, vm_page** p) __NONNULL((2));
42 zx_status_t pmm_alloc_page(uint alloc_flags, paddr_t* pa) __NONNULL((2));
43 zx_status_t pmm_alloc_page(uint alloc_flags, vm_page** p, paddr_t* pa) __NONNULL((2, 3));
44 
45 // Allocate a specific range of physical pages, adding to the tail of the passed list.
46 zx_status_t pmm_alloc_range(paddr_t address, size_t count, list_node* list) __NONNULL((3));
47 
48 // Allocate a run of contiguous pages, aligned on log2 byte boundary (0-31).
49 // Return the base address of the run in the physical address pointer and
50 // append the allocate page structures to the tail of the passed in list.
51 zx_status_t pmm_alloc_contiguous(size_t count, uint alloc_flags, uint8_t align_log2,
52                                  paddr_t* pa, list_node* list) __NONNULL((4, 5));
53 
54 // Free a list of physical pages.
55 void pmm_free(list_node* list) __NONNULL((1));
56 
57 // Free a single page.
58 void pmm_free_page(vm_page_t* page) __NONNULL((1));
59 
60 // Return count of unallocated physical pages in system.
61 uint64_t pmm_count_free_pages();
62 
63 // Return amount of physical memory in system, in bytes.
64 uint64_t pmm_count_total_bytes();
65 
66 // Counts the number of pages in every state. For every page in every arena,
67 // increments the corresponding VM_PAGE_STATE_*-indexed entry of
68 // |state_count|. Does not zero out the entries first.
69 void pmm_count_total_states(size_t state_count[VM_PAGE_STATE_COUNT_]) __NONNULL((1));
70 
71 // virtual to physical
72 paddr_t vaddr_to_paddr(const void* va);
73 
74 // paddr to vm_page_t
75 vm_page_t* paddr_to_vm_page(paddr_t addr);
76