blob: 7163b9d58caa01781992918585f5e348e95e7543 [file] [log] [blame]
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +01001/*
Jonny Svärd44b250b2022-04-19 15:35:01 +02002 * Copyright (c) 2021-2022 Arm Limited.
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +01003 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#ifndef COMMAND_STREAM_HPP
20#define COMMAND_STREAM_HPP
21
22/****************************************************************************
23 * Includes
24 ****************************************************************************/
25
26#include <array>
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +010027#include <ethosu_driver.h>
28#include <pmu_ethosu.h>
Anton Moberg908a07c2021-04-08 09:50:57 +020029#include <stddef.h>
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +010030
31/****************************************************************************
Jonny Svärdbec9ac12021-10-15 10:35:57 +020032 * Defines
33 ****************************************************************************/
34
35#ifndef ETHOSU_BASEP_INDEXES
36#define ETHOSU_BASEP_INDEXES 8
37#endif
38
39/****************************************************************************
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +010040 * Types
41 ****************************************************************************/
42
43namespace EthosU {
44namespace CommandStream {
45
46/****************************************************************************
47 * DataPointer
48 ****************************************************************************/
49
50struct DataPointer {
51 DataPointer();
52 DataPointer(const char *_data, size_t _size);
53
54 bool operator!=(const DataPointer &other);
55
56 const char *data;
57 size_t size;
58};
59
60/****************************************************************************
61 * Pmu
62 ****************************************************************************/
63
64using PmuEvents = std::array<ethosu_pmu_event_type, ETHOSU_PMU_NCOUNTERS>;
65
66class Pmu {
67public:
68 Pmu(ethosu_driver *_drv, const PmuEvents &_config = {});
69
70 void clear();
71 void print();
72
73 uint64_t getCycleCount() const;
74 uint32_t getEventCount(size_t index) const;
75
76private:
77 ethosu_driver *drv;
78 PmuEvents config;
79};
80
81/****************************************************************************
82 * CommandStream
83 ****************************************************************************/
84
Jonny Svärdc5941c42021-06-01 18:40:45 +020085using BasePointers = std::array<DataPointer, ETHOSU_BASEP_INDEXES>;
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +010086
87class CommandStream {
88public:
89 CommandStream(const DataPointer &_commandStream,
90 const BasePointers &_pointers = {},
Anton Moberg908a07c2021-04-08 09:50:57 +020091 const PmuEvents &_pmuEvents = {});
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +010092 virtual ~CommandStream();
93
94 int run(size_t repeat = 1);
Jonny Svärd44b250b2022-04-19 15:35:01 +020095 int run_async();
96 int wait_async(bool block = true);
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +010097
98 DataPointer &getCommandStream();
99 BasePointers &getBasePointers();
100 Pmu &getPmu();
101
102private:
103 ethosu_driver *drv;
104 DataPointer commandStream;
105 BasePointers basePointers;
106 Pmu pmu;
107};
108
Anton Moberg908a07c2021-04-08 09:50:57 +0200109#define DRIVER_ACTION_MAGIC() 'C', 'O', 'P', '1',
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +0100110
Anton Moberg908a07c2021-04-08 09:50:57 +0200111#define DRIVER_ACTION_COMMAND_STREAM(length) 0x02, (length >> 16) & 0xff, length & 0xff, (length >> 8) & 0xff,
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +0100112
Anton Moberg908a07c2021-04-08 09:50:57 +0200113#define DRIVER_ACTION_NOP() 0x05, 0x00, 0x00, 0x00,
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +0100114
Anton Moberg908a07c2021-04-08 09:50:57 +0200115#define NPU_OP_STOP(mask) (mask >> 8) && 0xff, mask & 0xff, 0x08, 0x00,
Kristofer Jonssonb5f7cfe2021-03-10 17:13:52 +0100116
117}; // namespace CommandStream
118}; // namespace EthosU
119
120#endif /* COMMAND_STREAM_HPP */