blob: 4ad2a33499de7662d18b70d096658be3cb44616c [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.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Creates driver actions that are embedded in the custom operator payload.
Louis Verhaard52078302020-11-18 13:35:06 +010018import struct
Tim Hall79d07d22020-04-27 18:20:16 +010019from typing import List
Diego Russoea6111a2020-04-14 18:41:58 +010020
21import numpy as np
22
Louis Verhaard52078302020-11-18 13:35:06 +010023from .api import NpuAccelerator
24from .architecture_features import Accelerator
25from .architecture_features import ArchitectureFeatures
26from .architecture_features import create_default_arch
erik.andersson@arm.com1878dab2021-03-16 09:40:24 +010027from .errors import VelaError
Diego Russoe8a10452020-04-21 17:39:10 +010028from .ethos_u55_regs.ethos_u55_regs import ARCH_VER
29from .ethos_u55_regs.ethos_u55_regs import config_r
30from .ethos_u55_regs.ethos_u55_regs import id_r
Tim Hall79d07d22020-04-27 18:20:16 +010031
32
33class DACommands:
34 Reserved = 0x00
35 Config = 0x01
36 Config_PatchShift = 4
37 CmdStream = 0x02
38 ReadAPB = 0x03
39 ReadAPB_CountShift = 12
40 ReadAPB_IndexMask = (1 << ReadAPB_CountShift) - 1
41 DumpSHRAM = 0x04
42 NOP = 0x05
43
44
45def make_da_tag(id: int, reserved: int, param: int) -> int:
46 tag: int = id
47 tag |= reserved << 8
48 tag |= param << 16
49 return tag
50
51
52def emit_fourcc(data: List[int], fourcc: str):
Diego Russoea6111a2020-04-14 18:41:58 +010053 assert data is not None
54 assert fourcc is not None
Tim Hall79d07d22020-04-27 18:20:16 +010055 assert len(fourcc) == 4
56 value: int = 0
57 value = fourcc[0].encode()[0]
58 value |= fourcc[1].encode()[0] << 8
59 value |= fourcc[2].encode()[0] << 16
60 value |= fourcc[3].encode()[0] << 24
61 data.append(value)
62
63
64def build_id_word():
65 arch_major_rev, arch_minor_rev, arch_patch_rev = (int(x) for x in ARCH_VER.split("."))
66 n = id_r()
67 n.set_arch_major_rev(arch_major_rev)
68 n.set_arch_minor_rev(arch_minor_rev)
69 n.set_arch_patch_rev(arch_patch_rev)
70 return n.word
71
72
73def build_config_word(arch):
Tim Hallf7e810a2020-06-25 15:04:31 +010074 macs_cc = arch.ncores * arch.config.macs
Tim Hall79d07d22020-04-27 18:20:16 +010075 log2_macs_cc = int(np.log2(macs_cc) + 0.5)
Tim Hallf7e810a2020-06-25 15:04:31 +010076 shram_size = arch.ncores * int(arch.shram_size_bytes / 1024)
Tim Hall79d07d22020-04-27 18:20:16 +010077 n = config_r()
Jonny Svärdbdb1d6e2022-01-04 12:38:29 +010078 if arch.is_ethos_u65_system:
79 n.set_product(1)
80 else:
81 n.set_product(0) # U55
Tim Hall79d07d22020-04-27 18:20:16 +010082 n.set_shram_size(shram_size)
83 n.set_cmd_stream_version(0) # may be incremented in the future
84 n.set_macs_per_cc(log2_macs_cc)
85 return n.word
86
87
88def emit_config(data: List[int], rel: int, patch: int, arch):
Diego Russoea6111a2020-04-14 18:41:58 +010089 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +010090 data.append(make_da_tag(DACommands.Config, 0, (patch << DACommands.Config_PatchShift) | rel))
91 data.append(build_config_word(arch))
92 data.append(build_id_word())
93
94
95def emit_cmd_stream_header(data: List[int], length: int):
Diego Russoea6111a2020-04-14 18:41:58 +010096 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +010097 # Insert NOPs to align start of command stream to 16 bytes
98 num_nops = 4 - ((len(data) + 1) % 4)
99 for _ in range(num_nops):
100 data.append(make_da_tag(DACommands.NOP, 0, 0))
101
102 # Use the reserved 8 bit as the length high
103 length_high = (length & 0x00FF0000) >> 16
104 length_low = length & 0x0000FFFF
105 data.append(make_da_tag(DACommands.CmdStream, length_high, length_low))
106
107
108def emit_reg_read(data: List[int], reg_index: int, reg_count: int = 1):
Diego Russoea6111a2020-04-14 18:41:58 +0100109 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100110 assert reg_index >= 0
111 assert reg_count >= 1
112 payload: int = (reg_index & DACommands.ReadAPB_IndexMask) | ((reg_count << DACommands.ReadAPB_CountShift) - 1)
113 data.append(make_da_tag(DACommands.ReadAPB, 0, payload))
114
115
116def emit_dump_shram(data: List[int]):
Diego Russoea6111a2020-04-14 18:41:58 +0100117 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100118 data.append(make_da_tag(DACommands.DumpSHRAM, 0, 0))
Louis Verhaard52078302020-11-18 13:35:06 +0100119
120
121def create_driver_payload(register_command_stream: List[int], arch: ArchitectureFeatures) -> bytes:
Jonas Ohlssond8575072022-03-30 10:30:25 +0200122 """Creates driver header and includes the given command"""
Louis Verhaard52078302020-11-18 13:35:06 +0100123 # Prepare driver actions for this command tensor
Dwight Lidman9b43f842020-12-08 17:56:44 +0100124 da_list: List[int] = []
Louis Verhaard52078302020-11-18 13:35:06 +0100125 emit_fourcc(da_list, "COP1")
126 emit_config(da_list, 0, 1, arch)
erik.andersson@arm.com1878dab2021-03-16 09:40:24 +0100127 if len(register_command_stream) >= 1 << 24:
128 raise VelaError(
129 "The command stream exceeds the driver size limit of 64 MiB. "
130 f"The current stream size is {4*len(register_command_stream)/2**20:.2F} MiB"
131 )
132
Louis Verhaard52078302020-11-18 13:35:06 +0100133 emit_cmd_stream_header(da_list, len(register_command_stream))
134
135 # Append command stream words
136 da_list.extend(register_command_stream)
137 # Convert to bytes, in little endian format
138 return struct.pack("<{0}I".format(len(da_list)), *da_list)
139
140
141def npu_create_driver_payload(register_command_stream: List[int], accelerator: NpuAccelerator) -> bytes:
142 """Internal implementation of the public facing API to create driver payload"""
143 arch = create_default_arch(Accelerator.from_npu_accelerator(accelerator))
144 return create_driver_payload(register_command_stream, arch)