r600g: move more DRM queries into winsys/radeon
[mesa.git] / src / gallium / winsys / r600 / drm / r600_bo.c
1 /*
2 * Copyright 2010 Dave Airlie
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Dave Airlie
25 */
26 #include "r600_priv.h"
27 #include "r600d.h"
28 #include "state_tracker/drm_driver.h"
29 #include "radeon_drm.h"
30
31 struct r600_bo *r600_bo(struct radeon *radeon,
32 unsigned size, unsigned alignment,
33 unsigned binding, unsigned usage)
34 {
35 struct r600_bo *bo;
36 struct radeon_bo *rbo;
37 uint32_t initial_domain, domains;
38
39 /* Staging resources particpate in transfers and blits only
40 * and are used for uploads and downloads from regular
41 * resources. We generate them internally for some transfers.
42 */
43 if (usage == PIPE_USAGE_STAGING) {
44 domains = RADEON_GEM_DOMAIN_GTT;
45 initial_domain = RADEON_GEM_DOMAIN_GTT;
46 } else {
47 domains = RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM;
48
49 switch(usage) {
50 case PIPE_USAGE_DYNAMIC:
51 case PIPE_USAGE_STREAM:
52 case PIPE_USAGE_STAGING:
53 initial_domain = RADEON_GEM_DOMAIN_GTT;
54 break;
55 case PIPE_USAGE_DEFAULT:
56 case PIPE_USAGE_STATIC:
57 case PIPE_USAGE_IMMUTABLE:
58 default:
59 initial_domain = RADEON_GEM_DOMAIN_VRAM;
60 break;
61 }
62 }
63
64 rbo = radeon_bo(radeon, 0, size, alignment, binding, initial_domain);
65 if (rbo == NULL) {
66 return NULL;
67 }
68
69 bo = calloc(1, sizeof(struct r600_bo));
70 bo->domains = domains;
71 bo->bo = rbo;
72
73 pipe_reference_init(&bo->reference, 1);
74 return bo;
75 }
76
77 struct r600_bo *r600_bo_handle(struct radeon *radeon, struct winsys_handle *whandle,
78 unsigned *stride, unsigned *array_mode)
79 {
80 struct r600_bo *bo = calloc(1, sizeof(struct r600_bo));
81 struct radeon_bo *rbo;
82 unsigned tiling_flags;
83
84 rbo = bo->bo = radeon_bo(radeon, whandle->handle, 0, 0, 0, 0);
85 if (rbo == NULL) {
86 free(bo);
87 return NULL;
88 }
89
90 pipe_reference_init(&bo->reference, 1);
91 bo->domains = RADEON_GEM_DOMAIN_GTT | RADEON_GEM_DOMAIN_VRAM;
92
93 if (stride)
94 *stride = whandle->stride;
95
96 radeon_bo_get_tiling_flags(radeon, rbo, &tiling_flags);
97 if (array_mode) {
98 if (tiling_flags) {
99 if (tiling_flags & RADEON_TILING_MACRO)
100 *array_mode = V_0280A0_ARRAY_2D_TILED_THIN1;
101 else if (tiling_flags & RADEON_TILING_MICRO)
102 *array_mode = V_0280A0_ARRAY_1D_TILED_THIN1;
103 } else {
104 *array_mode = 0;
105 }
106 }
107 return bo;
108 }
109
110 void *r600_bo_map(struct radeon *radeon, struct r600_bo *bo, unsigned usage, void *ctx)
111 {
112 struct pipe_context *pctx = ctx;
113
114 if (usage & PIPE_TRANSFER_UNSYNCHRONIZED) {
115 radeon_bo_map(radeon, bo->bo);
116 return (uint8_t *) bo->bo->data;
117 }
118
119 if (p_atomic_read(&bo->bo->reference.count) > 1) {
120 if (usage & PIPE_TRANSFER_DONTBLOCK) {
121 return NULL;
122 }
123 if (ctx) {
124 pctx->flush(pctx, NULL);
125 }
126 }
127
128 if (usage & PIPE_TRANSFER_DONTBLOCK) {
129 uint32_t domain;
130
131 if (radeon_bo_busy(radeon, bo->bo, &domain))
132 return NULL;
133 if (radeon_bo_map(radeon, bo->bo)) {
134 return NULL;
135 }
136 goto out;
137 }
138
139 radeon_bo_map(radeon, bo->bo);
140 if (radeon_bo_wait(radeon, bo->bo)) {
141 radeon_bo_unmap(radeon, bo->bo);
142 return NULL;
143 }
144
145 out:
146 return (uint8_t *) bo->bo->data;
147 }
148
149 void r600_bo_unmap(struct radeon *radeon, struct r600_bo *bo)
150 {
151 radeon_bo_unmap(radeon, bo->bo);
152 }
153
154 void r600_bo_destroy(struct radeon *radeon, struct r600_bo *bo)
155 {
156 radeon_bo_reference(radeon, &bo->bo, NULL);
157 free(bo);
158 }
159
160 boolean r600_bo_get_winsys_handle(struct radeon *radeon, struct r600_bo *bo,
161 unsigned stride, struct winsys_handle *whandle)
162 {
163 whandle->stride = stride;
164 switch(whandle->type) {
165 case DRM_API_HANDLE_TYPE_KMS:
166 whandle->handle = bo->bo->handle;
167 break;
168 case DRM_API_HANDLE_TYPE_SHARED:
169 if (radeon_bo_get_name(radeon, bo->bo, &whandle->handle))
170 return FALSE;
171 break;
172 default:
173 return FALSE;
174 }
175
176 return TRUE;
177 }