qos.c 19 KB

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