Merge remote branch 'origin/master' into nv50-compiler
[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 <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/mman.h>
31 #include <errno.h>
32 #include "radeon_priv.h"
33 #include "xf86drm.h"
34 #include "radeon_drm.h"
35
36 struct radeon_bo *radeon_bo(struct radeon *radeon, unsigned handle,
37 unsigned size, unsigned alignment, void *ptr)
38 {
39 struct radeon_bo *bo;
40 int r;
41
42 bo = calloc(1, sizeof(*bo));
43 if (bo == NULL) {
44 return NULL;
45 }
46 bo->size = size;
47 bo->handle = handle;
48 bo->refcount = 1;
49 bo->alignment = alignment;
50
51 if (handle) {
52 struct drm_gem_open open_arg;
53
54 memset(&open_arg, 0, sizeof(open_arg));
55 open_arg.name = handle;
56 r = drmIoctl(radeon->fd, DRM_IOCTL_GEM_OPEN, &open_arg);
57 if (r != 0) {
58 free(bo);
59 return NULL;
60 }
61 bo->handle = open_arg.handle;
62 bo->size = open_arg.size;
63 } else {
64 struct drm_radeon_gem_create args;
65
66 args.size = size;
67 args.alignment = alignment;
68 args.initial_domain = RADEON_GEM_DOMAIN_CPU;
69 args.flags = 0;
70 args.handle = 0;
71 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_GEM_CREATE,
72 &args, sizeof(args));
73 bo->handle = args.handle;
74 if (r) {
75 fprintf(stderr, "Failed to allocate :\n");
76 fprintf(stderr, " size : %d bytes\n", size);
77 fprintf(stderr, " alignment : %d bytes\n", alignment);
78 free(bo);
79 return NULL;
80 }
81 }
82 if (ptr) {
83 if (radeon_bo_map(radeon, bo)) {
84 fprintf(stderr, "%s failed to copy data into bo\n", __func__);
85 return radeon_bo_decref(radeon, bo);
86 }
87 memcpy(bo->data, ptr, size);
88 radeon_bo_unmap(radeon, bo);
89 }
90 return bo;
91 }
92
93 int radeon_bo_map(struct radeon *radeon, struct radeon_bo *bo)
94 {
95 struct drm_radeon_gem_mmap args;
96 void *ptr;
97 int r;
98
99 if (bo->map_count++ != 0) {
100 return 0;
101 }
102 /* Zero out args to make valgrind happy */
103 memset(&args, 0, sizeof(args));
104 args.handle = bo->handle;
105 args.offset = 0;
106 args.size = (uint64_t)bo->size;
107 r = drmCommandWriteRead(radeon->fd, DRM_RADEON_GEM_MMAP,
108 &args, sizeof(args));
109 if (r) {
110 fprintf(stderr, "error mapping %p 0x%08X (error = %d)\n",
111 bo, bo->handle, r);
112 return r;
113 }
114 ptr = mmap(0, args.size, PROT_READ|PROT_WRITE, MAP_SHARED, radeon->fd, args.addr_ptr);
115 if (ptr == MAP_FAILED) {
116 fprintf(stderr, "%s failed to map bo\n", __func__);
117 return -errno;
118 }
119 bo->data = ptr;
120 return 0;
121 }
122
123 void radeon_bo_unmap(struct radeon *radeon, struct radeon_bo *bo)
124 {
125 if (--bo->map_count > 0) {
126 return;
127 }
128 munmap(bo->data, bo->size);
129 bo->data = NULL;
130 }
131
132 struct radeon_bo *radeon_bo_incref(struct radeon *radeon, struct radeon_bo *bo)
133 {
134 bo->refcount++;
135 return bo;
136 }
137
138 struct radeon_bo *radeon_bo_decref(struct radeon *radeon, struct radeon_bo *bo)
139 {
140 struct drm_gem_close args;
141
142 if (bo == NULL)
143 return NULL;
144 if (--bo->refcount > 0) {
145 return NULL;
146 }
147
148 if (bo->map_count) {
149 munmap(bo->data, bo->size);
150 }
151 memset(&args, 0, sizeof(args));
152 args.handle = bo->handle;
153 drmIoctl(radeon->fd, DRM_IOCTL_GEM_CLOSE, &args);
154 memset(bo, 0, sizeof(struct radeon_bo));
155 free(bo);
156 return NULL;
157 }
158
159 int radeon_bo_wait(struct radeon *radeon, struct radeon_bo *bo)
160 {
161 struct drm_radeon_gem_wait_idle args;
162 int ret;
163
164 /* Zero out args to make valgrind happy */
165 memset(&args, 0, sizeof(args));
166 args.handle = bo->handle;
167 do {
168 ret = drmCommandWriteRead(radeon->fd, DRM_RADEON_GEM_WAIT_IDLE,
169 &args, sizeof(args));
170 } while (ret == -EBUSY);
171 return ret;
172 }