qos.c 17 KB

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