pm_qos_params.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 requests
  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 requests, notifiers
  18. *
  19. * User mode requests 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. * request is removed and a new qos target is computed. This way when the
  24. * request 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. /*#define DEBUG*/
  30. #include <linux/pm_qos_params.h>
  31. #include <linux/sched.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 requests 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 pm_qos_request_list {
  48. struct list_head list;
  49. union {
  50. s32 value;
  51. s32 usec;
  52. s32 kbps;
  53. };
  54. int pm_qos_class;
  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 pm_qos_request_list requests;
  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. .requests = {LIST_HEAD_INIT(cpu_dma_pm_qos.requests.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. .requests = {LIST_HEAD_INIT(network_lat_pm_qos.requests.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. .requests = {LIST_HEAD_INIT(network_throughput_pm_qos.requests.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 pm_qos_class)
  121. {
  122. s32 extreme_value;
  123. struct pm_qos_request_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[pm_qos_class]->default_value;
  128. list_for_each_entry(node,
  129. &pm_qos_array[pm_qos_class]->requests.list, list) {
  130. extreme_value = pm_qos_array[pm_qos_class]->comparitor(
  131. extreme_value, node->value);
  132. }
  133. if (atomic_read(&pm_qos_array[pm_qos_class]->target_value) !=
  134. extreme_value) {
  135. call_notifier = 1;
  136. atomic_set(&pm_qos_array[pm_qos_class]->target_value,
  137. extreme_value);
  138. pr_debug(KERN_ERR "new target for qos %d is %d\n", pm_qos_class,
  139. atomic_read(&pm_qos_array[pm_qos_class]->target_value));
  140. }
  141. spin_unlock_irqrestore(&pm_qos_lock, flags);
  142. if (call_notifier)
  143. blocking_notifier_call_chain(
  144. pm_qos_array[pm_qos_class]->notifiers,
  145. (unsigned long) extreme_value, NULL);
  146. }
  147. static int register_pm_qos_misc(struct pm_qos_object *qos)
  148. {
  149. qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
  150. qos->pm_qos_power_miscdev.name = qos->name;
  151. qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
  152. return misc_register(&qos->pm_qos_power_miscdev);
  153. }
  154. static int find_pm_qos_object_by_minor(int minor)
  155. {
  156. int pm_qos_class;
  157. for (pm_qos_class = 0;
  158. pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
  159. if (minor ==
  160. pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
  161. return pm_qos_class;
  162. }
  163. return -1;
  164. }
  165. /**
  166. * pm_qos_request - returns current system wide qos expectation
  167. * @pm_qos_class: identification of which qos value is requested
  168. *
  169. * This function returns the current target value in an atomic manner.
  170. */
  171. int pm_qos_request(int pm_qos_class)
  172. {
  173. return atomic_read(&pm_qos_array[pm_qos_class]->target_value);
  174. }
  175. EXPORT_SYMBOL_GPL(pm_qos_request);
  176. /**
  177. * pm_qos_add_request - inserts new qos request into the list
  178. * @pm_qos_class: identifies which list of qos request to us
  179. * @value: defines the qos request
  180. *
  181. * This function inserts a new entry in the pm_qos_class list of requested qos
  182. * performance characteristics. It recomputes the aggregate QoS expectations
  183. * for the pm_qos_class of parameters, and returns the pm_qos_request list
  184. * element as a handle for use in updating and removal. Call needs to save
  185. * this handle for later use.
  186. */
  187. struct pm_qos_request_list *pm_qos_add_request(int pm_qos_class, s32 value)
  188. {
  189. struct pm_qos_request_list *dep;
  190. unsigned long flags;
  191. dep = kzalloc(sizeof(struct pm_qos_request_list), GFP_KERNEL);
  192. if (dep) {
  193. if (value == PM_QOS_DEFAULT_VALUE)
  194. dep->value = pm_qos_array[pm_qos_class]->default_value;
  195. else
  196. dep->value = value;
  197. dep->pm_qos_class = pm_qos_class;
  198. spin_lock_irqsave(&pm_qos_lock, flags);
  199. list_add(&dep->list,
  200. &pm_qos_array[pm_qos_class]->requests.list);
  201. spin_unlock_irqrestore(&pm_qos_lock, flags);
  202. update_target(pm_qos_class);
  203. }
  204. return dep;
  205. }
  206. EXPORT_SYMBOL_GPL(pm_qos_add_request);
  207. /**
  208. * pm_qos_update_request - modifies an existing qos request
  209. * @pm_qos_req : handle to list element holding a pm_qos request to use
  210. * @value: defines the qos request
  211. *
  212. * Updates an existing qos request for the pm_qos_class of parameters along
  213. * with updating the target pm_qos_class value.
  214. *
  215. * Attempts are made to make this code callable on hot code paths.
  216. */
  217. void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
  218. s32 new_value)
  219. {
  220. unsigned long flags;
  221. int pending_update = 0;
  222. s32 temp;
  223. if (pm_qos_req) { /*guard against callers passing in null */
  224. spin_lock_irqsave(&pm_qos_lock, flags);
  225. if (new_value == PM_QOS_DEFAULT_VALUE)
  226. temp = pm_qos_array[pm_qos_req->pm_qos_class]->default_value;
  227. else
  228. temp = new_value;
  229. if (temp != pm_qos_req->value) {
  230. pending_update = 1;
  231. pm_qos_req->value = temp;
  232. }
  233. spin_unlock_irqrestore(&pm_qos_lock, flags);
  234. if (pending_update)
  235. update_target(pm_qos_req->pm_qos_class);
  236. }
  237. }
  238. EXPORT_SYMBOL_GPL(pm_qos_update_request);
  239. /**
  240. * pm_qos_remove_request - modifies an existing qos request
  241. * @pm_qos_req: handle to request list element
  242. *
  243. * Will remove pm qos request from the list of requests and
  244. * recompute the current target value for the pm_qos_class. Call this
  245. * on slow code paths.
  246. */
  247. void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
  248. {
  249. unsigned long flags;
  250. int qos_class;
  251. if (pm_qos_req == NULL)
  252. return;
  253. /* silent return to keep pcm code cleaner */
  254. qos_class = pm_qos_req->pm_qos_class;
  255. spin_lock_irqsave(&pm_qos_lock, flags);
  256. list_del(&pm_qos_req->list);
  257. kfree(pm_qos_req);
  258. spin_unlock_irqrestore(&pm_qos_lock, flags);
  259. update_target(qos_class);
  260. }
  261. EXPORT_SYMBOL_GPL(pm_qos_remove_request);
  262. /**
  263. * pm_qos_add_notifier - sets notification entry for changes to target value
  264. * @pm_qos_class: identifies which qos target changes should be notified.
  265. * @notifier: notifier block managed by caller.
  266. *
  267. * will register the notifier into a notification chain that gets called
  268. * upon changes to the pm_qos_class target value.
  269. */
  270. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  271. {
  272. int retval;
  273. retval = blocking_notifier_chain_register(
  274. pm_qos_array[pm_qos_class]->notifiers, notifier);
  275. return retval;
  276. }
  277. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  278. /**
  279. * pm_qos_remove_notifier - deletes notification entry from chain.
  280. * @pm_qos_class: identifies which qos target changes are notified.
  281. * @notifier: notifier block to be removed.
  282. *
  283. * will remove the notifier from the notification chain that gets called
  284. * upon changes to the pm_qos_class target value.
  285. */
  286. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  287. {
  288. int retval;
  289. retval = blocking_notifier_chain_unregister(
  290. pm_qos_array[pm_qos_class]->notifiers, notifier);
  291. return retval;
  292. }
  293. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  294. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  295. {
  296. long pm_qos_class;
  297. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  298. if (pm_qos_class >= 0) {
  299. filp->private_data = (void *) pm_qos_add_request(pm_qos_class,
  300. PM_QOS_DEFAULT_VALUE);
  301. if (filp->private_data)
  302. return 0;
  303. }
  304. return -EPERM;
  305. }
  306. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  307. {
  308. struct pm_qos_request_list *req;
  309. req = (struct pm_qos_request_list *)filp->private_data;
  310. pm_qos_remove_request(req);
  311. return 0;
  312. }
  313. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  314. size_t count, loff_t *f_pos)
  315. {
  316. s32 value;
  317. int x;
  318. char ascii_value[11];
  319. struct pm_qos_request_list *pm_qos_req;
  320. if (count == sizeof(s32)) {
  321. if (copy_from_user(&value, buf, sizeof(s32)))
  322. return -EFAULT;
  323. } else if (count == 11) { /* len('0x12345678/0') */
  324. if (copy_from_user(ascii_value, buf, 11))
  325. return -EFAULT;
  326. x = sscanf(ascii_value, "%x", &value);
  327. if (x != 1)
  328. return -EINVAL;
  329. pr_debug(KERN_ERR "%s, %d, 0x%x\n", ascii_value, x, value);
  330. } else
  331. return -EINVAL;
  332. pm_qos_req = (struct pm_qos_request_list *)filp->private_data;
  333. pm_qos_update_request(pm_qos_req, value);
  334. return count;
  335. }
  336. static int __init pm_qos_power_init(void)
  337. {
  338. int ret = 0;
  339. ret = register_pm_qos_misc(&cpu_dma_pm_qos);
  340. if (ret < 0) {
  341. printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
  342. return ret;
  343. }
  344. ret = register_pm_qos_misc(&network_lat_pm_qos);
  345. if (ret < 0) {
  346. printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
  347. return ret;
  348. }
  349. ret = register_pm_qos_misc(&network_throughput_pm_qos);
  350. if (ret < 0)
  351. printk(KERN_ERR
  352. "pm_qos_param: network_throughput setup failed\n");
  353. return ret;
  354. }
  355. late_initcall(pm_qos_power_init);