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