1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright 2000-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <strings.h>
30
31 #include <fcode/private.h>
32 #include <fcode/log.h>
33
34 #include <fcdriver/fcdriver.h>
35
36 static fstack_t
mem_map_in(fcode_env_t * env,fstack_t hi,fstack_t mid,fstack_t lo,fstack_t requested_len)37 mem_map_in(fcode_env_t *env, fstack_t hi, fstack_t mid, fstack_t lo,
38 fstack_t requested_len)
39 {
40 private_data_t *cdp = DEVICE_PRIVATE(env);
41 int error;
42 fc_cell_t requested_virt, adjusted_virt;
43 char *service = "map-in";
44 fstack_t mcookie = 0;
45 int pa_offset = 0, va_offset = 0;
46 fstack_t adjusted_len = 0;
47
48 pa_offset = lo & PAGEOFFSET;
49 lo &= PAGEMASK;
50
51 /* adjust the requested_len to a multiple of a pagesize */
52 requested_len = (requested_len + pa_offset + PAGEOFFSET) & PAGEMASK;
53
54 error = fc_run_priv(cdp->common, service, 4, 1,
55 fc_size2cell(requested_len), fc_uint32_t2cell(hi),
56 fc_uint32_t2cell(mid), fc_uint32_t2cell(lo), &requested_virt);
57
58 if (error)
59 throw_from_fclib(env, 1, "pci:%s: failed\n", service);
60
61 /*
62 * Check the requested_virt address and ensure that
63 * it starts at a page boundary.
64 */
65
66 va_offset = requested_virt & PAGEOFFSET;
67
68 if (va_offset != 0) {
69
70 /*
71 * Align the virtual address to a page boundary
72 * before mapping it to a mcookie. Recalcuate the
73 * length and round it up to the next multiple of a pagesize.
74 */
75
76 adjusted_virt = requested_virt & PAGEMASK;
77 adjusted_len = (requested_len + va_offset + PAGEOFFSET)
78 & PAGEMASK;
79 }
80
81 mcookie = mapping_to_mcookie(requested_virt, requested_len,
82 adjusted_virt, adjusted_len);
83
84 if (mcookie == 0)
85 throw_from_fclib(env, 1, "pci-mapin-> pci:%s:"
86 " mapping_to_mcookie failed\n", service);
87 /*
88 * Recalculate the address of the mcookie.
89 */
90
91 mcookie += va_offset + pa_offset;
92
93 debug_msg(DEBUG_REG_ACCESS, "pci:map-in: %llx -> %x\n",
94 (uint64_t)requested_virt, (uint32_t)mcookie);
95
96 return (mcookie);
97 }
98
99 static void
mem_map_out(fcode_env_t * env,fstack_t mcookie,fstack_t requested_len)100 mem_map_out(fcode_env_t *env, fstack_t mcookie, fstack_t requested_len)
101 {
102 private_data_t *cdp = DEVICE_PRIVATE(env);
103 char *service = "map-out";
104 fc_cell_t requested_virt;
105 int error;
106
107 if (!is_mcookie(mcookie)) {
108 log_message(MSG_ERROR, "pci:%s: %x not mcookie!\n", service,
109 (uint32_t)mcookie);
110 requested_virt = mcookie;
111 } else {
112 requested_virt = mcookie_to_rvirt(mcookie);
113 requested_len = mcookie_to_rlen(mcookie);
114 delete_mapping(mcookie);
115 debug_msg(DEBUG_REG_ACCESS, "pci:%s: %x -> %llx\n", service,
116 (uint32_t)mcookie, (uint64_t)requested_virt);
117 }
118
119 error = fc_run_priv(cdp->common, service, 2, 0,
120 fc_size2cell(requested_len), requested_virt);
121
122 if (error)
123 log_message(MSG_ERROR, "pci:%s: failed\n", service);
124 }
125
126 static void
pci_config_fetch(fcode_env_t * env,char * service)127 pci_config_fetch(fcode_env_t *env, char *service)
128 {
129 uint32_t cfgadd;
130 fc_cell_t value;
131 private_data_t *h = DEVICE_PRIVATE(env);
132 int error;
133
134 ASSERT(h);
135 CHECK_DEPTH(env, 1, service);
136 cfgadd = POP(DS);
137 error = fc_run_priv(h->common, service, 1, 1, fc_uint32_t2cell(cfgadd),
138 &value);
139
140 if (error)
141 throw_from_fclib(env, 1, "pci:%s ( %x ) FAIL\n", service,
142 cfgadd);
143
144 PUSH(DS, value);
145 }
146
147 static void
pci_config_store(fcode_env_t * env,char * service)148 pci_config_store(fcode_env_t *env, char *service)
149 {
150 uint32_t cfgadd;
151 fc_cell_t value;
152 private_data_t *h = DEVICE_PRIVATE(env);
153 int error;
154
155 ASSERT(h);
156 CHECK_DEPTH(env, 2, service);
157 cfgadd = POP(DS);
158 value = POP(DS);
159 error = fc_run_priv(h->common, service, 2, 0, fc_uint32_t2cell(cfgadd),
160 fc_uint32_t2cell(value));
161
162 if (error)
163 throw_from_fclib(env, 1, "pci:%s ( %x %x ) FAIL\n", service,
164 cfgadd, value);
165 }
166
167 static void
config_lfetch(fcode_env_t * env)168 config_lfetch(fcode_env_t *env)
169 {
170 pci_config_fetch(env, "config-l@");
171 }
172
173 static void
config_lstore(fcode_env_t * env)174 config_lstore(fcode_env_t *env)
175 {
176 pci_config_store(env, "config-l!");
177 }
178
179 static void
config_wfetch(fcode_env_t * env)180 config_wfetch(fcode_env_t *env)
181 {
182 pci_config_fetch(env, "config-w@");
183 }
184
185 static void
config_wstore(fcode_env_t * env)186 config_wstore(fcode_env_t *env)
187 {
188 pci_config_store(env, "config-w!");
189 }
190
191 static void
config_bfetch(fcode_env_t * env)192 config_bfetch(fcode_env_t *env)
193 {
194 pci_config_fetch(env, "config-b@");
195 }
196
197 static void
config_bstore(fcode_env_t * env)198 config_bstore(fcode_env_t *env)
199 {
200 pci_config_store(env, "config-b!");
201 }
202
203 static void
do_map_in(fcode_env_t * env)204 do_map_in(fcode_env_t *env)
205 {
206 fstack_t phi, pmid, plo, len, addr;
207
208 CHECK_DEPTH(env, 4, "pci:map-in");
209 len = POP(DS);
210 phi = POP(DS);
211 pmid = POP(DS);
212 plo = POP(DS);
213 addr = mem_map_in(env, phi, pmid, plo, len);
214 PUSH(DS, addr);
215 }
216
217 static void
do_map_out(fcode_env_t * env)218 do_map_out(fcode_env_t *env)
219 {
220 fstack_t addr, len;
221
222 CHECK_DEPTH(env, 2, "pci:map-out");
223 len = POP(DS);
224 addr = POP(DS);
225 mem_map_out(env, addr, len);
226 }
227
228 static void
do_encode_unit(fcode_env_t * env)229 do_encode_unit(fcode_env_t *env)
230 {
231 char enc_buf[64];
232 uint32_t hi;
233 int dev, fn;
234
235 CHECK_DEPTH(env, 3, "pci:encode-unit");
236 hi = POP(DS);
237 (void) POP(DS);
238 (void) POP(DS);
239
240 fn = ((hi >> 8) & 0x7);
241 dev = ((hi >> 11) & 0x1f);
242
243 if (fn) {
244 (void) sprintf(enc_buf, "%x,%x", dev, fn);
245 } else {
246 (void) sprintf(enc_buf, "%x", dev);
247 }
248 debug_msg(DEBUG_REG_ACCESS, "pci:encode-unit ( %x ) -> %s\n",
249 hi, enc_buf);
250 push_a_string(env, STRDUP(enc_buf));
251 }
252
253 static void
do_decode_unit(fcode_env_t * env)254 do_decode_unit(fcode_env_t *env)
255 {
256 int lo, hi, unit;
257 char *buf;
258
259 CHECK_DEPTH(env, 2, "pci:decode-unit");
260 buf = pop_a_string(env, NULL);
261 if (sscanf(buf, "%x,%x", &hi, &lo) != 2) {
262 throw_from_fclib(env, 1, "pci:decode-unit: '%s'", buf);
263 }
264 unit = ((hi & 0x1f) << 11);
265 unit |= ((lo & 0x7) << 8);
266 debug_msg(DEBUG_REG_ACCESS, "pci:decode-unit ( '%s' ) -> 0 0 %x\n",
267 buf, unit);
268 PUSH(DS, 0);
269 PUSH(DS, 0);
270 PUSH(DS, unit);
271 }
272
273 static void
do_device_id(fcode_env_t * env)274 do_device_id(fcode_env_t *env)
275 {
276 uint32_t cfgadd;
277 uint16_t ven_id, dev_id;
278 char buf[40];
279
280 CHECK_DEPTH(env, 3, "pci:device-id");
281 cfgadd = POP(DS);
282 (void) POP(DS);
283 (void) POP(DS);
284 PUSH(DS, cfgadd + PCI_CONF_VENID);
285 config_wfetch(env);
286 ven_id = POP(DS);
287 PUSH(DS, cfgadd + PCI_CONF_DEVID);
288 config_wfetch(env);
289 dev_id = POP(DS);
290 (void) sprintf(buf, "pci%x,%x", ven_id, dev_id);
291 push_a_string(env, STRDUP(buf));
292 }
293
294 static void
do_class_id(fcode_env_t * env)295 do_class_id(fcode_env_t *env)
296 {
297 uint32_t cfgadd;
298 uint8_t basclass, subclass, progclass;
299 char buf[40];
300
301 CHECK_DEPTH(env, 3, "pci:class-id");
302 cfgadd = POP(DS);
303 (void) POP(DS);
304 (void) POP(DS);
305 PUSH(DS, cfgadd + PCI_CONF_BASCLASS);
306 config_bfetch(env);
307 basclass = POP(DS);
308 PUSH(DS, cfgadd + PCI_CONF_SUBCLASS);
309 config_bfetch(env);
310 subclass = POP(DS);
311 PUSH(DS, cfgadd + PCI_CONF_PROGCLASS);
312 config_bfetch(env);
313 progclass = POP(DS);
314 (void) sprintf(buf, "pciclass%02x%02x%02x", basclass, subclass,
315 progclass);
316 push_a_string(env, STRDUP(buf));
317 }
318
319 #pragma init(_init)
320
321 static void
_init(void)322 _init(void)
323 {
324 fcode_env_t *env = initial_env;
325
326 ASSERT(env);
327 ASSERT(env->current_device);
328 NOTICE;
329
330 FORTH(0, "config-l@", config_lfetch);
331 FORTH(0, "config-l!", config_lstore);
332 FORTH(0, "config-w@", config_wfetch);
333 FORTH(0, "config-w!", config_wstore);
334 FORTH(0, "config-b@", config_bfetch);
335 FORTH(0, "config-b!", config_bstore);
336 FORTH(0, "map-in", do_map_in);
337 FORTH(0, "map-out", do_map_out);
338 FORTH(0, "decode-unit", do_decode_unit);
339 FORTH(0, "encode-unit", do_encode_unit);
340 FORTH(0, "device-id", do_device_id);
341 FORTH(0, "class-id", do_class_id);
342
343 install_dma_methods(env);
344 }
345