Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v2.6.20-rc3 514 lines 14 kB view raw
1/** 2 * \file drm_agpsupport.c 3 * DRM support for AGP/GART backend 4 * 5 * \author Rickard E. (Rik) Faith <faith@valinux.com> 6 * \author Gareth Hughes <gareth@valinux.com> 7 */ 8 9/* 10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 12 * All Rights Reserved. 13 * 14 * Permission is hereby granted, free of charge, to any person obtaining a 15 * copy of this software and associated documentation files (the "Software"), 16 * to deal in the Software without restriction, including without limitation 17 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 18 * and/or sell copies of the Software, and to permit persons to whom the 19 * Software is furnished to do so, subject to the following conditions: 20 * 21 * The above copyright notice and this permission notice (including the next 22 * paragraph) shall be included in all copies or substantial portions of the 23 * Software. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 28 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 31 * OTHER DEALINGS IN THE SOFTWARE. 32 */ 33 34#include "drmP.h" 35#include <linux/module.h> 36 37#if __OS_HAS_AGP 38 39/** 40 * Get AGP information. 41 * 42 * \param inode device inode. 43 * \param filp file pointer. 44 * \param cmd command. 45 * \param arg pointer to a (output) drm_agp_info structure. 46 * \return zero on success or a negative number on failure. 47 * 48 * Verifies the AGP device has been initialized and acquired and fills in the 49 * drm_agp_info structure with the information in drm_agp_head::agp_info. 50 */ 51int drm_agp_info(drm_device_t * dev, drm_agp_info_t * info) 52{ 53 DRM_AGP_KERN *kern; 54 55 if (!dev->agp || !dev->agp->acquired) 56 return -EINVAL; 57 58 kern = &dev->agp->agp_info; 59 info->agp_version_major = kern->version.major; 60 info->agp_version_minor = kern->version.minor; 61 info->mode = kern->mode; 62 info->aperture_base = kern->aper_base; 63 info->aperture_size = kern->aper_size * 1024 * 1024; 64 info->memory_allowed = kern->max_memory << PAGE_SHIFT; 65 info->memory_used = kern->current_memory << PAGE_SHIFT; 66 info->id_vendor = kern->device->vendor; 67 info->id_device = kern->device->device; 68 69 return 0; 70} 71 72EXPORT_SYMBOL(drm_agp_info); 73 74int drm_agp_info_ioctl(struct inode *inode, struct file *filp, 75 unsigned int cmd, unsigned long arg) 76{ 77 drm_file_t *priv = filp->private_data; 78 drm_device_t *dev = priv->head->dev; 79 drm_agp_info_t info; 80 int err; 81 82 err = drm_agp_info(dev, &info); 83 if (err) 84 return err; 85 86 if (copy_to_user((drm_agp_info_t __user *) arg, &info, sizeof(info))) 87 return -EFAULT; 88 return 0; 89} 90 91/** 92 * Acquire the AGP device. 93 * 94 * \param dev DRM device that is to acquire AGP. 95 * \return zero on success or a negative number on failure. 96 * 97 * Verifies the AGP device hasn't been acquired before and calls 98 * \c agp_backend_acquire. 99 */ 100int drm_agp_acquire(drm_device_t * dev) 101{ 102 if (!dev->agp) 103 return -ENODEV; 104 if (dev->agp->acquired) 105 return -EBUSY; 106 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev))) 107 return -ENODEV; 108 dev->agp->acquired = 1; 109 return 0; 110} 111 112EXPORT_SYMBOL(drm_agp_acquire); 113 114/** 115 * Acquire the AGP device (ioctl). 116 * 117 * \param inode device inode. 118 * \param filp file pointer. 119 * \param cmd command. 120 * \param arg user argument. 121 * \return zero on success or a negative number on failure. 122 * 123 * Verifies the AGP device hasn't been acquired before and calls 124 * \c agp_backend_acquire. 125 */ 126int drm_agp_acquire_ioctl(struct inode *inode, struct file *filp, 127 unsigned int cmd, unsigned long arg) 128{ 129 drm_file_t *priv = filp->private_data; 130 131 return drm_agp_acquire((drm_device_t *) priv->head->dev); 132} 133 134/** 135 * Release the AGP device. 136 * 137 * \param dev DRM device that is to release AGP. 138 * \return zero on success or a negative number on failure. 139 * 140 * Verifies the AGP device has been acquired and calls \c agp_backend_release. 141 */ 142int drm_agp_release(drm_device_t * dev) 143{ 144 if (!dev->agp || !dev->agp->acquired) 145 return -EINVAL; 146 agp_backend_release(dev->agp->bridge); 147 dev->agp->acquired = 0; 148 return 0; 149} 150EXPORT_SYMBOL(drm_agp_release); 151 152int drm_agp_release_ioctl(struct inode *inode, struct file *filp, 153 unsigned int cmd, unsigned long arg) 154{ 155 drm_file_t *priv = filp->private_data; 156 drm_device_t *dev = priv->head->dev; 157 158 return drm_agp_release(dev); 159} 160 161/** 162 * Enable the AGP bus. 163 * 164 * \param dev DRM device that has previously acquired AGP. 165 * \param mode Requested AGP mode. 166 * \return zero on success or a negative number on failure. 167 * 168 * Verifies the AGP device has been acquired but not enabled, and calls 169 * \c agp_enable. 170 */ 171int drm_agp_enable(drm_device_t * dev, drm_agp_mode_t mode) 172{ 173 if (!dev->agp || !dev->agp->acquired) 174 return -EINVAL; 175 176 dev->agp->mode = mode.mode; 177 agp_enable(dev->agp->bridge, mode.mode); 178 dev->agp->base = dev->agp->agp_info.aper_base; 179 dev->agp->enabled = 1; 180 return 0; 181} 182 183EXPORT_SYMBOL(drm_agp_enable); 184 185int drm_agp_enable_ioctl(struct inode *inode, struct file *filp, 186 unsigned int cmd, unsigned long arg) 187{ 188 drm_file_t *priv = filp->private_data; 189 drm_device_t *dev = priv->head->dev; 190 drm_agp_mode_t mode; 191 192 if (copy_from_user(&mode, (drm_agp_mode_t __user *) arg, sizeof(mode))) 193 return -EFAULT; 194 195 return drm_agp_enable(dev, mode); 196} 197 198/** 199 * Allocate AGP memory. 200 * 201 * \param inode device inode. 202 * \param filp file pointer. 203 * \param cmd command. 204 * \param arg pointer to a drm_agp_buffer structure. 205 * \return zero on success or a negative number on failure. 206 * 207 * Verifies the AGP device is present and has been acquired, allocates the 208 * memory via alloc_agp() and creates a drm_agp_mem entry for it. 209 */ 210int drm_agp_alloc(drm_device_t *dev, drm_agp_buffer_t *request) 211{ 212 drm_agp_mem_t *entry; 213 DRM_AGP_MEM *memory; 214 unsigned long pages; 215 u32 type; 216 217 if (!dev->agp || !dev->agp->acquired) 218 return -EINVAL; 219 if (!(entry = drm_alloc(sizeof(*entry), DRM_MEM_AGPLISTS))) 220 return -ENOMEM; 221 222 memset(entry, 0, sizeof(*entry)); 223 224 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE; 225 type = (u32) request->type; 226 if (!(memory = drm_alloc_agp(dev, pages, type))) { 227 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); 228 return -ENOMEM; 229 } 230 231 entry->handle = (unsigned long)memory->key + 1; 232 entry->memory = memory; 233 entry->bound = 0; 234 entry->pages = pages; 235 entry->prev = NULL; 236 entry->next = dev->agp->memory; 237 if (dev->agp->memory) 238 dev->agp->memory->prev = entry; 239 dev->agp->memory = entry; 240 241 request->handle = entry->handle; 242 request->physical = memory->physical; 243 244 return 0; 245} 246EXPORT_SYMBOL(drm_agp_alloc); 247 248int drm_agp_alloc_ioctl(struct inode *inode, struct file *filp, 249 unsigned int cmd, unsigned long arg) 250{ 251 drm_file_t *priv = filp->private_data; 252 drm_device_t *dev = priv->head->dev; 253 drm_agp_buffer_t request; 254 drm_agp_buffer_t __user *argp = (void __user *)arg; 255 int err; 256 257 if (copy_from_user(&request, argp, sizeof(request))) 258 return -EFAULT; 259 260 err = drm_agp_alloc(dev, &request); 261 if (err) 262 return err; 263 264 if (copy_to_user(argp, &request, sizeof(request))) { 265 drm_agp_mem_t *entry = dev->agp->memory; 266 267 dev->agp->memory = entry->next; 268 dev->agp->memory->prev = NULL; 269 drm_free_agp(entry->memory, entry->pages); 270 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); 271 return -EFAULT; 272 } 273 274 return 0; 275} 276 277/** 278 * Search for the AGP memory entry associated with a handle. 279 * 280 * \param dev DRM device structure. 281 * \param handle AGP memory handle. 282 * \return pointer to the drm_agp_mem structure associated with \p handle. 283 * 284 * Walks through drm_agp_head::memory until finding a matching handle. 285 */ 286static drm_agp_mem_t *drm_agp_lookup_entry(drm_device_t * dev, 287 unsigned long handle) 288{ 289 drm_agp_mem_t *entry; 290 291 for (entry = dev->agp->memory; entry; entry = entry->next) { 292 if (entry->handle == handle) 293 return entry; 294 } 295 return NULL; 296} 297 298/** 299 * Unbind AGP memory from the GATT (ioctl). 300 * 301 * \param inode device inode. 302 * \param filp file pointer. 303 * \param cmd command. 304 * \param arg pointer to a drm_agp_binding structure. 305 * \return zero on success or a negative number on failure. 306 * 307 * Verifies the AGP device is present and acquired, looks-up the AGP memory 308 * entry and passes it to the unbind_agp() function. 309 */ 310int drm_agp_unbind(drm_device_t *dev, drm_agp_binding_t *request) 311{ 312 drm_agp_mem_t *entry; 313 int ret; 314 315 if (!dev->agp || !dev->agp->acquired) 316 return -EINVAL; 317 if (!(entry = drm_agp_lookup_entry(dev, request->handle))) 318 return -EINVAL; 319 if (!entry->bound) 320 return -EINVAL; 321 ret = drm_unbind_agp(entry->memory); 322 if (ret == 0) 323 entry->bound = 0; 324 return ret; 325} 326EXPORT_SYMBOL(drm_agp_unbind); 327 328int drm_agp_unbind_ioctl(struct inode *inode, struct file *filp, 329 unsigned int cmd, unsigned long arg) 330{ 331 drm_file_t *priv = filp->private_data; 332 drm_device_t *dev = priv->head->dev; 333 drm_agp_binding_t request; 334 335 if (copy_from_user 336 (&request, (drm_agp_binding_t __user *) arg, sizeof(request))) 337 return -EFAULT; 338 339 return drm_agp_unbind(dev, &request); 340} 341 342/** 343 * Bind AGP memory into the GATT (ioctl) 344 * 345 * \param inode device inode. 346 * \param filp file pointer. 347 * \param cmd command. 348 * \param arg pointer to a drm_agp_binding structure. 349 * \return zero on success or a negative number on failure. 350 * 351 * Verifies the AGP device is present and has been acquired and that no memory 352 * is currently bound into the GATT. Looks-up the AGP memory entry and passes 353 * it to bind_agp() function. 354 */ 355int drm_agp_bind(drm_device_t *dev, drm_agp_binding_t *request) 356{ 357 drm_agp_mem_t *entry; 358 int retcode; 359 int page; 360 361 if (!dev->agp || !dev->agp->acquired) 362 return -EINVAL; 363 if (!(entry = drm_agp_lookup_entry(dev, request->handle))) 364 return -EINVAL; 365 if (entry->bound) 366 return -EINVAL; 367 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE; 368 if ((retcode = drm_bind_agp(entry->memory, page))) 369 return retcode; 370 entry->bound = dev->agp->base + (page << PAGE_SHIFT); 371 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n", 372 dev->agp->base, entry->bound); 373 return 0; 374} 375EXPORT_SYMBOL(drm_agp_bind); 376 377int drm_agp_bind_ioctl(struct inode *inode, struct file *filp, 378 unsigned int cmd, unsigned long arg) 379{ 380 drm_file_t *priv = filp->private_data; 381 drm_device_t *dev = priv->head->dev; 382 drm_agp_binding_t request; 383 384 if (copy_from_user 385 (&request, (drm_agp_binding_t __user *) arg, sizeof(request))) 386 return -EFAULT; 387 388 return drm_agp_bind(dev, &request); 389} 390 391/** 392 * Free AGP memory (ioctl). 393 * 394 * \param inode device inode. 395 * \param filp file pointer. 396 * \param cmd command. 397 * \param arg pointer to a drm_agp_buffer structure. 398 * \return zero on success or a negative number on failure. 399 * 400 * Verifies the AGP device is present and has been acquired and looks up the 401 * AGP memory entry. If the memory it's currently bound, unbind it via 402 * unbind_agp(). Frees it via free_agp() as well as the entry itself 403 * and unlinks from the doubly linked list it's inserted in. 404 */ 405int drm_agp_free(drm_device_t *dev, drm_agp_buffer_t *request) 406{ 407 drm_agp_mem_t *entry; 408 409 if (!dev->agp || !dev->agp->acquired) 410 return -EINVAL; 411 if (!(entry = drm_agp_lookup_entry(dev, request->handle))) 412 return -EINVAL; 413 if (entry->bound) 414 drm_unbind_agp(entry->memory); 415 416 if (entry->prev) 417 entry->prev->next = entry->next; 418 else 419 dev->agp->memory = entry->next; 420 421 if (entry->next) 422 entry->next->prev = entry->prev; 423 424 drm_free_agp(entry->memory, entry->pages); 425 drm_free(entry, sizeof(*entry), DRM_MEM_AGPLISTS); 426 return 0; 427} 428EXPORT_SYMBOL(drm_agp_free); 429 430int drm_agp_free_ioctl(struct inode *inode, struct file *filp, 431 unsigned int cmd, unsigned long arg) 432{ 433 drm_file_t *priv = filp->private_data; 434 drm_device_t *dev = priv->head->dev; 435 drm_agp_buffer_t request; 436 437 if (copy_from_user 438 (&request, (drm_agp_buffer_t __user *) arg, sizeof(request))) 439 return -EFAULT; 440 441 return drm_agp_free(dev, &request); 442} 443 444/** 445 * Initialize the AGP resources. 446 * 447 * \return pointer to a drm_agp_head structure. 448 * 449 * Gets the drm_agp_t structure which is made available by the agpgart module 450 * via the inter_module_* functions. Creates and initializes a drm_agp_head 451 * structure. 452 */ 453drm_agp_head_t *drm_agp_init(drm_device_t * dev) 454{ 455 drm_agp_head_t *head = NULL; 456 457 if (!(head = drm_alloc(sizeof(*head), DRM_MEM_AGPLISTS))) 458 return NULL; 459 memset((void *)head, 0, sizeof(*head)); 460 head->bridge = agp_find_bridge(dev->pdev); 461 if (!head->bridge) { 462 if (!(head->bridge = agp_backend_acquire(dev->pdev))) { 463 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS); 464 return NULL; 465 } 466 agp_copy_info(head->bridge, &head->agp_info); 467 agp_backend_release(head->bridge); 468 } else { 469 agp_copy_info(head->bridge, &head->agp_info); 470 } 471 if (head->agp_info.chipset == NOT_SUPPORTED) { 472 drm_free(head, sizeof(*head), DRM_MEM_AGPLISTS); 473 return NULL; 474 } 475 head->memory = NULL; 476 head->cant_use_aperture = head->agp_info.cant_use_aperture; 477 head->page_mask = head->agp_info.page_mask; 478 479 return head; 480} 481 482/** Calls agp_allocate_memory() */ 483DRM_AGP_MEM *drm_agp_allocate_memory(struct agp_bridge_data * bridge, 484 size_t pages, u32 type) 485{ 486 return agp_allocate_memory(bridge, pages, type); 487} 488 489/** Calls agp_free_memory() */ 490int drm_agp_free_memory(DRM_AGP_MEM * handle) 491{ 492 if (!handle) 493 return 0; 494 agp_free_memory(handle); 495 return 1; 496} 497 498/** Calls agp_bind_memory() */ 499int drm_agp_bind_memory(DRM_AGP_MEM * handle, off_t start) 500{ 501 if (!handle) 502 return -EINVAL; 503 return agp_bind_memory(handle, start); 504} 505 506/** Calls agp_unbind_memory() */ 507int drm_agp_unbind_memory(DRM_AGP_MEM * handle) 508{ 509 if (!handle) 510 return -EINVAL; 511 return agp_unbind_memory(handle); 512} 513 514#endif /* __OS_HAS_AGP */