Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2)  * AGPGART driver frontend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Copyright (C) 2004 Silicon Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * Copyright (C) 2002-2003 Dave Jones
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  * Copyright (C) 1999 Jeff Hartmann
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright (C) 1999 Precision Insight, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  * Copyright (C) 1999 Xi Graphics, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  * Permission is hereby granted, free of charge, to any person obtaining a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  * copy of this software and associated documentation files (the "Software"),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  * to deal in the Software without restriction, including without limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  * and/or sell copies of the Software, and to permit persons to whom the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14)  * Software is furnished to do so, subject to the following conditions:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16)  * The above copyright notice and this permission notice shall be included
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17)  * in all copies or substantial portions of the Software.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19)  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20)  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21)  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22)  * JEFF HARTMANN, OR ANY OTHER CONTRIBUTORS BE LIABLE FOR ANY CLAIM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23)  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24)  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25)  * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <linux/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <linux/pci.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #include <linux/miscdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #include <linux/agp_backend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #include <linux/agpgart.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #include <linux/fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #include "agp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) struct agp_front_data agp_fe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) struct agp_memory *agp_find_mem_by_key(int key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	struct agp_memory *curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 	if (agp_fe.current_controller == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	curr = agp_fe.current_controller->pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	while (curr != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 		if (curr->key == key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 		curr = curr->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	DBG("key=%d -> mem=%p", key, curr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	return curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) static void agp_remove_from_pool(struct agp_memory *temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 	struct agp_memory *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct agp_memory *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	/* Check to see if this is even in the memory pool */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 	DBG("mem=%p", temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	if (agp_find_mem_by_key(temp->key) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 		next = temp->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 		prev = temp->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 		if (prev != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 			prev->next = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 			if (next != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 				next->prev = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) 			/* This is the first item on the list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 			if (next != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 				next->prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 			agp_fe.current_controller->pool = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * Routines for managing each client's segment list -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * These routines handle adding and removing segments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  * to each auth'ed client.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) static struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) agp_segment_priv *agp_find_seg_in_client(const struct agp_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 						unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 					    int size, pgprot_t page_prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	struct agp_segment_priv *seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 	off_t pg_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	size_t pg_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 	pg_start = offset / 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	pg_count = size / 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 	seg = *(client->segments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	for (i = 0; i < client->num_segments; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 		if ((seg[i].pg_start == pg_start) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 		    (seg[i].pg_count == pg_count) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 		    (pgprot_val(seg[i].prot) == pgprot_val(page_prot))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 			return seg + i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) static void agp_remove_seg_from_client(struct agp_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 	DBG("client=%p", client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	if (client->segments != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 		if (*(client->segments) != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 			DBG("Freeing %p from client %p", *(client->segments), client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 			kfree(*(client->segments));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 		DBG("Freeing %p from client %p", client->segments, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 		kfree(client->segments);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 		client->segments = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) static void agp_add_seg_to_client(struct agp_client *client,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 			       struct agp_segment_priv ** seg, int num_segments)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	struct agp_segment_priv **prev_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	prev_seg = client->segments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	if (prev_seg != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 		agp_remove_seg_from_client(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 	DBG("Adding seg %p (%d segments) to client %p", seg, num_segments, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	client->num_segments = num_segments;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	client->segments = seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) static pgprot_t agp_convert_mmap_flags(int prot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	unsigned long prot_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	prot_bits = calc_vm_prot_bits(prot, 0) | VM_SHARED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	return vm_get_page_prot(prot_bits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) int agp_create_segment(struct agp_client *client, struct agp_region *region)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	struct agp_segment_priv **ret_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	struct agp_segment_priv *seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	struct agp_segment *user_seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	seg = kzalloc((sizeof(struct agp_segment_priv) * region->seg_count), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	if (seg == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 		kfree(region->seg_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 		region->seg_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	user_seg = region->seg_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	for (i = 0; i < region->seg_count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 		seg[i].pg_start = user_seg[i].pg_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		seg[i].pg_count = user_seg[i].pg_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		seg[i].prot = agp_convert_mmap_flags(user_seg[i].prot);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	kfree(region->seg_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	region->seg_list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	ret_seg = kmalloc(sizeof(void *), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	if (ret_seg == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		kfree(seg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	*ret_seg = seg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	agp_add_seg_to_client(client, ret_seg, region->seg_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) /* End - Routines for managing each client's segment list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) /* This function must only be called when current_controller != NULL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) static void agp_insert_into_pool(struct agp_memory * temp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	struct agp_memory *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	prev = agp_fe.current_controller->pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	if (prev != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		prev->prev = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		temp->next = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	agp_fe.current_controller->pool = temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) /* File private list routines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) struct agp_file_private *agp_find_private(pid_t pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	struct agp_file_private *curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	curr = agp_fe.file_priv_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 	while (curr != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		if (curr->my_pid == pid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 			return curr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		curr = curr->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) static void agp_insert_file_private(struct agp_file_private * priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	struct agp_file_private *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	prev = agp_fe.file_priv_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	if (prev != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		prev->prev = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	priv->next = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	agp_fe.file_priv_list = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) static void agp_remove_file_private(struct agp_file_private * priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	struct agp_file_private *next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 	struct agp_file_private *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	next = priv->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	prev = priv->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	if (prev != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 		prev->next = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		if (next != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 			next->prev = prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 		if (next != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 			next->prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 		agp_fe.file_priv_list = next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) /* End - File flag list routines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265)  * Wrappers for agp_free_memory & agp_allocate_memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266)  * These make sure that internal lists are kept updated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) void agp_free_memory_wrap(struct agp_memory *memory)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 	agp_remove_from_pool(memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 	agp_free_memory(memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) struct agp_memory *agp_allocate_memory_wrap(size_t pg_count, u32 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 	struct agp_memory *memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	memory = agp_allocate_memory(agp_bridge, pg_count, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	if (memory == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	agp_insert_into_pool(memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	return memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) /* Routines for managing the list of controllers -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287)  * These routines manage the current controller, and the list of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288)  * controllers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) static struct agp_controller *agp_find_controller_by_pid(pid_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 	struct agp_controller *controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	controller = agp_fe.controllers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	while (controller != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		if (controller->pid == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 			return controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		controller = controller->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) static struct agp_controller *agp_create_controller(pid_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 	struct agp_controller *controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 	controller = kzalloc(sizeof(struct agp_controller), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	if (controller == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	controller->pid = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	return controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) static int agp_insert_controller(struct agp_controller *controller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	struct agp_controller *prev_controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	prev_controller = agp_fe.controllers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	controller->next = prev_controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	if (prev_controller != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 		prev_controller->prev = controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 	agp_fe.controllers = controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) static void agp_remove_all_clients(struct agp_controller *controller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	struct agp_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	struct agp_client *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	client = controller->clients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	while (client) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 		struct agp_file_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 		temp = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		agp_remove_seg_from_client(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 		priv = agp_find_private(temp->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		if (priv != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 			clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 			clear_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		client = client->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		kfree(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) static void agp_remove_all_memory(struct agp_controller *controller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	struct agp_memory *memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 	struct agp_memory *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	memory = controller->pool;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	while (memory) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		temp = memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		memory = memory->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		agp_free_memory_wrap(temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) static int agp_remove_controller(struct agp_controller *controller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	struct agp_controller *prev_controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	struct agp_controller *next_controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	prev_controller = controller->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	next_controller = controller->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 	if (prev_controller != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		prev_controller->next = next_controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 		if (next_controller != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 			next_controller->prev = prev_controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 		if (next_controller != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 			next_controller->prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		agp_fe.controllers = next_controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	agp_remove_all_memory(controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	agp_remove_all_clients(controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	if (agp_fe.current_controller == controller) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		agp_fe.current_controller = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 		agp_fe.backend_acquired = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		agp_backend_release(agp_bridge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	kfree(controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) static void agp_controller_make_current(struct agp_controller *controller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	struct agp_client *clients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	clients = controller->clients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	while (clients != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 		struct agp_file_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 		priv = agp_find_private(clients->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		if (priv != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 			set_bit(AGP_FF_IS_VALID, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 			set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 		clients = clients->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	agp_fe.current_controller = controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) static void agp_controller_release_current(struct agp_controller *controller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 				      struct agp_file_private *controller_priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	struct agp_client *clients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 	clear_bit(AGP_FF_IS_VALID, &controller_priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	clients = controller->clients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	while (clients != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		struct agp_file_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		priv = agp_find_private(clients->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		if (priv != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 			clear_bit(AGP_FF_IS_VALID, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		clients = clients->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 	agp_fe.current_controller = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	agp_fe.used_by_controller = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	agp_backend_release(agp_bridge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448)  * Routines for managing client lists -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449)  * These routines are for managing the list of auth'ed clients.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) static struct agp_client
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) *agp_find_client_in_controller(struct agp_controller *controller, pid_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 	struct agp_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	if (controller == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	client = controller->clients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	while (client != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		if (client->pid == id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 			return client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		client = client->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static struct agp_controller *agp_find_controller_for_client(pid_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	struct agp_controller *controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	controller = agp_fe.controllers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	while (controller != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		if ((agp_find_client_in_controller(controller, id)) != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 			return controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		controller = controller->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) struct agp_client *agp_find_client_by_pid(pid_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 	struct agp_client *temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	if (agp_fe.current_controller == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 	temp = agp_find_client_in_controller(agp_fe.current_controller, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	return temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) static void agp_insert_client(struct agp_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 	struct agp_client *prev_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 	prev_client = agp_fe.current_controller->clients;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	client->next = prev_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (prev_client != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		prev_client->prev = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	agp_fe.current_controller->clients = client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 	agp_fe.current_controller->num_clients++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) struct agp_client *agp_create_client(pid_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 	struct agp_client *new_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 	new_client = kzalloc(sizeof(struct agp_client), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	if (new_client == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 	new_client->pid = id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	agp_insert_client(new_client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 	return new_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) int agp_remove_client(pid_t id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	struct agp_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	struct agp_client *prev_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	struct agp_client *next_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	struct agp_controller *controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	controller = agp_find_controller_for_client(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 	if (controller == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	client = agp_find_client_in_controller(controller, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	if (client == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	prev_client = client->prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	next_client = client->next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	if (prev_client != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		prev_client->next = next_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		if (next_client != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 			next_client->prev = prev_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		if (next_client != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 			next_client->prev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 		controller->clients = next_client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	controller->num_clients--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 	agp_remove_seg_from_client(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	kfree(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) /* End - Routines for managing client lists */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) /* File Operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) static int agp_mmap(struct file *file, struct vm_area_struct *vma)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	unsigned int size, current_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	struct agp_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	struct agp_file_private *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	struct agp_kern_info kerninfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	mutex_lock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	if (agp_fe.backend_acquired != true)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 		goto out_eperm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	if (!(test_bit(AGP_FF_IS_VALID, &priv->access_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		goto out_eperm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	agp_copy_info(agp_bridge, &kerninfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	size = vma->vm_end - vma->vm_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	current_size = kerninfo.aper_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	current_size = current_size * 0x100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	offset = vma->vm_pgoff << PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	DBG("%lx:%lx", offset, offset+size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		if ((size + offset) > current_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 			goto out_inval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 		client = agp_find_client_by_pid(current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		if (client == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 			goto out_eperm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 		if (!agp_find_seg_in_client(client, offset, size, vma->vm_page_prot))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 			goto out_inval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 		DBG("client vm_ops=%p", kerninfo.vm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 		if (kerninfo.vm_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 			vma->vm_ops = kerninfo.vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 		} else if (io_remap_pfn_range(vma, vma->vm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 				(kerninfo.aper_base + offset) >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 				size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 				pgprot_writecombine(vma->vm_page_prot))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 			goto out_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		if (size != current_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 			goto out_inval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		DBG("controller vm_ops=%p", kerninfo.vm_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		if (kerninfo.vm_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			vma->vm_ops = kerninfo.vm_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		} else if (io_remap_pfn_range(vma, vma->vm_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 				kerninfo.aper_base >> PAGE_SHIFT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 				size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 				pgprot_writecombine(vma->vm_page_prot))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			goto out_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 		mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) out_eperm:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 	return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) out_inval:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) out_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) static int agp_release(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	struct agp_file_private *priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	mutex_lock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	DBG("priv=%p", priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	if (test_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		struct agp_controller *controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 		controller = agp_find_controller_by_pid(priv->my_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 		if (controller != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 			if (controller == agp_fe.current_controller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 				agp_controller_release_current(controller, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			agp_remove_controller(controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 			controller = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	if (test_bit(AGP_FF_IS_CLIENT, &priv->access_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 		agp_remove_client(priv->my_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 	agp_remove_file_private(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	kfree(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 	file->private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) static int agp_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	int minor = iminor(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	struct agp_file_private *priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 	struct agp_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	if (minor != AGPGART_MINOR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	mutex_lock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	priv = kzalloc(sizeof(struct agp_file_private), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	if (priv == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 		mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	set_bit(AGP_FF_ALLOW_CLIENT, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	priv->my_pid = current->pid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 	if (capable(CAP_SYS_RAWIO))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		/* Root priv, can be controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 		set_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	client = agp_find_client_by_pid(current->pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	if (client != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		set_bit(AGP_FF_IS_CLIENT, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 		set_bit(AGP_FF_IS_VALID, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	file->private_data = (void *) priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	agp_insert_file_private(priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	DBG("private=%p, client=%p", priv, client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) static int agpioc_info_wrap(struct agp_file_private *priv, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	struct agp_info userinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	struct agp_kern_info kerninfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	agp_copy_info(agp_bridge, &kerninfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	memset(&userinfo, 0, sizeof(userinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	userinfo.version.major = kerninfo.version.major;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	userinfo.version.minor = kerninfo.version.minor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	userinfo.bridge_id = kerninfo.device->vendor |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	    (kerninfo.device->device << 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	userinfo.agp_mode = kerninfo.mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	userinfo.aper_base = kerninfo.aper_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	userinfo.aper_size = kerninfo.aper_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	userinfo.pg_total = userinfo.pg_system = kerninfo.max_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	userinfo.pg_used = kerninfo.current_memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	if (copy_to_user(arg, &userinfo, sizeof(struct agp_info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) int agpioc_acquire_wrap(struct agp_file_private *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 	struct agp_controller *controller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (!(test_bit(AGP_FF_ALLOW_CONTROLLER, &priv->access_flags)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		return -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	if (agp_fe.current_controller != NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	if (!agp_bridge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750)         if (atomic_read(&agp_bridge->agp_in_use))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751)                 return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 	atomic_inc(&agp_bridge->agp_in_use);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	agp_fe.backend_acquired = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	controller = agp_find_controller_by_pid(priv->my_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	if (controller != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 		agp_controller_make_current(controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		controller = agp_create_controller(priv->my_pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		if (controller == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			agp_fe.backend_acquired = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 			agp_backend_release(agp_bridge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		agp_insert_controller(controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		agp_controller_make_current(controller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	set_bit(AGP_FF_IS_CONTROLLER, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	set_bit(AGP_FF_IS_VALID, &priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) int agpioc_release_wrap(struct agp_file_private *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	agp_controller_release_current(agp_fe.current_controller, priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) int agpioc_setup_wrap(struct agp_file_private *priv, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 	struct agp_setup mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 	if (copy_from_user(&mode, arg, sizeof(struct agp_setup)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	agp_enable(agp_bridge, mode.agp_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) static int agpioc_reserve_wrap(struct agp_file_private *priv, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	struct agp_region reserve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	struct agp_client *client;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	struct agp_file_private *client_priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	if (copy_from_user(&reserve, arg, sizeof(struct agp_region)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	if ((unsigned) reserve.seg_count >= ~0U/sizeof(struct agp_segment))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	client = agp_find_client_by_pid(reserve.pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	if (reserve.seg_count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		/* remove a client */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		client_priv = agp_find_private(reserve.pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		if (client_priv != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 		if (client == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 			/* client is already removed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 		return agp_remove_client(reserve.pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 		struct agp_segment *segment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		if (reserve.seg_count >= 16384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		segment = kmalloc((sizeof(struct agp_segment) * reserve.seg_count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 				  GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		if (segment == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		if (copy_from_user(segment, (void __user *) reserve.seg_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 				   sizeof(struct agp_segment) * reserve.seg_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			kfree(segment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 			return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		reserve.seg_list = segment;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		if (client == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			/* Create the client and add the segment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 			client = agp_create_client(reserve.pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 			if (client == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 				kfree(segment);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 				return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 			client_priv = agp_find_private(reserve.pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 			if (client_priv != NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 				set_bit(AGP_FF_IS_CLIENT, &client_priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 				set_bit(AGP_FF_IS_VALID, &client_priv->access_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 		return agp_create_segment(client, &reserve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	/* Will never really happen */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) int agpioc_protect_wrap(struct agp_file_private *priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 	/* This function is not currently implemented */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) static int agpioc_allocate_wrap(struct agp_file_private *priv, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	struct agp_memory *memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	struct agp_allocate alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 	if (copy_from_user(&alloc, arg, sizeof(struct agp_allocate)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 	if (alloc.type >= AGP_USER_TYPES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	memory = agp_allocate_memory_wrap(alloc.pg_count, alloc.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	if (memory == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	alloc.key = memory->key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	alloc.physical = memory->physical;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	if (copy_to_user(arg, &alloc, sizeof(struct agp_allocate))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		agp_free_memory_wrap(memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) int agpioc_deallocate_wrap(struct agp_file_private *priv, int arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	struct agp_memory *memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	memory = agp_find_mem_by_key(arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	if (memory == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	agp_free_memory_wrap(memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) static int agpioc_bind_wrap(struct agp_file_private *priv, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	struct agp_bind bind_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	struct agp_memory *memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	if (copy_from_user(&bind_info, arg, sizeof(struct agp_bind)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	memory = agp_find_mem_by_key(bind_info.key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	if (memory == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	return agp_bind_memory(memory, bind_info.pg_start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) static int agpioc_unbind_wrap(struct agp_file_private *priv, void __user *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	struct agp_memory *memory;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	struct agp_unbind unbind;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	DBG("");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (copy_from_user(&unbind, arg, sizeof(struct agp_unbind)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	memory = agp_find_mem_by_key(unbind.key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	if (memory == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	return agp_unbind_memory(memory);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) static long agp_ioctl(struct file *file,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		     unsigned int cmd, unsigned long arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	struct agp_file_private *curr_priv = file->private_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	int ret_val = -ENOTTY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	DBG("priv=%p, cmd=%x", curr_priv, cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	mutex_lock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	if ((agp_fe.current_controller == NULL) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	    (cmd != AGPIOC_ACQUIRE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		ret_val = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		goto ioctl_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	if ((agp_fe.backend_acquired != true) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	    (cmd != AGPIOC_ACQUIRE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		ret_val = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 		goto ioctl_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	if (cmd != AGPIOC_ACQUIRE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		if (!(test_bit(AGP_FF_IS_CONTROLLER, &curr_priv->access_flags))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			ret_val = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			goto ioctl_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		/* Use the original pid of the controller,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		 * in case it's threaded */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		if (agp_fe.current_controller->pid != curr_priv->my_pid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 			ret_val = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			goto ioctl_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	case AGPIOC_INFO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		ret_val = agpioc_info_wrap(curr_priv, (void __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	case AGPIOC_ACQUIRE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 		ret_val = agpioc_acquire_wrap(curr_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	case AGPIOC_RELEASE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		ret_val = agpioc_release_wrap(curr_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	case AGPIOC_SETUP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		ret_val = agpioc_setup_wrap(curr_priv, (void __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	case AGPIOC_RESERVE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		ret_val = agpioc_reserve_wrap(curr_priv, (void __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	case AGPIOC_PROTECT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 		ret_val = agpioc_protect_wrap(curr_priv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	case AGPIOC_ALLOCATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		ret_val = agpioc_allocate_wrap(curr_priv, (void __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	case AGPIOC_DEALLOCATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 		ret_val = agpioc_deallocate_wrap(curr_priv, (int) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	case AGPIOC_BIND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 		ret_val = agpioc_bind_wrap(curr_priv, (void __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	case AGPIOC_UNBIND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		ret_val = agpioc_unbind_wrap(curr_priv, (void __user *) arg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	       
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	case AGPIOC_CHIPSET_FLUSH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) ioctl_out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	DBG("ioctl returns %d\n", ret_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	mutex_unlock(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	return ret_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) static const struct file_operations agp_fops =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	.owner		= THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	.llseek		= no_llseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	.unlocked_ioctl	= agp_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	.compat_ioctl	= compat_agp_ioctl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	.mmap		= agp_mmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	.open		= agp_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	.release	= agp_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) static struct miscdevice agp_miscdev =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	.minor	= AGPGART_MINOR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	.name	= "agpgart",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	.fops	= &agp_fops
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) int agp_frontend_initialize(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	memset(&agp_fe, 0, sizeof(struct agp_front_data));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	mutex_init(&(agp_fe.agp_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	if (misc_register(&agp_miscdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		printk(KERN_ERR PFX "unable to get minor: %d\n", AGPGART_MINOR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) void agp_frontend_cleanup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	misc_deregister(&agp_miscdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) }