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