dprc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright 2013-2016 Freescale Semiconductor Inc.
  4. *
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/fsl/mc.h>
  8. #include "fsl-mc-private.h"
  9. /**
  10. * dprc_open() - Open DPRC object for use
  11. * @mc_io: Pointer to MC portal's I/O object
  12. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  13. * @container_id: Container ID to open
  14. * @token: Returned token of DPRC object
  15. *
  16. * Return: '0' on Success; Error code otherwise.
  17. *
  18. * @warning Required before any operation on the object.
  19. */
  20. int dprc_open(struct fsl_mc_io *mc_io,
  21. u32 cmd_flags,
  22. int container_id,
  23. u16 *token)
  24. {
  25. struct fsl_mc_command cmd = { 0 };
  26. struct dprc_cmd_open *cmd_params;
  27. int err;
  28. /* prepare command */
  29. cmd.header = mc_encode_cmd_header(DPRC_CMDID_OPEN, cmd_flags,
  30. 0);
  31. cmd_params = (struct dprc_cmd_open *)cmd.params;
  32. cmd_params->container_id = cpu_to_le32(container_id);
  33. /* send command to mc*/
  34. err = mc_send_command(mc_io, &cmd);
  35. if (err)
  36. return err;
  37. /* retrieve response parameters */
  38. *token = mc_cmd_hdr_read_token(&cmd);
  39. return 0;
  40. }
  41. EXPORT_SYMBOL_GPL(dprc_open);
  42. /**
  43. * dprc_close() - Close the control session of the object
  44. * @mc_io: Pointer to MC portal's I/O object
  45. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  46. * @token: Token of DPRC object
  47. *
  48. * After this function is called, no further operations are
  49. * allowed on the object without opening a new control session.
  50. *
  51. * Return: '0' on Success; Error code otherwise.
  52. */
  53. int dprc_close(struct fsl_mc_io *mc_io,
  54. u32 cmd_flags,
  55. u16 token)
  56. {
  57. struct fsl_mc_command cmd = { 0 };
  58. /* prepare command */
  59. cmd.header = mc_encode_cmd_header(DPRC_CMDID_CLOSE, cmd_flags,
  60. token);
  61. /* send command to mc*/
  62. return mc_send_command(mc_io, &cmd);
  63. }
  64. EXPORT_SYMBOL_GPL(dprc_close);
  65. /**
  66. * dprc_set_irq() - Set IRQ information for the DPRC to trigger an interrupt.
  67. * @mc_io: Pointer to MC portal's I/O object
  68. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  69. * @token: Token of DPRC object
  70. * @irq_index: Identifies the interrupt index to configure
  71. * @irq_cfg: IRQ configuration
  72. *
  73. * Return: '0' on Success; Error code otherwise.
  74. */
  75. int dprc_set_irq(struct fsl_mc_io *mc_io,
  76. u32 cmd_flags,
  77. u16 token,
  78. u8 irq_index,
  79. struct dprc_irq_cfg *irq_cfg)
  80. {
  81. struct fsl_mc_command cmd = { 0 };
  82. struct dprc_cmd_set_irq *cmd_params;
  83. /* prepare command */
  84. cmd.header = mc_encode_cmd_header(DPRC_CMDID_SET_IRQ,
  85. cmd_flags,
  86. token);
  87. cmd_params = (struct dprc_cmd_set_irq *)cmd.params;
  88. cmd_params->irq_val = cpu_to_le32(irq_cfg->val);
  89. cmd_params->irq_index = irq_index;
  90. cmd_params->irq_addr = cpu_to_le64(irq_cfg->paddr);
  91. cmd_params->irq_num = cpu_to_le32(irq_cfg->irq_num);
  92. /* send command to mc*/
  93. return mc_send_command(mc_io, &cmd);
  94. }
  95. /**
  96. * dprc_set_irq_enable() - Set overall interrupt state.
  97. * @mc_io: Pointer to MC portal's I/O object
  98. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  99. * @token: Token of DPRC object
  100. * @irq_index: The interrupt index to configure
  101. * @en: Interrupt state - enable = 1, disable = 0
  102. *
  103. * Allows GPP software to control when interrupts are generated.
  104. * Each interrupt can have up to 32 causes. The enable/disable control's the
  105. * overall interrupt state. if the interrupt is disabled no causes will cause
  106. * an interrupt.
  107. *
  108. * Return: '0' on Success; Error code otherwise.
  109. */
  110. int dprc_set_irq_enable(struct fsl_mc_io *mc_io,
  111. u32 cmd_flags,
  112. u16 token,
  113. u8 irq_index,
  114. u8 en)
  115. {
  116. struct fsl_mc_command cmd = { 0 };
  117. struct dprc_cmd_set_irq_enable *cmd_params;
  118. /* prepare command */
  119. cmd.header = mc_encode_cmd_header(DPRC_CMDID_SET_IRQ_ENABLE,
  120. cmd_flags, token);
  121. cmd_params = (struct dprc_cmd_set_irq_enable *)cmd.params;
  122. cmd_params->enable = en & DPRC_ENABLE;
  123. cmd_params->irq_index = irq_index;
  124. /* send command to mc*/
  125. return mc_send_command(mc_io, &cmd);
  126. }
  127. /**
  128. * dprc_set_irq_mask() - Set interrupt mask.
  129. * @mc_io: Pointer to MC portal's I/O object
  130. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  131. * @token: Token of DPRC object
  132. * @irq_index: The interrupt index to configure
  133. * @mask: event mask to trigger interrupt;
  134. * each bit:
  135. * 0 = ignore event
  136. * 1 = consider event for asserting irq
  137. *
  138. * Every interrupt can have up to 32 causes and the interrupt model supports
  139. * masking/unmasking each cause independently
  140. *
  141. * Return: '0' on Success; Error code otherwise.
  142. */
  143. int dprc_set_irq_mask(struct fsl_mc_io *mc_io,
  144. u32 cmd_flags,
  145. u16 token,
  146. u8 irq_index,
  147. u32 mask)
  148. {
  149. struct fsl_mc_command cmd = { 0 };
  150. struct dprc_cmd_set_irq_mask *cmd_params;
  151. /* prepare command */
  152. cmd.header = mc_encode_cmd_header(DPRC_CMDID_SET_IRQ_MASK,
  153. cmd_flags, token);
  154. cmd_params = (struct dprc_cmd_set_irq_mask *)cmd.params;
  155. cmd_params->mask = cpu_to_le32(mask);
  156. cmd_params->irq_index = irq_index;
  157. /* send command to mc*/
  158. return mc_send_command(mc_io, &cmd);
  159. }
  160. /**
  161. * dprc_get_irq_status() - Get the current status of any pending interrupts.
  162. * @mc_io: Pointer to MC portal's I/O object
  163. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  164. * @token: Token of DPRC object
  165. * @irq_index: The interrupt index to configure
  166. * @status: Returned interrupts status - one bit per cause:
  167. * 0 = no interrupt pending
  168. * 1 = interrupt pending
  169. *
  170. * Return: '0' on Success; Error code otherwise.
  171. */
  172. int dprc_get_irq_status(struct fsl_mc_io *mc_io,
  173. u32 cmd_flags,
  174. u16 token,
  175. u8 irq_index,
  176. u32 *status)
  177. {
  178. struct fsl_mc_command cmd = { 0 };
  179. struct dprc_cmd_get_irq_status *cmd_params;
  180. struct dprc_rsp_get_irq_status *rsp_params;
  181. int err;
  182. /* prepare command */
  183. cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_IRQ_STATUS,
  184. cmd_flags, token);
  185. cmd_params = (struct dprc_cmd_get_irq_status *)cmd.params;
  186. cmd_params->status = cpu_to_le32(*status);
  187. cmd_params->irq_index = irq_index;
  188. /* send command to mc*/
  189. err = mc_send_command(mc_io, &cmd);
  190. if (err)
  191. return err;
  192. /* retrieve response parameters */
  193. rsp_params = (struct dprc_rsp_get_irq_status *)cmd.params;
  194. *status = le32_to_cpu(rsp_params->status);
  195. return 0;
  196. }
  197. /**
  198. * dprc_clear_irq_status() - Clear a pending interrupt's status
  199. * @mc_io: Pointer to MC portal's I/O object
  200. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  201. * @token: Token of DPRC object
  202. * @irq_index: The interrupt index to configure
  203. * @status: bits to clear (W1C) - one bit per cause:
  204. * 0 = don't change
  205. * 1 = clear status bit
  206. *
  207. * Return: '0' on Success; Error code otherwise.
  208. */
  209. int dprc_clear_irq_status(struct fsl_mc_io *mc_io,
  210. u32 cmd_flags,
  211. u16 token,
  212. u8 irq_index,
  213. u32 status)
  214. {
  215. struct fsl_mc_command cmd = { 0 };
  216. struct dprc_cmd_clear_irq_status *cmd_params;
  217. /* prepare command */
  218. cmd.header = mc_encode_cmd_header(DPRC_CMDID_CLEAR_IRQ_STATUS,
  219. cmd_flags, token);
  220. cmd_params = (struct dprc_cmd_clear_irq_status *)cmd.params;
  221. cmd_params->status = cpu_to_le32(status);
  222. cmd_params->irq_index = irq_index;
  223. /* send command to mc*/
  224. return mc_send_command(mc_io, &cmd);
  225. }
  226. /**
  227. * dprc_get_attributes() - Obtains container attributes
  228. * @mc_io: Pointer to MC portal's I/O object
  229. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  230. * @token: Token of DPRC object
  231. * @attributes Returned container attributes
  232. *
  233. * Return: '0' on Success; Error code otherwise.
  234. */
  235. int dprc_get_attributes(struct fsl_mc_io *mc_io,
  236. u32 cmd_flags,
  237. u16 token,
  238. struct dprc_attributes *attr)
  239. {
  240. struct fsl_mc_command cmd = { 0 };
  241. struct dprc_rsp_get_attributes *rsp_params;
  242. int err;
  243. /* prepare command */
  244. cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_ATTR,
  245. cmd_flags,
  246. token);
  247. /* send command to mc*/
  248. err = mc_send_command(mc_io, &cmd);
  249. if (err)
  250. return err;
  251. /* retrieve response parameters */
  252. rsp_params = (struct dprc_rsp_get_attributes *)cmd.params;
  253. attr->container_id = le32_to_cpu(rsp_params->container_id);
  254. attr->icid = le16_to_cpu(rsp_params->icid);
  255. attr->options = le32_to_cpu(rsp_params->options);
  256. attr->portal_id = le32_to_cpu(rsp_params->portal_id);
  257. return 0;
  258. }
  259. /**
  260. * dprc_get_obj_count() - Obtains the number of objects in the DPRC
  261. * @mc_io: Pointer to MC portal's I/O object
  262. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  263. * @token: Token of DPRC object
  264. * @obj_count: Number of objects assigned to the DPRC
  265. *
  266. * Return: '0' on Success; Error code otherwise.
  267. */
  268. int dprc_get_obj_count(struct fsl_mc_io *mc_io,
  269. u32 cmd_flags,
  270. u16 token,
  271. int *obj_count)
  272. {
  273. struct fsl_mc_command cmd = { 0 };
  274. struct dprc_rsp_get_obj_count *rsp_params;
  275. int err;
  276. /* prepare command */
  277. cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_COUNT,
  278. cmd_flags, token);
  279. /* send command to mc*/
  280. err = mc_send_command(mc_io, &cmd);
  281. if (err)
  282. return err;
  283. /* retrieve response parameters */
  284. rsp_params = (struct dprc_rsp_get_obj_count *)cmd.params;
  285. *obj_count = le32_to_cpu(rsp_params->obj_count);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(dprc_get_obj_count);
  289. /**
  290. * dprc_get_obj() - Get general information on an object
  291. * @mc_io: Pointer to MC portal's I/O object
  292. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  293. * @token: Token of DPRC object
  294. * @obj_index: Index of the object to be queried (< obj_count)
  295. * @obj_desc: Returns the requested object descriptor
  296. *
  297. * The object descriptors are retrieved one by one by incrementing
  298. * obj_index up to (not including) the value of obj_count returned
  299. * from dprc_get_obj_count(). dprc_get_obj_count() must
  300. * be called prior to dprc_get_obj().
  301. *
  302. * Return: '0' on Success; Error code otherwise.
  303. */
  304. int dprc_get_obj(struct fsl_mc_io *mc_io,
  305. u32 cmd_flags,
  306. u16 token,
  307. int obj_index,
  308. struct fsl_mc_obj_desc *obj_desc)
  309. {
  310. struct fsl_mc_command cmd = { 0 };
  311. struct dprc_cmd_get_obj *cmd_params;
  312. struct dprc_rsp_get_obj *rsp_params;
  313. int err;
  314. /* prepare command */
  315. cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_OBJ,
  316. cmd_flags,
  317. token);
  318. cmd_params = (struct dprc_cmd_get_obj *)cmd.params;
  319. cmd_params->obj_index = cpu_to_le32(obj_index);
  320. /* send command to mc*/
  321. err = mc_send_command(mc_io, &cmd);
  322. if (err)
  323. return err;
  324. /* retrieve response parameters */
  325. rsp_params = (struct dprc_rsp_get_obj *)cmd.params;
  326. obj_desc->id = le32_to_cpu(rsp_params->id);
  327. obj_desc->vendor = le16_to_cpu(rsp_params->vendor);
  328. obj_desc->irq_count = rsp_params->irq_count;
  329. obj_desc->region_count = rsp_params->region_count;
  330. obj_desc->state = le32_to_cpu(rsp_params->state);
  331. obj_desc->ver_major = le16_to_cpu(rsp_params->version_major);
  332. obj_desc->ver_minor = le16_to_cpu(rsp_params->version_minor);
  333. obj_desc->flags = le16_to_cpu(rsp_params->flags);
  334. strncpy(obj_desc->type, rsp_params->type, 16);
  335. obj_desc->type[15] = '\0';
  336. strncpy(obj_desc->label, rsp_params->label, 16);
  337. obj_desc->label[15] = '\0';
  338. return 0;
  339. }
  340. EXPORT_SYMBOL_GPL(dprc_get_obj);
  341. /**
  342. * dprc_set_obj_irq() - Set IRQ information for object to trigger an interrupt.
  343. * @mc_io: Pointer to MC portal's I/O object
  344. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  345. * @token: Token of DPRC object
  346. * @obj_type: Type of the object to set its IRQ
  347. * @obj_id: ID of the object to set its IRQ
  348. * @irq_index: The interrupt index to configure
  349. * @irq_cfg: IRQ configuration
  350. *
  351. * Return: '0' on Success; Error code otherwise.
  352. */
  353. int dprc_set_obj_irq(struct fsl_mc_io *mc_io,
  354. u32 cmd_flags,
  355. u16 token,
  356. char *obj_type,
  357. int obj_id,
  358. u8 irq_index,
  359. struct dprc_irq_cfg *irq_cfg)
  360. {
  361. struct fsl_mc_command cmd = { 0 };
  362. struct dprc_cmd_set_obj_irq *cmd_params;
  363. /* prepare command */
  364. cmd.header = mc_encode_cmd_header(DPRC_CMDID_SET_OBJ_IRQ,
  365. cmd_flags,
  366. token);
  367. cmd_params = (struct dprc_cmd_set_obj_irq *)cmd.params;
  368. cmd_params->irq_val = cpu_to_le32(irq_cfg->val);
  369. cmd_params->irq_index = irq_index;
  370. cmd_params->irq_addr = cpu_to_le64(irq_cfg->paddr);
  371. cmd_params->irq_num = cpu_to_le32(irq_cfg->irq_num);
  372. cmd_params->obj_id = cpu_to_le32(obj_id);
  373. strncpy(cmd_params->obj_type, obj_type, 16);
  374. cmd_params->obj_type[15] = '\0';
  375. /* send command to mc*/
  376. return mc_send_command(mc_io, &cmd);
  377. }
  378. EXPORT_SYMBOL_GPL(dprc_set_obj_irq);
  379. /**
  380. * dprc_get_obj_region() - Get region information for a specified object.
  381. * @mc_io: Pointer to MC portal's I/O object
  382. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  383. * @token: Token of DPRC object
  384. * @obj_type; Object type as returned in dprc_get_obj()
  385. * @obj_id: Unique object instance as returned in dprc_get_obj()
  386. * @region_index: The specific region to query
  387. * @region_desc: Returns the requested region descriptor
  388. *
  389. * Return: '0' on Success; Error code otherwise.
  390. */
  391. int dprc_get_obj_region(struct fsl_mc_io *mc_io,
  392. u32 cmd_flags,
  393. u16 token,
  394. char *obj_type,
  395. int obj_id,
  396. u8 region_index,
  397. struct dprc_region_desc *region_desc)
  398. {
  399. struct fsl_mc_command cmd = { 0 };
  400. struct dprc_cmd_get_obj_region *cmd_params;
  401. struct dprc_rsp_get_obj_region *rsp_params;
  402. int err;
  403. /* prepare command */
  404. cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_OBJ_REG,
  405. cmd_flags, token);
  406. cmd_params = (struct dprc_cmd_get_obj_region *)cmd.params;
  407. cmd_params->obj_id = cpu_to_le32(obj_id);
  408. cmd_params->region_index = region_index;
  409. strncpy(cmd_params->obj_type, obj_type, 16);
  410. cmd_params->obj_type[15] = '\0';
  411. /* send command to mc*/
  412. err = mc_send_command(mc_io, &cmd);
  413. if (err)
  414. return err;
  415. /* retrieve response parameters */
  416. rsp_params = (struct dprc_rsp_get_obj_region *)cmd.params;
  417. region_desc->base_offset = le64_to_cpu(rsp_params->base_addr);
  418. region_desc->size = le32_to_cpu(rsp_params->size);
  419. return 0;
  420. }
  421. EXPORT_SYMBOL_GPL(dprc_get_obj_region);
  422. /**
  423. * dprc_get_api_version - Get Data Path Resource Container API version
  424. * @mc_io: Pointer to Mc portal's I/O object
  425. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  426. * @major_ver: Major version of Data Path Resource Container API
  427. * @minor_ver: Minor version of Data Path Resource Container API
  428. *
  429. * Return: '0' on Success; Error code otherwise.
  430. */
  431. int dprc_get_api_version(struct fsl_mc_io *mc_io,
  432. u32 cmd_flags,
  433. u16 *major_ver,
  434. u16 *minor_ver)
  435. {
  436. struct fsl_mc_command cmd = { 0 };
  437. int err;
  438. /* prepare command */
  439. cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_API_VERSION,
  440. cmd_flags, 0);
  441. /* send command to mc */
  442. err = mc_send_command(mc_io, &cmd);
  443. if (err)
  444. return err;
  445. /* retrieve response parameters */
  446. mc_cmd_read_api_version(&cmd, major_ver, minor_ver);
  447. return 0;
  448. }
  449. /**
  450. * dprc_get_container_id - Get container ID associated with a given portal.
  451. * @mc_io: Pointer to Mc portal's I/O object
  452. * @cmd_flags: Command flags; one or more of 'MC_CMD_FLAG_'
  453. * @container_id: Requested container id
  454. *
  455. * Return: '0' on Success; Error code otherwise.
  456. */
  457. int dprc_get_container_id(struct fsl_mc_io *mc_io,
  458. u32 cmd_flags,
  459. int *container_id)
  460. {
  461. struct fsl_mc_command cmd = { 0 };
  462. int err;
  463. /* prepare command */
  464. cmd.header = mc_encode_cmd_header(DPRC_CMDID_GET_CONT_ID,
  465. cmd_flags,
  466. 0);
  467. /* send command to mc*/
  468. err = mc_send_command(mc_io, &cmd);
  469. if (err)
  470. return err;
  471. /* retrieve response parameters */
  472. *container_id = (int)mc_cmd_read_object_id(&cmd);
  473. return 0;
  474. }