古いPC の再利用: ThinkPad T420s へ FreeBSD をインストール

# ThinkPad T420s の構成

## BIOS 設定変更

ThinkPad T420s は Intel CPU 内臓 GPU (Integrated Graphics) と NVIDIA Quadro NVS (NVS 4200M) が載っています。
BIOS の設定で

- Intel CPU 内臓 GPU だけ
    - `Integrated Graphics`
- NVIDIA Quadro NVS (NVS 4200M)
    - `Discrete Graphics`
- 両方使う
    - `NVIDIA Optimus`

という設定ができますが、FreeBSD のドライバでは両方同時に使うことはできないようです。
そのため、BIOS の
 
- Config -> Display -> Graphics Devices
- Config -> Display -> OS Detection for NVIDIA Optimus

の値をどちらか一つだけを使う設定にしておきます。

| Key | Value |
| ---- | ---- |
| Graphics Devices | `Integrated Graphics` か `Discrete Graphics` のどちらか |
| OS Detection for NVIDIA Optimus | `Disabled` |

## `pciconf -l -v` の実行結果

### `Integrated Graphics` を設定した場合

```
hostb0@pci0:0:0:0:      class=0x060000 card=0x21d217aa chip=0x01048086 rev=0x09 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '2nd Generation Core Processor Family DRAM Controller'
    class      = bridge
    subclass   = HOST-PCI
vgapci0@pci0:0:2:0:     class=0x030000 card=0x21d317aa chip=0x01268086 rev=0x09 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '2nd Generation Core Processor Family Integrated Graphics Controller'
    class      = display
    subclass   = VGA
none0@pci0:0:22:0:      class=0x078000 card=0x21d217aa chip=0x1c3a8086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family MEI Controller'
    class      = simple comms
uart2@pci0:0:22:3:      class=0x070002 card=0x21d217aa chip=0x1c3d8086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family KT Controller'
    class      = simple comms
    subclass   = UART
em0@pci0:0:25:0:        class=0x020000 card=0x21ce17aa chip=0x15028086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '82579LM Gigabit Network Connection (Lewisville)'
    class      = network
    subclass   = ethernet
ehci0@pci0:0:26:0:      class=0x0c0320 card=0x21d217aa chip=0x1c2d8086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family USB Enhanced Host Controller'
    class      = serial bus
    subclass   = USB
hdac0@pci0:0:27:0:      class=0x040300 card=0x21d217aa chip=0x1c208086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family High Definition Audio Controller'
    class      = multimedia
    subclass   = HDA
pcib1@pci0:0:28:0:      class=0x060400 card=0x21d217aa chip=0x1c108086 rev=0xb4 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family PCI Express Root Port 1'
    class      = bridge
    subclass   = PCI-PCI
pcib2@pci0:0:28:1:      class=0x060400 card=0x21d217aa chip=0x1c128086 rev=0xb4 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family PCI Express Root Port 2'
    class      = bridge
    subclass   = PCI-PCI
pcib3@pci0:0:28:3:      class=0x060400 card=0x21d217aa chip=0x1c168086 rev=0xb4 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family PCI Express Root Port 4'
    class      = bridge
    subclass   = PCI-PCI
pcib4@pci0:0:28:4:      class=0x060400 card=0x21d217aa chip=0x1c188086 rev=0xb4 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family PCI Express Root Port 5'
    class      = bridge
    subclass   = PCI-PCI
ehci1@pci0:0:29:0:      class=0x0c0320 card=0x21d217aa chip=0x1c268086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family USB Enhanced Host Controller'
    class      = serial bus
    subclass   = USB
isab0@pci0:0:31:0:      class=0x060100 card=0x21d217aa chip=0x1c4f8086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = 'QM67 Express Chipset LPC Controller'
    class      = bridge
    subclass   = PCI-ISA
ahci0@pci0:0:31:2:      class=0x010601 card=0x21d217aa chip=0x1c038086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family 6 port Mobile SATA AHCI Controller'
    class      = mass storage
    subclass   = SATA
none1@pci0:0:31:3:      class=0x0c0500 card=0x21d217aa chip=0x1c228086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family SMBus Controller'
    class      = serial bus
    subclass   = SMBus
iwn0@pci0:3:0:0:        class=0x028000 card=0x13118086 chip=0x00898086 rev=0x5e hdr=0x00
    vendor     = 'Intel Corporation'
    device     = 'Centrino Advanced-N + WiMAX 6250 [Kilmer Peak]'
    class      = network
sdhci_pci0@pci0:5:0:0:  class=0x088000 card=0x21d217aa chip=0xe8231180 rev=0x04 hdr=0x00
    vendor     = 'Ricoh Co Ltd'
    device     = 'PCIe SDXC/MMC Host Controller'
    class      = base peripheral
xhci0@pci0:13:0:0:      class=0x0c0330 card=0x21d217aa chip=0x01941033 rev=0x04 hdr=0x00
    vendor     = 'NEC Corporation'
    device     = 'uPD720200 USB 3.0 Host Controller'
    class      = serial bus
    subclass   = USB
```

