162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright 2013 Red Hat Inc.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
562306a36Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
662306a36Sopenharmony_ci * to deal in the Software without restriction, including without limitation
762306a36Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
862306a36Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
962306a36Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
1262306a36Sopenharmony_ci * all copies or substantial portions of the Software.
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1562306a36Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1662306a36Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1762306a36Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
1862306a36Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
1962306a36Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
2062306a36Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
2162306a36Sopenharmony_ci *
2262306a36Sopenharmony_ci * Authors: Dave Airlie
2362306a36Sopenharmony_ci *          Alon Levy
2462306a36Sopenharmony_ci */
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci#include <drm/drm.h>
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_ci#include "qxl_drv.h"
2962306a36Sopenharmony_ci#include "qxl_object.h"
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_civoid qxl_gem_object_free(struct drm_gem_object *gobj)
3262306a36Sopenharmony_ci{
3362306a36Sopenharmony_ci	struct qxl_bo *qobj = gem_to_qxl_bo(gobj);
3462306a36Sopenharmony_ci	struct qxl_device *qdev;
3562306a36Sopenharmony_ci	struct ttm_buffer_object *tbo;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	qdev = to_qxl(gobj->dev);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	qxl_surface_evict(qdev, qobj, false);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	tbo = &qobj->tbo;
4262306a36Sopenharmony_ci	ttm_bo_put(tbo);
4362306a36Sopenharmony_ci}
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ciint qxl_gem_object_create(struct qxl_device *qdev, int size,
4662306a36Sopenharmony_ci			  int alignment, int initial_domain,
4762306a36Sopenharmony_ci			  bool discardable, bool kernel,
4862306a36Sopenharmony_ci			  struct qxl_surface *surf,
4962306a36Sopenharmony_ci			  struct drm_gem_object **obj)
5062306a36Sopenharmony_ci{
5162306a36Sopenharmony_ci	struct qxl_bo *qbo;
5262306a36Sopenharmony_ci	int r;
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	*obj = NULL;
5562306a36Sopenharmony_ci	/* At least align on page size */
5662306a36Sopenharmony_ci	if (alignment < PAGE_SIZE)
5762306a36Sopenharmony_ci		alignment = PAGE_SIZE;
5862306a36Sopenharmony_ci	r = qxl_bo_create(qdev, size, kernel, false, initial_domain, 0, surf, &qbo);
5962306a36Sopenharmony_ci	if (r) {
6062306a36Sopenharmony_ci		if (r != -ERESTARTSYS)
6162306a36Sopenharmony_ci			DRM_ERROR(
6262306a36Sopenharmony_ci			"Failed to allocate GEM object (%d, %d, %u, %d)\n",
6362306a36Sopenharmony_ci				  size, initial_domain, alignment, r);
6462306a36Sopenharmony_ci		return r;
6562306a36Sopenharmony_ci	}
6662306a36Sopenharmony_ci	*obj = &qbo->tbo.base;
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	mutex_lock(&qdev->gem.mutex);
6962306a36Sopenharmony_ci	list_add_tail(&qbo->list, &qdev->gem.objects);
7062306a36Sopenharmony_ci	mutex_unlock(&qdev->gem.mutex);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	return 0;
7362306a36Sopenharmony_ci}
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/*
7662306a36Sopenharmony_ci * If the caller passed a valid gobj pointer, it is responsible to call
7762306a36Sopenharmony_ci * drm_gem_object_put() when it no longer needs to acess the object.
7862306a36Sopenharmony_ci *
7962306a36Sopenharmony_ci * If gobj is NULL, it is handled internally.
8062306a36Sopenharmony_ci */
8162306a36Sopenharmony_ciint qxl_gem_object_create_with_handle(struct qxl_device *qdev,
8262306a36Sopenharmony_ci				      struct drm_file *file_priv,
8362306a36Sopenharmony_ci				      u32 domain,
8462306a36Sopenharmony_ci				      size_t size,
8562306a36Sopenharmony_ci				      struct qxl_surface *surf,
8662306a36Sopenharmony_ci				      struct drm_gem_object **gobj,
8762306a36Sopenharmony_ci				      uint32_t *handle)
8862306a36Sopenharmony_ci{
8962306a36Sopenharmony_ci	int r;
9062306a36Sopenharmony_ci	struct drm_gem_object *local_gobj;
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_ci	BUG_ON(!handle);
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	r = qxl_gem_object_create(qdev, size, 0,
9562306a36Sopenharmony_ci				  domain,
9662306a36Sopenharmony_ci				  false, false, surf,
9762306a36Sopenharmony_ci				  &local_gobj);
9862306a36Sopenharmony_ci	if (r)
9962306a36Sopenharmony_ci		return -ENOMEM;
10062306a36Sopenharmony_ci	r = drm_gem_handle_create(file_priv, local_gobj, handle);
10162306a36Sopenharmony_ci	if (r)
10262306a36Sopenharmony_ci		return r;
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_ci	if (gobj)
10562306a36Sopenharmony_ci		*gobj = local_gobj;
10662306a36Sopenharmony_ci	else
10762306a36Sopenharmony_ci		/* drop reference from allocate - handle holds it now */
10862306a36Sopenharmony_ci		drm_gem_object_put(local_gobj);
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci	return 0;
11162306a36Sopenharmony_ci}
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ciint qxl_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv)
11462306a36Sopenharmony_ci{
11562306a36Sopenharmony_ci	return 0;
11662306a36Sopenharmony_ci}
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_civoid qxl_gem_object_close(struct drm_gem_object *obj,
11962306a36Sopenharmony_ci			  struct drm_file *file_priv)
12062306a36Sopenharmony_ci{
12162306a36Sopenharmony_ci}
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_civoid qxl_gem_init(struct qxl_device *qdev)
12462306a36Sopenharmony_ci{
12562306a36Sopenharmony_ci	INIT_LIST_HEAD(&qdev->gem.objects);
12662306a36Sopenharmony_ci}
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_civoid qxl_gem_fini(struct qxl_device *qdev)
12962306a36Sopenharmony_ci{
13062306a36Sopenharmony_ci	qxl_bo_force_delete(qdev);
13162306a36Sopenharmony_ci}
132