aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/cpufreq/cpufreq.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/cpufreq/cpufreq.c')
-rw-r--r--drivers/cpufreq/cpufreq.c99
1 files changed, 68 insertions, 31 deletions
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 6e2ec0b18948..fd69086d08d5 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -761,6 +761,10 @@ static struct kobj_type ktype_cpufreq = {
761 * cpufreq_add_dev - add a CPU device 761 * cpufreq_add_dev - add a CPU device
762 * 762 *
763 * Adds the cpufreq interface for a CPU device. 763 * Adds the cpufreq interface for a CPU device.
764 *
765 * The Oracle says: try running cpufreq registration/unregistration concurrently
766 * with with cpu hotplugging and all hell will break loose. Tried to clean this
767 * mess up, but more thorough testing is needed. - Mathieu
764 */ 768 */
765static int cpufreq_add_dev(struct sys_device *sys_dev) 769static int cpufreq_add_dev(struct sys_device *sys_dev)
766{ 770{
@@ -772,9 +776,6 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
772 struct sys_device *cpu_sys_dev; 776 struct sys_device *cpu_sys_dev;
773 unsigned long flags; 777 unsigned long flags;
774 unsigned int j; 778 unsigned int j;
775#ifdef CONFIG_SMP
776 struct cpufreq_policy *managed_policy;
777#endif
778 779
779 if (cpu_is_offline(cpu)) 780 if (cpu_is_offline(cpu))
780 return 0; 781 return 0;
@@ -804,15 +805,12 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
804 goto nomem_out; 805 goto nomem_out;
805 } 806 }
806 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) { 807 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) {
807 kfree(policy);
808 ret = -ENOMEM; 808 ret = -ENOMEM;
809 goto nomem_out; 809 goto err_free_policy;
810 } 810 }
811 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) { 811 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) {
812 free_cpumask_var(policy->cpus);
813 kfree(policy);
814 ret = -ENOMEM; 812 ret = -ENOMEM;
815 goto nomem_out; 813 goto err_free_cpumask;
816 } 814 }
817 815
818 policy->cpu = cpu; 816 policy->cpu = cpu;
@@ -820,7 +818,8 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
820 818
821 /* Initially set CPU itself as the policy_cpu */ 819 /* Initially set CPU itself as the policy_cpu */
822 per_cpu(policy_cpu, cpu) = cpu; 820 per_cpu(policy_cpu, cpu) = cpu;
823 lock_policy_rwsem_write(cpu); 821 ret = (lock_policy_rwsem_write(cpu) < 0);
822 WARN_ON(ret);
824 823
825 init_completion(&policy->kobj_unregister); 824 init_completion(&policy->kobj_unregister);
826 INIT_WORK(&policy->update, handle_update); 825 INIT_WORK(&policy->update, handle_update);
@@ -833,7 +832,7 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
833 ret = cpufreq_driver->init(policy); 832 ret = cpufreq_driver->init(policy);
834 if (ret) { 833 if (ret) {
835 dprintk("initialization failed\n"); 834 dprintk("initialization failed\n");
836 goto err_out; 835 goto err_unlock_policy;
837 } 836 }
838 policy->user_policy.min = policy->min; 837 policy->user_policy.min = policy->min;
839 policy->user_policy.max = policy->max; 838 policy->user_policy.max = policy->max;
@@ -852,21 +851,31 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
852#endif 851#endif
853 852
854 for_each_cpu(j, policy->cpus) { 853 for_each_cpu(j, policy->cpus) {
854 struct cpufreq_policy *managed_policy;
855
855 if (cpu == j) 856 if (cpu == j)
856 continue; 857 continue;
857 858
858 /* Check for existing affected CPUs. 859 /* Check for existing affected CPUs.
859 * They may not be aware of it due to CPU Hotplug. 860 * They may not be aware of it due to CPU Hotplug.
861 * cpufreq_cpu_put is called when the device is removed
862 * in __cpufreq_remove_dev()
860 */ 863 */
861 managed_policy = cpufreq_cpu_get(j); /* FIXME: Where is this released? What about error paths? */ 864 managed_policy = cpufreq_cpu_get(j);
862 if (unlikely(managed_policy)) { 865 if (unlikely(managed_policy)) {
863 866
864 /* Set proper policy_cpu */ 867 /* Set proper policy_cpu */
865 unlock_policy_rwsem_write(cpu); 868 unlock_policy_rwsem_write(cpu);
866 per_cpu(policy_cpu, cpu) = managed_policy->cpu; 869 per_cpu(policy_cpu, cpu) = managed_policy->cpu;
867 870
868 if (lock_policy_rwsem_write(cpu) < 0) 871 if (lock_policy_rwsem_write(cpu) < 0) {
869 goto err_out_driver_exit; 872 /* Should not go through policy unlock path */
873 if (cpufreq_driver->exit)
874 cpufreq_driver->exit(policy);
875 ret = -EBUSY;
876 cpufreq_cpu_put(managed_policy);
877 goto err_free_cpumask;
878 }
870 879
871 spin_lock_irqsave(&cpufreq_driver_lock, flags); 880 spin_lock_irqsave(&cpufreq_driver_lock, flags);
872 cpumask_copy(managed_policy->cpus, policy->cpus); 881 cpumask_copy(managed_policy->cpus, policy->cpus);
@@ -878,11 +887,13 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
878 &managed_policy->kobj, 887 &managed_policy->kobj,
879 "cpufreq"); 888 "cpufreq");
880 if (ret) 889 if (ret)
881 goto err_out_driver_exit; 890 cpufreq_cpu_put(managed_policy);
882 891 /*
883 cpufreq_debug_enable_ratelimit(); 892 * Success. We only needed to be added to the mask.
884 ret = 0; 893 * Call driver->exit() because only the cpu parent of
885 goto err_out_driver_exit; /* call driver->exit() */ 894 * the kobj needed to call init().
895 */
896 goto out_driver_exit; /* call driver->exit() */
886 } 897 }
887 } 898 }
888#endif 899#endif
@@ -892,29 +903,31 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
892 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, &sys_dev->kobj, 903 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, &sys_dev->kobj,
893 "cpufreq"); 904 "cpufreq");
894 if (ret) 905 if (ret)
895 goto err_out_driver_exit; 906 goto out_driver_exit;
896 907
897 /* set up files for this cpu device */ 908 /* set up files for this cpu device */
898 drv_attr = cpufreq_driver->attr; 909 drv_attr = cpufreq_driver->attr;
899 while ((drv_attr) && (*drv_attr)) { 910 while ((drv_attr) && (*drv_attr)) {
900 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 911 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
901 if (ret) 912 if (ret)
902 goto err_out_driver_exit; 913 goto err_out_kobj_put;
903 drv_attr++; 914 drv_attr++;
904 } 915 }
905 if (cpufreq_driver->get) { 916 if (cpufreq_driver->get) {
906 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 917 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
907 if (ret) 918 if (ret)
908 goto err_out_driver_exit; 919 goto err_out_kobj_put;
909 } 920 }
910 if (cpufreq_driver->target) { 921 if (cpufreq_driver->target) {
911 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 922 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
912 if (ret) 923 if (ret)
913 goto err_out_driver_exit; 924 goto err_out_kobj_put;
914 } 925 }
915 926
916 spin_lock_irqsave(&cpufreq_driver_lock, flags); 927 spin_lock_irqsave(&cpufreq_driver_lock, flags);
917 for_each_cpu(j, policy->cpus) { 928 for_each_cpu(j, policy->cpus) {
929 if (!cpu_online(j))
930 continue;
918 per_cpu(cpufreq_cpu_data, j) = policy; 931 per_cpu(cpufreq_cpu_data, j) = policy;
919 per_cpu(policy_cpu, j) = policy->cpu; 932 per_cpu(policy_cpu, j) = policy->cpu;
920 } 933 }
@@ -922,18 +935,22 @@ static int cpufreq_add_dev(struct sys_device *sys_dev)
922 935
923 /* symlink affected CPUs */ 936 /* symlink affected CPUs */
924 for_each_cpu(j, policy->cpus) { 937 for_each_cpu(j, policy->cpus) {
938 struct cpufreq_policy *managed_policy;
939
925 if (j == cpu) 940 if (j == cpu)
926 continue; 941 continue;
927 if (!cpu_online(j)) 942 if (!cpu_online(j))
928 continue; 943 continue;
929 944
930 dprintk("CPU %u already managed, adding link\n", j); 945 dprintk("CPU %u already managed, adding link\n", j);
931 cpufreq_cpu_get(cpu); 946 managed_policy = cpufreq_cpu_get(cpu);
932 cpu_sys_dev = get_cpu_sysdev(j); 947 cpu_sys_dev = get_cpu_sysdev(j);
933 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, 948 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
934 "cpufreq"); 949 "cpufreq");
935 if (ret) 950 if (ret) {
951 cpufreq_cpu_put(managed_policy);
936 goto err_out_unregister; 952 goto err_out_unregister;
953 }
937 } 954 }
938 955
939 policy->governor = NULL; /* to assure that the starting sequence is 956 policy->governor = NULL; /* to assure that the starting sequence is
@@ -965,17 +982,20 @@ err_out_unregister:
965 per_cpu(cpufreq_cpu_data, j) = NULL; 982 per_cpu(cpufreq_cpu_data, j) = NULL;
966 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 983 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
967 984
985err_out_kobj_put:
968 kobject_put(&policy->kobj); 986 kobject_put(&policy->kobj);
969 wait_for_completion(&policy->kobj_unregister); 987 wait_for_completion(&policy->kobj_unregister);
970 988
971err_out_driver_exit: 989out_driver_exit:
972 if (cpufreq_driver->exit) 990 if (cpufreq_driver->exit)
973 cpufreq_driver->exit(policy); 991 cpufreq_driver->exit(policy);
974 992
975err_out: 993err_unlock_policy:
976 unlock_policy_rwsem_write(cpu); 994 unlock_policy_rwsem_write(cpu);
995err_free_cpumask:
996 free_cpumask_var(policy->cpus);
997err_free_policy:
977 kfree(policy); 998 kfree(policy);
978
979nomem_out: 999nomem_out:
980 module_put(cpufreq_driver->owner); 1000 module_put(cpufreq_driver->owner);
981module_out: 1001module_out:
@@ -1070,8 +1090,6 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
1070 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1090 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1071#endif 1091#endif
1072 1092
1073 unlock_policy_rwsem_write(cpu);
1074
1075 if (cpufreq_driver->target) 1093 if (cpufreq_driver->target)
1076 __cpufreq_governor(data, CPUFREQ_GOV_STOP); 1094 __cpufreq_governor(data, CPUFREQ_GOV_STOP);
1077 1095
@@ -1088,6 +1106,8 @@ static int __cpufreq_remove_dev(struct sys_device *sys_dev)
1088 if (cpufreq_driver->exit) 1106 if (cpufreq_driver->exit)
1089 cpufreq_driver->exit(data); 1107 cpufreq_driver->exit(data);
1090 1108
1109 unlock_policy_rwsem_write(cpu);
1110
1091 free_cpumask_var(data->related_cpus); 1111 free_cpumask_var(data->related_cpus);
1092 free_cpumask_var(data->cpus); 1112 free_cpumask_var(data->cpus);
1093 kfree(data); 1113 kfree(data);
@@ -1228,13 +1248,22 @@ EXPORT_SYMBOL(cpufreq_get);
1228 1248
1229static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg) 1249static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg)
1230{ 1250{
1231 int cpu = sysdev->id;
1232 int ret = 0; 1251 int ret = 0;
1252
1253#ifdef __powerpc__
1254 int cpu = sysdev->id;
1233 unsigned int cur_freq = 0; 1255 unsigned int cur_freq = 0;
1234 struct cpufreq_policy *cpu_policy; 1256 struct cpufreq_policy *cpu_policy;
1235 1257
1236 dprintk("suspending cpu %u\n", cpu); 1258 dprintk("suspending cpu %u\n", cpu);
1237 1259
1260 /*
1261 * This whole bogosity is here because Powerbooks are made of fail.
1262 * No sane platform should need any of the code below to be run.
1263 * (it's entirely the wrong thing to do, as driver->get may
1264 * reenable interrupts on some architectures).
1265 */
1266
1238 if (!cpu_online(cpu)) 1267 if (!cpu_online(cpu))
1239 return 0; 1268 return 0;
1240 1269
@@ -1293,6 +1322,7 @@ static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg)
1293 1322
1294out: 1323out:
1295 cpufreq_cpu_put(cpu_policy); 1324 cpufreq_cpu_put(cpu_policy);
1325#endif /* __powerpc__ */
1296 return ret; 1326 return ret;
1297} 1327}
1298 1328
@@ -1306,12 +1336,18 @@ out:
1306 */ 1336 */
1307static int cpufreq_resume(struct sys_device *sysdev) 1337static int cpufreq_resume(struct sys_device *sysdev)
1308{ 1338{
1309 int cpu = sysdev->id;
1310 int ret = 0; 1339 int ret = 0;
1340
1341#ifdef __powerpc__
1342 int cpu = sysdev->id;
1311 struct cpufreq_policy *cpu_policy; 1343 struct cpufreq_policy *cpu_policy;
1312 1344
1313 dprintk("resuming cpu %u\n", cpu); 1345 dprintk("resuming cpu %u\n", cpu);
1314 1346
1347 /* As with the ->suspend method, all the code below is
1348 * only necessary because Powerbooks suck.
1349 * See commit 42d4dc3f4e1e for jokes. */
1350
1315 if (!cpu_online(cpu)) 1351 if (!cpu_online(cpu))
1316 return 0; 1352 return 0;
1317 1353
@@ -1375,6 +1411,7 @@ out:
1375 schedule_work(&cpu_policy->update); 1411 schedule_work(&cpu_policy->update);
1376fail: 1412fail:
1377 cpufreq_cpu_put(cpu_policy); 1413 cpufreq_cpu_put(cpu_policy);
1414#endif /* __powerpc__ */
1378 return ret; 1415 return ret;
1379} 1416}
1380 1417