1/*
2 * Copyright 1993 by David Wexelblat <dwex@goblin.org>
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 David Wexelblat not be used in
9 * advertising or publicity pertaining to distribution of the software without
10 * specific, written prior permission.  David Wexelblat 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 * DAVID WEXELBLAT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16 * EVENT SHALL DAVID WEXELBLAT 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 <X11/X.h>
29
30#include "xf86.h"
31#include "xf86Priv.h"
32#include "xf86_OSlib.h"
33
34#ifndef MAP_FAILED
35#define MAP_FAILED ((void *)-1)
36#endif
37
38/*
39 * Read BIOS via mmap()ing DEV_MEM
40 */
41
42#ifndef __alpha__
43int
44xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
45	     int Len)
46{
47	int fd;
48	unsigned char *ptr;
49	int psize;
50	int mlen;
51
52	if ((fd = open(DEV_MEM, O_RDONLY)) < 0)
53	{
54		xf86Msg(X_WARNING, "xf86ReadBIOS: Failed to open %s (%s)\n",
55			DEV_MEM, strerror(errno));
56		return -1;
57	}
58	psize = getpagesize();
59	Offset += Base & (psize - 1);
60	Base &= ~(psize - 1);
61	mlen = (Offset + Len + psize - 1) & ~(psize - 1);
62	ptr = (unsigned char *)mmap((caddr_t)0, mlen, PROT_READ,
63					MAP_SHARED, fd, (off_t)Base);
64	if (ptr == MAP_FAILED)
65	{
66		xf86Msg(X_WARNING, "xf86ReadBIOS: %s mmap failed (%s)\n",
67			DEV_MEM, strerror(errno));
68		close(fd);
69		return -1;
70	}
71	DebugF("xf86ReadBIOS: BIOS at 0x%08x has signature 0x%04x\n",
72		Base, ptr[0] | (ptr[1] << 8));
73	(void)memcpy(Buf, (void *)(ptr + Offset), Len);
74	(void)munmap((caddr_t)ptr, mlen);
75	(void)close(fd);
76	return Len;
77}
78
79#else /* __alpha__ */
80
81  /*
82   *  We trick "mmap" into mapping BUS memory for us via BUS_BASE,
83   *  which is the KSEG address of the start of the DENSE memory
84   *  area.
85   */
86
87  /*
88   * NOTE: there prolly ought to be more validity checks and all
89   *  re: boundaries and sizes and such...
90   */
91
92#ifdef linux
93
94extern unsigned long _bus_base(void);
95#define BUS_BASE _bus_base()
96
97#else
98
99extern u_int64_t dense_base(void);
100#define BUS_BASE dense_base()
101
102#endif
103
104int
105xf86ReadBIOS(unsigned long Base, unsigned long Offset, unsigned char *Buf,
106	     int Len)
107{
108	caddr_t base;
109 	int fd;
110	int psize;
111	int mlen;
112
113	if ((fd = open(DEV_MEM, O_RDONLY)) < 0)
114	{
115		xf86Msg(X_WARNING, "xf86ReadBIOS: Failed to open %s (%s)\n",
116			DEV_MEM, strerror(errno));
117		return -1;
118	}
119
120	psize = getpagesize();
121	Offset += Base & (psize - 1);
122	Base &= ~(psize - 1);
123	mlen = (Offset + Len + psize - 1) & ~(psize - 1);
124	base = mmap((caddr_t)0, mlen, PROT_READ,
125		    MAP_SHARED, fd, (off_t)(Base + BUS_BASE));
126
127	if (base == MAP_FAILED)
128	{
129		xf86Msg(X_WARNING, "xf86ReadBIOS: Failed to mmap %s (%s)\n",
130			DEV_MEM, strerror(errno));
131		return -1;
132	}
133
134	xf86SlowBCopyFromBus((unsigned char *)(base+Offset), Buf, Len);
135
136	munmap((caddr_t)base, mlen);
137	close(fd);
138	return Len;
139}
140
141#endif /* __alpha__ */
142