Merge branch 'vendor/OPENSSL'
[dragonfly.git] / sys / dev / drm / drm_memory.c
1 /*-
2  *Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
3  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
4  * All Rights Reserved.
5  *
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:
12  *
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.
16  *
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.
24  *
25  * Authors:
26  *    Rickard E. (Rik) Faith <faith@valinux.com>
27  *    Gareth Hughes <gareth@valinux.com>
28  *
29  * $DragonFly: src/sys/dev/drm/drm_memory.c,v 1.1 2008/04/05 18:12:29 hasso Exp $
30  */
31
32 /** @file drm_memory.c
33  * Wrappers for kernel memory allocation routines, and MTRR management support.
34  *
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.
38  */
39
40 #include "drmP.h"
41
42 MALLOC_DEFINE(M_DRM, "drm", "DRM Data Structures");
43
44 void drm_mem_init(void)
45 {
46 #if defined(__NetBSD__) || defined(__OpenBSD__)
47         malloc_type_attach(M_DRM);
48 #endif
49 }
50
51 void drm_mem_uninit(void)
52 {
53 }
54
55 void *drm_alloc(size_t size, int area)
56 {
57         return malloc(size, M_DRM, M_NOWAIT);
58 }
59
60 void *drm_calloc(size_t nmemb, size_t size, int area)
61 {
62         return malloc(size * nmemb, M_DRM, M_NOWAIT | M_ZERO);
63 }
64
65 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
66 {
67         void *pt;
68
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);
75         }
76         return pt;
77 }
78
79 void drm_free(void *pt, size_t size, int area)
80 {
81         free(pt, M_DRM);
82 }
83
84 void *drm_ioremap(drm_device_t *dev, drm_local_map_t *map)
85 {
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
95 }
96
97 void drm_ioremapfree(drm_local_map_t *map)
98 {
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
104 }
105
106 #if defined(__FreeBSD__) || defined(__DragonFly__)
107 int
108 drm_mtrr_add(unsigned long offset, size_t size, int flags)
109 {
110         int act;
111         struct mem_range_desc mrdesc;
112
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);
119 }
120
121 int
122 drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags)
123 {
124         int act;
125         struct mem_range_desc mrdesc;
126
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);
133 }
134 #elif defined(__NetBSD__) || defined(__OpenBSD__)
135 int
136 drm_mtrr_add(unsigned long offset, size_t size, int flags)
137 {
138         struct mtrr mtrrmap;
139         int one = 1;
140
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);
146 }
147
148 int
149 drm_mtrr_del(unsigned long offset, size_t size, int flags)
150 {
151         struct mtrr mtrrmap;
152         int one = 1;
153
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);
159 }
160 #endif