1 /*
2  * Copyright (c) 2006-2023, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2022-12-14     WangXiaoyao  the first version
9  * 2023-03-20     WangXiaoyao  Format & add more testcases for API under mm_aspace.h
10  */
11 #include "common.h"
12 
13 /**
14  * @brief Testing all APIs under components/mm
15  */
16 
17 void ioremap_tc(void);
18 void flag_tc(void);
19 
20 #ifdef STANDALONE_TC
21 #define TC_ASSERT(expr)                                                        \
22     ((expr)                                                                    \
23          ? 0                                                                   \
24          : rt_kprintf("AssertFault(%d): %s\n", __LINE__, RT_STRINGIFY(expr)))
25 #else
26 #define TC_ASSERT(expr) uassert_true(expr)
27 #endif
28 
utest_tc_init(void)29 static rt_err_t utest_tc_init(void)
30 {
31     return RT_EOK;
32 }
33 
utest_tc_cleanup(void)34 static rt_err_t utest_tc_cleanup(void)
35 {
36     return RT_EOK;
37 }
38 
39 #include "test_aspace_api.h"
40 
testcase(void)41 static void testcase(void)
42 {
43     aspace_tc();
44     ioremap_tc();
45     flag_tc();
46 }
47 UTEST_TC_EXPORT(testcase, "testcases.mm.api_tc", utest_tc_init, utest_tc_cleanup, 20);
48 
ioremap_tc(void)49 void ioremap_tc(void)
50 {
51     const size_t bufsz = 0x1000;
52     void *paddr = (void *)rt_pages_alloc(rt_page_bits(bufsz)) + PV_OFFSET;
53     int *vaddr;
54     vaddr = rt_ioremap_cached(paddr, bufsz);
55     if (vaddr)
56     {
57         TC_ASSERT(*vaddr == *(int *)(paddr - PV_OFFSET));
58 
59         rt_iounmap(vaddr);
60         rt_pages_free(paddr - PV_OFFSET, 0);
61     }
62 }
63 
flag_tc(void)64 void flag_tc(void)
65 {
66     size_t flags;
67 
68     flags = MMF_CREATE(MMF_MAP_FIXED, 0x4000);
69     TC_ASSERT(MMF_GET_CNTL(flags) == (MMF_MAP_FIXED | MMF_REQUEST_ALIGN));
70     TC_ASSERT((1 << MMF_GET_ALIGN(flags)) == 0x4000);
71 
72     flags = MMF_CREATE(MMF_MAP_FIXED, 0);
73     TC_ASSERT(MMF_GET_CNTL(flags) == MMF_MAP_FIXED);
74     TC_ASSERT(MMF_GET_ALIGN(flags) == 0);
75 }
76 
77 #if 0
78 
79 #define BUF_SIZE (4ul << 20)
80 static char ALIGN(BUF_SIZE) buf[BUF_SIZE];
81 
82 void buddy_tc(void)
83 {
84     size_t total, free;
85     rt_page_get_info(&total, &free);
86 
87     rt_region_t region = {
88         .start = (size_t)buf,
89         .end = (size_t)buf + BUF_SIZE,
90     };
91 
92     size_t new_total, new_free;
93     rt_page_install(region);
94     rt_page_get_info(&new_total, &new_free);
95     TC_ASSERT(new_total - total == (BUF_SIZE >> ARCH_PAGE_SHIFT));
96     TC_ASSERT(new_free > free);
97 }
98 
99 void mmu_page_tc()
100 {
101     mm_aspace_t aspace = ASPACE_NEW();
102     size_t total, free;
103     rt_page_get_info(&total, &free);
104     rt_hw_mmu_map(aspace, (void *)0x3fffffffff, 0, ARCH_PAGE_SIZE,
105                   MMU_MAP_K_RWCB);
106     rt_hw_mmu_unmap(aspace, (void *)0x3fffffffff, ARCH_PAGE_SIZE);
107 
108     size_t new_total, new_free;
109     rt_page_get_info(&new_total, &new_free);
110     TC_ASSERT(new_free == free);
111     mm_aspace_delete(aspace);
112 }
113 #endif
114