xref: /freebsd/sys/dev/ioat/ioat_test.c (revision a4dc509f723944821bcfcc52005ff87c9a5dee5b)
1 /*-
2  * Copyright (C) 2012 Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/bus.h>
33 #include <sys/conf.h>
34 #include <sys/ioccom.h>
35 #include <sys/kernel.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/mutex.h>
40 #include <sys/rman.h>
41 #include <sys/sysctl.h>
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #include <machine/bus.h>
45 #include <machine/resource.h>
46 #include <machine/stdarg.h>
47 #include <vm/vm.h>
48 #include <vm/pmap.h>
49 
50 #include "ioat.h"
51 #include "ioat_hw.h"
52 #include "ioat_internal.h"
53 #include "ioat_test.h"
54 
55 #ifndef time_after
56 #define	time_after(a,b)		((long)(b) - (long)(a) < 0)
57 #endif
58 
59 MALLOC_DEFINE(M_IOAT_TEST, "ioat_test", "ioat test allocations");
60 
61 #define	IOAT_MAX_BUFS	256
62 
63 struct test_transaction {
64 	void			*buf[IOAT_MAX_BUFS];
65 	uint32_t		length;
66 	uint32_t		depth;
67 	struct ioat_test	*test;
68 	TAILQ_ENTRY(test_transaction)	entry;
69 };
70 
71 #define	IT_LOCK()	mtx_lock(&ioat_test_lk)
72 #define	IT_UNLOCK()	mtx_unlock(&ioat_test_lk)
73 #define	IT_ASSERT()	mtx_assert(&ioat_test_lk, MA_OWNED)
74 static struct mtx ioat_test_lk;
75 MTX_SYSINIT(ioat_test_lk, &ioat_test_lk, "test coordination mtx", MTX_DEF);
76 
77 static int g_thread_index = 1;
78 static struct cdev *g_ioat_cdev = NULL;
79 
80 #define	ioat_test_log(v, ...)	_ioat_test_log((v), "ioat_test: " __VA_ARGS__)
81 static inline void _ioat_test_log(int verbosity, const char *fmt, ...);
82 
83 static void
84 ioat_test_transaction_destroy(struct test_transaction *tx)
85 {
86 	int i;
87 
88 	for (i = 0; i < IOAT_MAX_BUFS; i++) {
89 		if (tx->buf[i] != NULL) {
90 			contigfree(tx->buf[i], tx->length, M_IOAT_TEST);
91 			tx->buf[i] = NULL;
92 		}
93 	}
94 
95 	free(tx, M_IOAT_TEST);
96 }
97 
98 static struct
99 test_transaction *ioat_test_transaction_create(unsigned num_buffers,
100     uint32_t buffer_size)
101 {
102 	struct test_transaction *tx;
103 	unsigned i;
104 
105 	tx = malloc(sizeof(*tx), M_IOAT_TEST, M_NOWAIT | M_ZERO);
106 	if (tx == NULL)
107 		return (NULL);
108 
109 	tx->length = buffer_size;
110 
111 	for (i = 0; i < num_buffers; i++) {
112 		tx->buf[i] = contigmalloc(buffer_size, M_IOAT_TEST, M_NOWAIT,
113 		    0, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);
114 
115 		if (tx->buf[i] == NULL) {
116 			ioat_test_transaction_destroy(tx);
117 			return (NULL);
118 		}
119 	}
120 	return (tx);
121 }
122 
123 static bool
124 ioat_compare_ok(struct test_transaction *tx)
125 {
126 	uint32_t i;
127 
128 	for (i = 0; i < tx->depth; i++) {
129 		if (memcmp(tx->buf[2*i], tx->buf[2*i+1], tx->length) != 0)
130 			return (false);
131 	}
132 	return (true);
133 }
134 
135 static void
136 ioat_dma_test_callback(void *arg)
137 {
138 	struct test_transaction *tx;
139 	struct ioat_test *test;
140 
141 	tx = arg;
142 	test = tx->test;
143 
144 	if (test->verify && !ioat_compare_ok(tx)) {
145 		ioat_test_log(0, "miscompare found\n");
146 		atomic_add_32(&test->status[IOAT_TEST_MISCOMPARE], tx->depth);
147 	} else if (!test->too_late)
148 		atomic_add_32(&test->status[IOAT_TEST_OK], tx->depth);
149 
150 	IT_LOCK();
151 	TAILQ_REMOVE(&test->pend_q, tx, entry);
152 	TAILQ_INSERT_TAIL(&test->free_q, tx, entry);
153 	wakeup(&test->free_q);
154 	IT_UNLOCK();
155 }
156 
157 static int
158 ioat_test_prealloc_memory(struct ioat_test *test, int index)
159 {
160 	uint32_t i, j, k;
161 	struct test_transaction *tx;
162 
163 	for (i = 0; i < test->transactions; i++) {
164 		tx = ioat_test_transaction_create(test->chain_depth * 2,
165 		    test->buffer_size);
166 		if (tx == NULL) {
167 			ioat_test_log(0, "tx == NULL - memory exhausted\n");
168 			test->status[IOAT_TEST_NO_MEMORY]++;
169 			return (ENOMEM);
170 		}
171 
172 		TAILQ_INSERT_HEAD(&test->free_q, tx, entry);
173 
174 		tx->test = test;
175 		tx->depth = test->chain_depth;
176 
177 		/* fill in source buffers */
178 		for (j = 0; j < (tx->length / sizeof(uint32_t)); j++) {
179 			uint32_t val = j + (index << 28);
180 
181 			for (k = 0; k < test->chain_depth; k++) {
182 				((uint32_t *)tx->buf[2*k])[j] = ~val;
183 				((uint32_t *)tx->buf[2*k+1])[j] = val;
184 			}
185 		}
186 	}
187 	return (0);
188 }
189 
190 static void
191 ioat_test_release_memory(struct ioat_test *test)
192 {
193 	struct test_transaction *tx, *s;
194 
195 	TAILQ_FOREACH_SAFE(tx, &test->free_q, entry, s)
196 		ioat_test_transaction_destroy(tx);
197 	TAILQ_INIT(&test->free_q);
198 
199 	TAILQ_FOREACH_SAFE(tx, &test->pend_q, entry, s)
200 		ioat_test_transaction_destroy(tx);
201 	TAILQ_INIT(&test->pend_q);
202 }
203 
204 static void
205 ioat_test_submit_1_tx(struct ioat_test *test, bus_dmaengine_t dma)
206 {
207 	struct test_transaction *tx;
208 	struct bus_dmadesc *desc;
209 	bus_dmaengine_callback_t cb;
210 	bus_addr_t src, dest;
211 	uint32_t i, flags;
212 
213 	IT_LOCK();
214 	while (TAILQ_EMPTY(&test->free_q))
215 		msleep(&test->free_q, &ioat_test_lk, 0, "test_submit", 0);
216 
217 	tx = TAILQ_FIRST(&test->free_q);
218 	TAILQ_REMOVE(&test->free_q, tx, entry);
219 	TAILQ_INSERT_HEAD(&test->pend_q, tx, entry);
220 	IT_UNLOCK();
221 
222 	ioat_acquire(dma);
223 	for (i = 0; i < tx->depth; i++) {
224 		src = vtophys((vm_offset_t)tx->buf[2*i]);
225 		dest = vtophys((vm_offset_t)tx->buf[2*i+1]);
226 
227 		if (i == tx->depth - 1) {
228 			cb = ioat_dma_test_callback;
229 			flags = DMA_INT_EN;
230 		} else {
231 			cb = NULL;
232 			flags = 0;
233 		}
234 
235 		desc = ioat_copy(dma, src, dest, tx->length, cb, tx, flags);
236 		if (desc == NULL)
237 			panic("Failed to allocate a ring slot "
238 			    "-- this shouldn't happen!");
239 	}
240 	ioat_release(dma);
241 }
242 
243 static void
244 ioat_dma_test(void *arg)
245 {
246 	struct ioat_test *test;
247 	bus_dmaengine_t dmaengine;
248 	uint32_t loops;
249 	int index, rc, start, end;
250 
251 	test = arg;
252 	memset(__DEVOLATILE(void *, test->status), 0, sizeof(test->status));
253 
254 	if (test->buffer_size > 1024 * 1024) {
255 		ioat_test_log(0, "Buffer size too large >1MB\n");
256 		test->status[IOAT_TEST_NO_MEMORY]++;
257 		return;
258 	}
259 
260 	if (test->chain_depth * 2 > IOAT_MAX_BUFS) {
261 		ioat_test_log(0, "Depth too large (> %u)\n",
262 		    (unsigned)IOAT_MAX_BUFS / 2);
263 		test->status[IOAT_TEST_NO_MEMORY]++;
264 		return;
265 	}
266 
267 	if (btoc((uint64_t)test->buffer_size * test->chain_depth *
268 	    test->transactions) > (physmem / 4)) {
269 		ioat_test_log(0, "Sanity check failed -- test would "
270 		    "use more than 1/4 of phys mem.\n");
271 		test->status[IOAT_TEST_NO_MEMORY]++;
272 		return;
273 	}
274 
275 	if ((uint64_t)test->transactions * test->chain_depth > (1<<16)) {
276 		ioat_test_log(0, "Sanity check failed -- test would "
277 		    "use more than available IOAT ring space.\n");
278 		test->status[IOAT_TEST_NO_MEMORY]++;
279 		return;
280 	}
281 
282 	dmaengine = ioat_get_dmaengine(test->channel_index);
283 	if (dmaengine == NULL) {
284 		ioat_test_log(0, "Couldn't acquire dmaengine\n");
285 		test->status[IOAT_TEST_NO_DMA_ENGINE]++;
286 		return;
287 	}
288 
289 	index = g_thread_index++;
290 	TAILQ_INIT(&test->free_q);
291 	TAILQ_INIT(&test->pend_q);
292 
293 	if (test->duration == 0)
294 		ioat_test_log(1, "Thread %d: num_loops remaining: 0x%08x\n",
295 		    index, test->transactions);
296 	else
297 		ioat_test_log(1, "Thread %d: starting\n", index);
298 
299 	rc = ioat_test_prealloc_memory(test, index);
300 	if (rc != 0) {
301 		ioat_test_log(0, "prealloc_memory: %d\n", rc);
302 		return;
303 	}
304 	wmb();
305 
306 	test->too_late = false;
307 	start = ticks;
308 	end = start + (((sbintime_t)test->duration * hz) / 1000);
309 
310 	for (loops = 0;; loops++) {
311 		if (test->duration == 0 && loops >= test->transactions)
312 			break;
313 		else if (test->duration != 0 && time_after(ticks, end)) {
314 			test->too_late = true;
315 			break;
316 		}
317 
318 		ioat_test_submit_1_tx(test, dmaengine);
319 	}
320 
321 	ioat_test_log(1, "Test Elapsed: %d ticks (overrun %d), %d sec.\n",
322 	    ticks - start, ticks - end, (ticks - start) / hz);
323 
324 	IT_LOCK();
325 	while (!TAILQ_EMPTY(&test->pend_q))
326 		msleep(&test->free_q, &ioat_test_lk, 0, "ioattestcompl", hz);
327 	IT_UNLOCK();
328 
329 	ioat_test_log(1, "Test Elapsed2: %d ticks (overrun %d), %d sec.\n",
330 	    ticks - start, ticks - end, (ticks - start) / hz);
331 
332 	ioat_test_release_memory(test);
333 }
334 
335 static int
336 ioat_test_open(struct cdev *dev, int flags, int fmt, struct thread *td)
337 {
338 
339 	return (0);
340 }
341 
342 static int
343 ioat_test_close(struct cdev *dev, int flags, int fmt, struct thread *td)
344 {
345 
346 	return (0);
347 }
348 
349 static int
350 ioat_test_ioctl(struct cdev *dev, unsigned long cmd, caddr_t arg, int flag,
351     struct thread *td)
352 {
353 
354 	switch (cmd) {
355 	case IOAT_DMATEST:
356 		ioat_dma_test(arg);
357 		break;
358 	default:
359 		return (EINVAL);
360 	}
361 	return (0);
362 }
363 
364 static struct cdevsw ioat_cdevsw = {
365 	.d_version =	D_VERSION,
366 	.d_flags =	0,
367 	.d_open =	ioat_test_open,
368 	.d_close =	ioat_test_close,
369 	.d_ioctl =	ioat_test_ioctl,
370 	.d_name =	"ioat_test",
371 };
372 
373 static int
374 enable_ioat_test(bool enable)
375 {
376 
377 	mtx_assert(&Giant, MA_OWNED);
378 
379 	if (enable && g_ioat_cdev == NULL) {
380 		g_ioat_cdev = make_dev(&ioat_cdevsw, 0, UID_ROOT, GID_WHEEL,
381 		    0600, "ioat_test");
382 	} else if (!enable && g_ioat_cdev != NULL) {
383 		destroy_dev(g_ioat_cdev);
384 		g_ioat_cdev = NULL;
385 	}
386 	return (0);
387 }
388 
389 static int
390 sysctl_enable_ioat_test(SYSCTL_HANDLER_ARGS)
391 {
392 	int error, enabled;
393 
394 	enabled = (g_ioat_cdev != NULL);
395 	error = sysctl_handle_int(oidp, &enabled, 0, req);
396 	if (error != 0 || req->newptr == NULL)
397 		return (error);
398 
399 	enable_ioat_test(enabled);
400 	return (0);
401 }
402 SYSCTL_PROC(_hw_ioat, OID_AUTO, enable_ioat_test, CTLTYPE_INT | CTLFLAG_RW,
403     0, 0, sysctl_enable_ioat_test, "I",
404     "Non-zero: Enable the /dev/ioat_test device");
405 
406 void
407 ioat_test_attach(void)
408 {
409 	char *val;
410 
411 	val = kern_getenv("hw.ioat.enable_ioat_test");
412 	if (val != NULL && strcmp(val, "0") != 0) {
413 		mtx_lock(&Giant);
414 		enable_ioat_test(true);
415 		mtx_unlock(&Giant);
416 	}
417 	freeenv(val);
418 }
419 
420 void
421 ioat_test_detach(void)
422 {
423 
424 	mtx_lock(&Giant);
425 	enable_ioat_test(false);
426 	mtx_unlock(&Giant);
427 }
428 
429 static inline void
430 _ioat_test_log(int verbosity, const char *fmt, ...)
431 {
432 	va_list argp;
433 
434 	if (verbosity > g_ioat_debug_level)
435 		return;
436 
437 	va_start(argp, fmt);
438 	vprintf(fmt, argp);
439 	va_end(argp);
440 }
441