1 /*
2  * Copyright (c) 2014 Travis Geiselbrecht
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 #if ARM_WITH_CACHE
24 
25 #include <stdbool.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <arch.h>
30 #include <arch/ops.h>
31 #include <lib/console.h>
32 #include <platform.h>
33 
bench_cache(size_t bufsize,uint8_t * buf)34 static void bench_cache(size_t bufsize, uint8_t *buf)
35 {
36     lk_bigtime_t t;
37     bool do_free;
38 
39     if (buf == 0) {
40         buf = memalign(PAGE_SIZE, bufsize);
41         do_free = true;
42     } else {
43         do_free = false;
44     }
45 
46     printf("buf %p, size %zu\n", buf, bufsize);
47 
48     if (!buf)
49         return;
50 
51     t = current_time_hires();
52     arch_clean_cache_range((addr_t)buf, bufsize);
53     t = current_time_hires() - t;
54 
55     printf("took %llu usecs to clean %d bytes (cold)\n", t, bufsize);
56 
57     memset(buf, 0x99, bufsize);
58 
59     t = current_time_hires();
60     arch_clean_cache_range((addr_t)buf, bufsize);
61     t = current_time_hires() - t;
62 
63     if (do_free)
64         free(buf);
65 
66     printf("took %llu usecs to clean %d bytes (hot)\n", t, bufsize);
67 }
68 
cache_tests(int argc,const cmd_args * argv)69 static int cache_tests(int argc, const cmd_args *argv)
70 {
71     uint8_t *buf;
72     buf = (uint8_t *)((argc > 1) ? argv[1].u : 0UL);
73 
74     printf("testing cache\n");
75 
76     bench_cache(2*1024, buf);
77     bench_cache(64*1024, buf);
78     bench_cache(256*1024, buf);
79     bench_cache(1*1024*1024, buf);
80     bench_cache(8*1024*1024, buf);
81     return 0;
82 }
83 
84 STATIC_COMMAND_START
85 STATIC_COMMAND("cache_tests", "test/bench the cpu cache", &cache_tests)
86 STATIC_COMMAND_END(cache_tests);
87 
88 #endif
89