### `Discrete Graphics` を設定した場合

```
hostb0@pci0:0:0:0:      class=0x060000 card=0x21d217aa chip=0x01048086 rev=0x09 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '2nd Generation Core Processor Family DRAM Controller'
    class      = bridge
    subclass   = HOST-PCI
pcib1@pci0:0:1:0:       class=0x060400 card=0x21d217aa chip=0x01018086 rev=0x09 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = 'Xeon E3-1200/2nd Generation Core Processor Family PCI Express Root Port'
    class      = bridge
    subclass   = PCI-PCI
none0@pci0:0:22:0:      class=0x078000 card=0x21d217aa chip=0x1c3a8086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family MEI Controller'
    class      = simple comms
uart2@pci0:0:22:3:      class=0x070002 card=0x21d217aa chip=0x1c3d8086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family KT Controller'
    class      = simple comms
    subclass   = UART
em0@pci0:0:25:0:        class=0x020000 card=0x21ce17aa chip=0x15028086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '82579LM Gigabit Network Connection (Lewisville)'
    class      = network
    subclass   = ethernet
ehci0@pci0:0:26:0:      class=0x0c0320 card=0x21d217aa chip=0x1c2d8086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family USB Enhanced Host Controller'
    class      = serial bus
    subclass   = USB
hdac1@pci0:0:27:0:      class=0x040300 card=0x21d217aa chip=0x1c208086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family High Definition Audio Controller'
    class      = multimedia
    subclass   = HDA
pcib2@pci0:0:28:0:      class=0x060400 card=0x21d217aa chip=0x1c108086 rev=0xb4 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family PCI Express Root Port 1'
    class      = bridge
    subclass   = PCI-PCI
pcib3@pci0:0:28:1:      class=0x060400 card=0x21d217aa chip=0x1c128086 rev=0xb4 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family PCI Express Root Port 2'
    class      = bridge
    subclass   = PCI-PCI
pcib4@pci0:0:28:3:      class=0x060400 card=0x21d217aa chip=0x1c168086 rev=0xb4 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family PCI Express Root Port 4'
    class      = bridge
    subclass   = PCI-PCI
pcib5@pci0:0:28:4:      class=0x060400 card=0x21d217aa chip=0x1c188086 rev=0xb4 hdr=0x01
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family PCI Express Root Port 5'
    class      = bridge
    subclass   = PCI-PCI
ehci1@pci0:0:29:0:      class=0x0c0320 card=0x21d217aa chip=0x1c268086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family USB Enhanced Host Controller'
    class      = serial bus
    subclass   = USB
isab0@pci0:0:31:0:      class=0x060100 card=0x21d217aa chip=0x1c4f8086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = 'QM67 Express Chipset LPC Controller'
    class      = bridge
    subclass   = PCI-ISA
ahci0@pci0:0:31:2:      class=0x010601 card=0x21d217aa chip=0x1c038086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family 6 port Mobile SATA AHCI Controller'
    class      = mass storage
    subclass   = SATA
none1@pci0:0:31:3:      class=0x0c0500 card=0x21d217aa chip=0x1c228086 rev=0x04 hdr=0x00
    vendor     = 'Intel Corporation'
    device     = '6 Series/C200 Series Chipset Family SMBus Controller'
    class      = serial bus
    subclass   = SMBus
vgapci0@pci0:1:0:0:     class=0x030000 card=0x21d217aa chip=0x105610de rev=0xa1 hdr=0x00
    vendor     = 'NVIDIA Corporation'
    device     = 'GF119M [NVS 4200M]'
    class      = display
    subclass   = VGA
hdac0@pci0:1:0:1:       class=0x040300 card=0x21d217aa chip=0x0e0810de rev=0xa1 hdr=0x00
    vendor     = 'NVIDIA Corporation'
    device     = 'GF119 HDMI Audio Controller'
    class      = multimedia
    subclass   = HDA
iwn0@pci0:3:0:0:        class=0x028000 card=0x13118086 chip=0x00898086 rev=0x5e hdr=0x00
    vendor     = 'Intel Corporation'
    device     = 'Centrino Advanced-N + WiMAX 6250 [Kilmer Peak]'
    class      = network
sdhci_pci0@pci0:5:0:0:  class=0x088000 card=0x21d217aa chip=0xe8231180 rev=0x04 hdr=0x00
    vendor     = 'Ricoh Co Ltd'
    device     = 'PCIe SDXC/MMC Host Controller'
    class      = base peripheral
xhci0@pci0:13:0:0:      class=0x0c0330 card=0x21d217aa chip=0x01941033 rev=0x04 hdr=0x00
    vendor     = 'NEC Corporation'
    device     = 'uPD720200 USB 3.0 Host Controller'
    class      = serial bus
    subclass   = USB
```

