xref: /kvm-unit-tests/lib/alloc_phys.c (revision 4363f1d9a646a5c7ea673bee8fc33ca6f2cddbd8)
1 /*
2  * Copyright (C) 2014, Red Hat Inc, Andrew Jones <drjones@redhat.com>
3  *
4  * This work is licensed under the terms of the GNU LGPL, version 2.
5  *
6  * This is a simple allocator that provides contiguous physical addresses
7  * with byte granularity.
8  */
9 #include "alloc.h"
10 #include "asm/spinlock.h"
11 #include "asm/io.h"
12 
13 #define PHYS_ALLOC_NR_REGIONS	256
14 
15 #define DEFAULT_MINIMUM_ALIGNMENT	32
16 
17 struct phys_alloc_region {
18 	phys_addr_t base;
19 	phys_addr_t size;
20 };
21 
22 static struct phys_alloc_region regions[PHYS_ALLOC_NR_REGIONS];
23 static int nr_regions;
24 
25 static struct spinlock lock;
26 static phys_addr_t base, top;
27 
28 static void *early_memalign(size_t alignment, size_t size);
29 static struct alloc_ops early_alloc_ops = {
30 	.memalign = early_memalign,
31 	.align_min = DEFAULT_MINIMUM_ALIGNMENT
32 };
33 
34 struct alloc_ops *alloc_ops = &early_alloc_ops;
35 
36 void phys_alloc_show(void)
37 {
38 	int i;
39 
40 	spin_lock(&lock);
41 	printf("phys_alloc minimum alignment: %#" PRIx64 "\n",
42 		(u64)early_alloc_ops.align_min);
43 	for (i = 0; i < nr_regions; ++i)
44 		printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n",
45 			(u64)regions[i].base,
46 			(u64)(regions[i].base + regions[i].size - 1),
47 			"USED");
48 	printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n",
49 		(u64)base, (u64)(top - 1), "FREE");
50 	spin_unlock(&lock);
51 }
52 
53 void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size)
54 {
55 	spin_lock(&lock);
56 	base = base_addr;
57 	top = base + size;
58 	nr_regions = 0;
59 	spin_unlock(&lock);
60 }
61 
62 void phys_alloc_set_minimum_alignment(phys_addr_t align)
63 {
64 	assert(align && !(align & (align - 1)));
65 	spin_lock(&lock);
66 	early_alloc_ops.align_min = align;
67 	spin_unlock(&lock);
68 }
69 
70 static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
71 					   phys_addr_t align, bool safe)
72 {
73 	static bool warned = false;
74 	phys_addr_t addr, size_orig = size;
75 	u64 top_safe;
76 
77 	spin_lock(&lock);
78 
79 	top_safe = top;
80 
81 	if (safe && sizeof(long) == 4)
82 		top_safe = MIN(top_safe, 1ULL << 32);
83 
84 	addr = ALIGN(base, align);
85 	size += addr - base;
86 
87 	if ((top_safe - base) < size) {
88 		printf("phys_alloc: requested=%#" PRIx64
89 		       " (align=%#" PRIx64 "), "
90 		       "need=%#" PRIx64 ", but free=%#" PRIx64 ". "
91 		       "top=%#" PRIx64 ", top_safe=%#" PRIx64 "\n",
92 		       (u64)size_orig, (u64)align, (u64)size, top_safe - base,
93 		       (u64)top, top_safe);
94 		spin_unlock(&lock);
95 		return INVALID_PHYS_ADDR;
96 	}
97 
98 	base += size;
99 
100 	if (nr_regions < PHYS_ALLOC_NR_REGIONS) {
101 		regions[nr_regions].base = addr;
102 		regions[nr_regions].size = size_orig;
103 		++nr_regions;
104 	} else if (!warned) {
105 		printf("WARNING: phys_alloc: No free log entries, "
106 		       "can no longer log allocations...\n");
107 		warned = true;
108 	}
109 
110 	spin_unlock(&lock);
111 
112 	return addr;
113 }
114 
115 void phys_alloc_get_unused(phys_addr_t *p_base, phys_addr_t *p_top)
116 {
117 	*p_base = base;
118 	*p_top = top;
119 }
120 
121 static void *early_memalign(size_t alignment, size_t size)
122 {
123 	phys_addr_t addr;
124 
125 	assert(alignment && !(alignment & (alignment - 1)));
126 
127 	addr = phys_alloc_aligned_safe(size, alignment, true);
128 	if (addr == INVALID_PHYS_ADDR)
129 		return NULL;
130 
131 	return phys_to_virt(addr);
132 }
133