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