The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/drm/drm_memory_debug.h

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* drm_memory.h -- Memory management wrappers for DRM -*- linux-c -*-
    2  * Created: Thu Feb  4 14:00:34 1999 by faith@valinux.com */
    3 /*-
    4  * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
    5  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
    6  * All Rights Reserved.
    7  *
    8  * Permission is hereby granted, free of charge, to any person obtaining a
    9  * copy of this software and associated documentation files (the "Software"),
   10  * to deal in the Software without restriction, including without limitation
   11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   12  * and/or sell copies of the Software, and to permit persons to whom the
   13  * Software is furnished to do so, subject to the following conditions:
   14  *
   15  * The above copyright notice and this permission notice (including the next
   16  * paragraph) shall be included in all copies or substantial portions of the
   17  * Software.
   18  *
   19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
   22  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
   23  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
   24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
   25  * OTHER DEALINGS IN THE SOFTWARE.
   26  *
   27  * Authors:
   28  *    Rickard E. (Rik) Faith <faith@valinux.com>
   29  *    Gareth Hughes <gareth@valinux.com>
   30  *
   31  * $FreeBSD$
   32  */
   33 
   34 #include "drmP.h"
   35 
   36 #define DRM_SYSCTL_PRINT(fmt, arg...)                           \
   37 do {                                                            \
   38         snprintf(buf, sizeof(buf), fmt, ##arg);                 \
   39         error = SYSCTL_OUT(req, buf, strlen(buf));              \
   40         if (error)                                              \
   41                 return error;                                   \
   42 } while (0)
   43 
   44 typedef struct drm_mem_stats {
   45         const char        *name;
   46         int               succeed_count;
   47         int               free_count;
   48         int               fail_count;
   49         unsigned long     bytes_allocated;
   50         unsigned long     bytes_freed;
   51 } drm_mem_stats_t;
   52 
   53 static DRM_SPINTYPE       DRM(mem_lock);
   54 static unsigned long      DRM(ram_available) = 0; /* In pages */
   55 static unsigned long      DRM(ram_used)      = 0;
   56 static drm_mem_stats_t    DRM(mem_stats)[]   = {
   57         [DRM_MEM_DMA]       = { "dmabufs"  },
   58         [DRM_MEM_SAREA]     = { "sareas"   },
   59         [DRM_MEM_DRIVER]    = { "driver"   },
   60         [DRM_MEM_MAGIC]     = { "magic"    },
   61         [DRM_MEM_IOCTLS]    = { "ioctltab" },
   62         [DRM_MEM_MAPS]      = { "maplist"  },
   63         [DRM_MEM_BUFS]      = { "buflist"  },
   64         [DRM_MEM_SEGS]      = { "seglist"  },
   65         [DRM_MEM_PAGES]     = { "pagelist" },
   66         [DRM_MEM_FILES]     = { "files"    },
   67         [DRM_MEM_QUEUES]    = { "queues"   },
   68         [DRM_MEM_CMDS]      = { "commands" },
   69         [DRM_MEM_MAPPINGS]  = { "mappings" },
   70         [DRM_MEM_BUFLISTS]  = { "buflists" },
   71         [DRM_MEM_AGPLISTS]  = { "agplist"  },
   72         [DRM_MEM_SGLISTS]   = { "sglist"   },
   73         [DRM_MEM_TOTALAGP]  = { "totalagp" },
   74         [DRM_MEM_BOUNDAGP]  = { "boundagp" },
   75         [DRM_MEM_CTXBITMAP] = { "ctxbitmap"},
   76         [DRM_MEM_STUB]      = { "stub"     },
   77         { NULL, 0, }            /* Last entry must be null */
   78 };
   79 
   80 void DRM(mem_init)(void)
   81 {
   82         drm_mem_stats_t *mem;
   83 
   84 #ifdef __NetBSD__
   85         malloc_type_attach(DRM(M_DRM));
   86 #endif
   87 
   88         DRM_SPININIT(DRM(mem_lock), "drm memory");
   89 
   90         for (mem = DRM(mem_stats); mem->name; ++mem) {
   91                 mem->succeed_count   = 0;
   92                 mem->free_count      = 0;
   93                 mem->fail_count      = 0;
   94                 mem->bytes_allocated = 0;
   95                 mem->bytes_freed     = 0;
   96         }
   97 
   98         DRM(ram_available) = 0; /* si.totalram */
   99         DRM(ram_used)      = 0;
  100 }
  101 
  102 void DRM(mem_uninit)(void)
  103 {
  104         DRM_SPINUNINIT(DRM(mem_lock));
  105 }
  106 
  107 #ifdef __FreeBSD__
  108 /* drm_mem_info is called whenever a process reads /dev/drm/mem. */
  109 static int
  110 DRM(_mem_info)(drm_mem_stats_t *stats, struct sysctl_oid *oidp, void *arg1, 
  111     int arg2, struct sysctl_req *req)
  112 {
  113         drm_mem_stats_t *pt;
  114         char buf[128];
  115         int error;
  116 
  117         DRM_SYSCTL_PRINT("                total counts                  "
  118                        " |    outstanding  \n");
  119         DRM_SYSCTL_PRINT("type     alloc freed fail     bytes      freed"
  120                        " | allocs      bytes\n\n");
  121         DRM_SYSCTL_PRINT("%-9.9s %5d %5d %4d %10lu          |\n",
  122                        "system", 0, 0, 0, DRM(ram_available));
  123         DRM_SYSCTL_PRINT("%-9.9s %5d %5d %4d %10lu          |\n",
  124                        "locked", 0, 0, 0, DRM(ram_used));
  125         DRM_SYSCTL_PRINT("\n");
  126         for (pt = stats; pt->name; pt++) {
  127                 DRM_SYSCTL_PRINT("%-9.9s %5d %5d %4d %10lu %10lu | %6d %10ld\n",
  128                                pt->name,
  129                                pt->succeed_count,
  130                                pt->free_count,
  131                                pt->fail_count,
  132                                pt->bytes_allocated,
  133                                pt->bytes_freed,
  134                                pt->succeed_count - pt->free_count,
  135                                (long)pt->bytes_allocated
  136                                - (long)pt->bytes_freed);
  137         }
  138         SYSCTL_OUT(req, "", 1);
  139         
  140         return 0;
  141 }
  142 
  143 int DRM(mem_info) DRM_SYSCTL_HANDLER_ARGS
  144 {
  145         int ret;
  146         drm_mem_stats_t *stats;
  147         
  148         stats = malloc(sizeof(DRM(mem_stats)), DRM(M_DRM), M_NOWAIT);
  149         if (stats == NULL)
  150                 return ENOMEM;
  151         
  152         DRM_SPINLOCK(&DRM(mem_lock));
  153         bcopy(DRM(mem_stats), stats, sizeof(DRM(mem_stats)));
  154         DRM_SPINUNLOCK(&DRM(mem_lock));
  155         
  156         ret = DRM(_mem_info)(stats, oidp, arg1, arg2, req);
  157         
  158         free(stats, DRM(M_DRM));
  159         return ret;
  160 }
  161 #endif /* __FreeBSD__ */
  162 
  163 void *DRM(alloc)(size_t size, int area)
  164 {
  165         void *pt;
  166 
  167         if (!size) {
  168                 DRM_MEM_ERROR(area, "Allocating 0 bytes\n");
  169                 return NULL;
  170         }
  171 
  172         if (!(pt = malloc(size, DRM(M_DRM), M_NOWAIT))) {
  173                 DRM_SPINLOCK(&DRM(mem_lock));
  174                 ++DRM(mem_stats)[area].fail_count;
  175                 DRM_SPINUNLOCK(&DRM(mem_lock));
  176                 return NULL;
  177         }
  178         DRM_SPINLOCK(&DRM(mem_lock));
  179         ++DRM(mem_stats)[area].succeed_count;
  180         DRM(mem_stats)[area].bytes_allocated += size;
  181         DRM_SPINUNLOCK(&DRM(mem_lock));
  182         return pt;
  183 }
  184 
  185 void *DRM(realloc)(void *oldpt, size_t oldsize, size_t size, int area)
  186 {
  187         void *pt;
  188 
  189         if (!(pt = DRM(alloc)(size, area))) return NULL;
  190         if (oldpt && oldsize) {
  191                 memcpy(pt, oldpt, oldsize);
  192                 DRM(free)(oldpt, oldsize, area);
  193         }
  194         return pt;
  195 }
  196 
  197 void DRM(free)(void *pt, size_t size, int area)
  198 {
  199         int alloc_count;
  200         int free_count;
  201 
  202         if (pt == NULL)
  203                 return;
  204         free(pt, DRM(M_DRM));
  205         DRM_SPINLOCK(&DRM(mem_lock));
  206         DRM(mem_stats)[area].bytes_freed += size;
  207         free_count  = ++DRM(mem_stats)[area].free_count;
  208         alloc_count =   DRM(mem_stats)[area].succeed_count;
  209         DRM_SPINUNLOCK(&DRM(mem_lock));
  210         if (free_count > alloc_count) {
  211                 DRM_MEM_ERROR(area, "Excess frees: %d frees, %d allocs\n",
  212                               free_count, alloc_count);
  213         }
  214 }
  215 
  216 void *DRM(ioremap)( drm_device_t *dev, drm_local_map_t *map )
  217 {
  218         void *pt;
  219 
  220         if (!map->size) {
  221                 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
  222                               "Mapping 0 bytes at 0x%08lx\n", map->offset);
  223                 return NULL;
  224         }
  225 #ifdef __NetBSD__
  226         map->iot = dev->pa.pa_memt;
  227 #endif
  228 
  229 #ifdef __FreeBSD__
  230         if (!(pt = pmap_mapdev(map->offset, map->size))) {
  231 #elif defined(__NetBSD__)
  232         if (bus_space_map(map->iot, map->offset, map->size, 
  233                 BUS_SPACE_MAP_LINEAR, &map->ioh)) {
  234 #endif
  235                 DRM_SPINLOCK(&DRM(mem_lock));
  236                 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
  237                 DRM_SPINUNLOCK(&DRM(mem_lock));
  238                 return NULL;
  239         }
  240 #ifdef __NetBSD__
  241         pt = bus_space_vaddr(map->iot, map->ioh);
  242 #endif
  243         DRM_SPINLOCK(&DRM(mem_lock));
  244         ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
  245         DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += map->size;
  246         DRM_SPINUNLOCK(&DRM(mem_lock));
  247         return pt;
  248 }
  249 
  250 /* unused so far */
  251 #if 0
  252 void *DRM(ioremap_nocache)(unsigned long offset, unsigned long size)
  253 {
  254         void *pt;
  255 
  256         if (!size) {
  257                 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
  258                               "Mapping 0 bytes at 0x%08lx\n", offset);
  259                 return NULL;
  260         }
  261 
  262         /* FIXME FOR BSD */
  263         if (!(pt = ioremap_nocache(offset, size))) {
  264                 DRM_SPINLOCK(&DRM(mem_lock));
  265                 ++DRM(mem_stats)[DRM_MEM_MAPPINGS].fail_count;
  266                 DRM_SPINUNLOCK(&DRM(mem_lock));
  267                 return NULL;
  268         }
  269         DRM_SPINLOCK(&DRM(mem_lock));
  270         ++DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
  271         DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_allocated += size;
  272         DRM_SPINUNLOCK(&DRM(mem_lock));
  273         return pt;
  274 }
  275 #endif
  276 
  277 void DRM(ioremapfree)(drm_local_map_t *map)
  278 {
  279         int alloc_count;
  280         int free_count;
  281 
  282         if (map->handle == NULL)
  283                 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
  284                               "Attempt to free NULL pointer\n");
  285         else
  286 #ifdef __FreeBSD__
  287                 pmap_unmapdev((vm_offset_t) map->handle, map->size);
  288 #elif defined(__NetBSD__)
  289                 bus_space_unmap(map->iot, map->ioh, map->size);
  290 #endif
  291 
  292         DRM_SPINLOCK(&DRM(mem_lock));
  293         DRM(mem_stats)[DRM_MEM_MAPPINGS].bytes_freed += map->size;
  294         free_count  = ++DRM(mem_stats)[DRM_MEM_MAPPINGS].free_count;
  295         alloc_count =   DRM(mem_stats)[DRM_MEM_MAPPINGS].succeed_count;
  296         DRM_SPINUNLOCK(&DRM(mem_lock));
  297         if (free_count > alloc_count) {
  298                 DRM_MEM_ERROR(DRM_MEM_MAPPINGS,
  299                               "Excess frees: %d frees, %d allocs\n",
  300                               free_count, alloc_count);
  301         }
  302 }
  303 
  304 #if __REALLY_HAVE_AGP
  305 agp_memory *DRM(alloc_agp)(int pages, u32 type)
  306 {
  307         agp_memory *handle;
  308 
  309         if (!pages) {
  310                 DRM_MEM_ERROR(DRM_MEM_TOTALAGP, "Allocating 0 pages\n");
  311                 return NULL;
  312         }
  313 
  314         if ((handle = DRM(agp_allocate_memory)(pages, type))) {
  315                 DRM_SPINLOCK(&DRM(mem_lock));
  316                 ++DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
  317                 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_allocated
  318                         += pages << PAGE_SHIFT;
  319                 DRM_SPINUNLOCK(&DRM(mem_lock));
  320                 return handle;
  321         }
  322         DRM_SPINLOCK(&DRM(mem_lock));
  323         ++DRM(mem_stats)[DRM_MEM_TOTALAGP].fail_count;
  324         DRM_SPINUNLOCK(&DRM(mem_lock));
  325         return NULL;
  326 }
  327 
  328 int DRM(free_agp)(agp_memory *handle, int pages)
  329 {
  330         int           alloc_count;
  331         int           free_count;
  332 
  333         if (!handle) {
  334                 DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
  335                               "Attempt to free NULL AGP handle\n");
  336                 return DRM_ERR(EINVAL);
  337         }
  338 
  339         if (DRM(agp_free_memory)(handle)) {
  340                 DRM_SPINLOCK(&DRM(mem_lock));
  341                 free_count  = ++DRM(mem_stats)[DRM_MEM_TOTALAGP].free_count;
  342                 alloc_count =   DRM(mem_stats)[DRM_MEM_TOTALAGP].succeed_count;
  343                 DRM(mem_stats)[DRM_MEM_TOTALAGP].bytes_freed
  344                         += pages << PAGE_SHIFT;
  345                 DRM_SPINUNLOCK(&DRM(mem_lock));
  346                 if (free_count > alloc_count) {
  347                         DRM_MEM_ERROR(DRM_MEM_TOTALAGP,
  348                                       "Excess frees: %d frees, %d allocs\n",
  349                                       free_count, alloc_count);
  350                 }
  351                 return 0;
  352         }
  353         return DRM_ERR(EINVAL);
  354 }
  355 
  356 int DRM(bind_agp)(agp_memory *handle, unsigned int start)
  357 {
  358         int retcode;
  359         device_t dev = DRM_AGP_FIND_DEVICE();
  360         struct agp_memory_info info;
  361 
  362         if (!dev)
  363                 return EINVAL;
  364 
  365         if (!handle) {
  366                 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
  367                               "Attempt to bind NULL AGP handle\n");
  368                 return DRM_ERR(EINVAL);
  369         }
  370 
  371         if (!(retcode = DRM(agp_bind_memory)(handle, start))) {
  372                 DRM_SPINLOCK(&DRM(mem_lock));
  373                 ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
  374                 agp_memory_info(dev, handle, &info);
  375                 DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_allocated
  376                         += info.ami_size;
  377                 DRM_SPINUNLOCK(&DRM(mem_lock));
  378                 return DRM_ERR(0);
  379         }
  380         DRM_SPINLOCK(&DRM(mem_lock));
  381         ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].fail_count;
  382         DRM_SPINUNLOCK(&DRM(mem_lock));
  383         return DRM_ERR(retcode);
  384 }
  385 
  386 int DRM(unbind_agp)(agp_memory *handle)
  387 {
  388         int alloc_count;
  389         int free_count;
  390         int retcode = EINVAL;
  391         device_t dev = DRM_AGP_FIND_DEVICE();
  392         struct agp_memory_info info;
  393 
  394         if (!dev)
  395                 return EINVAL;
  396 
  397         if (!handle) {
  398                 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
  399                               "Attempt to unbind NULL AGP handle\n");
  400                 return DRM_ERR(retcode);
  401         }
  402 
  403         agp_memory_info(dev, handle, &info);
  404 
  405         if ((retcode = DRM(agp_unbind_memory)(handle))) 
  406                 return DRM_ERR(retcode);
  407 
  408         DRM_SPINLOCK(&DRM(mem_lock));
  409         free_count  = ++DRM(mem_stats)[DRM_MEM_BOUNDAGP].free_count;
  410         alloc_count = DRM(mem_stats)[DRM_MEM_BOUNDAGP].succeed_count;
  411         DRM(mem_stats)[DRM_MEM_BOUNDAGP].bytes_freed
  412                 += info.ami_size;
  413         DRM_SPINUNLOCK(&DRM(mem_lock));
  414         if (free_count > alloc_count) {
  415                 DRM_MEM_ERROR(DRM_MEM_BOUNDAGP,
  416                               "Excess frees: %d frees, %d allocs\n",
  417                               free_count, alloc_count);
  418         }
  419         return DRM_ERR(retcode);
  420 }
  421 #endif

Cache object: fa2f440432cfe94fac310fb5d6a59126


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.