101e04c3fSmrg/* 201e04c3fSmrg * Copyright © 2018 Intel Corporation 301e04c3fSmrg * 401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a 501e04c3fSmrg * copy of this software and associated documentation files (the "Software"), 601e04c3fSmrg * to deal in the Software without restriction, including without limitation 701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the 901e04c3fSmrg * Software is furnished to do so, subject to the following conditions: 1001e04c3fSmrg * 1101e04c3fSmrg * The above copyright notice and this permission notice (including the next 1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the 1301e04c3fSmrg * Software. 1401e04c3fSmrg * 1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 2101e04c3fSmrg * IN THE SOFTWARE. 2201e04c3fSmrg */ 2301e04c3fSmrg 2401e04c3fSmrg#include <stdlib.h> 257ec681f3Smrg#include <inttypes.h> 2601e04c3fSmrg 2701e04c3fSmrg#include "util/u_math.h" 2801e04c3fSmrg#include "util/vma.h" 2901e04c3fSmrg 3001e04c3fSmrgstruct util_vma_hole { 3101e04c3fSmrg struct list_head link; 3201e04c3fSmrg uint64_t offset; 3301e04c3fSmrg uint64_t size; 3401e04c3fSmrg}; 3501e04c3fSmrg 3601e04c3fSmrg#define util_vma_foreach_hole(_hole, _heap) \ 3701e04c3fSmrg list_for_each_entry(struct util_vma_hole, _hole, &(_heap)->holes, link) 3801e04c3fSmrg 3901e04c3fSmrg#define util_vma_foreach_hole_safe(_hole, _heap) \ 4001e04c3fSmrg list_for_each_entry_safe(struct util_vma_hole, _hole, &(_heap)->holes, link) 4101e04c3fSmrg 427ec681f3Smrg#define util_vma_foreach_hole_safe_rev(_hole, _heap) \ 437ec681f3Smrg list_for_each_entry_safe_rev(struct util_vma_hole, _hole, &(_heap)->holes, link) 447ec681f3Smrg 4501e04c3fSmrgvoid 4601e04c3fSmrgutil_vma_heap_init(struct util_vma_heap *heap, 4701e04c3fSmrg uint64_t start, uint64_t size) 4801e04c3fSmrg{ 4901e04c3fSmrg list_inithead(&heap->holes); 5001e04c3fSmrg util_vma_heap_free(heap, start, size); 517ec681f3Smrg 527ec681f3Smrg /* Default to using high addresses */ 537ec681f3Smrg heap->alloc_high = true; 5401e04c3fSmrg} 5501e04c3fSmrg 5601e04c3fSmrgvoid 5701e04c3fSmrgutil_vma_heap_finish(struct util_vma_heap *heap) 5801e04c3fSmrg{ 5901e04c3fSmrg util_vma_foreach_hole_safe(hole, heap) 6001e04c3fSmrg free(hole); 6101e04c3fSmrg} 6201e04c3fSmrg 6301e04c3fSmrg#ifndef NDEBUG 6401e04c3fSmrgstatic void 6501e04c3fSmrgutil_vma_heap_validate(struct util_vma_heap *heap) 6601e04c3fSmrg{ 6701e04c3fSmrg uint64_t prev_offset = 0; 6801e04c3fSmrg util_vma_foreach_hole(hole, heap) { 6901e04c3fSmrg assert(hole->offset > 0); 7001e04c3fSmrg assert(hole->size > 0); 7101e04c3fSmrg 7201e04c3fSmrg if (&hole->link == heap->holes.next) { 7301e04c3fSmrg /* This must be the top-most hole. Assert that, if it overflows, it 7401e04c3fSmrg * overflows to 0, i.e. 2^64. 7501e04c3fSmrg */ 7601e04c3fSmrg assert(hole->size + hole->offset == 0 || 7701e04c3fSmrg hole->size + hole->offset > hole->offset); 7801e04c3fSmrg } else { 7901e04c3fSmrg /* This is not the top-most hole so it must not overflow and, in 8001e04c3fSmrg * fact, must be strictly lower than the top-most hole. If 8101e04c3fSmrg * hole->size + hole->offset == prev_offset, then we failed to join 8201e04c3fSmrg * holes during a util_vma_heap_free. 8301e04c3fSmrg */ 8401e04c3fSmrg assert(hole->size + hole->offset > hole->offset && 8501e04c3fSmrg hole->size + hole->offset < prev_offset); 8601e04c3fSmrg } 8701e04c3fSmrg prev_offset = hole->offset; 8801e04c3fSmrg } 8901e04c3fSmrg} 9001e04c3fSmrg#else 9101e04c3fSmrg#define util_vma_heap_validate(heap) 9201e04c3fSmrg#endif 9301e04c3fSmrg 947ec681f3Smrgstatic void 957ec681f3Smrgutil_vma_hole_alloc(struct util_vma_hole *hole, 967ec681f3Smrg uint64_t offset, uint64_t size) 977ec681f3Smrg{ 987ec681f3Smrg assert(hole->offset <= offset); 997ec681f3Smrg assert(hole->size >= offset - hole->offset + size); 1007ec681f3Smrg 1017ec681f3Smrg if (offset == hole->offset && size == hole->size) { 1027ec681f3Smrg /* Just get rid of the hole. */ 1037ec681f3Smrg list_del(&hole->link); 1047ec681f3Smrg free(hole); 1057ec681f3Smrg return; 1067ec681f3Smrg } 1077ec681f3Smrg 1087ec681f3Smrg assert(offset - hole->offset <= hole->size - size); 1097ec681f3Smrg uint64_t waste = (hole->size - size) - (offset - hole->offset); 1107ec681f3Smrg if (waste == 0) { 1117ec681f3Smrg /* We allocated at the top. Shrink the hole down. */ 1127ec681f3Smrg hole->size -= size; 1137ec681f3Smrg return; 1147ec681f3Smrg } 1157ec681f3Smrg 1167ec681f3Smrg if (offset == hole->offset) { 1177ec681f3Smrg /* We allocated at the bottom. Shrink the hole up. */ 1187ec681f3Smrg hole->offset += size; 1197ec681f3Smrg hole->size -= size; 1207ec681f3Smrg return; 1217ec681f3Smrg } 1227ec681f3Smrg 1237ec681f3Smrg /* We allocated in the middle. We need to split the old hole into two 1247ec681f3Smrg * holes, one high and one low. 1257ec681f3Smrg */ 1267ec681f3Smrg struct util_vma_hole *high_hole = calloc(1, sizeof(*hole)); 1277ec681f3Smrg high_hole->offset = offset + size; 1287ec681f3Smrg high_hole->size = waste; 1297ec681f3Smrg 1307ec681f3Smrg /* Adjust the hole to be the amount of space left at he bottom of the 1317ec681f3Smrg * original hole. 1327ec681f3Smrg */ 1337ec681f3Smrg hole->size = offset - hole->offset; 1347ec681f3Smrg 1357ec681f3Smrg /* Place the new hole before the old hole so that the list is in order 1367ec681f3Smrg * from high to low. 1377ec681f3Smrg */ 1387ec681f3Smrg list_addtail(&high_hole->link, &hole->link); 1397ec681f3Smrg} 1407ec681f3Smrg 14101e04c3fSmrguint64_t 14201e04c3fSmrgutil_vma_heap_alloc(struct util_vma_heap *heap, 14301e04c3fSmrg uint64_t size, uint64_t alignment) 14401e04c3fSmrg{ 14501e04c3fSmrg /* The caller is expected to reject zero-size allocations */ 14601e04c3fSmrg assert(size > 0); 14701e04c3fSmrg assert(alignment > 0); 14801e04c3fSmrg 14901e04c3fSmrg util_vma_heap_validate(heap); 15001e04c3fSmrg 1517ec681f3Smrg if (heap->alloc_high) { 1527ec681f3Smrg util_vma_foreach_hole_safe(hole, heap) { 1537ec681f3Smrg if (size > hole->size) 1547ec681f3Smrg continue; 15501e04c3fSmrg 1567ec681f3Smrg /* Compute the offset as the highest address where a chunk of the 1577ec681f3Smrg * given size can be without going over the top of the hole. 1587ec681f3Smrg * 1597ec681f3Smrg * This calculation is known to not overflow because we know that 1607ec681f3Smrg * hole->size + hole->offset can only overflow to 0 and size > 0. 1617ec681f3Smrg */ 1627ec681f3Smrg uint64_t offset = (hole->size - size) + hole->offset; 16301e04c3fSmrg 1647ec681f3Smrg /* Align the offset. We align down and not up because we are 1657ec681f3Smrg * allocating from the top of the hole and not the bottom. 1667ec681f3Smrg */ 1677ec681f3Smrg offset = (offset / alignment) * alignment; 16801e04c3fSmrg 1697ec681f3Smrg if (offset < hole->offset) 1707ec681f3Smrg continue; 17101e04c3fSmrg 1727ec681f3Smrg util_vma_hole_alloc(hole, offset, size); 17301e04c3fSmrg util_vma_heap_validate(heap); 17401e04c3fSmrg return offset; 17501e04c3fSmrg } 1767ec681f3Smrg } else { 1777ec681f3Smrg util_vma_foreach_hole_safe_rev(hole, heap) { 1787ec681f3Smrg if (size > hole->size) 1797ec681f3Smrg continue; 18001e04c3fSmrg 1817ec681f3Smrg uint64_t offset = hole->offset; 1827ec681f3Smrg 1837ec681f3Smrg /* Align the offset */ 1847ec681f3Smrg uint64_t misalign = offset % alignment; 1857ec681f3Smrg if (misalign) { 1867ec681f3Smrg uint64_t pad = alignment - misalign; 1877ec681f3Smrg if (pad > hole->size - size) 1887ec681f3Smrg continue; 18901e04c3fSmrg 1907ec681f3Smrg offset += pad; 1917ec681f3Smrg } 1927ec681f3Smrg 1937ec681f3Smrg util_vma_hole_alloc(hole, offset, size); 19401e04c3fSmrg util_vma_heap_validate(heap); 19501e04c3fSmrg return offset; 19601e04c3fSmrg } 1977ec681f3Smrg } 19801e04c3fSmrg 1997ec681f3Smrg /* Failed to allocate */ 2007ec681f3Smrg return 0; 2017ec681f3Smrg} 20201e04c3fSmrg 2037ec681f3Smrgbool 2047ec681f3Smrgutil_vma_heap_alloc_addr(struct util_vma_heap *heap, 2057ec681f3Smrg uint64_t offset, uint64_t size) 2067ec681f3Smrg{ 2077ec681f3Smrg /* An offset of 0 is reserved for allocation failure. It is not a valid 2087ec681f3Smrg * address and cannot be allocated. 2097ec681f3Smrg */ 2107ec681f3Smrg assert(offset > 0); 21101e04c3fSmrg 2127ec681f3Smrg /* Allocating something with a size of 0 is also not valid. */ 2137ec681f3Smrg assert(size > 0); 21401e04c3fSmrg 2157ec681f3Smrg /* It's possible for offset + size to wrap around if we touch the top of 2167ec681f3Smrg * the 64-bit address space, but we cannot go any higher than 2^64. 2177ec681f3Smrg */ 2187ec681f3Smrg assert(offset + size == 0 || offset + size > offset); 2197ec681f3Smrg 2207ec681f3Smrg /* Find the hole if one exists. */ 2217ec681f3Smrg util_vma_foreach_hole_safe(hole, heap) { 2227ec681f3Smrg if (hole->offset > offset) 2237ec681f3Smrg continue; 22401e04c3fSmrg 2257ec681f3Smrg /* Holes are ordered high-to-low so the first hole we find with 2267ec681f3Smrg * hole->offset <= is our hole. If it's not big enough to contain the 2277ec681f3Smrg * requested range, then the allocation fails. 2287ec681f3Smrg */ 2297ec681f3Smrg assert(hole->offset <= offset); 2307ec681f3Smrg if (hole->size < offset - hole->offset + size) 2317ec681f3Smrg return false; 2327ec681f3Smrg 2337ec681f3Smrg util_vma_hole_alloc(hole, offset, size); 2347ec681f3Smrg return true; 23501e04c3fSmrg } 23601e04c3fSmrg 2377ec681f3Smrg /* We didn't find a suitable hole */ 2387ec681f3Smrg return false; 23901e04c3fSmrg} 24001e04c3fSmrg 24101e04c3fSmrgvoid 24201e04c3fSmrgutil_vma_heap_free(struct util_vma_heap *heap, 24301e04c3fSmrg uint64_t offset, uint64_t size) 24401e04c3fSmrg{ 24501e04c3fSmrg /* An offset of 0 is reserved for allocation failure. It is not a valid 24601e04c3fSmrg * address and cannot be freed. 24701e04c3fSmrg */ 24801e04c3fSmrg assert(offset > 0); 24901e04c3fSmrg 25001e04c3fSmrg /* Freeing something with a size of 0 is also not valid. */ 25101e04c3fSmrg assert(size > 0); 25201e04c3fSmrg 25301e04c3fSmrg /* It's possible for offset + size to wrap around if we touch the top of 25401e04c3fSmrg * the 64-bit address space, but we cannot go any higher than 2^64. 25501e04c3fSmrg */ 25601e04c3fSmrg assert(offset + size == 0 || offset + size > offset); 25701e04c3fSmrg 25801e04c3fSmrg util_vma_heap_validate(heap); 25901e04c3fSmrg 26001e04c3fSmrg /* Find immediately higher and lower holes if they exist. */ 26101e04c3fSmrg struct util_vma_hole *high_hole = NULL, *low_hole = NULL; 26201e04c3fSmrg util_vma_foreach_hole(hole, heap) { 26301e04c3fSmrg if (hole->offset <= offset) { 26401e04c3fSmrg low_hole = hole; 26501e04c3fSmrg break; 26601e04c3fSmrg } 26701e04c3fSmrg high_hole = hole; 26801e04c3fSmrg } 26901e04c3fSmrg 27001e04c3fSmrg if (high_hole) 27101e04c3fSmrg assert(offset + size <= high_hole->offset); 27201e04c3fSmrg bool high_adjacent = high_hole && offset + size == high_hole->offset; 27301e04c3fSmrg 27401e04c3fSmrg if (low_hole) { 27501e04c3fSmrg assert(low_hole->offset + low_hole->size > low_hole->offset); 27601e04c3fSmrg assert(low_hole->offset + low_hole->size <= offset); 27701e04c3fSmrg } 27801e04c3fSmrg bool low_adjacent = low_hole && low_hole->offset + low_hole->size == offset; 27901e04c3fSmrg 28001e04c3fSmrg if (low_adjacent && high_adjacent) { 28101e04c3fSmrg /* Merge the two holes */ 28201e04c3fSmrg low_hole->size += size + high_hole->size; 28301e04c3fSmrg list_del(&high_hole->link); 28401e04c3fSmrg free(high_hole); 28501e04c3fSmrg } else if (low_adjacent) { 28601e04c3fSmrg /* Merge into the low hole */ 28701e04c3fSmrg low_hole->size += size; 28801e04c3fSmrg } else if (high_adjacent) { 28901e04c3fSmrg /* Merge into the high hole */ 29001e04c3fSmrg high_hole->offset = offset; 29101e04c3fSmrg high_hole->size += size; 29201e04c3fSmrg } else { 29301e04c3fSmrg /* Neither hole is adjacent; make a new one */ 29401e04c3fSmrg struct util_vma_hole *hole = calloc(1, sizeof(*hole)); 29501e04c3fSmrg 29601e04c3fSmrg hole->offset = offset; 29701e04c3fSmrg hole->size = size; 29801e04c3fSmrg 29901e04c3fSmrg /* Add it after the high hole so we maintain high-to-low ordering */ 30001e04c3fSmrg if (high_hole) 30101e04c3fSmrg list_add(&hole->link, &high_hole->link); 30201e04c3fSmrg else 30301e04c3fSmrg list_add(&hole->link, &heap->holes); 30401e04c3fSmrg } 30501e04c3fSmrg 30601e04c3fSmrg util_vma_heap_validate(heap); 30701e04c3fSmrg} 3087ec681f3Smrg 3097ec681f3Smrgvoid 3107ec681f3Smrgutil_vma_heap_print(struct util_vma_heap *heap, FILE *fp, 3117ec681f3Smrg const char *tab, uint64_t total_size) 3127ec681f3Smrg{ 3137ec681f3Smrg fprintf(fp, "%sutil_vma_heap:\n", tab); 3147ec681f3Smrg 3157ec681f3Smrg uint64_t total_free = 0; 3167ec681f3Smrg util_vma_foreach_hole(hole, heap) { 3177ec681f3Smrg fprintf(fp, "%s hole: offset = %"PRIu64" (0x%"PRIx64", " 3187ec681f3Smrg "size = %"PRIu64" (0x%"PRIx64")\n", 3197ec681f3Smrg tab, hole->offset, hole->offset, hole->size, hole->size); 3207ec681f3Smrg total_free += hole->size; 3217ec681f3Smrg } 3227ec681f3Smrg assert(total_free <= total_size); 3237ec681f3Smrg fprintf(fp, "%s%"PRIu64"B (0x%"PRIx64") free (%.2f%% full)\n", 3247ec681f3Smrg tab, total_free, total_free, 3257ec681f3Smrg ((double)(total_size - total_free) / (double)total_size) * 100); 3267ec681f3Smrg} 327