Skip to content

Commit 09495c9

Browse files
andyrosskartben
authored andcommitted
soc/mediatek: Ruffify python scripts
These in-tree scripts fail the new ruff checks. Clean things up so modifications can merge. Signed-off-by: Andy Ross <[email protected]>
1 parent fe5c11d commit 09495c9

File tree

2 files changed

+115
-85
lines changed

2 files changed

+115
-85
lines changed

soc/mediatek/mt8xxx/gen_img.py

Lines changed: 52 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env python3
22
# Copyright 2023 The ChromiumOS Authors
33
# SPDX-License-Identifier: Apache-2.0
4-
import sys
54
import struct
5+
import sys
6+
67
import elftools.elf.elffile
78
import elftools.elf.sections
89

@@ -19,13 +20,11 @@
1920
#
2021
# No padding or uninterpreted bytes.
2122

22-
FILE_MAGIC = 0xe463be95
23+
FILE_MAGIC = 0xE463BE95
2324

2425
elf_file = sys.argv[1]
2526
out_file = sys.argv[2]
2627

27-
ef = elftools.elf.elffile.ELFFile(open(elf_file, "rb"))
28-
2928
sram = bytearray()
3029
dram = bytearray()
3130

@@ -34,56 +33,67 @@
3433
# SOCs, but it's always a <=1M region in 0x4xxxxxxx. Just use what we
3534
# get, but validate that it fits.
3635
sram_block = 0
36+
37+
3738
def sram_off(addr):
3839
global sram_block
3940
if addr < 0x40000000 or addr >= 0x50000000:
4041
return -1
41-
block = addr & ~0xfffff
42+
block = addr & ~0xFFFFF
4243
assert sram_block in (0, block)
4344

4445
sram_block = block
4546
off = addr - sram_block
4647
assert off < 0x100000
4748
return off
4849

50+
4951
# Similar heuristics: current platforms put DRAM either at 0x60000000
5052
# or 0x90000000 with no more than 16M of range
5153
def dram_off(addr):
52-
if (addr >> 28 not in [6, 9]) or (addr & 0x0f000000 != 0):
54+
if (addr >> 28 not in [6, 9]) or (addr & 0x0F000000 != 0):
5355
return -1
54-
return addr & 0xffffff
55-
56-
for seg in ef.iter_segments():
57-
h = seg.header
58-
if h.p_type == "PT_LOAD":
59-
soff = sram_off(h.p_paddr)
60-
doff = dram_off(h.p_paddr)
61-
if soff >= 0:
62-
buf = sram
63-
off = soff
64-
elif doff >= 0:
65-
buf = dram
66-
off = doff
67-
else:
68-
print(f"Invalid PT_LOAD address {h.p_paddr:x}")
69-
sys.exit(1)
70-
71-
dat = seg.data()
72-
end = off + len(dat)
73-
if end > len(buf):
74-
buf.extend(b'\x00' * (end - len(buf)))
75-
76-
# pylint: disable=consider-using-enumerate
77-
for i in range(len(dat)):
78-
buf[i + off] = dat[i]
79-
80-
for sec in ef.iter_sections():
81-
if isinstance(sec, elftools.elf.sections.SymbolTableSection):
82-
for sym in sec.iter_symbols():
83-
if sym.name == "mtk_adsp_boot_entry":
84-
boot_vector = sym.entry['st_value']
85-
86-
of = open(out_file, "wb")
87-
of.write(struct.pack("<III", FILE_MAGIC, len(sram), boot_vector))
88-
of.write(sram)
89-
of.write(dram)
56+
return addr & 0xFFFFFF
57+
58+
59+
def read_elf(efile):
60+
ef = elftools.elf.elffile.ELFFile(efile)
61+
62+
for seg in ef.iter_segments():
63+
h = seg.header
64+
if h.p_type == "PT_LOAD":
65+
soff = sram_off(h.p_paddr)
66+
doff = dram_off(h.p_paddr)
67+
if soff >= 0:
68+
buf = sram
69+
off = soff
70+
elif doff >= 0:
71+
buf = dram
72+
off = doff
73+
else:
74+
print(f"Invalid PT_LOAD address {h.p_paddr:x}")
75+
sys.exit(1)
76+
77+
dat = seg.data()
78+
end = off + len(dat)
79+
if end > len(buf):
80+
buf.extend(b'\x00' * (end - len(buf)))
81+
82+
# pylint: disable=consider-using-enumerate
83+
for i in range(len(dat)):
84+
buf[i + off] = dat[i]
85+
86+
for sec in ef.iter_sections():
87+
if isinstance(sec, elftools.elf.sections.SymbolTableSection):
88+
for sym in sec.iter_symbols():
89+
if sym.name == "mtk_adsp_boot_entry":
90+
boot_vector = sym.entry['st_value']
91+
92+
with open(out_file, "wb") as of:
93+
of.write(struct.pack("<III", FILE_MAGIC, len(sram), boot_vector))
94+
of.write(sram)
95+
of.write(dram)
96+
97+
98+
with open(elf_file, "rb") as f:
99+
read_elf(f)

