iommu/iommu-debug: Make debug driver 32 bit compatible

Currently iommu-debug was enable only for 64 bit
targets as there were compilation issues coming
up while enabling it for 32 bit. Fix these issues
to ensure that we can use it on 32 bit targets
too.

Change-Id: If230af86f2fdd12fb5396f3b4736e57a123422bd
Signed-off-by: Susheel Khiani <skhiani@codeaurora.org>
Signed-off-by: Patrick Daly <pdaly@codeaurora.org>
diff --git a/drivers/iommu/iommu-debug.c b/drivers/iommu/iommu-debug.c
index d6f1346..6562cb6 100644
--- a/drivers/iommu/iommu-debug.c
+++ b/drivers/iommu/iommu-debug.c
@@ -28,6 +28,8 @@
 #include <asm/cacheflush.h>
 #include <asm/dma-iommu.h>
 
+#if defined(CONFIG_IOMMU_DEBUG_TRACKING) || defined(CONFIG_IOMMU_TESTS)
+
 static const char *iommu_debug_attr_to_string(enum iommu_attr attr)
 {
 	switch (attr) {
@@ -71,6 +73,7 @@
 		return "Unknown attr!";
 	}
 }
+#endif
 
 #ifdef CONFIG_IOMMU_DEBUG_TRACKING
 
@@ -435,6 +438,20 @@
 
 #ifdef CONFIG_IOMMU_TESTS
 
+#ifdef CONFIG_64BIT
+
+#define kstrtoux kstrtou64
+#define kstrtox_from_user kstrtoll_from_user
+#define kstrtosize_t kstrtoul
+
+#else
+
+#define kstrtoux kstrtou32
+#define kstrtox_from_user kstrtoint_from_user
+#define kstrtosize_t kstrtouint
+
+#endif
+
 static LIST_HEAD(iommu_debug_devices);
 static struct dentry *debugfs_tests_dir;
 static u32 iters_per_op = 1;
