diff options
Diffstat (limited to 'xorg-server/hw/xfree86/os-support')
-rw-r--r-- | xorg-server/hw/xfree86/os-support/bsd/i386_video.c | 1816 | ||||
-rw-r--r-- | xorg-server/hw/xfree86/os-support/bus/Sbus.c | 1380 | ||||
-rw-r--r-- | xorg-server/hw/xfree86/os-support/linux/int10/linux.c | 1088 | ||||
-rw-r--r-- | xorg-server/hw/xfree86/os-support/linux/lnx_agp.c | 748 | ||||
-rw-r--r-- | xorg-server/hw/xfree86/os-support/linux/lnx_video.c | 1756 | ||||
-rw-r--r-- | xorg-server/hw/xfree86/os-support/shared/posix_tty.c | 1310 | ||||
-rw-r--r-- | xorg-server/hw/xfree86/os-support/shared/vidmem.c | 592 | ||||
-rw-r--r-- | xorg-server/hw/xfree86/os-support/solaris/sun_agp.c | 654 |
8 files changed, 4672 insertions, 4672 deletions
diff --git a/xorg-server/hw/xfree86/os-support/bsd/i386_video.c b/xorg-server/hw/xfree86/os-support/bsd/i386_video.c index 10db9c7b8..0d457193a 100644 --- a/xorg-server/hw/xfree86/os-support/bsd/i386_video.c +++ b/xorg-server/hw/xfree86/os-support/bsd/i386_video.c @@ -1,908 +1,908 @@ -/* - * Copyright 1992 by Rich Murphey <Rich@Rice.edu> - * Copyright 1993 by David Wexelblat <dwex@goblin.org> - * - * Permission to use, copy, modify, distribute, and sell this software and its - * documentation for any purpose is hereby granted without fee, provided that - * the above copyright notice appear in all copies and that both that - * copyright notice and this permission notice appear in supporting - * documentation, and that the names of Rich Murphey and David Wexelblat - * not be used in advertising or publicity pertaining to distribution of - * the software without specific, written prior permission. Rich Murphey and - * David Wexelblat make no representations about the suitability of this - * software for any purpose. It is provided "as is" without express or - * implied warranty. - * - * RICH MURPHEY AND DAVID WEXELBLAT DISCLAIM ALL WARRANTIES WITH REGARD TO - * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS, IN NO EVENT SHALL RICH MURPHEY OR DAVID WEXELBLAT BE LIABLE FOR - * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER - * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF - * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN - * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - */ - -#ifdef HAVE_XORG_CONFIG_H -#include <xorg-config.h> -#endif - -#include <X11/X.h> -#include "xf86.h" -#include "xf86Priv.h" - -#include <errno.h> -#include <sys/mman.h> - -#ifdef HAS_MTRR_SUPPORT -#ifndef __NetBSD__ -#include <sys/types.h> -#include <sys/memrange.h> -#else -#include "memrange.h" -#endif -#define X_MTRR_ID "XFree86" -#endif - -#if defined(HAS_MTRR_BUILTIN) && defined(__NetBSD__) -#include <machine/mtrr.h> -#include <machine/sysarch.h> -#include <sys/queue.h> -#ifdef __x86_64__ -#define i386_set_mtrr x86_64_set_mtrr -#define i386_get_mtrr x86_64_get_mtrr -#define i386_iopl x86_64_iopl -#endif -#endif - -#include "xf86_OSlib.h" -#include "xf86OSpriv.h" - -#if defined(__NetBSD__) && !defined(MAP_FILE) -#define MAP_FLAGS MAP_SHARED -#else -#define MAP_FLAGS (MAP_FILE | MAP_SHARED) -#endif - -#ifndef MAP_FAILED -#define MAP_FAILED ((caddr_t)-1) -#endif - -#ifdef __OpenBSD__ -#define SYSCTL_MSG "\tCheck that you have set 'machdep.allowaperture=1'\n"\ - "\tin /etc/sysctl.conf and reboot your machine\n" \ - "\trefer to xf86(4) for details" -#define SYSCTL_MSG2 \ - "Check that you have set 'machdep.allowaperture=2'\n" \ - "\tin /etc/sysctl.conf and reboot your machine\n" \ - "\trefer to xf86(4) for details" -#endif - -/***************************************************************************/ -/* Video Memory Mapping section */ -/***************************************************************************/ - -static Bool useDevMem = FALSE; -static int devMemFd = -1; - -#ifdef HAS_APERTURE_DRV -#define DEV_APERTURE "/dev/xf86" -#endif -#define DEV_MEM "/dev/mem" - -static pointer mapVidMem(int, unsigned long, unsigned long, int); -static void unmapVidMem(int, pointer, unsigned long); - -#ifdef HAS_MTRR_SUPPORT -static pointer setWC(int, unsigned long, unsigned long, Bool, MessageType); -static void undoWC(int, pointer); -static Bool cleanMTRR(void); -#endif -#if defined(HAS_MTRR_BUILTIN) && defined(__NetBSD__) -static pointer NetBSDsetWC(int, unsigned long, unsigned long, Bool, - MessageType); -static void NetBSDundoWC(int, pointer); -#endif - -/* - * Check if /dev/mem can be mmap'd. If it can't print a warning when - * "warn" is TRUE. - */ -static void -checkDevMem(Bool warn) -{ - static Bool devMemChecked = FALSE; - int fd; - pointer base; - - if (devMemChecked) - return; - devMemChecked = TRUE; - - if ((fd = open(DEV_MEM, O_RDWR)) >= 0) - { - /* Try to map a page at the VGA address */ - base = mmap((caddr_t)0, 4096, PROT_READ | PROT_WRITE, - MAP_FLAGS, fd, (off_t)0xA0000); - - if (base != MAP_FAILED) - { - munmap((caddr_t)base, 4096); - devMemFd = fd; - useDevMem = TRUE; - return; - } else { - /* This should not happen */ - if (warn) - { - xf86Msg(X_WARNING, "checkDevMem: failed to mmap %s (%s)\n", - DEV_MEM, strerror(errno)); - } - useDevMem = FALSE; - return; - } - } -#ifndef HAS_APERTURE_DRV - if (warn) - { - xf86Msg(X_WARNING, "checkDevMem: failed to open %s (%s)\n", - DEV_MEM, strerror(errno)); - } - useDevMem = FALSE; - return; -#else - /* Failed to open /dev/mem, try the aperture driver */ - if ((fd = open(DEV_APERTURE, O_RDWR)) >= 0) - { - /* Try to map a page at the VGA address */ - base = mmap((caddr_t)0, 4096, PROT_READ | PROT_WRITE, - MAP_FLAGS, fd, (off_t)0xA0000); - - if (base != MAP_FAILED) - { - munmap((caddr_t)base, 4096); - devMemFd = fd; - useDevMem = TRUE; - xf86Msg(X_INFO, "checkDevMem: using aperture driver %s\n", - DEV_APERTURE); - return; - } else { - - if (warn) - { - xf86Msg(X_WARNING, "checkDevMem: failed to mmap %s (%s)\n", - DEV_APERTURE, strerror(errno)); - } - } - } else { - if (warn) - { -#ifndef __OpenBSD__ - xf86Msg(X_WARNING, "checkDevMem: failed to open %s and %s\n" - "\t(%s)\n", DEV_MEM, DEV_APERTURE, strerror(errno)); -#else /* __OpenBSD__ */ - xf86Msg(X_WARNING, "checkDevMem: failed to open %s and %s\n" - "\t(%s)\n%s", DEV_MEM, DEV_APERTURE, strerror(errno), - SYSCTL_MSG); -#endif /* __OpenBSD__ */ - } - } - - useDevMem = FALSE; - return; - -#endif -} - -void -xf86OSInitVidMem(VidMemInfoPtr pVidMem) -{ - checkDevMem(TRUE); - pVidMem->linearSupported = useDevMem; - pVidMem->mapMem = mapVidMem; - pVidMem->unmapMem = unmapVidMem; - -#if HAVE_PCI_SYSTEM_INIT_DEV_MEM - if (useDevMem) - pci_system_init_dev_mem(devMemFd); -#endif - -#ifdef HAS_MTRR_SUPPORT - if (useDevMem) { - if (cleanMTRR()) { - pVidMem->setWC = setWC; - pVidMem->undoWC = undoWC; - } - } -#endif -#if defined(HAS_MTRR_BUILTIN) && defined(__NetBSD__) - pVidMem->setWC = NetBSDsetWC; - pVidMem->undoWC = NetBSDundoWC; -#endif - pVidMem->initialised = TRUE; -} - -static pointer -mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags) -{ - pointer base; - - checkDevMem(FALSE); - - if (useDevMem) - { - if (devMemFd < 0) - { - FatalError("xf86MapVidMem: failed to open %s (%s)", - DEV_MEM, strerror(errno)); - } - base = mmap((caddr_t)0, Size, - (flags & VIDMEM_READONLY) ? - PROT_READ : (PROT_READ | PROT_WRITE), - MAP_FLAGS, devMemFd, (off_t)Base); - if (base == MAP_FAILED) - { - FatalError("%s: could not mmap %s [s=%lx,a=%lx] (%s)", - "xf86MapVidMem", DEV_MEM, Size, Base, - strerror(errno)); - } - return(base); - } - - /* else, mmap /dev/vga */ - if ((unsigned long)Base < 0xA0000 || (unsigned long)Base >= 0xC0000) - { - FatalError("%s: Address 0x%lx outside allowable range", - "xf86MapVidMem", Base); - } - base = mmap(0, Size, - (flags & VIDMEM_READONLY) ? - PROT_READ : (PROT_READ | PROT_WRITE), - MAP_FLAGS, xf86Info.screenFd, - (unsigned long)Base - 0xA0000 - ); - if (base == MAP_FAILED) - { - FatalError("xf86MapVidMem: Could not mmap /dev/vga (%s)", - strerror(errno)); - } - return(base); -} - -static void -unmapVidMem(int ScreenNum, pointer Base, unsigned long Size) -{ - munmap((caddr_t)Base, Size); -} - -/* - * Read BIOS via mmap()ing DEV_MEM - */ - -int -xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf, - int Len) -{ - unsigned char *ptr; - int psize; - int mlen; - - checkDevMem(TRUE); - if (devMemFd == -1) { - return(-1); - } - - psize = getpagesize(); - Offset += Base & (psize - 1); - Base &= ~(psize - 1); - mlen = (Offset + Len + psize - 1) & ~(psize - 1); - ptr = (unsigned char *)mmap((caddr_t)0, mlen, PROT_READ, - MAP_SHARED, devMemFd, (off_t)Base); - if ((long)ptr == -1) - { - xf86Msg(X_WARNING, - "xf86ReadBIOS: %s mmap[s=%x,a=%lx,o=%lx] failed (%s)\n", - DEV_MEM, Len, Base, Offset, strerror(errno)); -#ifdef __OpenBSD__ - if (Base < 0xa0000) { - xf86Msg(X_WARNING, SYSCTL_MSG2); - } -#endif - return(-1); - } -#ifdef DEBUG - ErrorF("xf86ReadBIOS: BIOS at 0x%08x has signature 0x%04x\n", - Base, ptr[0] | (ptr[1] << 8)); -#endif - (void)memcpy(Buf, (void *)(ptr + Offset), Len); - (void)munmap((caddr_t)ptr, mlen); -#ifdef DEBUG - xf86MsgVerb(X_INFO, 3, "xf86ReadBIOS(%x, %x, Buf, %x)" - "-> %02x %02x %02x %02x...\n", - Base, Offset, Len, Buf[0], Buf[1], Buf[2], Buf[3]); -#endif - return(Len); -} - -#ifdef USE_I386_IOPL -/***************************************************************************/ -/* I/O Permissions section */ -/***************************************************************************/ - -static Bool ExtendedEnabled = FALSE; - -Bool -xf86EnableIO() -{ - if (ExtendedEnabled) - return TRUE; - - if (i386_iopl(TRUE) < 0) - { -#ifndef __OpenBSD__ - xf86Msg(X_WARNING,"%s: Failed to set IOPL for extended I/O", - "xf86EnableIO"); -#else - xf86Msg(X_WARNING,"%s: Failed to set IOPL for extended I/O\n%s", - "xf86EnableIO", SYSCTL_MSG); -#endif - return FALSE; - } - ExtendedEnabled = TRUE; - - return TRUE; -} - -void -xf86DisableIO() -{ - if (!ExtendedEnabled) - return; - - i386_iopl(FALSE); - ExtendedEnabled = FALSE; - - return; -} - -#endif /* USE_I386_IOPL */ - -#ifdef USE_AMD64_IOPL -/***************************************************************************/ -/* I/O Permissions section */ -/***************************************************************************/ - -static Bool ExtendedEnabled = FALSE; - -Bool -xf86EnableIO() -{ - if (ExtendedEnabled) - return TRUE; - - if (amd64_iopl(TRUE) < 0) - { -#ifndef __OpenBSD__ - xf86Msg(X_WARNING,"%s: Failed to set IOPL for extended I/O", - "xf86EnableIO"); -#else - xf86Msg(X_WARNING,"%s: Failed to set IOPL for extended I/O\n%s", - "xf86EnableIO", SYSCTL_MSG); -#endif - return FALSE; - } - ExtendedEnabled = TRUE; - - return TRUE; -} - -void -xf86DisableIO() -{ - if (!ExtendedEnabled) - return; - - if (amd64_iopl(FALSE) == 0) { - ExtendedEnabled = FALSE; - } - /* Otherwise, the X server has revoqued its root uid, - and thus cannot give up IO privileges any more */ - - return; -} - -#endif /* USE_AMD64_IOPL */ - -#ifdef USE_DEV_IO -static int IoFd = -1; - -Bool -xf86EnableIO() -{ - if (IoFd >= 0) - return TRUE; - - if ((IoFd = open("/dev/io", O_RDWR)) == -1) - { - xf86Msg(X_WARNING,"xf86EnableIO: " - "Failed to open /dev/io for extended I/O"); - return FALSE; - } - return TRUE; -} - -void -xf86DisableIO() -{ - if (IoFd < 0) - return; - - close(IoFd); - IoFd = -1; - return; -} - -#endif - -#ifdef __NetBSD__ -/***************************************************************************/ -/* Set TV output mode */ -/***************************************************************************/ -void -xf86SetTVOut(int mode) -{ - switch (xf86Info.consType) - { -#ifdef PCCONS_SUPPORT - case PCCONS:{ - - if (ioctl (xf86Info.consoleFd, CONSOLE_X_TV_ON, &mode) < 0) - { - xf86Msg(X_WARNING, - "xf86SetTVOut: Could not set console to TV output, %s\n", - strerror(errno)); - } - } - break; -#endif /* PCCONS_SUPPORT */ - - default: - FatalError("Xf86SetTVOut: Unsupported console"); - break; - } - return; -} - -void -xf86SetRGBOut() -{ - switch (xf86Info.consType) - { -#ifdef PCCONS_SUPPORT - case PCCONS:{ - - if (ioctl (xf86Info.consoleFd, CONSOLE_X_TV_OFF, 0) < 0) - { - xf86Msg(X_WARNING, - "xf86SetTVOut: Could not set console to RGB output, %s\n", - strerror(errno)); - } - } - break; -#endif /* PCCONS_SUPPORT */ - - default: - FatalError("Xf86SetTVOut: Unsupported console"); - break; - } - return; -} -#endif - -#ifdef HAS_MTRR_SUPPORT -/* memory range (MTRR) support for FreeBSD */ - -/* - * This code is experimental. Some parts may be overkill, and other parts - * may be incomplete. - */ - -/* - * getAllRanges returns the full list of memory ranges with attributes set. - */ - -static struct mem_range_desc * -getAllRanges(int *nmr) -{ - struct mem_range_desc *mrd; - struct mem_range_op mro; - - /* - * Find how many ranges there are. If this fails, then the kernel - * probably doesn't have MTRR support. - */ - mro.mo_arg[0] = 0; - if (ioctl(devMemFd, MEMRANGE_GET, &mro)) - return NULL; - *nmr = mro.mo_arg[0]; - mrd = xnfalloc(*nmr * sizeof(struct mem_range_desc)); - mro.mo_arg[0] = *nmr; - mro.mo_desc = mrd; - if (ioctl(devMemFd, MEMRANGE_GET, &mro)) { - xfree(mrd); - return NULL; - } - return mrd; -} - -/* - * cleanMTRR removes any memory attribute that may be left by a previous - * X server. Normally there won't be any, but this takes care of the - * case where a server crashed without being able finish cleaning up. - */ - -static Bool -cleanMTRR() -{ - struct mem_range_desc *mrd; - struct mem_range_op mro; - int nmr, i; - - /* This shouldn't happen */ - if (devMemFd < 0) - return FALSE; - - if (!(mrd = getAllRanges(&nmr))) - return FALSE; - - for (i = 0; i < nmr; i++) { - if (strcmp(mrd[i].mr_owner, X_MTRR_ID) == 0 && - (mrd[i].mr_flags & MDF_ACTIVE)) { -#ifdef DEBUG - ErrorF("Clean for (0x%lx,0x%lx)\n", - (unsigned long)mrd[i].mr_base, - (unsigned long)mrd[i].mr_len); -#endif - if (mrd[i].mr_flags & MDF_FIXACTIVE) { - mro.mo_arg[0] = MEMRANGE_SET_UPDATE; - mrd[i].mr_flags = MDF_UNCACHEABLE; - } else { - mro.mo_arg[0] = MEMRANGE_SET_REMOVE; - } - mro.mo_desc = mrd + i; - ioctl(devMemFd, MEMRANGE_SET, &mro); - } - } -#ifdef DEBUG - sleep(10); -#endif - xfree(mrd); - return TRUE; -} - -typedef struct x_RangeRec { - struct mem_range_desc mrd; - Bool wasWC; - struct x_RangeRec * next; -} RangeRec, *RangePtr; - -static void -freeRangeList(RangePtr range) -{ - RangePtr rp; - - while (range) { - rp = range; - range = rp->next; - xfree(rp); - } -} - -static RangePtr -dupRangeList(RangePtr list) -{ - RangePtr new = NULL, rp, p; - - rp = list; - while (rp) { - p = xnfalloc(sizeof(RangeRec)); - *p = *rp; - p->next = new; - new = p; - rp = rp->next; - } - return new; -} - -static RangePtr -sortRangeList(RangePtr list) -{ - RangePtr rp1, rp2, copy, sorted = NULL, minp, prev, minprev; - unsigned long minBase; - - /* Sort by base address */ - rp1 = copy = dupRangeList(list); - while (rp1) { - minBase = rp1->mrd.mr_base; - minp = rp1; - minprev = NULL; - prev = rp1; - rp2 = rp1->next; - while (rp2) { - if (rp2->mrd.mr_base < minBase) { - minBase = rp2->mrd.mr_base; - minp = rp2; - minprev = prev; - } - prev = rp2; - rp2 = rp2->next; - } - if (minprev) { - minprev->next = minp->next; - rp1 = copy; - } else { - rp1 = minp->next; - } - minp->next = sorted; - sorted = minp; - } - return sorted; -} - -/* - * findRanges returns a list of ranges that overlap the specified range. - */ - -static void -findRanges(unsigned long base, unsigned long size, RangePtr *ucp, RangePtr *wcp) -{ - struct mem_range_desc *mrd; - int nmr, i; - RangePtr rp, *p; - - if (!(mrd = getAllRanges(&nmr))) - return; - - for (i = 0; i < nmr; i++) { - if ((mrd[i].mr_flags & MDF_ACTIVE) && - mrd[i].mr_base < base + size && - mrd[i].mr_base + mrd[i].mr_len > base) { - if (mrd[i].mr_flags & MDF_WRITECOMBINE) - p = wcp; - else if (mrd[i].mr_flags & MDF_UNCACHEABLE) - p = ucp; - else - continue; - rp = xnfalloc(sizeof(RangeRec)); - rp->mrd = mrd[i]; - rp->next = *p; - *p = rp; - } - } - xfree(mrd); -} - -/* - * This checks if the existing overlapping ranges fully cover the requested - * range. Is this overkill? - */ - -static Bool -fullCoverage(unsigned long base, unsigned long size, RangePtr overlap) -{ - RangePtr rp1, sorted = NULL; - unsigned long end; - - sorted = sortRangeList(overlap); - /* Look for gaps */ - rp1 = sorted; - end = base + size; - while (rp1) { - if (rp1->mrd.mr_base > base) { - freeRangeList(sorted); - return FALSE; - } else { - base = rp1->mrd.mr_base + rp1->mrd.mr_len; - } - if (base >= end) { - freeRangeList(sorted); - return TRUE; - } - rp1 = rp1->next; - } - freeRangeList(sorted); - return FALSE; -} - -static pointer -addWC(int screenNum, unsigned long base, unsigned long size, MessageType from) -{ - RangePtr uc = NULL, wc = NULL, retlist = NULL; - struct mem_range_desc mrd; - struct mem_range_op mro; - - findRanges(base, size, &uc, &wc); - - /* See of the full range is already WC */ - if (!uc && fullCoverage(base, size, wc)) { - xf86DrvMsg(screenNum, from, - "Write-combining range (0x%lx,0x%lx) was already set\n", - base, size); - return NULL; - } - - /* Otherwise, try to add the new range */ - mrd.mr_base = base; - mrd.mr_len = size; - strcpy(mrd.mr_owner, X_MTRR_ID); - mrd.mr_flags = MDF_WRITECOMBINE; - mro.mo_desc = &mrd; - mro.mo_arg[0] = MEMRANGE_SET_UPDATE; - if (ioctl(devMemFd, MEMRANGE_SET, &mro)) { - xf86DrvMsg(screenNum, X_WARNING, - "Failed to set write-combining range " - "(0x%lx,0x%lx)\n", base, size); - return NULL; - } else { - xf86DrvMsg(screenNum, from, - "Write-combining range (0x%lx,0x%lx)\n", base, size); - retlist = xnfalloc(sizeof(RangeRec)); - retlist->mrd = mrd; - retlist->wasWC = FALSE; - retlist->next = NULL; - return retlist; - } -} - -static pointer -delWC(int screenNum, unsigned long base, unsigned long size, MessageType from) -{ - RangePtr uc = NULL, wc = NULL, retlist = NULL; - struct mem_range_desc mrd; - struct mem_range_op mro; - - findRanges(base, size, &uc, &wc); - - /* - * See of the full range is already not WC, or if there is full - * coverage from UC ranges. - */ - if (!wc || fullCoverage(base, size, uc)) { - xf86DrvMsg(screenNum, from, - "Write-combining range (0x%lx,0x%lx) was already clear\n", - base, size); - return NULL; - } - - /* Otherwise, try to add the new range */ - mrd.mr_base = base; - mrd.mr_len = size; - strcpy(mrd.mr_owner, X_MTRR_ID); - mrd.mr_flags = MDF_UNCACHEABLE; - mro.mo_desc = &mrd; - mro.mo_arg[0] = MEMRANGE_SET_UPDATE; - if (ioctl(devMemFd, MEMRANGE_SET, &mro)) { - xf86DrvMsg(screenNum, X_WARNING, - "Failed to remove write-combining range " - "(0x%lx,0x%lx)\n", base, size); - /* XXX Should then remove all of the overlapping WC ranges */ - return NULL; - } else { - xf86DrvMsg(screenNum, from, - "Removed Write-combining range (0x%lx,0x%lx)\n", - base, size); - retlist = xnfalloc(sizeof(RangeRec)); - retlist->mrd = mrd; - retlist->wasWC = TRUE; - retlist->next = NULL; - return retlist; - } -} - -static pointer -setWC(int screenNum, unsigned long base, unsigned long size, Bool enable, - MessageType from) -{ - if (enable) - return addWC(screenNum, base, size, from); - else - return delWC(screenNum, base, size, from); -} - -static void -undoWC(int screenNum, pointer list) -{ - RangePtr rp; - struct mem_range_op mro; - Bool failed; - - rp = list; - while (rp) { -#ifdef DEBUG - ErrorF("Undo for (0x%lx,0x%lx), %d\n", - (unsigned long)rp->mrd.mr_base, - (unsigned long)rp->mrd.mr_len, rp->wasWC); -#endif - failed = FALSE; - if (rp->wasWC) { - mro.mo_arg[0] = MEMRANGE_SET_UPDATE; - rp->mrd.mr_flags = MDF_WRITECOMBINE; - strcpy(rp->mrd.mr_owner, "unknown"); - } else { - mro.mo_arg[0] = MEMRANGE_SET_REMOVE; - } - mro.mo_desc = &rp->mrd; - - if (ioctl(devMemFd, MEMRANGE_SET, &mro)) { - if (!rp->wasWC) { - mro.mo_arg[0] = MEMRANGE_SET_UPDATE; - rp->mrd.mr_flags = MDF_UNCACHEABLE; - strcpy(rp->mrd.mr_owner, "unknown"); - if (ioctl(devMemFd, MEMRANGE_SET, &mro)) - failed = TRUE; - } else - failed = TRUE; - } - if (failed) { - xf86DrvMsg(screenNum, X_WARNING, - "Failed to restore MTRR range (0x%lx,0x%lx)\n", - (unsigned long)rp->mrd.mr_base, - (unsigned long)rp->mrd.mr_len); - } - rp = rp->next; - } -} - -#endif /* HAS_MTRR_SUPPORT */ - - -#if defined(HAS_MTRR_BUILTIN) && defined(__NetBSD__) -static pointer -NetBSDsetWC(int screenNum, unsigned long base, unsigned long size, Bool enable, - MessageType from) -{ - struct mtrr *mtrrp; - int n; - - xf86DrvMsg(screenNum, X_WARNING, - "%s MTRR %lx - %lx\n", enable ? "set" : "remove", - base, (base + size)); - - mtrrp = xnfalloc(sizeof (struct mtrr)); - mtrrp->base = base; - mtrrp->len = size; - mtrrp->type = MTRR_TYPE_WC; - - /* - * MTRR_PRIVATE will make this MTRR get reset automatically - * if this process exits, so we have no need for an explicit - * cleanup operation when starting a new server. - */ - - if (enable) - mtrrp->flags = MTRR_VALID | MTRR_PRIVATE; - else - mtrrp->flags = 0; - n = 1; - - if (i386_set_mtrr(mtrrp, &n) < 0) { - xfree(mtrrp); - return NULL; - } - return mtrrp; -} - -static void -NetBSDundoWC(int screenNum, pointer list) -{ - struct mtrr *mtrrp = (struct mtrr *)list; - int n; - - if (mtrrp == NULL) - return; - n = 1; - mtrrp->flags &= ~MTRR_VALID; - i386_set_mtrr(mtrrp, &n); - xfree(mtrrp); -} -#endif +/*
+ * Copyright 1992 by Rich Murphey <Rich@Rice.edu>
+ * Copyright 1993 by David Wexelblat <dwex@goblin.org>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the names of Rich Murphey and David Wexelblat
+ * not be used in advertising or publicity pertaining to distribution of
+ * the software without specific, written prior permission. Rich Murphey and
+ * David Wexelblat make no representations about the suitability of this
+ * software for any purpose. It is provided "as is" without express or
+ * implied warranty.
+ *
+ * RICH MURPHEY AND DAVID WEXELBLAT DISCLAIM ALL WARRANTIES WITH REGARD TO
+ * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL RICH MURPHEY OR DAVID WEXELBLAT BE LIABLE FOR
+ * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
+ * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
+ * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include <X11/X.h>
+#include "xf86.h"
+#include "xf86Priv.h"
+
+#include <errno.h>
+#include <sys/mman.h>
+
+#ifdef HAS_MTRR_SUPPORT
+#ifndef __NetBSD__
+#include <sys/types.h>
+#include <sys/memrange.h>
+#else
+#include "memrange.h"
+#endif
+#define X_MTRR_ID "XFree86"
+#endif
+
+#if defined(HAS_MTRR_BUILTIN) && defined(__NetBSD__)
+#include <machine/mtrr.h>
+#include <machine/sysarch.h>
+#include <sys/queue.h>
+#ifdef __x86_64__
+#define i386_set_mtrr x86_64_set_mtrr
+#define i386_get_mtrr x86_64_get_mtrr
+#define i386_iopl x86_64_iopl
+#endif
+#endif
+
+#include "xf86_OSlib.h"
+#include "xf86OSpriv.h"
+
+#if defined(__NetBSD__) && !defined(MAP_FILE)
+#define MAP_FLAGS MAP_SHARED
+#else
+#define MAP_FLAGS (MAP_FILE | MAP_SHARED)
+#endif
+
+#ifndef MAP_FAILED
+#define MAP_FAILED ((caddr_t)-1)
+#endif
+
+#ifdef __OpenBSD__
+#define SYSCTL_MSG "\tCheck that you have set 'machdep.allowaperture=1'\n"\
+ "\tin /etc/sysctl.conf and reboot your machine\n" \
+ "\trefer to xf86(4) for details"
+#define SYSCTL_MSG2 \
+ "Check that you have set 'machdep.allowaperture=2'\n" \
+ "\tin /etc/sysctl.conf and reboot your machine\n" \
+ "\trefer to xf86(4) for details"
+#endif
+
+/***************************************************************************/
+/* Video Memory Mapping section */
+/***************************************************************************/
+
+static Bool useDevMem = FALSE;
+static int devMemFd = -1;
+
+#ifdef HAS_APERTURE_DRV
+#define DEV_APERTURE "/dev/xf86"
+#endif
+#define DEV_MEM "/dev/mem"
+
+static pointer mapVidMem(int, unsigned long, unsigned long, int);
+static void unmapVidMem(int, pointer, unsigned long);
+
+#ifdef HAS_MTRR_SUPPORT
+static pointer setWC(int, unsigned long, unsigned long, Bool, MessageType);
+static void undoWC(int, pointer);
+static Bool cleanMTRR(void);
+#endif
+#if defined(HAS_MTRR_BUILTIN) && defined(__NetBSD__)
+static pointer NetBSDsetWC(int, unsigned long, unsigned long, Bool,
+ MessageType);
+static void NetBSDundoWC(int, pointer);
+#endif
+
+/*
+ * Check if /dev/mem can be mmap'd. If it can't print a warning when
+ * "warn" is TRUE.
+ */
+static void
+checkDevMem(Bool warn)
+{
+ static Bool devMemChecked = FALSE;
+ int fd;
+ pointer base;
+
+ if (devMemChecked)
+ return;
+ devMemChecked = TRUE;
+
+ if ((fd = open(DEV_MEM, O_RDWR)) >= 0)
+ {
+ /* Try to map a page at the VGA address */
+ base = mmap((caddr_t)0, 4096, PROT_READ | PROT_WRITE,
+ MAP_FLAGS, fd, (off_t)0xA0000);
+
+ if (base != MAP_FAILED)
+ {
+ munmap((caddr_t)base, 4096);
+ devMemFd = fd;
+ useDevMem = TRUE;
+ return;
+ } else {
+ /* This should not happen */
+ if (warn)
+ {
+ xf86Msg(X_WARNING, "checkDevMem: failed to mmap %s (%s)\n",
+ DEV_MEM, strerror(errno));
+ }
+ useDevMem = FALSE;
+ return;
+ }
+ }
+#ifndef HAS_APERTURE_DRV
+ if (warn)
+ {
+ xf86Msg(X_WARNING, "checkDevMem: failed to open %s (%s)\n",
+ DEV_MEM, strerror(errno));
+ }
+ useDevMem = FALSE;
+ return;
+#else
+ /* Failed to open /dev/mem, try the aperture driver */
+ if ((fd = open(DEV_APERTURE, O_RDWR)) >= 0)
+ {
+ /* Try to map a page at the VGA address */
+ base = mmap((caddr_t)0, 4096, PROT_READ | PROT_WRITE,
+ MAP_FLAGS, fd, (off_t)0xA0000);
+
+ if (base != MAP_FAILED)
+ {
+ munmap((caddr_t)base, 4096);
+ devMemFd = fd;
+ useDevMem = TRUE;
+ xf86Msg(X_INFO, "checkDevMem: using aperture driver %s\n",
+ DEV_APERTURE);
+ return;
+ } else {
+
+ if (warn)
+ {
+ xf86Msg(X_WARNING, "checkDevMem: failed to mmap %s (%s)\n",
+ DEV_APERTURE, strerror(errno));
+ }
+ }
+ } else {
+ if (warn)
+ {
+#ifndef __OpenBSD__
+ xf86Msg(X_WARNING, "checkDevMem: failed to open %s and %s\n"
+ "\t(%s)\n", DEV_MEM, DEV_APERTURE, strerror(errno));
+#else /* __OpenBSD__ */
+ xf86Msg(X_WARNING, "checkDevMem: failed to open %s and %s\n"
+ "\t(%s)\n%s", DEV_MEM, DEV_APERTURE, strerror(errno),
+ SYSCTL_MSG);
+#endif /* __OpenBSD__ */
+ }
+ }
+
+ useDevMem = FALSE;
+ return;
+
+#endif
+}
+
+void
+xf86OSInitVidMem(VidMemInfoPtr pVidMem)
+{
+ checkDevMem(TRUE);
+ pVidMem->linearSupported = useDevMem;
+ pVidMem->mapMem = mapVidMem;
+ pVidMem->unmapMem = unmapVidMem;
+
+#if HAVE_PCI_SYSTEM_INIT_DEV_MEM
+ if (useDevMem)
+ pci_system_init_dev_mem(devMemFd);
+#endif
+
+#ifdef HAS_MTRR_SUPPORT
+ if (useDevMem) {
+ if (cleanMTRR()) {
+ pVidMem->setWC = setWC;
+ pVidMem->undoWC = undoWC;
+ }
+ }
+#endif
+#if defined(HAS_MTRR_BUILTIN) && defined(__NetBSD__)
+ pVidMem->setWC = NetBSDsetWC;
+ pVidMem->undoWC = NetBSDundoWC;
+#endif
+ pVidMem->initialised = TRUE;
+}
+
+static pointer
+mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags)
+{
+ pointer base;
+
+ checkDevMem(FALSE);
+
+ if (useDevMem)
+ {
+ if (devMemFd < 0)
+ {
+ FatalError("xf86MapVidMem: failed to open %s (%s)",
+ DEV_MEM, strerror(errno));
+ }
+ base = mmap((caddr_t)0, Size,
+ (flags & VIDMEM_READONLY) ?
+ PROT_READ : (PROT_READ | PROT_WRITE),
+ MAP_FLAGS, devMemFd, (off_t)Base);
+ if (base == MAP_FAILED)
+ {
+ FatalError("%s: could not mmap %s [s=%lx,a=%lx] (%s)",
+ "xf86MapVidMem", DEV_MEM, Size, Base,
+ strerror(errno));
+ }
+ return(base);
+ }
+
+ /* else, mmap /dev/vga */
+ if ((unsigned long)Base < 0xA0000 || (unsigned long)Base >= 0xC0000)
+ {
+ FatalError("%s: Address 0x%lx outside allowable range",
+ "xf86MapVidMem", Base);
+ }
+ base = mmap(0, Size,
+ (flags & VIDMEM_READONLY) ?
+ PROT_READ : (PROT_READ | PROT_WRITE),
+ MAP_FLAGS, xf86Info.screenFd,
+ (unsigned long)Base - 0xA0000
+ );
+ if (base == MAP_FAILED)
+ {
+ FatalError("xf86MapVidMem: Could not mmap /dev/vga (%s)",
+ strerror(errno));
+ }
+ return(base);
+}
+
+static void
+unmapVidMem(int ScreenNum, pointer Base, unsigned long Size)
+{
+ munmap((caddr_t)Base, Size);
+}
+
+/*
+ * Read BIOS via mmap()ing DEV_MEM
+ */
+
+int
+xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
+ int Len)
+{
+ unsigned char *ptr;
+ int psize;
+ int mlen;
+
+ checkDevMem(TRUE);
+ if (devMemFd == -1) {
+ return(-1);
+ }
+
+ psize = getpagesize();
+ Offset += Base & (psize - 1);
+ Base &= ~(psize - 1);
+ mlen = (Offset + Len + psize - 1) & ~(psize - 1);
+ ptr = (unsigned char *)mmap((caddr_t)0, mlen, PROT_READ,
+ MAP_SHARED, devMemFd, (off_t)Base);
+ if ((long)ptr == -1)
+ {
+ xf86Msg(X_WARNING,
+ "xf86ReadBIOS: %s mmap[s=%x,a=%lx,o=%lx] failed (%s)\n",
+ DEV_MEM, Len, Base, Offset, strerror(errno));
+#ifdef __OpenBSD__
+ if (Base < 0xa0000) {
+ xf86Msg(X_WARNING, SYSCTL_MSG2);
+ }
+#endif
+ return(-1);
+ }
+#ifdef DEBUG
+ ErrorF("xf86ReadBIOS: BIOS at 0x%08x has signature 0x%04x\n",
+ Base, ptr[0] | (ptr[1] << 8));
+#endif
+ (void)memcpy(Buf, (void *)(ptr + Offset), Len);
+ (void)munmap((caddr_t)ptr, mlen);
+#ifdef DEBUG
+ xf86MsgVerb(X_INFO, 3, "xf86ReadBIOS(%x, %x, Buf, %x)"
+ "-> %02x %02x %02x %02x...\n",
+ Base, Offset, Len, Buf[0], Buf[1], Buf[2], Buf[3]);
+#endif
+ return(Len);
+}
+
+#ifdef USE_I386_IOPL
+/***************************************************************************/
+/* I/O Permissions section */
+/***************************************************************************/
+
+static Bool ExtendedEnabled = FALSE;
+
+Bool
+xf86EnableIO()
+{
+ if (ExtendedEnabled)
+ return TRUE;
+
+ if (i386_iopl(TRUE) < 0)
+ {
+#ifndef __OpenBSD__
+ xf86Msg(X_WARNING,"%s: Failed to set IOPL for extended I/O",
+ "xf86EnableIO");
+#else
+ xf86Msg(X_WARNING,"%s: Failed to set IOPL for extended I/O\n%s",
+ "xf86EnableIO", SYSCTL_MSG);
+#endif
+ return FALSE;
+ }
+ ExtendedEnabled = TRUE;
+
+ return TRUE;
+}
+
+void
+xf86DisableIO()
+{
+ if (!ExtendedEnabled)
+ return;
+
+ i386_iopl(FALSE);
+ ExtendedEnabled = FALSE;
+
+ return;
+}
+
+#endif /* USE_I386_IOPL */
+
+#ifdef USE_AMD64_IOPL
+/***************************************************************************/
+/* I/O Permissions section */
+/***************************************************************************/
+
+static Bool ExtendedEnabled = FALSE;
+
+Bool
+xf86EnableIO()
+{
+ if (ExtendedEnabled)
+ return TRUE;
+
+ if (amd64_iopl(TRUE) < 0)
+ {
+#ifndef __OpenBSD__
+ xf86Msg(X_WARNING,"%s: Failed to set IOPL for extended I/O",
+ "xf86EnableIO");
+#else
+ xf86Msg(X_WARNING,"%s: Failed to set IOPL for extended I/O\n%s",
+ "xf86EnableIO", SYSCTL_MSG);
+#endif
+ return FALSE;
+ }
+ ExtendedEnabled = TRUE;
+
+ return TRUE;
+}
+
+void
+xf86DisableIO()
+{
+ if (!ExtendedEnabled)
+ return;
+
+ if (amd64_iopl(FALSE) == 0) {
+ ExtendedEnabled = FALSE;
+ }
+ /* Otherwise, the X server has revoqued its root uid,
+ and thus cannot give up IO privileges any more */
+
+ return;
+}
+
+#endif /* USE_AMD64_IOPL */
+
+#ifdef USE_DEV_IO
+static int IoFd = -1;
+
+Bool
+xf86EnableIO()
+{
+ if (IoFd >= 0)
+ return TRUE;
+
+ if ((IoFd = open("/dev/io", O_RDWR)) == -1)
+ {
+ xf86Msg(X_WARNING,"xf86EnableIO: "
+ "Failed to open /dev/io for extended I/O");
+ return FALSE;
+ }
+ return TRUE;
+}
+
+void
+xf86DisableIO()
+{
+ if (IoFd < 0)
+ return;
+
+ close(IoFd);
+ IoFd = -1;
+ return;
+}
+
+#endif
+
+#ifdef __NetBSD__
+/***************************************************************************/
+/* Set TV output mode */
+/***************************************************************************/
+void
+xf86SetTVOut(int mode)
+{
+ switch (xf86Info.consType)
+ {
+#ifdef PCCONS_SUPPORT
+ case PCCONS:{
+
+ if (ioctl (xf86Info.consoleFd, CONSOLE_X_TV_ON, &mode) < 0)
+ {
+ xf86Msg(X_WARNING,
+ "xf86SetTVOut: Could not set console to TV output, %s\n",
+ strerror(errno));
+ }
+ }
+ break;
+#endif /* PCCONS_SUPPORT */
+
+ default:
+ FatalError("Xf86SetTVOut: Unsupported console");
+ break;
+ }
+ return;
+}
+
+void
+xf86SetRGBOut()
+{
+ switch (xf86Info.consType)
+ {
+#ifdef PCCONS_SUPPORT
+ case PCCONS:{
+
+ if (ioctl (xf86Info.consoleFd, CONSOLE_X_TV_OFF, 0) < 0)
+ {
+ xf86Msg(X_WARNING,
+ "xf86SetTVOut: Could not set console to RGB output, %s\n",
+ strerror(errno));
+ }
+ }
+ break;
+#endif /* PCCONS_SUPPORT */
+
+ default:
+ FatalError("Xf86SetTVOut: Unsupported console");
+ break;
+ }
+ return;
+}
+#endif
+
+#ifdef HAS_MTRR_SUPPORT
+/* memory range (MTRR) support for FreeBSD */
+
+/*
+ * This code is experimental. Some parts may be overkill, and other parts
+ * may be incomplete.
+ */
+
+/*
+ * getAllRanges returns the full list of memory ranges with attributes set.
+ */
+
+static struct mem_range_desc *
+getAllRanges(int *nmr)
+{
+ struct mem_range_desc *mrd;
+ struct mem_range_op mro;
+
+ /*
+ * Find how many ranges there are. If this fails, then the kernel
+ * probably doesn't have MTRR support.
+ */
+ mro.mo_arg[0] = 0;
+ if (ioctl(devMemFd, MEMRANGE_GET, &mro))
+ return NULL;
+ *nmr = mro.mo_arg[0];
+ mrd = xnfalloc(*nmr * sizeof(struct mem_range_desc));
+ mro.mo_arg[0] = *nmr;
+ mro.mo_desc = mrd;
+ if (ioctl(devMemFd, MEMRANGE_GET, &mro)) {
+ free(mrd);
+ return NULL;
+ }
+ return mrd;
+}
+
+/*
+ * cleanMTRR removes any memory attribute that may be left by a previous
+ * X server. Normally there won't be any, but this takes care of the
+ * case where a server crashed without being able finish cleaning up.
+ */
+
+static Bool
+cleanMTRR()
+{
+ struct mem_range_desc *mrd;
+ struct mem_range_op mro;
+ int nmr, i;
+
+ /* This shouldn't happen */
+ if (devMemFd < 0)
+ return FALSE;
+
+ if (!(mrd = getAllRanges(&nmr)))
+ return FALSE;
+
+ for (i = 0; i < nmr; i++) {
+ if (strcmp(mrd[i].mr_owner, X_MTRR_ID) == 0 &&
+ (mrd[i].mr_flags & MDF_ACTIVE)) {
+#ifdef DEBUG
+ ErrorF("Clean for (0x%lx,0x%lx)\n",
+ (unsigned long)mrd[i].mr_base,
+ (unsigned long)mrd[i].mr_len);
+#endif
+ if (mrd[i].mr_flags & MDF_FIXACTIVE) {
+ mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+ mrd[i].mr_flags = MDF_UNCACHEABLE;
+ } else {
+ mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
+ }
+ mro.mo_desc = mrd + i;
+ ioctl(devMemFd, MEMRANGE_SET, &mro);
+ }
+ }
+#ifdef DEBUG
+ sleep(10);
+#endif
+ free(mrd);
+ return TRUE;
+}
+
+typedef struct x_RangeRec {
+ struct mem_range_desc mrd;
+ Bool wasWC;
+ struct x_RangeRec * next;
+} RangeRec, *RangePtr;
+
+static void
+freeRangeList(RangePtr range)
+{
+ RangePtr rp;
+
+ while (range) {
+ rp = range;
+ range = rp->next;
+ free(rp);
+ }
+}
+
+static RangePtr
+dupRangeList(RangePtr list)
+{
+ RangePtr new = NULL, rp, p;
+
+ rp = list;
+ while (rp) {
+ p = xnfalloc(sizeof(RangeRec));
+ *p = *rp;
+ p->next = new;
+ new = p;
+ rp = rp->next;
+ }
+ return new;
+}
+
+static RangePtr
+sortRangeList(RangePtr list)
+{
+ RangePtr rp1, rp2, copy, sorted = NULL, minp, prev, minprev;
+ unsigned long minBase;
+
+ /* Sort by base address */
+ rp1 = copy = dupRangeList(list);
+ while (rp1) {
+ minBase = rp1->mrd.mr_base;
+ minp = rp1;
+ minprev = NULL;
+ prev = rp1;
+ rp2 = rp1->next;
+ while (rp2) {
+ if (rp2->mrd.mr_base < minBase) {
+ minBase = rp2->mrd.mr_base;
+ minp = rp2;
+ minprev = prev;
+ }
+ prev = rp2;
+ rp2 = rp2->next;
+ }
+ if (minprev) {
+ minprev->next = minp->next;
+ rp1 = copy;
+ } else {
+ rp1 = minp->next;
+ }
+ minp->next = sorted;
+ sorted = minp;
+ }
+ return sorted;
+}
+
+/*
+ * findRanges returns a list of ranges that overlap the specified range.
+ */
+
+static void
+findRanges(unsigned long base, unsigned long size, RangePtr *ucp, RangePtr *wcp)
+{
+ struct mem_range_desc *mrd;
+ int nmr, i;
+ RangePtr rp, *p;
+
+ if (!(mrd = getAllRanges(&nmr)))
+ return;
+
+ for (i = 0; i < nmr; i++) {
+ if ((mrd[i].mr_flags & MDF_ACTIVE) &&
+ mrd[i].mr_base < base + size &&
+ mrd[i].mr_base + mrd[i].mr_len > base) {
+ if (mrd[i].mr_flags & MDF_WRITECOMBINE)
+ p = wcp;
+ else if (mrd[i].mr_flags & MDF_UNCACHEABLE)
+ p = ucp;
+ else
+ continue;
+ rp = xnfalloc(sizeof(RangeRec));
+ rp->mrd = mrd[i];
+ rp->next = *p;
+ *p = rp;
+ }
+ }
+ free(mrd);
+}
+
+/*
+ * This checks if the existing overlapping ranges fully cover the requested
+ * range. Is this overkill?
+ */
+
+static Bool
+fullCoverage(unsigned long base, unsigned long size, RangePtr overlap)
+{
+ RangePtr rp1, sorted = NULL;
+ unsigned long end;
+
+ sorted = sortRangeList(overlap);
+ /* Look for gaps */
+ rp1 = sorted;
+ end = base + size;
+ while (rp1) {
+ if (rp1->mrd.mr_base > base) {
+ freeRangeList(sorted);
+ return FALSE;
+ } else {
+ base = rp1->mrd.mr_base + rp1->mrd.mr_len;
+ }
+ if (base >= end) {
+ freeRangeList(sorted);
+ return TRUE;
+ }
+ rp1 = rp1->next;
+ }
+ freeRangeList(sorted);
+ return FALSE;
+}
+
+static pointer
+addWC(int screenNum, unsigned long base, unsigned long size, MessageType from)
+{
+ RangePtr uc = NULL, wc = NULL, retlist = NULL;
+ struct mem_range_desc mrd;
+ struct mem_range_op mro;
+
+ findRanges(base, size, &uc, &wc);
+
+ /* See of the full range is already WC */
+ if (!uc && fullCoverage(base, size, wc)) {
+ xf86DrvMsg(screenNum, from,
+ "Write-combining range (0x%lx,0x%lx) was already set\n",
+ base, size);
+ return NULL;
+ }
+
+ /* Otherwise, try to add the new range */
+ mrd.mr_base = base;
+ mrd.mr_len = size;
+ strcpy(mrd.mr_owner, X_MTRR_ID);
+ mrd.mr_flags = MDF_WRITECOMBINE;
+ mro.mo_desc = &mrd;
+ mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+ if (ioctl(devMemFd, MEMRANGE_SET, &mro)) {
+ xf86DrvMsg(screenNum, X_WARNING,
+ "Failed to set write-combining range "
+ "(0x%lx,0x%lx)\n", base, size);
+ return NULL;
+ } else {
+ xf86DrvMsg(screenNum, from,
+ "Write-combining range (0x%lx,0x%lx)\n", base, size);
+ retlist = xnfalloc(sizeof(RangeRec));
+ retlist->mrd = mrd;
+ retlist->wasWC = FALSE;
+ retlist->next = NULL;
+ return retlist;
+ }
+}
+
+static pointer
+delWC(int screenNum, unsigned long base, unsigned long size, MessageType from)
+{
+ RangePtr uc = NULL, wc = NULL, retlist = NULL;
+ struct mem_range_desc mrd;
+ struct mem_range_op mro;
+
+ findRanges(base, size, &uc, &wc);
+
+ /*
+ * See of the full range is already not WC, or if there is full
+ * coverage from UC ranges.
+ */
+ if (!wc || fullCoverage(base, size, uc)) {
+ xf86DrvMsg(screenNum, from,
+ "Write-combining range (0x%lx,0x%lx) was already clear\n",
+ base, size);
+ return NULL;
+ }
+
+ /* Otherwise, try to add the new range */
+ mrd.mr_base = base;
+ mrd.mr_len = size;
+ strcpy(mrd.mr_owner, X_MTRR_ID);
+ mrd.mr_flags = MDF_UNCACHEABLE;
+ mro.mo_desc = &mrd;
+ mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+ if (ioctl(devMemFd, MEMRANGE_SET, &mro)) {
+ xf86DrvMsg(screenNum, X_WARNING,
+ "Failed to remove write-combining range "
+ "(0x%lx,0x%lx)\n", base, size);
+ /* XXX Should then remove all of the overlapping WC ranges */
+ return NULL;
+ } else {
+ xf86DrvMsg(screenNum, from,
+ "Removed Write-combining range (0x%lx,0x%lx)\n",
+ base, size);
+ retlist = xnfalloc(sizeof(RangeRec));
+ retlist->mrd = mrd;
+ retlist->wasWC = TRUE;
+ retlist->next = NULL;
+ return retlist;
+ }
+}
+
+static pointer
+setWC(int screenNum, unsigned long base, unsigned long size, Bool enable,
+ MessageType from)
+{
+ if (enable)
+ return addWC(screenNum, base, size, from);
+ else
+ return delWC(screenNum, base, size, from);
+}
+
+static void
+undoWC(int screenNum, pointer list)
+{
+ RangePtr rp;
+ struct mem_range_op mro;
+ Bool failed;
+
+ rp = list;
+ while (rp) {
+#ifdef DEBUG
+ ErrorF("Undo for (0x%lx,0x%lx), %d\n",
+ (unsigned long)rp->mrd.mr_base,
+ (unsigned long)rp->mrd.mr_len, rp->wasWC);
+#endif
+ failed = FALSE;
+ if (rp->wasWC) {
+ mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+ rp->mrd.mr_flags = MDF_WRITECOMBINE;
+ strcpy(rp->mrd.mr_owner, "unknown");
+ } else {
+ mro.mo_arg[0] = MEMRANGE_SET_REMOVE;
+ }
+ mro.mo_desc = &rp->mrd;
+
+ if (ioctl(devMemFd, MEMRANGE_SET, &mro)) {
+ if (!rp->wasWC) {
+ mro.mo_arg[0] = MEMRANGE_SET_UPDATE;
+ rp->mrd.mr_flags = MDF_UNCACHEABLE;
+ strcpy(rp->mrd.mr_owner, "unknown");
+ if (ioctl(devMemFd, MEMRANGE_SET, &mro))
+ failed = TRUE;
+ } else
+ failed = TRUE;
+ }
+ if (failed) {
+ xf86DrvMsg(screenNum, X_WARNING,
+ "Failed to restore MTRR range (0x%lx,0x%lx)\n",
+ (unsigned long)rp->mrd.mr_base,
+ (unsigned long)rp->mrd.mr_len);
+ }
+ rp = rp->next;
+ }
+}
+
+#endif /* HAS_MTRR_SUPPORT */
+
+
+#if defined(HAS_MTRR_BUILTIN) && defined(__NetBSD__)
+static pointer
+NetBSDsetWC(int screenNum, unsigned long base, unsigned long size, Bool enable,
+ MessageType from)
+{
+ struct mtrr *mtrrp;
+ int n;
+
+ xf86DrvMsg(screenNum, X_WARNING,
+ "%s MTRR %lx - %lx\n", enable ? "set" : "remove",
+ base, (base + size));
+
+ mtrrp = xnfalloc(sizeof (struct mtrr));
+ mtrrp->base = base;
+ mtrrp->len = size;
+ mtrrp->type = MTRR_TYPE_WC;
+
+ /*
+ * MTRR_PRIVATE will make this MTRR get reset automatically
+ * if this process exits, so we have no need for an explicit
+ * cleanup operation when starting a new server.
+ */
+
+ if (enable)
+ mtrrp->flags = MTRR_VALID | MTRR_PRIVATE;
+ else
+ mtrrp->flags = 0;
+ n = 1;
+
+ if (i386_set_mtrr(mtrrp, &n) < 0) {
+ free(mtrrp);
+ return NULL;
+ }
+ return mtrrp;
+}
+
+static void
+NetBSDundoWC(int screenNum, pointer list)
+{
+ struct mtrr *mtrrp = (struct mtrr *)list;
+ int n;
+
+ if (mtrrp == NULL)
+ return;
+ n = 1;
+ mtrrp->flags &= ~MTRR_VALID;
+ i386_set_mtrr(mtrrp, &n);
+ free(mtrrp);
+}
+#endif
diff --git a/xorg-server/hw/xfree86/os-support/bus/Sbus.c b/xorg-server/hw/xfree86/os-support/bus/Sbus.c index 0b6205f0b..4560ef016 100644 --- a/xorg-server/hw/xfree86/os-support/bus/Sbus.c +++ b/xorg-server/hw/xfree86/os-support/bus/Sbus.c @@ -1,690 +1,690 @@ -/* - * SBUS and OpenPROM access functions. - * - * Copyright (C) 2000 Jakub Jelinek (jakub@redhat.com) - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * JAKUB JELINEK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER - * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN - * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ - -#ifdef HAVE_XORG_CONFIG_H -#include <xorg-config.h> -#endif - -#include <fcntl.h> -#include <stdio.h> -#include <unistd.h> -#include <stdlib.h> -#include <sys/ioctl.h> -#include <sys/mman.h> -#ifdef sun -#include <sys/utsname.h> -#endif -#include "xf86.h" -#include "xf86Priv.h" -#include "xf86_OSlib.h" - -#include "xf86sbusBus.h" -#include "xf86Sbus.h" - -int promRootNode; - -static int promFd = -1; -static int promCurrentNode; -static int promOpenCount = 0; -static int promP1275 = -1; -#define MAX_PROP 128 -#define MAX_VAL (4096-128-4) -static struct openpromio *promOpio; - -sbusDevicePtr *xf86SbusInfo = NULL; - -struct sbus_devtable sbusDeviceTable[] = { - { SBUS_DEVICE_BW2, FBTYPE_SUN2BW, "bwtwo", "sunbw2", "Sun Monochrome (bwtwo)" }, - { SBUS_DEVICE_CG2, FBTYPE_SUN2COLOR, "cgtwo", NULL, "Sun Color2 (cgtwo)" }, - { SBUS_DEVICE_CG3, FBTYPE_SUN3COLOR, "cgthree", "suncg3", "Sun Color3 (cgthree)" }, - { SBUS_DEVICE_CG4, FBTYPE_SUN4COLOR, "cgfour", NULL, "Sun Color4 (cgfour)" }, - { SBUS_DEVICE_CG6, FBTYPE_SUNFAST_COLOR, "cgsix", "suncg6", "Sun GX" }, - { SBUS_DEVICE_CG8, FBTYPE_MEMCOLOR, "cgeight", NULL, "Sun CG8/RasterOps" }, - { SBUS_DEVICE_CG12, FBTYPE_SUNGP3, "cgtwelve", NULL, "Sun GS (cgtwelve)" }, - { SBUS_DEVICE_CG14, FBTYPE_MDICOLOR, "cgfourteen", "suncg14", "Sun SX" }, - { SBUS_DEVICE_GT, FBTYPE_SUNGT, "gt", NULL, "Sun Graphics Tower" }, - { SBUS_DEVICE_MGX, -1, "mgx", NULL, "Quantum 3D MGXplus" }, - { SBUS_DEVICE_LEO, FBTYPE_SUNLEO, "leo", "sunleo", "Sun ZX or Turbo ZX" }, - { SBUS_DEVICE_TCX, FBTYPE_TCXCOLOR, "tcx", "suntcx", "Sun TCX" }, - { SBUS_DEVICE_FFB, FBTYPE_CREATOR, "ffb", "sunffb", "Sun FFB" }, - { SBUS_DEVICE_FFB, FBTYPE_CREATOR, "afb", "sunffb", "Sun Elite3D" }, - { 0, 0, NULL } -}; - -int -promGetSibling(int node) -{ - promOpio->oprom_size = sizeof(int); - - if (node == -1) return 0; - *(int *)promOpio->oprom_array = node; - if (ioctl(promFd, OPROMNEXT, promOpio) < 0) - return 0; - promCurrentNode = *(int *)promOpio->oprom_array; - return *(int *)promOpio->oprom_array; -} - -int -promGetChild(int node) -{ - promOpio->oprom_size = sizeof(int); - - if (!node || node == -1) return 0; - *(int *)promOpio->oprom_array = node; - if (ioctl(promFd, OPROMCHILD, promOpio) < 0) - return 0; - promCurrentNode = *(int *)promOpio->oprom_array; - return *(int *)promOpio->oprom_array; -} - -char * -promGetProperty(const char *prop, int *lenp) -{ - promOpio->oprom_size = MAX_VAL; - - strcpy(promOpio->oprom_array, prop); - if (ioctl(promFd, OPROMGETPROP, promOpio) < 0) - return 0; - if (lenp) *lenp = promOpio->oprom_size; - return promOpio->oprom_array; -} - -int -promGetBool(const char *prop) -{ - promOpio->oprom_size = 0; - - *(int *)promOpio->oprom_array = 0; - for (;;) { - promOpio->oprom_size = MAX_PROP; - if (ioctl(promFd, OPROMNXTPROP, promOpio) < 0) - return 0; - if (!promOpio->oprom_size) - return 0; - if (!strcmp(promOpio->oprom_array, prop)) - return 1; - } -} - -#define PROM_NODE_SIBLING 0x01 -#define PROM_NODE_PREF 0x02 -#define PROM_NODE_SBUS 0x04 -#define PROM_NODE_EBUS 0x08 -#define PROM_NODE_PCI 0x10 - -static int -promSetNode(sbusPromNodePtr pnode) -{ - int node; - - if (!pnode->node || pnode->node == -1) - return -1; - if (pnode->cookie[0] & PROM_NODE_SIBLING) - node = promGetSibling(pnode->cookie[1]); - else - node = promGetChild(pnode->cookie[1]); - if (pnode->node != node) - return -1; - return 0; -} - -static void -promIsP1275(void) -{ -#ifdef linux - FILE *f; - char buffer[1024]; - - if (promP1275 != -1) - return; - promP1275 = 0; - f = fopen("/proc/cpuinfo","r"); - if (!f) return; - while (fgets(buffer, 1024, f) != NULL) - if (!strncmp (buffer, "type", 4) && strstr (buffer, "sun4u")) { - promP1275 = 1; - break; - } - fclose(f); -#elif defined(sun) - struct utsname buffer; - - if ((uname(&buffer) >= 0) && !strcmp(buffer.machine, "sun4u")) - promP1275 = TRUE; - else - promP1275 = FALSE; -#elif defined(__FreeBSD__) - promP1275 = TRUE; -#else -#error Missing promIsP1275() function for this OS -#endif -} - -void -sparcPromClose(void) -{ - if (promOpenCount > 1) { - promOpenCount--; - return; - } - if (promFd != -1) { - close(promFd); - promFd = -1; - } - if (promOpio) { - xfree(promOpio); - promOpio = NULL; - } - promOpenCount = 0; -} - -int -sparcPromInit(void) -{ - if (promOpenCount) { - promOpenCount++; - return 0; - } - promFd = open("/dev/openprom", O_RDONLY, 0); - if (promFd == -1) - return -1; - promOpio = (struct openpromio *)xalloc(4096); - if (!promOpio) { - sparcPromClose(); - return -1; - } - promRootNode = promGetSibling(0); - if (!promRootNode) { - sparcPromClose(); - return -1; - } - promIsP1275(); - promOpenCount++; - - return 0; -} - -char * -sparcPromGetProperty(sbusPromNodePtr pnode, const char *prop, int *lenp) -{ - if (promSetNode(pnode)) - return NULL; - return promGetProperty(prop, lenp); -} - -int -sparcPromGetBool(sbusPromNodePtr pnode, const char *prop) -{ - if (promSetNode(pnode)) - return 0; - return promGetBool(prop); -} - -static char * -promWalkGetDriverName(int node, int oldnode) -{ - int nextnode; - int len; - char *prop; - int devId, i; - - prop = promGetProperty("device_type", &len); - if (prop && (len > 0)) do { - if (!strcmp(prop, "display")) { - prop = promGetProperty("name", &len); - if (!prop || len <= 0) - break; - while ((*prop >= 'A' && *prop <= 'Z') || *prop == ',') - prop++; - for (i = 0; sbusDeviceTable[i].devId; i++) - if (!strcmp(prop, sbusDeviceTable[i].promName)) - break; - devId = sbusDeviceTable[i].devId; - if (!devId) - break; - if (sbusDeviceTable[i].driverName) - return sbusDeviceTable[i].driverName; - } - } while (0); - - nextnode = promGetChild(node); - if (nextnode) { - char *name; - name = promWalkGetDriverName(nextnode, node); - if (name) - return name; - } - - nextnode = promGetSibling(node); - if (nextnode) - return promWalkGetDriverName(nextnode, node); - return NULL; -} - -char * -sparcDriverName(void) -{ - char *name; - - if (sparcPromInit() < 0) - return NULL; - promGetSibling(0); - name = promWalkGetDriverName(promRootNode, 0); - sparcPromClose(); - return name; -} - -static void -promWalkAssignNodes(int node, int oldnode, int flags, sbusDevicePtr *devicePtrs) -{ - int nextnode; - int len, sbus = flags & PROM_NODE_SBUS; - char *prop; - int devId, i, j; - sbusPromNode pNode, pNode2; - - prop = promGetProperty("device_type", &len); - if (prop && (len > 0)) do { - if (!strcmp(prop, "display")) { - prop = promGetProperty("name", &len); - if (!prop || len <= 0) - break; - while ((*prop >= 'A' && *prop <= 'Z') || *prop == ',') - prop++; - for (i = 0; sbusDeviceTable[i].devId; i++) - if (!strcmp(prop, sbusDeviceTable[i].promName)) - break; - devId = sbusDeviceTable[i].devId; - if (!devId) - break; - if (!sbus) { - if (devId == SBUS_DEVICE_FFB) { - /* - * All /SUNW,ffb outside of SBUS tree come before all - * /SUNW,afb outside of SBUS tree in Linux. - */ - if (!strcmp(prop, "afb")) - flags |= PROM_NODE_PREF; - } else if (devId != SBUS_DEVICE_CG14) - break; - } - for (i = 0; i < 32; i++) { - if (!devicePtrs[i] || devicePtrs[i]->devId != devId) - continue; - if (devicePtrs[i]->node.node) { - if ((devicePtrs[i]->node.cookie[0] & ~PROM_NODE_SIBLING) <= - (flags & ~PROM_NODE_SIBLING)) - continue; - for (j = i + 1, pNode = devicePtrs[i]->node; j < 32; j++) { - if (!devicePtrs[j] || devicePtrs[j]->devId != devId) - continue; - pNode2 = devicePtrs[j]->node; - devicePtrs[j]->node = pNode; - pNode = pNode2; - } - } - devicePtrs[i]->node.node = node; - devicePtrs[i]->node.cookie[0] = flags; - devicePtrs[i]->node.cookie[1] = oldnode; - break; - } - break; - } - } while (0); - - prop = promGetProperty("name", &len); - if (prop && len > 0) { - if (!strcmp(prop, "sbus") || !strcmp(prop, "sbi")) - sbus = PROM_NODE_SBUS; - } - - nextnode = promGetChild(node); - if (nextnode) - promWalkAssignNodes(nextnode, node, sbus, devicePtrs); - - nextnode = promGetSibling(node); - if (nextnode) - promWalkAssignNodes(nextnode, node, PROM_NODE_SIBLING | sbus, devicePtrs); -} - -void -sparcPromAssignNodes(void) -{ - sbusDevicePtr psdp, *psdpp; - int n, holes = 0, i, j; - FILE *f; - sbusDevicePtr devicePtrs[32]; - - memset(devicePtrs, 0, sizeof(devicePtrs)); - for (psdpp = xf86SbusInfo, n = 0; (psdp = *psdpp); psdpp++, n++) { - if (psdp->fbNum != n) - holes = 1; - devicePtrs[psdp->fbNum] = psdp; - } - if (holes && (f = fopen("/proc/fb", "r")) != NULL) { - /* We could not open one of fb devices, check /proc/fb to see what - * were the types of the cards missed. */ - char buffer[64]; - int fbNum, devId; - static struct { - int devId; - char *prefix; - } procFbPrefixes[] = { - { SBUS_DEVICE_BW2, "BWtwo" }, - { SBUS_DEVICE_CG14, "CGfourteen" }, - { SBUS_DEVICE_CG6, "CGsix" }, - { SBUS_DEVICE_CG3, "CGthree" }, - { SBUS_DEVICE_FFB, "Creator" }, - { SBUS_DEVICE_FFB, "Elite 3D" }, - { SBUS_DEVICE_LEO, "Leo" }, - { SBUS_DEVICE_TCX, "TCX" }, - { 0, NULL }, - }; - - while (fscanf(f, "%d %63s\n", &fbNum, buffer) == 2) { - for (i = 0; procFbPrefixes[i].devId; i++) - if (! strncmp(procFbPrefixes[i].prefix, buffer, - strlen(procFbPrefixes[i].prefix))) - break; - devId = procFbPrefixes[i].devId; - if (! devId) continue; - if (devicePtrs[fbNum]) { - if (devicePtrs[fbNum]->devId != devId) - xf86ErrorF("Inconsistent /proc/fb with FBIOGATTR\n"); - } else if (!devicePtrs[fbNum]) { - devicePtrs[fbNum] = psdp = xnfcalloc(sizeof (sbusDevice), 1); - psdp->devId = devId; - psdp->fbNum = fbNum; - psdp->fd = -2; - } - } - fclose(f); - } - promGetSibling(0); - promWalkAssignNodes(promRootNode, 0, PROM_NODE_PREF, devicePtrs); - for (i = 0, j = 0; i < 32; i++) - if (devicePtrs[i] && devicePtrs[i]->fbNum == -1) - j++; - xf86SbusInfo = xnfrealloc(xf86SbusInfo, sizeof(psdp) * (n + j + 1)); - for (i = 0, psdpp = xf86SbusInfo; i < 32; i++) - if (devicePtrs[i]) { - if (devicePtrs[i]->fbNum == -1) { - memmove(psdpp + 1, psdpp, sizeof(psdpp) * (n + 1)); - *psdpp = devicePtrs[i]; - } else - n--; - } -} - -static char * -promGetReg(int type) -{ - char *prop; - int len; - static char regstr[40]; - - regstr[0] = 0; - prop = promGetProperty("reg", &len); - if (prop && len >= 4) { - unsigned int *reg = (unsigned int *)prop; - if (!promP1275 || (type == PROM_NODE_SBUS) || (type == PROM_NODE_EBUS)) - sprintf (regstr, "@%x,%x", reg[0], reg[1]); - else if (type == PROM_NODE_PCI) { - if ((reg[0] >> 8) & 7) - sprintf (regstr, "@%x,%x", (reg[0] >> 11) & 0x1f, (reg[0] >> 8) & 7); - else - sprintf (regstr, "@%x", (reg[0] >> 11) & 0x1f); - } else if (len == 4) - sprintf (regstr, "@%x", reg[0]); - else { - unsigned int regs[2]; - - /* Things get more complicated on UPA. If upa-portid exists, - then address is @upa-portid,second-int-in-reg, otherwise - it is @first-int-in-reg/16,second-int-in-reg (well, probably - upa-portid always exists, but just to be safe). */ - memcpy (regs, reg, sizeof(regs)); - prop = promGetProperty("upa-portid", &len); - if (prop && len == 4) { - reg = (unsigned int *)prop; - sprintf (regstr, "@%x,%x", reg[0], regs[1]); - } else - sprintf (regstr, "@%x,%x", regs[0] >> 4, regs[1]); - } - } - return regstr; -} - -static int -promWalkNode2Pathname(char *path, int parent, int node, int searchNode, int type) -{ - int nextnode; - int len, ntype = type; - char *prop, *p; - - prop = promGetProperty("name", &len); - *path = '/'; - if (!prop || len <= 0) - return 0; - if ((!strcmp(prop, "sbus") || !strcmp(prop, "sbi")) && !type) - ntype = PROM_NODE_SBUS; - else if (!strcmp(prop, "ebus") && type == PROM_NODE_PCI) - ntype = PROM_NODE_EBUS; - else if (!strcmp(prop, "pci") && !type) - ntype = PROM_NODE_PCI; - strcpy (path + 1, prop); - p = promGetReg(type); - if (*p) - strcat (path, p); - if (node == searchNode) - return 1; - nextnode = promGetChild(node); - if (nextnode && - promWalkNode2Pathname(strchr(path, 0), node, nextnode, searchNode, ntype)) - return 1; - nextnode = promGetSibling(node); - if (nextnode && - promWalkNode2Pathname(path, parent, nextnode, searchNode, type)) - return 1; - return 0; -} - -char * -sparcPromNode2Pathname(sbusPromNodePtr pnode) -{ - char *ret; - - if (!pnode->node) return NULL; - ret = xalloc(4096); - if (!ret) return NULL; - if (promWalkNode2Pathname(ret, promRootNode, promGetChild(promRootNode), pnode->node, 0)) - return ret; - xfree(ret); - return NULL; -} - -static int -promWalkPathname2Node(char *name, char *regstr, int parent, int type) -{ - int len, node, ret; - char *prop, *p; - - for (;;) { - prop = promGetProperty("name", &len); - if (!prop || len <= 0) - return 0; - if ((!strcmp(prop, "sbus") || !strcmp(prop, "sbi")) && !type) - type = PROM_NODE_SBUS; - else if (!strcmp(prop, "ebus") && type == PROM_NODE_PCI) - type = PROM_NODE_EBUS; - else if (!strcmp(prop, "pci") && !type) - type = PROM_NODE_PCI; - for (node = promGetChild(parent); node; node = promGetSibling(node)) { - prop = promGetProperty("name", &len); - if (!prop || len <= 0) - continue; - if (*name && strcmp(name, prop)) - continue; - if (*regstr) { - p = promGetReg(type); - if (! *p || strcmp(p + 1, regstr)) - continue; - } - break; - } - if (!node) { - for (node = promGetChild(parent); node; node = promGetSibling(node)) { - ret = promWalkPathname2Node(name, regstr, node, type); - if (ret) return ret; - } - return 0; - } - name = strchr(regstr, 0) + 1; - if (! *name) - return node; - p = strchr(name, '/'); - if (p) - *p = 0; - else - p = strchr(name, 0); - regstr = strchr(name, '@'); - if (regstr) - *regstr++ = 0; - else - regstr = p; - if (name == regstr) - return 0; - parent = node; - } -} - -int -sparcPromPathname2Node(const char *pathName) -{ - int i; - char *name, *regstr, *p; - - i = strlen(pathName); - name = xalloc(i + 2); - if (! name) return 0; - strcpy (name, pathName); - name [i + 1] = 0; - if (name[0] != '/') - return 0; - p = strchr(name + 1, '/'); - if (p) - *p = 0; - else - p = strchr(name, 0); - regstr = strchr(name, '@'); - if (regstr) - *regstr++ = 0; - else - regstr = p; - if (name + 1 == regstr) - return 0; - promGetSibling(0); - i = promWalkPathname2Node(name + 1, regstr, promRootNode, 0); - xfree(name); - return i; -} - -pointer -xf86MapSbusMem(sbusDevicePtr psdp, unsigned long offset, unsigned long size) -{ - pointer ret; - unsigned long pagemask = getpagesize() - 1; - unsigned long off = offset & ~pagemask; - unsigned long len = ((offset + size + pagemask) & ~pagemask) - off; - - if (psdp->fd == -1) { - psdp->fd = open(psdp->device, O_RDWR); - if (psdp->fd == -1) - return NULL; - } else if (psdp->fd < 0) - return NULL; - - ret = (pointer) mmap (NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, - psdp->fd, off); - if (ret == (pointer) -1) { - ret = (pointer) mmap (NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, - psdp->fd, off); - } - if (ret == (pointer) -1) - return NULL; - - return (char *)ret + (offset - off); -} - -void -xf86UnmapSbusMem(sbusDevicePtr psdp, pointer addr, unsigned long size) -{ - unsigned long mask = getpagesize() - 1; - unsigned long base = (unsigned long)addr & ~mask; - unsigned long len = (((unsigned long)addr + size + mask) & ~mask) - base; - - munmap ((pointer)base, len); -} - -/* Tell OS that we are driving the HW cursor ourselves. */ -void -xf86SbusHideOsHwCursor(sbusDevicePtr psdp) -{ - struct fbcursor fbcursor; - unsigned char zeros[8]; - - memset(&fbcursor, 0, sizeof(fbcursor)); - memset(&zeros, 0, sizeof(zeros)); - fbcursor.cmap.count = 2; - fbcursor.cmap.red = zeros; - fbcursor.cmap.green = zeros; - fbcursor.cmap.blue = zeros; - fbcursor.image = (char *)zeros; - fbcursor.mask = (char *)zeros; - fbcursor.size.x = 32; - fbcursor.size.y = 1; - fbcursor.set = FB_CUR_SETALL; - ioctl(psdp->fd, FBIOSCURSOR, &fbcursor); -} - -/* Set HW cursor colormap. */ -void -xf86SbusSetOsHwCursorCmap(sbusDevicePtr psdp, int bg, int fg) -{ - struct fbcursor fbcursor; - unsigned char red[2], green[2], blue[2]; - - memset(&fbcursor, 0, sizeof(fbcursor)); - red[0] = bg >> 16; - green[0] = bg >> 8; - blue[0] = bg; - red[1] = fg >> 16; - green[1] = fg >> 8; - blue[1] = fg; - fbcursor.cmap.count = 2; - fbcursor.cmap.red = red; - fbcursor.cmap.green = green; - fbcursor.cmap.blue = blue; - fbcursor.set = FB_CUR_SETCMAP; - ioctl(psdp->fd, FBIOSCURSOR, &fbcursor); -} +/*
+ * SBUS and OpenPROM access functions.
+ *
+ * Copyright (C) 2000 Jakub Jelinek (jakub@redhat.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * JAKUB JELINEK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#ifdef sun
+#include <sys/utsname.h>
+#endif
+#include "xf86.h"
+#include "xf86Priv.h"
+#include "xf86_OSlib.h"
+
+#include "xf86sbusBus.h"
+#include "xf86Sbus.h"
+
+int promRootNode;
+
+static int promFd = -1;
+static int promCurrentNode;
+static int promOpenCount = 0;
+static int promP1275 = -1;
+#define MAX_PROP 128
+#define MAX_VAL (4096-128-4)
+static struct openpromio *promOpio;
+
+sbusDevicePtr *xf86SbusInfo = NULL;
+
+struct sbus_devtable sbusDeviceTable[] = {
+ { SBUS_DEVICE_BW2, FBTYPE_SUN2BW, "bwtwo", "sunbw2", "Sun Monochrome (bwtwo)" },
+ { SBUS_DEVICE_CG2, FBTYPE_SUN2COLOR, "cgtwo", NULL, "Sun Color2 (cgtwo)" },
+ { SBUS_DEVICE_CG3, FBTYPE_SUN3COLOR, "cgthree", "suncg3", "Sun Color3 (cgthree)" },
+ { SBUS_DEVICE_CG4, FBTYPE_SUN4COLOR, "cgfour", NULL, "Sun Color4 (cgfour)" },
+ { SBUS_DEVICE_CG6, FBTYPE_SUNFAST_COLOR, "cgsix", "suncg6", "Sun GX" },
+ { SBUS_DEVICE_CG8, FBTYPE_MEMCOLOR, "cgeight", NULL, "Sun CG8/RasterOps" },
+ { SBUS_DEVICE_CG12, FBTYPE_SUNGP3, "cgtwelve", NULL, "Sun GS (cgtwelve)" },
+ { SBUS_DEVICE_CG14, FBTYPE_MDICOLOR, "cgfourteen", "suncg14", "Sun SX" },
+ { SBUS_DEVICE_GT, FBTYPE_SUNGT, "gt", NULL, "Sun Graphics Tower" },
+ { SBUS_DEVICE_MGX, -1, "mgx", NULL, "Quantum 3D MGXplus" },
+ { SBUS_DEVICE_LEO, FBTYPE_SUNLEO, "leo", "sunleo", "Sun ZX or Turbo ZX" },
+ { SBUS_DEVICE_TCX, FBTYPE_TCXCOLOR, "tcx", "suntcx", "Sun TCX" },
+ { SBUS_DEVICE_FFB, FBTYPE_CREATOR, "ffb", "sunffb", "Sun FFB" },
+ { SBUS_DEVICE_FFB, FBTYPE_CREATOR, "afb", "sunffb", "Sun Elite3D" },
+ { 0, 0, NULL }
+};
+
+int
+promGetSibling(int node)
+{
+ promOpio->oprom_size = sizeof(int);
+
+ if (node == -1) return 0;
+ *(int *)promOpio->oprom_array = node;
+ if (ioctl(promFd, OPROMNEXT, promOpio) < 0)
+ return 0;
+ promCurrentNode = *(int *)promOpio->oprom_array;
+ return *(int *)promOpio->oprom_array;
+}
+
+int
+promGetChild(int node)
+{
+ promOpio->oprom_size = sizeof(int);
+
+ if (!node || node == -1) return 0;
+ *(int *)promOpio->oprom_array = node;
+ if (ioctl(promFd, OPROMCHILD, promOpio) < 0)
+ return 0;
+ promCurrentNode = *(int *)promOpio->oprom_array;
+ return *(int *)promOpio->oprom_array;
+}
+
+char *
+promGetProperty(const char *prop, int *lenp)
+{
+ promOpio->oprom_size = MAX_VAL;
+
+ strcpy(promOpio->oprom_array, prop);
+ if (ioctl(promFd, OPROMGETPROP, promOpio) < 0)
+ return 0;
+ if (lenp) *lenp = promOpio->oprom_size;
+ return promOpio->oprom_array;
+}
+
+int
+promGetBool(const char *prop)
+{
+ promOpio->oprom_size = 0;
+
+ *(int *)promOpio->oprom_array = 0;
+ for (;;) {
+ promOpio->oprom_size = MAX_PROP;
+ if (ioctl(promFd, OPROMNXTPROP, promOpio) < 0)
+ return 0;
+ if (!promOpio->oprom_size)
+ return 0;
+ if (!strcmp(promOpio->oprom_array, prop))
+ return 1;
+ }
+}
+
+#define PROM_NODE_SIBLING 0x01
+#define PROM_NODE_PREF 0x02
+#define PROM_NODE_SBUS 0x04
+#define PROM_NODE_EBUS 0x08
+#define PROM_NODE_PCI 0x10
+
+static int
+promSetNode(sbusPromNodePtr pnode)
+{
+ int node;
+
+ if (!pnode->node || pnode->node == -1)
+ return -1;
+ if (pnode->cookie[0] & PROM_NODE_SIBLING)
+ node = promGetSibling(pnode->cookie[1]);
+ else
+ node = promGetChild(pnode->cookie[1]);
+ if (pnode->node != node)
+ return -1;
+ return 0;
+}
+
+static void
+promIsP1275(void)
+{
+#ifdef linux
+ FILE *f;
+ char buffer[1024];
+
+ if (promP1275 != -1)
+ return;
+ promP1275 = 0;
+ f = fopen("/proc/cpuinfo","r");
+ if (!f) return;
+ while (fgets(buffer, 1024, f) != NULL)
+ if (!strncmp (buffer, "type", 4) && strstr (buffer, "sun4u")) {
+ promP1275 = 1;
+ break;
+ }
+ fclose(f);
+#elif defined(sun)
+ struct utsname buffer;
+
+ if ((uname(&buffer) >= 0) && !strcmp(buffer.machine, "sun4u"))
+ promP1275 = TRUE;
+ else
+ promP1275 = FALSE;
+#elif defined(__FreeBSD__)
+ promP1275 = TRUE;
+#else
+#error Missing promIsP1275() function for this OS
+#endif
+}
+
+void
+sparcPromClose(void)
+{
+ if (promOpenCount > 1) {
+ promOpenCount--;
+ return;
+ }
+ if (promFd != -1) {
+ close(promFd);
+ promFd = -1;
+ }
+ if (promOpio) {
+ free(promOpio);
+ promOpio = NULL;
+ }
+ promOpenCount = 0;
+}
+
+int
+sparcPromInit(void)
+{
+ if (promOpenCount) {
+ promOpenCount++;
+ return 0;
+ }
+ promFd = open("/dev/openprom", O_RDONLY, 0);
+ if (promFd == -1)
+ return -1;
+ promOpio = (struct openpromio *)malloc(4096);
+ if (!promOpio) {
+ sparcPromClose();
+ return -1;
+ }
+ promRootNode = promGetSibling(0);
+ if (!promRootNode) {
+ sparcPromClose();
+ return -1;
+ }
+ promIsP1275();
+ promOpenCount++;
+
+ return 0;
+}
+
+char *
+sparcPromGetProperty(sbusPromNodePtr pnode, const char *prop, int *lenp)
+{
+ if (promSetNode(pnode))
+ return NULL;
+ return promGetProperty(prop, lenp);
+}
+
+int
+sparcPromGetBool(sbusPromNodePtr pnode, const char *prop)
+{
+ if (promSetNode(pnode))
+ return 0;
+ return promGetBool(prop);
+}
+
+static char *
+promWalkGetDriverName(int node, int oldnode)
+{
+ int nextnode;
+ int len;
+ char *prop;
+ int devId, i;
+
+ prop = promGetProperty("device_type", &len);
+ if (prop && (len > 0)) do {
+ if (!strcmp(prop, "display")) {
+ prop = promGetProperty("name", &len);
+ if (!prop || len <= 0)
+ break;
+ while ((*prop >= 'A' && *prop <= 'Z') || *prop == ',')
+ prop++;
+ for (i = 0; sbusDeviceTable[i].devId; i++)
+ if (!strcmp(prop, sbusDeviceTable[i].promName))
+ break;
+ devId = sbusDeviceTable[i].devId;
+ if (!devId)
+ break;
+ if (sbusDeviceTable[i].driverName)
+ return sbusDeviceTable[i].driverName;
+ }
+ } while (0);
+
+ nextnode = promGetChild(node);
+ if (nextnode) {
+ char *name;
+ name = promWalkGetDriverName(nextnode, node);
+ if (name)
+ return name;
+ }
+
+ nextnode = promGetSibling(node);
+ if (nextnode)
+ return promWalkGetDriverName(nextnode, node);
+ return NULL;
+}
+
+char *
+sparcDriverName(void)
+{
+ char *name;
+
+ if (sparcPromInit() < 0)
+ return NULL;
+ promGetSibling(0);
+ name = promWalkGetDriverName(promRootNode, 0);
+ sparcPromClose();
+ return name;
+}
+
+static void
+promWalkAssignNodes(int node, int oldnode, int flags, sbusDevicePtr *devicePtrs)
+{
+ int nextnode;
+ int len, sbus = flags & PROM_NODE_SBUS;
+ char *prop;
+ int devId, i, j;
+ sbusPromNode pNode, pNode2;
+
+ prop = promGetProperty("device_type", &len);
+ if (prop && (len > 0)) do {
+ if (!strcmp(prop, "display")) {
+ prop = promGetProperty("name", &len);
+ if (!prop || len <= 0)
+ break;
+ while ((*prop >= 'A' && *prop <= 'Z') || *prop == ',')
+ prop++;
+ for (i = 0; sbusDeviceTable[i].devId; i++)
+ if (!strcmp(prop, sbusDeviceTable[i].promName))
+ break;
+ devId = sbusDeviceTable[i].devId;
+ if (!devId)
+ break;
+ if (!sbus) {
+ if (devId == SBUS_DEVICE_FFB) {
+ /*
+ * All /SUNW,ffb outside of SBUS tree come before all
+ * /SUNW,afb outside of SBUS tree in Linux.
+ */
+ if (!strcmp(prop, "afb"))
+ flags |= PROM_NODE_PREF;
+ } else if (devId != SBUS_DEVICE_CG14)
+ break;
+ }
+ for (i = 0; i < 32; i++) {
+ if (!devicePtrs[i] || devicePtrs[i]->devId != devId)
+ continue;
+ if (devicePtrs[i]->node.node) {
+ if ((devicePtrs[i]->node.cookie[0] & ~PROM_NODE_SIBLING) <=
+ (flags & ~PROM_NODE_SIBLING))
+ continue;
+ for (j = i + 1, pNode = devicePtrs[i]->node; j < 32; j++) {
+ if (!devicePtrs[j] || devicePtrs[j]->devId != devId)
+ continue;
+ pNode2 = devicePtrs[j]->node;
+ devicePtrs[j]->node = pNode;
+ pNode = pNode2;
+ }
+ }
+ devicePtrs[i]->node.node = node;
+ devicePtrs[i]->node.cookie[0] = flags;
+ devicePtrs[i]->node.cookie[1] = oldnode;
+ break;
+ }
+ break;
+ }
+ } while (0);
+
+ prop = promGetProperty("name", &len);
+ if (prop && len > 0) {
+ if (!strcmp(prop, "sbus") || !strcmp(prop, "sbi"))
+ sbus = PROM_NODE_SBUS;
+ }
+
+ nextnode = promGetChild(node);
+ if (nextnode)
+ promWalkAssignNodes(nextnode, node, sbus, devicePtrs);
+
+ nextnode = promGetSibling(node);
+ if (nextnode)
+ promWalkAssignNodes(nextnode, node, PROM_NODE_SIBLING | sbus, devicePtrs);
+}
+
+void
+sparcPromAssignNodes(void)
+{
+ sbusDevicePtr psdp, *psdpp;
+ int n, holes = 0, i, j;
+ FILE *f;
+ sbusDevicePtr devicePtrs[32];
+
+ memset(devicePtrs, 0, sizeof(devicePtrs));
+ for (psdpp = xf86SbusInfo, n = 0; (psdp = *psdpp); psdpp++, n++) {
+ if (psdp->fbNum != n)
+ holes = 1;
+ devicePtrs[psdp->fbNum] = psdp;
+ }
+ if (holes && (f = fopen("/proc/fb", "r")) != NULL) {
+ /* We could not open one of fb devices, check /proc/fb to see what
+ * were the types of the cards missed. */
+ char buffer[64];
+ int fbNum, devId;
+ static struct {
+ int devId;
+ char *prefix;
+ } procFbPrefixes[] = {
+ { SBUS_DEVICE_BW2, "BWtwo" },
+ { SBUS_DEVICE_CG14, "CGfourteen" },
+ { SBUS_DEVICE_CG6, "CGsix" },
+ { SBUS_DEVICE_CG3, "CGthree" },
+ { SBUS_DEVICE_FFB, "Creator" },
+ { SBUS_DEVICE_FFB, "Elite 3D" },
+ { SBUS_DEVICE_LEO, "Leo" },
+ { SBUS_DEVICE_TCX, "TCX" },
+ { 0, NULL },
+ };
+
+ while (fscanf(f, "%d %63s\n", &fbNum, buffer) == 2) {
+ for (i = 0; procFbPrefixes[i].devId; i++)
+ if (! strncmp(procFbPrefixes[i].prefix, buffer,
+ strlen(procFbPrefixes[i].prefix)))
+ break;
+ devId = procFbPrefixes[i].devId;
+ if (! devId) continue;
+ if (devicePtrs[fbNum]) {
+ if (devicePtrs[fbNum]->devId != devId)
+ xf86ErrorF("Inconsistent /proc/fb with FBIOGATTR\n");
+ } else if (!devicePtrs[fbNum]) {
+ devicePtrs[fbNum] = psdp = xnfcalloc(sizeof (sbusDevice), 1);
+ psdp->devId = devId;
+ psdp->fbNum = fbNum;
+ psdp->fd = -2;
+ }
+ }
+ fclose(f);
+ }
+ promGetSibling(0);
+ promWalkAssignNodes(promRootNode, 0, PROM_NODE_PREF, devicePtrs);
+ for (i = 0, j = 0; i < 32; i++)
+ if (devicePtrs[i] && devicePtrs[i]->fbNum == -1)
+ j++;
+ xf86SbusInfo = xnfrealloc(xf86SbusInfo, sizeof(psdp) * (n + j + 1));
+ for (i = 0, psdpp = xf86SbusInfo; i < 32; i++)
+ if (devicePtrs[i]) {
+ if (devicePtrs[i]->fbNum == -1) {
+ memmove(psdpp + 1, psdpp, sizeof(psdpp) * (n + 1));
+ *psdpp = devicePtrs[i];
+ } else
+ n--;
+ }
+}
+
+static char *
+promGetReg(int type)
+{
+ char *prop;
+ int len;
+ static char regstr[40];
+
+ regstr[0] = 0;
+ prop = promGetProperty("reg", &len);
+ if (prop && len >= 4) {
+ unsigned int *reg = (unsigned int *)prop;
+ if (!promP1275 || (type == PROM_NODE_SBUS) || (type == PROM_NODE_EBUS))
+ sprintf (regstr, "@%x,%x", reg[0], reg[1]);
+ else if (type == PROM_NODE_PCI) {
+ if ((reg[0] >> 8) & 7)
+ sprintf (regstr, "@%x,%x", (reg[0] >> 11) & 0x1f, (reg[0] >> 8) & 7);
+ else
+ sprintf (regstr, "@%x", (reg[0] >> 11) & 0x1f);
+ } else if (len == 4)
+ sprintf (regstr, "@%x", reg[0]);
+ else {
+ unsigned int regs[2];
+
+ /* Things get more complicated on UPA. If upa-portid exists,
+ then address is @upa-portid,second-int-in-reg, otherwise
+ it is @first-int-in-reg/16,second-int-in-reg (well, probably
+ upa-portid always exists, but just to be safe). */
+ memcpy (regs, reg, sizeof(regs));
+ prop = promGetProperty("upa-portid", &len);
+ if (prop && len == 4) {
+ reg = (unsigned int *)prop;
+ sprintf (regstr, "@%x,%x", reg[0], regs[1]);
+ } else
+ sprintf (regstr, "@%x,%x", regs[0] >> 4, regs[1]);
+ }
+ }
+ return regstr;
+}
+
+static int
+promWalkNode2Pathname(char *path, int parent, int node, int searchNode, int type)
+{
+ int nextnode;
+ int len, ntype = type;
+ char *prop, *p;
+
+ prop = promGetProperty("name", &len);
+ *path = '/';
+ if (!prop || len <= 0)
+ return 0;
+ if ((!strcmp(prop, "sbus") || !strcmp(prop, "sbi")) && !type)
+ ntype = PROM_NODE_SBUS;
+ else if (!strcmp(prop, "ebus") && type == PROM_NODE_PCI)
+ ntype = PROM_NODE_EBUS;
+ else if (!strcmp(prop, "pci") && !type)
+ ntype = PROM_NODE_PCI;
+ strcpy (path + 1, prop);
+ p = promGetReg(type);
+ if (*p)
+ strcat (path, p);
+ if (node == searchNode)
+ return 1;
+ nextnode = promGetChild(node);
+ if (nextnode &&
+ promWalkNode2Pathname(strchr(path, 0), node, nextnode, searchNode, ntype))
+ return 1;
+ nextnode = promGetSibling(node);
+ if (nextnode &&
+ promWalkNode2Pathname(path, parent, nextnode, searchNode, type))
+ return 1;
+ return 0;
+}
+
+char *
+sparcPromNode2Pathname(sbusPromNodePtr pnode)
+{
+ char *ret;
+
+ if (!pnode->node) return NULL;
+ ret = malloc(4096);
+ if (!ret) return NULL;
+ if (promWalkNode2Pathname(ret, promRootNode, promGetChild(promRootNode), pnode->node, 0))
+ return ret;
+ free(ret);
+ return NULL;
+}
+
+static int
+promWalkPathname2Node(char *name, char *regstr, int parent, int type)
+{
+ int len, node, ret;
+ char *prop, *p;
+
+ for (;;) {
+ prop = promGetProperty("name", &len);
+ if (!prop || len <= 0)
+ return 0;
+ if ((!strcmp(prop, "sbus") || !strcmp(prop, "sbi")) && !type)
+ type = PROM_NODE_SBUS;
+ else if (!strcmp(prop, "ebus") && type == PROM_NODE_PCI)
+ type = PROM_NODE_EBUS;
+ else if (!strcmp(prop, "pci") && !type)
+ type = PROM_NODE_PCI;
+ for (node = promGetChild(parent); node; node = promGetSibling(node)) {
+ prop = promGetProperty("name", &len);
+ if (!prop || len <= 0)
+ continue;
+ if (*name && strcmp(name, prop))
+ continue;
+ if (*regstr) {
+ p = promGetReg(type);
+ if (! *p || strcmp(p + 1, regstr))
+ continue;
+ }
+ break;
+ }
+ if (!node) {
+ for (node = promGetChild(parent); node; node = promGetSibling(node)) {
+ ret = promWalkPathname2Node(name, regstr, node, type);
+ if (ret) return ret;
+ }
+ return 0;
+ }
+ name = strchr(regstr, 0) + 1;
+ if (! *name)
+ return node;
+ p = strchr(name, '/');
+ if (p)
+ *p = 0;
+ else
+ p = strchr(name, 0);
+ regstr = strchr(name, '@');
+ if (regstr)
+ *regstr++ = 0;
+ else
+ regstr = p;
+ if (name == regstr)
+ return 0;
+ parent = node;
+ }
+}
+
+int
+sparcPromPathname2Node(const char *pathName)
+{
+ int i;
+ char *name, *regstr, *p;
+
+ i = strlen(pathName);
+ name = malloc(i + 2);
+ if (! name) return 0;
+ strcpy (name, pathName);
+ name [i + 1] = 0;
+ if (name[0] != '/')
+ return 0;
+ p = strchr(name + 1, '/');
+ if (p)
+ *p = 0;
+ else
+ p = strchr(name, 0);
+ regstr = strchr(name, '@');
+ if (regstr)
+ *regstr++ = 0;
+ else
+ regstr = p;
+ if (name + 1 == regstr)
+ return 0;
+ promGetSibling(0);
+ i = promWalkPathname2Node(name + 1, regstr, promRootNode, 0);
+ free(name);
+ return i;
+}
+
+pointer
+xf86MapSbusMem(sbusDevicePtr psdp, unsigned long offset, unsigned long size)
+{
+ pointer ret;
+ unsigned long pagemask = getpagesize() - 1;
+ unsigned long off = offset & ~pagemask;
+ unsigned long len = ((offset + size + pagemask) & ~pagemask) - off;
+
+ if (psdp->fd == -1) {
+ psdp->fd = open(psdp->device, O_RDWR);
+ if (psdp->fd == -1)
+ return NULL;
+ } else if (psdp->fd < 0)
+ return NULL;
+
+ ret = (pointer) mmap (NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
+ psdp->fd, off);
+ if (ret == (pointer) -1) {
+ ret = (pointer) mmap (NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
+ psdp->fd, off);
+ }
+ if (ret == (pointer) -1)
+ return NULL;
+
+ return (char *)ret + (offset - off);
+}
+
+void
+xf86UnmapSbusMem(sbusDevicePtr psdp, pointer addr, unsigned long size)
+{
+ unsigned long mask = getpagesize() - 1;
+ unsigned long base = (unsigned long)addr & ~mask;
+ unsigned long len = (((unsigned long)addr + size + mask) & ~mask) - base;
+
+ munmap ((pointer)base, len);
+}
+
+/* Tell OS that we are driving the HW cursor ourselves. */
+void
+xf86SbusHideOsHwCursor(sbusDevicePtr psdp)
+{
+ struct fbcursor fbcursor;
+ unsigned char zeros[8];
+
+ memset(&fbcursor, 0, sizeof(fbcursor));
+ memset(&zeros, 0, sizeof(zeros));
+ fbcursor.cmap.count = 2;
+ fbcursor.cmap.red = zeros;
+ fbcursor.cmap.green = zeros;
+ fbcursor.cmap.blue = zeros;
+ fbcursor.image = (char *)zeros;
+ fbcursor.mask = (char *)zeros;
+ fbcursor.size.x = 32;
+ fbcursor.size.y = 1;
+ fbcursor.set = FB_CUR_SETALL;
+ ioctl(psdp->fd, FBIOSCURSOR, &fbcursor);
+}
+
+/* Set HW cursor colormap. */
+void
+xf86SbusSetOsHwCursorCmap(sbusDevicePtr psdp, int bg, int fg)
+{
+ struct fbcursor fbcursor;
+ unsigned char red[2], green[2], blue[2];
+
+ memset(&fbcursor, 0, sizeof(fbcursor));
+ red[0] = bg >> 16;
+ green[0] = bg >> 8;
+ blue[0] = bg;
+ red[1] = fg >> 16;
+ green[1] = fg >> 8;
+ blue[1] = fg;
+ fbcursor.cmap.count = 2;
+ fbcursor.cmap.red = red;
+ fbcursor.cmap.green = green;
+ fbcursor.cmap.blue = blue;
+ fbcursor.set = FB_CUR_SETCMAP;
+ ioctl(psdp->fd, FBIOSCURSOR, &fbcursor);
+}
diff --git a/xorg-server/hw/xfree86/os-support/linux/int10/linux.c b/xorg-server/hw/xfree86/os-support/linux/int10/linux.c index 0cf3507c3..e3c8a984d 100644 --- a/xorg-server/hw/xfree86/os-support/linux/int10/linux.c +++ b/xorg-server/hw/xfree86/os-support/linux/int10/linux.c @@ -1,544 +1,544 @@ -/* - * linux specific part of the int10 module - * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2008 Egbert Eich - */ -#ifdef HAVE_XORG_CONFIG_H -#include <xorg-config.h> -#endif - -#include "xf86.h" -#include "xf86_OSproc.h" -#include "xf86Pci.h" -#include "compiler.h" -#define _INT10_PRIVATE -#include "xf86int10.h" -#ifdef __sparc__ -#define DEV_MEM "/dev/fb" -#else -#define DEV_MEM "/dev/mem" -#endif -#define ALLOC_ENTRIES(x) ((V_RAM / x) - 1) -#define SHMERRORPTR (pointer)(-1) - -#include <fcntl.h> -#include <errno.h> -#include <sys/mman.h> -#include <sys/ipc.h> -#include <sys/shm.h> -#include <unistd.h> -#include <string.h> - -static int counter = 0; -static unsigned long int10Generation = 0; - -static CARD8 read_b(xf86Int10InfoPtr pInt, int addr); -static CARD16 read_w(xf86Int10InfoPtr pInt, int addr); -static CARD32 read_l(xf86Int10InfoPtr pInt, int addr); -static void write_b(xf86Int10InfoPtr pInt, int addr, CARD8 val); -static void write_w(xf86Int10InfoPtr pInt, int addr, CARD16 val); -static void write_l(xf86Int10InfoPtr pInt, int addr, CARD32 val); - -int10MemRec linuxMem = { - read_b, - read_w, - read_l, - write_b, - write_w, - write_l -}; - -typedef struct { - int lowMem; - int highMem; - char* base; - char* base_high; - int screen; - char* alloc; -} linuxInt10Priv; - -#if defined DoSubModules - -typedef enum { - INT10_NOT_LOADED, - INT10_LOADED_VM86, - INT10_LOADED_X86EMU, - INT10_LOAD_FAILED -} Int10LinuxSubModuleState; - -static Int10LinuxSubModuleState loadedSubModule = INT10_NOT_LOADED; - -static Int10LinuxSubModuleState int10LinuxLoadSubModule(ScrnInfoPtr pScrn); - -#endif /* DoSubModules */ - -xf86Int10InfoPtr -xf86ExtendedInitInt10(int entityIndex, int Flags) -{ - xf86Int10InfoPtr pInt = NULL; - int screen; - int fd; - static void* vidMem = NULL; - static void* sysMem = NULL; - void* vMem = NULL; - void *options = NULL; - int low_mem; - int high_mem = -1; - char *base = SHMERRORPTR; - char *base_high = SHMERRORPTR; - int pagesize; - memType cs; - legacyVGARec vga; - Bool videoBiosMapped = FALSE; - - if (int10Generation != serverGeneration) { - counter = 0; - int10Generation = serverGeneration; - } - - screen = (xf86FindScreenForEntity(entityIndex))->scrnIndex; - - options = xf86HandleInt10Options(xf86Screens[screen],entityIndex); - - if (int10skip(options)) { - xfree(options); - return NULL; - } - -#if defined DoSubModules - if (loadedSubModule == INT10_NOT_LOADED) - loadedSubModule = int10LinuxLoadSubModule(xf86Screens[screen]); - - if (loadedSubModule == INT10_LOAD_FAILED) - return NULL; -#endif - - if ((!vidMem) || (!sysMem)) { - if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) { - if (!sysMem) { - DebugF("Mapping sys bios area\n"); - if ((sysMem = mmap((void *)(SYS_BIOS), BIOS_SIZE, - PROT_READ | PROT_EXEC, - MAP_SHARED | MAP_FIXED, fd, SYS_BIOS)) - == MAP_FAILED) { - xf86DrvMsg(screen, X_ERROR, "Cannot map SYS BIOS\n"); - close(fd); - goto error0; - } - } - if (!vidMem) { - DebugF("Mapping VRAM area\n"); - if ((vidMem = mmap((void *)(V_RAM), VRAM_SIZE, - PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_SHARED | MAP_FIXED, fd, V_RAM)) - == MAP_FAILED) { - xf86DrvMsg(screen, X_ERROR, "Cannot map V_RAM\n"); - close(fd); - goto error0; - } - } - close(fd); - } else { - xf86DrvMsg(screen, X_ERROR, "Cannot open %s\n", DEV_MEM); - goto error0; - } - } - - pInt = (xf86Int10InfoPtr)xnfcalloc(1, sizeof(xf86Int10InfoRec)); - pInt->scrnIndex = screen; - pInt->entityIndex = entityIndex; - pInt->dev = xf86GetPciInfoForEntity(entityIndex); - - if (!xf86Int10ExecSetup(pInt)) - goto error0; - pInt->mem = &linuxMem; - pagesize = getpagesize(); - pInt->private = (pointer)xnfcalloc(1, sizeof(linuxInt10Priv)); - ((linuxInt10Priv*)pInt->private)->screen = screen; - ((linuxInt10Priv*)pInt->private)->alloc = - (pointer)xnfcalloc(1, ALLOC_ENTRIES(pagesize)); - - if (!xf86IsEntityPrimary(entityIndex)) { - DebugF("Mapping high memory area\n"); - if ((high_mem = shmget(counter++, HIGH_MEM_SIZE, - IPC_CREAT | SHM_R | SHM_W)) == -1) { - if (errno == ENOSYS) - xf86DrvMsg(screen, X_ERROR, "shmget error\n Please reconfigure" - " your kernel to include System V IPC support\n"); - else - xf86DrvMsg(screen, X_ERROR, - "shmget(highmem) error: %s\n",strerror(errno)); - goto error1; - } - } else { - DebugF("Mapping Video BIOS\n"); - videoBiosMapped = TRUE; - if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) { - if ((vMem = mmap((void *)(V_BIOS), SYS_BIOS - V_BIOS, - PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_SHARED | MAP_FIXED, fd, V_BIOS)) - == MAP_FAILED) { - xf86DrvMsg(screen, X_ERROR, "Cannot map V_BIOS\n"); - close(fd); - goto error1; - } - close (fd); - } else - goto error1; - } - ((linuxInt10Priv*)pInt->private)->highMem = high_mem; - - DebugF("Mapping 640kB area\n"); - if ((low_mem = shmget(counter++, V_RAM, - IPC_CREAT | SHM_R | SHM_W)) == -1) { - xf86DrvMsg(screen, X_ERROR, - "shmget(lowmem) error: %s\n",strerror(errno)); - goto error2; - } - - ((linuxInt10Priv*)pInt->private)->lowMem = low_mem; - base = shmat(low_mem, 0, 0); - if (base == SHMERRORPTR) { - xf86DrvMsg(screen, X_ERROR, - "shmat(low_mem) error: %s\n",strerror(errno)); - goto error3; - } - ((linuxInt10Priv *)pInt->private)->base = base; - if (high_mem > -1) { - base_high = shmat(high_mem, 0, 0); - if (base_high == SHMERRORPTR) { - xf86DrvMsg(screen, X_ERROR, - "shmat(high_mem) error: %s\n",strerror(errno)); - goto error3; - } - ((linuxInt10Priv*)pInt->private)->base_high = base_high; - } else - ((linuxInt10Priv*)pInt->private)->base_high = NULL; - - if (!MapCurrentInt10(pInt)) - goto error3; - - Int10Current = pInt; - - DebugF("Mapping int area\n"); - if (xf86ReadBIOS(0, 0, (unsigned char *)0, LOW_PAGE_SIZE) < 0) { - xf86DrvMsg(screen, X_ERROR, "Cannot read int vect\n"); - goto error3; - } - DebugF("done\n"); - /* - * Read in everything between V_BIOS and SYS_BIOS as some system BIOSes - * have executable code there. Note that xf86ReadBIOS() can only bring in - * 64K bytes at a time. - */ - if (!videoBiosMapped) { - memset((pointer)V_BIOS, 0, SYS_BIOS - V_BIOS); - DebugF("Reading BIOS\n"); - for (cs = V_BIOS; cs < SYS_BIOS; cs += V_BIOS_SIZE) - if (xf86ReadBIOS(cs, 0, (pointer)cs, V_BIOS_SIZE) < V_BIOS_SIZE) - xf86DrvMsg(screen, X_WARNING, - "Unable to retrieve all of segment 0x%06lX.\n", - (long)cs); - DebugF("done\n"); - } - - if (xf86IsEntityPrimary(entityIndex) && !(initPrimary(options))) { - if (!xf86int10GetBiosSegment(pInt, NULL)) - goto error3; - - set_return_trap(pInt); -#ifdef _PC - pInt->Flags = Flags & (SET_BIOS_SCRATCH | RESTORE_BIOS_SCRATCH); - if (! (pInt->Flags & SET_BIOS_SCRATCH)) - pInt->Flags &= ~RESTORE_BIOS_SCRATCH; - xf86Int10SaveRestoreBIOSVars(pInt, TRUE); -#endif - } else { - const BusType location_type = xf86int10GetBiosLocationType(pInt); - - switch (location_type) { - case BUS_PCI: { - int err; - struct pci_device *rom_device = - xf86GetPciInfoForEntity(pInt->entityIndex); - -#if HAVE_PCI_DEVICE_ENABLE - pci_device_enable(rom_device); -#endif - - err = pci_device_read_rom(rom_device, (unsigned char *)(V_BIOS)); - if (err) { - xf86DrvMsg(screen,X_ERROR,"Cannot read V_BIOS (%s)\n", - strerror(err)); - goto error3; - } - - pInt->BIOSseg = V_BIOS >> 4; - break; - } - default: - goto error3; - } - - pInt->num = 0xe6; - reset_int_vect(pInt); - set_return_trap(pInt); - LockLegacyVGA(pInt, &vga); - xf86ExecX86int10(pInt); - UnlockLegacyVGA(pInt, &vga); - } -#ifdef DEBUG - dprint(0xc0000, 0x20); -#endif - - xfree(options); - return pInt; - -error3: - if (base_high) - shmdt(base_high); - shmdt(base); - shmdt(0); - if (base_high) - shmdt((char*)HIGH_MEM); - shmctl(low_mem, IPC_RMID, NULL); - Int10Current = NULL; -error2: - if (high_mem > -1) - shmctl(high_mem, IPC_RMID,NULL); -error1: - if (vMem) - munmap(vMem, SYS_BIOS - V_BIOS); - xfree(((linuxInt10Priv*)pInt->private)->alloc); - xfree(pInt->private); -error0: - xfree(options); - xfree(pInt); - return NULL; -} - -Bool -MapCurrentInt10(xf86Int10InfoPtr pInt) -{ - pointer addr; - int fd = -1; - - if (Int10Current) { - shmdt(0); - if (((linuxInt10Priv*)Int10Current->private)->highMem >= 0) - shmdt((char*)HIGH_MEM); - else - munmap((pointer)V_BIOS, (SYS_BIOS - V_BIOS)); - } - addr = shmat(((linuxInt10Priv*)pInt->private)->lowMem, (char*)1, SHM_RND); - if (addr == SHMERRORPTR) { - xf86DrvMsg(pInt->scrnIndex, X_ERROR, "Cannot shmat() low memory\n"); - xf86DrvMsg(pInt->scrnIndex, X_ERROR, - "shmat(low_mem) error: %s\n",strerror(errno)); - return FALSE; - } - if (mprotect((void*)0, V_RAM, PROT_READ|PROT_WRITE|PROT_EXEC) != 0) - xf86DrvMsg(pInt->scrnIndex, X_ERROR, - "Cannot set EXEC bit on low memory: %s\n", strerror(errno)); - - if (((linuxInt10Priv*)pInt->private)->highMem >= 0) { - addr = shmat(((linuxInt10Priv*)pInt->private)->highMem, - (char*)HIGH_MEM, 0); - if (addr == SHMERRORPTR) { - xf86DrvMsg(pInt->scrnIndex, X_ERROR, - "Cannot shmat() high memory\n"); - xf86DrvMsg(pInt->scrnIndex, X_ERROR, - "shmget error: %s\n",strerror(errno)); - return FALSE; - } - if (mprotect((void*)HIGH_MEM, HIGH_MEM_SIZE, - PROT_READ|PROT_WRITE|PROT_EXEC) != 0) - xf86DrvMsg(pInt->scrnIndex, X_ERROR, - "Cannot set EXEC bit on high memory: %s\n", - strerror(errno)); - } else { - if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) { - if (mmap((void *)(V_BIOS), SYS_BIOS - V_BIOS, - PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_SHARED | MAP_FIXED, fd, V_BIOS) - == MAP_FAILED) { - xf86DrvMsg(pInt->scrnIndex, X_ERROR, "Cannot map V_BIOS\n"); - close (fd); - return FALSE; - } - } else { - xf86DrvMsg(pInt->scrnIndex, X_ERROR, "Cannot open %s\n",DEV_MEM); - return FALSE; - } - close (fd); - } - - return TRUE; -} - -void -xf86FreeInt10(xf86Int10InfoPtr pInt) -{ - if (!pInt) - return; - -#ifdef _PC - xf86Int10SaveRestoreBIOSVars(pInt, FALSE); -#endif - if (Int10Current == pInt) { - shmdt(0); - if (((linuxInt10Priv*)pInt->private)->highMem >= 0) - shmdt((char*)HIGH_MEM); - else - munmap((pointer)V_BIOS, (SYS_BIOS - V_BIOS)); - Int10Current = NULL; - } - - if (((linuxInt10Priv*)pInt->private)->base_high) - shmdt(((linuxInt10Priv*)pInt->private)->base_high); - shmdt(((linuxInt10Priv*)pInt->private)->base); - shmctl(((linuxInt10Priv*)pInt->private)->lowMem, IPC_RMID, NULL); - if (((linuxInt10Priv*)pInt->private)->highMem >= 0) - shmctl(((linuxInt10Priv*)pInt->private)->highMem, IPC_RMID, NULL); - xfree(((linuxInt10Priv*)pInt->private)->alloc); - xfree(pInt->private); - xfree(pInt); -} - -void * -xf86Int10AllocPages(xf86Int10InfoPtr pInt, int num, int *off) -{ - int pagesize = getpagesize(); - int num_pages = ALLOC_ENTRIES(pagesize); - int i, j; - - for (i = 0; i < (num_pages - num); i++) { - if (((linuxInt10Priv*)pInt->private)->alloc[i] == 0) { - for (j = i; j < (num + i); j++) - if ((((linuxInt10Priv*)pInt->private)->alloc[j] != 0)) - break; - if (j == (num + i)) - break; - else - i = i + num; - } - } - if (i == (num_pages - num)) - return NULL; - - for (j = i; j < (i + num); j++) - ((linuxInt10Priv*)pInt->private)->alloc[j] = 1; - - *off = (i + 1) * pagesize; - - return ((linuxInt10Priv*)pInt->private)->base + ((i + 1) * pagesize); -} - -void -xf86Int10FreePages(xf86Int10InfoPtr pInt, void *pbase, int num) -{ - int pagesize = getpagesize(); - int first = (((unsigned long)pbase - - (unsigned long)((linuxInt10Priv*)pInt->private)->base) - / pagesize) - 1; - int i; - - for (i = first; i < (first + num); i++) - ((linuxInt10Priv*)pInt->private)->alloc[i] = 0; -} - -static CARD8 -read_b(xf86Int10InfoPtr pInt, int addr) -{ - return *((CARD8 *)(memType)addr); -} - -static CARD16 -read_w(xf86Int10InfoPtr pInt, int addr) -{ - return *((CARD16 *)(memType)addr); -} - -static CARD32 -read_l(xf86Int10InfoPtr pInt, int addr) -{ - return *((CARD32 *)(memType)addr); -} - -static void -write_b(xf86Int10InfoPtr pInt, int addr, CARD8 val) -{ - *((CARD8 *)(memType)addr) = val; -} - -static void -write_w(xf86Int10InfoPtr pInt, int addr, CARD16 val) -{ - *((CARD16 *)(memType)addr) = val; -} - -static -void write_l(xf86Int10InfoPtr pInt, int addr, CARD32 val) -{ - *((CARD32 *)(memType) addr) = val; -} - -pointer -xf86int10Addr(xf86Int10InfoPtr pInt, CARD32 addr) -{ - if (addr < V_RAM) - return ((linuxInt10Priv*)pInt->private)->base + addr; - else if (addr < V_BIOS) - return (pointer)(memType)addr; - else if (addr < SYS_BIOS) { - if (((linuxInt10Priv*)pInt->private)->base_high) - return (pointer)(((linuxInt10Priv*)pInt->private)->base_high - - V_BIOS + addr); - else - return (pointer) (memType)addr; - } else - return (pointer) (memType)addr; -} - -#if defined DoSubModules - -static Bool -vm86_tst(void) -{ - int __res; - -#ifdef __PIC__ - /* When compiling with -fPIC, we can't use asm constraint "b" because - %ebx is already taken by gcc. */ - __asm__ __volatile__("pushl %%ebx\n\t" - "movl %2,%%ebx\n\t" - "movl %1,%%eax\n\t" - "int $0x80\n\t" - "popl %%ebx" - :"=a" (__res) - :"n" ((int)113), "r" (NULL)); -#else - __asm__ __volatile__("int $0x80\n\t" - :"=a" (__res):"a" ((int)113), - "b" ((struct vm86_struct *)NULL)); -#endif - - if (__res < 0 && __res == -ENOSYS) - return FALSE; - - return TRUE; -} - -static Int10LinuxSubModuleState -int10LinuxLoadSubModule(ScrnInfoPtr pScrn) -{ - if (vm86_tst()) { - if (xf86LoadSubModule(pScrn,"vm86")) - return INT10_LOADED_VM86; - } - if (xf86LoadSubModule(pScrn,"x86emu")) - return INT10_LOADED_X86EMU; - - return INT10_LOAD_FAILED; -} - -#endif /* DoSubModules */ +/*
+ * linux specific part of the int10 module
+ * Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2008 Egbert Eich
+ */
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include "xf86.h"
+#include "xf86_OSproc.h"
+#include "xf86Pci.h"
+#include "compiler.h"
+#define _INT10_PRIVATE
+#include "xf86int10.h"
+#ifdef __sparc__
+#define DEV_MEM "/dev/fb"
+#else
+#define DEV_MEM "/dev/mem"
+#endif
+#define ALLOC_ENTRIES(x) ((V_RAM / x) - 1)
+#define SHMERRORPTR (pointer)(-1)
+
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <sys/ipc.h>
+#include <sys/shm.h>
+#include <unistd.h>
+#include <string.h>
+
+static int counter = 0;
+static unsigned long int10Generation = 0;
+
+static CARD8 read_b(xf86Int10InfoPtr pInt, int addr);
+static CARD16 read_w(xf86Int10InfoPtr pInt, int addr);
+static CARD32 read_l(xf86Int10InfoPtr pInt, int addr);
+static void write_b(xf86Int10InfoPtr pInt, int addr, CARD8 val);
+static void write_w(xf86Int10InfoPtr pInt, int addr, CARD16 val);
+static void write_l(xf86Int10InfoPtr pInt, int addr, CARD32 val);
+
+int10MemRec linuxMem = {
+ read_b,
+ read_w,
+ read_l,
+ write_b,
+ write_w,
+ write_l
+};
+
+typedef struct {
+ int lowMem;
+ int highMem;
+ char* base;
+ char* base_high;
+ int screen;
+ char* alloc;
+} linuxInt10Priv;
+
+#if defined DoSubModules
+
+typedef enum {
+ INT10_NOT_LOADED,
+ INT10_LOADED_VM86,
+ INT10_LOADED_X86EMU,
+ INT10_LOAD_FAILED
+} Int10LinuxSubModuleState;
+
+static Int10LinuxSubModuleState loadedSubModule = INT10_NOT_LOADED;
+
+static Int10LinuxSubModuleState int10LinuxLoadSubModule(ScrnInfoPtr pScrn);
+
+#endif /* DoSubModules */
+
+xf86Int10InfoPtr
+xf86ExtendedInitInt10(int entityIndex, int Flags)
+{
+ xf86Int10InfoPtr pInt = NULL;
+ int screen;
+ int fd;
+ static void* vidMem = NULL;
+ static void* sysMem = NULL;
+ void* vMem = NULL;
+ void *options = NULL;
+ int low_mem;
+ int high_mem = -1;
+ char *base = SHMERRORPTR;
+ char *base_high = SHMERRORPTR;
+ int pagesize;
+ memType cs;
+ legacyVGARec vga;
+ Bool videoBiosMapped = FALSE;
+
+ if (int10Generation != serverGeneration) {
+ counter = 0;
+ int10Generation = serverGeneration;
+ }
+
+ screen = (xf86FindScreenForEntity(entityIndex))->scrnIndex;
+
+ options = xf86HandleInt10Options(xf86Screens[screen],entityIndex);
+
+ if (int10skip(options)) {
+ free(options);
+ return NULL;
+ }
+
+#if defined DoSubModules
+ if (loadedSubModule == INT10_NOT_LOADED)
+ loadedSubModule = int10LinuxLoadSubModule(xf86Screens[screen]);
+
+ if (loadedSubModule == INT10_LOAD_FAILED)
+ return NULL;
+#endif
+
+ if ((!vidMem) || (!sysMem)) {
+ if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) {
+ if (!sysMem) {
+ DebugF("Mapping sys bios area\n");
+ if ((sysMem = mmap((void *)(SYS_BIOS), BIOS_SIZE,
+ PROT_READ | PROT_EXEC,
+ MAP_SHARED | MAP_FIXED, fd, SYS_BIOS))
+ == MAP_FAILED) {
+ xf86DrvMsg(screen, X_ERROR, "Cannot map SYS BIOS\n");
+ close(fd);
+ goto error0;
+ }
+ }
+ if (!vidMem) {
+ DebugF("Mapping VRAM area\n");
+ if ((vidMem = mmap((void *)(V_RAM), VRAM_SIZE,
+ PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_SHARED | MAP_FIXED, fd, V_RAM))
+ == MAP_FAILED) {
+ xf86DrvMsg(screen, X_ERROR, "Cannot map V_RAM\n");
+ close(fd);
+ goto error0;
+ }
+ }
+ close(fd);
+ } else {
+ xf86DrvMsg(screen, X_ERROR, "Cannot open %s\n", DEV_MEM);
+ goto error0;
+ }
+ }
+
+ pInt = (xf86Int10InfoPtr)xnfcalloc(1, sizeof(xf86Int10InfoRec));
+ pInt->scrnIndex = screen;
+ pInt->entityIndex = entityIndex;
+ pInt->dev = xf86GetPciInfoForEntity(entityIndex);
+
+ if (!xf86Int10ExecSetup(pInt))
+ goto error0;
+ pInt->mem = &linuxMem;
+ pagesize = getpagesize();
+ pInt->private = (pointer)xnfcalloc(1, sizeof(linuxInt10Priv));
+ ((linuxInt10Priv*)pInt->private)->screen = screen;
+ ((linuxInt10Priv*)pInt->private)->alloc =
+ (pointer)xnfcalloc(1, ALLOC_ENTRIES(pagesize));
+
+ if (!xf86IsEntityPrimary(entityIndex)) {
+ DebugF("Mapping high memory area\n");
+ if ((high_mem = shmget(counter++, HIGH_MEM_SIZE,
+ IPC_CREAT | SHM_R | SHM_W)) == -1) {
+ if (errno == ENOSYS)
+ xf86DrvMsg(screen, X_ERROR, "shmget error\n Please reconfigure"
+ " your kernel to include System V IPC support\n");
+ else
+ xf86DrvMsg(screen, X_ERROR,
+ "shmget(highmem) error: %s\n",strerror(errno));
+ goto error1;
+ }
+ } else {
+ DebugF("Mapping Video BIOS\n");
+ videoBiosMapped = TRUE;
+ if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) {
+ if ((vMem = mmap((void *)(V_BIOS), SYS_BIOS - V_BIOS,
+ PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_SHARED | MAP_FIXED, fd, V_BIOS))
+ == MAP_FAILED) {
+ xf86DrvMsg(screen, X_ERROR, "Cannot map V_BIOS\n");
+ close(fd);
+ goto error1;
+ }
+ close (fd);
+ } else
+ goto error1;
+ }
+ ((linuxInt10Priv*)pInt->private)->highMem = high_mem;
+
+ DebugF("Mapping 640kB area\n");
+ if ((low_mem = shmget(counter++, V_RAM,
+ IPC_CREAT | SHM_R | SHM_W)) == -1) {
+ xf86DrvMsg(screen, X_ERROR,
+ "shmget(lowmem) error: %s\n",strerror(errno));
+ goto error2;
+ }
+
+ ((linuxInt10Priv*)pInt->private)->lowMem = low_mem;
+ base = shmat(low_mem, 0, 0);
+ if (base == SHMERRORPTR) {
+ xf86DrvMsg(screen, X_ERROR,
+ "shmat(low_mem) error: %s\n",strerror(errno));
+ goto error3;
+ }
+ ((linuxInt10Priv *)pInt->private)->base = base;
+ if (high_mem > -1) {
+ base_high = shmat(high_mem, 0, 0);
+ if (base_high == SHMERRORPTR) {
+ xf86DrvMsg(screen, X_ERROR,
+ "shmat(high_mem) error: %s\n",strerror(errno));
+ goto error3;
+ }
+ ((linuxInt10Priv*)pInt->private)->base_high = base_high;
+ } else
+ ((linuxInt10Priv*)pInt->private)->base_high = NULL;
+
+ if (!MapCurrentInt10(pInt))
+ goto error3;
+
+ Int10Current = pInt;
+
+ DebugF("Mapping int area\n");
+ if (xf86ReadBIOS(0, 0, (unsigned char *)0, LOW_PAGE_SIZE) < 0) {
+ xf86DrvMsg(screen, X_ERROR, "Cannot read int vect\n");
+ goto error3;
+ }
+ DebugF("done\n");
+ /*
+ * Read in everything between V_BIOS and SYS_BIOS as some system BIOSes
+ * have executable code there. Note that xf86ReadBIOS() can only bring in
+ * 64K bytes at a time.
+ */
+ if (!videoBiosMapped) {
+ memset((pointer)V_BIOS, 0, SYS_BIOS - V_BIOS);
+ DebugF("Reading BIOS\n");
+ for (cs = V_BIOS; cs < SYS_BIOS; cs += V_BIOS_SIZE)
+ if (xf86ReadBIOS(cs, 0, (pointer)cs, V_BIOS_SIZE) < V_BIOS_SIZE)
+ xf86DrvMsg(screen, X_WARNING,
+ "Unable to retrieve all of segment 0x%06lX.\n",
+ (long)cs);
+ DebugF("done\n");
+ }
+
+ if (xf86IsEntityPrimary(entityIndex) && !(initPrimary(options))) {
+ if (!xf86int10GetBiosSegment(pInt, NULL))
+ goto error3;
+
+ set_return_trap(pInt);
+#ifdef _PC
+ pInt->Flags = Flags & (SET_BIOS_SCRATCH | RESTORE_BIOS_SCRATCH);
+ if (! (pInt->Flags & SET_BIOS_SCRATCH))
+ pInt->Flags &= ~RESTORE_BIOS_SCRATCH;
+ xf86Int10SaveRestoreBIOSVars(pInt, TRUE);
+#endif
+ } else {
+ const BusType location_type = xf86int10GetBiosLocationType(pInt);
+
+ switch (location_type) {
+ case BUS_PCI: {
+ int err;
+ struct pci_device *rom_device =
+ xf86GetPciInfoForEntity(pInt->entityIndex);
+
+#if HAVE_PCI_DEVICE_ENABLE
+ pci_device_enable(rom_device);
+#endif
+
+ err = pci_device_read_rom(rom_device, (unsigned char *)(V_BIOS));
+ if (err) {
+ xf86DrvMsg(screen,X_ERROR,"Cannot read V_BIOS (%s)\n",
+ strerror(err));
+ goto error3;
+ }
+
+ pInt->BIOSseg = V_BIOS >> 4;
+ break;
+ }
+ default:
+ goto error3;
+ }
+
+ pInt->num = 0xe6;
+ reset_int_vect(pInt);
+ set_return_trap(pInt);
+ LockLegacyVGA(pInt, &vga);
+ xf86ExecX86int10(pInt);
+ UnlockLegacyVGA(pInt, &vga);
+ }
+#ifdef DEBUG
+ dprint(0xc0000, 0x20);
+#endif
+
+ free(options);
+ return pInt;
+
+error3:
+ if (base_high)
+ shmdt(base_high);
+ shmdt(base);
+ shmdt(0);
+ if (base_high)
+ shmdt((char*)HIGH_MEM);
+ shmctl(low_mem, IPC_RMID, NULL);
+ Int10Current = NULL;
+error2:
+ if (high_mem > -1)
+ shmctl(high_mem, IPC_RMID,NULL);
+error1:
+ if (vMem)
+ munmap(vMem, SYS_BIOS - V_BIOS);
+ free(((linuxInt10Priv*)pInt->private)->alloc);
+ free(pInt->private);
+error0:
+ free(options);
+ free(pInt);
+ return NULL;
+}
+
+Bool
+MapCurrentInt10(xf86Int10InfoPtr pInt)
+{
+ pointer addr;
+ int fd = -1;
+
+ if (Int10Current) {
+ shmdt(0);
+ if (((linuxInt10Priv*)Int10Current->private)->highMem >= 0)
+ shmdt((char*)HIGH_MEM);
+ else
+ munmap((pointer)V_BIOS, (SYS_BIOS - V_BIOS));
+ }
+ addr = shmat(((linuxInt10Priv*)pInt->private)->lowMem, (char*)1, SHM_RND);
+ if (addr == SHMERRORPTR) {
+ xf86DrvMsg(pInt->scrnIndex, X_ERROR, "Cannot shmat() low memory\n");
+ xf86DrvMsg(pInt->scrnIndex, X_ERROR,
+ "shmat(low_mem) error: %s\n",strerror(errno));
+ return FALSE;
+ }
+ if (mprotect((void*)0, V_RAM, PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
+ xf86DrvMsg(pInt->scrnIndex, X_ERROR,
+ "Cannot set EXEC bit on low memory: %s\n", strerror(errno));
+
+ if (((linuxInt10Priv*)pInt->private)->highMem >= 0) {
+ addr = shmat(((linuxInt10Priv*)pInt->private)->highMem,
+ (char*)HIGH_MEM, 0);
+ if (addr == SHMERRORPTR) {
+ xf86DrvMsg(pInt->scrnIndex, X_ERROR,
+ "Cannot shmat() high memory\n");
+ xf86DrvMsg(pInt->scrnIndex, X_ERROR,
+ "shmget error: %s\n",strerror(errno));
+ return FALSE;
+ }
+ if (mprotect((void*)HIGH_MEM, HIGH_MEM_SIZE,
+ PROT_READ|PROT_WRITE|PROT_EXEC) != 0)
+ xf86DrvMsg(pInt->scrnIndex, X_ERROR,
+ "Cannot set EXEC bit on high memory: %s\n",
+ strerror(errno));
+ } else {
+ if ((fd = open(DEV_MEM, O_RDWR, 0)) >= 0) {
+ if (mmap((void *)(V_BIOS), SYS_BIOS - V_BIOS,
+ PROT_READ | PROT_WRITE | PROT_EXEC,
+ MAP_SHARED | MAP_FIXED, fd, V_BIOS)
+ == MAP_FAILED) {
+ xf86DrvMsg(pInt->scrnIndex, X_ERROR, "Cannot map V_BIOS\n");
+ close (fd);
+ return FALSE;
+ }
+ } else {
+ xf86DrvMsg(pInt->scrnIndex, X_ERROR, "Cannot open %s\n",DEV_MEM);
+ return FALSE;
+ }
+ close (fd);
+ }
+
+ return TRUE;
+}
+
+void
+xf86FreeInt10(xf86Int10InfoPtr pInt)
+{
+ if (!pInt)
+ return;
+
+#ifdef _PC
+ xf86Int10SaveRestoreBIOSVars(pInt, FALSE);
+#endif
+ if (Int10Current == pInt) {
+ shmdt(0);
+ if (((linuxInt10Priv*)pInt->private)->highMem >= 0)
+ shmdt((char*)HIGH_MEM);
+ else
+ munmap((pointer)V_BIOS, (SYS_BIOS - V_BIOS));
+ Int10Current = NULL;
+ }
+
+ if (((linuxInt10Priv*)pInt->private)->base_high)
+ shmdt(((linuxInt10Priv*)pInt->private)->base_high);
+ shmdt(((linuxInt10Priv*)pInt->private)->base);
+ shmctl(((linuxInt10Priv*)pInt->private)->lowMem, IPC_RMID, NULL);
+ if (((linuxInt10Priv*)pInt->private)->highMem >= 0)
+ shmctl(((linuxInt10Priv*)pInt->private)->highMem, IPC_RMID, NULL);
+ free(((linuxInt10Priv*)pInt->private)->alloc);
+ free(pInt->private);
+ free(pInt);
+}
+
+void *
+xf86Int10AllocPages(xf86Int10InfoPtr pInt, int num, int *off)
+{
+ int pagesize = getpagesize();
+ int num_pages = ALLOC_ENTRIES(pagesize);
+ int i, j;
+
+ for (i = 0; i < (num_pages - num); i++) {
+ if (((linuxInt10Priv*)pInt->private)->alloc[i] == 0) {
+ for (j = i; j < (num + i); j++)
+ if ((((linuxInt10Priv*)pInt->private)->alloc[j] != 0))
+ break;
+ if (j == (num + i))
+ break;
+ else
+ i = i + num;
+ }
+ }
+ if (i == (num_pages - num))
+ return NULL;
+
+ for (j = i; j < (i + num); j++)
+ ((linuxInt10Priv*)pInt->private)->alloc[j] = 1;
+
+ *off = (i + 1) * pagesize;
+
+ return ((linuxInt10Priv*)pInt->private)->base + ((i + 1) * pagesize);
+}
+
+void
+xf86Int10FreePages(xf86Int10InfoPtr pInt, void *pbase, int num)
+{
+ int pagesize = getpagesize();
+ int first = (((unsigned long)pbase
+ - (unsigned long)((linuxInt10Priv*)pInt->private)->base)
+ / pagesize) - 1;
+ int i;
+
+ for (i = first; i < (first + num); i++)
+ ((linuxInt10Priv*)pInt->private)->alloc[i] = 0;
+}
+
+static CARD8
+read_b(xf86Int10InfoPtr pInt, int addr)
+{
+ return *((CARD8 *)(memType)addr);
+}
+
+static CARD16
+read_w(xf86Int10InfoPtr pInt, int addr)
+{
+ return *((CARD16 *)(memType)addr);
+}
+
+static CARD32
+read_l(xf86Int10InfoPtr pInt, int addr)
+{
+ return *((CARD32 *)(memType)addr);
+}
+
+static void
+write_b(xf86Int10InfoPtr pInt, int addr, CARD8 val)
+{
+ *((CARD8 *)(memType)addr) = val;
+}
+
+static void
+write_w(xf86Int10InfoPtr pInt, int addr, CARD16 val)
+{
+ *((CARD16 *)(memType)addr) = val;
+}
+
+static
+void write_l(xf86Int10InfoPtr pInt, int addr, CARD32 val)
+{
+ *((CARD32 *)(memType) addr) = val;
+}
+
+pointer
+xf86int10Addr(xf86Int10InfoPtr pInt, CARD32 addr)
+{
+ if (addr < V_RAM)
+ return ((linuxInt10Priv*)pInt->private)->base + addr;
+ else if (addr < V_BIOS)
+ return (pointer)(memType)addr;
+ else if (addr < SYS_BIOS) {
+ if (((linuxInt10Priv*)pInt->private)->base_high)
+ return (pointer)(((linuxInt10Priv*)pInt->private)->base_high
+ - V_BIOS + addr);
+ else
+ return (pointer) (memType)addr;
+ } else
+ return (pointer) (memType)addr;
+}
+
+#if defined DoSubModules
+
+static Bool
+vm86_tst(void)
+{
+ int __res;
+
+#ifdef __PIC__
+ /* When compiling with -fPIC, we can't use asm constraint "b" because
+ %ebx is already taken by gcc. */
+ __asm__ __volatile__("pushl %%ebx\n\t"
+ "movl %2,%%ebx\n\t"
+ "movl %1,%%eax\n\t"
+ "int $0x80\n\t"
+ "popl %%ebx"
+ :"=a" (__res)
+ :"n" ((int)113), "r" (NULL));
+#else
+ __asm__ __volatile__("int $0x80\n\t"
+ :"=a" (__res):"a" ((int)113),
+ "b" ((struct vm86_struct *)NULL));
+#endif
+
+ if (__res < 0 && __res == -ENOSYS)
+ return FALSE;
+
+ return TRUE;
+}
+
+static Int10LinuxSubModuleState
+int10LinuxLoadSubModule(ScrnInfoPtr pScrn)
+{
+ if (vm86_tst()) {
+ if (xf86LoadSubModule(pScrn,"vm86"))
+ return INT10_LOADED_VM86;
+ }
+ if (xf86LoadSubModule(pScrn,"x86emu"))
+ return INT10_LOADED_X86EMU;
+
+ return INT10_LOAD_FAILED;
+}
+
+#endif /* DoSubModules */
diff --git a/xorg-server/hw/xfree86/os-support/linux/lnx_agp.c b/xorg-server/hw/xfree86/os-support/linux/lnx_agp.c index 61437406d..3fa897366 100644 --- a/xorg-server/hw/xfree86/os-support/linux/lnx_agp.c +++ b/xorg-server/hw/xfree86/os-support/linux/lnx_agp.c @@ -1,374 +1,374 @@ -/* - * Abstraction of the AGP GART interface. - * - * This version is for Linux and Free/Open/NetBSD. - * - * Copyright © 2000 VA Linux Systems, Inc. - * Copyright © 2001 The XFree86 Project, Inc. - */ - - -#ifdef HAVE_XORG_CONFIG_H -#include <xorg-config.h> -#endif - -#include <X11/X.h> -#include "xf86.h" -#include "xf86Priv.h" -#include "xf86_OSlib.h" -#include "xf86OSpriv.h" - -#if defined(linux) -#include <asm/ioctl.h> -#include <linux/agpgart.h> -#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) -#include <sys/ioctl.h> -#include <sys/agpio.h> -#endif - -#ifndef AGP_DEVICE -#define AGP_DEVICE "/dev/agpgart" -#endif -/* AGP page size is independent of the host page size. */ -#ifndef AGP_PAGE_SIZE -#define AGP_PAGE_SIZE 4096 -#endif -#define AGPGART_MAJOR_VERSION 0 -#define AGPGART_MINOR_VERSION 99 - -static int gartFd = -1; -static int acquiredScreen = -1; -static Bool initDone = FALSE; -/* - * Close /dev/agpgart. This frees all associated memory allocated during - * this server generation. - */ -Bool -xf86GARTCloseScreen(int screenNum) -{ - if(gartFd != -1) { - close(gartFd); - acquiredScreen = -1; - gartFd = -1; - initDone = FALSE; - } - return TRUE; -} - -/* - * Open /dev/agpgart. Keep it open until xf86GARTCloseScreen is called. - */ -static Bool -GARTInit(int screenNum) -{ - struct _agp_info agpinf; - - if (initDone) - return (gartFd != -1); - - initDone = TRUE; - - if (gartFd == -1) - gartFd = open(AGP_DEVICE, O_RDWR, 0); - else - return FALSE; - - if (gartFd == -1) { - xf86DrvMsg(screenNum, X_ERROR, - "GARTInit: Unable to open " AGP_DEVICE " (%s)\n", - strerror(errno)); - return FALSE; - } - - xf86AcquireGART(-1); - /* Check the kernel driver version. */ - if (ioctl(gartFd, AGPIOC_INFO, &agpinf) != 0) { - xf86DrvMsg(screenNum, X_ERROR, - "GARTInit: AGPIOC_INFO failed (%s)\n", strerror(errno)); - close(gartFd); - gartFd = -1; - return FALSE; - } - xf86ReleaseGART(-1); - -#if defined(linux) - /* Per Dave Jones, every effort will be made to keep the - * agpgart interface backwards compatible, so allow all - * future versions. - */ - if ( -#if (AGPGART_MAJOR_VERSION > 0) /* quiet compiler */ - agpinf.version.major < AGPGART_MAJOR_VERSION || -#endif - (agpinf.version.major == AGPGART_MAJOR_VERSION && - agpinf.version.minor < AGPGART_MINOR_VERSION)) { - xf86DrvMsg(screenNum, X_ERROR, - "GARTInit: Kernel agpgart driver version is not current" - " (%d.%d vs %d.%d)\n", - agpinf.version.major, agpinf.version.minor, - AGPGART_MAJOR_VERSION, AGPGART_MINOR_VERSION); - close(gartFd); - gartFd = -1; - return FALSE; - } -#endif - - return TRUE; -} - -Bool -xf86AgpGARTSupported(void) -{ - return GARTInit(-1); -} - -AgpInfoPtr -xf86GetAGPInfo(int screenNum) -{ - struct _agp_info agpinf; - AgpInfoPtr info; - - if (!GARTInit(screenNum)) - return NULL; - - - if ((info = xcalloc(sizeof(AgpInfo), 1)) == NULL) { - xf86DrvMsg(screenNum, X_ERROR, - "xf86GetAGPInfo: Failed to allocate AgpInfo\n"); - return NULL; - } - - memset((char*)&agpinf, 0, sizeof(agpinf)); - - if (ioctl(gartFd, AGPIOC_INFO, &agpinf) != 0) { - xf86DrvMsg(screenNum, X_ERROR, - "xf86GetAGPInfo: AGPIOC_INFO failed (%s)\n", - strerror(errno)); - return NULL; - } - - info->bridgeId = agpinf.bridge_id; - info->agpMode = agpinf.agp_mode; - info->base = agpinf.aper_base; - info->size = agpinf.aper_size; - info->totalPages = agpinf.pg_total; - info->systemPages = agpinf.pg_system; - info->usedPages = agpinf.pg_used; - - xf86DrvMsg(screenNum, X_INFO, "Kernel reported %zu total, %zu used\n", agpinf.pg_total, agpinf.pg_used); - - return info; -} - -/* - * XXX If multiple screens can acquire the GART, should we have a reference - * count instead of using acquiredScreen? - */ - -Bool -xf86AcquireGART(int screenNum) -{ - if (screenNum != -1 && !GARTInit(screenNum)) - return FALSE; - - if (screenNum == -1 || acquiredScreen != screenNum) { - if (ioctl(gartFd, AGPIOC_ACQUIRE, 0) != 0) { - xf86DrvMsg(screenNum, X_WARNING, - "xf86AcquireGART: AGPIOC_ACQUIRE failed (%s)\n", - strerror(errno)); - return FALSE; - } - acquiredScreen = screenNum; - } - return TRUE; -} - -Bool -xf86ReleaseGART(int screenNum) -{ - if (screenNum != -1 && !GARTInit(screenNum)) - return FALSE; - - if (acquiredScreen == screenNum) { - /* - * The FreeBSD agp driver removes allocations on release. - * The Linux driver doesn't. xf86ReleaseGART() is expected - * to give up access to the GART, but not to remove any - * allocations. - */ -#if !defined(linux) - if (screenNum == -1) -#endif - { - if (ioctl(gartFd, AGPIOC_RELEASE, 0) != 0) { - xf86DrvMsg(screenNum, X_WARNING, - "xf86ReleaseGART: AGPIOC_RELEASE failed (%s)\n", - strerror(errno)); - return FALSE; - } - acquiredScreen = -1; - } - return TRUE; - } - return FALSE; -} - -int -xf86AllocateGARTMemory(int screenNum, unsigned long size, int type, - unsigned long *physical) -{ - struct _agp_allocate alloc; - int pages; - - /* - * Allocates "size" bytes of GART memory (rounds up to the next - * page multiple) or type "type". A handle (key) for the allocated - * memory is returned. On error, the return value is -1. - */ - - if (!GARTInit(screenNum) || acquiredScreen != screenNum) - return -1; - - pages = (size / AGP_PAGE_SIZE); - if (size % AGP_PAGE_SIZE != 0) - pages++; - - /* XXX check for pages == 0? */ - - alloc.pg_count = pages; - alloc.type = type; - - if (ioctl(gartFd, AGPIOC_ALLOCATE, &alloc) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86AllocateGARTMemory: " - "allocation of %d pages failed\n\t(%s)\n", pages, - strerror(errno)); - return -1; - } - - if (physical) - *physical = alloc.physical; - - return alloc.key; -} - -Bool -xf86DeallocateGARTMemory(int screenNum, int key) -{ - if (!GARTInit(screenNum) || acquiredScreen != screenNum) - return FALSE; - - if (acquiredScreen != screenNum) { - xf86DrvMsg(screenNum, X_ERROR, - "xf86UnbindGARTMemory: AGP not acquired by this screen\n"); - return FALSE; - } - -#ifdef __linux__ - if (ioctl(gartFd, AGPIOC_DEALLOCATE, (int *)key) != 0) { -#else - if (ioctl(gartFd, AGPIOC_DEALLOCATE, &key) != 0) { -#endif - xf86DrvMsg(screenNum, X_WARNING,"xf86DeAllocateGARTMemory: " - "deallocation gart memory with key %d failed\n\t(%s)\n", - key, strerror(errno)); - return FALSE; - } - - return TRUE; -} - -/* Bind GART memory with "key" at "offset" */ -Bool -xf86BindGARTMemory(int screenNum, int key, unsigned long offset) -{ - struct _agp_bind bind; - int pageOffset; - - if (!GARTInit(screenNum) || acquiredScreen != screenNum) - return FALSE; - - if (acquiredScreen != screenNum) { - xf86DrvMsg(screenNum, X_ERROR, - "xf86BindGARTMemory: AGP not acquired by this screen\n"); - return FALSE; - } - - if (offset % AGP_PAGE_SIZE != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86BindGARTMemory: " - "offset (0x%lx) is not page-aligned (%d)\n", - offset, AGP_PAGE_SIZE); - return FALSE; - } - pageOffset = offset / AGP_PAGE_SIZE; - - xf86DrvMsgVerb(screenNum, X_INFO, 3, - "xf86BindGARTMemory: bind key %d at 0x%08lx " - "(pgoffset %d)\n", key, offset, pageOffset); - - bind.pg_start = pageOffset; - bind.key = key; - - if (ioctl(gartFd, AGPIOC_BIND, &bind) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86BindGARTMemory: " - "binding of gart memory with key %d\n" - "\tat offset 0x%lx failed (%s)\n", - key, offset, strerror(errno)); - return FALSE; - } - - return TRUE; -} - - -/* Unbind GART memory with "key" */ -Bool -xf86UnbindGARTMemory(int screenNum, int key) -{ - struct _agp_unbind unbind; - - if (!GARTInit(screenNum) || acquiredScreen != screenNum) - return FALSE; - - if (acquiredScreen != screenNum) { - xf86DrvMsg(screenNum, X_ERROR, - "xf86UnbindGARTMemory: AGP not acquired by this screen\n"); - return FALSE; - } - - unbind.priority = 0; - unbind.key = key; - - if (ioctl(gartFd, AGPIOC_UNBIND, &unbind) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86UnbindGARTMemory: " - "unbinding of gart memory with key %d " - "failed (%s)\n", key, strerror(errno)); - return FALSE; - } - - xf86DrvMsgVerb(screenNum, X_INFO, 3, - "xf86UnbindGARTMemory: unbind key %d\n", key); - - return TRUE; -} - - -/* XXX Interface may change. */ -Bool -xf86EnableAGP(int screenNum, CARD32 mode) -{ - agp_setup setup; - - if (!GARTInit(screenNum) || acquiredScreen != screenNum) - return FALSE; - - setup.agp_mode = mode; - if (ioctl(gartFd, AGPIOC_SETUP, &setup) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86EnableAGP: " - "AGPIOC_SETUP with mode %ld failed (%s)\n", - (unsigned long)mode, strerror(errno)); - return FALSE; - } - - return TRUE; -} - +/*
+ * Abstraction of the AGP GART interface.
+ *
+ * This version is for Linux and Free/Open/NetBSD.
+ *
+ * Copyright © 2000 VA Linux Systems, Inc.
+ * Copyright © 2001 The XFree86 Project, Inc.
+ */
+
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include <X11/X.h>
+#include "xf86.h"
+#include "xf86Priv.h"
+#include "xf86_OSlib.h"
+#include "xf86OSpriv.h"
+
+#if defined(linux)
+#include <asm/ioctl.h>
+#include <linux/agpgart.h>
+#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
+#include <sys/ioctl.h>
+#include <sys/agpio.h>
+#endif
+
+#ifndef AGP_DEVICE
+#define AGP_DEVICE "/dev/agpgart"
+#endif
+/* AGP page size is independent of the host page size. */
+#ifndef AGP_PAGE_SIZE
+#define AGP_PAGE_SIZE 4096
+#endif
+#define AGPGART_MAJOR_VERSION 0
+#define AGPGART_MINOR_VERSION 99
+
+static int gartFd = -1;
+static int acquiredScreen = -1;
+static Bool initDone = FALSE;
+/*
+ * Close /dev/agpgart. This frees all associated memory allocated during
+ * this server generation.
+ */
+Bool
+xf86GARTCloseScreen(int screenNum)
+{
+ if(gartFd != -1) {
+ close(gartFd);
+ acquiredScreen = -1;
+ gartFd = -1;
+ initDone = FALSE;
+ }
+ return TRUE;
+}
+
+/*
+ * Open /dev/agpgart. Keep it open until xf86GARTCloseScreen is called.
+ */
+static Bool
+GARTInit(int screenNum)
+{
+ struct _agp_info agpinf;
+
+ if (initDone)
+ return (gartFd != -1);
+
+ initDone = TRUE;
+
+ if (gartFd == -1)
+ gartFd = open(AGP_DEVICE, O_RDWR, 0);
+ else
+ return FALSE;
+
+ if (gartFd == -1) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "GARTInit: Unable to open " AGP_DEVICE " (%s)\n",
+ strerror(errno));
+ return FALSE;
+ }
+
+ xf86AcquireGART(-1);
+ /* Check the kernel driver version. */
+ if (ioctl(gartFd, AGPIOC_INFO, &agpinf) != 0) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "GARTInit: AGPIOC_INFO failed (%s)\n", strerror(errno));
+ close(gartFd);
+ gartFd = -1;
+ return FALSE;
+ }
+ xf86ReleaseGART(-1);
+
+#if defined(linux)
+ /* Per Dave Jones, every effort will be made to keep the
+ * agpgart interface backwards compatible, so allow all
+ * future versions.
+ */
+ if (
+#if (AGPGART_MAJOR_VERSION > 0) /* quiet compiler */
+ agpinf.version.major < AGPGART_MAJOR_VERSION ||
+#endif
+ (agpinf.version.major == AGPGART_MAJOR_VERSION &&
+ agpinf.version.minor < AGPGART_MINOR_VERSION)) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "GARTInit: Kernel agpgart driver version is not current"
+ " (%d.%d vs %d.%d)\n",
+ agpinf.version.major, agpinf.version.minor,
+ AGPGART_MAJOR_VERSION, AGPGART_MINOR_VERSION);
+ close(gartFd);
+ gartFd = -1;
+ return FALSE;
+ }
+#endif
+
+ return TRUE;
+}
+
+Bool
+xf86AgpGARTSupported(void)
+{
+ return GARTInit(-1);
+}
+
+AgpInfoPtr
+xf86GetAGPInfo(int screenNum)
+{
+ struct _agp_info agpinf;
+ AgpInfoPtr info;
+
+ if (!GARTInit(screenNum))
+ return NULL;
+
+
+ if ((info = calloc(sizeof(AgpInfo), 1)) == NULL) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "xf86GetAGPInfo: Failed to allocate AgpInfo\n");
+ return NULL;
+ }
+
+ memset((char*)&agpinf, 0, sizeof(agpinf));
+
+ if (ioctl(gartFd, AGPIOC_INFO, &agpinf) != 0) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "xf86GetAGPInfo: AGPIOC_INFO failed (%s)\n",
+ strerror(errno));
+ return NULL;
+ }
+
+ info->bridgeId = agpinf.bridge_id;
+ info->agpMode = agpinf.agp_mode;
+ info->base = agpinf.aper_base;
+ info->size = agpinf.aper_size;
+ info->totalPages = agpinf.pg_total;
+ info->systemPages = agpinf.pg_system;
+ info->usedPages = agpinf.pg_used;
+
+ xf86DrvMsg(screenNum, X_INFO, "Kernel reported %zu total, %zu used\n", agpinf.pg_total, agpinf.pg_used);
+
+ return info;
+}
+
+/*
+ * XXX If multiple screens can acquire the GART, should we have a reference
+ * count instead of using acquiredScreen?
+ */
+
+Bool
+xf86AcquireGART(int screenNum)
+{
+ if (screenNum != -1 && !GARTInit(screenNum))
+ return FALSE;
+
+ if (screenNum == -1 || acquiredScreen != screenNum) {
+ if (ioctl(gartFd, AGPIOC_ACQUIRE, 0) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING,
+ "xf86AcquireGART: AGPIOC_ACQUIRE failed (%s)\n",
+ strerror(errno));
+ return FALSE;
+ }
+ acquiredScreen = screenNum;
+ }
+ return TRUE;
+}
+
+Bool
+xf86ReleaseGART(int screenNum)
+{
+ if (screenNum != -1 && !GARTInit(screenNum))
+ return FALSE;
+
+ if (acquiredScreen == screenNum) {
+ /*
+ * The FreeBSD agp driver removes allocations on release.
+ * The Linux driver doesn't. xf86ReleaseGART() is expected
+ * to give up access to the GART, but not to remove any
+ * allocations.
+ */
+#if !defined(linux)
+ if (screenNum == -1)
+#endif
+ {
+ if (ioctl(gartFd, AGPIOC_RELEASE, 0) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING,
+ "xf86ReleaseGART: AGPIOC_RELEASE failed (%s)\n",
+ strerror(errno));
+ return FALSE;
+ }
+ acquiredScreen = -1;
+ }
+ return TRUE;
+ }
+ return FALSE;
+}
+
+int
+xf86AllocateGARTMemory(int screenNum, unsigned long size, int type,
+ unsigned long *physical)
+{
+ struct _agp_allocate alloc;
+ int pages;
+
+ /*
+ * Allocates "size" bytes of GART memory (rounds up to the next
+ * page multiple) or type "type". A handle (key) for the allocated
+ * memory is returned. On error, the return value is -1.
+ */
+
+ if (!GARTInit(screenNum) || acquiredScreen != screenNum)
+ return -1;
+
+ pages = (size / AGP_PAGE_SIZE);
+ if (size % AGP_PAGE_SIZE != 0)
+ pages++;
+
+ /* XXX check for pages == 0? */
+
+ alloc.pg_count = pages;
+ alloc.type = type;
+
+ if (ioctl(gartFd, AGPIOC_ALLOCATE, &alloc) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86AllocateGARTMemory: "
+ "allocation of %d pages failed\n\t(%s)\n", pages,
+ strerror(errno));
+ return -1;
+ }
+
+ if (physical)
+ *physical = alloc.physical;
+
+ return alloc.key;
+}
+
+Bool
+xf86DeallocateGARTMemory(int screenNum, int key)
+{
+ if (!GARTInit(screenNum) || acquiredScreen != screenNum)
+ return FALSE;
+
+ if (acquiredScreen != screenNum) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "xf86UnbindGARTMemory: AGP not acquired by this screen\n");
+ return FALSE;
+ }
+
+#ifdef __linux__
+ if (ioctl(gartFd, AGPIOC_DEALLOCATE, (int *)key) != 0) {
+#else
+ if (ioctl(gartFd, AGPIOC_DEALLOCATE, &key) != 0) {
+#endif
+ xf86DrvMsg(screenNum, X_WARNING,"xf86DeAllocateGARTMemory: "
+ "deallocation gart memory with key %d failed\n\t(%s)\n",
+ key, strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* Bind GART memory with "key" at "offset" */
+Bool
+xf86BindGARTMemory(int screenNum, int key, unsigned long offset)
+{
+ struct _agp_bind bind;
+ int pageOffset;
+
+ if (!GARTInit(screenNum) || acquiredScreen != screenNum)
+ return FALSE;
+
+ if (acquiredScreen != screenNum) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "xf86BindGARTMemory: AGP not acquired by this screen\n");
+ return FALSE;
+ }
+
+ if (offset % AGP_PAGE_SIZE != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86BindGARTMemory: "
+ "offset (0x%lx) is not page-aligned (%d)\n",
+ offset, AGP_PAGE_SIZE);
+ return FALSE;
+ }
+ pageOffset = offset / AGP_PAGE_SIZE;
+
+ xf86DrvMsgVerb(screenNum, X_INFO, 3,
+ "xf86BindGARTMemory: bind key %d at 0x%08lx "
+ "(pgoffset %d)\n", key, offset, pageOffset);
+
+ bind.pg_start = pageOffset;
+ bind.key = key;
+
+ if (ioctl(gartFd, AGPIOC_BIND, &bind) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86BindGARTMemory: "
+ "binding of gart memory with key %d\n"
+ "\tat offset 0x%lx failed (%s)\n",
+ key, offset, strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+
+/* Unbind GART memory with "key" */
+Bool
+xf86UnbindGARTMemory(int screenNum, int key)
+{
+ struct _agp_unbind unbind;
+
+ if (!GARTInit(screenNum) || acquiredScreen != screenNum)
+ return FALSE;
+
+ if (acquiredScreen != screenNum) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "xf86UnbindGARTMemory: AGP not acquired by this screen\n");
+ return FALSE;
+ }
+
+ unbind.priority = 0;
+ unbind.key = key;
+
+ if (ioctl(gartFd, AGPIOC_UNBIND, &unbind) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86UnbindGARTMemory: "
+ "unbinding of gart memory with key %d "
+ "failed (%s)\n", key, strerror(errno));
+ return FALSE;
+ }
+
+ xf86DrvMsgVerb(screenNum, X_INFO, 3,
+ "xf86UnbindGARTMemory: unbind key %d\n", key);
+
+ return TRUE;
+}
+
+
+/* XXX Interface may change. */
+Bool
+xf86EnableAGP(int screenNum, CARD32 mode)
+{
+ agp_setup setup;
+
+ if (!GARTInit(screenNum) || acquiredScreen != screenNum)
+ return FALSE;
+
+ setup.agp_mode = mode;
+ if (ioctl(gartFd, AGPIOC_SETUP, &setup) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86EnableAGP: "
+ "AGPIOC_SETUP with mode %ld failed (%s)\n",
+ (unsigned long)mode, strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
diff --git a/xorg-server/hw/xfree86/os-support/linux/lnx_video.c b/xorg-server/hw/xfree86/os-support/linux/lnx_video.c index 26a17425a..a515b4080 100644 --- a/xorg-server/hw/xfree86/os-support/linux/lnx_video.c +++ b/xorg-server/hw/xfree86/os-support/linux/lnx_video.c @@ -1,878 +1,878 @@ -/* - * Copyright 1992 by Orest Zborowski <obz@Kodak.com> - * Copyright 1993 by David Wexelblat <dwex@goblin.org> - * - * Permission to use, copy, modify, distribute, and sell this software and its - * documentation for any purpose is hereby granted without fee, provided that - * the above copyright notice appear in all copies and that both that - * copyright notice and this permission notice appear in supporting - * documentation, and that the names of Orest Zborowski and David Wexelblat - * not be used in advertising or publicity pertaining to distribution of - * the software without specific, written prior permission. Orest Zborowski - * and David Wexelblat make no representations about the suitability of this - * software for any purpose. It is provided "as is" without express or - * implied warranty. - * - * OREST ZBOROWSKI AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD - * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND - * FITNESS, IN NO EVENT SHALL OREST ZBOROWSKI OR DAVID WEXELBLAT BE LIABLE - * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - */ - -#ifdef HAVE_XORG_CONFIG_H -#include <xorg-config.h> -#endif - -#include <errno.h> -#include <string.h> - -#include <X11/X.h> -#include "input.h" -#include "scrnintstr.h" - -#include "xf86.h" -#include "xf86Priv.h" -#include "xf86_OSlib.h" -#include "xf86OSpriv.h" -#ifdef __alpha__ -#include "shared/xf86Axp.h" -#endif - -#ifdef HAS_MTRR_SUPPORT -#include <asm/mtrr.h> -#endif - -#ifndef MAP_FAILED -#define MAP_FAILED ((void *)-1) -#endif - -static Bool ExtendedEnabled = FALSE; - -#ifdef __ia64__ - -#include "compiler.h" -#include <sys/io.h> - -#elif !defined(__powerpc__) && \ - !defined(__mc68000__) && \ - !defined(__sparc__) && \ - !defined(__mips__) && \ - !defined(__arm__) - -/* - * Due to conflicts with "compiler.h", don't rely on <sys/io.h> to declare - * these. - */ -extern int ioperm(unsigned long __from, unsigned long __num, int __turn_on); -extern int iopl(int __level); - -#endif - -#ifdef __alpha__ -# define BUS_BASE bus_base -#else -#define BUS_BASE (0) -#endif /* __alpha__ */ - -/***************************************************************************/ -/* Video Memory Mapping section */ -/***************************************************************************/ - -static pointer mapVidMem(int, unsigned long, unsigned long, int); -static void unmapVidMem(int, pointer, unsigned long); -#if defined (__alpha__) -extern void sethae(unsigned long hae); -extern unsigned long _bus_base __P ((void)) __attribute__ ((const)); -extern unsigned long _bus_base_sparse __P ((void)) __attribute__ ((const)); - -static pointer mapVidMemSparse(int, unsigned long, unsigned long, int); -extern axpDevice lnxGetAXP(void); -static void unmapVidMemSparse(int, pointer, unsigned long); -static axpDevice axpSystem = -1; -static Bool needSparse; -static unsigned long hae_thresh; -static unsigned long hae_mask; -static unsigned long bus_base; -#endif - -#ifdef HAS_MTRR_SUPPORT - -#define SPLIT_WC_REGIONS 1 - -static pointer setWC(int, unsigned long, unsigned long, Bool, MessageType); -static void undoWC(int, pointer); - -/* The file desc for /proc/mtrr. Once opened, left opened, and the mtrr - driver will clean up when we exit. */ -#define MTRR_FD_UNOPENED (-1) /* We have yet to open /proc/mtrr */ -#define MTRR_FD_PROBLEM (-2) /* We tried to open /proc/mtrr, but had - a problem. */ -static int mtrr_fd = MTRR_FD_UNOPENED; - -/* Open /proc/mtrr. FALSE on failure. Will always fail on Linux 2.0, - and will fail on Linux 2.2 with MTRR support configured out, - so verbosity should be chosen appropriately. */ -static Bool -mtrr_open(int verbosity) -{ - /* Only report absence of /proc/mtrr once. */ - static Bool warned = FALSE; - - if (mtrr_fd == MTRR_FD_UNOPENED) { - mtrr_fd = open("/proc/mtrr", O_WRONLY); - - if (mtrr_fd < 0) - mtrr_fd = MTRR_FD_PROBLEM; - } - - if (mtrr_fd == MTRR_FD_PROBLEM) { - /* To make sure we only ever warn once, need to check - verbosity outside xf86MsgVerb */ - if (!warned && verbosity <= xf86GetVerbosity()) { - xf86MsgVerb(X_WARNING, verbosity, - "System lacks support for changing MTRRs\n"); - warned = TRUE; - } - - return FALSE; - } - else - return TRUE; -} - -/* - * We maintain a list of WC regions for each physical mapping so they can - * be undone when unmapping. - */ - -struct mtrr_wc_region { - struct mtrr_sentry sentry; - Bool added; /* added WC or removed it */ - struct mtrr_wc_region * next; -}; - - -static struct mtrr_wc_region * -mtrr_cull_wc_region(int screenNum, unsigned long base, unsigned long size, - MessageType from) -{ - /* Some BIOS writers thought that setting wc over the mmio - region of a graphics devices was a good idea. Try to fix - it. */ - - struct mtrr_gentry gent; - struct mtrr_wc_region *wcreturn = NULL, *wcr; - int count, ret=0; - - /* Linux 2.0 users should not get a warning without -verbose */ - if (!mtrr_open(2)) - return NULL; - - for (gent.regnum = 0; - ioctl(mtrr_fd, MTRRIOC_GET_ENTRY, &gent) >= 0; - gent.regnum++) { - if (gent.type != MTRR_TYPE_WRCOMB - || gent.base + gent.size <= base - || base + size <= gent.base) - continue; - - /* Found an overlapping region. Delete it. */ - - wcr = xalloc(sizeof(*wcr)); - if (!wcr) - return NULL; - wcr->sentry.base = gent.base; - wcr->sentry.size = gent.size; - wcr->sentry.type = MTRR_TYPE_WRCOMB; - wcr->added = FALSE; - - count = 3; - while (count-- && - (ret = ioctl(mtrr_fd, MTRRIOC_KILL_ENTRY, &(wcr->sentry))) < 0); - - if (ret >= 0) { - xf86DrvMsg(screenNum, from, - "Removed MMIO write-combining range " - "(0x%lx,0x%lx)\n", - (unsigned long) gent.base, (unsigned long) gent.size); - wcr->next = wcreturn; - wcreturn = wcr; - gent.regnum--; - } else { - xfree(wcr); - xf86DrvMsgVerb(screenNum, X_WARNING, 0, - "Failed to remove MMIO " - "write-combining range (0x%lx,0x%lx)\n", - gent.base, (unsigned long) gent.size); - } - } - return wcreturn; -} - - -static struct mtrr_wc_region * -mtrr_remove_offending(int screenNum, unsigned long base, unsigned long size, - MessageType from) -{ - struct mtrr_gentry gent; - struct mtrr_wc_region *wcreturn = NULL, **wcr; - - if (!mtrr_open(2)) - return NULL; - - wcr = &wcreturn; - for (gent.regnum = 0; - ioctl(mtrr_fd, MTRRIOC_GET_ENTRY, &gent) >= 0; gent.regnum++ ) { - if (gent.type == MTRR_TYPE_WRCOMB - && ((gent.base >= base && gent.base + gent.size < base + size) || - (gent.base > base && gent.base + gent.size <= base + size))) { - *wcr = mtrr_cull_wc_region(screenNum, gent.base, gent.size, from); - if (*wcr) gent.regnum--; - while(*wcr) { - wcr = &((*wcr)->next); - } - } - } - return wcreturn; -} - - -static struct mtrr_wc_region * -mtrr_add_wc_region(int screenNum, unsigned long base, unsigned long size, - MessageType from) -{ - struct mtrr_wc_region **wcr, *wcreturn, *curwcr; - - /* - * There can be only one.... - */ - - wcreturn = mtrr_remove_offending(screenNum, base, size, from); - wcr = &wcreturn; - while (*wcr) { - wcr = &((*wcr)->next); - } - - /* Linux 2.0 should not warn, unless the user explicitly asks for - WC. */ - - if (!mtrr_open(from == X_CONFIG ? 0 : 2)) - return wcreturn; - - *wcr = curwcr = xalloc(sizeof(**wcr)); - if (!curwcr) - return wcreturn; - - curwcr->sentry.base = base; - curwcr->sentry.size = size; - curwcr->sentry.type = MTRR_TYPE_WRCOMB; - curwcr->added = TRUE; - curwcr->next = NULL; - -#if SPLIT_WC_REGIONS - /* - * Splits up the write-combining region if it is not aligned on a - * size boundary. - */ - - { - unsigned long lbase, d_size = 1; - unsigned long n_size = size; - unsigned long n_base = base; - - for (lbase = n_base, d_size = 1; !(lbase & 1); - lbase = lbase >> 1, d_size <<= 1); - while (d_size > n_size) - d_size = d_size >> 1; - DebugF("WC_BASE: 0x%lx WC_END: 0x%lx\n",base,base+d_size-1); - n_base += d_size; - n_size -= d_size; - if (n_size) { - xf86DrvMsgVerb(screenNum,X_INFO,3,"Splitting WC range: " - "base: 0x%lx, size: 0x%lx\n",base,size); - curwcr->next = mtrr_add_wc_region(screenNum, n_base, n_size,from); - } - curwcr->sentry.size = d_size; - } - - /*****************************************************************/ -#endif /* SPLIT_WC_REGIONS */ - - if (ioctl(mtrr_fd, MTRRIOC_ADD_ENTRY, &curwcr->sentry) >= 0) { - /* Avoid printing on every VT switch */ - if (xf86ServerIsInitialising()) { - xf86DrvMsg(screenNum, from, - "Write-combining range (0x%lx,0x%lx)\n", - base, size); - } - return wcreturn; - } - else { - *wcr = curwcr->next; - xfree(curwcr); - - /* Don't complain about the VGA region: MTRR fixed - regions aren't currently supported, but might be in - the future. */ - if ((unsigned long)base >= 0x100000) { - xf86DrvMsgVerb(screenNum, X_WARNING, 0, - "Failed to set up write-combining range " - "(0x%lx,0x%lx)\n", base, size); - } - return wcreturn; - } -} - -static void -mtrr_undo_wc_region(int screenNum, struct mtrr_wc_region *wcr) -{ - struct mtrr_wc_region *p, *prev; - - if (mtrr_fd >= 0) { - p = wcr; - while (p) { - if (p->added) - ioctl(mtrr_fd, MTRRIOC_DEL_ENTRY, &p->sentry); - prev = p; - p = p->next; - xfree(prev); - } - } -} - -static pointer -setWC(int screenNum, unsigned long base, unsigned long size, Bool enable, - MessageType from) -{ - if (enable) - return mtrr_add_wc_region(screenNum, base, size, from); - else - return mtrr_cull_wc_region(screenNum, base, size, from); -} - -static void -undoWC(int screenNum, pointer regioninfo) -{ - mtrr_undo_wc_region(screenNum, regioninfo); -} - -#endif /* HAS_MTRR_SUPPORT */ - -void -xf86OSInitVidMem(VidMemInfoPtr pVidMem) -{ - pVidMem->linearSupported = TRUE; -#ifdef __alpha__ - if (axpSystem == -1) { - axpSystem = lnxGetAXP(); - if ((needSparse = (_bus_base_sparse() > 0))) { - hae_thresh = xf86AXPParams[axpSystem].hae_thresh; - hae_mask = xf86AXPParams[axpSystem].hae_mask; - } - bus_base = _bus_base(); - } - if (needSparse) { - xf86Msg(X_INFO,"Machine needs sparse mapping\n"); - pVidMem->mapMem = mapVidMemSparse; - pVidMem->unmapMem = unmapVidMemSparse; - } else { - xf86Msg(X_INFO,"Machine type has 8/16 bit access\n"); - pVidMem->mapMem = mapVidMem; - pVidMem->unmapMem = unmapVidMem; - } -#else - pVidMem->mapMem = mapVidMem; - pVidMem->unmapMem = unmapVidMem; -#endif /* __alpha__ */ - - -#ifdef HAS_MTRR_SUPPORT - pVidMem->setWC = setWC; - pVidMem->undoWC = undoWC; -#endif - pVidMem->initialised = TRUE; -} - -#ifdef __sparc__ -/* Basically, you simply cannot do this on Sparc. You have to do something portable - * like use /dev/fb* or mmap() on /proc/bus/pci/X/Y nodes. -DaveM - */ -static pointer mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags) -{ - return NULL; -} -#else -static pointer -mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags) -{ - pointer base; - int fd; - int mapflags = MAP_SHARED; - int prot; - memType realBase, alignOff; - - realBase = Base & ~(getpagesize() - 1); - alignOff = Base - realBase; - DebugF("base: %lx, realBase: %lx, alignOff: %lx \n", - Base,realBase,alignOff); - -#if defined(__ia64__) || defined(__arm__) || defined(__s390__) -#ifndef MAP_WRITECOMBINED -#define MAP_WRITECOMBINED 0x00010000 -#endif -#ifndef MAP_NONCACHED -#define MAP_NONCACHED 0x00020000 -#endif - if(flags & VIDMEM_FRAMEBUFFER) - mapflags |= MAP_WRITECOMBINED; - else - mapflags |= MAP_NONCACHED; -#endif - -#if 0 - /* this will disappear when people upgrade their kernels */ - fd = open(DEV_MEM, - ((flags & VIDMEM_READONLY) ? O_RDONLY : O_RDWR) | O_SYNC); -#else - fd = open(DEV_MEM, (flags & VIDMEM_READONLY) ? O_RDONLY : O_RDWR); -#endif - if (fd < 0) - { - FatalError("xf86MapVidMem: failed to open " DEV_MEM " (%s)\n", - strerror(errno)); - } - - if (flags & VIDMEM_READONLY) - prot = PROT_READ; - else - prot = PROT_READ | PROT_WRITE; - - /* This requires linux-0.99.pl10 or above */ - base = mmap((caddr_t)0, Size + alignOff, prot, mapflags, fd, - (off_t)realBase + BUS_BASE); - close(fd); - if (base == MAP_FAILED) { - FatalError("xf86MapVidMem: Could not mmap framebuffer" - " (0x%08lx,0x%lx) (%s)\n", Base, Size, - strerror(errno)); - } - DebugF("base: %lx aligned base: %lx\n",base, base + alignOff); - return (char *)base + alignOff; -} -#endif /* !(__sparc__) */ - -static void -unmapVidMem(int ScreenNum, pointer Base, unsigned long Size) -{ - memType alignOff = (memType)Base - - ((memType)Base & ~(getpagesize() - 1)); - - DebugF("alignment offset: %lx\n",alignOff); - munmap((caddr_t)((memType)Base - alignOff), (Size + alignOff)); -} - - -/***************************************************************************/ -/* I/O Permissions section */ -/***************************************************************************/ - -#if defined(__powerpc__) -volatile unsigned char *ioBase = NULL; - -#ifndef __NR_pciconfig_iobase -#define __NR_pciconfig_iobase 200 -#endif - -#endif - -Bool -xf86EnableIO(void) -{ -#if defined(__powerpc__) - int fd; - unsigned int ioBase_phys; -#endif - - if (ExtendedEnabled) - return TRUE; - -#if defined(__powerpc__) - ioBase_phys = syscall(__NR_pciconfig_iobase, 2, 0, 0); - - fd = open("/dev/mem", O_RDWR); - if (ioBase == NULL) { - ioBase = (volatile unsigned char *)mmap(0, 0x20000, - PROT_READ | PROT_WRITE, MAP_SHARED, fd, - ioBase_phys); -/* Should this be fatal or just a warning? */ -#if 0 - if (ioBase == MAP_FAILED) { - xf86Msg(X_WARNING, - "xf86EnableIOPorts: Failed to map iobase (%s)\n", - strerror(errno)); - return FALSE; - } -#endif - } - close(fd); -#elif !defined(__mc68000__) && !defined(__sparc__) && !defined(__mips__) && !defined(__sh__) && !defined(__hppa__) && !defined(__s390__) && !defined(__arm__) && !defined(__m32r__) - if (ioperm(0, 1024, 1) || iopl(3)) { - if (errno == ENODEV) - ErrorF("xf86EnableIOPorts: no I/O ports found\n"); - else - FatalError("xf86EnableIOPorts: failed to set IOPL" - " for I/O (%s)\n", strerror(errno)); - return FALSE; - } -# if !defined(__alpha__) - ioperm(0x40,4,0); /* trap access to the timer chip */ - ioperm(0x60,4,0); /* trap access to the keyboard controller */ -# endif -#endif - ExtendedEnabled = TRUE; - - return TRUE; -} - -void -xf86DisableIO(void) -{ - if (!ExtendedEnabled) - return; -#if defined(__powerpc__) - munmap(ioBase, 0x20000); - ioBase = NULL; -#elif !defined(__mc68000__) && !defined(__sparc__) && !defined(__mips__) && !defined(__sh__) && !defined(__hppa__) && !defined(__arm__) && !defined(__s390__) && !defined(__m32r__) - iopl(0); - ioperm(0, 1024, 0); -#endif - ExtendedEnabled = FALSE; - - return; -} - -#if defined (__alpha__) - -#define vuip volatile unsigned int * - -extern int readDense8(pointer Base, register unsigned long Offset); -extern int readDense16(pointer Base, register unsigned long Offset); -extern int readDense32(pointer Base, register unsigned long Offset); -extern void -writeDenseNB8(int Value, pointer Base, register unsigned long Offset); -extern void -writeDenseNB16(int Value, pointer Base, register unsigned long Offset); -extern void -writeDenseNB32(int Value, pointer Base, register unsigned long Offset); -extern void -writeDense8(int Value, pointer Base, register unsigned long Offset); -extern void -writeDense16(int Value, pointer Base, register unsigned long Offset); -extern void -writeDense32(int Value, pointer Base, register unsigned long Offset); - -static int readSparse8(pointer Base, register unsigned long Offset); -static int readSparse16(pointer Base, register unsigned long Offset); -static int readSparse32(pointer Base, register unsigned long Offset); -static void -writeSparseNB8(int Value, pointer Base, register unsigned long Offset); -static void -writeSparseNB16(int Value, pointer Base, register unsigned long Offset); -static void -writeSparseNB32(int Value, pointer Base, register unsigned long Offset); -static void -writeSparse8(int Value, pointer Base, register unsigned long Offset); -static void -writeSparse16(int Value, pointer Base, register unsigned long Offset); -static void -writeSparse32(int Value, pointer Base, register unsigned long Offset); - -#define DENSE_BASE 0x2ff00000000UL -#define SPARSE_BASE 0x30000000000UL - -static unsigned long msb_set = 0; - -static pointer -mapVidMemSparse(int ScreenNum, unsigned long Base, unsigned long Size, int flags) -{ - int fd, prot; - unsigned long ret, rets = 0; - - static Bool was_here = FALSE; - - if (!was_here) { - was_here = TRUE; - - xf86WriteMmio8 = writeSparse8; - xf86WriteMmio16 = writeSparse16; - xf86WriteMmio32 = writeSparse32; - xf86WriteMmioNB8 = writeSparseNB8; - xf86WriteMmioNB16 = writeSparseNB16; - xf86WriteMmioNB32 = writeSparseNB32; - xf86ReadMmio8 = readSparse8; - xf86ReadMmio16 = readSparse16; - xf86ReadMmio32 = readSparse32; - } - - fd = open(DEV_MEM, (flags & VIDMEM_READONLY) ? O_RDONLY : O_RDWR); - if (fd < 0) { - FatalError("xf86MapVidMem: failed to open " DEV_MEM " (%s)\n", - strerror(errno)); - } - -#if 0 - xf86Msg(X_INFO,"mapVidMemSparse: try Base 0x%lx size 0x%lx flags 0x%x\n", - Base, Size, flags); -#endif - - if (flags & VIDMEM_READONLY) - prot = PROT_READ; - else - prot = PROT_READ | PROT_WRITE; - - /* This requirers linux-0.99.pl10 or above */ - - /* - * Always do DENSE mmap, since read32/write32 currently require it. - */ - ret = (unsigned long)mmap((caddr_t)(DENSE_BASE + Base), Size, - prot, MAP_SHARED, fd, - (off_t) (bus_base + Base)); - - /* - * Do SPARSE mmap only when MMIO and not MMIO_32BIT, or FRAMEBUFFER - * and SPARSE (which should require the use of read/write macros). - * - * By not SPARSE mmapping an 8MB framebuffer, we can save approx. 256K - * bytes worth of pagetable (32 pages). - */ - if (((flags & VIDMEM_MMIO) && !(flags & VIDMEM_MMIO_32BIT)) || - ((flags & VIDMEM_FRAMEBUFFER) && (flags & VIDMEM_SPARSE))) - { - rets = (unsigned long)mmap((caddr_t)(SPARSE_BASE + (Base << 5)), - Size << 5, prot, MAP_SHARED, fd, - (off_t) _bus_base_sparse() + (Base << 5)); - } - - close(fd); - - if (ret == (unsigned long)MAP_FAILED) { - FatalError("xf86MapVidMemSparse: Could not (dense) mmap fb (%s)\n", - strerror(errno)); - } - - if (((flags & VIDMEM_MMIO) && !(flags & VIDMEM_MMIO_32BIT)) || - ((flags & VIDMEM_FRAMEBUFFER) && (flags & VIDMEM_SPARSE))) - { - if (rets == (unsigned long)MAP_FAILED || - rets != (SPARSE_BASE + (Base << 5))) - { - FatalError("mapVidMemSparse: Could not (sparse) mmap fb (%s)\n", - strerror(errno)); - } - } - -#if 1 - if (rets) - xf86Msg(X_INFO,"mapVidMemSparse: mapped Base 0x%lx size 0x%lx" - " to DENSE at 0x%lx and SPARSE at 0x%lx\n", - Base, Size, ret, rets); - else - xf86Msg(X_INFO,"mapVidMemSparse: mapped Base 0x%lx size 0x%lx" - " to DENSE only at 0x%lx\n", - Base, Size, ret); - -#endif - return (pointer) ret; -} - -static void -unmapVidMemSparse(int ScreenNum, pointer Base, unsigned long Size) -{ - unsigned long Offset = (unsigned long)Base - DENSE_BASE; -#if 1 - xf86Msg(X_INFO,"unmapVidMemSparse: unmapping Base 0x%lx Size 0x%lx\n", - Base, Size); -#endif - /* Unmap DENSE always. */ - munmap((caddr_t)Base, Size); - - /* Unmap SPARSE always, and ignore error in case we did not map it. */ - munmap((caddr_t)(SPARSE_BASE + (Offset << 5)), Size << 5); -} - -static int -readSparse8(pointer Base, register unsigned long Offset) -{ - register unsigned long result, shift; - register unsigned long msb; - - mem_barrier(); - Offset += (unsigned long)Base - DENSE_BASE; - shift = (Offset & 0x3) << 3; - if (Offset >= (hae_thresh)) { - msb = Offset & hae_mask; - Offset -= msb; - if (msb_set != msb) { - sethae(msb); - msb_set = msb; - } - } - - mem_barrier(); - result = *(vuip) (SPARSE_BASE + (Offset << 5)); - result >>= shift; - return 0xffUL & result; -} - -static int -readSparse16(pointer Base, register unsigned long Offset) -{ - register unsigned long result, shift; - register unsigned long msb; - - mem_barrier(); - Offset += (unsigned long)Base - DENSE_BASE; - shift = (Offset & 0x2) << 3; - if (Offset >= hae_thresh) { - msb = Offset & hae_mask; - Offset -= msb; - if (msb_set != msb) { - sethae(msb); - msb_set = msb; - } - } - - mem_barrier(); - result = *(vuip)(SPARSE_BASE + (Offset<<5) + (1<<(5-2))); - result >>= shift; - return 0xffffUL & result; -} - -static int -readSparse32(pointer Base, register unsigned long Offset) -{ - /* NOTE: this is really using DENSE. */ - mem_barrier(); - return *(vuip)((unsigned long)Base+(Offset)); -} - -static void -writeSparse8(int Value, pointer Base, register unsigned long Offset) -{ - register unsigned long msb; - register unsigned int b = Value & 0xffU; - - write_mem_barrier(); - Offset += (unsigned long)Base - DENSE_BASE; - if (Offset >= hae_thresh) { - msb = Offset & hae_mask; - Offset -= msb; - if (msb_set != msb) { - sethae(msb); - msb_set = msb; - } - } - - write_mem_barrier(); - *(vuip) (SPARSE_BASE + (Offset << 5)) = b * 0x01010101; -} - -static void -writeSparse16(int Value, pointer Base, register unsigned long Offset) -{ - register unsigned long msb; - register unsigned int w = Value & 0xffffU; - - write_mem_barrier(); - Offset += (unsigned long)Base - DENSE_BASE; - if (Offset >= hae_thresh) { - msb = Offset & hae_mask; - Offset -= msb; - if (msb_set != msb) { - sethae(msb); - msb_set = msb; - } - } - - write_mem_barrier(); - *(vuip)(SPARSE_BASE + (Offset<<5) + (1<<(5-2))) = w * 0x00010001; -} - -static void -writeSparse32(int Value, pointer Base, register unsigned long Offset) -{ - /* NOTE: this is really using DENSE. */ - write_mem_barrier(); - *(vuip)((unsigned long)Base + (Offset)) = Value; - return; -} - -static void -writeSparseNB8(int Value, pointer Base, register unsigned long Offset) -{ - register unsigned long msb; - register unsigned int b = Value & 0xffU; - - Offset += (unsigned long)Base - DENSE_BASE; - if (Offset >= hae_thresh) { - msb = Offset & hae_mask; - Offset -= msb; - if (msb_set != msb) { - sethae(msb); - msb_set = msb; - } - } - *(vuip) (SPARSE_BASE + (Offset << 5)) = b * 0x01010101; -} - -static void -writeSparseNB16(int Value, pointer Base, register unsigned long Offset) -{ - register unsigned long msb; - register unsigned int w = Value & 0xffffU; - - Offset += (unsigned long)Base - DENSE_BASE; - if (Offset >= hae_thresh) { - msb = Offset & hae_mask; - Offset -= msb; - if (msb_set != msb) { - sethae(msb); - msb_set = msb; - } - } - *(vuip)(SPARSE_BASE+(Offset<<5)+(1<<(5-2))) = w * 0x00010001; -} - -static void -writeSparseNB32(int Value, pointer Base, register unsigned long Offset) -{ - /* NOTE: this is really using DENSE. */ - *(vuip)((unsigned long)Base + (Offset)) = Value; - return; -} - -void (*xf86WriteMmio8)(int Value, pointer Base, unsigned long Offset) - = writeDense8; -void (*xf86WriteMmio16)(int Value, pointer Base, unsigned long Offset) - = writeDense16; -void (*xf86WriteMmio32)(int Value, pointer Base, unsigned long Offset) - = writeDense32; -void (*xf86WriteMmioNB8)(int Value, pointer Base, unsigned long Offset) - = writeDenseNB8; -void (*xf86WriteMmioNB16)(int Value, pointer Base, unsigned long Offset) - = writeDenseNB16; -void (*xf86WriteMmioNB32)(int Value, pointer Base, unsigned long Offset) - = writeDenseNB32; -int (*xf86ReadMmio8)(pointer Base, unsigned long Offset) - = readDense8; -int (*xf86ReadMmio16)(pointer Base, unsigned long Offset) - = readDense16; -int (*xf86ReadMmio32)(pointer Base, unsigned long Offset) - = readDense32; - -#endif /* __alpha__ */ +/*
+ * Copyright 1992 by Orest Zborowski <obz@Kodak.com>
+ * Copyright 1993 by David Wexelblat <dwex@goblin.org>
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and its
+ * documentation for any purpose is hereby granted without fee, provided that
+ * the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the names of Orest Zborowski and David Wexelblat
+ * not be used in advertising or publicity pertaining to distribution of
+ * the software without specific, written prior permission. Orest Zborowski
+ * and David Wexelblat make no representations about the suitability of this
+ * software for any purpose. It is provided "as is" without express or
+ * implied warranty.
+ *
+ * OREST ZBOROWSKI AND DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD
+ * TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS, IN NO EVENT SHALL OREST ZBOROWSKI OR DAVID WEXELBLAT BE LIABLE
+ * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ *
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include <errno.h>
+#include <string.h>
+
+#include <X11/X.h>
+#include "input.h"
+#include "scrnintstr.h"
+
+#include "xf86.h"
+#include "xf86Priv.h"
+#include "xf86_OSlib.h"
+#include "xf86OSpriv.h"
+#ifdef __alpha__
+#include "shared/xf86Axp.h"
+#endif
+
+#ifdef HAS_MTRR_SUPPORT
+#include <asm/mtrr.h>
+#endif
+
+#ifndef MAP_FAILED
+#define MAP_FAILED ((void *)-1)
+#endif
+
+static Bool ExtendedEnabled = FALSE;
+
+#ifdef __ia64__
+
+#include "compiler.h"
+#include <sys/io.h>
+
+#elif !defined(__powerpc__) && \
+ !defined(__mc68000__) && \
+ !defined(__sparc__) && \
+ !defined(__mips__) && \
+ !defined(__arm__)
+
+/*
+ * Due to conflicts with "compiler.h", don't rely on <sys/io.h> to declare
+ * these.
+ */
+extern int ioperm(unsigned long __from, unsigned long __num, int __turn_on);
+extern int iopl(int __level);
+
+#endif
+
+#ifdef __alpha__
+# define BUS_BASE bus_base
+#else
+#define BUS_BASE (0)
+#endif /* __alpha__ */
+
+/***************************************************************************/
+/* Video Memory Mapping section */
+/***************************************************************************/
+
+static pointer mapVidMem(int, unsigned long, unsigned long, int);
+static void unmapVidMem(int, pointer, unsigned long);
+#if defined (__alpha__)
+extern void sethae(unsigned long hae);
+extern unsigned long _bus_base __P ((void)) __attribute__ ((const));
+extern unsigned long _bus_base_sparse __P ((void)) __attribute__ ((const));
+
+static pointer mapVidMemSparse(int, unsigned long, unsigned long, int);
+extern axpDevice lnxGetAXP(void);
+static void unmapVidMemSparse(int, pointer, unsigned long);
+static axpDevice axpSystem = -1;
+static Bool needSparse;
+static unsigned long hae_thresh;
+static unsigned long hae_mask;
+static unsigned long bus_base;
+#endif
+
+#ifdef HAS_MTRR_SUPPORT
+
+#define SPLIT_WC_REGIONS 1
+
+static pointer setWC(int, unsigned long, unsigned long, Bool, MessageType);
+static void undoWC(int, pointer);
+
+/* The file desc for /proc/mtrr. Once opened, left opened, and the mtrr
+ driver will clean up when we exit. */
+#define MTRR_FD_UNOPENED (-1) /* We have yet to open /proc/mtrr */
+#define MTRR_FD_PROBLEM (-2) /* We tried to open /proc/mtrr, but had
+ a problem. */
+static int mtrr_fd = MTRR_FD_UNOPENED;
+
+/* Open /proc/mtrr. FALSE on failure. Will always fail on Linux 2.0,
+ and will fail on Linux 2.2 with MTRR support configured out,
+ so verbosity should be chosen appropriately. */
+static Bool
+mtrr_open(int verbosity)
+{
+ /* Only report absence of /proc/mtrr once. */
+ static Bool warned = FALSE;
+
+ if (mtrr_fd == MTRR_FD_UNOPENED) {
+ mtrr_fd = open("/proc/mtrr", O_WRONLY);
+
+ if (mtrr_fd < 0)
+ mtrr_fd = MTRR_FD_PROBLEM;
+ }
+
+ if (mtrr_fd == MTRR_FD_PROBLEM) {
+ /* To make sure we only ever warn once, need to check
+ verbosity outside xf86MsgVerb */
+ if (!warned && verbosity <= xf86GetVerbosity()) {
+ xf86MsgVerb(X_WARNING, verbosity,
+ "System lacks support for changing MTRRs\n");
+ warned = TRUE;
+ }
+
+ return FALSE;
+ }
+ else
+ return TRUE;
+}
+
+/*
+ * We maintain a list of WC regions for each physical mapping so they can
+ * be undone when unmapping.
+ */
+
+struct mtrr_wc_region {
+ struct mtrr_sentry sentry;
+ Bool added; /* added WC or removed it */
+ struct mtrr_wc_region * next;
+};
+
+
+static struct mtrr_wc_region *
+mtrr_cull_wc_region(int screenNum, unsigned long base, unsigned long size,
+ MessageType from)
+{
+ /* Some BIOS writers thought that setting wc over the mmio
+ region of a graphics devices was a good idea. Try to fix
+ it. */
+
+ struct mtrr_gentry gent;
+ struct mtrr_wc_region *wcreturn = NULL, *wcr;
+ int count, ret=0;
+
+ /* Linux 2.0 users should not get a warning without -verbose */
+ if (!mtrr_open(2))
+ return NULL;
+
+ for (gent.regnum = 0;
+ ioctl(mtrr_fd, MTRRIOC_GET_ENTRY, &gent) >= 0;
+ gent.regnum++) {
+ if (gent.type != MTRR_TYPE_WRCOMB
+ || gent.base + gent.size <= base
+ || base + size <= gent.base)
+ continue;
+
+ /* Found an overlapping region. Delete it. */
+
+ wcr = malloc(sizeof(*wcr));
+ if (!wcr)
+ return NULL;
+ wcr->sentry.base = gent.base;
+ wcr->sentry.size = gent.size;
+ wcr->sentry.type = MTRR_TYPE_WRCOMB;
+ wcr->added = FALSE;
+
+ count = 3;
+ while (count-- &&
+ (ret = ioctl(mtrr_fd, MTRRIOC_KILL_ENTRY, &(wcr->sentry))) < 0);
+
+ if (ret >= 0) {
+ xf86DrvMsg(screenNum, from,
+ "Removed MMIO write-combining range "
+ "(0x%lx,0x%lx)\n",
+ (unsigned long) gent.base, (unsigned long) gent.size);
+ wcr->next = wcreturn;
+ wcreturn = wcr;
+ gent.regnum--;
+ } else {
+ free(wcr);
+ xf86DrvMsgVerb(screenNum, X_WARNING, 0,
+ "Failed to remove MMIO "
+ "write-combining range (0x%lx,0x%lx)\n",
+ gent.base, (unsigned long) gent.size);
+ }
+ }
+ return wcreturn;
+}
+
+
+static struct mtrr_wc_region *
+mtrr_remove_offending(int screenNum, unsigned long base, unsigned long size,
+ MessageType from)
+{
+ struct mtrr_gentry gent;
+ struct mtrr_wc_region *wcreturn = NULL, **wcr;
+
+ if (!mtrr_open(2))
+ return NULL;
+
+ wcr = &wcreturn;
+ for (gent.regnum = 0;
+ ioctl(mtrr_fd, MTRRIOC_GET_ENTRY, &gent) >= 0; gent.regnum++ ) {
+ if (gent.type == MTRR_TYPE_WRCOMB
+ && ((gent.base >= base && gent.base + gent.size < base + size) ||
+ (gent.base > base && gent.base + gent.size <= base + size))) {
+ *wcr = mtrr_cull_wc_region(screenNum, gent.base, gent.size, from);
+ if (*wcr) gent.regnum--;
+ while(*wcr) {
+ wcr = &((*wcr)->next);
+ }
+ }
+ }
+ return wcreturn;
+}
+
+
+static struct mtrr_wc_region *
+mtrr_add_wc_region(int screenNum, unsigned long base, unsigned long size,
+ MessageType from)
+{
+ struct mtrr_wc_region **wcr, *wcreturn, *curwcr;
+
+ /*
+ * There can be only one....
+ */
+
+ wcreturn = mtrr_remove_offending(screenNum, base, size, from);
+ wcr = &wcreturn;
+ while (*wcr) {
+ wcr = &((*wcr)->next);
+ }
+
+ /* Linux 2.0 should not warn, unless the user explicitly asks for
+ WC. */
+
+ if (!mtrr_open(from == X_CONFIG ? 0 : 2))
+ return wcreturn;
+
+ *wcr = curwcr = malloc(sizeof(**wcr));
+ if (!curwcr)
+ return wcreturn;
+
+ curwcr->sentry.base = base;
+ curwcr->sentry.size = size;
+ curwcr->sentry.type = MTRR_TYPE_WRCOMB;
+ curwcr->added = TRUE;
+ curwcr->next = NULL;
+
+#if SPLIT_WC_REGIONS
+ /*
+ * Splits up the write-combining region if it is not aligned on a
+ * size boundary.
+ */
+
+ {
+ unsigned long lbase, d_size = 1;
+ unsigned long n_size = size;
+ unsigned long n_base = base;
+
+ for (lbase = n_base, d_size = 1; !(lbase & 1);
+ lbase = lbase >> 1, d_size <<= 1);
+ while (d_size > n_size)
+ d_size = d_size >> 1;
+ DebugF("WC_BASE: 0x%lx WC_END: 0x%lx\n",base,base+d_size-1);
+ n_base += d_size;
+ n_size -= d_size;
+ if (n_size) {
+ xf86DrvMsgVerb(screenNum,X_INFO,3,"Splitting WC range: "
+ "base: 0x%lx, size: 0x%lx\n",base,size);
+ curwcr->next = mtrr_add_wc_region(screenNum, n_base, n_size,from);
+ }
+ curwcr->sentry.size = d_size;
+ }
+
+ /*****************************************************************/
+#endif /* SPLIT_WC_REGIONS */
+
+ if (ioctl(mtrr_fd, MTRRIOC_ADD_ENTRY, &curwcr->sentry) >= 0) {
+ /* Avoid printing on every VT switch */
+ if (xf86ServerIsInitialising()) {
+ xf86DrvMsg(screenNum, from,
+ "Write-combining range (0x%lx,0x%lx)\n",
+ base, size);
+ }
+ return wcreturn;
+ }
+ else {
+ *wcr = curwcr->next;
+ free(curwcr);
+
+ /* Don't complain about the VGA region: MTRR fixed
+ regions aren't currently supported, but might be in
+ the future. */
+ if ((unsigned long)base >= 0x100000) {
+ xf86DrvMsgVerb(screenNum, X_WARNING, 0,
+ "Failed to set up write-combining range "
+ "(0x%lx,0x%lx)\n", base, size);
+ }
+ return wcreturn;
+ }
+}
+
+static void
+mtrr_undo_wc_region(int screenNum, struct mtrr_wc_region *wcr)
+{
+ struct mtrr_wc_region *p, *prev;
+
+ if (mtrr_fd >= 0) {
+ p = wcr;
+ while (p) {
+ if (p->added)
+ ioctl(mtrr_fd, MTRRIOC_DEL_ENTRY, &p->sentry);
+ prev = p;
+ p = p->next;
+ free(prev);
+ }
+ }
+}
+
+static pointer
+setWC(int screenNum, unsigned long base, unsigned long size, Bool enable,
+ MessageType from)
+{
+ if (enable)
+ return mtrr_add_wc_region(screenNum, base, size, from);
+ else
+ return mtrr_cull_wc_region(screenNum, base, size, from);
+}
+
+static void
+undoWC(int screenNum, pointer regioninfo)
+{
+ mtrr_undo_wc_region(screenNum, regioninfo);
+}
+
+#endif /* HAS_MTRR_SUPPORT */
+
+void
+xf86OSInitVidMem(VidMemInfoPtr pVidMem)
+{
+ pVidMem->linearSupported = TRUE;
+#ifdef __alpha__
+ if (axpSystem == -1) {
+ axpSystem = lnxGetAXP();
+ if ((needSparse = (_bus_base_sparse() > 0))) {
+ hae_thresh = xf86AXPParams[axpSystem].hae_thresh;
+ hae_mask = xf86AXPParams[axpSystem].hae_mask;
+ }
+ bus_base = _bus_base();
+ }
+ if (needSparse) {
+ xf86Msg(X_INFO,"Machine needs sparse mapping\n");
+ pVidMem->mapMem = mapVidMemSparse;
+ pVidMem->unmapMem = unmapVidMemSparse;
+ } else {
+ xf86Msg(X_INFO,"Machine type has 8/16 bit access\n");
+ pVidMem->mapMem = mapVidMem;
+ pVidMem->unmapMem = unmapVidMem;
+ }
+#else
+ pVidMem->mapMem = mapVidMem;
+ pVidMem->unmapMem = unmapVidMem;
+#endif /* __alpha__ */
+
+
+#ifdef HAS_MTRR_SUPPORT
+ pVidMem->setWC = setWC;
+ pVidMem->undoWC = undoWC;
+#endif
+ pVidMem->initialised = TRUE;
+}
+
+#ifdef __sparc__
+/* Basically, you simply cannot do this on Sparc. You have to do something portable
+ * like use /dev/fb* or mmap() on /proc/bus/pci/X/Y nodes. -DaveM
+ */
+static pointer mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags)
+{
+ return NULL;
+}
+#else
+static pointer
+mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int flags)
+{
+ pointer base;
+ int fd;
+ int mapflags = MAP_SHARED;
+ int prot;
+ memType realBase, alignOff;
+
+ realBase = Base & ~(getpagesize() - 1);
+ alignOff = Base - realBase;
+ DebugF("base: %lx, realBase: %lx, alignOff: %lx \n",
+ Base,realBase,alignOff);
+
+#if defined(__ia64__) || defined(__arm__) || defined(__s390__)
+#ifndef MAP_WRITECOMBINED
+#define MAP_WRITECOMBINED 0x00010000
+#endif
+#ifndef MAP_NONCACHED
+#define MAP_NONCACHED 0x00020000
+#endif
+ if(flags & VIDMEM_FRAMEBUFFER)
+ mapflags |= MAP_WRITECOMBINED;
+ else
+ mapflags |= MAP_NONCACHED;
+#endif
+
+#if 0
+ /* this will disappear when people upgrade their kernels */
+ fd = open(DEV_MEM,
+ ((flags & VIDMEM_READONLY) ? O_RDONLY : O_RDWR) | O_SYNC);
+#else
+ fd = open(DEV_MEM, (flags & VIDMEM_READONLY) ? O_RDONLY : O_RDWR);
+#endif
+ if (fd < 0)
+ {
+ FatalError("xf86MapVidMem: failed to open " DEV_MEM " (%s)\n",
+ strerror(errno));
+ }
+
+ if (flags & VIDMEM_READONLY)
+ prot = PROT_READ;
+ else
+ prot = PROT_READ | PROT_WRITE;
+
+ /* This requires linux-0.99.pl10 or above */
+ base = mmap((caddr_t)0, Size + alignOff, prot, mapflags, fd,
+ (off_t)realBase + BUS_BASE);
+ close(fd);
+ if (base == MAP_FAILED) {
+ FatalError("xf86MapVidMem: Could not mmap framebuffer"
+ " (0x%08lx,0x%lx) (%s)\n", Base, Size,
+ strerror(errno));
+ }
+ DebugF("base: %lx aligned base: %lx\n",base, base + alignOff);
+ return (char *)base + alignOff;
+}
+#endif /* !(__sparc__) */
+
+static void
+unmapVidMem(int ScreenNum, pointer Base, unsigned long Size)
+{
+ memType alignOff = (memType)Base
+ - ((memType)Base & ~(getpagesize() - 1));
+
+ DebugF("alignment offset: %lx\n",alignOff);
+ munmap((caddr_t)((memType)Base - alignOff), (Size + alignOff));
+}
+
+
+/***************************************************************************/
+/* I/O Permissions section */
+/***************************************************************************/
+
+#if defined(__powerpc__)
+volatile unsigned char *ioBase = NULL;
+
+#ifndef __NR_pciconfig_iobase
+#define __NR_pciconfig_iobase 200
+#endif
+
+#endif
+
+Bool
+xf86EnableIO(void)
+{
+#if defined(__powerpc__)
+ int fd;
+ unsigned int ioBase_phys;
+#endif
+
+ if (ExtendedEnabled)
+ return TRUE;
+
+#if defined(__powerpc__)
+ ioBase_phys = syscall(__NR_pciconfig_iobase, 2, 0, 0);
+
+ fd = open("/dev/mem", O_RDWR);
+ if (ioBase == NULL) {
+ ioBase = (volatile unsigned char *)mmap(0, 0x20000,
+ PROT_READ | PROT_WRITE, MAP_SHARED, fd,
+ ioBase_phys);
+/* Should this be fatal or just a warning? */
+#if 0
+ if (ioBase == MAP_FAILED) {
+ xf86Msg(X_WARNING,
+ "xf86EnableIOPorts: Failed to map iobase (%s)\n",
+ strerror(errno));
+ return FALSE;
+ }
+#endif
+ }
+ close(fd);
+#elif !defined(__mc68000__) && !defined(__sparc__) && !defined(__mips__) && !defined(__sh__) && !defined(__hppa__) && !defined(__s390__) && !defined(__arm__) && !defined(__m32r__)
+ if (ioperm(0, 1024, 1) || iopl(3)) {
+ if (errno == ENODEV)
+ ErrorF("xf86EnableIOPorts: no I/O ports found\n");
+ else
+ FatalError("xf86EnableIOPorts: failed to set IOPL"
+ " for I/O (%s)\n", strerror(errno));
+ return FALSE;
+ }
+# if !defined(__alpha__)
+ ioperm(0x40,4,0); /* trap access to the timer chip */
+ ioperm(0x60,4,0); /* trap access to the keyboard controller */
+# endif
+#endif
+ ExtendedEnabled = TRUE;
+
+ return TRUE;
+}
+
+void
+xf86DisableIO(void)
+{
+ if (!ExtendedEnabled)
+ return;
+#if defined(__powerpc__)
+ munmap(ioBase, 0x20000);
+ ioBase = NULL;
+#elif !defined(__mc68000__) && !defined(__sparc__) && !defined(__mips__) && !defined(__sh__) && !defined(__hppa__) && !defined(__arm__) && !defined(__s390__) && !defined(__m32r__)
+ iopl(0);
+ ioperm(0, 1024, 0);
+#endif
+ ExtendedEnabled = FALSE;
+
+ return;
+}
+
+#if defined (__alpha__)
+
+#define vuip volatile unsigned int *
+
+extern int readDense8(pointer Base, register unsigned long Offset);
+extern int readDense16(pointer Base, register unsigned long Offset);
+extern int readDense32(pointer Base, register unsigned long Offset);
+extern void
+writeDenseNB8(int Value, pointer Base, register unsigned long Offset);
+extern void
+writeDenseNB16(int Value, pointer Base, register unsigned long Offset);
+extern void
+writeDenseNB32(int Value, pointer Base, register unsigned long Offset);
+extern void
+writeDense8(int Value, pointer Base, register unsigned long Offset);
+extern void
+writeDense16(int Value, pointer Base, register unsigned long Offset);
+extern void
+writeDense32(int Value, pointer Base, register unsigned long Offset);
+
+static int readSparse8(pointer Base, register unsigned long Offset);
+static int readSparse16(pointer Base, register unsigned long Offset);
+static int readSparse32(pointer Base, register unsigned long Offset);
+static void
+writeSparseNB8(int Value, pointer Base, register unsigned long Offset);
+static void
+writeSparseNB16(int Value, pointer Base, register unsigned long Offset);
+static void
+writeSparseNB32(int Value, pointer Base, register unsigned long Offset);
+static void
+writeSparse8(int Value, pointer Base, register unsigned long Offset);
+static void
+writeSparse16(int Value, pointer Base, register unsigned long Offset);
+static void
+writeSparse32(int Value, pointer Base, register unsigned long Offset);
+
+#define DENSE_BASE 0x2ff00000000UL
+#define SPARSE_BASE 0x30000000000UL
+
+static unsigned long msb_set = 0;
+
+static pointer
+mapVidMemSparse(int ScreenNum, unsigned long Base, unsigned long Size, int flags)
+{
+ int fd, prot;
+ unsigned long ret, rets = 0;
+
+ static Bool was_here = FALSE;
+
+ if (!was_here) {
+ was_here = TRUE;
+
+ xf86WriteMmio8 = writeSparse8;
+ xf86WriteMmio16 = writeSparse16;
+ xf86WriteMmio32 = writeSparse32;
+ xf86WriteMmioNB8 = writeSparseNB8;
+ xf86WriteMmioNB16 = writeSparseNB16;
+ xf86WriteMmioNB32 = writeSparseNB32;
+ xf86ReadMmio8 = readSparse8;
+ xf86ReadMmio16 = readSparse16;
+ xf86ReadMmio32 = readSparse32;
+ }
+
+ fd = open(DEV_MEM, (flags & VIDMEM_READONLY) ? O_RDONLY : O_RDWR);
+ if (fd < 0) {
+ FatalError("xf86MapVidMem: failed to open " DEV_MEM " (%s)\n",
+ strerror(errno));
+ }
+
+#if 0
+ xf86Msg(X_INFO,"mapVidMemSparse: try Base 0x%lx size 0x%lx flags 0x%x\n",
+ Base, Size, flags);
+#endif
+
+ if (flags & VIDMEM_READONLY)
+ prot = PROT_READ;
+ else
+ prot = PROT_READ | PROT_WRITE;
+
+ /* This requirers linux-0.99.pl10 or above */
+
+ /*
+ * Always do DENSE mmap, since read32/write32 currently require it.
+ */
+ ret = (unsigned long)mmap((caddr_t)(DENSE_BASE + Base), Size,
+ prot, MAP_SHARED, fd,
+ (off_t) (bus_base + Base));
+
+ /*
+ * Do SPARSE mmap only when MMIO and not MMIO_32BIT, or FRAMEBUFFER
+ * and SPARSE (which should require the use of read/write macros).
+ *
+ * By not SPARSE mmapping an 8MB framebuffer, we can save approx. 256K
+ * bytes worth of pagetable (32 pages).
+ */
+ if (((flags & VIDMEM_MMIO) && !(flags & VIDMEM_MMIO_32BIT)) ||
+ ((flags & VIDMEM_FRAMEBUFFER) && (flags & VIDMEM_SPARSE)))
+ {
+ rets = (unsigned long)mmap((caddr_t)(SPARSE_BASE + (Base << 5)),
+ Size << 5, prot, MAP_SHARED, fd,
+ (off_t) _bus_base_sparse() + (Base << 5));
+ }
+
+ close(fd);
+
+ if (ret == (unsigned long)MAP_FAILED) {
+ FatalError("xf86MapVidMemSparse: Could not (dense) mmap fb (%s)\n",
+ strerror(errno));
+ }
+
+ if (((flags & VIDMEM_MMIO) && !(flags & VIDMEM_MMIO_32BIT)) ||
+ ((flags & VIDMEM_FRAMEBUFFER) && (flags & VIDMEM_SPARSE)))
+ {
+ if (rets == (unsigned long)MAP_FAILED ||
+ rets != (SPARSE_BASE + (Base << 5)))
+ {
+ FatalError("mapVidMemSparse: Could not (sparse) mmap fb (%s)\n",
+ strerror(errno));
+ }
+ }
+
+#if 1
+ if (rets)
+ xf86Msg(X_INFO,"mapVidMemSparse: mapped Base 0x%lx size 0x%lx"
+ " to DENSE at 0x%lx and SPARSE at 0x%lx\n",
+ Base, Size, ret, rets);
+ else
+ xf86Msg(X_INFO,"mapVidMemSparse: mapped Base 0x%lx size 0x%lx"
+ " to DENSE only at 0x%lx\n",
+ Base, Size, ret);
+
+#endif
+ return (pointer) ret;
+}
+
+static void
+unmapVidMemSparse(int ScreenNum, pointer Base, unsigned long Size)
+{
+ unsigned long Offset = (unsigned long)Base - DENSE_BASE;
+#if 1
+ xf86Msg(X_INFO,"unmapVidMemSparse: unmapping Base 0x%lx Size 0x%lx\n",
+ Base, Size);
+#endif
+ /* Unmap DENSE always. */
+ munmap((caddr_t)Base, Size);
+
+ /* Unmap SPARSE always, and ignore error in case we did not map it. */
+ munmap((caddr_t)(SPARSE_BASE + (Offset << 5)), Size << 5);
+}
+
+static int
+readSparse8(pointer Base, register unsigned long Offset)
+{
+ register unsigned long result, shift;
+ register unsigned long msb;
+
+ mem_barrier();
+ Offset += (unsigned long)Base - DENSE_BASE;
+ shift = (Offset & 0x3) << 3;
+ if (Offset >= (hae_thresh)) {
+ msb = Offset & hae_mask;
+ Offset -= msb;
+ if (msb_set != msb) {
+ sethae(msb);
+ msb_set = msb;
+ }
+ }
+
+ mem_barrier();
+ result = *(vuip) (SPARSE_BASE + (Offset << 5));
+ result >>= shift;
+ return 0xffUL & result;
+}
+
+static int
+readSparse16(pointer Base, register unsigned long Offset)
+{
+ register unsigned long result, shift;
+ register unsigned long msb;
+
+ mem_barrier();
+ Offset += (unsigned long)Base - DENSE_BASE;
+ shift = (Offset & 0x2) << 3;
+ if (Offset >= hae_thresh) {
+ msb = Offset & hae_mask;
+ Offset -= msb;
+ if (msb_set != msb) {
+ sethae(msb);
+ msb_set = msb;
+ }
+ }
+
+ mem_barrier();
+ result = *(vuip)(SPARSE_BASE + (Offset<<5) + (1<<(5-2)));
+ result >>= shift;
+ return 0xffffUL & result;
+}
+
+static int
+readSparse32(pointer Base, register unsigned long Offset)
+{
+ /* NOTE: this is really using DENSE. */
+ mem_barrier();
+ return *(vuip)((unsigned long)Base+(Offset));
+}
+
+static void
+writeSparse8(int Value, pointer Base, register unsigned long Offset)
+{
+ register unsigned long msb;
+ register unsigned int b = Value & 0xffU;
+
+ write_mem_barrier();
+ Offset += (unsigned long)Base - DENSE_BASE;
+ if (Offset >= hae_thresh) {
+ msb = Offset & hae_mask;
+ Offset -= msb;
+ if (msb_set != msb) {
+ sethae(msb);
+ msb_set = msb;
+ }
+ }
+
+ write_mem_barrier();
+ *(vuip) (SPARSE_BASE + (Offset << 5)) = b * 0x01010101;
+}
+
+static void
+writeSparse16(int Value, pointer Base, register unsigned long Offset)
+{
+ register unsigned long msb;
+ register unsigned int w = Value & 0xffffU;
+
+ write_mem_barrier();
+ Offset += (unsigned long)Base - DENSE_BASE;
+ if (Offset >= hae_thresh) {
+ msb = Offset & hae_mask;
+ Offset -= msb;
+ if (msb_set != msb) {
+ sethae(msb);
+ msb_set = msb;
+ }
+ }
+
+ write_mem_barrier();
+ *(vuip)(SPARSE_BASE + (Offset<<5) + (1<<(5-2))) = w * 0x00010001;
+}
+
+static void
+writeSparse32(int Value, pointer Base, register unsigned long Offset)
+{
+ /* NOTE: this is really using DENSE. */
+ write_mem_barrier();
+ *(vuip)((unsigned long)Base + (Offset)) = Value;
+ return;
+}
+
+static void
+writeSparseNB8(int Value, pointer Base, register unsigned long Offset)
+{
+ register unsigned long msb;
+ register unsigned int b = Value & 0xffU;
+
+ Offset += (unsigned long)Base - DENSE_BASE;
+ if (Offset >= hae_thresh) {
+ msb = Offset & hae_mask;
+ Offset -= msb;
+ if (msb_set != msb) {
+ sethae(msb);
+ msb_set = msb;
+ }
+ }
+ *(vuip) (SPARSE_BASE + (Offset << 5)) = b * 0x01010101;
+}
+
+static void
+writeSparseNB16(int Value, pointer Base, register unsigned long Offset)
+{
+ register unsigned long msb;
+ register unsigned int w = Value & 0xffffU;
+
+ Offset += (unsigned long)Base - DENSE_BASE;
+ if (Offset >= hae_thresh) {
+ msb = Offset & hae_mask;
+ Offset -= msb;
+ if (msb_set != msb) {
+ sethae(msb);
+ msb_set = msb;
+ }
+ }
+ *(vuip)(SPARSE_BASE+(Offset<<5)+(1<<(5-2))) = w * 0x00010001;
+}
+
+static void
+writeSparseNB32(int Value, pointer Base, register unsigned long Offset)
+{
+ /* NOTE: this is really using DENSE. */
+ *(vuip)((unsigned long)Base + (Offset)) = Value;
+ return;
+}
+
+void (*xf86WriteMmio8)(int Value, pointer Base, unsigned long Offset)
+ = writeDense8;
+void (*xf86WriteMmio16)(int Value, pointer Base, unsigned long Offset)
+ = writeDense16;
+void (*xf86WriteMmio32)(int Value, pointer Base, unsigned long Offset)
+ = writeDense32;
+void (*xf86WriteMmioNB8)(int Value, pointer Base, unsigned long Offset)
+ = writeDenseNB8;
+void (*xf86WriteMmioNB16)(int Value, pointer Base, unsigned long Offset)
+ = writeDenseNB16;
+void (*xf86WriteMmioNB32)(int Value, pointer Base, unsigned long Offset)
+ = writeDenseNB32;
+int (*xf86ReadMmio8)(pointer Base, unsigned long Offset)
+ = readDense8;
+int (*xf86ReadMmio16)(pointer Base, unsigned long Offset)
+ = readDense16;
+int (*xf86ReadMmio32)(pointer Base, unsigned long Offset)
+ = readDense32;
+
+#endif /* __alpha__ */
diff --git a/xorg-server/hw/xfree86/os-support/shared/posix_tty.c b/xorg-server/hw/xfree86/os-support/shared/posix_tty.c index da57939cf..7e2c4a181 100644 --- a/xorg-server/hw/xfree86/os-support/shared/posix_tty.c +++ b/xorg-server/hw/xfree86/os-support/shared/posix_tty.c @@ -1,655 +1,655 @@ -/* - * Copyright 1993-2003 by The XFree86 Project, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF - * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - * Except as contained in this notice, the name of the XFree86 Project shall - * not be used in advertising or otherwise to promote the sale, use or other - * dealings in this Software without prior written authorization from the - * XFree86 Project. - */ -/* - * - * Copyright (c) 1997 Metro Link Incorporated - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF - * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - * - * Except as contained in this notice, the name of the Metro Link shall not be - * used in advertising or otherwise to promote the sale, use or other dealings - * in this Software without prior written authorization from Metro Link. - * - */ - -#ifdef HAVE_XORG_CONFIG_H -#include <xorg-config.h> -#endif - -#include <X11/X.h> -#include "xf86.h" -#include "xf86Priv.h" -#include "xf86_OSlib.h" - -static int -GetBaud (int baudrate) -{ -#ifdef B300 - if (baudrate == 300) - return B300; -#endif -#ifdef B1200 - if (baudrate == 1200) - return B1200; -#endif -#ifdef B2400 - if (baudrate == 2400) - return B2400; -#endif -#ifdef B4800 - if (baudrate == 4800) - return B4800; -#endif -#ifdef B9600 - if (baudrate == 9600) - return B9600; -#endif -#ifdef B19200 - if (baudrate == 19200) - return B19200; -#endif -#ifdef B38400 - if (baudrate == 38400) - return B38400; -#endif -#ifdef B57600 - if (baudrate == 57600) - return B57600; -#endif -#ifdef B115200 - if (baudrate == 115200) - return B115200; -#endif -#ifdef B230400 - if (baudrate == 230400) - return B230400; -#endif -#ifdef B460800 - if (baudrate == 460800) - return B460800; -#endif - return (0); -} - -int -xf86OpenSerial (pointer options) -{ - struct termios t; - int fd, i; - char *dev; - - dev = xf86SetStrOption (options, "Device", NULL); - if (!dev) - { - xf86Msg (X_ERROR, "xf86OpenSerial: No Device specified.\n"); - return (-1); - } - - SYSCALL (fd = open (dev, O_RDWR | O_NONBLOCK)); - if (fd == -1) - { - xf86Msg (X_ERROR, - "xf86OpenSerial: Cannot open device %s\n\t%s.\n", - dev, strerror (errno)); - xfree(dev); - return (-1); - } - - if (!isatty (fd)) - { - /* Allow non-tty devices to be opened. */ - xfree(dev); - return (fd); - } - - /* set up default port parameters */ - SYSCALL (tcgetattr (fd, &t)); - t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR\ - |IGNCR|ICRNL|IXON); - t.c_oflag &= ~OPOST; - t.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); - t.c_cflag &= ~(CSIZE|PARENB); - t.c_cflag |= CS8|CLOCAL; - - cfsetispeed (&t, B9600); - cfsetospeed (&t, B9600); - t.c_cc[VMIN] = 1; - t.c_cc[VTIME] = 0; - - SYSCALL (tcsetattr (fd, TCSANOW, &t)); - - if (xf86SetSerial (fd, options) == -1) - { - SYSCALL (close (fd)); - xfree(dev); - return (-1); - } - - SYSCALL (i = fcntl (fd, F_GETFL, 0)); - if (i == -1) - { - SYSCALL (close (fd)); - xfree(dev); - return (-1); - } - i &= ~O_NONBLOCK; - SYSCALL (i = fcntl (fd, F_SETFL, i)); - if (i == -1) - { - SYSCALL (close (fd)); - xfree(dev); - return (-1); - } - xfree(dev); - return (fd); -} - -int -xf86SetSerial (int fd, pointer options) -{ - struct termios t; - int val; - const char *s; - int baud, r; - - if (fd < 0) - return -1; - - /* Don't try to set parameters for non-tty devices. */ - if (!isatty(fd)) - return 0; - - SYSCALL (tcgetattr (fd, &t)); - - if ((val = xf86SetIntOption (options, "BaudRate", 0))) - { - if ((baud = GetBaud (val))) - { - cfsetispeed (&t, baud); - cfsetospeed (&t, baud); - } - else - { - xf86Msg (X_ERROR, - "Invalid Option BaudRate value: %d\n", val); - return (-1); - } - } - - if ((val = xf86SetIntOption (options, "StopBits", 0))) - { - switch (val) - { - case 1: - t.c_cflag &= ~(CSTOPB); - break; - case 2: - t.c_cflag |= CSTOPB; - break; - default: - xf86Msg (X_ERROR, - "Invalid Option StopBits value: %d\n", val); - return (-1); - break; - } - } - - if ((val = xf86SetIntOption (options, "DataBits", 0))) - { - switch (val) - { - case 5: - t.c_cflag &= ~(CSIZE); - t.c_cflag |= CS5; - break; - case 6: - t.c_cflag &= ~(CSIZE); - t.c_cflag |= CS6; - break; - case 7: - t.c_cflag &= ~(CSIZE); - t.c_cflag |= CS7; - break; - case 8: - t.c_cflag &= ~(CSIZE); - t.c_cflag |= CS8; - break; - default: - xf86Msg (X_ERROR, - "Invalid Option DataBits value: %d\n", val); - return (-1); - break; - } - } - - if ((s = xf86SetStrOption (options, "Parity", NULL))) - { - if (xf86NameCmp (s, "Odd") == 0) - { - t.c_cflag |= PARENB | PARODD; - } - else if (xf86NameCmp (s, "Even") == 0) - { - t.c_cflag |= PARENB; - t.c_cflag &= ~(PARODD); - } - else if (xf86NameCmp (s, "None") == 0) - { - t.c_cflag &= ~(PARENB); - } - else - { - xf86Msg (X_ERROR, "Invalid Option Parity value: %s\n", - s); - return (-1); - } - } - - if ((val = xf86SetIntOption (options, "Vmin", -1)) != -1) - { - t.c_cc[VMIN] = val; - } - if ((val = xf86SetIntOption (options, "Vtime", -1)) != -1) - { - t.c_cc[VTIME] = val; - } - - if ((s = xf86SetStrOption (options, "FlowControl", NULL))) - { - xf86MarkOptionUsedByName (options, "FlowControl"); - if (xf86NameCmp (s, "Xoff") == 0) - { - t.c_iflag |= IXOFF; - } - else if (xf86NameCmp (s, "Xon") == 0) - { - t.c_iflag |= IXON; - } - else if (xf86NameCmp (s, "XonXoff") == 0) - { - t.c_iflag |= IXON|IXOFF; - } - else if (xf86NameCmp (s, "None") == 0) - { - t.c_iflag &= ~(IXON | IXOFF); - } - else - { - xf86Msg (X_ERROR, - "Invalid Option FlowControl value: %s\n", s); - return (-1); - } - } - - if ((xf86SetBoolOption (options, "ClearDTR", FALSE))) - { -#ifdef CLEARDTR_SUPPORT -# if defined(TIOCMBIC) - val = TIOCM_DTR; - SYSCALL (ioctl(fd, TIOCMBIC, &val)); -# else - SYSCALL (ioctl(fd, TIOCCDTR, NULL)); -# endif -#else - xf86Msg (X_WARNING, - "Option ClearDTR not supported on this OS\n"); - return (-1); -#endif - xf86MarkOptionUsedByName (options, "ClearDTR"); - } - - if ((xf86SetBoolOption (options, "ClearRTS", FALSE))) - { - xf86Msg (X_WARNING, - "Option ClearRTS not supported on this OS\n"); - return (-1); - xf86MarkOptionUsedByName (options, "ClearRTS"); - } - - SYSCALL (r = tcsetattr (fd, TCSANOW, &t)); - return (r); -} - -int -xf86SetSerialSpeed (int fd, int speed) -{ - struct termios t; - int baud, r; - - if (fd < 0) - return -1; - - /* Don't try to set parameters for non-tty devices. */ - if (!isatty(fd)) - return 0; - - SYSCALL (tcgetattr (fd, &t)); - - if ((baud = GetBaud (speed))) - { - cfsetispeed (&t, baud); - cfsetospeed (&t, baud); - } - else - { - xf86Msg (X_ERROR, - "Invalid Option BaudRate value: %d\n", speed); - return (-1); - } - - SYSCALL (r = tcsetattr (fd, TCSANOW, &t)); - return (r); -} - -int -xf86ReadSerial (int fd, void *buf, int count) -{ - int r; - int i; - - SYSCALL (r = read (fd, buf, count)); - DebugF("ReadingSerial: 0x%x", - (unsigned char)*(((unsigned char *)buf))); - for (i = 1; i < r; i++) - DebugF(", 0x%x",(unsigned char)*(((unsigned char *)buf) + i)); - DebugF("\n"); - return (r); -} - -int -xf86WriteSerial (int fd, const void *buf, int count) -{ - int r; - int i; - - DebugF("WritingSerial: 0x%x",(unsigned char)*(((unsigned char *)buf))); - for (i = 1; i < count; i++) - ErrorF(", 0x%x",(unsigned char)*(((unsigned char *)buf) + i)); - DebugF("\n"); - SYSCALL (r = write (fd, buf, count)); - return (r); -} - -int -xf86CloseSerial (int fd) -{ - int r; - - SYSCALL (r = close (fd)); - return (r); -} - -int -xf86WaitForInput (int fd, int timeout) -{ - fd_set readfds; - struct timeval to; - int r; - - FD_ZERO(&readfds); - - if (fd >= 0) { - FD_SET(fd, &readfds); - } - - to.tv_sec = timeout / 1000000; - to.tv_usec = timeout % 1000000; - - if (fd >= 0) { - SYSCALL (r = select (FD_SETSIZE, &readfds, NULL, NULL, &to)); - } - else { - SYSCALL (r = select (FD_SETSIZE, NULL, NULL, NULL, &to)); - } - xf86ErrorFVerb (9,"select returned %d\n", r); - return (r); -} - -int -xf86SerialSendBreak (int fd, int duration) -{ - int r; - - SYSCALL (r = tcsendbreak (fd, duration)); - return (r); - -} - -int -xf86FlushInput(int fd) -{ - fd_set fds; - struct timeval timeout; - char c[4]; - - DebugF("FlushingSerial\n"); - if (tcflush(fd, TCIFLUSH) == 0) - return 0; - - timeout.tv_sec = 0; - timeout.tv_usec = 0; - FD_ZERO(&fds); - FD_SET(fd, &fds); - while (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) { - if (read(fd, &c, sizeof(c)) < 1) - return 0; - FD_ZERO(&fds); - FD_SET(fd, &fds); - } - return 0; -} - -static struct states { - int xf; - int os; -} modemStates[] = { -#ifdef TIOCM_LE - { XF86_M_LE, TIOCM_LE }, -#endif -#ifdef TIOCM_DTR - { XF86_M_DTR, TIOCM_DTR }, -#endif -#ifdef TIOCM_RTS - { XF86_M_RTS, TIOCM_RTS }, -#endif -#ifdef TIOCM_ST - { XF86_M_ST, TIOCM_ST }, -#endif -#ifdef TIOCM_SR - { XF86_M_SR, TIOCM_SR }, -#endif -#ifdef TIOCM_CTS - { XF86_M_CTS, TIOCM_CTS }, -#endif -#ifdef TIOCM_CAR - { XF86_M_CAR, TIOCM_CAR }, -#elif defined(TIOCM_CD) - { XF86_M_CAR, TIOCM_CD }, -#endif -#ifdef TIOCM_RNG - { XF86_M_RNG, TIOCM_RNG }, -#elif defined(TIOCM_RI) - { XF86_M_CAR, TIOCM_RI }, -#endif -#ifdef TIOCM_DSR - { XF86_M_DSR, TIOCM_DSR }, -#endif -}; - -static int numStates = sizeof(modemStates) / sizeof(modemStates[0]); - -static int -xf2osState(int state) -{ - int i; - int ret = 0; - - for (i = 0; i < numStates; i++) - if (state & modemStates[i].xf) - ret |= modemStates[i].os; - return ret; -} - -static int -os2xfState(int state) -{ - int i; - int ret = 0; - - for (i = 0; i < numStates; i++) - if (state & modemStates[i].os) - ret |= modemStates[i].xf; - return ret; -} - -static int -getOsStateMask(void) -{ - int i; - int ret = 0; - for (i = 0; i < numStates; i++) - ret |= modemStates[i].os; - return ret; -} - -static int osStateMask = 0; - -int -xf86SetSerialModemState(int fd, int state) -{ - int ret; - int s; - - if (fd < 0) - return -1; - - /* Don't try to set parameters for non-tty devices. */ - if (!isatty(fd)) - return 0; - -#ifndef TIOCMGET - return -1; -#else - if (!osStateMask) - osStateMask = getOsStateMask(); - - state = xf2osState(state); - SYSCALL((ret = ioctl(fd, TIOCMGET, &s))); - if (ret < 0) - return -1; - s &= ~osStateMask; - s |= state; - SYSCALL((ret = ioctl(fd, TIOCMSET, &s))); - if (ret < 0) - return -1; - else - return 0; -#endif -} - -int -xf86GetSerialModemState(int fd) -{ - int ret; - int s; - - if (fd < 0) - return -1; - - /* Don't try to set parameters for non-tty devices. */ - if (!isatty(fd)) - return 0; - -#ifndef TIOCMGET - return -1; -#else - SYSCALL((ret = ioctl(fd, TIOCMGET, &s))); - if (ret < 0) - return -1; - return os2xfState(s); -#endif -} - -int -xf86SerialModemSetBits(int fd, int bits) -{ - int ret; - int s; - - if (fd < 0) - return -1; - - /* Don't try to set parameters for non-tty devices. */ - if (!isatty(fd)) - return 0; - -#ifndef TIOCMGET - return -1; -#else - s = xf2osState(bits); - SYSCALL((ret = ioctl(fd, TIOCMBIS, &s))); - return ret; -#endif -} - -int -xf86SerialModemClearBits(int fd, int bits) -{ - int ret; - int s; - - if (fd < 0) - return -1; - - /* Don't try to set parameters for non-tty devices. */ - if (!isatty(fd)) - return 0; - -#ifndef TIOCMGET - return -1; -#else - s = xf2osState(bits); - SYSCALL((ret = ioctl(fd, TIOCMBIC, &s))); - return ret; -#endif -} +/*
+ * Copyright 1993-2003 by The XFree86 Project, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Except as contained in this notice, the name of the XFree86 Project shall
+ * not be used in advertising or otherwise to promote the sale, use or other
+ * dealings in this Software without prior written authorization from the
+ * XFree86 Project.
+ */
+/*
+ *
+ * Copyright (c) 1997 Metro Link Incorporated
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
+ * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ *
+ * Except as contained in this notice, the name of the Metro Link shall not be
+ * used in advertising or otherwise to promote the sale, use or other dealings
+ * in this Software without prior written authorization from Metro Link.
+ *
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include <X11/X.h>
+#include "xf86.h"
+#include "xf86Priv.h"
+#include "xf86_OSlib.h"
+
+static int
+GetBaud (int baudrate)
+{
+#ifdef B300
+ if (baudrate == 300)
+ return B300;
+#endif
+#ifdef B1200
+ if (baudrate == 1200)
+ return B1200;
+#endif
+#ifdef B2400
+ if (baudrate == 2400)
+ return B2400;
+#endif
+#ifdef B4800
+ if (baudrate == 4800)
+ return B4800;
+#endif
+#ifdef B9600
+ if (baudrate == 9600)
+ return B9600;
+#endif
+#ifdef B19200
+ if (baudrate == 19200)
+ return B19200;
+#endif
+#ifdef B38400
+ if (baudrate == 38400)
+ return B38400;
+#endif
+#ifdef B57600
+ if (baudrate == 57600)
+ return B57600;
+#endif
+#ifdef B115200
+ if (baudrate == 115200)
+ return B115200;
+#endif
+#ifdef B230400
+ if (baudrate == 230400)
+ return B230400;
+#endif
+#ifdef B460800
+ if (baudrate == 460800)
+ return B460800;
+#endif
+ return (0);
+}
+
+int
+xf86OpenSerial (pointer options)
+{
+ struct termios t;
+ int fd, i;
+ char *dev;
+
+ dev = xf86SetStrOption (options, "Device", NULL);
+ if (!dev)
+ {
+ xf86Msg (X_ERROR, "xf86OpenSerial: No Device specified.\n");
+ return (-1);
+ }
+
+ SYSCALL (fd = open (dev, O_RDWR | O_NONBLOCK));
+ if (fd == -1)
+ {
+ xf86Msg (X_ERROR,
+ "xf86OpenSerial: Cannot open device %s\n\t%s.\n",
+ dev, strerror (errno));
+ free(dev);
+ return (-1);
+ }
+
+ if (!isatty (fd))
+ {
+ /* Allow non-tty devices to be opened. */
+ free(dev);
+ return (fd);
+ }
+
+ /* set up default port parameters */
+ SYSCALL (tcgetattr (fd, &t));
+ t.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR\
+ |IGNCR|ICRNL|IXON);
+ t.c_oflag &= ~OPOST;
+ t.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
+ t.c_cflag &= ~(CSIZE|PARENB);
+ t.c_cflag |= CS8|CLOCAL;
+
+ cfsetispeed (&t, B9600);
+ cfsetospeed (&t, B9600);
+ t.c_cc[VMIN] = 1;
+ t.c_cc[VTIME] = 0;
+
+ SYSCALL (tcsetattr (fd, TCSANOW, &t));
+
+ if (xf86SetSerial (fd, options) == -1)
+ {
+ SYSCALL (close (fd));
+ free(dev);
+ return (-1);
+ }
+
+ SYSCALL (i = fcntl (fd, F_GETFL, 0));
+ if (i == -1)
+ {
+ SYSCALL (close (fd));
+ free(dev);
+ return (-1);
+ }
+ i &= ~O_NONBLOCK;
+ SYSCALL (i = fcntl (fd, F_SETFL, i));
+ if (i == -1)
+ {
+ SYSCALL (close (fd));
+ free(dev);
+ return (-1);
+ }
+ free(dev);
+ return (fd);
+}
+
+int
+xf86SetSerial (int fd, pointer options)
+{
+ struct termios t;
+ int val;
+ const char *s;
+ int baud, r;
+
+ if (fd < 0)
+ return -1;
+
+ /* Don't try to set parameters for non-tty devices. */
+ if (!isatty(fd))
+ return 0;
+
+ SYSCALL (tcgetattr (fd, &t));
+
+ if ((val = xf86SetIntOption (options, "BaudRate", 0)))
+ {
+ if ((baud = GetBaud (val)))
+ {
+ cfsetispeed (&t, baud);
+ cfsetospeed (&t, baud);
+ }
+ else
+ {
+ xf86Msg (X_ERROR,
+ "Invalid Option BaudRate value: %d\n", val);
+ return (-1);
+ }
+ }
+
+ if ((val = xf86SetIntOption (options, "StopBits", 0)))
+ {
+ switch (val)
+ {
+ case 1:
+ t.c_cflag &= ~(CSTOPB);
+ break;
+ case 2:
+ t.c_cflag |= CSTOPB;
+ break;
+ default:
+ xf86Msg (X_ERROR,
+ "Invalid Option StopBits value: %d\n", val);
+ return (-1);
+ break;
+ }
+ }
+
+ if ((val = xf86SetIntOption (options, "DataBits", 0)))
+ {
+ switch (val)
+ {
+ case 5:
+ t.c_cflag &= ~(CSIZE);
+ t.c_cflag |= CS5;
+ break;
+ case 6:
+ t.c_cflag &= ~(CSIZE);
+ t.c_cflag |= CS6;
+ break;
+ case 7:
+ t.c_cflag &= ~(CSIZE);
+ t.c_cflag |= CS7;
+ break;
+ case 8:
+ t.c_cflag &= ~(CSIZE);
+ t.c_cflag |= CS8;
+ break;
+ default:
+ xf86Msg (X_ERROR,
+ "Invalid Option DataBits value: %d\n", val);
+ return (-1);
+ break;
+ }
+ }
+
+ if ((s = xf86SetStrOption (options, "Parity", NULL)))
+ {
+ if (xf86NameCmp (s, "Odd") == 0)
+ {
+ t.c_cflag |= PARENB | PARODD;
+ }
+ else if (xf86NameCmp (s, "Even") == 0)
+ {
+ t.c_cflag |= PARENB;
+ t.c_cflag &= ~(PARODD);
+ }
+ else if (xf86NameCmp (s, "None") == 0)
+ {
+ t.c_cflag &= ~(PARENB);
+ }
+ else
+ {
+ xf86Msg (X_ERROR, "Invalid Option Parity value: %s\n",
+ s);
+ return (-1);
+ }
+ }
+
+ if ((val = xf86SetIntOption (options, "Vmin", -1)) != -1)
+ {
+ t.c_cc[VMIN] = val;
+ }
+ if ((val = xf86SetIntOption (options, "Vtime", -1)) != -1)
+ {
+ t.c_cc[VTIME] = val;
+ }
+
+ if ((s = xf86SetStrOption (options, "FlowControl", NULL)))
+ {
+ xf86MarkOptionUsedByName (options, "FlowControl");
+ if (xf86NameCmp (s, "Xoff") == 0)
+ {
+ t.c_iflag |= IXOFF;
+ }
+ else if (xf86NameCmp (s, "Xon") == 0)
+ {
+ t.c_iflag |= IXON;
+ }
+ else if (xf86NameCmp (s, "XonXoff") == 0)
+ {
+ t.c_iflag |= IXON|IXOFF;
+ }
+ else if (xf86NameCmp (s, "None") == 0)
+ {
+ t.c_iflag &= ~(IXON | IXOFF);
+ }
+ else
+ {
+ xf86Msg (X_ERROR,
+ "Invalid Option FlowControl value: %s\n", s);
+ return (-1);
+ }
+ }
+
+ if ((xf86SetBoolOption (options, "ClearDTR", FALSE)))
+ {
+#ifdef CLEARDTR_SUPPORT
+# if defined(TIOCMBIC)
+ val = TIOCM_DTR;
+ SYSCALL (ioctl(fd, TIOCMBIC, &val));
+# else
+ SYSCALL (ioctl(fd, TIOCCDTR, NULL));
+# endif
+#else
+ xf86Msg (X_WARNING,
+ "Option ClearDTR not supported on this OS\n");
+ return (-1);
+#endif
+ xf86MarkOptionUsedByName (options, "ClearDTR");
+ }
+
+ if ((xf86SetBoolOption (options, "ClearRTS", FALSE)))
+ {
+ xf86Msg (X_WARNING,
+ "Option ClearRTS not supported on this OS\n");
+ return (-1);
+ xf86MarkOptionUsedByName (options, "ClearRTS");
+ }
+
+ SYSCALL (r = tcsetattr (fd, TCSANOW, &t));
+ return (r);
+}
+
+int
+xf86SetSerialSpeed (int fd, int speed)
+{
+ struct termios t;
+ int baud, r;
+
+ if (fd < 0)
+ return -1;
+
+ /* Don't try to set parameters for non-tty devices. */
+ if (!isatty(fd))
+ return 0;
+
+ SYSCALL (tcgetattr (fd, &t));
+
+ if ((baud = GetBaud (speed)))
+ {
+ cfsetispeed (&t, baud);
+ cfsetospeed (&t, baud);
+ }
+ else
+ {
+ xf86Msg (X_ERROR,
+ "Invalid Option BaudRate value: %d\n", speed);
+ return (-1);
+ }
+
+ SYSCALL (r = tcsetattr (fd, TCSANOW, &t));
+ return (r);
+}
+
+int
+xf86ReadSerial (int fd, void *buf, int count)
+{
+ int r;
+ int i;
+
+ SYSCALL (r = read (fd, buf, count));
+ DebugF("ReadingSerial: 0x%x",
+ (unsigned char)*(((unsigned char *)buf)));
+ for (i = 1; i < r; i++)
+ DebugF(", 0x%x",(unsigned char)*(((unsigned char *)buf) + i));
+ DebugF("\n");
+ return (r);
+}
+
+int
+xf86WriteSerial (int fd, const void *buf, int count)
+{
+ int r;
+ int i;
+
+ DebugF("WritingSerial: 0x%x",(unsigned char)*(((unsigned char *)buf)));
+ for (i = 1; i < count; i++)
+ ErrorF(", 0x%x",(unsigned char)*(((unsigned char *)buf) + i));
+ DebugF("\n");
+ SYSCALL (r = write (fd, buf, count));
+ return (r);
+}
+
+int
+xf86CloseSerial (int fd)
+{
+ int r;
+
+ SYSCALL (r = close (fd));
+ return (r);
+}
+
+int
+xf86WaitForInput (int fd, int timeout)
+{
+ fd_set readfds;
+ struct timeval to;
+ int r;
+
+ FD_ZERO(&readfds);
+
+ if (fd >= 0) {
+ FD_SET(fd, &readfds);
+ }
+
+ to.tv_sec = timeout / 1000000;
+ to.tv_usec = timeout % 1000000;
+
+ if (fd >= 0) {
+ SYSCALL (r = select (FD_SETSIZE, &readfds, NULL, NULL, &to));
+ }
+ else {
+ SYSCALL (r = select (FD_SETSIZE, NULL, NULL, NULL, &to));
+ }
+ xf86ErrorFVerb (9,"select returned %d\n", r);
+ return (r);
+}
+
+int
+xf86SerialSendBreak (int fd, int duration)
+{
+ int r;
+
+ SYSCALL (r = tcsendbreak (fd, duration));
+ return (r);
+
+}
+
+int
+xf86FlushInput(int fd)
+{
+ fd_set fds;
+ struct timeval timeout;
+ char c[4];
+
+ DebugF("FlushingSerial\n");
+ if (tcflush(fd, TCIFLUSH) == 0)
+ return 0;
+
+ timeout.tv_sec = 0;
+ timeout.tv_usec = 0;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ while (select(FD_SETSIZE, &fds, NULL, NULL, &timeout) > 0) {
+ if (read(fd, &c, sizeof(c)) < 1)
+ return 0;
+ FD_ZERO(&fds);
+ FD_SET(fd, &fds);
+ }
+ return 0;
+}
+
+static struct states {
+ int xf;
+ int os;
+} modemStates[] = {
+#ifdef TIOCM_LE
+ { XF86_M_LE, TIOCM_LE },
+#endif
+#ifdef TIOCM_DTR
+ { XF86_M_DTR, TIOCM_DTR },
+#endif
+#ifdef TIOCM_RTS
+ { XF86_M_RTS, TIOCM_RTS },
+#endif
+#ifdef TIOCM_ST
+ { XF86_M_ST, TIOCM_ST },
+#endif
+#ifdef TIOCM_SR
+ { XF86_M_SR, TIOCM_SR },
+#endif
+#ifdef TIOCM_CTS
+ { XF86_M_CTS, TIOCM_CTS },
+#endif
+#ifdef TIOCM_CAR
+ { XF86_M_CAR, TIOCM_CAR },
+#elif defined(TIOCM_CD)
+ { XF86_M_CAR, TIOCM_CD },
+#endif
+#ifdef TIOCM_RNG
+ { XF86_M_RNG, TIOCM_RNG },
+#elif defined(TIOCM_RI)
+ { XF86_M_CAR, TIOCM_RI },
+#endif
+#ifdef TIOCM_DSR
+ { XF86_M_DSR, TIOCM_DSR },
+#endif
+};
+
+static int numStates = sizeof(modemStates) / sizeof(modemStates[0]);
+
+static int
+xf2osState(int state)
+{
+ int i;
+ int ret = 0;
+
+ for (i = 0; i < numStates; i++)
+ if (state & modemStates[i].xf)
+ ret |= modemStates[i].os;
+ return ret;
+}
+
+static int
+os2xfState(int state)
+{
+ int i;
+ int ret = 0;
+
+ for (i = 0; i < numStates; i++)
+ if (state & modemStates[i].os)
+ ret |= modemStates[i].xf;
+ return ret;
+}
+
+static int
+getOsStateMask(void)
+{
+ int i;
+ int ret = 0;
+ for (i = 0; i < numStates; i++)
+ ret |= modemStates[i].os;
+ return ret;
+}
+
+static int osStateMask = 0;
+
+int
+xf86SetSerialModemState(int fd, int state)
+{
+ int ret;
+ int s;
+
+ if (fd < 0)
+ return -1;
+
+ /* Don't try to set parameters for non-tty devices. */
+ if (!isatty(fd))
+ return 0;
+
+#ifndef TIOCMGET
+ return -1;
+#else
+ if (!osStateMask)
+ osStateMask = getOsStateMask();
+
+ state = xf2osState(state);
+ SYSCALL((ret = ioctl(fd, TIOCMGET, &s)));
+ if (ret < 0)
+ return -1;
+ s &= ~osStateMask;
+ s |= state;
+ SYSCALL((ret = ioctl(fd, TIOCMSET, &s)));
+ if (ret < 0)
+ return -1;
+ else
+ return 0;
+#endif
+}
+
+int
+xf86GetSerialModemState(int fd)
+{
+ int ret;
+ int s;
+
+ if (fd < 0)
+ return -1;
+
+ /* Don't try to set parameters for non-tty devices. */
+ if (!isatty(fd))
+ return 0;
+
+#ifndef TIOCMGET
+ return -1;
+#else
+ SYSCALL((ret = ioctl(fd, TIOCMGET, &s)));
+ if (ret < 0)
+ return -1;
+ return os2xfState(s);
+#endif
+}
+
+int
+xf86SerialModemSetBits(int fd, int bits)
+{
+ int ret;
+ int s;
+
+ if (fd < 0)
+ return -1;
+
+ /* Don't try to set parameters for non-tty devices. */
+ if (!isatty(fd))
+ return 0;
+
+#ifndef TIOCMGET
+ return -1;
+#else
+ s = xf2osState(bits);
+ SYSCALL((ret = ioctl(fd, TIOCMBIS, &s)));
+ return ret;
+#endif
+}
+
+int
+xf86SerialModemClearBits(int fd, int bits)
+{
+ int ret;
+ int s;
+
+ if (fd < 0)
+ return -1;
+
+ /* Don't try to set parameters for non-tty devices. */
+ if (!isatty(fd))
+ return 0;
+
+#ifndef TIOCMGET
+ return -1;
+#else
+ s = xf2osState(bits);
+ SYSCALL((ret = ioctl(fd, TIOCMBIC, &s)));
+ return ret;
+#endif
+}
diff --git a/xorg-server/hw/xfree86/os-support/shared/vidmem.c b/xorg-server/hw/xfree86/os-support/shared/vidmem.c index 803ce09bd..6358138d8 100644 --- a/xorg-server/hw/xfree86/os-support/shared/vidmem.c +++ b/xorg-server/hw/xfree86/os-support/shared/vidmem.c @@ -1,296 +1,296 @@ -/* - * Copyright (c) 1993-2003 by The XFree86 Project, Inc. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - * - * Except as contained in this notice, the name of the copyright holder(s) - * and author(s) shall not be used in advertising or otherwise to promote - * the sale, use or other dealings in this Software without prior written - * authorization from the copyright holder(s) and author(s). - */ - - -#ifdef HAVE_XORG_CONFIG_H -#include <xorg-config.h> -#endif - -#include <X11/X.h> -#include "input.h" -#include "scrnintstr.h" - -#include "xf86.h" -#include "xf86Priv.h" -#include "xf86_OSlib.h" -#include "xf86OSpriv.h" - -/* - * This file contains the common part of the video memory mapping functions - */ - -/* - * Get a piece of the ScrnInfoRec. At the moment, this is only used to hold - * the MTRR option information, but it is likely to be expanded if we do - * auto unmapping of memory at VT switch. - * - */ - -typedef struct { - unsigned long physBase; - unsigned long size; - pointer virtBase; - pointer mtrrInfo; - int flags; -} MappingRec, *MappingPtr; - -typedef struct { - int numMappings; - MappingPtr * mappings; - Bool mtrrEnabled; - MessageType mtrrFrom; - Bool mtrrOptChecked; - ScrnInfoPtr pScrn; -} VidMapRec, *VidMapPtr; - -static int vidMapIndex = -1; - -#define VIDMAPPTR(p) ((VidMapPtr)((p)->privates[vidMapIndex].ptr)) - -static VidMemInfo vidMemInfo = {FALSE, }; -static VidMapRec vidMapRec = {0, NULL, TRUE, X_DEFAULT, FALSE, NULL}; - -static VidMapPtr -getVidMapRec(int scrnIndex) -{ - VidMapPtr vp; - ScrnInfoPtr pScrn; - - if ((scrnIndex < 0) || - !(pScrn = xf86Screens[scrnIndex])) - return &vidMapRec; - - if (vidMapIndex < 0) - vidMapIndex = xf86AllocateScrnInfoPrivateIndex(); - - if (VIDMAPPTR(pScrn) != NULL) - return VIDMAPPTR(pScrn); - - vp = pScrn->privates[vidMapIndex].ptr = xnfcalloc(sizeof(VidMapRec), 1); - vp->mtrrEnabled = TRUE; /* default to enabled */ - vp->mtrrFrom = X_DEFAULT; - vp->mtrrOptChecked = FALSE; - vp->pScrn = pScrn; - return vp; -} - -static MappingPtr -newMapping(VidMapPtr vp) -{ - vp->mappings = xnfrealloc(vp->mappings, sizeof(MappingPtr) * - (vp->numMappings + 1)); - vp->mappings[vp->numMappings] = xnfcalloc(sizeof(MappingRec), 1); - return vp->mappings[vp->numMappings++]; -} - -static MappingPtr -findMapping(VidMapPtr vp, pointer vbase, unsigned long size) -{ - int i; - - for (i = 0; i < vp->numMappings; i++) { - if (vp->mappings[i]->virtBase == vbase && - vp->mappings[i]->size == size) - return vp->mappings[i]; - } - return NULL; -} - -static void -removeMapping(VidMapPtr vp, MappingPtr mp) -{ - int i, found = 0; - - for (i = 0; i < vp->numMappings; i++) { - if (vp->mappings[i] == mp) { - found = 1; - xfree(vp->mappings[i]); - } else if (found) { - vp->mappings[i - 1] = vp->mappings[i]; - } - } - vp->numMappings--; - vp->mappings[vp->numMappings] = NULL; -} - -enum { OPTION_MTRR }; -static const OptionInfoRec opts[] = -{ - { OPTION_MTRR, "mtrr", OPTV_BOOLEAN, {0}, FALSE }, - { -1, NULL, OPTV_NONE, {0}, FALSE } -}; - -static void -checkMtrrOption(VidMapPtr vp) -{ - if (!vp->mtrrOptChecked && vp->pScrn && vp->pScrn->options != NULL) { - OptionInfoPtr options; - - options = xnfalloc(sizeof(opts)); - (void)memcpy(options, opts, sizeof(opts)); - xf86ProcessOptions(vp->pScrn->scrnIndex, vp->pScrn->options, - options); - if (xf86GetOptValBool(options, OPTION_MTRR, &vp->mtrrEnabled)) - vp->mtrrFrom = X_CONFIG; - xfree(options); - vp->mtrrOptChecked = TRUE; - } -} - -void -xf86MakeNewMapping(int ScreenNum, int Flags, unsigned long Base, unsigned long Size, pointer Vbase) -{ - VidMapPtr vp; - MappingPtr mp; - - vp = getVidMapRec(ScreenNum); - mp = newMapping(vp); - mp->physBase = Base; - mp->size = Size; - mp->virtBase = Vbase; - mp->flags = Flags; -} - -void -xf86InitVidMem(void) -{ - if (!vidMemInfo.initialised) { - memset(&vidMemInfo, 0, sizeof(VidMemInfo)); - xf86OSInitVidMem(&vidMemInfo); - } -} - -pointer -xf86MapVidMem(int ScreenNum, int Flags, unsigned long Base, unsigned long Size) -{ - pointer vbase = NULL; - VidMapPtr vp; - MappingPtr mp; - - if (((Flags & VIDMEM_FRAMEBUFFER) && - (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT)))) - FatalError("Mapping memory with more than one type\n"); - - xf86InitVidMem(); - if (!vidMemInfo.initialised || !vidMemInfo.mapMem) - return NULL; - - vbase = vidMemInfo.mapMem(ScreenNum, Base, Size, Flags); - - if (!vbase || vbase == (pointer)-1) - return NULL; - - vp = getVidMapRec(ScreenNum); - mp = newMapping(vp); - mp->physBase = Base; - mp->size = Size; - mp->virtBase = vbase; - mp->flags = Flags; - - /* - * Check the "mtrr" option even when MTRR isn't supported to avoid - * warnings about unrecognised options. - */ - checkMtrrOption(vp); - - if (vp->mtrrEnabled && vidMemInfo.setWC) { - if (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT)) - mp->mtrrInfo = - vidMemInfo.setWC(ScreenNum, Base, Size, FALSE, - vp->mtrrFrom); - else if (Flags & VIDMEM_FRAMEBUFFER) - mp->mtrrInfo = - vidMemInfo.setWC(ScreenNum, Base, Size, TRUE, - vp->mtrrFrom); - } - return vbase; -} - -void -xf86UnMapVidMem(int ScreenNum, pointer Base, unsigned long Size) -{ - VidMapPtr vp; - MappingPtr mp; - - if (!vidMemInfo.initialised || !vidMemInfo.unmapMem) { - xf86DrvMsg(ScreenNum, X_WARNING, - "xf86UnMapVidMem() called before xf86MapVidMem()\n"); - return; - } - - vp = getVidMapRec(ScreenNum); - mp = findMapping(vp, Base, Size); - if (!mp) { - xf86DrvMsg(ScreenNum, X_WARNING, - "xf86UnMapVidMem: cannot find region for [%p,0x%lx]\n", - Base, Size); - return; - } - if (vp->mtrrEnabled && vidMemInfo.undoWC && mp) - vidMemInfo.undoWC(ScreenNum, mp->mtrrInfo); - - vidMemInfo.unmapMem(ScreenNum, Base, Size); - removeMapping(vp, mp); -} - -Bool -xf86CheckMTRR(int ScreenNum) -{ - VidMapPtr vp = getVidMapRec(ScreenNum); - - /* - * Check the "mtrr" option even when MTRR isn't supported to avoid - * warnings about unrecognised options. - */ - checkMtrrOption(vp); - - if (vp->mtrrEnabled && vidMemInfo.setWC) - return TRUE; - - return FALSE; -} - -Bool -xf86LinearVidMem(void) -{ - xf86InitVidMem(); - return vidMemInfo.linearSupported; -} - -void -xf86MapReadSideEffects(int ScreenNum, int Flags, pointer base, - unsigned long Size) -{ - if (!(Flags & VIDMEM_READSIDEEFFECT)) - return; - - if (!vidMemInfo.initialised || !vidMemInfo.readSideEffects) - return; - - vidMemInfo.readSideEffects(ScreenNum, base, Size); -} - +/*
+ * Copyright (c) 1993-2003 by The XFree86 Project, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * Except as contained in this notice, the name of the copyright holder(s)
+ * and author(s) shall not be used in advertising or otherwise to promote
+ * the sale, use or other dealings in this Software without prior written
+ * authorization from the copyright holder(s) and author(s).
+ */
+
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include <X11/X.h>
+#include "input.h"
+#include "scrnintstr.h"
+
+#include "xf86.h"
+#include "xf86Priv.h"
+#include "xf86_OSlib.h"
+#include "xf86OSpriv.h"
+
+/*
+ * This file contains the common part of the video memory mapping functions
+ */
+
+/*
+ * Get a piece of the ScrnInfoRec. At the moment, this is only used to hold
+ * the MTRR option information, but it is likely to be expanded if we do
+ * auto unmapping of memory at VT switch.
+ *
+ */
+
+typedef struct {
+ unsigned long physBase;
+ unsigned long size;
+ pointer virtBase;
+ pointer mtrrInfo;
+ int flags;
+} MappingRec, *MappingPtr;
+
+typedef struct {
+ int numMappings;
+ MappingPtr * mappings;
+ Bool mtrrEnabled;
+ MessageType mtrrFrom;
+ Bool mtrrOptChecked;
+ ScrnInfoPtr pScrn;
+} VidMapRec, *VidMapPtr;
+
+static int vidMapIndex = -1;
+
+#define VIDMAPPTR(p) ((VidMapPtr)((p)->privates[vidMapIndex].ptr))
+
+static VidMemInfo vidMemInfo = {FALSE, };
+static VidMapRec vidMapRec = {0, NULL, TRUE, X_DEFAULT, FALSE, NULL};
+
+static VidMapPtr
+getVidMapRec(int scrnIndex)
+{
+ VidMapPtr vp;
+ ScrnInfoPtr pScrn;
+
+ if ((scrnIndex < 0) ||
+ !(pScrn = xf86Screens[scrnIndex]))
+ return &vidMapRec;
+
+ if (vidMapIndex < 0)
+ vidMapIndex = xf86AllocateScrnInfoPrivateIndex();
+
+ if (VIDMAPPTR(pScrn) != NULL)
+ return VIDMAPPTR(pScrn);
+
+ vp = pScrn->privates[vidMapIndex].ptr = xnfcalloc(sizeof(VidMapRec), 1);
+ vp->mtrrEnabled = TRUE; /* default to enabled */
+ vp->mtrrFrom = X_DEFAULT;
+ vp->mtrrOptChecked = FALSE;
+ vp->pScrn = pScrn;
+ return vp;
+}
+
+static MappingPtr
+newMapping(VidMapPtr vp)
+{
+ vp->mappings = xnfrealloc(vp->mappings, sizeof(MappingPtr) *
+ (vp->numMappings + 1));
+ vp->mappings[vp->numMappings] = xnfcalloc(sizeof(MappingRec), 1);
+ return vp->mappings[vp->numMappings++];
+}
+
+static MappingPtr
+findMapping(VidMapPtr vp, pointer vbase, unsigned long size)
+{
+ int i;
+
+ for (i = 0; i < vp->numMappings; i++) {
+ if (vp->mappings[i]->virtBase == vbase &&
+ vp->mappings[i]->size == size)
+ return vp->mappings[i];
+ }
+ return NULL;
+}
+
+static void
+removeMapping(VidMapPtr vp, MappingPtr mp)
+{
+ int i, found = 0;
+
+ for (i = 0; i < vp->numMappings; i++) {
+ if (vp->mappings[i] == mp) {
+ found = 1;
+ free(vp->mappings[i]);
+ } else if (found) {
+ vp->mappings[i - 1] = vp->mappings[i];
+ }
+ }
+ vp->numMappings--;
+ vp->mappings[vp->numMappings] = NULL;
+}
+
+enum { OPTION_MTRR };
+static const OptionInfoRec opts[] =
+{
+ { OPTION_MTRR, "mtrr", OPTV_BOOLEAN, {0}, FALSE },
+ { -1, NULL, OPTV_NONE, {0}, FALSE }
+};
+
+static void
+checkMtrrOption(VidMapPtr vp)
+{
+ if (!vp->mtrrOptChecked && vp->pScrn && vp->pScrn->options != NULL) {
+ OptionInfoPtr options;
+
+ options = xnfalloc(sizeof(opts));
+ (void)memcpy(options, opts, sizeof(opts));
+ xf86ProcessOptions(vp->pScrn->scrnIndex, vp->pScrn->options,
+ options);
+ if (xf86GetOptValBool(options, OPTION_MTRR, &vp->mtrrEnabled))
+ vp->mtrrFrom = X_CONFIG;
+ free(options);
+ vp->mtrrOptChecked = TRUE;
+ }
+}
+
+void
+xf86MakeNewMapping(int ScreenNum, int Flags, unsigned long Base, unsigned long Size, pointer Vbase)
+{
+ VidMapPtr vp;
+ MappingPtr mp;
+
+ vp = getVidMapRec(ScreenNum);
+ mp = newMapping(vp);
+ mp->physBase = Base;
+ mp->size = Size;
+ mp->virtBase = Vbase;
+ mp->flags = Flags;
+}
+
+void
+xf86InitVidMem(void)
+{
+ if (!vidMemInfo.initialised) {
+ memset(&vidMemInfo, 0, sizeof(VidMemInfo));
+ xf86OSInitVidMem(&vidMemInfo);
+ }
+}
+
+pointer
+xf86MapVidMem(int ScreenNum, int Flags, unsigned long Base, unsigned long Size)
+{
+ pointer vbase = NULL;
+ VidMapPtr vp;
+ MappingPtr mp;
+
+ if (((Flags & VIDMEM_FRAMEBUFFER) &&
+ (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT))))
+ FatalError("Mapping memory with more than one type\n");
+
+ xf86InitVidMem();
+ if (!vidMemInfo.initialised || !vidMemInfo.mapMem)
+ return NULL;
+
+ vbase = vidMemInfo.mapMem(ScreenNum, Base, Size, Flags);
+
+ if (!vbase || vbase == (pointer)-1)
+ return NULL;
+
+ vp = getVidMapRec(ScreenNum);
+ mp = newMapping(vp);
+ mp->physBase = Base;
+ mp->size = Size;
+ mp->virtBase = vbase;
+ mp->flags = Flags;
+
+ /*
+ * Check the "mtrr" option even when MTRR isn't supported to avoid
+ * warnings about unrecognised options.
+ */
+ checkMtrrOption(vp);
+
+ if (vp->mtrrEnabled && vidMemInfo.setWC) {
+ if (Flags & (VIDMEM_MMIO | VIDMEM_MMIO_32BIT))
+ mp->mtrrInfo =
+ vidMemInfo.setWC(ScreenNum, Base, Size, FALSE,
+ vp->mtrrFrom);
+ else if (Flags & VIDMEM_FRAMEBUFFER)
+ mp->mtrrInfo =
+ vidMemInfo.setWC(ScreenNum, Base, Size, TRUE,
+ vp->mtrrFrom);
+ }
+ return vbase;
+}
+
+void
+xf86UnMapVidMem(int ScreenNum, pointer Base, unsigned long Size)
+{
+ VidMapPtr vp;
+ MappingPtr mp;
+
+ if (!vidMemInfo.initialised || !vidMemInfo.unmapMem) {
+ xf86DrvMsg(ScreenNum, X_WARNING,
+ "xf86UnMapVidMem() called before xf86MapVidMem()\n");
+ return;
+ }
+
+ vp = getVidMapRec(ScreenNum);
+ mp = findMapping(vp, Base, Size);
+ if (!mp) {
+ xf86DrvMsg(ScreenNum, X_WARNING,
+ "xf86UnMapVidMem: cannot find region for [%p,0x%lx]\n",
+ Base, Size);
+ return;
+ }
+ if (vp->mtrrEnabled && vidMemInfo.undoWC && mp)
+ vidMemInfo.undoWC(ScreenNum, mp->mtrrInfo);
+
+ vidMemInfo.unmapMem(ScreenNum, Base, Size);
+ removeMapping(vp, mp);
+}
+
+Bool
+xf86CheckMTRR(int ScreenNum)
+{
+ VidMapPtr vp = getVidMapRec(ScreenNum);
+
+ /*
+ * Check the "mtrr" option even when MTRR isn't supported to avoid
+ * warnings about unrecognised options.
+ */
+ checkMtrrOption(vp);
+
+ if (vp->mtrrEnabled && vidMemInfo.setWC)
+ return TRUE;
+
+ return FALSE;
+}
+
+Bool
+xf86LinearVidMem(void)
+{
+ xf86InitVidMem();
+ return vidMemInfo.linearSupported;
+}
+
+void
+xf86MapReadSideEffects(int ScreenNum, int Flags, pointer base,
+ unsigned long Size)
+{
+ if (!(Flags & VIDMEM_READSIDEEFFECT))
+ return;
+
+ if (!vidMemInfo.initialised || !vidMemInfo.readSideEffects)
+ return;
+
+ vidMemInfo.readSideEffects(ScreenNum, base, Size);
+}
+
diff --git a/xorg-server/hw/xfree86/os-support/solaris/sun_agp.c b/xorg-server/hw/xfree86/os-support/solaris/sun_agp.c index e6a55a48b..a5fb5c6e6 100644 --- a/xorg-server/hw/xfree86/os-support/solaris/sun_agp.c +++ b/xorg-server/hw/xfree86/os-support/solaris/sun_agp.c @@ -1,327 +1,327 @@ -/* - * Abstraction of the AGP GART interface. - * - * This version is for Solaris. - * - * Copyright © 2000 VA Linux Systems, Inc. - * Copyright © 2001 The XFree86 Project, Inc. - */ -/* Copyright 2005 Sun Microsystems, Inc. All rights reserved. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice (including the next - * paragraph) shall be included in all copies or substantial portions of the - * Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -#ifdef HAVE_XORG_CONFIG_H -#include <xorg-config.h> -#endif - -#include <X11/X.h> -#include "xf86.h" -#include "xf86Priv.h" -#include "xf86_OSlib.h" -#include "xf86_OSproc.h" -#include <unistd.h> -#include <sys/ioccom.h> -#include <sys/types.h> -#include <fcntl.h> -#include <sys/agpgart.h> - -/* AGP page size is independent of the host page size. */ -#ifndef AGP_PAGE_SIZE -#define AGP_PAGE_SIZE 4096 -#endif - -static int gartFd = -1; -static int acquiredScreen = -1; -static Bool initDone = FALSE; -/* - * Close /dev/agpgart. This frees all associated memory allocated during - * this server generation. - */ -Bool -xf86GARTCloseScreen(int screenNum) -{ - if (gartFd != -1) { - close(gartFd); - acquiredScreen = -1; - gartFd = -1; - initDone = FALSE; - - xf86DrvMsg(screenNum, X_INFO, - "xf86GARTCloseScreen: device closed successfully\n"); - - } - return TRUE; -} - -/* - * Open /dev/agpgart. Keep it open until xf86GARTCloseScreen is called. - */ -static Bool -GARTInit(int screenNum) -{ - if (initDone) - return (gartFd != -1); - - if (gartFd == -1) - gartFd = open(AGP_DEVICE, O_RDWR); - else - return FALSE; - - if (gartFd == -1) { - xf86DrvMsg(screenNum, X_ERROR, - "GARTInit: Unable to open " AGP_DEVICE " (%s)\n", - strerror(errno)); - return FALSE; - } - - initDone = TRUE; - xf86DrvMsg(screenNum, X_INFO, - "GARTInit: " AGP_DEVICE " opened successfully\n"); - - return TRUE; -} - -Bool -xf86AgpGARTSupported(void) -{ - return (GARTInit(-1)); - -} - -AgpInfoPtr -xf86GetAGPInfo(int screenNum) -{ - agp_info_t agpinf; - AgpInfoPtr info; - - if (!GARTInit(screenNum)) - return NULL; - - if ((info = xcalloc(sizeof(AgpInfo), 1)) == NULL) { - xf86DrvMsg(screenNum, X_ERROR, - "xf86GetAGPInfo: Failed to allocate AgpInfo\n"); - return NULL; - } - - if (ioctl(gartFd, AGPIOC_INFO, &agpinf) != 0) { - xf86DrvMsg(screenNum, X_ERROR, - "xf86GetAGPInfo: AGPIOC_INFO failed (%s)\n", - strerror(errno)); - return NULL; - } - - info->bridgeId = agpinf.agpi_devid; - info->agpMode = agpinf.agpi_mode; - info->base = agpinf.agpi_aperbase; - info->size = agpinf.agpi_apersize; - info->totalPages = (unsigned long)agpinf.agpi_pgtotal; - info->systemPages = (unsigned long)agpinf.agpi_pgsystem; - info->usedPages = (unsigned long)agpinf.agpi_pgused; - - return info; -} - -Bool -xf86AcquireGART(int screenNum) -{ - - if (!GARTInit(screenNum)) - return FALSE; - - if (acquiredScreen != screenNum) { - if (ioctl(gartFd, AGPIOC_ACQUIRE, 0) != 0) { - xf86DrvMsg(screenNum, X_WARNING, - "xf86AcquireGART: AGPIOC_ACQUIRE failed (%s)\n", - strerror(errno)); - return FALSE; - } - acquiredScreen = screenNum; - xf86DrvMsg(screenNum, X_INFO, - "xf86AcquireGART: AGPIOC_ACQUIRE succeeded\n"); - } - return TRUE; -} - -Bool -xf86ReleaseGART(int screenNum) -{ - - if (!GARTInit(screenNum)) - return FALSE; - - if (acquiredScreen == screenNum) { - /* - * The FreeBSD agp driver removes allocations on release. - * The Solaris driver doesn't. xf86ReleaseGART() is expected - * to give up access to the GART, but not to remove any - * allocations. - */ - - if (ioctl(gartFd, AGPIOC_RELEASE, 0) != 0) { - xf86DrvMsg(screenNum, X_WARNING, - "xf86ReleaseGART: AGPIOC_RELEASE failed (%s)\n", - strerror(errno)); - return FALSE; - } - acquiredScreen = -1; - xf86DrvMsg(screenNum, X_INFO, - "xf86ReleaseGART: AGPIOC_RELEASE succeeded\n"); - return TRUE; - } - return FALSE; -} - -int -xf86AllocateGARTMemory(int screenNum, unsigned long size, int type, - unsigned long *physical) -{ - agp_allocate_t alloc; - int pages; - - /* - * Allocates "size" bytes of GART memory (rounds up to the next - * page multiple) or type "type". A handle (key) for the allocated - * memory is returned. On error, the return value is -1. - * "size" should be larger than 0, or AGPIOC_ALLOCATE ioctl will - * return error. - */ - - if (!GARTInit(screenNum) || (acquiredScreen != screenNum)) - return -1; - - pages = (size / AGP_PAGE_SIZE); - if (size % AGP_PAGE_SIZE != 0) - pages++; - - alloc.agpa_pgcount = pages; - alloc.agpa_type = type; - - if (ioctl(gartFd, AGPIOC_ALLOCATE, &alloc) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86AllocateGARTMemory: " - "allocation of %d pages failed\n\t(%s)\n", pages, - strerror(errno)); - return -1; - } - - if (physical) - *physical = (unsigned long)alloc.agpa_physical; - - return alloc.agpa_key; -} - -Bool -xf86DeallocateGARTMemory(int screenNum, int key) -{ - if (!GARTInit(screenNum) || (acquiredScreen != screenNum)) - return FALSE; - - if (ioctl(gartFd, AGPIOC_DEALLOCATE, (int *)key) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86DeAllocateGARTMemory: " - "deallocation of gart memory with key %d failed\n" - "\t(%s)\n", key, strerror(errno)); - return FALSE; - } - - return TRUE; -} - -/* Bind GART memory with "key" at "offset" */ -Bool -xf86BindGARTMemory(int screenNum, int key, unsigned long offset) -{ - agp_bind_t bind; - int pageOffset; - - if (!GARTInit(screenNum) || (acquiredScreen != screenNum)) - return FALSE; - - if (offset % AGP_PAGE_SIZE != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86BindGARTMemory: " - "offset (0x%lx) is not page-aligned (%d)\n", - offset, AGP_PAGE_SIZE); - return FALSE; - } - pageOffset = offset / AGP_PAGE_SIZE; - - xf86DrvMsgVerb(screenNum, X_INFO, 3, - "xf86BindGARTMemory: bind key %d at 0x%08lx " - "(pgoffset %d)\n", key, offset, pageOffset); - - bind.agpb_pgstart = pageOffset; - bind.agpb_key = key; - - if (ioctl(gartFd, AGPIOC_BIND, &bind) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86BindGARTMemory: " - "binding of gart memory with key %d\n" - "\tat offset 0x%lx failed (%s)\n", - key, offset, strerror(errno)); - return FALSE; - } - - return TRUE; -} - -/* Unbind GART memory with "key" */ -Bool -xf86UnbindGARTMemory(int screenNum, int key) -{ - agp_unbind_t unbind; - - if (!GARTInit(screenNum) || (acquiredScreen != screenNum)) - return FALSE; - - unbind.agpu_pri = 0; - unbind.agpu_key = key; - - if (ioctl(gartFd, AGPIOC_UNBIND, &unbind) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86UnbindGARTMemory: " - "unbinding of gart memory with key %d " - "failed (%s)\n", key, strerror(errno)); - return FALSE; - } - - xf86DrvMsgVerb(screenNum, X_INFO, 3, - "xf86UnbindGARTMemory: unbind key %d\n", key); - - return TRUE; -} - - -/* XXX Interface may change. */ -Bool -xf86EnableAGP(int screenNum, CARD32 mode) -{ - agp_setup_t setup; - - if (!GARTInit(screenNum) || (acquiredScreen != screenNum)) - return FALSE; - - setup.agps_mode = mode; - if (ioctl(gartFd, AGPIOC_SETUP, &setup) != 0) { - xf86DrvMsg(screenNum, X_WARNING, "xf86EnableAGP: " - "AGPIOC_SETUP with mode %x failed (%s)\n", - mode, strerror(errno)); - return FALSE; - } - - return TRUE; -} - +/*
+ * Abstraction of the AGP GART interface.
+ *
+ * This version is for Solaris.
+ *
+ * Copyright © 2000 VA Linux Systems, Inc.
+ * Copyright © 2001 The XFree86 Project, Inc.
+ */
+/* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#ifdef HAVE_XORG_CONFIG_H
+#include <xorg-config.h>
+#endif
+
+#include <X11/X.h>
+#include "xf86.h"
+#include "xf86Priv.h"
+#include "xf86_OSlib.h"
+#include "xf86_OSproc.h"
+#include <unistd.h>
+#include <sys/ioccom.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <sys/agpgart.h>
+
+/* AGP page size is independent of the host page size. */
+#ifndef AGP_PAGE_SIZE
+#define AGP_PAGE_SIZE 4096
+#endif
+
+static int gartFd = -1;
+static int acquiredScreen = -1;
+static Bool initDone = FALSE;
+/*
+ * Close /dev/agpgart. This frees all associated memory allocated during
+ * this server generation.
+ */
+Bool
+xf86GARTCloseScreen(int screenNum)
+{
+ if (gartFd != -1) {
+ close(gartFd);
+ acquiredScreen = -1;
+ gartFd = -1;
+ initDone = FALSE;
+
+ xf86DrvMsg(screenNum, X_INFO,
+ "xf86GARTCloseScreen: device closed successfully\n");
+
+ }
+ return TRUE;
+}
+
+/*
+ * Open /dev/agpgart. Keep it open until xf86GARTCloseScreen is called.
+ */
+static Bool
+GARTInit(int screenNum)
+{
+ if (initDone)
+ return (gartFd != -1);
+
+ if (gartFd == -1)
+ gartFd = open(AGP_DEVICE, O_RDWR);
+ else
+ return FALSE;
+
+ if (gartFd == -1) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "GARTInit: Unable to open " AGP_DEVICE " (%s)\n",
+ strerror(errno));
+ return FALSE;
+ }
+
+ initDone = TRUE;
+ xf86DrvMsg(screenNum, X_INFO,
+ "GARTInit: " AGP_DEVICE " opened successfully\n");
+
+ return TRUE;
+}
+
+Bool
+xf86AgpGARTSupported(void)
+{
+ return (GARTInit(-1));
+
+}
+
+AgpInfoPtr
+xf86GetAGPInfo(int screenNum)
+{
+ agp_info_t agpinf;
+ AgpInfoPtr info;
+
+ if (!GARTInit(screenNum))
+ return NULL;
+
+ if ((info = calloc(sizeof(AgpInfo), 1)) == NULL) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "xf86GetAGPInfo: Failed to allocate AgpInfo\n");
+ return NULL;
+ }
+
+ if (ioctl(gartFd, AGPIOC_INFO, &agpinf) != 0) {
+ xf86DrvMsg(screenNum, X_ERROR,
+ "xf86GetAGPInfo: AGPIOC_INFO failed (%s)\n",
+ strerror(errno));
+ return NULL;
+ }
+
+ info->bridgeId = agpinf.agpi_devid;
+ info->agpMode = agpinf.agpi_mode;
+ info->base = agpinf.agpi_aperbase;
+ info->size = agpinf.agpi_apersize;
+ info->totalPages = (unsigned long)agpinf.agpi_pgtotal;
+ info->systemPages = (unsigned long)agpinf.agpi_pgsystem;
+ info->usedPages = (unsigned long)agpinf.agpi_pgused;
+
+ return info;
+}
+
+Bool
+xf86AcquireGART(int screenNum)
+{
+
+ if (!GARTInit(screenNum))
+ return FALSE;
+
+ if (acquiredScreen != screenNum) {
+ if (ioctl(gartFd, AGPIOC_ACQUIRE, 0) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING,
+ "xf86AcquireGART: AGPIOC_ACQUIRE failed (%s)\n",
+ strerror(errno));
+ return FALSE;
+ }
+ acquiredScreen = screenNum;
+ xf86DrvMsg(screenNum, X_INFO,
+ "xf86AcquireGART: AGPIOC_ACQUIRE succeeded\n");
+ }
+ return TRUE;
+}
+
+Bool
+xf86ReleaseGART(int screenNum)
+{
+
+ if (!GARTInit(screenNum))
+ return FALSE;
+
+ if (acquiredScreen == screenNum) {
+ /*
+ * The FreeBSD agp driver removes allocations on release.
+ * The Solaris driver doesn't. xf86ReleaseGART() is expected
+ * to give up access to the GART, but not to remove any
+ * allocations.
+ */
+
+ if (ioctl(gartFd, AGPIOC_RELEASE, 0) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING,
+ "xf86ReleaseGART: AGPIOC_RELEASE failed (%s)\n",
+ strerror(errno));
+ return FALSE;
+ }
+ acquiredScreen = -1;
+ xf86DrvMsg(screenNum, X_INFO,
+ "xf86ReleaseGART: AGPIOC_RELEASE succeeded\n");
+ return TRUE;
+ }
+ return FALSE;
+}
+
+int
+xf86AllocateGARTMemory(int screenNum, unsigned long size, int type,
+ unsigned long *physical)
+{
+ agp_allocate_t alloc;
+ int pages;
+
+ /*
+ * Allocates "size" bytes of GART memory (rounds up to the next
+ * page multiple) or type "type". A handle (key) for the allocated
+ * memory is returned. On error, the return value is -1.
+ * "size" should be larger than 0, or AGPIOC_ALLOCATE ioctl will
+ * return error.
+ */
+
+ if (!GARTInit(screenNum) || (acquiredScreen != screenNum))
+ return -1;
+
+ pages = (size / AGP_PAGE_SIZE);
+ if (size % AGP_PAGE_SIZE != 0)
+ pages++;
+
+ alloc.agpa_pgcount = pages;
+ alloc.agpa_type = type;
+
+ if (ioctl(gartFd, AGPIOC_ALLOCATE, &alloc) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86AllocateGARTMemory: "
+ "allocation of %d pages failed\n\t(%s)\n", pages,
+ strerror(errno));
+ return -1;
+ }
+
+ if (physical)
+ *physical = (unsigned long)alloc.agpa_physical;
+
+ return alloc.agpa_key;
+}
+
+Bool
+xf86DeallocateGARTMemory(int screenNum, int key)
+{
+ if (!GARTInit(screenNum) || (acquiredScreen != screenNum))
+ return FALSE;
+
+ if (ioctl(gartFd, AGPIOC_DEALLOCATE, (int *)key) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86DeAllocateGARTMemory: "
+ "deallocation of gart memory with key %d failed\n"
+ "\t(%s)\n", key, strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* Bind GART memory with "key" at "offset" */
+Bool
+xf86BindGARTMemory(int screenNum, int key, unsigned long offset)
+{
+ agp_bind_t bind;
+ int pageOffset;
+
+ if (!GARTInit(screenNum) || (acquiredScreen != screenNum))
+ return FALSE;
+
+ if (offset % AGP_PAGE_SIZE != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86BindGARTMemory: "
+ "offset (0x%lx) is not page-aligned (%d)\n",
+ offset, AGP_PAGE_SIZE);
+ return FALSE;
+ }
+ pageOffset = offset / AGP_PAGE_SIZE;
+
+ xf86DrvMsgVerb(screenNum, X_INFO, 3,
+ "xf86BindGARTMemory: bind key %d at 0x%08lx "
+ "(pgoffset %d)\n", key, offset, pageOffset);
+
+ bind.agpb_pgstart = pageOffset;
+ bind.agpb_key = key;
+
+ if (ioctl(gartFd, AGPIOC_BIND, &bind) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86BindGARTMemory: "
+ "binding of gart memory with key %d\n"
+ "\tat offset 0x%lx failed (%s)\n",
+ key, offset, strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* Unbind GART memory with "key" */
+Bool
+xf86UnbindGARTMemory(int screenNum, int key)
+{
+ agp_unbind_t unbind;
+
+ if (!GARTInit(screenNum) || (acquiredScreen != screenNum))
+ return FALSE;
+
+ unbind.agpu_pri = 0;
+ unbind.agpu_key = key;
+
+ if (ioctl(gartFd, AGPIOC_UNBIND, &unbind) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86UnbindGARTMemory: "
+ "unbinding of gart memory with key %d "
+ "failed (%s)\n", key, strerror(errno));
+ return FALSE;
+ }
+
+ xf86DrvMsgVerb(screenNum, X_INFO, 3,
+ "xf86UnbindGARTMemory: unbind key %d\n", key);
+
+ return TRUE;
+}
+
+
+/* XXX Interface may change. */
+Bool
+xf86EnableAGP(int screenNum, CARD32 mode)
+{
+ agp_setup_t setup;
+
+ if (!GARTInit(screenNum) || (acquiredScreen != screenNum))
+ return FALSE;
+
+ setup.agps_mode = mode;
+ if (ioctl(gartFd, AGPIOC_SETUP, &setup) != 0) {
+ xf86DrvMsg(screenNum, X_WARNING, "xf86EnableAGP: "
+ "AGPIOC_SETUP with mode %x failed (%s)\n",
+ mode, strerror(errno));
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
|