Linux: Kernel Graphics Subsystem

Submitted by Jeremy
on May 21, 2007 - 10:15am

Jesse Barnes posted a summary of recent efforts to improve the Linux kernel's support for graphics, "in collaboration with the [framebuffer] guys, we've been working on enhancing the kernel's graphics subsystem in an attempt to bring some sanity to the Linux graphics world and avoid the situation we have now where several kernel and userspace drivers compete for control of graphics devices." He then explained, "there are several reasons to pull modesetting and proper multihead support into the kernel: suspend/resume, debugging (e.g. panic), non-X uses, and more reliable VT switch," going on to offer detail on each of these listed reasons. Jesse followed these explanations with an overview of the current status of the code:

"The current codebase is still incomplete in many ways: locking needs to be (re-)added around our various list manipulation paths, we need better initial configuration logic, only the Intel driver has any support (and it's still missing suspend/resume and accelerated FB functions), we need to check modes against monitor limitations (which come from EDID or the user), CVT and GTF based mode generation still isn't used by the DRM modesetting code, and much more. I'm hoping that by posting this now, we can get some ideas about what requirements other people have for graphics on Linux so we can prioritize our work."


From: Jesse Barnes [email blocked]
To:  linux-kernel
Subject: [RFC] enhancing the kernel's graphics subsystem
Date:	Thu, 17 May 2007 14:23:45 -0700

Patches at http://www.kernel.org/pub/linux/kernel/people/jbarnes/patches
  drm-modesetting-core.patch
  drm-modesetting-i915.patch
  console-unregister.patch
