xref: /titanic_52/usr/src/lib/efcode/pci/pci.c (revision c7158ae983f5a04c4a998f468ecefba6d23ba721)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <strings.h>
32 
33 #include <fcode/private.h>
34 #include <fcode/log.h>
35 
36 #include <fcdriver/fcdriver.h>
37 
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)
41 {
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;
49 
50 	pa_offset = lo & PAGEOFFSET;
51 	lo &= PAGEMASK;
52 
53 	/* adjust the requested_len to a multiple of a pagesize */
54 	requested_len = (requested_len + pa_offset + PAGEOFFSET) & PAGEMASK;
55 
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);
59 
60 	if (error)
61 		throw_from_fclib(env, 1, "pci:%s: failed\n", service);
62 
63 	/*
64 	 * Check the requested_virt address and ensure that
65 	 * it starts at a page boundary.
66 	 */
67 
68 	va_offset = requested_virt & PAGEOFFSET;
69 
70 	if (va_offset != 0) {
71 
72 		/*
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.
76 		 */
77 
78 		adjusted_virt = requested_virt & PAGEMASK;
79 		adjusted_len = (requested_len + va_offset + PAGEOFFSET)
80 		    & PAGEMASK;
81 	}
82 
83 	mcookie = mapping_to_mcookie(requested_virt, requested_len,
84 	    adjusted_virt, adjusted_len);
85 
86 	if (mcookie == NULL)
87 		throw_from_fclib(env, 1, "pci-mapin-> pci:%s:"
88 		    " mapping_to_mcookie failed\n", service);
89 	/*
90 	 * Recalculate the address of the mcookie.
91 	 */
92 
93 	mcookie += va_offset + pa_offset;
94 
95 	debug_msg(DEBUG_REG_ACCESS, "pci:map-in: %llx -> %x\n",
96 	    (uint64_t)requested_virt, (uint32_t)mcookie);
97 
98 	return (mcookie);
99 }
100 
101 static void
102 mem_map_out(fcode_env_t *env, fstack_t mcookie, fstack_t requested_len)
103 {
104 	private_data_t *cdp = DEVICE_PRIVATE(env);
105 	char *service = "map-out";
106 	fc_cell_t requested_virt;
107 	int error;
108 
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);
119 	}
120 
121 	error = fc_run_priv(cdp->common, service, 2, 0,
122 	    fc_size2cell(requested_len), requested_virt);
123 
124 	if (error)
125 		log_message(MSG_ERROR, "pci:%s: failed\n", service);
126 }
127 
128 static void
129 pci_config_fetch(fcode_env_t *env, char *service)
130 {
131 	uint32_t cfgadd;
132 	fc_cell_t value;
133 	private_data_t	*h = DEVICE_PRIVATE(env);
134 	int error;
135 
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);
141 
142 	if (error)
143 		throw_from_fclib(env, 1, "pci:%s ( %x ) FAIL\n", service,
144 		    cfgadd);
145 
146 	PUSH(DS, value);
147 }
148 
149 static void
150 pci_config_store(fcode_env_t *env, char *service)
151 {
152 	uint32_t cfgadd;
153 	fc_cell_t value;
154 	private_data_t	*h = DEVICE_PRIVATE(env);
155 	int error;
156 
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));
163 
164 	if (error)
165 		throw_from_fclib(env, 1, "pci:%s ( %x %x ) FAIL\n", service,
166 		    cfgadd, value);
167 }
168 
169 static void
170 config_lfetch(fcode_env_t *env)
171 {
172 	pci_config_fetch(env, "config-l@");
173 }
174 
175 static void
176 config_lstore(fcode_env_t *env)
177 {
178 	pci_config_store(env, "config-l!");
179 }
180 
181 static void
182 config_wfetch(fcode_env_t *env)
183 {
184 	pci_config_fetch(env, "config-w@");
185 }
186 
187 static void
188 config_wstore(fcode_env_t *env)
189 {
190 	pci_config_store(env, "config-w!");
191 }
192 
193 static void
194 config_bfetch(fcode_env_t *env)
195 {
196 	pci_config_fetch(env, "config-b@");
197 }
198 
199 static void
200 config_bstore(fcode_env_t *env)
201 {
202 	pci_config_store(env, "config-b!");
203 }
204 
205 static void
206 do_map_in(fcode_env_t *env)
207 {
208 	fstack_t phi, pmid, plo, len, addr;
209 
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);
217 }
218 
219 static void
220 do_map_out(fcode_env_t *env)
221 {
222 	fstack_t addr, len;
223 
224 	CHECK_DEPTH(env, 2, "pci:map-out");
225 	len = POP(DS);
226 	addr = POP(DS);
227 	mem_map_out(env, addr, len);
228 }
229 
230 static void
231 do_encode_unit(fcode_env_t *env)
232 {
233 	char enc_buf[64];
234 	uint32_t hi;
235 	int dev, fn;
236 
237 	CHECK_DEPTH(env, 3, "pci:encode-unit");
238 	hi = POP(DS);
239 	(void) POP(DS);
240 	(void) POP(DS);
241 
242 	fn  = ((hi >> 8) & 0x7);
243 	dev = ((hi >> 11) & 0x1f);
244 
245 	if (fn) {
246 		sprintf(enc_buf, "%x,%x", dev, fn);
247 	} else {
248 		sprintf(enc_buf, "%x", dev);
249 	}
250 	debug_msg(DEBUG_REG_ACCESS, "pci:encode-unit ( %x ) -> %s\n",
251 	    hi, enc_buf);
252 	push_a_string(env, STRDUP(enc_buf));
253 }
254 
255 static void
256 do_decode_unit(fcode_env_t *env)
257 {
258 	int lo, hi, unit;
259 	char *buf;
260 
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);
265 	}
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);
273 }
274 
275 static void
276 do_device_id(fcode_env_t *env)
277 {
278 	uint32_t cfgadd;
279 	uint16_t ven_id, dev_id;
280 	char buf[40];
281 
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));
294 }
295 
296 static void
297 do_class_id(fcode_env_t *env)
298 {
299 	uint32_t cfgadd;
300 	uint8_t basclass, subclass, progclass;
301 	char buf[40];
302 
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));
318 }
319 
320 #pragma init(_init)
321 
322 static void
323 _init(void)
324 {
325 	fcode_env_t *env = initial_env;
326 
327 	ASSERT(env);
328 	ASSERT(env->current_device);
329 	NOTICE;
330 
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);
343 
344 	install_dma_methods(env);
345 }
346