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 NSEC_PER_MSEC	1000000L
16 
17 #define DMA_MAP_BENCHMARK	_IOWR('d', 1, struct map_benchmark)
18 #define DMA_MAP_MAX_THREADS	1024
19 #define DMA_MAP_MAX_SECONDS     300
20 #define DMA_MAP_MAX_TRANS_DELAY	(10 * NSEC_PER_MSEC)
21 
22 #define DMA_MAP_BIDIRECTIONAL	0
23 #define DMA_MAP_TO_DEVICE	1
24 #define DMA_MAP_FROM_DEVICE	2
25 
26 static char *directions[] = {
27 	"BIDIRECTIONAL",
28 	"TO_DEVICE",
29 	"FROM_DEVICE",
30 };
31 
32 struct map_benchmark {
33 	__u64 avg_map_100ns; /* average map latency in 100ns */
34 	__u64 map_stddev; /* standard deviation of map latency */
35 	__u64 avg_unmap_100ns; /* as above */
36 	__u64 unmap_stddev;
37 	__u32 threads; /* how many threads will do map/unmap in parallel */
38 	__u32 seconds; /* how long the test will last */
39 	__s32 node; /* which numa node this benchmark will run on */
40 	__u32 dma_bits; /* DMA addressing capability */
41 	__u32 dma_dir; /* DMA data direction */
42 	__u32 dma_trans_ns; /* time for DMA transmission in ns */
43 	__u32 granule; /* how many PAGE_SIZE will do map/unmap once a time */
44 	__u8 expansion[76];	/* For future use */
45 };
46 
47 int main(int argc, char **argv)
48 {
49 	struct map_benchmark map;
50 	int fd, opt;
51 	/* default single thread, run 20 seconds on NUMA_NO_NODE */
52 	int threads = 1, seconds = 20, node = -1;
53 	/* default dma mask 32bit, bidirectional DMA */
54 	int bits = 32, xdelay = 0, dir = DMA_MAP_BIDIRECTIONAL;
55 	/* default granule 1 PAGESIZE */
56 	int granule = 1;
57 
58 	int cmd = DMA_MAP_BENCHMARK;
59 	char *p;
60 
61 	while ((opt = getopt(argc, argv, "t:s:n:b:d:x:g:")) != -1) {
62 		switch (opt) {
63 		case 't':
64 			threads = atoi(optarg);
65 			break;
66 		case 's':
67 			seconds = atoi(optarg);
68 			break;
69 		case 'n':
70 			node = atoi(optarg);
71 			break;
72 		case 'b':
73 			bits = atoi(optarg);
74 			break;
75 		case 'd':
76 			dir = atoi(optarg);
77 			break;
78 		case 'x':
79 			xdelay = atoi(optarg);
80 			break;
81 		case 'g':
82 			granule = atoi(optarg);
83 			break;
84 		default:
85 			return -1;
86 		}
87 	}
88 
89 	if (threads <= 0 || threads > DMA_MAP_MAX_THREADS) {
90 		fprintf(stderr, "invalid number of threads, must be in 1-%d\n",
91 			DMA_MAP_MAX_THREADS);
92 		exit(1);
93 	}
94 
95 	if (seconds <= 0 || seconds > DMA_MAP_MAX_SECONDS) {
96 		fprintf(stderr, "invalid number of seconds, must be in 1-%d\n",
97 			DMA_MAP_MAX_SECONDS);
98 		exit(1);
99 	}
100 
101 	if (xdelay < 0 || xdelay > DMA_MAP_MAX_TRANS_DELAY) {
102 		fprintf(stderr, "invalid transmit delay, must be in 0-%ld\n",
103 			DMA_MAP_MAX_TRANS_DELAY);
104 		exit(1);
105 	}
106 
107 	/* suppose the mininum DMA zone is 1MB in the world */
108 	if (bits < 20 || bits > 64) {
109 		fprintf(stderr, "invalid dma mask bit, must be in 20-64\n");
110 		exit(1);
111 	}
112 
113 	if (dir != DMA_MAP_BIDIRECTIONAL && dir != DMA_MAP_TO_DEVICE &&
114 			dir != DMA_MAP_FROM_DEVICE) {
115 		fprintf(stderr, "invalid dma direction\n");
116 		exit(1);
117 	}
118 
119 	if (granule < 1 || granule > 1024) {
120 		fprintf(stderr, "invalid granule size\n");
121 		exit(1);
122 	}
123 
124 	fd = open("/sys/kernel/debug/dma_map_benchmark", O_RDWR);
125 	if (fd == -1) {
126 		perror("open");
127 		exit(1);
128 	}
129 
130 	memset(&map, 0, sizeof(map));
131 	map.seconds = seconds;
132 	map.threads = threads;
133 	map.node = node;
134 	map.dma_bits = bits;
135 	map.dma_dir = dir;
136 	map.dma_trans_ns = xdelay;
137 	map.granule = granule;
138 
139 	if (ioctl(fd, cmd, &map)) {
140 		perror("ioctl");
141 		exit(1);
142 	}
143 
144 	printf("dma mapping benchmark: threads:%d seconds:%d node:%d dir:%s granule: %d\n",
145 			threads, seconds, node, dir[directions], granule);
146 	printf("average map latency(us):%.1f standard deviation:%.1f\n",
147 			map.avg_map_100ns/10.0, map.map_stddev/10.0);
148 	printf("average unmap latency(us):%.1f standard deviation:%.1f\n",
149 			map.avg_unmap_100ns/10.0, map.unmap_stddev/10.0);
150 
151 	return 0;
152 }
153