1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007, Intel Corporation.
6 * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com>
10 #include <linux/shmem_fs.h>
12 #include <asm/set_memory.h>
18 * GTT resource allocator - manage page mappings in GTT space
22 * psb_gtt_mask_pte - generate GTT pte entry
23 * @pfn: page number to encode
24 * @type: type of memory in the GTT
26 * Set the GTT entry for the appropriate memory type.
28 static inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type)
30 uint32_t mask = PSB_PTE_VALID;
32 /* Ensure we explode rather than put an invalid low mapping of
33 a high mapping page into the gtt */
34 BUG_ON(pfn & ~(0xFFFFFFFF >> PAGE_SHIFT));
36 if (type & PSB_MMU_CACHED_MEMORY)
37 mask |= PSB_PTE_CACHED;
38 if (type & PSB_MMU_RO_MEMORY)
40 if (type & PSB_MMU_WO_MEMORY)
43 return (pfn << PAGE_SHIFT) | mask;
47 * psb_gtt_entry - find the GTT entries for a gtt_range
48 * @dev: our DRM device
51 * Given a gtt_range object return the GTT offset of the page table
52 * entries for this gtt_range
54 static u32 __iomem *psb_gtt_entry(struct drm_device *dev, struct gtt_range *r)
56 struct drm_psb_private *dev_priv = dev->dev_private;
59 offset = r->resource.start - dev_priv->gtt_mem->start;
61 return dev_priv->gtt_map + (offset >> PAGE_SHIFT);
65 * psb_gtt_insert - put an object into the GTT
66 * @dev: our DRM device
70 * Take our preallocated GTT range and insert the GEM object into
71 * the GTT. This is protected via the gtt mutex which the caller
74 static int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r,
77 u32 __iomem *gtt_slot;
82 if (r->pages == NULL) {
87 WARN_ON(r->stolen); /* refcount these maybe ? */
89 gtt_slot = psb_gtt_entry(dev, r);
93 /* Make sure changes are visible to the GPU */
94 set_pages_array_wc(pages, r->npage);
97 /* Write our page entries into the GTT itself */
98 for (i = 0; i < r->npage; i++) {
99 pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]),
100 PSB_MMU_CACHED_MEMORY);
101 iowrite32(pte, gtt_slot++);
104 /* Make sure all the entries are set before we return */
105 ioread32(gtt_slot - 1);
111 * psb_gtt_remove - remove an object from the GTT
112 * @dev: our DRM device
115 * Remove a preallocated GTT range from the GTT. Overwrite all the
116 * page table entries with the dummy page. This is protected via the gtt
117 * mutex which the caller must hold.
119 static void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r)
121 struct drm_psb_private *dev_priv = dev->dev_private;
122 u32 __iomem *gtt_slot;
128 gtt_slot = psb_gtt_entry(dev, r);
129 pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page),
130 PSB_MMU_CACHED_MEMORY);
132 for (i = 0; i < r->npage; i++)
133 iowrite32(pte, gtt_slot++);
134 ioread32(gtt_slot - 1);
135 set_pages_array_wb(r->pages, r->npage);
139 * psb_gtt_attach_pages - attach and pin GEM pages
142 * Pin and build an in kernel list of the pages that back our GEM object.
143 * While we hold this the pages cannot be swapped out. This is protected
144 * via the gtt mutex which the caller must hold.
146 static int psb_gtt_attach_pages(struct gtt_range *gt)
152 pages = drm_gem_get_pages(>->gem);
154 return PTR_ERR(pages);
156 gt->npage = gt->gem.size / PAGE_SIZE;
163 * psb_gtt_detach_pages - attach and pin GEM pages
166 * Undo the effect of psb_gtt_attach_pages. At this point the pages
167 * must have been removed from the GTT as they could now be paged out
168 * and move bus address. This is protected via the gtt mutex which the
171 static void psb_gtt_detach_pages(struct gtt_range *gt)
173 drm_gem_put_pages(>->gem, gt->pages, true, false);
178 * psb_gtt_pin - pin pages into the GTT
181 * Pin a set of pages into the GTT. The pins are refcounted so that
182 * multiple pins need multiple unpins to undo.
184 * Non GEM backed objects treat this as a no-op as they are always GTT
187 int psb_gtt_pin(struct gtt_range *gt)
190 struct drm_device *dev = gt->gem.dev;
191 struct drm_psb_private *dev_priv = dev->dev_private;
192 u32 gpu_base = dev_priv->gtt.gatt_start;
194 mutex_lock(&dev_priv->gtt_mutex);
196 if (gt->in_gart == 0 && gt->stolen == 0) {
197 ret = psb_gtt_attach_pages(gt);
200 ret = psb_gtt_insert(dev, gt, 0);
202 psb_gtt_detach_pages(gt);
205 psb_mmu_insert_pages(psb_mmu_get_default_pd(dev_priv->mmu),
206 gt->pages, (gpu_base + gt->offset),
207 gt->npage, 0, 0, PSB_MMU_CACHED_MEMORY);
211 mutex_unlock(&dev_priv->gtt_mutex);
216 * psb_gtt_unpin - Drop a GTT pin requirement
219 * Undoes the effect of psb_gtt_pin. On the last drop the GEM object
220 * will be removed from the GTT which will also drop the page references
221 * and allow the VM to clean up or page stuff.
223 * Non GEM backed objects treat this as a no-op as they are always GTT
226 void psb_gtt_unpin(struct gtt_range *gt)
228 struct drm_device *dev = gt->gem.dev;
229 struct drm_psb_private *dev_priv = dev->dev_private;
230 u32 gpu_base = dev_priv->gtt.gatt_start;
232 mutex_lock(&dev_priv->gtt_mutex);
234 WARN_ON(!gt->in_gart);
237 if (gt->in_gart == 0 && gt->stolen == 0) {
238 psb_mmu_remove_pages(psb_mmu_get_default_pd(dev_priv->mmu),
239 (gpu_base + gt->offset), gt->npage, 0, 0);
240 psb_gtt_remove(dev, gt);
241 psb_gtt_detach_pages(gt);
244 mutex_unlock(&dev_priv->gtt_mutex);
248 * GTT resource allocator - allocate and manage GTT address space
252 * psb_gtt_alloc_range - allocate GTT address space
253 * @dev: Our DRM device
254 * @len: length (bytes) of address space required
255 * @name: resource name
256 * @backed: resource should be backed by stolen pages
257 * @align: requested alignment
259 * Ask the kernel core to find us a suitable range of addresses
260 * to use for a GTT mapping.
262 * Returns a gtt_range structure describing the object, or NULL on
263 * error. On successful return the resource is both allocated and marked
266 struct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len,
267 const char *name, int backed, u32 align)
269 struct drm_psb_private *dev_priv = dev->dev_private;
270 struct gtt_range *gt;
271 struct resource *r = dev_priv->gtt_mem;
273 unsigned long start, end;
276 /* The start of the GTT is the stolen pages */
278 end = r->start + dev_priv->gtt.stolen_size - 1;
280 /* The rest we will use for GEM backed objects */
281 start = r->start + dev_priv->gtt.stolen_size;
285 gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL);
288 gt->resource.name = name;
290 gt->in_gart = backed;
291 /* Ensure this is set for non GEM objects */
293 ret = allocate_resource(dev_priv->gtt_mem, >->resource,
294 len, start, end, align, NULL, NULL);
296 gt->offset = gt->resource.start - r->start;
304 * psb_gtt_free_range - release GTT address space
305 * @dev: our DRM device
306 * @gt: a mapping created with psb_gtt_alloc_range
308 * Release a resource that was allocated with psb_gtt_alloc_range. If the
309 * object has been pinned by mmap users we clean this up here currently.
311 void psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt)
313 /* Undo the mmap pin if we are destroying the object */
318 WARN_ON(gt->in_gart && !gt->stolen);
319 release_resource(>->resource);
323 static void psb_gtt_alloc(struct drm_device *dev)
325 struct drm_psb_private *dev_priv = dev->dev_private;
326 init_rwsem(&dev_priv->gtt.sem);
329 void psb_gtt_takedown(struct drm_device *dev)
331 struct drm_psb_private *dev_priv = dev->dev_private;
332 struct pci_dev *pdev = to_pci_dev(dev->dev);
334 if (dev_priv->gtt_map) {
335 iounmap(dev_priv->gtt_map);
336 dev_priv->gtt_map = NULL;
338 if (dev_priv->gtt_initialized) {
339 pci_write_config_word(pdev, PSB_GMCH_CTRL,
340 dev_priv->gmch_ctrl);
341 PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL);
342 (void) PSB_RVDC32(PSB_PGETBL_CTL);
344 if (dev_priv->vram_addr)
345 iounmap(dev_priv->gtt_map);
348 int psb_gtt_init(struct drm_device *dev, int resume)
350 struct drm_psb_private *dev_priv = dev->dev_private;
351 struct pci_dev *pdev = to_pci_dev(dev->dev);
353 unsigned long stolen_size, vram_stolen_size;
354 unsigned i, num_pages;
362 mutex_init(&dev_priv->gtt_mutex);
363 mutex_init(&dev_priv->mmap_mutex);
370 pci_read_config_word(pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl);
371 pci_write_config_word(pdev, PSB_GMCH_CTRL,
372 dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
374 dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL);
375 PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
376 (void) PSB_RVDC32(PSB_PGETBL_CTL);
378 /* The root resource we allocate address space from */
379 dev_priv->gtt_initialized = 1;
381 pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK;
384 * The video mmu has a hw bug when accessing 0x0D0000000.
385 * Make gatt start at 0x0e000,0000. This doesn't actually
386 * matter for us but may do if the video acceleration ever
389 pg->mmu_gatt_start = 0xE0000000;
391 pg->gtt_start = pci_resource_start(pdev, PSB_GTT_RESOURCE);
392 gtt_pages = pci_resource_len(pdev, PSB_GTT_RESOURCE)
394 /* CDV doesn't report this. In which case the system has 64 gtt pages */
395 if (pg->gtt_start == 0 || gtt_pages == 0) {
396 dev_dbg(dev->dev, "GTT PCI BAR not initialized.\n");
398 pg->gtt_start = dev_priv->pge_ctl;
401 pg->gatt_start = pci_resource_start(pdev, PSB_GATT_RESOURCE);
402 pg->gatt_pages = pci_resource_len(pdev, PSB_GATT_RESOURCE)
404 dev_priv->gtt_mem = &pdev->resource[PSB_GATT_RESOURCE];
406 if (pg->gatt_pages == 0 || pg->gatt_start == 0) {
407 static struct resource fudge; /* Preferably peppermint */
408 /* This can occur on CDV systems. Fudge it in this case.
409 We really don't care what imaginary space is being allocated
411 dev_dbg(dev->dev, "GATT PCI BAR not initialized.\n");
412 pg->gatt_start = 0x40000000;
413 pg->gatt_pages = (128 * 1024 * 1024) >> PAGE_SHIFT;
414 /* This is a little confusing but in fact the GTT is providing
415 a view from the GPU into memory and not vice versa. As such
416 this is really allocating space that is not the same as the
417 CPU address space on CDV */
418 fudge.start = 0x40000000;
419 fudge.end = 0x40000000 + 128 * 1024 * 1024 - 1;
420 fudge.name = "fudge";
421 fudge.flags = IORESOURCE_MEM;
422 dev_priv->gtt_mem = &fudge;
425 pci_read_config_dword(pdev, PSB_BSM, &dev_priv->stolen_base);
426 vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base
429 stolen_size = vram_stolen_size;
431 dev_dbg(dev->dev, "Stolen memory base 0x%x, size %luK\n",
432 dev_priv->stolen_base, vram_stolen_size / 1024);
434 if (resume && (gtt_pages != pg->gtt_pages) &&
435 (stolen_size != pg->stolen_size)) {
436 dev_err(dev->dev, "GTT resume error.\n");
441 pg->gtt_pages = gtt_pages;
442 pg->stolen_size = stolen_size;
443 dev_priv->vram_stolen_size = vram_stolen_size;
446 * Map the GTT and the stolen memory area
449 dev_priv->gtt_map = ioremap(pg->gtt_phys_start,
450 gtt_pages << PAGE_SHIFT);
451 if (!dev_priv->gtt_map) {
452 dev_err(dev->dev, "Failure to map gtt.\n");
458 dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base,
461 if (!dev_priv->vram_addr) {
462 dev_err(dev->dev, "Failure to map stolen base.\n");
468 * Insert vram stolen pages into the GTT
471 pfn_base = dev_priv->stolen_base >> PAGE_SHIFT;
472 num_pages = vram_stolen_size >> PAGE_SHIFT;
473 dev_dbg(dev->dev, "Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n",
474 num_pages, pfn_base << PAGE_SHIFT, 0);
475 for (i = 0; i < num_pages; ++i) {
476 pte = psb_gtt_mask_pte(pfn_base + i, PSB_MMU_CACHED_MEMORY);
477 iowrite32(pte, dev_priv->gtt_map + i);
481 * Init rest of GTT to the scratch page to avoid accidents or scribbles
484 pfn_base = page_to_pfn(dev_priv->scratch_page);
485 pte = psb_gtt_mask_pte(pfn_base, PSB_MMU_CACHED_MEMORY);
486 for (; i < gtt_pages; ++i)
487 iowrite32(pte, dev_priv->gtt_map + i);
489 (void) ioread32(dev_priv->gtt_map + i - 1);
493 psb_gtt_takedown(dev);
497 int psb_gtt_restore(struct drm_device *dev)
499 struct drm_psb_private *dev_priv = dev->dev_private;
500 struct resource *r = dev_priv->gtt_mem->child;
501 struct gtt_range *range;
502 unsigned int restored = 0, total = 0, size = 0;
504 /* On resume, the gtt_mutex is already initialized */
505 mutex_lock(&dev_priv->gtt_mutex);
506 psb_gtt_init(dev, 1);
509 range = container_of(r, struct gtt_range, resource);
511 psb_gtt_insert(dev, range, 1);
512 size += range->resource.end - range->resource.start;
518 mutex_unlock(&dev_priv->gtt_mutex);
519 DRM_DEBUG_DRIVER("Restored %u of %u gtt ranges (%u KB)", restored,
520 total, (size / 1024));