First import
[xorg_rtime.git] / xorg-server-1.4 / hw / kdrive / src / kmap.c
blobb92c1a84feac83ddfdcfa66d7bcbea38579d0df8
1 /*
2 * Copyright © 1999 Keith Packard
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of Keith Packard not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission. Keith Packard makes no
11 * representations about the suitability of this software for any purpose. It
12 * is provided "as is" without express or implied warranty.
14 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20 * PERFORMANCE OF THIS SOFTWARE.
23 #include <kdrive-config.h>
24 #include "kdrive.h"
26 #include <errno.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29 #ifdef HAVE_ASM_MTRR_H
30 #include <asm/mtrr.h>
31 #endif
33 #include <sys/ioctl.h>
35 void *
36 KdMapDevice (CARD32 addr, CARD32 size)
38 #ifdef WINDOWS
39 void *a;
40 void *d;
42 d = VirtualAlloc (NULL, size, MEM_RESERVE, PAGE_NOACCESS);
43 if (!d)
44 return NULL;
45 DRAW_DEBUG ((DEBUG_S3INIT, "Virtual address of 0x%x is 0x%x", addr, d));
46 a = VirtualCopyAddr (addr);
47 DRAW_DEBUG ((DEBUG_S3INIT, "Translated address is 0x%x", a));
48 if (!VirtualCopy (d, a, size,
49 PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL))
51 DRAW_DEBUG ((DEBUG_FAILURE, "VirtualCopy failed %d",
52 GetLastError ()));
53 return NULL;
55 DRAW_DEBUG ((DEBUG_S3INIT, "Device mapped successfully"));
56 return d;
57 #endif
58 #ifdef linux
59 void *a;
60 int fd;
62 #ifdef __arm__
63 fd = open ("/dev/mem", O_RDWR|O_SYNC);
64 #else
65 fd = open ("/dev/mem", O_RDWR);
66 #endif
67 if (fd < 0)
68 FatalError ("KdMapDevice: failed to open /dev/mem (%s)\n",
69 strerror (errno));
71 a = mmap ((caddr_t) 0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, addr);
72 close (fd);
73 if ((long) a == -1)
74 FatalError ("KdMapDevice: failed to map frame buffer (%s)\n",
75 strerror (errno));
76 return a;
77 #endif
78 #ifdef VXWORKS
79 return (void *) addr;
80 #endif
83 void
84 KdUnmapDevice (void *addr, CARD32 size)
86 #ifdef WINDOWS
87 VirtualFree (addr, size, MEM_DECOMMIT);
88 VirtualFree (addr, 0, MEM_RELEASE);
89 #endif
90 #ifdef linux
91 munmap (addr, size);
92 #endif
93 #ifdef VXWORKS
95 #endif
98 #ifdef HAVE_ASM_MTRR_H
99 static int mtrr;
100 #endif
102 void
103 KdSetMappedMode (CARD32 addr, CARD32 size, int mode)
105 #ifdef HAVE_ASM_MTRR_H
106 struct mtrr_sentry sentry;
107 unsigned long base, bound;
108 unsigned int type = MTRR_TYPE_WRBACK;
110 if (addr < 0x100000)
111 return;
112 if (!mtrr)
113 mtrr = open ("/proc/mtrr", 2);
114 if (mtrr > 0)
116 unsigned long nsize;
117 base = addr & ~((1<<22)-1);
118 bound = ((addr + size) + ((1<<22) - 1)) & ~((1<<22) - 1);
119 nsize = 1;
120 while (nsize < (bound - base))
121 nsize <<= 1;
122 switch (mode) {
123 case KD_MAPPED_MODE_REGISTERS:
124 type = MTRR_TYPE_UNCACHABLE;
125 break;
126 case KD_MAPPED_MODE_FRAMEBUFFER:
127 type = MTRR_TYPE_WRCOMB;
128 break;
130 sentry.base = base;
131 sentry.size = nsize;
132 sentry.type = type;
134 if (ioctl (mtrr, MTRRIOC_ADD_ENTRY, &sentry) < 0)
135 ErrorF ("MTRRIOC_ADD_ENTRY failed 0x%x 0x%x %d (errno %d)\n",
136 base, bound - base, type, errno);
138 #endif
141 void
142 KdResetMappedMode (CARD32 addr, CARD32 size, int mode)
144 #ifdef HAVE_ASM_MTRR_H
145 struct mtrr_sentry sentry;
146 unsigned long base, bound;
147 unsigned int type = MTRR_TYPE_WRBACK;
149 if (addr < 0x100000)
150 return;
151 if (!mtrr)
152 mtrr = open ("/proc/mtrr", 2);
153 if (mtrr > 0)
155 unsigned long nsize;
156 base = addr & ~((1<<22)-1);
157 bound = ((addr + size) + ((1<<22) - 1)) & ~((1<<22) - 1);
158 nsize = 1;
159 while (nsize < (bound - base))
160 nsize <<= 1;
161 switch (mode) {
162 case KD_MAPPED_MODE_REGISTERS:
163 type = MTRR_TYPE_UNCACHABLE;
164 break;
165 case KD_MAPPED_MODE_FRAMEBUFFER:
166 type = MTRR_TYPE_WRCOMB;
167 break;
169 sentry.base = base;
170 sentry.size = nsize;
171 sentry.type = type;
173 if (ioctl (mtrr, MTRRIOC_DEL_ENTRY, &sentry) < 0)
174 ErrorF ("MTRRIOC_DEL_ENTRY failed 0x%x 0x%x %d (errno %d)\n",
175 base, bound - base, type, errno);
177 #endif