1e88f27b3Smrg/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2e88f27b3Smrg
3e88f27b3Smrg/*
4e88f27b3Smrg * Copyright (C) 2011 Texas Instruments, Inc
5e88f27b3Smrg *
6e88f27b3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
7e88f27b3Smrg * copy of this software and associated documentation files (the "Software"),
8e88f27b3Smrg * to deal in the Software without restriction, including without limitation
9e88f27b3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10e88f27b3Smrg * and/or sell copies of the Software, and to permit persons to whom the
11e88f27b3Smrg * Software is furnished to do so, subject to the following conditions:
12e88f27b3Smrg *
13e88f27b3Smrg * The above copyright notice and this permission notice (including the next
14e88f27b3Smrg * paragraph) shall be included in all copies or substantial portions of the
15e88f27b3Smrg * Software.
16e88f27b3Smrg *
17e88f27b3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18e88f27b3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19e88f27b3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20e88f27b3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21e88f27b3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22e88f27b3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23e88f27b3Smrg * SOFTWARE.
24e88f27b3Smrg *
25e88f27b3Smrg * Authors:
26e88f27b3Smrg *    Rob Clark <rob@ti.com>
27e88f27b3Smrg */
28e88f27b3Smrg
29e88f27b3Smrg#include <stdlib.h>
30e88f27b3Smrg#include <linux/types.h>
31e88f27b3Smrg#include <errno.h>
32e88f27b3Smrg#include <sys/mman.h>
33e88f27b3Smrg#include <fcntl.h>
34e88f27b3Smrg#include <unistd.h>
35e88f27b3Smrg#include <pthread.h>
36e88f27b3Smrg
37e6188e58Smrg#include <libdrm_macros.h>
38e88f27b3Smrg#include <xf86drm.h>
39e88f27b3Smrg#include <xf86atomic.h>
40e88f27b3Smrg
41e88f27b3Smrg#include "omap_drm.h"
42e88f27b3Smrg#include "omap_drmif.h"
43e88f27b3Smrg
44e88f27b3Smrg#define __round_mask(x, y) ((__typeof__(x))((y)-1))
45e88f27b3Smrg#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
46e88f27b3Smrg#define PAGE_SIZE 4096
47e88f27b3Smrg
48e88f27b3Smrgstatic pthread_mutex_t table_lock = PTHREAD_MUTEX_INITIALIZER;
49e88f27b3Smrgstatic void * dev_table;
50e88f27b3Smrg
51e88f27b3Smrgstruct omap_device {
52e88f27b3Smrg	int fd;
53e88f27b3Smrg	atomic_t refcnt;
54e88f27b3Smrg
55e88f27b3Smrg	/* The handle_table is used to track GEM bo handles associated w/
56e88f27b3Smrg	 * this fd.  This is needed, in particular, when importing
57e88f27b3Smrg	 * dmabuf's because we don't want multiple 'struct omap_bo's
58e88f27b3Smrg	 * floating around with the same handle.  Otherwise, when the
59e88f27b3Smrg	 * first one is omap_bo_del()'d the handle becomes no longer
60e88f27b3Smrg	 * valid, and the remaining 'struct omap_bo's are left pointing
61e88f27b3Smrg	 * to an invalid handle (and possible a GEM bo that is already
62e88f27b3Smrg	 * free'd).
63e88f27b3Smrg	 */
64e88f27b3Smrg	void *handle_table;
65e88f27b3Smrg};
66e88f27b3Smrg
67e88f27b3Smrg/* a GEM buffer object allocated from the DRM device */
68e88f27b3Smrgstruct omap_bo {
69e88f27b3Smrg	struct omap_device	*dev;
70e88f27b3Smrg	void		*map;		/* userspace mmap'ing (if there is one) */
71e88f27b3Smrg	uint32_t	size;
72e88f27b3Smrg	uint32_t	handle;
73e88f27b3Smrg	uint32_t	name;		/* flink global handle (DRI2 name) */
74e88f27b3Smrg	uint64_t	offset;		/* offset to mmap() */
75e88f27b3Smrg	int		fd;		/* dmabuf handle */
76e88f27b3Smrg	atomic_t	refcnt;
77e88f27b3Smrg};
78e88f27b3Smrg
79e88f27b3Smrgstatic struct omap_device * omap_device_new_impl(int fd)
80e88f27b3Smrg{
81e88f27b3Smrg	struct omap_device *dev = calloc(sizeof(*dev), 1);
82e88f27b3Smrg	if (!dev)
83e88f27b3Smrg		return NULL;
84e88f27b3Smrg	dev->fd = fd;
85e88f27b3Smrg	atomic_set(&dev->refcnt, 1);
86e88f27b3Smrg	dev->handle_table = drmHashCreate();
87e88f27b3Smrg	return dev;
88e88f27b3Smrg}
89e88f27b3Smrg
907cdc0497Smrgdrm_public struct omap_device * omap_device_new(int fd)
91e88f27b3Smrg{
92e88f27b3Smrg	struct omap_device *dev = NULL;
93e88f27b3Smrg
94e88f27b3Smrg	pthread_mutex_lock(&table_lock);
95e88f27b3Smrg
96e88f27b3Smrg	if (!dev_table)
97e88f27b3Smrg		dev_table = drmHashCreate();
98e88f27b3Smrg
99e88f27b3Smrg	if (drmHashLookup(dev_table, fd, (void **)&dev)) {
100e88f27b3Smrg		/* not found, create new device */
101e88f27b3Smrg		dev = omap_device_new_impl(fd);
102e88f27b3Smrg		drmHashInsert(dev_table, fd, dev);
103e88f27b3Smrg	} else {
104e88f27b3Smrg		/* found, just incr refcnt */
105e88f27b3Smrg		dev = omap_device_ref(dev);
106e88f27b3Smrg	}
107e88f27b3Smrg
108e88f27b3Smrg	pthread_mutex_unlock(&table_lock);
109e88f27b3Smrg
110e88f27b3Smrg	return dev;
111e88f27b3Smrg}
112e88f27b3Smrg
1137cdc0497Smrgdrm_public struct omap_device * omap_device_ref(struct omap_device *dev)
114e88f27b3Smrg{
115e88f27b3Smrg	atomic_inc(&dev->refcnt);
116e88f27b3Smrg	return dev;
117e88f27b3Smrg}
118e88f27b3Smrg
1197cdc0497Smrgdrm_public void omap_device_del(struct omap_device *dev)
120e88f27b3Smrg{
121e88f27b3Smrg	if (!atomic_dec_and_test(&dev->refcnt))
122e88f27b3Smrg		return;
123e88f27b3Smrg	pthread_mutex_lock(&table_lock);
124e88f27b3Smrg	drmHashDestroy(dev->handle_table);
125e88f27b3Smrg	drmHashDelete(dev_table, dev->fd);
126e88f27b3Smrg	pthread_mutex_unlock(&table_lock);
127e88f27b3Smrg	free(dev);
128e88f27b3Smrg}
129e88f27b3Smrg
1307cdc0497Smrgdrm_public int
131baaff307Smrgomap_get_param(struct omap_device *dev, uint64_t param, uint64_t *value)
132e88f27b3Smrg{
133e88f27b3Smrg	struct drm_omap_param req = {
134e88f27b3Smrg			.param = param,
135e88f27b3Smrg	};
136e88f27b3Smrg	int ret;
137e88f27b3Smrg
138e88f27b3Smrg	ret = drmCommandWriteRead(dev->fd, DRM_OMAP_GET_PARAM, &req, sizeof(req));
139e88f27b3Smrg	if (ret) {
140e88f27b3Smrg		return ret;
141e88f27b3Smrg	}
142e88f27b3Smrg
143e88f27b3Smrg	*value = req.value;
144e88f27b3Smrg
145e88f27b3Smrg	return 0;
146e88f27b3Smrg}
147e88f27b3Smrg
1487cdc0497Smrgdrm_public int
149baaff307Smrgomap_set_param(struct omap_device *dev, uint64_t param, uint64_t value)
150e88f27b3Smrg{
151e88f27b3Smrg	struct drm_omap_param req = {
152e88f27b3Smrg			.param = param,
153e88f27b3Smrg			.value = value,
154e88f27b3Smrg	};
155e88f27b3Smrg	return drmCommandWrite(dev->fd, DRM_OMAP_SET_PARAM, &req, sizeof(req));
156e88f27b3Smrg}
157e88f27b3Smrg
158e88f27b3Smrg/* lookup a buffer from it's handle, call w/ table_lock held: */
159e88f27b3Smrgstatic struct omap_bo * lookup_bo(struct omap_device *dev,
160e88f27b3Smrg		uint32_t handle)
161e88f27b3Smrg{
162e88f27b3Smrg	struct omap_bo *bo = NULL;
163e88f27b3Smrg	if (!drmHashLookup(dev->handle_table, handle, (void **)&bo)) {
164e88f27b3Smrg		/* found, incr refcnt and return: */
165e88f27b3Smrg		bo = omap_bo_ref(bo);
166e88f27b3Smrg	}
167e88f27b3Smrg	return bo;
168e88f27b3Smrg}
169e88f27b3Smrg
170e88f27b3Smrg/* allocate a new buffer object, call w/ table_lock held */
171e88f27b3Smrgstatic struct omap_bo * bo_from_handle(struct omap_device *dev,
172e88f27b3Smrg		uint32_t handle)
173e88f27b3Smrg{
174e88f27b3Smrg	struct omap_bo *bo = calloc(sizeof(*bo), 1);
175e88f27b3Smrg	if (!bo) {
17649ef06a4Smrg		drmCloseBufferHandle(dev->fd, handle);
177e88f27b3Smrg		return NULL;
178e88f27b3Smrg	}
179e88f27b3Smrg	bo->dev = omap_device_ref(dev);
180e88f27b3Smrg	bo->handle = handle;
1813f012e29Smrg	bo->fd = -1;
182e88f27b3Smrg	atomic_set(&bo->refcnt, 1);
183e88f27b3Smrg	/* add ourselves to the handle table: */
184e88f27b3Smrg	drmHashInsert(dev->handle_table, handle, bo);
185e88f27b3Smrg	return bo;
186e88f27b3Smrg}
187e88f27b3Smrg
188e88f27b3Smrg/* allocate a new buffer object */
189e88f27b3Smrgstatic struct omap_bo * omap_bo_new_impl(struct omap_device *dev,
190e88f27b3Smrg		union omap_gem_size size, uint32_t flags)
191e88f27b3Smrg{
192e88f27b3Smrg	struct omap_bo *bo = NULL;
193e88f27b3Smrg	struct drm_omap_gem_new req = {
194e88f27b3Smrg			.size = size,
195e88f27b3Smrg			.flags = flags,
196e88f27b3Smrg	};
197e88f27b3Smrg
198e88f27b3Smrg	if (size.bytes == 0) {
199e88f27b3Smrg		goto fail;
200e88f27b3Smrg	}
201e88f27b3Smrg
202e88f27b3Smrg	if (drmCommandWriteRead(dev->fd, DRM_OMAP_GEM_NEW, &req, sizeof(req))) {
203e88f27b3Smrg		goto fail;
204e88f27b3Smrg	}
205e88f27b3Smrg
206e88f27b3Smrg	pthread_mutex_lock(&table_lock);
207e88f27b3Smrg	bo = bo_from_handle(dev, req.handle);
208e88f27b3Smrg	pthread_mutex_unlock(&table_lock);
209e88f27b3Smrg
210e88f27b3Smrg	if (flags & OMAP_BO_TILED) {
211e88f27b3Smrg		bo->size = round_up(size.tiled.width, PAGE_SIZE) * size.tiled.height;
212e88f27b3Smrg	} else {
213e88f27b3Smrg		bo->size = size.bytes;
214e88f27b3Smrg	}
215e88f27b3Smrg
216e88f27b3Smrg	return bo;
217e88f27b3Smrg
218e88f27b3Smrgfail:
219e88f27b3Smrg	free(bo);
220e88f27b3Smrg	return NULL;
221e88f27b3Smrg}
222e88f27b3Smrg
223e88f27b3Smrg
224e88f27b3Smrg/* allocate a new (un-tiled) buffer object */
2257cdc0497Smrgdrm_public struct omap_bo *
226baaff307Smrgomap_bo_new(struct omap_device *dev, uint32_t size, uint32_t flags)
227e88f27b3Smrg{
228e88f27b3Smrg	union omap_gem_size gsize = {
229e88f27b3Smrg			.bytes = size,
230e88f27b3Smrg	};
231e88f27b3Smrg	if (flags & OMAP_BO_TILED) {
232e88f27b3Smrg		return NULL;
233e88f27b3Smrg	}
234e88f27b3Smrg	return omap_bo_new_impl(dev, gsize, flags);
235e88f27b3Smrg}
236e88f27b3Smrg
237e88f27b3Smrg/* allocate a new buffer object */
2387cdc0497Smrgdrm_public struct omap_bo *
239baaff307Smrgomap_bo_new_tiled(struct omap_device *dev, uint32_t width,
240baaff307Smrg		  uint32_t height, uint32_t flags)
241e88f27b3Smrg{
242e88f27b3Smrg	union omap_gem_size gsize = {
243e88f27b3Smrg			.tiled = {
244e88f27b3Smrg				.width = width,
245e88f27b3Smrg				.height = height,
246e88f27b3Smrg			},
247e88f27b3Smrg	};
248e88f27b3Smrg	if (!(flags & OMAP_BO_TILED)) {
249e88f27b3Smrg		return NULL;
250e88f27b3Smrg	}
251e88f27b3Smrg	return omap_bo_new_impl(dev, gsize, flags);
252e88f27b3Smrg}
253e88f27b3Smrg
2547cdc0497Smrgdrm_public struct omap_bo *omap_bo_ref(struct omap_bo *bo)
255e88f27b3Smrg{
256e88f27b3Smrg	atomic_inc(&bo->refcnt);
257e88f27b3Smrg	return bo;
258e88f27b3Smrg}
259e88f27b3Smrg
260e88f27b3Smrg/* get buffer info */
261e88f27b3Smrgstatic int get_buffer_info(struct omap_bo *bo)
262e88f27b3Smrg{
263e88f27b3Smrg	struct drm_omap_gem_info req = {
264e88f27b3Smrg			.handle = bo->handle,
265e88f27b3Smrg	};
266e88f27b3Smrg	int ret = drmCommandWriteRead(bo->dev->fd, DRM_OMAP_GEM_INFO,
267e88f27b3Smrg			&req, sizeof(req));
268e88f27b3Smrg	if (ret) {
269e88f27b3Smrg		return ret;
270e88f27b3Smrg	}
271e88f27b3Smrg
272e88f27b3Smrg	/* really all we need for now is mmap offset */
273e88f27b3Smrg	bo->offset = req.offset;
274e88f27b3Smrg	bo->size = req.size;
275e88f27b3Smrg
276e88f27b3Smrg	return 0;
277e88f27b3Smrg}
278e88f27b3Smrg
279e88f27b3Smrg/* import a buffer object from DRI2 name */
2807cdc0497Smrgdrm_public struct omap_bo *
281baaff307Smrgomap_bo_from_name(struct omap_device *dev, uint32_t name)
282e88f27b3Smrg{
283e88f27b3Smrg	struct omap_bo *bo = NULL;
284e88f27b3Smrg	struct drm_gem_open req = {
285e88f27b3Smrg			.name = name,
286e88f27b3Smrg	};
287e88f27b3Smrg
288e88f27b3Smrg	pthread_mutex_lock(&table_lock);
289e88f27b3Smrg
290e88f27b3Smrg	if (drmIoctl(dev->fd, DRM_IOCTL_GEM_OPEN, &req)) {
291e88f27b3Smrg		goto fail;
292e88f27b3Smrg	}
293e88f27b3Smrg
294e88f27b3Smrg	bo = lookup_bo(dev, req.handle);
295e88f27b3Smrg	if (!bo) {
296e88f27b3Smrg		bo = bo_from_handle(dev, req.handle);
297e88f27b3Smrg		bo->name = name;
298e88f27b3Smrg	}
299e88f27b3Smrg
300e88f27b3Smrg	pthread_mutex_unlock(&table_lock);
301e88f27b3Smrg
302e88f27b3Smrg	return bo;
303e88f27b3Smrg
304e88f27b3Smrgfail:
305e88f27b3Smrg	pthread_mutex_unlock(&table_lock);
306e88f27b3Smrg	free(bo);
307e88f27b3Smrg	return NULL;
308e88f27b3Smrg}
309e88f27b3Smrg
310e88f27b3Smrg/* import a buffer from dmabuf fd, does not take ownership of the
311e88f27b3Smrg * fd so caller should close() the fd when it is otherwise done
312e88f27b3Smrg * with it (even if it is still using the 'struct omap_bo *')
313e88f27b3Smrg */
3147cdc0497Smrgdrm_public struct omap_bo *
315baaff307Smrgomap_bo_from_dmabuf(struct omap_device *dev, int fd)
316e88f27b3Smrg{
317e88f27b3Smrg	struct omap_bo *bo = NULL;
318e88f27b3Smrg	struct drm_prime_handle req = {
319e88f27b3Smrg			.fd = fd,
320e88f27b3Smrg	};
321e88f27b3Smrg	int ret;
322e88f27b3Smrg
323e88f27b3Smrg	pthread_mutex_lock(&table_lock);
324e88f27b3Smrg
325e88f27b3Smrg	ret = drmIoctl(dev->fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &req);
326e88f27b3Smrg	if (ret) {
327e88f27b3Smrg		goto fail;
328e88f27b3Smrg	}
329e88f27b3Smrg
330e88f27b3Smrg	bo = lookup_bo(dev, req.handle);
331e88f27b3Smrg	if (!bo) {
332e88f27b3Smrg		bo = bo_from_handle(dev, req.handle);
333e88f27b3Smrg	}
334e88f27b3Smrg
335e88f27b3Smrg	pthread_mutex_unlock(&table_lock);
336e88f27b3Smrg
337e88f27b3Smrg	return bo;
338e88f27b3Smrg
339e88f27b3Smrgfail:
340e88f27b3Smrg	pthread_mutex_unlock(&table_lock);
341e88f27b3Smrg	free(bo);
342e88f27b3Smrg	return NULL;
343e88f27b3Smrg}
344e88f27b3Smrg
345e88f27b3Smrg/* destroy a buffer object */
3467cdc0497Smrgdrm_public void omap_bo_del(struct omap_bo *bo)
347e88f27b3Smrg{
348e88f27b3Smrg	if (!bo) {
349e88f27b3Smrg		return;
350e88f27b3Smrg	}
351e88f27b3Smrg
352e88f27b3Smrg	if (!atomic_dec_and_test(&bo->refcnt))
353e88f27b3Smrg		return;
354e88f27b3Smrg
355e88f27b3Smrg	if (bo->map) {
356e88f27b3Smrg		munmap(bo->map, bo->size);
357e88f27b3Smrg	}
358e88f27b3Smrg
3593f012e29Smrg	if (bo->fd >= 0) {
360e88f27b3Smrg		close(bo->fd);
361e88f27b3Smrg	}
362e88f27b3Smrg
363e88f27b3Smrg	if (bo->handle) {
364e88f27b3Smrg		pthread_mutex_lock(&table_lock);
365e88f27b3Smrg		drmHashDelete(bo->dev->handle_table, bo->handle);
36649ef06a4Smrg		drmCloseBufferHandle(bo->dev->fd, bo->handle);
367e88f27b3Smrg		pthread_mutex_unlock(&table_lock);
368e88f27b3Smrg	}
369e88f27b3Smrg
370e88f27b3Smrg	omap_device_del(bo->dev);
371e88f27b3Smrg
372e88f27b3Smrg	free(bo);
373e88f27b3Smrg}
374e88f27b3Smrg
375e88f27b3Smrg/* get the global flink/DRI2 buffer name */
3767cdc0497Smrgdrm_public int omap_bo_get_name(struct omap_bo *bo, uint32_t *name)
377e88f27b3Smrg{
378e88f27b3Smrg	if (!bo->name) {
379e88f27b3Smrg		struct drm_gem_flink req = {
380e88f27b3Smrg				.handle = bo->handle,
381e88f27b3Smrg		};
382e88f27b3Smrg		int ret;
383e88f27b3Smrg
384e88f27b3Smrg		ret = drmIoctl(bo->dev->fd, DRM_IOCTL_GEM_FLINK, &req);
385e88f27b3Smrg		if (ret) {
386e88f27b3Smrg			return ret;
387e88f27b3Smrg		}
388e88f27b3Smrg
389e88f27b3Smrg		bo->name = req.name;
390e88f27b3Smrg	}
391e88f27b3Smrg
392e88f27b3Smrg	*name = bo->name;
393e88f27b3Smrg
394e88f27b3Smrg	return 0;
395e88f27b3Smrg}
396e88f27b3Smrg
3977cdc0497Smrgdrm_public uint32_t omap_bo_handle(struct omap_bo *bo)
398e88f27b3Smrg{
399e88f27b3Smrg	return bo->handle;
400e88f27b3Smrg}
401e88f27b3Smrg
402e88f27b3Smrg/* caller owns the dmabuf fd that is returned and is responsible
403e88f27b3Smrg * to close() it when done
404e88f27b3Smrg */
4057cdc0497Smrgdrm_public int omap_bo_dmabuf(struct omap_bo *bo)
406e88f27b3Smrg{
4073f012e29Smrg	if (bo->fd < 0) {
408e88f27b3Smrg		struct drm_prime_handle req = {
409e88f27b3Smrg				.handle = bo->handle,
4105324fb0dSmrg				.flags = DRM_CLOEXEC | DRM_RDWR,
411e88f27b3Smrg		};
412e88f27b3Smrg		int ret;
413e88f27b3Smrg
414e88f27b3Smrg		ret = drmIoctl(bo->dev->fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &req);
415e88f27b3Smrg		if (ret) {
416e88f27b3Smrg			return ret;
417e88f27b3Smrg		}
418e88f27b3Smrg
419e88f27b3Smrg		bo->fd = req.fd;
420e88f27b3Smrg	}
421e88f27b3Smrg	return dup(bo->fd);
422e88f27b3Smrg}
423e88f27b3Smrg
4247cdc0497Smrgdrm_public uint32_t omap_bo_size(struct omap_bo *bo)
425e88f27b3Smrg{
426e88f27b3Smrg	if (!bo->size) {
427e88f27b3Smrg		get_buffer_info(bo);
428e88f27b3Smrg	}
429e88f27b3Smrg	return bo->size;
430e88f27b3Smrg}
431e88f27b3Smrg
4327cdc0497Smrgdrm_public void *omap_bo_map(struct omap_bo *bo)
433e88f27b3Smrg{
434e88f27b3Smrg	if (!bo->map) {
435e88f27b3Smrg		if (!bo->offset) {
436e88f27b3Smrg			get_buffer_info(bo);
437e88f27b3Smrg		}
438e88f27b3Smrg
439e88f27b3Smrg		bo->map = mmap(0, bo->size, PROT_READ | PROT_WRITE,
440e88f27b3Smrg				MAP_SHARED, bo->dev->fd, bo->offset);
441e88f27b3Smrg		if (bo->map == MAP_FAILED) {
442e88f27b3Smrg			bo->map = NULL;
443e88f27b3Smrg		}
444e88f27b3Smrg	}
445e88f27b3Smrg	return bo->map;
446e88f27b3Smrg}
447e88f27b3Smrg
4487cdc0497Smrgdrm_public int omap_bo_cpu_prep(struct omap_bo *bo, enum omap_gem_op op)
449e88f27b3Smrg{
450e88f27b3Smrg	struct drm_omap_gem_cpu_prep req = {
451e88f27b3Smrg			.handle = bo->handle,
452e88f27b3Smrg			.op = op,
453e88f27b3Smrg	};
454e88f27b3Smrg	return drmCommandWrite(bo->dev->fd,
455e88f27b3Smrg			DRM_OMAP_GEM_CPU_PREP, &req, sizeof(req));
456e88f27b3Smrg}
457e88f27b3Smrg
4587cdc0497Smrgdrm_public int omap_bo_cpu_fini(struct omap_bo *bo, enum omap_gem_op op)
459e88f27b3Smrg{
460e88f27b3Smrg	struct drm_omap_gem_cpu_fini req = {
461e88f27b3Smrg			.handle = bo->handle,
462e88f27b3Smrg			.op = op,
463e88f27b3Smrg			.nregions = 0,
464e88f27b3Smrg	};
465e88f27b3Smrg	return drmCommandWrite(bo->dev->fd,
466e88f27b3Smrg			DRM_OMAP_GEM_CPU_FINI, &req, sizeof(req));
467e88f27b3Smrg}
468