xref: /netbsd-src/lib/libarch/alpha/alpha_bus_window.c (revision cd8a1657b46a55aae50dc03bd5314a2f81364b6f)
1 /*	$NetBSD: alpha_bus_window.c,v 1.1 2000/02/26 18:59:36 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Support for mapping Alpha bus windows.  This is currently used to
41  * provide bus space mapping support for XFree86.  In a perfect world,
42  * this would go away in favor of a real bus space mapping framework.
43  */
44 
45 #include <sys/types.h>
46 #include <sys/mman.h>
47 
48 #include <machine/sysarch.h>
49 
50 #include <fcntl.h>
51 #include <paths.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 int
57 alpha_bus_getwindows(type, abwp)
58 	int type;
59 	struct alpha_bus_window **abwp;
60 {
61 	struct alpha_bus_get_window_count_args count_args;
62 	struct alpha_bus_get_window_args window_args;
63 	struct alpha_bus_window *abw;
64 	int i;
65 
66 	count_args.type = type;
67 	if (sysarch(ALPHA_BUS_GET_WINDOW_COUNT, &count_args) != 0)
68 		return (-1);
69 
70 	abw = malloc(sizeof(*abw) * count_args.count);
71 	if (abw == NULL)
72 		return (-1);
73 	memset(abw, 0, sizeof(*abw) * count_args.count);
74 
75 	for (i = 0; i < count_args.count; i++) {
76 		window_args.type = type;
77 		window_args.window = i;
78 		window_args.translation = &abw[i].abw_abst;
79 		if (sysarch(ALPHA_BUS_GET_WINDOW, &window_args) != 0) {
80 			free(abw);
81 			return (-1);
82 		}
83 	}
84 
85 	*abwp = abw;
86 	return (count_args.count);
87 }
88 
89 int
90 alpha_bus_mapwindow(abw)
91 	struct alpha_bus_window *abw;
92 {
93 	struct alpha_bus_space_translation *abst = &abw->abw_abst;
94 	void *addr;
95 	size_t size;
96 	int fd;
97 
98 	fd = open(_PATH_MEM, O_RDWR, 0600);
99 	if (fd == -1)
100 		return (-1);
101 
102 	size = ((abst->abst_bus_end - abst->abst_bus_start) + 1) <<
103 	    abst->abst_addr_shift;
104 
105 	addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
106 	    fd, (off_t) abst->abst_sys_start);
107 
108 	(void) close(fd);
109 
110 	if (addr == MAP_FAILED)
111 		return (-1);
112 
113 	abw->abw_addr = addr;
114 	abw->abw_size = size;
115 
116 	return (0);
117 }
118 
119 void
120 alpha_bus_unmapwindow(abw)
121 	struct alpha_bus_window *abw;
122 {
123 
124 	(void) munmap(abw->abw_addr, abw->abw_size);
125 }
126