Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 11 Mar 1999 12:37:34 +1100
From:      David Dawes <dawes@rf900.physics.usyd.edu.au>
To:        Mike Smith <mike@smith.net.au>
Cc:        hackers@freebsd.org
Subject:   Re: Setting MTRRs from the X server
Message-ID:  <19990311123734.G15533@rf900.physics.usyd.edu.au>
In-Reply-To: <199903090149.RAA02067@dingo.cdrom.com>; from Mike Smith on Mon, Mar 08, 1999 at 05:49:47PM -0800
References:  <19990307153350.P4858@rf900.physics.usyd.edu.au> <199903090149.RAA02067@dingo.cdrom.com>

next in thread | previous in thread | raw e-mail | index | archive | help

--C7zPtVaVf+AK4Oqc
Content-Type: text/plain; charset=us-ascii

On Mon, Mar 08, 1999 at 05:49:47PM -0800, Mike Smith wrote:
>> For XFree86 4.0, we really need to be able to have the X server request
>> specific MTRR settings for different parts of a video card's physical
>> memory address space.  While turning WC on for the framebuffer is a big
>> performance boost, a more critical issue is being able to make sure that
>> WC is turned off for areas that are used for memory mapped I/O.  We've
>> found that some BIOSs enable WC in areas that our drivers want to use
>> for MMIO.  One example is the 0xb0000-0xbffff range.
>> 
>> So what I'm looking for is an interface that our X server can use to
>> request MTRR settings.  An interface for this is present in the Linux
>> 2.2.x kernel (and we recently added code to use it), although it doesn't
>> currently allow changing the settings for the low 1MB of address space
>> (but I'm told that will be added at some point).
>
>For want of anything better, I'd guess that emulating the Linux 
>interface would probably be the way to go.  Is there a spec for it 
>somewhere that can be read?

I've included a description of the current Linux interface below, and
the <asm/mtrr.h> header from the 2.2.2 kernel.  I can send a copy of
the code we have that uses it to anyone who is interested.  They have
a /proc/mtrr device that can be accessed via read()/write() and ioctl().
When using the ioctl interface, the changes are undone when the device
is closed.  Apparently the ioctl interface for removing mtrr entries
isn't implemented in current Linux kernels, so our code uses the ASCII
interface for that.  Also, they may be replacing /proc/mtrr with
/dev/cpu/mtrr at some point.  Providing a similar ioctl interface (using
whatever device naming is consistent with "the BSD way") would
be fine for our needs, providing that it can handle the MTRRs for the low
1MB of memory.

David

--C7zPtVaVf+AK4Oqc
Content-Type: text/plain
Content-Disposition: attachment; filename="mtrr.txt"

MTRR (Memory Type Range Register) control
2 May 1998
Richard Gooch
<rgooch@atnf.csiro.au>

  On Intel Pentium Pro/Pentium II systems the Memory Type Range
  Registers (MTRRs) may be used to control processor access to memory
  ranges. This is most useful when you have a video (VGA) card on a
  PCI or AGP bus. Enabling write-combining allows bus write transfers
  to be combined into a larger transfer before bursting over the
  PCI/AGP bus. This can increase performance of image write operations
  2.5 times or more.

  The CONFIG_MTRR option creates a /proc/mtrr file which may be used
  to manipulate your MTRRs. Typically the X server should use
  this. This should have a reasonably generic interface so that
  similar control registers on other processors can be easily
  supported.


There are two interfaces to /proc/mtrr: one is an ASCII interface
which allows you to read and write. The other is an ioctl()
interface. The ASCII interface is meant for administration. The
ioctl() interface is meant for C programmes (i.e. the X server). The
interfaces are described below, with sample commands and C code.

===============================================================================
Reading MTRRs from the shell:

% cat /proc/mtrr
reg00: base=0x00000000 (   0MB), size= 128MB: write-back, count=1
reg01: base=0x08000000 ( 128MB), size=  64MB: write-back, count=1
===============================================================================
Creating MTRRs from the shell:
# echo "base=0xf8000000 size=0x400000 type=write-combining" >! /proc/mtrr

And the result thereof:
% cat /proc/mtrr
reg00: base=0x00000000 (   0MB), size= 128MB: write-back, count=1
reg01: base=0x08000000 ( 128MB), size=  64MB: write-back, count=1
reg02: base=0xf8000000 (3968MB), size=   4MB: write-combining, count=1

This is for videoram at base address 0xf8000000 and size 4 MBytes. To
find out your base address, you need to look at the output of your X
server, which tells you where the linear framebuffer address is. A
typical line that you may get is:

(--) S3: PCI: 968 rev 0, Linear FB @ 0xf8000000

Note that you should only use the value from the X server, as it may
move the framebuffer base address, so the only value you can trust is
that reported by the X server.

To find out the size of your framebuffer (what, you don't actually
know?), the following line will tell you:

(--) S3: videoram:  4096k

That's 4 MBytes, which is 0x400000 bytes (in hexadecimal).
A patch is being written for XFree86 which will make this automatic:
in other words the X server will manipulate /proc/mtrr using the
ioctl() interface, so users won't have to do anything. If you use a
commercial X server, lobby your vendor to add support for MTRRs.
===============================================================================
Removing MTRRs from the shell:
% echo "disable=2" >! /proc/mtrr
===============================================================================
Reading MTRRs from a C programme using ioctl()'s:

/*  mtrr-show.c

    Source file for mtrr-show (example programme to show MTRRs using ioctl()'s)

    Copyright (C) 1997-1998  Richard Gooch

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Richard Gooch may be reached by email at  rgooch@atnf.csiro.au
    The postal address is:
      Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
*/

/*
    This programme will use an ioctl() on /proc/mtrr to show the current MTRR
    settings. This is an alternative to reading /proc/mtrr.


    Written by      Richard Gooch   17-DEC-1997

    Last updated by Richard Gooch   2-MAY-1998


*/
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#define MTRR_NEED_STRINGS
#include <asm/mtrr.h>

#define TRUE 1
#define FALSE 0
#define ERRSTRING strerror (errno)


int main ()
{
    int fd;
    struct mtrr_gentry gentry;

    if ( ( fd = open ("/proc/mtrr", O_RDONLY, 0) ) == -1 )
    {
	if (errno == ENOENT)
	{
	    fputs ("/proc/mtrr not found: not supported or you don't have a PPro?\n",
		   stderr);
	    exit (1);
	}
	fprintf (stderr, "Error opening /proc/mtrr\t%s\n", ERRSTRING);
	exit (2);
    }
    for (gentry.regnum = 0; ioctl (fd, MTRRIOC_GET_ENTRY, &gentry) == 0;
	 ++gentry.regnum)
    {
	if (gentry.size < 1)
	{
	    fprintf (stderr, "Register: %u disabled\n", gentry.regnum);
	    continue;
	}
	fprintf (stderr, "Register: %u base: 0x%lx size: 0x%lx type: %s\n",
		 gentry.regnum, gentry.base, gentry.size,
		 mtrr_strings[gentry.type]);
    }
    if (errno == EINVAL) exit (0);
    fprintf (stderr, "Error doing ioctl(2) on /dev/mtrr\t%s\n", ERRSTRING);
    exit (3);
}   /*  End Function main  */
===============================================================================
Creating MTRRs from a C programme using ioctl()'s:

/*  mtrr-add.c

    Source file for mtrr-add (example programme to add an MTRRs using ioctl())

    Copyright (C) 1997-1998  Richard Gooch

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Richard Gooch may be reached by email at  rgooch@atnf.csiro.au
    The postal address is:
      Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
*/

/*
    This programme will use an ioctl() on /proc/mtrr to add an entry. The first
    available mtrr is used. This is an alternative to writing /proc/mtrr.


    Written by      Richard Gooch   17-DEC-1997

    Last updated by Richard Gooch   2-MAY-1998


*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#define MTRR_NEED_STRINGS
#include <asm/mtrr.h>

#define TRUE 1
#define FALSE 0
#define ERRSTRING strerror (errno)


int main (int argc, char **argv)
{
    int fd;
    struct mtrr_sentry sentry;

    if (argc != 4)
    {
	fprintf (stderr, "Usage:\tmtrr-add base size type\n");
	exit (1);
    }
    sentry.base = strtoul (argv[1], NULL, 0);
    sentry.size = strtoul (argv[2], NULL, 0);
    for (sentry.type = 0; sentry.type < MTRR_NUM_TYPES; ++sentry.type)
    {
	if (strcmp (argv[3], mtrr_strings[sentry.type]) == 0) break;
    }
    if (sentry.type >= MTRR_NUM_TYPES)
    {
	fprintf (stderr, "Illegal type: \"%s\"\n", argv[3]);
	exit (2);
    }
    if ( ( fd = open ("/proc/mtrr", O_WRONLY, 0) ) == -1 )
    {
	if (errno == ENOENT)
	{
	    fputs ("/proc/mtrr not found: not supported or you don't have a PPro?\n",
		   stderr);
	    exit (3);
	}
	fprintf (stderr, "Error opening /proc/mtrr\t%s\n", ERRSTRING);
	exit (4);
    }
    if (ioctl (fd, MTRRIOC_ADD_ENTRY, &sentry) == -1)
    {
	fprintf (stderr, "Error doing ioctl(2) on /dev/mtrr\t%s\n", ERRSTRING);
	exit (5);
    }
    fprintf (stderr, "Sleeping for 5 seconds so you can see the new entry\n");
    sleep (5);
    close (fd);
    fputs ("I've just closed /proc/mtrr so now the new entry should be gone\n",
	   stderr);
}   /*  End Function main  */
===============================================================================

--C7zPtVaVf+AK4Oqc
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="mtrr.h"

/*  Generic MTRR (Memory Type Range Register) ioctls.

    Copyright (C) 1997-1998  Richard Gooch

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    Richard Gooch may be reached by email at  rgooch@atnf.csiro.au
    The postal address is:
      Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.
*/
#ifndef _LINUX_MTRR_H
#define _LINUX_MTRR_H

#include <linux/config.h>
#include <linux/ioctl.h>

#define	MTRR_IOCTL_BASE	'M'

struct mtrr_sentry
{
    unsigned long base;    /*  Base address     */
    unsigned long size;    /*  Size of region   */
    unsigned int type;     /*  Type of region   */
};

struct mtrr_gentry
{
    unsigned int regnum;   /*  Register number  */
    unsigned long base;    /*  Base address     */
    unsigned long size;    /*  Size of region   */
    unsigned int type;     /*  Type of region   */
};

/*  These are the various ioctls  */
#define MTRRIOC_ADD_ENTRY        _IOW(MTRR_IOCTL_BASE, 0, struct mtrr_sentry)
#define MTRRIOC_SET_ENTRY        _IOW(MTRR_IOCTL_BASE, 1, struct mtrr_sentry)
#define MTRRIOC_DEL_ENTRY        _IOW(MTRR_IOCTL_BASE, 2, struct mtrr_sentry)
#define MTRRIOC_GET_ENTRY        _IOWR(MTRR_IOCTL_BASE, 3, struct mtrr_gentry)

/*  These are the region types  */
#define MTRR_TYPE_UNCACHABLE 0
#define MTRR_TYPE_WRCOMB     1
/*#define MTRR_TYPE_         2*/
/*#define MTRR_TYPE_         3*/
#define MTRR_TYPE_WRTHROUGH  4
#define MTRR_TYPE_WRPROT     5
#define MTRR_TYPE_WRBACK     6
#define MTRR_NUM_TYPES       7

#ifdef MTRR_NEED_STRINGS
static char *mtrr_strings[MTRR_NUM_TYPES] =
{
    "uncachable",               /* 0 */
    "write-combining",          /* 1 */
    "?",                        /* 2 */
    "?",                        /* 3 */
    "write-through",            /* 4 */
    "write-protect",            /* 5 */
    "write-back",               /* 6 */
};
#endif

#ifdef __KERNEL__

/*  The following functions are for use by other drivers  */
# if defined(CONFIG_MTRR) || defined(CONFIG_MTRR_MODULE)
extern int mtrr_add (unsigned long base, unsigned long size,
		     unsigned int type, char increment);
extern int mtrr_del (int reg, unsigned long base, unsigned long size);
#  else
static __inline__ int mtrr_add (unsigned long base, unsigned long size,
				unsigned int type, char increment)
{
    return -ENODEV;
}
static __inline__ int mtrr_del (int reg, unsigned long base,
				unsigned long size)
{
    return -ENODEV;
}
#  endif

/*  The following functions are for initialisation: don't use them!  */
extern int mtrr_init (void);
#  if defined(__SMP__) && defined(CONFIG_MTRR)
extern void mtrr_init_boot_cpu (void);
extern void mtrr_init_secondary_cpu (void);
#  endif

#endif

#endif  /*  _LINUX_MTRR_H  */

--C7zPtVaVf+AK4Oqc--


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-hackers" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?19990311123734.G15533>