2 * Copyright 2012 Red Hat Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sub license, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
15 * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
18 * USE OR OTHER DEALINGS IN THE SOFTWARE.
20 * The above copyright notice and this permission notice (including the
21 * next paragraph) shall be included in all copies or substantial portions
26 * Authors: Dave Airlie <airlied@redhat.com>
29 #include <drm/ttm/ttm_page_alloc.h>
31 #include "mgag200_drv.h"
33 static inline struct mga_device *
34 mgag200_bdev(struct ttm_bo_device *bd)
36 return container_of(bd, struct mga_device, ttm.bdev);
40 mgag200_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
41 struct ttm_mem_type_manager *man)
45 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
46 man->available_caching = TTM_PL_MASK_CACHING;
47 man->default_caching = TTM_PL_FLAG_CACHED;
50 man->func = &ttm_bo_manager_func;
51 man->flags = TTM_MEMTYPE_FLAG_FIXED |
52 TTM_MEMTYPE_FLAG_MAPPABLE;
53 man->available_caching = TTM_PL_FLAG_UNCACHED |
55 man->default_caching = TTM_PL_FLAG_WC;
58 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
64 static int mgag200_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
65 struct ttm_mem_reg *mem)
67 struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
68 struct mga_device *mdev = mgag200_bdev(bdev);
72 mem->bus.size = mem->num_pages << PAGE_SHIFT;
74 mem->bus.is_iomem = false;
75 if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
77 switch (mem->mem_type) {
82 mem->bus.offset = mem->start << PAGE_SHIFT;
83 mem->bus.base = pci_resource_start(mdev->dev->pdev, 0);
84 mem->bus.is_iomem = true;
93 static void mgag200_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
97 static void mgag200_ttm_backend_destroy(struct ttm_tt *tt)
103 static struct ttm_backend_func mgag200_tt_backend_func = {
104 .destroy = &mgag200_ttm_backend_destroy,
108 static struct ttm_tt *mgag200_ttm_tt_create(struct ttm_buffer_object *bo,
113 tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
116 tt->func = &mgag200_tt_backend_func;
117 if (ttm_tt_init(tt, bo, page_flags)) {
124 struct ttm_bo_driver mgag200_bo_driver = {
125 .ttm_tt_create = mgag200_ttm_tt_create,
126 .init_mem_type = mgag200_bo_init_mem_type,
127 .eviction_valuable = ttm_bo_eviction_valuable,
128 .evict_flags = drm_gem_vram_bo_driver_evict_flags,
130 .verify_access = drm_gem_vram_bo_driver_verify_access,
131 .io_mem_reserve = &mgag200_ttm_io_mem_reserve,
132 .io_mem_free = &mgag200_ttm_io_mem_free,
135 int mgag200_mm_init(struct mga_device *mdev)
138 struct drm_device *dev = mdev->dev;
139 struct ttm_bo_device *bdev = &mdev->ttm.bdev;
141 ret = ttm_bo_device_init(&mdev->ttm.bdev,
143 dev->anon_inode->i_mapping,
146 DRM_ERROR("Error initialising bo driver; %d\n", ret);
150 ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM, mdev->mc.vram_size >> PAGE_SHIFT);
152 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
156 arch_io_reserve_memtype_wc(pci_resource_start(dev->pdev, 0),
157 pci_resource_len(dev->pdev, 0));
159 mdev->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
160 pci_resource_len(dev->pdev, 0));
165 void mgag200_mm_fini(struct mga_device *mdev)
167 struct drm_device *dev = mdev->dev;
169 ttm_bo_device_release(&mdev->ttm.bdev);
171 arch_io_free_memtype_wc(pci_resource_start(dev->pdev, 0),
172 pci_resource_len(dev->pdev, 0));
173 arch_phys_wc_del(mdev->fb_mtrr);
177 int mgag200_mmap(struct file *filp, struct vm_area_struct *vma)
179 struct drm_file *file_priv = filp->private_data;
180 struct mga_device *mdev = file_priv->minor->dev->dev_private;
182 return ttm_bo_mmap(filp, vma, &mdev->ttm.bdev);