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> |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 21 | |
| 22 | #include "msm_drv.h" |
| 23 | #include "msm_gem.h" |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 24 | #include "msm_gpu.h" |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 25 | |
| 26 | |
| 27 | /* called with dev->struct_mutex held */ |
| 28 | static struct page **get_pages(struct drm_gem_object *obj) |
| 29 | { |
| 30 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 31 | |
| 32 | if (!msm_obj->pages) { |
| 33 | struct drm_device *dev = obj->dev; |
| 34 | struct page **p = drm_gem_get_pages(obj, 0); |
| 35 | int npages = obj->size >> PAGE_SHIFT; |
| 36 | |
| 37 | if (IS_ERR(p)) { |
| 38 | dev_err(dev->dev, "could not get pages: %ld\n", |
| 39 | PTR_ERR(p)); |
| 40 | return p; |
| 41 | } |
| 42 | |
| 43 | msm_obj->sgt = drm_prime_pages_to_sg(p, npages); |
Wei Yongjun | 1f70e07 | 2013-09-11 06:56:12 +0800 | [diff] [blame] | 44 | if (IS_ERR(msm_obj->sgt)) { |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 45 | dev_err(dev->dev, "failed to allocate sgt\n"); |
Wei Yongjun | 1f70e07 | 2013-09-11 06:56:12 +0800 | [diff] [blame] | 46 | return ERR_CAST(msm_obj->sgt); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | msm_obj->pages = p; |
| 50 | |
| 51 | /* For non-cached buffers, ensure the new pages are clean |
| 52 | * because display controller, GPU, etc. are not coherent: |
| 53 | */ |
| 54 | if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) |
| 55 | dma_map_sg(dev->dev, msm_obj->sgt->sgl, |
| 56 | msm_obj->sgt->nents, DMA_BIDIRECTIONAL); |
| 57 | } |
| 58 | |
| 59 | return msm_obj->pages; |
| 60 | } |
| 61 | |
| 62 | static void put_pages(struct drm_gem_object *obj) |
| 63 | { |
| 64 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 65 | |
| 66 | if (msm_obj->pages) { |
| 67 | /* For non-cached buffers, ensure the new pages are clean |
| 68 | * because display controller, GPU, etc. are not coherent: |
| 69 | */ |
| 70 | if (msm_obj->flags & (MSM_BO_WC|MSM_BO_UNCACHED)) |
| 71 | dma_unmap_sg(obj->dev->dev, msm_obj->sgt->sgl, |
| 72 | msm_obj->sgt->nents, DMA_BIDIRECTIONAL); |
| 73 | sg_free_table(msm_obj->sgt); |
| 74 | kfree(msm_obj->sgt); |
| 75 | |
| 76 | drm_gem_put_pages(obj, msm_obj->pages, true, false); |
| 77 | msm_obj->pages = NULL; |
| 78 | } |
| 79 | } |
| 80 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 81 | struct page **msm_gem_get_pages(struct drm_gem_object *obj) |
| 82 | { |
| 83 | struct drm_device *dev = obj->dev; |
| 84 | struct page **p; |
| 85 | mutex_lock(&dev->struct_mutex); |
| 86 | p = get_pages(obj); |
| 87 | mutex_unlock(&dev->struct_mutex); |
| 88 | return p; |
| 89 | } |
| 90 | |
| 91 | void msm_gem_put_pages(struct drm_gem_object *obj) |
| 92 | { |
| 93 | /* when we start tracking the pin count, then do something here */ |
| 94 | } |
| 95 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 96 | int msm_gem_mmap_obj(struct drm_gem_object *obj, |
| 97 | struct vm_area_struct *vma) |
| 98 | { |
| 99 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 100 | |
| 101 | vma->vm_flags &= ~VM_PFNMAP; |
| 102 | vma->vm_flags |= VM_MIXEDMAP; |
| 103 | |
| 104 | if (msm_obj->flags & MSM_BO_WC) { |
| 105 | vma->vm_page_prot = pgprot_writecombine(vm_get_page_prot(vma->vm_flags)); |
| 106 | } else if (msm_obj->flags & MSM_BO_UNCACHED) { |
| 107 | vma->vm_page_prot = pgprot_noncached(vm_get_page_prot(vma->vm_flags)); |
| 108 | } else { |
| 109 | /* |
| 110 | * Shunt off cached objs to shmem file so they have their own |
| 111 | * address_space (so unmap_mapping_range does what we want, |
| 112 | * in particular in the case of mmap'd dmabufs) |
| 113 | */ |
| 114 | fput(vma->vm_file); |
| 115 | get_file(obj->filp); |
| 116 | vma->vm_pgoff = 0; |
| 117 | vma->vm_file = obj->filp; |
| 118 | |
| 119 | vma->vm_page_prot = vm_get_page_prot(vma->vm_flags); |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int msm_gem_mmap(struct file *filp, struct vm_area_struct *vma) |
| 126 | { |
| 127 | int ret; |
| 128 | |
| 129 | ret = drm_gem_mmap(filp, vma); |
| 130 | if (ret) { |
| 131 | DBG("mmap failed: %d", ret); |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | return msm_gem_mmap_obj(vma->vm_private_data, vma); |
| 136 | } |
| 137 | |
| 138 | int msm_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 139 | { |
| 140 | struct drm_gem_object *obj = vma->vm_private_data; |
| 141 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 142 | struct drm_device *dev = obj->dev; |
| 143 | struct page **pages; |
| 144 | unsigned long pfn; |
| 145 | pgoff_t pgoff; |
| 146 | int ret; |
| 147 | |
| 148 | /* Make sure we don't parallel update on a fault, nor move or remove |
| 149 | * something from beneath our feet |
| 150 | */ |
| 151 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 152 | if (ret) |
| 153 | goto out; |
| 154 | |
| 155 | /* make sure we have pages attached now */ |
| 156 | pages = get_pages(obj); |
| 157 | if (IS_ERR(pages)) { |
| 158 | ret = PTR_ERR(pages); |
| 159 | goto out_unlock; |
| 160 | } |
| 161 | |
| 162 | /* We don't use vmf->pgoff since that has the fake offset: */ |
| 163 | pgoff = ((unsigned long)vmf->virtual_address - |
| 164 | vma->vm_start) >> PAGE_SHIFT; |
| 165 | |
| 166 | pfn = page_to_pfn(msm_obj->pages[pgoff]); |
| 167 | |
| 168 | VERB("Inserting %p pfn %lx, pa %lx", vmf->virtual_address, |
| 169 | pfn, pfn << PAGE_SHIFT); |
| 170 | |
| 171 | ret = vm_insert_mixed(vma, (unsigned long)vmf->virtual_address, pfn); |
| 172 | |
| 173 | out_unlock: |
| 174 | mutex_unlock(&dev->struct_mutex); |
| 175 | out: |
| 176 | switch (ret) { |
| 177 | case -EAGAIN: |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 178 | case 0: |
| 179 | case -ERESTARTSYS: |
| 180 | case -EINTR: |
| 181 | return VM_FAULT_NOPAGE; |
| 182 | case -ENOMEM: |
| 183 | return VM_FAULT_OOM; |
| 184 | default: |
| 185 | return VM_FAULT_SIGBUS; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | /** get mmap offset */ |
| 190 | static uint64_t mmap_offset(struct drm_gem_object *obj) |
| 191 | { |
| 192 | struct drm_device *dev = obj->dev; |
| 193 | int ret; |
| 194 | |
| 195 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 196 | |
| 197 | /* Make it mmapable */ |
| 198 | ret = drm_gem_create_mmap_offset(obj); |
| 199 | |
| 200 | if (ret) { |
| 201 | dev_err(dev->dev, "could not allocate mmap offset\n"); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | return drm_vma_node_offset_addr(&obj->vma_node); |
| 206 | } |
| 207 | |
| 208 | uint64_t msm_gem_mmap_offset(struct drm_gem_object *obj) |
| 209 | { |
| 210 | uint64_t offset; |
| 211 | mutex_lock(&obj->dev->struct_mutex); |
| 212 | offset = mmap_offset(obj); |
| 213 | mutex_unlock(&obj->dev->struct_mutex); |
| 214 | return offset; |
| 215 | } |
| 216 | |
| 217 | /* helpers for dealing w/ iommu: */ |
| 218 | static int map_range(struct iommu_domain *domain, unsigned int iova, |
| 219 | struct sg_table *sgt, unsigned int len, int prot) |
| 220 | { |
| 221 | struct scatterlist *sg; |
| 222 | unsigned int da = iova; |
| 223 | unsigned int i, j; |
| 224 | int ret; |
| 225 | |
| 226 | if (!domain || !sgt) |
| 227 | return -EINVAL; |
| 228 | |
| 229 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 230 | u32 pa = sg_phys(sg) - sg->offset; |
| 231 | size_t bytes = sg->length + sg->offset; |
| 232 | |
| 233 | VERB("map[%d]: %08x %08x(%x)", i, iova, pa, bytes); |
| 234 | |
| 235 | ret = iommu_map(domain, da, pa, bytes, prot); |
| 236 | if (ret) |
| 237 | goto fail; |
| 238 | |
| 239 | da += bytes; |
| 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | |
| 244 | fail: |
| 245 | da = iova; |
| 246 | |
| 247 | for_each_sg(sgt->sgl, sg, i, j) { |
| 248 | size_t bytes = sg->length + sg->offset; |
| 249 | iommu_unmap(domain, da, bytes); |
| 250 | da += bytes; |
| 251 | } |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | static void unmap_range(struct iommu_domain *domain, unsigned int iova, |
| 256 | struct sg_table *sgt, unsigned int len) |
| 257 | { |
| 258 | struct scatterlist *sg; |
| 259 | unsigned int da = iova; |
| 260 | int i; |
| 261 | |
| 262 | for_each_sg(sgt->sgl, sg, sgt->nents, i) { |
| 263 | size_t bytes = sg->length + sg->offset; |
| 264 | size_t unmapped; |
| 265 | |
| 266 | unmapped = iommu_unmap(domain, da, bytes); |
| 267 | if (unmapped < bytes) |
| 268 | break; |
| 269 | |
| 270 | VERB("unmap[%d]: %08x(%x)", i, iova, bytes); |
| 271 | |
| 272 | BUG_ON(!IS_ALIGNED(bytes, PAGE_SIZE)); |
| 273 | |
| 274 | da += bytes; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* should be called under struct_mutex.. although it can be called |
| 279 | * from atomic context without struct_mutex to acquire an extra |
| 280 | * iova ref if you know one is already held. |
| 281 | * |
| 282 | * That means when I do eventually need to add support for unpinning |
| 283 | * the refcnt counter needs to be atomic_t. |
| 284 | */ |
| 285 | int msm_gem_get_iova_locked(struct drm_gem_object *obj, int id, |
| 286 | uint32_t *iova) |
| 287 | { |
| 288 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 289 | int ret = 0; |
| 290 | |
| 291 | if (!msm_obj->domain[id].iova) { |
| 292 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 293 | uint32_t offset = (uint32_t)mmap_offset(obj); |
| 294 | struct page **pages; |
| 295 | pages = get_pages(obj); |
| 296 | if (IS_ERR(pages)) |
| 297 | return PTR_ERR(pages); |
| 298 | // XXX ideally we would not map buffers writable when not needed... |
| 299 | ret = map_range(priv->iommus[id], offset, msm_obj->sgt, |
| 300 | obj->size, IOMMU_READ | IOMMU_WRITE); |
| 301 | msm_obj->domain[id].iova = offset; |
| 302 | } |
| 303 | |
| 304 | if (!ret) |
| 305 | *iova = msm_obj->domain[id].iova; |
| 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | int msm_gem_get_iova(struct drm_gem_object *obj, int id, uint32_t *iova) |
| 311 | { |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame^] | 312 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 313 | int ret; |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame^] | 314 | |
| 315 | /* this is safe right now because we don't unmap until the |
| 316 | * bo is deleted: |
| 317 | */ |
| 318 | if (msm_obj->domain[id].iova) { |
| 319 | *iova = msm_obj->domain[id].iova; |
| 320 | return 0; |
| 321 | } |
| 322 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 323 | mutex_lock(&obj->dev->struct_mutex); |
| 324 | ret = msm_gem_get_iova_locked(obj, id, iova); |
| 325 | mutex_unlock(&obj->dev->struct_mutex); |
| 326 | return ret; |
| 327 | } |
| 328 | |
| 329 | void msm_gem_put_iova(struct drm_gem_object *obj, int id) |
| 330 | { |
| 331 | // XXX TODO .. |
| 332 | // NOTE: probably don't need a _locked() version.. we wouldn't |
| 333 | // normally unmap here, but instead just mark that it could be |
| 334 | // unmapped (if the iova refcnt drops to zero), but then later |
| 335 | // if another _get_iova_locked() fails we can start unmapping |
| 336 | // things that are no longer needed.. |
| 337 | } |
| 338 | |
| 339 | int msm_gem_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 340 | struct drm_mode_create_dumb *args) |
| 341 | { |
| 342 | args->pitch = align_pitch(args->width, args->bpp); |
| 343 | args->size = PAGE_ALIGN(args->pitch * args->height); |
| 344 | return msm_gem_new_handle(dev, file, args->size, |
| 345 | MSM_BO_SCANOUT | MSM_BO_WC, &args->handle); |
| 346 | } |
| 347 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 348 | int msm_gem_dumb_map_offset(struct drm_file *file, struct drm_device *dev, |
| 349 | uint32_t handle, uint64_t *offset) |
| 350 | { |
| 351 | struct drm_gem_object *obj; |
| 352 | int ret = 0; |
| 353 | |
| 354 | /* GEM does all our handle to object mapping */ |
| 355 | obj = drm_gem_object_lookup(dev, file, handle); |
| 356 | if (obj == NULL) { |
| 357 | ret = -ENOENT; |
| 358 | goto fail; |
| 359 | } |
| 360 | |
| 361 | *offset = msm_gem_mmap_offset(obj); |
| 362 | |
| 363 | drm_gem_object_unreference_unlocked(obj); |
| 364 | |
| 365 | fail: |
| 366 | return ret; |
| 367 | } |
| 368 | |
| 369 | void *msm_gem_vaddr_locked(struct drm_gem_object *obj) |
| 370 | { |
| 371 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 372 | WARN_ON(!mutex_is_locked(&obj->dev->struct_mutex)); |
| 373 | if (!msm_obj->vaddr) { |
| 374 | struct page **pages = get_pages(obj); |
| 375 | if (IS_ERR(pages)) |
| 376 | return ERR_CAST(pages); |
| 377 | msm_obj->vaddr = vmap(pages, obj->size >> PAGE_SHIFT, |
| 378 | VM_MAP, pgprot_writecombine(PAGE_KERNEL)); |
| 379 | } |
| 380 | return msm_obj->vaddr; |
| 381 | } |
| 382 | |
| 383 | void *msm_gem_vaddr(struct drm_gem_object *obj) |
| 384 | { |
| 385 | void *ret; |
| 386 | mutex_lock(&obj->dev->struct_mutex); |
| 387 | ret = msm_gem_vaddr_locked(obj); |
| 388 | mutex_unlock(&obj->dev->struct_mutex); |
| 389 | return ret; |
| 390 | } |
| 391 | |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame^] | 392 | /* setup callback for when bo is no longer busy.. |
| 393 | * TODO probably want to differentiate read vs write.. |
| 394 | */ |
| 395 | int msm_gem_queue_inactive_cb(struct drm_gem_object *obj, |
| 396 | struct msm_fence_cb *cb) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 397 | { |
| 398 | struct drm_device *dev = obj->dev; |
| 399 | struct msm_drm_private *priv = dev->dev_private; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 400 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 401 | int ret = 0; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 402 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 403 | mutex_lock(&dev->struct_mutex); |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame^] | 404 | if (!list_empty(&cb->work.entry)) { |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 405 | ret = -EINVAL; |
| 406 | } else if (is_active(msm_obj)) { |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame^] | 407 | cb->fence = max(msm_obj->read_fence, msm_obj->write_fence); |
| 408 | list_add_tail(&cb->work.entry, &priv->fence_cbs); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 409 | } else { |
Rob Clark | edd4fc6 | 2013-09-14 14:01:55 -0400 | [diff] [blame^] | 410 | queue_work(priv->wq, &cb->work); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 411 | } |
| 412 | mutex_unlock(&dev->struct_mutex); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 413 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 414 | return ret; |
| 415 | } |
| 416 | |
| 417 | void msm_gem_move_to_active(struct drm_gem_object *obj, |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 418 | struct msm_gpu *gpu, bool write, uint32_t fence) |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 419 | { |
| 420 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 421 | msm_obj->gpu = gpu; |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 422 | if (write) |
| 423 | msm_obj->write_fence = fence; |
| 424 | else |
| 425 | msm_obj->read_fence = fence; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 426 | list_del_init(&msm_obj->mm_list); |
| 427 | list_add_tail(&msm_obj->mm_list, &gpu->active_list); |
| 428 | } |
| 429 | |
| 430 | void msm_gem_move_to_inactive(struct drm_gem_object *obj) |
| 431 | { |
| 432 | struct drm_device *dev = obj->dev; |
| 433 | struct msm_drm_private *priv = dev->dev_private; |
| 434 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 435 | |
| 436 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 437 | |
| 438 | msm_obj->gpu = NULL; |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 439 | msm_obj->read_fence = 0; |
| 440 | msm_obj->write_fence = 0; |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 441 | list_del_init(&msm_obj->mm_list); |
| 442 | list_add_tail(&msm_obj->mm_list, &priv->inactive_list); |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | int msm_gem_cpu_prep(struct drm_gem_object *obj, uint32_t op, |
| 446 | struct timespec *timeout) |
| 447 | { |
| 448 | struct drm_device *dev = obj->dev; |
| 449 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 450 | int ret = 0; |
| 451 | |
Rob Clark | f816f27 | 2013-09-11 17:34:07 -0400 | [diff] [blame] | 452 | if (is_active(msm_obj)) { |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 453 | uint32_t fence = 0; |
Rob Clark | f816f27 | 2013-09-11 17:34:07 -0400 | [diff] [blame] | 454 | |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 455 | if (op & MSM_PREP_READ) |
| 456 | fence = msm_obj->write_fence; |
| 457 | if (op & MSM_PREP_WRITE) |
| 458 | fence = max(fence, msm_obj->read_fence); |
Rob Clark | f816f27 | 2013-09-11 17:34:07 -0400 | [diff] [blame] | 459 | if (op & MSM_PREP_NOSYNC) |
| 460 | timeout = NULL; |
| 461 | |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 462 | ret = msm_wait_fence_interruptable(dev, fence, timeout); |
| 463 | } |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 464 | |
| 465 | /* TODO cache maintenance */ |
| 466 | |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | int msm_gem_cpu_fini(struct drm_gem_object *obj) |
| 471 | { |
| 472 | /* TODO cache maintenance */ |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | #ifdef CONFIG_DEBUG_FS |
| 477 | void msm_gem_describe(struct drm_gem_object *obj, struct seq_file *m) |
| 478 | { |
| 479 | struct drm_device *dev = obj->dev; |
| 480 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 481 | uint64_t off = drm_vma_node_start(&obj->vma_node); |
| 482 | |
| 483 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 484 | seq_printf(m, "%08x: %c(r=%u,w=%u) %2d (%2d) %08llx %p %d\n", |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 485 | msm_obj->flags, is_active(msm_obj) ? 'A' : 'I', |
Rob Clark | bf6811f | 2013-09-01 13:25:09 -0400 | [diff] [blame] | 486 | msm_obj->read_fence, msm_obj->write_fence, |
| 487 | obj->name, obj->refcount.refcount.counter, |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 488 | off, msm_obj->vaddr, obj->size); |
| 489 | } |
| 490 | |
| 491 | void msm_gem_describe_objects(struct list_head *list, struct seq_file *m) |
| 492 | { |
| 493 | struct msm_gem_object *msm_obj; |
| 494 | int count = 0; |
| 495 | size_t size = 0; |
| 496 | |
| 497 | list_for_each_entry(msm_obj, list, mm_list) { |
| 498 | struct drm_gem_object *obj = &msm_obj->base; |
| 499 | seq_printf(m, " "); |
| 500 | msm_gem_describe(obj, m); |
| 501 | count++; |
| 502 | size += obj->size; |
| 503 | } |
| 504 | |
| 505 | seq_printf(m, "Total %d objects, %zu bytes\n", count, size); |
| 506 | } |
| 507 | #endif |
| 508 | |
| 509 | void msm_gem_free_object(struct drm_gem_object *obj) |
| 510 | { |
| 511 | struct drm_device *dev = obj->dev; |
| 512 | struct msm_gem_object *msm_obj = to_msm_bo(obj); |
| 513 | int id; |
| 514 | |
| 515 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 516 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 517 | /* object should not be on active list: */ |
| 518 | WARN_ON(is_active(msm_obj)); |
| 519 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 520 | list_del(&msm_obj->mm_list); |
| 521 | |
| 522 | for (id = 0; id < ARRAY_SIZE(msm_obj->domain); id++) { |
| 523 | if (msm_obj->domain[id].iova) { |
| 524 | struct msm_drm_private *priv = obj->dev->dev_private; |
| 525 | uint32_t offset = (uint32_t)mmap_offset(obj); |
| 526 | unmap_range(priv->iommus[id], offset, |
| 527 | msm_obj->sgt, obj->size); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | drm_gem_free_mmap_offset(obj); |
| 532 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 533 | if (obj->import_attach) { |
| 534 | if (msm_obj->vaddr) |
| 535 | dma_buf_vunmap(obj->import_attach->dmabuf, msm_obj->vaddr); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 536 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 537 | /* Don't drop the pages for imported dmabuf, as they are not |
| 538 | * ours, just free the array we allocated: |
| 539 | */ |
| 540 | if (msm_obj->pages) |
| 541 | drm_free_large(msm_obj->pages); |
| 542 | |
| 543 | } else { |
| 544 | if (msm_obj->vaddr) |
| 545 | vunmap(msm_obj->vaddr); |
| 546 | put_pages(obj); |
| 547 | } |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 548 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 549 | if (msm_obj->resv == &msm_obj->_resv) |
| 550 | reservation_object_fini(msm_obj->resv); |
| 551 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 552 | drm_gem_object_release(obj); |
| 553 | |
| 554 | kfree(msm_obj); |
| 555 | } |
| 556 | |
| 557 | /* convenience method to construct a GEM buffer object, and userspace handle */ |
| 558 | int msm_gem_new_handle(struct drm_device *dev, struct drm_file *file, |
| 559 | uint32_t size, uint32_t flags, uint32_t *handle) |
| 560 | { |
| 561 | struct drm_gem_object *obj; |
| 562 | int ret; |
| 563 | |
| 564 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 565 | if (ret) |
| 566 | return ret; |
| 567 | |
| 568 | obj = msm_gem_new(dev, size, flags); |
| 569 | |
| 570 | mutex_unlock(&dev->struct_mutex); |
| 571 | |
| 572 | if (IS_ERR(obj)) |
| 573 | return PTR_ERR(obj); |
| 574 | |
| 575 | ret = drm_gem_handle_create(file, obj, handle); |
| 576 | |
| 577 | /* drop reference from allocate - handle holds it now */ |
| 578 | drm_gem_object_unreference_unlocked(obj); |
| 579 | |
| 580 | return ret; |
| 581 | } |
| 582 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 583 | static int msm_gem_new_impl(struct drm_device *dev, |
| 584 | uint32_t size, uint32_t flags, |
| 585 | struct drm_gem_object **obj) |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 586 | { |
| 587 | struct msm_drm_private *priv = dev->dev_private; |
| 588 | struct msm_gem_object *msm_obj; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 589 | |
| 590 | switch (flags & MSM_BO_CACHE_MASK) { |
| 591 | case MSM_BO_UNCACHED: |
| 592 | case MSM_BO_CACHED: |
| 593 | case MSM_BO_WC: |
| 594 | break; |
| 595 | default: |
| 596 | dev_err(dev->dev, "invalid cache flag: %x\n", |
| 597 | (flags & MSM_BO_CACHE_MASK)); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 598 | return -EINVAL; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | msm_obj = kzalloc(sizeof(*msm_obj), GFP_KERNEL); |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 602 | if (!msm_obj) |
| 603 | return -ENOMEM; |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 604 | |
| 605 | msm_obj->flags = flags; |
| 606 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 607 | msm_obj->resv = &msm_obj->_resv; |
| 608 | reservation_object_init(msm_obj->resv); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 609 | |
Rob Clark | 7198e6b | 2013-07-19 12:59:32 -0400 | [diff] [blame] | 610 | INIT_LIST_HEAD(&msm_obj->submit_entry); |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 611 | list_add_tail(&msm_obj->mm_list, &priv->inactive_list); |
| 612 | |
Rob Clark | 05b8491 | 2013-09-28 11:28:35 -0400 | [diff] [blame] | 613 | *obj = &msm_obj->base; |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | struct drm_gem_object *msm_gem_new(struct drm_device *dev, |
| 619 | uint32_t size, uint32_t flags) |
| 620 | { |
| 621 | struct drm_gem_object *obj; |
| 622 | int ret; |
| 623 | |
| 624 | WARN_ON(!mutex_is_locked(&dev->struct_mutex)); |
| 625 | |
| 626 | size = PAGE_ALIGN(size); |
| 627 | |
| 628 | ret = msm_gem_new_impl(dev, size, flags, &obj); |
| 629 | if (ret) |
| 630 | goto fail; |
| 631 | |
| 632 | ret = drm_gem_object_init(dev, obj, size); |
| 633 | if (ret) |
| 634 | goto fail; |
| 635 | |
| 636 | return obj; |
| 637 | |
| 638 | fail: |
| 639 | if (obj) |
| 640 | drm_gem_object_unreference_unlocked(obj); |
| 641 | |
| 642 | return ERR_PTR(ret); |
| 643 | } |
| 644 | |
| 645 | struct drm_gem_object *msm_gem_import(struct drm_device *dev, |
| 646 | uint32_t size, struct sg_table *sgt) |
| 647 | { |
| 648 | struct msm_gem_object *msm_obj; |
| 649 | struct drm_gem_object *obj; |
| 650 | int ret, npages; |
| 651 | |
| 652 | size = PAGE_ALIGN(size); |
| 653 | |
| 654 | ret = msm_gem_new_impl(dev, size, MSM_BO_WC, &obj); |
| 655 | if (ret) |
| 656 | goto fail; |
| 657 | |
| 658 | drm_gem_private_object_init(dev, obj, size); |
| 659 | |
| 660 | npages = size / PAGE_SIZE; |
| 661 | |
| 662 | msm_obj = to_msm_bo(obj); |
| 663 | msm_obj->sgt = sgt; |
| 664 | msm_obj->pages = drm_malloc_ab(npages, sizeof(struct page *)); |
| 665 | if (!msm_obj->pages) { |
| 666 | ret = -ENOMEM; |
| 667 | goto fail; |
| 668 | } |
| 669 | |
| 670 | ret = drm_prime_sg_to_page_addr_arrays(sgt, msm_obj->pages, NULL, npages); |
| 671 | if (ret) |
| 672 | goto fail; |
| 673 | |
Rob Clark | c8afe68 | 2013-06-26 12:44:06 -0400 | [diff] [blame] | 674 | return obj; |
| 675 | |
| 676 | fail: |
| 677 | if (obj) |
| 678 | drm_gem_object_unreference_unlocked(obj); |
| 679 | |
| 680 | return ERR_PTR(ret); |
| 681 | } |