drm_memory.c revision 1.5
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
41void
42drm_mem_init(void)
43{
44}
45
46void
47drm_mem_uninit(void)
48{
49}
50
51void*
52drm_alloc(size_t size, int area)
53{
54	return malloc(size, M_DRM, M_NOWAIT);
55}
56
57void *
58drm_calloc(size_t nmemb, size_t size, int area)
59{
60	/* XXX overflow checking */
61	return malloc(size * nmemb, M_DRM, M_NOWAIT | M_ZERO);
62}
63
64void *
65drm_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
79void
80drm_free(void *pt, size_t size, int area)
81{
82	free(pt, M_DRM);
83}
84
85void *
86drm_ioremap(struct drm_device *dev, drm_local_map_t *map)
87{
88	struct vga_pci_bar *bar = NULL;
89	int i;
90
91	DRM_DEBUG("offset: 0x%x size: 0x%x type: %d\n", map->offset, map->size,
92	    map->type);
93
94	if (map->type == _DRM_AGP || map->type == _DRM_FRAME_BUFFER) {
95	/*
96	 * there can be multiple agp maps in the same BAR, agp also
97	 * quite possibly isn't the same as the vga device, just try
98	 * to map it.
99	 */
100		DRM_DEBUG("AGP map\n");
101		map->bst = dev->pa.pa_memt;
102		if (bus_space_map(map->bst, map->offset,
103		    map->size, BUS_SPACE_MAP_LINEAR, &map->bsh)) {
104			DRM_ERROR("ioremap fail\n");
105			return (NULL);
106		}
107		goto done;
108	} else {
109		for (i = 0 ; i < DRM_MAX_PCI_RESOURCE; ++i) {
110			bar = vga_pci_bar_info(dev->vga_softc, i);
111			if (bar == NULL)
112				continue;
113
114			if (bar->base == map->offset) {
115				DRM_DEBUG("REGISTERS map\n");
116				map->bsr = vga_pci_bar_map(dev->vga_softc,
117				    bar->addr, map->size, BUS_SPACE_MAP_LINEAR);
118				if (map->bsr == NULL) {
119					DRM_ERROR("ioremap fail\n");
120					return (NULL);
121				}
122				map->bst = map->bsr->bst;
123				map->bsh = map->bsr->bsh;
124				goto done;
125			}
126		}
127	}
128done:
129	/* handles are still supposed to be kernel virtual addresses */
130	return bus_space_vaddr(map->bst, map->bsh);
131}
132
133void
134drm_ioremapfree(drm_local_map_t *map)
135{
136	if (map != NULL && map->bsr != NULL)
137		vga_pci_bar_unmap(map->bsr);
138	else
139		bus_space_unmap(map->bst, map->bsh, map->size);
140}
141
142int
143drm_mtrr_add(unsigned long offset, size_t size, int flags)
144{
145#ifndef DRM_NO_MTRR
146	int act;
147	struct mem_range_desc mrdesc;
148
149	mrdesc.mr_base = offset;
150	mrdesc.mr_len = size;
151	mrdesc.mr_flags = flags;
152	act = MEMRANGE_SET_UPDATE;
153	strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
154	return mem_range_attr_set(&mrdesc, &act);
155#else
156	return 0;
157#endif
158}
159
160int
161drm_mtrr_del(int __unused handle, unsigned long offset, size_t size, int flags)
162{
163#ifndef DRM_NO_MTRR
164	int act;
165	struct mem_range_desc mrdesc;
166
167	mrdesc.mr_base = offset;
168	mrdesc.mr_len = size;
169	mrdesc.mr_flags = flags;
170	act = MEMRANGE_SET_REMOVE;
171	strlcpy(mrdesc.mr_owner, "drm", sizeof(mrdesc.mr_owner));
172	return mem_range_attr_set(&mrdesc, &act);
173#else
174	return 0;
175#endif
176}
177