pm_qos_params.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 target_value or 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. s32 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 = 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 = 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 = 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 (pm_qos_array[target]->target_value != extreme_value) {
  135. call_notifier = 1;
  136. pm_qos_array[target]->target_value = extreme_value;
  137. pr_debug(KERN_ERR "new target for qos %d is %d\n", target,
  138. 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. int ret_val;
  172. unsigned long flags;
  173. spin_lock_irqsave(&pm_qos_lock, flags);
  174. ret_val = pm_qos_array[pm_qos_class]->target_value;
  175. spin_unlock_irqrestore(&pm_qos_lock, flags);
  176. return ret_val;
  177. }
  178. EXPORT_SYMBOL_GPL(pm_qos_requirement);
  179. /**
  180. * pm_qos_add_requirement - inserts new qos request into the list
  181. * @pm_qos_class: identifies which list of qos request to us
  182. * @name: identifies the request
  183. * @value: defines the qos request
  184. *
  185. * This function inserts a new entry in the pm_qos_class list of requested qos
  186. * performance charactoistics. It recomputes the agregate QoS expectations for
  187. * the pm_qos_class of parrameters.
  188. */
  189. int pm_qos_add_requirement(int pm_qos_class, char *name, s32 value)
  190. {
  191. struct requirement_list *dep;
  192. unsigned long flags;
  193. dep = kzalloc(sizeof(struct requirement_list), GFP_KERNEL);
  194. if (dep) {
  195. if (value == PM_QOS_DEFAULT_VALUE)
  196. dep->value = pm_qos_array[pm_qos_class]->default_value;
  197. else
  198. dep->value = value;
  199. dep->name = kstrdup(name, GFP_KERNEL);
  200. if (!dep->name)
  201. goto cleanup;
  202. spin_lock_irqsave(&pm_qos_lock, flags);
  203. list_add(&dep->list,
  204. &pm_qos_array[pm_qos_class]->requirements.list);
  205. spin_unlock_irqrestore(&pm_qos_lock, flags);
  206. update_target(pm_qos_class);
  207. return 0;
  208. }
  209. cleanup:
  210. kfree(dep);
  211. return -ENOMEM;
  212. }
  213. EXPORT_SYMBOL_GPL(pm_qos_add_requirement);
  214. /**
  215. * pm_qos_update_requirement - modifies an existing qos request
  216. * @pm_qos_class: identifies which list of qos request to us
  217. * @name: identifies the request
  218. * @value: defines the qos request
  219. *
  220. * Updates an existing qos requierement for the pm_qos_class of parameters along
  221. * with updating the target pm_qos_class value.
  222. *
  223. * If the named request isn't in the lest then no change is made.
  224. */
  225. int pm_qos_update_requirement(int pm_qos_class, char *name, s32 new_value)
  226. {
  227. unsigned long flags;
  228. struct requirement_list *node;
  229. int pending_update = 0;
  230. spin_lock_irqsave(&pm_qos_lock, flags);
  231. list_for_each_entry(node,
  232. &pm_qos_array[pm_qos_class]->requirements.list, list) {
  233. if (strcmp(node->name, name) == 0) {
  234. if (new_value == PM_QOS_DEFAULT_VALUE)
  235. node->value =
  236. pm_qos_array[pm_qos_class]->default_value;
  237. else
  238. node->value = new_value;
  239. pending_update = 1;
  240. break;
  241. }
  242. }
  243. spin_unlock_irqrestore(&pm_qos_lock, flags);
  244. if (pending_update)
  245. update_target(pm_qos_class);
  246. return 0;
  247. }
  248. EXPORT_SYMBOL_GPL(pm_qos_update_requirement);
  249. /**
  250. * pm_qos_remove_requirement - modifies an existing qos request
  251. * @pm_qos_class: identifies which list of qos request to us
  252. * @name: identifies the request
  253. *
  254. * Will remove named qos request from pm_qos_class list of parrameters and
  255. * recompute the current target value for the pm_qos_class.
  256. */
  257. void pm_qos_remove_requirement(int pm_qos_class, char *name)
  258. {
  259. unsigned long flags;
  260. struct requirement_list *node;
  261. int pending_update = 0;
  262. spin_lock_irqsave(&pm_qos_lock, flags);
  263. list_for_each_entry(node,
  264. &pm_qos_array[pm_qos_class]->requirements.list, list) {
  265. if (strcmp(node->name, name) == 0) {
  266. kfree(node->name);
  267. list_del(&node->list);
  268. kfree(node);
  269. pending_update = 1;
  270. break;
  271. }
  272. }
  273. spin_unlock_irqrestore(&pm_qos_lock, flags);
  274. if (pending_update)
  275. update_target(pm_qos_class);
  276. }
  277. EXPORT_SYMBOL_GPL(pm_qos_remove_requirement);
  278. /**
  279. * pm_qos_add_notifier - sets notification entry for changes to target value
  280. * @pm_qos_class: identifies which qos target changes should be notified.
  281. * @notifier: notifier block managed by caller.
  282. *
  283. * will register the notifier into a notification chain that gets called
  284. * uppon changes to the pm_qos_class target value.
  285. */
  286. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  287. {
  288. int retval;
  289. retval = blocking_notifier_chain_register(
  290. pm_qos_array[pm_qos_class]->notifiers, notifier);
  291. return retval;
  292. }
  293. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  294. /**
  295. * pm_qos_remove_notifier - deletes notification entry from chain.
  296. * @pm_qos_class: identifies which qos target changes are notified.
  297. * @notifier: notifier block to be removed.
  298. *
  299. * will remove the notifier from the notification chain that gets called
  300. * uppon changes to the pm_qos_class target value.
  301. */
  302. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  303. {
  304. int retval;
  305. retval = blocking_notifier_chain_unregister(
  306. pm_qos_array[pm_qos_class]->notifiers, notifier);
  307. return retval;
  308. }
  309. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  310. #define PID_NAME_LEN sizeof("process_1234567890")
  311. static char name[PID_NAME_LEN];
  312. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  313. {
  314. int ret;
  315. long pm_qos_class;
  316. lock_kernel();
  317. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  318. if (pm_qos_class >= 0) {
  319. filp->private_data = (void *)pm_qos_class;
  320. sprintf(name, "process_%d", current->pid);
  321. ret = pm_qos_add_requirement(pm_qos_class, name,
  322. PM_QOS_DEFAULT_VALUE);
  323. if (ret >= 0) {
  324. unlock_kernel();
  325. return 0;
  326. }
  327. }
  328. unlock_kernel();
  329. return -EPERM;
  330. }
  331. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  332. {
  333. int pm_qos_class;
  334. pm_qos_class = (long)filp->private_data;
  335. sprintf(name, "process_%d", current->pid);
  336. pm_qos_remove_requirement(pm_qos_class, name);
  337. return 0;
  338. }
  339. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  340. size_t count, loff_t *f_pos)
  341. {
  342. s32 value;
  343. int pm_qos_class;
  344. pm_qos_class = (long)filp->private_data;
  345. if (count != sizeof(s32))
  346. return -EINVAL;
  347. if (copy_from_user(&value, buf, sizeof(s32)))
  348. return -EFAULT;
  349. sprintf(name, "process_%d", current->pid);
  350. pm_qos_update_requirement(pm_qos_class, name, value);
  351. return sizeof(s32);
  352. }
  353. static int __init pm_qos_power_init(void)
  354. {
  355. int ret = 0;
  356. ret = register_pm_qos_misc(&cpu_dma_pm_qos);
  357. if (ret < 0) {
  358. printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
  359. return ret;
  360. }
  361. ret = register_pm_qos_misc(&network_lat_pm_qos);
  362. if (ret < 0) {
  363. printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
  364. return ret;
  365. }
  366. ret = register_pm_qos_misc(&network_throughput_pm_qos);
  367. if (ret < 0)
  368. printk(KERN_ERR
  369. "pm_qos_param: network_throughput setup failed\n");
  370. return ret;
  371. }
  372. late_initcall(pm_qos_power_init);