hc
2024-01-05 071106ecf68c401173c58808b1cf5f68cc50d390
kernel/arch/ia64/hp/common/sba_iommu.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 ** IA64 System Bus Adapter (SBA) I/O MMU manager
34 **
....@@ -8,10 +9,6 @@
89 ** Portions (c) 2000 Grant Grundler (from parisc I/O MMU code)
910 ** Portions (c) 1999 Dave S. Miller (from sparc64 I/O MMU code)
1011 **
11
-** This program is free software; you can redistribute it and/or modify
12
-** it under the terms of the GNU General Public License as published by
13
-** the Free Software Foundation; either version 2 of the License, or
14
-** (at your option) any later version.
1512 **
1613 **
1714 ** This module initializes the IOC (I/O Controller) found on HP
....@@ -36,8 +33,9 @@
3633 #include <linux/bitops.h> /* hweight64() */
3734 #include <linux/crash_dump.h>
3835 #include <linux/iommu-helper.h>
39
-#include <linux/dma-mapping.h>
36
+#include <linux/dma-map-ops.h>
4037 #include <linux/prefetch.h>
38
+#include <linux/swiotlb.h>
4139
4240 #include <asm/delay.h> /* ia64_get_itc() */
4341 #include <asm/io.h>
....@@ -45,8 +43,6 @@
4543 #include <asm/dma.h>
4644
4745 #include <asm/acpi-ext.h>
48
-
49
-extern int swiotlb_late_init_with_default_size (size_t size);
5046
5147 #define PFX "IOC: "
5248
....@@ -254,12 +250,8 @@
254250 static u64 prefetch_spill_page;
255251 #endif
256252
257
-#ifdef CONFIG_PCI
258
-# define GET_IOC(dev) ((dev_is_pci(dev)) \
253
+#define GET_IOC(dev) ((dev_is_pci(dev)) \
259254 ? ((struct ioc *) PCI_CONTROLLER(to_pci_dev(dev))->iommu) : NULL)
260
-#else
261
-# define GET_IOC(dev) NULL
262
-#endif
263255
264256 /*
265257 ** DMA_CHUNK_SIZE is used by the SCSI mid-layer to break up
....@@ -493,8 +485,7 @@
493485 ASSERT(((unsigned long) ioc->res_hint & (sizeof(unsigned long) - 1UL)) == 0);
494486 ASSERT(res_ptr < res_end);
495487
496
- boundary_size = (unsigned long long)dma_get_seg_boundary(dev) + 1;
497
- boundary_size = ALIGN(boundary_size, 1ULL << iovp_shift) >> iovp_shift;
488
+ boundary_size = dma_get_seg_boundary_nr_pages(dev, iovp_shift);
498489
499490 BUG_ON(ioc->ibase & ~iovp_mask);
500491 shift = ioc->ibase >> iovp_shift;
....@@ -907,14 +898,15 @@
907898 }
908899
909900 /**
910
- * sba_map_single_attrs - map one buffer and return IOVA for DMA
901
+ * sba_map_page - map one buffer and return IOVA for DMA
911902 * @dev: instance of PCI owned by the driver that's asking.
912
- * @addr: driver buffer to map.
913
- * @size: number of bytes to map in driver buffer.
914
- * @dir: R/W or both.
903
+ * @page: page to map
904
+ * @poff: offset into page
905
+ * @size: number of bytes to map
906
+ * @dir: dma direction
915907 * @attrs: optional dma attributes
916908 *
917
- * See Documentation/DMA-API-HOWTO.txt
909
+ * See Documentation/core-api/dma-api-howto.rst
918910 */
919911 static dma_addr_t sba_map_page(struct device *dev, struct page *page,
920912 unsigned long poff, size_t size,
....@@ -944,7 +936,7 @@
944936 ** Device is bit capable of DMA'ing to the buffer...
945937 ** just return the PCI address of ptr
946938 */
947
- DBG_BYPASS("sba_map_single_attrs() bypass mask/addr: "
939
+ DBG_BYPASS("sba_map_page() bypass mask/addr: "
948940 "0x%lx/0x%lx\n",
949941 to_pci_dev(dev)->dma_mask, pci_addr);
950942 return pci_addr;
....@@ -966,14 +958,14 @@
966958
967959 #ifdef ASSERT_PDIR_SANITY
968960 spin_lock_irqsave(&ioc->res_lock, flags);
969
- if (sba_check_pdir(ioc,"Check before sba_map_single_attrs()"))
961
+ if (sba_check_pdir(ioc,"Check before sba_map_page()"))
970962 panic("Sanity check failed");
971963 spin_unlock_irqrestore(&ioc->res_lock, flags);
972964 #endif
973965
974966 pide = sba_alloc_range(ioc, dev, size);
975967 if (pide < 0)
976
- return 0;
968
+ return DMA_MAPPING_ERROR;
977969
978970 iovp = (dma_addr_t) pide << iovp_shift;
979971
....@@ -997,18 +989,10 @@
997989 /* form complete address */
998990 #ifdef ASSERT_PDIR_SANITY
999991 spin_lock_irqsave(&ioc->res_lock, flags);
1000
- sba_check_pdir(ioc,"Check after sba_map_single_attrs()");
992
+ sba_check_pdir(ioc,"Check after sba_map_page()");
1001993 spin_unlock_irqrestore(&ioc->res_lock, flags);
1002994 #endif
1003995 return SBA_IOVA(ioc, iovp, offset);
1004
-}
1005
-
1006
-static dma_addr_t sba_map_single_attrs(struct device *dev, void *addr,
1007
- size_t size, enum dma_data_direction dir,
1008
- unsigned long attrs)
1009
-{
1010
- return sba_map_page(dev, virt_to_page(addr),
1011
- (unsigned long)addr & ~PAGE_MASK, size, dir, attrs);
1012996 }
1013997
1014998 #ifdef ENABLE_MARK_CLEAN
....@@ -1036,14 +1020,14 @@
10361020 #endif
10371021
10381022 /**
1039
- * sba_unmap_single_attrs - unmap one IOVA and free resources
1023
+ * sba_unmap_page - unmap one IOVA and free resources
10401024 * @dev: instance of PCI owned by the driver that's asking.
10411025 * @iova: IOVA of driver buffer previously mapped.
10421026 * @size: number of bytes mapped in driver buffer.
10431027 * @dir: R/W or both.
10441028 * @attrs: optional dma attributes
10451029 *
1046
- * See Documentation/DMA-API-HOWTO.txt
1030
+ * See Documentation/core-api/dma-api-howto.rst
10471031 */
10481032 static void sba_unmap_page(struct device *dev, dma_addr_t iova, size_t size,
10491033 enum dma_data_direction dir, unsigned long attrs)
....@@ -1063,7 +1047,7 @@
10631047 /*
10641048 ** Address does not fall w/in IOVA, must be bypassing
10651049 */
1066
- DBG_BYPASS("sba_unmap_single_attrs() bypass addr: 0x%lx\n",
1050
+ DBG_BYPASS("sba_unmap_page() bypass addr: 0x%lx\n",
10671051 iova);
10681052
10691053 #ifdef ENABLE_MARK_CLEAN
....@@ -1114,48 +1098,36 @@
11141098 #endif /* DELAYED_RESOURCE_CNT == 0 */
11151099 }
11161100
1117
-void sba_unmap_single_attrs(struct device *dev, dma_addr_t iova, size_t size,
1118
- enum dma_data_direction dir, unsigned long attrs)
1119
-{
1120
- sba_unmap_page(dev, iova, size, dir, attrs);
1121
-}
1122
-
11231101 /**
11241102 * sba_alloc_coherent - allocate/map shared mem for DMA
11251103 * @dev: instance of PCI owned by the driver that's asking.
11261104 * @size: number of bytes mapped in driver buffer.
11271105 * @dma_handle: IOVA of new buffer.
11281106 *
1129
- * See Documentation/DMA-API-HOWTO.txt
1107
+ * See Documentation/core-api/dma-api-howto.rst
11301108 */
11311109 static void *
11321110 sba_alloc_coherent(struct device *dev, size_t size, dma_addr_t *dma_handle,
11331111 gfp_t flags, unsigned long attrs)
11341112 {
1113
+ struct page *page;
11351114 struct ioc *ioc;
1115
+ int node = -1;
11361116 void *addr;
11371117
11381118 ioc = GET_IOC(dev);
11391119 ASSERT(ioc);
1140
-
11411120 #ifdef CONFIG_NUMA
1142
- {
1143
- struct page *page;
1144
-
1145
- page = alloc_pages_node(ioc->node, flags, get_order(size));
1146
- if (unlikely(!page))
1147
- return NULL;
1148
-
1149
- addr = page_address(page);
1150
- }
1151
-#else
1152
- addr = (void *) __get_free_pages(flags, get_order(size));
1121
+ node = ioc->node;
11531122 #endif
1154
- if (unlikely(!addr))
1123
+
1124
+ page = alloc_pages_node(node, flags, get_order(size));
1125
+ if (unlikely(!page))
11551126 return NULL;
11561127
1128
+ addr = page_address(page);
11571129 memset(addr, 0, size);
1158
- *dma_handle = virt_to_phys(addr);
1130
+ *dma_handle = page_to_phys(page);
11591131
11601132 #ifdef ALLOW_IOV_BYPASS
11611133 ASSERT(dev->coherent_dma_mask);
....@@ -1174,9 +1146,10 @@
11741146 * If device can't bypass or bypass is disabled, pass the 32bit fake
11751147 * device to map single to get an iova mapping.
11761148 */
1177
- *dma_handle = sba_map_single_attrs(&ioc->sac_only_dev->dev, addr,
1178
- size, 0, 0);
1179
-
1149
+ *dma_handle = sba_map_page(&ioc->sac_only_dev->dev, page, 0, size,
1150
+ DMA_BIDIRECTIONAL, 0);
1151
+ if (dma_mapping_error(dev, *dma_handle))
1152
+ return NULL;
11801153 return addr;
11811154 }
11821155
....@@ -1188,12 +1161,12 @@
11881161 * @vaddr: virtual address IOVA of "consistent" buffer.
11891162 * @dma_handler: IO virtual address of "consistent" buffer.
11901163 *
1191
- * See Documentation/DMA-API-HOWTO.txt
1164
+ * See Documentation/core-api/dma-api-howto.rst
11921165 */
11931166 static void sba_free_coherent(struct device *dev, size_t size, void *vaddr,
11941167 dma_addr_t dma_handle, unsigned long attrs)
11951168 {
1196
- sba_unmap_single_attrs(dev, dma_handle, size, 0, 0);
1169
+ sba_unmap_page(dev, dma_handle, size, 0, 0);
11971170 free_pages((unsigned long) vaddr, get_order(size));
11981171 }
11991172
....@@ -1451,7 +1424,7 @@
14511424 * @dir: R/W or both.
14521425 * @attrs: optional dma attributes
14531426 *
1454
- * See Documentation/DMA-API-HOWTO.txt
1427
+ * See Documentation/core-api/dma-api-howto.rst
14551428 */
14561429 static int sba_map_sg_attrs(struct device *dev, struct scatterlist *sglist,
14571430 int nents, enum dma_data_direction dir,
....@@ -1483,7 +1456,10 @@
14831456 /* Fast path single entry scatterlists. */
14841457 if (nents == 1) {
14851458 sglist->dma_length = sglist->length;
1486
- sglist->dma_address = sba_map_single_attrs(dev, sba_sg_address(sglist), sglist->length, dir, attrs);
1459
+ sglist->dma_address = sba_map_page(dev, sg_page(sglist),
1460
+ sglist->offset, sglist->length, dir, attrs);
1461
+ if (dma_mapping_error(dev, sglist->dma_address))
1462
+ return 0;
14871463 return 1;
14881464 }
14891465
....@@ -1547,7 +1523,7 @@
15471523 * @dir: R/W or both.
15481524 * @attrs: optional dma attributes
15491525 *
1550
- * See Documentation/DMA-API-HOWTO.txt
1526
+ * See Documentation/core-api/dma-api-howto.rst
15511527 */
15521528 static void sba_unmap_sg_attrs(struct device *dev, struct scatterlist *sglist,
15531529 int nents, enum dma_data_direction dir,
....@@ -1572,8 +1548,8 @@
15721548
15731549 while (nents && sglist->dma_length) {
15741550
1575
- sba_unmap_single_attrs(dev, sglist->dma_address,
1576
- sglist->dma_length, dir, attrs);
1551
+ sba_unmap_page(dev, sglist->dma_address, sglist->dma_length,
1552
+ dir, attrs);
15771553 sglist = sg_next(sglist);
15781554 nents--;
15791555 }
....@@ -1759,9 +1735,7 @@
17591735 controller->iommu = ioc;
17601736 sac->sysdata = controller;
17611737 sac->dma_mask = 0xFFFFFFFFUL;
1762
-#ifdef CONFIG_PCI
17631738 sac->dev.bus = &pci_bus_type;
1764
-#endif
17651739 ioc->sac_only_dev = sac;
17661740 }
17671741
....@@ -2080,29 +2054,37 @@
20802054 /* This has to run before acpi_scan_init(). */
20812055 arch_initcall(acpi_sba_ioc_init_acpi);
20822056
2083
-extern const struct dma_map_ops swiotlb_dma_ops;
2057
+static int sba_dma_supported (struct device *dev, u64 mask)
2058
+{
2059
+ /* make sure it's at least 32bit capable */
2060
+ return ((mask & 0xFFFFFFFFUL) == 0xFFFFFFFFUL);
2061
+}
2062
+
2063
+static const struct dma_map_ops sba_dma_ops = {
2064
+ .alloc = sba_alloc_coherent,
2065
+ .free = sba_free_coherent,
2066
+ .map_page = sba_map_page,
2067
+ .unmap_page = sba_unmap_page,
2068
+ .map_sg = sba_map_sg_attrs,
2069
+ .unmap_sg = sba_unmap_sg_attrs,
2070
+ .dma_supported = sba_dma_supported,
2071
+ .mmap = dma_common_mmap,
2072
+ .get_sgtable = dma_common_get_sgtable,
2073
+ .alloc_pages = dma_common_alloc_pages,
2074
+ .free_pages = dma_common_free_pages,
2075
+};
20842076
20852077 static int __init
20862078 sba_init(void)
20872079 {
2088
- if (!ia64_platform_is("hpzx1") && !ia64_platform_is("hpzx1_swiotlb"))
2089
- return 0;
2090
-
2091
-#if defined(CONFIG_IA64_GENERIC)
2092
- /* If we are booting a kdump kernel, the sba_iommu will
2093
- * cause devices that were not shutdown properly to MCA
2094
- * as soon as they are turned back on. Our only option for
2095
- * a successful kdump kernel boot is to use the swiotlb.
2080
+ /*
2081
+ * If we are booting a kdump kernel, the sba_iommu will cause devices
2082
+ * that were not shutdown properly to MCA as soon as they are turned
2083
+ * back on. Our only option for a successful kdump kernel boot is to
2084
+ * use swiotlb.
20962085 */
2097
- if (is_kdump_kernel()) {
2098
- dma_ops = &swiotlb_dma_ops;
2099
- if (swiotlb_late_init_with_default_size(64 * (1<<20)) != 0)
2100
- panic("Unable to initialize software I/O TLB:"
2101
- " Try machvec=dig boot option");
2102
- machvec_init("dig");
2086
+ if (is_kdump_kernel())
21032087 return 0;
2104
- }
2105
-#endif
21062088
21072089 /*
21082090 * ioc_found should be populated by the acpi_sba_ioc_handler's .attach()
....@@ -2111,43 +2093,18 @@
21112093 while (ioc_found)
21122094 acpi_sba_ioc_add(ioc_found);
21132095
2114
- if (!ioc_list) {
2115
-#ifdef CONFIG_IA64_GENERIC
2116
- /*
2117
- * If we didn't find something sba_iommu can claim, we
2118
- * need to setup the swiotlb and switch to the dig machvec.
2119
- */
2120
- dma_ops = &swiotlb_dma_ops;
2121
- if (swiotlb_late_init_with_default_size(64 * (1<<20)) != 0)
2122
- panic("Unable to find SBA IOMMU or initialize "
2123
- "software I/O TLB: Try machvec=dig boot option");
2124
- machvec_init("dig");
2125
-#else
2126
- panic("Unable to find SBA IOMMU: Try a generic or DIG kernel");
2127
-#endif
2096
+ if (!ioc_list)
21282097 return 0;
2129
- }
21302098
2131
-#if defined(CONFIG_IA64_GENERIC) || defined(CONFIG_IA64_HP_ZX1_SWIOTLB)
2132
- /*
2133
- * hpzx1_swiotlb needs to have a fairly small swiotlb bounce
2134
- * buffer setup to support devices with smaller DMA masks than
2135
- * sba_iommu can handle.
2136
- */
2137
- if (ia64_platform_is("hpzx1_swiotlb")) {
2138
- extern void hwsw_init(void);
2139
-
2140
- hwsw_init();
2141
- }
2142
-#endif
2143
-
2144
-#ifdef CONFIG_PCI
21452099 {
21462100 struct pci_bus *b = NULL;
21472101 while ((b = pci_find_next_bus(b)) != NULL)
21482102 sba_connect_bus(b);
21492103 }
2150
-#endif
2104
+
2105
+ /* no need for swiotlb with the iommu */
2106
+ swiotlb_exit();
2107
+ dma_ops = &sba_dma_ops;
21512108
21522109 #ifdef CONFIG_PROC_FS
21532110 ioc_proc_init();
....@@ -2162,17 +2119,6 @@
21622119 {
21632120 reserve_sba_gart = 0;
21642121 return 1;
2165
-}
2166
-
2167
-static int sba_dma_supported (struct device *dev, u64 mask)
2168
-{
2169
- /* make sure it's at least 32bit capable */
2170
- return ((mask & 0xFFFFFFFFUL) == 0xFFFFFFFFUL);
2171
-}
2172
-
2173
-static int sba_dma_mapping_error(struct device *dev, dma_addr_t dma_addr)
2174
-{
2175
- return 0;
21762122 }
21772123
21782124 __setup("nosbagart", nosbagart);
....@@ -2199,23 +2145,3 @@
21992145 }
22002146
22012147 __setup("sbapagesize=",sba_page_override);
2202
-
2203
-const struct dma_map_ops sba_dma_ops = {
2204
- .alloc = sba_alloc_coherent,
2205
- .free = sba_free_coherent,
2206
- .map_page = sba_map_page,
2207
- .unmap_page = sba_unmap_page,
2208
- .map_sg = sba_map_sg_attrs,
2209
- .unmap_sg = sba_unmap_sg_attrs,
2210
- .sync_single_for_cpu = machvec_dma_sync_single,
2211
- .sync_sg_for_cpu = machvec_dma_sync_sg,
2212
- .sync_single_for_device = machvec_dma_sync_single,
2213
- .sync_sg_for_device = machvec_dma_sync_sg,
2214
- .dma_supported = sba_dma_supported,
2215
- .mapping_error = sba_dma_mapping_error,
2216
-};
2217
-
2218
-void sba_dma_init(void)
2219
-{
2220
- dma_ops = &sba_dma_ops;
2221
-}