igt_kmod.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * Copyright © 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

24 25 26
#include <signal.h>
#include <errno.h>

27
#include "igt_aux.h"
28 29
#include "igt_core.h"
#include "igt_kmod.h"
30
#include "igt_sysfs.h"
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

/**
 * SECTION:igt_kmod
 * @short_description: Wrappers around libkmod for module loading/unloading
 * @title: kmod
 * @include: igt.h
 *
 * This library provides helpers to load/unload module driver.
 *
 * Note on loading/reloading:
 *
 * Loading/unload/reloading the driver requires that resources to /dev/dri to
 * be released (closed). A potential mistake would be to submit commands to the
 * GPU by having a fd returned by @drm_open_driver, which is closed by atexit
 * signal handler so reloading/unloading the driver will fail if performed
 * afterwards. One possible solution to this issue is to use
 * @__drm_open_driver() or use @igt_set_module_param() to set module parameters
 * dynamically.
 */

51 52 53 54 55 56 57 58 59
static void squelch(void *data, int priority,
		    const char *file, int line, const char *fn,
		    const char *format, va_list args)
{
}

static struct kmod_ctx *kmod_ctx(void)
{
	static struct kmod_ctx *ctx;
60 61 62
	const char **config_paths = NULL;
	char *config_paths_str;
	char *dirname;
63

64 65
	if (ctx)
		goto out;
66

67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	dirname = getenv("IGT_KMOD_DIRNAME");
	if (dirname)
		igt_debug("kmod dirname = %s\n", dirname);

	config_paths_str = getenv("IGT_KMOD_CONFIG_PATHS");
	if (config_paths_str)
		igt_debug("kmod config paths = %s\n", config_paths_str);

	if (config_paths_str) {
		unsigned count = !!strlen(config_paths_str);
		unsigned i;
		char* p;

		p = config_paths_str;
		while ((p = strchr(p, ':'))) p++, count++;


		config_paths = malloc(sizeof(*config_paths) * (count + 1));
		igt_assert(config_paths != NULL);

		p = config_paths_str;
		for (i = 0; i < count; ++i) {
89
			igt_assert(p != NULL);
90 91 92 93 94 95
			config_paths[i] = p;

			if ((p = strchr(p, ':')))
				*p++ = '\0';
		}
		config_paths[i] = NULL;
96 97
	}

98 99 100 101 102 103 104
	ctx = kmod_new(dirname, config_paths);
	igt_assert(ctx != NULL);

	free(config_paths);

	kmod_set_log_fn(ctx, squelch, NULL);
out:
105 106
	return ctx;
}
107 108 109 110 111 112 113 114 115 116 117 118 119 120

/**
 * igt_kmod_is_loaded:
 * @mod_name: The name of the module.
 *
 * Returns: True in case the module has been found or false otherwise.
 *
 * Function to check the existance of module @mod_name in list of loaded kernel
 * modules.
 *
 */
bool
igt_kmod_is_loaded(const char *mod_name)
{
121
	struct kmod_ctx *ctx = kmod_ctx();
122 123 124
	struct kmod_list *mod, *list;
	bool ret = false;

125
	if (kmod_module_new_from_loaded(ctx, &list) < 0)
126 127 128 129 130 131
		goto out;

	kmod_list_foreach(mod, list) {
		struct kmod_module *kmod = kmod_module_get_module(mod);
		const char *kmod_name = kmod_module_get_name(kmod);

132
		if (!strcmp(kmod_name, mod_name)) {
133 134 135 136 137 138 139 140 141 142 143
			kmod_module_unref(kmod);
			ret = true;
			break;
		}
		kmod_module_unref(kmod);
	}
	kmod_module_unref_list(list);
out:
	return ret;
}

