<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE sections SYSTEM "/dtd/book.dtd">

<!-- The content of this document is licensed under the CC-BY-SA license -->
<!-- See http://creativecommons.org/licenses/by-sa/2.5 -->

<!-- $Header: /var/cvsroot/gentoo/xml/htdocs/doc/en/handbook/draft/2006.0/hb-install-mips-kernel.xml,v 1.2 2006/02/12 14:31:58 nightmorph Exp $ -->

<sections>

<version>1.15</version>
<date>2006-02-12</date>

<section>
<title>Timezone</title>
<body>

<p>
You first need to select your timezone so that your system knows where it is
located. Look for your timezone in <path>/usr/share/zoneinfo</path>, then copy
it to <path>/etc/localtime</path>. Please avoid the
<path>/usr/share/zoneinfo/Etc/GMT*</path> timezones as their names do not
indicate the expected zones. For instance, <path>GMT-8</path> is in fact GMT+8.
</p>

<pre caption="Setting the timezone information to GMT">
# <i>cp /usr/share/zoneinfo/GMT /etc/localtime</i>
</pre>

</body>
</section>
<section>
<title>Installing the Sources</title>
<subsection>
<title>Choosing a Kernel</title>
<body>

<p>
The core around which all distributions are built is the Linux kernel. It is the
layer between the user programs and your system hardware. Gentoo provides its
users several possible kernel sources. A full listing with description is
available at the <uri link="/doc/en/gentoo-kernel.xml">Gentoo Kernel
Guide</uri>. 
</p>

<p>
MIPS-based systems have just the one kernel tree to choose from, <c>mips-sources</c>.
Special patches are needed to make certain systems work.  The table below
displays the USE flags required to enable the patchsets.  Note that Indy,
Indigo2 (R4k) and Challenge S systems do not require any additional patchsets.
</p>

<table>
<tr>
  <th>USE flag</th>
  <th>System</th>
</tr>
<tr>
  <ti><c>cobalt</c></ti>
  <ti>Cobalt Qube/RaQ support</ti>
</tr>
<tr>
  <ti><c>ip27</c></ti>
  <ti>SGI Origin 200/2000 support (1)</ti>
</tr>
<tr>
  <ti><c>ip28</c></ti>
  <ti>SGI Indigo2 Impact (R10000) support (1,2)</ti>
</tr>
<tr>
  <ti><c>ip30</c></ti>
  <ti>SGI Octane/Octane2 support (1)</ti>
</tr>
</table>

<note>
The <c>USE="-doc"</c> disables the generation of kernel API documentation, which
would otherwise pull in numerous dependancies not required for this step.
</note>

<pre caption="Setting USE flags and merging kernel sources...">
<comment>(Substitute cobalt with the appropriate USE flag for your system if required)</comment>
# <i>mkdir /etc/portage</i>
# <i>echo "sys-kernel/mips-sources cobalt" &gt;&gt; /etc/portage/package.use</i>

<comment>(Unpack &amp; install the kernel sources)</comment>
# <i>USE="-doc" emerge mips-sources</i>
</pre>

<impo>
(1) On the Origin 200/2000, Indigo2 Impact (R10000), Octane/Octane2 and O2, a
64-bit kernel is required to boot these systems.  For these machines, you should
switch to the <path>default-linux/mips/mips64/RELEASE</path>
profile by altering the <path>/etc/make.profile</path> symlink.  Then you can
<c>emerge gcc-mips64</c> to create a cross-compiler for building 64-bit kernels.
</impo>

<impo>
(2) The Indigo2 Impact is a special case in that it requires special patch to
be applied to <c>gcc</c> before it will build an IP28 kernel correctly.  This
patch adds support for the <c>-mip28-cache-barriers</c> CFLAG, which is used 
when building kernels to work around the inherent hardware bug brought on by
speculative execution.  This USE flag can be set in
<path>/etc/make.conf</path>.  This is done for you by setting your profile to
<path>default-linux/mips/mips64/ip28/RELEASE</path>, which also
takes care of (1).
</impo>

<pre caption="Changing Profiles and installing gcc-mips64...">
<comment>(This assumes PORTDIR is in the usual location; /usr/portage)</comment>
# <i>rm /etc/make.profile</i>
# <i>ln -s /usr/portage/profiles/default-linux/mips/mips64/2005.0</i> <comment>(or .../mips64/ip28/2005.0)</comment>

<comment>(Install the 64-bit kernel toolchain)</comment>
# <i>emerge gcc-mips64</i>
</pre>

<p>
When you take a look in <path>/usr/src</path> you should see a symlink called
<path>linux</path> pointing to your kernel source. In this case, the installed
kernel source points to <c>mips-sources-2.4.24</c>. Your version may be
different, so keep this in mind.
</p>

