pm_qos_params.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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/spinlock.h>
  32. #include <linux/slab.h>
  33. #include <linux/time.h>
  34. #include <linux/fs.h>
  35. #include <linux/device.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/string.h>
  38. #include <linux/platform_device.h>
  39. #include <linux/init.h>
  40. #include <linux/uaccess.h>
  41. /*
  42. * locking rule: all changes to target_value or requirements or notifiers lists
  43. * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
  44. * held, taken with _irqsave. One lock to rule them all
  45. */
  46. struct requirement_list {
  47. struct list_head list;
  48. union {
  49. s32 value;
  50. s32 usec;
  51. s32 kbps;
  52. };
  53. char *name;
  54. };
  55. static s32 max_compare(s32 v1, s32 v2);
  56. static s32 min_compare(s32 v1, s32 v2);
  57. struct pm_qos_object {
  58. struct requirement_list requirements;
  59. struct blocking_notifier_head *notifiers;
  60. struct miscdevice pm_qos_power_miscdev;
  61. char *name;
  62. s32 default_value;
  63. s32 target_value;
  64. s32 (*comparitor)(s32, s32);
  65. };
  66. static struct pm_qos_object null_pm_qos;
  67. static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
  68. static struct pm_qos_object cpu_dma_pm_qos = {
  69. .requirements = {LIST_HEAD_INIT(cpu_dma_pm_qos.requirements.list)},
  70. .notifiers = &cpu_dma_lat_notifier,
  71. .name = "cpu_dma_latency",
  72. .default_value = 2000 * USEC_PER_SEC,
  73. .target_value = 2000 * USEC_PER_SEC,
  74. .comparitor = min_compare
  75. };
  76. static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
  77. static struct pm_qos_object network_lat_pm_qos = {
  78. .requirements = {LIST_HEAD_INIT(network_lat_pm_qos.requirements.list)},
  79. .notifiers = &network_lat_notifier,
  80. .name = "network_latency",
  81. .default_value = 2000 * USEC_PER_SEC,
  82. .target_value = 2000 * USEC_PER_SEC,
  83. .comparitor = min_compare
  84. };
  85. static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
  86. static struct pm_qos_object network_throughput_pm_qos = {
  87. .requirements =
  88. {LIST_HEAD_INIT(network_throughput_pm_qos.requirements.list)},
  89. .notifiers = &network_throughput_notifier,
  90. .name = "network_throughput",
  91. .default_value = 0,
  92. .target_value = 0,
  93. .comparitor = max_compare
  94. };
  95. static struct pm_qos_object *pm_qos_array[] = {
  96. &null_pm_qos,
  97. &cpu_dma_pm_qos,
  98. &network_lat_pm_qos,
  99. &network_throughput_pm_qos
  100. };
  101. static DEFINE_SPINLOCK(pm_qos_lock);
  102. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  103. size_t count, loff_t *f_pos);
  104. static int pm_qos_power_open(struct inode *inode, struct file *filp);
  105. static int pm_qos_power_release(struct inode *inode, struct file *filp);
  106. static const struct file_operations pm_qos_power_fops = {
  107. .write = pm_qos_power_write,
  108. .open = pm_qos_power_open,
  109. .release = pm_qos_power_release,
  110. };
  111. /* static helper functions */
  112. static s32 max_compare(s32 v1, s32 v2)
  113. {
  114. return max(v1, v2);
  115. }
  116. static s32 min_compare(s32 v1, s32 v2)
  117. {
  118. return min(v1, v2);
  119. }
  120. static void update_target(int target)
  121. {
  122. s32 extreme_value;
  123. struct requirement_list *node;
  124. unsigned long flags;
  125. int call_notifier = 0;
  126. spin_lock_irqsave(&pm_qos_lock, flags);
  127. extreme_value = pm_qos_array[target]->default_value;
  128. list_for_each_entry(node,
  129. &pm_qos_array[target]->requirements.list, list) {
  130. extreme_value = pm_qos_array[target]->comparitor(
  131. extreme_value, node->value);
  132. }
  133. if (pm_qos_array[target]->target_value != extreme_value) {
  134. call_notifier = 1;
  135. pm_qos_array[target]->target_value = extreme_value;
  136. pr_debug(KERN_ERR "new target for qos %d is %d\n", target,
  137. pm_qos_array[target]->target_value);
  138. }
  139. spin_unlock_irqrestore(&pm_qos_lock, flags);
  140. if (call_notifier)
  141. blocking_notifier_call_chain(pm_qos_array[target]->notifiers,
  142. (unsigned long) extreme_value, NULL);
  143. }
  144. static int register_pm_qos_misc(struct pm_qos_object *qos)
  145. {
  146. qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
  147. qos->pm_qos_power_miscdev.name = qos->name;
  148. qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
  149. return misc_register(&qos->pm_qos_power_miscdev);
  150. }
  151. static int find_pm_qos_object_by_minor(int minor)
  152. {
  153. int pm_qos_class;
  154. for (pm_qos_class = 0;
  155. pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
  156. if (minor ==
  157. pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
  158. return pm_qos_class;
  159. }
  160. return -1;
  161. }
  162. /**
  163. * pm_qos_requirement - returns current system wide qos expectation
  164. * @pm_qos_class: identification of which qos value is requested
  165. *
  166. * This function returns the current target value in an atomic manner.
  167. */
  168. int pm_qos_requirement(int pm_qos_class)
  169. {
  170. int ret_val;
  171. unsigned long flags;
  172. spin_lock_irqsave(&pm_qos_lock, flags);
  173. ret_val = pm_qos_array[pm_qos_class]->target_value;
  174. spin_unlock_irqrestore(&pm_qos_lock, flags);
  175. return ret_val;
  176. }
  177. EXPORT_SYMBOL_GPL(pm_qos_requirement);
  178. /**
  179. * pm_qos_add_requirement - inserts new qos request into the list
  180. * @pm_qos_class: identifies which list of qos request to us
  181. * @name: identifies the request
  182. * @value: defines the qos request
  183. *
  184. * This function inserts a new entry in the pm_qos_class list of requested qos
  185. * performance charactoistics. It recomputes the agregate QoS expectations for
  186. * the pm_qos_class of parrameters.
  187. */
  188. int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value)
  189. {
  190. struct requirement_list *dep;
  191. unsigned long flags;
  192. dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL);
  193. if (dep) {
  194. if (value == PM_QOS_DEFAULT_VALUE)
  195. dep->value = pm_qos_array[pm_qos_class]->default_value;
  196. else
  197. dep->value = value;
  198. dep->name = kstrdup(name, GFP_KERNEL);
  199. if (!dep->name)
  200. goto cleanup;
  201. spin_lock_irqsave(&pm_qos_lock, flags);
  202. list_add(&dep->list,
  203. &pm_qos_array[pm_qos_class]->requirements.list);
  204. spin_unlock_irqrestore(&pm_qos_lock, flags);
  205. update_target(pm_qos_class);
  206. return 0;
  207. }
  208. cleanup:
  209. kfree(dep);
  210. return -ENOMEM;
  211. }
  212. EXPORT_SYMBOL_GPL(pm_qos_add_requirement);
  213. /**
  214. * pm_qos_update_requirement - modifies an existing qos request
  215. * @pm_qos_class: identifies which list of qos request to us
  216. * @name: identifies the request
  217. * @value: defines the qos request
  218. *
  219. * Updates an existing qos requierement for the pm_qos_class of parameters along
  220. * with updating the target pm_qos_class value.
  221. *
  222. * If the named request isn't in the lest then no change is made.
  223. */
  224. int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value)
  225. {
  226. unsigned long flags;
  227. struct requirement_list *node;
  228. int pending_update = 0;
  229. spin_lock_irqsave(&pm_qos_lock, flags);
  230. list_for_each_entry(node,
  231. &pm_qos_array[pm_qos_class]->requirements.list, list) {
  232. if (strcmp(node->name, name) == 0) {
  233. if (new_value == PM_QOS_DEFAULT_VALUE)
  234. node->value =
  235. pm_qos_array[pm_qos_class]->default_value;
  236. else
  237. node->value = new_value;
  238. pending_update = 1;
  239. break;
  240. }
  241. }
  242. spin_unlock_irqrestore(&pm_qos_lock, flags);
  243. if (pending_update)
  244. update_target(pm_qos_class);
  245. return 0;
  246. }
  247. EXPORT_SYMBOL_GPL(pm_qos_update_requirement);
  248. /**
  249. * pm_qos_remove_requirement - modifies an existing qos request
  250. * @pm_qos_class: identifies which list of qos request to us
  251. * @name: identifies the request
  252. *
  253. * Will remove named qos request from pm_qos_class list of parrameters and
  254. * recompute the current target value for the pm_qos_class.
  255. */
  256. void pm_qos_remove_requirement(int pm_qos_class, char *name)
  257. {
  258. unsigned long flags;
  259. struct requirement_list *node;
  260. int pending_update = 0;
  261. spin_lock_irqsave(&pm_qos_lock, flags);
  262. list_for_each_entry(node,
  263. &pm_qos_array[pm_qos_class]->requirements.list, list) {
  264. if (strcmp(node->name, name) == 0) {
  265. kfree(node->name);
  266. list_del(&node->list);
  267. kfree(node);
  268. pending_update = 1;
  269. break;
  270. }
  271. }
  272. spin_unlock_irqrestore(&pm_qos_lock, flags);
  273. if (pending_update)
  274. update_target(pm_qos_class);
  275. }
  276. EXPORT_SYMBOL_GPL(pm_qos_remove_requirement);
  277. /**
  278. * pm_qos_add_notifier - sets notification entry for changes to target value
  279. * @pm_qos_class: identifies which qos target changes should be notified.
  280. * @notifier: notifier block managed by caller.
  281. *
  282. * will register the notifier into a notification chain that gets called
  283. * uppon changes to the pm_qos_class target value.
  284. */
  285. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  286. {
  287. int retval;
  288. retval = blocking_notifier_chain_register(
  289. pm_qos_array[pm_qos_class]->notifiers, notifier);
  290. return retval;
  291. }
  292. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  293. /**
  294. * pm_qos_remove_notifier - deletes notification entry from chain.
  295. * @pm_qos_class: identifies which qos target changes are notified.
  296. * @notifier: notifier block to be removed.
  297. *
  298. * will remove the notifier from the notification chain that gets called
  299. * uppon changes to the pm_qos_class target value.
  300. */
  301. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  302. {
  303. int retval;
  304. retval = blocking_notifier_chain_unregister(
  305. pm_qos_array[pm_qos_class]->notifiers, notifier);
  306. return retval;
  307. }
  308. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  309. #define PID_NAME_LEN sizeof("process_1234567890")
  310. static char name[PID_NAME_LEN];
  311. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  312. {
  313. int ret;
  314. long pm_qos_class;
  315. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  316. if (pm_qos_class >= 0) {
  317. filp->private_data = (void *)pm_qos_class;
  318. sprintf(name, "process_%d", current->pid);
  319. ret = pm_qos_add_requirement(pm_qos_class, name,
  320. PM_QOS_DEFAULT_VALUE);
  321. if (ret >= 0)
  322. return 0;
  323. }
  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);