dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / efcode / pci / pci.c
blob53aa22cdf9a4da4f913ba9b94890556a44ea0a3b
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2000-2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <strings.h>
33 #include <fcode/private.h>
34 #include <fcode/log.h>
36 #include <fcdriver/fcdriver.h>
38 static fstack_t
39 mem_map_in(fcode_env_t *env, fstack_t hi, fstack_t mid, fstack_t lo,
40 fstack_t requested_len)
42 private_data_t *cdp = DEVICE_PRIVATE(env);
43 int error;
44 fc_cell_t requested_virt, adjusted_virt;
45 char *service = "map-in";
46 fstack_t mcookie = NULL;
47 int pa_offset = 0, va_offset = 0;
48 fstack_t adjusted_len = 0;
50 pa_offset = lo & PAGEOFFSET;
51 lo &= PAGEMASK;
53 /* adjust the requested_len to a multiple of a pagesize */
54 requested_len = (requested_len + pa_offset + PAGEOFFSET) & PAGEMASK;
56 error = fc_run_priv(cdp->common, service, 4, 1,
57 fc_size2cell(requested_len), fc_uint32_t2cell(hi),
58 fc_uint32_t2cell(mid), fc_uint32_t2cell(lo), &requested_virt);
60 if (error)
61 throw_from_fclib(env, 1, "pci:%s: failed\n", service);
64 * Check the requested_virt address and ensure that
65 * it starts at a page boundary.
68 va_offset = requested_virt & PAGEOFFSET;
70 if (va_offset != 0) {
73 * Align the virtual address to a page boundary
74 * before mapping it to a mcookie. Recalcuate the
75 * length and round it up to the next multiple of a pagesize.
78 adjusted_virt = requested_virt & PAGEMASK;
79 adjusted_len = (requested_len + va_offset + PAGEOFFSET)
80 & PAGEMASK;
83 mcookie = mapping_to_mcookie(requested_virt, requested_len,
84 adjusted_virt, adjusted_len);
86 if (mcookie == NULL)
87 throw_from_fclib(env, 1, "pci-mapin-> pci:%s:"
88 " mapping_to_mcookie failed\n", service);
90 * Recalculate the address of the mcookie.
93 mcookie += va_offset + pa_offset;
95 debug_msg(DEBUG_REG_ACCESS, "pci:map-in: %llx -> %x\n",
96 (uint64_t)requested_virt, (uint32_t)mcookie);
98 return (mcookie);
101 static void
102 mem_map_out(fcode_env_t *env, fstack_t mcookie, fstack_t requested_len)
104 private_data_t *cdp = DEVICE_PRIVATE(env);
105 char *service = "map-out";
106 fc_cell_t requested_virt;
107 int error;
109 if (!is_mcookie(mcookie)) {
110 log_message(MSG_ERROR, "pci:%s: %x not mcookie!\n", service,
111 (uint32_t)mcookie);
112 requested_virt = mcookie;
113 } else {
114 requested_virt = mcookie_to_rvirt(mcookie);
115 requested_len = mcookie_to_rlen(mcookie);
116 delete_mapping(mcookie);
117 debug_msg(DEBUG_REG_ACCESS, "pci:%s: %x -> %llx\n", service,
118 (uint32_t)mcookie, (uint64_t)requested_virt);
121 error = fc_run_priv(cdp->common, service, 2, 0,
122 fc_size2cell(requested_len), requested_virt);
124 if (error)
125 log_message(MSG_ERROR, "pci:%s: failed\n", service);
128 static void
129 pci_config_fetch(fcode_env_t *env, char *service)
131 uint32_t cfgadd;
132 fc_cell_t value;
133 private_data_t *h = DEVICE_PRIVATE(env);
134 int error;
136 ASSERT(h);
137 CHECK_DEPTH(env, 1, service);
138 cfgadd = POP(DS);
139 error = fc_run_priv(h->common, service, 1, 1, fc_uint32_t2cell(cfgadd),
140 &value);
142 if (error)
143 throw_from_fclib(env, 1, "pci:%s ( %x ) FAIL\n", service,
144 cfgadd);
146 PUSH(DS, value);
149 static void
150 pci_config_store(fcode_env_t *env, char *service)
152 uint32_t cfgadd;
153 fc_cell_t value;
154 private_data_t *h = DEVICE_PRIVATE(env);
155 int error;
157 ASSERT(h);
158 CHECK_DEPTH(env, 2, service);
159 cfgadd = POP(DS);
160 value = POP(DS);
161 error = fc_run_priv(h->common, service, 2, 0, fc_uint32_t2cell(cfgadd),
162 fc_uint32_t2cell(value));
164 if (error)
165 throw_from_fclib(env, 1, "pci:%s ( %x %x ) FAIL\n", service,
166 cfgadd, value);
169 static void
170 config_lfetch(fcode_env_t *env)
172 pci_config_fetch(env, "config-l@");
175 static void
176 config_lstore(fcode_env_t *env)
178 pci_config_store(env, "config-l!");
181 static void
182 config_wfetch(fcode_env_t *env)
184 pci_config_fetch(env, "config-w@");
187 static void
188 config_wstore(fcode_env_t *env)
190 pci_config_store(env, "config-w!");
193 static void
194 config_bfetch(fcode_env_t *env)
196 pci_config_fetch(env, "config-b@");
199 static void
200 config_bstore(fcode_env_t *env)
202 pci_config_store(env, "config-b!");
205 static void
206 do_map_in(fcode_env_t *env)
208 fstack_t phi, pmid, plo, len, addr;
210 CHECK_DEPTH(env, 4, "pci:map-in");
211 len = POP(DS);
212 phi = POP(DS);
213 pmid = POP(DS);
214 plo = POP(DS);
215 addr = mem_map_in(env, phi, pmid, plo, len);
216 PUSH(DS, addr);
219 static void
220 do_map_out(fcode_env_t *env)
222 fstack_t addr, len;
224 CHECK_DEPTH(env, 2, "pci:map-out");
225 len = POP(DS);
226 addr = POP(DS);
227 mem_map_out(env, addr, len);
230 static void
231 do_encode_unit(fcode_env_t *env)
233 char enc_buf[64];
234 uint32_t hi;
235 int dev, fn;
237 CHECK_DEPTH(env, 3, "pci:encode-unit");
238 hi = POP(DS);
239 (void) POP(DS);
240 (void) POP(DS);
242 fn = ((hi >> 8) & 0x7);
243 dev = ((hi >> 11) & 0x1f);
245 if (fn) {
246 sprintf(enc_buf, "%x,%x", dev, fn);
247 } else {
248 sprintf(enc_buf, "%x", dev);
250 debug_msg(DEBUG_REG_ACCESS, "pci:encode-unit ( %x ) -> %s\n",
251 hi, enc_buf);
252 push_a_string(env, STRDUP(enc_buf));
255 static void
256 do_decode_unit(fcode_env_t *env)
258 int lo, hi, unit;
259 char *buf;
261 CHECK_DEPTH(env, 2, "pci:decode-unit");
262 buf = pop_a_string(env, NULL);
263 if (sscanf(buf, "%x,%x", &hi, &lo) != 2) {
264 throw_from_fclib(env, 1, "pci:decode-unit: '%s'", buf);
266 unit = ((hi & 0x1f) << 11);
267 unit |= ((lo & 0x7) << 8);
268 debug_msg(DEBUG_REG_ACCESS, "pci:decode-unit ( '%s' ) -> 0 0 %x\n",
269 buf, unit);
270 PUSH(DS, 0);
271 PUSH(DS, 0);
272 PUSH(DS, unit);
275 static void
276 do_device_id(fcode_env_t *env)
278 uint32_t cfgadd;
279 uint16_t ven_id, dev_id;
280 char buf[40];
282 CHECK_DEPTH(env, 3, "pci:device-id");
283 cfgadd = POP(DS);
284 (void) POP(DS);
285 (void) POP(DS);
286 PUSH(DS, cfgadd + PCI_CONF_VENID);
287 config_wfetch(env);
288 ven_id = POP(DS);
289 PUSH(DS, cfgadd + PCI_CONF_DEVID);
290 config_wfetch(env);
291 dev_id = POP(DS);
292 sprintf(buf, "pci%x,%x", ven_id, dev_id);
293 push_a_string(env, STRDUP(buf));
296 static void
297 do_class_id(fcode_env_t *env)
299 uint32_t cfgadd;
300 uint8_t basclass, subclass, progclass;
301 char buf[40];
303 CHECK_DEPTH(env, 3, "pci:class-id");
304 cfgadd = POP(DS);
305 (void) POP(DS);
306 (void) POP(DS);
307 PUSH(DS, cfgadd + PCI_CONF_BASCLASS);
308 config_bfetch(env);
309 basclass = POP(DS);
310 PUSH(DS, cfgadd + PCI_CONF_SUBCLASS);
311 config_bfetch(env);
312 subclass = POP(DS);
313 PUSH(DS, cfgadd + PCI_CONF_PROGCLASS);
314 config_bfetch(env);
315 progclass = POP(DS);
316 sprintf(buf, "pciclass%02x%02x%02x", basclass, subclass, progclass);
317 push_a_string(env, STRDUP(buf));
320 #pragma init(_init)
322 static void
323 _init(void)
325 fcode_env_t *env = initial_env;
327 ASSERT(env);
328 ASSERT(env->current_device);
329 NOTICE;
331 FORTH(0, "config-l@", config_lfetch);
332 FORTH(0, "config-l!", config_lstore);
333 FORTH(0, "config-w@", config_wfetch);
334 FORTH(0, "config-w!", config_wstore);
335 FORTH(0, "config-b@", config_bfetch);
336 FORTH(0, "config-b!", config_bstore);
337 FORTH(0, "map-in", do_map_in);
338 FORTH(0, "map-out", do_map_out);
339 FORTH(0, "decode-unit", do_decode_unit);
340 FORTH(0, "encode-unit", do_encode_unit);
341 FORTH(0, "device-id", do_device_id);
342 FORTH(0, "class-id", do_class_id);
344 install_dma_methods(env);