]> git.proxmox.com Git - mirror_qemu.git/blame - include/exec/memory.h
cpu: Change qemu_init_vcpu() argument to CPUState
[mirror_qemu.git] / include / exec / memory.h
CommitLineData
093bc2cd
AK
1/*
2 * Physical memory management API
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. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
14#ifndef MEMORY_H
15#define MEMORY_H
16
17#ifndef CONFIG_USER_ONLY
18
19#include <stdint.h>
20#include <stdbool.h>
21#include "qemu-common.h"
022c62cb
PB
22#include "exec/cpu-common.h"
23#include "exec/hwaddr.h"
1de7afc9 24#include "qemu/queue.h"
022c62cb
PB
25#include "exec/iorange.h"
26#include "exec/ioport.h"
1de7afc9 27#include "qemu/int128.h"
06866575 28#include "qemu/notify.h"
093bc2cd 29
052e87b0
PB
30#define MAX_PHYS_ADDR_SPACE_BITS 62
31#define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
32
093bc2cd 33typedef struct MemoryRegionOps MemoryRegionOps;
627a0e90 34typedef struct MemoryRegionPortio MemoryRegionPortio;
74901c3b 35typedef struct MemoryRegionMmio MemoryRegionMmio;
093bc2cd
AK
36
37/* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
38 * registration.
39 */
40#define DIRTY_MEMORY_VGA 0
41#define DIRTY_MEMORY_CODE 1
42#define DIRTY_MEMORY_MIGRATION 3
43
74901c3b
AK
44struct MemoryRegionMmio {
45 CPUReadMemoryFunc *read[3];
46 CPUWriteMemoryFunc *write[3];
47};
48
a2d33521
AK
49/* Internal use; thunks between old-style IORange and MemoryRegions. */
50typedef struct MemoryRegionIORange MemoryRegionIORange;
51struct MemoryRegionIORange {
52 IORange iorange;
53 MemoryRegion *mr;
a8170e5e 54 hwaddr offset;
a2d33521
AK
55};
56
30951157
AK
57typedef struct IOMMUTLBEntry IOMMUTLBEntry;
58
59/* See address_space_translate: bit 0 is read, bit 1 is write. */
60typedef enum {
61 IOMMU_NONE = 0,
62 IOMMU_RO = 1,
63 IOMMU_WO = 2,
64 IOMMU_RW = 3,
65} IOMMUAccessFlags;
66
67struct IOMMUTLBEntry {
68 AddressSpace *target_as;
69 hwaddr iova;
70 hwaddr translated_addr;
71 hwaddr addr_mask; /* 0xfff = 4k translation */
72 IOMMUAccessFlags perm;
73};
74
093bc2cd
AK
75/*
76 * Memory region callbacks
77 */
78struct MemoryRegionOps {
79 /* Read from the memory region. @addr is relative to @mr; @size is
80 * in bytes. */
81 uint64_t (*read)(void *opaque,
a8170e5e 82 hwaddr addr,
093bc2cd
AK
83 unsigned size);
84 /* Write to the memory region. @addr is relative to @mr; @size is
85 * in bytes. */
86 void (*write)(void *opaque,
a8170e5e 87 hwaddr addr,
093bc2cd
AK
88 uint64_t data,
89 unsigned size);
90
91 enum device_endian endianness;
92 /* Guest-visible constraints: */
93 struct {
94 /* If nonzero, specify bounds on access sizes beyond which a machine
95 * check is thrown.
96 */
97 unsigned min_access_size;
98 unsigned max_access_size;
99 /* If true, unaligned accesses are supported. Otherwise unaligned
100 * accesses throw machine checks.
101 */
102 bool unaligned;
897fa7cf
AK
103 /*
104 * If present, and returns #false, the transaction is not accepted
105 * by the device (and results in machine dependent behaviour such
106 * as a machine check exception).
107 */
a8170e5e 108 bool (*accepts)(void *opaque, hwaddr addr,
897fa7cf 109 unsigned size, bool is_write);
093bc2cd
AK
110 } valid;
111 /* Internal implementation constraints: */
112 struct {
113 /* If nonzero, specifies the minimum size implemented. Smaller sizes
114 * will be rounded upwards and a partial result will be returned.
115 */
116 unsigned min_access_size;
117 /* If nonzero, specifies the maximum size implemented. Larger sizes
118 * will be done as a series of accesses with smaller sizes.
119 */
120 unsigned max_access_size;
121 /* If true, unaligned accesses are supported. Otherwise all accesses
122 * are converted to (possibly multiple) naturally aligned accesses.
123 */
124 bool unaligned;
125 } impl;
627a0e90
AK
126
127 /* If .read and .write are not present, old_portio may be used for
128 * backwards compatibility with old portio registration
129 */
130 const MemoryRegionPortio *old_portio;
74901c3b
AK
131 /* If .read and .write are not present, old_mmio may be used for
132 * backwards compatibility with old mmio registration
133 */
134 const MemoryRegionMmio old_mmio;
093bc2cd
AK
135};
136
30951157
AK
137typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
138
139struct MemoryRegionIOMMUOps {
140 /* Return a TLB entry that contains a given address. */
141 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr);
142};
143
093bc2cd 144typedef struct CoalescedMemoryRange CoalescedMemoryRange;
3e9d69e7 145typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
093bc2cd
AK
146
147struct MemoryRegion {
148 /* All fields are private - violators will be prosecuted */
149 const MemoryRegionOps *ops;
30951157 150 const MemoryRegionIOMMUOps *iommu_ops;
093bc2cd
AK
151 void *opaque;
152 MemoryRegion *parent;
08dafab4 153 Int128 size;
a8170e5e 154 hwaddr addr;
545e92e0 155 void (*destructor)(MemoryRegion *mr);
093bc2cd 156 ram_addr_t ram_addr;
b3b00c78 157 bool subpage;
14a3c10a 158 bool terminates;
5f9a5ea1 159 bool romd_mode;
8ea9252a 160 bool ram;
fb1cd6f9 161 bool readonly; /* For RAM regions */
6bba19ba 162 bool enabled;
75c578dc 163 bool rom_device;
1660e72d 164 bool warning_printed; /* For reservations */
d410515e 165 bool flush_coalesced_mmio;
093bc2cd 166 MemoryRegion *alias;
a8170e5e 167 hwaddr alias_offset;
093bc2cd
AK
168 unsigned priority;
169 bool may_overlap;
170 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
171 QTAILQ_ENTRY(MemoryRegion) subregions_link;
172 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
173 const char *name;
5a583347 174 uint8_t dirty_log_mask;
3e9d69e7
AK
175 unsigned ioeventfd_nb;
176 MemoryRegionIoeventfd *ioeventfds;
06866575 177 NotifierList iommu_notify;
093bc2cd
AK
178};
179
627a0e90
AK
180struct MemoryRegionPortio {
181 uint32_t offset;
182 uint32_t len;
183 unsigned size;
184 IOPortReadFunc *read;
185 IOPortWriteFunc *write;
186};
187
2dd30228 188#define PORTIO_END_OF_LIST() { }
627a0e90 189
9ad2bbc1
AK
190/**
191 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
192 */
193struct AddressSpace {
194 /* All fields are private. */
7dca8043 195 char *name;
9ad2bbc1
AK
196 MemoryRegion *root;
197 struct FlatView *current_map;
198 int ioeventfd_nb;
199 struct MemoryRegionIoeventfd *ioeventfds;
ac1970fb 200 struct AddressSpaceDispatch *dispatch;
0d673e36 201 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
9ad2bbc1
AK
202};
203
e2177955
AK
204/**
205 * MemoryRegionSection: describes a fragment of a #MemoryRegion
206 *
207 * @mr: the region, or %NULL if empty
7664e80c 208 * @address_space: the address space the region is mapped in
e2177955
AK
209 * @offset_within_region: the beginning of the section, relative to @mr's start
210 * @size: the size of the section; will not exceed @mr's boundaries
211 * @offset_within_address_space: the address of the first byte of the section
212 * relative to the region's address space
7a8499e8 213 * @readonly: writes to this section are ignored
e2177955
AK
214 */
215struct MemoryRegionSection {
216 MemoryRegion *mr;
f6790af6 217 AddressSpace *address_space;
a8170e5e 218 hwaddr offset_within_region;
052e87b0 219 Int128 size;
a8170e5e 220 hwaddr offset_within_address_space;
7a8499e8 221 bool readonly;
e2177955
AK
222};
223
7664e80c
AK
224typedef struct MemoryListener MemoryListener;
225
226/**
227 * MemoryListener: callbacks structure for updates to the physical memory map
228 *
229 * Allows a component to adjust to changes in the guest-visible memory map.
230 * Use with memory_listener_register() and memory_listener_unregister().
231 */
232struct MemoryListener {
50c1e149
AK
233 void (*begin)(MemoryListener *listener);
234 void (*commit)(MemoryListener *listener);
7664e80c
AK
235 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
236 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
50c1e149 237 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
7664e80c
AK
238 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section);
239 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section);
240 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
241 void (*log_global_start)(MemoryListener *listener);
242 void (*log_global_stop)(MemoryListener *listener);
80a1ea37 243 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
753d5e14 244 bool match_data, uint64_t data, EventNotifier *e);
80a1ea37 245 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
753d5e14 246 bool match_data, uint64_t data, EventNotifier *e);
95d2994a 247 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
a8170e5e 248 hwaddr addr, hwaddr len);
95d2994a 249 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
a8170e5e 250 hwaddr addr, hwaddr len);
72e22d2f
AK
251 /* Lower = earlier (during add), later (during del) */
252 unsigned priority;
f6790af6 253 AddressSpace *address_space_filter;
72e22d2f 254 QTAILQ_ENTRY(MemoryListener) link;
7664e80c
AK
255};
256
093bc2cd
AK
257/**
258 * memory_region_init: Initialize a memory region
259 *
69ddaf66 260 * The region typically acts as a container for other memory regions. Use
093bc2cd
AK
261 * memory_region_add_subregion() to add subregions.
262 *
263 * @mr: the #MemoryRegion to be initialized
264 * @name: used for debugging; not visible to the user or ABI
265 * @size: size of the region; any subregions beyond this size will be clipped
266 */
267void memory_region_init(MemoryRegion *mr,
268 const char *name,
269 uint64_t size);
270/**
271 * memory_region_init_io: Initialize an I/O memory region.
272 *
69ddaf66 273 * Accesses into the region will cause the callbacks in @ops to be called.
093bc2cd
AK
274 * if @size is nonzero, subregions will be clipped to @size.
275 *
276 * @mr: the #MemoryRegion to be initialized.
277 * @ops: a structure containing read and write callbacks to be used when
278 * I/O is performed on the region.
279 * @opaque: passed to to the read and write callbacks of the @ops structure.
280 * @name: used for debugging; not visible to the user or ABI
281 * @size: size of the region.
282 */
283void memory_region_init_io(MemoryRegion *mr,
284 const MemoryRegionOps *ops,
285 void *opaque,
286 const char *name,
287 uint64_t size);
288
289/**
290 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
69ddaf66 291 * region will modify memory directly.
093bc2cd
AK
292 *
293 * @mr: the #MemoryRegion to be initialized.
c5705a77 294 * @name: the name of the region.
093bc2cd
AK
295 * @size: size of the region.
296 */
297void memory_region_init_ram(MemoryRegion *mr,
093bc2cd
AK
298 const char *name,
299 uint64_t size);
300
301/**
1a7e8cae
BZ
302 * memory_region_init_ram_ptr: Initialize RAM memory region from a
303 * user-provided pointer. Accesses into the
304 * region will modify memory directly.
093bc2cd
AK
305 *
306 * @mr: the #MemoryRegion to be initialized.
c5705a77 307 * @name: the name of the region.
093bc2cd
AK
308 * @size: size of the region.
309 * @ptr: memory to be mapped; must contain at least @size bytes.
310 */
311void memory_region_init_ram_ptr(MemoryRegion *mr,
093bc2cd
AK
312 const char *name,
313 uint64_t size,
314 void *ptr);
315
316/**
317 * memory_region_init_alias: Initialize a memory region that aliases all or a
318 * part of another memory region.
319 *
320 * @mr: the #MemoryRegion to be initialized.
321 * @name: used for debugging; not visible to the user or ABI
322 * @orig: the region to be referenced; @mr will be equivalent to
323 * @orig between @offset and @offset + @size - 1.
324 * @offset: start of the section in @orig to be referenced.
325 * @size: size of the region.
326 */
327void memory_region_init_alias(MemoryRegion *mr,
328 const char *name,
329 MemoryRegion *orig,
a8170e5e 330 hwaddr offset,
093bc2cd 331 uint64_t size);
d0a9b5bc
AK
332
333/**
334 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
335 * handled via callbacks.
336 *
337 * @mr: the #MemoryRegion to be initialized.
338 * @ops: callbacks for write access handling.
c5705a77 339 * @name: the name of the region.
d0a9b5bc
AK
340 * @size: size of the region.
341 */
342void memory_region_init_rom_device(MemoryRegion *mr,
343 const MemoryRegionOps *ops,
75f5941c 344 void *opaque,
d0a9b5bc
AK
345 const char *name,
346 uint64_t size);
347
1660e72d
JK
348/**
349 * memory_region_init_reservation: Initialize a memory region that reserves
350 * I/O space.
351 *
352 * A reservation region primariy serves debugging purposes. It claims I/O
353 * space that is not supposed to be handled by QEMU itself. Any access via
354 * the memory API will cause an abort().
355 *
356 * @mr: the #MemoryRegion to be initialized
357 * @name: used for debugging; not visible to the user or ABI
358 * @size: size of the region.
359 */
360void memory_region_init_reservation(MemoryRegion *mr,
361 const char *name,
362 uint64_t size);
30951157
AK
363
364/**
365 * memory_region_init_iommu: Initialize a memory region that translates
366 * addresses
367 *
368 * An IOMMU region translates addresses and forwards accesses to a target
369 * memory region.
370 *
371 * @mr: the #MemoryRegion to be initialized
372 * @ops: a function that translates addresses into the @target region
373 * @name: used for debugging; not visible to the user or ABI
374 * @size: size of the region.
375 */
376void memory_region_init_iommu(MemoryRegion *mr,
377 const MemoryRegionIOMMUOps *ops,
378 const char *name,
379 uint64_t size);
380
093bc2cd 381/**
69ddaf66 382 * memory_region_destroy: Destroy a memory region and reclaim all resources.
093bc2cd
AK
383 *
384 * @mr: the region to be destroyed. May not currently be a subregion
385 * (see memory_region_add_subregion()) or referenced in an alias
386 * (see memory_region_init_alias()).
387 */
388void memory_region_destroy(MemoryRegion *mr);
389
390/**
391 * memory_region_size: get a memory region's size.
392 *
393 * @mr: the memory region being queried.
394 */
395uint64_t memory_region_size(MemoryRegion *mr);
396
8ea9252a
AK
397/**
398 * memory_region_is_ram: check whether a memory region is random access
399 *
400 * Returns %true is a memory region is random access.
401 *
402 * @mr: the memory region being queried
403 */
404bool memory_region_is_ram(MemoryRegion *mr);
405
fd062573 406/**
5f9a5ea1 407 * memory_region_is_romd: check whether a memory region is in ROMD mode
fd062573 408 *
5f9a5ea1 409 * Returns %true if a memory region is a ROM device and currently set to allow
fd062573
BS
410 * direct reads.
411 *
412 * @mr: the memory region being queried
413 */
414static inline bool memory_region_is_romd(MemoryRegion *mr)
415{
5f9a5ea1 416 return mr->rom_device && mr->romd_mode;
fd062573
BS
417}
418
30951157
AK
419/**
420 * memory_region_is_iommu: check whether a memory region is an iommu
421 *
422 * Returns %true is a memory region is an iommu.
423 *
424 * @mr: the memory region being queried
425 */
426bool memory_region_is_iommu(MemoryRegion *mr);
427
06866575
DG
428/**
429 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
430 *
431 * @mr: the memory region that was changed
432 * @entry: the new entry in the IOMMU translation table. The entry
433 * replaces all old entries for the same virtual I/O address range.
434 * Deleted entries have .@perm == 0.
435 */
436void memory_region_notify_iommu(MemoryRegion *mr,
437 IOMMUTLBEntry entry);
438
439/**
440 * memory_region_register_iommu_notifier: register a notifier for changes to
441 * IOMMU translation entries.
442 *
443 * @mr: the memory region to observe
444 * @n: the notifier to be added; the notifier receives a pointer to an
445 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
446 * valid on exit from the notifier.
447 */
448void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
449
450/**
451 * memory_region_unregister_iommu_notifier: unregister a notifier for
452 * changes to IOMMU translation entries.
453 *
454 * @n: the notifier to be removed.
455 */
456void memory_region_unregister_iommu_notifier(Notifier *n);
457
8991c79b
AK
458/**
459 * memory_region_name: get a memory region's name
460 *
461 * Returns the string that was used to initialize the memory region.
462 *
463 * @mr: the memory region being queried
464 */
465const char *memory_region_name(MemoryRegion *mr);
466
55043ba3
AK
467/**
468 * memory_region_is_logging: return whether a memory region is logging writes
469 *
470 * Returns %true if the memory region is logging writes
471 *
472 * @mr: the memory region being queried
473 */
474bool memory_region_is_logging(MemoryRegion *mr);
475
ce7923da
AK
476/**
477 * memory_region_is_rom: check whether a memory region is ROM
478 *
479 * Returns %true is a memory region is read-only memory.
480 *
481 * @mr: the memory region being queried
482 */
483bool memory_region_is_rom(MemoryRegion *mr);
484
093bc2cd
AK
485/**
486 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
487 *
488 * Returns a host pointer to a RAM memory region (created with
489 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
490 * care.
491 *
492 * @mr: the memory region being queried.
493 */
494void *memory_region_get_ram_ptr(MemoryRegion *mr);
495
093bc2cd
AK
496/**
497 * memory_region_set_log: Turn dirty logging on or off for a region.
498 *
499 * Turns dirty logging on or off for a specified client (display, migration).
500 * Only meaningful for RAM regions.
501 *
502 * @mr: the memory region being updated.
503 * @log: whether dirty logging is to be enabled or disabled.
504 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
505 * %DIRTY_MEMORY_VGA.
506 */
507void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
508
509/**
cd7a45c9
BS
510 * memory_region_get_dirty: Check whether a range of bytes is dirty
511 * for a specified client.
093bc2cd 512 *
cd7a45c9 513 * Checks whether a range of bytes has been written to since the last
093bc2cd
AK
514 * call to memory_region_reset_dirty() with the same @client. Dirty logging
515 * must be enabled.
516 *
517 * @mr: the memory region being queried.
518 * @addr: the address (relative to the start of the region) being queried.
cd7a45c9 519 * @size: the size of the range being queried.
093bc2cd
AK
520 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
521 * %DIRTY_MEMORY_VGA.
522 */
a8170e5e
AK
523bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
524 hwaddr size, unsigned client);
093bc2cd
AK
525
526/**
fd4aa979 527 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
093bc2cd 528 *
fd4aa979
BS
529 * Marks a range of bytes as dirty, after it has been dirtied outside
530 * guest code.
093bc2cd 531 *
fd4aa979 532 * @mr: the memory region being dirtied.
093bc2cd 533 * @addr: the address (relative to the start of the region) being dirtied.
fd4aa979 534 * @size: size of the range being dirtied.
093bc2cd 535 */
a8170e5e
AK
536void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
537 hwaddr size);
093bc2cd 538
6c279db8
JQ
539/**
540 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
541 * for a specified client. It clears them.
542 *
543 * Checks whether a range of bytes has been written to since the last
544 * call to memory_region_reset_dirty() with the same @client. Dirty logging
545 * must be enabled.
546 *
547 * @mr: the memory region being queried.
548 * @addr: the address (relative to the start of the region) being queried.
549 * @size: the size of the range being queried.
550 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
551 * %DIRTY_MEMORY_VGA.
552 */
553bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
554 hwaddr size, unsigned client);
093bc2cd
AK
555/**
556 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
557 * any external TLBs (e.g. kvm)
558 *
559 * Flushes dirty information from accelerators such as kvm and vhost-net
560 * and makes it available to users of the memory API.
561 *
562 * @mr: the region being flushed.
563 */
564void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
565
566/**
567 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
568 * client.
569 *
570 * Marks a range of pages as no longer dirty.
571 *
572 * @mr: the region being updated.
573 * @addr: the start of the subrange being cleaned.
574 * @size: the size of the subrange being cleaned.
575 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
576 * %DIRTY_MEMORY_VGA.
577 */
a8170e5e
AK
578void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
579 hwaddr size, unsigned client);
093bc2cd
AK
580
581/**
582 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
583 *
584 * Allows a memory region to be marked as read-only (turning it into a ROM).
585 * only useful on RAM regions.
586 *
587 * @mr: the region being updated.
588 * @readonly: whether rhe region is to be ROM or RAM.
589 */
590void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
591
d0a9b5bc 592/**
5f9a5ea1 593 * memory_region_rom_device_set_romd: enable/disable ROMD mode
d0a9b5bc
AK
594 *
595 * Allows a ROM device (initialized with memory_region_init_rom_device() to
5f9a5ea1
JK
596 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
597 * device is mapped to guest memory and satisfies read access directly.
598 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
599 * Writes are always handled by the #MemoryRegion.write function.
d0a9b5bc
AK
600 *
601 * @mr: the memory region to be updated
5f9a5ea1 602 * @romd_mode: %true to put the region into ROMD mode
d0a9b5bc 603 */
5f9a5ea1 604void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
d0a9b5bc 605
093bc2cd
AK
606/**
607 * memory_region_set_coalescing: Enable memory coalescing for the region.
608 *
609 * Enabled writes to a region to be queued for later processing. MMIO ->write
610 * callbacks may be delayed until a non-coalesced MMIO is issued.
611 * Only useful for IO regions. Roughly similar to write-combining hardware.
612 *
613 * @mr: the memory region to be write coalesced
614 */
615void memory_region_set_coalescing(MemoryRegion *mr);
616
617/**
618 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
619 * a region.
620 *
621 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
622 * Multiple calls can be issued coalesced disjoint ranges.
623 *
624 * @mr: the memory region to be updated.
625 * @offset: the start of the range within the region to be coalesced.
626 * @size: the size of the subrange to be coalesced.
627 */
628void memory_region_add_coalescing(MemoryRegion *mr,
a8170e5e 629 hwaddr offset,
093bc2cd
AK
630 uint64_t size);
631
632/**
633 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
634 *
635 * Disables any coalescing caused by memory_region_set_coalescing() or
636 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
637 * hardware.
638 *
639 * @mr: the memory region to be updated.
640 */
641void memory_region_clear_coalescing(MemoryRegion *mr);
642
d410515e
JK
643/**
644 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
645 * accesses.
646 *
647 * Ensure that pending coalesced MMIO request are flushed before the memory
648 * region is accessed. This property is automatically enabled for all regions
649 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
650 *
651 * @mr: the memory region to be updated.
652 */
653void memory_region_set_flush_coalesced(MemoryRegion *mr);
654
655/**
656 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
657 * accesses.
658 *
659 * Clear the automatic coalesced MMIO flushing enabled via
660 * memory_region_set_flush_coalesced. Note that this service has no effect on
661 * memory regions that have MMIO coalescing enabled for themselves. For them,
662 * automatic flushing will stop once coalescing is disabled.
663 *
664 * @mr: the memory region to be updated.
665 */
666void memory_region_clear_flush_coalesced(MemoryRegion *mr);
667
3e9d69e7
AK
668/**
669 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
670 * is written to a location.
671 *
672 * Marks a word in an IO region (initialized with memory_region_init_io())
673 * as a trigger for an eventfd event. The I/O callback will not be called.
69ddaf66 674 * The caller must be prepared to handle failure (that is, take the required
3e9d69e7
AK
675 * action if the callback _is_ called).
676 *
677 * @mr: the memory region being updated.
678 * @addr: the address within @mr that is to be monitored
679 * @size: the size of the access to trigger the eventfd
680 * @match_data: whether to match against @data, instead of just @addr
681 * @data: the data to match against the guest write
682 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
683 **/
684void memory_region_add_eventfd(MemoryRegion *mr,
a8170e5e 685 hwaddr addr,
3e9d69e7
AK
686 unsigned size,
687 bool match_data,
688 uint64_t data,
753d5e14 689 EventNotifier *e);
3e9d69e7
AK
690
691/**
69ddaf66 692 * memory_region_del_eventfd: Cancel an eventfd.
3e9d69e7 693 *
69ddaf66
ASRJ
694 * Cancels an eventfd trigger requested by a previous
695 * memory_region_add_eventfd() call.
3e9d69e7
AK
696 *
697 * @mr: the memory region being updated.
698 * @addr: the address within @mr that is to be monitored
699 * @size: the size of the access to trigger the eventfd
700 * @match_data: whether to match against @data, instead of just @addr
701 * @data: the data to match against the guest write
702 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
703 */
704void memory_region_del_eventfd(MemoryRegion *mr,
a8170e5e 705 hwaddr addr,
3e9d69e7
AK
706 unsigned size,
707 bool match_data,
708 uint64_t data,
753d5e14
PB
709 EventNotifier *e);
710
093bc2cd 711/**
69ddaf66 712 * memory_region_add_subregion: Add a subregion to a container.
093bc2cd 713 *
69ddaf66 714 * Adds a subregion at @offset. The subregion may not overlap with other
093bc2cd
AK
715 * subregions (except for those explicitly marked as overlapping). A region
716 * may only be added once as a subregion (unless removed with
717 * memory_region_del_subregion()); use memory_region_init_alias() if you
718 * want a region to be a subregion in multiple locations.
719 *
720 * @mr: the region to contain the new subregion; must be a container
721 * initialized with memory_region_init().
722 * @offset: the offset relative to @mr where @subregion is added.
723 * @subregion: the subregion to be added.
724 */
725void memory_region_add_subregion(MemoryRegion *mr,
a8170e5e 726 hwaddr offset,
093bc2cd
AK
727 MemoryRegion *subregion);
728/**
1a7e8cae
BZ
729 * memory_region_add_subregion_overlap: Add a subregion to a container
730 * with overlap.
093bc2cd 731 *
69ddaf66 732 * Adds a subregion at @offset. The subregion may overlap with other
093bc2cd
AK
733 * subregions. Conflicts are resolved by having a higher @priority hide a
734 * lower @priority. Subregions without priority are taken as @priority 0.
735 * A region may only be added once as a subregion (unless removed with
736 * memory_region_del_subregion()); use memory_region_init_alias() if you
737 * want a region to be a subregion in multiple locations.
738 *
739 * @mr: the region to contain the new subregion; must be a container
740 * initialized with memory_region_init().
741 * @offset: the offset relative to @mr where @subregion is added.
742 * @subregion: the subregion to be added.
743 * @priority: used for resolving overlaps; highest priority wins.
744 */
745void memory_region_add_subregion_overlap(MemoryRegion *mr,
a8170e5e 746 hwaddr offset,
093bc2cd
AK
747 MemoryRegion *subregion,
748 unsigned priority);
e34911c4
AK
749
750/**
751 * memory_region_get_ram_addr: Get the ram address associated with a memory
752 * region
753 *
dabdf394 754 * DO NOT USE THIS FUNCTION. This is a temporary workaround while the Xen
e34911c4
AK
755 * code is being reworked.
756 */
757ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
758
093bc2cd
AK
759/**
760 * memory_region_del_subregion: Remove a subregion.
761 *
762 * Removes a subregion from its container.
763 *
764 * @mr: the container to be updated.
765 * @subregion: the region being removed; must be a current subregion of @mr.
766 */
767void memory_region_del_subregion(MemoryRegion *mr,
768 MemoryRegion *subregion);
769
6bba19ba
AK
770/*
771 * memory_region_set_enabled: dynamically enable or disable a region
772 *
773 * Enables or disables a memory region. A disabled memory region
774 * ignores all accesses to itself and its subregions. It does not
775 * obscure sibling subregions with lower priority - it simply behaves as
776 * if it was removed from the hierarchy.
777 *
778 * Regions default to being enabled.
779 *
780 * @mr: the region to be updated
781 * @enabled: whether to enable or disable the region
782 */
783void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
784
2282e1af
AK
785/*
786 * memory_region_set_address: dynamically update the address of a region
787 *
788 * Dynamically updates the address of a region, relative to its parent.
789 * May be used on regions are currently part of a memory hierarchy.
790 *
791 * @mr: the region to be updated
792 * @addr: new address, relative to parent region
793 */
a8170e5e 794void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
2282e1af 795
4703359e
AK
796/*
797 * memory_region_set_alias_offset: dynamically update a memory alias's offset
798 *
799 * Dynamically updates the offset into the target region that an alias points
800 * to, as if the fourth argument to memory_region_init_alias() has changed.
801 *
802 * @mr: the #MemoryRegion to be updated; should be an alias.
803 * @offset: the new offset into the target memory region
804 */
805void memory_region_set_alias_offset(MemoryRegion *mr,
a8170e5e 806 hwaddr offset);
4703359e 807
e2177955 808/**
73034e9e
PB
809 * memory_region_find: translate an address/size relative to a
810 * MemoryRegion into a #MemoryRegionSection.
e2177955 811 *
73034e9e
PB
812 * Locates the first #MemoryRegion within @mr that overlaps the range
813 * given by @addr and @size.
e2177955
AK
814 *
815 * Returns a #MemoryRegionSection that describes a contiguous overlap.
816 * It will have the following characteristics:
e2177955
AK
817 * .@size = 0 iff no overlap was found
818 * .@mr is non-%NULL iff an overlap was found
819 *
73034e9e
PB
820 * Remember that in the return value the @offset_within_region is
821 * relative to the returned region (in the .@mr field), not to the
822 * @mr argument.
823 *
824 * Similarly, the .@offset_within_address_space is relative to the
825 * address space that contains both regions, the passed and the
826 * returned one. However, in the special case where the @mr argument
827 * has no parent (and thus is the root of the address space), the
828 * following will hold:
829 * .@offset_within_address_space >= @addr
830 * .@offset_within_address_space + .@size <= @addr + @size
831 *
832 * @mr: a MemoryRegion within which @addr is a relative address
833 * @addr: start of the area within @as to be searched
e2177955
AK
834 * @size: size of the area to be searched
835 */
73034e9e 836MemoryRegionSection memory_region_find(MemoryRegion *mr,
a8170e5e 837 hwaddr addr, uint64_t size);
e2177955 838
86e775c6 839/**
1d671369 840 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
86e775c6
AK
841 *
842 * Synchronizes the dirty page log for an entire address space.
1d671369 843 * @as: the address space that contains the memory being synchronized
86e775c6 844 */
1d671369 845void address_space_sync_dirty_bitmap(AddressSpace *as);
86e775c6 846
69ddaf66
ASRJ
847/**
848 * memory_region_transaction_begin: Start a transaction.
849 *
850 * During a transaction, changes will be accumulated and made visible
dabdf394 851 * only when the transaction ends (is committed).
4ef4db86
AK
852 */
853void memory_region_transaction_begin(void);
69ddaf66
ASRJ
854
855/**
856 * memory_region_transaction_commit: Commit a transaction and make changes
857 * visible to the guest.
4ef4db86
AK
858 */
859void memory_region_transaction_commit(void);
860
7664e80c
AK
861/**
862 * memory_listener_register: register callbacks to be called when memory
863 * sections are mapped or unmapped into an address
864 * space
865 *
866 * @listener: an object containing the callbacks to be called
7376e582 867 * @filter: if non-%NULL, only regions in this address space will be observed
7664e80c 868 */
f6790af6 869void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
7664e80c
AK
870
871/**
872 * memory_listener_unregister: undo the effect of memory_listener_register()
873 *
874 * @listener: an object containing the callbacks to be removed
875 */
876void memory_listener_unregister(MemoryListener *listener);
877
878/**
879 * memory_global_dirty_log_start: begin dirty logging for all regions
880 */
881void memory_global_dirty_log_start(void);
882
883/**
1a7e8cae 884 * memory_global_dirty_log_stop: end dirty logging for all regions
7664e80c
AK
885 */
886void memory_global_dirty_log_stop(void);
887
314e2987
BS
888void mtree_info(fprintf_function mon_printf, void *f);
889
9ad2bbc1
AK
890/**
891 * address_space_init: initializes an address space
892 *
893 * @as: an uninitialized #AddressSpace
894 * @root: a #MemoryRegion that routes addesses for the address space
7dca8043
AK
895 * @name: an address space name. The name is only used for debugging
896 * output.
9ad2bbc1 897 */
7dca8043 898void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
9ad2bbc1 899
83f3c251
AK
900
901/**
902 * address_space_destroy: destroy an address space
903 *
904 * Releases all resources associated with an address space. After an address space
905 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
906 * as well.
907 *
908 * @as: address space to be destroyed
909 */
910void address_space_destroy(AddressSpace *as);
911
ac1970fb
AK
912/**
913 * address_space_rw: read from or write to an address space.
914 *
30951157
AK
915 * Return true if the operation hit any unassigned memory or encountered an
916 * IOMMU fault.
fd8aaa76 917 *
ac1970fb
AK
918 * @as: #AddressSpace to be accessed
919 * @addr: address within that address space
920 * @buf: buffer with the data transferred
921 * @is_write: indicates the transfer direction
922 */
fd8aaa76 923bool address_space_rw(AddressSpace *as, hwaddr addr, uint8_t *buf,
ac1970fb
AK
924 int len, bool is_write);
925
926/**
927 * address_space_write: write to address space.
928 *
30951157
AK
929 * Return true if the operation hit any unassigned memory or encountered an
930 * IOMMU fault.
fd8aaa76 931 *
ac1970fb
AK
932 * @as: #AddressSpace to be accessed
933 * @addr: address within that address space
934 * @buf: buffer with the data transferred
935 */
fd8aaa76 936bool address_space_write(AddressSpace *as, hwaddr addr,
ac1970fb
AK
937 const uint8_t *buf, int len);
938
939/**
940 * address_space_read: read from an address space.
941 *
30951157
AK
942 * Return true if the operation hit any unassigned memory or encountered an
943 * IOMMU fault.
fd8aaa76 944 *
ac1970fb
AK
945 * @as: #AddressSpace to be accessed
946 * @addr: address within that address space
947 * @buf: buffer with the data transferred
948 */
fd8aaa76 949bool address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len);
ac1970fb 950
149f54b5 951/* address_space_translate: translate an address range into an address space
5c8a00ce 952 * into a MemoryRegion and an address range into that section
149f54b5
PB
953 *
954 * @as: #AddressSpace to be accessed
955 * @addr: address within that address space
956 * @xlat: pointer to address within the returned memory region section's
957 * #MemoryRegion.
958 * @len: pointer to length
959 * @is_write: indicates the transfer direction
960 */
5c8a00ce
PB
961MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
962 hwaddr *xlat, hwaddr *len,
963 bool is_write);
149f54b5 964
51644ab7
PB
965/* address_space_access_valid: check for validity of accessing an address
966 * space range
967 *
30951157
AK
968 * Check whether memory is assigned to the given address space range, and
969 * access is permitted by any IOMMU regions that are active for the address
970 * space.
51644ab7
PB
971 *
972 * For now, addr and len should be aligned to a page size. This limitation
973 * will be lifted in the future.
974 *
975 * @as: #AddressSpace to be accessed
976 * @addr: address within that address space
977 * @len: length of the area to be checked
978 * @is_write: indicates the transfer direction
979 */
980bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
981
ac1970fb
AK
982/* address_space_map: map a physical memory region into a host virtual address
983 *
984 * May map a subset of the requested range, given by and returned in @plen.
985 * May return %NULL if resources needed to perform the mapping are exhausted.
986 * Use only for reads OR writes - not for read-modify-write operations.
987 * Use cpu_register_map_client() to know when retrying the map operation is
988 * likely to succeed.
989 *
990 * @as: #AddressSpace to be accessed
991 * @addr: address within that address space
992 * @plen: pointer to length of buffer; updated on return
993 * @is_write: indicates the transfer direction
994 */
a8170e5e
AK
995void *address_space_map(AddressSpace *as, hwaddr addr,
996 hwaddr *plen, bool is_write);
ac1970fb
AK
997
998/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
999 *
1000 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1001 * the amount of memory that was actually read or written by the caller.
1002 *
1003 * @as: #AddressSpace used
1004 * @addr: address within that address space
1005 * @len: buffer length as returned by address_space_map()
1006 * @access_len: amount of data actually transferred
1007 * @is_write: indicates the transfer direction
1008 */
a8170e5e
AK
1009void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1010 int is_write, hwaddr access_len);
ac1970fb
AK
1011
1012
093bc2cd
AK
1013#endif
1014
1015#endif