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