drm_memory.c revision 1.1
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#ifndef __OpenBSD__
42MALLOC_DEFINE(M_DRM, "drm", "DRM Data Structures");
43#endif
44
45void drm_mem_init(void)
46{
47#if defined(__NetBSD__)
48/*
49	malloc_type_attach(M_DRM);
50*/
51#endif
52}
53
54void drm_mem_uninit(void)
55{
56}
57
58void *drm_alloc(size_t size, int area)
59{
60	return malloc(size, M_DRM, M_NOWAIT);
61}
62
63void *drm_calloc(size_t nmemb, size_t size, int area)
64{
65	/* XXX overflow checking */
66	return malloc(size * nmemb, M_DRM, M_NOWAIT | M_ZERO);
67}
68
69void *drm_realloc(void *oldpt, size_t oldsize, size_t size, int area)
70{
71	void *pt;
72
73	pt = malloc(size, M_DRM, M_NOWAIT);
74	if (pt == NULL)
75		return NULL;
76	if (oldpt && oldsize) {
77		memcpy(pt, oldpt, oldsize);
78		free(oldpt, M_DRM);
79	}
80	return pt;
81}
82
83void drm_free(void *pt, size_t size, int area)
84{
85	free(pt, M_DRM);
86}
87
88void *drm_ioremap(drm_device_t *dev, drm_local_map_t *map)
89{
90#ifdef __FreeBSD__
91	return pmap_mapdev(map->offset, map->size);
92#elif defined(__NetBSD__) || defined(__OpenBSD__)
93	int ret;
94	if (!map->bst)
95	map->bst = dev->pa.pa_memt;
96	if ((ret = bus_space_map(map->bst, map->offset, map->size,
97	    BUS_SPACE_MAP_LINEAR, &map->bsh))) {
98		DRM_ERROR("Failed to map offset = 0x%08lx, size = 0x%08lx, type = %d, ret = %d\n", map->offset,
99		    map->size, map->type, ret);
100		return NULL;
101	}
102	DRM_INFO("mapped offset = 0x%08lx, size = 0x%08lx, type = %d\n", map->offset,
103		    map->offset, map->size);
104	return bus_space_vaddr(map->bst, map->bsh);
105#endif
106}
107
108void drm_ioremapfree(drm_local_map_t *map)
109{
110#ifdef __FreeBSD__
111	pmap_unmapdev((vm_offset_t) map->handle, map->size);
112#elif defined(__NetBSD__) || defined(__OpenBSD__)
113	bus_space_unmap(map->bst, map->bsh, map->size);
114#endif
115}
116
117#if defined(__FreeBSD__) || defined(__OpenBSD__)
118int
119drm_mtrr_add(unsigned long offset, size_t size, int flags)
120{
121#ifndef DRM_NO_MTRR
122	int act;
123	struct mem_range_desc mrdesc;
124
125	mrdesc.mr_base = offset;
126	mrdesc.mr_len = size;
127	mrdesc.mr_flags = flags;
128	act = MEMRANGE_SET_UPDATE;
129	strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
130	return mem_range_attr_set(&mrdesc, &act);
131#else
132	return 0;
133#endif
134}
135
136int
137#if defined(__OpenBSD__)
138drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags)
139#else
140drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags)
141#endif
142{
143#ifndef DRM_NO_MTRR
144	int act;
145	struct mem_range_desc mrdesc;
146
147	mrdesc.mr_base = offset;
148	mrdesc.mr_len = size;
149	mrdesc.mr_flags = flags;
150	act = MEMRANGE_SET_REMOVE;
151	strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
152	return mem_range_attr_set(&mrdesc, &act);
153#else
154	return 0;
155#endif
156}
157#elif defined(__NetBSD__)
158int
159drm_mtrr_add(unsigned long offset, size_t size, int flags)
160{
161#ifndef DRM_NO_MTRR
162	struct mtrr mtrrmap;
163	int one = 1;
164
165	DRM_DEBUG("offset=%lx size=%ld\n", (long)offset, (long)size);
166	mtrrmap.base = offset;
167	mtrrmap.len = size;
168	mtrrmap.type = flags;
169	mtrrmap.flags = MTRR_VALID;
170	return mtrr_set(&mtrrmap, &one, NULL, MTRR_GETSET_KERNEL);
171#else
172	return 0;
173#endif
174}
175
176int
177drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags)
178{
179#ifndef DRM_NO_MTRR
180	struct mtrr mtrrmap;
181	int one = 1;
182
183	DRM_DEBUG("offset=%lx size=%ld\n", (long)offset, (long)size);
184	mtrrmap.base = offset;
185	mtrrmap.len = size;
186	mtrrmap.type = flags;
187	mtrrmap.flags = 0;
188	return mtrr_set(&mtrrmap, &one, NULL, MTRR_GETSET_KERNEL);
189#else
190	return 0;
191#endif
192}
193#endif
194