Skip to contentSkip to conversation

drm: Add a driver for kvm emulated Cirrus

PATCH In review Matthew Garrett 5 participants 6 messages Apr 18, 2011 – Aug 2, 2011

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm: Add a driver for kvm emulated Cirrus

qemu-kvm emulates a Cirrus GPU, including its acceleration engine. We typically then run a Cirrus-specific X driver on top of this, which turns requests into commands and sends them to the emulated accelerator. This all seems to be unnecessary overhead given that we're just going to end up writing to memory from the host instead, and performance is almost certainly going to be better using an unaccelerated framebuffer and a guest-side shadow.

This patch provides a simple modesetting-only KMS driver for the hardware emulated in qemu-kvm. It's stripped down to the point where it's able to program the emulation, but would almost certainly fail miserably if asked to run on real hardware. It's intended to reduce virt overhead slightly, but also to serve as a template to writing a basic KMS driver.

The code and structure are heavily derived from Matt Turner's glint driver, with the modesetting code cribbed from cirrusfb (hence the license).

---
 drivers/gpu/drm/Kconfig                     |    8 +
 drivers/gpu/drm/Makefile                    |    1 +
 drivers/gpu/drm/cirrus/Makefile             |    6 +
 drivers/gpu/drm/cirrus/cirrus.h             |   40 ++++
 drivers/gpu/drm/cirrus/cirrus_crtc.c        |  277 +++++++++++++++++++++++++++
 drivers/gpu/drm/cirrus/cirrus_device.c      |  111 +++++++++++
 drivers/gpu/drm/cirrus/cirrus_display.c     |   70 +++++++
 drivers/gpu/drm/cirrus/cirrus_drv.c         |   91 +++++++++
 drivers/gpu/drm/cirrus/cirrus_drv.h         |  127 ++++++++++++
 drivers/gpu/drm/cirrus/cirrus_encoder.c     |  139 ++++++++++++++
 drivers/gpu/drm/cirrus/cirrus_fbdev.c       |  214 +++++++++++++++++++++
 drivers/gpu/drm/cirrus/cirrus_framebuffer.c |   48 +++++
 drivers/gpu/drm/cirrus/cirrus_kms.c         |   59 ++++++
 drivers/gpu/drm/cirrus/cirrus_mode.h        |   54 +++++
 drivers/gpu/drm/cirrus/cirrus_vga.c         |  102 ++++++++++
 15 files changed, 1347 insertions(+), 0 deletions(-)
 create mode 100644 drivers/gpu/drm/cirrus/Makefile
 create mode 100644 drivers/gpu/drm/cirrus/cirrus.h
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_crtc.c
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_device.c
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_display.c
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_drv.c
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_drv.h
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_encoder.c
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_fbdev.c
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_framebuffer.c
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_kms.c
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_mode.h
 create mode 100644 drivers/gpu/drm/cirrus/cirrus_vga.c
drivers/gpu/drm/Kconfig +8 -0
index c58f691..2f54d2f 100644
@@ -126,6 +126,14 @@ config DRM_I915_KMS
 	  the driver to bind to PCI devices, which precludes loading things
 	  like intelfb.
 
+config DRM_KVM_CIRRUS
+	tristate "Kernel modesetting driver for qemu-kvm Cirrus emulation"
+	depends on DRM && PCI
+	select FB_CFB_FILLRECT
+	select FB_CFB_COPYAREA
+	select FB_CFB_IMAGEBLIT
+	select DRM_KMS_HELPER
+
 config DRM_MGA
 	tristate "Matrox g200/g400"
 	depends on DRM && PCI
drivers/gpu/drm/Makefile +1 -0
index 89cf05a..11e486f 100644
@@ -30,6 +30,7 @@ obj-$(CONFIG_DRM_RADEON)+= radeon/
 obj-$(CONFIG_DRM_MGA)	+= mga/
 obj-$(CONFIG_DRM_I810)	+= i810/
 obj-$(CONFIG_DRM_I915)  += i915/
+obj-$(CONFIG_DRM_KVM_CIRRUS) += cirrus/
 obj-$(CONFIG_DRM_SIS)   += sis/
 obj-$(CONFIG_DRM_SAVAGE)+= savage/
 obj-$(CONFIG_DRM_VMWGFX)+= vmwgfx/
