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>
29 #ifdef HAVE_ASM_MTRR_H
33 #include <sys/ioctl.h>
36 KdMapDevice (CARD32 addr
, CARD32 size
)
42 d
= VirtualAlloc (NULL
, size
, MEM_RESERVE
, PAGE_NOACCESS
);
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",
55 DRAW_DEBUG ((DEBUG_S3INIT
, "Device mapped successfully"));
63 fd
= open ("/dev/mem", O_RDWR
|O_SYNC
);
65 fd
= open ("/dev/mem", O_RDWR
);
68 FatalError ("KdMapDevice: failed to open /dev/mem (%s)\n",
71 a
= mmap ((caddr_t
) 0, size
, PROT_READ
|PROT_WRITE
, MAP_SHARED
, fd
, addr
);
74 FatalError ("KdMapDevice: failed to map frame buffer (%s)\n",
84 KdUnmapDevice (void *addr
, CARD32 size
)
87 VirtualFree (addr
, size
, MEM_DECOMMIT
);
88 VirtualFree (addr
, 0, MEM_RELEASE
);
98 #ifdef HAVE_ASM_MTRR_H
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
;
113 mtrr
= open ("/proc/mtrr", 2);
117 base
= addr
& ~((1<<22)-1);
118 bound
= ((addr
+ size
) + ((1<<22) - 1)) & ~((1<<22) - 1);
120 while (nsize
< (bound
- base
))
123 case KD_MAPPED_MODE_REGISTERS
:
124 type
= MTRR_TYPE_UNCACHABLE
;
126 case KD_MAPPED_MODE_FRAMEBUFFER
:
127 type
= MTRR_TYPE_WRCOMB
;
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
);
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
;
152 mtrr
= open ("/proc/mtrr", 2);
156 base
= addr
& ~((1<<22)-1);
157 bound
= ((addr
+ size
) + ((1<<22) - 1)) & ~((1<<22) - 1);
159 while (nsize
< (bound
- base
))
162 case KD_MAPPED_MODE_REGISTERS
:
163 type
= MTRR_TYPE_UNCACHABLE
;
165 case KD_MAPPED_MODE_FRAMEBUFFER
:
166 type
= MTRR_TYPE_WRCOMB
;
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
);