Sunday, March 8, 2015

X.org Patched and Packaged for i.MX6 on Debian

In the previous installment, I described how to build a minimal xserver using X.org modular build script, in order to incorporate a supposedly critical patch to xf86drm.h for the arm architecture. Following on to that experiment, X.org will now be installed mostly from .deb packages, using the Debian package builder to create patched versions of libdrm, mesa, and xorg-server. The motivation for this is to avoid creating a system from a mix of source-built and package-installed software, which generally proves to be a pain to maintain. Unfortunately, for accelerated GPU support, the only viable option is the binary package released by Freescale through the Yocto project - this will introduce another set of headaches (always fighting with ldconfig for priority of /usr/lib/libGL.so etc.), due to not being packaged per Debian or Ubuntu standards. 

Take a look at my previous post.for advice on creating a chroot environment - as is highly recommended for package building - to ensure that the packages are built on a clean system with all dependencies correctly met. The primary purpose of the chroot is solely to make software builds. However, it's worth noting that by the time you work through all the steps, you'll have pretty much everything installed in the chroot to actually run the graphical server. In fact, it's even possible to install and run with GPU acceleration in this chroot configuration (but pretty certain that the GPU support should NOT be installed on the clean build system ... see notes on glmark below).

Configuring this system with the minimum amount of package rebuilding, being very careful of the order of packages installed, and ensuring that the patched packages are actually installed, requires a fair amount of familiarity/creativity with dpkg and aptitude which is not given in great detail here ... may improve or simplify as I expand my knowledge of these tools.

Before entering the chroot environment, it is strongly suggested to create the various standard system mount points - they're not always needed but it's a real good idea: 

sudo mkdir $chroot/run/udev # must be created manually

mount --bind /proc $chroot/proc
mount --bind /run/udev $chroot/run/udev
mount --bind /dev $chroot/dev
mount -t devpts none $chroot/dev/pts
mount --bind /sys $chroot/sys # needed for proper 'LoadModule: "evdev"'

# suggest adding a little sugar to shell prompt in the chroot:
# echo >> $chroot/home/user/.bashrc ...
text=" xorg_drm"
export PS1='\[\e[1;32m\]\u@\h:\w${text}$\[\e[m\] '

With the following additions to $chroot/etc/apt/sources.list, xorg-xserver-1.15.0 will be installed (compatibility with Kernel and i.MX6-Vivante-GPU-blobs 3.10.31 series). :

deb http://snapshot.debian.org/archive/debian/20140320T161557Z/ jessie main
deb-src http://snapshot.debian.org/archive/debian/20140320T161557Z/ jessie main

Using some information found over at hackedbellini to incorporate the following setup:

cat $chroot/etc/apt/preferences.d/xorg-xserver
Package: xserver-*
Pin: origin "snapshot.debian.org"
Pin-Priority: 1001

Force mesa-9.2 series (problems encountered with location of .drirc in mesa-10.1.0, which can probably be sorted out with a little more effort):

Package: *mesa-*
Pin: origin "snapshot.debian.org"
Pin-Priority: 1001

Package: *-mesa*
Pin: origin "snapshot.debian.org"
Pin-Priority: 1001

# Switch into the chroot environment and continue setup ... 
# suppress "Valid-Until" warnings from apt-get:
apt-get -o Acquire::Check-Valid-Until=false update 

# Get some prerequisites installed (there may be more):
apt-get install build-essential fakeroot devscripts

The Debian build system is plenty well documented in other places so expect a lot of details to be glossed over. 

On the build system, a lot of packages will be installed by apt-get build-dep, but there are also notes on packages installed explicitly by apt-get install (but be careful in case the information is not complete). 

Building and installing patched libdrm
By default, the package system will build some sub-modules such as nouveau and radeon that aren't needed on i.MX6, or may have to be present to satisfy other package dependencies. See debian/rules and my previous post for ideas on configurable options that can be disabled. Getting on with it ...

cd pkgsrc/libdrm 
apt-get source libdrm
wget https://raw.githubusercontent.com/Freescale/meta-fsl-arm/master/recipes-graphics/drm/libdrm/mx6/drm-update-arm.patch

cd libdrm-2.4.52
dpkg-checkbuilddeps # not a bad idea to check what dependencies will be installed

apt-get build-dep libdrm

cp  ../drm-update-arm.patch debian/patches/80-drm-update-arm.patch
echo 80-drm-update-arm.patch >> debian/patches/series
debuild -b -uc -us

dpkg-buildpackage -b -j4 -us -uc  # alternative to debuild - note -j option to use those cores!

Should see something like this happening in the configuration phase:

Applying patch 10_drm-update-arm.patch
patching file xf86drm.h

