1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright (c) 2011, Intel Corporation.
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>
17 #include <drm/drm_vma_manager.h>
21 void psb_gem_free_object(struct drm_gem_object
*obj
)
23 struct gtt_range
*gtt
= container_of(obj
, struct gtt_range
, gem
);
25 /* Remove the list map if one is present */
26 drm_gem_free_mmap_offset(obj
);
27 drm_gem_object_release(obj
);
29 /* This must occur last as it frees up the memory of the GEM object */
30 psb_gtt_free_range(obj
->dev
, gtt
);
33 int psb_gem_get_aperture(struct drm_device
*dev
, void *data
,
34 struct drm_file
*file
)
40 * psb_gem_create - create a mappable object
41 * @file: the DRM file of the client
43 * @size: the size requested
44 * @handlep: returned handle (opaque number)
46 * Create a GEM object, fill in the boilerplate and attach a handle to
47 * it so that userspace can speak about it. This does the core work
48 * for the various methods that do/will create GEM objects for things
50 int psb_gem_create(struct drm_file
*file
, struct drm_device
*dev
, u64 size
,
51 u32
*handlep
, int stolen
, u32 align
)
57 size
= roundup(size
, PAGE_SIZE
);
59 /* Allocate our object - for now a direct gtt range which is not
60 stolen memory backed */
61 r
= psb_gtt_alloc_range(dev
, size
, "gem", 0, PAGE_SIZE
);
63 dev_err(dev
->dev
, "no memory for %lld byte GEM object\n", size
);
66 /* Initialize the extra goodies GEM needs to do all the hard work */
67 if (drm_gem_object_init(dev
, &r
->gem
, size
) != 0) {
68 psb_gtt_free_range(dev
, r
);
69 /* GEM doesn't give an error code so use -ENOMEM */
70 dev_err(dev
->dev
, "GEM init failed for %lld\n", size
);
73 /* Limit the object to 32bit mappings */
74 mapping_set_gfp_mask(r
->gem
.filp
->f_mapping
, GFP_KERNEL
| __GFP_DMA32
);
75 /* Give the object a handle so we can carry it more easily */
76 ret
= drm_gem_handle_create(file
, &r
->gem
, &handle
);
78 dev_err(dev
->dev
, "GEM handle failed for %p, %lld\n",
80 drm_gem_object_release(&r
->gem
);
81 psb_gtt_free_range(dev
, r
);
84 /* We have the initial and handle reference but need only one now */
85 drm_gem_object_put_unlocked(&r
->gem
);
91 * psb_gem_dumb_create - create a dumb buffer
92 * @drm_file: our client file
94 * @args: the requested arguments copied from userspace
96 * Allocate a buffer suitable for use for a frame buffer of the
97 * form described by user space. Give userspace a handle by which
100 int psb_gem_dumb_create(struct drm_file
*file
, struct drm_device
*dev
,
101 struct drm_mode_create_dumb
*args
)
103 args
->pitch
= ALIGN(args
->width
* ((args
->bpp
+ 7) / 8), 64);
104 args
->size
= args
->pitch
* args
->height
;
105 return psb_gem_create(file
, dev
, args
->size
, &args
->handle
, 0,
110 * psb_gem_fault - pagefault handler for GEM objects
111 * @vma: the VMA of the GEM object
114 * Invoked when a fault occurs on an mmap of a GEM managed area. GEM
115 * does most of the work for us including the actual map/unmap calls
116 * but we need to do the actual page work.
118 * This code eventually needs to handle faulting objects in and out
119 * of the GTT and repacking it when we run out of space. We can put
120 * that off for now and for our simple uses
122 * The VMA was set up by GEM. In doing so it also ensured that the
123 * vma->vm_private_data points to the GEM object that is backing this
126 vm_fault_t
psb_gem_fault(struct vm_fault
*vmf
)
128 struct vm_area_struct
*vma
= vmf
->vma
;
129 struct drm_gem_object
*obj
;
135 struct drm_device
*dev
;
136 struct drm_psb_private
*dev_priv
;
138 obj
= vma
->vm_private_data
; /* GEM object */
140 dev_priv
= dev
->dev_private
;
142 r
= container_of(obj
, struct gtt_range
, gem
); /* Get the gtt range */
144 /* Make sure we don't parallel update on a fault, nor move or remove
145 something from beneath our feet */
146 mutex_lock(&dev_priv
->mmap_mutex
);
148 /* For now the mmap pins the object and it stays pinned. As things
149 stand that will do us no harm */
150 if (r
->mmapping
== 0) {
151 err
= psb_gtt_pin(r
);
153 dev_err(dev
->dev
, "gma500: pin failed: %d\n", err
);
154 ret
= vmf_error(err
);
160 /* Page relative to the VMA start - we must calculate this ourselves
161 because vmf->pgoff is the fake GEM offset */
162 page_offset
= (vmf
->address
- vma
->vm_start
) >> PAGE_SHIFT
;
164 /* CPU view of the page, don't go via the GART for CPU writes */
166 pfn
= (dev_priv
->stolen_base
+ r
->offset
) >> PAGE_SHIFT
;
168 pfn
= page_to_pfn(r
->pages
[page_offset
]);
169 ret
= vmf_insert_pfn(vma
, vmf
->address
, pfn
);
171 mutex_unlock(&dev_priv
->mmap_mutex
);