Save Ukraine

Gentoo Linux on the MacBook Pro 8,2 (late 2011)

Christian Kruse,

Why Linux?

First of all I'd like to explain why I want to use Linux on my Apple notebook, because I have been asked this alot… Late 2011 I bought a Macbook Pro. It was meant as a developer machine and it is a nice piece of hardware. 15,3" HD display (a native resolution of 1680x1050 pixels), anti-glare, quad core i7 and a 120GB SSD. I modified it and put another 4GB of RAM and a traditional 750GB HDD in it. All in all: a really nice machine. But I don't like Lion. While OS X 10.6 was quite nice, in Lion everything got worse. They killed the virtual desktops (yeah, I know, I could use them like Apple expects me to – but I don't want to, I don't like their way), the whole OS got MUCH slower and I am quite familiar with the fucking beachball – unbelievable on this machine!

And as if this is not bad enough, Apple sucks more and more. The whole App Store thing is a fiasko and with Mountain Lion they revealed where the journey should go. Mind the Gatekeeper technology – although it is not implemented very good, it shows what Apple wants: control.

But this is not about Apple's or my morals, this is about how to migrate to Linux. So I just summarize the special cases you have to mind when dealing with the MacBook.

Prerequisits

First of all: do not delete OS X completely. The only way to install EFI updates and security fixes is via the software update of OS X. But don't despair, HFS+ supports live resizing.

Resizing

So clean up your OS X installation (I simply deleted all non-OSX programs and cleaned up my ~/Library folder) and start a shell. Resizing with diskutil is easy. First find out what the device name of your disk partition is:

$ diskutil list
#:                   type name               size      identifier
/dev/disk0
#:                       TYPE NAME                    SIZE       IDENTIFIER
0:      GUID_partition_scheme                         120.0 GB   disk0
1:                        EFI                         200.0 MB   disk0s1
2:                  Apple_HFS Macintosh HD            118.0 GB   disk0s2
3:                 Apple_Boot Recovery HD             792.2 MB   disk0s3

Look for your volumne name, in this case it is Macintosh HD, the second slice on the first disk. So the device name is /dev/disk0s2. To resize that volume, simply use diskutil resizeVolume device new_size. You can use units, e.g.

$ diskutil resizeVolume /dev/disk0s2 20GB

Diskutil may complain that the new size is to small, in this case simply add a few gigs.

Linux boot medium

If you modified your MacBook as I did (replaced the superdrive with a HDD), you cannot boot from an external CDROM drive. To understand why, you have to understand the way EFI handles booting: there are two boot modes. The first one is the „EFI mode,“ all hardware can be accessed directly but you don't have a BIOS environment. The second mode is the „BIOS emulation mode“. In this mode EFI simulates a BIOS and the hardware is accessed through this emulation layer. This BIOS mode gets activated automatically when you boot from a non-EFI ready medium like a CDROM drive.

But when the EFI enters the BIOS mode, the USB CDROM drive disappears and booting stops. The alternative is a USB stick, but EFI sucks a lot and does not boot from non-EFI ready USB sticks. So what we have to do is to create a USB stick which supports EFI booting. I created an image I created an image archboot-efi-2012-06-27.img and uploaded it, you can install it to any USB stick bigger or equal to 8 gigs. I will write an entry about how to create such a stick yourself later this week. For now, just download the image and write it to the USB stick with dd if=archboot-efi-2012-06-27.img of=/dev/yourstick bs=1M.

To boot from the USB medium, plug it in and press the option key while starting your MacBook. This brings up a boot manager like menu where you can choose the USB stick. When the screen goes black, don't bother: this is because of the two graphic cards. After a few moments the screen should turn on again.

Installing Gentoo

Of course we want to use full disk encryption (encrypted root) nowadays. So just follow the Gentoo handbook and the DM-Crypt with LUKS tutorial until you hit the kernel installation. Don't install the Gentoo sources or the vanilla kernel; for now (2012-06-27) we need the 3.5 rc4 or higher kernel. Grab the kernel sources directly from kernel.org and extract them to /usr/src. Don't forget to symlink your kernel source directory to /usr/src/linux. You also need two patches to get VGA switcheroo working and another one to get the LCD backlight working: the radeon BIOS patch and the Apple GMUX vga switcheroo patch for VGA switcheroo and the apple_bl GMUX backlight patch. The radeon BIOS patch enables the radeon driver to load the firmware from disk and the Apple GMUX vga switcheroo patch implementes a VGA switcheroo driver for the proprietary GMUX device by Apple. Download the patches and apply them with patch -p1 ../patchfile. When you patched the kernel, open drivers/gpu/vga/apple_gmux.c and comment out line 370:

