2 * Copyright 2010 Matt Turner.
3 * Copyright 2012 Red Hat
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.
9 * Authors: Matthew Garrett
15 #include "drm_crtc_helper.h"
16 #include "mgag200_drv.h"
18 static void mga_user_framebuffer_destroy(struct drm_framebuffer
*fb
)
20 struct mga_framebuffer
*mga_fb
= to_mga_framebuffer(fb
);
22 drm_gem_object_unreference_unlocked(mga_fb
->obj
);
23 drm_framebuffer_cleanup(fb
);
27 static int mga_user_framebuffer_create_handle(struct drm_framebuffer
*fb
,
28 struct drm_file
*file_priv
,
34 static const struct drm_framebuffer_funcs mga_fb_funcs
= {
35 .destroy
= mga_user_framebuffer_destroy
,
36 .create_handle
= mga_user_framebuffer_create_handle
,
39 int mgag200_framebuffer_init(struct drm_device
*dev
,
40 struct mga_framebuffer
*gfb
,
41 struct drm_mode_fb_cmd2
*mode_cmd
,
42 struct drm_gem_object
*obj
)
44 int ret
= drm_framebuffer_init(dev
, &gfb
->base
, &mga_fb_funcs
);
46 DRM_ERROR("drm_framebuffer_init failed: %d\n", ret
);
49 drm_helper_mode_fill_fb_struct(&gfb
->base
, mode_cmd
);
54 static struct drm_framebuffer
*
55 mgag200_user_framebuffer_create(struct drm_device
*dev
,
56 struct drm_file
*filp
,
57 struct drm_mode_fb_cmd2
*mode_cmd
)
59 struct drm_gem_object
*obj
;
60 struct mga_framebuffer
*mga_fb
;
63 obj
= drm_gem_object_lookup(dev
, filp
, mode_cmd
->handles
[0]);
65 return ERR_PTR(-ENOENT
);
67 mga_fb
= kzalloc(sizeof(*mga_fb
), GFP_KERNEL
);
69 drm_gem_object_unreference_unlocked(obj
);
70 return ERR_PTR(-ENOMEM
);
73 ret
= mgag200_framebuffer_init(dev
, mga_fb
, mode_cmd
, obj
);
75 drm_gem_object_unreference_unlocked(obj
);
82 static const struct drm_mode_config_funcs mga_mode_funcs
= {
83 .fb_create
= mgag200_user_framebuffer_create
,
86 /* Unmap the framebuffer from the core and release the memory */
87 static void mga_vram_fini(struct mga_device
*mdev
)
89 pci_iounmap(mdev
->dev
->pdev
, mdev
->rmmio
);
91 if (mdev
->mc
.vram_base
)
92 release_mem_region(mdev
->mc
.vram_base
, mdev
->mc
.vram_window
);
95 static int mga_probe_vram(struct mga_device
*mdev
, void __iomem
*mem
)
103 orig
= ioread16(mem
);
106 for (offset
= 0x100000; offset
< mdev
->mc
.vram_window
; offset
+= 0x4000) {
107 orig1
= ioread8(mem
+ offset
);
108 orig2
= ioread8(mem
+ offset
+ 0x100);
110 iowrite16(0xaa55, mem
+ offset
);
111 iowrite16(0xaa55, mem
+ offset
+ 0x100);
113 test1
= ioread16(mem
+ offset
);
114 test2
= ioread16(mem
);
116 iowrite16(orig1
, mem
+ offset
);
117 iowrite16(orig2
, mem
+ offset
+ 0x100);
119 if (test1
!= 0xaa55) {
128 iowrite16(orig
, mem
);
129 return offset
- 65536;
132 /* Map the framebuffer from the card and configure the core */
133 static int mga_vram_init(struct mga_device
*mdev
)
136 struct apertures_struct
*aper
= alloc_apertures(1);
139 mdev
->mc
.vram_base
= pci_resource_start(mdev
->dev
->pdev
, 0);
140 mdev
->mc
.vram_window
= pci_resource_len(mdev
->dev
->pdev
, 0);
142 aper
->ranges
[0].base
= mdev
->mc
.vram_base
;
143 aper
->ranges
[0].size
= mdev
->mc
.vram_window
;
146 remove_conflicting_framebuffers(aper
, "mgafb", true);
148 if (!request_mem_region(mdev
->mc
.vram_base
, mdev
->mc
.vram_window
,
150 DRM_ERROR("can't reserve VRAM\n");
154 mem
= pci_iomap(mdev
->dev
->pdev
, 0, 0);
156 mdev
->mc
.vram_size
= mga_probe_vram(mdev
, mem
);
158 pci_iounmap(mdev
->dev
->pdev
, mem
);
163 static int mgag200_device_init(struct drm_device
*dev
,
166 struct mga_device
*mdev
= dev
->dev_private
;
171 /* Hardcode the number of CRTCs to 1 */
174 pci_read_config_dword(dev
->pdev
, PCI_MGA_OPTION
, &option
);
175 mdev
->has_sdram
= !(option
& (1 << 14));
177 /* BAR 0 is the framebuffer, BAR 1 contains registers */
178 mdev
->rmmio_base
= pci_resource_start(mdev
->dev
->pdev
, 1);
179 mdev
->rmmio_size
= pci_resource_len(mdev
->dev
->pdev
, 1);
181 if (!request_mem_region(mdev
->rmmio_base
, mdev
->rmmio_size
,
183 DRM_ERROR("can't reserve mmio registers\n");
187 mdev
->rmmio
= pci_iomap(dev
->pdev
, 1, 0);
188 if (mdev
->rmmio
== NULL
)
191 /* stash G200 SE model number for later use */
192 if (IS_G200_SE(mdev
))
193 mdev
->reg_1e24
= RREG32(0x1e24);
195 ret
= mga_vram_init(mdev
);
197 release_mem_region(mdev
->rmmio_base
, mdev
->rmmio_size
);
201 mdev
->bpp_shifts
[0] = 0;
202 mdev
->bpp_shifts
[1] = 1;
203 mdev
->bpp_shifts
[2] = 0;
204 mdev
->bpp_shifts
[3] = 2;
208 void mgag200_device_fini(struct mga_device
*mdev
)
210 release_mem_region(mdev
->rmmio_base
, mdev
->rmmio_size
);
215 * Functions here will be called by the core once it's bound the driver to
220 int mgag200_driver_load(struct drm_device
*dev
, unsigned long flags
)
222 struct mga_device
*mdev
;
225 mdev
= kzalloc(sizeof(struct mga_device
), GFP_KERNEL
);
228 dev
->dev_private
= (void *)mdev
;
231 r
= mgag200_device_init(dev
, flags
);
233 dev_err(&dev
->pdev
->dev
, "Fatal error during GPU init: %d\n", r
);
236 r
= mgag200_mm_init(mdev
);
240 drm_mode_config_init(dev
);
241 dev
->mode_config
.funcs
= (void *)&mga_mode_funcs
;
242 dev
->mode_config
.min_width
= 0;
243 dev
->mode_config
.min_height
= 0;
244 dev
->mode_config
.preferred_depth
= 24;
245 dev
->mode_config
.prefer_shadow
= 1;
247 r
= mgag200_modeset_init(mdev
);
249 dev_err(&dev
->pdev
->dev
, "Fatal error during modeset init: %d\n", r
);
252 mgag200_driver_unload(dev
);
256 int mgag200_driver_unload(struct drm_device
*dev
)
258 struct mga_device
*mdev
= dev
->dev_private
;
262 mgag200_modeset_fini(mdev
);
263 mgag200_fbdev_fini(mdev
);
264 drm_mode_config_cleanup(dev
);
265 mgag200_mm_fini(mdev
);
266 mgag200_device_fini(mdev
);
268 dev
->dev_private
= NULL
;
272 int mgag200_gem_create(struct drm_device
*dev
,
273 u32 size
, bool iskernel
,
274 struct drm_gem_object
**obj
)
276 struct mgag200_bo
*astbo
;
281 size
= roundup(size
, PAGE_SIZE
);
285 ret
= mgag200_bo_create(dev
, size
, 0, 0, &astbo
);
287 if (ret
!= -ERESTARTSYS
)
288 DRM_ERROR("failed to allocate GEM object\n");
295 int mgag200_dumb_create(struct drm_file
*file
,
296 struct drm_device
*dev
,
297 struct drm_mode_create_dumb
*args
)
300 struct drm_gem_object
*gobj
;
303 args
->pitch
= args
->width
* ((args
->bpp
+ 7) / 8);
304 args
->size
= args
->pitch
* args
->height
;
306 ret
= mgag200_gem_create(dev
, args
->size
, false,
311 ret
= drm_gem_handle_create(file
, gobj
, &handle
);
312 drm_gem_object_unreference_unlocked(gobj
);
316 args
->handle
= handle
;
320 int mgag200_dumb_destroy(struct drm_file
*file
,
321 struct drm_device
*dev
,
324 return drm_gem_handle_delete(file
, handle
);
327 int mgag200_gem_init_object(struct drm_gem_object
*obj
)
333 void mgag200_bo_unref(struct mgag200_bo
**bo
)
335 struct ttm_buffer_object
*tbo
;
347 void mgag200_gem_free_object(struct drm_gem_object
*obj
)
349 struct mgag200_bo
*mgag200_bo
= gem_to_mga_bo(obj
);
353 mgag200_bo_unref(&mgag200_bo
);
357 static inline u64
mgag200_bo_mmap_offset(struct mgag200_bo
*bo
)
359 return bo
->bo
.addr_space_offset
;
363 mgag200_dumb_mmap_offset(struct drm_file
*file
,
364 struct drm_device
*dev
,
368 struct drm_gem_object
*obj
;
370 struct mgag200_bo
*bo
;
372 mutex_lock(&dev
->struct_mutex
);
373 obj
= drm_gem_object_lookup(dev
, file
, handle
);
379 bo
= gem_to_mga_bo(obj
);
380 *offset
= mgag200_bo_mmap_offset(bo
);
382 drm_gem_object_unreference(obj
);
385 mutex_unlock(&dev
->struct_mutex
);