Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / gma500 / gem.c
blobdb827e591403c7db640b6ce3b6851ccb8b345155
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * psb GEM interface
5 * Copyright (c) 2011, Intel Corporation.
7 * Authors: Alan Cox
9 * TODO:
10 * - we need to work out if the MMU is relevant (eg for
11 * accelerated operations on a GEM object)
14 #include <linux/pagemap.h>
16 #include <drm/drm.h>
17 #include <drm/drm_vma_manager.h>
19 #include "psb_drv.h"
21 static vm_fault_t psb_gem_fault(struct vm_fault *vmf);
23 static void psb_gem_free_object(struct drm_gem_object *obj)
25 struct gtt_range *gtt = container_of(obj, struct gtt_range, gem);
27 /* Remove the list map if one is present */
28 drm_gem_free_mmap_offset(obj);
29 drm_gem_object_release(obj);
31 /* This must occur last as it frees up the memory of the GEM object */
32 psb_gtt_free_range(obj->dev, gtt);
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,
41 const struct drm_gem_object_funcs psb_gem_object_funcs = {
42 .free = psb_gem_free_object,
43 .vm_ops = &psb_gem_vm_ops,
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)
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
57 int psb_gem_create(struct drm_file *file, struct drm_device *dev, u64 size,
58 u32 *handlep, int stolen, u32 align)
60 struct gtt_range *r;
61 int ret;
62 u32 handle;
64 size = roundup(size, PAGE_SIZE);
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;
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;
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;
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;
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
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.
108 int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
109 struct drm_mode_create_dumb *args)
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);
118 * psb_gem_fault - pagefault handler for GEM objects
119 * @vma: the VMA of the GEM object
120 * @vmf: fault detail
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.
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
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.
134 static vm_fault_t psb_gem_fault(struct vm_fault *vmf)
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;
146 obj = vma->vm_private_data; /* GEM object */
147 dev = obj->dev;
148 dev_priv = dev->dev_private;
150 r = container_of(obj, struct gtt_range, gem); /* Get the gtt range */
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);
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;
165 r->mmapping = 1;
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;
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);
181 return ret;