qos.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 int pm_qos_dbg_show_requests(struct seq_file *s, void *unused)
  162. {
  163. struct pm_qos_object *qos = (struct pm_qos_object *)s->private;
  164. struct pm_qos_constraints *c;
  165. struct pm_qos_request *req;
  166. char *type;
  167. unsigned long flags;
  168. int tot_reqs = 0;
  169. int active_reqs = 0;
  170. if (IS_ERR_OR_NULL(qos)) {
  171. pr_err("%s: bad qos param!\n", __func__);
  172. return -EINVAL;
  173. }
  174. c = qos->constraints;
  175. if (IS_ERR_OR_NULL(c)) {
  176. pr_err("%s: Bad constraints on qos?\n", __func__);
  177. return -EINVAL;
  178. }
  179. /* Lock to ensure we have a snapshot */
  180. spin_lock_irqsave(&pm_qos_lock, flags);
  181. if (plist_head_empty(&c->list)) {
  182. seq_puts(s, "Empty!\n");
  183. goto out;
  184. }
  185. switch (c->type) {
  186. case PM_QOS_MIN:
  187. type = "Minimum";
  188. break;
  189. case PM_QOS_MAX:
  190. type = "Maximum";
  191. break;
  192. case PM_QOS_SUM:
  193. type = "Sum";
  194. break;
  195. default:
  196. type = "Unknown";
  197. }
  198. plist_for_each_entry(req, &c->list, node) {
  199. char *state = "Default";
  200. if ((req->node).prio != c->default_value) {
  201. active_reqs++;
  202. state = "Active";
  203. }
  204. tot_reqs++;
  205. seq_printf(s, "%d: %d: %s\n", tot_reqs,
  206. (req->node).prio, state);
  207. }
  208. seq_printf(s, "Type=%s, Value=%d, Requests: active=%d / total=%d\n",
  209. type, pm_qos_get_value(c), active_reqs, tot_reqs);
  210. out:
  211. spin_unlock_irqrestore(&pm_qos_lock, flags);
  212. return 0;
  213. }
  214. static int pm_qos_dbg_open(struct inode *inode, struct file *file)
  215. {
  216. return single_open(file, pm_qos_dbg_show_requests,
  217. inode->i_private);
  218. }
  219. static const struct file_operations pm_qos_debug_fops = {
  220. .open = pm_qos_dbg_open,
  221. .read = seq_read,
  222. .llseek = seq_lseek,
  223. .release = single_release,
  224. };
  225. /**
  226. * pm_qos_update_target - manages the constraints list and calls the notifiers
  227. * if needed
  228. * @c: constraints data struct
  229. * @node: request to add to the list, to update or to remove
  230. * @action: action to take on the constraints list
  231. * @value: value of the request to add or update
  232. *
  233. * This function returns 1 if the aggregated constraint value has changed, 0
  234. * otherwise.
  235. */
  236. int pm_qos_update_target(struct pm_qos_constraints *c, struct plist_node *node,
  237. enum pm_qos_req_action action, int value)
  238. {
  239. unsigned long flags;
  240. int prev_value, curr_value, new_value;
  241. int ret;
  242. spin_lock_irqsave(&pm_qos_lock, flags);
  243. prev_value = pm_qos_get_value(c);
  244. if (value == PM_QOS_DEFAULT_VALUE)
  245. new_value = c->default_value;
  246. else
  247. new_value = value;
  248. switch (action) {
  249. case PM_QOS_REMOVE_REQ:
  250. plist_del(node, &c->list);
  251. break;
  252. case PM_QOS_UPDATE_REQ:
  253. /*
  254. * to change the list, we atomically remove, reinit
  255. * with new value and add, then see if the extremal
  256. * changed
  257. */
  258. plist_del(node, &c->list);
  259. /* fall through */
  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. /* fall through */
  323. case PM_QOS_ADD_REQ:
  324. req->flags = val;
  325. INIT_LIST_HEAD(&req->node);
  326. list_add_tail(&req->node, &pqf->list);
  327. pqf->effective_flags |= val;
  328. break;
  329. default:
  330. /* no action */
  331. ;
  332. }
  333. curr_value = list_empty(&pqf->list) ? 0 : pqf->effective_flags;
  334. spin_unlock_irqrestore(&pm_qos_lock, irqflags);
  335. trace_pm_qos_update_flags(action, prev_value, curr_value);
  336. return prev_value != curr_value;
  337. }
  338. /**
  339. * pm_qos_request - returns current system wide qos expectation
  340. * @pm_qos_class: identification of which qos value is requested
  341. *
  342. * This function returns the current target value.
  343. */
  344. int pm_qos_request(int pm_qos_class)
  345. {
  346. return pm_qos_read_value(pm_qos_array[pm_qos_class]->constraints);
  347. }
  348. EXPORT_SYMBOL_GPL(pm_qos_request);
  349. int pm_qos_request_active(struct pm_qos_request *req)
  350. {
  351. return req->pm_qos_class != 0;
  352. }
  353. EXPORT_SYMBOL_GPL(pm_qos_request_active);
  354. static void __pm_qos_update_request(struct pm_qos_request *req,
  355. s32 new_value)
  356. {
  357. trace_pm_qos_update_request(req->pm_qos_class, new_value);
  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. }
  363. /**
  364. * pm_qos_work_fn - the timeout handler of pm_qos_update_request_timeout
  365. * @work: work struct for the delayed work (timeout)
  366. *
  367. * This cancels the timeout request by falling back to the default at timeout.
  368. */
  369. static void pm_qos_work_fn(struct work_struct *work)
  370. {
  371. struct pm_qos_request *req = container_of(to_delayed_work(work),
  372. struct pm_qos_request,
  373. work);
  374. __pm_qos_update_request(req, PM_QOS_DEFAULT_VALUE);
  375. }
  376. /**
  377. * pm_qos_add_request - inserts new qos request into the list
  378. * @req: pointer to a preallocated handle
  379. * @pm_qos_class: identifies which list of qos request to use
  380. * @value: defines the qos request
  381. *
  382. * This function inserts a new entry in the pm_qos_class list of requested qos
  383. * performance characteristics. It recomputes the aggregate QoS expectations
  384. * for the pm_qos_class of parameters and initializes the pm_qos_request
  385. * handle. Caller needs to save this handle for later use in updates and
  386. * removal.
  387. */
  388. void pm_qos_add_request(struct pm_qos_request *req,
  389. int pm_qos_class, s32 value)
  390. {
  391. if (!req) /*guard against callers passing in null */
  392. return;
  393. if (pm_qos_request_active(req)) {
  394. WARN(1, KERN_ERR "pm_qos_add_request() called for already added request\n");
  395. return;
  396. }
  397. req->pm_qos_class = pm_qos_class;
  398. INIT_DELAYED_WORK(&req->work, pm_qos_work_fn);
  399. trace_pm_qos_add_request(pm_qos_class, value);
  400. pm_qos_update_target(pm_qos_array[pm_qos_class]->constraints,
  401. &req->node, PM_QOS_ADD_REQ, value);
  402. }
  403. EXPORT_SYMBOL_GPL(pm_qos_add_request);
  404. /**
  405. * pm_qos_update_request - modifies an existing qos request
  406. * @req : handle to list element holding a pm_qos request to use
  407. * @value: defines the qos request
  408. *
  409. * Updates an existing qos request for the pm_qos_class of parameters along
  410. * with updating the target pm_qos_class value.
  411. *
  412. * Attempts are made to make this code callable on hot code paths.
  413. */
  414. void pm_qos_update_request(struct pm_qos_request *req,
  415. s32 new_value)
  416. {
  417. if (!req) /*guard against callers passing in null */
  418. return;
  419. if (!pm_qos_request_active(req)) {
  420. WARN(1, KERN_ERR "pm_qos_update_request() called for unknown object\n");
  421. return;
  422. }
  423. cancel_delayed_work_sync(&req->work);
  424. __pm_qos_update_request(req, new_value);
  425. }
  426. EXPORT_SYMBOL_GPL(pm_qos_update_request);
  427. /**
  428. * pm_qos_update_request_timeout - modifies an existing qos request temporarily.
  429. * @req : handle to list element holding a pm_qos request to use
  430. * @new_value: defines the temporal qos request
  431. * @timeout_us: the effective duration of this qos request in usecs.
  432. *
  433. * After timeout_us, this qos request is cancelled automatically.
  434. */
  435. void pm_qos_update_request_timeout(struct pm_qos_request *req, s32 new_value,
  436. unsigned long timeout_us)
  437. {
  438. if (!req)
  439. return;
  440. if (WARN(!pm_qos_request_active(req),
  441. "%s called for unknown object.", __func__))
  442. return;
  443. cancel_delayed_work_sync(&req->work);
  444. trace_pm_qos_update_request_timeout(req->pm_qos_class,
  445. new_value, timeout_us);
  446. if (new_value != req->node.prio)
  447. pm_qos_update_target(
  448. pm_qos_array[req->pm_qos_class]->constraints,
  449. &req->node, PM_QOS_UPDATE_REQ, new_value);
  450. schedule_delayed_work(&req->work, usecs_to_jiffies(timeout_us));
  451. }
  452. /**
  453. * pm_qos_remove_request - modifies an existing qos request
  454. * @req: handle to request list element
  455. *
  456. * Will remove pm qos request from the list of constraints and
  457. * recompute the current target value for the pm_qos_class. Call this
  458. * on slow code paths.
  459. */
  460. void pm_qos_remove_request(struct pm_qos_request *req)
  461. {
  462. if (!req) /*guard against callers passing in null */
  463. return;
  464. /* silent return to keep pcm code cleaner */
  465. if (!pm_qos_request_active(req)) {
  466. WARN(1, KERN_ERR "pm_qos_remove_request() called for unknown object\n");
  467. return;
  468. }
  469. cancel_delayed_work_sync(&req->work);
  470. trace_pm_qos_remove_request(req->pm_qos_class, PM_QOS_DEFAULT_VALUE);
  471. pm_qos_update_target(pm_qos_array[req->pm_qos_class]->constraints,
  472. &req->node, PM_QOS_REMOVE_REQ,
  473. PM_QOS_DEFAULT_VALUE);
  474. memset(req, 0, sizeof(*req));
  475. }
  476. EXPORT_SYMBOL_GPL(pm_qos_remove_request);
  477. /**
  478. * pm_qos_add_notifier - sets notification entry for changes to target value
  479. * @pm_qos_class: identifies which qos target changes should be notified.
  480. * @notifier: notifier block managed by caller.
  481. *
  482. * will register the notifier into a notification chain that gets called
  483. * upon changes to the pm_qos_class target value.
  484. */
  485. int pm_qos_add_notifier(int pm_qos_class, struct notifier_block *notifier)
  486. {
  487. int retval;
  488. retval = blocking_notifier_chain_register(
  489. pm_qos_array[pm_qos_class]->constraints->notifiers,
  490. notifier);
  491. return retval;
  492. }
  493. EXPORT_SYMBOL_GPL(pm_qos_add_notifier);
  494. /**
  495. * pm_qos_remove_notifier - deletes notification entry from chain.
  496. * @pm_qos_class: identifies which qos target changes are notified.
  497. * @notifier: notifier block to be removed.
  498. *
  499. * will remove the notifier from the notification chain that gets called
  500. * upon changes to the pm_qos_class target value.
  501. */
  502. int pm_qos_remove_notifier(int pm_qos_class, struct notifier_block *notifier)
  503. {
  504. int retval;
  505. retval = blocking_notifier_chain_unregister(
  506. pm_qos_array[pm_qos_class]->constraints->notifiers,
  507. notifier);
  508. return retval;
  509. }
  510. EXPORT_SYMBOL_GPL(pm_qos_remove_notifier);
  511. /* User space interface to PM QoS classes via misc devices */
  512. static int register_pm_qos_misc(struct pm_qos_object *qos, struct dentry *d)
  513. {
  514. qos->pm_qos_power_miscdev.minor = MISC_DYNAMIC_MINOR;
  515. qos->pm_qos_power_miscdev.name = qos->name;
  516. qos->pm_qos_power_miscdev.fops = &pm_qos_power_fops;
  517. if (d) {
  518. (void)debugfs_create_file(qos->name, S_IRUGO, d,
  519. (void *)qos, &pm_qos_debug_fops);
  520. }
  521. return misc_register(&qos->pm_qos_power_miscdev);
  522. }
  523. static int find_pm_qos_object_by_minor(int minor)
  524. {
  525. int pm_qos_class;
  526. for (pm_qos_class = PM_QOS_CPU_DMA_LATENCY;
  527. pm_qos_class < PM_QOS_NUM_CLASSES; pm_qos_class++) {
  528. if (minor ==
  529. pm_qos_array[pm_qos_class]->pm_qos_power_miscdev.minor)
  530. return pm_qos_class;
  531. }
  532. return -1;
  533. }
  534. static int pm_qos_power_open(struct inode *inode, struct file *filp)
  535. {
  536. long pm_qos_class;
  537. pm_qos_class = find_pm_qos_object_by_minor(iminor(inode));
  538. if (pm_qos_class >= PM_QOS_CPU_DMA_LATENCY) {
  539. struct pm_qos_request *req = kzalloc(sizeof(*req), GFP_KERNEL);
  540. if (!req)
  541. return -ENOMEM;
  542. pm_qos_add_request(req, pm_qos_class, PM_QOS_DEFAULT_VALUE);
  543. filp->private_data = req;
  544. return 0;
  545. }
  546. return -EPERM;
  547. }
  548. static int pm_qos_power_release(struct inode *inode, struct file *filp)
  549. {
  550. struct pm_qos_request *req;
  551. req = filp->private_data;
  552. pm_qos_remove_request(req);
  553. kfree(req);
  554. return 0;
  555. }
  556. static ssize_t pm_qos_power_read(struct file *filp, char __user *buf,
  557. size_t count, loff_t *f_pos)
  558. {
  559. s32 value;
  560. unsigned long flags;
  561. struct pm_qos_request *req = filp->private_data;
  562. if (!req)
  563. return -EINVAL;
  564. if (!pm_qos_request_active(req))
  565. return -EINVAL;
  566. spin_lock_irqsave(&pm_qos_lock, flags);
  567. value = pm_qos_get_value(pm_qos_array[req->pm_qos_class]->constraints);
  568. spin_unlock_irqrestore(&pm_qos_lock, flags);
  569. return simple_read_from_buffer(buf, count, f_pos, &value, sizeof(s32));
  570. }
  571. static ssize_t pm_qos_power_write(struct file *filp, const char __user *buf,
  572. size_t count, loff_t *f_pos)
  573. {
  574. s32 value;
  575. struct pm_qos_request *req;
  576. if (count == sizeof(s32)) {
  577. if (copy_from_user(&value, buf, sizeof(s32)))
  578. return -EFAULT;
  579. } else {
  580. int ret;
  581. ret = kstrtos32_from_user(buf, count, 16, &value);
  582. if (ret)
  583. return ret;
  584. }
  585. req = filp->private_data;
  586. pm_qos_update_request(req, value);
  587. return count;
  588. }
  589. static int __init pm_qos_power_init(void)
  590. {
  591. int ret = 0;
  592. int i;
  593. struct dentry *d;
  594. BUILD_BUG_ON(ARRAY_SIZE(pm_qos_array) != PM_QOS_NUM_CLASSES);
  595. d = debugfs_create_dir("pm_qos", NULL);
  596. if (IS_ERR_OR_NULL(d))
  597. d = NULL;
  598. for (i = PM_QOS_CPU_DMA_LATENCY; i < PM_QOS_NUM_CLASSES; i++) {
  599. ret = register_pm_qos_misc(pm_qos_array[i], d);
  600. if (ret < 0) {
  601. pr_err("%s: %s setup failed\n",
  602. __func__, pm_qos_array[i]->name);
  603. return ret;
  604. }
  605. }
  606. return ret;
  607. }
  608. late_initcall(pm_qos_power_init);