summaryrefslogtreecommitdiff
path: root/radeon/radeon_bo.h
AgeCommit message (Collapse)Author
2010-02-18radeon: add square-tiling flagMarek Olšák
2010-01-14radeon: indentation & trailing space cleanupJerome Glisse
2009-12-21radeon: straighten out the API insanity.Dave Airlie
as Michel pointed out we are exposing too much info for these object for this to be maintainable going forward. This patch set minimises the exposed parts of the radeon_bo and radeon_cs objects to the piece necessary for ddx/mesa to operate at a decent speed. The major problem is mesa contains a legacy BO/CS managers which we still need to expose functionality to, and we really cannot change the API until we can drop the non-KMS codepaths. Signed-off-by: Dave Airlie <airlied@redhat.com>
2009-11-20Merge remote branch 'origin/master' into libdrmKristian Høgsberg
2009-11-17Move libdrm/ up one levelKristian Høgsberg
> 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
/**************************************************************************
 * 
 * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND., USA
 * All Rights Reserved.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 * 
 * 
 **************************************************************************/

#include "drmP.h"

static void drm_ttm_ipi_handler(void *null)
{
	flush_agp_cache();
}

static void drm_ttm_cache_flush(void) 
{
	if (on_each_cpu(drm_ttm_ipi_handler, NULL, 1, 1) != 0)
		DRM_ERROR("Timed out waiting for drm cache flush.\n");
}


/*
 * Use kmalloc if possible. Otherwise fall back to vmalloc.
 */

static void ttm_alloc_pages(drm_ttm_t *ttm)
{
	unsigned long size = ttm->num_pages * sizeof(*ttm->pages);
	ttm->pages = NULL;

	if (drm_alloc_memctl(size))
		return;

	if (size <= PAGE_SIZE) {
		ttm->pages = drm_calloc(1, size, DRM_MEM_TTM);
	}
	if (!ttm->pages) {
		ttm->pages = vmalloc_user(size);
		if (ttm->pages)
			ttm->page_flags |= DRM_TTM_PAGE_VMALLOC;
	}
	if (!ttm->pages) {
		drm_free_memctl(size);
	}
}

static void ttm_free_pages(drm_ttm_t *ttm)
{
	unsigned long size = ttm->num_pages * sizeof(*ttm->pages);

	if (ttm->page_flags & DRM_TTM_PAGE_VMALLOC) {
		vfree(ttm->pages);
		ttm->page_flags &= ~DRM_TTM_PAGE_VMALLOC;
	} else {
		drm_free(ttm->pages, size, DRM_MEM_TTM);
	}
	drm_free_memctl(size);
	ttm->pages = NULL;
}

/*
 * Unmap all vma pages from vmas mapping this ttm.
 */

static int unmap_vma_pages(drm_ttm_t * ttm)
{
	drm_device_t *dev = ttm->dev;
	loff_t offset = ((loff_t) ttm->mapping_offset) << PAGE_SHIFT;
	loff_t holelen = ((loff_t) ttm->num_pages) << PAGE_SHIFT;

#ifdef DRM_ODD_MM_COMPAT
	int ret;
	ret = drm_ttm_lock_mm(ttm);
	if (ret)
		return ret;
#endif
	unmap_mapping_range(dev->dev_mapping, offset, holelen, 1);
#ifdef DRM_ODD_MM_COMPAT
	drm_ttm_finish_unmap(ttm);
#endif
	return 0;
}

/*
 * Change caching policy for the linear kernel map 
 * for range of pages in a ttm.
 */

static int drm_set_caching(drm_ttm_t * ttm, int noncached)
{
	int i;
	struct page **cur_page;
	int do_tlbflush = 0;

	if ((ttm->page_flags & DRM_TTM_PAGE_UNCACHED) == noncached)
		return 0;

	if (noncached) 
		drm_ttm_cache_flush();

	for (i = 0; i < ttm->num_pages; ++i) {
		cur_page = ttm->pages + i;
		if (*cur_page) {
			if (!PageHighMem(*cur_page)) {
				if (noncached) {
					map_page_into_agp(*cur_page);
				} else {
					unmap_page_from_agp(*cur_page);
				}
				do_tlbflush = 1;
			}
		}
	}
	if (do_tlbflush)
		flush_agp_mappings();

	DRM_MASK_VAL(ttm->page_flags, DRM_TTM_PAGE_UNCACHED, noncached);

	return 0;
}

/*
 * Free all resources associated with a ttm.
 */

