From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:00
So far it's assumed possible to map the guest RAM 1:1 to the bus, which
works with a small number of devices. SRIOV changes it as the user can
configure hundreds VFs and since phyp preallocates TCEs and does not
allow IOMMU pages bigger than 64K, it has to limit the number of TCEs
per a PE to limit waste of physical pages.
As of today, if the assumed direct mapping is not possible, DDW creation
is skipped and the default DMA window "ibm,dma-window" is used instead.
Using the DDW instead of the default DMA window may allow to expand the
amount of memory that can be DMA-mapped, given the number of pages (TCEs)
may stay the same (or increase) and the default DMA window offers only
4k-pages while DDW may offer larger pages (4k, 64k, 16M ...).
Patch #1 replaces hard-coded 4K page size with a variable containing the
correct page size for the window.
Patch #2 introduces iommu_table_in_use(), and replace manual bit-field
checking where it's used. It will be used for aborting enable_ddw() if
there is any current iommu allocation and we are trying single window
indirect mapping.
Patch #3 introduces iommu_pseries_alloc_table() that will be helpful
when indirect mapping needs to replace the iommu_table.
Patch #4 adds helpers for adding DDWs in the list.
Patch #5 refactors enable_ddw() so it returns if direct mapping is
possible, instead of DMA offset. It helps for next patches on
indirect DMA mapping and also allows DMA windows starting at 0x00.
Patch #6 bring new helper to simplify enable_ddw(), allowing
some reorganization for introducing indirect mapping DDW.
Patch #7 adds new helper _iommu_table_setparms() and use it in other
*setparams*() to fill iommu_table. It will also be used for creating a
new iommu_table for indirect mapping.
Patch #8 updates remove_dma_window() to accept different property names,
so we can introduce a new property for indirect mapping.
Patch #9 extracts find_existing_ddw_windows() into
find_existing_ddw_windows_named(), and calls it by it's property name.
This will be useful when the property for indirect mapping is created,
so we can search the device-tree for both properties.
Patch #10:
Instead of destroying the created DDW if it doesn't map the whole
partition, make use of it instead of the default DMA window as it improves
performance. Also, update the iommu_table and re-generate the pools.
It introduces a new property name for DDW with indirect DMA mapping.
Patch #11:
Does some renaming of 'direct window' to 'dma window', given the DDW
created can now be also used in indirect mapping if direct mapping is not
available.
All patches were tested into an LPAR with an virtio-net interface that
allows default DMA window and DDW to coexist.
Changes since v4:
- Solve conflicts with new upstream versions
- Avoid unecessary code moving by doing variable declaration before definition
- Rename _iommu_table_setparms to iommu_table_setparms_common and changed base
parameter from unsigned long to void* in order to avoid unecessary casting.
- Fix breaking case for existing direct-mapping.
- Fix IORESOURCE_MEM bound issue
- Move new tbl to pci->table_group->tables[1] instead of replacing [0]
v4 Link: https://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=241597&state=%2A&archive=both
Changes since v3:
- Fixed inverted free order at ddw_property_create()
- Updated goto tag naming
v3 Link: https://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=240287&state=%2A&archive=both
Changes since v2:
- Some patches got removed from the series and sent by themselves,
- New tbl created for DDW + indirect mapping reserves MMIO32 space,
- Improved reserved area algorithm,
- Improved commit messages,
- Removed define for default DMA window prop name,
- Avoided some unnecessary renaming,
- Removed some unnecessary empty lines,
- Changed some code moving to forward declarations.
v2 Link: http://patchwork.ozlabs.org/project/linuxppc-dev/list/?series=201210&state=%2A&archive=both
Leonardo Bras (11):
powerpc/pseries/iommu: Replace hard-coded page shift
powerpc/kernel/iommu: Add new iommu_table_in_use() helper
powerpc/pseries/iommu: Add iommu_pseries_alloc_table() helper
powerpc/pseries/iommu: Add ddw_list_new_entry() helper
powerpc/pseries/iommu: Allow DDW windows starting at 0x00
powerpc/pseries/iommu: Add ddw_property_create() and refactor
enable_ddw()
powerpc/pseries/iommu: Reorganize iommu_table_setparms*() with new
helper
powerpc/pseries/iommu: Update remove_dma_window() to accept property
name
powerpc/pseries/iommu: Find existing DDW with given property name
powerpc/pseries/iommu: Make use of DDW for indirect mapping
powerpc/pseries/iommu: Rename "direct window" to "dma window"
arch/powerpc/include/asm/iommu.h | 1 +
arch/powerpc/include/asm/tce.h | 8 -
arch/powerpc/kernel/iommu.c | 65 ++--
arch/powerpc/platforms/pseries/iommu.c | 481 +++++++++++++++----------
4 files changed, 330 insertions(+), 225 deletions(-)
--
2.32.0
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:03
Some functions assume IOMMU page size can only be 4K (pageshift == 12).
Update them to accept any page size passed, so we can use 64K pages.
In the process, some defines like TCE_SHIFT were made obsolete, and then
removed.
IODA3 Revision 3.0_prd1 (OpenPowerFoundation), Figures 3.4 and 3.5 show
a RPN of 52-bit, and considers a 12-bit pageshift, so there should be
no need of using TCE_RPN_MASK, which masks out any bit after 40 in rpn.
It's usage removed from tce_build_pSeries(), tce_build_pSeriesLP(), and
tce_buildmulti_pSeriesLP().
Most places had a tbl struct, so using tbl->it_page_shift was simple.
tce_free_pSeriesLP() was a special case, since callers not always have a
tbl struct, so adding a tceshift parameter seems the right thing to do.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/include/asm/tce.h | 8 ------
arch/powerpc/platforms/pseries/iommu.c | 39 +++++++++++++++-----------
2 files changed, 23 insertions(+), 24 deletions(-)
@@ -107,6 +107,8 @@ static int tce_build_pSeries(struct iommu_table *tbl, long index,u64proto_tce;__be64*tcep;u64rpn;+constunsignedlongtceshift=tbl->it_page_shift;+constunsignedlongpagesize=IOMMU_PAGE_SIZE(tbl);proto_tce=TCE_PCI_READ;// Read allowed
@@ -117,10 +119,10 @@ static int tce_build_pSeries(struct iommu_table *tbl, long index,while(npages--){/* can't move this out since we might cross MEMBLOCK boundary */-rpn=__pa(uaddr)>>TCE_SHIFT;-*tcep=cpu_to_be64(proto_tce|(rpn&TCE_RPN_MASK)<<TCE_RPN_SHIFT);+rpn=__pa(uaddr)>>tceshift;+*tcep=cpu_to_be64(proto_tce|rpn<<tceshift);-uaddr+=TCE_PAGE_SIZE;+uaddr+=pagesize;tcep++;}return0;
@@ -146,7 +148,7 @@ static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)returnbe64_to_cpu(*tcep);}-staticvoidtce_free_pSeriesLP(unsignedlongliobn,long,long);+staticvoidtce_free_pSeriesLP(unsignedlongliobn,long,long,long);staticvoidtce_freemulti_pSeriesLP(structiommu_table*,long,long);staticinttce_build_pSeriesLP(unsignedlongliobn,longtcenum,longtceshift,
@@ -166,12 +168,12 @@ static int tce_build_pSeriesLP(unsigned long liobn, long tcenum, long tceshift,proto_tce|=TCE_PCI_WRITE;while(npages--){-tce=proto_tce|(rpn&TCE_RPN_MASK)<<tceshift;+tce=proto_tce|rpn<<tceshift;rc=plpar_tce_put((u64)liobn,(u64)tcenum<<tceshift,tce);if(unlikely(rc==H_NOT_ENOUGH_RESOURCES)){ret=(int)rc;-tce_free_pSeriesLP(liobn,tcenum_start,+tce_free_pSeriesLP(liobn,tcenum_start,tceshift,(npages_start-(npages+1)));break;}
@@ -205,10 +207,11 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,longtcenum_start=tcenum,npages_start=npages;intret=0;unsignedlongflags;+constunsignedlongtceshift=tbl->it_page_shift;if((npages==1)||!firmware_has_feature(FW_FEATURE_PUT_TCE_IND)){returntce_build_pSeriesLP(tbl->it_index,tcenum,-tbl->it_page_shift,npages,uaddr,+tceshift,npages,uaddr,direction,attrs);}
@@ -225,13 +228,13 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,if(!tcep){local_irq_restore(flags);returntce_build_pSeriesLP(tbl->it_index,tcenum,-tbl->it_page_shift,+tceshift,npages,uaddr,direction,attrs);}__this_cpu_write(tce_page,tcep);}-rpn=__pa(uaddr)>>TCE_SHIFT;+rpn=__pa(uaddr)>>tceshift;proto_tce=TCE_PCI_READ;if(direction!=DMA_TO_DEVICE)proto_tce|=TCE_PCI_WRITE;
@@ -245,12 +248,12 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,limit=min_t(long,npages,4096/TCE_ENTRY_SIZE);for(l=0;l<limit;l++){-tcep[l]=cpu_to_be64(proto_tce|(rpn&TCE_RPN_MASK)<<TCE_RPN_SHIFT);+tcep[l]=cpu_to_be64(proto_tce|rpn<<tceshift);rpn++;}rc=plpar_tce_put_indirect((u64)tbl->it_index,-(u64)tcenum<<12,+(u64)tcenum<<tceshift,(u64)__pa(tcep),limit);
@@ -277,12 +280,13 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,returnret;}-staticvoidtce_free_pSeriesLP(unsignedlongliobn,longtcenum,longnpages)+staticvoidtce_free_pSeriesLP(unsignedlongliobn,longtcenum,longtceshift,+longnpages){u64rc;while(npages--){-rc=plpar_tce_put((u64)liobn,(u64)tcenum<<12,0);+rc=plpar_tce_put((u64)liobn,(u64)tcenum<<tceshift,0);if(rc&&printk_ratelimit()){printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n",rc);
@@ -301,9 +305,11 @@ static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long nu64rc;if(!firmware_has_feature(FW_FEATURE_STUFF_TCE))-returntce_free_pSeriesLP(tbl->it_index,tcenum,npages);+returntce_free_pSeriesLP(tbl->it_index,tcenum,+tbl->it_page_shift,npages);-rc=plpar_tce_stuff((u64)tbl->it_index,(u64)tcenum<<12,0,npages);+rc=plpar_tce_stuff((u64)tbl->it_index,+(u64)tcenum<<tbl->it_page_shift,0,npages);if(rc&&printk_ratelimit()){printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
@@ -319,7 +325,8 @@ static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)u64rc;unsignedlongtce_ret;-rc=plpar_tce_get((u64)tbl->it_index,(u64)tcenum<<12,&tce_ret);+rc=plpar_tce_get((u64)tbl->it_index,+(u64)tcenum<<tbl->it_page_shift,&tce_ret);if(rc&&printk_ratelimit()){printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n",rc);
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:06
Having a function to check if the iommu table has any allocation helps
deciding if a tbl can be reset for using a new DMA window.
It should be enough to replace all instances of !bitmap_empty(tbl...).
iommu_table_in_use() skips reserved memory, so we don't need to worry about
releasing it before testing. This causes iommu_table_release_pages() to
become unnecessary, given it is only used to remove reserved memory for
testing.
Also, only allow storing reserved memory values in tbl if they are valid
in the table, so there is no need to check it in the new helper.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/include/asm/iommu.h | 1 +
arch/powerpc/kernel/iommu.c | 65 ++++++++++++++++----------------
2 files changed, 34 insertions(+), 32 deletions(-)
@@ -690,32 +690,24 @@ static void iommu_table_reserve_pages(struct iommu_table *tbl,if(tbl->it_offset==0)set_bit(0,tbl->it_map);-tbl->it_reserved_start=res_start;-tbl->it_reserved_end=res_end;--/* Check if res_start..res_end isn't empty and overlaps the table */-if(res_start&&res_end&&-(tbl->it_offset+tbl->it_size<res_start||-res_end<tbl->it_offset))-return;+if(res_start<tbl->it_offset)+res_start=tbl->it_offset;-for(i=tbl->it_reserved_start;i<tbl->it_reserved_end;++i)-set_bit(i-tbl->it_offset,tbl->it_map);-}+if(res_end>(tbl->it_offset+tbl->it_size))+res_end=tbl->it_offset+tbl->it_size;-staticvoidiommu_table_release_pages(structiommu_table*tbl)-{-inti;+/* Check if res_start..res_end is a valid range in the table */+if(res_start>=res_end){+tbl->it_reserved_start=tbl->it_offset;+tbl->it_reserved_end=tbl->it_offset;+return;+}-/*-*Incasewehavereservedthefirstbit,weshouldnotemit-*thewarningbelow.-*/-if(tbl->it_offset==0)-clear_bit(0,tbl->it_map);+tbl->it_reserved_start=res_start;+tbl->it_reserved_end=res_end;for(i=tbl->it_reserved_start;i<tbl->it_reserved_end;++i)-clear_bit(i-tbl->it_offset,tbl->it_map);+set_bit(i-tbl->it_offset,tbl->it_map);}/*
@@ -1099,18 +1105,13 @@ int iommu_take_ownership(struct iommu_table *tbl)for(i=0;i<tbl->nr_pools;i++)spin_lock_nest_lock(&tbl->pools[i].lock,&tbl->large_pool.lock);-iommu_table_release_pages(tbl);--if(!bitmap_empty(tbl->it_map,tbl->it_size)){+if(iommu_table_in_use(tbl)){pr_err("iommu_tce: it_map is not empty");ret=-EBUSY;-/* Undo iommu_table_release_pages, i.e. restore bit#0, etc */-iommu_table_reserve_pages(tbl,tbl->it_reserved_start,-tbl->it_reserved_end);-}else{-memset(tbl->it_map,0xff,sz);}+memset(tbl->it_map,0xff,sz);+for(i=0;i<tbl->nr_pools;i++)spin_unlock(&tbl->pools[i].lock);spin_unlock_irqrestore(&tbl->large_pool.lock,flags);
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:08
Creates a helper to allow allocating a new iommu_table without the need
to reallocate the iommu_group.
This will be helpful for replacing the iommu_table for the new DMA window,
after we remove the old one with iommu_tce_table_put().
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 25 ++++++++++++++-----------
1 file changed, 14 insertions(+), 11 deletions(-)
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:15
There are two functions creating direct_window_list entries in a
similar way, so create a ddw_list_new_entry() to avoid duplicity and
simplify those functions.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 32 +++++++++++++++++---------
1 file changed, 21 insertions(+), 11 deletions(-)
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:18
enable_ddw() currently returns the address of the DMA window, which is
considered invalid if has the value 0x00.
Also, it only considers valid an address returned from find_existing_ddw
if it's not 0x00.
Changing this behavior makes sense, given the users of enable_ddw() only
need to know if direct mapping is possible. It can also allow a DMA window
starting at 0x00 to be used.
This will be helpful for using a DDW with indirect mapping, as the window
address will be different than 0x00, but it will not map the whole
partition.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 36 +++++++++++++-------------
1 file changed, 18 insertions(+), 18 deletions(-)
@@ -853,25 +853,26 @@ static void remove_ddw(struct device_node *np, bool remove_prop)np,ret);}-staticu64find_existing_ddw(structdevice_node*pdn,int*window_shift)+staticboolfind_existing_ddw(structdevice_node*pdn,u64*dma_addr,int*window_shift){structdirect_window*window;conststructdynamic_dma_window_prop*direct64;-u64dma_addr=0;+boolfound=false;spin_lock(&direct_window_list_lock);/* check if we already created a window and dupe that config if so */list_for_each_entry(window,&direct_window_list,list){if(window->device==pdn){direct64=window->prop;-dma_addr=be64_to_cpu(direct64->dma_base);+*dma_addr=be64_to_cpu(direct64->dma_base);*window_shift=be32_to_cpu(direct64->window_shift);+found=true;break;}}spin_unlock(&direct_window_list_lock);-returndma_addr;+returnfound;}staticstructdirect_window*ddw_list_new_entry(structdevice_node*pdn,
@@ -1161,20 +1162,20 @@ static int iommu_get_page_shift(u32 query_page_size)*pdn:theparentpenodewiththeibm,dma_windowproperty*Future:alsocheckifwecanremapthebasewindowforourbasepagesize*-*returnsthedmaoffsetforusebythedirectmappedDMAcode.+*returnstrueifcanmapallpages(directmapping),falseotherwise..*/-staticu64enable_ddw(structpci_dev*dev,structdevice_node*pdn)+staticboolenable_ddw(structpci_dev*dev,structdevice_node*pdn){intlen=0,ret;intmax_ram_len=order_base_2(ddw_memory_hotplug_max());structddw_query_responsequery;structddw_create_responsecreate;intpage_shift;-u64dma_addr;structdevice_node*dn;u32ddw_avail[DDW_APPLICABLE_SIZE];structdirect_window*window;structproperty*win64;+boolddw_enabled=false;structdynamic_dma_window_prop*ddwprop;structfailed_ddw_pdn*fpdn;booldefault_win_removed=false;
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:28
Code used to create a ddw property that was previously scattered in
enable_ddw() is now gathered in ddw_property_create(), which deals with
allocation and filling the property, letting it ready for
of_property_add(), which now occurs in sequence.
This created an opportunity to reorganize the second part of enable_ddw():
Without this patch enable_ddw() does, in order:
kzalloc() property & members, create_ddw(), fill ddwprop inside property,
ddw_list_new_entry(), do tce_setrange_multi_pSeriesLP_walk in all memory,
of_add_property(), and list_add().
With this patch enable_ddw() does, in order:
create_ddw(), ddw_property_create(), of_add_property(),
ddw_list_new_entry(), do tce_setrange_multi_pSeriesLP_walk in all memory,
and list_add().
This change requires of_remove_property() in case anything fails after
of_add_property(), but we get to do tce_setrange_multi_pSeriesLP_walk
in all memory, which looks the most expensive operation, only if
everything else succeeds.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 93 ++++++++++++++++----------
1 file changed, 57 insertions(+), 36 deletions(-)
@@ -1293,72 +1322,64 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)1ULL<<page_shift);gotoout_failed;}-win64=kzalloc(sizeof(structproperty),GFP_KERNEL);-if(!win64){-dev_info(&dev->dev,-"couldn't allocate property for 64bit dma window\n");-gotoout_failed;-}-win64->name=kstrdup(DIRECT64_PROPNAME,GFP_KERNEL);-win64->value=ddwprop=kmalloc(sizeof(*ddwprop),GFP_KERNEL);-win64->length=sizeof(*ddwprop);-if(!win64->name||!win64->value){-dev_info(&dev->dev,-"couldn't allocate property name and value\n");-gotoout_free_prop;-}ret=create_ddw(dev,ddw_avail,&create,page_shift,len);if(ret!=0)-gotoout_free_prop;--ddwprop->liobn=cpu_to_be32(create.liobn);-ddwprop->dma_base=cpu_to_be64(((u64)create.addr_hi<<32)|-create.addr_lo);-ddwprop->tce_shift=cpu_to_be32(page_shift);-ddwprop->window_shift=cpu_to_be32(len);+gotoout_failed;dev_dbg(&dev->dev,"created tce table LIOBN 0x%x for %pOF\n",create.liobn,dn);-window=ddw_list_new_entry(pdn,ddwprop);+win_addr=((u64)create.addr_hi<<32)|create.addr_lo;+win64=ddw_property_create(DIRECT64_PROPNAME,create.liobn,win_addr,+page_shift,len);+if(!win64){+dev_info(&dev->dev,+"couldn't allocate property, property name, or value\n");+gotoout_remove_win;+}++ret=of_add_property(pdn,win64);+if(ret){+dev_err(&dev->dev,"unable to add dma window property for %pOF: %d",+pdn,ret);+gotoout_free_prop;+}++window=ddw_list_new_entry(pdn,win64->value);if(!window)-gotoout_clear_window;+gotoout_del_prop;ret=walk_system_ram_range(0,memblock_end_of_DRAM()>>PAGE_SHIFT,win64->value,tce_setrange_multi_pSeriesLP_walk);if(ret){dev_info(&dev->dev,"failed to map direct window for %pOF: %d\n",dn,ret);-gotoout_free_window;-}--ret=of_add_property(pdn,win64);-if(ret){-dev_err(&dev->dev,"unable to add dma window property for %pOF: %d",-pdn,ret);-gotoout_free_window;+gotoout_del_list;}spin_lock(&direct_window_list_lock);list_add(&window->list,&direct_window_list);spin_unlock(&direct_window_list_lock);-dev->dev.archdata.dma_offset=be64_to_cpu(ddwprop->dma_base);+dev->dev.archdata.dma_offset=win_addr;ddw_enabled=true;gotoout_unlock;-out_free_window:+out_del_list:kfree(window);-out_clear_window:-remove_ddw(pdn,true);+out_del_prop:+of_remove_property(pdn,win64);out_free_prop:kfree(win64->name);kfree(win64->value);kfree(win64);+out_remove_win:+remove_ddw(pdn,true);+out_failed:if(default_win_removed)reset_dma_window(dev,pdn);
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:40
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 <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 72 ++++++++++++++------------
1 file changed, 38 insertions(+), 34 deletions(-)
@@ -501,6 +501,24 @@ static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,returntce_setrange_multi_pSeriesLP(start_pfn,num_pfn,arg);}+staticvoidiommu_table_setparms_common(structiommu_table*tbl,unsignedlongbusno,+unsignedlongliobn,unsignedlongwin_addr,+unsignedlongwindow_size,unsignedlongpage_shift,+void*base,structiommu_table_ops*table_ops)+{+tbl->it_busno=busno;+tbl->it_index=liobn;+tbl->it_offset=win_addr>>page_shift;+tbl->it_size=window_size>>page_shift;+tbl->it_page_shift=page_shift;+tbl->it_base=(unsignedlong)base;+tbl->it_blocksize=16;+tbl->it_type=TCE_PCI;+tbl->it_ops=table_ops;+}++structiommu_table_opsiommu_table_pseries_ops;+staticvoidiommu_table_setparms(structpci_controller*phb,structdevice_node*dn,structiommu_table*tbl)
@@ -509,8 +527,13 @@ static void iommu_table_setparms(struct pci_controller *phb,constunsignedlong*basep;constu32*sizep;-node=phb->dn;+/* Test if we are going over 2GB of DMA space */+if(phb->dma_window_base_cur+phb->dma_window_size>SZ_2G){+udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");+panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");+}+node=phb->dn;basep=of_get_property(node,"linux,tce-base",NULL);sizep=of_get_property(node,"linux,tce-size",NULL);if(basep==NULL||sizep==NULL){
@@ -519,33 +542,18 @@ static void iommu_table_setparms(struct pci_controller *phb,return;}-tbl->it_base=(unsignedlong)__va(*basep);+iommu_table_setparms_common(tbl,phb->bus->number,0,phb->dma_window_base_cur,+phb->dma_window_size,IOMMU_PAGE_SHIFT_4K,+__va(*basep),&iommu_table_pseries_ops);if(!is_kdump_kernel())memset((void*)tbl->it_base,0,*sizep);-tbl->it_busno=phb->bus->number;-tbl->it_page_shift=IOMMU_PAGE_SHIFT_4K;--/* Units of tce entries */-tbl->it_offset=phb->dma_window_base_cur>>tbl->it_page_shift;--/* Test if we are going over 2GB of DMA space */-if(phb->dma_window_base_cur+phb->dma_window_size>0x80000000ul){-udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");-panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");-}-phb->dma_window_base_cur+=phb->dma_window_size;--/* Set the tce table size - measured in entries */-tbl->it_size=phb->dma_window_size>>tbl->it_page_shift;--tbl->it_index=0;-tbl->it_blocksize=16;-tbl->it_type=TCE_PCI;}+structiommu_table_opsiommu_table_lpar_multi_ops;+/**iommu_table_setparms_lpar*
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:44
Update remove_dma_window() so it can be used to remove DDW with a given
property name.
This enables the creation of new property names for DDW, so we can
have different usage for it, like indirect mapping.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:46
At the moment pseries stores information about created directly mapped
DDW window in DIRECT64_PROPNAME.
With the objective of implementing indirect DMA mapping with DDW, it's
necessary to have another propriety name to make sure kexec'ing into older
kernels does not break, as it would if we reuse DIRECT64_PROPNAME.
In order to have this, find_existing_ddw_windows() needs to be able to
look for different property names.
Extract find_existing_ddw_windows() into find_existing_ddw_windows_named()
and calls it with current property name.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 25 +++++++++++++++----------
1 file changed, 15 insertions(+), 10 deletions(-)
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:52
So far it's assumed possible to map the guest RAM 1:1 to the bus, which
works with a small number of devices. SRIOV changes it as the user can
configure hundreds VFs and since phyp preallocates TCEs and does not
allow IOMMU pages bigger than 64K, it has to limit the number of TCEs
per a PE to limit waste of physical pages.
As of today, if the assumed direct mapping is not possible, DDW creation
is skipped and the default DMA window "ibm,dma-window" is used instead.
By using DDW, indirect mapping can get more TCEs than available for the
default DMA window, and also get access to using much larger pagesizes
(16MB as implemented in qemu vs 4k from default DMA window), causing a
significant increase on the maximum amount of memory that can be IOMMU
mapped at the same time.
Indirect mapping will only be used if direct mapping is not a
possibility.
For indirect mapping, it's necessary to re-create the iommu_table with
the new DMA window parameters, so iommu_alloc() can use it.
Removing the default DMA window for using DDW with indirect mapping
is only allowed if there is no current IOMMU memory allocated in
the iommu_table. enable_ddw() is aborted otherwise.
Even though there won't be both direct and indirect mappings at the
same time, we can't reuse the DIRECT64_PROPNAME property name, or else
an older kexec()ed kernel can assume direct mapping, and skip
iommu_alloc(), causing undesirable behavior.
So a new property name DMA64_PROPNAME "linux,dma64-ddr-window-info"
was created to represent a DDW that does not allow direct mapping.
Signed-off-by: Leonardo Bras <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 87 +++++++++++++++++++++-----
1 file changed, 72 insertions(+), 15 deletions(-)
@@ -1307,8 +1313,7 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)query.page_size);gotoout_failed;}-/* verify the window * number of ptes will map the partition */-/* check largest block * page size > max memory hotplug addr */+/**The"ibm,pmemory"canappearanywhereintheaddressspace.*Assumingitisstillbackedbypagestructs,tryMAX_PHYSMEM_BITS
@@ -1324,13 +1329,25 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)dev_info(&dev->dev,"Skipping ibm,pmemory");}+/* check if the available block * number of ptes will map everything */if(query.largest_available_block<(1ULL<<(len-page_shift))){dev_dbg(&dev->dev,"can't map partition max 0x%llx with %llu %llu-sized pages\n",1ULL<<len,query.largest_available_block,1ULL<<page_shift);-gotoout_failed;++/* DDW + IOMMU on single window may fail if there is any allocation */+if(default_win_removed&&iommu_table_in_use(tbl)){+dev_dbg(&dev->dev,"current IOMMU table in use, can't be replaced.\n");+gotoout_failed;+}++len=order_base_2(query.largest_available_block<<page_shift);+win_name=DMA64_PROPNAME;+}else{+direct_mapping=true;+win_name=DIRECT64_PROPNAME;}ret=create_ddw(dev,ddw_avail,&create,page_shift,len);
@@ -1360,12 +1376,51 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)if(!window)gotoout_del_prop;-ret=walk_system_ram_range(0,memblock_end_of_DRAM()>>PAGE_SHIFT,-win64->value,tce_setrange_multi_pSeriesLP_walk);-if(ret){-dev_info(&dev->dev,"failed to map direct window for %pOF: %d\n",-dn,ret);-gotoout_del_list;+if(direct_mapping){+/* DDW maps the whole partition, so enable direct DMA mapping */+ret=walk_system_ram_range(0,memblock_end_of_DRAM()>>PAGE_SHIFT,+win64->value,tce_setrange_multi_pSeriesLP_walk);+if(ret){+dev_info(&dev->dev,"failed to map direct window for %pOF: %d\n",+dn,ret);+gotoout_del_list;+}+}else{+structiommu_table*newtbl;+inti;++for(i=0;i<ARRAY_SIZE(pci->phb->mem_resources);i++){+constunsignedlongmask=IORESOURCE_MEM_64|IORESOURCE_MEM;++/* Look for MMIO32 */+if((pci->phb->mem_resources[i].flags&mask)==IORESOURCE_MEM)+break;+}++if(i==ARRAY_SIZE(pci->phb->mem_resources))+gotoout_del_list;++/* New table for using DDW instead of the default DMA window */+newtbl=iommu_pseries_alloc_table(pci->phb->node);+if(!newtbl){+dev_dbg(&dev->dev,"couldn't create new IOMMU table\n");+gotoout_del_list;+}++iommu_table_setparms_common(newtbl,pci->phb->bus->number,create.liobn,win_addr,+1UL<<len,page_shift,NULL,&iommu_table_lpar_multi_ops);+iommu_init_table(newtbl,pci->phb->node,pci->phb->mem_resources[i].start,+pci->phb->mem_resources[i].end);++pci->table_group->tables[1]=newtbl;++/* Keep default DMA window stuct if removed */+if(default_win_removed){+tbl->it_size=0;+kfree(tbl->it_map);+}++set_iommu_table_base(&dev->dev,newtbl);}spin_lock(&direct_window_list_lock);
From: Leonardo Bras <hidden> Date: 2021-07-16 08:28:55
A previous change introduced the usage of DDW as a bigger indirect DMA
mapping when the DDW available size does not map the whole partition.
As most of the code that manipulates direct mappings was reused for
indirect mappings, it's necessary to rename all names and debug/info
messages to reflect that it can be used for both kinds of mapping.
This should cause no behavioural change, just adjust naming.
Signed-off-by: Leonardo Bras <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 91 +++++++++++++-------------
1 file changed, 47 insertions(+), 44 deletions(-)
@@ -369,11 +369,11 @@ struct ddw_create_response {u32addr_lo;};-staticLIST_HEAD(direct_window_list);+staticLIST_HEAD(dma_win_list);/* prevents races between memory on/offline and window creation */-staticDEFINE_SPINLOCK(direct_window_list_lock);+staticDEFINE_SPINLOCK(dma_win_list_lock);/* protects initializing window twice for same device */-staticDEFINE_MUTEX(direct_window_init_mutex);+staticDEFINE_MUTEX(dma_win_init_mutex);#define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"#define DMA64_PROPNAME "linux,dma64-ddr-window-info"
@@ -713,7 +713,10 @@ static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %pOF\n",dn);-/* Find nearest ibm,dma-window, walking up the device tree */+/*+*Findnearestibm,dma-window(defaultDMAwindow),walkingupthe+*devicetree+*/for(pdn=dn;pdn!=NULL;pdn=pdn->parent){dma_window=of_get_property(pdn,"ibm,dma-window",NULL);if(dma_window!=NULL)
@@ -822,11 +825,11 @@ static void remove_dma_window(struct device_node *np, u32 *ddw_avail,ret=rtas_call(ddw_avail[DDW_REMOVE_PE_DMA_WIN],1,1,NULL,liobn);if(ret)-pr_warn("%pOF: failed to remove direct window: rtas returned "+pr_warn("%pOF: failed to remove DMA window: rtas returned ""%d to ibm,remove-pe-dma-window(%x) %llx\n",np,ret,ddw_avail[DDW_REMOVE_PE_DMA_WIN],liobn);else-pr_debug("%pOF: successfully removed direct window: rtas returned "+pr_debug("%pOF: successfully removed DMA window: rtas returned ""%d to ibm,remove-pe-dma-window(%x) %llx\n",np,ret,ddw_avail[DDW_REMOVE_PE_DMA_WIN],liobn);}
@@ -854,37 +857,37 @@ static int remove_ddw(struct device_node *np, bool remove_prop, const char *win_ret=of_remove_property(np,win);if(ret)-pr_warn("%pOF: failed to remove direct window property: %d\n",+pr_warn("%pOF: failed to remove DMA window property: %d\n",np,ret);return0;}staticboolfind_existing_ddw(structdevice_node*pdn,u64*dma_addr,int*window_shift){-structdirect_window*window;-conststructdynamic_dma_window_prop*direct64;+structdma_win*window;+conststructdynamic_dma_window_prop*dma64;boolfound=false;-spin_lock(&direct_window_list_lock);+spin_lock(&dma_win_list_lock);/* check if we already created a window and dupe that config if so */-list_for_each_entry(window,&direct_window_list,list){+list_for_each_entry(window,&dma_win_list,list){if(window->device==pdn){-direct64=window->prop;-*dma_addr=be64_to_cpu(direct64->dma_base);-*window_shift=be32_to_cpu(direct64->window_shift);+dma64=window->prop;+*dma_addr=be64_to_cpu(dma64->dma_base);+*window_shift=be32_to_cpu(dma64->window_shift);found=true;break;}}-spin_unlock(&direct_window_list_lock);+spin_unlock(&dma_win_list_lock);returnfound;}-staticstructdirect_window*ddw_list_new_entry(structdevice_node*pdn,-conststructdynamic_dma_window_prop*dma64)+staticstructdma_win*ddw_list_new_entry(structdevice_node*pdn,+conststructdynamic_dma_window_prop*dma64){-structdirect_window*window;+structdma_win*window;window=kzalloc(sizeof(*window),GFP_KERNEL);if(!window)
Some functions assume IOMMU page size can only be 4K (pageshift == 12).
Update them to accept any page size passed, so we can use 64K pages.
In the process, some defines like TCE_SHIFT were made obsolete, and then
removed.
IODA3 Revision 3.0_prd1 (OpenPowerFoundation), Figures 3.4 and 3.5 show
a RPN of 52-bit, and considers a 12-bit pageshift, so there should be
no need of using TCE_RPN_MASK, which masks out any bit after 40 in rpn.
It's usage removed from tce_build_pSeries(), tce_build_pSeriesLP(), and
tce_buildmulti_pSeriesLP().
Most places had a tbl struct, so using tbl->it_page_shift was simple.
tce_free_pSeriesLP() was a special case, since callers not always have a
tbl struct, so adding a tceshift parameter seems the right thing to do.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
@@ -107,6 +107,8 @@ static int tce_build_pSeries(struct iommu_table *tbl, long index,u64proto_tce;__be64*tcep;u64rpn;+constunsignedlongtceshift=tbl->it_page_shift;+constunsignedlongpagesize=IOMMU_PAGE_SIZE(tbl);proto_tce=TCE_PCI_READ;// Read allowed
@@ -117,10 +119,10 @@ static int tce_build_pSeries(struct iommu_table *tbl, long index,while(npages--){/* can't move this out since we might cross MEMBLOCK boundary */-rpn=__pa(uaddr)>>TCE_SHIFT;-*tcep=cpu_to_be64(proto_tce|(rpn&TCE_RPN_MASK)<<TCE_RPN_SHIFT);+rpn=__pa(uaddr)>>tceshift;+*tcep=cpu_to_be64(proto_tce|rpn<<tceshift);-uaddr+=TCE_PAGE_SIZE;+uaddr+=pagesize;tcep++;}return0;
@@ -146,7 +148,7 @@ static unsigned long tce_get_pseries(struct iommu_table *tbl, long index)returnbe64_to_cpu(*tcep);}-staticvoidtce_free_pSeriesLP(unsignedlongliobn,long,long);+staticvoidtce_free_pSeriesLP(unsignedlongliobn,long,long,long);staticvoidtce_freemulti_pSeriesLP(structiommu_table*,long,long);staticinttce_build_pSeriesLP(unsignedlongliobn,longtcenum,longtceshift,
@@ -166,12 +168,12 @@ static int tce_build_pSeriesLP(unsigned long liobn, long tcenum, long tceshift,proto_tce|=TCE_PCI_WRITE;while(npages--){-tce=proto_tce|(rpn&TCE_RPN_MASK)<<tceshift;+tce=proto_tce|rpn<<tceshift;rc=plpar_tce_put((u64)liobn,(u64)tcenum<<tceshift,tce);if(unlikely(rc==H_NOT_ENOUGH_RESOURCES)){ret=(int)rc;-tce_free_pSeriesLP(liobn,tcenum_start,+tce_free_pSeriesLP(liobn,tcenum_start,tceshift,(npages_start-(npages+1)));break;}
@@ -205,10 +207,11 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,longtcenum_start=tcenum,npages_start=npages;intret=0;unsignedlongflags;+constunsignedlongtceshift=tbl->it_page_shift;if((npages==1)||!firmware_has_feature(FW_FEATURE_PUT_TCE_IND)){returntce_build_pSeriesLP(tbl->it_index,tcenum,-tbl->it_page_shift,npages,uaddr,+tceshift,npages,uaddr,direction,attrs);}
@@ -225,13 +228,13 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,if(!tcep){local_irq_restore(flags);returntce_build_pSeriesLP(tbl->it_index,tcenum,-tbl->it_page_shift,+tceshift,npages,uaddr,direction,attrs);}__this_cpu_write(tce_page,tcep);}-rpn=__pa(uaddr)>>TCE_SHIFT;+rpn=__pa(uaddr)>>tceshift;proto_tce=TCE_PCI_READ;if(direction!=DMA_TO_DEVICE)proto_tce|=TCE_PCI_WRITE;
@@ -245,12 +248,12 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,limit=min_t(long,npages,4096/TCE_ENTRY_SIZE);for(l=0;l<limit;l++){-tcep[l]=cpu_to_be64(proto_tce|(rpn&TCE_RPN_MASK)<<TCE_RPN_SHIFT);+tcep[l]=cpu_to_be64(proto_tce|rpn<<tceshift);rpn++;}rc=plpar_tce_put_indirect((u64)tbl->it_index,-(u64)tcenum<<12,+(u64)tcenum<<tceshift,(u64)__pa(tcep),limit);
@@ -277,12 +280,13 @@ static int tce_buildmulti_pSeriesLP(struct iommu_table *tbl, long tcenum,returnret;}-staticvoidtce_free_pSeriesLP(unsignedlongliobn,longtcenum,longnpages)+staticvoidtce_free_pSeriesLP(unsignedlongliobn,longtcenum,longtceshift,+longnpages){u64rc;while(npages--){-rc=plpar_tce_put((u64)liobn,(u64)tcenum<<12,0);+rc=plpar_tce_put((u64)liobn,(u64)tcenum<<tceshift,0);if(rc&&printk_ratelimit()){printk("tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n",rc);
@@ -301,9 +305,11 @@ static void tce_freemulti_pSeriesLP(struct iommu_table *tbl, long tcenum, long nu64rc;if(!firmware_has_feature(FW_FEATURE_STUFF_TCE))-returntce_free_pSeriesLP(tbl->it_index,tcenum,npages);+returntce_free_pSeriesLP(tbl->it_index,tcenum,+tbl->it_page_shift,npages);-rc=plpar_tce_stuff((u64)tbl->it_index,(u64)tcenum<<12,0,npages);+rc=plpar_tce_stuff((u64)tbl->it_index,+(u64)tcenum<<tbl->it_page_shift,0,npages);if(rc&&printk_ratelimit()){printk("tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
@@ -319,7 +325,8 @@ static unsigned long tce_get_pSeriesLP(struct iommu_table *tbl, long tcenum)u64rc;unsignedlongtce_ret;-rc=plpar_tce_get((u64)tbl->it_index,(u64)tcenum<<12,&tce_ret);+rc=plpar_tce_get((u64)tbl->it_index,+(u64)tcenum<<tbl->it_page_shift,&tce_ret);if(rc&&printk_ratelimit()){printk("tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n",rc);
@@ -1099,18 +1105,13 @@ int iommu_take_ownership(struct iommu_table *tbl) for (i = 0; i < tbl->nr_pools; i++) spin_lock_nest_lock(&tbl->pools[i].lock, &tbl->large_pool.lock);- iommu_table_release_pages(tbl);-- if (!bitmap_empty(tbl->it_map, tbl->it_size)) {+ if (iommu_table_in_use(tbl)) { pr_err("iommu_tce: it_map is not empty"); ret = -EBUSY;- /* Undo iommu_table_release_pages, i.e. restore bit#0, etc */- iommu_table_reserve_pages(tbl, tbl->it_reserved_start,- tbl->it_reserved_end);- } else {- memset(tbl->it_map, 0xff, sz); }+ memset(tbl->it_map, 0xff, sz);+
So if the table is not empty, we fail (EBUSY) but we now also completely
overwrite the bitmap. It was in an unexpected state, but we're making it
worse. Or am I missing something?
Fred
for (i = 0; i < tbl->nr_pools; i++)
spin_unlock(&tbl->pools[i].lock);
spin_unlock_irqrestore(&tbl->large_pool.lock, flags);
Creates a helper to allow allocating a new iommu_table without the need
to reallocate the iommu_group.
This will be helpful for replacing the iommu_table for the new DMA window,
after we remove the old one with iommu_tce_table_put().
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 25 ++++++++++++++-----------
1 file changed, 14 insertions(+), 11 deletions(-)
Nitpick: for readability, we'd usually expect the error path to be
detected with the if statement and keep going on the good path, and here
the code does the opposite. No big deal though, so
Reviewed-by: Frederic Barrat <redacted>
There are two functions creating direct_window_list entries in a
similar way, so create a ddw_list_new_entry() to avoid duplicity and
simplify those functions.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
On Mon, 2021-07-19 at 15:48 +0200, Frederic Barrat wrote:
On 16/07/2021 10:27, Leonardo Bras wrote:
quoted
Some functions assume IOMMU page size can only be 4K (pageshift ==
12).
Update them to accept any page size passed, so we can use 64K
pages.
In the process, some defines like TCE_SHIFT were made obsolete, and
then
removed.
IODA3 Revision 3.0_prd1 (OpenPowerFoundation), Figures 3.4 and 3.5
show
a RPN of 52-bit, and considers a 12-bit pageshift, so there should
be
no need of using TCE_RPN_MASK, which masks out any bit after 40 in
rpn.
It's usage removed from tce_build_pSeries(), tce_build_pSeriesLP(),
and
tce_buildmulti_pSeriesLP().
Most places had a tbl struct, so using tbl->it_page_shift was
simple.
tce_free_pSeriesLP() was a special case, since callers not always
have a
tbl struct, so adding a tceshift parameter seems the right thing to
do.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
On Mon, 2021-07-19 at 16:14 +0200, Frederic Barrat wrote:
On 16/07/2021 10:27, Leonardo Bras wrote:
quoted
There are two functions creating direct_window_list entries in a
similar way, so create a ddw_list_new_entry() to avoid duplicity
and
simplify those functions.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
On Mon, 2021-07-19 at 16:04 +0200, Frederic Barrat wrote:
On 16/07/2021 10:27, Leonardo Bras wrote:
quoted
Creates a helper to allow allocating a new iommu_table without the
need
to reallocate the iommu_group.
This will be helpful for replacing the iommu_table for the new DMA
window,
after we remove the old one with iommu_tce_table_put().
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 25 ++++++++++++++-------
----
1 file changed, 14 insertions(+), 11 deletions(-)
Nitpick: for readability, we'd usually expect the error path to be
detected with the if statement and keep going on the good path, and
here
the code does the opposite. No big deal though, so
Reviewed-by: Frederic Barrat <redacted>
Thanks for the tip and review!
Best regards,
Leonardo Bras
Hello Fred, thanks for this feedback!
Sorry if I miss anything, this snippet was written for v1 over an year
ago, and I have not taken a look at it ever since.
On Mon, 2021-07-19 at 15:53 +0200, Frederic Barrat wrote:
On 16/07/2021 10:27, Leonardo Bras wrote:
quoted
@@ -1099,18 +1105,13 @@ int iommu_take_ownership(struct iommu_table
*tbl)
for (i = 0; i < tbl->nr_pools; i++)
spin_lock_nest_lock(&tbl->pools[i].lock, &tbl-
quoted
large_pool.lock);
- iommu_table_release_pages(tbl);
-
- if (!bitmap_empty(tbl->it_map, tbl->it_size)) {
+ if (iommu_table_in_use(tbl)) {
pr_err("iommu_tce: it_map is not empty");
ret = -EBUSY;
- /* Undo iommu_table_release_pages, i.e. restore
bit#0, etc */
- iommu_table_reserve_pages(tbl, tbl-
So if the table is not empty, we fail (EBUSY) but we now also
completely
overwrite the bitmap. It was in an unexpected state, but we're making
it
worse. Or am I missing something?
IIRC there was a reason to do that at the time, but TBH I don't really
remember it, and by looking at the code right now you seem to be
correct about this causing trouble.
I will send a v6 fixing it soon.
Please review the remaining patches for some issue I may be missing.
Alexey, any comments on that?
Fred
Again, thank you for reviewing Fred!
Best regards,
Leonardo Bras
Hello Fred, thanks for this feedback!
Sorry if I miss anything, this snippet was written for v1 over an year
ago, and I have not taken a look at it ever since.
On Mon, 2021-07-19 at 15:53 +0200, Frederic Barrat wrote:
quoted
On 16/07/2021 10:27, Leonardo Bras wrote:
quoted
@@ -1099,18 +1105,13 @@ int iommu_take_ownership(struct iommu_table
*tbl)
for (i = 0; i < tbl->nr_pools; i++)
spin_lock_nest_lock(&tbl->pools[i].lock, &tbl-
quoted
large_pool.lock);
- iommu_table_release_pages(tbl);
-
- if (!bitmap_empty(tbl->it_map, tbl->it_size)) {
+ if (iommu_table_in_use(tbl)) {
pr_err("iommu_tce: it_map is not empty");
ret = -EBUSY;
- /* Undo iommu_table_release_pages, i.e. restore
bit#0, etc */
- iommu_table_reserve_pages(tbl, tbl-
So if the table is not empty, we fail (EBUSY) but we now also
completely
overwrite the bitmap. It was in an unexpected state, but we're making
it
worse. Or am I missing something?
IIRC there was a reason to do that at the time, but TBH I don't really
remember it, and by looking at the code right now you seem to be
correct about this causing trouble.
I will send a v6 fixing it soon.
Please review the remaining patches for some issue I may be missing.
Alexey, any comments on that?
Agree with Fred, this is a bug, EBUSY is not that unexpected :-/ Thanks,
quoted
Fred
Again, thank you for reviewing Fred!
Best regards,
Leonardo Bras
enable_ddw() currently returns the address of the DMA window, which is
considered invalid if has the value 0x00.
Also, it only considers valid an address returned from find_existing_ddw
if it's not 0x00.
Changing this behavior makes sense, given the users of enable_ddw() only
need to know if direct mapping is possible. It can also allow a DMA window
starting at 0x00 to be used.
This will be helpful for using a DDW with indirect mapping, as the window
address will be different than 0x00, but it will not map the whole
partition.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
Looks good to me
Reviewed-by: Frederic Barrat <redacted>
@@ -853,25 +853,26 @@ static void remove_ddw(struct device_node *np, bool remove_prop)np,ret);}-staticu64find_existing_ddw(structdevice_node*pdn,int*window_shift)+staticboolfind_existing_ddw(structdevice_node*pdn,u64*dma_addr,int*window_shift){structdirect_window*window;conststructdynamic_dma_window_prop*direct64;-u64dma_addr=0;+boolfound=false;spin_lock(&direct_window_list_lock);/* check if we already created a window and dupe that config if so */list_for_each_entry(window,&direct_window_list,list){if(window->device==pdn){direct64=window->prop;-dma_addr=be64_to_cpu(direct64->dma_base);+*dma_addr=be64_to_cpu(direct64->dma_base);*window_shift=be32_to_cpu(direct64->window_shift);+found=true;break;}}spin_unlock(&direct_window_list_lock);-returndma_addr;+returnfound;}staticstructdirect_window*ddw_list_new_entry(structdevice_node*pdn,
@@ -1161,20 +1162,20 @@ static int iommu_get_page_shift(u32 query_page_size)*pdn:theparentpenodewiththeibm,dma_windowproperty*Future:alsocheckifwecanremapthebasewindowforourbasepagesize*-*returnsthedmaoffsetforusebythedirectmappedDMAcode.+*returnstrueifcanmapallpages(directmapping),falseotherwise..*/-staticu64enable_ddw(structpci_dev*dev,structdevice_node*pdn)+staticboolenable_ddw(structpci_dev*dev,structdevice_node*pdn){intlen=0,ret;intmax_ram_len=order_base_2(ddw_memory_hotplug_max());structddw_query_responsequery;structddw_create_responsecreate;intpage_shift;-u64dma_addr;structdevice_node*dn;u32ddw_avail[DDW_APPLICABLE_SIZE];structdirect_window*window;structproperty*win64;+boolddw_enabled=false;structdynamic_dma_window_prop*ddwprop;structfailed_ddw_pdn*fpdn;booldefault_win_removed=false;
Code used to create a ddw property that was previously scattered in
enable_ddw() is now gathered in ddw_property_create(), which deals with
allocation and filling the property, letting it ready for
of_property_add(), which now occurs in sequence.
This created an opportunity to reorganize the second part of enable_ddw():
Without this patch enable_ddw() does, in order:
kzalloc() property & members, create_ddw(), fill ddwprop inside property,
ddw_list_new_entry(), do tce_setrange_multi_pSeriesLP_walk in all memory,
of_add_property(), and list_add().
With this patch enable_ddw() does, in order:
create_ddw(), ddw_property_create(), of_add_property(),
ddw_list_new_entry(), do tce_setrange_multi_pSeriesLP_walk in all memory,
and list_add().
This change requires of_remove_property() in case anything fails after
of_add_property(), but we get to do tce_setrange_multi_pSeriesLP_walk
in all memory, which looks the most expensive operation, only if
everything else succeeds.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 93 ++++++++++++++++----------
1 file changed, 57 insertions(+), 36 deletions(-)
@@ -1293,72 +1322,64 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)1ULL<<page_shift);gotoout_failed;}-win64=kzalloc(sizeof(structproperty),GFP_KERNEL);-if(!win64){-dev_info(&dev->dev,-"couldn't allocate property for 64bit dma window\n");-gotoout_failed;-}-win64->name=kstrdup(DIRECT64_PROPNAME,GFP_KERNEL);-win64->value=ddwprop=kmalloc(sizeof(*ddwprop),GFP_KERNEL);-win64->length=sizeof(*ddwprop);-if(!win64->name||!win64->value){-dev_info(&dev->dev,-"couldn't allocate property name and value\n");-gotoout_free_prop;-}ret=create_ddw(dev,ddw_avail,&create,page_shift,len);if(ret!=0)-gotoout_free_prop;--ddwprop->liobn=cpu_to_be32(create.liobn);-ddwprop->dma_base=cpu_to_be64(((u64)create.addr_hi<<32)|-create.addr_lo);-ddwprop->tce_shift=cpu_to_be32(page_shift);-ddwprop->window_shift=cpu_to_be32(len);+gotoout_failed;dev_dbg(&dev->dev,"created tce table LIOBN 0x%x for %pOF\n",create.liobn,dn);-window=ddw_list_new_entry(pdn,ddwprop);+win_addr=((u64)create.addr_hi<<32)|create.addr_lo;+win64=ddw_property_create(DIRECT64_PROPNAME,create.liobn,win_addr,+page_shift,len);+if(!win64){+dev_info(&dev->dev,+"couldn't allocate property, property name, or value\n");+gotoout_remove_win;+}++ret=of_add_property(pdn,win64);+if(ret){+dev_err(&dev->dev,"unable to add dma window property for %pOF: %d",+pdn,ret);+gotoout_free_prop;+}++window=ddw_list_new_entry(pdn,win64->value);if(!window)-gotoout_clear_window;+gotoout_del_prop;ret=walk_system_ram_range(0,memblock_end_of_DRAM()>>PAGE_SHIFT,win64->value,tce_setrange_multi_pSeriesLP_walk);if(ret){dev_info(&dev->dev,"failed to map direct window for %pOF: %d\n",dn,ret);-gotoout_free_window;-}--ret=of_add_property(pdn,win64);-if(ret){-dev_err(&dev->dev,"unable to add dma window property for %pOF: %d",-pdn,ret);-gotoout_free_window;+gotoout_del_list;}spin_lock(&direct_window_list_lock);list_add(&window->list,&direct_window_list);spin_unlock(&direct_window_list_lock);-dev->dev.archdata.dma_offset=be64_to_cpu(ddwprop->dma_base);+dev->dev.archdata.dma_offset=win_addr;ddw_enabled=true;gotoout_unlock;-out_free_window:+out_del_list:kfree(window);-out_clear_window:-remove_ddw(pdn,true);+out_del_prop:+of_remove_property(pdn,win64);out_free_prop:kfree(win64->name);kfree(win64->value);kfree(win64);+out_remove_win:+remove_ddw(pdn,true);
I believe there's a small problem here. We jump directly to
out_remove_win if allocating the property failed. Yet, the first thing
remove_ddw() does is look for the property. So it will never find it and
the window is never removed by the hypervisor.
Fred
+
out_failed:
if (default_win_removed)
reset_dma_window(dev, pdn);
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 <redacted>
---
Looks good
Reviewed-by: Frederic Barrat <redacted>
@@ -501,6 +501,24 @@ static int tce_setrange_multi_pSeriesLP_walk(unsigned long start_pfn,returntce_setrange_multi_pSeriesLP(start_pfn,num_pfn,arg);}+staticvoidiommu_table_setparms_common(structiommu_table*tbl,unsignedlongbusno,+unsignedlongliobn,unsignedlongwin_addr,+unsignedlongwindow_size,unsignedlongpage_shift,+void*base,structiommu_table_ops*table_ops)+{+tbl->it_busno=busno;+tbl->it_index=liobn;+tbl->it_offset=win_addr>>page_shift;+tbl->it_size=window_size>>page_shift;+tbl->it_page_shift=page_shift;+tbl->it_base=(unsignedlong)base;+tbl->it_blocksize=16;+tbl->it_type=TCE_PCI;+tbl->it_ops=table_ops;+}++structiommu_table_opsiommu_table_pseries_ops;+staticvoidiommu_table_setparms(structpci_controller*phb,structdevice_node*dn,structiommu_table*tbl)
@@ -509,8 +527,13 @@ static void iommu_table_setparms(struct pci_controller *phb,constunsignedlong*basep;constu32*sizep;-node=phb->dn;+/* Test if we are going over 2GB of DMA space */+if(phb->dma_window_base_cur+phb->dma_window_size>SZ_2G){+udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");+panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");+}+node=phb->dn;basep=of_get_property(node,"linux,tce-base",NULL);sizep=of_get_property(node,"linux,tce-size",NULL);if(basep==NULL||sizep==NULL){
@@ -519,33 +542,18 @@ static void iommu_table_setparms(struct pci_controller *phb,return;}-tbl->it_base=(unsignedlong)__va(*basep);+iommu_table_setparms_common(tbl,phb->bus->number,0,phb->dma_window_base_cur,+phb->dma_window_size,IOMMU_PAGE_SHIFT_4K,+__va(*basep),&iommu_table_pseries_ops);if(!is_kdump_kernel())memset((void*)tbl->it_base,0,*sizep);-tbl->it_busno=phb->bus->number;-tbl->it_page_shift=IOMMU_PAGE_SHIFT_4K;--/* Units of tce entries */-tbl->it_offset=phb->dma_window_base_cur>>tbl->it_page_shift;--/* Test if we are going over 2GB of DMA space */-if(phb->dma_window_base_cur+phb->dma_window_size>0x80000000ul){-udbg_printf("PCI_DMA: Unexpected number of IOAs under this PHB.\n");-panic("PCI_DMA: Unexpected number of IOAs under this PHB.\n");-}-phb->dma_window_base_cur+=phb->dma_window_size;--/* Set the tce table size - measured in entries */-tbl->it_size=phb->dma_window_size>>tbl->it_page_shift;--tbl->it_index=0;-tbl->it_blocksize=16;-tbl->it_type=TCE_PCI;}+structiommu_table_opsiommu_table_lpar_multi_ops;+/**iommu_table_setparms_lpar*
Update remove_dma_window() so it can be used to remove DDW with a given
property name.
This enables the creation of new property names for DDW, so we can
have different usage for it, like indirect mapping.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
@@ -1547,7 +1548,7 @@ static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long acti * we have to remove the property when releasing * the device node. */- remove_ddw(np, false);+ remove_ddw(np, false, DIRECT64_PROPNAME); if (pci && pci->table_group) iommu_pseries_free_group(pci->table_group, np->full_name);
At the moment pseries stores information about created directly mapped
DDW window in DIRECT64_PROPNAME.
With the objective of implementing indirect DMA mapping with DDW, it's
necessary to have another propriety name to make sure kexec'ing into older
kernels does not break, as it would if we reuse DIRECT64_PROPNAME.
In order to have this, find_existing_ddw_windows() needs to be able to
look for different property names.
Extract find_existing_ddw_windows() into find_existing_ddw_windows_named()
and calls it with current property name.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
So far it's assumed possible to map the guest RAM 1:1 to the bus, which
works with a small number of devices. SRIOV changes it as the user can
configure hundreds VFs and since phyp preallocates TCEs and does not
allow IOMMU pages bigger than 64K, it has to limit the number of TCEs
per a PE to limit waste of physical pages.
As of today, if the assumed direct mapping is not possible, DDW creation
is skipped and the default DMA window "ibm,dma-window" is used instead.
By using DDW, indirect mapping can get more TCEs than available for the
default DMA window, and also get access to using much larger pagesizes
(16MB as implemented in qemu vs 4k from default DMA window), causing a
significant increase on the maximum amount of memory that can be IOMMU
mapped at the same time.
Indirect mapping will only be used if direct mapping is not a
possibility.
For indirect mapping, it's necessary to re-create the iommu_table with
the new DMA window parameters, so iommu_alloc() can use it.
Removing the default DMA window for using DDW with indirect mapping
is only allowed if there is no current IOMMU memory allocated in
the iommu_table. enable_ddw() is aborted otherwise.
Even though there won't be both direct and indirect mappings at the
same time, we can't reuse the DIRECT64_PROPNAME property name, or else
an older kexec()ed kernel can assume direct mapping, and skip
iommu_alloc(), causing undesirable behavior.
So a new property name DMA64_PROPNAME "linux,dma64-ddr-window-info"
was created to represent a DDW that does not allow direct mapping.
Signed-off-by: Leonardo Bras <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 87 +++++++++++++++++++++-----
1 file changed, 72 insertions(+), 15 deletions(-)
@@ -1307,8 +1313,7 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)query.page_size);gotoout_failed;}-/* verify the window * number of ptes will map the partition */-/* check largest block * page size > max memory hotplug addr */+/**The"ibm,pmemory"canappearanywhereintheaddressspace.*Assumingitisstillbackedbypagestructs,tryMAX_PHYSMEM_BITS
@@ -1324,13 +1329,25 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)dev_info(&dev->dev,"Skipping ibm,pmemory");}+/* check if the available block * number of ptes will map everything */if(query.largest_available_block<(1ULL<<(len-page_shift))){dev_dbg(&dev->dev,"can't map partition max 0x%llx with %llu %llu-sized pages\n",1ULL<<len,query.largest_available_block,1ULL<<page_shift);-gotoout_failed;++/* DDW + IOMMU on single window may fail if there is any allocation */+if(default_win_removed&&iommu_table_in_use(tbl)){+dev_dbg(&dev->dev,"current IOMMU table in use, can't be replaced.\n");+gotoout_failed;+}++len=order_base_2(query.largest_available_block<<page_shift);+win_name=DMA64_PROPNAME;+}else{+direct_mapping=true;+win_name=DIRECT64_PROPNAME;}ret=create_ddw(dev,ddw_avail,&create,page_shift,len);
@@ -1360,12 +1376,51 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn)if(!window)gotoout_del_prop;-ret=walk_system_ram_range(0,memblock_end_of_DRAM()>>PAGE_SHIFT,-win64->value,tce_setrange_multi_pSeriesLP_walk);-if(ret){-dev_info(&dev->dev,"failed to map direct window for %pOF: %d\n",-dn,ret);-gotoout_del_list;+if(direct_mapping){+/* DDW maps the whole partition, so enable direct DMA mapping */+ret=walk_system_ram_range(0,memblock_end_of_DRAM()>>PAGE_SHIFT,+win64->value,tce_setrange_multi_pSeriesLP_walk);+if(ret){+dev_info(&dev->dev,"failed to map direct window for %pOF: %d\n",+dn,ret);+gotoout_del_list;+}+}else{+structiommu_table*newtbl;+inti;++for(i=0;i<ARRAY_SIZE(pci->phb->mem_resources);i++){+constunsignedlongmask=IORESOURCE_MEM_64|IORESOURCE_MEM;++/* Look for MMIO32 */+if((pci->phb->mem_resources[i].flags&mask)==IORESOURCE_MEM)+break;+}++if(i==ARRAY_SIZE(pci->phb->mem_resources))+gotoout_del_list;
So we exit and do nothing if there's no MMIO32 bar?
Isn't the intent just to figure out the MMIO32 area to reserve it when
init'ing the table? In which case we could default to 0,0
I'm actually not clear why we are reserving this area on pseries.
+
+ /* New table for using DDW instead of the default DMA window */
+ newtbl = iommu_pseries_alloc_table(pci->phb->node);
+ if (!newtbl) {
+ dev_dbg(&dev->dev, "couldn't create new IOMMU table\n");
+ goto out_del_list;
+ }
+
+ iommu_table_setparms_common(newtbl, pci->phb->bus->number, create.liobn, win_addr,
+ 1UL << len, page_shift, NULL, &iommu_table_lpar_multi_ops);
+ iommu_init_table(newtbl, pci->phb->node, pci->phb->mem_resources[i].start,
+ pci->phb->mem_resources[i].end);
+
+ pci->table_group->tables[1] = newtbl;
+
+ /* Keep default DMA window stuct if removed */
+ if (default_win_removed) {
+ tbl->it_size = 0;
+ kfree(tbl->it_map);
+ }
+
+ set_iommu_table_base(&dev->dev, newtbl);
}
spin_lock(&direct_window_list_lock);
Somewhere around here, we have:
out_remove_win:
remove_ddw(pdn, true, DIRECT64_PROPNAME);
We should replace with:
remove_ddw(pdn, true, win_name);
Fred
quoted hunk
@@ -1408,10 +1463,10 @@ static bool enable_ddw(struct pci_dev *dev, struct device_node *pdn) * as RAM, then we failed to create a window to cover persistent * memory and need to set the DMA limit. */- if (pmem_present && ddw_enabled && (len == max_ram_len))+ if (pmem_present && ddw_enabled && direct_mapping && len == max_ram_len) dev->dev.bus_dma_limit = dev->dev.archdata.dma_offset + (1ULL << len);- return ddw_enabled;+ return ddw_enabled && direct_mapping; } static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
@@ -1553,7 +1608,9 @@ static int iommu_reconfig_notifier(struct notifier_block *nb, unsigned long acti * we have to remove the property when releasing * the device node. */- remove_ddw(np, false, DIRECT64_PROPNAME);+ if (remove_ddw(np, false, DIRECT64_PROPNAME))+ remove_ddw(np, false, DMA64_PROPNAME);+ if (pci && pci->table_group) iommu_pseries_free_group(pci->table_group, np->full_name);
A previous change introduced the usage of DDW as a bigger indirect DMA
mapping when the DDW available size does not map the whole partition.
As most of the code that manipulates direct mappings was reused for
indirect mappings, it's necessary to rename all names and debug/info
messages to reflect that it can be used for both kinds of mapping.
This should cause no behavioural change, just adjust naming.
Signed-off-by: Leonardo Bras <redacted>
---
@@ -369,11 +369,11 @@ struct ddw_create_response {u32addr_lo;};-staticLIST_HEAD(direct_window_list);+staticLIST_HEAD(dma_win_list);/* prevents races between memory on/offline and window creation */-staticDEFINE_SPINLOCK(direct_window_list_lock);+staticDEFINE_SPINLOCK(dma_win_list_lock);/* protects initializing window twice for same device */-staticDEFINE_MUTEX(direct_window_init_mutex);+staticDEFINE_MUTEX(dma_win_init_mutex);#define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"#define DMA64_PROPNAME "linux,dma64-ddr-window-info"
@@ -713,7 +713,10 @@ static void pci_dma_bus_setup_pSeriesLP(struct pci_bus *bus)pr_debug("pci_dma_bus_setup_pSeriesLP: setting up bus %pOF\n",dn);-/* Find nearest ibm,dma-window, walking up the device tree */+/*+*Findnearestibm,dma-window(defaultDMAwindow),walkingupthe+*devicetree+*/for(pdn=dn;pdn!=NULL;pdn=pdn->parent){dma_window=of_get_property(pdn,"ibm,dma-window",NULL);if(dma_window!=NULL)
@@ -822,11 +825,11 @@ static void remove_dma_window(struct device_node *np, u32 *ddw_avail,ret=rtas_call(ddw_avail[DDW_REMOVE_PE_DMA_WIN],1,1,NULL,liobn);if(ret)-pr_warn("%pOF: failed to remove direct window: rtas returned "+pr_warn("%pOF: failed to remove DMA window: rtas returned ""%d to ibm,remove-pe-dma-window(%x) %llx\n",np,ret,ddw_avail[DDW_REMOVE_PE_DMA_WIN],liobn);else-pr_debug("%pOF: successfully removed direct window: rtas returned "+pr_debug("%pOF: successfully removed DMA window: rtas returned ""%d to ibm,remove-pe-dma-window(%x) %llx\n",np,ret,ddw_avail[DDW_REMOVE_PE_DMA_WIN],liobn);}
@@ -854,37 +857,37 @@ static int remove_ddw(struct device_node *np, bool remove_prop, const char *win_ret=of_remove_property(np,win);if(ret)-pr_warn("%pOF: failed to remove direct window property: %d\n",+pr_warn("%pOF: failed to remove DMA window property: %d\n",np,ret);return0;}staticboolfind_existing_ddw(structdevice_node*pdn,u64*dma_addr,int*window_shift){-structdirect_window*window;-conststructdynamic_dma_window_prop*direct64;+structdma_win*window;+conststructdynamic_dma_window_prop*dma64;boolfound=false;-spin_lock(&direct_window_list_lock);+spin_lock(&dma_win_list_lock);/* check if we already created a window and dupe that config if so */-list_for_each_entry(window,&direct_window_list,list){+list_for_each_entry(window,&dma_win_list,list){if(window->device==pdn){-direct64=window->prop;-*dma_addr=be64_to_cpu(direct64->dma_base);-*window_shift=be32_to_cpu(direct64->window_shift);+dma64=window->prop;+*dma_addr=be64_to_cpu(dma64->dma_base);+*window_shift=be32_to_cpu(dma64->window_shift);found=true;break;}}-spin_unlock(&direct_window_list_lock);+spin_unlock(&dma_win_list_lock);returnfound;}-staticstructdirect_window*ddw_list_new_entry(structdevice_node*pdn,-conststructdynamic_dma_window_prop*dma64)+staticstructdma_win*ddw_list_new_entry(structdevice_node*pdn,+conststructdynamic_dma_window_prop*dma64){-structdirect_window*window;+structdma_win*window;window=kzalloc(sizeof(*window),GFP_KERNEL);if(!window)
So far it's assumed possible to map the guest RAM 1:1 to the bus, which
works with a small number of devices. SRIOV changes it as the user can
configure hundreds VFs and since phyp preallocates TCEs and does not
allow IOMMU pages bigger than 64K, it has to limit the number of TCEs
per a PE to limit waste of physical pages.
As of today, if the assumed direct mapping is not possible, DDW creation
is skipped and the default DMA window "ibm,dma-window" is used instead.
By using DDW, indirect mapping can get more TCEs than available for the
default DMA window, and also get access to using much larger pagesizes
(16MB as implemented in qemu vs 4k from default DMA window), causing a
significant increase on the maximum amount of memory that can be IOMMU
mapped at the same time.
Indirect mapping will only be used if direct mapping is not a
possibility.
For indirect mapping, it's necessary to re-create the iommu_table with
the new DMA window parameters, so iommu_alloc() can use it.
Removing the default DMA window for using DDW with indirect mapping
is only allowed if there is no current IOMMU memory allocated in
the iommu_table. enable_ddw() is aborted otherwise.
Even though there won't be both direct and indirect mappings at the
same time, we can't reuse the DIRECT64_PROPNAME property name, or else
an older kexec()ed kernel can assume direct mapping, and skip
iommu_alloc(), causing undesirable behavior.
So a new property name DMA64_PROPNAME "linux,dma64-ddr-window-info"
was created to represent a DDW that does not allow direct mapping.
Signed-off-by: Leonardo Bras <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 87 +++++++++++++++++++++-----
1 file changed, 72 insertions(+), 15 deletions(-)
struct device_node *pdn)
query.page_size);
goto out_failed;
}
- /* verify the window * number of ptes will map the partition */
- /* check largest block * page size > max memory hotplug addr */
+
/*
* The "ibm,pmemory" can appear anywhere in the address space.
* Assuming it is still backed by page structs, try
MAX_PHYSMEM_BITS
struct device_node *pdn)
if (!window)
goto out_del_prop;
- ret = walk_system_ram_range(0, memblock_end_of_DRAM() >> PAGE_SHIFT,
- win64->value, tce_setrange_multi_pSeriesLP_walk);
- if (ret) {
- dev_info(&dev->dev, "failed to map direct window for %pOF:
%d\n",
- dn, ret);
- goto out_del_list;
+ if (direct_mapping) {
+ /* DDW maps the whole partition, so enable direct DMA mapping */
+ ret = walk_system_ram_range(0, memblock_end_of_DRAM() >>
PAGE_SHIFT,
+ win64->value,
tce_setrange_multi_pSeriesLP_walk);
+ if (ret) {
+ dev_info(&dev->dev, "failed to map direct window for
%pOF: %d\n",
+ dn, ret);
+ goto out_del_list;
+ }
+ } else {
+ struct iommu_table *newtbl;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(pci->phb->mem_resources); i++) {
+ const unsigned long mask = IORESOURCE_MEM_64 |
IORESOURCE_MEM;
+
+ /* Look for MMIO32 */
+ if ((pci->phb->mem_resources[i].flags & mask) ==
IORESOURCE_MEM)
+ break;
+ }
+
+ if (i == ARRAY_SIZE(pci->phb->mem_resources))
+ goto out_del_list;
So we exit and do nothing if there's no MMIO32 bar?
Isn't the intent just to figure out the MMIO32 area to reserve it when
init'ing the table? In which case we could default to 0,0
I'm actually not clear why we are reserving this area on pseries.
If we do not reserve it, then the iommu code will allocate DMA pages
from there and these addresses are MMIO32 from the kernel pov at least.
I saw crashes when (I think) a device tried DMAing to the top 2GB of the
bus space which happened to be a some other device's BAR.
quoted
+
+ /* New table for using DDW instead of the default DMA window */
+ newtbl = iommu_pseries_alloc_table(pci->phb->node);
+ if (!newtbl) {
+ dev_dbg(&dev->dev, "couldn't create new IOMMU table\n");
+ goto out_del_list;
+ }
+
+ iommu_table_setparms_common(newtbl, pci->phb->bus->number,
create.liobn, win_addr,
+ 1UL << len, page_shift, NULL,
&iommu_table_lpar_multi_ops);
+ iommu_init_table(newtbl, pci->phb->node,
pci->phb->mem_resources[i].start,
+ pci->phb->mem_resources[i].end);
+
+ pci->table_group->tables[1] = newtbl;
+
+ /* Keep default DMA window stuct if removed */
+ if (default_win_removed) {
+ tbl->it_size = 0;
+ kfree(tbl->it_map);
+ }
+
+ set_iommu_table_base(&dev->dev, newtbl);
}
spin_lock(&direct_window_list_lock);
Somewhere around here, we have:
out_remove_win:
remove_ddw(pdn, true, DIRECT64_PROPNAME);
We should replace with:
remove_ddw(pdn, true, win_name);
True. Good spotting. Or rework remove_dma_window() to take just a liobn.
Thanks,
struct device_node *pdn)
* as RAM, then we failed to create a window to cover persistent
* memory and need to set the DMA limit.
*/
- if (pmem_present && ddw_enabled && (len == max_ram_len))
+ if (pmem_present && ddw_enabled && direct_mapping && len ==
max_ram_len)
dev->dev.bus_dma_limit = dev->dev.archdata.dma_offset +
(1ULL << len);
- return ddw_enabled;
+ return ddw_enabled && direct_mapping;
}
static void pci_dma_dev_setup_pSeriesLP(struct pci_dev *dev)
@@ -1553,7 +1608,9 @@ static int iommu_reconfig_notifier(struct
notifier_block *nb, unsigned long acti
* we have to remove the property when releasing
* the device node.
*/
- remove_ddw(np, false, DIRECT64_PROPNAME);
+ if (remove_ddw(np, false, DIRECT64_PROPNAME))
+ remove_ddw(np, false, DMA64_PROPNAME);
+
if (pci && pci->table_group)
iommu_pseries_free_group(pci->table_group,
np->full_name);
+ struct iommu_table *newtbl;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(pci->phb->mem_resources); i++) {
+ const unsigned long mask = IORESOURCE_MEM_64 |
IORESOURCE_MEM;
+
+ /* Look for MMIO32 */
+ if ((pci->phb->mem_resources[i].flags & mask) ==
IORESOURCE_MEM)
+ break;
+ }
+
+ if (i == ARRAY_SIZE(pci->phb->mem_resources))
+ goto out_del_list;
So we exit and do nothing if there's no MMIO32 bar?
Isn't the intent just to figure out the MMIO32 area to reserve it when
init'ing the table? In which case we could default to 0,0
I'm actually not clear why we are reserving this area on pseries.
If we do not reserve it, then the iommu code will allocate DMA pages
from there and these addresses are MMIO32 from the kernel pov at least.
I saw crashes when (I think) a device tried DMAing to the top 2GB of the
bus space which happened to be a some other device's BAR.
hmmm... then figuring out the correct range needs more work. We could
have more than one MMIO32 bar. And they don't have to be adjacent. I
don't see that we are reserving any range on the initial table though
(on pseries).
Fred
+ struct iommu_table *newtbl;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(pci->phb->mem_resources); i++) {
+ const unsigned long mask = IORESOURCE_MEM_64 |
IORESOURCE_MEM;
+
+ /* Look for MMIO32 */
+ if ((pci->phb->mem_resources[i].flags & mask) ==
IORESOURCE_MEM)
+ break;
+ }
+
+ if (i == ARRAY_SIZE(pci->phb->mem_resources))
+ goto out_del_list;
So we exit and do nothing if there's no MMIO32 bar?
Isn't the intent just to figure out the MMIO32 area to reserve it
when init'ing the table? In which case we could default to 0,0
I'm actually not clear why we are reserving this area on pseries.
If we do not reserve it, then the iommu code will allocate DMA pages
from there and these addresses are MMIO32 from the kernel pov at
least. I saw crashes when (I think) a device tried DMAing to the top
2GB of the bus space which happened to be a some other device's BAR.
hmmm... then figuring out the correct range needs more work. We could
have more than one MMIO32 bar. And they don't have to be adjacent.
They all have to be within the MMIO32 window of a PHB and we reserve the
entire window here.
I
don't see that we are reserving any range on the initial table though
(on pseries).
True, we did not need to, as the hypervisor always took care of DMA and
MMIO32 regions to not overlap.
And in this series we do not (strictly speaking) need this either as
phyp never allocates more than one window dynamically and that only
window is always the second one starting from 0x800.0000.0000.0000. It
is probably my mistake that KVM allows a new window to start from 0 -
PAPR did not prohibit this explicitly.
And for the KVM case, we do not need to remove the default window as KVM
can pretty much always allocate as many TCE as the VM wants. But we
still allow removing the default window and creating a huge one instead
at 0x0 as this way we can allow 1:1 for every single PCI device even if
it only allows 48 (or similar but less than 64bit) DMA. Hope this makes
sense. Thanks,
--
Alexey
I believe there's a small problem here. We jump directly to
out_remove_win if allocating the property failed. Yet, the first
thing
remove_ddw() does is look for the property. So it will never find it
and
the window is never removed by the hypervisor.
Fred
That makes sense, thanks for catching this one!
What I intended here was just removing the DDW, so I think it should be
ok replacing remove_ddw() by a new helper that only does the rtas-call.
I will send a v6 with this change soon.
quoted
+
out_failed:
if (default_win_removed)
reset_dma_window(dev, pdn);
Hello Alexey, Fred. Thanks for reviewing!
On Wed, 2021-07-21 at 13:32 +1000, Alexey Kardashevskiy wrote:
quoted
quoted
spin_lock(&direct_window_list_lock);
Somewhere around here, we have:
out_remove_win:
remove_ddw(pdn, true, DIRECT64_PROPNAME);
We should replace with:
remove_ddw(pdn, true, win_name);
True. Good spotting. Or rework remove_dma_window() to take just a
liobn.
Thanks,
Fred,
Good catch! On rebasing the last changes I did miss this one.
Due to reworking v5 06/11, I ended up with a solution that takes a
liobn instead of a win name (as suggested by Alexey), and this one is
no more :)
Hello Fred, thanks for the feedback!
On Tue, 2021-07-20 at 19:51 +0200, Frederic Barrat wrote:
On 16/07/2021 10:27, Leonardo Bras wrote:
quoted
Update remove_dma_window() so it can be used to remove DDW with a
given
property name.
This enables the creation of new property names for DDW, so we can
have different usage for it, like indirect mapping.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
Alexey, Fred:
On Fri, 2021-07-23 at 15:34 +1000, Alexey Kardashevskiy wrote:
On 22/07/2021 01:04, Frederic Barrat wrote:
quoted
On 21/07/2021 05:32, Alexey Kardashevskiy wrote:
quoted
quoted
quoted
+ struct iommu_table *newtbl;
+ int i;
+
+ for (i = 0; i < ARRAY_SIZE(pci->phb->mem_resources);
i++) {
+ const unsigned long mask = IORESOURCE_MEM_64 |
IORESOURCE_MEM;
+
+ /* Look for MMIO32 */
+ if ((pci->phb->mem_resources[i].flags & mask) ==
IORESOURCE_MEM)
+ break;
+ }
+
+ if (i == ARRAY_SIZE(pci->phb->mem_resources))
+ goto out_del_list;
So we exit and do nothing if there's no MMIO32 bar?
Isn't the intent just to figure out the MMIO32 area to reserve
it
when init'ing the table? In which case we could default to 0,0
I'm actually not clear why we are reserving this area on
pseries.
If we do not reserve it, then the iommu code will allocate DMA
pages
from there and these addresses are MMIO32 from the kernel pov at
least. I saw crashes when (I think) a device tried DMAing to the
top
2GB of the bus space which happened to be a some other device's
BAR.
hmmm... then figuring out the correct range needs more work. We
could
have more than one MMIO32 bar. And they don't have to be adjacent.
They all have to be within the MMIO32 window of a PHB and we reserve
the
entire window here.
quoted
I
don't see that we are reserving any range on the initial table
though
(on pseries).
True, we did not need to, as the hypervisor always took care of DMA
and
MMIO32 regions to not overlap.
And in this series we do not (strictly speaking) need this either as
phyp never allocates more than one window dynamically and that only
window is always the second one starting from 0x800.0000.0000.0000.
It
is probably my mistake that KVM allows a new window to start from 0 -
PAPR did not prohibit this explicitly.
And for the KVM case, we do not need to remove the default window as
KVM
can pretty much always allocate as many TCE as the VM wants. But we
still allow removing the default window and creating a huge one
instead
at 0x0 as this way we can allow 1:1 for every single PCI device even
if
it only allows 48 (or similar but less than 64bit) DMA. Hope this
makes
sense. Thanks,
Thank you for this discussion, I got to learn a lot!
If I got this, no further change will be necessary, is that correct?
I am testing a v6, and I intend to send it soon.
On Tue, 2021-08-17 at 02:59 -0300, Leonardo Brás wrote:
Hello Fred, thanks for the feedback!
On Tue, 2021-07-20 at 19:51 +0200, Frederic Barrat wrote:
quoted
On 16/07/2021 10:27, Leonardo Bras wrote:
quoted
Update remove_dma_window() so it can be used to remove DDW with a
given
property name.
This enables the creation of new property names for DDW, so we
can
have different usage for it, like indirect mapping.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 21 +++++++++++--------
--
1 file changed, 11 insertions(+), 10 deletions(-)
Why switch to returning an int? None of the callers check it.
IIRC, in a previous version it did make sense, which is not the case
anymore. I will revert this.
Thanks!
Oh, sorry about that, it is in fact still needed:
It will make sense in patch v5 10/11:
On iommu_reconfig_notifier(), if (action == OF_RECONFIG_DETACH_NODE),
we need to remove a DDW if it exists.
As there may be different window names, it tests for DIRECT64_PROPNAME,
and if it's not found, it tests for DMA64_PROPNAME.
This approach will skip scanning for DMA64_PROPNAME if
DIRECT64_PROPNAME was found, as both may not exist in the same node.
But for this approach to work we need remove_ddw() to return error if
the property is not found.
Does it make sense? or should I just test for both?
Best regards,
Leonardo Bras
On Tue, 2021-08-17 at 02:59 -0300, Leonardo Brás wrote:
quoted
Hello Fred, thanks for the feedback!
On Tue, 2021-07-20 at 19:51 +0200, Frederic Barrat wrote:
quoted
On 16/07/2021 10:27, Leonardo Bras wrote:
quoted
Update remove_dma_window() so it can be used to remove DDW with a
given
property name.
This enables the creation of new property names for DDW, so we
can
have different usage for it, like indirect mapping.
Signed-off-by: Leonardo Bras <redacted>
Reviewed-by: Alexey Kardashevskiy <redacted>
---
arch/powerpc/platforms/pseries/iommu.c | 21 +++++++++++--------
--
1 file changed, 11 insertions(+), 10 deletions(-)
Why switch to returning an int? None of the callers check it.
IIRC, in a previous version it did make sense, which is not the case
anymore. I will revert this.
Thanks!
Oh, sorry about that, it is in fact still needed:
Then you should have added it in 10/11.
It will make sense in patch v5 10/11:
On iommu_reconfig_notifier(), if (action == OF_RECONFIG_DETACH_NODE),
we need to remove a DDW if it exists.
As there may be different window names, it tests for DIRECT64_PROPNAME,
and if it's not found, it tests for DMA64_PROPNAME.
This approach will skip scanning for DMA64_PROPNAME if
DIRECT64_PROPNAME was found, as both may not exist in the same node.
But for this approach to work we need remove_ddw() to return error if
the property is not found.
Does it make sense? or should I just test for both?
Or you could just try removing both without checking the return code, it
is one extra of_find_property in very rare code path. Not worth
reposting though imho. (sorry I was off last week, catching up). Thanks,
--
Alexey