This source file includes following definitions.
- __resource_add
- __resource_del
- __release_cpp_area
- nfp_cpp_area_put
- nfp_cpp_area_get
- nfp_cpp_free
- nfp_cpp_model
- nfp_cpp_interface
- nfp_cpp_serial
- nfp_cpp_set_mu_locality_lsb
- nfp_cpp_mu_locality_lsb
- nfp_cpp_area_alloc_with_name
- nfp_cpp_area_alloc
- nfp_cpp_area_alloc_acquire
- nfp_cpp_area_free
- nfp_cpp_area_acquire_try
- __nfp_cpp_area_acquire
- nfp_cpp_area_acquire
- nfp_cpp_area_acquire_nonblocking
- nfp_cpp_area_release
- nfp_cpp_area_release_free
- nfp_cpp_area_read
- nfp_cpp_area_write
- nfp_cpp_area_size
- nfp_cpp_area_name
- nfp_cpp_area_priv
- nfp_cpp_area_cpp
- nfp_cpp_area_resource
- nfp_cpp_area_phys
- nfp_cpp_area_iomem
- nfp_cpp_area_readl
- nfp_cpp_area_writel
- nfp_cpp_area_readq
- nfp_cpp_area_writeq
- nfp_cpp_area_fill
- nfp_cpp_area_cache_add
- area_cache_get
- area_cache_put
- __nfp_cpp_read
- nfp_cpp_read
- __nfp_cpp_write
- nfp_cpp_write
- nfp_xpb_to_cpp
- nfp_xpb_readl
- nfp_xpb_writel
- nfp_xpb_writelm
- nfp_cpp_dev_release
- nfp_cpp_from_operations
- nfp_cpp_priv
- nfp_cpp_device
- nfp_cpp_explicit_acquire
- nfp_cpp_explicit_set_target
- nfp_cpp_explicit_set_data
- nfp_cpp_explicit_set_signal
- nfp_cpp_explicit_set_posted
- nfp_cpp_explicit_put
- nfp_cpp_explicit_do
- nfp_cpp_explicit_get
- nfp_cpp_explicit_release
- nfp_cpp_explicit_cpp
- nfp_cpp_explicit_priv
1
2
3
4
5
6
7
8
9
10
11
12 #include <asm/unaligned.h>
13 #include <linux/delay.h>
14 #include <linux/device.h>
15 #include <linux/ioport.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/mutex.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/wait.h>
22
23 #include "nfp_arm.h"
24 #include "nfp_cpp.h"
25 #include "nfp6000/nfp6000.h"
26
27 #define NFP_ARM_GCSR_SOFTMODEL2 0x0000014c
28 #define NFP_ARM_GCSR_SOFTMODEL3 0x00000150
29
30 struct nfp_cpp_resource {
31 struct list_head list;
32 const char *name;
33 u32 cpp_id;
34 u64 start;
35 u64 end;
36 };
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 struct nfp_cpp {
60 struct device dev;
61
62 void *priv;
63
64 u32 model;
65 u16 interface;
66 u8 serial[NFP_SERIAL_LEN];
67
68 const struct nfp_cpp_operations *op;
69 struct list_head resource_list;
70 rwlock_t resource_lock;
71 wait_queue_head_t waitq;
72
73 u32 imb_cat_table[16];
74 unsigned int mu_locality_lsb;
75
76 struct mutex area_cache_mutex;
77 struct list_head area_cache_list;
78 };
79
80
81 struct nfp_cpp_area_cache {
82 struct list_head entry;
83 u32 id;
84 u64 addr;
85 u32 size;
86 struct nfp_cpp_area *area;
87 };
88
89 struct nfp_cpp_area {
90 struct nfp_cpp *cpp;
91 struct kref kref;
92 atomic_t refcount;
93 struct mutex mutex;
94 unsigned long long offset;
95 unsigned long size;
96 struct nfp_cpp_resource resource;
97 void __iomem *iomem;
98
99 };
100
101 struct nfp_cpp_explicit {
102 struct nfp_cpp *cpp;
103 struct nfp_cpp_explicit_command cmd;
104
105 };
106
107 static void __resource_add(struct list_head *head, struct nfp_cpp_resource *res)
108 {
109 struct nfp_cpp_resource *tmp;
110 struct list_head *pos;
111
112 list_for_each(pos, head) {
113 tmp = container_of(pos, struct nfp_cpp_resource, list);
114
115 if (tmp->cpp_id > res->cpp_id)
116 break;
117
118 if (tmp->cpp_id == res->cpp_id && tmp->start > res->start)
119 break;
120 }
121
122 list_add_tail(&res->list, pos);
123 }
124
125 static void __resource_del(struct nfp_cpp_resource *res)
126 {
127 list_del_init(&res->list);
128 }
129
130 static void __release_cpp_area(struct kref *kref)
131 {
132 struct nfp_cpp_area *area =
133 container_of(kref, struct nfp_cpp_area, kref);
134 struct nfp_cpp *cpp = nfp_cpp_area_cpp(area);
135
136 if (area->cpp->op->area_cleanup)
137 area->cpp->op->area_cleanup(area);
138
139 write_lock(&cpp->resource_lock);
140 __resource_del(&area->resource);
141 write_unlock(&cpp->resource_lock);
142 kfree(area);
143 }
144
145 static void nfp_cpp_area_put(struct nfp_cpp_area *area)
146 {
147 kref_put(&area->kref, __release_cpp_area);
148 }
149
150 static struct nfp_cpp_area *nfp_cpp_area_get(struct nfp_cpp_area *area)
151 {
152 kref_get(&area->kref);
153
154 return area;
155 }
156
157
158
159
160
161 void nfp_cpp_free(struct nfp_cpp *cpp)
162 {
163 struct nfp_cpp_area_cache *cache, *ctmp;
164 struct nfp_cpp_resource *res, *rtmp;
165
166
167 list_for_each_entry_safe(cache, ctmp, &cpp->area_cache_list, entry) {
168 list_del(&cache->entry);
169 if (cache->id)
170 nfp_cpp_area_release(cache->area);
171 nfp_cpp_area_free(cache->area);
172 kfree(cache);
173 }
174
175
176 WARN_ON(!list_empty(&cpp->resource_list));
177
178
179 list_for_each_entry_safe(res, rtmp, &cpp->resource_list, list) {
180 struct nfp_cpp_area *area = container_of(res,
181 struct nfp_cpp_area,
182 resource);
183
184 dev_err(cpp->dev.parent, "Dangling area: %d:%d:%d:0x%0llx-0x%0llx%s%s\n",
185 NFP_CPP_ID_TARGET_of(res->cpp_id),
186 NFP_CPP_ID_ACTION_of(res->cpp_id),
187 NFP_CPP_ID_TOKEN_of(res->cpp_id),
188 res->start, res->end,
189 res->name ? " " : "",
190 res->name ? res->name : "");
191
192 if (area->cpp->op->area_release)
193 area->cpp->op->area_release(area);
194
195 __release_cpp_area(&area->kref);
196 }
197
198 if (cpp->op->free)
199 cpp->op->free(cpp);
200
201 device_unregister(&cpp->dev);
202
203 kfree(cpp);
204 }
205
206
207
208
209
210
211
212 u32 nfp_cpp_model(struct nfp_cpp *cpp)
213 {
214 return cpp->model;
215 }
216
217
218
219
220
221
222
223 u16 nfp_cpp_interface(struct nfp_cpp *cpp)
224 {
225 return cpp->interface;
226 }
227
228
229
230
231
232
233
234
235 int nfp_cpp_serial(struct nfp_cpp *cpp, const u8 **serial)
236 {
237 *serial = &cpp->serial[0];
238 return sizeof(cpp->serial);
239 }
240
241 #define NFP_IMB_TGTADDRESSMODECFG_MODE_of(_x) (((_x) >> 13) & 0x7)
242 #define NFP_IMB_TGTADDRESSMODECFG_ADDRMODE BIT(12)
243 #define NFP_IMB_TGTADDRESSMODECFG_ADDRMODE_32_BIT 0
244 #define NFP_IMB_TGTADDRESSMODECFG_ADDRMODE_40_BIT BIT(12)
245
246 static int nfp_cpp_set_mu_locality_lsb(struct nfp_cpp *cpp)
247 {
248 unsigned int mode, addr40;
249 u32 imbcppat;
250 int res;
251
252 imbcppat = cpp->imb_cat_table[NFP_CPP_TARGET_MU];
253 mode = NFP_IMB_TGTADDRESSMODECFG_MODE_of(imbcppat);
254 addr40 = !!(imbcppat & NFP_IMB_TGTADDRESSMODECFG_ADDRMODE);
255
256 res = nfp_cppat_mu_locality_lsb(mode, addr40);
257 if (res < 0)
258 return res;
259 cpp->mu_locality_lsb = res;
260
261 return 0;
262 }
263
264 unsigned int nfp_cpp_mu_locality_lsb(struct nfp_cpp *cpp)
265 {
266 return cpp->mu_locality_lsb;
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284 struct nfp_cpp_area *
285 nfp_cpp_area_alloc_with_name(struct nfp_cpp *cpp, u32 dest, const char *name,
286 unsigned long long address, unsigned long size)
287 {
288 struct nfp_cpp_area *area;
289 u64 tmp64 = address;
290 int err, name_len;
291
292
293 err = nfp_target_cpp(dest, tmp64, &dest, &tmp64, cpp->imb_cat_table);
294 if (err < 0)
295 return NULL;
296
297 address = tmp64;
298
299 if (!name)
300 name = "(reserved)";
301
302 name_len = strlen(name) + 1;
303 area = kzalloc(sizeof(*area) + cpp->op->area_priv_size + name_len,
304 GFP_KERNEL);
305 if (!area)
306 return NULL;
307
308 area->cpp = cpp;
309 area->resource.name = (void *)area + sizeof(*area) +
310 cpp->op->area_priv_size;
311 memcpy((char *)area->resource.name, name, name_len);
312
313 area->resource.cpp_id = dest;
314 area->resource.start = address;
315 area->resource.end = area->resource.start + size - 1;
316 INIT_LIST_HEAD(&area->resource.list);
317
318 atomic_set(&area->refcount, 0);
319 kref_init(&area->kref);
320 mutex_init(&area->mutex);
321
322 if (cpp->op->area_init) {
323 int err;
324
325 err = cpp->op->area_init(area, dest, address, size);
326 if (err < 0) {
327 kfree(area);
328 return NULL;
329 }
330 }
331
332 write_lock(&cpp->resource_lock);
333 __resource_add(&cpp->resource_list, &area->resource);
334 write_unlock(&cpp->resource_lock);
335
336 area->offset = address;
337 area->size = size;
338
339 return area;
340 }
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356 struct nfp_cpp_area *
357 nfp_cpp_area_alloc(struct nfp_cpp *cpp, u32 dest,
358 unsigned long long address, unsigned long size)
359 {
360 return nfp_cpp_area_alloc_with_name(cpp, dest, NULL, address, size);
361 }
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379 struct nfp_cpp_area *
380 nfp_cpp_area_alloc_acquire(struct nfp_cpp *cpp, const char *name, u32 dest,
381 unsigned long long address, unsigned long size)
382 {
383 struct nfp_cpp_area *area;
384
385 area = nfp_cpp_area_alloc_with_name(cpp, dest, name, address, size);
386 if (!area)
387 return NULL;
388
389 if (nfp_cpp_area_acquire(area)) {
390 nfp_cpp_area_free(area);
391 return NULL;
392 }
393
394 return area;
395 }
396
397
398
399
400
401
402
403 void nfp_cpp_area_free(struct nfp_cpp_area *area)
404 {
405 if (atomic_read(&area->refcount))
406 nfp_warn(area->cpp, "Warning: freeing busy area\n");
407 nfp_cpp_area_put(area);
408 }
409
410 static bool nfp_cpp_area_acquire_try(struct nfp_cpp_area *area, int *status)
411 {
412 *status = area->cpp->op->area_acquire(area);
413
414 return *status != -EAGAIN;
415 }
416
417 static int __nfp_cpp_area_acquire(struct nfp_cpp_area *area)
418 {
419 int err, status;
420
421 if (atomic_inc_return(&area->refcount) > 1)
422 return 0;
423
424 if (!area->cpp->op->area_acquire)
425 return 0;
426
427 err = wait_event_interruptible(area->cpp->waitq,
428 nfp_cpp_area_acquire_try(area, &status));
429 if (!err)
430 err = status;
431 if (err) {
432 nfp_warn(area->cpp, "Warning: area wait failed: %d\n", err);
433 atomic_dec(&area->refcount);
434 return err;
435 }
436
437 nfp_cpp_area_get(area);
438
439 return 0;
440 }
441
442
443
444
445
446
447
448
449
450
451 int nfp_cpp_area_acquire(struct nfp_cpp_area *area)
452 {
453 int ret;
454
455 mutex_lock(&area->mutex);
456 ret = __nfp_cpp_area_acquire(area);
457 mutex_unlock(&area->mutex);
458
459 return ret;
460 }
461
462
463
464
465
466
467
468
469
470
471
472
473 int nfp_cpp_area_acquire_nonblocking(struct nfp_cpp_area *area)
474 {
475 mutex_lock(&area->mutex);
476 if (atomic_inc_return(&area->refcount) == 1) {
477 if (area->cpp->op->area_acquire) {
478 int err;
479
480 err = area->cpp->op->area_acquire(area);
481 if (err < 0) {
482 atomic_dec(&area->refcount);
483 mutex_unlock(&area->mutex);
484 return err;
485 }
486 }
487 }
488 mutex_unlock(&area->mutex);
489
490 nfp_cpp_area_get(area);
491 return 0;
492 }
493
494
495
496
497
498
499
500 void nfp_cpp_area_release(struct nfp_cpp_area *area)
501 {
502 mutex_lock(&area->mutex);
503
504 if (atomic_dec_and_test(&area->refcount)) {
505 if (area->cpp->op->area_release) {
506 area->cpp->op->area_release(area);
507
508 wake_up_interruptible_all(&area->cpp->waitq);
509 }
510 }
511 mutex_unlock(&area->mutex);
512
513 nfp_cpp_area_put(area);
514 }
515
516
517
518
519
520
521
522 void nfp_cpp_area_release_free(struct nfp_cpp_area *area)
523 {
524 nfp_cpp_area_release(area);
525 nfp_cpp_area_free(area);
526 }
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542 int nfp_cpp_area_read(struct nfp_cpp_area *area,
543 unsigned long offset, void *kernel_vaddr,
544 size_t length)
545 {
546 return area->cpp->op->area_read(area, kernel_vaddr, offset, length);
547 }
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563 int nfp_cpp_area_write(struct nfp_cpp_area *area,
564 unsigned long offset, const void *kernel_vaddr,
565 size_t length)
566 {
567 return area->cpp->op->area_write(area, kernel_vaddr, offset, length);
568 }
569
570
571
572
573
574
575
576 size_t nfp_cpp_area_size(struct nfp_cpp_area *cpp_area)
577 {
578 return cpp_area->size;
579 }
580
581
582
583
584
585
586
587 const char *nfp_cpp_area_name(struct nfp_cpp_area *cpp_area)
588 {
589 return cpp_area->resource.name;
590 }
591
592
593
594
595
596
597
598 void *nfp_cpp_area_priv(struct nfp_cpp_area *cpp_area)
599 {
600 return &cpp_area[1];
601 }
602
603
604
605
606
607
608
609 struct nfp_cpp *nfp_cpp_area_cpp(struct nfp_cpp_area *cpp_area)
610 {
611 return cpp_area->cpp;
612 }
613
614
615
616
617
618
619
620
621
622 struct resource *nfp_cpp_area_resource(struct nfp_cpp_area *area)
623 {
624 struct resource *res = NULL;
625
626 if (area->cpp->op->area_resource)
627 res = area->cpp->op->area_resource(area);
628
629 return res;
630 }
631
632
633
634
635
636
637
638
639
640 phys_addr_t nfp_cpp_area_phys(struct nfp_cpp_area *area)
641 {
642 phys_addr_t addr = ~0;
643
644 if (area->cpp->op->area_phys)
645 addr = area->cpp->op->area_phys(area);
646
647 return addr;
648 }
649
650
651
652
653
654
655
656
657
658
659
660
661 void __iomem *nfp_cpp_area_iomem(struct nfp_cpp_area *area)
662 {
663 void __iomem *iomem = NULL;
664
665 if (area->cpp->op->area_iomem)
666 iomem = area->cpp->op->area_iomem(area);
667
668 return iomem;
669 }
670
671
672
673
674
675
676
677
678
679 int nfp_cpp_area_readl(struct nfp_cpp_area *area,
680 unsigned long offset, u32 *value)
681 {
682 u8 tmp[4];
683 int n;
684
685 n = nfp_cpp_area_read(area, offset, &tmp, sizeof(tmp));
686 if (n != sizeof(tmp))
687 return n < 0 ? n : -EIO;
688
689 *value = get_unaligned_le32(tmp);
690 return 0;
691 }
692
693
694
695
696
697
698
699
700
701 int nfp_cpp_area_writel(struct nfp_cpp_area *area,
702 unsigned long offset, u32 value)
703 {
704 u8 tmp[4];
705 int n;
706
707 put_unaligned_le32(value, tmp);
708 n = nfp_cpp_area_write(area, offset, &tmp, sizeof(tmp));
709
710 return n == sizeof(tmp) ? 0 : n < 0 ? n : -EIO;
711 }
712
713
714
715
716
717
718
719
720
721 int nfp_cpp_area_readq(struct nfp_cpp_area *area,
722 unsigned long offset, u64 *value)
723 {
724 u8 tmp[8];
725 int n;
726
727 n = nfp_cpp_area_read(area, offset, &tmp, sizeof(tmp));
728 if (n != sizeof(tmp))
729 return n < 0 ? n : -EIO;
730
731 *value = get_unaligned_le64(tmp);
732 return 0;
733 }
734
735
736
737
738
739
740
741
742
743 int nfp_cpp_area_writeq(struct nfp_cpp_area *area,
744 unsigned long offset, u64 value)
745 {
746 u8 tmp[8];
747 int n;
748
749 put_unaligned_le64(value, tmp);
750 n = nfp_cpp_area_write(area, offset, &tmp, sizeof(tmp));
751
752 return n == sizeof(tmp) ? 0 : n < 0 ? n : -EIO;
753 }
754
755
756
757
758
759
760
761
762
763
764
765
766 int nfp_cpp_area_fill(struct nfp_cpp_area *area,
767 unsigned long offset, u32 value, size_t length)
768 {
769 u8 tmp[4];
770 size_t i;
771 int k;
772
773 put_unaligned_le32(value, tmp);
774
775 if (offset % sizeof(tmp) || length % sizeof(tmp))
776 return -EINVAL;
777
778 for (i = 0; i < length; i += sizeof(tmp)) {
779 k = nfp_cpp_area_write(area, offset + i, &tmp, sizeof(tmp));
780 if (k < 0)
781 return k;
782 }
783
784 return i;
785 }
786
787
788
789
790
791
792 int nfp_cpp_area_cache_add(struct nfp_cpp *cpp, size_t size)
793 {
794 struct nfp_cpp_area_cache *cache;
795 struct nfp_cpp_area *area;
796
797
798
799
800 area = nfp_cpp_area_alloc(cpp, NFP_CPP_ID(7, NFP_CPP_ACTION_RW, 0),
801 0, size);
802 if (!area)
803 return -ENOMEM;
804
805 cache = kzalloc(sizeof(*cache), GFP_KERNEL);
806 if (!cache)
807 return -ENOMEM;
808
809 cache->id = 0;
810 cache->addr = 0;
811 cache->size = size;
812 cache->area = area;
813 mutex_lock(&cpp->area_cache_mutex);
814 list_add_tail(&cache->entry, &cpp->area_cache_list);
815 mutex_unlock(&cpp->area_cache_mutex);
816
817 return 0;
818 }
819
820 static struct nfp_cpp_area_cache *
821 area_cache_get(struct nfp_cpp *cpp, u32 id,
822 u64 addr, unsigned long *offset, size_t length)
823 {
824 struct nfp_cpp_area_cache *cache;
825 int err;
826
827
828
829
830
831 if (length == 0 || id == 0)
832 return NULL;
833
834
835 err = nfp_target_cpp(id, addr, &id, &addr, cpp->imb_cat_table);
836 if (err < 0)
837 return NULL;
838
839 mutex_lock(&cpp->area_cache_mutex);
840
841 if (list_empty(&cpp->area_cache_list)) {
842 mutex_unlock(&cpp->area_cache_mutex);
843 return NULL;
844 }
845
846 addr += *offset;
847
848
849 list_for_each_entry(cache, &cpp->area_cache_list, entry) {
850 if (id == cache->id &&
851 addr >= cache->addr &&
852 addr + length <= cache->addr + cache->size)
853 goto exit;
854 }
855
856
857 cache = list_entry(cpp->area_cache_list.prev,
858 struct nfp_cpp_area_cache, entry);
859
860
861 if (round_down(addr + length - 1, cache->size) !=
862 round_down(addr, cache->size)) {
863 mutex_unlock(&cpp->area_cache_mutex);
864 return NULL;
865 }
866
867
868 if (cache->id) {
869 nfp_cpp_area_release(cache->area);
870 cache->id = 0;
871 cache->addr = 0;
872 }
873
874
875 cache->id = id;
876 cache->addr = addr & ~(u64)(cache->size - 1);
877
878
879 if (cpp->op->area_init) {
880 err = cpp->op->area_init(cache->area,
881 id, cache->addr, cache->size);
882 if (err < 0) {
883 mutex_unlock(&cpp->area_cache_mutex);
884 return NULL;
885 }
886 }
887
888
889 err = nfp_cpp_area_acquire(cache->area);
890 if (err < 0) {
891 mutex_unlock(&cpp->area_cache_mutex);
892 return NULL;
893 }
894
895 exit:
896
897 *offset = addr - cache->addr;
898 return cache;
899 }
900
901 static void
902 area_cache_put(struct nfp_cpp *cpp, struct nfp_cpp_area_cache *cache)
903 {
904 if (!cache)
905 return;
906
907
908 list_del(&cache->entry);
909 list_add(&cache->entry, &cpp->area_cache_list);
910
911 mutex_unlock(&cpp->area_cache_mutex);
912 }
913
914 static int __nfp_cpp_read(struct nfp_cpp *cpp, u32 destination,
915 unsigned long long address, void *kernel_vaddr,
916 size_t length)
917 {
918 struct nfp_cpp_area_cache *cache;
919 struct nfp_cpp_area *area;
920 unsigned long offset = 0;
921 int err;
922
923 cache = area_cache_get(cpp, destination, address, &offset, length);
924 if (cache) {
925 area = cache->area;
926 } else {
927 area = nfp_cpp_area_alloc(cpp, destination, address, length);
928 if (!area)
929 return -ENOMEM;
930
931 err = nfp_cpp_area_acquire(area);
932 if (err) {
933 nfp_cpp_area_free(area);
934 return err;
935 }
936 }
937
938 err = nfp_cpp_area_read(area, offset, kernel_vaddr, length);
939
940 if (cache)
941 area_cache_put(cpp, cache);
942 else
943 nfp_cpp_area_release_free(area);
944
945 return err;
946 }
947
948
949
950
951
952
953
954
955
956
957
958 int nfp_cpp_read(struct nfp_cpp *cpp, u32 destination,
959 unsigned long long address, void *kernel_vaddr,
960 size_t length)
961 {
962 size_t n, offset;
963 int ret;
964
965 for (offset = 0; offset < length; offset += n) {
966 unsigned long long r_addr = address + offset;
967
968
969 n = min_t(size_t, length - offset,
970 ALIGN(r_addr + 1, NFP_CPP_SAFE_AREA_SIZE) - r_addr);
971
972 ret = __nfp_cpp_read(cpp, destination, address + offset,
973 kernel_vaddr + offset, n);
974 if (ret < 0)
975 return ret;
976 if (ret != n)
977 return offset + n;
978 }
979
980 return length;
981 }
982
983 static int __nfp_cpp_write(struct nfp_cpp *cpp, u32 destination,
984 unsigned long long address,
985 const void *kernel_vaddr, size_t length)
986 {
987 struct nfp_cpp_area_cache *cache;
988 struct nfp_cpp_area *area;
989 unsigned long offset = 0;
990 int err;
991
992 cache = area_cache_get(cpp, destination, address, &offset, length);
993 if (cache) {
994 area = cache->area;
995 } else {
996 area = nfp_cpp_area_alloc(cpp, destination, address, length);
997 if (!area)
998 return -ENOMEM;
999
1000 err = nfp_cpp_area_acquire(area);
1001 if (err) {
1002 nfp_cpp_area_free(area);
1003 return err;
1004 }
1005 }
1006
1007 err = nfp_cpp_area_write(area, offset, kernel_vaddr, length);
1008
1009 if (cache)
1010 area_cache_put(cpp, cache);
1011 else
1012 nfp_cpp_area_release_free(area);
1013
1014 return err;
1015 }
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027 int nfp_cpp_write(struct nfp_cpp *cpp, u32 destination,
1028 unsigned long long address,
1029 const void *kernel_vaddr, size_t length)
1030 {
1031 size_t n, offset;
1032 int ret;
1033
1034 for (offset = 0; offset < length; offset += n) {
1035 unsigned long long w_addr = address + offset;
1036
1037
1038 n = min_t(size_t, length - offset,
1039 ALIGN(w_addr + 1, NFP_CPP_SAFE_AREA_SIZE) - w_addr);
1040
1041 ret = __nfp_cpp_write(cpp, destination, address + offset,
1042 kernel_vaddr + offset, n);
1043 if (ret < 0)
1044 return ret;
1045 if (ret != n)
1046 return offset + n;
1047 }
1048
1049 return length;
1050 }
1051
1052
1053 static u32 nfp_xpb_to_cpp(struct nfp_cpp *cpp, u32 *xpb_addr)
1054 {
1055 int island;
1056 u32 xpb;
1057
1058 xpb = NFP_CPP_ID(14, NFP_CPP_ACTION_RW, 0);
1059
1060
1061
1062 island = (*xpb_addr >> 24) & 0x3f;
1063 if (!island)
1064 return xpb;
1065
1066 if (island != 1) {
1067 *xpb_addr |= 1 << 30;
1068 return xpb;
1069 }
1070
1071
1072 *xpb_addr &= ~0x7f000000;
1073 if (*xpb_addr < 0x60000) {
1074 *xpb_addr |= 1 << 30;
1075 } else {
1076
1077 if (NFP_CPP_INTERFACE_TYPE_of(nfp_cpp_interface(cpp))
1078 != NFP_CPP_INTERFACE_TYPE_ARM)
1079 *xpb_addr |= 1 << 24;
1080 }
1081
1082 return xpb;
1083 }
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093 int nfp_xpb_readl(struct nfp_cpp *cpp, u32 xpb_addr, u32 *value)
1094 {
1095 u32 cpp_dest = nfp_xpb_to_cpp(cpp, &xpb_addr);
1096
1097 return nfp_cpp_readl(cpp, cpp_dest, xpb_addr, value);
1098 }
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108 int nfp_xpb_writel(struct nfp_cpp *cpp, u32 xpb_addr, u32 value)
1109 {
1110 u32 cpp_dest = nfp_xpb_to_cpp(cpp, &xpb_addr);
1111
1112 return nfp_cpp_writel(cpp, cpp_dest, xpb_addr, value);
1113 }
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 int nfp_xpb_writelm(struct nfp_cpp *cpp, u32 xpb_tgt,
1127 u32 mask, u32 value)
1128 {
1129 int err;
1130 u32 tmp;
1131
1132 err = nfp_xpb_readl(cpp, xpb_tgt, &tmp);
1133 if (err < 0)
1134 return err;
1135
1136 tmp &= ~mask;
1137 tmp |= mask & value;
1138 return nfp_xpb_writel(cpp, xpb_tgt, tmp);
1139 }
1140
1141
1142 static struct lock_class_key nfp_cpp_resource_lock_key;
1143
1144 static void nfp_cpp_dev_release(struct device *dev)
1145 {
1146
1147 }
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160 struct nfp_cpp *
1161 nfp_cpp_from_operations(const struct nfp_cpp_operations *ops,
1162 struct device *parent, void *priv)
1163 {
1164 const u32 arm = NFP_CPP_ID(NFP_CPP_TARGET_ARM, NFP_CPP_ACTION_RW, 0);
1165 struct nfp_cpp *cpp;
1166 int ifc, err;
1167 u32 mask[2];
1168 u32 xpbaddr;
1169 size_t tgt;
1170
1171 cpp = kzalloc(sizeof(*cpp), GFP_KERNEL);
1172 if (!cpp) {
1173 err = -ENOMEM;
1174 goto err_malloc;
1175 }
1176
1177 cpp->op = ops;
1178 cpp->priv = priv;
1179
1180 ifc = ops->get_interface(parent);
1181 if (ifc < 0) {
1182 err = ifc;
1183 goto err_free_cpp;
1184 }
1185 cpp->interface = ifc;
1186 if (ops->read_serial) {
1187 err = ops->read_serial(parent, cpp->serial);
1188 if (err)
1189 goto err_free_cpp;
1190 }
1191
1192 rwlock_init(&cpp->resource_lock);
1193 init_waitqueue_head(&cpp->waitq);
1194 lockdep_set_class(&cpp->resource_lock, &nfp_cpp_resource_lock_key);
1195 INIT_LIST_HEAD(&cpp->resource_list);
1196 INIT_LIST_HEAD(&cpp->area_cache_list);
1197 mutex_init(&cpp->area_cache_mutex);
1198 cpp->dev.init_name = "cpp";
1199 cpp->dev.parent = parent;
1200 cpp->dev.release = nfp_cpp_dev_release;
1201 err = device_register(&cpp->dev);
1202 if (err < 0) {
1203 put_device(&cpp->dev);
1204 goto err_free_cpp;
1205 }
1206
1207 dev_set_drvdata(&cpp->dev, cpp);
1208
1209
1210
1211
1212 if (cpp->op->init) {
1213 err = cpp->op->init(cpp);
1214 if (err < 0) {
1215 dev_err(parent,
1216 "NFP interface initialization failed\n");
1217 goto err_out;
1218 }
1219 }
1220
1221 err = nfp_cpp_model_autodetect(cpp, &cpp->model);
1222 if (err < 0) {
1223 dev_err(parent, "NFP model detection failed\n");
1224 goto err_out;
1225 }
1226
1227 for (tgt = 0; tgt < ARRAY_SIZE(cpp->imb_cat_table); tgt++) {
1228
1229 xpbaddr = 0x000a0000 + (tgt * 4);
1230 err = nfp_xpb_readl(cpp, xpbaddr,
1231 &cpp->imb_cat_table[tgt]);
1232 if (err < 0) {
1233 dev_err(parent,
1234 "Can't read CPP mapping from device\n");
1235 goto err_out;
1236 }
1237 }
1238
1239 nfp_cpp_readl(cpp, arm, NFP_ARM_GCSR + NFP_ARM_GCSR_SOFTMODEL2,
1240 &mask[0]);
1241 nfp_cpp_readl(cpp, arm, NFP_ARM_GCSR + NFP_ARM_GCSR_SOFTMODEL3,
1242 &mask[1]);
1243
1244 err = nfp_cpp_set_mu_locality_lsb(cpp);
1245 if (err < 0) {
1246 dev_err(parent, "Can't calculate MU locality bit offset\n");
1247 goto err_out;
1248 }
1249
1250 dev_info(cpp->dev.parent, "Model: 0x%08x, SN: %pM, Ifc: 0x%04x\n",
1251 nfp_cpp_model(cpp), cpp->serial, nfp_cpp_interface(cpp));
1252
1253 return cpp;
1254
1255 err_out:
1256 device_unregister(&cpp->dev);
1257 err_free_cpp:
1258 kfree(cpp);
1259 err_malloc:
1260 return ERR_PTR(err);
1261 }
1262
1263
1264
1265
1266
1267
1268
1269 void *nfp_cpp_priv(struct nfp_cpp *cpp)
1270 {
1271 return cpp->priv;
1272 }
1273
1274
1275
1276
1277
1278
1279
1280 struct device *nfp_cpp_device(struct nfp_cpp *cpp)
1281 {
1282 return &cpp->dev;
1283 }
1284
1285 #define NFP_EXPL_OP(func, expl, args...) \
1286 ({ \
1287 struct nfp_cpp *cpp = nfp_cpp_explicit_cpp(expl); \
1288 int err = -ENODEV; \
1289 \
1290 if (cpp->op->func) \
1291 err = cpp->op->func(expl, ##args); \
1292 err; \
1293 })
1294
1295 #define NFP_EXPL_OP_NR(func, expl, args...) \
1296 ({ \
1297 struct nfp_cpp *cpp = nfp_cpp_explicit_cpp(expl); \
1298 \
1299 if (cpp->op->func) \
1300 cpp->op->func(expl, ##args); \
1301 \
1302 })
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313 struct nfp_cpp_explicit *nfp_cpp_explicit_acquire(struct nfp_cpp *cpp)
1314 {
1315 struct nfp_cpp_explicit *expl;
1316 int err;
1317
1318 expl = kzalloc(sizeof(*expl) + cpp->op->explicit_priv_size, GFP_KERNEL);
1319 if (!expl)
1320 return NULL;
1321
1322 expl->cpp = cpp;
1323 err = NFP_EXPL_OP(explicit_acquire, expl);
1324 if (err < 0) {
1325 kfree(expl);
1326 return NULL;
1327 }
1328
1329 return expl;
1330 }
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341 int nfp_cpp_explicit_set_target(struct nfp_cpp_explicit *expl,
1342 u32 cpp_id, u8 len, u8 mask)
1343 {
1344 expl->cmd.cpp_id = cpp_id;
1345 expl->cmd.len = len;
1346 expl->cmd.byte_mask = mask;
1347
1348 return 0;
1349 }
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359 int nfp_cpp_explicit_set_data(struct nfp_cpp_explicit *expl,
1360 u8 data_master, u16 data_ref)
1361 {
1362 expl->cmd.data_master = data_master;
1363 expl->cmd.data_ref = data_ref;
1364
1365 return 0;
1366 }
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376 int nfp_cpp_explicit_set_signal(struct nfp_cpp_explicit *expl,
1377 u8 signal_master, u8 signal_ref)
1378 {
1379 expl->cmd.signal_master = signal_master;
1380 expl->cmd.signal_ref = signal_ref;
1381
1382 return 0;
1383 }
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396 int nfp_cpp_explicit_set_posted(struct nfp_cpp_explicit *expl, int posted,
1397 u8 siga,
1398 enum nfp_cpp_explicit_signal_mode siga_mode,
1399 u8 sigb,
1400 enum nfp_cpp_explicit_signal_mode sigb_mode)
1401 {
1402 expl->cmd.posted = posted;
1403 expl->cmd.siga = siga;
1404 expl->cmd.sigb = sigb;
1405 expl->cmd.siga_mode = siga_mode;
1406 expl->cmd.sigb_mode = sigb_mode;
1407
1408 return 0;
1409 }
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424 int nfp_cpp_explicit_put(struct nfp_cpp_explicit *expl,
1425 const void *buff, size_t len)
1426 {
1427 return NFP_EXPL_OP(explicit_put, expl, buff, len);
1428 }
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440 int nfp_cpp_explicit_do(struct nfp_cpp_explicit *expl, u64 address)
1441 {
1442 return NFP_EXPL_OP(explicit_do, expl, &expl->cmd, address);
1443 }
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461 int nfp_cpp_explicit_get(struct nfp_cpp_explicit *expl, void *buff, size_t len)
1462 {
1463 return NFP_EXPL_OP(explicit_get, expl, buff, len);
1464 }
1465
1466
1467
1468
1469
1470
1471 void nfp_cpp_explicit_release(struct nfp_cpp_explicit *expl)
1472 {
1473 NFP_EXPL_OP_NR(explicit_release, expl);
1474 kfree(expl);
1475 }
1476
1477
1478
1479
1480
1481
1482
1483 struct nfp_cpp *nfp_cpp_explicit_cpp(struct nfp_cpp_explicit *cpp_explicit)
1484 {
1485 return cpp_explicit->cpp;
1486 }
1487
1488
1489
1490
1491
1492
1493
1494 void *nfp_cpp_explicit_priv(struct nfp_cpp_explicit *cpp_explicit)
1495 {
1496 return &cpp_explicit[1];
1497 }