<pre caption="Viewing the kernel source symlink">
# <i>ls -l /usr/src/linux</i>
lrwxrwxrwx    1 root     root           12 Oct 13 11:04 /usr/src/linux -&gt; linux-2.6.13.4
</pre>

<p>
If this isn't the case (i.e. the symlink points to a different kernel source) 
change the symlink before you continue:
</p>

<pre caption="Changing the kernel source symlink">
# <i>rm /usr/src/linux</i>
# <i>cd /usr/src</i>
# <i>ln -s linux-2.6.13.4 linux</i>
</pre>

<p>
Now it is time to configure and compile your kernel source. 
</p>

</body>
</subsection>
</section>
<section>
<title>Kernel Compilation &amp; Installation</title>
<subsection>
<title>Introduction</title>
<body>

<p>
Previously, we went through the manual configuration of how to set up the kernel
sources.  This has become impractical with the number of systems we now support.
This section details various sources for sample kernel configurations.
</p>

</body>
</subsection>
<subsection>
<title>Using sample configurations in the kernel source</title>
<body>

<p>
Many of the systems supported have sample .configs hiding in amongst the kernel
source.  Not all systems have configs distributed in this way.  Those that do,
can be configured using the commands mentioned in the table below.
</p>

<table>
<tr>
  <th>System</th>
  <th>Configure command</th>
</tr>
<tr>
  <ti>Cobalt Servers</ti>
  <ti><c>make cobalt_defconfig</c></ti>
</tr>
<tr>
  <ti>Indy, Indigo2 (R4k), Challenge S</ti>
  <ti><c>make ip22_defconfig</c></ti>
</tr>
<tr>
  <ti>Origin 200/2000</ti>
  <ti><c>make ip27_defconfig</c></ti>
</tr>
<tr>
  <ti>Indigo2 Impact (R10k)</ti>
  <ti>
    <c>make ip28_defconfig</c> (requires <c>mips-sources-2.6.14.5</c> or
    later)
  </ti>
</tr>
<tr>
  <ti>O2</ti>
  <ti><c>make ip32_defconfig</c></ti>
</tr>
</table>

</body>
</subsection>

<subsection>
<title>Using the running kernel config from the installation media</title>
<body>

<p>
All of the Gentoo installation images provide a kernel config option as part of
the image itself, accessible as <path>/proc/config.gz</path>.  This may be used
in many cases.  It is best though if your kernel source matches closely, the
kernel that is currently running.  To extract it, simply run it through
<c>zcat</c> as shown below.
</p>

<pre caption="Extracting .config from /proc/config.gz">
# <i>zcat /proc/config.gz > .config</i>
</pre>

<impo>
This kernel config is set up for a netboot image.  That
is, it will expect to find a root filesystem image somewhere nearby, either as a
directory for initramfs, or a loopback device for initrd.  When you run <c>make
menuconfig</c> below, don't forget to go into General Setup and disable the
options for initramfs.
</impo>

</body>
</subsection>

<subsection>
<title>The Hardware Compatability Database</title>
<body>

<p>
As an aid to users in finding working settings, a hardware compatability
database was set up.  This database lists the support for various MIPS devices,
and allows users to contribute kernel configurations that are known to work.
The address for this site is
<uri>http://stuartl.longlandclan.hopto.org/gentoo/mips</uri>.
</p>

<p>
If you find this service useful, you're welcome to contribute your notes and
.config files so that others may benefit from your experience.  It should be
noted however that there is no guarantee that any of the configuration files
downloaded from this site will work.
</p>

</body>
</subsection>

<subsection>
<title>Customising the configuration for your needs.</title>
<body>

<p>
Once you have found a configuration, download it into your kernel source
directory, and rename it to <path>.config</path>.  From there, you can run
<c>make oldconfig</c> to bring everything up to date, and allow you to customise
the configuration before compiling.
</p>

<pre caption="Configuring the kernel">
# <i>cd /usr/src/linux</i>
# <i>cp /path/to/example-config .config</i>
# <i>make oldconfig</i>
<comment>(Just press ENTER at each prompt to accept the defaults... we'll customise later)</comment>
# <i>make menuconfig</i>
</pre>

<impo>
In the Kernel Hacking section, there is an option named "Are You Using A Cross
Compiler?".  This tells the kernel Makefiles to prepend "<c>mips-linux-</c>" (or
<c>mipsel-linux</c> ... etc) to <c>gcc</c> and <c>as</c> commands when compiling
the kernel.  This should be turned off, even if cross-compiling.  Instead, if
you do need to call a cross-compiler, specify the prefix using the
<c>CROSS_COMPILE</c> variable as shown in the next section.
</impo>

</body>
</subsection>
<subsection id="compiling">
<title>Compiling and Installing</title>
<body>

