Wednesday, December 20, 2023

RISC-v on ZC706 Evaluation Board - Part IV: petalinux

 One year ago (Dec 2022) I made two posts to try modelling RISC-v on ZC706:

Then later in this April I post RISC-v on ZC706 Evaluation Board - Part III to dive into generating FPGA bit stream with Vivado 2016.2. It is a bit success, but eventually still running into issue: "./fesvr-zynq pk hello" still get "ERROR: No cores found". As mentioned in the old post, likely that I need to setup PetaLinux. After several busy months, now I get some spare time to tackle this.

fesvr-zynq probably was built from rocket-chip/riscv-tools/riscv-fesvr, but per https://github.com/ucb-bar/fpga-zynq/issues/87, the issue likely is not coming from this binary:

zhemao commented Jul 9, 2018

Sorry, I forgot to update all the binaries in the uramdisk.image.gz.

Could you please try the files in the master branch of fpga-images-zedboard? I've tested them on my board and they seem to work. https://github.com/ucb-bar/fpga-images-zedboard.

But likely https://github.com/ucb-bar/fpga-images-zc706 not updated. So likely what I need is using PetaLinux tools to build this uramdisk.image.gz for fpga-images-zc706.

Since I haven't been touching the zc706 board for a while, also it is now connected to a new Linux host, let me refresh the whole thing for reproducing the problem. With zc706 UART connected to Linux, once power up the board, kernel log shows:

 usb 2-10: new high-speed USB device number 7 using xhci_hcd
 usb 2-10: New USB device found, idVendor=0403, idProduct=6014, bcdDevice= 9.00
 usb 2-10: New USB device strings: Mfr=1, Product=2, SerialNumber=3
 usb 2-10: Product: Digilent USB Device
 usb 2-10: Manufacturer: Digilent
 usb 2-10: SerialNumber: 210251842966
 ftdi_sio 2-10:1.0: FTDI USB Serial Device converter detected
 usb 2-10: Detected FT232H
 usb 2-10: FTDI USB Serial Device converter now attached to ttyUSB0

This ttyUSB0 likely is the Jtag. Per host kernel log ([    5.208150] usb 4-4: cp210x converter now attached to ttyUSB1), the Uart over USB device ttyUSB1 is there no matter the zc706 board is powered on or off. To connect to the Uart, would need terminal like putty(needs XServer) or minicom(runs in shell). I take minicom as I'm connecting the host with ssh. Run: sudo minicom -s to setup serial port.

Once connected, it would prompt zynq login, use root/root which is default credential. If Ethernet cable is connected, then can use ssh to login the board, the default IP is 192.168.1.5 (refer to the fpga-zynq project page).

With the h/w environment ready, now let's start the PetaLinux adventure.

I have downloaded PetaLinux Tools Installer and ZC706 BSP from Xilinx site several months back. The site also provides common images for Embedded Vitis Platforms. And per note:The ‘common image’ packages contain a prebuilt Linux kernel and root file system that can be used with any Zynq™, Zynq™ MP or Versal board for embedded Vitis platform developers. These files need to be copied to an SD flash card along with the platform specific boot image (boot.bin) available from the Vitis™ Embedded Platform page. Also included is the ‘sysroot’ required for embedded Vitis platform application development. The sdk.sh script must to be used to setup the compressed Yocto Project SDK on a Linux development host properly...PetaLinux Tools installation is not necessary to use the common images. Licenses and sources used to create the common image content is provided. This is a bit confusing. Which do we really need? Regarding Vivado v.s. Vitis, Digilent has a page of explanation: Fundamentally, the differences are down to the perspective of the developer using the tool. Vivado offers a hardware-centric approach to designing hardware, while Vitis offers a software-centric approach to developing *both* hardware and software. Sounds like we don't need Vitis stuff? 

To start the journey, I'm following UG1144 - PetaLinux Tools Documentation Reference Guide.

To setup working environment, run:

source <path-to-installed-PetaLinux>/settings.sh

To use PetaLinux SDK, run:

