/* drm_memory.h -- Memory management wrappers for DRM -*- linux-c -*- * Created: Thu Feb 4 14:00:34 1999 by faith@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. * * Authors: * Rickard E. (Rik) Faith * Gareth Hughes */ #define __NO_VERSION__ #include #include "drmP.h" #include 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_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; } 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; } char *DRM(strdup)(const char *s, int area) { char *pt; int length = s ? strlen(s) : 0; if (!(pt = DRM(alloc)(length+1, area))) return NULL; strcpy(pt, s); return pt; } void DRM(strfree)(const char *s, int area) { unsigned int size; if (!s) return; size = 1 + (s ? strlen(s) : 0); DRM(free)((void *)s, size, area); } 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); } } 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) { mem_map_reserve(virt_to_page(addr)); } return address; } void DRM(free_pages)(unsigned long address, int order, int area) { unsigned long byte/* r128_drv.c -- ATI Rage 128 driver -*- linux-c -*- * Created: Mon Dec 13 09:47:27 1999 by faith@precisioninsight.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. * * Authors: * Rickard E. (Rik) Faith <faith@valinux.com> * Gareth Hughes <gareth@valinux.com> */ #include <linux/config.h> #include "drmP.h" #include "drm.h" #include "r128_drm.h" #include "r128_drv.h" #include "drm_pciids.h" static int postinit(struct drm_device *dev, unsigned long flags) { DRM_INFO("Initialized %s %d.%d.%d %s on minor %d: %s\n", DRIVER_NAME, DRIVER_MAJOR, DRIVER_MINOR, DRIVER_PATCHLEVEL, DRIVER_DATE, dev->primary.minor, pci_pretty_name(dev->pdev) ); return 0; } static int version(drm_version_t * version) { int len; version->version_major = DRIVER_MAJOR; version->version_minor = DRIVER_MINOR; version->version_patchlevel = DRIVER_PATCHLEVEL; DRM_COPY(version->name, DRIVER_NAME); DRM_COPY(version->date, DRIVER_DATE); DRM_COPY(version->desc, DRIVER_DESC); return 0; } static struct pci_device_id pciidlist[] = { r128_PCI_IDS }; extern drm_ioctl_desc_t r128_ioctls[]; extern int r128_max_ioctl; static int probe(struct pci_dev *pdev, const struct pci_device_id *ent); static struct drm_driver driver = { .driver_features = DRIVER_USE_AGP | DRIVER_USE_MTRR | DRIVER_PCI_DMA | DRIVER_SG | DRIVER_HAVE_DMA | DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_IRQ_VBL, .dev_priv_size = sizeof(drm_r128_buf_priv_t), .prerelease = r128_driver_prerelease, .pretakedown = r128_driver_pretakedown, .vblank_wait = r128_driver_vblank_wait, .irq_preinstall = r128_driver_irq_preinstall, .irq_postinstall = r128_driver_irq_postinstall, .irq_uninstall = r128_driver_irq_uninstall, .irq_handler = r128_driver_irq_handler, .reclaim_buffers = drm_core_reclaim_buffers, .get_map_ofs = drm_core_get_map_ofs, .get_reg_ofs = drm_core_get_reg_ofs, .postinit = postinit, .version = version, .ioctls = r128_ioctls, .dma_ioctl = r128_cce_buffers, .fops = { .owner = THIS_MODULE, .open = drm_open, .release = drm_release, .ioctl = drm_ioctl, .mmap = drm_mmap, .poll = drm_poll, .fasync = drm_fasync, }, .pci_driver = { .name = DRIVER_NAME, .id_table = pciidlist, .probe = probe, .remove = __devexit_p(drm_cleanup_pci), } }; static int probe(struct pci_dev *pdev, const struct pci_device_id *ent) { return drm_get_dev(pdev, ent, &driver); } static int __init r128_init(void) { driver.num_ioctls = r128_max_ioctl; return drm_init(&driver, pciidlist); } static void __exit r128_exit(void) { drm_exit(&driver); } module_init(r128_init); module_exit(r128_exit); MODULE_AUTHOR(DRIVER_AUTHOR); MODULE_DESCRIPTION(DRIVER_DESC); MODULE_LICENSE("GPL and additional rights");