pm_qos_params.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. enum pm_qos_type {
  48. PM_QOS_MAX, /* return the largest value */
  49. PM_QOS_MIN /* return the smallest value */
  50. };
  51. struct pm_qos_object {
  52. struct plist_head requests;
  53. struct blocking_notifier_head *notifiers;
  54. struct miscdevice pm_qos_power_miscdev;
  55. char *name;
  56. s32 default_value;
  57. enum pm_qos_type type;
  58. };
  59. static DEFINE_SPINLOCK(pm_qos_lock);
  60. static struct pm_qos_object null_pm_qos;
  61. static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
  62. static struct pm_qos_object cpu_dma_pm_qos = {
  63. .requests = PLIST_HEAD_INIT(cpu_dma_pm_qos.requests, pm_qos_lock),
  64. .notifiers = &cpu_dma_lat_notifier,
  65. .name = "cpu_dma_latency",
  66. .default_value = 2000 * USEC_PER_SEC,
  67. .type = PM_QOS_MIN,
  68. };
  69. static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
  70. static struct pm_qos_object network_lat_pm_qos = {
  71. .requests = PLIST_HEAD_INIT(network_lat_pm_qos.requests, pm_qos_lock),
  72. .notifiers = &network_lat_notifier,
  73. .name = "network_latency",
  74. .default_value = 2000 * USEC_PER_SEC,
  75. .type = PM_QOS_MIN
  76. };
  77. static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
  78. static struct pm_qos_object network_throughput_pm_qos = {
  79. .requests = PLIST_HEAD_INIT(network_throughput_pm_qos.requests, pm_qos_lock),
  80. .notifiers = &network_throughput_notifier,
  81. .name = "network_throughput",
  82. .default_value = 0,
  83. .type = PM_QOS_MAX,
  84. };
  85. static struct pm_qos_object *pm_qos_array[] = {
  86. &null_pm_qos,
  87. &cpu_dma_pm_qos,
  88. &network_lat_pm_qos,
  89. &network_throughput_pm_qos
  90. };
  91. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  92. size_t count, loff_t *f_pos);
  93. static int pm_qos_power_open(struct inode *inode, struct file *filp);
  94. static int pm_qos_power_release(struct inode *inode, struct file *filp);
  95. static const struct file_operations pm_qos_power_fops = {
  96. .write = pm_qos_power_write,
  97. .open = pm_qos_power_open,
  98. .release = pm_qos_power_release,
  99. .llseek = noop_llseek,
  100. };
  101. /* unlocked internal variant */
  102. static inline int pm_qos_get_value(struct pm_qos_object *o)
  103. {
  104. if (plist_head_empty(&o->requests))
  105. return o->default_value;
  106. switch (o->type) {
  107. case PM_QOS_MIN:
  108. return plist_first(&o->requests)->prio;
  109. case PM_QOS_MAX:
  110. return plist_last(&o->requests)->prio;
  111. default:
  112. /* runtime check for not using enum */
  113. BUG();
  114. }
  115. }
  116. static void update_target(struct pm_qos_object *o, struct plist_node *node,
  117. int del, int value)
  118. {
  119. unsigned long flags;
  120. int prev_value, curr_value;
  121. spin_lock_irqsave(&pm_qos_lock, flags);
  122. prev_value = pm_qos_get_value(o);
  123. /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
  124. if (value != PM_QOS_DEFAULT_VALUE) {
  125. /*
  126. * to change the list, we atomically remove, reinit
  127. * with new value and add, then see if the extremal
  128. * changed
  129. */
  130. plist_del(node, &o->requests);
  131. plist_node_init(node, value);
  132. plist_add(node, &o->requests);
  133. } else if (del) {
  134. plist_del(node, &o->requests);
  135. } else {
  136. plist_add(node, &o->requests);
  137. }
  138. curr_value = pm_qos_get_value(o);
  139. spin_unlock_irqrestore(&pm_qos_lock, flags);
  140. if (prev_value != curr_value)
  141. blocking_notifier_call_chain(o->notifiers,
  142. (unsigned long)curr_value,
  143. 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_request - 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_request(int pm_qos_class)
  170. {
  171. unsigned long flags;
  172. int value;
  173. spin_lock_irqsave(&pm_qos_lock, flags);
  174. value = pm_qos_get_value(pm_qos_array[pm_qos_class]);
  175. spin_unlock_irqrestore(&pm_qos_lock, flags);
  176. return value;
  177. }
  178. EXPORT_SYMBOL_GPL(pm_qos_request);
  179. int pm_qos_request_active(struct pm_qos_request_list *req)
  180. {
  181. return req->pm_qos_class != 0;
  182. }
  183. EXPORT_SYMBOL_GPL(pm_qos_request_active);
  184. /**
  185. * pm_qos_add_request - inserts new qos request into the list
  186. * @dep: pointer to a preallocated handle
  187. * @pm_qos_class: identifies which list of qos request to use
  188. * @value: defines the qos request
  189. *
  190. * This function inserts a new entry in the pm_qos_class list of requested qos
  191. * performance characteristics. It recomputes the aggregate QoS expectations
  192. * for the pm_qos_class of parameters and initializes the pm_qos_request_list
  193. * handle. Caller needs to save this handle for later use in updates and
  194. * removal.
  195. */
  196. void pm_qos_add_request(struct pm_qos_request_list *dep,
  197. int pm_qos_class, s32 value)
  198. {
  199. struct pm_qos_object *o = pm_qos_array[pm_qos_class];
  200. int new_value;
  201. if (pm_qos_request_active(dep)) {
  202. WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
  203. return;
  204. }
  205. if (value == PM_QOS_DEFAULT_VALUE)
  206. new_value = o->default_value;
  207. else
  208. new_value = value;
  209. plist_node_init(&dep->list, new_value);
  210. dep->pm_qos_class = pm_qos_class;
  211. update_target(o, &dep->list, 0, PM_QOS_DEFAULT_VALUE);
  212. }
  213. EXPORT_SYMBOL_GPL(pm_qos_add_request);
  214. /**
  215. * pm_qos_update_request - modifies an existing qos request
  216. * @pm_qos_req : handle to list element holding a pm_qos request to use
  217. * @value: defines the qos request
  218. *
  219. * Updates an existing qos request for the pm_qos_class of parameters along
  220. * with updating the target pm_qos_class value.
  221. *
  222. * Attempts are made to make this code callable on hot code paths.
  223. */
  224. void pm_qos_update_request(struct pm_qos_request_list *pm_qos_req,
  225. s32 new_value)
  226. {
  227. s32 temp;
  228. struct pm_qos_object *o;
  229. if (!pm_qos_req) /*guard against callers passing in null */
  230. return;
  231. if (!pm_qos_request_active(pm_qos_req)) {
  232. WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
  233. return;
  234. }
  235. o = pm_qos_array[pm_qos_req->pm_qos_class];
  236. if (new_value == PM_QOS_DEFAULT_VALUE)
  237. temp = o->default_value;
  238. else
  239. temp = new_value;
  240. if (temp != pm_qos_req->list.prio)
  241. update_target(o, &pm_qos_req->list, 0, temp);
  242. }
  243. EXPORT_SYMBOL_GPL(pm_qos_update_request);
  244. /**
  245. * pm_qos_remove_request - modifies an existing qos request
  246. * @pm_qos_req: handle to request list element
  247. *
  248. * Will remove pm qos request from the list of requests and
  249. * recompute the current target value for the pm_qos_class. Call this
  250. * on slow code paths.
  251. */
  252. void pm_qos_remove_request(struct pm_qos_request_list *pm_qos_req)
  253. {
  254. struct pm_qos_object *o;
  255. if (pm_qos_req == NULL)
  256. return;
  257. /* silent return to keep pcm code cleaner */
  258. if (!pm_qos_request_active(pm_qos_req)) {
  259. WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
  260. return;
  261. }
  262. o = pm_qos_array[pm_qos_req->pm_qos_class];
  263. update_target(o, &pm_qos_req->list, 1, PM_QOS_DEFAULT_VALUE);
  264. memset(pm_qos_req, 0, sizeof(*pm_qos_req));
  265. }
  266. EXPORT_SYMBOL_GPL(pm_qos_remove_request);
  267. /**
  268. * pm_qos_add_notifier - sets notification entry for changes to target value
  269. * @pm_qos_class: identifies which qos target changes should be notified.
  270. * @notifier: notifier block managed by caller.
  271. *
  272. * will register the notifier into a notification chain that gets called
  273. * upon changes to the pm_qos_class target value.
  274. */
  275. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  276. {
  277. int retval;
  278. retval = blocking_notifier_chain_register(
  279. pm_qos_array[pm_qos_class]->notifiers, notifier);
  280. return retval;
  281. }
  282. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  283. /**
  284. * pm_qos_remove_notifier - deletes notification entry from chain.
  285. * @pm_qos_class: identifies which qos target changes are notified.
  286. * @notifier: notifier block to be removed.
  287. *
  288. * will remove the notifier from the notification chain that gets called
  289. * upon changes to the pm_qos_class target value.
  290. */
  291. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  292. {
  293. int retval;
  294. retval = blocking_notifier_chain_unregister(
  295. pm_qos_array[pm_qos_class]->notifiers, notifier);
  296. return retval;
  297. }
  298. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  299. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  300. {
  301. long pm_qos_class;
  302. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  303. if (pm_qos_class >= 0) {
  304. struct pm_qos_request_list *req = kzalloc(sizeof(*req), GFP_KERNEL);
  305. if (!req)
  306. return -ENOMEM;
  307. pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
  308. filp->private_data = req;
  309. if (filp->private_data)
  310. return 0;
  311. }
  312. return -EPERM;
  313. }
  314. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  315. {
  316. struct pm_qos_request_list *req;
  317. req = filp->private_data;
  318. pm_qos_remove_request(req);
  319. kfree(req);
  320. return 0;
  321. }
  322. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  323. size_t count, loff_t *f_pos)
  324. {
  325. s32 value;
  326. int x;
  327. char ascii_value[11];
  328. struct pm_qos_request_list *pm_qos_req;
  329. if (count == sizeof(s32)) {
  330. if (copy_from_user(&value, buf, sizeof(s32)))
  331. return -EFAULT;
  332. } else if (count == 11) { /* len('0x12345678/0') */
  333. if (copy_from_user(ascii_value, buf, 11))
  334. return -EFAULT;
  335. if (strlen(ascii_value) != 10)
  336. return -EINVAL;
  337. x = sscanf(ascii_value, "%x", &value);
  338. if (x != 1)
  339. return -EINVAL;
  340. pr_debug("%s, %d, 0x%x\n", ascii_value, x, value);
  341. } else
  342. return -EINVAL;
  343. pm_qos_req = filp->private_data;
  344. pm_qos_update_request(pm_qos_req, value);
  345. return count;
  346. }
  347. static int __init pm_qos_power_init(void)
  348. {
  349. int ret = 0;
  350. ret = register_pm_qos_misc(&cpu_dma_pm_qos);
  351. if (ret < 0) {
  352. printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
  353. return ret;
  354. }
  355. ret = register_pm_qos_misc(&network_lat_pm_qos);
  356. if (ret < 0) {
  357. printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
  358. return ret;
  359. }
  360. ret = register_pm_qos_misc(&network_throughput_pm_qos);
  361. if (ret < 0)
  362. printk(KERN_ERR
  363. "pm_qos_param: network_throughput setup failed\n");
  364. return ret;
  365. }
  366. late_initcall(pm_qos_power_init);