xref: /netbsd-src/lib/libarch/alpha/alpha_pci_mem.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin /*	$NetBSD: alpha_pci_mem.c,v 1.5 2008/04/28 20:22:55 martin 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 PCI/EISA/ISA memory space.  This is currently used
34cd8a1657Sthorpej  * to provide such support for XFree86.  In a perfect world, this would go
35cd8a1657Sthorpej  * away in favor of a real bus space mapping framework.
36cd8a1657Sthorpej  */
37cd8a1657Sthorpej 
38cd8a1657Sthorpej #include <sys/param.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 struct alpha_bus_window *alpha_pci_mem_windows;
50cd8a1657Sthorpej int alpha_pci_mem_window_count;
51cd8a1657Sthorpej 
52cd8a1657Sthorpej void *
alpha_pci_mem_map(bus_addr_t memaddr,bus_size_t memsize,int flags,struct alpha_bus_space_translation * rabst)53f9c5bef1Sthorpej alpha_pci_mem_map(bus_addr_t memaddr, bus_size_t memsize, int flags,
54f9c5bef1Sthorpej     struct alpha_bus_space_translation *rabst)
55cd8a1657Sthorpej {
56cd8a1657Sthorpej 	struct alpha_bus_window *abw;
57cd8a1657Sthorpej 	void *addr;
58cd8a1657Sthorpej 	int prefetchable = flags & BUS_SPACE_MAP_PREFETCHABLE;
59cd8a1657Sthorpej 	int linear = flags & BUS_SPACE_MAP_LINEAR;
60cd8a1657Sthorpej 	bus_size_t offset;
61cd8a1657Sthorpej 	int i, fd;
62cd8a1657Sthorpej 
63cd8a1657Sthorpej 	/*
64cd8a1657Sthorpej 	 * Can't have linear without prefetchable.
65cd8a1657Sthorpej 	 */
66cd8a1657Sthorpej 	if (linear && !prefetchable)
67cd8a1657Sthorpej 		return (MAP_FAILED);
68cd8a1657Sthorpej 
69cd8a1657Sthorpej 	if (alpha_pci_mem_windows == NULL) {
70cd8a1657Sthorpej 		i = alpha_bus_getwindows(ALPHA_BUS_TYPE_PCI_MEM, &abw);
71cd8a1657Sthorpej 		if (i <= 0)
72cd8a1657Sthorpej 			return (MAP_FAILED);
73cd8a1657Sthorpej 
74cd8a1657Sthorpej 		alpha_pci_mem_windows = abw;
75cd8a1657Sthorpej 		alpha_pci_mem_window_count = i;
76cd8a1657Sthorpej 	}
77cd8a1657Sthorpej 
78cd8a1657Sthorpej 	for (i = 0; i < alpha_pci_mem_window_count; i++) {
79cd8a1657Sthorpej 		abw = &alpha_pci_mem_windows[i];
80cd8a1657Sthorpej 		if (memaddr < abw->abw_abst.abst_bus_start ||
81cd8a1657Sthorpej 		    (memaddr + (memsize - 1)) > abw->abw_abst.abst_bus_end)
82cd8a1657Sthorpej 			continue;
83cd8a1657Sthorpej 
84c70a898bSmycroft 		/*
85c70a898bSmycroft 		 * Prefetchable memory must be mapped in dense space;
86c70a898bSmycroft 		 * otherwise use sparse space.
87c70a898bSmycroft 		 */
88c70a898bSmycroft 		if (prefetchable &&
89c70a898bSmycroft 		    (abw->abw_abst.abst_flags & ABST_DENSE) == 0)
90c70a898bSmycroft 			continue;
91c70a898bSmycroft 		if (!prefetchable &&
92c70a898bSmycroft 		    (abw->abw_abst.abst_flags & ABST_DENSE) != 0)
93cd8a1657Sthorpej 			continue;
94cd8a1657Sthorpej 
95cd8a1657Sthorpej 		/* Looks like we have a winner! */
96cd8a1657Sthorpej 		goto found;
97cd8a1657Sthorpej 	}
98cd8a1657Sthorpej 
99c70a898bSmycroft 	/* Not found in any of the windows. */
100c70a898bSmycroft 	return (MAP_FAILED);
101c70a898bSmycroft 
102cd8a1657Sthorpej  found:
103cd8a1657Sthorpej 	fd = open(_PATH_MEM, O_RDWR, 0600);
104cd8a1657Sthorpej 	if (fd == -1)
105cd8a1657Sthorpej 		return (MAP_FAILED);
106cd8a1657Sthorpej 
107c70a898bSmycroft 	if (prefetchable)
108c70a898bSmycroft 		abw->abw_abst.abst_addr_shift = 0;
109cd8a1657Sthorpej 	memsize <<= abw->abw_abst.abst_addr_shift;
110cd8a1657Sthorpej 	offset = (memaddr - abw->abw_abst.abst_bus_start) <<
111cd8a1657Sthorpej 	    abw->abw_abst.abst_addr_shift;
112cd8a1657Sthorpej 
113cd8a1657Sthorpej 	addr = mmap(NULL, memsize, PROT_READ|PROT_WRITE, MAP_FILE|MAP_SHARED,
114cd8a1657Sthorpej 	    fd, (off_t) (abw->abw_abst.abst_sys_start + offset));
115cd8a1657Sthorpej 
116cd8a1657Sthorpej 	(void) close(fd);
117cd8a1657Sthorpej 
118cd8a1657Sthorpej 	if (addr != MAP_FAILED) {
119cd8a1657Sthorpej 		/*
120cd8a1657Sthorpej 		 * Make a copy of the space translation so that the caller
121cd8a1657Sthorpej 		 * can do the correct access.
122cd8a1657Sthorpej 		 */
123cd8a1657Sthorpej 		*rabst = abw->abw_abst;
124cd8a1657Sthorpej 	}
125cd8a1657Sthorpej 	return (addr);
126cd8a1657Sthorpej }
127cd8a1657Sthorpej 
128cd8a1657Sthorpej void
alpha_pci_mem_unmap(struct alpha_bus_space_translation * abst,void * addr,bus_size_t size)129f9c5bef1Sthorpej alpha_pci_mem_unmap(struct alpha_bus_space_translation *abst, void *addr,
130f9c5bef1Sthorpej     bus_size_t size)
131cd8a1657Sthorpej {
132cd8a1657Sthorpej 
133edf0a0d6Ssimonb 	(void) munmap(addr, size << abst->abst_addr_shift);
134cd8a1657Sthorpej }
135