1/*
2 * Copyright 1997, 1998 by UCHIYAMA Yasushi
3 *
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 UCHIYAMA Yasushi not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  UCHIYAMA Yasushi makes no representations
11 * about the suitability of this software for any purpose.  It is provided
12 * "as is" without express or implied warranty.
13 *
14 * UCHIYAMA YASUSHI DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL UCHIYAMA YASUSHI 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.
21 *
22 */
23
24#ifdef HAVE_XORG_CONFIG_H
25#include <xorg-config.h>
26#endif
27
28#include <mach.h>
29#include <device/device.h>
30#include <mach/machine/mach_i386.h>
31
32#include <X11/X.h>
33#include "input.h"
34#include "scrnintstr.h"
35
36#include "xf86.h"
37#include "xf86Priv.h"
38#include "xf86_OSlib.h"
39#include "xf86OSpriv.h"
40
41/**************************************************************************
42 * Video Memory Mapping section
43 ***************************************************************************/
44static pointer
45mapVidMem(int ScreenNum, unsigned long Base, unsigned long Size, int Flags)
46{
47    mach_port_t device,mem_dev;
48    memory_object_t mem_obj;
49    kern_return_t err;
50    vm_address_t addr=(vm_address_t)0;
51
52    err = get_privileged_ports (NULL, &device);
53    if( err )
54    {
55	errno = err;
56	FatalError("xf86MapVidMem() can't get_privileged_ports. (%s)\n",strerror(errno));
57    }
58    err = device_open(device,D_READ|D_WRITE,"mem",&mem_dev);
59    mach_port_deallocate (mach_task_self(), device);
60    if( err )
61    {
62	errno = err;
63	FatalError("xf86MapVidMem() can't device_open. (%s)\n",strerror(errno));
64    }
65
66    err = device_map(mem_dev,VM_PROT_READ|VM_PROT_WRITE, Base , Size ,&mem_obj,0);
67    if( err )
68    {
69	errno = err;
70	FatalError("xf86MapVidMem() can't device_map. (%s)\n",strerror(errno));
71    }
72    err = vm_map(mach_task_self(),
73		 &addr,
74		 Size,
75		 0,     /* mask */
76		 TRUE,  /* anywhere */
77		 mem_obj,
78		 (vm_offset_t)Base,
79		 FALSE, /* copy on write */
80		 VM_PROT_READ|VM_PROT_WRITE,
81		 VM_PROT_READ|VM_PROT_WRITE,
82		 VM_INHERIT_SHARE);
83    mach_port_deallocate(mach_task_self(),mem_obj);
84    if( err )
85    {
86	errno = err;
87	FatalError("xf86MapVidMem() can't vm_map.(mem_obj) (%s)\n",strerror(errno));
88    }
89    mach_port_deallocate(mach_task_self(),mem_dev);
90    if( err )
91    {
92	errno = err;
93	FatalError("xf86MapVidMem() can't mach_port_deallocate.(mem_dev) (%s)\n",strerror(errno));
94    }
95    return (pointer)addr;
96}
97
98static void
99unmapVidMem(int ScreenNum,pointer Base,unsigned long Size)
100{
101    kern_return_t err = vm_deallocate(mach_task_self(), (int)Base, Size);
102    if( err )
103    {
104	errno = err;
105	ErrorF("xf86UnMapVidMem: can't dealloc framebuffer space (%s)\n",strerror(errno));
106    }
107    return;
108}
109
110/**************************************************************************
111 * I/O Permissions section
112 ***************************************************************************/
113
114/*
115 * Due to conflicts with "compiler.h", don't rely on <sys/io.h> to declare
116 * this.
117 */
118extern int ioperm(unsigned long __from, unsigned long __num, int __turn_on);
119
120Bool
121xf86EnableIO()
122{
123    if (ioperm(0, 0x10000, 1)) {
124	FatalError("xf86EnableIO: ioperm() failed (%s)\n", strerror(errno));
125	return FALSE;
126    }
127#if 0
128    /*
129     * Trapping disabled for now, as some VBIOSes (mga-g450 notably) use these
130     * ports, and the int10 wrapper is not emulating them. (Note that it's
131     * effectively what happens in the Linux variant too, as iopl() is used
132     * there, making the ioperm() meaningless.)
133     *
134     * Reenable this when int10 gets fixed.  */
135    ioperm(0x40,4,0); /* trap access to the timer chip */
136    ioperm(0x60,4,0); /* trap access to the keyboard controller */
137#endif
138    return TRUE;
139}
140
141void
142xf86DisableIO()
143{
144    ioperm(0,0x10000,0);
145    return;
146}
147
148void
149xf86OSInitVidMem(VidMemInfoPtr pVidMem)
150{
151	pVidMem->linearSupported = TRUE;
152	pVidMem->mapMem = mapVidMem;
153	pVidMem->unmapMem = unmapVidMem;
154	pVidMem->initialised = TRUE;
155}
156