Welcome!

Linux Authors: Liz McMillan, Frank Huerta, Elizabeth White, Pat Romanski, Sandi Mappic

Related Topics: Linux

Linux: Article

How to make an Nvidia GeForce work with a Linux 2.5 kernel

The Nvidia accelerated driver is not an official part of the kernel source tree. How to make it part of yours.

(LinuxWorld) -- If you have an Nvidia GeForce card and you're using it to play games under Linux, then you must be using the Nvidia Linux drivers for your card. (See resources for links to previous columns covering these drivers.) Playing games like Unreal Tournament and Quake 3 is nothing like living dangerously, unless you're playing them at work and your boss happens to drop by. If you really want to live dangerously, however, you should try playing them with a kernel from the latest development branch, 2.5.

You probably can't, because the Nvidia accelerated driver won't compile with the latest 2.5 kernels. At least, not until you follow the tips in this column. (Although I make no guarantees -- what works on my system may not work on yours.)

Kernel tree tangent

In case you're reasonably new to Linux, here's how the kernels progress. The latest version 2.0.x kernel (currently at 2.0.39 as of this writing) is the oldest but theoretically the most stable Linux kernel available. Naturally, it lacks some of the features and hardware support available in later kernels. The 2.2.x branch (the latest of which is currently 2.2.20) is also extremely stable but it, too, lacks features and hardware support you can find in later kernels. The most recent stable branch is 2.4.x, of which 2.4.17 is the latest. Some folks do not consider 2.4 to be as stable a branch of kernels as the previous stable branches, but with a few exceptions the 2.4 kernels have been working extremely well for my limited needs.

You probably noticed by now the second numbers in all stable branches are even numbers, such as 2.0, 2.2, and 2.4. The even number tells you that you are using what is considered a stable kernel. The odd numbers in between tell you the kernel is still in development. For example, the development branch 2.1.x existed long before the first 2.2.x kernel was released. (Coincidentally, the first number, 2, is even, but that has no meaning. It will change when Linux gets to version 3.0.)

Version 2.5 is the latest development branch. Here's the problem. There are many significant changes scheduled for the 2.6 release of the Linux kernel, and these changes are being merged in to 2.5 little by little. Many of them are under the hood changes that most people won't know are there. However, kernel developers are keenly aware of these changes, because the changes break a lot of the existing kernel code. Someone eventually finds these problems, fixes them, and submits a patch for the 2.5 source code branch.

Nvidia offers its accelerated driver as a combination of source code and binary code. The 2.5 branch broke the source code portion of the Nvidia driver. Actually, the latest 2.4 kernels also broke the Nvidia driver, but not to the same extent as 2.5.

The Nvidia code is fixable. However, the Nvidia accelerated driver is not an official part of the kernel source tree. It isn't even licensed under the GPL. You may not submit a patch to the kernel to fix this driver because the Linux kernel developers don't manage it.

Tweaking Nvidia's nose

What you can do, however, is post a patch on the kernel development list so others can apply and use the patch if they wish. You can publish the patch in an article, like this one.

Does this violate the Nvidia license? I have no idea. This author wagers Nvidia isn't likely to be bent out of shape by a patch that allows more Linux users to purchase and use Nvidia-based display cards.

