1 #include <linux/device.h>
2 #include <linux/dma-map-ops.h>
3 #include <linux/dma-mapping.h>
4 #include <linux/dmapool.h>
5 #include <linux/kernel.h>
6 #include <linux/ktime.h>
7 #include <linux/module.h>
11 struct dma_pool_pair {
16 struct dmapool_parms {
22 static const struct dmapool_parms pool_parms[] = {
23 { .size = 16, .align = 16, .boundary = 0 },
24 { .size = 64, .align = 64, .boundary = 0 },
25 { .size = 256, .align = 256, .boundary = 0 },
26 { .size = 1024, .align = 1024, .boundary = 0 },
27 { .size = 4096, .align = 4096, .boundary = 0 },
28 { .size = 68, .align = 32, .boundary = 4096 },
31 static struct dma_pool *pool;
32 static struct device test_dev;
35 static inline int nr_blocks(int size)
37 return clamp_t(int, (PAGE_SIZE / size) * 512, 1024, 8192);
40 static int dmapool_test_alloc(struct dma_pool_pair *p, int blocks)
44 for (i = 0; i < blocks; i++) {
45 p[i].v = dma_pool_alloc(pool, GFP_KERNEL,
51 for (i = 0; i < blocks; i++)
52 dma_pool_free(pool, p[i].v, p[i].dma);
57 for (--i; i >= 0; i--)
58 dma_pool_free(pool, p[i].v, p[i].dma);
62 static int dmapool_test_block(const struct dmapool_parms *parms)
64 int blocks = nr_blocks(parms->size);
65 ktime_t start_time, end_time;
66 struct dma_pool_pair *p;
69 p = kcalloc(blocks, sizeof(*p), GFP_KERNEL);
73 pool = dma_pool_create("test pool", &test_dev, parms->size,
74 parms->align, parms->boundary);
80 start_time = ktime_get();
81 for (i = 0; i < NR_TESTS; i++) {
82 ret = dmapool_test_alloc(p, blocks);
88 end_time = ktime_get();
90 printk("dmapool test: size:%-4zu align:%-4zu blocks:%-4d time:%llu\n",
91 parms->size, parms->align, blocks,
92 ktime_us_delta(end_time, start_time));
95 dma_pool_destroy(pool);
101 static void dmapool_test_release(struct device *dev)
105 static int dmapool_checks(void)
109 ret = dev_set_name(&test_dev, "dmapool-test");
113 ret = device_register(&test_dev);
115 printk("%s: register failed:%d\n", __func__, ret);
119 test_dev.release = dmapool_test_release;
120 set_dma_ops(&test_dev, NULL);
121 test_dev.dma_mask = &dma_mask;
122 ret = dma_set_mask_and_coherent(&test_dev, DMA_BIT_MASK(64));
124 printk("%s: mask failed:%d\n", __func__, ret);
128 for (i = 0; i < ARRAY_SIZE(pool_parms); i++) {
129 ret = dmapool_test_block(&pool_parms[i]);
135 device_del(&test_dev);
137 put_device(&test_dev);
141 static void dmapool_exit(void)
145 module_init(dmapool_checks);
146 module_exit(dmapool_exit);
147 MODULE_LICENSE("GPL");