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