Home | History | Annotate | Line # | Download | only in bsd-core
      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 (at) valinux.com>
     27  *    Gareth Hughes <gareth (at) 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 "drmP.h"
     37 
     38 #if defined(__FreeBSD__)
     39 #if __FreeBSD_version >= 800004
     40 #include <dev/agp/agpreg.h>
     41 #else /* __FreeBSD_version >= 800004 */
     42 #include <pci/agpreg.h>
     43 #endif /* __FreeBSD_version >= 800004 */
     44 #include <dev/pci/pcireg.h>
     45 #endif
     46 
     47 /* Returns 1 if AGP or 0 if not. */
     48 static int
     49 drm_device_find_capability(struct drm_device *dev, int cap)
     50 {
     51 #if defined(__FreeBSD__)
     52 #if __FreeBSD_version >= 602102
     53 
     54 	return (pci_find_extcap(dev->device, cap, NULL) == 0);
     55 #else
     56 	/* Code taken from agp.c.  IWBNI that was a public interface. */
     57 	u_int32_t status;
     58 	u_int8_t ptr, next;
     59 
     60 	/*
     61 	 * Check the CAP_LIST bit of the PCI status register first.
     62 	 */
     63 	status = pci_read_config(dev->device, PCIR_STATUS, 2);
     64 	if (!(status & 0x10))
     65 		return 0;
     66 
     67 	/*
     68 	 * Traverse the capabilities list.
     69 	 */
     70 	for (ptr = pci_read_config(dev->device, AGP_CAPPTR, 1);
     71 	     ptr != 0;
     72 	     ptr = next) {
     73 		u_int32_t capid = pci_read_config(dev->device, ptr, 4);
     74 		next = AGP_CAPID_GET_NEXT_PTR(capid);
     75 
     76 		/*
     77 		 * If this capability entry ID is cap, then we are done.
     78 		 */
     79 		if (AGP_CAPID_GET_CAP_ID(capid) == cap)
     80 			return 1;
     81 	}
     82 
     83 	return 0;
     84 #endif
     85 #elif   defined(__NetBSD__)
     86 	return pci_get_capability(dev->pa.pa_pc, dev->pa.pa_tag, cap, NULL,
     87 				  NULL);
     88 #endif
     89 }
     90 
     91 int drm_device_is_agp(struct drm_device *dev)
     92 {
     93 	if (dev->driver->device_is_agp != NULL) {
     94 		int ret;
     95 
     96 		/* device_is_agp returns a tristate, 0 = not AGP, 1 = definitely
     97 		 * AGP, 2 = fall back to PCI capability
     98 		 */
     99 		ret = (*dev->driver->device_is_agp)(dev);
    100 		if (ret != DRM_MIGHT_BE_AGP)
    101 			return ret;
    102 	}
    103 
    104 	return (drm_device_find_capability(dev, PCIY_AGP));
    105 }
    106 
    107 int drm_device_is_pcie(struct drm_device *dev)
    108 {
    109 	return (drm_device_find_capability(dev, PCIY_EXPRESS));
    110 }
    111 
    112 int drm_agp_info(struct drm_device * dev, struct drm_agp_info *info)
    113 {
    114 	struct agp_info *kern;
    115 
    116 	if (!dev->agp || !dev->agp->acquired)
    117 		return EINVAL;
    118 
    119 	kern                   = &dev->agp->info;
    120 	agp_get_info(dev->agp->agpdev, kern);
    121 	info->agp_version_major = 1;
    122 	info->agp_version_minor = 0;
    123 	info->mode              = kern->ai_mode;
    124 	info->aperture_base     = kern->ai_aperture_base;
    125 	info->aperture_size     = kern->ai_aperture_size;
    126 	info->memory_allowed    = kern->ai_memory_allowed;
    127 	info->memory_used       = kern->ai_memory_used;
    128 	info->id_vendor         = kern->ai_devid & 0xffff;
    129 	info->id_device         = kern->ai_devid >> 16;
    130 
    131 	return 0;
    132 }
    133 
    134 int drm_agp_info_ioctl(struct drm_device *dev, void *data,
    135 		       struct drm_file *file_priv)
    136 {
    137 	int err;
    138 	struct drm_agp_info info;
    139 
    140 	err = drm_agp_info(dev, &info);
    141 	if (err != 0)
    142 		return err;
    143 
    144 	*(struct drm_agp_info *) data = info;
    145 	return 0;
    146 }
    147 
    148 int drm_agp_acquire_ioctl(struct drm_device *dev, void *data,
    149 			  struct drm_file *file_priv)
    150 {
    151 
    152 	return drm_agp_acquire(dev);
    153 }
    154 
    155 int drm_agp_acquire(struct drm_device *dev)
    156 {
    157 	int retcode;
    158 
    159 	if (!dev->agp || dev->agp->acquired)
    160 		return EINVAL;
    161 
    162 	retcode = agp_acquire(dev->agp->agpdev);
    163 	if (retcode)
    164 		return retcode;
    165 
    166 	dev->agp->acquired = 1;
    167 	return 0;
    168 }
    169 
    170 int drm_agp_release_ioctl(struct drm_device *dev, void *data,
    171 			  struct drm_file *file_priv)
    172 {
    173 
    174 	return drm_agp_release(dev);
    175 }
    176 
    177 int drm_agp_release(struct drm_device * dev)
    178 {
    179 	if (!dev->agp || !dev->agp->acquired)
    180 		return EINVAL;
    181 	agp_release(dev->agp->agpdev);
    182 	dev->agp->acquired = 0;
    183 	return 0;
    184 }
    185 
    186 int drm_agp_enable(struct drm_device *dev, struct drm_agp_mode mode)
    187 {
    188 
    189 	if (!dev->agp || !dev->agp->acquired)
    190 		return EINVAL;
    191 
    192 	dev->agp->mode    = mode.mode;
    193 	agp_enable(dev->agp->agpdev, mode.mode);
    194 	dev->agp->enabled = 1;
    195 	return 0;
    196 }
    197 
    198 int drm_agp_enable_ioctl(struct drm_device *dev, void *data,
    199 			 struct drm_file *file_priv)
    200 {
    201 	struct drm_agp_mode mode;
    202 
    203 	mode = *(struct drm_agp_mode *) data;
    204 
    205 	return drm_agp_enable(dev, mode);
    206 }
    207 
    208 int drm_agp_alloc(struct drm_device *dev, struct drm_agp_buffer *request)
    209 {
    210 	drm_agp_mem_t    *entry;
    211 	void	         *handle;
    212 	unsigned long    pages;
    213 	u_int32_t	 type;
    214 	struct agp_memory_info info;
    215 
    216 	if (!dev->agp || !dev->agp->acquired)
    217 		return EINVAL;
    218 
    219 	entry = malloc(sizeof(*entry), DRM_MEM_AGPLISTS, M_NOWAIT | M_ZERO);
    220 	if (entry == NULL)
    221 		return ENOMEM;
    222 
    223 	pages = (request->size + PAGE_SIZE - 1) / PAGE_SIZE;
    224 	type = (u_int32_t) request->type;
    225 
    226 	DRM_UNLOCK();
    227 	handle = drm_agp_allocate_memory(pages, type);
    228 	DRM_LOCK();
    229 	if (handle == NULL) {
    230 		free(entry, DRM_MEM_AGPLISTS);
    231 		return ENOMEM;
    232 	}
    233 
    234 	entry->handle    = handle;
    235 	entry->bound     = 0;
    236 	entry->pages     = pages;
    237 	entry->prev      = NULL;
    238 	entry->next      = dev->agp->memory;
    239 	if (dev->agp->memory)
    240 		dev->agp->memory->prev = entry;
    241 	dev->agp->memory = entry;
    242 
    243 	agp_memory_info(dev->agp->agpdev, entry->handle, &info);
    244 
    245 	request->handle   = (unsigned long) entry->handle;
    246         request->physical = info.ami_physical;
    247 
    248 	return 0;
    249 }
    250 
    251 int drm_agp_alloc_ioctl(struct drm_device *dev, void *data,
    252 			struct drm_file *file_priv)
    253 {
    254 	struct drm_agp_buffer request;
    255 	int retcode;
    256 
    257 	request = *(struct drm_agp_buffer *) data;
    258 
    259 	DRM_LOCK();
    260 	retcode = drm_agp_alloc(dev, &request);
    261 	DRM_UNLOCK();
    262 
    263 	*(struct drm_agp_buffer *) data = request;
    264 
    265 	return retcode;
    266 }
    267 
    268 static drm_agp_mem_t * drm_agp_lookup_entry(struct drm_device *dev,
    269 					    void *handle)
    270 {
    271 	drm_agp_mem_t *entry;
    272 
    273 	for (entry = dev->agp->memory; entry; entry = entry->next) {
    274 		if (entry->handle == handle) return entry;
    275 	}
    276 	return NULL;
    277 }
    278 
    279 int drm_agp_unbind(struct drm_device *dev, struct drm_agp_binding *request)
    280 {
    281 	drm_agp_mem_t     *entry;
    282 	int retcode;
    283 
    284 	if (!dev->agp || !dev->agp->acquired)
    285 		return EINVAL;
    286 
    287 	entry = drm_agp_lookup_entry(dev, (void *)request->handle);
    288 	if (entry == NULL || !entry->bound)
    289 		return EINVAL;
    290 
    291 	DRM_UNLOCK();
    292 	retcode = drm_agp_unbind_memory(entry->handle);
    293 	DRM_LOCK();
    294 
    295 	if (retcode == 0)
    296 		entry->bound = 0;
    297 
    298 	return retcode;
    299 }
    300 
    301 int drm_agp_unbind_ioctl(struct drm_device *dev, void *data,
    302 			 struct drm_file *file_priv)
    303 {
    304 	struct drm_agp_binding request;
    305 	int retcode;
    306 
    307 	request = *(struct drm_agp_binding *) data;
    308 
    309 	DRM_LOCK();
    310 	retcode = drm_agp_unbind(dev, &request);
    311 	DRM_UNLOCK();
    312 
    313 	return retcode;
    314 }
    315 
    316 int drm_agp_bind(struct drm_device *dev, struct drm_agp_binding *request)
    317 {
    318 	drm_agp_mem_t     *entry;
    319 	int               retcode;
    320 	int               page;
    321 
    322 	if (!dev->agp || !dev->agp->acquired)
    323 		return EINVAL;
    324 
    325 	DRM_DEBUG("agp_bind, page_size=%x\n", PAGE_SIZE);
    326 
    327 	entry = drm_agp_lookup_entry(dev, (void *)request->handle);
    328 	if (entry == NULL || entry->bound)
    329 		return EINVAL;
    330 
    331 	page = (request->offset + PAGE_SIZE - 1) / PAGE_SIZE;
    332 
    333 	DRM_UNLOCK();
    334 	retcode = drm_agp_bind_memory(entry->handle, page);
    335 	DRM_LOCK();
    336 	if (retcode == 0)
    337 		entry->bound = dev->agp->base + (page << PAGE_SHIFT);
    338 
    339 	return retcode;
    340 }
    341 
    342 int drm_agp_bind_ioctl(struct drm_device *dev, void *data,
    343 		       struct drm_file *file_priv)
    344 {
    345 	struct drm_agp_binding request;
    346 	int retcode;
    347 
    348 	request = *(struct drm_agp_binding *) data;
    349 
    350 	DRM_LOCK();
    351 	retcode = drm_agp_bind(dev, &request);
    352 	DRM_UNLOCK();
    353 
    354 	return retcode;
    355 }
    356 
    357 int drm_agp_free(struct drm_device *dev, struct drm_agp_buffer *request)
    358 {
    359 	drm_agp_mem_t    *entry;
    360 
    361 	if (!dev->agp || !dev->agp->acquired)
    362 		return EINVAL;
    363 
    364 	entry = drm_agp_lookup_entry(dev, (void*)request->handle);
    365 	if (entry == NULL)
    366 		return EINVAL;
    367 
    368 	if (entry->prev)
    369 		entry->prev->next = entry->next;
    370 	else
    371 		dev->agp->memory  = entry->next;
    372 	if (entry->next)
    373 		entry->next->prev = entry->prev;
    374 
    375 	DRM_UNLOCK();
    376 	if (entry->bound)
    377 		drm_agp_unbind_memory(entry->handle);
    378 	drm_agp_free_memory(entry->handle);
    379 	DRM_LOCK();
    380 
    381 	free(entry, DRM_MEM_AGPLISTS);
    382 
    383 	return 0;
    384 
    385 }
    386 
    387 int drm_agp_free_ioctl(struct drm_device *dev, void *data,
    388 		       struct drm_file *file_priv)
    389 {
    390 	struct drm_agp_buffer request;
    391 	int retcode;
    392 
    393 	request = *(struct drm_agp_buffer *) data;
    394 
    395 	DRM_LOCK();
    396 	retcode = drm_agp_free(dev, &request);
    397 	DRM_UNLOCK();
    398 
    399 	return retcode;
    400 }
    401 
    402 drm_agp_head_t *
    403 drm_agp_init(struct drm_device *dev)
    404 {
    405 	device_t agpdev;
    406 	drm_agp_head_t *head   = NULL;
    407 	int      agp_available = 1;
    408 
    409 	agpdev = DRM_AGP_FIND_DEVICE();
    410 	if (!agpdev)
    411 		agp_available = 0;
    412 
    413 	DRM_DEBUG("agp_available = %d\n", agp_available);
    414 
    415 	if (agp_available) {
    416 		head = malloc(sizeof(*head), DRM_MEM_AGPLISTS,
    417 		    M_NOWAIT | M_ZERO);
    418 		if (head == NULL)
    419 			return NULL;
    420 		head->agpdev = agpdev;
    421 		agp_get_info(agpdev, &head->info);
    422 		head->base = head->info.ai_aperture_base;
    423 		head->memory = NULL;
    424 #if defined(__FreeBSD__)
    425 		DRM_INFO(
    426 #elif   defined(__NetBSD__)
    427 		aprint_normal_dev(dev->device,
    428 #endif
    429 			 "AGP at 0x%08lx %dMB\n",
    430 			 (long)head->info.ai_aperture_base,
    431 			 (int)(head->info.ai_aperture_size >> 20));
    432 	}
    433 	return head;
    434 }
    435 
    436 void *drm_agp_allocate_memory(size_t pages, u32 type)
    437 {
    438 	device_t agpdev;
    439 
    440 	agpdev = DRM_AGP_FIND_DEVICE();
    441 	if (!agpdev)
    442 		return NULL;
    443 
    444 	return agp_alloc_memory(agpdev, type, pages << AGP_PAGE_SHIFT);
    445 }
    446 
    447 int drm_agp_free_memory(void *handle)
    448 {
    449 	device_t agpdev;
    450 
    451 	agpdev = DRM_AGP_FIND_DEVICE();
    452 	if (!agpdev || !handle)
    453 		return 0;
    454 
    455 	agp_free_memory(agpdev, handle);
    456 	return 1;
    457 }
    458 
    459 int drm_agp_bind_memory(void *handle, off_t start)
    460 {
    461 	device_t agpdev;
    462 
    463 	agpdev = DRM_AGP_FIND_DEVICE();
    464 	if (!agpdev || !handle)
    465 		return EINVAL;
    466 
    467 	return agp_bind_memory(agpdev, handle, start * PAGE_SIZE);
    468 }
    469 
    470 int drm_agp_unbind_memory(void *handle)
    471 {
    472 	device_t agpdev;
    473 
    474 	agpdev = DRM_AGP_FIND_DEVICE();
    475 	if (!agpdev || !handle)
    476 		return EINVAL;
    477 
    478 	return agp_unbind_memory(agpdev, handle);
    479 }
    480