1450N/A/*
1450N/A * Copyright © 2008-2013 Intel Corporation
1450N/A *
1450N/A * Permission is hereby granted, free of charge, to any person obtaining a
1450N/A * copy of this software and associated documentation files (the "Software"),
1450N/A * to deal in the Software without restriction, including without limitation
1450N/A * the rights to use, copy, modify, merge, publish, distribute, sublicense,
1450N/A * and/or sell copies of the Software, and to permit persons to whom the
1450N/A * Software is furnished to do so, subject to the following conditions:
1450N/A *
1450N/A * The above copyright notice and this permission notice (including the next
1450N/A * paragraph) shall be included in all copies or substantial portions of the
1450N/A * Software.
1450N/A *
1450N/A * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1450N/A * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1450N/A * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1450N/A * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1450N/A * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
1450N/A * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1450N/A * IN THE SOFTWARE.
1450N/A *
1450N/A * Authors:
1450N/A * Eric Anholt <eric@anholt.net>
1450N/A * Chris Wilson <chris@chris-wilson.co.uk>
1450N/A *
1450N/A */
1450N/A
1450N/A/*
1450N/A * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
1450N/A */
1450N/A
1450N/A#include "drmP.h"
1450N/A#include "i915_drm.h"
1450N/A#include "i915_drv.h"
1450N/A
1450N/A/*
1450N/A * The BIOS typically reserves some of the system's memory for the exclusive
1450N/A * use of the integrated graphics. This memory is no longer available for
1450N/A * use by the OS and so the user finds that his system has less memory
1450N/A * available than he put in. We refer to this memory as stolen.
1450N/A *
1450N/A * The BIOS will allocate its framebuffer from the stolen memory. Our
1450N/A * goal is try to reuse that object for our own fbcon which must always
1450N/A * be available for panics. Anything else we can reuse the stolen memory
1450N/A * for is a boon.
1450N/A */
1450N/A
1450N/Astatic unsigned long i915_stolen_to_physical(struct drm_device *dev)
1450N/A{
1450N/A struct pci_dev *pdev = dev->pdev;
1450N/A u32 base;
1450N/A
1450N/A /* On the machines I have tested the Graphics Base of Stolen Memory
1450N/A * is unreliable, so on those compute the base by subtracting the
1450N/A * stolen memory from the Top of Low Usable DRAM which is where the
1450N/A * BIOS places the graphics stolen memory.
1450N/A *
1450N/A * On gen2, the layout is slightly different with the Graphics Segment
1450N/A * immediately following Top of Memory (or Top of Usable DRAM). Note
1450N/A * it appears that TOUD is only reported by 865g, so we just use the
1450N/A * top of memory as determined by the e820 probe.
1450N/A *
1450N/A * XXX gen2 requires an unavailable symbol and 945gm fails with
1450N/A * its value of TOLUD.
1450N/A */
1450N/A base = 0;
1450N/A if (IS_VALLEYVIEW(dev)) {
1450N/A pci_read_config_dword(dev->pdev, 0x5c, &base);
1450N/A base &= ~((1<<20) - 1);
1450N/A } else if (INTEL_INFO(dev)->gen >= 6) {
1450N/A /* Read Base Data of Stolen Memory Register (BDSM) directly.
1450N/A * Note that there is also a MCHBAR miror at 0x1080c0 or
1450N/A * we could use device 2:0x5c instead.
1450N/A */
1450N/A pci_read_config_dword(pdev, 0xB0, &base);
1450N/A base &= ~4095; /* lower bits used for locking register */
1450N/A } else if (INTEL_INFO(dev)->gen > 3 || IS_G33(dev)) {
1450N/A /* Read Graphics Base of Stolen Memory directly */
1450N/A pci_read_config_dword(pdev, 0xA4, &base);
1450N/A#if 0
1450N/A } else if (IS_GEN3(dev)) {
1450N/A u8 val;
1450N/A /* Stolen is immediately below Top of Low Usable DRAM */
1450N/A pci_read_config_byte(pdev, 0x9c, &val);
1450N/A base = val >> 3 << 27;
1450N/A base -= dev_priv->mm.gtt->stolen_size;
1450N/A } else {
1450N/A /* Stolen is immediately above Top of Memory */
1450N/A base = max_low_pfn_mapped << PAGE_SHIFT;
1450N/A#endif
1450N/A }
1450N/A
1450N/A return base;
1450N/A}
1450N/A
1450N/Astatic int i915_setup_compression(struct drm_device *dev, int size)
1450N/A{
1450N/A struct drm_i915_private *dev_priv = dev->dev_private;
1450N/A struct drm_mm_node *compressed_fb, *compressed_llb;
1450N/A
1450N/A /* Try to over-allocate to reduce reallocations and fragmentation */
1450N/A compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen,
1450N/A size <<= 1, 4096, 0);
1450N/A if (!compressed_fb)
1450N/A compressed_fb = drm_mm_search_free(&dev_priv->mm.stolen,
1450N/A size >>= 1, 4096, 0);
1450N/A if (compressed_fb)
1450N/A compressed_fb = drm_mm_get_block(compressed_fb, size, 4096);
1450N/A if (!compressed_fb)
1450N/A goto err;
1450N/A
1450N/A if (HAS_PCH_SPLIT(dev))
1450N/A I915_WRITE(ILK_DPFC_CB_BASE, compressed_fb->start);
1450N/A else if (IS_GM45(dev)) {
1450N/A I915_WRITE(DPFC_CB_BASE, compressed_fb->start);
1450N/A } else {
1450N/A compressed_llb = drm_mm_search_free(&dev_priv->mm.stolen,
1450N/A 4096, 4096, 0);
1450N/A if (compressed_llb)
1450N/A compressed_llb = drm_mm_get_block(compressed_llb,
1450N/A 4096, 4096);
1450N/A if (!compressed_llb)
1450N/A goto err_fb;
1450N/A
1450N/A dev_priv->compressed_llb = compressed_llb;
1450N/A
1450N/A I915_WRITE(FBC_CFB_BASE,
1450N/A dev_priv->mm.stolen_base + compressed_fb->start);
1450N/A I915_WRITE(FBC_LL_BASE,
1450N/A dev_priv->mm.stolen_base + compressed_llb->start);
1450N/A }
1450N/A
1450N/A dev_priv->compressed_fb = compressed_fb;
1450N/A dev_priv->cfb_size = size;
1450N/A
1450N/A DRM_DEBUG_KMS("reserved %d bytes of contiguous stolen space for FBC\n",
1450N/A size);
1450N/A
1450N/A return 0;
1450N/A
1450N/Aerr_fb:
1450N/A drm_mm_put_block(compressed_fb);
1450N/Aerr:
1450N/A return -ENOSPC;
1450N/A}
1450N/A
1450N/Aint i915_gem_stolen_setup_compression(struct drm_device *dev, int size)
1450N/A{
1450N/A struct drm_i915_private *dev_priv = dev->dev_private;
1450N/A
1450N/A if (!drm_mm_initialized(&dev_priv->mm.stolen))
1450N/A return -ENODEV;
1450N/A
1450N/A if (size < dev_priv->cfb_size)
1450N/A return 0;
1450N/A
1450N/A /* Release any current block */
1450N/A i915_gem_stolen_cleanup_compression(dev);
1450N/A
1450N/A return i915_setup_compression(dev, size);
1450N/A}
1450N/A
1450N/Avoid i915_gem_stolen_cleanup_compression(struct drm_device *dev)
1450N/A{
1450N/A struct drm_i915_private *dev_priv = dev->dev_private;
1450N/A
1450N/A if (dev_priv->cfb_size == 0)
1450N/A return;
1450N/A
1450N/A if (dev_priv->compressed_fb)
1450N/A drm_mm_put_block(dev_priv->compressed_fb);
1450N/A
1450N/A if (dev_priv->compressed_llb)
1450N/A drm_mm_put_block(dev_priv->compressed_llb);
1450N/A
1450N/A dev_priv->cfb_size = 0;
1450N/A}
1450N/A
1450N/Avoid i915_gem_cleanup_stolen(struct drm_device *dev)
1450N/A{
1450N/A struct drm_i915_private *dev_priv = dev->dev_private;
1450N/A
1450N/A if (!drm_mm_initialized(&dev_priv->mm.stolen))
1450N/A return;
1450N/A
1450N/A i915_gem_stolen_cleanup_compression(dev);
1450N/A drm_mm_takedown(&dev_priv->mm.stolen);
1450N/A}
1450N/A
1450N/Aint i915_gem_init_stolen(struct drm_device *dev)
1450N/A{
1450N/A struct drm_i915_private *dev_priv = dev->dev_private;
1450N/A int bios_reserved = 0;
1450N/A
1450N/A dev_priv->mm.stolen_base = i915_stolen_to_physical(dev);
1450N/A if (dev_priv->mm.stolen_base == 0)
1450N/A return 0;
1450N/A
1450N/A DRM_DEBUG_KMS("found %zd bytes of stolen memory at %08lx\n",
1450N/A dev_priv->gtt.stolen_size, dev_priv->mm.stolen_base);
1450N/A
1450N/A if (IS_VALLEYVIEW(dev))
1450N/A bios_reserved = 1024*1024; /* top 1M on VLV/BYT */
1450N/A
1450N/A /* Basic memrange allocator for stolen space */
1450N/A drm_mm_init(&dev_priv->mm.stolen, 0, dev_priv->gtt.stolen_size -
1450N/A bios_reserved);
1450N/A
1450N/A return 0;
1450N/A}
1450N/A
1450N/A#if 0
1450N/Astatic struct sg_table *
1450N/Ai915_pages_create_for_stolen(struct drm_device *dev,
1450N/A u32 offset, u32 size)
1450N/A{
1450N/A struct drm_i915_private *dev_priv = dev->dev_private;
1450N/A caddr_t *page_list;
1450N/A
1450N/A DRM_DEBUG_DRIVER("offset=0x%x, size=%d\n", offset, size);
1450N/A BUG_ON(offset > dev_priv->gtt.stolen_size - size);
1450N/A
1450N/A /* We hide that we have no struct page backing our stolen object
1450N/A * by wrapping the contiguous physical allocation with a fake
1450N/A * dma mapping in a single scatterlist.
1450N/A */
1450N/A
1450N/A page_list = kmem_zalloc(btop(size) * sizeof(caddr_t), KM_SLEEP);
1450N/A st = kmalloc(sizeof(*st), GFP_KERNEL);
1450N/A if (st == NULL)
1450N/A return NULL;
1450N/A
1450N/A if (sg_alloc_table(st, 1, GFP_KERNEL)) {
1450N/A kfree(st);
1450N/A return NULL;
1450N/A }
1450N/A
1450N/A sg = st->sgl;
1450N/A sg->offset = offset;
1450N/A sg->length = size;
1450N/A
1450N/A sg_dma_address(sg) = (dma_addr_t)dev_priv->mm.stolen_base + offset;
1450N/A sg_dma_len(sg) = size;
1450N/A
1450N/A return st;
1450N/A}
1450N/A#endif
1450N/A
1450N/Astatic int i915_gem_object_get_pages_stolen(struct drm_i915_gem_object *obj)
1450N/A{
1450N/A BUG();
1450N/A return -EINVAL;
1450N/A}
1450N/A
1450N/Astatic void i915_gem_object_put_pages_stolen(struct drm_i915_gem_object *obj)
1450N/A{
1450N/A /* Should only be called during free */
1450N/A}
1450N/A
1450N/Astatic const struct drm_i915_gem_object_ops i915_gem_object_stolen_ops = {
1450N/A .get_pages = i915_gem_object_get_pages_stolen,
1450N/A .put_pages = i915_gem_object_put_pages_stolen,
1450N/A};
1450N/A
1450N/A
1450N/Astatic struct drm_i915_gem_object *
1450N/A_i915_gem_object_create_stolen(struct drm_device *dev,
1450N/A struct drm_mm_node *stolen)
1450N/A{
1450N/A#if 0
1450N/A struct drm_i915_gem_object *obj;
1450N/A
1450N/A obj = i915_gem_object_alloc(dev);
1450N/A if (obj == NULL)
1450N/A return NULL;
1450N/A
1450N/A if (drm_gem_private_object_init(dev, &obj->base, stolen->size))
1450N/A goto cleanup;
1450N/A
1450N/A i915_gem_object_init(obj, &i915_gem_object_stolen_ops);
1450N/A
1450N/A obj->page_list = i915_pages_create_for_stolen(dev,
1450N/A stolen->start, stolen->size);
1450N/A if (obj->pages == NULL)
1450N/A goto cleanup;
1450N/A
1450N/A obj->has_dma_mapping = true;
1450N/A i915_gem_object_pin_pages(obj);
1450N/A obj->stolen = stolen;
1450N/A
1450N/A obj->base.write_domain = I915_GEM_DOMAIN_GTT;
1450N/A obj->base.read_domains = I915_GEM_DOMAIN_GTT;
1450N/A obj->cache_level = I915_CACHE_NONE;
1450N/A
1450N/A return obj;
1450N/A
1450N/Acleanup:
1450N/A i915_gem_object_free(obj);
1450N/A#endif
1450N/A return NULL;
1450N/A}
1450N/A
1450N/A
1450N/Astruct drm_i915_gem_object *
1450N/Ai915_gem_object_create_stolen(struct drm_device *dev, u32 size)
1450N/A{
1450N/A DRM_DEBUG_KMS("Not support stolen object yet");
1450N/A return NULL;
1450N/A#if 0
1450N/A struct drm_i915_private *dev_priv = dev->dev_private;
1450N/A struct drm_i915_gem_object *obj;
1450N/A struct drm_mm_node *stolen;
1450N/A
1450N/A if (!drm_mm_initialized(&dev_priv->mm.stolen))
1450N/A return NULL;
1450N/A
1450N/A DRM_DEBUG_KMS("creating stolen object: size=%x\n", size);
1450N/A if (size == 0)
1450N/A return NULL;
1450N/A
1450N/A stolen = drm_mm_search_free(&dev_priv->mm.stolen, size, 4096, 0);
1450N/A if (stolen)
1450N/A stolen = drm_mm_get_block(stolen, size, 4096);
1450N/A if (stolen == NULL)
1450N/A return NULL;
1450N/A
1450N/A obj = _i915_gem_object_create_stolen(dev, stolen);
1450N/A if (obj)
1450N/A return obj;
1450N/A
1450N/A drm_mm_put_block(stolen);
1450N/A return NULL;
1450N/A#endif
1450N/A}
1450N/A
1450N/Astruct drm_i915_gem_object *
1450N/Ai915_gem_object_create_stolen_for_preallocated(struct drm_device *dev,
1450N/A u32 stolen_offset,
1450N/A u32 gtt_offset,
1450N/A u32 size)
1450N/A{
1450N/A struct drm_i915_private *dev_priv = dev->dev_private;
1450N/A struct drm_i915_gem_object *obj;
1450N/A struct drm_mm_node *stolen;
1450N/A
1450N/A if (!drm_mm_initialized(&dev_priv->mm.stolen))
1450N/A return NULL;
1450N/A
1450N/A DRM_DEBUG_KMS("creating preallocated stolen object: stolen_offset=%x, gtt_offset=%x, size=%x\n",
1450N/A stolen_offset, gtt_offset, size);
1450N/A
1450N/A /* KISS and expect everything to be page-aligned */
1450N/A BUG_ON(stolen_offset & 4095);
1450N/A BUG_ON(size & 4095);
1450N/A
1450N/A if (size == 0)
1450N/A return NULL;
1450N/A
1450N/A stolen = drm_mm_create_block(&dev_priv->mm.stolen,
1450N/A stolen_offset, size,
1450N/A false);
1450N/A if (stolen == NULL) {
1450N/A DRM_DEBUG_KMS("failed to allocate stolen space\n");
1450N/A return NULL;
1450N/A }
1450N/A
1450N/A obj = _i915_gem_object_create_stolen(dev, stolen);
1450N/A if (obj == NULL) {
1450N/A DRM_DEBUG_KMS("failed to allocate stolen object\n");
1450N/A drm_mm_put_block(stolen);
1450N/A return NULL;
1450N/A }
1450N/A
1450N/A /* Some objects just need physical mem from stolen space */
1450N/A if (gtt_offset == 0xffffffff)
1450N/A return obj;
1450N/A
1450N/A /* To simplify the initialisation sequence between KMS and GTT,
1450N/A * we allow construction of the stolen object prior to
1450N/A * setting up the GTT space. The actual reservation will occur
1450N/A * later.
1450N/A */
1450N/A if (drm_mm_initialized(&dev_priv->mm.gtt_space)) {
1450N/A obj->gtt_space = drm_mm_create_block(&dev_priv->mm.gtt_space,
1450N/A gtt_offset, size,
1450N/A false);
1450N/A if (obj->gtt_space == NULL) {
1450N/A DRM_DEBUG_KMS("failed to allocate stolen GTT space\n");
1450N/A drm_gem_object_unreference(&obj->base);
1450N/A return NULL;
1450N/A }
1450N/A } else
1450N/A obj->gtt_space = I915_GTT_RESERVED;
1450N/A
1450N/A obj->gtt_offset = gtt_offset;
1450N/A obj->has_global_gtt_mapping = 1;
1450N/A
1450N/A list_add_tail(&obj->global_list, &dev_priv->mm.bound_list, (caddr_t)obj);
1450N/A list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list, (caddr_t)obj);
1450N/A
1450N/A return obj;
1450N/A}
1450N/A
1450N/Avoid
1450N/Ai915_gem_object_release_stolen(struct drm_i915_gem_object *obj)
1450N/A{
1450N/A if (obj->stolen) {
1450N/A drm_mm_put_block(obj->stolen);
1450N/A obj->stolen = NULL;
1450N/A }
1450N/A}