1 // Copyright 2018-2019 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "xtensa_perfmon_access.h"
16 #include "xtensa-debug-module.h"
17 #include "eri.h"
18 
19 
xtensa_perfmon_init(int id,uint16_t select,uint16_t mask,int kernelcnt,int tracelevel)20 esp_err_t xtensa_perfmon_init(int id, uint16_t select, uint16_t mask, int kernelcnt, int tracelevel)
21 {
22     if (id >= ERI_PERFMON_MAX) {
23         return ESP_ERR_INVALID_ARG;
24     }
25     uint32_t pmc =
26         ((tracelevel & PMCTRL_TRACELEVEL_MASK) << PMCTRL_TRACELEVEL_SHIFT) |
27         ((select & PMCTRL_SELECT_MASK) << PMCTRL_SELECT_SHIFT) |
28         ((mask & PMCTRL_MASK_MASK) << PMCTRL_MASK_SHIFT) |
29         ((kernelcnt & 1) << PMCTRL_KRNLCNT_SHIFT);
30 
31     eri_write(ERI_PERFMON_PM0 + id * sizeof(int32_t), 0);
32     eri_write(ERI_PERFMON_PMCTRL0 + id * sizeof(int32_t), pmc);
33     return ESP_OK;
34 }
35 
xtensa_perfmon_dump(void)36 void xtensa_perfmon_dump(void)
37 {
38     uint32_t pgm = eri_read(ERI_PERFMON_PGM);
39     uint32_t intpc = eri_read(ERI_PERFMON_INTPC);
40     printf("perfmon: PGM=%08x, INTPC=%08x\n", pgm, intpc);
41 
42     for (int i = 0 ; i < ERI_PERFMON_MAX ; i++) {
43         uint32_t pm = eri_read(ERI_PERFMON_PM0 + i * sizeof(int32_t));
44         uint32_t pmctrl = eri_read(ERI_PERFMON_PMCTRL0 + i * sizeof(int32_t));
45         uint32_t pmstat = eri_read(ERI_PERFMON_PMSTAT0 + i * sizeof(int32_t));
46         printf("perfmon: pm%i=%08x, pmctrl%i=%08x, pmstat%i=%08x\n", i, pm, i, pmctrl, i, pmstat);
47     }
48 }
49 
xtensa_perfmon_reset(int id)50 esp_err_t xtensa_perfmon_reset(int id)
51 {
52     if (id >= ERI_PERFMON_MAX) {
53         return ESP_ERR_INVALID_ARG;
54     }
55     eri_write(ERI_PERFMON_PM0 + id * sizeof(int32_t), 0);
56     return ESP_OK;
57 }
58 
xtensa_perfmon_start(void)59 void xtensa_perfmon_start(void)
60 {
61     eri_write(ERI_PERFMON_PGM, PGM_PMEN);
62 }
63 
xtensa_perfmon_stop(void)64 void xtensa_perfmon_stop(void)
65 {
66     eri_write(ERI_PERFMON_PGM, 0);
67 }
68 
xtensa_perfmon_value(int id)69 uint32_t xtensa_perfmon_value(int id)
70 {
71     if (id >= ERI_PERFMON_MAX) {
72         return 0;
73     }
74     uint32_t result = eri_read(ERI_PERFMON_PM0 + id * sizeof(int32_t));
75     return result;
76 }
77 
xtensa_perfmon_overflow(int id)78 esp_err_t xtensa_perfmon_overflow(int id)
79 {
80     if (id >= ERI_PERFMON_MAX) {
81         return ESP_ERR_INVALID_ARG;
82     }
83     uint32_t result = eri_read(ERI_PERFMON_PMSTAT0 + id * sizeof(int32_t));
84     if (result & 1) {
85         return ESP_FAIL;
86     }
87     return ESP_OK;
88 }
89