1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * AGPGART driver frontend compatibility ioctls
3*4882a593Smuzhiyun * Copyright (C) 2004 Silicon Graphics, Inc.
4*4882a593Smuzhiyun * Copyright (C) 2002-2003 Dave Jones
5*4882a593Smuzhiyun * Copyright (C) 1999 Jeff Hartmann
6*4882a593Smuzhiyun * Copyright (C) 1999 Precision Insight, Inc.
7*4882a593Smuzhiyun * Copyright (C) 1999 Xi Graphics, Inc.
8*4882a593Smuzhiyun *
9*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
10*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
11*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
12*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
14*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
15*4882a593Smuzhiyun *
16*4882a593Smuzhiyun * The above copyright notice and this permission notice shall be included
17*4882a593Smuzhiyun * in all copies or substantial portions of the Software.
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20*4882a593Smuzhiyun * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22*4882a593Smuzhiyun * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
23*4882a593Smuzhiyun * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24*4882a593Smuzhiyun * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
25*4882a593Smuzhiyun * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <linux/kernel.h>
30*4882a593Smuzhiyun #include <linux/pci.h>
31*4882a593Smuzhiyun #include <linux/fs.h>
32*4882a593Smuzhiyun #include <linux/agpgart.h>
33*4882a593Smuzhiyun #include <linux/slab.h>
34*4882a593Smuzhiyun #include <linux/uaccess.h>
35*4882a593Smuzhiyun #include "agp.h"
36*4882a593Smuzhiyun #include "compat_ioctl.h"
37*4882a593Smuzhiyun
compat_agpioc_info_wrap(struct agp_file_private * priv,void __user * arg)38*4882a593Smuzhiyun static int compat_agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun struct agp_info32 userinfo;
41*4882a593Smuzhiyun struct agp_kern_info kerninfo;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun agp_copy_info(agp_bridge, &kerninfo);
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun userinfo.version.major = kerninfo.version.major;
46*4882a593Smuzhiyun userinfo.version.minor = kerninfo.version.minor;
47*4882a593Smuzhiyun userinfo.bridge_id = kerninfo.device->vendor |
48*4882a593Smuzhiyun (kerninfo.device->device << 16);
49*4882a593Smuzhiyun userinfo.agp_mode = kerninfo.mode;
50*4882a593Smuzhiyun userinfo.aper_base = (compat_long_t)kerninfo.aper_base;
51*4882a593Smuzhiyun userinfo.aper_size = kerninfo.aper_size;
52*4882a593Smuzhiyun userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
53*4882a593Smuzhiyun userinfo.pg_used = kerninfo.current_memory;
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun if (copy_to_user(arg, &userinfo, sizeof(userinfo)))
56*4882a593Smuzhiyun return -EFAULT;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun return 0;
59*4882a593Smuzhiyun }
60*4882a593Smuzhiyun
compat_agpioc_reserve_wrap(struct agp_file_private * priv,void __user * arg)61*4882a593Smuzhiyun static int compat_agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun struct agp_region32 ureserve;
64*4882a593Smuzhiyun struct agp_region kreserve;
65*4882a593Smuzhiyun struct agp_client *client;
66*4882a593Smuzhiyun struct agp_file_private *client_priv;
67*4882a593Smuzhiyun
68*4882a593Smuzhiyun DBG("");
69*4882a593Smuzhiyun if (copy_from_user(&ureserve, arg, sizeof(ureserve)))
70*4882a593Smuzhiyun return -EFAULT;
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun if ((unsigned) ureserve.seg_count >= ~0U/sizeof(struct agp_segment32))
73*4882a593Smuzhiyun return -EFAULT;
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun kreserve.pid = ureserve.pid;
76*4882a593Smuzhiyun kreserve.seg_count = ureserve.seg_count;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun client = agp_find_client_by_pid(kreserve.pid);
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun if (kreserve.seg_count == 0) {
81*4882a593Smuzhiyun /* remove a client */
82*4882a593Smuzhiyun client_priv = agp_find_private(kreserve.pid);
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun if (client_priv != NULL) {
85*4882a593Smuzhiyun set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
86*4882a593Smuzhiyun set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
87*4882a593Smuzhiyun }
88*4882a593Smuzhiyun if (client == NULL) {
89*4882a593Smuzhiyun /* client is already removed */
90*4882a593Smuzhiyun return 0;
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun return agp_remove_client(kreserve.pid);
93*4882a593Smuzhiyun } else {
94*4882a593Smuzhiyun struct agp_segment32 *usegment;
95*4882a593Smuzhiyun struct agp_segment *ksegment;
96*4882a593Smuzhiyun int seg;
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun if (ureserve.seg_count >= 16384)
99*4882a593Smuzhiyun return -EINVAL;
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun usegment = kmalloc_array(ureserve.seg_count,
102*4882a593Smuzhiyun sizeof(*usegment),
103*4882a593Smuzhiyun GFP_KERNEL);
104*4882a593Smuzhiyun if (!usegment)
105*4882a593Smuzhiyun return -ENOMEM;
106*4882a593Smuzhiyun
107*4882a593Smuzhiyun ksegment = kmalloc_array(kreserve.seg_count,
108*4882a593Smuzhiyun sizeof(*ksegment),
109*4882a593Smuzhiyun GFP_KERNEL);
110*4882a593Smuzhiyun if (!ksegment) {
111*4882a593Smuzhiyun kfree(usegment);
112*4882a593Smuzhiyun return -ENOMEM;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun if (copy_from_user(usegment, (void __user *) ureserve.seg_list,
116*4882a593Smuzhiyun sizeof(*usegment) * ureserve.seg_count)) {
117*4882a593Smuzhiyun kfree(usegment);
118*4882a593Smuzhiyun kfree(ksegment);
119*4882a593Smuzhiyun return -EFAULT;
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun for (seg = 0; seg < ureserve.seg_count; seg++) {
123*4882a593Smuzhiyun ksegment[seg].pg_start = usegment[seg].pg_start;
124*4882a593Smuzhiyun ksegment[seg].pg_count = usegment[seg].pg_count;
125*4882a593Smuzhiyun ksegment[seg].prot = usegment[seg].prot;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun kfree(usegment);
129*4882a593Smuzhiyun kreserve.seg_list = ksegment;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (client == NULL) {
132*4882a593Smuzhiyun /* Create the client and add the segment */
133*4882a593Smuzhiyun client = agp_create_client(kreserve.pid);
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun if (client == NULL) {
136*4882a593Smuzhiyun kfree(ksegment);
137*4882a593Smuzhiyun return -ENOMEM;
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun client_priv = agp_find_private(kreserve.pid);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun if (client_priv != NULL) {
142*4882a593Smuzhiyun set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
143*4882a593Smuzhiyun set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun return agp_create_segment(client, &kreserve);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun /* Will never really happen */
149*4882a593Smuzhiyun return -EINVAL;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun
compat_agpioc_allocate_wrap(struct agp_file_private * priv,void __user * arg)152*4882a593Smuzhiyun static int compat_agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
153*4882a593Smuzhiyun {
154*4882a593Smuzhiyun struct agp_memory *memory;
155*4882a593Smuzhiyun struct agp_allocate32 alloc;
156*4882a593Smuzhiyun
157*4882a593Smuzhiyun DBG("");
158*4882a593Smuzhiyun if (copy_from_user(&alloc, arg, sizeof(alloc)))
159*4882a593Smuzhiyun return -EFAULT;
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (memory == NULL)
164*4882a593Smuzhiyun return -ENOMEM;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun alloc.key = memory->key;
167*4882a593Smuzhiyun alloc.physical = memory->physical;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun if (copy_to_user(arg, &alloc, sizeof(alloc))) {
170*4882a593Smuzhiyun agp_free_memory_wrap(memory);
171*4882a593Smuzhiyun return -EFAULT;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun return 0;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
compat_agpioc_bind_wrap(struct agp_file_private * priv,void __user * arg)176*4882a593Smuzhiyun static int compat_agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun struct agp_bind32 bind_info;
179*4882a593Smuzhiyun struct agp_memory *memory;
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun DBG("");
182*4882a593Smuzhiyun if (copy_from_user(&bind_info, arg, sizeof(bind_info)))
183*4882a593Smuzhiyun return -EFAULT;
184*4882a593Smuzhiyun
185*4882a593Smuzhiyun memory = agp_find_mem_by_key(bind_info.key);
186*4882a593Smuzhiyun
187*4882a593Smuzhiyun if (memory == NULL)
188*4882a593Smuzhiyun return -EINVAL;
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun return agp_bind_memory(memory, bind_info.pg_start);
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
compat_agpioc_unbind_wrap(struct agp_file_private * priv,void __user * arg)193*4882a593Smuzhiyun static int compat_agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun struct agp_memory *memory;
196*4882a593Smuzhiyun struct agp_unbind32 unbind;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun DBG("");
199*4882a593Smuzhiyun if (copy_from_user(&unbind, arg, sizeof(unbind)))
200*4882a593Smuzhiyun return -EFAULT;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun memory = agp_find_mem_by_key(unbind.key);
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun if (memory == NULL)
205*4882a593Smuzhiyun return -EINVAL;
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun return agp_unbind_memory(memory);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
compat_agp_ioctl(struct file * file,unsigned int cmd,unsigned long arg)210*4882a593Smuzhiyun long compat_agp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun struct agp_file_private *curr_priv = file->private_data;
213*4882a593Smuzhiyun int ret_val = -ENOTTY;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun mutex_lock(&(agp_fe.agp_mutex));
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun if ((agp_fe.current_controller == NULL) &&
218*4882a593Smuzhiyun (cmd != AGPIOC_ACQUIRE32)) {
219*4882a593Smuzhiyun ret_val = -EINVAL;
220*4882a593Smuzhiyun goto ioctl_out;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun if ((agp_fe.backend_acquired != true) &&
223*4882a593Smuzhiyun (cmd != AGPIOC_ACQUIRE32)) {
224*4882a593Smuzhiyun ret_val = -EBUSY;
225*4882a593Smuzhiyun goto ioctl_out;
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun if (cmd != AGPIOC_ACQUIRE32) {
228*4882a593Smuzhiyun if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
229*4882a593Smuzhiyun ret_val = -EPERM;
230*4882a593Smuzhiyun goto ioctl_out;
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun /* Use the original pid of the controller,
233*4882a593Smuzhiyun * in case it's threaded */
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun if (agp_fe.current_controller->pid != curr_priv->my_pid) {
236*4882a593Smuzhiyun ret_val = -EBUSY;
237*4882a593Smuzhiyun goto ioctl_out;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun }
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun switch (cmd) {
242*4882a593Smuzhiyun case AGPIOC_INFO32:
243*4882a593Smuzhiyun ret_val = compat_agpioc_info_wrap(curr_priv, (void __user *) arg);
244*4882a593Smuzhiyun break;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun case AGPIOC_ACQUIRE32:
247*4882a593Smuzhiyun ret_val = agpioc_acquire_wrap(curr_priv);
248*4882a593Smuzhiyun break;
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun case AGPIOC_RELEASE32:
251*4882a593Smuzhiyun ret_val = agpioc_release_wrap(curr_priv);
252*4882a593Smuzhiyun break;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun case AGPIOC_SETUP32:
255*4882a593Smuzhiyun ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
256*4882a593Smuzhiyun break;
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun case AGPIOC_RESERVE32:
259*4882a593Smuzhiyun ret_val = compat_agpioc_reserve_wrap(curr_priv, (void __user *) arg);
260*4882a593Smuzhiyun break;
261*4882a593Smuzhiyun
262*4882a593Smuzhiyun case AGPIOC_PROTECT32:
263*4882a593Smuzhiyun ret_val = agpioc_protect_wrap(curr_priv);
264*4882a593Smuzhiyun break;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun case AGPIOC_ALLOCATE32:
267*4882a593Smuzhiyun ret_val = compat_agpioc_allocate_wrap(curr_priv, (void __user *) arg);
268*4882a593Smuzhiyun break;
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun case AGPIOC_DEALLOCATE32:
271*4882a593Smuzhiyun ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
272*4882a593Smuzhiyun break;
273*4882a593Smuzhiyun
274*4882a593Smuzhiyun case AGPIOC_BIND32:
275*4882a593Smuzhiyun ret_val = compat_agpioc_bind_wrap(curr_priv, (void __user *) arg);
276*4882a593Smuzhiyun break;
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun case AGPIOC_UNBIND32:
279*4882a593Smuzhiyun ret_val = compat_agpioc_unbind_wrap(curr_priv, (void __user *) arg);
280*4882a593Smuzhiyun break;
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun case AGPIOC_CHIPSET_FLUSH32:
283*4882a593Smuzhiyun break;
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun
286*4882a593Smuzhiyun ioctl_out:
287*4882a593Smuzhiyun DBG("ioctl returns %d\n", ret_val);
288*4882a593Smuzhiyun mutex_unlock(&(agp_fe.agp_mutex));
289*4882a593Smuzhiyun return ret_val;
290*4882a593Smuzhiyun }
291*4882a593Smuzhiyun
292