blob: 7f1cc7f3339ff65cace4c8d2899cfa6ef20f97ec [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) {
Chia-I Wu1f8fc7c2014-08-07 11:09:11 +080046 mem->base.dbg =
47 intel_base_dbg_create(XGL_DBG_OBJECT_GPU_MEMORY, info, 0);
Chia-I Wuf9911eb2014-08-06 13:50:31 +080048 if (!mem->base.dbg) {
49 intel_mem_free(mem);
50 return XGL_ERROR_OUT_OF_MEMORY;
51 }
52 }
Chia-I Wu26f0bd02014-08-07 10:38:40 +080053 mem->base.get_info = intel_base_get_info;
Chia-I Wuf9911eb2014-08-06 13:50:31 +080054
55 mem->bo = intel_winsys_alloc_buffer(dev->winsys,
56 "xgl-gpu-memory", info->allocationSize, 0);
57 if (!mem->bo) {
58 intel_mem_free(mem);
59 return XGL_ERROR_UNKNOWN;
60 }
61
Courtney Goeltzenleuchterc35ab462014-08-06 17:10:04 -060062 *mem_ret = mem;
63
Chia-I Wuf9911eb2014-08-06 13:50:31 +080064 return XGL_SUCCESS;
65}
66
67void intel_mem_free(struct intel_mem *mem)
68{
69 if (mem->bo)
70 intel_bo_unreference(mem->bo);
71
72 if (mem->base.dbg)
73 intel_base_dbg_destroy(mem->base.dbg);
74
75 icd_free(mem);
76}
77
78XGL_RESULT intel_mem_set_priority(struct intel_mem *mem,
79 XGL_MEMORY_PRIORITY priority)
80{
81 /* pin the bo when XGL_MEMORY_PRIORITY_VERY_HIGH? */
82 return XGL_SUCCESS;
83}
84
85void *intel_mem_map(struct intel_mem *mem, XGL_FLAGS flags)
86{
87 return intel_bo_map_unsynchronized(mem->bo);
88}
89
90void intel_mem_unmap(struct intel_mem *mem)
91{
92 intel_bo_unmap(mem->bo);
93}
94
95XGL_RESULT XGLAPI intelAllocMemory(
96 XGL_DEVICE device,
97 const XGL_MEMORY_ALLOC_INFO* pAllocInfo,
98 XGL_GPU_MEMORY* pMem)
99{
100 struct intel_dev *dev = intel_dev(device);
101
102 return intel_mem_alloc(dev, pAllocInfo, (struct intel_mem **) pMem);
103}
104
105XGL_RESULT XGLAPI intelFreeMemory(
106 XGL_GPU_MEMORY mem_)
107{
108 struct intel_mem *mem = intel_mem(mem_);
109
110 intel_mem_free(mem);
111
112 return XGL_SUCCESS;
113}
114
115XGL_RESULT XGLAPI intelSetMemoryPriority(
116 XGL_GPU_MEMORY mem_,
117 XGL_MEMORY_PRIORITY priority)
118{
119 struct intel_mem *mem = intel_mem(mem_);
120
121 return intel_mem_set_priority(mem, priority);
122}
123
124XGL_RESULT XGLAPI intelMapMemory(
125 XGL_GPU_MEMORY mem_,
126 XGL_FLAGS flags,
127 XGL_VOID** ppData)
128{
129 struct intel_mem *mem = intel_mem(mem_);
130 void *ptr = intel_mem_map(mem, flags);
131
132 *ppData = ptr;
133
134 return (ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN;
135}
136
137XGL_RESULT XGLAPI intelUnmapMemory(
138 XGL_GPU_MEMORY mem_)
139{
140 struct intel_mem *mem = intel_mem(mem_);
141
142 intel_mem_unmap(mem);
143
144 return XGL_SUCCESS;
145}