28 #include <linux/types.h>
29 #include <linux/slab.h>
33 #include <linux/sched.h>
34 #include <linux/string.h>
35 #include <linux/pci.h>
42 #include <asm/iommu.h>
43 #include <asm/pci-bridge.h>
44 #include <asm/machdep.h>
50 #include <asm/mmzone.h>
55 static void tce_invalidate_pSeries_sw(
struct iommu_table *tbl,
78 while (start <=
end) {
79 out_be64(invalidate, start);
85 long npages,
unsigned long uaddr,
109 if (tbl->it_type & TCE_PCI_SWINV_CREATE)
110 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
115 static void tce_free_pSeries(
struct iommu_table *tbl,
long index,
long npages)
124 if (tbl->it_type & TCE_PCI_SWINV_FREE)
125 tce_invalidate_pSeries_sw(tbl, tces, tcep - 1);
128 static unsigned long tce_get_pseries(
struct iommu_table *tbl,
long index)
137 static void tce_free_pSeriesLP(
struct iommu_table*,
long,
long);
138 static void tce_freemulti_pSeriesLP(
struct iommu_table*,
long,
long);
140 static int tce_build_pSeriesLP(
struct iommu_table *tbl,
long tcenum,
141 long npages,
unsigned long uaddr,
149 long tcenum_start = tcenum, npages_start = npages;
158 rc = plpar_tce_put((
u64)tbl->it_index, (
u64)tcenum << 12, tce);
160 if (
unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
162 tce_free_pSeriesLP(tbl, tcenum_start,
163 (npages_start - (npages + 1)));
167 if (rc && printk_ratelimit()) {
168 printk(
"tce_build_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
169 printk(
"\tindex = 0x%llx\n", (
u64)tbl->it_index);
170 printk(
"\ttcenum = 0x%llx\n", (
u64)tcenum);
171 printk(
"\ttce val = 0x%llx\n", tce );
183 static int tce_buildmulti_pSeriesLP(
struct iommu_table *tbl,
long tcenum,
184 long npages,
unsigned long uaddr,
193 long tcenum_start = tcenum, npages_start = npages;
198 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
214 return tce_build_pSeriesLP(tbl, tcenum, npages, uaddr,
233 for (l = 0; l <
limit; l++) {
238 rc = plpar_tce_put_indirect((
u64)tbl->it_index,
245 }
while (npages > 0 && !rc);
249 if (
unlikely(rc == H_NOT_ENOUGH_RESOURCES)) {
251 tce_freemulti_pSeriesLP(tbl, tcenum_start,
252 (npages_start - (npages + limit)));
256 if (rc && printk_ratelimit()) {
257 printk(
"tce_buildmulti_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
258 printk(
"\tindex = 0x%llx\n", (
u64)tbl->it_index);
259 printk(
"\tnpages = 0x%llx\n", (
u64)npages);
260 printk(
"\ttce[0] val = 0x%llx\n", tcep[0]);
266 static void tce_free_pSeriesLP(
struct iommu_table *tbl,
long tcenum,
long npages)
271 rc = plpar_tce_put((
u64)tbl->it_index, (
u64)tcenum << 12, 0);
273 if (rc && printk_ratelimit()) {
274 printk(
"tce_free_pSeriesLP: plpar_tce_put failed. rc=%lld\n", rc);
275 printk(
"\tindex = 0x%llx\n", (
u64)tbl->it_index);
276 printk(
"\ttcenum = 0x%llx\n", (
u64)tcenum);
285 static void tce_freemulti_pSeriesLP(
struct iommu_table *tbl,
long tcenum,
long npages)
289 rc = plpar_tce_stuff((
u64)tbl->it_index, (
u64)tcenum << 12, 0, npages);
291 if (rc && printk_ratelimit()) {
292 printk(
"tce_freemulti_pSeriesLP: plpar_tce_stuff failed\n");
293 printk(
"\trc = %lld\n", rc);
294 printk(
"\tindex = 0x%llx\n", (
u64)tbl->it_index);
295 printk(
"\tnpages = 0x%llx\n", (
u64)npages);
300 static unsigned long tce_get_pSeriesLP(
struct iommu_table *tbl,
long tcenum)
303 unsigned long tce_ret;
305 rc = plpar_tce_get((
u64)tbl->it_index, (
u64)tcenum << 12, &tce_ret);
307 if (rc && printk_ratelimit()) {
308 printk(
"tce_get_pSeriesLP: plpar_tce_get failed. rc=%lld\n", rc);
309 printk(
"\tindex = 0x%llx\n", (
u64)tbl->it_index);
310 printk(
"\ttcenum = 0x%llx\n", (
u64)tcenum);
350 #define DIRECT64_PROPNAME "linux,direct64-ddr-window-info"
352 static int tce_clearrange_multi_pSeriesLP(
unsigned long start_pfn,
353 unsigned long num_pfn,
const void *
arg)
357 u64 tce_size, num_tce, dma_offset,
next;
367 num_tce += next & (tce_size - 1);
368 next &= ~(tce_size - 1);
371 num_tce |= tce_size - 1;
379 limit =
min_t(
long, num_tce, 512);
386 }
while (num_tce > 0 && !rc);
391 static int tce_setrange_multi_pSeriesLP(
unsigned long start_pfn,
392 unsigned long num_pfn,
const void *
arg)
395 u64 *tcep, tce_size, num_tce, dma_offset,
next, proto_tce,
liobn;
421 num_tce += next & (tce_size - 1);
422 next &= ~(tce_size - 1);
425 num_tce |= tce_size - 1;
437 for (l = 0; l <
limit; l++) {
438 tcep[
l] = proto_tce |
next;
442 rc = plpar_tce_put_indirect(liobn,
448 }
while (num_tce > 0 && !rc);
456 static int tce_setrange_multi_pSeriesLP_walk(
unsigned long start_pfn,
457 unsigned long num_pfn,
void *arg)
459 return tce_setrange_multi_pSeriesLP(start_pfn, num_pfn, arg);
469 const unsigned long *basep, *sw_inval;
476 if (basep ==
NULL || sizep ==
NULL) {
478 "missing tce entries !\n", dn->
full_name);
484 if (!is_kdump_kernel())
493 if (phb->dma_window_base_cur + phb->dma_window_size > 0x80000000ul) {
494 udbg_printf(
"PCI_DMA: Unexpected number of IOAs under this PHB.\n");
495 panic(
"PCI_DMA: Unexpected number of IOAs under this PHB.\n");
498 phb->dma_window_base_cur += phb->dma_window_size;
504 tbl->it_blocksize = 16;
505 tbl->it_type = TCE_PCI;
518 tbl->it_index = (
unsigned long)
ioremap(sw_inval[0], 8);
520 tbl->it_type = TCE_PCI_SWINV_CREATE | TCE_PCI_SWINV_FREE;
532 const void *dma_window)
540 tbl->it_blocksize = 16;
541 tbl->it_type = TCE_PCI;
555 dn = pci_bus_to_OF_node(bus);
572 while (isa_dn && isa_dn != dn)
576 of_node_put(isa_dn_orig);
579 for (children = 0, tmp = dn->
child; tmp; tmp = tmp->
sibling)
582 pr_debug(
"Children: %d\n", children);
592 pci->phb->dma_window_size = 0x80000000ul;
594 while (pci->phb->dma_window_size * children > 0x80000000ul)
595 pci->phb->dma_window_size >>= 1;
596 pr_debug(
"No ISA/IDE, window size is 0x%llx\n",
597 pci->phb->dma_window_size);
598 pci->phb->dma_window_base_cur = 0;
608 pci->phb->dma_window_size = 0x8000000ul;
609 pci->phb->dma_window_base_cur = 0x8000000ul;
614 iommu_table_setparms(pci->phb, dn, tbl);
618 pci->phb->dma_window_size = 0x80000000ul;
619 while (pci->phb->dma_window_size * children > 0x70000000ul)
620 pci->phb->dma_window_size >>= 1;
622 pr_debug(
"ISA/IDE, window size is 0x%llx\n", pci->phb->dma_window_size);
631 const void *dma_window =
NULL;
633 dn = pci_bus_to_OF_node(bus);
635 pr_debug(
"pci_dma_bus_setup_pSeriesLP: setting up bus %s\n",
639 for (pdn = dn; pdn !=
NULL; pdn = pdn->
parent) {
641 if (dma_window !=
NULL)
645 if (dma_window ==
NULL) {
646 pr_debug(
" no ibm,dma-window property !\n");
652 pr_debug(
" parent is %s, iommu_table: 0x%p\n",
655 if (!ppci->iommu_table) {
658 iommu_table_setparms_lpar(ppci->phb, pdn, tbl, dma_window);
660 pr_debug(
" created table: %p\n", ppci->iommu_table);
670 pr_debug(
"pci_dma_dev_setup_pSeries: %s\n", pci_name(dev));
672 dn = dev->
dev.of_node;
678 if (!dev->
bus->self) {
681 pr_debug(
" --> first child, no bridge. Allocating iommu table.\n");
684 iommu_table_setparms(phb, dn, tbl);
697 if (dn && PCI_DN(dn))
706 static int __init disable_ddw_setup(
char *
str)
716 static inline void __remove_ddw(
struct device_node *np,
const u32 *ddw_avail,
u64 liobn)
722 pr_warning(
"%s: failed to remove DMA window: rtas returned "
723 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
724 np->
full_name, ret, ddw_avail[2], liobn);
726 pr_debug(
"%s: successfully removed DMA window: rtas returned "
727 "%d to ibm,remove-pe-dma-window(%x) %llx\n",
728 np->
full_name, ret, ddw_avail[2], liobn);
735 const u32 *ddw_avail;
744 if (!ddw_avail || len < 3 *
sizeof(
u32) || win64->
length <
sizeof(*dwp))
751 ret = tce_clearrange_multi_pSeriesLP(0,
754 pr_warning(
"%s failed to clear tces in window.\n",
757 pr_debug(
"%s successfully cleared tces in window.\n",
760 __remove_ddw(np, ddw_avail, liobn);
765 pr_warning(
"%s: failed to remove direct window property: %d\n",
775 spin_lock(&direct_window_list_lock);
778 if (window->
device == pdn) {
779 direct64 = window->
prop;
784 spin_unlock(&direct_window_list_lock);
789 static int find_existing_ddw_windows(
void)
796 if (!firmware_has_feature(FW_FEATURE_LPAR))
804 window = kzalloc(
sizeof(*window),
GFP_KERNEL);
812 window->
prop = direct64;
813 spin_lock(&direct_window_list_lock);
814 list_add(&window->
list, &direct_window_list);
815 spin_unlock(&direct_window_list_lock);
822 static int query_ddw(
struct pci_dev *dev,
const u32 *ddw_avail,
825 struct eeh_dev *edev;
836 edev = pci_dev_to_eeh_dev(dev);
837 cfg_addr = edev->config_addr;
838 if (edev->pe_config_addr)
839 cfg_addr = edev->pe_config_addr;
840 buid = edev->phb->buid;
843 cfg_addr, BUID_HI(buid), BUID_LO(buid));
844 dev_info(&dev->
dev,
"ibm,query-pe-dma-windows(%x) %x %x %x"
845 " returned %d\n", ddw_avail[0], cfg_addr, BUID_HI(buid),
850 static int create_ddw(
struct pci_dev *dev,
const u32 *ddw_avail,
854 struct eeh_dev *edev;
865 edev = pci_dev_to_eeh_dev(dev);
866 cfg_addr = edev->config_addr;
867 if (edev->pe_config_addr)
868 cfg_addr = edev->pe_config_addr;
869 buid = edev->phb->buid;
873 ret =
rtas_call(ddw_avail[1], 5, 4, (
u32 *)create, cfg_addr,
874 BUID_HI(buid), BUID_LO(buid), page_shift, window_shift);
877 "ibm,create-pe-dma-window(%x) %x %x %x %x %x returned %d "
878 "(liobn = 0x%x starting addr = %x %x)\n", ddw_avail[1],
879 cfg_addr, BUID_HI(buid), BUID_LO(buid), page_shift,
885 static void restore_default_window(
struct pci_dev *dev,
886 u32 ddw_restore_token,
unsigned long liobn)
888 struct eeh_dev *edev;
899 edev = pci_dev_to_eeh_dev(dev);
900 cfg_addr = edev->config_addr;
901 if (edev->pe_config_addr)
902 cfg_addr = edev->pe_config_addr;
903 buid = edev->phb->buid;
907 BUID_HI(buid), BUID_LO(buid));
910 "ibm,reset-pe-dma-windows(%x) %x %x %x returned %d\n",
911 ddw_restore_token, cfg_addr, BUID_HI(buid), BUID_LO(buid), ret);
935 u32 ddw_restore_token = 0;
939 const void *dma_window =
NULL;
944 dma_addr = find_existing_ddw(pdn);
957 if (!ddw_avail || len < 3 *
sizeof(
u32))
968 if (ddw_extensions) {
974 switch (ddw_extensions[0]) {
977 ddw_restore_token = ddw_extensions[1];
987 if (ddw_restore_token) {
990 __remove_ddw(pdn, ddw_avail, liobn);
999 dn = pci_device_to_OF_node(dev);
1000 ret = query_ddw(dev, ddw_avail, &query);
1002 goto out_restore_window;
1010 dev_dbg(&dev->
dev,
"no free dynamic windows");
1011 goto out_restore_window;
1020 dev_dbg(&dev->
dev,
"no supported direct page size in mask %x",
1022 goto out_restore_window;
1026 max_addr = memory_hotplug_max();
1028 dev_dbg(&dev->
dev,
"can't map partiton max 0x%llx with %u "
1030 1ULL << page_shift);
1031 goto out_restore_window;
1037 "couldn't allocate property for 64bit dma window\n");
1038 goto out_restore_window;
1042 win64->
length =
sizeof(*ddwprop);
1045 "couldn't allocate property name and value\n");
1049 ret = create_ddw(dev, ddw_avail, &create, page_shift, len);
1058 dev_dbg(&dev->
dev,
"created tce table LIOBN 0x%x for %s\n",
1061 window = kzalloc(
sizeof(*window),
GFP_KERNEL);
1063 goto out_clear_window;
1066 win64->
value, tce_setrange_multi_pSeriesLP_walk);
1068 dev_info(&dev->
dev,
"failed to map direct window for %s: %d\n",
1070 goto out_free_window;
1075 dev_err(&dev->
dev,
"unable to add dma window property for %s: %d",
1077 goto out_free_window;
1081 window->
prop = ddwprop;
1082 spin_lock(&direct_window_list_lock);
1083 list_add(&window->
list, &direct_window_list);
1084 spin_unlock(&direct_window_list_lock);
1086 dma_addr = of_read_number(&create.
addr_hi, 2);
1101 if (ddw_restore_token)
1102 restore_default_window(dev, ddw_restore_token, liobn);
1113 const void *dma_window =
NULL;
1116 pr_debug(
"pci_dma_dev_setup_pSeriesLP: %s\n", pci_name(dev));
1124 dn = pci_device_to_OF_node(dev);
1127 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
1134 if (!pdn || !PCI_DN(pdn)) {
1136 "no DMA window found for pci dev=%s dn=%s\n",
1137 pci_name(dev), of_node_full_name(dn));
1143 if (!pci->iommu_table) {
1146 iommu_table_setparms_lpar(pci->phb, pdn, tbl, dma_window);
1148 pr_debug(
" created table: %p\n", pci->iommu_table);
1150 pr_debug(
" found DMA window, table: %p\n", pci->iommu_table);
1153 set_iommu_table_base(&dev->
dev, pci->iommu_table);
1158 bool ddw_enabled =
false;
1161 const void *dma_window =
NULL;
1174 dn = pci_device_to_OF_node(pdev);
1183 for (pdn = dn; pdn && PCI_DN(pdn) && !PCI_DN(pdn)->iommu_table;
1189 if (pdn && PCI_DN(pdn)) {
1190 dma_offset = enable_ddw(pdev, pdn);
1191 if (dma_offset != 0) {
1192 dev_info(dev,
"Using 64-bit direct DMA at offset %llx\n", dma_offset);
1193 set_dma_offset(dev, dma_offset);
1202 dev_info(dev,
"Restoring 32-bit DMA via iommu\n");
1224 dn = pci_device_to_OF_node(pdev);
1227 for (; dn && PCI_DN(dn) && !PCI_DN(dn)->iommu_table;
1232 if (dn && PCI_DN(dn) &&
1241 #define pci_dma_bus_setup_pSeries NULL
1242 #define pci_dma_dev_setup_pSeries NULL
1243 #define pci_dma_bus_setup_pSeriesLP NULL
1244 #define pci_dma_dev_setup_pSeriesLP NULL
1245 #define dma_set_mask_pSeriesLP NULL
1246 #define dma_get_required_mask_pSeriesLP NULL
1258 spin_lock(&direct_window_list_lock);
1260 ret |= tce_setrange_multi_pSeriesLP(arg->
start_pfn,
1264 spin_unlock(&direct_window_list_lock);
1268 spin_lock(&direct_window_list_lock);
1270 ret |= tce_clearrange_multi_pSeriesLP(arg->
start_pfn,
1274 spin_unlock(&direct_window_list_lock);
1286 .notifier_call = iommu_mem_notifier,
1289 static int iommu_reconfig_notifier(
struct notifier_block *nb,
unsigned long action,
void *node)
1291 int err = NOTIFY_OK;
1293 struct pci_dn *pci = PCI_DN(np);
1297 case PSERIES_RECONFIG_REMOVE:
1298 if (pci && pci->iommu_table)
1301 spin_lock(&direct_window_list_lock);
1303 if (window->
device == np) {
1309 spin_unlock(&direct_window_list_lock);
1329 .notifier_call = iommu_reconfig_notifier,
1338 if (firmware_has_feature(FW_FEATURE_LPAR)) {
1339 if (firmware_has_feature(FW_FEATURE_MULTITCE)) {
1340 ppc_md.tce_build = tce_buildmulti_pSeriesLP;
1341 ppc_md.tce_free = tce_freemulti_pSeriesLP;
1343 ppc_md.tce_build = tce_build_pSeriesLP;
1344 ppc_md.tce_free = tce_free_pSeriesLP;
1346 ppc_md.tce_get = tce_get_pSeriesLP;
1352 ppc_md.tce_build = tce_build_pSeries;
1353 ppc_md.tce_free = tce_free_pSeries;
1354 ppc_md.tce_get = tce_get_pseries;
1366 static int __init disable_multitce(
char *
str)
1368 if (
strcmp(str,
"off") == 0 &&
1369 firmware_has_feature(FW_FEATURE_LPAR) &&
1370 firmware_has_feature(FW_FEATURE_MULTITCE)) {
1372 ppc_md.tce_build = tce_build_pSeriesLP;
1373 ppc_md.tce_free = tce_free_pSeriesLP;
1374 powerpc_firmware_features &= ~FW_FEATURE_MULTITCE;
1379 __setup(
"multitce=", disable_multitce);