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 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 29 | static dma_addr_t physaddr(struct drm_gem_object *obj) |
| 30 | { |
| 31 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 32 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 33 | return (((dma_addr_t)msm_obj->vram_node->start) << PAGE_SHIFT) + |
| 34 | priv->vram.paddr; |
| 35 | } |
| 36 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 37 | static bool use_pages(struct drm_gem_object *obj) |
| 38 | { |
| 39 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 40 | return !msm_obj->vram_node; |
| 41 | } |
| 42 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 43 | /* allocate pages from VRAM carveout, used when no IOMMU: */ |
| 44 | static struct page **get_pages_vram(struct drm_gem_object *obj, |
| 45 | int npages) |
| 46 | { |
| 47 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 48 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 49 | dma_addr_t paddr; |
| 50 | struct page **p; |
| 51 | int ret, i; |
| 52 | |
| 53 | p = drm_malloc_ab(npages, sizeof(struct page *)); |
| 54 | if (!p) |
| 55 | return ERR_PTR(-ENOMEM); |
| 56 | |
| 57 | ret = drm_mm_insert_node(&priv->vram.mm, msm_obj->vram_node, |
| 58 | npages, 0, DRM_MM_SEARCH_DEFAULT); |
| 59 | if (ret) { |
| 60 | drm_free_large(p); |
| 61 | return ERR_PTR(ret); |
| 62 | } |
| 63 | |
| 64 | paddr = physaddr(obj); |
| 65 | for (i = 0; i < npages; i++) { |
| 66 | p[i] = phys_to_page(paddr); |
| 67 | paddr += PAGE_SIZE; |
| 68 | } |
| 69 | |
| 70 | return p; |
| 71 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 72 | |
| 73 | /* called with dev->struct_mutex held */ |
| 74 | static struct page **get_pages(struct drm_gem_object *obj) |
| 75 | { |
| 76 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 77 | |
| 78 | if (!msm_obj->pages) { |
| 79 | struct drm_device *dev = obj->dev; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 80 | struct page **p; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 81 | int npages = obj->size >> PAGE_SHIFT; |
| 82 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 83 | if (use_pages(obj)) |
David Herrmann | 0cdbe8a | 2014-05-25 12:59:47 +0200 | [diff] [blame] | 84 | p = drm_gem_get_pages(obj); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 85 | else |
| 86 | p = get_pages_vram(obj, npages); |
| 87 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 88 | if (IS_ERR(p)) { |
| 89 | dev_err(dev->dev, "could not get pages: %ld\n", |
| 90 | PTR_ERR(p)); |
| 91 | return p; |
| 92 | } |
| 93 | |
| 94 | msm_obj->sgt = drm_prime_pages_to_sg(p, npages); |
Wei Yongjun | 1f70e07 | 2013-09-11 06:56:12 +0800 | [diff] [blame] | 95 | if (IS_ERR(msm_obj->sgt)) { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 96 | dev_err(dev->dev, "failed to allocate sgt\n"); |
Wei Yongjun | 1f70e07 | 2013-09-11 06:56:12 +0800 | [diff] [blame] | 97 | return ERR_CAST(msm_obj->sgt); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | msm_obj->pages = p; |
| 101 | |
| 102 | /* For non-cached buffers, ensure the new pages are clean |
| 103 | * because display controller, GPU, etc. are not coherent: |
| 104 | */ |
| 105 | if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) |
| 106 | dma_map_sg(dev->dev, msm_obj->sgt->sgl, |
| 107 | msm_obj->sgt->nents, DMA_BIDIRECTIONAL); |
| 108 | } |
| 109 | |
| 110 | return msm_obj->pages; |
| 111 | } |
| 112 | |
| 113 | static void put_pages(struct drm_gem_object *obj) |
| 114 | { |
| 115 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 116 | |
| 117 | if (msm_obj->pages) { |
| 118 | /* For non-cached buffers, ensure the new pages are clean |
| 119 | * because display controller, GPU, etc. are not coherent: |
| 120 | */ |
| 121 | if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) |
| 122 | dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl, |
| 123 | msm_obj->sgt->nents, DMA_BIDIRECTIONAL); |
| 124 | sg_free_table(msm_obj->sgt); |
| 125 | kfree(msm_obj->sgt); |
| 126 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 127 | if (use_pages(obj)) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 128 | drm_gem_put_pages(obj, msm_obj->pages, true, false); |
Micah Richert | 1ffa242 | 2014-04-09 14:11:31 -0700 | [diff] [blame] | 129 | else { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 130 | drm_mm_remove_node(msm_obj->vram_node); |
Micah Richert | 1ffa242 | 2014-04-09 14:11:31 -0700 | [diff] [blame] | 131 | drm_free_large(msm_obj->pages); |
| 132 | } |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 133 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 134 | msm_obj->pages = NULL; |
| 135 | } |
| 136 | } |
| 137 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 138 | struct page **msm_gem_get_pages(struct drm_gem_object *obj) |
| 139 | { |
| 140 | struct drm_device *dev = obj->dev; |
| 141 | struct page **p; |
| 142 | mutex_lock(&dev->struct_mutex); |
| 143 | p = get_pages(obj); |
| 144 | mutex_unlock(&dev->struct_mutex); |
| 145 | return p; |
| 146 | } |
| 147 | |
| 148 | void msm_gem_put_pages(struct drm_gem_object *obj) |
| 149 | { |
| 150 | /* when we start tracking the pin count, then do something here */ |
| 151 | } |
| 152 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 153 | int msm_gem_mmap_obj(struct drm_gem_object *obj, |
| 154 | struct vm_area_struct *vma) |
| 155 | { |
| 156 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 157 | |
| 158 | vma->vm_flags &= ~VM_PFNMAP; |
| 159 | vma->vm_flags |= VM_MIXEDMAP; |
| 160 | |
| 161 | if (msm_obj->flags & MSM_BO_WC) { |
| 162 | vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 163 | } else if (msm_obj->flags & MSM_BO_UNCACHED) { |
| 164 | vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 165 | } else { |
| 166 | /* |
| 167 | * Shunt off cached objs to shmem file so they have their own |
| 168 | * address_space (so unmap_mapping_range does what we want, |
| 169 | * in particular in the case of mmap'd dmabufs) |
| 170 | */ |
| 171 | fput(vma->vm_file); |
| 172 | get_file(obj->filp); |
| 173 | vma->vm_pgoff = 0; |
| 174 | vma->vm_file = obj->filp; |
| 175 | |
| 176 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 177 | } |
| 178 | |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 183 | { |
| 184 | int ret; |
| 185 | |
| 186 | ret = drm_gem_mmap(filp, vma); |
| 187 | if (ret) { |
| 188 | DBG("mmap failed: %d", ret); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | return msm_gem_mmap_obj(vma->vm_private_data, vma); |
| 193 | } |
| 194 | |
| 195 | int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 196 | { |
| 197 | struct drm_gem_object *obj = vma->vm_private_data; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 198 | struct drm_device *dev = obj->dev; |
| 199 | struct page **pages; |
| 200 | unsigned long pfn; |
| 201 | pgoff_t pgoff; |
| 202 | int ret; |
| 203 | |
| 204 | /* Make sure we don't parallel update on a fault, nor move or remove |
| 205 | * something from beneath our feet |
| 206 | */ |
| 207 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 208 | if (ret) |
| 209 | goto out; |
| 210 | |
| 211 | /* make sure we have pages attached now */ |
| 212 | pages = get_pages(obj); |
| 213 | if (IS_ERR(pages)) { |
| 214 | ret = PTR_ERR(pages); |
| 215 | goto out_unlock; |
| 216 | } |
| 217 | |
| 218 | /* We don't use vmf->pgoff since that has the fake offset: */ |
| 219 | pgoff = ((unsigned long)vmf->virtual_address - |
| 220 | vma->vm_start) >> PAGE_SHIFT; |
| 221 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 222 | pfn = page_to_pfn(pages[pgoff]); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 223 | |
| 224 | VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address, |
| 225 | pfn, pfn << PAGE_SHIFT); |
| 226 | |
Dan Williams | 01c8f1c | 2016-01-15 16:56:40 -0800 | [diff] [blame] | 227 | ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, |
| 228 | __pfn_to_pfn_t(pfn, PFN_DEV)); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 229 | |
| 230 | out_unlock: |
| 231 | mutex_unlock(&dev->struct_mutex); |
| 232 | out: |
| 233 | switch (ret) { |
| 234 | case -EAGAIN: |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 235 | case 0: |
| 236 | case -ERESTARTSYS: |
| 237 | case -EINTR: |
Rob Clark | 505886d | 2013-10-20 11:57:52 -0400 | [diff] [blame] | 238 | case -EBUSY: |
| 239 | /* |
| 240 | * EBUSY is ok: this just means that another thread |
| 241 | * already did the job. |
| 242 | */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 243 | return VM_FAULT_NOPAGE; |
| 244 | case -ENOMEM: |
| 245 | return VM_FAULT_OOM; |
| 246 | default: |
| 247 | return VM_FAULT_SIGBUS; |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** get mmap offset */ |
| 252 | static uint64_t mmap_offset(struct drm_gem_object *obj) |
| 253 | { |
| 254 | struct drm_device *dev = obj->dev; |
| 255 | int ret; |
| 256 | |
| 257 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 258 | |
| 259 | /* Make it mmapable */ |
| 260 | ret = drm_gem_create_mmap_offset(obj); |
| 261 | |
| 262 | if (ret) { |
| 263 | dev_err(dev->dev, "could not allocate mmap offset\n"); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | return drm_vma_node_offset_addr(&obj->vma_node); |
| 268 | } |
| 269 | |
| 270 | uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj) |
| 271 | { |
| 272 | uint64_t offset; |
| 273 | mutex_lock(&obj->dev->struct_mutex); |
| 274 | offset = mmap_offset(obj); |
| 275 | mutex_unlock(&obj->dev->struct_mutex); |
| 276 | return offset; |
| 277 | } |
| 278 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 279 | /* should be called under struct_mutex.. although it can be called |
| 280 | * from atomic context without struct_mutex to acquire an extra |
| 281 | * iova ref if you know one is already held. |
| 282 | * |
| 283 | * That means when I do eventually need to add support for unpinning |
| 284 | * the refcnt counter needs to be atomic_t. |
| 285 | */ |
| 286 | int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id, |
| 287 | uint32_t *iova) |
| 288 | { |
| 289 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 290 | int ret = 0; |
| 291 | |
| 292 | if (!msm_obj->domain[id].iova) { |
| 293 | struct msm_drm_private *priv = obj->dev->dev_private; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 294 | struct page **pages = get_pages(obj); |
| 295 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 296 | if (IS_ERR(pages)) |
| 297 | return PTR_ERR(pages); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 298 | |
| 299 | if (iommu_present(&platform_bus_type)) { |
Rob Clark | 1c4997f | 2014-07-01 14:49:55 -0400 | [diff] [blame] | 300 | struct msm_mmu *mmu = priv->mmus[id]; |
| 301 | uint32_t offset; |
| 302 | |
| 303 | if (WARN_ON(!mmu)) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | offset = (uint32_t)mmap_offset(obj); |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 307 | ret = mmu->funcs->map(mmu, offset, msm_obj->sgt, |
| 308 | obj->size, IOMMU_READ | IOMMU_WRITE); |
| 309 | msm_obj->domain[id].iova = offset; |
| 310 | } else { |
| 311 | msm_obj->domain[id].iova = physaddr(obj); |
| 312 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | if (!ret) |
| 316 | *iova = msm_obj->domain[id].iova; |
| 317 | |
| 318 | return ret; |
| 319 | } |
| 320 | |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 321 | /* get iova, taking a reference. Should have a matching put */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 322 | int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint32_t *iova) |
| 323 | { |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame] | 324 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 325 | int ret; |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame] | 326 | |
| 327 | /* this is safe right now because we don't unmap until the |
| 328 | * bo is deleted: |
| 329 | */ |
| 330 | if (msm_obj->domain[id].iova) { |
| 331 | *iova = msm_obj->domain[id].iova; |
| 332 | return 0; |
| 333 | } |
| 334 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 335 | mutex_lock(&obj->dev->struct_mutex); |
| 336 | ret = msm_gem_get_iova_locked(obj, id, iova); |
| 337 | mutex_unlock(&obj->dev->struct_mutex); |
| 338 | return ret; |
| 339 | } |
| 340 | |
Rob Clark | 2638d90 | 2014-11-08 09:13:37 -0500 | [diff] [blame] | 341 | /* get iova without taking a reference, used in places where you have |
| 342 | * already done a 'msm_gem_get_iova()'. |
| 343 | */ |
| 344 | uint32_t msm_gem_iova(struct drm_gem_object *obj, int id) |
| 345 | { |
| 346 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 347 | WARN_ON(!msm_obj->domain[id].iova); |
| 348 | return msm_obj->domain[id].iova; |
| 349 | } |
| 350 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 351 | void msm_gem_put_iova(struct drm_gem_object *obj, int id) |
| 352 | { |
| 353 | // XXX TODO .. |
| 354 | // NOTE: probably don't need a _locked() version.. we wouldn't |
| 355 | // normally unmap here, but instead just mark that it could be |
| 356 | // unmapped (if the iova refcnt drops to zero), but then later |
| 357 | // if another _get_iova_locked() fails we can start unmapping |
| 358 | // things that are no longer needed.. |
| 359 | } |
| 360 | |
| 361 | int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 362 | struct drm_mode_create_dumb *args) |
| 363 | { |
| 364 | args->pitch = align_pitch(args->width, args->bpp); |
| 365 | args->size = PAGE_ALIGN(args->pitch * args->height); |
| 366 | return msm_gem_new_handle(dev, file, args->size, |
| 367 | MSM_BO_SCANOUT | MSM_BO_WC, &args->handle); |
| 368 | } |
| 369 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 370 | int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, |
| 371 | uint32_t handle, uint64_t *offset) |
| 372 | { |
| 373 | struct drm_gem_object *obj; |
| 374 | int ret = 0; |
| 375 | |
| 376 | /* GEM does all our handle to object mapping */ |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame^] | 377 | obj = drm_gem_object_lookup(file, handle); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 378 | if (obj == NULL) { |
| 379 | ret = -ENOENT; |
| 380 | goto fail; |
| 381 | } |
| 382 | |
| 383 | *offset = msm_gem_mmap_offset(obj); |
| 384 | |
| 385 | drm_gem_object_unreference_unlocked(obj); |
| 386 | |
| 387 | fail: |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | void *msm_gem_vaddr_locked(struct drm_gem_object *obj) |
| 392 | { |
| 393 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 394 | WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); |
| 395 | if (!msm_obj->vaddr) { |
| 396 | struct page **pages = get_pages(obj); |
| 397 | if (IS_ERR(pages)) |
| 398 | return ERR_CAST(pages); |
| 399 | msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT, |
| 400 | VM_MAP, pgprot_writecombine(PAGE_KERNEL)); |
| 401 | } |
| 402 | return msm_obj->vaddr; |
| 403 | } |
| 404 | |
| 405 | void *msm_gem_vaddr(struct drm_gem_object *obj) |
| 406 | { |
| 407 | void *ret; |
| 408 | mutex_lock(&obj->dev->struct_mutex); |
| 409 | ret = msm_gem_vaddr_locked(obj); |
| 410 | mutex_unlock(&obj->dev->struct_mutex); |
| 411 | return ret; |
| 412 | } |
| 413 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 414 | /* must be called before _move_to_active().. */ |
| 415 | int msm_gem_sync_object(struct drm_gem_object *obj, |
| 416 | struct msm_fence_context *fctx, bool exclusive) |
| 417 | { |
| 418 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 419 | struct reservation_object_list *fobj; |
| 420 | struct fence *fence; |
| 421 | int i, ret; |
| 422 | |
| 423 | if (!exclusive) { |
| 424 | /* NOTE: _reserve_shared() must happen before _add_shared_fence(), |
| 425 | * which makes this a slightly strange place to call it. OTOH this |
| 426 | * is a convenient can-fail point to hook it in. (And similar to |
| 427 | * how etnaviv and nouveau handle this.) |
| 428 | */ |
| 429 | ret = reservation_object_reserve_shared(msm_obj->resv); |
| 430 | if (ret) |
| 431 | return ret; |
| 432 | } |
| 433 | |
| 434 | fobj = reservation_object_get_list(msm_obj->resv); |
| 435 | if (!fobj || (fobj->shared_count == 0)) { |
| 436 | fence = reservation_object_get_excl(msm_obj->resv); |
| 437 | /* don't need to wait on our own fences, since ring is fifo */ |
| 438 | if (fence && (fence->context != fctx->context)) { |
| 439 | ret = fence_wait(fence, true); |
| 440 | if (ret) |
| 441 | return ret; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (!exclusive || !fobj) |
| 446 | return 0; |
| 447 | |
| 448 | for (i = 0; i < fobj->shared_count; i++) { |
| 449 | fence = rcu_dereference_protected(fobj->shared[i], |
| 450 | reservation_object_held(msm_obj->resv)); |
| 451 | if (fence->context != fctx->context) { |
| 452 | ret = fence_wait(fence, true); |
| 453 | if (ret) |
| 454 | return ret; |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | return 0; |
| 459 | } |
| 460 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 461 | void msm_gem_move_to_active(struct drm_gem_object *obj, |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 462 | struct msm_gpu *gpu, bool exclusive, struct fence *fence) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 463 | { |
| 464 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 465 | msm_obj->gpu = gpu; |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 466 | if (exclusive) |
| 467 | reservation_object_add_excl_fence(msm_obj->resv, fence); |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 468 | else |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 469 | reservation_object_add_shared_fence(msm_obj->resv, fence); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 470 | list_del_init(&msm_obj->mm_list); |
| 471 | list_add_tail(&msm_obj->mm_list, &gpu->active_list); |
| 472 | } |
| 473 | |
| 474 | void msm_gem_move_to_inactive(struct drm_gem_object *obj) |
| 475 | { |
| 476 | struct drm_device *dev = obj->dev; |
| 477 | struct msm_drm_private *priv = dev->dev_private; |
| 478 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 479 | |
| 480 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 481 | |
| 482 | msm_obj->gpu = NULL; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 483 | list_del_init(&msm_obj->mm_list); |
| 484 | list_add_tail(&msm_obj->mm_list, &priv->inactive_list); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 485 | } |
| 486 | |
Rob Clark | ba00c3f | 2016-03-16 18:18:17 -0400 | [diff] [blame] | 487 | int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, ktime_t *timeout) |
| 488 | { |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 489 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 490 | bool write = !!(op & MSM_PREP_WRITE); |
| 491 | |
| 492 | if (op & MSM_PREP_NOSYNC) { |
| 493 | if (!reservation_object_test_signaled_rcu(msm_obj->resv, write)) |
| 494 | return -EBUSY; |
| 495 | } else { |
| 496 | int ret; |
| 497 | |
| 498 | ret = reservation_object_wait_timeout_rcu(msm_obj->resv, write, |
| 499 | true, timeout_to_jiffies(timeout)); |
| 500 | if (ret <= 0) |
| 501 | return ret == 0 ? -ETIMEDOUT : ret; |
| 502 | } |
Rob Clark | ba00c3f | 2016-03-16 18:18:17 -0400 | [diff] [blame] | 503 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 504 | /* TODO cache maintenance */ |
| 505 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 506 | return 0; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | int msm_gem_cpu_fini(struct drm_gem_object *obj) |
| 510 | { |
| 511 | /* TODO cache maintenance */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | |
| 515 | #ifdef CONFIG_DEBUG_FS |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 516 | static void describe_fence(struct fence *fence, const char *type, |
| 517 | struct seq_file *m) |
| 518 | { |
| 519 | if (!fence_is_signaled(fence)) |
| 520 | seq_printf(m, "\t%9s: %s %s seq %u\n", type, |
| 521 | fence->ops->get_driver_name(fence), |
| 522 | fence->ops->get_timeline_name(fence), |
| 523 | fence->seqno); |
| 524 | } |
| 525 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 526 | void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m) |
| 527 | { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 528 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 529 | struct reservation_object *robj = msm_obj->resv; |
| 530 | struct reservation_object_list *fobj; |
| 531 | struct fence *fence; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 532 | uint64_t off = drm_vma_node_start(&obj->vma_node); |
| 533 | |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 534 | WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); |
| 535 | |
| 536 | seq_printf(m, "%08x: %c %2d (%2d) %08llx %p %zu\n", |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 537 | msm_obj->flags, is_active(msm_obj) ? 'A' : 'I', |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 538 | obj->name, obj->refcount.refcount.counter, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 539 | off, msm_obj->vaddr, obj->size); |
Rob Clark | b6295f9 | 2016-03-15 18:26:28 -0400 | [diff] [blame] | 540 | |
| 541 | rcu_read_lock(); |
| 542 | fobj = rcu_dereference(robj->fence); |
| 543 | if (fobj) { |
| 544 | unsigned int i, shared_count = fobj->shared_count; |
| 545 | |
| 546 | for (i = 0; i < shared_count; i++) { |
| 547 | fence = rcu_dereference(fobj->shared[i]); |
| 548 | describe_fence(fence, "Shared", m); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | fence = rcu_dereference(robj->fence_excl); |
| 553 | if (fence) |
| 554 | describe_fence(fence, "Exclusive", m); |
| 555 | rcu_read_unlock(); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | void msm_gem_describe_objects(struct list_head *list, struct seq_file *m) |
| 559 | { |
| 560 | struct msm_gem_object *msm_obj; |
| 561 | int count = 0; |
| 562 | size_t size = 0; |
| 563 | |
| 564 | list_for_each_entry(msm_obj, list, mm_list) { |
| 565 | struct drm_gem_object *obj = &msm_obj->base; |
| 566 | seq_printf(m, " "); |
| 567 | msm_gem_describe(obj, m); |
| 568 | count++; |
| 569 | size += obj->size; |
| 570 | } |
| 571 | |
| 572 | seq_printf(m, "Total %d objects, %zu bytes\n", count, size); |
| 573 | } |
| 574 | #endif |
| 575 | |
| 576 | void msm_gem_free_object(struct drm_gem_object *obj) |
| 577 | { |
| 578 | struct drm_device *dev = obj->dev; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 579 | struct msm_drm_private *priv = obj->dev->dev_private; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 580 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 581 | int id; |
| 582 | |
| 583 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 584 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 585 | /* object should not be on active list: */ |
| 586 | WARN_ON(is_active(msm_obj)); |
| 587 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 588 | list_del(&msm_obj->mm_list); |
| 589 | |
| 590 | for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 591 | struct msm_mmu *mmu = priv->mmus[id]; |
| 592 | if (mmu && msm_obj->domain[id].iova) { |
Rob Clark | 257d06f | 2014-11-12 15:25:50 -0500 | [diff] [blame] | 593 | uint32_t offset = msm_obj->domain[id].iova; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 594 | mmu->funcs->unmap(mmu, offset, msm_obj->sgt, obj->size); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 595 | } |
| 596 | } |
| 597 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 598 | if (obj->import_attach) { |
| 599 | if (msm_obj->vaddr) |
| 600 | dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 601 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 602 | /* Don't drop the pages for imported dmabuf, as they are not |
| 603 | * ours, just free the array we allocated: |
| 604 | */ |
| 605 | if (msm_obj->pages) |
| 606 | drm_free_large(msm_obj->pages); |
| 607 | |
jilai wang | f28730c | 2015-04-07 13:51:32 -0400 | [diff] [blame] | 608 | drm_prime_gem_destroy(obj, msm_obj->sgt); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 609 | } else { |
Markus Elfring | 264f7d6 | 2014-11-25 14:30:28 +0100 | [diff] [blame] | 610 | vunmap(msm_obj->vaddr); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 611 | put_pages(obj); |
| 612 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 613 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 614 | if (msm_obj->resv == &msm_obj->_resv) |
| 615 | reservation_object_fini(msm_obj->resv); |
| 616 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 617 | drm_gem_object_release(obj); |
| 618 | |
| 619 | kfree(msm_obj); |
| 620 | } |
| 621 | |
| 622 | /* convenience method to construct a GEM buffer object, and userspace handle */ |
| 623 | int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, |
| 624 | uint32_t size, uint32_t flags, uint32_t *handle) |
| 625 | { |
| 626 | struct drm_gem_object *obj; |
| 627 | int ret; |
| 628 | |
| 629 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 630 | if (ret) |
| 631 | return ret; |
| 632 | |
| 633 | obj = msm_gem_new(dev, size, flags); |
| 634 | |
| 635 | mutex_unlock(&dev->struct_mutex); |
| 636 | |
| 637 | if (IS_ERR(obj)) |
| 638 | return PTR_ERR(obj); |
| 639 | |
| 640 | ret = drm_gem_handle_create(file, obj, handle); |
| 641 | |
| 642 | /* drop reference from allocate - handle holds it now */ |
| 643 | drm_gem_object_unreference_unlocked(obj); |
| 644 | |
| 645 | return ret; |
| 646 | } |
| 647 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 648 | static int msm_gem_new_impl(struct drm_device *dev, |
| 649 | uint32_t size, uint32_t flags, |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 650 | struct reservation_object *resv, |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 651 | struct drm_gem_object **obj) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 652 | { |
| 653 | struct msm_drm_private *priv = dev->dev_private; |
| 654 | struct msm_gem_object *msm_obj; |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 655 | unsigned sz; |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 656 | bool use_vram = false; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 657 | |
| 658 | switch (flags & MSM_BO_CACHE_MASK) { |
| 659 | case MSM_BO_UNCACHED: |
| 660 | case MSM_BO_CACHED: |
| 661 | case MSM_BO_WC: |
| 662 | break; |
| 663 | default: |
| 664 | dev_err(dev->dev, "invalid cache flag: %x\n", |
| 665 | (flags & MSM_BO_CACHE_MASK)); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 666 | return -EINVAL; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 667 | } |
| 668 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 669 | if (!iommu_present(&platform_bus_type)) |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 670 | use_vram = true; |
| 671 | else if ((flags & MSM_BO_STOLEN) && priv->vram.size) |
| 672 | use_vram = true; |
| 673 | |
| 674 | if (WARN_ON(use_vram && !priv->vram.size)) |
| 675 | return -EINVAL; |
| 676 | |
| 677 | sz = sizeof(*msm_obj); |
| 678 | if (use_vram) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 679 | sz += sizeof(struct drm_mm_node); |
| 680 | |
| 681 | msm_obj = kzalloc(sz, GFP_KERNEL); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 682 | if (!msm_obj) |
| 683 | return -ENOMEM; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 684 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 685 | if (use_vram) |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 686 | msm_obj->vram_node = (void *)&msm_obj[1]; |
| 687 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 688 | msm_obj->flags = flags; |
| 689 | |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 690 | if (resv) { |
| 691 | msm_obj->resv = resv; |
| 692 | } else { |
| 693 | msm_obj->resv = &msm_obj->_resv; |
| 694 | reservation_object_init(msm_obj->resv); |
| 695 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 696 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 697 | INIT_LIST_HEAD(&msm_obj->submit_entry); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 698 | list_add_tail(&msm_obj->mm_list, &priv->inactive_list); |
| 699 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 700 | *obj = &msm_obj->base; |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | struct drm_gem_object *msm_gem_new(struct drm_device *dev, |
| 706 | uint32_t size, uint32_t flags) |
| 707 | { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 708 | struct drm_gem_object *obj = NULL; |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 709 | int ret; |
| 710 | |
| 711 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 712 | |
| 713 | size = PAGE_ALIGN(size); |
| 714 | |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 715 | ret = msm_gem_new_impl(dev, size, flags, NULL, &obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 716 | if (ret) |
| 717 | goto fail; |
| 718 | |
Rob Clark | 072f1f9 | 2015-03-03 15:04:25 -0500 | [diff] [blame] | 719 | if (use_pages(obj)) { |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 720 | ret = drm_gem_object_init(dev, obj, size); |
| 721 | if (ret) |
| 722 | goto fail; |
| 723 | } else { |
| 724 | drm_gem_private_object_init(dev, obj, size); |
| 725 | } |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 726 | |
| 727 | return obj; |
| 728 | |
| 729 | fail: |
| 730 | if (obj) |
Rob Clark | 9999f10 | 2014-02-04 14:17:32 -0500 | [diff] [blame] | 731 | drm_gem_object_unreference(obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 732 | |
| 733 | return ERR_PTR(ret); |
| 734 | } |
| 735 | |
| 736 | struct drm_gem_object *msm_gem_import(struct drm_device *dev, |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 737 | struct dma_buf *dmabuf, struct sg_table *sgt) |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 738 | { |
| 739 | struct msm_gem_object *msm_obj; |
| 740 | struct drm_gem_object *obj; |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 741 | uint32_t size; |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 742 | int ret, npages; |
| 743 | |
Rob Clark | 871d812 | 2013-11-16 12:56:06 -0500 | [diff] [blame] | 744 | /* if we don't have IOMMU, don't bother pretending we can import: */ |
| 745 | if (!iommu_present(&platform_bus_type)) { |
| 746 | dev_err(dev->dev, "cannot import without IOMMU\n"); |
| 747 | return ERR_PTR(-EINVAL); |
| 748 | } |
| 749 | |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 750 | size = PAGE_ALIGN(dmabuf->size); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 751 | |
Rob Clark | 79f0e20 | 2016-03-16 12:40:35 -0400 | [diff] [blame] | 752 | ret = msm_gem_new_impl(dev, size, MSM_BO_WC, dmabuf->resv, &obj); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 753 | if (ret) |
| 754 | goto fail; |
| 755 | |
| 756 | drm_gem_private_object_init(dev, obj, size); |
| 757 | |
| 758 | npages = size / PAGE_SIZE; |
| 759 | |
| 760 | msm_obj = to_msm_bo(obj); |
| 761 | msm_obj->sgt = sgt; |
| 762 | msm_obj->pages = drm_malloc_ab(npages, sizeof(struct page *)); |
| 763 | if (!msm_obj->pages) { |
| 764 | ret = -ENOMEM; |
| 765 | goto fail; |
| 766 | } |
| 767 | |
| 768 | ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages); |
| 769 | if (ret) |
| 770 | goto fail; |
| 771 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 772 | return obj; |
| 773 | |
| 774 | fail: |
| 775 | if (obj) |
| 776 | drm_gem_object_unreference_unlocked(obj); |
| 777 | |
| 778 | return ERR_PTR(ret); |
| 779 | } |