# FreeBSD をインストール

FreeBSD のインストールについては ThinkPad T420s 特有のものはありません。

USB Disk から起動してインストールできますので、memstick.img を使ってインストールするのが楽だと思います。

- https://download.freebsd.org/ftp/snapshots/ISO-IMAGES/12.2/FreeBSD-12.2-STABLE-amd64-20201126-r368037-memstick.img

# X のインストール

利用するグラフィックチップに合わせて X のドライバーを選びます。
それ以外は通常の X のインストールと変わりません。

## Intel CPU 内臓 GPU を使う場合

対応しているドライバーは xf86-video-intel になります。

```
pkg install -y xf86-video-intel
```

## NVIDIA Quadro NVS (NVS 4200M) を使う場合

対応しているドライバーは nvidia-driver-340 になります。

```
pkg install -y nvidia-driver-340
```

インストール後にドライバーの kernel module をロードしておきます。

```
kldload nvidia
```

再起動してもドライバーがロードされるように /boot/loader.conf.local に設定を追加しておきます。

```shell
echo 'nvidia_load="YES"' >> /boot/loader.conf.local
```

そのままだとうまくカードを認識できないため、/usr/local/etc/X11/xorg.conf.d/thinkpad-t420s.conf を作成して BusID を明示的に指定しておきます。

```shell
cat > /usr/local/etc/X11/xorg.conf.d/thinkpad-t420s.conf <<EOM
Section "Device"
        Identifier "Card0"
        Driver     "nvidia"
        BusID      "PCI:1:0:0"
EndSection
EOM
```

## xorg のインストール

xorg をインストールして X Windows System をインストールします。

```
pkg install -y xorg
```

## startx で確認

startx を実行して動作を確認します。
xterm が起動したら xwininfo で画面サイズが 1600x900 で認識できているかを確認します。

# まとめ

ThinkPad T420s に FreeBSD をインストールする場合、BIOS 設定と graphic driver の選択に注意するだけでした。

ちょっと古い PC に FreeBSD をインストールする参考にしてみてください。

# 参照
- [Lenovo T420s (Nvidia Quatro 4200M) resolution problem](https://forums.freebsd.org/threads/lenovo-t420s-nvidia-quatro-4200m-resolution-problem.24935/)
- [最新のレガシー GPU バージョン (340.xx シリーズ): 340.108](https://www.nvidia.co.jp/Download/driverResults.aspx/156259/jp)