qos.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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.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/kernel.h>
  42. #include <linux/uaccess.h>
  43. /*
  44. * locking rule: all changes to constraints or notifiers lists
  45. * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
  46. * held, taken with _irqsave. One lock to rule them all
  47. */
  48. struct pm_qos_object {
  49. struct pm_qos_constraints *constraints;
  50. struct miscdevice pm_qos_power_miscdev;
  51. char *name;
  52. };
  53. static DEFINE_SPINLOCK(pm_qos_lock);
  54. static struct pm_qos_object null_pm_qos;
  55. static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
  56. static struct pm_qos_constraints cpu_dma_constraints = {
  57. .list = PLIST_HEAD_INIT(cpu_dma_constraints.list),
  58. .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
  59. .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
  60. .type = PM_QOS_MIN,
  61. .notifiers = &cpu_dma_lat_notifier,
  62. };
  63. static struct pm_qos_object cpu_dma_pm_qos = {
  64. .constraints = &cpu_dma_constraints,
  65. };
  66. static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
  67. static struct pm_qos_constraints network_lat_constraints = {
  68. .list = PLIST_HEAD_INIT(network_lat_constraints.list),
  69. .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
  70. .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
  71. .type = PM_QOS_MIN,
  72. .notifiers = &network_lat_notifier,
  73. };
  74. static struct pm_qos_object network_lat_pm_qos = {
  75. .constraints = &network_lat_constraints,
  76. .name = "network_latency",
  77. };
  78. static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
  79. static struct pm_qos_constraints network_tput_constraints = {
  80. .list = PLIST_HEAD_INIT(network_tput_constraints.list),
  81. .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
  82. .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
  83. .type = PM_QOS_MAX,
  84. .notifiers = &network_throughput_notifier,
  85. };
  86. static struct pm_qos_object network_throughput_pm_qos = {
  87. .constraints = &network_tput_constraints,
  88. .name = "network_throughput",
  89. };
  90. static struct pm_qos_object *pm_qos_array[] = {
  91. &null_pm_qos,
  92. &cpu_dma_pm_qos,
  93. &network_lat_pm_qos,
  94. &network_throughput_pm_qos
  95. };
  96. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  97. size_t count, loff_t *f_pos);
  98. static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
  99. size_t count, loff_t *f_pos);
  100. static int pm_qos_power_open(struct inode *inode, struct file *filp);
  101. static int pm_qos_power_release(struct inode *inode, struct file *filp);
  102. static const struct file_operations pm_qos_power_fops = {
  103. .write = pm_qos_power_write,
  104. .read = pm_qos_power_read,
  105. .open = pm_qos_power_open,
  106. .release = pm_qos_power_release,
  107. .llseek = noop_llseek,
  108. };
  109. /* unlocked internal variant */
  110. static inline int pm_qos_get_value(struct pm_qos_object *o)
  111. {
  112. if (plist_head_empty(&o->constraints->list))
  113. return o->constraints->default_value;
  114. switch (o->constraints->type) {
  115. case PM_QOS_MIN:
  116. return plist_first(&o->constraints->list)->prio;
  117. case PM_QOS_MAX:
  118. return plist_last(&o->constraints->list)->prio;
  119. default:
  120. /* runtime check for not using enum */
  121. BUG();
  122. }
  123. }
  124. static inline s32 pm_qos_read_value(struct pm_qos_object *o)
  125. {
  126. return o->constraints->target_value;
  127. }
  128. static inline void pm_qos_set_value(struct pm_qos_object *o, s32 value)
  129. {
  130. o->constraints->target_value = value;
  131. }
  132. static void update_target(struct pm_qos_object *o, struct plist_node *node,
  133. int del, int value)
  134. {
  135. unsigned long flags;
  136. int prev_value, curr_value;
  137. spin_lock_irqsave(&pm_qos_lock, flags);
  138. prev_value = pm_qos_get_value(o);
  139. /* PM_QOS_DEFAULT_VALUE is a signal that the value is unchanged */
  140. if (value != PM_QOS_DEFAULT_VALUE) {
  141. /*
  142. * to change the list, we atomically remove, reinit
  143. * with new value and add, then see if the extremal
  144. * changed
  145. */
  146. plist_del(node, &o->constraints->list);
  147. plist_node_init(node, value);
  148. plist_add(node, &o->constraints->list);
  149. } else if (del) {
  150. plist_del(node, &o->constraints->list);
  151. } else {
  152. plist_add(node, &o->constraints->list);
  153. }
  154. curr_value = pm_qos_get_value(o);
  155. pm_qos_set_value(o, curr_value);
  156. spin_unlock_irqrestore(&pm_qos_lock, flags);
  157. if (prev_value != curr_value)
  158. blocking_notifier_call_chain(o->constraints->notifiers,
  159. (unsigned long)curr_value,
  160. NULL);
  161. }
  162. /**
  163. * pm_qos_request - 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.
  167. */
  168. int pm_qos_request(int pm_qos_class)
  169. {
  170. return pm_qos_read_value(pm_qos_array[pm_qos_class]);
  171. }
  172. EXPORT_SYMBOL_GPL(pm_qos_request);
  173. int pm_qos_request_active(struct pm_qos_request *req)
  174. {
  175. return req->pm_qos_class != 0;
  176. }
  177. EXPORT_SYMBOL_GPL(pm_qos_request_active);
  178. /**
  179. * pm_qos_add_request - inserts new qos request into the list
  180. * @req: pointer to a preallocated handle
  181. * @pm_qos_class: identifies which list of qos request to use
  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 characteristics. It recomputes the aggregate QoS expectations
  186. * for the pm_qos_class of parameters and initializes the pm_qos_request
  187. * handle. Caller needs to save this handle for later use in updates and
  188. * removal.
  189. */
  190. void pm_qos_add_request(struct pm_qos_request *req,
  191. int pm_qos_class, s32 value)
  192. {
  193. struct pm_qos_object *o = pm_qos_array[pm_qos_class];
  194. int new_value;
  195. if (pm_qos_request_active(req)) {
  196. WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
  197. return;
  198. }
  199. if (value == PM_QOS_DEFAULT_VALUE)
  200. new_value = o->constraints->default_value;
  201. else
  202. new_value = value;
  203. plist_node_init(&req->node, new_value);
  204. req->pm_qos_class = pm_qos_class;
  205. update_target(o, &req->node, 0, PM_QOS_DEFAULT_VALUE);
  206. }
  207. EXPORT_SYMBOL_GPL(pm_qos_add_request);
  208. /**
  209. * pm_qos_update_request - modifies an existing qos request
  210. * @req : handle to list element holding a pm_qos request to use
  211. * @value: defines the qos request
  212. *
  213. * Updates an existing qos request for the pm_qos_class of parameters along
  214. * with updating the target pm_qos_class value.
  215. *
  216. * Attempts are made to make this code callable on hot code paths.
  217. */
  218. void pm_qos_update_request(struct pm_qos_request *req,
  219. s32 new_value)
  220. {
  221. s32 temp;
  222. struct pm_qos_object *o;
  223. if (!req) /*guard against callers passing in null */
  224. return;
  225. if (!pm_qos_request_active(req)) {
  226. WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
  227. return;
  228. }
  229. o = pm_qos_array[req->pm_qos_class];
  230. if (new_value == PM_QOS_DEFAULT_VALUE)
  231. temp = o->constraints->default_value;
  232. else
  233. temp = new_value;
  234. if (temp != req->node.prio)
  235. update_target(o, &req->node, 0, temp);
  236. }
  237. EXPORT_SYMBOL_GPL(pm_qos_update_request);
  238. /**
  239. * pm_qos_remove_request - modifies an existing qos request
  240. * @req: handle to request list element
  241. *
  242. * Will remove pm qos request from the list of constraints and
  243. * recompute the current target value for the pm_qos_class. Call this
  244. * on slow code paths.
  245. */
  246. void pm_qos_remove_request(struct pm_qos_request *req)
  247. {
  248. struct pm_qos_object *o;
  249. if (req == NULL)
  250. return;
  251. /* silent return to keep pcm code cleaner */
  252. if (!pm_qos_request_active(req)) {
  253. WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
  254. return;
  255. }
  256. o = pm_qos_array[req->pm_qos_class];
  257. update_target(o, &req->node, 1, PM_QOS_DEFAULT_VALUE);
  258. memset(req, 0, sizeof(*req));
  259. }
  260. EXPORT_SYMBOL_GPL(pm_qos_remove_request);
  261. /**
  262. * pm_qos_add_notifier - sets notification entry for changes to target value
  263. * @pm_qos_class: identifies which qos target changes should be notified.
  264. * @notifier: notifier block managed by caller.
  265. *
  266. * will register the notifier into a notification chain that gets called
  267. * upon changes to the pm_qos_class target value.
  268. */
  269. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  270. {
  271. int retval;
  272. retval = blocking_notifier_chain_register(
  273. pm_qos_array[pm_qos_class]->constraints->notifiers,
  274. 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]->constraints->notifiers,
  291. notifier);
  292. return retval;
  293. }
  294. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  295. /* User space interface to PM QoS classes via misc devices */
  296. static int register_pm_qos_misc(struct pm_qos_object *qos)
  297. {
  298. qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
  299. qos->pm_qos_power_miscdev.name = qos->name;
  300. qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
  301. return misc_register(&qos->pm_qos_power_miscdev);
  302. }
  303. static int find_pm_qos_object_by_minor(int minor)
  304. {
  305. int pm_qos_class;
  306. for (pm_qos_class = 0;
  307. pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
  308. if (minor ==
  309. pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
  310. return pm_qos_class;
  311. }
  312. return -1;
  313. }
  314. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  315. {
  316. long pm_qos_class;
  317. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  318. if (pm_qos_class >= 0) {
  319. struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL);
  320. if (!req)
  321. return -ENOMEM;
  322. pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
  323. filp->private_data = req;
  324. if (filp->private_data)
  325. return 0;
  326. }
  327. return -EPERM;
  328. }
  329. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  330. {
  331. struct pm_qos_request *req;
  332. req = filp->private_data;
  333. pm_qos_remove_request(req);
  334. kfree(req);
  335. return 0;
  336. }
  337. static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
  338. size_t count, loff_t *f_pos)
  339. {
  340. s32 value;
  341. unsigned long flags;
  342. struct pm_qos_object *o;
  343. struct pm_qos_request *req = filp->private_data;
  344. if (!req)
  345. return -EINVAL;
  346. if (!pm_qos_request_active(req))
  347. return -EINVAL;
  348. o = pm_qos_array[req->pm_qos_class];
  349. spin_lock_irqsave(&pm_qos_lock, flags);
  350. value = pm_qos_get_value(o);
  351. spin_unlock_irqrestore(&pm_qos_lock, flags);
  352. return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
  353. }
  354. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  355. size_t count, loff_t *f_pos)
  356. {
  357. s32 value;
  358. struct pm_qos_request *req;
  359. if (count == sizeof(s32)) {
  360. if (copy_from_user(&value, buf, sizeof(s32)))
  361. return -EFAULT;
  362. } else if (count <= 11) { /* ASCII perhaps? */
  363. char ascii_value[11];
  364. unsigned long int ulval;
  365. int ret;
  366. if (copy_from_user(ascii_value, buf, count))
  367. return -EFAULT;
  368. if (count > 10) {
  369. if (ascii_value[10] == '\n')
  370. ascii_value[10] = '\0';
  371. else
  372. return -EINVAL;
  373. } else {
  374. ascii_value[count] = '\0';
  375. }
  376. ret = strict_strtoul(ascii_value, 16, &ulval);
  377. if (ret) {
  378. pr_debug("%s, 0x%lx, 0x%x\n", ascii_value, ulval, ret);
  379. return -EINVAL;
  380. }
  381. value = (s32)lower_32_bits(ulval);
  382. } else {
  383. return -EINVAL;
  384. }
  385. req = filp->private_data;
  386. pm_qos_update_request(req, value);
  387. return count;
  388. }
  389. static int __init pm_qos_power_init(void)
  390. {
  391. int ret = 0;
  392. ret = register_pm_qos_misc(&cpu_dma_pm_qos);
  393. if (ret < 0) {
  394. printk(KERN_ERR "pm_qos_param: cpu_dma_latency setup failed\n");
  395. return ret;
  396. }
  397. ret = register_pm_qos_misc(&network_lat_pm_qos);
  398. if (ret < 0) {
  399. printk(KERN_ERR "pm_qos_param: network_latency setup failed\n");
  400. return ret;
  401. }
  402. ret = register_pm_qos_misc(&network_throughput_pm_qos);
  403. if (ret < 0)
  404. printk(KERN_ERR
  405. "pm_qos_param: network_throughput setup failed\n");
  406. return ret;
  407. }
  408. late_initcall(pm_qos_power_init);