Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * AGPGART driver frontend compatibility ioctls
0003  * Copyright (C) 2004 Silicon Graphics, Inc.
0004  * Copyright (C) 2002-2003 Dave Jones
0005  * Copyright (C) 1999 Jeff Hartmann
0006  * Copyright (C) 1999 Precision Insight, Inc.
0007  * Copyright (C) 1999 Xi Graphics, Inc.
0008  *
0009  * Permission is hereby granted, free of charge, to any person obtaining a
0010  * copy of this software and associated documentation files (the "Software"),
0011  * to deal in the Software without restriction, including without limitation
0012  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
0013  * and/or sell copies of the Software, and to permit persons to whom the
0014  * Software is furnished to do so, subject to the following conditions:
0015  *
0016  * The above copyright notice and this permission notice shall be included
0017  * in all copies or substantial portions of the Software.
0018  *
0019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0020  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
0022  * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
0023  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
0024  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
0025  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0026  *
0027  */
0028 
0029 #include <linux/kernel.h>
0030 #include <linux/pci.h>
0031 #include <linux/fs.h>
0032 #include <linux/agpgart.h>
0033 #include <linux/slab.h>
0034 #include <linux/uaccess.h>
0035 #include "agp.h"
0036 #include "compat_ioctl.h"
0037 
0038 static int compat_agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
0039 {
0040     struct agp_info32 userinfo;
0041     struct agp_kern_info kerninfo;
0042 
0043     agp_copy_info(agp_bridge, &kerninfo);
0044 
0045     userinfo.version.major = kerninfo.version.major;
0046     userinfo.version.minor = kerninfo.version.minor;
0047     userinfo.bridge_id = kerninfo.device->vendor |
0048         (kerninfo.device->device << 16);
0049     userinfo.agp_mode = kerninfo.mode;
0050     userinfo.aper_base = (compat_long_t)kerninfo.aper_base;
0051     userinfo.aper_size = kerninfo.aper_size;
0052     userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
0053     userinfo.pg_used = kerninfo.current_memory;
0054 
0055     if (copy_to_user(arg, &userinfo, sizeof(userinfo)))
0056         return -EFAULT;
0057 
0058     return 0;
0059 }
0060 
0061 static int compat_agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
0062 {
0063     struct agp_region32 ureserve;
0064     struct agp_region kreserve;
0065     struct agp_client *client;
0066     struct agp_file_private *client_priv;
0067 
0068     DBG("");
0069     if (copy_from_user(&ureserve, arg, sizeof(ureserve)))
0070         return -EFAULT;
0071 
0072     if ((unsigned) ureserve.seg_count >= ~0U/sizeof(struct agp_segment32))
0073         return -EFAULT;
0074 
0075     kreserve.pid = ureserve.pid;
0076     kreserve.seg_count = ureserve.seg_count;
0077 
0078     client = agp_find_client_by_pid(kreserve.pid);
0079 
0080     if (kreserve.seg_count == 0) {
0081         /* remove a client */
0082         client_priv = agp_find_private(kreserve.pid);
0083 
0084         if (client_priv != NULL) {
0085             set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
0086             set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
0087         }
0088         if (client == NULL) {
0089             /* client is already removed */
0090             return 0;
0091         }
0092         return agp_remove_client(kreserve.pid);
0093     } else {
0094         struct agp_segment32 *usegment;
0095         struct agp_segment *ksegment;
0096         int seg;
0097 
0098         if (ureserve.seg_count >= 16384)
0099             return -EINVAL;
0100 
0101         usegment = kmalloc_array(ureserve.seg_count,
0102                      sizeof(*usegment),
0103                      GFP_KERNEL);
0104         if (!usegment)
0105             return -ENOMEM;
0106 
0107         ksegment = kmalloc_array(kreserve.seg_count,
0108                      sizeof(*ksegment),
0109                      GFP_KERNEL);
0110         if (!ksegment) {
0111             kfree(usegment);
0112             return -ENOMEM;
0113         }
0114 
0115         if (copy_from_user(usegment, (void __user *) ureserve.seg_list,
0116                    sizeof(*usegment) * ureserve.seg_count)) {
0117             kfree(usegment);
0118             kfree(ksegment);
0119             return -EFAULT;
0120         }
0121 
0122         for (seg = 0; seg < ureserve.seg_count; seg++) {
0123             ksegment[seg].pg_start = usegment[seg].pg_start;
0124             ksegment[seg].pg_count = usegment[seg].pg_count;
0125             ksegment[seg].prot = usegment[seg].prot;
0126         }
0127 
0128         kfree(usegment);
0129         kreserve.seg_list = ksegment;
0130 
0131         if (client == NULL) {
0132             /* Create the client and add the segment */
0133             client = agp_create_client(kreserve.pid);
0134 
0135             if (client == NULL) {
0136                 kfree(ksegment);
0137                 return -ENOMEM;
0138             }
0139             client_priv = agp_find_private(kreserve.pid);
0140 
0141             if (client_priv != NULL) {
0142                 set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
0143                 set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
0144             }
0145         }
0146         return agp_create_segment(client, &kreserve);
0147     }
0148     /* Will never really happen */
0149     return -EINVAL;
0150 }
0151 
0152 static int compat_agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
0153 {
0154     struct agp_memory *memory;
0155     struct agp_allocate32 alloc;
0156 
0157     DBG("");
0158     if (copy_from_user(&alloc, arg, sizeof(alloc)))
0159         return -EFAULT;
0160 
0161     memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
0162 
0163     if (memory == NULL)
0164         return -ENOMEM;
0165 
0166     alloc.key = memory->key;
0167     alloc.physical = memory->physical;
0168 
0169     if (copy_to_user(arg, &alloc, sizeof(alloc))) {
0170         agp_free_memory_wrap(memory);
0171         return -EFAULT;
0172     }
0173     return 0;
0174 }
0175 
0176 static int compat_agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
0177 {
0178     struct agp_bind32 bind_info;
0179     struct agp_memory *memory;
0180 
0181     DBG("");
0182     if (copy_from_user(&bind_info, arg, sizeof(bind_info)))
0183         return -EFAULT;
0184 
0185     memory = agp_find_mem_by_key(bind_info.key);
0186 
0187     if (memory == NULL)
0188         return -EINVAL;
0189 
0190     return agp_bind_memory(memory, bind_info.pg_start);
0191 }
0192 
0193 static int compat_agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
0194 {
0195     struct agp_memory *memory;
0196     struct agp_unbind32 unbind;
0197 
0198     DBG("");
0199     if (copy_from_user(&unbind, arg, sizeof(unbind)))
0200         return -EFAULT;
0201 
0202     memory = agp_find_mem_by_key(unbind.key);
0203 
0204     if (memory == NULL)
0205         return -EINVAL;
0206 
0207     return agp_unbind_memory(memory);
0208 }
0209 
0210 long compat_agp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
0211 {
0212     struct agp_file_private *curr_priv = file->private_data;
0213     int ret_val = -ENOTTY;
0214 
0215     mutex_lock(&(agp_fe.agp_mutex));
0216 
0217     if ((agp_fe.current_controller == NULL) &&
0218         (cmd != AGPIOC_ACQUIRE32)) {
0219         ret_val = -EINVAL;
0220         goto ioctl_out;
0221     }
0222     if ((agp_fe.backend_acquired != true) &&
0223         (cmd != AGPIOC_ACQUIRE32)) {
0224         ret_val = -EBUSY;
0225         goto ioctl_out;
0226     }
0227     if (cmd != AGPIOC_ACQUIRE32) {
0228         if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
0229             ret_val = -EPERM;
0230             goto ioctl_out;
0231         }
0232         /* Use the original pid of the controller,
0233          * in case it's threaded */
0234 
0235         if (agp_fe.current_controller->pid != curr_priv->my_pid) {
0236             ret_val = -EBUSY;
0237             goto ioctl_out;
0238         }
0239     }
0240 
0241     switch (cmd) {
0242     case AGPIOC_INFO32:
0243         ret_val = compat_agpioc_info_wrap(curr_priv, (void __user *) arg);
0244         break;
0245 
0246     case AGPIOC_ACQUIRE32:
0247         ret_val = agpioc_acquire_wrap(curr_priv);
0248         break;
0249 
0250     case AGPIOC_RELEASE32:
0251         ret_val = agpioc_release_wrap(curr_priv);
0252         break;
0253 
0254     case AGPIOC_SETUP32:
0255         ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
0256         break;
0257 
0258     case AGPIOC_RESERVE32:
0259         ret_val = compat_agpioc_reserve_wrap(curr_priv, (void __user *) arg);
0260         break;
0261 
0262     case AGPIOC_PROTECT32:
0263         ret_val = agpioc_protect_wrap(curr_priv);
0264         break;
0265 
0266     case AGPIOC_ALLOCATE32:
0267         ret_val = compat_agpioc_allocate_wrap(curr_priv, (void __user *) arg);
0268         break;
0269 
0270     case AGPIOC_DEALLOCATE32:
0271         ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
0272         break;
0273 
0274     case AGPIOC_BIND32:
0275         ret_val = compat_agpioc_bind_wrap(curr_priv, (void __user *) arg);
0276         break;
0277 
0278     case AGPIOC_UNBIND32:
0279         ret_val = compat_agpioc_unbind_wrap(curr_priv, (void __user *) arg);
0280         break;
0281 
0282     case AGPIOC_CHIPSET_FLUSH32:
0283         break;
0284     }
0285 
0286 ioctl_out:
0287     DBG("ioctl returns %d\n", ret_val);
0288     mutex_unlock(&(agp_fe.agp_mutex));
0289     return ret_val;
0290 }
0291