wait_for_completion(&powerchange_done);

should be changed to

//wait_for_completion(&powerchange_done);

You have to do this because there seems to be a change in switcheroo handling with recent kernels: we are waiting for an interupt, but it will never come (at least not in 10 minutes – that's the time I waited first).

I've also got a working kernel config file for you ;-) If you create your own configuration, be aware: i915 should be compiled into the kernel and radeon should be compiled as a module.

The bootloader

You can boot either in BIOS mode or in EFI mode. I suggest you to boot in EFI mode, because the BIOS emulation is buggy. Also you cannot use the integrated graphics in BIOS mode. Setting up grub2 is pretty easy: ensure that your EFI partition is mounted at /boot/efi and /boot/efi/EFI exists. Add sys-boot/efibootmgr and sys-boot/grub to /etc/portage/package.keywords. In /etc/portage/make.conf (or /etc/make.conf on older systems) add GRUB_PLATFORMS="efi-64". To finalize the installation, execute

grub2-install --boot-directory=/boot --bootloader-id=grub2 --no-floppy --recheck
grub2-mkconfig -o /boot/grub2/grub.cfg

This creates an EFI boot file in /boot/efi/EFI/grub2/ and a grub config file in /boot/grub2/grub.cfg. After that you have to modify the configuration to add some boot parameters to the kernel: the kernel command line should include i915.modeset=1 i915.lvds_channel_mode=2 i915.lvds_use_ssc=0 radeon.modeset=1. the modeset parameters cause the GPU drivers to switch the graphic resolution, the lvds_channel_mode=2 tells the i915 driver that there are two LVDS channels and the lvds_use_ssc=0 disables Spread-Spectrum Clocking, which is buggy with some i5/i7 chipsets.

You also have to modify the configuration for some video issues. Instead of calling load_video add the following code:

set gfxpayload=keep
# Switch gmux to IGD
outb 0x728 1
outb 0x710 2
outb 0x740 2

This causes the GMUX device to switch to the integrated graphics card.

Hardware support

Hardware support is not that bad at all. Wifi is supported, the SDCard reader is supported and ethernet and bluetooth are supported as well – looks good :) But you need some manual work to get everything rollin'.

Wifi

Wifi works with the b43 driver as a module and CONFIG_B43_PHY_HT=y as well as CONFIG_B43_BCMA=y activated. You have to install at least =net-wireless/b43-firmware-5.100.138. As of now (2012-06-20) the portage tree has not got version bumped, so I wrote an ebuild file for you. Have a look at the "using custom ebuilds with Gentoo" article if you don't know how to use it. As of kernel 3.5 suspend recovery works, so you don't need to remove the module when suspending.

Suspend

Suspending kind of works out of the box. If the notebook wakes up, the radeon card gets activated. So you need a script to re-activate the i915 again. Have a look at this article about Debian on the MacBook Pro 8,2 for the information about suspend/resume. Just copy the C file and the shell script to the right places, you don't need uswsusp or SUSPEND_MODULES="b43 bcma".

Be aware: when you wake up the macbook, there is a bug with the radeon driver. It causes entries like those in the kernel log:

[drm:atom_op_jump] ERROR atombios stuck in loop for more than 5secs aborting
[drm:atom_execute_table_locked] ERROR atombios stuck executing D37A (len 62, WS 0, PS 0) @ 0xD396
[drm:atom_op_jump] ERROR atombios stuck in loop for more than 5secs aborting

This causes the suspend/resume to take longer time than usual. But it works, just be patient! :)

Touchpad

The touchpad works flawlessly with xf86-input-mtrack.

External monitor

Works, when VGA switcheroo works. VGA switcheroo works with the kernel patches above. Also, copy the two radeon firmware files int10.bin and vbios.bin to /lib/firmware/radeon. You have to switch the graphics card to the discrete:

$ echo dis > /sys/kernel/debug/vgaswitcheroo/switch

Don't forget to switch back before suspending or it will crash upon wakeup! You can do this by echo'ing again:

$ echo igd > /sys/kernel/debug/vgaswitcheroo/switch

LCD backlight

Works with the above mentioned apple_bl GMUX backlight patch. I didn't get pommed working, but I didn't care enough to look why. I use xbacklight to change the backlight, it is part of the xorg distribution. You can also fiddle with the proc file directly, have a look at the files /sys/class/backlight/acpi_video0/brightness (contains the current brightness level) and /sys/class/backlight/acpi_video0/max_brightness (contains the maximum brightness level).