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"
24 #define CREATE_TRACE_POINTS
25 #include "vas-trace.h"
28 * Compute the paste address region for the window @window using the
29 * ->paste_base_addr and ->paste_win_id_shift we got from device tree.
31 static void compute_paste_address(struct vas_window *window, u64 *addr, int *len)
36 base = window->vinst->paste_base_addr;
37 shift = window->vinst->paste_win_id_shift;
38 winid = window->winid;
40 *addr = base + (winid << shift);
44 pr_debug("Txwin #%d: Paste addr 0x%llx\n", winid, *addr);
47 u64 vas_win_paste_addr(struct vas_window *win)
51 compute_paste_address(win, &addr, NULL);
55 EXPORT_SYMBOL(vas_win_paste_addr);
57 static inline void get_hvwc_mmio_bar(struct vas_window *window,
62 pbaddr = window->vinst->hvwc_bar_start;
63 *start = pbaddr + window->winid * VAS_HVWC_SIZE;
67 static inline void get_uwc_mmio_bar(struct vas_window *window,
72 pbaddr = window->vinst->uwc_bar_start;
73 *start = pbaddr + window->winid * VAS_UWC_SIZE;
78 * Map the paste bus address of the given send window into kernel address
79 * space. Unlike MMIO regions (map_mmio_region() below), paste region must
80 * be mapped cache-able and is only applicable to send windows.
82 static void *map_paste_region(struct vas_window *txwin)
89 name = kasprintf(GFP_KERNEL, "window-v%d-w%d", txwin->vinst->vas_id,
94 txwin->paste_addr_name = name;
95 compute_paste_address(txwin, &start, &len);
97 if (!request_mem_region(start, len, name)) {
98 pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
99 __func__, start, len);
103 map = ioremap_cache(start, len);
105 pr_devel("%s(): ioremap_cache(0x%llx, %d) failed\n", __func__,
110 pr_devel("Mapped paste addr 0x%llx to kaddr 0x%p\n", start, map);
115 return ERR_PTR(-ENOMEM);
118 static void *map_mmio_region(char *name, u64 start, int len)
122 if (!request_mem_region(start, len, name)) {
123 pr_devel("%s(): request_mem_region(0x%llx, %d) failed\n",
124 __func__, start, len);
128 map = ioremap(start, len);
130 pr_devel("%s(): ioremap(0x%llx, %d) failed\n", __func__, start,
138 static void unmap_region(void *addr, u64 start, int len)
141 release_mem_region((phys_addr_t)start, len);
145 * Unmap the paste address region for a window.
147 static void unmap_paste_region(struct vas_window *window)
152 if (window->paste_kaddr) {
153 compute_paste_address(window, &busaddr_start, &len);
154 unmap_region(window->paste_kaddr, busaddr_start, len);
155 window->paste_kaddr = NULL;
156 kfree(window->paste_addr_name);
157 window->paste_addr_name = NULL;
162 * Unmap the MMIO regions for a window. Hold the vas_mutex so we don't
163 * unmap when the window's debugfs dir is in use. This serializes close
164 * of a window even on another VAS instance but since its not a critical
165 * path, just minimize the time we hold the mutex for now. We can add
166 * a per-instance mutex later if necessary.
168 static void unmap_winctx_mmio_bars(struct vas_window *window)
175 mutex_lock(&vas_mutex);
177 hvwc_map = window->hvwc_map;
178 window->hvwc_map = NULL;
180 uwc_map = window->uwc_map;
181 window->uwc_map = NULL;
183 mutex_unlock(&vas_mutex);
186 get_hvwc_mmio_bar(window, &busaddr_start, &len);
187 unmap_region(hvwc_map, busaddr_start, len);
191 get_uwc_mmio_bar(window, &busaddr_start, &len);
192 unmap_region(uwc_map, busaddr_start, len);
197 * Find the Hypervisor Window Context (HVWC) MMIO Base Address Region and the
198 * OS/User Window Context (UWC) MMIO Base Address Region for the given window.
199 * Map these bus addresses and save the mapped kernel addresses in @window.
201 int map_winctx_mmio_bars(struct vas_window *window)
206 get_hvwc_mmio_bar(window, &start, &len);
207 window->hvwc_map = map_mmio_region("HVWCM_Window", start, len);
209 get_uwc_mmio_bar(window, &start, &len);
210 window->uwc_map = map_mmio_region("UWCM_Window", start, len);
212 if (!window->hvwc_map || !window->uwc_map) {
213 unmap_winctx_mmio_bars(window);
221 * Reset all valid registers in the HV and OS/User Window Contexts for
222 * the window identified by @window.
224 * NOTE: We cannot really use a for loop to reset window context. Not all
225 * offsets in a window context are valid registers and the valid
226 * registers are not sequential. And, we can only write to offsets
227 * with valid registers.
229 void reset_window_regs(struct vas_window *window)
231 write_hvwc_reg(window, VREG(LPID), 0ULL);
232 write_hvwc_reg(window, VREG(PID), 0ULL);
233 write_hvwc_reg(window, VREG(XLATE_MSR), 0ULL);
234 write_hvwc_reg(window, VREG(XLATE_LPCR), 0ULL);
235 write_hvwc_reg(window, VREG(XLATE_CTL), 0ULL);
236 write_hvwc_reg(window, VREG(AMR), 0ULL);
237 write_hvwc_reg(window, VREG(SEIDR), 0ULL);
238 write_hvwc_reg(window, VREG(FAULT_TX_WIN), 0ULL);
239 write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
240 write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), 0ULL);
241 write_hvwc_reg(window, VREG(PSWID), 0ULL);
242 write_hvwc_reg(window, VREG(LFIFO_BAR), 0ULL);
243 write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), 0ULL);
244 write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), 0ULL);
245 write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
246 write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
247 write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
248 write_hvwc_reg(window, VREG(LRX_WCRED), 0ULL);
249 write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
250 write_hvwc_reg(window, VREG(TX_WCRED), 0ULL);
251 write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
252 write_hvwc_reg(window, VREG(LFIFO_SIZE), 0ULL);
253 write_hvwc_reg(window, VREG(WINCTL), 0ULL);
254 write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
255 write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), 0ULL);
256 write_hvwc_reg(window, VREG(TX_RSVD_BUF_COUNT), 0ULL);
257 write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), 0ULL);
258 write_hvwc_reg(window, VREG(LNOTIFY_CTL), 0ULL);
259 write_hvwc_reg(window, VREG(LNOTIFY_PID), 0ULL);
260 write_hvwc_reg(window, VREG(LNOTIFY_LPID), 0ULL);
261 write_hvwc_reg(window, VREG(LNOTIFY_TID), 0ULL);
262 write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), 0ULL);
263 write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
265 /* Skip read-only registers: NX_UTIL and NX_UTIL_SE */
268 * The send and receive window credit adder registers are also
269 * accessible from HVWC and have been initialized above. We don't
270 * need to initialize from the OS/User Window Context, so skip
273 * write_uwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
274 * write_uwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
279 * Initialize window context registers related to Address Translation.
280 * These registers are common to send/receive windows although they
281 * differ for user/kernel windows. As we resolve the TODOs we may
282 * want to add fields to vas_winctx and move the initialization to
283 * init_vas_winctx_regs().
285 static void init_xlate_regs(struct vas_window *window, bool user_win)
290 * MSR_TA, MSR_US are false for both kernel and user.
291 * MSR_DR and MSR_PR are false for kernel.
294 val = SET_FIELD(VAS_XLATE_MSR_HV, val, 1);
295 val = SET_FIELD(VAS_XLATE_MSR_SF, val, 1);
297 val = SET_FIELD(VAS_XLATE_MSR_DR, val, 1);
298 val = SET_FIELD(VAS_XLATE_MSR_PR, val, 1);
300 write_hvwc_reg(window, VREG(XLATE_MSR), val);
302 lpcr = mfspr(SPRN_LPCR);
305 * NOTE: From Section 5.7.8.1 Segment Lookaside Buffer of the
306 * Power ISA, v3.0B, Page size encoding is 0 = 4KB, 5 = 64KB.
308 * NOTE: From Section 1.3.1, Address Translation Context of the
309 * Nest MMU Workbook, LPCR_SC should be 0 for Power9.
311 val = SET_FIELD(VAS_XLATE_LPCR_PAGE_SIZE, val, 5);
312 val = SET_FIELD(VAS_XLATE_LPCR_ISL, val, lpcr & LPCR_ISL);
313 val = SET_FIELD(VAS_XLATE_LPCR_TC, val, lpcr & LPCR_TC);
314 val = SET_FIELD(VAS_XLATE_LPCR_SC, val, 0);
315 write_hvwc_reg(window, VREG(XLATE_LPCR), val);
318 * Section 1.3.1 (Address translation Context) of NMMU workbook.
319 * 0b00 Hashed Page Table mode
322 * 0b11 Radix on Radix
325 val = SET_FIELD(VAS_XLATE_MODE, val, radix_enabled() ? 3 : 2);
326 write_hvwc_reg(window, VREG(XLATE_CTL), val);
329 * TODO: Can we mfspr(AMR) even for user windows?
332 val = SET_FIELD(VAS_AMR, val, mfspr(SPRN_AMR));
333 write_hvwc_reg(window, VREG(AMR), val);
336 val = SET_FIELD(VAS_SEIDR, val, 0);
337 write_hvwc_reg(window, VREG(SEIDR), val);
341 * Initialize Reserved Send Buffer Count for the send window. It involves
342 * writing to the register, reading it back to confirm that the hardware
343 * has enough buffers to reserve. See section 1.3.1.2.1 of VAS workbook.
345 * Since we can only make a best-effort attempt to fulfill the request,
346 * we don't return any errors if we cannot.
348 * TODO: Reserved (aka dedicated) send buffers are not supported yet.
350 static void init_rsvd_tx_buf_count(struct vas_window *txwin,
351 struct vas_winctx *winctx)
353 write_hvwc_reg(txwin, VREG(TX_RSVD_BUF_COUNT), 0ULL);
358 * Initialize window context registers for a receive window.
359 * Except for caching control and marking window open, the registers
360 * are initialized in the order listed in Section 3.1.4 (Window Context
361 * Cache Register Details) of the VAS workbook although they don't need
364 * Design note: For NX receive windows, NX allocates the FIFO buffer in OPAL
365 * (so that it can get a large contiguous area) and passes that buffer
366 * to kernel via device tree. We now write that buffer address to the
367 * FIFO BAR. Would it make sense to do this all in OPAL? i.e have OPAL
368 * write the per-chip RX FIFO addresses to the windows during boot-up
369 * as a one-time task? That could work for NX but what about other
370 * receivers? Let the receivers tell us the rx-fifo buffers for now.
372 int init_winctx_regs(struct vas_window *window, struct vas_winctx *winctx)
377 reset_window_regs(window);
380 val = SET_FIELD(VAS_LPID, val, winctx->lpid);
381 write_hvwc_reg(window, VREG(LPID), val);
384 val = SET_FIELD(VAS_PID_ID, val, winctx->pidr);
385 write_hvwc_reg(window, VREG(PID), val);
387 init_xlate_regs(window, winctx->user_win);
390 val = SET_FIELD(VAS_FAULT_TX_WIN, val, 0);
391 write_hvwc_reg(window, VREG(FAULT_TX_WIN), val);
393 /* In PowerNV, interrupts go to HV. */
394 write_hvwc_reg(window, VREG(OSU_INTR_SRC_RA), 0ULL);
397 val = SET_FIELD(VAS_HV_INTR_SRC_RA, val, winctx->irq_port);
398 write_hvwc_reg(window, VREG(HV_INTR_SRC_RA), val);
401 val = SET_FIELD(VAS_PSWID_EA_HANDLE, val, winctx->pswid);
402 write_hvwc_reg(window, VREG(PSWID), val);
404 write_hvwc_reg(window, VREG(SPARE1), 0ULL);
405 write_hvwc_reg(window, VREG(SPARE2), 0ULL);
406 write_hvwc_reg(window, VREG(SPARE3), 0ULL);
409 * NOTE: VAS expects the FIFO address to be copied into the LFIFO_BAR
410 * register as is - do NOT shift the address into VAS_LFIFO_BAR
411 * bit fields! Ok to set the page migration select fields -
412 * VAS ignores the lower 10+ bits in the address anyway, because
413 * the minimum FIFO size is 1K?
415 * See also: Design note in function header.
417 val = __pa(winctx->rx_fifo);
418 val = SET_FIELD(VAS_PAGE_MIGRATION_SELECT, val, 0);
419 write_hvwc_reg(window, VREG(LFIFO_BAR), val);
422 val = SET_FIELD(VAS_LDATA_STAMP, val, winctx->data_stamp);
423 write_hvwc_reg(window, VREG(LDATA_STAMP_CTL), val);
426 val = SET_FIELD(VAS_LDMA_TYPE, val, winctx->dma_type);
427 val = SET_FIELD(VAS_LDMA_FIFO_DISABLE, val, winctx->fifo_disable);
428 write_hvwc_reg(window, VREG(LDMA_CACHE_CTL), val);
430 write_hvwc_reg(window, VREG(LRFIFO_PUSH), 0ULL);
431 write_hvwc_reg(window, VREG(CURR_MSG_COUNT), 0ULL);
432 write_hvwc_reg(window, VREG(LNOTIFY_AFTER_COUNT), 0ULL);
435 val = SET_FIELD(VAS_LRX_WCRED, val, winctx->wcreds_max);
436 write_hvwc_reg(window, VREG(LRX_WCRED), val);
439 val = SET_FIELD(VAS_TX_WCRED, val, winctx->wcreds_max);
440 write_hvwc_reg(window, VREG(TX_WCRED), val);
442 write_hvwc_reg(window, VREG(LRX_WCRED_ADDER), 0ULL);
443 write_hvwc_reg(window, VREG(TX_WCRED_ADDER), 0ULL);
445 fifo_size = winctx->rx_fifo_size / 1024;
448 val = SET_FIELD(VAS_LFIFO_SIZE, val, ilog2(fifo_size));
449 write_hvwc_reg(window, VREG(LFIFO_SIZE), val);
451 /* Update window control and caching control registers last so
452 * we mark the window open only after fully initializing it and
453 * pushing context to cache.
456 write_hvwc_reg(window, VREG(WIN_STATUS), 0ULL);
458 init_rsvd_tx_buf_count(window, winctx);
460 /* for a send window, point to the matching receive window */
462 val = SET_FIELD(VAS_LRX_WIN_ID, val, winctx->rx_win_id);
463 write_hvwc_reg(window, VREG(LRFIFO_WIN_PTR), val);
465 write_hvwc_reg(window, VREG(SPARE4), 0ULL);
468 val = SET_FIELD(VAS_NOTIFY_DISABLE, val, winctx->notify_disable);
469 val = SET_FIELD(VAS_INTR_DISABLE, val, winctx->intr_disable);
470 val = SET_FIELD(VAS_NOTIFY_EARLY, val, winctx->notify_early);
471 val = SET_FIELD(VAS_NOTIFY_OSU_INTR, val, winctx->notify_os_intr_reg);
472 write_hvwc_reg(window, VREG(LNOTIFY_CTL), val);
475 val = SET_FIELD(VAS_LNOTIFY_PID, val, winctx->lnotify_pid);
476 write_hvwc_reg(window, VREG(LNOTIFY_PID), val);
479 val = SET_FIELD(VAS_LNOTIFY_LPID, val, winctx->lnotify_lpid);
480 write_hvwc_reg(window, VREG(LNOTIFY_LPID), val);
483 val = SET_FIELD(VAS_LNOTIFY_TID, val, winctx->lnotify_tid);
484 write_hvwc_reg(window, VREG(LNOTIFY_TID), val);
487 val = SET_FIELD(VAS_LNOTIFY_MIN_SCOPE, val, winctx->min_scope);
488 val = SET_FIELD(VAS_LNOTIFY_MAX_SCOPE, val, winctx->max_scope);
489 write_hvwc_reg(window, VREG(LNOTIFY_SCOPE), val);
491 /* Skip read-only registers NX_UTIL and NX_UTIL_SE */
493 write_hvwc_reg(window, VREG(SPARE5), 0ULL);
494 write_hvwc_reg(window, VREG(NX_UTIL_ADDER), 0ULL);
495 write_hvwc_reg(window, VREG(SPARE6), 0ULL);
497 /* Finally, push window context to memory and... */
499 val = SET_FIELD(VAS_PUSH_TO_MEM, val, 1);
500 write_hvwc_reg(window, VREG(WIN_CTX_CACHING_CTL), val);
502 /* ... mark the window open for business */
504 val = SET_FIELD(VAS_WINCTL_REJ_NO_CREDIT, val, winctx->rej_no_credit);
505 val = SET_FIELD(VAS_WINCTL_PIN, val, winctx->pin_win);
506 val = SET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val, winctx->tx_wcred_mode);
507 val = SET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val, winctx->rx_wcred_mode);
508 val = SET_FIELD(VAS_WINCTL_TX_WORD_MODE, val, winctx->tx_word_mode);
509 val = SET_FIELD(VAS_WINCTL_RX_WORD_MODE, val, winctx->rx_word_mode);
510 val = SET_FIELD(VAS_WINCTL_FAULT_WIN, val, winctx->fault_win);
511 val = SET_FIELD(VAS_WINCTL_NX_WIN, val, winctx->nx_win);
512 val = SET_FIELD(VAS_WINCTL_OPEN, val, 1);
513 write_hvwc_reg(window, VREG(WINCTL), val);
518 static void vas_release_window_id(struct ida *ida, int winid)
520 ida_free(ida, winid);
523 static int vas_assign_window_id(struct ida *ida)
525 int winid = ida_alloc_max(ida, VAS_WINDOWS_PER_CHIP - 1, GFP_KERNEL);
527 if (winid == -ENOSPC) {
528 pr_err("Too many (%d) open windows\n", VAS_WINDOWS_PER_CHIP);
535 static void vas_window_free(struct vas_window *window)
537 int winid = window->winid;
538 struct vas_instance *vinst = window->vinst;
540 unmap_winctx_mmio_bars(window);
542 vas_window_free_dbgdir(window);
546 vas_release_window_id(&vinst->ida, winid);
549 static struct vas_window *vas_window_alloc(struct vas_instance *vinst)
552 struct vas_window *window;
554 winid = vas_assign_window_id(&vinst->ida);
556 return ERR_PTR(winid);
558 window = kzalloc(sizeof(*window), GFP_KERNEL);
562 window->vinst = vinst;
563 window->winid = winid;
565 if (map_winctx_mmio_bars(window))
568 vas_window_init_dbgdir(window);
574 vas_release_window_id(&vinst->ida, winid);
575 return ERR_PTR(-ENOMEM);
578 static void put_rx_win(struct vas_window *rxwin)
580 /* Better not be a send window! */
581 WARN_ON_ONCE(rxwin->tx_win);
583 atomic_dec(&rxwin->num_txwins);
587 * Find the user space receive window given the @pswid.
588 * - We must have a valid vasid and it must belong to this instance.
589 * (so both send and receive windows are on the same VAS instance)
590 * - The window must refer to an OPEN, FTW, RECEIVE window.
592 * NOTE: We access ->windows[] table and assume that vinst->mutex is held.
594 static struct vas_window *get_user_rxwin(struct vas_instance *vinst, u32 pswid)
597 struct vas_window *rxwin;
599 decode_pswid(pswid, &vasid, &winid);
601 if (vinst->vas_id != vasid)
602 return ERR_PTR(-EINVAL);
604 rxwin = vinst->windows[winid];
606 if (!rxwin || rxwin->tx_win || rxwin->cop != VAS_COP_TYPE_FTW)
607 return ERR_PTR(-EINVAL);
613 * Get the VAS receive window associated with NX engine identified
614 * by @cop and if applicable, @pswid.
616 * See also function header of set_vinst_win().
618 static struct vas_window *get_vinst_rxwin(struct vas_instance *vinst,
619 enum vas_cop_type cop, u32 pswid)
621 struct vas_window *rxwin;
623 mutex_lock(&vinst->mutex);
625 if (cop == VAS_COP_TYPE_FTW)
626 rxwin = get_user_rxwin(vinst, pswid);
628 rxwin = vinst->rxwin[cop] ?: ERR_PTR(-EINVAL);
631 atomic_inc(&rxwin->num_txwins);
633 mutex_unlock(&vinst->mutex);
639 * We have two tables of windows in a VAS instance. The first one,
640 * ->windows[], contains all the windows in the instance and allows
641 * looking up a window by its id. It is used to look up send windows
642 * during fault handling and receive windows when pairing user space
643 * send/receive windows.
645 * The second table, ->rxwin[], contains receive windows that are
646 * associated with NX engines. This table has VAS_COP_TYPE_MAX
647 * entries and is used to look up a receive window by its
650 * Here, we save @window in the ->windows[] table. If it is a receive
651 * window, we also save the window in the ->rxwin[] table.
653 static void set_vinst_win(struct vas_instance *vinst,
654 struct vas_window *window)
656 int id = window->winid;
658 mutex_lock(&vinst->mutex);
661 * There should only be one receive window for a coprocessor type
662 * unless its a user (FTW) window.
664 if (!window->user_win && !window->tx_win) {
665 WARN_ON_ONCE(vinst->rxwin[window->cop]);
666 vinst->rxwin[window->cop] = window;
669 WARN_ON_ONCE(vinst->windows[id] != NULL);
670 vinst->windows[id] = window;
672 mutex_unlock(&vinst->mutex);
676 * Clear this window from the table(s) of windows for this VAS instance.
677 * See also function header of set_vinst_win().
679 static void clear_vinst_win(struct vas_window *window)
681 int id = window->winid;
682 struct vas_instance *vinst = window->vinst;
684 mutex_lock(&vinst->mutex);
686 if (!window->user_win && !window->tx_win) {
687 WARN_ON_ONCE(!vinst->rxwin[window->cop]);
688 vinst->rxwin[window->cop] = NULL;
691 WARN_ON_ONCE(vinst->windows[id] != window);
692 vinst->windows[id] = NULL;
694 mutex_unlock(&vinst->mutex);
697 static void init_winctx_for_rxwin(struct vas_window *rxwin,
698 struct vas_rx_win_attr *rxattr,
699 struct vas_winctx *winctx)
702 * We first zero (memset()) all fields and only set non-zero fields.
703 * Following fields are 0/false but maybe deserve a comment:
705 * ->notify_os_intr_reg In powerNV, send intrs to HV
706 * ->notify_disable False for NX windows
707 * ->intr_disable False for Fault Windows
708 * ->xtra_write False for NX windows
709 * ->notify_early NA for NX windows
710 * ->rsvd_txbuf_count NA for Rx windows
711 * ->lpid, ->pid, ->tid NA for Rx windows
714 memset(winctx, 0, sizeof(struct vas_winctx));
716 winctx->rx_fifo = rxattr->rx_fifo;
717 winctx->rx_fifo_size = rxattr->rx_fifo_size;
718 winctx->wcreds_max = rxwin->wcreds_max;
719 winctx->pin_win = rxattr->pin_win;
721 winctx->nx_win = rxattr->nx_win;
722 winctx->fault_win = rxattr->fault_win;
723 winctx->user_win = rxattr->user_win;
724 winctx->rej_no_credit = rxattr->rej_no_credit;
725 winctx->rx_word_mode = rxattr->rx_win_ord_mode;
726 winctx->tx_word_mode = rxattr->tx_win_ord_mode;
727 winctx->rx_wcred_mode = rxattr->rx_wcred_mode;
728 winctx->tx_wcred_mode = rxattr->tx_wcred_mode;
729 winctx->notify_early = rxattr->notify_early;
731 if (winctx->nx_win) {
732 winctx->data_stamp = true;
733 winctx->intr_disable = true;
734 winctx->pin_win = true;
736 WARN_ON_ONCE(winctx->fault_win);
737 WARN_ON_ONCE(!winctx->rx_word_mode);
738 WARN_ON_ONCE(!winctx->tx_word_mode);
739 WARN_ON_ONCE(winctx->notify_after_count);
740 } else if (winctx->fault_win) {
741 winctx->notify_disable = true;
742 } else if (winctx->user_win) {
744 * Section 1.8.1 Low Latency Core-Core Wake up of
747 * - disable credit checks ([tr]x_wcred_mode = false)
748 * - disable FIFO writes
749 * - enable ASB_Notify, disable interrupt
751 winctx->fifo_disable = true;
752 winctx->intr_disable = true;
753 winctx->rx_fifo = NULL;
756 winctx->lnotify_lpid = rxattr->lnotify_lpid;
757 winctx->lnotify_pid = rxattr->lnotify_pid;
758 winctx->lnotify_tid = rxattr->lnotify_tid;
759 winctx->pswid = rxattr->pswid;
760 winctx->dma_type = VAS_DMA_TYPE_INJECT;
761 winctx->tc_mode = rxattr->tc_mode;
763 winctx->min_scope = VAS_SCOPE_LOCAL;
764 winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
767 static bool rx_win_args_valid(enum vas_cop_type cop,
768 struct vas_rx_win_attr *attr)
770 pr_debug("Rxattr: fault %d, notify %d, intr %d, early %d, fifo %d\n",
771 attr->fault_win, attr->notify_disable,
772 attr->intr_disable, attr->notify_early,
775 if (cop >= VAS_COP_TYPE_MAX)
778 if (cop != VAS_COP_TYPE_FTW &&
779 attr->rx_fifo_size < VAS_RX_FIFO_SIZE_MIN)
782 if (attr->rx_fifo_size > VAS_RX_FIFO_SIZE_MAX)
785 if (attr->wcreds_max > VAS_RX_WCREDS_MAX)
789 /* cannot be fault or user window if it is nx */
790 if (attr->fault_win || attr->user_win)
793 * Section 3.1.4.32: NX Windows must not disable notification,
794 * and must not enable interrupts or early notification.
796 if (attr->notify_disable || !attr->intr_disable ||
799 } else if (attr->fault_win) {
800 /* cannot be both fault and user window */
805 * Section 3.1.4.32: Fault windows must disable notification
806 * but not interrupts.
808 if (!attr->notify_disable || attr->intr_disable)
811 } else if (attr->user_win) {
813 * User receive windows are only for fast-thread-wakeup
814 * (FTW). They don't need a FIFO and must disable interrupts
816 if (attr->rx_fifo || attr->rx_fifo_size || !attr->intr_disable)
819 /* Rx window must be one of NX or Fault or User window. */
826 void vas_init_rx_win_attr(struct vas_rx_win_attr *rxattr, enum vas_cop_type cop)
828 memset(rxattr, 0, sizeof(*rxattr));
830 if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
831 rxattr->pin_win = true;
832 rxattr->nx_win = true;
833 rxattr->fault_win = false;
834 rxattr->intr_disable = true;
835 rxattr->rx_wcred_mode = true;
836 rxattr->tx_wcred_mode = true;
837 rxattr->rx_win_ord_mode = true;
838 rxattr->tx_win_ord_mode = true;
839 } else if (cop == VAS_COP_TYPE_FAULT) {
840 rxattr->pin_win = true;
841 rxattr->fault_win = true;
842 rxattr->notify_disable = true;
843 rxattr->rx_wcred_mode = true;
844 rxattr->tx_wcred_mode = true;
845 rxattr->rx_win_ord_mode = true;
846 rxattr->tx_win_ord_mode = true;
847 } else if (cop == VAS_COP_TYPE_FTW) {
848 rxattr->user_win = true;
849 rxattr->intr_disable = true;
852 * As noted in the VAS Workbook we disable credit checks.
853 * If we enable credit checks in the future, we must also
854 * implement a mechanism to return the user credits or new
855 * paste operations will fail.
859 EXPORT_SYMBOL_GPL(vas_init_rx_win_attr);
861 struct vas_window *vas_rx_win_open(int vasid, enum vas_cop_type cop,
862 struct vas_rx_win_attr *rxattr)
864 struct vas_window *rxwin;
865 struct vas_winctx winctx;
866 struct vas_instance *vinst;
868 trace_vas_rx_win_open(current, vasid, cop, rxattr);
870 if (!rx_win_args_valid(cop, rxattr))
871 return ERR_PTR(-EINVAL);
873 vinst = find_vas_instance(vasid);
875 pr_devel("vasid %d not found!\n", vasid);
876 return ERR_PTR(-EINVAL);
878 pr_devel("Found instance %d\n", vasid);
880 rxwin = vas_window_alloc(vinst);
882 pr_devel("Unable to allocate memory for Rx window\n");
886 rxwin->tx_win = false;
887 rxwin->nx_win = rxattr->nx_win;
888 rxwin->user_win = rxattr->user_win;
890 rxwin->wcreds_max = rxattr->wcreds_max ?: VAS_WCREDS_DEFAULT;
891 if (rxattr->user_win)
892 rxwin->pid = task_pid_vnr(current);
894 init_winctx_for_rxwin(rxwin, rxattr, &winctx);
895 init_winctx_regs(rxwin, &winctx);
897 set_vinst_win(vinst, rxwin);
901 EXPORT_SYMBOL_GPL(vas_rx_win_open);
903 void vas_init_tx_win_attr(struct vas_tx_win_attr *txattr, enum vas_cop_type cop)
905 memset(txattr, 0, sizeof(*txattr));
907 if (cop == VAS_COP_TYPE_842 || cop == VAS_COP_TYPE_842_HIPRI) {
908 txattr->rej_no_credit = false;
909 txattr->rx_wcred_mode = true;
910 txattr->tx_wcred_mode = true;
911 txattr->rx_win_ord_mode = true;
912 txattr->tx_win_ord_mode = true;
913 } else if (cop == VAS_COP_TYPE_FTW) {
914 txattr->user_win = true;
917 EXPORT_SYMBOL_GPL(vas_init_tx_win_attr);
919 static void init_winctx_for_txwin(struct vas_window *txwin,
920 struct vas_tx_win_attr *txattr,
921 struct vas_winctx *winctx)
924 * We first zero all fields and only set non-zero ones. Following
925 * are some fields set to 0/false for the stated reason:
927 * ->notify_os_intr_reg In powernv, send intrs to HV
928 * ->rsvd_txbuf_count Not supported yet.
929 * ->notify_disable False for NX windows
930 * ->xtra_write False for NX windows
931 * ->notify_early NA for NX windows
932 * ->lnotify_lpid NA for Tx windows
933 * ->lnotify_pid NA for Tx windows
934 * ->lnotify_tid NA for Tx windows
935 * ->tx_win_cred_mode Ignore for now for NX windows
936 * ->rx_win_cred_mode Ignore for now for NX windows
938 memset(winctx, 0, sizeof(struct vas_winctx));
940 winctx->wcreds_max = txwin->wcreds_max;
942 winctx->user_win = txattr->user_win;
943 winctx->nx_win = txwin->rxwin->nx_win;
944 winctx->pin_win = txattr->pin_win;
945 winctx->rej_no_credit = txattr->rej_no_credit;
946 winctx->rsvd_txbuf_enable = txattr->rsvd_txbuf_enable;
948 winctx->rx_wcred_mode = txattr->rx_wcred_mode;
949 winctx->tx_wcred_mode = txattr->tx_wcred_mode;
950 winctx->rx_word_mode = txattr->rx_win_ord_mode;
951 winctx->tx_word_mode = txattr->tx_win_ord_mode;
952 winctx->rsvd_txbuf_count = txattr->rsvd_txbuf_count;
954 winctx->intr_disable = true;
956 winctx->data_stamp = true;
958 winctx->lpid = txattr->lpid;
959 winctx->pidr = txattr->pidr;
960 winctx->rx_win_id = txwin->rxwin->winid;
962 winctx->dma_type = VAS_DMA_TYPE_INJECT;
963 winctx->tc_mode = txattr->tc_mode;
964 winctx->min_scope = VAS_SCOPE_LOCAL;
965 winctx->max_scope = VAS_SCOPE_VECTORED_GROUP;
970 static bool tx_win_args_valid(enum vas_cop_type cop,
971 struct vas_tx_win_attr *attr)
973 if (attr->tc_mode != VAS_THRESH_DISABLED)
976 if (cop > VAS_COP_TYPE_MAX)
979 if (attr->wcreds_max > VAS_TX_WCREDS_MAX)
982 if (attr->user_win &&
983 (cop != VAS_COP_TYPE_FTW || attr->rsvd_txbuf_count))
989 struct vas_window *vas_tx_win_open(int vasid, enum vas_cop_type cop,
990 struct vas_tx_win_attr *attr)
993 struct vas_window *txwin;
994 struct vas_window *rxwin;
995 struct vas_winctx winctx;
996 struct vas_instance *vinst;
998 trace_vas_tx_win_open(current, vasid, cop, attr);
1000 if (!tx_win_args_valid(cop, attr))
1001 return ERR_PTR(-EINVAL);
1004 * If caller did not specify a vasid but specified the PSWID of a
1005 * receive window (applicable only to FTW windows), use the vasid
1006 * from that receive window.
1008 if (vasid == -1 && attr->pswid)
1009 decode_pswid(attr->pswid, &vasid, NULL);
1011 vinst = find_vas_instance(vasid);
1013 pr_devel("vasid %d not found!\n", vasid);
1014 return ERR_PTR(-EINVAL);
1017 rxwin = get_vinst_rxwin(vinst, cop, attr->pswid);
1018 if (IS_ERR(rxwin)) {
1019 pr_devel("No RxWin for vasid %d, cop %d\n", vasid, cop);
1023 txwin = vas_window_alloc(vinst);
1024 if (IS_ERR(txwin)) {
1025 rc = PTR_ERR(txwin);
1031 txwin->rxwin = rxwin;
1032 txwin->nx_win = txwin->rxwin->nx_win;
1033 txwin->pid = attr->pid;
1034 txwin->user_win = attr->user_win;
1035 txwin->wcreds_max = attr->wcreds_max ?: VAS_WCREDS_DEFAULT;
1037 init_winctx_for_txwin(txwin, attr, &winctx);
1039 init_winctx_regs(txwin, &winctx);
1042 * If its a kernel send window, map the window address into the
1043 * kernel's address space. For user windows, user must issue an
1044 * mmap() to map the window into their address space.
1046 * NOTE: If kernel ever resubmits a user CRB after handling a page
1047 * fault, we will need to map this into kernel as well.
1049 if (!txwin->user_win) {
1050 txwin->paste_kaddr = map_paste_region(txwin);
1051 if (IS_ERR(txwin->paste_kaddr)) {
1052 rc = PTR_ERR(txwin->paste_kaddr);
1057 * A user mapping must ensure that context switch issues
1058 * CP_ABORT for this thread.
1060 rc = set_thread_uses_vas();
1065 set_vinst_win(vinst, txwin);
1070 vas_window_free(txwin);
1077 EXPORT_SYMBOL_GPL(vas_tx_win_open);
1079 int vas_copy_crb(void *crb, int offset)
1081 return vas_copy(crb, offset);
1083 EXPORT_SYMBOL_GPL(vas_copy_crb);
1085 #define RMA_LSMP_REPORT_ENABLE PPC_BIT(53)
1086 int vas_paste_crb(struct vas_window *txwin, int offset, bool re)
1092 trace_vas_paste_crb(current, txwin);
1095 * Only NX windows are supported for now and hardware assumes
1096 * report-enable flag is set for NX windows. Ensure software
1099 WARN_ON_ONCE(txwin->nx_win && !re);
1101 addr = txwin->paste_kaddr;
1104 * Set the REPORT_ENABLE bit (equivalent to writing
1105 * to 1K offset of the paste address)
1107 val = SET_FIELD(RMA_LSMP_REPORT_ENABLE, 0ULL, 1);
1112 * Map the raw CR value from vas_paste() to an error code (there
1113 * is just pass or fail for now though).
1115 rc = vas_paste(addr, offset);
1121 pr_debug("Txwin #%d: Msg count %llu\n", txwin->winid,
1122 read_hvwc_reg(txwin, VREG(LRFIFO_PUSH)));
1126 EXPORT_SYMBOL_GPL(vas_paste_crb);
1129 * If credit checking is enabled for this window, poll for the return
1130 * of window credits (i.e for NX engines to process any outstanding CRBs).
1131 * Since NX-842 waits for the CRBs to be processed before closing the
1132 * window, we should not have to wait for too long.
1134 * TODO: We retry in 10ms intervals now. We could/should probably peek at
1135 * the VAS_LRFIFO_PUSH_OFFSET register to get an estimate of pending
1136 * CRBs on the FIFO and compute the delay dynamically on each retry.
1137 * But that is not really needed until we support NX-GZIP access from
1138 * user space. (NX-842 driver waits for CSB and Fast thread-wakeup
1139 * doesn't use credit checking).
1141 static void poll_window_credits(struct vas_window *window)
1146 val = read_hvwc_reg(window, VREG(WINCTL));
1148 mode = GET_FIELD(VAS_WINCTL_TX_WCRED_MODE, val);
1150 mode = GET_FIELD(VAS_WINCTL_RX_WCRED_MODE, val);
1155 if (window->tx_win) {
1156 val = read_hvwc_reg(window, VREG(TX_WCRED));
1157 creds = GET_FIELD(VAS_TX_WCRED, val);
1159 val = read_hvwc_reg(window, VREG(LRX_WCRED));
1160 creds = GET_FIELD(VAS_LRX_WCRED, val);
1163 if (creds < window->wcreds_max) {
1165 set_current_state(TASK_UNINTERRUPTIBLE);
1166 schedule_timeout(msecs_to_jiffies(10));
1172 * Wait for the window to go to "not-busy" state. It should only take a
1173 * short time to queue a CRB, so window should not be busy for too long.
1174 * Trying 5ms intervals.
1176 static void poll_window_busy_state(struct vas_window *window)
1182 val = read_hvwc_reg(window, VREG(WIN_STATUS));
1183 busy = GET_FIELD(VAS_WIN_BUSY, val);
1186 set_current_state(TASK_UNINTERRUPTIBLE);
1187 schedule_timeout(msecs_to_jiffies(5));
1193 * Have the hardware cast a window out of cache and wait for it to
1196 * NOTE: It can take a relatively long time to cast the window context
1197 * out of the cache. It is not strictly necessary to cast out if:
1199 * - we clear the "Pin Window" bit (so hardware is free to evict)
1201 * - we re-initialize the window context when it is reassigned.
1203 * We do the former in vas_win_close() and latter in vas_win_open().
1204 * So, ignoring the cast-out for now. We can add it as needed. If
1205 * casting out becomes necessary we should consider offloading the
1206 * job to a worker thread, so the window close can proceed quickly.
1208 static void poll_window_castout(struct vas_window *window)
1214 * Unpin and close a window so no new requests are accepted and the
1215 * hardware can evict this window from cache if necessary.
1217 static void unpin_close_window(struct vas_window *window)
1221 val = read_hvwc_reg(window, VREG(WINCTL));
1222 val = SET_FIELD(VAS_WINCTL_PIN, val, 0);
1223 val = SET_FIELD(VAS_WINCTL_OPEN, val, 0);
1224 write_hvwc_reg(window, VREG(WINCTL), val);
1230 * See Section 1.12.1 of VAS workbook v1.05 for details on closing window:
1231 * - Disable new paste operations (unmap paste address)
1232 * - Poll for the "Window Busy" bit to be cleared
1233 * - Clear the Open/Enable bit for the Window.
1234 * - Poll for return of window Credits (implies FIFO empty for Rx win?)
1235 * - Unpin and cast window context out of cache
1237 * Besides the hardware, kernel has some bookkeeping of course.
1239 int vas_win_close(struct vas_window *window)
1244 if (!window->tx_win && atomic_read(&window->num_txwins) != 0) {
1245 pr_devel("Attempting to close an active Rx window!\n");
1250 unmap_paste_region(window);
1252 clear_vinst_win(window);
1254 poll_window_busy_state(window);
1256 unpin_close_window(window);
1258 poll_window_credits(window);
1260 poll_window_castout(window);
1262 /* if send window, drop reference to matching receive window */
1264 put_rx_win(window->rxwin);
1266 vas_window_free(window);
1270 EXPORT_SYMBOL_GPL(vas_win_close);
1273 * Return a system-wide unique window id for the window @win.
1275 u32 vas_win_id(struct vas_window *win)
1277 return encode_pswid(win->vinst->vas_id, win->winid);
1279 EXPORT_SYMBOL_GPL(vas_win_id);