2 * Copyright 2016-17 IBM Corp.
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.
10 #define pr_fmt(fmt) "vas: " fmt
12 #include <linux/types.h>
13 #include <linux/mutex.h>
14 #include <linux/slab.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>
22 #include "copy-paste.h"
25 * Compute the paste address region for the window @window using the
26 * ->paste_base_addr and ->paste_win_id_shift we got from device tree.
28 static void compute_paste_address(struct vas_window
*window
, u64
*addr
, int *len
)
33 base
= window
->vinst
->paste_base_addr
;
34 shift
= window
->vinst
->paste_win_id_shift
;
35 winid
= window
->winid
;
37 *addr
= base
+ (winid
<< shift
);
41 pr_debug("Txwin #%d: Paste addr 0x%llx\n", winid
, *addr
);
44 u64
vas_win_paste_addr(struct vas_window
*win
)
48 compute_paste_address(win
, &addr
, NULL
);
52 EXPORT_SYMBOL(vas_win_paste_addr
);
54 static inline void get_hvwc_mmio_bar(struct vas_window
*window
,
59 pbaddr
= window
->vinst
->hvwc_bar_start
;
60 *start
= pbaddr
+ window
->winid
* VAS_HVWC_SIZE
;
64 static inline void get_uwc_mmio_bar(struct vas_window
*window
,
69 pbaddr
= window
->vinst
->uwc_bar_start
;
70 *start
= pbaddr
+ window
->winid
* VAS_UWC_SIZE
;
75 * Map the paste bus address of the given send window into kernel address
76 * space. Unlike MMIO regions (map_mmio_region() below), paste region must
77 * be mapped cache-able and is only applicable to send windows.
79 static void *map_paste_region(struct vas_window
*txwin
)
86 name
= kasprintf(GFP_KERNEL
, "window-v%d-w%d", txwin
->vinst
->vas_id
,
91 txwin
->paste_addr_name
= name
;
92 compute_paste_address(txwin
, &start
, &len
);
94 if (!request_mem_region(start
, len
, name
)) {
95 pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
96 __func__
, start
, len
);
100 map
= ioremap_cache(start
, len
);
102 pr_devel("%s(): ioremap_cache(0x%llx, %d) failed\n", __func__
,
107 pr_devel("Mapped paste addr 0x%llx to kaddr 0x%p\n", start
, map
);
112 return ERR_PTR(-ENOMEM
);
115 static void *map_mmio_region(char *name
, u64 start
, int len
)
119 if (!request_mem_region(start
, len
, name
)) {
120 pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
121 __func__
, start
, len
);
125 map
= ioremap(start
, len
);
127 pr_devel("%s(): ioremap(0x%llx, %d) failed\n", __func__
, start
,
135 static void unmap_region(void *addr
, u64 start
, int len
)
138 release_mem_region((phys_addr_t
)start
, len
);
142 * Unmap the paste address region for a window.
144 static void unmap_paste_region(struct vas_window
*window
)
149 if (window
->paste_kaddr
) {
150 compute_paste_address(window
, &busaddr_start
, &len
);
151 unmap_region(window
->paste_kaddr
, busaddr_start
, len
);
152 window
->paste_kaddr
= NULL
;
153 kfree(window
->paste_addr_name
);
154 window
->paste_addr_name
= NULL
;
159 * Unmap the MMIO regions for a window. Hold the vas_mutex so we don't
160 * unmap when the window's debugfs dir is in use. This serializes close
161 * of a window even on another VAS instance but since its not a critical
162 * path, just minimize the time we hold the mutex for now. We can add
163 * a per-instance mutex later if necessary.
165 static void unmap_winctx_mmio_bars(struct vas_window
*window
)
172 mutex_lock(&vas_mutex
);
174 hvwc_map
= window
->hvwc_map
;
175 window
->hvwc_map
= NULL
;
177 uwc_map
= window
->uwc_map
;
178 window
->uwc_map
= NULL
;
180 mutex_unlock(&vas_mutex
);
183 get_hvwc_mmio_bar(window
, &busaddr_start
, &len
);
184 unmap_region(hvwc_map
, busaddr_start
, len
);
188 get_uwc_mmio_bar(window
, &busaddr_start
, &len
);
189 unmap_region(uwc_map
, busaddr_start
, len
);
194 * Find the Hypervisor Window Context (HVWC) MMIO Base Address Region and the
195 * OS/User Window Context (UWC) MMIO Base Address Region for the given window.
196 * Map these bus addresses and save the mapped kernel addresses in @window.
198 int map_winctx_mmio_bars(struct vas_window
*window
)
203 get_hvwc_mmio_bar(window
, &start
, &len
);
204 window
->hvwc_map
= map_mmio_region("HVWCM_Window", start
, len
);
206 get_uwc_mmio_bar(window
, &start
, &len
);
207 window
->uwc_map
= map_mmio_region("UWCM_Window", start
, len
);
209 if (!window
->hvwc_map
|| !window
->uwc_map
) {
210 unmap_winctx_mmio_bars(window
);
218 * Reset all valid registers in the HV and OS/User Window Contexts for
219 * the window identified by @window.
221 * NOTE: We cannot really use a for loop to reset window context. Not all
222 * offsets in a window context are valid registers and the valid
223 * registers are not sequential. And, we can only write to offsets
224 * with valid registers.
226 void reset_window_regs(struct vas_window
*window
)
228 write_hvwc_reg(window
, VREG(LPID
), 0ULL);
229 write_hvwc_reg(window
, VREG(PID
), 0ULL);
230 write_hvwc_reg(window
, VREG(XLATE_MSR
), 0ULL);
231 write_hvwc_reg(window
, VREG(XLATE_LPCR
), 0ULL);
232 write_hvwc_reg(window
, VREG(XLATE_CTL
), 0ULL);
233 write_hvwc_reg(window
, VREG(AMR
), 0ULL);
234 write_hvwc_reg(window
, VREG(SEIDR
), 0ULL);
235 write_hvwc_reg(window
, VREG(FAULT_TX_WIN
), 0ULL);
236 write_hvwc_reg(window
, VREG(OSU_INTR_SRC_RA
), 0ULL);
237 write_hvwc_reg(window
, VREG(HV_INTR_SRC_RA
), 0ULL);
238 write_hvwc_reg(window
, VREG(PSWID
), 0ULL);
239 write_hvwc_reg(window
, VREG(LFIFO_BAR
), 0ULL);
240 write_hvwc_reg(window
, VREG(LDATA_STAMP_CTL
), 0ULL);
241 write_hvwc_reg(window
, VREG(LDMA_CACHE_CTL
), 0ULL);
242 write_hvwc_reg(window
, VREG(LRFIFO_PUSH
), 0ULL);
243 write_hvwc_reg(window
, VREG(CURR_MSG_COUNT
), 0ULL);
244 write_hvwc_reg(window
, VREG(LNOTIFY_AFTER_COUNT
), 0ULL);
245 write_hvwc_reg(window
, VREG(LRX_WCRED
), 0ULL);
246 write_hvwc_reg(window
, VREG(LRX_WCRED_ADDER
), 0ULL);
247 write_hvwc_reg(window
, VREG(TX_WCRED
), 0ULL);
248 write_hvwc_reg(window
, VREG(TX_WCRED_ADDER
), 0ULL);
249 write_hvwc_reg(window
, VREG(LFIFO_SIZE
), 0ULL);
250 write_hvwc_reg(window
, VREG(WINCTL
), 0ULL);
251 write_hvwc_reg(window
, VREG(WIN_STATUS
), 0ULL);
252 write_hvwc_reg(window
, VREG(WIN_CTX_CACHING_CTL
), 0ULL);
253 write_hvwc_reg(window
, VREG(TX_RSVD_BUF_COUNT
), 0ULL);
254 write_hvwc_reg(window
, VREG(LRFIFO_WIN_PTR
), 0ULL);
255 write_hvwc_reg(window
, VREG(LNOTIFY_CTL
), 0ULL);
256 write_hvwc_reg(window
, VREG(LNOTIFY_PID
), 0ULL);
257 write_hvwc_reg(window
, VREG(LNOTIFY_LPID
), 0ULL);
258 write_hvwc_reg(window
, VREG(LNOTIFY_TID
), 0ULL);
259 write_hvwc_reg(window
, VREG(LNOTIFY_SCOPE
), 0ULL);
260 write_hvwc_reg(window
, VREG(NX_UTIL_ADDER
), 0ULL);
262 /* Skip read-only registers: NX_UTIL and NX_UTIL_SE */
265 * The send and receive window credit adder registers are also
266 * accessible from HVWC and have been initialized above. We don't
267 * need to initialize from the OS/User Window Context, so skip
270 * write_uwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
271 * write_uwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
276 * Initialize window context registers related to Address Translation.
277 * These registers are common to send/receive windows although they
278 * differ for user/kernel windows. As we resolve the TODOs we may
279 * want to add fields to vas_winctx and move the initialization to
280 * init_vas_winctx_regs().
282 static void init_xlate_regs(struct vas_window
*window
, bool user_win
)
287 * MSR_TA, MSR_US are false for both kernel and user.
288 * MSR_DR and MSR_PR are false for kernel.
291 val
= SET_FIELD(VAS_XLATE_MSR_HV
, val
, 1);
292 val
= SET_FIELD(VAS_XLATE_MSR_SF
, val
, 1);
294 val
= SET_FIELD(VAS_XLATE_MSR_DR
, val
, 1);
295 val
= SET_FIELD(VAS_XLATE_MSR_PR
, val
, 1);
297 write_hvwc_reg(window
, VREG(XLATE_MSR
), val
);
299 lpcr
= mfspr(SPRN_LPCR
);
302 * NOTE: From Section 5.7.8.1 Segment Lookaside Buffer of the
303 * Power ISA, v3.0B, Page size encoding is 0 = 4KB, 5 = 64KB.
305 * NOTE: From Section 1.3.1, Address Translation Context of the
306 * Nest MMU Workbook, LPCR_SC should be 0 for Power9.
308 val
= SET_FIELD(VAS_XLATE_LPCR_PAGE_SIZE
, val
, 5);
309 val
= SET_FIELD(VAS_XLATE_LPCR_ISL
, val
, lpcr
& LPCR_ISL
);
310 val
= SET_FIELD(VAS_XLATE_LPCR_TC
, val
, lpcr
& LPCR_TC
);
311 val
= SET_FIELD(VAS_XLATE_LPCR_SC
, val
, 0);
312 write_hvwc_reg(window
, VREG(XLATE_LPCR
), val
);
315 * Section 1.3.1 (Address translation Context) of NMMU workbook.
316 * 0b00 Hashed Page Table mode
319 * 0b11 Radix on Radix
322 val
= SET_FIELD(VAS_XLATE_MODE
, val
, radix_enabled() ? 3 : 2);
323 write_hvwc_reg(window
, VREG(XLATE_CTL
), val
);
326 * TODO: Can we mfspr(AMR) even for user windows?
329 val
= SET_FIELD(VAS_AMR
, val
, mfspr(SPRN_AMR
));
330 write_hvwc_reg(window
, VREG(AMR
), val
);
333 val
= SET_FIELD(VAS_SEIDR
, val
, 0);
334 write_hvwc_reg(window
, VREG(SEIDR
), val
);
338 * Initialize Reserved Send Buffer Count for the send window. It involves
339 * writing to the register, reading it back to confirm that the hardware
340 * has enough buffers to reserve. See section 1.3.1.2.1 of VAS workbook.
342 * Since we can only make a best-effort attempt to fulfill the request,
343 * we don't return any errors if we cannot.
345 * TODO: Reserved (aka dedicated) send buffers are not supported yet.
347 static void init_rsvd_tx_buf_count(struct vas_window
*txwin
,
348 struct vas_winctx
*winctx
)
350 write_hvwc_reg(txwin
, VREG(TX_RSVD_BUF_COUNT
), 0ULL);
355 * Initialize window context registers for a receive window.
356 * Except for caching control and marking window open, the registers
357 * are initialized in the order listed in Section 3.1.4 (Window Context
358 * Cache Register Details) of the VAS workbook although they don't need
361 * Design note: For NX receive windows, NX allocates the FIFO buffer in OPAL
362 * (so that it can get a large contiguous area) and passes that buffer
363 * to kernel via device tree. We now write that buffer address to the
364 * FIFO BAR. Would it make sense to do this all in OPAL? i.e have OPAL
365 * write the per-chip RX FIFO addresses to the windows during boot-up
366 * as a one-time task? That could work for NX but what about other
367 * receivers? Let the receivers tell us the rx-fifo buffers for now.
369 int init_winctx_regs(struct vas_window
*window
, struct vas_winctx
*winctx
)
374 reset_window_regs(window
);
377 val
= SET_FIELD(VAS_LPID
, val
, winctx
->lpid
);
378 write_hvwc_reg(window
, VREG(LPID
), val
);
381 val
= SET_FIELD(VAS_PID_ID
, val
, winctx
->pidr
);
382 write_hvwc_reg(window
, VREG(PID
), val
);
384 init_xlate_regs(window
, winctx
->user_win
);
387 val
= SET_FIELD(VAS_FAULT_TX_WIN
, val
, 0);
388 write_hvwc_reg(window
, VREG(FAULT_TX_WIN
), val
);
390 /* In PowerNV, interrupts go to HV. */
391 write_hvwc_reg(window
, VREG(OSU_INTR_SRC_RA
), 0ULL);
394 val
= SET_FIELD(VAS_HV_INTR_SRC_RA
, val
, winctx
->irq_port
);
395 write_hvwc_reg(window
, VREG(HV_INTR_SRC_RA
), val
);
398 val
= SET_FIELD(VAS_PSWID_EA_HANDLE
, val
, winctx
->pswid
);
399 write_hvwc_reg(window
, VREG(PSWID
), val
);
401 write_hvwc_reg(window
, VREG(SPARE1
), 0ULL);
402 write_hvwc_reg(window
, VREG(SPARE2
), 0ULL);
403 write_hvwc_reg(window
, VREG(SPARE3
), 0ULL);
406 * NOTE: VAS expects the FIFO address to be copied into the LFIFO_BAR
407 * register as is - do NOT shift the address into VAS_LFIFO_BAR
408 * bit fields! Ok to set the page migration select fields -
409 * VAS ignores the lower 10+ bits in the address anyway, because
410 * the minimum FIFO size is 1K?
412 * See also: Design note in function header.
414 val
= __pa(winctx
->rx_fifo
);
415 val
= SET_FIELD(VAS_PAGE_MIGRATION_SELECT
, val
, 0);
416 write_hvwc_reg(window
, VREG(LFIFO_BAR
), val
);
419 val
= SET_FIELD(VAS_LDATA_STAMP
, val
, winctx
->data_stamp
);
420 write_hvwc_reg(window
, VREG(LDATA_STAMP_CTL
), val
);
423 val
= SET_FIELD(VAS_LDMA_TYPE
, val
, winctx
->dma_type
);
424 val
= SET_FIELD(VAS_LDMA_FIFO_DISABLE
, val
, winctx
->fifo_disable
);
425 write_hvwc_reg(window
, VREG(LDMA_CACHE_CTL
), val
);
427 write_hvwc_reg(window
, VREG(LRFIFO_PUSH
), 0ULL);
428 write_hvwc_reg(window
, VREG(CURR_MSG_COUNT
), 0ULL);
429 write_hvwc_reg(window
, VREG(LNOTIFY_AFTER_COUNT
), 0ULL);
432 val
= SET_FIELD(VAS_LRX_WCRED
, val
, winctx
->wcreds_max
);
433 write_hvwc_reg(window
, VREG(LRX_WCRED
), val
);
436 val
= SET_FIELD(VAS_TX_WCRED
, val
, winctx
->wcreds_max
);
437 write_hvwc_reg(window
, VREG(TX_WCRED
), val
);
439 write_hvwc_reg(window
, VREG(LRX_WCRED_ADDER
), 0ULL);
440 write_hvwc_reg(window
, VREG(TX_WCRED_ADDER
), 0ULL);
442 fifo_size
= winctx
->rx_fifo_size
/ 1024;
445 val
= SET_FIELD(VAS_LFIFO_SIZE
, val
, ilog2(fifo_size
));
446 write_hvwc_reg(window
, VREG(LFIFO_SIZE
), val
);
448 /* Update window control and caching control registers last so
449 * we mark the window open only after fully initializing it and
450 * pushing context to cache.
453 write_hvwc_reg(window
, VREG(WIN_STATUS
), 0ULL);
455 init_rsvd_tx_buf_count(window
, winctx
);
457 /* for a send window, point to the matching receive window */
459 val
= SET_FIELD(VAS_LRX_WIN_ID
, val
, winctx
->rx_win_id
);
460 write_hvwc_reg(window
, VREG(LRFIFO_WIN_PTR
), val
);
462 write_hvwc_reg(window
, VREG(SPARE4
), 0ULL);
465 val
= SET_FIELD(VAS_NOTIFY_DISABLE
, val
, winctx
->notify_disable
);
466 val
= SET_FIELD(VAS_INTR_DISABLE
, val
, winctx
->intr_disable
);
467 val
= SET_FIELD(VAS_NOTIFY_EARLY
, val
, winctx
->notify_early
);
468 val
= SET_FIELD(VAS_NOTIFY_OSU_INTR
, val
, winctx
->notify_os_intr_reg
);
469 write_hvwc_reg(window
, VREG(LNOTIFY_CTL
), val
);
472 val
= SET_FIELD(VAS_LNOTIFY_PID
, val
, winctx
->lnotify_pid
);
473 write_hvwc_reg(window
, VREG(LNOTIFY_PID
), val
);
476 val
= SET_FIELD(VAS_LNOTIFY_LPID
, val
, winctx
->lnotify_lpid
);
477 write_hvwc_reg(window
, VREG(LNOTIFY_LPID
), val
);
480 val
= SET_FIELD(VAS_LNOTIFY_TID
, val
, winctx
->lnotify_tid
);
481 write_hvwc_reg(window
, VREG(LNOTIFY_TID
), val
);
484 val
= SET_FIELD(VAS_LNOTIFY_MIN_SCOPE
, val
, winctx
->min_scope
);
485 val
= SET_FIELD(VAS_LNOTIFY_MAX_SCOPE
, val
, winctx
->max_scope
);
486 write_hvwc_reg(window
, VREG(LNOTIFY_SCOPE
), val
);
488 /* Skip read-only registers NX_UTIL and NX_UTIL_SE */
490 write_hvwc_reg(window
, VREG(SPARE5
), 0ULL);
491 write_hvwc_reg(window
, VREG(NX_UTIL_ADDER
), 0ULL);
492 write_hvwc_reg(window
, VREG(SPARE6
), 0ULL);
494 /* Finally, push window context to memory and... */
496 val
= SET_FIELD(VAS_PUSH_TO_MEM
, val
, 1);
497 write_hvwc_reg(window
, VREG(WIN_CTX_CACHING_CTL
), val
);
499 /* ... mark the window open for business */
501 val
= SET_FIELD(VAS_WINCTL_REJ_NO_CREDIT
, val
, winctx
->rej_no_credit
);
502 val
= SET_FIELD(VAS_WINCTL_PIN
, val
, winctx
->pin_win
);
503 val
= SET_FIELD(VAS_WINCTL_TX_WCRED_MODE
, val
, winctx
->tx_wcred_mode
);
504 val
= SET_FIELD(VAS_WINCTL_RX_WCRED_MODE
, val
, winctx
->rx_wcred_mode
);
505 val
= SET_FIELD(VAS_WINCTL_TX_WORD_MODE
, val
, winctx
->tx_word_mode
);
506 val
= SET_FIELD(VAS_WINCTL_RX_WORD_MODE
, val
, winctx
->rx_word_mode
);
507 val
= SET_FIELD(VAS_WINCTL_FAULT_WIN
, val
, winctx
->fault_win
);
508 val
= SET_FIELD(VAS_WINCTL_NX_WIN
, val
, winctx
->nx_win
);
509 val
= SET_FIELD(VAS_WINCTL_OPEN
, val
, 1);
510 write_hvwc_reg(window
, VREG(WINCTL
), val
);
515 static DEFINE_SPINLOCK(vas_ida_lock
);
517 static void vas_release_window_id(struct ida
*ida
, int winid
)
519 spin_lock(&vas_ida_lock
);
520 ida_remove(ida
, winid
);
521 spin_unlock(&vas_ida_lock
);
524 static int vas_assign_window_id(struct ida
*ida
)
529 rc
= ida_pre_get(ida
, GFP_KERNEL
);
533 spin_lock(&vas_ida_lock
);
534 rc
= ida_get_new(ida
, &winid
);
535 spin_unlock(&vas_ida_lock
);
536 } while (rc
== -EAGAIN
);
541 if (winid
> VAS_WINDOWS_PER_CHIP
) {
542 pr_err("Too many (%d) open windows\n", winid
);
543 vas_release_window_id(ida
, winid
);
550 static void vas_window_free(struct vas_window
*window
)
552 int winid
= window
->winid
;
553 struct vas_instance
*vinst
= window
->vinst
;
555 unmap_winctx_mmio_bars(window
);
557 vas_window_free_dbgdir(window
);
561 vas_release_window_id(&vinst
->ida
, winid
);
564 static struct vas_window
*vas_window_alloc(struct vas_instance
*vinst
)
567 struct vas_window
*window
;
569 winid
= vas_assign_window_id(&vinst
->ida
);
571 return ERR_PTR(winid
);
573 window
= kzalloc(sizeof(*window
), GFP_KERNEL
);
577 window
->vinst
= vinst
;
578 window
->winid
= winid
;
580 if (map_winctx_mmio_bars(window
))
583 vas_window_init_dbgdir(window
);
589 vas_release_window_id(&vinst
->ida
, winid
);
590 return ERR_PTR(-ENOMEM
);
593 static void put_rx_win(struct vas_window
*rxwin
)
595 /* Better not be a send window! */
596 WARN_ON_ONCE(rxwin
->tx_win
);
598 atomic_dec(&rxwin
->num_txwins
);
602 * Find the user space receive window given the @pswid.
603 * - We must have a valid vasid and it must belong to this instance.
604 * (so both send and receive windows are on the same VAS instance)
605 * - The window must refer to an OPEN, FTW, RECEIVE window.
607 * NOTE: We access ->windows[] table and assume that vinst->mutex is held.
609 static struct vas_window
*get_user_rxwin(struct vas_instance
*vinst
, u32 pswid
)
612 struct vas_window
*rxwin
;
614 decode_pswid(pswid
, &vasid
, &winid
);
616 if (vinst
->vas_id
!= vasid
)
617 return ERR_PTR(-EINVAL
);
619 rxwin
= vinst
->windows
[winid
];
621 if (!rxwin
|| rxwin
->tx_win
|| rxwin
->cop
!= VAS_COP_TYPE_FTW
)
622 return ERR_PTR(-EINVAL
);
628 * Get the VAS receive window associated with NX engine identified
629 * by @cop and if applicable, @pswid.
631 * See also function header of set_vinst_win().
633 static struct vas_window
*get_vinst_rxwin(struct vas_instance
*vinst
,
634 enum vas_cop_type cop
, u32 pswid
)
636 struct vas_window
*rxwin
;
638 mutex_lock(&vinst
->mutex
);
640 if (cop
== VAS_COP_TYPE_FTW
)
641 rxwin
= get_user_rxwin(vinst
, pswid
);
643 rxwin
= vinst
->rxwin
[cop
] ?: ERR_PTR(-EINVAL
);
646 atomic_inc(&rxwin
->num_txwins
);
648 mutex_unlock(&vinst
->mutex
);
654 * We have two tables of windows in a VAS instance. The first one,
655 * ->windows[], contains all the windows in the instance and allows
656 * looking up a window by its id. It is used to look up send windows
657 * during fault handling and receive windows when pairing user space
658 * send/receive windows.
660 * The second table, ->rxwin[], contains receive windows that are
661 * associated with NX engines. This table has VAS_COP_TYPE_MAX
662 * entries and is used to look up a receive window by its
665 * Here, we save @window in the ->windows[] table. If it is a receive
666 * window, we also save the window in the ->rxwin[] table.
668 static void set_vinst_win(struct vas_instance
*vinst
,
669 struct vas_window
*window
)
671 int id
= window
->winid
;
673 mutex_lock(&vinst
->mutex
);
676 * There should only be one receive window for a coprocessor type
677 * unless its a user (FTW) window.
679 if (!window
->user_win
&& !window
->tx_win
) {
680 WARN_ON_ONCE(vinst
->rxwin
[window
->cop
]);
681 vinst
->rxwin
[window
->cop
] = window
;
684 WARN_ON_ONCE(vinst
->windows
[id
] != NULL
);
685 vinst
->windows
[id
] = window
;
687 mutex_unlock(&vinst
->mutex
);
691 * Clear this window from the table(s) of windows for this VAS instance.
692 * See also function header of set_vinst_win().
694 static void clear_vinst_win(struct vas_window
*window
)
696 int id
= window
->winid
;
697 struct vas_instance
*vinst
= window
->vinst
;
699 mutex_lock(&vinst
->mutex
);
701 if (!window
->user_win
&& !window
->tx_win
) {
702 WARN_ON_ONCE(!vinst
->rxwin
[window
->cop
]);
703 vinst
->rxwin
[window
->cop
] = NULL
;
706 WARN_ON_ONCE(vinst
->windows
[id
] != window
);
707 vinst
->windows
[id
] = NULL
;
709 mutex_unlock(&vinst
->mutex
);
712 static void init_winctx_for_rxwin(struct vas_window
*rxwin
,
713 struct vas_rx_win_attr
*rxattr
,
714 struct vas_winctx
*winctx
)
717 * We first zero (memset()) all fields and only set non-zero fields.
718 * Following fields are 0/false but maybe deserve a comment:
720 * ->notify_os_intr_reg In powerNV, send intrs to HV
721 * ->notify_disable False for NX windows
722 * ->intr_disable False for Fault Windows
723 * ->xtra_write False for NX windows
724 * ->notify_early NA for NX windows
725 * ->rsvd_txbuf_count NA for Rx windows
726 * ->lpid, ->pid, ->tid NA for Rx windows
729 memset(winctx
, 0, sizeof(struct vas_winctx
));
731 winctx
->rx_fifo
= rxattr
->rx_fifo
;
732 winctx
->rx_fifo_size
= rxattr
->rx_fifo_size
;
733 winctx
->wcreds_max
= rxwin
->wcreds_max
;
734 winctx
->pin_win
= rxattr
->pin_win
;
736 winctx
->nx_win
= rxattr
->nx_win
;
737 winctx
->fault_win
= rxattr
->fault_win
;
738 winctx
->user_win
= rxattr
->user_win
;
739 winctx
->rej_no_credit
= rxattr
->rej_no_credit
;
740 winctx
->rx_word_mode
= rxattr
->rx_win_ord_mode
;
741 winctx
->tx_word_mode
= rxattr
->tx_win_ord_mode
;
742 winctx
->rx_wcred_mode
= rxattr
->rx_wcred_mode
;
743 winctx
->tx_wcred_mode
= rxattr
->tx_wcred_mode
;
744 winctx
->notify_early
= rxattr
->notify_early
;
746 if (winctx
->nx_win
) {
747 winctx
->data_stamp
= true;
748 winctx
->intr_disable
= true;
749 winctx
->pin_win
= true;
751 WARN_ON_ONCE(winctx
->fault_win
);
752 WARN_ON_ONCE(!winctx
->rx_word_mode
);
753 WARN_ON_ONCE(!winctx
->tx_word_mode
);
754 WARN_ON_ONCE(winctx
->notify_after_count
);
755 } else if (winctx
->fault_win
) {
756 winctx
->notify_disable
= true;
757 } else if (winctx
->user_win
) {
759 * Section 1.8.1 Low Latency Core-Core Wake up of
762 * - disable credit checks ([tr]x_wcred_mode = false)
763 * - disable FIFO writes
764 * - enable ASB_Notify, disable interrupt
766 winctx
->fifo_disable
= true;
767 winctx
->intr_disable
= true;
768 winctx
->rx_fifo
= NULL
;
771 winctx
->lnotify_lpid
= rxattr
->lnotify_lpid
;
772 winctx
->lnotify_pid
= rxattr
->lnotify_pid
;
773 winctx
->lnotify_tid
= rxattr
->lnotify_tid
;
774 winctx
->pswid
= rxattr
->pswid
;
775 winctx
->dma_type
= VAS_DMA_TYPE_INJECT
;
776 winctx
->tc_mode
= rxattr
->tc_mode
;
778 winctx
->min_scope
= VAS_SCOPE_LOCAL
;
779 winctx
->max_scope
= VAS_SCOPE_VECTORED_GROUP
;
782 static bool rx_win_args_valid(enum vas_cop_type cop
,
783 struct vas_rx_win_attr
*attr
)
785 pr_debug("Rxattr: fault %d, notify %d, intr %d, early %d, fifo %d\n",
786 attr
->fault_win
, attr
->notify_disable
,
787 attr
->intr_disable
, attr
->notify_early
,
790 if (cop
>= VAS_COP_TYPE_MAX
)
793 if (cop
!= VAS_COP_TYPE_FTW
&&
794 attr
->rx_fifo_size
< VAS_RX_FIFO_SIZE_MIN
)
797 if (attr
->rx_fifo_size
> VAS_RX_FIFO_SIZE_MAX
)
800 if (attr
->wcreds_max
> VAS_RX_WCREDS_MAX
)
804 /* cannot be fault or user window if it is nx */
805 if (attr
->fault_win
|| attr
->user_win
)
808 * Section 3.1.4.32: NX Windows must not disable notification,
809 * and must not enable interrupts or early notification.
811 if (attr
->notify_disable
|| !attr
->intr_disable
||
814 } else if (attr
->fault_win
) {
815 /* cannot be both fault and user window */
820 * Section 3.1.4.32: Fault windows must disable notification
821 * but not interrupts.
823 if (!attr
->notify_disable
|| attr
->intr_disable
)
826 } else if (attr
->user_win
) {
828 * User receive windows are only for fast-thread-wakeup
829 * (FTW). They don't need a FIFO and must disable interrupts
831 if (attr
->rx_fifo
|| attr
->rx_fifo_size
|| !attr
->intr_disable
)
834 /* Rx window must be one of NX or Fault or User window. */
841 void vas_init_rx_win_attr(struct vas_rx_win_attr
*rxattr
, enum vas_cop_type cop
)
843 memset(rxattr
, 0, sizeof(*rxattr
));
845 if (cop
== VAS_COP_TYPE_842
|| cop
== VAS_COP_TYPE_842_HIPRI
) {
846 rxattr
->pin_win
= true;
847 rxattr
->nx_win
= true;
848 rxattr
->fault_win
= false;
849 rxattr
->intr_disable
= true;
850 rxattr
->rx_wcred_mode
= true;
851 rxattr
->tx_wcred_mode
= true;
852 rxattr
->rx_win_ord_mode
= true;
853 rxattr
->tx_win_ord_mode
= true;
854 } else if (cop
== VAS_COP_TYPE_FAULT
) {
855 rxattr
->pin_win
= true;
856 rxattr
->fault_win
= true;
857 rxattr
->notify_disable
= true;
858 rxattr
->rx_wcred_mode
= true;
859 rxattr
->tx_wcred_mode
= true;
860 rxattr
->rx_win_ord_mode
= true;
861 rxattr
->tx_win_ord_mode
= true;
862 } else if (cop
== VAS_COP_TYPE_FTW
) {
863 rxattr
->user_win
= true;
864 rxattr
->intr_disable
= true;
867 * As noted in the VAS Workbook we disable credit checks.
868 * If we enable credit checks in the future, we must also
869 * implement a mechanism to return the user credits or new
870 * paste operations will fail.
874 EXPORT_SYMBOL_GPL(vas_init_rx_win_attr
);
876 struct vas_window
*vas_rx_win_open(int vasid
, enum vas_cop_type cop
,
877 struct vas_rx_win_attr
*rxattr
)
879 struct vas_window
*rxwin
;
880 struct vas_winctx winctx
;
881 struct vas_instance
*vinst
;
883 if (!rx_win_args_valid(cop
, rxattr
))
884 return ERR_PTR(-EINVAL
);
886 vinst
= find_vas_instance(vasid
);
888 pr_devel("vasid %d not found!\n", vasid
);
889 return ERR_PTR(-EINVAL
);
891 pr_devel("Found instance %d\n", vasid
);
893 rxwin
= vas_window_alloc(vinst
);
895 pr_devel("Unable to allocate memory for Rx window\n");
899 rxwin
->tx_win
= false;
900 rxwin
->nx_win
= rxattr
->nx_win
;
901 rxwin
->user_win
= rxattr
->user_win
;
903 rxwin
->wcreds_max
= rxattr
->wcreds_max
?: VAS_WCREDS_DEFAULT
;
904 if (rxattr
->user_win
)
905 rxwin
->pid
= task_pid_vnr(current
);
907 init_winctx_for_rxwin(rxwin
, rxattr
, &winctx
);
908 init_winctx_regs(rxwin
, &winctx
);
910 set_vinst_win(vinst
, rxwin
);
914 EXPORT_SYMBOL_GPL(vas_rx_win_open
);
916 void vas_init_tx_win_attr(struct vas_tx_win_attr
*txattr
, enum vas_cop_type cop
)
918 memset(txattr
, 0, sizeof(*txattr
));
920 if (cop
== VAS_COP_TYPE_842
|| cop
== VAS_COP_TYPE_842_HIPRI
) {
921 txattr
->rej_no_credit
= false;
922 txattr
->rx_wcred_mode
= true;
923 txattr
->tx_wcred_mode
= true;
924 txattr
->rx_win_ord_mode
= true;
925 txattr
->tx_win_ord_mode
= true;
926 } else if (cop
== VAS_COP_TYPE_FTW
) {
927 txattr
->user_win
= true;
930 EXPORT_SYMBOL_GPL(vas_init_tx_win_attr
);
932 static void init_winctx_for_txwin(struct vas_window
*txwin
,
933 struct vas_tx_win_attr
*txattr
,
934 struct vas_winctx
*winctx
)
937 * We first zero all fields and only set non-zero ones. Following
938 * are some fields set to 0/false for the stated reason:
940 * ->notify_os_intr_reg In powernv, send intrs to HV
941 * ->rsvd_txbuf_count Not supported yet.
942 * ->notify_disable False for NX windows
943 * ->xtra_write False for NX windows
944 * ->notify_early NA for NX windows
945 * ->lnotify_lpid NA for Tx windows
946 * ->lnotify_pid NA for Tx windows
947 * ->lnotify_tid NA for Tx windows
948 * ->tx_win_cred_mode Ignore for now for NX windows
949 * ->rx_win_cred_mode Ignore for now for NX windows
951 memset(winctx
, 0, sizeof(struct vas_winctx
));
953 winctx
->wcreds_max
= txwin
->wcreds_max
;
955 winctx
->user_win
= txattr
->user_win
;
956 winctx
->nx_win
= txwin
->rxwin
->nx_win
;
957 winctx
->pin_win
= txattr
->pin_win
;
958 winctx
->rej_no_credit
= txattr
->rej_no_credit
;
959 winctx
->rsvd_txbuf_enable
= txattr
->rsvd_txbuf_enable
;
961 winctx
->rx_wcred_mode
= txattr
->rx_wcred_mode
;
962 winctx
->tx_wcred_mode
= txattr
->tx_wcred_mode
;
963 winctx
->rx_word_mode
= txattr
->rx_win_ord_mode
;
964 winctx
->tx_word_mode
= txattr
->tx_win_ord_mode
;
965 winctx
->rsvd_txbuf_count
= txattr
->rsvd_txbuf_count
;
967 winctx
->intr_disable
= true;
969 winctx
->data_stamp
= true;
971 winctx
->lpid
= txattr
->lpid
;
972 winctx
->pidr
= txattr
->pidr
;
973 winctx
->rx_win_id
= txwin
->rxwin
->winid
;
975 winctx
->dma_type
= VAS_DMA_TYPE_INJECT
;
976 winctx
->tc_mode
= txattr
->tc_mode
;
977 winctx
->min_scope
= VAS_SCOPE_LOCAL
;
978 winctx
->max_scope
= VAS_SCOPE_VECTORED_GROUP
;
983 static bool tx_win_args_valid(enum vas_cop_type cop
,
984 struct vas_tx_win_attr
*attr
)
986 if (attr
->tc_mode
!= VAS_THRESH_DISABLED
)
989 if (cop
> VAS_COP_TYPE_MAX
)
992 if (attr
->wcreds_max
> VAS_TX_WCREDS_MAX
)
995 if (attr
->user_win
&&
996 (cop
!= VAS_COP_TYPE_FTW
|| attr
->rsvd_txbuf_count
))
1002 struct vas_window
*vas_tx_win_open(int vasid
, enum vas_cop_type cop
,
1003 struct vas_tx_win_attr
*attr
)
1006 struct vas_window
*txwin
;
1007 struct vas_window
*rxwin
;
1008 struct vas_winctx winctx
;
1009 struct vas_instance
*vinst
;
1011 if (!tx_win_args_valid(cop
, attr
))
1012 return ERR_PTR(-EINVAL
);
1015 * If caller did not specify a vasid but specified the PSWID of a
1016 * receive window (applicable only to FTW windows), use the vasid
1017 * from that receive window.
1019 if (vasid
== -1 && attr
->pswid
)
1020 decode_pswid(attr
->pswid
, &vasid
, NULL
);
1022 vinst
= find_vas_instance(vasid
);
1024 pr_devel("vasid %d not found!\n", vasid
);
1025 return ERR_PTR(-EINVAL
);
1028 rxwin
= get_vinst_rxwin(vinst
, cop
, attr
->pswid
);
1029 if (IS_ERR(rxwin
)) {
1030 pr_devel("No RxWin for vasid %d, cop %d\n", vasid
, cop
);
1034 txwin
= vas_window_alloc(vinst
);
1035 if (IS_ERR(txwin
)) {
1036 rc
= PTR_ERR(txwin
);
1042 txwin
->rxwin
= rxwin
;
1043 txwin
->nx_win
= txwin
->rxwin
->nx_win
;
1044 txwin
->pid
= attr
->pid
;
1045 txwin
->user_win
= attr
->user_win
;
1046 txwin
->wcreds_max
= attr
->wcreds_max
?: VAS_WCREDS_DEFAULT
;
1048 init_winctx_for_txwin(txwin
, attr
, &winctx
);
1050 init_winctx_regs(txwin
, &winctx
);
1053 * If its a kernel send window, map the window address into the
1054 * kernel's address space. For user windows, user must issue an
1055 * mmap() to map the window into their address space.
1057 * NOTE: If kernel ever resubmits a user CRB after handling a page
1058 * fault, we will need to map this into kernel as well.
1060 if (!txwin
->user_win
) {
1061 txwin
->paste_kaddr
= map_paste_region(txwin
);
1062 if (IS_ERR(txwin
->paste_kaddr
)) {
1063 rc
= PTR_ERR(txwin
->paste_kaddr
);
1068 * A user mapping must ensure that context switch issues
1069 * CP_ABORT for this thread.
1071 rc
= set_thread_uses_vas();
1076 set_vinst_win(vinst
, txwin
);
1081 vas_window_free(txwin
);
1088 EXPORT_SYMBOL_GPL(vas_tx_win_open
);
1090 int vas_copy_crb(void *crb
, int offset
)
1092 return vas_copy(crb
, offset
);
1094 EXPORT_SYMBOL_GPL(vas_copy_crb
);
1096 #define RMA_LSMP_REPORT_ENABLE PPC_BIT(53)
1097 int vas_paste_crb(struct vas_window
*txwin
, int offset
, bool re
)
1104 * Only NX windows are supported for now and hardware assumes
1105 * report-enable flag is set for NX windows. Ensure software
1108 WARN_ON_ONCE(txwin
->nx_win
&& !re
);
1110 addr
= txwin
->paste_kaddr
;
1113 * Set the REPORT_ENABLE bit (equivalent to writing
1114 * to 1K offset of the paste address)
1116 val
= SET_FIELD(RMA_LSMP_REPORT_ENABLE
, 0ULL, 1);
1121 * Map the raw CR value from vas_paste() to an error code (there
1122 * is just pass or fail for now though).
1124 rc
= vas_paste(addr
, offset
);
1130 pr_debug("Txwin #%d: Msg count %llu\n", txwin
->winid
,
1131 read_hvwc_reg(txwin
, VREG(LRFIFO_PUSH
)));
1135 EXPORT_SYMBOL_GPL(vas_paste_crb
);
1138 * If credit checking is enabled for this window, poll for the return
1139 * of window credits (i.e for NX engines to process any outstanding CRBs).
1140 * Since NX-842 waits for the CRBs to be processed before closing the
1141 * window, we should not have to wait for too long.
1143 * TODO: We retry in 10ms intervals now. We could/should probably peek at
1144 * the VAS_LRFIFO_PUSH_OFFSET register to get an estimate of pending
1145 * CRBs on the FIFO and compute the delay dynamically on each retry.
1146 * But that is not really needed until we support NX-GZIP access from
1147 * user space. (NX-842 driver waits for CSB and Fast thread-wakeup
1148 * doesn't use credit checking).
1150 static void poll_window_credits(struct vas_window
*window
)
1155 val
= read_hvwc_reg(window
, VREG(WINCTL
));
1157 mode
= GET_FIELD(VAS_WINCTL_TX_WCRED_MODE
, val
);
1159 mode
= GET_FIELD(VAS_WINCTL_RX_WCRED_MODE
, val
);
1164 if (window
->tx_win
) {
1165 val
= read_hvwc_reg(window
, VREG(TX_WCRED
));
1166 creds
= GET_FIELD(VAS_TX_WCRED
, val
);
1168 val
= read_hvwc_reg(window
, VREG(LRX_WCRED
));
1169 creds
= GET_FIELD(VAS_LRX_WCRED
, val
);
1172 if (creds
< window
->wcreds_max
) {
1174 set_current_state(TASK_UNINTERRUPTIBLE
);
1175 schedule_timeout(msecs_to_jiffies(10));
1181 * Wait for the window to go to "not-busy" state. It should only take a
1182 * short time to queue a CRB, so window should not be busy for too long.
1183 * Trying 5ms intervals.
1185 static void poll_window_busy_state(struct vas_window
*window
)
1191 val
= read_hvwc_reg(window
, VREG(WIN_STATUS
));
1192 busy
= GET_FIELD(VAS_WIN_BUSY
, val
);
1195 set_current_state(TASK_UNINTERRUPTIBLE
);
1196 schedule_timeout(msecs_to_jiffies(5));
1202 * Have the hardware cast a window out of cache and wait for it to
1205 * NOTE: It can take a relatively long time to cast the window context
1206 * out of the cache. It is not strictly necessary to cast out if:
1208 * - we clear the "Pin Window" bit (so hardware is free to evict)
1210 * - we re-initialize the window context when it is reassigned.
1212 * We do the former in vas_win_close() and latter in vas_win_open().
1213 * So, ignoring the cast-out for now. We can add it as needed. If
1214 * casting out becomes necessary we should consider offloading the
1215 * job to a worker thread, so the window close can proceed quickly.
1217 static void poll_window_castout(struct vas_window
*window
)
1223 * Unpin and close a window so no new requests are accepted and the
1224 * hardware can evict this window from cache if necessary.
1226 static void unpin_close_window(struct vas_window
*window
)
1230 val
= read_hvwc_reg(window
, VREG(WINCTL
));
1231 val
= SET_FIELD(VAS_WINCTL_PIN
, val
, 0);
1232 val
= SET_FIELD(VAS_WINCTL_OPEN
, val
, 0);
1233 write_hvwc_reg(window
, VREG(WINCTL
), val
);
1239 * See Section 1.12.1 of VAS workbook v1.05 for details on closing window:
1240 * - Disable new paste operations (unmap paste address)
1241 * - Poll for the "Window Busy" bit to be cleared
1242 * - Clear the Open/Enable bit for the Window.
1243 * - Poll for return of window Credits (implies FIFO empty for Rx win?)
1244 * - Unpin and cast window context out of cache
1246 * Besides the hardware, kernel has some bookkeeping of course.
1248 int vas_win_close(struct vas_window
*window
)
1253 if (!window
->tx_win
&& atomic_read(&window
->num_txwins
) != 0) {
1254 pr_devel("Attempting to close an active Rx window!\n");
1259 unmap_paste_region(window
);
1261 clear_vinst_win(window
);
1263 poll_window_busy_state(window
);
1265 unpin_close_window(window
);
1267 poll_window_credits(window
);
1269 poll_window_castout(window
);
1271 /* if send window, drop reference to matching receive window */
1273 put_rx_win(window
->rxwin
);
1275 vas_window_free(window
);
1279 EXPORT_SYMBOL_GPL(vas_win_close
);
1282 * Return a system-wide unique window id for the window @win.
1284 u32
vas_win_id(struct vas_window
*win
)
1286 return encode_pswid(win
->vinst
->vas_id
, win
->winid
);
1288 EXPORT_SYMBOL_GPL(vas_win_id
);