source <path-to-installed-PetaLinux>/components/yocto/buildtools/environment-setup-x86_64-petalinux-linux
The first is creating PetaLinux project with zc706 BSP:
petalinux-create -t project -s <path-to-bsp>

As the user guide said, using -p to specify the platform architecture, such as '-p arm' for Zynq and '-p aarch64' for Zynq UltraScale+ MPSoC, may save a little disk space. Also tftp server is preferred, Without a TFTP service, you cannot download Linux system images to the target system using the U-Boot network/TFTP capabilities. This can be ignored for other boot modes.

The user guide mentioned exporting hardware platform to PetaLinux project, which can be done from Vivado menu File=>Export=>Export Hardware=>choice of 'Pre-synthesis' and 'Include bitstream', no idea, so I selected the 2nd options to include more. This step generates rocketchip_wrapper.xsa file. 

The user guide also mentioned creating project with template, I guess that is for complexity, since I already created project from BSP, I don't see a need of that.

Run 'petalinux-config --get-hw-description ../fpga-zynq/zc706/zc706_rocketchip_ZynqFPGAConfig/rocketchip_wrapper.xsa' to launch system config. There is lot of setting, and likely can keep the default setting per the User Guide.

Run 'petalinux-build' to build images and it will take a while (will perform a lot of download as well), with 4232 tasks in my case.

The project folder will hold pre-built image once created, even before performing the build. Description of images can be found from the README under the root of the project. The build will generate new images to images/linux folder, such as: zynq_fsbl.elf, image.ub, zImage, uImage, u-boot.elf,  system.dtb, and rootfs.tar.gz, which might be the replacement of uramdisk.image.gz? The fpga-zynq project  zc706/fpga-images-zc706 contains files as:
boot.bif
boot.bin
devicetree.dtb
uImage
uramdisk.image.gz
boot_image
|-FSBL.elf
|-rocketchip_wrapper.bit
|-u-boot.elf

So the boot_image folder contains FSBL, FPGA bitstream and U-Boot.

The  uramdisk.image.gz seems is not a normal gzip archive as I cannot open it with 7-zip, therefore cannot confirm whether it is rootfs.tar.gz from the build. Following https://xilinx.github.io/Embedded-Design-Tutorials/docs/2022.1/build/html/docs/Introduction/Zynq7000-EDT/4-linux-for-zynq.html, I packed the boot.bin with following:

cd images/linux
petalinux-package --boot --fsbl zynq_fsbl.elf --fpga system.bit --u-boot u-boot.elf

Then copy the generated "BOOT.BIN" to SD card, yes, the name is all in capital.

Some Acronym for PetaLinux:

FSBL: First Stage BootLoader

PMU: Platform Management Unit, manage and perform the multi-staged booting process

TF-A: Trust Firmware-A, a transparent bare-metal application layer executed in Exception Level 3 (EL3) on the APU.

BIF: Bootgen Image Format

APU: Application Processing Unit

RPU: Realtime Processing Unit

MCS: might stand for Intel Micro Computer Set, which is file ext name for Intel Hex (refer to wikipedia)

Saturday, September 2, 2023

Developing code with WSL: 2 - Install docker

(continue of Developing code with WSL: 1 - Common problem)

The easy way to try docker with WSL would be installing Docker Desktop on host Windows. Some detail can be found from Microsoft tutorial site: Get started with Docker containers on WSL | Microsoft Learn. But if you prefer to decouple Docker with the host OS, just want to install everything under WSL, you can following Docker Linux installation instruction to just install Docker engine, such as instruction here: https://docs.docker.com/engine/install/ubuntu/#install-using-the-repository

When run  ~$ docker run hello-world, might get this:

docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.

If google this, may see this got asked on Stack Overflow: ubuntu - docker:Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

The suggestion there is run: systemctl start docker && systemctl enable docker. Then may get another error message:

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
 

