Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 Red Hat |
| 3 | * Author: Rob Clark <robdclark@gmail.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/spinlock.h> |
| 19 | #include <linux/shmem_fs.h> |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 20 | #include <linux/dma-buf.h> |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 21 | #include <linux/pfn_t.h> |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 22 | |
| 23 | #include "msm_drv.h" |
Rob Clark | fde5de6 | 2016-03-15 15:35:08 -0400 | [diff] [blame] | 24 | #include "msm_fence.h" |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 25 | #include "msm_gem.h" |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 26 | #include "msm_gpu.h" |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 27 | #include "msm_mmu.h" |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 28 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 29 | static void msm_gem_vunmap_locked(struct drm_gem_object *obj); |
| 30 | |
| 31 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 32 | static dma_addr_t physaddr(struct drm_gem_object *obj) |
| 33 | { |
| 34 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 35 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 36 | return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) + |
| 37 | priv->vram.paddr; |
| 38 | } |
| 39 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 40 | static bool use_pages(struct drm_gem_object *obj) |
| 41 | { |
| 42 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 43 | return !msm_obj->vram_node; |
| 44 | } |
| 45 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 46 | /* allocate pages from VRAM carveout, used when no IOMMU: */ |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 47 | 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] | 48 | { |
| 49 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 50 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 51 | dma_addr_t paddr; |
| 52 | struct page **p; |
| 53 | int ret, i; |
| 54 | |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 55 | p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 56 | if (!p) |
| 57 | return ERR_PTR(-ENOMEM); |
| 58 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 59 | spin_lock(&priv->vram.lock); |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 60 | 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] | 61 | spin_unlock(&priv->vram.lock); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 62 | if (ret) { |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 63 | kvfree(p); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 64 | return ERR_PTR(ret); |
| 65 | } |
| 66 | |
| 67 | paddr = physaddr(obj); |
| 68 | for (i = 0; i < npages; i++) { |
| 69 | p[i] = phys_to_page(paddr); |
| 70 | paddr += PAGE_SIZE; |
| 71 | } |
| 72 | |
| 73 | return p; |
| 74 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 75 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 76 | static struct page **get_pages(struct drm_gem_object *obj) |
| 77 | { |
| 78 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 79 | |
| 80 | if (!msm_obj->pages) { |
| 81 | struct drm_device *dev = obj->dev; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 82 | struct page **p; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 83 | int npages = obj->size >> PAGE_SHIFT; |
| 84 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 85 | if (use_pages(obj)) |
David Herrmann | 0cdbe8a | 2014-05-25 12:59:47 +0200 | [diff] [blame] | 86 | p = drm_gem_get_pages(obj); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 87 | else |
| 88 | p = get_pages_vram(obj, npages); |
| 89 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 90 | if (IS_ERR(p)) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame^] | 91 | DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n", |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 92 | PTR_ERR(p)); |
| 93 | return p; |
| 94 | } |
| 95 | |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 96 | msm_obj->pages = p; |
| 97 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 98 | msm_obj->sgt = drm_prime_pages_to_sg(p, npages); |
Wei Yongjun | 1f70e07 | 2013-09-11 06:56:12 +0800 | [diff] [blame] | 99 | if (IS_ERR(msm_obj->sgt)) { |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 100 | void *ptr = ERR_CAST(msm_obj->sgt); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 101 | |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame^] | 102 | DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n"); |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 103 | msm_obj->sgt = NULL; |
| 104 | return ptr; |
| 105 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 106 | |
| 107 | /* For non-cached buffers, ensure the new pages are clean |
| 108 | * because display controller, GPU, etc. are not coherent: |
| 109 | */ |
| 110 | if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) |
| 111 | dma_map_sg(dev->dev, msm_obj->sgt->sgl, |
| 112 | msm_obj->sgt->nents, DMA_BIDIRECTIONAL); |
| 113 | } |
| 114 | |
| 115 | return msm_obj->pages; |
| 116 | } |
| 117 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 118 | static void put_pages_vram(struct drm_gem_object *obj) |
| 119 | { |
| 120 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 121 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 122 | |
| 123 | spin_lock(&priv->vram.lock); |
| 124 | drm_mm_remove_node(msm_obj->vram_node); |
| 125 | spin_unlock(&priv->vram.lock); |
| 126 | |
| 127 | kvfree(msm_obj->pages); |
| 128 | } |
| 129 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 130 | static void put_pages(struct drm_gem_object *obj) |
| 131 | { |
| 132 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 133 | |
| 134 | if (msm_obj->pages) { |
Ben Hutchings | 3976626 | 2018-04-03 23:38:45 +0100 | [diff] [blame] | 135 | if (msm_obj->sgt) { |
| 136 | /* For non-cached buffers, ensure the new |
| 137 | * pages are clean because display controller, |
| 138 | * GPU, etc. are not coherent: |
| 139 | */ |
| 140 | if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) |
| 141 | dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl, |
| 142 | msm_obj->sgt->nents, |
| 143 | DMA_BIDIRECTIONAL); |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 144 | |
Prakash Kamliya | 62e3a3e | 2017-12-04 19:10:15 +0530 | [diff] [blame] | 145 | sg_free_table(msm_obj->sgt); |
Ben Hutchings | 3976626 | 2018-04-03 23:38:45 +0100 | [diff] [blame] | 146 | kfree(msm_obj->sgt); |
| 147 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 148 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 149 | if (use_pages(obj)) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 150 | drm_gem_put_pages(obj, msm_obj->pages, true, false); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 151 | else |
| 152 | put_pages_vram(obj); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 153 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 154 | msm_obj->pages = NULL; |
| 155 | } |
| 156 | } |
| 157 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 158 | struct page **msm_gem_get_pages(struct drm_gem_object *obj) |
| 159 | { |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 160 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 161 | struct page **p; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 162 | |
| 163 | mutex_lock(&msm_obj->lock); |
| 164 | |
| 165 | if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) { |
| 166 | mutex_unlock(&msm_obj->lock); |
| 167 | return ERR_PTR(-EBUSY); |
| 168 | } |
| 169 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 170 | p = get_pages(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 171 | mutex_unlock(&msm_obj->lock); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 172 | return p; |
| 173 | } |
| 174 | |
| 175 | void msm_gem_put_pages(struct drm_gem_object *obj) |
| 176 | { |
| 177 | /* when we start tracking the pin count, then do something here */ |
| 178 | } |
| 179 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 180 | int msm_gem_mmap_obj(struct drm_gem_object *obj, |
| 181 | struct vm_area_struct *vma) |
| 182 | { |
| 183 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 184 | |
| 185 | vma->vm_flags &= ~VM_PFNMAP; |
| 186 | vma->vm_flags |= VM_MIXEDMAP; |
| 187 | |
| 188 | if (msm_obj->flags & MSM_BO_WC) { |
| 189 | vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 190 | } else if (msm_obj->flags & MSM_BO_UNCACHED) { |
| 191 | vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 192 | } else { |
| 193 | /* |
| 194 | * Shunt off cached objs to shmem file so they have their own |
| 195 | * address_space (so unmap_mapping_range does what we want, |
| 196 | * in particular in the case of mmap'd dmabufs) |
| 197 | */ |
| 198 | fput(vma->vm_file); |
| 199 | get_file(obj->filp); |
| 200 | vma->vm_pgoff = 0; |
| 201 | vma->vm_file = obj->filp; |
| 202 | |
| 203 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 210 | { |
| 211 | int ret; |
| 212 | |
| 213 | ret = drm_gem_mmap(filp, vma); |
| 214 | if (ret) { |
| 215 | DBG("mmap failed: %d", ret); |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | return msm_gem_mmap_obj(vma->vm_private_data, vma); |
| 220 | } |
| 221 | |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 222 | vm_fault_t msm_gem_fault(struct vm_fault *vmf) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 223 | { |
Dave Jiang | 11bac80 | 2017-02-24 14:56:41 -0800 | [diff] [blame] | 224 | struct vm_area_struct *vma = vmf->vma; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 225 | struct drm_gem_object *obj = vma->vm_private_data; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 226 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 227 | struct page **pages; |
| 228 | unsigned long pfn; |
| 229 | pgoff_t pgoff; |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 230 | int err; |
| 231 | vm_fault_t ret; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 232 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 233 | /* |
| 234 | * vm_ops.open/drm_gem_mmap_obj and close get and put |
| 235 | * a reference on obj. So, we dont need to hold one here. |
Rob Clark | d78d383 | 2016-08-22 15:28:38 -0400 | [diff] [blame] | 236 | */ |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 237 | err = mutex_lock_interruptible(&msm_obj->lock); |
| 238 | if (err) { |
| 239 | ret = VM_FAULT_NOPAGE; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 240 | goto out; |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 241 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 242 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 243 | if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) { |
| 244 | mutex_unlock(&msm_obj->lock); |
| 245 | return VM_FAULT_SIGBUS; |
| 246 | } |
| 247 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 248 | /* make sure we have pages attached now */ |
| 249 | pages = get_pages(obj); |
| 250 | if (IS_ERR(pages)) { |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 251 | ret = vmf_error(PTR_ERR(pages)); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 252 | goto out_unlock; |
| 253 | } |
| 254 | |
| 255 | /* We don't use vmf->pgoff since that has the fake offset: */ |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 256 | pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 257 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 258 | pfn = page_to_pfn(pages[pgoff]); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 259 | |
Jan Kara | 1a29d85 | 2016-12-14 15:07:01 -0800 | [diff] [blame] | 260 | VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 261 | pfn, pfn << PAGE_SHIFT); |
| 262 | |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 263 | 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] | 264 | out_unlock: |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 265 | mutex_unlock(&msm_obj->lock); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 266 | out: |
Souptick Joarder | a5f74ec | 2018-05-21 22:59:48 +0530 | [diff] [blame] | 267 | return ret; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /** get mmap offset */ |
| 271 | static uint64_t mmap_offset(struct drm_gem_object *obj) |
| 272 | { |
| 273 | struct drm_device *dev = obj->dev; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 274 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 275 | int ret; |
| 276 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 277 | WARN_ON(!mutex_is_locked(&msm_obj->lock)); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 278 | |
| 279 | /* Make it mmapable */ |
| 280 | ret = drm_gem_create_mmap_offset(obj); |
| 281 | |
| 282 | if (ret) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame^] | 283 | DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n"); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | return drm_vma_node_offset_addr(&obj->vma_node); |
| 288 | } |
| 289 | |
| 290 | uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj) |
| 291 | { |
| 292 | uint64_t offset; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 293 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 294 | |
| 295 | mutex_lock(&msm_obj->lock); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 296 | offset = mmap_offset(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 297 | mutex_unlock(&msm_obj->lock); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 298 | return offset; |
| 299 | } |
| 300 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 301 | static struct msm_gem_vma *add_vma(struct drm_gem_object *obj, |
| 302 | struct msm_gem_address_space *aspace) |
| 303 | { |
| 304 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 305 | struct msm_gem_vma *vma; |
| 306 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 307 | WARN_ON(!mutex_is_locked(&msm_obj->lock)); |
| 308 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 309 | vma = kzalloc(sizeof(*vma), GFP_KERNEL); |
| 310 | if (!vma) |
| 311 | return ERR_PTR(-ENOMEM); |
| 312 | |
| 313 | vma->aspace = aspace; |
| 314 | |
| 315 | list_add_tail(&vma->list, &msm_obj->vmas); |
| 316 | |
| 317 | return vma; |
| 318 | } |
| 319 | |
| 320 | static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj, |
| 321 | struct msm_gem_address_space *aspace) |
| 322 | { |
| 323 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 324 | struct msm_gem_vma *vma; |
| 325 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 326 | WARN_ON(!mutex_is_locked(&msm_obj->lock)); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 327 | |
| 328 | list_for_each_entry(vma, &msm_obj->vmas, list) { |
| 329 | if (vma->aspace == aspace) |
| 330 | return vma; |
| 331 | } |
| 332 | |
| 333 | return NULL; |
| 334 | } |
| 335 | |
| 336 | static void del_vma(struct msm_gem_vma *vma) |
| 337 | { |
| 338 | if (!vma) |
| 339 | return; |
| 340 | |
| 341 | list_del(&vma->list); |
| 342 | kfree(vma); |
| 343 | } |
| 344 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 345 | /* Called with msm_obj->lock locked */ |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 346 | static void |
| 347 | put_iova(struct drm_gem_object *obj) |
| 348 | { |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 349 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 350 | struct msm_gem_vma *vma, *tmp; |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 351 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 352 | WARN_ON(!mutex_is_locked(&msm_obj->lock)); |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 353 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 354 | list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) { |
| 355 | msm_gem_unmap_vma(vma->aspace, vma, msm_obj->sgt); |
| 356 | del_vma(vma); |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 360 | /* get iova, taking a reference. Should have a matching put */ |
| 361 | int msm_gem_get_iova(struct drm_gem_object *obj, |
Rob Clark | 8bdcd94 | 2017-06-13 11:07:08 -0400 | [diff] [blame] | 362 | struct msm_gem_address_space *aspace, uint64_t *iova) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 363 | { |
| 364 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 365 | struct msm_gem_vma *vma; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 366 | int ret = 0; |
| 367 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 368 | mutex_lock(&msm_obj->lock); |
| 369 | |
| 370 | if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) { |
| 371 | mutex_unlock(&msm_obj->lock); |
| 372 | return -EBUSY; |
| 373 | } |
Rob Clark | cb1e381 | 2017-06-13 09:15:36 -0400 | [diff] [blame] | 374 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 375 | vma = lookup_vma(obj, aspace); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 376 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 377 | if (!vma) { |
| 378 | struct page **pages; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 379 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 380 | vma = add_vma(obj, aspace); |
Dan Carpenter | 71e3dfa | 2017-07-10 10:20:42 +0300 | [diff] [blame] | 381 | if (IS_ERR(vma)) { |
| 382 | ret = PTR_ERR(vma); |
| 383 | goto unlock; |
| 384 | } |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 385 | |
| 386 | pages = get_pages(obj); |
| 387 | if (IS_ERR(pages)) { |
| 388 | ret = PTR_ERR(pages); |
| 389 | goto fail; |
| 390 | } |
| 391 | |
| 392 | ret = msm_gem_map_vma(aspace, vma, msm_obj->sgt, |
| 393 | obj->size >> PAGE_SHIFT); |
| 394 | if (ret) |
| 395 | goto fail; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 396 | } |
| 397 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 398 | *iova = vma->iova; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 399 | |
| 400 | mutex_unlock(&msm_obj->lock); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 401 | return 0; |
| 402 | |
| 403 | fail: |
| 404 | del_vma(vma); |
Dan Carpenter | 71e3dfa | 2017-07-10 10:20:42 +0300 | [diff] [blame] | 405 | unlock: |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 406 | mutex_unlock(&msm_obj->lock); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 407 | return ret; |
| 408 | } |
| 409 | |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 410 | /* get iova without taking a reference, used in places where you have |
| 411 | * already done a 'msm_gem_get_iova()'. |
| 412 | */ |
Rob Clark | 8bdcd94 | 2017-06-13 11:07:08 -0400 | [diff] [blame] | 413 | uint64_t msm_gem_iova(struct drm_gem_object *obj, |
| 414 | struct msm_gem_address_space *aspace) |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 415 | { |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 416 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 417 | struct msm_gem_vma *vma; |
| 418 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 419 | mutex_lock(&msm_obj->lock); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 420 | vma = lookup_vma(obj, aspace); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 421 | mutex_unlock(&msm_obj->lock); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 422 | WARN_ON(!vma); |
| 423 | |
| 424 | return vma ? vma->iova : 0; |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 425 | } |
| 426 | |
Rob Clark | 8bdcd94 | 2017-06-13 11:07:08 -0400 | [diff] [blame] | 427 | void msm_gem_put_iova(struct drm_gem_object *obj, |
| 428 | struct msm_gem_address_space *aspace) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 429 | { |
| 430 | // XXX TODO .. |
| 431 | // NOTE: probably don't need a _locked() version.. we wouldn't |
| 432 | // normally unmap here, but instead just mark that it could be |
| 433 | // unmapped (if the iova refcnt drops to zero), but then later |
| 434 | // if another _get_iova_locked() fails we can start unmapping |
| 435 | // things that are no longer needed.. |
| 436 | } |
| 437 | |
| 438 | int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 439 | struct drm_mode_create_dumb *args) |
| 440 | { |
| 441 | args->pitch = align_pitch(args->width, args->bpp); |
| 442 | args->size = PAGE_ALIGN(args->pitch * args->height); |
| 443 | return msm_gem_new_handle(dev, file, args->size, |
| 444 | MSM_BO_SCANOUT | MSM_BO_WC, &args->handle); |
| 445 | } |
| 446 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 447 | int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, |
| 448 | uint32_t handle, uint64_t *offset) |
| 449 | { |
| 450 | struct drm_gem_object *obj; |
| 451 | int ret = 0; |
| 452 | |
| 453 | /* GEM does all our handle to object mapping */ |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 454 | obj = drm_gem_object_lookup(file, handle); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 455 | if (obj == NULL) { |
| 456 | ret = -ENOENT; |
| 457 | goto fail; |
| 458 | } |
| 459 | |
| 460 | *offset = msm_gem_mmap_offset(obj); |
| 461 | |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 462 | drm_gem_object_put_unlocked(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 463 | |
| 464 | fail: |
| 465 | return ret; |
| 466 | } |
| 467 | |
Rob Clark | fad33f4 | 2017-09-15 08:38:20 -0400 | [diff] [blame] | 468 | static void *get_vaddr(struct drm_gem_object *obj, unsigned madv) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 469 | { |
Rob Clark | e1e9db2 | 2016-05-27 11:16:28 -0400 | [diff] [blame] | 470 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 471 | int ret = 0; |
| 472 | |
| 473 | mutex_lock(&msm_obj->lock); |
| 474 | |
Rob Clark | fad33f4 | 2017-09-15 08:38:20 -0400 | [diff] [blame] | 475 | if (WARN_ON(msm_obj->madv > madv)) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame^] | 476 | 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] | 477 | msm_obj->madv, madv); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 478 | mutex_unlock(&msm_obj->lock); |
| 479 | return ERR_PTR(-EBUSY); |
| 480 | } |
| 481 | |
| 482 | /* increment vmap_count *before* vmap() call, so shrinker can |
| 483 | * check vmap_count (is_vunmapable()) outside of msm_obj->lock. |
| 484 | * This guarantees that we won't try to msm_gem_vunmap() this |
| 485 | * same object from within the vmap() call (while we already |
| 486 | * hold msm_obj->lock) |
| 487 | */ |
| 488 | msm_obj->vmap_count++; |
| 489 | |
| 490 | if (!msm_obj->vaddr) { |
| 491 | struct page **pages = get_pages(obj); |
| 492 | if (IS_ERR(pages)) { |
| 493 | ret = PTR_ERR(pages); |
| 494 | goto fail; |
| 495 | } |
| 496 | msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT, |
| 497 | VM_MAP, pgprot_writecombine(PAGE_KERNEL)); |
| 498 | if (msm_obj->vaddr == NULL) { |
| 499 | ret = -ENOMEM; |
| 500 | goto fail; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | mutex_unlock(&msm_obj->lock); |
| 505 | return msm_obj->vaddr; |
| 506 | |
| 507 | fail: |
Rob Clark | e1e9db2 | 2016-05-27 11:16:28 -0400 | [diff] [blame] | 508 | msm_obj->vmap_count--; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 509 | mutex_unlock(&msm_obj->lock); |
| 510 | return ERR_PTR(ret); |
Rob Clark | 18f2304 | 2016-05-26 16:24:35 -0400 | [diff] [blame] | 511 | } |
| 512 | |
Rob Clark | fad33f4 | 2017-09-15 08:38:20 -0400 | [diff] [blame] | 513 | void *msm_gem_get_vaddr(struct drm_gem_object *obj) |
| 514 | { |
| 515 | return get_vaddr(obj, MSM_MADV_WILLNEED); |
| 516 | } |
| 517 | |
| 518 | /* |
| 519 | * Don't use this! It is for the very special case of dumping |
| 520 | * submits from GPU hangs or faults, were the bo may already |
| 521 | * be MSM_MADV_DONTNEED, but we know the buffer is still on the |
| 522 | * active list. |
| 523 | */ |
| 524 | void *msm_gem_get_vaddr_active(struct drm_gem_object *obj) |
| 525 | { |
| 526 | return get_vaddr(obj, __MSM_MADV_PURGED); |
| 527 | } |
| 528 | |
Rob Clark | 18f2304 | 2016-05-26 16:24:35 -0400 | [diff] [blame] | 529 | void msm_gem_put_vaddr(struct drm_gem_object *obj) |
| 530 | { |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 531 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 532 | |
| 533 | mutex_lock(&msm_obj->lock); |
| 534 | WARN_ON(msm_obj->vmap_count < 1); |
| 535 | msm_obj->vmap_count--; |
| 536 | mutex_unlock(&msm_obj->lock); |
Rob Clark | 18f2304 | 2016-05-26 16:24:35 -0400 | [diff] [blame] | 537 | } |
| 538 | |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 539 | /* Update madvise status, returns true if not purged, else |
| 540 | * false or -errno. |
| 541 | */ |
| 542 | int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv) |
| 543 | { |
| 544 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 545 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 546 | mutex_lock(&msm_obj->lock); |
| 547 | |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 548 | WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); |
| 549 | |
| 550 | if (msm_obj->madv != __MSM_MADV_PURGED) |
| 551 | msm_obj->madv = madv; |
| 552 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 553 | madv = msm_obj->madv; |
| 554 | |
| 555 | mutex_unlock(&msm_obj->lock); |
| 556 | |
| 557 | return (madv != __MSM_MADV_PURGED); |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 558 | } |
| 559 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 560 | void msm_gem_purge(struct drm_gem_object *obj, enum msm_gem_lock subclass) |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 561 | { |
| 562 | struct drm_device *dev = obj->dev; |
| 563 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 564 | |
| 565 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 566 | WARN_ON(!is_purgeable(msm_obj)); |
| 567 | WARN_ON(obj->import_attach); |
| 568 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 569 | mutex_lock_nested(&msm_obj->lock, subclass); |
| 570 | |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 571 | put_iova(obj); |
| 572 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 573 | msm_gem_vunmap_locked(obj); |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 574 | |
| 575 | put_pages(obj); |
| 576 | |
| 577 | msm_obj->madv = __MSM_MADV_PURGED; |
| 578 | |
| 579 | drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping); |
| 580 | drm_gem_free_mmap_offset(obj); |
| 581 | |
| 582 | /* Our goal here is to return as much of the memory as |
| 583 | * is possible back to the system as we are called from OOM. |
| 584 | * To do this we must instruct the shmfs to drop all of its |
| 585 | * backing pages, *now*. |
| 586 | */ |
| 587 | shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1); |
| 588 | |
| 589 | invalidate_mapping_pages(file_inode(obj->filp)->i_mapping, |
| 590 | 0, (loff_t)-1); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 591 | |
| 592 | mutex_unlock(&msm_obj->lock); |
Rob Clark | 6820939 | 2016-05-17 16:19:32 -0400 | [diff] [blame] | 593 | } |
| 594 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 595 | static void msm_gem_vunmap_locked(struct drm_gem_object *obj) |
Rob Clark | e1e9db2 | 2016-05-27 11:16:28 -0400 | [diff] [blame] | 596 | { |
| 597 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 598 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 599 | WARN_ON(!mutex_is_locked(&msm_obj->lock)); |
| 600 | |
Rob Clark | e1e9db2 | 2016-05-27 11:16:28 -0400 | [diff] [blame] | 601 | if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj))) |
| 602 | return; |
| 603 | |
| 604 | vunmap(msm_obj->vaddr); |
| 605 | msm_obj->vaddr = NULL; |
| 606 | } |
| 607 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 608 | void msm_gem_vunmap(struct drm_gem_object *obj, enum msm_gem_lock subclass) |
| 609 | { |
| 610 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 611 | |
| 612 | mutex_lock_nested(&msm_obj->lock, subclass); |
| 613 | msm_gem_vunmap_locked(obj); |
| 614 | mutex_unlock(&msm_obj->lock); |
| 615 | } |
| 616 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 617 | /* must be called before _move_to_active().. */ |
| 618 | int msm_gem_sync_object(struct drm_gem_object *obj, |
| 619 | struct msm_fence_context *fctx, bool exclusive) |
| 620 | { |
| 621 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 622 | struct reservation_object_list *fobj; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 623 | struct dma_fence *fence; |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 624 | int i, ret; |
| 625 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 626 | fobj = reservation_object_get_list(msm_obj->resv); |
| 627 | if (!fobj || (fobj->shared_count == 0)) { |
| 628 | fence = reservation_object_get_excl(msm_obj->resv); |
| 629 | /* don't need to wait on our own fences, since ring is fifo */ |
| 630 | if (fence && (fence->context != fctx->context)) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 631 | ret = dma_fence_wait(fence, true); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 632 | if (ret) |
| 633 | return ret; |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (!exclusive || !fobj) |
| 638 | return 0; |
| 639 | |
| 640 | for (i = 0; i < fobj->shared_count; i++) { |
| 641 | fence = rcu_dereference_protected(fobj->shared[i], |
| 642 | reservation_object_held(msm_obj->resv)); |
| 643 | if (fence->context != fctx->context) { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 644 | ret = dma_fence_wait(fence, true); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 645 | if (ret) |
| 646 | return ret; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 653 | void msm_gem_move_to_active(struct drm_gem_object *obj, |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 654 | struct msm_gpu *gpu, bool exclusive, struct dma_fence *fence) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 655 | { |
| 656 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 657 | WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 658 | msm_obj->gpu = gpu; |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 659 | if (exclusive) |
| 660 | reservation_object_add_excl_fence(msm_obj->resv, fence); |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 661 | else |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 662 | reservation_object_add_shared_fence(msm_obj->resv, fence); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 663 | list_del_init(&msm_obj->mm_list); |
| 664 | list_add_tail(&msm_obj->mm_list, &gpu->active_list); |
| 665 | } |
| 666 | |
| 667 | void msm_gem_move_to_inactive(struct drm_gem_object *obj) |
| 668 | { |
| 669 | struct drm_device *dev = obj->dev; |
| 670 | struct msm_drm_private *priv = dev->dev_private; |
| 671 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 672 | |
| 673 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 674 | |
| 675 | msm_obj->gpu = NULL; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 676 | list_del_init(&msm_obj->mm_list); |
| 677 | list_add_tail(&msm_obj->mm_list, &priv->inactive_list); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 678 | } |
| 679 | |
Rob Clark | ba00c3f | 2016-03-16 18:18:17 -0400 | [diff] [blame] | 680 | int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout) |
| 681 | { |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 682 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 683 | bool write = !!(op & MSM_PREP_WRITE); |
Chris Wilson | f755e22 | 2016-08-29 08:08:26 +0100 | [diff] [blame] | 684 | unsigned long remain = |
| 685 | op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout); |
| 686 | long ret; |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 687 | |
Chris Wilson | f755e22 | 2016-08-29 08:08:26 +0100 | [diff] [blame] | 688 | ret = reservation_object_wait_timeout_rcu(msm_obj->resv, write, |
| 689 | true, remain); |
| 690 | if (ret == 0) |
| 691 | return remain == 0 ? -EBUSY : -ETIMEDOUT; |
| 692 | else if (ret < 0) |
| 693 | return ret; |
Rob Clark | ba00c3f | 2016-03-16 18:18:17 -0400 | [diff] [blame] | 694 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 695 | /* TODO cache maintenance */ |
| 696 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 697 | return 0; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | int msm_gem_cpu_fini(struct drm_gem_object *obj) |
| 701 | { |
| 702 | /* TODO cache maintenance */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | #ifdef CONFIG_DEBUG_FS |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 707 | static void describe_fence(struct dma_fence *fence, const char *type, |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 708 | struct seq_file *m) |
| 709 | { |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 710 | if (!dma_fence_is_signaled(fence)) |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 711 | seq_printf(m, "\t%9s: %s %s seq %u\n", type, |
| 712 | fence->ops->get_driver_name(fence), |
| 713 | fence->ops->get_timeline_name(fence), |
| 714 | fence->seqno); |
| 715 | } |
| 716 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 717 | void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m) |
| 718 | { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 719 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 720 | struct reservation_object *robj = msm_obj->resv; |
| 721 | struct reservation_object_list *fobj; |
Chris Wilson | f54d186 | 2016-10-25 13:00:45 +0100 | [diff] [blame] | 722 | struct dma_fence *fence; |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 723 | struct msm_gem_vma *vma; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 724 | uint64_t off = drm_vma_node_start(&obj->vma_node); |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 725 | const char *madv; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 726 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 727 | mutex_lock(&msm_obj->lock); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 728 | |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 729 | switch (msm_obj->madv) { |
| 730 | case __MSM_MADV_PURGED: |
| 731 | madv = " purged"; |
| 732 | break; |
| 733 | case MSM_MADV_DONTNEED: |
| 734 | madv = " purgeable"; |
| 735 | break; |
| 736 | case MSM_MADV_WILLNEED: |
| 737 | default: |
| 738 | madv = ""; |
| 739 | break; |
| 740 | } |
| 741 | |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 742 | seq_printf(m, "%08x: %c %2d (%2d) %08llx %p\t", |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 743 | msm_obj->flags, is_active(msm_obj) ? 'A' : 'I', |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 744 | obj->name, kref_read(&obj->refcount), |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 745 | off, msm_obj->vaddr); |
| 746 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 747 | /* FIXME: we need to print the address space here too */ |
| 748 | list_for_each_entry(vma, &msm_obj->vmas, list) |
| 749 | seq_printf(m, " %08llx", vma->iova); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 750 | |
| 751 | seq_printf(m, " %zu%s\n", obj->size, madv); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 752 | |
| 753 | rcu_read_lock(); |
| 754 | fobj = rcu_dereference(robj->fence); |
| 755 | if (fobj) { |
| 756 | unsigned int i, shared_count = fobj->shared_count; |
| 757 | |
| 758 | for (i = 0; i < shared_count; i++) { |
| 759 | fence = rcu_dereference(fobj->shared[i]); |
| 760 | describe_fence(fence, "Shared", m); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | fence = rcu_dereference(robj->fence_excl); |
| 765 | if (fence) |
| 766 | describe_fence(fence, "Exclusive", m); |
| 767 | rcu_read_unlock(); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 768 | |
| 769 | mutex_unlock(&msm_obj->lock); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 770 | } |
| 771 | |
| 772 | void msm_gem_describe_objects(struct list_head *list, struct seq_file *m) |
| 773 | { |
| 774 | struct msm_gem_object *msm_obj; |
| 775 | int count = 0; |
| 776 | size_t size = 0; |
| 777 | |
| 778 | list_for_each_entry(msm_obj, list, mm_list) { |
| 779 | struct drm_gem_object *obj = &msm_obj->base; |
| 780 | seq_printf(m, " "); |
| 781 | msm_gem_describe(obj, m); |
| 782 | count++; |
| 783 | size += obj->size; |
| 784 | } |
| 785 | |
| 786 | seq_printf(m, "Total %d objects, %zu bytes\n", count, size); |
| 787 | } |
| 788 | #endif |
| 789 | |
Rob Clark | d71b6bd | 2018-02-14 11:14:23 -0500 | [diff] [blame] | 790 | /* don't call directly! Use drm_gem_object_put() and friends */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 791 | void msm_gem_free_object(struct drm_gem_object *obj) |
| 792 | { |
| 793 | struct drm_device *dev = obj->dev; |
| 794 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 795 | |
| 796 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 797 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 798 | /* object should not be on active list: */ |
| 799 | WARN_ON(is_active(msm_obj)); |
| 800 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 801 | list_del(&msm_obj->mm_list); |
| 802 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 803 | mutex_lock(&msm_obj->lock); |
| 804 | |
Rob Clark | 4fe5f65 | 2016-06-01 11:38:28 -0400 | [diff] [blame] | 805 | put_iova(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 806 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 807 | if (obj->import_attach) { |
| 808 | if (msm_obj->vaddr) |
| 809 | dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 810 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 811 | /* Don't drop the pages for imported dmabuf, as they are not |
| 812 | * ours, just free the array we allocated: |
| 813 | */ |
| 814 | if (msm_obj->pages) |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 815 | kvfree(msm_obj->pages); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 816 | |
jilai wang | f28730c | 2015-04-07 13:51:32 -0400 | [diff] [blame] | 817 | drm_prime_gem_destroy(obj, msm_obj->sgt); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 818 | } else { |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 819 | msm_gem_vunmap_locked(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 820 | put_pages(obj); |
| 821 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 822 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 823 | if (msm_obj->resv == &msm_obj->_resv) |
| 824 | reservation_object_fini(msm_obj->resv); |
| 825 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 826 | drm_gem_object_release(obj); |
| 827 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 828 | mutex_unlock(&msm_obj->lock); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 829 | kfree(msm_obj); |
| 830 | } |
| 831 | |
| 832 | /* convenience method to construct a GEM buffer object, and userspace handle */ |
| 833 | int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, |
| 834 | uint32_t size, uint32_t flags, uint32_t *handle) |
| 835 | { |
| 836 | struct drm_gem_object *obj; |
| 837 | int ret; |
| 838 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 839 | obj = msm_gem_new(dev, size, flags); |
| 840 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 841 | if (IS_ERR(obj)) |
| 842 | return PTR_ERR(obj); |
| 843 | |
| 844 | ret = drm_gem_handle_create(file, obj, handle); |
| 845 | |
| 846 | /* drop reference from allocate - handle holds it now */ |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 847 | drm_gem_object_put_unlocked(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 848 | |
| 849 | return ret; |
| 850 | } |
| 851 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 852 | static int msm_gem_new_impl(struct drm_device *dev, |
| 853 | uint32_t size, uint32_t flags, |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 854 | struct reservation_object *resv, |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 855 | struct drm_gem_object **obj, |
| 856 | bool struct_mutex_locked) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 857 | { |
| 858 | struct msm_drm_private *priv = dev->dev_private; |
| 859 | struct msm_gem_object *msm_obj; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 860 | |
| 861 | switch (flags & MSM_BO_CACHE_MASK) { |
| 862 | case MSM_BO_UNCACHED: |
| 863 | case MSM_BO_CACHED: |
| 864 | case MSM_BO_WC: |
| 865 | break; |
| 866 | default: |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame^] | 867 | DRM_DEV_ERROR(dev->dev, "invalid cache flag: %x\n", |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 868 | (flags & MSM_BO_CACHE_MASK)); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 869 | return -EINVAL; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 870 | } |
| 871 | |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 872 | msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 873 | if (!msm_obj) |
| 874 | return -ENOMEM; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 875 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 876 | mutex_init(&msm_obj->lock); |
| 877 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 878 | msm_obj->flags = flags; |
Rob Clark | 4cd33c4 | 2016-05-17 15:44:49 -0400 | [diff] [blame] | 879 | msm_obj->madv = MSM_MADV_WILLNEED; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 880 | |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 881 | if (resv) { |
| 882 | msm_obj->resv = resv; |
| 883 | } else { |
| 884 | msm_obj->resv = &msm_obj->_resv; |
| 885 | reservation_object_init(msm_obj->resv); |
| 886 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 887 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 888 | INIT_LIST_HEAD(&msm_obj->submit_entry); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 889 | INIT_LIST_HEAD(&msm_obj->vmas); |
| 890 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 891 | if (struct_mutex_locked) { |
| 892 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 893 | list_add_tail(&msm_obj->mm_list, &priv->inactive_list); |
| 894 | } else { |
| 895 | mutex_lock(&dev->struct_mutex); |
| 896 | list_add_tail(&msm_obj->mm_list, &priv->inactive_list); |
| 897 | mutex_unlock(&dev->struct_mutex); |
| 898 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 899 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 900 | *obj = &msm_obj->base; |
| 901 | |
| 902 | return 0; |
| 903 | } |
| 904 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 905 | static struct drm_gem_object *_msm_gem_new(struct drm_device *dev, |
| 906 | uint32_t size, uint32_t flags, bool struct_mutex_locked) |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 907 | { |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 908 | struct msm_drm_private *priv = dev->dev_private; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 909 | struct drm_gem_object *obj = NULL; |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 910 | bool use_vram = false; |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 911 | int ret; |
| 912 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 913 | size = PAGE_ALIGN(size); |
| 914 | |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 915 | if (!iommu_present(&platform_bus_type)) |
| 916 | use_vram = true; |
| 917 | else if ((flags & MSM_BO_STOLEN) && priv->vram.size) |
| 918 | use_vram = true; |
| 919 | |
| 920 | if (WARN_ON(use_vram && !priv->vram.size)) |
| 921 | return ERR_PTR(-EINVAL); |
| 922 | |
Jordan Crouse | 1a5dff5 | 2017-03-07 10:02:51 -0700 | [diff] [blame] | 923 | /* Disallow zero sized objects as they make the underlying |
| 924 | * infrastructure grumpy |
| 925 | */ |
| 926 | if (size == 0) |
| 927 | return ERR_PTR(-EINVAL); |
| 928 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 929 | ret = msm_gem_new_impl(dev, size, flags, NULL, &obj, struct_mutex_locked); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 930 | if (ret) |
| 931 | goto fail; |
| 932 | |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 933 | if (use_vram) { |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 934 | struct msm_gem_vma *vma; |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 935 | struct page **pages; |
Hans Verkuil | b3949a9 | 2017-07-30 14:42:36 +0200 | [diff] [blame] | 936 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 937 | |
| 938 | mutex_lock(&msm_obj->lock); |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 939 | |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 940 | vma = add_vma(obj, NULL); |
Hans Verkuil | b3949a9 | 2017-07-30 14:42:36 +0200 | [diff] [blame] | 941 | mutex_unlock(&msm_obj->lock); |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 942 | if (IS_ERR(vma)) { |
| 943 | ret = PTR_ERR(vma); |
| 944 | goto fail; |
| 945 | } |
| 946 | |
| 947 | to_msm_bo(obj)->vram_node = &vma->node; |
| 948 | |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 949 | drm_gem_private_object_init(dev, obj, size); |
| 950 | |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 951 | pages = get_pages(obj); |
| 952 | if (IS_ERR(pages)) { |
| 953 | ret = PTR_ERR(pages); |
| 954 | goto fail; |
| 955 | } |
Rob Clark | 4b85f7f | 2017-06-13 13:54:13 -0400 | [diff] [blame] | 956 | |
| 957 | vma->iova = physaddr(obj); |
Rob Clark | f4839bd | 2017-06-13 11:50:05 -0400 | [diff] [blame] | 958 | } else { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 959 | ret = drm_gem_object_init(dev, obj, size); |
| 960 | if (ret) |
| 961 | goto fail; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 962 | } |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 963 | |
| 964 | return obj; |
| 965 | |
| 966 | fail: |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 967 | drm_gem_object_put_unlocked(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 968 | return ERR_PTR(ret); |
| 969 | } |
| 970 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 971 | struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev, |
| 972 | uint32_t size, uint32_t flags) |
| 973 | { |
| 974 | return _msm_gem_new(dev, size, flags, true); |
| 975 | } |
| 976 | |
| 977 | struct drm_gem_object *msm_gem_new(struct drm_device *dev, |
| 978 | uint32_t size, uint32_t flags) |
| 979 | { |
| 980 | return _msm_gem_new(dev, size, flags, false); |
| 981 | } |
| 982 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 983 | struct drm_gem_object *msm_gem_import(struct drm_device *dev, |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 984 | struct dma_buf *dmabuf, struct sg_table *sgt) |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 985 | { |
| 986 | struct msm_gem_object *msm_obj; |
| 987 | struct drm_gem_object *obj; |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 988 | uint32_t size; |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 989 | int ret, npages; |
| 990 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 991 | /* if we don't have IOMMU, don't bother pretending we can import: */ |
| 992 | if (!iommu_present(&platform_bus_type)) { |
Mamta Shukla | 6a41da1 | 2018-10-20 23:19:26 +0530 | [diff] [blame^] | 993 | DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n"); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 994 | return ERR_PTR(-EINVAL); |
| 995 | } |
| 996 | |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 997 | size = PAGE_ALIGN(dmabuf->size); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 998 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 999 | ret = msm_gem_new_impl(dev, size, MSM_BO_WC, dmabuf->resv, &obj, false); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1000 | if (ret) |
| 1001 | goto fail; |
| 1002 | |
| 1003 | drm_gem_private_object_init(dev, obj, size); |
| 1004 | |
| 1005 | npages = size / PAGE_SIZE; |
| 1006 | |
| 1007 | msm_obj = to_msm_bo(obj); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1008 | mutex_lock(&msm_obj->lock); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1009 | msm_obj->sgt = sgt; |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 1010 | msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1011 | if (!msm_obj->pages) { |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1012 | mutex_unlock(&msm_obj->lock); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1013 | ret = -ENOMEM; |
| 1014 | goto fail; |
| 1015 | } |
| 1016 | |
| 1017 | ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1018 | if (ret) { |
| 1019 | mutex_unlock(&msm_obj->lock); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1020 | goto fail; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1021 | } |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 1022 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 1023 | mutex_unlock(&msm_obj->lock); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1024 | return obj; |
| 1025 | |
| 1026 | fail: |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 1027 | drm_gem_object_put_unlocked(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 1028 | return ERR_PTR(ret); |
| 1029 | } |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 1030 | |
| 1031 | static void *_msm_gem_kernel_new(struct drm_device *dev, uint32_t size, |
| 1032 | uint32_t flags, struct msm_gem_address_space *aspace, |
| 1033 | struct drm_gem_object **bo, uint64_t *iova, bool locked) |
| 1034 | { |
| 1035 | void *vaddr; |
| 1036 | struct drm_gem_object *obj = _msm_gem_new(dev, size, flags, locked); |
| 1037 | int ret; |
| 1038 | |
| 1039 | if (IS_ERR(obj)) |
| 1040 | return ERR_CAST(obj); |
| 1041 | |
| 1042 | if (iova) { |
| 1043 | ret = msm_gem_get_iova(obj, aspace, iova); |
| 1044 | if (ret) { |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 1045 | drm_gem_object_put(obj); |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 1046 | return ERR_PTR(ret); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | vaddr = msm_gem_get_vaddr(obj); |
Wei Yongjun | c9811d0 | 2017-10-11 11:36:56 +0000 | [diff] [blame] | 1051 | if (IS_ERR(vaddr)) { |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 1052 | msm_gem_put_iova(obj, aspace); |
Steve Kowalik | dc9a9b3 | 2018-01-26 14:55:54 +1100 | [diff] [blame] | 1053 | drm_gem_object_put(obj); |
Wei Yongjun | c9811d0 | 2017-10-11 11:36:56 +0000 | [diff] [blame] | 1054 | return ERR_CAST(vaddr); |
Jordan Crouse | 8223286 | 2017-07-27 10:42:40 -0600 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | if (bo) |
| 1058 | *bo = obj; |
| 1059 | |
| 1060 | return vaddr; |
| 1061 | } |
| 1062 | |
| 1063 | void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size, |
| 1064 | uint32_t flags, struct msm_gem_address_space *aspace, |
| 1065 | struct drm_gem_object **bo, uint64_t *iova) |
| 1066 | { |
| 1067 | return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, false); |
| 1068 | } |
| 1069 | |
| 1070 | void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size, |
| 1071 | uint32_t flags, struct msm_gem_address_space *aspace, |
| 1072 | struct drm_gem_object **bo, uint64_t *iova) |
| 1073 | { |
| 1074 | return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, true); |
| 1075 | } |