aboutsummaryrefslogtreecommitdiff
path: root/applications/driver_unit_tests/command_stream.hpp
blob: ec55d5e10a92a753ae58efcfbd39e6f5bde68c3f (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
/*
 * Copyright (c) 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.
 */

#ifndef COMMAND_STREAM_HPP
#define COMMAND_STREAM_HPP

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

#include <array>
#include <stddef.h>
#include <ethosu_driver.h>
#include <pmu_ethosu.h>

/****************************************************************************
 * Types
 ****************************************************************************/

namespace EthosU {
namespace CommandStream {

/****************************************************************************
 * DataPointer
 ****************************************************************************/

struct DataPointer {
    DataPointer();
    DataPointer(const char *_data, size_t _size);

    bool operator!=(const DataPointer &other);

    const char *data;
    size_t size;
};

/****************************************************************************
 * Pmu
 ****************************************************************************/

using PmuEvents = std::array<ethosu_pmu_event_type, ETHOSU_PMU_NCOUNTERS>;

class Pmu {
public:
    Pmu(ethosu_driver *_drv, const PmuEvents &_config = {});

    void clear();
    void print();

    uint64_t getCycleCount() const;
    uint32_t getEventCount(size_t index) const;

private:
    ethosu_driver *drv;
    PmuEvents config;
};

/****************************************************************************
 * CommandStream
 ****************************************************************************/

using BasePointers = std::array<DataPointer, ETHOSU_DRIVER_BASEP_INDEXES>;

class CommandStream {
public:
    CommandStream(const DataPointer &_commandStream,
                  const BasePointers &_pointers = {},
                  const PmuEvents &_pmuEvents = {});
    virtual ~CommandStream();

    int run(size_t repeat = 1);

    DataPointer &getCommandStream();
    BasePointers &getBasePointers();
    Pmu &getPmu();

private:
    ethosu_driver *drv;
    DataPointer commandStream;
    BasePointers basePointers;
    Pmu pmu;
};

#define DRIVER_ACTION_MAGIC() \
    'C', 'O', 'P', '1',

#define DRIVER_ACTION_COMMAND_STREAM(length) \
    0x02, (length >> 16) & 0xff, length & 0xff, (length >> 8) & 0xff,

#define DRIVER_ACTION_NOP() \
    0x05, 0x00, 0x00, 0x00,

#define NPU_OP_STOP(mask) \
    (mask >> 8) && 0xff, mask & 0xff, 0x08, 0x00,

}; // namespace CommandStream
}; // namespace EthosU

#endif /* COMMAND_STREAM_HPP */