pm_qos_params.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * This module exposes the interface to kernel space for specifying
  3. * QoS dependencies. It provides infrastructure for registration of:
  4. *
  5. * Dependents on a QoS value : register requirements
  6. * Watchers of QoS value : get notified when target QoS value changes
  7. *
  8. * This QoS design is best effort based. Dependents register their QoS needs.
  9. * Watchers register to keep track of the current QoS needs of the system.
  10. *
  11. * There are 3 basic classes of QoS parameter: latency, timeout, throughput
  12. * each have defined units:
  13. * latency: usec
  14. * timeout: usec <-- currently not used.
  15. * throughput: kbs (kilo byte / sec)
  16. *
  17. * There are lists of pm_qos_objects each one wrapping requirements, notifiers
  18. *
  19. * User mode requirements on a QOS parameter register themselves to the
  20. * subsystem by opening the device node /dev/... and writing there request to
  21. * the node. As long as the process holds a file handle open to the node the
  22. * client continues to be accounted for. Upon file release the usermode
  23. * requirement is removed and a new qos target is computed. This way when the
  24. * requirement that the application has is cleaned up when closes the file
  25. * pointer or exits the pm_qos_object will get an opportunity to clean up.
  26. *
  27. * Mark Gross <mgross@linux.intel.com>
  28. */
  29. #include <linux/pm_qos_params.h>
  30. #include <linux/sched.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/slab.h>
  34. #include <linux/time.h>
  35. #include <linux/fs.h>
  36. #include <linux/device.h>
  37. #include <linux/miscdevice.h>
  38. #include <linux/string.h>
  39. #include <linux/platform_device.h>
  40. #include <linux/init.h>
  41. #include <linux/uaccess.h>
  42. /*
  43. * locking rule: all changes to requirements or notifiers lists
  44. * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
  45. * held, taken with _irqsave. One lock to rule them all
  46. */
  47. struct requirement_list {
  48. struct list_head list;
  49. union {
  50. s32 value;
  51. s32 usec;
  52. s32 kbps;
  53. };
  54. char *name;
  55. };
  56. static s32 max_compare(s32 v1, s32 v2);
  57. static s32 min_compare(s32 v1, s32 v2);
  58. struct pm_qos_object {
  59. struct requirement_list requirements;
  60. struct blocking_notifier_head *notifiers;
  61. struct miscdevice pm_qos_power_miscdev;
  62. char *name;
  63. s32 default_value;
  64. atomic_t target_value;
  65. s32 (*comparitor)(s32, s32);
  66. };
  67. static struct pm_qos_object null_pm_qos;
  68. static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
  69. static struct pm_qos_object cpu_dma_pm_qos = {
  70. .requirements = {LIST_HEAD_INIT(cpu_dma_pm_qos.requirements.list)},
  71. .notifiers = &cpu_dma_lat_notifier,
  72. .name = "cpu_dma_latency",
  73. .default_value = 2000 * USEC_PER_SEC,
  74. .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
  75. .comparitor = min_compare
  76. };
  77. static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
  78. static struct pm_qos_object network_lat_pm_qos = {
  79. .requirements = {LIST_HEAD_INIT(network_lat_pm_qos.requirements.list)},
  80. .notifiers = &network_lat_notifier,
  81. .name = "network_latency",
  82. .default_value = 2000 * USEC_PER_SEC,
  83. .target_value = ATOMIC_INIT(2000 * USEC_PER_SEC),
  84. .comparitor = min_compare
  85. };
  86. static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
  87. static struct pm_qos_object network_throughput_pm_qos = {
  88. .requirements =
  89. {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)},
  90. .notifiers = &network_throughput_notifier,
  91. .name = "network_throughput",
  92. .default_value = 0,
  93. .target_value = ATOMIC_INIT(0),
  94. .comparitor = max_compare
  95. };
  96. static struct pm_qos_object *pm_qos_array[] = {
  97. &null_pm_qos,
  98. &cpu_dma_pm_qos,
  99. &network_lat_pm_qos,
  100. &network_throughput_pm_qos
  101. };
  102. static DEFINE_SPINLOCK(pm_qos_lock);
  103. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  104. size_t count, loff_t *f_pos);
  105. static int pm_qos_power_open(struct inode *inode, struct file *filp);
  106. static int pm_qos_power_release(struct inode *inode, struct file *filp);
  107. static const struct file_operations pm_qos_power_fops = {
  108. .write = pm_qos_power_write,
  109. .open = pm_qos_power_open,
  110. .release = pm_qos_power_release,
  111. };
  112. /* static helper functions */
  113. static s32 max_compare(s32 v1, s32 v2)
  114. {
  115. return max(v1, v2);
  116. }
  117. static s32 min_compare(s32 v1, s32 v2)
  118. {
  119. return min(v1, v2);
  120. }
  121. static void update_target(int target)
  122. {
  123. s32 extreme_value;
  124. struct requirement_list *node;
  125. unsigned long flags;
  126. int call_notifier = 0;
  127. spin_lock_irqsave(&pm_qos_lock, flags);
  128. extreme_value = pm_qos_array[target]->default_value;
  129. list_for_each_entry(node,
  130. &pm_qos_array[target]->requirements.list, list) {
  131. extreme_value = pm_qos_array[target]->comparitor(
  132. extreme_value, node->value);
  133. }
  134. if (atomic_read(&pm_qos_array[target]->target_value) != extreme_value) {
  135. call_notifier = 1;
  136. atomic_set(&pm_qos_array[target]->target_value, extreme_value);
  137. pr_debug(KERN_ERR "new target for qos %d is %d\n", target,
  138. atomic_read(&pm_qos_array[target]->target_value));
  139. }
  140. spin_unlock_irqrestore(&pm_qos_lock, flags);
  141. if (call_notifier)
  142. blocking_notifier_call_chain(pm_qos_array[target]->notifiers,
  143. (unsigned long) extreme_value, NULL);
  144. }
  145. static int register_pm_qos_misc(struct pm_qos_object *qos)
  146. {
  147. qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
  148. qos->pm_qos_power_miscdev.name = qos->name;
  149. qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
  150. return misc_register(&qos->pm_qos_power_miscdev);
  151. }
  152. static int find_pm_qos_object_by_minor(int minor)
  153. {
  154. int pm_qos_class;
  155. for (pm_qos_class = 0;
  156. pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
  157. if (minor ==
  158. pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
  159. return pm_qos_class;
  160. }
  161. return -1;
  162. }
  163. /**
  164. * pm_qos_requirement - returns current system wide qos expectation
  165. * @pm_qos_class: identification of which qos value is requested
  166. *
  167. * This function returns the current target value in an atomic manner.
  168. */
  169. int pm_qos_requirement(int pm_qos_class)
  170. {
  171. return atomic_read(&pm_qos_array[pm_qos_class]->target_value);
  172. }
  173. EXPORT_SYMBOL_GPL(pm_qos_requirement);
  174. /**
  175. * pm_qos_add_requirement - inserts new qos request into the list
  176. * @pm_qos_class: identifies which list of qos request to us
  177. * @name: identifies the request
  178. * @value: defines the qos request
  179. *
  180. * This function inserts a new entry in the pm_qos_class list of requested qos
  181. * performance characteristics. It recomputes the aggregate QoS expectations
  182. * for the pm_qos_class of parameters.
  183. */
  184. int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value)
  185. {
  186. struct requirement_list *dep;
  187. unsigned long flags;
  188. dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL);
  189. if (dep) {
  190. if (value == PM_QOS_DEFAULT_VALUE)
  191. dep->value = pm_qos_array[pm_qos_class]->default_value;
  192. else
  193. dep->value = value;
  194. dep->name = kstrdup(name, GFP_KERNEL);
  195. if (!dep->name)
  196. goto cleanup;
  197. spin_lock_irqsave(&pm_qos_lock, flags);
  198. list_add(&dep->list,
  199. &pm_qos_array[pm_qos_class]->requirements.list);
  200. spin_unlock_irqrestore(&pm_qos_lock, flags);
  201. update_target(pm_qos_class);
  202. return 0;
  203. }
  204. cleanup:
  205. kfree(dep);
  206. return -ENOMEM;
  207. }
  208. EXPORT_SYMBOL_GPL(pm_qos_add_requirement);
  209. /**
  210. * pm_qos_update_requirement - modifies an existing qos request
  211. * @pm_qos_class: identifies which list of qos request to us
  212. * @name: identifies the request
  213. * @value: defines the qos request
  214. *
  215. * Updates an existing qos requirement for the pm_qos_class of parameters along
  216. * with updating the target pm_qos_class value.
  217. *
  218. * If the named request isn't in the list then no change is made.
  219. */
  220. int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value)
  221. {
  222. unsigned long flags;
  223. struct requirement_list *node;
  224. int pending_update = 0;
  225. spin_lock_irqsave(&pm_qos_lock, flags);
  226. list_for_each_entry(node,
  227. &pm_qos_array[pm_qos_class]->requirements.list, list) {
  228. if (strcmp(node->name, name) == 0) {
  229. if (new_value == PM_QOS_DEFAULT_VALUE)
  230. node->value =
  231. pm_qos_array[pm_qos_class]->default_value;
  232. else
  233. node->value = new_value;
  234. pending_update = 1;
  235. break;
  236. }
  237. }
  238. spin_unlock_irqrestore(&pm_qos_lock, flags);
  239. if (pending_update)
  240. update_target(pm_qos_class);
  241. return 0;
  242. }
  243. EXPORT_SYMBOL_GPL(pm_qos_update_requirement);
  244. /**
  245. * pm_qos_remove_requirement - modifies an existing qos request
  246. * @pm_qos_class: identifies which list of qos request to us
  247. * @name: identifies the request
  248. *
  249. * Will remove named qos request from pm_qos_class list of parameters and
  250. * recompute the current target value for the pm_qos_class.
  251. */
  252. void pm_qos_remove_requirement(int pm_qos_class, char *name)
  253. {
  254. unsigned long flags;
  255. struct requirement_list *node;
  256. int pending_update = 0;
  257. spin_lock_irqsave(&pm_qos_lock, flags);
  258. list_for_each_entry(node,
  259. &pm_qos_array[pm_qos_class]->requirements.list, list) {
  260. if (strcmp(node->name, name) == 0) {
  261. kfree(node->name);
  262. list_del(&node->list);
  263. kfree(node);
  264. pending_update = 1;
  265. break;
  266. }
  267. }
  268. spin_unlock_irqrestore(&pm_qos_lock, flags);
  269. if (pending_update)
  270. update_target(pm_qos_class);
  271. }
  272. EXPORT_SYMBOL_GPL(pm_qos_remove_requirement);
  273. /**
  274. * pm_qos_add_notifier - sets notification entry for changes to target value
  275. * @pm_qos_class: identifies which qos target changes should be notified.
  276. * @notifier: notifier block managed by caller.
  277. *
  278. * will register the notifier into a notification chain that gets called
  279. * upon changes to the pm_qos_class target value.
  280. */
  281. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  282. {
  283. int retval;
  284. retval = blocking_notifier_chain_register(
  285. pm_qos_array[pm_qos_class]->notifiers, notifier);
  286. return retval;
  287. }
  288. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  289. /**
  290. * pm_qos_remove_notifier - deletes notification entry from chain.
  291. * @pm_qos_class: identifies which qos target changes are notified.
  292. * @notifier: notifier block to be removed.
  293. *
  294. * will remove the notifier from the notification chain that gets called
  295. * upon changes to the pm_qos_class target value.
  296. */
  297. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  298. {
  299. int retval;
  300. retval = blocking_notifier_chain_unregister(
  301. pm_qos_array[pm_qos_class]->notifiers, notifier);
  302. return retval;
  303. }
  304. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  305. #define PID_NAME_LEN sizeof("process_1234567890")
  306. static char name[PID_NAME_LEN];
  307. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  308. {
  309. int ret;
  310. long pm_qos_class;
  311. lock_kernel();
  312. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  313. if (pm_qos_class >= 0) {
  314. filp->private_data = (void *)pm_qos_class;
  315. sprintf(name, "process_%d", current->pid);
  316. ret = pm_qos_add_requirement(pm_qos_class, name,
  317. PM_QOS_DEFAULT_VALUE);
  318. if (ret >= 0) {
  319. unlock_kernel();
  320. return 0;
  321. }
  322. }
  323. unlock_kernel();
  324. return -EPERM;
  325. }
  326. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  327. {
  328. int pm_qos_class;
  329. pm_qos_class = (long)filp->private_data;
  330. sprintf(name, "process_%d", current->pid);
  331. pm_qos_remove_requirement(pm_qos_class, name);
  332. return 0;
  333. }
  334. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  335. size_t count, loff_t *f_pos)
  336. {
  337. s32 value;
  338. int pm_qos_class;
  339. pm_qos_class = (long)filp->private_data;
  340. if (count != sizeof(s32))
  341. return -EINVAL;
  342. if (copy_from_user(&value, buf, sizeof(s32)))
  343. return -EFAULT;
  344. sprintf(name, "process_%d", current->pid);
  345. pm_qos_update_requirement(pm_qos_class, name, value);
  346. return sizeof(s32);
  347. }
  348. static int __init pm_qos_power_init(void)
  349. {
  350. int ret = 0;
  351. ret = register_pm_qos_misc(&cpu_dma_pm_qos);
  352. if (ret < 0) {
  353. printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
  354. return ret;
  355. }
  356. ret = register_pm_qos_misc(&network_lat_pm_qos);
  357. if (ret < 0) {
  358. printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
  359. return ret;
  360. }
  361. ret = register_pm_qos_misc(&network_throughput_pm_qos);
  362. if (ret < 0)
  363. printk(KERN_ERR
  364. "pm_qos_param: network_throughput setup failed\n");
  365. return ret;
  366. }
  367. late_initcall(pm_qos_power_init);