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