int drm_destroy_ttm(drm_ttm_t * ttm)
{

	int i;
	struct page **cur_page;
	drm_ttm_backend_t *be;

	if (!ttm)
		return 0;

	if (atomic_read(&ttm->vma_count) > 0) {
		ttm->destroy = 1;
		DRM_ERROR("VMAs are still alive. Skipping destruction.\n");
		return -EBUSY;
	}

	DRM_DEBUG("Destroying a ttm\n");

#ifdef DRM_TTM_ODD_COMPAT
	BUG_ON(!list_empty(&ttm->vma_list));
	BUG_ON(!list_empty(&ttm->p_mm_list));
#endif
	be = ttm->be;
	if (be) {
		be->destroy(be);
		ttm->be = NULL;
	}

	if (ttm->pages) {
		drm_buffer_manager_t *bm = &ttm->dev->bm;
		if (ttm->page_flags & DRM_TTM_PAGE_UNCACHED)
			drm_set_caching(ttm, 0);

		for (i = 0; i < ttm->num_pages; ++i) {
			cur_page = ttm->pages + i;
			if (*cur_page) {
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
				unlock_page(*cur_page);
#else
				ClearPageReserved(*cur_page);
#endif
				if (page_count(*cur_page) != 1) {
					DRM_ERROR("Erroneous page count. "
						  "Leaking pages.\n");
				}
				if (page_mapped(*cur_page)) {
					DRM_ERROR("Erroneous map count. "
						  "Leaking page mappings.\n");
				}

				/*
				 * End debugging.
				 */

				__free_page(*cur_page);
				drm_free_memctl(PAGE_SIZE);
				--bm->cur_pages;
			}
		}
		ttm_free_pages(ttm);
	}

	drm_ctl_free(ttm, sizeof(*ttm), DRM_MEM_TTM);
	return 0;
}

static int drm_ttm_populate(drm_ttm_t * ttm)
{
	struct page *page;
	unsigned long i;
	drm_buffer_manager_t *bm;
	drm_ttm_backend_t *be;

	if (ttm->state != ttm_unpopulated)
		return 0;

	bm = &ttm->dev->bm;
	be = ttm->be;
	for (i = 0; i < ttm->num_pages; ++i) {
		page = ttm->pages[i];
		if (!page) {
			if (drm_alloc_memctl(PAGE_SIZE)) {
				return -ENOMEM;
			}
			page = alloc_page(GFP_KERNEL | __GFP_ZERO | GFP_DMA32);
			if (!page) {
				drm_free_memctl(PAGE_SIZE);
				return -ENOMEM;
			}
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,15))
			SetPageLocked(page);
#else
			SetPageReserved(page);
#endif
			ttm->pages[i] = page;
			++bm->cur_pages;
		}
	}
	be->populate(be, ttm->num_pages, ttm->pages);
	ttm->state = ttm_unbound;
	return 0;
}

/*
 * Initialize a ttm.
 */

static drm_ttm_t *drm_init_ttm(struct drm_device *dev, unsigned long size)
{
	drm_bo_driver_t *bo_driver = dev->driver->bo_driver;
	drm_ttm_t *ttm;

	if (!bo_driver)
		return NULL;

	ttm = drm_ctl_calloc(1, sizeof(*ttm), DRM_MEM_TTM);
	if (!ttm)
		return NULL;

#ifdef DRM_ODD_MM_COMPAT
	INIT_LIST_HEAD(&ttm->p_mm_list);
	INIT_LIST_HEAD(&ttm->vma_list);
#endif

	ttm->dev = dev;
	atomic_set(&ttm->vma_count, 0);

	ttm->destroy = 0;
	ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;

	ttm->page_flags = 0;

	/*
	 * Account also for AGP module memory usage.
	 */

	ttm_alloc_pages(ttm);
	if (!ttm->pages) {
		drm_destroy_ttm(ttm);
		DRM_ERROR("Failed allocating page table\n");
		return NULL;
	}
	ttm->be = bo_driver->create_ttm_backend_entry(dev);
	if (!ttm->be) {
		drm_destroy_ttm(ttm);
		DRM_ERROR("Failed creating ttm backend entry\n");
		return NULL;
	}
	ttm->state = ttm_unpopulated;
	return ttm;
}

/*
 * Unbind a ttm region from the aperture.
 */

int drm_evict_ttm(drm_ttm_t * ttm)
{
	drm_ttm_backend_t *be = ttm->be;
	int ret;

	switch (ttm->state) {
	case ttm_bound:
		if (be->needs_ub_cache_adjust(be)) {
			ret = unmap_vma_pages(ttm);
			if (ret) {
				return ret;
			}
		}
		be->unbind(be);
		break;
	default:
		break;
	}
	ttm->state = ttm_evicted;
	return 0;
}

void drm_fixup_ttm_caching(drm_ttm_t * ttm)
{

	if (ttm->state == ttm_evicted) {
		drm_ttm_backend_t *be = ttm->be;
		if (be->needs_ub_cache_adjust(be)) {
			drm_set_caching(ttm, 0);
		}
		ttm->state = ttm_unbound;
	}
}

int drm_unbind_ttm(drm_ttm_t * ttm)
{
	int ret = 0;

	if (ttm->state == ttm_bound)
		ret = drm_evict_ttm(ttm);

	if (ret)
		return ret;

	drm_fixup_ttm_caching(ttm);
	return 0;
}

