xref: /kvm-unit-tests/lib/alloc_phys.c (revision 2b527259a3b423f61a8d4b9546c350cc00847aa6)
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 	assert(base < top_safe);
85 
86 	addr = ALIGN(base, align);
87 	size += addr - base;
88 
89 	if ((top_safe - base) < size) {
90 		printf("phys_alloc: requested=%#" PRIx64
91 		       " (align=%#" PRIx64 "), "
92 		       "need=%#" PRIx64 ", but free=%#" PRIx64 ". "
93 		       "top=%#" PRIx64 ", top_safe=%#" PRIx64 "\n",
94 		       (u64)size_orig, (u64)align, (u64)size, top_safe - base,
95 		       (u64)top, top_safe);
96 		spin_unlock(&lock);
97 		return INVALID_PHYS_ADDR;
98 	}
99 
100 	base += size;
101 
102 	if (nr_regions < PHYS_ALLOC_NR_REGIONS) {
103 		regions[nr_regions].base = addr;
104 		regions[nr_regions].size = size_orig;
105 		++nr_regions;
106 	} else if (!warned) {
107 		printf("WARNING: phys_alloc: No free log entries, "
108 		       "can no longer log allocations...\n");
109 		warned = true;
110 	}
111 
112 	spin_unlock(&lock);
113 
114 	return addr;
115 }
116 
117 void phys_alloc_get_unused(phys_addr_t *p_base, phys_addr_t *p_top)
118 {
119 	*p_base = base;
120 	*p_top = top;
121 	if (base == top)
122 		return;
123 	spin_lock(&lock);
124 	regions[nr_regions].base = base;
125 	regions[nr_regions].size = top - base;
126 	++nr_regions;
127 	base = top;
128 	spin_unlock(&lock);
129 }
130 
131 static void *early_memalign(size_t alignment, size_t size)
132 {
133 	phys_addr_t addr;
134 
135 	assert(alignment && !(alignment & (alignment - 1)));
136 
137 	addr = phys_alloc_aligned_safe(size, alignment, true);
138 	if (addr == INVALID_PHYS_ADDR)
139 		return NULL;
140 
141 	return phys_to_virt(addr);
142 }
143