mc.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Freescale Management Complex (MC) bus public interface
  4. *
  5. * Copyright (C) 2014-2016 Freescale Semiconductor, Inc.
  6. * Author: German Rivera <German.Rivera@freescale.com>
  7. *
  8. */
  9. #ifndef _FSL_MC_H_
  10. #define _FSL_MC_H_
  11. #include <linux/device.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/interrupt.h>
  14. #define FSL_MC_VENDOR_FREESCALE 0x1957
  15. struct irq_domain;
  16. struct msi_domain_info;
  17. struct fsl_mc_device;
  18. struct fsl_mc_io;
  19. /**
  20. * struct fsl_mc_driver - MC object device driver object
  21. * @driver: Generic device driver
  22. * @match_id_table: table of supported device matching Ids
  23. * @probe: Function called when a device is added
  24. * @remove: Function called when a device is removed
  25. * @shutdown: Function called at shutdown time to quiesce the device
  26. * @suspend: Function called when a device is stopped
  27. * @resume: Function called when a device is resumed
  28. *
  29. * Generic DPAA device driver object for device drivers that are registered
  30. * with a DPRC bus. This structure is to be embedded in each device-specific
  31. * driver structure.
  32. */
  33. struct fsl_mc_driver {
  34. struct device_driver driver;
  35. const struct fsl_mc_device_id *match_id_table;
  36. int (*probe)(struct fsl_mc_device *dev);
  37. int (*remove)(struct fsl_mc_device *dev);
  38. void (*shutdown)(struct fsl_mc_device *dev);
  39. int (*suspend)(struct fsl_mc_device *dev, pm_message_t state);
  40. int (*resume)(struct fsl_mc_device *dev);
  41. };
  42. #define to_fsl_mc_driver(_drv) \
  43. container_of(_drv, struct fsl_mc_driver, driver)
  44. /**
  45. * enum fsl_mc_pool_type - Types of allocatable MC bus resources
  46. *
  47. * Entries in these enum are used as indices in the array of resource
  48. * pools of an fsl_mc_bus object.
  49. */
  50. enum fsl_mc_pool_type {
  51. FSL_MC_POOL_DPMCP = 0x0, /* corresponds to "dpmcp" in the MC */
  52. FSL_MC_POOL_DPBP, /* corresponds to "dpbp" in the MC */
  53. FSL_MC_POOL_DPCON, /* corresponds to "dpcon" in the MC */
  54. FSL_MC_POOL_IRQ,
  55. /*
  56. * NOTE: New resource pool types must be added before this entry
  57. */
  58. FSL_MC_NUM_POOL_TYPES
  59. };
  60. /**
  61. * struct fsl_mc_resource - MC generic resource
  62. * @type: type of resource
  63. * @id: unique MC resource Id within the resources of the same type
  64. * @data: pointer to resource-specific data if the resource is currently
  65. * allocated, or NULL if the resource is not currently allocated.
  66. * @parent_pool: pointer to the parent resource pool from which this
  67. * resource is allocated from.
  68. * @node: Node in the free list of the corresponding resource pool
  69. *
  70. * NOTE: This structure is to be embedded as a field of specific
  71. * MC resource structures.
  72. */
  73. struct fsl_mc_resource {
  74. enum fsl_mc_pool_type type;
  75. s32 id;
  76. void *data;
  77. struct fsl_mc_resource_pool *parent_pool;
  78. struct list_head node;
  79. };
  80. /**
  81. * struct fsl_mc_device_irq - MC object device message-based interrupt
  82. * @msi_desc: pointer to MSI descriptor allocated by fsl_mc_msi_alloc_descs()
  83. * @mc_dev: MC object device that owns this interrupt
  84. * @dev_irq_index: device-relative IRQ index
  85. * @resource: MC generic resource associated with the interrupt
  86. */
  87. struct fsl_mc_device_irq {
  88. struct msi_desc *msi_desc;
  89. struct fsl_mc_device *mc_dev;
  90. u8 dev_irq_index;
  91. struct fsl_mc_resource resource;
  92. };
  93. #define to_fsl_mc_irq(_mc_resource) \
  94. container_of(_mc_resource, struct fsl_mc_device_irq, resource)
  95. /* Opened state - Indicates that an object is open by at least one owner */
  96. #define FSL_MC_OBJ_STATE_OPEN 0x00000001
  97. /* Plugged state - Indicates that the object is plugged */
  98. #define FSL_MC_OBJ_STATE_PLUGGED 0x00000002
  99. /**
  100. * Shareability flag - Object flag indicating no memory shareability.
  101. * the object generates memory accesses that are non coherent with other
  102. * masters;
  103. * user is responsible for proper memory handling through IOMMU configuration.
  104. */
  105. #define FSL_MC_OBJ_FLAG_NO_MEM_SHAREABILITY 0x0001
  106. /**
  107. * struct fsl_mc_obj_desc - Object descriptor
  108. * @type: Type of object: NULL terminated string
  109. * @id: ID of logical object resource
  110. * @vendor: Object vendor identifier
  111. * @ver_major: Major version number
  112. * @ver_minor: Minor version number
  113. * @irq_count: Number of interrupts supported by the object
  114. * @region_count: Number of mappable regions supported by the object
  115. * @state: Object state: combination of FSL_MC_OBJ_STATE_ states
  116. * @label: Object label: NULL terminated string
  117. * @flags: Object's flags
  118. */
  119. struct fsl_mc_obj_desc {
  120. char type[16];
  121. int id;
  122. u16 vendor;
  123. u16 ver_major;
  124. u16 ver_minor;
  125. u8 irq_count;
  126. u8 region_count;
  127. u32 state;
  128. char label[16];
  129. u16 flags;
  130. };
  131. /**
  132. * Bit masks for a MC object device (struct fsl_mc_device) flags
  133. */
  134. #define FSL_MC_IS_DPRC 0x0001
  135. /**
  136. * struct fsl_mc_device - MC object device object
  137. * @dev: Linux driver model device object
  138. * @dma_mask: Default DMA mask
  139. * @flags: MC object device flags
  140. * @icid: Isolation context ID for the device
  141. * @mc_handle: MC handle for the corresponding MC object opened
  142. * @mc_io: Pointer to MC IO object assigned to this device or
  143. * NULL if none.
  144. * @obj_desc: MC description of the DPAA device
  145. * @regions: pointer to array of MMIO region entries
  146. * @irqs: pointer to array of pointers to interrupts allocated to this device
  147. * @resource: generic resource associated with this MC object device, if any.
  148. *
  149. * Generic device object for MC object devices that are "attached" to a
  150. * MC bus.
  151. *
  152. * NOTES:
  153. * - For a non-DPRC object its icid is the same as its parent DPRC's icid.
  154. * - The SMMU notifier callback gets invoked after device_add() has been
  155. * called for an MC object device, but before the device-specific probe
  156. * callback gets called.
  157. * - DP_OBJ_DPRC objects are the only MC objects that have built-in MC
  158. * portals. For all other MC objects, their device drivers are responsible for
  159. * allocating MC portals for them by calling fsl_mc_portal_allocate().
  160. * - Some types of MC objects (e.g., DP_OBJ_DPBP, DP_OBJ_DPCON) are
  161. * treated as resources that can be allocated/deallocated from the
  162. * corresponding resource pool in the object's parent DPRC, using the
  163. * fsl_mc_object_allocate()/fsl_mc_object_free() functions. These MC objects
  164. * are known as "allocatable" objects. For them, the corresponding
  165. * fsl_mc_device's 'resource' points to the associated resource object.
  166. * For MC objects that are not allocatable (e.g., DP_OBJ_DPRC, DP_OBJ_DPNI),
  167. * 'resource' is NULL.
  168. */
  169. struct fsl_mc_device {
  170. struct device dev;
  171. u64 dma_mask;
  172. u16 flags;
  173. u16 icid;
  174. u16 mc_handle;
  175. struct fsl_mc_io *mc_io;
  176. struct fsl_mc_obj_desc obj_desc;
  177. struct resource *regions;
  178. struct fsl_mc_device_irq **irqs;
  179. struct fsl_mc_resource *resource;
  180. };
  181. #define to_fsl_mc_device(_dev) \
  182. container_of(_dev, struct fsl_mc_device, dev)
  183. #define MC_CMD_NUM_OF_PARAMS 7
  184. struct mc_cmd_header {
  185. u8 src_id;
  186. u8 flags_hw;
  187. u8 status;
  188. u8 flags_sw;
  189. __le16 token;
  190. __le16 cmd_id;
  191. };
  192. struct fsl_mc_command {
  193. u64 header;
  194. u64 params[MC_CMD_NUM_OF_PARAMS];
  195. };
  196. enum mc_cmd_status {
  197. MC_CMD_STATUS_OK = 0x0, /* Completed successfully */
  198. MC_CMD_STATUS_READY = 0x1, /* Ready to be processed */
  199. MC_CMD_STATUS_AUTH_ERR = 0x3, /* Authentication error */
  200. MC_CMD_STATUS_NO_PRIVILEGE = 0x4, /* No privilege */
  201. MC_CMD_STATUS_DMA_ERR = 0x5, /* DMA or I/O error */
  202. MC_CMD_STATUS_CONFIG_ERR = 0x6, /* Configuration error */
  203. MC_CMD_STATUS_TIMEOUT = 0x7, /* Operation timed out */
  204. MC_CMD_STATUS_NO_RESOURCE = 0x8, /* No resources */
  205. MC_CMD_STATUS_NO_MEMORY = 0x9, /* No memory available */
  206. MC_CMD_STATUS_BUSY = 0xA, /* Device is busy */
  207. MC_CMD_STATUS_UNSUPPORTED_OP = 0xB, /* Unsupported operation */
  208. MC_CMD_STATUS_INVALID_STATE = 0xC /* Invalid state */
  209. };
  210. /*
  211. * MC command flags
  212. */
  213. /* High priority flag */
  214. #define MC_CMD_FLAG_PRI 0x80
  215. /* Command completion flag */
  216. #define MC_CMD_FLAG_INTR_DIS 0x01
  217. static inline u64 mc_encode_cmd_header(u16 cmd_id,
  218. u32 cmd_flags,
  219. u16 token)
  220. {
  221. u64 header = 0;
  222. struct mc_cmd_header *hdr = (struct mc_cmd_header *)&header;
  223. hdr->cmd_id = cpu_to_le16(cmd_id);
  224. hdr->token = cpu_to_le16(token);
  225. hdr->status = MC_CMD_STATUS_READY;
  226. if (cmd_flags & MC_CMD_FLAG_PRI)
  227. hdr->flags_hw = MC_CMD_FLAG_PRI;
  228. if (cmd_flags & MC_CMD_FLAG_INTR_DIS)
  229. hdr->flags_sw = MC_CMD_FLAG_INTR_DIS;
  230. return header;
  231. }
  232. static inline u16 mc_cmd_hdr_read_token(struct fsl_mc_command *cmd)
  233. {
  234. struct mc_cmd_header *hdr = (struct mc_cmd_header *)&cmd->header;
  235. u16 token = le16_to_cpu(hdr->token);
  236. return token;
  237. }
  238. struct mc_rsp_create {
  239. __le32 object_id;
  240. };
  241. struct mc_rsp_api_ver {
  242. __le16 major_ver;
  243. __le16 minor_ver;
  244. };
  245. static inline u32 mc_cmd_read_object_id(struct fsl_mc_command *cmd)
  246. {
  247. struct mc_rsp_create *rsp_params;
  248. rsp_params = (struct mc_rsp_create *)cmd->params;
  249. return le32_to_cpu(rsp_params->object_id);
  250. }
  251. static inline void mc_cmd_read_api_version(struct fsl_mc_command *cmd,
  252. u16 *major_ver,
  253. u16 *minor_ver)
  254. {
  255. struct mc_rsp_api_ver *rsp_params;
  256. rsp_params = (struct mc_rsp_api_ver *)cmd->params;
  257. *major_ver = le16_to_cpu(rsp_params->major_ver);
  258. *minor_ver = le16_to_cpu(rsp_params->minor_ver);
  259. }
  260. /**
  261. * Bit masks for a MC I/O object (struct fsl_mc_io) flags
  262. */
  263. #define FSL_MC_IO_ATOMIC_CONTEXT_PORTAL 0x0001
  264. /**
  265. * struct fsl_mc_io - MC I/O object to be passed-in to mc_send_command()
  266. * @dev: device associated with this Mc I/O object
  267. * @flags: flags for mc_send_command()
  268. * @portal_size: MC command portal size in bytes
  269. * @portal_phys_addr: MC command portal physical address
  270. * @portal_virt_addr: MC command portal virtual address
  271. * @dpmcp_dev: pointer to the DPMCP device associated with the MC portal.
  272. *
  273. * Fields are only meaningful if the FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag is not
  274. * set:
  275. * @mutex: Mutex to serialize mc_send_command() calls that use the same MC
  276. * portal, if the fsl_mc_io object was created with the
  277. * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag off. mc_send_command() calls for this
  278. * fsl_mc_io object must be made only from non-atomic context.
  279. *
  280. * Fields are only meaningful if the FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag is
  281. * set:
  282. * @spinlock: Spinlock to serialize mc_send_command() calls that use the same MC
  283. * portal, if the fsl_mc_io object was created with the
  284. * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag on. mc_send_command() calls for this
  285. * fsl_mc_io object can be made from atomic or non-atomic context.
  286. */
  287. struct fsl_mc_io {
  288. struct device *dev;
  289. u16 flags;
  290. u32 portal_size;
  291. phys_addr_t portal_phys_addr;
  292. void __iomem *portal_virt_addr;
  293. struct fsl_mc_device *dpmcp_dev;
  294. union {
  295. /*
  296. * This field is only meaningful if the
  297. * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag is not set
  298. */
  299. struct mutex mutex; /* serializes mc_send_command() */
  300. /*
  301. * This field is only meaningful if the
  302. * FSL_MC_IO_ATOMIC_CONTEXT_PORTAL flag is set
  303. */
  304. spinlock_t spinlock; /* serializes mc_send_command() */
  305. };
  306. };
  307. int mc_send_command(struct fsl_mc_io *mc_io, struct fsl_mc_command *cmd);
  308. #ifdef CONFIG_FSL_MC_BUS
  309. #define dev_is_fsl_mc(_dev) ((_dev)->bus == &fsl_mc_bus_type)
  310. #else
  311. /* If fsl-mc bus is not present device cannot belong to fsl-mc bus */
  312. #define dev_is_fsl_mc(_dev) (0)
  313. #endif
  314. /* Macro to check if a device is a container device */
  315. #define fsl_mc_is_cont_dev(_dev) (to_fsl_mc_device(_dev)->flags & \
  316. FSL_MC_IS_DPRC)
  317. /* Macro to get the container device of a MC device */
  318. #define fsl_mc_cont_dev(_dev) (fsl_mc_is_cont_dev(_dev) ? \
  319. (_dev) : (_dev)->parent)
  320. /*
  321. * module_fsl_mc_driver() - Helper macro for drivers that don't do
  322. * anything special in module init/exit. This eliminates a lot of
  323. * boilerplate. Each module may only use this macro once, and
  324. * calling it replaces module_init() and module_exit()
  325. */
  326. #define module_fsl_mc_driver(__fsl_mc_driver) \
  327. module_driver(__fsl_mc_driver, fsl_mc_driver_register, \
  328. fsl_mc_driver_unregister)
  329. /*
  330. * Macro to avoid include chaining to get THIS_MODULE
  331. */
  332. #define fsl_mc_driver_register(drv) \
  333. __fsl_mc_driver_register(drv, THIS_MODULE)
  334. int __must_check __fsl_mc_driver_register(struct fsl_mc_driver *fsl_mc_driver,
  335. struct module *owner);
  336. void fsl_mc_driver_unregister(struct fsl_mc_driver *driver);
  337. int __must_check fsl_mc_portal_allocate(struct fsl_mc_device *mc_dev,
  338. u16 mc_io_flags,
  339. struct fsl_mc_io **new_mc_io);
  340. void fsl_mc_portal_free(struct fsl_mc_io *mc_io);
  341. int fsl_mc_portal_reset(struct fsl_mc_io *mc_io);
  342. int __must_check fsl_mc_object_allocate(struct fsl_mc_device *mc_dev,
  343. enum fsl_mc_pool_type pool_type,
  344. struct fsl_mc_device **new_mc_adev);
  345. void fsl_mc_object_free(struct fsl_mc_device *mc_adev);
  346. struct irq_domain *fsl_mc_msi_create_irq_domain(struct fwnode_handle *fwnode,
  347. struct msi_domain_info *info,
  348. struct irq_domain *parent);
  349. int __must_check fsl_mc_allocate_irqs(struct fsl_mc_device *mc_dev);
  350. void fsl_mc_free_irqs(struct fsl_mc_device *mc_dev);
  351. extern struct bus_type fsl_mc_bus_type;
  352. extern struct device_type fsl_mc_bus_dprc_type;
  353. extern struct device_type fsl_mc_bus_dpni_type;
  354. extern struct device_type fsl_mc_bus_dpio_type;
  355. extern struct device_type fsl_mc_bus_dpsw_type;
  356. extern struct device_type fsl_mc_bus_dpbp_type;
  357. extern struct device_type fsl_mc_bus_dpcon_type;
  358. extern struct device_type fsl_mc_bus_dpmcp_type;
  359. extern struct device_type fsl_mc_bus_dpmac_type;
  360. extern struct device_type fsl_mc_bus_dprtc_type;
  361. static inline bool is_fsl_mc_bus_dprc(const struct fsl_mc_device *mc_dev)
  362. {
  363. return mc_dev->dev.type == &fsl_mc_bus_dprc_type;
  364. }
  365. static inline bool is_fsl_mc_bus_dpni(const struct fsl_mc_device *mc_dev)
  366. {
  367. return mc_dev->dev.type == &fsl_mc_bus_dpni_type;
  368. }
  369. static inline bool is_fsl_mc_bus_dpio(const struct fsl_mc_device *mc_dev)
  370. {
  371. return mc_dev->dev.type == &fsl_mc_bus_dpio_type;
  372. }
  373. static inline bool is_fsl_mc_bus_dpsw(const struct fsl_mc_device *mc_dev)
  374. {
  375. return mc_dev->dev.type == &fsl_mc_bus_dpsw_type;
  376. }
  377. static inline bool is_fsl_mc_bus_dpbp(const struct fsl_mc_device *mc_dev)
  378. {
  379. return mc_dev->dev.type == &fsl_mc_bus_dpbp_type;
  380. }
  381. static inline bool is_fsl_mc_bus_dpcon(const struct fsl_mc_device *mc_dev)
  382. {
  383. return mc_dev->dev.type == &fsl_mc_bus_dpcon_type;
  384. }
  385. static inline bool is_fsl_mc_bus_dpmcp(const struct fsl_mc_device *mc_dev)
  386. {
  387. return mc_dev->dev.type == &fsl_mc_bus_dpmcp_type;
  388. }
  389. static inline bool is_fsl_mc_bus_dpmac(const struct fsl_mc_device *mc_dev)
  390. {
  391. return mc_dev->dev.type == &fsl_mc_bus_dpmac_type;
  392. }
  393. static inline bool is_fsl_mc_bus_dprtc(const struct fsl_mc_device *mc_dev)
  394. {
  395. return mc_dev->dev.type == &fsl_mc_bus_dprtc_type;
  396. }
  397. /*
  398. * Data Path Buffer Pool (DPBP) API
  399. * Contains initialization APIs and runtime control APIs for DPBP
  400. */
  401. int dpbp_open(struct fsl_mc_io *mc_io,
  402. u32 cmd_flags,
  403. int dpbp_id,
  404. u16 *token);
  405. int dpbp_close(struct fsl_mc_io *mc_io,
  406. u32 cmd_flags,
  407. u16 token);
  408. int dpbp_enable(struct fsl_mc_io *mc_io,
  409. u32 cmd_flags,
  410. u16 token);
  411. int dpbp_disable(struct fsl_mc_io *mc_io,
  412. u32 cmd_flags,
  413. u16 token);
  414. int dpbp_reset(struct fsl_mc_io *mc_io,
  415. u32 cmd_flags,
  416. u16 token);
  417. /**
  418. * struct dpbp_attr - Structure representing DPBP attributes
  419. * @id: DPBP object ID
  420. * @bpid: Hardware buffer pool ID; should be used as an argument in
  421. * acquire/release operations on buffers
  422. */
  423. struct dpbp_attr {
  424. int id;
  425. u16 bpid;
  426. };
  427. int dpbp_get_attributes(struct fsl_mc_io *mc_io,
  428. u32 cmd_flags,
  429. u16 token,
  430. struct dpbp_attr *attr);
  431. /* Data Path Concentrator (DPCON) API
  432. * Contains initialization APIs and runtime control APIs for DPCON
  433. */
  434. /**
  435. * Use it to disable notifications; see dpcon_set_notification()
  436. */
  437. #define DPCON_INVALID_DPIO_ID (int)(-1)
  438. int dpcon_open(struct fsl_mc_io *mc_io,
  439. u32 cmd_flags,
  440. int dpcon_id,
  441. u16 *token);
  442. int dpcon_close(struct fsl_mc_io *mc_io,
  443. u32 cmd_flags,
  444. u16 token);
  445. int dpcon_enable(struct fsl_mc_io *mc_io,
  446. u32 cmd_flags,
  447. u16 token);
  448. int dpcon_disable(struct fsl_mc_io *mc_io,
  449. u32 cmd_flags,
  450. u16 token);
  451. int dpcon_reset(struct fsl_mc_io *mc_io,
  452. u32 cmd_flags,
  453. u16 token);
  454. /**
  455. * struct dpcon_attr - Structure representing DPCON attributes
  456. * @id: DPCON object ID
  457. * @qbman_ch_id: Channel ID to be used by dequeue operation
  458. * @num_priorities: Number of priorities for the DPCON channel (1-8)
  459. */
  460. struct dpcon_attr {
  461. int id;
  462. u16 qbman_ch_id;
  463. u8 num_priorities;
  464. };
  465. int dpcon_get_attributes(struct fsl_mc_io *mc_io,
  466. u32 cmd_flags,
  467. u16 token,
  468. struct dpcon_attr *attr);
  469. /**
  470. * struct dpcon_notification_cfg - Structure representing notification params
  471. * @dpio_id: DPIO object ID; must be configured with a notification channel;
  472. * to disable notifications set it to 'DPCON_INVALID_DPIO_ID';
  473. * @priority: Priority selection within the DPIO channel; valid values
  474. * are 0-7, depending on the number of priorities in that channel
  475. * @user_ctx: User context value provided with each CDAN message
  476. */
  477. struct dpcon_notification_cfg {
  478. int dpio_id;
  479. u8 priority;
  480. u64 user_ctx;
  481. };
  482. int dpcon_set_notification(struct fsl_mc_io *mc_io,
  483. u32 cmd_flags,
  484. u16 token,
  485. struct dpcon_notification_cfg *cfg);
  486. #endif /* _FSL_MC_H_ */