1
2
3
4
5
6
7
8
9#include <linux/device.h>
10#include <linux/slab.h>
11#include <linux/module.h>
12#include <linux/of_device.h>
13
14#include <linux/pci-epc.h>
15#include <linux/pci-epf.h>
16#include <linux/pci-ep-cfs.h>
17
18static struct class *pci_epc_class;
19
20static void devm_pci_epc_release(struct device *dev, void *res)
21{
22 struct pci_epc *epc = *(struct pci_epc **)res;
23
24 pci_epc_destroy(epc);
25}
26
27static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
28{
29 struct pci_epc **epc = res;
30
31 return *epc == match_data;
32}
33
34
35
36
37
38
39
40void pci_epc_put(struct pci_epc *epc)
41{
42 if (!epc || IS_ERR(epc))
43 return;
44
45 module_put(epc->ops->owner);
46 put_device(&epc->dev);
47}
48EXPORT_SYMBOL_GPL(pci_epc_put);
49
50
51
52
53
54
55
56
57struct pci_epc *pci_epc_get(const char *epc_name)
58{
59 int ret = -EINVAL;
60 struct pci_epc *epc;
61 struct device *dev;
62 struct class_dev_iter iter;
63
64 class_dev_iter_init(&iter, pci_epc_class, NULL, NULL);
65 while ((dev = class_dev_iter_next(&iter))) {
66 if (strcmp(epc_name, dev_name(dev)))
67 continue;
68
69 epc = to_pci_epc(dev);
70 if (!try_module_get(epc->ops->owner)) {
71 ret = -EINVAL;
72 goto err;
73 }
74
75 class_dev_iter_exit(&iter);
76 get_device(&epc->dev);
77 return epc;
78 }
79
80err:
81 class_dev_iter_exit(&iter);
82 return ERR_PTR(ret);
83}
84EXPORT_SYMBOL_GPL(pci_epc_get);
85
86
87
88
89
90
91
92
93enum pci_barno
94pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
95{
96 return pci_epc_get_next_free_bar(epc_features, BAR_0);
97}
98EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
99
100
101
102
103
104
105
106
107
108enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
109 *epc_features, enum pci_barno bar)
110{
111 unsigned long free_bar;
112
113 if (!epc_features)
114 return BAR_0;
115
116
117 if ((epc_features->bar_fixed_64bit << 1) & 1 << bar)
118 bar++;
119
120
121 free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit;
122
123
124 free_bar <<= 1;
125 free_bar |= epc_features->reserved_bar;
126
127 free_bar = find_next_zero_bit(&free_bar, 6, bar);
128 if (free_bar > 5)
129 return NO_BAR;
130
131 return free_bar;
132}
133EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
134
135
136
137
138
139
140
141
142
143
144
145
146
147const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
148 u8 func_no, u8 vfunc_no)
149{
150 const struct pci_epc_features *epc_features;
151
152 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
153 return NULL;
154
155 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
156 return NULL;
157
158 if (!epc->ops->get_features)
159 return NULL;
160
161 mutex_lock(&epc->lock);
162 epc_features = epc->ops->get_features(epc, func_no, vfunc_no);
163 mutex_unlock(&epc->lock);
164
165 return epc_features;
166}
167EXPORT_SYMBOL_GPL(pci_epc_get_features);
168
169
170
171
172
173
174
175void pci_epc_stop(struct pci_epc *epc)
176{
177 if (IS_ERR(epc) || !epc->ops->stop)
178 return;
179
180 mutex_lock(&epc->lock);
181 epc->ops->stop(epc);
182 mutex_unlock(&epc->lock);
183}
184EXPORT_SYMBOL_GPL(pci_epc_stop);
185
186
187
188
189
190
191
192int pci_epc_start(struct pci_epc *epc)
193{
194 int ret;
195
196 if (IS_ERR(epc))
197 return -EINVAL;
198
199 if (!epc->ops->start)
200 return 0;
201
202 mutex_lock(&epc->lock);
203 ret = epc->ops->start(epc);
204 mutex_unlock(&epc->lock);
205
206 return ret;
207}
208EXPORT_SYMBOL_GPL(pci_epc_start);
209
210
211
212
213
214
215
216
217
218
219
220int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
221 enum pci_epc_irq_type type, u16 interrupt_num)
222{
223 int ret;
224
225 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
226 return -EINVAL;
227
228 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
229 return -EINVAL;
230
231 if (!epc->ops->raise_irq)
232 return 0;
233
234 mutex_lock(&epc->lock);
235 ret = epc->ops->raise_irq(epc, func_no, vfunc_no, type, interrupt_num);
236 mutex_unlock(&epc->lock);
237
238 return ret;
239}
240EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
264 phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size,
265 u32 *msi_data, u32 *msi_addr_offset)
266{
267 int ret;
268
269 if (IS_ERR_OR_NULL(epc))
270 return -EINVAL;
271
272 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
273 return -EINVAL;
274
275 if (!epc->ops->map_msi_irq)
276 return -EINVAL;
277
278 mutex_lock(&epc->lock);
279 ret = epc->ops->map_msi_irq(epc, func_no, vfunc_no, phys_addr,
280 interrupt_num, entry_size, msi_data,
281 msi_addr_offset);
282 mutex_unlock(&epc->lock);
283
284 return ret;
285}
286EXPORT_SYMBOL_GPL(pci_epc_map_msi_irq);
287
288
289
290
291
292
293
294
295
296int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
297{
298 int interrupt;
299
300 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
301 return 0;
302
303 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
304 return 0;
305
306 if (!epc->ops->get_msi)
307 return 0;
308
309 mutex_lock(&epc->lock);
310 interrupt = epc->ops->get_msi(epc, func_no, vfunc_no);
311 mutex_unlock(&epc->lock);
312
313 if (interrupt < 0)
314 return 0;
315
316 interrupt = 1 << interrupt;
317
318 return interrupt;
319}
320EXPORT_SYMBOL_GPL(pci_epc_get_msi);
321
322
323
324
325
326
327
328
329
330
331int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts)
332{
333 int ret;
334 u8 encode_int;
335
336 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
337 interrupts > 32)
338 return -EINVAL;
339
340 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
341 return -EINVAL;
342
343 if (!epc->ops->set_msi)
344 return 0;
345
346 encode_int = order_base_2(interrupts);
347
348 mutex_lock(&epc->lock);
349 ret = epc->ops->set_msi(epc, func_no, vfunc_no, encode_int);
350 mutex_unlock(&epc->lock);
351
352 return ret;
353}
354EXPORT_SYMBOL_GPL(pci_epc_set_msi);
355
356
357
358
359
360
361
362
363
364int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
365{
366 int interrupt;
367
368 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
369 return 0;
370
371 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
372 return 0;
373
374 if (!epc->ops->get_msix)
375 return 0;
376
377 mutex_lock(&epc->lock);
378 interrupt = epc->ops->get_msix(epc, func_no, vfunc_no);
379 mutex_unlock(&epc->lock);
380
381 if (interrupt < 0)
382 return 0;
383
384 return interrupt + 1;
385}
386EXPORT_SYMBOL_GPL(pci_epc_get_msix);
387
388
389
390
391
392
393
394
395
396
397
398
399int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
400 u16 interrupts, enum pci_barno bir, u32 offset)
401{
402 int ret;
403
404 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
405 interrupts < 1 || interrupts > 2048)
406 return -EINVAL;
407
408 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
409 return -EINVAL;
410
411 if (!epc->ops->set_msix)
412 return 0;
413
414 mutex_lock(&epc->lock);
415 ret = epc->ops->set_msix(epc, func_no, vfunc_no, interrupts - 1, bir,
416 offset);
417 mutex_unlock(&epc->lock);
418
419 return ret;
420}
421EXPORT_SYMBOL_GPL(pci_epc_set_msix);
422
423
424
425
426
427
428
429
430
431
432void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
433 phys_addr_t phys_addr)
434{
435 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
436 return;
437
438 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
439 return;
440
441 if (!epc->ops->unmap_addr)
442 return;
443
444 mutex_lock(&epc->lock);
445 epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr);
446 mutex_unlock(&epc->lock);
447}
448EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
449
450
451
452
453
454
455
456
457
458
459
460
461int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
462 phys_addr_t phys_addr, u64 pci_addr, size_t size)
463{
464 int ret;
465
466 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
467 return -EINVAL;
468
469 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
470 return -EINVAL;
471
472 if (!epc->ops->map_addr)
473 return 0;
474
475 mutex_lock(&epc->lock);
476 ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr,
477 size);
478 mutex_unlock(&epc->lock);
479
480 return ret;
481}
482EXPORT_SYMBOL_GPL(pci_epc_map_addr);
483
484
485
486
487
488
489
490
491
492
493void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
494 struct pci_epf_bar *epf_bar)
495{
496 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
497 (epf_bar->barno == BAR_5 &&
498 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
499 return;
500
501 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
502 return;
503
504 if (!epc->ops->clear_bar)
505 return;
506
507 mutex_lock(&epc->lock);
508 epc->ops->clear_bar(epc, func_no, vfunc_no, epf_bar);
509 mutex_unlock(&epc->lock);
510}
511EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
512
513
514
515
516
517
518
519
520
521
522int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
523 struct pci_epf_bar *epf_bar)
524{
525 int ret;
526 int flags = epf_bar->flags;
527
528 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
529 (epf_bar->barno == BAR_5 &&
530 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
531 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
532 flags & PCI_BASE_ADDRESS_IO_MASK) ||
533 (upper_32_bits(epf_bar->size) &&
534 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
535 return -EINVAL;
536
537 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
538 return -EINVAL;
539
540 if (!epc->ops->set_bar)
541 return 0;
542
543 mutex_lock(&epc->lock);
544 ret = epc->ops->set_bar(epc, func_no, vfunc_no, epf_bar);
545 mutex_unlock(&epc->lock);
546
547 return ret;
548}
549EXPORT_SYMBOL_GPL(pci_epc_set_bar);
550
551
552
553
554
555
556
557
558
559
560
561
562
563int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
564 struct pci_epf_header *header)
565{
566 int ret;
567
568 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
569 return -EINVAL;
570
571 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
572 return -EINVAL;
573
574
575 if (vfunc_no > 1)
576 return -EINVAL;
577
578 if (!epc->ops->write_header)
579 return 0;
580
581 mutex_lock(&epc->lock);
582 ret = epc->ops->write_header(epc, func_no, vfunc_no, header);
583 mutex_unlock(&epc->lock);
584
585 return ret;
586}
587EXPORT_SYMBOL_GPL(pci_epc_write_header);
588
589
590
591
592
593
594
595
596
597
598
599
600int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
601 enum pci_epc_interface_type type)
602{
603 struct list_head *list;
604 u32 func_no;
605 int ret = 0;
606
607 if (IS_ERR_OR_NULL(epc) || epf->is_vf)
608 return -EINVAL;
609
610 if (type == PRIMARY_INTERFACE && epf->epc)
611 return -EBUSY;
612
613 if (type == SECONDARY_INTERFACE && epf->sec_epc)
614 return -EBUSY;
615
616 mutex_lock(&epc->lock);
617 func_no = find_first_zero_bit(&epc->function_num_map,
618 BITS_PER_LONG);
619 if (func_no >= BITS_PER_LONG) {
620 ret = -EINVAL;
621 goto ret;
622 }
623
624 if (func_no > epc->max_functions - 1) {
625 dev_err(&epc->dev, "Exceeding max supported Function Number\n");
626 ret = -EINVAL;
627 goto ret;
628 }
629
630 set_bit(func_no, &epc->function_num_map);
631 if (type == PRIMARY_INTERFACE) {
632 epf->func_no = func_no;
633 epf->epc = epc;
634 list = &epf->list;
635 } else {
636 epf->sec_epc_func_no = func_no;
637 epf->sec_epc = epc;
638 list = &epf->sec_epc_list;
639 }
640
641 list_add_tail(list, &epc->pci_epf);
642ret:
643 mutex_unlock(&epc->lock);
644
645 return ret;
646}
647EXPORT_SYMBOL_GPL(pci_epc_add_epf);
648
649
650
651
652
653
654
655
656
657
658void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
659 enum pci_epc_interface_type type)
660{
661 struct list_head *list;
662 u32 func_no = 0;
663
664 if (!epc || IS_ERR(epc) || !epf)
665 return;
666
667 if (type == PRIMARY_INTERFACE) {
668 func_no = epf->func_no;
669 list = &epf->list;
670 } else {
671 func_no = epf->sec_epc_func_no;
672 list = &epf->sec_epc_list;
673 }
674
675 mutex_lock(&epc->lock);
676 clear_bit(func_no, &epc->function_num_map);
677 list_del(list);
678 epf->epc = NULL;
679 mutex_unlock(&epc->lock);
680}
681EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
682
683
684
685
686
687
688
689
690
691void pci_epc_linkup(struct pci_epc *epc)
692{
693 if (!epc || IS_ERR(epc))
694 return;
695
696 atomic_notifier_call_chain(&epc->notifier, LINK_UP, NULL);
697}
698EXPORT_SYMBOL_GPL(pci_epc_linkup);
699
700
701
702
703
704
705
706
707
708void pci_epc_init_notify(struct pci_epc *epc)
709{
710 if (!epc || IS_ERR(epc))
711 return;
712
713 atomic_notifier_call_chain(&epc->notifier, CORE_INIT, NULL);
714}
715EXPORT_SYMBOL_GPL(pci_epc_init_notify);
716
717
718
719
720
721
722
723void pci_epc_destroy(struct pci_epc *epc)
724{
725 pci_ep_cfs_remove_epc_group(epc->group);
726 device_unregister(&epc->dev);
727 kfree(epc);
728}
729EXPORT_SYMBOL_GPL(pci_epc_destroy);
730
731
732
733
734
735
736
737
738
739void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
740{
741 int r;
742
743 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
744 epc);
745 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
746}
747EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
748
749
750
751
752
753
754
755
756
757struct pci_epc *
758__pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
759 struct module *owner)
760{
761 int ret;
762 struct pci_epc *epc;
763
764 if (WARN_ON(!dev)) {
765 ret = -EINVAL;
766 goto err_ret;
767 }
768
769 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
770 if (!epc) {
771 ret = -ENOMEM;
772 goto err_ret;
773 }
774
775 mutex_init(&epc->lock);
776 INIT_LIST_HEAD(&epc->pci_epf);
777 ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier);
778
779 device_initialize(&epc->dev);
780 epc->dev.class = pci_epc_class;
781 epc->dev.parent = dev;
782 epc->ops = ops;
783
784 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
785 if (ret)
786 goto put_dev;
787
788 ret = device_add(&epc->dev);
789 if (ret)
790 goto put_dev;
791
792 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
793
794 return epc;
795
796put_dev:
797 put_device(&epc->dev);
798 kfree(epc);
799
800err_ret:
801 return ERR_PTR(ret);
802}
803EXPORT_SYMBOL_GPL(__pci_epc_create);
804
805
806
807
808
809
810
811
812
813
814
815
816struct pci_epc *
817__devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
818 struct module *owner)
819{
820 struct pci_epc **ptr, *epc;
821
822 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
823 if (!ptr)
824 return ERR_PTR(-ENOMEM);
825
826 epc = __pci_epc_create(dev, ops, owner);
827 if (!IS_ERR(epc)) {
828 *ptr = epc;
829 devres_add(dev, ptr);
830 } else {
831 devres_free(ptr);
832 }
833
834 return epc;
835}
836EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
837
838static int __init pci_epc_init(void)
839{
840 pci_epc_class = class_create(THIS_MODULE, "pci_epc");
841 if (IS_ERR(pci_epc_class)) {
842 pr_err("failed to create pci epc class --> %ld\n",
843 PTR_ERR(pci_epc_class));
844 return PTR_ERR(pci_epc_class);
845 }
846
847 return 0;
848}
849module_init(pci_epc_init);
850
851static void __exit pci_epc_exit(void)
852{
853 class_destroy(pci_epc_class);
854}
855module_exit(pci_epc_exit);
856
857MODULE_DESCRIPTION("PCI EPC Library");
858MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
859MODULE_LICENSE("GPL v2");
860