Remove unnecessary casts from PCI DMA code in lsi53c895a
[qemu/qmp-unstable.git] / memory.h
blob53bf261792fc51776d3b4f8e471652d2e1f752d6
1 /*
2 * Physical memory management API
4 * Copyright 2011 Red Hat, Inc. and/or its affiliates
6 * Authors:
7 * Avi Kivity <avi@redhat.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #ifndef MEMORY_H
15 #define MEMORY_H
17 #ifndef CONFIG_USER_ONLY
19 #include <stdint.h>
20 #include <stdbool.h>
21 #include "qemu-common.h"
22 #include "cpu-common.h"
23 #include "targphys.h"
24 #include "qemu-queue.h"
25 #include "iorange.h"
26 #include "ioport.h"
27 #include "int128.h"
29 typedef struct MemoryRegionOps MemoryRegionOps;
30 typedef struct MemoryRegion MemoryRegion;
31 typedef struct MemoryRegionPortio MemoryRegionPortio;
32 typedef struct MemoryRegionMmio MemoryRegionMmio;
34 /* Must match *_DIRTY_FLAGS in cpu-all.h. To be replaced with dynamic
35 * registration.
37 #define DIRTY_MEMORY_VGA 0
38 #define DIRTY_MEMORY_CODE 1
39 #define DIRTY_MEMORY_MIGRATION 3
41 struct MemoryRegionMmio {
42 CPUReadMemoryFunc *read[3];
43 CPUWriteMemoryFunc *write[3];
47 * Memory region callbacks
49 struct MemoryRegionOps {
50 /* Read from the memory region. @addr is relative to @mr; @size is
51 * in bytes. */
52 uint64_t (*read)(void *opaque,
53 target_phys_addr_t addr,
54 unsigned size);
55 /* Write to the memory region. @addr is relative to @mr; @size is
56 * in bytes. */
57 void (*write)(void *opaque,
58 target_phys_addr_t addr,
59 uint64_t data,
60 unsigned size);
62 enum device_endian endianness;
63 /* Guest-visible constraints: */
64 struct {
65 /* If nonzero, specify bounds on access sizes beyond which a machine
66 * check is thrown.
68 unsigned min_access_size;
69 unsigned max_access_size;
70 /* If true, unaligned accesses are supported. Otherwise unaligned
71 * accesses throw machine checks.
73 bool unaligned;
75 * If present, and returns #false, the transaction is not accepted
76 * by the device (and results in machine dependent behaviour such
77 * as a machine check exception).
79 bool (*accepts)(void *opaque, target_phys_addr_t addr,
80 unsigned size, bool is_write);
81 } valid;
82 /* Internal implementation constraints: */
83 struct {
84 /* If nonzero, specifies the minimum size implemented. Smaller sizes
85 * will be rounded upwards and a partial result will be returned.
87 unsigned min_access_size;
88 /* If nonzero, specifies the maximum size implemented. Larger sizes
89 * will be done as a series of accesses with smaller sizes.
91 unsigned max_access_size;
92 /* If true, unaligned accesses are supported. Otherwise all accesses
93 * are converted to (possibly multiple) naturally aligned accesses.
95 bool unaligned;
96 } impl;
98 /* If .read and .write are not present, old_portio may be used for
99 * backwards compatibility with old portio registration
101 const MemoryRegionPortio *old_portio;
102 /* If .read and .write are not present, old_mmio may be used for
103 * backwards compatibility with old mmio registration
105 const MemoryRegionMmio old_mmio;
108 typedef struct CoalescedMemoryRange CoalescedMemoryRange;
109 typedef struct MemoryRegionIoeventfd MemoryRegionIoeventfd;
111 struct MemoryRegion {
112 /* All fields are private - violators will be prosecuted */
113 const MemoryRegionOps *ops;
114 void *opaque;
115 MemoryRegion *parent;
116 Int128 size;
117 target_phys_addr_t addr;
118 target_phys_addr_t offset;
119 bool backend_registered;
120 void (*destructor)(MemoryRegion *mr);
121 ram_addr_t ram_addr;
122 IORange iorange;
123 bool terminates;
124 bool readable;
125 bool readonly; /* For RAM regions */
126 MemoryRegion *alias;
127 target_phys_addr_t alias_offset;
128 unsigned priority;
129 bool may_overlap;
130 QTAILQ_HEAD(subregions, MemoryRegion) subregions;
131 QTAILQ_ENTRY(MemoryRegion) subregions_link;
132 QTAILQ_HEAD(coalesced_ranges, CoalescedMemoryRange) coalesced;
133 const char *name;
134 uint8_t dirty_log_mask;
135 unsigned ioeventfd_nb;
136 MemoryRegionIoeventfd *ioeventfds;
139 struct MemoryRegionPortio {
140 uint32_t offset;
141 uint32_t len;
142 unsigned size;
143 IOPortReadFunc *read;
144 IOPortWriteFunc *write;
147 #define PORTIO_END_OF_LIST() { }
150 * memory_region_init: Initialize a memory region
152 * The region typically acts as a container for other memory regions. Us
153 * memory_region_add_subregion() to add subregions.
155 * @mr: the #MemoryRegion to be initialized
156 * @name: used for debugging; not visible to the user or ABI
157 * @size: size of the region; any subregions beyond this size will be clipped
159 void memory_region_init(MemoryRegion *mr,
160 const char *name,
161 uint64_t size);
163 * memory_region_init_io: Initialize an I/O memory region.
165 * Accesses into the region will be cause the callbacks in @ops to be called.
166 * if @size is nonzero, subregions will be clipped to @size.
168 * @mr: the #MemoryRegion to be initialized.
169 * @ops: a structure containing read and write callbacks to be used when
170 * I/O is performed on the region.
171 * @opaque: passed to to the read and write callbacks of the @ops structure.
172 * @name: used for debugging; not visible to the user or ABI
173 * @size: size of the region.
175 void memory_region_init_io(MemoryRegion *mr,
176 const MemoryRegionOps *ops,
177 void *opaque,
178 const char *name,
179 uint64_t size);
182 * memory_region_init_ram: Initialize RAM memory region. Accesses into the
183 * region will be modify memory directly.
185 * @mr: the #MemoryRegion to be initialized.
186 * @dev: a device associated with the region; may be %NULL.
187 * @name: the name of the region; the pair (@dev, @name) must be globally
188 * unique. The name is part of the save/restore ABI and so cannot be
189 * changed.
190 * @size: size of the region.
192 void memory_region_init_ram(MemoryRegion *mr,
193 DeviceState *dev, /* FIXME: layering violation */
194 const char *name,
195 uint64_t size);
198 * memory_region_init_ram: Initialize RAM memory region from a user-provided.
199 * pointer. Accesses into the region will be modify
200 * memory directly.
202 * @mr: the #MemoryRegion to be initialized.
203 * @dev: a device associated with the region; may be %NULL.
204 * @name: the name of the region; the pair (@dev, @name) must be globally
205 * unique. The name is part of the save/restore ABI and so cannot be
206 * changed.
207 * @size: size of the region.
208 * @ptr: memory to be mapped; must contain at least @size bytes.
210 void memory_region_init_ram_ptr(MemoryRegion *mr,
211 DeviceState *dev, /* FIXME: layering violation */
212 const char *name,
213 uint64_t size,
214 void *ptr);
217 * memory_region_init_alias: Initialize a memory region that aliases all or a
218 * part of another memory region.
220 * @mr: the #MemoryRegion to be initialized.
221 * @name: used for debugging; not visible to the user or ABI
222 * @orig: the region to be referenced; @mr will be equivalent to
223 * @orig between @offset and @offset + @size - 1.
224 * @offset: start of the section in @orig to be referenced.
225 * @size: size of the region.
227 void memory_region_init_alias(MemoryRegion *mr,
228 const char *name,
229 MemoryRegion *orig,
230 target_phys_addr_t offset,
231 uint64_t size);
234 * memory_region_init_rom_device: Initialize a ROM memory region. Writes are
235 * handled via callbacks.
237 * @mr: the #MemoryRegion to be initialized.
238 * @ops: callbacks for write access handling.
239 * @dev: a device associated with the region; may be %NULL.
240 * @name: the name of the region; the pair (@dev, @name) must be globally
241 * unique. The name is part of the save/restore ABI and so cannot be
242 * changed.
243 * @size: size of the region.
245 void memory_region_init_rom_device(MemoryRegion *mr,
246 const MemoryRegionOps *ops,
247 void *opaque,
248 DeviceState *dev, /* FIXME: layering violation */
249 const char *name,
250 uint64_t size);
253 * memory_region_destroy: Destroy a memory region and relaim all resources.
255 * @mr: the region to be destroyed. May not currently be a subregion
256 * (see memory_region_add_subregion()) or referenced in an alias
257 * (see memory_region_init_alias()).
259 void memory_region_destroy(MemoryRegion *mr);
262 * memory_region_size: get a memory region's size.
264 * @mr: the memory region being queried.
266 uint64_t memory_region_size(MemoryRegion *mr);
269 * memory_region_get_ram_ptr: Get a pointer into a RAM memory region.
271 * Returns a host pointer to a RAM memory region (created with
272 * memory_region_init_ram() or memory_region_init_ram_ptr()). Use with
273 * care.
275 * @mr: the memory region being queried.
277 void *memory_region_get_ram_ptr(MemoryRegion *mr);
280 * memory_region_set_offset: Sets an offset to be added to MemoryRegionOps
281 * callbacks.
283 * This function is deprecated and should not be used in new code.
285 void memory_region_set_offset(MemoryRegion *mr, target_phys_addr_t offset);
288 * memory_region_set_log: Turn dirty logging on or off for a region.
290 * Turns dirty logging on or off for a specified client (display, migration).
291 * Only meaningful for RAM regions.
293 * @mr: the memory region being updated.
294 * @log: whether dirty logging is to be enabled or disabled.
295 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
296 * %DIRTY_MEMORY_VGA.
298 void memory_region_set_log(MemoryRegion *mr, bool log, unsigned client);
301 * memory_region_get_dirty: Check whether a page is dirty for a specified
302 * client.
304 * Checks whether a page has been written to since the last
305 * call to memory_region_reset_dirty() with the same @client. Dirty logging
306 * must be enabled.
308 * @mr: the memory region being queried.
309 * @addr: the address (relative to the start of the region) being queried.
310 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
311 * %DIRTY_MEMORY_VGA.
313 bool memory_region_get_dirty(MemoryRegion *mr, target_phys_addr_t addr,
314 unsigned client);
317 * memory_region_set_dirty: Mark a page as dirty in a memory region.
319 * Marks a page as dirty, after it has been dirtied outside guest code.
321 * @mr: the memory region being queried.
322 * @addr: the address (relative to the start of the region) being dirtied.
324 void memory_region_set_dirty(MemoryRegion *mr, target_phys_addr_t addr);
327 * memory_region_sync_dirty_bitmap: Synchronize a region's dirty bitmap with
328 * any external TLBs (e.g. kvm)
330 * Flushes dirty information from accelerators such as kvm and vhost-net
331 * and makes it available to users of the memory API.
333 * @mr: the region being flushed.
335 void memory_region_sync_dirty_bitmap(MemoryRegion *mr);
338 * memory_region_reset_dirty: Mark a range of pages as clean, for a specified
339 * client.
341 * Marks a range of pages as no longer dirty.
343 * @mr: the region being updated.
344 * @addr: the start of the subrange being cleaned.
345 * @size: the size of the subrange being cleaned.
346 * @client: the user of the logging information; %DIRTY_MEMORY_MIGRATION or
347 * %DIRTY_MEMORY_VGA.
349 void memory_region_reset_dirty(MemoryRegion *mr, target_phys_addr_t addr,
350 target_phys_addr_t size, unsigned client);
353 * memory_region_set_readonly: Turn a memory region read-only (or read-write)
355 * Allows a memory region to be marked as read-only (turning it into a ROM).
356 * only useful on RAM regions.
358 * @mr: the region being updated.
359 * @readonly: whether rhe region is to be ROM or RAM.
361 void memory_region_set_readonly(MemoryRegion *mr, bool readonly);
364 * memory_region_rom_device_set_readable: enable/disable ROM readability
366 * Allows a ROM device (initialized with memory_region_init_rom_device() to
367 * to be marked as readable (default) or not readable. When it is readable,
368 * the device is mapped to guest memory. When not readable, reads are
369 * forwarded to the #MemoryRegion.read function.
371 * @mr: the memory region to be updated
372 * @readable: whether reads are satisified directly (%true) or via callbacks
373 * (%false)
375 void memory_region_rom_device_set_readable(MemoryRegion *mr, bool readable);
378 * memory_region_set_coalescing: Enable memory coalescing for the region.
380 * Enabled writes to a region to be queued for later processing. MMIO ->write
381 * callbacks may be delayed until a non-coalesced MMIO is issued.
382 * Only useful for IO regions. Roughly similar to write-combining hardware.
384 * @mr: the memory region to be write coalesced
386 void memory_region_set_coalescing(MemoryRegion *mr);
389 * memory_region_add_coalescing: Enable memory coalescing for a sub-range of
390 * a region.
392 * Like memory_region_set_coalescing(), but works on a sub-range of a region.
393 * Multiple calls can be issued coalesced disjoint ranges.
395 * @mr: the memory region to be updated.
396 * @offset: the start of the range within the region to be coalesced.
397 * @size: the size of the subrange to be coalesced.
399 void memory_region_add_coalescing(MemoryRegion *mr,
400 target_phys_addr_t offset,
401 uint64_t size);
404 * memory_region_clear_coalescing: Disable MMIO coalescing for the region.
406 * Disables any coalescing caused by memory_region_set_coalescing() or
407 * memory_region_add_coalescing(). Roughly equivalent to uncacheble memory
408 * hardware.
410 * @mr: the memory region to be updated.
412 void memory_region_clear_coalescing(MemoryRegion *mr);
415 * memory_region_add_eventfd: Request an eventfd to be triggered when a word
416 * is written to a location.
418 * Marks a word in an IO region (initialized with memory_region_init_io())
419 * as a trigger for an eventfd event. The I/O callback will not be called.
420 * The caller must be prepared to handle failure (hat is, take the required
421 * action if the callback _is_ called).
423 * @mr: the memory region being updated.
424 * @addr: the address within @mr that is to be monitored
425 * @size: the size of the access to trigger the eventfd
426 * @match_data: whether to match against @data, instead of just @addr
427 * @data: the data to match against the guest write
428 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
430 void memory_region_add_eventfd(MemoryRegion *mr,
431 target_phys_addr_t addr,
432 unsigned size,
433 bool match_data,
434 uint64_t data,
435 int fd);
438 * memory_region_del_eventfd: Cancel and eventfd.
440 * Cancels an eventfd trigger request by a previous memory_region_add_eventfd()
441 * call.
443 * @mr: the memory region being updated.
444 * @addr: the address within @mr that is to be monitored
445 * @size: the size of the access to trigger the eventfd
446 * @match_data: whether to match against @data, instead of just @addr
447 * @data: the data to match against the guest write
448 * @fd: the eventfd to be triggered when @addr, @size, and @data all match.
450 void memory_region_del_eventfd(MemoryRegion *mr,
451 target_phys_addr_t addr,
452 unsigned size,
453 bool match_data,
454 uint64_t data,
455 int fd);
457 * memory_region_add_subregion: Add a sub-region to a container.
459 * Adds a sub-region at @offset. The sub-region may not overlap with other
460 * subregions (except for those explicitly marked as overlapping). A region
461 * may only be added once as a subregion (unless removed with
462 * memory_region_del_subregion()); use memory_region_init_alias() if you
463 * want a region to be a subregion in multiple locations.
465 * @mr: the region to contain the new subregion; must be a container
466 * initialized with memory_region_init().
467 * @offset: the offset relative to @mr where @subregion is added.
468 * @subregion: the subregion to be added.
470 void memory_region_add_subregion(MemoryRegion *mr,
471 target_phys_addr_t offset,
472 MemoryRegion *subregion);
474 * memory_region_add_subregion: Add a sub-region to a container, with overlap.
476 * Adds a sub-region at @offset. The sub-region may overlap with other
477 * subregions. Conflicts are resolved by having a higher @priority hide a
478 * lower @priority. Subregions without priority are taken as @priority 0.
479 * A region may only be added once as a subregion (unless removed with
480 * memory_region_del_subregion()); use memory_region_init_alias() if you
481 * want a region to be a subregion in multiple locations.
483 * @mr: the region to contain the new subregion; must be a container
484 * initialized with memory_region_init().
485 * @offset: the offset relative to @mr where @subregion is added.
486 * @subregion: the subregion to be added.
487 * @priority: used for resolving overlaps; highest priority wins.
489 void memory_region_add_subregion_overlap(MemoryRegion *mr,
490 target_phys_addr_t offset,
491 MemoryRegion *subregion,
492 unsigned priority);
494 * memory_region_del_subregion: Remove a subregion.
496 * Removes a subregion from its container.
498 * @mr: the container to be updated.
499 * @subregion: the region being removed; must be a current subregion of @mr.
501 void memory_region_del_subregion(MemoryRegion *mr,
502 MemoryRegion *subregion);
504 /* Start a transaction; changes will be accumulated and made visible only
505 * when the transaction ends.
507 void memory_region_transaction_begin(void);
508 /* Commit a transaction and make changes visible to the guest.
510 void memory_region_transaction_commit(void);
512 void mtree_info(fprintf_function mon_printf, void *f);
514 #endif
516 #endif