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