1/*	$NetBSD: alpha_bus_window.c,v 1.2 2001/07/17 17:46:42 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 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32/*
33 * Support for mapping Alpha bus windows.  This is currently used to
34 * provide bus space mapping support for XFree86.  In a perfect world,
35 * this would go away in favor of a real bus space mapping framework.
36 */
37
38#include <sys/types.h>
39#include <sys/mman.h>
40
41#include <machine/sysarch.h>
42
43#include <fcntl.h>
44#include <paths.h>
45#include <stdlib.h>
46#include <string.h>
47#include <unistd.h>
48
49int
50alpha_bus_getwindows(int type, struct alpha_bus_window **abwp)
51{
52	struct alpha_bus_get_window_count_args count_args;
53	struct alpha_bus_get_window_args window_args;
54	struct alpha_bus_window *abw;
55	int i;
56
57	count_args.type = type;
58	if (sysarch(ALPHA_BUS_GET_WINDOW_COUNT, &count_args) != 0)
59		return (-1);
60
61	abw = malloc(sizeof(*abw) * count_args.count);
62	if (abw == NULL)
63		return (-1);
64	memset(abw, 0, sizeof(*abw) * count_args.count);
65
66	for (i = 0; i < count_args.count; i++) {
67		window_args.type = type;
68		window_args.window = i;
69		window_args.translation = &abw[i].abw_abst;
70		if (sysarch(ALPHA_BUS_GET_WINDOW, &window_args) != 0) {
71			free(abw);
72			return (-1);
73		}
74	}
75
76	*abwp = abw;
77	return (count_args.count);
78}
79
80int
81alpha_bus_mapwindow(struct alpha_bus_window *abw)
82{
83	struct alpha_bus_space_translation *abst = &abw->abw_abst;
84	void *addr;
85	size_t size;
86	int fd;
87
88	fd = open(_PATH_MEM, O_RDWR, 0600);
89	if (fd == -1)
90		return (-1);
91
92	size = ((abst->abst_bus_end - abst->abst_bus_start) + 1) <<
93	    abst->abst_addr_shift;
94
95	addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
96	    fd, (off_t) abst->abst_sys_start);
97
98	(void) close(fd);
99
100	if (addr == MAP_FAILED)
101		return (-1);
102
103	abw->abw_addr = addr;
104	abw->abw_size = size;
105
106	return (0);
107}
108
109void
110alpha_bus_unmapwindow(struct alpha_bus_window *abw)
111{
112
113	(void) munmap(abw->abw_addr, abw->abw_size);
114}
115