Skip to content

Commit 9456480

Browse files
damien-lemoalbjorn-helgaas
authored andcommitted
PCI: rockchip-ep: Refactor rockchip_pcie_ep_probe() memory allocations
Introduce the function rockchip_pcie_ep_init_ob_mem() allocate the outbound memory regions and memory needed for IRQ handling. These changes tidy up rockchip_pcie_ep_probe(). No functional change. Link: https://lore.kernel.org/r/20241017015849.190271-9-dlemoal@kernel.org Signed-off-by: Damien Le Moal <dlemoal@kernel.org> Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org> Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
1 parent 2968534 commit 9456480

File tree

1 file changed

+61
-46
lines changed

1 file changed

+61
-46
lines changed

drivers/pci/controller/pcie-rockchip-ep.c

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -532,15 +532,66 @@ static const struct of_device_id rockchip_pcie_ep_of_match[] = {
532532
{},
533533
};
534534

535+
static int rockchip_pcie_ep_init_ob_mem(struct rockchip_pcie_ep *ep)
536+
{
537+
struct rockchip_pcie *rockchip = &ep->rockchip;
538+
struct device *dev = rockchip->dev;
539+
struct pci_epc_mem_window *windows = NULL;
540+
int err, i;
541+
542+
ep->ob_addr = devm_kcalloc(dev, ep->max_regions, sizeof(*ep->ob_addr),
543+
GFP_KERNEL);
544+
545+
if (!ep->ob_addr)
546+
return -ENOMEM;
547+
548+
windows = devm_kcalloc(dev, ep->max_regions,
549+
sizeof(struct pci_epc_mem_window), GFP_KERNEL);
550+
if (!windows)
551+
return -ENOMEM;
552+
553+
for (i = 0; i < ep->max_regions; i++) {
554+
windows[i].phys_base = rockchip->mem_res->start + (SZ_1M * i);
555+
windows[i].size = SZ_1M;
556+
windows[i].page_size = SZ_1M;
557+
}
558+
err = pci_epc_multi_mem_init(ep->epc, windows, ep->max_regions);
559+
devm_kfree(dev, windows);
560+
561+
if (err < 0) {
562+
dev_err(dev, "failed to initialize the memory space\n");
563+
return err;
564+
}
565+
566+
ep->irq_cpu_addr = pci_epc_mem_alloc_addr(ep->epc, &ep->irq_phys_addr,
567+
SZ_1M);
568+
if (!ep->irq_cpu_addr) {
569+
dev_err(dev, "failed to reserve memory space for MSI\n");
570+
goto err_epc_mem_exit;
571+
}
572+
573+
ep->irq_pci_addr = ROCKCHIP_PCIE_EP_DUMMY_IRQ_ADDR;
574+
575+
return 0;
576+
577+
err_epc_mem_exit:
578+
pci_epc_mem_exit(ep->epc);
579+
580+
return err;
581+
}
582+
583+
static void rockchip_pcie_ep_exit_ob_mem(struct rockchip_pcie_ep *ep)
584+
{
585+
pci_epc_mem_exit(ep->epc);
586+
}
587+
535588
static int rockchip_pcie_ep_probe(struct platform_device *pdev)
536589
{
537590
struct device *dev = &pdev->dev;
538591
struct rockchip_pcie_ep *ep;
539592
struct rockchip_pcie *rockchip;
540593
struct pci_epc *epc;
541-
size_t max_regions;
542-
struct pci_epc_mem_window *windows = NULL;
543-
int err, i;
594+
int err;
544595
u32 cfg_msi, cfg_msix_cp;
545596

546597
ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL);
@@ -564,10 +615,14 @@ static int rockchip_pcie_ep_probe(struct platform_device *pdev)
564615
if (err)
565616
return err;
566617

567-
err = rockchip_pcie_enable_clocks(rockchip);
618+
err = rockchip_pcie_ep_init_ob_mem(ep);
568619
if (err)
569620
return err;
570621

622+
err = rockchip_pcie_enable_clocks(rockchip);
623+
if (err)
624+
goto err_exit_ob_mem;
625+
571626
err = rockchip_pcie_init_port(rockchip);
572627
if (err)
573628
goto err_disable_clocks;
@@ -576,47 +631,9 @@ static int rockchip_pcie_ep_probe(struct platform_device *pdev)
576631
rockchip_pcie_write(rockchip, PCIE_CLIENT_LINK_TRAIN_ENABLE,
577632
PCIE_CLIENT_CONFIG);
578633

579-
max_regions = ep->max_regions;
580-
ep->ob_addr = devm_kcalloc(dev, max_regions, sizeof(*ep->ob_addr),
581-
GFP_KERNEL);
582-
583-
if (!ep->ob_addr) {
584-
err = -ENOMEM;
585-
goto err_uninit_port;
586-
}
587-
588634
/* Only enable function 0 by default */
589635
rockchip_pcie_write(rockchip, BIT(0), PCIE_CORE_PHY_FUNC_CFG);
590636

591-
windows = devm_kcalloc(dev, ep->max_regions,
592-
sizeof(struct pci_epc_mem_window), GFP_KERNEL);
593-
if (!windows) {
594-
err = -ENOMEM;
595-
goto err_uninit_port;
596-
}
597-
for (i = 0; i < ep->max_regions; i++) {
598-
windows[i].phys_base = rockchip->mem_res->start + (SZ_1M * i);
599-
windows[i].size = SZ_1M;
600-
windows[i].page_size = SZ_1M;
601-
}
602-
err = pci_epc_multi_mem_init(epc, windows, ep->max_regions);
603-
devm_kfree(dev, windows);
604-
605-
if (err < 0) {
606-
dev_err(dev, "failed to initialize the memory space\n");
607-
goto err_uninit_port;
608-
}
609-
610-
ep->irq_cpu_addr = pci_epc_mem_alloc_addr(epc, &ep->irq_phys_addr,
611-
SZ_1M);
612-
if (!ep->irq_cpu_addr) {
613-
dev_err(dev, "failed to reserve memory space for MSI\n");
614-
err = -ENOMEM;
615-
goto err_epc_mem_exit;
616-
}
617-
618-
ep->irq_pci_addr = ROCKCHIP_PCIE_EP_DUMMY_IRQ_ADDR;
619-
620637
/*
621638
* MSI-X is not supported but the controller still advertises the MSI-X
622639
* capability by default, which can lead to the Root Complex side
@@ -646,10 +663,8 @@ static int rockchip_pcie_ep_probe(struct platform_device *pdev)
646663
pci_epc_init_notify(epc);
647664

648665
return 0;
649-
err_epc_mem_exit:
650-
pci_epc_mem_exit(epc);
651-
err_uninit_port:
652-
rockchip_pcie_deinit_phys(rockchip);
666+
err_exit_ob_mem:
667+
rockchip_pcie_ep_exit_ob_mem(ep);
653668
err_disable_clocks:
654669
rockchip_pcie_disable_clocks(rockchip);
655670
return err;

0 commit comments

Comments
 (0)