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