Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

ACPI / driver core: Store an ACPI device pointer in struct acpi_dev_node

Modify struct acpi_dev_node to contain a pointer to struct acpi_device
associated with the given device object (that is, its ACPI companion
device) instead of an ACPI handle corresponding to it. Introduce two
new macros for manipulating that pointer in a CONFIG_ACPI-safe way,
ACPI_COMPANION() and ACPI_COMPANION_SET(), and rework the
ACPI_HANDLE() macro to take the above changes into account.
Drop the ACPI_HANDLE_SET() macro entirely and rework its users to
use ACPI_COMPANION_SET() instead. For some of them who used to
pass the result of acpi_get_child() directly to ACPI_HANDLE_SET()
introduce a helper routine acpi_preset_companion() doing an
equivalent thing.

The main motivation for doing this is that there are things
represented by struct acpi_device objects that don't have valid
ACPI handles (so called fixed ACPI hardware features, such as
power and sleep buttons) and we would like to create platform
device objects for them and "glue" them to their ACPI companions
in the usual way (which currently is impossible due to the
lack of valid ACPI handles). However, there are more reasons
why it may be useful.

First, struct acpi_device pointers allow of much better type checking
than void pointers which are ACPI handles, so it should be more
difficult to write buggy code using modified struct acpi_dev_node
and the new macros. Second, the change should help to reduce (over
time) the number of places in which the result of ACPI_HANDLE() is
passed to acpi_bus_get_device() in order to obtain a pointer to the
struct acpi_device associated with the given "physical" device,
because now that pointer is returned by ACPI_COMPANION() directly.
Finally, the change should make it easier to write generic code that
will build both for CONFIG_ACPI set and unset without adding explicit
compiler directives to it.

Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Tested-by: Mika Westerberg <mika.westerberg@linux.intel.com> # on Haswell
Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>
Reviewed-by: Aaron Lu <aaron.lu@intel.com> # for ATA and SDIO part

