xref: /openbsd-src/sys/dev/pci/drm/drm_memory.c (revision 897fc685943471cf985a0fe38ba076ea6fe74fa5)
1 /* $OpenBSD: drm_memory.c,v 1.27 2015/06/04 06:07:23 jsg Exp $ */
2 /*-
3  *Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
4  * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
5  * All Rights Reserved.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11  * and/or sell copies of the Software, and to permit persons to whom the
12  * Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
21  * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
22  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24  * OTHER DEALINGS IN THE SOFTWARE.
25  *
26  * Authors:
27  *    Rickard E. (Rik) Faith <faith@valinux.com>
28  *    Gareth Hughes <gareth@valinux.com>
29  *
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 #if !defined(__amd64__) && !defined(__i386__)
43 #define DRM_NO_MTRR	1
44 #endif
45 
46 void*
47 drm_alloc(size_t size)
48 {
49 	return (malloc(size, M_DRM, M_NOWAIT));
50 }
51 
52 void *
53 drm_calloc(size_t nmemb, size_t size)
54 {
55 	if (nmemb == 0 || SIZE_MAX / nmemb < size)
56 		return (NULL);
57 	else
58 		return mallocarray(size, nmemb, M_DRM, M_NOWAIT | M_ZERO);
59 }
60 
61 void *
62 drm_realloc(void *oldpt, size_t oldsize, size_t size)
63 {
64 	void *pt;
65 
66 	pt = malloc(size, M_DRM, M_NOWAIT);
67 	if (pt == NULL)
68 		return NULL;
69 	if (oldpt && oldsize) {
70 		memcpy(pt, oldpt, min(oldsize, size));
71 		free(oldpt, M_DRM, 0);
72 	}
73 	return pt;
74 }
75 
76 void
77 drm_free(void *pt)
78 {
79 	if (pt != NULL)
80 		free(pt, M_DRM, 0);
81 }
82 
83 int
84 drm_mtrr_add(unsigned long offset, size_t size, int flags)
85 {
86 #ifndef DRM_NO_MTRR
87 	int act;
88 	struct mem_range_desc mrdesc;
89 
90 	mrdesc.mr_base = offset;
91 	mrdesc.mr_len = size;
92 	mrdesc.mr_flags = flags;
93 	act = MEMRANGE_SET_UPDATE;
94 	strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
95 	return mem_range_attr_set(&mrdesc, &act);
96 #else
97 	return 0;
98 #endif
99 }
100 
101 int
102 drm_mtrr_del(int handle, unsigned long offset, size_t size, int flags)
103 {
104 #ifndef DRM_NO_MTRR
105 	int act;
106 	struct mem_range_desc mrdesc;
107 
108 	mrdesc.mr_base = offset;
109 	mrdesc.mr_len = size;
110 	mrdesc.mr_flags = flags;
111 	act = MEMRANGE_SET_REMOVE;
112 	strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
113 	return mem_range_attr_set(&mrdesc, &act);
114 #else
115 	return 0;
116 #endif
117 }
118