summaryrefslogtreecommitdiff
path: root/sys/dev/pci/drm/drm_gem_cma_helper.c
blob: ba61bba96405513276b162b134533eb76278232f (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
/* $OpenBSD: drm_gem_cma_helper.c,v 1.5 2021/07/07 02:38:21 jsg Exp $ */
/* $NetBSD: drm_gem_cma_helper.c,v 1.9 2019/11/05 23:29:28 jmcneill Exp $ */
/*-
 * Copyright (c) 2015-2017 Jared McNeill <jmcneill@invisible.ca>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/param.h>

#include <drm/drm_device.h>
#include <drm/drm_gem_cma_helper.h>

#include <uvm/uvm.h>

static struct drm_gem_cma_object *
drm_gem_cma_create_internal(struct drm_device *ddev, size_t size,
    struct sg_table *sgt)
{
	struct drm_gem_cma_object *obj;
	int error, nsegs;

	obj = malloc(sizeof(*obj), M_DRM, M_WAITOK | M_ZERO);
	obj->dmat = ddev->dmat;
	obj->dmasize = size;

	if (sgt) {
#ifdef notyet
		error = -drm_prime_sg_to_bus_dmamem(obj->dmat, obj->dmasegs, 1,
		    &nsegs, sgt);
#endif
		error = -ENOMEM;
	} else {
		error = bus_dmamem_alloc(obj->dmat, obj->dmasize,
		    PAGE_SIZE, 0, obj->dmasegs, 1, &nsegs,
		    BUS_DMA_WAITOK);
	}
	if (error)
		goto failed;
	error = bus_dmamem_map(obj->dmat, obj->dmasegs, nsegs,
	    obj->dmasize, &obj->vaddr,
	    BUS_DMA_WAITOK | BUS_DMA_NOCACHE);
	if (error)
		goto free;
	error = bus_dmamap_create(obj->dmat, obj->dmasize, 1,
	    obj->dmasize, 0, BUS_DMA_WAITOK, &obj->dmamap);
	if (error)
		goto unmap;
	error = bus_dmamap_load(obj->dmat, obj->dmamap, obj->vaddr,
	    obj->dmasize, NULL, BUS_DMA_WAITOK);
	if (error)
		goto destroy;

#ifdef notyet
	if (!sgt)
#endif
		memset(obj->vaddr, 0, obj->dmasize);

	error = drm_gem_object_init(ddev, &obj->base, size);
	if (error)
		goto unload;

	return obj;

unload:
	bus_dmamap_unload(obj->dmat, obj->dmamap);
destroy:
	bus_dmamap_destroy(obj->dmat, obj->dmamap);
unmap:
	bus_dmamem_unmap(obj->dmat, obj->vaddr, obj->dmasize);
free:
#ifdef notyet
	if (obj->sgt)
		drm_prime_sg_free(obj->sgt);
	else
#endif
		bus_dmamem_free(obj->dmat, obj->dmasegs, nsegs);
failed:
	free(obj, M_DRM, sizeof(*obj));

	return NULL;
}

struct drm_gem_cma_object *
drm_gem_cma_create(struct drm_device *ddev, size_t size)
{

	return drm_gem_cma_create_internal(ddev, size, NULL);
}

static void
drm_gem_cma_obj_free(struct drm_gem_cma_object *obj)
{

	bus_dmamap_unload(obj->dmat, obj->dmamap);
	bus_dmamap_destroy(obj->dmat, obj->dmamap);
	bus_dmamem_unmap(obj->dmat, obj->vaddr, obj->dmasize);
#ifdef notyet
	if (obj->sgt)
		drm_prime_sg_free(obj->sgt);
	else
#endif
		bus_dmamem_free(obj->dmat, obj->dmasegs, 1);
	free(obj, M_DRM, sizeof(*obj));
}

void
drm_gem_cma_free_object(struct drm_gem_object *gem_obj)
{
	struct drm_gem_cma_object *obj = to_drm_gem_cma_obj(gem_obj);

	drm_gem_free_mmap_offset(gem_obj);
	drm_gem_object_release(gem_obj);
	drm_gem_cma_obj_free(obj);
}

int
drm_gem_cma_dumb_create(struct drm_file *file_priv, struct drm_device *ddev,
    struct drm_mode_create_dumb *args)
{
	struct drm_gem_cma_object *obj;
	uint32_t handle;
	int error;

	args->pitch = args->width * ((args->bpp + 7) / 8);
	args->size = args->pitch * args->height;
	args->size = roundup(args->size, PAGE_SIZE);
	args->handle = 0;

	obj = drm_gem_cma_create(ddev, args->size);
	if (obj == NULL)
		return -ENOMEM;

	error = drm_gem_handle_create(file_priv, &obj->base, &handle);
	drm_gem_object_put(&obj->base);
	if (error) {
		drm_gem_cma_obj_free(obj);
		return error;
	}

	args->handle = handle;

	return 0;
}

int
drm_gem_cma_fault(struct drm_gem_object *gem_obj, struct uvm_faultinfo *ufi,
    off_t offset, vaddr_t vaddr, vm_page_t *pps, int npages, int centeridx,
    vm_prot_t access_type, int flags)
{
	struct vm_map_entry *entry = ufi->entry;
	struct drm_gem_cma_object *obj = to_drm_gem_cma_obj(gem_obj);
	struct uvm_object *uobj = &obj->base.uobj;
	paddr_t paddr;
	int lcv, retval;
	vm_prot_t mapprot;

	offset -= drm_vma_node_offset_addr(&obj->base.vma_node);
	mapprot = ufi->entry->protection;

	retval = 0;
	for (lcv = 0; lcv < npages; lcv++, offset += PAGE_SIZE,
	    vaddr += PAGE_SIZE) {
		if ((flags & PGO_ALLPAGES) == 0 && lcv != centeridx)
			continue;

		if (pps[lcv] == PGO_DONTCARE)
			continue;

		paddr = bus_dmamem_mmap(obj->dmat, obj->dmasegs, 1,
		    offset, access_type, BUS_DMA_NOCACHE);
		if (paddr == -1) {
			retval = VM_PAGER_BAD;
			break;
		}

		if (pmap_enter(ufi->orig_map->pmap, vaddr, paddr,
		    mapprot, PMAP_CANFAIL | mapprot) != 0) {
			pmap_update(ufi->orig_map->pmap);
			uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap,
			    uobj);
			uvm_wait("drm_gem_cma_fault");
			return VM_PAGER_REFAULT;
		}
	}

	pmap_update(ufi->orig_map->pmap);
	uvmfault_unlockall(ufi, ufi->entry->aref.ar_amap, uobj);

	return retval;
}

struct sg_table *
drm_gem_cma_prime_get_sg_table(struct drm_gem_object *gem_obj)
{
	struct drm_gem_cma_object *obj = to_drm_gem_cma_obj(gem_obj);

	return NULL;
#ifdef notyet
	return drm_prime_bus_dmamem_to_sg(obj->dmat, obj->dmasegs, 1);
#endif
}

struct drm_gem_object *
drm_gem_cma_prime_import_sg_table(struct drm_device *ddev,
    struct dma_buf_attachment *attach, struct sg_table *sgt)
{
	return NULL;
#ifdef notyet
	size_t size = drm_prime_sg_size(sgt);
	struct drm_gem_cma_object *obj;

	obj = drm_gem_cma_create_internal(ddev, size, sgt);
	if (obj == NULL)
		return ERR_PTR(-ENOMEM);

	return &obj->base;
#endif
}

void *
drm_gem_cma_prime_vmap(struct drm_gem_object *gem_obj)
{
	struct drm_gem_cma_object *obj = to_drm_gem_cma_obj(gem_obj);

	return obj->vaddr;
}

void
drm_gem_cma_prime_vunmap(struct drm_gem_object *gem_obj, void *vaddr)
{
	struct drm_gem_cma_object *obj =
	    to_drm_gem_cma_obj(gem_obj);

	KASSERT(vaddr == obj->vaddr);
}