Fix pciid detect on non-8800 cards (they identify as NV8x, not NV5x)
[nouveau] / src / nv_mem.c
1 #include "nv_include.h"
2
3 NVAllocRec *NVAllocateMemory(NVPtr pNv, int type, int size)
4 {
5         struct drm_nouveau_mem_alloc memalloc;
6         NVAllocRec *mem;
7
8         mem = malloc(sizeof(NVAllocRec));
9         if (!mem)
10                 return NULL;
11
12         memalloc.flags         = type | NOUVEAU_MEM_MAPPED;
13         memalloc.size          = size;
14         memalloc.alignment     = 0;
15         if (drmCommandWriteRead(pNv->drm_fd, DRM_NOUVEAU_MEM_ALLOC, &memalloc,
16                                 sizeof(memalloc))) {
17                 ErrorF("NOUVEAU_MEM_ALLOC failed.  "
18                         "flags=0x%08x, size=%lld (%d)\n",
19                         memalloc.flags, memalloc.size, errno);
20                 free(mem);
21                 return NULL;
22         }
23         mem->type   = memalloc.flags;
24         mem->size   = memalloc.size;
25         mem->offset = memalloc.offset;
26
27         if (drmMap(pNv->drm_fd, memalloc.map_handle, mem->size, &mem->map)) {
28                 ErrorF("drmMap() failed. handle=0x%x, size=%lld (%d)\n",
29                                 memalloc.map_handle, mem->size, errno);
30                 mem->map  = NULL;
31                 NVFreeMemory(pNv, mem);
32                 return NULL;
33         }
34
35         return mem;
36 }
37
38 void NVFreeMemory(NVPtr pNv, NVAllocRec *mem)
39 {
40         struct drm_nouveau_mem_free memfree;
41
42         if (mem) {
43                 if (mem->map) {
44                         if (drmUnmap(mem->map, mem->size))
45                                 ErrorF("drmUnmap() failed.  "
46                                         "map=%p, size=%lld\n",
47                                         mem->map, mem->size);
48                 }
49
50                 memfree.flags = mem->type;
51                 memfree.offset = mem->offset;
52
53                 if (drmCommandWriteRead(pNv->drm_fd,
54                                         DRM_NOUVEAU_MEM_FREE, &memfree,
55                                         sizeof(memfree))) {
56                         ErrorF("NOUVEAU_MEM_FREE failed.  "
57                                 "flags=0x%08x, offset=0x%llx (%d)\n",
58                                 mem->type, mem->size, errno);
59                 }
60                 free(mem);
61         }
62 }
63