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