Skip to content

Commit fc8cba8

Browse files
LeoBrasmpe
authored andcommitted
powerpc/pseries/iommu: Reorganize iommu_table_setparms*() with new helper
Add a new helper _iommu_table_setparms(), and use it in iommu_table_setparms() and iommu_table_setparms_lpar() to avoid duplicated code. Also, setting tbl->it_ops was happening outsite iommu_table_setparms*(), so move it to the new helper. Since we need the iommu_table_ops to be declared before used, declare iommu_table_lpar_multi_ops and iommu_table_pseries_ops to before their respective iommu_table_setparms*(). Signed-off-by: Leonardo Bras <[email protected]> Reviewed-by: Frederic Barrat <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 7ed2ed2 commit fc8cba8

File tree

1 file changed

+38
-34
lines changed
  • arch/powerpc/platforms/pseries

1 file changed

+38
-34
lines changed

arch/powerpc/platforms/pseries/iommu.c

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,24 @@ static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,
501501
return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
502502
}
503503

504+
static void iommu_table_setparms_common(struct iommu_table *tbl, unsigned long busno,
505+
unsigned long liobn, unsigned long win_addr,
506+
unsigned long window_size, unsigned long page_shift,
507+
void *base, struct iommu_table_ops *table_ops)
508+
{
509+
tbl->it_busno = busno;
510+
tbl->it_index = liobn;
511+
tbl->it_offset = win_addr >> page_shift;
512+
tbl->it_size = window_size >> page_shift;
513+
tbl->it_page_shift = page_shift;
514+
tbl->it_base = (unsigned long)base;
515+
tbl->it_blocksize = 16;
516+
tbl->it_type = TCE_PCI;
517+
tbl->it_ops = table_ops;
518+
}
519+
520+
struct iommu_table_ops iommu_table_pseries_ops;
521+
504522
static void iommu_table_setparms(struct pci_controller *phb,
505523
struct device_node *dn,
506524
struct iommu_table *tbl)
@@ -509,8 +527,13 @@ static void iommu_table_setparms(struct pci_controller *phb,
509527
const unsigned long *basep;
510528
const u32 *sizep;
511529

512-
node = phb->dn;
530+
/* Test if we are going over 2GB of DMA space */
531+
if (phb->dma_window_base_cur + phb->dma_window_size > SZ_2G) {
532+
udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
533+
panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
534+
}
513535

536+
node = phb->dn;
514537
basep = of_get_property(node, "linux,tce-base", NULL);
515538
sizep = of_get_property(node, "linux,tce-size", NULL);
516539
if (basep == NULL || sizep == NULL) {
@@ -519,33 +542,18 @@ static void iommu_table_setparms(struct pci_controller *phb,
519542
return;
520543
}
521544

522-
tbl->it_base = (unsigned long)__va(*basep);
545+
iommu_table_setparms_common(tbl, phb->bus->number, 0, phb->dma_window_base_cur,
546+
phb->dma_window_size, IOMMU_PAGE_SHIFT_4K,
547+
__va(*basep), &iommu_table_pseries_ops);
523548

524549
if (!is_kdump_kernel())
525550
memset((void *)tbl->it_base, 0, *sizep);
526551

527-
tbl->it_busno = phb->bus->number;
528-
tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
529-
530-
/* Units of tce entries */
531-
tbl->it_offset = phb->dma_window_base_cur >> tbl->it_page_shift;
532-
533-
/* Test if we are going over 2GB of DMA space */
534-
if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
535-
udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
536-
panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");
537-
}
538-
539552
phb->dma_window_base_cur += phb->dma_window_size;
540-
541-
/* Set the tce table size - measured in entries */
542-
tbl->it_size = phb->dma_window_size >> tbl->it_page_shift;
543-
544-
tbl->it_index = 0;
545-
tbl->it_blocksize = 16;
546-
tbl->it_type = TCE_PCI;
547553
}
548554

555+
struct iommu_table_ops iommu_table_lpar_multi_ops;
556+
549557
/*
550558
* iommu_table_setparms_lpar
551559
*
@@ -557,17 +565,13 @@ static void iommu_table_setparms_lpar(struct pci_controller *phb,
557565
struct iommu_table_group *table_group,
558566
const __be32 *dma_window)
559567
{
560-
unsigned long offset, size;
568+
unsigned long offset, size, liobn;
561569

562-
of_parse_dma_window(dn, dma_window, &tbl->it_index, &offset, &size);
570+
of_parse_dma_window(dn, dma_window, &liobn, &offset, &size);
571+
572+
iommu_table_setparms_common(tbl, phb->bus->number, liobn, offset, size, IOMMU_PAGE_SHIFT_4K, NULL,
573+
&iommu_table_lpar_multi_ops);
563574

564-
tbl->it_busno = phb->bus->number;
565-
tbl->it_page_shift = IOMMU_PAGE_SHIFT_4K;
566-
tbl->it_base = 0;
567-
tbl->it_blocksize = 16;
568-
tbl->it_type = TCE_PCI;
569-
tbl->it_offset = offset >> tbl->it_page_shift;
570-
tbl->it_size = size >> tbl->it_page_shift;
571575

572576
table_group->tce32_start = offset;
573577
table_group->tce32_size = size;
@@ -647,7 +651,7 @@ static void pci_dma_bus_setup_pSeries(struct pci_bus *bus)
647651
tbl = pci->table_group->tables[0];
648652

649653
iommu_table_setparms(pci->phb, dn, tbl);
650-
tbl->it_ops = &iommu_table_pseries_ops;
654+
651655
if (!iommu_init_table(tbl, pci->phb->node, 0, 0))
652656
panic("Failed to initialize iommu table");
653657

@@ -730,7 +734,7 @@ static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)
730734
tbl = ppci->table_group->tables[0];
731735
iommu_table_setparms_lpar(ppci->phb, pdn, tbl,
732736
ppci->table_group, dma_window);
733-
tbl->it_ops = &iommu_table_lpar_multi_ops;
737+
734738
if (!iommu_init_table(tbl, ppci->phb->node, 0, 0))
735739
panic("Failed to initialize iommu table");
736740
iommu_register_group(ppci->table_group,
@@ -760,7 +764,7 @@ static void pci_dma_dev_setup_pSeries(struct pci_dev *dev)
760764
PCI_DN(dn)->table_group = iommu_pseries_alloc_group(phb->node);
761765
tbl = PCI_DN(dn)->table_group->tables[0];
762766
iommu_table_setparms(phb, dn, tbl);
763-
tbl->it_ops = &iommu_table_pseries_ops;
767+
764768
if (!iommu_init_table(tbl, phb->node, 0, 0))
765769
panic("Failed to initialize iommu table");
766770

@@ -1461,7 +1465,7 @@ static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
14611465
tbl = pci->table_group->tables[0];
14621466
iommu_table_setparms_lpar(pci->phb, pdn, tbl,
14631467
pci->table_group, dma_window);
1464-
tbl->it_ops = &iommu_table_lpar_multi_ops;
1468+
14651469
iommu_init_table(tbl, pci->phb->node, 0, 0);
14661470
iommu_register_group(pci->table_group,
14671471
pci_domain_nr(pci->phb->bus), 0);

0 commit comments

Comments
 (0)