vas.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Copyright 2016-17 IBM Corp.
  4. */
  5. #ifndef _ASM_POWERPC_VAS_H
  6. #define _ASM_POWERPC_VAS_H
  7. #include <linux/sched/mm.h>
  8. #include <linux/mmu_context.h>
  9. #include <asm/icswx.h>
  10. #include <uapi/asm/vas-api.h>
  11. /*
  12. * Min and max FIFO sizes are based on Version 1.05 Section 3.1.4.25
  13. * (Local FIFO Size Register) of the VAS workbook.
  14. */
  15. #define VAS_RX_FIFO_SIZE_MIN (1 << 10) /* 1KB */
  16. #define VAS_RX_FIFO_SIZE_MAX (8 << 20) /* 8MB */
  17. /*
  18. * Threshold Control Mode: Have paste operation fail if the number of
  19. * requests in receive FIFO exceeds a threshold.
  20. *
  21. * NOTE: No special error code yet if paste is rejected because of these
  22. * limits. So users can't distinguish between this and other errors.
  23. */
  24. #define VAS_THRESH_DISABLED 0
  25. #define VAS_THRESH_FIFO_GT_HALF_FULL 1
  26. #define VAS_THRESH_FIFO_GT_QTR_FULL 2
  27. #define VAS_THRESH_FIFO_GT_EIGHTH_FULL 3
  28. /*
  29. * VAS window Linux status bits
  30. */
  31. #define VAS_WIN_ACTIVE 0x0 /* Used in platform independent */
  32. /* vas mmap() */
  33. /* Window is closed in the hypervisor due to lost credit */
  34. #define VAS_WIN_NO_CRED_CLOSE 0x00000001
  35. /* Window is closed due to migration */
  36. #define VAS_WIN_MIGRATE_CLOSE 0x00000002
  37. /*
  38. * Get/Set bit fields
  39. */
  40. #define GET_FIELD(m, v) (((v) & (m)) >> MASK_LSH(m))
  41. #define MASK_LSH(m) (__builtin_ffsl(m) - 1)
  42. #define SET_FIELD(m, v, val) \
  43. (((v) & ~(m)) | ((((typeof(v))(val)) << MASK_LSH(m)) & (m)))
  44. /*
  45. * Co-processor Engine type.
  46. */
  47. enum vas_cop_type {
  48. VAS_COP_TYPE_FAULT,
  49. VAS_COP_TYPE_842,
  50. VAS_COP_TYPE_842_HIPRI,
  51. VAS_COP_TYPE_GZIP,
  52. VAS_COP_TYPE_GZIP_HIPRI,
  53. VAS_COP_TYPE_FTW,
  54. VAS_COP_TYPE_MAX,
  55. };
  56. /*
  57. * User space VAS windows are opened by tasks and take references
  58. * to pid and mm until windows are closed.
  59. * Stores pid, mm, and tgid for each window.
  60. */
  61. struct vas_user_win_ref {
  62. struct pid *pid; /* PID of owner */
  63. struct pid *tgid; /* Thread group ID of owner */
  64. struct mm_struct *mm; /* Linux process mm_struct */
  65. struct mutex mmap_mutex; /* protects paste address mmap() */
  66. /* with DLPAR close/open windows */
  67. struct vm_area_struct *vma; /* Save VMA and used in DLPAR ops */
  68. };
  69. /*
  70. * Common VAS window struct on PowerNV and PowerVM
  71. */
  72. struct vas_window {
  73. u32 winid;
  74. u32 wcreds_max; /* Window credits */
  75. u32 status; /* Window status used in OS */
  76. enum vas_cop_type cop;
  77. struct vas_user_win_ref task_ref;
  78. char *dbgname;
  79. struct dentry *dbgdir;
  80. };
  81. /*
  82. * User space window operations used for powernv and powerVM
  83. */
  84. struct vas_user_win_ops {
  85. struct vas_window * (*open_win)(int vas_id, u64 flags,
  86. enum vas_cop_type);
  87. u64 (*paste_addr)(struct vas_window *);
  88. int (*close_win)(struct vas_window *);
  89. };
  90. static inline void put_vas_user_win_ref(struct vas_user_win_ref *ref)
  91. {
  92. /* Drop references to pid, tgid, and mm */
  93. put_pid(ref->pid);
  94. put_pid(ref->tgid);
  95. if (ref->mm)
  96. mmdrop(ref->mm);
  97. }
  98. static inline void vas_user_win_add_mm_context(struct vas_user_win_ref *ref)
  99. {
  100. mm_context_add_vas_window(ref->mm);
  101. /*
  102. * Even a process that has no foreign real address mapping can
  103. * use an unpaired COPY instruction (to no real effect). Issue
  104. * CP_ABORT to clear any pending COPY and prevent a covert
  105. * channel.
  106. *
  107. * __switch_to() will issue CP_ABORT on future context switches
  108. * if process / thread has any open VAS window (Use
  109. * current->mm->context.vas_windows).
  110. */
  111. asm volatile(PPC_CP_ABORT);
  112. }
  113. /*
  114. * Receive window attributes specified by the (in-kernel) owner of window.
  115. */
  116. struct vas_rx_win_attr {
  117. u64 rx_fifo;
  118. int rx_fifo_size;
  119. int wcreds_max;
  120. bool pin_win;
  121. bool rej_no_credit;
  122. bool tx_wcred_mode;
  123. bool rx_wcred_mode;
  124. bool tx_win_ord_mode;
  125. bool rx_win_ord_mode;
  126. bool data_stamp;
  127. bool nx_win;
  128. bool fault_win;
  129. bool user_win;
  130. bool notify_disable;
  131. bool intr_disable;
  132. bool notify_early;
  133. int lnotify_lpid;
  134. int lnotify_pid;
  135. int lnotify_tid;
  136. u32 pswid;
  137. int tc_mode;
  138. };
  139. /*
  140. * Window attributes specified by the in-kernel owner of a send window.
  141. */
  142. struct vas_tx_win_attr {
  143. enum vas_cop_type cop;
  144. int wcreds_max;
  145. int lpid;
  146. int pidr; /* hardware PID (from SPRN_PID) */
  147. int pswid;
  148. int rsvd_txbuf_count;
  149. int tc_mode;
  150. bool user_win;
  151. bool pin_win;
  152. bool rej_no_credit;
  153. bool rsvd_txbuf_enable;
  154. bool tx_wcred_mode;
  155. bool rx_wcred_mode;
  156. bool tx_win_ord_mode;
  157. bool rx_win_ord_mode;
  158. };
  159. #ifdef CONFIG_PPC_POWERNV
  160. /*
  161. * Helper to map a chip id to VAS id.
  162. * For POWER9, this is a 1:1 mapping. In the future this maybe a 1:N
  163. * mapping in which case, we will need to update this helper.
  164. *
  165. * Return the VAS id or -1 if no matching vasid is found.
  166. */
  167. int chip_to_vas_id(int chipid);
  168. /*
  169. * Helper to initialize receive window attributes to defaults for an
  170. * NX window.
  171. */
  172. void vas_init_rx_win_attr(struct vas_rx_win_attr *rxattr, enum vas_cop_type cop);
  173. /*
  174. * Open a VAS receive window for the instance of VAS identified by @vasid
  175. * Use @attr to initialize the attributes of the window.
  176. *
  177. * Return a handle to the window or ERR_PTR() on error.
  178. */
  179. struct vas_window *vas_rx_win_open(int vasid, enum vas_cop_type cop,
  180. struct vas_rx_win_attr *attr);
  181. /*
  182. * Helper to initialize send window attributes to defaults for an NX window.
  183. */
  184. extern void vas_init_tx_win_attr(struct vas_tx_win_attr *txattr,
  185. enum vas_cop_type cop);
  186. /*
  187. * Open a VAS send window for the instance of VAS identified by @vasid
  188. * and the co-processor type @cop. Use @attr to initialize attributes
  189. * of the window.
  190. *
  191. * Note: The instance of VAS must already have an open receive window for
  192. * the coprocessor type @cop.
  193. *
  194. * Return a handle to the send window or ERR_PTR() on error.
  195. */
  196. struct vas_window *vas_tx_win_open(int vasid, enum vas_cop_type cop,
  197. struct vas_tx_win_attr *attr);
  198. /*
  199. * Close the send or receive window identified by @win. For receive windows
  200. * return -EAGAIN if there are active send windows attached to this receive
  201. * window.
  202. */
  203. int vas_win_close(struct vas_window *win);
  204. /*
  205. * Copy the co-processor request block (CRB) @crb into the local L2 cache.
  206. */
  207. int vas_copy_crb(void *crb, int offset);
  208. /*
  209. * Paste a previously copied CRB (see vas_copy_crb()) from the L2 cache to
  210. * the hardware address associated with the window @win. @re is expected/
  211. * assumed to be true for NX windows.
  212. */
  213. int vas_paste_crb(struct vas_window *win, int offset, bool re);
  214. int vas_register_api_powernv(struct module *mod, enum vas_cop_type cop_type,
  215. const char *name);
  216. void vas_unregister_api_powernv(void);
  217. #endif
  218. #ifdef CONFIG_PPC_PSERIES
  219. /* VAS Capabilities */
  220. #define VAS_GZIP_QOS_FEAT 0x1
  221. #define VAS_GZIP_DEF_FEAT 0x2
  222. #define VAS_GZIP_QOS_FEAT_BIT PPC_BIT(VAS_GZIP_QOS_FEAT) /* Bit 1 */
  223. #define VAS_GZIP_DEF_FEAT_BIT PPC_BIT(VAS_GZIP_DEF_FEAT) /* Bit 2 */
  224. /* NX Capabilities */
  225. #define VAS_NX_GZIP_FEAT 0x1
  226. #define VAS_NX_GZIP_FEAT_BIT PPC_BIT(VAS_NX_GZIP_FEAT) /* Bit 1 */
  227. /*
  228. * These structs are used to retrieve overall VAS capabilities that
  229. * the hypervisor provides.
  230. */
  231. struct hv_vas_all_caps {
  232. __be64 descriptor;
  233. __be64 feat_type;
  234. } __packed __aligned(0x1000);
  235. struct vas_all_caps {
  236. u64 descriptor;
  237. u64 feat_type;
  238. };
  239. int h_query_vas_capabilities(const u64 hcall, u8 query_type, u64 result);
  240. int vas_register_api_pseries(struct module *mod,
  241. enum vas_cop_type cop_type, const char *name);
  242. void vas_unregister_api_pseries(void);
  243. #endif
  244. /*
  245. * Register / unregister coprocessor type to VAS API which will be exported
  246. * to user space. Applications can use this API to open / close window
  247. * which can be used to send / receive requests directly to cooprcessor.
  248. *
  249. * Only NX GZIP coprocessor type is supported now, but this API can be
  250. * used for others in future.
  251. */
  252. int vas_register_coproc_api(struct module *mod, enum vas_cop_type cop_type,
  253. const char *name,
  254. const struct vas_user_win_ops *vops);
  255. void vas_unregister_coproc_api(void);
  256. int get_vas_user_win_ref(struct vas_user_win_ref *task_ref);
  257. void vas_update_csb(struct coprocessor_request_block *crb,
  258. struct vas_user_win_ref *task_ref);
  259. void vas_dump_crb(struct coprocessor_request_block *crb);
  260. #endif /* __ASM_POWERPC_VAS_H */