blob: db1496c6679e60c5b58e3ecce65c78ee5d87b2c1 [file] [log] [blame]
Anthony Liguori8a0743c2013-04-16 09:45:18 -05001/*
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 Armbruster26491a32013-06-26 15:52:22 +020014#include "libqos/fw_cfg.h"
Anthony Liguori8a0743c2013-04-16 09:45:18 -050015
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
24typedef struct PCAlloc
25{
26 QGuestAllocator alloc;
27
28 uint64_t start;
29 uint64_t end;
30} PCAlloc;
31
32static 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
49static void pc_free(QGuestAllocator *allocator, uint64_t addr)
50{
51}
52
53QGuestAllocator *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}