1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/file.h>
16#include <linux/idr.h>
17#include <linux/module.h>
18#include <linux/mount.h>
19#include <linux/poll.h>
20#include <linux/sched/signal.h>
21
22#include <misc/ocxl.h>
23
24#include <uapi/misc/cxl.h>
25
26#include "backend.h"
27#include "ocxl_hw.h"
28
29
30
31
32
33#define OCXLFLASH_FS_MAGIC 0x1697698f
34
35static int ocxlflash_fs_cnt;
36static struct vfsmount *ocxlflash_vfs_mount;
37
38static const struct dentry_operations ocxlflash_fs_dops = {
39 .d_dname = simple_dname,
40};
41
42
43
44
45
46
47
48
49
50
51static struct dentry *ocxlflash_fs_mount(struct file_system_type *fs_type,
52 int flags, const char *dev_name,
53 void *data)
54{
55 return mount_pseudo(fs_type, "ocxlflash:", NULL, &ocxlflash_fs_dops,
56 OCXLFLASH_FS_MAGIC);
57}
58
59static struct file_system_type ocxlflash_fs_type = {
60 .name = "ocxlflash",
61 .owner = THIS_MODULE,
62 .mount = ocxlflash_fs_mount,
63 .kill_sb = kill_anon_super,
64};
65
66
67
68
69
70static void ocxlflash_release_mapping(struct ocxlflash_context *ctx)
71{
72 if (ctx->mapping)
73 simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
74 ctx->mapping = NULL;
75}
76
77
78
79
80
81
82
83
84
85
86
87static struct file *ocxlflash_getfile(struct device *dev, const char *name,
88 const struct file_operations *fops,
89 void *priv, int flags)
90{
91 struct file *file;
92 struct inode *inode;
93 int rc;
94
95 if (fops->owner && !try_module_get(fops->owner)) {
96 dev_err(dev, "%s: Owner does not exist\n", __func__);
97 rc = -ENOENT;
98 goto err1;
99 }
100
101 rc = simple_pin_fs(&ocxlflash_fs_type, &ocxlflash_vfs_mount,
102 &ocxlflash_fs_cnt);
103 if (unlikely(rc < 0)) {
104 dev_err(dev, "%s: Cannot mount ocxlflash pseudofs rc=%d\n",
105 __func__, rc);
106 goto err2;
107 }
108
109 inode = alloc_anon_inode(ocxlflash_vfs_mount->mnt_sb);
110 if (IS_ERR(inode)) {
111 rc = PTR_ERR(inode);
112 dev_err(dev, "%s: alloc_anon_inode failed rc=%d\n",
113 __func__, rc);
114 goto err3;
115 }
116
117 file = alloc_file_pseudo(inode, ocxlflash_vfs_mount, name,
118 flags & (O_ACCMODE | O_NONBLOCK), fops);
119 if (IS_ERR(file)) {
120 rc = PTR_ERR(file);
121 dev_err(dev, "%s: alloc_file failed rc=%d\n",
122 __func__, rc);
123 goto err4;
124 }
125
126 file->private_data = priv;
127out:
128 return file;
129err4:
130 iput(inode);
131err3:
132 simple_release_fs(&ocxlflash_vfs_mount, &ocxlflash_fs_cnt);
133err2:
134 module_put(fops->owner);
135err1:
136 file = ERR_PTR(rc);
137 goto out;
138}
139
140
141
142
143
144
145
146static void __iomem *ocxlflash_psa_map(void *ctx_cookie)
147{
148 struct ocxlflash_context *ctx = ctx_cookie;
149 struct device *dev = ctx->hw_afu->dev;
150
151 mutex_lock(&ctx->state_mutex);
152 if (ctx->state != STARTED) {
153 dev_err(dev, "%s: Context not started, state=%d\n", __func__,
154 ctx->state);
155 mutex_unlock(&ctx->state_mutex);
156 return NULL;
157 }
158 mutex_unlock(&ctx->state_mutex);
159
160 return ioremap(ctx->psn_phys, ctx->psn_size);
161}
162
163
164
165
166
167static void ocxlflash_psa_unmap(void __iomem *addr)
168{
169 iounmap(addr);
170}
171
172
173
174
175
176
177
178static int ocxlflash_process_element(void *ctx_cookie)
179{
180 struct ocxlflash_context *ctx = ctx_cookie;
181
182 return ctx->pe;
183}
184
185
186
187
188
189
190
191
192
193
194
195
196static int afu_map_irq(u64 flags, struct ocxlflash_context *ctx, int num,
197 irq_handler_t handler, void *cookie, char *name)
198{
199 struct ocxl_hw_afu *afu = ctx->hw_afu;
200 struct device *dev = afu->dev;
201 struct ocxlflash_irqs *irq;
202 void __iomem *vtrig;
203 u32 virq;
204 int rc = 0;
205
206 if (num < 0 || num >= ctx->num_irqs) {
207 dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
208 rc = -ENOENT;
209 goto out;
210 }
211
212 irq = &ctx->irqs[num];
213 virq = irq_create_mapping(NULL, irq->hwirq);
214 if (unlikely(!virq)) {
215 dev_err(dev, "%s: irq_create_mapping failed\n", __func__);
216 rc = -ENOMEM;
217 goto out;
218 }
219
220 rc = request_irq(virq, handler, 0, name, cookie);
221 if (unlikely(rc)) {
222 dev_err(dev, "%s: request_irq failed rc=%d\n", __func__, rc);
223 goto err1;
224 }
225
226 vtrig = ioremap(irq->ptrig, PAGE_SIZE);
227 if (unlikely(!vtrig)) {
228 dev_err(dev, "%s: Trigger page mapping failed\n", __func__);
229 rc = -ENOMEM;
230 goto err2;
231 }
232
233 irq->virq = virq;
234 irq->vtrig = vtrig;
235out:
236 return rc;
237err2:
238 free_irq(virq, cookie);
239err1:
240 irq_dispose_mapping(virq);
241 goto out;
242}
243
244
245
246
247
248
249
250
251
252
253
254static int ocxlflash_map_afu_irq(void *ctx_cookie, int num,
255 irq_handler_t handler, void *cookie,
256 char *name)
257{
258 return afu_map_irq(0, ctx_cookie, num, handler, cookie, name);
259}
260
261
262
263
264
265
266
267
268static void afu_unmap_irq(u64 flags, struct ocxlflash_context *ctx, int num,
269 void *cookie)
270{
271 struct ocxl_hw_afu *afu = ctx->hw_afu;
272 struct device *dev = afu->dev;
273 struct ocxlflash_irqs *irq;
274
275 if (num < 0 || num >= ctx->num_irqs) {
276 dev_err(dev, "%s: Interrupt %d not allocated\n", __func__, num);
277 return;
278 }
279
280 irq = &ctx->irqs[num];
281 if (irq->vtrig)
282 iounmap(irq->vtrig);
283
284 if (irq_find_mapping(NULL, irq->hwirq)) {
285 free_irq(irq->virq, cookie);
286 irq_dispose_mapping(irq->virq);
287 }
288
289 memset(irq, 0, sizeof(*irq));
290}
291
292
293
294
295
296
297
298static void ocxlflash_unmap_afu_irq(void *ctx_cookie, int num, void *cookie)
299{
300 return afu_unmap_irq(0, ctx_cookie, num, cookie);
301}
302
303
304
305
306
307
308
309
310static u64 ocxlflash_get_irq_objhndl(void *ctx_cookie, int irq)
311{
312 struct ocxlflash_context *ctx = ctx_cookie;
313
314 if (irq < 0 || irq >= ctx->num_irqs)
315 return 0;
316
317 return (__force u64)ctx->irqs[irq].vtrig;
318}
319
320
321
322
323
324
325
326static void ocxlflash_xsl_fault(void *data, u64 addr, u64 dsisr)
327{
328 struct ocxlflash_context *ctx = data;
329
330 spin_lock(&ctx->slock);
331 ctx->fault_addr = addr;
332 ctx->fault_dsisr = dsisr;
333 ctx->pending_fault = true;
334 spin_unlock(&ctx->slock);
335
336 wake_up_all(&ctx->wq);
337}
338
339
340
341
342
343
344
345
346
347static int start_context(struct ocxlflash_context *ctx)
348{
349 struct ocxl_hw_afu *afu = ctx->hw_afu;
350 struct ocxl_afu_config *acfg = &afu->acfg;
351 void *link_token = afu->link_token;
352 struct device *dev = afu->dev;
353 bool master = ctx->master;
354 struct mm_struct *mm;
355 int rc = 0;
356 u32 pid;
357
358 mutex_lock(&ctx->state_mutex);
359 if (ctx->state != OPENED) {
360 dev_err(dev, "%s: Context state invalid, state=%d\n",
361 __func__, ctx->state);
362 rc = -EINVAL;
363 goto out;
364 }
365
366 if (master) {
367 ctx->psn_size = acfg->global_mmio_size;
368 ctx->psn_phys = afu->gmmio_phys;
369 } else {
370 ctx->psn_size = acfg->pp_mmio_stride;
371 ctx->psn_phys = afu->ppmmio_phys + (ctx->pe * ctx->psn_size);
372 }
373
374
375 if (master) {
376 pid = 0;
377 mm = NULL;
378 } else {
379 pid = current->mm->context.id;
380 mm = current->mm;
381 }
382
383 rc = ocxl_link_add_pe(link_token, ctx->pe, pid, 0, 0, mm,
384 ocxlflash_xsl_fault, ctx);
385 if (unlikely(rc)) {
386 dev_err(dev, "%s: ocxl_link_add_pe failed rc=%d\n",
387 __func__, rc);
388 goto out;
389 }
390
391 ctx->state = STARTED;
392out:
393 mutex_unlock(&ctx->state_mutex);
394 return rc;
395}
396
397
398
399
400
401
402
403static int ocxlflash_start_context(void *ctx_cookie)
404{
405 struct ocxlflash_context *ctx = ctx_cookie;
406
407 return start_context(ctx);
408}
409
410
411
412
413
414
415
416static int ocxlflash_stop_context(void *ctx_cookie)
417{
418 struct ocxlflash_context *ctx = ctx_cookie;
419 struct ocxl_hw_afu *afu = ctx->hw_afu;
420 struct ocxl_afu_config *acfg = &afu->acfg;
421 struct pci_dev *pdev = afu->pdev;
422 struct device *dev = afu->dev;
423 enum ocxlflash_ctx_state state;
424 int rc = 0;
425
426 mutex_lock(&ctx->state_mutex);
427 state = ctx->state;
428 ctx->state = CLOSED;
429 mutex_unlock(&ctx->state_mutex);
430 if (state != STARTED)
431 goto out;
432
433 rc = ocxl_config_terminate_pasid(pdev, acfg->dvsec_afu_control_pos,
434 ctx->pe);
435 if (unlikely(rc)) {
436 dev_err(dev, "%s: ocxl_config_terminate_pasid failed rc=%d\n",
437 __func__, rc);
438
439 if (rc == -EBUSY)
440 goto out;
441 }
442
443 rc = ocxl_link_remove_pe(afu->link_token, ctx->pe);
444 if (unlikely(rc)) {
445 dev_err(dev, "%s: ocxl_link_remove_pe failed rc=%d\n",
446 __func__, rc);
447 goto out;
448 }
449out:
450 return rc;
451}
452
453
454
455
456
457static int ocxlflash_afu_reset(void *ctx_cookie)
458{
459 struct ocxlflash_context *ctx = ctx_cookie;
460 struct device *dev = ctx->hw_afu->dev;
461
462
463 dev_err_once(dev, "%s: afu_reset() fop not supported\n", __func__);
464
465
466 return 0;
467}
468
469
470
471
472
473static void ocxlflash_set_master(void *ctx_cookie)
474{
475 struct ocxlflash_context *ctx = ctx_cookie;
476
477 ctx->master = true;
478}
479
480
481
482
483
484
485
486
487static void *ocxlflash_get_context(struct pci_dev *pdev, void *afu_cookie)
488{
489 struct ocxl_hw_afu *afu = afu_cookie;
490
491 return afu->ocxl_ctx;
492}
493
494
495
496
497
498
499
500
501static void *ocxlflash_dev_context_init(struct pci_dev *pdev, void *afu_cookie)
502{
503 struct ocxl_hw_afu *afu = afu_cookie;
504 struct device *dev = afu->dev;
505 struct ocxlflash_context *ctx;
506 int rc;
507
508 ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
509 if (unlikely(!ctx)) {
510 dev_err(dev, "%s: Context allocation failed\n", __func__);
511 rc = -ENOMEM;
512 goto err1;
513 }
514
515 idr_preload(GFP_KERNEL);
516 rc = idr_alloc(&afu->idr, ctx, 0, afu->max_pasid, GFP_NOWAIT);
517 idr_preload_end();
518 if (unlikely(rc < 0)) {
519 dev_err(dev, "%s: idr_alloc failed rc=%d\n", __func__, rc);
520 goto err2;
521 }
522
523 spin_lock_init(&ctx->slock);
524 init_waitqueue_head(&ctx->wq);
525 mutex_init(&ctx->state_mutex);
526
527 ctx->state = OPENED;
528 ctx->pe = rc;
529 ctx->master = false;
530 ctx->mapping = NULL;
531 ctx->hw_afu = afu;
532 ctx->irq_bitmap = 0;
533 ctx->pending_irq = false;
534 ctx->pending_fault = false;
535out:
536 return ctx;
537err2:
538 kfree(ctx);
539err1:
540 ctx = ERR_PTR(rc);
541 goto out;
542}
543
544
545
546
547
548
549
550static int ocxlflash_release_context(void *ctx_cookie)
551{
552 struct ocxlflash_context *ctx = ctx_cookie;
553 struct device *dev;
554 int rc = 0;
555
556 if (!ctx)
557 goto out;
558
559 dev = ctx->hw_afu->dev;
560 mutex_lock(&ctx->state_mutex);
561 if (ctx->state >= STARTED) {
562 dev_err(dev, "%s: Context in use, state=%d\n", __func__,
563 ctx->state);
564 mutex_unlock(&ctx->state_mutex);
565 rc = -EBUSY;
566 goto out;
567 }
568 mutex_unlock(&ctx->state_mutex);
569
570 idr_remove(&ctx->hw_afu->idr, ctx->pe);
571 ocxlflash_release_mapping(ctx);
572 kfree(ctx);
573out:
574 return rc;
575}
576
577
578
579
580
581
582static void ocxlflash_perst_reloads_same_image(void *afu_cookie, bool image)
583{
584 struct ocxl_hw_afu *afu = afu_cookie;
585
586 afu->perst_same_image = image;
587}
588
589
590
591
592
593
594
595
596
597static ssize_t ocxlflash_read_adapter_vpd(struct pci_dev *pdev, void *buf,
598 size_t count)
599{
600 return pci_read_vpd(pdev, 0, count, buf);
601}
602
603
604
605
606
607static void free_afu_irqs(struct ocxlflash_context *ctx)
608{
609 struct ocxl_hw_afu *afu = ctx->hw_afu;
610 struct device *dev = afu->dev;
611 int i;
612
613 if (!ctx->irqs) {
614 dev_err(dev, "%s: Interrupts not allocated\n", __func__);
615 return;
616 }
617
618 for (i = ctx->num_irqs; i >= 0; i--)
619 ocxl_link_free_irq(afu->link_token, ctx->irqs[i].hwirq);
620
621 kfree(ctx->irqs);
622 ctx->irqs = NULL;
623}
624
625
626
627
628
629
630
631
632static int alloc_afu_irqs(struct ocxlflash_context *ctx, int num)
633{
634 struct ocxl_hw_afu *afu = ctx->hw_afu;
635 struct device *dev = afu->dev;
636 struct ocxlflash_irqs *irqs;
637 u64 addr;
638 int rc = 0;
639 int hwirq;
640 int i;
641
642 if (ctx->irqs) {
643 dev_err(dev, "%s: Interrupts already allocated\n", __func__);
644 rc = -EEXIST;
645 goto out;
646 }
647
648 if (num > OCXL_MAX_IRQS) {
649 dev_err(dev, "%s: Too many interrupts num=%d\n", __func__, num);
650 rc = -EINVAL;
651 goto out;
652 }
653
654 irqs = kcalloc(num, sizeof(*irqs), GFP_KERNEL);
655 if (unlikely(!irqs)) {
656 dev_err(dev, "%s: Context irqs allocation failed\n", __func__);
657 rc = -ENOMEM;
658 goto out;
659 }
660
661 for (i = 0; i < num; i++) {
662 rc = ocxl_link_irq_alloc(afu->link_token, &hwirq, &addr);
663 if (unlikely(rc)) {
664 dev_err(dev, "%s: ocxl_link_irq_alloc failed rc=%d\n",
665 __func__, rc);
666 goto err;
667 }
668
669 irqs[i].hwirq = hwirq;
670 irqs[i].ptrig = addr;
671 }
672
673 ctx->irqs = irqs;
674 ctx->num_irqs = num;
675out:
676 return rc;
677err:
678 for (i = i-1; i >= 0; i--)
679 ocxl_link_free_irq(afu->link_token, irqs[i].hwirq);
680 kfree(irqs);
681 goto out;
682}
683
684
685
686
687
688
689
690
691static int ocxlflash_allocate_afu_irqs(void *ctx_cookie, int num)
692{
693 return alloc_afu_irqs(ctx_cookie, num);
694}
695
696
697
698
699
700static void ocxlflash_free_afu_irqs(void *ctx_cookie)
701{
702 free_afu_irqs(ctx_cookie);
703}
704
705
706
707
708
709static void ocxlflash_unconfig_afu(struct ocxl_hw_afu *afu)
710{
711 if (afu->gmmio_virt) {
712 iounmap(afu->gmmio_virt);
713 afu->gmmio_virt = NULL;
714 }
715}
716
717
718
719
720
721static void ocxlflash_destroy_afu(void *afu_cookie)
722{
723 struct ocxl_hw_afu *afu = afu_cookie;
724 int pos;
725
726 if (!afu)
727 return;
728
729 ocxlflash_release_context(afu->ocxl_ctx);
730 idr_destroy(&afu->idr);
731
732
733 pos = afu->acfg.dvsec_afu_control_pos;
734 ocxl_config_set_afu_state(afu->pdev, pos, 0);
735
736 ocxlflash_unconfig_afu(afu);
737 kfree(afu);
738}
739
740
741
742
743
744
745
746
747static int ocxlflash_config_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
748{
749 struct ocxl_fn_config *fcfg = &afu->fcfg;
750 struct device *dev = &pdev->dev;
751 u16 base, enabled, supported;
752 int rc = 0;
753
754
755 rc = ocxl_config_read_function(pdev, fcfg);
756 if (unlikely(rc)) {
757 dev_err(dev, "%s: ocxl_config_read_function failed rc=%d\n",
758 __func__, rc);
759 goto out;
760 }
761
762
763 if (fcfg->max_afu_index >= 0) {
764 afu->is_present = true;
765 if (fcfg->max_afu_index != 0)
766 dev_warn(dev, "%s: Unexpected AFU index value %d\n",
767 __func__, fcfg->max_afu_index);
768 }
769
770 rc = ocxl_config_get_actag_info(pdev, &base, &enabled, &supported);
771 if (unlikely(rc)) {
772 dev_err(dev, "%s: ocxl_config_get_actag_info failed rc=%d\n",
773 __func__, rc);
774 goto out;
775 }
776
777 afu->fn_actag_base = base;
778 afu->fn_actag_enabled = enabled;
779
780 ocxl_config_set_actag(pdev, fcfg->dvsec_function_pos, base, enabled);
781 dev_dbg(dev, "%s: Function acTag range base=%u enabled=%u\n",
782 __func__, base, enabled);
783
784 rc = ocxl_link_setup(pdev, 0, &afu->link_token);
785 if (unlikely(rc)) {
786 dev_err(dev, "%s: ocxl_link_setup failed rc=%d\n",
787 __func__, rc);
788 goto out;
789 }
790
791 rc = ocxl_config_set_TL(pdev, fcfg->dvsec_tl_pos);
792 if (unlikely(rc)) {
793 dev_err(dev, "%s: ocxl_config_set_TL failed rc=%d\n",
794 __func__, rc);
795 goto err;
796 }
797out:
798 return rc;
799err:
800 ocxl_link_release(pdev, afu->link_token);
801 goto out;
802}
803
804
805
806
807
808
809static void ocxlflash_unconfig_fn(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
810{
811 ocxl_link_release(pdev, afu->link_token);
812}
813
814
815
816
817
818
819
820static int ocxlflash_map_mmio(struct ocxl_hw_afu *afu)
821{
822 struct ocxl_afu_config *acfg = &afu->acfg;
823 struct pci_dev *pdev = afu->pdev;
824 struct device *dev = afu->dev;
825 phys_addr_t gmmio, ppmmio;
826 int rc = 0;
827
828 rc = pci_request_region(pdev, acfg->global_mmio_bar, "ocxlflash");
829 if (unlikely(rc)) {
830 dev_err(dev, "%s: pci_request_region for global failed rc=%d\n",
831 __func__, rc);
832 goto out;
833 }
834 gmmio = pci_resource_start(pdev, acfg->global_mmio_bar);
835 gmmio += acfg->global_mmio_offset;
836
837 rc = pci_request_region(pdev, acfg->pp_mmio_bar, "ocxlflash");
838 if (unlikely(rc)) {
839 dev_err(dev, "%s: pci_request_region for pp bar failed rc=%d\n",
840 __func__, rc);
841 goto err1;
842 }
843 ppmmio = pci_resource_start(pdev, acfg->pp_mmio_bar);
844 ppmmio += acfg->pp_mmio_offset;
845
846 afu->gmmio_virt = ioremap(gmmio, acfg->global_mmio_size);
847 if (unlikely(!afu->gmmio_virt)) {
848 dev_err(dev, "%s: MMIO mapping failed\n", __func__);
849 rc = -ENOMEM;
850 goto err2;
851 }
852
853 afu->gmmio_phys = gmmio;
854 afu->ppmmio_phys = ppmmio;
855out:
856 return rc;
857err2:
858 pci_release_region(pdev, acfg->pp_mmio_bar);
859err1:
860 pci_release_region(pdev, acfg->global_mmio_bar);
861 goto out;
862}
863
864
865
866
867
868
869
870
871
872
873static int ocxlflash_config_afu(struct pci_dev *pdev, struct ocxl_hw_afu *afu)
874{
875 struct ocxl_afu_config *acfg = &afu->acfg;
876 struct ocxl_fn_config *fcfg = &afu->fcfg;
877 struct device *dev = &pdev->dev;
878 int count;
879 int base;
880 int pos;
881 int rc = 0;
882
883
884 if (!afu->is_present)
885 goto out;
886
887
888 rc = ocxl_config_read_afu(pdev, fcfg, acfg, 0);
889 if (unlikely(rc)) {
890 dev_err(dev, "%s: ocxl_config_read_afu failed rc=%d\n",
891 __func__, rc);
892 goto out;
893 }
894
895
896 base = afu->fn_actag_base;
897 count = min_t(int, acfg->actag_supported, afu->fn_actag_enabled);
898 pos = acfg->dvsec_afu_control_pos;
899
900 ocxl_config_set_afu_actag(pdev, pos, base, count);
901 dev_dbg(dev, "%s: acTag base=%d enabled=%d\n", __func__, base, count);
902 afu->afu_actag_base = base;
903 afu->afu_actag_enabled = count;
904 afu->max_pasid = 1 << acfg->pasid_supported_log;
905
906 ocxl_config_set_afu_pasid(pdev, pos, 0, acfg->pasid_supported_log);
907
908 rc = ocxlflash_map_mmio(afu);
909 if (unlikely(rc)) {
910 dev_err(dev, "%s: ocxlflash_map_mmio failed rc=%d\n",
911 __func__, rc);
912 goto out;
913 }
914
915
916 ocxl_config_set_afu_state(pdev, acfg->dvsec_afu_control_pos, 1);
917out:
918 return rc;
919}
920
921
922
923
924
925
926
927static void *ocxlflash_create_afu(struct pci_dev *pdev)
928{
929 struct device *dev = &pdev->dev;
930 struct ocxlflash_context *ctx;
931 struct ocxl_hw_afu *afu;
932 int rc;
933
934 afu = kzalloc(sizeof(*afu), GFP_KERNEL);
935 if (unlikely(!afu)) {
936 dev_err(dev, "%s: HW AFU allocation failed\n", __func__);
937 goto out;
938 }
939
940 afu->pdev = pdev;
941 afu->dev = dev;
942 idr_init(&afu->idr);
943
944 rc = ocxlflash_config_fn(pdev, afu);
945 if (unlikely(rc)) {
946 dev_err(dev, "%s: Function configuration failed rc=%d\n",
947 __func__, rc);
948 goto err1;
949 }
950
951 rc = ocxlflash_config_afu(pdev, afu);
952 if (unlikely(rc)) {
953 dev_err(dev, "%s: AFU configuration failed rc=%d\n",
954 __func__, rc);
955 goto err2;
956 }
957
958 ctx = ocxlflash_dev_context_init(pdev, afu);
959 if (IS_ERR(ctx)) {
960 rc = PTR_ERR(ctx);
961 dev_err(dev, "%s: ocxlflash_dev_context_init failed rc=%d\n",
962 __func__, rc);
963 goto err3;
964 }
965
966 afu->ocxl_ctx = ctx;
967out:
968 return afu;
969err3:
970 ocxlflash_unconfig_afu(afu);
971err2:
972 ocxlflash_unconfig_fn(pdev, afu);
973err1:
974 idr_destroy(&afu->idr);
975 kfree(afu);
976 afu = NULL;
977 goto out;
978}
979
980
981
982
983
984
985
986static inline bool ctx_event_pending(struct ocxlflash_context *ctx)
987{
988 if (ctx->pending_irq || ctx->pending_fault)
989 return true;
990
991 return false;
992}
993
994
995
996
997
998
999
1000
1001static unsigned int afu_poll(struct file *file, struct poll_table_struct *poll)
1002{
1003 struct ocxlflash_context *ctx = file->private_data;
1004 struct device *dev = ctx->hw_afu->dev;
1005 ulong lock_flags;
1006 int mask = 0;
1007
1008 poll_wait(file, &ctx->wq, poll);
1009
1010 spin_lock_irqsave(&ctx->slock, lock_flags);
1011 if (ctx_event_pending(ctx))
1012 mask |= POLLIN | POLLRDNORM;
1013 else if (ctx->state == CLOSED)
1014 mask |= POLLERR;
1015 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1016
1017 dev_dbg(dev, "%s: Poll wait completed for pe %i mask %i\n",
1018 __func__, ctx->pe, mask);
1019
1020 return mask;
1021}
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032static ssize_t afu_read(struct file *file, char __user *buf, size_t count,
1033 loff_t *off)
1034{
1035 struct ocxlflash_context *ctx = file->private_data;
1036 struct device *dev = ctx->hw_afu->dev;
1037 struct cxl_event event;
1038 ulong lock_flags;
1039 ssize_t esize;
1040 ssize_t rc;
1041 int bit;
1042 DEFINE_WAIT(event_wait);
1043
1044 if (*off != 0) {
1045 dev_err(dev, "%s: Non-zero offset not supported, off=%lld\n",
1046 __func__, *off);
1047 rc = -EINVAL;
1048 goto out;
1049 }
1050
1051 spin_lock_irqsave(&ctx->slock, lock_flags);
1052
1053 for (;;) {
1054 prepare_to_wait(&ctx->wq, &event_wait, TASK_INTERRUPTIBLE);
1055
1056 if (ctx_event_pending(ctx) || (ctx->state == CLOSED))
1057 break;
1058
1059 if (file->f_flags & O_NONBLOCK) {
1060 dev_err(dev, "%s: File cannot be blocked on I/O\n",
1061 __func__);
1062 rc = -EAGAIN;
1063 goto err;
1064 }
1065
1066 if (signal_pending(current)) {
1067 dev_err(dev, "%s: Signal pending on the process\n",
1068 __func__);
1069 rc = -ERESTARTSYS;
1070 goto err;
1071 }
1072
1073 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1074 schedule();
1075 spin_lock_irqsave(&ctx->slock, lock_flags);
1076 }
1077
1078 finish_wait(&ctx->wq, &event_wait);
1079
1080 memset(&event, 0, sizeof(event));
1081 event.header.process_element = ctx->pe;
1082 event.header.size = sizeof(struct cxl_event_header);
1083 if (ctx->pending_irq) {
1084 esize = sizeof(struct cxl_event_afu_interrupt);
1085 event.header.size += esize;
1086 event.header.type = CXL_EVENT_AFU_INTERRUPT;
1087
1088 bit = find_first_bit(&ctx->irq_bitmap, ctx->num_irqs);
1089 clear_bit(bit, &ctx->irq_bitmap);
1090 event.irq.irq = bit + 1;
1091 if (bitmap_empty(&ctx->irq_bitmap, ctx->num_irqs))
1092 ctx->pending_irq = false;
1093 } else if (ctx->pending_fault) {
1094 event.header.size += sizeof(struct cxl_event_data_storage);
1095 event.header.type = CXL_EVENT_DATA_STORAGE;
1096 event.fault.addr = ctx->fault_addr;
1097 event.fault.dsisr = ctx->fault_dsisr;
1098 ctx->pending_fault = false;
1099 }
1100
1101 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1102
1103 if (copy_to_user(buf, &event, event.header.size)) {
1104 dev_err(dev, "%s: copy_to_user failed\n", __func__);
1105 rc = -EFAULT;
1106 goto out;
1107 }
1108
1109 rc = event.header.size;
1110out:
1111 return rc;
1112err:
1113 finish_wait(&ctx->wq, &event_wait);
1114 spin_unlock_irqrestore(&ctx->slock, lock_flags);
1115 goto out;
1116}
1117
1118
1119
1120
1121
1122
1123
1124
1125static int afu_release(struct inode *inode, struct file *file)
1126{
1127 struct ocxlflash_context *ctx = file->private_data;
1128 int i;
1129
1130
1131 for (i = ctx->num_irqs; i >= 0; i--)
1132 afu_unmap_irq(0, ctx, i, ctx);
1133 free_afu_irqs(ctx);
1134
1135 return ocxlflash_release_context(ctx);
1136}
1137
1138
1139
1140
1141
1142
1143
1144static vm_fault_t ocxlflash_mmap_fault(struct vm_fault *vmf)
1145{
1146 struct vm_area_struct *vma = vmf->vma;
1147 struct ocxlflash_context *ctx = vma->vm_file->private_data;
1148 struct device *dev = ctx->hw_afu->dev;
1149 u64 mmio_area, offset;
1150
1151 offset = vmf->pgoff << PAGE_SHIFT;
1152 if (offset >= ctx->psn_size)
1153 return VM_FAULT_SIGBUS;
1154
1155 mutex_lock(&ctx->state_mutex);
1156 if (ctx->state != STARTED) {
1157 dev_err(dev, "%s: Context not started, state=%d\n",
1158 __func__, ctx->state);
1159 mutex_unlock(&ctx->state_mutex);
1160 return VM_FAULT_SIGBUS;
1161 }
1162 mutex_unlock(&ctx->state_mutex);
1163
1164 mmio_area = ctx->psn_phys;
1165 mmio_area += offset;
1166
1167 return vmf_insert_pfn(vma, vmf->address, mmio_area >> PAGE_SHIFT);
1168}
1169
1170static const struct vm_operations_struct ocxlflash_vmops = {
1171 .fault = ocxlflash_mmap_fault,
1172};
1173
1174
1175
1176
1177
1178
1179
1180
1181static int afu_mmap(struct file *file, struct vm_area_struct *vma)
1182{
1183 struct ocxlflash_context *ctx = file->private_data;
1184
1185 if ((vma_pages(vma) + vma->vm_pgoff) >
1186 (ctx->psn_size >> PAGE_SHIFT))
1187 return -EINVAL;
1188
1189 vma->vm_flags |= VM_IO | VM_PFNMAP;
1190 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1191 vma->vm_ops = &ocxlflash_vmops;
1192 return 0;
1193}
1194
1195static const struct file_operations ocxl_afu_fops = {
1196 .owner = THIS_MODULE,
1197 .poll = afu_poll,
1198 .read = afu_read,
1199 .release = afu_release,
1200 .mmap = afu_mmap,
1201};
1202
1203#define PATCH_FOPS(NAME) \
1204 do { if (!fops->NAME) fops->NAME = ocxl_afu_fops.NAME; } while (0)
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214static struct file *ocxlflash_get_fd(void *ctx_cookie,
1215 struct file_operations *fops, int *fd)
1216{
1217 struct ocxlflash_context *ctx = ctx_cookie;
1218 struct device *dev = ctx->hw_afu->dev;
1219 struct file *file;
1220 int flags, fdtmp;
1221 int rc = 0;
1222 char *name = NULL;
1223
1224
1225 if (ctx->mapping) {
1226 dev_err(dev, "%s: Context is already mapped to an fd\n",
1227 __func__);
1228 rc = -EEXIST;
1229 goto err1;
1230 }
1231
1232 flags = O_RDWR | O_CLOEXEC;
1233
1234
1235 rc = get_unused_fd_flags(flags);
1236 if (unlikely(rc < 0)) {
1237 dev_err(dev, "%s: get_unused_fd_flags failed rc=%d\n",
1238 __func__, rc);
1239 goto err1;
1240 }
1241 fdtmp = rc;
1242
1243
1244 if (fops) {
1245 PATCH_FOPS(poll);
1246 PATCH_FOPS(read);
1247 PATCH_FOPS(release);
1248 PATCH_FOPS(mmap);
1249 } else
1250 fops = (struct file_operations *)&ocxl_afu_fops;
1251
1252 name = kasprintf(GFP_KERNEL, "ocxlflash:%d", ctx->pe);
1253 file = ocxlflash_getfile(dev, name, fops, ctx, flags);
1254 kfree(name);
1255 if (IS_ERR(file)) {
1256 rc = PTR_ERR(file);
1257 dev_err(dev, "%s: ocxlflash_getfile failed rc=%d\n",
1258 __func__, rc);
1259 goto err2;
1260 }
1261
1262 ctx->mapping = file->f_mapping;
1263 *fd = fdtmp;
1264out:
1265 return file;
1266err2:
1267 put_unused_fd(fdtmp);
1268err1:
1269 file = ERR_PTR(rc);
1270 goto out;
1271}
1272
1273
1274
1275
1276
1277
1278
1279static void *ocxlflash_fops_get_context(struct file *file)
1280{
1281 return file->private_data;
1282}
1283
1284
1285
1286
1287
1288
1289
1290
1291static irqreturn_t ocxlflash_afu_irq(int irq, void *data)
1292{
1293 struct ocxlflash_context *ctx = data;
1294 struct device *dev = ctx->hw_afu->dev;
1295 int i;
1296
1297 dev_dbg(dev, "%s: Interrupt raised for pe %i virq %i\n",
1298 __func__, ctx->pe, irq);
1299
1300 for (i = 0; i < ctx->num_irqs; i++) {
1301 if (ctx->irqs[i].virq == irq)
1302 break;
1303 }
1304 if (unlikely(i >= ctx->num_irqs)) {
1305 dev_err(dev, "%s: Received AFU IRQ out of range\n", __func__);
1306 goto out;
1307 }
1308
1309 spin_lock(&ctx->slock);
1310 set_bit(i - 1, &ctx->irq_bitmap);
1311 ctx->pending_irq = true;
1312 spin_unlock(&ctx->slock);
1313
1314 wake_up_all(&ctx->wq);
1315out:
1316 return IRQ_HANDLED;
1317}
1318
1319
1320
1321
1322
1323
1324
1325
1326static int ocxlflash_start_work(void *ctx_cookie, u64 num_irqs)
1327{
1328 struct ocxlflash_context *ctx = ctx_cookie;
1329 struct ocxl_hw_afu *afu = ctx->hw_afu;
1330 struct device *dev = afu->dev;
1331 char *name;
1332 int rc = 0;
1333 int i;
1334
1335 rc = alloc_afu_irqs(ctx, num_irqs);
1336 if (unlikely(rc < 0)) {
1337 dev_err(dev, "%s: alloc_afu_irqs failed rc=%d\n", __func__, rc);
1338 goto out;
1339 }
1340
1341 for (i = 0; i < num_irqs; i++) {
1342 name = kasprintf(GFP_KERNEL, "ocxlflash-%s-pe%i-%i",
1343 dev_name(dev), ctx->pe, i);
1344 rc = afu_map_irq(0, ctx, i, ocxlflash_afu_irq, ctx, name);
1345 kfree(name);
1346 if (unlikely(rc < 0)) {
1347 dev_err(dev, "%s: afu_map_irq failed rc=%d\n",
1348 __func__, rc);
1349 goto err;
1350 }
1351 }
1352
1353 rc = start_context(ctx);
1354 if (unlikely(rc)) {
1355 dev_err(dev, "%s: start_context failed rc=%d\n", __func__, rc);
1356 goto err;
1357 }
1358out:
1359 return rc;
1360err:
1361 for (i = i-1; i >= 0; i--)
1362 afu_unmap_irq(0, ctx, i, ctx);
1363 free_afu_irqs(ctx);
1364 goto out;
1365};
1366
1367
1368
1369
1370
1371
1372
1373
1374static int ocxlflash_fd_mmap(struct file *file, struct vm_area_struct *vma)
1375{
1376 return afu_mmap(file, vma);
1377}
1378
1379
1380
1381
1382
1383
1384
1385
1386static int ocxlflash_fd_release(struct inode *inode, struct file *file)
1387{
1388 return afu_release(inode, file);
1389}
1390
1391
1392const struct cxlflash_backend_ops cxlflash_ocxl_ops = {
1393 .module = THIS_MODULE,
1394 .psa_map = ocxlflash_psa_map,
1395 .psa_unmap = ocxlflash_psa_unmap,
1396 .process_element = ocxlflash_process_element,
1397 .map_afu_irq = ocxlflash_map_afu_irq,
1398 .unmap_afu_irq = ocxlflash_unmap_afu_irq,
1399 .get_irq_objhndl = ocxlflash_get_irq_objhndl,
1400 .start_context = ocxlflash_start_context,
1401 .stop_context = ocxlflash_stop_context,
1402 .afu_reset = ocxlflash_afu_reset,
1403 .set_master = ocxlflash_set_master,
1404 .get_context = ocxlflash_get_context,
1405 .dev_context_init = ocxlflash_dev_context_init,
1406 .release_context = ocxlflash_release_context,
1407 .perst_reloads_same_image = ocxlflash_perst_reloads_same_image,
1408 .read_adapter_vpd = ocxlflash_read_adapter_vpd,
1409 .allocate_afu_irqs = ocxlflash_allocate_afu_irqs,
1410 .free_afu_irqs = ocxlflash_free_afu_irqs,
1411 .create_afu = ocxlflash_create_afu,
1412 .destroy_afu = ocxlflash_destroy_afu,
1413 .get_fd = ocxlflash_get_fd,
1414 .fops_get_context = ocxlflash_fops_get_context,
1415 .start_work = ocxlflash_start_work,
1416 .fd_mmap = ocxlflash_fd_mmap,
1417 .fd_release = ocxlflash_fd_release,
1418};
1419