Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 "msm_drv.h" |
| 19 | #include "msm_gem.h" |
| 20 | #include "msm_mmu.h" |
| 21 | |
Jordan Crouse | ee546cd | 2017-03-07 10:02:52 -0700 | [diff] [blame] | 22 | static void |
| 23 | msm_gem_address_space_destroy(struct kref *kref) |
| 24 | { |
| 25 | struct msm_gem_address_space *aspace = container_of(kref, |
| 26 | struct msm_gem_address_space, kref); |
| 27 | |
| 28 | drm_mm_takedown(&aspace->mm); |
| 29 | if (aspace->mmu) |
| 30 | aspace->mmu->funcs->destroy(aspace->mmu); |
| 31 | kfree(aspace); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | void msm_gem_address_space_put(struct msm_gem_address_space *aspace) |
| 36 | { |
| 37 | if (aspace) |
| 38 | kref_put(&aspace->kref, msm_gem_address_space_destroy); |
| 39 | } |
| 40 | |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 41 | void |
| 42 | msm_gem_unmap_vma(struct msm_gem_address_space *aspace, |
| 43 | struct msm_gem_vma *vma, struct sg_table *sgt) |
| 44 | { |
| 45 | if (!vma->iova) |
| 46 | return; |
| 47 | |
| 48 | if (aspace->mmu) { |
| 49 | unsigned size = vma->node.size << PAGE_SHIFT; |
| 50 | aspace->mmu->funcs->unmap(aspace->mmu, vma->iova, sgt, size); |
| 51 | } |
| 52 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 53 | spin_lock(&aspace->lock); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 54 | drm_mm_remove_node(&vma->node); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 55 | spin_unlock(&aspace->lock); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 56 | |
| 57 | vma->iova = 0; |
Jordan Crouse | ee546cd | 2017-03-07 10:02:52 -0700 | [diff] [blame] | 58 | |
| 59 | msm_gem_address_space_put(aspace); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int |
| 63 | msm_gem_map_vma(struct msm_gem_address_space *aspace, |
| 64 | struct msm_gem_vma *vma, struct sg_table *sgt, int npages) |
| 65 | { |
| 66 | int ret; |
| 67 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 68 | spin_lock(&aspace->lock); |
| 69 | if (WARN_ON(drm_mm_node_allocated(&vma->node))) { |
| 70 | spin_unlock(&aspace->lock); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 71 | return 0; |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 72 | } |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 73 | |
Chris Wilson | 4e64e55 | 2017-02-02 21:04:38 +0000 | [diff] [blame] | 74 | ret = drm_mm_insert_node(&aspace->mm, &vma->node, npages); |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 75 | spin_unlock(&aspace->lock); |
| 76 | |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 77 | if (ret) |
| 78 | return ret; |
| 79 | |
| 80 | vma->iova = vma->node.start << PAGE_SHIFT; |
| 81 | |
| 82 | if (aspace->mmu) { |
| 83 | unsigned size = npages << PAGE_SHIFT; |
| 84 | ret = aspace->mmu->funcs->map(aspace->mmu, vma->iova, sgt, |
| 85 | size, IOMMU_READ | IOMMU_WRITE); |
| 86 | } |
| 87 | |
Jordan Crouse | ee546cd | 2017-03-07 10:02:52 -0700 | [diff] [blame] | 88 | /* Get a reference to the aspace to keep it around */ |
| 89 | kref_get(&aspace->kref); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 90 | |
Jordan Crouse | ee546cd | 2017-03-07 10:02:52 -0700 | [diff] [blame] | 91 | return ret; |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | struct msm_gem_address_space * |
| 95 | msm_gem_address_space_create(struct device *dev, struct iommu_domain *domain, |
| 96 | const char *name) |
| 97 | { |
| 98 | struct msm_gem_address_space *aspace; |
| 99 | |
| 100 | aspace = kzalloc(sizeof(*aspace), GFP_KERNEL); |
| 101 | if (!aspace) |
| 102 | return ERR_PTR(-ENOMEM); |
| 103 | |
Sushmita Susheelendra | 0e08270 | 2017-06-13 16:52:54 -0600 | [diff] [blame] | 104 | spin_lock_init(&aspace->lock); |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 105 | aspace->name = name; |
| 106 | aspace->mmu = msm_iommu_new(dev, domain); |
| 107 | |
| 108 | drm_mm_init(&aspace->mm, (domain->geometry.aperture_start >> PAGE_SHIFT), |
| 109 | (domain->geometry.aperture_end >> PAGE_SHIFT) - 1); |
| 110 | |
Jordan Crouse | ee546cd | 2017-03-07 10:02:52 -0700 | [diff] [blame] | 111 | kref_init(&aspace->kref); |
| 112 | |
Rob Clark | 667ce33 | 2016-09-28 19:58:32 -0400 | [diff] [blame] | 113 | return aspace; |
| 114 | } |