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
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]
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"
33 #include <fcode/private.h>
34 #include <fcode/log.h>
36 #include <fcdriver/fcdriver.h>
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
);
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
;
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
);
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
;
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
)
83 mcookie
= mapping_to_mcookie(requested_virt
, requested_len
,
84 adjusted_virt
, adjusted_len
);
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
);
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
;
109 if (!is_mcookie(mcookie
)) {
110 log_message(MSG_ERROR
, "pci:%s: %x not mcookie!\n", service
,
112 requested_virt
= mcookie
;
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
);
125 log_message(MSG_ERROR
, "pci:%s: failed\n", service
);
129 pci_config_fetch(fcode_env_t
*env
, char *service
)
133 private_data_t
*h
= DEVICE_PRIVATE(env
);
137 CHECK_DEPTH(env
, 1, service
);
139 error
= fc_run_priv(h
->common
, service
, 1, 1, fc_uint32_t2cell(cfgadd
),
143 throw_from_fclib(env
, 1, "pci:%s ( %x ) FAIL\n", service
,
150 pci_config_store(fcode_env_t
*env
, char *service
)
154 private_data_t
*h
= DEVICE_PRIVATE(env
);
158 CHECK_DEPTH(env
, 2, service
);
161 error
= fc_run_priv(h
->common
, service
, 2, 0, fc_uint32_t2cell(cfgadd
),
162 fc_uint32_t2cell(value
));
165 throw_from_fclib(env
, 1, "pci:%s ( %x %x ) FAIL\n", service
,
170 config_lfetch(fcode_env_t
*env
)
172 pci_config_fetch(env
, "config-l@");
176 config_lstore(fcode_env_t
*env
)
178 pci_config_store(env
, "config-l!");
182 config_wfetch(fcode_env_t
*env
)
184 pci_config_fetch(env
, "config-w@");
188 config_wstore(fcode_env_t
*env
)
190 pci_config_store(env
, "config-w!");
194 config_bfetch(fcode_env_t
*env
)
196 pci_config_fetch(env
, "config-b@");
200 config_bstore(fcode_env_t
*env
)
202 pci_config_store(env
, "config-b!");
206 do_map_in(fcode_env_t
*env
)
208 fstack_t phi
, pmid
, plo
, len
, addr
;
210 CHECK_DEPTH(env
, 4, "pci:map-in");
215 addr
= mem_map_in(env
, phi
, pmid
, plo
, len
);
220 do_map_out(fcode_env_t
*env
)
224 CHECK_DEPTH(env
, 2, "pci:map-out");
227 mem_map_out(env
, addr
, len
);
231 do_encode_unit(fcode_env_t
*env
)
237 CHECK_DEPTH(env
, 3, "pci:encode-unit");
242 fn
= ((hi
>> 8) & 0x7);
243 dev
= ((hi
>> 11) & 0x1f);
246 sprintf(enc_buf
, "%x,%x", dev
, fn
);
248 sprintf(enc_buf
, "%x", dev
);
250 debug_msg(DEBUG_REG_ACCESS
, "pci:encode-unit ( %x ) -> %s\n",
252 push_a_string(env
, STRDUP(enc_buf
));
256 do_decode_unit(fcode_env_t
*env
)
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",
276 do_device_id(fcode_env_t
*env
)
279 uint16_t ven_id
, dev_id
;
282 CHECK_DEPTH(env
, 3, "pci:device-id");
286 PUSH(DS
, cfgadd
+ PCI_CONF_VENID
);
289 PUSH(DS
, cfgadd
+ PCI_CONF_DEVID
);
292 sprintf(buf
, "pci%x,%x", ven_id
, dev_id
);
293 push_a_string(env
, STRDUP(buf
));
297 do_class_id(fcode_env_t
*env
)
300 uint8_t basclass
, subclass
, progclass
;
303 CHECK_DEPTH(env
, 3, "pci:class-id");
307 PUSH(DS
, cfgadd
+ PCI_CONF_BASCLASS
);
310 PUSH(DS
, cfgadd
+ PCI_CONF_SUBCLASS
);
313 PUSH(DS
, cfgadd
+ PCI_CONF_PROGCLASS
);
316 sprintf(buf
, "pciclass%02x%02x%02x", basclass
, subclass
, progclass
);
317 push_a_string(env
, STRDUP(buf
));
325 fcode_env_t
*env
= initial_env
;
328 ASSERT(env
->current_device
);
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
);