blob: e278b6a547bde6fb8e3b8e3b5f7b93f9e8348d56 [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
Thomas Zimmermann09daa2e2019-12-03 11:04:00 +010011#include <linux/pci.h>
12
David Howells760285e2012-10-02 18:01:07 +010013#include <drm/drm_crtc_helper.h>
Thomas Zimmermann5d177182019-06-27 10:09:09 +020014#include <drm/drm_gem_framebuffer_helper.h>
Sam Ravnborg9f397802019-06-23 12:35:42 +020015
Dave Airlie414c4532012-04-17 15:01:25 +010016#include "mgag200_drv.h"
17
Dave Airlie414c4532012-04-17 15:01:25 +010018static const struct drm_mode_config_funcs mga_mode_funcs = {
Thomas Zimmermann5d177182019-06-27 10:09:09 +020019 .fb_create = drm_gem_fb_create
Dave Airlie414c4532012-04-17 15:01:25 +010020};
21
Dave Airlie414c4532012-04-17 15:01:25 +010022static int mga_probe_vram(struct mga_device *mdev, void __iomem *mem)
23{
24 int offset;
25 int orig;
26 int test1, test2;
27 int orig1, orig2;
Mathieu Larouche6d857c12015-08-21 09:24:05 -040028 unsigned int vram_size;
Dave Airlie414c4532012-04-17 15:01:25 +010029
30 /* Probe */
31 orig = ioread16(mem);
32 iowrite16(0, mem);
33
Mathieu Larouche6d857c12015-08-21 09:24:05 -040034 vram_size = mdev->mc.vram_window;
35
36 if ((mdev->type == G200_EW3) && (vram_size >= 0x1000000)) {
37 vram_size = vram_size - 0x400000;
38 }
39
40 for (offset = 0x100000; offset < vram_size; offset += 0x4000) {
Dave Airlie414c4532012-04-17 15:01:25 +010041 orig1 = ioread8(mem + offset);
42 orig2 = ioread8(mem + offset + 0x100);
43
44 iowrite16(0xaa55, mem + offset);
45 iowrite16(0xaa55, mem + offset + 0x100);
46
47 test1 = ioread16(mem + offset);
48 test2 = ioread16(mem);
49
50 iowrite16(orig1, mem + offset);
51 iowrite16(orig2, mem + offset + 0x100);
52
53 if (test1 != 0xaa55) {
54 break;
55 }
56
57 if (test2) {
58 break;
59 }
60 }
61
62 iowrite16(orig, mem);
63 return offset - 65536;
64}
65
66/* Map the framebuffer from the card and configure the core */
67static int mga_vram_init(struct mga_device *mdev)
68{
69 void __iomem *mem;
Dave Airlie414c4532012-04-17 15:01:25 +010070
71 /* BAR 0 is VRAM */
72 mdev->mc.vram_base = pci_resource_start(mdev->dev->pdev, 0);
73 mdev->mc.vram_window = pci_resource_len(mdev->dev->pdev, 0);
74
Christopher Harveyc2ed8842013-04-05 16:15:30 +000075 if (!devm_request_mem_region(mdev->dev->dev, mdev->mc.vram_base, mdev->mc.vram_window,
Dave Airlie414c4532012-04-17 15:01:25 +010076 "mgadrmfb_vram")) {
77 DRM_ERROR("can't reserve VRAM\n");
78 return -ENXIO;
79 }
80
81 mem = pci_iomap(mdev->dev->pdev, 0, 0);
Arvind Yadav4b0ea932017-01-03 17:00:27 +053082 if (!mem)
83 return -ENOMEM;
Dave Airlie414c4532012-04-17 15:01:25 +010084
85 mdev->mc.vram_size = mga_probe_vram(mdev, mem);
86
87 pci_iounmap(mdev->dev->pdev, mem);
88
89 return 0;
90}
91
92static int mgag200_device_init(struct drm_device *dev,
93 uint32_t flags)
94{
95 struct mga_device *mdev = dev->dev_private;
96 int ret, option;
97
Thomas Zimmermannd6d437d2019-11-26 11:15:28 +010098 mdev->flags = mgag200_flags_from_driver_data(flags);
Thomas Zimmermann3a8a5aba2019-11-26 11:15:27 +010099 mdev->type = mgag200_type_from_driver_data(flags);
Dave Airlie414c4532012-04-17 15:01:25 +0100100
101 /* Hardcode the number of CRTCs to 1 */
102 mdev->num_crtc = 1;
103
104 pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option);
105 mdev->has_sdram = !(option & (1 << 14));
106
107 /* BAR 0 is the framebuffer, BAR 1 contains registers */
108 mdev->rmmio_base = pci_resource_start(mdev->dev->pdev, 1);
109 mdev->rmmio_size = pci_resource_len(mdev->dev->pdev, 1);
110
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000111 if (!devm_request_mem_region(mdev->dev->dev, mdev->rmmio_base, mdev->rmmio_size,
Dave Airlie414c4532012-04-17 15:01:25 +0100112 "mgadrmfb_mmio")) {
113 DRM_ERROR("can't reserve mmio registers\n");
114 return -ENOMEM;
115 }
116
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000117 mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
Dave Airlie414c4532012-04-17 15:01:25 +0100118 if (mdev->rmmio == NULL)
119 return -ENOMEM;
120
121 /* stash G200 SE model number for later use */
Thomas Zimmermannc0826f42019-12-04 14:34:35 +0100122 if (IS_G200_SE(mdev)) {
Julia Lemireabbee622013-06-27 13:38:59 -0400123 mdev->unique_rev_id = RREG32(0x1e24);
Thomas Zimmermannc0826f42019-12-04 14:34:35 +0100124 DRM_DEBUG("G200 SE unique revision id is 0x%x\n",
125 mdev->unique_rev_id);
126 }
Dave Airlie414c4532012-04-17 15:01:25 +0100127
128 ret = mga_vram_init(mdev);
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000129 if (ret)
Dave Airlie414c4532012-04-17 15:01:25 +0100130 return ret;
Dave Airlie414c4532012-04-17 15:01:25 +0100131
132 mdev->bpp_shifts[0] = 0;
133 mdev->bpp_shifts[1] = 1;
134 mdev->bpp_shifts[2] = 0;
135 mdev->bpp_shifts[3] = 2;
136 return 0;
137}
138
Dave Airlie414c4532012-04-17 15:01:25 +0100139/*
140 * Functions here will be called by the core once it's bound the driver to
141 * a PCI device
142 */
143
144
145int mgag200_driver_load(struct drm_device *dev, unsigned long flags)
146{
147 struct mga_device *mdev;
148 int r;
149
Christopher Harveyc2ed8842013-04-05 16:15:30 +0000150 mdev = devm_kzalloc(dev->dev, sizeof(struct mga_device), GFP_KERNEL);
Dave Airlie414c4532012-04-17 15:01:25 +0100151 if (mdev == NULL)
152 return -ENOMEM;
153 dev->dev_private = (void *)mdev;
154 mdev->dev = dev;
155
156 r = mgag200_device_init(dev, flags);
157 if (r) {
158 dev_err(&dev->pdev->dev, "Fatal error during GPU init: %d\n", r);
Christopher Harvey27911972013-06-05 11:29:57 -0400159 return r;
Dave Airlie414c4532012-04-17 15:01:25 +0100160 }
161 r = mgag200_mm_init(mdev);
162 if (r)
Archit Taneja728f8662015-09-17 16:30:55 +0530163 goto err_mm;
Dave Airlie414c4532012-04-17 15:01:25 +0100164
165 drm_mode_config_init(dev);
166 dev->mode_config.funcs = (void *)&mga_mode_funcs;
Thomas Zimmermann2c51a662019-09-27 11:13:00 +0200167 if (IS_G200_SE(mdev) && mdev->vram_fb_available < (2048*1024))
Dave Airlie918be882014-01-21 01:47:46 -0500168 dev->mode_config.preferred_depth = 16;
169 else
Thomas Zimmermann90f479a2019-07-03 09:58:42 +0200170 dev->mode_config.preferred_depth = 32;
Dave Airlie414c4532012-04-17 15:01:25 +0100171 dev->mode_config.prefer_shadow = 1;
172
173 r = mgag200_modeset_init(mdev);
Christopher Harveya080db92013-06-05 15:24:26 -0400174 if (r) {
Dave Airlie414c4532012-04-17 15:01:25 +0100175 dev_err(&dev->pdev->dev, "Fatal error during modeset init: %d\n", r);
Archit Taneja728f8662015-09-17 16:30:55 +0530176 goto err_modeset;
Christopher Harveya080db92013-06-05 15:24:26 -0400177 }
178
Thomas Zimmermann49b8d5a2019-09-27 11:12:56 +0200179 r = mgag200_cursor_init(mdev);
180 if (r)
Archit Taneja728f8662015-09-17 16:30:55 +0530181 dev_warn(&dev->pdev->dev,
Thomas Zimmermann49b8d5a2019-09-27 11:12:56 +0200182 "Could not initialize cursors. Not doing hardware cursors.\n");
Christopher Harveya080db92013-06-05 15:24:26 -0400183
Thomas Zimmermann90f479a2019-07-03 09:58:42 +0200184 r = drm_fbdev_generic_setup(mdev->dev, 0);
185 if (r)
186 goto err_modeset;
187
Archit Taneja728f8662015-09-17 16:30:55 +0530188 return 0;
189
190err_modeset:
191 drm_mode_config_cleanup(dev);
Thomas Zimmermann49b8d5a2019-09-27 11:12:56 +0200192 mgag200_cursor_fini(mdev);
Archit Taneja728f8662015-09-17 16:30:55 +0530193 mgag200_mm_fini(mdev);
194err_mm:
195 dev->dev_private = NULL;
196
Dave Airlie414c4532012-04-17 15:01:25 +0100197 return r;
198}
199
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200200void mgag200_driver_unload(struct drm_device *dev)
Dave Airlie414c4532012-04-17 15:01:25 +0100201{
202 struct mga_device *mdev = dev->dev_private;
203
204 if (mdev == NULL)
Gabriel Krisman Bertazi11b3c202017-01-06 15:57:31 -0200205 return;
Dave Airlie414c4532012-04-17 15:01:25 +0100206 mgag200_modeset_fini(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +0100207 drm_mode_config_cleanup(dev);
Thomas Zimmermann49b8d5a2019-09-27 11:12:56 +0200208 mgag200_cursor_fini(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +0100209 mgag200_mm_fini(mdev);
Dave Airlie414c4532012-04-17 15:01:25 +0100210 dev->dev_private = NULL;
Dave Airlie414c4532012-04-17 15:01:25 +0100211}