Linux Audio

Check our new training course

Embedded Linux training

Mar 10-20, 2025, special US time zones
Register
Loading...
v6.13.7
 
  1/* qxl_drv.c -- QXL driver -*- linux-c -*-
  2 *
  3 * Copyright 2011 Red Hat, Inc.
  4 * All Rights Reserved.
  5 *
  6 * Permission is hereby granted, free of charge, to any person obtaining a
  7 * copy of this software and associated documentation files (the "Software"),
  8 * to deal in the Software without restriction, including without limitation
  9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 10 * and/or sell copies of the Software, and to permit persons to whom the
 11 * Software is furnished to do so, subject to the following conditions:
 12 *
 13 * The above copyright notice and this permission notice (including the next
 14 * paragraph) shall be included in all copies or substantial portions of the
 15 * Software.
 16 *
 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 23 * OTHER DEALINGS IN THE SOFTWARE.
 24 *
 25 * Authors:
 26 *    Dave Airlie <airlie@redhat.com>
 27 *    Alon Levy <alevy@redhat.com>
 28 */
 29
 30#include "qxl_drv.h"
 31
 32#include <linux/aperture.h>
 33#include <linux/module.h>
 34#include <linux/pci.h>
 35#include <linux/vgaarb.h>
 36
 37#include <drm/drm.h>
 38#include <drm/drm_atomic_helper.h>
 39#include <drm/drm_client_setup.h>
 40#include <drm/drm_drv.h>
 41#include <drm/drm_fbdev_ttm.h>
 42#include <drm/drm_file.h>
 43#include <drm/drm_gem_ttm_helper.h>
 44#include <drm/drm_module.h>
 45#include <drm/drm_modeset_helper.h>
 46#include <drm/drm_prime.h>
 47#include <drm/drm_probe_helper.h>
 48
 
 
 
 
 49#include "qxl_object.h"
 50
 51static const struct pci_device_id pciidlist[] = {
 
 52	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
 53	  0xffff00, 0 },
 54	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
 55	  0xffff00, 0 },
 56	{ 0, 0, 0 },
 57};
 58MODULE_DEVICE_TABLE(pci, pciidlist);
 59
 60static int qxl_modeset = -1;
 61int qxl_num_crtc = 4;
 62
 63MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
 64module_param_named(modeset, qxl_modeset, int, 0400);
 65
 66MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
 67module_param_named(num_heads, qxl_num_crtc, int, 0400);
 68
 69static struct drm_driver qxl_driver;
 70static struct pci_driver qxl_pci_driver;
 71
 72static int
 73qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 74{
 75	struct qxl_device *qdev;
 76	int ret;
 77
 78	if (pdev->revision < 4) {
 79		DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
 80			  " use xf86-video-qxl in user mode");
 81		return -EINVAL; /* TODO: ENODEV ? */
 82	}
 83
 84	qdev = devm_drm_dev_alloc(&pdev->dev, &qxl_driver,
 85				  struct qxl_device, ddev);
 86	if (IS_ERR(qdev)) {
 87		pr_err("Unable to init drm dev");
 88		return -ENOMEM;
 89	}
 90
 91	ret = pci_enable_device(pdev);
 92	if (ret)
 93		return ret;
 94
 95	ret = aperture_remove_conflicting_pci_devices(pdev, qxl_driver.name);
 96	if (ret)
 97		goto disable_pci;
 98
 99	if (pci_is_vga(pdev) && pdev->revision < 5) {
100		ret = vga_get_interruptible(pdev, VGA_RSRC_LEGACY_IO);
101		if (ret) {
102			DRM_ERROR("can't get legacy vga ioports\n");
103			goto disable_pci;
104		}
105	}
106
107	ret = qxl_device_init(qdev, pdev);
108	if (ret)
109		goto put_vga;
110
111	ret = qxl_modeset_init(qdev);
112	if (ret)
113		goto unload;
114
115	drm_kms_helper_poll_init(&qdev->ddev);
116
117	/* Complete initialization. */
118	ret = drm_dev_register(&qdev->ddev, ent->driver_data);
119	if (ret)
120		goto modeset_cleanup;
121
122	drm_client_setup(&qdev->ddev, NULL);
123	return 0;
124
125modeset_cleanup:
126	qxl_modeset_fini(qdev);
127unload:
128	qxl_device_fini(qdev);
129put_vga:
130	if (pci_is_vga(pdev) && pdev->revision < 5)
131		vga_put(pdev, VGA_RSRC_LEGACY_IO);
132disable_pci:
133	pci_disable_device(pdev);
134
135	return ret;
136}
137
138static void qxl_drm_release(struct drm_device *dev)
139{
140	struct qxl_device *qdev = to_qxl(dev);
141
142	/*
143	 * TODO: qxl_device_fini() call should be in qxl_pci_remove(),
144	 * reordering qxl_modeset_fini() + qxl_device_fini() calls is
145	 * non-trivial though.
146	 */
147	qxl_modeset_fini(qdev);
148	qxl_device_fini(qdev);
149}
150
151static void
152qxl_pci_remove(struct pci_dev *pdev)
153{
154	struct drm_device *dev = pci_get_drvdata(pdev);
155
156	drm_dev_unregister(dev);
157	drm_atomic_helper_shutdown(dev);
158	if (pci_is_vga(pdev) && pdev->revision < 5)
159		vga_put(pdev, VGA_RSRC_LEGACY_IO);
160}
161
162static void
163qxl_pci_shutdown(struct pci_dev *pdev)
164{
165	drm_atomic_helper_shutdown(pci_get_drvdata(pdev));
166}
167
168DEFINE_DRM_GEM_FOPS(qxl_fops);
 
 
 
 
 
 
 
