aboutsummaryrefslogtreecommitdiff
path: root/src/ethosu_pmu.c
blob: e11636a4d978da3463b74573a71d191026df3037 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (c) 2019-2021 Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the License); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*****************************************************************************
 * Includes
 *****************************************************************************/

#include "ethosu_device.h"
#include "ethosu_driver.h"
#include "ethosu_interface.h"
#include "ethosu_log.h"
#include "pmu_ethosu.h"

#include <assert.h>
#include <inttypes.h>
#include <stddef.h>

/*****************************************************************************
 * Defines
 *****************************************************************************/

#define MASK_0_31_BITS (0xFFFFFFFF)
#define MASK_32_47_BITS (0xFFFF00000000)

#define COMMA ,
#define SEMICOLON ;

#define EVTYPE(A, name)                                                                                                \
    case PMU_EVENT_##name:                                                                                             \
        return ETHOSU_PMU_##name

#define EVID(A, name) (PMU_EVENT_##name)

/*****************************************************************************
 * Variables
 *****************************************************************************/

static const enum pmu_event eventbyid[] = {EXPAND_PMU_EVENT(EVID, COMMA)};

/*****************************************************************************
 * Static functions
 *****************************************************************************/

static enum ethosu_pmu_event_type pmu_event_type(uint32_t id)
{
    switch (id)
    {
        EXPAND_PMU_EVENT(EVTYPE, SEMICOLON);
    default:
        LOG_ERR("Unknown PMU event id: 0x%" PRIx32, id);
    }

    return ETHOSU_PMU_SENTINEL;
}

static uint32_t pmu_event_value(enum ethosu_pmu_event_type event)
{
    int a = event;
    if ((a < ETHOSU_PMU_SENTINEL) && (a >= ETHOSU_PMU_NO_EVENT))
    {
        return eventbyid[event];
    }
    else
    {
        return (uint32_t)(-1);
    }
}

/*****************************************************************************
 * Functions
 *****************************************************************************/

void ETHOSU_PMU_Enable(struct ethosu_driver *drv)
{
    LOG_DEBUG("Enable PMU");
    struct pmcr_r pmcr = {0};
    pmcr.cnt_en        = 1;
    set_clock_and_power_request(drv, ETHOSU_PMU_REQUEST, ETHOSU_CLOCK_Q_DISABLE, ETHOSU_POWER_Q_DISABLE);
    drv->dev->reg->PMCR.word = pmcr.word;
}

void ETHOSU_PMU_Disable(struct ethosu_driver *drv)
{
    LOG_DEBUG("Disable PMU");
    drv->dev->reg->PMCR.word = 0;
    set_clock_and_power_request(drv, ETHOSU_PMU_REQUEST, ETHOSU_CLOCK_Q_ENABLE, ETHOSU_POWER_Q_ENABLE);
}

uint32_t ETHOSU_PMU_Get_NumEventCounters(void)
{
    return NPU_REG_PMEVCNTR_ARRLEN;
}

void ETHOSU_PMU_Set_EVTYPER(struct ethosu_driver *drv, uint32_t num, enum ethosu_pmu_event_type type)
{
    assert(num < ETHOSU_PMU_NCOUNTERS);
    uint32_t val = pmu_event_value(type);
    LOG_DEBUG("num=%u, type=%d, val=%u", num, type, val);
    drv->dev->reg->PMEVTYPER[num].word = val;
}

enum ethosu_pmu_event_type ETHOSU_PMU_Get_EVTYPER(struct ethosu_driver *drv, uint32_t num)
{
    assert(num < ETHOSU_PMU_NCOUNTERS);
    uint32_t val                    = drv->dev->reg->PMEVTYPER[num].word;
    enum ethosu_pmu_event_type type = pmu_event_type(val);
    LOG_DEBUG("num=%u, type=%d, val=%u", num, type, val);
    return type;
}

void ETHOSU_PMU_CYCCNT_Reset(struct ethosu_driver *drv)
{
    LOG_DEBUG("Reset PMU cycle counter");
    struct pmcr_r pmcr;
    pmcr.word                = drv->dev->reg->PMCR.word;
    pmcr.cycle_cnt_rst       = 1;
    drv->dev->reg->PMCR.word = pmcr.word;
}

void ETHOSU_PMU_EVCNTR_ALL_Reset(struct ethosu_driver *drv)
{
    LOG_DEBUG("Reset all events");
    struct pmcr_r pmcr;
    pmcr.word                = drv->dev->reg->PMCR.word;
    pmcr.event_cnt_rst       = 1;
    drv->dev->reg->PMCR.word = pmcr.word;
}

void ETHOSU_PMU_CNTR_Enable(struct ethosu_driver *drv, uint32_t mask)
{
    LOG_DEBUG("mask=0x%08x", mask);
    drv->dev->reg->PMCNTENSET.word = mask;
}

void ETHOSU_PMU_CNTR_Disable(struct ethosu_driver *drv, uint32_t mask)
{
    LOG_DEBUG("mask=0x%08x", mask);
    drv->dev->reg->PMCNTENCLR.word = mask;
}

uint32_t ETHOSU_PMU_CNTR_Status(struct ethosu_driver *drv)
{
    uint32_t pmcntenset = drv->dev->reg->PMCNTENSET.word;
    LOG_DEBUG("mask=0x%08x", pmcntenset);
    return pmcntenset;
}

