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