summaryrefslogtreecommitdiff
path: root/src/nv_mem.c
blob: 44e3d39f57e10cdf78c05500a45b5042d9e80b56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "nv_include.h"

NVAllocRec *NVAllocateMemory(NVPtr pNv, int type, int size)
{
	drm_nouveau_mem_alloc_t memalloc;
	NVAllocRec *mem;

	mem = malloc(sizeof(NVAllocRec));
	if (!mem)
		return NULL;

	memalloc.flags         = type | NOUVEAU_MEM_MAPPED;
	memalloc.size          = size;
	memalloc.alignment     = 0;
	if (drmCommandWriteRead(pNv->drm_fd, DRM_NOUVEAU_MEM_ALLOC, &memalloc,
				sizeof(memalloc))) {
		ErrorF("NOUVEAU_MEM_ALLOC failed.  "
			"flags=0x%08x, size=%lld (%d)\n",
			mem->type, mem->size, errno);
		free(mem);
		return NULL;
	}
	mem->type   = memalloc.flags;
	mem->size   = memalloc.size;
	mem->offset = memalloc.offset;

	if (drmMap(pNv->drm_fd, memalloc.map_handle, mem->size, &mem->map)) {
		ErrorF("drmMap() failed. handle=0x%llx, size=%lld (%d)\n",
				memalloc.map_handle, mem->size, errno);
		mem->map  = NULL;
		NVFreeMemory(pNv, mem);
		return NULL;
	}

	return mem;
}

void NVFreeMemory(NVPtr pNv, NVAllocRec *mem)
{
	drm_nouveau_mem_free_t memfree;

	if (mem) {
		if (mem->map) {
			if (drmUnmap(mem->map, mem->size))
				ErrorF("drmUnmap() failed.  "
					"map=%p, size=%lld\n",
					mem->map, mem->size);
		}

		memfree.flags = mem->type;
		memfree.offset = mem->offset;

		if (drmCommandWriteRead(pNv->drm_fd,
					DRM_NOUVEAU_MEM_FREE, &memfree,
					sizeof(memfree))) {
			ErrorF("NOUVEAU_MEM_FREE failed.  "
				"flags=0x%08x, offset=0x%llx (%d)\n",
				mem->type, mem->size, errno);
		}
		free(mem);
	}
}