Anthony Liguori | 8a0743c | 2013-04-16 09:45:18 -0500 | [diff] [blame] | 1 | /* |
| 2 | * libqos malloc support for PC |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2012-2013 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "libqos/malloc-pc.h" |
Markus Armbruster | 26491a3 | 2013-06-26 15:52:22 +0200 | [diff] [blame] | 14 | #include "libqos/fw_cfg.h" |
Anthony Liguori | 8a0743c | 2013-04-16 09:45:18 -0500 | [diff] [blame] | 15 | |
| 16 | #define NO_QEMU_PROTOS |
| 17 | #include "hw/nvram/fw_cfg.h" |
| 18 | |
| 19 | #include "qemu-common.h" |
| 20 | #include <glib.h> |
| 21 | |
| 22 | #define PAGE_SIZE (4096) |
| 23 | |
| 24 | typedef struct PCAlloc |
| 25 | { |
| 26 | QGuestAllocator alloc; |
| 27 | |
| 28 | uint64_t start; |
| 29 | uint64_t end; |
| 30 | } PCAlloc; |
| 31 | |
| 32 | static uint64_t pc_alloc(QGuestAllocator *allocator, size_t size) |
| 33 | { |
| 34 | PCAlloc *s = container_of(allocator, PCAlloc, alloc); |
| 35 | uint64_t addr; |
| 36 | |
| 37 | |
| 38 | size += (PAGE_SIZE - 1); |
| 39 | size &= PAGE_SIZE; |
| 40 | |
| 41 | g_assert_cmpint((s->start + size), <=, s->end); |
| 42 | |
| 43 | addr = s->start; |
| 44 | s->start += size; |
| 45 | |
| 46 | return addr; |
| 47 | } |
| 48 | |
| 49 | static void pc_free(QGuestAllocator *allocator, uint64_t addr) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | QGuestAllocator *pc_alloc_init(void) |
| 54 | { |
| 55 | PCAlloc *s = g_malloc0(sizeof(*s)); |
| 56 | uint64_t ram_size; |
| 57 | QFWCFG *fw_cfg = pc_fw_cfg_init(); |
| 58 | |
| 59 | s->alloc.alloc = pc_alloc; |
| 60 | s->alloc.free = pc_free; |
| 61 | |
| 62 | ram_size = qfw_cfg_get_u64(fw_cfg, FW_CFG_RAM_SIZE); |
| 63 | |
| 64 | /* Start at 1MB */ |
| 65 | s->start = 1 << 20; |
| 66 | |
| 67 | /* Respect PCI hole */ |
| 68 | s->end = MIN(ram_size, 0xE0000000); |
| 69 | |
| 70 | return &s->alloc; |
| 71 | } |