uint64_t ETHOSU_PMU_Get_CCNTR(struct ethosu_driver *drv)
{
    uint32_t val_lo = drv->dev->reg->PMCCNTR.CYCLE_CNT_LO;
    uint32_t val_hi = drv->dev->reg->PMCCNTR.CYCLE_CNT_HI;
    uint64_t val    = ((uint64_t)val_hi << 32) | val_lo;

    LOG_DEBUG("val=%" PRIu64, val);
    return val;
}

void ETHOSU_PMU_Set_CCNTR(struct ethosu_driver *drv, uint64_t val)
{
    uint32_t active = ETHOSU_PMU_CNTR_Status(drv) & ETHOSU_PMU_CCNT_Msk;

    LOG_DEBUG("val=%llu", val);

    if (active)
    {
        ETHOSU_PMU_CNTR_Disable(drv, ETHOSU_PMU_CCNT_Msk);
    }

    drv->dev->reg->PMCCNTR.CYCLE_CNT_LO = val & MASK_0_31_BITS;
    drv->dev->reg->PMCCNTR.CYCLE_CNT_HI = (val & MASK_32_47_BITS) >> 32;

    if (active)
    {
        ETHOSU_PMU_CNTR_Enable(drv, ETHOSU_PMU_CCNT_Msk);
    }
}

uint32_t ETHOSU_PMU_Get_EVCNTR(struct ethosu_driver *drv, uint32_t num)
{
    assert(num < ETHOSU_PMU_NCOUNTERS);
    uint32_t val = drv->dev->reg->PMEVCNTR[num].word;
    LOG_DEBUG("num=%u, val=%u", num, val);

    return val;
}

void ETHOSU_PMU_Set_EVCNTR(struct ethosu_driver *drv, uint32_t num, uint32_t val)
{
    assert(num < ETHOSU_PMU_NCOUNTERS);
    LOG_DEBUG("num=%u, val=%u", num, val);
    drv->dev->reg->PMEVCNTR[num].word = val;
}

uint32_t ETHOSU_PMU_Get_CNTR_OVS(struct ethosu_driver *drv)
{
    LOG_DEBUG("");
    return drv->dev->reg->PMOVSSET.word;
}

void ETHOSU_PMU_Set_CNTR_OVS(struct ethosu_driver *drv, uint32_t mask)
{
    LOG_DEBUG("");
    drv->dev->reg->PMOVSCLR.word = mask;
}

void ETHOSU_PMU_Set_CNTR_IRQ_Enable(struct ethosu_driver *drv, uint32_t mask)
{
    LOG_DEBUG("mask=0x%08x", mask);
    drv->dev->reg->PMINTSET.word = mask;
}

void ETHOSU_PMU_Set_CNTR_IRQ_Disable(struct ethosu_driver *drv, uint32_t mask)
{
    LOG_DEBUG("mask=0x%08x", mask);
    drv->dev->reg->PMINTCLR.word = mask;
}

uint32_t ETHOSU_PMU_Get_IRQ_Enable(struct ethosu_driver *drv)
{
    uint32_t pmint = drv->dev->reg->PMINTSET.word;
    LOG_DEBUG("mask=0x%08x", pmint);
    return pmint;
}

void ETHOSU_PMU_CNTR_Increment(struct ethosu_driver *drv, uint32_t mask)
{
    LOG_DEBUG("");
    uint32_t cntrs_active = ETHOSU_PMU_CNTR_Status(drv);

    // Disable counters
    ETHOSU_PMU_CNTR_Disable(drv, mask);

    // Increment cycle counter
    if (mask & ETHOSU_PMU_CCNT_Msk)
    {
        uint64_t val                        = ETHOSU_PMU_Get_CCNTR(drv) + 1;
        drv->dev->reg->PMCCNTR.CYCLE_CNT_LO = val & MASK_0_31_BITS;
        drv->dev->reg->PMCCNTR.CYCLE_CNT_HI = (val & MASK_32_47_BITS) >> 32;
    }

    for (int i = 0; i < ETHOSU_PMU_NCOUNTERS; i++)
    {
        if (mask & (1 << i))
        {
            uint32_t val                    = ETHOSU_PMU_Get_EVCNTR(drv, i);
            drv->dev->reg->PMEVCNTR[i].word = val + 1;
        }
    }

    // Reenable the active counters
    ETHOSU_PMU_CNTR_Enable(drv, cntrs_active);
}

void ETHOSU_PMU_PMCCNTR_CFG_Set_Start_Event(struct ethosu_driver *drv, enum ethosu_pmu_event_type start_event)
{
    LOG_DEBUG("start_event=%u", start_event);
    uint32_t val = pmu_event_value(start_event);
    struct pmccntr_cfg_r cfg;
    cfg.word                        = drv->dev->reg->PMCCNTR_CFG.word;
    cfg.CYCLE_CNT_CFG_START         = val;
    drv->dev->reg->PMCCNTR_CFG.word = cfg.word;
}

void ETHOSU_PMU_PMCCNTR_CFG_Set_Stop_Event(struct ethosu_driver *drv, enum ethosu_pmu_event_type stop_event)
{
    LOG_DEBUG("stop_event=%u", stop_event);
    uint32_t val = pmu_event_value(stop_event);
    struct pmccntr_cfg_r cfg;
    cfg.word                        = drv->dev->reg->PMCCNTR_CFG.word;
    cfg.CYCLE_CNT_CFG_STOP          = val;
    drv->dev->reg->PMCCNTR_CFG.word = cfg.word;
}