gvt.c 11.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Copyright(c) 2011-2016 Intel Corporation. All rights reserved.
 *
 * 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.
22 23 24 25 26 27 28 29 30
 *
 * Authors:
 *    Kevin Tian <kevin.tian@intel.com>
 *    Eddie Dong <eddie.dong@intel.com>
 *
 * Contributors:
 *    Niu Bing <bing.niu@intel.com>
 *    Zhi Wang <zhi.a.wang@intel.com>
 *
31 32 33 34
 */

#include <linux/types.h>
#include <xen/xen.h>
35
#include <linux/kthread.h>
36 37

#include "i915_drv.h"
38
#include "gvt.h"
39 40
#include <linux/vfio.h>
#include <linux/mdev.h>
41 42 43 44 45 46 47 48

struct intel_gvt_host intel_gvt_host;

static const char * const supported_hypervisors[] = {
	[INTEL_GVT_HYPERVISOR_XEN] = "XEN",
	[INTEL_GVT_HYPERVISOR_KVM] = "KVM",
};

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
static struct intel_vgpu_type *intel_gvt_find_vgpu_type(struct intel_gvt *gvt,
		const char *name)
{
	int i;
	struct intel_vgpu_type *t;
	const char *driver_name = dev_driver_string(
			&gvt->dev_priv->drm.pdev->dev);

	for (i = 0; i < gvt->num_types; i++) {
		t = &gvt->types[i];
		if (!strncmp(t->name, name + strlen(driver_name) + 1,
			sizeof(t->name)))
			return t;
	}

	return NULL;
}

static ssize_t available_instances_show(struct kobject *kobj,
					struct device *dev, char *buf)
{
	struct intel_vgpu_type *type;
	unsigned int num = 0;
	void *gvt = kdev_to_i915(dev)->gvt;

	type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
	if (!type)
		num = 0;
	else
		num = type->avail_instance;

	return sprintf(buf, "%u\n", num);
}

static ssize_t device_api_show(struct kobject *kobj, struct device *dev,
		char *buf)
{
	return sprintf(buf, "%s\n", VFIO_DEVICE_API_PCI_STRING);
}

static ssize_t description_show(struct kobject *kobj, struct device *dev,
		char *buf)
{
	struct intel_vgpu_type *type;
	void *gvt = kdev_to_i915(dev)->gvt;

	type = intel_gvt_find_vgpu_type(gvt, kobject_name(kobj));
	if (!type)
		return 0;

	return sprintf(buf, "low_gm_size: %dMB\nhigh_gm_size: %dMB\n"
		       "fence: %d\nresolution: %s\n"
		       "weight: %d\n",
		       BYTES_TO_MB(type->low_gm_size),
		       BYTES_TO_MB(type->high_gm_size),
		       type->fence, vgpu_edid_str(type->resolution),
		       type->weight);
}

static MDEV_TYPE_ATTR_RO(available_instances);
static MDEV_TYPE_ATTR_RO(device_api);
static MDEV_TYPE_ATTR_RO(description);

static struct attribute *gvt_type_attrs[] = {
	&mdev_type_attr_available_instances.attr,
	&mdev_type_attr_device_api.attr,
	&mdev_type_attr_description.attr,
	NULL,
};

static struct attribute_group *gvt_vgpu_type_groups[] = {
	[0 ... NR_MAX_INTEL_VGPU_TYPES - 1] = NULL,
};

123
static bool intel_get_gvt_attrs(struct attribute_group ***intel_vgpu_type_groups)
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
{
	*intel_vgpu_type_groups = gvt_vgpu_type_groups;
	return true;
}

static bool intel_gvt_init_vgpu_type_groups(struct intel_gvt *gvt)
{
	int i, j;
	struct intel_vgpu_type *type;
	struct attribute_group *group;

	for (i = 0; i < gvt->num_types; i++) {
		type = &gvt->types[i];

		group = kzalloc(sizeof(struct attribute_group), GFP_KERNEL);
		if (WARN_ON(!group))
			goto unwind;

		group->name = type->name;
		group->attrs = gvt_type_attrs;
		gvt_vgpu_type_groups[i] = group;
	}

	return true;

unwind:
	for (j = 0; j < i; j++) {
		group = gvt_vgpu_type_groups[j];
		kfree(group);
	}

	return false;
}

