r600g: let radeon_winsys maintain the list of relocations
[mesa.git] / src / gallium / winsys / r600 / drm / radeon_bo.c
1 /*
2 * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
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 * Jerome Glisse
25 */
26 #define _FILE_OFFSET_BITS 64
27 #include "r600_priv.h"
28 #include "util/u_hash_table.h"
29 #include "util/u_memory.h"
30 #include "radeon_drm.h"
31 #include "xf86drm.h"
32 #include <sys/mman.h>
33 #include <errno.h>
34
35 int radeon_bo_fixed_map(struct radeon *radeon, struct radeon_bo *bo)
36 {
37 struct drm_radeon_gem_mmap args;
38 void *ptr;
39 int r;
40
41 /* Zero out args to make valgrind happy */
42 memset(&args, 0, sizeof(args));
43 args.handle = bo->handle;
44 args.offset = 0;
45 args.size = (uint64_t)bo->size;
46 r = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_MMAP,
47 &args, sizeof(args));
48 if (r) {
49 fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n",
50 bo, bo->handle, r);
51 return r;
52 }
53 ptr = mmap(0, args.size, PROT_READ|PROT_WRITE, MAP_SHARED, radeon->info.fd, args.addr_ptr);
54 if (ptr == MAP_FAILED) {
55 fprintf(stderr, "%s failed to map bo\n", __func__);
56 return -errno;
57 }
58 bo->data = ptr;
59
60 bo->map_count++;
61 return 0;
62 }
63
64 static void radeon_bo_fixed_unmap(struct radeon *radeon, struct radeon_bo *bo)
65 {
66 if (bo->data) {
67 munmap(bo->data, bo->size);
68 bo->data = NULL;
69 }
70 }
71
72 #include "state_tracker/drm_driver.h"
73
74 struct radeon_bo *radeon_bo(struct radeon *radeon, unsigned handle,
75 unsigned size, unsigned alignment, unsigned bind,
76 unsigned initial_domain)
77 {
78 struct radeon_bo *bo;
79 struct winsys_handle whandle = {};
80 whandle.handle = handle;
81
82 bo = calloc(1, sizeof(*bo));
83 if (bo == NULL) {
84 return NULL;
85 }
86 pipe_reference_init(&bo->reference, 1);
87
88 if (handle) {
89 bo->buf = radeon->ws->buffer_from_handle(radeon->ws, &whandle, NULL, &size);
90 } else {
91 bo->buf = radeon->ws->buffer_create(radeon->ws, size, alignment, bind, initial_domain);
92 }
93 if (!bo->buf) {
94 FREE(bo);
95 return NULL;
96 }
97 bo->cs_buf = radeon->ws->buffer_get_cs_handle(bo->buf);
98 bo->handle = radeon->ws->trans_get_buffer_handle(bo->buf);
99 bo->size = size;
100 return bo;
101 }
102
103 static void radeon_bo_destroy(struct radeon *radeon, struct radeon_bo *bo)
104 {
105 radeon_bo_fixed_unmap(radeon, bo);
106 pb_reference(&bo->buf, NULL);
107 FREE(bo);
108 }
109
110 void radeon_bo_reference(struct radeon *radeon,
111 struct radeon_bo **dst,
112 struct radeon_bo *src)
113 {
114 struct radeon_bo *old = *dst;
115 if (pipe_reference(&(*dst)->reference, &src->reference)) {
116 radeon_bo_destroy(radeon, old);
117 }
118 *dst = src;
119 }
120
121 int radeon_bo_wait(struct radeon *radeon, struct radeon_bo *bo)
122 {
123 struct drm_radeon_gem_wait_idle args;
124 int ret;
125
126 /* Zero out args to make valgrind happy */
127 memset(&args, 0, sizeof(args));
128 args.handle = bo->handle;
129 do {
130 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_WAIT_IDLE,
131 &args, sizeof(args));
132 } while (ret == -EBUSY);
133 return ret;
134 }
135
136 int radeon_bo_busy(struct radeon *radeon, struct radeon_bo *bo, uint32_t *domain)
137 {
138 struct drm_radeon_gem_busy args;
139 int ret;
140
141 memset(&args, 0, sizeof(args));
142 args.handle = bo->handle;
143 args.domain = 0;
144
145 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_BUSY,
146 &args, sizeof(args));
147
148 *domain = args.domain;
149 return ret;
150 }
151
152 int radeon_bo_get_tiling_flags(struct radeon *radeon,
153 struct radeon_bo *bo,
154 uint32_t *tiling_flags)
155 {
156 struct drm_radeon_gem_get_tiling args = {};
157 int ret;
158
159 args.handle = bo->handle;
160 ret = drmCommandWriteRead(radeon->info.fd, DRM_RADEON_GEM_GET_TILING,
161 &args, sizeof(args));
162 if (ret)
163 return ret;
164
165 *tiling_flags = args.tiling_flags;
166 return ret;
167 }
168
169 int radeon_bo_get_name(struct radeon *radeon,
170 struct radeon_bo *bo,
171 uint32_t *name)
172 {
173 struct drm_gem_flink flink;
174 int ret;
175
176 flink.handle = bo->handle;
177 ret = drmIoctl(radeon->info.fd, DRM_IOCTL_GEM_FLINK, &flink);
178 if (ret)
179 return ret;
180
181 *name = flink.name;
182 return ret;
183 }