From a4ff342afe2d05c6bb8f479b51c7365412763e9c Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Mon, 9 Jun 2003 02:40:02 -0700 Subject: [driver model] Rewrite system device API System devices are special, and after two years of listening to Linus preach this, it finally sunk in enough to do something about. We don't need to regard them as real devices that reside on a peripheral bus and can be dynamically bound to drivers. If we discover, e.g. a CPU, we know by default that we have a driver for it, and we know damn well that we have a CPU. We still need to keep track of all the devices, and all the devices of a particular type. The kobject infrastructure allows us to do this, without the overhead of the regular model. A new subsystem is defined that registers as a child object of devices_subsys, giving us: /sys/devices/system/ struct sysdev_class { struct list_head drivers; /* Default operations for these types of devices */ int (*shutdown)(struct sys_device *); int (*suspend)(struct sys_device *, u32 state); int (*resume)(struct sys_device *); struct kset kset; }; Defines a type of system device. These are registered on startup, by e.g. drivers/base/cpu.c. The methods are default operations for devices of that type that may or may not be used. For things like the i8259 controller, these will be filled in, since it is registered by the same component that the device controls reside in. For things like CPUs, generic code will register the class, but other architecture-specific or otherwise configurable drivers may register auxillary drivers, that look like: struct sysdev_driver { struct list_head entry; int (*add)(struct sys_device *); int (*remove)(struct sys_device *); int (*shutdown)(struct sys_device *); int (*suspend)(struct sys_device *, u32 state); int (*resume)(struct sys_device *); }; Each auxillary driver gets called during each operation on a device of a particular class. Auxillary drivers may register with a NULL class parameter, in which case they will be added to a list of 'global drivers' that get called for each device of each class. Besides providing a decent of cleanup for system device drivers, this also allows: - Special handling of system devices during power transitions. We no longer have to worry about shutting down the PIC before we shut down any devices. We can shut down the system devices after we've shut down every other device. Ditto for suspend/resume cycles. Almost (if not) all PM actions for system devices happen with interrupts off, and require only one call, which makes that easier. But, we can also make sure we take care of these last during suspend and first during resume. - Easy expression of configurable device-specific interfaces. Namely cpufreq and mtrr. We don't have to worry about mispresentation in the driver model (like recent MTRR patches) or using a cumbersome interface ({device,class}_interface) that don't receive all the necessary calls. - Consolidation of userspace representation. No longer do we have /sys/devices/sys, /sys/bus/sys, and /sys/class/cpu, etc. We have only /sys/devices/system: # tree /sys/devices/system/ /sys/devices/system/ |-- cpu | `-- cpu0 |-- i8259 | `-- i82590 |-- lapic | `-- lapic0 |-- rtc | `-- rtc0 `-- timer `-- timer0 Each directory in 'system' is the class, and each directory under that is the instance of each device in that class. --- include/linux/device.h | 68 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 58 insertions(+), 10 deletions(-) (limited to 'include') diff --git a/include/linux/device.h b/include/linux/device.h index 3604d351f3f0..de674eaea31c 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -351,24 +351,72 @@ extern int (*platform_notify_remove)(struct device * dev); extern struct device * get_device(struct device * dev); extern void put_device(struct device * dev); + /* drivers/base/sys.c */ -struct sys_root { - u32 id; - struct device dev; - struct device sysdev; +/** + * System devices follow a slightly different driver model. + * They don't need to do dynammic driver binding, can't be probed, + * and don't reside on any type of peripheral bus. + * So, we represent and treat them a little differently. + * + * We still have a notion of a driver for a system device, because we still + * want to perform basic operations on these devices. + * + * We also support auxillary drivers binding to devices of a certain class. + * + * This allows configurable drivers to register themselves for devices of + * a certain type. And, it allows class definitions to reside in generic + * code while arch-specific code can register specific drivers. + * + * Auxillary drivers registered with a NULL cls are registered as drivers + * for all system devices, and get notification calls for each device. + */ + +struct sys_device; + +struct sysdev_class { + struct list_head drivers; + + /* Default operations for these types of devices */ + int (*shutdown)(struct sys_device *); + int (*suspend)(struct sys_device *, u32 state); + int (*resume)(struct sys_device *); + struct kset kset; +}; + + +extern int sysdev_class_register(struct sysdev_class *); +extern void sysdev_class_unregister(struct sysdev_class *); + + +/** + * Auxillary system device drivers. + */ + +struct sysdev_driver { + struct list_head entry; + int (*add)(struct sys_device *); + int (*remove)(struct sys_device *); + int (*shutdown)(struct sys_device *); + int (*suspend)(struct sys_device *, u32 state); + int (*resume)(struct sys_device *); }; -extern int sys_register_root(struct sys_root *); -extern void sys_unregister_root(struct sys_root *); + +extern int sysdev_driver_register(struct sysdev_class *, struct sysdev_driver *); +extern void sysdev_driver_unregister(struct sysdev_class *, struct sysdev_driver *); +/** + * sys_devices can be simplified a lot from regular devices, because they're + * simply not as versatile. + */ + struct sys_device { - char * name; u32 id; - struct sys_root * root; - struct device dev; - struct class_device class_dev; + struct sysdev_class * cls; + struct kobject kobj; }; extern int sys_device_register(struct sys_device *); -- cgit v1.2.3 From fab9792907faadd0803217fd73801b5f9eb29922 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Mon, 9 Jun 2003 02:40:34 -0700 Subject: [list.h] Add list_for_each_entry_reverse --- include/linux/list.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'include') diff --git a/include/linux/list.h b/include/linux/list.h index a6dea8afd99d..c46a278cce05 100644 --- a/include/linux/list.h +++ b/include/linux/list.h @@ -296,6 +296,20 @@ static inline void list_splice_init(struct list_head *list, pos = list_entry(pos->member.next, typeof(*pos), member), \ prefetch(pos->member.next)) +/** + * list_for_each_entry_reverse - iterate backwards over list of given type. + * @pos: the type * to use as a loop counter. + * @head: the head for your list. + * @member: the name of the list_struct within the struct. + */ +#define list_for_each_entry_reverse(pos, head, member) \ + for (pos = list_entry((head)->prev, typeof(*pos), member), \ + prefetch(pos->member.prev); \ + &pos->member != (head); \ + pos = list_entry(pos->member.prev, typeof(*pos), member), \ + prefetch(pos->member.prev)) + + /** * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry * @pos: the type * to use as a loop counter. -- cgit v1.2.3 From 651d7ed273c8b08bae12928149e4b1dbc693e076 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Mon, 9 Jun 2003 02:41:22 -0700 Subject: [kobject] Add set_kset_name Shorthand macro for initializing only the name of an embedded kset in an object. --- include/linux/kobject.h | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'include') diff --git a/include/linux/kobject.h b/include/linux/kobject.h index c982391cf8d6..5d42248dd95f 100644 --- a/include/linux/kobject.h +++ b/include/linux/kobject.h @@ -118,6 +118,14 @@ static inline struct kobj_type * get_ktype(struct kobject * k) extern struct kobject * kset_find_obj(struct kset *, const char *); +/** + * Use this when initializing an embedded kset with no other + * fields to initialize. + */ +#define set_kset_name(str) .kset = { .kobj = { .name = str } } + + + struct subsystem { struct kset kset; struct rw_semaphore rwsem; -- cgit v1.2.3 From 35e5d23476e00fb2d709b90d8fb4cc4a9caa660c Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Mon, 9 Jun 2003 21:58:22 -0700 Subject: [driver model] Create include/linux/sysdev.h and define sysdev_attribute. Split out all system device definitions from device.h into their own header sysdev.h Define struct sysdev_attribute and define functions to export attributes in sysfs. --- drivers/base/sys.c | 41 ++++++++++++++++++++-- include/linux/sysdev.h | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 include/linux/sysdev.h (limited to 'include') diff --git a/drivers/base/sys.c b/drivers/base/sys.c index aa71e40a865e..6ae79a036f0f 100644 --- a/drivers/base/sys.c +++ b/drivers/base/sys.c @@ -14,7 +14,7 @@ #define DEBUG -#include +#include #include #include #include @@ -25,10 +25,47 @@ extern struct subsystem devices_subsys; +#define to_sysdev(k) container_of(k,struct sys_device,kobj) +#define to_sysdev_attr(a) container_of(a,struct sysdev_attribute,attr) + + +static ssize_t +sysdev_show(struct kobject * kobj, struct attribute * attr, char * buffer) +{ + struct sys_device * sysdev = to_sysdev(kobj); + struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr); + + if (sysdev_attr->show) + return sysdev_attr->show(sysdev,buffer); + return 0; +} + + +static ssize_t +sysdev_store(struct kobject * kobj, struct attribute * attr, + const char * buffer, size_t count) +{ + struct sys_device * sysdev = to_sysdev(kobj); + struct sysdev_attribute * sysdev_attr = to_sysdev_attr(attr); + + if (sysdev_attr->store) + return sysdev_attr->store(sysdev,buffer,count); + return 0; +} + +static struct sysfs_ops sysfs_ops = { + .show = sysdev_show, + .store = sysdev_store, +}; + +static struct kobj_type ktype_sysdev = { + .sysfs_ops = &sysfs_ops, +}; + /* * declare system_subsys */ -decl_subsys(system,NULL,NULL); +decl_subsys(system,&ktype_sysdev,NULL); int sysdev_class_register(struct sysdev_class * cls) { diff --git a/include/linux/sysdev.h b/include/linux/sysdev.h new file mode 100644 index 000000000000..2a90db8d41de --- /dev/null +++ b/include/linux/sysdev.h @@ -0,0 +1,94 @@ +/** + * System devices follow a slightly different driver model. + * They don't need to do dynammic driver binding, can't be probed, + * and don't reside on any type of peripheral bus. + * So, we represent and treat them a little differently. + * + * We still have a notion of a driver for a system device, because we still + * want to perform basic operations on these devices. + * + * We also support auxillary drivers binding to devices of a certain class. + * + * This allows configurable drivers to register themselves for devices of + * a certain type. And, it allows class definitions to reside in generic + * code while arch-specific code can register specific drivers. + * + * Auxillary drivers registered with a NULL cls are registered as drivers + * for all system devices, and get notification calls for each device. + */ + + +#ifndef _SYSDEV_H_ +#define _SYSDEV_H_ + +#include + + +struct sys_device; + +struct sysdev_class { + struct list_head drivers; + + /* Default operations for these types of devices */ + int (*shutdown)(struct sys_device *); + int (*suspend)(struct sys_device *, u32 state); + int (*resume)(struct sys_device *); + struct kset kset; +}; + + +extern int sysdev_class_register(struct sysdev_class *); +extern void sysdev_class_unregister(struct sysdev_class *); + + +/** + * Auxillary system device drivers. + */ + +struct sysdev_driver { + struct list_head entry; + int (*add)(struct sys_device *); + int (*remove)(struct sys_device *); + int (*shutdown)(struct sys_device *); + int (*suspend)(struct sys_device *, u32 state); + int (*resume)(struct sys_device *); +}; + + +extern int sysdev_driver_register(struct sysdev_class *, struct sysdev_driver *); +extern void sysdev_driver_unregister(struct sysdev_class *, struct sysdev_driver *); + + +/** + * sys_devices can be simplified a lot from regular devices, because they're + * simply not as versatile. + */ + +struct sys_device { + u32 id; + struct sysdev_class * cls; + struct kobject kobj; +}; + +extern int sys_device_register(struct sys_device *); +extern void sys_device_unregister(struct sys_device *); + + +struct sysdev_attribute { + struct attribute attr; + ssize_t (*show)(struct sys_device *, char *); + ssize_t (*store)(struct sys_device *, const char *, size_t); +}; + + +#define SYSDEV_ATTR(_name,_mode,_show,_store) \ +struct sysdev_attribute attr_##_name = { \ + .attr = {.name = __stringify(_name), .mode = _mode }, \ + .show = _show, \ + .store = _store, \ +}; + +extern int sysdev_create_file(struct sys_device *, struct sysdev_attribute *); +extern void sysdev_remove_file(struct sys_device *, struct sysdev_attribute *); + +#endif /* _SYSDEV_H_ */ -- cgit v1.2.3 From c81d6bf04f5fd7078b7373cc6cd7c7c917fc9fe9 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Mon, 9 Jun 2003 22:00:51 -0700 Subject: [numa nodes] Convert to use new system device API --- drivers/base/node.c | 41 +++++++++++------------------------------ include/linux/node.h | 4 ++-- 2 files changed, 13 insertions(+), 32 deletions(-) (limited to 'include') diff --git a/drivers/base/node.c b/drivers/base/node.c index 48fa6de5580a..8dc7ab848216 100644 --- a/drivers/base/node.c +++ b/drivers/base/node.c @@ -10,15 +10,8 @@ #include - -static struct class node_class = { - .name = "node", -}; - - -static struct device_driver node_driver = { - .name = "node", - .bus = &system_bus_type, +static struct sysdev_class node_class = { + set_kset_name("node"), }; @@ -27,7 +20,7 @@ static ssize_t node_read_cpumap(struct device * dev, char * buf) struct node *node_dev = to_node(to_root(dev)); return sprintf(buf,"%lx\n",node_dev->cpumap); } -static DEVICE_ATTR(cpumap,S_IRUGO,node_read_cpumap,NULL); +static SYSDEV_ATTR(cpumap,S_IRUGO,node_read_cpumap,NULL); #define K(x) ((x) << (PAGE_SHIFT - 10)) static ssize_t node_read_meminfo(struct device * dev, char * buf) @@ -53,7 +46,7 @@ static ssize_t node_read_meminfo(struct device * dev, char * buf) nid, K(i.freeram-i.freehigh)); } #undef K -static DEVICE_ATTR(meminfo,S_IRUGO,node_read_meminfo,NULL); +static SYSDEV_ATTR(meminfo,S_IRUGO,node_read_meminfo,NULL); /* @@ -67,17 +60,13 @@ int __init register_node(struct node *node, int num, struct node *parent) int error; node->cpumap = node_to_cpumask(num); - node->sysroot.id = num; - if (parent) - node->sysroot.dev.parent = &parent->sysroot.sysdev; - snprintf(node->sysroot.dev.name, DEVICE_NAME_SIZE, "Node %u", num); - snprintf(node->sysroot.dev.bus_id, BUS_ID_SIZE, "node%u", num); - node->sysroot.dev.driver = &node_driver; - node->sysroot.dev.bus = &system_bus_type; - error = sys_register_root(&node->sysroot); + node->sysdev.id = num; + node->sysdev.cls = &node_class; + error = sys_device_register(&node->sysdev); + if (!error){ - device_create_file(&node->sysroot.dev, &dev_attr_cpumap); - device_create_file(&node->sysroot.dev, &dev_attr_meminfo); + sys_device_create_file(&node->sysroot.dev, &attr_cpumap); + sys_device_create_file(&node->sysroot.dev, &attr_meminfo); } return error; } @@ -85,14 +74,6 @@ int __init register_node(struct node *node, int num, struct node *parent) int __init register_node_type(void) { - int error; - - error = class_register(&node_class); - if (!error) { - error = driver_register(&node_driver); - if (error) - class_unregister(&node_class); - } - return error; + return sysdev_class_register(&node_class); } postcore_initcall(register_node_type); diff --git a/include/linux/node.h b/include/linux/node.h index 294606bbff5a..90543a94b86e 100644 --- a/include/linux/node.h +++ b/include/linux/node.h @@ -19,11 +19,11 @@ #ifndef _LINUX_NODE_H_ #define _LINUX_NODE_H_ -#include +#include struct node { unsigned long cpumap; /* Bitmap of CPUs on the Node */ - struct sys_root sysroot; + struct sys_device sysdev; }; extern int register_node(struct node *, int, struct node *); -- cgit v1.2.3 From ce8e83b38225d532c843693ebfe52ab3a041b783 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Mon, 9 Jun 2003 22:01:36 -0700 Subject: [driver model] Remove system device definitions from device.h Should have been in earlier changeset. D'oh. --- include/linux/device.h | 72 -------------------------------------------------- 1 file changed, 72 deletions(-) (limited to 'include') diff --git a/include/linux/device.h b/include/linux/device.h index de674eaea31c..1bd92551c077 100644 --- a/include/linux/device.h +++ b/include/linux/device.h @@ -352,78 +352,6 @@ extern struct device * get_device(struct device * dev); extern void put_device(struct device * dev); -/* drivers/base/sys.c */ - -/** - * System devices follow a slightly different driver model. - * They don't need to do dynammic driver binding, can't be probed, - * and don't reside on any type of peripheral bus. - * So, we represent and treat them a little differently. - * - * We still have a notion of a driver for a system device, because we still - * want to perform basic operations on these devices. - * - * We also support auxillary drivers binding to devices of a certain class. - * - * This allows configurable drivers to register themselves for devices of - * a certain type. And, it allows class definitions to reside in generic - * code while arch-specific code can register specific drivers. - * - * Auxillary drivers registered with a NULL cls are registered as drivers - * for all system devices, and get notification calls for each device. - */ - -struct sys_device; - -struct sysdev_class { - struct list_head drivers; - - /* Default operations for these types of devices */ - int (*shutdown)(struct sys_device *); - int (*suspend)(struct sys_device *, u32 state); - int (*resume)(struct sys_device *); - struct kset kset; -}; - - -extern int sysdev_class_register(struct sysdev_class *); -extern void sysdev_class_unregister(struct sysdev_class *); - - -/** - * Auxillary system device drivers. - */ - -struct sysdev_driver { - struct list_head entry; - int (*add)(struct sys_device *); - int (*remove)(struct sys_device *); - int (*shutdown)(struct sys_device *); - int (*suspend)(struct sys_device *, u32 state); - int (*resume)(struct sys_device *); -}; - - -extern int sysdev_driver_register(struct sysdev_class *, struct sysdev_driver *); -extern void sysdev_driver_unregister(struct sysdev_class *, struct sysdev_driver *); - - -/** - * sys_devices can be simplified a lot from regular devices, because they're - * simply not as versatile. - */ - -struct sys_device { - u32 id; - struct sysdev_class * cls; - struct kobject kobj; -}; - -extern int sys_device_register(struct sys_device *); -extern void sys_device_unregister(struct sys_device *); - -extern struct bus_type system_bus_type; - /* drivers/base/platform.c */ struct platform_device { -- cgit v1.2.3 From d507789a02e933ab503a4457abd6f56c579dea05 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Mon, 9 Jun 2003 23:58:37 -0700 Subject: [driver model] Add save() and restore() methods for system device drivers. It turns out that at least some system device drivers need to allocate memory and/or sleep for one reason or another when either saving or restoring state. Instead of adding a 'level' paramter to the suspend() and resume() methods, which I despise and think is a horrible programming interface, two new methods have been added to struct sysdev_driver: int (*save)(struct sys_device *, u32 state); int (*restore)(struct sys_device *); that are called explicitly before and after suspend() and resume() respectively, with interrupts enabled. This gives the drivers the flexibility to allocate memory and sleep, if necessary. --- drivers/base/power.c | 37 ++++++++++++---- drivers/base/sys.c | 117 +++++++++++++++++++++++++++++++++++++++++++------ include/linux/sysdev.h | 4 ++ 3 files changed, 137 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/drivers/base/power.c b/drivers/base/power.c index 081ad32cadfd..7f5a6abc4e00 100644 --- a/drivers/base/power.c +++ b/drivers/base/power.c @@ -30,9 +30,11 @@ extern struct subsystem devices_subsys; * they only get one called once when interrupts are disabled. */ -extern int sys_device_shutdown(void); -extern int sys_device_suspend(u32 state); -extern int sys_device_resume(void); +extern int sysdev_shutdown(void); +extern int sysdev_save(u32 state); +extern int sysdev_suspend(u32 state); +extern int sysdev_resume(void); +extern int sysdev_restore(void); /** * device_suspend - suspend/remove all devices on the device ree @@ -64,8 +66,19 @@ int device_suspend(u32 state, u32 level) } up_write(&devices_subsys.rwsem); - if (level == SUSPEND_POWER_DOWN) - sys_device_suspend(state); + /* + * Make sure system devices are suspended. + */ + switch(level) { + case SUSPEND_SAVE_STATE: + sysdev_save(state); + break; + case SUSPEND_POWER_DOWN: + sysdev_suspend(state); + break; + default: + break; + } return error; } @@ -82,8 +95,16 @@ void device_resume(u32 level) { struct list_head * node; - if (level == RESUME_POWER_ON) - sys_device_resume(); + switch (level) { + case RESUME_POWER_ON: + sysdev_resume(); + break; + case RESUME_RESTORE_STATE: + sysdev_restore(); + break; + default: + break; + } down_write(&devices_subsys.rwsem); list_for_each_prev(node,&devices_subsys.kset.list) { @@ -119,7 +140,7 @@ void device_shutdown(void) } up_write(&devices_subsys.rwsem); - sys_device_shutdown(); + sysdev_shutdown(); } EXPORT_SYMBOL(device_suspend); diff --git a/drivers/base/sys.c b/drivers/base/sys.c index 6ae79a036f0f..3d6d2e1d1147 100644 --- a/drivers/base/sys.c +++ b/drivers/base/sys.c @@ -200,7 +200,7 @@ void sys_device_unregister(struct sys_device * sysdev) /** - * sys_device_shutdown - Shut down all system devices. + * sysdev_shutdown - Shut down all system devices. * * Loop over each class of system devices, and the devices in each * of those classes. For each device, we call the shutdown method for @@ -213,7 +213,7 @@ void sys_device_unregister(struct sys_device * sysdev) * after their parents. */ -void sys_device_shutdown(void) +void sysdev_shutdown(void) { struct sysdev_class * cls; @@ -252,7 +252,53 @@ void sys_device_shutdown(void) /** - * sys_device_suspend - Suspend all system devices. + * sysdev_save - Save system device state + * @state: Power state we're entering. + * + * This is called when the system is going to sleep, but before interrupts + * have been disabled. This allows system device drivers to allocate and + * save device state, including sleeping during the process.. + */ + +int sysdev_save(u32 state) +{ + struct sysdev_class * cls; + + pr_debug("Saving System Device State\n"); + + down_write(&system_subsys.rwsem); + + list_for_each_entry_reverse(cls,&system_subsys.kset.list, + kset.kobj.entry) { + struct sys_device * sysdev; + pr_debug("Saving state for type '%s':\n",cls->kset.kobj.name); + + list_for_each_entry(sysdev,&cls->kset.list,kobj.entry) { + struct sysdev_driver * drv; + + pr_debug(" %s\n",sysdev->kobj.name); + + list_for_each_entry(drv,&global_drivers,entry) { + if (drv->save) + drv->save(sysdev,state); + } + + list_for_each_entry(drv,&cls->drivers,entry) { + if (drv->save) + drv->save(sysdev,state); + } + + if (cls->save) + cls->save(sysdev,state); + } + } + up_write(&system_subsys.rwsem); + return 0; +} + + +/** + * sysdev_suspend - Suspend all system devices. * @state: Power state to enter. * * We perform an almost identical operation as sys_device_shutdown() @@ -263,7 +309,7 @@ void sys_device_shutdown(void) * warning and return an error. */ -int sys_device_suspend(u32 state) +int sysdev_suspend(u32 state) { struct sysdev_class * cls; @@ -308,7 +354,7 @@ int sys_device_suspend(u32 state) /** - * sys_device_resume - Bring system devices back to life. + * sysdev_resume - Bring system devices back to life. * * Similar to sys_device_suspend(), but we iterate the list forwards * to guarantee that parent devices are resumed before their children. @@ -316,7 +362,7 @@ int sys_device_suspend(u32 state) * Note: Interrupts are disabled when called. */ -int sys_device_resume(void) +int sysdev_resume(void) { struct sysdev_class * cls; @@ -334,27 +380,72 @@ int sys_device_resume(void) struct sysdev_driver * drv; pr_debug(" %s\n",sysdev->kobj.name); - /* Call global drivers first. */ - list_for_each_entry(drv,&global_drivers,entry) { + /* First, call the class-specific one */ + if (cls->resume) + cls->resume(sysdev); + + /* Call auxillary drivers next. */ + list_for_each_entry(drv,&cls->drivers,entry) { if (drv->resume) drv->resume(sysdev); } - /* Call auxillary drivers next. */ - list_for_each_entry(drv,&cls->drivers,entry) { + /* Call global drivers. */ + list_for_each_entry(drv,&global_drivers,entry) { if (drv->resume) drv->resume(sysdev); } - /* Now call the generic one */ - if (cls->resume) - cls->resume(sysdev); } } up_write(&system_subsys.rwsem); return 0; } + +/** + * sysdev_restore - Restore system device state + * + * This is called during a suspend/resume cycle last, after interrupts + * have been re-enabled. This is intended for auxillary drivers, etc, + * that may sleep when restoring state. + */ + +int sysdev_restore(void) +{ + struct sysdev_class * cls; + + down_write(&system_subsys.rwsem); + pr_debug("Restoring System Device State\n"); + + list_for_each_entry(cls,&system_subsys.kset.list,kset.kobj.entry) { + struct sys_device * sysdev; + + pr_debug("Restoring state for type '%s':\n",cls->kset.kobj.name); + list_for_each_entry(sysdev,&cls->kset.list,kobj.entry) { + struct sysdev_driver * drv; + pr_debug(" %s\n",sysdev->kobj.name); + + if (cls->restore) + cls->restore(sysdev); + + list_for_each_entry(drv,&cls->drivers,entry) { + if (drv->restore) + drv->restore(sysdev); + } + + list_for_each_entry(drv,&global_drivers,entry) { + if (drv->restore) + drv->restore(sysdev); + } + } + } + + up_write(&system_subsys.rwsem); + return 0; +} + + int __init sys_bus_init(void) { system_subsys.kset.kobj.parent = &devices_subsys.kset.kobj; diff --git a/include/linux/sysdev.h b/include/linux/sysdev.h index 2a90db8d41de..4bc3e22b5104 100644 --- a/include/linux/sysdev.h +++ b/include/linux/sysdev.h @@ -31,8 +31,10 @@ struct sysdev_class { /* Default operations for these types of devices */ int (*shutdown)(struct sys_device *); + int (*save)(struct sys_device *, u32 state); int (*suspend)(struct sys_device *, u32 state); int (*resume)(struct sys_device *); + int (*restore)(struct sys_device *); struct kset kset; }; @@ -50,8 +52,10 @@ struct sysdev_driver { int (*add)(struct sys_device *); int (*remove)(struct sys_device *); int (*shutdown)(struct sys_device *); + int (*save)(struct sys_device *, u32 state); int (*suspend)(struct sys_device *, u32 state); int (*resume)(struct sys_device *); + int (*restore)(struct sys_device *); }; -- cgit v1.2.3 From 42ed4a84e267845d0af8eb39a8173f0ded151dc4 Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Tue, 10 Jun 2003 00:27:26 -0700 Subject: [cpu] Use sysdev.h instead of device.h and export cpu_sysdev_class --- include/linux/cpu.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/linux/cpu.h b/include/linux/cpu.h index e9e7ebfd63d4..0975def4a2ba 100644 --- a/include/linux/cpu.h +++ b/include/linux/cpu.h @@ -19,7 +19,7 @@ #ifndef _LINUX_CPU_H_ #define _LINUX_CPU_H_ -#include +#include #include #include @@ -29,7 +29,7 @@ struct cpu { }; extern int register_cpu(struct cpu *, int, struct node *); -extern struct class cpu_class; +extern struct sysdev_class cpu_sysdev_class; /* Stop CPUs going up and down. */ extern struct semaphore cpucontrol; -- cgit v1.2.3 From e15eba7e68cf7d40626f0d311dfdc94f57385d1d Mon Sep 17 00:00:00 2001 From: Patrick Mochel Date: Tue, 10 Jun 2003 00:32:08 -0700 Subject: [cpufreq] Convert to use new system device API - Remove explicit call from arm PM sequence, as its handled implicitly by sysdev_restore() in driver model core. --- arch/arm/kernel/pm.c | 8 ----- include/linux/cpufreq.h | 4 --- kernel/cpufreq.c | 83 +++++++++++++++++++------------------------------ 3 files changed, 32 insertions(+), 63 deletions(-) (limited to 'include') diff --git a/arch/arm/kernel/pm.c b/arch/arm/kernel/pm.c index 03fcb771b579..e80142d9304b 100644 --- a/arch/arm/kernel/pm.c +++ b/arch/arm/kernel/pm.c @@ -11,7 +11,6 @@ #include #include #include -#include #include #include @@ -66,13 +65,6 @@ int suspend(void) */ device_resume(RESUME_POWER_ON); - /* - * Restore the CPU frequency settings. - */ -#ifdef CONFIG_CPU_FREQ - cpufreq_restore(); -#endif - /* * Resume LDM devices. */ diff --git a/include/linux/cpufreq.h b/include/linux/cpufreq.h index 3dc9062bd414..1bdb797bf9bd 100644 --- a/include/linux/cpufreq.h +++ b/include/linux/cpufreq.h @@ -205,10 +205,6 @@ struct freq_attr { int cpufreq_set_policy(struct cpufreq_policy *policy); int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu); -#ifdef CONFIG_PM -int cpufreq_restore(void); -#endif - /* the proc_intf.c needs this */ int cpufreq_parse_governor (char *str_governor, unsigned int *policy, struct cpufreq_governor **governor); diff --git a/kernel/cpufreq.c b/kernel/cpufreq.c index 8446f4d97a85..e1ddcb3461c2 100644 --- a/kernel/cpufreq.c +++ b/kernel/cpufreq.c @@ -52,8 +52,6 @@ static DECLARE_RWSEM (cpufreq_notifier_rwsem); static LIST_HEAD(cpufreq_governor_list); static DECLARE_MUTEX (cpufreq_governor_sem); -static struct class_interface cpufreq_interface; - static int cpufreq_cpu_get(unsigned int cpu) { if (cpu >= NR_CPUS) return 0; @@ -110,24 +108,8 @@ int cpufreq_parse_governor (char *str_governor, unsigned int *policy, struct cpu EXPORT_SYMBOL_GPL(cpufreq_parse_governor); -/* forward declarations */ -static int cpufreq_add_dev (struct class_device * dev); -static void cpufreq_remove_dev (struct class_device * dev); - /* drivers/base/cpu.c */ -extern struct device_class cpu_devclass; - -static struct class_interface cpufreq_interface = { - .class = &cpu_class, - .add = &cpufreq_add_dev, - .remove = &cpufreq_remove_dev, -}; - -static inline int to_cpu_nr (struct class_device *dev) -{ - struct sys_device * cpu_sys_dev = container_of(dev->dev, struct sys_device, dev); - return (cpu_sys_dev->id); -} +extern struct sysdev_class cpu_sysdev_class; /** @@ -327,9 +309,9 @@ static struct kobj_type ktype_cpufreq = { * * Adds the cpufreq interface for a CPU device. */ -static int cpufreq_add_dev (struct class_device * class_dev) +static int cpufreq_add_dev (struct sys_device * sys_dev) { - unsigned int cpu = to_cpu_nr(class_dev); + unsigned int cpu = sys_dev->id; int ret = 0; struct cpufreq_policy new_policy; struct cpufreq_policy *policy; @@ -354,14 +336,12 @@ static int cpufreq_add_dev (struct class_device * class_dev) memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); - class_set_devdata(class_dev, policy); up(&cpufreq_driver_sem); init_MUTEX(&policy->lock); /* prepare interface data */ - policy->kobj.parent = &class_dev->kobj; + policy->kobj.parent = &sys_dev->kobj; policy->kobj.ktype = &ktype_cpufreq; -// policy->dev = dev->dev; strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN); ret = kobject_register(&policy->kobj); @@ -392,12 +372,12 @@ static int cpufreq_add_dev (struct class_device * class_dev) * * Removes the cpufreq interface for a CPU device. */ -static void cpufreq_remove_dev (struct class_device * class_dev) +static int cpufreq_remove_dev (struct sys_device * sys_dev) { - unsigned int cpu = to_cpu_nr(class_dev); + unsigned int cpu = sys_dev->id; if (!kobject_get(&cpufreq_driver->policy[cpu].kobj)) - return; + return -EFAULT; down(&cpufreq_driver_sem); if ((cpufreq_driver->target) && @@ -417,9 +397,17 @@ static void cpufreq_remove_dev (struct class_device * class_dev) up(&cpufreq_driver_sem); kobject_put(&cpufreq_driver->policy[cpu].kobj); - return; + return 0; } +static int cpufreq_restore(struct sys_device *); + +static struct sysdev_driver cpufreq_sysdev_driver = { + .add = cpufreq_add_dev, + .remove = cpufreq_remove_dev, + .restore = cpufreq_restore, +}; + /********************************************************************* * NOTIFIER LISTS INTERFACE * @@ -839,7 +827,7 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data) memset(cpufreq_driver->policy, 0, NR_CPUS * sizeof(struct cpufreq_policy)); - return class_interface_register(&cpufreq_interface); + return sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver); } EXPORT_SYMBOL_GPL(cpufreq_register_driver); @@ -857,7 +845,7 @@ int cpufreq_unregister_driver(struct cpufreq_driver *driver) if (!cpufreq_driver || (driver != cpufreq_driver)) return -EINVAL; - class_interface_unregister(&cpufreq_interface); + sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); down(&cpufreq_driver_sem); kfree(cpufreq_driver->policy); @@ -870,41 +858,34 @@ EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); #ifdef CONFIG_PM + /** * cpufreq_restore - restore the CPU clock frequency after resume * * Restore the CPU clock frequency so that our idea of the current * frequency reflects the actual hardware. */ -int cpufreq_restore(void) +static int cpufreq_restore(struct sys_device * sysdev) { - struct cpufreq_policy policy; - unsigned int i; + int cpu = sysdev->id; unsigned int ret = 0; + struct cpufreq_policy policy; - if (in_interrupt()) - panic("cpufreq_restore() called from interrupt context!"); - - if (!try_module_get(cpufreq_driver->owner)) - goto error_out; - - for (i=0;ipolicy[i], sizeof(struct cpufreq_policy)); + memcpy(&policy, &cpufreq_driver->policy[cpu], + sizeof(struct cpufreq_policy)); up(&cpufreq_driver_sem); - ret += cpufreq_set_policy(&policy); - - cpufreq_cpu_put(i); + ret = cpufreq_set_policy(&policy); + cpufreq_cpu_put(cpu); } - module_put(cpufreq_driver->owner); - error_out: return ret; } -EXPORT_SYMBOL_GPL(cpufreq_restore); + #else -#define cpufreq_restore() do {} while (0) +static int cpufreq_restore(struct sys_device * sysdev) +{ + return 0; +} #endif /* CONFIG_PM */ -- cgit v1.2.3