summaryrefslogtreecommitdiff
path: root/drivers/gpu/drm/openchrome/openchrome_object.c
blob: c79f28d87d000d8e5f2c97a124d91ea3d3f79645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright © 2018-2019 Kevin Brace
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including
 * the next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
/*
 * Author(s):
 *
 * Kevin Brace <kevinbrace@gmx.com>
 */
/*
 * openchrome_object.c
 *
 * Manages Buffer Objects (BO) via TTM.
 * Part of the TTM memory allocator.
 *
 */

#include <drm/drm_file.h>

#include <drm/ttm/ttm_bo_api.h>
#include <drm/ttm/ttm_bo_driver.h>

#include "openchrome_drv.h"


static void openchrome_gem_free(struct drm_gem_object *obj)
{
	struct openchrome_bo *bo = container_of(obj,
					struct openchrome_bo, gem);

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	ttm_bo_put(&bo->ttm_bo);

	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
}

static const struct drm_gem_object_funcs openchrome_gem_object_funcs = {
	.free = openchrome_gem_free,
};

int openchrome_ttm_mmap(struct file *filp, struct vm_area_struct *vma)
{
	struct drm_file *file_priv = filp->private_data;
	struct openchrome_drm_private *dev_private =
				file_priv->minor->dev->dev_private;
	int ret = -EINVAL;

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	if (!dev_private) {
		DRM_DEBUG_KMS("No device private data.\n");
		ret = -EINVAL;
		goto exit;
	}

	ret = ttm_bo_mmap(filp, vma, &dev_private->bdev);
exit:
	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
	return ret;
}

void openchrome_ttm_domain_to_placement(struct openchrome_bo *bo,
					uint32_t ttm_domain)
{
	unsigned i = 0;

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	bo->placement.placement = bo->placements;
	bo->placement.busy_placement = bo->placements;

	if (ttm_domain == TTM_PL_SYSTEM) {
		bo->placements[i].fpfn = 0;
		bo->placements[i].lpfn = 0;
		bo->placements[i].mem_type = TTM_PL_SYSTEM;
		bo->placements[i].flags = 0;
		i++;
	}

	if (ttm_domain == TTM_PL_TT) {
		bo->placements[i].fpfn = 0;
		bo->placements[i].lpfn = 0;
		bo->placements[i].mem_type = TTM_PL_TT;
		bo->placements[i].flags = 0;
		i++;
	}

	if (ttm_domain == TTM_PL_VRAM) {
		bo->placements[i].fpfn = 0;
		bo->placements[i].lpfn = 0;
		bo->placements[i].mem_type = TTM_PL_VRAM;
		bo->placements[i].flags = 0;
		i++;
	}

	bo->placement.num_placement = i;
	bo->placement.num_busy_placement = i;

	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
}

void openchrome_ttm_bo_destroy(struct ttm_buffer_object *tbo)
{
	struct openchrome_bo *bo = container_of(tbo,
					struct openchrome_bo, ttm_bo);

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	drm_gem_object_release(&bo->gem);
	kfree(bo);

	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
}

int openchrome_bo_pin(struct openchrome_bo *bo,
			uint32_t ttm_domain)
{
	struct ttm_operation_ctx ctx = {false, false};
	int ret = 0;

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	if (bo->ttm_bo.pin_count) {
		goto pin;
	}

	openchrome_ttm_domain_to_placement(bo, ttm_domain);
	ret = ttm_bo_validate(&bo->ttm_bo, &bo->placement, &ctx);
	if (ret) {
		goto exit;
	}

pin:
	ttm_bo_pin(&bo->ttm_bo);
exit:

	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
	return ret;
}

void openchrome_bo_unpin(struct openchrome_bo *bo)
{
	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	ttm_bo_unpin(&bo->ttm_bo);

	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
}

int openchrome_bo_create(struct drm_device *dev,
				struct ttm_bo_device *bdev,
				uint64_t size,
				enum ttm_bo_type type,
				uint32_t ttm_domain,
				bool kmap,
				struct openchrome_bo **bo_ptr)
{
	struct openchrome_drm_private *dev_private = dev->dev_private;
	struct openchrome_bo *bo;
	size_t acc_size;
	int ret;

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

//	bo = kzalloc(sizeof(struct openchrome_bo), GFP_KERNEL);
	bo = kzalloc(sizeof(*bo), GFP_KERNEL);
	if (!bo) {
		DRM_ERROR("Cannot allocate a TTM buffer object.\n");
		ret = -ENOMEM;
		goto exit;
	}

