pm_qos_params.c 13 KB

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