blob: 517c5693ad6995c710e88a986c1f3d0100e0a520 [file] [log] [blame]
Thomas Gleixnerc51669e2019-05-31 01:09:37 -07001// SPDX-License-Identifier: GPL-2.0-only
Dave Airlie414c4532012-04-17 15:01:25 +01002/*
3 * Copyright 2010 Matt Turner.
4 * Copyright 2012 Red Hat
5 *
Dave Airlie414c4532012-04-17 15:01:25 +01006 * Authors: Matthew Garrett
7 * Matt Turner
8 * Dave Airlie
9 */
Sam Ravnborg9f397802019-06-23 12:35:42 +020010
David Howells760285e2012-10-02 18:01:07 +010011#include <drm/drm_crtc_helper.h>
Thomas Zimmermann5d177182019-06-27 10:09:09 +020012#include <drm/drm_gem_framebuffer_helper.h>
Sam Ravnborg9f397802019-06-23 12:35:42 +020013#include <drm/drm_pci.h>
14
Dave Airlie414c4532012-04-17 15:01:25 +010015#include "mgag200_drv.h"
16
Dave Airlie414c4532012-04-17 15:01:25 +010017static const struct drm_mode_config_funcs mga_mode_funcs = {
Thomas Zimmermann5d177182019-06-27 10:09:09 +020018 .fb_create = drm_gem_fb_create
Dave Airlie414c4532012-04-17 15:01:25 +010019};
20
Dave Airlie414c4532012-04-17 15:01:25 +010021static int mga_probe_vram(struct mga_device *mdev, void __iomem *mem)
22{
23 int offset;
24 int orig;
25 int test1, test2;
26 int orig1, orig2;
Mathieu Larouche6d857c12015-08-21 09:24:05 -040027 unsigned int vram_size;
Dave Airlie414c4532012-04-17 15:01:25 +010028
29 /* Probe */
30 orig = ioread16(mem);
31 iowrite16(0, mem);
32
Mathieu Larouche6d857c12015-08-21 09:24:05 -040033 vram_size = mdev->mc.vram_window;
34
35 if ((mdev->type == G200_EW3) && (vram_size >= 0x1000000)) {
36 vram_size = vram_size - 0x400000;
37 }
38
39 for (offset = 0x100000; offset < vram_size; offset += 0x4000) {
Dave Airlie414c4532012-04-17 15:01:25 +010040 orig1 = ioread8(mem + offset);
41 orig2 = ioread8(mem + offset + 0x100);
42
43 iowrite16(0xaa55, mem + offset);
44 iowrite16(0xaa55, mem + offset + 0x100);
45
46 test1 = ioread16(mem + offset);
47 test2 = ioread16(mem);
48
49 iowrite16(orig1, mem + offset);
50 iowrite16(orig2, mem + offset + 0x100);
51
52 if (test1 != 0xaa55) {
53 break;
54 }
55
56 if (test2) {
57 break;
58 }
59 }
60
61 iowrite16(orig, mem);
62 return offset - 65536;
63}
64
65/* Map the framebuffer from the card and configure the core */
66static int mga_vram_init(struct mga_device *mdev)
67{
68 void __iomem *mem;
Dave Airlie414c4532012-04-17 15:01:25 +010069
70 /* BAR 0 is VRAM */
71 mdev->mc.vram_base = pci_resource_start(mdev->dev->pdev, 0);
72 mdev->mc.vram_window = pci_resource_len(mdev->dev->pdev, 0);
73
Christopher Harveyc2ed8842013-04-05 16:15:30 +000074 if (!devm_request_mem_region(mdev->dev->dev, mdev->mc.vram_base, mdev->mc.vram_window,
Dave Airlie414c4532012-04-17 15:01:25 +010075 "mgadrmfb_vram")) {
76 DRM_ERROR("can't reserve VRAM\n");
77 return -ENXIO;
78 }
79
80 mem = pci_iomap(mdev->dev->pdev, 0, 0);
Arvind Yadav4b0ea932017-01-03 17:00:27 +053081 if (!mem)
82 return -ENOMEM;
Dave Airlie414c4532012-04-17 15:01:25 +010083
84 mdev->mc.vram_size = mga_probe_vram(mdev, mem);
85
86 pci_iounmap(mdev->dev->pdev, mem);
87
88 return 0;
89}
90
91static int mgag200_device_init(struct drm_device *dev,
92 uint32_t flags)
93{
94 struct mga_device *mdev = dev->dev_private;
95 int ret, option;
96
Thomas Zimmermann3a8a5aba2019-11-26 11:15:27 +010097 mdev->type = mgag200_type_from_driver_data(flags);
Dave Airlie414c4532012-04-17 15:01:25 +010098
99 /* Hardcode the number of CRTCs to 1 */
100 mdev->num_crtc = 1;
101
102 pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option);
103 mdev->has_sdram = !(option & (1 << 14));
104
105 /* BAR 0 is the framebuffer, BAR 1 contains registers */
106 mdev->rmmio_base = pci_resource_start(mdev->dev->pdev, 1);
107 mdev->rmmio_size = pci_resource_len(mdev->dev->pdev, 1);
108
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000109 if (!devm_request_mem_region(mdev->dev->dev, mdev->rmmio_base, mdev->rmmio_size,
Dave Airlie414c4532012-04-17 15:01:25 +0100110 "mgadrmfb_mmio")) {
111 DRM_ERROR("can't reserve mmio registers\n");
112 return -ENOMEM;
113 }
114
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000115 mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
Dave Airlie414c4532012-04-17 15:01:25 +0100116 if (mdev->rmmio == NULL)
117 return -ENOMEM;
118
119 /* stash G200 SE model number for later use */
120 if (IS_G200_SE(mdev))
Julia Lemireabbee622013-06-27 13:38:59 -0400121 mdev->unique_rev_id = RREG32(0x1e24);
Dave Airlie414c4532012-04-17 15:01:25 +0100122
123 ret = mga_vram_init(mdev);
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000124 if (ret)
Dave Airlie414c4532012-04-17 15:01:25 +0100125 return ret;
Dave Airlie414c4532012-04-17 15:01:25 +0100126
127 mdev->bpp_shifts[0] = 0;
128 mdev->bpp_shifts[1] = 1;
129 mdev->bpp_shifts[2] = 0;
130 mdev->bpp_shifts[3] = 2;
131 return 0;
132}
133
Dave Airlie414c4532012-04-17 15:01:25 +0100134/*
135 * Functions here will be called by the core once it's bound the driver to
136 * a PCI device
137 */
138
139
140int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
141{
142 struct mga_device *mdev;
143 int r;
144
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000145 mdev = devm_kzalloc(dev->dev, sizeof(struct mga_device), GFP_KERNEL);
Dave Airlie414c4532012-04-17 15:01:25 +0100146 if (mdev == NULL)
147 return -ENOMEM;
148 dev->dev_private = (void *)mdev;
149 mdev->dev = dev;
150
151 r = mgag200_device_init(dev, flags);
152 if (r) {
153 dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
Christopher Harvey27911972013-06-05 11:29:57 -0400154 return r;
Dave Airlie414c4532012-04-17 15:01:25 +0100155 }
156 r = mgag200_mm_init(mdev);
157 if (r)
Archit Taneja728f8662015-09-17 16:30:55 +0530158 goto err_mm;
Dave Airlie414c4532012-04-17 15:01:25 +0100159
160 drm_mode_config_init(dev);
161 dev->mode_config.funcs = (void *)&mga_mode_funcs;
Thomas Zimmermann2c51a662019-09-27 11:13:00 +0200162 if (IS_G200_SE(mdev) && mdev->vram_fb_available < (2048*1024))
Dave Airlie918be882014-01-21 01:47:46 -0500163 dev->mode_config.preferred_depth = 16;
164 else
Thomas Zimmermann90f479a2019-07-03 09:58:42 +0200165 dev->mode_config.preferred_depth = 32;
Dave Airlie414c4532012-04-17 15:01:25 +0100166 dev->mode_config.prefer_shadow = 1;
167
168 r = mgag200_modeset_init(mdev);
Christopher Harveya080db92013-06-05 15:24:26 -0400169 if (r) {
Dave Airlie414c4532012-04-17 15:01:25 +0100170 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
Archit Taneja728f8662015-09-17 16:30:55 +0530171 goto err_modeset;
Christopher Harveya080db92013-06-05 15:24:26 -0400172 }
173
Thomas Zimmermann49b8d5a2019-09-27 11:12:56 +0200174 r = mgag200_cursor_init(mdev);
175 if (r)
Archit Taneja728f8662015-09-17 16:30:55 +0530176 dev_warn(&dev->pdev->dev,
Thomas Zimmermann49b8d5a2019-09-27 11:12:56 +0200177 "Could not initialize cursors. Not doing hardware cursors.\n");
Christopher Harveya080db92013-06-05 15:24:26 -0400178
Thomas Zimmermann90f479a2019-07-03 09:58:42 +0200179 r = drm_fbdev_generic_setup(mdev->dev, 0);
180 if (r)
181 goto err_modeset;
182
Archit Taneja728f8662015-09-17 16:30:55 +0530183 return 0;
184
185err_modeset:
186 drm_mode_config_cleanup(dev);
Thomas Zimmermann49b8d5a2019-09-27 11:12:56 +0200187 mgag200_cursor_fini(mdev);
Archit Taneja728f8662015-09-17 16:30:55 +0530188 mgag200_mm_fini(mdev);
189err_mm:
190 dev->dev_private = NULL;
191
Dave Airlie414c4532012-04-17 15:01:25 +0100192 return r;
193}
194
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200195void mgag200_driver_unload(struct drm_device *dev)
Dave Airlie414c4532012-04-17 15:01:25 +0100196{
197 struct mga_device *mdev = dev->dev_private;
198
199 if (mdev == NULL)
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200200 return;
Dave Airlie414c4532012-04-17 15:01:25 +0100201 mgag200_modeset_fini(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +0100202 drm_mode_config_cleanup(dev);
Thomas Zimmermann49b8d5a2019-09-27 11:12:56 +0200203 mgag200_cursor_fini(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +0100204 mgag200_mm_fini(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +0100205 dev->dev_private = NULL;
Dave Airlie414c4532012-04-17 15:01:25 +0100206}