drivers/gpu/drm/cirrus/Makefile new file +6 -0
new file mode 100644
index 0000000..126dd50
@@ -0,0 +1,6 @@
+ccflags-y := -Iinclude/drm
+cirrus-y   := cirrus_crtc.o cirrus_encoder.o cirrus_device.o cirrus_display.o \
+	cirrus_drv.o cirrus_fbdev.o cirrus_framebuffer.o \
+	cirrus_kms.o cirrus_vga.o
+
+obj-$(CONFIG_DRM_KVM_CIRRUS) += cirrus.o
drivers/gpu/drm/cirrus/cirrus.h new file +40 -0
new file mode 100644
index 0000000..44ad60a
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2010 Matt Turner.
+ * Copyright 2011 Red Hat <[email protected]>
+ *
+ * This file is subject to the terms and conditions of the GNU General
+ * Public License version 2. See the file COPYING in the main
+ * directory of this archive for more details.
+ *
+ * Authors: Matthew Garrett
+ *			Matt Turner
+ */
+#ifndef __CIRRUS_H__
+#define __CIRRUS_H__
+
+#include "cirrus_mode.h"
+
+struct cirrus_mc {
+	resource_size_t			vram_size;
+	resource_size_t			vram_base;
+};
+
+struct cirrus_device {
+	struct device			*dev;
+	struct drm_device		*ddev;
+	struct pci_dev			*pdev;
+	unsigned long			flags;
+
+	resource_size_t			rmmio_base;
+	resource_size_t			rmmio_size;
+	void __iomem			*rmmio;
+
+	drm_local_map_t			*framebuffer;
+
+	struct cirrus_mc			mc;
+	struct cirrus_mode_info		mode_info;
+
+	int				num_crtc;
+};
+
+#endif				/* __CIRRUS_H__ */
drivers/gpu/drm/cirrus/cirrus_crtc.c new file +277 -0

277 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_device.c new file +111 -0

111 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_display.c new file +70 -0

70 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_drv.c new file +91 -0

91 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_drv.h new file +127 -0

127 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_encoder.c new file +139 -0

139 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_fbdev.c new file +214 -0

214 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_framebuffer.c new file +48 -0

48 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_kms.c new file +59 -0

59 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_mode.h new file +54 -0

54 lines not on this page · raw message

drivers/gpu/drm/cirrus/cirrus_vga.c new file +102 -0

102 lines not on this page · raw message

Signature · 1 line
1.7.4.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Matthew Garrett

qemu-kvm emulates a Cirrus GPU, including its acceleration engine. We typically then run a Cirrus-specific X driver on top of this, which turns requests into commands and sends them to the emulated accelerator. This all seems to be unnecessary overhead given that we're just going to end up writing to memory from the host instead, and performance is almost certainly going to be better using an unaccelerated framebuffer and a guest-side shadow.

This patch provides a simple modesetting-only KMS driver for the hardware emulated in qemu-kvm. It's stripped down to the point where it's able to program the emulation, but would almost certainly fail miserably if asked to run on real hardware. It's intended to reduce virt overhead slightly, but also to serve as a template to writing a basic KMS driver.

Quoted 3 lines

The code and structure are heavily derived from Matt Turner's glint driver, with the modesetting code cribbed from cirrusfb (hence the license).

Nice!

Quoted 1 line

+#define CIRRUS_DPMS_CLEARED (-1)

I wanted to add a DPMS_CLEARED to DRM, since it's duplicated in at least Nouveau, glint, and now cirrusfb. I guess we should fix that at some point.

The only other nit-pick I've got is that I named variables gfb and gfbdev because I'm uncreative with variable names and because glint started with a 'g'. Not important though.

Thanks, I'll have to give it a try. Please have a

Matt

#2 lore raw
In reply to Matthew Garrett

So has this been benchmarked - intuitively I'd agree and expect that a shadowfb driver ought to give best performance.

drivers/gpu/drm/cirrus/cirrus_device.c line 37

