xref: /openbmc/linux/drivers/gpu/drm/gma500/gtt.c (revision 4ce94eab)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2007, Intel Corporation.
4  * All Rights Reserved.
5  *
6  * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com>
7  *	    Alan Cox <alan@linux.intel.com>
8  */
9 
10 #include <linux/shmem_fs.h>
11 
12 #include <asm/set_memory.h>
13 
14 #include "blitter.h"
15 #include "psb_drv.h"
16 
17 
18 /*
19  *	GTT resource allocator - manage page mappings in GTT space
20  */
21 
22 /**
23  *	psb_gtt_mask_pte	-	generate GTT pte entry
24  *	@pfn: page number to encode
25  *	@type: type of memory in the GTT
26  *
27  *	Set the GTT entry for the appropriate memory type.
28  */
29 static inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type)
30 {
31 	uint32_t mask = PSB_PTE_VALID;
32 
33 	/* Ensure we explode rather than put an invalid low mapping of
34 	   a high mapping page into the gtt */
35 	BUG_ON(pfn & ~(0xFFFFFFFF >> PAGE_SHIFT));
36 
37 	if (type & PSB_MMU_CACHED_MEMORY)
38 		mask |= PSB_PTE_CACHED;
39 	if (type & PSB_MMU_RO_MEMORY)
40 		mask |= PSB_PTE_RO;
41 	if (type & PSB_MMU_WO_MEMORY)
42 		mask |= PSB_PTE_WO;
43 
44 	return (pfn << PAGE_SHIFT) | mask;
45 }
46 
47 /**
48  *	psb_gtt_entry		-	find the GTT entries for a gtt_range
49  *	@dev: our DRM device
50  *	@r: our GTT range
51  *
52  *	Given a gtt_range object return the GTT offset of the page table
53  *	entries for this gtt_range
54  */
55 static u32 __iomem *psb_gtt_entry(struct drm_device *dev, struct gtt_range *r)
56 {
57 	struct drm_psb_private *dev_priv = dev->dev_private;
58 	unsigned long offset;
59 
60 	offset = r->resource.start - dev_priv->gtt_mem->start;
61 
62 	return dev_priv->gtt_map + (offset >> PAGE_SHIFT);
63 }
64 
65 /**
66  *	psb_gtt_insert	-	put an object into the GTT
67  *	@dev: our DRM device
68  *	@r: our GTT range
69  *	@resume: on resume
70  *
71  *	Take our preallocated GTT range and insert the GEM object into
72  *	the GTT. This is protected via the gtt mutex which the caller
73  *	must hold.
74  */
75 static int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r,
76 			  int resume)
77 {
78 	u32 __iomem *gtt_slot;
79 	u32 pte;
80 	struct page **pages;
81 	int i;
82 
83 	if (r->pages == NULL) {
84 		WARN_ON(1);
85 		return -EINVAL;
86 	}
87 
88 	WARN_ON(r->stolen);	/* refcount these maybe ? */
89 
90 	gtt_slot = psb_gtt_entry(dev, r);
91 	pages = r->pages;
92 
93 	if (!resume) {
94 		/* Make sure changes are visible to the GPU */
95 		set_pages_array_wc(pages, r->npage);
96 	}
97 
98 	/* Write our page entries into the GTT itself */
99 	for (i = 0; i < r->npage; i++) {
100 		pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]),
101 				       PSB_MMU_CACHED_MEMORY);
102 		iowrite32(pte, gtt_slot++);
103 	}
104 
105 	/* Make sure all the entries are set before we return */
106 	ioread32(gtt_slot - 1);
107 
108 	return 0;
109 }
110 
111 /**
112  *	psb_gtt_remove	-	remove an object from the GTT
113  *	@dev: our DRM device
114  *	@r: our GTT range
115  *
116  *	Remove a preallocated GTT range from the GTT. Overwrite all the
117  *	page table entries with the dummy page. This is protected via the gtt
118  *	mutex which the caller must hold.
119  */
120 static void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r)
121 {
122 	struct drm_psb_private *dev_priv = dev->dev_private;
123 	u32 __iomem *gtt_slot;
124 	u32 pte;
125 	int i;
126 
127 	WARN_ON(r->stolen);
128 
129 	gtt_slot = psb_gtt_entry(dev, r);
130 	pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page),
131 			       PSB_MMU_CACHED_MEMORY);
132 
133 	for (i = 0; i < r->npage; i++)
134 		iowrite32(pte, gtt_slot++);
135 	ioread32(gtt_slot - 1);
136 	set_pages_array_wb(r->pages, r->npage);
137 }
138 
139 /**
140  *	psb_gtt_attach_pages	-	attach and pin GEM pages
141  *	@gt: the gtt range
142  *
143  *	Pin and build an in kernel list of the pages that back our GEM object.
144  *	While we hold this the pages cannot be swapped out. This is protected
145  *	via the gtt mutex which the caller must hold.
146  */
147 static int psb_gtt_attach_pages(struct gtt_range *gt)
148 {
149 	struct page **pages;
150 
151 	WARN_ON(gt->pages);
152 
153 	pages = drm_gem_get_pages(&gt->gem);
154 	if (IS_ERR(pages))
155 		return PTR_ERR(pages);
156 
157 	gt->npage = gt->gem.size / PAGE_SIZE;
158 	gt->pages = pages;
159 
160 	return 0;
161 }
162 
163 /**
164  *	psb_gtt_detach_pages	-	attach and pin GEM pages
165  *	@gt: the gtt range
166  *
167  *	Undo the effect of psb_gtt_attach_pages. At this point the pages
168  *	must have been removed from the GTT as they could now be paged out
169  *	and move bus address. This is protected via the gtt mutex which the
170  *	caller must hold.
171  */
172 static void psb_gtt_detach_pages(struct gtt_range *gt)
173 {
174 	drm_gem_put_pages(&gt->gem, gt->pages, true, false);
175 	gt->pages = NULL;
176 }
177 
178 /**
179  *	psb_gtt_pin		-	pin pages into the GTT
180  *	@gt: range to pin
181  *
182  *	Pin a set of pages into the GTT. The pins are refcounted so that
183  *	multiple pins need multiple unpins to undo.
184  *
185  *	Non GEM backed objects treat this as a no-op as they are always GTT
186  *	backed objects.
187  */
188 int psb_gtt_pin(struct gtt_range *gt)
189 {
190 	int ret = 0;
191 	struct drm_device *dev = gt->gem.dev;
192 	struct drm_psb_private *dev_priv = dev->dev_private;
193 	u32 gpu_base = dev_priv->gtt.gatt_start;
194 
195 	mutex_lock(&dev_priv->gtt_mutex);
196 
197 	if (gt->in_gart == 0 && gt->stolen == 0) {
198 		ret = psb_gtt_attach_pages(gt);
199 		if (ret < 0)
200 			goto out;
201 		ret = psb_gtt_insert(dev, gt, 0);
202 		if (ret < 0) {
203 			psb_gtt_detach_pages(gt);
204 			goto out;
205 		}
206 		psb_mmu_insert_pages(psb_mmu_get_default_pd(dev_priv->mmu),
207 				     gt->pages, (gpu_base + gt->offset),
208 				     gt->npage, 0, 0, PSB_MMU_CACHED_MEMORY);
209 	}
210 	gt->in_gart++;
211 out:
212 	mutex_unlock(&dev_priv->gtt_mutex);
213 	return ret;
214 }
215 
216 /**
217  *	psb_gtt_unpin		-	Drop a GTT pin requirement
218  *	@gt: range to pin
219  *
220  *	Undoes the effect of psb_gtt_pin. On the last drop the GEM object
221  *	will be removed from the GTT which will also drop the page references
222  *	and allow the VM to clean up or page stuff.
223  *
224  *	Non GEM backed objects treat this as a no-op as they are always GTT
225  *	backed objects.
226  */
227 void psb_gtt_unpin(struct gtt_range *gt)
228 {
229 	struct drm_device *dev = gt->gem.dev;
230 	struct drm_psb_private *dev_priv = dev->dev_private;
231 	u32 gpu_base = dev_priv->gtt.gatt_start;
232 	int ret;
233 
234 	/* While holding the gtt_mutex no new blits can be initiated */
235 	mutex_lock(&dev_priv->gtt_mutex);
236 
237 	/* Wait for any possible usage of the memory to be finished */
238 	ret = gma_blt_wait_idle(dev_priv);
239 	if (ret) {
240 		DRM_ERROR("Failed to idle the blitter, unpin failed!");
241 		goto out;
242 	}
243 
244 	WARN_ON(!gt->in_gart);
245 
246 	gt->in_gart--;
247 	if (gt->in_gart == 0 && gt->stolen == 0) {
248 		psb_mmu_remove_pages(psb_mmu_get_default_pd(dev_priv->mmu),
249 				     (gpu_base + gt->offset), gt->npage, 0, 0);
250 		psb_gtt_remove(dev, gt);
251 		psb_gtt_detach_pages(gt);
252 	}
253 
254 out:
255 	mutex_unlock(&dev_priv->gtt_mutex);
256 }
257 
258 /*
259  *	GTT resource allocator - allocate and manage GTT address space
260  */
261 
262 /**
263  *	psb_gtt_alloc_range	-	allocate GTT address space
264  *	@dev: Our DRM device
265  *	@len: length (bytes) of address space required
266  *	@name: resource name
267  *	@backed: resource should be backed by stolen pages
268  *	@align: requested alignment
269  *
270  *	Ask the kernel core to find us a suitable range of addresses
271  *	to use for a GTT mapping.
272  *
273  *	Returns a gtt_range structure describing the object, or NULL on
274  *	error. On successful return the resource is both allocated and marked
275  *	as in use.
276  */
277 struct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len,
278 				      const char *name, int backed, u32 align)
279 {
280 	struct drm_psb_private *dev_priv = dev->dev_private;
281 	struct gtt_range *gt;
282 	struct resource *r = dev_priv->gtt_mem;
283 	int ret;
284 	unsigned long start, end;
285 
286 	if (backed) {
287 		/* The start of the GTT is the stolen pages */
288 		start = r->start;
289 		end = r->start + dev_priv->gtt.stolen_size - 1;
290 	} else {
291 		/* The rest we will use for GEM backed objects */
292 		start = r->start + dev_priv->gtt.stolen_size;
293 		end = r->end;
294 	}
295 
296 	gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL);
297 	if (gt == NULL)
298 		return NULL;
299 	gt->resource.name = name;
300 	gt->stolen = backed;
301 	gt->in_gart = backed;
302 	/* Ensure this is set for non GEM objects */
303 	gt->gem.dev = dev;
304 	ret = allocate_resource(dev_priv->gtt_mem, &gt->resource,
305 				len, start, end, align, NULL, NULL);
306 	if (ret == 0) {
307 		gt->offset = gt->resource.start - r->start;
308 		return gt;
309 	}
310 	kfree(gt);
311 	return NULL;
312 }
313 
314 /**
315  *	psb_gtt_free_range	-	release GTT address space
316  *	@dev: our DRM device
317  *	@gt: a mapping created with psb_gtt_alloc_range
318  *
319  *	Release a resource that was allocated with psb_gtt_alloc_range. If the
320  *	object has been pinned by mmap users we clean this up here currently.
321  */
322 void psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt)
323 {
324 	/* Undo the mmap pin if we are destroying the object */
325 	if (gt->mmapping) {
326 		psb_gtt_unpin(gt);
327 		gt->mmapping = 0;
328 	}
329 	WARN_ON(gt->in_gart && !gt->stolen);
330 	release_resource(&gt->resource);
331 	kfree(gt);
332 }
333 
334 static void psb_gtt_alloc(struct drm_device *dev)
335 {
336 	struct drm_psb_private *dev_priv = dev->dev_private;
337 	init_rwsem(&dev_priv->gtt.sem);
338 }
339 
340 void psb_gtt_takedown(struct drm_device *dev)
341 {
342 	struct drm_psb_private *dev_priv = dev->dev_private;
343 	struct pci_dev *pdev = to_pci_dev(dev->dev);
344 
345 	if (dev_priv->gtt_map) {
346 		iounmap(dev_priv->gtt_map);
347 		dev_priv->gtt_map = NULL;
348 	}
349 	if (dev_priv->gtt_initialized) {
350 		pci_write_config_word(pdev, PSB_GMCH_CTRL,
351 				      dev_priv->gmch_ctrl);
352 		PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL);
353 		(void) PSB_RVDC32(PSB_PGETBL_CTL);
354 	}
355 	if (dev_priv->vram_addr)
356 		iounmap(dev_priv->gtt_map);
357 }
358 
359 int psb_gtt_init(struct drm_device *dev, int resume)
360 {
361 	struct drm_psb_private *dev_priv = dev->dev_private;
362 	struct pci_dev *pdev = to_pci_dev(dev->dev);
363 	unsigned gtt_pages;
364 	unsigned long stolen_size, vram_stolen_size;
365 	unsigned i, num_pages;
366 	unsigned pfn_base;
367 	struct psb_gtt *pg;
368 
369 	int ret = 0;
370 	uint32_t pte;
371 
372 	if (!resume) {
373 		mutex_init(&dev_priv->gtt_mutex);
374 		mutex_init(&dev_priv->mmap_mutex);
375 		psb_gtt_alloc(dev);
376 	}
377 
378 	pg = &dev_priv->gtt;
379 
380 	/* Enable the GTT */
381 	pci_read_config_word(pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl);
382 	pci_write_config_word(pdev, PSB_GMCH_CTRL,
383 			      dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
384 
385 	dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL);
386 	PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
387 	(void) PSB_RVDC32(PSB_PGETBL_CTL);
388 
389 	/* The root resource we allocate address space from */
390 	dev_priv->gtt_initialized = 1;
391 
392 	pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK;
393 
394 	/*
395 	 *	The video mmu has a hw bug when accessing 0x0D0000000.
396 	 *	Make gatt start at 0x0e000,0000. This doesn't actually
397 	 *	matter for us but may do if the video acceleration ever
398 	 *	gets opened up.
399 	 */
400 	pg->mmu_gatt_start = 0xE0000000;
401 
402 	pg->gtt_start = pci_resource_start(pdev, PSB_GTT_RESOURCE);
403 	gtt_pages = pci_resource_len(pdev, PSB_GTT_RESOURCE)
404 								>> PAGE_SHIFT;
405 	/* CDV doesn't report this. In which case the system has 64 gtt pages */
406 	if (pg->gtt_start == 0 || gtt_pages == 0) {
407 		dev_dbg(dev->dev, "GTT PCI BAR not initialized.\n");
408 		gtt_pages = 64;
409 		pg->gtt_start = dev_priv->pge_ctl;
410 	}
411 
412 	pg->gatt_start = pci_resource_start(pdev, PSB_GATT_RESOURCE);
413 	pg->gatt_pages = pci_resource_len(pdev, PSB_GATT_RESOURCE)
414 								>> PAGE_SHIFT;
415 	dev_priv->gtt_mem = &pdev->resource[PSB_GATT_RESOURCE];
416 
417 	if (pg->gatt_pages == 0 || pg->gatt_start == 0) {
418 		static struct resource fudge;	/* Preferably peppermint */
419 		/* This can occur on CDV systems. Fudge it in this case.
420 		   We really don't care what imaginary space is being allocated
421 		   at this point */
422 		dev_dbg(dev->dev, "GATT PCI BAR not initialized.\n");
423 		pg->gatt_start = 0x40000000;
424 		pg->gatt_pages = (128 * 1024 * 1024) >> PAGE_SHIFT;
425 		/* This is a little confusing but in fact the GTT is providing
426 		   a view from the GPU into memory and not vice versa. As such
427 		   this is really allocating space that is not the same as the
428 		   CPU address space on CDV */
429 		fudge.start = 0x40000000;
430 		fudge.end = 0x40000000 + 128 * 1024 * 1024 - 1;
431 		fudge.name = "fudge";
432 		fudge.flags = IORESOURCE_MEM;
433 		dev_priv->gtt_mem = &fudge;
434 	}
435 
436 	pci_read_config_dword(pdev, PSB_BSM, &dev_priv->stolen_base);
437 	vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base
438 								- PAGE_SIZE;
439 
440 	stolen_size = vram_stolen_size;
441 
442 	dev_dbg(dev->dev, "Stolen memory base 0x%x, size %luK\n",
443 			dev_priv->stolen_base, vram_stolen_size / 1024);
444 
445 	if (resume && (gtt_pages != pg->gtt_pages) &&
446 	    (stolen_size != pg->stolen_size)) {
447 		dev_err(dev->dev, "GTT resume error.\n");
448 		ret = -EINVAL;
449 		goto out_err;
450 	}
451 
452 	pg->gtt_pages = gtt_pages;
453 	pg->stolen_size = stolen_size;
454 	dev_priv->vram_stolen_size = vram_stolen_size;
455 
456 	/*
457 	 *	Map the GTT and the stolen memory area
458 	 */
459 	if (!resume)
460 		dev_priv->gtt_map = ioremap(pg->gtt_phys_start,
461 						gtt_pages << PAGE_SHIFT);
462 	if (!dev_priv->gtt_map) {
463 		dev_err(dev->dev, "Failure to map gtt.\n");
464 		ret = -ENOMEM;
465 		goto out_err;
466 	}
467 
468 	if (!resume)
469 		dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base,
470 						 stolen_size);
471 
472 	if (!dev_priv->vram_addr) {
473 		dev_err(dev->dev, "Failure to map stolen base.\n");
474 		ret = -ENOMEM;
475 		goto out_err;
476 	}
477 
478 	/*
479 	 * Insert vram stolen pages into the GTT
480 	 */
481 
482 	pfn_base = dev_priv->stolen_base >> PAGE_SHIFT;
483 	num_pages = vram_stolen_size >> PAGE_SHIFT;
484 	dev_dbg(dev->dev, "Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n",
485 		num_pages, pfn_base << PAGE_SHIFT, 0);
486 	for (i = 0; i < num_pages; ++i) {
487 		pte = psb_gtt_mask_pte(pfn_base + i, PSB_MMU_CACHED_MEMORY);
488 		iowrite32(pte, dev_priv->gtt_map + i);
489 	}
490 
491 	/*
492 	 * Init rest of GTT to the scratch page to avoid accidents or scribbles
493 	 */
494 
495 	pfn_base = page_to_pfn(dev_priv->scratch_page);
496 	pte = psb_gtt_mask_pte(pfn_base, PSB_MMU_CACHED_MEMORY);
497 	for (; i < gtt_pages; ++i)
498 		iowrite32(pte, dev_priv->gtt_map + i);
499 
500 	(void) ioread32(dev_priv->gtt_map + i - 1);
501 	return 0;
502 
503 out_err:
504 	psb_gtt_takedown(dev);
505 	return ret;
506 }
507 
508 int psb_gtt_restore(struct drm_device *dev)
509 {
510 	struct drm_psb_private *dev_priv = dev->dev_private;
511 	struct resource *r = dev_priv->gtt_mem->child;
512 	struct gtt_range *range;
513 	unsigned int restored = 0, total = 0, size = 0;
514 
515 	/* On resume, the gtt_mutex is already initialized */
516 	mutex_lock(&dev_priv->gtt_mutex);
517 	psb_gtt_init(dev, 1);
518 
519 	while (r != NULL) {
520 		range = container_of(r, struct gtt_range, resource);
521 		if (range->pages) {
522 			psb_gtt_insert(dev, range, 1);
523 			size += range->resource.end - range->resource.start;
524 			restored++;
525 		}
526 		r = r->sibling;
527 		total++;
528 	}
529 	mutex_unlock(&dev_priv->gtt_mutex);
530 	DRM_DEBUG_DRIVER("Restored %u of %u gtt ranges (%u KB)", restored,
531 			 total, (size / 1024));
532 
533 	return 0;
534 }
535