blob: 7263f4373f07c9b9899c55f4a60a6142905d914e [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Rob Clarkc8afe682013-06-26 12:44:06 -04002/*
3 * Copyright (C) 2013 Red Hat
4 * Author: Rob Clark <robdclark@gmail.com>
Rob Clarkc8afe682013-06-26 12:44:06 -04005 */
6
7#include <linux/spinlock.h>
8#include <linux/shmem_fs.h>
Rob Clark05b84912013-09-28 11:28:35 -04009#include <linux/dma-buf.h>
Dan Williams01c8f1c2016-01-15 16:56:40 -080010#include <linux/pfn_t.h>
Rob Clarkc8afe682013-06-26 12:44:06 -040011
Sam Ravnborgfeea39a2019-08-04 08:55:51 +020012#include <drm/drm_prime.h>
13
Rob Clarkc8afe682013-06-26 12:44:06 -040014#include "msm_drv.h"
Rob Clarkfde5de62016-03-15 15:35:08 -040015#include "msm_fence.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040016#include "msm_gem.h"
Rob Clark7198e6b2013-07-19 12:59:32 -040017#include "msm_gpu.h"
Rob Clark871d8122013-11-16 12:56:06 -050018#include "msm_mmu.h"
Rob Clarkc8afe682013-06-26 12:44:06 -040019
Sushmita Susheelendra0e082702017-06-13 16:52:54 -060020static void msm_gem_vunmap_locked(struct drm_gem_object *obj);
21
22
Rob Clark871d8122013-11-16 12:56:06 -050023static 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 Clark072f1f92015-03-03 15:04:25 -050031static 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 Clark3de433c2019-07-30 14:46:28 -070037/*
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
51static void sync_for_device(struct msm_gem_object *msm_obj)
52{
53 struct device *dev = msm_obj->base.dev->dev;
54
55 if (get_dma_ops(dev)) {
56 dma_sync_sg_for_device(dev, msm_obj->sgt->sgl,
57 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
58 } else {
59 dma_map_sg(dev, msm_obj->sgt->sgl,
60 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
61 }
62}
63
64static void sync_for_cpu(struct msm_gem_object *msm_obj)
65{
66 struct device *dev = msm_obj->base.dev->dev;
67
68 if (get_dma_ops(dev)) {
69 dma_sync_sg_for_cpu(dev, msm_obj->sgt->sgl,
70 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
71 } else {
72 dma_unmap_sg(dev, msm_obj->sgt->sgl,
73 msm_obj->sgt->nents, DMA_BIDIRECTIONAL);
74 }
75}
76
Rob Clark871d8122013-11-16 12:56:06 -050077/* allocate pages from VRAM carveout, used when no IOMMU: */
Sushmita Susheelendra0e082702017-06-13 16:52:54 -060078static struct page **get_pages_vram(struct drm_gem_object *obj, int npages)
Rob Clark871d8122013-11-16 12:56:06 -050079{
80 struct msm_gem_object *msm_obj = to_msm_bo(obj);
81 struct msm_drm_private *priv = obj->dev->dev_private;
82 dma_addr_t paddr;
83 struct page **p;
84 int ret, i;
85
Michal Hocko20981052017-05-17 14:23:12 +020086 p = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
Rob Clark871d8122013-11-16 12:56:06 -050087 if (!p)
88 return ERR_PTR(-ENOMEM);
89
Sushmita Susheelendra0e082702017-06-13 16:52:54 -060090 spin_lock(&priv->vram.lock);
Chris Wilson4e64e552017-02-02 21:04:38 +000091 ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, npages);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -060092 spin_unlock(&priv->vram.lock);
Rob Clark871d8122013-11-16 12:56:06 -050093 if (ret) {
Michal Hocko20981052017-05-17 14:23:12 +020094 kvfree(p);
Rob Clark871d8122013-11-16 12:56:06 -050095 return ERR_PTR(ret);
96 }
97
98 paddr = physaddr(obj);
99 for (i = 0; i < npages; i++) {
100 p[i] = phys_to_page(paddr);
101 paddr += PAGE_SIZE;
102 }
103
104 return p;
105}
Rob Clarkc8afe682013-06-26 12:44:06 -0400106
Rob Clarkc8afe682013-06-26 12:44:06 -0400107static struct page **get_pages(struct drm_gem_object *obj)
108{
109 struct msm_gem_object *msm_obj = to_msm_bo(obj);
110
111 if (!msm_obj->pages) {
112 struct drm_device *dev = obj->dev;
Rob Clark871d8122013-11-16 12:56:06 -0500113 struct page **p;
Rob Clarkc8afe682013-06-26 12:44:06 -0400114 int npages = obj->size >> PAGE_SHIFT;
115
Rob Clark072f1f92015-03-03 15:04:25 -0500116 if (use_pages(obj))
David Herrmann0cdbe8a2014-05-25 12:59:47 +0200117 p = drm_gem_get_pages(obj);
Rob Clark871d8122013-11-16 12:56:06 -0500118 else
119 p = get_pages_vram(obj, npages);
120
Rob Clarkc8afe682013-06-26 12:44:06 -0400121 if (IS_ERR(p)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530122 DRM_DEV_ERROR(dev->dev, "could not get pages: %ld\n",
Rob Clarkc8afe682013-06-26 12:44:06 -0400123 PTR_ERR(p));
124 return p;
125 }
126
Prakash Kamliya62e3a3e2017-12-04 19:10:15 +0530127 msm_obj->pages = p;
128
Rob Clarkc8afe682013-06-26 12:44:06 -0400129 msm_obj->sgt = drm_prime_pages_to_sg(p, npages);
Wei Yongjun1f70e072013-09-11 06:56:12 +0800130 if (IS_ERR(msm_obj->sgt)) {
Prakash Kamliya62e3a3e2017-12-04 19:10:15 +0530131 void *ptr = ERR_CAST(msm_obj->sgt);
Rob Clarkc8afe682013-06-26 12:44:06 -0400132
Mamta Shukla6a41da12018-10-20 23:19:26 +0530133 DRM_DEV_ERROR(dev->dev, "failed to allocate sgt\n");
Prakash Kamliya62e3a3e2017-12-04 19:10:15 +0530134 msm_obj->sgt = NULL;
135 return ptr;
136 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400137
138 /* For non-cached buffers, ensure the new pages are clean
139 * because display controller, GPU, etc. are not coherent:
140 */
141 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
Rob Clark3de433c2019-07-30 14:46:28 -0700142 sync_for_device(msm_obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400143 }
144
145 return msm_obj->pages;
146}
147
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600148static void put_pages_vram(struct drm_gem_object *obj)
149{
150 struct msm_gem_object *msm_obj = to_msm_bo(obj);
151 struct msm_drm_private *priv = obj->dev->dev_private;
152
153 spin_lock(&priv->vram.lock);
154 drm_mm_remove_node(msm_obj->vram_node);
155 spin_unlock(&priv->vram.lock);
156
157 kvfree(msm_obj->pages);
158}
159
Rob Clarkc8afe682013-06-26 12:44:06 -0400160static void put_pages(struct drm_gem_object *obj)
161{
162 struct msm_gem_object *msm_obj = to_msm_bo(obj);
163
164 if (msm_obj->pages) {
Ben Hutchings39766262018-04-03 23:38:45 +0100165 if (msm_obj->sgt) {
166 /* For non-cached buffers, ensure the new
167 * pages are clean because display controller,
168 * GPU, etc. are not coherent:
169 */
170 if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED))
Rob Clark3de433c2019-07-30 14:46:28 -0700171 sync_for_cpu(msm_obj);
Prakash Kamliya62e3a3e2017-12-04 19:10:15 +0530172
Prakash Kamliya62e3a3e2017-12-04 19:10:15 +0530173 sg_free_table(msm_obj->sgt);
Ben Hutchings39766262018-04-03 23:38:45 +0100174 kfree(msm_obj->sgt);
175 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400176
Rob Clark072f1f92015-03-03 15:04:25 -0500177 if (use_pages(obj))
Rob Clark871d8122013-11-16 12:56:06 -0500178 drm_gem_put_pages(obj, msm_obj->pages, true, false);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600179 else
180 put_pages_vram(obj);
Rob Clark871d8122013-11-16 12:56:06 -0500181
Rob Clarkc8afe682013-06-26 12:44:06 -0400182 msm_obj->pages = NULL;
183 }
184}
185
Rob Clark05b84912013-09-28 11:28:35 -0400186struct page **msm_gem_get_pages(struct drm_gem_object *obj)
187{
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600188 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clark05b84912013-09-28 11:28:35 -0400189 struct page **p;
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600190
191 mutex_lock(&msm_obj->lock);
192
193 if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
194 mutex_unlock(&msm_obj->lock);
195 return ERR_PTR(-EBUSY);
196 }
197
Rob Clark05b84912013-09-28 11:28:35 -0400198 p = get_pages(obj);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600199 mutex_unlock(&msm_obj->lock);
Rob Clark05b84912013-09-28 11:28:35 -0400200 return p;
201}
202
203void msm_gem_put_pages(struct drm_gem_object *obj)
204{
205 /* when we start tracking the pin count, then do something here */
206}
207
Rob Clarkc8afe682013-06-26 12:44:06 -0400208int msm_gem_mmap_obj(struct drm_gem_object *obj,
209 struct vm_area_struct *vma)
210{
211 struct msm_gem_object *msm_obj = to_msm_bo(obj);
212
213 vma->vm_flags &= ~VM_PFNMAP;
214 vma->vm_flags |= VM_MIXEDMAP;
215
216 if (msm_obj->flags & MSM_BO_WC) {
217 vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags));
218 } else if (msm_obj->flags & MSM_BO_UNCACHED) {
219 vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags));
220 } else {
221 /*
222 * Shunt off cached objs to shmem file so they have their own
223 * address_space (so unmap_mapping_range does what we want,
224 * in particular in the case of mmap'd dmabufs)
225 */
226 fput(vma->vm_file);
227 get_file(obj->filp);
228 vma->vm_pgoff = 0;
229 vma->vm_file = obj->filp;
230
231 vma->vm_page_prot = vm_get_page_prot(vma->vm_flags);
232 }
233
234 return 0;
235}
236
237int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma)
238{
239 int ret;
240
241 ret = drm_gem_mmap(filp, vma);
242 if (ret) {
243 DBG("mmap failed: %d", ret);
244 return ret;
245 }
246
247 return msm_gem_mmap_obj(vma->vm_private_data, vma);
248}
249
Souptick Joardera5f74ec2018-05-21 22:59:48 +0530250vm_fault_t msm_gem_fault(struct vm_fault *vmf)
Rob Clarkc8afe682013-06-26 12:44:06 -0400251{
Dave Jiang11bac802017-02-24 14:56:41 -0800252 struct vm_area_struct *vma = vmf->vma;
Rob Clarkc8afe682013-06-26 12:44:06 -0400253 struct drm_gem_object *obj = vma->vm_private_data;
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600254 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400255 struct page **pages;
256 unsigned long pfn;
257 pgoff_t pgoff;
Souptick Joardera5f74ec2018-05-21 22:59:48 +0530258 int err;
259 vm_fault_t ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400260
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600261 /*
262 * vm_ops.open/drm_gem_mmap_obj and close get and put
263 * a reference on obj. So, we dont need to hold one here.
Rob Clarkd78d3832016-08-22 15:28:38 -0400264 */
Souptick Joardera5f74ec2018-05-21 22:59:48 +0530265 err = mutex_lock_interruptible(&msm_obj->lock);
266 if (err) {
267 ret = VM_FAULT_NOPAGE;
Rob Clarkc8afe682013-06-26 12:44:06 -0400268 goto out;
Souptick Joardera5f74ec2018-05-21 22:59:48 +0530269 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400270
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600271 if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED)) {
272 mutex_unlock(&msm_obj->lock);
273 return VM_FAULT_SIGBUS;
274 }
275
Rob Clarkc8afe682013-06-26 12:44:06 -0400276 /* make sure we have pages attached now */
277 pages = get_pages(obj);
278 if (IS_ERR(pages)) {
Souptick Joardera5f74ec2018-05-21 22:59:48 +0530279 ret = vmf_error(PTR_ERR(pages));
Rob Clarkc8afe682013-06-26 12:44:06 -0400280 goto out_unlock;
281 }
282
283 /* We don't use vmf->pgoff since that has the fake offset: */
Jan Kara1a29d852016-12-14 15:07:01 -0800284 pgoff = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
Rob Clarkc8afe682013-06-26 12:44:06 -0400285
Rob Clark871d8122013-11-16 12:56:06 -0500286 pfn = page_to_pfn(pages[pgoff]);
Rob Clarkc8afe682013-06-26 12:44:06 -0400287
Jan Kara1a29d852016-12-14 15:07:01 -0800288 VERB("Inserting %p pfn %lx, pa %lx", (void *)vmf->address,
Rob Clarkc8afe682013-06-26 12:44:06 -0400289 pfn, pfn << PAGE_SHIFT);
290
Souptick Joardera5f74ec2018-05-21 22:59:48 +0530291 ret = vmf_insert_mixed(vma, vmf->address, __pfn_to_pfn_t(pfn, PFN_DEV));
Rob Clarkc8afe682013-06-26 12:44:06 -0400292out_unlock:
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600293 mutex_unlock(&msm_obj->lock);
Rob Clarkc8afe682013-06-26 12:44:06 -0400294out:
Souptick Joardera5f74ec2018-05-21 22:59:48 +0530295 return ret;
Rob Clarkc8afe682013-06-26 12:44:06 -0400296}
297
298/** get mmap offset */
299static uint64_t mmap_offset(struct drm_gem_object *obj)
300{
301 struct drm_device *dev = obj->dev;
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600302 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400303 int ret;
304
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600305 WARN_ON(!mutex_is_locked(&msm_obj->lock));
Rob Clarkc8afe682013-06-26 12:44:06 -0400306
307 /* Make it mmapable */
308 ret = drm_gem_create_mmap_offset(obj);
309
310 if (ret) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530311 DRM_DEV_ERROR(dev->dev, "could not allocate mmap offset\n");
Rob Clarkc8afe682013-06-26 12:44:06 -0400312 return 0;
313 }
314
315 return drm_vma_node_offset_addr(&obj->vma_node);
316}
317
318uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj)
319{
320 uint64_t offset;
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600321 struct msm_gem_object *msm_obj = to_msm_bo(obj);
322
323 mutex_lock(&msm_obj->lock);
Rob Clarkc8afe682013-06-26 12:44:06 -0400324 offset = mmap_offset(obj);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600325 mutex_unlock(&msm_obj->lock);
Rob Clarkc8afe682013-06-26 12:44:06 -0400326 return offset;
327}
328
Rob Clark4b85f7f2017-06-13 13:54:13 -0400329static struct msm_gem_vma *add_vma(struct drm_gem_object *obj,
330 struct msm_gem_address_space *aspace)
331{
332 struct msm_gem_object *msm_obj = to_msm_bo(obj);
333 struct msm_gem_vma *vma;
334
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600335 WARN_ON(!mutex_is_locked(&msm_obj->lock));
336
Rob Clark4b85f7f2017-06-13 13:54:13 -0400337 vma = kzalloc(sizeof(*vma), GFP_KERNEL);
338 if (!vma)
339 return ERR_PTR(-ENOMEM);
340
341 vma->aspace = aspace;
342
343 list_add_tail(&vma->list, &msm_obj->vmas);
344
345 return vma;
346}
347
348static struct msm_gem_vma *lookup_vma(struct drm_gem_object *obj,
349 struct msm_gem_address_space *aspace)
350{
351 struct msm_gem_object *msm_obj = to_msm_bo(obj);
352 struct msm_gem_vma *vma;
353
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600354 WARN_ON(!mutex_is_locked(&msm_obj->lock));
Rob Clark4b85f7f2017-06-13 13:54:13 -0400355
356 list_for_each_entry(vma, &msm_obj->vmas, list) {
357 if (vma->aspace == aspace)
358 return vma;
359 }
360
361 return NULL;
362}
363
364static void del_vma(struct msm_gem_vma *vma)
365{
366 if (!vma)
367 return;
368
369 list_del(&vma->list);
370 kfree(vma);
371}
372
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600373/* Called with msm_obj->lock locked */
Rob Clark4fe5f652016-06-01 11:38:28 -0400374static void
375put_iova(struct drm_gem_object *obj)
376{
Rob Clark4fe5f652016-06-01 11:38:28 -0400377 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clark4b85f7f2017-06-13 13:54:13 -0400378 struct msm_gem_vma *vma, *tmp;
Rob Clark4fe5f652016-06-01 11:38:28 -0400379
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600380 WARN_ON(!mutex_is_locked(&msm_obj->lock));
Rob Clark4fe5f652016-06-01 11:38:28 -0400381
Rob Clark4b85f7f2017-06-13 13:54:13 -0400382 list_for_each_entry_safe(vma, tmp, &msm_obj->vmas, list) {
Brian Masneyd67f1b62019-06-02 21:01:31 -0400383 if (vma->aspace) {
384 msm_gem_purge_vma(vma->aspace, vma);
385 msm_gem_close_vma(vma->aspace, vma);
386 }
Rob Clark4b85f7f2017-06-13 13:54:13 -0400387 del_vma(vma);
Rob Clark4fe5f652016-06-01 11:38:28 -0400388 }
389}
390
Jordan Crousec0ee9792018-11-07 15:35:48 -0700391static int msm_gem_get_iova_locked(struct drm_gem_object *obj,
Rob Clark8bdcd942017-06-13 11:07:08 -0400392 struct msm_gem_address_space *aspace, uint64_t *iova)
Rob Clarkc8afe682013-06-26 12:44:06 -0400393{
394 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clark4b85f7f2017-06-13 13:54:13 -0400395 struct msm_gem_vma *vma;
Rob Clarkc8afe682013-06-26 12:44:06 -0400396 int ret = 0;
397
Jordan Crousec0ee9792018-11-07 15:35:48 -0700398 WARN_ON(!mutex_is_locked(&msm_obj->lock));
Rob Clarkcb1e3812017-06-13 09:15:36 -0400399
Rob Clark4b85f7f2017-06-13 13:54:13 -0400400 vma = lookup_vma(obj, aspace);
Rob Clark871d8122013-11-16 12:56:06 -0500401
Rob Clark4b85f7f2017-06-13 13:54:13 -0400402 if (!vma) {
Rob Clark4b85f7f2017-06-13 13:54:13 -0400403 vma = add_vma(obj, aspace);
Jordan Crousec0ee9792018-11-07 15:35:48 -0700404 if (IS_ERR(vma))
405 return PTR_ERR(vma);
Rob Clark4b85f7f2017-06-13 13:54:13 -0400406
Jordan Crousec0ee9792018-11-07 15:35:48 -0700407 ret = msm_gem_init_vma(aspace, vma, obj->size >> PAGE_SHIFT);
408 if (ret) {
409 del_vma(vma);
410 return ret;
Rob Clark4b85f7f2017-06-13 13:54:13 -0400411 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400412 }
413
Rob Clark4b85f7f2017-06-13 13:54:13 -0400414 *iova = vma->iova;
415 return 0;
Jordan Crousec0ee9792018-11-07 15:35:48 -0700416}
Rob Clark4b85f7f2017-06-13 13:54:13 -0400417
Jordan Crousec0ee9792018-11-07 15:35:48 -0700418static int msm_gem_pin_iova(struct drm_gem_object *obj,
419 struct msm_gem_address_space *aspace)
420{
421 struct msm_gem_object *msm_obj = to_msm_bo(obj);
422 struct msm_gem_vma *vma;
423 struct page **pages;
Rob Clarkbbc2cd02019-01-09 14:25:05 -0500424 int prot = IOMMU_READ;
425
426 if (!(msm_obj->flags & MSM_BO_GPU_READONLY))
427 prot |= IOMMU_WRITE;
Jordan Crousec0ee9792018-11-07 15:35:48 -0700428
429 WARN_ON(!mutex_is_locked(&msm_obj->lock));
430
431 if (WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED))
432 return -EBUSY;
433
434 vma = lookup_vma(obj, aspace);
435 if (WARN_ON(!vma))
436 return -EINVAL;
437
438 pages = get_pages(obj);
439 if (IS_ERR(pages))
440 return PTR_ERR(pages);
441
Rob Clarkbbc2cd02019-01-09 14:25:05 -0500442 return msm_gem_map_vma(aspace, vma, prot,
443 msm_obj->sgt, obj->size >> PAGE_SHIFT);
Jordan Crousec0ee9792018-11-07 15:35:48 -0700444}
445
Jordan Crouse9fe041f2018-11-07 15:35:50 -0700446/* get iova and pin it. Should have a matching put */
447int msm_gem_get_and_pin_iova(struct drm_gem_object *obj,
Jordan Crousec0ee9792018-11-07 15:35:48 -0700448 struct msm_gem_address_space *aspace, uint64_t *iova)
449{
450 struct msm_gem_object *msm_obj = to_msm_bo(obj);
451 u64 local;
452 int ret;
453
454 mutex_lock(&msm_obj->lock);
455
456 ret = msm_gem_get_iova_locked(obj, aspace, &local);
457
458 if (!ret)
459 ret = msm_gem_pin_iova(obj, aspace);
460
461 if (!ret)
462 *iova = local;
463
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600464 mutex_unlock(&msm_obj->lock);
Rob Clarkc8afe682013-06-26 12:44:06 -0400465 return ret;
466}
467
Jordan Crouse7ad0e8c2018-11-07 15:35:51 -0700468/*
469 * Get an iova but don't pin it. Doesn't need a put because iovas are currently
470 * valid for the life of the object
471 */
Jordan Crouse9fe041f2018-11-07 15:35:50 -0700472int msm_gem_get_iova(struct drm_gem_object *obj,
473 struct msm_gem_address_space *aspace, uint64_t *iova)
474{
475 struct msm_gem_object *msm_obj = to_msm_bo(obj);
476 int ret;
477
478 mutex_lock(&msm_obj->lock);
479 ret = msm_gem_get_iova_locked(obj, aspace, iova);
480 mutex_unlock(&msm_obj->lock);
481
482 return ret;
483}
484
Rob Clark2638d902014-11-08 09:13:37 -0500485/* get iova without taking a reference, used in places where you have
Jordan Crouse9fe041f2018-11-07 15:35:50 -0700486 * already done a 'msm_gem_get_and_pin_iova' or 'msm_gem_get_iova'
Rob Clark2638d902014-11-08 09:13:37 -0500487 */
Rob Clark8bdcd942017-06-13 11:07:08 -0400488uint64_t msm_gem_iova(struct drm_gem_object *obj,
489 struct msm_gem_address_space *aspace)
Rob Clark2638d902014-11-08 09:13:37 -0500490{
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600491 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clark4b85f7f2017-06-13 13:54:13 -0400492 struct msm_gem_vma *vma;
493
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600494 mutex_lock(&msm_obj->lock);
Rob Clark4b85f7f2017-06-13 13:54:13 -0400495 vma = lookup_vma(obj, aspace);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600496 mutex_unlock(&msm_obj->lock);
Rob Clark4b85f7f2017-06-13 13:54:13 -0400497 WARN_ON(!vma);
498
499 return vma ? vma->iova : 0;
Rob Clark2638d902014-11-08 09:13:37 -0500500}
501
Jordan Crouse7ad0e8c2018-11-07 15:35:51 -0700502/*
503 * Unpin a iova by updating the reference counts. The memory isn't actually
504 * purged until something else (shrinker, mm_notifier, destroy, etc) decides
505 * to get rid of it
506 */
507void msm_gem_unpin_iova(struct drm_gem_object *obj,
Rob Clark8bdcd942017-06-13 11:07:08 -0400508 struct msm_gem_address_space *aspace)
Rob Clarkc8afe682013-06-26 12:44:06 -0400509{
Jordan Crouse7ad0e8c2018-11-07 15:35:51 -0700510 struct msm_gem_object *msm_obj = to_msm_bo(obj);
511 struct msm_gem_vma *vma;
512
513 mutex_lock(&msm_obj->lock);
514 vma = lookup_vma(obj, aspace);
515
516 if (!WARN_ON(!vma))
517 msm_gem_unmap_vma(aspace, vma);
518
519 mutex_unlock(&msm_obj->lock);
Rob Clarkc8afe682013-06-26 12:44:06 -0400520}
521
522int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
523 struct drm_mode_create_dumb *args)
524{
525 args->pitch = align_pitch(args->width, args->bpp);
526 args->size = PAGE_ALIGN(args->pitch * args->height);
527 return msm_gem_new_handle(dev, file, args->size,
Jordan Crouse0815d772018-11-07 15:35:52 -0700528 MSM_BO_SCANOUT | MSM_BO_WC, &args->handle, "dumb");
Rob Clarkc8afe682013-06-26 12:44:06 -0400529}
530
Rob Clarkc8afe682013-06-26 12:44:06 -0400531int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev,
532 uint32_t handle, uint64_t *offset)
533{
534 struct drm_gem_object *obj;
535 int ret = 0;
536
537 /* GEM does all our handle to object mapping */
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100538 obj = drm_gem_object_lookup(file, handle);
Rob Clarkc8afe682013-06-26 12:44:06 -0400539 if (obj == NULL) {
540 ret = -ENOENT;
541 goto fail;
542 }
543
544 *offset = msm_gem_mmap_offset(obj);
545
Steve Kowalikdc9a9b32018-01-26 14:55:54 +1100546 drm_gem_object_put_unlocked(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400547
548fail:
549 return ret;
550}
551
Rob Clarkfad33f42017-09-15 08:38:20 -0400552static void *get_vaddr(struct drm_gem_object *obj, unsigned madv)
Rob Clarkc8afe682013-06-26 12:44:06 -0400553{
Rob Clarke1e9db22016-05-27 11:16:28 -0400554 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600555 int ret = 0;
556
557 mutex_lock(&msm_obj->lock);
558
Rob Clarkfad33f42017-09-15 08:38:20 -0400559 if (WARN_ON(msm_obj->madv > madv)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +0530560 DRM_DEV_ERROR(obj->dev->dev, "Invalid madv state: %u vs %u\n",
Rob Clarkfad33f42017-09-15 08:38:20 -0400561 msm_obj->madv, madv);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600562 mutex_unlock(&msm_obj->lock);
563 return ERR_PTR(-EBUSY);
564 }
565
566 /* increment vmap_count *before* vmap() call, so shrinker can
567 * check vmap_count (is_vunmapable()) outside of msm_obj->lock.
568 * This guarantees that we won't try to msm_gem_vunmap() this
569 * same object from within the vmap() call (while we already
570 * hold msm_obj->lock)
571 */
572 msm_obj->vmap_count++;
573
574 if (!msm_obj->vaddr) {
575 struct page **pages = get_pages(obj);
576 if (IS_ERR(pages)) {
577 ret = PTR_ERR(pages);
578 goto fail;
579 }
580 msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT,
581 VM_MAP, pgprot_writecombine(PAGE_KERNEL));
582 if (msm_obj->vaddr == NULL) {
583 ret = -ENOMEM;
584 goto fail;
585 }
586 }
587
588 mutex_unlock(&msm_obj->lock);
589 return msm_obj->vaddr;
590
591fail:
Rob Clarke1e9db22016-05-27 11:16:28 -0400592 msm_obj->vmap_count--;
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600593 mutex_unlock(&msm_obj->lock);
594 return ERR_PTR(ret);
Rob Clark18f23042016-05-26 16:24:35 -0400595}
596
Rob Clarkfad33f42017-09-15 08:38:20 -0400597void *msm_gem_get_vaddr(struct drm_gem_object *obj)
598{
599 return get_vaddr(obj, MSM_MADV_WILLNEED);
600}
601
602/*
603 * Don't use this! It is for the very special case of dumping
604 * submits from GPU hangs or faults, were the bo may already
605 * be MSM_MADV_DONTNEED, but we know the buffer is still on the
606 * active list.
607 */
608void *msm_gem_get_vaddr_active(struct drm_gem_object *obj)
609{
610 return get_vaddr(obj, __MSM_MADV_PURGED);
611}
612
Rob Clark18f23042016-05-26 16:24:35 -0400613void msm_gem_put_vaddr(struct drm_gem_object *obj)
614{
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600615 struct msm_gem_object *msm_obj = to_msm_bo(obj);
616
617 mutex_lock(&msm_obj->lock);
618 WARN_ON(msm_obj->vmap_count < 1);
619 msm_obj->vmap_count--;
620 mutex_unlock(&msm_obj->lock);
Rob Clark18f23042016-05-26 16:24:35 -0400621}
622
Rob Clark4cd33c42016-05-17 15:44:49 -0400623/* Update madvise status, returns true if not purged, else
624 * false or -errno.
625 */
626int msm_gem_madvise(struct drm_gem_object *obj, unsigned madv)
627{
628 struct msm_gem_object *msm_obj = to_msm_bo(obj);
629
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600630 mutex_lock(&msm_obj->lock);
631
Rob Clark4cd33c42016-05-17 15:44:49 -0400632 WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex));
633
634 if (msm_obj->madv != __MSM_MADV_PURGED)
635 msm_obj->madv = madv;
636
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600637 madv = msm_obj->madv;
638
639 mutex_unlock(&msm_obj->lock);
640
641 return (madv != __MSM_MADV_PURGED);
Rob Clark4cd33c42016-05-17 15:44:49 -0400642}
643
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600644void msm_gem_purge(struct drm_gem_object *obj, enum msm_gem_lock subclass)
Rob Clark68209392016-05-17 16:19:32 -0400645{
646 struct drm_device *dev = obj->dev;
647 struct msm_gem_object *msm_obj = to_msm_bo(obj);
648
649 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
650 WARN_ON(!is_purgeable(msm_obj));
651 WARN_ON(obj->import_attach);
652
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600653 mutex_lock_nested(&msm_obj->lock, subclass);
654
Rob Clark68209392016-05-17 16:19:32 -0400655 put_iova(obj);
656
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600657 msm_gem_vunmap_locked(obj);
Rob Clark68209392016-05-17 16:19:32 -0400658
659 put_pages(obj);
660
661 msm_obj->madv = __MSM_MADV_PURGED;
662
663 drm_vma_node_unmap(&obj->vma_node, dev->anon_inode->i_mapping);
664 drm_gem_free_mmap_offset(obj);
665
666 /* Our goal here is to return as much of the memory as
667 * is possible back to the system as we are called from OOM.
668 * To do this we must instruct the shmfs to drop all of its
669 * backing pages, *now*.
670 */
671 shmem_truncate_range(file_inode(obj->filp), 0, (loff_t)-1);
672
673 invalidate_mapping_pages(file_inode(obj->filp)->i_mapping,
674 0, (loff_t)-1);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600675
676 mutex_unlock(&msm_obj->lock);
Rob Clark68209392016-05-17 16:19:32 -0400677}
678
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600679static void msm_gem_vunmap_locked(struct drm_gem_object *obj)
Rob Clarke1e9db22016-05-27 11:16:28 -0400680{
681 struct msm_gem_object *msm_obj = to_msm_bo(obj);
682
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600683 WARN_ON(!mutex_is_locked(&msm_obj->lock));
684
Rob Clarke1e9db22016-05-27 11:16:28 -0400685 if (!msm_obj->vaddr || WARN_ON(!is_vunmapable(msm_obj)))
686 return;
687
688 vunmap(msm_obj->vaddr);
689 msm_obj->vaddr = NULL;
690}
691
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600692void msm_gem_vunmap(struct drm_gem_object *obj, enum msm_gem_lock subclass)
693{
694 struct msm_gem_object *msm_obj = to_msm_bo(obj);
695
696 mutex_lock_nested(&msm_obj->lock, subclass);
697 msm_gem_vunmap_locked(obj);
698 mutex_unlock(&msm_obj->lock);
699}
700
Rob Clarkb6295f92016-03-15 18:26:28 -0400701/* must be called before _move_to_active().. */
702int msm_gem_sync_object(struct drm_gem_object *obj,
703 struct msm_fence_context *fctx, bool exclusive)
704{
Christian König52791ee2019-08-11 10:06:32 +0200705 struct dma_resv_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100706 struct dma_fence *fence;
Rob Clarkb6295f92016-03-15 18:26:28 -0400707 int i, ret;
708
Christian König52791ee2019-08-11 10:06:32 +0200709 fobj = dma_resv_get_list(obj->resv);
Rob Clarkb6295f92016-03-15 18:26:28 -0400710 if (!fobj || (fobj->shared_count == 0)) {
Christian König52791ee2019-08-11 10:06:32 +0200711 fence = dma_resv_get_excl(obj->resv);
Rob Clarkb6295f92016-03-15 18:26:28 -0400712 /* don't need to wait on our own fences, since ring is fifo */
713 if (fence && (fence->context != fctx->context)) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100714 ret = dma_fence_wait(fence, true);
Rob Clarkb6295f92016-03-15 18:26:28 -0400715 if (ret)
716 return ret;
717 }
718 }
719
720 if (!exclusive || !fobj)
721 return 0;
722
723 for (i = 0; i < fobj->shared_count; i++) {
724 fence = rcu_dereference_protected(fobj->shared[i],
Christian König52791ee2019-08-11 10:06:32 +0200725 dma_resv_held(obj->resv));
Rob Clarkb6295f92016-03-15 18:26:28 -0400726 if (fence->context != fctx->context) {
Chris Wilsonf54d1862016-10-25 13:00:45 +0100727 ret = dma_fence_wait(fence, true);
Rob Clarkb6295f92016-03-15 18:26:28 -0400728 if (ret)
729 return ret;
730 }
731 }
732
733 return 0;
734}
735
Rob Clark7198e6b2013-07-19 12:59:32 -0400736void msm_gem_move_to_active(struct drm_gem_object *obj,
Chris Wilsonf54d1862016-10-25 13:00:45 +0100737 struct msm_gpu *gpu, bool exclusive, struct dma_fence *fence)
Rob Clark7198e6b2013-07-19 12:59:32 -0400738{
739 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Rob Clark4cd33c42016-05-17 15:44:49 -0400740 WARN_ON(msm_obj->madv != MSM_MADV_WILLNEED);
Rob Clark7198e6b2013-07-19 12:59:32 -0400741 msm_obj->gpu = gpu;
Rob Clarkb6295f92016-03-15 18:26:28 -0400742 if (exclusive)
Christian König52791ee2019-08-11 10:06:32 +0200743 dma_resv_add_excl_fence(obj->resv, fence);
Rob Clarkbf6811f2013-09-01 13:25:09 -0400744 else
Christian König52791ee2019-08-11 10:06:32 +0200745 dma_resv_add_shared_fence(obj->resv, fence);
Rob Clark7198e6b2013-07-19 12:59:32 -0400746 list_del_init(&msm_obj->mm_list);
747 list_add_tail(&msm_obj->mm_list, &gpu->active_list);
748}
749
750void msm_gem_move_to_inactive(struct drm_gem_object *obj)
751{
752 struct drm_device *dev = obj->dev;
753 struct msm_drm_private *priv = dev->dev_private;
754 struct msm_gem_object *msm_obj = to_msm_bo(obj);
755
756 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
757
758 msm_obj->gpu = NULL;
Rob Clark7198e6b2013-07-19 12:59:32 -0400759 list_del_init(&msm_obj->mm_list);
760 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
Rob Clark7198e6b2013-07-19 12:59:32 -0400761}
762
Rob Clarkba00c3f2016-03-16 18:18:17 -0400763int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout)
764{
Rob Clarkb6295f92016-03-15 18:26:28 -0400765 bool write = !!(op & MSM_PREP_WRITE);
Chris Wilsonf755e222016-08-29 08:08:26 +0100766 unsigned long remain =
767 op & MSM_PREP_NOSYNC ? 0 : timeout_to_jiffies(timeout);
768 long ret;
Rob Clarkb6295f92016-03-15 18:26:28 -0400769
Christian König52791ee2019-08-11 10:06:32 +0200770 ret = dma_resv_wait_timeout_rcu(obj->resv, write,
Chris Wilsonf755e222016-08-29 08:08:26 +0100771 true, remain);
772 if (ret == 0)
773 return remain == 0 ? -EBUSY : -ETIMEDOUT;
774 else if (ret < 0)
775 return ret;
Rob Clarkba00c3f2016-03-16 18:18:17 -0400776
Rob Clark7198e6b2013-07-19 12:59:32 -0400777 /* TODO cache maintenance */
778
Rob Clarkb6295f92016-03-15 18:26:28 -0400779 return 0;
Rob Clark7198e6b2013-07-19 12:59:32 -0400780}
781
782int msm_gem_cpu_fini(struct drm_gem_object *obj)
783{
784 /* TODO cache maintenance */
Rob Clarkc8afe682013-06-26 12:44:06 -0400785 return 0;
786}
787
788#ifdef CONFIG_DEBUG_FS
Chris Wilsonf54d1862016-10-25 13:00:45 +0100789static void describe_fence(struct dma_fence *fence, const char *type,
Rob Clarkb6295f92016-03-15 18:26:28 -0400790 struct seq_file *m)
791{
Chris Wilsonf54d1862016-10-25 13:00:45 +0100792 if (!dma_fence_is_signaled(fence))
Dave Airliea3115622019-01-10 06:20:15 +1000793 seq_printf(m, "\t%9s: %s %s seq %llu\n", type,
Rob Clarkb6295f92016-03-15 18:26:28 -0400794 fence->ops->get_driver_name(fence),
795 fence->ops->get_timeline_name(fence),
796 fence->seqno);
797}
798
Rob Clarkc8afe682013-06-26 12:44:06 -0400799void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m)
800{
Rob Clarkc8afe682013-06-26 12:44:06 -0400801 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Christian König52791ee2019-08-11 10:06:32 +0200802 struct dma_resv *robj = obj->resv;
803 struct dma_resv_list *fobj;
Chris Wilsonf54d1862016-10-25 13:00:45 +0100804 struct dma_fence *fence;
Rob Clark4b85f7f2017-06-13 13:54:13 -0400805 struct msm_gem_vma *vma;
Rob Clarkc8afe682013-06-26 12:44:06 -0400806 uint64_t off = drm_vma_node_start(&obj->vma_node);
Rob Clark4cd33c42016-05-17 15:44:49 -0400807 const char *madv;
Rob Clarkc8afe682013-06-26 12:44:06 -0400808
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600809 mutex_lock(&msm_obj->lock);
Rob Clarkb6295f92016-03-15 18:26:28 -0400810
Rob Clark4cd33c42016-05-17 15:44:49 -0400811 switch (msm_obj->madv) {
812 case __MSM_MADV_PURGED:
813 madv = " purged";
814 break;
815 case MSM_MADV_DONTNEED:
816 madv = " purgeable";
817 break;
818 case MSM_MADV_WILLNEED:
819 default:
820 madv = "";
821 break;
822 }
823
Jordan Crouse575f0482018-11-07 15:35:49 -0700824 seq_printf(m, "%08x: %c %2d (%2d) %08llx %p",
Rob Clark7198e6b2013-07-19 12:59:32 -0400825 msm_obj->flags, is_active(msm_obj) ? 'A' : 'I',
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100826 obj->name, kref_read(&obj->refcount),
Rob Clark667ce332016-09-28 19:58:32 -0400827 off, msm_obj->vaddr);
828
Jordan Crouse0815d772018-11-07 15:35:52 -0700829 seq_printf(m, " %08zu %9s %-32s\n", obj->size, madv, msm_obj->name);
Rob Clark667ce332016-09-28 19:58:32 -0400830
Jordan Crouse575f0482018-11-07 15:35:49 -0700831 if (!list_empty(&msm_obj->vmas)) {
832
Jordan Crouse7ad0e8c2018-11-07 15:35:51 -0700833 seq_puts(m, " vmas:");
Jordan Crouse575f0482018-11-07 15:35:49 -0700834
835 list_for_each_entry(vma, &msm_obj->vmas, list)
Brian Masney90f94662019-05-13 19:41:05 -0400836 seq_printf(m, " [%s: %08llx,%s,inuse=%d]",
837 vma->aspace != NULL ? vma->aspace->name : NULL,
Jordan Crouse7ad0e8c2018-11-07 15:35:51 -0700838 vma->iova, vma->mapped ? "mapped" : "unmapped",
839 vma->inuse);
Jordan Crouse575f0482018-11-07 15:35:49 -0700840
841 seq_puts(m, "\n");
842 }
Rob Clarkb6295f92016-03-15 18:26:28 -0400843
844 rcu_read_lock();
845 fobj = rcu_dereference(robj->fence);
846 if (fobj) {
847 unsigned int i, shared_count = fobj->shared_count;
848
849 for (i = 0; i < shared_count; i++) {
850 fence = rcu_dereference(fobj->shared[i]);
851 describe_fence(fence, "Shared", m);
852 }
853 }
854
855 fence = rcu_dereference(robj->fence_excl);
856 if (fence)
857 describe_fence(fence, "Exclusive", m);
858 rcu_read_unlock();
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600859
860 mutex_unlock(&msm_obj->lock);
Rob Clarkc8afe682013-06-26 12:44:06 -0400861}
862
863void msm_gem_describe_objects(struct list_head *list, struct seq_file *m)
864{
865 struct msm_gem_object *msm_obj;
866 int count = 0;
867 size_t size = 0;
868
Jordan Crouse0815d772018-11-07 15:35:52 -0700869 seq_puts(m, " flags id ref offset kaddr size madv name\n");
Rob Clarkc8afe682013-06-26 12:44:06 -0400870 list_for_each_entry(msm_obj, list, mm_list) {
871 struct drm_gem_object *obj = &msm_obj->base;
Jordan Crouse575f0482018-11-07 15:35:49 -0700872 seq_puts(m, " ");
Rob Clarkc8afe682013-06-26 12:44:06 -0400873 msm_gem_describe(obj, m);
874 count++;
875 size += obj->size;
876 }
877
878 seq_printf(m, "Total %d objects, %zu bytes\n", count, size);
879}
880#endif
881
Rob Clarkd71b6bd2018-02-14 11:14:23 -0500882/* don't call directly! Use drm_gem_object_put() and friends */
Rob Clarkc8afe682013-06-26 12:44:06 -0400883void msm_gem_free_object(struct drm_gem_object *obj)
884{
Rob Clarkc8afe682013-06-26 12:44:06 -0400885 struct msm_gem_object *msm_obj = to_msm_bo(obj);
Kristian H. Kristensen48e7f182019-03-20 10:09:08 -0700886 struct drm_device *dev = obj->dev;
887 struct msm_drm_private *priv = dev->dev_private;
888
889 if (llist_add(&msm_obj->freed, &priv->free_list))
890 queue_work(priv->wq, &priv->free_work);
891}
892
893static void free_object(struct msm_gem_object *msm_obj)
894{
895 struct drm_gem_object *obj = &msm_obj->base;
896 struct drm_device *dev = obj->dev;
Rob Clarkc8afe682013-06-26 12:44:06 -0400897
898 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
899
Rob Clark7198e6b2013-07-19 12:59:32 -0400900 /* object should not be on active list: */
901 WARN_ON(is_active(msm_obj));
902
Rob Clarkc8afe682013-06-26 12:44:06 -0400903 list_del(&msm_obj->mm_list);
904
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600905 mutex_lock(&msm_obj->lock);
906
Rob Clark4fe5f652016-06-01 11:38:28 -0400907 put_iova(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400908
Rob Clark05b84912013-09-28 11:28:35 -0400909 if (obj->import_attach) {
910 if (msm_obj->vaddr)
911 dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr);
Rob Clarkc8afe682013-06-26 12:44:06 -0400912
Rob Clark05b84912013-09-28 11:28:35 -0400913 /* Don't drop the pages for imported dmabuf, as they are not
914 * ours, just free the array we allocated:
915 */
916 if (msm_obj->pages)
Michal Hocko20981052017-05-17 14:23:12 +0200917 kvfree(msm_obj->pages);
Rob Clark05b84912013-09-28 11:28:35 -0400918
jilai wangf28730c2015-04-07 13:51:32 -0400919 drm_prime_gem_destroy(obj, msm_obj->sgt);
Rob Clark05b84912013-09-28 11:28:35 -0400920 } else {
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600921 msm_gem_vunmap_locked(obj);
Rob Clark05b84912013-09-28 11:28:35 -0400922 put_pages(obj);
923 }
Rob Clarkc8afe682013-06-26 12:44:06 -0400924
925 drm_gem_object_release(obj);
926
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600927 mutex_unlock(&msm_obj->lock);
Rob Clarkc8afe682013-06-26 12:44:06 -0400928 kfree(msm_obj);
929}
930
Kristian H. Kristensen48e7f182019-03-20 10:09:08 -0700931void msm_gem_free_work(struct work_struct *work)
932{
933 struct msm_drm_private *priv =
934 container_of(work, struct msm_drm_private, free_work);
935 struct drm_device *dev = priv->dev;
936 struct llist_node *freed;
937 struct msm_gem_object *msm_obj, *next;
938
939 while ((freed = llist_del_all(&priv->free_list))) {
940
941 mutex_lock(&dev->struct_mutex);
942
943 llist_for_each_entry_safe(msm_obj, next,
944 freed, freed)
945 free_object(msm_obj);
946
947 mutex_unlock(&dev->struct_mutex);
948
949 if (need_resched())
950 break;
951 }
952}
953
Rob Clarkc8afe682013-06-26 12:44:06 -0400954/* convenience method to construct a GEM buffer object, and userspace handle */
955int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file,
Jordan Crouse0815d772018-11-07 15:35:52 -0700956 uint32_t size, uint32_t flags, uint32_t *handle,
957 char *name)
Rob Clarkc8afe682013-06-26 12:44:06 -0400958{
959 struct drm_gem_object *obj;
960 int ret;
961
Rob Clarkc8afe682013-06-26 12:44:06 -0400962 obj = msm_gem_new(dev, size, flags);
963
Rob Clarkc8afe682013-06-26 12:44:06 -0400964 if (IS_ERR(obj))
965 return PTR_ERR(obj);
966
Jordan Crouse0815d772018-11-07 15:35:52 -0700967 if (name)
968 msm_gem_object_set_name(obj, "%s", name);
969
Rob Clarkc8afe682013-06-26 12:44:06 -0400970 ret = drm_gem_handle_create(file, obj, handle);
971
972 /* drop reference from allocate - handle holds it now */
Steve Kowalikdc9a9b32018-01-26 14:55:54 +1100973 drm_gem_object_put_unlocked(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -0400974
975 return ret;
976}
977
Rob Clark05b84912013-09-28 11:28:35 -0400978static int msm_gem_new_impl(struct drm_device *dev,
979 uint32_t size, uint32_t flags,
Sushmita Susheelendra0e082702017-06-13 16:52:54 -0600980 struct drm_gem_object **obj,
981 bool struct_mutex_locked)
Rob Clarkc8afe682013-06-26 12:44:06 -0400982{
983 struct msm_drm_private *priv = dev->dev_private;
984 struct msm_gem_object *msm_obj;
Rob Clarkc8afe682013-06-26 12:44:06 -0400985
986 switch (flags & MSM_BO_CACHE_MASK) {
987 case MSM_BO_UNCACHED:
988 case MSM_BO_CACHED:
989 case MSM_BO_WC:
990 break;
991 default:
Mamta Shukla6a41da12018-10-20 23:19:26 +0530992 DRM_DEV_ERROR(dev->dev, "invalid cache flag: %x\n",
Rob Clarkc8afe682013-06-26 12:44:06 -0400993 (flags & MSM_BO_CACHE_MASK));
Rob Clark05b84912013-09-28 11:28:35 -0400994 return -EINVAL;
Rob Clarkc8afe682013-06-26 12:44:06 -0400995 }
996
Rob Clark667ce332016-09-28 19:58:32 -0400997 msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL);
Rob Clark05b84912013-09-28 11:28:35 -0400998 if (!msm_obj)
999 return -ENOMEM;
Rob Clarkc8afe682013-06-26 12:44:06 -04001000
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001001 mutex_init(&msm_obj->lock);
1002
Rob Clarkc8afe682013-06-26 12:44:06 -04001003 msm_obj->flags = flags;
Rob Clark4cd33c42016-05-17 15:44:49 -04001004 msm_obj->madv = MSM_MADV_WILLNEED;
Rob Clarkc8afe682013-06-26 12:44:06 -04001005
Rob Clark7198e6b2013-07-19 12:59:32 -04001006 INIT_LIST_HEAD(&msm_obj->submit_entry);
Rob Clark4b85f7f2017-06-13 13:54:13 -04001007 INIT_LIST_HEAD(&msm_obj->vmas);
1008
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001009 if (struct_mutex_locked) {
1010 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
1011 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
1012 } else {
1013 mutex_lock(&dev->struct_mutex);
1014 list_add_tail(&msm_obj->mm_list, &priv->inactive_list);
1015 mutex_unlock(&dev->struct_mutex);
1016 }
Rob Clarkc8afe682013-06-26 12:44:06 -04001017
Rob Clark05b84912013-09-28 11:28:35 -04001018 *obj = &msm_obj->base;
1019
1020 return 0;
1021}
1022
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001023static struct drm_gem_object *_msm_gem_new(struct drm_device *dev,
1024 uint32_t size, uint32_t flags, bool struct_mutex_locked)
Rob Clark05b84912013-09-28 11:28:35 -04001025{
Rob Clarkf4839bd2017-06-13 11:50:05 -04001026 struct msm_drm_private *priv = dev->dev_private;
Rob Clark871d8122013-11-16 12:56:06 -05001027 struct drm_gem_object *obj = NULL;
Rob Clarkf4839bd2017-06-13 11:50:05 -04001028 bool use_vram = false;
Rob Clark05b84912013-09-28 11:28:35 -04001029 int ret;
1030
Rob Clark05b84912013-09-28 11:28:35 -04001031 size = PAGE_ALIGN(size);
1032
Jonathan Marekc2052a42018-11-14 17:08:04 -05001033 if (!msm_use_mmu(dev))
Rob Clarkf4839bd2017-06-13 11:50:05 -04001034 use_vram = true;
Jonathan Marek86f46f22018-11-21 20:52:30 -05001035 else if ((flags & (MSM_BO_STOLEN | MSM_BO_SCANOUT)) && priv->vram.size)
Rob Clarkf4839bd2017-06-13 11:50:05 -04001036 use_vram = true;
1037
1038 if (WARN_ON(use_vram && !priv->vram.size))
1039 return ERR_PTR(-EINVAL);
1040
Jordan Crouse1a5dff52017-03-07 10:02:51 -07001041 /* Disallow zero sized objects as they make the underlying
1042 * infrastructure grumpy
1043 */
1044 if (size == 0)
1045 return ERR_PTR(-EINVAL);
1046
Daniel Vetter5ebeb022019-06-14 22:36:01 +02001047 ret = msm_gem_new_impl(dev, size, flags, &obj, struct_mutex_locked);
Rob Clark05b84912013-09-28 11:28:35 -04001048 if (ret)
1049 goto fail;
1050
Rob Clarkf4839bd2017-06-13 11:50:05 -04001051 if (use_vram) {
Rob Clark4b85f7f2017-06-13 13:54:13 -04001052 struct msm_gem_vma *vma;
Rob Clarkf4839bd2017-06-13 11:50:05 -04001053 struct page **pages;
Hans Verkuilb3949a92017-07-30 14:42:36 +02001054 struct msm_gem_object *msm_obj = to_msm_bo(obj);
1055
1056 mutex_lock(&msm_obj->lock);
Rob Clarkf4839bd2017-06-13 11:50:05 -04001057
Rob Clark4b85f7f2017-06-13 13:54:13 -04001058 vma = add_vma(obj, NULL);
Hans Verkuilb3949a92017-07-30 14:42:36 +02001059 mutex_unlock(&msm_obj->lock);
Rob Clark4b85f7f2017-06-13 13:54:13 -04001060 if (IS_ERR(vma)) {
1061 ret = PTR_ERR(vma);
1062 goto fail;
1063 }
1064
1065 to_msm_bo(obj)->vram_node = &vma->node;
1066
Rob Clarkf4839bd2017-06-13 11:50:05 -04001067 drm_gem_private_object_init(dev, obj, size);
1068
Rob Clarkf4839bd2017-06-13 11:50:05 -04001069 pages = get_pages(obj);
1070 if (IS_ERR(pages)) {
1071 ret = PTR_ERR(pages);
1072 goto fail;
1073 }
Rob Clark4b85f7f2017-06-13 13:54:13 -04001074
1075 vma->iova = physaddr(obj);
Rob Clarkf4839bd2017-06-13 11:50:05 -04001076 } else {
Rob Clark871d8122013-11-16 12:56:06 -05001077 ret = drm_gem_object_init(dev, obj, size);
1078 if (ret)
1079 goto fail;
Lucas Stach0abdba42019-02-28 07:23:29 +01001080 /*
1081 * Our buffers are kept pinned, so allocating them from the
1082 * MOVABLE zone is a really bad idea, and conflicts with CMA.
1083 * See comments above new_inode() why this is required _and_
1084 * expected if you're going to pin these pages.
1085 */
1086 mapping_set_gfp_mask(obj->filp->f_mapping, GFP_HIGHUSER);
Rob Clark871d8122013-11-16 12:56:06 -05001087 }
Rob Clark05b84912013-09-28 11:28:35 -04001088
1089 return obj;
1090
1091fail:
Steve Kowalikdc9a9b32018-01-26 14:55:54 +11001092 drm_gem_object_put_unlocked(obj);
Rob Clark05b84912013-09-28 11:28:35 -04001093 return ERR_PTR(ret);
1094}
1095
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001096struct drm_gem_object *msm_gem_new_locked(struct drm_device *dev,
1097 uint32_t size, uint32_t flags)
1098{
1099 return _msm_gem_new(dev, size, flags, true);
1100}
1101
1102struct drm_gem_object *msm_gem_new(struct drm_device *dev,
1103 uint32_t size, uint32_t flags)
1104{
1105 return _msm_gem_new(dev, size, flags, false);
1106}
1107
Rob Clark05b84912013-09-28 11:28:35 -04001108struct drm_gem_object *msm_gem_import(struct drm_device *dev,
Rob Clark79f0e202016-03-16 12:40:35 -04001109 struct dma_buf *dmabuf, struct sg_table *sgt)
Rob Clark05b84912013-09-28 11:28:35 -04001110{
1111 struct msm_gem_object *msm_obj;
1112 struct drm_gem_object *obj;
Rob Clark79f0e202016-03-16 12:40:35 -04001113 uint32_t size;
Rob Clark05b84912013-09-28 11:28:35 -04001114 int ret, npages;
1115
Rob Clark871d8122013-11-16 12:56:06 -05001116 /* if we don't have IOMMU, don't bother pretending we can import: */
Jonathan Marekc2052a42018-11-14 17:08:04 -05001117 if (!msm_use_mmu(dev)) {
Mamta Shukla6a41da12018-10-20 23:19:26 +05301118 DRM_DEV_ERROR(dev->dev, "cannot import without IOMMU\n");
Rob Clark871d8122013-11-16 12:56:06 -05001119 return ERR_PTR(-EINVAL);
1120 }
1121
Rob Clark79f0e202016-03-16 12:40:35 -04001122 size = PAGE_ALIGN(dmabuf->size);
Rob Clark05b84912013-09-28 11:28:35 -04001123
Daniel Vetter5ebeb022019-06-14 22:36:01 +02001124 ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj, false);
Rob Clark05b84912013-09-28 11:28:35 -04001125 if (ret)
1126 goto fail;
1127
1128 drm_gem_private_object_init(dev, obj, size);
1129
1130 npages = size / PAGE_SIZE;
1131
1132 msm_obj = to_msm_bo(obj);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001133 mutex_lock(&msm_obj->lock);
Rob Clark05b84912013-09-28 11:28:35 -04001134 msm_obj->sgt = sgt;
Michal Hocko20981052017-05-17 14:23:12 +02001135 msm_obj->pages = kvmalloc_array(npages, sizeof(struct page *), GFP_KERNEL);
Rob Clark05b84912013-09-28 11:28:35 -04001136 if (!msm_obj->pages) {
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001137 mutex_unlock(&msm_obj->lock);
Rob Clark05b84912013-09-28 11:28:35 -04001138 ret = -ENOMEM;
1139 goto fail;
1140 }
1141
1142 ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages);
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001143 if (ret) {
1144 mutex_unlock(&msm_obj->lock);
Rob Clark05b84912013-09-28 11:28:35 -04001145 goto fail;
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001146 }
Rob Clark05b84912013-09-28 11:28:35 -04001147
Sushmita Susheelendra0e082702017-06-13 16:52:54 -06001148 mutex_unlock(&msm_obj->lock);
Rob Clarkc8afe682013-06-26 12:44:06 -04001149 return obj;
1150
1151fail:
Steve Kowalikdc9a9b32018-01-26 14:55:54 +11001152 drm_gem_object_put_unlocked(obj);
Rob Clarkc8afe682013-06-26 12:44:06 -04001153 return ERR_PTR(ret);
1154}
Jordan Crouse82232862017-07-27 10:42:40 -06001155
1156static void *_msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1157 uint32_t flags, struct msm_gem_address_space *aspace,
1158 struct drm_gem_object **bo, uint64_t *iova, bool locked)
1159{
1160 void *vaddr;
1161 struct drm_gem_object *obj = _msm_gem_new(dev, size, flags, locked);
1162 int ret;
1163
1164 if (IS_ERR(obj))
1165 return ERR_CAST(obj);
1166
1167 if (iova) {
Jordan Crouse9fe041f2018-11-07 15:35:50 -07001168 ret = msm_gem_get_and_pin_iova(obj, aspace, iova);
Jordan Crouse93f7abf2018-11-02 09:25:19 -06001169 if (ret)
1170 goto err;
Jordan Crouse82232862017-07-27 10:42:40 -06001171 }
1172
1173 vaddr = msm_gem_get_vaddr(obj);
Wei Yongjunc9811d02017-10-11 11:36:56 +00001174 if (IS_ERR(vaddr)) {
Jordan Crouse7ad0e8c2018-11-07 15:35:51 -07001175 msm_gem_unpin_iova(obj, aspace);
Jordan Crouse93f7abf2018-11-02 09:25:19 -06001176 ret = PTR_ERR(vaddr);
1177 goto err;
Jordan Crouse82232862017-07-27 10:42:40 -06001178 }
1179
1180 if (bo)
1181 *bo = obj;
1182
1183 return vaddr;
Jordan Crouse93f7abf2018-11-02 09:25:19 -06001184err:
1185 if (locked)
1186 drm_gem_object_put(obj);
1187 else
1188 drm_gem_object_put_unlocked(obj);
1189
1190 return ERR_PTR(ret);
1191
Jordan Crouse82232862017-07-27 10:42:40 -06001192}
1193
1194void *msm_gem_kernel_new(struct drm_device *dev, uint32_t size,
1195 uint32_t flags, struct msm_gem_address_space *aspace,
1196 struct drm_gem_object **bo, uint64_t *iova)
1197{
1198 return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, false);
1199}
1200
1201void *msm_gem_kernel_new_locked(struct drm_device *dev, uint32_t size,
1202 uint32_t flags, struct msm_gem_address_space *aspace,
1203 struct drm_gem_object **bo, uint64_t *iova)
1204{
1205 return _msm_gem_kernel_new(dev, size, flags, aspace, bo, iova, true);
1206}
Jordan Crouse1e29dff2018-11-07 15:35:46 -07001207
1208void msm_gem_kernel_put(struct drm_gem_object *bo,
1209 struct msm_gem_address_space *aspace, bool locked)
1210{
1211 if (IS_ERR_OR_NULL(bo))
1212 return;
1213
1214 msm_gem_put_vaddr(bo);
Jordan Crouse7ad0e8c2018-11-07 15:35:51 -07001215 msm_gem_unpin_iova(bo, aspace);
Jordan Crouse1e29dff2018-11-07 15:35:46 -07001216
1217 if (locked)
1218 drm_gem_object_put(bo);
1219 else
1220 drm_gem_object_put_unlocked(bo);
1221}
Jordan Crouse0815d772018-11-07 15:35:52 -07001222
1223void msm_gem_object_set_name(struct drm_gem_object *bo, const char *fmt, ...)
1224{
1225 struct msm_gem_object *msm_obj = to_msm_bo(bo);
1226 va_list ap;
1227
1228 if (!fmt)
1229 return;
1230
1231 va_start(ap, fmt);
1232 vsnprintf(msm_obj->name, sizeof(msm_obj->name), fmt, ap);
1233 va_end(ap);
1234}