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