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 unsigned long long sta;
85 if (check_children && list_empty(&adev->children))
88 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
89 if (status == AE_NOT_FOUND)
90 return FIND_CHILD_MIN_SCORE;
92 if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
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 if (adev->pnp.type.platform_id)
103 return FIND_CHILD_MIN_SCORE;
105 return FIND_CHILD_MAX_SCORE;
108 struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
109 u64 address, bool check_children)
111 struct acpi_device *adev, *ret = NULL;
117 list_for_each_entry(adev, &parent->children, node) {
118 acpi_bus_address addr = acpi_device_adr(adev);
121 if (!adev->pnp.type.bus_address || addr != address)
125 /* This is the first matching object. Save it. */
130 * There is more than one matching device object with the same
131 * _ADR value. That really is unexpected, so we are kind of
132 * beyond the scope of the spec here. We have to choose which
133 * one to return, though.
135 * First, check if the previously found object is good enough
136 * and return it if so. Second, do the same for the object that
140 ret_score = find_child_checks(ret, check_children);
141 if (ret_score == FIND_CHILD_MAX_SCORE)
144 score = find_child_checks(adev, check_children);
145 if (score == FIND_CHILD_MAX_SCORE) {
147 } else if (score > ret_score) {
154 EXPORT_SYMBOL_GPL(acpi_find_child_device);
156 static void acpi_physnode_link_name(char *buf, unsigned int node_id)
159 snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
160 PHYSICAL_NODE_STRING "%u", node_id);
162 strcpy(buf, PHYSICAL_NODE_STRING);
165 int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
167 struct acpi_device_physical_node *physical_node, *pn;
168 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
169 struct list_head *physnode_list;
170 unsigned int node_id;
171 int retval = -EINVAL;
173 if (has_acpi_companion(dev)) {
175 dev_warn(dev, "ACPI companion already set\n");
178 acpi_dev = ACPI_COMPANION(dev);
184 acpi_dev_get(acpi_dev);
186 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
187 if (!physical_node) {
192 mutex_lock(&acpi_dev->physical_node_lock);
195 * Keep the list sorted by node_id so that the IDs of removed nodes can
196 * be recycled easily.
198 physnode_list = &acpi_dev->physical_node_list;
200 list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
202 if (pn->dev == dev) {
203 mutex_unlock(&acpi_dev->physical_node_lock);
205 dev_warn(dev, "Already associated with ACPI node\n");
206 kfree(physical_node);
207 if (ACPI_COMPANION(dev) != acpi_dev)
211 acpi_dev_put(acpi_dev);
214 if (pn->node_id == node_id) {
215 physnode_list = &pn->node;
220 physical_node->node_id = node_id;
221 physical_node->dev = dev;
222 list_add(&physical_node->node, physnode_list);
223 acpi_dev->physical_node_count++;
225 if (!has_acpi_companion(dev))
226 ACPI_COMPANION_SET(dev, acpi_dev);
228 acpi_physnode_link_name(physical_node_name, node_id);
229 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
232 dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
233 physical_node_name, retval);
235 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
238 dev_err(dev, "Failed to create link firmware_node (%d)\n",
241 mutex_unlock(&acpi_dev->physical_node_lock);
243 if (acpi_dev->wakeup.flags.valid)
244 device_set_wakeup_capable(dev, true);
249 ACPI_COMPANION_SET(dev, NULL);
251 acpi_dev_put(acpi_dev);
254 EXPORT_SYMBOL_GPL(acpi_bind_one);
256 int acpi_unbind_one(struct device *dev)
258 struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
259 struct acpi_device_physical_node *entry;
264 mutex_lock(&acpi_dev->physical_node_lock);
266 list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
267 if (entry->dev == dev) {
268 char physnode_name[PHYSICAL_NODE_NAME_SIZE];
270 list_del(&entry->node);
271 acpi_dev->physical_node_count--;
273 acpi_physnode_link_name(physnode_name, entry->node_id);
274 sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
275 sysfs_remove_link(&dev->kobj, "firmware_node");
276 ACPI_COMPANION_SET(dev, NULL);
277 /* Drop references taken by acpi_bind_one(). */
279 acpi_dev_put(acpi_dev);
284 mutex_unlock(&acpi_dev->physical_node_lock);
287 EXPORT_SYMBOL_GPL(acpi_unbind_one);
289 void acpi_device_notify(struct device *dev)
291 struct acpi_device *adev;
294 ret = acpi_bind_one(dev, NULL);
296 struct acpi_bus_type *type = acpi_get_bus_type(dev);
301 adev = type->find_companion(dev);
303 dev_dbg(dev, "ACPI companion not found\n");
306 ret = acpi_bind_one(dev, adev);
315 adev = ACPI_COMPANION(dev);
317 if (dev_is_pci(dev)) {
318 pci_acpi_setup(dev, adev);
320 } else if (dev_is_platform(dev)) {
321 acpi_configure_pmsi_domain(dev);
325 if (adev->handler && adev->handler->bind)
326 adev->handler->bind(dev);
329 acpi_handle_debug(ACPI_HANDLE(dev), "Bound to device %s\n",
335 dev_dbg(dev, "No ACPI support\n");
338 void acpi_device_notify_remove(struct device *dev)
340 struct acpi_device *adev = ACPI_COMPANION(dev);
346 pci_acpi_cleanup(dev, adev);
347 else if (adev->handler && adev->handler->unbind)
348 adev->handler->unbind(dev);
350 acpi_unbind_one(dev);