drm_memory.c revision 1.1.2.1 1 1.1.2.1 riastrad /* $NetBSD: drm_memory.c,v 1.1.2.1 2013/07/24 02:23:06 riastradh Exp $ */
2 1.1.2.1 riastrad
3 1.1.2.1 riastrad /*-
4 1.1.2.1 riastrad * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1.2.1 riastrad * All rights reserved.
6 1.1.2.1 riastrad *
7 1.1.2.1 riastrad * This code is derived from software contributed to The NetBSD Foundation
8 1.1.2.1 riastrad * by Taylor R. Campbell.
9 1.1.2.1 riastrad *
10 1.1.2.1 riastrad * Redistribution and use in source and binary forms, with or without
11 1.1.2.1 riastrad * modification, are permitted provided that the following conditions
12 1.1.2.1 riastrad * are met:
13 1.1.2.1 riastrad * 1. Redistributions of source code must retain the above copyright
14 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer.
15 1.1.2.1 riastrad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.2.1 riastrad * notice, this list of conditions and the following disclaimer in the
17 1.1.2.1 riastrad * documentation and/or other materials provided with the distribution.
18 1.1.2.1 riastrad *
19 1.1.2.1 riastrad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1.2.1 riastrad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1.2.1 riastrad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1.2.1 riastrad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1.2.1 riastrad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1.2.1 riastrad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1.2.1 riastrad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1.2.1 riastrad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1.2.1 riastrad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1.2.1 riastrad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1.2.1 riastrad * POSSIBILITY OF SUCH DAMAGE.
30 1.1.2.1 riastrad */
31 1.1.2.1 riastrad
32 1.1.2.1 riastrad #include <sys/cdefs.h>
33 1.1.2.1 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_memory.c,v 1.1.2.1 2013/07/24 02:23:06 riastradh Exp $");
34 1.1.2.1 riastrad
35 1.1.2.1 riastrad /* XXX Cargo-culted from the old drm_memory.c. */
36 1.1.2.1 riastrad
37 1.1.2.1 riastrad #ifdef _KERNEL_OPT
38 1.1.2.1 riastrad #include "agp_i810.h"
39 1.1.2.1 riastrad #include "genfb.h"
40 1.1.2.1 riastrad #else
41 1.1.2.1 riastrad #define NAGP_I810 1 /* XXX WTF? */
42 1.1.2.1 riastrad #define NGENFB 0 /* XXX WTF? */
43 1.1.2.1 riastrad #endif
44 1.1.2.1 riastrad
45 1.1.2.1 riastrad #include <sys/bus.h>
46 1.1.2.1 riastrad
47 1.1.2.1 riastrad #if NAGP_I810 > 0
48 1.1.2.1 riastrad /* XXX include order botch -- shouldn't need to include pcivar.h */
49 1.1.2.1 riastrad #include <dev/pci/pcivar.h>
50 1.1.2.1 riastrad #include <dev/pci/agpvar.h>
51 1.1.2.1 riastrad #endif
52 1.1.2.1 riastrad
53 1.1.2.1 riastrad #if NGENFB > 0
54 1.1.2.1 riastrad #include <dev/wsfb/genfbvar.h>
55 1.1.2.1 riastrad #endif
56 1.1.2.1 riastrad
57 1.1.2.1 riastrad #include <drm/drmP.h>
58 1.1.2.1 riastrad
59 1.1.2.1 riastrad /*
60 1.1.2.1 riastrad * XXX drm_bus_borrow is a horrible kludge!
61 1.1.2.1 riastrad */
62 1.1.2.1 riastrad static bool
63 1.1.2.1 riastrad drm_bus_borrow(bus_addr_t base, bus_space_handle_t *handlep)
64 1.1.2.1 riastrad {
65 1.1.2.1 riastrad
66 1.1.2.1 riastrad #if NAGP_I810 > 0
67 1.1.2.1 riastrad if (agp_i810_borrow(base, handlep))
68 1.1.2.1 riastrad return true;
69 1.1.2.1 riastrad #endif
70 1.1.2.1 riastrad
71 1.1.2.1 riastrad #if NGENFB > 0
72 1.1.2.1 riastrad if (genfb_borrow(base, handlep))
73 1.1.2.1 riastrad return true;
74 1.1.2.1 riastrad #endif
75 1.1.2.1 riastrad
76 1.1.2.1 riastrad return false;
77 1.1.2.1 riastrad }
78 1.1.2.1 riastrad
79 1.1.2.1 riastrad void *
80 1.1.2.1 riastrad drm_ioremap(struct drm_device *dev, struct drm_local_map *map)
81 1.1.2.1 riastrad {
82 1.1.2.1 riastrad const bus_space_tag_t bst = dev->bst;
83 1.1.2.1 riastrad unsigned int unit;
84 1.1.2.1 riastrad int error;
85 1.1.2.1 riastrad
86 1.1.2.1 riastrad /*
87 1.1.2.1 riastrad * Search dev's bus maps for a match.
88 1.1.2.1 riastrad */
89 1.1.2.1 riastrad for (unit = 0; unit < dev->bus_nmaps; unit++) {
90 1.1.2.1 riastrad struct drm_bus_map *const bm = &dev->bus_maps[unit];
91 1.1.2.1 riastrad
92 1.1.2.1 riastrad /* Reject maps starting after the request. */
93 1.1.2.1 riastrad if (map->offset < bm->bm_base)
94 1.1.2.1 riastrad continue;
95 1.1.2.1 riastrad
96 1.1.2.1 riastrad /* Reject maps smaller than the request. */
97 1.1.2.1 riastrad if (bm->bm_size < map->size)
98 1.1.2.1 riastrad continue;
99 1.1.2.1 riastrad
100 1.1.2.1 riastrad /*
101 1.1.2.1 riastrad * Reject maps that the request doesn't fit in. (Make
102 1.1.2.1 riastrad * sure to avoid integer overflow.)
103 1.1.2.1 riastrad */
104 1.1.2.1 riastrad if ((bm->bm_size - map->size) <
105 1.1.2.1 riastrad (map->offset - bm->bm_base))
106 1.1.2.1 riastrad continue;
107 1.1.2.1 riastrad
108 1.1.2.1 riastrad /* Has it been mapped yet? If not, map it. */
109 1.1.2.1 riastrad if (bm->bm_mapped == 0) {
110 1.1.2.1 riastrad KASSERT(ISSET(bm->bm_flags, BUS_SPACE_MAP_LINEAR));
111 1.1.2.1 riastrad error = bus_space_map(bst, bm->bm_base,
112 1.1.2.1 riastrad bm->bm_size, bm->bm_flags, &bm->bm_bsh);
113 1.1.2.1 riastrad if (error) {
114 1.1.2.1 riastrad if (drm_bus_borrow(map->offset, &map->bsh)) {
115 1.1.2.1 riastrad map->bus_map = NULL;
116 1.1.2.1 riastrad goto win;
117 1.1.2.1 riastrad }
118 1.1.2.1 riastrad return NULL;
119 1.1.2.1 riastrad }
120 1.1.2.1 riastrad }
121 1.1.2.1 riastrad
122 1.1.2.1 riastrad /* Mark it used and make a subregion just for the request. */
123 1.1.2.1 riastrad bm->bm_mapped++;
124 1.1.2.1 riastrad error = bus_space_subregion(bst, bm->bm_bsh,
125 1.1.2.1 riastrad map->offset - bm->bm_base, map->size, &map->bsh);
126 1.1.2.1 riastrad if (error) {
127 1.1.2.1 riastrad /*
128 1.1.2.1 riastrad * Back out: unmark it and, if nobody else was
129 1.1.2.1 riastrad * using it, unmap it.
130 1.1.2.1 riastrad */
131 1.1.2.1 riastrad if (--bm->bm_mapped == 0)
132 1.1.2.1 riastrad bus_space_unmap(bst, bm->bm_bsh,
133 1.1.2.1 riastrad bm->bm_size);
134 1.1.2.1 riastrad return NULL;
135 1.1.2.1 riastrad }
136 1.1.2.1 riastrad
137 1.1.2.1 riastrad /* Got it! */
138 1.1.2.1 riastrad map->bus_map = bm;
139 1.1.2.1 riastrad goto win;
140 1.1.2.1 riastrad }
141 1.1.2.1 riastrad
142 1.1.2.1 riastrad /*
143 1.1.2.1 riastrad * No dice. Try mapping it directly ourselves.
144 1.1.2.1 riastrad *
145 1.1.2.1 riastrad * XXX Is this sensible? What prevents us from clobbering some
146 1.1.2.1 riastrad * existing map? And what does this have to do with agp?
147 1.1.2.1 riastrad */
148 1.1.2.1 riastrad for (unit = 0; unit < dev->agp_nmaps; unit++) {
149 1.1.2.1 riastrad struct drm_bus_map *const bm = &dev->agp_maps[unit];
150 1.1.2.1 riastrad
151 1.1.2.1 riastrad /* Is this one allocated? */
152 1.1.2.1 riastrad if (bm->bm_mapped > 0) {
153 1.1.2.1 riastrad /*
154 1.1.2.1 riastrad * Make sure it has the same base.
155 1.1.2.1 riastrad *
156 1.1.2.1 riastrad * XXX Why must it be the same base? Can't we
157 1.1.2.1 riastrad * subregion here too?
158 1.1.2.1 riastrad */
159 1.1.2.1 riastrad if (bm->bm_base != map->offset)
160 1.1.2.1 riastrad continue;
161 1.1.2.1 riastrad
162 1.1.2.1 riastrad /* Make sure it's big enough. */
163 1.1.2.1 riastrad if (bm->bm_size < map->size)
164 1.1.2.1 riastrad continue;
165 1.1.2.1 riastrad
166 1.1.2.1 riastrad /* Mark it used and return it. */
167 1.1.2.1 riastrad bm->bm_mapped++;
168 1.1.2.1 riastrad
169 1.1.2.1 riastrad /* XXX size is an input/output parameter too...? */
170 1.1.2.1 riastrad map->size = bm->bm_size;
171 1.1.2.1 riastrad
172 1.1.2.1 riastrad map->bsh = bm->bm_bsh;
173 1.1.2.1 riastrad map->bus_map = bm;
174 1.1.2.1 riastrad goto win;
175 1.1.2.1 riastrad } else {
176 1.1.2.1 riastrad const int flags = BUS_SPACE_MAP_PREFETCHABLE |
177 1.1.2.1 riastrad BUS_SPACE_MAP_LINEAR;
178 1.1.2.1 riastrad
179 1.1.2.1 riastrad /* Try mapping the request. */
180 1.1.2.1 riastrad error = bus_space_map(bst, map->offset, map->size,
181 1.1.2.1 riastrad flags, &bm->bm_bsh);
182 1.1.2.1 riastrad if (error)
183 1.1.2.1 riastrad return NULL; /* XXX Why not continue? */
184 1.1.2.1 riastrad
185 1.1.2.1 riastrad /* Got it. Allocate this bus map. */
186 1.1.2.1 riastrad bm->bm_mapped++;
187 1.1.2.1 riastrad bm->bm_base = map->offset;
188 1.1.2.1 riastrad bm->bm_size = map->size;
189 1.1.2.1 riastrad bm->bm_flags = flags; /* XXX What for? */
190 1.1.2.1 riastrad
191 1.1.2.1 riastrad map->bsh = bm->bm_bsh;
192 1.1.2.1 riastrad map->bus_map = bm;
193 1.1.2.1 riastrad goto win;
194 1.1.2.1 riastrad }
195 1.1.2.1 riastrad }
196 1.1.2.1 riastrad
197 1.1.2.1 riastrad return NULL;
198 1.1.2.1 riastrad
199 1.1.2.1 riastrad win:
200 1.1.2.1 riastrad return bus_space_vaddr(bst, map->bsh);
201 1.1.2.1 riastrad }
202 1.1.2.1 riastrad
203 1.1.2.1 riastrad void
204 1.1.2.1 riastrad drm_iounmap(struct drm_device *dev, struct drm_local_map *map)
205 1.1.2.1 riastrad {
206 1.1.2.1 riastrad const bus_space_tag_t bst = dev->bst;
207 1.1.2.1 riastrad struct drm_bus_map *const bm = map->bus_map;
208 1.1.2.1 riastrad
209 1.1.2.1 riastrad /*
210 1.1.2.1 riastrad * bm may be null if we have committed the horrible deed of
211 1.1.2.1 riastrad * borrowing from agp_i810 or genfb.
212 1.1.2.1 riastrad */
213 1.1.2.1 riastrad if (bm != NULL) {
214 1.1.2.1 riastrad KASSERT(bm->bm_mapped > 0);
215 1.1.2.1 riastrad if (--bm->bm_mapped)
216 1.1.2.1 riastrad bus_space_unmap(bst, bm->bm_bsh, bm->bm_size);
217 1.1.2.1 riastrad }
218 1.1.2.1 riastrad }
219