2 * Copyright (c) 2007, Intel Corporation.
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.
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
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.
18 * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com>
19 * Alan Cox <alan@linux.intel.com>
23 #include <linux/shmem_fs.h>
29 * GTT resource allocator - manage page mappings in GTT space
33 * psb_gtt_mask_pte - generate GTT pte entry
34 * @pfn: page number to encode
35 * @type: type of memory in the GTT
37 * Set the GTT entry for the appropriate memory type.
39 static inline uint32_t psb_gtt_mask_pte(uint32_t pfn
, int type
)
41 uint32_t mask
= PSB_PTE_VALID
;
43 /* Ensure we explode rather than put an invalid low mapping of
44 a high mapping page into the gtt */
45 BUG_ON(pfn
& ~(0xFFFFFFFF >> PAGE_SHIFT
));
47 if (type
& PSB_MMU_CACHED_MEMORY
)
48 mask
|= PSB_PTE_CACHED
;
49 if (type
& PSB_MMU_RO_MEMORY
)
51 if (type
& PSB_MMU_WO_MEMORY
)
54 return (pfn
<< PAGE_SHIFT
) | mask
;
58 * psb_gtt_entry - find the GTT entries for a gtt_range
59 * @dev: our DRM device
62 * Given a gtt_range object return the GTT offset of the page table
63 * entries for this gtt_range
65 static u32 __iomem
*psb_gtt_entry(struct drm_device
*dev
, struct gtt_range
*r
)
67 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
70 offset
= r
->resource
.start
- dev_priv
->gtt_mem
->start
;
72 return dev_priv
->gtt_map
+ (offset
>> PAGE_SHIFT
);
76 * psb_gtt_insert - put an object into the GTT
77 * @dev: our DRM device
81 * Take our preallocated GTT range and insert the GEM object into
82 * the GTT. This is protected via the gtt mutex which the caller
85 static int psb_gtt_insert(struct drm_device
*dev
, struct gtt_range
*r
,
88 u32 __iomem
*gtt_slot
;
93 if (r
->pages
== NULL
) {
98 WARN_ON(r
->stolen
); /* refcount these maybe ? */
100 gtt_slot
= psb_gtt_entry(dev
, r
);
104 /* Make sure changes are visible to the GPU */
105 set_pages_array_wc(pages
, r
->npage
);
108 /* Write our page entries into the GTT itself */
109 for (i
= r
->roll
; i
< r
->npage
; i
++) {
110 pte
= psb_gtt_mask_pte(page_to_pfn(r
->pages
[i
]),
111 PSB_MMU_CACHED_MEMORY
);
112 iowrite32(pte
, gtt_slot
++);
114 for (i
= 0; i
< r
->roll
; i
++) {
115 pte
= psb_gtt_mask_pte(page_to_pfn(r
->pages
[i
]),
116 PSB_MMU_CACHED_MEMORY
);
117 iowrite32(pte
, gtt_slot
++);
119 /* Make sure all the entries are set before we return */
120 ioread32(gtt_slot
- 1);
126 * psb_gtt_remove - remove an object from the GTT
127 * @dev: our DRM device
130 * Remove a preallocated GTT range from the GTT. Overwrite all the
131 * page table entries with the dummy page. This is protected via the gtt
132 * mutex which the caller must hold.
134 void psb_gtt_remove(struct drm_device
*dev
, struct gtt_range
*r
)
136 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
137 u32 __iomem
*gtt_slot
;
143 gtt_slot
= psb_gtt_entry(dev
, r
);
144 pte
= psb_gtt_mask_pte(page_to_pfn(dev_priv
->scratch_page
),
145 PSB_MMU_CACHED_MEMORY
);
147 for (i
= 0; i
< r
->npage
; i
++)
148 iowrite32(pte
, gtt_slot
++);
149 ioread32(gtt_slot
- 1);
150 set_pages_array_wb(r
->pages
, r
->npage
);
154 * psb_gtt_roll - set scrolling position
155 * @dev: our DRM device
156 * @r: the gtt mapping we are using
159 * Roll an existing pinned mapping by moving the pages through the GTT.
160 * This allows us to implement hardware scrolling on the consoles without
163 void psb_gtt_roll(struct drm_device
*dev
, struct gtt_range
*r
, int roll
)
165 u32 __iomem
*gtt_slot
;
169 if (roll
>= r
->npage
) {
176 /* Not currently in the GTT - no worry we will write the mapping at
177 the right position when it gets pinned */
178 if (!r
->stolen
&& !r
->in_gart
)
181 gtt_slot
= psb_gtt_entry(dev
, r
);
183 for (i
= r
->roll
; i
< r
->npage
; i
++) {
184 pte
= psb_gtt_mask_pte(page_to_pfn(r
->pages
[i
]),
185 PSB_MMU_CACHED_MEMORY
);
186 iowrite32(pte
, gtt_slot
++);
188 for (i
= 0; i
< r
->roll
; i
++) {
189 pte
= psb_gtt_mask_pte(page_to_pfn(r
->pages
[i
]),
190 PSB_MMU_CACHED_MEMORY
);
191 iowrite32(pte
, gtt_slot
++);
193 ioread32(gtt_slot
- 1);
197 * psb_gtt_attach_pages - attach and pin GEM pages
200 * Pin and build an in kernel list of the pages that back our GEM object.
201 * While we hold this the pages cannot be swapped out. This is protected
202 * via the gtt mutex which the caller must hold.
204 static int psb_gtt_attach_pages(struct gtt_range
*gt
)
210 pages
= drm_gem_get_pages(>
->gem
);
212 return PTR_ERR(pages
);
214 gt
->npage
= gt
->gem
.size
/ PAGE_SIZE
;
221 * psb_gtt_detach_pages - attach and pin GEM pages
224 * Undo the effect of psb_gtt_attach_pages. At this point the pages
225 * must have been removed from the GTT as they could now be paged out
226 * and move bus address. This is protected via the gtt mutex which the
229 static void psb_gtt_detach_pages(struct gtt_range
*gt
)
231 drm_gem_put_pages(>
->gem
, gt
->pages
, true, false);
236 * psb_gtt_pin - pin pages into the GTT
239 * Pin a set of pages into the GTT. The pins are refcounted so that
240 * multiple pins need multiple unpins to undo.
242 * Non GEM backed objects treat this as a no-op as they are always GTT
245 int psb_gtt_pin(struct gtt_range
*gt
)
248 struct drm_device
*dev
= gt
->gem
.dev
;
249 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
250 u32 gpu_base
= dev_priv
->gtt
.gatt_start
;
252 mutex_lock(&dev_priv
->gtt_mutex
);
254 if (gt
->in_gart
== 0 && gt
->stolen
== 0) {
255 ret
= psb_gtt_attach_pages(gt
);
258 ret
= psb_gtt_insert(dev
, gt
, 0);
260 psb_gtt_detach_pages(gt
);
263 psb_mmu_insert_pages(psb_mmu_get_default_pd(dev_priv
->mmu
),
264 gt
->pages
, (gpu_base
+ gt
->offset
),
265 gt
->npage
, 0, 0, PSB_MMU_CACHED_MEMORY
);
269 mutex_unlock(&dev_priv
->gtt_mutex
);
274 * psb_gtt_unpin - Drop a GTT pin requirement
277 * Undoes the effect of psb_gtt_pin. On the last drop the GEM object
278 * will be removed from the GTT which will also drop the page references
279 * and allow the VM to clean up or page stuff.
281 * Non GEM backed objects treat this as a no-op as they are always GTT
284 void psb_gtt_unpin(struct gtt_range
*gt
)
286 struct drm_device
*dev
= gt
->gem
.dev
;
287 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
288 u32 gpu_base
= dev_priv
->gtt
.gatt_start
;
291 /* While holding the gtt_mutex no new blits can be initiated */
292 mutex_lock(&dev_priv
->gtt_mutex
);
294 /* Wait for any possible usage of the memory to be finished */
295 ret
= gma_blt_wait_idle(dev_priv
);
297 DRM_ERROR("Failed to idle the blitter, unpin failed!");
301 WARN_ON(!gt
->in_gart
);
304 if (gt
->in_gart
== 0 && gt
->stolen
== 0) {
305 psb_mmu_remove_pages(psb_mmu_get_default_pd(dev_priv
->mmu
),
306 (gpu_base
+ gt
->offset
), gt
->npage
, 0, 0);
307 psb_gtt_remove(dev
, gt
);
308 psb_gtt_detach_pages(gt
);
312 mutex_unlock(&dev_priv
->gtt_mutex
);
316 * GTT resource allocator - allocate and manage GTT address space
320 * psb_gtt_alloc_range - allocate GTT address space
321 * @dev: Our DRM device
322 * @len: length (bytes) of address space required
323 * @name: resource name
324 * @backed: resource should be backed by stolen pages
325 * @align: requested alignment
327 * Ask the kernel core to find us a suitable range of addresses
328 * to use for a GTT mapping.
330 * Returns a gtt_range structure describing the object, or NULL on
331 * error. On successful return the resource is both allocated and marked
334 struct gtt_range
*psb_gtt_alloc_range(struct drm_device
*dev
, int len
,
335 const char *name
, int backed
, u32 align
)
337 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
338 struct gtt_range
*gt
;
339 struct resource
*r
= dev_priv
->gtt_mem
;
341 unsigned long start
, end
;
344 /* The start of the GTT is the stolen pages */
346 end
= r
->start
+ dev_priv
->gtt
.stolen_size
- 1;
348 /* The rest we will use for GEM backed objects */
349 start
= r
->start
+ dev_priv
->gtt
.stolen_size
;
353 gt
= kzalloc(sizeof(struct gtt_range
), GFP_KERNEL
);
356 gt
->resource
.name
= name
;
358 gt
->in_gart
= backed
;
360 /* Ensure this is set for non GEM objects */
362 ret
= allocate_resource(dev_priv
->gtt_mem
, >
->resource
,
363 len
, start
, end
, align
, NULL
, NULL
);
365 gt
->offset
= gt
->resource
.start
- r
->start
;
373 * psb_gtt_free_range - release GTT address space
374 * @dev: our DRM device
375 * @gt: a mapping created with psb_gtt_alloc_range
377 * Release a resource that was allocated with psb_gtt_alloc_range. If the
378 * object has been pinned by mmap users we clean this up here currently.
380 void psb_gtt_free_range(struct drm_device
*dev
, struct gtt_range
*gt
)
382 /* Undo the mmap pin if we are destroying the object */
387 WARN_ON(gt
->in_gart
&& !gt
->stolen
);
388 release_resource(>
->resource
);
392 static void psb_gtt_alloc(struct drm_device
*dev
)
394 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
395 init_rwsem(&dev_priv
->gtt
.sem
);
398 void psb_gtt_takedown(struct drm_device
*dev
)
400 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
402 if (dev_priv
->gtt_map
) {
403 iounmap(dev_priv
->gtt_map
);
404 dev_priv
->gtt_map
= NULL
;
406 if (dev_priv
->gtt_initialized
) {
407 pci_write_config_word(dev
->pdev
, PSB_GMCH_CTRL
,
408 dev_priv
->gmch_ctrl
);
409 PSB_WVDC32(dev_priv
->pge_ctl
, PSB_PGETBL_CTL
);
410 (void) PSB_RVDC32(PSB_PGETBL_CTL
);
412 if (dev_priv
->vram_addr
)
413 iounmap(dev_priv
->gtt_map
);
416 int psb_gtt_init(struct drm_device
*dev
, int resume
)
418 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
420 unsigned long stolen_size
, vram_stolen_size
;
421 unsigned i
, num_pages
;
429 mutex_init(&dev_priv
->gtt_mutex
);
430 mutex_init(&dev_priv
->mmap_mutex
);
437 pci_read_config_word(dev
->pdev
, PSB_GMCH_CTRL
, &dev_priv
->gmch_ctrl
);
438 pci_write_config_word(dev
->pdev
, PSB_GMCH_CTRL
,
439 dev_priv
->gmch_ctrl
| _PSB_GMCH_ENABLED
);
441 dev_priv
->pge_ctl
= PSB_RVDC32(PSB_PGETBL_CTL
);
442 PSB_WVDC32(dev_priv
->pge_ctl
| _PSB_PGETBL_ENABLED
, PSB_PGETBL_CTL
);
443 (void) PSB_RVDC32(PSB_PGETBL_CTL
);
445 /* The root resource we allocate address space from */
446 dev_priv
->gtt_initialized
= 1;
448 pg
->gtt_phys_start
= dev_priv
->pge_ctl
& PAGE_MASK
;
451 * The video mmu has a hw bug when accessing 0x0D0000000.
452 * Make gatt start at 0x0e000,0000. This doesn't actually
453 * matter for us but may do if the video acceleration ever
456 pg
->mmu_gatt_start
= 0xE0000000;
458 pg
->gtt_start
= pci_resource_start(dev
->pdev
, PSB_GTT_RESOURCE
);
459 gtt_pages
= pci_resource_len(dev
->pdev
, PSB_GTT_RESOURCE
)
461 /* CDV doesn't report this. In which case the system has 64 gtt pages */
462 if (pg
->gtt_start
== 0 || gtt_pages
== 0) {
463 dev_dbg(dev
->dev
, "GTT PCI BAR not initialized.\n");
465 pg
->gtt_start
= dev_priv
->pge_ctl
;
468 pg
->gatt_start
= pci_resource_start(dev
->pdev
, PSB_GATT_RESOURCE
);
469 pg
->gatt_pages
= pci_resource_len(dev
->pdev
, PSB_GATT_RESOURCE
)
471 dev_priv
->gtt_mem
= &dev
->pdev
->resource
[PSB_GATT_RESOURCE
];
473 if (pg
->gatt_pages
== 0 || pg
->gatt_start
== 0) {
474 static struct resource fudge
; /* Preferably peppermint */
475 /* This can occur on CDV systems. Fudge it in this case.
476 We really don't care what imaginary space is being allocated
478 dev_dbg(dev
->dev
, "GATT PCI BAR not initialized.\n");
479 pg
->gatt_start
= 0x40000000;
480 pg
->gatt_pages
= (128 * 1024 * 1024) >> PAGE_SHIFT
;
481 /* This is a little confusing but in fact the GTT is providing
482 a view from the GPU into memory and not vice versa. As such
483 this is really allocating space that is not the same as the
484 CPU address space on CDV */
485 fudge
.start
= 0x40000000;
486 fudge
.end
= 0x40000000 + 128 * 1024 * 1024 - 1;
487 fudge
.name
= "fudge";
488 fudge
.flags
= IORESOURCE_MEM
;
489 dev_priv
->gtt_mem
= &fudge
;
492 pci_read_config_dword(dev
->pdev
, PSB_BSM
, &dev_priv
->stolen_base
);
493 vram_stolen_size
= pg
->gtt_phys_start
- dev_priv
->stolen_base
496 stolen_size
= vram_stolen_size
;
498 dev_dbg(dev
->dev
, "Stolen memory base 0x%x, size %luK\n",
499 dev_priv
->stolen_base
, vram_stolen_size
/ 1024);
501 if (resume
&& (gtt_pages
!= pg
->gtt_pages
) &&
502 (stolen_size
!= pg
->stolen_size
)) {
503 dev_err(dev
->dev
, "GTT resume error.\n");
508 pg
->gtt_pages
= gtt_pages
;
509 pg
->stolen_size
= stolen_size
;
510 dev_priv
->vram_stolen_size
= vram_stolen_size
;
513 * Map the GTT and the stolen memory area
516 dev_priv
->gtt_map
= ioremap_nocache(pg
->gtt_phys_start
,
517 gtt_pages
<< PAGE_SHIFT
);
518 if (!dev_priv
->gtt_map
) {
519 dev_err(dev
->dev
, "Failure to map gtt.\n");
525 dev_priv
->vram_addr
= ioremap_wc(dev_priv
->stolen_base
,
528 if (!dev_priv
->vram_addr
) {
529 dev_err(dev
->dev
, "Failure to map stolen base.\n");
535 * Insert vram stolen pages into the GTT
538 pfn_base
= dev_priv
->stolen_base
>> PAGE_SHIFT
;
539 num_pages
= vram_stolen_size
>> PAGE_SHIFT
;
540 dev_dbg(dev
->dev
, "Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n",
541 num_pages
, pfn_base
<< PAGE_SHIFT
, 0);
542 for (i
= 0; i
< num_pages
; ++i
) {
543 pte
= psb_gtt_mask_pte(pfn_base
+ i
, PSB_MMU_CACHED_MEMORY
);
544 iowrite32(pte
, dev_priv
->gtt_map
+ i
);
548 * Init rest of GTT to the scratch page to avoid accidents or scribbles
551 pfn_base
= page_to_pfn(dev_priv
->scratch_page
);
552 pte
= psb_gtt_mask_pte(pfn_base
, PSB_MMU_CACHED_MEMORY
);
553 for (; i
< gtt_pages
; ++i
)
554 iowrite32(pte
, dev_priv
->gtt_map
+ i
);
556 (void) ioread32(dev_priv
->gtt_map
+ i
- 1);
560 psb_gtt_takedown(dev
);
564 int psb_gtt_restore(struct drm_device
*dev
)
566 struct drm_psb_private
*dev_priv
= dev
->dev_private
;
567 struct resource
*r
= dev_priv
->gtt_mem
->child
;
568 struct gtt_range
*range
;
569 unsigned int restored
= 0, total
= 0, size
= 0;
571 /* On resume, the gtt_mutex is already initialized */
572 mutex_lock(&dev_priv
->gtt_mutex
);
573 psb_gtt_init(dev
, 1);
576 range
= container_of(r
, struct gtt_range
, resource
);
578 psb_gtt_insert(dev
, range
, 1);
579 size
+= range
->resource
.end
- range
->resource
.start
;
585 mutex_unlock(&dev_priv
->gtt_mutex
);
586 DRM_DEBUG_DRIVER("Restored %u of %u gtt ranges (%u KB)", restored
,
587 total
, (size
/ 1024));