aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/ioat/ioat_test.c
blob: 3f88ed98a6893f8e87946564035f49e4ea28a1a4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
/*-
 * Copyright (C) 2012 Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/conf.h>
#include <sys/ioccom.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/rman.h>
#include <sys/sysctl.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <machine/stdarg.h>
#include <vm/vm.h>
#include <vm/pmap.h>

#include "ioat.h"
#include "ioat_hw.h"
#include "ioat_internal.h"
#include "ioat_test.h"

#ifndef time_after
#define	time_after(a,b)		((long)(b) - (long)(a) < 0)
#endif

MALLOC_DEFINE(M_IOAT_TEST, "ioat_test", "ioat test allocations");

#define	IOAT_MAX_BUFS	256

struct test_transaction {
	void			*buf[IOAT_MAX_BUFS];
	uint32_t		length;
	uint32_t		depth;
	struct ioat_test	*test;
	TAILQ_ENTRY(test_transaction)	entry;
};

#define	IT_LOCK()	mtx_lock(&ioat_test_lk)
#define	IT_UNLOCK()	mtx_unlock(&ioat_test_lk)
#define	IT_ASSERT()	mtx_assert(&ioat_test_lk, MA_OWNED)
static struct mtx ioat_test_lk;
MTX_SYSINIT(ioat_test_lk, &ioat_test_lk, "test coordination mtx", MTX_DEF);

static int g_thread_index = 1;
static struct cdev *g_ioat_cdev = NULL;

#define	ioat_test_log(v, ...)	_ioat_test_log((v), "ioat_test: " __VA_ARGS__)
static inline void _ioat_test_log(int verbosity, const char *fmt, ...);

static void
ioat_test_transaction_destroy(struct test_transaction *tx)
{
	int i;

	for (i = 0; i < IOAT_MAX_BUFS; i++) {
		if (tx->buf[i] != NULL) {
			contigfree(tx->buf[i], tx->length, M_IOAT_TEST);
			tx->buf[i] = NULL;
		}
	}

	free(tx, M_IOAT_TEST);
}

static struct
test_transaction *ioat_test_transaction_create(unsigned num_buffers,
    uint32_t buffer_size)
{
	struct test_transaction *tx;
	unsigned i;

	tx = malloc(sizeof(*tx), M_IOAT_TEST, M_NOWAIT | M_ZERO);
	if (tx == NULL)
		return (NULL);

	tx->length = buffer_size;

	for (i = 0; i < num_buffers; i++) {
		tx->buf[i] = contigmalloc(buffer_size, M_IOAT_TEST, M_NOWAIT,
		    0, BUS_SPACE_MAXADDR, PAGE_SIZE, 0);

		if (tx->buf[i] == NULL) {
			ioat_test_transaction_destroy(tx);
			return (NULL);
		}
	}
	return (tx);
}

static bool
ioat_compare_ok(struct test_transaction *tx)
{
	struct ioat_test *test;
	char *dst, *src;
	uint32_t i, j;

	test = tx->test;

	for (i = 0; i < tx->depth; i++) {
		dst = tx->buf[2 * i + 1];
		src = tx->buf[2 * i];

		if (test->testkind == IOAT_TEST_FILL) {
			for (j = 0; j < tx->length; j += sizeof(uint64_t)) {
				if (memcmp(src, &dst[j],
					MIN(sizeof(uint64_t), tx->length - j))
				    != 0)
					return (false);
			}
		} else if (test->testkind == IOAT_TEST_DMA)
			if (memcmp(src, dst, tx->length) != 0)
				return (false);
	}
	return (true);
}

static void
ioat_dma_test_callback(void *arg)
{
	struct test_transaction *tx;
	struct ioat_test *test;

	tx = arg;
	test = tx->test;

	if (test->verify && !ioat_compare_ok(tx)) {
		ioat_test_log(0, "miscompare found\n");
		atomic_add_32(&test->status[IOAT_TEST_MISCOMPARE], tx->depth);
	} else if (!test->too_late)
		atomic_add_32(&test->status[IOAT_TEST_OK], tx->depth);

	IT_LOCK();
	TAILQ_REMOVE(&test->pend_q, tx, entry);
	TAILQ_INSERT_TAIL(&test->free_q, tx, entry);
	wakeup(&test->free_q);
	IT_UNLOCK();
}

static int
ioat_test_prealloc_memory(struct ioat_test *test, int index)
{
	uint32_t i, j, k;
	struct test_transaction *tx;

	for (i = 0; i < test->transactions; i++) {
		tx = ioat_test_transaction_create(test->chain_depth * 2,
		    test->buffer_size);
		if (tx == NULL) {
			ioat_test_log(0, "tx == NULL - memory exhausted\n");
			test->status[IOAT_TEST_NO_MEMORY]++;
			return (ENOMEM);
		}

		TAILQ_INSERT_HEAD(&test->free_q, tx, entry);

		tx->test = test;
		tx->depth = test->chain_depth;

		/* fill in source buffers */
		for (j = 0; j < (tx->length / sizeof(uint32_t)); j++) {
			uint32_t val = j + (index << 28);

			for (k = 0; k < test->chain_depth; k++) {
				((uint32_t *)tx->buf[2*k])[j] = ~val;
				((uint32_t *)tx->buf[2*k+1])[j] = val;
			}
		}
	}
	return (0);
}

