Sync with HEAD.
[dragonfly.git] / sys / dev / drm / drm_memory.c
blobd4c7adc97115d82480d9883944e068fa45a23d1c
1 /*-
2 *Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4 * All Rights Reserved.
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:
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.
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.
25 * Authors:
26 * Rickard E. (Rik) Faith <faith@valinux.com>
27 * Gareth Hughes <gareth@valinux.com>
29 * $DragonFly: src/sys/dev/drm/drm_memory.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
32 /** @file drm_memory.c
33 * Wrappers for kernel memory allocation routines, and MTRR management support.
35 * This file previously implemented a memory consumption tracking system using
36 * the "area" argument for various different types of allocations, but that
37 * has been stripped out for now.
40 #include "drmP.h"
42 MALLOC_DEFINE(M_DRM, "drm", "DRM Data Structures");
44 void drm_mem_init(void)
46 #if defined(__NetBSD__) || defined(__OpenBSD__)
47 malloc_type_attach(M_DRM);
48 #endif
51 void drm_mem_uninit(void)
55 void *drm_alloc(size_t size, int area)
57 return malloc(size, M_DRM, M_NOWAIT);
60 void *drm_calloc(size_t nmemb, size_t size, int area)
62 return malloc(size * nmemb, M_DRM, M_NOWAIT | M_ZERO);
65 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
67 void *pt;
69 pt = malloc(size, M_DRM, M_NOWAIT);
70 if (pt == NULL)
71 return NULL;
72 if (oldpt && oldsize) {
73 memcpy(pt, oldpt, oldsize);
74 free(oldpt, M_DRM);
76 return pt;
79 void drm_free(void *pt, size_t size, int area)
81 free(pt, M_DRM);
84 void *drm_ioremap(drm_device_t *dev, drm_local_map_t *map)
86 #if defined(__FreeBSD__) || defined(__DragonFly__)
87 return pmap_mapdev(map->offset, map->size);
88 #elif defined(__NetBSD__) || defined(__OpenBSD__)
89 map->bst = dev->pa.pa_memt;
90 if (bus_space_map(map->bst, map->offset, map->size,
91 BUS_SPACE_MAP_LINEAR, &map->bsh))
92 return NULL;
93 return bus_space_vaddr(map->bst, map->bsh);
94 #endif
97 void drm_ioremapfree(drm_local_map_t *map)
99 #if defined(__FreeBSD__) || defined(__DragonFly__)
100 pmap_unmapdev((vm_offset_t) map->handle, map->size);
101 #elif defined(__NetBSD__) || defined(__OpenBSD__)
102 bus_space_unmap(map->bst, map->bsh, map->size);
103 #endif
106 #if defined(__FreeBSD__) || defined(__DragonFly__)
108 drm_mtrr_add(unsigned long offset, size_t size, int flags)
110 int act;
111 struct mem_range_desc mrdesc;
113 mrdesc.mr_base = offset;
114 mrdesc.mr_len = size;
115 mrdesc.mr_flags = flags;
116 act = MEMRANGE_SET_UPDATE;
117 strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
118 return mem_range_attr_set(&mrdesc, &act);
122 drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags)
124 int act;
125 struct mem_range_desc mrdesc;
127 mrdesc.mr_base = offset;
128 mrdesc.mr_len = size;
129 mrdesc.mr_flags = flags;
130 act = MEMRANGE_SET_REMOVE;
131 strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
132 return mem_range_attr_set(&mrdesc, &act);
134 #elif defined(__NetBSD__) || defined(__OpenBSD__)
136 drm_mtrr_add(unsigned long offset, size_t size, int flags)
138 struct mtrr mtrrmap;
139 int one = 1;
141 mtrrmap.base = offset;
142 mtrrmap.len = size;
143 mtrrmap.type = flags;
144 mtrrmap.flags = MTRR_VALID;
145 return mtrr_set(&mtrrmap, &one, NULL, MTRR_GETSET_KERNEL);
149 drm_mtrr_del(unsigned long offset, size_t size, int flags)
151 struct mtrr mtrrmap;
152 int one = 1;
154 mtrrmap.base = offset;
155 mtrrmap.len = size;
156 mtrrmap.type = flags;
157 mtrrmap.flags = 0;
158 return mtrr_set(&mtrrmap, &one, NULL, MTRR_GETSET_KERNEL);
160 #endif