<p>
Now that your kernel is configured, it is time to compile and install it. Exit 
the configuration and start the compilation process:
</p>

<note>
On 64-bit machines, you need to specify
<c>CROSS_COMPILE=mips64-unknown-linux-gnu-</c> (or <c>mips64el-...</c> if on
a little-endian system) to use the 64-bit compiler.
</note>

<pre caption="Compiling the kernel">
<comment>(For 2.4 kernel only)</comment>
# <i>make dep</i>

<comment>(2.4 and 2.6 kernels -- compiling natively)</comment>
# <i>make vmlinux modules modules_install</i>

<comment>(2.4 and 2.6 kernels -- cross-compiling on target machine)</comment>
<comment>(    Adjust the mips64-unknown-linux-gnu- accordingly    )</comment>
# <i>make vmlinux modules modules_install CROSS_COMPILE=mips64-unknown-linux-gnu-</i>

<comment>(When compiling on another machine, such as an x86 box... use the)</comment>
<comment>( following commands to compile the kernel &amp; install modules into)</comment>
<comment>(  a specific directory to be transferred to the target machine. )</comment>
# <i>make vmlinux modules CROSS_COMPILE=mips64-unknown-linux-gnu-</i>
# <i>make modules_install INSTALL_MOD_PATH=/somewhere</i>
</pre>

<impo>
When compiling a 64-bit kernel for the Indy, Indigo2 (R4k), Challenge S and O2,
use the <c>vmlinux.32</c> target instead of <c>vmlinux</c>.  Otherwise, your
machine will not be able to boot.  This is to work around the PROM not
understanding the ELF64 format.
</impo>

<pre caption="Using the vmlinux.32 target">
# <i>make vmlinux.32</i>
<comment>(This will create vmlinux.32 -- which is your final kernel)</comment>
</pre>

<p>
When the kernel has finished compiling, copy the kernel image to
<path>/boot</path>. 
</p>

<note>
On Cobalt servers, the bootloader will expect to see a compressed kernel image.
Remember to <c>gzip -9</c> the file once it is in <path>/boot</path>.
</note>

<pre caption="Installing the kernel">
# <i>cp vmlinux /boot/kernel-2.6.13.4</i>

<comment>(Cobalt Servers -- Compressing the kernel image)</comment>
# <i>gzip -9v /boot/kernel-2.6.13.4</i>
</pre>

<!--
Moot point, now that we use arcboot on SGI, and CoLo on Cobalts
<p>
If your system doesn't boot ELF kernels, compile the
kernel using <c>make vmlinux.ecoff</c> instead of <c>make vmlinux</c>. The
kernel image will be saved as <path>arch/mips/boot/vmlinux.ecoff</path> instead
of <path>vmlinux</path>.
</p>
-->
</body>
</subsection>
</section>
<section id="kernel_modules">
<title>Installing Separate Kernel Modules</title>
<subsection>
<title>Configuring the Modules</title>
<body>

<p>
You should list the modules you want automatically loaded in one of the relevant
files listed below.  You can add extra options to the modules too if you want.
</p>

<ul>
  <li><path>/etc/modules.autoload.d/kernel-2.4</path> - Kernel 2.4 only</li>
  <li><path>/etc/modules.autoload.d/kernel-2.6</path> - Kernel 2.6 only</li>
  <li><path>/etc/modules.autoload</path> - All Kernels</li>
</ul>

<p>
Therefore, if you have a kernel module that you want to load -- which is common
to both 2.4 and 2.6, then place it in <path>/etc/modules.autoload</path>.
Otherwise it can be placed in the appropriate file.
</p>

<p>
To view all available modules, run the following <c>find</c> command. Don't
forget to substitute "&lt;kernel version&gt;" with the version of the kernel you
just compiled:
</p>

<pre caption="Viewing all available modules">
# <i>find /lib/modules/&lt;kernel version&gt;/ -type f -iname '*.o' -or -iname '*.ko'</i>
</pre>

<p>
For instance, to automatically load the <c>3c59x.o</c> module, edit the
appropriate file and enter the module name in it.
</p>

<pre caption="Editing /etc/modules.autoload.d/kernel-2.4">
# <i>nano -w /etc/modules.autoload.d/kernel-2.4</i>
</pre>

<pre caption="/etc/modules.autoload.d/kernel-2.4 or kernel-2.6">
3c59x
</pre>

<p>
This can be also done in a one-liner:
</p>

<pre caption="Adding a module -- the one-liner method">
# <i>echo '3c59x' >> /etc/modules.autoload.d/kernel-2.4</i>
<comment>(or kernel-2.6, or /etc/modules.autoload...)</comment>
</pre>

<p>
Continue the installation with <uri link="?part=1&amp;chap=8">Configuring 
your System</uri>.
</p>

</body>
</subsection>
</section>
</sections>