I wrote my own patches to the Nvidia source code to get it to compile under the latest 2.4 kernels and 2.5 kernels. I must admit Martin Huenniger did a much better job with the patch he submitted to the kernel mailing list. It is more complete than mine is, and it compiles for both the latest 2.4 and 2.5 kernels. (I haven't tried it with the latest 2.5 branch yet, but Martin claims it works). The patch below is therefore a modification of Martin's version (I fixed one minor error in his patch).

The patch is against the 2314 version of the Nvidia kernel driver. Here's how to apply it. The easiest way is to boot to the version of the kernel for which you want to create this driver. Save the patch text below to a file called nvidia.patch and place that file in the directory right above your NVIDIA 2314 kernel source directory. Change to the NVIDIA kernel source directory and enter the command cat ../nvidia.patch | patch -p1 --dry-run. If everything looks like it worked, do it again without the --dry-run switch. Then run make clean and make install.

If it's going to work at all, you should be able to see the NVdriver module in the list of installed modules (use the command lsmod to get a list of installed modules). In some cases, you may have to install the module yourself with the command modprobe NVdriver, and then check with lsmod to see if it worked.

Finally, it is possible the patch below won't work at all for you if something about the white space (spaces and tabs) is changed by publishing it on LinuxWorld or by your browser. I'll place a gzipped version of the patch in the downloads section of www.varlinux.org just in case you have problems. Enjoy, and let me know how or if it works for you. (Editor's note: Readers can also find a text-only version of the following at http://www.linuxworld.com/site-stories/2002/0222.nvidiacode.txt.)

diff -Nru NVIDIA_kernel-1.0-2314/nv.c NVIDIA_kernel-1.0-2314.new/nv.c
--- NVIDIA_kernel-1.0-2314/nv.c  Fri Nov 30 20:11:06 2001
+++ NVIDIA_kernel-1.0-2314.new/nv.c Wed Feb 20 11:54:56 2002
@@ -50,6 +50,13 @@
#include 
#endif

+/* Since 2.5.x this is needed for the coorect lookup of the page table entry */ + +#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) +#include +#include +#endif + #include #include // pte bit definitions #include // cli(), *_flags @@ -1146,11 +1153,23 @@

/* for control device, just jump to its open routine */ /* after setting up the private data */ + + /* I don't really know the correct kernel version since when it changed */ +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) if (NV_DEVICE_IS_CONTROL_DEVICE(inode->i_rdev)) return nv_kern_ctl_open(inode, file); +#else + if (NV_DEVICE_IS_CONTROL_DEVICE(kdev_val(inode->i_rdev))) + return nv_kern_ctl_open(inode, file); +#endif

/* what device are we talking about? */ - devnum = NV_DEVICE_NUMBER(inode->i_rdev); + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) + devnum = NV_DEVICE_NUMBER(inode->i_rdev); +#else + devnum = NV_DEVICE_NUMBER(kdev_val(inode->i_rdev)); +#endif if (devnum >= NV_MAX_DEVICES) { rc = -ENODEV; @@ -1257,8 +1276,14 @@

/* for control device, just jump to its open routine */ /* after setting up the private data */ + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) if (NV_DEVICE_IS_CONTROL_DEVICE(inode->i_rdev)) return nv_kern_ctl_close(inode, file); +#else + if(NV_DEVICE_IS_CONTROL_DEVICE(kdev_val(inode->i_rdev))) + return nv_kern_ctl_close(inode, file); +#endif

NV_DMSG(nv, "close");

@@ -1383,11 +1408,21 @@ #if defined(IA64) vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); #endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) if (remap_page_range(vma->vm_start, (u32) (nv->reg_physical_address) + LINUX_VMA_OFFS(vma) - NV_MMAP_REG_OFFSET, vma->vm_end - vma->vm_start, vma->vm_page_prot)) return -EAGAIN; +#else + if (remap_page_range(vma, + vma->vm_start, + (u32) (nv->reg_physical_address) + LINUX_VMA_OFFS(vma) - NV_MMAP_REG_OFFSET, + vma->vm_end - vma->vm_start, + vma->vm_page_prot)) + return -EAGAIN; +#endif

/* mark it as IO so that we don't dump it on core dump */ vma->vm_flags |= VM_IO; @@ -1400,11 +1435,21 @@ #if defined(IA64) vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); #endif + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) if (remap_page_range(vma->vm_start, (u32) (nv->fb_physical_address) + LINUX_VMA_OFFS(vma) - NV_MMAP_FB_OFFSET, vma->vm_end - vma->vm_start, vma->vm_page_prot)) return -EAGAIN; +#else + if (remap_page_range(vma, + vma->vm_start, + (u32) (nv->fb_physical_address) + LINUX_VMA_OFFS(vma) - NV_MMAP_FB_OFFSET, + vma->vm_end - vma->vm_start, + vma->vm_page_prot)) + return -EAGAIN; +#endif

