xref: /linux/drivers/acpi/glue.c (revision b91ad39f29ceeee3e3d37eedff6b7c912855623c)
1 // SPDX-License-Identifier: GPL-2.0-only
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.
7  */
8 
9 #define pr_fmt(fmt) "ACPI: " fmt
10 
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>
23 
24 #include "internal.h"
25 
26 static LIST_HEAD(bus_type_list);
27 static DECLARE_RWSEM(bus_type_sem);
28 
29 #define PHYSICAL_NODE_STRING "physical_node"
30 #define PHYSICAL_NODE_NAME_SIZE (sizeof(PHYSICAL_NODE_STRING) + 10)
31 
32 int register_acpi_bus_type(struct acpi_bus_type *type)
33 {
34 	if (acpi_disabled)
35 		return -ENODEV;
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);
41 		return 0;
42 	}
43 	return -ENODEV;
44 }
45 EXPORT_SYMBOL_GPL(register_acpi_bus_type);
46 
47 int 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);
55 		pr_info("bus type %s unregistered\n", type->name);
56 		return 0;
57 	}
58 	return -ENODEV;
59 }
60 EXPORT_SYMBOL_GPL(unregister_acpi_bus_type);
61 
62 static struct acpi_device *acpi_companion_lookup(struct device *dev)
63 {
64 	struct acpi_bus_type *type;
65 
66 	if (!dev->type)
67 		return NULL;
68 
69 	guard(rwsem_read)(&bus_type_sem);
70 
71 	list_for_each_entry(type, &bus_type_list, list) {
72 		struct acpi_device *adev;
73 
74 		if (!type->match(dev))
75 			continue;
76 
77 		adev = type->find_companion(dev);
78 		if (!adev) {
79 			dev_dbg(dev, "ACPI companion not found\n");
80 			return NULL;
81 		}
82 		if (acpi_bind_one(dev, adev)) {
83 			dev_dbg(dev, "Binding to ACPI companion failed\n");
84 			return NULL;
85 		}
86 		if (type->setup)
87 			type->setup(dev);
88 
89 		return adev;
90 	}
91 
92 	return NULL;
93 }
94 
95 #define FIND_CHILD_MIN_SCORE	1
96 #define FIND_CHILD_MID_SCORE	2
97 #define FIND_CHILD_MAX_SCORE	3
98 
99 static int match_any(struct acpi_device *adev, void *not_used)
100 {
101 	return 1;
102 }
103 
104 static bool acpi_dev_has_children(struct acpi_device *adev)
105 {
106 	return acpi_dev_for_each_child(adev, match_any, NULL) > 0;
107 }
108 
109 static int find_child_checks(struct acpi_device *adev, bool check_children)
110 {
111 	if (check_children && !acpi_dev_has_children(adev))
112 		return -ENODEV;
113 
114 	if (!acpi_has_method(adev->handle, METHOD_NAME__STA)) {
115 		/*
116 		 * Special case: backlight device objects without _STA are
117 		 * preferred to other objects with the same _ADR value, because
118 		 * it is more likely that they are actually useful.
119 		 */
120 		if (adev->pnp.type.backlight)
121 			return FIND_CHILD_MID_SCORE;
122 
123 		return FIND_CHILD_MIN_SCORE;
124 	}
125 
126 	if (!acpi_device_is_present(adev))
127 		return -ENODEV;
128 
129 	/*
130 	 * If the device has a _HID returning a valid ACPI/PNP device ID, it is
131 	 * better to make it look less attractive here, so that the other device
132 	 * with the same _ADR value (that may not have a valid device ID) can be
133 	 * matched going forward.  [This means a second spec violation in a row,
134 	 * so whatever we do here is best effort anyway.]
135 	 */
136 	if (adev->pnp.type.platform_id)
137 		return FIND_CHILD_MIN_SCORE;
138 
139 	return FIND_CHILD_MAX_SCORE;
140 }
141 
142 struct find_child_walk_data {
143 	struct acpi_device *adev;
144 	u64 address;
145 	int score;
146 	bool check_sta;
147 	bool check_children;
148 };
149 
150 static int check_one_child(struct acpi_device *adev, void *data)
151 {
152 	struct find_child_walk_data *wd = data;
153 	int score;
154 
155 	if (!adev->pnp.type.bus_address || acpi_device_adr(adev) != wd->address)
156 		return 0;
157 
158 	if (!wd->adev) {
159 		/*
160 		 * This is the first matching object, so save it.  If it is not
161 		 * necessary to look for any other matching objects, stop the
162 		 * search.
163 		 */
164 		wd->adev = adev;
165 		return !(wd->check_sta || wd->check_children);
166 	}
167 
168 	/*
169 	 * There is more than one matching device object with the same _ADR
170 	 * value.  That really is unexpected, so we are kind of beyond the scope
171 	 * of the spec here.  We have to choose which one to return, though.
172 	 *
173 	 * First, get the score for the previously found object and terminate
174 	 * the walk if it is maximum.
175 	*/
176 	if (!wd->score) {
177 		score = find_child_checks(wd->adev, wd->check_children);
178 		if (score == FIND_CHILD_MAX_SCORE)
179 			return 1;
180 
181 		wd->score = score;
182 	}
183 	/*
184 	 * Second, if the object that has just been found has a better score,
185 	 * replace the previously found one with it and terminate the walk if
186 	 * the new score is maximum.
187 	 */
188 	score = find_child_checks(adev, wd->check_children);
189 	if (score > wd->score) {
190 		wd->adev = adev;
191 		if (score == FIND_CHILD_MAX_SCORE)
192 			return 1;
193 
194 		wd->score = score;
195 	}
196 
197 	/* Continue, because there may be better matches. */
198 	return 0;
199 }
200 
201 static struct acpi_device *acpi_find_child(struct acpi_device *parent,
202 					   u64 address, bool check_children,
203 					   bool check_sta)
204 {
205 	struct find_child_walk_data wd = {
206 		.address = address,
207 		.check_children = check_children,
208 		.check_sta = check_sta,
209 		.adev = NULL,
210 		.score = 0,
211 	};
212 
213 	if (parent)
214 		acpi_dev_for_each_child(parent, check_one_child, &wd);
215 
216 	return wd.adev;
217 }
218 
219 struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
220 					   u64 address, bool check_children)
221 {
222 	return acpi_find_child(parent, address, check_children, true);
223 }
224 EXPORT_SYMBOL_GPL(acpi_find_child_device);
225 
226 struct acpi_device *acpi_find_child_by_adr(struct acpi_device *adev,
227 					   acpi_bus_address adr)
228 {
229 	return acpi_find_child(adev, adr, false, false);
230 }
231 EXPORT_SYMBOL_GPL(acpi_find_child_by_adr);
232 
233 static void acpi_physnode_link_name(char *buf, unsigned int node_id)
234 {
235 	if (node_id > 0)
236 		snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
237 			 PHYSICAL_NODE_STRING "%u", node_id);
238 	else
239 		strcpy(buf, PHYSICAL_NODE_STRING);
240 }
241 
242 int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
243 {
244 	struct acpi_device_physical_node *physical_node, *pn;
245 	struct acpi_device *comp_dev = ACPI_COMPANION(dev);
246 	char physical_node_name[PHYSICAL_NODE_NAME_SIZE];
247 	struct list_head *physnode_list;
248 	unsigned int node_id;
249 	int retval = -EINVAL;
250 
251 	if (!acpi_dev) {
252 		if (!comp_dev)
253 			return -EINVAL;
254 
255 		/* If the companion has been set upfront, pick it up. */
256 		acpi_dev = comp_dev;
257 	} else if (comp_dev && acpi_dev != comp_dev) {
258 		dev_warn(dev, "ACPI companion already set to %s which is not %s\n",
259 			 acpi_dev_name(comp_dev), acpi_dev_name(acpi_dev));
260 		return -EEXIST;
261 	}
262 
263 	guard(mutex)(&acpi_dev->physical_node_lock);
264 
265 	/*
266 	 * Keep the list sorted by node_id so that the IDs of removed nodes can
267 	 * be recycled easily.
268 	 */
269 	physnode_list = &acpi_dev->physical_node_list;
270 	node_id = 0;
271 	list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
272 		/* Sanity check. */
273 		if (pn->dev == dev) {
274 			if (!comp_dev) {
275 				/* Really unexpected. */
276 				ACPI_COMPANION_SET(dev, acpi_dev);
277 				dev_warn(&acpi_dev->dev,
278 					 "Physical device list corruption fixed up\n");
279 			}
280 			return 0;
281 		}
282 		if (pn->node_id == node_id) {
283 			physnode_list = &pn->node;
284 			node_id++;
285 		}
286 	}
287 
288 	physical_node = kzalloc_obj(*physical_node);
289 	if (!physical_node)
290 		return -ENOMEM;
291 
292 	acpi_dev_get(acpi_dev);
293 	get_device(dev);
294 
295 	physical_node->node_id = node_id;
296 	physical_node->dev = dev;
297 	list_add(&physical_node->node, physnode_list);
298 	acpi_dev->physical_node_count++;
299 
300 	if (!comp_dev)
301 		ACPI_COMPANION_SET(dev, acpi_dev);
302 
303 	acpi_physnode_link_name(physical_node_name, node_id);
304 	retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
305 				   physical_node_name);
306 	if (retval)
307 		dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
308 			physical_node_name, retval);
309 
310 	retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
311 				   "firmware_node");
312 	if (retval)
313 		dev_err(dev, "Failed to create link firmware_node (%d)\n",
314 			retval);
315 
316 	if (acpi_dev->wakeup.flags.valid)
317 		device_set_wakeup_capable(dev, true);
318 
319 	return 0;
320 }
321 EXPORT_SYMBOL_GPL(acpi_bind_one);
322 
323 void acpi_unbind_one(struct device *dev)
324 {
325 	struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
326 	struct acpi_device_physical_node *entry;
327 
328 	if (!acpi_dev)
329 		return;
330 
331 	mutex_lock(&acpi_dev->physical_node_lock);
332 
333 	list_for_each_entry(entry, &acpi_dev->physical_node_list, node)
334 		if (entry->dev == dev) {
335 			char physnode_name[PHYSICAL_NODE_NAME_SIZE];
336 
337 			list_del(&entry->node);
338 			acpi_dev->physical_node_count--;
339 
340 			acpi_physnode_link_name(physnode_name, entry->node_id);
341 			sysfs_remove_link(&acpi_dev->dev.kobj, physnode_name);
342 			sysfs_remove_link(&dev->kobj, "firmware_node");
343 			ACPI_COMPANION_SET(dev, NULL);
344 
345 			mutex_unlock(&acpi_dev->physical_node_lock);
346 
347 			/* Drop references taken by acpi_bind_one(). */
348 			put_device(dev);
349 			acpi_dev_put(acpi_dev);
350 			kfree(entry);
351 			return;
352 		}
353 
354 	mutex_unlock(&acpi_dev->physical_node_lock);
355 }
356 EXPORT_SYMBOL_GPL(acpi_unbind_one);
357 
358 void acpi_device_notify(struct device *dev)
359 {
360 	struct acpi_device *adev;
361 	int ret;
362 
363 	/* ACPI devices have no ACPI companions. */
364 	if (dev->bus == &acpi_bus_type)
365 		return;
366 
367 	ret = acpi_bind_one(dev, NULL);
368 	if (ret) {
369 		adev = acpi_companion_lookup(dev);
370 		if (!adev)
371 			return;
372 	} else {
373 		adev = ACPI_COMPANION(dev);
374 
375 		if (dev_is_pci(dev)) {
376 			pci_acpi_setup(dev, adev);
377 		} else if (dev_is_platform(dev)) {
378 			acpi_configure_pmsi_domain(dev);
379 
380 			if (adev->handler && adev->handler->bind)
381 				adev->handler->bind(dev);
382 		}
383 	}
384 
385 	dev_dbg(dev, "Bound to ACPI device %s\n", acpi_dev_name(adev));
386 }
387 
388 void acpi_device_notify_remove(struct device *dev)
389 {
390 	struct acpi_device *adev = ACPI_COMPANION(dev);
391 
392 	if (!adev)
393 		return;
394 
395 	if (dev_is_pci(dev))
396 		pci_acpi_cleanup(dev, adev);
397 	else if (adev->handler && adev->handler->unbind)
398 		adev->handler->unbind(dev);
399 
400 	acpi_unbind_one(dev);
401 }
402