This is due to limitation of older version of WSL which does not support systemd, or systemd flag has not been set in WSL setting. In that case, try: sudo service docker start. Systemd is supported with WSL version 0.67.6 or newer(reference from Microsoft: Systemd support is now available in WSL! - Windows Command Line). As the link mentioned, if checking WSL version with 'wsl --version' failed, it means  you are running the in-Windows version of WSL and need to upgrade to the Store version.

To enable systemd:You will need to edit the wsl.conf file to ensure systemd starts up on boot.Add these lines to the /etc/wsl.conf with sudo:

[boot]
systemd=true

With that, run  ~$ docker run hello-world would get this:

~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
719385e32844: Pull complete
Digest: sha256:dcba6daec718f547568c562956fa47e1b03673dd010fe6ee58ca806767031d1c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

If you prefer not to use 'sudo' for docker operation, may refer to this 'Ask Ubuntu' post. It works for docker 0.5.3 and up. Basically you would need to add the desired user to 'docker' group, like this:

sudo gpasswd -a $USER docker

Then re-login this user if it is the current user. For WSL, you can run 'logout' and you will automatically get re-login. After login, should be able to run docker command without 'sudo'.

Developing code with WSL: 1 - Common problem

<WIP>

The two things Microsoft did recent years I would give a thumb up are WSL and VisualStudio Code. Both are gaining a lot of attraction along all software developers, I believe. Now, I'd like to make some note with WSL first. I had talked about WSL before, as:
Feb 2019: Running Linux on Windows

Jun 2020: Windows10 运行Ubuntu - WSL2

I'm going to create multiple post for what I learned while using WSL. This first one would be some common tips. 

Accessing files cross host Windows and WSL filesystem 

share folder: /mnt/c

running windows application

share git: cache issue

If user frequently or occasionally would switch between Windows and WSL, and run git command, may see that git will refresh index. As explained from this stackoverflow post:

  • The thing you're using here, which Git variously calls the index, the staging area, or the cache, does in fact contain cache data.

  • The cache data that it contains is the result of system calls.

  • The system call data returned by a Linux system is different from the system call data returned by a Windows system.

Especially if have VS-Code installed on Windows, and w/o using WSL extension to access the repo, VS-Code will run the windows native git.exe with the GitLens extension.

Another thing user may need to consider is: under WSL, the host Windows' git.exe is faster than the WSL linux git. So a simple and efficient solution would be set an alias by adding one line in ~/.bashrc file with:

alias git=git.exe

With this, under WSL it will always invoke the Windows git.exe for fetching the index. It's faster, no refresh when switch between Windows and WSL Linux. A side note is this won't work for tcl/expect which is used by the autobld script. This probably is due to the way how WSL is handling running navtive Windows app under WSL. For that, we can take the other approach mentioned in the same stackoverflow post: set environment variable of GIT_INDEX_FILE

 Makefile : Clock skew detected

Makefile : Clock skew detected - Stack Overflow, this is not the case for WSL build. In fact: WSL2 Clock is out of sync with Windows - Stack Overflow. Another easy way to get around this is: close all WSL session, then from a console, issue: wsl --shutdown. Then open a new WSL session, it will automatically sync the time with Windows host.

GUI: Windows 11, or MobaXterm and 32 bit/64bit executable

Refer to Run Linux on Windows - WSL

Thursday, April 6, 2023

RISC-v on ZC706 EVB - Part III: build w/ Win11/WSL2/Vivado 2016.2

 Continue my journey with of RISC-v on ZC706 Evaluation Board - Part II. Finally get some spare time can back on this. Since it may need some effort to get PetaLinux setup and debug the executable issue, I changed my focus to setup Vivado 2016.2 on another Windows 11 + WSL2 PC. Luckily Xilinx_Vivado_SDK_2016.2_0605_1.tar.gz is just around 11.1GB, even much smaller than 2016.4 package. And I chose WSL2 as on Windows 11 it support GUI, and would be less hassle do the build under Linux. However, I do run into several problem with fresh installed WSL2:

  • Got 'libawt_xawt.so: libXrender.so.1: cannot open shared object file: No such file or directory' when running Vivado xsetup to install Vivado. Search from internet shows I need to do "sudo apt-get install libxrender1" , and that did work. Then hit next issue as below:
  • Got 'libawt_xawt.so: libXtst.so.6: cannot open shared object file: No such file or directory', turns out that I need do 'sudo apt install libxtst6'. Then next issue:
  • Got 'libawt_xawt.so: libXi.so.6: cannot open shared object file: No such file or directory', need to do "sudo apt install libxi6"

