xref: /openbmc/linux/drivers/acpi/glue.c (revision 64cf26f0)
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_bus_type *acpi_get_bus_type(struct device *dev)
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) {
68 		if (tmp->match(dev)) {
69 			ret = tmp;
70 			break;
71 		}
72 	}
73 	up_read(&bus_type_sem);
74 	return ret;
75 }
76 
77 #define FIND_CHILD_MIN_SCORE	1
78 #define FIND_CHILD_MAX_SCORE	2
79 
80 static int find_child_checks(struct acpi_device *adev, bool check_children)
81 {
82 	bool sta_present = true;
83 	unsigned long long sta;
84 	acpi_status status;
85 
86 	status = acpi_evaluate_integer(adev->handle, "_STA", NULL, &sta);
87 	if (status == AE_NOT_FOUND)
88 		sta_present = false;
89 	else if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_ENABLED))
90 		return -ENODEV;
91 
92 	if (check_children && list_empty(&adev->children))
93 		return -ENODEV;
94 
95 	/*
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.]
101 	 */
102 	return sta_present && !adev->pnp.type.platform_id ?
103 			FIND_CHILD_MAX_SCORE : FIND_CHILD_MIN_SCORE;
104 }
105 
106 struct acpi_device *acpi_find_child_device(struct acpi_device *parent,
107 					   u64 address, bool check_children)
108 {
109 	struct acpi_device *adev, *ret = NULL;
110 	int ret_score = 0;
111 
112 	if (!parent)
113 		return NULL;
114 
115 	list_for_each_entry(adev, &parent->children, node) {
116 		acpi_bus_address addr = acpi_device_adr(adev);
117 		int score;
118 
119 		if (!adev->pnp.type.bus_address || addr != address)
120 			continue;
121 
122 		if (!ret) {
123 			/* This is the first matching object.  Save it. */
124 			ret = adev;
125 			continue;
126 		}
127 		/*
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.
132 		 *
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
135 		 * we've just found.
136 		 */
137 		if (!ret_score) {
138 			ret_score = find_child_checks(ret, check_children);
139 			if (ret_score == FIND_CHILD_MAX_SCORE)
140 				return ret;
141 		}
142 		score = find_child_checks(adev, check_children);
143 		if (score == FIND_CHILD_MAX_SCORE) {
144 			return adev;
145 		} else if (score > ret_score) {
146 			ret = adev;
147 			ret_score = score;
148 		}
149 	}
150 	return ret;
151 }
152 EXPORT_SYMBOL_GPL(acpi_find_child_device);
153 
154 static void acpi_physnode_link_name(char *buf, unsigned int node_id)
155 {
156 	if (node_id > 0)
157 		snprintf(buf, PHYSICAL_NODE_NAME_SIZE,
158 			 PHYSICAL_NODE_STRING "%u", node_id);
159 	else
160 		strcpy(buf, PHYSICAL_NODE_STRING);
161 }
162 
163 int acpi_bind_one(struct device *dev, struct acpi_device *acpi_dev)
164 {
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;
170 
171 	if (has_acpi_companion(dev)) {
172 		if (acpi_dev) {
173 			dev_warn(dev, "ACPI companion already set\n");
174 			return -EINVAL;
175 		} else {
176 			acpi_dev = ACPI_COMPANION(dev);
177 		}
178 	}
179 	if (!acpi_dev)
180 		return -EINVAL;
181 
182 	acpi_dev_get(acpi_dev);
183 	get_device(dev);
184 	physical_node = kzalloc(sizeof(*physical_node), GFP_KERNEL);
185 	if (!physical_node) {
186 		retval = -ENOMEM;
187 		goto err;
188 	}
189 
190 	mutex_lock(&acpi_dev->physical_node_lock);
191 
192 	/*
193 	 * Keep the list sorted by node_id so that the IDs of removed nodes can
194 	 * be recycled easily.
195 	 */
196 	physnode_list = &acpi_dev->physical_node_list;
197 	node_id = 0;
198 	list_for_each_entry(pn, &acpi_dev->physical_node_list, node) {
199 		/* Sanity check. */
200 		if (pn->dev == dev) {
201 			mutex_unlock(&acpi_dev->physical_node_lock);
202 
203 			dev_warn(dev, "Already associated with ACPI node\n");
204 			kfree(physical_node);
205 			if (ACPI_COMPANION(dev) != acpi_dev)
206 				goto err;
207 
208 			put_device(dev);
209 			acpi_dev_put(acpi_dev);
210 			return 0;
211 		}
212 		if (pn->node_id == node_id) {
213 			physnode_list = &pn->node;
214 			node_id++;
215 		}
216 	}
217 
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++;
222 
223 	if (!has_acpi_companion(dev))
224 		ACPI_COMPANION_SET(dev, acpi_dev);
225 
226 	acpi_physnode_link_name(physical_node_name, node_id);
227 	retval = sysfs_create_link(&acpi_dev->dev.kobj, &dev->kobj,
228 				   physical_node_name);
229 	if (retval)
230 		dev_err(&acpi_dev->dev, "Failed to create link %s (%d)\n",
231 			physical_node_name, retval);
232 
233 	retval = sysfs_create_link(&dev->kobj, &acpi_dev->dev.kobj,
234 				   "firmware_node");
235 	if (retval)
236 		dev_err(dev, "Failed to create link firmware_node (%d)\n",
237 			retval);
238 
239 	mutex_unlock(&acpi_dev->physical_node_lock);
240 
241 	if (acpi_dev->wakeup.flags.valid)
242 		device_set_wakeup_capable(dev, true);
243 
244 	return 0;
245 
246  err:
247 	ACPI_COMPANION_SET(dev, NULL);
248 	put_device(dev);
249 	acpi_dev_put(acpi_dev);
250 	return retval;
251 }
252 EXPORT_SYMBOL_GPL(acpi_bind_one);
253 
254 int acpi_unbind_one(struct device *dev)
255 {
256 	struct acpi_device *acpi_dev = ACPI_COMPANION(dev);
257 	struct acpi_device_physical_node *entry;
258 
259 	if (!acpi_dev)
260 		return 0;
261 
262 	mutex_lock(&acpi_dev->physical_node_lock);
263 
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];
267 
268 			list_del(&entry->node);
269 			acpi_dev->physical_node_count--;
270 
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(). */
276 			put_device(dev);
277 			acpi_dev_put(acpi_dev);
278 			kfree(entry);
279 			break;
280 		}
281 
282 	mutex_unlock(&acpi_dev->physical_node_lock);
283 	return 0;
284 }
285 EXPORT_SYMBOL_GPL(acpi_unbind_one);
286 
287 void acpi_device_notify(struct device *dev)
288 {
289 	struct acpi_device *adev;
290 	int ret;
291 
292 	ret = acpi_bind_one(dev, NULL);
293 	if (ret) {
294 		struct acpi_bus_type *type = acpi_get_bus_type(dev);
295 
296 		if (!type)
297 			goto err;
298 
299 		adev = type->find_companion(dev);
300 		if (!adev) {
301 			dev_dbg(dev, "ACPI companion not found\n");
302 			goto err;
303 		}
304 		ret = acpi_bind_one(dev, adev);
305 		if (ret)
306 			goto err;
307 
308 		if (type->setup) {
309 			type->setup(dev);
310 			goto done;
311 		}
312 	} else {
313 		adev = ACPI_COMPANION(dev);
314 
315 		if (dev_is_pci(dev)) {
316 			pci_acpi_setup(dev, adev);
317 			goto done;
318 		} else if (dev_is_platform(dev)) {
319 			acpi_configure_pmsi_domain(dev);
320 		}
321 	}
322 
323 	if (adev->handler && adev->handler->bind)
324 		adev->handler->bind(dev);
325 
326 done:
327 	acpi_handle_debug(ACPI_HANDLE(dev), "Bound to device %s\n",
328 			  dev_name(dev));
329 
330 	return;
331 
332 err:
333 	dev_dbg(dev, "No ACPI support\n");
334 }
335 
336 void acpi_device_notify_remove(struct device *dev)
337 {
338 	struct acpi_device *adev = ACPI_COMPANION(dev);
339 
340 	if (!adev)
341 		return;
342 
343 	if (dev_is_pci(dev))
344 		pci_acpi_cleanup(dev, adev);
345 	else if (adev->handler && adev->handler->unbind)
346 		adev->handler->unbind(dev);
347 
348 	acpi_unbind_one(dev);
349 }
350 
351 int acpi_dev_turn_off_if_unused(struct device *dev, void *not_used)
352 {
353 	struct acpi_device *adev = to_acpi_device(dev);
354 
355 	/*
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.
358 	 */
359 	if (adev->pnp.type.hardware_id)
360 		return 0;
361 
362 	mutex_lock(&adev->physical_node_lock);
363 
364 	/*
365 	 * Device objects without device IDs are not in use if they have no
366 	 * corresponding physical device objects.
367 	 */
368 	if (list_empty(&adev->physical_node_list))
369 		acpi_device_set_power(adev, ACPI_STATE_D3_COLD);
370 
371 	mutex_unlock(&adev->physical_node_lock);
372 
373 	return 0;
374 }
375