+70 -68
+1 -1
arch/ia64/hp/common/sba_iommu.c
··· 1992 1992 if (PCI_CONTROLLER(bus)->iommu) 1993 1993 return; 1994 1994 1995 - handle = PCI_CONTROLLER(bus)->acpi_handle; 1995 + handle = acpi_device_handle(PCI_CONTROLLER(bus)->companion); 1996 1996 if (!handle) 1997 1997 return; 1998 1998
+1 -1
arch/ia64/include/asm/pci.h
··· 95 95 }; 96 96 97 97 struct pci_controller { 98 - void *acpi_handle; 98 + struct acpi_device *companion; 99 99 void *iommu; 100 100 int segment; 101 101 int node; /* nearest node with memory or -1 for global allocation */
+3 -3
arch/ia64/pci/pci.c
··· 436 436 if (!controller) 437 437 return NULL; 438 438 439 - controller->acpi_handle = device->handle; 439 + controller->companion = device; 440 440 441 - pxm = acpi_get_pxm(controller->acpi_handle); 441 + pxm = acpi_get_pxm(device->handle); 442 442 #ifdef CONFIG_NUMA 443 443 if (pxm >= 0) 444 444 controller->node = pxm_to_node(pxm); ··· 489 489 { 490 490 struct pci_controller *controller = bridge->bus->sysdata; 491 491 492 - ACPI_HANDLE_SET(&bridge->dev, controller->acpi_handle); 492 + ACPI_COMPANION_SET(&bridge->dev, controller->companion); 493 493 return 0; 494 494 } 495 495
+2 -2
arch/ia64/sn/kernel/io_acpi_init.c
··· 132 132 struct acpi_resource_vendor_typed *vendor; 133 133 134 134 135 - handle = PCI_CONTROLLER(bus)->acpi_handle; 135 + handle = acpi_device_handle(PCI_CONTROLLER(bus)->companion); 136 136 status = acpi_get_vendor_resource(handle, METHOD_NAME__CRS, 137 137 &sn_uuid, &buffer); 138 138 if (ACPI_FAILURE(status)) { ··· 360 360 acpi_status status; 361 361 struct acpi_buffer name_buffer = { ACPI_ALLOCATE_BUFFER, NULL }; 362 362 363 - rootbus_handle = PCI_CONTROLLER(dev)->acpi_handle; 363 + rootbus_handle = acpi_device_handle(PCI_CONTROLLER(dev)->companion); 364 364 status = acpi_evaluate_integer(rootbus_handle, METHOD_NAME__SEG, NULL, 365 365 &segment); 366 366 if (ACPI_SUCCESS(status)) {
+1 -1
arch/x86/include/asm/pci.h
··· 15 15 int domain; /* PCI domain */ 16 16 int node; /* NUMA node */ 17 17 #ifdef CONFIG_ACPI 18 - void *acpi; /* ACPI-specific data */ 18 + struct acpi_device *companion; /* ACPI companion device */ 19 19 #endif 20 20 #ifdef CONFIG_X86_64 21 21 void *iommu; /* IOMMU private data */
+2 -2
arch/x86/pci/acpi.c
··· 518 518 sd = &info->sd; 519 519 sd->domain = domain; 520 520 sd->node = node; 521 - sd->acpi = device->handle; 521 + sd->companion = device; 522 522 /* 523 523 * Maybe the desired pci bus has been already scanned. In such case 524 524 * it is unnecessary to scan the pci bus with the given domain,busnum. ··· 589 589 { 590 590 struct pci_sysdata *sd = bridge->bus->sysdata; 591 591 592 - ACPI_HANDLE_SET(&bridge->dev, sd->acpi); 592 + ACPI_COMPANION_SET(&bridge->dev, sd->companion); 593 593 return 0; 594 594 } 595 595
+1 -1
drivers/acpi/acpi_platform.c
··· 111 111 pdevinfo.id = -1; 112 112 pdevinfo.res = resources; 113 113 pdevinfo.num_res = count; 114 - pdevinfo.acpi_node.handle = adev->handle; 114 + pdevinfo.acpi_node.companion = adev; 115 115 pdev = platform_device_register_full(&pdevinfo); 116 116 if (IS_ERR(pdev)) { 117 117 dev_err(&adev->dev, "platform device creation failed: %ld\n",
+1 -5
drivers/acpi/device_pm.c
··· 22 22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23 23 */ 24 24 25 - #include <linux/device.h> 25 + #include <linux/acpi.h> 26 26 #include <linux/export.h> 27 27 #include <linux/mutex.h> 28 28 #include <linux/pm_qos.h> 29 29 #include <linux/pm_runtime.h> 30 - 31 - #include <acpi/acpi.h> 32 - #include <acpi/acpi_bus.h> 33 - #include <acpi/acpi_drivers.h> 34 30 35 31 #include "internal.h" 36 32
+23 -24
drivers/acpi/glue.c
··· 197 197 198 198 int acpi_bind_one(struct device *dev, acpi_handle handle) 199 199 { 200 - struct acpi_device *acpi_dev; 201 - acpi_status status; 200 + struct acpi_device *acpi_dev = NULL; 202 201 struct acpi_device_physical_node *physical_node, *pn; 203 202 char physical_node_name[PHYSICAL_NODE_NAME_SIZE]; 204 203 struct list_head *physnode_list; 205 204 unsigned int node_id; 206 205 int retval = -EINVAL; 207 206 208 - if (ACPI_HANDLE(dev)) { 207 + if (ACPI_COMPANION(dev)) { 209 208 if (handle) { 210 - dev_warn(dev, "ACPI handle is already set\n"); 209 + dev_warn(dev, "ACPI companion already set\n"); 211 210 return -EINVAL; 212 211 } else { 213 - handle = ACPI_HANDLE(dev); 212 + acpi_dev = ACPI_COMPANION(dev); 214 213 } 214 + } else { 215 + acpi_bus_get_device(handle, &acpi_dev); 215 216 } 216 - if (!handle) 217 + if (!acpi_dev) 217 218 return -EINVAL; 218 219 219 220 get_device(dev); 220 - status = acpi_bus_get_device(handle, &acpi_dev); 221 - if (ACPI_FAILURE(status)) 222 - goto err; 223 - 224 221 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL); 225 222 if (!physical_node) { 226 223 retval = -ENOMEM; ··· 239 242 240 243 dev_warn(dev, "Already associated with ACPI node\n"); 241 244 kfree(physical_node); 242 - if (ACPI_HANDLE(dev) != handle) 245 + if (ACPI_COMPANION(dev) != acpi_dev) 243 246 goto err; 244 247 245 248 put_device(dev); ··· 256 259 list_add(&physical_node->node, physnode_list); 257 260 acpi_dev->physical_node_count++; 258 261 259 - if (!ACPI_HANDLE(dev)) 260 - ACPI_HANDLE_SET(dev, acpi_dev->handle); 262 + if (!ACPI_COMPANION(dev)) 263 + ACPI_COMPANION_SET(dev, acpi_dev); 261 264 262 265 acpi_physnode_link_name(physical_node_name, node_id); 263 266 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj, ··· 280 283 return 0; 281 284 282 285 err: 283 - ACPI_HANDLE_SET(dev, NULL); 286 + ACPI_COMPANION_SET(dev, NULL); 284 287 put_device(dev); 285 288 return retval; 286 289 } ··· 288 291 289 292 int acpi_unbind_one(struct device *dev) 290 293 { 294 + struct acpi_device *acpi_dev = ACPI_COMPANION(dev); 291 295 struct acpi_device_physical_node *entry; 292 - struct acpi_device *acpi_dev; 293 - acpi_status status; 294 296 295 - if (!ACPI_HANDLE(dev)) 297 + if (!acpi_dev) 296 298 return 0; 297 - 298 - status = acpi_bus_get_device(ACPI_HANDLE(dev), &acpi_dev); 299 - if (ACPI_FAILURE(status)) { 300 - dev_err(dev, "Oops, ACPI handle corrupt in %s()\n", __func__); 301 - return -EINVAL; 302 - } 303 299 304 300 mutex_lock(&acpi_dev->physical_node_lock); 305 301 ··· 306 316 acpi_physnode_link_name(physnode_name, entry->node_id); 307 317 sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name); 308 318 sysfs_remove_link(&dev->kobj, "firmware_node"); 309 - ACPI_HANDLE_SET(dev, NULL); 319 + ACPI_COMPANION_SET(dev, NULL); 310 320 /* acpi_bind_one() increase refcnt by one. */ 311 321 put_device(dev); 312 322 kfree(entry); ··· 317 327 return 0; 318 328 } 319 329 EXPORT_SYMBOL_GPL(acpi_unbind_one); 330 + 331 + void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr) 332 + { 333 + struct acpi_device *adev; 334 + 335 + if (!acpi_bus_get_device(acpi_get_child(parent, addr), &adev)) 336 + ACPI_COMPANION_SET(dev, adev); 337 + } 338 + EXPORT_SYMBOL_GPL(acpi_preset_companion); 320 339 321 340 static int acpi_platform_notify(struct device *dev) 322 341 {
+2 -2
drivers/ata/libata-acpi.c
··· 185 185 if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_handle) 186 186 return; 187 187 188 - ACPI_HANDLE_SET(&ap->tdev, acpi_get_child(host_handle, ap->port_no)); 188 + acpi_preset_companion(&ap->tdev, host_handle, ap->port_no); 189 189 190 190 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0) 191 191 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID; ··· 222 222 parent_handle = port_handle; 223 223 } 224 224 225 - ACPI_HANDLE_SET(&dev->tdev, acpi_get_child(parent_handle, adr)); 225 + acpi_preset_companion(&dev->tdev, parent_handle, adr); 226 226 227 227 register_hotplug_dock_device(ata_dev_acpi_handle(dev), 228 228 &ata_acpi_dev_dock_ops, dev, NULL, NULL);
+2 -2
drivers/base/platform.c
··· 432 432 goto err_alloc; 433 433 434 434 pdev->dev.parent = pdevinfo->parent; 435 - ACPI_HANDLE_SET(&pdev->dev, pdevinfo->acpi_node.handle); 435 + ACPI_COMPANION_SET(&pdev->dev, pdevinfo->acpi_node.companion); 436 436 437 437 if (pdevinfo->dma_mask) { 438 438 /* ··· 463 463 ret = platform_device_add(pdev); 464 464 if (ret) { 465 465 err: 466 - ACPI_HANDLE_SET(&pdev->dev, NULL); 466 + ACPI_COMPANION_SET(&pdev->dev, NULL); 467 467 kfree(pdev->dev.dma_mask); 468 468 469 469 err_alloc:
+1
drivers/gpio/gpiolib.c
··· 13 13 #include <linux/acpi_gpio.h> 14 14 #include <linux/idr.h> 15 15 #include <linux/slab.h> 16 + #include <linux/acpi.h> 16 17 17 18 #define CREATE_TRACE_POINTS 18 19 #include <trace/events/gpio.h>
+1 -2
drivers/gpu/drm/radeon/radeon_atpx_handler.c
··· 8 8 */ 9 9 #include <linux/vga_switcheroo.h> 10 10 #include <linux/slab.h> 11 - #include <acpi/acpi.h> 12 - #include <acpi/acpi_bus.h> 11 + #include <linux/acpi.h> 13 12 #include <linux/pci.h> 14 13 15 14 #include "radeon_acpi.h"
+1 -1
drivers/hid/i2c-hid/i2c-hid.c
··· 1012 1012 hid->hid_get_raw_report = i2c_hid_get_raw_report; 1013 1013 hid->hid_output_raw_report = i2c_hid_output_raw_report; 1014 1014 hid->dev.parent = &client->dev; 1015 - ACPI_HANDLE_SET(&hid->dev, ACPI_HANDLE(&client->dev)); 1015 + ACPI_COMPANION_SET(&hid->dev, ACPI_COMPANION(&client->dev)); 1016 1016 hid->bus = BUS_I2C; 1017 1017 hid->version = le16_to_cpu(ihid->hdesc.bcdVersion); 1018 1018 hid->vendor = le16_to_cpu(ihid->hdesc.wVendorID);
+2 -2
drivers/i2c/i2c-core.c
··· 674 674 client->dev.bus = &i2c_bus_type; 675 675 client->dev.type = &i2c_client_type; 676 676 client->dev.of_node = info->of_node; 677 - ACPI_HANDLE_SET(&client->dev, info->acpi_node.handle); 677 + ACPI_COMPANION_SET(&client->dev, info->acpi_node.companion); 678 678 679 679 /* For 10-bit clients, add an arbitrary offset to avoid collisions */ 680 680 dev_set_name(&client->dev, "%d-%04x", i2c_adapter_id(adap), ··· 1103 1103 return AE_OK; 1104 1104 1105 1105 memset(&info, 0, sizeof(info)); 1106 - info.acpi_node.handle = handle; 1106 + info.acpi_node.companion = adev; 1107 1107 info.irq = -1; 1108 1108 1109 1109 INIT_LIST_HEAD(&resource_list);
+1 -2
drivers/ide/ide-acpi.c
··· 7 7 * Copyright (C) 2006 Hannes Reinecke 8 8 */ 9 9 10 + #include <linux/acpi.h> 10 11 #include <linux/ata.h> 11 12 #include <linux/delay.h> 12 13 #include <linux/device.h> ··· 19 18 #include <linux/pci.h> 20 19 #include <linux/dmi.h> 21 20 #include <linux/module.h> 22 - 23 - #include <acpi/acpi_bus.h> 24 21 25 22 #define REGS_PER_GTF 7 26 23
+1 -2
drivers/mmc/core/sdio_bus.c
··· 308 308 struct mmc_host *host = func->card->host; 309 309 u64 addr = (host->slotno << 16) | func->num; 310 310 311 - ACPI_HANDLE_SET(&func->dev, 312 - acpi_get_child(ACPI_HANDLE(host->parent), addr)); 311 + acpi_preset_companion(&func->dev, ACPI_HANDLE(host->parent), addr); 313 312 } 314 313 #else 315 314 static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
+4 -4
drivers/pci/hotplug/sgi_hotplug.c
··· 9 9 * Work to add BIOS PROM support was completed by Mike Habeck. 10 10 */ 11 11 12 + #include <linux/acpi.h> 12 13 #include <linux/init.h> 13 14 #include <linux/kernel.h> 14 15 #include <linux/module.h> ··· 30 29 #include <asm/sn/sn_feature_sets.h> 31 30 #include <asm/sn/sn_sal.h> 32 31 #include <asm/sn/types.h> 33 - #include <linux/acpi.h> 34 32 #include <asm/sn/acpi.h> 35 33 36 34 #include "../pci.h" ··· 414 414 acpi_handle rethandle; 415 415 acpi_status ret; 416 416 417 - phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle; 417 + phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion); 418 418 419 419 if (acpi_bus_get_device(phandle, &pdevice)) { 420 420 dev_dbg(&slot->pci_bus->self->dev, ··· 495 495 496 496 /* free the ACPI resources for the slot */ 497 497 if (SN_ACPI_BASE_SUPPORT() && 498 - PCI_CONTROLLER(slot->pci_bus)->acpi_handle) { 498 + PCI_CONTROLLER(slot->pci_bus)->companion) { 499 499 unsigned long long adr; 500 500 struct acpi_device *device; 501 501 acpi_handle phandle; ··· 504 504 acpi_status ret; 505 505 506 506 /* Get the rootbus node pointer */ 507 - phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle; 507 + phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion); 508 508 509 509 acpi_scan_lock_acquire(); 510 510 /*
+1 -1
drivers/spi/spi.c
··· 1144 1144 return AE_NO_MEMORY; 1145 1145 } 1146 1146 1147 - ACPI_HANDLE_SET(&spi->dev, handle); 1147 + ACPI_COMPANION_SET(&spi->dev, adev); 1148 1148 spi->irq = -1; 1149 1149 1150 1150 INIT_LIST_HEAD(&resource_list);
+1 -1
include/acpi/acpi_bus.h
··· 431 431 { 432 432 return acpi_find_child(handle, addr, false); 433 433 } 434 + void acpi_preset_companion(struct device *dev, acpi_handle parent, u64 addr); 434 435 int acpi_is_root_bridge(acpi_handle); 435 436 struct acpi_pci_root *acpi_pci_find_root(acpi_handle handle); 436 - #define DEVICE_ACPI_HANDLE(dev) ((acpi_handle)ACPI_HANDLE(dev)) 437 437 438 438 int acpi_enable_wakeup_device_power(struct acpi_device *dev, int state); 439 439 int acpi_disable_wakeup_device_power(struct acpi_device *dev);
+15
include/linux/acpi.h
··· 44 44 #include <acpi/acpi_numa.h> 45 45 #include <asm/acpi.h> 46 46 47 + static inline acpi_handle acpi_device_handle(struct acpi_device *adev) 48 + { 49 + return adev ? adev->handle : NULL; 50 + } 51 + 52 + #define ACPI_COMPANION(dev) ((dev)->acpi_node.companion) 53 + #define ACPI_COMPANION_SET(dev, adev) ACPI_COMPANION(dev) = (adev) 54 + #define ACPI_HANDLE(dev) acpi_device_handle(ACPI_COMPANION(dev)) 55 + 47 56 enum acpi_irq_model_id { 48 57 ACPI_IRQ_MODEL_PIC = 0, 49 58 ACPI_IRQ_MODEL_IOAPIC, ··· 410 401 411 402 #define acpi_disabled 1 412 403 404 + #define ACPI_COMPANION(dev) (NULL) 405 + #define ACPI_COMPANION_SET(dev, adev) do { } while (0) 406 + #define ACPI_HANDLE(dev) (NULL) 407 + 413 408 static inline void acpi_early_init(void) { } 414 409 415 410 static inline int early_acpi_boot_init(void) ··· 481 468 #define ACPI_PTR(_ptr) (NULL) 482 469 483 470 #endif /* !CONFIG_ACPI */ 471 + 472 + #define DEVICE_ACPI_HANDLE(dev) ACPI_HANDLE(dev) 484 473 485 474 #ifdef CONFIG_ACPI 486 475 void acpi_os_set_prepare_sleep(int (*func)(u8 sleep_state,
+3 -9
include/linux/device.h
··· 644 644 unsigned long segment_boundary_mask; 645 645 }; 646 646 647 + struct acpi_device; 648 + 647 649 struct acpi_dev_node { 648 650 #ifdef CONFIG_ACPI 649 - void *handle; 651 + struct acpi_device *companion; 650 652 #endif 651 653 }; 652 654 ··· 791 789 { 792 790 return container_of(kobj, struct device, kobj); 793 791 } 794 - 795 - #ifdef CONFIG_ACPI 796 - #define ACPI_HANDLE(dev) ((dev)->acpi_node.handle) 797 - #define ACPI_HANDLE_SET(dev, _handle_) (dev)->acpi_node.handle = (_handle_) 798 - #else 799 - #define ACPI_HANDLE(dev) (NULL) 800 - #define ACPI_HANDLE_SET(dev, _handle_) do { } while (0) 801 - #endif 802 792 803 793 /* Get the wakeup routines, which depend on struct device */ 804 794 #include <linux/pm_wakeup.h>