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