18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * AGPGART driver frontend 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/types.h> 308c2ecf20Sopenharmony_ci#include <linux/kernel.h> 318c2ecf20Sopenharmony_ci#include <linux/module.h> 328c2ecf20Sopenharmony_ci#include <linux/mman.h> 338c2ecf20Sopenharmony_ci#include <linux/pci.h> 348c2ecf20Sopenharmony_ci#include <linux/miscdevice.h> 358c2ecf20Sopenharmony_ci#include <linux/agp_backend.h> 368c2ecf20Sopenharmony_ci#include <linux/agpgart.h> 378c2ecf20Sopenharmony_ci#include <linux/slab.h> 388c2ecf20Sopenharmony_ci#include <linux/mm.h> 398c2ecf20Sopenharmony_ci#include <linux/fs.h> 408c2ecf20Sopenharmony_ci#include <linux/sched.h> 418c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 428c2ecf20Sopenharmony_ci#include "agp.h" 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistruct agp_front_data agp_fe; 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistruct agp_memory *agp_find_mem_by_key(int key) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci struct agp_memory *curr; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci if (agp_fe.current_controller == NULL) 518c2ecf20Sopenharmony_ci return NULL; 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ci curr = agp_fe.current_controller->pool; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci while (curr != NULL) { 568c2ecf20Sopenharmony_ci if (curr->key == key) 578c2ecf20Sopenharmony_ci break; 588c2ecf20Sopenharmony_ci curr = curr->next; 598c2ecf20Sopenharmony_ci } 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci DBG("key=%d -> mem=%p", key, curr); 628c2ecf20Sopenharmony_ci return curr; 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic void agp_remove_from_pool(struct agp_memory *temp) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct agp_memory *prev; 688c2ecf20Sopenharmony_ci struct agp_memory *next; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci /* Check to see if this is even in the memory pool */ 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci DBG("mem=%p", temp); 738c2ecf20Sopenharmony_ci if (agp_find_mem_by_key(temp->key) != NULL) { 748c2ecf20Sopenharmony_ci next = temp->next; 758c2ecf20Sopenharmony_ci prev = temp->prev; 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci if (prev != NULL) { 788c2ecf20Sopenharmony_ci prev->next = next; 798c2ecf20Sopenharmony_ci if (next != NULL) 808c2ecf20Sopenharmony_ci next->prev = prev; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci } else { 838c2ecf20Sopenharmony_ci /* This is the first item on the list */ 848c2ecf20Sopenharmony_ci if (next != NULL) 858c2ecf20Sopenharmony_ci next->prev = NULL; 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci agp_fe.current_controller->pool = next; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci } 908c2ecf20Sopenharmony_ci} 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci/* 938c2ecf20Sopenharmony_ci * Routines for managing each client's segment list - 948c2ecf20Sopenharmony_ci * These routines handle adding and removing segments 958c2ecf20Sopenharmony_ci * to each auth'ed client. 968c2ecf20Sopenharmony_ci */ 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic struct 998c2ecf20Sopenharmony_ciagp_segment_priv *agp_find_seg_in_client(const struct agp_client *client, 1008c2ecf20Sopenharmony_ci unsigned long offset, 1018c2ecf20Sopenharmony_ci int size, pgprot_t page_prot) 1028c2ecf20Sopenharmony_ci{ 1038c2ecf20Sopenharmony_ci struct agp_segment_priv *seg; 1048c2ecf20Sopenharmony_ci int i; 1058c2ecf20Sopenharmony_ci off_t pg_start; 1068c2ecf20Sopenharmony_ci size_t pg_count; 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci pg_start = offset / 4096; 1098c2ecf20Sopenharmony_ci pg_count = size / 4096; 1108c2ecf20Sopenharmony_ci seg = *(client->segments); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci for (i = 0; i < client->num_segments; i++) { 1138c2ecf20Sopenharmony_ci if ((seg[i].pg_start == pg_start) && 1148c2ecf20Sopenharmony_ci (seg[i].pg_count == pg_count) && 1158c2ecf20Sopenharmony_ci (pgprot_val(seg[i].prot) == pgprot_val(page_prot))) { 1168c2ecf20Sopenharmony_ci return seg + i; 1178c2ecf20Sopenharmony_ci } 1188c2ecf20Sopenharmony_ci } 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci return NULL; 1218c2ecf20Sopenharmony_ci} 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_cistatic void agp_remove_seg_from_client(struct agp_client *client) 1248c2ecf20Sopenharmony_ci{ 1258c2ecf20Sopenharmony_ci DBG("client=%p", client); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci if (client->segments != NULL) { 1288c2ecf20Sopenharmony_ci if (*(client->segments) != NULL) { 1298c2ecf20Sopenharmony_ci DBG("Freeing %p from client %p", *(client->segments), client); 1308c2ecf20Sopenharmony_ci kfree(*(client->segments)); 1318c2ecf20Sopenharmony_ci } 1328c2ecf20Sopenharmony_ci DBG("Freeing %p from client %p", client->segments, client); 1338c2ecf20Sopenharmony_ci kfree(client->segments); 1348c2ecf20Sopenharmony_ci client->segments = NULL; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic void agp_add_seg_to_client(struct agp_client *client, 1398c2ecf20Sopenharmony_ci struct agp_segment_priv ** seg, int num_segments) 1408c2ecf20Sopenharmony_ci{ 1418c2ecf20Sopenharmony_ci struct agp_segment_priv **prev_seg; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci prev_seg = client->segments; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci if (prev_seg != NULL) 1468c2ecf20Sopenharmony_ci agp_remove_seg_from_client(client); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci DBG("Adding seg %p (%d segments) to client %p", seg, num_segments, client); 1498c2ecf20Sopenharmony_ci client->num_segments = num_segments; 1508c2ecf20Sopenharmony_ci client->segments = seg; 1518c2ecf20Sopenharmony_ci} 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_cistatic pgprot_t agp_convert_mmap_flags(int prot) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci unsigned long prot_bits; 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci prot_bits = calc_vm_prot_bits(prot, 0) | VM_SHARED; 1588c2ecf20Sopenharmony_ci return vm_get_page_prot(prot_bits); 1598c2ecf20Sopenharmony_ci} 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ciint agp_create_segment(struct agp_client *client, struct agp_region *region) 1628c2ecf20Sopenharmony_ci{ 1638c2ecf20Sopenharmony_ci struct agp_segment_priv **ret_seg; 1648c2ecf20Sopenharmony_ci struct agp_segment_priv *seg; 1658c2ecf20Sopenharmony_ci struct agp_segment *user_seg; 1668c2ecf20Sopenharmony_ci size_t i; 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL); 1698c2ecf20Sopenharmony_ci if (seg == NULL) { 1708c2ecf20Sopenharmony_ci kfree(region->seg_list); 1718c2ecf20Sopenharmony_ci region->seg_list = NULL; 1728c2ecf20Sopenharmony_ci return -ENOMEM; 1738c2ecf20Sopenharmony_ci } 1748c2ecf20Sopenharmony_ci user_seg = region->seg_list; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci for (i = 0; i < region->seg_count; i++) { 1778c2ecf20Sopenharmony_ci seg[i].pg_start = user_seg[i].pg_start; 1788c2ecf20Sopenharmony_ci seg[i].pg_count = user_seg[i].pg_count; 1798c2ecf20Sopenharmony_ci seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot); 1808c2ecf20Sopenharmony_ci } 1818c2ecf20Sopenharmony_ci kfree(region->seg_list); 1828c2ecf20Sopenharmony_ci region->seg_list = NULL; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci ret_seg = kmalloc(sizeof(void *), GFP_KERNEL); 1858c2ecf20Sopenharmony_ci if (ret_seg == NULL) { 1868c2ecf20Sopenharmony_ci kfree(seg); 1878c2ecf20Sopenharmony_ci return -ENOMEM; 1888c2ecf20Sopenharmony_ci } 1898c2ecf20Sopenharmony_ci *ret_seg = seg; 1908c2ecf20Sopenharmony_ci agp_add_seg_to_client(client, ret_seg, region->seg_count); 1918c2ecf20Sopenharmony_ci return 0; 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_ci/* End - Routines for managing each client's segment list */ 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci/* This function must only be called when current_controller != NULL */ 1978c2ecf20Sopenharmony_cistatic void agp_insert_into_pool(struct agp_memory * temp) 1988c2ecf20Sopenharmony_ci{ 1998c2ecf20Sopenharmony_ci struct agp_memory *prev; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci prev = agp_fe.current_controller->pool; 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci if (prev != NULL) { 2048c2ecf20Sopenharmony_ci prev->prev = temp; 2058c2ecf20Sopenharmony_ci temp->next = prev; 2068c2ecf20Sopenharmony_ci } 2078c2ecf20Sopenharmony_ci agp_fe.current_controller->pool = temp; 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci/* File private list routines */ 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_cistruct agp_file_private *agp_find_private(pid_t pid) 2148c2ecf20Sopenharmony_ci{ 2158c2ecf20Sopenharmony_ci struct agp_file_private *curr; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci curr = agp_fe.file_priv_list; 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci while (curr != NULL) { 2208c2ecf20Sopenharmony_ci if (curr->my_pid == pid) 2218c2ecf20Sopenharmony_ci return curr; 2228c2ecf20Sopenharmony_ci curr = curr->next; 2238c2ecf20Sopenharmony_ci } 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci return NULL; 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic void agp_insert_file_private(struct agp_file_private * priv) 2298c2ecf20Sopenharmony_ci{ 2308c2ecf20Sopenharmony_ci struct agp_file_private *prev; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci prev = agp_fe.file_priv_list; 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci if (prev != NULL) 2358c2ecf20Sopenharmony_ci prev->prev = priv; 2368c2ecf20Sopenharmony_ci priv->next = prev; 2378c2ecf20Sopenharmony_ci agp_fe.file_priv_list = priv; 2388c2ecf20Sopenharmony_ci} 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_cistatic void agp_remove_file_private(struct agp_file_private * priv) 2418c2ecf20Sopenharmony_ci{ 2428c2ecf20Sopenharmony_ci struct agp_file_private *next; 2438c2ecf20Sopenharmony_ci struct agp_file_private *prev; 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci next = priv->next; 2468c2ecf20Sopenharmony_ci prev = priv->prev; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci if (prev != NULL) { 2498c2ecf20Sopenharmony_ci prev->next = next; 2508c2ecf20Sopenharmony_ci 2518c2ecf20Sopenharmony_ci if (next != NULL) 2528c2ecf20Sopenharmony_ci next->prev = prev; 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci } else { 2558c2ecf20Sopenharmony_ci if (next != NULL) 2568c2ecf20Sopenharmony_ci next->prev = NULL; 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci agp_fe.file_priv_list = next; 2598c2ecf20Sopenharmony_ci } 2608c2ecf20Sopenharmony_ci} 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci/* End - File flag list routines */ 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci/* 2658c2ecf20Sopenharmony_ci * Wrappers for agp_free_memory & agp_allocate_memory 2668c2ecf20Sopenharmony_ci * These make sure that internal lists are kept updated. 2678c2ecf20Sopenharmony_ci */ 2688c2ecf20Sopenharmony_civoid agp_free_memory_wrap(struct agp_memory *memory) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci agp_remove_from_pool(memory); 2718c2ecf20Sopenharmony_ci agp_free_memory(memory); 2728c2ecf20Sopenharmony_ci} 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_cistruct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type) 2758c2ecf20Sopenharmony_ci{ 2768c2ecf20Sopenharmony_ci struct agp_memory *memory; 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ci memory = agp_allocate_memory(agp_bridge, pg_count, type); 2798c2ecf20Sopenharmony_ci if (memory == NULL) 2808c2ecf20Sopenharmony_ci return NULL; 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_ci agp_insert_into_pool(memory); 2838c2ecf20Sopenharmony_ci return memory; 2848c2ecf20Sopenharmony_ci} 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ci/* Routines for managing the list of controllers - 2878c2ecf20Sopenharmony_ci * These routines manage the current controller, and the list of 2888c2ecf20Sopenharmony_ci * controllers 2898c2ecf20Sopenharmony_ci */ 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_cistatic struct agp_controller *agp_find_controller_by_pid(pid_t id) 2928c2ecf20Sopenharmony_ci{ 2938c2ecf20Sopenharmony_ci struct agp_controller *controller; 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci controller = agp_fe.controllers; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci while (controller != NULL) { 2988c2ecf20Sopenharmony_ci if (controller->pid == id) 2998c2ecf20Sopenharmony_ci return controller; 3008c2ecf20Sopenharmony_ci controller = controller->next; 3018c2ecf20Sopenharmony_ci } 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci return NULL; 3048c2ecf20Sopenharmony_ci} 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_cistatic struct agp_controller *agp_create_controller(pid_t id) 3078c2ecf20Sopenharmony_ci{ 3088c2ecf20Sopenharmony_ci struct agp_controller *controller; 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL); 3118c2ecf20Sopenharmony_ci if (controller == NULL) 3128c2ecf20Sopenharmony_ci return NULL; 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci controller->pid = id; 3158c2ecf20Sopenharmony_ci return controller; 3168c2ecf20Sopenharmony_ci} 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_cistatic int agp_insert_controller(struct agp_controller *controller) 3198c2ecf20Sopenharmony_ci{ 3208c2ecf20Sopenharmony_ci struct agp_controller *prev_controller; 3218c2ecf20Sopenharmony_ci 3228c2ecf20Sopenharmony_ci prev_controller = agp_fe.controllers; 3238c2ecf20Sopenharmony_ci controller->next = prev_controller; 3248c2ecf20Sopenharmony_ci 3258c2ecf20Sopenharmony_ci if (prev_controller != NULL) 3268c2ecf20Sopenharmony_ci prev_controller->prev = controller; 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ci agp_fe.controllers = controller; 3298c2ecf20Sopenharmony_ci 3308c2ecf20Sopenharmony_ci return 0; 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_cistatic void agp_remove_all_clients(struct agp_controller *controller) 3348c2ecf20Sopenharmony_ci{ 3358c2ecf20Sopenharmony_ci struct agp_client *client; 3368c2ecf20Sopenharmony_ci struct agp_client *temp; 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci client = controller->clients; 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci while (client) { 3418c2ecf20Sopenharmony_ci struct agp_file_private *priv; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci temp = client; 3448c2ecf20Sopenharmony_ci agp_remove_seg_from_client(temp); 3458c2ecf20Sopenharmony_ci priv = agp_find_private(temp->pid); 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci if (priv != NULL) { 3488c2ecf20Sopenharmony_ci clear_bit(AGP_FF_IS_VALID, &priv->access_flags); 3498c2ecf20Sopenharmony_ci clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags); 3508c2ecf20Sopenharmony_ci } 3518c2ecf20Sopenharmony_ci client = client->next; 3528c2ecf20Sopenharmony_ci kfree(temp); 3538c2ecf20Sopenharmony_ci } 3548c2ecf20Sopenharmony_ci} 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_cistatic void agp_remove_all_memory(struct agp_controller *controller) 3578c2ecf20Sopenharmony_ci{ 3588c2ecf20Sopenharmony_ci struct agp_memory *memory; 3598c2ecf20Sopenharmony_ci struct agp_memory *temp; 3608c2ecf20Sopenharmony_ci 3618c2ecf20Sopenharmony_ci memory = controller->pool; 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci while (memory) { 3648c2ecf20Sopenharmony_ci temp = memory; 3658c2ecf20Sopenharmony_ci memory = memory->next; 3668c2ecf20Sopenharmony_ci agp_free_memory_wrap(temp); 3678c2ecf20Sopenharmony_ci } 3688c2ecf20Sopenharmony_ci} 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_cistatic int agp_remove_controller(struct agp_controller *controller) 3718c2ecf20Sopenharmony_ci{ 3728c2ecf20Sopenharmony_ci struct agp_controller *prev_controller; 3738c2ecf20Sopenharmony_ci struct agp_controller *next_controller; 3748c2ecf20Sopenharmony_ci 3758c2ecf20Sopenharmony_ci prev_controller = controller->prev; 3768c2ecf20Sopenharmony_ci next_controller = controller->next; 3778c2ecf20Sopenharmony_ci 3788c2ecf20Sopenharmony_ci if (prev_controller != NULL) { 3798c2ecf20Sopenharmony_ci prev_controller->next = next_controller; 3808c2ecf20Sopenharmony_ci if (next_controller != NULL) 3818c2ecf20Sopenharmony_ci next_controller->prev = prev_controller; 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci } else { 3848c2ecf20Sopenharmony_ci if (next_controller != NULL) 3858c2ecf20Sopenharmony_ci next_controller->prev = NULL; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci agp_fe.controllers = next_controller; 3888c2ecf20Sopenharmony_ci } 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_ci agp_remove_all_memory(controller); 3918c2ecf20Sopenharmony_ci agp_remove_all_clients(controller); 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci if (agp_fe.current_controller == controller) { 3948c2ecf20Sopenharmony_ci agp_fe.current_controller = NULL; 3958c2ecf20Sopenharmony_ci agp_fe.backend_acquired = false; 3968c2ecf20Sopenharmony_ci agp_backend_release(agp_bridge); 3978c2ecf20Sopenharmony_ci } 3988c2ecf20Sopenharmony_ci kfree(controller); 3998c2ecf20Sopenharmony_ci return 0; 4008c2ecf20Sopenharmony_ci} 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_cistatic void agp_controller_make_current(struct agp_controller *controller) 4038c2ecf20Sopenharmony_ci{ 4048c2ecf20Sopenharmony_ci struct agp_client *clients; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci clients = controller->clients; 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci while (clients != NULL) { 4098c2ecf20Sopenharmony_ci struct agp_file_private *priv; 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_ci priv = agp_find_private(clients->pid); 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ci if (priv != NULL) { 4148c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_VALID, &priv->access_flags); 4158c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_CLIENT, &priv->access_flags); 4168c2ecf20Sopenharmony_ci } 4178c2ecf20Sopenharmony_ci clients = clients->next; 4188c2ecf20Sopenharmony_ci } 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci agp_fe.current_controller = controller; 4218c2ecf20Sopenharmony_ci} 4228c2ecf20Sopenharmony_ci 4238c2ecf20Sopenharmony_cistatic void agp_controller_release_current(struct agp_controller *controller, 4248c2ecf20Sopenharmony_ci struct agp_file_private *controller_priv) 4258c2ecf20Sopenharmony_ci{ 4268c2ecf20Sopenharmony_ci struct agp_client *clients; 4278c2ecf20Sopenharmony_ci 4288c2ecf20Sopenharmony_ci clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags); 4298c2ecf20Sopenharmony_ci clients = controller->clients; 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci while (clients != NULL) { 4328c2ecf20Sopenharmony_ci struct agp_file_private *priv; 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ci priv = agp_find_private(clients->pid); 4358c2ecf20Sopenharmony_ci 4368c2ecf20Sopenharmony_ci if (priv != NULL) 4378c2ecf20Sopenharmony_ci clear_bit(AGP_FF_IS_VALID, &priv->access_flags); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_ci clients = clients->next; 4408c2ecf20Sopenharmony_ci } 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci agp_fe.current_controller = NULL; 4438c2ecf20Sopenharmony_ci agp_fe.used_by_controller = false; 4448c2ecf20Sopenharmony_ci agp_backend_release(agp_bridge); 4458c2ecf20Sopenharmony_ci} 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci/* 4488c2ecf20Sopenharmony_ci * Routines for managing client lists - 4498c2ecf20Sopenharmony_ci * These routines are for managing the list of auth'ed clients. 4508c2ecf20Sopenharmony_ci */ 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_cistatic struct agp_client 4538c2ecf20Sopenharmony_ci*agp_find_client_in_controller(struct agp_controller *controller, pid_t id) 4548c2ecf20Sopenharmony_ci{ 4558c2ecf20Sopenharmony_ci struct agp_client *client; 4568c2ecf20Sopenharmony_ci 4578c2ecf20Sopenharmony_ci if (controller == NULL) 4588c2ecf20Sopenharmony_ci return NULL; 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci client = controller->clients; 4618c2ecf20Sopenharmony_ci 4628c2ecf20Sopenharmony_ci while (client != NULL) { 4638c2ecf20Sopenharmony_ci if (client->pid == id) 4648c2ecf20Sopenharmony_ci return client; 4658c2ecf20Sopenharmony_ci client = client->next; 4668c2ecf20Sopenharmony_ci } 4678c2ecf20Sopenharmony_ci 4688c2ecf20Sopenharmony_ci return NULL; 4698c2ecf20Sopenharmony_ci} 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_cistatic struct agp_controller *agp_find_controller_for_client(pid_t id) 4728c2ecf20Sopenharmony_ci{ 4738c2ecf20Sopenharmony_ci struct agp_controller *controller; 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci controller = agp_fe.controllers; 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci while (controller != NULL) { 4788c2ecf20Sopenharmony_ci if ((agp_find_client_in_controller(controller, id)) != NULL) 4798c2ecf20Sopenharmony_ci return controller; 4808c2ecf20Sopenharmony_ci controller = controller->next; 4818c2ecf20Sopenharmony_ci } 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci return NULL; 4848c2ecf20Sopenharmony_ci} 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_cistruct agp_client *agp_find_client_by_pid(pid_t id) 4878c2ecf20Sopenharmony_ci{ 4888c2ecf20Sopenharmony_ci struct agp_client *temp; 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_ci if (agp_fe.current_controller == NULL) 4918c2ecf20Sopenharmony_ci return NULL; 4928c2ecf20Sopenharmony_ci 4938c2ecf20Sopenharmony_ci temp = agp_find_client_in_controller(agp_fe.current_controller, id); 4948c2ecf20Sopenharmony_ci return temp; 4958c2ecf20Sopenharmony_ci} 4968c2ecf20Sopenharmony_ci 4978c2ecf20Sopenharmony_cistatic void agp_insert_client(struct agp_client *client) 4988c2ecf20Sopenharmony_ci{ 4998c2ecf20Sopenharmony_ci struct agp_client *prev_client; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci prev_client = agp_fe.current_controller->clients; 5028c2ecf20Sopenharmony_ci client->next = prev_client; 5038c2ecf20Sopenharmony_ci 5048c2ecf20Sopenharmony_ci if (prev_client != NULL) 5058c2ecf20Sopenharmony_ci prev_client->prev = client; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci agp_fe.current_controller->clients = client; 5088c2ecf20Sopenharmony_ci agp_fe.current_controller->num_clients++; 5098c2ecf20Sopenharmony_ci} 5108c2ecf20Sopenharmony_ci 5118c2ecf20Sopenharmony_cistruct agp_client *agp_create_client(pid_t id) 5128c2ecf20Sopenharmony_ci{ 5138c2ecf20Sopenharmony_ci struct agp_client *new_client; 5148c2ecf20Sopenharmony_ci 5158c2ecf20Sopenharmony_ci new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL); 5168c2ecf20Sopenharmony_ci if (new_client == NULL) 5178c2ecf20Sopenharmony_ci return NULL; 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci new_client->pid = id; 5208c2ecf20Sopenharmony_ci agp_insert_client(new_client); 5218c2ecf20Sopenharmony_ci return new_client; 5228c2ecf20Sopenharmony_ci} 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ciint agp_remove_client(pid_t id) 5258c2ecf20Sopenharmony_ci{ 5268c2ecf20Sopenharmony_ci struct agp_client *client; 5278c2ecf20Sopenharmony_ci struct agp_client *prev_client; 5288c2ecf20Sopenharmony_ci struct agp_client *next_client; 5298c2ecf20Sopenharmony_ci struct agp_controller *controller; 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci controller = agp_find_controller_for_client(id); 5328c2ecf20Sopenharmony_ci if (controller == NULL) 5338c2ecf20Sopenharmony_ci return -EINVAL; 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci client = agp_find_client_in_controller(controller, id); 5368c2ecf20Sopenharmony_ci if (client == NULL) 5378c2ecf20Sopenharmony_ci return -EINVAL; 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ci prev_client = client->prev; 5408c2ecf20Sopenharmony_ci next_client = client->next; 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci if (prev_client != NULL) { 5438c2ecf20Sopenharmony_ci prev_client->next = next_client; 5448c2ecf20Sopenharmony_ci if (next_client != NULL) 5458c2ecf20Sopenharmony_ci next_client->prev = prev_client; 5468c2ecf20Sopenharmony_ci 5478c2ecf20Sopenharmony_ci } else { 5488c2ecf20Sopenharmony_ci if (next_client != NULL) 5498c2ecf20Sopenharmony_ci next_client->prev = NULL; 5508c2ecf20Sopenharmony_ci controller->clients = next_client; 5518c2ecf20Sopenharmony_ci } 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci controller->num_clients--; 5548c2ecf20Sopenharmony_ci agp_remove_seg_from_client(client); 5558c2ecf20Sopenharmony_ci kfree(client); 5568c2ecf20Sopenharmony_ci return 0; 5578c2ecf20Sopenharmony_ci} 5588c2ecf20Sopenharmony_ci 5598c2ecf20Sopenharmony_ci/* End - Routines for managing client lists */ 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci/* File Operations */ 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_cistatic int agp_mmap(struct file *file, struct vm_area_struct *vma) 5648c2ecf20Sopenharmony_ci{ 5658c2ecf20Sopenharmony_ci unsigned int size, current_size; 5668c2ecf20Sopenharmony_ci unsigned long offset; 5678c2ecf20Sopenharmony_ci struct agp_client *client; 5688c2ecf20Sopenharmony_ci struct agp_file_private *priv = file->private_data; 5698c2ecf20Sopenharmony_ci struct agp_kern_info kerninfo; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci mutex_lock(&(agp_fe.agp_mutex)); 5728c2ecf20Sopenharmony_ci 5738c2ecf20Sopenharmony_ci if (agp_fe.backend_acquired != true) 5748c2ecf20Sopenharmony_ci goto out_eperm; 5758c2ecf20Sopenharmony_ci 5768c2ecf20Sopenharmony_ci if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags))) 5778c2ecf20Sopenharmony_ci goto out_eperm; 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci agp_copy_info(agp_bridge, &kerninfo); 5808c2ecf20Sopenharmony_ci size = vma->vm_end - vma->vm_start; 5818c2ecf20Sopenharmony_ci current_size = kerninfo.aper_size; 5828c2ecf20Sopenharmony_ci current_size = current_size * 0x100000; 5838c2ecf20Sopenharmony_ci offset = vma->vm_pgoff << PAGE_SHIFT; 5848c2ecf20Sopenharmony_ci DBG("%lx:%lx", offset, offset+size); 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) { 5878c2ecf20Sopenharmony_ci if ((size + offset) > current_size) 5888c2ecf20Sopenharmony_ci goto out_inval; 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci client = agp_find_client_by_pid(current->pid); 5918c2ecf20Sopenharmony_ci 5928c2ecf20Sopenharmony_ci if (client == NULL) 5938c2ecf20Sopenharmony_ci goto out_eperm; 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot)) 5968c2ecf20Sopenharmony_ci goto out_inval; 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_ci DBG("client vm_ops=%p", kerninfo.vm_ops); 5998c2ecf20Sopenharmony_ci if (kerninfo.vm_ops) { 6008c2ecf20Sopenharmony_ci vma->vm_ops = kerninfo.vm_ops; 6018c2ecf20Sopenharmony_ci } else if (io_remap_pfn_range(vma, vma->vm_start, 6028c2ecf20Sopenharmony_ci (kerninfo.aper_base + offset) >> PAGE_SHIFT, 6038c2ecf20Sopenharmony_ci size, 6048c2ecf20Sopenharmony_ci pgprot_writecombine(vma->vm_page_prot))) { 6058c2ecf20Sopenharmony_ci goto out_again; 6068c2ecf20Sopenharmony_ci } 6078c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 6088c2ecf20Sopenharmony_ci return 0; 6098c2ecf20Sopenharmony_ci } 6108c2ecf20Sopenharmony_ci 6118c2ecf20Sopenharmony_ci if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) { 6128c2ecf20Sopenharmony_ci if (size != current_size) 6138c2ecf20Sopenharmony_ci goto out_inval; 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci DBG("controller vm_ops=%p", kerninfo.vm_ops); 6168c2ecf20Sopenharmony_ci if (kerninfo.vm_ops) { 6178c2ecf20Sopenharmony_ci vma->vm_ops = kerninfo.vm_ops; 6188c2ecf20Sopenharmony_ci } else if (io_remap_pfn_range(vma, vma->vm_start, 6198c2ecf20Sopenharmony_ci kerninfo.aper_base >> PAGE_SHIFT, 6208c2ecf20Sopenharmony_ci size, 6218c2ecf20Sopenharmony_ci pgprot_writecombine(vma->vm_page_prot))) { 6228c2ecf20Sopenharmony_ci goto out_again; 6238c2ecf20Sopenharmony_ci } 6248c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 6258c2ecf20Sopenharmony_ci return 0; 6268c2ecf20Sopenharmony_ci } 6278c2ecf20Sopenharmony_ci 6288c2ecf20Sopenharmony_ciout_eperm: 6298c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 6308c2ecf20Sopenharmony_ci return -EPERM; 6318c2ecf20Sopenharmony_ci 6328c2ecf20Sopenharmony_ciout_inval: 6338c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 6348c2ecf20Sopenharmony_ci return -EINVAL; 6358c2ecf20Sopenharmony_ci 6368c2ecf20Sopenharmony_ciout_again: 6378c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 6388c2ecf20Sopenharmony_ci return -EAGAIN; 6398c2ecf20Sopenharmony_ci} 6408c2ecf20Sopenharmony_ci 6418c2ecf20Sopenharmony_cistatic int agp_release(struct inode *inode, struct file *file) 6428c2ecf20Sopenharmony_ci{ 6438c2ecf20Sopenharmony_ci struct agp_file_private *priv = file->private_data; 6448c2ecf20Sopenharmony_ci 6458c2ecf20Sopenharmony_ci mutex_lock(&(agp_fe.agp_mutex)); 6468c2ecf20Sopenharmony_ci 6478c2ecf20Sopenharmony_ci DBG("priv=%p", priv); 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) { 6508c2ecf20Sopenharmony_ci struct agp_controller *controller; 6518c2ecf20Sopenharmony_ci 6528c2ecf20Sopenharmony_ci controller = agp_find_controller_by_pid(priv->my_pid); 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci if (controller != NULL) { 6558c2ecf20Sopenharmony_ci if (controller == agp_fe.current_controller) 6568c2ecf20Sopenharmony_ci agp_controller_release_current(controller, priv); 6578c2ecf20Sopenharmony_ci agp_remove_controller(controller); 6588c2ecf20Sopenharmony_ci controller = NULL; 6598c2ecf20Sopenharmony_ci } 6608c2ecf20Sopenharmony_ci } 6618c2ecf20Sopenharmony_ci 6628c2ecf20Sopenharmony_ci if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) 6638c2ecf20Sopenharmony_ci agp_remove_client(priv->my_pid); 6648c2ecf20Sopenharmony_ci 6658c2ecf20Sopenharmony_ci agp_remove_file_private(priv); 6668c2ecf20Sopenharmony_ci kfree(priv); 6678c2ecf20Sopenharmony_ci file->private_data = NULL; 6688c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 6698c2ecf20Sopenharmony_ci return 0; 6708c2ecf20Sopenharmony_ci} 6718c2ecf20Sopenharmony_ci 6728c2ecf20Sopenharmony_cistatic int agp_open(struct inode *inode, struct file *file) 6738c2ecf20Sopenharmony_ci{ 6748c2ecf20Sopenharmony_ci int minor = iminor(inode); 6758c2ecf20Sopenharmony_ci struct agp_file_private *priv; 6768c2ecf20Sopenharmony_ci struct agp_client *client; 6778c2ecf20Sopenharmony_ci 6788c2ecf20Sopenharmony_ci if (minor != AGPGART_MINOR) 6798c2ecf20Sopenharmony_ci return -ENXIO; 6808c2ecf20Sopenharmony_ci 6818c2ecf20Sopenharmony_ci mutex_lock(&(agp_fe.agp_mutex)); 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL); 6848c2ecf20Sopenharmony_ci if (priv == NULL) { 6858c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 6868c2ecf20Sopenharmony_ci return -ENOMEM; 6878c2ecf20Sopenharmony_ci } 6888c2ecf20Sopenharmony_ci 6898c2ecf20Sopenharmony_ci set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags); 6908c2ecf20Sopenharmony_ci priv->my_pid = current->pid; 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_ci if (capable(CAP_SYS_RAWIO)) 6938c2ecf20Sopenharmony_ci /* Root priv, can be controller */ 6948c2ecf20Sopenharmony_ci set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags); 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_ci client = agp_find_client_by_pid(current->pid); 6978c2ecf20Sopenharmony_ci 6988c2ecf20Sopenharmony_ci if (client != NULL) { 6998c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_CLIENT, &priv->access_flags); 7008c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_VALID, &priv->access_flags); 7018c2ecf20Sopenharmony_ci } 7028c2ecf20Sopenharmony_ci file->private_data = (void *) priv; 7038c2ecf20Sopenharmony_ci agp_insert_file_private(priv); 7048c2ecf20Sopenharmony_ci DBG("private=%p, client=%p", priv, client); 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 7078c2ecf20Sopenharmony_ci 7088c2ecf20Sopenharmony_ci return 0; 7098c2ecf20Sopenharmony_ci} 7108c2ecf20Sopenharmony_ci 7118c2ecf20Sopenharmony_cistatic int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg) 7128c2ecf20Sopenharmony_ci{ 7138c2ecf20Sopenharmony_ci struct agp_info userinfo; 7148c2ecf20Sopenharmony_ci struct agp_kern_info kerninfo; 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci agp_copy_info(agp_bridge, &kerninfo); 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci memset(&userinfo, 0, sizeof(userinfo)); 7198c2ecf20Sopenharmony_ci userinfo.version.major = kerninfo.version.major; 7208c2ecf20Sopenharmony_ci userinfo.version.minor = kerninfo.version.minor; 7218c2ecf20Sopenharmony_ci userinfo.bridge_id = kerninfo.device->vendor | 7228c2ecf20Sopenharmony_ci (kerninfo.device->device << 16); 7238c2ecf20Sopenharmony_ci userinfo.agp_mode = kerninfo.mode; 7248c2ecf20Sopenharmony_ci userinfo.aper_base = kerninfo.aper_base; 7258c2ecf20Sopenharmony_ci userinfo.aper_size = kerninfo.aper_size; 7268c2ecf20Sopenharmony_ci userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory; 7278c2ecf20Sopenharmony_ci userinfo.pg_used = kerninfo.current_memory; 7288c2ecf20Sopenharmony_ci 7298c2ecf20Sopenharmony_ci if (copy_to_user(arg, &userinfo, sizeof(struct agp_info))) 7308c2ecf20Sopenharmony_ci return -EFAULT; 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci return 0; 7338c2ecf20Sopenharmony_ci} 7348c2ecf20Sopenharmony_ci 7358c2ecf20Sopenharmony_ciint agpioc_acquire_wrap(struct agp_file_private *priv) 7368c2ecf20Sopenharmony_ci{ 7378c2ecf20Sopenharmony_ci struct agp_controller *controller; 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci DBG(""); 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags))) 7428c2ecf20Sopenharmony_ci return -EPERM; 7438c2ecf20Sopenharmony_ci 7448c2ecf20Sopenharmony_ci if (agp_fe.current_controller != NULL) 7458c2ecf20Sopenharmony_ci return -EBUSY; 7468c2ecf20Sopenharmony_ci 7478c2ecf20Sopenharmony_ci if (!agp_bridge) 7488c2ecf20Sopenharmony_ci return -ENODEV; 7498c2ecf20Sopenharmony_ci 7508c2ecf20Sopenharmony_ci if (atomic_read(&agp_bridge->agp_in_use)) 7518c2ecf20Sopenharmony_ci return -EBUSY; 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_ci atomic_inc(&agp_bridge->agp_in_use); 7548c2ecf20Sopenharmony_ci 7558c2ecf20Sopenharmony_ci agp_fe.backend_acquired = true; 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci controller = agp_find_controller_by_pid(priv->my_pid); 7588c2ecf20Sopenharmony_ci 7598c2ecf20Sopenharmony_ci if (controller != NULL) { 7608c2ecf20Sopenharmony_ci agp_controller_make_current(controller); 7618c2ecf20Sopenharmony_ci } else { 7628c2ecf20Sopenharmony_ci controller = agp_create_controller(priv->my_pid); 7638c2ecf20Sopenharmony_ci 7648c2ecf20Sopenharmony_ci if (controller == NULL) { 7658c2ecf20Sopenharmony_ci agp_fe.backend_acquired = false; 7668c2ecf20Sopenharmony_ci agp_backend_release(agp_bridge); 7678c2ecf20Sopenharmony_ci return -ENOMEM; 7688c2ecf20Sopenharmony_ci } 7698c2ecf20Sopenharmony_ci agp_insert_controller(controller); 7708c2ecf20Sopenharmony_ci agp_controller_make_current(controller); 7718c2ecf20Sopenharmony_ci } 7728c2ecf20Sopenharmony_ci 7738c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags); 7748c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_VALID, &priv->access_flags); 7758c2ecf20Sopenharmony_ci return 0; 7768c2ecf20Sopenharmony_ci} 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ciint agpioc_release_wrap(struct agp_file_private *priv) 7798c2ecf20Sopenharmony_ci{ 7808c2ecf20Sopenharmony_ci DBG(""); 7818c2ecf20Sopenharmony_ci agp_controller_release_current(agp_fe.current_controller, priv); 7828c2ecf20Sopenharmony_ci return 0; 7838c2ecf20Sopenharmony_ci} 7848c2ecf20Sopenharmony_ci 7858c2ecf20Sopenharmony_ciint agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg) 7868c2ecf20Sopenharmony_ci{ 7878c2ecf20Sopenharmony_ci struct agp_setup mode; 7888c2ecf20Sopenharmony_ci 7898c2ecf20Sopenharmony_ci DBG(""); 7908c2ecf20Sopenharmony_ci if (copy_from_user(&mode, arg, sizeof(struct agp_setup))) 7918c2ecf20Sopenharmony_ci return -EFAULT; 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_ci agp_enable(agp_bridge, mode.agp_mode); 7948c2ecf20Sopenharmony_ci return 0; 7958c2ecf20Sopenharmony_ci} 7968c2ecf20Sopenharmony_ci 7978c2ecf20Sopenharmony_cistatic int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg) 7988c2ecf20Sopenharmony_ci{ 7998c2ecf20Sopenharmony_ci struct agp_region reserve; 8008c2ecf20Sopenharmony_ci struct agp_client *client; 8018c2ecf20Sopenharmony_ci struct agp_file_private *client_priv; 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci DBG(""); 8048c2ecf20Sopenharmony_ci if (copy_from_user(&reserve, arg, sizeof(struct agp_region))) 8058c2ecf20Sopenharmony_ci return -EFAULT; 8068c2ecf20Sopenharmony_ci 8078c2ecf20Sopenharmony_ci if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment)) 8088c2ecf20Sopenharmony_ci return -EFAULT; 8098c2ecf20Sopenharmony_ci 8108c2ecf20Sopenharmony_ci client = agp_find_client_by_pid(reserve.pid); 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci if (reserve.seg_count == 0) { 8138c2ecf20Sopenharmony_ci /* remove a client */ 8148c2ecf20Sopenharmony_ci client_priv = agp_find_private(reserve.pid); 8158c2ecf20Sopenharmony_ci 8168c2ecf20Sopenharmony_ci if (client_priv != NULL) { 8178c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags); 8188c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_VALID, &client_priv->access_flags); 8198c2ecf20Sopenharmony_ci } 8208c2ecf20Sopenharmony_ci if (client == NULL) { 8218c2ecf20Sopenharmony_ci /* client is already removed */ 8228c2ecf20Sopenharmony_ci return 0; 8238c2ecf20Sopenharmony_ci } 8248c2ecf20Sopenharmony_ci return agp_remove_client(reserve.pid); 8258c2ecf20Sopenharmony_ci } else { 8268c2ecf20Sopenharmony_ci struct agp_segment *segment; 8278c2ecf20Sopenharmony_ci 8288c2ecf20Sopenharmony_ci if (reserve.seg_count >= 16384) 8298c2ecf20Sopenharmony_ci return -EINVAL; 8308c2ecf20Sopenharmony_ci 8318c2ecf20Sopenharmony_ci segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count), 8328c2ecf20Sopenharmony_ci GFP_KERNEL); 8338c2ecf20Sopenharmony_ci 8348c2ecf20Sopenharmony_ci if (segment == NULL) 8358c2ecf20Sopenharmony_ci return -ENOMEM; 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci if (copy_from_user(segment, (void __user *) reserve.seg_list, 8388c2ecf20Sopenharmony_ci sizeof(struct agp_segment) * reserve.seg_count)) { 8398c2ecf20Sopenharmony_ci kfree(segment); 8408c2ecf20Sopenharmony_ci return -EFAULT; 8418c2ecf20Sopenharmony_ci } 8428c2ecf20Sopenharmony_ci reserve.seg_list = segment; 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_ci if (client == NULL) { 8458c2ecf20Sopenharmony_ci /* Create the client and add the segment */ 8468c2ecf20Sopenharmony_ci client = agp_create_client(reserve.pid); 8478c2ecf20Sopenharmony_ci 8488c2ecf20Sopenharmony_ci if (client == NULL) { 8498c2ecf20Sopenharmony_ci kfree(segment); 8508c2ecf20Sopenharmony_ci return -ENOMEM; 8518c2ecf20Sopenharmony_ci } 8528c2ecf20Sopenharmony_ci client_priv = agp_find_private(reserve.pid); 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci if (client_priv != NULL) { 8558c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags); 8568c2ecf20Sopenharmony_ci set_bit(AGP_FF_IS_VALID, &client_priv->access_flags); 8578c2ecf20Sopenharmony_ci } 8588c2ecf20Sopenharmony_ci } 8598c2ecf20Sopenharmony_ci return agp_create_segment(client, &reserve); 8608c2ecf20Sopenharmony_ci } 8618c2ecf20Sopenharmony_ci /* Will never really happen */ 8628c2ecf20Sopenharmony_ci return -EINVAL; 8638c2ecf20Sopenharmony_ci} 8648c2ecf20Sopenharmony_ci 8658c2ecf20Sopenharmony_ciint agpioc_protect_wrap(struct agp_file_private *priv) 8668c2ecf20Sopenharmony_ci{ 8678c2ecf20Sopenharmony_ci DBG(""); 8688c2ecf20Sopenharmony_ci /* This function is not currently implemented */ 8698c2ecf20Sopenharmony_ci return -EINVAL; 8708c2ecf20Sopenharmony_ci} 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_cistatic int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg) 8738c2ecf20Sopenharmony_ci{ 8748c2ecf20Sopenharmony_ci struct agp_memory *memory; 8758c2ecf20Sopenharmony_ci struct agp_allocate alloc; 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci DBG(""); 8788c2ecf20Sopenharmony_ci if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate))) 8798c2ecf20Sopenharmony_ci return -EFAULT; 8808c2ecf20Sopenharmony_ci 8818c2ecf20Sopenharmony_ci if (alloc.type >= AGP_USER_TYPES) 8828c2ecf20Sopenharmony_ci return -EINVAL; 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ci memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type); 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci if (memory == NULL) 8878c2ecf20Sopenharmony_ci return -ENOMEM; 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci alloc.key = memory->key; 8908c2ecf20Sopenharmony_ci alloc.physical = memory->physical; 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) { 8938c2ecf20Sopenharmony_ci agp_free_memory_wrap(memory); 8948c2ecf20Sopenharmony_ci return -EFAULT; 8958c2ecf20Sopenharmony_ci } 8968c2ecf20Sopenharmony_ci return 0; 8978c2ecf20Sopenharmony_ci} 8988c2ecf20Sopenharmony_ci 8998c2ecf20Sopenharmony_ciint agpioc_deallocate_wrap(struct agp_file_private *priv, int arg) 9008c2ecf20Sopenharmony_ci{ 9018c2ecf20Sopenharmony_ci struct agp_memory *memory; 9028c2ecf20Sopenharmony_ci 9038c2ecf20Sopenharmony_ci DBG(""); 9048c2ecf20Sopenharmony_ci memory = agp_find_mem_by_key(arg); 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci if (memory == NULL) 9078c2ecf20Sopenharmony_ci return -EINVAL; 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_ci agp_free_memory_wrap(memory); 9108c2ecf20Sopenharmony_ci return 0; 9118c2ecf20Sopenharmony_ci} 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_cistatic int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg) 9148c2ecf20Sopenharmony_ci{ 9158c2ecf20Sopenharmony_ci struct agp_bind bind_info; 9168c2ecf20Sopenharmony_ci struct agp_memory *memory; 9178c2ecf20Sopenharmony_ci 9188c2ecf20Sopenharmony_ci DBG(""); 9198c2ecf20Sopenharmony_ci if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind))) 9208c2ecf20Sopenharmony_ci return -EFAULT; 9218c2ecf20Sopenharmony_ci 9228c2ecf20Sopenharmony_ci memory = agp_find_mem_by_key(bind_info.key); 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_ci if (memory == NULL) 9258c2ecf20Sopenharmony_ci return -EINVAL; 9268c2ecf20Sopenharmony_ci 9278c2ecf20Sopenharmony_ci return agp_bind_memory(memory, bind_info.pg_start); 9288c2ecf20Sopenharmony_ci} 9298c2ecf20Sopenharmony_ci 9308c2ecf20Sopenharmony_cistatic int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg) 9318c2ecf20Sopenharmony_ci{ 9328c2ecf20Sopenharmony_ci struct agp_memory *memory; 9338c2ecf20Sopenharmony_ci struct agp_unbind unbind; 9348c2ecf20Sopenharmony_ci 9358c2ecf20Sopenharmony_ci DBG(""); 9368c2ecf20Sopenharmony_ci if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind))) 9378c2ecf20Sopenharmony_ci return -EFAULT; 9388c2ecf20Sopenharmony_ci 9398c2ecf20Sopenharmony_ci memory = agp_find_mem_by_key(unbind.key); 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci if (memory == NULL) 9428c2ecf20Sopenharmony_ci return -EINVAL; 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci return agp_unbind_memory(memory); 9458c2ecf20Sopenharmony_ci} 9468c2ecf20Sopenharmony_ci 9478c2ecf20Sopenharmony_cistatic long agp_ioctl(struct file *file, 9488c2ecf20Sopenharmony_ci unsigned int cmd, unsigned long arg) 9498c2ecf20Sopenharmony_ci{ 9508c2ecf20Sopenharmony_ci struct agp_file_private *curr_priv = file->private_data; 9518c2ecf20Sopenharmony_ci int ret_val = -ENOTTY; 9528c2ecf20Sopenharmony_ci 9538c2ecf20Sopenharmony_ci DBG("priv=%p, cmd=%x", curr_priv, cmd); 9548c2ecf20Sopenharmony_ci mutex_lock(&(agp_fe.agp_mutex)); 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_ci if ((agp_fe.current_controller == NULL) && 9578c2ecf20Sopenharmony_ci (cmd != AGPIOC_ACQUIRE)) { 9588c2ecf20Sopenharmony_ci ret_val = -EINVAL; 9598c2ecf20Sopenharmony_ci goto ioctl_out; 9608c2ecf20Sopenharmony_ci } 9618c2ecf20Sopenharmony_ci if ((agp_fe.backend_acquired != true) && 9628c2ecf20Sopenharmony_ci (cmd != AGPIOC_ACQUIRE)) { 9638c2ecf20Sopenharmony_ci ret_val = -EBUSY; 9648c2ecf20Sopenharmony_ci goto ioctl_out; 9658c2ecf20Sopenharmony_ci } 9668c2ecf20Sopenharmony_ci if (cmd != AGPIOC_ACQUIRE) { 9678c2ecf20Sopenharmony_ci if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) { 9688c2ecf20Sopenharmony_ci ret_val = -EPERM; 9698c2ecf20Sopenharmony_ci goto ioctl_out; 9708c2ecf20Sopenharmony_ci } 9718c2ecf20Sopenharmony_ci /* Use the original pid of the controller, 9728c2ecf20Sopenharmony_ci * in case it's threaded */ 9738c2ecf20Sopenharmony_ci 9748c2ecf20Sopenharmony_ci if (agp_fe.current_controller->pid != curr_priv->my_pid) { 9758c2ecf20Sopenharmony_ci ret_val = -EBUSY; 9768c2ecf20Sopenharmony_ci goto ioctl_out; 9778c2ecf20Sopenharmony_ci } 9788c2ecf20Sopenharmony_ci } 9798c2ecf20Sopenharmony_ci 9808c2ecf20Sopenharmony_ci switch (cmd) { 9818c2ecf20Sopenharmony_ci case AGPIOC_INFO: 9828c2ecf20Sopenharmony_ci ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg); 9838c2ecf20Sopenharmony_ci break; 9848c2ecf20Sopenharmony_ci 9858c2ecf20Sopenharmony_ci case AGPIOC_ACQUIRE: 9868c2ecf20Sopenharmony_ci ret_val = agpioc_acquire_wrap(curr_priv); 9878c2ecf20Sopenharmony_ci break; 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ci case AGPIOC_RELEASE: 9908c2ecf20Sopenharmony_ci ret_val = agpioc_release_wrap(curr_priv); 9918c2ecf20Sopenharmony_ci break; 9928c2ecf20Sopenharmony_ci 9938c2ecf20Sopenharmony_ci case AGPIOC_SETUP: 9948c2ecf20Sopenharmony_ci ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg); 9958c2ecf20Sopenharmony_ci break; 9968c2ecf20Sopenharmony_ci 9978c2ecf20Sopenharmony_ci case AGPIOC_RESERVE: 9988c2ecf20Sopenharmony_ci ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg); 9998c2ecf20Sopenharmony_ci break; 10008c2ecf20Sopenharmony_ci 10018c2ecf20Sopenharmony_ci case AGPIOC_PROTECT: 10028c2ecf20Sopenharmony_ci ret_val = agpioc_protect_wrap(curr_priv); 10038c2ecf20Sopenharmony_ci break; 10048c2ecf20Sopenharmony_ci 10058c2ecf20Sopenharmony_ci case AGPIOC_ALLOCATE: 10068c2ecf20Sopenharmony_ci ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg); 10078c2ecf20Sopenharmony_ci break; 10088c2ecf20Sopenharmony_ci 10098c2ecf20Sopenharmony_ci case AGPIOC_DEALLOCATE: 10108c2ecf20Sopenharmony_ci ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg); 10118c2ecf20Sopenharmony_ci break; 10128c2ecf20Sopenharmony_ci 10138c2ecf20Sopenharmony_ci case AGPIOC_BIND: 10148c2ecf20Sopenharmony_ci ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg); 10158c2ecf20Sopenharmony_ci break; 10168c2ecf20Sopenharmony_ci 10178c2ecf20Sopenharmony_ci case AGPIOC_UNBIND: 10188c2ecf20Sopenharmony_ci ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg); 10198c2ecf20Sopenharmony_ci break; 10208c2ecf20Sopenharmony_ci 10218c2ecf20Sopenharmony_ci case AGPIOC_CHIPSET_FLUSH: 10228c2ecf20Sopenharmony_ci break; 10238c2ecf20Sopenharmony_ci } 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ciioctl_out: 10268c2ecf20Sopenharmony_ci DBG("ioctl returns %d\n", ret_val); 10278c2ecf20Sopenharmony_ci mutex_unlock(&(agp_fe.agp_mutex)); 10288c2ecf20Sopenharmony_ci return ret_val; 10298c2ecf20Sopenharmony_ci} 10308c2ecf20Sopenharmony_ci 10318c2ecf20Sopenharmony_cistatic const struct file_operations agp_fops = 10328c2ecf20Sopenharmony_ci{ 10338c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 10348c2ecf20Sopenharmony_ci .llseek = no_llseek, 10358c2ecf20Sopenharmony_ci .unlocked_ioctl = agp_ioctl, 10368c2ecf20Sopenharmony_ci#ifdef CONFIG_COMPAT 10378c2ecf20Sopenharmony_ci .compat_ioctl = compat_agp_ioctl, 10388c2ecf20Sopenharmony_ci#endif 10398c2ecf20Sopenharmony_ci .mmap = agp_mmap, 10408c2ecf20Sopenharmony_ci .open = agp_open, 10418c2ecf20Sopenharmony_ci .release = agp_release, 10428c2ecf20Sopenharmony_ci}; 10438c2ecf20Sopenharmony_ci 10448c2ecf20Sopenharmony_cistatic struct miscdevice agp_miscdev = 10458c2ecf20Sopenharmony_ci{ 10468c2ecf20Sopenharmony_ci .minor = AGPGART_MINOR, 10478c2ecf20Sopenharmony_ci .name = "agpgart", 10488c2ecf20Sopenharmony_ci .fops = &agp_fops 10498c2ecf20Sopenharmony_ci}; 10508c2ecf20Sopenharmony_ci 10518c2ecf20Sopenharmony_ciint agp_frontend_initialize(void) 10528c2ecf20Sopenharmony_ci{ 10538c2ecf20Sopenharmony_ci memset(&agp_fe, 0, sizeof(struct agp_front_data)); 10548c2ecf20Sopenharmony_ci mutex_init(&(agp_fe.agp_mutex)); 10558c2ecf20Sopenharmony_ci 10568c2ecf20Sopenharmony_ci if (misc_register(&agp_miscdev)) { 10578c2ecf20Sopenharmony_ci printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR); 10588c2ecf20Sopenharmony_ci return -EIO; 10598c2ecf20Sopenharmony_ci } 10608c2ecf20Sopenharmony_ci return 0; 10618c2ecf20Sopenharmony_ci} 10628c2ecf20Sopenharmony_ci 10638c2ecf20Sopenharmony_civoid agp_frontend_cleanup(void) 10648c2ecf20Sopenharmony_ci{ 10658c2ecf20Sopenharmony_ci misc_deregister(&agp_miscdev); 10668c2ecf20Sopenharmony_ci} 1067