Chia-I Wu | f9911eb | 2014-08-06 13:50:31 +0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 29 | XGL_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 Goeltzenleuchter | 5975c1d | 2014-08-06 17:09:11 -0600 | [diff] [blame] | 35 | if ((info->alignment != 0) && (4096 % info->alignment)) |
Chia-I Wu | f9911eb | 2014-08-06 13:50:31 +0800 | [diff] [blame] | 36 | return XGL_ERROR_INVALID_ALIGNMENT; |
| 37 | if (info->heapCount != 1 || info->heaps[0] != 0) |
| 38 | return XGL_ERROR_INVALID_POINTER; |
| 39 | |
Chia-I Wu | bbf2c93 | 2014-08-07 12:20:08 +0800 | [diff] [blame^] | 40 | mem = (struct intel_mem *) intel_base_create(sizeof(*mem), |
| 41 | dev->base.dbg, XGL_DBG_OBJECT_GPU_MEMORY, info, 0); |
Chia-I Wu | f9911eb | 2014-08-06 13:50:31 +0800 | [diff] [blame] | 42 | if (!mem) |
| 43 | return XGL_ERROR_OUT_OF_MEMORY; |
| 44 | |
Chia-I Wu | f9911eb | 2014-08-06 13:50:31 +0800 | [diff] [blame] | 45 | mem->bo = intel_winsys_alloc_buffer(dev->winsys, |
| 46 | "xgl-gpu-memory", info->allocationSize, 0); |
| 47 | if (!mem->bo) { |
| 48 | intel_mem_free(mem); |
| 49 | return XGL_ERROR_UNKNOWN; |
| 50 | } |
| 51 | |
Courtney Goeltzenleuchter | c35ab46 | 2014-08-06 17:10:04 -0600 | [diff] [blame] | 52 | *mem_ret = mem; |
| 53 | |
Chia-I Wu | f9911eb | 2014-08-06 13:50:31 +0800 | [diff] [blame] | 54 | return XGL_SUCCESS; |
| 55 | } |
| 56 | |
| 57 | void intel_mem_free(struct intel_mem *mem) |
| 58 | { |
| 59 | if (mem->bo) |
| 60 | intel_bo_unreference(mem->bo); |
| 61 | |
Chia-I Wu | bbf2c93 | 2014-08-07 12:20:08 +0800 | [diff] [blame^] | 62 | intel_base_destroy(&mem->base); |
Chia-I Wu | f9911eb | 2014-08-06 13:50:31 +0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | XGL_RESULT intel_mem_set_priority(struct intel_mem *mem, |
| 66 | XGL_MEMORY_PRIORITY priority) |
| 67 | { |
| 68 | /* pin the bo when XGL_MEMORY_PRIORITY_VERY_HIGH? */ |
| 69 | return XGL_SUCCESS; |
| 70 | } |
| 71 | |
| 72 | void *intel_mem_map(struct intel_mem *mem, XGL_FLAGS flags) |
| 73 | { |
| 74 | return intel_bo_map_unsynchronized(mem->bo); |
| 75 | } |
| 76 | |
| 77 | void intel_mem_unmap(struct intel_mem *mem) |
| 78 | { |
| 79 | intel_bo_unmap(mem->bo); |
| 80 | } |
| 81 | |
| 82 | XGL_RESULT XGLAPI intelAllocMemory( |
| 83 | XGL_DEVICE device, |
| 84 | const XGL_MEMORY_ALLOC_INFO* pAllocInfo, |
| 85 | XGL_GPU_MEMORY* pMem) |
| 86 | { |
| 87 | struct intel_dev *dev = intel_dev(device); |
| 88 | |
| 89 | return intel_mem_alloc(dev, pAllocInfo, (struct intel_mem **) pMem); |
| 90 | } |
| 91 | |
| 92 | XGL_RESULT XGLAPI intelFreeMemory( |
| 93 | XGL_GPU_MEMORY mem_) |
| 94 | { |
| 95 | struct intel_mem *mem = intel_mem(mem_); |
| 96 | |
| 97 | intel_mem_free(mem); |
| 98 | |
| 99 | return XGL_SUCCESS; |
| 100 | } |
| 101 | |
| 102 | XGL_RESULT XGLAPI intelSetMemoryPriority( |
| 103 | XGL_GPU_MEMORY mem_, |
| 104 | XGL_MEMORY_PRIORITY priority) |
| 105 | { |
| 106 | struct intel_mem *mem = intel_mem(mem_); |
| 107 | |
| 108 | return intel_mem_set_priority(mem, priority); |
| 109 | } |
| 110 | |
| 111 | XGL_RESULT XGLAPI intelMapMemory( |
| 112 | XGL_GPU_MEMORY mem_, |
| 113 | XGL_FLAGS flags, |
| 114 | XGL_VOID** ppData) |
| 115 | { |
| 116 | struct intel_mem *mem = intel_mem(mem_); |
| 117 | void *ptr = intel_mem_map(mem, flags); |
| 118 | |
| 119 | *ppData = ptr; |
| 120 | |
| 121 | return (ptr) ? XGL_SUCCESS : XGL_ERROR_UNKNOWN; |
| 122 | } |
| 123 | |
| 124 | XGL_RESULT XGLAPI intelUnmapMemory( |
| 125 | XGL_GPU_MEMORY mem_) |
| 126 | { |
| 127 | struct intel_mem *mem = intel_mem(mem_); |
| 128 | |
| 129 | intel_mem_unmap(mem); |
| 130 | |
| 131 | return XGL_SUCCESS; |
| 132 | } |