Linux Audio

Check our new training course

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