blob: 54e5c9e1e6fab7fa36bc99c08e09eb3388df305f [file] [log] [blame]
Alan Cox8c8f1c92011-11-03 18:21:09 +00001/*
2 * Copyright (c) 2007, Intel Corporation.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com>
19 * Alan Cox <alan@linux.intel.com>
20 */
21
22#include <drm/drmP.h>
Alan Coxe912b6d2012-01-24 16:57:42 +000023#include <linux/shmem_fs.h>
Alan Cox8c8f1c92011-11-03 18:21:09 +000024#include "psb_drv.h"
25
26
27/*
28 * GTT resource allocator - manage page mappings in GTT space
29 */
30
31/**
32 * psb_gtt_mask_pte - generate GTT pte entry
33 * @pfn: page number to encode
34 * @type: type of memory in the GTT
35 *
36 * Set the GTT entry for the appropriate memory type.
37 */
38static inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type)
39{
40 uint32_t mask = PSB_PTE_VALID;
41
Alan Cox398b4702012-04-25 14:38:47 +010042 /* Ensure we explode rather than put an invalid low mapping of
43 a high mapping page into the gtt */
44 BUG_ON(pfn & ~(0xFFFFFFFF >> PAGE_SHIFT));
45
Alan Cox8c8f1c92011-11-03 18:21:09 +000046 if (type & PSB_MMU_CACHED_MEMORY)
47 mask |= PSB_PTE_CACHED;
48 if (type & PSB_MMU_RO_MEMORY)
49 mask |= PSB_PTE_RO;
50 if (type & PSB_MMU_WO_MEMORY)
51 mask |= PSB_PTE_WO;
52
53 return (pfn << PAGE_SHIFT) | mask;
54}
55
56/**
57 * psb_gtt_entry - find the GTT entries for a gtt_range
58 * @dev: our DRM device
59 * @r: our GTT range
60 *
61 * Given a gtt_range object return the GTT offset of the page table
62 * entries for this gtt_range
63 */
Kirill A. Shutemovffe94d92012-03-08 16:03:55 +000064static u32 *psb_gtt_entry(struct drm_device *dev, struct gtt_range *r)
Alan Cox8c8f1c92011-11-03 18:21:09 +000065{
66 struct drm_psb_private *dev_priv = dev->dev_private;
67 unsigned long offset;
68
69 offset = r->resource.start - dev_priv->gtt_mem->start;
70
71 return dev_priv->gtt_map + (offset >> PAGE_SHIFT);
72}
73
74/**
75 * psb_gtt_insert - put an object into the GTT
76 * @dev: our DRM device
77 * @r: our GTT range
78 *
79 * Take our preallocated GTT range and insert the GEM object into
Alan Coxa7460922011-11-29 22:21:03 +000080 * the GTT. This is protected via the gtt mutex which the caller
81 * must hold.
Alan Cox8c8f1c92011-11-03 18:21:09 +000082 */
83static int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r)
84{
85 u32 *gtt_slot, pte;
86 struct page **pages;
87 int i;
88
89 if (r->pages == NULL) {
90 WARN_ON(1);
91 return -EINVAL;
92 }
93
94 WARN_ON(r->stolen); /* refcount these maybe ? */
95
96 gtt_slot = psb_gtt_entry(dev, r);
97 pages = r->pages;
98
99 /* Make sure changes are visible to the GPU */
Alan Coxd955e712012-04-25 14:37:00 +0100100 set_pages_array_wc(pages, r->npage);
Alan Cox8c8f1c92011-11-03 18:21:09 +0000101
102 /* Write our page entries into the GTT itself */
Alan Coxa6ba5822011-11-29 22:27:22 +0000103 for (i = r->roll; i < r->npage; i++) {
104 pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
105 iowrite32(pte, gtt_slot++);
106 }
107 for (i = 0; i < r->roll; i++) {
108 pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
Alan Cox8c8f1c92011-11-03 18:21:09 +0000109 iowrite32(pte, gtt_slot++);
110 }
111 /* Make sure all the entries are set before we return */
112 ioread32(gtt_slot - 1);
Alan Coxa6ba5822011-11-29 22:27:22 +0000113
Alan Cox8c8f1c92011-11-03 18:21:09 +0000114 return 0;
115}
116
117/**
118 * psb_gtt_remove - remove an object from the GTT
119 * @dev: our DRM device
120 * @r: our GTT range
121 *
122 * Remove a preallocated GTT range from the GTT. Overwrite all the
Alan Coxa7460922011-11-29 22:21:03 +0000123 * page table entries with the dummy page. This is protected via the gtt
124 * mutex which the caller must hold.
Alan Cox8c8f1c92011-11-03 18:21:09 +0000125 */
Alan Cox8c8f1c92011-11-03 18:21:09 +0000126static void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r)
127{
128 struct drm_psb_private *dev_priv = dev->dev_private;
129 u32 *gtt_slot, pte;
130 int i;
131
132 WARN_ON(r->stolen);
133
134 gtt_slot = psb_gtt_entry(dev, r);
135 pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page), 0);
136
137 for (i = 0; i < r->npage; i++)
138 iowrite32(pte, gtt_slot++);
139 ioread32(gtt_slot - 1);
140 set_pages_array_wb(r->pages, r->npage);
141}
142
143/**
Alan Coxa6ba5822011-11-29 22:27:22 +0000144 * psb_gtt_roll - set scrolling position
145 * @dev: our DRM device
146 * @r: the gtt mapping we are using
147 * @roll: roll offset
148 *
149 * Roll an existing pinned mapping by moving the pages through the GTT.
150 * This allows us to implement hardware scrolling on the consoles without
151 * a 2D engine
152 */
153void psb_gtt_roll(struct drm_device *dev, struct gtt_range *r, int roll)
154{
155 u32 *gtt_slot, pte;
156 int i;
157
158 if (roll >= r->npage) {
159 WARN_ON(1);
160 return;
161 }
162
163 r->roll = roll;
164
165 /* Not currently in the GTT - no worry we will write the mapping at
166 the right position when it gets pinned */
167 if (!r->stolen && !r->in_gart)
168 return;
169
170 gtt_slot = psb_gtt_entry(dev, r);
171
172 for (i = r->roll; i < r->npage; i++) {
173 pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
174 iowrite32(pte, gtt_slot++);
175 }
176 for (i = 0; i < r->roll; i++) {
177 pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 0);
178 iowrite32(pte, gtt_slot++);
179 }
180 ioread32(gtt_slot - 1);
181}
182
183/**
Alan Cox8c8f1c92011-11-03 18:21:09 +0000184 * psb_gtt_attach_pages - attach and pin GEM pages
185 * @gt: the gtt range
186 *
187 * Pin and build an in kernel list of the pages that back our GEM object.
Alan Coxa7460922011-11-29 22:21:03 +0000188 * While we hold this the pages cannot be swapped out. This is protected
189 * via the gtt mutex which the caller must hold.
Alan Cox8c8f1c92011-11-03 18:21:09 +0000190 */
191static int psb_gtt_attach_pages(struct gtt_range *gt)
192{
193 struct inode *inode;
194 struct address_space *mapping;
195 int i;
196 struct page *p;
197 int pages = gt->gem.size / PAGE_SIZE;
198
199 WARN_ON(gt->pages);
200
201 /* This is the shared memory object that backs the GEM resource */
202 inode = gt->gem.filp->f_path.dentry->d_inode;
203 mapping = inode->i_mapping;
204
205 gt->pages = kmalloc(pages * sizeof(struct page *), GFP_KERNEL);
206 if (gt->pages == NULL)
207 return -ENOMEM;
208 gt->npage = pages;
209
210 for (i = 0; i < pages; i++) {
Alan Coxe912b6d2012-01-24 16:57:42 +0000211 p = shmem_read_mapping_page(mapping, i);
Alan Cox8c8f1c92011-11-03 18:21:09 +0000212 if (IS_ERR(p))
213 goto err;
214 gt->pages[i] = p;
215 }
216 return 0;
217
218err:
219 while (i--)
220 page_cache_release(gt->pages[i]);
221 kfree(gt->pages);
222 gt->pages = NULL;
223 return PTR_ERR(p);
224}
225
226/**
227 * psb_gtt_detach_pages - attach and pin GEM pages
228 * @gt: the gtt range
229 *
230 * Undo the effect of psb_gtt_attach_pages. At this point the pages
231 * must have been removed from the GTT as they could now be paged out
Alan Coxa7460922011-11-29 22:21:03 +0000232 * and move bus address. This is protected via the gtt mutex which the
233 * caller must hold.
Alan Cox8c8f1c92011-11-03 18:21:09 +0000234 */
235static void psb_gtt_detach_pages(struct gtt_range *gt)
236{
237 int i;
238 for (i = 0; i < gt->npage; i++) {
239 /* FIXME: do we need to force dirty */
240 set_page_dirty(gt->pages[i]);
241 page_cache_release(gt->pages[i]);
242 }
243 kfree(gt->pages);
244 gt->pages = NULL;
245}
246
247/**
248 * psb_gtt_pin - pin pages into the GTT
249 * @gt: range to pin
250 *
251 * Pin a set of pages into the GTT. The pins are refcounted so that
252 * multiple pins need multiple unpins to undo.
253 *
254 * Non GEM backed objects treat this as a no-op as they are always GTT
255 * backed objects.
256 */
257int psb_gtt_pin(struct gtt_range *gt)
258{
259 int ret = 0;
260 struct drm_device *dev = gt->gem.dev;
261 struct drm_psb_private *dev_priv = dev->dev_private;
262
263 mutex_lock(&dev_priv->gtt_mutex);
264
265 if (gt->in_gart == 0 && gt->stolen == 0) {
266 ret = psb_gtt_attach_pages(gt);
267 if (ret < 0)
268 goto out;
269 ret = psb_gtt_insert(dev, gt);
270 if (ret < 0) {
271 psb_gtt_detach_pages(gt);
272 goto out;
273 }
274 }
275 gt->in_gart++;
276out:
277 mutex_unlock(&dev_priv->gtt_mutex);
278 return ret;
279}
280
281/**
282 * psb_gtt_unpin - Drop a GTT pin requirement
283 * @gt: range to pin
284 *
285 * Undoes the effect of psb_gtt_pin. On the last drop the GEM object
286 * will be removed from the GTT which will also drop the page references
287 * and allow the VM to clean up or page stuff.
288 *
289 * Non GEM backed objects treat this as a no-op as they are always GTT
290 * backed objects.
291 */
292void psb_gtt_unpin(struct gtt_range *gt)
293{
294 struct drm_device *dev = gt->gem.dev;
295 struct drm_psb_private *dev_priv = dev->dev_private;
296
297 mutex_lock(&dev_priv->gtt_mutex);
298
299 WARN_ON(!gt->in_gart);
300
301 gt->in_gart--;
302 if (gt->in_gart == 0 && gt->stolen == 0) {
303 psb_gtt_remove(dev, gt);
304 psb_gtt_detach_pages(gt);
305 }
306 mutex_unlock(&dev_priv->gtt_mutex);
307}
308
309/*
310 * GTT resource allocator - allocate and manage GTT address space
311 */
312
313/**
314 * psb_gtt_alloc_range - allocate GTT address space
315 * @dev: Our DRM device
316 * @len: length (bytes) of address space required
317 * @name: resource name
318 * @backed: resource should be backed by stolen pages
319 *
320 * Ask the kernel core to find us a suitable range of addresses
321 * to use for a GTT mapping.
322 *
323 * Returns a gtt_range structure describing the object, or NULL on
324 * error. On successful return the resource is both allocated and marked
325 * as in use.
326 */
327struct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len,
328 const char *name, int backed)
329{
330 struct drm_psb_private *dev_priv = dev->dev_private;
331 struct gtt_range *gt;
332 struct resource *r = dev_priv->gtt_mem;
333 int ret;
334 unsigned long start, end;
335
336 if (backed) {
337 /* The start of the GTT is the stolen pages */
338 start = r->start;
339 end = r->start + dev_priv->gtt.stolen_size - 1;
340 } else {
341 /* The rest we will use for GEM backed objects */
342 start = r->start + dev_priv->gtt.stolen_size;
343 end = r->end;
344 }
345
346 gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL);
347 if (gt == NULL)
348 return NULL;
349 gt->resource.name = name;
350 gt->stolen = backed;
351 gt->in_gart = backed;
Alan Coxa6ba5822011-11-29 22:27:22 +0000352 gt->roll = 0;
Alan Cox8c8f1c92011-11-03 18:21:09 +0000353 /* Ensure this is set for non GEM objects */
354 gt->gem.dev = dev;
355 ret = allocate_resource(dev_priv->gtt_mem, &gt->resource,
356 len, start, end, PAGE_SIZE, NULL, NULL);
357 if (ret == 0) {
358 gt->offset = gt->resource.start - r->start;
359 return gt;
360 }
361 kfree(gt);
362 return NULL;
363}
364
365/**
366 * psb_gtt_free_range - release GTT address space
367 * @dev: our DRM device
368 * @gt: a mapping created with psb_gtt_alloc_range
369 *
370 * Release a resource that was allocated with psb_gtt_alloc_range. If the
371 * object has been pinned by mmap users we clean this up here currently.
372 */
373void psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt)
374{
375 /* Undo the mmap pin if we are destroying the object */
376 if (gt->mmapping) {
377 psb_gtt_unpin(gt);
378 gt->mmapping = 0;
379 }
380 WARN_ON(gt->in_gart && !gt->stolen);
381 release_resource(&gt->resource);
382 kfree(gt);
383}
384
Kirill A. Shutemovffe94d92012-03-08 16:03:55 +0000385static void psb_gtt_alloc(struct drm_device *dev)
Alan Cox8c8f1c92011-11-03 18:21:09 +0000386{
387 struct drm_psb_private *dev_priv = dev->dev_private;
388 init_rwsem(&dev_priv->gtt.sem);
389}
390
391void psb_gtt_takedown(struct drm_device *dev)
392{
393 struct drm_psb_private *dev_priv = dev->dev_private;
394
395 if (dev_priv->gtt_map) {
396 iounmap(dev_priv->gtt_map);
397 dev_priv->gtt_map = NULL;
398 }
399 if (dev_priv->gtt_initialized) {
400 pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
401 dev_priv->gmch_ctrl);
402 PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL);
403 (void) PSB_RVDC32(PSB_PGETBL_CTL);
404 }
405 if (dev_priv->vram_addr)
406 iounmap(dev_priv->gtt_map);
407}
408
409int psb_gtt_init(struct drm_device *dev, int resume)
410{
411 struct drm_psb_private *dev_priv = dev->dev_private;
412 unsigned gtt_pages;
413 unsigned long stolen_size, vram_stolen_size;
414 unsigned i, num_pages;
415 unsigned pfn_base;
416 uint32_t vram_pages;
417 uint32_t dvmt_mode = 0;
418 struct psb_gtt *pg;
419
420 int ret = 0;
421 uint32_t pte;
422
423 mutex_init(&dev_priv->gtt_mutex);
424
425 psb_gtt_alloc(dev);
426 pg = &dev_priv->gtt;
427
428 /* Enable the GTT */
429 pci_read_config_word(dev->pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl);
430 pci_write_config_word(dev->pdev, PSB_GMCH_CTRL,
431 dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED);
432
433 dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL);
434 PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL);
435 (void) PSB_RVDC32(PSB_PGETBL_CTL);
436
437 /* The root resource we allocate address space from */
438 dev_priv->gtt_initialized = 1;
439
440 pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK;
441
442 /*
Alan Coxa7460922011-11-29 22:21:03 +0000443 * The video mmu has a hw bug when accessing 0x0D0000000.
444 * Make gatt start at 0x0e000,0000. This doesn't actually
445 * matter for us but may do if the video acceleration ever
446 * gets opened up.
Alan Cox8c8f1c92011-11-03 18:21:09 +0000447 */
448 pg->mmu_gatt_start = 0xE0000000;
449
450 pg->gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE);
451 gtt_pages = pci_resource_len(dev->pdev, PSB_GTT_RESOURCE)
452 >> PAGE_SHIFT;
Alan Cox055bf382012-03-05 14:22:16 +0000453 /* CDV doesn't report this. In which case the system has 64 gtt pages */
Alan Cox8c8f1c92011-11-03 18:21:09 +0000454 if (pg->gtt_start == 0 || gtt_pages == 0) {
Alan Cox055bf382012-03-05 14:22:16 +0000455 dev_dbg(dev->dev, "GTT PCI BAR not initialized.\n");
Alan Cox8c8f1c92011-11-03 18:21:09 +0000456 gtt_pages = 64;
457 pg->gtt_start = dev_priv->pge_ctl;
458 }
459
460 pg->gatt_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE);
461 pg->gatt_pages = pci_resource_len(dev->pdev, PSB_GATT_RESOURCE)
462 >> PAGE_SHIFT;
463 dev_priv->gtt_mem = &dev->pdev->resource[PSB_GATT_RESOURCE];
464
465 if (pg->gatt_pages == 0 || pg->gatt_start == 0) {
466 static struct resource fudge; /* Preferably peppermint */
Alan Cox055bf382012-03-05 14:22:16 +0000467 /* This can occur on CDV systems. Fudge it in this case.
Alan Cox8c8f1c92011-11-03 18:21:09 +0000468 We really don't care what imaginary space is being allocated
469 at this point */
Alan Cox055bf382012-03-05 14:22:16 +0000470 dev_dbg(dev->dev, "GATT PCI BAR not initialized.\n");
Alan Cox8c8f1c92011-11-03 18:21:09 +0000471 pg->gatt_start = 0x40000000;
472 pg->gatt_pages = (128 * 1024 * 1024) >> PAGE_SHIFT;
Alan Coxa7460922011-11-29 22:21:03 +0000473 /* This is a little confusing but in fact the GTT is providing
474 a view from the GPU into memory and not vice versa. As such
475 this is really allocating space that is not the same as the
476 CPU address space on CDV */
Alan Cox8c8f1c92011-11-03 18:21:09 +0000477 fudge.start = 0x40000000;
478 fudge.end = 0x40000000 + 128 * 1024 * 1024 - 1;
479 fudge.name = "fudge";
480 fudge.flags = IORESOURCE_MEM;
481 dev_priv->gtt_mem = &fudge;
482 }
483
484 pci_read_config_dword(dev->pdev, PSB_BSM, &dev_priv->stolen_base);
485 vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base
486 - PAGE_SIZE;
487
488 stolen_size = vram_stolen_size;
489
490 printk(KERN_INFO "Stolen memory information\n");
491 printk(KERN_INFO " base in RAM: 0x%x\n", dev_priv->stolen_base);
492 printk(KERN_INFO " size: %luK, calculated by (GTT RAM base) - (Stolen base), seems wrong\n",
493 vram_stolen_size/1024);
494 dvmt_mode = (dev_priv->gmch_ctrl >> 4) & 0x7;
495 printk(KERN_INFO " the correct size should be: %dM(dvmt mode=%d)\n",
496 (dvmt_mode == 1) ? 1 : (2 << (dvmt_mode - 1)), dvmt_mode);
497
498 if (resume && (gtt_pages != pg->gtt_pages) &&
499 (stolen_size != pg->stolen_size)) {
500 dev_err(dev->dev, "GTT resume error.\n");
501 ret = -EINVAL;
502 goto out_err;
503 }
504
505 pg->gtt_pages = gtt_pages;
506 pg->stolen_size = stolen_size;
507 dev_priv->vram_stolen_size = vram_stolen_size;
508
509 /*
510 * Map the GTT and the stolen memory area
511 */
512 dev_priv->gtt_map = ioremap_nocache(pg->gtt_phys_start,
513 gtt_pages << PAGE_SHIFT);
514 if (!dev_priv->gtt_map) {
515 dev_err(dev->dev, "Failure to map gtt.\n");
516 ret = -ENOMEM;
517 goto out_err;
518 }
519
520 dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base, stolen_size);
521 if (!dev_priv->vram_addr) {
522 dev_err(dev->dev, "Failure to map stolen base.\n");
523 ret = -ENOMEM;
524 goto out_err;
525 }
526
527 /*
528 * Insert vram stolen pages into the GTT
529 */
530
531 pfn_base = dev_priv->stolen_base >> PAGE_SHIFT;
532 vram_pages = num_pages = vram_stolen_size >> PAGE_SHIFT;
533 printk(KERN_INFO"Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n",
534 num_pages, pfn_base << PAGE_SHIFT, 0);
535 for (i = 0; i < num_pages; ++i) {
536 pte = psb_gtt_mask_pte(pfn_base + i, 0);
537 iowrite32(pte, dev_priv->gtt_map + i);
538 }
539
540 /*
541 * Init rest of GTT to the scratch page to avoid accidents or scribbles
542 */
543
544 pfn_base = page_to_pfn(dev_priv->scratch_page);
545 pte = psb_gtt_mask_pte(pfn_base, 0);
546 for (; i < gtt_pages; ++i)
547 iowrite32(pte, dev_priv->gtt_map + i);
548
549 (void) ioread32(dev_priv->gtt_map + i - 1);
550 return 0;
551
552out_err:
553 psb_gtt_takedown(dev);
554 return ret;
555}