With that, xsetup is up and running. For installing option, I selected Vivado HL Design Edition as the WebPACK one might not have all features I need, and I don't need DSP support (for choices, may refer to Xilinx 2016.4 WebPACK Vivado and SDK Install on Windows 7 SP1 (css-techhelp.com) for more detail). Then the installation stuck with the flash screen for quite long time, which made me doubt whether it hanged or crashed. Luckily, it is not. After a while, the install option diag shows up, and I can select and continue the installation. At beginning I checked the option for creating Desktop shortcut, but not sure where it will go as I don't have Ubuntu Desktop installed. The installation not proceeding after a while, with log file name showed in console. By checking the log, likely there is issue with the shortcut. So re-run the installer, this time, not select the shortcut and create group box, however, installer still hangs, and log file shows it probably is running tcl script for generating the device list:

Executing script Generating installed device list: /home/Suser/Vivado/2016.2/bin/vivado [-nolog, -nojournal, -mode, batch, -source, /home/$user/.xinstall/Vivado_2016.2/scripts/xlpartinfo.tcl, -tclargs, /home/$user/Vivado/2016.2/data/parts/installed_devices.txt]
Tried 'sudo apt install tcl' but doesn't help. If manually run about  line, will get:
application-specific initialization failed: couldn't load file "librdi_commontasks.so": libncurses.so.5: cannot open shared object file: No such file or directory

Per this link, need to do: sudo apt install libncurses5

With that, I successfully installed Vivado 2016.2 under WSL. Then I start to pull fpga-zynq.git and build with it as described in fpga-zynq: 2) Pushing your rocket modifications to the FPGA, steps as:
  • Start WSL Ubuntu terminal
  • git clone https://github.com/ucb-bar/fpga-zynq.git to /mnt/c/workspace which can be easily accessed from Windows
  • Run sudo apt install openjdk-8-jdk-headless; sudo update-alternatives --install "/usr/bin/java" "java" "/usr/lib/jvm/java-8-openjdk-amd64/bin/java" 1
  • There is a strange thing happened. To install JDK8 under WSL, sometime would need to do sudo apt update, but somehow WSL give me an error as: "Release file for http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease is not valid yet (invalid for another 1d 3h 20min 52s). Updates for this repository will not be applied.". https://itsfoss.com/fix-repository-not-valid-yet-error-ubuntu/ gives me a clue on this: my WSL clock is skewed for some reason and it is lagged more than 1 day. So have to issue 'wsl --shutdown' then restart WSL to fix the problem
  • For fresh WSL, would need do: sudo apt install make
  • cd to git repo zc706 subfolder, run:
    make init-submodules
  • Run below command, may get a lot of warn and error related to sbt, probably just ignore them:
    make rocket
  • If see Cannot run program "dtc", do: sudo apt install device-tree-compiler
  • Once 'make rocket' successfully completed, run Vivado setting script in the installation path such as: source ~/Vivado/2016.2/settings64.sh
  • run  make project, and soon will see something like 'Wrote  : </mnt/c/code/fpga-zynq/zc706/zc706_rocketchip_ZynqFPGAConfig/zc706_rocketchip_ZynqFPGAConfig.srcs/sources_1/bd/system/system.bd>' at the end. 'system.bd' is folder contains system folder.
  • Now run
    make fpga-images-zc706/boot.bin
    will see output like:

Successfully read diagram <system> from BD file </mnt/c/code/fpga-zynq/zc706/zc706_rocketchip_ZynqFPGAConfig/zc706_rocketchip_ZynqFPGAConfig.srcs/sources_1/bd/system/system.bd>

