Linux 5.19-rc3
[linux-2.6-block.git] / drivers / acpi / glue.c
CommitLineData
55716d26 1// SPDX-License-Identifier: GPL-2.0-only
4e10d12a
DSL
2/*
3 * Link physical devices with ACPI devices support
4 *
5 * Copyright (c) 2005 David Shaohua Li <shaohua.li@intel.com>
6 * Copyright (c) 2005 Intel Corp.
4e10d12a 7 */
d4f54a18 8
e2935abb
HG
9#define pr_fmt(fmt) "ACPI: " fmt
10
d4f54a18 11#include <linux/acpi_iort.h>
214f2c90 12#include <linux/export.h>
4e10d12a
DSL
13#include <linux/init.h>
14#include <linux/list.h>
15#include <linux/device.h>
5a0e3ad6 16#include <linux/slab.h>
4e10d12a
DSL
17#include <linux/rwsem.h>
18#include <linux/acpi.h>
d0562674 19#include <linux/dma-mapping.h>
47954481
RW
20#include <linux/pci.h>
21#include <linux/pci-acpi.h>
d4f54a18 22#include <linux/platform_device.h>
4e10d12a 23
a192a958
LB
24#include "internal.h"
25
4e10d12a
DSL
26static LIST_HEAD(bus_type_list);
27static DECLARE_RWSEM(bus_type_sem);
28
1033f904 29#define PHYSICAL_NODE_STRING "physical_node"
007ccfcf 30#define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
1033f904 31
4e10d12a
DSL
32int register_acpi_bus_type(struct acpi_bus_type *type)
33{
34 if (acpi_disabled)
35 return -ENODEV;
e3f02c52 36 if (type && type->match && type->find_companion) {
4e10d12a
DSL
37 down_write(&bus_type_sem);
38 list_add_tail(&type->list, &bus_type_list);
39 up_write(&bus_type_sem);
e2935abb 40 pr_info("bus type %s registered\n", type->name);
4e10d12a
DSL
41 return 0;
42 }
43 return -ENODEV;
44}
91e4d5a1 45EXPORT_SYMBOL_GPL(register_acpi_bus_type);
4e10d12a 46
4e10d12a
DSL
47int unregister_acpi_bus_type(struct acpi_bus_type *type)
48{
49 if (acpi_disabled)
50 return 0;
51 if (type) {
52 down_write(&bus_type_sem);
53 list_del_init(&type->list);
54 up_write(&bus_type_sem);
e2935abb 55 pr_info("bus type %s unregistered\n", type->name);
4e10d12a
DSL
56 return 0;
57 }
58 return -ENODEV;
59}
91e4d5a1 60EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
4e10d12a 61
53540098 62static struct acpi_bus_type *acpi_get_bus_type(struct device *dev)
4e10d12a
DSL
63{
64 struct acpi_bus_type *tmp, *ret = NULL;
65
66 down_read(&bus_type_sem);
67 list_for_each_entry(tmp, &bus_type_list, list) {
53540098 68 if (tmp->match(dev)) {
4e10d12a
DSL
69 ret = tmp;
70 break;
71 }
72 }
73 up_read(&bus_type_sem);
74 return ret;
75}
76
11b88ee2
RW
77#define FIND_CHILD_MIN_SCORE 1
78#define FIND_CHILD_MAX_SCORE 2
79
d9fef0c4 80static int find_child_checks(struct acpi_device *adev, bool check_children)
60f75b8e
RW
81{
82 unsigned long long sta;
83 acpi_status status;
84
b7fbf4ce
RW
85 if (check_children && list_empty(&adev->children))
86 return -ENODEV;
87
d9fef0c4 88 status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
11b88ee2 89 if (status == AE_NOT_FOUND)
b7fbf4ce 90 return FIND_CHILD_MIN_SCORE;
60f75b8e 91
b7fbf4ce 92 if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
d9fef0c4 93 return -ENODEV;
60f75b8e 94
c2a6bbaf 95 /*
fdad4e7a
RW
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.]
c2a6bbaf 101 */
b7fbf4ce
RW
102 if (adev->pnp.type.platform_id)
103 return FIND_CHILD_MIN_SCORE;
104
105 return FIND_CHILD_MAX_SCORE;
60f75b8e
RW
106}
107
d9fef0c4
RW
108struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
109 u64 address, bool check_children)
60f75b8e 110{
d9fef0c4
RW
111 struct acpi_device *adev, *ret = NULL;
112 int ret_score = 0;
113
5ce79d20
RW
114 if (!parent)
115 return NULL;
116
d9fef0c4 117 list_for_each_entry(adev, &parent->children, node) {
61a3c78d 118 acpi_bus_address addr = acpi_device_adr(adev);
d9fef0c4
RW
119 int score;
120
61a3c78d 121 if (!adev->pnp.type.bus_address || addr != address)
d9fef0c4
RW
122 continue;
123
124 if (!ret) {
125 /* This is the first matching object. Save it. */
126 ret = adev;
127 continue;
128 }
129 /*
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.
134 *
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
137 * we've just found.
138 */
139 if (!ret_score) {
140 ret_score = find_child_checks(ret, check_children);
141 if (ret_score == FIND_CHILD_MAX_SCORE)
142 return ret;
143 }
144 score = find_child_checks(adev, check_children);
145 if (score == FIND_CHILD_MAX_SCORE) {
146 return adev;
147 } else if (score > ret_score) {
148 ret = adev;
149 ret_score = score;
150 }
4e10d12a 151 }
d9fef0c4 152 return ret;
4e10d12a 153}
9c5ad36d 154EXPORT_SYMBOL_GPL(acpi_find_child_device);
4e10d12a 155
bdbdbf91
RW
156static void acpi_physnode_link_name(char *buf, unsigned int node_id)
157{
158 if (node_id > 0)
159 snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
160 PHYSICAL_NODE_STRING "%u", node_id);
161 else
162 strcpy(buf, PHYSICAL_NODE_STRING);
163}
164
24dee1fc 165int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
4e10d12a 166{
f3fd0c8a 167 struct acpi_device_physical_node *physical_node, *pn;
007ccfcf
RW
168 char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
169 struct list_head *physnode_list;
170 unsigned int node_id;
1033f904 171 int retval = -EINVAL;
4e10d12a 172
ca5b74d2 173 if (has_acpi_companion(dev)) {
24dee1fc 174 if (acpi_dev) {
7b199811 175 dev_warn(dev, "ACPI companion already set\n");
f3fd0c8a
RW
176 return -EINVAL;
177 } else {
7b199811 178 acpi_dev = ACPI_COMPANION(dev);
f3fd0c8a 179 }
4e10d12a 180 }
7b199811 181 if (!acpi_dev)
f3fd0c8a 182 return -EINVAL;
1033f904 183
4cbaba4e 184 acpi_dev_get(acpi_dev);
4e10d12a 185 get_device(dev);
f3fd0c8a 186 physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
1033f904
LT
187 if (!physical_node) {
188 retval = -ENOMEM;
189 goto err;
4e10d12a 190 }
4e10d12a 191
1033f904 192 mutex_lock(&acpi_dev->physical_node_lock);
f3fd0c8a 193
007ccfcf
RW
194 /*
195 * Keep the list sorted by node_id so that the IDs of removed nodes can
196 * be recycled easily.
197 */
198 physnode_list = &acpi_dev->physical_node_list;
199 node_id = 0;
200 list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
201 /* Sanity check. */
f3fd0c8a 202 if (pn->dev == dev) {
3342c753
RW
203 mutex_unlock(&acpi_dev->physical_node_lock);
204
f3fd0c8a 205 dev_warn(dev, "Already associated with ACPI node\n");
3342c753 206 kfree(physical_node);
7b199811 207 if (ACPI_COMPANION(dev) != acpi_dev)
3342c753 208 goto err;
3fe444ad 209
3342c753 210 put_device(dev);
4cbaba4e 211 acpi_dev_put(acpi_dev);
3342c753 212 return 0;
f3fd0c8a 213 }
007ccfcf
RW
214 if (pn->node_id == node_id) {
215 physnode_list = &pn->node;
216 node_id++;
217 }
1071695f
DB
218 }
219
007ccfcf 220 physical_node->node_id = node_id;
1033f904 221 physical_node->dev = dev;
007ccfcf 222 list_add(&physical_node->node, physnode_list);
1033f904 223 acpi_dev->physical_node_count++;
f3fd0c8a 224
ca5b74d2 225 if (!has_acpi_companion(dev))
7b199811 226 ACPI_COMPANION_SET(dev, acpi_dev);
1033f904 227
bdbdbf91 228 acpi_physnode_link_name(physical_node_name, node_id);
1033f904 229 retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
f501b6ec 230 physical_node_name);
464c1147
RW
231 if (retval)
232 dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
233 physical_node_name, retval);
234
1033f904 235 retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
f501b6ec 236 "firmware_node");
464c1147
RW
237 if (retval)
238 dev_err(dev, "Failed to create link firmware_node (%d)\n",
239 retval);
1033f904 240
40055206
RW
241 mutex_unlock(&acpi_dev->physical_node_lock);
242
1033f904
LT
243 if (acpi_dev->wakeup.flags.valid)
244 device_set_wakeup_capable(dev, true);
245
4e10d12a 246 return 0;
1033f904
LT
247
248 err:
7b199811 249 ACPI_COMPANION_SET(dev, NULL);
1033f904 250 put_device(dev);
4cbaba4e 251 acpi_dev_put(acpi_dev);
1033f904 252 return retval;
4e10d12a 253}
ac212b69 254EXPORT_SYMBOL_GPL(acpi_bind_one);
4e10d12a 255
ac212b69 256int acpi_unbind_one(struct device *dev)
4e10d12a 257{
7b199811 258 struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
1033f904 259 struct acpi_device_physical_node *entry;
1033f904 260
7b199811 261 if (!acpi_dev)
4e10d12a 262 return 0;
1071695f 263
1033f904 264 mutex_lock(&acpi_dev->physical_node_lock);
3e332783
RW
265
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];
269
270 list_del(&entry->node);
271 acpi_dev->physical_node_count--;
272
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");
7b199811 276 ACPI_COMPANION_SET(dev, NULL);
a104b4d4 277 /* Drop references taken by acpi_bind_one(). */
3e332783 278 put_device(dev);
4cbaba4e 279 acpi_dev_put(acpi_dev);
3e332783
RW
280 kfree(entry);
281 break;
282 }
283
1033f904 284 mutex_unlock(&acpi_dev->physical_node_lock);
4e10d12a
DSL
285 return 0;
286}
ac212b69 287EXPORT_SYMBOL_GPL(acpi_unbind_one);
4e10d12a 288
d0b8e398 289void acpi_device_notify(struct device *dev)
4e10d12a 290{
9cb32acf 291 struct acpi_device *adev;
11909ca1 292 int ret;
4e10d12a 293
f3fd0c8a 294 ret = acpi_bind_one(dev, NULL);
42878a9f 295 if (ret) {
2ef52366
RW
296 struct acpi_bus_type *type = acpi_get_bus_type(dev);
297
42878a9f
RW
298 if (!type)
299 goto err;
e3f02c52
RW
300
301 adev = type->find_companion(dev);
302 if (!adev) {
42878a9f 303 dev_dbg(dev, "ACPI companion not found\n");
42878a9f 304 goto err;
11909ca1 305 }
24dee1fc 306 ret = acpi_bind_one(dev, adev);
11909ca1 307 if (ret)
42878a9f 308 goto err;
11909ca1 309
2ef52366
RW
310 if (type->setup) {
311 type->setup(dev);
312 goto done;
313 }
47954481 314 } else {
2ef52366 315 adev = ACPI_COMPANION(dev);
d4f54a18 316
2ef52366
RW
317 if (dev_is_pci(dev)) {
318 pci_acpi_setup(dev, adev);
319 goto done;
320 } else if (dev_is_platform(dev)) {
321 acpi_configure_pmsi_domain(dev);
322 }
47954481 323 }
d4f54a18 324
2ef52366 325 if (adev->handler && adev->handler->bind)
9cb32acf 326 adev->handler->bind(dev);
4e10d12a 327
2ef52366 328done:
42878a9f
RW
329 acpi_handle_debug(ACPI_HANDLE(dev), "Bound to device %s\n",
330 dev_name(dev));
4e10d12a 331
7d625e5b 332 return;
42878a9f
RW
333
334err:
335 dev_dbg(dev, "No ACPI support\n");
4e10d12a
DSL
336}
337
d0b8e398 338void acpi_device_notify_remove(struct device *dev)
4e10d12a 339{
9cb32acf 340 struct acpi_device *adev = ACPI_COMPANION(dev);
11909ca1 341
9cb32acf 342 if (!adev)
7d625e5b 343 return;
9cb32acf 344
c4d19838 345 if (dev_is_pci(dev))
47954481 346 pci_acpi_cleanup(dev, adev);
9cb32acf
RW
347 else if (adev->handler && adev->handler->unbind)
348 adev->handler->unbind(dev);
11909ca1 349
4e10d12a 350 acpi_unbind_one(dev);
4e10d12a 351}