blob: bd15af2005d7d5ed442cf47fb5176cf2a1ff94f2 [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
Tim Hall79d07d22020-04-27 18:20:16 +010021from typing import List
Diego Russoea6111a2020-04-14 18:41:58 +010022
23import numpy as np
24
25from .ethos_u55_regs.ethos_u55_regs import config_r, id_r, ARCH_VER
Tim Hall79d07d22020-04-27 18:20:16 +010026
27
28class DACommands:
29 Reserved = 0x00
30 Config = 0x01
31 Config_PatchShift = 4
32 CmdStream = 0x02
33 ReadAPB = 0x03
34 ReadAPB_CountShift = 12
35 ReadAPB_IndexMask = (1 << ReadAPB_CountShift) - 1
36 DumpSHRAM = 0x04
37 NOP = 0x05
38
39
40def make_da_tag(id: int, reserved: int, param: int) -> int:
41 tag: int = id
42 tag |= reserved << 8
43 tag |= param << 16
44 return tag
45
46
47def emit_fourcc(data: List[int], fourcc: str):
Diego Russoea6111a2020-04-14 18:41:58 +010048 assert data is not None
49 assert fourcc is not None
Tim Hall79d07d22020-04-27 18:20:16 +010050 assert len(fourcc) == 4
51 value: int = 0
52 value = fourcc[0].encode()[0]
53 value |= fourcc[1].encode()[0] << 8
54 value |= fourcc[2].encode()[0] << 16
55 value |= fourcc[3].encode()[0] << 24
56 data.append(value)
57
58
59def build_id_word():
60 arch_major_rev, arch_minor_rev, arch_patch_rev = (int(x) for x in ARCH_VER.split("."))
61 n = id_r()
62 n.set_arch_major_rev(arch_major_rev)
63 n.set_arch_minor_rev(arch_minor_rev)
64 n.set_arch_patch_rev(arch_patch_rev)
65 return n.word
66
67
68def build_config_word(arch):
69 macs_cc = arch.config.macs
70 log2_macs_cc = int(np.log2(macs_cc) + 0.5)
71 shram_size = int(arch.shram_size_bytes / 1024)
72 n = config_r()
73 n.set_shram_size(shram_size)
74 n.set_cmd_stream_version(0) # may be incremented in the future
75 n.set_macs_per_cc(log2_macs_cc)
76 return n.word
77
78
79def emit_config(data: List[int], rel: int, patch: int, arch):
Diego Russoea6111a2020-04-14 18:41:58 +010080 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +010081 data.append(make_da_tag(DACommands.Config, 0, (patch << DACommands.Config_PatchShift) | rel))
82 data.append(build_config_word(arch))
83 data.append(build_id_word())
84
85
86def emit_cmd_stream_header(data: List[int], length: int):
Diego Russoea6111a2020-04-14 18:41:58 +010087 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +010088 # Insert NOPs to align start of command stream to 16 bytes
89 num_nops = 4 - ((len(data) + 1) % 4)
90 for _ in range(num_nops):
91 data.append(make_da_tag(DACommands.NOP, 0, 0))
92
93 # Use the reserved 8 bit as the length high
94 length_high = (length & 0x00FF0000) >> 16
95 length_low = length & 0x0000FFFF
96 data.append(make_da_tag(DACommands.CmdStream, length_high, length_low))
97
98
99def emit_reg_read(data: List[int], reg_index: int, reg_count: int = 1):
Diego Russoea6111a2020-04-14 18:41:58 +0100100 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100101 assert reg_index >= 0
102 assert reg_count >= 1
103 payload: int = (reg_index & DACommands.ReadAPB_IndexMask) | ((reg_count << DACommands.ReadAPB_CountShift) - 1)
104 data.append(make_da_tag(DACommands.ReadAPB, 0, payload))
105
106
107def emit_dump_shram(data: List[int]):
Diego Russoea6111a2020-04-14 18:41:58 +0100108 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100109 data.append(make_da_tag(DACommands.DumpSHRAM, 0, 0))