1 /*
2  * Copyright (c) 2023 Carlo Caione <ccaione@baylibre.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 #include <zephyr/mem_mgmt/mem_attr.h>
9 #include <zephyr/dt-bindings/memory-attr/memory-attr-arm.h>
10 
ZTEST(mem_attr,test_mem_attr)11 ZTEST(mem_attr, test_mem_attr)
12 {
13 	const struct mem_attr_region_t *region;
14 	size_t num_regions;
15 
16 	num_regions = mem_attr_get_regions(&region);
17 	zassert_equal(num_regions, 2, "No regions returned");
18 
19 	/*
20 	 * Check the data in the regions
21 	 */
22 	for (size_t idx = 0; idx < num_regions; idx++) {
23 		if (region[idx].dt_size == 0x1000) {
24 			zassert_equal(region[idx].dt_addr, 0x10000000, "Wrong region address");
25 			zassert_equal(region[idx].dt_size, 0x1000, "Wrong region size");
26 			zassert_equal(region[idx].dt_attr, DT_MEM_ARM_MPU_FLASH |
27 							   DT_MEM_NON_VOLATILE,
28 							   "Wrong region address");
29 			zassert_str_equal(region[idx].dt_name,
30 					  "memory@10000000", "Wrong name");
31 		} else {
32 			zassert_equal(region[idx].dt_addr, 0x20000000, "Wrong region address");
33 			zassert_equal(region[idx].dt_size, 0x2000, "Wrong region size");
34 			zassert_equal(region[idx].dt_attr, DT_MEM_ARM_MPU_RAM_NOCACHE,
35 							   "Wrong region address");
36 			zassert_str_equal(region[idx].dt_name,
37 					  "memory@20000000", "Wrong name");
38 		}
39 	}
40 
41 	/*
42 	 * Check the input sanitization
43 	 */
44 	zassert_equal(mem_attr_check_buf((void *) 0x10000000, 0x0, DT_MEM_NON_VOLATILE),
45 		      -ENOTSUP, "Unexpected return value");
46 
47 	/*
48 	 * Check a buffer with the correct properties
49 	 */
50 	zassert_equal(mem_attr_check_buf((void *) 0x10000100, 0x100,
51 					 DT_MEM_ARM_MPU_FLASH | DT_MEM_NON_VOLATILE),
52 		      0, "Unexpected return value");
53 	zassert_equal(mem_attr_check_buf((void *) 0x20000000, 0x2000, DT_MEM_ARM_MPU_RAM_NOCACHE),
54 		      0, "Unexpected return value");
55 
56 	/*
57 	 * Check partial attributes
58 	 */
59 	zassert_equal(mem_attr_check_buf((void *) 0x10000100, 0x100, DT_MEM_NON_VOLATILE),
60 		      0, "Unexpected return value");
61 
62 	/*
63 	 * Check a buffer with the wrong attribute
64 	 */
65 	zassert_equal(mem_attr_check_buf((void *) 0x20000000, 0x2000, DT_MEM_OOO),
66 		      -EINVAL, "Unexpected return value");
67 
68 	/*
69 	 * Check a buffer outsize the regions
70 	 */
71 	zassert_equal(mem_attr_check_buf((void *) 0x40000000, 0x1000, DT_MEM_NON_VOLATILE),
72 		      -ENOBUFS, "Unexpected return value");
73 
74 	/*
75 	 * Check a buffer too big for the region
76 	 */
77 	zassert_equal(mem_attr_check_buf((void *) 0x10000000, 0x2000, DT_MEM_NON_VOLATILE),
78 		      -ENOSPC, "Unexpected return value");
79 
80 	/*
81 	 * Check a buffer in a disabled region
82 	 */
83 	zassert_equal(mem_attr_check_buf((void *) 0x30000000, 0x1000, DT_MEM_OOO),
84 		      -ENOBUFS, "Unexpected return value");
85 }
86 
87 ZTEST_SUITE(mem_attr, NULL, NULL, NULL, NULL, NULL);
88