]> git.proxmox.com Git - mirror_qemu.git/blame - include/exec/memory.h
imx: Use memory_region_init_rom() for ROMs
[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
1ab4c8ce
JQ
19#define DIRTY_MEMORY_VGA 0
20#define DIRTY_MEMORY_CODE 1
21#define DIRTY_MEMORY_MIGRATION 2
22#define DIRTY_MEMORY_NUM 3 /* num of dirty bits */
23
022c62cb 24#include "exec/cpu-common.h"
ce927ed9 25#ifndef CONFIG_USER_ONLY
022c62cb 26#include "exec/hwaddr.h"
ce927ed9 27#endif
cc05c43a 28#include "exec/memattrs.h"
1de7afc9 29#include "qemu/queue.h"
1de7afc9 30#include "qemu/int128.h"
06866575 31#include "qemu/notify.h"
b4fefef9 32#include "qom/object.h"
374f2981 33#include "qemu/rcu.h"
093bc2cd 34
07bdaa41
PB
35#define RAM_ADDR_INVALID (~(ram_addr_t)0)
36
052e87b0
PB
37#define MAX_PHYS_ADDR_SPACE_BITS 62
38#define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1)
39
b4fefef9
PC
40#define TYPE_MEMORY_REGION "qemu:memory-region"
41#define MEMORY_REGION(obj) \
42 OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION)
43
093bc2cd 44typedef struct MemoryRegionOps MemoryRegionOps;
74901c3b 45typedef struct MemoryRegionMmio MemoryRegionMmio;
093bc2cd 46
74901c3b
AK
47struct MemoryRegionMmio {
48 CPUReadMemoryFunc *read[3];
49 CPUWriteMemoryFunc *write[3];
50};
51
30951157
AK
52typedef struct IOMMUTLBEntry IOMMUTLBEntry;
53
54/* See address_space_translate: bit 0 is read, bit 1 is write. */
55typedef enum {
56 IOMMU_NONE = 0,
57 IOMMU_RO = 1,
58 IOMMU_WO = 2,
59 IOMMU_RW = 3,
60} IOMMUAccessFlags;
61
62struct IOMMUTLBEntry {
63 AddressSpace *target_as;
64 hwaddr iova;
65 hwaddr translated_addr;
66 hwaddr addr_mask; /* 0xfff = 4k translation */
67 IOMMUAccessFlags perm;
68};
69
cc05c43a
PM
70/* New-style MMIO accessors can indicate that the transaction failed.
71 * A zero (MEMTX_OK) response means success; anything else is a failure
72 * of some kind. The memory subsystem will bitwise-OR together results
73 * if it is synthesizing an operation from multiple smaller accesses.
74 */
75#define MEMTX_OK 0
76#define MEMTX_ERROR (1U << 0) /* device returned an error */
77#define MEMTX_DECODE_ERROR (1U << 1) /* nothing at that address */
78typedef uint32_t MemTxResult;
79
093bc2cd
AK
80/*
81 * Memory region callbacks
82 */
83struct MemoryRegionOps {
84 /* Read from the memory region. @addr is relative to @mr; @size is
85 * in bytes. */
86 uint64_t (*read)(void *opaque,
a8170e5e 87 hwaddr addr,
093bc2cd
AK
88 unsigned size);
89 /* Write to the memory region. @addr is relative to @mr; @size is
90 * in bytes. */
91 void (*write)(void *opaque,
a8170e5e 92 hwaddr addr,
093bc2cd
AK
93 uint64_t data,
94 unsigned size);
95
cc05c43a
PM
96 MemTxResult (*read_with_attrs)(void *opaque,
97 hwaddr addr,
98 uint64_t *data,
99 unsigned size,
100 MemTxAttrs attrs);
101 MemTxResult (*write_with_attrs)(void *opaque,
102 hwaddr addr,
103 uint64_t data,
104 unsigned size,
105 MemTxAttrs attrs);
106
093bc2cd
AK
107 enum device_endian endianness;
108 /* Guest-visible constraints: */
109 struct {
110 /* If nonzero, specify bounds on access sizes beyond which a machine
111 * check is thrown.
112 */
113 unsigned min_access_size;
114 unsigned max_access_size;
115 /* If true, unaligned accesses are supported. Otherwise unaligned
116 * accesses throw machine checks.
117 */
118 bool unaligned;
897fa7cf
AK
119 /*
120 * If present, and returns #false, the transaction is not accepted
121 * by the device (and results in machine dependent behaviour such
122 * as a machine check exception).
123 */
a8170e5e 124 bool (*accepts)(void *opaque, hwaddr addr,
897fa7cf 125 unsigned size, bool is_write);
093bc2cd
AK
126 } valid;
127 /* Internal implementation constraints: */
128 struct {
129 /* If nonzero, specifies the minimum size implemented. Smaller sizes
130 * will be rounded upwards and a partial result will be returned.
131 */
132 unsigned min_access_size;
133 /* If nonzero, specifies the maximum size implemented. Larger sizes
134 * will be done as a series of accesses with smaller sizes.
135 */
136 unsigned max_access_size;
137 /* If true, unaligned accesses are supported. Otherwise all accesses
138 * are converted to (possibly multiple) naturally aligned accesses.
139 */
edc1ba7a 140 bool unaligned;
093bc2cd 141 } impl;
627a0e90 142
74901c3b
AK
143 /* If .read and .write are not present, old_mmio may be used for
144 * backwards compatibility with old mmio registration
145 */
146 const MemoryRegionMmio old_mmio;
093bc2cd
AK
147};
148
30951157
AK
149typedef struct MemoryRegionIOMMUOps MemoryRegionIOMMUOps;
150
151struct MemoryRegionIOMMUOps {
152 /* Return a TLB entry that contains a given address. */
8d7b8cb9 153 IOMMUTLBEntry (*translate)(MemoryRegion *iommu, hwaddr addr, bool is_write);
f682e9c2
AK
154 /* Returns minimum supported page size */
155 uint64_t (*get_min_page_size)(MemoryRegion *iommu);
d22d8956
AK
156 /* Called when the first notifier is set */
157 void (*notify_started)(MemoryRegion *iommu);
158 /* Called when the last notifier is removed */
159 void (*notify_stopped)(MemoryRegion *iommu);
30951157
AK
160};
161
093bc2cd 162typedef struct CoalescedMemoryRange CoalescedMemoryRange;
3e9d69e7 163typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
093bc2cd
AK
164
165struct MemoryRegion {
b4fefef9 166 Object parent_obj;
a676854f 167
093bc2cd 168 /* All fields are private - violators will be prosecuted */
a676854f
PB
169
170 /* The following fields should fit in a cache line */
171 bool romd_mode;
172 bool ram;
173 bool subpage;
174 bool readonly; /* For RAM regions */
175 bool rom_device;
176 bool flush_coalesced_mmio;
177 bool global_locking;
178 uint8_t dirty_log_mask;
58eaa217 179 RAMBlock *ram_block;
612263cf 180 Object *owner;
30951157 181 const MemoryRegionIOMMUOps *iommu_ops;
a676854f
PB
182
183 const MemoryRegionOps *ops;
093bc2cd 184 void *opaque;
feca4ac1 185 MemoryRegion *container;
08dafab4 186 Int128 size;
a8170e5e 187 hwaddr addr;
545e92e0 188 void (*destructor)(MemoryRegion *mr);
a2b257d6 189 uint64_t align;
14a3c10a 190 bool terminates;
e4dc3f59 191 bool skip_dump;
6bba19ba 192 bool enabled;
1660e72d 193 bool warning_printed; /* For reservations */
deb809ed 194 uint8_t vga_logging_count;
093bc2cd 195 MemoryRegion *alias;
a8170e5e 196 hwaddr alias_offset;
d33382da 197 int32_t priority;
093bc2cd
AK
198 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
199 QTAILQ_ENTRY(MemoryRegion) subregions_link;
200 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
302fa283 201 const char *name;
3e9d69e7
AK
202 unsigned ioeventfd_nb;
203 MemoryRegionIoeventfd *ioeventfds;
06866575 204 NotifierList iommu_notify;
093bc2cd
AK
205};
206
c2fc83e8
PB
207/**
208 * MemoryListener: callbacks structure for updates to the physical memory map
209 *
210 * Allows a component to adjust to changes in the guest-visible memory map.
211 * Use with memory_listener_register() and memory_listener_unregister().
212 */
213struct MemoryListener {
214 void (*begin)(MemoryListener *listener);
215 void (*commit)(MemoryListener *listener);
216 void (*region_add)(MemoryListener *listener, MemoryRegionSection *section);
217 void (*region_del)(MemoryListener *listener, MemoryRegionSection *section);
218 void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section);
b2dfd71c
PB
219 void (*log_start)(MemoryListener *listener, MemoryRegionSection *section,
220 int old, int new);
221 void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section,
222 int old, int new);
c2fc83e8
PB
223 void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section);
224 void (*log_global_start)(MemoryListener *listener);
225 void (*log_global_stop)(MemoryListener *listener);
226 void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section,
227 bool match_data, uint64_t data, EventNotifier *e);
228 void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section,
229 bool match_data, uint64_t data, EventNotifier *e);
230 void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section,
231 hwaddr addr, hwaddr len);
232 void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section,
233 hwaddr addr, hwaddr len);
234 /* Lower = earlier (during add), later (during del) */
235 unsigned priority;
236 AddressSpace *address_space_filter;
237 QTAILQ_ENTRY(MemoryListener) link;
238};
239
9ad2bbc1
AK
240/**
241 * AddressSpace: describes a mapping of addresses to #MemoryRegion objects
242 */
243struct AddressSpace {
244 /* All fields are private. */
374f2981 245 struct rcu_head rcu;
7dca8043 246 char *name;
9ad2bbc1 247 MemoryRegion *root;
f0c02d15
PC
248 int ref_count;
249 bool malloced;
374f2981
PB
250
251 /* Accessed via RCU. */
9ad2bbc1 252 struct FlatView *current_map;
374f2981 253
9ad2bbc1
AK
254 int ioeventfd_nb;
255 struct MemoryRegionIoeventfd *ioeventfds;
ac1970fb 256 struct AddressSpaceDispatch *dispatch;
00752703 257 struct AddressSpaceDispatch *next_dispatch;
89ae337a
PB
258 MemoryListener dispatch_listener;
259
0d673e36 260 QTAILQ_ENTRY(AddressSpace) address_spaces_link;
9ad2bbc1
AK
261};
262
e2177955
AK
263/**
264 * MemoryRegionSection: describes a fragment of a #MemoryRegion
265 *
266 * @mr: the region, or %NULL if empty
7664e80c 267 * @address_space: the address space the region is mapped in
e2177955
AK
268 * @offset_within_region: the beginning of the section, relative to @mr's start
269 * @size: the size of the section; will not exceed @mr's boundaries
270 * @offset_within_address_space: the address of the first byte of the section
271 * relative to the region's address space
7a8499e8 272 * @readonly: writes to this section are ignored
e2177955
AK
273 */
274struct MemoryRegionSection {
275 MemoryRegion *mr;
f6790af6 276 AddressSpace *address_space;
a8170e5e 277 hwaddr offset_within_region;
052e87b0 278 Int128 size;
a8170e5e 279 hwaddr offset_within_address_space;
7a8499e8 280 bool readonly;
e2177955
AK
281};
282
093bc2cd
AK
283/**
284 * memory_region_init: Initialize a memory region
285 *
69ddaf66 286 * The region typically acts as a container for other memory regions. Use
093bc2cd
AK
287 * memory_region_add_subregion() to add subregions.
288 *
289 * @mr: the #MemoryRegion to be initialized
2c9b15ca 290 * @owner: the object that tracks the region's reference count
093bc2cd
AK
291 * @name: used for debugging; not visible to the user or ABI
292 * @size: size of the region; any subregions beyond this size will be clipped
293 */
294void memory_region_init(MemoryRegion *mr,
2c9b15ca 295 struct Object *owner,
093bc2cd
AK
296 const char *name,
297 uint64_t size);
46637be2
PB
298
299/**
300 * memory_region_ref: Add 1 to a memory region's reference count
301 *
302 * Whenever memory regions are accessed outside the BQL, they need to be
303 * preserved against hot-unplug. MemoryRegions actually do not have their
304 * own reference count; they piggyback on a QOM object, their "owner".
305 * This function adds a reference to the owner.
306 *
307 * All MemoryRegions must have an owner if they can disappear, even if the
308 * device they belong to operates exclusively under the BQL. This is because
309 * the region could be returned at any time by memory_region_find, and this
310 * is usually under guest control.
311 *
312 * @mr: the #MemoryRegion
313 */
314void memory_region_ref(MemoryRegion *mr);
315
316/**
317 * memory_region_unref: Remove 1 to a memory region's reference count
318 *
319 * Whenever memory regions are accessed outside the BQL, they need to be
320 * preserved against hot-unplug. MemoryRegions actually do not have their
321 * own reference count; they piggyback on a QOM object, their "owner".
322 * This function removes a reference to the owner and possibly destroys it.
323 *
324 * @mr: the #MemoryRegion
325 */
326void memory_region_unref(MemoryRegion *mr);
327
093bc2cd
AK
328/**
329 * memory_region_init_io: Initialize an I/O memory region.
330 *
69ddaf66 331 * Accesses into the region will cause the callbacks in @ops to be called.
093bc2cd
AK
332 * if @size is nonzero, subregions will be clipped to @size.
333 *
334 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 335 * @owner: the object that tracks the region's reference count
093bc2cd
AK
336 * @ops: a structure containing read and write callbacks to be used when
337 * I/O is performed on the region.
b6af0975 338 * @opaque: passed to the read and write callbacks of the @ops structure.
093bc2cd
AK
339 * @name: used for debugging; not visible to the user or ABI
340 * @size: size of the region.
341 */
342void memory_region_init_io(MemoryRegion *mr,
2c9b15ca 343 struct Object *owner,
093bc2cd
AK
344 const MemoryRegionOps *ops,
345 void *opaque,
346 const char *name,
347 uint64_t size);
348
349/**
350 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
69ddaf66 351 * region will modify memory directly.
093bc2cd
AK
352 *
353 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 354 * @owner: the object that tracks the region's reference count
c5705a77 355 * @name: the name of the region.
093bc2cd 356 * @size: size of the region.
49946538 357 * @errp: pointer to Error*, to store an error if it happens.
093bc2cd
AK
358 */
359void memory_region_init_ram(MemoryRegion *mr,
2c9b15ca 360 struct Object *owner,
093bc2cd 361 const char *name,
49946538
HT
362 uint64_t size,
363 Error **errp);
093bc2cd 364
60786ef3
MT
365/**
366 * memory_region_init_resizeable_ram: Initialize memory region with resizeable
367 * RAM. Accesses into the region will
368 * modify memory directly. Only an initial
369 * portion of this RAM is actually used.
370 * The used size can change across reboots.
371 *
372 * @mr: the #MemoryRegion to be initialized.
373 * @owner: the object that tracks the region's reference count
374 * @name: the name of the region.
375 * @size: used size of the region.
376 * @max_size: max size of the region.
377 * @resized: callback to notify owner about used size change.
378 * @errp: pointer to Error*, to store an error if it happens.
379 */
380void memory_region_init_resizeable_ram(MemoryRegion *mr,
381 struct Object *owner,
382 const char *name,
383 uint64_t size,
384 uint64_t max_size,
385 void (*resized)(const char*,
386 uint64_t length,
387 void *host),
388 Error **errp);
0b183fc8
PB
389#ifdef __linux__
390/**
391 * memory_region_init_ram_from_file: Initialize RAM memory region with a
392 * mmap-ed backend.
393 *
394 * @mr: the #MemoryRegion to be initialized.
395 * @owner: the object that tracks the region's reference count
396 * @name: the name of the region.
397 * @size: size of the region.
dbcb8981 398 * @share: %true if memory must be mmaped with the MAP_SHARED flag
0b183fc8 399 * @path: the path in which to allocate the RAM.
7f56e740 400 * @errp: pointer to Error*, to store an error if it happens.
0b183fc8
PB
401 */
402void memory_region_init_ram_from_file(MemoryRegion *mr,
403 struct Object *owner,
404 const char *name,
405 uint64_t size,
dbcb8981 406 bool share,
7f56e740
PB
407 const char *path,
408 Error **errp);
0b183fc8
PB
409#endif
410
093bc2cd 411/**
1a7e8cae
BZ
412 * memory_region_init_ram_ptr: Initialize RAM memory region from a
413 * user-provided pointer. Accesses into the
414 * region will modify memory directly.
093bc2cd
AK
415 *
416 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 417 * @owner: the object that tracks the region's reference count
c5705a77 418 * @name: the name of the region.
093bc2cd
AK
419 * @size: size of the region.
420 * @ptr: memory to be mapped; must contain at least @size bytes.
421 */
422void memory_region_init_ram_ptr(MemoryRegion *mr,
2c9b15ca 423 struct Object *owner,
093bc2cd
AK
424 const char *name,
425 uint64_t size,
426 void *ptr);
427
428/**
429 * memory_region_init_alias: Initialize a memory region that aliases all or a
430 * part of another memory region.
431 *
432 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 433 * @owner: the object that tracks the region's reference count
093bc2cd
AK
434 * @name: used for debugging; not visible to the user or ABI
435 * @orig: the region to be referenced; @mr will be equivalent to
436 * @orig between @offset and @offset + @size - 1.
437 * @offset: start of the section in @orig to be referenced.
438 * @size: size of the region.
439 */
440void memory_region_init_alias(MemoryRegion *mr,
2c9b15ca 441 struct Object *owner,
093bc2cd
AK
442 const char *name,
443 MemoryRegion *orig,
a8170e5e 444 hwaddr offset,
093bc2cd 445 uint64_t size);
d0a9b5bc 446
a1777f7f
PM
447/**
448 * memory_region_init_rom: Initialize a ROM memory region.
449 *
450 * This has the same effect as calling memory_region_init_ram()
451 * and then marking the resulting region read-only with
452 * memory_region_set_readonly().
453 *
454 * @mr: the #MemoryRegion to be initialized.
455 * @owner: the object that tracks the region's reference count
456 * @name: the name of the region.
457 * @size: size of the region.
458 * @errp: pointer to Error*, to store an error if it happens.
459 */
460void memory_region_init_rom(MemoryRegion *mr,
461 struct Object *owner,
462 const char *name,
463 uint64_t size,
464 Error **errp);
465
d0a9b5bc
AK
466/**
467 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
468 * handled via callbacks.
469 *
6d6d2abf
PF
470 * If NULL callbacks pointer is given, then I/O space is not supposed to be
471 * handled by QEMU itself. Any access via the memory API will cause an abort().
472 *
d0a9b5bc 473 * @mr: the #MemoryRegion to be initialized.
2c9b15ca 474 * @owner: the object that tracks the region's reference count
d0a9b5bc 475 * @ops: callbacks for write access handling.
c5705a77 476 * @name: the name of the region.
d0a9b5bc 477 * @size: size of the region.
33e0eb52 478 * @errp: pointer to Error*, to store an error if it happens.
d0a9b5bc
AK
479 */
480void memory_region_init_rom_device(MemoryRegion *mr,
2c9b15ca 481 struct Object *owner,
d0a9b5bc 482 const MemoryRegionOps *ops,
75f5941c 483 void *opaque,
d0a9b5bc 484 const char *name,
33e0eb52
HT
485 uint64_t size,
486 Error **errp);
d0a9b5bc 487
1660e72d
JK
488/**
489 * memory_region_init_reservation: Initialize a memory region that reserves
490 * I/O space.
491 *
492 * A reservation region primariy serves debugging purposes. It claims I/O
493 * space that is not supposed to be handled by QEMU itself. Any access via
494 * the memory API will cause an abort().
6d6d2abf
PF
495 * This function is deprecated. Use memory_region_init_io() with NULL
496 * callbacks instead.
1660e72d
JK
497 *
498 * @mr: the #MemoryRegion to be initialized
2c9b15ca 499 * @owner: the object that tracks the region's reference count
1660e72d
JK
500 * @name: used for debugging; not visible to the user or ABI
501 * @size: size of the region.
502 */
6d6d2abf
PF
503static inline void memory_region_init_reservation(MemoryRegion *mr,
504 Object *owner,
1660e72d 505 const char *name,
6d6d2abf
PF
506 uint64_t size)
507{
508 memory_region_init_io(mr, owner, NULL, mr, name, size);
509}
30951157
AK
510
511/**
512 * memory_region_init_iommu: Initialize a memory region that translates
513 * addresses
514 *
515 * An IOMMU region translates addresses and forwards accesses to a target
516 * memory region.
517 *
518 * @mr: the #MemoryRegion to be initialized
2c9b15ca 519 * @owner: the object that tracks the region's reference count
30951157
AK
520 * @ops: a function that translates addresses into the @target region
521 * @name: used for debugging; not visible to the user or ABI
522 * @size: size of the region.
523 */
524void memory_region_init_iommu(MemoryRegion *mr,
2c9b15ca 525 struct Object *owner,
30951157
AK
526 const MemoryRegionIOMMUOps *ops,
527 const char *name,
528 uint64_t size);
529
803c0816
PB
530/**
531 * memory_region_owner: get a memory region's owner.
532 *
533 * @mr: the memory region being queried.
534 */
535struct Object *memory_region_owner(MemoryRegion *mr);
536
093bc2cd
AK
537/**
538 * memory_region_size: get a memory region's size.
539 *
540 * @mr: the memory region being queried.
541 */
542uint64_t memory_region_size(MemoryRegion *mr);
543
8ea9252a
AK
544/**
545 * memory_region_is_ram: check whether a memory region is random access
546 *
547 * Returns %true is a memory region is random access.
548 *
549 * @mr: the memory region being queried
550 */
1619d1fe
PB
551static inline bool memory_region_is_ram(MemoryRegion *mr)
552{
553 return mr->ram;
554}
8ea9252a 555
e4dc3f59
ND
556/**
557 * memory_region_is_skip_dump: check whether a memory region should not be
558 * dumped
559 *
560 * Returns %true is a memory region should not be dumped(e.g. VFIO BAR MMAP).
561 *
562 * @mr: the memory region being queried
563 */
564bool memory_region_is_skip_dump(MemoryRegion *mr);
565
566/**
567 * memory_region_set_skip_dump: Set skip_dump flag, dump will ignore this memory
568 * region
569 *
570 * @mr: the memory region being queried
571 */
572void memory_region_set_skip_dump(MemoryRegion *mr);
573
fd062573 574/**
5f9a5ea1 575 * memory_region_is_romd: check whether a memory region is in ROMD mode
fd062573 576 *
5f9a5ea1 577 * Returns %true if a memory region is a ROM device and currently set to allow
fd062573
BS
578 * direct reads.
579 *
580 * @mr: the memory region being queried
581 */
582static inline bool memory_region_is_romd(MemoryRegion *mr)
583{
5f9a5ea1 584 return mr->rom_device && mr->romd_mode;
fd062573
BS
585}
586
30951157
AK
587/**
588 * memory_region_is_iommu: check whether a memory region is an iommu
589 *
590 * Returns %true is a memory region is an iommu.
591 *
592 * @mr: the memory region being queried
593 */
1619d1fe
PB
594static inline bool memory_region_is_iommu(MemoryRegion *mr)
595{
596 return mr->iommu_ops;
597}
598
30951157 599
f682e9c2
AK
600/**
601 * memory_region_iommu_get_min_page_size: get minimum supported page size
602 * for an iommu
603 *
604 * Returns minimum supported page size for an iommu.
605 *
606 * @mr: the memory region being queried
607 */
608uint64_t memory_region_iommu_get_min_page_size(MemoryRegion *mr);
609
06866575
DG
610/**
611 * memory_region_notify_iommu: notify a change in an IOMMU translation entry.
612 *
613 * @mr: the memory region that was changed
614 * @entry: the new entry in the IOMMU translation table. The entry
615 * replaces all old entries for the same virtual I/O address range.
616 * Deleted entries have .@perm == 0.
617 */
618void memory_region_notify_iommu(MemoryRegion *mr,
619 IOMMUTLBEntry entry);
620
621/**
622 * memory_region_register_iommu_notifier: register a notifier for changes to
623 * IOMMU translation entries.
624 *
625 * @mr: the memory region to observe
626 * @n: the notifier to be added; the notifier receives a pointer to an
627 * #IOMMUTLBEntry as the opaque value; the pointer ceases to be
628 * valid on exit from the notifier.
629 */
630void memory_region_register_iommu_notifier(MemoryRegion *mr, Notifier *n);
631
a788f227
DG
632/**
633 * memory_region_iommu_replay: replay existing IOMMU translations to
f682e9c2
AK
634 * a notifier with the minimum page granularity returned by
635 * mr->iommu_ops->get_page_size().
a788f227
DG
636 *
637 * @mr: the memory region to observe
638 * @n: the notifier to which to replay iommu mappings
a788f227
DG
639 * @is_write: Whether to treat the replay as a translate "write"
640 * through the iommu
641 */
f682e9c2 642void memory_region_iommu_replay(MemoryRegion *mr, Notifier *n, bool is_write);
a788f227 643
06866575
DG
644/**
645 * memory_region_unregister_iommu_notifier: unregister a notifier for
646 * changes to IOMMU translation entries.
647 *
d22d8956
AK
648 * @mr: the memory region which was observed and for which notity_stopped()
649 * needs to be called
06866575
DG
650 * @n: the notifier to be removed.
651 */
d22d8956 652void memory_region_unregister_iommu_notifier(MemoryRegion *mr, Notifier *n);
06866575 653
8991c79b
AK
654/**
655 * memory_region_name: get a memory region's name
656 *
657 * Returns the string that was used to initialize the memory region.
658 *
659 * @mr: the memory region being queried
660 */
5d546d4b 661const char *memory_region_name(const MemoryRegion *mr);
8991c79b 662
55043ba3
AK
663/**
664 * memory_region_is_logging: return whether a memory region is logging writes
665 *
2d1a35be 666 * Returns %true if the memory region is logging writes for the given client
55043ba3
AK
667 *
668 * @mr: the memory region being queried
2d1a35be 669 * @client: the client being queried
55043ba3 670 */
2d1a35be
PB
671bool memory_region_is_logging(MemoryRegion *mr, uint8_t client);
672
673/**
674 * memory_region_get_dirty_log_mask: return the clients for which a
675 * memory region is logging writes.
676 *
677e7805
PB
677 * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants
678 * are the bit indices.
2d1a35be
PB
679 *
680 * @mr: the memory region being queried
681 */
682uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr);
55043ba3 683
ce7923da
AK
684/**
685 * memory_region_is_rom: check whether a memory region is ROM
686 *
687 * Returns %true is a memory region is read-only memory.
688 *
689 * @mr: the memory region being queried
690 */
1619d1fe
PB
691static inline bool memory_region_is_rom(MemoryRegion *mr)
692{
693 return mr->ram && mr->readonly;
694}
695
ce7923da 696
a35ba7be
PB
697/**
698 * memory_region_get_fd: Get a file descriptor backing a RAM memory region.
699 *
700 * Returns a file descriptor backing a file-based RAM memory region,
701 * or -1 if the region is not a file-based RAM memory region.
702 *
703 * @mr: the RAM or alias memory region being queried.
704 */
705int memory_region_get_fd(MemoryRegion *mr);
706
4ff87573
PB
707/**
708 * memory_region_set_fd: Mark a RAM memory region as backed by a
709 * file descriptor.
710 *
711 * This function is typically used after memory_region_init_ram_ptr().
712 *
713 * @mr: the memory region being queried.
714 * @fd: the file descriptor that backs @mr.
715 */
716void memory_region_set_fd(MemoryRegion *mr, int fd);
717
07bdaa41
PB
718/**
719 * memory_region_from_host: Convert a pointer into a RAM memory region
720 * and an offset within it.
721 *
722 * Given a host pointer inside a RAM memory region (created with
723 * memory_region_init_ram() or memory_region_init_ram_ptr()), return
724 * the MemoryRegion and the offset within it.
725 *
726 * Use with care; by the time this function returns, the returned pointer is
727 * not protected by RCU anymore. If the caller is not within an RCU critical
728 * section and does not hold the iothread lock, it must have other means of
729 * protecting the pointer, such as a reference to the region that includes
730 * the incoming ram_addr_t.
731 *
732 * @mr: the memory region being queried.
733 */
734MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset);
735
093bc2cd
AK
736/**
737 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
738 *
739 * Returns a host pointer to a RAM memory region (created with
49b24afc
PB
740 * memory_region_init_ram() or memory_region_init_ram_ptr()).
741 *
742 * Use with care; by the time this function returns, the returned pointer is
743 * not protected by RCU anymore. If the caller is not within an RCU critical
744 * section and does not hold the iothread lock, it must have other means of
745 * protecting the pointer, such as a reference to the region that includes
746 * the incoming ram_addr_t.
093bc2cd
AK
747 *
748 * @mr: the memory region being queried.
749 */
750void *memory_region_get_ram_ptr(MemoryRegion *mr);
751
37d7c084
PB
752/* memory_region_ram_resize: Resize a RAM region.
753 *
754 * Only legal before guest might have detected the memory size: e.g. on
755 * incoming migration, or right after reset.
756 *
757 * @mr: a memory region created with @memory_region_init_resizeable_ram.
758 * @newsize: the new size the region
759 * @errp: pointer to Error*, to store an error if it happens.
760 */
761void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize,
762 Error **errp);
763
093bc2cd
AK
764/**
765 * memory_region_set_log: Turn dirty logging on or off for a region.
766 *
767 * Turns dirty logging on or off for a specified client (display, migration).
768 * Only meaningful for RAM regions.
769 *
770 * @mr: the memory region being updated.
771 * @log: whether dirty logging is to be enabled or disabled.
dbddac6d 772 * @client: the user of the logging information; %DIRTY_MEMORY_VGA only.
093bc2cd
AK
773 */
774void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
775
776/**
cd7a45c9
BS
777 * memory_region_get_dirty: Check whether a range of bytes is dirty
778 * for a specified client.
093bc2cd 779 *
cd7a45c9 780 * Checks whether a range of bytes has been written to since the last
093bc2cd
AK
781 * call to memory_region_reset_dirty() with the same @client. Dirty logging
782 * must be enabled.
783 *
784 * @mr: the memory region being queried.
785 * @addr: the address (relative to the start of the region) being queried.
cd7a45c9 786 * @size: the size of the range being queried.
093bc2cd
AK
787 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
788 * %DIRTY_MEMORY_VGA.
789 */
a8170e5e
AK
790bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr,
791 hwaddr size, unsigned client);
093bc2cd
AK
792
793/**
fd4aa979 794 * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region.
093bc2cd 795 *
fd4aa979
BS
796 * Marks a range of bytes as dirty, after it has been dirtied outside
797 * guest code.
093bc2cd 798 *
fd4aa979 799 * @mr: the memory region being dirtied.
093bc2cd 800 * @addr: the address (relative to the start of the region) being dirtied.
fd4aa979 801 * @size: size of the range being dirtied.
093bc2cd 802 */
a8170e5e
AK
803void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr,
804 hwaddr size);
093bc2cd 805
6c279db8
JQ
806/**
807 * memory_region_test_and_clear_dirty: Check whether a range of bytes is dirty
808 * for a specified client. It clears them.
809 *
810 * Checks whether a range of bytes has been written to since the last
811 * call to memory_region_reset_dirty() with the same @client. Dirty logging
812 * must be enabled.
813 *
814 * @mr: the memory region being queried.
815 * @addr: the address (relative to the start of the region) being queried.
816 * @size: the size of the range being queried.
817 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
818 * %DIRTY_MEMORY_VGA.
819 */
820bool memory_region_test_and_clear_dirty(MemoryRegion *mr, hwaddr addr,
821 hwaddr size, unsigned client);
093bc2cd
AK
822/**
823 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
824 * any external TLBs (e.g. kvm)
825 *
826 * Flushes dirty information from accelerators such as kvm and vhost-net
827 * and makes it available to users of the memory API.
828 *
829 * @mr: the region being flushed.
830 */
831void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
832
833/**
834 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
835 * client.
836 *
837 * Marks a range of pages as no longer dirty.
838 *
839 * @mr: the region being updated.
840 * @addr: the start of the subrange being cleaned.
841 * @size: the size of the subrange being cleaned.
842 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
843 * %DIRTY_MEMORY_VGA.
844 */
a8170e5e
AK
845void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr,
846 hwaddr size, unsigned client);
093bc2cd
AK
847
848/**
849 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
850 *
851 * Allows a memory region to be marked as read-only (turning it into a ROM).
852 * only useful on RAM regions.
853 *
854 * @mr: the region being updated.
855 * @readonly: whether rhe region is to be ROM or RAM.
856 */
857void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
858
d0a9b5bc 859/**
5f9a5ea1 860 * memory_region_rom_device_set_romd: enable/disable ROMD mode
d0a9b5bc
AK
861 *
862 * Allows a ROM device (initialized with memory_region_init_rom_device() to
5f9a5ea1
JK
863 * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the
864 * device is mapped to guest memory and satisfies read access directly.
865 * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function.
866 * Writes are always handled by the #MemoryRegion.write function.
d0a9b5bc
AK
867 *
868 * @mr: the memory region to be updated
5f9a5ea1 869 * @romd_mode: %true to put the region into ROMD mode
d0a9b5bc 870 */
5f9a5ea1 871void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode);
d0a9b5bc 872
093bc2cd
AK
873/**
874 * memory_region_set_coalescing: Enable memory coalescing for the region.
875 *
876 * Enabled writes to a region to be queued for later processing. MMIO ->write
877 * callbacks may be delayed until a non-coalesced MMIO is issued.
878 * Only useful for IO regions. Roughly similar to write-combining hardware.
879 *
880 * @mr: the memory region to be write coalesced
881 */
882void memory_region_set_coalescing(MemoryRegion *mr);
883
884/**
885 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
886 * a region.
887 *
888 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
889 * Multiple calls can be issued coalesced disjoint ranges.
890 *
891 * @mr: the memory region to be updated.
892 * @offset: the start of the range within the region to be coalesced.
893 * @size: the size of the subrange to be coalesced.
894 */
895void memory_region_add_coalescing(MemoryRegion *mr,
a8170e5e 896 hwaddr offset,
093bc2cd
AK
897 uint64_t size);
898
899/**
900 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
901 *
902 * Disables any coalescing caused by memory_region_set_coalescing() or
903 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
904 * hardware.
905 *
906 * @mr: the memory region to be updated.
907 */
908void memory_region_clear_coalescing(MemoryRegion *mr);
909
d410515e
JK
910/**
911 * memory_region_set_flush_coalesced: Enforce memory coalescing flush before
912 * accesses.
913 *
914 * Ensure that pending coalesced MMIO request are flushed before the memory
915 * region is accessed. This property is automatically enabled for all regions
916 * passed to memory_region_set_coalescing() and memory_region_add_coalescing().
917 *
918 * @mr: the memory region to be updated.
919 */
920void memory_region_set_flush_coalesced(MemoryRegion *mr);
921
922/**
923 * memory_region_clear_flush_coalesced: Disable memory coalescing flush before
924 * accesses.
925 *
926 * Clear the automatic coalesced MMIO flushing enabled via
927 * memory_region_set_flush_coalesced. Note that this service has no effect on
928 * memory regions that have MMIO coalescing enabled for themselves. For them,
929 * automatic flushing will stop once coalescing is disabled.
930 *
931 * @mr: the memory region to be updated.
932 */
933void memory_region_clear_flush_coalesced(MemoryRegion *mr);
934
196ea131
JK
935/**
936 * memory_region_set_global_locking: Declares the access processing requires
937 * QEMU's global lock.
938 *
939 * When this is invoked, accesses to the memory region will be processed while
940 * holding the global lock of QEMU. This is the default behavior of memory
941 * regions.
942 *
943 * @mr: the memory region to be updated.
944 */
945void memory_region_set_global_locking(MemoryRegion *mr);
946
947/**
948 * memory_region_clear_global_locking: Declares that access processing does
949 * not depend on the QEMU global lock.
950 *
951 * By clearing this property, accesses to the memory region will be processed
952 * outside of QEMU's global lock (unless the lock is held on when issuing the
953 * access request). In this case, the device model implementing the access
954 * handlers is responsible for synchronization of concurrency.
955 *
956 * @mr: the memory region to be updated.
957 */
958void memory_region_clear_global_locking(MemoryRegion *mr);
959
3e9d69e7
AK
960/**
961 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
962 * is written to a location.
963 *
964 * Marks a word in an IO region (initialized with memory_region_init_io())
965 * as a trigger for an eventfd event. The I/O callback will not be called.
69ddaf66 966 * The caller must be prepared to handle failure (that is, take the required
3e9d69e7
AK
967 * action if the callback _is_ called).
968 *
969 * @mr: the memory region being updated.
970 * @addr: the address within @mr that is to be monitored
971 * @size: the size of the access to trigger the eventfd
972 * @match_data: whether to match against @data, instead of just @addr
973 * @data: the data to match against the guest write
974 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
975 **/
976void memory_region_add_eventfd(MemoryRegion *mr,
a8170e5e 977 hwaddr addr,
3e9d69e7
AK
978 unsigned size,
979 bool match_data,
980 uint64_t data,
753d5e14 981 EventNotifier *e);
3e9d69e7
AK
982
983/**
69ddaf66 984 * memory_region_del_eventfd: Cancel an eventfd.
3e9d69e7 985 *
69ddaf66
ASRJ
986 * Cancels an eventfd trigger requested by a previous
987 * memory_region_add_eventfd() call.
3e9d69e7
AK
988 *
989 * @mr: the memory region being updated.
990 * @addr: the address within @mr that is to be monitored
991 * @size: the size of the access to trigger the eventfd
992 * @match_data: whether to match against @data, instead of just @addr
993 * @data: the data to match against the guest write
994 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
995 */
996void memory_region_del_eventfd(MemoryRegion *mr,
a8170e5e 997 hwaddr addr,
3e9d69e7
AK
998 unsigned size,
999 bool match_data,
1000 uint64_t data,
753d5e14
PB
1001 EventNotifier *e);
1002
093bc2cd 1003/**
69ddaf66 1004 * memory_region_add_subregion: Add a subregion to a container.
093bc2cd 1005 *
69ddaf66 1006 * Adds a subregion at @offset. The subregion may not overlap with other
093bc2cd
AK
1007 * subregions (except for those explicitly marked as overlapping). A region
1008 * may only be added once as a subregion (unless removed with
1009 * memory_region_del_subregion()); use memory_region_init_alias() if you
1010 * want a region to be a subregion in multiple locations.
1011 *
1012 * @mr: the region to contain the new subregion; must be a container
1013 * initialized with memory_region_init().
1014 * @offset: the offset relative to @mr where @subregion is added.
1015 * @subregion: the subregion to be added.
1016 */
1017void memory_region_add_subregion(MemoryRegion *mr,
a8170e5e 1018 hwaddr offset,
093bc2cd
AK
1019 MemoryRegion *subregion);
1020/**
1a7e8cae
BZ
1021 * memory_region_add_subregion_overlap: Add a subregion to a container
1022 * with overlap.
093bc2cd 1023 *
69ddaf66 1024 * Adds a subregion at @offset. The subregion may overlap with other
093bc2cd
AK
1025 * subregions. Conflicts are resolved by having a higher @priority hide a
1026 * lower @priority. Subregions without priority are taken as @priority 0.
1027 * A region may only be added once as a subregion (unless removed with
1028 * memory_region_del_subregion()); use memory_region_init_alias() if you
1029 * want a region to be a subregion in multiple locations.
1030 *
1031 * @mr: the region to contain the new subregion; must be a container
1032 * initialized with memory_region_init().
1033 * @offset: the offset relative to @mr where @subregion is added.
1034 * @subregion: the subregion to be added.
1035 * @priority: used for resolving overlaps; highest priority wins.
1036 */
1037void memory_region_add_subregion_overlap(MemoryRegion *mr,
a8170e5e 1038 hwaddr offset,
093bc2cd 1039 MemoryRegion *subregion,
a1ff8ae0 1040 int priority);
e34911c4
AK
1041
1042/**
1043 * memory_region_get_ram_addr: Get the ram address associated with a memory
1044 * region
e34911c4 1045 */
7ebb2745 1046ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr);
e34911c4 1047
a2b257d6 1048uint64_t memory_region_get_alignment(const MemoryRegion *mr);
093bc2cd
AK
1049/**
1050 * memory_region_del_subregion: Remove a subregion.
1051 *
1052 * Removes a subregion from its container.
1053 *
1054 * @mr: the container to be updated.
1055 * @subregion: the region being removed; must be a current subregion of @mr.
1056 */
1057void memory_region_del_subregion(MemoryRegion *mr,
1058 MemoryRegion *subregion);
1059
6bba19ba
AK
1060/*
1061 * memory_region_set_enabled: dynamically enable or disable a region
1062 *
1063 * Enables or disables a memory region. A disabled memory region
1064 * ignores all accesses to itself and its subregions. It does not
1065 * obscure sibling subregions with lower priority - it simply behaves as
1066 * if it was removed from the hierarchy.
1067 *
1068 * Regions default to being enabled.
1069 *
1070 * @mr: the region to be updated
1071 * @enabled: whether to enable or disable the region
1072 */
1073void memory_region_set_enabled(MemoryRegion *mr, bool enabled);
1074
2282e1af
AK
1075/*
1076 * memory_region_set_address: dynamically update the address of a region
1077 *
feca4ac1 1078 * Dynamically updates the address of a region, relative to its container.
2282e1af
AK
1079 * May be used on regions are currently part of a memory hierarchy.
1080 *
1081 * @mr: the region to be updated
feca4ac1 1082 * @addr: new address, relative to container region
2282e1af 1083 */
a8170e5e 1084void memory_region_set_address(MemoryRegion *mr, hwaddr addr);
2282e1af 1085
e7af4c67
MT
1086/*
1087 * memory_region_set_size: dynamically update the size of a region.
1088 *
1089 * Dynamically updates the size of a region.
1090 *
1091 * @mr: the region to be updated
1092 * @size: used size of the region.
1093 */
1094void memory_region_set_size(MemoryRegion *mr, uint64_t size);
1095
4703359e
AK
1096/*
1097 * memory_region_set_alias_offset: dynamically update a memory alias's offset
1098 *
1099 * Dynamically updates the offset into the target region that an alias points
1100 * to, as if the fourth argument to memory_region_init_alias() has changed.
1101 *
1102 * @mr: the #MemoryRegion to be updated; should be an alias.
1103 * @offset: the new offset into the target memory region
1104 */
1105void memory_region_set_alias_offset(MemoryRegion *mr,
a8170e5e 1106 hwaddr offset);
4703359e 1107
3ce10901 1108/**
feca4ac1
PB
1109 * memory_region_present: checks if an address relative to a @container
1110 * translates into #MemoryRegion within @container
3ce10901 1111 *
feca4ac1 1112 * Answer whether a #MemoryRegion within @container covers the address
3ce10901
PB
1113 * @addr.
1114 *
feca4ac1
PB
1115 * @container: a #MemoryRegion within which @addr is a relative address
1116 * @addr: the area within @container to be searched
3ce10901 1117 */
feca4ac1 1118bool memory_region_present(MemoryRegion *container, hwaddr addr);
3ce10901 1119
eed2bacf
IM
1120/**
1121 * memory_region_is_mapped: returns true if #MemoryRegion is mapped
1122 * into any address space.
1123 *
1124 * @mr: a #MemoryRegion which should be checked if it's mapped
1125 */
1126bool memory_region_is_mapped(MemoryRegion *mr);
1127
e2177955 1128/**
73034e9e
PB
1129 * memory_region_find: translate an address/size relative to a
1130 * MemoryRegion into a #MemoryRegionSection.
e2177955 1131 *
73034e9e
PB
1132 * Locates the first #MemoryRegion within @mr that overlaps the range
1133 * given by @addr and @size.
e2177955
AK
1134 *
1135 * Returns a #MemoryRegionSection that describes a contiguous overlap.
1136 * It will have the following characteristics:
e2177955
AK
1137 * .@size = 0 iff no overlap was found
1138 * .@mr is non-%NULL iff an overlap was found
1139 *
73034e9e
PB
1140 * Remember that in the return value the @offset_within_region is
1141 * relative to the returned region (in the .@mr field), not to the
1142 * @mr argument.
1143 *
1144 * Similarly, the .@offset_within_address_space is relative to the
1145 * address space that contains both regions, the passed and the
1146 * returned one. However, in the special case where the @mr argument
feca4ac1 1147 * has no container (and thus is the root of the address space), the
73034e9e
PB
1148 * following will hold:
1149 * .@offset_within_address_space >= @addr
1150 * .@offset_within_address_space + .@size <= @addr + @size
1151 *
1152 * @mr: a MemoryRegion within which @addr is a relative address
1153 * @addr: start of the area within @as to be searched
e2177955
AK
1154 * @size: size of the area to be searched
1155 */
73034e9e 1156MemoryRegionSection memory_region_find(MemoryRegion *mr,
a8170e5e 1157 hwaddr addr, uint64_t size);
e2177955 1158
86e775c6 1159/**
1d671369 1160 * address_space_sync_dirty_bitmap: synchronize the dirty log for all memory
86e775c6
AK
1161 *
1162 * Synchronizes the dirty page log for an entire address space.
1d671369 1163 * @as: the address space that contains the memory being synchronized
86e775c6 1164 */
1d671369 1165void address_space_sync_dirty_bitmap(AddressSpace *as);
86e775c6 1166
69ddaf66
ASRJ
1167/**
1168 * memory_region_transaction_begin: Start a transaction.
1169 *
1170 * During a transaction, changes will be accumulated and made visible
dabdf394 1171 * only when the transaction ends (is committed).
4ef4db86
AK
1172 */
1173void memory_region_transaction_begin(void);
69ddaf66
ASRJ
1174
1175/**
1176 * memory_region_transaction_commit: Commit a transaction and make changes
1177 * visible to the guest.
4ef4db86
AK
1178 */
1179void memory_region_transaction_commit(void);
1180
7664e80c
AK
1181/**
1182 * memory_listener_register: register callbacks to be called when memory
1183 * sections are mapped or unmapped into an address
1184 * space
1185 *
1186 * @listener: an object containing the callbacks to be called
7376e582 1187 * @filter: if non-%NULL, only regions in this address space will be observed
7664e80c 1188 */
f6790af6 1189void memory_listener_register(MemoryListener *listener, AddressSpace *filter);
7664e80c
AK
1190
1191/**
1192 * memory_listener_unregister: undo the effect of memory_listener_register()
1193 *
1194 * @listener: an object containing the callbacks to be removed
1195 */
1196void memory_listener_unregister(MemoryListener *listener);
1197
1198/**
1199 * memory_global_dirty_log_start: begin dirty logging for all regions
1200 */
1201void memory_global_dirty_log_start(void);
1202
1203/**
1a7e8cae 1204 * memory_global_dirty_log_stop: end dirty logging for all regions
7664e80c
AK
1205 */
1206void memory_global_dirty_log_stop(void);
1207
314e2987
BS
1208void mtree_info(fprintf_function mon_printf, void *f);
1209
3b643495
PM
1210/**
1211 * memory_region_dispatch_read: perform a read directly to the specified
1212 * MemoryRegion.
1213 *
1214 * @mr: #MemoryRegion to access
1215 * @addr: address within that region
1216 * @pval: pointer to uint64_t which the data is written to
1217 * @size: size of the access in bytes
1218 * @attrs: memory transaction attributes to use for the access
1219 */
1220MemTxResult memory_region_dispatch_read(MemoryRegion *mr,
1221 hwaddr addr,
1222 uint64_t *pval,
1223 unsigned size,
1224 MemTxAttrs attrs);
1225/**
1226 * memory_region_dispatch_write: perform a write directly to the specified
1227 * MemoryRegion.
1228 *
1229 * @mr: #MemoryRegion to access
1230 * @addr: address within that region
1231 * @data: data to write
1232 * @size: size of the access in bytes
1233 * @attrs: memory transaction attributes to use for the access
1234 */
1235MemTxResult memory_region_dispatch_write(MemoryRegion *mr,
1236 hwaddr addr,
1237 uint64_t data,
1238 unsigned size,
1239 MemTxAttrs attrs);
1240
9ad2bbc1
AK
1241/**
1242 * address_space_init: initializes an address space
1243 *
1244 * @as: an uninitialized #AddressSpace
67cc32eb 1245 * @root: a #MemoryRegion that routes addresses for the address space
7dca8043
AK
1246 * @name: an address space name. The name is only used for debugging
1247 * output.
9ad2bbc1 1248 */
7dca8043 1249void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name);
9ad2bbc1 1250
f0c02d15
PC
1251/**
1252 * address_space_init_shareable: return an address space for a memory region,
1253 * creating it if it does not already exist
1254 *
1255 * @root: a #MemoryRegion that routes addresses for the address space
1256 * @name: an address space name. The name is only used for debugging
1257 * output.
1258 *
1259 * This function will return a pointer to an existing AddressSpace
1260 * which was initialized with the specified MemoryRegion, or it will
1261 * create and initialize one if it does not already exist. The ASes
1262 * are reference-counted, so the memory will be freed automatically
1263 * when the AddressSpace is destroyed via address_space_destroy.
1264 */
1265AddressSpace *address_space_init_shareable(MemoryRegion *root,
1266 const char *name);
83f3c251
AK
1267
1268/**
1269 * address_space_destroy: destroy an address space
1270 *
1271 * Releases all resources associated with an address space. After an address space
1272 * is destroyed, its root memory region (given by address_space_init()) may be destroyed
1273 * as well.
1274 *
1275 * @as: address space to be destroyed
1276 */
1277void address_space_destroy(AddressSpace *as);
1278
ac1970fb
AK
1279/**
1280 * address_space_rw: read from or write to an address space.
1281 *
5c9eb028
PM
1282 * Return a MemTxResult indicating whether the operation succeeded
1283 * or failed (eg unassigned memory, device rejected the transaction,
1284 * IOMMU fault).
fd8aaa76 1285 *
ac1970fb
AK
1286 * @as: #AddressSpace to be accessed
1287 * @addr: address within that address space
5c9eb028 1288 * @attrs: memory transaction attributes
ac1970fb
AK
1289 * @buf: buffer with the data transferred
1290 * @is_write: indicates the transfer direction
1291 */
5c9eb028
PM
1292MemTxResult address_space_rw(AddressSpace *as, hwaddr addr,
1293 MemTxAttrs attrs, uint8_t *buf,
1294 int len, bool is_write);
ac1970fb
AK
1295
1296/**
1297 * address_space_write: write to address space.
1298 *
5c9eb028
PM
1299 * Return a MemTxResult indicating whether the operation succeeded
1300 * or failed (eg unassigned memory, device rejected the transaction,
1301 * IOMMU fault).
fd8aaa76 1302 *
ac1970fb
AK
1303 * @as: #AddressSpace to be accessed
1304 * @addr: address within that address space
5c9eb028 1305 * @attrs: memory transaction attributes
ac1970fb
AK
1306 * @buf: buffer with the data transferred
1307 */
5c9eb028
PM
1308MemTxResult address_space_write(AddressSpace *as, hwaddr addr,
1309 MemTxAttrs attrs,
1310 const uint8_t *buf, int len);
ac1970fb 1311
3cc8f884 1312/* address_space_ld*: load from an address space
50013115
PM
1313 * address_space_st*: store to an address space
1314 *
1315 * These functions perform a load or store of the byte, word,
1316 * longword or quad to the specified address within the AddressSpace.
1317 * The _le suffixed functions treat the data as little endian;
1318 * _be indicates big endian; no suffix indicates "same endianness
1319 * as guest CPU".
1320 *
1321 * The "guest CPU endianness" accessors are deprecated for use outside
1322 * target-* code; devices should be CPU-agnostic and use either the LE
1323 * or the BE accessors.
1324 *
1325 * @as #AddressSpace to be accessed
1326 * @addr: address within that address space
1327 * @val: data value, for stores
1328 * @attrs: memory transaction attributes
1329 * @result: location to write the success/failure of the transaction;
1330 * if NULL, this information is discarded
1331 */
1332uint32_t address_space_ldub(AddressSpace *as, hwaddr addr,
1333 MemTxAttrs attrs, MemTxResult *result);
1334uint32_t address_space_lduw_le(AddressSpace *as, hwaddr addr,
1335 MemTxAttrs attrs, MemTxResult *result);
1336uint32_t address_space_lduw_be(AddressSpace *as, hwaddr addr,
1337 MemTxAttrs attrs, MemTxResult *result);
1338uint32_t address_space_ldl_le(AddressSpace *as, hwaddr addr,
1339 MemTxAttrs attrs, MemTxResult *result);
1340uint32_t address_space_ldl_be(AddressSpace *as, hwaddr addr,
1341 MemTxAttrs attrs, MemTxResult *result);
1342uint64_t address_space_ldq_le(AddressSpace *as, hwaddr addr,
1343 MemTxAttrs attrs, MemTxResult *result);
1344uint64_t address_space_ldq_be(AddressSpace *as, hwaddr addr,
1345 MemTxAttrs attrs, MemTxResult *result);
1346void address_space_stb(AddressSpace *as, hwaddr addr, uint32_t val,
1347 MemTxAttrs attrs, MemTxResult *result);
1348void address_space_stw_le(AddressSpace *as, hwaddr addr, uint32_t val,
1349 MemTxAttrs attrs, MemTxResult *result);
1350void address_space_stw_be(AddressSpace *as, hwaddr addr, uint32_t val,
1351 MemTxAttrs attrs, MemTxResult *result);
1352void address_space_stl_le(AddressSpace *as, hwaddr addr, uint32_t val,
1353 MemTxAttrs attrs, MemTxResult *result);
1354void address_space_stl_be(AddressSpace *as, hwaddr addr, uint32_t val,
1355 MemTxAttrs attrs, MemTxResult *result);
1356void address_space_stq_le(AddressSpace *as, hwaddr addr, uint64_t val,
1357 MemTxAttrs attrs, MemTxResult *result);
1358void address_space_stq_be(AddressSpace *as, hwaddr addr, uint64_t val,
1359 MemTxAttrs attrs, MemTxResult *result);
1360
149f54b5 1361/* address_space_translate: translate an address range into an address space
41063e1e
PB
1362 * into a MemoryRegion and an address range into that section. Should be
1363 * called from an RCU critical section, to avoid that the last reference
1364 * to the returned region disappears after address_space_translate returns.
149f54b5
PB
1365 *
1366 * @as: #AddressSpace to be accessed
1367 * @addr: address within that address space
1368 * @xlat: pointer to address within the returned memory region section's
1369 * #MemoryRegion.
1370 * @len: pointer to length
1371 * @is_write: indicates the transfer direction
1372 */
5c8a00ce
PB
1373MemoryRegion *address_space_translate(AddressSpace *as, hwaddr addr,
1374 hwaddr *xlat, hwaddr *len,
1375 bool is_write);
149f54b5 1376
51644ab7
PB
1377/* address_space_access_valid: check for validity of accessing an address
1378 * space range
1379 *
30951157
AK
1380 * Check whether memory is assigned to the given address space range, and
1381 * access is permitted by any IOMMU regions that are active for the address
1382 * space.
51644ab7
PB
1383 *
1384 * For now, addr and len should be aligned to a page size. This limitation
1385 * will be lifted in the future.
1386 *
1387 * @as: #AddressSpace to be accessed
1388 * @addr: address within that address space
1389 * @len: length of the area to be checked
1390 * @is_write: indicates the transfer direction
1391 */
1392bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, bool is_write);
1393
ac1970fb
AK
1394/* address_space_map: map a physical memory region into a host virtual address
1395 *
1396 * May map a subset of the requested range, given by and returned in @plen.
1397 * May return %NULL if resources needed to perform the mapping are exhausted.
1398 * Use only for reads OR writes - not for read-modify-write operations.
1399 * Use cpu_register_map_client() to know when retrying the map operation is
1400 * likely to succeed.
1401 *
1402 * @as: #AddressSpace to be accessed
1403 * @addr: address within that address space
1404 * @plen: pointer to length of buffer; updated on return
1405 * @is_write: indicates the transfer direction
1406 */
a8170e5e
AK
1407void *address_space_map(AddressSpace *as, hwaddr addr,
1408 hwaddr *plen, bool is_write);
ac1970fb
AK
1409
1410/* address_space_unmap: Unmaps a memory region previously mapped by address_space_map()
1411 *
1412 * Will also mark the memory as dirty if @is_write == %true. @access_len gives
1413 * the amount of memory that was actually read or written by the caller.
1414 *
1415 * @as: #AddressSpace used
1416 * @addr: address within that address space
1417 * @len: buffer length as returned by address_space_map()
1418 * @access_len: amount of data actually transferred
1419 * @is_write: indicates the transfer direction
1420 */
a8170e5e
AK
1421void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len,
1422 int is_write, hwaddr access_len);
ac1970fb
AK
1423
1424
a203ac70
PB
1425/* Internal functions, part of the implementation of address_space_read. */
1426MemTxResult address_space_read_continue(AddressSpace *as, hwaddr addr,
1427 MemTxAttrs attrs, uint8_t *buf,
1428 int len, hwaddr addr1, hwaddr l,
1429 MemoryRegion *mr);
3cc8f884
PB
1430MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr,
1431 MemTxAttrs attrs, uint8_t *buf, int len);
0878d0e1 1432void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr);
3cc8f884
PB
1433
1434static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write)
1435{
1436 if (is_write) {
1437 return memory_region_is_ram(mr) && !mr->readonly;
1438 } else {
1439 return memory_region_is_ram(mr) || memory_region_is_romd(mr);
1440 }
3cc8f884
PB
1441}
1442
1443/**
1444 * address_space_read: read from an address space.
1445 *
1446 * Return a MemTxResult indicating whether the operation succeeded
1447 * or failed (eg unassigned memory, device rejected the transaction,
1448 * IOMMU fault).
1449 *
1450 * @as: #AddressSpace to be accessed
1451 * @addr: address within that address space
1452 * @attrs: memory transaction attributes
1453 * @buf: buffer with the data transferred
1454 */
1455static inline __attribute__((__always_inline__))
1456MemTxResult address_space_read(AddressSpace *as, hwaddr addr, MemTxAttrs attrs,
1457 uint8_t *buf, int len)
1458{
1459 MemTxResult result = MEMTX_OK;
1460 hwaddr l, addr1;
1461 void *ptr;
1462 MemoryRegion *mr;
1463
1464 if (__builtin_constant_p(len)) {
1465 if (len) {
1466 rcu_read_lock();
1467 l = len;
1468 mr = address_space_translate(as, addr, &addr1, &l, false);
1469 if (len == l && memory_access_is_direct(mr, false)) {
0878d0e1 1470 ptr = qemu_map_ram_ptr(mr->ram_block, addr1);
3cc8f884
PB
1471 memcpy(buf, ptr, len);
1472 } else {
1473 result = address_space_read_continue(as, addr, attrs, buf, len,
1474 addr1, l, mr);
1475 }
1476 rcu_read_unlock();
1477 }
1478 } else {
1479 result = address_space_read_full(as, addr, attrs, buf, len);
1480 }
1481 return result;
1482}
a203ac70 1483
093bc2cd
AK
1484#endif
1485
1486#endif