	/*
	 * It is imperative to page align the requested buffer size
	 * prior to a memory allocation request, or various memory
	 * allocation related system instabilities may occur.
	 */
	size = ALIGN(size, PAGE_SIZE);

	ret = drm_gem_object_init(dev, &bo->gem, size);
	if (ret) {
		DRM_ERROR("Cannot initialize a GEM object.\n");
		goto error;
	}

	bo->gem.funcs = &openchrome_gem_object_funcs;

	openchrome_ttm_domain_to_placement(bo, ttm_domain);
	acc_size = ttm_bo_dma_acc_size(&dev_private->bdev, size,
					sizeof(struct openchrome_bo));
	ret = ttm_bo_init(&dev_private->bdev,
				&bo->ttm_bo,
				size,
				type,
				&bo->placement,
				PAGE_SIZE >> PAGE_SHIFT,
				false, acc_size,
				NULL, NULL,
				openchrome_ttm_bo_destroy);
	if (ret) {
		DRM_ERROR("Cannot initialize a TTM object.\n");
		goto exit;
	}

	if (kmap) {
		ret = ttm_bo_reserve(&bo->ttm_bo, true, false, NULL);
		if (ret) {
			ttm_bo_put(&bo->ttm_bo);
			goto exit;
		}

		ret = openchrome_bo_pin(bo, ttm_domain);
		ttm_bo_unreserve(&bo->ttm_bo);
		if (ret) {
			ttm_bo_put(&bo->ttm_bo);
			goto exit;
		}

		ret = ttm_bo_kmap(&bo->ttm_bo, 0,
					bo->ttm_bo.mem.num_pages,
					&bo->kmap);
		if (ret) {
			ttm_bo_put(&bo->ttm_bo);
			goto exit;
		}
	}

	*bo_ptr = bo;
	goto exit;
error:
	kfree(bo);
exit:
	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
	return ret;
}

void openchrome_bo_destroy(struct openchrome_bo *bo, bool kmap)
{
	int ret;

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	if (kmap) {
		ttm_bo_kunmap(&bo->kmap);

		ret = ttm_bo_reserve(&bo->ttm_bo, true, false, NULL);
		if (ret) {
			goto exit;
		}

		openchrome_bo_unpin(bo);
		ttm_bo_unreserve(&bo->ttm_bo);
		if (ret) {
			goto exit;
		}
	}

	ttm_bo_put(&bo->ttm_bo);
exit:
	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
}

int openchrome_mm_init(struct openchrome_drm_private *dev_private)
{
	struct drm_device *dev = dev_private->dev;
	int ret;

	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	/*
	 * Initialize bdev ttm_bo_device struct.
	 */
	ret = ttm_bo_device_init(&dev_private->bdev,
				&openchrome_bo_driver,
				dev->dev,
				dev->anon_inode->i_mapping,
				dev->vma_offset_manager,
				false,
				dev_private->need_dma32);
	if (ret) {
		DRM_ERROR("Failed initializing buffer object driver.\n");
		goto exit;
	}

	/*
	 * Initialize TTM range manager for VRAM management.
	 */
	ret = ttm_range_man_init(&dev_private->bdev, TTM_PL_VRAM,
				false,
				dev_private->vram_size >> PAGE_SHIFT);
	if (ret) {
		DRM_ERROR("Failed initializing TTM VRAM memory manager.\n");
		goto exit;
	}

exit:
	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
	return ret;
}

void openchrome_mm_fini(struct openchrome_drm_private *dev_private)
{
	DRM_DEBUG_KMS("Entered %s.\n", __func__);

	ttm_range_man_fini(&dev_private->bdev, TTM_PL_VRAM);

	ttm_bo_device_release(&dev_private->bdev);

	DRM_DEBUG_KMS("Exiting %s.\n", __func__);
}