1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * VAS user space API for its accelerators (Only NX-GZIP is supported now)
4 * Copyright (C) 2019 Haren Myneni, IBM Corp
7 #include <linux/kernel.h>
8 #include <linux/device.h>
9 #include <linux/cdev.h>
11 #include <linux/slab.h>
12 #include <linux/uaccess.h>
14 #include <uapi/asm/vas-api.h>
18 * The driver creates the device node that can be used as follows:
21 * fd = open("/dev/crypto/nx-gzip", O_RDWR);
22 * rc = ioctl(fd, VAS_TX_WIN_OPEN, &attr);
23 * paste_addr = mmap(NULL, PAGE_SIZE, prot, MAP_SHARED, fd, 0ULL).
24 * vas_copy(&crb, 0, 1);
25 * vas_paste(paste_addr, 0, 1);
26 * close(fd) or exit process to close window.
28 * where "vas_copy" and "vas_paste" are defined in copy-paste.h.
29 * copy/paste returns to the user space directly. So refer NX hardware
30 * documententation for exact copy/paste usage and completion / error
35 * Wrapper object for the nx-gzip device - there is just one instance of
36 * this node for the whole system.
38 static struct coproc_dev
{
40 struct device
*device
;
44 enum vas_cop_type cop_type
;
47 struct coproc_instance
{
48 struct coproc_dev
*coproc
;
49 struct vas_window
*txwin
;
52 static char *coproc_devnode(struct device
*dev
, umode_t
*mode
)
54 return kasprintf(GFP_KERNEL
, "crypto/%s", dev_name(dev
));
57 static int coproc_open(struct inode
*inode
, struct file
*fp
)
59 struct coproc_instance
*cp_inst
;
61 cp_inst
= kzalloc(sizeof(*cp_inst
), GFP_KERNEL
);
65 cp_inst
->coproc
= container_of(inode
->i_cdev
, struct coproc_dev
,
67 fp
->private_data
= cp_inst
;
72 static int coproc_ioc_tx_win_open(struct file
*fp
, unsigned long arg
)
74 void __user
*uptr
= (void __user
*)arg
;
75 struct vas_tx_win_attr txattr
= {};
76 struct vas_tx_win_open_attr uattr
;
77 struct coproc_instance
*cp_inst
;
78 struct vas_window
*txwin
;
81 cp_inst
= fp
->private_data
;
84 * One window for file descriptor
89 rc
= copy_from_user(&uattr
, uptr
, sizeof(uattr
));
91 pr_err("%s(): copy_from_user() returns %d\n", __func__
, rc
);
95 if (uattr
.version
!= 1) {
96 pr_err("Invalid version\n");
100 vasid
= uattr
.vas_id
;
102 vas_init_tx_win_attr(&txattr
, cp_inst
->coproc
->cop_type
);
104 txattr
.lpid
= mfspr(SPRN_LPID
);
105 txattr
.pidr
= mfspr(SPRN_PID
);
106 txattr
.user_win
= true;
107 txattr
.rsvd_txbuf_count
= false;
108 txattr
.pswid
= false;
110 pr_devel("Pid %d: Opening txwin, PIDR %ld\n", txattr
.pidr
,
113 txwin
= vas_tx_win_open(vasid
, cp_inst
->coproc
->cop_type
, &txattr
);
115 pr_err("%s() vas_tx_win_open() failed, %ld\n", __func__
,
117 return PTR_ERR(txwin
);
120 cp_inst
->txwin
= txwin
;
125 static int coproc_release(struct inode
*inode
, struct file
*fp
)
127 struct coproc_instance
*cp_inst
= fp
->private_data
;
129 if (cp_inst
->txwin
) {
130 vas_win_close(cp_inst
->txwin
);
131 cp_inst
->txwin
= NULL
;
135 fp
->private_data
= NULL
;
138 * We don't know here if user has other receive windows
139 * open, so we can't really call clear_thread_tidr().
140 * So, once the process calls set_thread_tidr(), the
141 * TIDR value sticks around until process exits, resulting
142 * in an extra copy in restore_sprs().
148 static int coproc_mmap(struct file
*fp
, struct vm_area_struct
*vma
)
150 struct coproc_instance
*cp_inst
= fp
->private_data
;
151 struct vas_window
*txwin
;
157 txwin
= cp_inst
->txwin
;
159 if ((vma
->vm_end
- vma
->vm_start
) > PAGE_SIZE
) {
160 pr_debug("%s(): size 0x%zx, PAGE_SIZE 0x%zx\n", __func__
,
161 (vma
->vm_end
- vma
->vm_start
), PAGE_SIZE
);
165 /* Ensure instance has an open send window */
167 pr_err("%s(): No send window open?\n", __func__
);
171 vas_win_paste_addr(txwin
, &paste_addr
, NULL
);
172 pfn
= paste_addr
>> PAGE_SHIFT
;
174 /* flags, page_prot from cxl_mmap(), except we want cachable */
175 vma
->vm_flags
|= VM_IO
| VM_PFNMAP
;
176 vma
->vm_page_prot
= pgprot_cached(vma
->vm_page_prot
);
178 prot
= __pgprot(pgprot_val(vma
->vm_page_prot
) | _PAGE_DIRTY
);
180 rc
= remap_pfn_range(vma
, vma
->vm_start
, pfn
+ vma
->vm_pgoff
,
181 vma
->vm_end
- vma
->vm_start
, prot
);
183 pr_devel("%s(): paste addr %llx at %lx, rc %d\n", __func__
,
184 paste_addr
, vma
->vm_start
, rc
);
189 static long coproc_ioctl(struct file
*fp
, unsigned int cmd
, unsigned long arg
)
192 case VAS_TX_WIN_OPEN
:
193 return coproc_ioc_tx_win_open(fp
, arg
);
199 static struct file_operations coproc_fops
= {
201 .release
= coproc_release
,
203 .unlocked_ioctl
= coproc_ioctl
,
207 * Supporting only nx-gzip coprocessor type now, but this API code
208 * extended to other coprocessor types later.
210 int vas_register_coproc_api(struct module
*mod
, enum vas_cop_type cop_type
,
216 rc
= alloc_chrdev_region(&coproc_device
.devt
, 1, 1, name
);
218 pr_err("Unable to allocate coproc major number: %i\n", rc
);
222 pr_devel("%s device allocated, dev [%i,%i]\n", name
,
223 MAJOR(coproc_device
.devt
), MINOR(coproc_device
.devt
));
225 coproc_device
.class = class_create(mod
, name
);
226 if (IS_ERR(coproc_device
.class)) {
227 rc
= PTR_ERR(coproc_device
.class);
228 pr_err("Unable to create %s class %d\n", name
, rc
);
231 coproc_device
.class->devnode
= coproc_devnode
;
232 coproc_device
.cop_type
= cop_type
;
234 coproc_fops
.owner
= mod
;
235 cdev_init(&coproc_device
.cdev
, &coproc_fops
);
237 devno
= MKDEV(MAJOR(coproc_device
.devt
), 0);
238 rc
= cdev_add(&coproc_device
.cdev
, devno
, 1);
240 pr_err("cdev_add() failed %d\n", rc
);
244 coproc_device
.device
= device_create(coproc_device
.class, NULL
,
245 devno
, NULL
, name
, MINOR(devno
));
246 if (IS_ERR(coproc_device
.device
)) {
247 rc
= PTR_ERR(coproc_device
.device
);
248 pr_err("Unable to create coproc-%d %d\n", MINOR(devno
), rc
);
252 pr_devel("%s: Added dev [%d,%d]\n", __func__
, MAJOR(devno
),
258 cdev_del(&coproc_device
.cdev
);
260 class_destroy(coproc_device
.class);
262 unregister_chrdev_region(coproc_device
.devt
, 1);
265 EXPORT_SYMBOL_GPL(vas_register_coproc_api
);
267 void vas_unregister_coproc_api(void)
271 cdev_del(&coproc_device
.cdev
);
272 devno
= MKDEV(MAJOR(coproc_device
.devt
), 0);
273 device_destroy(coproc_device
.class, devno
);
275 class_destroy(coproc_device
.class);
276 unregister_chrdev_region(coproc_device
.devt
, 1);
278 EXPORT_SYMBOL_GPL(vas_unregister_coproc_api
);