trio.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright 2012 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. /*
  15. *
  16. * An API for allocating, configuring, and manipulating TRIO hardware
  17. * resources
  18. */
  19. /*
  20. *
  21. * The TILE-Gx TRIO shim provides connections to external devices via
  22. * PCIe or other transaction IO standards. The gxio_trio_ API,
  23. * declared in <gxio/trio.h>, allows applications to allocate and
  24. * configure TRIO IO resources like DMA command rings, memory map
  25. * windows, and device interrupts. The following sections introduce
  26. * the various components of the API. We strongly recommend reading
  27. * the TRIO section of the IO Device Guide (UG404) before working with
  28. * this API.
  29. *
  30. * @section trio__ingress TRIO Ingress Hardware Resources
  31. *
  32. * The TRIO ingress hardware is responsible for examining incoming
  33. * PCIe or StreamIO packets and choosing a processing mechanism based
  34. * on the packets' bus address. The gxio_trio_ API can be used to
  35. * configure different handlers for different ranges of bus address
  36. * space. The user can configure "mapped memory" and "scatter queue"
  37. * regions to match incoming packets within 4kB-aligned ranges of bus
  38. * addresses. Each range specifies a different set of mapping
  39. * parameters to be applied when handling the ingress packet. The
  40. * following sections describe how to work with MapMem and scatter
  41. * queue regions.
  42. *
  43. * @subsection trio__mapmem TRIO MapMem Regions
  44. *
  45. * TRIO mapped memory (or MapMem) regions allow the user to map
  46. * incoming read and write requests directly to the application's
  47. * memory space. MapMem regions are allocated via
  48. * gxio_trio_alloc_memory_maps(). Given an integer MapMem number,
  49. * applications can use gxio_trio_init_memory_map() to specify the
  50. * range of bus addresses that will match the region and the range of
  51. * virtual addresses to which those packets will be applied.
  52. *
  53. * As with many other gxio APIs, the programmer must be sure to
  54. * register memory pages that will be used with MapMem regions. Pages
  55. * can be registered with TRIO by allocating an ASID (address space
  56. * identifier) and then using gxio_trio_register_page() to register up to
  57. * 16 pages with the hardware. The initialization functions for
  58. * resources that require registered memory (MapMem, scatter queues,
  59. * push DMA, and pull DMA) then take an 'asid' parameter in order to
  60. * configure which set of registered pages is used by each resource.
  61. *
  62. * @subsection trio__scatter_queue TRIO Scatter Queues
  63. *
  64. * The TRIO shim's scatter queue regions allow users to dynamically
  65. * map buffers from a large address space into a small range of bus
  66. * addresses. This is particularly helpful for PCIe endpoint devices,
  67. * where the host generally limits the size of BARs to tens of
  68. * megabytes.
  69. *
  70. * Each scatter queue consists of a memory map region, a queue of
  71. * tile-side buffer VAs to be mapped to that region, and a bus-mapped
  72. * "doorbell" register that the remote endpoint can write to trigger a
  73. * dequeue of the current buffer VA, thus swapping in a new buffer.
  74. * The VAs pushed onto a scatter queue must be 4kB aligned, so
  75. * applications may need to use higher-level protocols to inform
  76. * remote entities that they should apply some additional, sub-4kB
  77. * offset when reading or writing the scatter queue region. For more
  78. * information, see the IO Device Guide (UG404).
  79. *
  80. * @section trio__egress TRIO Egress Hardware Resources
  81. *
  82. * The TRIO shim supports two mechanisms for egress packet generation:
  83. * programmed IO (PIO) and push/pull DMA. PIO allows applications to
  84. * create MMIO mappings for PCIe or StreamIO address space, such that
  85. * the application can generate word-sized read or write transactions
  86. * by issuing load or store instructions. Push and pull DMA are tuned
  87. * for larger transactions; they use specialized hardware engines to
  88. * transfer large blocks of data at line rate.
  89. *
  90. * @subsection trio__pio TRIO Programmed IO
  91. *
  92. * Programmed IO allows applications to create MMIO mappings for PCIe
  93. * or StreamIO address space. The hardware PIO regions support access
  94. * to PCIe configuration, IO, and memory space, but the gxio_trio API
  95. * only supports memory space accesses. PIO regions are allocated
  96. * with gxio_trio_alloc_pio_regions() and initialized via
  97. * gxio_trio_init_pio_region(). Once a region is bound to a range of
  98. * bus address via the initialization function, the application can
  99. * use gxio_trio_map_pio_region() to create MMIO mappings from its VA
  100. * space onto the range of bus addresses supported by the PIO region.
  101. *
  102. * @subsection trio_dma TRIO Push and Pull DMA
  103. *
  104. * The TRIO push and pull DMA engines allow users to copy blocks of
  105. * data between application memory and the bus. Push DMA generates
  106. * write packets that copy from application memory to the bus and pull
  107. * DMA generates read packets that copy from the bus into application
  108. * memory. The DMA engines are managed via an API that is very
  109. * similar to the mPIPE eDMA interface. For a detailed explanation of
  110. * the eDMA queue API, see @ref gxio_mpipe_wrappers.
  111. *
  112. * Push and pull DMA queues are allocated via
  113. * gxio_trio_alloc_push_dma_ring() / gxio_trio_alloc_pull_dma_ring().
  114. * Once allocated, users generally use a ::gxio_trio_dma_queue_t
  115. * object to manage the queue, providing easy wrappers for reserving
  116. * command slots in the DMA command ring, filling those slots, and
  117. * waiting for commands to complete. DMA queues can be initialized
  118. * via gxio_trio_init_push_dma_queue() or
  119. * gxio_trio_init_pull_dma_queue().
  120. *
  121. * See @ref trio/push_dma/app.c for an example of how to use push DMA.
  122. *
  123. * @section trio_shortcomings Plans for Future API Revisions
  124. *
  125. * The simulation framework is incomplete. Future features include:
  126. *
  127. * - Support for reset and deallocation of resources.
  128. *
  129. * - Support for pull DMA.
  130. *
  131. * - Support for interrupt regions and user-space interrupt delivery.
  132. *
  133. * - Support for getting BAR mappings and reserving regions of BAR
  134. * address space.
  135. */
  136. #ifndef _GXIO_TRIO_H_
  137. #define _GXIO_TRIO_H_
  138. #include <linux/types.h>
  139. #include <gxio/common.h>
  140. #include <gxio/dma_queue.h>
  141. #include <arch/trio_constants.h>
  142. #include <arch/trio.h>
  143. #include <arch/trio_pcie_intfc.h>
  144. #include <arch/trio_pcie_rc.h>
  145. #include <arch/trio_shm.h>
  146. #include <hv/drv_trio_intf.h>
  147. #include <hv/iorpc.h>
  148. /* A context object used to manage TRIO hardware resources. */
  149. typedef struct {
  150. /* File descriptor for calling up to Linux (and thus the HV). */
  151. int fd;
  152. /* The VA at which the MAC MMIO registers are mapped. */
  153. char *mmio_base_mac;
  154. /* The VA at which the PIO config space are mapped for each PCIe MAC.
  155. Gx36 has max 3 PCIe MACs per TRIO shim. */
  156. char *mmio_base_pio_cfg[TILEGX_TRIO_PCIES];
  157. #ifdef USE_SHARED_PCIE_CONFIG_REGION
  158. /* Index of the shared PIO region for PCI config access. */
  159. int pio_cfg_index;
  160. #else
  161. /* Index of the PIO region for PCI config access per MAC. */
  162. int pio_cfg_index[TILEGX_TRIO_PCIES];
  163. #endif
  164. /* The VA at which the push DMA MMIO registers are mapped. */
  165. char *mmio_push_dma[TRIO_NUM_PUSH_DMA_RINGS];
  166. /* The VA at which the pull DMA MMIO registers are mapped. */
  167. char *mmio_pull_dma[TRIO_NUM_PUSH_DMA_RINGS];
  168. /* Application space ID. */
  169. unsigned int asid;
  170. } gxio_trio_context_t;
  171. /* Command descriptor for push or pull DMA. */
  172. typedef TRIO_DMA_DESC_t gxio_trio_dma_desc_t;
  173. /* A convenient, thread-safe interface to an eDMA ring. */
  174. typedef struct {
  175. /* State object for tracking head and tail pointers. */
  176. __gxio_dma_queue_t dma_queue;
  177. /* The ring entries. */
  178. gxio_trio_dma_desc_t *dma_descs;
  179. /* The number of entries minus one. */
  180. unsigned long mask_num_entries;
  181. /* The log2() of the number of entries. */
  182. unsigned int log2_num_entries;
  183. } gxio_trio_dma_queue_t;
  184. /* Initialize a TRIO context.
  185. *
  186. * This function allocates a TRIO "service domain" and maps the MMIO
  187. * registers into the the caller's VA space.
  188. *
  189. * @param trio_index Which TRIO shim; Gx36 must pass 0.
  190. * @param context Context object to be initialized.
  191. */
  192. extern int gxio_trio_init(gxio_trio_context_t *context,
  193. unsigned int trio_index);
  194. /* This indicates that an ASID hasn't been allocated. */
  195. #define GXIO_ASID_NULL -1
  196. /* Ordering modes for map memory regions and scatter queue regions. */
  197. typedef enum gxio_trio_order_mode_e {
  198. /* Writes are not ordered. Reads always wait for previous writes. */
  199. GXIO_TRIO_ORDER_MODE_UNORDERED =
  200. TRIO_MAP_MEM_SETUP__ORDER_MODE_VAL_UNORDERED,
  201. /* Both writes and reads wait for previous transactions to complete. */
  202. GXIO_TRIO_ORDER_MODE_STRICT =
  203. TRIO_MAP_MEM_SETUP__ORDER_MODE_VAL_STRICT,
  204. /* Writes are ordered unless the incoming packet has the
  205. relaxed-ordering attributes set. */
  206. GXIO_TRIO_ORDER_MODE_OBEY_PACKET =
  207. TRIO_MAP_MEM_SETUP__ORDER_MODE_VAL_REL_ORD
  208. } gxio_trio_order_mode_t;
  209. /* Initialize a memory mapping region.
  210. *
  211. * @param context An initialized TRIO context.
  212. * @param map A Memory map region allocated by gxio_trio_alloc_memory_map().
  213. * @param target_mem VA of backing memory, should be registered via
  214. * gxio_trio_register_page() and aligned to 4kB.
  215. * @param target_size Length of the memory mapping, must be a multiple
  216. * of 4kB.
  217. * @param asid ASID to be used for Tile-side address translation.
  218. * @param mac MAC number.
  219. * @param bus_address Bus address at which the mapping starts.
  220. * @param order_mode Memory ordering mode for this mapping.
  221. * @return Zero on success, else ::GXIO_TRIO_ERR_BAD_MEMORY_MAP,
  222. * GXIO_TRIO_ERR_BAD_ASID, or ::GXIO_TRIO_ERR_BAD_BUS_RANGE.
  223. */
  224. extern int gxio_trio_init_memory_map(gxio_trio_context_t *context,
  225. unsigned int map, void *target_mem,
  226. size_t target_size, unsigned int asid,
  227. unsigned int mac, uint64_t bus_address,
  228. gxio_trio_order_mode_t order_mode);
  229. /* Flags that can be passed to resource allocation functions. */
  230. enum gxio_trio_alloc_flags_e {
  231. GXIO_TRIO_ALLOC_FIXED = HV_TRIO_ALLOC_FIXED,
  232. };
  233. /* Flags that can be passed to memory registration functions. */
  234. enum gxio_trio_mem_flags_e {
  235. /* Do not fill L3 when writing, and invalidate lines upon egress. */
  236. GXIO_TRIO_MEM_FLAG_NT_HINT = IORPC_MEM_BUFFER_FLAG_NT_HINT,
  237. /* L3 cache fills should only populate IO cache ways. */
  238. GXIO_TRIO_MEM_FLAG_IO_PIN = IORPC_MEM_BUFFER_FLAG_IO_PIN,
  239. };
  240. /* Flag indicating a request generator uses a special traffic
  241. class. */
  242. #define GXIO_TRIO_FLAG_TRAFFIC_CLASS(N) HV_TRIO_FLAG_TC(N)
  243. /* Flag indicating a request generator uses a virtual function
  244. number. */
  245. #define GXIO_TRIO_FLAG_VFUNC(N) HV_TRIO_FLAG_VFUNC(N)
  246. /*****************************************************************
  247. * Memory Registration *
  248. ******************************************************************/
  249. /* Allocate Application Space Identifiers (ASIDs). Each ASID can
  250. * register up to 16 page translations. ASIDs are used by memory map
  251. * regions, scatter queues, and DMA queues to translate application
  252. * VAs into memory system PAs.
  253. *
  254. * @param context An initialized TRIO context.
  255. * @param count Number of ASIDs required.
  256. * @param first Index of first ASID if ::GXIO_TRIO_ALLOC_FIXED flag
  257. * is set, otherwise ignored.
  258. * @param flags Flag bits, including bits from ::gxio_trio_alloc_flags_e.
  259. * @return Index of first ASID, or ::GXIO_TRIO_ERR_NO_ASID if allocation
  260. * failed.
  261. */
  262. extern int gxio_trio_alloc_asids(gxio_trio_context_t *context,
  263. unsigned int count, unsigned int first,
  264. unsigned int flags);
  265. #endif /* ! _GXIO_TRIO_H_ */