1 // SPDX-License-Identifier: GPL-2.0-only
3 * Link physical devices with ACPI devices support
6 * Copyright (c) 2005 Intel Corp.
9 #define pr_fmt(fmt) "ACPI: " fmt
11 #include <linux/acpi_iort.h>
12 #include <linux/export.h>
13 #include <linux/init.h>
14 #include <linux/list.h>
15 #include <linux/device.h>
16 #include <linux/slab.h>
17 #include <linux/rwsem.h>
18 #include <linux/acpi.h>
19 #include <linux/dma-mapping.h>
20 #include <linux/pci.h>
21 #include <linux/pci-acpi.h>
22 #include <linux/platform_device.h>
26 static LIST_HEAD(bus_type_list);
27 static DECLARE_RWSEM(bus_type_sem);
29 #define PHYSICAL_NODE_STRING "physical_node"
30 #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
32 int register_acpi_bus_type(struct acpi_bus_type *type)
36 if (type && type->match && type->find_companion) {
37 down_write(&bus_type_sem);
38 list_add_tail(&type->list, &bus_type_list);
39 up_write(&bus_type_sem);
40 pr_info("bus type %s registered\n", type->name);
45 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
47 int unregister_acpi_bus_type(struct acpi_bus_type *type)
52 down_write(&bus_type_sem);
53 list_del_init(&type->list);
54 up_write(&bus_type_sem);
55 pr_info("bus type %s unregistered\n", type->name);
60 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
62 static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
64 struct acpi_bus_type *tmp, *ret = NULL;
66 down_read(&bus_type_sem);
67 list_for_each_entry(tmp, &bus_type_list, list) {
68 if (tmp->match(dev)) {
73 up_read(&bus_type_sem);
77 #define FIND_CHILD_MIN_SCORE 1
78 #define FIND_CHILD_MAX_SCORE 2
80 static int find_child_checks(struct acpi_device *adev, bool check_children)
82 bool sta_present = true;
83 unsigned long long sta;
86 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
87 if (status == AE_NOT_FOUND)
89 else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
92 if (check_children && list_empty(&adev->children))
96 * If the device has a _HID returning a valid ACPI/PNP device ID, it is
97 * better to make it look less attractive here, so that the other device
98 * with the same _ADR value (that may not have a valid device ID) can be
99 * matched going forward. [This means a second spec violation in a row,
100 * so whatever we do here is best effort anyway.]
102 return sta_present && !adev->pnp.type.platform_id ?
103 FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
106 struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
107 u64 address, bool check_children)
109 struct acpi_device *adev, *ret = NULL;
115 list_for_each_entry(adev, &parent->children, node) {
116 acpi_bus_address addr = acpi_device_adr(adev);
119 if (!adev->pnp.type.bus_address || addr != address)
123 /* This is the first matching object. Save it. */
128 * There is more than one matching device object with the same
129 * _ADR value. That really is unexpected, so we are kind of
130 * beyond the scope of the spec here. We have to choose which
131 * one to return, though.
133 * First, check if the previously found object is good enough
134 * and return it if so. Second, do the same for the object that
138 ret_score = find_child_checks(ret, check_children);
139 if (ret_score == FIND_CHILD_MAX_SCORE)
142 score = find_child_checks(adev, check_children);
143 if (score == FIND_CHILD_MAX_SCORE) {
145 } else if (score > ret_score) {
152 EXPORT_SYMBOL_GPL(acpi_find_child_device);
154 static void acpi_physnode_link_name(char *buf, unsigned int node_id)
157 snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
158 PHYSICAL_NODE_STRING "%u", node_id);
160 strcpy(buf, PHYSICAL_NODE_STRING);
163 int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
165 struct acpi_device_physical_node *physical_node, *pn;
166 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
167 struct list_head *physnode_list;
168 unsigned int node_id;
169 int retval = -EINVAL;
171 if (has_acpi_companion(dev)) {
173 dev_warn(dev, "ACPI companion already set\n");
176 acpi_dev = ACPI_COMPANION(dev);
182 acpi_dev_get(acpi_dev);
184 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
185 if (!physical_node) {
190 mutex_lock(&acpi_dev->physical_node_lock);
193 * Keep the list sorted by node_id so that the IDs of removed nodes can
194 * be recycled easily.
196 physnode_list = &acpi_dev->physical_node_list;
198 list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
200 if (pn->dev == dev) {
201 mutex_unlock(&acpi_dev->physical_node_lock);
203 dev_warn(dev, "Already associated with ACPI node\n");
204 kfree(physical_node);
205 if (ACPI_COMPANION(dev) != acpi_dev)
209 acpi_dev_put(acpi_dev);
212 if (pn->node_id == node_id) {
213 physnode_list = &pn->node;
218 physical_node->node_id = node_id;
219 physical_node->dev = dev;
220 list_add(&physical_node->node, physnode_list);
221 acpi_dev->physical_node_count++;
223 if (!has_acpi_companion(dev))
224 ACPI_COMPANION_SET(dev, acpi_dev);
226 acpi_physnode_link_name(physical_node_name, node_id);
227 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
230 dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
231 physical_node_name, retval);
233 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
236 dev_err(dev, "Failed to create link firmware_node (%d)\n",
239 mutex_unlock(&acpi_dev->physical_node_lock);
241 if (acpi_dev->wakeup.flags.valid)
242 device_set_wakeup_capable(dev, true);
247 ACPI_COMPANION_SET(dev, NULL);
249 acpi_dev_put(acpi_dev);
252 EXPORT_SYMBOL_GPL(acpi_bind_one);
254 int acpi_unbind_one(struct device *dev)
256 struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
257 struct acpi_device_physical_node *entry;
262 mutex_lock(&acpi_dev->physical_node_lock);
264 list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
265 if (entry->dev == dev) {
266 char physnode_name[PHYSICAL_NODE_NAME_SIZE];
268 list_del(&entry->node);
269 acpi_dev->physical_node_count--;
271 acpi_physnode_link_name(physnode_name, entry->node_id);
272 sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
273 sysfs_remove_link(&dev->kobj, "firmware_node");
274 ACPI_COMPANION_SET(dev, NULL);
275 /* Drop references taken by acpi_bind_one(). */
277 acpi_dev_put(acpi_dev);
282 mutex_unlock(&acpi_dev->physical_node_lock);
285 EXPORT_SYMBOL_GPL(acpi_unbind_one);
287 void acpi_device_notify(struct device *dev)
289 struct acpi_device *adev;
292 ret = acpi_bind_one(dev, NULL);
294 struct acpi_bus_type *type = acpi_get_bus_type(dev);
299 adev = type->find_companion(dev);
301 dev_dbg(dev, "ACPI companion not found\n");
304 ret = acpi_bind_one(dev, adev);
313 adev = ACPI_COMPANION(dev);
315 if (dev_is_pci(dev)) {
316 pci_acpi_setup(dev, adev);
318 } else if (dev_is_platform(dev)) {
319 acpi_configure_pmsi_domain(dev);
323 if (adev->handler && adev->handler->bind)
324 adev->handler->bind(dev);
327 acpi_handle_debug(ACPI_HANDLE(dev), "Bound to device %s\n",
333 dev_dbg(dev, "No ACPI support\n");
336 void acpi_device_notify_remove(struct device *dev)
338 struct acpi_device *adev = ACPI_COMPANION(dev);
344 pci_acpi_cleanup(dev, adev);
345 else if (adev->handler && adev->handler->unbind)
346 adev->handler->unbind(dev);
348 acpi_unbind_one(dev);
351 int acpi_dev_turn_off_if_unused(struct device *dev, void *not_used)
353 struct acpi_device *adev = to_acpi_device(dev);
356 * Skip device objects with device IDs, because they may be in use even
357 * if they are not companions of any physical device objects.
359 if (adev->pnp.type.hardware_id)
362 mutex_lock(&adev->physical_node_lock);
365 * Device objects without device IDs are not in use if they have no
366 * corresponding physical device objects.
368 if (list_empty(&adev->physical_node_list))
369 acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
371 mutex_unlock(&adev->physical_node_lock);