xref: /openbmc/qemu/include/system/ram_addr.h (revision 637a8b25a6ff233540b3d1b656359294f5dfb33f)
1 /*
2  * Declarations for cpu physical memory functions
3  *
4  * Copyright 2011 Red Hat, Inc. and/or its affiliates
5  *
6  * Authors:
7  *  Avi Kivity <avi@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or
10  * later.  See the COPYING file in the top-level directory.
11  *
12  */
13 
14 /*
15  * This header is for use by exec.c and memory.c ONLY.  Do not include it.
16  * The functions declared here will be removed soon.
17  */
18 
19 #ifndef SYSTEM_RAM_ADDR_H
20 #define SYSTEM_RAM_ADDR_H
21 
22 #include "system/ramblock.h"
23 #include "exec/target_page.h"
24 #include "exec/hwaddr.h"
25 
26 extern uint64_t total_dirty_pages;
27 
28 /**
29  * clear_bmap_size: calculate clear bitmap size
30  *
31  * @pages: number of guest pages
32  * @shift: guest page number shift
33  *
34  * Returns: number of bits for the clear bitmap
35  */
36 static inline long clear_bmap_size(uint64_t pages, uint8_t shift)
37 {
38     return DIV_ROUND_UP(pages, 1UL << shift);
39 }
40 
41 /**
42  * clear_bmap_set: set clear bitmap for the page range.  Must be with
43  * bitmap_mutex held.
44  *
45  * @rb: the ramblock to operate on
46  * @start: the start page number
47  * @size: number of pages to set in the bitmap
48  *
49  * Returns: None
50  */
51 static inline void clear_bmap_set(RAMBlock *rb, uint64_t start,
52                                   uint64_t npages)
53 {
54     uint8_t shift = rb->clear_bmap_shift;
55 
56     bitmap_set(rb->clear_bmap, start >> shift, clear_bmap_size(npages, shift));
57 }
58 
59 /**
60  * clear_bmap_test_and_clear: test clear bitmap for the page, clear if set.
61  * Must be with bitmap_mutex held.
62  *
63  * @rb: the ramblock to operate on
64  * @page: the page number to check
65  *
66  * Returns: true if the bit was set, false otherwise
67  */
68 static inline bool clear_bmap_test_and_clear(RAMBlock *rb, uint64_t page)
69 {
70     uint8_t shift = rb->clear_bmap_shift;
71 
72     return bitmap_test_and_clear(rb->clear_bmap, page >> shift, 1);
73 }
74 
75 static inline unsigned long int ramblock_recv_bitmap_offset(void *host_addr,
76                                                             RAMBlock *rb)
77 {
78     uint64_t host_addr_offset =
79             (uint64_t)(uintptr_t)(host_addr - (void *)rb->host);
80     return host_addr_offset >> TARGET_PAGE_BITS;
81 }
82 
83 /**
84  * qemu_ram_alloc_from_file,
85  * qemu_ram_alloc_from_fd:  Allocate a ram block from the specified backing
86  *                          file or device
87  *
88  * Parameters:
89  *  @size: the size in bytes of the ram block
90  *  @max_size: the maximum size of the block after resizing
91  *  @mr: the memory region where the ram block is
92  *  @resized: callback after calls to qemu_ram_resize
93  *  @ram_flags: RamBlock flags. Supported flags: RAM_SHARED, RAM_PMEM,
94  *              RAM_NORESERVE, RAM_PROTECTED, RAM_NAMED_FILE, RAM_READONLY,
95  *              RAM_READONLY_FD, RAM_GUEST_MEMFD
96  *  @mem_path or @fd: specify the backing file or device
97  *  @offset: Offset into target file
98  *  @grow: extend file if necessary (but an empty file is always extended).
99  *  @errp: pointer to Error*, to store an error if it happens
100  *
101  * Return:
102  *  On success, return a pointer to the ram block.
103  *  On failure, return NULL.
104  */
105 typedef void (*qemu_ram_resize_cb)(const char *, uint64_t length, void *host);
106 
107 RAMBlock *qemu_ram_alloc_from_file(ram_addr_t size, MemoryRegion *mr,
108                                    uint32_t ram_flags, const char *mem_path,
109                                    off_t offset, Error **errp);
110 RAMBlock *qemu_ram_alloc_from_fd(ram_addr_t size, ram_addr_t max_size,
111                                  qemu_ram_resize_cb resized, MemoryRegion *mr,
112                                  uint32_t ram_flags, int fd, off_t offset,
113                                  bool grow,
114                                  Error **errp);
115 
116 RAMBlock *qemu_ram_alloc_from_ptr(ram_addr_t size, void *host,
117                                   MemoryRegion *mr, Error **errp);
118 RAMBlock *qemu_ram_alloc(ram_addr_t size, uint32_t ram_flags, MemoryRegion *mr,
119                          Error **errp);
120 RAMBlock *qemu_ram_alloc_resizeable(ram_addr_t size, ram_addr_t max_size,
121                                     qemu_ram_resize_cb resized,
122                                     MemoryRegion *mr, Error **errp);
123 void qemu_ram_free(RAMBlock *block);
124 
125 int qemu_ram_resize(RAMBlock *block, ram_addr_t newsize, Error **errp);
126 
127 void qemu_ram_msync(RAMBlock *block, ram_addr_t start, ram_addr_t length);
128 
129 /* Clear whole block of mem */
130 static inline void qemu_ram_block_writeback(RAMBlock *block)
131 {
132     qemu_ram_msync(block, 0, block->used_length);
133 }
134 
135 #endif
136