summaryrefslogtreecommitdiff
AgeCommit message (Expand)Author
2007-11-15nouveau: be verbose about PPC bios for now.Stephane Marchesin
2007-11-15nouveau: revert the nv34 context size change, it was not the culprit after all.Stephane Marchesin
2007-11-15nouveau: use get_property instead of of_get_property on pre-2.6.22 kernels.Stephane Marchesin
2007-11-15Merge branch 'master' of git+ssh://git.freedesktop.org/git/mesa/drm into mode...Thomas Hellstrom
2007-11-15mm fixups.Thomas Hellstrom
2007-11-15libdrm: return crtc id to userspaceDave Airlie
2007-11-15libdrm: add crtc/output ids to userspace interfaceDave Airlie
2007-11-15drm: send correct fb id to userspaceDave Airlie
2007-11-15modes: pass type to userspace for preferred showingDave Airlie
2007-11-15drm: don't remove mappings added by the driverDave Airlie
2007-11-15intel: fixup || vs |Dave Airlie
2007-11-15tests: update for new drm interfaceDave Airlie
2007-11-15Merge branch 'master' into modesetting-101Dave Airlie
2007-11-15i915: remove excess debug outputDave Airlie
2007-11-15nouveau: Copy the PPC bios to RAMIN on init, that lets us do proper output de...Stephane Marchesin
2007-11-15intel: add flushing for i8xx chipsets.Dave Airlie
2007-11-14nouveau: nv30: missing ramin init, does it brake other hw?Patrice Mandin
2007-11-14Add new shared header file drm_internal.h.Kristian Høgsberg
2007-11-14nouveau: adjust the size of the NV34 context. That fixes mobile PPC cards.Stephane Marchesin
2007-11-13Fix ttmtest.Thomas Hellstrom
2007-11-14nouveau: Also wait until CACHE1 gets emptied.Ben Skeggs
2007-11-14Revert "nouveau: stub superioctl"Ben Skeggs
2007-11-14Merge branch 'fifo-cleanup' into upstream-masterBen Skeggs
2007-11-14nouveau: Attempt to wait for channel idle before we destroy it.Ben Skeggs
2007-11-14nouveau: Use "new" NV40 USER control regs.Ben Skeggs
2007-11-14nouveau: store user control reg offsets in channel structBen Skeggs
2007-11-14nouveau: funcs to determine active channel on PFIFO.Ben Skeggs
2007-11-14nouveau: stub superioctlBen Skeggs
2007-11-10Make sure PLLs are enabled before writing pipe configuration regsJesse Barnes
2007-11-09suspend() and resume() need kernel 2.6.22 or laterPatrice Mandin
2007-11-09drm: check edid data, so we deal well with broken driver.Jerome Glisse
2007-11-09drm: split edid handling in get_edid & add_edid_modeJerome Glisse
2007-11-09i915: cleanup pageflip derefs sarea even if no sarea existsDave Airlie
2007-11-07Merge branch 'master' of git+ssh://git.freedesktop.org/git/mesa/drm into mode...Thomas Hellstrom
2007-11-07Don't overwrite TTM page flags.Thomas Hellstrom
2007-11-07i915: oops disable TTM is backwardsDave Airlie
2007-11-06Merge branch 'master' of git+ssh://git.freedesktop.org/git/mesa/drm into mode...Thomas Hellstrom
2007-11-06Avoid buffers not ending up on a list in some cases.Thomas Hellstrom
2007-11-06Add missing drm_regman.c file.Thomas Hellstrom
2007-11-06i915: disable TTM on 8xx chips for now until flushing is solvedDave Airlie
2007-11-06i915: compat code doesn't work in i8xx hw.Dave Airlie
2007-11-06[PATCH] i915: fix missing G33 detect in IS_I9XXZhenyu Wang
2007-11-06drm/agp: kernel style fixesDave Airlie
2007-11-06i915: cleanup most of the whitespaceDave Airlie
2007-11-05Merge branch 'master' into modesetting-101Thomas Hellstrom
2007-11-05Export a symbol.Thomas Hellstrom
2007-11-05Fix a user-buffer check.Thomas Hellstrom
2007-11-05remove unused defineAlan Hourihane
2007-11-05remove duplicate and obsolete ioctl statementsAlan Hourihane
2007-11-05add missing lockAlan Hourihane
ef='#n349'>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
/**
 * \file drm_memory_debug.c
 * Memory management wrappers for DRM.
 *
 * \author Rickard E. (Rik) Faith <faith@valinux.com>
 * \author Gareth Hughes <gareth@valinux.com>
 */

/*
 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
 * 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, sublicense,
 * 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 above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * 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 NONINFRINGEMENT.  IN NO EVENT SHALL
 * VA LINUX SYSTEMS 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.
 */

#include <linux/config.h>
#include "drmP.h"

#ifdef DEBUG_MEMORY

