ipa_rm.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* Copyright (c) 2013, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/workqueue.h>
  14. #include <mach/ipa.h>
  15. #include "ipa_i.h"
  16. #include "ipa_rm_dependency_graph.h"
  17. #include "ipa_rm_i.h"
  18. #include "ipa_rm_resource.h"
  19. struct ipa_rm_context_type {
  20. struct ipa_rm_dep_graph *dep_graph;
  21. struct workqueue_struct *ipa_rm_wq;
  22. rwlock_t lock;
  23. };
  24. static struct ipa_rm_context_type *ipa_rm_ctx;
  25. /**
  26. * ipa_rm_create_resource() - create resource
  27. * @create_params: [in] parameters needed
  28. * for resource initialization
  29. *
  30. * Returns: 0 on success, negative on failure
  31. *
  32. * This function is called by IPA RM client to initialize client's resources.
  33. * This API should be called before any other IPA RM API
  34. * on given resource name.
  35. *
  36. */
  37. int ipa_rm_create_resource(struct ipa_rm_create_params *create_params)
  38. {
  39. struct ipa_rm_resource *resource;
  40. int result;
  41. if (!create_params)
  42. return -EINVAL;
  43. write_lock(&ipa_rm_ctx->lock);
  44. if (ipa_rm_dep_graph_get_resource(ipa_rm_ctx->dep_graph,
  45. create_params->name,
  46. &resource) == 0) {
  47. result = -EEXIST;
  48. goto bail;
  49. }
  50. result = ipa_rm_resource_create(create_params,
  51. &resource);
  52. if (result)
  53. goto bail;
  54. result = ipa_rm_dep_graph_add(ipa_rm_ctx->dep_graph, resource);
  55. if (result)
  56. ipa_rm_resource_delete(resource);
  57. bail:
  58. write_unlock(&ipa_rm_ctx->lock);
  59. return result;
  60. }
  61. EXPORT_SYMBOL(ipa_rm_create_resource);
  62. /**
  63. * ipa_rm_delete_resource() - delete resource
  64. * @resource_name: name of resource to be deleted
  65. *
  66. * Returns: 0 on success, negative on failure
  67. *
  68. * This function is called by IPA RM client to delete client's resources.
  69. *
  70. */
  71. int ipa_rm_delete_resource(enum ipa_rm_resource_name resource_name)
  72. {
  73. struct ipa_rm_resource *resource;
  74. int result;
  75. IPADBG("IPA RM ::ipa_rm_delete_resource num[%d] ENTER\n",
  76. resource_name);
  77. write_lock(&ipa_rm_ctx->lock);
  78. if (ipa_rm_dep_graph_get_resource(ipa_rm_ctx->dep_graph,
  79. resource_name,
  80. &resource) != 0) {
  81. IPADBG("ipa_rm_delete_resource param are bad********\n");
  82. result = -EINVAL;
  83. goto bail;
  84. }
  85. result = ipa_rm_resource_delete(resource);
  86. if (result) {
  87. IPADBG("error in ipa_rm_resource_delete\n");
  88. goto bail;
  89. }
  90. result = ipa_rm_dep_graph_remove(ipa_rm_ctx->dep_graph,
  91. resource_name);
  92. IPADBG("IPA RM ::ipa_rm_delete_resource [%d] SUCCESS\n",
  93. resource_name);
  94. bail:
  95. write_unlock(&ipa_rm_ctx->lock);
  96. return result;
  97. }
  98. EXPORT_SYMBOL(ipa_rm_delete_resource);
  99. /**
  100. * ipa_rm_add_dependency() - create dependency
  101. * between 2 resources
  102. * @resource_name: name of dependent resource
  103. * @depends_on_name: name of its dependency
  104. *
  105. * Returns: 0 on success, negative on failure
  106. *
  107. * Side effects: IPA_RM_RESORCE_GRANTED could be generated
  108. * in case client registered with IPA RM
  109. */
  110. int ipa_rm_add_dependency(enum ipa_rm_resource_name resource_name,
  111. enum ipa_rm_resource_name depends_on_name)
  112. {
  113. int result;
  114. write_lock(&ipa_rm_ctx->lock);
  115. result = ipa_rm_dep_graph_add_dependency(
  116. ipa_rm_ctx->dep_graph,
  117. resource_name,
  118. depends_on_name);
  119. write_unlock(&ipa_rm_ctx->lock);
  120. return result;
  121. }
  122. EXPORT_SYMBOL(ipa_rm_add_dependency);
  123. /**
  124. * ipa_rm_delete_dependency() - create dependency
  125. * between 2 resources
  126. * @resource_name: name of dependent resource
  127. * @depends_on_name: name of its dependency
  128. *
  129. * Returns: 0 on success, negative on failure
  130. *
  131. * Side effects: IPA_RM_RESORCE_GRANTED could be generated
  132. * in case client registered with IPA RM
  133. */
  134. int ipa_rm_delete_dependency(enum ipa_rm_resource_name resource_name,
  135. enum ipa_rm_resource_name depends_on_name)
  136. {
  137. int result;
  138. write_lock(&ipa_rm_ctx->lock);
  139. result = ipa_rm_dep_graph_delete_dependency(
  140. ipa_rm_ctx->dep_graph,
  141. resource_name,
  142. depends_on_name);
  143. write_unlock(&ipa_rm_ctx->lock);
  144. return result;
  145. }
  146. EXPORT_SYMBOL(ipa_rm_delete_dependency);
  147. /**
  148. * ipa_rm_request_resource() - request resource
  149. * @resource_name: [in] name of the requested resource
  150. *
  151. * Returns: 0 on success, negative on failure
  152. *
  153. * All registered callbacks are called with IPA_RM_RESOURCE_GRANTED
  154. * on successful completion of this operation.
  155. */
  156. int ipa_rm_request_resource(enum ipa_rm_resource_name resource_name)
  157. {
  158. struct ipa_rm_resource *resource;
  159. int result;
  160. IPADBG("IPA RM ::ipa_rm_request_resource ENTER\n");
  161. if (!IPA_RM_RESORCE_IS_PROD(resource_name))
  162. return -EINVAL;
  163. read_lock(&ipa_rm_ctx->lock);
  164. if (ipa_rm_dep_graph_get_resource(ipa_rm_ctx->dep_graph,
  165. resource_name,
  166. &resource) != 0) {
  167. result = -EPERM;
  168. goto bail;
  169. }
  170. result = ipa_rm_resource_producer_request(
  171. (struct ipa_rm_resource_prod *)resource);
  172. bail:
  173. IPADBG("IPA RM ::ipa_rm_request_resource EXIT [%d]\n", result);
  174. read_unlock(&ipa_rm_ctx->lock);
  175. return result;
  176. }
  177. EXPORT_SYMBOL(ipa_rm_request_resource);
  178. /**
  179. * ipa_rm_release_resource() - release resource
  180. * @resource_name: [in] name of the requested resource
  181. *
  182. * Returns: 0 on success, negative on failure
  183. *
  184. * All registered callbacks are called with IPA_RM_RESOURCE_RELEASED
  185. * on successful completion of this operation.
  186. */
  187. int ipa_rm_release_resource(enum ipa_rm_resource_name resource_name)
  188. {
  189. struct ipa_rm_resource *resource;
  190. int result;
  191. IPADBG("IPA RM ::ipa_rm_release_resource ENTER\n");
  192. if (!IPA_RM_RESORCE_IS_PROD(resource_name))
  193. return -EINVAL;
  194. read_lock(&ipa_rm_ctx->lock);
  195. if (ipa_rm_dep_graph_get_resource(ipa_rm_ctx->dep_graph,
  196. resource_name,
  197. &resource) != 0) {
  198. result = -EPERM;
  199. goto bail;
  200. }
  201. result = ipa_rm_resource_producer_release(
  202. (struct ipa_rm_resource_prod *)resource);
  203. bail:
  204. IPADBG("IPA RM ::ipa_rm_release_resource EXIT [%d]\n", result);
  205. read_unlock(&ipa_rm_ctx->lock);
  206. return result;
  207. }
  208. EXPORT_SYMBOL(ipa_rm_release_resource);
  209. /**
  210. * ipa_rm_register() - register for event
  211. * @resource_name: resource name
  212. * @reg_params: [in] registration parameters
  213. *
  214. * Returns: 0 on success, negative on failure
  215. *
  216. * Registration parameters provided here should be the same
  217. * as provided later in ipa_rm_deregister() call.
  218. */
  219. int ipa_rm_register(enum ipa_rm_resource_name resource_name,
  220. struct ipa_rm_register_params *reg_params)
  221. {
  222. int result;
  223. struct ipa_rm_resource *resource;
  224. if (!IPA_RM_RESORCE_IS_PROD(resource_name))
  225. return -EINVAL;
  226. read_lock(&ipa_rm_ctx->lock);
  227. if (ipa_rm_dep_graph_get_resource(ipa_rm_ctx->dep_graph,
  228. resource_name,
  229. &resource) != 0) {
  230. result = -EPERM;
  231. goto bail;
  232. }
  233. result = ipa_rm_resource_producer_register(
  234. (struct ipa_rm_resource_prod *)resource,
  235. reg_params,
  236. true);
  237. bail:
  238. read_unlock(&ipa_rm_ctx->lock);
  239. return result;
  240. }
  241. EXPORT_SYMBOL(ipa_rm_register);
  242. /**
  243. * ipa_rm_deregister() - cancel the registration
  244. * @resource_name: resource name
  245. * @reg_params: [in] registration parameters
  246. *
  247. * Returns: 0 on success, negative on failure
  248. *
  249. * Registration parameters provided here should be the same
  250. * as provided in ipa_rm_register() call.
  251. */
  252. int ipa_rm_deregister(enum ipa_rm_resource_name resource_name,
  253. struct ipa_rm_register_params *reg_params)
  254. {
  255. int result;
  256. struct ipa_rm_resource *resource;
  257. if (!IPA_RM_RESORCE_IS_PROD(resource_name))
  258. return -EINVAL;
  259. read_lock(&ipa_rm_ctx->lock);
  260. if (ipa_rm_dep_graph_get_resource(ipa_rm_ctx->dep_graph,
  261. resource_name,
  262. &resource) != 0) {
  263. result = -EPERM;
  264. goto bail;
  265. }
  266. result = ipa_rm_resource_producer_deregister(
  267. (struct ipa_rm_resource_prod *)resource,
  268. reg_params);
  269. bail:
  270. read_unlock(&ipa_rm_ctx->lock);
  271. return result;
  272. }
  273. EXPORT_SYMBOL(ipa_rm_deregister);
  274. /**
  275. * ipa_rm_notify_completion() -
  276. * consumer driver notification for
  277. * request_resource / release_resource operations
  278. * completion
  279. * @event: notified event
  280. * @resource_name: resource name
  281. *
  282. * Returns: 0 on success, negative on failure
  283. */
  284. int ipa_rm_notify_completion(enum ipa_rm_event event,
  285. enum ipa_rm_resource_name resource_name)
  286. {
  287. int result;
  288. if (!IPA_RM_RESORCE_IS_CONS(resource_name)) {
  289. result = -EINVAL;
  290. goto bail;
  291. }
  292. ipa_rm_wq_send_cmd(IPA_RM_WQ_RESOURCE_CB,
  293. resource_name,
  294. event,
  295. false);
  296. result = 0;
  297. bail:
  298. return result;
  299. }
  300. EXPORT_SYMBOL(ipa_rm_notify_completion);
  301. static void ipa_rm_wq_handler(struct work_struct *work)
  302. {
  303. struct ipa_rm_resource *resource;
  304. struct ipa_rm_wq_work_type *ipa_rm_work =
  305. container_of(work,
  306. struct ipa_rm_wq_work_type,
  307. work);
  308. switch (ipa_rm_work->wq_cmd) {
  309. case IPA_RM_WQ_NOTIFY_PROD:
  310. if (!IPA_RM_RESORCE_IS_PROD(ipa_rm_work->resource_name))
  311. return;
  312. read_lock(&ipa_rm_ctx->lock);
  313. if (ipa_rm_dep_graph_get_resource(ipa_rm_ctx->dep_graph,
  314. ipa_rm_work->resource_name,
  315. &resource) != 0){
  316. read_unlock(&ipa_rm_ctx->lock);
  317. return;
  318. }
  319. ipa_rm_resource_producer_notify_clients(
  320. (struct ipa_rm_resource_prod *)resource,
  321. ipa_rm_work->event,
  322. ipa_rm_work->notify_registered_only);
  323. read_unlock(&ipa_rm_ctx->lock);
  324. break;
  325. case IPA_RM_WQ_NOTIFY_CONS:
  326. break;
  327. case IPA_RM_WQ_RESOURCE_CB:
  328. read_lock(&ipa_rm_ctx->lock);
  329. if (ipa_rm_dep_graph_get_resource(ipa_rm_ctx->dep_graph,
  330. ipa_rm_work->resource_name,
  331. &resource) != 0){
  332. read_unlock(&ipa_rm_ctx->lock);
  333. return;
  334. }
  335. ipa_rm_resource_consumer_handle_cb(
  336. (struct ipa_rm_resource_cons *)resource,
  337. ipa_rm_work->event);
  338. read_unlock(&ipa_rm_ctx->lock);
  339. break;
  340. default:
  341. break;
  342. }
  343. kfree((void *) work);
  344. }
  345. /**
  346. * ipa_rm_wq_send_cmd() - send a command for deferred work
  347. * @wq_cmd: command that should be executed
  348. * @resource_name: resource on which command should be executed
  349. * @notify_registered_only: notify only clients registered by
  350. * ipa_rm_register()
  351. *
  352. * Returns: 0 on success, negative otherwise
  353. */
  354. int ipa_rm_wq_send_cmd(enum ipa_rm_wq_cmd wq_cmd,
  355. enum ipa_rm_resource_name resource_name,
  356. enum ipa_rm_event event,
  357. bool notify_registered_only)
  358. {
  359. int result = -ENOMEM;
  360. struct ipa_rm_wq_work_type *work = kzalloc(sizeof(*work), GFP_KERNEL);
  361. if (work) {
  362. INIT_WORK((struct work_struct *)work, ipa_rm_wq_handler);
  363. work->wq_cmd = wq_cmd;
  364. work->resource_name = resource_name;
  365. work->event = event;
  366. work->notify_registered_only = notify_registered_only;
  367. result = queue_work(ipa_rm_ctx->ipa_rm_wq,
  368. (struct work_struct *)work);
  369. }
  370. return result;
  371. }
  372. /**
  373. * ipa_rm_initialize() - initialize IPA RM component
  374. *
  375. * Returns: 0 on success, negative otherwise
  376. */
  377. int ipa_rm_initialize(void)
  378. {
  379. int result;
  380. ipa_rm_ctx = kzalloc(sizeof(*ipa_rm_ctx), GFP_KERNEL);
  381. if (!ipa_rm_ctx) {
  382. result = -ENOMEM;
  383. goto bail;
  384. }
  385. ipa_rm_ctx->ipa_rm_wq = create_singlethread_workqueue("ipa_rm_wq");
  386. if (!ipa_rm_ctx->ipa_rm_wq) {
  387. result = -ENOMEM;
  388. goto create_wq_fail;
  389. }
  390. result = ipa_rm_dep_graph_create(&(ipa_rm_ctx->dep_graph));
  391. if (result)
  392. goto graph_alloc_fail;
  393. rwlock_init(&ipa_rm_ctx->lock);
  394. IPADBG("IPA RM ipa_rm_initialize SUCCESS\n");
  395. return 0;
  396. graph_alloc_fail:
  397. destroy_workqueue(ipa_rm_ctx->ipa_rm_wq);
  398. create_wq_fail:
  399. kfree(ipa_rm_ctx);
  400. bail:
  401. return result;
  402. }
  403. /**
  404. * ipa_rm_stat() - print RM stat
  405. * @buf: [in] The user buff used to print
  406. * @size: [in] The size of buf
  407. * Returns: number of bytes used on success, negative on failure
  408. *
  409. * This function is called by ipa_debugfs in order to receive
  410. * a full picture of the current state of the RM
  411. */
  412. int ipa_rm_stat(char *buf, int size)
  413. {
  414. int i, cnt = 0, result = EINVAL;
  415. struct ipa_rm_resource *resource = NULL;
  416. if (!buf || size < 0)
  417. goto bail;
  418. read_lock(&ipa_rm_ctx->lock);
  419. for (i = 0; i < IPA_RM_RESOURCE_PROD_MAX; ++i) {
  420. result = ipa_rm_dep_graph_get_resource(
  421. ipa_rm_ctx->dep_graph,
  422. i,
  423. &resource);
  424. if (!result) {
  425. result = ipa_rm_resource_producer_print_stat(
  426. resource, buf + cnt,
  427. size-cnt);
  428. if (result < 0)
  429. goto bail;
  430. cnt += result;
  431. }
  432. }
  433. result = cnt;
  434. bail:
  435. read_unlock(&ipa_rm_ctx->lock);
  436. return result;
  437. }
  438. /**
  439. * ipa_rm_exit() - free all IPA RM resources
  440. */
  441. void ipa_rm_exit(void)
  442. {
  443. ipa_rm_dep_graph_delete(ipa_rm_ctx->dep_graph);
  444. destroy_workqueue(ipa_rm_ctx->ipa_rm_wq);
  445. kfree(ipa_rm_ctx);
  446. ipa_rm_ctx = NULL;
  447. }