blob: 86c4a369bb311345e7b26886949b087968d0ee06 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# 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, WITHOUT
13# 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
18# Description:
19# Creates driver actions that are embedded in the custom operator payload.
20
21import numpy as np
22from typing import List
23from .ethos_u55_regs.ethos_u55_regs import *
24
25
26class DACommands:
27 Reserved = 0x00
28 Config = 0x01
29 Config_PatchShift = 4
30 CmdStream = 0x02
31 ReadAPB = 0x03
32 ReadAPB_CountShift = 12
33 ReadAPB_IndexMask = (1 << ReadAPB_CountShift) - 1
34 DumpSHRAM = 0x04
35 NOP = 0x05
36
37
38def make_da_tag(id: int, reserved: int, param: int) -> int:
39 tag: int = id
40 tag |= reserved << 8
41 tag |= param << 16
42 return tag
43
44
45def emit_fourcc(data: List[int], fourcc: str):
46 assert data != None
47 assert fourcc != None
48 assert len(fourcc) == 4
49 value: int = 0
50 value = fourcc[0].encode()[0]
51 value |= fourcc[1].encode()[0] << 8
52 value |= fourcc[2].encode()[0] << 16
53 value |= fourcc[3].encode()[0] << 24
54 data.append(value)
55
56
57def build_id_word():
58 arch_major_rev, arch_minor_rev, arch_patch_rev = (int(x) for x in ARCH_VER.split("."))
59 n = id_r()
60 n.set_arch_major_rev(arch_major_rev)
61 n.set_arch_minor_rev(arch_minor_rev)
62 n.set_arch_patch_rev(arch_patch_rev)
63 return n.word
64
65
66def build_config_word(arch):
67 macs_cc = arch.config.macs
68 log2_macs_cc = int(np.log2(macs_cc) + 0.5)
69 shram_size = int(arch.shram_size_bytes / 1024)
70 n = config_r()
71 n.set_shram_size(shram_size)
72 n.set_cmd_stream_version(0) # may be incremented in the future
73 n.set_macs_per_cc(log2_macs_cc)
74 return n.word
75
76
77def emit_config(data: List[int], rel: int, patch: int, arch):
78 assert data != None
79 data.append(make_da_tag(DACommands.Config, 0, (patch << DACommands.Config_PatchShift) | rel))
80 data.append(build_config_word(arch))
81 data.append(build_id_word())
82
83
84def emit_cmd_stream_header(data: List[int], length: int):
85 assert data != None
86 # Insert NOPs to align start of command stream to 16 bytes
87 num_nops = 4 - ((len(data) + 1) % 4)
88 for _ in range(num_nops):
89 data.append(make_da_tag(DACommands.NOP, 0, 0))
90
91 # Use the reserved 8 bit as the length high
92 length_high = (length & 0x00FF0000) >> 16
93 length_low = length & 0x0000FFFF
94 data.append(make_da_tag(DACommands.CmdStream, length_high, length_low))
95
96
97def emit_reg_read(data: List[int], reg_index: int, reg_count: int = 1):
98 assert data != None
99 assert reg_index >= 0
100 assert reg_count >= 1
101 payload: int = (reg_index & DACommands.ReadAPB_IndexMask) | ((reg_count << DACommands.ReadAPB_CountShift) - 1)
102 data.append(make_da_tag(DACommands.ReadAPB, 0, payload))
103
104
105def emit_dump_shram(data: List[int]):
106 assert data != None
107 data.append(make_da_tag(DACommands.DumpSHRAM, 0, 0))