powerpc/vas: Add platform specific user window operations
[linux-2.6-microblaze.git] / arch / powerpc / platforms / powernv / vas-window.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright 2016-17 IBM Corp.
4  */
5
6 #define pr_fmt(fmt) "vas: " fmt
7
8 #include <linux/types.h>
9 #include <linux/mutex.h>
10 #include <linux/slab.h>
11 #include <linux/io.h>
12 #include <linux/log2.h>
13 #include <linux/rcupdate.h>
14 #include <linux/cred.h>
15 #include <linux/sched/mm.h>
16 #include <linux/mmu_context.h>
17 #include <asm/switch_to.h>
18 #include <asm/ppc-opcode.h>
19 #include <asm/vas.h>
20 #include "vas.h"
21 #include "copy-paste.h"
22
23 #define CREATE_TRACE_POINTS
24 #include "vas-trace.h"
25
26 /*
27  * Compute the paste address region for the window @window using the
28  * ->paste_base_addr and ->paste_win_id_shift we got from device tree.
29  */
30 void vas_win_paste_addr(struct vas_window *window, u64 *addr, int *len)
31 {
32         int winid;
33         u64 base, shift;
34
35         base = window->vinst->paste_base_addr;
36         shift = window->vinst->paste_win_id_shift;
37         winid = window->winid;
38
39         *addr  = base + (winid << shift);
40         if (len)
41                 *len = PAGE_SIZE;
42
43         pr_debug("Txwin #%d: Paste addr 0x%llx\n", winid, *addr);
44 }
45
46 static inline void get_hvwc_mmio_bar(struct vas_window *window,
47                         u64 *start, int *len)
48 {
49         u64 pbaddr;
50
51         pbaddr = window->vinst->hvwc_bar_start;
52         *start = pbaddr + window->winid * VAS_HVWC_SIZE;
53         *len = VAS_HVWC_SIZE;
54 }
55
56 static inline void get_uwc_mmio_bar(struct vas_window *window,
57                         u64 *start, int *len)
58 {
59         u64 pbaddr;
60
61         pbaddr = window->vinst->uwc_bar_start;
62         *start = pbaddr + window->winid * VAS_UWC_SIZE;
63         *len = VAS_UWC_SIZE;
64 }
65
66 /*
67  * Map the paste bus address of the given send window into kernel address
68  * space. Unlike MMIO regions (map_mmio_region() below), paste region must
69  * be mapped cache-able and is only applicable to send windows.
70  */
71 static void *map_paste_region(struct vas_window *txwin)
72 {
73         int len;
74         void *map;
75         char *name;
76         u64 start;
77
78         name = kasprintf(GFP_KERNEL, "window-v%d-w%d", txwin->vinst->vas_id,
79                                 txwin->winid);
80         if (!name)
81                 goto free_name;
82
83         txwin->paste_addr_name = name;
84         vas_win_paste_addr(txwin, &start, &len);
85
86         if (!request_mem_region(start, len, name)) {
87                 pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
88                                 __func__, start, len);
89                 goto free_name;
90         }
91
92         map = ioremap_cache(start, len);
93         if (!map) {
94                 pr_devel("%s(): ioremap_cache(0x%llx, %d) failed\n", __func__,
95                                 start, len);
96                 goto free_name;
97         }
98
99         pr_devel("Mapped paste addr 0x%llx to kaddr 0x%p\n", start, map);
100         return map;
101
102 free_name:
103         kfree(name);
104         return ERR_PTR(-ENOMEM);
105 }
106
107 static void *map_mmio_region(char *name, u64 start, int len)
108 {
109         void *map;
110
111         if (!request_mem_region(start, len, name)) {
112                 pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
113                                 __func__, start, len);
114                 return NULL;
115         }
116
117         map = ioremap(start, len);
118         if (!map) {
119                 pr_devel("%s(): ioremap(0x%llx, %d) failed\n", __func__, start,
120                                 len);
121                 return NULL;
122         }
123
124         return map;
125 }
126
127 static void unmap_region(void *addr, u64 start, int len)
128 {
129         iounmap(addr);
130         release_mem_region((phys_addr_t)start, len);
131 }
132
133 /*
134  * Unmap the paste address region for a window.
135  */
136 static void unmap_paste_region(struct vas_window *window)
137 {
138         int len;
139         u64 busaddr_start;
140
141         if (window->paste_kaddr) {
142                 vas_win_paste_addr(window, &busaddr_start, &len);
143                 unmap_region(window->paste_kaddr, busaddr_start, len);
144                 window->paste_kaddr = NULL;
145                 kfree(window->paste_addr_name);
146                 window->paste_addr_name = NULL;
147         }
148 }
149
150 /*
151  * Unmap the MMIO regions for a window. Hold the vas_mutex so we don't
152  * unmap when the window's debugfs dir is in use. This serializes close
153  * of a window even on another VAS instance but since its not a critical
154  * path, just minimize the time we hold the mutex for now. We can add
155  * a per-instance mutex later if necessary.
156  */
157 static void unmap_winctx_mmio_bars(struct vas_window *window)
158 {
159         int len;
160         void *uwc_map;
161         void *hvwc_map;
162         u64 busaddr_start;
163
164         mutex_lock(&vas_mutex);
165
166         hvwc_map = window->hvwc_map;
167         window->hvwc_map = NULL;
168
169         uwc_map = window->uwc_map;
170         window->uwc_map = NULL;
171
172         mutex_unlock(&vas_mutex);
173
174         if (hvwc_map) {
175                 get_hvwc_mmio_bar(window, &busaddr_start, &len);
176                 unmap_region(hvwc_map, busaddr_start, len);
177         }
178
179         if (uwc_map) {
180                 get_uwc_mmio_bar(window, &busaddr_start, &len);
181                 unmap_region(uwc_map, busaddr_start, len);
182         }
183 }
184
185 /*
186  * Find the Hypervisor Window Context (HVWC) MMIO Base Address Region and the
187  * OS/User Window Context (UWC) MMIO Base Address Region for the given window.
188  * Map these bus addresses and save the mapped kernel addresses in @window.
189  */
190 static int map_winctx_mmio_bars(struct vas_window *window)
191 {
192         int len;
193         u64 start;
194
195         get_hvwc_mmio_bar(window, &start, &len);
196         window->hvwc_map = map_mmio_region("HVWCM_Window", start, len);
197
198         get_uwc_mmio_bar(window, &start, &len);
199         window->uwc_map = map_mmio_region("UWCM_Window", start, len);
200
201         if (!window->hvwc_map || !window->uwc_map) {
202                 unmap_winctx_mmio_bars(window);
203                 return -1;
204         }
205
206         return 0;
207 }
208
209 /*
210  * Reset all valid registers in the HV and OS/User Window Contexts for
211  * the window identified by @window.
212  *
213  * NOTE: We cannot really use a for loop to reset window context. Not all
214  *       offsets in a window context are valid registers and the valid
215  *       registers are not sequential. And, we can only write to offsets
216  *       with valid registers.
217  */
218 static void reset_window_regs(struct vas_window *window)
219 {
220         write_hvwc_reg(window, VREG(LPID), 0ULL);
221         write_hvwc_reg(window, VREG(PID), 0ULL);
222         write_hvwc_reg(window, VREG(XLATE_MSR), 0ULL);
223         write_hvwc_reg(window, VREG(XLATE_LPCR), 0ULL);
224         write_hvwc_reg(window, VREG(XLATE_CTL), 0ULL);
225         write_hvwc_reg(window, VREG(AMR), 0ULL);
226         write_hvwc_reg(window, VREG(SEIDR), 0ULL);
227         write_hvwc_reg(window, VREG(FAULT_TX_WIN), 0ULL);
228         write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
229         write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), 0ULL);
230         write_hvwc_reg(window, VREG(PSWID), 0ULL);
231         write_hvwc_reg(window, VREG(LFIFO_BAR), 0ULL);
232         write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), 0ULL);
233         write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), 0ULL);
234         write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
235         write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
236         write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
237         write_hvwc_reg(window, VREG(LRX_WCRED), 0ULL);
238         write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
239         write_hvwc_reg(window, VREG(TX_WCRED), 0ULL);
240         write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
241         write_hvwc_reg(window, VREG(LFIFO_SIZE), 0ULL);
242         write_hvwc_reg(window, VREG(WINCTL), 0ULL);
243         write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
244         write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), 0ULL);
245         write_hvwc_reg(window, VREG(TX_RSVD_BUF_COUNT), 0ULL);
246         write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), 0ULL);
247         write_hvwc_reg(window, VREG(LNOTIFY_CTL), 0ULL);
248         write_hvwc_reg(window, VREG(LNOTIFY_PID), 0ULL);
249         write_hvwc_reg(window, VREG(LNOTIFY_LPID), 0ULL);
250         write_hvwc_reg(window, VREG(LNOTIFY_TID), 0ULL);
251         write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), 0ULL);
252         write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
253
254         /* Skip read-only registers: NX_UTIL and NX_UTIL_SE */
255
256         /*
257          * The send and receive window credit adder registers are also
258          * accessible from HVWC and have been initialized above. We don't
259          * need to initialize from the OS/User Window Context, so skip
260          * following calls:
261          *
262          *      write_uwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
263          *      write_uwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
264          */
265 }
266
267 /*
268  * Initialize window context registers related to Address Translation.
269  * These registers are common to send/receive windows although they
270  * differ for user/kernel windows. As we resolve the TODOs we may
271  * want to add fields to vas_winctx and move the initialization to
272  * init_vas_winctx_regs().
273  */
274 static void init_xlate_regs(struct vas_window *window, bool user_win)
275 {
276         u64 lpcr, val;
277
278         /*
279          * MSR_TA, MSR_US are false for both kernel and user.
280          * MSR_DR and MSR_PR are false for kernel.
281          */
282         val = 0ULL;
283         val = SET_FIELD(VAS_XLATE_MSR_HV, val, 1);
284         val = SET_FIELD(VAS_XLATE_MSR_SF, val, 1);
285         if (user_win) {
286                 val = SET_FIELD(VAS_XLATE_MSR_DR, val, 1);
287                 val = SET_FIELD(VAS_XLATE_MSR_PR, val, 1);
288         }
289         write_hvwc_reg(window, VREG(XLATE_MSR), val);
290
291         lpcr = mfspr(SPRN_LPCR);
292         val = 0ULL;
293         /*
294          * NOTE: From Section 5.7.8.1 Segment Lookaside Buffer of the
295          *       Power ISA, v3.0B, Page size encoding is 0 = 4KB, 5 = 64KB.
296          *
297          * NOTE: From Section 1.3.1, Address Translation Context of the
298          *       Nest MMU Workbook, LPCR_SC should be 0 for Power9.
299          */
300         val = SET_FIELD(VAS_XLATE_LPCR_PAGE_SIZE, val, 5);
301         val = SET_FIELD(VAS_XLATE_LPCR_ISL, val, lpcr & LPCR_ISL);
302         val = SET_FIELD(VAS_XLATE_LPCR_TC, val, lpcr & LPCR_TC);
303         val = SET_FIELD(VAS_XLATE_LPCR_SC, val, 0);
304         write_hvwc_reg(window, VREG(XLATE_LPCR), val);
305
306         /*
307          * Section 1.3.1 (Address translation Context) of NMMU workbook.
308          *      0b00    Hashed Page Table mode
309          *      0b01    Reserved
310          *      0b10    Radix on HPT
311          *      0b11    Radix on Radix
312          */
313         val = 0ULL;
314         val = SET_FIELD(VAS_XLATE_MODE, val, radix_enabled() ? 3 : 2);
315         write_hvwc_reg(window, VREG(XLATE_CTL), val);
316
317         /*
318          * TODO: Can we mfspr(AMR) even for user windows?
319          */
320         val = 0ULL;
321         val = SET_FIELD(VAS_AMR, val, mfspr(SPRN_AMR));
322         write_hvwc_reg(window, VREG(AMR), val);
323
324         val = 0ULL;
325         val = SET_FIELD(VAS_SEIDR, val, 0);
326         write_hvwc_reg(window, VREG(SEIDR), val);
327 }
328
329 /*
330  * Initialize Reserved Send Buffer Count for the send window. It involves
331  * writing to the register, reading it back to confirm that the hardware
332  * has enough buffers to reserve. See section 1.3.1.2.1 of VAS workbook.
333  *
334  * Since we can only make a best-effort attempt to fulfill the request,
335  * we don't return any errors if we cannot.
336  *
337  * TODO: Reserved (aka dedicated) send buffers are not supported yet.
338  */
339 static void init_rsvd_tx_buf_count(struct vas_window *txwin,
340                                 struct vas_winctx *winctx)
341 {
342         write_hvwc_reg(txwin, VREG(TX_RSVD_BUF_COUNT), 0ULL);
343 }
344
345 /*
346  * init_winctx_regs()
347  *      Initialize window context registers for a receive window.
348  *      Except for caching control and marking window open, the registers
349  *      are initialized in the order listed in Section 3.1.4 (Window Context
350  *      Cache Register Details) of the VAS workbook although they don't need
351  *      to be.
352  *
353  * Design note: For NX receive windows, NX allocates the FIFO buffer in OPAL
354  *      (so that it can get a large contiguous area) and passes that buffer
355  *      to kernel via device tree. We now write that buffer address to the
356  *      FIFO BAR. Would it make sense to do this all in OPAL? i.e have OPAL
357  *      write the per-chip RX FIFO addresses to the windows during boot-up
358  *      as a one-time task? That could work for NX but what about other
359  *      receivers?  Let the receivers tell us the rx-fifo buffers for now.
360  */
361 static void init_winctx_regs(struct vas_window *window,
362                              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, winctx->fault_win_id);
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
506 static void vas_release_window_id(struct ida *ida, int winid)
507 {
508         ida_free(ida, winid);
509 }
510
511 static int vas_assign_window_id(struct ida *ida)
512 {
513         int winid = ida_alloc_max(ida, VAS_WINDOWS_PER_CHIP - 1, GFP_KERNEL);
514
515         if (winid == -ENOSPC) {
516                 pr_err("Too many (%d) open windows\n", VAS_WINDOWS_PER_CHIP);
517                 return -EAGAIN;
518         }
519
520         return winid;
521 }
522
523 static void vas_window_free(struct vas_window *window)
524 {
525         int winid = window->winid;
526         struct vas_instance *vinst = window->vinst;
527
528         unmap_winctx_mmio_bars(window);
529
530         vas_window_free_dbgdir(window);
531
532         kfree(window);
533
534         vas_release_window_id(&vinst->ida, winid);
535 }
536
537 static struct vas_window *vas_window_alloc(struct vas_instance *vinst)
538 {
539         int winid;
540         struct vas_window *window;
541
542         winid = vas_assign_window_id(&vinst->ida);
543         if (winid < 0)
544                 return ERR_PTR(winid);
545
546         window = kzalloc(sizeof(*window), GFP_KERNEL);
547         if (!window)
548                 goto out_free;
549
550         window->vinst = vinst;
551         window->winid = winid;
552
553         if (map_winctx_mmio_bars(window))
554                 goto out_free;
555
556         vas_window_init_dbgdir(window);
557
558         return window;
559
560 out_free:
561         kfree(window);
562         vas_release_window_id(&vinst->ida, winid);
563         return ERR_PTR(-ENOMEM);
564 }
565
566 static void put_rx_win(struct vas_window *rxwin)
567 {
568         /* Better not be a send window! */
569         WARN_ON_ONCE(rxwin->tx_win);
570
571         atomic_dec(&rxwin->num_txwins);
572 }
573
574 /*
575  * Find the user space receive window given the @pswid.
576  *      - We must have a valid vasid and it must belong to this instance.
577  *        (so both send and receive windows are on the same VAS instance)
578  *      - The window must refer to an OPEN, FTW, RECEIVE window.
579  *
580  * NOTE: We access ->windows[] table and assume that vinst->mutex is held.
581  */
582 static struct vas_window *get_user_rxwin(struct vas_instance *vinst, u32 pswid)
583 {
584         int vasid, winid;
585         struct vas_window *rxwin;
586
587         decode_pswid(pswid, &vasid, &winid);
588
589         if (vinst->vas_id != vasid)
590                 return ERR_PTR(-EINVAL);
591
592         rxwin = vinst->windows[winid];
593
594         if (!rxwin || rxwin->tx_win || rxwin->cop != VAS_COP_TYPE_FTW)
595                 return ERR_PTR(-EINVAL);
596
597         return rxwin;
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_FTW)
614                 rxwin = get_user_rxwin(vinst, pswid);
615         else
616                 rxwin = vinst->rxwin[cop] ?: 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         if (rxwin->vinst->virq)
754                 winctx->irq_port = rxwin->vinst->irq_port;
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)
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                 cop == VAS_COP_TYPE_GZIP || cop == VAS_COP_TYPE_GZIP_HIPRI) {
822                 rxattr->pin_win = true;
823                 rxattr->nx_win = true;
824                 rxattr->fault_win = false;
825                 rxattr->intr_disable = true;
826                 rxattr->rx_wcred_mode = true;
827                 rxattr->tx_wcred_mode = true;
828                 rxattr->rx_win_ord_mode = true;
829                 rxattr->tx_win_ord_mode = true;
830         } else if (cop == VAS_COP_TYPE_FAULT) {
831                 rxattr->pin_win = true;
832                 rxattr->fault_win = true;
833                 rxattr->notify_disable = true;
834                 rxattr->rx_wcred_mode = true;
835                 rxattr->rx_win_ord_mode = true;
836                 rxattr->rej_no_credit = true;
837                 rxattr->tc_mode = VAS_THRESH_DISABLED;
838         } else if (cop == VAS_COP_TYPE_FTW) {
839                 rxattr->user_win = true;
840                 rxattr->intr_disable = true;
841
842                 /*
843                  * As noted in the VAS Workbook we disable credit checks.
844                  * If we enable credit checks in the future, we must also
845                  * implement a mechanism to return the user credits or new
846                  * paste operations will fail.
847                  */
848         }
849 }
850 EXPORT_SYMBOL_GPL(vas_init_rx_win_attr);
851
852 struct vas_window *vas_rx_win_open(int vasid, enum vas_cop_type cop,
853                         struct vas_rx_win_attr *rxattr)
854 {
855         struct vas_window *rxwin;
856         struct vas_winctx winctx;
857         struct vas_instance *vinst;
858
859         trace_vas_rx_win_open(current, vasid, cop, rxattr);
860
861         if (!rx_win_args_valid(cop, rxattr))
862                 return ERR_PTR(-EINVAL);
863
864         vinst = find_vas_instance(vasid);
865         if (!vinst) {
866                 pr_devel("vasid %d not found!\n", vasid);
867                 return ERR_PTR(-EINVAL);
868         }
869         pr_devel("Found instance %d\n", vasid);
870
871         rxwin = vas_window_alloc(vinst);
872         if (IS_ERR(rxwin)) {
873                 pr_devel("Unable to allocate memory for Rx window\n");
874                 return rxwin;
875         }
876
877         rxwin->tx_win = false;
878         rxwin->nx_win = rxattr->nx_win;
879         rxwin->user_win = rxattr->user_win;
880         rxwin->cop = cop;
881         rxwin->wcreds_max = rxattr->wcreds_max;
882
883         init_winctx_for_rxwin(rxwin, rxattr, &winctx);
884         init_winctx_regs(rxwin, &winctx);
885
886         set_vinst_win(vinst, rxwin);
887
888         return rxwin;
889 }
890 EXPORT_SYMBOL_GPL(vas_rx_win_open);
891
892 void vas_init_tx_win_attr(struct vas_tx_win_attr *txattr, enum vas_cop_type cop)
893 {
894         memset(txattr, 0, sizeof(*txattr));
895
896         if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI ||
897                 cop == VAS_COP_TYPE_GZIP || cop == VAS_COP_TYPE_GZIP_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          * IRQ and fault window setup is successful. Set fault window
953          * for the send window so that ready to handle faults.
954          */
955         if (txwin->vinst->virq)
956                 winctx->fault_win_id = txwin->vinst->fault_win->winid;
957
958         winctx->dma_type = VAS_DMA_TYPE_INJECT;
959         winctx->tc_mode = txattr->tc_mode;
960         winctx->min_scope = VAS_SCOPE_LOCAL;
961         winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
962         if (txwin->vinst->virq)
963                 winctx->irq_port = txwin->vinst->irq_port;
964
965         winctx->pswid = txattr->pswid ? txattr->pswid :
966                         encode_pswid(txwin->vinst->vas_id, txwin->winid);
967 }
968
969 static bool tx_win_args_valid(enum vas_cop_type cop,
970                         struct vas_tx_win_attr *attr)
971 {
972         if (attr->tc_mode != VAS_THRESH_DISABLED)
973                 return false;
974
975         if (cop > VAS_COP_TYPE_MAX)
976                 return false;
977
978         if (attr->wcreds_max > VAS_TX_WCREDS_MAX)
979                 return false;
980
981         if (attr->user_win) {
982                 if (attr->rsvd_txbuf_count)
983                         return false;
984
985                 if (cop != VAS_COP_TYPE_FTW && cop != VAS_COP_TYPE_GZIP &&
986                         cop != VAS_COP_TYPE_GZIP_HIPRI)
987                         return false;
988         }
989
990         return true;
991 }
992
993 struct vas_window *vas_tx_win_open(int vasid, enum vas_cop_type cop,
994                         struct vas_tx_win_attr *attr)
995 {
996         int rc;
997         struct vas_window *txwin;
998         struct vas_window *rxwin;
999         struct vas_winctx winctx;
1000         struct vas_instance *vinst;
1001
1002         trace_vas_tx_win_open(current, vasid, cop, attr);
1003
1004         if (!tx_win_args_valid(cop, attr))
1005                 return ERR_PTR(-EINVAL);
1006
1007         /*
1008          * If caller did not specify a vasid but specified the PSWID of a
1009          * receive window (applicable only to FTW windows), use the vasid
1010          * from that receive window.
1011          */
1012         if (vasid == -1 && attr->pswid)
1013                 decode_pswid(attr->pswid, &vasid, NULL);
1014
1015         vinst = find_vas_instance(vasid);
1016         if (!vinst) {
1017                 pr_devel("vasid %d not found!\n", vasid);
1018                 return ERR_PTR(-EINVAL);
1019         }
1020
1021         rxwin = get_vinst_rxwin(vinst, cop, attr->pswid);
1022         if (IS_ERR(rxwin)) {
1023                 pr_devel("No RxWin for vasid %d, cop %d\n", vasid, cop);
1024                 return rxwin;
1025         }
1026
1027         txwin = vas_window_alloc(vinst);
1028         if (IS_ERR(txwin)) {
1029                 rc = PTR_ERR(txwin);
1030                 goto put_rxwin;
1031         }
1032
1033         txwin->cop = cop;
1034         txwin->tx_win = 1;
1035         txwin->rxwin = rxwin;
1036         txwin->nx_win = txwin->rxwin->nx_win;
1037         txwin->user_win = attr->user_win;
1038         txwin->wcreds_max = attr->wcreds_max ?: VAS_WCREDS_DEFAULT;
1039
1040         init_winctx_for_txwin(txwin, attr, &winctx);
1041
1042         init_winctx_regs(txwin, &winctx);
1043
1044         /*
1045          * If its a kernel send window, map the window address into the
1046          * kernel's address space. For user windows, user must issue an
1047          * mmap() to map the window into their address space.
1048          *
1049          * NOTE: If kernel ever resubmits a user CRB after handling a page
1050          *       fault, we will need to map this into kernel as well.
1051          */
1052         if (!txwin->user_win) {
1053                 txwin->paste_kaddr = map_paste_region(txwin);
1054                 if (IS_ERR(txwin->paste_kaddr)) {
1055                         rc = PTR_ERR(txwin->paste_kaddr);
1056                         goto free_window;
1057                 }
1058         } else {
1059                 /*
1060                  * Interrupt hanlder or fault window setup failed. Means
1061                  * NX can not generate fault for page fault. So not
1062                  * opening for user space tx window.
1063                  */
1064                 if (!vinst->virq) {
1065                         rc = -ENODEV;
1066                         goto free_window;
1067                 }
1068
1069                 /*
1070                  * Window opened by a child thread may not be closed when
1071                  * it exits. So take reference to its pid and release it
1072                  * when the window is free by parent thread.
1073                  * Acquire a reference to the task's pid to make sure
1074                  * pid will not be re-used - needed only for multithread
1075                  * applications.
1076                  */
1077                 txwin->pid = get_task_pid(current, PIDTYPE_PID);
1078                 /*
1079                  * Acquire a reference to the task's mm.
1080                  */
1081                 txwin->mm = get_task_mm(current);
1082
1083                 if (!txwin->mm) {
1084                         put_pid(txwin->pid);
1085                         pr_err("VAS: pid(%d): mm_struct is not found\n",
1086                                         current->pid);
1087                         rc = -EPERM;
1088                         goto free_window;
1089                 }
1090
1091                 mmgrab(txwin->mm);
1092                 mmput(txwin->mm);
1093                 mm_context_add_vas_window(txwin->mm);
1094                 /*
1095                  * Process closes window during exit. In the case of
1096                  * multithread application, the child thread can open
1097                  * window and can exit without closing it. so takes tgid
1098                  * reference until window closed to make sure tgid is not
1099                  * reused.
1100                  */
1101                 txwin->tgid = find_get_pid(task_tgid_vnr(current));
1102                 /*
1103                  * Even a process that has no foreign real address mapping can
1104                  * use an unpaired COPY instruction (to no real effect). Issue
1105                  * CP_ABORT to clear any pending COPY and prevent a covert
1106                  * channel.
1107                  *
1108                  * __switch_to() will issue CP_ABORT on future context switches
1109                  * if process / thread has any open VAS window (Use
1110                  * current->mm->context.vas_windows).
1111                  */
1112                 asm volatile(PPC_CP_ABORT);
1113         }
1114
1115         set_vinst_win(vinst, txwin);
1116
1117         return txwin;
1118
1119 free_window:
1120         vas_window_free(txwin);
1121
1122 put_rxwin:
1123         put_rx_win(rxwin);
1124         return ERR_PTR(rc);
1125
1126 }
1127 EXPORT_SYMBOL_GPL(vas_tx_win_open);
1128
1129 int vas_copy_crb(void *crb, int offset)
1130 {
1131         return vas_copy(crb, offset);
1132 }
1133 EXPORT_SYMBOL_GPL(vas_copy_crb);
1134
1135 #define RMA_LSMP_REPORT_ENABLE PPC_BIT(53)
1136 int vas_paste_crb(struct vas_window *txwin, int offset, bool re)
1137 {
1138         int rc;
1139         void *addr;
1140         uint64_t val;
1141
1142         trace_vas_paste_crb(current, txwin);
1143
1144         /*
1145          * Only NX windows are supported for now and hardware assumes
1146          * report-enable flag is set for NX windows. Ensure software
1147          * complies too.
1148          */
1149         WARN_ON_ONCE(txwin->nx_win && !re);
1150
1151         addr = txwin->paste_kaddr;
1152         if (re) {
1153                 /*
1154                  * Set the REPORT_ENABLE bit (equivalent to writing
1155                  * to 1K offset of the paste address)
1156                  */
1157                 val = SET_FIELD(RMA_LSMP_REPORT_ENABLE, 0ULL, 1);
1158                 addr += val;
1159         }
1160
1161         /*
1162          * Map the raw CR value from vas_paste() to an error code (there
1163          * is just pass or fail for now though).
1164          */
1165         rc = vas_paste(addr, offset);
1166         if (rc == 2)
1167                 rc = 0;
1168         else
1169                 rc = -EINVAL;
1170
1171         pr_debug("Txwin #%d: Msg count %llu\n", txwin->winid,
1172                         read_hvwc_reg(txwin, VREG(LRFIFO_PUSH)));
1173
1174         return rc;
1175 }
1176 EXPORT_SYMBOL_GPL(vas_paste_crb);
1177
1178 /*
1179  * If credit checking is enabled for this window, poll for the return
1180  * of window credits (i.e for NX engines to process any outstanding CRBs).
1181  * Since NX-842 waits for the CRBs to be processed before closing the
1182  * window, we should not have to wait for too long.
1183  *
1184  * TODO: We retry in 10ms intervals now. We could/should probably peek at
1185  *      the VAS_LRFIFO_PUSH_OFFSET register to get an estimate of pending
1186  *      CRBs on the FIFO and compute the delay dynamically on each retry.
1187  *      But that is not really needed until we support NX-GZIP access from
1188  *      user space. (NX-842 driver waits for CSB and Fast thread-wakeup
1189  *      doesn't use credit checking).
1190  */
1191 static void poll_window_credits(struct vas_window *window)
1192 {
1193         u64 val;
1194         int creds, mode;
1195         int count = 0;
1196
1197         val = read_hvwc_reg(window, VREG(WINCTL));
1198         if (window->tx_win)
1199                 mode = GET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val);
1200         else
1201                 mode = GET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val);
1202
1203         if (!mode)
1204                 return;
1205 retry:
1206         if (window->tx_win) {
1207                 val = read_hvwc_reg(window, VREG(TX_WCRED));
1208                 creds = GET_FIELD(VAS_TX_WCRED, val);
1209         } else {
1210                 val = read_hvwc_reg(window, VREG(LRX_WCRED));
1211                 creds = GET_FIELD(VAS_LRX_WCRED, val);
1212         }
1213
1214         /*
1215          * Takes around few milliseconds to complete all pending requests
1216          * and return credits.
1217          * TODO: Scan fault FIFO and invalidate CRBs points to this window
1218          *       and issue CRB Kill to stop all pending requests. Need only
1219          *       if there is a bug in NX or fault handling in kernel.
1220          */
1221         if (creds < window->wcreds_max) {
1222                 val = 0;
1223                 set_current_state(TASK_UNINTERRUPTIBLE);
1224                 schedule_timeout(msecs_to_jiffies(10));
1225                 count++;
1226                 /*
1227                  * Process can not close send window until all credits are
1228                  * returned.
1229                  */
1230                 if (!(count % 1000))
1231                         pr_warn_ratelimited("VAS: pid %d stuck. Waiting for credits returned for Window(%d). creds %d, Retries %d\n",
1232                                 vas_window_pid(window), window->winid,
1233                                 creds, count);
1234
1235                 goto retry;
1236         }
1237 }
1238
1239 /*
1240  * Wait for the window to go to "not-busy" state. It should only take a
1241  * short time to queue a CRB, so window should not be busy for too long.
1242  * Trying 5ms intervals.
1243  */
1244 static void poll_window_busy_state(struct vas_window *window)
1245 {
1246         int busy;
1247         u64 val;
1248         int count = 0;
1249
1250 retry:
1251         val = read_hvwc_reg(window, VREG(WIN_STATUS));
1252         busy = GET_FIELD(VAS_WIN_BUSY, val);
1253         if (busy) {
1254                 val = 0;
1255                 set_current_state(TASK_UNINTERRUPTIBLE);
1256                 schedule_timeout(msecs_to_jiffies(10));
1257                 count++;
1258                 /*
1259                  * Takes around few milliseconds to process all pending
1260                  * requests.
1261                  */
1262                 if (!(count % 1000))
1263                         pr_warn_ratelimited("VAS: pid %d stuck. Window (ID=%d) is in busy state. Retries %d\n",
1264                                 vas_window_pid(window), window->winid, count);
1265
1266                 goto retry;
1267         }
1268 }
1269
1270 /*
1271  * Have the hardware cast a window out of cache and wait for it to
1272  * be completed.
1273  *
1274  * NOTE: It can take a relatively long time to cast the window context
1275  *      out of the cache. It is not strictly necessary to cast out if:
1276  *
1277  *      - we clear the "Pin Window" bit (so hardware is free to evict)
1278  *
1279  *      - we re-initialize the window context when it is reassigned.
1280  *
1281  *      We do the former in vas_win_close() and latter in vas_win_open().
1282  *      So, ignoring the cast-out for now. We can add it as needed. If
1283  *      casting out becomes necessary we should consider offloading the
1284  *      job to a worker thread, so the window close can proceed quickly.
1285  */
1286 static void poll_window_castout(struct vas_window *window)
1287 {
1288         /* stub for now */
1289 }
1290
1291 /*
1292  * Unpin and close a window so no new requests are accepted and the
1293  * hardware can evict this window from cache if necessary.
1294  */
1295 static void unpin_close_window(struct vas_window *window)
1296 {
1297         u64 val;
1298
1299         val = read_hvwc_reg(window, VREG(WINCTL));
1300         val = SET_FIELD(VAS_WINCTL_PIN, val, 0);
1301         val = SET_FIELD(VAS_WINCTL_OPEN, val, 0);
1302         write_hvwc_reg(window, VREG(WINCTL), val);
1303 }
1304
1305 /*
1306  * Close a window.
1307  *
1308  * See Section 1.12.1 of VAS workbook v1.05 for details on closing window:
1309  *      - Disable new paste operations (unmap paste address)
1310  *      - Poll for the "Window Busy" bit to be cleared
1311  *      - Clear the Open/Enable bit for the Window.
1312  *      - Poll for return of window Credits (implies FIFO empty for Rx win?)
1313  *      - Unpin and cast window context out of cache
1314  *
1315  * Besides the hardware, kernel has some bookkeeping of course.
1316  */
1317 int vas_win_close(struct vas_window *window)
1318 {
1319         if (!window)
1320                 return 0;
1321
1322         if (!window->tx_win && atomic_read(&window->num_txwins) != 0) {
1323                 pr_devel("Attempting to close an active Rx window!\n");
1324                 WARN_ON_ONCE(1);
1325                 return -EBUSY;
1326         }
1327
1328         unmap_paste_region(window);
1329
1330         poll_window_busy_state(window);
1331
1332         unpin_close_window(window);
1333
1334         poll_window_credits(window);
1335
1336         clear_vinst_win(window);
1337
1338         poll_window_castout(window);
1339
1340         /* if send window, drop reference to matching receive window */
1341         if (window->tx_win) {
1342                 if (window->user_win) {
1343                         /* Drop references to pid. tgid and mm */
1344                         put_pid(window->pid);
1345                         put_pid(window->tgid);
1346                         if (window->mm) {
1347                                 mm_context_remove_vas_window(window->mm);
1348                                 mmdrop(window->mm);
1349                         }
1350                 }
1351                 put_rx_win(window->rxwin);
1352         }
1353
1354         vas_window_free(window);
1355
1356         return 0;
1357 }
1358 EXPORT_SYMBOL_GPL(vas_win_close);
1359
1360 /*
1361  * Return credit for the given window.
1362  * Send windows and fault window uses credit mechanism as follows:
1363  *
1364  * Send windows:
1365  * - The default number of credits available for each send window is
1366  *   1024. It means 1024 requests can be issued asynchronously at the
1367  *   same time. If the credit is not available, that request will be
1368  *   returned with RMA_Busy.
1369  * - One credit is taken when NX request is issued.
1370  * - This credit is returned after NX processed that request.
1371  * - If NX encounters translation error, kernel will return the
1372  *   credit on the specific send window after processing the fault CRB.
1373  *
1374  * Fault window:
1375  * - The total number credits available is FIFO_SIZE/CRB_SIZE.
1376  *   Means 4MB/128 in the current implementation. If credit is not
1377  *   available, RMA_Reject is returned.
1378  * - A credit is taken when NX pastes CRB in fault FIFO.
1379  * - The kernel with return credit on fault window after reading entry
1380  *   from fault FIFO.
1381  */
1382 void vas_return_credit(struct vas_window *window, bool tx)
1383 {
1384         uint64_t val;
1385
1386         val = 0ULL;
1387         if (tx) { /* send window */
1388                 val = SET_FIELD(VAS_TX_WCRED, val, 1);
1389                 write_hvwc_reg(window, VREG(TX_WCRED_ADDER), val);
1390         } else {
1391                 val = SET_FIELD(VAS_LRX_WCRED, val, 1);
1392                 write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), val);
1393         }
1394 }
1395
1396 struct vas_window *vas_pswid_to_window(struct vas_instance *vinst,
1397                 uint32_t pswid)
1398 {
1399         struct vas_window *window;
1400         int winid;
1401
1402         if (!pswid) {
1403                 pr_devel("%s: called for pswid 0!\n", __func__);
1404                 return ERR_PTR(-ESRCH);
1405         }
1406
1407         decode_pswid(pswid, NULL, &winid);
1408
1409         if (winid >= VAS_WINDOWS_PER_CHIP)
1410                 return ERR_PTR(-ESRCH);
1411
1412         /*
1413          * If application closes the window before the hardware
1414          * returns the fault CRB, we should wait in vas_win_close()
1415          * for the pending requests. so the window must be active
1416          * and the process alive.
1417          *
1418          * If its a kernel process, we should not get any faults and
1419          * should not get here.
1420          */
1421         window = vinst->windows[winid];
1422
1423         if (!window) {
1424                 pr_err("PSWID decode: Could not find window for winid %d pswid %d vinst 0x%p\n",
1425                         winid, pswid, vinst);
1426                 return NULL;
1427         }
1428
1429         /*
1430          * Do some sanity checks on the decoded window.  Window should be
1431          * NX GZIP user send window. FTW windows should not incur faults
1432          * since their CRBs are ignored (not queued on FIFO or processed
1433          * by NX).
1434          */
1435         if (!window->tx_win || !window->user_win || !window->nx_win ||
1436                         window->cop == VAS_COP_TYPE_FAULT ||
1437                         window->cop == VAS_COP_TYPE_FTW) {
1438                 pr_err("PSWID decode: id %d, tx %d, user %d, nx %d, cop %d\n",
1439                         winid, window->tx_win, window->user_win,
1440                         window->nx_win, window->cop);
1441                 WARN_ON(1);
1442         }
1443
1444         return window;
1445 }
1446
1447 static struct vas_window *vas_user_win_open(int vas_id, u64 flags,
1448                                 enum vas_cop_type cop_type)
1449 {
1450         struct vas_tx_win_attr txattr = {};
1451
1452         vas_init_tx_win_attr(&txattr, cop_type);
1453
1454         txattr.lpid = mfspr(SPRN_LPID);
1455         txattr.pidr = mfspr(SPRN_PID);
1456         txattr.user_win = true;
1457         txattr.rsvd_txbuf_count = false;
1458         txattr.pswid = false;
1459
1460         pr_devel("Pid %d: Opening txwin, PIDR %ld\n", txattr.pidr,
1461                                 mfspr(SPRN_PID));
1462
1463         return vas_tx_win_open(vas_id, cop_type, &txattr);
1464 }
1465
1466 static u64 vas_user_win_paste_addr(struct vas_window *win)
1467 {
1468         u64 paste_addr;
1469
1470         vas_win_paste_addr(win, &paste_addr, NULL);
1471
1472         return paste_addr;
1473 }
1474
1475 static int vas_user_win_close(struct vas_window *txwin)
1476 {
1477
1478         vas_win_close(txwin);
1479
1480         return 0;
1481 }
1482
1483 static const struct vas_user_win_ops vops =  {
1484         .open_win       =       vas_user_win_open,
1485         .paste_addr     =       vas_user_win_paste_addr,
1486         .close_win      =       vas_user_win_close,
1487 };
1488
1489 /*
1490  * Supporting only nx-gzip coprocessor type now, but this API code
1491  * extended to other coprocessor types later.
1492  */
1493 int vas_register_api_powernv(struct module *mod, enum vas_cop_type cop_type,
1494                              const char *name)
1495 {
1496
1497         return vas_register_coproc_api(mod, cop_type, name, &vops);
1498 }
1499 EXPORT_SYMBOL_GPL(vas_register_api_powernv);
1500
1501 void vas_unregister_api_powernv(void)
1502 {
1503         vas_unregister_coproc_api();
1504 }
1505 EXPORT_SYMBOL_GPL(vas_unregister_api_powernv);