1 1.2 riastrad /* $NetBSD: qxl_gem.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $ */ 2 1.2 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2013 Red Hat Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * Authors: Dave Airlie 25 1.1 riastrad * Alon Levy 26 1.1 riastrad */ 27 1.1 riastrad 28 1.2 riastrad #include <sys/cdefs.h> 29 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: qxl_gem.c,v 1.3 2021/12/18 23:45:42 riastradh Exp $"); 30 1.2 riastrad 31 1.3 riastrad #include <drm/drm.h> 32 1.3 riastrad 33 1.1 riastrad #include "qxl_drv.h" 34 1.1 riastrad #include "qxl_object.h" 35 1.1 riastrad 36 1.1 riastrad void qxl_gem_object_free(struct drm_gem_object *gobj) 37 1.1 riastrad { 38 1.1 riastrad struct qxl_bo *qobj = gem_to_qxl_bo(gobj); 39 1.2 riastrad struct qxl_device *qdev; 40 1.2 riastrad struct ttm_buffer_object *tbo; 41 1.2 riastrad 42 1.2 riastrad qdev = (struct qxl_device *)gobj->dev->dev_private; 43 1.2 riastrad 44 1.2 riastrad qxl_surface_evict(qdev, qobj, false); 45 1.1 riastrad 46 1.2 riastrad tbo = &qobj->tbo; 47 1.3 riastrad ttm_bo_put(tbo); 48 1.1 riastrad } 49 1.1 riastrad 50 1.1 riastrad int qxl_gem_object_create(struct qxl_device *qdev, int size, 51 1.1 riastrad int alignment, int initial_domain, 52 1.1 riastrad bool discardable, bool kernel, 53 1.1 riastrad struct qxl_surface *surf, 54 1.1 riastrad struct drm_gem_object **obj) 55 1.1 riastrad { 56 1.1 riastrad struct qxl_bo *qbo; 57 1.1 riastrad int r; 58 1.1 riastrad 59 1.1 riastrad *obj = NULL; 60 1.1 riastrad /* At least align on page size */ 61 1.1 riastrad if (alignment < PAGE_SIZE) 62 1.1 riastrad alignment = PAGE_SIZE; 63 1.1 riastrad r = qxl_bo_create(qdev, size, kernel, false, initial_domain, surf, &qbo); 64 1.1 riastrad if (r) { 65 1.1 riastrad if (r != -ERESTARTSYS) 66 1.1 riastrad DRM_ERROR( 67 1.1 riastrad "Failed to allocate GEM object (%d, %d, %u, %d)\n", 68 1.1 riastrad size, initial_domain, alignment, r); 69 1.1 riastrad return r; 70 1.1 riastrad } 71 1.3 riastrad *obj = &qbo->tbo.base; 72 1.1 riastrad 73 1.1 riastrad mutex_lock(&qdev->gem.mutex); 74 1.1 riastrad list_add_tail(&qbo->list, &qdev->gem.objects); 75 1.1 riastrad mutex_unlock(&qdev->gem.mutex); 76 1.1 riastrad 77 1.1 riastrad return 0; 78 1.1 riastrad } 79 1.1 riastrad 80 1.1 riastrad int qxl_gem_object_create_with_handle(struct qxl_device *qdev, 81 1.1 riastrad struct drm_file *file_priv, 82 1.1 riastrad u32 domain, 83 1.1 riastrad size_t size, 84 1.1 riastrad struct qxl_surface *surf, 85 1.1 riastrad struct qxl_bo **qobj, 86 1.1 riastrad uint32_t *handle) 87 1.1 riastrad { 88 1.1 riastrad struct drm_gem_object *gobj; 89 1.1 riastrad int r; 90 1.1 riastrad 91 1.1 riastrad BUG_ON(!qobj); 92 1.1 riastrad BUG_ON(!handle); 93 1.1 riastrad 94 1.1 riastrad r = qxl_gem_object_create(qdev, size, 0, 95 1.1 riastrad domain, 96 1.1 riastrad false, false, surf, 97 1.1 riastrad &gobj); 98 1.1 riastrad if (r) 99 1.1 riastrad return -ENOMEM; 100 1.1 riastrad r = drm_gem_handle_create(file_priv, gobj, handle); 101 1.1 riastrad if (r) 102 1.1 riastrad return r; 103 1.1 riastrad /* drop reference from allocate - handle holds it now */ 104 1.1 riastrad *qobj = gem_to_qxl_bo(gobj); 105 1.3 riastrad drm_gem_object_put_unlocked(gobj); 106 1.1 riastrad return 0; 107 1.1 riastrad } 108 1.1 riastrad 109 1.1 riastrad int qxl_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv) 110 1.1 riastrad { 111 1.1 riastrad return 0; 112 1.1 riastrad } 113 1.1 riastrad 114 1.1 riastrad void qxl_gem_object_close(struct drm_gem_object *obj, 115 1.1 riastrad struct drm_file *file_priv) 116 1.1 riastrad { 117 1.1 riastrad } 118 1.1 riastrad 119 1.3 riastrad void qxl_gem_init(struct qxl_device *qdev) 120 1.1 riastrad { 121 1.1 riastrad INIT_LIST_HEAD(&qdev->gem.objects); 122 1.1 riastrad } 123 1.1 riastrad 124 1.1 riastrad void qxl_gem_fini(struct qxl_device *qdev) 125 1.1 riastrad { 126 1.1 riastrad qxl_bo_force_delete(qdev); 127 1.1 riastrad } 128