0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/device.h>
0010 #include <linux/slab.h>
0011 #include <linux/module.h>
0012 #include <linux/of_device.h>
0013
0014 #include <linux/pci-epc.h>
0015 #include <linux/pci-epf.h>
0016 #include <linux/pci-ep-cfs.h>
0017
0018 static struct class *pci_epc_class;
0019
0020 static void devm_pci_epc_release(struct device *dev, void *res)
0021 {
0022 struct pci_epc *epc = *(struct pci_epc **)res;
0023
0024 pci_epc_destroy(epc);
0025 }
0026
0027 static int devm_pci_epc_match(struct device *dev, void *res, void *match_data)
0028 {
0029 struct pci_epc **epc = res;
0030
0031 return *epc == match_data;
0032 }
0033
0034
0035
0036
0037
0038
0039
0040 void pci_epc_put(struct pci_epc *epc)
0041 {
0042 if (!epc || IS_ERR(epc))
0043 return;
0044
0045 module_put(epc->ops->owner);
0046 put_device(&epc->dev);
0047 }
0048 EXPORT_SYMBOL_GPL(pci_epc_put);
0049
0050
0051
0052
0053
0054
0055
0056
0057 struct pci_epc *pci_epc_get(const char *epc_name)
0058 {
0059 int ret = -EINVAL;
0060 struct pci_epc *epc;
0061 struct device *dev;
0062 struct class_dev_iter iter;
0063
0064 class_dev_iter_init(&iter, pci_epc_class, NULL, NULL);
0065 while ((dev = class_dev_iter_next(&iter))) {
0066 if (strcmp(epc_name, dev_name(dev)))
0067 continue;
0068
0069 epc = to_pci_epc(dev);
0070 if (!try_module_get(epc->ops->owner)) {
0071 ret = -EINVAL;
0072 goto err;
0073 }
0074
0075 class_dev_iter_exit(&iter);
0076 get_device(&epc->dev);
0077 return epc;
0078 }
0079
0080 err:
0081 class_dev_iter_exit(&iter);
0082 return ERR_PTR(ret);
0083 }
0084 EXPORT_SYMBOL_GPL(pci_epc_get);
0085
0086
0087
0088
0089
0090
0091
0092
0093 enum pci_barno
0094 pci_epc_get_first_free_bar(const struct pci_epc_features *epc_features)
0095 {
0096 return pci_epc_get_next_free_bar(epc_features, BAR_0);
0097 }
0098 EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108 enum pci_barno pci_epc_get_next_free_bar(const struct pci_epc_features
0109 *epc_features, enum pci_barno bar)
0110 {
0111 unsigned long free_bar;
0112
0113 if (!epc_features)
0114 return BAR_0;
0115
0116
0117 if ((epc_features->bar_fixed_64bit << 1) & 1 << bar)
0118 bar++;
0119
0120
0121 free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit;
0122
0123
0124 free_bar <<= 1;
0125 free_bar |= epc_features->reserved_bar;
0126
0127 free_bar = find_next_zero_bit(&free_bar, 6, bar);
0128 if (free_bar > 5)
0129 return NO_BAR;
0130
0131 return free_bar;
0132 }
0133 EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147 const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
0148 u8 func_no, u8 vfunc_no)
0149 {
0150 const struct pci_epc_features *epc_features;
0151
0152 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0153 return NULL;
0154
0155 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0156 return NULL;
0157
0158 if (!epc->ops->get_features)
0159 return NULL;
0160
0161 mutex_lock(&epc->lock);
0162 epc_features = epc->ops->get_features(epc, func_no, vfunc_no);
0163 mutex_unlock(&epc->lock);
0164
0165 return epc_features;
0166 }
0167 EXPORT_SYMBOL_GPL(pci_epc_get_features);
0168
0169
0170
0171
0172
0173
0174
0175 void pci_epc_stop(struct pci_epc *epc)
0176 {
0177 if (IS_ERR(epc) || !epc->ops->stop)
0178 return;
0179
0180 mutex_lock(&epc->lock);
0181 epc->ops->stop(epc);
0182 mutex_unlock(&epc->lock);
0183 }
0184 EXPORT_SYMBOL_GPL(pci_epc_stop);
0185
0186
0187
0188
0189
0190
0191
0192 int pci_epc_start(struct pci_epc *epc)
0193 {
0194 int ret;
0195
0196 if (IS_ERR(epc))
0197 return -EINVAL;
0198
0199 if (!epc->ops->start)
0200 return 0;
0201
0202 mutex_lock(&epc->lock);
0203 ret = epc->ops->start(epc);
0204 mutex_unlock(&epc->lock);
0205
0206 return ret;
0207 }
0208 EXPORT_SYMBOL_GPL(pci_epc_start);
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220 int pci_epc_raise_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0221 enum pci_epc_irq_type type, u16 interrupt_num)
0222 {
0223 int ret;
0224
0225 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0226 return -EINVAL;
0227
0228 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0229 return -EINVAL;
0230
0231 if (!epc->ops->raise_irq)
0232 return 0;
0233
0234 mutex_lock(&epc->lock);
0235 ret = epc->ops->raise_irq(epc, func_no, vfunc_no, type, interrupt_num);
0236 mutex_unlock(&epc->lock);
0237
0238 return ret;
0239 }
0240 EXPORT_SYMBOL_GPL(pci_epc_raise_irq);
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261
0262
0263 int pci_epc_map_msi_irq(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0264 phys_addr_t phys_addr, u8 interrupt_num, u32 entry_size,
0265 u32 *msi_data, u32 *msi_addr_offset)
0266 {
0267 int ret;
0268
0269 if (IS_ERR_OR_NULL(epc))
0270 return -EINVAL;
0271
0272 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0273 return -EINVAL;
0274
0275 if (!epc->ops->map_msi_irq)
0276 return -EINVAL;
0277
0278 mutex_lock(&epc->lock);
0279 ret = epc->ops->map_msi_irq(epc, func_no, vfunc_no, phys_addr,
0280 interrupt_num, entry_size, msi_data,
0281 msi_addr_offset);
0282 mutex_unlock(&epc->lock);
0283
0284 return ret;
0285 }
0286 EXPORT_SYMBOL_GPL(pci_epc_map_msi_irq);
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 int pci_epc_get_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
0297 {
0298 int interrupt;
0299
0300 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0301 return 0;
0302
0303 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0304 return 0;
0305
0306 if (!epc->ops->get_msi)
0307 return 0;
0308
0309 mutex_lock(&epc->lock);
0310 interrupt = epc->ops->get_msi(epc, func_no, vfunc_no);
0311 mutex_unlock(&epc->lock);
0312
0313 if (interrupt < 0)
0314 return 0;
0315
0316 interrupt = 1 << interrupt;
0317
0318 return interrupt;
0319 }
0320 EXPORT_SYMBOL_GPL(pci_epc_get_msi);
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331 int pci_epc_set_msi(struct pci_epc *epc, u8 func_no, u8 vfunc_no, u8 interrupts)
0332 {
0333 int ret;
0334 u8 encode_int;
0335
0336 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
0337 interrupts < 1 || interrupts > 32)
0338 return -EINVAL;
0339
0340 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0341 return -EINVAL;
0342
0343 if (!epc->ops->set_msi)
0344 return 0;
0345
0346 encode_int = order_base_2(interrupts);
0347
0348 mutex_lock(&epc->lock);
0349 ret = epc->ops->set_msi(epc, func_no, vfunc_no, encode_int);
0350 mutex_unlock(&epc->lock);
0351
0352 return ret;
0353 }
0354 EXPORT_SYMBOL_GPL(pci_epc_set_msi);
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364 int pci_epc_get_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no)
0365 {
0366 int interrupt;
0367
0368 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0369 return 0;
0370
0371 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0372 return 0;
0373
0374 if (!epc->ops->get_msix)
0375 return 0;
0376
0377 mutex_lock(&epc->lock);
0378 interrupt = epc->ops->get_msix(epc, func_no, vfunc_no);
0379 mutex_unlock(&epc->lock);
0380
0381 if (interrupt < 0)
0382 return 0;
0383
0384 return interrupt + 1;
0385 }
0386 EXPORT_SYMBOL_GPL(pci_epc_get_msix);
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399 int pci_epc_set_msix(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0400 u16 interrupts, enum pci_barno bir, u32 offset)
0401 {
0402 int ret;
0403
0404 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
0405 interrupts < 1 || interrupts > 2048)
0406 return -EINVAL;
0407
0408 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0409 return -EINVAL;
0410
0411 if (!epc->ops->set_msix)
0412 return 0;
0413
0414 mutex_lock(&epc->lock);
0415 ret = epc->ops->set_msix(epc, func_no, vfunc_no, interrupts - 1, bir,
0416 offset);
0417 mutex_unlock(&epc->lock);
0418
0419 return ret;
0420 }
0421 EXPORT_SYMBOL_GPL(pci_epc_set_msix);
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432 void pci_epc_unmap_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0433 phys_addr_t phys_addr)
0434 {
0435 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0436 return;
0437
0438 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0439 return;
0440
0441 if (!epc->ops->unmap_addr)
0442 return;
0443
0444 mutex_lock(&epc->lock);
0445 epc->ops->unmap_addr(epc, func_no, vfunc_no, phys_addr);
0446 mutex_unlock(&epc->lock);
0447 }
0448 EXPORT_SYMBOL_GPL(pci_epc_unmap_addr);
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461 int pci_epc_map_addr(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0462 phys_addr_t phys_addr, u64 pci_addr, size_t size)
0463 {
0464 int ret;
0465
0466 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0467 return -EINVAL;
0468
0469 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0470 return -EINVAL;
0471
0472 if (!epc->ops->map_addr)
0473 return 0;
0474
0475 mutex_lock(&epc->lock);
0476 ret = epc->ops->map_addr(epc, func_no, vfunc_no, phys_addr, pci_addr,
0477 size);
0478 mutex_unlock(&epc->lock);
0479
0480 return ret;
0481 }
0482 EXPORT_SYMBOL_GPL(pci_epc_map_addr);
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493 void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0494 struct pci_epf_bar *epf_bar)
0495 {
0496 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
0497 (epf_bar->barno == BAR_5 &&
0498 epf_bar->flags & PCI_BASE_ADDRESS_MEM_TYPE_64))
0499 return;
0500
0501 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0502 return;
0503
0504 if (!epc->ops->clear_bar)
0505 return;
0506
0507 mutex_lock(&epc->lock);
0508 epc->ops->clear_bar(epc, func_no, vfunc_no, epf_bar);
0509 mutex_unlock(&epc->lock);
0510 }
0511 EXPORT_SYMBOL_GPL(pci_epc_clear_bar);
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522 int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0523 struct pci_epf_bar *epf_bar)
0524 {
0525 int ret;
0526 int flags = epf_bar->flags;
0527
0528 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions ||
0529 (epf_bar->barno == BAR_5 &&
0530 flags & PCI_BASE_ADDRESS_MEM_TYPE_64) ||
0531 (flags & PCI_BASE_ADDRESS_SPACE_IO &&
0532 flags & PCI_BASE_ADDRESS_IO_MASK) ||
0533 (upper_32_bits(epf_bar->size) &&
0534 !(flags & PCI_BASE_ADDRESS_MEM_TYPE_64)))
0535 return -EINVAL;
0536
0537 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0538 return -EINVAL;
0539
0540 if (!epc->ops->set_bar)
0541 return 0;
0542
0543 mutex_lock(&epc->lock);
0544 ret = epc->ops->set_bar(epc, func_no, vfunc_no, epf_bar);
0545 mutex_unlock(&epc->lock);
0546
0547 return ret;
0548 }
0549 EXPORT_SYMBOL_GPL(pci_epc_set_bar);
0550
0551
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563 int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
0564 struct pci_epf_header *header)
0565 {
0566 int ret;
0567
0568 if (IS_ERR_OR_NULL(epc) || func_no >= epc->max_functions)
0569 return -EINVAL;
0570
0571 if (vfunc_no > 0 && (!epc->max_vfs || vfunc_no > epc->max_vfs[func_no]))
0572 return -EINVAL;
0573
0574
0575 if (vfunc_no > 1)
0576 return -EINVAL;
0577
0578 if (!epc->ops->write_header)
0579 return 0;
0580
0581 mutex_lock(&epc->lock);
0582 ret = epc->ops->write_header(epc, func_no, vfunc_no, header);
0583 mutex_unlock(&epc->lock);
0584
0585 return ret;
0586 }
0587 EXPORT_SYMBOL_GPL(pci_epc_write_header);
0588
0589
0590
0591
0592
0593
0594
0595
0596
0597
0598
0599
0600 int pci_epc_add_epf(struct pci_epc *epc, struct pci_epf *epf,
0601 enum pci_epc_interface_type type)
0602 {
0603 struct list_head *list;
0604 u32 func_no;
0605 int ret = 0;
0606
0607 if (IS_ERR_OR_NULL(epc) || epf->is_vf)
0608 return -EINVAL;
0609
0610 if (type == PRIMARY_INTERFACE && epf->epc)
0611 return -EBUSY;
0612
0613 if (type == SECONDARY_INTERFACE && epf->sec_epc)
0614 return -EBUSY;
0615
0616 mutex_lock(&epc->lock);
0617 func_no = find_first_zero_bit(&epc->function_num_map,
0618 BITS_PER_LONG);
0619 if (func_no >= BITS_PER_LONG) {
0620 ret = -EINVAL;
0621 goto ret;
0622 }
0623
0624 if (func_no > epc->max_functions - 1) {
0625 dev_err(&epc->dev, "Exceeding max supported Function Number\n");
0626 ret = -EINVAL;
0627 goto ret;
0628 }
0629
0630 set_bit(func_no, &epc->function_num_map);
0631 if (type == PRIMARY_INTERFACE) {
0632 epf->func_no = func_no;
0633 epf->epc = epc;
0634 list = &epf->list;
0635 } else {
0636 epf->sec_epc_func_no = func_no;
0637 epf->sec_epc = epc;
0638 list = &epf->sec_epc_list;
0639 }
0640
0641 list_add_tail(list, &epc->pci_epf);
0642 ret:
0643 mutex_unlock(&epc->lock);
0644
0645 return ret;
0646 }
0647 EXPORT_SYMBOL_GPL(pci_epc_add_epf);
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658 void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
0659 enum pci_epc_interface_type type)
0660 {
0661 struct list_head *list;
0662 u32 func_no = 0;
0663
0664 if (!epc || IS_ERR(epc) || !epf)
0665 return;
0666
0667 if (type == PRIMARY_INTERFACE) {
0668 func_no = epf->func_no;
0669 list = &epf->list;
0670 } else {
0671 func_no = epf->sec_epc_func_no;
0672 list = &epf->sec_epc_list;
0673 }
0674
0675 mutex_lock(&epc->lock);
0676 clear_bit(func_no, &epc->function_num_map);
0677 list_del(list);
0678 epf->epc = NULL;
0679 mutex_unlock(&epc->lock);
0680 }
0681 EXPORT_SYMBOL_GPL(pci_epc_remove_epf);
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691 void pci_epc_linkup(struct pci_epc *epc)
0692 {
0693 if (!epc || IS_ERR(epc))
0694 return;
0695
0696 atomic_notifier_call_chain(&epc->notifier, LINK_UP, NULL);
0697 }
0698 EXPORT_SYMBOL_GPL(pci_epc_linkup);
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708 void pci_epc_init_notify(struct pci_epc *epc)
0709 {
0710 if (!epc || IS_ERR(epc))
0711 return;
0712
0713 atomic_notifier_call_chain(&epc->notifier, CORE_INIT, NULL);
0714 }
0715 EXPORT_SYMBOL_GPL(pci_epc_init_notify);
0716
0717
0718
0719
0720
0721
0722
0723 void pci_epc_destroy(struct pci_epc *epc)
0724 {
0725 pci_ep_cfs_remove_epc_group(epc->group);
0726 device_unregister(&epc->dev);
0727 kfree(epc);
0728 }
0729 EXPORT_SYMBOL_GPL(pci_epc_destroy);
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739 void devm_pci_epc_destroy(struct device *dev, struct pci_epc *epc)
0740 {
0741 int r;
0742
0743 r = devres_destroy(dev, devm_pci_epc_release, devm_pci_epc_match,
0744 epc);
0745 dev_WARN_ONCE(dev, r, "couldn't find PCI EPC resource\n");
0746 }
0747 EXPORT_SYMBOL_GPL(devm_pci_epc_destroy);
0748
0749
0750
0751
0752
0753
0754
0755
0756
0757 struct pci_epc *
0758 __pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
0759 struct module *owner)
0760 {
0761 int ret;
0762 struct pci_epc *epc;
0763
0764 if (WARN_ON(!dev)) {
0765 ret = -EINVAL;
0766 goto err_ret;
0767 }
0768
0769 epc = kzalloc(sizeof(*epc), GFP_KERNEL);
0770 if (!epc) {
0771 ret = -ENOMEM;
0772 goto err_ret;
0773 }
0774
0775 mutex_init(&epc->lock);
0776 INIT_LIST_HEAD(&epc->pci_epf);
0777 ATOMIC_INIT_NOTIFIER_HEAD(&epc->notifier);
0778
0779 device_initialize(&epc->dev);
0780 epc->dev.class = pci_epc_class;
0781 epc->dev.parent = dev;
0782 epc->ops = ops;
0783
0784 ret = dev_set_name(&epc->dev, "%s", dev_name(dev));
0785 if (ret)
0786 goto put_dev;
0787
0788 ret = device_add(&epc->dev);
0789 if (ret)
0790 goto put_dev;
0791
0792 epc->group = pci_ep_cfs_add_epc_group(dev_name(dev));
0793
0794 return epc;
0795
0796 put_dev:
0797 put_device(&epc->dev);
0798 kfree(epc);
0799
0800 err_ret:
0801 return ERR_PTR(ret);
0802 }
0803 EXPORT_SYMBOL_GPL(__pci_epc_create);
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814
0815
0816 struct pci_epc *
0817 __devm_pci_epc_create(struct device *dev, const struct pci_epc_ops *ops,
0818 struct module *owner)
0819 {
0820 struct pci_epc **ptr, *epc;
0821
0822 ptr = devres_alloc(devm_pci_epc_release, sizeof(*ptr), GFP_KERNEL);
0823 if (!ptr)
0824 return ERR_PTR(-ENOMEM);
0825
0826 epc = __pci_epc_create(dev, ops, owner);
0827 if (!IS_ERR(epc)) {
0828 *ptr = epc;
0829 devres_add(dev, ptr);
0830 } else {
0831 devres_free(ptr);
0832 }
0833
0834 return epc;
0835 }
0836 EXPORT_SYMBOL_GPL(__devm_pci_epc_create);
0837
0838 static int __init pci_epc_init(void)
0839 {
0840 pci_epc_class = class_create(THIS_MODULE, "pci_epc");
0841 if (IS_ERR(pci_epc_class)) {
0842 pr_err("failed to create pci epc class --> %ld\n",
0843 PTR_ERR(pci_epc_class));
0844 return PTR_ERR(pci_epc_class);
0845 }
0846
0847 return 0;
0848 }
0849 module_init(pci_epc_init);
0850
0851 static void __exit pci_epc_exit(void)
0852 {
0853 class_destroy(pci_epc_class);
0854 }
0855 module_exit(pci_epc_exit);
0856
0857 MODULE_DESCRIPTION("PCI EPC Library");
0858 MODULE_AUTHOR("Kishon Vijay Abraham I <kishon@ti.com>");
0859 MODULE_LICENSE("GPL v2");