pm_qos_params.c 12 KB

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