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 <linux/pagemap.h> 27 28 #include <drm/drm.h> 29 #include <drm/drm_vma_manager.h> 30 31 #include "psb_drv.h" 32 33 void psb_gem_free_object(struct drm_gem_object *obj) 34 { 35 struct gtt_range *gtt = container_of(obj, struct gtt_range, gem); 36 37 /* Remove the list map if one is present */ 38 drm_gem_free_mmap_offset(obj); 39 drm_gem_object_release(obj); 40 41 /* This must occur last as it frees up the memory of the GEM object */ 42 psb_gtt_free_range(obj->dev, gtt); 43 } 44 45 int psb_gem_get_aperture(struct drm_device *dev, void *data, 46 struct drm_file *file) 47 { 48 return -EINVAL; 49 } 50 51 /** 52 * psb_gem_create - create a mappable object 53 * @file: the DRM file of the client 54 * @dev: our device 55 * @size: the size requested 56 * @handlep: returned handle (opaque number) 57 * 58 * Create a GEM object, fill in the boilerplate and attach a handle to 59 * it so that userspace can speak about it. This does the core work 60 * for the various methods that do/will create GEM objects for things 61 */ 62 int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size, 63 u32 *handlep, int stolen, u32 align) 64 { 65 struct gtt_range *r; 66 int ret; 67 u32 handle; 68 69 size = roundup(size, PAGE_SIZE); 70 71 /* Allocate our object - for now a direct gtt range which is not 72 stolen memory backed */ 73 r = psb_gtt_alloc_range(dev, size, "gem", 0, PAGE_SIZE); 74 if (r == NULL) { 75 dev_err(dev->dev, "no memory for %lld byte GEM object\n", size); 76 return -ENOSPC; 77 } 78 /* Initialize the extra goodies GEM needs to do all the hard work */ 79 if (drm_gem_object_init(dev, &r->gem, size) != 0) { 80 psb_gtt_free_range(dev, r); 81 /* GEM doesn't give an error code so use -ENOMEM */ 82 dev_err(dev->dev, "GEM init failed for %lld\n", size); 83 return -ENOMEM; 84 } 85 /* Limit the object to 32bit mappings */ 86 mapping_set_gfp_mask(r->gem.filp->f_mapping, GFP_KERNEL | __GFP_DMA32); 87 /* Give the object a handle so we can carry it more easily */ 88 ret = drm_gem_handle_create(file, &r->gem, &handle); 89 if (ret) { 90 dev_err(dev->dev, "GEM handle failed for %p, %lld\n", 91 &r->gem, size); 92 drm_gem_object_release(&r->gem); 93 psb_gtt_free_range(dev, r); 94 return ret; 95 } 96 /* We have the initial and handle reference but need only one now */ 97 drm_gem_object_put_unlocked(&r->gem); 98 *handlep = handle; 99 return 0; 100 } 101 102 /** 103 * psb_gem_dumb_create - create a dumb buffer 104 * @drm_file: our client file 105 * @dev: our device 106 * @args: the requested arguments copied from userspace 107 * 108 * Allocate a buffer suitable for use for a frame buffer of the 109 * form described by user space. Give userspace a handle by which 110 * to reference it. 111 */ 112 int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev, 113 struct drm_mode_create_dumb *args) 114 { 115 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64); 116 args->size = args->pitch * args->height; 117 return psb_gem_create(file, dev, args->size, &args->handle, 0, 118 PAGE_SIZE); 119 } 120 121 /** 122 * psb_gem_fault - pagefault handler for GEM objects 123 * @vma: the VMA of the GEM object 124 * @vmf: fault detail 125 * 126 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM 127 * does most of the work for us including the actual map/unmap calls 128 * but we need to do the actual page work. 129 * 130 * This code eventually needs to handle faulting objects in and out 131 * of the GTT and repacking it when we run out of space. We can put 132 * that off for now and for our simple uses 133 * 134 * The VMA was set up by GEM. In doing so it also ensured that the 135 * vma->vm_private_data points to the GEM object that is backing this 136 * mapping. 137 */ 138 vm_fault_t psb_gem_fault(struct vm_fault *vmf) 139 { 140 struct vm_area_struct *vma = vmf->vma; 141 struct drm_gem_object *obj; 142 struct gtt_range *r; 143 int err; 144 vm_fault_t ret; 145 unsigned long pfn; 146 pgoff_t page_offset; 147 struct drm_device *dev; 148 struct drm_psb_private *dev_priv; 149 150 obj = vma->vm_private_data; /* GEM object */ 151 dev = obj->dev; 152 dev_priv = dev->dev_private; 153 154 r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */ 155 156 /* Make sure we don't parallel update on a fault, nor move or remove 157 something from beneath our feet */ 158 mutex_lock(&dev_priv->mmap_mutex); 159 160 /* For now the mmap pins the object and it stays pinned. As things 161 stand that will do us no harm */ 162 if (r->mmapping == 0) { 163 err = psb_gtt_pin(r); 164 if (err < 0) { 165 dev_err(dev->dev, "gma500: pin failed: %d\n", err); 166 ret = vmf_error(err); 167 goto fail; 168 } 169 r->mmapping = 1; 170 } 171 172 /* Page relative to the VMA start - we must calculate this ourselves 173 because vmf->pgoff is the fake GEM offset */ 174 page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT; 175 176 /* CPU view of the page, don't go via the GART for CPU writes */ 177 if (r->stolen) 178 pfn = (dev_priv->stolen_base + r->offset) >> PAGE_SHIFT; 179 else 180 pfn = page_to_pfn(r->pages[page_offset]); 181 ret = vmf_insert_pfn(vma, vmf->address, pfn); 182 fail: 183 mutex_unlock(&dev_priv->mmap_mutex); 184 185 return ret; 186 } 187