soc/mediatek/mt8xxx/mtk_adsp_load.py

Lines changed: 63 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# Copyright 2023 The ChromiumOS Authors
33
# SPDX-License-Identifier: Apache-2.0
44
import ctypes
5+
import mmap
56
import os
7+
import re
8+
import struct
69
import sys
7-
import mmap
810
import time
9-
import struct
1011
from glob import glob
11-
import re
1212

1313
# MT8195 audio firmware load/debug gadget
1414

@@ -34,20 +34,21 @@
3434
# phandles pointing to "adsp_mem_region" and "adsp_dma_mem_region"
3535
# nodes under "/reserved-memory").
3636

37-
FILE_MAGIC = 0xe463be95
37+
FILE_MAGIC = 0xE463BE95
3838

3939
# Runtime mmap objects for each MAPPINGS entry
4040
maps = {}
4141

42+
4243
# Returns a string (e.g. "mt8195", "mt8186", "mt8188") if a supported
4344
# adsp is detected, or None if not
4445
def detect():
45-
compat = readfile(glob("/proc/device-tree/**/adsp@*/compatible",
46-
recursive=True)[0], "r")
46+
compat = readfile(glob("/proc/device-tree/**/adsp@*/compatible", recursive=True)[0], "r")
4747
m = re.match(r'.*(mt\d{4})-dsp', compat)
4848
if m:
4949
return m.group(1)
5050

51+
5152
# Parse devicetree to find the MMIO mappings: there is an "adsp" node
5253
# (in various locations) with an array of named "reg" mappings. It
5354
# also refers by reference to reserved-memory regions of system
@@ -59,7 +60,7 @@ def mappings():
5960
path = glob("/proc/device-tree/**/adsp@*/", recursive=True)[0]
6061
rnames = readfile(path + "reg-names", "r").split('\0')[:-1]
6162
regs = struct.unpack(f">{2 * len(rnames)}Q", readfile(path + "reg"))
62-
maps = { n : (regs[2*i], regs[2*i+1]) for i, n in enumerate(rnames) }
63+
maps = {n: (regs[2 * i], regs[2 * i + 1]) for i, n in enumerate(rnames)}
6364
for i, ph in enumerate(struct.unpack(">II", readfile(path + "memory-region"))):
6465
for rmem in glob("/proc/device-tree/reserved-memory/*/"):
6566
phf = rmem + "phandle"
@@ -69,78 +70,81 @@ def mappings():
6970
break
7071
return maps
7172

73+
7274
# Register API for 8195
73-
class MT8195():
75+
class MT8195:
7476
def __init__(self, maps):
7577
# Create a Regs object for the registers
7678
r = Regs(ctypes.addressof(ctypes.c_int.from_buffer(maps["cfg"])))
77-
r.ALTRESETVEC = 0x0004 # Xtensa boot address
78-
r.RESET_SW = 0x0024 # Xtensa halt/reset/boot control
79-
r.PDEBUGBUS0 = 0x000c # Unclear, enabled by host, unused by SOF?
80-
r.SRAM_POOL_CON = 0x0930 # SRAM power control: low 4 bits (banks?) enable
81-
r.EMI_MAP_ADDR = 0x981c # == host SRAM mapping - 0x40000000 (controls MMIO map?)
79+
r.ALTRESETVEC = 0x0004 # Xtensa boot address
80+
r.RESET_SW = 0x0024 # Xtensa halt/reset/boot control
81+
r.PDEBUGBUS0 = 0x000C # Unclear, enabled by host, unused by SOF?
82+
r.SRAM_POOL_CON = 0x0930 # SRAM power control: low 4 bits (banks?) enable
83+
r.EMI_MAP_ADDR = 0x981C # == host SRAM mapping - 0x40000000 (controls MMIO map?)
8284
r.freeze()
8385
self.cfg = r
8486

8587
def logrange(self):
8688
return range(0x700000, 0x800000)
8789

