1
2
3
4
5
6
7
8
9
10#include <drm/drmP.h>
11#include <drm/drm_crtc_helper.h>
12#include "mgag200_drv.h"
13
14static void mga_user_framebuffer_destroy(struct drm_framebuffer *fb)
15{
16 struct mga_framebuffer *mga_fb = to_mga_framebuffer(fb);
17
18 drm_gem_object_put_unlocked(mga_fb->obj);
19 drm_framebuffer_cleanup(fb);
20 kfree(fb);
21}
22
23static const struct drm_framebuffer_funcs mga_fb_funcs = {
24 .destroy = mga_user_framebuffer_destroy,
25};
26
27int mgag200_framebuffer_init(struct drm_device *dev,
28 struct mga_framebuffer *gfb,
29 const struct drm_mode_fb_cmd2 *mode_cmd,
30 struct drm_gem_object *obj)
31{
32 int ret;
33
34 drm_helper_mode_fill_fb_struct(dev, &gfb->base, mode_cmd);
35 gfb->obj = obj;
36 ret = drm_framebuffer_init(dev, &gfb->base, &mga_fb_funcs);
37 if (ret) {
38 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
39 return ret;
40 }
41 return 0;
42}
43
44static struct drm_framebuffer *
45mgag200_user_framebuffer_create(struct drm_device *dev,
46 struct drm_file *filp,
47 const struct drm_mode_fb_cmd2 *mode_cmd)
48{
49 struct drm_gem_object *obj;
50 struct mga_framebuffer *mga_fb;
51 int ret;
52
53 obj = drm_gem_object_lookup(filp, mode_cmd->handles[0]);
54 if (obj == NULL)
55 return ERR_PTR(-ENOENT);
56
57 mga_fb = kzalloc(sizeof(*mga_fb), GFP_KERNEL);
58 if (!mga_fb) {
59 drm_gem_object_put_unlocked(obj);
60 return ERR_PTR(-ENOMEM);
61 }
62
63 ret = mgag200_framebuffer_init(dev, mga_fb, mode_cmd, obj);
64 if (ret) {
65 drm_gem_object_put_unlocked(obj);
66 kfree(mga_fb);
67 return ERR_PTR(ret);
68 }
69 return &mga_fb->base;
70}
71
72static const struct drm_mode_config_funcs mga_mode_funcs = {
73 .fb_create = mgag200_user_framebuffer_create,
74};
75
76static int mga_probe_vram(struct mga_device *mdev, void __iomem *mem)
77{
78 int offset;
79 int orig;
80 int test1, test2;
81 int orig1, orig2;
82 unsigned int vram_size;
83
84
85 orig = ioread16(mem);
86 iowrite16(0, mem);
87
88 vram_size = mdev->mc.vram_window;
89
90 if ((mdev->type == G200_EW3) && (vram_size >= 0x1000000)) {
91 vram_size = vram_size - 0x400000;
92 }
93
94 for (offset = 0x100000; offset < vram_size; offset += 0x4000) {
95 orig1 = ioread8(mem + offset);
96 orig2 = ioread8(mem + offset + 0x100);
97
98 iowrite16(0xaa55, mem + offset);
99 iowrite16(0xaa55, mem + offset + 0x100);
100
101 test1 = ioread16(mem + offset);
102 test2 = ioread16(mem);
103
104 iowrite16(orig1, mem + offset);
105 iowrite16(orig2, mem + offset + 0x100);
106
107 if (test1 != 0xaa55) {
108 break;
109 }
110
111 if (test2) {
112 break;
113 }
114 }
115
116 iowrite16(orig, mem);
117 return offset - 65536;
118}
119
120
121static int mga_vram_init(struct mga_device *mdev)
122{
123 void __iomem *mem;
124
125
126 mdev->mc.vram_base = pci_resource_start(mdev->dev->pdev, 0);
127 mdev->mc.vram_window = pci_resource_len(mdev->dev->pdev, 0);
128
129 if (!devm_request_mem_region(mdev->dev->dev, mdev->mc.vram_base, mdev->mc.vram_window,
130 "mgadrmfb_vram")) {
131 DRM_ERROR("can't reserve VRAM\n");
132 return -ENXIO;
133 }
134
135 mem = pci_iomap(mdev->dev->pdev, 0, 0);
136 if (!mem)
137 return -ENOMEM;
138
139 mdev->mc.vram_size = mga_probe_vram(mdev, mem);
140
141 pci_iounmap(mdev->dev->pdev, mem);
142
143 return 0;
144}
145
146static int mgag200_device_init(struct drm_device *dev,
147 uint32_t flags)
148{
149 struct mga_device *mdev = dev->dev_private;
150 int ret, option;
151
152 mdev->flags = mgag200_flags_from_driver_data(flags);
153 mdev->type = mgag200_type_from_driver_data(flags);
154
155
156 mdev->num_crtc = 1;
157
158 pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option);
159 mdev->has_sdram = !(option & (1 << 14));
160
161
162 mdev->rmmio_base = pci_resource_start(mdev->dev->pdev, 1);
163 mdev->rmmio_size = pci_resource_len(mdev->dev->pdev, 1);
164
165 if (!devm_request_mem_region(mdev->dev->dev, mdev->rmmio_base, mdev->rmmio_size,
166 "mgadrmfb_mmio")) {
167 DRM_ERROR("can't reserve mmio registers\n");
168 return -ENOMEM;
169 }
170
171 mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
172 if (mdev->rmmio == NULL)
173 return -ENOMEM;
174
175
176 if (IS_G200_SE(mdev))
177 mdev->unique_rev_id = RREG32(0x1e24);
178
179 ret = mga_vram_init(mdev);
180 if (ret)
181 return ret;
182
183 mdev->bpp_shifts[0] = 0;
184 mdev->bpp_shifts[1] = 1;
185 mdev->bpp_shifts[2] = 0;
186 mdev->bpp_shifts[3] = 2;
187 return 0;
188}
189
190
191
192
193
194
195
196int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
197{
198 struct mga_device *mdev;
199 int r;
200
201 mdev = devm_kzalloc(dev->dev, sizeof(struct mga_device), GFP_KERNEL);
202 if (mdev == NULL)
203 return -ENOMEM;
204 dev->dev_private = (void *)mdev;
205 mdev->dev = dev;
206
207 r = mgag200_device_init(dev, flags);
208 if (r) {
209 dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
210 return r;
211 }
212 r = mgag200_mm_init(mdev);
213 if (r)
214 goto err_mm;
215
216 drm_mode_config_init(dev);
217 dev->mode_config.funcs = (void *)&mga_mode_funcs;
218 if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
219 dev->mode_config.preferred_depth = 16;
220 else
221 dev->mode_config.preferred_depth = 24;
222 dev->mode_config.prefer_shadow = 1;
223
224 r = mgag200_modeset_init(mdev);
225 if (r) {
226 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
227 goto err_modeset;
228 }
229
230
231 mdev->cursor.pixels_1 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
232 roundup(48*64, PAGE_SIZE),
233 0, 0);
234 mdev->cursor.pixels_2 = drm_gem_vram_create(dev, &dev->vram_mm->bdev,
235 roundup(48*64, PAGE_SIZE),
236 0, 0);
237 if (IS_ERR(mdev->cursor.pixels_2) || IS_ERR(mdev->cursor.pixels_1)) {
238 mdev->cursor.pixels_1 = NULL;
239 mdev->cursor.pixels_2 = NULL;
240 dev_warn(&dev->pdev->dev,
241 "Could not allocate space for cursors. Not doing hardware cursors.\n");
242 }
243 mdev->cursor.pixels_current = NULL;
244
245 return 0;
246
247err_modeset:
248 drm_mode_config_cleanup(dev);
249 mgag200_mm_fini(mdev);
250err_mm:
251 dev->dev_private = NULL;
252
253 return r;
254}
255
256void mgag200_driver_unload(struct drm_device *dev)
257{
258 struct mga_device *mdev = dev->dev_private;
259
260 if (mdev == NULL)
261 return;
262 mgag200_modeset_fini(mdev);
263 mgag200_fbdev_fini(mdev);
264 drm_mode_config_cleanup(dev);
265 mgag200_mm_fini(mdev);
266 dev->dev_private = NULL;
267}
268
269int mgag200_gem_create(struct drm_device *dev,
270 u32 size, bool iskernel,
271 struct drm_gem_object **obj)
272{
273 struct drm_gem_vram_object *gbo;
274 int ret;
275
276 *obj = NULL;
277
278 size = roundup(size, PAGE_SIZE);
279 if (size == 0)
280 return -EINVAL;
281
282 gbo = drm_gem_vram_create(dev, &dev->vram_mm->bdev, size, 0, false);
283 if (IS_ERR(gbo)) {
284 ret = PTR_ERR(gbo);
285 if (ret != -ERESTARTSYS)
286 DRM_ERROR("failed to allocate GEM object\n");
287 return ret;
288 }
289 *obj = &gbo->gem;
290 return 0;
291}
292