1 /*- 2 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California. 4 * All Rights Reserved. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the next 14 * paragraph) shall be included in all copies or substantial portions of the 15 * Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 20 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 * OTHER DEALINGS IN THE SOFTWARE. 24 * 25 * Author: 26 * Rickard E. (Rik) Faith <faith@valinux.com> 27 * Gareth Hughes <gareth@valinux.com> 28 * 29 */ 30 31 /** @file drm_agpsupport.c 32 * Support code for tying the kernel AGP support to DRM drivers and 33 * the DRM's AGP ioctls. 34 */ 35 36 #include "dev/drm/drmP.h" 37 38 #include <dev/agp/agpreg.h> 39 #include <bus/pci/pcireg.h> 40 41 /* Returns 1 if AGP or 0 if not. */ 42 static int 43 drm_device_find_capability(struct drm_device *dev, int cap) 44 { 45 /* Code taken from agp.c. IWBNI that was a public interface. */ 46 u_int32_t status; 47 u_int8_t ptr, next; 48 49 /* 50 * Check the CAP_LIST bit of the PCI status register first. 51 */ 52 status = pci_read_config(dev->device, PCIR_STATUS, 2); 53 if (!(status & 0x10)) 54 return 0; 55 56 /* 57 * Traverse the capabilities list. 58 */ 59 for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1); 60 ptr != 0; 61 ptr = next) { 62 u_int32_t capid = pci_read_config(dev->device, ptr, 4); 63 next = AGP_CAPID_GET_NEXT_PTR(capid); 64 65 /* 66 * If this capability entry ID is cap, then we are done. 67 */ 68 if (AGP_CAPID_GET_CAP_ID(capid) == cap) 69 return 1; 70 } 71 72 return 0; 73 } 74 75 int drm_device_is_agp(struct drm_device *dev) 76 { 77 if (dev->driver->device_is_agp != NULL) { 78 int ret; 79 80 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely 81 * AGP, 2 = fall back to PCI capability 82 */ 83 ret = (*dev->driver->device_is_agp)(dev); 84 if (ret != DRM_MIGHT_BE_AGP) 85 return ret; 86 } 87 88 return (drm_device_find_capability(dev, PCIY_AGP)); 89 } 90 91 int drm_device_is_pcie(struct drm_device *dev) 92 { 93 return (drm_device_find_capability(dev, PCIY_EXPRESS)); 94 } 95 96 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info) 97 { 98 struct agp_info *kern; 99 100 if (!dev->agp || !dev->agp->acquired) 101 return EINVAL; 102 103 kern = &dev->agp->info; 104 agp_get_info(dev->agp->agpdev, kern); 105 info->agp_version_major = 1; 106 info->agp_version_minor = 0; 107 info->mode = kern->ai_mode; 108 info->aperture_base = kern->ai_aperture_base; 109 info->aperture_size = kern->ai_aperture_size; 110 info->memory_allowed = kern->ai_memory_allowed; 111 info->memory_used = kern->ai_memory_used; 112 info->id_vendor = kern->ai_devid & 0xffff; 113 info->id_device = kern->ai_devid >> 16; 114 115 return 0; 116 } 117 118 int drm_agp_info_ioctl(struct drm_device *dev, void *data, 119 struct drm_file *file_priv) 120 { 121 int err; 122 struct drm_agp_info info; 123 124 err = drm_agp_info(dev, &info); 125 if (err != 0) 126 return err; 127 128 *(struct drm_agp_info *) data = info; 129 return 0; 130 } 131 132 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data, 133 struct drm_file *file_priv) 134 { 135 136 return drm_agp_acquire(dev); 137 } 138 139 int drm_agp_acquire(struct drm_device *dev) 140 { 141 int retcode; 142 143 if (!dev->agp || dev->agp->acquired) 144 return EINVAL; 145 146 retcode = agp_acquire(dev->agp->agpdev); 147 if (retcode) 148 return retcode; 149 150 dev->agp->acquired = 1; 151 return 0; 152 } 153 154 int drm_agp_release_ioctl(struct drm_device *dev, void *data, 155 struct drm_file *file_priv) 156 { 157 158 return drm_agp_release(dev); 159 } 160 161 int drm_agp_release(struct drm_device * dev) 162 { 163 if (!dev->agp || !dev->agp->acquired) 164 return EINVAL; 165 agp_release(dev->agp->agpdev); 166 dev->agp->acquired = 0; 167 return 0; 168 } 169 170 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode) 171 { 172 173 if (!dev->agp || !dev->agp->acquired) 174 return EINVAL; 175 176 dev->agp->mode = mode.mode; 177 agp_enable(dev->agp->agpdev, mode.mode); 178 dev->agp->enabled = 1; 179 return 0; 180 } 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 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request) 193 { 194 drm_agp_mem_t *entry; 195 void *handle; 196 unsigned long pages; 197 u_int32_t type; 198 struct agp_memory_info info; 199 200 if (!dev->agp || !dev->agp->acquired) 201 return EINVAL; 202 203 entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO); 204 if (entry == NULL) 205 return ENOMEM; 206 207 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE; 208 type = (u_int32_t) request->type; 209 210 DRM_UNLOCK(); 211 handle = drm_agp_allocate_memory(pages, type); 212 DRM_LOCK(); 213 if (handle == NULL) { 214 free(entry, DRM_MEM_AGPLISTS); 215 return ENOMEM; 216 } 217 218 entry->handle = handle; 219 entry->bound = 0; 220 entry->pages = pages; 221 entry->prev = NULL; 222 entry->next = dev->agp->memory; 223 if (dev->agp->memory) 224 dev->agp->memory->prev = entry; 225 dev->agp->memory = entry; 226 227 agp_memory_info(dev->agp->agpdev, entry->handle, &info); 228 229 request->handle = (unsigned long) entry->handle; 230 request->physical = info.ami_physical; 231 232 return 0; 233 } 234 235 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data, 236 struct drm_file *file_priv) 237 { 238 struct drm_agp_buffer request; 239 int retcode; 240 241 request = *(struct drm_agp_buffer *) data; 242 243 DRM_LOCK(); 244 retcode = drm_agp_alloc(dev, &request); 245 DRM_UNLOCK(); 246 247 *(struct drm_agp_buffer *) data = request; 248 249 return retcode; 250 } 251 252 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev, 253 void *handle) 254 { 255 drm_agp_mem_t *entry; 256 257 for (entry = dev->agp->memory; entry; entry = entry->next) { 258 if (entry->handle == handle) return entry; 259 } 260 return NULL; 261 } 262 263 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request) 264 { 265 drm_agp_mem_t *entry; 266 int retcode; 267 268 if (!dev->agp || !dev->agp->acquired) 269 return EINVAL; 270 271 entry = drm_agp_lookup_entry(dev, (void *)request->handle); 272 if (entry == NULL || !entry->bound) 273 return EINVAL; 274 275 DRM_UNLOCK(); 276 retcode = drm_agp_unbind_memory(entry->handle); 277 DRM_LOCK(); 278 279 if (retcode == 0) 280 entry->bound = 0; 281 282 return retcode; 283 } 284 285 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data, 286 struct drm_file *file_priv) 287 { 288 struct drm_agp_binding request; 289 int retcode; 290 291 request = *(struct drm_agp_binding *) data; 292 293 DRM_LOCK(); 294 retcode = drm_agp_unbind(dev, &request); 295 DRM_UNLOCK(); 296 297 return retcode; 298 } 299 300 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request) 301 { 302 drm_agp_mem_t *entry; 303 int retcode; 304 int page; 305 306 if (!dev->agp || !dev->agp->acquired) 307 return EINVAL; 308 309 DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE); 310 311 entry = drm_agp_lookup_entry(dev, (void *)request->handle); 312 if (entry == NULL || entry->bound) 313 return EINVAL; 314 315 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE; 316 317 DRM_UNLOCK(); 318 retcode = drm_agp_bind_memory(entry->handle, page); 319 DRM_LOCK(); 320 if (retcode == 0) 321 entry->bound = dev->agp->base + (page << PAGE_SHIFT); 322 323 return retcode; 324 } 325 326 int drm_agp_bind_ioctl(struct drm_device *dev, void *data, 327 struct drm_file *file_priv) 328 { 329 struct drm_agp_binding request; 330 int retcode; 331 332 request = *(struct drm_agp_binding *) data; 333 334 DRM_LOCK(); 335 retcode = drm_agp_bind(dev, &request); 336 DRM_UNLOCK(); 337 338 return retcode; 339 } 340 341 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request) 342 { 343 drm_agp_mem_t *entry; 344 345 if (!dev->agp || !dev->agp->acquired) 346 return EINVAL; 347 348 entry = drm_agp_lookup_entry(dev, (void*)request->handle); 349 if (entry == NULL) 350 return EINVAL; 351 352 if (entry->prev) 353 entry->prev->next = entry->next; 354 else 355 dev->agp->memory = entry->next; 356 if (entry->next) 357 entry->next->prev = entry->prev; 358 359 DRM_UNLOCK(); 360 if (entry->bound) 361 drm_agp_unbind_memory(entry->handle); 362 drm_agp_free_memory(entry->handle); 363 DRM_LOCK(); 364 365 free(entry, DRM_MEM_AGPLISTS); 366 367 return 0; 368 369 } 370 371 int drm_agp_free_ioctl(struct drm_device *dev, void *data, 372 struct drm_file *file_priv) 373 { 374 struct drm_agp_buffer request; 375 int retcode; 376 377 request = *(struct drm_agp_buffer *) data; 378 379 DRM_LOCK(); 380 retcode = drm_agp_free(dev, &request); 381 DRM_UNLOCK(); 382 383 return retcode; 384 } 385 386 drm_agp_head_t *drm_agp_init(void) 387 { 388 device_t agpdev; 389 drm_agp_head_t *head = NULL; 390 int agp_available = 1; 391 392 agpdev = DRM_AGP_FIND_DEVICE(); 393 if (!agpdev) 394 agp_available = 0; 395 396 DRM_DEBUG("agp_available = %d\n", agp_available); 397 398 if (agp_available) { 399 head = malloc(sizeof(*head), DRM_MEM_AGPLISTS, 400 M_NOWAIT | M_ZERO); 401 if (head == NULL) 402 return NULL; 403 head->agpdev = agpdev; 404 agp_get_info(agpdev, &head->info); 405 head->base = head->info.ai_aperture_base; 406 head->memory = NULL; 407 DRM_INFO("AGP at 0x%08lx %dMB\n", 408 (long)head->info.ai_aperture_base, 409 (int)(head->info.ai_aperture_size >> 20)); 410 } 411 return head; 412 } 413 414 void *drm_agp_allocate_memory(size_t pages, u32 type) 415 { 416 device_t agpdev; 417 418 agpdev = DRM_AGP_FIND_DEVICE(); 419 if (!agpdev) 420 return NULL; 421 422 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT); 423 } 424 425 int drm_agp_free_memory(void *handle) 426 { 427 device_t agpdev; 428 429 agpdev = DRM_AGP_FIND_DEVICE(); 430 if (!agpdev || !handle) 431 return 0; 432 433 agp_free_memory(agpdev, handle); 434 return 1; 435 } 436 437 int drm_agp_bind_memory(void *handle, off_t start) 438 { 439 device_t agpdev; 440 441 agpdev = DRM_AGP_FIND_DEVICE(); 442 if (!agpdev || !handle) 443 return EINVAL; 444 445 return agp_bind_memory(agpdev, handle, start * PAGE_SIZE); 446 } 447 448 int drm_agp_unbind_memory(void *handle) 449 { 450 device_t agpdev; 451 452 agpdev = DRM_AGP_FIND_DEVICE(); 453 if (!agpdev || !handle) 454 return EINVAL; 455 456 return agp_unbind_memory(agpdev, handle); 457 } 458