blob: b8c065ad3c90b60c426f8047f6a767937975e3e3 [file] [log] [blame]
Chia-I Wuf9911eb2014-08-06 13:50:31 +08001/*
2 * XGL
3 *
4 * Copyright (C) 2014 LunarG, Inc.
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 shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24
25#include "kmd/winsys.h"
26#include "dev.h"
27#include "mem.h"
28
29XGL_RESULT intel_mem_alloc(struct intel_dev *dev,
30 const XGL_MEMORY_ALLOC_INFO *info,
31 struct intel_mem **mem_ret)
32{
33 struct intel_mem *mem;
34
Courtney Goeltzenleuchter5975c1d2014-08-06 17:09:11 -060035 if ((info->alignment != 0) && (4096 % info->alignment))
Chia-I Wuf9911eb2014-08-06 13:50:31 +080036 return XGL_ERROR_INVALID_ALIGNMENT;
37 if (info->heapCount != 1 || info->heaps[0] != 0)
38 return XGL_ERROR_INVALID_POINTER;
39
40 mem = icd_alloc(sizeof(*mem), 0, XGL_SYSTEM_ALLOC_API_OBJECT);
41 if (!mem)
42 return XGL_ERROR_OUT_OF_MEMORY;
43
44 mem->base.dispatch = dev->base.dispatch;
45 if (dev->base.dbg) {
46 mem->base.dbg =
47 intel_base_dbg_create(XGL_DBG_OBJECT_GPU_MEMORY, NULL, 0);
48 if (!mem->base.dbg) {
49 intel_mem_free(mem);
50 return XGL_ERROR_OUT_OF_MEMORY;
51 }
52 }
53
54 mem->bo = intel_winsys_alloc_buffer(dev->winsys,
55 "xgl-gpu-memory", info->allocationSize, 0);
56 if (!mem->bo) {
57 intel_mem_free(mem);
58 return XGL_ERROR_UNKNOWN;
59 }
60
Courtney Goeltzenleuchterc35ab462014-08-06 17:10:04 -060061 *mem_ret = mem;
62
Chia-I Wuf9911eb2014-08-06 13:50:31 +080063 return XGL_SUCCESS;
64}
65
66void intel_mem_free(struct intel_mem *mem)
67{
68 if (mem->bo)
69 intel_bo_unreference(mem->bo);
70
71 if (mem->base.dbg)
72 intel_base_dbg_destroy(mem->base.dbg);
73
74 icd_free(mem);
75}
76
77XGL_RESULT intel_mem_set_priority(struct intel_mem *mem,
78 XGL_MEMORY_PRIORITY priority)
79{
80 /* pin the bo when XGL_MEMORY_PRIORITY_VERY_HIGH? */
81 return XGL_SUCCESS;
82}
83
84void *intel_mem_map(struct intel_mem *mem, XGL_FLAGS flags)
85{
86 return intel_bo_map_unsynchronized(mem->bo);
87}
88
89void intel_mem_unmap(struct intel_mem *mem)
90{
91 intel_bo_unmap(mem->bo);
92}
93
94XGL_RESULT XGLAPI intelAllocMemory(
95 XGL_DEVICE device,
96 const XGL_MEMORY_ALLOC_INFO* pAllocInfo,
97 XGL_GPU_MEMORY* pMem)
98{
99 struct intel_dev *dev = intel_dev(device);
100
101 return intel_mem_alloc(dev, pAllocInfo, (struct intel_mem **) pMem);
102}
103
104XGL_RESULT XGLAPI intelFreeMemory(
105 XGL_GPU_MEMORY mem_)
106{
107 struct intel_mem *mem = intel_mem(mem_);
108
109 intel_mem_free(mem);
110
111 return XGL_SUCCESS;
112}
113
114XGL_RESULT XGLAPI intelSetMemoryPriority(
115 XGL_GPU_MEMORY mem_,
116 XGL_MEMORY_PRIORITY priority)
117{
118 struct intel_mem *mem = intel_mem(mem_);
119
120 return intel_mem_set_priority(mem, priority);
121}
122
123XGL_RESULT XGLAPI intelMapMemory(
124 XGL_GPU_MEMORY mem_,
125 XGL_FLAGS flags,
126 XGL_VOID** ppData)
127{
128 struct intel_mem *mem = intel_mem(mem_);
129 void *ptr = intel_mem_map(mem, flags);
130
131 *ppData = ptr;
132
133 return (ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
134}
135
136XGL_RESULT XGLAPI intelUnmapMemory(
137 XGL_GPU_MEMORY mem_)
138{
139 struct intel_mem *mem = intel_mem(mem_);
140
141 intel_mem_unmap(mem);
142
143 return XGL_SUCCESS;
144}