// mark it as IO so that we don't dump it on core dump vma->vm_flags |= VM_IO; @@ -1435,8 +1480,14 @@ while (pages--) { page = (unsigned long) at->page_table[i++]; + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) if (remap_page_range(start, page, PAGE_SIZE, PAGE_SHARED)) return -EAGAIN; +#else + if (remap_page_range(vma, start, page, PAGE_SIZE, PAGE_SHARED)) + return -EAGAIN; +#endif start += PAGE_SIZE; pos += PAGE_SIZE; } @@ -2298,7 +2349,11 @@ if (pmd_none(*pg_mid_dir)) goto failed;

+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) pg_table = pte_offset(pg_mid_dir, address); +#else + pg_table = pte_offset_map(pg_mid_dir, address); +#endif if (!pte_present(*pg_table)) goto failed;

diff -Nru NVIDIA_kernel-1.0-2314/os-interface.c NVIDIA_kernel-1.0-2314.new/os-interface.c --- NVIDIA_kernel-1.0-2314/os-interface.c Fri Nov 30 20:11:06 2001 +++ NVIDIA_kernel-1.0-2314.new/os-interface.c Wed Feb 20 11:52:23 2002 @@ -1445,9 +1445,15 @@

uaddr = *priv;

- /* finally, let's do it! */ - err = remap_page_range( (size_t) uaddr, (size_t) paddr, size_bytes, - PAGE_SHARED); + /* finally, let's do it! */ + +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) + err = remap_page_range( (size_t) uaddr, (size_t) paddr, size_bytes, + PAGE_SHARED); +#else + err = remap_page_range( kaddr, (size_t) uaddr, (size_t) paddr, size_bytes, + PAGE_SHARED); +#endif

if (err != 0) { @@ -1473,9 +1479,14 @@

uaddr = *priv;

- /* finally, let's do it! */ - err = remap_page_range( (size_t) uaddr, (size_t) start, size_bytes, + /* finally, let's do it! */ +#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) + err = remap_page_range( (size_t) uaddr, (size_t) start, size_bytes, + PAGE_SHARED); +#else + err = remap_page_range( *priv, (size_t) uaddr, (size_t) start, size_bytes, PAGE_SHARED); +#endif

if (err != 0) { @@ -2027,13 +2038,25 @@

agp_addr = agpinfo.aper_base + (agp_data->offset << PAGE_SHIFT);

+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0) err = remap_page_range(vma->vm_start, (size_t) agp_addr, agp_data->num_pages << PAGE_SHIFT, #if defined(IA64) vma->vm_page_prot); #else PAGE_SHARED); -#endif +#endif /* IA64 */ + +#else + err = remap_page_range(vma, + vma->vm_start, (size_t) agp_addr, + agp_data->num_pages << PAGE_SHIFT, +#if defined(IA64) + vma->vm_page_prot); +#else + PAGE_SHARED); +#endif /* IA64 */ +#endif /* LINUX_VERSION_CODE */

if (err) { printk(KERN_ERR "NVRM: AGPGART: unable to remap %lu pages\n",

One final question

As much as I like my GeForce 3 card, I am under the impression ATI is more amenable to open source development. I also heard the ATI Radeon 8500 is a killer display card, often beating the best GeForce cards in terms of quality and speed. If it is true the ATI drivers are open source (or closer to being open source than Nvidia's), it may be worth the switch at some point.

Are there any ATI users out there who can confirm or refute the above claims? Are you happy with your card? Did you have any trouble getting it working under Linux? Can you play accelerated 3D games in Linux with this card? Drop me a line, and I'll pass along any interesting information you share.

More Stories By Nicholas Petreley

Nicholas Petreley is a computer consultant and author in Asheville, NC.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.