|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +/* |
| 3 | + * Copyright(c) 2023 Intel Corporation. All rights reserved. |
| 4 | + * |
| 5 | + * Author: Guennadi Liakhovetski <guennadi.liakhovetski@linux.intel.com> |
| 6 | + */ |
| 7 | + |
| 8 | +#include <errno.h> |
| 9 | +#include <stdbool.h> |
| 10 | +#include <stdlib.h> |
| 11 | + |
| 12 | +#include <sof/boot_test.h> |
| 13 | +#include <sof/lib/vregion.h> |
| 14 | + |
| 15 | +#include <zephyr/logging/log.h> |
| 16 | +#include <zephyr/ztest.h> |
| 17 | + |
| 18 | +LOG_MODULE_DECLARE(sof_boot_test, CONFIG_SOF_LOG_LEVEL); |
| 19 | + |
| 20 | +static struct vregion *test_vreg_create(void) |
| 21 | +{ |
| 22 | + struct vregion *vreg = vregion_create(CONFIG_MM_DRV_PAGE_SIZE - 100, |
| 23 | + CONFIG_MM_DRV_PAGE_SIZE); |
| 24 | + |
| 25 | + zassert_not_null(vreg); |
| 26 | + |
| 27 | + return vreg; |
| 28 | +} |
| 29 | + |
| 30 | +static void test_vreg_alloc_lifet(struct vregion *vreg) |
| 31 | +{ |
| 32 | + void *ptr = vregion_alloc(vreg, VREGION_MEM_TYPE_LIFETIME, 2000); |
| 33 | + |
| 34 | + zassert_not_null(ptr); |
| 35 | + |
| 36 | + void *ptr_align = vregion_alloc_align(vreg, VREGION_MEM_TYPE_LIFETIME, 2000, 16); |
| 37 | + |
| 38 | + zassert_not_null(ptr_align); |
| 39 | + zassert_equal((uintptr_t)ptr_align & 15, 0); |
| 40 | + |
| 41 | + void *ptr_nomem = vregion_alloc(vreg, VREGION_MEM_TYPE_LIFETIME, 2000); |
| 42 | + |
| 43 | + zassert_is_null(ptr_nomem); |
| 44 | + |
| 45 | + vregion_free(vreg, ptr_align); |
| 46 | + vregion_free(vreg, ptr); |
| 47 | + |
| 48 | + /* Freeing isn't possible with LIFETIME */ |
| 49 | + ptr_nomem = vregion_alloc(vreg, VREGION_MEM_TYPE_LIFETIME, 2000); |
| 50 | + |
| 51 | + zassert_is_null(ptr_nomem); |
| 52 | +} |
| 53 | + |
| 54 | +static void test_vreg_alloc_tmp(struct vregion *vreg) |
| 55 | +{ |
| 56 | + void *ptr = vregion_alloc(vreg, VREGION_MEM_TYPE_INTERIM, 20); |
| 57 | + |
| 58 | + zassert_not_null(ptr); |
| 59 | + |
| 60 | + void *ptr_align = vregion_alloc_align(vreg, VREGION_MEM_TYPE_INTERIM, 2000, 16); |
| 61 | + |
| 62 | + zassert_not_null(ptr_align); |
| 63 | + zassert_equal((uintptr_t)ptr_align & 15, 0); |
| 64 | + |
| 65 | + void *ptr_nomem = vregion_alloc(vreg, VREGION_MEM_TYPE_INTERIM, 2000); |
| 66 | + |
| 67 | + zassert_is_null(ptr_nomem); |
| 68 | + |
| 69 | + vregion_free(vreg, ptr_align); |
| 70 | + vregion_free(vreg, ptr); |
| 71 | + |
| 72 | + /* Should be possible to allocate again */ |
| 73 | + ptr = vregion_alloc(vreg, VREGION_MEM_TYPE_INTERIM, 2000); |
| 74 | + |
| 75 | + zassert_not_null(ptr); |
| 76 | +} |
| 77 | + |
| 78 | +static void test_vreg_destroy(struct vregion *vreg) |
| 79 | +{ |
| 80 | + vregion_info(vreg); |
| 81 | + vregion_destroy(vreg); |
| 82 | +} |
| 83 | + |
| 84 | +ZTEST(sof_boot, vregion) |
| 85 | +{ |
| 86 | + struct vregion *vreg = test_vreg_create(); |
| 87 | + |
| 88 | + /* Test interim allocations */ |
| 89 | + test_vreg_alloc_tmp(vreg); |
| 90 | + /* Test lifetime allocations */ |
| 91 | + test_vreg_alloc_lifet(vreg); |
| 92 | + |
| 93 | + test_vreg_destroy(vreg); |
| 94 | +} |
0 commit comments