xref: /netbsd/sys/arch/alpha/alpha/sys_machdep.c (revision bf9ec67e)
1 /* $NetBSD: sys_machdep.c,v 1.14 2002/01/14 00:53:16 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1994, 1995, 1996 Carnegie-Mellon University.
41  * All rights reserved.
42  *
43  * Author: Chris G. Demetriou
44  *
45  * Permission to use, copy, modify and distribute this software and
46  * its documentation is hereby granted, provided that both the copyright
47  * notice and this permission notice appear in all copies of the
48  * software, derivative works or modified versions, and any portions
49  * thereof, and that both notices appear in supporting documentation.
50  *
51  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
52  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
53  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
54  *
55  * Carnegie Mellon requests users of this software to return to
56  *
57  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
58  *  School of Computer Science
59  *  Carnegie Mellon University
60  *  Pittsburgh PA 15213-3890
61  *
62  * any improvements or extensions that they make and grant Carnegie the
63  * rights to redistribute these changes.
64  */
65 
66 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
67 
68 __KERNEL_RCSID(0, "$NetBSD: sys_machdep.c,v 1.14 2002/01/14 00:53:16 thorpej Exp $");
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/device.h>
73 #include <sys/proc.h>
74 
75 #include <sys/mount.h>
76 #include <sys/syscallargs.h>
77 
78 #include <machine/fpu.h>
79 #include <machine/sysarch.h>
80 
81 #include <dev/pci/pcivar.h>
82 
83 u_int	alpha_bus_window_count[ALPHA_BUS_TYPE_MAX + 1];
84 
85 int	(*alpha_bus_get_window)(int, int,
86 	    struct alpha_bus_space_translation *);
87 
88 struct alpha_pci_chipset *alpha_pci_chipset;
89 
90 int
91 sys_sysarch(struct proc *p, void *v, register_t *retval)
92 {
93 	struct sys_sysarch_args /* {
94 		syscallarg(int) op;
95 		syscallarg(void *) parms;
96 	} */ *uap = v;
97 	int error = 0;
98 
99 	switch(SCARG(uap, op)) {
100 	case ALPHA_FPGETMASK:
101 		*retval = FP_C_TO_NETBSD_MASK(p->p_md.md_flags);
102 		break;
103 	case ALPHA_FPGETSTICKY:
104 		*retval = FP_C_TO_NETBSD_FLAG(p->p_md.md_flags);
105 		break;
106 	case ALPHA_FPSETMASK:
107 	case ALPHA_FPSETSTICKY:
108 	    {
109 		fp_except m;
110 		u_int64_t md_flags;
111 		struct alpha_fp_except_args args;
112 
113 		error = copyin(SCARG(uap, parms), &args, sizeof args);
114 		if (error)
115 			return error;
116 		m = args.mask;
117 		md_flags = p->p_md.md_flags;
118 		switch (SCARG(uap, op)) {
119 		case ALPHA_FPSETMASK:
120 			*retval = FP_C_TO_NETBSD_MASK(md_flags);
121 			md_flags = SET_FP_C_MASK(md_flags, m);
122 			break;
123 		case ALPHA_FPSETSTICKY:
124 			*retval = FP_C_TO_NETBSD_FLAG(md_flags);
125 			md_flags = SET_FP_C_FLAG(md_flags, m);
126 			break;
127 		}
128 		alpha_write_fp_c(p, md_flags);
129 		break;
130 	    }
131 	case ALPHA_GET_FP_C:
132 	    {
133 		struct alpha_fp_c_args args;
134 
135 		args.fp_c = alpha_read_fp_c(p);
136 		error = copyout(&args, SCARG(uap, parms), sizeof args);
137 		break;
138 	    }
139 	case ALPHA_SET_FP_C:
140 	    {
141 		struct alpha_fp_c_args args;
142 
143 		error = copyin(SCARG(uap, parms), &args, sizeof args);
144 		if (error)
145 			return (error);
146 		if ((args.fp_c >> 63) != 0)
147 			args.fp_c |= IEEE_INHERIT;
148 		alpha_write_fp_c(p, args.fp_c);
149 		break;
150 	    }
151 	case ALPHA_BUS_GET_WINDOW_COUNT:
152 	    {
153 		struct alpha_bus_get_window_count_args args;
154 
155 		error = copyin(SCARG(uap, parms), &args, sizeof(args));
156 		if (error)
157 			return (error);
158 
159 		if (args.type > ALPHA_BUS_TYPE_MAX)
160 			return (EINVAL);
161 
162 		if (alpha_bus_window_count[args.type] == 0)
163 			return (EOPNOTSUPP);
164 
165 		args.count = alpha_bus_window_count[args.type];
166 		error = copyout(&args, SCARG(uap, parms), sizeof(args));
167 		break;
168 	    }
169 
170 	case ALPHA_BUS_GET_WINDOW:
171 	    {
172 		struct alpha_bus_space_translation abst;
173 		struct alpha_bus_get_window_args args;
174 
175 		error = copyin(SCARG(uap, parms), &args, sizeof(args));
176 		if (error)
177 			return (error);
178 
179 		if (args.type > ALPHA_BUS_TYPE_MAX)
180 			return (EINVAL);
181 
182 		if (alpha_bus_window_count[args.type] == 0)
183 			return (EOPNOTSUPP);
184 
185 		if (args.window >= alpha_bus_window_count[args.type])
186 			return (EINVAL);
187 
188 		error = (*alpha_bus_get_window)(args.type, args.window,
189 		    &abst);
190 		if (error)
191 			return (error);
192 
193 		error = copyout(&abst, args.translation, sizeof(abst));
194 		break;
195 	    }
196 
197 	case ALPHA_PCI_CONF_READWRITE:
198 	    {
199 		struct alpha_pci_conf_readwrite_args args;
200 		pcitag_t tag;
201 
202 		error = copyin(SCARG(uap, parms), &args, sizeof(args));
203 		if (error)
204 			return (error);
205 
206 		if (alpha_pci_chipset == NULL)
207 			return (EOPNOTSUPP);
208 
209 		if (args.bus > 0xff)		/* XXX MAGIC NUMBER */
210 			return (EINVAL);
211 		if (args.device > pci_bus_maxdevs(alpha_pci_chipset, args.bus))
212 			return (EINVAL);
213 		if (args.function > 7)		/* XXX MAGIC NUMBER */
214 			return (EINVAL);
215 		if (args.reg > 0xff)		/* XXX MAGIC NUMBER */
216 			return (EINVAL);
217 
218 		tag = pci_make_tag(alpha_pci_chipset, args.bus, args.device,
219 		    args.function);
220 
221 		if (args.write)
222 			pci_conf_write(alpha_pci_chipset, tag, args.reg,
223 			    args.val);
224 		else {
225 			args.val = pci_conf_read(alpha_pci_chipset, tag,
226 			    args.reg);
227 			error = copyout(&args, SCARG(uap, parms), sizeof(args));
228 		}
229 		break;
230 	    }
231 
232 	default:
233 		error = EINVAL;
234 		break;
235 	}
236 
237 	return (error);
238 }
239