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