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