Verilog Output written to : /mnt/c/code/fpga-zynq/zc706/zc706_rocketchip_ZynqFPGAConfig/zc706_rocketchip_ZynqFPGAConfig.srcs/sources_1/bd/system/hdl/system.v
Verilog Output written to : /mnt/c/code/fpga-zynq/zc706/zc706_rocketchip_ZynqFPGAConfig/zc706_rocketchip_ZynqFPGAConfig.srcs/sources_1/bd/system/hdl/system_wrapper.v
Wrote  : </mnt/c/code/fpga-zynq/zc706/zc706_rocketchip_ZynqFPGAConfig/zc706_rocketchip_ZynqFPGAConfig.srcs/sources_1/bd/system/system.bd>
INFO: [BD 41-1029] Generation completed for the IP Integrator block proc_sys_reset_0 .
WARNING: [xilinx.com:ip:processing_system7:5.5-1] system_processing_system7_0_0: The Zynq BFM requires an AXI BFM license to run. Please ensure that you have purchased and setup the AXI BFM license prior to running simulation with this block. Please contact your Xilinx sales office for more information on purchasing this license
INFO: [BD 41-1029] Generation completed for the IP Integrator block processing_system7_0
 

...

Attempting to get a license for feature 'Synthesis' and/or device 'xc7z045'
WARNING: [Common 17-348] Failed to get the license for feature 'Synthesis' and/or device 'xc7z045'
3 Infos, 1 Warnings, 0 Critical Warnings and 1 Errors encountered.
synth_design failed
ERROR: [Common 17-345] A valid license was not found for feature 'Synthesis' and/or device 'xc7z045'. Please run the Vivado License Manager for assistance in determining
which features and devices are licensed for your system.
Resolution: Check the status of your licenses in the Vivado License Manager. For debug help search Xilinx Support for "Licensing FAQ".
INFO: [Common 17-206] Exiting Vivado at Fri Apr  7 19:48:34 2023...
[Fri Apr  7 19:48:34 2023] synth_1 finished
wait_on_run: Time (s): cpu = 00:00:00.15 ; elapsed = 00:00:28 . Memory (MB): peak = 1186.469 ; gain = 0.000 ; free physical = 241 ; free virtual = 1777
# launch_runs impl_1 -to_step write_bitstream
ERROR: [Common 17-70] Application Exception: Failed to launch run 'impl_1' due to failures in the following run(s):
synth_1
These failed run(s) need to be reset prior to launching 'impl_1' again.

...

Likely would need a license to run synth and impl. As mentioned at https://dzone.com/articles/create-a-bootbin-program-an-sd-card-and-boot-a-zc7, The ZC706 has an XC7Z045-2FFG900 C SoC. This chip is not supported in WebPACK (see [link]) like the chip on the ZC702. You can get a 30-day license at [link].

Next, might see 'bootgen: command not found'. This could happen if user didn't run the Xilinx Vivado settings64.sh shell script, or XiLinx SDK wasn't installed. If the SDK is installed, bootgen should be available under Vivado/2016.2/bin folder. Without SDK, make boot.bin would fail. So need to select SDK during installation as below (I only select Zynq-7000 to save space and installing time. Also, not select Acquire or Manage a License Key as I will deal with that separately):

If already have Vivado installed, then it won't allow you to re-install at same location. Would need to launch Vivado, then goto help->Add Design Tools or Devices...->Check SDK and click install.

After all this, finally can successfully build the boot.bin. With this boot.bin copied to SD card, it can successfully boot the PetaLinux (connect the Uart USB, and use root/root to login). But running "./fesvr-zynq pk hello" still get "ERROR: No cores found".

Also tried following the prompted message for the problem 5 mentioned in Risc-v on zc706 evaluation board - Part I: ERROR: [BD_TCL-109] This script was generated using Vivado <2016.2> and is being run in <2022.1> of Vivado. Please run the script in Vivado <2016.2> then open the design in Vivado <2022.1>. Upgrade the design by running "Tools => Report => Report IP Status...", then run write_bd_tcl to create an updated script.

