2 * Copyright 2007 Ben Skeggs
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "nv_include.h"
25 NVAllocRec *NVAllocateMemory(NVPtr pNv, int type, int size)
27 struct drm_nouveau_mem_alloc memalloc;
30 mem = malloc(sizeof(NVAllocRec));
34 memalloc.flags = type | NOUVEAU_MEM_MAPPED;
36 memalloc.alignment = 0;
37 if (drmCommandWriteRead(pNv->drm_fd, DRM_NOUVEAU_MEM_ALLOC, &memalloc,
39 ErrorF("NOUVEAU_MEM_ALLOC failed. "
40 "flags=0x%08x, size=%lld (%d)\n",
41 memalloc.flags, memalloc.size, errno);
45 mem->type = memalloc.flags;
46 mem->size = memalloc.size;
47 mem->offset = memalloc.offset;
49 if (drmMap(pNv->drm_fd, memalloc.map_handle, mem->size, &mem->map)) {
50 ErrorF("drmMap() failed. handle=0x%x, size=%lld (%d)\n",
51 memalloc.map_handle, mem->size, errno);
53 NVFreeMemory(pNv, mem);
60 void NVFreeMemory(NVPtr pNv, NVAllocRec *mem)
62 struct drm_nouveau_mem_free memfree;
66 if (drmUnmap(mem->map, mem->size))
67 ErrorF("drmUnmap() failed. "
68 "map=%p, size=%lld\n",
72 memfree.flags = mem->type;
73 memfree.offset = mem->offset;
75 if (drmCommandWriteRead(pNv->drm_fd,
76 DRM_NOUVEAU_MEM_FREE, &memfree,
78 ErrorF("NOUVEAU_MEM_FREE failed. "
79 "flags=0x%08x, offset=0x%llx (%d)\n",
80 mem->type, mem->size, errno);