1 /* 2 * Copyright (C) 2015 Red Hat, Inc. 3 * All Rights Reserved. 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining 6 * a copy of this software and associated documentation files (the 7 * "Software"), to deal in the Software without restriction, including 8 * without limitation the rights to use, copy, modify, merge, publish, 9 * distribute, sublicense, and/or sell copies of the Software, and to 10 * permit persons to whom the Software is furnished to do so, subject to 11 * the following conditions: 12 * 13 * The above copyright notice and this permission notice (including the 14 * next paragraph) shall be included in all copies or substantial 15 * portions of the Software. 16 * 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE 21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 */ 25 26 #include "virtgpu_drv.h" 27 28 static void virtio_gpu_ttm_bo_destroy(struct ttm_buffer_object *tbo) 29 { 30 struct virtio_gpu_object *bo; 31 struct virtio_gpu_device *vgdev; 32 33 bo = container_of(tbo, struct virtio_gpu_object, tbo); 34 vgdev = (struct virtio_gpu_device *)bo->gem_base.dev->dev_private; 35 36 if (bo->hw_res_handle) 37 virtio_gpu_cmd_unref_resource(vgdev, bo->hw_res_handle); 38 if (bo->pages) 39 virtio_gpu_object_free_sg_table(bo); 40 if (bo->vmap) 41 virtio_gpu_object_kunmap(bo); 42 drm_gem_object_release(&bo->gem_base); 43 kfree(bo); 44 } 45 46 static void virtio_gpu_init_ttm_placement(struct virtio_gpu_object *vgbo, 47 bool pinned) 48 { 49 u32 c = 1; 50 u32 pflag = pinned ? TTM_PL_FLAG_NO_EVICT : 0; 51 52 vgbo->placement.placement = &vgbo->placement_code; 53 vgbo->placement.busy_placement = &vgbo->placement_code; 54 vgbo->placement_code.fpfn = 0; 55 vgbo->placement_code.lpfn = 0; 56 vgbo->placement_code.flags = 57 TTM_PL_MASK_CACHING | TTM_PL_FLAG_TT | pflag; 58 vgbo->placement.num_placement = c; 59 vgbo->placement.num_busy_placement = c; 60 61 } 62 63 int virtio_gpu_object_create(struct virtio_gpu_device *vgdev, 64 unsigned long size, bool kernel, bool pinned, 65 struct virtio_gpu_object **bo_ptr) 66 { 67 struct virtio_gpu_object *bo; 68 enum ttm_bo_type type; 69 size_t acc_size; 70 int ret; 71 72 if (kernel) 73 type = ttm_bo_type_kernel; 74 else 75 type = ttm_bo_type_device; 76 *bo_ptr = NULL; 77 78 acc_size = ttm_bo_dma_acc_size(&vgdev->mman.bdev, size, 79 sizeof(struct virtio_gpu_object)); 80 81 bo = kzalloc(sizeof(struct virtio_gpu_object), GFP_KERNEL); 82 if (bo == NULL) 83 return -ENOMEM; 84 size = roundup(size, PAGE_SIZE); 85 ret = drm_gem_object_init(vgdev->ddev, &bo->gem_base, size); 86 if (ret != 0) { 87 kfree(bo); 88 return ret; 89 } 90 bo->dumb = false; 91 virtio_gpu_init_ttm_placement(bo, pinned); 92 93 ret = ttm_bo_init(&vgdev->mman.bdev, &bo->tbo, size, type, 94 &bo->placement, 0, !kernel, acc_size, 95 NULL, NULL, &virtio_gpu_ttm_bo_destroy); 96 /* ttm_bo_init failure will call the destroy */ 97 if (ret != 0) 98 return ret; 99 100 *bo_ptr = bo; 101 return 0; 102 } 103 104 void virtio_gpu_object_kunmap(struct virtio_gpu_object *bo) 105 { 106 bo->vmap = NULL; 107 ttm_bo_kunmap(&bo->kmap); 108 } 109 110 int virtio_gpu_object_kmap(struct virtio_gpu_object *bo) 111 { 112 bool is_iomem; 113 int r; 114 115 WARN_ON(bo->vmap); 116 117 r = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages, &bo->kmap); 118 if (r) 119 return r; 120 bo->vmap = ttm_kmap_obj_virtual(&bo->kmap, &is_iomem); 121 return 0; 122 } 123 124 int virtio_gpu_object_get_sg_table(struct virtio_gpu_device *qdev, 125 struct virtio_gpu_object *bo) 126 { 127 int ret; 128 struct page **pages = bo->tbo.ttm->pages; 129 int nr_pages = bo->tbo.num_pages; 130 struct ttm_operation_ctx ctx = { 131 .interruptible = false, 132 .no_wait_gpu = false 133 }; 134 135 /* wtf swapping */ 136 if (bo->pages) 137 return 0; 138 139 if (bo->tbo.ttm->state == tt_unpopulated) 140 bo->tbo.ttm->bdev->driver->ttm_tt_populate(bo->tbo.ttm, &ctx); 141 bo->pages = kmalloc(sizeof(struct sg_table), GFP_KERNEL); 142 if (!bo->pages) 143 goto out; 144 145 ret = sg_alloc_table_from_pages(bo->pages, pages, nr_pages, 0, 146 nr_pages << PAGE_SHIFT, GFP_KERNEL); 147 if (ret) 148 goto out; 149 return 0; 150 out: 151 kfree(bo->pages); 152 bo->pages = NULL; 153 return -ENOMEM; 154 } 155 156 void virtio_gpu_object_free_sg_table(struct virtio_gpu_object *bo) 157 { 158 sg_free_table(bo->pages); 159 kfree(bo->pages); 160 bo->pages = NULL; 161 } 162 163 int virtio_gpu_object_wait(struct virtio_gpu_object *bo, bool no_wait) 164 { 165 int r; 166 167 r = ttm_bo_reserve(&bo->tbo, true, no_wait, NULL); 168 if (unlikely(r != 0)) 169 return r; 170 r = ttm_bo_wait(&bo->tbo, true, no_wait); 171 ttm_bo_unreserve(&bo->tbo); 172 return r; 173 } 174 175