qos.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. #include <linux/export.h>
  44. /*
  45. * locking rule: all changes to constraints or notifiers lists
  46. * or pm_qos_object list and pm_qos_objects need to happen with pm_qos_lock
  47. * held, taken with _irqsave. One lock to rule them all
  48. */
  49. struct pm_qos_object {
  50. struct pm_qos_constraints *constraints;
  51. struct miscdevice pm_qos_power_miscdev;
  52. char *name;
  53. };
  54. static DEFINE_SPINLOCK(pm_qos_lock);
  55. static struct pm_qos_object null_pm_qos;
  56. static BLOCKING_NOTIFIER_HEAD(cpu_dma_lat_notifier);
  57. static struct pm_qos_constraints cpu_dma_constraints = {
  58. .list = PLIST_HEAD_INIT(cpu_dma_constraints.list),
  59. .target_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
  60. .default_value = PM_QOS_CPU_DMA_LAT_DEFAULT_VALUE,
  61. .type = PM_QOS_MIN,
  62. .notifiers = &cpu_dma_lat_notifier,
  63. };
  64. static struct pm_qos_object cpu_dma_pm_qos = {
  65. .constraints = &cpu_dma_constraints,
  66. .name = "cpu_dma_latency",
  67. };
  68. static BLOCKING_NOTIFIER_HEAD(network_lat_notifier);
  69. static struct pm_qos_constraints network_lat_constraints = {
  70. .list = PLIST_HEAD_INIT(network_lat_constraints.list),
  71. .target_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
  72. .default_value = PM_QOS_NETWORK_LAT_DEFAULT_VALUE,
  73. .type = PM_QOS_MIN,
  74. .notifiers = &network_lat_notifier,
  75. };
  76. static struct pm_qos_object network_lat_pm_qos = {
  77. .constraints = &network_lat_constraints,
  78. .name = "network_latency",
  79. };
  80. static BLOCKING_NOTIFIER_HEAD(network_throughput_notifier);
  81. static struct pm_qos_constraints network_tput_constraints = {
  82. .list = PLIST_HEAD_INIT(network_tput_constraints.list),
  83. .target_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
  84. .default_value = PM_QOS_NETWORK_THROUGHPUT_DEFAULT_VALUE,
  85. .type = PM_QOS_MAX,
  86. .notifiers = &network_throughput_notifier,
  87. };
  88. static struct pm_qos_object network_throughput_pm_qos = {
  89. .constraints = &network_tput_constraints,
  90. .name = "network_throughput",
  91. };
  92. static struct pm_qos_object *pm_qos_array[] = {
  93. &null_pm_qos,
  94. &cpu_dma_pm_qos,
  95. &network_lat_pm_qos,
  96. &network_throughput_pm_qos
  97. };
  98. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  99. size_t count, loff_t *f_pos);
  100. static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
  101. size_t count, loff_t *f_pos);
  102. static int pm_qos_power_open(struct inode *inode, struct file *filp);
  103. static int pm_qos_power_release(struct inode *inode, struct file *filp);
  104. static const struct file_operations pm_qos_power_fops = {
  105. .write = pm_qos_power_write,
  106. .read = pm_qos_power_read,
  107. .open = pm_qos_power_open,
  108. .release = pm_qos_power_release,
  109. .llseek = noop_llseek,
  110. };
  111. /* unlocked internal variant */
  112. static inline int pm_qos_get_value(struct pm_qos_constraints *c)
  113. {
  114. if (plist_head_empty(&c->list))
  115. return c->default_value;
  116. switch (c->type) {
  117. case PM_QOS_MIN:
  118. return plist_first(&c->list)->prio;
  119. case PM_QOS_MAX:
  120. return plist_last(&c->list)->prio;
  121. default:
  122. /* runtime check for not using enum */
  123. BUG();
  124. return PM_QOS_DEFAULT_VALUE;
  125. }
  126. }
  127. s32 pm_qos_read_value(struct pm_qos_constraints *c)
  128. {
  129. return c->target_value;
  130. }
  131. static inline void pm_qos_set_value(struct pm_qos_constraints *c, s32 value)
  132. {
  133. c->target_value = value;
  134. }
  135. /**
  136. * pm_qos_update_target - manages the constraints list and calls the notifiers
  137. * if needed
  138. * @c: constraints data struct
  139. * @node: request to add to the list, to update or to remove
  140. * @action: action to take on the constraints list
  141. * @value: value of the request to add or update
  142. *
  143. * This function returns 1 if the aggregated constraint value has changed, 0
  144. * otherwise.
  145. */
  146. int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
  147. enum pm_qos_req_action action, int value)
  148. {
  149. unsigned long flags;
  150. int prev_value, curr_value, new_value;
  151. spin_lock_irqsave(&pm_qos_lock, flags);
  152. prev_value = pm_qos_get_value(c);
  153. if (value == PM_QOS_DEFAULT_VALUE)
  154. new_value = c->default_value;
  155. else
  156. new_value = value;
  157. switch (action) {
  158. case PM_QOS_REMOVE_REQ:
  159. plist_del(node, &c->list);
  160. break;
  161. case PM_QOS_UPDATE_REQ:
  162. /*
  163. * to change the list, we atomically remove, reinit
  164. * with new value and add, then see if the extremal
  165. * changed
  166. */
  167. plist_del(node, &c->list);
  168. case PM_QOS_ADD_REQ:
  169. plist_node_init(node, new_value);
  170. plist_add(node, &c->list);
  171. break;
  172. default:
  173. /* no action */
  174. ;
  175. }
  176. curr_value = pm_qos_get_value(c);
  177. pm_qos_set_value(c, curr_value);
  178. spin_unlock_irqrestore(&pm_qos_lock, flags);
  179. if (prev_value != curr_value) {
  180. blocking_notifier_call_chain(c->notifiers,
  181. (unsigned long)curr_value,
  182. NULL);
  183. return 1;
  184. } else {
  185. return 0;
  186. }
  187. }
  188. /**
  189. * pm_qos_flags_remove_req - Remove device PM QoS flags request.
  190. * @pqf: Device PM QoS flags set to remove the request from.
  191. * @req: Request to remove from the set.
  192. */
  193. static void pm_qos_flags_remove_req(struct pm_qos_flags *pqf,
  194. struct pm_qos_flags_request *req)
  195. {
  196. s32 val = 0;
  197. list_del(&req->node);
  198. list_for_each_entry(req, &pqf->list, node)
  199. val |= req->flags;
  200. pqf->effective_flags = val;
  201. }
  202. /**
  203. * pm_qos_update_flags - Update a set of PM QoS flags.
  204. * @pqf: Set of flags to update.
  205. * @req: Request to add to the set, to modify, or to remove from the set.
  206. * @action: Action to take on the set.
  207. * @val: Value of the request to add or modify.
  208. *
  209. * Update the given set of PM QoS flags and call notifiers if the aggregate
  210. * value has changed. Returns 1 if the aggregate constraint value has changed,
  211. * 0 otherwise.
  212. */
  213. bool pm_qos_update_flags(struct pm_qos_flags *pqf,
  214. struct pm_qos_flags_request *req,
  215. enum pm_qos_req_action action, s32 val)
  216. {
  217. unsigned long irqflags;
  218. s32 prev_value, curr_value;
  219. spin_lock_irqsave(&pm_qos_lock, irqflags);
  220. prev_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
  221. switch (action) {
  222. case PM_QOS_REMOVE_REQ:
  223. pm_qos_flags_remove_req(pqf, req);
  224. break;
  225. case PM_QOS_UPDATE_REQ:
  226. pm_qos_flags_remove_req(pqf, req);
  227. case PM_QOS_ADD_REQ:
  228. req->flags = val;
  229. INIT_LIST_HEAD(&req->node);
  230. list_add_tail(&req->node, &pqf->list);
  231. pqf->effective_flags |= val;
  232. break;
  233. default:
  234. /* no action */
  235. ;
  236. }
  237. curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
  238. spin_unlock_irqrestore(&pm_qos_lock, irqflags);
  239. return prev_value != curr_value;
  240. }
  241. /**
  242. * pm_qos_request - returns current system wide qos expectation
  243. * @pm_qos_class: identification of which qos value is requested
  244. *
  245. * This function returns the current target value.
  246. */
  247. int pm_qos_request(int pm_qos_class)
  248. {
  249. return pm_qos_read_value(pm_qos_array[pm_qos_class]->constraints);
  250. }
  251. EXPORT_SYMBOL_GPL(pm_qos_request);
  252. int pm_qos_request_active(struct pm_qos_request *req)
  253. {
  254. return req->pm_qos_class != 0;
  255. }
  256. EXPORT_SYMBOL_GPL(pm_qos_request_active);
  257. /**
  258. * pm_qos_work_fn - the timeout handler of pm_qos_update_request_timeout
  259. * @work: work struct for the delayed work (timeout)
  260. *
  261. * This cancels the timeout request by falling back to the default at timeout.
  262. */
  263. static void pm_qos_work_fn(struct work_struct *work)
  264. {
  265. struct pm_qos_request *req = container_of(to_delayed_work(work),
  266. struct pm_qos_request,
  267. work);
  268. pm_qos_update_request(req, PM_QOS_DEFAULT_VALUE);
  269. }
  270. /**
  271. * pm_qos_add_request - inserts new qos request into the list
  272. * @req: pointer to a preallocated handle
  273. * @pm_qos_class: identifies which list of qos request to use
  274. * @value: defines the qos request
  275. *
  276. * This function inserts a new entry in the pm_qos_class list of requested qos
  277. * performance characteristics. It recomputes the aggregate QoS expectations
  278. * for the pm_qos_class of parameters and initializes the pm_qos_request
  279. * handle. Caller needs to save this handle for later use in updates and
  280. * removal.
  281. */
  282. void pm_qos_add_request(struct pm_qos_request *req,
  283. int pm_qos_class, s32 value)
  284. {
  285. if (!req) /*guard against callers passing in null */
  286. return;
  287. if (pm_qos_request_active(req)) {
  288. WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
  289. return;
  290. }
  291. req->pm_qos_class = pm_qos_class;
  292. INIT_DELAYED_WORK(&req->work, pm_qos_work_fn);
  293. pm_qos_update_target(pm_qos_array[pm_qos_class]->constraints,
  294. &req->node, PM_QOS_ADD_REQ, value);
  295. }
  296. EXPORT_SYMBOL_GPL(pm_qos_add_request);
  297. /**
  298. * pm_qos_update_request - modifies an existing qos request
  299. * @req : handle to list element holding a pm_qos request to use
  300. * @value: defines the qos request
  301. *
  302. * Updates an existing qos request for the pm_qos_class of parameters along
  303. * with updating the target pm_qos_class value.
  304. *
  305. * Attempts are made to make this code callable on hot code paths.
  306. */
  307. void pm_qos_update_request(struct pm_qos_request *req,
  308. s32 new_value)
  309. {
  310. if (!req) /*guard against callers passing in null */
  311. return;
  312. if (!pm_qos_request_active(req)) {
  313. WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
  314. return;
  315. }
  316. cancel_delayed_work_sync(&req->work);
  317. if (new_value != req->node.prio)
  318. pm_qos_update_target(
  319. pm_qos_array[req->pm_qos_class]->constraints,
  320. &req->node, PM_QOS_UPDATE_REQ, new_value);
  321. }
  322. EXPORT_SYMBOL_GPL(pm_qos_update_request);
  323. /**
  324. * pm_qos_update_request_timeout - modifies an existing qos request temporarily.
  325. * @req : handle to list element holding a pm_qos request to use
  326. * @new_value: defines the temporal qos request
  327. * @timeout_us: the effective duration of this qos request in usecs.
  328. *
  329. * After timeout_us, this qos request is cancelled automatically.
  330. */
  331. void pm_qos_update_request_timeout(struct pm_qos_request *req, s32 new_value,
  332. unsigned long timeout_us)
  333. {
  334. if (!req)
  335. return;
  336. if (WARN(!pm_qos_request_active(req),
  337. "%s called for unknown object.", __func__))
  338. return;
  339. cancel_delayed_work_sync(&req->work);
  340. if (new_value != req->node.prio)
  341. pm_qos_update_target(
  342. pm_qos_array[req->pm_qos_class]->constraints,
  343. &req->node, PM_QOS_UPDATE_REQ, new_value);
  344. schedule_delayed_work(&req->work, usecs_to_jiffies(timeout_us));
  345. }
  346. /**
  347. * pm_qos_remove_request - modifies an existing qos request
  348. * @req: handle to request list element
  349. *
  350. * Will remove pm qos request from the list of constraints and
  351. * recompute the current target value for the pm_qos_class. Call this
  352. * on slow code paths.
  353. */
  354. void pm_qos_remove_request(struct pm_qos_request *req)
  355. {
  356. if (!req) /*guard against callers passing in null */
  357. return;
  358. /* silent return to keep pcm code cleaner */
  359. if (!pm_qos_request_active(req)) {
  360. WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
  361. return;
  362. }
  363. cancel_delayed_work_sync(&req->work);
  364. pm_qos_update_target(pm_qos_array[req->pm_qos_class]->constraints,
  365. &req->node, PM_QOS_REMOVE_REQ,
  366. PM_QOS_DEFAULT_VALUE);
  367. memset(req, 0, sizeof(*req));
  368. }
  369. EXPORT_SYMBOL_GPL(pm_qos_remove_request);
  370. /**
  371. * pm_qos_add_notifier - sets notification entry for changes to target value
  372. * @pm_qos_class: identifies which qos target changes should be notified.
  373. * @notifier: notifier block managed by caller.
  374. *
  375. * will register the notifier into a notification chain that gets called
  376. * upon changes to the pm_qos_class target value.
  377. */
  378. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  379. {
  380. int retval;
  381. retval = blocking_notifier_chain_register(
  382. pm_qos_array[pm_qos_class]->constraints->notifiers,
  383. notifier);
  384. return retval;
  385. }
  386. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  387. /**
  388. * pm_qos_remove_notifier - deletes notification entry from chain.
  389. * @pm_qos_class: identifies which qos target changes are notified.
  390. * @notifier: notifier block to be removed.
  391. *
  392. * will remove the notifier from the notification chain that gets called
  393. * upon changes to the pm_qos_class target value.
  394. */
  395. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  396. {
  397. int retval;
  398. retval = blocking_notifier_chain_unregister(
  399. pm_qos_array[pm_qos_class]->constraints->notifiers,
  400. notifier);
  401. return retval;
  402. }
  403. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  404. /* User space interface to PM QoS classes via misc devices */
  405. static int register_pm_qos_misc(struct pm_qos_object *qos)
  406. {
  407. qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
  408. qos->pm_qos_power_miscdev.name = qos->name;
  409. qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
  410. return misc_register(&qos->pm_qos_power_miscdev);
  411. }
  412. static int find_pm_qos_object_by_minor(int minor)
  413. {
  414. int pm_qos_class;
  415. for (pm_qos_class = PM_QOS_CPU_DMA_LATENCY;
  416. pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
  417. if (minor ==
  418. pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
  419. return pm_qos_class;
  420. }
  421. return -1;
  422. }
  423. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  424. {
  425. long pm_qos_class;
  426. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  427. if (pm_qos_class >= PM_QOS_CPU_DMA_LATENCY) {
  428. struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL);
  429. if (!req)
  430. return -ENOMEM;
  431. pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
  432. filp->private_data = req;
  433. return 0;
  434. }
  435. return -EPERM;
  436. }
  437. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  438. {
  439. struct pm_qos_request *req;
  440. req = filp->private_data;
  441. pm_qos_remove_request(req);
  442. kfree(req);
  443. return 0;
  444. }
  445. static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
  446. size_t count, loff_t *f_pos)
  447. {
  448. s32 value;
  449. unsigned long flags;
  450. struct pm_qos_request *req = filp->private_data;
  451. if (!req)
  452. return -EINVAL;
  453. if (!pm_qos_request_active(req))
  454. return -EINVAL;
  455. spin_lock_irqsave(&pm_qos_lock, flags);
  456. value = pm_qos_get_value(pm_qos_array[req->pm_qos_class]->constraints);
  457. spin_unlock_irqrestore(&pm_qos_lock, flags);
  458. return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
  459. }
  460. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  461. size_t count, loff_t *f_pos)
  462. {
  463. s32 value;
  464. struct pm_qos_request *req;
  465. if (count == sizeof(s32)) {
  466. if (copy_from_user(&value, buf, sizeof(s32)))
  467. return -EFAULT;
  468. } else if (count <= 11) { /* ASCII perhaps? */
  469. char ascii_value[11];
  470. unsigned long int ulval;
  471. int ret;
  472. if (copy_from_user(ascii_value, buf, count))
  473. return -EFAULT;
  474. if (count > 10) {
  475. if (ascii_value[10] == '\n')
  476. ascii_value[10] = '\0';
  477. else
  478. return -EINVAL;
  479. } else {
  480. ascii_value[count] = '\0';
  481. }
  482. ret = kstrtoul(ascii_value, 16, &ulval);
  483. if (ret) {
  484. pr_debug("%s, 0x%lx, 0x%x\n", ascii_value, ulval, ret);
  485. return -EINVAL;
  486. }
  487. value = (s32)lower_32_bits(ulval);
  488. } else {
  489. return -EINVAL;
  490. }
  491. req = filp->private_data;
  492. pm_qos_update_request(req, value);
  493. return count;
  494. }
  495. static int __init pm_qos_power_init(void)
  496. {
  497. int ret = 0;
  498. int i;
  499. BUILD_BUG_ON(ARRAY_SIZE(pm_qos_array) != PM_QOS_NUM_CLASSES);
  500. for (i = PM_QOS_CPU_DMA_LATENCY; i < PM_QOS_NUM_CLASSES; i++) {
  501. ret = register_pm_qos_misc(pm_qos_array[i]);
  502. if (ret < 0) {
  503. printk(KERN_ERR "pm_qos_param: %s setup failed\n",
  504. pm_qos_array[i]->name);
  505. return ret;
  506. }
  507. }
  508. return ret;
  509. }
  510. late_initcall(pm_qos_power_init);