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 struct ioat_test *test; 127 char *dst, *src; 128 uint32_t i, j; 129 130 test = tx->test; 131 132 for (i = 0; i < tx->depth; i++) { 133 dst = tx->buf[2 * i + 1]; 134 src = tx->buf[2 * i]; 135 136 if (test->testkind == IOAT_TEST_FILL) { 137 for (j = 0; j < tx->length; j += sizeof(uint64_t)) { 138 if (memcmp(src, &dst[j], 139 MIN(sizeof(uint64_t), tx->length - j)) 140 != 0) 141 return (false); 142 } 143 } else if (test->testkind == IOAT_TEST_DMA) 144 if (memcmp(src, dst, tx->length) != 0) 145 return (false); 146 } 147 return (true); 148 } 149 150 static void 151 ioat_dma_test_callback(void *arg) 152 { 153 struct test_transaction *tx; 154 struct ioat_test *test; 155 156 tx = arg; 157 test = tx->test; 158 159 if (test->verify && !ioat_compare_ok(tx)) { 160 ioat_test_log(0, "miscompare found\n"); 161 atomic_add_32(&test->status[IOAT_TEST_MISCOMPARE], tx->depth); 162 } else if (!test->too_late) 163 atomic_add_32(&test->status[IOAT_TEST_OK], tx->depth); 164 165 IT_LOCK(); 166 TAILQ_REMOVE(&test->pend_q, tx, entry); 167 TAILQ_INSERT_TAIL(&test->free_q, tx, entry); 168 wakeup(&test->free_q); 169 IT_UNLOCK(); 170 } 171 172 static int 173 ioat_test_prealloc_memory(struct ioat_test *test, int index) 174 { 175 uint32_t i, j, k; 176 struct test_transaction *tx; 177 178 for (i = 0; i < test->transactions; i++) { 179 tx = ioat_test_transaction_create(test->chain_depth * 2, 180 test->buffer_size); 181 if (tx == NULL) { 182 ioat_test_log(0, "tx == NULL - memory exhausted\n"); 183 test->status[IOAT_TEST_NO_MEMORY]++; 184 return (ENOMEM); 185 } 186 187 TAILQ_INSERT_HEAD(&test->free_q, tx, entry); 188 189 tx->test = test; 190 tx->depth = test->chain_depth; 191 192 /* fill in source buffers */ 193 for (j = 0; j < (tx->length / sizeof(uint32_t)); j++) { 194 uint32_t val = j + (index << 28); 195 196 for (k = 0; k < test->chain_depth; k++) { 197 ((uint32_t *)tx->buf[2*k])[j] = ~val; 198 ((uint32_t *)tx->buf[2*k+1])[j] = val; 199 } 200 } 201 } 202 return (0); 203 } 204 205 static void 206 ioat_test_release_memory(struct ioat_test *test) 207 { 208 struct test_transaction *tx, *s; 209 210 TAILQ_FOREACH_SAFE(tx, &test->free_q, entry, s) 211 ioat_test_transaction_destroy(tx); 212 TAILQ_INIT(&test->free_q); 213 214 TAILQ_FOREACH_SAFE(tx, &test->pend_q, entry, s) 215 ioat_test_transaction_destroy(tx); 216 TAILQ_INIT(&test->pend_q); 217 } 218 219 static void 220 ioat_test_submit_1_tx(struct ioat_test *test, bus_dmaengine_t dma) 221 { 222 struct test_transaction *tx; 223 struct bus_dmadesc *desc; 224 bus_dmaengine_callback_t cb; 225 bus_addr_t src, dest; 226 uint64_t fillpattern; 227 uint32_t i, flags; 228 229 desc = NULL; 230 231 IT_LOCK(); 232 while (TAILQ_EMPTY(&test->free_q)) 233 msleep(&test->free_q, &ioat_test_lk, 0, "test_submit", 0); 234 235 tx = TAILQ_FIRST(&test->free_q); 236 TAILQ_REMOVE(&test->free_q, tx, entry); 237 TAILQ_INSERT_HEAD(&test->pend_q, tx, entry); 238 IT_UNLOCK(); 239 240 ioat_acquire(dma); 241 for (i = 0; i < tx->depth; i++) { 242 src = vtophys((vm_offset_t)tx->buf[2*i]); 243 dest = vtophys((vm_offset_t)tx->buf[2*i+1]); 244 245 if (i == tx->depth - 1) { 246 cb = ioat_dma_test_callback; 247 flags = DMA_INT_EN; 248 } else { 249 cb = NULL; 250 flags = 0; 251 } 252 253 if (test->testkind == IOAT_TEST_DMA) 254 desc = ioat_copy(dma, dest, src, tx->length, cb, tx, 255 flags); 256 else if (test->testkind == IOAT_TEST_FILL) { 257 fillpattern = *(uint64_t *)tx->buf[2*i]; 258 desc = ioat_blockfill(dma, dest, fillpattern, 259 tx->length, cb, tx, flags); 260 } 261 262 if (desc == NULL) 263 panic("Failed to allocate a ring slot " 264 "-- this shouldn't happen!"); 265 } 266 ioat_release(dma); 267 } 268 269 static void 270 ioat_dma_test(void *arg) 271 { 272 struct ioat_test *test; 273 bus_dmaengine_t dmaengine; 274 uint32_t loops; 275 int index, rc, start, end; 276 277 test = arg; 278 memset(__DEVOLATILE(void *, test->status), 0, sizeof(test->status)); 279 280 if (test->buffer_size > 1024 * 1024) { 281 ioat_test_log(0, "Buffer size too large >1MB\n"); 282 test->status[IOAT_TEST_NO_MEMORY]++; 283 return; 284 } 285 286 if (test->chain_depth * 2 > IOAT_MAX_BUFS) { 287 ioat_test_log(0, "Depth too large (> %u)\n", 288 (unsigned)IOAT_MAX_BUFS / 2); 289 test->status[IOAT_TEST_NO_MEMORY]++; 290 return; 291 } 292 293 if (btoc((uint64_t)test->buffer_size * test->chain_depth * 294 test->transactions) > (physmem / 4)) { 295 ioat_test_log(0, "Sanity check failed -- test would " 296 "use more than 1/4 of phys mem.\n"); 297 test->status[IOAT_TEST_NO_MEMORY]++; 298 return; 299 } 300 301 if ((uint64_t)test->transactions * test->chain_depth > (1<<16)) { 302 ioat_test_log(0, "Sanity check failed -- test would " 303 "use more than available IOAT ring space.\n"); 304 test->status[IOAT_TEST_NO_MEMORY]++; 305 return; 306 } 307 308 if (test->testkind >= IOAT_NUM_TESTKINDS) { 309 ioat_test_log(0, "Invalid kind %u\n", 310 (unsigned)test->testkind); 311 test->status[IOAT_TEST_INVALID_INPUT]++; 312 return; 313 } 314 315 dmaengine = ioat_get_dmaengine(test->channel_index); 316 if (dmaengine == NULL) { 317 ioat_test_log(0, "Couldn't acquire dmaengine\n"); 318 test->status[IOAT_TEST_NO_DMA_ENGINE]++; 319 return; 320 } 321 322 index = g_thread_index++; 323 TAILQ_INIT(&test->free_q); 324 TAILQ_INIT(&test->pend_q); 325 326 if (test->duration == 0) 327 ioat_test_log(1, "Thread %d: num_loops remaining: 0x%08x\n", 328 index, test->transactions); 329 else 330 ioat_test_log(1, "Thread %d: starting\n", index); 331 332 rc = ioat_test_prealloc_memory(test, index); 333 if (rc != 0) { 334 ioat_test_log(0, "prealloc_memory: %d\n", rc); 335 goto out; 336 } 337 wmb(); 338 339 test->too_late = false; 340 start = ticks; 341 end = start + (((sbintime_t)test->duration * hz) / 1000); 342 343 for (loops = 0;; loops++) { 344 if (test->duration == 0 && loops >= test->transactions) 345 break; 346 else if (test->duration != 0 && time_after(ticks, end)) { 347 test->too_late = true; 348 break; 349 } 350 351 ioat_test_submit_1_tx(test, dmaengine); 352 } 353 354 ioat_test_log(1, "Test Elapsed: %d ticks (overrun %d), %d sec.\n", 355 ticks - start, ticks - end, (ticks - start) / hz); 356 357 IT_LOCK(); 358 while (!TAILQ_EMPTY(&test->pend_q)) 359 msleep(&test->free_q, &ioat_test_lk, 0, "ioattestcompl", hz); 360 IT_UNLOCK(); 361 362 ioat_test_log(1, "Test Elapsed2: %d ticks (overrun %d), %d sec.\n", 363 ticks - start, ticks - end, (ticks - start) / hz); 364 365 ioat_test_release_memory(test); 366 out: 367 ioat_put_dmaengine(dmaengine); 368 } 369 370 static int 371 ioat_test_open(struct cdev *dev, int flags, int fmt, struct thread *td) 372 { 373 374 return (0); 375 } 376 377 static int 378 ioat_test_close(struct cdev *dev, int flags, int fmt, struct thread *td) 379 { 380 381 return (0); 382 } 383 384 static int 385 ioat_test_ioctl(struct cdev *dev, unsigned long cmd, caddr_t arg, int flag, 386 struct thread *td) 387 { 388 389 switch (cmd) { 390 case IOAT_DMATEST: 391 ioat_dma_test(arg); 392 break; 393 default: 394 return (EINVAL); 395 } 396 return (0); 397 } 398 399 static struct cdevsw ioat_cdevsw = { 400 .d_version = D_VERSION, 401 .d_flags = 0, 402 .d_open = ioat_test_open, 403 .d_close = ioat_test_close, 404 .d_ioctl = ioat_test_ioctl, 405 .d_name = "ioat_test", 406 }; 407 408 static int 409 enable_ioat_test(bool enable) 410 { 411 412 mtx_assert(&Giant, MA_OWNED); 413 414 if (enable && g_ioat_cdev == NULL) { 415 g_ioat_cdev = make_dev(&ioat_cdevsw, 0, UID_ROOT, GID_WHEEL, 416 0600, "ioat_test"); 417 } else if (!enable && g_ioat_cdev != NULL) { 418 destroy_dev(g_ioat_cdev); 419 g_ioat_cdev = NULL; 420 } 421 return (0); 422 } 423 424 static int 425 sysctl_enable_ioat_test(SYSCTL_HANDLER_ARGS) 426 { 427 int error, enabled; 428 429 enabled = (g_ioat_cdev != NULL); 430 error = sysctl_handle_int(oidp, &enabled, 0, req); 431 if (error != 0 || req->newptr == NULL) 432 return (error); 433 434 enable_ioat_test(enabled); 435 return (0); 436 } 437 SYSCTL_PROC(_hw_ioat, OID_AUTO, enable_ioat_test, CTLTYPE_INT | CTLFLAG_RW, 438 0, 0, sysctl_enable_ioat_test, "I", 439 "Non-zero: Enable the /dev/ioat_test device"); 440 441 void 442 ioat_test_attach(void) 443 { 444 char *val; 445 446 val = kern_getenv("hw.ioat.enable_ioat_test"); 447 if (val != NULL && strcmp(val, "0") != 0) { 448 mtx_lock(&Giant); 449 enable_ioat_test(true); 450 mtx_unlock(&Giant); 451 } 452 freeenv(val); 453 } 454 455 void 456 ioat_test_detach(void) 457 { 458 459 mtx_lock(&Giant); 460 enable_ioat_test(false); 461 mtx_unlock(&Giant); 462 } 463 464 static inline void 465 _ioat_test_log(int verbosity, const char *fmt, ...) 466 { 467 va_list argp; 468 469 if (verbosity > g_ioat_debug_level) 470 return; 471 472 va_start(argp, fmt); 473 vprintf(fmt, argp); 474 va_end(argp); 475 } 476