# Fun Stuff to try: using Video Memory as a Block Device

## mdeininger

I found this interesting piece while looking for some nvram docs:

http://hedera.linuxnews.pl/_news/2002/09/03/_long/1445.html

hope nobody posted that yet, couldn't find it with a search so i guess i'd post about it *winks*

In short that's a howto on how to use odd ranges of memory as a block device, for example you can use that unused video memory on your graphics card for temporary storage. For that you have go about as follows (if you read that link above, note that you don't need xfree):

Compile the MTD stuff as modules (it should work if you compile it in, but then you'd have to reboot...)

in kernel 2.6 you need "Device Drivers"->"Memory Technology Devices" and "Device Drivers"->"Memory Technology Devices"->"Caching Block Device access to MTD devices" compiled in or as a module, and "Device Drivers"->"Memory Technology Devices"->"Self-contained MTD Device Drivers"->"Physical System RAM" as a module.

(the guide says to use the slram driver, but the phram driver seems to be more recent)

Find an area of RAM to use

use cat /proc/pci (or lspci) to figure out where your video card stores its memory. nVidia cards seem to have two memory areas, use the one that reads "prefetchable" in /proc/pci or the one that has the right amount of graphics memory with lspci.

for example, the card in my server box has the /proc/pci entry

```

  Bus  1, device   0, function  0:

    VGA compatible controller: nVidia Corporation NV18 [GeForce4 MX 440SE AGP 8x] (rev 162).

      IRQ 11.

      Master Capable.  Latency=64.  Min Gnt=5.Max Lat=1.

      Non-prefetchable 32 bit memory at 0xe8000000 [0xe8ffffff].

      Prefetchable 32 bit memory at 0xe4000000 [0xe7ffffff].

```

and i used 0xe4000000.

Calculate the right starting address for your memory device.

This is easy if you're using a headless box: just add a "Safety Mibibyte" to the base address and you should be fine:

```

 0xe4000000+0x00100000 (1MiB in hex) = 0xe4100000

```

If you're using X11, you need to reserve some memory for that -- you can calculate that by doing some math: max x-resolution * max y-resolution * max bits-per-pixel * some pages-just-to-be-sure. You will probably end up with one or two megabytes. Give it four MiB if you have enough VRAM and you're on the safe side  :Wink: 

```

 0xe4000000+0x00400000 (4MiB in hex) = 0xe4400000

```

you also need to restrict XOrg to this amount of memory by adding

```

 VideoRam 4096

```

to your graphics cards' device definition in /etc/X11/xorg.conf.

load the bugger and see if it worked  :Very Happy: 

the syntax for loading the module is like this:

```

 # modprobe phram phram=<name>,<start>,<length>

```

so i for one used:

```

 # modprobe phram phram=vram,0xe4100000,63Mi

```

you can see if it worked by doing a cat /proc/mtd. a good result looks like:

```

chronos linux # cat /proc/mtd

dev:    size   erasesize  name

mtd0: 03e00000 00001000 "vram"

```

If it worked, load mtdblock

```

 # modprobe mtdblock

```

Now you should be able to use /dev/mtdblock0 like any other regular block device

Put a filesystem on it or use it as swap, whatever you feel like.

Grin, be happy and think that's too g33k1sh to be of any apparent use  :Very Happy: 

------

jeez, linux does have some odd things you can do with it doesn't it?  :Very Happy: 

come to think of it, that could actually come in handy as swap space...

----------

## rojaro

Hi,

thats somewhat interessting ... i've got a small file server running which is equiped with an older GF2 card which has 64MB of RAM onboard. Since i almost never have a monitor attached to it, this piece of memory could be used for something else. But i cant think of anything usefull to do with that "won" memory.

- rojaro -

----------

## mdeininger

well, it's the same for me, i've no clue what to use that for on my fileserver box... it could be handy for my router -- the os on that thing is 8 megs and its running off a 64 megs compactflash; i could squeeze that into the gfx ram on boot, so that the frequently written-to logfiles don't kill the flashcard or make a cow filesystem with the flash memory ro and everything written going to the gfx ram. but the logs are already in main ram i think -- + the router is running freebsd anyway so that's  not gonna help there

*shrugs* i made an fs on that and put my logs on it -- that way i got a ramdisk that doesn't eat "real" ram but still acts like one.

i *think* the driver was originally intended to be used for any excess memory that your motherboard couldn't handle, for example some old boards where the bios couldn't handle more than 64 megs of ram could contain dimms with more and you're supposedly able to use that driver on any ram above 64 megs and then you could make that excess memory a swapfile, thus gaining your full ram. dirty hack but supposedly works.

it's interesting to see it works on gfx memory tho  :Very Happy: 

----------

## arach

I wonder how fast this VRAM is, if it's not slow as hell it could be used as a swap device  :Wink: 

----------

## artworcs

I've followed the instructions and managed to create a 48Mb ext2 partition in the video memory. But it seems a little slow. hdparm gives about 5Mb/s and file copy is ~ 2Mb/s.

----------

## NewBlackDak

I wonder if faster video ram would make faster mem?

Anyone tried it with PCI-E cards?

----------

## nephros

Sick -- sick! -- bastards.  :Cool: 

Now if I read this correctly a little typo in one of the modprobe lines could have you overwriting your BIOS/CMOS nvram right?

----------

## mdeininger

lol, i dunno if it could do that actually -- that would probably only work if linux maps the bios to somewhere since it's running in protected mode? if thought you could only get access to the bios flash area in real mode on x86? now the cmos bit, that might work ...  :Very Happy: 

about that thing with the vram being slow... i think that could be a problem with the graphics card. i only get about 5megs/sec myself but i thought that was due to my garphics card being slow, old and in an agp 1x slot  :Wink: .

still i agree, 5megs/sec is rather slow and makes the thing useless -- as NewBlackDak said, did anyone try this with a pci-e card?  :Smile: 

----------

## artworcs

I know that its an old post, but i recently found out that the limitation is in the AGP. AGP has a bandwith of 2.1GB/s processor->videocard, but only 0.2GB/s in the oposite way. This limitation was for a simple reason: because of the design of the AGP for games, it was never imagined that the information sould be sent anywhere else but to the screen. This limitaion does not exist with a PCI Express card as it has 4G bandwith both ways. 

offtopic: one can write shader programs to take advantage of the processing power of modern graphics cards. As a demonstration of the processing power of modern graphics card, here is a small benchmark:

Sorting of an array using QuickSort containing 18000000 element takes ~17 sec on a P4 3.4GHz but only ~2 sec on a GeForce 6800 Ultra. 

More information can be found on: http://gpgpu.org/

----------

## mdeininger

ah, that explains a lot! actually, that even explains why framebuffer effects are so very much slower on an x86 compared to -- say -- an n64.

thanks very much for that information *smile*

----------

## pilo

```

hdparm -tT /dev/mtdblock0 

/dev/mtdblock0:

 Timing cached reads:   2724 MB in  2.00 seconds = 1361.91 MB/sec

HDIO_DRIVE_CMD(null) (wait for flush complete) failed: Inappropriate ioctl for device

 Timing buffered disk reads:   12 MB in  3.43 seconds =   3.50 MB/sec

HDIO_DRIVE_CMD(null) (wait for flush complete) failed: Inappropriate ioctl for device

```

time cp gives about 30-35MiB/s

This on a 6600GT PCI-E.

----------

## tcx

Well, I used it on my headless mac mini G4 for a swap device.

I wrote an init script that did a mkswap /dev/mtdblock0 and swapon -p 1 /dev/mtdblock0 after loading the modules.

This way this swap had to be filled in order for the hdd swap to start being used.

Better to use this "free space" as a swap area than no to use it at all.

The video card is a ATI with 64MB ram.

----------

## suicidal_orange_II

I was just playing about with this, but it doesn't seem to work.  I get a device of the specified size that I can partition, but on writing  (in fdisk) it complains and can't re-read the partition table.  

I disabled X just to test this, so followed the example to the letter  :Sad: 

I'm using a geforce 8800gts, which shows as having 

```

01:00.0 VGA compatible controller: nVidia Corporation G80 [GeForce 8800 GTS] (rev a2) (prog-if 00 [VGA])

        Subsystem: nVidia Corporation Unknown device 0420

        Flags: bus master, fast devsel, latency 0, IRQ 16

        Memory at e2000000 (32-bit, non-prefetchable) [size=16M]

        Memory at d0000000 (64-bit, prefetchable) [size=256M]

        Memory at e0000000 (64-bit, non-prefetchable) [size=32M]

        I/O ports at 3000 [size=128]

        Capabilities: [60] Power Management version 2

        Capabilities: [68] Message Signalled Interrupts: Mask- 64bit+ Queue=0/0 Enable-

        Capabilities: [78] Express Endpoint IRQ 0

```

Anyone know if it showing the wrong memory size as prefetchable is a problem? its a 320mb card.

If anyone can direct me to some "further reading" I'd happily read it, but all I find are patches and changelogs, and nothing when addding 8800gts

Thanks in advance,

Suicidal_Orange

----------

## tr3

 *suicidal_orange_II wrote:*   

> I was just playing about with this, but it doesn't seem to work.  I get a device of the specified size that I can partition, but on writing  (in fdisk) it complains and can't re-read the partition table.  
> 
> I disabled X just to test this, so followed the example to the letter 
> 
> I'm using a geforce 8800gts, which shows as having 
> ...

 

hi, i was trying this too (with a geforce 6200) but it doesn't work for me too, i created an ext2fs on the mtd device (mtdblock0) but when i try to write on it i can write only 24Mbytes, then i get an i/o error (if X is running when i get that error i get a lot of glitches on the monitor)

btw, i'm still trying it, if i find a solution i'll tell you.

ps: nv drivers doesn't support VideoRam option (xorg.conf), i had to change a couple of lines in the sources to get X use only 40M of VRAM

----------

