ACPI-Based Support For Intel Thunderbolt Hot-PlugIn the Linux* Kernel
Rafael J. Wysocki
Intel Open Source Technology Center
March 25, 2014
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 1 / 25
Outline
1 Technology OverviewIntel ThunderboltACPI Events Handling
2 Support In The PCI SubsystemACPI-Based PCI Hot-Plug (ACPIPHP)PCI Device Addition/Removal And Concurrency
3 Resources
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 2 / 25
Technology Overview Intel Thunderbolt
Intel Thunderbolt – General Information
Device interconnect technology
1 DisplayPort forwarding
Transparent with respect to software.
2 PCI Express with hot-plug
PCIe hierarchy branch (multiple devices) on a single link.Daisy chains of devices.Out-of-band event (hot-add, hot-remove) signaling.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 3 / 25
Technology Overview Intel Thunderbolt
Thunderbolt Signaling Arrangements
Apple systems
Events signaled through hardware interrupts.
Handled by the OS (interrupt handler).
OS responsible for PCIe bus re-configuration.
Systems from other vendors
Events signaled through SMI (System Management Interrupts).
Handled by the BIOS in SMM (System Management Mode).
BIOS responsible for PCIe bus re-configuration.
OS notified via ACPI bus check notifications.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 4 / 25
Technology Overview Intel Thunderbolt
BIOS-Based Thunderbolt Event Handling Control Flow
Link Event (plug, unplug)
SMI
ACPI GPE
SMI handler (SMM) Re-configure PCIe Bus
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 5 / 25
Technology Overview ACPI Events Handling
ACPI GPE (Simplest Case)
General Purpose Event (GPE)
Pair of bits: enable and status (same position, different registers).
One enable and one status register per block.
Two “fixed” blocks, GPE block 0 and GPE block 1.
Status bit set triggers an ACPI SCI (System Control Interrupt).
Status bit may be set if the corresponding enable bit is set.
GPE number: 0 . . . 0xFF
Bit position within GPE blocks 0 and 1.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 6 / 25
Technology Overview ACPI Events Handling
GPE Handling Control Flow
GPE Status Set
ACPI Interrupt Interrupt Handler
Determine GPE Number
GPE Handler Present? Execute GPE Handler
_Lxx / _Exx Present? Execute _Lxx / _Exx
Wakeup GPE? Notify (Object, Device Wake)
GPE Number Determined
YES
NO
NO
YES
YES
Queue up GPE Work GPE Work Function
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 7 / 25
Technology Overview ACPI Events Handling
ACPI Namespace
Kernel data structure
Hierarchy (tree) of objects representing various system parts.
Supposed to reflect system topology.
Contains data and code (methods).
Covers things known to the BIOS only.
ACPI namespace management
Created during system startup.
May be dynamically extended and (theoretically) shrinked later.
Encoded in data structures called definition blocks in ACPI tables.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 8 / 25
Technology Overview ACPI Events Handling
ACPI Namespace Continued
ACPI Machine Language (AML)
Byte code compiled from sources in ACPI Source Language (ASL) andused for encoding definition blocks.
ACPI Namespace
AML Interpreter
Hardware
Kernel Core
+
Device Drivers
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 9 / 25
Technology Overview ACPI Events Handling
ACPI GPE Handling
ACPI interrupt (e.g. SCI) handler determines the GPE number
1 Execute _Lxx/_Exx (AML methods associated with GPEs) if present.
2 Execute Notify (Object, Device Wake) if possible.
In a typical system
_Lxx/_Exx determine target objects and execute Notify () for them.
Notify (Object, Notification Value)
AML operator causing callbacks supplied by the OS (kernel) to be run.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 10 / 25
Technology Overview ACPI Events Handling
ACPI Device Representation
Devices are represented by ACPI namespace nodes
Objects below those nodes may represent
Data (names, addresses etc.).
Control methods (pieces of AML code).
Dependent (child) devices.
Each namespace object (except for the root) has exactly one parent.
Each namespace object can be unambiguously identified by the path to itfrom the namespace root.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 11 / 25
Technology Overview ACPI Events Handling
Device Objects In ACPI Namespace (Example)
Device (P0P1)
Device (PCI0)
Name (_HID) Method (_BBN)
Name (_CID)
Name (_UID)
Name (PR00)
Name (AR00)
Method (_PRT)
Method (_PS0)
Method (_PS3)
Name (BUF0)
Method (_CRS)
Name (GUID)
Name (SUPP)
Name (CTRL)
Name (XCNT)
Method (_OSC)
Name (_ADR)
Method (_PRW) Name (PR04)
Name (AR04)Method (_PRT)
Device (LCPB)
Name (_ADR)
Name (_HID)
Name (_UID)
Method (_DIS)
Method (_PRS)
Method (_SRS)
Method (_CRS)
Method (_STA)
Device (LNKA) Device (LNKB)
Name (_HID)
Name (_UID)
Method (_DIS)
Method (_PRS)
Method (_SRS)
Method (_CRS)
Method (_STA)
Device (DMAC)
Method (_CRS)Name (_HID)
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 12 / 25
Support In The PCI Subsystem ACPI-Based PCI Hot-Plug (ACPIPHP)
ACPI Notification Handlers
Notify () executes callbacks supplied by the OS
notify_handler(ACPI object handle, event code, data pointer)
acpi_install_notify_handler(ACPI object handle, handler type,
notify_handler, data pointer)
Problem: Ensure that relevant notify handlers are present
Before 3.12: Objects corresponding to “removable” PCI devices.
3.12 – 3.14: All objects corresponding to PCI devices (almost).
3.15-rc: Use the global notify handler.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 13 / 25
Support In The PCI Subsystem ACPI-Based PCI Hot-Plug (ACPIPHP)
Identification Of Notification Targets
Target device identification problem
Find the PCI device (struct pci_dev), if present, represented by thegiven object in the ACPI namespace.
Before 3.12: Use the data pointer argument of notify handlers
Racy: That pointer may become invalid while the handler is running.
3.12 – 3.14: Attach context data to namespace objects
Use acpi_attach_data()/acpi_get_data() and proper locking.
In 3.15-rc context data are attached to struct acpi_device objects.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 14 / 25
Support In The PCI Subsystem ACPI-Based PCI Hot-Plug (ACPIPHP)
ACPI Bus Check Notifications Handling
Bus Check Notification (code 0)
Bus re-enumeration is required (starting at the target object).
Walk the bus from the target device down.
Remove devices that are not present any more.
Add devices that has just appeared.
Before 3.12 that only really worked for PCI devices with correspondingACPI namespace objects (insufficient for Thunderbolt hot-plug).
3.12 and later check the entire PCI tree branch below the notify target.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 15 / 25
Support In The PCI Subsystem ACPI-Based PCI Hot-Plug (ACPIPHP)
Preservation Of BIOS-Provided Device Configuration
Hot-added devices may be already configured by the BIOS.
Changing their configuration may lead to problems.
BIOS-Provided configuration settings should be used a long as they work
3.10 and earlier kernels always tried to configure hot-added devices.
3.11 and later do their best to use the BIOS-provided settings.
The change in 3.11 was made as a fix related to docking stations holdingPCIe devices, but it also is necessary for supporting Thunderbolt hot-plug.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 16 / 25
Support In The PCI Subsystem ACPI-Based PCI Hot-Plug (ACPIPHP)
Serialization With Respect To System Suspend/Resume
Before 3.12 PCI hot-plug notifications (bus check, device check, ejectrequests) might be handled during system suspend or resume.
That lead to problems when device drivers’ .remove() callbacks wereexecuted before their system resume callbacks (or between them).
3.12 and later kernels use locking to prevent hot-plug notifications frombeing handled during system suspend/resume.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 17 / 25
Support In The PCI Subsystem PCI Device Addition/Removal And Concurrency
PCI Device Remove And Bus Rescan sysfs Interface
/sys/devices/pci0000:00/.../remove
Remove struct pci_dev representing the device (as though the devicewas removed).
/sys/devices/pci0000:00/.../rescan
Re-scan the PCI bus staring from this device, possibly addingstruct pci_dev objects that are missing.
This has to be synchronized with hot-plug notification handling, but thatsynchronization was not there in 3.13 and earlier kernels.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 18 / 25
Support In The PCI Subsystem PCI Device Addition/Removal And Concurrency
PCI Device Remove And Bus Rescan Synchronization
In 3.14 the locking used internally by the remove/rescan sysfs interfacewas extended to all PCI device addition and removal code paths.
In 3.14 and later kernels all code paths involving PCI device addition orremoval are (supposed to be) mutually exclusive.
Among other things this allows different PCI device hot-plug frameworksto co-exist in the same system.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 19 / 25
Support In The PCI Subsystem PCI Device Addition/Removal And Concurrency
Surprise Device Removal And PCI Drivers
With Thunderbolt PCIe devices may physically go away before theirdrivers’ .remove() callbacks are executed.
That may happen at any time.
PCI device drivers should be prepared for that
Lack of device response need not be an error.
Handle the lack of device response gracefully.
Do not wait for device response forever (or poll it forever).
Clean up everything even if the device doesn’t respond.
During the clean-up do not touch hardware that is gone.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 20 / 25
Summary
Intel Thunderbolt is supported on systems where the BIOSparticipates in the handling of link events.
ACPI bus check notifications are then used to notify the kernel aboutPCIe bus configuration changes.
The kernel code responsible for handling those notifications had to bemodified substantially to cover Thunderbolt hot-plug properly.
The PCI subsystem’s locking was hardened in the process.
Thunderbolt hot-plug support implies that device drivers have to beprepared to handle surprise device removal gracefully.
Questions?
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 21 / 25
Resources
References
Intel Corp., ThunderboltTM Technology for Developers
(http://www.intel.com/content/www/us/en/io/thunderbolt/thunderbolt-technology-developer.html).
R. J. Wysocki, Grand Unification of ACPI-Based Device Hot-Plug
(http://events.linuxfoundation.org/sites/events/files/slides/acpi_hotplug_plain.pdf).
R. J. Wysocki, Implementing ACPI 5 Features – Device Enumeration And Power Management
(http://events.linuxfoundation.org/images/stories/slides/lfcs2013_wysocki.pdf).
L. Brown, ACPI5 on Linux
(http://www.linuxplumbersconf.org/2012/wp-content/uploads/2012/09/LPC2012-ACPI5.pdf).
Advanced Configuration and Power Interface Specification, Revision 5.0a
(http://www.acpi.info/DOWNLOADS/ACPI_5_Errata%20A.pdf).
L. Duflot, O. Levillain, B. Morin, ACPI Design Principles and Concerns
(http://www.ssi.gouv.fr/IMG/pdf/Pres_acpi.pdf).
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 22 / 25
Resources
Documentation And Source Code
https://acpica.org
Documentation/acpi/
include/acpi/
include/linux/acpi.h
include/linux/device.h
include/linux/pci*.h
drivers/acpi/
drivers/pci/
drivers/pci/hotplug/acpiphp*
drivers/pci/pci-acpi.c
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 23 / 25
Legal Information
Intel is a trademark of Intel Corporation in the U. S. and other countries.*Other names and brands may be claimed as the property of others.Copyright c© 2014 Intel Corporation, All rights reserved.
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 24 / 25
Thanks!
Thank you for attention!
Rafael J. Wysocki (Intel OTC) ACPI-Based Thunderbolt Hot-Plug March 25, 2014 25 / 25