1.. SPDX-License-Identifier: GPL-2.0
2
3=============================
4ACPI Based Device Enumeration
5=============================
6
7ACPI 5 introduced a set of new resources (UartTSerialBus, I2cSerialBus,
8SpiSerialBus, GpioIo and GpioInt) which can be used in enumerating slave
9devices behind serial bus controllers.
10
11In addition we are starting to see peripherals integrated in the
12SoC/Chipset to appear only in ACPI namespace. These are typically devices
13that are accessed through memory-mapped registers.
14
15In order to support this and re-use the existing drivers as much as
16possible we decided to do following:
17
18  - Devices that have no bus connector resource are represented as
19    platform devices.
20
21  - Devices behind real busses where there is a connector resource
22    are represented as struct spi_device or struct i2c_device. Note
23    that standard UARTs are not busses so there is no struct uart_device,
24    although some of them may be represented by sturct serdev_device.
25
26As both ACPI and Device Tree represent a tree of devices (and their
27resources) this implementation follows the Device Tree way as much as
28possible.
29
30The ACPI implementation enumerates devices behind busses (platform, SPI,
31I2C, and in some cases UART), creates the physical devices and binds them
32to their ACPI handle in the ACPI namespace.
33
34This means that when ACPI_HANDLE(dev) returns non-NULL the device was
35enumerated from ACPI namespace. This handle can be used to extract other
36device-specific configuration. There is an example of this below.
37
38Platform bus support
39====================
40
41Since we are using platform devices to represent devices that are not
42connected to any physical bus we only need to implement a platform driver
43for the device and add supported ACPI IDs. If this same IP-block is used on
44some other non-ACPI platform, the driver might work out of the box or needs
45some minor changes.
46
47Adding ACPI support for an existing driver should be pretty
48straightforward. Here is the simplest example::
49
50	static const struct acpi_device_id mydrv_acpi_match[] = {
51		/* ACPI IDs here */
52		{ }
53	};
54	MODULE_DEVICE_TABLE(acpi, mydrv_acpi_match);
55
56	static struct platform_driver my_driver = {
57		...
58		.driver = {
59			.acpi_match_table = mydrv_acpi_match,
60		},
61	};
62
63If the driver needs to perform more complex initialization like getting and
64configuring GPIOs it can get its ACPI handle and extract this information
65from ACPI tables.
66
67DMA support
68===========
69
70DMA controllers enumerated via ACPI should be registered in the system to
71provide generic access to their resources. For example, a driver that would
72like to be accessible to slave devices via generic API call
73dma_request_chan() must register itself at the end of the probe function like
74this::
75
76	err = devm_acpi_dma_controller_register(dev, xlate_func, dw);
77	/* Handle the error if it's not a case of !CONFIG_ACPI */
78
79and implement custom xlate function if needed (usually acpi_dma_simple_xlate()
80is enough) which converts the FixedDMA resource provided by struct
81acpi_dma_spec into the corresponding DMA channel. A piece of code for that case
82could look like::
83
84	#ifdef CONFIG_ACPI
85	struct filter_args {
86		/* Provide necessary information for the filter_func */
87		...
88	};
89
90	static bool filter_func(struct dma_chan *chan, void *param)
91	{
92		/* Choose the proper channel */
93		...
94	}
95
96	static struct dma_chan *xlate_func(struct acpi_dma_spec *dma_spec,
97			struct acpi_dma *adma)
98	{
99		dma_cap_mask_t cap;
100		struct filter_args args;
101
102		/* Prepare arguments for filter_func */
103		...
104		return dma_request_channel(cap, filter_func, &args);
105	}
106	#else
107	static struct dma_chan *xlate_func(struct acpi_dma_spec *dma_spec,
108			struct acpi_dma *adma)
109	{
110		return NULL;
111	}
112	#endif
113
114dma_request_chan() will call xlate_func() for each registered DMA controller.
115In the xlate function the proper channel must be chosen based on
116information in struct acpi_dma_spec and the properties of the controller
117provided by struct acpi_dma.
118
119Clients must call dma_request_chan() with the string parameter that corresponds
120to a specific FixedDMA resource. By default "tx" means the first entry of the
121FixedDMA resource array, "rx" means the second entry. The table below shows a
122layout::
123
124	Device (I2C0)
125	{
126		...
127		Method (_CRS, 0, NotSerialized)
128		{
129			Name (DBUF, ResourceTemplate ()
130			{
131				FixedDMA (0x0018, 0x0004, Width32bit, _Y48)
132				FixedDMA (0x0019, 0x0005, Width32bit, )
133			})
134		...
135		}
136	}
137
138So, the FixedDMA with request line 0x0018 is "tx" and next one is "rx" in
139this example.
140
141In robust cases the client unfortunately needs to call
142acpi_dma_request_slave_chan_by_index() directly and therefore choose the
143specific FixedDMA resource by its index.
144
145Named Interrupts
146================
147
148Drivers enumerated via ACPI can have names to interrupts in the ACPI table
149which can be used to get the IRQ number in the driver.
150
151The interrupt name can be listed in _DSD as 'interrupt-names'. The names
152should be listed as an array of strings which will map to the Interrupt()
153resource in the ACPI table corresponding to its index.
154
155The table below shows an example of its usage::
156
157    Device (DEV0) {
158        ...
159        Name (_CRS, ResourceTemplate() {
160            ...
161            Interrupt (ResourceConsumer, Level, ActiveHigh, Exclusive) {
162                0x20,
163                0x24
164            }
165        })
166
167        Name (_DSD, Package () {
168            ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
169            Package () {
170                Package () {"interrupt-names",
171                Package (2) {"default", "alert"}},
172            }
173        ...
174        })
175    }
176
177The interrupt name 'default' will correspond to 0x20 in Interrupt()
178resource and 'alert' to 0x24. Note that only the Interrupt() resource
179is mapped and not GpioInt() or similar.
180
181The driver can call the function - fwnode_irq_get_byname() with the fwnode
182and interrupt name as arguments to get the corresponding IRQ number.
183
184SPI serial bus support
185======================
186
187Slave devices behind SPI bus have SpiSerialBus resource attached to them.
188This is extracted automatically by the SPI core and the slave devices are
189enumerated once spi_register_master() is called by the bus driver.
190
191Here is what the ACPI namespace for a SPI slave might look like::
192
193	Device (EEP0)
194	{
195		Name (_ADR, 1)
196		Name (_CID, Package () {
197			"ATML0025",
198			"AT25",
199		})
200		...
201		Method (_CRS, 0, NotSerialized)
202		{
203			SPISerialBus(1, PolarityLow, FourWireMode, 8,
204				ControllerInitiated, 1000000, ClockPolarityLow,
205				ClockPhaseFirst, "\\_SB.PCI0.SPI1",)
206		}
207		...
208
209The SPI device drivers only need to add ACPI IDs in a similar way than with
210the platform device drivers. Below is an example where we add ACPI support
211to at25 SPI eeprom driver (this is meant for the above ACPI snippet)::
212
213	static const struct acpi_device_id at25_acpi_match[] = {
214		{ "AT25", 0 },
215		{ }
216	};
217	MODULE_DEVICE_TABLE(acpi, at25_acpi_match);
218
219	static struct spi_driver at25_driver = {
220		.driver = {
221			...
222			.acpi_match_table = at25_acpi_match,
223		},
224	};
225
226Note that this driver actually needs more information like page size of the
227eeprom, etc. This information can be passed via _DSD method like::
228
229	Device (EEP0)
230	{
231		...
232		Name (_DSD, Package ()
233		{
234			ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
235			Package ()
236			{
237				Package () { "size", 1024 },
238				Package () { "pagesize", 32 },
239				Package () { "address-width", 16 },
240			}
241		})
242	}
243
244Then the at25 SPI driver can get this configuration by calling device property
245APIs during ->probe() phase like::
246
247	err = device_property_read_u32(dev, "size", &size);
248	if (err)
249		...error handling...
250
251	err = device_property_read_u32(dev, "pagesize", &page_size);
252	if (err)
253		...error handling...
254
255	err = device_property_read_u32(dev, "address-width", &addr_width);
256	if (err)
257		...error handling...
258
259I2C serial bus support
260======================
261
262The slaves behind I2C bus controller only need to add the ACPI IDs like
263with the platform and SPI drivers. The I2C core automatically enumerates
264any slave devices behind the controller device once the adapter is
265registered.
266
267Below is an example of how to add ACPI support to the existing mpu3050
268input driver::
269
270	static const struct acpi_device_id mpu3050_acpi_match[] = {
271		{ "MPU3050", 0 },
272		{ }
273	};
274	MODULE_DEVICE_TABLE(acpi, mpu3050_acpi_match);
275
276	static struct i2c_driver mpu3050_i2c_driver = {
277		.driver	= {
278			.name	= "mpu3050",
279			.pm	= &mpu3050_pm,
280			.of_match_table = mpu3050_of_match,
281			.acpi_match_table = mpu3050_acpi_match,
282		},
283		.probe		= mpu3050_probe,
284		.remove		= mpu3050_remove,
285		.id_table	= mpu3050_ids,
286	};
287	module_i2c_driver(mpu3050_i2c_driver);
288
289Reference to PWM device
290=======================
291
292Sometimes a device can be a consumer of PWM channel. Obviously OS would like
293to know which one. To provide this mapping the special property has been
294introduced, i.e.::
295
296    Device (DEV)
297    {
298        Name (_DSD, Package ()
299        {
300            ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
301            Package () {
302                Package () { "compatible", Package () { "pwm-leds" } },
303                Package () { "label", "alarm-led" },
304                Package () { "pwms",
305                    Package () {
306                        "\\_SB.PCI0.PWM",  // <PWM device reference>
307                        0,                 // <PWM index>
308                        600000000,         // <PWM period>
309                        0,                 // <PWM flags>
310                    }
311                }
312            }
313        })
314        ...
315    }
316
317In the above example the PWM-based LED driver references to the PWM channel 0
318of \_SB.PCI0.PWM device with initial period setting equal to 600 ms (note that
319value is given in nanoseconds).
320
321GPIO support
322============
323
324ACPI 5 introduced two new resources to describe GPIO connections: GpioIo
325and GpioInt. These resources can be used to pass GPIO numbers used by
326the device to the driver. ACPI 5.1 extended this with _DSD (Device
327Specific Data) which made it possible to name the GPIOs among other things.
328
329For example::
330
331	Device (DEV)
332	{
333		Method (_CRS, 0, NotSerialized)
334		{
335			Name (SBUF, ResourceTemplate()
336			{
337				// Used to power on/off the device
338				GpioIo (Exclusive, PullNone, 0, 0, IoRestrictionOutputOnly,
339					"\\_SB.PCI0.GPI0", 0, ResourceConsumer) { 85 }
340
341				// Interrupt for the device
342				GpioInt (Edge, ActiveHigh, ExclusiveAndWake, PullNone, 0,
343					 "\\_SB.PCI0.GPI0", 0, ResourceConsumer) { 88 }
344			}
345
346			Return (SBUF)
347		}
348
349		// ACPI 5.1 _DSD used for naming the GPIOs
350		Name (_DSD, Package ()
351		{
352			ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
353			Package ()
354			{
355				Package () { "power-gpios", Package () { ^DEV, 0, 0, 0 } },
356				Package () { "irq-gpios", Package () { ^DEV, 1, 0, 0 } },
357			}
358		})
359		...
360	}
361
362These GPIO numbers are controller relative and path "\\_SB.PCI0.GPI0"
363specifies the path to the controller. In order to use these GPIOs in Linux
364we need to translate them to the corresponding Linux GPIO descriptors.
365
366There is a standard GPIO API for that and is documented in
367Documentation/admin-guide/gpio/.
368
369In the above example we can get the corresponding two GPIO descriptors with
370a code like this::
371
372	#include <linux/gpio/consumer.h>
373	...
374
375	struct gpio_desc *irq_desc, *power_desc;
376
377	irq_desc = gpiod_get(dev, "irq");
378	if (IS_ERR(irq_desc))
379		/* handle error */
380
381	power_desc = gpiod_get(dev, "power");
382	if (IS_ERR(power_desc))
383		/* handle error */
384
385	/* Now we can use the GPIO descriptors */
386
387There are also devm_* versions of these functions which release the
388descriptors once the device is released.
389
390See Documentation/firmware-guide/acpi/gpio-properties.rst for more information
391about the _DSD binding related to GPIOs.
392
393RS-485 support
394==============
395
396ACPI _DSD (Device Specific Data) can be used to describe RS-485 capability
397of UART.
398
399For example::
400
401	Device (DEV)
402	{
403		...
404
405		// ACPI 5.1 _DSD used for RS-485 capabilities
406		Name (_DSD, Package ()
407		{
408			ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
409			Package ()
410			{
411				Package () {"rs485-rts-active-low", Zero},
412				Package () {"rs485-rx-active-high", Zero},
413				Package () {"rs485-rx-during-tx", Zero},
414			}
415		})
416		...
417
418MFD devices
419===========
420
421The MFD devices register their children as platform devices. For the child
422devices there needs to be an ACPI handle that they can use to reference
423parts of the ACPI namespace that relate to them. In the Linux MFD subsystem
424we provide two ways:
425
426  - The children share the parent ACPI handle.
427  - The MFD cell can specify the ACPI id of the device.
428
429For the first case, the MFD drivers do not need to do anything. The
430resulting child platform device will have its ACPI_COMPANION() set to point
431to the parent device.
432
433If the ACPI namespace has a device that we can match using an ACPI id or ACPI
434adr, the cell should be set like::
435
436	static struct mfd_cell_acpi_match my_subdevice_cell_acpi_match = {
437		.pnpid = "XYZ0001",
438		.adr = 0,
439	};
440
441	static struct mfd_cell my_subdevice_cell = {
442		.name = "my_subdevice",
443		/* set the resources relative to the parent */
444		.acpi_match = &my_subdevice_cell_acpi_match,
445	};
446
447The ACPI id "XYZ0001" is then used to lookup an ACPI device directly under
448the MFD device and if found, that ACPI companion device is bound to the
449resulting child platform device.
450
451Device Tree namespace link device ID
452====================================
453
454The Device Tree protocol uses device identification based on the "compatible"
455property whose value is a string or an array of strings recognized as device
456identifiers by drivers and the driver core.  The set of all those strings may be
457regarded as a device identification namespace analogous to the ACPI/PNP device
458ID namespace.  Consequently, in principle it should not be necessary to allocate
459a new (and arguably redundant) ACPI/PNP device ID for a devices with an existing
460identification string in the Device Tree (DT) namespace, especially if that ID
461is only needed to indicate that a given device is compatible with another one,
462presumably having a matching driver in the kernel already.
463
464In ACPI, the device identification object called _CID (Compatible ID) is used to
465list the IDs of devices the given one is compatible with, but those IDs must
466belong to one of the namespaces prescribed by the ACPI specification (see
467Section 6.1.2 of ACPI 6.0 for details) and the DT namespace is not one of them.
468Moreover, the specification mandates that either a _HID or an _ADR identification
469object be present for all ACPI objects representing devices (Section 6.1 of ACPI
4706.0).  For non-enumerable bus types that object must be _HID and its value must
471be a device ID from one of the namespaces prescribed by the specification too.
472
473The special DT namespace link device ID, PRP0001, provides a means to use the
474existing DT-compatible device identification in ACPI and to satisfy the above
475requirements following from the ACPI specification at the same time.  Namely,
476if PRP0001 is returned by _HID, the ACPI subsystem will look for the
477"compatible" property in the device object's _DSD and will use the value of that
478property to identify the corresponding device in analogy with the original DT
479device identification algorithm.  If the "compatible" property is not present
480or its value is not valid, the device will not be enumerated by the ACPI
481subsystem.  Otherwise, it will be enumerated automatically as a platform device
482(except when an I2C or SPI link from the device to its parent is present, in
483which case the ACPI core will leave the device enumeration to the parent's
484driver) and the identification strings from the "compatible" property value will
485be used to find a driver for the device along with the device IDs listed by _CID
486(if present).
487
488Analogously, if PRP0001 is present in the list of device IDs returned by _CID,
489the identification strings listed by the "compatible" property value (if present
490and valid) will be used to look for a driver matching the device, but in that
491case their relative priority with respect to the other device IDs listed by
492_HID and _CID depends on the position of PRP0001 in the _CID return package.
493Specifically, the device IDs returned by _HID and preceding PRP0001 in the _CID
494return package will be checked first.  Also in that case the bus type the device
495will be enumerated to depends on the device ID returned by _HID.
496
497For example, the following ACPI sample might be used to enumerate an lm75-type
498I2C temperature sensor and match it to the driver using the Device Tree
499namespace link::
500
501	Device (TMP0)
502	{
503		Name (_HID, "PRP0001")
504		Name (_DSD, Package () {
505			ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
506			Package () {
507				Package () { "compatible", "ti,tmp75" },
508			}
509		})
510		Method (_CRS, 0, Serialized)
511		{
512			Name (SBUF, ResourceTemplate ()
513			{
514				I2cSerialBusV2 (0x48, ControllerInitiated,
515					400000, AddressingMode7Bit,
516					"\\_SB.PCI0.I2C1", 0x00,
517					ResourceConsumer, , Exclusive,)
518			})
519			Return (SBUF)
520		}
521	}
522
523It is valid to define device objects with a _HID returning PRP0001 and without
524the "compatible" property in the _DSD or a _CID as long as one of their
525ancestors provides a _DSD with a valid "compatible" property.  Such device
526objects are then simply regarded as additional "blocks" providing hierarchical
527configuration information to the driver of the composite ancestor device.
528
529However, PRP0001 can only be returned from either _HID or _CID of a device
530object if all of the properties returned by the _DSD associated with it (either
531the _DSD of the device object itself or the _DSD of its ancestor in the
532"composite device" case described above) can be used in the ACPI environment.
533Otherwise, the _DSD itself is regarded as invalid and therefore the "compatible"
534property returned by it is meaningless.
535
536Refer to Documentation/firmware-guide/acpi/DSD-properties-rules.rst for more
537information.
538
539PCI hierarchy representation
540============================
541
542Sometimes could be useful to enumerate a PCI device, knowing its position on the
543PCI bus.
544
545For example, some systems use PCI devices soldered directly on the mother board,
546in a fixed position (ethernet, Wi-Fi, serial ports, etc.). In this conditions it
547is possible to refer to these PCI devices knowing their position on the PCI bus
548topology.
549
550To identify a PCI device, a complete hierarchical description is required, from
551the chipset root port to the final device, through all the intermediate
552bridges/switches of the board.
553
554For example, let us assume to have a system with a PCIe serial port, an
555Exar XR17V3521, soldered on the main board. This UART chip also includes
55616 GPIOs and we want to add the property ``gpio-line-names`` [1] to these pins.
557In this case, the ``lspci`` output for this component is::
558
559	07:00.0 Serial controller: Exar Corp. XR17V3521 Dual PCIe UART (rev 03)
560
561The complete ``lspci`` output (manually reduced in length) is::
562
563	00:00.0 Host bridge: Intel Corp... Host Bridge (rev 0d)
564	...
565	00:13.0 PCI bridge: Intel Corp... PCI Express Port A #1 (rev fd)
566	00:13.1 PCI bridge: Intel Corp... PCI Express Port A #2 (rev fd)
567	00:13.2 PCI bridge: Intel Corp... PCI Express Port A #3 (rev fd)
568	00:14.0 PCI bridge: Intel Corp... PCI Express Port B #1 (rev fd)
569	00:14.1 PCI bridge: Intel Corp... PCI Express Port B #2 (rev fd)
570	...
571	05:00.0 PCI bridge: Pericom Semiconductor Device 2404 (rev 05)
572	06:01.0 PCI bridge: Pericom Semiconductor Device 2404 (rev 05)
573	06:02.0 PCI bridge: Pericom Semiconductor Device 2404 (rev 05)
574	06:03.0 PCI bridge: Pericom Semiconductor Device 2404 (rev 05)
575	07:00.0 Serial controller: Exar Corp. XR17V3521 Dual PCIe UART (rev 03) <-- Exar
576	...
577
578The bus topology is::
579
580	-[0000:00]-+-00.0
581	           ...
582	           +-13.0-[01]----00.0
583	           +-13.1-[02]----00.0
584	           +-13.2-[03]--
585	           +-14.0-[04]----00.0
586	           +-14.1-[05-09]----00.0-[06-09]--+-01.0-[07]----00.0 <-- Exar
587	           |                               +-02.0-[08]----00.0
588	           |                               \-03.0-[09]--
589	           ...
590	           \-1f.1
591
592To describe this Exar device on the PCI bus, we must start from the ACPI name
593of the chipset bridge (also called "root port") with address::
594
595	Bus: 0 - Device: 14 - Function: 1
596
597To find this information is necessary disassemble the BIOS ACPI tables, in
598particular the DSDT (see also [2])::
599
600	mkdir ~/tables/
601	cd ~/tables/
602	acpidump > acpidump
603	acpixtract -a acpidump
604	iasl -e ssdt?.* -d dsdt.dat
605
606Now, in the dsdt.dsl, we have to search the device whose address is related to
6070x14 (device) and 0x01 (function). In this case we can find the following
608device::
609
610	Scope (_SB.PCI0)
611	{
612	... other definitions follow ...
613		Device (RP02)
614		{
615			Method (_ADR, 0, NotSerialized)  // _ADR: Address
616			{
617				If ((RPA2 != Zero))
618				{
619					Return (RPA2) /* \RPA2 */
620				}
621				Else
622				{
623					Return (0x00140001)
624				}
625			}
626	... other definitions follow ...
627
628and the _ADR method [3] returns exactly the device/function couple that
629we are looking for. With this information and analyzing the above ``lspci``
630output (both the devices list and the devices tree), we can write the following
631ACPI description for the Exar PCIe UART, also adding the list of its GPIO line
632names::
633
634	Scope (_SB.PCI0.RP02)
635	{
636		Device (BRG1) //Bridge
637		{
638			Name (_ADR, 0x0000)
639
640			Device (BRG2) //Bridge
641			{
642				Name (_ADR, 0x00010000)
643
644				Device (EXAR)
645				{
646					Name (_ADR, 0x0000)
647
648					Name (_DSD, Package ()
649					{
650						ToUUID("daffd814-6eba-4d8c-8a91-bc9bbf4aa301"),
651						Package ()
652						{
653							Package ()
654							{
655								"gpio-line-names",
656								Package ()
657								{
658									"mode_232",
659									"mode_422",
660									"mode_485",
661									"misc_1",
662									"misc_2",
663									"misc_3",
664									"",
665									"",
666									"aux_1",
667									"aux_2",
668									"aux_3",
669								}
670							}
671						}
672					})
673				}
674			}
675		}
676	}
677
678The location "_SB.PCI0.RP02" is obtained by the above investigation in the
679dsdt.dsl table, whereas the device names "BRG1", "BRG2" and "EXAR" are
680created analyzing the position of the Exar UART in the PCI bus topology.
681
682References
683==========
684
685[1] Documentation/firmware-guide/acpi/gpio-properties.rst
686
687[2] Documentation/admin-guide/acpi/initrd_table_override.rst
688
689[3] ACPI Specifications, Version 6.3 - Paragraph 6.1.1 _ADR Address)
690    https://uefi.org/sites/default/files/resources/ACPI_6_3_May16.pdf,
691    referenced 2020-11-18
692