]>
Commit | Line | Data |
---|---|---|
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 | ||
022c62cb PB |
19 | #include "exec/cpu-common.h" |
20 | #include "exec/hwaddr.h" | |
cc05c43a | 21 | #include "exec/memattrs.h" |
0987d735 | 22 | #include "exec/ramlist.h" |
1de7afc9 | 23 | #include "qemu/queue.h" |
1de7afc9 | 24 | #include "qemu/int128.h" |
06866575 | 25 | #include "qemu/notify.h" |
b4fefef9 | 26 | #include "qom/object.h" |
374f2981 | 27 | #include "qemu/rcu.h" |
1221a474 | 28 | #include "hw/qdev-core.h" |
093bc2cd | 29 | |
07bdaa41 PB |
30 | #define RAM_ADDR_INVALID (~(ram_addr_t)0) |
31 | ||
052e87b0 PB |
32 | #define MAX_PHYS_ADDR_SPACE_BITS 62 |
33 | #define MAX_PHYS_ADDR (((hwaddr)1 << MAX_PHYS_ADDR_SPACE_BITS) - 1) | |
34 | ||
b4fefef9 PC |
35 | #define TYPE_MEMORY_REGION "qemu:memory-region" |
36 | #define MEMORY_REGION(obj) \ | |
37 | OBJECT_CHECK(MemoryRegion, (obj), TYPE_MEMORY_REGION) | |
38 | ||
3df9d748 AK |
39 | #define TYPE_IOMMU_MEMORY_REGION "qemu:iommu-memory-region" |
40 | #define IOMMU_MEMORY_REGION(obj) \ | |
41 | OBJECT_CHECK(IOMMUMemoryRegion, (obj), TYPE_IOMMU_MEMORY_REGION) | |
1221a474 AK |
42 | #define IOMMU_MEMORY_REGION_CLASS(klass) \ |
43 | OBJECT_CLASS_CHECK(IOMMUMemoryRegionClass, (klass), \ | |
44 | TYPE_IOMMU_MEMORY_REGION) | |
45 | #define IOMMU_MEMORY_REGION_GET_CLASS(obj) \ | |
46 | OBJECT_GET_CLASS(IOMMUMemoryRegionClass, (obj), \ | |
47 | TYPE_IOMMU_MEMORY_REGION) | |
3df9d748 | 48 | |
093bc2cd | 49 | typedef struct MemoryRegionOps MemoryRegionOps; |
74901c3b | 50 | typedef struct MemoryRegionMmio MemoryRegionMmio; |
093bc2cd | 51 | |
74901c3b AK |
52 | struct MemoryRegionMmio { |
53 | CPUReadMemoryFunc *read[3]; | |
54 | CPUWriteMemoryFunc *write[3]; | |
55 | }; | |
56 | ||
30951157 AK |
57 | typedef struct IOMMUTLBEntry IOMMUTLBEntry; |
58 | ||
59 | /* See address_space_translate: bit 0 is read, bit 1 is write. */ | |
60 | typedef enum { | |
61 | IOMMU_NONE = 0, | |
62 | IOMMU_RO = 1, | |
63 | IOMMU_WO = 2, | |
64 | IOMMU_RW = 3, | |
65 | } IOMMUAccessFlags; | |
66 | ||
f06a696d PX |
67 | #define IOMMU_ACCESS_FLAG(r, w) (((r) ? IOMMU_RO : 0) | ((w) ? IOMMU_WO : 0)) |
68 | ||
30951157 AK |
69 | struct IOMMUTLBEntry { |
70 | AddressSpace *target_as; | |
71 | hwaddr iova; | |
72 | hwaddr translated_addr; | |
73 | hwaddr addr_mask; /* 0xfff = 4k translation */ | |
74 | IOMMUAccessFlags perm; | |
75 | }; | |
76 | ||
cdb30812 PX |
77 | /* |
78 | * Bitmap for different IOMMUNotifier capabilities. Each notifier can | |
79 | * register with one or multiple IOMMU Notifier capability bit(s). | |
80 | */ | |
81 | typedef enum { | |
82 | IOMMU_NOTIFIER_NONE = 0, | |
83 | /* Notify cache invalidations */ | |
84 | IOMMU_NOTIFIER_UNMAP = 0x1, | |
85 | /* Notify entry changes (newly created entries) */ | |
86 | IOMMU_NOTIFIER_MAP = 0x2, | |
87 | } IOMMUNotifierFlag; | |
88 | ||
89 | #define IOMMU_NOTIFIER_ALL (IOMMU_NOTIFIER_MAP | IOMMU_NOTIFIER_UNMAP) | |
90 | ||
698feb5e PX |
91 | struct IOMMUNotifier; |
92 | typedef void (*IOMMUNotify)(struct IOMMUNotifier *notifier, | |
93 | IOMMUTLBEntry *data); | |
94 | ||
cdb30812 | 95 | struct IOMMUNotifier { |
698feb5e | 96 | IOMMUNotify notify; |
cdb30812 | 97 | IOMMUNotifierFlag notifier_flags; |
698feb5e PX |
98 | /* Notify for address space range start <= addr <= end */ |
99 | hwaddr start; | |
100 | hwaddr end; | |
cb1efcf4 | 101 | int iommu_idx; |
cdb30812 PX |
102 | QLIST_ENTRY(IOMMUNotifier) node; |
103 | }; | |
104 | typedef struct IOMMUNotifier IOMMUNotifier; | |
105 | ||
b0e5de93 JH |
106 | /* RAM is pre-allocated and passed into qemu_ram_alloc_from_ptr */ |
107 | #define RAM_PREALLOC (1 << 0) | |
108 | ||
109 | /* RAM is mmap-ed with MAP_SHARED */ | |
110 | #define RAM_SHARED (1 << 1) | |
111 | ||
112 | /* Only a portion of RAM (used_length) is actually used, and migrated. | |
113 | * This used_length size can change across reboots. | |
114 | */ | |
115 | #define RAM_RESIZEABLE (1 << 2) | |
116 | ||
117 | /* UFFDIO_ZEROPAGE is available on this RAMBlock to atomically | |
118 | * zero the page and wake waiting processes. | |
119 | * (Set during postcopy) | |
120 | */ | |
121 | #define RAM_UF_ZEROPAGE (1 << 3) | |
122 | ||
123 | /* RAM can be migrated */ | |
124 | #define RAM_MIGRATABLE (1 << 4) | |
125 | ||
a4de8552 JH |
126 | /* RAM is a persistent kind memory */ |
127 | #define RAM_PMEM (1 << 5) | |
128 | ||
698feb5e PX |
129 | static inline void iommu_notifier_init(IOMMUNotifier *n, IOMMUNotify fn, |
130 | IOMMUNotifierFlag flags, | |
cb1efcf4 PM |
131 | hwaddr start, hwaddr end, |
132 | int iommu_idx) | |
698feb5e PX |
133 | { |
134 | n->notify = fn; | |
135 | n->notifier_flags = flags; | |
136 | n->start = start; | |
137 | n->end = end; | |
cb1efcf4 | 138 | n->iommu_idx = iommu_idx; |
698feb5e PX |
139 | } |
140 | ||
093bc2cd AK |
141 | /* |
142 | * Memory region callbacks | |
143 | */ | |
144 | struct MemoryRegionOps { | |
145 | /* Read from the memory region. @addr is relative to @mr; @size is | |
146 | * in bytes. */ | |
147 | uint64_t (*read)(void *opaque, | |
a8170e5e | 148 | hwaddr addr, |
093bc2cd AK |
149 | unsigned size); |
150 | /* Write to the memory region. @addr is relative to @mr; @size is | |
151 | * in bytes. */ | |
152 | void (*write)(void *opaque, | |
a8170e5e | 153 | hwaddr addr, |
093bc2cd AK |
154 | uint64_t data, |
155 | unsigned size); | |
156 | ||
cc05c43a PM |
157 | MemTxResult (*read_with_attrs)(void *opaque, |
158 | hwaddr addr, | |
159 | uint64_t *data, | |
160 | unsigned size, | |
161 | MemTxAttrs attrs); | |
162 | MemTxResult (*write_with_attrs)(void *opaque, | |
163 | hwaddr addr, | |
164 | uint64_t data, | |
165 | unsigned size, | |
166 | MemTxAttrs attrs); | |
167 | ||
093bc2cd AK |
168 | enum device_endian endianness; |
169 | /* Guest-visible constraints: */ | |
170 | struct { | |
171 | /* If nonzero, specify bounds on access sizes beyond which a machine | |
172 | * check is thrown. | |
173 | */ | |
174 | unsigned min_access_size; | |
175 | unsigned max_access_size; | |
176 | /* If true, unaligned accesses are supported. Otherwise unaligned | |
177 | * accesses throw machine checks. | |
178 | */ | |
179 | bool unaligned; | |
897fa7cf AK |
180 | /* |
181 | * If present, and returns #false, the transaction is not accepted | |
182 | * by the device (and results in machine dependent behaviour such | |
183 | * as a machine check exception). | |
184 | */ | |
a8170e5e | 185 | bool (*accepts)(void *opaque, hwaddr addr, |
8372d383 PM |
186 | unsigned size, bool is_write, |
187 | MemTxAttrs attrs); | |
093bc2cd AK |
188 | } valid; |
189 | /* Internal implementation constraints: */ | |
190 | struct { | |
191 | /* If nonzero, specifies the minimum size implemented. Smaller sizes | |
192 | * will be rounded upwards and a partial result will be returned. | |
193 | */ | |
194 | unsigned min_access_size; | |
195 | /* If nonzero, specifies the maximum size implemented. Larger sizes | |
196 | * will be done as a series of accesses with smaller sizes. | |
197 | */ | |
198 | unsigned max_access_size; | |
199 | /* If true, unaligned accesses are supported. Otherwise all accesses | |
200 | * are converted to (possibly multiple) naturally aligned accesses. | |
201 | */ | |
edc1ba7a | 202 | bool unaligned; |
093bc2cd | 203 | } impl; |
627a0e90 | 204 | |
74901c3b AK |
205 | /* If .read and .write are not present, old_mmio may be used for |
206 | * backwards compatibility with old mmio registration | |
207 | */ | |
208 | const MemoryRegionMmio old_mmio; | |
093bc2cd AK |
209 | }; |
210 | ||
f1334de6 AK |
211 | enum IOMMUMemoryRegionAttr { |
212 | IOMMU_ATTR_SPAPR_TCE_FD | |
213 | }; | |
214 | ||
2ce931d0 PM |
215 | /** |
216 | * IOMMUMemoryRegionClass: | |
217 | * | |
218 | * All IOMMU implementations need to subclass TYPE_IOMMU_MEMORY_REGION | |
219 | * and provide an implementation of at least the @translate method here | |
220 | * to handle requests to the memory region. Other methods are optional. | |
221 | * | |
222 | * The IOMMU implementation must use the IOMMU notifier infrastructure | |
223 | * to report whenever mappings are changed, by calling | |
224 | * memory_region_notify_iommu() (or, if necessary, by calling | |
225 | * memory_region_notify_one() for each registered notifier). | |
21f40209 PM |
226 | * |
227 | * Conceptually an IOMMU provides a mapping from input address | |
228 | * to an output TLB entry. If the IOMMU is aware of memory transaction | |
229 | * attributes and the output TLB entry depends on the transaction | |
230 | * attributes, we represent this using IOMMU indexes. Each index | |
231 | * selects a particular translation table that the IOMMU has: | |
232 | * @attrs_to_index returns the IOMMU index for a set of transaction attributes | |
233 | * @translate takes an input address and an IOMMU index | |
234 | * and the mapping returned can only depend on the input address and the | |
235 | * IOMMU index. | |
236 | * | |
237 | * Most IOMMUs don't care about the transaction attributes and support | |
238 | * only a single IOMMU index. A more complex IOMMU might have one index | |
239 | * for secure transactions and one for non-secure transactions. | |
2ce931d0 | 240 | */ |
1221a474 AK |
241 | typedef struct IOMMUMemoryRegionClass { |
242 | /* private */ | |
243 | struct DeviceClass parent_class; | |
30951157 | 244 | |
bf55b7af | 245 | /* |
2ce931d0 PM |
246 | * Return a TLB entry that contains a given address. |
247 | * | |
248 | * The IOMMUAccessFlags indicated via @flag are optional and may | |
249 | * be specified as IOMMU_NONE to indicate that the caller needs | |
250 | * the full translation information for both reads and writes. If | |
251 | * the access flags are specified then the IOMMU implementation | |
252 | * may use this as an optimization, to stop doing a page table | |
253 | * walk as soon as it knows that the requested permissions are not | |
254 | * allowed. If IOMMU_NONE is passed then the IOMMU must do the | |
255 | * full page table walk and report the permissions in the returned | |
256 | * IOMMUTLBEntry. (Note that this implies that an IOMMU may not | |
257 | * return different mappings for reads and writes.) | |
258 | * | |
259 | * The returned information remains valid while the caller is | |
260 | * holding the big QEMU lock or is inside an RCU critical section; | |
261 | * if the caller wishes to cache the mapping beyond that it must | |
262 | * register an IOMMU notifier so it can invalidate its cached | |
263 | * information when the IOMMU mapping changes. | |
264 | * | |
265 | * @iommu: the IOMMUMemoryRegion | |
266 | * @hwaddr: address to be translated within the memory region | |
267 | * @flag: requested access permissions | |
2c91bcf2 | 268 | * @iommu_idx: IOMMU index for the translation |
bf55b7af | 269 | */ |
3df9d748 | 270 | IOMMUTLBEntry (*translate)(IOMMUMemoryRegion *iommu, hwaddr addr, |
2c91bcf2 | 271 | IOMMUAccessFlags flag, int iommu_idx); |
2ce931d0 PM |
272 | /* Returns minimum supported page size in bytes. |
273 | * If this method is not provided then the minimum is assumed to | |
274 | * be TARGET_PAGE_SIZE. | |
275 | * | |
276 | * @iommu: the IOMMUMemoryRegion | |
277 | */ | |
3df9d748 | 278 | uint64_t (*get_min_page_size)(IOMMUMemoryRegion *iommu); |
2ce931d0 PM |
279 | /* Called when IOMMU Notifier flag changes (ie when the set of |
280 | * events which IOMMU users are requesting notification for changes). | |
281 | * Optional method -- need not be provided if the IOMMU does not | |
282 | * need to know exactly which events must be notified. | |
283 | * | |
284 | * @iommu: the IOMMUMemoryRegion | |
285 | * @old_flags: events which previously needed to be notified | |
286 | * @new_flags: events which now need to be notified | |
287 | */ | |
3df9d748 | 288 | void (*notify_flag_changed)(IOMMUMemoryRegion *iommu, |
5bf3d319 PX |
289 | IOMMUNotifierFlag old_flags, |
290 | IOMMUNotifierFlag new_flags); | |
2ce931d0 PM |
291 | /* Called to handle memory_region_iommu_replay(). |
292 | * | |
293 | * The default implementation of memory_region_iommu_replay() is to | |
294 | * call the IOMMU translate method for every page in the address space | |
295 | * with flag == IOMMU_NONE and then call the notifier if translate | |
296 | * returns a valid mapping. If this method is implemented then it | |
297 | * overrides the default behaviour, and must provide the full semantics | |
298 | * of memory_region_iommu_replay(), by calling @notifier for every | |
299 | * translation present in the IOMMU. | |
300 | * | |
301 | * Optional method -- an IOMMU only needs to provide this method | |
302 | * if the default is inefficient or produces undesirable side effects. | |
303 | * | |
304 | * Note: this is not related to record-and-replay functionality. | |
305 | */ | |
3df9d748 | 306 | void (*replay)(IOMMUMemoryRegion *iommu, IOMMUNotifier *notifier); |
f1334de6 | 307 | |
2ce931d0 PM |
308 | /* Get IOMMU misc attributes. This is an optional method that |
309 | * can be used to allow users of the IOMMU to get implementation-specific | |
310 | * information. The IOMMU implements this method to handle calls | |
311 | * by IOMMU users to memory_region_iommu_get_attr() by filling in | |
312 | * the arbitrary data pointer for any IOMMUMemoryRegionAttr values that | |
313 | * the IOMMU supports. If the method is unimplemented then | |
314 | * memory_region_iommu_get_attr() will always return -EINVAL. | |
315 | * | |
316 | * @iommu: the IOMMUMemoryRegion | |
317 | * @attr: attribute being queried | |
318 | * @data: memory to fill in with the attribute data | |
319 | * | |
320 | * Returns 0 on success, or a negative errno; in particular | |
321 | * returns -EINVAL for unrecognized or unimplemented attribute types. | |
322 | */ | |
323 | int (*get_attr)(IOMMUMemoryRegion *iommu, enum IOMMUMemoryRegionAttr attr, | |
f1334de6 | 324 | void *data); |
21f40209 PM |
325 | |
326 | /* Return the IOMMU index to use for a given set of transaction attributes. | |
327 | * | |
328 | * Optional method: if an IOMMU only supports a single IOMMU index then | |
329 | * the default implementation of memory_region_iommu_attrs_to_index() | |
330 | * will return 0. | |
331 | * | |
332 | * The indexes supported by an IOMMU must be contiguous, starting at 0. | |
333 | * | |
334 | * @iommu: the IOMMUMemoryRegion | |
335 | * @attrs: memory transaction attributes | |
336 | */ | |
337 | int (*attrs_to_index)(IOMMUMemoryRegion *iommu, MemTxAttrs attrs); | |
338 | ||
339 | /* Return the number of IOMMU indexes this IOMMU supports. | |
340 | * | |
341 | * Optional method: if this method is not provided, then | |
342 | * memory_region_iommu_num_indexes() will return 1, indicating that | |
343 | * only a single IOMMU index is supported. | |
344 | * | |
345 | * @iommu: the IOMMUMemoryRegion | |
346 | */ | |
347 | int (*num_indexes)(IOMMUMemoryRegion *iommu); | |
1221a474 | 348 | } IOMMUMemoryRegionClass; |
30951157 | 349 | |
093bc2cd | 350 | typedef struct CoalescedMemoryRange CoalescedMemoryRange; |
3e9d69e7 | 351 | typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd; |
093bc2cd AK |
352 | |
353 | struct MemoryRegion { | |
b4fefef9 | 354 | Object parent_obj; |
a676854f | 355 | |
093bc2cd | 356 | /* All fields are private - violators will be prosecuted */ |
a676854f PB |
357 | |
358 | /* The following fields should fit in a cache line */ | |
359 | bool romd_mode; | |
360 | bool ram; | |
361 | bool subpage; | |
362 | bool readonly; /* For RAM regions */ | |
363 | bool rom_device; | |
364 | bool flush_coalesced_mmio; | |
365 | bool global_locking; | |
366 | uint8_t dirty_log_mask; | |
3df9d748 | 367 | bool is_iommu; |
58eaa217 | 368 | RAMBlock *ram_block; |
612263cf | 369 | Object *owner; |
a676854f PB |
370 | |
371 | const MemoryRegionOps *ops; | |
093bc2cd | 372 | void *opaque; |
feca4ac1 | 373 | MemoryRegion *container; |
08dafab4 | 374 | Int128 size; |
a8170e5e | 375 | hwaddr addr; |
545e92e0 | 376 | void (*destructor)(MemoryRegion *mr); |
a2b257d6 | 377 | uint64_t align; |
14a3c10a | 378 | bool terminates; |
21e00fa5 | 379 | bool ram_device; |
6bba19ba | 380 | bool enabled; |
1660e72d | 381 | bool warning_printed; /* For reservations */ |
deb809ed | 382 | uint8_t vga_logging_count; |
093bc2cd | 383 | MemoryRegion *alias; |
a8170e5e | 384 | hwaddr alias_offset; |
d33382da | 385 | int32_t priority; |
093bc2cd AK |
386 | QTAILQ_HEAD(subregions, MemoryRegion) subregions; |
387 | QTAILQ_ENTRY(MemoryRegion) subregions_link; | |
388 | QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced; | |
302fa283 | 389 | const char *name; |
3e9d69e7 AK |
390 | unsigned ioeventfd_nb; |
391 | MemoryRegionIoeventfd *ioeventfds; | |
3df9d748 AK |
392 | }; |
393 | ||
394 | struct IOMMUMemoryRegion { | |
395 | MemoryRegion parent_obj; | |
396 | ||
cdb30812 | 397 | QLIST_HEAD(, IOMMUNotifier) iommu_notify; |
5bf3d319 | 398 | IOMMUNotifierFlag iommu_notify_flags; |
093bc2cd AK |
399 | }; |
400 | ||
512fa408 PX |
401 | #define IOMMU_NOTIFIER_FOREACH(n, mr) \ |
402 | QLIST_FOREACH((n), &(mr)->iommu_notify, node) | |
403 | ||
c2fc83e8 PB |
404 | /** |
405 | * MemoryListener: callbacks structure for updates to the physical memory map | |
406 | * | |
407 | * Allows a component to adjust to changes in the guest-visible memory map. | |
408 | * Use with memory_listener_register() and memory_listener_unregister(). | |
409 | */ | |
410 | struct MemoryListener { | |
411 | void (*begin)(MemoryListener *listener); | |
412 | void (*commit)(MemoryListener *listener); | |
413 | void (*region_add)(MemoryListener *listener, MemoryRegionSection *section); | |
414 | void (*region_del)(MemoryListener *listener, MemoryRegionSection *section); | |
415 | void (*region_nop)(MemoryListener *listener, MemoryRegionSection *section); | |
b2dfd71c PB |
416 | void (*log_start)(MemoryListener *listener, MemoryRegionSection *section, |
417 | int old, int new); | |
418 | void (*log_stop)(MemoryListener *listener, MemoryRegionSection *section, | |
419 | int old, int new); | |
c2fc83e8 PB |
420 | void (*log_sync)(MemoryListener *listener, MemoryRegionSection *section); |
421 | void (*log_global_start)(MemoryListener *listener); | |
422 | void (*log_global_stop)(MemoryListener *listener); | |
423 | void (*eventfd_add)(MemoryListener *listener, MemoryRegionSection *section, | |
424 | bool match_data, uint64_t data, EventNotifier *e); | |
425 | void (*eventfd_del)(MemoryListener *listener, MemoryRegionSection *section, | |
426 | bool match_data, uint64_t data, EventNotifier *e); | |
427 | void (*coalesced_mmio_add)(MemoryListener *listener, MemoryRegionSection *section, | |
428 | hwaddr addr, hwaddr len); | |
429 | void (*coalesced_mmio_del)(MemoryListener *listener, MemoryRegionSection *section, | |
430 | hwaddr addr, hwaddr len); | |
431 | /* Lower = earlier (during add), later (during del) */ | |
432 | unsigned priority; | |
d45fa784 | 433 | AddressSpace *address_space; |
c2fc83e8 | 434 | QTAILQ_ENTRY(MemoryListener) link; |
9a54635d | 435 | QTAILQ_ENTRY(MemoryListener) link_as; |
c2fc83e8 PB |
436 | }; |
437 | ||
9ad2bbc1 AK |
438 | /** |
439 | * AddressSpace: describes a mapping of addresses to #MemoryRegion objects | |
440 | */ | |
441 | struct AddressSpace { | |
442 | /* All fields are private. */ | |
374f2981 | 443 | struct rcu_head rcu; |
7dca8043 | 444 | char *name; |
9ad2bbc1 | 445 | MemoryRegion *root; |
374f2981 PB |
446 | |
447 | /* Accessed via RCU. */ | |
9ad2bbc1 | 448 | struct FlatView *current_map; |
374f2981 | 449 | |
9ad2bbc1 AK |
450 | int ioeventfd_nb; |
451 | struct MemoryRegionIoeventfd *ioeventfds; | |
9a54635d | 452 | QTAILQ_HEAD(memory_listeners_as, MemoryListener) listeners; |
0d673e36 | 453 | QTAILQ_ENTRY(AddressSpace) address_spaces_link; |
9ad2bbc1 AK |
454 | }; |
455 | ||
785a507e PB |
456 | typedef struct AddressSpaceDispatch AddressSpaceDispatch; |
457 | typedef struct FlatRange FlatRange; | |
458 | ||
459 | /* Flattened global view of current active memory hierarchy. Kept in sorted | |
460 | * order. | |
461 | */ | |
462 | struct FlatView { | |
463 | struct rcu_head rcu; | |
464 | unsigned ref; | |
465 | FlatRange *ranges; | |
466 | unsigned nr; | |
467 | unsigned nr_allocated; | |
468 | struct AddressSpaceDispatch *dispatch; | |
469 | MemoryRegion *root; | |
470 | }; | |
471 | ||
472 | static inline FlatView *address_space_to_flatview(AddressSpace *as) | |
473 | { | |
474 | return atomic_rcu_read(&as->current_map); | |
475 | } | |
476 | ||
16620684 | 477 | |
e2177955 AK |
478 | /** |
479 | * MemoryRegionSection: describes a fragment of a #MemoryRegion | |
480 | * | |
481 | * @mr: the region, or %NULL if empty | |
57914ecb | 482 | * @fv: the flat view of the address space the region is mapped in |
e2177955 AK |
483 | * @offset_within_region: the beginning of the section, relative to @mr's start |
484 | * @size: the size of the section; will not exceed @mr's boundaries | |
485 | * @offset_within_address_space: the address of the first byte of the section | |
486 | * relative to the region's address space | |
7a8499e8 | 487 | * @readonly: writes to this section are ignored |
e2177955 AK |
488 | */ |
489 | struct MemoryRegionSection { | |
490 | MemoryRegion *mr; | |
16620684 | 491 | FlatView *fv; |
a8170e5e | 492 | hwaddr offset_within_region; |
052e87b0 | 493 | Int128 size; |
a8170e5e | 494 | hwaddr offset_within_address_space; |
7a8499e8 | 495 | bool readonly; |
e2177955 AK |
496 | }; |
497 | ||
093bc2cd AK |
498 | /** |
499 | * memory_region_init: Initialize a memory region | |
500 | * | |
69ddaf66 | 501 | * The region typically acts as a container for other memory regions. Use |
093bc2cd AK |
502 | * memory_region_add_subregion() to add subregions. |
503 | * | |
504 | * @mr: the #MemoryRegion to be initialized | |
2c9b15ca | 505 | * @owner: the object that tracks the region's reference count |
093bc2cd AK |
506 | * @name: used for debugging; not visible to the user or ABI |
507 | * @size: size of the region; any subregions beyond this size will be clipped | |
508 | */ | |
509 | void memory_region_init(MemoryRegion *mr, | |
2c9b15ca | 510 | struct Object *owner, |
093bc2cd AK |
511 | const char *name, |
512 | uint64_t size); | |
46637be2 PB |
513 | |
514 | /** | |
515 | * memory_region_ref: Add 1 to a memory region's reference count | |
516 | * | |
517 | * Whenever memory regions are accessed outside the BQL, they need to be | |
518 | * preserved against hot-unplug. MemoryRegions actually do not have their | |
519 | * own reference count; they piggyback on a QOM object, their "owner". | |
520 | * This function adds a reference to the owner. | |
521 | * | |
522 | * All MemoryRegions must have an owner if they can disappear, even if the | |
523 | * device they belong to operates exclusively under the BQL. This is because | |
524 | * the region could be returned at any time by memory_region_find, and this | |
525 | * is usually under guest control. | |
526 | * | |
527 | * @mr: the #MemoryRegion | |
528 | */ | |
529 | void memory_region_ref(MemoryRegion *mr); | |
530 | ||
531 | /** | |
532 | * memory_region_unref: Remove 1 to a memory region's reference count | |
533 | * | |
534 | * Whenever memory regions are accessed outside the BQL, they need to be | |
535 | * preserved against hot-unplug. MemoryRegions actually do not have their | |
536 | * own reference count; they piggyback on a QOM object, their "owner". | |
537 | * This function removes a reference to the owner and possibly destroys it. | |
538 | * | |
539 | * @mr: the #MemoryRegion | |
540 | */ | |
541 | void memory_region_unref(MemoryRegion *mr); | |
542 | ||
093bc2cd AK |
543 | /** |
544 | * memory_region_init_io: Initialize an I/O memory region. | |
545 | * | |
69ddaf66 | 546 | * Accesses into the region will cause the callbacks in @ops to be called. |
093bc2cd AK |
547 | * if @size is nonzero, subregions will be clipped to @size. |
548 | * | |
549 | * @mr: the #MemoryRegion to be initialized. | |
2c9b15ca | 550 | * @owner: the object that tracks the region's reference count |
093bc2cd AK |
551 | * @ops: a structure containing read and write callbacks to be used when |
552 | * I/O is performed on the region. | |
b6af0975 | 553 | * @opaque: passed to the read and write callbacks of the @ops structure. |
093bc2cd AK |
554 | * @name: used for debugging; not visible to the user or ABI |
555 | * @size: size of the region. | |
556 | */ | |
557 | void memory_region_init_io(MemoryRegion *mr, | |
2c9b15ca | 558 | struct Object *owner, |
093bc2cd AK |
559 | const MemoryRegionOps *ops, |
560 | void *opaque, | |
561 | const char *name, | |
562 | uint64_t size); | |
563 | ||
564 | /** | |
1cfe48c1 PM |
565 | * memory_region_init_ram_nomigrate: Initialize RAM memory region. Accesses |
566 | * into the region will modify memory | |
567 | * directly. | |
093bc2cd AK |
568 | * |
569 | * @mr: the #MemoryRegion to be initialized. | |
2c9b15ca | 570 | * @owner: the object that tracks the region's reference count |
e8f5fe2d DDAG |
571 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
572 | * must be unique within any device | |
093bc2cd | 573 | * @size: size of the region. |
49946538 | 574 | * @errp: pointer to Error*, to store an error if it happens. |
a5c0234b PM |
575 | * |
576 | * Note that this function does not do anything to cause the data in the | |
577 | * RAM memory region to be migrated; that is the responsibility of the caller. | |
093bc2cd | 578 | */ |
1cfe48c1 PM |
579 | void memory_region_init_ram_nomigrate(MemoryRegion *mr, |
580 | struct Object *owner, | |
581 | const char *name, | |
582 | uint64_t size, | |
583 | Error **errp); | |
093bc2cd | 584 | |
06329cce MA |
585 | /** |
586 | * memory_region_init_ram_shared_nomigrate: Initialize RAM memory region. | |
587 | * Accesses into the region will | |
588 | * modify memory directly. | |
589 | * | |
590 | * @mr: the #MemoryRegion to be initialized. | |
591 | * @owner: the object that tracks the region's reference count | |
592 | * @name: Region name, becomes part of RAMBlock name used in migration stream | |
593 | * must be unique within any device | |
594 | * @size: size of the region. | |
595 | * @share: allow remapping RAM to different addresses | |
596 | * @errp: pointer to Error*, to store an error if it happens. | |
597 | * | |
598 | * Note that this function is similar to memory_region_init_ram_nomigrate. | |
599 | * The only difference is part of the RAM region can be remapped. | |
600 | */ | |
601 | void memory_region_init_ram_shared_nomigrate(MemoryRegion *mr, | |
602 | struct Object *owner, | |
603 | const char *name, | |
604 | uint64_t size, | |
605 | bool share, | |
606 | Error **errp); | |
607 | ||
60786ef3 MT |
608 | /** |
609 | * memory_region_init_resizeable_ram: Initialize memory region with resizeable | |
610 | * RAM. Accesses into the region will | |
611 | * modify memory directly. Only an initial | |
612 | * portion of this RAM is actually used. | |
613 | * The used size can change across reboots. | |
614 | * | |
615 | * @mr: the #MemoryRegion to be initialized. | |
616 | * @owner: the object that tracks the region's reference count | |
e8f5fe2d DDAG |
617 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
618 | * must be unique within any device | |
60786ef3 MT |
619 | * @size: used size of the region. |
620 | * @max_size: max size of the region. | |
621 | * @resized: callback to notify owner about used size change. | |
622 | * @errp: pointer to Error*, to store an error if it happens. | |
a5c0234b PM |
623 | * |
624 | * Note that this function does not do anything to cause the data in the | |
625 | * RAM memory region to be migrated; that is the responsibility of the caller. | |
60786ef3 MT |
626 | */ |
627 | void memory_region_init_resizeable_ram(MemoryRegion *mr, | |
628 | struct Object *owner, | |
629 | const char *name, | |
630 | uint64_t size, | |
631 | uint64_t max_size, | |
632 | void (*resized)(const char*, | |
633 | uint64_t length, | |
634 | void *host), | |
635 | Error **errp); | |
d5dbde46 | 636 | #ifdef CONFIG_POSIX |
cbfc0171 | 637 | |
0b183fc8 PB |
638 | /** |
639 | * memory_region_init_ram_from_file: Initialize RAM memory region with a | |
640 | * mmap-ed backend. | |
641 | * | |
642 | * @mr: the #MemoryRegion to be initialized. | |
643 | * @owner: the object that tracks the region's reference count | |
e8f5fe2d DDAG |
644 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
645 | * must be unique within any device | |
0b183fc8 | 646 | * @size: size of the region. |
98376843 HZ |
647 | * @align: alignment of the region base address; if 0, the default alignment |
648 | * (getpagesize()) will be used. | |
cbfc0171 JH |
649 | * @ram_flags: Memory region features: |
650 | * - RAM_SHARED: memory must be mmaped with the MAP_SHARED flag | |
a4de8552 | 651 | * - RAM_PMEM: the memory is persistent memory |
cbfc0171 | 652 | * Other bits are ignored now. |
0b183fc8 | 653 | * @path: the path in which to allocate the RAM. |
7f56e740 | 654 | * @errp: pointer to Error*, to store an error if it happens. |
a5c0234b PM |
655 | * |
656 | * Note that this function does not do anything to cause the data in the | |
657 | * RAM memory region to be migrated; that is the responsibility of the caller. | |
0b183fc8 PB |
658 | */ |
659 | void memory_region_init_ram_from_file(MemoryRegion *mr, | |
660 | struct Object *owner, | |
661 | const char *name, | |
662 | uint64_t size, | |
98376843 | 663 | uint64_t align, |
cbfc0171 | 664 | uint32_t ram_flags, |
7f56e740 PB |
665 | const char *path, |
666 | Error **errp); | |
fea617c5 MAL |
667 | |
668 | /** | |
669 | * memory_region_init_ram_from_fd: Initialize RAM memory region with a | |
670 | * mmap-ed backend. | |
671 | * | |
672 | * @mr: the #MemoryRegion to be initialized. | |
673 | * @owner: the object that tracks the region's reference count | |
674 | * @name: the name of the region. | |
675 | * @size: size of the region. | |
676 | * @share: %true if memory must be mmaped with the MAP_SHARED flag | |
677 | * @fd: the fd to mmap. | |
678 | * @errp: pointer to Error*, to store an error if it happens. | |
a5c0234b PM |
679 | * |
680 | * Note that this function does not do anything to cause the data in the | |
681 | * RAM memory region to be migrated; that is the responsibility of the caller. | |
fea617c5 MAL |
682 | */ |
683 | void memory_region_init_ram_from_fd(MemoryRegion *mr, | |
684 | struct Object *owner, | |
685 | const char *name, | |
686 | uint64_t size, | |
687 | bool share, | |
688 | int fd, | |
689 | Error **errp); | |
0b183fc8 PB |
690 | #endif |
691 | ||
093bc2cd | 692 | /** |
1a7e8cae BZ |
693 | * memory_region_init_ram_ptr: Initialize RAM memory region from a |
694 | * user-provided pointer. Accesses into the | |
695 | * region will modify memory directly. | |
093bc2cd AK |
696 | * |
697 | * @mr: the #MemoryRegion to be initialized. | |
2c9b15ca | 698 | * @owner: the object that tracks the region's reference count |
e8f5fe2d DDAG |
699 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
700 | * must be unique within any device | |
093bc2cd AK |
701 | * @size: size of the region. |
702 | * @ptr: memory to be mapped; must contain at least @size bytes. | |
a5c0234b PM |
703 | * |
704 | * Note that this function does not do anything to cause the data in the | |
705 | * RAM memory region to be migrated; that is the responsibility of the caller. | |
093bc2cd AK |
706 | */ |
707 | void memory_region_init_ram_ptr(MemoryRegion *mr, | |
2c9b15ca | 708 | struct Object *owner, |
093bc2cd AK |
709 | const char *name, |
710 | uint64_t size, | |
711 | void *ptr); | |
712 | ||
21e00fa5 AW |
713 | /** |
714 | * memory_region_init_ram_device_ptr: Initialize RAM device memory region from | |
715 | * a user-provided pointer. | |
716 | * | |
717 | * A RAM device represents a mapping to a physical device, such as to a PCI | |
718 | * MMIO BAR of an vfio-pci assigned device. The memory region may be mapped | |
719 | * into the VM address space and access to the region will modify memory | |
720 | * directly. However, the memory region should not be included in a memory | |
721 | * dump (device may not be enabled/mapped at the time of the dump), and | |
722 | * operations incompatible with manipulating MMIO should be avoided. Replaces | |
723 | * skip_dump flag. | |
724 | * | |
725 | * @mr: the #MemoryRegion to be initialized. | |
726 | * @owner: the object that tracks the region's reference count | |
727 | * @name: the name of the region. | |
728 | * @size: size of the region. | |
729 | * @ptr: memory to be mapped; must contain at least @size bytes. | |
a5c0234b PM |
730 | * |
731 | * Note that this function does not do anything to cause the data in the | |
732 | * RAM memory region to be migrated; that is the responsibility of the caller. | |
733 | * (For RAM device memory regions, migrating the contents rarely makes sense.) | |
21e00fa5 AW |
734 | */ |
735 | void memory_region_init_ram_device_ptr(MemoryRegion *mr, | |
736 | struct Object *owner, | |
737 | const char *name, | |
738 | uint64_t size, | |
739 | void *ptr); | |
740 | ||
093bc2cd AK |
741 | /** |
742 | * memory_region_init_alias: Initialize a memory region that aliases all or a | |
743 | * part of another memory region. | |
744 | * | |
745 | * @mr: the #MemoryRegion to be initialized. | |
2c9b15ca | 746 | * @owner: the object that tracks the region's reference count |
093bc2cd AK |
747 | * @name: used for debugging; not visible to the user or ABI |
748 | * @orig: the region to be referenced; @mr will be equivalent to | |
749 | * @orig between @offset and @offset + @size - 1. | |
750 | * @offset: start of the section in @orig to be referenced. | |
751 | * @size: size of the region. | |
752 | */ | |
753 | void memory_region_init_alias(MemoryRegion *mr, | |
2c9b15ca | 754 | struct Object *owner, |
093bc2cd AK |
755 | const char *name, |
756 | MemoryRegion *orig, | |
a8170e5e | 757 | hwaddr offset, |
093bc2cd | 758 | uint64_t size); |
d0a9b5bc | 759 | |
a1777f7f | 760 | /** |
b59821a9 | 761 | * memory_region_init_rom_nomigrate: Initialize a ROM memory region. |
a1777f7f | 762 | * |
b59821a9 | 763 | * This has the same effect as calling memory_region_init_ram_nomigrate() |
a1777f7f PM |
764 | * and then marking the resulting region read-only with |
765 | * memory_region_set_readonly(). | |
766 | * | |
b59821a9 PM |
767 | * Note that this function does not do anything to cause the data in the |
768 | * RAM side of the memory region to be migrated; that is the responsibility | |
769 | * of the caller. | |
770 | * | |
a1777f7f PM |
771 | * @mr: the #MemoryRegion to be initialized. |
772 | * @owner: the object that tracks the region's reference count | |
e8f5fe2d DDAG |
773 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
774 | * must be unique within any device | |
a1777f7f PM |
775 | * @size: size of the region. |
776 | * @errp: pointer to Error*, to store an error if it happens. | |
777 | */ | |
b59821a9 PM |
778 | void memory_region_init_rom_nomigrate(MemoryRegion *mr, |
779 | struct Object *owner, | |
780 | const char *name, | |
781 | uint64_t size, | |
782 | Error **errp); | |
a1777f7f | 783 | |
d0a9b5bc | 784 | /** |
b59821a9 PM |
785 | * memory_region_init_rom_device_nomigrate: Initialize a ROM memory region. |
786 | * Writes are handled via callbacks. | |
787 | * | |
788 | * Note that this function does not do anything to cause the data in the | |
789 | * RAM side of the memory region to be migrated; that is the responsibility | |
790 | * of the caller. | |
d0a9b5bc AK |
791 | * |
792 | * @mr: the #MemoryRegion to be initialized. | |
2c9b15ca | 793 | * @owner: the object that tracks the region's reference count |
39e0b03d | 794 | * @ops: callbacks for write access handling (must not be NULL). |
57914ecb | 795 | * @opaque: passed to the read and write callbacks of the @ops structure. |
e8f5fe2d DDAG |
796 | * @name: Region name, becomes part of RAMBlock name used in migration stream |
797 | * must be unique within any device | |
d0a9b5bc | 798 | * @size: size of the region. |
33e0eb52 | 799 | * @errp: pointer to Error*, to store an error if it happens. |
d0a9b5bc | 800 | */ |
b59821a9 PM |
801 | void memory_region_init_rom_device_nomigrate(MemoryRegion *mr, |
802 | struct Object *owner, | |
803 | const MemoryRegionOps *ops, | |
804 | void *opaque, | |
805 | const char *name, | |
806 | uint64_t size, | |
807 | Error **errp); | |
d0a9b5bc | 808 | |
30951157 | 809 | /** |
1221a474 AK |
810 | * memory_region_init_iommu: Initialize a memory region of a custom type |
811 | * that translates addresses | |
30951157 AK |
812 | * |
813 | * An IOMMU region translates addresses and forwards accesses to a target | |
814 | * memory region. | |
815 | * | |
2ce931d0 PM |
816 | * The IOMMU implementation must define a subclass of TYPE_IOMMU_MEMORY_REGION. |
817 | * @_iommu_mr should be a pointer to enough memory for an instance of | |
818 | * that subclass, @instance_size is the size of that subclass, and | |
819 | * @mrtypename is its name. This function will initialize @_iommu_mr as an | |
820 | * instance of the subclass, and its methods will then be called to handle | |
821 | * accesses to the memory region. See the documentation of | |
822 | * #IOMMUMemoryRegionClass for further details. | |
823 | * | |
1221a474 AK |
824 | * @_iommu_mr: the #IOMMUMemoryRegion to be initialized |
825 | * @instance_size: the IOMMUMemoryRegion subclass instance size | |
57914ecb | 826 | * @mrtypename: the type name of the #IOMMUMemoryRegion |
2c9b15ca | 827 | * @owner: the object that tracks the region's reference count |
30951157 AK |
828 | * @name: used for debugging; not visible to the user or ABI |
829 | * @size: size of the region. | |
830 | */ | |
1221a474 AK |
831 | void memory_region_init_iommu(void *_iommu_mr, |
832 | size_t instance_size, | |
833 | const char *mrtypename, | |
834 | Object *owner, | |
30951157 AK |
835 | const char *name, |
836 | uint64_t size); | |
837 | ||
b08199c6 PM |
838 | /** |
839 | * memory_region_init_ram - Initialize RAM memory region. Accesses into the | |
840 | * region will modify memory directly. | |
841 | * | |
842 | * @mr: the #MemoryRegion to be initialized | |
843 | * @owner: the object that tracks the region's reference count (must be | |
844 | * TYPE_DEVICE or a subclass of TYPE_DEVICE, or NULL) | |
845 | * @name: name of the memory region | |
846 | * @size: size of the region in bytes | |
847 | * @errp: pointer to Error*, to store an error if it happens. | |
848 | * | |
849 | * This function allocates RAM for a board model or device, and | |
850 | * arranges for it to be migrated (by calling vmstate_register_ram() | |
851 | * if @owner is a DeviceState, or vmstate_register_ram_global() if | |
852 | * @owner is NULL). | |
853 | * | |
854 | * TODO: Currently we restrict @owner to being either NULL (for | |
855 | * global RAM regions with no owner) or devices, so that we can | |
856 | * give the RAM block a unique name for migration purposes. | |
857 | * We should lift this restriction and allow arbitrary Objects. | |
858 | * If you pass a non-NULL non-device @owner then we will assert. | |
859 | */ | |
860 | void memory_region_init_ram(MemoryRegion *mr, | |
861 | struct Object *owner, | |
862 | const char *name, | |
863 | uint64_t size, | |
864 | Error **errp); | |
865 | ||
866 | /** | |
867 | * memory_region_init_rom: Initialize a ROM memory region. | |
868 | * | |
869 | * This has the same effect as calling memory_region_init_ram() | |
870 | * and then marking the resulting region read-only with | |
871 | * memory_region_set_readonly(). This includes arranging for the | |
872 | * contents to be migrated. | |
873 | * | |
874 | * TODO: Currently we restrict @owner to being either NULL (for | |
875 | * global RAM regions with no owner) or devices, so that we can | |
876 | * give the RAM block a unique name for migration purposes. | |
877 | * We should lift this restriction and allow arbitrary Objects. | |
878 | * If you pass a non-NULL non-device @owner then we will assert. | |
879 | * | |
880 | * @mr: the #MemoryRegion to be initialized. | |
881 | * @owner: the object that tracks the region's reference count | |
882 | * @name: Region name, becomes part of RAMBlock name used in migration stream | |
883 | * must be unique within any device | |
884 | * @size: size of the region. | |
885 | * @errp: pointer to Error*, to store an error if it happens. | |
886 | */ | |
887 | void memory_region_init_rom(MemoryRegion *mr, | |
888 | struct Object *owner, | |
889 | const char *name, | |
890 | uint64_t size, | |
891 | Error **errp); | |
892 | ||
893 | /** | |
894 | * memory_region_init_rom_device: Initialize a ROM memory region. | |
895 | * Writes are handled via callbacks. | |
896 | * | |
897 | * This function initializes a memory region backed by RAM for reads | |
898 | * and callbacks for writes, and arranges for the RAM backing to | |
899 | * be migrated (by calling vmstate_register_ram() | |
900 | * if @owner is a DeviceState, or vmstate_register_ram_global() if | |
901 | * @owner is NULL). | |
902 | * | |
903 | * TODO: Currently we restrict @owner to being either NULL (for | |
904 | * global RAM regions with no owner) or devices, so that we can | |
905 | * give the RAM block a unique name for migration purposes. | |
906 | * We should lift this restriction and allow arbitrary Objects. | |
907 | * If you pass a non-NULL non-device @owner then we will assert. | |
908 | * | |
909 | * @mr: the #MemoryRegion to be initialized. | |
910 | * @owner: the object that tracks the region's reference count | |
911 | * @ops: callbacks for write access handling (must not be NULL). | |
912 | * @name: Region name, becomes part of RAMBlock name used in migration stream | |
913 | * must be unique within any device | |
914 | * @size: size of the region. | |
915 | * @errp: pointer to Error*, to store an error if it happens. | |
916 | */ | |
917 | void memory_region_init_rom_device(MemoryRegion *mr, | |
918 | struct Object *owner, | |
919 | const MemoryRegionOps *ops, | |
920 | void *opaque, | |
921 | const char *name, | |
922 | uint64_t size, | |
923 | Error **errp); | |
924 | ||
925 | ||
803c0816 PB |
926 | /** |
927 | * memory_region_owner: get a memory region's owner. | |
928 | * | |
929 | * @mr: the memory region being queried. | |
930 | */ | |
931 | struct Object *memory_region_owner(MemoryRegion *mr); | |
932 | ||
093bc2cd AK |
933 | /** |
934 | * memory_region_size: get a memory region's size. | |
935 | * | |
936 | * @mr: the memory region being queried. | |
937 | */ | |
938 | uint64_t memory_region_size(MemoryRegion *mr); | |
939 | ||
8ea9252a AK |
940 | /** |
941 | * memory_region_is_ram: check whether a memory region is random access | |
942 | * | |
943 | * Returns %true is a memory region is random access. | |
944 | * | |
945 | * @mr: the memory region being queried | |
946 | */ | |
1619d1fe PB |
947 | static inline bool memory_region_is_ram(MemoryRegion *mr) |
948 | { | |
949 | return mr->ram; | |
950 | } | |
8ea9252a | 951 | |
e4dc3f59 | 952 | /** |
21e00fa5 | 953 | * memory_region_is_ram_device: check whether a memory region is a ram device |
e4dc3f59 | 954 | * |
21e00fa5 | 955 | * Returns %true is a memory region is a device backed ram region |
e4dc3f59 ND |
956 | * |
957 | * @mr: the memory region being queried | |
958 | */ | |
21e00fa5 | 959 | bool memory_region_is_ram_device(MemoryRegion *mr); |
e4dc3f59 | 960 | |
fd062573 | 961 | /** |
5f9a5ea1 | 962 | * memory_region_is_romd: check whether a memory region is in ROMD mode |
fd062573 | 963 | * |
5f9a5ea1 | 964 | * Returns %true if a memory region is a ROM device and currently set to allow |
fd062573 BS |
965 | * direct reads. |
966 | * | |
967 | * @mr: the memory region being queried | |
968 | */ | |
969 | static inline bool memory_region_is_romd(MemoryRegion *mr) | |
970 | { | |
5f9a5ea1 | 971 | return mr->rom_device && mr->romd_mode; |
fd062573 BS |
972 | } |
973 | ||
30951157 | 974 | /** |
3df9d748 | 975 | * memory_region_get_iommu: check whether a memory region is an iommu |
30951157 | 976 | * |
3df9d748 AK |
977 | * Returns pointer to IOMMUMemoryRegion if a memory region is an iommu, |
978 | * otherwise NULL. | |
30951157 AK |
979 | * |
980 | * @mr: the memory region being queried | |
981 | */ | |
3df9d748 | 982 | static inline IOMMUMemoryRegion *memory_region_get_iommu(MemoryRegion *mr) |
1619d1fe | 983 | { |
12d37882 | 984 | if (mr->alias) { |
3df9d748 AK |
985 | return memory_region_get_iommu(mr->alias); |
986 | } | |
987 | if (mr->is_iommu) { | |
988 | return (IOMMUMemoryRegion *) mr; | |
12d37882 | 989 | } |
3df9d748 | 990 | return NULL; |
1619d1fe PB |
991 | } |
992 | ||
1221a474 AK |
993 | /** |
994 | * memory_region_get_iommu_class_nocheck: returns iommu memory region class | |
995 | * if an iommu or NULL if not | |
996 | * | |
57914ecb JZ |
997 | * Returns pointer to IOMMUMemoryRegionClass if a memory region is an iommu, |
998 | * otherwise NULL. This is fast path avoiding QOM checking, use with caution. | |
1221a474 AK |
999 | * |
1000 | * @mr: the memory region being queried | |
1001 | */ | |
1002 | static inline IOMMUMemoryRegionClass *memory_region_get_iommu_class_nocheck( | |
1003 | IOMMUMemoryRegion *iommu_mr) | |
1004 | { | |
1005 | return (IOMMUMemoryRegionClass *) (((Object *)iommu_mr)->class); | |
1006 | } | |
1007 | ||
3df9d748 | 1008 | #define memory_region_is_iommu(mr) (memory_region_get_iommu(mr) != NULL) |
30951157 | 1009 | |
f682e9c2 AK |
1010 | /** |
1011 | * memory_region_iommu_get_min_page_size: get minimum supported page size | |
1012 | * for an iommu | |
1013 | * | |
1014 | * Returns minimum supported page size for an iommu. | |
1015 | * | |
3df9d748 | 1016 | * @iommu_mr: the memory region being queried |
f682e9c2 | 1017 | */ |
3df9d748 | 1018 | uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr); |
f682e9c2 | 1019 | |
06866575 DG |
1020 | /** |
1021 | * memory_region_notify_iommu: notify a change in an IOMMU translation entry. | |
1022 | * | |
cdb30812 PX |
1023 | * The notification type will be decided by entry.perm bits: |
1024 | * | |
1025 | * - For UNMAP (cache invalidation) notifies: set entry.perm to IOMMU_NONE. | |
1026 | * - For MAP (newly added entry) notifies: set entry.perm to the | |
1027 | * permission of the page (which is definitely !IOMMU_NONE). | |
1028 | * | |
1029 | * Note: for any IOMMU implementation, an in-place mapping change | |
1030 | * should be notified with an UNMAP followed by a MAP. | |
1031 | * | |
3df9d748 | 1032 | * @iommu_mr: the memory region that was changed |
cb1efcf4 | 1033 | * @iommu_idx: the IOMMU index for the translation table which has changed |
06866575 DG |
1034 | * @entry: the new entry in the IOMMU translation table. The entry |
1035 | * replaces all old entries for the same virtual I/O address range. | |
1036 | * Deleted entries have .@perm == 0. | |
1037 | */ | |
3df9d748 | 1038 | void memory_region_notify_iommu(IOMMUMemoryRegion *iommu_mr, |
cb1efcf4 | 1039 | int iommu_idx, |
06866575 DG |
1040 | IOMMUTLBEntry entry); |
1041 | ||
bd2bfa4c PX |
1042 | /** |
1043 | * memory_region_notify_one: notify a change in an IOMMU translation | |
1044 | * entry to a single notifier | |
1045 | * | |
1046 | * This works just like memory_region_notify_iommu(), but it only | |
1047 | * notifies a specific notifier, not all of them. | |
1048 | * | |
1049 | * @notifier: the notifier to be notified | |
1050 | * @entry: the new entry in the IOMMU translation table. The entry | |
1051 | * replaces all old entries for the same virtual I/O address range. | |
1052 | * Deleted entries have .@perm == 0. | |
1053 | */ | |
1054 | void memory_region_notify_one(IOMMUNotifier *notifier, | |
1055 | IOMMUTLBEntry *entry); | |
1056 | ||
06866575 DG |
1057 | /** |
1058 | * memory_region_register_iommu_notifier: register a notifier for changes to | |
1059 | * IOMMU translation entries. | |
1060 | * | |
1061 | * @mr: the memory region to observe | |
cdb30812 PX |
1062 | * @n: the IOMMUNotifier to be added; the notify callback receives a |
1063 | * pointer to an #IOMMUTLBEntry as the opaque value; the pointer | |
1064 | * ceases to be valid on exit from the notifier. | |
06866575 | 1065 | */ |
cdb30812 PX |
1066 | void memory_region_register_iommu_notifier(MemoryRegion *mr, |
1067 | IOMMUNotifier *n); | |
06866575 | 1068 | |
a788f227 DG |
1069 | /** |
1070 | * memory_region_iommu_replay: replay existing IOMMU translations to | |
f682e9c2 AK |
1071 | * a notifier with the minimum page granularity returned by |
1072 | * mr->iommu_ops->get_page_size(). | |
a788f227 | 1073 | * |
2ce931d0 PM |
1074 | * Note: this is not related to record-and-replay functionality. |
1075 | * | |
3df9d748 | 1076 | * @iommu_mr: the memory region to observe |
a788f227 | 1077 | * @n: the notifier to which to replay iommu mappings |
a788f227 | 1078 | */ |
3df9d748 | 1079 | void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n); |
a788f227 | 1080 | |
de472e4a PX |
1081 | /** |
1082 | * memory_region_iommu_replay_all: replay existing IOMMU translations | |
1083 | * to all the notifiers registered. | |
1084 | * | |
2ce931d0 PM |
1085 | * Note: this is not related to record-and-replay functionality. |
1086 | * | |
3df9d748 | 1087 | * @iommu_mr: the memory region to observe |
de472e4a | 1088 | */ |
3df9d748 | 1089 | void memory_region_iommu_replay_all(IOMMUMemoryRegion *iommu_mr); |
de472e4a | 1090 | |
06866575 DG |
1091 | /** |
1092 | * memory_region_unregister_iommu_notifier: unregister a notifier for | |
1093 | * changes to IOMMU translation entries. | |
1094 | * | |
d22d8956 AK |
1095 | * @mr: the memory region which was observed and for which notity_stopped() |
1096 | * needs to be called | |
06866575 DG |
1097 | * @n: the notifier to be removed. |
1098 | */ | |
cdb30812 PX |
1099 | void memory_region_unregister_iommu_notifier(MemoryRegion *mr, |
1100 | IOMMUNotifier *n); | |
06866575 | 1101 | |
f1334de6 AK |
1102 | /** |
1103 | * memory_region_iommu_get_attr: return an IOMMU attr if get_attr() is | |
1104 | * defined on the IOMMU. | |
1105 | * | |
2ce931d0 PM |
1106 | * Returns 0 on success, or a negative errno otherwise. In particular, |
1107 | * -EINVAL indicates that the IOMMU does not support the requested | |
1108 | * attribute. | |
f1334de6 AK |
1109 | * |
1110 | * @iommu_mr: the memory region | |
1111 | * @attr: the requested attribute | |
1112 | * @data: a pointer to the requested attribute data | |
1113 | */ | |
1114 | int memory_region_iommu_get_attr(IOMMUMemoryRegion *iommu_mr, | |
1115 | enum IOMMUMemoryRegionAttr attr, | |
1116 | void *data); | |
1117 | ||
21f40209 PM |
1118 | /** |
1119 | * memory_region_iommu_attrs_to_index: return the IOMMU index to | |
1120 | * use for translations with the given memory transaction attributes. | |
1121 | * | |
1122 | * @iommu_mr: the memory region | |
1123 | * @attrs: the memory transaction attributes | |
1124 | */ | |
1125 | int memory_region_iommu_attrs_to_index(IOMMUMemoryRegion *iommu_mr, | |
1126 | MemTxAttrs attrs); | |
1127 | ||
1128 | /** | |
1129 | * memory_region_iommu_num_indexes: return the total number of IOMMU | |
1130 | * indexes that this IOMMU supports. | |
1131 | * | |
1132 | * @iommu_mr: the memory region | |
1133 | */ | |
1134 | int memory_region_iommu_num_indexes(IOMMUMemoryRegion *iommu_mr); | |
1135 | ||
8991c79b AK |
1136 | /** |
1137 | * memory_region_name: get a memory region's name | |
1138 | * | |
1139 | * Returns the string that was used to initialize the memory region. | |
1140 | * | |
1141 | * @mr: the memory region being queried | |
1142 | */ | |
5d546d4b | 1143 | const char *memory_region_name(const MemoryRegion *mr); |
8991c79b | 1144 | |
55043ba3 AK |
1145 | /** |
1146 | * memory_region_is_logging: return whether a memory region is logging writes | |
1147 | * | |
2d1a35be | 1148 | * Returns %true if the memory region is logging writes for the given client |
55043ba3 AK |
1149 | * |
1150 | * @mr: the memory region being queried | |
2d1a35be | 1151 | * @client: the client being queried |
55043ba3 | 1152 | */ |
2d1a35be PB |
1153 | bool memory_region_is_logging(MemoryRegion *mr, uint8_t client); |
1154 | ||
1155 | /** | |
1156 | * memory_region_get_dirty_log_mask: return the clients for which a | |
1157 | * memory region is logging writes. | |
1158 | * | |
677e7805 PB |
1159 | * Returns a bitmap of clients, in which the DIRTY_MEMORY_* constants |
1160 | * are the bit indices. | |
2d1a35be PB |
1161 | * |
1162 | * @mr: the memory region being queried | |
1163 | */ | |
1164 | uint8_t memory_region_get_dirty_log_mask(MemoryRegion *mr); | |
55043ba3 | 1165 | |
ce7923da AK |
1166 | /** |
1167 | * memory_region_is_rom: check whether a memory region is ROM | |
1168 | * | |
1169 | * Returns %true is a memory region is read-only memory. | |
1170 | * | |
1171 | * @mr: the memory region being queried | |
1172 | */ | |
1619d1fe PB |
1173 | static inline bool memory_region_is_rom(MemoryRegion *mr) |
1174 | { | |
1175 | return mr->ram && mr->readonly; | |
1176 | } | |
1177 | ||
ce7923da | 1178 | |
a35ba7be PB |
1179 | /** |
1180 | * memory_region_get_fd: Get a file descriptor backing a RAM memory region. | |
1181 | * | |
1182 | * Returns a file descriptor backing a file-based RAM memory region, | |
1183 | * or -1 if the region is not a file-based RAM memory region. | |
1184 | * | |
1185 | * @mr: the RAM or alias memory region being queried. | |
1186 | */ | |
1187 | int memory_region_get_fd(MemoryRegion *mr); | |
1188 | ||
07bdaa41 PB |
1189 | /** |
1190 | * memory_region_from_host: Convert a pointer into a RAM memory region | |
1191 | * and an offset within it. | |
1192 | * | |
1193 | * Given a host pointer inside a RAM memory region (created with | |
1194 | * memory_region_init_ram() or memory_region_init_ram_ptr()), return | |
1195 | * the MemoryRegion and the offset within it. | |
1196 | * | |
1197 | * Use with care; by the time this function returns, the returned pointer is | |
1198 | * not protected by RCU anymore. If the caller is not within an RCU critical | |
1199 | * section and does not hold the iothread lock, it must have other means of | |
1200 | * protecting the pointer, such as a reference to the region that includes | |
1201 | * the incoming ram_addr_t. | |
1202 | * | |
57914ecb JZ |
1203 | * @ptr: the host pointer to be converted |
1204 | * @offset: the offset within memory region | |
07bdaa41 PB |
1205 | */ |
1206 | MemoryRegion *memory_region_from_host(void *ptr, ram_addr_t *offset); | |
1207 | ||
093bc2cd AK |
1208 | /** |
1209 | * memory_region_get_ram_ptr: Get a pointer into a RAM memory region. | |
1210 | * | |
1211 | * Returns a host pointer to a RAM memory region (created with | |
49b24afc PB |
1212 | * memory_region_init_ram() or memory_region_init_ram_ptr()). |
1213 | * | |
1214 | * Use with care; by the time this function returns, the returned pointer is | |
1215 | * not protected by RCU anymore. If the caller is not within an RCU critical | |
1216 | * section and does not hold the iothread lock, it must have other means of | |
1217 | * protecting the pointer, such as a reference to the region that includes | |
1218 | * the incoming ram_addr_t. | |
093bc2cd AK |
1219 | * |
1220 | * @mr: the memory region being queried. | |
1221 | */ | |
1222 | void *memory_region_get_ram_ptr(MemoryRegion *mr); | |
1223 | ||
37d7c084 PB |
1224 | /* memory_region_ram_resize: Resize a RAM region. |
1225 | * | |
1226 | * Only legal before guest might have detected the memory size: e.g. on | |
1227 | * incoming migration, or right after reset. | |
1228 | * | |
1229 | * @mr: a memory region created with @memory_region_init_resizeable_ram. | |
1230 | * @newsize: the new size the region | |
1231 | * @errp: pointer to Error*, to store an error if it happens. | |
1232 | */ | |
1233 | void memory_region_ram_resize(MemoryRegion *mr, ram_addr_t newsize, | |
1234 | Error **errp); | |
1235 | ||
093bc2cd AK |
1236 | /** |
1237 | * memory_region_set_log: Turn dirty logging on or off for a region. | |
1238 | * | |
1239 | * Turns dirty logging on or off for a specified client (display, migration). | |
1240 | * Only meaningful for RAM regions. | |
1241 | * | |
1242 | * @mr: the memory region being updated. | |
1243 | * @log: whether dirty logging is to be enabled or disabled. | |
dbddac6d | 1244 | * @client: the user of the logging information; %DIRTY_MEMORY_VGA only. |
093bc2cd AK |
1245 | */ |
1246 | void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client); | |
1247 | ||
1248 | /** | |
cd7a45c9 BS |
1249 | * memory_region_get_dirty: Check whether a range of bytes is dirty |
1250 | * for a specified client. | |
093bc2cd | 1251 | * |
cd7a45c9 | 1252 | * Checks whether a range of bytes has been written to since the last |
093bc2cd AK |
1253 | * call to memory_region_reset_dirty() with the same @client. Dirty logging |
1254 | * must be enabled. | |
1255 | * | |
1256 | * @mr: the memory region being queried. | |
1257 | * @addr: the address (relative to the start of the region) being queried. | |
cd7a45c9 | 1258 | * @size: the size of the range being queried. |
093bc2cd AK |
1259 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or |
1260 | * %DIRTY_MEMORY_VGA. | |
1261 | */ | |
a8170e5e AK |
1262 | bool memory_region_get_dirty(MemoryRegion *mr, hwaddr addr, |
1263 | hwaddr size, unsigned client); | |
093bc2cd AK |
1264 | |
1265 | /** | |
fd4aa979 | 1266 | * memory_region_set_dirty: Mark a range of bytes as dirty in a memory region. |
093bc2cd | 1267 | * |
fd4aa979 BS |
1268 | * Marks a range of bytes as dirty, after it has been dirtied outside |
1269 | * guest code. | |
093bc2cd | 1270 | * |
fd4aa979 | 1271 | * @mr: the memory region being dirtied. |
093bc2cd | 1272 | * @addr: the address (relative to the start of the region) being dirtied. |
fd4aa979 | 1273 | * @size: size of the range being dirtied. |
093bc2cd | 1274 | */ |
a8170e5e AK |
1275 | void memory_region_set_dirty(MemoryRegion *mr, hwaddr addr, |
1276 | hwaddr size); | |
093bc2cd | 1277 | |
8deaf12c GH |
1278 | /** |
1279 | * memory_region_snapshot_and_clear_dirty: Get a snapshot of the dirty | |
1280 | * bitmap and clear it. | |
1281 | * | |
1282 | * Creates a snapshot of the dirty bitmap, clears the dirty bitmap and | |
1283 | * returns the snapshot. The snapshot can then be used to query dirty | |
77302fb5 PB |
1284 | * status, using memory_region_snapshot_get_dirty. Snapshotting allows |
1285 | * querying the same page multiple times, which is especially useful for | |
1286 | * display updates where the scanlines often are not page aligned. | |
8deaf12c GH |
1287 | * |
1288 | * The dirty bitmap region which gets copyed into the snapshot (and | |
1289 | * cleared afterwards) can be larger than requested. The boundaries | |
1290 | * are rounded up/down so complete bitmap longs (covering 64 pages on | |
1291 | * 64bit hosts) can be copied over into the bitmap snapshot. Which | |
1292 | * isn't a problem for display updates as the extra pages are outside | |
1293 | * the visible area, and in case the visible area changes a full | |
1294 | * display redraw is due anyway. Should other use cases for this | |
1295 | * function emerge we might have to revisit this implementation | |
1296 | * detail. | |
1297 | * | |
1298 | * Use g_free to release DirtyBitmapSnapshot. | |
1299 | * | |
1300 | * @mr: the memory region being queried. | |
1301 | * @addr: the address (relative to the start of the region) being queried. | |
1302 | * @size: the size of the range being queried. | |
1303 | * @client: the user of the logging information; typically %DIRTY_MEMORY_VGA. | |
1304 | */ | |
1305 | DirtyBitmapSnapshot *memory_region_snapshot_and_clear_dirty(MemoryRegion *mr, | |
1306 | hwaddr addr, | |
1307 | hwaddr size, | |
1308 | unsigned client); | |
1309 | ||
1310 | /** | |
1311 | * memory_region_snapshot_get_dirty: Check whether a range of bytes is dirty | |
1312 | * in the specified dirty bitmap snapshot. | |
1313 | * | |
1314 | * @mr: the memory region being queried. | |
1315 | * @snap: the dirty bitmap snapshot | |
1316 | * @addr: the address (relative to the start of the region) being queried. | |
1317 | * @size: the size of the range being queried. | |
1318 | */ | |
1319 | bool memory_region_snapshot_get_dirty(MemoryRegion *mr, | |
1320 | DirtyBitmapSnapshot *snap, | |
1321 | hwaddr addr, hwaddr size); | |
1322 | ||
093bc2cd AK |
1323 | /** |
1324 | * memory_region_reset_dirty: Mark a range of pages as clean, for a specified | |
1325 | * client. | |
1326 | * | |
1327 | * Marks a range of pages as no longer dirty. | |
1328 | * | |
1329 | * @mr: the region being updated. | |
1330 | * @addr: the start of the subrange being cleaned. | |
1331 | * @size: the size of the subrange being cleaned. | |
1332 | * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or | |
1333 | * %DIRTY_MEMORY_VGA. | |
1334 | */ | |
a8170e5e AK |
1335 | void memory_region_reset_dirty(MemoryRegion *mr, hwaddr addr, |
1336 | hwaddr size, unsigned client); | |
093bc2cd AK |
1337 | |
1338 | /** | |
1339 | * memory_region_set_readonly: Turn a memory region read-only (or read-write) | |
1340 | * | |
1341 | * Allows a memory region to be marked as read-only (turning it into a ROM). | |
1342 | * only useful on RAM regions. | |
1343 | * | |
1344 | * @mr: the region being updated. | |
1345 | * @readonly: whether rhe region is to be ROM or RAM. | |
1346 | */ | |
1347 | void memory_region_set_readonly(MemoryRegion *mr, bool readonly); | |
1348 | ||
d0a9b5bc | 1349 | /** |
5f9a5ea1 | 1350 | * memory_region_rom_device_set_romd: enable/disable ROMD mode |
d0a9b5bc AK |
1351 | * |
1352 | * Allows a ROM device (initialized with memory_region_init_rom_device() to | |
5f9a5ea1 JK |
1353 | * set to ROMD mode (default) or MMIO mode. When it is in ROMD mode, the |
1354 | * device is mapped to guest memory and satisfies read access directly. | |
1355 | * When in MMIO mode, reads are forwarded to the #MemoryRegion.read function. | |
1356 | * Writes are always handled by the #MemoryRegion.write function. | |
d0a9b5bc AK |
1357 | * |
1358 | * @mr: the memory region to be updated | |
5f9a5ea1 | 1359 | * @romd_mode: %true to put the region into ROMD mode |
d0a9b5bc | 1360 | */ |
5f9a5ea1 | 1361 | void memory_region_rom_device_set_romd(MemoryRegion *mr, bool romd_mode); |
d0a9b5bc | 1362 | |
093bc2cd AK |
1363 | /** |
1364 | * memory_region_set_coalescing: Enable memory coalescing for the region. | |
1365 | * | |
1366 | * Enabled writes to a region to be queued for later processing. MMIO ->write | |
1367 | * callbacks may be delayed until a non-coalesced MMIO is issued. | |
1368 | * Only useful for IO regions. Roughly similar to write-combining hardware. | |
1369 | * | |
1370 | * @mr: the memory region to be write coalesced | |
1371 | */ | |
1372 | void memory_region_set_coalescing(MemoryRegion *mr); | |
1373 | ||
1374 | /** | |
1375 | * memory_region_add_coalescing: Enable memory coalescing for a sub-range of | |
1376 | * a region. | |
1377 | * | |
1378 | * Like memory_region_set_coalescing(), but works on a sub-range of a region. | |
1379 | * Multiple calls can be issued coalesced disjoint ranges. | |
1380 | * | |
1381 | * @mr: the memory region to be updated. | |
1382 | * @offset: the start of the range within the region to be coalesced. | |
1383 | * @size: the size of the subrange to be coalesced. | |
1384 | */ | |
1385 | void memory_region_add_coalescing(MemoryRegion *mr, | |
a8170e5e | 1386 | hwaddr offset, |
093bc2cd AK |
1387 | uint64_t size); |
1388 | ||
1389 | /** | |
1390 | * memory_region_clear_coalescing: Disable MMIO coalescing for the region. | |
1391 | * | |
1392 | * Disables any coalescing caused by memory_region_set_coalescing() or | |
1393 | * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory | |
1394 | * hardware. | |
1395 | * | |
1396 | * @mr: the memory region to be updated. | |
1397 | */ | |
1398 | void memory_region_clear_coalescing(MemoryRegion *mr); | |
1399 | ||
d410515e JK |
1400 | /** |
1401 | * memory_region_set_flush_coalesced: Enforce memory coalescing flush before | |
1402 | * accesses. | |
1403 | * | |
1404 | * Ensure that pending coalesced MMIO request are flushed before the memory | |
1405 | * region is accessed. This property is automatically enabled for all regions | |
1406 | * passed to memory_region_set_coalescing() and memory_region_add_coalescing(). | |
1407 | * | |
1408 | * @mr: the memory region to be updated. | |
1409 | */ | |
1410 | void memory_region_set_flush_coalesced(MemoryRegion *mr); | |
1411 | ||
1412 | /** | |
1413 | * memory_region_clear_flush_coalesced: Disable memory coalescing flush before | |
1414 | * accesses. | |
1415 | * | |
1416 | * Clear the automatic coalesced MMIO flushing enabled via | |
1417 | * memory_region_set_flush_coalesced. Note that this service has no effect on | |
1418 | * memory regions that have MMIO coalescing enabled for themselves. For them, | |
1419 | * automatic flushing will stop once coalescing is disabled. | |
1420 | * | |
1421 | * @mr: the memory region to be updated. | |
1422 | */ | |
1423 | void memory_region_clear_flush_coalesced(MemoryRegion *mr); | |
1424 | ||
196ea131 JK |
1425 | /** |
1426 | * memory_region_clear_global_locking: Declares that access processing does | |
1427 | * not depend on the QEMU global lock. | |
1428 | * | |
1429 | * By clearing this property, accesses to the memory region will be processed | |
1430 | * outside of QEMU's global lock (unless the lock is held on when issuing the | |
1431 | * access request). In this case, the device model implementing the access | |
1432 | * handlers is responsible for synchronization of concurrency. | |
1433 | * | |
1434 | * @mr: the memory region to be updated. | |
1435 | */ | |
1436 | void memory_region_clear_global_locking(MemoryRegion *mr); | |
1437 | ||
3e9d69e7 AK |
1438 | /** |
1439 | * memory_region_add_eventfd: Request an eventfd to be triggered when a word | |
1440 | * is written to a location. | |
1441 | * | |
1442 | * Marks a word in an IO region (initialized with memory_region_init_io()) | |
1443 | * as a trigger for an eventfd event. The I/O callback will not be called. | |
69ddaf66 | 1444 | * The caller must be prepared to handle failure (that is, take the required |
3e9d69e7 AK |
1445 | * action if the callback _is_ called). |
1446 | * | |
1447 | * @mr: the memory region being updated. | |
1448 | * @addr: the address within @mr that is to be monitored | |
1449 | * @size: the size of the access to trigger the eventfd | |
1450 | * @match_data: whether to match against @data, instead of just @addr | |
1451 | * @data: the data to match against the guest write | |
57914ecb | 1452 | * @e: event notifier to be triggered when @addr, @size, and @data all match. |
3e9d69e7 AK |
1453 | **/ |
1454 | void memory_region_add_eventfd(MemoryRegion *mr, | |
a8170e5e | 1455 | hwaddr addr, |
3e9d69e7 AK |
1456 | unsigned size, |
1457 | bool match_data, | |
1458 | uint64_t data, | |
753d5e14 | 1459 | EventNotifier *e); |
3e9d69e7 AK |
1460 | |
1461 | /** | |
69ddaf66 | 1462 | * memory_region_del_eventfd: Cancel an eventfd. |
3e9d69e7 | 1463 | * |
69ddaf66 ASRJ |
1464 | * Cancels an eventfd trigger requested by a previous |
1465 | * memory_region_add_eventfd() call. | |
3e9d69e7 AK |
1466 | * |
1467 | * @mr: the memory region being updated. | |
1468 | * @addr: the address within @mr that is to be monitored | |
1469 | * @size: the size of the access to trigger the eventfd | |
1470 | * @match_data: whether to match against @data, instead of just @addr | |
1471 | * @data: the data to match against the guest write | |
57914ecb | 1472 | * @e: event notifier to be triggered when @addr, @size, and @data all match. |
3e9d69e7 AK |
1473 | */ |
1474 | void memory_region_del_eventfd(MemoryRegion *mr, | |
a8170e5e | 1475 | hwaddr addr, |
3e9d69e7 AK |
1476 | unsigned size, |
1477 | bool match_data, | |
1478 | uint64_t data, | |
753d5e14 PB |
1479 | EventNotifier *e); |
1480 | ||
093bc2cd | 1481 | /** |
69ddaf66 | 1482 | * memory_region_add_subregion: Add a subregion to a container. |
093bc2cd | 1483 | * |
69ddaf66 | 1484 | * Adds a subregion at @offset. The subregion may not overlap with other |
093bc2cd AK |
1485 | * subregions (except for those explicitly marked as overlapping). A region |
1486 | * may only be added once as a subregion (unless removed with | |
1487 | * memory_region_del_subregion()); use memory_region_init_alias() if you | |
1488 | * want a region to be a subregion in multiple locations. | |
1489 | * | |
1490 | * @mr: the region to contain the new subregion; must be a container | |
1491 | * initialized with memory_region_init(). | |
1492 | * @offset: the offset relative to @mr where @subregion is added. | |
1493 | * @subregion: the subregion to be added. | |
1494 | */ | |
1495 | void memory_region_add_subregion(MemoryRegion *mr, | |
a8170e5e | 1496 | hwaddr offset, |
093bc2cd AK |
1497 | MemoryRegion *subregion); |
1498 | /** | |
1a7e8cae BZ |
1499 | * memory_region_add_subregion_overlap: Add a subregion to a container |
1500 | * with overlap. | |
093bc2cd | 1501 | * |
69ddaf66 | 1502 | * Adds a subregion at @offset. The subregion may overlap with other |
093bc2cd AK |
1503 | * subregions. Conflicts are resolved by having a higher @priority hide a |
1504 | * lower @priority. Subregions without priority are taken as @priority 0. | |
1505 | * A region may only be added once as a subregion (unless removed with | |
1506 | * memory_region_del_subregion()); use memory_region_init_alias() if you | |
1507 | * want a region to be a subregion in multiple locations. | |
1508 | * | |
1509 | * @mr: the region to contain the new subregion; must be a container | |
1510 | * initialized with memory_region_init(). | |
1511 | * @offset: the offset relative to @mr where @subregion is added. | |
1512 | * @subregion: the subregion to be added. | |
1513 | * @priority: used for resolving overlaps; highest priority wins. | |
1514 | */ | |
1515 | void memory_region_add_subregion_overlap(MemoryRegion *mr, | |
a8170e5e | 1516 | hwaddr offset, |
093bc2cd | 1517 | MemoryRegion *subregion, |
a1ff8ae0 | 1518 | int priority); |
e34911c4 AK |
1519 | |
1520 | /** | |
1521 | * memory_region_get_ram_addr: Get the ram address associated with a memory | |
1522 | * region | |
e34911c4 | 1523 | */ |
7ebb2745 | 1524 | ram_addr_t memory_region_get_ram_addr(MemoryRegion *mr); |
e34911c4 | 1525 | |
a2b257d6 | 1526 | uint64_t memory_region_get_alignment(const MemoryRegion *mr); |
093bc2cd AK |
1527 | /** |
1528 | * memory_region_del_subregion: Remove a subregion. | |
1529 | * | |
1530 | * Removes a subregion from its container. | |
1531 | * | |
1532 | * @mr: the container to be updated. | |
1533 | * @subregion: the region being removed; must be a current subregion of @mr. | |
1534 | */ | |
1535 | void memory_region_del_subregion(MemoryRegion *mr, | |
1536 | MemoryRegion *subregion); | |
1537 | ||
6bba19ba AK |
1538 | /* |
1539 | * memory_region_set_enabled: dynamically enable or disable a region | |
1540 | * | |
1541 | * Enables or disables a memory region. A disabled memory region | |
1542 | * ignores all accesses to itself and its subregions. It does not | |
1543 | * obscure sibling subregions with lower priority - it simply behaves as | |
1544 | * if it was removed from the hierarchy. | |
1545 | * | |
1546 | * Regions default to being enabled. | |
1547 | * | |
1548 | * @mr: the region to be updated | |
1549 | * @enabled: whether to enable or disable the region | |
1550 | */ | |
1551 | void memory_region_set_enabled(MemoryRegion *mr, bool enabled); | |
1552 | ||
2282e1af AK |
1553 | /* |
1554 | * memory_region_set_address: dynamically update the address of a region | |
1555 | * | |
feca4ac1 | 1556 | * Dynamically updates the address of a region, relative to its container. |
2282e1af AK |
1557 | * May be used on regions are currently part of a memory hierarchy. |
1558 | * | |
1559 | * @mr: the region to be updated | |
feca4ac1 | 1560 | * @addr: new address, relative to container region |
2282e1af | 1561 | */ |
a8170e5e | 1562 | void memory_region_set_address(MemoryRegion *mr, hwaddr addr); |
2282e1af | 1563 | |
e7af4c67 MT |
1564 | /* |
1565 | * memory_region_set_size: dynamically update the size of a region. | |
1566 | * | |
1567 | * Dynamically updates the size of a region. | |
1568 | * | |
1569 | * @mr: the region to be updated | |
1570 | * @size: used size of the region. | |
1571 | */ | |
1572 | void memory_region_set_size(MemoryRegion *mr, uint64_t size); | |
1573 | ||
4703359e AK |
1574 | /* |
1575 | * memory_region_set_alias_offset: dynamically update a memory alias's offset | |
1576 | * | |
1577 | * Dynamically updates the offset into the target region that an alias points | |
1578 | * to, as if the fourth argument to memory_region_init_alias() has changed. | |
1579 | * | |
1580 | * @mr: the #MemoryRegion to be updated; should be an alias. | |
1581 | * @offset: the new offset into the target memory region | |
1582 | */ | |
1583 | void memory_region_set_alias_offset(MemoryRegion *mr, | |
a8170e5e | 1584 | hwaddr offset); |
4703359e | 1585 | |
3ce10901 | 1586 | /** |
feca4ac1 PB |
1587 | * memory_region_present: checks if an address relative to a @container |
1588 | * translates into #MemoryRegion within @container | |
3ce10901 | 1589 | * |
feca4ac1 | 1590 | * Answer whether a #MemoryRegion within @container covers the address |
3ce10901 PB |
1591 | * @addr. |
1592 | * | |
feca4ac1 PB |
1593 | * @container: a #MemoryRegion within which @addr is a relative address |
1594 | * @addr: the area within @container to be searched | |
3ce10901 | 1595 | */ |
feca4ac1 | 1596 | bool memory_region_present(MemoryRegion *container, hwaddr addr); |
3ce10901 | 1597 | |
eed2bacf IM |
1598 | /** |
1599 | * memory_region_is_mapped: returns true if #MemoryRegion is mapped | |
1600 | * into any address space. | |
1601 | * | |
1602 | * @mr: a #MemoryRegion which should be checked if it's mapped | |
1603 | */ | |
1604 | bool memory_region_is_mapped(MemoryRegion *mr); | |
1605 | ||
e2177955 | 1606 | /** |
73034e9e PB |
1607 | * memory_region_find: translate an address/size relative to a |
1608 | * MemoryRegion into a #MemoryRegionSection. | |
e2177955 | 1609 | * |
73034e9e PB |
1610 | * Locates the first #MemoryRegion within @mr that overlaps the range |
1611 | * given by @addr and @size. | |
e2177955 AK |
1612 | * |
1613 | * Returns a #MemoryRegionSection that describes a contiguous overlap. | |
1614 | * It will have the following characteristics: | |
e2177955 AK |
1615 | * .@size = 0 iff no overlap was found |
1616 | * .@mr is non-%NULL iff an overlap was found | |
1617 | * | |
73034e9e PB |
1618 | * Remember that in the return value the @offset_within_region is |
1619 | * relative to the returned region (in the .@mr field), not to the | |
1620 | * @mr argument. | |
1621 | * | |
1622 | * Similarly, the .@offset_within_address_space is relative to the | |
1623 | * address space that contains both regions, the passed and the | |
1624 | * returned one. However, in the special case where the @mr argument | |
feca4ac1 | 1625 | * has no container (and thus is the root of the address space), the |
73034e9e PB |
1626 | * following will hold: |
1627 | * .@offset_within_address_space >= @addr | |
1628 | * .@offset_within_address_space + .@size <= @addr + @size | |
1629 | * | |
1630 | * @mr: a MemoryRegion within which @addr is a relative address | |
1631 | * @addr: start of the area within @as to be searched | |
e2177955 AK |
1632 | * @size: size of the area to be searched |
1633 | */ | |
73034e9e | 1634 | MemoryRegionSection memory_region_find(MemoryRegion *mr, |
a8170e5e | 1635 | hwaddr addr, uint64_t size); |
e2177955 | 1636 | |
86e775c6 | 1637 | /** |
9c1f8f44 | 1638 | * memory_global_dirty_log_sync: synchronize the dirty log for all memory |
86e775c6 | 1639 | * |
9c1f8f44 | 1640 | * Synchronizes the dirty page log for all address spaces. |
86e775c6 | 1641 | */ |
9c1f8f44 | 1642 | void memory_global_dirty_log_sync(void); |
86e775c6 | 1643 | |
69ddaf66 ASRJ |
1644 | /** |
1645 | * memory_region_transaction_begin: Start a transaction. | |
1646 | * | |
1647 | * During a transaction, changes will be accumulated and made visible | |
dabdf394 | 1648 | * only when the transaction ends (is committed). |
4ef4db86 AK |
1649 | */ |
1650 | void memory_region_transaction_begin(void); | |
69ddaf66 ASRJ |
1651 | |
1652 | /** | |
1653 | * memory_region_transaction_commit: Commit a transaction and make changes | |
1654 | * visible to the guest. | |
4ef4db86 AK |
1655 | */ |
1656 | void memory_region_transaction_commit(void); | |
1657 | ||
7664e80c AK |
1658 | /** |
1659 | * memory_listener_register: register callbacks to be called when memory | |
1660 | * sections are mapped or unmapped into an address | |
1661 | * space | |
1662 | * | |
1663 | * @listener: an object containing the callbacks to be called | |
7376e582 | 1664 | * @filter: if non-%NULL, only regions in this address space will be observed |
7664e80c | 1665 | */ |
f6790af6 | 1666 | void memory_listener_register(MemoryListener *listener, AddressSpace *filter); |
7664e80c AK |
1667 | |
1668 | /** | |
1669 | * memory_listener_unregister: undo the effect of memory_listener_register() | |
1670 | * | |
1671 | * @listener: an object containing the callbacks to be removed | |
1672 | */ | |
1673 | void memory_listener_unregister(MemoryListener *listener); | |
1674 | ||
1675 | /** | |
1676 | * memory_global_dirty_log_start: begin dirty logging for all regions | |
1677 | */ | |
1678 | void memory_global_dirty_log_start(void); | |
1679 | ||
1680 | /** | |
1a7e8cae | 1681 | * memory_global_dirty_log_stop: end dirty logging for all regions |
7664e80c AK |
1682 | */ |
1683 | void memory_global_dirty_log_stop(void); | |
1684 | ||
5e8fd947 | 1685 | void mtree_info(fprintf_function mon_printf, void *f, bool flatview, |
fc051ae6 | 1686 | bool dispatch_tree, bool owner); |
314e2987 | 1687 | |
3b643495 PM |
1688 | /** |
1689 | * memory_region_dispatch_read: perform a read directly to the specified | |
1690 | * MemoryRegion. | |
1691 | * | |
1692 | * @mr: #MemoryRegion to access | |
1693 | * @addr: address within that region | |
1694 | * @pval: pointer to uint64_t which the data is written to | |
1695 | * @size: size of the access in bytes | |
1696 | * @attrs: memory transaction attributes to use for the access | |
1697 | */ | |
1698 | MemTxResult memory_region_dispatch_read(MemoryRegion *mr, | |
1699 | hwaddr addr, | |
1700 | uint64_t *pval, | |
1701 | unsigned size, | |
1702 | MemTxAttrs attrs); | |
1703 | /** | |
1704 | * memory_region_dispatch_write: perform a write directly to the specified | |
1705 | * MemoryRegion. | |
1706 | * | |
1707 | * @mr: #MemoryRegion to access | |
1708 | * @addr: address within that region | |
1709 | * @data: data to write | |
1710 | * @size: size of the access in bytes | |
1711 | * @attrs: memory transaction attributes to use for the access | |
1712 | */ | |
1713 | MemTxResult memory_region_dispatch_write(MemoryRegion *mr, | |
1714 | hwaddr addr, | |
1715 | uint64_t data, | |
1716 | unsigned size, | |
1717 | MemTxAttrs attrs); | |
1718 | ||
9ad2bbc1 AK |
1719 | /** |
1720 | * address_space_init: initializes an address space | |
1721 | * | |
1722 | * @as: an uninitialized #AddressSpace | |
67cc32eb | 1723 | * @root: a #MemoryRegion that routes addresses for the address space |
7dca8043 AK |
1724 | * @name: an address space name. The name is only used for debugging |
1725 | * output. | |
9ad2bbc1 | 1726 | */ |
7dca8043 | 1727 | void address_space_init(AddressSpace *as, MemoryRegion *root, const char *name); |
9ad2bbc1 | 1728 | |
83f3c251 AK |
1729 | /** |
1730 | * address_space_destroy: destroy an address space | |
1731 | * | |
1732 | * Releases all resources associated with an address space. After an address space | |
1733 | * is destroyed, its root memory region (given by address_space_init()) may be destroyed | |
1734 | * as well. | |
1735 | * | |
1736 | * @as: address space to be destroyed | |
1737 | */ | |
1738 | void address_space_destroy(AddressSpace *as); | |
1739 | ||
ac1970fb AK |
1740 | /** |
1741 | * address_space_rw: read from or write to an address space. | |
1742 | * | |
5c9eb028 PM |
1743 | * Return a MemTxResult indicating whether the operation succeeded |
1744 | * or failed (eg unassigned memory, device rejected the transaction, | |
1745 | * IOMMU fault). | |
fd8aaa76 | 1746 | * |
ac1970fb AK |
1747 | * @as: #AddressSpace to be accessed |
1748 | * @addr: address within that address space | |
5c9eb028 | 1749 | * @attrs: memory transaction attributes |
ac1970fb | 1750 | * @buf: buffer with the data transferred |
57914ecb | 1751 | * @len: the number of bytes to read or write |
ac1970fb AK |
1752 | * @is_write: indicates the transfer direction |
1753 | */ | |
5c9eb028 PM |
1754 | MemTxResult address_space_rw(AddressSpace *as, hwaddr addr, |
1755 | MemTxAttrs attrs, uint8_t *buf, | |
1756 | int len, bool is_write); | |
ac1970fb AK |
1757 | |
1758 | /** | |
1759 | * address_space_write: write to address space. | |
1760 | * | |
5c9eb028 PM |
1761 | * Return a MemTxResult indicating whether the operation succeeded |
1762 | * or failed (eg unassigned memory, device rejected the transaction, | |
1763 | * IOMMU fault). | |
fd8aaa76 | 1764 | * |
ac1970fb AK |
1765 | * @as: #AddressSpace to be accessed |
1766 | * @addr: address within that address space | |
5c9eb028 | 1767 | * @attrs: memory transaction attributes |
ac1970fb | 1768 | * @buf: buffer with the data transferred |
57914ecb | 1769 | * @len: the number of bytes to write |
ac1970fb | 1770 | */ |
5c9eb028 PM |
1771 | MemTxResult address_space_write(AddressSpace *as, hwaddr addr, |
1772 | MemTxAttrs attrs, | |
1773 | const uint8_t *buf, int len); | |
ac1970fb | 1774 | |
3cc8f884 | 1775 | /* address_space_ld*: load from an address space |
50013115 PM |
1776 | * address_space_st*: store to an address space |
1777 | * | |
1778 | * These functions perform a load or store of the byte, word, | |
1779 | * longword or quad to the specified address within the AddressSpace. | |
1780 | * The _le suffixed functions treat the data as little endian; | |
1781 | * _be indicates big endian; no suffix indicates "same endianness | |
1782 | * as guest CPU". | |
1783 | * | |
1784 | * The "guest CPU endianness" accessors are deprecated for use outside | |
1785 | * target-* code; devices should be CPU-agnostic and use either the LE | |
1786 | * or the BE accessors. | |
1787 | * | |
1788 | * @as #AddressSpace to be accessed | |
1789 | * @addr: address within that address space | |
1790 | * @val: data value, for stores | |
1791 | * @attrs: memory transaction attributes | |
1792 | * @result: location to write the success/failure of the transaction; | |
1793 | * if NULL, this information is discarded | |
1794 | */ | |
4269c82b PB |
1795 | |
1796 | #define SUFFIX | |
1797 | #define ARG1 as | |
1798 | #define ARG1_DECL AddressSpace *as | |
1799 | #include "exec/memory_ldst.inc.h" | |
1800 | ||
1801 | #define SUFFIX | |
1802 | #define ARG1 as | |
1803 | #define ARG1_DECL AddressSpace *as | |
1804 | #include "exec/memory_ldst_phys.inc.h" | |
0ce265ff | 1805 | |
1f4e496e | 1806 | struct MemoryRegionCache { |
48564041 | 1807 | void *ptr; |
1f4e496e | 1808 | hwaddr xlat; |
1f4e496e | 1809 | hwaddr len; |
48564041 PB |
1810 | FlatView *fv; |
1811 | MemoryRegionSection mrs; | |
1812 | bool is_write; | |
1f4e496e PB |
1813 | }; |
1814 | ||
48564041 PB |
1815 | #define MEMORY_REGION_CACHE_INVALID ((MemoryRegionCache) { .mrs.mr = NULL }) |
1816 | ||
5eba0404 | 1817 | |
4269c82b PB |
1818 | /* address_space_ld*_cached: load from a cached #MemoryRegion |
1819 | * address_space_st*_cached: store into a cached #MemoryRegion | |
1820 | * | |
1821 | * These functions perform a load or store of the byte, word, | |
1822 | * longword or quad to the specified address. The address is | |
1823 | * a physical address in the AddressSpace, but it must lie within | |
1824 | * a #MemoryRegion that was mapped with address_space_cache_init. | |
1825 | * | |
1826 | * The _le suffixed functions treat the data as little endian; | |
1827 | * _be indicates big endian; no suffix indicates "same endianness | |
1828 | * as guest CPU". | |
1829 | * | |
1830 | * The "guest CPU endianness" accessors are deprecated for use outside | |
1831 | * target-* code; devices should be CPU-agnostic and use either the LE | |
1832 | * or the BE accessors. | |
1833 | * | |
1834 | * @cache: previously initialized #MemoryRegionCache to be accessed | |
1835 | * @addr: address within the address space | |
1836 | * @val: data value, for stores | |
1837 | * @attrs: memory transaction attributes | |
1838 | * @result: location to write the success/failure of the transaction; | |
1839 | * if NULL, this information is discarded | |
1840 | */ | |
1841 | ||
48564041 | 1842 | #define SUFFIX _cached_slow |
4269c82b PB |
1843 | #define ARG1 cache |
1844 | #define ARG1_DECL MemoryRegionCache *cache | |
1845 | #include "exec/memory_ldst.inc.h" | |
1846 | ||
48564041 PB |
1847 | /* Inline fast path for direct RAM access. */ |
1848 | static inline uint8_t address_space_ldub_cached(MemoryRegionCache *cache, | |
1849 | hwaddr addr, MemTxAttrs attrs, MemTxResult *result) | |
1850 | { | |
1851 | assert(addr < cache->len); | |
1852 | if (likely(cache->ptr)) { | |
1853 | return ldub_p(cache->ptr + addr); | |
1854 | } else { | |
1855 | return address_space_ldub_cached_slow(cache, addr, attrs, result); | |
1856 | } | |
1857 | } | |
1858 | ||
1859 | static inline void address_space_stb_cached(MemoryRegionCache *cache, | |
1860 | hwaddr addr, uint32_t val, MemTxAttrs attrs, MemTxResult *result) | |
1861 | { | |
1862 | assert(addr < cache->len); | |
1863 | if (likely(cache->ptr)) { | |
1864 | stb_p(cache->ptr + addr, val); | |
1865 | } else { | |
1866 | address_space_stb_cached_slow(cache, addr, val, attrs, result); | |
1867 | } | |
1868 | } | |
1869 | ||
1870 | #define ENDIANNESS _le | |
1871 | #include "exec/memory_ldst_cached.inc.h" | |
1872 | ||
1873 | #define ENDIANNESS _be | |
1874 | #include "exec/memory_ldst_cached.inc.h" | |
1875 | ||
4269c82b PB |
1876 | #define SUFFIX _cached |
1877 | #define ARG1 cache | |
1878 | #define ARG1_DECL MemoryRegionCache *cache | |
1879 | #include "exec/memory_ldst_phys.inc.h" | |
1880 | ||
1f4e496e PB |
1881 | /* address_space_cache_init: prepare for repeated access to a physical |
1882 | * memory region | |
1883 | * | |
1884 | * @cache: #MemoryRegionCache to be filled | |
1885 | * @as: #AddressSpace to be accessed | |
1886 | * @addr: address within that address space | |
1887 | * @len: length of buffer | |
1888 | * @is_write: indicates the transfer direction | |
1889 | * | |
1890 | * Will only work with RAM, and may map a subset of the requested range by | |
1891 | * returning a value that is less than @len. On failure, return a negative | |
1892 | * errno value. | |
1893 | * | |
1894 | * Because it only works with RAM, this function can be used for | |
1895 | * read-modify-write operations. In this case, is_write should be %true. | |
1896 | * | |
1897 | * Note that addresses passed to the address_space_*_cached functions | |
1898 | * are relative to @addr. | |
1899 | */ | |
1900 | int64_t address_space_cache_init(MemoryRegionCache *cache, | |
1901 | AddressSpace *as, | |
1902 | hwaddr addr, | |
1903 | hwaddr len, | |
1904 | bool is_write); | |
1905 | ||
1906 | /** | |
1907 | * address_space_cache_invalidate: complete a write to a #MemoryRegionCache | |
1908 | * | |
1909 | * @cache: The #MemoryRegionCache to operate on. | |
1910 | * @addr: The first physical address that was written, relative to the | |
1911 | * address that was passed to @address_space_cache_init. | |
1912 | * @access_len: The number of bytes that were written starting at @addr. | |
1913 | */ | |
1914 | void address_space_cache_invalidate(MemoryRegionCache *cache, | |
1915 | hwaddr addr, | |
1916 | hwaddr access_len); | |
1917 | ||
1918 | /** | |
1919 | * address_space_cache_destroy: free a #MemoryRegionCache | |
1920 | * | |
1921 | * @cache: The #MemoryRegionCache whose memory should be released. | |
1922 | */ | |
1923 | void address_space_cache_destroy(MemoryRegionCache *cache); | |
1924 | ||
052c8fa9 JW |
1925 | /* address_space_get_iotlb_entry: translate an address into an IOTLB |
1926 | * entry. Should be called from an RCU critical section. | |
1927 | */ | |
1928 | IOMMUTLBEntry address_space_get_iotlb_entry(AddressSpace *as, hwaddr addr, | |
7446eb07 | 1929 | bool is_write, MemTxAttrs attrs); |
1f4e496e | 1930 | |
149f54b5 | 1931 | /* address_space_translate: translate an address range into an address space |
41063e1e PB |
1932 | * into a MemoryRegion and an address range into that section. Should be |
1933 | * called from an RCU critical section, to avoid that the last reference | |
1934 | * to the returned region disappears after address_space_translate returns. | |
149f54b5 | 1935 | * |
57914ecb | 1936 | * @fv: #FlatView to be accessed |
149f54b5 PB |
1937 | * @addr: address within that address space |
1938 | * @xlat: pointer to address within the returned memory region section's | |
1939 | * #MemoryRegion. | |
1940 | * @len: pointer to length | |
1941 | * @is_write: indicates the transfer direction | |
bc6b1cec | 1942 | * @attrs: memory attributes |
149f54b5 | 1943 | */ |
16620684 AK |
1944 | MemoryRegion *flatview_translate(FlatView *fv, |
1945 | hwaddr addr, hwaddr *xlat, | |
efa99a2f PM |
1946 | hwaddr *len, bool is_write, |
1947 | MemTxAttrs attrs); | |
16620684 AK |
1948 | |
1949 | static inline MemoryRegion *address_space_translate(AddressSpace *as, | |
1950 | hwaddr addr, hwaddr *xlat, | |
bc6b1cec PM |
1951 | hwaddr *len, bool is_write, |
1952 | MemTxAttrs attrs) | |
16620684 AK |
1953 | { |
1954 | return flatview_translate(address_space_to_flatview(as), | |
efa99a2f | 1955 | addr, xlat, len, is_write, attrs); |
16620684 | 1956 | } |
149f54b5 | 1957 | |
51644ab7 PB |
1958 | /* address_space_access_valid: check for validity of accessing an address |
1959 | * space range | |
1960 | * | |
30951157 AK |
1961 | * Check whether memory is assigned to the given address space range, and |
1962 | * access is permitted by any IOMMU regions that are active for the address | |
1963 | * space. | |
51644ab7 PB |
1964 | * |
1965 | * For now, addr and len should be aligned to a page size. This limitation | |
1966 | * will be lifted in the future. | |
1967 | * | |
1968 | * @as: #AddressSpace to be accessed | |
1969 | * @addr: address within that address space | |
1970 | * @len: length of the area to be checked | |
1971 | * @is_write: indicates the transfer direction | |
fddffa42 | 1972 | * @attrs: memory attributes |
51644ab7 | 1973 | */ |
fddffa42 PM |
1974 | bool address_space_access_valid(AddressSpace *as, hwaddr addr, int len, |
1975 | bool is_write, MemTxAttrs attrs); | |
51644ab7 | 1976 | |
ac1970fb AK |
1977 | /* address_space_map: map a physical memory region into a host virtual address |
1978 | * | |
1979 | * May map a subset of the requested range, given by and returned in @plen. | |
1980 | * May return %NULL if resources needed to perform the mapping are exhausted. | |
1981 | * Use only for reads OR writes - not for read-modify-write operations. | |
1982 | * Use cpu_register_map_client() to know when retrying the map operation is | |
1983 | * likely to succeed. | |
1984 | * | |
1985 | * @as: #AddressSpace to be accessed | |
1986 | * @addr: address within that address space | |
1987 | * @plen: pointer to length of buffer; updated on return | |
1988 | * @is_write: indicates the transfer direction | |
f26404fb | 1989 | * @attrs: memory attributes |
ac1970fb | 1990 | */ |
a8170e5e | 1991 | void *address_space_map(AddressSpace *as, hwaddr addr, |
f26404fb | 1992 | hwaddr *plen, bool is_write, MemTxAttrs attrs); |
ac1970fb AK |
1993 | |
1994 | /* address_space_unmap: Unmaps a memory region previously mapped by address_space_map() | |
1995 | * | |
1996 | * Will also mark the memory as dirty if @is_write == %true. @access_len gives | |
1997 | * the amount of memory that was actually read or written by the caller. | |
1998 | * | |
1999 | * @as: #AddressSpace used | |
57914ecb | 2000 | * @buffer: host pointer as returned by address_space_map() |
ac1970fb AK |
2001 | * @len: buffer length as returned by address_space_map() |
2002 | * @access_len: amount of data actually transferred | |
2003 | * @is_write: indicates the transfer direction | |
2004 | */ | |
a8170e5e AK |
2005 | void address_space_unmap(AddressSpace *as, void *buffer, hwaddr len, |
2006 | int is_write, hwaddr access_len); | |
ac1970fb AK |
2007 | |
2008 | ||
a203ac70 | 2009 | /* Internal functions, part of the implementation of address_space_read. */ |
b2a44fca PB |
2010 | MemTxResult address_space_read_full(AddressSpace *as, hwaddr addr, |
2011 | MemTxAttrs attrs, uint8_t *buf, int len); | |
16620684 AK |
2012 | MemTxResult flatview_read_continue(FlatView *fv, hwaddr addr, |
2013 | MemTxAttrs attrs, uint8_t *buf, | |
2014 | int len, hwaddr addr1, hwaddr l, | |
2015 | MemoryRegion *mr); | |
0878d0e1 | 2016 | void *qemu_map_ram_ptr(RAMBlock *ram_block, ram_addr_t addr); |
3cc8f884 | 2017 | |
48564041 PB |
2018 | /* Internal functions, part of the implementation of address_space_read_cached |
2019 | * and address_space_write_cached. */ | |
2020 | void address_space_read_cached_slow(MemoryRegionCache *cache, | |
2021 | hwaddr addr, void *buf, int len); | |
2022 | void address_space_write_cached_slow(MemoryRegionCache *cache, | |
2023 | hwaddr addr, const void *buf, int len); | |
2024 | ||
3cc8f884 PB |
2025 | static inline bool memory_access_is_direct(MemoryRegion *mr, bool is_write) |
2026 | { | |
2027 | if (is_write) { | |
4a2e242b AW |
2028 | return memory_region_is_ram(mr) && |
2029 | !mr->readonly && !memory_region_is_ram_device(mr); | |
3cc8f884 | 2030 | } else { |
4a2e242b AW |
2031 | return (memory_region_is_ram(mr) && !memory_region_is_ram_device(mr)) || |
2032 | memory_region_is_romd(mr); | |
3cc8f884 | 2033 | } |
3cc8f884 PB |
2034 | } |
2035 | ||
2036 | /** | |
2037 | * address_space_read: read from an address space. | |
2038 | * | |
2039 | * Return a MemTxResult indicating whether the operation succeeded | |
2040 | * or failed (eg unassigned memory, device rejected the transaction, | |
b2a44fca | 2041 | * IOMMU fault). Called within RCU critical section. |
3cc8f884 | 2042 | * |
b2a44fca | 2043 | * @as: #AddressSpace to be accessed |
3cc8f884 PB |
2044 | * @addr: address within that address space |
2045 | * @attrs: memory transaction attributes | |
2046 | * @buf: buffer with the data transferred | |
2047 | */ | |
2048 | static inline __attribute__((__always_inline__)) | |
b2a44fca PB |
2049 | MemTxResult address_space_read(AddressSpace *as, hwaddr addr, |
2050 | MemTxAttrs attrs, uint8_t *buf, | |
2051 | int len) | |
3cc8f884 PB |
2052 | { |
2053 | MemTxResult result = MEMTX_OK; | |
2054 | hwaddr l, addr1; | |
2055 | void *ptr; | |
2056 | MemoryRegion *mr; | |
b2a44fca | 2057 | FlatView *fv; |
3cc8f884 PB |
2058 | |
2059 | if (__builtin_constant_p(len)) { | |
2060 | if (len) { | |
2061 | rcu_read_lock(); | |
b2a44fca | 2062 | fv = address_space_to_flatview(as); |
3cc8f884 | 2063 | l = len; |
efa99a2f | 2064 | mr = flatview_translate(fv, addr, &addr1, &l, false, attrs); |
3cc8f884 | 2065 | if (len == l && memory_access_is_direct(mr, false)) { |
0878d0e1 | 2066 | ptr = qemu_map_ram_ptr(mr->ram_block, addr1); |
3cc8f884 PB |
2067 | memcpy(buf, ptr, len); |
2068 | } else { | |
16620684 AK |
2069 | result = flatview_read_continue(fv, addr, attrs, buf, len, |
2070 | addr1, l, mr); | |
3cc8f884 PB |
2071 | } |
2072 | rcu_read_unlock(); | |
2073 | } | |
2074 | } else { | |
b2a44fca | 2075 | result = address_space_read_full(as, addr, attrs, buf, len); |
3cc8f884 PB |
2076 | } |
2077 | return result; | |
2078 | } | |
a203ac70 | 2079 | |
1f4e496e PB |
2080 | /** |
2081 | * address_space_read_cached: read from a cached RAM region | |
2082 | * | |
2083 | * @cache: Cached region to be addressed | |
2084 | * @addr: address relative to the base of the RAM region | |
2085 | * @buf: buffer with the data transferred | |
2086 | * @len: length of the data transferred | |
2087 | */ | |
2088 | static inline void | |
2089 | address_space_read_cached(MemoryRegionCache *cache, hwaddr addr, | |
2090 | void *buf, int len) | |
2091 | { | |
2092 | assert(addr < cache->len && len <= cache->len - addr); | |
48564041 PB |
2093 | if (likely(cache->ptr)) { |
2094 | memcpy(buf, cache->ptr + addr, len); | |
2095 | } else { | |
2096 | address_space_read_cached_slow(cache, addr, buf, len); | |
2097 | } | |
1f4e496e PB |
2098 | } |
2099 | ||
2100 | /** | |
2101 | * address_space_write_cached: write to a cached RAM region | |
2102 | * | |
2103 | * @cache: Cached region to be addressed | |
2104 | * @addr: address relative to the base of the RAM region | |
2105 | * @buf: buffer with the data transferred | |
2106 | * @len: length of the data transferred | |
2107 | */ | |
2108 | static inline void | |
2109 | address_space_write_cached(MemoryRegionCache *cache, hwaddr addr, | |
2110 | void *buf, int len) | |
2111 | { | |
2112 | assert(addr < cache->len && len <= cache->len - addr); | |
48564041 PB |
2113 | if (likely(cache->ptr)) { |
2114 | memcpy(cache->ptr + addr, buf, len); | |
2115 | } else { | |
2116 | address_space_write_cached_slow(cache, addr, buf, len); | |
2117 | } | |
1f4e496e PB |
2118 | } |
2119 | ||
093bc2cd AK |
2120 | #endif |
2121 | ||
2122 | #endif |