-
Notifications
You must be signed in to change notification settings - Fork 60
/
2001-opae-add-intel-fpga-drivers.patch-
11085 lines (11081 loc) · 286 KB
/
2001-opae-add-intel-fpga-drivers.patch-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
From 9c29ac842efedda96c2fe090b98a4f85830ba93f Mon Sep 17 00:00:00 2001
From: Miguel Bernal Marin <miguel.bernal.marin@linux.intel.com>
Date: Thu, 1 Feb 2018 11:05:28 -0600
Subject: [PATCH 2001/2002] opae: add intel fpga drivers
This driver are taken from https://github.com/OPAE/opae-sdk and ported
to mainline kernel tree.
Signed-off-by: Miguel Bernal Marin <miguel.bernal.marin@linux.intel.com>
---
drivers/fpga/intel/Kconfig | 38 +
drivers/fpga/intel/Makefile | 24 +
drivers/fpga/intel/afu-check.c | 149 +++
drivers/fpga/intel/afu-error.c | 276 ++++++
drivers/fpga/intel/afu.c | 1177 +++++++++++++++++++++++
drivers/fpga/intel/afu.h | 80 ++
drivers/fpga/intel/altera-asmip2.c | 466 +++++++++
drivers/fpga/intel/altera-asmip2.h | 24 +
drivers/fpga/intel/dma-region.c | 372 ++++++++
drivers/fpga/intel/feature-dev.c | 368 ++++++++
drivers/fpga/intel/feature-dev.h | 1839 ++++++++++++++++++++++++++++++++++++
drivers/fpga/intel/fme-dperf.c | 415 ++++++++
drivers/fpga/intel/fme-error.c | 452 +++++++++
drivers/fpga/intel/fme-iperf.c | 771 +++++++++++++++
drivers/fpga/intel/fme-main.c | 1124 ++++++++++++++++++++++
drivers/fpga/intel/fme-perf.c | 715 ++++++++++++++
drivers/fpga/intel/fme-pr.c | 487 ++++++++++
drivers/fpga/intel/fme.h | 88 ++
drivers/fpga/intel/pcie.c | 1404 +++++++++++++++++++++++++++
drivers/fpga/intel/pcie_check.c | 141 +++
drivers/fpga/intel/region.c | 130 +++
include/uapi/linux/intel-fpga.h | 354 +++++++
22 files changed, 10894 insertions(+)
create mode 100644 drivers/fpga/intel/Kconfig
create mode 100644 drivers/fpga/intel/Makefile
create mode 100644 drivers/fpga/intel/afu-check.c
create mode 100644 drivers/fpga/intel/afu-error.c
create mode 100644 drivers/fpga/intel/afu.c
create mode 100644 drivers/fpga/intel/afu.h
create mode 100644 drivers/fpga/intel/altera-asmip2.c
create mode 100644 drivers/fpga/intel/altera-asmip2.h
create mode 100644 drivers/fpga/intel/dma-region.c
create mode 100644 drivers/fpga/intel/feature-dev.c
create mode 100644 drivers/fpga/intel/feature-dev.h
create mode 100644 drivers/fpga/intel/fme-dperf.c
create mode 100644 drivers/fpga/intel/fme-error.c
create mode 100644 drivers/fpga/intel/fme-iperf.c
create mode 100644 drivers/fpga/intel/fme-main.c
create mode 100644 drivers/fpga/intel/fme-perf.c
create mode 100644 drivers/fpga/intel/fme-pr.c
create mode 100644 drivers/fpga/intel/fme.h
create mode 100644 drivers/fpga/intel/pcie.c
create mode 100644 drivers/fpga/intel/pcie_check.c
create mode 100644 drivers/fpga/intel/region.c
create mode 100644 include/uapi/linux/intel-fpga.h
diff --git a/drivers/fpga/intel/Kconfig b/drivers/fpga/intel/Kconfig
new file mode 100644
index 000000000000..3a7226811629
--- /dev/null
+++ b/drivers/fpga/intel/Kconfig
@@ -0,0 +1,38 @@
+#
+# Open Programmable Acceleration Engine (OPAE) kernel driver
+#
+
+config FPGA_INTEL_OPAE
+ bool "Enabling Open Programmable Acceleration Engine (OPAE) "
+ help
+ Enable the Open Programmable Acceleration Engine (OPAE) driver.
+ More info at https://01.org/OPAE
+
+
+if FPGA_INTEL_OPAE
+
+config FPGA_ALTERA_ASMIP2
+ tristate "OPAE ALTERA ASMIP2"
+ default m
+ help
+ Enable OPAE ALTERA ASMIP2
+
+config FPGA_INTEL_PCI
+ tristate "OPAE PCI"
+ default m
+ help
+ Enable OPAE PCI
+
+config FPGA_INTEL_FME
+ tristate "OPAE FME"
+ default m
+ help
+ Enable OPAE FME
+
+config FPGA_INTEL_AFU
+ tristate "OPAE AFU"
+ default m
+ help
+ Enable OPAE AFU
+
+endif # FPGA_INTEL_OPAE
diff --git a/drivers/fpga/intel/Makefile b/drivers/fpga/intel/Makefile
new file mode 100644
index 000000000000..2f83f1b20c27
--- /dev/null
+++ b/drivers/fpga/intel/Makefile
@@ -0,0 +1,24 @@
+
+ccflags-y += -Wno-unused-value -Wno-unused-label
+ccflags-y += -DCONFIG_AS_AVX512
+
+obj-$(CONFIG_FPGA_ALTERA_ASMIP2) += altera-asmip2.o
+obj-$(CONFIG_FPGA_INTEL_PCI) += intel-fpga-pci.o
+obj-$(CONFIG_FPGA_INTEL_FME) += intel-fpga-fme.o
+obj-$(CONFIG_FPGA_INTEL_AFU) += intel-fpga-afu.o
+
+intel-fpga-pci-y := pcie.o \
+ pcie_check.o \
+ feature-dev.o
+
+intel-fpga-fme-y := fme-pr.o \
+ fme-iperf.o \
+ fme-dperf.o \
+ fme-error.o \
+ fme-main.o
+
+intel-fpga-afu-y := afu.o \
+ region.o \
+ dma-region.o \
+ afu-error.o \
+ afu-check.o
diff --git a/drivers/fpga/intel/afu-check.c b/drivers/fpga/intel/afu-check.c
new file mode 100644
index 000000000000..a75871e30c9e
--- /dev/null
+++ b/drivers/fpga/intel/afu-check.c
@@ -0,0 +1,149 @@
+#include "afu.h"
+
+static void port_check_reg(struct device *dev, void __iomem *addr,
+ const char *reg_name, u64 dflt)
+{
+ u64 value = readq(addr);
+
+ if (value != dflt)
+ dev_dbg(dev, "%s: incorrect value 0x%llx vs defautl 0x%llx\n",
+ reg_name, (unsigned long long)value,
+ (unsigned long long)dflt);
+}
+
+struct feature_port_header hdr_dflt = {
+ .port_mailbox = 0x0000000000000000,
+ .scratchpad = 0x0000000000000000,
+ .capability = {
+ .csr = 0x0000000100010000,
+ },
+ .control = {
+ /* Port Reset Bit is cleared in PCIe driver */
+ .csr = 0x0000000000000004,
+ },
+ .status = {
+ .csr = 0x0000000000000000,
+ },
+ .rsvd2 = 0x0000000000000000,
+ .user_clk_freq_cmd0 = 0x0000000000000000,
+ .user_clk_freq_cmd1 = 0x0000000000000000,
+ .user_clk_freq_sts0 = 0x0000000000000000,
+ .user_clk_freq_sts1 = 0x0000000000000000,
+};
+
+int port_hdr_test(struct platform_device *pdev, struct feature *feature)
+{
+ struct feature_port_header *port_hdr = feature->ioaddr;
+
+ /* Check if default value of hardware registers matches with spec */
+ port_check_reg(&pdev->dev, &port_hdr->port_mailbox,
+ "hdr:port_mailbox", hdr_dflt.port_mailbox);
+ port_check_reg(&pdev->dev, &port_hdr->scratchpad,
+ "hdr:scratchpad", hdr_dflt.scratchpad);
+ port_check_reg(&pdev->dev, &port_hdr->capability,
+ "hdr:capability", hdr_dflt.capability.csr);
+ port_check_reg(&pdev->dev, &port_hdr->control,
+ "hdr:control", hdr_dflt.control.csr);
+ port_check_reg(&pdev->dev, &port_hdr->status,
+ "hdr:status", hdr_dflt.status.csr);
+ port_check_reg(&pdev->dev, &port_hdr->rsvd2,
+ "hdr:rsvd2", hdr_dflt.rsvd2);
+ port_check_reg(&pdev->dev, &port_hdr->user_clk_freq_cmd0,
+ "hdr:user_clk_cmd0", hdr_dflt.user_clk_freq_cmd0);
+ port_check_reg(&pdev->dev, &port_hdr->user_clk_freq_cmd1,
+ "hdr:user_clk_cmd1", hdr_dflt.user_clk_freq_cmd1);
+ port_check_reg(&pdev->dev, &port_hdr->user_clk_freq_sts0,
+ "hdr:user_clk_sts0", hdr_dflt.user_clk_freq_sts0);
+ port_check_reg(&pdev->dev, &port_hdr->user_clk_freq_sts1,
+ "hdr:user_clk_sts1", hdr_dflt.user_clk_freq_sts1);
+
+ dev_dbg(&pdev->dev, "%s finished\n", __func__);
+
+ return 0;
+}
+
+struct feature_port_error err_dflt = {
+ .error_mask = {
+ .csr = 0x0000000000000000,
+ },
+ .port_error = {
+ .csr = 0x0000000000000000,
+ },
+ .port_first_error = {
+ .csr = 0x0000000000000000,
+ },
+ .malreq0 = {
+ .header_lsb = 0x0000000000000000,
+ },
+ .malreq1 = {
+ .header_msb = 0x0000000000000000,
+ },
+ .port_debug = {
+ .port_debug = 0x0000000000000000,
+ },
+};
+
+int port_err_test(struct platform_device *pdev, struct feature *feature)
+{
+ struct feature_port_error *port_err = feature->ioaddr;
+
+ port_check_reg(&pdev->dev, &port_err->error_mask,
+ "err:error_mask", err_dflt.error_mask.csr);
+ port_check_reg(&pdev->dev, &port_err->port_error,
+ "err:port_error", err_dflt.port_error.csr);
+ port_check_reg(&pdev->dev, &port_err->port_first_error,
+ "err:port_first_err", err_dflt.port_first_error.csr);
+ port_check_reg(&pdev->dev, &port_err->malreq0,
+ "err:malreq0", err_dflt.malreq0.header_lsb);
+ port_check_reg(&pdev->dev, &port_err->malreq1,
+ "err:malreq1", err_dflt.malreq1.header_msb);
+ port_check_reg(&pdev->dev, &port_err->port_debug,
+ "err:port_debug", err_dflt.port_debug.port_debug);
+
+ dev_dbg(&pdev->dev, "%s finished\n", __func__);
+ return 0;
+}
+
+struct feature_port_umsg umsg_dflt = {
+ .capability = {
+ .csr = 0x0000000000000008,
+ },
+ .baseaddr = {
+ .csr = 0x0000000000000000,
+ },
+ .mode = {
+ .csr = 0x0000000000000000,
+ },
+};
+
+int port_umsg_test(struct platform_device *pdev, struct feature *feature)
+{
+ struct feature_port_umsg *port_umsg = feature->ioaddr;
+
+ port_check_reg(&pdev->dev, &port_umsg->capability,
+ "umsg:capaiblity", umsg_dflt.capability.csr);
+ port_check_reg(&pdev->dev, &port_umsg->baseaddr,
+ "umsg:baseaddr", umsg_dflt.baseaddr.csr);
+ port_check_reg(&pdev->dev, &port_umsg->mode,
+ "umsg:mode", umsg_dflt.mode.csr);
+
+ dev_dbg(&pdev->dev, "%s finished\n", __func__);
+ return 0;
+}
+
+struct feature_port_stp stp_dflt = {
+ .stp_status = {
+ .csr = 0x0000000000000000,
+ },
+};
+
+int port_stp_test(struct platform_device *pdev, struct feature *feature)
+{
+ struct feature_port_stp *port_stp = feature->ioaddr;
+
+ port_check_reg(&pdev->dev, &port_stp->stp_status,
+ "stp:stp_csr", stp_dflt.stp_status.csr);
+
+ dev_dbg(&pdev->dev, "%s finished\n", __func__);
+ return 0;
+}
diff --git a/drivers/fpga/intel/afu-error.c b/drivers/fpga/intel/afu-error.c
new file mode 100644
index 000000000000..32b151c11098
--- /dev/null
+++ b/drivers/fpga/intel/afu-error.c
@@ -0,0 +1,276 @@
+/*
+ * Driver for FPGA Accelerated Function Unit (AFU) Error Handling
+ *
+ * Copyright 2016 Intel Corporation, Inc.
+ *
+ * Authors:
+ * Wu Hao <hao.wu@linux.intel.com>
+ * Xiao Guangrong <guangrong.xiao@linux.intel.com>
+ * Joseph Grecco <joe.grecco@intel.com>
+ * Enno Luebbers <enno.luebbers@intel.com>
+ * Tim Whisonant <tim.whisonant@intel.com>
+ * Ananda Ravuri <ananda.ravuri@intel.com>
+ * Mitchel, Henry <henry.mitchel@intel.com>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include <linux/uaccess.h>
+#include "afu.h"
+
+/* Mask / Unmask Port Errors by the Error Mask register. */
+static void port_err_mask(struct device *dev, bool mask)
+{
+ struct feature_port_error *port_err;
+ struct feature_port_err_key err_mask;
+
+ port_err = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_ERROR);
+
+ if (mask)
+ err_mask.csr = PORT_ERR_MASK;
+ else
+ err_mask.csr = 0;
+
+ writeq(err_mask.csr, &port_err->error_mask);
+}
+
+/* Clear All Port Errors. */
+static int port_err_clear(struct device *dev, u64 err)
+{
+ struct feature_port_header *port_hdr;
+ struct feature_port_error *port_err;
+ struct feature_port_err_key mask;
+ struct feature_port_first_err_key first;
+ struct feature_port_status status;
+ int ret = 0;
+
+ port_err = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_ERROR);
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ /*
+ * Clear All Port Errors
+ *
+ * - Check for AP6 State
+ * - Halt Port by keeping Port in reset
+ * - Set PORT Error mask to all 1 to mask errors
+ * - Clear all errors
+ * - Set Port mask to all 0 to enable errors
+ * - All errors start capturing new errors
+ * - Enable Port by pulling the port out of reset
+ */
+
+ /* If device is still in AP6 state, can not clear any error.*/
+ status.csr = readq(&port_hdr->status);
+ if (status.power_state == PORT_POWER_STATE_AP6) {
+ dev_err(dev, "Could not clear errors, device in AP6 state.\n");
+ return -EBUSY;
+ }
+
+ /* Halt Port by keeping Port in reset */
+ ret = __fpga_port_disable(to_platform_device(dev));
+ if (ret)
+ return ret;
+
+ /* Mask all errors */
+ port_err_mask(dev, true);
+
+ /* Clear errors if err input matches with current port errors.*/
+ mask.csr = readq(&port_err->port_error);
+
+ if (mask.csr == err) {
+ writeq(mask.csr, &port_err->port_error);
+
+ first.csr = readq(&port_err->port_first_error);
+ writeq(first.csr, &port_err->port_first_error);
+ } else
+ ret = -EBUSY;
+
+ /* Clear mask */
+ port_err_mask(dev, false);
+
+ /* Enable the Port by clear the reset */
+ __fpga_port_enable(to_platform_device(dev));
+
+ return ret;
+}
+
+static ssize_t
+revision_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct feature_port_error *port_err;
+ struct feature_header header;
+
+ port_err = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_ERROR);
+
+ header.csr = readq(&port_err->header);
+
+ return scnprintf(buf, PAGE_SIZE, "%d\n", header.revision);
+}
+static DEVICE_ATTR_RO(revision);
+
+static ssize_t
+errors_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct feature_port_error *port_err;
+ struct feature_port_err_key error;
+
+ port_err = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_ERROR);
+
+ error.csr = readq(&port_err->port_error);
+
+ return scnprintf(buf, PAGE_SIZE, "0x%llx\n",
+ (unsigned long long)error.csr);
+}
+static DEVICE_ATTR_RO(errors);
+
+static ssize_t
+first_error_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct feature_port_error *port_err;
+ struct feature_port_first_err_key first_error;
+
+ port_err = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_ERROR);
+
+ first_error.csr = readq(&port_err->port_first_error);
+
+ return scnprintf(buf, PAGE_SIZE, "0x%llx\n",
+ (unsigned long long)first_error.csr);
+}
+static DEVICE_ATTR_RO(first_error);
+
+static ssize_t first_malformed_req_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct feature_port_error *port_err;
+ struct feature_port_malformed_req0 malreq0;
+ struct feature_port_malformed_req1 malreq1;
+
+ port_err = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_ERROR);
+
+ malreq0.header_lsb = readq(&port_err->malreq0);
+ malreq1.header_msb = readq(&port_err->malreq1);
+
+ return scnprintf(buf, PAGE_SIZE, "0x%016llx%016llx\n",
+ (unsigned long long)malreq1.header_msb,
+ (unsigned long long)malreq0.header_lsb);
+}
+static DEVICE_ATTR_RO(first_malformed_req);
+
+static ssize_t clear_store(struct device *dev,
+ struct device_attribute *attr, const char *buff, size_t count)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ int ret;
+ u64 value;
+
+ if (kstrtou64(buff, 0, &value))
+ return -EINVAL;
+
+ WARN_ON(!is_feature_present(dev, PORT_FEATURE_ID_HEADER));
+
+ mutex_lock(&pdata->lock);
+ ret = port_err_clear(dev, value);
+ mutex_unlock(&pdata->lock);
+
+ if (ret)
+ return ret;
+ else
+ return count;
+}
+static DEVICE_ATTR_WO(clear);
+
+static struct attribute *port_err_attrs[] = {
+ &dev_attr_revision.attr,
+ &dev_attr_errors.attr,
+ &dev_attr_first_error.attr,
+ &dev_attr_first_malformed_req.attr,
+ &dev_attr_clear.attr,
+ NULL,
+};
+
+static struct attribute_group port_err_attr_group = {
+ .attrs = port_err_attrs,
+ .name = "errors",
+};
+
+static int port_err_init(struct platform_device *pdev, struct feature *feature)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+ struct fpga_afu *afu;
+
+ dev_dbg(&pdev->dev, "PORT ERR Init.\n");
+
+ mutex_lock(&pdata->lock);
+ port_err_mask(&pdev->dev, false);
+ afu = fpga_pdata_get_private(pdata);
+ if (feature->ctx_num)
+ afu->capability |= FPGA_PORT_CAP_ERR_IRQ;
+ mutex_unlock(&pdata->lock);
+
+ return sysfs_create_group(&pdev->dev.kobj, &port_err_attr_group);
+}
+
+static void port_err_uinit(struct platform_device *pdev,
+ struct feature *feature)
+{
+ dev_dbg(&pdev->dev, "PORT ERR UInit.\n");
+
+ sysfs_remove_group(&pdev->dev.kobj, &port_err_attr_group);
+}
+
+static long port_err_set_irq(struct platform_device *pdev,
+ struct feature *feature, unsigned long arg)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+ struct fpga_port_err_irq_set hdr;
+ struct fpga_afu *afu;
+ unsigned long minsz;
+ long ret;
+
+ minsz = offsetofend(struct fpga_port_err_irq_set, evtfd);
+
+ if (copy_from_user(&hdr, (void __user *)arg, minsz))
+ return -EFAULT;
+
+ if (hdr.argsz < minsz || hdr.flags)
+ return -EINVAL;
+
+ mutex_lock(&pdata->lock);
+ afu = fpga_pdata_get_private(pdata);
+ if (!(afu->capability & FPGA_PORT_CAP_ERR_IRQ)) {
+ mutex_unlock(&pdata->lock);
+ return -ENODEV;
+ }
+
+ ret = fpga_msix_set_block(feature, 0, 1, &hdr.evtfd);
+ mutex_unlock(&pdata->lock);
+
+ return ret;
+}
+
+static long
+port_err_ioctl(struct platform_device *pdev, struct feature *feature,
+ unsigned int cmd, unsigned long arg)
+{
+ long ret;
+
+ switch (cmd) {
+ case FPGA_PORT_ERR_SET_IRQ:
+ ret = port_err_set_irq(pdev, feature, arg);
+ break;
+ default:
+ dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
+ return -ENODEV;
+ }
+
+ return ret;
+}
+
+struct feature_ops port_err_ops = {
+ .init = port_err_init,
+ .uinit = port_err_uinit,
+ .ioctl = port_err_ioctl,
+ .test = port_err_test,
+};
diff --git a/drivers/fpga/intel/afu.c b/drivers/fpga/intel/afu.c
new file mode 100644
index 000000000000..27e31dfc2d64
--- /dev/null
+++ b/drivers/fpga/intel/afu.c
@@ -0,0 +1,1177 @@
+/*
+ * Driver for FPGA Accelerated Function Unit (AFU)
+ *
+ * Copyright 2016 Intel Corporation, Inc.
+ *
+ * Authors:
+ * Wu Hao <hao.wu@linux.intel.com>
+ * Xiao Guangrong <guangrong.xiao@linux.intel.com>
+ * Joseph Grecco <joe.grecco@intel.com>
+ * Enno Luebbers <enno.luebbers@intel.com>
+ * Tim Whisonant <tim.whisonant@intel.com>
+ * Ananda Ravuri <ananda.ravuri@intel.com>
+ * Mitchel, Henry <henry.mitchel@intel.com>
+ *
+ * This work is licensed under the terms of the GNU GPL version 2. See
+ * the COPYING file in the top-level directory.
+ *
+ */
+
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/uaccess.h>
+#include <linux/stddef.h>
+#include <linux/errno.h>
+#include <linux/delay.h>
+#include <linux/fs.h>
+#include <linux/dma-mapping.h>
+#include <linux/intel-fpga.h>
+
+#include "afu.h"
+
+/* sysfs attributes for port_hdr feature */
+static ssize_t
+revision_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct feature_port_header *port_hdr
+ = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+ struct feature_header header;
+
+ header.csr = readq(&port_hdr->header);
+
+ return scnprintf(buf, PAGE_SIZE, "%d\n", header.revision);
+}
+
+static DEVICE_ATTR_RO(revision);
+
+static ssize_t
+id_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ int id = fpga_port_id(to_platform_device(dev));
+
+ return scnprintf(buf, PAGE_SIZE, "%d\n", id);
+}
+static DEVICE_ATTR_RO(id);
+
+static ssize_t
+ltr_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct feature_port_header *port_hdr;
+ struct feature_port_control control;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ control.csr = readq(&port_hdr->control);
+
+ return scnprintf(buf, PAGE_SIZE, "%d\n", control.latency_tolerance);
+}
+static DEVICE_ATTR_RO(ltr);
+
+static ssize_t
+ap1_event_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ struct feature_port_status status;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ status.csr = readq(&port_hdr->status);
+ mutex_unlock(&pdata->lock);
+
+ return scnprintf(buf, PAGE_SIZE, "%d\n", status.ap1_event);
+}
+
+static ssize_t
+ap1_event_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ struct feature_port_status status;
+ u8 ap1_event;
+ int err;
+
+ err = kstrtou8(buf, 0, &ap1_event);
+ if (err)
+ return err;
+
+ if (ap1_event != 1)
+ return -EINVAL;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ status.csr = readq(&port_hdr->status);
+ status.ap1_event = ap1_event;
+ writeq(status.csr, &port_hdr->status);
+ mutex_unlock(&pdata->lock);
+
+ return count;
+}
+static DEVICE_ATTR_RW(ap1_event);
+
+static ssize_t
+ap2_event_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ struct feature_port_status status;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ status.csr = readq(&port_hdr->status);
+ mutex_unlock(&pdata->lock);
+
+ return scnprintf(buf, PAGE_SIZE, "%d\n", status.ap2_event);
+}
+
+static ssize_t
+ap2_event_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ struct feature_port_status status;
+ u8 ap2_event;
+ int err;
+
+ err = kstrtou8(buf, 0, &ap2_event);
+ if (err)
+ return err;
+
+ if (ap2_event != 1)
+ return -EINVAL;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ status.csr = readq(&port_hdr->status);
+ status.ap2_event = ap2_event;
+ writeq(status.csr, &port_hdr->status);
+ mutex_unlock(&pdata->lock);
+
+ return count;
+}
+static DEVICE_ATTR_RW(ap2_event);
+
+static ssize_t
+power_state_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ struct feature_port_status status;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ status.csr = readq(&port_hdr->status);
+ mutex_unlock(&pdata->lock);
+
+ return scnprintf(buf, PAGE_SIZE, "0x%x\n", status.power_state);
+}
+static DEVICE_ATTR_RO(power_state);
+
+static ssize_t
+userclk_freqcmd_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ u64 userclk_freq_cmd;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ userclk_freq_cmd = readq(&port_hdr->user_clk_freq_cmd0);
+ mutex_unlock(&pdata->lock);
+
+ return scnprintf(buf, PAGE_SIZE, "0x%llx\n", userclk_freq_cmd);
+}
+
+static ssize_t
+userclk_freqcmd_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ u64 userclk_freq_cmd;
+ int err;
+
+ err = kstrtou64(buf, 0, &userclk_freq_cmd);
+ if (err)
+ return err;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ writeq(userclk_freq_cmd, &port_hdr->user_clk_freq_cmd0);
+ mutex_unlock(&pdata->lock);
+
+ return count;
+}
+static DEVICE_ATTR_RW(userclk_freqcmd);
+
+static ssize_t
+userclk_freqcntrcmd_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ u64 userclk_freqcntr_cmd;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ userclk_freqcntr_cmd = readq(&port_hdr->user_clk_freq_cmd1);
+ mutex_unlock(&pdata->lock);
+
+ return scnprintf(buf, PAGE_SIZE, "0x%llx\n", userclk_freqcntr_cmd);
+}
+
+static ssize_t
+userclk_freqcntrcmd_store(struct device *dev, struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr;
+ u64 userclk_freqcntr_cmd;
+ int err;
+
+ err = kstrtou64(buf, 0, &userclk_freqcntr_cmd);
+ if (err)
+ return err;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ mutex_lock(&pdata->lock);
+ writeq(userclk_freqcntr_cmd, &port_hdr->user_clk_freq_cmd1);
+ mutex_unlock(&pdata->lock);
+
+ return count;
+}
+static DEVICE_ATTR_RW(userclk_freqcntrcmd);
+
+static ssize_t
+userclk_freqsts_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct feature_port_header *port_hdr;
+ u64 userclk_freq_sts;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ userclk_freq_sts = readq(&port_hdr->user_clk_freq_sts0);
+
+ return scnprintf(buf, PAGE_SIZE, "0x%llx\n", userclk_freq_sts);
+}
+static DEVICE_ATTR_RO(userclk_freqsts);
+
+static ssize_t
+userclk_freqcntrsts_show(struct device *dev, struct device_attribute *attr,
+ char *buf)
+{
+ struct feature_port_header *port_hdr;
+ u64 userclk_freqcntr_sts;
+
+ port_hdr = get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_HEADER);
+
+ userclk_freqcntr_sts = readq(&port_hdr->user_clk_freq_sts1);
+
+ return scnprintf(buf, PAGE_SIZE, "0x%llx\n", userclk_freqcntr_sts);
+}
+static DEVICE_ATTR_RO(userclk_freqcntrsts);
+
+static const struct attribute *port_hdr_attrs[] = {
+ &dev_attr_revision.attr,
+ &dev_attr_id.attr,
+ &dev_attr_ltr.attr,
+ &dev_attr_ap1_event.attr,
+ &dev_attr_ap2_event.attr,
+ &dev_attr_power_state.attr,
+ &dev_attr_userclk_freqcmd.attr,
+ &dev_attr_userclk_freqcntrcmd.attr,
+ &dev_attr_userclk_freqsts.attr,
+ &dev_attr_userclk_freqcntrsts.attr,
+ NULL,
+};
+
+static int port_hdr_init(struct platform_device *pdev, struct feature *feature)
+{
+ dev_dbg(&pdev->dev, "PORT HDR Init.\n");
+
+ fpga_port_reset(pdev);
+
+ return sysfs_create_files(&pdev->dev.kobj, port_hdr_attrs);
+}
+
+static void port_hdr_uinit(struct platform_device *pdev,
+ struct feature *feature)
+{
+ dev_dbg(&pdev->dev, "PORT HDR UInit.\n");
+
+ sysfs_remove_files(&pdev->dev.kobj, port_hdr_attrs);
+}
+
+static long
+port_hdr_ioctl(struct platform_device *pdev, struct feature *feature,
+ unsigned int cmd, unsigned long arg)
+{
+ long ret;
+
+ switch (cmd) {
+ case FPGA_PORT_RESET:
+ if (!arg)
+ ret = fpga_port_reset(pdev);
+ else
+ ret = -EINVAL;
+ break;
+ default:
+ dev_dbg(&pdev->dev, "%x cmd not handled", cmd);
+ ret = -ENODEV;
+ }
+
+ return ret;
+}
+
+struct feature_ops port_hdr_ops = {
+ .init = port_hdr_init,
+ .uinit = port_hdr_uinit,
+ .ioctl = port_hdr_ioctl,
+ .test = port_hdr_test,
+};
+
+/* sysfs attributes for port_uafu feature */
+static ssize_t
+afu_id_show(struct device *dev, struct device_attribute *attr, char *buf)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(dev);
+ struct feature_port_header *port_hdr =
+ get_feature_ioaddr_by_index(dev, PORT_FEATURE_ID_UAFU);
+ u64 guidl;
+ u64 guidh;
+
+ mutex_lock(&pdata->lock);
+ if (pdata->disable_count) {
+ mutex_unlock(&pdata->lock);
+ return -EBUSY;
+ }
+
+ guidl = readq(&port_hdr->afu_header.guid.b[0]);
+ guidh = readq(&port_hdr->afu_header.guid.b[8]);
+ mutex_unlock(&pdata->lock);
+
+ return scnprintf(buf, PAGE_SIZE, "%016llx%016llx\n", guidh, guidl);
+}
+static DEVICE_ATTR_RO(afu_id);
+
+static const struct attribute *port_uafu_attrs[] = {
+ &dev_attr_afu_id.attr,
+ NULL
+};
+
+static int port_afu_init(struct platform_device *pdev, struct feature *feature)
+{
+ struct resource *res = &pdev->resource[feature->resource_index];
+ u32 flags = FPGA_REGION_READ | FPGA_REGION_WRITE | FPGA_REGION_MMAP;
+ int ret;
+
+ dev_dbg(&pdev->dev, "PORT AFU Init.\n");
+
+ ret = afu_region_add(dev_get_platdata(&pdev->dev),
+ FPGA_PORT_INDEX_UAFU, resource_size(res),
+ res->start, flags);
+ if (ret)
+ return ret;
+
+ return sysfs_create_files(&pdev->dev.kobj, port_uafu_attrs);
+}
+
+static void port_afu_uinit(struct platform_device *pdev,
+ struct feature *feature)
+{
+ dev_dbg(&pdev->dev, "PORT AFU UInit.\n");
+
+ sysfs_remove_files(&pdev->dev.kobj, port_uafu_attrs);
+}
+
+static long port_afu_set_irq(struct platform_device *pdev,
+ struct feature *feature, unsigned long arg)
+{
+ struct feature_platform_data *pdata = dev_get_platdata(&pdev->dev);
+ struct fpga_port_uafu_irq_set hdr;
+ struct fpga_afu *afu;
+ unsigned long minsz;
+ int32_t *fds = NULL;
+ long ret = 0;
+
+ minsz = offsetofend(struct fpga_port_uafu_irq_set, count);
+
+ if (copy_from_user(&hdr, (void __user *)arg, minsz))
+ return -EFAULT;
+
+ if (hdr.argsz < minsz || hdr.flags)
+ return -EINVAL;
+
+ if ((hdr.start + hdr.count > feature->ctx_num) ||
+ (hdr.start + hdr.count < hdr.start) || !hdr.count)
+ return -EINVAL;
+
+ fds = memdup_user((void __user *)(arg + minsz),
+ hdr.count * sizeof(int32_t));
+ if (IS_ERR(fds))
+ return PTR_ERR(fds);