Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Red Hat |
| 4 | * Author: Rob Clark <robdclark@gmail.com> |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 5 | */ |
| 6 | |
Christoph Hellwig | 0a0f0d8 | 2020-09-22 15:31:03 +0200 | [diff] [blame] | 7 | #include <linux/dma-map-ops.h> |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 8 | #include <linux/spinlock.h> |
| 9 | #include <linux/shmem_fs.h> |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 10 | #include <linux/dma-buf.h> |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 11 | #include <linux/pfn_t.h> |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 12 | |
Sam Ravnborg | feea39a | 2019-08-04 08:55:51 +0200 | [diff] [blame] | 13 | #include <drm/drm_prime.h> |
| 14 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 15 | #include "msm_drv.h" |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 16 | #include "msm_fence.h" |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 17 | #include "msm_gem.h" |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 18 | #include "msm_gpu.h" |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 19 | #include "msm_mmu.h" |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 20 | |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 21 | static void update_inactive(struct msm_gem_object *msm_obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 22 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 23 | static dma_addr_t physaddr(struct drm_gem_object *obj) |
| 24 | { |
| 25 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 26 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 27 | return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) + |
| 28 | priv->vram.paddr; |
| 29 | } |
| 30 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 31 | static bool use_pages(struct drm_gem_object *obj) |
| 32 | { |
| 33 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 34 | return !msm_obj->vram_node; |
| 35 | } |
| 36 | |
Rob Clark | 3de433c | 2019-07-30 14:46:28 -0700 | [diff] [blame] | 37 | /* |
| 38 | * Cache sync.. this is a bit over-complicated, to fit dma-mapping |
| 39 | * API. Really GPU cache is out of scope here (handled on cmdstream) |
| 40 | * and all we need to do is invalidate newly allocated pages before |
| 41 | * mapping to CPU as uncached/writecombine. |
| 42 | * |
| 43 | * On top of this, we have the added headache, that depending on |
| 44 | * display generation, the display's iommu may be wired up to either |
| 45 | * the toplevel drm device (mdss), or to the mdp sub-node, meaning |
| 46 | * that here we either have dma-direct or iommu ops. |
| 47 | * |
| 48 | * Let this be a cautionary tail of abstraction gone wrong. |
| 49 | */ |
| 50 | |
| 51 | static void sync_for_device(struct msm_gem_object *msm_obj) |
| 52 | { |
| 53 | struct device *dev = msm_obj->base.dev->dev; |
| 54 | |
Dave Airlie | 91d0ca3 | 2020-09-29 10:18:49 +1000 | [diff] [blame] | 55 | dma_map_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0); |
Rob Clark | 3de433c | 2019-07-30 14:46:28 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static void sync_for_cpu(struct msm_gem_object *msm_obj) |
| 59 | { |
| 60 | struct device *dev = msm_obj->base.dev->dev; |
| 61 | |
Dave Airlie | 91d0ca3 | 2020-09-29 10:18:49 +1000 | [diff] [blame] | 62 | dma_unmap_sgtable(dev, msm_obj->sgt, DMA_BIDIRECTIONAL, 0); |
Rob Clark | 3de433c | 2019-07-30 14:46:28 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 65 | /* allocate pages from VRAM carveout, used when no IOMMU: */ |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 66 | static struct page **get_pages_vram(struct drm_gem_object *obj, int npages) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 67 | { |
| 68 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 69 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 70 | dma_addr_t paddr; |
| 71 | struct page **p; |
| 72 | int ret, i; |
| 73 | |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 74 | p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 75 | if (!p) |
| 76 | return ERR_PTR(-ENOMEM); |
| 77 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 78 | spin_lock(&priv->vram.lock); |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 79 | ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 80 | spin_unlock(&priv->vram.lock); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 81 | if (ret) { |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 82 | kvfree(p); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 83 | return ERR_PTR(ret); |
| 84 | } |
| 85 | |
| 86 | paddr = physaddr(obj); |
| 87 | for (i = 0; i < npages; i++) { |
| 88 | p[i] = phys_to_page(paddr); |
| 89 | paddr += PAGE_SIZE; |
| 90 | } |
| 91 | |
| 92 | return p; |
| 93 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 94 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 95 | static struct page **get_pages(struct drm_gem_object *obj) |
| 96 | { |
| 97 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 98 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 99 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Iskren Chernev | 07fcad0 | 2020-12-28 23:31:31 +0200 | [diff] [blame] | 100 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 101 | if (!msm_obj->pages) { |
| 102 | struct drm_device *dev = obj->dev; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 103 | struct page **p; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 104 | int npages = obj->size >> PAGE_SHIFT; |
| 105 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 106 | if (use_pages(obj)) |
David Herrmann | 0cdbe8a | 2014-05-25 12:59:47 +0200 | [diff] [blame] | 107 | p = drm_gem_get_pages(obj); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 108 | else |
| 109 | p = get_pages_vram(obj, npages); |
| 110 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 111 | if (IS_ERR(p)) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 112 | DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n", |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 113 | PTR_ERR(p)); |
| 114 | return p; |
| 115 | } |
| 116 | |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 117 | msm_obj->pages = p; |
| 118 | |
Gerd Hoffmann | 707d561 | 2020-09-07 13:24:25 +0200 | [diff] [blame] | 119 | msm_obj->sgt = drm_prime_pages_to_sg(obj->dev, p, npages); |
Wei Yongjun | 1f70e07 | 2013-09-11 06:56:12 +0800 | [diff] [blame] | 120 | if (IS_ERR(msm_obj->sgt)) { |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 121 | void *ptr = ERR_CAST(msm_obj->sgt); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 122 | |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 123 | DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n"); |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 124 | msm_obj->sgt = NULL; |
| 125 | return ptr; |
| 126 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 127 | |
| 128 | /* For non-cached buffers, ensure the new pages are clean |
| 129 | * because display controller, GPU, etc. are not coherent: |
| 130 | */ |
| 131 | if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) |
Rob Clark | 3de433c | 2019-07-30 14:46:28 -0700 | [diff] [blame] | 132 | sync_for_device(msm_obj); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 133 | |
| 134 | GEM_WARN_ON(msm_obj->active_count); |
| 135 | update_inactive(msm_obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | return msm_obj->pages; |
| 139 | } |
| 140 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 141 | static void put_pages_vram(struct drm_gem_object *obj) |
| 142 | { |
| 143 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 144 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 145 | |
| 146 | spin_lock(&priv->vram.lock); |
| 147 | drm_mm_remove_node(msm_obj->vram_node); |
| 148 | spin_unlock(&priv->vram.lock); |
| 149 | |
| 150 | kvfree(msm_obj->pages); |
| 151 | } |
| 152 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 153 | static void put_pages(struct drm_gem_object *obj) |
| 154 | { |
| 155 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 156 | |
| 157 | if (msm_obj->pages) { |
Ben Hutchings | 3976626 | 2018-04-03 23:38:45 +0100 | [diff] [blame] | 158 | if (msm_obj->sgt) { |
| 159 | /* For non-cached buffers, ensure the new |
| 160 | * pages are clean because display controller, |
| 161 | * GPU, etc. are not coherent: |
| 162 | */ |
| 163 | if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) |
Rob Clark | 3de433c | 2019-07-30 14:46:28 -0700 | [diff] [blame] | 164 | sync_for_cpu(msm_obj); |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 165 | |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 166 | sg_free_table(msm_obj->sgt); |
Ben Hutchings | 3976626 | 2018-04-03 23:38:45 +0100 | [diff] [blame] | 167 | kfree(msm_obj->sgt); |
Rob Clark | b9a31d0 | 2021-04-05 10:45:26 -0700 | [diff] [blame] | 168 | msm_obj->sgt = NULL; |
Ben Hutchings | 3976626 | 2018-04-03 23:38:45 +0100 | [diff] [blame] | 169 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 170 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 171 | if (use_pages(obj)) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 172 | drm_gem_put_pages(obj, msm_obj->pages, true, false); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 173 | else |
| 174 | put_pages_vram(obj); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 175 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 176 | msm_obj->pages = NULL; |
| 177 | } |
| 178 | } |
| 179 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 180 | struct page **msm_gem_get_pages(struct drm_gem_object *obj) |
| 181 | { |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 182 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 183 | struct page **p; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 184 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 185 | msm_gem_lock(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 186 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 187 | if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) { |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 188 | msm_gem_unlock(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 189 | return ERR_PTR(-EBUSY); |
| 190 | } |
| 191 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 192 | p = get_pages(obj); |
Rob Clark | 10f7616 | 2021-04-26 16:53:25 -0700 | [diff] [blame] | 193 | |
| 194 | if (!IS_ERR(p)) { |
| 195 | msm_obj->pin_count++; |
| 196 | update_inactive(msm_obj); |
| 197 | } |
| 198 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 199 | msm_gem_unlock(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 200 | return p; |
| 201 | } |
| 202 | |
| 203 | void msm_gem_put_pages(struct drm_gem_object *obj) |
| 204 | { |
Rob Clark | 10f7616 | 2021-04-26 16:53:25 -0700 | [diff] [blame] | 205 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 206 | |
| 207 | msm_gem_lock(obj); |
| 208 | msm_obj->pin_count--; |
| 209 | GEM_WARN_ON(msm_obj->pin_count < 0); |
| 210 | update_inactive(msm_obj); |
| 211 | msm_gem_unlock(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 214 | int msm_gem_mmap_obj(struct drm_gem_object *obj, |
| 215 | struct vm_area_struct *vma) |
| 216 | { |
| 217 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 218 | |
| 219 | vma->vm_flags &= ~VM_PFNMAP; |
| 220 | vma->vm_flags |= VM_MIXEDMAP; |
| 221 | |
| 222 | if (msm_obj->flags & MSM_BO_WC) { |
| 223 | vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 224 | } else if (msm_obj->flags & MSM_BO_UNCACHED) { |
| 225 | vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 226 | } else { |
| 227 | /* |
| 228 | * Shunt off cached objs to shmem file so they have their own |
| 229 | * address_space (so unmap_mapping_range does what we want, |
| 230 | * in particular in the case of mmap'd dmabufs) |
| 231 | */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 232 | vma->vm_pgoff = 0; |
Christian König | 295992f | 2020-09-14 15:09:33 +0200 | [diff] [blame] | 233 | vma_set_file(vma, obj->filp); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 234 | |
| 235 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 236 | } |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 242 | { |
| 243 | int ret; |
| 244 | |
| 245 | ret = drm_gem_mmap(filp, vma); |
| 246 | if (ret) { |
| 247 | DBG("mmap failed: %d", ret); |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | return msm_gem_mmap_obj(vma->vm_private_data, vma); |
| 252 | } |
| 253 | |
Thomas Zimmermann | 3c9edd9 | 2020-09-23 12:21:46 +0200 | [diff] [blame] | 254 | static vm_fault_t msm_gem_fault(struct vm_fault *vmf) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 255 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 256 | struct vm_area_struct *vma = vmf->vma; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 257 | struct drm_gem_object *obj = vma->vm_private_data; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 258 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 259 | struct page **pages; |
| 260 | unsigned long pfn; |
| 261 | pgoff_t pgoff; |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 262 | int err; |
| 263 | vm_fault_t ret; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 264 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 265 | /* |
| 266 | * vm_ops.open/drm_gem_mmap_obj and close get and put |
| 267 | * a reference on obj. So, we dont need to hold one here. |
Rob Clark | d78d383 | 2016-08-22 15:28:38 -0400 | [diff] [blame] | 268 | */ |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 269 | err = msm_gem_lock_interruptible(obj); |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 270 | if (err) { |
| 271 | ret = VM_FAULT_NOPAGE; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 272 | goto out; |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 273 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 274 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 275 | if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) { |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 276 | msm_gem_unlock(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 277 | return VM_FAULT_SIGBUS; |
| 278 | } |
| 279 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 280 | /* make sure we have pages attached now */ |
| 281 | pages = get_pages(obj); |
| 282 | if (IS_ERR(pages)) { |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 283 | ret = vmf_error(PTR_ERR(pages)); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 284 | goto out_unlock; |
| 285 | } |
| 286 | |
| 287 | /* We don't use vmf->pgoff since that has the fake offset: */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 288 | pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 289 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 290 | pfn = page_to_pfn(pages[pgoff]); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 291 | |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 292 | VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 293 | pfn, pfn << PAGE_SHIFT); |
| 294 | |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 295 | ret = vmf_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV)); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 296 | out_unlock: |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 297 | msm_gem_unlock(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 298 | out: |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 299 | return ret; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /** get mmap offset */ |
| 303 | static uint64_t mmap_offset(struct drm_gem_object *obj) |
| 304 | { |
| 305 | struct drm_device *dev = obj->dev; |
| 306 | int ret; |
| 307 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 308 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 309 | |
| 310 | /* Make it mmapable */ |
| 311 | ret = drm_gem_create_mmap_offset(obj); |
| 312 | |
| 313 | if (ret) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 314 | DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n"); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
| 318 | return drm_vma_node_offset_addr(&obj->vma_node); |
| 319 | } |
| 320 | |
| 321 | uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj) |
| 322 | { |
| 323 | uint64_t offset; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 324 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 325 | msm_gem_lock(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 326 | offset = mmap_offset(obj); |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 327 | msm_gem_unlock(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 328 | return offset; |
| 329 | } |
| 330 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 331 | static struct msm_gem_vma *add_vma(struct drm_gem_object *obj, |
| 332 | struct msm_gem_address_space *aspace) |
| 333 | { |
| 334 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 335 | struct msm_gem_vma *vma; |
| 336 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 337 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 338 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 339 | vma = kzalloc(sizeof(*vma), GFP_KERNEL); |
| 340 | if (!vma) |
| 341 | return ERR_PTR(-ENOMEM); |
| 342 | |
| 343 | vma->aspace = aspace; |
| 344 | |
| 345 | list_add_tail(&vma->list, &msm_obj->vmas); |
| 346 | |
| 347 | return vma; |
| 348 | } |
| 349 | |
| 350 | static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj, |
| 351 | struct msm_gem_address_space *aspace) |
| 352 | { |
| 353 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 354 | struct msm_gem_vma *vma; |
| 355 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 356 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 357 | |
| 358 | list_for_each_entry(vma, &msm_obj->vmas, list) { |
| 359 | if (vma->aspace == aspace) |
| 360 | return vma; |
| 361 | } |
| 362 | |
| 363 | return NULL; |
| 364 | } |
| 365 | |
| 366 | static void del_vma(struct msm_gem_vma *vma) |
| 367 | { |
| 368 | if (!vma) |
| 369 | return; |
| 370 | |
| 371 | list_del(&vma->list); |
| 372 | kfree(vma); |
| 373 | } |
| 374 | |
Rob Clark | 20d0ae2 | 2021-04-05 10:45:27 -0700 | [diff] [blame] | 375 | /** |
| 376 | * If close is true, this also closes the VMA (releasing the allocated |
| 377 | * iova range) in addition to removing the iommu mapping. In the eviction |
| 378 | * case (!close), we keep the iova allocated, but only remove the iommu |
| 379 | * mapping. |
| 380 | */ |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 381 | static void |
Rob Clark | 20d0ae2 | 2021-04-05 10:45:27 -0700 | [diff] [blame] | 382 | put_iova_spaces(struct drm_gem_object *obj, bool close) |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 383 | { |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 384 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Iskren Chernev | 9b73bde | 2020-11-26 15:02:23 +0200 | [diff] [blame] | 385 | struct msm_gem_vma *vma; |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 386 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 387 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 388 | |
Iskren Chernev | 9b73bde | 2020-11-26 15:02:23 +0200 | [diff] [blame] | 389 | list_for_each_entry(vma, &msm_obj->vmas, list) { |
Brian Masney | d67f1b6 | 2019-06-02 21:01:31 -0400 | [diff] [blame] | 390 | if (vma->aspace) { |
| 391 | msm_gem_purge_vma(vma->aspace, vma); |
Rob Clark | 20d0ae2 | 2021-04-05 10:45:27 -0700 | [diff] [blame] | 392 | if (close) |
| 393 | msm_gem_close_vma(vma->aspace, vma); |
Brian Masney | d67f1b6 | 2019-06-02 21:01:31 -0400 | [diff] [blame] | 394 | } |
Iskren Chernev | 9b73bde | 2020-11-26 15:02:23 +0200 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | /* Called with msm_obj locked */ |
| 399 | static void |
| 400 | put_iova_vmas(struct drm_gem_object *obj) |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 401 | { |
| 402 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 403 | struct msm_gem_vma *vma, *tmp; |
| 404 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 405 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 406 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 407 | list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) { |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 408 | del_vma(vma); |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 409 | } |
| 410 | } |
| 411 | |
Rob Clark | 8117e5e | 2020-10-23 09:51:04 -0700 | [diff] [blame] | 412 | static int get_iova_locked(struct drm_gem_object *obj, |
Jonathan Marek | d3b8877 | 2020-04-23 17:09:13 -0400 | [diff] [blame] | 413 | struct msm_gem_address_space *aspace, uint64_t *iova, |
| 414 | u64 range_start, u64 range_end) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 415 | { |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 416 | struct msm_gem_vma *vma; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 417 | int ret = 0; |
| 418 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 419 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | cb1e381 | 2017-06-13 09:15:36 -0400 | [diff] [blame] | 420 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 421 | vma = lookup_vma(obj, aspace); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 422 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 423 | if (!vma) { |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 424 | vma = add_vma(obj, aspace); |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 425 | if (IS_ERR(vma)) |
| 426 | return PTR_ERR(vma); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 427 | |
Jonathan Marek | d3b8877 | 2020-04-23 17:09:13 -0400 | [diff] [blame] | 428 | ret = msm_gem_init_vma(aspace, vma, obj->size >> PAGE_SHIFT, |
| 429 | range_start, range_end); |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 430 | if (ret) { |
| 431 | del_vma(vma); |
| 432 | return ret; |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 433 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 436 | *iova = vma->iova; |
| 437 | return 0; |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 438 | } |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 439 | |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 440 | static int msm_gem_pin_iova(struct drm_gem_object *obj, |
| 441 | struct msm_gem_address_space *aspace) |
| 442 | { |
| 443 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 444 | struct msm_gem_vma *vma; |
| 445 | struct page **pages; |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 446 | int ret, prot = IOMMU_READ; |
Rob Clark | bbc2cd0 | 2019-01-09 14:25:05 -0500 | [diff] [blame] | 447 | |
| 448 | if (!(msm_obj->flags & MSM_BO_GPU_READONLY)) |
| 449 | prot |= IOMMU_WRITE; |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 450 | |
Jonathan Marek | 0b462d7 | 2020-04-23 17:09:14 -0400 | [diff] [blame] | 451 | if (msm_obj->flags & MSM_BO_MAP_PRIV) |
| 452 | prot |= IOMMU_PRIV; |
| 453 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 454 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 455 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 456 | if (GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 457 | return -EBUSY; |
| 458 | |
| 459 | vma = lookup_vma(obj, aspace); |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 460 | if (GEM_WARN_ON(!vma)) |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 461 | return -EINVAL; |
| 462 | |
| 463 | pages = get_pages(obj); |
| 464 | if (IS_ERR(pages)) |
| 465 | return PTR_ERR(pages); |
| 466 | |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 467 | ret = msm_gem_map_vma(aspace, vma, prot, |
Rob Clark | bbc2cd0 | 2019-01-09 14:25:05 -0500 | [diff] [blame] | 468 | msm_obj->sgt, obj->size >> PAGE_SHIFT); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 469 | |
| 470 | if (!ret) |
| 471 | msm_obj->pin_count++; |
| 472 | |
| 473 | return ret; |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 474 | } |
| 475 | |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 476 | static int get_and_pin_iova_range_locked(struct drm_gem_object *obj, |
Jonathan Marek | d3b8877 | 2020-04-23 17:09:13 -0400 | [diff] [blame] | 477 | struct msm_gem_address_space *aspace, uint64_t *iova, |
| 478 | u64 range_start, u64 range_end) |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 479 | { |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 480 | u64 local; |
| 481 | int ret; |
| 482 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 483 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 484 | |
Rob Clark | 8117e5e | 2020-10-23 09:51:04 -0700 | [diff] [blame] | 485 | ret = get_iova_locked(obj, aspace, &local, |
Jonathan Marek | d3b8877 | 2020-04-23 17:09:13 -0400 | [diff] [blame] | 486 | range_start, range_end); |
Jordan Crouse | c0ee979 | 2018-11-07 15:35:48 -0700 | [diff] [blame] | 487 | |
| 488 | if (!ret) |
| 489 | ret = msm_gem_pin_iova(obj, aspace); |
| 490 | |
| 491 | if (!ret) |
| 492 | *iova = local; |
| 493 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 494 | return ret; |
| 495 | } |
| 496 | |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 497 | /* |
| 498 | * get iova and pin it. Should have a matching put |
| 499 | * limits iova to specified range (in pages) |
| 500 | */ |
| 501 | int msm_gem_get_and_pin_iova_range(struct drm_gem_object *obj, |
| 502 | struct msm_gem_address_space *aspace, uint64_t *iova, |
| 503 | u64 range_start, u64 range_end) |
| 504 | { |
| 505 | int ret; |
| 506 | |
| 507 | msm_gem_lock(obj); |
| 508 | ret = get_and_pin_iova_range_locked(obj, aspace, iova, range_start, range_end); |
| 509 | msm_gem_unlock(obj); |
| 510 | |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | int msm_gem_get_and_pin_iova_locked(struct drm_gem_object *obj, |
| 515 | struct msm_gem_address_space *aspace, uint64_t *iova) |
| 516 | { |
| 517 | return get_and_pin_iova_range_locked(obj, aspace, iova, 0, U64_MAX); |
| 518 | } |
| 519 | |
Jonathan Marek | d3b8877 | 2020-04-23 17:09:13 -0400 | [diff] [blame] | 520 | /* get iova and pin it. Should have a matching put */ |
| 521 | int msm_gem_get_and_pin_iova(struct drm_gem_object *obj, |
| 522 | struct msm_gem_address_space *aspace, uint64_t *iova) |
| 523 | { |
| 524 | return msm_gem_get_and_pin_iova_range(obj, aspace, iova, 0, U64_MAX); |
| 525 | } |
| 526 | |
Jordan Crouse | 7ad0e8c | 2018-11-07 15:35:51 -0700 | [diff] [blame] | 527 | /* |
| 528 | * Get an iova but don't pin it. Doesn't need a put because iovas are currently |
| 529 | * valid for the life of the object |
| 530 | */ |
Jordan Crouse | 9fe041f | 2018-11-07 15:35:50 -0700 | [diff] [blame] | 531 | int msm_gem_get_iova(struct drm_gem_object *obj, |
| 532 | struct msm_gem_address_space *aspace, uint64_t *iova) |
| 533 | { |
Jordan Crouse | 9fe041f | 2018-11-07 15:35:50 -0700 | [diff] [blame] | 534 | int ret; |
| 535 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 536 | msm_gem_lock(obj); |
Rob Clark | 8117e5e | 2020-10-23 09:51:04 -0700 | [diff] [blame] | 537 | ret = get_iova_locked(obj, aspace, iova, 0, U64_MAX); |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 538 | msm_gem_unlock(obj); |
Jordan Crouse | 9fe041f | 2018-11-07 15:35:50 -0700 | [diff] [blame] | 539 | |
| 540 | return ret; |
| 541 | } |
| 542 | |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 543 | /* get iova without taking a reference, used in places where you have |
Jordan Crouse | 9fe041f | 2018-11-07 15:35:50 -0700 | [diff] [blame] | 544 | * already done a 'msm_gem_get_and_pin_iova' or 'msm_gem_get_iova' |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 545 | */ |
Rob Clark | 8bdcd94 | 2017-06-13 11:07:08 -0400 | [diff] [blame] | 546 | uint64_t msm_gem_iova(struct drm_gem_object *obj, |
| 547 | struct msm_gem_address_space *aspace) |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 548 | { |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 549 | struct msm_gem_vma *vma; |
| 550 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 551 | msm_gem_lock(obj); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 552 | vma = lookup_vma(obj, aspace); |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 553 | msm_gem_unlock(obj); |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 554 | GEM_WARN_ON(!vma); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 555 | |
| 556 | return vma ? vma->iova : 0; |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 557 | } |
| 558 | |
Jordan Crouse | 7ad0e8c | 2018-11-07 15:35:51 -0700 | [diff] [blame] | 559 | /* |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 560 | * Locked variant of msm_gem_unpin_iova() |
| 561 | */ |
| 562 | void msm_gem_unpin_iova_locked(struct drm_gem_object *obj, |
| 563 | struct msm_gem_address_space *aspace) |
| 564 | { |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 565 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 566 | struct msm_gem_vma *vma; |
| 567 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 568 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 569 | |
| 570 | vma = lookup_vma(obj, aspace); |
| 571 | |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 572 | if (!GEM_WARN_ON(!vma)) { |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 573 | msm_gem_unmap_vma(aspace, vma); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 574 | |
| 575 | msm_obj->pin_count--; |
| 576 | GEM_WARN_ON(msm_obj->pin_count < 0); |
| 577 | |
| 578 | update_inactive(msm_obj); |
| 579 | } |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | /* |
Jordan Crouse | 7ad0e8c | 2018-11-07 15:35:51 -0700 | [diff] [blame] | 583 | * Unpin a iova by updating the reference counts. The memory isn't actually |
| 584 | * purged until something else (shrinker, mm_notifier, destroy, etc) decides |
| 585 | * to get rid of it |
| 586 | */ |
| 587 | void msm_gem_unpin_iova(struct drm_gem_object *obj, |
Rob Clark | 8bdcd94 | 2017-06-13 11:07:08 -0400 | [diff] [blame] | 588 | struct msm_gem_address_space *aspace) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 589 | { |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 590 | msm_gem_lock(obj); |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 591 | msm_gem_unpin_iova_locked(obj, aspace); |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 592 | msm_gem_unlock(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 596 | struct drm_mode_create_dumb *args) |
| 597 | { |
| 598 | args->pitch = align_pitch(args->width, args->bpp); |
| 599 | args->size = PAGE_ALIGN(args->pitch * args->height); |
| 600 | return msm_gem_new_handle(dev, file, args->size, |
Jordan Crouse | 0815d77 | 2018-11-07 15:35:52 -0700 | [diff] [blame] | 601 | MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb"); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 602 | } |
| 603 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 604 | int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, |
| 605 | uint32_t handle, uint64_t *offset) |
| 606 | { |
| 607 | struct drm_gem_object *obj; |
| 608 | int ret = 0; |
| 609 | |
| 610 | /* GEM does all our handle to object mapping */ |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 611 | obj = drm_gem_object_lookup(file, handle); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 612 | if (obj == NULL) { |
| 613 | ret = -ENOENT; |
| 614 | goto fail; |
| 615 | } |
| 616 | |
| 617 | *offset = msm_gem_mmap_offset(obj); |
| 618 | |
Emil Velikov | f7d3395 | 2020-05-15 10:51:04 +0100 | [diff] [blame] | 619 | drm_gem_object_put(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 620 | |
| 621 | fail: |
| 622 | return ret; |
| 623 | } |
| 624 | |
Rob Clark | fad33f4 | 2017-09-15 08:38:20 -0400 | [diff] [blame] | 625 | static void *get_vaddr(struct drm_gem_object *obj, unsigned madv) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 626 | { |
Rob Clark | e1e9db2 | 2016-05-27 11:16:28 -0400 | [diff] [blame] | 627 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 628 | int ret = 0; |
| 629 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 630 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 631 | |
Daniel Vetter | 8b6b7d8 | 2020-05-14 22:11:17 +0200 | [diff] [blame] | 632 | if (obj->import_attach) |
| 633 | return ERR_PTR(-ENODEV); |
| 634 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 635 | if (GEM_WARN_ON(msm_obj->madv > madv)) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 636 | DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n", |
Rob Clark | fad33f4 | 2017-09-15 08:38:20 -0400 | [diff] [blame] | 637 | msm_obj->madv, madv); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 638 | return ERR_PTR(-EBUSY); |
| 639 | } |
| 640 | |
| 641 | /* increment vmap_count *before* vmap() call, so shrinker can |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 642 | * check vmap_count (is_vunmapable()) outside of msm_obj lock. |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 643 | * This guarantees that we won't try to msm_gem_vunmap() this |
| 644 | * same object from within the vmap() call (while we already |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 645 | * hold msm_obj lock) |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 646 | */ |
| 647 | msm_obj->vmap_count++; |
| 648 | |
| 649 | if (!msm_obj->vaddr) { |
| 650 | struct page **pages = get_pages(obj); |
| 651 | if (IS_ERR(pages)) { |
| 652 | ret = PTR_ERR(pages); |
| 653 | goto fail; |
| 654 | } |
| 655 | msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT, |
| 656 | VM_MAP, pgprot_writecombine(PAGE_KERNEL)); |
| 657 | if (msm_obj->vaddr == NULL) { |
| 658 | ret = -ENOMEM; |
| 659 | goto fail; |
| 660 | } |
Rob Clark | 10f7616 | 2021-04-26 16:53:25 -0700 | [diff] [blame] | 661 | |
| 662 | update_inactive(msm_obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 663 | } |
| 664 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 665 | return msm_obj->vaddr; |
| 666 | |
| 667 | fail: |
Rob Clark | e1e9db2 | 2016-05-27 11:16:28 -0400 | [diff] [blame] | 668 | msm_obj->vmap_count--; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 669 | return ERR_PTR(ret); |
Rob Clark | 18f2304 | 2016-05-26 16:24:35 -0400 | [diff] [blame] | 670 | } |
| 671 | |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 672 | void *msm_gem_get_vaddr_locked(struct drm_gem_object *obj) |
| 673 | { |
| 674 | return get_vaddr(obj, MSM_MADV_WILLNEED); |
| 675 | } |
| 676 | |
Rob Clark | fad33f4 | 2017-09-15 08:38:20 -0400 | [diff] [blame] | 677 | void *msm_gem_get_vaddr(struct drm_gem_object *obj) |
| 678 | { |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 679 | void *ret; |
| 680 | |
| 681 | msm_gem_lock(obj); |
| 682 | ret = msm_gem_get_vaddr_locked(obj); |
| 683 | msm_gem_unlock(obj); |
| 684 | |
| 685 | return ret; |
Rob Clark | fad33f4 | 2017-09-15 08:38:20 -0400 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | /* |
| 689 | * Don't use this! It is for the very special case of dumping |
| 690 | * submits from GPU hangs or faults, were the bo may already |
| 691 | * be MSM_MADV_DONTNEED, but we know the buffer is still on the |
| 692 | * active list. |
| 693 | */ |
| 694 | void *msm_gem_get_vaddr_active(struct drm_gem_object *obj) |
| 695 | { |
| 696 | return get_vaddr(obj, __MSM_MADV_PURGED); |
| 697 | } |
| 698 | |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 699 | void msm_gem_put_vaddr_locked(struct drm_gem_object *obj) |
Rob Clark | 18f2304 | 2016-05-26 16:24:35 -0400 | [diff] [blame] | 700 | { |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 701 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 702 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 703 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
| 704 | GEM_WARN_ON(msm_obj->vmap_count < 1); |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 705 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 706 | msm_obj->vmap_count--; |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 707 | } |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 708 | |
| 709 | void msm_gem_put_vaddr(struct drm_gem_object *obj) |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 710 | { |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 711 | msm_gem_lock(obj); |
Rob Clark | e4b87d2 | 2020-10-23 09:51:06 -0700 | [diff] [blame] | 712 | msm_gem_put_vaddr_locked(obj); |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 713 | msm_gem_unlock(obj); |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /* Update madvise status, returns true if not purged, else |
| 717 | * false or -errno. |
| 718 | */ |
| 719 | int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv) |
| 720 | { |
| 721 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 722 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 723 | msm_gem_lock(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 724 | |
| 725 | if (msm_obj->madv != __MSM_MADV_PURGED) |
| 726 | msm_obj->madv = madv; |
| 727 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 728 | madv = msm_obj->madv; |
| 729 | |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 730 | /* If the obj is inactive, we might need to move it |
| 731 | * between inactive lists |
| 732 | */ |
| 733 | if (msm_obj->active_count == 0) |
| 734 | update_inactive(msm_obj); |
| 735 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 736 | msm_gem_unlock(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 737 | |
| 738 | return (madv != __MSM_MADV_PURGED); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 739 | } |
| 740 | |
Rob Clark | 599089c | 2020-10-23 09:51:07 -0700 | [diff] [blame] | 741 | void msm_gem_purge(struct drm_gem_object *obj) |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 742 | { |
| 743 | struct drm_device *dev = obj->dev; |
| 744 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 745 | |
Rob Clark | 81d4d59 | 2021-04-05 10:45:30 -0700 | [diff] [blame] | 746 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 747 | GEM_WARN_ON(!is_purgeable(msm_obj)); |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 748 | |
Rob Clark | 20d0ae2 | 2021-04-05 10:45:27 -0700 | [diff] [blame] | 749 | /* Get rid of any iommu mapping(s): */ |
| 750 | put_iova_spaces(obj, true); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 751 | |
Rob Clark | 599089c | 2020-10-23 09:51:07 -0700 | [diff] [blame] | 752 | msm_gem_vunmap(obj); |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 753 | |
Rob Clark | 81d4d59 | 2021-04-05 10:45:30 -0700 | [diff] [blame] | 754 | drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping); |
| 755 | |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 756 | put_pages(obj); |
| 757 | |
Iskren Chernev | 9b73bde | 2020-11-26 15:02:23 +0200 | [diff] [blame] | 758 | put_iova_vmas(obj); |
| 759 | |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 760 | msm_obj->madv = __MSM_MADV_PURGED; |
Rob Clark | 25ed38b | 2021-04-02 14:12:26 -0700 | [diff] [blame] | 761 | update_inactive(msm_obj); |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 762 | |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 763 | drm_gem_free_mmap_offset(obj); |
| 764 | |
| 765 | /* Our goal here is to return as much of the memory as |
| 766 | * is possible back to the system as we are called from OOM. |
| 767 | * To do this we must instruct the shmfs to drop all of its |
| 768 | * backing pages, *now*. |
| 769 | */ |
| 770 | shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1); |
| 771 | |
| 772 | invalidate_mapping_pages(file_inode(obj->filp)->i_mapping, |
| 773 | 0, (loff_t)-1); |
| 774 | } |
| 775 | |
Rob Clark | 63f17ef | 2021-04-05 10:45:31 -0700 | [diff] [blame] | 776 | /** |
| 777 | * Unpin the backing pages and make them available to be swapped out. |
| 778 | */ |
| 779 | void msm_gem_evict(struct drm_gem_object *obj) |
| 780 | { |
| 781 | struct drm_device *dev = obj->dev; |
| 782 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 783 | |
| 784 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
| 785 | GEM_WARN_ON(is_unevictable(msm_obj)); |
| 786 | GEM_WARN_ON(!msm_obj->evictable); |
| 787 | GEM_WARN_ON(msm_obj->active_count); |
| 788 | |
| 789 | /* Get rid of any iommu mapping(s): */ |
| 790 | put_iova_spaces(obj, false); |
| 791 | |
| 792 | drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping); |
| 793 | |
| 794 | put_pages(obj); |
| 795 | |
| 796 | update_inactive(msm_obj); |
| 797 | } |
| 798 | |
Rob Clark | 599089c | 2020-10-23 09:51:07 -0700 | [diff] [blame] | 799 | void msm_gem_vunmap(struct drm_gem_object *obj) |
Rob Clark | e1e9db2 | 2016-05-27 11:16:28 -0400 | [diff] [blame] | 800 | { |
| 801 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 802 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 803 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 804 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 805 | if (!msm_obj->vaddr || GEM_WARN_ON(!is_vunmapable(msm_obj))) |
Rob Clark | e1e9db2 | 2016-05-27 11:16:28 -0400 | [diff] [blame] | 806 | return; |
| 807 | |
| 808 | vunmap(msm_obj->vaddr); |
| 809 | msm_obj->vaddr = NULL; |
| 810 | } |
| 811 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 812 | /* must be called before _move_to_active().. */ |
| 813 | int msm_gem_sync_object(struct drm_gem_object *obj, |
| 814 | struct msm_fence_context *fctx, bool exclusive) |
| 815 | { |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 816 | struct dma_resv_list *fobj; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 817 | struct dma_fence *fence; |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 818 | int i, ret; |
| 819 | |
Christian König | 60f800b | 2021-07-02 13:16:42 +0200 | [diff] [blame^] | 820 | fence = dma_resv_excl_fence(obj->resv); |
| 821 | /* don't need to wait on our own fences, since ring is fifo */ |
| 822 | if (fence && (fence->context != fctx->context)) { |
| 823 | ret = dma_fence_wait(fence, true); |
| 824 | if (ret) |
| 825 | return ret; |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 826 | } |
| 827 | |
Christian König | 60f800b | 2021-07-02 13:16:42 +0200 | [diff] [blame^] | 828 | fobj = dma_resv_shared_list(obj->resv); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 829 | if (!exclusive || !fobj) |
| 830 | return 0; |
| 831 | |
| 832 | for (i = 0; i < fobj->shared_count; i++) { |
| 833 | fence = rcu_dereference_protected(fobj->shared[i], |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 834 | dma_resv_held(obj->resv)); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 835 | if (fence->context != fctx->context) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 836 | ret = dma_fence_wait(fence, true); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 837 | if (ret) |
| 838 | return ret; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | return 0; |
| 843 | } |
| 844 | |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 845 | void msm_gem_active_get(struct drm_gem_object *obj, struct msm_gpu *gpu) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 846 | { |
| 847 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 848 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 849 | |
| 850 | might_sleep(); |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 851 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
| 852 | GEM_WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED); |
| 853 | GEM_WARN_ON(msm_obj->dontneed); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 854 | GEM_WARN_ON(!msm_obj->sgt); |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 855 | |
Rob Clark | ab5c54c | 2020-11-16 09:48:49 -0800 | [diff] [blame] | 856 | if (msm_obj->active_count++ == 0) { |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 857 | mutex_lock(&priv->mm_lock); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 858 | if (msm_obj->evictable) |
| 859 | mark_unevictable(msm_obj); |
Rob Clark | cc8a4d5 | 2021-03-31 18:27:19 -0700 | [diff] [blame] | 860 | list_del(&msm_obj->mm_list); |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 861 | list_add_tail(&msm_obj->mm_list, &gpu->active_list); |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 862 | mutex_unlock(&priv->mm_lock); |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 863 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 864 | } |
| 865 | |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 866 | void msm_gem_active_put(struct drm_gem_object *obj) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 867 | { |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 868 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 869 | |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 870 | might_sleep(); |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 871 | GEM_WARN_ON(!msm_gem_is_locked(obj)); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 872 | |
Rob Clark | ab5c54c | 2020-11-16 09:48:49 -0800 | [diff] [blame] | 873 | if (--msm_obj->active_count == 0) { |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 874 | update_inactive(msm_obj); |
Akhil P Oommen | 9d8baa2 | 2020-09-22 20:25:26 +0530 | [diff] [blame] | 875 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 876 | } |
| 877 | |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 878 | static void update_inactive(struct msm_gem_object *msm_obj) |
| 879 | { |
| 880 | struct msm_drm_private *priv = msm_obj->base.dev->dev_private; |
| 881 | |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 882 | GEM_WARN_ON(!msm_gem_is_locked(&msm_obj->base)); |
| 883 | |
| 884 | if (msm_obj->active_count != 0) |
| 885 | return; |
| 886 | |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 887 | mutex_lock(&priv->mm_lock); |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 888 | |
Rob Clark | cc8a4d5 | 2021-03-31 18:27:19 -0700 | [diff] [blame] | 889 | if (msm_obj->dontneed) |
Rob Clark | 0054eeb | 2021-04-06 08:18:16 -0700 | [diff] [blame] | 890 | mark_unpurgeable(msm_obj); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 891 | if (msm_obj->evictable) |
| 892 | mark_unevictable(msm_obj); |
Rob Clark | cc8a4d5 | 2021-03-31 18:27:19 -0700 | [diff] [blame] | 893 | |
| 894 | list_del(&msm_obj->mm_list); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 895 | if ((msm_obj->madv == MSM_MADV_WILLNEED) && msm_obj->sgt) { |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 896 | list_add_tail(&msm_obj->mm_list, &priv->inactive_willneed); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 897 | mark_evictable(msm_obj); |
Rob Clark | cc8a4d5 | 2021-03-31 18:27:19 -0700 | [diff] [blame] | 898 | } else if (msm_obj->madv == MSM_MADV_DONTNEED) { |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 899 | list_add_tail(&msm_obj->mm_list, &priv->inactive_dontneed); |
Rob Clark | 0054eeb | 2021-04-06 08:18:16 -0700 | [diff] [blame] | 900 | mark_purgeable(msm_obj); |
Rob Clark | cc8a4d5 | 2021-03-31 18:27:19 -0700 | [diff] [blame] | 901 | } else { |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 902 | GEM_WARN_ON((msm_obj->madv != __MSM_MADV_PURGED) && msm_obj->sgt); |
| 903 | list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned); |
Rob Clark | cc8a4d5 | 2021-03-31 18:27:19 -0700 | [diff] [blame] | 904 | } |
Rob Clark | 3edfa30 | 2020-11-16 09:48:51 -0800 | [diff] [blame] | 905 | |
| 906 | mutex_unlock(&priv->mm_lock); |
| 907 | } |
| 908 | |
Rob Clark | ba00c3f | 2016-03-16 18:18:17 -0400 | [diff] [blame] | 909 | int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout) |
| 910 | { |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 911 | bool write = !!(op & MSM_PREP_WRITE); |
Chris Wilson | f755e22 | 2016-08-29 08:08:26 +0100 | [diff] [blame] | 912 | unsigned long remain = |
| 913 | op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout); |
| 914 | long ret; |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 915 | |
Christian König | d3fae3b | 2021-06-02 13:01:15 +0200 | [diff] [blame] | 916 | ret = dma_resv_wait_timeout(obj->resv, write, true, remain); |
Chris Wilson | f755e22 | 2016-08-29 08:08:26 +0100 | [diff] [blame] | 917 | if (ret == 0) |
| 918 | return remain == 0 ? -EBUSY : -ETIMEDOUT; |
| 919 | else if (ret < 0) |
| 920 | return ret; |
Rob Clark | ba00c3f | 2016-03-16 18:18:17 -0400 | [diff] [blame] | 921 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 922 | /* TODO cache maintenance */ |
| 923 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 924 | return 0; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | int msm_gem_cpu_fini(struct drm_gem_object *obj) |
| 928 | { |
| 929 | /* TODO cache maintenance */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 930 | return 0; |
| 931 | } |
| 932 | |
| 933 | #ifdef CONFIG_DEBUG_FS |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 934 | static void describe_fence(struct dma_fence *fence, const char *type, |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 935 | struct seq_file *m) |
| 936 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 937 | if (!dma_fence_is_signaled(fence)) |
Dave Airlie | a311562 | 2019-01-10 06:20:15 +1000 | [diff] [blame] | 938 | seq_printf(m, "\t%9s: %s %s seq %llu\n", type, |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 939 | fence->ops->get_driver_name(fence), |
| 940 | fence->ops->get_timeline_name(fence), |
| 941 | fence->seqno); |
| 942 | } |
| 943 | |
Rob Clark | 528107c | 2021-03-31 18:27:21 -0700 | [diff] [blame] | 944 | void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m, |
| 945 | struct msm_gem_stats *stats) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 946 | { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 947 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Christian König | 52791ee | 2019-08-11 10:06:32 +0200 | [diff] [blame] | 948 | struct dma_resv *robj = obj->resv; |
| 949 | struct dma_resv_list *fobj; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 950 | struct dma_fence *fence; |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 951 | struct msm_gem_vma *vma; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 952 | uint64_t off = drm_vma_node_start(&obj->vma_node); |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 953 | const char *madv; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 954 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 955 | msm_gem_lock(obj); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 956 | |
Rob Clark | 528107c | 2021-03-31 18:27:21 -0700 | [diff] [blame] | 957 | stats->all.count++; |
| 958 | stats->all.size += obj->size; |
| 959 | |
| 960 | if (is_active(msm_obj)) { |
| 961 | stats->active.count++; |
| 962 | stats->active.size += obj->size; |
| 963 | } |
| 964 | |
Rob Clark | f48f356 | 2021-04-05 10:45:28 -0700 | [diff] [blame] | 965 | if (msm_obj->pages) { |
| 966 | stats->resident.count++; |
| 967 | stats->resident.size += obj->size; |
| 968 | } |
| 969 | |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 970 | switch (msm_obj->madv) { |
| 971 | case __MSM_MADV_PURGED: |
Rob Clark | 528107c | 2021-03-31 18:27:21 -0700 | [diff] [blame] | 972 | stats->purged.count++; |
| 973 | stats->purged.size += obj->size; |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 974 | madv = " purged"; |
| 975 | break; |
| 976 | case MSM_MADV_DONTNEED: |
Rob Clark | 0054eeb | 2021-04-06 08:18:16 -0700 | [diff] [blame] | 977 | stats->purgeable.count++; |
| 978 | stats->purgeable.size += obj->size; |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 979 | madv = " purgeable"; |
| 980 | break; |
| 981 | case MSM_MADV_WILLNEED: |
| 982 | default: |
| 983 | madv = ""; |
| 984 | break; |
| 985 | } |
| 986 | |
Jordan Crouse | 575f048 | 2018-11-07 15:35:49 -0700 | [diff] [blame] | 987 | seq_printf(m, "%08x: %c %2d (%2d) %08llx %p", |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 988 | msm_obj->flags, is_active(msm_obj) ? 'A' : 'I', |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 989 | obj->name, kref_read(&obj->refcount), |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 990 | off, msm_obj->vaddr); |
| 991 | |
Jordan Crouse | 0815d77 | 2018-11-07 15:35:52 -0700 | [diff] [blame] | 992 | seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 993 | |
Jordan Crouse | 575f048 | 2018-11-07 15:35:49 -0700 | [diff] [blame] | 994 | if (!list_empty(&msm_obj->vmas)) { |
| 995 | |
Jordan Crouse | 7ad0e8c | 2018-11-07 15:35:51 -0700 | [diff] [blame] | 996 | seq_puts(m, " vmas:"); |
Jordan Crouse | 575f048 | 2018-11-07 15:35:49 -0700 | [diff] [blame] | 997 | |
Rob Clark | 25faf2f | 2020-08-17 15:01:45 -0700 | [diff] [blame] | 998 | list_for_each_entry(vma, &msm_obj->vmas, list) { |
| 999 | const char *name, *comm; |
| 1000 | if (vma->aspace) { |
| 1001 | struct msm_gem_address_space *aspace = vma->aspace; |
| 1002 | struct task_struct *task = |
| 1003 | get_pid_task(aspace->pid, PIDTYPE_PID); |
| 1004 | if (task) { |
| 1005 | comm = kstrdup(task->comm, GFP_KERNEL); |
| 1006 | } else { |
| 1007 | comm = NULL; |
| 1008 | } |
| 1009 | name = aspace->name; |
| 1010 | } else { |
| 1011 | name = comm = NULL; |
| 1012 | } |
| 1013 | seq_printf(m, " [%s%s%s: aspace=%p, %08llx,%s,inuse=%d]", |
| 1014 | name, comm ? ":" : "", comm ? comm : "", |
| 1015 | vma->aspace, vma->iova, |
| 1016 | vma->mapped ? "mapped" : "unmapped", |
Jordan Crouse | 7ad0e8c | 2018-11-07 15:35:51 -0700 | [diff] [blame] | 1017 | vma->inuse); |
Rob Clark | 25faf2f | 2020-08-17 15:01:45 -0700 | [diff] [blame] | 1018 | kfree(comm); |
| 1019 | } |
Jordan Crouse | 575f048 | 2018-11-07 15:35:49 -0700 | [diff] [blame] | 1020 | |
| 1021 | seq_puts(m, "\n"); |
| 1022 | } |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 1023 | |
| 1024 | rcu_read_lock(); |
Christian König | fb5ce73 | 2021-05-11 14:11:41 +0200 | [diff] [blame] | 1025 | fobj = dma_resv_shared_list(robj); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 1026 | if (fobj) { |
| 1027 | unsigned int i, shared_count = fobj->shared_count; |
| 1028 | |
| 1029 | for (i = 0; i < shared_count; i++) { |
| 1030 | fence = rcu_dereference(fobj->shared[i]); |
| 1031 | describe_fence(fence, "Shared", m); |
| 1032 | } |
| 1033 | } |
| 1034 | |
Christian König | 6edbd6a | 2021-05-10 16:14:09 +0200 | [diff] [blame] | 1035 | fence = dma_resv_excl_fence(robj); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 1036 | if (fence) |
| 1037 | describe_fence(fence, "Exclusive", m); |
| 1038 | rcu_read_unlock(); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1039 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 1040 | msm_gem_unlock(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | void msm_gem_describe_objects(struct list_head *list, struct seq_file *m) |
| 1044 | { |
Rob Clark | 528107c | 2021-03-31 18:27:21 -0700 | [diff] [blame] | 1045 | struct msm_gem_stats stats = {}; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1046 | struct msm_gem_object *msm_obj; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1047 | |
Jordan Crouse | 0815d77 | 2018-11-07 15:35:52 -0700 | [diff] [blame] | 1048 | seq_puts(m, " flags id ref offset kaddr size madv name\n"); |
Rob Clark | 6ed0897 | 2021-03-31 18:27:20 -0700 | [diff] [blame] | 1049 | list_for_each_entry(msm_obj, list, node) { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1050 | struct drm_gem_object *obj = &msm_obj->base; |
Jordan Crouse | 575f048 | 2018-11-07 15:35:49 -0700 | [diff] [blame] | 1051 | seq_puts(m, " "); |
Rob Clark | 528107c | 2021-03-31 18:27:21 -0700 | [diff] [blame] | 1052 | msm_gem_describe(obj, m, &stats); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1053 | } |
| 1054 | |
Colin Ian King | f1902c6 | 2021-04-06 14:39:39 +0100 | [diff] [blame] | 1055 | seq_printf(m, "Total: %4d objects, %9zu bytes\n", |
Rob Clark | 528107c | 2021-03-31 18:27:21 -0700 | [diff] [blame] | 1056 | stats.all.count, stats.all.size); |
Colin Ian King | f1902c6 | 2021-04-06 14:39:39 +0100 | [diff] [blame] | 1057 | seq_printf(m, "Active: %4d objects, %9zu bytes\n", |
Rob Clark | 528107c | 2021-03-31 18:27:21 -0700 | [diff] [blame] | 1058 | stats.active.count, stats.active.size); |
Rob Clark | f48f356 | 2021-04-05 10:45:28 -0700 | [diff] [blame] | 1059 | seq_printf(m, "Resident: %4d objects, %9zu bytes\n", |
| 1060 | stats.resident.count, stats.resident.size); |
Colin Ian King | f1902c6 | 2021-04-06 14:39:39 +0100 | [diff] [blame] | 1061 | seq_printf(m, "Purgeable: %4d objects, %9zu bytes\n", |
Rob Clark | 0054eeb | 2021-04-06 08:18:16 -0700 | [diff] [blame] | 1062 | stats.purgeable.count, stats.purgeable.size); |
Colin Ian King | f1902c6 | 2021-04-06 14:39:39 +0100 | [diff] [blame] | 1063 | seq_printf(m, "Purged: %4d objects, %9zu bytes\n", |
Rob Clark | 528107c | 2021-03-31 18:27:21 -0700 | [diff] [blame] | 1064 | stats.purged.count, stats.purged.size); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1065 | } |
| 1066 | #endif |
| 1067 | |
Emil Velikov | eecd7fd | 2020-05-15 10:50:51 +0100 | [diff] [blame] | 1068 | /* don't call directly! Use drm_gem_object_put_locked() and friends */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1069 | void msm_gem_free_object(struct drm_gem_object *obj) |
| 1070 | { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1071 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Kristian H. Kristensen | 48e7f18 | 2019-03-20 10:09:08 -0700 | [diff] [blame] | 1072 | struct drm_device *dev = obj->dev; |
| 1073 | struct msm_drm_private *priv = dev->dev_private; |
| 1074 | |
Rob Clark | 6ed0897 | 2021-03-31 18:27:20 -0700 | [diff] [blame] | 1075 | mutex_lock(&priv->obj_lock); |
| 1076 | list_del(&msm_obj->node); |
| 1077 | mutex_unlock(&priv->obj_lock); |
| 1078 | |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 1079 | mutex_lock(&priv->mm_lock); |
Rob Clark | cc8a4d5 | 2021-03-31 18:27:19 -0700 | [diff] [blame] | 1080 | if (msm_obj->dontneed) |
Rob Clark | 0054eeb | 2021-04-06 08:18:16 -0700 | [diff] [blame] | 1081 | mark_unpurgeable(msm_obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1082 | list_del(&msm_obj->mm_list); |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 1083 | mutex_unlock(&priv->mm_lock); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1084 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 1085 | msm_gem_lock(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1086 | |
| 1087 | /* object should not be on active list: */ |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 1088 | GEM_WARN_ON(is_active(msm_obj)); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1089 | |
Rob Clark | 20d0ae2 | 2021-04-05 10:45:27 -0700 | [diff] [blame] | 1090 | put_iova_spaces(obj, true); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1091 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1092 | if (obj->import_attach) { |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 1093 | GEM_WARN_ON(msm_obj->vaddr); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1094 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1095 | /* Don't drop the pages for imported dmabuf, as they are not |
| 1096 | * ours, just free the array we allocated: |
| 1097 | */ |
Jiapeng Zhong | dd5d08b | 2021-01-26 17:51:19 +0800 | [diff] [blame] | 1098 | kvfree(msm_obj->pages); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1099 | |
Rob Clark | 57f0481 | 2020-12-10 09:40:28 -0800 | [diff] [blame] | 1100 | put_iova_vmas(obj); |
| 1101 | |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 1102 | /* dma_buf_detach() grabs resv lock, so we need to unlock |
| 1103 | * prior to drm_prime_gem_destroy |
| 1104 | */ |
| 1105 | msm_gem_unlock(obj); |
| 1106 | |
jilai wang | f28730c | 2015-04-07 13:51:32 -0400 | [diff] [blame] | 1107 | drm_prime_gem_destroy(obj, msm_obj->sgt); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1108 | } else { |
Rob Clark | 599089c | 2020-10-23 09:51:07 -0700 | [diff] [blame] | 1109 | msm_gem_vunmap(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1110 | put_pages(obj); |
Rob Clark | 57f0481 | 2020-12-10 09:40:28 -0800 | [diff] [blame] | 1111 | put_iova_vmas(obj); |
Rob Clark | 6c0e3ea | 2020-10-23 09:51:10 -0700 | [diff] [blame] | 1112 | msm_gem_unlock(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1113 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1114 | |
| 1115 | drm_gem_object_release(obj); |
| 1116 | |
| 1117 | kfree(msm_obj); |
| 1118 | } |
| 1119 | |
| 1120 | /* convenience method to construct a GEM buffer object, and userspace handle */ |
| 1121 | int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, |
Jordan Crouse | 0815d77 | 2018-11-07 15:35:52 -0700 | [diff] [blame] | 1122 | uint32_t size, uint32_t flags, uint32_t *handle, |
| 1123 | char *name) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1124 | { |
| 1125 | struct drm_gem_object *obj; |
| 1126 | int ret; |
| 1127 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1128 | obj = msm_gem_new(dev, size, flags); |
| 1129 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1130 | if (IS_ERR(obj)) |
| 1131 | return PTR_ERR(obj); |
| 1132 | |
Jordan Crouse | 0815d77 | 2018-11-07 15:35:52 -0700 | [diff] [blame] | 1133 | if (name) |
| 1134 | msm_gem_object_set_name(obj, "%s", name); |
| 1135 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1136 | ret = drm_gem_handle_create(file, obj, handle); |
| 1137 | |
| 1138 | /* drop reference from allocate - handle holds it now */ |
Emil Velikov | f7d3395 | 2020-05-15 10:51:04 +0100 | [diff] [blame] | 1139 | drm_gem_object_put(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1140 | |
| 1141 | return ret; |
| 1142 | } |
| 1143 | |
Thomas Zimmermann | 3c9edd9 | 2020-09-23 12:21:46 +0200 | [diff] [blame] | 1144 | static const struct vm_operations_struct vm_ops = { |
| 1145 | .fault = msm_gem_fault, |
| 1146 | .open = drm_gem_vm_open, |
| 1147 | .close = drm_gem_vm_close, |
| 1148 | }; |
| 1149 | |
| 1150 | static const struct drm_gem_object_funcs msm_gem_object_funcs = { |
| 1151 | .free = msm_gem_free_object, |
| 1152 | .pin = msm_gem_prime_pin, |
| 1153 | .unpin = msm_gem_prime_unpin, |
| 1154 | .get_sg_table = msm_gem_prime_get_sg_table, |
| 1155 | .vmap = msm_gem_prime_vmap, |
| 1156 | .vunmap = msm_gem_prime_vunmap, |
| 1157 | .vm_ops = &vm_ops, |
| 1158 | }; |
| 1159 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1160 | static int msm_gem_new_impl(struct drm_device *dev, |
| 1161 | uint32_t size, uint32_t flags, |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1162 | struct drm_gem_object **obj) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1163 | { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1164 | struct msm_gem_object *msm_obj; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1165 | |
| 1166 | switch (flags & MSM_BO_CACHE_MASK) { |
| 1167 | case MSM_BO_UNCACHED: |
| 1168 | case MSM_BO_CACHED: |
| 1169 | case MSM_BO_WC: |
| 1170 | break; |
| 1171 | default: |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 1172 | DRM_DEV_ERROR(dev->dev, "invalid cache flag: %x\n", |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1173 | (flags & MSM_BO_CACHE_MASK)); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1174 | return -EINVAL; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1175 | } |
| 1176 | |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 1177 | msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1178 | if (!msm_obj) |
| 1179 | return -ENOMEM; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1180 | |
| 1181 | msm_obj->flags = flags; |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 1182 | msm_obj->madv = MSM_MADV_WILLNEED; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1183 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 1184 | INIT_LIST_HEAD(&msm_obj->submit_entry); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 1185 | INIT_LIST_HEAD(&msm_obj->vmas); |
| 1186 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1187 | *obj = &msm_obj->base; |
Thomas Zimmermann | 3c9edd9 | 2020-09-23 12:21:46 +0200 | [diff] [blame] | 1188 | (*obj)->funcs = &msm_gem_object_funcs; |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1189 | |
| 1190 | return 0; |
| 1191 | } |
| 1192 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1193 | static struct drm_gem_object *_msm_gem_new(struct drm_device *dev, |
| 1194 | uint32_t size, uint32_t flags, bool struct_mutex_locked) |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1195 | { |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1196 | struct msm_drm_private *priv = dev->dev_private; |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1197 | struct msm_gem_object *msm_obj; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 1198 | struct drm_gem_object *obj = NULL; |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1199 | bool use_vram = false; |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1200 | int ret; |
| 1201 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1202 | size = PAGE_ALIGN(size); |
| 1203 | |
Jonathan Marek | c2052a4 | 2018-11-14 17:08:04 -0500 | [diff] [blame] | 1204 | if (!msm_use_mmu(dev)) |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1205 | use_vram = true; |
Jonathan Marek | 86f46f2 | 2018-11-21 20:52:30 -0500 | [diff] [blame] | 1206 | else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size) |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1207 | use_vram = true; |
| 1208 | |
Rob Clark | 90643a2 | 2021-04-05 10:45:24 -0700 | [diff] [blame] | 1209 | if (GEM_WARN_ON(use_vram && !priv->vram.size)) |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1210 | return ERR_PTR(-EINVAL); |
| 1211 | |
Jordan Crouse | 1a5dff5 | 2017-03-07 10:02:51 -0700 | [diff] [blame] | 1212 | /* Disallow zero sized objects as they make the underlying |
| 1213 | * infrastructure grumpy |
| 1214 | */ |
| 1215 | if (size == 0) |
| 1216 | return ERR_PTR(-EINVAL); |
| 1217 | |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1218 | ret = msm_gem_new_impl(dev, size, flags, &obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1219 | if (ret) |
| 1220 | goto fail; |
| 1221 | |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1222 | msm_obj = to_msm_bo(obj); |
| 1223 | |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1224 | if (use_vram) { |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 1225 | struct msm_gem_vma *vma; |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1226 | struct page **pages; |
Hans Verkuil | b3949a9 | 2017-07-30 14:42:36 +0200 | [diff] [blame] | 1227 | |
Iskren Chernev | a694ffe | 2020-12-28 23:31:30 +0200 | [diff] [blame] | 1228 | drm_gem_private_object_init(dev, obj, size); |
| 1229 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 1230 | msm_gem_lock(obj); |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1231 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 1232 | vma = add_vma(obj, NULL); |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 1233 | msm_gem_unlock(obj); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 1234 | if (IS_ERR(vma)) { |
| 1235 | ret = PTR_ERR(vma); |
| 1236 | goto fail; |
| 1237 | } |
| 1238 | |
| 1239 | to_msm_bo(obj)->vram_node = &vma->node; |
| 1240 | |
Iskren Chernev | 07fcad0 | 2020-12-28 23:31:31 +0200 | [diff] [blame] | 1241 | msm_gem_lock(obj); |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1242 | pages = get_pages(obj); |
Iskren Chernev | 07fcad0 | 2020-12-28 23:31:31 +0200 | [diff] [blame] | 1243 | msm_gem_unlock(obj); |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1244 | if (IS_ERR(pages)) { |
| 1245 | ret = PTR_ERR(pages); |
| 1246 | goto fail; |
| 1247 | } |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 1248 | |
| 1249 | vma->iova = physaddr(obj); |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 1250 | } else { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 1251 | ret = drm_gem_object_init(dev, obj, size); |
| 1252 | if (ret) |
| 1253 | goto fail; |
Lucas Stach | 0abdba4 | 2019-02-28 07:23:29 +0100 | [diff] [blame] | 1254 | /* |
| 1255 | * Our buffers are kept pinned, so allocating them from the |
| 1256 | * MOVABLE zone is a really bad idea, and conflicts with CMA. |
| 1257 | * See comments above new_inode() why this is required _and_ |
| 1258 | * expected if you're going to pin these pages. |
| 1259 | */ |
| 1260 | mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 1261 | } |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1262 | |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 1263 | mutex_lock(&priv->mm_lock); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 1264 | list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned); |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 1265 | mutex_unlock(&priv->mm_lock); |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1266 | |
Rob Clark | 6ed0897 | 2021-03-31 18:27:20 -0700 | [diff] [blame] | 1267 | mutex_lock(&priv->obj_lock); |
| 1268 | list_add_tail(&msm_obj->node, &priv->objects); |
| 1269 | mutex_unlock(&priv->obj_lock); |
| 1270 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1271 | return obj; |
| 1272 | |
| 1273 | fail: |
Rob Clark | ce0a9dc0 | 2020-10-23 09:51:11 -0700 | [diff] [blame] | 1274 | if (struct_mutex_locked) { |
| 1275 | drm_gem_object_put_locked(obj); |
| 1276 | } else { |
| 1277 | drm_gem_object_put(obj); |
| 1278 | } |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1279 | return ERR_PTR(ret); |
| 1280 | } |
| 1281 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1282 | struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev, |
| 1283 | uint32_t size, uint32_t flags) |
| 1284 | { |
| 1285 | return _msm_gem_new(dev, size, flags, true); |
| 1286 | } |
| 1287 | |
| 1288 | struct drm_gem_object *msm_gem_new(struct drm_device *dev, |
| 1289 | uint32_t size, uint32_t flags) |
| 1290 | { |
| 1291 | return _msm_gem_new(dev, size, flags, false); |
| 1292 | } |
| 1293 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1294 | struct drm_gem_object *msm_gem_import(struct drm_device *dev, |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 1295 | struct dma_buf *dmabuf, struct sg_table *sgt) |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1296 | { |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1297 | struct msm_drm_private *priv = dev->dev_private; |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1298 | struct msm_gem_object *msm_obj; |
| 1299 | struct drm_gem_object *obj; |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 1300 | uint32_t size; |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1301 | int ret, npages; |
| 1302 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 1303 | /* if we don't have IOMMU, don't bother pretending we can import: */ |
Jonathan Marek | c2052a4 | 2018-11-14 17:08:04 -0500 | [diff] [blame] | 1304 | if (!msm_use_mmu(dev)) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame] | 1305 | DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n"); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 1306 | return ERR_PTR(-EINVAL); |
| 1307 | } |
| 1308 | |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 1309 | size = PAGE_ALIGN(dmabuf->size); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1310 | |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1311 | ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1312 | if (ret) |
| 1313 | goto fail; |
| 1314 | |
| 1315 | drm_gem_private_object_init(dev, obj, size); |
| 1316 | |
| 1317 | npages = size / PAGE_SIZE; |
| 1318 | |
| 1319 | msm_obj = to_msm_bo(obj); |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 1320 | msm_gem_lock(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1321 | msm_obj->sgt = sgt; |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 1322 | msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1323 | if (!msm_obj->pages) { |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 1324 | msm_gem_unlock(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1325 | ret = -ENOMEM; |
| 1326 | goto fail; |
| 1327 | } |
| 1328 | |
Christian König | c67e627 | 2020-10-08 12:57:32 +0200 | [diff] [blame] | 1329 | ret = drm_prime_sg_to_page_array(sgt, msm_obj->pages, npages); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1330 | if (ret) { |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 1331 | msm_gem_unlock(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1332 | goto fail; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1333 | } |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1334 | |
Rob Clark | a6ae74c | 2020-10-23 09:51:03 -0700 | [diff] [blame] | 1335 | msm_gem_unlock(obj); |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1336 | |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 1337 | mutex_lock(&priv->mm_lock); |
Rob Clark | 64fcbde | 2021-04-05 10:45:29 -0700 | [diff] [blame] | 1338 | list_add_tail(&msm_obj->mm_list, &priv->inactive_unpinned); |
Rob Clark | d984457 | 2020-10-23 09:51:14 -0700 | [diff] [blame] | 1339 | mutex_unlock(&priv->mm_lock); |
Akhil P Oommen | 3cbdc8d | 2020-07-10 02:01:55 +0530 | [diff] [blame] | 1340 | |
Rob Clark | 6ed0897 | 2021-03-31 18:27:20 -0700 | [diff] [blame] | 1341 | mutex_lock(&priv->obj_lock); |
| 1342 | list_add_tail(&msm_obj->node, &priv->objects); |
| 1343 | mutex_unlock(&priv->obj_lock); |
| 1344 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1345 | return obj; |
| 1346 | |
| 1347 | fail: |
Emil Velikov | f7d3395 | 2020-05-15 10:51:04 +0100 | [diff] [blame] | 1348 | drm_gem_object_put(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1349 | return ERR_PTR(ret); |
| 1350 | } |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 1351 | |
| 1352 | static void *_msm_gem_kernel_new(struct drm_device *dev, uint32_t size, |
| 1353 | uint32_t flags, struct msm_gem_address_space *aspace, |
| 1354 | struct drm_gem_object **bo, uint64_t *iova, bool locked) |
| 1355 | { |
| 1356 | void *vaddr; |
| 1357 | struct drm_gem_object *obj = _msm_gem_new(dev, size, flags, locked); |
| 1358 | int ret; |
| 1359 | |
| 1360 | if (IS_ERR(obj)) |
| 1361 | return ERR_CAST(obj); |
| 1362 | |
| 1363 | if (iova) { |
Jordan Crouse | 9fe041f | 2018-11-07 15:35:50 -0700 | [diff] [blame] | 1364 | ret = msm_gem_get_and_pin_iova(obj, aspace, iova); |
Jordan Crouse | 93f7abf | 2018-11-02 09:25:19 -0600 | [diff] [blame] | 1365 | if (ret) |
| 1366 | goto err; |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | vaddr = msm_gem_get_vaddr(obj); |
Wei Yongjun | c9811d0 | 2017-10-11 11:36:56 +0000 | [diff] [blame] | 1370 | if (IS_ERR(vaddr)) { |
Jordan Crouse | 7ad0e8c | 2018-11-07 15:35:51 -0700 | [diff] [blame] | 1371 | msm_gem_unpin_iova(obj, aspace); |
Jordan Crouse | 93f7abf | 2018-11-02 09:25:19 -0600 | [diff] [blame] | 1372 | ret = PTR_ERR(vaddr); |
| 1373 | goto err; |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | if (bo) |
| 1377 | *bo = obj; |
| 1378 | |
| 1379 | return vaddr; |
Jordan Crouse | 93f7abf | 2018-11-02 09:25:19 -0600 | [diff] [blame] | 1380 | err: |
| 1381 | if (locked) |
Emil Velikov | eecd7fd | 2020-05-15 10:50:51 +0100 | [diff] [blame] | 1382 | drm_gem_object_put_locked(obj); |
Jordan Crouse | 93f7abf | 2018-11-02 09:25:19 -0600 | [diff] [blame] | 1383 | else |
Emil Velikov | f7d3395 | 2020-05-15 10:51:04 +0100 | [diff] [blame] | 1384 | drm_gem_object_put(obj); |
Jordan Crouse | 93f7abf | 2018-11-02 09:25:19 -0600 | [diff] [blame] | 1385 | |
| 1386 | return ERR_PTR(ret); |
| 1387 | |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size, |
| 1391 | uint32_t flags, struct msm_gem_address_space *aspace, |
| 1392 | struct drm_gem_object **bo, uint64_t *iova) |
| 1393 | { |
| 1394 | return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, false); |
| 1395 | } |
| 1396 | |
| 1397 | void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size, |
| 1398 | uint32_t flags, struct msm_gem_address_space *aspace, |
| 1399 | struct drm_gem_object **bo, uint64_t *iova) |
| 1400 | { |
| 1401 | return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, true); |
| 1402 | } |
Jordan Crouse | 1e29dff | 2018-11-07 15:35:46 -0700 | [diff] [blame] | 1403 | |
| 1404 | void msm_gem_kernel_put(struct drm_gem_object *bo, |
| 1405 | struct msm_gem_address_space *aspace, bool locked) |
| 1406 | { |
| 1407 | if (IS_ERR_OR_NULL(bo)) |
| 1408 | return; |
| 1409 | |
| 1410 | msm_gem_put_vaddr(bo); |
Jordan Crouse | 7ad0e8c | 2018-11-07 15:35:51 -0700 | [diff] [blame] | 1411 | msm_gem_unpin_iova(bo, aspace); |
Jordan Crouse | 1e29dff | 2018-11-07 15:35:46 -0700 | [diff] [blame] | 1412 | |
| 1413 | if (locked) |
Emil Velikov | eecd7fd | 2020-05-15 10:50:51 +0100 | [diff] [blame] | 1414 | drm_gem_object_put_locked(bo); |
Jordan Crouse | 1e29dff | 2018-11-07 15:35:46 -0700 | [diff] [blame] | 1415 | else |
Emil Velikov | f7d3395 | 2020-05-15 10:51:04 +0100 | [diff] [blame] | 1416 | drm_gem_object_put(bo); |
Jordan Crouse | 1e29dff | 2018-11-07 15:35:46 -0700 | [diff] [blame] | 1417 | } |
Jordan Crouse | 0815d77 | 2018-11-07 15:35:52 -0700 | [diff] [blame] | 1418 | |
| 1419 | void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...) |
| 1420 | { |
| 1421 | struct msm_gem_object *msm_obj = to_msm_bo(bo); |
| 1422 | va_list ap; |
| 1423 | |
| 1424 | if (!fmt) |
| 1425 | return; |
| 1426 | |
| 1427 | va_start(ap, fmt); |
| 1428 | vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap); |
| 1429 | va_end(ap); |
| 1430 | } |