Home | History | Annotate | Line # | Download | only in alpha
alpha_bus_window.c revision 1.2
      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  * 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(int type, struct alpha_bus_window **abwp)
     58 {
     59 	struct alpha_bus_get_window_count_args count_args;
     60 	struct alpha_bus_get_window_args window_args;
     61 	struct alpha_bus_window *abw;
     62 	int i;
     63 
     64 	count_args.type = type;
     65 	if (sysarch(ALPHA_BUS_GET_WINDOW_COUNT, &count_args) != 0)
     66 		return (-1);
     67 
     68 	abw = malloc(sizeof(*abw) * count_args.count);
     69 	if (abw == NULL)
     70 		return (-1);
     71 	memset(abw, 0, sizeof(*abw) * count_args.count);
     72 
     73 	for (i = 0; i < count_args.count; i++) {
     74 		window_args.type = type;
     75 		window_args.window = i;
     76 		window_args.translation = &abw[i].abw_abst;
     77 		if (sysarch(ALPHA_BUS_GET_WINDOW, &window_args) != 0) {
     78 			free(abw);
     79 			return (-1);
     80 		}
     81 	}
     82 
     83 	*abwp = abw;
     84 	return (count_args.count);
     85 }
     86 
     87 int
     88 alpha_bus_mapwindow(struct alpha_bus_window *abw)
     89 {
     90 	struct alpha_bus_space_translation *abst = &abw->abw_abst;
     91 	void *addr;
     92 	size_t size;
     93 	int fd;
     94 
     95 	fd = open(_PATH_MEM, O_RDWR, 0600);
     96 	if (fd == -1)
     97 		return (-1);
     98 
     99 	size = ((abst->abst_bus_end - abst->abst_bus_start) + 1) <<
    100 	    abst->abst_addr_shift;
    101 
    102 	addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
    103 	    fd, (off_t) abst->abst_sys_start);
    104 
    105 	(void) close(fd);
    106 
    107 	if (addr == MAP_FAILED)
    108 		return (-1);
    109 
    110 	abw->abw_addr = addr;
    111 	abw->abw_size = size;
    112 
    113 	return (0);
    114 }
    115 
    116 void
    117 alpha_bus_unmapwindow(struct alpha_bus_window *abw)
    118 {
    119 
    120 	(void) munmap(abw->abw_addr, abw->abw_size);
    121 }
    122