vas.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright 2016-17 IBM Corp.
  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; either version
  7. * 2 of the License, or (at your option) any later version.
  8. */
  9. #ifndef _VAS_H
  10. #define _VAS_H
  11. #include <linux/atomic.h>
  12. #include <linux/idr.h>
  13. #include <asm/vas.h>
  14. #include <linux/io.h>
  15. /*
  16. * Overview of Virtual Accelerator Switchboard (VAS).
  17. *
  18. * VAS is a hardware "switchboard" that allows senders and receivers to
  19. * exchange messages with _minimal_ kernel involvment. The receivers are
  20. * typically NX coprocessor engines that perform compression or encryption
  21. * in hardware, but receivers can also be other software threads.
  22. *
  23. * Senders are user/kernel threads that submit compression/encryption or
  24. * other requests to the receivers. Senders must format their messages as
  25. * Coprocessor Request Blocks (CRB)s and submit them using the "copy" and
  26. * "paste" instructions which were introduced in Power9.
  27. *
  28. * A Power node can have (upto?) 8 Power chips. There is one instance of
  29. * VAS in each Power9 chip. Each instance of VAS has 64K windows or ports,
  30. * Senders and receivers must each connect to a separate window before they
  31. * can exchange messages through the switchboard.
  32. *
  33. * Each window is described by two types of window contexts:
  34. *
  35. * Hypervisor Window Context (HVWC) of size VAS_HVWC_SIZE bytes
  36. *
  37. * OS/User Window Context (UWC) of size VAS_UWC_SIZE bytes.
  38. *
  39. * A window context can be viewed as a set of 64-bit registers. The settings
  40. * in these registers configure/control/determine the behavior of the VAS
  41. * hardware when messages are sent/received through the window. The registers
  42. * in the HVWC are configured by the kernel while the registers in the UWC can
  43. * be configured by the kernel or by the user space application that is using
  44. * the window.
  45. *
  46. * The HVWCs for all windows on a specific instance of VAS are in a contiguous
  47. * range of hardware addresses or Base address region (BAR) referred to as the
  48. * HVWC BAR for the instance. Similarly the UWCs for all windows on an instance
  49. * are referred to as the UWC BAR for the instance.
  50. *
  51. * The two BARs for each instance are defined Power9 MMIO Ranges spreadsheet
  52. * and available to the kernel in the VAS node's "reg" property in the device
  53. * tree:
  54. *
  55. * /proc/device-tree/vasm@.../reg
  56. *
  57. * (see vas_probe() for details on the reg property).
  58. *
  59. * The kernel maps the HVWC and UWC BAR regions into the kernel address
  60. * space (hvwc_map and uwc_map). The kernel can then access the window
  61. * contexts of a specific window using:
  62. *
  63. * hvwc = hvwc_map + winid * VAS_HVWC_SIZE.
  64. * uwc = uwc_map + winid * VAS_UWC_SIZE.
  65. *
  66. * where winid is the window index (0..64K).
  67. *
  68. * As mentioned, a window context is used to "configure" a window. Besides
  69. * this configuration address, each _send_ window also has a unique hardware
  70. * "paste" address that is used to submit requests/CRBs (see vas_paste_crb()).
  71. *
  72. * The hardware paste address for a window is computed using the "paste
  73. * base address" and "paste win id shift" reg properties in the VAS device
  74. * tree node using:
  75. *
  76. * paste_addr = paste_base + ((winid << paste_win_id_shift))
  77. *
  78. * (again, see vas_probe() for ->paste_base_addr and ->paste_win_id_shift).
  79. *
  80. * The kernel maps this hardware address into the sender's address space
  81. * after which they can use the 'paste' instruction (new in Power9) to
  82. * send a message (submit a request aka CRB) to the coprocessor.
  83. *
  84. * NOTE: In the initial version, senders can only in-kernel drivers/threads.
  85. * Support for user space threads will be added in follow-on patches.
  86. *
  87. * TODO: Do we need to map the UWC into user address space so they can return
  88. * credits? Its NA for NX but may be needed for other receive windows.
  89. *
  90. */
  91. #define VAS_WINDOWS_PER_CHIP (64 << 10)
  92. /*
  93. * Hypervisor and OS/USer Window Context sizes
  94. */
  95. #define VAS_HVWC_SIZE 512
  96. #define VAS_UWC_SIZE PAGE_SIZE
  97. /*
  98. * Initial per-process credits.
  99. * Max send window credits: 4K-1 (12-bits in VAS_TX_WCRED)
  100. * Max receive window credits: 64K-1 (16 bits in VAS_LRX_WCRED)
  101. *
  102. * TODO: Needs tuning for per-process credits
  103. */
  104. #define VAS_WCREDS_MIN 16
  105. #define VAS_WCREDS_MAX ((64 << 10) - 1)
  106. #define VAS_WCREDS_DEFAULT (1 << 10)
  107. /*
  108. * VAS Window Context Register Offsets and bitmasks.
  109. * See Section 3.1.4 of VAS Work book
  110. */
  111. #define VAS_LPID_OFFSET 0x010
  112. #define VAS_LPID PPC_BITMASK(0, 11)
  113. #define VAS_PID_OFFSET 0x018
  114. #define VAS_PID_ID PPC_BITMASK(0, 19)
  115. #define VAS_XLATE_MSR_OFFSET 0x020
  116. #define VAS_XLATE_MSR_DR PPC_BIT(0)
  117. #define VAS_XLATE_MSR_TA PPC_BIT(1)
  118. #define VAS_XLATE_MSR_PR PPC_BIT(2)
  119. #define VAS_XLATE_MSR_US PPC_BIT(3)
  120. #define VAS_XLATE_MSR_HV PPC_BIT(4)
  121. #define VAS_XLATE_MSR_SF PPC_BIT(5)
  122. #define VAS_XLATE_LPCR_OFFSET 0x028
  123. #define VAS_XLATE_LPCR_PAGE_SIZE PPC_BITMASK(0, 2)
  124. #define VAS_XLATE_LPCR_ISL PPC_BIT(3)
  125. #define VAS_XLATE_LPCR_TC PPC_BIT(4)
  126. #define VAS_XLATE_LPCR_SC PPC_BIT(5)
  127. #define VAS_XLATE_CTL_OFFSET 0x030
  128. #define VAS_XLATE_MODE PPC_BITMASK(0, 1)
  129. #define VAS_AMR_OFFSET 0x040
  130. #define VAS_AMR PPC_BITMASK(0, 63)
  131. #define VAS_SEIDR_OFFSET 0x048
  132. #define VAS_SEIDR PPC_BITMASK(0, 63)
  133. #define VAS_FAULT_TX_WIN_OFFSET 0x050
  134. #define VAS_FAULT_TX_WIN PPC_BITMASK(48, 63)
  135. #define VAS_OSU_INTR_SRC_RA_OFFSET 0x060
  136. #define VAS_OSU_INTR_SRC_RA PPC_BITMASK(8, 63)
  137. #define VAS_HV_INTR_SRC_RA_OFFSET 0x070
  138. #define VAS_HV_INTR_SRC_RA PPC_BITMASK(8, 63)
  139. #define VAS_PSWID_OFFSET 0x078
  140. #define VAS_PSWID_EA_HANDLE PPC_BITMASK(0, 31)
  141. #define VAS_SPARE1_OFFSET 0x080
  142. #define VAS_SPARE2_OFFSET 0x088
  143. #define VAS_SPARE3_OFFSET 0x090
  144. #define VAS_SPARE4_OFFSET 0x130
  145. #define VAS_SPARE5_OFFSET 0x160
  146. #define VAS_SPARE6_OFFSET 0x188
  147. #define VAS_LFIFO_BAR_OFFSET 0x0A0
  148. #define VAS_LFIFO_BAR PPC_BITMASK(8, 53)
  149. #define VAS_PAGE_MIGRATION_SELECT PPC_BITMASK(54, 56)
  150. #define VAS_LDATA_STAMP_CTL_OFFSET 0x0A8
  151. #define VAS_LDATA_STAMP PPC_BITMASK(0, 1)
  152. #define VAS_XTRA_WRITE PPC_BIT(2)
  153. #define VAS_LDMA_CACHE_CTL_OFFSET 0x0B0
  154. #define VAS_LDMA_TYPE PPC_BITMASK(0, 1)
  155. #define VAS_LDMA_FIFO_DISABLE PPC_BIT(2)
  156. #define VAS_LRFIFO_PUSH_OFFSET 0x0B8
  157. #define VAS_LRFIFO_PUSH PPC_BITMASK(0, 15)
  158. #define VAS_CURR_MSG_COUNT_OFFSET 0x0C0
  159. #define VAS_CURR_MSG_COUNT PPC_BITMASK(0, 7)
  160. #define VAS_LNOTIFY_AFTER_COUNT_OFFSET 0x0C8
  161. #define VAS_LNOTIFY_AFTER_COUNT PPC_BITMASK(0, 7)
  162. #define VAS_LRX_WCRED_OFFSET 0x0E0
  163. #define VAS_LRX_WCRED PPC_BITMASK(0, 15)
  164. #define VAS_LRX_WCRED_ADDER_OFFSET 0x190
  165. #define VAS_LRX_WCRED_ADDER PPC_BITMASK(0, 15)
  166. #define VAS_TX_WCRED_OFFSET 0x0F0
  167. #define VAS_TX_WCRED PPC_BITMASK(4, 15)
  168. #define VAS_TX_WCRED_ADDER_OFFSET 0x1A0
  169. #define VAS_TX_WCRED_ADDER PPC_BITMASK(4, 15)
  170. #define VAS_LFIFO_SIZE_OFFSET 0x100
  171. #define VAS_LFIFO_SIZE PPC_BITMASK(0, 3)
  172. #define VAS_WINCTL_OFFSET 0x108
  173. #define VAS_WINCTL_OPEN PPC_BIT(0)
  174. #define VAS_WINCTL_REJ_NO_CREDIT PPC_BIT(1)
  175. #define VAS_WINCTL_PIN PPC_BIT(2)
  176. #define VAS_WINCTL_TX_WCRED_MODE PPC_BIT(3)
  177. #define VAS_WINCTL_RX_WCRED_MODE PPC_BIT(4)
  178. #define VAS_WINCTL_TX_WORD_MODE PPC_BIT(5)
  179. #define VAS_WINCTL_RX_WORD_MODE PPC_BIT(6)
  180. #define VAS_WINCTL_RSVD_TXBUF PPC_BIT(7)
  181. #define VAS_WINCTL_THRESH_CTL PPC_BITMASK(8, 9)
  182. #define VAS_WINCTL_FAULT_WIN PPC_BIT(10)
  183. #define VAS_WINCTL_NX_WIN PPC_BIT(11)
  184. #define VAS_WIN_STATUS_OFFSET 0x110
  185. #define VAS_WIN_BUSY PPC_BIT(1)
  186. #define VAS_WIN_CTX_CACHING_CTL_OFFSET 0x118
  187. #define VAS_CASTOUT_REQ PPC_BIT(0)
  188. #define VAS_PUSH_TO_MEM PPC_BIT(1)
  189. #define VAS_WIN_CACHE_STATUS PPC_BIT(4)
  190. #define VAS_TX_RSVD_BUF_COUNT_OFFSET 0x120
  191. #define VAS_RXVD_BUF_COUNT PPC_BITMASK(58, 63)
  192. #define VAS_LRFIFO_WIN_PTR_OFFSET 0x128
  193. #define VAS_LRX_WIN_ID PPC_BITMASK(0, 15)
  194. /*
  195. * Local Notification Control Register controls what happens in _response_
  196. * to a paste command and hence applies only to receive windows.
  197. */
  198. #define VAS_LNOTIFY_CTL_OFFSET 0x138
  199. #define VAS_NOTIFY_DISABLE PPC_BIT(0)
  200. #define VAS_INTR_DISABLE PPC_BIT(1)
  201. #define VAS_NOTIFY_EARLY PPC_BIT(2)
  202. #define VAS_NOTIFY_OSU_INTR PPC_BIT(3)
  203. #define VAS_LNOTIFY_PID_OFFSET 0x140
  204. #define VAS_LNOTIFY_PID PPC_BITMASK(0, 19)
  205. #define VAS_LNOTIFY_LPID_OFFSET 0x148
  206. #define VAS_LNOTIFY_LPID PPC_BITMASK(0, 11)
  207. #define VAS_LNOTIFY_TID_OFFSET 0x150
  208. #define VAS_LNOTIFY_TID PPC_BITMASK(0, 15)
  209. #define VAS_LNOTIFY_SCOPE_OFFSET 0x158
  210. #define VAS_LNOTIFY_MIN_SCOPE PPC_BITMASK(0, 1)
  211. #define VAS_LNOTIFY_MAX_SCOPE PPC_BITMASK(2, 3)
  212. #define VAS_NX_UTIL_OFFSET 0x1B0
  213. #define VAS_NX_UTIL PPC_BITMASK(0, 63)
  214. /* SE: Side effects */
  215. #define VAS_NX_UTIL_SE_OFFSET 0x1B8
  216. #define VAS_NX_UTIL_SE PPC_BITMASK(0, 63)
  217. #define VAS_NX_UTIL_ADDER_OFFSET 0x180
  218. #define VAS_NX_UTIL_ADDER PPC_BITMASK(32, 63)
  219. /*
  220. * Local Notify Scope Control Register. (Receive windows only).
  221. */
  222. enum vas_notify_scope {
  223. VAS_SCOPE_LOCAL,
  224. VAS_SCOPE_GROUP,
  225. VAS_SCOPE_VECTORED_GROUP,
  226. VAS_SCOPE_UNUSED,
  227. };
  228. /*
  229. * Local DMA Cache Control Register (Receive windows only).
  230. */
  231. enum vas_dma_type {
  232. VAS_DMA_TYPE_INJECT,
  233. VAS_DMA_TYPE_WRITE,
  234. };
  235. /*
  236. * Local Notify Scope Control Register. (Receive windows only).
  237. * Not applicable to NX receive windows.
  238. */
  239. enum vas_notify_after_count {
  240. VAS_NOTIFY_AFTER_256 = 0,
  241. VAS_NOTIFY_NONE,
  242. VAS_NOTIFY_AFTER_2
  243. };
  244. /*
  245. * One per instance of VAS. Each instance will have a separate set of
  246. * receive windows, one per coprocessor type.
  247. *
  248. * See also function header of set_vinst_win() for details on ->windows[]
  249. * and ->rxwin[] tables.
  250. */
  251. struct vas_instance {
  252. int vas_id;
  253. struct ida ida;
  254. struct list_head node;
  255. struct platform_device *pdev;
  256. u64 hvwc_bar_start;
  257. u64 uwc_bar_start;
  258. u64 paste_base_addr;
  259. u64 paste_win_id_shift;
  260. struct mutex mutex;
  261. struct vas_window *rxwin[VAS_COP_TYPE_MAX];
  262. struct vas_window *windows[VAS_WINDOWS_PER_CHIP];
  263. };
  264. /*
  265. * In-kernel state a VAS window. One per window.
  266. */
  267. struct vas_window {
  268. /* Fields common to send and receive windows */
  269. struct vas_instance *vinst;
  270. int winid;
  271. bool tx_win; /* True if send window */
  272. bool nx_win; /* True if NX window */
  273. bool user_win; /* True if user space window */
  274. void *hvwc_map; /* HV window context */
  275. void *uwc_map; /* OS/User window context */
  276. pid_t pid; /* Linux process id of owner */
  277. /* Fields applicable only to send windows */
  278. void *paste_kaddr;
  279. char *paste_addr_name;
  280. struct vas_window *rxwin;
  281. /* Feilds applicable only to receive windows */
  282. enum vas_cop_type cop;
  283. atomic_t num_txwins;
  284. };
  285. /*
  286. * Container for the hardware state of a window. One per-window.
  287. *
  288. * A VAS Window context is a 512-byte area in the hardware that contains
  289. * a set of 64-bit registers. Individual bit-fields in these registers
  290. * determine the configuration/operation of the hardware. struct vas_winctx
  291. * is a container for the register fields in the window context.
  292. */
  293. struct vas_winctx {
  294. void *rx_fifo;
  295. int rx_fifo_size;
  296. int wcreds_max;
  297. int rsvd_txbuf_count;
  298. bool user_win;
  299. bool nx_win;
  300. bool fault_win;
  301. bool rsvd_txbuf_enable;
  302. bool pin_win;
  303. bool rej_no_credit;
  304. bool tx_wcred_mode;
  305. bool rx_wcred_mode;
  306. bool tx_word_mode;
  307. bool rx_word_mode;
  308. bool data_stamp;
  309. bool xtra_write;
  310. bool notify_disable;
  311. bool intr_disable;
  312. bool fifo_disable;
  313. bool notify_early;
  314. bool notify_os_intr_reg;
  315. int lpid;
  316. int pidr; /* value from SPRN_PID, not linux pid */
  317. int lnotify_lpid;
  318. int lnotify_pid;
  319. int lnotify_tid;
  320. u32 pswid;
  321. int rx_win_id;
  322. int fault_win_id;
  323. int tc_mode;
  324. u64 irq_port;
  325. enum vas_dma_type dma_type;
  326. enum vas_notify_scope min_scope;
  327. enum vas_notify_scope max_scope;
  328. enum vas_notify_after_count notify_after_count;
  329. };
  330. extern struct vas_instance *find_vas_instance(int vasid);
  331. /*
  332. * VREG(x):
  333. * Expand a register's short name (eg: LPID) into two parameters:
  334. * - the register's short name in string form ("LPID"), and
  335. * - the name of the macro (eg: VAS_LPID_OFFSET), defining the
  336. * register's offset in the window context
  337. */
  338. #define VREG_SFX(n, s) __stringify(n), VAS_##n##s
  339. #define VREG(r) VREG_SFX(r, _OFFSET)
  340. #ifdef vas_debug
  341. static inline void dump_rx_win_attr(struct vas_rx_win_attr *attr)
  342. {
  343. pr_err("fault %d, notify %d, intr %d early %d\n",
  344. attr->fault_win, attr->notify_disable,
  345. attr->intr_disable, attr->notify_early);
  346. pr_err("rx_fifo_size %d, max value %d\n",
  347. attr->rx_fifo_size, VAS_RX_FIFO_SIZE_MAX);
  348. }
  349. static inline void vas_log_write(struct vas_window *win, char *name,
  350. void *regptr, u64 val)
  351. {
  352. if (val)
  353. pr_err("%swin #%d: %s reg %p, val 0x%016llx\n",
  354. win->tx_win ? "Tx" : "Rx", win->winid, name,
  355. regptr, val);
  356. }
  357. #else /* vas_debug */
  358. #define vas_log_write(win, name, reg, val)
  359. #define dump_rx_win_attr(attr)
  360. #endif /* vas_debug */
  361. static inline void write_uwc_reg(struct vas_window *win, char *name,
  362. s32 reg, u64 val)
  363. {
  364. void *regptr;
  365. regptr = win->uwc_map + reg;
  366. vas_log_write(win, name, regptr, val);
  367. out_be64(regptr, val);
  368. }
  369. static inline void write_hvwc_reg(struct vas_window *win, char *name,
  370. s32 reg, u64 val)
  371. {
  372. void *regptr;
  373. regptr = win->hvwc_map + reg;
  374. vas_log_write(win, name, regptr, val);
  375. out_be64(regptr, val);
  376. }
  377. static inline u64 read_hvwc_reg(struct vas_window *win,
  378. char *name __maybe_unused, s32 reg)
  379. {
  380. return in_be64(win->hvwc_map+reg);
  381. }
  382. #ifdef vas_debug
  383. static void print_fifo_msg_count(struct vas_window *txwin)
  384. {
  385. uint64_t read_hvwc_reg(struct vas_window *w, char *n, uint64_t o);
  386. pr_devel("Winid %d, Msg count %llu\n", txwin->winid,
  387. (uint64_t)read_hvwc_reg(txwin, VREG(LRFIFO_PUSH)));
  388. }
  389. #else /* vas_debug */
  390. #define print_fifo_msg_count(window)
  391. #endif /* vas_debug */
  392. #endif /* _VAS_H */