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