1 /*
2 * Copyright 2010 Matt Turner.
3 * Copyright 2012 Red Hat
4 *
5 * This file is subject to the terms and conditions of the GNU General
6 * Public License version 2. See the file COPYING in the main
7 * directory of this archive for more details.
8 *
9 * Authors: Matthew Garrett
10 * Matt Turner
11 * Dave Airlie
12 */
13 #include <drm/drmP.h>
14 #include <drm/drm_crtc_helper.h>
15 #include "mgag200_drv.h"
16
mga_user_framebuffer_destroy(struct drm_framebuffer * fb)17 static void mga_user_framebuffer_destroy(struct drm_framebuffer *fb)
18 {
19 struct mga_framebuffer *mga_fb = to_mga_framebuffer(fb);
20 if (mga_fb->obj)
21 drm_gem_object_unreference_unlocked(mga_fb->obj);
22 drm_framebuffer_cleanup(fb);
23 kfree(fb);
24 }
25
26 static const struct drm_framebuffer_funcs mga_fb_funcs = {
27 .destroy = mga_user_framebuffer_destroy,
28 };
29
mgag200_framebuffer_init(struct drm_device * dev,struct mga_framebuffer * gfb,struct drm_mode_fb_cmd2 * mode_cmd,struct drm_gem_object * obj)30 int mgag200_framebuffer_init(struct drm_device *dev,
31 struct mga_framebuffer *gfb,
32 struct drm_mode_fb_cmd2 *mode_cmd,
33 struct drm_gem_object *obj)
34 {
35 int ret;
36
37 drm_helper_mode_fill_fb_struct(&gfb->base, mode_cmd);
38 gfb->obj = obj;
39 ret = drm_framebuffer_init(dev, &gfb->base, &mga_fb_funcs);
40 if (ret) {
41 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret);
42 return ret;
43 }
44 return 0;
45 }
46
47 static struct drm_framebuffer *
mgag200_user_framebuffer_create(struct drm_device * dev,struct drm_file * filp,struct drm_mode_fb_cmd2 * mode_cmd)48 mgag200_user_framebuffer_create(struct drm_device *dev,
49 struct drm_file *filp,
50 struct drm_mode_fb_cmd2 *mode_cmd)
51 {
52 struct drm_gem_object *obj;
53 struct mga_framebuffer *mga_fb;
54 int ret;
55
56 obj = drm_gem_object_lookup(dev, filp, mode_cmd->handles[0]);
57 if (obj == NULL)
58 return ERR_PTR(-ENOENT);
59
60 mga_fb = kzalloc(sizeof(*mga_fb), GFP_KERNEL);
61 if (!mga_fb) {
62 drm_gem_object_unreference_unlocked(obj);
63 return ERR_PTR(-ENOMEM);
64 }
65
66 ret = mgag200_framebuffer_init(dev, mga_fb, mode_cmd, obj);
67 if (ret) {
68 drm_gem_object_unreference_unlocked(obj);
69 kfree(mga_fb);
70 return ERR_PTR(ret);
71 }
72 return &mga_fb->base;
73 }
74
75 static const struct drm_mode_config_funcs mga_mode_funcs = {
76 .fb_create = mgag200_user_framebuffer_create,
77 };
78
mga_probe_vram(struct mga_device * mdev,void __iomem * mem)79 static int mga_probe_vram(struct mga_device *mdev, void __iomem *mem)
80 {
81 int offset;
82 int orig;
83 int test1, test2;
84 int orig1, orig2;
85
86 /* Probe */
87 orig = ioread16(mem);
88 iowrite16(0, mem);
89
90 for (offset = 0x100000; offset < mdev->mc.vram_window; offset += 0x4000) {
91 orig1 = ioread8(mem + offset);
92 orig2 = ioread8(mem + offset + 0x100);
93
94 iowrite16(0xaa55, mem + offset);
95 iowrite16(0xaa55, mem + offset + 0x100);
96
97 test1 = ioread16(mem + offset);
98 test2 = ioread16(mem);
99
100 iowrite16(orig1, mem + offset);
101 iowrite16(orig2, mem + offset + 0x100);
102
103 if (test1 != 0xaa55) {
104 break;
105 }
106
107 if (test2) {
108 break;
109 }
110 }
111
112 iowrite16(orig, mem);
113 return offset - 65536;
114 }
115
116 /* Map the framebuffer from the card and configure the core */
mga_vram_init(struct mga_device * mdev)117 static int mga_vram_init(struct mga_device *mdev)
118 {
119 void __iomem *mem;
120 struct apertures_struct *aper = alloc_apertures(1);
121 if (!aper)
122 return -ENOMEM;
123
124 /* BAR 0 is VRAM */
125 mdev->mc.vram_base = pci_resource_start(mdev->dev->pdev, 0);
126 mdev->mc.vram_window = pci_resource_len(mdev->dev->pdev, 0);
127
128 aper->ranges[0].base = mdev->mc.vram_base;
129 aper->ranges[0].size = mdev->mc.vram_window;
130
131 remove_conflicting_framebuffers(aper, "mgafb", true);
132 kfree(aper);
133
134 if (!devm_request_mem_region(mdev->dev->dev, mdev->mc.vram_base, mdev->mc.vram_window,
135 "mgadrmfb_vram")) {
136 DRM_ERROR("can't reserve VRAM\n");
137 return -ENXIO;
138 }
139
140 mem = pci_iomap(mdev->dev->pdev, 0, 0);
141 if (!mem)
142 return -ENOMEM;
143
144 mdev->mc.vram_size = mga_probe_vram(mdev, mem);
145
146 pci_iounmap(mdev->dev->pdev, mem);
147
148 return 0;
149 }
150
mgag200_device_init(struct drm_device * dev,uint32_t flags)151 static int mgag200_device_init(struct drm_device *dev,
152 uint32_t flags)
153 {
154 struct mga_device *mdev = dev->dev_private;
155 int ret, option;
156
157 mdev->type = flags;
158
159 /* Hardcode the number of CRTCs to 1 */
160 mdev->num_crtc = 1;
161
162 pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option);
163 mdev->has_sdram = !(option & (1 << 14));
164
165 /* BAR 0 is the framebuffer, BAR 1 contains registers */
166 mdev->rmmio_base = pci_resource_start(mdev->dev->pdev, 1);
167 mdev->rmmio_size = pci_resource_len(mdev->dev->pdev, 1);
168
169 if (!devm_request_mem_region(mdev->dev->dev, mdev->rmmio_base, mdev->rmmio_size,
170 "mgadrmfb_mmio")) {
171 DRM_ERROR("can't reserve mmio registers\n");
172 return -ENOMEM;
173 }
174
175 mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
176 if (mdev->rmmio == NULL)
177 return -ENOMEM;
178
179 /* stash G200 SE model number for later use */
180 if (IS_G200_SE(mdev))
181 mdev->unique_rev_id = RREG32(0x1e24);
182
183 ret = mga_vram_init(mdev);
184 if (ret)
185 return ret;
186
187 mdev->bpp_shifts[0] = 0;
188 mdev->bpp_shifts[1] = 1;
189 mdev->bpp_shifts[2] = 0;
190 mdev->bpp_shifts[3] = 2;
191 return 0;
192 }
193
194 /*
195 * Functions here will be called by the core once it's bound the driver to
196 * a PCI device
197 */
198
199
mgag200_driver_load(struct drm_device * dev,unsigned long flags)200 int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
201 {
202 struct mga_device *mdev;
203 int r;
204
205 mdev = devm_kzalloc(dev->dev, sizeof(struct mga_device), GFP_KERNEL);
206 if (mdev == NULL)
207 return -ENOMEM;
208 dev->dev_private = (void *)mdev;
209 mdev->dev = dev;
210
211 r = mgag200_device_init(dev, flags);
212 if (r) {
213 dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
214 return r;
215 }
216 r = mgag200_mm_init(mdev);
217 if (r)
218 goto out;
219
220 drm_mode_config_init(dev);
221 dev->mode_config.funcs = (void *)&mga_mode_funcs;
222 if (IS_G200_SE(mdev) && mdev->mc.vram_size < (2048*1024))
223 dev->mode_config.preferred_depth = 16;
224 else
225 dev->mode_config.preferred_depth = 24;
226 dev->mode_config.prefer_shadow = 1;
227
228 r = mgag200_modeset_init(mdev);
229 if (r) {
230 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
231 goto out;
232 }
233
234 /* Make small buffers to store a hardware cursor (double buffered icon updates) */
235 mgag200_bo_create(dev, roundup(48*64, PAGE_SIZE), 0, 0,
236 &mdev->cursor.pixels_1);
237 mgag200_bo_create(dev, roundup(48*64, PAGE_SIZE), 0, 0,
238 &mdev->cursor.pixels_2);
239 if (!mdev->cursor.pixels_2 || !mdev->cursor.pixels_1)
240 goto cursor_nospace;
241 mdev->cursor.pixels_current = mdev->cursor.pixels_1;
242 mdev->cursor.pixels_prev = mdev->cursor.pixels_2;
243 goto cursor_done;
244 cursor_nospace:
245 mdev->cursor.pixels_1 = NULL;
246 mdev->cursor.pixels_2 = NULL;
247 dev_warn(&dev->pdev->dev, "Could not allocate space for cursors. Not doing hardware cursors.\n");
248 cursor_done:
249
250 out:
251 if (r)
252 mgag200_driver_unload(dev);
253 return r;
254 }
255
mgag200_driver_unload(struct drm_device * dev)256 int mgag200_driver_unload(struct drm_device *dev)
257 {
258 struct mga_device *mdev = dev->dev_private;
259
260 if (mdev == NULL)
261 return 0;
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 return 0;
268 }
269
mgag200_gem_create(struct drm_device * dev,u32 size,bool iskernel,struct drm_gem_object ** obj)270 int mgag200_gem_create(struct drm_device *dev,
271 u32 size, bool iskernel,
272 struct drm_gem_object **obj)
273 {
274 struct mgag200_bo *astbo;
275 int ret;
276
277 *obj = NULL;
278
279 size = roundup(size, PAGE_SIZE);
280 if (size == 0)
281 return -EINVAL;
282
283 ret = mgag200_bo_create(dev, size, 0, 0, &astbo);
284 if (ret) {
285 if (ret != -ERESTARTSYS)
286 DRM_ERROR("failed to allocate GEM object\n");
287 return ret;
288 }
289 *obj = &astbo->gem;
290 return 0;
291 }
292
mgag200_dumb_create(struct drm_file * file,struct drm_device * dev,struct drm_mode_create_dumb * args)293 int mgag200_dumb_create(struct drm_file *file,
294 struct drm_device *dev,
295 struct drm_mode_create_dumb *args)
296 {
297 int ret;
298 struct drm_gem_object *gobj;
299 u32 handle;
300
301 args->pitch = args->width * ((args->bpp + 7) / 8);
302 args->size = args->pitch * args->height;
303
304 ret = mgag200_gem_create(dev, args->size, false,
305 &gobj);
306 if (ret)
307 return ret;
308
309 ret = drm_gem_handle_create(file, gobj, &handle);
310 drm_gem_object_unreference_unlocked(gobj);
311 if (ret)
312 return ret;
313
314 args->handle = handle;
315 return 0;
316 }
317
mgag200_bo_unref(struct mgag200_bo ** bo)318 static void mgag200_bo_unref(struct mgag200_bo **bo)
319 {
320 struct ttm_buffer_object *tbo;
321
322 if ((*bo) == NULL)
323 return;
324
325 tbo = &((*bo)->bo);
326 ttm_bo_unref(&tbo);
327 *bo = NULL;
328 }
329
mgag200_gem_free_object(struct drm_gem_object * obj)330 void mgag200_gem_free_object(struct drm_gem_object *obj)
331 {
332 struct mgag200_bo *mgag200_bo = gem_to_mga_bo(obj);
333
334 mgag200_bo_unref(&mgag200_bo);
335 }
336
337
mgag200_bo_mmap_offset(struct mgag200_bo * bo)338 static inline u64 mgag200_bo_mmap_offset(struct mgag200_bo *bo)
339 {
340 return drm_vma_node_offset_addr(&bo->bo.vma_node);
341 }
342
343 int
mgag200_dumb_mmap_offset(struct drm_file * file,struct drm_device * dev,uint32_t handle,uint64_t * offset)344 mgag200_dumb_mmap_offset(struct drm_file *file,
345 struct drm_device *dev,
346 uint32_t handle,
347 uint64_t *offset)
348 {
349 struct drm_gem_object *obj;
350 int ret;
351 struct mgag200_bo *bo;
352
353 mutex_lock(&dev->struct_mutex);
354 obj = drm_gem_object_lookup(dev, file, handle);
355 if (obj == NULL) {
356 ret = -ENOENT;
357 goto out_unlock;
358 }
359
360 bo = gem_to_mga_bo(obj);
361 *offset = mgag200_bo_mmap_offset(bo);
362
363 drm_gem_object_unreference(obj);
364 ret = 0;
365 out_unlock:
366 mutex_unlock(&dev->struct_mutex);
367 return ret;
368
369 }
370