(Sorry the first two are slightly too big for lkml; they're against the 
DRM tree at git://git.freedesktop.org/git/mesa/drm.)

In collaboration with the FB guys, we've been working on enhancing the 
kernel's graphics subsystem in an attempt to bring some sanity to the 
Linux graphics world and avoid the situation we have now where several 
kernel and userspace drivers compete for control of graphics devices.

In the interest of getting some early feedback, I thought I'd post a 
description of how we've structured things so far, along with some of 
the early code, to get some feedback on the direction.

Why in the kernel?

There are several reasons to pull modesetting and proper multihead 
support into the kernel:
  - suspend/resume
  - debugging (e.g. panic)
  - non-X uses
  - more reliable VT switch
Each of the above is covered in more detail below.

Suspend/resume

Currently, the kernel has to rely on an external application (X, 
vbetool, etc.), or worse, ACPI, to reset video devices to the proper 
state after resume.  If one of these systems has trouble or crashes 
during or shortly after resume, the system will become unusable, with 
little indication as to why (see Debugging below).  Putting code into 
the kernel to perform low level modesetting (i.e. without video BIOS 
support) will allow the kernel to resume to the correct mode 
automatically and more quickly than would be possible otherwise.

Debugging

As mentioned above, if something goes wrong with modesetting during 
resume, the user has very little indication of where things went wrong.  
Likewise, if a panic or oops occurs while an application like X is 
running, the user will experience a hard hang, rather than the much 
more pleasant "blue penguin of death" (to be coded).  With kernel 
modesetting support, the kernel should be able to display panic and 
oops messages directly on the console, even if a graphical application 
is running, since it would have awareness of the current mode, display 
depth, pitch, and other variables needed to display output.  Another 
possibility is multihead debugging:  one display could run the user's 
applications (i.e. a "normal" display) while a secondary display could 
run a system level debugger, allowing the user to stop the machine, 
investigate memory, step through programs, etc. (admittedly this is 
somewhat far fetched).

Non-X uses

As it stands, non-X based applications wanting to use video devices have 
two options:  either take over the hardware themselves or use the 
existing kernel fb layer.  The former is obviously a tall order given 
the complexity of current graphics devices, while the latter isn't 
featureful enough to expose multiple outputs, perform per-device 
locking so that multiple clients can share the device, etc.  A kernel 
based modsetting and multihead API would make developing such 
applications much easier.

VT switch

Currently, the kernel relies on an external program to restore the 
graphics state when a VT switch occurs.  This doesn't always work, with 
similar results to the suspend/resume case:  an apparently hung or 
unusable machine.  Of course, the kernel can't unconditionally preempt 
the graphics device to set a new mode, but having modesetting in the 
kernel will give it a much better chance of coordinating with the DRM 
command dispatch code to find a good time to set a new mode.

Interfaces

With the above patches, the kernel DRM layer manages the output devices, 
available modes, and calls into the low level DRM drivers to set modes 
and probe outputs devices for attached displays, much like the X 
server's internal RandR 1.2 APIs.  It also provides userspace with an 
interface to these functions (Jakob based these APIs on the X server's 
Randr extension, but there are differences).

DRM/FB cooperation

Another major factor to consider when enhancing modesetting in the 
kernel is DRM and FB cooperation.  Currently, FB isn't aware of DRM 
drivers, and DRM is only minimally aware of FB (such that it can bind 
to PCI devices even after FB drivers have already done so).  As a 
result, any modesetting done by either layer results in memory 
allocation that may not be honored by the other side (and/or the X 
server, which has its own idea of how memory is being used).  To 
properly address interoperability, both the FB and DRM layers need to 
share a common memory manager, common suspend/resume code, and common 
modesetting code.  In addition, applications must use these layers in 
some way to avoid conflicts (e.g. X should call into the DRM or FB 
layers to do memory allocation).

What about the FB layer?

Today, the FB layer is really only well aware of a single head, and 
doesn't do full EDID parsing, therefore its knowledge of available 
modes is limited.  On the plus side, it's able to fetch EDID data where 
possible and generate modes using the VESA CVT specification.

In kernel APIs

The kernel APIs are broken up into several parts, as documented in 
drm_crtc.h.  There are two sets of callbacks that a driver must 
implement to fully support the model, one for the driver's CRTC(s), and 
one for its supported outputs.

Userland APIs

DRM_IOCTL_MODE_GETRESOURCES - get number of CRTCs, FBs
DRM_IOCTL_MODE_GETCRTC - get info about a given CRTC
DRM_IOCTL_MODE_GETOUTPUT - get info about a given output
DRM_IOCTL_MODE_SETCRTC - set CRTC parameters
DRM_IOCTL_MODE_ADDFB - add a new FB object
DRM_IOCTL_MODE_RMFB - remove an FB object
DRM_IOCTL_MODE_GETFB - get info about an FB object

Notes on the current codebase

The current codebase is still incomplete in many ways:  locking needs to 
be (re-)added around our various list manipulation paths, we need 
better initial configuration logic, only the Intel driver has any 
support (and it's still missing suspend/resume and accelerated FB 
functions), we need to check modes against monitor limitations (which 
come from EDID or the user), CVT and GTF based mode generation still 
isn't used by the DRM modesetting code, and much more.  I'm hoping that 
by posting this now, we can get some ideas about what requirements 
other people have for graphics on Linux so we can prioritize our work.

And of course, large chunks of this code came from X.Org's modesetting 
and Intel driver code, but it should all be marked with the proper 
copyrights and licenses if it wasn't written from scratch.

Comments, questions, suggestions?

Thanks,
Jesse

Related Links:

I'm not a kernel hacker/dev

Anonymous (not verified)
on
May 21, 2007 - 2:42pm

I'm not a kernel hacker/dev but couldn't work also be done with the major graphics providers (ATI/AMD and nvidia) to get things like SLI/crossfire, better multi-monitor support, and hardware accelerated HD, blue-ray, and H.264 playback?

Vendor Support

Anonymous (not verified)
on
May 21, 2007 - 7:51pm

They could but they prefer push their binary blobs into the kernel. It seems to be a case of "Intelectual Property" s.t. which open minded people have difficulty to grasp.

These open minded people

Anonymous (not verified)
on
May 22, 2007 - 6:54am

These open minded people also seems to be poorer that the commercial dudes. I have no problem accepting that companies want to stay closed as long as possible.

That would be fine if binary

Anonymous (not verified)
on
May 22, 2007 - 9:51am

That would be fine if binary blobs worked on my PPC system, if they weren't riddled with security holes, and impossible for the kernel developers to support. Then Microsoft gets to point at open source and say it sucks, because now the quality of open source OS depends on some binary-blob vendor.

"just works"

Kokooko (not verified)
on
May 22, 2007 - 11:19am

Just look at Known Issues in nvidia and don't say that closed binary blobs "just works" :)

http://us.download.nvidia.com/XFree86/Linux-x86/100.14.06/README/chapter...

Support for other devices

Jesse Barnes (not verified)
on
May 22, 2007 - 8:26pm

Well, if you're talking about volume, Intel is the biggest due to its embedded graphics chips, but I know what you mean. :)

Yeah, there's no reason ATI and NV can't be supported in this infrastructure. In fact, there are early versions of code doing similar things for ATI, and the nouveau hackers are well on their way toward having a fully featured open source driver, so I expect them to come on board quickly.

As for the feature list you mentioned, this work is definitely targetted at good multihead support (in fact the latest X.Org releases already have very good multihead support). The other features are more suited to X driver extensions rather than kernel work though, but for Intel at least I know they're planned.

Oh, God, at last

gmv (not verified)
on
May 21, 2007 - 6:14pm

I've been thinking about this since y2k. Thank God you guys took this initiative. This project is a must. Happy coding :)

I can't agree more with you.

georg_H (not verified)
on
May 22, 2007 - 6:43pm

I can't agree more with you.

I couldn't fail to disagree with you less

Anonymous (not verified)
on
May 23, 2007 - 7:43am

.

KGI.

Leonardo L. (not verified)
on
May 21, 2007 - 10:04pm

It would be nice to have some of the KGI [http://www.kgi-project.org/] effort integrated into this enhancement.

Please stay calm, people! =P

Anonymous (not verified)
on
May 22, 2007 - 1:04am

DRM is meant for Direct Rendering Management here, not Digital Restrictions Management ;-)

Some background info on this

ctg (not verified)
on
May 22, 2007 - 1:04am

Here is a nice article from keithp which provides some background to this

http://keithp.com/blog/kernel-mode-drivers.html

Didn't we learn the lesson from MS world ?

Anonymous (not verified)
on
May 22, 2007 - 1:52am

MS and their MS WINDOWS 'whatever_version' was known for years for the BSD (BlueScreenofDeath). One of the reasons for BSD was the fact that they integrated __graphic__ drivers into the kernel to sped up rendering.... yes, 95/98/me/2000 were fast for graphics... but, on the same token, they were fast to lock up the system..

SO, is it smart to integrate graphics into the kernel ?

MS put the full GUI into the

Anonymous (not verified)
on
May 22, 2007 - 2:35am

MS put the full GUI into the kernel, while this work only adds drivers; think of this as the equivalent of ALSA.

Wrong. Windows has some

Anonymous (not verified)
on
May 22, 2007 - 3:55am

Wrong. Windows has some parts of GDI (Graphics Device Interface) that are responsible for interacting with the graphics device within the kernel; it is simply an interface for drawing graphics on the screen. All the GUI is rendered through GDI entirely in user space.

Which is completely unlike

Anonymous (not verified)
on
May 22, 2007 - 4:09pm

Which is completely unlike the X drivers in Userspace that manage to lock up my system so that even the magic SysRq key doesn't work. Right.

Any time you mess with hardware, you have the risk of lockups and other meltdowns. It doesn't matter if it's done from userspace or kernelspace.

But in the case of Linux and X, it's actually worse than Windows. Instead of having just one point of failure (a kernel mode graphics driver shim for mode-setting and video hardware management), you have several different drivers, in both kernel and userspace that compete for the hardware. Every time you do a VT switch, a very precarious dance is carried out that sometimes ends in terrible lockups. With a common graphics driver in the kernel for things like modesetting, this would no longer be a problem and would be no more dangerous (perhaps even less so) than the current situation with userspace drivers.

But in the case of Linux and

Anonymous (not verified)
on
May 22, 2007 - 5:24pm

But in the case of Linux and X, it's actually worse than Windows. Instead of having just one point of failure (a kernel mode graphics driver shim for mode-setting and video hardware management), you have several different drivers, in both kernel and userspace that compete for the hardware.

This is a good reason to move the VT drivers from the kernel to userspace, not to move more userspace into the kernel.

Mode setting and resource

Anonymous (not verified)
on
May 23, 2007 - 9:25am

Mode setting and resource sharing is NOT a failure prone component of drivers.

Oh really?

on
May 25, 2007 - 12:38pm

Try switching back and forth between X and console a few dozen times on various cards... I think you'll find a lot of mysterious failures. I had one card whose display would become "flickery" after such a cycling because who-knows-what wasn't reinitialized properly. I've had others just up and crash and not be able to restore graphics modes without a reboot. etc. etc.

How is this not failure prone?

A lot of the fun comes from the kernel expecting to do part of the job, and userspace expecting to do a different part of the job that sometimes overlaps and sometimes leaves gaps and still other times does a little of both. Really, the whole job belongs in one place or the other. Unless we want to offload *all* console responsibilities to a user space task, most of this belongs in the kernel.

Which way a given person falls on this argument depends on inclination. Linux leans towards monolithic kernel, so while it might end up with its own kernel thread, it will probably end up in the kernel. Others prefer to move all this to user space. No sense dredging up the monolithic-vs-microkernel argument here. :-) The main point is that Linux is finally doing something about the really annoying division of labor as it exists, instead putting the dividing line somewhere a little more sane.