At a minimum, install the following from pkgsrc/libdrm (or just do dpkg -i pkgsrc/*\.deb):

dpkg -i libdrm2_2.4.52-1_armhf.deb libdrm-dev_2.4.52-1_armhf.deb libdrm-nouveau2_2.4.52-1_armhf.deb libdrm-radeon1_2.4.52-1_armhf.deb libdrm-omap1_2.4.52-1_armhf.deb

The arm patch should now be merged into /usr/include/xf86drm.h, verify if unsure!

Building mesa against patched system
By default, the package system will build a kitchen-sink Mesalib ... see debian/rules for the configuration options.

mkdir pkgsrc/mesa
cd pkgsrc/mesa
apt-get source mesa

cd mesa-10.1.0  # todo recheck that I have mesa-10 installed

Make sure libdrm dependencies already met - advise verifying that apt agrees that the libdrm dependencies installed by dpkg are actually installed:
dpkg-checkbuilddeps 

apt-get build-dep mesa  # check first with apt-get -s if super paranoid 

debuild -b -uc -us

An odd thing I noticed, was a second configuration phase in the mesa build ... GLESv2 finally appears on the second pass.

        OpenGL:          yes (ES1: no ES2: no)

        OpenGL:          yes (ES1: yes ES2: yes)


If the mesa build completes successfully, the next module to build is xorg-server, which requires a slew of dependencies to be installed. The freshly built mesa libs should be installed ahead of running apt-get build-dep xorg-server. Suggest first running apt-get -s to verify what will be installed. Pretty sure there is a more efficient way but I don't know the tricks yet.

apt-get install libdrm-freedreno1 libelf1 libexpat1  libllvm3.3 libllvm3.5 libwayland-client0 libwayland-server0  libx11-xcb1 libxcb-dri2-0 libxcb-render0 libxcb-shape0 libxcb-xfixes0  libxfixes3

# here's another batch of 'em that can be knocked out now
apt-get install  keyboard-configuration libfontenc1 libfreetype6 libice6 libpciaccess0 libpixman-1-0 libpng12-0 libsm6  libtxc-dxtn-s2tc0 libxaw7 libxcb-glx0 libxdamage1 libxfont1 libxkbfile1  libxmu6 libxpm4 libxshmfence1 libxt6 libxxf86vm1 x11-common x11-xkb-utils  xfonts-base xfonts-encodings xfonts-utils xkb-data 

# keep going 
apt-get install libpthread-stubs0-dev libx11-dev libx11-doc libx11-xcb-dev libxau-dev  libxcb-dri2-0-dev libxcb-glx0-dev libxcb1-dev libxdamage-dev libxdmcp-dev libxext-dev  libxfixes-dev libxxf86vm-dev x11proto-core-dev x11proto-damage-dev x11proto-dri2-dev  x11proto-fixes-dev x11proto-gl-dev x11proto-input-dev x11proto-kb-dev x11proto-xext-dev  x11proto-xf86vidmode-dev xorg-sgml-doctools xtrans-dev

# At some point, the mesa packages can be installed ... or possibly even the whole shebang:
dpkg -i pkgssrc/mesa/*.deb

# expect dpkg to error out on the following, which should be ok.-
# just clean up the staged installs with dpkg -r
 libgl1-mesa-swx11_9.2.2-1_armhf.deb
 libgl1-mesa-swx11-dev_9.2.2-1_armhf.deb
 libgbm-dev
 libgl1-mesa-swx11-dbg:armhf

Building xorg-server against patched system

mkdir pkgsrc/xorg-server

cd pkgsrc/xorg-server

apt-get source xorg-server
cd xorg-server-1.15.0
dpkg-checkbuilddeps

apt-get build-dep xorg-server

debuild -b -uc -us  # dpkg-buildpackage -b -j4 -us -uc

# build-dep should have installed the needed support, but if not:
# double check not all dependencies needed if omitted these: 
# xdmx xdmx-tools xserver-xephyr

apt-get -s install libpciaccess-dev libpixman-1-dev libxkbfile-dev x11proto-dri3-dev x11proto-fonts-dev x11proto-present-dev x11proto-randr-dev x11proto-render-dev x11proto-resource-dev x11proto-scrnsaver-dev x11proto-video-dev x11proto-xf86bigfont-dev x11proto-xf86dri-dev x11proto-xinerama-dev 

Install away ... all except xdmx xdmx-tools xserver-xephyr

Wrapping it up
It doesn't take much more to get a minimal graphical system running - minimum meaning you only get an xterm - but no WM.

apt-get install keyboard-configuration xserver-xorg-video-fbdev xserver-xorg

# Install at least these from the xserver-xorg builds if you 
# haven't already ...

dpkg -i xserver-common_1.15.0-2_all.deb  xserver-xorg-core_1.15.0-2_armhf.deb xserver-xorg-dev_1.15.0-2_armhf.deb

# ...almost there:

apt-get install xinit

Finally, it should be time for the startX moment. Did it work?

What next?
A few more steps are required to enable GPU acceleration in X11 on i.MX6. For bonus points, build .debs for mesa-utils (glxgears, glxinfo) and mesa-utils-extra (es2gears, es2info). They are dependent upon xf86drm.h, although at this time I haven't confirmed the failure mode of the stock packages built against the unpatched header.

AFAIK, source packages not required for these
apt-get install libglew1.10 libglu1-mesa 

apt-get source mesa-utils
apt-get source mesa-utils-extra

Finally, glmark-es2 seems to be available only in source code. Not a problem! Expect complications to arise with the Yocto packages installed on the system e.g. breaks the glmark-es2 build:


In file included from /usr/include/EGL/eglplatform.h:38:0,
                 from /usr/include/EGL/egl.h:39,
                 from ../src/gl-state-egl.h:26,
                 from ../src/main.cpp:48:
/usr/include/EGL/eglvivante.h:242:2: error: #error "Platform not recognized"
 #error "Platform not recognized"

A working build environment for glmark2 can be created on a chroot filesystem, featuring the patched libdrm and those mesa and xorg-server packages (built against the patched libdrm) installed ... DO NOT install the GPU headers or libs from Yocto on your clean chroot filesystem.

To wrap it up, noting the versions of support packages used so far:
http://downloads.yoctoproject.org/mirror/sources/xserver-xorg-video-imx-viv-3.10.31-1.1.0-beta.tar.gz
http://downloads.yoctoproject.org/mirror/sources/gpu-viv-bin-mx6q-3.10.31-1.1.0-beta-hfp.bin