Skip to content

Commit 41f8526

Browse files
committed
powerpc/pseries/iommu: create DDW for devices with DMA mask less than 64-bits
jira LE-4159 Rebuild_History Non-Buildable kernel-5.14.0-570.42.2.el9_6 commit-author Gaurav Batra <gbatra@linux.ibm.com> commit 67dfc11 Starting with PAPR level 2.13, platform supports placing PHB in limited address mode. Devices that support DMA masks less that 64-bit but greater than 32-bits are placed in limited address mode. In this mode, the starting DMA address returned by the DDW is 4GB. When the device driver calls dma_supported, with mask less then 64-bit, the PowerPC IOMMU driver places PHB in the Limited Addressing Mode before creating DDW. Signed-off-by: Gaurav Batra <gbatra@linux.ibm.com> Signed-off-by: Madhavan Srinivasan <maddy@linux.ibm.com> Link: https://patch.msgid.link/20250108164814.73250-1-gbatra@linux.ibm.com (cherry picked from commit 67dfc11) Signed-off-by: Jonathan Maple <jmaple@ciq.com>
1 parent 08cd329 commit 41f8526

File tree

1 file changed

+94
-16
lines changed
  • arch/powerpc/platforms/pseries

1 file changed

+94
-16
lines changed

arch/powerpc/platforms/pseries/iommu.c

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ enum {
5252
enum {
5353
DDW_EXT_SIZE = 0,
5454
DDW_EXT_RESET_DMA_WIN = 1,
55-
DDW_EXT_QUERY_OUT_SIZE = 2
55+
DDW_EXT_QUERY_OUT_SIZE = 2,
56+
DDW_EXT_LIMITED_ADDR_MODE = 3
5657
};
5758

5859
static struct iommu_table *iommu_pseries_alloc_table(int node)
@@ -1330,6 +1331,54 @@ static void reset_dma_window(struct pci_dev *dev, struct device_node *par_dn)
13301331
ret);
13311332
}
13321333

