Skip to content

Commit fa8fef0

Browse files
kishonbjorn-helgaas
authored andcommitted
PCI: endpoint: Add helper API to get the 'next' unreserved BAR
Add an API to get the next unreserved BAR starting from a given BAR number that can be used by the endpoint function. Link: https://lore.kernel.org/r/20210201195809.7342-4-kishon@ti.com Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com> Signed-off-by: Lorenzo Pieralisi <lorenzo.pieralisi@arm.com> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent 959a48d commit fa8fef0

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

drivers/pci/endpoint/pci-epc-core.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,50 @@ EXPORT_SYMBOL_GPL(pci_epc_get);
8787
* pci_epc_get_first_free_bar() - helper to get first unreserved BAR
8888
* @epc_features: pci_epc_features structure that holds the reserved bar bitmap
8989
*
90-
* Invoke to get the first unreserved BAR that can be used for endpoint
90+
* Invoke to get the first unreserved BAR that can be used by the endpoint
9191
* function. For any incorrect value in reserved_bar return '0'.
9292
*/
9393
unsigned int pci_epc_get_first_free_bar(const struct pci_epc_features
9494
*epc_features)
95+
{
96+
return pci_epc_get_next_free_bar(epc_features, BAR_0);
97+
}
98+
EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
99+
100+
/**
101+
* pci_epc_get_next_free_bar() - helper to get unreserved BAR starting from @bar
102+
* @epc_features: pci_epc_features structure that holds the reserved bar bitmap
103+
* @bar: the starting BAR number from where unreserved BAR should be searched
104+
*
105+
* Invoke to get the next unreserved BAR starting from @bar that can be used
106+
* for endpoint function. For any incorrect value in reserved_bar return '0'.
107+
*/
108+
unsigned int pci_epc_get_next_free_bar(const struct pci_epc_features
109+
*epc_features, enum pci_barno bar)
95110
{
96111
unsigned long free_bar;
97112

98113
if (!epc_features)
99114
return 0;
100115

116+
/* If 'bar - 1' is a 64-bit BAR, move to the next BAR */
117+
if ((epc_features->bar_fixed_64bit << 1) & 1 << bar)
118+
bar++;
119+
101120
/* Find if the reserved BAR is also a 64-bit BAR */
102121
free_bar = epc_features->reserved_bar & epc_features->bar_fixed_64bit;
103122

104123
/* Set the adjacent bit if the reserved BAR is also a 64-bit BAR */
105124
free_bar <<= 1;
106125
free_bar |= epc_features->reserved_bar;
107126

108-
/* Now find the free BAR */
109-
free_bar = ffz(free_bar);
127+
free_bar = find_next_zero_bit(&free_bar, 6, bar);
110128
if (free_bar > 5)
111129
return 0;
112130

113131
return free_bar;
114132
}
115-
EXPORT_SYMBOL_GPL(pci_epc_get_first_free_bar);
133+
EXPORT_SYMBOL_GPL(pci_epc_get_next_free_bar);
116134

117135
/**
118136
* pci_epc_get_features() - get the features supported by EPC

include/linux/pci-epc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ const struct pci_epc_features *pci_epc_get_features(struct pci_epc *epc,
203203
u8 func_no);
204204
unsigned int pci_epc_get_first_free_bar(const struct pci_epc_features
205205
*epc_features);
206+
unsigned int pci_epc_get_next_free_bar(const struct pci_epc_features
207+
*epc_features, enum pci_barno bar);
206208
struct pci_epc *pci_epc_get(const char *epc_name);
207209
void pci_epc_put(struct pci_epc *epc);
208210

0 commit comments

Comments
 (0)