Not in kernel space

on
May 25, 2007 - 5:12am

I do agree with you: moving drivers (graphic, audio, whatever) to kernel space makes the system less robust to failures and, in the end, creates systems more prone to deadlocks.

I would see a serious implementation of drivers in userlevel...

Learning from MS

Jesse Barnes (not verified)
on
May 22, 2007 - 8:13pm

Well, we're not integrating graphics, in any complete sense, into the kernel. This patchset mainly just lets the kernel in on what's going on with respect to how the graphics hardware is programmed. The majority of graphics programming is still done at the application level (by the X DDX and Mesa DRI drivers) but the kernel knows which mode is currently programmed so if a panic or oops occurs, we can get our very own PoD screen (penguin of death) to indicate that something bad happened.

So really, this patchset shouldn't affect stability one way or another, but should make debugging and several other things easier.

hmm, would it be possible to

turn.self.off (not verified)
on
May 25, 2007 - 8:16pm

hmm, would it be possible to have a kind of "recover from dead X" mode?

ok, for someone using X based GUI apps, its bad enough, but at least it would be less of a need to take the whole system down for a reboot after X goes belly up for some reason...

i kinda think that the kernel should know just enough to do basic 2D graphics, but not much beyond that point. but it should not hand over control of the displays to a user space app without a way to get control back if said user space app does something funny.