144 145
static int modprobe(struct kmod_module *kmod, const char *options)
{
146 147 148 149 150 151 152
	unsigned int flags;

	flags = 0;
	if (options) /* force a fresh load to set the new options */
		flags |= KMOD_PROBE_FAIL_ON_LOADED;

	return kmod_module_probe_insert_module(kmod, flags, options,
153 154 155
					       NULL, NULL, NULL);
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
/**
 * igt_kmod_load:
 * @mod_name: The name of the module
 * @opts: Parameters for the module. NULL in case no parameters
 * are to be passed, or a '\0' terminated string otherwise.
 *
 * Returns: 0 in case of success or -errno in case the module could not
 * be loaded.
 *
 * This function loads a kernel module using the name specified in @mod_name.
 *
 * @Note: This functions doesn't automatically resolve other module
 * dependencies so make make sure you load the dependencies module(s) before
 * this one.
 */
int
igt_kmod_load(const char *mod_name, const char *opts)
{
174
	struct kmod_ctx *ctx = kmod_ctx();
175 176 177 178
	struct kmod_module *kmod;
	int err = 0;

	err = kmod_module_new_from_name(ctx, mod_name, &kmod);
179
	if (err < 0)
180 181
		goto out;

182
	err = modprobe(kmod, opts);
183 184 185 186
	if (err < 0) {
		switch (err) {
		case -EEXIST:
			igt_debug("Module %s already inserted\n",
187
				  kmod_module_get_name(kmod));
188 189 190
			break;
		case -ENOENT:
			igt_debug("Unknown symbol in module %s or "
191 192
				  "unknown parameter\n",
				  kmod_module_get_name(kmod));
193 194 195
			break;
		default:
			igt_debug("Could not insert %s (%s)\n",
196
				  kmod_module_get_name(kmod), strerror(-err));
197 198 199 200 201
			break;
		}
	}
out:
	kmod_module_unref(kmod);
202
	return err < 0 ? err : 0;
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
}


/**
 * igt_kmod_unload:
 * @mod_name: Module name.
 * @flags: flags are passed directly to libkmod and can be:
 * KMOD_REMOVE_FORCE or KMOD_REMOVE_NOWAIT.
 *
 * Returns: 0 in case of success or -errno otherwise.
 *
 * Removes the module @mod_name.
 *
 */
int
igt_kmod_unload(const char *mod_name, unsigned int flags)
{
220
	struct kmod_ctx *ctx = kmod_ctx();
221
	struct kmod_module *kmod;
222
	int err;
223 224 225 226

	err = kmod_module_new_from_name(ctx, mod_name, &kmod);
	if (err < 0) {
		igt_debug("Could not use module %s (%s)\n", mod_name,
227
			  strerror(-err));
228 229 230 231 232 233
		goto out;
	}

	err = kmod_module_remove_module(kmod, flags);
	if (err < 0) {
		igt_debug("Could not remove module %s (%s)\n", mod_name,
234
			  strerror(-err));
235 236 237 238
	}

out:
	kmod_module_unref(kmod);
239
	return err < 0 ? err : 0;
240 241 242 243 244 245 246 247 248 249
}

/**
 *
 * igt_kmod_list_loaded: List all modules currently loaded.
 *
 */
void
igt_kmod_list_loaded(void)
{
250
	struct kmod_ctx *ctx = kmod_ctx();
251 252
	struct kmod_list *module, *list;

253
	if (kmod_module_new_from_loaded(ctx, &list) < 0)
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
		return;

	igt_info("Module\t\t      Used by\n");

	kmod_list_foreach(module, list) {
		struct kmod_module *kmod = kmod_module_get_module(module);
		struct kmod_list *module_deps, *module_deps_list;

		igt_info("%-24s", kmod_module_get_name(kmod));
		module_deps_list = kmod_module_get_holders(kmod);
		if (module_deps_list) {

			kmod_list_foreach(module_deps, module_deps_list) {
				struct kmod_module *kmod_dep;

				kmod_dep = kmod_module_get_module(module_deps);
270
				igt_info("%s", kmod_module_get_name(kmod_dep));
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

				if (kmod_list_next(module_deps_list, module_deps))
					igt_info(",");

				kmod_module_unref(kmod_dep);
			}
		}
		kmod_module_unref_list(module_deps_list);

		igt_info("\n");
		kmod_module_unref(kmod);
	}

	kmod_module_unref_list(list);
}

/**
 * igt_i915_driver_load:
 * @opts: options to pass to i915 driver
 *
 * Loads the i915 driver and its dependencies.
 *
 */
int
igt_i915_driver_load(const char *opts)
{
	if (opts)
		igt_info("Reloading i915 with %s\n\n", opts);

	if (igt_kmod_load("i915", opts)) {
		igt_warn("Could not load i915\n");
		return IGT_EXIT_FAILURE;
	}

305
	bind_fbcon(true);
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
	igt_kmod_load("snd_hda_intel", NULL);

	return IGT_EXIT_SUCCESS;
}

/**
 * igt_i915_driver_unload:
 *
 * Unloads the i915 driver and its dependencies.
 *
 */
int
igt_i915_driver_unload(void)
{
	/* unbind vt */
321
	bind_fbcon(false);
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

	if (igt_kmod_is_loaded("snd_hda_intel")) {
		igt_terminate_process(SIGTERM, "alsactl");

		/* unbind snd_hda_intel */
		kick_snd_hda_intel();

		if (igt_kmod_unload("snd_hda_intel", 0)) {
			igt_warn("Could not unload snd_hda_intel\n");
			igt_kmod_list_loaded();
			igt_lsof("/dev/snd");
			return IGT_EXIT_FAILURE;
		}
	}

337 338 339 340 341 342 343 344 345 346 347
	if (igt_kmod_is_loaded("snd_hdmi_lpe_audio")) {
		igt_terminate_process(SIGTERM, "alsactl");

		if (igt_kmod_unload("snd_hdmi_lpe_audio", 0)) {
			igt_warn("Could not unload snd_hdmi_lpe_audio\n");
			igt_kmod_list_loaded();
			igt_lsof("/dev/snd");
			return IGT_EXIT_FAILURE;
		}
	}

348
	/* gen5 */
349
	if (igt_kmod_is_loaded("intel_ips"))
350 351 352 353 354 355 356 357 358 359 360
		igt_kmod_unload("intel_ips", 0);

	if (igt_kmod_is_loaded("i915")) {
		if (igt_kmod_unload("i915", 0)) {
			igt_warn("Could not unload i915\n");
			igt_kmod_list_loaded();
			igt_lsof("/dev/dri");
			return IGT_EXIT_SKIP;
		}
	}

361
	if (igt_kmod_is_loaded("intel-gtt"))
362 363 364 365 366 367 368 369 370 371 372 373
		igt_kmod_unload("intel-gtt", 0);

	igt_kmod_unload("drm_kms_helper", 0);
	igt_kmod_unload("drm", 0);

	if (igt_kmod_is_loaded("i915")) {
		igt_warn("i915.ko still loaded!\n");
		return IGT_EXIT_FAILURE;
	}

	return IGT_EXIT_SUCCESS;
}
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

static void kmsg_dump(int fd)
{
	FILE *file;

	file = NULL;
	if (fd != -1)
		file = fdopen(fd, "r");
	if (file) {
		size_t len = 0;
		char *line = NULL;

		while (getline(&line, &len, file) != -1) {
			char *start = strchr(line, ':');
			if (start)
				igt_warn("%s", start + 2);
		}

		free(line);
		fclose(file);
	} else {
		igt_warn("Unable to retrieve kernel log (from /dev/kmsg)\n");
	}
}

399
static void tests_add(struct igt_kselftest_list *tl, struct igt_list *list)
400
{
401
	struct igt_kselftest_list *pos;
402 403 404 405 406 407 408 409

	igt_list_for_each(pos, list, link)
		if (pos->number > tl->number)
			break;

	igt_list_add_tail(&tl->link, &pos->link);
}

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
void igt_kselftest_get_tests(struct kmod_module *kmod,
			     const char *filter,
			     struct igt_list *tests)
{
	const char *param_prefix = "igt__";
	const int prefix_len = strlen(param_prefix);
	struct kmod_list *d, *pre;
	struct igt_kselftest_list *tl;

	pre = NULL;
	if (!kmod_module_get_info(kmod, &pre))
		return;

	kmod_list_foreach(d, pre) {
		const char *key, *val;
		char *colon;
		int offset;

		key = kmod_module_info_get_key(d);
		if (strcmp(key, "parmtype"))
			continue;

		val = kmod_module_info_get_value(d);
		if (!val || strncmp(val, param_prefix, prefix_len))
			continue;

		offset = strlen(val) + 1;
		tl = malloc(sizeof(*tl) + offset);
		if (!tl)
			continue;

		memcpy(tl->param, val, offset);
		colon = strchr(tl->param, ':');
		*colon = '\0';

		tl->number = 0;
		tl->name = tl->param + prefix_len;
		if (sscanf(tl->name, "%u__%n",
			   &tl->number, &offset) == 1)
			tl->name += offset;

		if (filter && strncmp(tl->name, filter, strlen(filter))) {
			free(tl);
			continue;
		}

		tests_add(tl, tests);
	}
	kmod_module_info_free_list(pre);
}

461 462 463 464 465 466 467 468
static int open_parameters(const char *module_name)
{
	char path[256];

	snprintf(path, sizeof(path), "/sys/module/%s/parameters", module_name);
	return open(path, O_RDONLY);
}

469 470
int igt_kselftest_init(struct igt_kselftest *tst,
		       const char *module_name)
471
{
472
	int err;
473

474
	memset(tst, 0, sizeof(*tst));
475

476
	tst->module_name = strdup(module_name);
477 478
	if (!tst->module_name)
		return 1;
479

480
	tst->kmsg = -1;
481

482 483 484
	err = kmod_module_new_from_name(kmod_ctx(), module_name, &tst->kmod);
	if (err)
		return err;
485

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
	return 0;
}

int igt_kselftest_begin(struct igt_kselftest *tst)
{
	int err;

	if (strcmp(tst->module_name, "i915") == 0)
		igt_i915_driver_unload();

	err = kmod_module_remove_module(tst->kmod, KMOD_REMOVE_FORCE);
	igt_require(err == 0 || err == -ENOENT);

	tst->kmsg = open("/dev/kmsg", O_RDONLY | O_NONBLOCK);

	return 0;
}

int igt_kselftest_execute(struct igt_kselftest *tst,
			  struct igt_kselftest_list *tl,
			  const char *options,
			  const char *result)
{
	char buf[1024];
	int err;

	lseek(tst->kmsg, 0, SEEK_END);

	snprintf(buf, sizeof(buf), "%s=1 %s", tl->param, options ?: "");

	err = modprobe(tst->kmod, buf);
	if (err == 0 && result) {
		int dir = open_parameters(tst->module_name);
		igt_sysfs_scanf(dir, result, "%d", &err);
		close(dir);
521
	}
522 523 524 525
	if (err == -ENOTTY) /* special case */
		err = 0;
	if (err)
		kmsg_dump(tst->kmsg);
526

527
	kmod_module_remove_module(tst->kmod, 0);
528

529 530 531 532 533 534 535
	errno = 0;
	igt_assert_f(err == 0,
		     "kselftest \"%s %s\" failed: %s [%d]\n",
		     tst->module_name, buf, strerror(-err), -err);

	return err;
}
536

537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
void igt_kselftest_end(struct igt_kselftest *tst)
{
	kmod_module_remove_module(tst->kmod, KMOD_REMOVE_FORCE);
	close(tst->kmsg);
}

void igt_kselftest_fini(struct igt_kselftest *tst)
{
	free(tst->module_name);
	kmod_module_unref(tst->kmod);
}

void igt_kselftests(const char *module_name,
		    const char *options,
		    const char *result,
552
		    const char *filter)
553 554 555 556 557
{
	struct igt_kselftest tst;
	IGT_LIST(tests);
	struct igt_kselftest_list *tl, *tn;

558 559 560
	if (igt_kselftest_init(&tst, module_name) != 0)
		return;

561 562
	igt_fixture
		igt_require(igt_kselftest_begin(&tst) == 0);
563

564 565 566 567 568
	igt_kselftest_get_tests(tst.kmod, filter, &tests);
	igt_list_for_each_safe(tl, tn, &tests, link) {
		igt_subtest_f("%s", tl->name)
			igt_kselftest_execute(&tst, tl, options, result);
		free(tl);
569
	}
570

571 572 573
	igt_fixture {
		igt_kselftest_end(&tst);
		igt_require(!igt_list_empty(&tests));
574
	}
575 576

	igt_kselftest_fini(&tst);
577
}