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 * $FreeBSD: src/sys/dev/drm2/drm_agpsupport.c,v 1.1 2012/05/22 11:07:44 kib Exp $ 30 */ 31 32 /** @file drm_agpsupport.c 33 * Support code for tying the kernel AGP support to DRM drivers and 34 * the DRM's AGP ioctls. 35 */ 36 37 #include <drm/drmP.h> 38 #include <linux/module.h> 39 #include "drm_legacy.h" 40 41 #include <dev/agp/agpreg.h> 42 #include <bus/pci/pcireg.h> 43 44 /* Returns 1 if AGP or 0 if not. */ 45 static int 46 drm_device_find_capability(struct drm_device *dev, int cap) 47 { 48 return (pci_find_extcap(dev->dev, cap, NULL) == 0); 49 } 50 51 int drm_device_is_agp(struct drm_device *dev) 52 { 53 if (dev->driver->device_is_agp != NULL) { 54 int ret; 55 56 /* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely 57 * AGP, 2 = fall back to PCI capability 58 */ 59 ret = (*dev->driver->device_is_agp)(dev); 60 if (ret != DRM_MIGHT_BE_AGP) 61 return ret; 62 } 63 64 return (drm_device_find_capability(dev, PCIY_AGP)); 65 } 66 67 int drm_device_is_pcie(struct drm_device *dev) 68 { 69 return (drm_device_find_capability(dev, PCIY_EXPRESS)); 70 } 71 72 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info) 73 { 74 struct agp_info *kern; 75 76 if (!dev->agp || !dev->agp->acquired) 77 return -EINVAL; 78 79 kern = &dev->agp->agp_info; 80 agp_get_info(dev->agp->agpdev, kern); 81 info->agp_version_major = 1; 82 info->agp_version_minor = 0; 83 info->mode = kern->ai_mode; 84 info->aperture_base = kern->ai_aperture_base; 85 info->aperture_size = kern->ai_aperture_size; 86 info->memory_allowed = kern->ai_memory_allowed; 87 info->memory_used = kern->ai_memory_used; 88 info->id_vendor = kern->ai_devid & 0xffff; 89 info->id_device = kern->ai_devid >> 16; 90 91 return 0; 92 } 93 94 int drm_agp_info_ioctl(struct drm_device *dev, void *data, 95 struct drm_file *file_priv) 96 { 97 int err; 98 struct drm_agp_info info; 99 100 err = drm_agp_info(dev, &info); 101 if (err != 0) 102 return err; 103 104 *(struct drm_agp_info *) data = info; 105 return 0; 106 } 107 108 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data, 109 struct drm_file *file_priv) 110 { 111 112 return drm_agp_acquire(dev); 113 } 114 115 int drm_agp_acquire(struct drm_device *dev) 116 { 117 int retcode; 118 119 if (!dev->agp || dev->agp->acquired) 120 return -EINVAL; 121 122 retcode = -agp_acquire(dev->agp->agpdev); 123 if (retcode) 124 return retcode; 125 126 dev->agp->acquired = 1; 127 return 0; 128 } 129 130 int drm_agp_release_ioctl(struct drm_device *dev, void *data, 131 struct drm_file *file_priv) 132 { 133 134 return drm_agp_release(dev); 135 } 136 137 int drm_agp_release(struct drm_device * dev) 138 { 139 if (!dev->agp || !dev->agp->acquired) 140 return -EINVAL; 141 agp_release(dev->agp->agpdev); 142 dev->agp->acquired = 0; 143 return 0; 144 } 145 146 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode) 147 { 148 149 if (!dev->agp || !dev->agp->acquired) 150 return -EINVAL; 151 152 dev->agp->mode = mode.mode; 153 agp_enable(dev->agp->agpdev, mode.mode); 154 dev->agp->enabled = 1; 155 return 0; 156 } 157 158 int drm_agp_enable_ioctl(struct drm_device *dev, void *data, 159 struct drm_file *file_priv) 160 { 161 struct drm_agp_mode mode; 162 163 mode = *(struct drm_agp_mode *) data; 164 165 return drm_agp_enable(dev, mode); 166 } 167 168 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request) 169 { 170 drm_agp_mem_t *entry; 171 void *handle; 172 unsigned long pages; 173 u_int32_t type; 174 struct agp_memory_info info; 175 176 if (!dev->agp || !dev->agp->acquired) 177 return -EINVAL; 178 179 entry = kmalloc(sizeof(*entry), M_DRM, M_WAITOK | M_NULLOK | M_ZERO); 180 if (entry == NULL) 181 return -ENOMEM; 182 183 pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE; 184 type = (u_int32_t) request->type; 185 186 handle = drm_agp_allocate_memory(pages, type); 187 if (handle == NULL) { 188 drm_free(entry, M_DRM); 189 return -ENOMEM; 190 } 191 192 entry->handle = handle; 193 entry->bound = 0; 194 entry->pages = pages; 195 entry->prev = NULL; 196 entry->next = dev->agp->memory; 197 if (dev->agp->memory) 198 dev->agp->memory->prev = entry; 199 dev->agp->memory = entry; 200 201 agp_memory_info(dev->agp->agpdev, entry->handle, &info); 202 203 request->handle = (unsigned long) entry->handle; 204 request->physical = info.ami_physical; 205 206 return 0; 207 } 208 209 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data, 210 struct drm_file *file_priv) 211 { 212 struct drm_agp_buffer request; 213 int retcode; 214 215 request = *(struct drm_agp_buffer *) data; 216 217 retcode = drm_agp_alloc(dev, &request); 218 219 *(struct drm_agp_buffer *) data = request; 220 221 return retcode; 222 } 223 224 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev, 225 void *handle) 226 { 227 drm_agp_mem_t *entry; 228 229 for (entry = dev->agp->memory; entry; entry = entry->next) { 230 if (entry->handle == handle) return entry; 231 } 232 return NULL; 233 } 234 235 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request) 236 { 237 drm_agp_mem_t *entry; 238 int retcode; 239 240 if (!dev->agp || !dev->agp->acquired) 241 return EINVAL; 242 243 entry = drm_agp_lookup_entry(dev, (void *)request->handle); 244 if (entry == NULL || !entry->bound) 245 return EINVAL; 246 247 retcode = drm_agp_unbind_memory(entry->handle); 248 249 if (retcode == 0) 250 entry->bound = 0; 251 252 return retcode; 253 } 254 255 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data, 256 struct drm_file *file_priv) 257 { 258 struct drm_agp_binding request; 259 int retcode; 260 261 request = *(struct drm_agp_binding *) data; 262 263 retcode = drm_agp_unbind(dev, &request); 264 265 return retcode; 266 } 267 268 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request) 269 { 270 drm_agp_mem_t *entry; 271 int retcode; 272 int page; 273 274 if (!dev->agp || !dev->agp->acquired) 275 return -EINVAL; 276 277 DRM_DEBUG("agp_bind, page_size=%x\n", (int)PAGE_SIZE); 278 279 entry = drm_agp_lookup_entry(dev, (void *)request->handle); 280 if (entry == NULL || entry->bound) 281 return -EINVAL; 282 283 page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE; 284 285 retcode = drm_agp_bind_memory(entry->handle, page); 286 if (retcode == 0) 287 entry->bound = dev->agp->base + (page << PAGE_SHIFT); 288 289 return retcode; 290 } 291 292 int drm_agp_bind_ioctl(struct drm_device *dev, void *data, 293 struct drm_file *file_priv) 294 { 295 struct drm_agp_binding request; 296 int retcode; 297 298 request = *(struct drm_agp_binding *) data; 299 300 retcode = drm_agp_bind(dev, &request); 301 302 return retcode; 303 } 304 305 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request) 306 { 307 drm_agp_mem_t *entry; 308 309 if (!dev->agp || !dev->agp->acquired) 310 return -EINVAL; 311 312 entry = drm_agp_lookup_entry(dev, (void*)request->handle); 313 if (entry == NULL) 314 return -EINVAL; 315 316 if (entry->prev) 317 entry->prev->next = entry->next; 318 else 319 dev->agp->memory = entry->next; 320 if (entry->next) 321 entry->next->prev = entry->prev; 322 323 if (entry->bound) 324 drm_agp_unbind_memory(entry->handle); 325 drm_agp_free_memory(entry->handle); 326 327 drm_free(entry, M_DRM); 328 329 return 0; 330 331 } 332 333 int drm_agp_free_ioctl(struct drm_device *dev, void *data, 334 struct drm_file *file_priv) 335 { 336 struct drm_agp_buffer request; 337 int retcode; 338 339 request = *(struct drm_agp_buffer *) data; 340 341 retcode = drm_agp_free(dev, &request); 342 343 return retcode; 344 } 345 346 struct drm_agp_head *drm_agp_init(void) 347 { 348 device_t agpdev; 349 struct drm_agp_head *head = NULL; 350 int agp_available = 1; 351 352 agpdev = DRM_AGP_FIND_DEVICE(); 353 if (!agpdev) 354 agp_available = 0; 355 356 DRM_DEBUG("agp_available = %d\n", agp_available); 357 358 if (agp_available) { 359 head = kmalloc(sizeof(*head), M_DRM, 360 M_WAITOK | M_NULLOK | M_ZERO); 361 if (head == NULL) 362 return NULL; 363 head->agpdev = agpdev; 364 agp_get_info(agpdev, &head->agp_info); 365 head->base = head->agp_info.ai_aperture_base; 366 head->memory = NULL; 367 DRM_INFO("AGP at 0x%08lx %dMB\n", 368 (long)head->agp_info.ai_aperture_base, 369 (int)(head->agp_info.ai_aperture_size >> 20)); 370 } 371 return head; 372 } 373 374 void *drm_agp_allocate_memory(size_t pages, u32 type) 375 { 376 device_t agpdev; 377 378 agpdev = DRM_AGP_FIND_DEVICE(); 379 if (!agpdev) 380 return NULL; 381 382 return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT); 383 } 384 385 int drm_agp_free_memory(void *handle) 386 { 387 device_t agpdev; 388 389 agpdev = DRM_AGP_FIND_DEVICE(); 390 if (!agpdev || !handle) 391 return 0; 392 393 agp_free_memory(agpdev, handle); 394 return 1; 395 } 396 397 int drm_agp_bind_memory(void *handle, off_t start) 398 { 399 device_t agpdev; 400 401 agpdev = DRM_AGP_FIND_DEVICE(); 402 if (!agpdev || !handle) 403 return -EINVAL; 404 405 return -agp_bind_memory(agpdev, handle, start * PAGE_SIZE); 406 } 407 408 int drm_agp_unbind_memory(void *handle) 409 { 410 device_t agpdev; 411 412 agpdev = DRM_AGP_FIND_DEVICE(); 413 if (!agpdev || !handle) 414 return -EINVAL; 415 416 return -agp_unbind_memory(agpdev, handle); 417 } 418