xref: /linux/drivers/gpu/drm/gma500/gem.c (revision 16dad99de8f2abfcec9836e2031613a713e339d2)
1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2e32681d6SAlan Cox /*
3e32681d6SAlan Cox  *  psb GEM interface
4e32681d6SAlan Cox  *
5e32681d6SAlan Cox  * Copyright (c) 2011, Intel Corporation.
6e32681d6SAlan Cox  *
7e32681d6SAlan Cox  * Authors: Alan Cox
8e32681d6SAlan Cox  *
9e32681d6SAlan Cox  * TODO:
10e32681d6SAlan Cox  *	-	we need to work out if the MMU is relevant (eg for
11e32681d6SAlan Cox  *		accelerated operations on a GEM object)
12e32681d6SAlan Cox  */
13e32681d6SAlan Cox 
140c7b178aSSam Ravnborg #include <linux/pagemap.h>
150c7b178aSSam Ravnborg 
1633e079bcSThomas Zimmermann #include <asm/set_memory.h>
1733e079bcSThomas Zimmermann 
18e32681d6SAlan Cox #include <drm/drm.h>
190de23977SDavid Herrmann #include <drm/drm_vma_manager.h>
20d825c565SSam Ravnborg 
21ce5735b6SLee Jones #include "gem.h"
22e32681d6SAlan Cox #include "psb_drv.h"
23e32681d6SAlan Cox 
24f2d061edSThomas Zimmermann int psb_gem_pin(struct psb_gem_object *pobj)
251f9f6790SThomas Zimmermann {
26f2d061edSThomas Zimmermann 	struct drm_gem_object *obj = &pobj->base;
27f2d061edSThomas Zimmermann 	struct drm_device *dev = obj->dev;
281f9f6790SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
291f9f6790SThomas Zimmermann 	u32 gpu_base = dev_priv->gtt.gatt_start;
300b80214bSThomas Zimmermann 	struct page **pages;
310b80214bSThomas Zimmermann 	unsigned int npages;
32e1f80341SThomas Zimmermann 	int ret;
331f9f6790SThomas Zimmermann 
34*16dad99dSThomas Zimmermann 	ret = dma_resv_lock(obj->resv, NULL);
35*16dad99dSThomas Zimmermann 	if (drm_WARN_ONCE(dev, ret, "dma_resv_lock() failed, ret=%d\n", ret))
36*16dad99dSThomas Zimmermann 		return ret;
37*16dad99dSThomas Zimmermann 
381f9f6790SThomas Zimmermann 	mutex_lock(&dev_priv->gtt_mutex);
391f9f6790SThomas Zimmermann 
40f2d061edSThomas Zimmermann 	if (pobj->in_gart || pobj->stolen)
410b80214bSThomas Zimmermann 		goto out; /* already mapped */
420b80214bSThomas Zimmermann 
43f2d061edSThomas Zimmermann 	pages = drm_gem_get_pages(obj);
440b80214bSThomas Zimmermann 	if (IS_ERR(pages)) {
450b80214bSThomas Zimmermann 		ret = PTR_ERR(pages);
460b80214bSThomas Zimmermann 		goto err_mutex_unlock;
470b80214bSThomas Zimmermann 	}
480b80214bSThomas Zimmermann 
49f2d061edSThomas Zimmermann 	npages = obj->size / PAGE_SIZE;
500b80214bSThomas Zimmermann 
5133e079bcSThomas Zimmermann 	set_pages_array_wc(pages, npages);
5233e079bcSThomas Zimmermann 
53f2d061edSThomas Zimmermann 	psb_gtt_insert_pages(dev_priv, &pobj->resource, pages);
540b80214bSThomas Zimmermann 	psb_mmu_insert_pages(psb_mmu_get_default_pd(dev_priv->mmu), pages,
55f2d061edSThomas Zimmermann 			     (gpu_base + pobj->offset), npages, 0, 0,
560b80214bSThomas Zimmermann 			     PSB_MMU_CACHED_MEMORY);
570b80214bSThomas Zimmermann 
58f2d061edSThomas Zimmermann 	pobj->pages = pages;
590b80214bSThomas Zimmermann 
601f9f6790SThomas Zimmermann out:
61f2d061edSThomas Zimmermann 	++pobj->in_gart;
620b80214bSThomas Zimmermann 	mutex_unlock(&dev_priv->gtt_mutex);
63*16dad99dSThomas Zimmermann 	dma_resv_unlock(obj->resv);
640b80214bSThomas Zimmermann 
650b80214bSThomas Zimmermann 	return 0;
660b80214bSThomas Zimmermann 
670b80214bSThomas Zimmermann err_mutex_unlock:
681f9f6790SThomas Zimmermann 	mutex_unlock(&dev_priv->gtt_mutex);
69*16dad99dSThomas Zimmermann 	dma_resv_unlock(obj->resv);
701f9f6790SThomas Zimmermann 	return ret;
711f9f6790SThomas Zimmermann }
721f9f6790SThomas Zimmermann 
73f2d061edSThomas Zimmermann void psb_gem_unpin(struct psb_gem_object *pobj)
741f9f6790SThomas Zimmermann {
75f2d061edSThomas Zimmermann 	struct drm_gem_object *obj = &pobj->base;
76f2d061edSThomas Zimmermann 	struct drm_device *dev = obj->dev;
771f9f6790SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
781f9f6790SThomas Zimmermann 	u32 gpu_base = dev_priv->gtt.gatt_start;
79de2d1822SThomas Zimmermann 	unsigned long npages;
80*16dad99dSThomas Zimmermann 	int ret;
81*16dad99dSThomas Zimmermann 
82*16dad99dSThomas Zimmermann 	ret = dma_resv_lock(obj->resv, NULL);
83*16dad99dSThomas Zimmermann 	if (drm_WARN_ONCE(dev, ret, "dma_resv_lock() failed, ret=%d\n", ret))
84*16dad99dSThomas Zimmermann 		return;
851f9f6790SThomas Zimmermann 
861f9f6790SThomas Zimmermann 	mutex_lock(&dev_priv->gtt_mutex);
871f9f6790SThomas Zimmermann 
88f2d061edSThomas Zimmermann 	WARN_ON(!pobj->in_gart);
891f9f6790SThomas Zimmermann 
90f2d061edSThomas Zimmermann 	--pobj->in_gart;
910b80214bSThomas Zimmermann 
92f2d061edSThomas Zimmermann 	if (pobj->in_gart || pobj->stolen)
930b80214bSThomas Zimmermann 		goto out;
940b80214bSThomas Zimmermann 
95de2d1822SThomas Zimmermann 	npages = obj->size / PAGE_SIZE;
96de2d1822SThomas Zimmermann 
971f9f6790SThomas Zimmermann 	psb_mmu_remove_pages(psb_mmu_get_default_pd(dev_priv->mmu),
98de2d1822SThomas Zimmermann 			     (gpu_base + pobj->offset), npages, 0, 0);
99f2d061edSThomas Zimmermann 	psb_gtt_remove_pages(dev_priv, &pobj->resource);
1001f9f6790SThomas Zimmermann 
10133e079bcSThomas Zimmermann 	/* Reset caching flags */
102de2d1822SThomas Zimmermann 	set_pages_array_wb(pobj->pages, npages);
10333e079bcSThomas Zimmermann 
104f2d061edSThomas Zimmermann 	drm_gem_put_pages(obj, pobj->pages, true, false);
105f2d061edSThomas Zimmermann 	pobj->pages = NULL;
1060b80214bSThomas Zimmermann 
1070b80214bSThomas Zimmermann out:
1081f9f6790SThomas Zimmermann 	mutex_unlock(&dev_priv->gtt_mutex);
109*16dad99dSThomas Zimmermann 	dma_resv_unlock(obj->resv);
1101f9f6790SThomas Zimmermann }
1111f9f6790SThomas Zimmermann 
1127cd467d0SThomas Zimmermann static vm_fault_t psb_gem_fault(struct vm_fault *vmf);
1137cd467d0SThomas Zimmermann 
1147cd467d0SThomas Zimmermann static void psb_gem_free_object(struct drm_gem_object *obj)
115e32681d6SAlan Cox {
116f2d061edSThomas Zimmermann 	struct psb_gem_object *pobj = to_psb_gem_object(obj);
1174d46259fSLaurent Pinchart 
1184d46259fSLaurent Pinchart 	drm_gem_object_release(obj);
1194d46259fSLaurent Pinchart 
1203c101135SThomas Zimmermann 	/* Undo the mmap pin if we are destroying the object */
121f2d061edSThomas Zimmermann 	if (pobj->mmapping)
122f2d061edSThomas Zimmermann 		psb_gem_unpin(pobj);
1233c101135SThomas Zimmermann 
124f2d061edSThomas Zimmermann 	WARN_ON(pobj->in_gart && !pobj->stolen);
1253c101135SThomas Zimmermann 
126f2d061edSThomas Zimmermann 	release_resource(&pobj->resource);
127f2d061edSThomas Zimmermann 	kfree(pobj);
128e32681d6SAlan Cox }
129e32681d6SAlan Cox 
1307cd467d0SThomas Zimmermann static const struct vm_operations_struct psb_gem_vm_ops = {
1317cd467d0SThomas Zimmermann 	.fault = psb_gem_fault,
1327cd467d0SThomas Zimmermann 	.open = drm_gem_vm_open,
1337cd467d0SThomas Zimmermann 	.close = drm_gem_vm_close,
1347cd467d0SThomas Zimmermann };
1357cd467d0SThomas Zimmermann 
136957a2d0eSThomas Zimmermann static const struct drm_gem_object_funcs psb_gem_object_funcs = {
1377cd467d0SThomas Zimmermann 	.free = psb_gem_free_object,
1387cd467d0SThomas Zimmermann 	.vm_ops = &psb_gem_vm_ops,
1397cd467d0SThomas Zimmermann };
1407cd467d0SThomas Zimmermann 
141f2d061edSThomas Zimmermann struct psb_gem_object *
142576d4d2dSThomas Zimmermann psb_gem_create(struct drm_device *dev, u64 size, const char *name, bool stolen, u32 align)
143e32681d6SAlan Cox {
1443c101135SThomas Zimmermann 	struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
145f2d061edSThomas Zimmermann 	struct psb_gem_object *pobj;
146576d4d2dSThomas Zimmermann 	struct drm_gem_object *obj;
147e32681d6SAlan Cox 	int ret;
148e32681d6SAlan Cox 
149e32681d6SAlan Cox 	size = roundup(size, PAGE_SIZE);
150e32681d6SAlan Cox 
151f2d061edSThomas Zimmermann 	pobj = kzalloc(sizeof(*pobj), GFP_KERNEL);
152f2d061edSThomas Zimmermann 	if (!pobj)
1533c101135SThomas Zimmermann 		return ERR_PTR(-ENOMEM);
154f2d061edSThomas Zimmermann 	obj = &pobj->base;
155576d4d2dSThomas Zimmermann 
1563c101135SThomas Zimmermann 	/* GTT resource */
1573c101135SThomas Zimmermann 
158f2d061edSThomas Zimmermann 	ret = psb_gtt_allocate_resource(dev_priv, &pobj->resource, name, size, align, stolen,
159f2d061edSThomas Zimmermann 					&pobj->offset);
1603c101135SThomas Zimmermann 	if (ret)
1613c101135SThomas Zimmermann 		goto err_kfree;
1623c101135SThomas Zimmermann 
1633c101135SThomas Zimmermann 	if (stolen) {
164f2d061edSThomas Zimmermann 		pobj->stolen = true;
165f2d061edSThomas Zimmermann 		pobj->in_gart = 1;
1663c101135SThomas Zimmermann 	}
1673c101135SThomas Zimmermann 
1683c101135SThomas Zimmermann 	/* GEM object */
1693c101135SThomas Zimmermann 
170576d4d2dSThomas Zimmermann 	obj->funcs = &psb_gem_object_funcs;
171576d4d2dSThomas Zimmermann 
172957a2d0eSThomas Zimmermann 	if (stolen) {
173957a2d0eSThomas Zimmermann 		drm_gem_private_object_init(dev, obj, size);
174957a2d0eSThomas Zimmermann 	} else {
175576d4d2dSThomas Zimmermann 		ret = drm_gem_object_init(dev, obj, size);
176576d4d2dSThomas Zimmermann 		if (ret)
1773c101135SThomas Zimmermann 			goto err_release_resource;
178576d4d2dSThomas Zimmermann 
179576d4d2dSThomas Zimmermann 		/* Limit the object to 32-bit mappings */
180576d4d2dSThomas Zimmermann 		mapping_set_gfp_mask(obj->filp->f_mapping, GFP_KERNEL | __GFP_DMA32);
181957a2d0eSThomas Zimmermann 	}
182576d4d2dSThomas Zimmermann 
183f2d061edSThomas Zimmermann 	return pobj;
184576d4d2dSThomas Zimmermann 
1853c101135SThomas Zimmermann err_release_resource:
186f2d061edSThomas Zimmermann 	release_resource(&pobj->resource);
1873c101135SThomas Zimmermann err_kfree:
188f2d061edSThomas Zimmermann 	kfree(pobj);
189576d4d2dSThomas Zimmermann 	return ERR_PTR(ret);
190e32681d6SAlan Cox }
191e32681d6SAlan Cox 
192e32681d6SAlan Cox /**
193e32681d6SAlan Cox  *	psb_gem_dumb_create	-	create a dumb buffer
19492bd69c7SLee Jones  *	@file: our client file
195e32681d6SAlan Cox  *	@dev: our device
196e32681d6SAlan Cox  *	@args: the requested arguments copied from userspace
197e32681d6SAlan Cox  *
198e32681d6SAlan Cox  *	Allocate a buffer suitable for use for a frame buffer of the
199e32681d6SAlan Cox  *	form described by user space. Give userspace a handle by which
200e32681d6SAlan Cox  *	to reference it.
201e32681d6SAlan Cox  */
202e32681d6SAlan Cox int psb_gem_dumb_create(struct drm_file *file, struct drm_device *dev,
203e32681d6SAlan Cox 			struct drm_mode_create_dumb *args)
204e32681d6SAlan Cox {
205576d4d2dSThomas Zimmermann 	size_t pitch, size;
206f2d061edSThomas Zimmermann 	struct psb_gem_object *pobj;
207576d4d2dSThomas Zimmermann 	struct drm_gem_object *obj;
208576d4d2dSThomas Zimmermann 	u32 handle;
209576d4d2dSThomas Zimmermann 	int ret;
210576d4d2dSThomas Zimmermann 
211576d4d2dSThomas Zimmermann 	pitch = args->width * DIV_ROUND_UP(args->bpp, 8);
212576d4d2dSThomas Zimmermann 	pitch = ALIGN(pitch, 64);
213576d4d2dSThomas Zimmermann 
214576d4d2dSThomas Zimmermann 	size = pitch * args->height;
215576d4d2dSThomas Zimmermann 	size = roundup(size, PAGE_SIZE);
216576d4d2dSThomas Zimmermann 	if (!size)
217576d4d2dSThomas Zimmermann 		return -EINVAL;
218576d4d2dSThomas Zimmermann 
219f2d061edSThomas Zimmermann 	pobj = psb_gem_create(dev, size, "gem", false, PAGE_SIZE);
220f2d061edSThomas Zimmermann 	if (IS_ERR(pobj))
221f2d061edSThomas Zimmermann 		return PTR_ERR(pobj);
222f2d061edSThomas Zimmermann 	obj = &pobj->base;
223576d4d2dSThomas Zimmermann 
224576d4d2dSThomas Zimmermann 	ret = drm_gem_handle_create(file, obj, &handle);
225576d4d2dSThomas Zimmermann 	if (ret)
226576d4d2dSThomas Zimmermann 		goto err_drm_gem_object_put;
227576d4d2dSThomas Zimmermann 
228576d4d2dSThomas Zimmermann 	drm_gem_object_put(obj);
229576d4d2dSThomas Zimmermann 
230576d4d2dSThomas Zimmermann 	args->pitch = pitch;
231576d4d2dSThomas Zimmermann 	args->size = size;
232576d4d2dSThomas Zimmermann 	args->handle = handle;
233576d4d2dSThomas Zimmermann 
234576d4d2dSThomas Zimmermann 	return 0;
235576d4d2dSThomas Zimmermann 
236576d4d2dSThomas Zimmermann err_drm_gem_object_put:
237576d4d2dSThomas Zimmermann 	drm_gem_object_put(obj);
238576d4d2dSThomas Zimmermann 	return ret;
239e32681d6SAlan Cox }
240e32681d6SAlan Cox 
241e32681d6SAlan Cox /**
242e32681d6SAlan Cox  *	psb_gem_fault		-	pagefault handler for GEM objects
243e32681d6SAlan Cox  *	@vmf: fault detail
244e32681d6SAlan Cox  *
245e32681d6SAlan Cox  *	Invoked when a fault occurs on an mmap of a GEM managed area. GEM
246e32681d6SAlan Cox  *	does most of the work for us including the actual map/unmap calls
247e32681d6SAlan Cox  *	but we need to do the actual page work.
248e32681d6SAlan Cox  *
249e32681d6SAlan Cox  *	This code eventually needs to handle faulting objects in and out
250e32681d6SAlan Cox  *	of the GTT and repacking it when we run out of space. We can put
251e32681d6SAlan Cox  *	that off for now and for our simple uses
252e32681d6SAlan Cox  *
253e32681d6SAlan Cox  *	The VMA was set up by GEM. In doing so it also ensured that the
254e32681d6SAlan Cox  *	vma->vm_private_data points to the GEM object that is backing this
255e32681d6SAlan Cox  *	mapping.
256e32681d6SAlan Cox  */
2577cd467d0SThomas Zimmermann static vm_fault_t psb_gem_fault(struct vm_fault *vmf)
258e32681d6SAlan Cox {
25911bac800SDave Jiang 	struct vm_area_struct *vma = vmf->vma;
260e32681d6SAlan Cox 	struct drm_gem_object *obj;
261f2d061edSThomas Zimmermann 	struct psb_gem_object *pobj;
2620edf6813SSouptick Joarder 	int err;
2630edf6813SSouptick Joarder 	vm_fault_t ret;
264e32681d6SAlan Cox 	unsigned long pfn;
265e32681d6SAlan Cox 	pgoff_t page_offset;
266e32681d6SAlan Cox 	struct drm_device *dev;
267e32681d6SAlan Cox 	struct drm_psb_private *dev_priv;
268e32681d6SAlan Cox 
269e32681d6SAlan Cox 	obj = vma->vm_private_data;	/* GEM object */
270e32681d6SAlan Cox 	dev = obj->dev;
271f71635e8SThomas Zimmermann 	dev_priv = to_drm_psb_private(dev);
272e32681d6SAlan Cox 
273f2d061edSThomas Zimmermann 	pobj = to_psb_gem_object(obj);
274e32681d6SAlan Cox 
275e32681d6SAlan Cox 	/* Make sure we don't parallel update on a fault, nor move or remove
276e32681d6SAlan Cox 	   something from beneath our feet */
277737292a3SDaniel Vetter 	mutex_lock(&dev_priv->mmap_mutex);
278e32681d6SAlan Cox 
279e32681d6SAlan Cox 	/* For now the mmap pins the object and it stays pinned. As things
280e32681d6SAlan Cox 	   stand that will do us no harm */
281f2d061edSThomas Zimmermann 	if (pobj->mmapping == 0) {
282f2d061edSThomas Zimmermann 		err = psb_gem_pin(pobj);
2830edf6813SSouptick Joarder 		if (err < 0) {
2840edf6813SSouptick Joarder 			dev_err(dev->dev, "gma500: pin failed: %d\n", err);
2850edf6813SSouptick Joarder 			ret = vmf_error(err);
286e32681d6SAlan Cox 			goto fail;
287e32681d6SAlan Cox 		}
288f2d061edSThomas Zimmermann 		pobj->mmapping = 1;
289e32681d6SAlan Cox 	}
290e32681d6SAlan Cox 
291e32681d6SAlan Cox 	/* Page relative to the VMA start - we must calculate this ourselves
292e32681d6SAlan Cox 	   because vmf->pgoff is the fake GEM offset */
2931a29d85eSJan Kara 	page_offset = (vmf->address - vma->vm_start) >> PAGE_SHIFT;
294e32681d6SAlan Cox 
295e32681d6SAlan Cox 	/* CPU view of the page, don't go via the GART for CPU writes */
296f2d061edSThomas Zimmermann 	if (pobj->stolen)
297f2d061edSThomas Zimmermann 		pfn = (dev_priv->stolen_base + pobj->offset) >> PAGE_SHIFT;
298e32681d6SAlan Cox 	else
299f2d061edSThomas Zimmermann 		pfn = page_to_pfn(pobj->pages[page_offset]);
3000edf6813SSouptick Joarder 	ret = vmf_insert_pfn(vma, vmf->address, pfn);
301e32681d6SAlan Cox fail:
302737292a3SDaniel Vetter 	mutex_unlock(&dev_priv->mmap_mutex);
3030edf6813SSouptick Joarder 
3040edf6813SSouptick Joarder 	return ret;
305e32681d6SAlan Cox }
306