mc-io.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
  2. /*
  3. * Copyright 2013-2016 Freescale Semiconductor Inc.
  4. *
  5. */
  6. #include <linux/io.h>
  7. #include <linux/fsl/mc.h>
  8. #include "fsl-mc-private.h"
  9. static int fsl_mc_io_set_dpmcp(struct fsl_mc_io *mc_io,
  10. struct fsl_mc_device *dpmcp_dev)
  11. {
  12. int error;
  13. if (mc_io->dpmcp_dev)
  14. return -EINVAL;
  15. if (dpmcp_dev->mc_io)
  16. return -EINVAL;
  17. error = dpmcp_open(mc_io,
  18. 0,
  19. dpmcp_dev->obj_desc.id,
  20. &dpmcp_dev->mc_handle);
  21. if (error < 0)
  22. return error;
  23. mc_io->dpmcp_dev = dpmcp_dev;
  24. dpmcp_dev->mc_io = mc_io;
  25. return 0;
  26. }
  27. static void fsl_mc_io_unset_dpmcp(struct fsl_mc_io *mc_io)
  28. {
  29. int error;
  30. struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
  31. error = dpmcp_close(mc_io,
  32. 0,
  33. dpmcp_dev->mc_handle);
  34. if (error < 0) {
  35. dev_err(&dpmcp_dev->dev, "dpmcp_close() failed: %d\n",
  36. error);
  37. }
  38. mc_io->dpmcp_dev = NULL;
  39. dpmcp_dev->mc_io = NULL;
  40. }
  41. /**
  42. * Creates an MC I/O object
  43. *
  44. * @dev: device to be associated with the MC I/O object
  45. * @mc_portal_phys_addr: physical address of the MC portal to use
  46. * @mc_portal_size: size in bytes of the MC portal
  47. * @dpmcp-dev: Pointer to the DPMCP object associated with this MC I/O
  48. * object or NULL if none.
  49. * @flags: flags for the new MC I/O object
  50. * @new_mc_io: Area to return pointer to newly created MC I/O object
  51. *
  52. * Returns '0' on Success; Error code otherwise.
  53. */
  54. int __must_check fsl_create_mc_io(struct device *dev,
  55. phys_addr_t mc_portal_phys_addr,
  56. u32 mc_portal_size,
  57. struct fsl_mc_device *dpmcp_dev,
  58. u32 flags, struct fsl_mc_io **new_mc_io)
  59. {
  60. int error;
  61. struct fsl_mc_io *mc_io;
  62. void __iomem *mc_portal_virt_addr;
  63. struct resource *res;
  64. mc_io = devm_kzalloc(dev, sizeof(*mc_io), GFP_KERNEL);
  65. if (!mc_io)
  66. return -ENOMEM;
  67. mc_io->dev = dev;
  68. mc_io->flags = flags;
  69. mc_io->portal_phys_addr = mc_portal_phys_addr;
  70. mc_io->portal_size = mc_portal_size;
  71. if (flags & FSL_MC_IO_ATOMIC_CONTEXT_PORTAL)
  72. spin_lock_init(&mc_io->spinlock);
  73. else
  74. mutex_init(&mc_io->mutex);
  75. res = devm_request_mem_region(dev,
  76. mc_portal_phys_addr,
  77. mc_portal_size,
  78. "mc_portal");
  79. if (!res) {
  80. dev_err(dev,
  81. "devm_request_mem_region failed for MC portal %pa\n",
  82. &mc_portal_phys_addr);
  83. return -EBUSY;
  84. }
  85. mc_portal_virt_addr = devm_ioremap_nocache(dev,
  86. mc_portal_phys_addr,
  87. mc_portal_size);
  88. if (!mc_portal_virt_addr) {
  89. dev_err(dev,
  90. "devm_ioremap_nocache failed for MC portal %pa\n",
  91. &mc_portal_phys_addr);
  92. return -ENXIO;
  93. }
  94. mc_io->portal_virt_addr = mc_portal_virt_addr;
  95. if (dpmcp_dev) {
  96. error = fsl_mc_io_set_dpmcp(mc_io, dpmcp_dev);
  97. if (error < 0)
  98. goto error_destroy_mc_io;
  99. }
  100. *new_mc_io = mc_io;
  101. return 0;
  102. error_destroy_mc_io:
  103. fsl_destroy_mc_io(mc_io);
  104. return error;
  105. }
  106. /**
  107. * Destroys an MC I/O object
  108. *
  109. * @mc_io: MC I/O object to destroy
  110. */
  111. void fsl_destroy_mc_io(struct fsl_mc_io *mc_io)
  112. {
  113. struct fsl_mc_device *dpmcp_dev;
  114. if (!mc_io)
  115. return;
  116. dpmcp_dev = mc_io->dpmcp_dev;
  117. if (dpmcp_dev)
  118. fsl_mc_io_unset_dpmcp(mc_io);
  119. devm_iounmap(mc_io->dev, mc_io->portal_virt_addr);
  120. devm_release_mem_region(mc_io->dev,
  121. mc_io->portal_phys_addr,
  122. mc_io->portal_size);
  123. mc_io->portal_virt_addr = NULL;
  124. devm_kfree(mc_io->dev, mc_io);
  125. }
  126. /**
  127. * fsl_mc_portal_allocate - Allocates an MC portal
  128. *
  129. * @mc_dev: MC device for which the MC portal is to be allocated
  130. * @mc_io_flags: Flags for the fsl_mc_io object that wraps the allocated
  131. * MC portal.
  132. * @new_mc_io: Pointer to area where the pointer to the fsl_mc_io object
  133. * that wraps the allocated MC portal is to be returned
  134. *
  135. * This function allocates an MC portal from the device's parent DPRC,
  136. * from the corresponding MC bus' pool of MC portals and wraps
  137. * it in a new fsl_mc_io object. If 'mc_dev' is a DPRC itself, the
  138. * portal is allocated from its own MC bus.
  139. */
  140. int __must_check fsl_mc_portal_allocate(struct fsl_mc_device *mc_dev,
  141. u16 mc_io_flags,
  142. struct fsl_mc_io **new_mc_io)
  143. {
  144. struct fsl_mc_device *mc_bus_dev;
  145. struct fsl_mc_bus *mc_bus;
  146. phys_addr_t mc_portal_phys_addr;
  147. size_t mc_portal_size;
  148. struct fsl_mc_device *dpmcp_dev;
  149. int error = -EINVAL;
  150. struct fsl_mc_resource *resource = NULL;
  151. struct fsl_mc_io *mc_io = NULL;
  152. if (mc_dev->flags & FSL_MC_IS_DPRC) {
  153. mc_bus_dev = mc_dev;
  154. } else {
  155. if (!dev_is_fsl_mc(mc_dev->dev.parent))
  156. return error;
  157. mc_bus_dev = to_fsl_mc_device(mc_dev->dev.parent);
  158. }
  159. mc_bus = to_fsl_mc_bus(mc_bus_dev);
  160. *new_mc_io = NULL;
  161. error = fsl_mc_resource_allocate(mc_bus, FSL_MC_POOL_DPMCP, &resource);
  162. if (error < 0)
  163. return error;
  164. error = -EINVAL;
  165. dpmcp_dev = resource->data;
  166. if (dpmcp_dev->obj_desc.ver_major < DPMCP_MIN_VER_MAJOR ||
  167. (dpmcp_dev->obj_desc.ver_major == DPMCP_MIN_VER_MAJOR &&
  168. dpmcp_dev->obj_desc.ver_minor < DPMCP_MIN_VER_MINOR)) {
  169. dev_err(&dpmcp_dev->dev,
  170. "ERROR: Version %d.%d of DPMCP not supported.\n",
  171. dpmcp_dev->obj_desc.ver_major,
  172. dpmcp_dev->obj_desc.ver_minor);
  173. error = -ENOTSUPP;
  174. goto error_cleanup_resource;
  175. }
  176. mc_portal_phys_addr = dpmcp_dev->regions[0].start;
  177. mc_portal_size = resource_size(dpmcp_dev->regions);
  178. error = fsl_create_mc_io(&mc_bus_dev->dev,
  179. mc_portal_phys_addr,
  180. mc_portal_size, dpmcp_dev,
  181. mc_io_flags, &mc_io);
  182. if (error < 0)
  183. goto error_cleanup_resource;
  184. dpmcp_dev->consumer_link = device_link_add(&mc_dev->dev,
  185. &dpmcp_dev->dev,
  186. DL_FLAG_AUTOREMOVE_CONSUMER);
  187. if (!dpmcp_dev->consumer_link) {
  188. error = -EINVAL;
  189. goto error_cleanup_mc_io;
  190. }
  191. *new_mc_io = mc_io;
  192. return 0;
  193. error_cleanup_mc_io:
  194. fsl_destroy_mc_io(mc_io);
  195. error_cleanup_resource:
  196. fsl_mc_resource_free(resource);
  197. return error;
  198. }
  199. EXPORT_SYMBOL_GPL(fsl_mc_portal_allocate);
  200. /**
  201. * fsl_mc_portal_free - Returns an MC portal to the pool of free MC portals
  202. * of a given MC bus
  203. *
  204. * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
  205. */
  206. void fsl_mc_portal_free(struct fsl_mc_io *mc_io)
  207. {
  208. struct fsl_mc_device *dpmcp_dev;
  209. struct fsl_mc_resource *resource;
  210. /*
  211. * Every mc_io obtained by calling fsl_mc_portal_allocate() is supposed
  212. * to have a DPMCP object associated with.
  213. */
  214. dpmcp_dev = mc_io->dpmcp_dev;
  215. resource = dpmcp_dev->resource;
  216. if (!resource || resource->type != FSL_MC_POOL_DPMCP)
  217. return;
  218. if (resource->data != dpmcp_dev)
  219. return;
  220. fsl_destroy_mc_io(mc_io);
  221. fsl_mc_resource_free(resource);
  222. dpmcp_dev->consumer_link = NULL;
  223. }
  224. EXPORT_SYMBOL_GPL(fsl_mc_portal_free);
  225. /**
  226. * fsl_mc_portal_reset - Resets the dpmcp object for a given fsl_mc_io object
  227. *
  228. * @mc_io: Pointer to the fsl_mc_io object that wraps the MC portal to free
  229. */
  230. int fsl_mc_portal_reset(struct fsl_mc_io *mc_io)
  231. {
  232. int error;
  233. struct fsl_mc_device *dpmcp_dev = mc_io->dpmcp_dev;
  234. error = dpmcp_reset(mc_io, 0, dpmcp_dev->mc_handle);
  235. if (error < 0) {
  236. dev_err(&dpmcp_dev->dev, "dpmcp_reset() failed: %d\n", error);
  237. return error;
  238. }
  239. return 0;
  240. }
  241. EXPORT_SYMBOL_GPL(fsl_mc_portal_reset);