162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (C) 2015 Red Hat, Inc.
362306a36Sopenharmony_ci * All Rights Reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Authors:
662306a36Sopenharmony_ci *    Dave Airlie <airlied@redhat.com>
762306a36Sopenharmony_ci *    Gerd Hoffmann <kraxel@redhat.com>
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
1062306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
1162306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
1262306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1362306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
1462306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1562306a36Sopenharmony_ci *
1662306a36Sopenharmony_ci * The above copyright notice and this permission notice (including the next
1762306a36Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
1862306a36Sopenharmony_ci * Software.
1962306a36Sopenharmony_ci *
2062306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2162306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2262306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
2362306a36Sopenharmony_ci * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
2462306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
2562306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2662306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2762306a36Sopenharmony_ci */
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci#include <linux/module.h>
3062306a36Sopenharmony_ci#include <linux/pci.h>
3162306a36Sopenharmony_ci#include <linux/poll.h>
3262306a36Sopenharmony_ci#include <linux/wait.h>
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include <drm/drm.h>
3562306a36Sopenharmony_ci#include <drm/drm_aperture.h>
3662306a36Sopenharmony_ci#include <drm/drm_atomic_helper.h>
3762306a36Sopenharmony_ci#include <drm/drm_drv.h>
3862306a36Sopenharmony_ci#include <drm/drm_fbdev_generic.h>
3962306a36Sopenharmony_ci#include <drm/drm_file.h>
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#include "virtgpu_drv.h"
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_cistatic const struct drm_driver driver;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_cistatic int virtio_gpu_modeset = -1;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ciMODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
4862306a36Sopenharmony_cimodule_param_named(modeset, virtio_gpu_modeset, int, 0400);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_cistatic int virtio_gpu_pci_quirk(struct drm_device *dev)
5162306a36Sopenharmony_ci{
5262306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(dev->dev);
5362306a36Sopenharmony_ci	const char *pname = dev_name(&pdev->dev);
5462306a36Sopenharmony_ci	bool vga = (pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA;
5562306a36Sopenharmony_ci	int ret;
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci	DRM_INFO("pci: %s detected at %s\n",
5862306a36Sopenharmony_ci		 vga ? "virtio-vga" : "virtio-gpu-pci",
5962306a36Sopenharmony_ci		 pname);
6062306a36Sopenharmony_ci	if (vga) {
6162306a36Sopenharmony_ci		ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, &driver);
6262306a36Sopenharmony_ci		if (ret)
6362306a36Sopenharmony_ci			return ret;
6462306a36Sopenharmony_ci	}
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci	return 0;
6762306a36Sopenharmony_ci}
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic int virtio_gpu_probe(struct virtio_device *vdev)
7062306a36Sopenharmony_ci{
7162306a36Sopenharmony_ci	struct drm_device *dev;
7262306a36Sopenharmony_ci	int ret;
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	if (drm_firmware_drivers_only() && virtio_gpu_modeset == -1)
7562306a36Sopenharmony_ci		return -EINVAL;
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci	if (virtio_gpu_modeset == 0)
7862306a36Sopenharmony_ci		return -EINVAL;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	/*
8162306a36Sopenharmony_ci	 * The virtio-gpu device is a virtual device that doesn't have DMA
8262306a36Sopenharmony_ci	 * ops assigned to it, nor DMA mask set and etc. Its parent device
8362306a36Sopenharmony_ci	 * is actual GPU device we want to use it for the DRM's device in
8462306a36Sopenharmony_ci	 * order to benefit from using generic DRM APIs.
8562306a36Sopenharmony_ci	 */
8662306a36Sopenharmony_ci	dev = drm_dev_alloc(&driver, vdev->dev.parent);
8762306a36Sopenharmony_ci	if (IS_ERR(dev))
8862306a36Sopenharmony_ci		return PTR_ERR(dev);
8962306a36Sopenharmony_ci	vdev->priv = dev;
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	if (dev_is_pci(vdev->dev.parent)) {
9262306a36Sopenharmony_ci		ret = virtio_gpu_pci_quirk(dev);
9362306a36Sopenharmony_ci		if (ret)
9462306a36Sopenharmony_ci			goto err_free;
9562306a36Sopenharmony_ci	}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	dma_set_max_seg_size(dev->dev, dma_max_mapping_size(dev->dev) ?: UINT_MAX);
9862306a36Sopenharmony_ci	ret = virtio_gpu_init(vdev, dev);
9962306a36Sopenharmony_ci	if (ret)
10062306a36Sopenharmony_ci		goto err_free;
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_ci	ret = drm_dev_register(dev, 0);
10362306a36Sopenharmony_ci	if (ret)
10462306a36Sopenharmony_ci		goto err_deinit;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	drm_fbdev_generic_setup(vdev->priv, 32);
10762306a36Sopenharmony_ci	return 0;
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_cierr_deinit:
11062306a36Sopenharmony_ci	virtio_gpu_deinit(dev);
11162306a36Sopenharmony_cierr_free:
11262306a36Sopenharmony_ci	drm_dev_put(dev);
11362306a36Sopenharmony_ci	return ret;
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic void virtio_gpu_remove(struct virtio_device *vdev)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	struct drm_device *dev = vdev->priv;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	drm_dev_unplug(dev);
12162306a36Sopenharmony_ci	drm_atomic_helper_shutdown(dev);
12262306a36Sopenharmony_ci	virtio_gpu_deinit(dev);
12362306a36Sopenharmony_ci	drm_dev_put(dev);
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_cistatic void virtio_gpu_config_changed(struct virtio_device *vdev)
12762306a36Sopenharmony_ci{
12862306a36Sopenharmony_ci	struct drm_device *dev = vdev->priv;
12962306a36Sopenharmony_ci	struct virtio_gpu_device *vgdev = dev->dev_private;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	schedule_work(&vgdev->config_changed_work);
13262306a36Sopenharmony_ci}
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_cistatic struct virtio_device_id id_table[] = {
13562306a36Sopenharmony_ci	{ VIRTIO_ID_GPU, VIRTIO_DEV_ANY_ID },
13662306a36Sopenharmony_ci	{ 0 },
13762306a36Sopenharmony_ci};
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_cistatic unsigned int features[] = {
14062306a36Sopenharmony_ci#ifdef __LITTLE_ENDIAN
14162306a36Sopenharmony_ci	/*
14262306a36Sopenharmony_ci	 * Gallium command stream send by virgl is native endian.
14362306a36Sopenharmony_ci	 * Because of that we only support little endian guests on
14462306a36Sopenharmony_ci	 * little endian hosts.
14562306a36Sopenharmony_ci	 */
14662306a36Sopenharmony_ci	VIRTIO_GPU_F_VIRGL,
14762306a36Sopenharmony_ci#endif
14862306a36Sopenharmony_ci	VIRTIO_GPU_F_EDID,
14962306a36Sopenharmony_ci	VIRTIO_GPU_F_RESOURCE_UUID,
15062306a36Sopenharmony_ci	VIRTIO_GPU_F_RESOURCE_BLOB,
15162306a36Sopenharmony_ci	VIRTIO_GPU_F_CONTEXT_INIT,
15262306a36Sopenharmony_ci};
15362306a36Sopenharmony_cistatic struct virtio_driver virtio_gpu_driver = {
15462306a36Sopenharmony_ci	.feature_table = features,
15562306a36Sopenharmony_ci	.feature_table_size = ARRAY_SIZE(features),
15662306a36Sopenharmony_ci	.driver.name = KBUILD_MODNAME,
15762306a36Sopenharmony_ci	.driver.owner = THIS_MODULE,
15862306a36Sopenharmony_ci	.id_table = id_table,
15962306a36Sopenharmony_ci	.probe = virtio_gpu_probe,
16062306a36Sopenharmony_ci	.remove = virtio_gpu_remove,
16162306a36Sopenharmony_ci	.config_changed = virtio_gpu_config_changed
16262306a36Sopenharmony_ci};
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_cimodule_virtio_driver(virtio_gpu_driver);
16562306a36Sopenharmony_ci
16662306a36Sopenharmony_ciMODULE_DEVICE_TABLE(virtio, id_table);
16762306a36Sopenharmony_ciMODULE_DESCRIPTION("Virtio GPU driver");
16862306a36Sopenharmony_ciMODULE_LICENSE("GPL and additional rights");
16962306a36Sopenharmony_ciMODULE_AUTHOR("Dave Airlie <airlied@redhat.com>");
17062306a36Sopenharmony_ciMODULE_AUTHOR("Gerd Hoffmann <kraxel@redhat.com>");
17162306a36Sopenharmony_ciMODULE_AUTHOR("Alon Levy");
17262306a36Sopenharmony_ci
17362306a36Sopenharmony_ciDEFINE_DRM_GEM_FOPS(virtio_gpu_driver_fops);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistatic const struct drm_driver driver = {
17662306a36Sopenharmony_ci	/*
17762306a36Sopenharmony_ci	 * If KMS is disabled DRIVER_MODESET and DRIVER_ATOMIC are masked
17862306a36Sopenharmony_ci	 * out via drm_device::driver_features:
17962306a36Sopenharmony_ci	 */
18062306a36Sopenharmony_ci	.driver_features = DRIVER_MODESET | DRIVER_GEM | DRIVER_RENDER | DRIVER_ATOMIC |
18162306a36Sopenharmony_ci			   DRIVER_SYNCOBJ | DRIVER_SYNCOBJ_TIMELINE | DRIVER_CURSOR_HOTSPOT,
18262306a36Sopenharmony_ci	.open = virtio_gpu_driver_open,
18362306a36Sopenharmony_ci	.postclose = virtio_gpu_driver_postclose,
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	.dumb_create = virtio_gpu_mode_dumb_create,
18662306a36Sopenharmony_ci	.dumb_map_offset = virtio_gpu_mode_dumb_mmap,
18762306a36Sopenharmony_ci
18862306a36Sopenharmony_ci#if defined(CONFIG_DEBUG_FS)
18962306a36Sopenharmony_ci	.debugfs_init = virtio_gpu_debugfs_init,
19062306a36Sopenharmony_ci#endif
19162306a36Sopenharmony_ci	.gem_prime_import = virtgpu_gem_prime_import,
19262306a36Sopenharmony_ci	.gem_prime_import_sg_table = virtgpu_gem_prime_import_sg_table,
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	.gem_create_object = virtio_gpu_create_object,
19562306a36Sopenharmony_ci	.fops = &virtio_gpu_driver_fops,
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	.ioctls = virtio_gpu_ioctls,
19862306a36Sopenharmony_ci	.num_ioctls = DRM_VIRTIO_NUM_IOCTLS,
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	.name = DRIVER_NAME,
20162306a36Sopenharmony_ci	.desc = DRIVER_DESC,
20262306a36Sopenharmony_ci	.date = DRIVER_DATE,
20362306a36Sopenharmony_ci	.major = DRIVER_MAJOR,
20462306a36Sopenharmony_ci	.minor = DRIVER_MINOR,
20562306a36Sopenharmony_ci	.patchlevel = DRIVER_PATCHLEVEL,
20662306a36Sopenharmony_ci
20762306a36Sopenharmony_ci	.release = virtio_gpu_release,
20862306a36Sopenharmony_ci};
209