Same old, mal-informed criticism

Roland Nagtegaal (not verified)
on
May 23, 2007 - 7:27am

This is the same old superficial criticism that I have heard for years, and it is malinformed.

Nobody is proposing to put the entire graphics subsystem in the linux kernel. Graphics drivers are very complicated things that do many tasks, and also on many levels. Not even in Windows the entire graphics system has ever been in the windows kernel. The reason for the many crashes in windows has more to do with sloppy driver coding, graphics hardware that was not designed for a clear split between kernel and userspace, and many bugs in windows itself.

Some of the tasks of a graphics system include:
- graphics mode setting (*)
- text mode setting (*)
- negotiation between graphics cards and monitors, for finding propers modes etc. (ddc protocol) (*)
- graphics memory management (*)
- graphics card register management (*)
- 3D and 2D acceleration command stream brokering (so that multiple processes can use a GPU) (*)
- translation of OpenGL (or other systems like DirectX) into the card-specific command stream (*/**)
- drawing pixels, lines etc. (*)
- drawing and calculating triangles, shading, texture mapping etc. (**)
- hidden feature removal (**)
- drawing in windows (**)
- hardware video scaling (**)
- decoding and encoding video, or at least part of it (i.e. Fast Fourier Transforms) (**)
- overlays of television cards, webcams and such (**)
- user interface element such as widgets, window borders (***)
- graphics libraries like OpenGL (**)
- also the X window system does _networking_, it actually is a networking protocol (***)
before anything else
- textmode or graphics-textmode (like fb now) virtual terminals (***)
- X Window system
- many more

Certainly some of these thing do belong in the kernel, and indeed have been for many years. In this respect Windows and Linux are not so different. I dare say that instability caused by the design of the graphics system (or rather, lack thereof), is worse in Linux than Windows. It has been the main cause of all crashes I have ever seen in desktop Linux, from 1994 until now.
Also, non-determinism and uninterruptible code paths in the graphics system is one of the main causes of audio hickups, i.e. missing real-time deadlines I have seen.

I marked the list above with *, ** or ***, and I think that a split must be made between items as follows:

(*) part of kernel, to be used via maybe a /dev/graphics file, or a maybe a /proc/sys setting, or an IOCTL
(**) implemented in a priviliged user-space daemon, not unlike samba is a daemon that interfaces with the in-kernel networking stack.
(***) implemented in a userspace library, or an actual application, using or depending on things marked (*) or (**).

And in windows, whatever flaws it may have, you do not see the ugly flickering between graphics and textmode when logging out and in again. That should be totally unneccessary.

The problems this group are addressing are real:
- no unified memory management between different graphics subsystems
- duplicated code effort on many levels (at least mode setting and drawing)
- blindly competing graphics subsystems, causing stability issues for desktop users
- possibility of splitting graphics drivers in modular, functionally separate parts, at least in the code, if not in discrete .ko kernel modules.
- better task-switching between graphical processes.
- more determinism, bringing Linux closer to a real-time multimedia operating system
- better multi-head, and multi-seat support

Could you explain?

renoxyz (not verified)
on
May 25, 2007 - 1:18pm

What is the "- text mode setting (*)" you're writing about, AFAIK the videocards in our PCs don't have any text mode setting, only graphical so the text mode is an abstraction over this.

And I don't really understand the difference between this and virtual terminals.

Or is-it about some special devices which are text mode only?

What is the "- text mode

Anonymous (not verified)
on
May 25, 2007 - 5:03pm

What is the "- text mode setting (*)" you're writing about

80x25 and 80x40 are the most common text mode settings. There's a bunch of other ones, though.

AFAIK the videocards in our PCs don't have any text mode setting, only graphical so the text mode is an abstraction over this.

Text mode is part of the ancient VGA specification, and yes, modern graphics chipsets do have it.

OK thanks for the

renoxyz (not verified)
on
May 26, 2007 - 6:19am

OK thanks for the information.

Pretty much every unix out

on
June 2, 2007 - 6:10am

Pretty much every unix out there - except those using Xfree86 or Xorg - has graphics drivers inside the kernel.

A must needed initiative!

Anonymous (not verified)
on
May 22, 2007 - 2:34pm

A must needed initiative! The basics now work fine, but more advanced things, like hotswapping laptops to docking stations with monitors and such have never really worked for me with linux (or didn't know how to get it working)

A must needed initiative!

John Harry (not verified)
on
May 28, 2007 - 2:05pm

Multihead support in the framebuffer is a very attractive idea.The possibility of running some screens with ttys and some with X would be great.Also making the kernel more aware of what is going on on X department might avoid X from hard locking the system.X shouldn't be able to do that, the same way X applications shouldn't be able to crash/freeze X.
I can't remember the number of times X crashed on me, most of them leaving the console alone, but others requiring a kill via ssh or worst, a hard reset.
Please be very careful with this initiative, make sure it solves the current problems instead of introducing new ones.Good luck and good coding.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.