Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright 2016-17 IBM Corp.
0004  */
0005 
0006 #ifndef _VAS_H
0007 #define _VAS_H
0008 #include <linux/atomic.h>
0009 #include <linux/idr.h>
0010 #include <asm/vas.h>
0011 #include <linux/io.h>
0012 #include <linux/dcache.h>
0013 #include <linux/mutex.h>
0014 #include <linux/stringify.h>
0015 
0016 /*
0017  * Overview of Virtual Accelerator Switchboard (VAS).
0018  *
0019  * VAS is a hardware "switchboard" that allows senders and receivers to
0020  * exchange messages with _minimal_ kernel involvment. The receivers are
0021  * typically NX coprocessor engines that perform compression or encryption
0022  * in hardware, but receivers can also be other software threads.
0023  *
0024  * Senders are user/kernel threads that submit compression/encryption or
0025  * other requests to the receivers. Senders must format their messages as
0026  * Coprocessor Request Blocks (CRB)s and submit them using the "copy" and
0027  * "paste" instructions which were introduced in Power9.
0028  *
0029  * A Power node can have (upto?) 8 Power chips. There is one instance of
0030  * VAS in each Power9 chip. Each instance of VAS has 64K windows or ports,
0031  * Senders and receivers must each connect to a separate window before they
0032  * can exchange messages through the switchboard.
0033  *
0034  * Each window is described by two types of window contexts:
0035  *
0036  *  Hypervisor Window Context (HVWC) of size VAS_HVWC_SIZE bytes
0037  *
0038  *  OS/User Window Context (UWC) of size VAS_UWC_SIZE bytes.
0039  *
0040  * A window context can be viewed as a set of 64-bit registers. The settings
0041  * in these registers configure/control/determine the behavior of the VAS
0042  * hardware when messages are sent/received through the window. The registers
0043  * in the HVWC are configured by the kernel while the registers in the UWC can
0044  * be configured by the kernel or by the user space application that is using
0045  * the window.
0046  *
0047  * The HVWCs for all windows on a specific instance of VAS are in a contiguous
0048  * range of hardware addresses or Base address region (BAR) referred to as the
0049  * HVWC BAR for the instance. Similarly the UWCs for all windows on an instance
0050  * are referred to as the UWC BAR for the instance.
0051  *
0052  * The two BARs for each instance are defined Power9 MMIO Ranges spreadsheet
0053  * and available to the kernel in the VAS node's "reg" property in the device
0054  * tree:
0055  *
0056  *  /proc/device-tree/vasm@.../reg
0057  *
0058  * (see vas_probe() for details on the reg property).
0059  *
0060  * The kernel maps the HVWC and UWC BAR regions into the kernel address
0061  * space (hvwc_map and uwc_map). The kernel can then access the window
0062  * contexts of a specific window using:
0063  *
0064  *   hvwc = hvwc_map + winid * VAS_HVWC_SIZE.
0065  *   uwc = uwc_map + winid * VAS_UWC_SIZE.
0066  *
0067  * where winid is the window index (0..64K).
0068  *
0069  * As mentioned, a window context is used to "configure" a window. Besides
0070  * this configuration address, each _send_ window also has a unique hardware
0071  * "paste" address that is used to submit requests/CRBs (see vas_paste_crb()).
0072  *
0073  * The hardware paste address for a window is computed using the "paste
0074  * base address" and "paste win id shift" reg properties in the VAS device
0075  * tree node using:
0076  *
0077  *  paste_addr = paste_base + ((winid << paste_win_id_shift))
0078  *
0079  * (again, see vas_probe() for ->paste_base_addr and ->paste_win_id_shift).
0080  *
0081  * The kernel maps this hardware address into the sender's address space
0082  * after which they can use the 'paste' instruction (new in Power9) to
0083  * send a message (submit a request aka CRB) to the coprocessor.
0084  *
0085  * NOTE: In the initial version, senders can only in-kernel drivers/threads.
0086  *   Support for user space threads will be added in follow-on patches.
0087  *
0088  * TODO: Do we need to map the UWC into user address space so they can return
0089  *   credits? Its NA for NX but may be needed for other receive windows.
0090  *
0091  */
0092 
0093 #define VAS_WINDOWS_PER_CHIP        (64 << 10)
0094 
0095 /*
0096  * Hypervisor and OS/USer Window Context sizes
0097  */
0098 #define VAS_HVWC_SIZE           512
0099 #define VAS_UWC_SIZE            PAGE_SIZE
0100 
0101 /*
0102  * Initial per-process credits.
0103  * Max send window credits:    4K-1 (12-bits in VAS_TX_WCRED)
0104  *
0105  * TODO: Needs tuning for per-process credits
0106  */
0107 #define VAS_TX_WCREDS_MAX       ((4 << 10) - 1)
0108 #define VAS_WCREDS_DEFAULT      (1 << 10)
0109 
0110 /*
0111  * VAS Window Context Register Offsets and bitmasks.
0112  * See Section 3.1.4 of VAS Work book
0113  */
0114 #define VAS_LPID_OFFSET         0x010
0115 #define VAS_LPID            PPC_BITMASK(0, 11)
0116 
0117 #define VAS_PID_OFFSET          0x018
0118 #define VAS_PID_ID          PPC_BITMASK(0, 19)
0119 
0120 #define VAS_XLATE_MSR_OFFSET        0x020
0121 #define VAS_XLATE_MSR_DR        PPC_BIT(0)
0122 #define VAS_XLATE_MSR_TA        PPC_BIT(1)
0123 #define VAS_XLATE_MSR_PR        PPC_BIT(2)
0124 #define VAS_XLATE_MSR_US        PPC_BIT(3)
0125 #define VAS_XLATE_MSR_HV        PPC_BIT(4)
0126 #define VAS_XLATE_MSR_SF        PPC_BIT(5)
0127 
0128 #define VAS_XLATE_LPCR_OFFSET       0x028
0129 #define VAS_XLATE_LPCR_PAGE_SIZE    PPC_BITMASK(0, 2)
0130 #define VAS_XLATE_LPCR_ISL      PPC_BIT(3)
0131 #define VAS_XLATE_LPCR_TC       PPC_BIT(4)
0132 #define VAS_XLATE_LPCR_SC       PPC_BIT(5)
0133 
0134 #define VAS_XLATE_CTL_OFFSET        0x030
0135 #define VAS_XLATE_MODE          PPC_BITMASK(0, 1)
0136 
0137 #define VAS_AMR_OFFSET          0x040
0138 #define VAS_AMR             PPC_BITMASK(0, 63)
0139 
0140 #define VAS_SEIDR_OFFSET        0x048
0141 #define VAS_SEIDR           PPC_BITMASK(0, 63)
0142 
0143 #define VAS_FAULT_TX_WIN_OFFSET     0x050
0144 #define VAS_FAULT_TX_WIN        PPC_BITMASK(48, 63)
0145 
0146 #define VAS_OSU_INTR_SRC_RA_OFFSET  0x060
0147 #define VAS_OSU_INTR_SRC_RA     PPC_BITMASK(8, 63)
0148 
0149 #define VAS_HV_INTR_SRC_RA_OFFSET   0x070
0150 #define VAS_HV_INTR_SRC_RA      PPC_BITMASK(8, 63)
0151 
0152 #define VAS_PSWID_OFFSET        0x078
0153 #define VAS_PSWID_EA_HANDLE     PPC_BITMASK(0, 31)
0154 
0155 #define VAS_SPARE1_OFFSET       0x080
0156 #define VAS_SPARE2_OFFSET       0x088
0157 #define VAS_SPARE3_OFFSET       0x090
0158 #define VAS_SPARE4_OFFSET       0x130
0159 #define VAS_SPARE5_OFFSET       0x160
0160 #define VAS_SPARE6_OFFSET       0x188
0161 
0162 #define VAS_LFIFO_BAR_OFFSET        0x0A0
0163 #define VAS_LFIFO_BAR           PPC_BITMASK(8, 53)
0164 #define VAS_PAGE_MIGRATION_SELECT   PPC_BITMASK(54, 56)
0165 
0166 #define VAS_LDATA_STAMP_CTL_OFFSET  0x0A8
0167 #define VAS_LDATA_STAMP         PPC_BITMASK(0, 1)
0168 #define VAS_XTRA_WRITE          PPC_BIT(2)
0169 
0170 #define VAS_LDMA_CACHE_CTL_OFFSET   0x0B0
0171 #define VAS_LDMA_TYPE           PPC_BITMASK(0, 1)
0172 #define VAS_LDMA_FIFO_DISABLE       PPC_BIT(2)
0173 
0174 #define VAS_LRFIFO_PUSH_OFFSET      0x0B8
0175 #define VAS_LRFIFO_PUSH         PPC_BITMASK(0, 15)
0176 
0177 #define VAS_CURR_MSG_COUNT_OFFSET   0x0C0
0178 #define VAS_CURR_MSG_COUNT      PPC_BITMASK(0, 7)
0179 
0180 #define VAS_LNOTIFY_AFTER_COUNT_OFFSET  0x0C8
0181 #define VAS_LNOTIFY_AFTER_COUNT     PPC_BITMASK(0, 7)
0182 
0183 #define VAS_LRX_WCRED_OFFSET        0x0E0
0184 #define VAS_LRX_WCRED           PPC_BITMASK(0, 15)
0185 
0186 #define VAS_LRX_WCRED_ADDER_OFFSET  0x190
0187 #define VAS_LRX_WCRED_ADDER     PPC_BITMASK(0, 15)
0188 
0189 #define VAS_TX_WCRED_OFFSET     0x0F0
0190 #define VAS_TX_WCRED            PPC_BITMASK(4, 15)
0191 
0192 #define VAS_TX_WCRED_ADDER_OFFSET   0x1A0
0193 #define VAS_TX_WCRED_ADDER      PPC_BITMASK(4, 15)
0194 
0195 #define VAS_LFIFO_SIZE_OFFSET       0x100
0196 #define VAS_LFIFO_SIZE          PPC_BITMASK(0, 3)
0197 
0198 #define VAS_WINCTL_OFFSET       0x108
0199 #define VAS_WINCTL_OPEN         PPC_BIT(0)
0200 #define VAS_WINCTL_REJ_NO_CREDIT    PPC_BIT(1)
0201 #define VAS_WINCTL_PIN          PPC_BIT(2)
0202 #define VAS_WINCTL_TX_WCRED_MODE    PPC_BIT(3)
0203 #define VAS_WINCTL_RX_WCRED_MODE    PPC_BIT(4)
0204 #define VAS_WINCTL_TX_WORD_MODE     PPC_BIT(5)
0205 #define VAS_WINCTL_RX_WORD_MODE     PPC_BIT(6)
0206 #define VAS_WINCTL_RSVD_TXBUF       PPC_BIT(7)
0207 #define VAS_WINCTL_THRESH_CTL       PPC_BITMASK(8, 9)
0208 #define VAS_WINCTL_FAULT_WIN        PPC_BIT(10)
0209 #define VAS_WINCTL_NX_WIN       PPC_BIT(11)
0210 
0211 #define VAS_WIN_STATUS_OFFSET       0x110
0212 #define VAS_WIN_BUSY            PPC_BIT(1)
0213 
0214 #define VAS_WIN_CTX_CACHING_CTL_OFFSET  0x118
0215 #define VAS_CASTOUT_REQ         PPC_BIT(0)
0216 #define VAS_PUSH_TO_MEM         PPC_BIT(1)
0217 #define VAS_WIN_CACHE_STATUS        PPC_BIT(4)
0218 
0219 #define VAS_TX_RSVD_BUF_COUNT_OFFSET    0x120
0220 #define VAS_RXVD_BUF_COUNT      PPC_BITMASK(58, 63)
0221 
0222 #define VAS_LRFIFO_WIN_PTR_OFFSET   0x128
0223 #define VAS_LRX_WIN_ID          PPC_BITMASK(0, 15)
0224 
0225 /*
0226  * Local Notification Control Register controls what happens in _response_
0227  * to a paste command and hence applies only to receive windows.
0228  */
0229 #define VAS_LNOTIFY_CTL_OFFSET      0x138
0230 #define VAS_NOTIFY_DISABLE      PPC_BIT(0)
0231 #define VAS_INTR_DISABLE        PPC_BIT(1)
0232 #define VAS_NOTIFY_EARLY        PPC_BIT(2)
0233 #define VAS_NOTIFY_OSU_INTR     PPC_BIT(3)
0234 
0235 #define VAS_LNOTIFY_PID_OFFSET      0x140
0236 #define VAS_LNOTIFY_PID         PPC_BITMASK(0, 19)
0237 
0238 #define VAS_LNOTIFY_LPID_OFFSET     0x148
0239 #define VAS_LNOTIFY_LPID        PPC_BITMASK(0, 11)
0240 
0241 #define VAS_LNOTIFY_TID_OFFSET      0x150
0242 #define VAS_LNOTIFY_TID         PPC_BITMASK(0, 15)
0243 
0244 #define VAS_LNOTIFY_SCOPE_OFFSET    0x158
0245 #define VAS_LNOTIFY_MIN_SCOPE       PPC_BITMASK(0, 1)
0246 #define VAS_LNOTIFY_MAX_SCOPE       PPC_BITMASK(2, 3)
0247 
0248 #define VAS_NX_UTIL_OFFSET      0x1B0
0249 #define VAS_NX_UTIL         PPC_BITMASK(0, 63)
0250 
0251 /* SE: Side effects */
0252 #define VAS_NX_UTIL_SE_OFFSET       0x1B8
0253 #define VAS_NX_UTIL_SE          PPC_BITMASK(0, 63)
0254 
0255 #define VAS_NX_UTIL_ADDER_OFFSET    0x180
0256 #define VAS_NX_UTIL_ADDER       PPC_BITMASK(32, 63)
0257 
0258 /*
0259  * VREG(x):
0260  * Expand a register's short name (eg: LPID) into two parameters:
0261  *  - the register's short name in string form ("LPID"), and
0262  *  - the name of the macro (eg: VAS_LPID_OFFSET), defining the
0263  *    register's offset in the window context
0264  */
0265 #define VREG_SFX(n, s)  __stringify(n), VAS_##n##s
0266 #define VREG(r)     VREG_SFX(r, _OFFSET)
0267 
0268 /*
0269  * Local Notify Scope Control Register. (Receive windows only).
0270  */
0271 enum vas_notify_scope {
0272     VAS_SCOPE_LOCAL,
0273     VAS_SCOPE_GROUP,
0274     VAS_SCOPE_VECTORED_GROUP,
0275     VAS_SCOPE_UNUSED,
0276 };
0277 
0278 /*
0279  * Local DMA Cache Control Register (Receive windows only).
0280  */
0281 enum vas_dma_type {
0282     VAS_DMA_TYPE_INJECT,
0283     VAS_DMA_TYPE_WRITE,
0284 };
0285 
0286 /*
0287  * Local Notify Scope Control Register. (Receive windows only).
0288  * Not applicable to NX receive windows.
0289  */
0290 enum vas_notify_after_count {
0291     VAS_NOTIFY_AFTER_256 = 0,
0292     VAS_NOTIFY_NONE,
0293     VAS_NOTIFY_AFTER_2
0294 };
0295 
0296 /*
0297  * NX can generate an interrupt for multiple faults and expects kernel
0298  * to process all of them. So read all valid CRB entries until find the
0299  * invalid one. So use pswid which is pasted by NX and ccw[0] (reserved
0300  * bit in BE) to check valid CRB. CCW[0] will not be touched by user
0301  * space. Application gets CRB formt error if it updates this bit.
0302  *
0303  * Invalidate FIFO during allocation and process all entries from last
0304  * successful read until finds invalid pswid and ccw[0] values.
0305  * After reading each CRB entry from fault FIFO, the kernel invalidate
0306  * it by updating pswid with FIFO_INVALID_ENTRY and CCW[0] with
0307  * CCW0_INVALID.
0308  */
0309 #define FIFO_INVALID_ENTRY  0xffffffff
0310 #define CCW0_INVALID        1
0311 
0312 /*
0313  * One per instance of VAS. Each instance will have a separate set of
0314  * receive windows, one per coprocessor type.
0315  *
0316  * See also function header of set_vinst_win() for details on ->windows[]
0317  * and ->rxwin[] tables.
0318  */
0319 struct vas_instance {
0320     int vas_id;
0321     struct ida ida;
0322     struct list_head node;
0323     struct platform_device *pdev;
0324 
0325     u64 hvwc_bar_start;
0326     u64 uwc_bar_start;
0327     u64 paste_base_addr;
0328     u64 paste_win_id_shift;
0329 
0330     u64 irq_port;
0331     int virq;
0332     int fault_crbs;
0333     int fault_fifo_size;
0334     int fifo_in_progress;   /* To wake up thread or return IRQ_HANDLED */
0335     spinlock_t fault_lock;  /* Protects fifo_in_progress update */
0336     void *fault_fifo;
0337     struct pnv_vas_window *fault_win; /* Fault window */
0338 
0339     struct mutex mutex;
0340     struct pnv_vas_window *rxwin[VAS_COP_TYPE_MAX];
0341     struct pnv_vas_window *windows[VAS_WINDOWS_PER_CHIP];
0342 
0343     char *name;
0344     char *dbgname;
0345     struct dentry *dbgdir;
0346 };
0347 
0348 /*
0349  * In-kernel state a VAS window on PowerNV. One per window.
0350  */
0351 struct pnv_vas_window {
0352     struct vas_window vas_win;
0353     /* Fields common to send and receive windows */
0354     struct vas_instance *vinst;
0355     bool tx_win;        /* True if send window */
0356     bool nx_win;        /* True if NX window */
0357     bool user_win;      /* True if user space window */
0358     void *hvwc_map;     /* HV window context */
0359     void *uwc_map;      /* OS/User window context */
0360 
0361     /* Fields applicable only to send windows */
0362     void *paste_kaddr;
0363     char *paste_addr_name;
0364     struct pnv_vas_window *rxwin;
0365 
0366     /* Fields applicable only to receive windows */
0367     atomic_t num_txwins;
0368 };
0369 
0370 /*
0371  * Container for the hardware state of a window. One per-window.
0372  *
0373  * A VAS Window context is a 512-byte area in the hardware that contains
0374  * a set of 64-bit registers. Individual bit-fields in these registers
0375  * determine the configuration/operation of the hardware. struct vas_winctx
0376  * is a container for the register fields in the window context.
0377  */
0378 struct vas_winctx {
0379     u64 rx_fifo;
0380     int rx_fifo_size;
0381     int wcreds_max;
0382     int rsvd_txbuf_count;
0383 
0384     bool user_win;
0385     bool nx_win;
0386     bool fault_win;
0387     bool rsvd_txbuf_enable;
0388     bool pin_win;
0389     bool rej_no_credit;
0390     bool tx_wcred_mode;
0391     bool rx_wcred_mode;
0392     bool tx_word_mode;
0393     bool rx_word_mode;
0394     bool data_stamp;
0395     bool xtra_write;
0396     bool notify_disable;
0397     bool intr_disable;
0398     bool fifo_disable;
0399     bool notify_early;
0400     bool notify_os_intr_reg;
0401 
0402     int lpid;
0403     int pidr;       /* value from SPRN_PID, not linux pid */
0404     int lnotify_lpid;
0405     int lnotify_pid;
0406     int lnotify_tid;
0407     u32 pswid;
0408     int rx_win_id;
0409     int fault_win_id;
0410     int tc_mode;
0411 
0412     u64 irq_port;
0413 
0414     enum vas_dma_type dma_type;
0415     enum vas_notify_scope min_scope;
0416     enum vas_notify_scope max_scope;
0417     enum vas_notify_after_count notify_after_count;
0418 };
0419 
0420 extern struct mutex vas_mutex;
0421 
0422 extern struct vas_instance *find_vas_instance(int vasid);
0423 extern void vas_init_dbgdir(void);
0424 extern void vas_instance_init_dbgdir(struct vas_instance *vinst);
0425 extern void vas_window_init_dbgdir(struct pnv_vas_window *win);
0426 extern void vas_window_free_dbgdir(struct pnv_vas_window *win);
0427 extern int vas_setup_fault_window(struct vas_instance *vinst);
0428 extern irqreturn_t vas_fault_thread_fn(int irq, void *data);
0429 extern irqreturn_t vas_fault_handler(int irq, void *dev_id);
0430 extern void vas_return_credit(struct pnv_vas_window *window, bool tx);
0431 extern struct pnv_vas_window *vas_pswid_to_window(struct vas_instance *vinst,
0432                         uint32_t pswid);
0433 extern void vas_win_paste_addr(struct pnv_vas_window *window, u64 *addr,
0434                 int *len);
0435 
0436 static inline int vas_window_pid(struct vas_window *window)
0437 {
0438     return pid_vnr(window->task_ref.pid);
0439 }
0440 
0441 static inline void vas_log_write(struct pnv_vas_window *win, char *name,
0442             void *regptr, u64 val)
0443 {
0444     if (val)
0445         pr_debug("%swin #%d: %s reg %p, val 0x%016llx\n",
0446                 win->tx_win ? "Tx" : "Rx", win->vas_win.winid,
0447                 name, regptr, val);
0448 }
0449 
0450 static inline void write_uwc_reg(struct pnv_vas_window *win, char *name,
0451             s32 reg, u64 val)
0452 {
0453     void *regptr;
0454 
0455     regptr = win->uwc_map + reg;
0456     vas_log_write(win, name, regptr, val);
0457 
0458     out_be64(regptr, val);
0459 }
0460 
0461 static inline void write_hvwc_reg(struct pnv_vas_window *win, char *name,
0462             s32 reg, u64 val)
0463 {
0464     void *regptr;
0465 
0466     regptr = win->hvwc_map + reg;
0467     vas_log_write(win, name, regptr, val);
0468 
0469     out_be64(regptr, val);
0470 }
0471 
0472 static inline u64 read_hvwc_reg(struct pnv_vas_window *win,
0473             char *name __maybe_unused, s32 reg)
0474 {
0475     return in_be64(win->hvwc_map+reg);
0476 }
0477 
0478 /*
0479  * Encode/decode the Partition Send Window ID (PSWID) for a window in
0480  * a way that we can uniquely identify any window in the system. i.e.
0481  * we should be able to locate the 'struct vas_window' given the PSWID.
0482  *
0483  *  Bits    Usage
0484  *  0:7 VAS id (8 bits)
0485  *  8:15    Unused, 0 (3 bits)
0486  *  16:31   Window id (16 bits)
0487  */
0488 static inline u32 encode_pswid(int vasid, int winid)
0489 {
0490     return ((u32)winid | (vasid << (31 - 7)));
0491 }
0492 
0493 static inline void decode_pswid(u32 pswid, int *vasid, int *winid)
0494 {
0495     if (vasid)
0496         *vasid = pswid >> (31 - 7) & 0xFF;
0497 
0498     if (winid)
0499         *winid = pswid & 0xFFFF;
0500 }
0501 #endif /* _VAS_H */