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 
10 #define pr_fmt(fmt) "vas: " fmt
11 
12 #include <linux/types.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.h>
15 #include <linux/io.h>
16 #include <linux/log2.h>
17 #include <linux/rcupdate.h>
18 #include <linux/cred.h>
19 
20 #include "vas.h"
21 #include "copy-paste.h"
22 
23 /*
24  * Compute the paste address region for the window @window using the
25  * ->paste_base_addr and ->paste_win_id_shift we got from device tree.
26  */
27 static void compute_paste_address(struct vas_window *window, u64 *addr, int *len)
28 {
29 	int winid;
30 	u64 base, shift;
31 
32 	base = window->vinst->paste_base_addr;
33 	shift = window->vinst->paste_win_id_shift;
34 	winid = window->winid;
35 
36 	*addr  = base + (winid << shift);
37 	if (len)
38 		*len = PAGE_SIZE;
39 
40 	pr_debug("Txwin #%d: Paste addr 0x%llx\n", winid, *addr);
41 }
42 
43 u64 vas_win_paste_addr(struct vas_window *win)
44 {
45 	u64 addr;
46 
47 	compute_paste_address(win, &addr, NULL);
48 
49 	return addr;
50 }
51 EXPORT_SYMBOL(vas_win_paste_addr);
52 
53 static inline void get_hvwc_mmio_bar(struct vas_window *window,
54 			u64 *start, int *len)
55 {
56 	u64 pbaddr;
57 
58 	pbaddr = window->vinst->hvwc_bar_start;
59 	*start = pbaddr + window->winid * VAS_HVWC_SIZE;
60 	*len = VAS_HVWC_SIZE;
61 }
62 
63 static inline void get_uwc_mmio_bar(struct vas_window *window,
64 			u64 *start, int *len)
65 {
66 	u64 pbaddr;
67 
68 	pbaddr = window->vinst->uwc_bar_start;
69 	*start = pbaddr + window->winid * VAS_UWC_SIZE;
70 	*len = VAS_UWC_SIZE;
71 }
72 
73 /*
74  * Map the paste bus address of the given send window into kernel address
75  * space. Unlike MMIO regions (map_mmio_region() below), paste region must
76  * be mapped cache-able and is only applicable to send windows.
77  */
78 static void *map_paste_region(struct vas_window *txwin)
79 {
80 	int len;
81 	void *map;
82 	char *name;
83 	u64 start;
84 
85 	name = kasprintf(GFP_KERNEL, "window-v%d-w%d", txwin->vinst->vas_id,
86 				txwin->winid);
87 	if (!name)
88 		goto free_name;
89 
90 	txwin->paste_addr_name = name;
91 	compute_paste_address(txwin, &start, &len);
92 
93 	if (!request_mem_region(start, len, name)) {
94 		pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
95 				__func__, start, len);
96 		goto free_name;
97 	}
98 
99 	map = ioremap_cache(start, len);
100 	if (!map) {
101 		pr_devel("%s(): ioremap_cache(0x%llx, %d) failed\n", __func__,
102 				start, len);
103 		goto free_name;
104 	}
105 
106 	pr_devel("Mapped paste addr 0x%llx to kaddr 0x%p\n", start, map);
107 	return map;
108 
109 free_name:
110 	kfree(name);
111 	return ERR_PTR(-ENOMEM);
112 }
113 
114 static void *map_mmio_region(char *name, u64 start, int len)
115 {
116 	void *map;
117 
118 	if (!request_mem_region(start, len, name)) {
119 		pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
120 				__func__, start, len);
121 		return NULL;
122 	}
123 
124 	map = ioremap(start, len);
125 	if (!map) {
126 		pr_devel("%s(): ioremap(0x%llx, %d) failed\n", __func__, start,
127 				len);
128 		return NULL;
129 	}
130 
131 	return map;
132 }
133 
134 static void unmap_region(void *addr, u64 start, int len)
135 {
136 	iounmap(addr);
137 	release_mem_region((phys_addr_t)start, len);
138 }
139 
140 /*
141  * Unmap the paste address region for a window.
142  */
143 static void unmap_paste_region(struct vas_window *window)
144 {
145 	int len;
146 	u64 busaddr_start;
147 
148 	if (window->paste_kaddr) {
149 		compute_paste_address(window, &busaddr_start, &len);
150 		unmap_region(window->paste_kaddr, busaddr_start, len);
151 		window->paste_kaddr = NULL;
152 		kfree(window->paste_addr_name);
153 		window->paste_addr_name = NULL;
154 	}
155 }
156 
157 /*
158  * Unmap the MMIO regions for a window. Hold the vas_mutex so we don't
159  * unmap when the window's debugfs dir is in use. This serializes close
160  * of a window even on another VAS instance but since its not a critical
161  * path, just minimize the time we hold the mutex for now. We can add
162  * a per-instance mutex later if necessary.
163  */
164 static void unmap_winctx_mmio_bars(struct vas_window *window)
165 {
166 	int len;
167 	void *uwc_map;
168 	void *hvwc_map;
169 	u64 busaddr_start;
170 
171 	mutex_lock(&vas_mutex);
172 
173 	hvwc_map = window->hvwc_map;
174 	window->hvwc_map = NULL;
175 
176 	uwc_map = window->uwc_map;
177 	window->uwc_map = NULL;
178 
179 	mutex_unlock(&vas_mutex);
180 
181 	if (hvwc_map) {
182 		get_hvwc_mmio_bar(window, &busaddr_start, &len);
183 		unmap_region(hvwc_map, busaddr_start, len);
184 	}
185 
186 	if (uwc_map) {
187 		get_uwc_mmio_bar(window, &busaddr_start, &len);
188 		unmap_region(uwc_map, busaddr_start, len);
189 	}
190 }
191 
192 /*
193  * Find the Hypervisor Window Context (HVWC) MMIO Base Address Region and the
194  * OS/User Window Context (UWC) MMIO Base Address Region for the given window.
195  * Map these bus addresses and save the mapped kernel addresses in @window.
196  */
197 int map_winctx_mmio_bars(struct vas_window *window)
198 {
199 	int len;
200 	u64 start;
201 
202 	get_hvwc_mmio_bar(window, &start, &len);
203 	window->hvwc_map = map_mmio_region("HVWCM_Window", start, len);
204 
205 	get_uwc_mmio_bar(window, &start, &len);
206 	window->uwc_map = map_mmio_region("UWCM_Window", start, len);
207 
208 	if (!window->hvwc_map || !window->uwc_map) {
209 		unmap_winctx_mmio_bars(window);
210 		return -1;
211 	}
212 
213 	return 0;
214 }
215 
216 /*
217  * Reset all valid registers in the HV and OS/User Window Contexts for
218  * the window identified by @window.
219  *
220  * NOTE: We cannot really use a for loop to reset window context. Not all
221  *	 offsets in a window context are valid registers and the valid
222  *	 registers are not sequential. And, we can only write to offsets
223  *	 with valid registers.
224  */
225 void reset_window_regs(struct vas_window *window)
226 {
227 	write_hvwc_reg(window, VREG(LPID), 0ULL);
228 	write_hvwc_reg(window, VREG(PID), 0ULL);
229 	write_hvwc_reg(window, VREG(XLATE_MSR), 0ULL);
230 	write_hvwc_reg(window, VREG(XLATE_LPCR), 0ULL);
231 	write_hvwc_reg(window, VREG(XLATE_CTL), 0ULL);
232 	write_hvwc_reg(window, VREG(AMR), 0ULL);
233 	write_hvwc_reg(window, VREG(SEIDR), 0ULL);
234 	write_hvwc_reg(window, VREG(FAULT_TX_WIN), 0ULL);
235 	write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
236 	write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), 0ULL);
237 	write_hvwc_reg(window, VREG(PSWID), 0ULL);
238 	write_hvwc_reg(window, VREG(LFIFO_BAR), 0ULL);
239 	write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), 0ULL);
240 	write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), 0ULL);
241 	write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
242 	write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
243 	write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
244 	write_hvwc_reg(window, VREG(LRX_WCRED), 0ULL);
245 	write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
246 	write_hvwc_reg(window, VREG(TX_WCRED), 0ULL);
247 	write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
248 	write_hvwc_reg(window, VREG(LFIFO_SIZE), 0ULL);
249 	write_hvwc_reg(window, VREG(WINCTL), 0ULL);
250 	write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
251 	write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), 0ULL);
252 	write_hvwc_reg(window, VREG(TX_RSVD_BUF_COUNT), 0ULL);
253 	write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), 0ULL);
254 	write_hvwc_reg(window, VREG(LNOTIFY_CTL), 0ULL);
255 	write_hvwc_reg(window, VREG(LNOTIFY_PID), 0ULL);
256 	write_hvwc_reg(window, VREG(LNOTIFY_LPID), 0ULL);
257 	write_hvwc_reg(window, VREG(LNOTIFY_TID), 0ULL);
258 	write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), 0ULL);
259 	write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
260 
261 	/* Skip read-only registers: NX_UTIL and NX_UTIL_SE */
262 
263 	/*
264 	 * The send and receive window credit adder registers are also
265 	 * accessible from HVWC and have been initialized above. We don't
266 	 * need to initialize from the OS/User Window Context, so skip
267 	 * following calls:
268 	 *
269 	 *	write_uwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
270 	 *	write_uwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
271 	 */
272 }
273 
274 /*
275  * Initialize window context registers related to Address Translation.
276  * These registers are common to send/receive windows although they
277  * differ for user/kernel windows. As we resolve the TODOs we may
278  * want to add fields to vas_winctx and move the initialization to
279  * init_vas_winctx_regs().
280  */
281 static void init_xlate_regs(struct vas_window *window, bool user_win)
282 {
283 	u64 lpcr, val;
284 
285 	/*
286 	 * MSR_TA, MSR_US are false for both kernel and user.
287 	 * MSR_DR and MSR_PR are false for kernel.
288 	 */
289 	val = 0ULL;
290 	val = SET_FIELD(VAS_XLATE_MSR_HV, val, 1);
291 	val = SET_FIELD(VAS_XLATE_MSR_SF, val, 1);
292 	if (user_win) {
293 		val = SET_FIELD(VAS_XLATE_MSR_DR, val, 1);
294 		val = SET_FIELD(VAS_XLATE_MSR_PR, val, 1);
295 	}
296 	write_hvwc_reg(window, VREG(XLATE_MSR), val);
297 
298 	lpcr = mfspr(SPRN_LPCR);
299 	val = 0ULL;
300 	/*
301 	 * NOTE: From Section 5.7.8.1 Segment Lookaside Buffer of the
302 	 *	 Power ISA, v3.0B, Page size encoding is 0 = 4KB, 5 = 64KB.
303 	 *
304 	 * NOTE: From Section 1.3.1, Address Translation Context of the
305 	 *	 Nest MMU Workbook, LPCR_SC should be 0 for Power9.
306 	 */
307 	val = SET_FIELD(VAS_XLATE_LPCR_PAGE_SIZE, val, 5);
308 	val = SET_FIELD(VAS_XLATE_LPCR_ISL, val, lpcr & LPCR_ISL);
309 	val = SET_FIELD(VAS_XLATE_LPCR_TC, val, lpcr & LPCR_TC);
310 	val = SET_FIELD(VAS_XLATE_LPCR_SC, val, 0);
311 	write_hvwc_reg(window, VREG(XLATE_LPCR), val);
312 
313 	/*
314 	 * Section 1.3.1 (Address translation Context) of NMMU workbook.
315 	 *	0b00	Hashed Page Table mode
316 	 *	0b01	Reserved
317 	 *	0b10	Radix on HPT
318 	 *	0b11	Radix on Radix
319 	 */
320 	val = 0ULL;
321 	val = SET_FIELD(VAS_XLATE_MODE, val, radix_enabled() ? 3 : 2);
322 	write_hvwc_reg(window, VREG(XLATE_CTL), val);
323 
324 	/*
325 	 * TODO: Can we mfspr(AMR) even for user windows?
326 	 */
327 	val = 0ULL;
328 	val = SET_FIELD(VAS_AMR, val, mfspr(SPRN_AMR));
329 	write_hvwc_reg(window, VREG(AMR), val);
330 
331 	val = 0ULL;
332 	val = SET_FIELD(VAS_SEIDR, val, 0);
333 	write_hvwc_reg(window, VREG(SEIDR), val);
334 }
335 
336 /*
337  * Initialize Reserved Send Buffer Count for the send window. It involves
338  * writing to the register, reading it back to confirm that the hardware
339  * has enough buffers to reserve. See section 1.3.1.2.1 of VAS workbook.
340  *
341  * Since we can only make a best-effort attempt to fulfill the request,
342  * we don't return any errors if we cannot.
343  *
344  * TODO: Reserved (aka dedicated) send buffers are not supported yet.
345  */
346 static void init_rsvd_tx_buf_count(struct vas_window *txwin,
347 				struct vas_winctx *winctx)
348 {
349 	write_hvwc_reg(txwin, VREG(TX_RSVD_BUF_COUNT), 0ULL);
350 }
351 
352 /*
353  * init_winctx_regs()
354  *	Initialize window context registers for a receive window.
355  *	Except for caching control and marking window open, the registers
356  *	are initialized in the order listed in Section 3.1.4 (Window Context
357  *	Cache Register Details) of the VAS workbook although they don't need
358  *	to be.
359  *
360  * Design note: For NX receive windows, NX allocates the FIFO buffer in OPAL
361  *	(so that it can get a large contiguous area) and passes that buffer
362  *	to kernel via device tree. We now write that buffer address to the
363  *	FIFO BAR. Would it make sense to do this all in OPAL? i.e have OPAL
364  *	write the per-chip RX FIFO addresses to the windows during boot-up
365  *	as a one-time task? That could work for NX but what about other
366  *	receivers?  Let the receivers tell us the rx-fifo buffers for now.
367  */
368 int init_winctx_regs(struct vas_window *window, struct vas_winctx *winctx)
369 {
370 	u64 val;
371 	int fifo_size;
372 
373 	reset_window_regs(window);
374 
375 	val = 0ULL;
376 	val = SET_FIELD(VAS_LPID, val, winctx->lpid);
377 	write_hvwc_reg(window, VREG(LPID), val);
378 
379 	val = 0ULL;
380 	val = SET_FIELD(VAS_PID_ID, val, winctx->pidr);
381 	write_hvwc_reg(window, VREG(PID), val);
382 
383 	init_xlate_regs(window, winctx->user_win);
384 
385 	val = 0ULL;
386 	val = SET_FIELD(VAS_FAULT_TX_WIN, val, 0);
387 	write_hvwc_reg(window, VREG(FAULT_TX_WIN), val);
388 
389 	/* In PowerNV, interrupts go to HV. */
390 	write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
391 
392 	val = 0ULL;
393 	val = SET_FIELD(VAS_HV_INTR_SRC_RA, val, winctx->irq_port);
394 	write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), val);
395 
396 	val = 0ULL;
397 	val = SET_FIELD(VAS_PSWID_EA_HANDLE, val, winctx->pswid);
398 	write_hvwc_reg(window, VREG(PSWID), val);
399 
400 	write_hvwc_reg(window, VREG(SPARE1), 0ULL);
401 	write_hvwc_reg(window, VREG(SPARE2), 0ULL);
402 	write_hvwc_reg(window, VREG(SPARE3), 0ULL);
403 
404 	/*
405 	 * NOTE: VAS expects the FIFO address to be copied into the LFIFO_BAR
406 	 *	 register as is - do NOT shift the address into VAS_LFIFO_BAR
407 	 *	 bit fields! Ok to set the page migration select fields -
408 	 *	 VAS ignores the lower 10+ bits in the address anyway, because
409 	 *	 the minimum FIFO size is 1K?
410 	 *
411 	 * See also: Design note in function header.
412 	 */
413 	val = __pa(winctx->rx_fifo);
414 	val = SET_FIELD(VAS_PAGE_MIGRATION_SELECT, val, 0);
415 	write_hvwc_reg(window, VREG(LFIFO_BAR), val);
416 
417 	val = 0ULL;
418 	val = SET_FIELD(VAS_LDATA_STAMP, val, winctx->data_stamp);
419 	write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), val);
420 
421 	val = 0ULL;
422 	val = SET_FIELD(VAS_LDMA_TYPE, val, winctx->dma_type);
423 	val = SET_FIELD(VAS_LDMA_FIFO_DISABLE, val, winctx->fifo_disable);
424 	write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), val);
425 
426 	write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
427 	write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
428 	write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
429 
430 	val = 0ULL;
431 	val = SET_FIELD(VAS_LRX_WCRED, val, winctx->wcreds_max);
432 	write_hvwc_reg(window, VREG(LRX_WCRED), val);
433 
434 	val = 0ULL;
435 	val = SET_FIELD(VAS_TX_WCRED, val, winctx->wcreds_max);
436 	write_hvwc_reg(window, VREG(TX_WCRED), val);
437 
438 	write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
439 	write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
440 
441 	fifo_size = winctx->rx_fifo_size / 1024;
442 
443 	val = 0ULL;
444 	val = SET_FIELD(VAS_LFIFO_SIZE, val, ilog2(fifo_size));
445 	write_hvwc_reg(window, VREG(LFIFO_SIZE), val);
446 
447 	/* Update window control and caching control registers last so
448 	 * we mark the window open only after fully initializing it and
449 	 * pushing context to cache.
450 	 */
451 
452 	write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
453 
454 	init_rsvd_tx_buf_count(window, winctx);
455 
456 	/* for a send window, point to the matching receive window */
457 	val = 0ULL;
458 	val = SET_FIELD(VAS_LRX_WIN_ID, val, winctx->rx_win_id);
459 	write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), val);
460 
461 	write_hvwc_reg(window, VREG(SPARE4), 0ULL);
462 
463 	val = 0ULL;
464 	val = SET_FIELD(VAS_NOTIFY_DISABLE, val, winctx->notify_disable);
465 	val = SET_FIELD(VAS_INTR_DISABLE, val, winctx->intr_disable);
466 	val = SET_FIELD(VAS_NOTIFY_EARLY, val, winctx->notify_early);
467 	val = SET_FIELD(VAS_NOTIFY_OSU_INTR, val, winctx->notify_os_intr_reg);
468 	write_hvwc_reg(window, VREG(LNOTIFY_CTL), val);
469 
470 	val = 0ULL;
471 	val = SET_FIELD(VAS_LNOTIFY_PID, val, winctx->lnotify_pid);
472 	write_hvwc_reg(window, VREG(LNOTIFY_PID), val);
473 
474 	val = 0ULL;
475 	val = SET_FIELD(VAS_LNOTIFY_LPID, val, winctx->lnotify_lpid);
476 	write_hvwc_reg(window, VREG(LNOTIFY_LPID), val);
477 
478 	val = 0ULL;
479 	val = SET_FIELD(VAS_LNOTIFY_TID, val, winctx->lnotify_tid);
480 	write_hvwc_reg(window, VREG(LNOTIFY_TID), val);
481 
482 	val = 0ULL;
483 	val = SET_FIELD(VAS_LNOTIFY_MIN_SCOPE, val, winctx->min_scope);
484 	val = SET_FIELD(VAS_LNOTIFY_MAX_SCOPE, val, winctx->max_scope);
485 	write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), val);
486 
487 	/* Skip read-only registers NX_UTIL and NX_UTIL_SE */
488 
489 	write_hvwc_reg(window, VREG(SPARE5), 0ULL);
490 	write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
491 	write_hvwc_reg(window, VREG(SPARE6), 0ULL);
492 
493 	/* Finally, push window context to memory and... */
494 	val = 0ULL;
495 	val = SET_FIELD(VAS_PUSH_TO_MEM, val, 1);
496 	write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), val);
497 
498 	/* ... mark the window open for business */
499 	val = 0ULL;
500 	val = SET_FIELD(VAS_WINCTL_REJ_NO_CREDIT, val, winctx->rej_no_credit);
501 	val = SET_FIELD(VAS_WINCTL_PIN, val, winctx->pin_win);
502 	val = SET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val, winctx->tx_wcred_mode);
503 	val = SET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val, winctx->rx_wcred_mode);
504 	val = SET_FIELD(VAS_WINCTL_TX_WORD_MODE, val, winctx->tx_word_mode);
505 	val = SET_FIELD(VAS_WINCTL_RX_WORD_MODE, val, winctx->rx_word_mode);
506 	val = SET_FIELD(VAS_WINCTL_FAULT_WIN, val, winctx->fault_win);
507 	val = SET_FIELD(VAS_WINCTL_NX_WIN, val, winctx->nx_win);
508 	val = SET_FIELD(VAS_WINCTL_OPEN, val, 1);
509 	write_hvwc_reg(window, VREG(WINCTL), val);
510 
511 	return 0;
512 }
513 
514 static DEFINE_SPINLOCK(vas_ida_lock);
515 
516 static void vas_release_window_id(struct ida *ida, int winid)
517 {
518 	spin_lock(&vas_ida_lock);
519 	ida_remove(ida, winid);
520 	spin_unlock(&vas_ida_lock);
521 }
522 
523 static int vas_assign_window_id(struct ida *ida)
524 {
525 	int rc, winid;
526 
527 	do {
528 		rc = ida_pre_get(ida, GFP_KERNEL);
529 		if (!rc)
530 			return -EAGAIN;
531 
532 		spin_lock(&vas_ida_lock);
533 		rc = ida_get_new(ida, &winid);
534 		spin_unlock(&vas_ida_lock);
535 	} while (rc == -EAGAIN);
536 
537 	if (rc)
538 		return rc;
539 
540 	if (winid > VAS_WINDOWS_PER_CHIP) {
541 		pr_err("Too many (%d) open windows\n", winid);
542 		vas_release_window_id(ida, winid);
543 		return -EAGAIN;
544 	}
545 
546 	return winid;
547 }
548 
549 static void vas_window_free(struct vas_window *window)
550 {
551 	int winid = window->winid;
552 	struct vas_instance *vinst = window->vinst;
553 
554 	unmap_winctx_mmio_bars(window);
555 
556 	vas_window_free_dbgdir(window);
557 
558 	kfree(window);
559 
560 	vas_release_window_id(&vinst->ida, winid);
561 }
562 
563 static struct vas_window *vas_window_alloc(struct vas_instance *vinst)
564 {
565 	int winid;
566 	struct vas_window *window;
567 
568 	winid = vas_assign_window_id(&vinst->ida);
569 	if (winid < 0)
570 		return ERR_PTR(winid);
571 
572 	window = kzalloc(sizeof(*window), GFP_KERNEL);
573 	if (!window)
574 		goto out_free;
575 
576 	window->vinst = vinst;
577 	window->winid = winid;
578 
579 	if (map_winctx_mmio_bars(window))
580 		goto out_free;
581 
582 	vas_window_init_dbgdir(window);
583 
584 	return window;
585 
586 out_free:
587 	kfree(window);
588 	vas_release_window_id(&vinst->ida, winid);
589 	return ERR_PTR(-ENOMEM);
590 }
591 
592 static void put_rx_win(struct vas_window *rxwin)
593 {
594 	/* Better not be a send window! */
595 	WARN_ON_ONCE(rxwin->tx_win);
596 
597 	atomic_dec(&rxwin->num_txwins);
598 }
599 
600 /*
601  * Get the VAS receive window associated with NX engine identified
602  * by @cop and if applicable, @pswid.
603  *
604  * See also function header of set_vinst_win().
605  */
606 static struct vas_window *get_vinst_rxwin(struct vas_instance *vinst,
607 			enum vas_cop_type cop, u32 pswid)
608 {
609 	struct vas_window *rxwin;
610 
611 	mutex_lock(&vinst->mutex);
612 
613 	if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI)
614 		rxwin = vinst->rxwin[cop] ?: ERR_PTR(-EINVAL);
615 	else
616 		rxwin = ERR_PTR(-EINVAL);
617 
618 	if (!IS_ERR(rxwin))
619 		atomic_inc(&rxwin->num_txwins);
620 
621 	mutex_unlock(&vinst->mutex);
622 
623 	return rxwin;
624 }
625 
626 /*
627  * We have two tables of windows in a VAS instance. The first one,
628  * ->windows[], contains all the windows in the instance and allows
629  * looking up a window by its id. It is used to look up send windows
630  * during fault handling and receive windows when pairing user space
631  * send/receive windows.
632  *
633  * The second table, ->rxwin[], contains receive windows that are
634  * associated with NX engines. This table has VAS_COP_TYPE_MAX
635  * entries and is used to look up a receive window by its
636  * coprocessor type.
637  *
638  * Here, we save @window in the ->windows[] table. If it is a receive
639  * window, we also save the window in the ->rxwin[] table.
640  */
641 static void set_vinst_win(struct vas_instance *vinst,
642 			struct vas_window *window)
643 {
644 	int id = window->winid;
645 
646 	mutex_lock(&vinst->mutex);
647 
648 	/*
649 	 * There should only be one receive window for a coprocessor type
650 	 * unless its a user (FTW) window.
651 	 */
652 	if (!window->user_win && !window->tx_win) {
653 		WARN_ON_ONCE(vinst->rxwin[window->cop]);
654 		vinst->rxwin[window->cop] = window;
655 	}
656 
657 	WARN_ON_ONCE(vinst->windows[id] != NULL);
658 	vinst->windows[id] = window;
659 
660 	mutex_unlock(&vinst->mutex);
661 }
662 
663 /*
664  * Clear this window from the table(s) of windows for this VAS instance.
665  * See also function header of set_vinst_win().
666  */
667 static void clear_vinst_win(struct vas_window *window)
668 {
669 	int id = window->winid;
670 	struct vas_instance *vinst = window->vinst;
671 
672 	mutex_lock(&vinst->mutex);
673 
674 	if (!window->user_win && !window->tx_win) {
675 		WARN_ON_ONCE(!vinst->rxwin[window->cop]);
676 		vinst->rxwin[window->cop] = NULL;
677 	}
678 
679 	WARN_ON_ONCE(vinst->windows[id] != window);
680 	vinst->windows[id] = NULL;
681 
682 	mutex_unlock(&vinst->mutex);
683 }
684 
685 static void init_winctx_for_rxwin(struct vas_window *rxwin,
686 			struct vas_rx_win_attr *rxattr,
687 			struct vas_winctx *winctx)
688 {
689 	/*
690 	 * We first zero (memset()) all fields and only set non-zero fields.
691 	 * Following fields are 0/false but maybe deserve a comment:
692 	 *
693 	 *	->notify_os_intr_reg	In powerNV, send intrs to HV
694 	 *	->notify_disable	False for NX windows
695 	 *	->intr_disable		False for Fault Windows
696 	 *	->xtra_write		False for NX windows
697 	 *	->notify_early		NA for NX windows
698 	 *	->rsvd_txbuf_count	NA for Rx windows
699 	 *	->lpid, ->pid, ->tid	NA for Rx windows
700 	 */
701 
702 	memset(winctx, 0, sizeof(struct vas_winctx));
703 
704 	winctx->rx_fifo = rxattr->rx_fifo;
705 	winctx->rx_fifo_size = rxattr->rx_fifo_size;
706 	winctx->wcreds_max = rxwin->wcreds_max;
707 	winctx->pin_win = rxattr->pin_win;
708 
709 	winctx->nx_win = rxattr->nx_win;
710 	winctx->fault_win = rxattr->fault_win;
711 	winctx->user_win = rxattr->user_win;
712 	winctx->rej_no_credit = rxattr->rej_no_credit;
713 	winctx->rx_word_mode = rxattr->rx_win_ord_mode;
714 	winctx->tx_word_mode = rxattr->tx_win_ord_mode;
715 	winctx->rx_wcred_mode = rxattr->rx_wcred_mode;
716 	winctx->tx_wcred_mode = rxattr->tx_wcred_mode;
717 	winctx->notify_early = rxattr->notify_early;
718 
719 	if (winctx->nx_win) {
720 		winctx->data_stamp = true;
721 		winctx->intr_disable = true;
722 		winctx->pin_win = true;
723 
724 		WARN_ON_ONCE(winctx->fault_win);
725 		WARN_ON_ONCE(!winctx->rx_word_mode);
726 		WARN_ON_ONCE(!winctx->tx_word_mode);
727 		WARN_ON_ONCE(winctx->notify_after_count);
728 	} else if (winctx->fault_win) {
729 		winctx->notify_disable = true;
730 	} else if (winctx->user_win) {
731 		/*
732 		 * Section 1.8.1 Low Latency Core-Core Wake up of
733 		 * the VAS workbook:
734 		 *
735 		 *      - disable credit checks ([tr]x_wcred_mode = false)
736 		 *      - disable FIFO writes
737 		 *      - enable ASB_Notify, disable interrupt
738 		 */
739 		winctx->fifo_disable = true;
740 		winctx->intr_disable = true;
741 		winctx->rx_fifo = NULL;
742 	}
743 
744 	winctx->lnotify_lpid = rxattr->lnotify_lpid;
745 	winctx->lnotify_pid = rxattr->lnotify_pid;
746 	winctx->lnotify_tid = rxattr->lnotify_tid;
747 	winctx->pswid = rxattr->pswid;
748 	winctx->dma_type = VAS_DMA_TYPE_INJECT;
749 	winctx->tc_mode = rxattr->tc_mode;
750 
751 	winctx->min_scope = VAS_SCOPE_LOCAL;
752 	winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
753 }
754 
755 static bool rx_win_args_valid(enum vas_cop_type cop,
756 			struct vas_rx_win_attr *attr)
757 {
758 	pr_debug("Rxattr: fault %d, notify %d, intr %d, early %d, fifo %d\n",
759 			attr->fault_win, attr->notify_disable,
760 			attr->intr_disable, attr->notify_early,
761 			attr->rx_fifo_size);
762 
763 	if (cop >= VAS_COP_TYPE_MAX)
764 		return false;
765 
766 	if (cop != VAS_COP_TYPE_FTW &&
767 				attr->rx_fifo_size < VAS_RX_FIFO_SIZE_MIN)
768 		return false;
769 
770 	if (attr->rx_fifo_size > VAS_RX_FIFO_SIZE_MAX)
771 		return false;
772 
773 	if (attr->wcreds_max > VAS_RX_WCREDS_MAX)
774 		return false;
775 
776 	if (attr->nx_win) {
777 		/* cannot be fault or user window if it is nx */
778 		if (attr->fault_win || attr->user_win)
779 			return false;
780 		/*
781 		 * Section 3.1.4.32: NX Windows must not disable notification,
782 		 *	and must not enable interrupts or early notification.
783 		 */
784 		if (attr->notify_disable || !attr->intr_disable ||
785 				attr->notify_early)
786 			return false;
787 	} else if (attr->fault_win) {
788 		/* cannot be both fault and user window */
789 		if (attr->user_win)
790 			return false;
791 
792 		/*
793 		 * Section 3.1.4.32: Fault windows must disable notification
794 		 *	but not interrupts.
795 		 */
796 		if (!attr->notify_disable || attr->intr_disable)
797 			return false;
798 
799 	} else if (attr->user_win) {
800 		/*
801 		 * User receive windows are only for fast-thread-wakeup
802 		 * (FTW). They don't need a FIFO and must disable interrupts
803 		 */
804 		if (attr->rx_fifo || attr->rx_fifo_size || !attr->intr_disable)
805 			return false;
806 	} else {
807 		/* Rx window must be one of NX or Fault or User window. */
808 		return false;
809 	}
810 
811 	return true;
812 }
813 
814 void vas_init_rx_win_attr(struct vas_rx_win_attr *rxattr, enum vas_cop_type cop)
815 {
816 	memset(rxattr, 0, sizeof(*rxattr));
817 
818 	if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
819 		rxattr->pin_win = true;
820 		rxattr->nx_win = true;
821 		rxattr->fault_win = false;
822 		rxattr->intr_disable = true;
823 		rxattr->rx_wcred_mode = true;
824 		rxattr->tx_wcred_mode = true;
825 		rxattr->rx_win_ord_mode = true;
826 		rxattr->tx_win_ord_mode = true;
827 	} else if (cop == VAS_COP_TYPE_FAULT) {
828 		rxattr->pin_win = true;
829 		rxattr->fault_win = true;
830 		rxattr->notify_disable = true;
831 		rxattr->rx_wcred_mode = true;
832 		rxattr->tx_wcred_mode = true;
833 		rxattr->rx_win_ord_mode = true;
834 		rxattr->tx_win_ord_mode = true;
835 	} else if (cop == VAS_COP_TYPE_FTW) {
836 		rxattr->user_win = true;
837 		rxattr->intr_disable = true;
838 
839 		/*
840 		 * As noted in the VAS Workbook we disable credit checks.
841 		 * If we enable credit checks in the future, we must also
842 		 * implement a mechanism to return the user credits or new
843 		 * paste operations will fail.
844 		 */
845 	}
846 }
847 EXPORT_SYMBOL_GPL(vas_init_rx_win_attr);
848 
849 struct vas_window *vas_rx_win_open(int vasid, enum vas_cop_type cop,
850 			struct vas_rx_win_attr *rxattr)
851 {
852 	struct vas_window *rxwin;
853 	struct vas_winctx winctx;
854 	struct vas_instance *vinst;
855 
856 	if (!rx_win_args_valid(cop, rxattr))
857 		return ERR_PTR(-EINVAL);
858 
859 	vinst = find_vas_instance(vasid);
860 	if (!vinst) {
861 		pr_devel("vasid %d not found!\n", vasid);
862 		return ERR_PTR(-EINVAL);
863 	}
864 	pr_devel("Found instance %d\n", vasid);
865 
866 	rxwin = vas_window_alloc(vinst);
867 	if (IS_ERR(rxwin)) {
868 		pr_devel("Unable to allocate memory for Rx window\n");
869 		return rxwin;
870 	}
871 
872 	rxwin->tx_win = false;
873 	rxwin->nx_win = rxattr->nx_win;
874 	rxwin->user_win = rxattr->user_win;
875 	rxwin->cop = cop;
876 	rxwin->wcreds_max = rxattr->wcreds_max ?: VAS_WCREDS_DEFAULT;
877 	if (rxattr->user_win)
878 		rxwin->pid = task_pid_vnr(current);
879 
880 	init_winctx_for_rxwin(rxwin, rxattr, &winctx);
881 	init_winctx_regs(rxwin, &winctx);
882 
883 	set_vinst_win(vinst, rxwin);
884 
885 	return rxwin;
886 }
887 EXPORT_SYMBOL_GPL(vas_rx_win_open);
888 
889 void vas_init_tx_win_attr(struct vas_tx_win_attr *txattr, enum vas_cop_type cop)
890 {
891 	memset(txattr, 0, sizeof(*txattr));
892 
893 	if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
894 		txattr->rej_no_credit = false;
895 		txattr->rx_wcred_mode = true;
896 		txattr->tx_wcred_mode = true;
897 		txattr->rx_win_ord_mode = true;
898 		txattr->tx_win_ord_mode = true;
899 	} else if (cop == VAS_COP_TYPE_FTW) {
900 		txattr->user_win = true;
901 	}
902 }
903 EXPORT_SYMBOL_GPL(vas_init_tx_win_attr);
904 
905 static void init_winctx_for_txwin(struct vas_window *txwin,
906 			struct vas_tx_win_attr *txattr,
907 			struct vas_winctx *winctx)
908 {
909 	/*
910 	 * We first zero all fields and only set non-zero ones. Following
911 	 * are some fields set to 0/false for the stated reason:
912 	 *
913 	 *	->notify_os_intr_reg	In powernv, send intrs to HV
914 	 *	->rsvd_txbuf_count	Not supported yet.
915 	 *	->notify_disable	False for NX windows
916 	 *	->xtra_write		False for NX windows
917 	 *	->notify_early		NA for NX windows
918 	 *	->lnotify_lpid		NA for Tx windows
919 	 *	->lnotify_pid		NA for Tx windows
920 	 *	->lnotify_tid		NA for Tx windows
921 	 *	->tx_win_cred_mode	Ignore for now for NX windows
922 	 *	->rx_win_cred_mode	Ignore for now for NX windows
923 	 */
924 	memset(winctx, 0, sizeof(struct vas_winctx));
925 
926 	winctx->wcreds_max = txwin->wcreds_max;
927 
928 	winctx->user_win = txattr->user_win;
929 	winctx->nx_win = txwin->rxwin->nx_win;
930 	winctx->pin_win = txattr->pin_win;
931 	winctx->rej_no_credit = txattr->rej_no_credit;
932 	winctx->rsvd_txbuf_enable = txattr->rsvd_txbuf_enable;
933 
934 	winctx->rx_wcred_mode = txattr->rx_wcred_mode;
935 	winctx->tx_wcred_mode = txattr->tx_wcred_mode;
936 	winctx->rx_word_mode = txattr->rx_win_ord_mode;
937 	winctx->tx_word_mode = txattr->tx_win_ord_mode;
938 	winctx->rsvd_txbuf_count = txattr->rsvd_txbuf_count;
939 
940 	if (winctx->nx_win) {
941 		winctx->data_stamp = true;
942 		winctx->intr_disable = true;
943 	}
944 
945 	winctx->lpid = txattr->lpid;
946 	winctx->pidr = txattr->pidr;
947 	winctx->rx_win_id = txwin->rxwin->winid;
948 
949 	winctx->dma_type = VAS_DMA_TYPE_INJECT;
950 	winctx->tc_mode = txattr->tc_mode;
951 	winctx->min_scope = VAS_SCOPE_LOCAL;
952 	winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
953 
954 	winctx->pswid = 0;
955 }
956 
957 static bool tx_win_args_valid(enum vas_cop_type cop,
958 			struct vas_tx_win_attr *attr)
959 {
960 	if (attr->tc_mode != VAS_THRESH_DISABLED)
961 		return false;
962 
963 	if (cop > VAS_COP_TYPE_MAX)
964 		return false;
965 
966 	if (attr->wcreds_max > VAS_TX_WCREDS_MAX)
967 		return false;
968 
969 	if (attr->user_win &&
970 			(cop != VAS_COP_TYPE_FTW || attr->rsvd_txbuf_count))
971 		return false;
972 
973 	return true;
974 }
975 
976 struct vas_window *vas_tx_win_open(int vasid, enum vas_cop_type cop,
977 			struct vas_tx_win_attr *attr)
978 {
979 	int rc;
980 	struct vas_window *txwin;
981 	struct vas_window *rxwin;
982 	struct vas_winctx winctx;
983 	struct vas_instance *vinst;
984 
985 	if (!tx_win_args_valid(cop, attr))
986 		return ERR_PTR(-EINVAL);
987 
988 	vinst = find_vas_instance(vasid);
989 	if (!vinst) {
990 		pr_devel("vasid %d not found!\n", vasid);
991 		return ERR_PTR(-EINVAL);
992 	}
993 
994 	rxwin = get_vinst_rxwin(vinst, cop, attr->pswid);
995 	if (IS_ERR(rxwin)) {
996 		pr_devel("No RxWin for vasid %d, cop %d\n", vasid, cop);
997 		return rxwin;
998 	}
999 
1000 	txwin = vas_window_alloc(vinst);
1001 	if (IS_ERR(txwin)) {
1002 		rc = PTR_ERR(txwin);
1003 		goto put_rxwin;
1004 	}
1005 
1006 	txwin->cop = cop;
1007 	txwin->tx_win = 1;
1008 	txwin->rxwin = rxwin;
1009 	txwin->nx_win = txwin->rxwin->nx_win;
1010 	txwin->pid = attr->pid;
1011 	txwin->user_win = attr->user_win;
1012 	txwin->wcreds_max = attr->wcreds_max ?: VAS_WCREDS_DEFAULT;
1013 
1014 	init_winctx_for_txwin(txwin, attr, &winctx);
1015 
1016 	init_winctx_regs(txwin, &winctx);
1017 
1018 	/*
1019 	 * If its a kernel send window, map the window address into the
1020 	 * kernel's address space. For user windows, user must issue an
1021 	 * mmap() to map the window into their address space.
1022 	 *
1023 	 * NOTE: If kernel ever resubmits a user CRB after handling a page
1024 	 *	 fault, we will need to map this into kernel as well.
1025 	 */
1026 	if (!txwin->user_win) {
1027 		txwin->paste_kaddr = map_paste_region(txwin);
1028 		if (IS_ERR(txwin->paste_kaddr)) {
1029 			rc = PTR_ERR(txwin->paste_kaddr);
1030 			goto free_window;
1031 		}
1032 	}
1033 
1034 	set_vinst_win(vinst, txwin);
1035 
1036 	return txwin;
1037 
1038 free_window:
1039 	vas_window_free(txwin);
1040 
1041 put_rxwin:
1042 	put_rx_win(rxwin);
1043 	return ERR_PTR(rc);
1044 
1045 }
1046 EXPORT_SYMBOL_GPL(vas_tx_win_open);
1047 
1048 int vas_copy_crb(void *crb, int offset)
1049 {
1050 	return vas_copy(crb, offset);
1051 }
1052 EXPORT_SYMBOL_GPL(vas_copy_crb);
1053 
1054 #define RMA_LSMP_REPORT_ENABLE PPC_BIT(53)
1055 int vas_paste_crb(struct vas_window *txwin, int offset, bool re)
1056 {
1057 	int rc;
1058 	void *addr;
1059 	uint64_t val;
1060 
1061 	/*
1062 	 * Only NX windows are supported for now and hardware assumes
1063 	 * report-enable flag is set for NX windows. Ensure software
1064 	 * complies too.
1065 	 */
1066 	WARN_ON_ONCE(txwin->nx_win && !re);
1067 
1068 	addr = txwin->paste_kaddr;
1069 	if (re) {
1070 		/*
1071 		 * Set the REPORT_ENABLE bit (equivalent to writing
1072 		 * to 1K offset of the paste address)
1073 		 */
1074 		val = SET_FIELD(RMA_LSMP_REPORT_ENABLE, 0ULL, 1);
1075 		addr += val;
1076 	}
1077 
1078 	/*
1079 	 * Map the raw CR value from vas_paste() to an error code (there
1080 	 * is just pass or fail for now though).
1081 	 */
1082 	rc = vas_paste(addr, offset);
1083 	if (rc == 2)
1084 		rc = 0;
1085 	else
1086 		rc = -EINVAL;
1087 
1088 	pr_debug("Txwin #%d: Msg count %llu\n", txwin->winid,
1089 			read_hvwc_reg(txwin, VREG(LRFIFO_PUSH)));
1090 
1091 	return rc;
1092 }
1093 EXPORT_SYMBOL_GPL(vas_paste_crb);
1094 
1095 /*
1096  * If credit checking is enabled for this window, poll for the return
1097  * of window credits (i.e for NX engines to process any outstanding CRBs).
1098  * Since NX-842 waits for the CRBs to be processed before closing the
1099  * window, we should not have to wait for too long.
1100  *
1101  * TODO: We retry in 10ms intervals now. We could/should probably peek at
1102  *	the VAS_LRFIFO_PUSH_OFFSET register to get an estimate of pending
1103  *	CRBs on the FIFO and compute the delay dynamically on each retry.
1104  *	But that is not really needed until we support NX-GZIP access from
1105  *	user space. (NX-842 driver waits for CSB and Fast thread-wakeup
1106  *	doesn't use credit checking).
1107  */
1108 static void poll_window_credits(struct vas_window *window)
1109 {
1110 	u64 val;
1111 	int creds, mode;
1112 
1113 	val = read_hvwc_reg(window, VREG(WINCTL));
1114 	if (window->tx_win)
1115 		mode = GET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val);
1116 	else
1117 		mode = GET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val);
1118 
1119 	if (!mode)
1120 		return;
1121 retry:
1122 	if (window->tx_win) {
1123 		val = read_hvwc_reg(window, VREG(TX_WCRED));
1124 		creds = GET_FIELD(VAS_TX_WCRED, val);
1125 	} else {
1126 		val = read_hvwc_reg(window, VREG(LRX_WCRED));
1127 		creds = GET_FIELD(VAS_LRX_WCRED, val);
1128 	}
1129 
1130 	if (creds < window->wcreds_max) {
1131 		val = 0;
1132 		set_current_state(TASK_UNINTERRUPTIBLE);
1133 		schedule_timeout(msecs_to_jiffies(10));
1134 		goto retry;
1135 	}
1136 }
1137 
1138 /*
1139  * Wait for the window to go to "not-busy" state. It should only take a
1140  * short time to queue a CRB, so window should not be busy for too long.
1141  * Trying 5ms intervals.
1142  */
1143 static void poll_window_busy_state(struct vas_window *window)
1144 {
1145 	int busy;
1146 	u64 val;
1147 
1148 retry:
1149 	val = read_hvwc_reg(window, VREG(WIN_STATUS));
1150 	busy = GET_FIELD(VAS_WIN_BUSY, val);
1151 	if (busy) {
1152 		val = 0;
1153 		set_current_state(TASK_UNINTERRUPTIBLE);
1154 		schedule_timeout(msecs_to_jiffies(5));
1155 		goto retry;
1156 	}
1157 }
1158 
1159 /*
1160  * Have the hardware cast a window out of cache and wait for it to
1161  * be completed.
1162  *
1163  * NOTE: It can take a relatively long time to cast the window context
1164  *	out of the cache. It is not strictly necessary to cast out if:
1165  *
1166  *	- we clear the "Pin Window" bit (so hardware is free to evict)
1167  *
1168  *	- we re-initialize the window context when it is reassigned.
1169  *
1170  *	We do the former in vas_win_close() and latter in vas_win_open().
1171  *	So, ignoring the cast-out for now. We can add it as needed. If
1172  *	casting out becomes necessary we should consider offloading the
1173  *	job to a worker thread, so the window close can proceed quickly.
1174  */
1175 static void poll_window_castout(struct vas_window *window)
1176 {
1177 	/* stub for now */
1178 }
1179 
1180 /*
1181  * Unpin and close a window so no new requests are accepted and the
1182  * hardware can evict this window from cache if necessary.
1183  */
1184 static void unpin_close_window(struct vas_window *window)
1185 {
1186 	u64 val;
1187 
1188 	val = read_hvwc_reg(window, VREG(WINCTL));
1189 	val = SET_FIELD(VAS_WINCTL_PIN, val, 0);
1190 	val = SET_FIELD(VAS_WINCTL_OPEN, val, 0);
1191 	write_hvwc_reg(window, VREG(WINCTL), val);
1192 }
1193 
1194 /*
1195  * Close a window.
1196  *
1197  * See Section 1.12.1 of VAS workbook v1.05 for details on closing window:
1198  *	- Disable new paste operations (unmap paste address)
1199  *	- Poll for the "Window Busy" bit to be cleared
1200  *	- Clear the Open/Enable bit for the Window.
1201  *	- Poll for return of window Credits (implies FIFO empty for Rx win?)
1202  *	- Unpin and cast window context out of cache
1203  *
1204  * Besides the hardware, kernel has some bookkeeping of course.
1205  */
1206 int vas_win_close(struct vas_window *window)
1207 {
1208 	if (!window)
1209 		return 0;
1210 
1211 	if (!window->tx_win && atomic_read(&window->num_txwins) != 0) {
1212 		pr_devel("Attempting to close an active Rx window!\n");
1213 		WARN_ON_ONCE(1);
1214 		return -EBUSY;
1215 	}
1216 
1217 	unmap_paste_region(window);
1218 
1219 	clear_vinst_win(window);
1220 
1221 	poll_window_busy_state(window);
1222 
1223 	unpin_close_window(window);
1224 
1225 	poll_window_credits(window);
1226 
1227 	poll_window_castout(window);
1228 
1229 	/* if send window, drop reference to matching receive window */
1230 	if (window->tx_win)
1231 		put_rx_win(window->rxwin);
1232 
1233 	vas_window_free(window);
1234 
1235 	return 0;
1236 }
1237 EXPORT_SYMBOL_GPL(vas_win_close);
1238