qos.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. /**
  261. * pm_qos_work_fn - the timeout handler of pm_qos_update_request_timeout
  262. * @work: work struct for the delayed work (timeout)
  263. *
  264. * This cancels the timeout request by falling back to the default at timeout.
  265. */
  266. static void pm_qos_work_fn(struct work_struct *work)
  267. {
  268. struct pm_qos_request *req = container_of(to_delayed_work(work),
  269. struct pm_qos_request,
  270. work);
  271. pm_qos_update_request(req, PM_QOS_DEFAULT_VALUE);
  272. }
  273. /**
  274. * pm_qos_add_request - inserts new qos request into the list
  275. * @req: pointer to a preallocated handle
  276. * @pm_qos_class: identifies which list of qos request to use
  277. * @value: defines the qos request
  278. *
  279. * This function inserts a new entry in the pm_qos_class list of requested qos
  280. * performance characteristics. It recomputes the aggregate QoS expectations
  281. * for the pm_qos_class of parameters and initializes the pm_qos_request
  282. * handle. Caller needs to save this handle for later use in updates and
  283. * removal.
  284. */
  285. void pm_qos_add_request(struct pm_qos_request *req,
  286. int pm_qos_class, s32 value)
  287. {
  288. if (!req) /*guard against callers passing in null */
  289. return;
  290. if (pm_qos_request_active(req)) {
  291. WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
  292. return;
  293. }
  294. req->pm_qos_class = pm_qos_class;
  295. INIT_DELAYED_WORK(&req->work, pm_qos_work_fn);
  296. trace_pm_qos_add_request(pm_qos_class, value);
  297. pm_qos_update_target(pm_qos_array[pm_qos_class]->constraints,
  298. &req->node, PM_QOS_ADD_REQ, value);
  299. }
  300. EXPORT_SYMBOL_GPL(pm_qos_add_request);
  301. /**
  302. * pm_qos_update_request - modifies an existing qos request
  303. * @req : handle to list element holding a pm_qos request to use
  304. * @value: defines the qos request
  305. *
  306. * Updates an existing qos request for the pm_qos_class of parameters along
  307. * with updating the target pm_qos_class value.
  308. *
  309. * Attempts are made to make this code callable on hot code paths.
  310. */
  311. void pm_qos_update_request(struct pm_qos_request *req,
  312. s32 new_value)
  313. {
  314. if (!req) /*guard against callers passing in null */
  315. return;
  316. if (!pm_qos_request_active(req)) {
  317. WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
  318. return;
  319. }
  320. cancel_delayed_work_sync(&req->work);
  321. trace_pm_qos_update_request(req->pm_qos_class, new_value);
  322. if (new_value != req->node.prio)
  323. pm_qos_update_target(
  324. pm_qos_array[req->pm_qos_class]->constraints,
  325. &req->node, PM_QOS_UPDATE_REQ, new_value);
  326. }
  327. EXPORT_SYMBOL_GPL(pm_qos_update_request);
  328. /**
  329. * pm_qos_update_request_timeout - modifies an existing qos request temporarily.
  330. * @req : handle to list element holding a pm_qos request to use
  331. * @new_value: defines the temporal qos request
  332. * @timeout_us: the effective duration of this qos request in usecs.
  333. *
  334. * After timeout_us, this qos request is cancelled automatically.
  335. */
  336. void pm_qos_update_request_timeout(struct pm_qos_request *req, s32 new_value,
  337. unsigned long timeout_us)
  338. {
  339. if (!req)
  340. return;
  341. if (WARN(!pm_qos_request_active(req),
  342. "%s called for unknown object.", __func__))
  343. return;
  344. cancel_delayed_work_sync(&req->work);
  345. trace_pm_qos_update_request_timeout(req->pm_qos_class,
  346. new_value, timeout_us);
  347. if (new_value != req->node.prio)
  348. pm_qos_update_target(
  349. pm_qos_array[req->pm_qos_class]->constraints,
  350. &req->node, PM_QOS_UPDATE_REQ, new_value);
  351. schedule_delayed_work(&req->work, usecs_to_jiffies(timeout_us));
  352. }
  353. /**
  354. * pm_qos_remove_request - modifies an existing qos request
  355. * @req: handle to request list element
  356. *
  357. * Will remove pm qos request from the list of constraints and
  358. * recompute the current target value for the pm_qos_class. Call this
  359. * on slow code paths.
  360. */
  361. void pm_qos_remove_request(struct pm_qos_request *req)
  362. {
  363. if (!req) /*guard against callers passing in null */
  364. return;
  365. /* silent return to keep pcm code cleaner */
  366. if (!pm_qos_request_active(req)) {
  367. WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
  368. return;
  369. }
  370. cancel_delayed_work_sync(&req->work);
  371. trace_pm_qos_remove_request(req->pm_qos_class, PM_QOS_DEFAULT_VALUE);
  372. pm_qos_update_target(pm_qos_array[req->pm_qos_class]->constraints,
  373. &req->node, PM_QOS_REMOVE_REQ,
  374. PM_QOS_DEFAULT_VALUE);
  375. memset(req, 0, sizeof(*req));
  376. }
  377. EXPORT_SYMBOL_GPL(pm_qos_remove_request);
  378. /**
  379. * pm_qos_add_notifier - sets notification entry for changes to target value
  380. * @pm_qos_class: identifies which qos target changes should be notified.
  381. * @notifier: notifier block managed by caller.
  382. *
  383. * will register the notifier into a notification chain that gets called
  384. * upon changes to the pm_qos_class target value.
  385. */
  386. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  387. {
  388. int retval;
  389. retval = blocking_notifier_chain_register(
  390. pm_qos_array[pm_qos_class]->constraints->notifiers,
  391. notifier);
  392. return retval;
  393. }
  394. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  395. /**
  396. * pm_qos_remove_notifier - deletes notification entry from chain.
  397. * @pm_qos_class: identifies which qos target changes are notified.
  398. * @notifier: notifier block to be removed.
  399. *
  400. * will remove the notifier from the notification chain that gets called
  401. * upon changes to the pm_qos_class target value.
  402. */
  403. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  404. {
  405. int retval;
  406. retval = blocking_notifier_chain_unregister(
  407. pm_qos_array[pm_qos_class]->constraints->notifiers,
  408. notifier);
  409. return retval;
  410. }
  411. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  412. /* User space interface to PM QoS classes via misc devices */
  413. static int register_pm_qos_misc(struct pm_qos_object *qos)
  414. {
  415. qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
  416. qos->pm_qos_power_miscdev.name = qos->name;
  417. qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
  418. return misc_register(&qos->pm_qos_power_miscdev);
  419. }
  420. static int find_pm_qos_object_by_minor(int minor)
  421. {
  422. int pm_qos_class;
  423. for (pm_qos_class = PM_QOS_CPU_DMA_LATENCY;
  424. pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
  425. if (minor ==
  426. pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
  427. return pm_qos_class;
  428. }
  429. return -1;
  430. }
  431. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  432. {
  433. long pm_qos_class;
  434. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  435. if (pm_qos_class >= PM_QOS_CPU_DMA_LATENCY) {
  436. struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL);
  437. if (!req)
  438. return -ENOMEM;
  439. pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
  440. filp->private_data = req;
  441. return 0;
  442. }
  443. return -EPERM;
  444. }
  445. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  446. {
  447. struct pm_qos_request *req;
  448. req = filp->private_data;
  449. pm_qos_remove_request(req);
  450. kfree(req);
  451. return 0;
  452. }
  453. static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
  454. size_t count, loff_t *f_pos)
  455. {
  456. s32 value;
  457. unsigned long flags;
  458. struct pm_qos_request *req = filp->private_data;
  459. if (!req)
  460. return -EINVAL;
  461. if (!pm_qos_request_active(req))
  462. return -EINVAL;
  463. spin_lock_irqsave(&pm_qos_lock, flags);
  464. value = pm_qos_get_value(pm_qos_array[req->pm_qos_class]->constraints);
  465. spin_unlock_irqrestore(&pm_qos_lock, flags);
  466. return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
  467. }
  468. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  469. size_t count, loff_t *f_pos)
  470. {
  471. s32 value;
  472. struct pm_qos_request *req;
  473. if (count == sizeof(s32)) {
  474. if (copy_from_user(&value, buf, sizeof(s32)))
  475. return -EFAULT;
  476. } else if (count <= 11) { /* ASCII perhaps? */
  477. char ascii_value[11];
  478. unsigned long int ulval;
  479. int ret;
  480. if (copy_from_user(ascii_value, buf, count))
  481. return -EFAULT;
  482. if (count > 10) {
  483. if (ascii_value[10] == '\n')
  484. ascii_value[10] = '\0';
  485. else
  486. return -EINVAL;
  487. } else {
  488. ascii_value[count] = '\0';
  489. }
  490. ret = kstrtoul(ascii_value, 16, &ulval);
  491. if (ret) {
  492. pr_debug("%s, 0x%lx, 0x%x\n", ascii_value, ulval, ret);
  493. return -EINVAL;
  494. }
  495. value = (s32)lower_32_bits(ulval);
  496. } else {
  497. return -EINVAL;
  498. }
  499. req = filp->private_data;
  500. pm_qos_update_request(req, value);
  501. return count;
  502. }
  503. static int __init pm_qos_power_init(void)
  504. {
  505. int ret = 0;
  506. int i;
  507. BUILD_BUG_ON(ARRAY_SIZE(pm_qos_array) != PM_QOS_NUM_CLASSES);
  508. for (i = PM_QOS_CPU_DMA_LATENCY; i < PM_QOS_NUM_CLASSES; i++) {
  509. ret = register_pm_qos_misc(pm_qos_array[i]);
  510. if (ret < 0) {
  511. printk(KERN_ERR "pm_qos_param: %s setup failed\n",
  512. pm_qos_array[i]->name);
  513. return ret;
  514. }
  515. }
  516. return ret;
  517. }
  518. late_initcall(pm_qos_power_init);