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