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