169
170static int qxl_drm_freeze(struct drm_device *dev)
171{
172	struct pci_dev *pdev = to_pci_dev(dev->dev);
173	struct qxl_device *qdev = to_qxl(dev);
174	int ret;
175
176	ret = drm_mode_config_helper_suspend(dev);
177	if (ret)
178		return ret;
 
 
 
 
 
 
 
 
 
179
180	qxl_destroy_monitors_object(qdev);
181	qxl_surf_evict(qdev);
182	qxl_vram_evict(qdev);
183
184	while (!qxl_check_idle(qdev->command_ring));
185	while (!qxl_check_idle(qdev->release_ring))
186		qxl_queue_garbage_collect(qdev, 1);
187
188	pci_save_state(pdev);
189
190	return 0;
191}
192
193static int qxl_drm_resume(struct drm_device *dev, bool thaw)
194{
195	struct qxl_device *qdev = to_qxl(dev);
196
197	qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
198	if (!thaw) {
199		qxl_reinit_memslots(qdev);
 
200	}
201
202	qxl_create_monitors_object(qdev);
203	return drm_mode_config_helper_resume(dev);
 
 
 
 
 
 
 
204}
205
206static int qxl_pm_suspend(struct device *dev)
207{
208	struct pci_dev *pdev = to_pci_dev(dev);
209	struct drm_device *drm_dev = pci_get_drvdata(pdev);
210	int error;
211
212	error = qxl_drm_freeze(drm_dev);
213	if (error)
214		return error;
215
216	pci_disable_device(pdev);
217	pci_set_power_state(pdev, PCI_D3hot);
218	return 0;
219}
220
221static int qxl_pm_resume(struct device *dev)
222{
223	struct pci_dev *pdev = to_pci_dev(dev);
224	struct drm_device *drm_dev = pci_get_drvdata(pdev);
225	struct qxl_device *qdev = to_qxl(drm_dev);
226
227	pci_set_power_state(pdev, PCI_D0);
228	pci_restore_state(pdev);
229	if (pci_enable_device(pdev)) {
230		return -EIO;
231	}
232
233	qxl_io_reset(qdev);
234	return qxl_drm_resume(drm_dev, false);
235}
236
237static int qxl_pm_thaw(struct device *dev)
238{
239	struct drm_device *drm_dev = dev_get_drvdata(dev);
 
240
241	return qxl_drm_resume(drm_dev, true);
242}
243
244static int qxl_pm_freeze(struct device *dev)
245{
246	struct drm_device *drm_dev = dev_get_drvdata(dev);
 
247
248	return qxl_drm_freeze(drm_dev);
249}
250
251static int qxl_pm_restore(struct device *dev)
252{
253	struct pci_dev *pdev = to_pci_dev(dev);
254	struct drm_device *drm_dev = pci_get_drvdata(pdev);
255	struct qxl_device *qdev = to_qxl(drm_dev);
256
257	qxl_io_reset(qdev);
258	return qxl_drm_resume(drm_dev, false);
259}
260
261static const struct dev_pm_ops qxl_pm_ops = {
262	.suspend = qxl_pm_suspend,
263	.resume = qxl_pm_resume,
264	.freeze = qxl_pm_freeze,
265	.thaw = qxl_pm_thaw,
266	.poweroff = qxl_pm_freeze,
267	.restore = qxl_pm_restore,
268};
269static struct pci_driver qxl_pci_driver = {
270	 .name = DRIVER_NAME,
271	 .id_table = pciidlist,
272	 .probe = qxl_pci_probe,
273	 .remove = qxl_pci_remove,
274	 .shutdown = qxl_pci_shutdown,
275	 .driver.pm = &qxl_pm_ops,
276};
277
278static const struct drm_ioctl_desc qxl_ioctls[] = {
279	DRM_IOCTL_DEF_DRV(QXL_ALLOC, qxl_alloc_ioctl, DRM_AUTH),
280	DRM_IOCTL_DEF_DRV(QXL_MAP, qxl_map_ioctl, DRM_AUTH),
281	DRM_IOCTL_DEF_DRV(QXL_EXECBUFFER, qxl_execbuffer_ioctl, DRM_AUTH),
282	DRM_IOCTL_DEF_DRV(QXL_UPDATE_AREA, qxl_update_area_ioctl, DRM_AUTH),
283	DRM_IOCTL_DEF_DRV(QXL_GETPARAM, qxl_getparam_ioctl, DRM_AUTH),
284	DRM_IOCTL_DEF_DRV(QXL_CLIENTCAP, qxl_clientcap_ioctl, DRM_AUTH),
285	DRM_IOCTL_DEF_DRV(QXL_ALLOC_SURF, qxl_alloc_surf_ioctl, DRM_AUTH),
286};
287
288static struct drm_driver qxl_driver = {
289	.driver_features = DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC | DRIVER_CURSOR_HOTSPOT,
 
 
 
 
290
291	.dumb_create = qxl_mode_dumb_create,
292	.dumb_map_offset = drm_gem_ttm_dumb_map_offset,
 
293#if defined(CONFIG_DEBUG_FS)
294	.debugfs_init = qxl_debugfs_init,
 
295#endif
296	.gem_prime_import_sg_table = qxl_gem_prime_import_sg_table,
297	DRM_FBDEV_TTM_DRIVER_OPS,
 
298	.fops = &qxl_fops,
299	.ioctls = qxl_ioctls,
300	.num_ioctls = ARRAY_SIZE(qxl_ioctls),
301	.name = DRIVER_NAME,
302	.desc = DRIVER_DESC,
303	.date = DRIVER_DATE,
304	.major = 0,
305	.minor = 1,
306	.patchlevel = 0,
307
308	.release = qxl_drm_release,
309};
310
311drm_module_pci_driver_if_modeset(qxl_pci_driver, qxl_modeset);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
313MODULE_AUTHOR(DRIVER_AUTHOR);
314MODULE_DESCRIPTION(DRIVER_DESC);
315MODULE_LICENSE("GPL and additional rights");
v3.15
  1/* vim: set ts=8 sw=8 tw=78 ai noexpandtab */
  2/* qxl_drv.c -- QXL driver -*- linux-c -*-
  3 *
  4 * Copyright 2011 Red Hat, Inc.
  5 * All Rights Reserved.
  6 *
  7 * Permission is hereby granted, free of charge, to any person obtaining a
  8 * copy of this software and associated documentation files (the "Software"),
  9 * to deal in the Software without restriction, including without limitation
 10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 11 * and/or sell copies of the Software, and to permit persons to whom the
 12 * Software is furnished to do so, subject to the following conditions:
 13 *
 14 * The above copyright notice and this permission notice (including the next
 15 * paragraph) shall be included in all copies or substantial portions of the
 16 * Software.
 17 *
 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 21 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 22 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 23 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 24 * OTHER DEALINGS IN THE SOFTWARE.
 25 *
 26 * Authors:
 27 *    Dave Airlie <airlie@redhat.com>
 28 *    Alon Levy <alevy@redhat.com>
 29 */
 30
 
 
 
 31#include <linux/module.h>
 32#include <linux/console.h>
 
 
 
 
 
 
 
 
 
 
 
 
 
 33
 34#include "drmP.h"
 35#include "drm/drm.h"
 36#include "drm_crtc_helper.h"
 37#include "qxl_drv.h"
 38#include "qxl_object.h"
 39
 40extern int qxl_max_ioctls;
 41static DEFINE_PCI_DEVICE_TABLE(pciidlist) = {
 42	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_VGA << 8,
 43	  0xffff00, 0 },
 44	{ 0x1b36, 0x100, PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_DISPLAY_OTHER << 8,
 45	  0xffff00, 0 },
 46	{ 0, 0, 0 },
 47};
 48MODULE_DEVICE_TABLE(pci, pciidlist);
 49
 50static int qxl_modeset = -1;
 51int qxl_num_crtc = 4;
 52
 53MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
 54module_param_named(modeset, qxl_modeset, int, 0400);
 55
 56MODULE_PARM_DESC(num_heads, "Number of virtual crtcs to expose (default 4)");
 57module_param_named(num_heads, qxl_num_crtc, int, 0400);
 58
 59static struct drm_driver qxl_driver;
 60static struct pci_driver qxl_pci_driver;
 61
 62static int
 63qxl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 64{
 
 
 
 65	if (pdev->revision < 4) {
 66		DRM_ERROR("qxl too old, doesn't support client_monitors_config,"
 67			  " use xf86-video-qxl in user mode");
 68		return -EINVAL; /* TODO: ENODEV ? */
 69	}
 70	return drm_get_pci_dev(pdev, ent, &qxl_driver);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 71}
 72
 73static void
 74qxl_pci_remove(struct pci_dev *pdev)
 75{
 76	struct drm_device *dev = pci_get_drvdata(pdev);
 77
 78	drm_put_dev(dev);
 
 
 
 
 
 
 
 
 
 79}
 80
 81static const struct file_operations qxl_fops = {
 82	.owner = THIS_MODULE,
 83	.open = drm_open,
 84	.release = drm_release,
 85	.unlocked_ioctl = drm_ioctl,
 86	.poll = drm_poll,
 87	.mmap = qxl_mmap,
 88};
 89
 90static int qxl_drm_freeze(struct drm_device *dev)
 91{
 92	struct pci_dev *pdev = dev->pdev;
 93	struct qxl_device *qdev = dev->dev_private;
 94	struct drm_crtc *crtc;
 95
 96	drm_kms_helper_poll_disable(dev);
 97
 98	console_lock();
 99	qxl_fbdev_set_suspend(qdev, 1);
100	console_unlock();
101
102	/* unpin the front buffers */
103	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
104		struct drm_crtc_helper_funcs *crtc_funcs = crtc->helper_private;
105		if (crtc->enabled)
106			(*crtc_funcs->disable)(crtc);
107	}
108
109	qxl_destroy_monitors_object(qdev);
110	qxl_surf_evict(qdev);
111	qxl_vram_evict(qdev);
112
113	while (!qxl_check_idle(qdev->command_ring));
114	while (!qxl_check_idle(qdev->release_ring))
115		qxl_queue_garbage_collect(qdev, 1);
116
117	pci_save_state(pdev);
118
119	return 0;
120}
121
122static int qxl_drm_resume(struct drm_device *dev, bool thaw)
123{
124	struct qxl_device *qdev = dev->dev_private;
125
126	qdev->ram_header->int_mask = QXL_INTERRUPT_MASK;
127	if (!thaw) {
128		qxl_reinit_memslots(qdev);
129		qxl_ring_init_hdr(qdev->release_ring);
130	}
131
132	qxl_create_monitors_object(qdev);
133	drm_helper_resume_force_mode(dev);
134
135	console_lock();
136	qxl_fbdev_set_suspend(qdev, 0);
137	console_unlock();
138
139	drm_kms_helper_poll_enable(dev);
140	return 0;
141}
142
143static int qxl_pm_suspend(struct device *dev)
144{
145	struct pci_dev *pdev = to_pci_dev(dev);
146	struct drm_device *drm_dev = pci_get_drvdata(pdev);
147	int error;
148
149	error = qxl_drm_freeze(drm_dev);
150	if (error)
151		return error;
152
153	pci_disable_device(pdev);
154	pci_set_power_state(pdev, PCI_D3hot);
155	return 0;
156}
157
158static int qxl_pm_resume(struct device *dev)
159{
160	struct pci_dev *pdev = to_pci_dev(dev);
161	struct drm_device *drm_dev = pci_get_drvdata(pdev);
 
162
163	pci_set_power_state(pdev, PCI_D0);
164	pci_restore_state(pdev);
165	if (pci_enable_device(pdev)) {
166		return -EIO;
167	}
168
 
169	return qxl_drm_resume(drm_dev, false);
170}
171
172static int qxl_pm_thaw(struct device *dev)
173{
174	struct pci_dev *pdev = to_pci_dev(dev);
175	struct drm_device *drm_dev = pci_get_drvdata(pdev);
176
177	return qxl_drm_resume(drm_dev, true);
178}
179
180static int qxl_pm_freeze(struct device *dev)
181{
182	struct pci_dev *pdev = to_pci_dev(dev);
183	struct drm_device *drm_dev = pci_get_drvdata(pdev);
184
185	return qxl_drm_freeze(drm_dev);
186}
187
188static int qxl_pm_restore(struct device *dev)
189{
190	struct pci_dev *pdev = to_pci_dev(dev);
191	struct drm_device *drm_dev = pci_get_drvdata(pdev);
192	struct qxl_device *qdev = drm_dev->dev_private;
193
194	qxl_io_reset(qdev);
195	return qxl_drm_resume(drm_dev, false);
196}
197
198static const struct dev_pm_ops qxl_pm_ops = {
199	.suspend = qxl_pm_suspend,
200	.resume = qxl_pm_resume,
201	.freeze = qxl_pm_freeze,
202	.thaw = qxl_pm_thaw,
203	.poweroff = qxl_pm_freeze,
204	.restore = qxl_pm_restore,
205};
206static struct pci_driver qxl_pci_driver = {
207	 .name = DRIVER_NAME,
208	 .id_table = pciidlist,
209	 .probe = qxl_pci_probe,
210	 .remove = qxl_pci_remove,
 
211	 .driver.pm = &qxl_pm_ops,
212};
213
 
 
 
 
 
 
 
 
 
 
214static struct drm_driver qxl_driver = {
215	.driver_features = DRIVER_GEM | DRIVER_MODESET |
216			   DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED,
217	.dev_priv_size = 0,
218	.load = qxl_driver_load,
219	.unload = qxl_driver_unload,
220
221	.dumb_create = qxl_mode_dumb_create,
222	.dumb_map_offset = qxl_mode_dumb_mmap,
223	.dumb_destroy = drm_gem_dumb_destroy,
224#if defined(CONFIG_DEBUG_FS)
225	.debugfs_init = qxl_debugfs_init,
226	.debugfs_cleanup = qxl_debugfs_takedown,
227#endif
228	.gem_free_object = qxl_gem_object_free,
229	.gem_open_object = qxl_gem_object_open,
230	.gem_close_object = qxl_gem_object_close,
231	.fops = &qxl_fops,
232	.ioctls = qxl_ioctls,
233	.irq_handler = qxl_irq_handler,
234	.name = DRIVER_NAME,
235	.desc = DRIVER_DESC,
236	.date = DRIVER_DATE,
237	.major = 0,
238	.minor = 1,
239	.patchlevel = 0,
 
 
240};
241
242static int __init qxl_init(void)
243{
244#ifdef CONFIG_VGA_CONSOLE
245	if (vgacon_text_force() && qxl_modeset == -1)
246		return -EINVAL;
247#endif
248
249	if (qxl_modeset == 0)
250		return -EINVAL;
251	qxl_driver.num_ioctls = qxl_max_ioctls;
252	return drm_pci_init(&qxl_driver, &qxl_pci_driver);
253}
254
255static void __exit qxl_exit(void)
256{
257	drm_pci_exit(&qxl_driver, &qxl_pci_driver);
258}
259
260module_init(qxl_init);
261module_exit(qxl_exit);
262
263MODULE_AUTHOR(DRIVER_AUTHOR);
264MODULE_DESCRIPTION(DRIVER_DESC);
265MODULE_LICENSE("GPL and additional rights");