18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2007, Intel Corporation. 48c2ecf20Sopenharmony_ci * All Rights Reserved. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Authors: Thomas Hellstrom <thomas-at-tungstengraphics.com> 78c2ecf20Sopenharmony_ci * Alan Cox <alan@linux.intel.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/shmem_fs.h> 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ci#include <asm/set_memory.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci#include "blitter.h" 158c2ecf20Sopenharmony_ci#include "psb_drv.h" 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* 198c2ecf20Sopenharmony_ci * GTT resource allocator - manage page mappings in GTT space 208c2ecf20Sopenharmony_ci */ 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/** 238c2ecf20Sopenharmony_ci * psb_gtt_mask_pte - generate GTT pte entry 248c2ecf20Sopenharmony_ci * @pfn: page number to encode 258c2ecf20Sopenharmony_ci * @type: type of memory in the GTT 268c2ecf20Sopenharmony_ci * 278c2ecf20Sopenharmony_ci * Set the GTT entry for the appropriate memory type. 288c2ecf20Sopenharmony_ci */ 298c2ecf20Sopenharmony_cistatic inline uint32_t psb_gtt_mask_pte(uint32_t pfn, int type) 308c2ecf20Sopenharmony_ci{ 318c2ecf20Sopenharmony_ci uint32_t mask = PSB_PTE_VALID; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci /* Ensure we explode rather than put an invalid low mapping of 348c2ecf20Sopenharmony_ci a high mapping page into the gtt */ 358c2ecf20Sopenharmony_ci BUG_ON(pfn & ~(0xFFFFFFFF >> PAGE_SHIFT)); 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci if (type & PSB_MMU_CACHED_MEMORY) 388c2ecf20Sopenharmony_ci mask |= PSB_PTE_CACHED; 398c2ecf20Sopenharmony_ci if (type & PSB_MMU_RO_MEMORY) 408c2ecf20Sopenharmony_ci mask |= PSB_PTE_RO; 418c2ecf20Sopenharmony_ci if (type & PSB_MMU_WO_MEMORY) 428c2ecf20Sopenharmony_ci mask |= PSB_PTE_WO; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ci return (pfn << PAGE_SHIFT) | mask; 458c2ecf20Sopenharmony_ci} 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci/** 488c2ecf20Sopenharmony_ci * psb_gtt_entry - find the GTT entries for a gtt_range 498c2ecf20Sopenharmony_ci * @dev: our DRM device 508c2ecf20Sopenharmony_ci * @r: our GTT range 518c2ecf20Sopenharmony_ci * 528c2ecf20Sopenharmony_ci * Given a gtt_range object return the GTT offset of the page table 538c2ecf20Sopenharmony_ci * entries for this gtt_range 548c2ecf20Sopenharmony_ci */ 558c2ecf20Sopenharmony_cistatic u32 __iomem *psb_gtt_entry(struct drm_device *dev, struct gtt_range *r) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 588c2ecf20Sopenharmony_ci unsigned long offset; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci offset = r->resource.start - dev_priv->gtt_mem->start; 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci return dev_priv->gtt_map + (offset >> PAGE_SHIFT); 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/** 668c2ecf20Sopenharmony_ci * psb_gtt_insert - put an object into the GTT 678c2ecf20Sopenharmony_ci * @dev: our DRM device 688c2ecf20Sopenharmony_ci * @r: our GTT range 698c2ecf20Sopenharmony_ci * @resume: on resume 708c2ecf20Sopenharmony_ci * 718c2ecf20Sopenharmony_ci * Take our preallocated GTT range and insert the GEM object into 728c2ecf20Sopenharmony_ci * the GTT. This is protected via the gtt mutex which the caller 738c2ecf20Sopenharmony_ci * must hold. 748c2ecf20Sopenharmony_ci */ 758c2ecf20Sopenharmony_cistatic int psb_gtt_insert(struct drm_device *dev, struct gtt_range *r, 768c2ecf20Sopenharmony_ci int resume) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci u32 __iomem *gtt_slot; 798c2ecf20Sopenharmony_ci u32 pte; 808c2ecf20Sopenharmony_ci struct page **pages; 818c2ecf20Sopenharmony_ci int i; 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ci if (r->pages == NULL) { 848c2ecf20Sopenharmony_ci WARN_ON(1); 858c2ecf20Sopenharmony_ci return -EINVAL; 868c2ecf20Sopenharmony_ci } 878c2ecf20Sopenharmony_ci 888c2ecf20Sopenharmony_ci WARN_ON(r->stolen); /* refcount these maybe ? */ 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci gtt_slot = psb_gtt_entry(dev, r); 918c2ecf20Sopenharmony_ci pages = r->pages; 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci if (!resume) { 948c2ecf20Sopenharmony_ci /* Make sure changes are visible to the GPU */ 958c2ecf20Sopenharmony_ci set_pages_array_wc(pages, r->npage); 968c2ecf20Sopenharmony_ci } 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_ci /* Write our page entries into the GTT itself */ 998c2ecf20Sopenharmony_ci for (i = r->roll; i < r->npage; i++) { 1008c2ecf20Sopenharmony_ci pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 1018c2ecf20Sopenharmony_ci PSB_MMU_CACHED_MEMORY); 1028c2ecf20Sopenharmony_ci iowrite32(pte, gtt_slot++); 1038c2ecf20Sopenharmony_ci } 1048c2ecf20Sopenharmony_ci for (i = 0; i < r->roll; i++) { 1058c2ecf20Sopenharmony_ci pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 1068c2ecf20Sopenharmony_ci PSB_MMU_CACHED_MEMORY); 1078c2ecf20Sopenharmony_ci iowrite32(pte, gtt_slot++); 1088c2ecf20Sopenharmony_ci } 1098c2ecf20Sopenharmony_ci /* Make sure all the entries are set before we return */ 1108c2ecf20Sopenharmony_ci ioread32(gtt_slot - 1); 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci return 0; 1138c2ecf20Sopenharmony_ci} 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci/** 1168c2ecf20Sopenharmony_ci * psb_gtt_remove - remove an object from the GTT 1178c2ecf20Sopenharmony_ci * @dev: our DRM device 1188c2ecf20Sopenharmony_ci * @r: our GTT range 1198c2ecf20Sopenharmony_ci * 1208c2ecf20Sopenharmony_ci * Remove a preallocated GTT range from the GTT. Overwrite all the 1218c2ecf20Sopenharmony_ci * page table entries with the dummy page. This is protected via the gtt 1228c2ecf20Sopenharmony_ci * mutex which the caller must hold. 1238c2ecf20Sopenharmony_ci */ 1248c2ecf20Sopenharmony_cistatic void psb_gtt_remove(struct drm_device *dev, struct gtt_range *r) 1258c2ecf20Sopenharmony_ci{ 1268c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 1278c2ecf20Sopenharmony_ci u32 __iomem *gtt_slot; 1288c2ecf20Sopenharmony_ci u32 pte; 1298c2ecf20Sopenharmony_ci int i; 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci WARN_ON(r->stolen); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci gtt_slot = psb_gtt_entry(dev, r); 1348c2ecf20Sopenharmony_ci pte = psb_gtt_mask_pte(page_to_pfn(dev_priv->scratch_page), 1358c2ecf20Sopenharmony_ci PSB_MMU_CACHED_MEMORY); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci for (i = 0; i < r->npage; i++) 1388c2ecf20Sopenharmony_ci iowrite32(pte, gtt_slot++); 1398c2ecf20Sopenharmony_ci ioread32(gtt_slot - 1); 1408c2ecf20Sopenharmony_ci set_pages_array_wb(r->pages, r->npage); 1418c2ecf20Sopenharmony_ci} 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci/** 1448c2ecf20Sopenharmony_ci * psb_gtt_roll - set scrolling position 1458c2ecf20Sopenharmony_ci * @dev: our DRM device 1468c2ecf20Sopenharmony_ci * @r: the gtt mapping we are using 1478c2ecf20Sopenharmony_ci * @roll: roll offset 1488c2ecf20Sopenharmony_ci * 1498c2ecf20Sopenharmony_ci * Roll an existing pinned mapping by moving the pages through the GTT. 1508c2ecf20Sopenharmony_ci * This allows us to implement hardware scrolling on the consoles without 1518c2ecf20Sopenharmony_ci * a 2D engine 1528c2ecf20Sopenharmony_ci */ 1538c2ecf20Sopenharmony_civoid psb_gtt_roll(struct drm_device *dev, struct gtt_range *r, int roll) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci u32 __iomem *gtt_slot; 1568c2ecf20Sopenharmony_ci u32 pte; 1578c2ecf20Sopenharmony_ci int i; 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci if (roll >= r->npage) { 1608c2ecf20Sopenharmony_ci WARN_ON(1); 1618c2ecf20Sopenharmony_ci return; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci r->roll = roll; 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci /* Not currently in the GTT - no worry we will write the mapping at 1678c2ecf20Sopenharmony_ci the right position when it gets pinned */ 1688c2ecf20Sopenharmony_ci if (!r->stolen && !r->in_gart) 1698c2ecf20Sopenharmony_ci return; 1708c2ecf20Sopenharmony_ci 1718c2ecf20Sopenharmony_ci gtt_slot = psb_gtt_entry(dev, r); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci for (i = r->roll; i < r->npage; i++) { 1748c2ecf20Sopenharmony_ci pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 1758c2ecf20Sopenharmony_ci PSB_MMU_CACHED_MEMORY); 1768c2ecf20Sopenharmony_ci iowrite32(pte, gtt_slot++); 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci for (i = 0; i < r->roll; i++) { 1798c2ecf20Sopenharmony_ci pte = psb_gtt_mask_pte(page_to_pfn(r->pages[i]), 1808c2ecf20Sopenharmony_ci PSB_MMU_CACHED_MEMORY); 1818c2ecf20Sopenharmony_ci iowrite32(pte, gtt_slot++); 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci ioread32(gtt_slot - 1); 1848c2ecf20Sopenharmony_ci} 1858c2ecf20Sopenharmony_ci 1868c2ecf20Sopenharmony_ci/** 1878c2ecf20Sopenharmony_ci * psb_gtt_attach_pages - attach and pin GEM pages 1888c2ecf20Sopenharmony_ci * @gt: the gtt range 1898c2ecf20Sopenharmony_ci * 1908c2ecf20Sopenharmony_ci * Pin and build an in kernel list of the pages that back our GEM object. 1918c2ecf20Sopenharmony_ci * While we hold this the pages cannot be swapped out. This is protected 1928c2ecf20Sopenharmony_ci * via the gtt mutex which the caller must hold. 1938c2ecf20Sopenharmony_ci */ 1948c2ecf20Sopenharmony_cistatic int psb_gtt_attach_pages(struct gtt_range *gt) 1958c2ecf20Sopenharmony_ci{ 1968c2ecf20Sopenharmony_ci struct page **pages; 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci WARN_ON(gt->pages); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci pages = drm_gem_get_pages(>->gem); 2018c2ecf20Sopenharmony_ci if (IS_ERR(pages)) 2028c2ecf20Sopenharmony_ci return PTR_ERR(pages); 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci gt->npage = gt->gem.size / PAGE_SIZE; 2058c2ecf20Sopenharmony_ci gt->pages = pages; 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci return 0; 2088c2ecf20Sopenharmony_ci} 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci/** 2118c2ecf20Sopenharmony_ci * psb_gtt_detach_pages - attach and pin GEM pages 2128c2ecf20Sopenharmony_ci * @gt: the gtt range 2138c2ecf20Sopenharmony_ci * 2148c2ecf20Sopenharmony_ci * Undo the effect of psb_gtt_attach_pages. At this point the pages 2158c2ecf20Sopenharmony_ci * must have been removed from the GTT as they could now be paged out 2168c2ecf20Sopenharmony_ci * and move bus address. This is protected via the gtt mutex which the 2178c2ecf20Sopenharmony_ci * caller must hold. 2188c2ecf20Sopenharmony_ci */ 2198c2ecf20Sopenharmony_cistatic void psb_gtt_detach_pages(struct gtt_range *gt) 2208c2ecf20Sopenharmony_ci{ 2218c2ecf20Sopenharmony_ci drm_gem_put_pages(>->gem, gt->pages, true, false); 2228c2ecf20Sopenharmony_ci gt->pages = NULL; 2238c2ecf20Sopenharmony_ci} 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci/** 2268c2ecf20Sopenharmony_ci * psb_gtt_pin - pin pages into the GTT 2278c2ecf20Sopenharmony_ci * @gt: range to pin 2288c2ecf20Sopenharmony_ci * 2298c2ecf20Sopenharmony_ci * Pin a set of pages into the GTT. The pins are refcounted so that 2308c2ecf20Sopenharmony_ci * multiple pins need multiple unpins to undo. 2318c2ecf20Sopenharmony_ci * 2328c2ecf20Sopenharmony_ci * Non GEM backed objects treat this as a no-op as they are always GTT 2338c2ecf20Sopenharmony_ci * backed objects. 2348c2ecf20Sopenharmony_ci */ 2358c2ecf20Sopenharmony_ciint psb_gtt_pin(struct gtt_range *gt) 2368c2ecf20Sopenharmony_ci{ 2378c2ecf20Sopenharmony_ci int ret = 0; 2388c2ecf20Sopenharmony_ci struct drm_device *dev = gt->gem.dev; 2398c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 2408c2ecf20Sopenharmony_ci u32 gpu_base = dev_priv->gtt.gatt_start; 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_ci mutex_lock(&dev_priv->gtt_mutex); 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci if (gt->in_gart == 0 && gt->stolen == 0) { 2458c2ecf20Sopenharmony_ci ret = psb_gtt_attach_pages(gt); 2468c2ecf20Sopenharmony_ci if (ret < 0) 2478c2ecf20Sopenharmony_ci goto out; 2488c2ecf20Sopenharmony_ci ret = psb_gtt_insert(dev, gt, 0); 2498c2ecf20Sopenharmony_ci if (ret < 0) { 2508c2ecf20Sopenharmony_ci psb_gtt_detach_pages(gt); 2518c2ecf20Sopenharmony_ci goto out; 2528c2ecf20Sopenharmony_ci } 2538c2ecf20Sopenharmony_ci psb_mmu_insert_pages(psb_mmu_get_default_pd(dev_priv->mmu), 2548c2ecf20Sopenharmony_ci gt->pages, (gpu_base + gt->offset), 2558c2ecf20Sopenharmony_ci gt->npage, 0, 0, PSB_MMU_CACHED_MEMORY); 2568c2ecf20Sopenharmony_ci } 2578c2ecf20Sopenharmony_ci gt->in_gart++; 2588c2ecf20Sopenharmony_ciout: 2598c2ecf20Sopenharmony_ci mutex_unlock(&dev_priv->gtt_mutex); 2608c2ecf20Sopenharmony_ci return ret; 2618c2ecf20Sopenharmony_ci} 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_ci/** 2648c2ecf20Sopenharmony_ci * psb_gtt_unpin - Drop a GTT pin requirement 2658c2ecf20Sopenharmony_ci * @gt: range to pin 2668c2ecf20Sopenharmony_ci * 2678c2ecf20Sopenharmony_ci * Undoes the effect of psb_gtt_pin. On the last drop the GEM object 2688c2ecf20Sopenharmony_ci * will be removed from the GTT which will also drop the page references 2698c2ecf20Sopenharmony_ci * and allow the VM to clean up or page stuff. 2708c2ecf20Sopenharmony_ci * 2718c2ecf20Sopenharmony_ci * Non GEM backed objects treat this as a no-op as they are always GTT 2728c2ecf20Sopenharmony_ci * backed objects. 2738c2ecf20Sopenharmony_ci */ 2748c2ecf20Sopenharmony_civoid psb_gtt_unpin(struct gtt_range *gt) 2758c2ecf20Sopenharmony_ci{ 2768c2ecf20Sopenharmony_ci struct drm_device *dev = gt->gem.dev; 2778c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 2788c2ecf20Sopenharmony_ci u32 gpu_base = dev_priv->gtt.gatt_start; 2798c2ecf20Sopenharmony_ci int ret; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci /* While holding the gtt_mutex no new blits can be initiated */ 2828c2ecf20Sopenharmony_ci mutex_lock(&dev_priv->gtt_mutex); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci /* Wait for any possible usage of the memory to be finished */ 2858c2ecf20Sopenharmony_ci ret = gma_blt_wait_idle(dev_priv); 2868c2ecf20Sopenharmony_ci if (ret) { 2878c2ecf20Sopenharmony_ci DRM_ERROR("Failed to idle the blitter, unpin failed!"); 2888c2ecf20Sopenharmony_ci goto out; 2898c2ecf20Sopenharmony_ci } 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci WARN_ON(!gt->in_gart); 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci gt->in_gart--; 2948c2ecf20Sopenharmony_ci if (gt->in_gart == 0 && gt->stolen == 0) { 2958c2ecf20Sopenharmony_ci psb_mmu_remove_pages(psb_mmu_get_default_pd(dev_priv->mmu), 2968c2ecf20Sopenharmony_ci (gpu_base + gt->offset), gt->npage, 0, 0); 2978c2ecf20Sopenharmony_ci psb_gtt_remove(dev, gt); 2988c2ecf20Sopenharmony_ci psb_gtt_detach_pages(gt); 2998c2ecf20Sopenharmony_ci } 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ciout: 3028c2ecf20Sopenharmony_ci mutex_unlock(&dev_priv->gtt_mutex); 3038c2ecf20Sopenharmony_ci} 3048c2ecf20Sopenharmony_ci 3058c2ecf20Sopenharmony_ci/* 3068c2ecf20Sopenharmony_ci * GTT resource allocator - allocate and manage GTT address space 3078c2ecf20Sopenharmony_ci */ 3088c2ecf20Sopenharmony_ci 3098c2ecf20Sopenharmony_ci/** 3108c2ecf20Sopenharmony_ci * psb_gtt_alloc_range - allocate GTT address space 3118c2ecf20Sopenharmony_ci * @dev: Our DRM device 3128c2ecf20Sopenharmony_ci * @len: length (bytes) of address space required 3138c2ecf20Sopenharmony_ci * @name: resource name 3148c2ecf20Sopenharmony_ci * @backed: resource should be backed by stolen pages 3158c2ecf20Sopenharmony_ci * @align: requested alignment 3168c2ecf20Sopenharmony_ci * 3178c2ecf20Sopenharmony_ci * Ask the kernel core to find us a suitable range of addresses 3188c2ecf20Sopenharmony_ci * to use for a GTT mapping. 3198c2ecf20Sopenharmony_ci * 3208c2ecf20Sopenharmony_ci * Returns a gtt_range structure describing the object, or NULL on 3218c2ecf20Sopenharmony_ci * error. On successful return the resource is both allocated and marked 3228c2ecf20Sopenharmony_ci * as in use. 3238c2ecf20Sopenharmony_ci */ 3248c2ecf20Sopenharmony_cistruct gtt_range *psb_gtt_alloc_range(struct drm_device *dev, int len, 3258c2ecf20Sopenharmony_ci const char *name, int backed, u32 align) 3268c2ecf20Sopenharmony_ci{ 3278c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 3288c2ecf20Sopenharmony_ci struct gtt_range *gt; 3298c2ecf20Sopenharmony_ci struct resource *r = dev_priv->gtt_mem; 3308c2ecf20Sopenharmony_ci int ret; 3318c2ecf20Sopenharmony_ci unsigned long start, end; 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_ci if (backed) { 3348c2ecf20Sopenharmony_ci /* The start of the GTT is the stolen pages */ 3358c2ecf20Sopenharmony_ci start = r->start; 3368c2ecf20Sopenharmony_ci end = r->start + dev_priv->gtt.stolen_size - 1; 3378c2ecf20Sopenharmony_ci } else { 3388c2ecf20Sopenharmony_ci /* The rest we will use for GEM backed objects */ 3398c2ecf20Sopenharmony_ci start = r->start + dev_priv->gtt.stolen_size; 3408c2ecf20Sopenharmony_ci end = r->end; 3418c2ecf20Sopenharmony_ci } 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci gt = kzalloc(sizeof(struct gtt_range), GFP_KERNEL); 3448c2ecf20Sopenharmony_ci if (gt == NULL) 3458c2ecf20Sopenharmony_ci return NULL; 3468c2ecf20Sopenharmony_ci gt->resource.name = name; 3478c2ecf20Sopenharmony_ci gt->stolen = backed; 3488c2ecf20Sopenharmony_ci gt->in_gart = backed; 3498c2ecf20Sopenharmony_ci gt->roll = 0; 3508c2ecf20Sopenharmony_ci /* Ensure this is set for non GEM objects */ 3518c2ecf20Sopenharmony_ci gt->gem.dev = dev; 3528c2ecf20Sopenharmony_ci ret = allocate_resource(dev_priv->gtt_mem, >->resource, 3538c2ecf20Sopenharmony_ci len, start, end, align, NULL, NULL); 3548c2ecf20Sopenharmony_ci if (ret == 0) { 3558c2ecf20Sopenharmony_ci gt->offset = gt->resource.start - r->start; 3568c2ecf20Sopenharmony_ci return gt; 3578c2ecf20Sopenharmony_ci } 3588c2ecf20Sopenharmony_ci kfree(gt); 3598c2ecf20Sopenharmony_ci return NULL; 3608c2ecf20Sopenharmony_ci} 3618c2ecf20Sopenharmony_ci 3628c2ecf20Sopenharmony_ci/** 3638c2ecf20Sopenharmony_ci * psb_gtt_free_range - release GTT address space 3648c2ecf20Sopenharmony_ci * @dev: our DRM device 3658c2ecf20Sopenharmony_ci * @gt: a mapping created with psb_gtt_alloc_range 3668c2ecf20Sopenharmony_ci * 3678c2ecf20Sopenharmony_ci * Release a resource that was allocated with psb_gtt_alloc_range. If the 3688c2ecf20Sopenharmony_ci * object has been pinned by mmap users we clean this up here currently. 3698c2ecf20Sopenharmony_ci */ 3708c2ecf20Sopenharmony_civoid psb_gtt_free_range(struct drm_device *dev, struct gtt_range *gt) 3718c2ecf20Sopenharmony_ci{ 3728c2ecf20Sopenharmony_ci /* Undo the mmap pin if we are destroying the object */ 3738c2ecf20Sopenharmony_ci if (gt->mmapping) { 3748c2ecf20Sopenharmony_ci psb_gtt_unpin(gt); 3758c2ecf20Sopenharmony_ci gt->mmapping = 0; 3768c2ecf20Sopenharmony_ci } 3778c2ecf20Sopenharmony_ci WARN_ON(gt->in_gart && !gt->stolen); 3788c2ecf20Sopenharmony_ci release_resource(>->resource); 3798c2ecf20Sopenharmony_ci kfree(gt); 3808c2ecf20Sopenharmony_ci} 3818c2ecf20Sopenharmony_ci 3828c2ecf20Sopenharmony_cistatic void psb_gtt_alloc(struct drm_device *dev) 3838c2ecf20Sopenharmony_ci{ 3848c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 3858c2ecf20Sopenharmony_ci init_rwsem(&dev_priv->gtt.sem); 3868c2ecf20Sopenharmony_ci} 3878c2ecf20Sopenharmony_ci 3888c2ecf20Sopenharmony_civoid psb_gtt_takedown(struct drm_device *dev) 3898c2ecf20Sopenharmony_ci{ 3908c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci if (dev_priv->gtt_map) { 3938c2ecf20Sopenharmony_ci iounmap(dev_priv->gtt_map); 3948c2ecf20Sopenharmony_ci dev_priv->gtt_map = NULL; 3958c2ecf20Sopenharmony_ci } 3968c2ecf20Sopenharmony_ci if (dev_priv->gtt_initialized) { 3978c2ecf20Sopenharmony_ci pci_write_config_word(dev->pdev, PSB_GMCH_CTRL, 3988c2ecf20Sopenharmony_ci dev_priv->gmch_ctrl); 3998c2ecf20Sopenharmony_ci PSB_WVDC32(dev_priv->pge_ctl, PSB_PGETBL_CTL); 4008c2ecf20Sopenharmony_ci (void) PSB_RVDC32(PSB_PGETBL_CTL); 4018c2ecf20Sopenharmony_ci } 4028c2ecf20Sopenharmony_ci if (dev_priv->vram_addr) 4038c2ecf20Sopenharmony_ci iounmap(dev_priv->gtt_map); 4048c2ecf20Sopenharmony_ci} 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ciint psb_gtt_init(struct drm_device *dev, int resume) 4078c2ecf20Sopenharmony_ci{ 4088c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 4098c2ecf20Sopenharmony_ci unsigned gtt_pages; 4108c2ecf20Sopenharmony_ci unsigned long stolen_size, vram_stolen_size; 4118c2ecf20Sopenharmony_ci unsigned i, num_pages; 4128c2ecf20Sopenharmony_ci unsigned pfn_base; 4138c2ecf20Sopenharmony_ci struct psb_gtt *pg; 4148c2ecf20Sopenharmony_ci 4158c2ecf20Sopenharmony_ci int ret = 0; 4168c2ecf20Sopenharmony_ci uint32_t pte; 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ci if (!resume) { 4198c2ecf20Sopenharmony_ci mutex_init(&dev_priv->gtt_mutex); 4208c2ecf20Sopenharmony_ci mutex_init(&dev_priv->mmap_mutex); 4218c2ecf20Sopenharmony_ci psb_gtt_alloc(dev); 4228c2ecf20Sopenharmony_ci } 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci pg = &dev_priv->gtt; 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci /* Enable the GTT */ 4278c2ecf20Sopenharmony_ci pci_read_config_word(dev->pdev, PSB_GMCH_CTRL, &dev_priv->gmch_ctrl); 4288c2ecf20Sopenharmony_ci pci_write_config_word(dev->pdev, PSB_GMCH_CTRL, 4298c2ecf20Sopenharmony_ci dev_priv->gmch_ctrl | _PSB_GMCH_ENABLED); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci dev_priv->pge_ctl = PSB_RVDC32(PSB_PGETBL_CTL); 4328c2ecf20Sopenharmony_ci PSB_WVDC32(dev_priv->pge_ctl | _PSB_PGETBL_ENABLED, PSB_PGETBL_CTL); 4338c2ecf20Sopenharmony_ci (void) PSB_RVDC32(PSB_PGETBL_CTL); 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci /* The root resource we allocate address space from */ 4368c2ecf20Sopenharmony_ci dev_priv->gtt_initialized = 1; 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci pg->gtt_phys_start = dev_priv->pge_ctl & PAGE_MASK; 4398c2ecf20Sopenharmony_ci 4408c2ecf20Sopenharmony_ci /* 4418c2ecf20Sopenharmony_ci * The video mmu has a hw bug when accessing 0x0D0000000. 4428c2ecf20Sopenharmony_ci * Make gatt start at 0x0e000,0000. This doesn't actually 4438c2ecf20Sopenharmony_ci * matter for us but may do if the video acceleration ever 4448c2ecf20Sopenharmony_ci * gets opened up. 4458c2ecf20Sopenharmony_ci */ 4468c2ecf20Sopenharmony_ci pg->mmu_gatt_start = 0xE0000000; 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci pg->gtt_start = pci_resource_start(dev->pdev, PSB_GTT_RESOURCE); 4498c2ecf20Sopenharmony_ci gtt_pages = pci_resource_len(dev->pdev, PSB_GTT_RESOURCE) 4508c2ecf20Sopenharmony_ci >> PAGE_SHIFT; 4518c2ecf20Sopenharmony_ci /* CDV doesn't report this. In which case the system has 64 gtt pages */ 4528c2ecf20Sopenharmony_ci if (pg->gtt_start == 0 || gtt_pages == 0) { 4538c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "GTT PCI BAR not initialized.\n"); 4548c2ecf20Sopenharmony_ci gtt_pages = 64; 4558c2ecf20Sopenharmony_ci pg->gtt_start = dev_priv->pge_ctl; 4568c2ecf20Sopenharmony_ci } 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_ci pg->gatt_start = pci_resource_start(dev->pdev, PSB_GATT_RESOURCE); 4598c2ecf20Sopenharmony_ci pg->gatt_pages = pci_resource_len(dev->pdev, PSB_GATT_RESOURCE) 4608c2ecf20Sopenharmony_ci >> PAGE_SHIFT; 4618c2ecf20Sopenharmony_ci dev_priv->gtt_mem = &dev->pdev->resource[PSB_GATT_RESOURCE]; 4628c2ecf20Sopenharmony_ci 4638c2ecf20Sopenharmony_ci if (pg->gatt_pages == 0 || pg->gatt_start == 0) { 4648c2ecf20Sopenharmony_ci static struct resource fudge; /* Preferably peppermint */ 4658c2ecf20Sopenharmony_ci /* This can occur on CDV systems. Fudge it in this case. 4668c2ecf20Sopenharmony_ci We really don't care what imaginary space is being allocated 4678c2ecf20Sopenharmony_ci at this point */ 4688c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "GATT PCI BAR not initialized.\n"); 4698c2ecf20Sopenharmony_ci pg->gatt_start = 0x40000000; 4708c2ecf20Sopenharmony_ci pg->gatt_pages = (128 * 1024 * 1024) >> PAGE_SHIFT; 4718c2ecf20Sopenharmony_ci /* This is a little confusing but in fact the GTT is providing 4728c2ecf20Sopenharmony_ci a view from the GPU into memory and not vice versa. As such 4738c2ecf20Sopenharmony_ci this is really allocating space that is not the same as the 4748c2ecf20Sopenharmony_ci CPU address space on CDV */ 4758c2ecf20Sopenharmony_ci fudge.start = 0x40000000; 4768c2ecf20Sopenharmony_ci fudge.end = 0x40000000 + 128 * 1024 * 1024 - 1; 4778c2ecf20Sopenharmony_ci fudge.name = "fudge"; 4788c2ecf20Sopenharmony_ci fudge.flags = IORESOURCE_MEM; 4798c2ecf20Sopenharmony_ci dev_priv->gtt_mem = &fudge; 4808c2ecf20Sopenharmony_ci } 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ci pci_read_config_dword(dev->pdev, PSB_BSM, &dev_priv->stolen_base); 4838c2ecf20Sopenharmony_ci vram_stolen_size = pg->gtt_phys_start - dev_priv->stolen_base 4848c2ecf20Sopenharmony_ci - PAGE_SIZE; 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci stolen_size = vram_stolen_size; 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "Stolen memory base 0x%x, size %luK\n", 4898c2ecf20Sopenharmony_ci dev_priv->stolen_base, vram_stolen_size / 1024); 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci if (resume && (gtt_pages != pg->gtt_pages) && 4928c2ecf20Sopenharmony_ci (stolen_size != pg->stolen_size)) { 4938c2ecf20Sopenharmony_ci dev_err(dev->dev, "GTT resume error.\n"); 4948c2ecf20Sopenharmony_ci ret = -EINVAL; 4958c2ecf20Sopenharmony_ci goto out_err; 4968c2ecf20Sopenharmony_ci } 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci pg->gtt_pages = gtt_pages; 4998c2ecf20Sopenharmony_ci pg->stolen_size = stolen_size; 5008c2ecf20Sopenharmony_ci dev_priv->vram_stolen_size = vram_stolen_size; 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci /* 5038c2ecf20Sopenharmony_ci * Map the GTT and the stolen memory area 5048c2ecf20Sopenharmony_ci */ 5058c2ecf20Sopenharmony_ci if (!resume) 5068c2ecf20Sopenharmony_ci dev_priv->gtt_map = ioremap(pg->gtt_phys_start, 5078c2ecf20Sopenharmony_ci gtt_pages << PAGE_SHIFT); 5088c2ecf20Sopenharmony_ci if (!dev_priv->gtt_map) { 5098c2ecf20Sopenharmony_ci dev_err(dev->dev, "Failure to map gtt.\n"); 5108c2ecf20Sopenharmony_ci ret = -ENOMEM; 5118c2ecf20Sopenharmony_ci goto out_err; 5128c2ecf20Sopenharmony_ci } 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci if (!resume) 5158c2ecf20Sopenharmony_ci dev_priv->vram_addr = ioremap_wc(dev_priv->stolen_base, 5168c2ecf20Sopenharmony_ci stolen_size); 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci if (!dev_priv->vram_addr) { 5198c2ecf20Sopenharmony_ci dev_err(dev->dev, "Failure to map stolen base.\n"); 5208c2ecf20Sopenharmony_ci ret = -ENOMEM; 5218c2ecf20Sopenharmony_ci goto out_err; 5228c2ecf20Sopenharmony_ci } 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci /* 5258c2ecf20Sopenharmony_ci * Insert vram stolen pages into the GTT 5268c2ecf20Sopenharmony_ci */ 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci pfn_base = dev_priv->stolen_base >> PAGE_SHIFT; 5298c2ecf20Sopenharmony_ci num_pages = vram_stolen_size >> PAGE_SHIFT; 5308c2ecf20Sopenharmony_ci dev_dbg(dev->dev, "Set up %d stolen pages starting at 0x%08x, GTT offset %dK\n", 5318c2ecf20Sopenharmony_ci num_pages, pfn_base << PAGE_SHIFT, 0); 5328c2ecf20Sopenharmony_ci for (i = 0; i < num_pages; ++i) { 5338c2ecf20Sopenharmony_ci pte = psb_gtt_mask_pte(pfn_base + i, PSB_MMU_CACHED_MEMORY); 5348c2ecf20Sopenharmony_ci iowrite32(pte, dev_priv->gtt_map + i); 5358c2ecf20Sopenharmony_ci } 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci /* 5388c2ecf20Sopenharmony_ci * Init rest of GTT to the scratch page to avoid accidents or scribbles 5398c2ecf20Sopenharmony_ci */ 5408c2ecf20Sopenharmony_ci 5418c2ecf20Sopenharmony_ci pfn_base = page_to_pfn(dev_priv->scratch_page); 5428c2ecf20Sopenharmony_ci pte = psb_gtt_mask_pte(pfn_base, PSB_MMU_CACHED_MEMORY); 5438c2ecf20Sopenharmony_ci for (; i < gtt_pages; ++i) 5448c2ecf20Sopenharmony_ci iowrite32(pte, dev_priv->gtt_map + i); 5458c2ecf20Sopenharmony_ci 5468c2ecf20Sopenharmony_ci (void) ioread32(dev_priv->gtt_map + i - 1); 5478c2ecf20Sopenharmony_ci return 0; 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ciout_err: 5508c2ecf20Sopenharmony_ci psb_gtt_takedown(dev); 5518c2ecf20Sopenharmony_ci return ret; 5528c2ecf20Sopenharmony_ci} 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ciint psb_gtt_restore(struct drm_device *dev) 5558c2ecf20Sopenharmony_ci{ 5568c2ecf20Sopenharmony_ci struct drm_psb_private *dev_priv = dev->dev_private; 5578c2ecf20Sopenharmony_ci struct resource *r = dev_priv->gtt_mem->child; 5588c2ecf20Sopenharmony_ci struct gtt_range *range; 5598c2ecf20Sopenharmony_ci unsigned int restored = 0, total = 0, size = 0; 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci /* On resume, the gtt_mutex is already initialized */ 5628c2ecf20Sopenharmony_ci mutex_lock(&dev_priv->gtt_mutex); 5638c2ecf20Sopenharmony_ci psb_gtt_init(dev, 1); 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci while (r != NULL) { 5668c2ecf20Sopenharmony_ci range = container_of(r, struct gtt_range, resource); 5678c2ecf20Sopenharmony_ci if (range->pages) { 5688c2ecf20Sopenharmony_ci psb_gtt_insert(dev, range, 1); 5698c2ecf20Sopenharmony_ci size += range->resource.end - range->resource.start; 5708c2ecf20Sopenharmony_ci restored++; 5718c2ecf20Sopenharmony_ci } 5728c2ecf20Sopenharmony_ci r = r->sibling; 5738c2ecf20Sopenharmony_ci total++; 5748c2ecf20Sopenharmony_ci } 5758c2ecf20Sopenharmony_ci mutex_unlock(&dev_priv->gtt_mutex); 5768c2ecf20Sopenharmony_ci DRM_DEBUG_DRIVER("Restored %u of %u gtt ranges (%u KB)", restored, 5778c2ecf20Sopenharmony_ci total, (size / 1024)); 5788c2ecf20Sopenharmony_ci 5798c2ecf20Sopenharmony_ci return 0; 5808c2ecf20Sopenharmony_ci} 581