ia64/linux-2.6.18-xen.hg

view drivers/acpi/scan.c @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jun 05 14:01:20 2009 +0100 (2009-06-05)
parents fad85221407b
children
line source
1 /*
2 * scan.c - support for transforming the ACPI namespace into individual objects
3 */
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/kernel.h>
8 #include <linux/acpi.h>
10 #include <acpi/acpi_drivers.h>
11 #include <acpi/acinterp.h> /* for acpi_ex_eisa_id_to_string() */
13 #define _COMPONENT ACPI_BUS_COMPONENT
14 ACPI_MODULE_NAME("scan")
15 #define STRUCT_TO_INT(s) (*((int*)&s))
16 extern struct acpi_device *acpi_root;
18 #define ACPI_BUS_CLASS "system_bus"
19 #define ACPI_BUS_HID "ACPI_BUS"
20 #define ACPI_BUS_DRIVER_NAME "ACPI Bus Driver"
21 #define ACPI_BUS_DEVICE_NAME "System Bus"
23 static LIST_HEAD(acpi_device_list);
24 DEFINE_SPINLOCK(acpi_device_lock);
25 LIST_HEAD(acpi_wakeup_device_list);
28 static void acpi_device_release(struct kobject *kobj)
29 {
30 struct acpi_device *dev = container_of(kobj, struct acpi_device, kobj);
31 kfree(dev->pnp.cid_list);
32 kfree(dev);
33 }
35 typedef void acpi_device_sysfs_files(struct kobject *,
36 const struct attribute *);
38 static void setup_sys_fs_device_files(struct acpi_device *dev,
39 acpi_device_sysfs_files * func);
41 #define create_sysfs_device_files(dev) \
42 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_create_file)
43 #define remove_sysfs_device_files(dev) \
44 setup_sys_fs_device_files(dev, (acpi_device_sysfs_files *)&sysfs_remove_file)
46 #define to_acpi_device(n) container_of(n, struct acpi_device, kobj)
47 #define to_handle_attr(n) container_of(n, struct acpi_device_attribute, attr);
49 static ssize_t acpi_device_attr_show(struct kobject *kobj,
50 struct attribute *attr, char *buf)
51 {
52 struct acpi_device *device = to_acpi_device(kobj);
53 struct acpi_device_attribute *attribute = to_handle_attr(attr);
54 return attribute->show ? attribute->show(device, buf) : -EIO;
55 }
56 static ssize_t acpi_device_attr_store(struct kobject *kobj,
57 struct attribute *attr, const char *buf,
58 size_t len)
59 {
60 struct acpi_device *device = to_acpi_device(kobj);
61 struct acpi_device_attribute *attribute = to_handle_attr(attr);
62 return attribute->store ? attribute->store(device, buf, len) : -EIO;
63 }
65 static struct sysfs_ops acpi_device_sysfs_ops = {
66 .show = acpi_device_attr_show,
67 .store = acpi_device_attr_store,
68 };
70 static struct kobj_type ktype_acpi_ns = {
71 .sysfs_ops = &acpi_device_sysfs_ops,
72 .release = acpi_device_release,
73 };
75 static int namespace_uevent(struct kset *kset, struct kobject *kobj,
76 char **envp, int num_envp, char *buffer,
77 int buffer_size)
78 {
79 struct acpi_device *dev = to_acpi_device(kobj);
80 int i = 0;
81 int len = 0;
83 if (!dev->driver)
84 return 0;
86 if (add_uevent_var(envp, num_envp, &i, buffer, buffer_size, &len,
87 "PHYSDEVDRIVER=%s", dev->driver->name))
88 return -ENOMEM;
90 envp[i] = NULL;
92 return 0;
93 }
95 static struct kset_uevent_ops namespace_uevent_ops = {
96 .uevent = &namespace_uevent,
97 };
99 static struct kset acpi_namespace_kset = {
100 .kobj = {
101 .name = "namespace",
102 },
103 .subsys = &acpi_subsys,
104 .ktype = &ktype_acpi_ns,
105 .uevent_ops = &namespace_uevent_ops,
106 };
108 static ssize_t
109 acpi_device_hid_show(struct acpi_device *acpi_dev, char *buf)
110 {
111 return sprintf(buf, "%s\n", acpi_dev->pnp.hardware_id);
112 }
113 ACPI_DEVICE_ATTR(hid, 0444, acpi_device_hid_show, NULL);
115 static ssize_t
116 acpi_device_uid_show(struct acpi_device *acpi_dev, char *buf)
117 {
118 return sprintf(buf, "%s\n", acpi_dev->pnp.unique_id);
119 }
120 ACPI_DEVICE_ATTR(uid, 0444, acpi_device_uid_show, NULL);
122 static int acpi_device_setup_files(struct acpi_device *dev)
123 {
124 int result = 0;
126 if (dev->flags.hardware_id) {
127 result = sysfs_create_file(&dev->kobj,
128 &acpi_device_attr_hid.attr);
129 if (result)
130 goto end;
131 }
133 if (dev->flags.unique_id) {
134 result = sysfs_create_file(&dev->kobj,
135 &acpi_device_attr_uid.attr);
136 if (result)
137 goto end;
138 }
140 end:
141 return result;
142 }
144 static void acpi_device_register(struct acpi_device *device,
145 struct acpi_device *parent)
146 {
147 int err;
149 /*
150 * Linkage
151 * -------
152 * Link this device to its parent and siblings.
153 */
154 INIT_LIST_HEAD(&device->children);
155 INIT_LIST_HEAD(&device->node);
156 INIT_LIST_HEAD(&device->g_list);
157 INIT_LIST_HEAD(&device->wakeup_list);
159 spin_lock(&acpi_device_lock);
160 if (device->parent) {
161 list_add_tail(&device->node, &device->parent->children);
162 list_add_tail(&device->g_list, &device->parent->g_list);
163 } else
164 list_add_tail(&device->g_list, &acpi_device_list);
165 if (device->wakeup.flags.valid)
166 list_add_tail(&device->wakeup_list, &acpi_wakeup_device_list);
167 spin_unlock(&acpi_device_lock);
169 strlcpy(device->kobj.name, device->pnp.bus_id, KOBJ_NAME_LEN);
170 if (parent)
171 device->kobj.parent = &parent->kobj;
172 device->kobj.ktype = &ktype_acpi_ns;
173 device->kobj.kset = &acpi_namespace_kset;
174 err = kobject_register(&device->kobj);
175 if (err < 0)
176 printk(KERN_WARNING "%s: kobject_register error: %d\n",
177 __FUNCTION__, err);
178 create_sysfs_device_files(device);
179 acpi_device_setup_files(device);
180 }
182 static void acpi_device_unregister(struct acpi_device *device, int type)
183 {
184 spin_lock(&acpi_device_lock);
185 if (device->parent) {
186 list_del(&device->node);
187 list_del(&device->g_list);
188 } else
189 list_del(&device->g_list);
191 list_del(&device->wakeup_list);
193 spin_unlock(&acpi_device_lock);
195 acpi_detach_data(device->handle, acpi_bus_data_handler);
196 remove_sysfs_device_files(device);
197 kobject_unregister(&device->kobj);
198 }
200 void acpi_bus_data_handler(acpi_handle handle, u32 function, void *context)
201 {
203 /* TBD */
205 return;
206 }
208 static int acpi_bus_get_power_flags(struct acpi_device *device)
209 {
210 acpi_status status = 0;
211 acpi_handle handle = NULL;
212 u32 i = 0;
215 /*
216 * Power Management Flags
217 */
218 status = acpi_get_handle(device->handle, "_PSC", &handle);
219 if (ACPI_SUCCESS(status))
220 device->power.flags.explicit_get = 1;
221 status = acpi_get_handle(device->handle, "_IRC", &handle);
222 if (ACPI_SUCCESS(status))
223 device->power.flags.inrush_current = 1;
225 /*
226 * Enumerate supported power management states
227 */
228 for (i = ACPI_STATE_D0; i <= ACPI_STATE_D3; i++) {
229 struct acpi_device_power_state *ps = &device->power.states[i];
230 char object_name[5] = { '_', 'P', 'R', '0' + i, '\0' };
232 /* Evaluate "_PRx" to se if power resources are referenced */
233 acpi_evaluate_reference(device->handle, object_name, NULL,
234 &ps->resources);
235 if (ps->resources.count) {
236 device->power.flags.power_resources = 1;
237 ps->flags.valid = 1;
238 }
240 /* Evaluate "_PSx" to see if we can do explicit sets */
241 object_name[2] = 'S';
242 status = acpi_get_handle(device->handle, object_name, &handle);
243 if (ACPI_SUCCESS(status)) {
244 ps->flags.explicit_set = 1;
245 ps->flags.valid = 1;
246 }
248 /* State is valid if we have some power control */
249 if (ps->resources.count || ps->flags.explicit_set)
250 ps->flags.valid = 1;
252 ps->power = -1; /* Unknown - driver assigned */
253 ps->latency = -1; /* Unknown - driver assigned */
254 }
256 /* Set defaults for D0 and D3 states (always valid) */
257 device->power.states[ACPI_STATE_D0].flags.valid = 1;
258 device->power.states[ACPI_STATE_D0].power = 100;
259 device->power.states[ACPI_STATE_D3].flags.valid = 1;
260 device->power.states[ACPI_STATE_D3].power = 0;
262 /* TBD: System wake support and resource requirements. */
264 device->power.state = ACPI_STATE_UNKNOWN;
266 return 0;
267 }
269 int acpi_match_ids(struct acpi_device *device, char *ids)
270 {
271 if (device->flags.hardware_id)
272 if (strstr(ids, device->pnp.hardware_id))
273 return 0;
275 if (device->flags.compatible_ids) {
276 struct acpi_compatible_id_list *cid_list = device->pnp.cid_list;
277 int i;
279 /* compare multiple _CID entries against driver ids */
280 for (i = 0; i < cid_list->count; i++) {
281 if (strstr(ids, cid_list->id[i].value))
282 return 0;
283 }
284 }
285 return -ENOENT;
286 }
288 static acpi_status
289 acpi_bus_extract_wakeup_device_power_package(struct acpi_device *device,
290 union acpi_object *package)
291 {
292 int i = 0;
293 union acpi_object *element = NULL;
295 if (!device || !package || (package->package.count < 2))
296 return AE_BAD_PARAMETER;
298 element = &(package->package.elements[0]);
299 if (!element)
300 return AE_BAD_PARAMETER;
301 if (element->type == ACPI_TYPE_PACKAGE) {
302 if ((element->package.count < 2) ||
303 (element->package.elements[0].type !=
304 ACPI_TYPE_LOCAL_REFERENCE)
305 || (element->package.elements[1].type != ACPI_TYPE_INTEGER))
306 return AE_BAD_DATA;
307 device->wakeup.gpe_device =
308 element->package.elements[0].reference.handle;
309 device->wakeup.gpe_number =
310 (u32) element->package.elements[1].integer.value;
311 } else if (element->type == ACPI_TYPE_INTEGER) {
312 device->wakeup.gpe_number = element->integer.value;
313 } else
314 return AE_BAD_DATA;
316 element = &(package->package.elements[1]);
317 if (element->type != ACPI_TYPE_INTEGER) {
318 return AE_BAD_DATA;
319 }
320 device->wakeup.sleep_state = element->integer.value;
322 if ((package->package.count - 2) > ACPI_MAX_HANDLES) {
323 return AE_NO_MEMORY;
324 }
325 device->wakeup.resources.count = package->package.count - 2;
326 for (i = 0; i < device->wakeup.resources.count; i++) {
327 element = &(package->package.elements[i + 2]);
328 if (element->type != ACPI_TYPE_ANY) {
329 return AE_BAD_DATA;
330 }
332 device->wakeup.resources.handles[i] = element->reference.handle;
333 }
335 return AE_OK;
336 }
338 static int acpi_bus_get_wakeup_device_flags(struct acpi_device *device)
339 {
340 acpi_status status = 0;
341 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
342 union acpi_object *package = NULL;
345 /* _PRW */
346 status = acpi_evaluate_object(device->handle, "_PRW", NULL, &buffer);
347 if (ACPI_FAILURE(status)) {
348 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PRW"));
349 goto end;
350 }
352 package = (union acpi_object *)buffer.pointer;
353 status = acpi_bus_extract_wakeup_device_power_package(device, package);
354 if (ACPI_FAILURE(status)) {
355 ACPI_EXCEPTION((AE_INFO, status, "Extracting _PRW package"));
356 goto end;
357 }
359 kfree(buffer.pointer);
361 device->wakeup.flags.valid = 1;
362 /* Power button, Lid switch always enable wakeup */
363 if (!acpi_match_ids(device, "PNP0C0D,PNP0C0C,PNP0C0E"))
364 device->wakeup.flags.run_wake = 1;
366 end:
367 if (ACPI_FAILURE(status))
368 device->flags.wake_capable = 0;
369 return 0;
370 }
372 /* --------------------------------------------------------------------------
373 ACPI sysfs device file support
374 -------------------------------------------------------------------------- */
375 static ssize_t acpi_eject_store(struct acpi_device *device,
376 const char *buf, size_t count);
378 ACPI_DEVICE_ATTR(eject, 0200, NULL, acpi_eject_store);
380 /**
381 * setup_sys_fs_device_files - sets up the device files under device namespace
382 * @dev: acpi_device object
383 * @func: function pointer to create or destroy the device file
384 */
385 static void
386 setup_sys_fs_device_files(struct acpi_device *dev,
387 acpi_device_sysfs_files * func)
388 {
389 acpi_status status;
390 acpi_handle temp = NULL;
392 /*
393 * If device has _EJ0, 'eject' file is created that is used to trigger
394 * hot-removal function from userland.
395 */
396 status = acpi_get_handle(dev->handle, "_EJ0", &temp);
397 if (ACPI_SUCCESS(status))
398 (*(func)) (&dev->kobj, &acpi_device_attr_eject.attr);
399 }
401 static int acpi_eject_operation(acpi_handle handle, int lockable)
402 {
403 struct acpi_object_list arg_list;
404 union acpi_object arg;
405 acpi_status status = AE_OK;
407 /*
408 * TBD: evaluate _PS3?
409 */
411 if (lockable) {
412 arg_list.count = 1;
413 arg_list.pointer = &arg;
414 arg.type = ACPI_TYPE_INTEGER;
415 arg.integer.value = 0;
416 acpi_evaluate_object(handle, "_LCK", &arg_list, NULL);
417 }
419 arg_list.count = 1;
420 arg_list.pointer = &arg;
421 arg.type = ACPI_TYPE_INTEGER;
422 arg.integer.value = 1;
424 /*
425 * TBD: _EJD support.
426 */
428 status = acpi_evaluate_object(handle, "_EJ0", &arg_list, NULL);
429 if (ACPI_FAILURE(status)) {
430 return (-ENODEV);
431 }
433 return (0);
434 }
436 static ssize_t
437 acpi_eject_store(struct acpi_device *device, const char *buf, size_t count)
438 {
439 int result;
440 int ret = count;
441 int islockable;
442 acpi_status status;
443 acpi_handle handle;
444 acpi_object_type type = 0;
446 if ((!count) || (buf[0] != '1')) {
447 return -EINVAL;
448 }
449 #ifndef FORCE_EJECT
450 if (device->driver == NULL) {
451 ret = -ENODEV;
452 goto err;
453 }
454 #endif
455 status = acpi_get_type(device->handle, &type);
456 if (ACPI_FAILURE(status) || (!device->flags.ejectable)) {
457 ret = -ENODEV;
458 goto err;
459 }
461 islockable = device->flags.lockable;
462 handle = device->handle;
464 result = acpi_bus_trim(device, 1);
466 if (!result)
467 result = acpi_eject_operation(handle, islockable);
469 if (result) {
470 ret = -EBUSY;
471 }
472 err:
473 return ret;
474 }
476 /* --------------------------------------------------------------------------
477 Performance Management
478 -------------------------------------------------------------------------- */
480 static int acpi_bus_get_perf_flags(struct acpi_device *device)
481 {
482 device->performance.state = ACPI_STATE_UNKNOWN;
483 return 0;
484 }
486 /* --------------------------------------------------------------------------
487 Driver Management
488 -------------------------------------------------------------------------- */
490 static LIST_HEAD(acpi_bus_drivers);
492 /**
493 * acpi_bus_match - match device IDs to driver's supported IDs
494 * @device: the device that we are trying to match to a driver
495 * @driver: driver whose device id table is being checked
496 *
497 * Checks the device's hardware (_HID) or compatible (_CID) ids to see if it
498 * matches the specified driver's criteria.
499 */
500 static int
501 acpi_bus_match(struct acpi_device *device, struct acpi_driver *driver)
502 {
503 if (driver && driver->ops.match)
504 return driver->ops.match(device, driver);
505 return acpi_match_ids(device, driver->ids);
506 }
508 /**
509 * acpi_bus_driver_init - add a device to a driver
510 * @device: the device to add and initialize
511 * @driver: driver for the device
512 *
513 * Used to initialize a device via its device driver. Called whenever a
514 * driver is bound to a device. Invokes the driver's add() and start() ops.
515 */
516 static int
517 acpi_bus_driver_init(struct acpi_device *device, struct acpi_driver *driver)
518 {
519 int result = 0;
522 if (!device || !driver)
523 return -EINVAL;
525 if (!driver->ops.add)
526 return -ENOSYS;
528 result = driver->ops.add(device);
529 if (result) {
530 device->driver = NULL;
531 acpi_driver_data(device) = NULL;
532 return result;
533 }
535 device->driver = driver;
537 /*
538 * TBD - Configuration Management: Assign resources to device based
539 * upon possible configuration and currently allocated resources.
540 */
542 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
543 "Driver successfully bound to device\n"));
544 return 0;
545 }
547 static int acpi_start_single_object(struct acpi_device *device)
548 {
549 int result = 0;
550 struct acpi_driver *driver;
553 if (!(driver = device->driver))
554 return 0;
556 if (driver->ops.start) {
557 result = driver->ops.start(device);
558 if (result && driver->ops.remove)
559 driver->ops.remove(device, ACPI_BUS_REMOVAL_NORMAL);
560 }
562 return result;
563 }
565 static void acpi_driver_attach(struct acpi_driver *drv)
566 {
567 struct list_head *node, *next;
570 spin_lock(&acpi_device_lock);
571 list_for_each_safe(node, next, &acpi_device_list) {
572 struct acpi_device *dev =
573 container_of(node, struct acpi_device, g_list);
575 if (dev->driver || !dev->status.present)
576 continue;
577 spin_unlock(&acpi_device_lock);
579 if (!acpi_bus_match(dev, drv)) {
580 if (!acpi_bus_driver_init(dev, drv)) {
581 acpi_start_single_object(dev);
582 atomic_inc(&drv->references);
583 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
584 "Found driver [%s] for device [%s]\n",
585 drv->name, dev->pnp.bus_id));
586 }
587 }
588 spin_lock(&acpi_device_lock);
589 }
590 spin_unlock(&acpi_device_lock);
591 }
593 static void acpi_driver_detach(struct acpi_driver *drv)
594 {
595 struct list_head *node, *next;
598 spin_lock(&acpi_device_lock);
599 list_for_each_safe(node, next, &acpi_device_list) {
600 struct acpi_device *dev =
601 container_of(node, struct acpi_device, g_list);
603 if (dev->driver == drv) {
604 spin_unlock(&acpi_device_lock);
605 if (drv->ops.remove)
606 drv->ops.remove(dev, ACPI_BUS_REMOVAL_NORMAL);
607 spin_lock(&acpi_device_lock);
608 dev->driver = NULL;
609 dev->driver_data = NULL;
610 atomic_dec(&drv->references);
611 }
612 }
613 spin_unlock(&acpi_device_lock);
614 }
616 /**
617 * acpi_bus_register_driver - register a driver with the ACPI bus
618 * @driver: driver being registered
619 *
620 * Registers a driver with the ACPI bus. Searches the namespace for all
621 * devices that match the driver's criteria and binds. Returns zero for
622 * success or a negative error status for failure.
623 */
624 int acpi_bus_register_driver(struct acpi_driver *driver)
625 {
627 if (acpi_disabled)
628 return -ENODEV;
630 spin_lock(&acpi_device_lock);
631 list_add_tail(&driver->node, &acpi_bus_drivers);
632 spin_unlock(&acpi_device_lock);
633 acpi_driver_attach(driver);
635 return 0;
636 }
638 EXPORT_SYMBOL(acpi_bus_register_driver);
640 /**
641 * acpi_bus_unregister_driver - unregisters a driver with the APIC bus
642 * @driver: driver to unregister
643 *
644 * Unregisters a driver with the ACPI bus. Searches the namespace for all
645 * devices that match the driver's criteria and unbinds.
646 */
647 void acpi_bus_unregister_driver(struct acpi_driver *driver)
648 {
649 acpi_driver_detach(driver);
651 if (!atomic_read(&driver->references)) {
652 spin_lock(&acpi_device_lock);
653 list_del_init(&driver->node);
654 spin_unlock(&acpi_device_lock);
655 }
656 return;
657 }
659 EXPORT_SYMBOL(acpi_bus_unregister_driver);
661 /**
662 * acpi_bus_find_driver - check if there is a driver installed for the device
663 * @device: device that we are trying to find a supporting driver for
664 *
665 * Parses the list of registered drivers looking for a driver applicable for
666 * the specified device.
667 */
668 static int acpi_bus_find_driver(struct acpi_device *device)
669 {
670 int result = 0;
671 struct list_head *node, *next;
674 spin_lock(&acpi_device_lock);
675 list_for_each_safe(node, next, &acpi_bus_drivers) {
676 struct acpi_driver *driver =
677 container_of(node, struct acpi_driver, node);
679 atomic_inc(&driver->references);
680 spin_unlock(&acpi_device_lock);
681 if (!acpi_bus_match(device, driver)) {
682 result = acpi_bus_driver_init(device, driver);
683 if (!result)
684 goto Done;
685 }
686 atomic_dec(&driver->references);
687 spin_lock(&acpi_device_lock);
688 }
689 spin_unlock(&acpi_device_lock);
691 Done:
692 return result;
693 }
695 /* --------------------------------------------------------------------------
696 Device Enumeration
697 -------------------------------------------------------------------------- */
699 acpi_status
700 acpi_bus_get_ejd(acpi_handle handle, acpi_handle *ejd)
701 {
702 acpi_status status;
703 acpi_handle tmp;
704 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
705 union acpi_object *obj;
707 status = acpi_get_handle(handle, "_EJD", &tmp);
708 if (ACPI_FAILURE(status))
709 return status;
711 status = acpi_evaluate_object(handle, "_EJD", NULL, &buffer);
712 if (ACPI_SUCCESS(status)) {
713 obj = buffer.pointer;
714 status = acpi_get_handle(NULL, obj->string.pointer, ejd);
715 kfree(buffer.pointer);
716 }
717 return status;
718 }
719 EXPORT_SYMBOL_GPL(acpi_bus_get_ejd);
722 static int acpi_bus_get_flags(struct acpi_device *device)
723 {
724 acpi_status status = AE_OK;
725 acpi_handle temp = NULL;
728 /* Presence of _STA indicates 'dynamic_status' */
729 status = acpi_get_handle(device->handle, "_STA", &temp);
730 if (ACPI_SUCCESS(status))
731 device->flags.dynamic_status = 1;
733 /* Presence of _CID indicates 'compatible_ids' */
734 status = acpi_get_handle(device->handle, "_CID", &temp);
735 if (ACPI_SUCCESS(status))
736 device->flags.compatible_ids = 1;
738 /* Presence of _RMV indicates 'removable' */
739 status = acpi_get_handle(device->handle, "_RMV", &temp);
740 if (ACPI_SUCCESS(status))
741 device->flags.removable = 1;
743 /* Presence of _EJD|_EJ0 indicates 'ejectable' */
744 status = acpi_get_handle(device->handle, "_EJD", &temp);
745 if (ACPI_SUCCESS(status))
746 device->flags.ejectable = 1;
747 else {
748 status = acpi_get_handle(device->handle, "_EJ0", &temp);
749 if (ACPI_SUCCESS(status))
750 device->flags.ejectable = 1;
751 }
753 /* Presence of _LCK indicates 'lockable' */
754 status = acpi_get_handle(device->handle, "_LCK", &temp);
755 if (ACPI_SUCCESS(status))
756 device->flags.lockable = 1;
758 /* Presence of _PS0|_PR0 indicates 'power manageable' */
759 status = acpi_get_handle(device->handle, "_PS0", &temp);
760 if (ACPI_FAILURE(status))
761 status = acpi_get_handle(device->handle, "_PR0", &temp);
762 if (ACPI_SUCCESS(status))
763 device->flags.power_manageable = 1;
765 /* Presence of _PRW indicates wake capable */
766 status = acpi_get_handle(device->handle, "_PRW", &temp);
767 if (ACPI_SUCCESS(status))
768 device->flags.wake_capable = 1;
770 /* TBD: Peformance management */
772 return 0;
773 }
775 static void acpi_device_get_busid(struct acpi_device *device,
776 acpi_handle handle, int type)
777 {
778 char bus_id[5] = { '?', 0 };
779 struct acpi_buffer buffer = { sizeof(bus_id), bus_id };
780 int i = 0;
782 /*
783 * Bus ID
784 * ------
785 * The device's Bus ID is simply the object name.
786 * TBD: Shouldn't this value be unique (within the ACPI namespace)?
787 */
788 switch (type) {
789 case ACPI_BUS_TYPE_SYSTEM:
790 strcpy(device->pnp.bus_id, "ACPI");
791 break;
792 case ACPI_BUS_TYPE_POWER_BUTTON:
793 strcpy(device->pnp.bus_id, "PWRF");
794 break;
795 case ACPI_BUS_TYPE_SLEEP_BUTTON:
796 strcpy(device->pnp.bus_id, "SLPF");
797 break;
798 default:
799 acpi_get_name(handle, ACPI_SINGLE_NAME, &buffer);
800 /* Clean up trailing underscores (if any) */
801 for (i = 3; i > 1; i--) {
802 if (bus_id[i] == '_')
803 bus_id[i] = '\0';
804 else
805 break;
806 }
807 strcpy(device->pnp.bus_id, bus_id);
808 break;
809 }
810 }
812 static void acpi_device_set_id(struct acpi_device *device,
813 struct acpi_device *parent, acpi_handle handle,
814 int type)
815 {
816 struct acpi_device_info *info;
817 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
818 char *hid = NULL;
819 char *uid = NULL;
820 struct acpi_compatible_id_list *cid_list = NULL;
821 acpi_status status;
823 switch (type) {
824 case ACPI_BUS_TYPE_DEVICE:
825 status = acpi_get_object_info(handle, &buffer);
826 if (ACPI_FAILURE(status)) {
827 printk("%s: Error reading device info\n", __FUNCTION__);
828 return;
829 }
831 info = buffer.pointer;
832 if (info->valid & ACPI_VALID_HID)
833 hid = info->hardware_id.value;
834 if (info->valid & ACPI_VALID_UID)
835 uid = info->unique_id.value;
836 if (info->valid & ACPI_VALID_CID)
837 cid_list = &info->compatibility_id;
838 if (info->valid & ACPI_VALID_ADR) {
839 device->pnp.bus_address = info->address;
840 device->flags.bus_address = 1;
841 }
842 break;
843 case ACPI_BUS_TYPE_POWER:
844 hid = ACPI_POWER_HID;
845 break;
846 case ACPI_BUS_TYPE_PROCESSOR:
847 hid = ACPI_PROCESSOR_HID;
848 break;
849 case ACPI_BUS_TYPE_SYSTEM:
850 hid = ACPI_SYSTEM_HID;
851 break;
852 case ACPI_BUS_TYPE_THERMAL:
853 hid = ACPI_THERMAL_HID;
854 break;
855 case ACPI_BUS_TYPE_POWER_BUTTON:
856 hid = ACPI_BUTTON_HID_POWERF;
857 break;
858 case ACPI_BUS_TYPE_SLEEP_BUTTON:
859 hid = ACPI_BUTTON_HID_SLEEPF;
860 break;
861 }
863 /*
864 * \_SB
865 * ----
866 * Fix for the system root bus device -- the only root-level device.
867 */
868 if (((acpi_handle)parent == ACPI_ROOT_OBJECT) && (type == ACPI_BUS_TYPE_DEVICE)) {
869 hid = ACPI_BUS_HID;
870 strcpy(device->pnp.device_name, ACPI_BUS_DEVICE_NAME);
871 strcpy(device->pnp.device_class, ACPI_BUS_CLASS);
872 }
874 if (hid) {
875 strcpy(device->pnp.hardware_id, hid);
876 device->flags.hardware_id = 1;
877 }
878 if (uid) {
879 strcpy(device->pnp.unique_id, uid);
880 device->flags.unique_id = 1;
881 }
882 if (cid_list) {
883 device->pnp.cid_list = kmalloc(cid_list->size, GFP_KERNEL);
884 if (device->pnp.cid_list)
885 memcpy(device->pnp.cid_list, cid_list, cid_list->size);
886 else
887 printk(KERN_ERR "Memory allocation error\n");
888 }
890 kfree(buffer.pointer);
891 }
893 static int acpi_device_set_context(struct acpi_device *device, int type)
894 {
895 acpi_status status = AE_OK;
896 int result = 0;
897 /*
898 * Context
899 * -------
900 * Attach this 'struct acpi_device' to the ACPI object. This makes
901 * resolutions from handle->device very efficient. Note that we need
902 * to be careful with fixed-feature devices as they all attach to the
903 * root object.
904 */
905 if (type != ACPI_BUS_TYPE_POWER_BUTTON &&
906 type != ACPI_BUS_TYPE_SLEEP_BUTTON) {
907 status = acpi_attach_data(device->handle,
908 acpi_bus_data_handler, device);
910 if (ACPI_FAILURE(status)) {
911 printk("Error attaching device data\n");
912 result = -ENODEV;
913 }
914 }
915 return result;
916 }
918 static void acpi_device_get_debug_info(struct acpi_device *device,
919 acpi_handle handle, int type)
920 {
921 #ifdef CONFIG_ACPI_DEBUG_OUTPUT
922 char *type_string = NULL;
923 char name[80] = { '?', '\0' };
924 struct acpi_buffer buffer = { sizeof(name), name };
926 switch (type) {
927 case ACPI_BUS_TYPE_DEVICE:
928 type_string = "Device";
929 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
930 break;
931 case ACPI_BUS_TYPE_POWER:
932 type_string = "Power Resource";
933 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
934 break;
935 case ACPI_BUS_TYPE_PROCESSOR:
936 type_string = "Processor";
937 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
938 break;
939 case ACPI_BUS_TYPE_SYSTEM:
940 type_string = "System";
941 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
942 break;
943 case ACPI_BUS_TYPE_THERMAL:
944 type_string = "Thermal Zone";
945 acpi_get_name(handle, ACPI_FULL_PATHNAME, &buffer);
946 break;
947 case ACPI_BUS_TYPE_POWER_BUTTON:
948 type_string = "Power Button";
949 sprintf(name, "PWRB");
950 break;
951 case ACPI_BUS_TYPE_SLEEP_BUTTON:
952 type_string = "Sleep Button";
953 sprintf(name, "SLPB");
954 break;
955 }
957 printk(KERN_DEBUG "Found %s %s [%p]\n", type_string, name, handle);
958 #endif /*CONFIG_ACPI_DEBUG_OUTPUT */
959 }
961 static int acpi_bus_remove(struct acpi_device *dev, int rmdevice)
962 {
963 int result = 0;
964 struct acpi_driver *driver;
967 if (!dev)
968 return -EINVAL;
970 driver = dev->driver;
972 if ((driver) && (driver->ops.remove)) {
974 if (driver->ops.stop) {
975 result = driver->ops.stop(dev, ACPI_BUS_REMOVAL_EJECT);
976 if (result)
977 return result;
978 }
980 result = dev->driver->ops.remove(dev, ACPI_BUS_REMOVAL_EJECT);
981 if (result) {
982 return result;
983 }
985 atomic_dec(&dev->driver->references);
986 dev->driver = NULL;
987 acpi_driver_data(dev) = NULL;
988 }
990 if (!rmdevice)
991 return 0;
993 if (dev->flags.bus_address) {
994 if ((dev->parent) && (dev->parent->ops.unbind))
995 dev->parent->ops.unbind(dev);
996 }
998 acpi_device_unregister(dev, ACPI_BUS_REMOVAL_EJECT);
1000 return 0;
1003 static int
1004 acpi_add_single_object(struct acpi_device **child,
1005 struct acpi_device *parent, acpi_handle handle, int type)
1007 int result = 0;
1008 struct acpi_device *device = NULL;
1011 if (!child)
1012 return -EINVAL;
1014 device = kmalloc(sizeof(struct acpi_device), GFP_KERNEL);
1015 if (!device) {
1016 printk(KERN_ERR PREFIX "Memory allocation error\n");
1017 return -ENOMEM;
1019 memset(device, 0, sizeof(struct acpi_device));
1021 device->handle = handle;
1022 device->parent = parent;
1024 acpi_device_get_busid(device, handle, type);
1026 /*
1027 * Flags
1028 * -----
1029 * Get prior to calling acpi_bus_get_status() so we know whether
1030 * or not _STA is present. Note that we only look for object
1031 * handles -- cannot evaluate objects until we know the device is
1032 * present and properly initialized.
1033 */
1034 result = acpi_bus_get_flags(device);
1035 if (result)
1036 goto end;
1038 /*
1039 * Status
1040 * ------
1041 * See if the device is present. We always assume that non-Device
1042 * and non-Processor objects (e.g. thermal zones, power resources,
1043 * etc.) are present, functioning, etc. (at least when parent object
1044 * is present). Note that _STA has a different meaning for some
1045 * objects (e.g. power resources) so we need to be careful how we use
1046 * it.
1047 */
1048 switch (type) {
1049 case ACPI_BUS_TYPE_PROCESSOR:
1050 case ACPI_BUS_TYPE_DEVICE:
1051 result = acpi_bus_get_status(device);
1052 if (ACPI_FAILURE(result) || !device->status.present) {
1053 result = -ENOENT;
1054 goto end;
1056 break;
1057 default:
1058 STRUCT_TO_INT(device->status) = 0x0F;
1059 break;
1062 /*
1063 * Initialize Device
1064 * -----------------
1065 * TBD: Synch with Core's enumeration/initialization process.
1066 */
1068 /*
1069 * Hardware ID, Unique ID, & Bus Address
1070 * -------------------------------------
1071 */
1072 acpi_device_set_id(device, parent, handle, type);
1074 /*
1075 * Power Management
1076 * ----------------
1077 */
1078 if (device->flags.power_manageable) {
1079 result = acpi_bus_get_power_flags(device);
1080 if (result)
1081 goto end;
1084 /*
1085 * Wakeup device management
1086 *-----------------------
1087 */
1088 if (device->flags.wake_capable) {
1089 result = acpi_bus_get_wakeup_device_flags(device);
1090 if (result)
1091 goto end;
1094 /*
1095 * Performance Management
1096 * ----------------------
1097 */
1098 if (device->flags.performance_manageable) {
1099 result = acpi_bus_get_perf_flags(device);
1100 if (result)
1101 goto end;
1104 if ((result = acpi_device_set_context(device, type)))
1105 goto end;
1107 acpi_device_get_debug_info(device, handle, type);
1109 acpi_device_register(device, parent);
1111 /*
1112 * Bind _ADR-Based Devices
1113 * -----------------------
1114 * If there's a a bus address (_ADR) then we utilize the parent's
1115 * 'bind' function (if exists) to bind the ACPI- and natively-
1116 * enumerated device representations.
1117 */
1118 if (device->flags.bus_address) {
1119 if (device->parent && device->parent->ops.bind)
1120 device->parent->ops.bind(device);
1123 /*
1124 * Locate & Attach Driver
1125 * ----------------------
1126 * If there's a hardware id (_HID) or compatible ids (_CID) we check
1127 * to see if there's a driver installed for this kind of device. Note
1128 * that drivers can install before or after a device is enumerated.
1130 * TBD: Assumes LDM provides driver hot-plug capability.
1131 */
1132 acpi_bus_find_driver(device);
1134 end:
1135 if (!result)
1136 *child = device;
1137 else {
1138 kfree(device->pnp.cid_list);
1139 kfree(device);
1142 return result;
1145 static int acpi_bus_scan(struct acpi_device *start, struct acpi_bus_ops *ops)
1147 acpi_status status = AE_OK;
1148 struct acpi_device *parent = NULL;
1149 struct acpi_device *child = NULL;
1150 acpi_handle phandle = NULL;
1151 acpi_handle chandle = NULL;
1152 acpi_object_type type = 0;
1153 u32 level = 1;
1156 if (!start)
1157 return -EINVAL;
1159 parent = start;
1160 phandle = start->handle;
1162 /*
1163 * Parse through the ACPI namespace, identify all 'devices', and
1164 * create a new 'struct acpi_device' for each.
1165 */
1166 while ((level > 0) && parent) {
1168 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1169 chandle, &chandle);
1171 /*
1172 * If this scope is exhausted then move our way back up.
1173 */
1174 if (ACPI_FAILURE(status)) {
1175 level--;
1176 chandle = phandle;
1177 acpi_get_parent(phandle, &phandle);
1178 if (parent->parent)
1179 parent = parent->parent;
1180 continue;
1183 status = acpi_get_type(chandle, &type);
1184 if (ACPI_FAILURE(status))
1185 continue;
1187 /*
1188 * If this is a scope object then parse it (depth-first).
1189 */
1190 if (type == ACPI_TYPE_LOCAL_SCOPE) {
1191 level++;
1192 phandle = chandle;
1193 chandle = NULL;
1194 continue;
1197 /*
1198 * We're only interested in objects that we consider 'devices'.
1199 */
1200 switch (type) {
1201 case ACPI_TYPE_DEVICE:
1202 type = ACPI_BUS_TYPE_DEVICE;
1203 break;
1204 case ACPI_TYPE_PROCESSOR:
1205 type = ACPI_BUS_TYPE_PROCESSOR;
1206 break;
1207 case ACPI_TYPE_THERMAL:
1208 type = ACPI_BUS_TYPE_THERMAL;
1209 break;
1210 case ACPI_TYPE_POWER:
1211 type = ACPI_BUS_TYPE_POWER;
1212 break;
1213 default:
1214 continue;
1217 if (ops->acpi_op_add)
1218 status = acpi_add_single_object(&child, parent,
1219 chandle, type);
1220 else
1221 status = acpi_bus_get_device(chandle, &child);
1223 if (ACPI_FAILURE(status))
1224 continue;
1226 if (ops->acpi_op_start) {
1227 status = acpi_start_single_object(child);
1228 if (ACPI_FAILURE(status))
1229 continue;
1232 /*
1233 * If the device is present, enabled, and functioning then
1234 * parse its scope (depth-first). Note that we need to
1235 * represent absent devices to facilitate PnP notifications
1236 * -- but only the subtree head (not all of its children,
1237 * which will be enumerated when the parent is inserted).
1239 * TBD: Need notifications and other detection mechanisms
1240 * in place before we can fully implement this.
1241 */
1242 if (child->status.present) {
1243 status = acpi_get_next_object(ACPI_TYPE_ANY, chandle,
1244 NULL, NULL);
1245 if (ACPI_SUCCESS(status)) {
1246 level++;
1247 phandle = chandle;
1248 chandle = NULL;
1249 parent = child;
1254 return 0;
1257 int
1258 acpi_bus_add(struct acpi_device **child,
1259 struct acpi_device *parent, acpi_handle handle, int type)
1261 int result;
1262 struct acpi_bus_ops ops;
1265 result = acpi_add_single_object(child, parent, handle, type);
1266 if (!result) {
1267 memset(&ops, 0, sizeof(ops));
1268 ops.acpi_op_add = 1;
1269 result = acpi_bus_scan(*child, &ops);
1271 return result;
1274 EXPORT_SYMBOL(acpi_bus_add);
1276 int acpi_bus_start(struct acpi_device *device)
1278 int result;
1279 struct acpi_bus_ops ops;
1282 if (!device)
1283 return -EINVAL;
1285 result = acpi_start_single_object(device);
1286 if (!result) {
1287 memset(&ops, 0, sizeof(ops));
1288 ops.acpi_op_start = 1;
1289 result = acpi_bus_scan(device, &ops);
1291 return result;
1294 EXPORT_SYMBOL(acpi_bus_start);
1296 int acpi_bus_trim(struct acpi_device *start, int rmdevice)
1298 acpi_status status;
1299 struct acpi_device *parent, *child;
1300 acpi_handle phandle, chandle;
1301 acpi_object_type type;
1302 u32 level = 1;
1303 int err = 0;
1305 parent = start;
1306 phandle = start->handle;
1307 child = chandle = NULL;
1309 while ((level > 0) && parent && (!err)) {
1310 status = acpi_get_next_object(ACPI_TYPE_ANY, phandle,
1311 chandle, &chandle);
1313 /*
1314 * If this scope is exhausted then move our way back up.
1315 */
1316 if (ACPI_FAILURE(status)) {
1317 level--;
1318 chandle = phandle;
1319 acpi_get_parent(phandle, &phandle);
1320 child = parent;
1321 parent = parent->parent;
1323 if (level == 0)
1324 err = acpi_bus_remove(child, rmdevice);
1325 else
1326 err = acpi_bus_remove(child, 1);
1328 continue;
1331 status = acpi_get_type(chandle, &type);
1332 if (ACPI_FAILURE(status)) {
1333 continue;
1335 /*
1336 * If there is a device corresponding to chandle then
1337 * parse it (depth-first).
1338 */
1339 if (acpi_bus_get_device(chandle, &child) == 0) {
1340 level++;
1341 phandle = chandle;
1342 chandle = NULL;
1343 parent = child;
1345 continue;
1347 return err;
1349 EXPORT_SYMBOL_GPL(acpi_bus_trim);
1352 static int acpi_bus_scan_fixed(struct acpi_device *root)
1354 int result = 0;
1355 struct acpi_device *device = NULL;
1358 if (!root)
1359 return -ENODEV;
1361 /*
1362 * Enumerate all fixed-feature devices.
1363 */
1364 if (acpi_fadt.pwr_button == 0) {
1365 result = acpi_add_single_object(&device, acpi_root,
1366 NULL,
1367 ACPI_BUS_TYPE_POWER_BUTTON);
1368 if (!result)
1369 result = acpi_start_single_object(device);
1372 if (acpi_fadt.sleep_button == 0) {
1373 result = acpi_add_single_object(&device, acpi_root,
1374 NULL,
1375 ACPI_BUS_TYPE_SLEEP_BUTTON);
1376 if (!result)
1377 result = acpi_start_single_object(device);
1380 return result;
1384 static inline struct acpi_device * to_acpi_dev(struct device * dev)
1386 return container_of(dev, struct acpi_device, dev);
1390 static int root_suspend(struct acpi_device * acpi_dev, pm_message_t state)
1392 struct acpi_device * dev, * next;
1393 int result;
1395 spin_lock(&acpi_device_lock);
1396 list_for_each_entry_safe_reverse(dev, next, &acpi_device_list, g_list) {
1397 if (dev->driver && dev->driver->ops.suspend) {
1398 spin_unlock(&acpi_device_lock);
1399 result = dev->driver->ops.suspend(dev, 0);
1400 if (result) {
1401 printk(KERN_ERR PREFIX "[%s - %s] Suspend failed: %d\n",
1402 acpi_device_name(dev),
1403 acpi_device_bid(dev), result);
1405 spin_lock(&acpi_device_lock);
1408 spin_unlock(&acpi_device_lock);
1409 return 0;
1413 static int acpi_device_suspend(struct device * dev, pm_message_t state)
1415 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1417 /*
1418 * For now, we should only register 1 generic device -
1419 * the ACPI root device - and from there, we walk the
1420 * tree of ACPI devices to suspend each one using the
1421 * ACPI driver methods.
1422 */
1423 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1424 root_suspend(acpi_dev, state);
1425 return 0;
1430 static int root_resume(struct acpi_device * acpi_dev)
1432 struct acpi_device * dev, * next;
1433 int result;
1435 spin_lock(&acpi_device_lock);
1436 list_for_each_entry_safe(dev, next, &acpi_device_list, g_list) {
1437 if (dev->driver && dev->driver->ops.resume) {
1438 spin_unlock(&acpi_device_lock);
1439 result = dev->driver->ops.resume(dev, 0);
1440 if (result) {
1441 printk(KERN_ERR PREFIX "[%s - %s] resume failed: %d\n",
1442 acpi_device_name(dev),
1443 acpi_device_bid(dev), result);
1445 spin_lock(&acpi_device_lock);
1448 spin_unlock(&acpi_device_lock);
1449 return 0;
1453 static int acpi_device_resume(struct device * dev)
1455 struct acpi_device * acpi_dev = to_acpi_dev(dev);
1457 /*
1458 * For now, we should only register 1 generic device -
1459 * the ACPI root device - and from there, we walk the
1460 * tree of ACPI devices to resume each one using the
1461 * ACPI driver methods.
1462 */
1463 if (acpi_dev->handle == ACPI_ROOT_OBJECT)
1464 root_resume(acpi_dev);
1465 return 0;
1469 static struct bus_type acpi_bus_type = {
1470 .name = "acpi",
1471 .suspend = acpi_device_suspend,
1472 .resume = acpi_device_resume,
1473 };
1477 static int __init acpi_scan_init(void)
1479 int result;
1480 struct acpi_bus_ops ops;
1483 if (acpi_disabled)
1484 return 0;
1486 result = kset_register(&acpi_namespace_kset);
1487 if (result < 0)
1488 printk(KERN_ERR PREFIX "kset_register error: %d\n", result);
1490 result = bus_register(&acpi_bus_type);
1491 if (result) {
1492 /* We don't want to quit even if we failed to add suspend/resume */
1493 printk(KERN_ERR PREFIX "Could not register bus type\n");
1496 /*
1497 * Create the root device in the bus's device tree
1498 */
1499 result = acpi_add_single_object(&acpi_root, NULL, ACPI_ROOT_OBJECT,
1500 ACPI_BUS_TYPE_SYSTEM);
1501 if (result)
1502 goto Done;
1504 result = acpi_start_single_object(acpi_root);
1505 if (result)
1506 goto Done;
1508 acpi_root->dev.bus = &acpi_bus_type;
1509 snprintf(acpi_root->dev.bus_id, BUS_ID_SIZE, "%s", acpi_bus_type.name);
1510 result = device_register(&acpi_root->dev);
1511 if (result) {
1512 /* We don't want to quit even if we failed to add suspend/resume */
1513 printk(KERN_ERR PREFIX "Could not register device\n");
1516 /*
1517 * Enumerate devices in the ACPI namespace.
1518 */
1519 result = acpi_bus_scan_fixed(acpi_root);
1520 if (!result) {
1521 memset(&ops, 0, sizeof(ops));
1522 ops.acpi_op_add = 1;
1523 ops.acpi_op_start = 1;
1524 result = acpi_bus_scan(acpi_root, &ops);
1527 if (result)
1528 acpi_device_unregister(acpi_root, ACPI_BUS_REMOVAL_NORMAL);
1530 Done:
1531 return result;
1534 subsys_initcall(acpi_scan_init);