1 /*
2  * Copyright (c) 2014 Travis Geiselbrecht
3  *
4  * Use of this source code is governed by a MIT-style
5  * license that can be found in the LICENSE file or at
6  * https://opensource.org/licenses/MIT
7  */
8 #if ARM_WITH_CACHE
9 
10 #include <stdbool.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <arch.h>
15 #include <arch/ops.h>
16 #include <lk/console_cmd.h>
17 #include <platform.h>
18 
bench_cache(size_t bufsize,uint8_t * buf)19 static void bench_cache(size_t bufsize, uint8_t *buf) {
20     lk_bigtime_t t;
21     bool do_free;
22 
23     if (buf == 0) {
24         buf = memalign(PAGE_SIZE, bufsize);
25         do_free = true;
26     } else {
27         do_free = false;
28     }
29 
30     printf("buf %p, size %zu\n", buf, bufsize);
31 
32     if (!buf)
33         return;
34 
35     t = current_time_hires();
36     arch_clean_cache_range((addr_t)buf, bufsize);
37     t = current_time_hires() - t;
38 
39     printf("took %llu usecs to clean %d bytes (cold)\n", t, bufsize);
40 
41     memset(buf, 0x99, bufsize);
42 
43     t = current_time_hires();
44     arch_clean_cache_range((addr_t)buf, bufsize);
45     t = current_time_hires() - t;
46 
47     if (do_free)
48         free(buf);
49 
50     printf("took %llu usecs to clean %d bytes (hot)\n", t, bufsize);
51 }
52 
cache_tests(int argc,const console_cmd_args * argv)53 static int cache_tests(int argc, const console_cmd_args *argv) {
54     uint8_t *buf;
55     buf = (uint8_t *)((argc > 1) ? argv[1].u : 0UL);
56 
57     printf("testing cache\n");
58 
59     bench_cache(2*1024, buf);
60     bench_cache(64*1024, buf);
61     bench_cache(256*1024, buf);
62     bench_cache(1*1024*1024, buf);
63     bench_cache(8*1024*1024, buf);
64     return 0;
65 }
66 
67 STATIC_COMMAND_START
68 STATIC_COMMAND("cache_tests", "test/bench the cpu cache", &cache_tests)
69 STATIC_COMMAND_END(cache_tests);
70 
71 #endif
72