Add additional quirks from ddx
[platform/upstream/libdrm.git] / bsd-core / 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  */
30
31 /** @file drm_memory.c
32  * Wrappers for kernel memory allocation routines, and MTRR management support.
33  *
34  * This file previously implemented a memory consumption tracking system using
35  * the "area" argument for various different types of allocations, but that
36  * has been stripped out for now.
37  */
38
39 #include "drmP.h"
40
41 MALLOC_DEFINE(M_DRM, "drm", "DRM Data Structures");
42
43 void drm_mem_init(void)
44 {
45 #if defined(__NetBSD__) || defined(__OpenBSD__)
46         malloc_type_attach(M_DRM);
47 #endif
48 }
49
50 void drm_mem_uninit(void)
51 {
52 }
53
54 void *drm_alloc(size_t size, int area)
55 {
56         return malloc(size, M_DRM, M_NOWAIT);
57 }
58
59 void *drm_calloc(size_t nmemb, size_t size, int area)
60 {
61         return malloc(size * nmemb, M_DRM, M_NOWAIT | M_ZERO);
62 }
63
64 void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
65 {
66         void *pt;
67
68         pt = malloc(size, M_DRM, M_NOWAIT);
69         if (pt == NULL)
70                 return NULL;
71         if (oldpt && oldsize) {
72                 memcpy(pt, oldpt, oldsize);
73                 free(oldpt, M_DRM);
74         }
75         return pt;
76 }
77
78 void drm_free(void *pt, size_t size, int area)
79 {
80         free(pt, M_DRM);
81 }
82
83 void *drm_ioremap(struct drm_device *dev, drm_local_map_t *map)
84 {
85 #ifdef __FreeBSD__
86         return pmap_mapdev(map->offset, map->size);
87 #elif defined(__NetBSD__) || defined(__OpenBSD__)
88         map->bst = dev->pa.pa_memt;
89         if (bus_space_map(map->bst, map->offset, map->size, 
90             BUS_SPACE_MAP_LINEAR, &map->bsh))
91                 return NULL;
92         return bus_space_vaddr(map->bst, map->bsh);
93 #endif
94 }
95
96 void drm_ioremapfree(drm_local_map_t *map)
97 {
98 #ifdef __FreeBSD__
99         pmap_unmapdev((vm_offset_t) map->handle, map->size);
100 #elif defined(__NetBSD__) || defined(__OpenBSD__)
101         bus_space_unmap(map->bst, map->bsh, map->size);
102 #endif
103 }
104
105 #ifdef __FreeBSD__
106 int
107 drm_mtrr_add(unsigned long offset, size_t size, int flags)
108 {
109         int act;
110         struct mem_range_desc mrdesc;
111
112         mrdesc.mr_base = offset;
113         mrdesc.mr_len = size;
114         mrdesc.mr_flags = flags;
115         act = MEMRANGE_SET_UPDATE;
116         strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
117         return mem_range_attr_set(&mrdesc, &act);
118 }
119
120 int
121 drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags)
122 {
123         int act;
124         struct mem_range_desc mrdesc;
125
126         mrdesc.mr_base = offset;
127         mrdesc.mr_len = size;
128         mrdesc.mr_flags = flags;
129         act = MEMRANGE_SET_REMOVE;
130         strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
131         return mem_range_attr_set(&mrdesc, &act);
132 }
133 #elif defined(__NetBSD__) || defined(__OpenBSD__)
134 int
135 drm_mtrr_add(unsigned long offset, size_t size, int flags)
136 {
137         struct mtrr mtrrmap;
138         int one = 1;
139
140         mtrrmap.base = offset;
141         mtrrmap.len = size;
142         mtrrmap.type = flags;
143         mtrrmap.flags = MTRR_VALID;
144         return mtrr_set(&mtrrmap, &one, NULL, MTRR_GETSET_KERNEL);
145 }
146
147 int
148 drm_mtrr_del(unsigned long offset, size_t size, int flags)
149 {
150         struct mtrr mtrrmap;
151         int one = 1;
152
153         mtrrmap.base = offset;
154         mtrrmap.len = size;
155         mtrrmap.type = flags;
156         mtrrmap.flags = 0;
157         return mtrr_set(&mtrrmap, &one, NULL, MTRR_GETSET_KERNEL);
158 }
159 #endif