static void
ioat_test_release_memory(struct ioat_test *test)
{
	struct test_transaction *tx, *s;

	TAILQ_FOREACH_SAFE(tx, &test->free_q, entry, s)
		ioat_test_transaction_destroy(tx);
	TAILQ_INIT(&test->free_q);

	TAILQ_FOREACH_SAFE(tx, &test->pend_q, entry, s)
		ioat_test_transaction_destroy(tx);
	TAILQ_INIT(&test->pend_q);
}

static void
ioat_test_submit_1_tx(struct ioat_test *test, bus_dmaengine_t dma)
{
	struct test_transaction *tx;
	struct bus_dmadesc *desc;
	bus_dmaengine_callback_t cb;
	bus_addr_t src, dest;
	uint64_t fillpattern;
	uint32_t i, flags;

	desc = NULL;

	IT_LOCK();
	while (TAILQ_EMPTY(&test->free_q))
		msleep(&test->free_q, &ioat_test_lk, 0, "test_submit", 0);

	tx = TAILQ_FIRST(&test->free_q);
	TAILQ_REMOVE(&test->free_q, tx, entry);
	TAILQ_INSERT_HEAD(&test->pend_q, tx, entry);
	IT_UNLOCK();

	ioat_acquire(dma);
	for (i = 0; i < tx->depth; i++) {
		src = vtophys((vm_offset_t)tx->buf[2*i]);
		dest = vtophys((vm_offset_t)tx->buf[2*i+1]);

		if (i == tx->depth - 1) {
			cb = ioat_dma_test_callback;
			flags = DMA_INT_EN;
		} else {
			cb = NULL;
			flags = 0;
		}

		if (test->testkind == IOAT_TEST_DMA)
			desc = ioat_copy(dma, dest, src, tx->length, cb, tx,
			    flags);
		else if (test->testkind == IOAT_TEST_FILL) {
			fillpattern = *(uint64_t *)tx->buf[2*i];
			desc = ioat_blockfill(dma, dest, fillpattern,
			    tx->length, cb, tx, flags);
		}

		if (desc == NULL)
			panic("Failed to allocate a ring slot "
			    "-- this shouldn't happen!");
	}
	ioat_release(dma);
}

