Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * psb GEM interface |
| 3 | * |
| 4 | * Copyright (c) 2011, Intel Corporation. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 13 | * more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License along with |
| 16 | * this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Authors: Alan Cox |
| 20 | * |
| 21 | * TODO: |
| 22 | * - we need to work out if the MMU is relevant (eg for |
| 23 | * accelerated operations on a GEM object) |
| 24 | */ |
| 25 | |
| 26 | #include <drm/drmP.h> |
| 27 | #include <drm/drm.h> |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 28 | #include <drm/gma_drm.h> |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 29 | #include <drm/drm_vma_manager.h> |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 30 | #include "psb_drv.h" |
| 31 | |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 32 | void psb_gem_free_object(struct drm_gem_object *obj) |
| 33 | { |
| 34 | struct gtt_range *gtt = container_of(obj, struct gtt_range, gem); |
Laurent Pinchart | 4d46259 | 2012-07-24 16:47:34 +0100 | [diff] [blame] | 35 | |
| 36 | /* Remove the list map if one is present */ |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 37 | drm_gem_free_mmap_offset(obj); |
Laurent Pinchart | 4d46259 | 2012-07-24 16:47:34 +0100 | [diff] [blame] | 38 | drm_gem_object_release(obj); |
| 39 | |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 40 | /* This must occur last as it frees up the memory of the GEM object */ |
| 41 | psb_gtt_free_range(obj->dev, gtt); |
| 42 | } |
| 43 | |
| 44 | int psb_gem_get_aperture(struct drm_device *dev, void *data, |
| 45 | struct drm_file *file) |
| 46 | { |
| 47 | return -EINVAL; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * psb_gem_dumb_map_gtt - buffer mapping for dumb interface |
| 52 | * @file: our drm client file |
| 53 | * @dev: drm device |
| 54 | * @handle: GEM handle to the object (from dumb_create) |
| 55 | * |
| 56 | * Do the necessary setup to allow the mapping of the frame buffer |
| 57 | * into user memory. We don't have to do much here at the moment. |
| 58 | */ |
| 59 | int psb_gem_dumb_map_gtt(struct drm_file *file, struct drm_device *dev, |
| 60 | uint32_t handle, uint64_t *offset) |
| 61 | { |
| 62 | int ret = 0; |
| 63 | struct drm_gem_object *obj; |
| 64 | |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 65 | /* GEM does all our handle to object mapping */ |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 66 | obj = drm_gem_object_lookup(file, handle); |
Daniel Vetter | 0e3089f | 2015-11-23 10:32:52 +0100 | [diff] [blame] | 67 | if (obj == NULL) |
| 68 | return -ENOENT; |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 69 | |
| 70 | /* Make it mmapable */ |
David Herrmann | 0de2397 | 2013-07-24 21:07:52 +0200 | [diff] [blame] | 71 | ret = drm_gem_create_mmap_offset(obj); |
| 72 | if (ret) |
| 73 | goto out; |
| 74 | *offset = drm_vma_node_offset_addr(&obj->vma_node); |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 75 | out: |
Daniel Vetter | 0e3089f | 2015-11-23 10:32:52 +0100 | [diff] [blame] | 76 | drm_gem_object_unreference_unlocked(obj); |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 77 | return ret; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * psb_gem_create - create a mappable object |
| 82 | * @file: the DRM file of the client |
| 83 | * @dev: our device |
| 84 | * @size: the size requested |
| 85 | * @handlep: returned handle (opaque number) |
| 86 | * |
| 87 | * Create a GEM object, fill in the boilerplate and attach a handle to |
| 88 | * it so that userspace can speak about it. This does the core work |
| 89 | * for the various methods that do/will create GEM objects for things |
| 90 | */ |
Patrik Jakobsson | c269c68 | 2014-01-06 02:39:10 +0100 | [diff] [blame] | 91 | int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size, |
| 92 | u32 *handlep, int stolen, u32 align) |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 93 | { |
| 94 | struct gtt_range *r; |
| 95 | int ret; |
| 96 | u32 handle; |
| 97 | |
| 98 | size = roundup(size, PAGE_SIZE); |
| 99 | |
| 100 | /* Allocate our object - for now a direct gtt range which is not |
| 101 | stolen memory backed */ |
Patrik Jakobsson | c269c68 | 2014-01-06 02:39:10 +0100 | [diff] [blame] | 102 | r = psb_gtt_alloc_range(dev, size, "gem", 0, PAGE_SIZE); |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 103 | if (r == NULL) { |
| 104 | dev_err(dev->dev, "no memory for %lld byte GEM object\n", size); |
| 105 | return -ENOSPC; |
| 106 | } |
| 107 | /* Initialize the extra goodies GEM needs to do all the hard work */ |
| 108 | if (drm_gem_object_init(dev, &r->gem, size) != 0) { |
| 109 | psb_gtt_free_range(dev, r); |
Alan Cox | a746092 | 2011-11-29 22:21:03 +0000 | [diff] [blame] | 110 | /* GEM doesn't give an error code so use -ENOMEM */ |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 111 | dev_err(dev->dev, "GEM init failed for %lld\n", size); |
| 112 | return -ENOMEM; |
| 113 | } |
Alan Cox | 398b470 | 2012-04-25 14:38:47 +0100 | [diff] [blame] | 114 | /* Limit the object to 32bit mappings */ |
| 115 | mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32); |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 116 | /* Give the object a handle so we can carry it more easily */ |
| 117 | ret = drm_gem_handle_create(file, &r->gem, &handle); |
| 118 | if (ret) { |
| 119 | dev_err(dev->dev, "GEM handle failed for %p, %lld\n", |
| 120 | &r->gem, size); |
| 121 | drm_gem_object_release(&r->gem); |
| 122 | psb_gtt_free_range(dev, r); |
| 123 | return ret; |
| 124 | } |
| 125 | /* We have the initial and handle reference but need only one now */ |
Daniel Vetter | d3e376f | 2015-11-23 10:32:49 +0100 | [diff] [blame] | 126 | drm_gem_object_unreference_unlocked(&r->gem); |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 127 | *handlep = handle; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * psb_gem_dumb_create - create a dumb buffer |
| 133 | * @drm_file: our client file |
| 134 | * @dev: our device |
| 135 | * @args: the requested arguments copied from userspace |
| 136 | * |
| 137 | * Allocate a buffer suitable for use for a frame buffer of the |
| 138 | * form described by user space. Give userspace a handle by which |
| 139 | * to reference it. |
| 140 | */ |
| 141 | int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev, |
| 142 | struct drm_mode_create_dumb *args) |
| 143 | { |
| 144 | args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64); |
| 145 | args->size = args->pitch * args->height; |
Patrik Jakobsson | c269c68 | 2014-01-06 02:39:10 +0100 | [diff] [blame] | 146 | return psb_gem_create(file, dev, args->size, &args->handle, 0, |
| 147 | PAGE_SIZE); |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 151 | * psb_gem_fault - pagefault handler for GEM objects |
| 152 | * @vma: the VMA of the GEM object |
| 153 | * @vmf: fault detail |
| 154 | * |
| 155 | * Invoked when a fault occurs on an mmap of a GEM managed area. GEM |
| 156 | * does most of the work for us including the actual map/unmap calls |
| 157 | * but we need to do the actual page work. |
| 158 | * |
| 159 | * This code eventually needs to handle faulting objects in and out |
| 160 | * of the GTT and repacking it when we run out of space. We can put |
| 161 | * that off for now and for our simple uses |
| 162 | * |
| 163 | * The VMA was set up by GEM. In doing so it also ensured that the |
| 164 | * vma->vm_private_data points to the GEM object that is backing this |
| 165 | * mapping. |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 166 | */ |
| 167 | int psb_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 168 | { |
| 169 | struct drm_gem_object *obj; |
| 170 | struct gtt_range *r; |
| 171 | int ret; |
| 172 | unsigned long pfn; |
| 173 | pgoff_t page_offset; |
| 174 | struct drm_device *dev; |
| 175 | struct drm_psb_private *dev_priv; |
| 176 | |
| 177 | obj = vma->vm_private_data; /* GEM object */ |
| 178 | dev = obj->dev; |
| 179 | dev_priv = dev->dev_private; |
| 180 | |
| 181 | r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */ |
| 182 | |
| 183 | /* Make sure we don't parallel update on a fault, nor move or remove |
| 184 | something from beneath our feet */ |
Daniel Vetter | 737292a | 2015-11-23 10:32:53 +0100 | [diff] [blame] | 185 | mutex_lock(&dev_priv->mmap_mutex); |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 186 | |
| 187 | /* For now the mmap pins the object and it stays pinned. As things |
| 188 | stand that will do us no harm */ |
| 189 | if (r->mmapping == 0) { |
| 190 | ret = psb_gtt_pin(r); |
| 191 | if (ret < 0) { |
| 192 | dev_err(dev->dev, "gma500: pin failed: %d\n", ret); |
| 193 | goto fail; |
| 194 | } |
| 195 | r->mmapping = 1; |
| 196 | } |
| 197 | |
| 198 | /* Page relative to the VMA start - we must calculate this ourselves |
| 199 | because vmf->pgoff is the fake GEM offset */ |
| 200 | page_offset = ((unsigned long) vmf->virtual_address - vma->vm_start) |
| 201 | >> PAGE_SHIFT; |
| 202 | |
| 203 | /* CPU view of the page, don't go via the GART for CPU writes */ |
| 204 | if (r->stolen) |
| 205 | pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT; |
| 206 | else |
| 207 | pfn = page_to_pfn(r->pages[page_offset]); |
| 208 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); |
| 209 | |
| 210 | fail: |
Daniel Vetter | 737292a | 2015-11-23 10:32:53 +0100 | [diff] [blame] | 211 | mutex_unlock(&dev_priv->mmap_mutex); |
Alan Cox | e32681d | 2011-11-03 18:20:58 +0000 | [diff] [blame] | 212 | switch (ret) { |
| 213 | case 0: |
| 214 | case -ERESTARTSYS: |
| 215 | case -EINTR: |
| 216 | return VM_FAULT_NOPAGE; |
| 217 | case -ENOMEM: |
| 218 | return VM_FAULT_OOM; |
| 219 | default: |
| 220 | return VM_FAULT_SIGBUS; |
| 221 | } |
| 222 | } |