I opened the project file zc706_rocketchip_ZynqFPGAConfig.xpr created by 2016.2 Vivado with 2022.1 Vivado, and keep the project as Read-only while the new version Vivado prompt for choice. It would still treat the board type as error:

  • [Board 49-67] The board_part definition was not found for xilinx.com:zc706:part0:1.0. This can happen sometimes when you use custom board part. You can resolve this issue by setting 'board.repoPaths' parameter, pointing to the location of custom board files. Valid board_part values can be retrieved with the 'get_board_parts' Tcl command.

For this, no need to update update zc706/src/tcl/zc706_rocketchip_ZynqFPGAConfig.tcl, but update the project xpr file directly as it is XML format, just update <Option Name="BoardPart" Val="xilinx.com:zc706:part0:1.0"/> to xilinx.com:zc706:part0:1.4. For above update instruction, I cannot find 'write_bd_tcl' script, but did find there is a man doc as Vivado/2022.1/doc/eng/man/write_bd_tcl. And 'Report IP' now is under menu/Report but not Tools. Open the Tcl Console (menu/Window->Tcl Console), issue command write_bd_tcl with output filename. It may prompt:  ERROR: [BD 5-229] Please open or create a block design first. So need to select from left Flow Navigator->IP INTEGRATOR-> Open Block Design before running the write_bd_tcl command.



Saturday, March 4, 2023

CodeFormer: AI Face Enhancement, 图片人脸智能增强

最近看到这个由周尚长博士领队的几位南洋理工大学S-Lab成员开发的开源程序,用来恢复和增强照片中的人脸,效果惊人。可以点进以下3分半的you-tube看一下就知我所言不虚(包括1927 Fifth Solvay International Conference群英会的照片)。

 

Face Restoration

Face Color Enhancement and Restoration

Face Inpainting

 

https://shangchenzhou.com/projects/CodeFormer/ 是项目主页,有些简单介绍。code的github网址是:https://github.com/sczhou/CodeFormer,附有简单的安装使用说明,除了要git clone代码,和下载样本数据外,主要是要设置conda。关于conda, 可参见:学习TensorFlow - 设置Python环境。Miniconda就足够了(参见https://stackoverflow.com/questions/45421163/anaconda-vs-miniconda)

注意,CUDA不是必须的,像我的电脑是AMD APU,不支持nVida的CUDA,也可以完美的运行CodeFormer。

当你用CodeFormer转化自己的照片时,最初可能并不会注意到转化后的照片有什么不一样,记住,一定要zoom in,放大再放大,看细节,一定有惊喜。

除了增强照片,Codeformer还支持video的人脸增强(需要引入FFMpeg)。有空要研究研究怎样加入AMD ROMc的支持,加快在我的AMD计算机上的处理速度。

 

 

Wednesday, January 4, 2023

Setup ssh tunnel/port forward on Windows

 Reference link:

https://docs.oracle.com/cd/E24628_01/install.121/e22624/preinstall_req_cygwin_ssh.htm

https://linuxize.com/post/how-to-setup-ssh-tunneling/

Make sure for Cygwin, has openssh installed. Then run
ssh-host-config

*** Query: Enter the value of CYGWIN for the daemon: [] binmode ntsec

Then run this to start the service: cygrunsrv -S sshd

May need to change Windows firewall setting to open the port 22.

 

SSH tunneling or SSH port forwarding is a method of creating an encrypted SSH connection between a client and a server machine through which services ports can be relayed.

There are three types of SSH port forwarding:

  • Local Port Forwarding. - Forwards a connection from the client host to the SSH server host and then to the destination host port.
  • Remote Port Forwarding. - Forwards a port from the server host to the client host and then to the destination host port.
  • Dynamic Port Forwarding. - Creates a SOCKS proxy server that allows communication across a range of ports.