blob: 86bed1106a65751bdab5dc9e9dd70b34a611c2cb [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
Diego Russoe8a10452020-04-21 17:39:10 +010027from .ethos_u55_regs.ethos_u55_regs import ARCH_VER
28from .ethos_u55_regs.ethos_u55_regs import config_r
29from .ethos_u55_regs.ethos_u55_regs import id_r
Tim Hall79d07d22020-04-27 18:20:16 +010030
31
32class DACommands:
33 Reserved = 0x00
34 Config = 0x01
35 Config_PatchShift = 4
36 CmdStream = 0x02
37 ReadAPB = 0x03
38 ReadAPB_CountShift = 12
39 ReadAPB_IndexMask = (1 << ReadAPB_CountShift) - 1
40 DumpSHRAM = 0x04
41 NOP = 0x05
42
43
44def make_da_tag(id: int, reserved: int, param: int) -> int:
45 tag: int = id
46 tag |= reserved << 8
47 tag |= param << 16
48 return tag
49
50
51def emit_fourcc(data: List[int], fourcc: str):
Diego Russoea6111a2020-04-14 18:41:58 +010052 assert data is not None
53 assert fourcc is not None
Tim Hall79d07d22020-04-27 18:20:16 +010054 assert len(fourcc) == 4
55 value: int = 0
56 value = fourcc[0].encode()[0]
57 value |= fourcc[1].encode()[0] << 8
58 value |= fourcc[2].encode()[0] << 16
59 value |= fourcc[3].encode()[0] << 24
60 data.append(value)
61
62
63def build_id_word():
64 arch_major_rev, arch_minor_rev, arch_patch_rev = (int(x) for x in ARCH_VER.split("."))
65 n = id_r()
66 n.set_arch_major_rev(arch_major_rev)
67 n.set_arch_minor_rev(arch_minor_rev)
68 n.set_arch_patch_rev(arch_patch_rev)
69 return n.word
70
71
72def build_config_word(arch):
Tim Hallf7e810a2020-06-25 15:04:31 +010073 macs_cc = arch.ncores * arch.config.macs
Tim Hall79d07d22020-04-27 18:20:16 +010074 log2_macs_cc = int(np.log2(macs_cc) + 0.5)
Tim Hallf7e810a2020-06-25 15:04:31 +010075 shram_size = arch.ncores * int(arch.shram_size_bytes / 1024)
Tim Hall79d07d22020-04-27 18:20:16 +010076 n = config_r()
77 n.set_shram_size(shram_size)
78 n.set_cmd_stream_version(0) # may be incremented in the future
79 n.set_macs_per_cc(log2_macs_cc)
80 return n.word
81
82
83def emit_config(data: List[int], rel: int, patch: int, arch):
Diego Russoea6111a2020-04-14 18:41:58 +010084 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +010085 data.append(make_da_tag(DACommands.Config, 0, (patch << DACommands.Config_PatchShift) | rel))
86 data.append(build_config_word(arch))
87 data.append(build_id_word())
88
89
90def emit_cmd_stream_header(data: List[int], length: int):
Diego Russoea6111a2020-04-14 18:41:58 +010091 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +010092 # Insert NOPs to align start of command stream to 16 bytes
93 num_nops = 4 - ((len(data) + 1) % 4)
94 for _ in range(num_nops):
95 data.append(make_da_tag(DACommands.NOP, 0, 0))
96
97 # Use the reserved 8 bit as the length high
98 length_high = (length & 0x00FF0000) >> 16
99 length_low = length & 0x0000FFFF
100 data.append(make_da_tag(DACommands.CmdStream, length_high, length_low))
101
102
103def emit_reg_read(data: List[int], reg_index: int, reg_count: int = 1):
Diego Russoea6111a2020-04-14 18:41:58 +0100104 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100105 assert reg_index >= 0
106 assert reg_count >= 1
107 payload: int = (reg_index & DACommands.ReadAPB_IndexMask) | ((reg_count << DACommands.ReadAPB_CountShift) - 1)
108 data.append(make_da_tag(DACommands.ReadAPB, 0, payload))
109
110
111def emit_dump_shram(data: List[int]):
Diego Russoea6111a2020-04-14 18:41:58 +0100112 assert data is not None
Tim Hall79d07d22020-04-27 18:20:16 +0100113 data.append(make_da_tag(DACommands.DumpSHRAM, 0, 0))
Louis Verhaard52078302020-11-18 13:35:06 +0100114
115
116def create_driver_payload(register_command_stream: List[int], arch: ArchitectureFeatures) -> bytes:
117 """Creates driver header and includes the given command
118 """
119 # Prepare driver actions for this command tensor
120 da_list = []
121 emit_fourcc(da_list, "COP1")
122 emit_config(da_list, 0, 1, arch)
123 emit_cmd_stream_header(da_list, len(register_command_stream))
124
125 # Append command stream words
126 da_list.extend(register_command_stream)
127 # Convert to bytes, in little endian format
128 return struct.pack("<{0}I".format(len(da_list)), *da_list)
129
130
131def npu_create_driver_payload(register_command_stream: List[int], accelerator: NpuAccelerator) -> bytes:
132 """Internal implementation of the public facing API to create driver payload"""
133 arch = create_default_arch(Accelerator.from_npu_accelerator(accelerator))
134 return create_driver_payload(register_command_stream, arch)