8890
def stop(self):
89-
self.cfg.RESET_SW |= 8 # Set RUNSTALL: halt CPU
90-
self.cfg.RESET_SW |= 3 # Set low two bits: "BRESET|DRESET"
91+
self.cfg.RESET_SW |= 8 # Set RUNSTALL: halt CPU
92+
self.cfg.RESET_SW |= 3 # Set low two bits: "BRESET|DRESET"
9193

9294
def start(self, boot_vector):
9395
self.stop()
94-
self.cfg.RESET_SW |= 0x10 # Enable "alternate reset" boot vector
96+
self.cfg.RESET_SW |= 0x10 # Enable "alternate reset" boot vector
9597
self.cfg.ALTRESETVEC = boot_vector
96-
self.cfg.RESET_SW &= ~3 # Release reset bits
97-
self.cfg.RESET_SW &= ~8 # Clear RUNSTALL: go!
98+
self.cfg.RESET_SW &= ~3 # Release reset bits
99+
self.cfg.RESET_SW &= ~8 # Clear RUNSTALL: go!
100+
98101

99102
# Register API for 8186/8188
100-
class MT818x():
103+
class MT818x:
101104
def __init__(self, maps):
102105
# These have registers spread across two blocks
103106
cfg_base = ctypes.addressof(ctypes.c_int.from_buffer(maps["cfg"]))
104107
sec_base = ctypes.addressof(ctypes.c_int.from_buffer(maps["sec"]))
105108
self.cfg = Regs(cfg_base)
106109
self.cfg.SW_RSTN = 0x00
107-
self.cfg.IO_CONFIG = 0x0c
110+
self.cfg.IO_CONFIG = 0x0C
108111
self.cfg.freeze()
109112
self.sec = Regs(sec_base)
110113
self.sec.ALTVEC_C0 = 0x04
111-
self.sec.ALTVECSEL = 0x0c
114+
self.sec.ALTVECSEL = 0x0C
112115
self.sec.freeze()
113116

114117
def logrange(self):
115118
return range(0x700000, 0x800000)
116119

117120
def stop(self):
118-
self.cfg.IO_CONFIG |= (1<<31) # Set RUNSTALL to stop core
121+
self.cfg.IO_CONFIG |= 1 << 31 # Set RUNSTALL to stop core
119122
time.sleep(0.1)
120-
self.cfg.SW_RSTN |= 0x11 # Assert reset: SW_RSTN_C0|SW_DBG_RSTN_C0
123+
self.cfg.SW_RSTN |= 0x11 # Assert reset: SW_RSTN_C0|SW_DBG_RSTN_C0
121124

122125
# Note: 8186 and 8188 use different bits in ALTVECSEC, but
123126
# it's safe to write both to enable the alternate boot vector
124127
def start(self, boot_vector):
125-
self.cfg.IO_CONFIG |= (1<<31) # Set RUNSTALL
128+
self.cfg.IO_CONFIG |= 1 << 31 # Set RUNSTALL
126129
self.sec.ALTVEC_C0 = boot_vector
127-
self.sec.ALTVECSEL = 0x03 # Enable alternate vector
128-
self.cfg.SW_RSTN |= 0x00000011 # Assert reset
129-
self.cfg.SW_RSTN &= 0xffffffee # Release reset
130-
self.cfg.IO_CONFIG &= 0x7fffffff # Clear RUNSTALL
130+
self.sec.ALTVECSEL = 0x03 # Enable alternate vector
131+
self.cfg.SW_RSTN |= 0x00000011 # Assert reset
132+
self.cfg.SW_RSTN &= 0xFFFFFFEE # Release reset
133+
self.cfg.IO_CONFIG &= 0x7FFFFFFF # Clear RUNSTALL
131134

132-
class MT8196():
135+
136+
class MT8196:
133137
def __init__(self, maps):
134138
cfg_base = ctypes.addressof(ctypes.c_int.from_buffer(maps["cfg"]))
135139
sec_base = ctypes.addressof(ctypes.c_int.from_buffer(maps["sec"]))
136140
self.cfg = Regs(cfg_base)
137141
self.cfg.CFGREG_SW_RSTN = 0x0000
138-
self.cfg.MBOX_IRQ_EN = 0x009c
142+
self.cfg.MBOX_IRQ_EN = 0x009C
139143
self.cfg.HIFI_RUNSTALL = 0x0108
140144
self.cfg.freeze()
141145
self.sec = Regs(sec_base)
142146
self.sec.ALTVEC_C0 = 0x04
143-
self.sec.ALTVECSEL = 0x0c
147+
self.sec.ALTVECSEL = 0x0C
144148
self.sec.freeze()
145149