Quoted 9 lines
drivers/gpu/drm/cirrus/cirrus_device.c new file +9 -0
+/* Map the framebuffer from the card and configure the core */
+static int cirrus_vram_init(struct cirrus_device *cdev)
+{
+	int ret;
+
+	/* BAR 0 is VRAM */
+	cdev->mc.vram_base = pci_resource_start(cdev->ddev->pdev, 0);
+	/* We have 4MB of VRAM */
+	cdev->mc.vram_size = 4 * 1024 * 1024;

For real hardware at least you should check that the pci resource is at least 4Mb long before doing this, otherwise the resulting progression in the fail case is that you map some other device into user address space, which isn't good!

Quoted 6 lines
drivers/gpu/drm/cirrus/cirrus_encoder.c new file +6 -0
+static void cirrus_encoder_mode_set(struct drm_encoder *encoder,
+				struct drm_display_mode *mode,
+				struct drm_display_mode *adjusted_mode)
+{
+	struct drm_device *dev = encoder->dev;
+	struct cirrus_device *cdev = dev->dev_private;

drivers/gpu/drm/cirrus/cirrus_encoder.c line 56

Quoted 10 lines
drivers/gpu/drm/cirrus/cirrus_encoder.c new file +10 -0
+
+	unsigned char tmp;
+
+	switch (encoder->crtc->fb->bits_per_pixel) {
+	case 8:
+		tmp = 0x0;
+		break;
+	case 16:
+		/* Enable 16 bit mode */
+		WREG_HDR(0x01);

If you switch back from 16 does this not need clearing ?

Alan

#3 lore raw
Quoted 2 lines · Alan Cox

So has this been benchmarked - intuitively I'd agree and expect that a shadowfb driver ought to give best performance.

No, but it's noticably nicer to use under virt-manager. I'll try to come up with some numbers.

Quoted 6 lines
Quoted 6 lines
drivers/gpu/drm/cirrus/cirrus_device.c new file +6 -0
+/* Map the framebuffer from the card and configure the core */
+static int cirrus_vram_init(struct cirrus_device *cdev)
+{
+	int ret;
+
+	/* BAR 0 is VRAM */
Quoted 8 lines

drivers/gpu/drm/cirrus/cirrus_device.c line 37

Quoted 3 lines
drivers/gpu/drm/cirrus/cirrus_device.c new file +3 -0
+	cdev->mc.vram_base = pci_resource_start(cdev->ddev->pdev, 0);
+	/* We have 4MB of VRAM */
+	cdev->mc.vram_size = 4 * 1024 * 1024;

For real hardware at least you should check that the pci resource is at least 4Mb long before doing this, otherwise the resulting progression in the fail case is that you map some other device into user address space, which isn't good!

True. The PCI table is restrictive enough that it won't bind to real hardware, so I don't know if it's worth it to be paranoid.

Quoted 10 lines
Quoted 10 lines
drivers/gpu/drm/cirrus/cirrus_encoder.c new file +10 -0
+static void cirrus_encoder_mode_set(struct drm_encoder *encoder,
+				struct drm_display_mode *mode,
+				struct drm_display_mode *adjusted_mode)
+{
+	struct drm_device *dev = encoder->dev;
+	struct cirrus_device *cdev = dev->dev_private;
+
+	unsigned char tmp;
+
+	switch (encoder->crtc->fb->bits_per_pixel) {
Quoted 8 lines

drivers/gpu/drm/cirrus/cirrus_encoder.c line 56

Quoted 6 lines
drivers/gpu/drm/cirrus/cirrus_encoder.c new file +6 -0
+	case 8:
+		tmp = 0x0;
+		break;
+	case 16:
+		/* Enable 16 bit mode */
+		WREG_HDR(0x01);

If you switch back from 16 does this not need clearing ?

Nope. qemu just looks at this to distinguish between 15 and 16 bit, and I've no intention of supporting 15 bit...

Signature · 1 line
Matthew Garrett | [email protected]
#4 lore raw
Quoted 6 lines · Matthew Garrett
Quoted 2 lines · Alan Cox

So has this been benchmarked - intuitively I'd agree and expect that a shadowfb driver ought to give best performance.

No, but it's noticably nicer to use under virt-manager. I'll try to come up with some numbers.

x11perf shows it winning on every benchmark, with results ranging from 10% to >20000% higher.

Signature · 1 line
Matthew Garrett | [email protected]
#5 lore raw
In reply to Matthew Garrett

Hi

Quoted 14 lines · Matthew Garrett

qemu-kvm emulates a Cirrus GPU, including its acceleration engine. We typically then run a Cirrus-specific X driver on top of this, which turns requests into commands and sends them to the emulated accelerator. This all seems to be unnecessary overhead given that we're just going to end up writing to memory from the host instead, and performance is almost certainly going to be better using an unaccelerated framebuffer and a guest-side shadow.

This patch provides a simple modesetting-only KMS driver for the hardware emulated in qemu-kvm. It's stripped down to the point where it's able to program the emulation, but would almost certainly fail miserably if asked to run on real hardware. It's intended to reduce virt overhead slightly, but also to serve as a template to writing a basic KMS driver.

Quoted 3 lines

The code and structure are heavily derived from Matt Turner's glint driver, with the modesetting code cribbed from cirrusfb (hence the license).

[...]

What is the status of this KMS based drm driver for cirrus-kvm? I've been successfully testing this patch in 2.6.{38,39} and 3.0 (Debian unstable userspace, xserver-xorg-core <=2:1.10.2.902-1, xserver-xorg-video-cirrus <=1:1.3.2-4+b1, qemu-kvm <=0.14.1+dfsg-4) with pretty good results. While it shares the same issues with plain cirrus (no regressions) before this patch (inverted colors in KDE, window management issues in LXDE), the KMS based drm driver seems to be a big improvement over all.

Regards
Stefan Lippers-Hollmann