vas.h 15 KB

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