146150
def logrange(self):
@@ -162,6 +166,7 @@ def start(self, boot_vector):
162166
self.cfg.CFGREG_SW_RSTN &= ~0x11
163167
self.cfg.HIFI_RUNSTALL &= ~0x1000
164168

169+
165170
# Temporary logging protocol: watch the 1M null-terminated log
166171
# stream at 0x60700000 -- the top of the linkable region of
167172
# existing SOF firmware, before the heap. Nothing uses this
@@ -182,30 +187,37 @@ def log(dev):
182187
sys.stdout.buffer.write(msg)
183188
sys.stdout.buffer.flush()
184189

190+
185191
# (Cribbed from cavstool.py)
186192
class Regs:
187193
def __init__(self, base_addr):
188194
vars(self)["base_addr"] = base_addr
189195
vars(self)["ptrs"] = {}
190196
vars(self)["frozen"] = False
197+
191198
def freeze(self):
192199
vars(self)["frozen"] = True
200+
193201
def __setattr__(self, name, val):
194202
if not self.frozen and name not in self.ptrs:
195203
addr = self.base_addr + val
196204
self.ptrs[name] = ctypes.c_uint32.from_address(addr)
197205
else:
198206
self.ptrs[name].value = val
207+
199208
def __getattr__(self, name):
200209
return self.ptrs[name].value
201210

211+
202212
def readfile(f, mode="rb"):
203213
return open(f, mode).read()
204214

215+
205216
def le4(bstr):
206217
assert len(bstr) == 4
207218
return struct.unpack("<I", bstr)[0]
208219

220+
209221
def main():
210222
dsp = detect()
211223
assert dsp
@@ -214,13 +226,18 @@ def main():
214226
mmio = mappings()
215227

216228
# Open device and establish mappings
217-
devmem_fd = open("/dev/mem", "wb+")
218-
for mp in mmio:
219-
paddr = mmio[mp][0]
220-
mapsz = mmio[mp][1]
221-
mapsz = int((mapsz + 4095) / 4096) * 4096
222-
maps[mp] = mmap.mmap(devmem_fd.fileno(), mapsz, offset=paddr,
223-
flags=mmap.MAP_SHARED, prot=mmap.PROT_WRITE|mmap.PROT_READ)
229+
with open("/dev/mem", "wb+") as devmem_fd:
230+
for mp in mmio:
231+
paddr = mmio[mp][0]
232+
mapsz = mmio[mp][1]
233+
mapsz = int((mapsz + 4095) / 4096) * 4096
234+
maps[mp] = mmap.mmap(
235+
devmem_fd.fileno(),
236+
mapsz,
237+
offset=paddr,
238+
flags=mmap.MAP_SHARED,
239+
prot=mmap.PROT_WRITE | mmap.PROT_READ,
240+
)
224241

225242
if dsp == "mt8195":
226243
dev = MT8195(maps)
@@ -230,12 +247,14 @@ def main():
230247
dev = MT8196(maps)
231248

232249
if sys.argv[1] == "load":
233-
dat = open(sys.argv[2], "rb").read()
234-
assert le4(dat[0:4])== FILE_MAGIC
250+
dat = None
251+
with open(sys.argv[2], "rb") as f:
252+
dat = f.read()
253+
assert le4(dat[0:4]) == FILE_MAGIC
235254
sram_len = le4(dat[4:8])
236255
boot_vector = le4(dat[8:12])
237-
sram = dat[12:12+sram_len]
238-
dram = dat[12 + sram_len:]
256+
sram = dat[12 : 12 + sram_len]
257+
dram = dat[12 + sram_len :]
239258
assert len(sram) <= mmio["sram"][1]
240259
assert len(dram) <= mmio["dram1"][1]
241260

@@ -246,7 +265,7 @@ def main():
246265
# pylint: disable=consider-using-enumerate
247266
for i in range(sram_len):
248267
maps["sram"][i] = sram[i]
249-
#for i in range(sram_len, mmio["sram"][1]):
268+
# for i in range(sram_len, mmio["sram"][1]):
250269
# maps["sram"][i] = 0
251270
for i in range(len(dram)):
252271
maps["dram1"][i] = dram[i]
@@ -266,5 +285,6 @@ def main():
266285
else:
267286
print(f"Usage: {sys.argv[0]} log | load <file>")
268287

288+
269289
if __name__ == "__main__":
270290
main()

0 commit comments

Comments
 (0)