static void
ioat_dma_test(void *arg)
{
	struct ioat_test *test;
	bus_dmaengine_t dmaengine;
	uint32_t loops;
	int index, rc, start, end;

	test = arg;
	memset(__DEVOLATILE(void *, test->status), 0, sizeof(test->status));

	if (test->buffer_size > 1024 * 1024) {
		ioat_test_log(0, "Buffer size too large >1MB\n");
		test->status[IOAT_TEST_NO_MEMORY]++;
		return;
	}

	if (test->chain_depth * 2 > IOAT_MAX_BUFS) {
		ioat_test_log(0, "Depth too large (> %u)\n",
		    (unsigned)IOAT_MAX_BUFS / 2);
		test->status[IOAT_TEST_NO_MEMORY]++;
		return;
	}

	if (btoc((uint64_t)test->buffer_size * test->chain_depth *
	    test->transactions) > (physmem / 4)) {
		ioat_test_log(0, "Sanity check failed -- test would "
		    "use more than 1/4 of phys mem.\n");
		test->status[IOAT_TEST_NO_MEMORY]++;
		return;
	}

	if ((uint64_t)test->transactions * test->chain_depth > (1<<16)) {
		ioat_test_log(0, "Sanity check failed -- test would "
		    "use more than available IOAT ring space.\n");
		test->status[IOAT_TEST_NO_MEMORY]++;
		return;
	}

	if (test->testkind >= IOAT_NUM_TESTKINDS) {
		ioat_test_log(0, "Invalid kind %u\n",
		    (unsigned)test->testkind);
		test->status[IOAT_TEST_INVALID_INPUT]++;
		return;
	}

	dmaengine = ioat_get_dmaengine(test->channel_index);
	if (dmaengine == NULL) {
		ioat_test_log(0, "Couldn't acquire dmaengine\n");
		test->status[IOAT_TEST_NO_DMA_ENGINE]++;
		return;
	}

	index = g_thread_index++;
	TAILQ_INIT(&test->free_q);
	TAILQ_INIT(&test->pend_q);

	if (test->duration == 0)
		ioat_test_log(1, "Thread %d: num_loops remaining: 0x%08x\n",
		    index, test->transactions);
	else
		ioat_test_log(1, "Thread %d: starting\n", index);

	rc = ioat_test_prealloc_memory(test, index);
	if (rc != 0) {
		ioat_test_log(0, "prealloc_memory: %d\n", rc);
		goto out;
	}
	wmb();

	test->too_late = false;
	start = ticks;
	end = start + (((sbintime_t)test->duration * hz) / 1000);

	for (loops = 0;; loops++) {
		if (test->duration == 0 && loops >= test->transactions)
			break;
		else if (test->duration != 0 && time_after(ticks, end)) {
			test->too_late = true;
			break;
		}

		ioat_test_submit_1_tx(test, dmaengine);
	}

	ioat_test_log(1, "Test Elapsed: %d ticks (overrun %d), %d sec.\n",
	    ticks - start, ticks - end, (ticks - start) / hz);

	IT_LOCK();
	while (!TAILQ_EMPTY(&test->pend_q))
		msleep(&test->free_q, &ioat_test_lk, 0, "ioattestcompl", hz);
	IT_UNLOCK();

	ioat_test_log(1, "Test Elapsed2: %d ticks (overrun %d), %d sec.\n",
	    ticks - start, ticks - end, (ticks - start) / hz);

	ioat_test_release_memory(test);
out:
	ioat_put_dmaengine(dmaengine);
}

static int
ioat_test_open(struct cdev *dev, int flags, int fmt, struct thread *td)
{

	return (0);
}

static int
ioat_test_close(struct cdev *dev, int flags, int fmt, struct thread *td)
{

	return (0);
}

static int
ioat_test_ioctl(struct cdev *dev, unsigned long cmd, caddr_t arg, int flag,
    struct thread *td)
{

	switch (cmd) {
	case IOAT_DMATEST:
		ioat_dma_test(arg);
		break;
	default:
		return (EINVAL);
	}
	return (0);
}

static struct cdevsw ioat_cdevsw = {
	.d_version =	D_VERSION,
	.d_flags =	0,
	.d_open =	ioat_test_open,
	.d_close =	ioat_test_close,
	.d_ioctl =	ioat_test_ioctl,
	.d_name =	"ioat_test",
};

static int
enable_ioat_test(bool enable)
{

	mtx_assert(&Giant, MA_OWNED);

	if (enable && g_ioat_cdev == NULL) {
		g_ioat_cdev = make_dev(&ioat_cdevsw, 0, UID_ROOT, GID_WHEEL,
		    0600, "ioat_test");
	} else if (!enable && g_ioat_cdev != NULL) {
		destroy_dev(g_ioat_cdev);
		g_ioat_cdev = NULL;
	}
	return (0);
}

static int
sysctl_enable_ioat_test(SYSCTL_HANDLER_ARGS)
{
	int error, enabled;

	enabled = (g_ioat_cdev != NULL);
	error = sysctl_handle_int(oidp, &enabled, 0, req);
	if (error != 0 || req->newptr == NULL)
		return (error);

	enable_ioat_test(enabled);
	return (0);
}
SYSCTL_PROC(_hw_ioat, OID_AUTO, enable_ioat_test, CTLTYPE_INT | CTLFLAG_RW,
    0, 0, sysctl_enable_ioat_test, "I",
    "Non-zero: Enable the /dev/ioat_test device");

void
ioat_test_attach(void)
{
	char *val;

	val = kern_getenv("hw.ioat.enable_ioat_test");
	if (val != NULL && strcmp(val, "0") != 0) {
		mtx_lock(&Giant);
		enable_ioat_test(true);
		mtx_unlock(&Giant);
	}
	freeenv(val);
}

void
ioat_test_detach(void)
{

	mtx_lock(&Giant);
	enable_ioat_test(false);
	mtx_unlock(&Giant);
}

static inline void
_ioat_test_log(int verbosity, const char *fmt, ...)
{
	va_list argp;

	if (verbosity > g_ioat_debug_level)
		return;

	va_start(argp, fmt);
	vprintf(fmt, argp);
	va_end(argp);
}