]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/mgag200/mgag200_ttm.c
drm/mgag200: Convert mgag200 driver to |struct drm_gem_vram_object|
[linux.git] / drivers / gpu / drm / mgag200 / mgag200_ttm.c
1 /*
2  * Copyright 2012 Red Hat Inc.
3  *
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:
11  *
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.
19  *
20  * The above copyright notice and this permission notice (including the
21  * next paragraph) shall be included in all copies or substantial portions
22  * of the Software.
23  *
24  */
25 /*
26  * Authors: Dave Airlie <airlied@redhat.com>
27  */
28 #include <drm/drmP.h>
29 #include <drm/ttm/ttm_page_alloc.h>
30
31 #include "mgag200_drv.h"
32
33 static inline struct mga_device *
34 mgag200_bdev(struct ttm_bo_device *bd)
35 {
36         return container_of(bd, struct mga_device, ttm.bdev);
37 }
38
39 static int
40 mgag200_bo_init_mem_type(struct ttm_bo_device *bdev, uint32_t type,
41                      struct ttm_mem_type_manager *man)
42 {
43         switch (type) {
44         case TTM_PL_SYSTEM:
45                 man->flags = TTM_MEMTYPE_FLAG_MAPPABLE;
46                 man->available_caching = TTM_PL_MASK_CACHING;
47                 man->default_caching = TTM_PL_FLAG_CACHED;
48                 break;
49         case TTM_PL_VRAM:
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 |
54                         TTM_PL_FLAG_WC;
55                 man->default_caching = TTM_PL_FLAG_WC;
56                 break;
57         default:
58                 DRM_ERROR("Unsupported memory type %u\n", (unsigned)type);
59                 return -EINVAL;
60         }
61         return 0;
62 }
63
64 static int mgag200_ttm_io_mem_reserve(struct ttm_bo_device *bdev,
65                                   struct ttm_mem_reg *mem)
66 {
67         struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type];
68         struct mga_device *mdev = mgag200_bdev(bdev);
69
70         mem->bus.addr = NULL;
71         mem->bus.offset = 0;
72         mem->bus.size = mem->num_pages << PAGE_SHIFT;
73         mem->bus.base = 0;
74         mem->bus.is_iomem = false;
75         if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE))
76                 return -EINVAL;
77         switch (mem->mem_type) {
78         case TTM_PL_SYSTEM:
79                 /* system memory */
80                 return 0;
81         case TTM_PL_VRAM:
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;
85                 break;
86         default:
87                 return -EINVAL;
88                 break;
89         }
90         return 0;
91 }
92
93 static void mgag200_ttm_io_mem_free(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem)
94 {
95 }
96
97 static void mgag200_ttm_backend_destroy(struct ttm_tt *tt)
98 {
99         ttm_tt_fini(tt);
100         kfree(tt);
101 }
102
103 static struct ttm_backend_func mgag200_tt_backend_func = {
104         .destroy = &mgag200_ttm_backend_destroy,
105 };
106
107
108 static struct ttm_tt *mgag200_ttm_tt_create(struct ttm_buffer_object *bo,
109                                             uint32_t page_flags)
110 {
111         struct ttm_tt *tt;
112
113         tt = kzalloc(sizeof(struct ttm_tt), GFP_KERNEL);
114         if (tt == NULL)
115                 return NULL;
116         tt->func = &mgag200_tt_backend_func;
117         if (ttm_tt_init(tt, bo, page_flags)) {
118                 kfree(tt);
119                 return NULL;
120         }
121         return tt;
122 }
123
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,
129         .move = NULL,
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,
133 };
134
135 int mgag200_mm_init(struct mga_device *mdev)
136 {
137         int ret;
138         struct drm_device *dev = mdev->dev;
139         struct ttm_bo_device *bdev = &mdev->ttm.bdev;
140
141         ret = ttm_bo_device_init(&mdev->ttm.bdev,
142                                  &mgag200_bo_driver,
143                                  dev->anon_inode->i_mapping,
144                                  true);
145         if (ret) {
146                 DRM_ERROR("Error initialising bo driver; %d\n", ret);
147                 return ret;
148         }
149
150         ret = ttm_bo_init_mm(bdev, TTM_PL_VRAM, mdev->mc.vram_size >> PAGE_SHIFT);
151         if (ret) {
152                 DRM_ERROR("Failed ttm VRAM init: %d\n", ret);
153                 return ret;
154         }
155
156         arch_io_reserve_memtype_wc(pci_resource_start(dev->pdev, 0),
157                                    pci_resource_len(dev->pdev, 0));
158
159         mdev->fb_mtrr = arch_phys_wc_add(pci_resource_start(dev->pdev, 0),
160                                          pci_resource_len(dev->pdev, 0));
161
162         return 0;
163 }
164
165 void mgag200_mm_fini(struct mga_device *mdev)
166 {
167         struct drm_device *dev = mdev->dev;
168
169         ttm_bo_device_release(&mdev->ttm.bdev);
170
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);
174         mdev->fb_mtrr = 0;
175 }
176
177 int mgag200_mmap(struct file *filp, struct vm_area_struct *vma)
178 {
179         struct drm_file *file_priv = filp->private_data;
180         struct mga_device *mdev = file_priv->minor->dev->dev_private;
181
182         return ttm_bo_mmap(filp, vma, &mdev->ttm.bdev);
183 }