@@ -530,10 +547,10 @@
 static void iommu_debug_device_profiling(struct seq_file *s, struct device *dev,
 					 enum iommu_attr attrs[],
 					 void *attr_values[], int nattrs,
-					 const unsigned long sizes[])
+					 const size_t sizes[])
 {
 	int i;
-	const unsigned long *sz;
+	const size_t *sz;
 	struct iommu_domain *domain;
 	unsigned long iova = 0x10000;
 	phys_addr_t paddr = 0xa000;
@@ -569,7 +586,7 @@
 	seq_printf(s, "(average over %d iterations)\n", iters_per_op);
 	seq_printf(s, "%8s %19s %16s\n", "size", "iommu_map", "iommu_unmap");
 	for (sz = sizes; *sz; ++sz) {
-		unsigned long size = *sz;
+		size_t size = *sz;
 		size_t unmapped;
 		u64 map_elapsed_ns = 0, unmap_elapsed_ns = 0;
 		u64 map_elapsed_us = 0, unmap_elapsed_us = 0;
@@ -601,8 +618,10 @@
 			unmap_elapsed_ns += timespec_to_ns(&diff);
 		}
 
-		map_elapsed_ns /= iters_per_op;
-		unmap_elapsed_ns /= iters_per_op;
+		map_elapsed_ns = div_u64_rem(map_elapsed_ns, iters_per_op,
+				&map_elapsed_rem);
+		unmap_elapsed_ns = div_u64_rem(unmap_elapsed_ns, iters_per_op,
+				&unmap_elapsed_rem);
 
 		map_elapsed_us = div_u64_rem(map_elapsed_ns, 1000,
 						&map_elapsed_rem);
@@ -618,7 +637,7 @@
 	seq_putc(s, '\n');
 	seq_printf(s, "%8s %19s %16s\n", "size", "iommu_map_sg", "iommu_unmap");
 	for (sz = sizes; *sz; ++sz) {
-		unsigned long size = *sz;
+		size_t size = *sz;
 		size_t unmapped;
 		u64 map_elapsed_ns = 0, unmap_elapsed_ns = 0;
 		u64 map_elapsed_us = 0, unmap_elapsed_us = 0;
@@ -659,8 +678,10 @@
 			unmap_elapsed_ns += timespec_to_ns(&diff);
 		}
 
-		map_elapsed_ns /= iters_per_op;
-		unmap_elapsed_ns /= iters_per_op;
+		map_elapsed_ns = div_u64_rem(map_elapsed_ns, iters_per_op,
+				&map_elapsed_rem);
+		unmap_elapsed_ns = div_u64_rem(unmap_elapsed_ns, iters_per_op,
+				&unmap_elapsed_rem);
 
 		map_elapsed_us = div_u64_rem(map_elapsed_ns, 1000,
 						&map_elapsed_rem);
@@ -685,7 +706,7 @@
 static int iommu_debug_profiling_show(struct seq_file *s, void *ignored)
 {
 	struct iommu_debug_device *ddev = s->private;
-	const unsigned long sizes[] = { SZ_4K, SZ_64K, SZ_2M, SZ_1M * 12,
+	const size_t sizes[] = { SZ_4K, SZ_64K, SZ_2M, SZ_1M * 12,
 					SZ_1M * 20, 0 };
 	enum iommu_attr attrs[] = {
 		DOMAIN_ATTR_ATOMIC,
@@ -714,7 +735,7 @@
 static int iommu_debug_secure_profiling_show(struct seq_file *s, void *ignored)
 {
 	struct iommu_debug_device *ddev = s->private;
-	const unsigned long sizes[] = { SZ_4K, SZ_64K, SZ_2M, SZ_1M * 12,
+	const size_t sizes[] = { SZ_4K, SZ_64K, SZ_2M, SZ_1M * 12,
 					SZ_1M * 20, 0 };
 
 	enum iommu_attr attrs[] = {
@@ -799,7 +820,7 @@
 	if (!virt)
 		goto out;
 
-	mapping = arm_iommu_create_mapping(&platform_bus_type, 0, SZ_1G * 4ULL);
+	mapping = arm_iommu_create_mapping(&platform_bus_type, 0, SZ_1G * 4UL);
 	if (!mapping) {
 		seq_puts(s, "fast_smmu_create_mapping failed\n");
 		goto out_kfree;
@@ -820,7 +841,7 @@
 		goto out_detach;
 	}
 	for (experiment = 0; experiment < 2; ++experiment) {
-		u64 map_avg = 0, unmap_avg = 0;
+		size_t map_avg = 0, unmap_avg = 0;
 
 		for (i = 0; i < 10; ++i) {
 			struct timespec tbefore, tafter, diff;
@@ -857,7 +878,7 @@
 				   i < 9 ? ", " : "");
 		}
 		map_avg /= 10;
-		seq_printf(s, "] (avg: %llu)\n", map_avg);
+		seq_printf(s, "] (avg: %zu)\n", map_avg);
 
 		seq_printf(s, "%13s %24s (ns): [", extra_labels[experiment],
 			   "dma_unmap_single_attrs");
@@ -867,7 +888,7 @@
 				   i < 9 ? ", " : "");
 		}
 		unmap_avg /= 10;
-		seq_printf(s, "] (avg: %llu)\n", unmap_avg);
+		seq_printf(s, "] (avg: %zu)\n", unmap_avg);
 	}
 
 out_disable_config_clocks:
@@ -1351,7 +1372,7 @@
 	int ret = -EINVAL, fast = 1;
 	phys_addr_t pt_phys;
 
-	mapping = arm_iommu_create_mapping(&platform_bus_type, 0, SZ_1G * 4ULL);
+	mapping = arm_iommu_create_mapping(&platform_bus_type, 0, SZ_1G * 4UL);
 	if (!mapping)
 		goto out;
 
@@ -1586,7 +1607,7 @@
 	struct iommu_debug_device *ddev = file->private_data;
 	dma_addr_t iova;
 
-	if (kstrtoll_from_user(ubuf, count, 0, &iova)) {
+	if (kstrtox_from_user(ubuf, count, 0, &iova)) {
 		pr_err("Invalid format for iova\n");
 		ddev->iova = 0;
 		return -EINVAL;
@@ -1685,13 +1706,13 @@
 	/* split up the words */
 	*comma1 = *comma2 = *comma3 = '\0';
 
-	if (kstrtou64(buf, 0, &iova))
+	if (kstrtoux(buf, 0, &iova))
 		goto invalid_format;
 
-	if (kstrtou64(comma1 + 1, 0, &phys))
+	if (kstrtoux(comma1 + 1, 0, &phys))
 		goto invalid_format;
 
-	if (kstrtoul(comma2 + 1, 0, &size))
+	if (kstrtosize_t(comma2 + 1, 0, &size))
 		goto invalid_format;
 
 	if (kstrtoint(comma3 + 1, 0, &prot))
@@ -1757,10 +1778,10 @@
 	/* split up the words */
 	*comma1 = '\0';
 
-	if (kstrtou64(buf, 0, &iova))
+	if (kstrtoux(buf, 0, &iova))
 		goto invalid_format;
 
-	if (kstrtoul(comma1 + 1, 0, &size))
+	if (kstrtosize_t(comma1 + 1, 0, &size))
 		goto invalid_format;
 
 	unmapped = iommu_unmap(ddev->domain, iova, size);