Motr  M0
aarch64_cycle_counter.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2021 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 #ifdef CONFIG_AARCH64
22 
23 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
24 
25 
26 #include "lib/trace.h"
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/version.h>
30 
35 static void enable_user_space_access_pccnt(void *dt)
36 {
37  uint64_t pmcr_val;
38 
39  /* Enables the cycle counter register */
40  asm volatile("msr pmcntenset_el0, %0" :: "r" ((u64)(1 << 31)));
41 
42  /* Performance Monitors User Enable Register. */
43  asm volatile("msr pmuserenr_el0, %0" : : "r"(1 |((u64)(1 << 2))));
44 
45  /* Reset cycle counter(0 bit) and start all event(2nd bit) counters */
46  asm volatile("mrs %0, pmcr_el0" : "=r" (pmcr_val));
47  asm volatile("msr pmcr_el0, %0" : : "r" (pmcr_val | 1 |((u64)(1 << 2))));
48 }
49 
50 static void disable_user_space_pccnt_access(void *dt)
51 {
52  /* Enables the cycle counter register */
53  asm volatile("msr pmcntenset_el0, %0" :: "r" (0 << 31));
54 
55  /* Disable user-mode access to counters. */
56  asm volatile("msr pmuserenr_el0, %0" : : "r"((u64)0));
57 }
58 
59 int start_cycle_counter(void)
60 {
61  /* Call the function on each cpu with NULL param and wait for completion*/
62  on_each_cpu(enable_user_space_access_pccnt, NULL, 1);
63  return 0;
64 }
65 
66 void finish_cycle_counter(void)
67 {
68  /* Call the function on each cpu with NULL param and wait for completion*/
69  on_each_cpu(disable_user_space_pccnt_access, NULL, 1);
70 }
71 #endif /* CONFIG_AARCH64 */
72 
73 #undef M0_TRACE_SUBSYSTEM
74 
75 /*
76  * Local variables:
77  * c-indentation-style: "K&R"
78  * c-basic-offset: 8
79  * tab-width: 8
80  * fill-column: 80
81  * scroll-step: 1
82  * End:
83  */
84 /*
85  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
86  */
#define NULL
Definition: misc.h:38
int start_cycle_counter(void)
void finish_cycle_counter(void)