More copyrights I remember...
[nouveau] / src / nv_mem.c
1 /*
2  * Copyright 2007 Ben Skeggs
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * SOFTWARE.
21  */
22
23 #include "nv_include.h"
24
25 NVAllocRec *NVAllocateMemory(NVPtr pNv, int type, int size)
26 {
27         struct drm_nouveau_mem_alloc memalloc;
28         NVAllocRec *mem;
29
30         mem = malloc(sizeof(NVAllocRec));
31         if (!mem)
32                 return NULL;
33
34         memalloc.flags         = type | NOUVEAU_MEM_MAPPED;
35         memalloc.size          = size;
36         memalloc.alignment     = 0;
37         if (drmCommandWriteRead(pNv->drm_fd, DRM_NOUVEAU_MEM_ALLOC, &memalloc,
38                                 sizeof(memalloc))) {
39                 ErrorF("NOUVEAU_MEM_ALLOC failed.  "
40                         "flags=0x%08x, size=%lld (%d)\n",
41                         memalloc.flags, memalloc.size, errno);
42                 free(mem);
43                 return NULL;
44         }
45         mem->type   = memalloc.flags;
46         mem->size   = memalloc.size;
47         mem->offset = memalloc.offset;
48
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);
52                 mem->map  = NULL;
53                 NVFreeMemory(pNv, mem);
54                 return NULL;
55         }
56
57         return mem;
58 }
59
60 void NVFreeMemory(NVPtr pNv, NVAllocRec *mem)
61 {
62         struct drm_nouveau_mem_free memfree;
63
64         if (mem) {
65                 if (mem->map) {
66                         if (drmUnmap(mem->map, mem->size))
67                                 ErrorF("drmUnmap() failed.  "
68                                         "map=%p, size=%lld\n",
69                                         mem->map, mem->size);
70                 }
71
72                 memfree.flags = mem->type;
73                 memfree.offset = mem->offset;
74
75                 if (drmCommandWriteRead(pNv->drm_fd,
76                                         DRM_NOUVEAU_MEM_FREE, &memfree,
77                                         sizeof(memfree))) {
78                         ErrorF("NOUVEAU_MEM_FREE failed.  "
79                                 "flags=0x%08x, offset=0x%llx (%d)\n",
80                                 mem->type, mem->size, errno);
81                 }
82                 free(mem);
83         }
84 }
85