typedef struct drm_mem_stats {
	const char *name;
	int succeed_count;
	int free_count;
	int fail_count;
	unsigned long bytes_allocated;
	unsigned long bytes_freed;
} drm_mem_stats_t;

static spinlock_t drm_mem_lock = SPIN_LOCK_UNLOCKED;
static unsigned long drm_ram_available = 0;	/* In pages */
static unsigned long drm_ram_used = 0;
static drm_mem_stats_t drm_mem_stats[] = {
	[DRM_MEM_DMA] = {"dmabufs"},
	[DRM_MEM_SAREA] = {"sareas"},
	[DRM_MEM_DRIVER] = {"driver"},
	[DRM_MEM_MAGIC] = {"magic"},
	[DRM_MEM_IOCTLS] = {"ioctltab"},
	[DRM_MEM_MAPS] = {"maplist"},
	[DRM_MEM_VMAS] = {"vmalist"},
	[DRM_MEM_BUFS] = {"buflist"},
	[DRM_MEM_SEGS] = {"seglist"},
	[DRM_MEM_PAGES] = {"pagelist"},
	[DRM_MEM_FILES] = {"files"},
	[DRM_MEM_QUEUES] = {"queues"},
	[DRM_MEM_CMDS] = {"commands"},
	[DRM_MEM_MAPPINGS] = {"mappings"},
	[DRM_MEM_BUFLISTS] = {"buflists"},
	[DRM_MEM_AGPLISTS] = {"agplist"},
	[DRM_MEM_SGLISTS] = {"sglist"},
	[DRM_MEM_TOTALAGP] = {"totalagp"},
	[DRM_MEM_BOUNDAGP] = {"boundagp"},
	[DRM_MEM_CTXBITMAP] = {"ctxbitmap"},
	[DRM_MEM_CTXLIST] = {"ctxlist"},
	[DRM_MEM_STUB] = {"stub"},
	{NULL, 0,}		/* Last entry must be null */
};

void drm_mem_init(void)
{
	drm_mem_stats_t *mem;
	struct sysinfo si;

	for (mem = drm_mem_stats; mem->name; ++mem) {
		mem->succeed_count = 0;
		mem->free_count = 0;
		mem->fail_count = 0;
		mem->bytes_allocated = 0;
		mem->bytes_freed = 0;
	}

	si_meminfo(&si);
	drm_ram_available = si.totalram;
	drm_ram_used = 0;
}

/* drm_mem_info is called whenever a process reads /dev/drm/mem. */

static int drm__mem_info(char *buf, char **start, off_t offset,
			 int request, int *eof, void *data)
{
	drm_mem_stats_t *pt;
	int len = 0;

	if (offset > DRM_PROC_LIMIT) {
		*eof = 1;
		return 0;
	}

	*eof = 0;
	*start = &buf[offset];

	DRM_PROC_PRINT("		  total counts			"
		       " |    outstanding  \n");
	DRM_PROC_PRINT("type	   alloc freed fail	bytes	   freed"
		       " | allocs      bytes\n\n");
	DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB         |\n",
		       "system", 0, 0, 0,
		       drm_ram_available << (PAGE_SHIFT - 10));
	DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu kB         |\n",
		       "locked", 0, 0, 0, drm_ram_used >> 10);
	DRM_PROC_PRINT("\n");
	for (pt = drm_mem_stats; pt->name; pt++) {
		DRM_PROC_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
			       pt->name,
			       pt->succeed_count,
			       pt->free_count,
			       pt->fail_count,
			       pt->bytes_allocated,
			       pt->bytes_freed,
			       pt->succeed_count - pt->free_count,
			       (long)pt->bytes_allocated
			       - (long)pt->bytes_freed);
	}

	if (len > request + offset)
		return request;
	*eof = 1;
	return len - offset;
}

int drm_mem_info(char *buf, char **start, off_t offset,
		 int len, int *eof, void *data)
{
	int ret;

	spin_lock(&drm_mem_lock);
	ret = drm__mem_info(buf, start, offset, len, eof, data);
	spin_unlock(&drm_mem_lock);
	return ret;
}

void *drm_alloc(size_t size, int area)
{
	void *pt;

	if (!size) {
		DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
		return NULL;
	}

	if (!(pt = kmalloc(size, GFP_KERNEL))) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[area].fail_count;
		spin_unlock(&drm_mem_lock);
		return NULL;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[area].succeed_count;
	drm_mem_stats[area].bytes_allocated += size;
	spin_unlock(&drm_mem_lock);
	return pt;
}
EXPORT_SYMBOL(drm_alloc);

void *drm_calloc(size_t nmemb, size_t size, int area)
{
	void *addr;

	addr = drm_alloc(nmemb * size, area);
	if (addr != NULL)
		memset((void *)addr, 0, size * nmemb);

	return addr;
}
EXPORT_SYMBOL(drm_calloc);