int drm_bind_ttm(drm_ttm_t * ttm, int cached, unsigned long aper_offset)
{

	int ret = 0;
	drm_ttm_backend_t *be;

	if (!ttm)
		return -EINVAL;
	if (ttm->state == ttm_bound)
		return 0;

	be = ttm->be;

	ret = drm_ttm_populate(ttm);
	if (ret)
		return ret;
	if (ttm->state == ttm_unbound && !cached) {
		ret = unmap_vma_pages(ttm);
		if (ret)
			return ret;

		drm_set_caching(ttm, DRM_TTM_PAGE_UNCACHED);
	}
#ifdef DRM_ODD_MM_COMPAT
	else if (ttm->state == ttm_evicted && !cached) {
		ret = drm_ttm_lock_mm(ttm);
		if (ret)
			return ret;
	}
#endif
	if ((ret = be->bind(be, aper_offset, cached))) {
		ttm->state = ttm_evicted;
#ifdef DRM_ODD_MM_COMPAT
		if (be->needs_ub_cache_adjust(be))
			drm_ttm_unlock_mm(ttm);
#endif
		DRM_ERROR("Couldn't bind backend.\n");
		return ret;
	}

	ttm->aper_offset = aper_offset;
	ttm->state = ttm_bound;

#ifdef DRM_ODD_MM_COMPAT
	if (be->needs_ub_cache_adjust(be)) {
		ret = drm_ttm_remap_bound(ttm);
		if (ret)
			return ret;
	}
#endif

	return 0;
}

/*
 * dev->struct_mutex locked.
 */
static void drm_ttm_object_remove(drm_device_t * dev, drm_ttm_object_t * object)
{
	drm_map_list_t *list = &object->map_list;
	drm_local_map_t *map;

	if (list->user_token)
		drm_ht_remove_item(&dev->map_hash, &list->hash);

	if (list->file_offset_node) {
		drm_mm_put_block(list->file_offset_node);
		list->file_offset_node = NULL;
	}

	map = list->map;

	if (map) {
		drm_ttm_t *ttm = (drm_ttm_t *) map->offset;
		if (ttm) {
			if (drm_destroy_ttm(ttm) != -EBUSY) {
				drm_ctl_free(map, sizeof(*map), DRM_MEM_TTM);
			}
		} else {
			drm_ctl_free(map, sizeof(*map), DRM_MEM_TTM);
		}
	}

	drm_ctl_free(object, sizeof(*object), DRM_MEM_TTM);
}

void drm_ttm_object_deref_locked(drm_device_t * dev, drm_ttm_object_t * to)
{
	if (atomic_dec_and_test(&to->usage)) {
		drm_ttm_object_remove(dev, to);
	}
}

void drm_ttm_object_deref_unlocked(drm_device_t * dev, drm_ttm_object_t * to)
{
	if (atomic_dec_and_test(&to->usage)) {
		mutex_lock(&dev->struct_mutex);
		if (atomic_read(&to->usage) == 0)
			drm_ttm_object_remove(dev, to);
		mutex_unlock(&dev->struct_mutex);
	}
}

/*
 * Create a ttm and add it to the drm book-keeping. 
 * dev->struct_mutex locked.
 */

int drm_ttm_object_create(drm_device_t * dev, unsigned long size,
			  uint32_t flags, drm_ttm_object_t ** ttm_object)
{
	drm_ttm_object_t *object;
	drm_map_list_t *list;
	drm_local_map_t *map;
	drm_ttm_t *ttm;

	object = drm_ctl_calloc(1, sizeof(*object), DRM_MEM_TTM);
	if (!object)
		return -ENOMEM;
	object->flags = flags;
	list = &object->map_list;

	list->map = drm_ctl_calloc(1, sizeof(*map), DRM_MEM_TTM);
	if (!list->map) {
		drm_ttm_object_remove(dev, object);
		return -ENOMEM;
	}
	map = list->map;

	ttm = drm_init_ttm(dev, size);
	if (!ttm) {
		DRM_ERROR("Could not create ttm\n");
		drm_ttm_object_remove(dev, object);
		return -ENOMEM;
	}

	map->offset = (unsigned long)ttm;
	map->type = _DRM_TTM;
	map->flags = _DRM_REMOVABLE;
	map->size = ttm->num_pages * PAGE_SIZE;
	map->handle = (void *)object;

	/*
	 * Add a one-page "hole" to the block size to avoid the mm subsystem
	 * merging vmas.
	 * FIXME: Is this really needed?
	 */

	list->file_offset_node = drm_mm_search_free(&dev->offset_manager,
						    ttm->num_pages + 1, 0, 0);
	if (!list->file_offset_node) {
		drm_ttm_object_remove(dev, object);
		return -ENOMEM;
	}
	list->file_offset_node = drm_mm_get_block(list->file_offset_node,
						  ttm->num_pages + 1, 0);

	list->hash.key = list->file_offset_node->start;

	if (drm_ht_insert_item(&dev->map_hash, &list->hash)) {
		drm_ttm_object_remove(dev, object);
		return -ENOMEM;
	}

	list->user_token = ((drm_u64_t) list->hash.key) << PAGE_SHIFT;
	ttm->mapping_offset = list->hash.key;
	atomic_set(&object->usage, 1);
	*ttm_object = object;
	return 0;
}