1334+
/*
1335+
* Platforms support placing PHB in limited address mode starting with LoPAR
1336+
* level 2.13 implement. In this mode, the DMA address returned by DDW is over
1337+
* 4GB but, less than 64-bits. This benefits IO adapters that don't support
1338+
* 64-bits for DMA addresses.
1339+
*/
1340+
static int limited_dma_window(struct pci_dev *dev, struct device_node *par_dn)
1341+
{
1342+
int ret;
1343+
u32 cfg_addr, reset_dma_win, las_supported;
1344+
u64 buid;
1345+
struct device_node *dn;
1346+
struct pci_dn *pdn;
1347+
1348+
ret = ddw_read_ext(par_dn, DDW_EXT_RESET_DMA_WIN, &reset_dma_win);
1349+
if (ret)
1350+
goto out;
1351+
1352+
ret = ddw_read_ext(par_dn, DDW_EXT_LIMITED_ADDR_MODE, &las_supported);
1353+
1354+
/* Limited Address Space extension available on the platform but DDW in
1355+
* limited addressing mode not supported
1356+
*/
1357+
if (!ret && !las_supported)
1358+
ret = -EPROTO;
1359+
1360+
if (ret) {
1361+
dev_info(&dev->dev, "Limited Address Space for DDW not Supported, err: %d", ret);
1362+
goto out;
1363+
}
1364+
1365+
dn = pci_device_to_OF_node(dev);
1366+
pdn = PCI_DN(dn);
1367+
buid = pdn->phb->buid;
1368+
cfg_addr = (pdn->busno << 16) | (pdn->devfn << 8);
1369+
1370+
ret = rtas_call(reset_dma_win, 4, 1, NULL, cfg_addr, BUID_HI(buid),
1371+
BUID_LO(buid), 1);
1372+
if (ret)
1373+
dev_info(&dev->dev,
1374+
"ibm,reset-pe-dma-windows(%x) for Limited Addr Support: %x %x %x returned %d ",
1375+
reset_dma_win, cfg_addr, BUID_HI(buid), BUID_LO(buid),
1376+
ret);
1377+
1378+
out:
1379+
return ret;
1380+
}
1381+
13331382
/* Return largest page shift based on "IO Page Sizes" output of ibm,query-pe-dma-window. */
13341383
static int iommu_get_page_shift(u32 query_page_size)
13351384
{
@@ -1397,7 +1446,7 @@ static struct property *ddw_property_create(const char *propname, u32 liobn, u64
13971446
*
13981447
* returns true if can map all pages (direct mapping), false otherwise..
13991448
*/
1400-
static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
1449+
static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn, u64 dma_mask)
14011450
{
14021451
int len = 0, ret;
14031452
int max_ram_len = order_base_2(ddw_memory_hotplug_max());
@@ -1416,6 +1465,9 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
14161465
bool pmem_present;
14171466
struct pci_dn *pci = PCI_DN(pdn);
14181467
struct property *default_win = NULL;
1468+
bool limited_addr_req = false, limited_addr_enabled = false;
1469+
int dev_max_ddw;
1470+
int ddw_sz;
14191471

14201472
dn = of_find_node_by_type(NULL, "ibm,pmemory");
14211473
pmem_present = dn != NULL;
@@ -1442,7 +1494,6 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
14421494
* the ibm,ddw-applicable property holds the tokens for:
14431495
* ibm,query-pe-dma-window
14441496
* ibm,create-pe-dma-window
1445-
* ibm,remove-pe-dma-window
14461497
* for the given node in that order.
14471498
* the property is actually in the parent, not the PE
14481499
*/
@@ -1462,6 +1513,20 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
14621513
if (ret != 0)
14631514
goto out_failed;
14641515

1516+
/* DMA Limited Addressing required? This is when the driver has
1517+
* requested to create DDW but supports mask which is less than 64-bits
1518+
*/
1519+
limited_addr_req = (dma_mask != DMA_BIT_MASK(64));
1520+
1521+
/* place the PHB in Limited Addressing mode */
1522+
if (limited_addr_req) {
1523+
if (limited_dma_window(dev, pdn))
1524+
goto out_failed;
1525+
1526+
/* PHB is in Limited address mode */
1527+
limited_addr_enabled = true;
1528+
}
1529+
14651530
/*
14661531
* If there is no window available, remove the default DMA window,
14671532
* if it's present. This will make all the resources available to the
@@ -1508,6 +1573,15 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
15081573
goto out_failed;
15091574
}
15101575

1576+
/* Maximum DMA window size that the device can address (in log2) */
1577+
dev_max_ddw = fls64(dma_mask);
1578+
1579+
/* If the device DMA mask is less than 64-bits, make sure the DMA window
1580+
* size is not bigger than what the device can access
1581+
*/
1582+
ddw_sz = min(order_base_2(query.largest_available_block << page_shift),
1583+
dev_max_ddw);
1584+
15111585
/*
15121586
* The "ibm,pmemory" can appear anywhere in the address space.
15131587
* Assuming it is still backed by page structs, try MAX_PHYSMEM_BITS
@@ -1516,23 +1590,21 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
15161590
*/
15171591
len = max_ram_len;
15181592
if (pmem_present) {
1519-
if (query.largest_available_block >=
1520-
(1ULL << (MAX_PHYSMEM_BITS - page_shift)))
1593+
if (ddw_sz >= MAX_PHYSMEM_BITS)
15211594
len = MAX_PHYSMEM_BITS;
15221595
else
15231596
dev_info(&dev->dev, "Skipping ibm,pmemory");
15241597
}
15251598

15261599
/* check if the available block * number of ptes will map everything */
1527-
if (query.largest_available_block < (1ULL << (len - page_shift))) {
1600+
if (ddw_sz < len) {
15281601
dev_dbg(&dev->dev,
15291602
"can't map partition max 0x%llx with %llu %llu-sized pages\n",
15301603
1ULL << len,
15311604
query.largest_available_block,
15321605
1ULL << page_shift);
15331606

1534-
len = order_base_2(query.largest_available_block << page_shift);
1535-
1607+
len = ddw_sz;
15361608
dynamic_mapping = true;
15371609
} else {
15381610
direct_mapping = !default_win_removed ||
@@ -1546,8 +1618,9 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
15461618
*/
15471619
if (default_win_removed && pmem_present && !direct_mapping) {
15481620
/* DDW is big enough to be split */
1549-
if ((query.largest_available_block << page_shift) >=
1550-
MIN_DDW_VPMEM_DMA_WINDOW + (1ULL << max_ram_len)) {
1621+
if ((1ULL << ddw_sz) >=
1622+
MIN_DDW_VPMEM_DMA_WINDOW + (1ULL << max_ram_len)) {
1623+
15511624
direct_mapping = true;
15521625

15531626
/* offset of the Dynamic part of DDW */
@@ -1558,8 +1631,7 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
15581631
dynamic_mapping = true;
15591632

15601633
/* create max size DDW possible */
1561-
len = order_base_2(query.largest_available_block
1562-
<< page_shift);
1634+
len = ddw_sz;
15631635
}
15641636
}
15651637

@@ -1687,7 +1759,7 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
16871759
__remove_dma_window(pdn, ddw_avail, create.liobn);
16881760

16891761
out_failed:
1690-
if (default_win_removed)
1762+
if (default_win_removed || limited_addr_enabled)
16911763
reset_dma_window(dev, pdn);
16921764

16931765
fpdn = kzalloc(sizeof(*fpdn), GFP_KERNEL);
@@ -1706,6 +1778,9 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)
17061778
dev->dev.bus_dma_limit = dev->dev.archdata.dma_offset +
17071779
(1ULL << max_ram_len);
17081780

1781+
dev_info(&dev->dev, "lsa_required: %x, lsa_enabled: %x, direct mapping: %x\n",
1782+
limited_addr_req, limited_addr_enabled, direct_mapping);
1783+
17091784
return direct_mapping;
17101785
}
17111786

@@ -1831,8 +1906,11 @@ static bool iommu_bypass_supported_pSeriesLP(struct pci_dev *pdev, u64 dma_mask)
18311906
{
18321907
struct device_node *dn = pci_device_to_OF_node(pdev), *pdn;
18331908

1834-
/* only attempt to use a new window if 64-bit DMA is requested */
1835-
if (dma_mask < DMA_BIT_MASK(64))
1909+
/* For DDW, DMA mask should be more than 32-bits. For mask more then
1910+
* 32-bits but less then 64-bits, DMA addressing is supported in
1911+
* Limited Addressing mode.
1912+
*/
1913+
if (dma_mask <= DMA_BIT_MASK(32))
18361914
return false;
18371915

18381916
dev_dbg(&pdev->dev, "node is %pOF\n", dn);
@@ -1845,7 +1923,7 @@ static bool iommu_bypass_supported_pSeriesLP(struct pci_dev *pdev, u64 dma_mask)
18451923
*/
18461924
pdn = pci_dma_find(dn, NULL);
18471925
if (pdn && PCI_DN(pdn))
1848-
return enable_ddw(pdev, pdn);
1926+
return enable_ddw(pdev, pdn, dma_mask);
18491927

18501928
return false;
18511929
}

0 commit comments

Comments
 (0)