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 <drm/drmP.h> 35 #include <linux/errno.h> 36 #include <linux/export.h> 37 #include <linux/module.h> 38 #include <linux/slab.h> 39 40 #if __OS_HAS_AGP 41 42 #include <asm/agp.h> 43 44 /** 45 * Get AGP information. 46 * 47 * \param inode device inode. 48 * \param file_priv DRM file private. 49 * \param cmd command. 50 * \param arg pointer to a (output) drm_agp_info structure. 51 * \return zero on success or a negative number on failure. 52 * 53 * Verifies the AGP device has been initialized and acquired and fills in the 54 * drm_agp_info structure with the information in drm_agp_head::agp_info. 55 */ 56 int drm_agp_info(struct drm_device *dev, struct drm_agp_info *info) 57 { 58 struct agp_kern_info *kern; 59 60 if (!dev->agp || !dev->agp->acquired) 61 return -EINVAL; 62 63 kern = &dev->agp->agp_info; 64 #if __NetBSD__ 65 info->agp_version_major = 1; 66 info->agp_version_minor = 0; 67 info->mode = kern->aki_info.ai_mode; 68 info->aperture_base = kern->aki_info.ai_aperture_base; 69 info->aperture_size = kern->aki_info.ai_aperture_size; 70 info->memory_allowed = kern->aki_info.ai_memory_allowed; 71 info->memory_used = kern->aki_info.ai_memory_used; 72 info->id_vendor = PCI_VENDOR(kern->aki_info.ai_devid); 73 info->id_device = PCI_PRODUCT(kern->aki_info.ai_devid); 74 #else 75 info->agp_version_major = kern->version.major; 76 info->agp_version_minor = kern->version.minor; 77 info->mode = kern->mode; 78 info->aperture_base = kern->aper_base; 79 info->aperture_size = kern->aper_size * 1024 * 1024; 80 info->memory_allowed = kern->max_memory << PAGE_SHIFT; 81 info->memory_used = kern->current_memory << PAGE_SHIFT; 82 info->id_vendor = kern->device->vendor; 83 info->id_device = kern->device->device; 84 #endif 85 86 return 0; 87 } 88 89 EXPORT_SYMBOL(drm_agp_info); 90 91 int drm_agp_info_ioctl(struct drm_device *dev, void *data, 92 struct drm_file *file_priv) 93 { 94 struct drm_agp_info *info = data; 95 int err; 96 97 err = drm_agp_info(dev, info); 98 if (err) 99 return err; 100 101 return 0; 102 } 103 104 /** 105 * Acquire the AGP device. 106 * 107 * \param dev DRM device that is to acquire AGP. 108 * \return zero on success or a negative number on failure. 109 * 110 * Verifies the AGP device hasn't been acquired before and calls 111 * \c agp_backend_acquire. 112 */ 113 int drm_agp_acquire(struct drm_device * dev) 114 { 115 if (!dev->agp) 116 return -ENODEV; 117 if (dev->agp->acquired) 118 return -EBUSY; 119 if (!(dev->agp->bridge = agp_backend_acquire(dev->pdev))) 120 return -ENODEV; 121 dev->agp->acquired = 1; 122 return 0; 123 } 124 125 EXPORT_SYMBOL(drm_agp_acquire); 126 127 /** 128 * Acquire the AGP device (ioctl). 129 * 130 * \param inode device inode. 131 * \param file_priv DRM file private. 132 * \param cmd command. 133 * \param arg user argument. 134 * \return zero on success or a negative number on failure. 135 * 136 * Verifies the AGP device hasn't been acquired before and calls 137 * \c agp_backend_acquire. 138 */ 139 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data, 140 struct drm_file *file_priv) 141 { 142 return drm_agp_acquire((struct drm_device *) file_priv->minor->dev); 143 } 144 145 /** 146 * Release the AGP device. 147 * 148 * \param dev DRM device that is to release AGP. 149 * \return zero on success or a negative number on failure. 150 * 151 * Verifies the AGP device has been acquired and calls \c agp_backend_release. 152 */ 153 int drm_agp_release(struct drm_device * dev) 154 { 155 if (!dev->agp || !dev->agp->acquired) 156 return -EINVAL; 157 agp_backend_release(dev->agp->bridge); 158 dev->agp->acquired = 0; 159 return 0; 160 } 161 EXPORT_SYMBOL(drm_agp_release); 162 163 int drm_agp_release_ioctl(struct drm_device *dev, void *data, 164 struct drm_file *file_priv) 165 { 166 return drm_agp_release(dev); 167 } 168 169 /** 170 * Enable the AGP bus. 171 * 172 * \param dev DRM device that has previously acquired AGP. 173 * \param mode Requested AGP mode. 174 * \return zero on success or a negative number on failure. 175 * 176 * Verifies the AGP device has been acquired but not enabled, and calls 177 * \c agp_enable. 178 */ 179 int drm_agp_enable(struct drm_device * dev, struct drm_agp_mode mode) 180 { 181 if (!dev->agp || !dev->agp->acquired) 182 return -EINVAL; 183 184 dev->agp->mode = mode.mode; 185 agp_enable(dev->agp->bridge, mode.mode); 186 dev->agp->enabled = 1; 187 return 0; 188 } 189 190 EXPORT_SYMBOL(drm_agp_enable); 191 192 int drm_agp_enable_ioctl(struct drm_device *dev, void *data, 193 struct drm_file *file_priv) 194 { 195 struct drm_agp_mode *mode = data; 196 197 return drm_agp_enable(dev, *mode); 198 } 199 200 /** 201 * Allocate AGP memory. 202 * 203 * \param inode device inode. 204 * \param file_priv file private pointer. 205 * \param cmd command. 206 * \param arg pointer to a drm_agp_buffer structure. 207 * \return zero on success or a negative number on failure. 208 * 209 * Verifies the AGP device is present and has been acquired, allocates the 210 * memory via agp_allocate_memory() and creates a drm_agp_mem entry for it. 211 */ 212 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request) 213 { 214 struct drm_agp_mem *entry; 215 struct agp_memory *memory; 216 unsigned long pages; 217 u32 type; 218 219 if (!dev->agp || !dev->agp->acquired) 220 return -EINVAL; 221 if (!(entry = kzalloc(sizeof(*entry), GFP_KERNEL))) 222 return -ENOMEM; 223 224 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE; 225 type = (u32) request->type; 226 if (!(memory = agp_allocate_memory(dev->agp->bridge, pages, type))) { 227 kfree(entry); 228 return -ENOMEM; 229 } 230 231 #ifdef __NetBSD__ 232 /* I presume the `+ 1' is there to avoid an id of 0 or something. */ 233 entry->handle = (unsigned long)memory->am_id + 1; 234 #else 235 entry->handle = (unsigned long)memory->key + 1; 236 #endif 237 entry->memory = memory; 238 entry->bound = 0; 239 entry->pages = pages; 240 list_add(&entry->head, &dev->agp->memory); 241 242 request->handle = entry->handle; 243 #ifdef __NetBSD__ 244 { 245 struct agp_memory_info info; 246 agp_memory_info(dev->agp->bridge, memory, &info); 247 request->physical = info.ami_physical; 248 } 249 #else 250 request->physical = memory->physical; 251 #endif 252 253 return 0; 254 } 255 EXPORT_SYMBOL(drm_agp_alloc); 256 257 258 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data, 259 struct drm_file *file_priv) 260 { 261 struct drm_agp_buffer *request = data; 262 263 return drm_agp_alloc(dev, request); 264 } 265 266 /** 267 * Search for the AGP memory entry associated with a handle. 268 * 269 * \param dev DRM device structure. 270 * \param handle AGP memory handle. 271 * \return pointer to the drm_agp_mem structure associated with \p handle. 272 * 273 * Walks through drm_agp_head::memory until finding a matching handle. 274 */ 275 static struct drm_agp_mem *drm_agp_lookup_entry(struct drm_device * dev, 276 unsigned long handle) 277 { 278 struct drm_agp_mem *entry; 279 280 list_for_each_entry(entry, &dev->agp->memory, head) { 281 if (entry->handle == handle) 282 return entry; 283 } 284 return NULL; 285 } 286 287 /** 288 * Unbind AGP memory from the GATT (ioctl). 289 * 290 * \param inode device inode. 291 * \param file_priv DRM file private. 292 * \param cmd command. 293 * \param arg pointer to a drm_agp_binding structure. 294 * \return zero on success or a negative number on failure. 295 * 296 * Verifies the AGP device is present and acquired, looks-up the AGP memory 297 * entry and passes it to the unbind_agp() function. 298 */ 299 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request) 300 { 301 struct drm_agp_mem *entry; 302 int ret; 303 304 if (!dev->agp || !dev->agp->acquired) 305 return -EINVAL; 306 if (!(entry = drm_agp_lookup_entry(dev, request->handle))) 307 return -EINVAL; 308 if (!entry->bound) 309 return -EINVAL; 310 #ifdef __NetBSD__ 311 ret = drm_unbind_agp(dev->agp->bridge, entry->memory); 312 #else 313 ret = drm_unbind_agp(entry->memory); 314 #endif 315 if (ret == 0) 316 entry->bound = 0; 317 return ret; 318 } 319 EXPORT_SYMBOL(drm_agp_unbind); 320 321 322 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data, 323 struct drm_file *file_priv) 324 { 325 struct drm_agp_binding *request = data; 326 327 return drm_agp_unbind(dev, request); 328 } 329 330 /** 331 * Bind AGP memory into the GATT (ioctl) 332 * 333 * \param inode device inode. 334 * \param file_priv DRM file private. 335 * \param cmd command. 336 * \param arg pointer to a drm_agp_binding structure. 337 * \return zero on success or a negative number on failure. 338 * 339 * Verifies the AGP device is present and has been acquired and that no memory 340 * is currently bound into the GATT. Looks-up the AGP memory entry and passes 341 * it to bind_agp() function. 342 */ 343 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request) 344 { 345 struct drm_agp_mem *entry; 346 int retcode; 347 int page; 348 349 if (!dev->agp || !dev->agp->acquired) 350 return -EINVAL; 351 if (!(entry = drm_agp_lookup_entry(dev, request->handle))) 352 return -EINVAL; 353 if (entry->bound) 354 return -EINVAL; 355 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE; 356 #ifdef __NetBSD__ 357 if ((retcode = drm_bind_agp(dev->agp->bridge, entry->memory, page))) 358 return retcode; 359 #else 360 if ((retcode = drm_bind_agp(entry->memory, page))) 361 return retcode; 362 #endif 363 entry->bound = dev->agp->base + (page << PAGE_SHIFT); 364 DRM_DEBUG("base = 0x%lx entry->bound = 0x%lx\n", 365 dev->agp->base, entry->bound); 366 return 0; 367 } 368 EXPORT_SYMBOL(drm_agp_bind); 369 370 371 int drm_agp_bind_ioctl(struct drm_device *dev, void *data, 372 struct drm_file *file_priv) 373 { 374 struct drm_agp_binding *request = data; 375 376 return drm_agp_bind(dev, request); 377 } 378 379 /** 380 * Free AGP memory (ioctl). 381 * 382 * \param inode device inode. 383 * \param file_priv DRM file private. 384 * \param cmd command. 385 * \param arg pointer to a drm_agp_buffer structure. 386 * \return zero on success or a negative number on failure. 387 * 388 * Verifies the AGP device is present and has been acquired and looks up the 389 * AGP memory entry. If the memory it's currently bound, unbind it via 390 * unbind_agp(). Frees it via free_agp() as well as the entry itself 391 * and unlinks from the doubly linked list it's inserted in. 392 */ 393 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request) 394 { 395 struct drm_agp_mem *entry; 396 397 if (!dev->agp || !dev->agp->acquired) 398 return -EINVAL; 399 if (!(entry = drm_agp_lookup_entry(dev, request->handle))) 400 return -EINVAL; 401 if (entry->bound) 402 #ifdef __NetBSD__ 403 drm_unbind_agp(dev->agp->bridge, entry->memory); 404 #else 405 drm_unbind_agp(entry->memory); 406 #endif 407 408 list_del(&entry->head); 409 410 #ifdef __NetBSD__ 411 drm_free_agp(dev->agp->bridge, entry->memory, entry->pages); 412 #else 413 drm_free_agp(entry->memory, entry->pages); 414 #endif 415 kfree(entry); 416 return 0; 417 } 418 EXPORT_SYMBOL(drm_agp_free); 419 420 421 422 int drm_agp_free_ioctl(struct drm_device *dev, void *data, 423 struct drm_file *file_priv) 424 { 425 struct drm_agp_buffer *request = data; 426 427 return drm_agp_free(dev, request); 428 } 429 430 /** 431 * Initialize the AGP resources. 432 * 433 * \return pointer to a drm_agp_head structure. 434 * 435 * Gets the drm_agp_t structure which is made available by the agpgart module 436 * via the inter_module_* functions. Creates and initializes a drm_agp_head 437 * structure. 438 * 439 * Note that final cleanup of the kmalloced structure is directly done in 440 * drm_pci_agp_destroy. 441 */ 442 struct drm_agp_head *drm_agp_init(struct drm_device *dev) 443 { 444 struct drm_agp_head *head = NULL; 445 446 if (!(head = kzalloc(sizeof(*head), GFP_KERNEL))) 447 return NULL; 448 head->bridge = agp_find_bridge(dev->pdev); 449 if (!head->bridge) { 450 if (!(head->bridge = agp_backend_acquire(dev->pdev))) { 451 kfree(head); 452 return NULL; 453 } 454 agp_copy_info(head->bridge, &head->agp_info); 455 agp_backend_release(head->bridge); 456 } else { 457 agp_copy_info(head->bridge, &head->agp_info); 458 } 459 #ifndef __NetBSD__ 460 /* Why would anything even attach in this case? */ 461 if (head->agp_info.chipset == NOT_SUPPORTED) { 462 kfree(head); 463 return NULL; 464 } 465 #endif 466 INIT_LIST_HEAD(&head->memory); 467 #ifdef __NetBSD__ 468 head->cant_use_aperture = false; /* XXX */ 469 head->page_mask = ~0UL; 470 head->base = head->agp_info.aki_info.ai_aperture_base; 471 #else 472 head->cant_use_aperture = head->agp_info.cant_use_aperture; 473 head->page_mask = head->agp_info.page_mask; 474 head->base = head->agp_info.aper_base; 475 #endif 476 return head; 477 } 478 479 /** 480 * drm_agp_clear - Clear AGP resource list 481 * @dev: DRM device 482 * 483 * Iterate over all AGP resources and remove them. But keep the AGP head 484 * intact so it can still be used. It is safe to call this if AGP is disabled or 485 * was already removed. 486 * 487 * If DRIVER_MODESET is active, nothing is done to protect the modesetting 488 * resources from getting destroyed. Drivers are responsible of cleaning them up 489 * during device shutdown. 490 */ 491 void drm_agp_clear(struct drm_device *dev) 492 { 493 struct drm_agp_mem *entry, *tempe; 494 495 if (!dev->agp) 496 return; 497 if (drm_core_check_feature(dev, DRIVER_MODESET)) 498 return; 499 500 list_for_each_entry_safe(entry, tempe, &dev->agp->memory, head) { 501 if (entry->bound) 502 #ifdef __NetBSD__ 503 drm_unbind_agp(dev->agp->bridge, entry->memory); 504 #endif 505 #ifdef __NetBSD__ 506 drm_free_agp(dev->agp->bridge, entry->memory, entry->pages); 507 #else 508 drm_free_agp(entry->memory, entry->pages); 509 #endif 510 kfree(entry); 511 } 512 INIT_LIST_HEAD(&dev->agp->memory); 513 514 if (dev->agp->acquired) 515 drm_agp_release(dev); 516 517 dev->agp->acquired = 0; 518 dev->agp->enabled = 0; 519 } 520 521 #ifndef __NetBSD__ /* XXX Dead code that doesn't make sense... */ 522 /** 523 * Binds a collection of pages into AGP memory at the given offset, returning 524 * the AGP memory structure containing them. 525 * 526 * No reference is held on the pages during this time -- it is up to the 527 * caller to handle that. 528 */ 529 struct agp_memory * 530 drm_agp_bind_pages(struct drm_device *dev, 531 struct page **pages, 532 unsigned long num_pages, 533 uint32_t gtt_offset, 534 u32 type) 535 { 536 struct agp_memory *mem; 537 int ret, i; 538 539 DRM_DEBUG("\n"); 540 541 mem = agp_allocate_memory(dev->agp->bridge, num_pages, 542 type); 543 if (mem == NULL) { 544 DRM_ERROR("Failed to allocate memory for %ld pages\n", 545 num_pages); 546 return NULL; 547 } 548 549 for (i = 0; i < num_pages; i++) 550 mem->pages[i] = pages[i]; 551 mem->page_count = num_pages; 552 553 mem->is_flushed = true; 554 ret = agp_bind_memory(mem, gtt_offset / PAGE_SIZE); 555 if (ret != 0) { 556 DRM_ERROR("Failed to bind AGP memory: %d\n", ret); 557 agp_free_memory(mem); 558 return NULL; 559 } 560 561 return mem; 562 } 563 EXPORT_SYMBOL(drm_agp_bind_pages); 564 565 #endif 566 567 #endif /* __OS_HAS_AGP */ 568