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