xref: /OK3568_Linux_fs/kernel/arch/powerpc/platforms/powernv/pci-sriov.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun 
3*4882a593Smuzhiyun #include <linux/kernel.h>
4*4882a593Smuzhiyun #include <linux/ioport.h>
5*4882a593Smuzhiyun #include <linux/bitmap.h>
6*4882a593Smuzhiyun #include <linux/pci.h>
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun #include <asm/opal.h>
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include "pci.h"
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun /* for pci_dev_is_added() */
13*4882a593Smuzhiyun #include "../../../../drivers/pci/pci.h"
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /*
16*4882a593Smuzhiyun  * The majority of the complexity in supporting SR-IOV on PowerNV comes from
17*4882a593Smuzhiyun  * the need to put the MMIO space for each VF into a separate PE. Internally
18*4882a593Smuzhiyun  * the PHB maps MMIO addresses to a specific PE using the "Memory BAR Table".
19*4882a593Smuzhiyun  * The MBT historically only applied to the 64bit MMIO window of the PHB
20*4882a593Smuzhiyun  * so it's common to see it referred to as the "M64BT".
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * An MBT entry stores the mapped range as an <base>,<mask> pair. This forces
23*4882a593Smuzhiyun  * the address range that we want to map to be power-of-two sized and aligned.
24*4882a593Smuzhiyun  * For conventional PCI devices this isn't really an issue since PCI device BARs
25*4882a593Smuzhiyun  * have the same requirement.
26*4882a593Smuzhiyun  *
27*4882a593Smuzhiyun  * For a SR-IOV BAR things are a little more awkward since size and alignment
28*4882a593Smuzhiyun  * are not coupled. The alignment is set based on the the per-VF BAR size, but
29*4882a593Smuzhiyun  * the total BAR area is: number-of-vfs * per-vf-size. The number of VFs
30*4882a593Smuzhiyun  * isn't necessarily a power of two, so neither is the total size. To fix that
31*4882a593Smuzhiyun  * we need to finesse (read: hack) the Linux BAR allocator so that it will
32*4882a593Smuzhiyun  * allocate the SR-IOV BARs in a way that lets us map them using the MBT.
33*4882a593Smuzhiyun  *
34*4882a593Smuzhiyun  * The changes to size and alignment that we need to do depend on the "mode"
35*4882a593Smuzhiyun  * of MBT entry that we use. We only support SR-IOV on PHB3 (IODA2) and above,
36*4882a593Smuzhiyun  * so as a baseline we can assume that we have the following BAR modes
37*4882a593Smuzhiyun  * available:
38*4882a593Smuzhiyun  *
39*4882a593Smuzhiyun  *   NB: $PE_COUNT is the number of PEs that the PHB supports.
40*4882a593Smuzhiyun  *
41*4882a593Smuzhiyun  * a) A segmented BAR that splits the mapped range into $PE_COUNT equally sized
42*4882a593Smuzhiyun  *    segments. The n'th segment is mapped to the n'th PE.
43*4882a593Smuzhiyun  * b) An un-segmented BAR that maps the whole address range to a specific PE.
44*4882a593Smuzhiyun  *
45*4882a593Smuzhiyun  *
46*4882a593Smuzhiyun  * We prefer to use mode a) since it only requires one MBT entry per SR-IOV BAR
47*4882a593Smuzhiyun  * For comparison b) requires one entry per-VF per-BAR, or:
48*4882a593Smuzhiyun  * (num-vfs * num-sriov-bars) in total. To use a) we need the size of each segment
49*4882a593Smuzhiyun  * to equal the size of the per-VF BAR area. So:
50*4882a593Smuzhiyun  *
51*4882a593Smuzhiyun  *	new_size = per-vf-size * number-of-PEs
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * The alignment for the SR-IOV BAR also needs to be changed from per-vf-size
54*4882a593Smuzhiyun  * to "new_size", calculated above. Implementing this is a convoluted process
55*4882a593Smuzhiyun  * which requires several hooks in the PCI core:
56*4882a593Smuzhiyun  *
57*4882a593Smuzhiyun  * 1. In pcibios_add_device() we call pnv_pci_ioda_fixup_iov().
58*4882a593Smuzhiyun  *
59*4882a593Smuzhiyun  *    At this point the device has been probed and the device's BARs are sized,
60*4882a593Smuzhiyun  *    but no resource allocations have been done. The SR-IOV BARs are sized
61*4882a593Smuzhiyun  *    based on the maximum number of VFs supported by the device and we need
62*4882a593Smuzhiyun  *    to increase that to new_size.
63*4882a593Smuzhiyun  *
64*4882a593Smuzhiyun  * 2. Later, when Linux actually assigns resources it tries to make the resource
65*4882a593Smuzhiyun  *    allocations for each PCI bus as compact as possible. As a part of that it
66*4882a593Smuzhiyun  *    sorts the BARs on a bus by their required alignment, which is calculated
67*4882a593Smuzhiyun  *    using pci_resource_alignment().
68*4882a593Smuzhiyun  *
69*4882a593Smuzhiyun  *    For IOV resources this goes:
70*4882a593Smuzhiyun  *    pci_resource_alignment()
71*4882a593Smuzhiyun  *        pci_sriov_resource_alignment()
72*4882a593Smuzhiyun  *            pcibios_sriov_resource_alignment()
73*4882a593Smuzhiyun  *                pnv_pci_iov_resource_alignment()
74*4882a593Smuzhiyun  *
75*4882a593Smuzhiyun  *    Our hook overrides the default alignment, equal to the per-vf-size, with
76*4882a593Smuzhiyun  *    new_size computed above.
77*4882a593Smuzhiyun  *
78*4882a593Smuzhiyun  * 3. When userspace enables VFs for a device:
79*4882a593Smuzhiyun  *
80*4882a593Smuzhiyun  *    sriov_enable()
81*4882a593Smuzhiyun  *       pcibios_sriov_enable()
82*4882a593Smuzhiyun  *           pnv_pcibios_sriov_enable()
83*4882a593Smuzhiyun  *
84*4882a593Smuzhiyun  *    This is where we actually allocate PE numbers for each VF and setup the
85*4882a593Smuzhiyun  *    MBT mapping for each SR-IOV BAR. In steps 1) and 2) we setup an "arena"
86*4882a593Smuzhiyun  *    where each MBT segment is equal in size to the VF BAR so we can shift
87*4882a593Smuzhiyun  *    around the actual SR-IOV BAR location within this arena. We need this
88*4882a593Smuzhiyun  *    ability because the PE space is shared by all devices on the same PHB.
89*4882a593Smuzhiyun  *    When using mode a) described above segment 0 in maps to PE#0 which might
90*4882a593Smuzhiyun  *    be already being used by another device on the PHB.
91*4882a593Smuzhiyun  *
92*4882a593Smuzhiyun  *    As a result we need allocate a contigious range of PE numbers, then shift
93*4882a593Smuzhiyun  *    the address programmed into the SR-IOV BAR of the PF so that the address
94*4882a593Smuzhiyun  *    of VF0 matches up with the segment corresponding to the first allocated
95*4882a593Smuzhiyun  *    PE number. This is handled in pnv_pci_vf_resource_shift().
96*4882a593Smuzhiyun  *
97*4882a593Smuzhiyun  *    Once all that is done we return to the PCI core which then enables VFs,
98*4882a593Smuzhiyun  *    scans them and creates pci_devs for each. The init process for a VF is
99*4882a593Smuzhiyun  *    largely the same as a normal device, but the VF is inserted into the IODA
100*4882a593Smuzhiyun  *    PE that we allocated for it rather than the PE associated with the bus.
101*4882a593Smuzhiyun  *
102*4882a593Smuzhiyun  * 4. When userspace disables VFs we unwind the above in
103*4882a593Smuzhiyun  *    pnv_pcibios_sriov_disable(). Fortunately this is relatively simple since
104*4882a593Smuzhiyun  *    we don't need to validate anything, just tear down the mappings and
105*4882a593Smuzhiyun  *    move SR-IOV resource back to its "proper" location.
106*4882a593Smuzhiyun  *
107*4882a593Smuzhiyun  * That's how mode a) works. In theory mode b) (single PE mapping) is less work
108*4882a593Smuzhiyun  * since we can map each individual VF with a separate BAR. However, there's a
109*4882a593Smuzhiyun  * few limitations:
110*4882a593Smuzhiyun  *
111*4882a593Smuzhiyun  * 1) For IODA2 mode b) has a minimum alignment requirement of 32MB. This makes
112*4882a593Smuzhiyun  *    it only usable for devices with very large per-VF BARs. Such devices are
113*4882a593Smuzhiyun  *    similar to Big Foot. They definitely exist, but I've never seen one.
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  * 2) The number of MBT entries that we have is limited. PHB3 and PHB4 only
116*4882a593Smuzhiyun  *    16 total and some are needed for. Most SR-IOV capable network cards can support
117*4882a593Smuzhiyun  *    more than 16 VFs on each port.
118*4882a593Smuzhiyun  *
119*4882a593Smuzhiyun  * We use b) when using a) would use more than 1/4 of the entire 64 bit MMIO
120*4882a593Smuzhiyun  * window of the PHB.
121*4882a593Smuzhiyun  *
122*4882a593Smuzhiyun  *
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * PHB4 (IODA3) added a few new features that would be useful for SR-IOV. It
125*4882a593Smuzhiyun  * allowed the MBT to map 32bit MMIO space in addition to 64bit which allows
126*4882a593Smuzhiyun  * us to support SR-IOV BARs in the 32bit MMIO window. This is useful since
127*4882a593Smuzhiyun  * the Linux BAR allocation will place any BAR marked as non-prefetchable into
128*4882a593Smuzhiyun  * the non-prefetchable bridge window, which is 32bit only. It also added two
129*4882a593Smuzhiyun  * new modes:
130*4882a593Smuzhiyun  *
131*4882a593Smuzhiyun  * c) A segmented BAR similar to a), but each segment can be individually
132*4882a593Smuzhiyun  *    mapped to any PE. This is matches how the 32bit MMIO window worked on
133*4882a593Smuzhiyun  *    IODA1&2.
134*4882a593Smuzhiyun  *
135*4882a593Smuzhiyun  * d) A segmented BAR with 8, 64, or 128 segments. This works similarly to a),
136*4882a593Smuzhiyun  *    but with fewer segments and configurable base PE.
137*4882a593Smuzhiyun  *
138*4882a593Smuzhiyun  *    i.e. The n'th segment maps to the (n + base)'th PE.
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  *    The base PE is also required to be a multiple of the window size.
141*4882a593Smuzhiyun  *
142*4882a593Smuzhiyun  * Unfortunately, the OPAL API doesn't currently (as of skiboot v6.6) allow us
143*4882a593Smuzhiyun  * to exploit any of the IODA3 features.
144*4882a593Smuzhiyun  */
145*4882a593Smuzhiyun 
pnv_pci_ioda_fixup_iov_resources(struct pci_dev * pdev)146*4882a593Smuzhiyun static void pnv_pci_ioda_fixup_iov_resources(struct pci_dev *pdev)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun 	struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
149*4882a593Smuzhiyun 	struct resource *res;
150*4882a593Smuzhiyun 	int i;
151*4882a593Smuzhiyun 	resource_size_t vf_bar_sz;
152*4882a593Smuzhiyun 	struct pnv_iov_data *iov;
153*4882a593Smuzhiyun 	int mul;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	iov = kzalloc(sizeof(*iov), GFP_KERNEL);
156*4882a593Smuzhiyun 	if (!iov)
157*4882a593Smuzhiyun 		goto disable_iov;
158*4882a593Smuzhiyun 	pdev->dev.archdata.iov_data = iov;
159*4882a593Smuzhiyun 	mul = phb->ioda.total_pe_num;
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
162*4882a593Smuzhiyun 		res = &pdev->resource[i + PCI_IOV_RESOURCES];
163*4882a593Smuzhiyun 		if (!res->flags || res->parent)
164*4882a593Smuzhiyun 			continue;
165*4882a593Smuzhiyun 		if (!pnv_pci_is_m64_flags(res->flags)) {
166*4882a593Smuzhiyun 			dev_warn(&pdev->dev, "Don't support SR-IOV with non M64 VF BAR%d: %pR. \n",
167*4882a593Smuzhiyun 				 i, res);
168*4882a593Smuzhiyun 			goto disable_iov;
169*4882a593Smuzhiyun 		}
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 		vf_bar_sz = pci_iov_resource_size(pdev, i + PCI_IOV_RESOURCES);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 		/*
174*4882a593Smuzhiyun 		 * Generally, one segmented M64 BAR maps one IOV BAR. However,
175*4882a593Smuzhiyun 		 * if a VF BAR is too large we end up wasting a lot of space.
176*4882a593Smuzhiyun 		 * If each VF needs more than 1/4 of the default m64 segment
177*4882a593Smuzhiyun 		 * then each VF BAR should be mapped in single-PE mode to reduce
178*4882a593Smuzhiyun 		 * the amount of space required. This does however limit the
179*4882a593Smuzhiyun 		 * number of VFs we can support.
180*4882a593Smuzhiyun 		 *
181*4882a593Smuzhiyun 		 * The 1/4 limit is arbitrary and can be tweaked.
182*4882a593Smuzhiyun 		 */
183*4882a593Smuzhiyun 		if (vf_bar_sz > (phb->ioda.m64_segsize >> 2)) {
184*4882a593Smuzhiyun 			/*
185*4882a593Smuzhiyun 			 * On PHB3, the minimum size alignment of M64 BAR in
186*4882a593Smuzhiyun 			 * single mode is 32MB. If this VF BAR is smaller than
187*4882a593Smuzhiyun 			 * 32MB, but still too large for a segmented window
188*4882a593Smuzhiyun 			 * then we can't map it and need to disable SR-IOV for
189*4882a593Smuzhiyun 			 * this device.
190*4882a593Smuzhiyun 			 */
191*4882a593Smuzhiyun 			if (vf_bar_sz < SZ_32M) {
192*4882a593Smuzhiyun 				pci_err(pdev, "VF BAR%d: %pR can't be mapped in single PE mode\n",
193*4882a593Smuzhiyun 					i, res);
194*4882a593Smuzhiyun 				goto disable_iov;
195*4882a593Smuzhiyun 			}
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 			iov->m64_single_mode[i] = true;
198*4882a593Smuzhiyun 			continue;
199*4882a593Smuzhiyun 		}
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 		/*
202*4882a593Smuzhiyun 		 * This BAR can be mapped with one segmented window, so adjust
203*4882a593Smuzhiyun 		 * te resource size to accommodate.
204*4882a593Smuzhiyun 		 */
205*4882a593Smuzhiyun 		pci_dbg(pdev, " Fixing VF BAR%d: %pR to\n", i, res);
206*4882a593Smuzhiyun 		res->end = res->start + vf_bar_sz * mul - 1;
207*4882a593Smuzhiyun 		pci_dbg(pdev, "                       %pR\n", res);
208*4882a593Smuzhiyun 
209*4882a593Smuzhiyun 		pci_info(pdev, "VF BAR%d: %pR (expanded to %d VFs for PE alignment)",
210*4882a593Smuzhiyun 			 i, res, mul);
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun 		iov->need_shift = true;
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	return;
216*4882a593Smuzhiyun 
217*4882a593Smuzhiyun disable_iov:
218*4882a593Smuzhiyun 	/* Save ourselves some MMIO space by disabling the unusable BARs */
219*4882a593Smuzhiyun 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
220*4882a593Smuzhiyun 		res = &pdev->resource[i + PCI_IOV_RESOURCES];
221*4882a593Smuzhiyun 		res->flags = 0;
222*4882a593Smuzhiyun 		res->end = res->start - 1;
223*4882a593Smuzhiyun 	}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	pdev->dev.archdata.iov_data = NULL;
226*4882a593Smuzhiyun 	kfree(iov);
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
pnv_pci_ioda_fixup_iov(struct pci_dev * pdev)229*4882a593Smuzhiyun void pnv_pci_ioda_fixup_iov(struct pci_dev *pdev)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	if (WARN_ON(pci_dev_is_added(pdev)))
232*4882a593Smuzhiyun 		return;
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	if (pdev->is_virtfn) {
235*4882a593Smuzhiyun 		struct pnv_ioda_pe *pe = pnv_ioda_get_pe(pdev);
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 		/*
238*4882a593Smuzhiyun 		 * VF PEs are single-device PEs so their pdev pointer needs to
239*4882a593Smuzhiyun 		 * be set. The pdev doesn't exist when the PE is allocated (in
240*4882a593Smuzhiyun 		 * (pcibios_sriov_enable()) so we fix it up here.
241*4882a593Smuzhiyun 		 */
242*4882a593Smuzhiyun 		pe->pdev = pdev;
243*4882a593Smuzhiyun 		WARN_ON(!(pe->flags & PNV_IODA_PE_VF));
244*4882a593Smuzhiyun 	} else if (pdev->is_physfn) {
245*4882a593Smuzhiyun 		/*
246*4882a593Smuzhiyun 		 * For PFs adjust their allocated IOV resources to match what
247*4882a593Smuzhiyun 		 * the PHB can support using it's M64 BAR table.
248*4882a593Smuzhiyun 		 */
249*4882a593Smuzhiyun 		pnv_pci_ioda_fixup_iov_resources(pdev);
250*4882a593Smuzhiyun 	}
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun 
pnv_pci_iov_resource_alignment(struct pci_dev * pdev,int resno)253*4882a593Smuzhiyun resource_size_t pnv_pci_iov_resource_alignment(struct pci_dev *pdev,
254*4882a593Smuzhiyun 						      int resno)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun 	resource_size_t align = pci_iov_resource_size(pdev, resno);
257*4882a593Smuzhiyun 	struct pnv_phb *phb = pci_bus_to_pnvhb(pdev->bus);
258*4882a593Smuzhiyun 	struct pnv_iov_data *iov = pnv_iov_get(pdev);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/*
261*4882a593Smuzhiyun 	 * iov can be null if we have an SR-IOV device with IOV BAR that can't
262*4882a593Smuzhiyun 	 * be placed in the m64 space (i.e. The BAR is 32bit or non-prefetch).
263*4882a593Smuzhiyun 	 * In that case we don't allow VFs to be enabled since one of their
264*4882a593Smuzhiyun 	 * BARs would not be placed in the correct PE.
265*4882a593Smuzhiyun 	 */
266*4882a593Smuzhiyun 	if (!iov)
267*4882a593Smuzhiyun 		return align;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	/*
270*4882a593Smuzhiyun 	 * If we're using single mode then we can just use the native VF BAR
271*4882a593Smuzhiyun 	 * alignment. We validated that it's possible to use a single PE
272*4882a593Smuzhiyun 	 * window above when we did the fixup.
273*4882a593Smuzhiyun 	 */
274*4882a593Smuzhiyun 	if (iov->m64_single_mode[resno - PCI_IOV_RESOURCES])
275*4882a593Smuzhiyun 		return align;
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 	/*
278*4882a593Smuzhiyun 	 * On PowerNV platform, IOV BAR is mapped by M64 BAR to enable the
279*4882a593Smuzhiyun 	 * SR-IOV. While from hardware perspective, the range mapped by M64
280*4882a593Smuzhiyun 	 * BAR should be size aligned.
281*4882a593Smuzhiyun 	 *
282*4882a593Smuzhiyun 	 * This function returns the total IOV BAR size if M64 BAR is in
283*4882a593Smuzhiyun 	 * Shared PE mode or just VF BAR size if not.
284*4882a593Smuzhiyun 	 * If the M64 BAR is in Single PE mode, return the VF BAR size or
285*4882a593Smuzhiyun 	 * M64 segment size if IOV BAR size is less.
286*4882a593Smuzhiyun 	 */
287*4882a593Smuzhiyun 	return phb->ioda.total_pe_num * align;
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun 
pnv_pci_vf_release_m64(struct pci_dev * pdev,u16 num_vfs)290*4882a593Smuzhiyun static int pnv_pci_vf_release_m64(struct pci_dev *pdev, u16 num_vfs)
291*4882a593Smuzhiyun {
292*4882a593Smuzhiyun 	struct pnv_iov_data   *iov;
293*4882a593Smuzhiyun 	struct pnv_phb        *phb;
294*4882a593Smuzhiyun 	int window_id;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	phb = pci_bus_to_pnvhb(pdev->bus);
297*4882a593Smuzhiyun 	iov = pnv_iov_get(pdev);
298*4882a593Smuzhiyun 
299*4882a593Smuzhiyun 	for_each_set_bit(window_id, iov->used_m64_bar_mask, MAX_M64_BARS) {
300*4882a593Smuzhiyun 		opal_pci_phb_mmio_enable(phb->opal_id,
301*4882a593Smuzhiyun 					 OPAL_M64_WINDOW_TYPE,
302*4882a593Smuzhiyun 					 window_id,
303*4882a593Smuzhiyun 					 0);
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 		clear_bit(window_id, &phb->ioda.m64_bar_alloc);
306*4882a593Smuzhiyun 	}
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	return 0;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun /*
313*4882a593Smuzhiyun  * PHB3 and beyond support segmented windows. The window's address range
314*4882a593Smuzhiyun  * is subdivided into phb->ioda.total_pe_num segments and there's a 1-1
315*4882a593Smuzhiyun  * mapping between PEs and segments.
316*4882a593Smuzhiyun  */
pnv_ioda_map_m64_segmented(struct pnv_phb * phb,int window_id,resource_size_t start,resource_size_t size)317*4882a593Smuzhiyun static int64_t pnv_ioda_map_m64_segmented(struct pnv_phb *phb,
318*4882a593Smuzhiyun 					  int window_id,
319*4882a593Smuzhiyun 					  resource_size_t start,
320*4882a593Smuzhiyun 					  resource_size_t size)
321*4882a593Smuzhiyun {
322*4882a593Smuzhiyun 	int64_t rc;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	rc = opal_pci_set_phb_mem_window(phb->opal_id,
325*4882a593Smuzhiyun 					 OPAL_M64_WINDOW_TYPE,
326*4882a593Smuzhiyun 					 window_id,
327*4882a593Smuzhiyun 					 start,
328*4882a593Smuzhiyun 					 0, /* unused */
329*4882a593Smuzhiyun 					 size);
330*4882a593Smuzhiyun 	if (rc)
331*4882a593Smuzhiyun 		goto out;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	rc = opal_pci_phb_mmio_enable(phb->opal_id,
334*4882a593Smuzhiyun 				      OPAL_M64_WINDOW_TYPE,
335*4882a593Smuzhiyun 				      window_id,
336*4882a593Smuzhiyun 				      OPAL_ENABLE_M64_SPLIT);
337*4882a593Smuzhiyun out:
338*4882a593Smuzhiyun 	if (rc)
339*4882a593Smuzhiyun 		pr_err("Failed to map M64 window #%d: %lld\n", window_id, rc);
340*4882a593Smuzhiyun 
341*4882a593Smuzhiyun 	return rc;
342*4882a593Smuzhiyun }
343*4882a593Smuzhiyun 
pnv_ioda_map_m64_single(struct pnv_phb * phb,int pe_num,int window_id,resource_size_t start,resource_size_t size)344*4882a593Smuzhiyun static int64_t pnv_ioda_map_m64_single(struct pnv_phb *phb,
345*4882a593Smuzhiyun 				       int pe_num,
346*4882a593Smuzhiyun 				       int window_id,
347*4882a593Smuzhiyun 				       resource_size_t start,
348*4882a593Smuzhiyun 				       resource_size_t size)
349*4882a593Smuzhiyun {
350*4882a593Smuzhiyun 	int64_t rc;
351*4882a593Smuzhiyun 
352*4882a593Smuzhiyun 	/*
353*4882a593Smuzhiyun 	 * The API for setting up m64 mmio windows seems to have been designed
354*4882a593Smuzhiyun 	 * with P7-IOC in mind. For that chip each M64 BAR (window) had a fixed
355*4882a593Smuzhiyun 	 * split of 8 equally sized segments each of which could individually
356*4882a593Smuzhiyun 	 * assigned to a PE.
357*4882a593Smuzhiyun 	 *
358*4882a593Smuzhiyun 	 * The problem with this is that the API doesn't have any way to
359*4882a593Smuzhiyun 	 * communicate the number of segments we want on a BAR. This wasn't
360*4882a593Smuzhiyun 	 * a problem for p7-ioc since you didn't have a choice, but the
361*4882a593Smuzhiyun 	 * single PE windows added in PHB3 don't map cleanly to this API.
362*4882a593Smuzhiyun 	 *
363*4882a593Smuzhiyun 	 * As a result we've got this slightly awkward process where we
364*4882a593Smuzhiyun 	 * call opal_pci_map_pe_mmio_window() to put the single in single
365*4882a593Smuzhiyun 	 * PE mode, and set the PE for the window before setting the address
366*4882a593Smuzhiyun 	 * bounds. We need to do it this way because the single PE windows
367*4882a593Smuzhiyun 	 * for PHB3 have different alignment requirements on PHB3.
368*4882a593Smuzhiyun 	 */
369*4882a593Smuzhiyun 	rc = opal_pci_map_pe_mmio_window(phb->opal_id,
370*4882a593Smuzhiyun 					 pe_num,
371*4882a593Smuzhiyun 					 OPAL_M64_WINDOW_TYPE,
372*4882a593Smuzhiyun 					 window_id,
373*4882a593Smuzhiyun 					 0);
374*4882a593Smuzhiyun 	if (rc)
375*4882a593Smuzhiyun 		goto out;
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	/*
378*4882a593Smuzhiyun 	 * NB: In single PE mode the window needs to be aligned to 32MB
379*4882a593Smuzhiyun 	 */
380*4882a593Smuzhiyun 	rc = opal_pci_set_phb_mem_window(phb->opal_id,
381*4882a593Smuzhiyun 					 OPAL_M64_WINDOW_TYPE,
382*4882a593Smuzhiyun 					 window_id,
383*4882a593Smuzhiyun 					 start,
384*4882a593Smuzhiyun 					 0, /* ignored by FW, m64 is 1-1 */
385*4882a593Smuzhiyun 					 size);
386*4882a593Smuzhiyun 	if (rc)
387*4882a593Smuzhiyun 		goto out;
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun 	/*
390*4882a593Smuzhiyun 	 * Now actually enable it. We specified the BAR should be in "non-split"
391*4882a593Smuzhiyun 	 * mode so FW will validate that the BAR is in single PE mode.
392*4882a593Smuzhiyun 	 */
393*4882a593Smuzhiyun 	rc = opal_pci_phb_mmio_enable(phb->opal_id,
394*4882a593Smuzhiyun 				      OPAL_M64_WINDOW_TYPE,
395*4882a593Smuzhiyun 				      window_id,
396*4882a593Smuzhiyun 				      OPAL_ENABLE_M64_NON_SPLIT);
397*4882a593Smuzhiyun out:
398*4882a593Smuzhiyun 	if (rc)
399*4882a593Smuzhiyun 		pr_err("Error mapping single PE BAR\n");
400*4882a593Smuzhiyun 
401*4882a593Smuzhiyun 	return rc;
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun 
pnv_pci_alloc_m64_bar(struct pnv_phb * phb,struct pnv_iov_data * iov)404*4882a593Smuzhiyun static int pnv_pci_alloc_m64_bar(struct pnv_phb *phb, struct pnv_iov_data *iov)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun 	int win;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	do {
409*4882a593Smuzhiyun 		win = find_next_zero_bit(&phb->ioda.m64_bar_alloc,
410*4882a593Smuzhiyun 				phb->ioda.m64_bar_idx + 1, 0);
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 		if (win >= phb->ioda.m64_bar_idx + 1)
413*4882a593Smuzhiyun 			return -1;
414*4882a593Smuzhiyun 	} while (test_and_set_bit(win, &phb->ioda.m64_bar_alloc));
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	set_bit(win, iov->used_m64_bar_mask);
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	return win;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun 
pnv_pci_vf_assign_m64(struct pci_dev * pdev,u16 num_vfs)421*4882a593Smuzhiyun static int pnv_pci_vf_assign_m64(struct pci_dev *pdev, u16 num_vfs)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun 	struct pnv_iov_data   *iov;
424*4882a593Smuzhiyun 	struct pnv_phb        *phb;
425*4882a593Smuzhiyun 	int                    win;
426*4882a593Smuzhiyun 	struct resource       *res;
427*4882a593Smuzhiyun 	int                    i, j;
428*4882a593Smuzhiyun 	int64_t                rc;
429*4882a593Smuzhiyun 	resource_size_t        size, start;
430*4882a593Smuzhiyun 	int                    base_pe_num;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	phb = pci_bus_to_pnvhb(pdev->bus);
433*4882a593Smuzhiyun 	iov = pnv_iov_get(pdev);
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
436*4882a593Smuzhiyun 		res = &pdev->resource[i + PCI_IOV_RESOURCES];
437*4882a593Smuzhiyun 		if (!res->flags || !res->parent)
438*4882a593Smuzhiyun 			continue;
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun 		/* don't need single mode? map everything in one go! */
441*4882a593Smuzhiyun 		if (!iov->m64_single_mode[i]) {
442*4882a593Smuzhiyun 			win = pnv_pci_alloc_m64_bar(phb, iov);
443*4882a593Smuzhiyun 			if (win < 0)
444*4882a593Smuzhiyun 				goto m64_failed;
445*4882a593Smuzhiyun 
446*4882a593Smuzhiyun 			size = resource_size(res);
447*4882a593Smuzhiyun 			start = res->start;
448*4882a593Smuzhiyun 
449*4882a593Smuzhiyun 			rc = pnv_ioda_map_m64_segmented(phb, win, start, size);
450*4882a593Smuzhiyun 			if (rc)
451*4882a593Smuzhiyun 				goto m64_failed;
452*4882a593Smuzhiyun 
453*4882a593Smuzhiyun 			continue;
454*4882a593Smuzhiyun 		}
455*4882a593Smuzhiyun 
456*4882a593Smuzhiyun 		/* otherwise map each VF with single PE BARs */
457*4882a593Smuzhiyun 		size = pci_iov_resource_size(pdev, PCI_IOV_RESOURCES + i);
458*4882a593Smuzhiyun 		base_pe_num = iov->vf_pe_arr[0].pe_number;
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 		for (j = 0; j < num_vfs; j++) {
461*4882a593Smuzhiyun 			win = pnv_pci_alloc_m64_bar(phb, iov);
462*4882a593Smuzhiyun 			if (win < 0)
463*4882a593Smuzhiyun 				goto m64_failed;
464*4882a593Smuzhiyun 
465*4882a593Smuzhiyun 			start = res->start + size * j;
466*4882a593Smuzhiyun 			rc = pnv_ioda_map_m64_single(phb, win,
467*4882a593Smuzhiyun 						     base_pe_num + j,
468*4882a593Smuzhiyun 						     start,
469*4882a593Smuzhiyun 						     size);
470*4882a593Smuzhiyun 			if (rc)
471*4882a593Smuzhiyun 				goto m64_failed;
472*4882a593Smuzhiyun 		}
473*4882a593Smuzhiyun 	}
474*4882a593Smuzhiyun 	return 0;
475*4882a593Smuzhiyun 
476*4882a593Smuzhiyun m64_failed:
477*4882a593Smuzhiyun 	pnv_pci_vf_release_m64(pdev, num_vfs);
478*4882a593Smuzhiyun 	return -EBUSY;
479*4882a593Smuzhiyun }
480*4882a593Smuzhiyun 
pnv_ioda_release_vf_PE(struct pci_dev * pdev)481*4882a593Smuzhiyun static void pnv_ioda_release_vf_PE(struct pci_dev *pdev)
482*4882a593Smuzhiyun {
483*4882a593Smuzhiyun 	struct pnv_phb        *phb;
484*4882a593Smuzhiyun 	struct pnv_ioda_pe    *pe, *pe_n;
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	phb = pci_bus_to_pnvhb(pdev->bus);
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	if (!pdev->is_physfn)
489*4882a593Smuzhiyun 		return;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	/* FIXME: Use pnv_ioda_release_pe()? */
492*4882a593Smuzhiyun 	list_for_each_entry_safe(pe, pe_n, &phb->ioda.pe_list, list) {
493*4882a593Smuzhiyun 		if (pe->parent_dev != pdev)
494*4882a593Smuzhiyun 			continue;
495*4882a593Smuzhiyun 
496*4882a593Smuzhiyun 		pnv_pci_ioda2_release_pe_dma(pe);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 		/* Remove from list */
499*4882a593Smuzhiyun 		mutex_lock(&phb->ioda.pe_list_mutex);
500*4882a593Smuzhiyun 		list_del(&pe->list);
501*4882a593Smuzhiyun 		mutex_unlock(&phb->ioda.pe_list_mutex);
502*4882a593Smuzhiyun 
503*4882a593Smuzhiyun 		pnv_ioda_deconfigure_pe(phb, pe);
504*4882a593Smuzhiyun 
505*4882a593Smuzhiyun 		pnv_ioda_free_pe(pe);
506*4882a593Smuzhiyun 	}
507*4882a593Smuzhiyun }
508*4882a593Smuzhiyun 
pnv_pci_vf_resource_shift(struct pci_dev * dev,int offset)509*4882a593Smuzhiyun static int pnv_pci_vf_resource_shift(struct pci_dev *dev, int offset)
510*4882a593Smuzhiyun {
511*4882a593Smuzhiyun 	struct resource *res, res2;
512*4882a593Smuzhiyun 	struct pnv_iov_data *iov;
513*4882a593Smuzhiyun 	resource_size_t size;
514*4882a593Smuzhiyun 	u16 num_vfs;
515*4882a593Smuzhiyun 	int i;
516*4882a593Smuzhiyun 
517*4882a593Smuzhiyun 	if (!dev->is_physfn)
518*4882a593Smuzhiyun 		return -EINVAL;
519*4882a593Smuzhiyun 	iov = pnv_iov_get(dev);
520*4882a593Smuzhiyun 
521*4882a593Smuzhiyun 	/*
522*4882a593Smuzhiyun 	 * "offset" is in VFs.  The M64 windows are sized so that when they
523*4882a593Smuzhiyun 	 * are segmented, each segment is the same size as the IOV BAR.
524*4882a593Smuzhiyun 	 * Each segment is in a separate PE, and the high order bits of the
525*4882a593Smuzhiyun 	 * address are the PE number.  Therefore, each VF's BAR is in a
526*4882a593Smuzhiyun 	 * separate PE, and changing the IOV BAR start address changes the
527*4882a593Smuzhiyun 	 * range of PEs the VFs are in.
528*4882a593Smuzhiyun 	 */
529*4882a593Smuzhiyun 	num_vfs = iov->num_vfs;
530*4882a593Smuzhiyun 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
531*4882a593Smuzhiyun 		res = &dev->resource[i + PCI_IOV_RESOURCES];
532*4882a593Smuzhiyun 		if (!res->flags || !res->parent)
533*4882a593Smuzhiyun 			continue;
534*4882a593Smuzhiyun 		if (iov->m64_single_mode[i])
535*4882a593Smuzhiyun 			continue;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 		/*
538*4882a593Smuzhiyun 		 * The actual IOV BAR range is determined by the start address
539*4882a593Smuzhiyun 		 * and the actual size for num_vfs VFs BAR.  This check is to
540*4882a593Smuzhiyun 		 * make sure that after shifting, the range will not overlap
541*4882a593Smuzhiyun 		 * with another device.
542*4882a593Smuzhiyun 		 */
543*4882a593Smuzhiyun 		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
544*4882a593Smuzhiyun 		res2.flags = res->flags;
545*4882a593Smuzhiyun 		res2.start = res->start + (size * offset);
546*4882a593Smuzhiyun 		res2.end = res2.start + (size * num_vfs) - 1;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		if (res2.end > res->end) {
549*4882a593Smuzhiyun 			dev_err(&dev->dev, "VF BAR%d: %pR would extend past %pR (trying to enable %d VFs shifted by %d)\n",
550*4882a593Smuzhiyun 				i, &res2, res, num_vfs, offset);
551*4882a593Smuzhiyun 			return -EBUSY;
552*4882a593Smuzhiyun 		}
553*4882a593Smuzhiyun 	}
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	/*
556*4882a593Smuzhiyun 	 * Since M64 BAR shares segments among all possible 256 PEs,
557*4882a593Smuzhiyun 	 * we have to shift the beginning of PF IOV BAR to make it start from
558*4882a593Smuzhiyun 	 * the segment which belongs to the PE number assigned to the first VF.
559*4882a593Smuzhiyun 	 * This creates a "hole" in the /proc/iomem which could be used for
560*4882a593Smuzhiyun 	 * allocating other resources so we reserve this area below and
561*4882a593Smuzhiyun 	 * release when IOV is released.
562*4882a593Smuzhiyun 	 */
563*4882a593Smuzhiyun 	for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) {
564*4882a593Smuzhiyun 		res = &dev->resource[i + PCI_IOV_RESOURCES];
565*4882a593Smuzhiyun 		if (!res->flags || !res->parent)
566*4882a593Smuzhiyun 			continue;
567*4882a593Smuzhiyun 		if (iov->m64_single_mode[i])
568*4882a593Smuzhiyun 			continue;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 		size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES);
571*4882a593Smuzhiyun 		res2 = *res;
572*4882a593Smuzhiyun 		res->start += size * offset;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 		dev_info(&dev->dev, "VF BAR%d: %pR shifted to %pR (%sabling %d VFs shifted by %d)\n",
575*4882a593Smuzhiyun 			 i, &res2, res, (offset > 0) ? "En" : "Dis",
576*4882a593Smuzhiyun 			 num_vfs, offset);
577*4882a593Smuzhiyun 
578*4882a593Smuzhiyun 		if (offset < 0) {
579*4882a593Smuzhiyun 			devm_release_resource(&dev->dev, &iov->holes[i]);
580*4882a593Smuzhiyun 			memset(&iov->holes[i], 0, sizeof(iov->holes[i]));
581*4882a593Smuzhiyun 		}
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 		pci_update_resource(dev, i + PCI_IOV_RESOURCES);
584*4882a593Smuzhiyun 
585*4882a593Smuzhiyun 		if (offset > 0) {
586*4882a593Smuzhiyun 			iov->holes[i].start = res2.start;
587*4882a593Smuzhiyun 			iov->holes[i].end = res2.start + size * offset - 1;
588*4882a593Smuzhiyun 			iov->holes[i].flags = IORESOURCE_BUS;
589*4882a593Smuzhiyun 			iov->holes[i].name = "pnv_iov_reserved";
590*4882a593Smuzhiyun 			devm_request_resource(&dev->dev, res->parent,
591*4882a593Smuzhiyun 					&iov->holes[i]);
592*4882a593Smuzhiyun 		}
593*4882a593Smuzhiyun 	}
594*4882a593Smuzhiyun 	return 0;
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun 
pnv_pci_sriov_disable(struct pci_dev * pdev)597*4882a593Smuzhiyun static void pnv_pci_sriov_disable(struct pci_dev *pdev)
598*4882a593Smuzhiyun {
599*4882a593Smuzhiyun 	u16                    num_vfs, base_pe;
600*4882a593Smuzhiyun 	struct pnv_iov_data   *iov;
601*4882a593Smuzhiyun 
602*4882a593Smuzhiyun 	iov = pnv_iov_get(pdev);
603*4882a593Smuzhiyun 	num_vfs = iov->num_vfs;
604*4882a593Smuzhiyun 	base_pe = iov->vf_pe_arr[0].pe_number;
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	if (WARN_ON(!iov))
607*4882a593Smuzhiyun 		return;
608*4882a593Smuzhiyun 
609*4882a593Smuzhiyun 	/* Release VF PEs */
610*4882a593Smuzhiyun 	pnv_ioda_release_vf_PE(pdev);
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	/* Un-shift the IOV BARs if we need to */
613*4882a593Smuzhiyun 	if (iov->need_shift)
614*4882a593Smuzhiyun 		pnv_pci_vf_resource_shift(pdev, -base_pe);
615*4882a593Smuzhiyun 
616*4882a593Smuzhiyun 	/* Release M64 windows */
617*4882a593Smuzhiyun 	pnv_pci_vf_release_m64(pdev, num_vfs);
618*4882a593Smuzhiyun }
619*4882a593Smuzhiyun 
pnv_ioda_setup_vf_PE(struct pci_dev * pdev,u16 num_vfs)620*4882a593Smuzhiyun static void pnv_ioda_setup_vf_PE(struct pci_dev *pdev, u16 num_vfs)
621*4882a593Smuzhiyun {
622*4882a593Smuzhiyun 	struct pnv_phb        *phb;
623*4882a593Smuzhiyun 	struct pnv_ioda_pe    *pe;
624*4882a593Smuzhiyun 	int                    pe_num;
625*4882a593Smuzhiyun 	u16                    vf_index;
626*4882a593Smuzhiyun 	struct pnv_iov_data   *iov;
627*4882a593Smuzhiyun 	struct pci_dn         *pdn;
628*4882a593Smuzhiyun 
629*4882a593Smuzhiyun 	if (!pdev->is_physfn)
630*4882a593Smuzhiyun 		return;
631*4882a593Smuzhiyun 
632*4882a593Smuzhiyun 	phb = pci_bus_to_pnvhb(pdev->bus);
633*4882a593Smuzhiyun 	pdn = pci_get_pdn(pdev);
634*4882a593Smuzhiyun 	iov = pnv_iov_get(pdev);
635*4882a593Smuzhiyun 
636*4882a593Smuzhiyun 	/* Reserve PE for each VF */
637*4882a593Smuzhiyun 	for (vf_index = 0; vf_index < num_vfs; vf_index++) {
638*4882a593Smuzhiyun 		int vf_devfn = pci_iov_virtfn_devfn(pdev, vf_index);
639*4882a593Smuzhiyun 		int vf_bus = pci_iov_virtfn_bus(pdev, vf_index);
640*4882a593Smuzhiyun 		struct pci_dn *vf_pdn;
641*4882a593Smuzhiyun 
642*4882a593Smuzhiyun 		pe = &iov->vf_pe_arr[vf_index];
643*4882a593Smuzhiyun 		pe->phb = phb;
644*4882a593Smuzhiyun 		pe->flags = PNV_IODA_PE_VF;
645*4882a593Smuzhiyun 		pe->pbus = NULL;
646*4882a593Smuzhiyun 		pe->parent_dev = pdev;
647*4882a593Smuzhiyun 		pe->mve_number = -1;
648*4882a593Smuzhiyun 		pe->rid = (vf_bus << 8) | vf_devfn;
649*4882a593Smuzhiyun 
650*4882a593Smuzhiyun 		pe_num = pe->pe_number;
651*4882a593Smuzhiyun 		pe_info(pe, "VF %04d:%02d:%02d.%d associated with PE#%x\n",
652*4882a593Smuzhiyun 			pci_domain_nr(pdev->bus), pdev->bus->number,
653*4882a593Smuzhiyun 			PCI_SLOT(vf_devfn), PCI_FUNC(vf_devfn), pe_num);
654*4882a593Smuzhiyun 
655*4882a593Smuzhiyun 		if (pnv_ioda_configure_pe(phb, pe)) {
656*4882a593Smuzhiyun 			/* XXX What do we do here ? */
657*4882a593Smuzhiyun 			pnv_ioda_free_pe(pe);
658*4882a593Smuzhiyun 			pe->pdev = NULL;
659*4882a593Smuzhiyun 			continue;
660*4882a593Smuzhiyun 		}
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 		/* Put PE to the list */
663*4882a593Smuzhiyun 		mutex_lock(&phb->ioda.pe_list_mutex);
664*4882a593Smuzhiyun 		list_add_tail(&pe->list, &phb->ioda.pe_list);
665*4882a593Smuzhiyun 		mutex_unlock(&phb->ioda.pe_list_mutex);
666*4882a593Smuzhiyun 
667*4882a593Smuzhiyun 		/* associate this pe to it's pdn */
668*4882a593Smuzhiyun 		list_for_each_entry(vf_pdn, &pdn->parent->child_list, list) {
669*4882a593Smuzhiyun 			if (vf_pdn->busno == vf_bus &&
670*4882a593Smuzhiyun 			    vf_pdn->devfn == vf_devfn) {
671*4882a593Smuzhiyun 				vf_pdn->pe_number = pe_num;
672*4882a593Smuzhiyun 				break;
673*4882a593Smuzhiyun 			}
674*4882a593Smuzhiyun 		}
675*4882a593Smuzhiyun 
676*4882a593Smuzhiyun 		pnv_pci_ioda2_setup_dma_pe(phb, pe);
677*4882a593Smuzhiyun 	}
678*4882a593Smuzhiyun }
679*4882a593Smuzhiyun 
pnv_pci_sriov_enable(struct pci_dev * pdev,u16 num_vfs)680*4882a593Smuzhiyun static int pnv_pci_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun 	struct pnv_ioda_pe    *base_pe;
683*4882a593Smuzhiyun 	struct pnv_iov_data   *iov;
684*4882a593Smuzhiyun 	struct pnv_phb        *phb;
685*4882a593Smuzhiyun 	int                    ret;
686*4882a593Smuzhiyun 	u16                    i;
687*4882a593Smuzhiyun 
688*4882a593Smuzhiyun 	phb = pci_bus_to_pnvhb(pdev->bus);
689*4882a593Smuzhiyun 	iov = pnv_iov_get(pdev);
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	/*
692*4882a593Smuzhiyun 	 * There's a calls to IODA2 PE setup code littered throughout. We could
693*4882a593Smuzhiyun 	 * probably fix that, but we'd still have problems due to the
694*4882a593Smuzhiyun 	 * restriction inherent on IODA1 PHBs.
695*4882a593Smuzhiyun 	 *
696*4882a593Smuzhiyun 	 * NB: We class IODA3 as IODA2 since they're very similar.
697*4882a593Smuzhiyun 	 */
698*4882a593Smuzhiyun 	if (phb->type != PNV_PHB_IODA2) {
699*4882a593Smuzhiyun 		pci_err(pdev, "SR-IOV is not supported on this PHB\n");
700*4882a593Smuzhiyun 		return -ENXIO;
701*4882a593Smuzhiyun 	}
702*4882a593Smuzhiyun 
703*4882a593Smuzhiyun 	if (!iov) {
704*4882a593Smuzhiyun 		dev_info(&pdev->dev, "don't support this SRIOV device with non 64bit-prefetchable IOV BAR\n");
705*4882a593Smuzhiyun 		return -ENOSPC;
706*4882a593Smuzhiyun 	}
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun 	/* allocate a contigious block of PEs for our VFs */
709*4882a593Smuzhiyun 	base_pe = pnv_ioda_alloc_pe(phb, num_vfs);
710*4882a593Smuzhiyun 	if (!base_pe) {
711*4882a593Smuzhiyun 		pci_err(pdev, "Unable to allocate PEs for %d VFs\n", num_vfs);
712*4882a593Smuzhiyun 		return -EBUSY;
713*4882a593Smuzhiyun 	}
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 	iov->vf_pe_arr = base_pe;
716*4882a593Smuzhiyun 	iov->num_vfs = num_vfs;
717*4882a593Smuzhiyun 
718*4882a593Smuzhiyun 	/* Assign M64 window accordingly */
719*4882a593Smuzhiyun 	ret = pnv_pci_vf_assign_m64(pdev, num_vfs);
720*4882a593Smuzhiyun 	if (ret) {
721*4882a593Smuzhiyun 		dev_info(&pdev->dev, "Not enough M64 window resources\n");
722*4882a593Smuzhiyun 		goto m64_failed;
723*4882a593Smuzhiyun 	}
724*4882a593Smuzhiyun 
725*4882a593Smuzhiyun 	/*
726*4882a593Smuzhiyun 	 * When using one M64 BAR to map one IOV BAR, we need to shift
727*4882a593Smuzhiyun 	 * the IOV BAR according to the PE# allocated to the VFs.
728*4882a593Smuzhiyun 	 * Otherwise, the PE# for the VF will conflict with others.
729*4882a593Smuzhiyun 	 */
730*4882a593Smuzhiyun 	if (iov->need_shift) {
731*4882a593Smuzhiyun 		ret = pnv_pci_vf_resource_shift(pdev, base_pe->pe_number);
732*4882a593Smuzhiyun 		if (ret)
733*4882a593Smuzhiyun 			goto shift_failed;
734*4882a593Smuzhiyun 	}
735*4882a593Smuzhiyun 
736*4882a593Smuzhiyun 	/* Setup VF PEs */
737*4882a593Smuzhiyun 	pnv_ioda_setup_vf_PE(pdev, num_vfs);
738*4882a593Smuzhiyun 
739*4882a593Smuzhiyun 	return 0;
740*4882a593Smuzhiyun 
741*4882a593Smuzhiyun shift_failed:
742*4882a593Smuzhiyun 	pnv_pci_vf_release_m64(pdev, num_vfs);
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun m64_failed:
745*4882a593Smuzhiyun 	for (i = 0; i < num_vfs; i++)
746*4882a593Smuzhiyun 		pnv_ioda_free_pe(&iov->vf_pe_arr[i]);
747*4882a593Smuzhiyun 
748*4882a593Smuzhiyun 	return ret;
749*4882a593Smuzhiyun }
750*4882a593Smuzhiyun 
pnv_pcibios_sriov_disable(struct pci_dev * pdev)751*4882a593Smuzhiyun int pnv_pcibios_sriov_disable(struct pci_dev *pdev)
752*4882a593Smuzhiyun {
753*4882a593Smuzhiyun 	pnv_pci_sriov_disable(pdev);
754*4882a593Smuzhiyun 
755*4882a593Smuzhiyun 	/* Release PCI data */
756*4882a593Smuzhiyun 	remove_sriov_vf_pdns(pdev);
757*4882a593Smuzhiyun 	return 0;
758*4882a593Smuzhiyun }
759*4882a593Smuzhiyun 
pnv_pcibios_sriov_enable(struct pci_dev * pdev,u16 num_vfs)760*4882a593Smuzhiyun int pnv_pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs)
761*4882a593Smuzhiyun {
762*4882a593Smuzhiyun 	/* Allocate PCI data */
763*4882a593Smuzhiyun 	add_sriov_vf_pdns(pdev);
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	return pnv_pci_sriov_enable(pdev, num_vfs);
766*4882a593Smuzhiyun }
767