qos.c 17 KB

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