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