static void intel_gvt_cleanup_vgpu_type_groups(struct intel_gvt *gvt)
{
	int i;
	struct attribute_group *group;

	for (i = 0; i < gvt->num_types; i++) {
		group = gvt_vgpu_type_groups[i];
		gvt_vgpu_type_groups[i] = NULL;
		kfree(group);
	}
}

170
static const struct intel_gvt_ops intel_gvt_ops = {
171 172
	.emulate_cfg_read = intel_vgpu_emulate_cfg_read,
	.emulate_cfg_write = intel_vgpu_emulate_cfg_write,
Z
Zhi Wang 已提交
173 174
	.emulate_mmio_read = intel_vgpu_emulate_mmio_read,
	.emulate_mmio_write = intel_vgpu_emulate_mmio_write,
175 176
	.vgpu_create = intel_gvt_create_vgpu,
	.vgpu_destroy = intel_gvt_destroy_vgpu,
177
	.vgpu_release = intel_gvt_release_vgpu,
178
	.vgpu_reset = intel_gvt_reset_vgpu,
179 180
	.vgpu_activate = intel_gvt_activate_vgpu,
	.vgpu_deactivate = intel_gvt_deactivate_vgpu,
181 182
	.gvt_find_vgpu_type = intel_gvt_find_vgpu_type,
	.get_gvt_attrs = intel_get_gvt_attrs,
183 184
	.vgpu_query_plane = intel_vgpu_query_plane,
	.vgpu_get_dmabuf = intel_vgpu_get_dmabuf,
185
	.write_protect_handler = intel_vgpu_page_track_handler,
H
Hang Yuan 已提交
186
	.emulate_hotplug = intel_vgpu_emulate_hotplug,
187 188
};

