map_benchmark.c 8.6 KB

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