1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2007, Intel Corporation.
6 * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com>
10 #include "gem.h" /* TODO: for struct psb_gem_object, see psb_gtt_restore() */
15 * GTT resource allocator - manage page mappings in GTT space
18 int psb_gtt_allocate_resource(struct drm_psb_private *pdev, struct resource *res,
19 const char *name, resource_size_t size, resource_size_t align,
20 bool stolen, u32 *offset)
22 struct resource *root = pdev->gtt_mem;
23 resource_size_t start, end;
27 /* The start of the GTT is backed by stolen pages. */
29 end = root->start + pdev->gtt.stolen_size - 1;
31 /* The rest is backed by system pages. */
32 start = root->start + pdev->gtt.stolen_size;
37 ret = allocate_resource(root, res, size, start, end, align, NULL, NULL);
40 *offset = res->start - root->start;
46 * psb_gtt_mask_pte - generate GTT pte entry
47 * @pfn: page number to encode
48 * @type: type of memory in the GTT
50 * Set the GTT entry for the appropriate memory type.
52 static inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type)
54 uint32_t mask = PSB_PTE_VALID;
56 /* Ensure we explode rather than put an invalid low mapping of
57 a high mapping page into the gtt */
58 BUG_ON(pfn & ~(0xFFFFFFFF >> PAGE_SHIFT));
60 if (type & PSB_MMU_CACHED_MEMORY)
61 mask |= PSB_PTE_CACHED;
62 if (type & PSB_MMU_RO_MEMORY)
64 if (type & PSB_MMU_WO_MEMORY)
67 return (pfn << PAGE_SHIFT) | mask;
70 static u32 __iomem *psb_gtt_entry(struct drm_psb_private *pdev, const struct resource *res)
72 unsigned long offset = res->start - pdev->gtt_mem->start;
74 return pdev->gtt_map + (offset >> PAGE_SHIFT);
78 * Take our preallocated GTT range and insert the GEM object into
79 * the GTT. This is protected via the gtt mutex which the caller
82 void psb_gtt_insert_pages(struct drm_psb_private *pdev, const struct resource *res,
85 resource_size_t npages, i;
86 u32 __iomem *gtt_slot;
89 /* Write our page entries into the GTT itself */
91 npages = resource_size(res) >> PAGE_SHIFT;
92 gtt_slot = psb_gtt_entry(pdev, res);
94 for (i = 0; i < npages; ++i, ++gtt_slot) {
95 pte = psb_gtt_mask_pte(page_to_pfn(pages[i]), PSB_MMU_CACHED_MEMORY);
96 iowrite32(pte, gtt_slot);
99 /* Make sure all the entries are set before we return */
100 ioread32(gtt_slot - 1);
104 * Remove a preallocated GTT range from the GTT. Overwrite all the
105 * page table entries with the dummy page. This is protected via the gtt
106 * mutex which the caller must hold.
108 void psb_gtt_remove_pages(struct drm_psb_private *pdev, const struct resource *res)
110 resource_size_t npages, i;
111 u32 __iomem *gtt_slot;
114 /* Install scratch page for the resource */
116 pte = psb_gtt_mask_pte(page_to_pfn(pdev->scratch_page), PSB_MMU_CACHED_MEMORY);
118 npages = resource_size(res) >> PAGE_SHIFT;
119 gtt_slot = psb_gtt_entry(pdev, res);
121 for (i = 0; i < npages; ++i, ++gtt_slot)
122 iowrite32(pte, gtt_slot);
124 /* Make sure all the entries are set before we return */
125 ioread32(gtt_slot - 1);
128 static void psb_gtt_alloc(struct drm_device *dev)
130 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
131 init_rwsem(&dev_priv->gtt.sem);
134 void psb_gtt_takedown(struct drm_device *dev)
136 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
137 struct pci_dev *pdev = to_pci_dev(dev->dev);
139 if (dev_priv->gtt_map) {
140 iounmap(dev_priv->gtt_map);
141 dev_priv->gtt_map = NULL;
143 if (dev_priv->gtt_initialized) {
144 pci_write_config_word(pdev, PSB_GMCH_CTRL,
145 dev_priv->gmch_ctrl);
146 PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL);
147 (void) PSB_RVDC32(PSB_PGETBL_CTL);
149 if (dev_priv->vram_addr)
150 iounmap(dev_priv->gtt_map);
153 int psb_gtt_init(struct drm_device *dev, int resume)
155 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
156 struct pci_dev *pdev = to_pci_dev(dev->dev);
158 unsigned long stolen_size, vram_stolen_size;
159 unsigned i, num_pages;
167 mutex_init(&dev_priv->gtt_mutex);
168 mutex_init(&dev_priv->mmap_mutex);
175 pci_read_config_word(pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl);
176 pci_write_config_word(pdev, PSB_GMCH_CTRL,
177 dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
179 dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL);
180 PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
181 (void) PSB_RVDC32(PSB_PGETBL_CTL);
183 /* The root resource we allocate address space from */
184 dev_priv->gtt_initialized = 1;
186 pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK;
189 * The video mmu has a hw bug when accessing 0x0D0000000.
190 * Make gatt start at 0x0e000,0000. This doesn't actually
191 * matter for us but may do if the video acceleration ever
194 pg->mmu_gatt_start = 0xE0000000;
196 pg->gtt_start = pci_resource_start(pdev, PSB_GTT_RESOURCE);
197 gtt_pages = pci_resource_len(pdev, PSB_GTT_RESOURCE)
199 /* CDV doesn't report this. In which case the system has 64 gtt pages */
200 if (pg->gtt_start == 0 || gtt_pages == 0) {
201 dev_dbg(dev->dev, "GTT PCI BAR not initialized.\n");
203 pg->gtt_start = dev_priv->pge_ctl;
206 pg->gatt_start = pci_resource_start(pdev, PSB_GATT_RESOURCE);
207 pg->gatt_pages = pci_resource_len(pdev, PSB_GATT_RESOURCE)
209 dev_priv->gtt_mem = &pdev->resource[PSB_GATT_RESOURCE];
211 if (pg->gatt_pages == 0 || pg->gatt_start == 0) {
212 static struct resource fudge; /* Preferably peppermint */
213 /* This can occur on CDV systems. Fudge it in this case.
214 We really don't care what imaginary space is being allocated
216 dev_dbg(dev->dev, "GATT PCI BAR not initialized.\n");
217 pg->gatt_start = 0x40000000;
218 pg->gatt_pages = (128 * 1024 * 1024) >> PAGE_SHIFT;
219 /* This is a little confusing but in fact the GTT is providing
220 a view from the GPU into memory and not vice versa. As such
221 this is really allocating space that is not the same as the
222 CPU address space on CDV */
223 fudge.start = 0x40000000;
224 fudge.end = 0x40000000 + 128 * 1024 * 1024 - 1;
225 fudge.name = "fudge";
226 fudge.flags = IORESOURCE_MEM;
227 dev_priv->gtt_mem = &fudge;
230 pci_read_config_dword(pdev, PSB_BSM, &dev_priv->stolen_base);
231 vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base
234 stolen_size = vram_stolen_size;
236 dev_dbg(dev->dev, "Stolen memory base 0x%x, size %luK\n",
237 dev_priv->stolen_base, vram_stolen_size / 1024);
239 if (resume && (gtt_pages != pg->gtt_pages) &&
240 (stolen_size != pg->stolen_size)) {
241 dev_err(dev->dev, "GTT resume error.\n");
246 pg->gtt_pages = gtt_pages;
247 pg->stolen_size = stolen_size;
248 dev_priv->vram_stolen_size = vram_stolen_size;
251 * Map the GTT and the stolen memory area
254 dev_priv->gtt_map = ioremap(pg->gtt_phys_start,
255 gtt_pages << PAGE_SHIFT);
256 if (!dev_priv->gtt_map) {
257 dev_err(dev->dev, "Failure to map gtt.\n");
263 dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base,
266 if (!dev_priv->vram_addr) {
267 dev_err(dev->dev, "Failure to map stolen base.\n");
273 * Insert vram stolen pages into the GTT
276 pfn_base = dev_priv->stolen_base >> PAGE_SHIFT;
277 num_pages = vram_stolen_size >> PAGE_SHIFT;
278 dev_dbg(dev->dev, "Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n",
279 num_pages, pfn_base << PAGE_SHIFT, 0);
280 for (i = 0; i < num_pages; ++i) {
281 pte = psb_gtt_mask_pte(pfn_base + i, PSB_MMU_CACHED_MEMORY);
282 iowrite32(pte, dev_priv->gtt_map + i);
286 * Init rest of GTT to the scratch page to avoid accidents or scribbles
289 pfn_base = page_to_pfn(dev_priv->scratch_page);
290 pte = psb_gtt_mask_pte(pfn_base, PSB_MMU_CACHED_MEMORY);
291 for (; i < gtt_pages; ++i)
292 iowrite32(pte, dev_priv->gtt_map + i);
294 (void) ioread32(dev_priv->gtt_map + i - 1);
298 psb_gtt_takedown(dev);
302 int psb_gtt_restore(struct drm_device *dev)
304 struct drm_psb_private *dev_priv = to_drm_psb_private(dev);
305 struct resource *r = dev_priv->gtt_mem->child;
306 struct psb_gem_object *pobj;
307 unsigned int restored = 0, total = 0, size = 0;
309 /* On resume, the gtt_mutex is already initialized */
310 mutex_lock(&dev_priv->gtt_mutex);
311 psb_gtt_init(dev, 1);
315 * TODO: GTT restoration needs a refactoring, so that we don't have to touch
316 * struct psb_gem_object here. The type represents a GEM object and is
317 * not related to the GTT itself.
319 pobj = container_of(r, struct psb_gem_object, resource);
321 psb_gtt_insert_pages(dev_priv, &pobj->resource, pobj->pages);
322 size += pobj->resource.end - pobj->resource.start;
328 mutex_unlock(&dev_priv->gtt_mutex);
329 DRM_DEBUG_DRIVER("Restored %u of %u gtt ranges (%u KB)", restored,
330 total, (size / 1024));