Merge branch 'akpm' (patches from Andrew)
[linux-2.6-microblaze.git] / kernel / dma / map_benchmark.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2020 Hisilicon Limited.
4  */
5
6 #define pr_fmt(fmt)     KBUILD_MODNAME ": " fmt
7
8 #include <linux/debugfs.h>
9 #include <linux/delay.h>
10 #include <linux/device.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/kernel.h>
13 #include <linux/kthread.h>
14 #include <linux/math64.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/timekeeping.h>
20
21 #define DMA_MAP_BENCHMARK       _IOWR('d', 1, struct map_benchmark)
22 #define DMA_MAP_MAX_THREADS     1024
23 #define DMA_MAP_MAX_SECONDS     300
24 #define DMA_MAP_MAX_TRANS_DELAY (10 * NSEC_PER_MSEC)
25
26 #define DMA_MAP_BIDIRECTIONAL   0
27 #define DMA_MAP_TO_DEVICE       1
28 #define DMA_MAP_FROM_DEVICE     2
29
30 struct map_benchmark {
31         __u64 avg_map_100ns; /* average map latency in 100ns */
32         __u64 map_stddev; /* standard deviation of map latency */
33         __u64 avg_unmap_100ns; /* as above */
34         __u64 unmap_stddev;
35         __u32 threads; /* how many threads will do map/unmap in parallel */
36         __u32 seconds; /* how long the test will last */
37         __s32 node; /* which numa node this benchmark will run on */
38         __u32 dma_bits; /* DMA addressing capability */
39         __u32 dma_dir; /* DMA data direction */
40         __u32 dma_trans_ns; /* time for DMA transmission in ns */
41         __u8 expansion[80];     /* For future use */
42 };
43
44 struct map_benchmark_data {
45         struct map_benchmark bparam;
46         struct device *dev;
47         struct dentry  *debugfs;
48         enum dma_data_direction dir;
49         atomic64_t sum_map_100ns;
50         atomic64_t sum_unmap_100ns;
51         atomic64_t sum_sq_map;
52         atomic64_t sum_sq_unmap;
53         atomic64_t loops;
54 };
55
56 static int map_benchmark_thread(void *data)
57 {
58         void *buf;
59         dma_addr_t dma_addr;
60         struct map_benchmark_data *map = data;
61         int ret = 0;
62
63         buf = (void *)__get_free_page(GFP_KERNEL);
64         if (!buf)
65                 return -ENOMEM;
66
67         while (!kthread_should_stop())  {
68                 u64 map_100ns, unmap_100ns, map_sq, unmap_sq;
69                 ktime_t map_stime, map_etime, unmap_stime, unmap_etime;
70                 ktime_t map_delta, unmap_delta;
71
72                 /*
73                  * for a non-coherent device, if we don't stain them in the
74                  * cache, this will give an underestimate of the real-world
75                  * overhead of BIDIRECTIONAL or TO_DEVICE mappings;
76                  * 66 means evertything goes well! 66 is lucky.
77                  */
78                 if (map->dir != DMA_FROM_DEVICE)
79                         memset(buf, 0x66, PAGE_SIZE);
80
81                 map_stime = ktime_get();
82                 dma_addr = dma_map_single(map->dev, buf, PAGE_SIZE, map->dir);
83                 if (unlikely(dma_mapping_error(map->dev, dma_addr))) {
84                         pr_err("dma_map_single failed on %s\n",
85                                 dev_name(map->dev));
86                         ret = -ENOMEM;
87                         goto out;
88                 }
89                 map_etime = ktime_get();
90                 map_delta = ktime_sub(map_etime, map_stime);
91
92                 /* Pretend DMA is transmitting */
93                 ndelay(map->bparam.dma_trans_ns);
94
95                 unmap_stime = ktime_get();
96                 dma_unmap_single(map->dev, dma_addr, PAGE_SIZE, map->dir);
97                 unmap_etime = ktime_get();
98                 unmap_delta = ktime_sub(unmap_etime, unmap_stime);
99
100                 /* calculate sum and sum of squares */
101
102                 map_100ns = div64_ul(map_delta,  100);
103                 unmap_100ns = div64_ul(unmap_delta, 100);
104                 map_sq = map_100ns * map_100ns;
105                 unmap_sq = unmap_100ns * unmap_100ns;
106
107                 atomic64_add(map_100ns, &map->sum_map_100ns);
108                 atomic64_add(unmap_100ns, &map->sum_unmap_100ns);
109                 atomic64_add(map_sq, &map->sum_sq_map);
110                 atomic64_add(unmap_sq, &map->sum_sq_unmap);
111                 atomic64_inc(&map->loops);
112         }
113
114 out:
115         free_page((unsigned long)buf);
116         return ret;
117 }
118
119 static int do_map_benchmark(struct map_benchmark_data *map)
120 {
121         struct task_struct **tsk;
122         int threads = map->bparam.threads;
123         int node = map->bparam.node;
124         const cpumask_t *cpu_mask = cpumask_of_node(node);
125         u64 loops;
126         int ret = 0;
127         int i;
128
129         tsk = kmalloc_array(threads, sizeof(*tsk), GFP_KERNEL);
130         if (!tsk)
131                 return -ENOMEM;
132
133         get_device(map->dev);
134
135         for (i = 0; i < threads; i++) {
136                 tsk[i] = kthread_create_on_node(map_benchmark_thread, map,
137                                 map->bparam.node, "dma-map-benchmark/%d", i);
138                 if (IS_ERR(tsk[i])) {
139                         pr_err("create dma_map thread failed\n");
140                         ret = PTR_ERR(tsk[i]);
141                         goto out;
142                 }
143
144                 if (node != NUMA_NO_NODE)
145                         kthread_bind_mask(tsk[i], cpu_mask);
146         }
147
148         /* clear the old value in the previous benchmark */
149         atomic64_set(&map->sum_map_100ns, 0);
150         atomic64_set(&map->sum_unmap_100ns, 0);
151         atomic64_set(&map->sum_sq_map, 0);
152         atomic64_set(&map->sum_sq_unmap, 0);
153         atomic64_set(&map->loops, 0);
154
155         for (i = 0; i < threads; i++) {
156                 get_task_struct(tsk[i]);
157                 wake_up_process(tsk[i]);
158         }
159
160         msleep_interruptible(map->bparam.seconds * 1000);
161
162         /* wait for the completion of benchmark threads */
163         for (i = 0; i < threads; i++) {
164                 ret = kthread_stop(tsk[i]);
165                 if (ret)
166                         goto out;
167         }
168
169         loops = atomic64_read(&map->loops);
170         if (likely(loops > 0)) {
171                 u64 map_variance, unmap_variance;
172                 u64 sum_map = atomic64_read(&map->sum_map_100ns);
173                 u64 sum_unmap = atomic64_read(&map->sum_unmap_100ns);
174                 u64 sum_sq_map = atomic64_read(&map->sum_sq_map);
175                 u64 sum_sq_unmap = atomic64_read(&map->sum_sq_unmap);
176
177                 /* average latency */
178                 map->bparam.avg_map_100ns = div64_u64(sum_map, loops);
179                 map->bparam.avg_unmap_100ns = div64_u64(sum_unmap, loops);
180
181                 /* standard deviation of latency */
182                 map_variance = div64_u64(sum_sq_map, loops) -
183                                 map->bparam.avg_map_100ns *
184                                 map->bparam.avg_map_100ns;
185                 unmap_variance = div64_u64(sum_sq_unmap, loops) -
186                                 map->bparam.avg_unmap_100ns *
187                                 map->bparam.avg_unmap_100ns;
188                 map->bparam.map_stddev = int_sqrt64(map_variance);
189                 map->bparam.unmap_stddev = int_sqrt64(unmap_variance);
190         }
191
192 out:
193         for (i = 0; i < threads; i++)
194                 put_task_struct(tsk[i]);
195         put_device(map->dev);
196         kfree(tsk);
197         return ret;
198 }
199
200 static long map_benchmark_ioctl(struct file *file, unsigned int cmd,
201                 unsigned long arg)
202 {
203         struct map_benchmark_data *map = file->private_data;
204         void __user *argp = (void __user *)arg;
205         u64 old_dma_mask;
206
207         int ret;
208
209         if (copy_from_user(&map->bparam, argp, sizeof(map->bparam)))
210                 return -EFAULT;
211
212         switch (cmd) {
213         case DMA_MAP_BENCHMARK:
214                 if (map->bparam.threads == 0 ||
215                     map->bparam.threads > DMA_MAP_MAX_THREADS) {
216                         pr_err("invalid thread number\n");
217                         return -EINVAL;
218                 }
219
220                 if (map->bparam.seconds == 0 ||
221                     map->bparam.seconds > DMA_MAP_MAX_SECONDS) {
222                         pr_err("invalid duration seconds\n");
223                         return -EINVAL;
224                 }
225
226                 if (map->bparam.dma_trans_ns > DMA_MAP_MAX_TRANS_DELAY) {
227                         pr_err("invalid transmission delay\n");
228                         return -EINVAL;
229                 }
230
231                 if (map->bparam.node != NUMA_NO_NODE &&
232                     !node_possible(map->bparam.node)) {
233                         pr_err("invalid numa node\n");
234                         return -EINVAL;
235                 }
236
237                 switch (map->bparam.dma_dir) {
238                 case DMA_MAP_BIDIRECTIONAL:
239                         map->dir = DMA_BIDIRECTIONAL;
240                         break;
241                 case DMA_MAP_FROM_DEVICE:
242                         map->dir = DMA_FROM_DEVICE;
243                         break;
244                 case DMA_MAP_TO_DEVICE:
245                         map->dir = DMA_TO_DEVICE;
246                         break;
247                 default:
248                         pr_err("invalid DMA direction\n");
249                         return -EINVAL;
250                 }
251
252                 old_dma_mask = dma_get_mask(map->dev);
253
254                 ret = dma_set_mask(map->dev,
255                                    DMA_BIT_MASK(map->bparam.dma_bits));
256                 if (ret) {
257                         pr_err("failed to set dma_mask on device %s\n",
258                                 dev_name(map->dev));
259                         return -EINVAL;
260                 }
261
262                 ret = do_map_benchmark(map);
263
264                 /*
265                  * restore the original dma_mask as many devices' dma_mask are
266                  * set by architectures, acpi, busses. When we bind them back
267                  * to their original drivers, those drivers shouldn't see
268                  * dma_mask changed by benchmark
269                  */
270                 dma_set_mask(map->dev, old_dma_mask);
271                 break;
272         default:
273                 return -EINVAL;
274         }
275
276         if (copy_to_user(argp, &map->bparam, sizeof(map->bparam)))
277                 return -EFAULT;
278
279         return ret;
280 }
281
282 static const struct file_operations map_benchmark_fops = {
283         .open                   = simple_open,
284         .unlocked_ioctl         = map_benchmark_ioctl,
285 };
286
287 static void map_benchmark_remove_debugfs(void *data)
288 {
289         struct map_benchmark_data *map = (struct map_benchmark_data *)data;
290
291         debugfs_remove(map->debugfs);
292 }
293
294 static int __map_benchmark_probe(struct device *dev)
295 {
296         struct dentry *entry;
297         struct map_benchmark_data *map;
298         int ret;
299
300         map = devm_kzalloc(dev, sizeof(*map), GFP_KERNEL);
301         if (!map)
302                 return -ENOMEM;
303         map->dev = dev;
304
305         ret = devm_add_action(dev, map_benchmark_remove_debugfs, map);
306         if (ret) {
307                 pr_err("Can't add debugfs remove action\n");
308                 return ret;
309         }
310
311         /*
312          * we only permit a device bound with this driver, 2nd probe
313          * will fail
314          */
315         entry = debugfs_create_file("dma_map_benchmark", 0600, NULL, map,
316                         &map_benchmark_fops);
317         if (IS_ERR(entry))
318                 return PTR_ERR(entry);
319         map->debugfs = entry;
320
321         return 0;
322 }
323
324 static int map_benchmark_platform_probe(struct platform_device *pdev)
325 {
326         return __map_benchmark_probe(&pdev->dev);
327 }
328
329 static struct platform_driver map_benchmark_platform_driver = {
330         .driver         = {
331                 .name   = "dma_map_benchmark",
332         },
333         .probe = map_benchmark_platform_probe,
334 };
335
336 static int
337 map_benchmark_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
338 {
339         return __map_benchmark_probe(&pdev->dev);
340 }
341
342 static struct pci_driver map_benchmark_pci_driver = {
343         .name   = "dma_map_benchmark",
344         .probe  = map_benchmark_pci_probe,
345 };
346
347 static int __init map_benchmark_init(void)
348 {
349         int ret;
350
351         ret = pci_register_driver(&map_benchmark_pci_driver);
352         if (ret)
353                 return ret;
354
355         ret = platform_driver_register(&map_benchmark_platform_driver);
356         if (ret) {
357                 pci_unregister_driver(&map_benchmark_pci_driver);
358                 return ret;
359         }
360
361         return 0;
362 }
363
364 static void __exit map_benchmark_cleanup(void)
365 {
366         platform_driver_unregister(&map_benchmark_platform_driver);
367         pci_unregister_driver(&map_benchmark_pci_driver);
368 }
369
370 module_init(map_benchmark_init);
371 module_exit(map_benchmark_cleanup);
372
373 MODULE_AUTHOR("Barry Song <song.bao.hua@hisilicon.com>");
374 MODULE_DESCRIPTION("dma_map benchmark driver");
375 MODULE_LICENSE("GPL");