void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
{
	void *pt;

	if (!(pt = drm_alloc(size, area)))
		return NULL;
	if (oldpt && oldsize) {
		memcpy(pt, oldpt, oldsize);
		drm_free(oldpt, oldsize, area);
	}
	return pt;
}
EXPORT_SYMBOL(drm_realloc);

void drm_free(void *pt, size_t size, int area)
{
	int alloc_count;
	int free_count;

	if (!pt)
		DRM_MEM_ERROR(area, "Attempt to free NULL pointer\n");
	else
		kfree(pt);
	spin_lock(&drm_mem_lock);
	drm_mem_stats[area].bytes_freed += size;
	free_count = ++drm_mem_stats[area].free_count;
	alloc_count = drm_mem_stats[area].succeed_count;
	spin_unlock(&drm_mem_lock);
	if (free_count > alloc_count) {
		DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
			      free_count, alloc_count);
	}
}
EXPORT_SYMBOL(drm_free);

unsigned long drm_alloc_pages(int order, int area)
{
	unsigned long address;
	unsigned long bytes = PAGE_SIZE << order;
	unsigned long addr;
	unsigned int sz;

	spin_lock(&drm_mem_lock);
	if ((drm_ram_used >> PAGE_SHIFT)
	    > (DRM_RAM_PERCENT * drm_ram_available) / 100) {
		spin_unlock(&drm_mem_lock);
		return 0;
	}
	spin_unlock(&drm_mem_lock);

	address = __get_free_pages(GFP_KERNEL, order);
	if (!address) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[area].fail_count;
		spin_unlock(&drm_mem_lock);
		return 0;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[area].succeed_count;
	drm_mem_stats[area].bytes_allocated += bytes;
	drm_ram_used += bytes;
	spin_unlock(&drm_mem_lock);

	/* Zero outside the lock */
	memset((void *)address, 0, bytes);

	/* Reserve */
	for (addr = address, sz = bytes;
	     sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
		SetPageReserved(virt_to_page(addr));
	}

	return address;
}

void drm_free_pages(unsigned long address, int order, int area)
{
	unsigned long bytes = PAGE_SIZE << order;
	int alloc_count;
	int free_count;
	unsigned long addr;
	unsigned int sz;

	if (!address) {
		DRM_MEM_ERROR(area, "Attempt to free address 0\n");
	} else {
		/* Unreserve */
		for (addr = address, sz = bytes;
		     sz > 0; addr += PAGE_SIZE, sz -= PAGE_SIZE) {
			ClearPageReserved(virt_to_page(addr));
		}
		free_pages(address, order);
	}

	spin_lock(&drm_mem_lock);
	free_count = ++drm_mem_stats[area].free_count;
	alloc_count = drm_mem_stats[area].succeed_count;
	drm_mem_stats[area].bytes_freed += bytes;
	drm_ram_used -= bytes;
	spin_unlock(&drm_mem_lock);
	if (free_count > alloc_count) {
		DRM_MEM_ERROR(area,
			      "Excess frees: %d frees, %d allocs\n",
			      free_count, alloc_count);
	}
}

void *drm_ioremap(unsigned long offset, unsigned long size, drm_device_t * dev)
{
	void *pt;

	if (!size) {
		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
			      "Mapping 0 bytes at 0x%08lx\n", offset);
		return NULL;
	}

	if (!(pt = drm_ioremap(offset, size, dev))) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
		spin_unlock(&drm_mem_lock);
		return NULL;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
	drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
	spin_unlock(&drm_mem_lock);
	return pt;
}
EXPORT_SYMBOL(drm_ioremap);

void *drm_ioremap_nocache(unsigned long offset, unsigned long size,
			  drm_device_t * dev)
{
	void *pt;

	if (!size) {
		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
			      "Mapping 0 bytes at 0x%08lx\n", offset);
		return NULL;
	}

	if (!(pt = drm_ioremap_nocache(offset, size, dev))) {
		spin_lock(&drm_mem_lock);
		++drm_mem_stats[DRM_MEM_MAPPINGS].fail_count;
		spin_unlock(&drm_mem_lock);
		return NULL;
	}
	spin_lock(&drm_mem_lock);
	++drm_mem_stats[DRM_MEM_MAPPINGS].succeed_count;
	drm_mem_stats[DRM_MEM_MAPPINGS].bytes_allocated += size;
	spin_unlock(&drm_mem_lock);
	return pt;
}
EXPORT_SYMBOL(drm_ioremap_nocache);

void drm_ioremapfree(void *pt, unsigned long size, drm_device_t * dev)
{
	int alloc_count;
	int free_count;

	if (!pt)
		DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
			      "Attempt to free NULL pointer\n");
	else
		drm_ioremapfree(pt, size, dev);

	spin_lock(&drm_mem_lock);
	drm_mem_stats[DRM_MEM_MAPPINGS].bytes_freed += size;