189 190
static void init_device_info(struct intel_gvt *gvt)
{
191
	struct intel_gvt_device_info *info = &gvt->device_info;
192
	struct pci_dev *pdev = gvt->dev_priv->drm.pdev;
193

194 195 196 197 198 199 200 201 202
	info->max_support_vgpus = 8;
	info->cfg_space_size = PCI_CFG_SPACE_EXP_SIZE;
	info->mmio_size = 2 * 1024 * 1024;
	info->mmio_bar = 0;
	info->gtt_start_offset = 8 * 1024 * 1024;
	info->gtt_entry_size = 8;
	info->gtt_entry_size_shift = 3;
	info->gmadr_bytes_in_cmd = 8;
	info->max_surface_size = 36 * 1024 * 1024;
203
	info->msi_cap_offset = pdev->msi_cap;
204 205
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
static int gvt_service_thread(void *data)
{
	struct intel_gvt *gvt = (struct intel_gvt *)data;
	int ret;

	gvt_dbg_core("service thread start\n");

	while (!kthread_should_stop()) {
		ret = wait_event_interruptible(gvt->service_thread_wq,
				kthread_should_stop() || gvt->service_request);

		if (kthread_should_stop())
			break;

		if (WARN_ONCE(ret, "service thread is waken up by signal.\n"))
			continue;

		if (test_and_clear_bit(INTEL_GVT_REQUEST_EMULATE_VBLANK,
224
					(void *)&gvt->service_request))
225
			intel_gvt_emulate_vblank(gvt);
226

227 228 229
		if (test_bit(INTEL_GVT_REQUEST_SCHED,
				(void *)&gvt->service_request) ||
			test_bit(INTEL_GVT_REQUEST_EVENT_SCHED,
230 231 232
					(void *)&gvt->service_request)) {
			intel_gvt_schedule(gvt);
		}
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
	}

	return 0;
}

static void clean_service_thread(struct intel_gvt *gvt)
{
	kthread_stop(gvt->service_thread);
}

static int init_service_thread(struct intel_gvt *gvt)
{
	init_waitqueue_head(&gvt->service_thread_wq);

	gvt->service_thread = kthread_run(gvt_service_thread,
			gvt, "gvt_service_thread");
	if (IS_ERR(gvt->service_thread)) {
		gvt_err("fail to start service thread.\n");
		return PTR_ERR(gvt->service_thread);
	}
	return 0;
}

256 257
/**
 * intel_gvt_clean_device - clean a GVT device
258
 * @dev_priv: i915 private
259 260 261 262 263 264 265
 *
 * This function is called at the driver unloading stage, to free the
 * resources owned by a GVT device.
 *
 */
void intel_gvt_clean_device(struct drm_i915_private *dev_priv)
{
266
	struct intel_gvt *gvt = to_gvt(dev_priv);
267

268
	if (drm_WARN_ON(&dev_priv->drm, !gvt))
269 270
		return;

271 272 273 274
	intel_gvt_destroy_idle_vgpu(gvt->idle_vgpu);
	intel_gvt_cleanup_vgpu_type_groups(gvt);
	intel_gvt_clean_vgpu_types(gvt);

275
	intel_gvt_debugfs_clean(gvt);
276
	clean_service_thread(gvt);
Z
Zhi Wang 已提交
277
	intel_gvt_clean_cmd_parser(gvt);
278
	intel_gvt_clean_sched_policy(gvt);
Z
Zhi Wang 已提交
279
	intel_gvt_clean_workload_scheduler(gvt);
280
	intel_gvt_clean_gtt(gvt);
281
	intel_gvt_clean_irq(gvt);
282
	intel_gvt_free_firmware(gvt);
283
	intel_gvt_clean_mmio_info(gvt);
284 285
	idr_destroy(&gvt->vgpu_idr);

286 287
	kfree(dev_priv->gvt);
	dev_priv->gvt = NULL;
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
}

/**
 * intel_gvt_init_device - initialize a GVT device
 * @dev_priv: drm i915 private data
 *
 * This function is called at the initialization stage, to initialize
 * necessary GVT components.
 *
 * Returns:
 * Zero on success, negative error code if failed.
 *
 */
int intel_gvt_init_device(struct drm_i915_private *dev_priv)
{
303
	struct intel_gvt *gvt;
P
Ping Gao 已提交
304
	struct intel_vgpu *vgpu;
305 306
	int ret;

307
	if (drm_WARN_ON(&dev_priv->drm, dev_priv->gvt))
308 309
		return -EEXIST;

310 311 312 313
	gvt = kzalloc(sizeof(struct intel_gvt), GFP_KERNEL);
	if (!gvt)
		return -ENOMEM;

314 315
	gvt_dbg_core("init gvt device\n");

316
	idr_init(&gvt->vgpu_idr);
317
	spin_lock_init(&gvt->scheduler.mmio_context_lock);
318
	mutex_init(&gvt->lock);
319
	mutex_init(&gvt->sched_lock);
320
	gvt->dev_priv = dev_priv;
321
	dev_priv->gvt = gvt;
322

323
	init_device_info(gvt);
324 325 326

	ret = intel_gvt_setup_mmio_info(gvt);
	if (ret)
327
		goto out_clean_idr;
328

329 330
	intel_gvt_init_engine_mmio_context(gvt);

331 332 333 334
	ret = intel_gvt_load_firmware(gvt);
	if (ret)
		goto out_clean_mmio_info;

335 336 337 338
	ret = intel_gvt_init_irq(gvt);
	if (ret)
		goto out_free_firmware;

339 340 341 342
	ret = intel_gvt_init_gtt(gvt);
	if (ret)
		goto out_clean_irq;

Z
Zhi Wang 已提交
343
	ret = intel_gvt_init_workload_scheduler(gvt);
344
	if (ret)
345
		goto out_clean_gtt;
346

347
	ret = intel_gvt_init_sched_policy(gvt);
Z
Zhi Wang 已提交
348 349 350
	if (ret)
		goto out_clean_workload_scheduler;

Z
Zhi Wang 已提交
351
	ret = intel_gvt_init_cmd_parser(gvt);
352 353 354
	if (ret)
		goto out_clean_sched_policy;

Z
Zhi Wang 已提交
355 356 357 358
	ret = init_service_thread(gvt);
	if (ret)
		goto out_clean_cmd_parser;

359 360 361 362
	ret = intel_gvt_init_vgpu_types(gvt);
	if (ret)
		goto out_clean_thread;

363 364 365 366 367 368
	ret = intel_gvt_init_vgpu_type_groups(gvt);
	if (ret == false) {
		gvt_err("failed to init vgpu type groups: %d\n", ret);
		goto out_clean_types;
	}

P
Ping Gao 已提交
369 370 371 372 373 374 375 376
	vgpu = intel_gvt_create_idle_vgpu(gvt);
	if (IS_ERR(vgpu)) {
		ret = PTR_ERR(vgpu);
		gvt_err("failed to create idle vgpu\n");
		goto out_clean_types;
	}
	gvt->idle_vgpu = vgpu;

377
	intel_gvt_debugfs_init(gvt);
378

379
	gvt_dbg_core("gvt device initialization is done\n");
380 381
	intel_gvt_host.dev = &dev_priv->drm.pdev->dev;
	intel_gvt_host.initialized = true;
382
	return 0;
383

384 385
out_clean_types:
	intel_gvt_clean_vgpu_types(gvt);
386 387
out_clean_thread:
	clean_service_thread(gvt);
Z
Zhi Wang 已提交
388 389
out_clean_cmd_parser:
	intel_gvt_clean_cmd_parser(gvt);
390 391
out_clean_sched_policy:
	intel_gvt_clean_sched_policy(gvt);
Z
Zhi Wang 已提交
392 393
out_clean_workload_scheduler:
	intel_gvt_clean_workload_scheduler(gvt);
394 395
out_clean_gtt:
	intel_gvt_clean_gtt(gvt);
396 397
out_clean_irq:
	intel_gvt_clean_irq(gvt);
398 399
out_free_firmware:
	intel_gvt_free_firmware(gvt);
400 401
out_clean_mmio_info:
	intel_gvt_clean_mmio_info(gvt);
402 403
out_clean_idr:
	idr_destroy(&gvt->vgpu_idr);
404
	kfree(gvt);
405
	dev_priv->gvt = NULL;
406
	return ret;
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
int
intel_gvt_register_hypervisor(struct intel_gvt_mpt *m)
{
	int ret;
	void *gvt;

	if (!intel_gvt_host.initialized)
		return -ENODEV;

	if (m->type != INTEL_GVT_HYPERVISOR_KVM &&
	    m->type != INTEL_GVT_HYPERVISOR_XEN)
		return -EINVAL;

	/* Get a reference for device model module */
	if (!try_module_get(THIS_MODULE))
		return -ENODEV;

	intel_gvt_host.mpt = m;
	intel_gvt_host.hypervisor_type = m->type;
	gvt = (void *)kdev_to_i915(intel_gvt_host.dev)->gvt;

	ret = intel_gvt_hypervisor_host_init(intel_gvt_host.dev, gvt,
					     &intel_gvt_ops);
	if (ret < 0) {
		gvt_err("Failed to init %s hypervisor module\n",
			supported_hypervisors[intel_gvt_host.hypervisor_type]);
		module_put(THIS_MODULE);
		return -ENODEV;
	}
	gvt_dbg_core("Running with hypervisor %s in host mode\n",
		     supported_hypervisors[intel_gvt_host.hypervisor_type]);
	return 0;
}
EXPORT_SYMBOL_GPL(intel_gvt_register_hypervisor);

void
intel_gvt_unregister_hypervisor(void)
{
	intel_gvt_hypervisor_host_exit(intel_gvt_host.dev);
	module_put(THIS_MODULE);
}
EXPORT_SYMBOL_GPL(intel_gvt_unregister_hypervisor);