0001
0002
0003 #include <linux/kernel.h>
0004 #include <linux/ioport.h>
0005 #include <linux/bitmap.h>
0006 #include <linux/pci.h>
0007
0008 #include <asm/opal.h>
0009
0010 #include "pci.h"
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143 static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
0144 {
0145 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
0146 struct resource *res;
0147 int i;
0148 resource_size_t vf_bar_sz;
0149 struct pnv_iov_data *iov;
0150 int mul;
0151
0152 iov = kzalloc(sizeof(*iov), GFP_KERNEL);
0153 if (!iov)
0154 goto disable_iov;
0155 pdev->dev.archdata.iov_data = iov;
0156 mul = phb->ioda.total_pe_num;
0157
0158 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0159 res = &pdev->resource[i + PCI_IOV_RESOURCES];
0160 if (!res->flags || res->parent)
0161 continue;
0162 if (!pnv_pci_is_m64_flags(res->flags)) {
0163 dev_warn(&pdev->dev, "Don't support SR-IOV with non M64 VF BAR%d: %pR. \n",
0164 i, res);
0165 goto disable_iov;
0166 }
0167
0168 vf_bar_sz = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180 if (vf_bar_sz > (phb->ioda.m64_segsize >> 2)) {
0181
0182
0183
0184
0185
0186
0187
0188 if (vf_bar_sz < SZ_32M) {
0189 pci_err(pdev, "VF BAR%d: %pR can't be mapped in single PE mode\n",
0190 i, res);
0191 goto disable_iov;
0192 }
0193
0194 iov->m64_single_mode[i] = true;
0195 continue;
0196 }
0197
0198
0199
0200
0201
0202 pci_dbg(pdev, " Fixing VF BAR%d: %pR to\n", i, res);
0203 res->end = res->start + vf_bar_sz * mul - 1;
0204 pci_dbg(pdev, " %pR\n", res);
0205
0206 pci_info(pdev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
0207 i, res, mul);
0208
0209 iov->need_shift = true;
0210 }
0211
0212 return;
0213
0214 disable_iov:
0215
0216 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0217 res = &pdev->resource[i + PCI_IOV_RESOURCES];
0218 res->flags = 0;
0219 res->end = res->start - 1;
0220 }
0221
0222 pdev->dev.archdata.iov_data = NULL;
0223 kfree(iov);
0224 }
0225
0226 void pnv_pci_ioda_fixup_iov(struct pci_dev *pdev)
0227 {
0228 if (pdev->is_virtfn) {
0229 struct pnv_ioda_pe *pe = pnv_ioda_get_pe(pdev);
0230
0231
0232
0233
0234
0235
0236 pe->pdev = pdev;
0237 WARN_ON(!(pe->flags & PNV_IODA_PE_VF));
0238 } else if (pdev->is_physfn) {
0239
0240
0241
0242
0243 pnv_pci_ioda_fixup_iov_resources(pdev);
0244 }
0245 }
0246
0247 resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
0248 int resno)
0249 {
0250 resource_size_t align = pci_iov_resource_size(pdev, resno);
0251 struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
0252 struct pnv_iov_data *iov = pnv_iov_get(pdev);
0253
0254
0255
0256
0257
0258
0259
0260 if (!iov)
0261 return align;
0262
0263
0264
0265
0266
0267
0268 if (iov->m64_single_mode[resno - PCI_IOV_RESOURCES])
0269 return align;
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280
0281 return phb->ioda.total_pe_num * align;
0282 }
0283
0284 static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
0285 {
0286 struct pnv_iov_data *iov;
0287 struct pnv_phb *phb;
0288 int window_id;
0289
0290 phb = pci_bus_to_pnvhb(pdev->bus);
0291 iov = pnv_iov_get(pdev);
0292
0293 for_each_set_bit(window_id, iov->used_m64_bar_mask, MAX_M64_BARS) {
0294 opal_pci_phb_mmio_enable(phb->opal_id,
0295 OPAL_M64_WINDOW_TYPE,
0296 window_id,
0297 0);
0298
0299 clear_bit(window_id, &phb->ioda.m64_bar_alloc);
0300 }
0301
0302 return 0;
0303 }
0304
0305
0306
0307
0308
0309
0310
0311 static int64_t pnv_ioda_map_m64_segmented(struct pnv_phb *phb,
0312 int window_id,
0313 resource_size_t start,
0314 resource_size_t size)
0315 {
0316 int64_t rc;
0317
0318 rc = opal_pci_set_phb_mem_window(phb->opal_id,
0319 OPAL_M64_WINDOW_TYPE,
0320 window_id,
0321 start,
0322 0,
0323 size);
0324 if (rc)
0325 goto out;
0326
0327 rc = opal_pci_phb_mmio_enable(phb->opal_id,
0328 OPAL_M64_WINDOW_TYPE,
0329 window_id,
0330 OPAL_ENABLE_M64_SPLIT);
0331 out:
0332 if (rc)
0333 pr_err("Failed to map M64 window #%d: %lld\n", window_id, rc);
0334
0335 return rc;
0336 }
0337
0338 static int64_t pnv_ioda_map_m64_single(struct pnv_phb *phb,
0339 int pe_num,
0340 int window_id,
0341 resource_size_t start,
0342 resource_size_t size)
0343 {
0344 int64_t rc;
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363 rc = opal_pci_map_pe_mmio_window(phb->opal_id,
0364 pe_num,
0365 OPAL_M64_WINDOW_TYPE,
0366 window_id,
0367 0);
0368 if (rc)
0369 goto out;
0370
0371
0372
0373
0374 rc = opal_pci_set_phb_mem_window(phb->opal_id,
0375 OPAL_M64_WINDOW_TYPE,
0376 window_id,
0377 start,
0378 0,
0379 size);
0380 if (rc)
0381 goto out;
0382
0383
0384
0385
0386
0387 rc = opal_pci_phb_mmio_enable(phb->opal_id,
0388 OPAL_M64_WINDOW_TYPE,
0389 window_id,
0390 OPAL_ENABLE_M64_NON_SPLIT);
0391 out:
0392 if (rc)
0393 pr_err("Error mapping single PE BAR\n");
0394
0395 return rc;
0396 }
0397
0398 static int pnv_pci_alloc_m64_bar(struct pnv_phb *phb, struct pnv_iov_data *iov)
0399 {
0400 int win;
0401
0402 do {
0403 win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
0404 phb->ioda.m64_bar_idx + 1, 0);
0405
0406 if (win >= phb->ioda.m64_bar_idx + 1)
0407 return -1;
0408 } while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
0409
0410 set_bit(win, iov->used_m64_bar_mask);
0411
0412 return win;
0413 }
0414
0415 static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
0416 {
0417 struct pnv_iov_data *iov;
0418 struct pnv_phb *phb;
0419 int win;
0420 struct resource *res;
0421 int i, j;
0422 int64_t rc;
0423 resource_size_t size, start;
0424 int base_pe_num;
0425
0426 phb = pci_bus_to_pnvhb(pdev->bus);
0427 iov = pnv_iov_get(pdev);
0428
0429 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0430 res = &pdev->resource[i + PCI_IOV_RESOURCES];
0431 if (!res->flags || !res->parent)
0432 continue;
0433
0434
0435 if (!iov->m64_single_mode[i]) {
0436 win = pnv_pci_alloc_m64_bar(phb, iov);
0437 if (win < 0)
0438 goto m64_failed;
0439
0440 size = resource_size(res);
0441 start = res->start;
0442
0443 rc = pnv_ioda_map_m64_segmented(phb, win, start, size);
0444 if (rc)
0445 goto m64_failed;
0446
0447 continue;
0448 }
0449
0450
0451 size = pci_iov_resource_size(pdev, PCI_IOV_RESOURCES + i);
0452 base_pe_num = iov->vf_pe_arr[0].pe_number;
0453
0454 for (j = 0; j < num_vfs; j++) {
0455 win = pnv_pci_alloc_m64_bar(phb, iov);
0456 if (win < 0)
0457 goto m64_failed;
0458
0459 start = res->start + size * j;
0460 rc = pnv_ioda_map_m64_single(phb, win,
0461 base_pe_num + j,
0462 start,
0463 size);
0464 if (rc)
0465 goto m64_failed;
0466 }
0467 }
0468 return 0;
0469
0470 m64_failed:
0471 pnv_pci_vf_release_m64(pdev, num_vfs);
0472 return -EBUSY;
0473 }
0474
0475 static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
0476 {
0477 struct pnv_phb *phb;
0478 struct pnv_ioda_pe *pe, *pe_n;
0479
0480 phb = pci_bus_to_pnvhb(pdev->bus);
0481
0482 if (!pdev->is_physfn)
0483 return;
0484
0485
0486 list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
0487 if (pe->parent_dev != pdev)
0488 continue;
0489
0490 pnv_pci_ioda2_release_pe_dma(pe);
0491
0492
0493 mutex_lock(&phb->ioda.pe_list_mutex);
0494 list_del(&pe->list);
0495 mutex_unlock(&phb->ioda.pe_list_mutex);
0496
0497 pnv_ioda_deconfigure_pe(phb, pe);
0498
0499 pnv_ioda_free_pe(pe);
0500 }
0501 }
0502
0503 static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
0504 {
0505 struct resource *res, res2;
0506 struct pnv_iov_data *iov;
0507 resource_size_t size;
0508 u16 num_vfs;
0509 int i;
0510
0511 if (!dev->is_physfn)
0512 return -EINVAL;
0513 iov = pnv_iov_get(dev);
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523 num_vfs = iov->num_vfs;
0524 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0525 res = &dev->resource[i + PCI_IOV_RESOURCES];
0526 if (!res->flags || !res->parent)
0527 continue;
0528 if (iov->m64_single_mode[i])
0529 continue;
0530
0531
0532
0533
0534
0535
0536
0537 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
0538 res2.flags = res->flags;
0539 res2.start = res->start + (size * offset);
0540 res2.end = res2.start + (size * num_vfs) - 1;
0541
0542 if (res2.end > res->end) {
0543 dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
0544 i, &res2, res, num_vfs, offset);
0545 return -EBUSY;
0546 }
0547 }
0548
0549
0550
0551
0552
0553
0554
0555
0556
0557 for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
0558 res = &dev->resource[i + PCI_IOV_RESOURCES];
0559 if (!res->flags || !res->parent)
0560 continue;
0561 if (iov->m64_single_mode[i])
0562 continue;
0563
0564 size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
0565 res2 = *res;
0566 res->start += size * offset;
0567
0568 dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
0569 i, &res2, res, (offset > 0) ? "En" : "Dis",
0570 num_vfs, offset);
0571
0572 if (offset < 0) {
0573 devm_release_resource(&dev->dev, &iov->holes[i]);
0574 memset(&iov->holes[i], 0, sizeof(iov->holes[i]));
0575 }
0576
0577 pci_update_resource(dev, i + PCI_IOV_RESOURCES);
0578
0579 if (offset > 0) {
0580 iov->holes[i].start = res2.start;
0581 iov->holes[i].end = res2.start + size * offset - 1;
0582 iov->holes[i].flags = IORESOURCE_BUS;
0583 iov->holes[i].name = "pnv_iov_reserved";
0584 devm_request_resource(&dev->dev, res->parent,
0585 &iov->holes[i]);
0586 }
0587 }
0588 return 0;
0589 }
0590
0591 static void pnv_pci_sriov_disable(struct pci_dev *pdev)
0592 {
0593 u16 num_vfs, base_pe;
0594 struct pnv_iov_data *iov;
0595
0596 iov = pnv_iov_get(pdev);
0597 num_vfs = iov->num_vfs;
0598 base_pe = iov->vf_pe_arr[0].pe_number;
0599
0600 if (WARN_ON(!iov))
0601 return;
0602
0603
0604 pnv_ioda_release_vf_PE(pdev);
0605
0606
0607 if (iov->need_shift)
0608 pnv_pci_vf_resource_shift(pdev, -base_pe);
0609
0610
0611 pnv_pci_vf_release_m64(pdev, num_vfs);
0612 }
0613
0614 static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
0615 {
0616 struct pnv_phb *phb;
0617 struct pnv_ioda_pe *pe;
0618 int pe_num;
0619 u16 vf_index;
0620 struct pnv_iov_data *iov;
0621 struct pci_dn *pdn;
0622
0623 if (!pdev->is_physfn)
0624 return;
0625
0626 phb = pci_bus_to_pnvhb(pdev->bus);
0627 pdn = pci_get_pdn(pdev);
0628 iov = pnv_iov_get(pdev);
0629
0630
0631 for (vf_index = 0; vf_index < num_vfs; vf_index++) {
0632 int vf_devfn = pci_iov_virtfn_devfn(pdev, vf_index);
0633 int vf_bus = pci_iov_virtfn_bus(pdev, vf_index);
0634 struct pci_dn *vf_pdn;
0635
0636 pe = &iov->vf_pe_arr[vf_index];
0637 pe->phb = phb;
0638 pe->flags = PNV_IODA_PE_VF;
0639 pe->pbus = NULL;
0640 pe->parent_dev = pdev;
0641 pe->mve_number = -1;
0642 pe->rid = (vf_bus << 8) | vf_devfn;
0643
0644 pe_num = pe->pe_number;
0645 pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
0646 pci_domain_nr(pdev->bus), pdev->bus->number,
0647 PCI_SLOT(vf_devfn), PCI_FUNC(vf_devfn), pe_num);
0648
0649 if (pnv_ioda_configure_pe(phb, pe)) {
0650
0651 pnv_ioda_free_pe(pe);
0652 pe->pdev = NULL;
0653 continue;
0654 }
0655
0656
0657 mutex_lock(&phb->ioda.pe_list_mutex);
0658 list_add_tail(&pe->list, &phb->ioda.pe_list);
0659 mutex_unlock(&phb->ioda.pe_list_mutex);
0660
0661
0662 list_for_each_entry(vf_pdn, &pdn->parent->child_list, list) {
0663 if (vf_pdn->busno == vf_bus &&
0664 vf_pdn->devfn == vf_devfn) {
0665 vf_pdn->pe_number = pe_num;
0666 break;
0667 }
0668 }
0669
0670 pnv_pci_ioda2_setup_dma_pe(phb, pe);
0671 }
0672 }
0673
0674 static int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
0675 {
0676 struct pnv_ioda_pe *base_pe;
0677 struct pnv_iov_data *iov;
0678 struct pnv_phb *phb;
0679 int ret;
0680 u16 i;
0681
0682 phb = pci_bus_to_pnvhb(pdev->bus);
0683 iov = pnv_iov_get(pdev);
0684
0685
0686
0687
0688
0689
0690
0691
0692 if (phb->type != PNV_PHB_IODA2) {
0693 pci_err(pdev, "SR-IOV is not supported on this PHB\n");
0694 return -ENXIO;
0695 }
0696
0697 if (!iov) {
0698 dev_info(&pdev->dev, "don't support this SRIOV device with non 64bit-prefetchable IOV BAR\n");
0699 return -ENOSPC;
0700 }
0701
0702
0703 base_pe = pnv_ioda_alloc_pe(phb, num_vfs);
0704 if (!base_pe) {
0705 pci_err(pdev, "Unable to allocate PEs for %d VFs\n", num_vfs);
0706 return -EBUSY;
0707 }
0708
0709 iov->vf_pe_arr = base_pe;
0710 iov->num_vfs = num_vfs;
0711
0712
0713 ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
0714 if (ret) {
0715 dev_info(&pdev->dev, "Not enough M64 window resources\n");
0716 goto m64_failed;
0717 }
0718
0719
0720
0721
0722
0723
0724 if (iov->need_shift) {
0725 ret = pnv_pci_vf_resource_shift(pdev, base_pe->pe_number);
0726 if (ret)
0727 goto shift_failed;
0728 }
0729
0730
0731 pnv_ioda_setup_vf_PE(pdev, num_vfs);
0732
0733 return 0;
0734
0735 shift_failed:
0736 pnv_pci_vf_release_m64(pdev, num_vfs);
0737
0738 m64_failed:
0739 for (i = 0; i < num_vfs; i++)
0740 pnv_ioda_free_pe(&iov->vf_pe_arr[i]);
0741
0742 return ret;
0743 }
0744
0745 int pnv_pcibios_sriov_disable(struct pci_dev *pdev)
0746 {
0747 pnv_pci_sriov_disable(pdev);
0748
0749
0750 remove_sriov_vf_pdns(pdev);
0751 return 0;
0752 }
0753
0754 int pnv_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
0755 {
0756
0757 add_sriov_vf_pdns(pdev);
0758
0759 return pnv_pci_sriov_enable(pdev, num_vfs);
0760 }