1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 Hisilicon Limited.
4  */
5 
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <sys/ioctl.h>
12 #include <sys/mman.h>
13 #include <linux/types.h>
14 
15 #define DMA_MAP_BENCHMARK	_IOWR('d', 1, struct map_benchmark)
16 #define DMA_MAP_MAX_THREADS	1024
17 #define DMA_MAP_MAX_SECONDS     300
18 
19 #define DMA_MAP_BIDIRECTIONAL	0
20 #define DMA_MAP_TO_DEVICE	1
21 #define DMA_MAP_FROM_DEVICE	2
22 
23 static char *directions[] = {
24 	"BIDIRECTIONAL",
25 	"TO_DEVICE",
26 	"FROM_DEVICE",
27 };
28 
29 struct map_benchmark {
30 	__u64 avg_map_100ns; /* average map latency in 100ns */
31 	__u64 map_stddev; /* standard deviation of map latency */
32 	__u64 avg_unmap_100ns; /* as above */
33 	__u64 unmap_stddev;
34 	__u32 threads; /* how many threads will do map/unmap in parallel */
35 	__u32 seconds; /* how long the test will last */
36 	__s32 node; /* which numa node this benchmark will run on */
37 	__u32 dma_bits; /* DMA addressing capability */
38 	__u32 dma_dir; /* DMA data direction */
39 	__u8 expansion[84];	/* For future use */
40 };
41 
42 int main(int argc, char **argv)
43 {
44 	struct map_benchmark map;
45 	int fd, opt;
46 	/* default single thread, run 20 seconds on NUMA_NO_NODE */
47 	int threads = 1, seconds = 20, node = -1;
48 	/* default dma mask 32bit, bidirectional DMA */
49 	int bits = 32, dir = DMA_MAP_BIDIRECTIONAL;
50 
51 	int cmd = DMA_MAP_BENCHMARK;
52 	char *p;
53 
54 	while ((opt = getopt(argc, argv, "t:s:n:b:d:")) != -1) {
55 		switch (opt) {
56 		case 't':
57 			threads = atoi(optarg);
58 			break;
59 		case 's':
60 			seconds = atoi(optarg);
61 			break;
62 		case 'n':
63 			node = atoi(optarg);
64 			break;
65 		case 'b':
66 			bits = atoi(optarg);
67 			break;
68 		case 'd':
69 			dir = atoi(optarg);
70 			break;
71 		default:
72 			return -1;
73 		}
74 	}
75 
76 	if (threads <= 0 || threads > DMA_MAP_MAX_THREADS) {
77 		fprintf(stderr, "invalid number of threads, must be in 1-%d\n",
78 			DMA_MAP_MAX_THREADS);
79 		exit(1);
80 	}
81 
82 	if (seconds <= 0 || seconds > DMA_MAP_MAX_SECONDS) {
83 		fprintf(stderr, "invalid number of seconds, must be in 1-%d\n",
84 			DMA_MAP_MAX_SECONDS);
85 		exit(1);
86 	}
87 
88 	/* suppose the mininum DMA zone is 1MB in the world */
89 	if (bits < 20 || bits > 64) {
90 		fprintf(stderr, "invalid dma mask bit, must be in 20-64\n");
91 		exit(1);
92 	}
93 
94 	if (dir != DMA_MAP_BIDIRECTIONAL && dir != DMA_MAP_TO_DEVICE &&
95 			dir != DMA_MAP_FROM_DEVICE) {
96 		fprintf(stderr, "invalid dma direction\n");
97 		exit(1);
98 	}
99 
100 	fd = open("/sys/kernel/debug/dma_map_benchmark", O_RDWR);
101 	if (fd == -1) {
102 		perror("open");
103 		exit(1);
104 	}
105 
106 	memset(&map, 0, sizeof(map));
107 	map.seconds = seconds;
108 	map.threads = threads;
109 	map.node = node;
110 	map.dma_bits = bits;
111 	map.dma_dir = dir;
112 	if (ioctl(fd, cmd, &map)) {
113 		perror("ioctl");
114 		exit(1);
115 	}
116 
117 	printf("dma mapping benchmark: threads:%d seconds:%d node:%d dir:%s\n",
118 			threads, seconds, node, dir[directions]);
119 	printf("average map latency(us):%.1f standard deviation:%.1f\n",
120 			map.avg_map_100ns/10.0, map.map_stddev/10.0);
121 	printf("average unmap latency(us):%.1f standard deviation:%.1f\n",
122 			map.avg_unmap_100ns/10.0, map.unmap_stddev/10.0);
123 
124 	return 0;
125 }
126