Skip to content

Commit 4eb2084

Browse files
floatiouskwilczynski
authored andcommitted
PCI: endpoint: Add pci_epc_bar_size_to_rebar_cap()
Add a helper function to convert a size to the representation used by the Resizable BAR Capability Register. Signed-off-by: Niklas Cassel <[email protected]> Reviewed-by: Manivannan Sadhasivam <[email protected]> Link: https://lore.kernel.org/r/[email protected] [mani: squashed the change that added PCIe spec reference to comments from https://lore.kernel.org/linux-pci/[email protected]] Signed-off-by: Manivannan Sadhasivam <[email protected]> Signed-off-by: Krzysztof Wilczyński <[email protected]>
1 parent 52132f3 commit 4eb2084

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,33 @@ int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
638638
}
639639
EXPORT_SYMBOL_GPL(pci_epc_set_bar);
640640

641+
/**
642+
* pci_epc_bar_size_to_rebar_cap() - convert a size to the representation used
643+
* by the Resizable BAR Capability Register
644+
* @size: the size to convert
645+
* @cap: where to store the result
646+
*
647+
* Returns 0 on success and a negative error code in case of error.
648+
*/
649+
int pci_epc_bar_size_to_rebar_cap(size_t size, u32 *cap)
650+
{
651+
/*
652+
* As per PCIe r6.0, sec 7.8.6.2, min size for a resizable BAR is 1 MB,
653+
* thus disallow a requested BAR size smaller than 1 MB.
654+
* Disallow a requested BAR size larger than 128 TB.
655+
*/
656+
if (size < SZ_1M || (u64)size > (SZ_128G * 1024))
657+
return -EINVAL;
658+
659+
*cap = ilog2(size) - ilog2(SZ_1M);
660+
661+
/* Sizes in REBAR_CAP start at BIT(4). */
662+
*cap = BIT(*cap + 4);
663+
664+
return 0;
665+
}
666+
EXPORT_SYMBOL_GPL(pci_epc_bar_size_to_rebar_cap);
667+
641668
/**
642669
* pci_epc_write_header() - write standard configuration header
643670
* @epc: the EPC device to which the configuration header should be written

include/linux/pci-epc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ void pci_epc_remove_epf(struct pci_epc *epc, struct pci_epf *epf,
275275
enum pci_epc_interface_type type);
276276
int pci_epc_write_header(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
277277
struct pci_epf_header *hdr);
278+
int pci_epc_bar_size_to_rebar_cap(size_t size, u32 *cap);
278279
int pci_epc_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
279280
struct pci_epf_bar *epf_bar);
280281
void pci_epc_clear_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,

0 commit comments

Comments
 (0)