18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * AGPGART driver frontend compatibility ioctls
38c2ecf20Sopenharmony_ci * Copyright (C) 2004 Silicon Graphics, Inc.
48c2ecf20Sopenharmony_ci * Copyright (C) 2002-2003 Dave Jones
58c2ecf20Sopenharmony_ci * Copyright (C) 1999 Jeff Hartmann
68c2ecf20Sopenharmony_ci * Copyright (C) 1999 Precision Insight, Inc.
78c2ecf20Sopenharmony_ci * Copyright (C) 1999 Xi Graphics, Inc.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
108c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
118c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
128c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
138c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
148c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included
178c2ecf20Sopenharmony_ci * in all copies or substantial portions of the Software.
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
208c2ecf20Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
218c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
228c2ecf20Sopenharmony_ci * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
238c2ecf20Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
248c2ecf20Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
258c2ecf20Sopenharmony_ci * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
268c2ecf20Sopenharmony_ci *
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <linux/kernel.h>
308c2ecf20Sopenharmony_ci#include <linux/pci.h>
318c2ecf20Sopenharmony_ci#include <linux/fs.h>
328c2ecf20Sopenharmony_ci#include <linux/agpgart.h>
338c2ecf20Sopenharmony_ci#include <linux/slab.h>
348c2ecf20Sopenharmony_ci#include <linux/uaccess.h>
358c2ecf20Sopenharmony_ci#include "agp.h"
368c2ecf20Sopenharmony_ci#include "compat_ioctl.h"
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int compat_agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct agp_info32 userinfo;
418c2ecf20Sopenharmony_ci	struct agp_kern_info kerninfo;
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci	agp_copy_info(agp_bridge, &kerninfo);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	userinfo.version.major = kerninfo.version.major;
468c2ecf20Sopenharmony_ci	userinfo.version.minor = kerninfo.version.minor;
478c2ecf20Sopenharmony_ci	userinfo.bridge_id = kerninfo.device->vendor |
488c2ecf20Sopenharmony_ci	    (kerninfo.device->device << 16);
498c2ecf20Sopenharmony_ci	userinfo.agp_mode = kerninfo.mode;
508c2ecf20Sopenharmony_ci	userinfo.aper_base = (compat_long_t)kerninfo.aper_base;
518c2ecf20Sopenharmony_ci	userinfo.aper_size = kerninfo.aper_size;
528c2ecf20Sopenharmony_ci	userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
538c2ecf20Sopenharmony_ci	userinfo.pg_used = kerninfo.current_memory;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	if (copy_to_user(arg, &userinfo, sizeof(userinfo)))
568c2ecf20Sopenharmony_ci		return -EFAULT;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return 0;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic int compat_agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
628c2ecf20Sopenharmony_ci{
638c2ecf20Sopenharmony_ci	struct agp_region32 ureserve;
648c2ecf20Sopenharmony_ci	struct agp_region kreserve;
658c2ecf20Sopenharmony_ci	struct agp_client *client;
668c2ecf20Sopenharmony_ci	struct agp_file_private *client_priv;
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	DBG("");
698c2ecf20Sopenharmony_ci	if (copy_from_user(&ureserve, arg, sizeof(ureserve)))
708c2ecf20Sopenharmony_ci		return -EFAULT;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	if ((unsigned) ureserve.seg_count >= ~0U/sizeof(struct agp_segment32))
738c2ecf20Sopenharmony_ci		return -EFAULT;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	kreserve.pid = ureserve.pid;
768c2ecf20Sopenharmony_ci	kreserve.seg_count = ureserve.seg_count;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	client = agp_find_client_by_pid(kreserve.pid);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	if (kreserve.seg_count == 0) {
818c2ecf20Sopenharmony_ci		/* remove a client */
828c2ecf20Sopenharmony_ci		client_priv = agp_find_private(kreserve.pid);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci		if (client_priv != NULL) {
858c2ecf20Sopenharmony_ci			set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
868c2ecf20Sopenharmony_ci			set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
878c2ecf20Sopenharmony_ci		}
888c2ecf20Sopenharmony_ci		if (client == NULL) {
898c2ecf20Sopenharmony_ci			/* client is already removed */
908c2ecf20Sopenharmony_ci			return 0;
918c2ecf20Sopenharmony_ci		}
928c2ecf20Sopenharmony_ci		return agp_remove_client(kreserve.pid);
938c2ecf20Sopenharmony_ci	} else {
948c2ecf20Sopenharmony_ci		struct agp_segment32 *usegment;
958c2ecf20Sopenharmony_ci		struct agp_segment *ksegment;
968c2ecf20Sopenharmony_ci		int seg;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		if (ureserve.seg_count >= 16384)
998c2ecf20Sopenharmony_ci			return -EINVAL;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		usegment = kmalloc_array(ureserve.seg_count,
1028c2ecf20Sopenharmony_ci					 sizeof(*usegment),
1038c2ecf20Sopenharmony_ci					 GFP_KERNEL);
1048c2ecf20Sopenharmony_ci		if (!usegment)
1058c2ecf20Sopenharmony_ci			return -ENOMEM;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci		ksegment = kmalloc_array(kreserve.seg_count,
1088c2ecf20Sopenharmony_ci					 sizeof(*ksegment),
1098c2ecf20Sopenharmony_ci					 GFP_KERNEL);
1108c2ecf20Sopenharmony_ci		if (!ksegment) {
1118c2ecf20Sopenharmony_ci			kfree(usegment);
1128c2ecf20Sopenharmony_ci			return -ENOMEM;
1138c2ecf20Sopenharmony_ci		}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci		if (copy_from_user(usegment, (void __user *) ureserve.seg_list,
1168c2ecf20Sopenharmony_ci				   sizeof(*usegment) * ureserve.seg_count)) {
1178c2ecf20Sopenharmony_ci			kfree(usegment);
1188c2ecf20Sopenharmony_ci			kfree(ksegment);
1198c2ecf20Sopenharmony_ci			return -EFAULT;
1208c2ecf20Sopenharmony_ci		}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci		for (seg = 0; seg < ureserve.seg_count; seg++) {
1238c2ecf20Sopenharmony_ci			ksegment[seg].pg_start = usegment[seg].pg_start;
1248c2ecf20Sopenharmony_ci			ksegment[seg].pg_count = usegment[seg].pg_count;
1258c2ecf20Sopenharmony_ci			ksegment[seg].prot = usegment[seg].prot;
1268c2ecf20Sopenharmony_ci		}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci		kfree(usegment);
1298c2ecf20Sopenharmony_ci		kreserve.seg_list = ksegment;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci		if (client == NULL) {
1328c2ecf20Sopenharmony_ci			/* Create the client and add the segment */
1338c2ecf20Sopenharmony_ci			client = agp_create_client(kreserve.pid);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci			if (client == NULL) {
1368c2ecf20Sopenharmony_ci				kfree(ksegment);
1378c2ecf20Sopenharmony_ci				return -ENOMEM;
1388c2ecf20Sopenharmony_ci			}
1398c2ecf20Sopenharmony_ci			client_priv = agp_find_private(kreserve.pid);
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci			if (client_priv != NULL) {
1428c2ecf20Sopenharmony_ci				set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
1438c2ecf20Sopenharmony_ci				set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
1448c2ecf20Sopenharmony_ci			}
1458c2ecf20Sopenharmony_ci		}
1468c2ecf20Sopenharmony_ci		return agp_create_segment(client, &kreserve);
1478c2ecf20Sopenharmony_ci	}
1488c2ecf20Sopenharmony_ci	/* Will never really happen */
1498c2ecf20Sopenharmony_ci	return -EINVAL;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic int compat_agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	struct agp_memory *memory;
1558c2ecf20Sopenharmony_ci	struct agp_allocate32 alloc;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	DBG("");
1588c2ecf20Sopenharmony_ci	if (copy_from_user(&alloc, arg, sizeof(alloc)))
1598c2ecf20Sopenharmony_ci		return -EFAULT;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	if (memory == NULL)
1648c2ecf20Sopenharmony_ci		return -ENOMEM;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	alloc.key = memory->key;
1678c2ecf20Sopenharmony_ci	alloc.physical = memory->physical;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	if (copy_to_user(arg, &alloc, sizeof(alloc))) {
1708c2ecf20Sopenharmony_ci		agp_free_memory_wrap(memory);
1718c2ecf20Sopenharmony_ci		return -EFAULT;
1728c2ecf20Sopenharmony_ci	}
1738c2ecf20Sopenharmony_ci	return 0;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic int compat_agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	struct agp_bind32 bind_info;
1798c2ecf20Sopenharmony_ci	struct agp_memory *memory;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	DBG("");
1828c2ecf20Sopenharmony_ci	if (copy_from_user(&bind_info, arg, sizeof(bind_info)))
1838c2ecf20Sopenharmony_ci		return -EFAULT;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	memory = agp_find_mem_by_key(bind_info.key);
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (memory == NULL)
1888c2ecf20Sopenharmony_ci		return -EINVAL;
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	return agp_bind_memory(memory, bind_info.pg_start);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic int compat_agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct agp_memory *memory;
1968c2ecf20Sopenharmony_ci	struct agp_unbind32 unbind;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	DBG("");
1998c2ecf20Sopenharmony_ci	if (copy_from_user(&unbind, arg, sizeof(unbind)))
2008c2ecf20Sopenharmony_ci		return -EFAULT;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	memory = agp_find_mem_by_key(unbind.key);
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	if (memory == NULL)
2058c2ecf20Sopenharmony_ci		return -EINVAL;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	return agp_unbind_memory(memory);
2088c2ecf20Sopenharmony_ci}
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cilong compat_agp_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2118c2ecf20Sopenharmony_ci{
2128c2ecf20Sopenharmony_ci	struct agp_file_private *curr_priv = file->private_data;
2138c2ecf20Sopenharmony_ci	int ret_val = -ENOTTY;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci	mutex_lock(&(agp_fe.agp_mutex));
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	if ((agp_fe.current_controller == NULL) &&
2188c2ecf20Sopenharmony_ci	    (cmd != AGPIOC_ACQUIRE32)) {
2198c2ecf20Sopenharmony_ci		ret_val = -EINVAL;
2208c2ecf20Sopenharmony_ci		goto ioctl_out;
2218c2ecf20Sopenharmony_ci	}
2228c2ecf20Sopenharmony_ci	if ((agp_fe.backend_acquired != true) &&
2238c2ecf20Sopenharmony_ci	    (cmd != AGPIOC_ACQUIRE32)) {
2248c2ecf20Sopenharmony_ci		ret_val = -EBUSY;
2258c2ecf20Sopenharmony_ci		goto ioctl_out;
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci	if (cmd != AGPIOC_ACQUIRE32) {
2288c2ecf20Sopenharmony_ci		if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
2298c2ecf20Sopenharmony_ci			ret_val = -EPERM;
2308c2ecf20Sopenharmony_ci			goto ioctl_out;
2318c2ecf20Sopenharmony_ci		}
2328c2ecf20Sopenharmony_ci		/* Use the original pid of the controller,
2338c2ecf20Sopenharmony_ci		 * in case it's threaded */
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci		if (agp_fe.current_controller->pid != curr_priv->my_pid) {
2368c2ecf20Sopenharmony_ci			ret_val = -EBUSY;
2378c2ecf20Sopenharmony_ci			goto ioctl_out;
2388c2ecf20Sopenharmony_ci		}
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	switch (cmd) {
2428c2ecf20Sopenharmony_ci	case AGPIOC_INFO32:
2438c2ecf20Sopenharmony_ci		ret_val = compat_agpioc_info_wrap(curr_priv, (void __user *) arg);
2448c2ecf20Sopenharmony_ci		break;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	case AGPIOC_ACQUIRE32:
2478c2ecf20Sopenharmony_ci		ret_val = agpioc_acquire_wrap(curr_priv);
2488c2ecf20Sopenharmony_ci		break;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	case AGPIOC_RELEASE32:
2518c2ecf20Sopenharmony_ci		ret_val = agpioc_release_wrap(curr_priv);
2528c2ecf20Sopenharmony_ci		break;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	case AGPIOC_SETUP32:
2558c2ecf20Sopenharmony_ci		ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
2568c2ecf20Sopenharmony_ci		break;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci	case AGPIOC_RESERVE32:
2598c2ecf20Sopenharmony_ci		ret_val = compat_agpioc_reserve_wrap(curr_priv, (void __user *) arg);
2608c2ecf20Sopenharmony_ci		break;
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_ci	case AGPIOC_PROTECT32:
2638c2ecf20Sopenharmony_ci		ret_val = agpioc_protect_wrap(curr_priv);
2648c2ecf20Sopenharmony_ci		break;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	case AGPIOC_ALLOCATE32:
2678c2ecf20Sopenharmony_ci		ret_val = compat_agpioc_allocate_wrap(curr_priv, (void __user *) arg);
2688c2ecf20Sopenharmony_ci		break;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	case AGPIOC_DEALLOCATE32:
2718c2ecf20Sopenharmony_ci		ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
2728c2ecf20Sopenharmony_ci		break;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	case AGPIOC_BIND32:
2758c2ecf20Sopenharmony_ci		ret_val = compat_agpioc_bind_wrap(curr_priv, (void __user *) arg);
2768c2ecf20Sopenharmony_ci		break;
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	case AGPIOC_UNBIND32:
2798c2ecf20Sopenharmony_ci		ret_val = compat_agpioc_unbind_wrap(curr_priv, (void __user *) arg);
2808c2ecf20Sopenharmony_ci		break;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	case AGPIOC_CHIPSET_FLUSH32:
2838c2ecf20Sopenharmony_ci		break;
2848c2ecf20Sopenharmony_ci	}
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ciioctl_out:
2878c2ecf20Sopenharmony_ci	DBG("ioctl returns %d\n", ret_val);
2888c2ecf20Sopenharmony_ci	mutex_unlock(&(agp_fe.agp_mutex));
2898c2ecf20Sopenharmony_ci	return ret_val;
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
292