diff options
Diffstat (limited to 'meta-xilinx-core/classes/dfx_user_dts.bbclass')
-rw-r--r-- | meta-xilinx-core/classes/dfx_user_dts.bbclass | 267 |
1 files changed, 267 insertions, 0 deletions
diff --git a/meta-xilinx-core/classes/dfx_user_dts.bbclass b/meta-xilinx-core/classes/dfx_user_dts.bbclass new file mode 100644 index 00000000..4404aa05 --- /dev/null +++ b/meta-xilinx-core/classes/dfx_user_dts.bbclass | |||
@@ -0,0 +1,267 @@ | |||
1 | # This bbclass is inherited by flat, DFx Static and DFx RP firmware recipes. | ||
2 | # dfx_user_dts.bbclass expects user to generate pl dtsi for flat, DFx Static | ||
3 | # and DFx RP xsa outside of yocto. | ||
4 | |||
5 | inherit devicetree | ||
6 | |||
7 | DEPENDS = "dtc-native bootgen-native" | ||
8 | |||
9 | # recipes that inherit from this class need to use an appropriate machine | ||
10 | # override for COMPATIBLE_MACHINE to build successfully; don't allow building | ||
11 | # for microblaze MACHINE | ||
12 | COMPATIBLE_MACHINE ?= "^$" | ||
13 | COMPATIBLE_MACHINE:microblaze = "^$" | ||
14 | |||
15 | PACKAGE_ARCH = "${MACHINE_ARCH}" | ||
16 | |||
17 | PROVIDES = "" | ||
18 | |||
19 | do_fetch[cleandirs] = "${B}" | ||
20 | |||
21 | DT_PADDING_SIZE = "0x1000" | ||
22 | BOOTGEN_FLAGS ?= " -arch ${SOC_FAMILY} -w ${@bb.utils.contains('SOC_FAMILY','zynqmp','','-process_bitstream bin',d)}" | ||
23 | |||
24 | S ?= "${WORKDIR}" | ||
25 | FW_DIR ?= "" | ||
26 | DTSI_PATH ?= "" | ||
27 | DTBO_PATH ?= "" | ||
28 | DT_FILES_PATH = "${S}/${DTSI_PATH}" | ||
29 | FIRMWARE_NAME_DT_FILE ?= "" | ||
30 | USER_DTS_FILE ?= "" | ||
31 | |||
32 | FIRMWARE_NAME_DT_FILE[doc] = "DT file which has firmware-name device-tree property" | ||
33 | USER_DTS_FILE[doc] = "Final DTSI or DTS file which is used for packaging final DT overlay" | ||
34 | |||
35 | python() { | ||
36 | import re | ||
37 | soc_family = d.getVar("SOC_FAMILY") | ||
38 | if "git://" in d.getVar("SRC_URI") or "https://" in d.getVar("SRC_URI"): | ||
39 | d.setVar("S",'${WORKDIR}/git/'+d.getVar("FW_DIR")) | ||
40 | else: | ||
41 | dtsi_found = False | ||
42 | dtbo_found = False | ||
43 | bit_found = False | ||
44 | bin_found = False | ||
45 | pdi_found = False | ||
46 | |||
47 | # Required Inputs | ||
48 | if '.dtsi' in d.getVar("SRC_URI") or '.dts' in d.getVar("SRC_URI"): | ||
49 | dtsi_found = True | ||
50 | d.setVar("DTSI_PATH",os.path.dirname([a for a in d.getVar('SRC_URI').split() if '.dtsi' in a or '.dts' in a][0].lstrip('file://'))) | ||
51 | |||
52 | if '.dtbo' in d.getVar("SRC_URI"): | ||
53 | dtbo_found = True | ||
54 | d.setVar("DTBO_PATH",os.path.dirname([a for a in d.getVar('SRC_URI').split() if '.dtbo' in a][0].lstrip('file://'))) | ||
55 | |||
56 | if '.bit' in d.getVar("SRC_URI") and soc_family != "versal": | ||
57 | bit_found = True | ||
58 | d.setVar("BIT_PATH",os.path.dirname([a for a in d.getVar('SRC_URI').split() if '.bit' in a][0].lstrip('file://'))) | ||
59 | |||
60 | if '.bin' in d.getVar("SRC_URI") and soc_family != "versal": | ||
61 | bin_found = True | ||
62 | d.setVar("BIT_PATH",os.path.dirname([a for a in d.getVar('SRC_URI').split() if '.bin' in a][0].lstrip('file://'))) | ||
63 | |||
64 | if '.pdi' in d.getVar("SRC_URI") and soc_family == "versal": | ||
65 | pdi_found = True | ||
66 | d.setVar("PDI_PATH",os.path.dirname([a for a in d.getVar('SRC_URI').split() if '.pdi' in a][0].lstrip('file://'))) | ||
67 | |||
68 | # Check for valid combination of input files in SRC_URI | ||
69 | if dtsi_found or dtbo_found: | ||
70 | bb.debug(2, "dtsi or dtbo found in SRC_URI") | ||
71 | if bit_found or pdi_found or bin_found: | ||
72 | bb.debug(2, "bitstream or pdi found in SRC_URI") | ||
73 | elif bit_found and bin_found: | ||
74 | raise bb.parse.SkipRecipe("Both '.bit' and '.bin' file found in SRC_URI, either .bit or .bin file is supported but not both.") | ||
75 | else: | ||
76 | raise bb.parse.SkipRecipe("Need one '.bit' or one '.pdi' file added to SRC_URI ") | ||
77 | else: | ||
78 | raise bb.parse.SkipRecipe("Need one '.dtsi' or one '.dtbo' file added to SRC_URI ") | ||
79 | |||
80 | # Check for valid combination of dtsi and dts files in SRC_URI | ||
81 | # Following file combinations are not supported use case. | ||
82 | # 1. More than one '.dtsi' and zero '.dts' file. | ||
83 | # 2. More than one '.dts' and zero or more than one '.dtsi'file . | ||
84 | pattern_dts = re.compile(r'[.]+dts\b') | ||
85 | pattern_dtsi = re.compile(r'[.]+dtsi\b') | ||
86 | dts_count = len([*re.finditer(pattern_dts, d.getVar('SRC_URI'))]) | ||
87 | dtsi_count = len([*re.finditer(pattern_dtsi, d.getVar("SRC_URI"))]) | ||
88 | |||
89 | if dtsi_count > 1 and dts_count == 0: | ||
90 | raise bb.parse.SkipRecipe("Recipe has more than one '.dtsi' and zero '.dts' found, this is an unsupported use case") | ||
91 | elif dts_count > 1: | ||
92 | raise bb.parse.SkipRecipe("Recipe has more than one '.dts' and zero or more than one '.dtsi' found, this is an unsupported use case") | ||
93 | |||
94 | # Optional input | ||
95 | if '.json' in d.getVar("SRC_URI"): | ||
96 | d.setVar("JSON_PATH",os.path.dirname([a for a in d.getVar('SRC_URI').split() if '.json' in a][0].lstrip('file://'))) | ||
97 | |||
98 | if '.xclbin' in d.getVar("SRC_URI"): | ||
99 | d.setVar("XCL_PATH",os.path.dirname([a for a in d.getVar('SRC_URI').split() if '.xclbin' in a][0].lstrip('file://'))) | ||
100 | } | ||
101 | |||
102 | # Function to get dts or dtsi file count. | ||
103 | def get_dt_count(d, dt_ext): | ||
104 | import glob | ||
105 | dt_count = sum(1 for f in glob.iglob((d.getVar('S') + (d.getVar('DTSI_PATH')) + '/*.' + dt_ext),recursive=True) if os.path.isfile(f)) | ||
106 | return dt_count | ||
107 | |||
108 | # Function to search for dt firmware-name property in dts or dtsi file. | ||
109 | python find_firmware_file() { | ||
110 | import glob | ||
111 | pattern_fw = 'firmware-name' | ||
112 | search_count = 0 | ||
113 | for dt_files in glob.iglob((d.getVar('S') + (d.getVar('DTSI_PATH')) + '/*.dts*'),recursive=True): | ||
114 | with open(dt_files, "r") as f: | ||
115 | current_fd = f.read() | ||
116 | if pattern_fw in current_fd: | ||
117 | search_count += 1 | ||
118 | if search_count > 1: | ||
119 | bb.error("firmware-name dt property found in more than one dt files! Please fix the dts or dtsi file.") | ||
120 | break | ||
121 | else: | ||
122 | d.setVar('FIRMWARE_NAME_DT_FILE', os.path.basename(dt_files)) | ||
123 | } | ||
124 | |||
125 | do_configure[prefuncs] += "find_firmware_file" | ||
126 | |||
127 | python do_configure() { | ||
128 | import glob, re, shutil | ||
129 | soc_family = d.getVar("SOC_FAMILY") | ||
130 | |||
131 | if bb.utils.contains('MACHINE_FEATURES', 'fpga-overlay', False, True, d): | ||
132 | bb.warn("Using fpga-manager.bbclass requires fpga-overlay MACHINE_FEATURE to be enabled") | ||
133 | |||
134 | # Renaming firmware-name using $PN as bitstream/PDI will be renamed using | ||
135 | # $PN when generating the bin/pdi file. | ||
136 | if os.path.isfile(d.getVar('S') + (d.getVar('DTSI_PATH') or '') + '/' + d.getVar('FIRMWARE_NAME_DT_FILE')): | ||
137 | orig_dtsi = glob.glob(d.getVar('S')+ (d.getVar('DTSI_PATH') or '') + '/' + d.getVar('FIRMWARE_NAME_DT_FILE'))[0] | ||
138 | new_dtsi = d.getVar('S') + '/pl.dtsi_firmwarename' | ||
139 | with open(new_dtsi, 'w') as newdtsi: | ||
140 | with open(orig_dtsi) as olddtsi: | ||
141 | for line in olddtsi: | ||
142 | if soc_family == 'versal': | ||
143 | newdtsi.write(re.sub('firmware-name.*\".*\"','firmware-name = \"'+d.getVar('PN')+'.pdi\"',line)) | ||
144 | else: | ||
145 | newdtsi.write(re.sub('firmware-name.*\".*\"','firmware-name = \"'+d.getVar('PN')+'.bit.bin\"',line)) | ||
146 | shutil.move(new_dtsi,orig_dtsi) | ||
147 | } | ||
148 | |||
149 | do_compile[prefuncs] += "find_firmware_file" | ||
150 | |||
151 | python devicetree_do_compile:append() { | ||
152 | import glob, subprocess, shutil | ||
153 | soc_family = d.getVar("SOC_FAMILY") | ||
154 | |||
155 | dtbo_count = sum(1 for f in glob.iglob((d.getVar('S') + '/*.dtbo'),recursive=True) if os.path.isfile(f)) | ||
156 | |||
157 | # Skip devicetree do_compile task if input file is dtbo in SRC_URI | ||
158 | if not dtbo_count: | ||
159 | # Convert .bit to bit.bin format only if dtsi is input. | ||
160 | # In case of dtbo as input, bbclass doesn't know if firmware-name is .bit or | ||
161 | # .bit.bin format and corresponding file name. Hence we are not doing | ||
162 | # bit.bin conversion. | ||
163 | if soc_family != 'versal' and glob.glob(d.getVar('S') + '/' + d.getVar('FIRMWARE_NAME_DT_FILE')): | ||
164 | pn = d.getVar('PN') | ||
165 | biffile = pn + '.bif' | ||
166 | |||
167 | with open(biffile, 'w') as f: | ||
168 | f.write('all:\n{\n\t' + glob.glob(d.getVar('S')+(d.getVar('BIT_PATH') or '') + '/*.bit')[0] + '\n}') | ||
169 | |||
170 | bootgenargs = ["bootgen"] + (d.getVar("BOOTGEN_FLAGS") or "").split() | ||
171 | bootgenargs += ["-image", biffile, "-o", pn + ".bit.bin"] | ||
172 | subprocess.run(bootgenargs, check = True) | ||
173 | |||
174 | # In Zynq7k using both "-process_bitstream bin" and "-o" in bootgen flag, | ||
175 | # to convert bit file to bin format, "-o" option will not be effective | ||
176 | # and generated output file name is ${S}+${BIT_PATH}/<bit_file_name>.bit.bin | ||
177 | # file, Hence we need to rename this file from <bit_file_name>.bit.bin to | ||
178 | # ${PN}.bit.bin which matches the firmware name in dtbo and move | ||
179 | # ${PN}.bit.bin to ${B} directory. | ||
180 | if soc_family == 'zynq': | ||
181 | src_bitbin_file = glob.glob(d.getVar('S') + (d.getVar('BIT_PATH') or '') + '/*.bit.bin')[0] | ||
182 | dst_bitbin_file = d.getVar('B') + '/' + pn + '.bit.bin' | ||
183 | shutil.move(src_bitbin_file, dst_bitbin_file) | ||
184 | |||
185 | if not os.path.isfile(pn + ".bit.bin"): | ||
186 | bb.fatal("Couldn't find %s file, Enable '-log trace' in BOOTGEN_FLAGS" \ | ||
187 | "and check bootgen_log.txt" % (d.getVar('B') + '/' + pn + '.bit.bin')) | ||
188 | } | ||
189 | |||
190 | # If user inputs both dtsi and dts files then device-tree will generate dtbo | ||
191 | # files for each dt file, Hence to package the firmware pick the right user dt | ||
192 | # overlay file. | ||
193 | python find_user_dts_overlay_file() { | ||
194 | import glob | ||
195 | dtbo_count = sum(1 for f in glob.iglob((d.getVar('S') + '/*.dtbo'),recursive=True) if os.path.isfile(f)) | ||
196 | # Skip if input file is dtbo in SRC_URI | ||
197 | if not dtbo_count: | ||
198 | dts_count = get_dt_count(d, 'dts') | ||
199 | dtsi_count = get_dt_count(d, 'dtsi') | ||
200 | if dtsi_count == 1 and dts_count == 0: | ||
201 | dts_file =glob.glob(d.getVar('S')+ (d.getVar('DTSI_PATH') or '') + '/*.dtsi')[0] | ||
202 | elif dtsi_count >=0 and dts_count == 1: | ||
203 | dts_file = glob.glob(d.getVar('S')+ (d.getVar('DTSI_PATH') or '') + '/*.dts')[0] | ||
204 | |||
205 | d.setVar('USER_DTS_FILE', os.path.splitext(os.path.basename(dts_file))[0]) | ||
206 | } | ||
207 | |||
208 | do_install[prefuncs] += "find_user_dts_overlay_file" | ||
209 | |||
210 | do_install() { | ||
211 | install -d ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/ | ||
212 | |||
213 | # In case of dtbo as input, dtbo will be copied from directly from ${S} | ||
214 | # In case of dtsi as input, dtbo will be copied from directly from ${B} | ||
215 | if [ `ls ${S}/*.dtbo | wc -l` -eq 1 ]; then | ||
216 | install -Dm 0644 ${S}/*.dtbo ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/ | ||
217 | elif [ `ls ${S}/*.dtbo | wc -l` -gt 1 ]; then | ||
218 | bbfatal "Multiple DTBO found, use the right DTBO in SRC_URI from the following:\n$(basename -a ${S}/*.dtbo)" | ||
219 | elif [ -f ${B}/${USER_DTS_FILE}.dtbo ]; then | ||
220 | install -Dm 0644 ${B}/${USER_DTS_FILE}.dtbo ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/${PN}.dtbo | ||
221 | else | ||
222 | bbfatal "A dtbo ending '.dtbo' expected but not found" | ||
223 | fi | ||
224 | |||
225 | if [ "${SOC_FAMILY}" == "versal" ]; then | ||
226 | # In case of dtbo as input, pdi will be copied from directly from ${S} | ||
227 | # without renaming the pdi name to ${PN}.pdi | ||
228 | if [ `ls ${S}/*.pdi | wc -l` -eq 1 ] && [ `ls ${S}/*.dtbo | wc -l` -eq 1 ]; then | ||
229 | install -Dm 0644 ${S}/*.pdi ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/ | ||
230 | elif [ `ls ${S}/*.pdi | wc -l` -gt 1 ]; then | ||
231 | bbfatal "Multiple PDI found, use the right PDI in SRC_URI from the following:\n$(basename -a ${S}/*.pdi)" | ||
232 | elif [ `ls ${S}/*.pdi | wc -l` -eq 1 ] && [ -f ${B}/${USER_DTS_FILE}.dtbo ]; then | ||
233 | install -Dm 0644 ${S}/*.pdi ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/${PN}.pdi | ||
234 | else | ||
235 | bbfatal "A PDI file with '.pdi' expected but not found" | ||
236 | fi | ||
237 | else | ||
238 | # In case of dtbo as input, .bit or .bin will be copied from directly | ||
239 | # from ${S} without renaming the .bit/.bin name to ${PN}.bit/${PN}.bin | ||
240 | # if more than one .bit/.bin file is found then fail the task. | ||
241 | if [ `ls ${S}/*.bit | wc -l` -gt 1 ]; then | ||
242 | bbfatal "Multiple .bit found, use the right .bit in SRC_URI from the following:\n$(basename -a ${S}/*.bit)" | ||
243 | elif [ `ls ${S}/*.bin | wc -l` -gt 1 ]; then | ||
244 | bbfatal "Multiple .bin found, use the right .bin in SRC_URI from the following:\n$(basename -a ${S}/*.bin)" | ||
245 | elif [ `ls ${S}/*.bit | wc -l` -eq 1 ] && [ `ls ${S}/*.dtbo | wc -l` -eq 1 ]; then | ||
246 | install -Dm 0644 ${S}/*.bit ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/ | ||
247 | elif [ `ls ${S}/*.bin | wc -l` -eq 1 ] && [ `ls ${S}/*.dtbo | wc -l` -eq 1 ]; then | ||
248 | install -Dm 0644 ${S}/*.bin ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/ | ||
249 | elif [ -f ${B}/${PN}.bit.bin ] && [ -f ${B}/${USER_DTS_FILE}.dtbo ]; then | ||
250 | install -Dm 0644 ${B}/${PN}.bit.bin ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/${PN}.bit.bin | ||
251 | else | ||
252 | bbfatal "A bitstream file with '.bit' or '.bin' expected but not found" | ||
253 | fi | ||
254 | fi | ||
255 | |||
256 | if ls ${S}/${XCL_PATH}/*.xclbin >/dev/null 2>&1; then | ||
257 | install -Dm 0644 ${S}/${XCL_PATH}/*.xclbin ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/${PN}.xclbin | ||
258 | fi | ||
259 | |||
260 | if [ -f ${S}/${JSON_PATH}/shell.json ] || [ -f ${S}/${JSON_PATH}/accel.json ]; then | ||
261 | install -Dm 0644 ${S}/${JSON_PATH}/*.json ${D}/${nonarch_base_libdir}/firmware/xilinx/${PN}/ | ||
262 | fi | ||
263 | } | ||
264 | |||
265 | do_deploy[noexec] = "1" | ||
266 | |||
267 | FILES:${PN} += "${nonarch_base_libdir}/firmware/xilinx/${PN}" | ||