blob: a701d956325717fb2bf99f0fc93cc325388f209e [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 2012 Red Hat
4 *
Dave Airlie414c4532012-04-17 15:01:25 +01005 * Authors: Matthew Garrett
6 * Dave Airlie
7 */
Sam Ravnborg9f397802019-06-23 12:35:42 +02008
Dave Airlie414c4532012-04-17 15:01:25 +01009#include <linux/console.h>
Thomas Zimmermann9623ecb2019-12-05 10:02:52 +010010#include <linux/module.h>
11#include <linux/pci.h>
Sam Ravnborg018315d2020-08-07 20:05:47 +020012#include <linux/vmalloc.h>
Sam Ravnborg9f397802019-06-23 12:35:42 +020013
Thomas Zimmermann6848c292021-04-12 15:10:42 +020014#include <drm/drm_aperture.h>
Sam Ravnborg9f397802019-06-23 12:35:42 +020015#include <drm/drm_drv.h>
16#include <drm/drm_file.h>
17#include <drm/drm_ioctl.h>
Sam Ravnborg9f397802019-06-23 12:35:42 +020018#include <drm/drm_pciids.h>
Dave Airlie414c4532012-04-17 15:01:25 +010019
20#include "mgag200_drv.h"
21
Thomas Zimmermann913ec472020-05-15 10:32:33 +020022int mgag200_modeset = -1;
Dave Airlie414c4532012-04-17 15:01:25 +010023MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
24module_param_named(modeset, mgag200_modeset, int, 0400);
25
Thomas Zimmermann09870622020-06-05 15:57:57 +020026/*
27 * DRM driver
28 */
29
30DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
31
Daniel Vetter70a59dd2020-11-04 11:04:24 +010032static const struct drm_driver mgag200_driver = {
Thomas Zimmermann09870622020-06-05 15:57:57 +020033 .driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
34 .fops = &mgag200_driver_fops,
35 .name = DRIVER_NAME,
36 .desc = DRIVER_DESC,
37 .date = DRIVER_DATE,
38 .major = DRIVER_MAJOR,
39 .minor = DRIVER_MINOR,
40 .patchlevel = DRIVER_PATCHLEVEL,
41 DRM_GEM_SHMEM_DRIVER_OPS,
42};
43
44/*
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +020045 * DRM device
46 */
47
Thomas Zimmermann42452162020-07-30 12:28:38 +020048static bool mgag200_has_sgram(struct mga_device *mdev)
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +020049{
Thomas Zimmermann832eddf2020-06-05 15:58:02 +020050 struct drm_device *dev = &mdev->base;
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +010051 struct pci_dev *pdev = to_pci_dev(dev->dev);
Thomas Zimmermann42452162020-07-30 12:28:38 +020052 u32 option;
53 int ret;
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +020054
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +010055 ret = pci_read_config_dword(pdev, PCI_MGA_OPTION, &option);
Thomas Zimmermann42452162020-07-30 12:28:38 +020056 if (drm_WARN(dev, ret, "failed to read PCI config dword: %d\n", ret))
57 return false;
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +020058
Thomas Zimmermann42452162020-07-30 12:28:38 +020059 return !!(option & PCI_MGA_OPTION_HARDPWMSK);
60}
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +020061
Thomas Zimmermann42452162020-07-30 12:28:38 +020062static int mgag200_regs_init(struct mga_device *mdev)
63{
64 struct drm_device *dev = &mdev->base;
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +010065 struct pci_dev *pdev = to_pci_dev(dev->dev);
Thomas Zimmermann20217082020-07-30 12:28:39 +020066 u32 option, option2;
Thomas Zimmermann78e5b502020-07-30 12:28:40 +020067 u8 crtcext3;
Thomas Zimmermann42452162020-07-30 12:28:38 +020068
Thomas Zimmermann20217082020-07-30 12:28:39 +020069 switch (mdev->type) {
Thomas Zimmermanne20dfd22020-07-30 12:28:44 +020070 case G200_PCI:
71 case G200_AGP:
72 if (mgag200_has_sgram(mdev))
73 option = 0x4049cd21;
74 else
75 option = 0x40499121;
76 option2 = 0x00008000;
77 break;
Thomas Zimmermann20217082020-07-30 12:28:39 +020078 case G200_SE_A:
79 case G200_SE_B:
Thomas Zimmermannd3dc1352020-08-04 08:51:58 +020080 option = 0x40049120;
Thomas Zimmermann20217082020-07-30 12:28:39 +020081 if (mgag200_has_sgram(mdev))
82 option |= PCI_MGA_OPTION_HARDPWMSK;
83 option2 = 0x00008000;
84 break;
85 case G200_WB:
86 case G200_EW3:
87 option = 0x41049120;
88 option2 = 0x0000b000;
89 break;
90 case G200_EV:
91 option = 0x00000120;
92 option2 = 0x0000b000;
93 break;
94 case G200_EH:
95 case G200_EH3:
96 option = 0x00000120;
97 option2 = 0x0000b000;
98 break;
99 default:
100 option = 0;
101 option2 = 0;
102 }
103
104 if (option)
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +0100105 pci_write_config_dword(pdev, PCI_MGA_OPTION, option);
Thomas Zimmermann20217082020-07-30 12:28:39 +0200106 if (option2)
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +0100107 pci_write_config_dword(pdev, PCI_MGA_OPTION2, option2);
Thomas Zimmermann42452162020-07-30 12:28:38 +0200108
109 /* BAR 1 contains registers */
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +0100110 mdev->rmmio_base = pci_resource_start(pdev, 1);
111 mdev->rmmio_size = pci_resource_len(pdev, 1);
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +0200112
113 if (!devm_request_mem_region(dev->dev, mdev->rmmio_base,
114 mdev->rmmio_size, "mgadrmfb_mmio")) {
115 drm_err(dev, "can't reserve mmio registers\n");
116 return -ENOMEM;
117 }
118
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +0100119 mdev->rmmio = pcim_iomap(pdev, 1, 0);
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +0200120 if (mdev->rmmio == NULL)
121 return -ENOMEM;
122
Thomas Zimmermann78e5b502020-07-30 12:28:40 +0200123 RREG_ECRT(0x03, crtcext3);
124 crtcext3 |= MGAREG_CRTCEXT3_MGAMODE;
125 WREG_ECRT(0x03, crtcext3);
126
Thomas Zimmermann42452162020-07-30 12:28:38 +0200127 return 0;
128}
129
Thomas Zimmermanne20dfd22020-07-30 12:28:44 +0200130static void mgag200_g200_interpret_bios(struct mga_device *mdev,
131 const unsigned char *bios,
132 size_t size)
133{
134 static const char matrox[] = {'M', 'A', 'T', 'R', 'O', 'X'};
135 static const unsigned int expected_length[6] = {
136 0, 64, 64, 64, 128, 128
137 };
138 struct drm_device *dev = &mdev->base;
139 const unsigned char *pins;
140 unsigned int pins_len, version;
141 int offset;
142 int tmp;
143
144 /* Test for MATROX string. */
145 if (size < 45 + sizeof(matrox))
146 return;
147 if (memcmp(&bios[45], matrox, sizeof(matrox)) != 0)
148 return;
149
150 /* Get the PInS offset. */
151 if (size < MGA_BIOS_OFFSET + 2)
152 return;
153 offset = (bios[MGA_BIOS_OFFSET + 1] << 8) | bios[MGA_BIOS_OFFSET];
154
155 /* Get PInS data structure. */
156
157 if (size < offset + 6)
158 return;
159 pins = bios + offset;
160 if (pins[0] == 0x2e && pins[1] == 0x41) {
161 version = pins[5];
162 pins_len = pins[2];
163 } else {
164 version = 1;
165 pins_len = pins[0] + (pins[1] << 8);
166 }
167
168 if (version < 1 || version > 5) {
169 drm_warn(dev, "Unknown BIOS PInS version: %d\n", version);
170 return;
171 }
172 if (pins_len != expected_length[version]) {
Colin Ian Kingcd6da0b2020-08-26 09:47:27 +0100173 drm_warn(dev, "Unexpected BIOS PInS size: %d expected: %d\n",
Thomas Zimmermanne20dfd22020-07-30 12:28:44 +0200174 pins_len, expected_length[version]);
175 return;
176 }
177 if (size < offset + pins_len)
178 return;
179
180 drm_dbg_kms(dev, "MATROX BIOS PInS version %d size: %d found\n",
181 version, pins_len);
182
183 /* Extract the clock values */
184
185 switch (version) {
186 case 1:
187 tmp = pins[24] + (pins[25] << 8);
188 if (tmp)
189 mdev->model.g200.pclk_max = tmp * 10;
190 break;
191 case 2:
192 if (pins[41] != 0xff)
193 mdev->model.g200.pclk_max = (pins[41] + 100) * 1000;
194 break;
195 case 3:
196 if (pins[36] != 0xff)
197 mdev->model.g200.pclk_max = (pins[36] + 100) * 1000;
198 if (pins[52] & 0x20)
199 mdev->model.g200.ref_clk = 14318;
200 break;
201 case 4:
202 if (pins[39] != 0xff)
203 mdev->model.g200.pclk_max = pins[39] * 4 * 1000;
204 if (pins[92] & 0x01)
205 mdev->model.g200.ref_clk = 14318;
206 break;
207 case 5:
208 tmp = pins[4] ? 8000 : 6000;
209 if (pins[123] != 0xff)
210 mdev->model.g200.pclk_min = pins[123] * tmp;
211 if (pins[38] != 0xff)
212 mdev->model.g200.pclk_max = pins[38] * tmp;
213 if (pins[110] & 0x01)
214 mdev->model.g200.ref_clk = 14318;
215 break;
216 default:
217 break;
218 }
219}
220
221static void mgag200_g200_init_refclk(struct mga_device *mdev)
222{
223 struct drm_device *dev = &mdev->base;
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +0100224 struct pci_dev *pdev = to_pci_dev(dev->dev);
Thomas Zimmermanne20dfd22020-07-30 12:28:44 +0200225 unsigned char __iomem *rom;
226 unsigned char *bios;
227 size_t size;
228
229 mdev->model.g200.pclk_min = 50000;
230 mdev->model.g200.pclk_max = 230000;
231 mdev->model.g200.ref_clk = 27050;
232
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +0100233 rom = pci_map_rom(pdev, &size);
Thomas Zimmermanne20dfd22020-07-30 12:28:44 +0200234 if (!rom)
235 return;
236
237 bios = vmalloc(size);
238 if (!bios)
239 goto out;
240 memcpy_fromio(bios, rom, size);
241
242 if (size != 0 && bios[0] == 0x55 && bios[1] == 0xaa)
243 mgag200_g200_interpret_bios(mdev, bios, size);
244
245 drm_dbg_kms(dev, "pclk_min: %ld pclk_max: %ld ref_clk: %ld\n",
246 mdev->model.g200.pclk_min, mdev->model.g200.pclk_max,
247 mdev->model.g200.ref_clk);
248
249 vfree(bios);
250out:
Thomas Zimmermann0e6aadc2020-12-01 11:35:34 +0100251 pci_unmap_rom(pdev, rom);
Thomas Zimmermanne20dfd22020-07-30 12:28:44 +0200252}
253
Thomas Zimmermannfb188252020-07-30 12:28:43 +0200254static void mgag200_g200se_init_unique_id(struct mga_device *mdev)
255{
256 struct drm_device *dev = &mdev->base;
257
258 /* stash G200 SE model number for later use */
259 mdev->model.g200se.unique_rev_id = RREG32(0x1e24);
260
261 drm_dbg(dev, "G200 SE unique revision id is 0x%x\n",
262 mdev->model.g200se.unique_rev_id);
263}
264
Thomas Zimmermann42452162020-07-30 12:28:38 +0200265static int mgag200_device_init(struct mga_device *mdev, unsigned long flags)
266{
267 struct drm_device *dev = &mdev->base;
268 int ret;
269
270 mdev->flags = mgag200_flags_from_driver_data(flags);
271 mdev->type = mgag200_type_from_driver_data(flags);
272
273 ret = mgag200_regs_init(mdev);
274 if (ret)
275 return ret;
276
Thomas Zimmermanne20dfd22020-07-30 12:28:44 +0200277 if (mdev->type == G200_PCI || mdev->type == G200_AGP)
278 mgag200_g200_init_refclk(mdev);
279 else if (IS_G200_SE(mdev))
Thomas Zimmermannfb188252020-07-30 12:28:43 +0200280 mgag200_g200se_init_unique_id(mdev);
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +0200281
282 ret = mgag200_mm_init(mdev);
283 if (ret)
Thomas Zimmermannc714dd92020-06-05 15:58:00 +0200284 return ret;
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +0200285
286 ret = mgag200_modeset_init(mdev);
287 if (ret) {
288 drm_err(dev, "Fatal error during modeset init: %d\n", ret);
Thomas Zimmermannc714dd92020-06-05 15:58:00 +0200289 return ret;
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +0200290 }
291
292 return 0;
Thomas Zimmermannc714dd92020-06-05 15:58:00 +0200293}
294
Thomas Zimmermann3c8923c2020-06-05 15:58:01 +0200295static struct mga_device *
Thomas Zimmermann832eddf2020-06-05 15:58:02 +0200296mgag200_device_create(struct pci_dev *pdev, unsigned long flags)
Thomas Zimmermannc714dd92020-06-05 15:58:00 +0200297{
Thomas Zimmermann3c8923c2020-06-05 15:58:01 +0200298 struct drm_device *dev;
Thomas Zimmermannc714dd92020-06-05 15:58:00 +0200299 struct mga_device *mdev;
300 int ret;
301
Thomas Zimmermannfb7ba0c2020-06-05 15:58:03 +0200302 mdev = devm_drm_dev_alloc(&pdev->dev, &mgag200_driver,
303 struct mga_device, base);
304 if (IS_ERR(mdev))
305 return mdev;
Thomas Zimmermann832eddf2020-06-05 15:58:02 +0200306 dev = &mdev->base;
307
Thomas Zimmermann3c8923c2020-06-05 15:58:01 +0200308 pci_set_drvdata(pdev, dev);
309
Thomas Zimmermannc714dd92020-06-05 15:58:00 +0200310 ret = mgag200_device_init(mdev, flags);
311 if (ret)
Thomas Zimmermannfb7ba0c2020-06-05 15:58:03 +0200312 return ERR_PTR(ret);
Thomas Zimmermannc714dd92020-06-05 15:58:00 +0200313
Thomas Zimmermann3c8923c2020-06-05 15:58:01 +0200314 return mdev;
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +0200315}
316
Thomas Zimmermannba5b90e82020-06-05 15:57:59 +0200317/*
Thomas Zimmermann09870622020-06-05 15:57:57 +0200318 * PCI driver
319 */
Dave Airlie414c4532012-04-17 15:01:25 +0100320
Thomas Zimmermannfdcb6b12020-06-05 15:57:58 +0200321static const struct pci_device_id mgag200_pciidlist[] = {
Thomas Zimmermanne20dfd22020-07-30 12:28:44 +0200322 { PCI_VENDOR_ID_MATROX, 0x520, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_PCI },
323 { PCI_VENDOR_ID_MATROX, 0x521, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_AGP },
Thomas Zimmermann4adf0b42019-12-06 09:19:01 +0100324 { PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
Thomas Zimmermann1591fad2019-11-26 11:15:29 +0100325 G200_SE_A | MGAG200_FLAG_HW_BUG_NO_STARTADD},
Dave Airlie414c4532012-04-17 15:01:25 +0100326 { PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B },
327 { PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV },
328 { PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB },
329 { PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH },
330 { PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER },
Mathieu Larouche6d857c12015-08-21 09:24:05 -0400331 { PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 },
Mathieu Larouchef0493e62016-10-21 12:47:07 -0400332 { PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 },
Dave Airlie414c4532012-04-17 15:01:25 +0100333 {0,}
334};
335
Thomas Zimmermannfdcb6b12020-06-05 15:57:58 +0200336MODULE_DEVICE_TABLE(pci, mgag200_pciidlist);
Dave Airlie414c4532012-04-17 15:01:25 +0100337
Thomas Zimmermannfdcb6b12020-06-05 15:57:58 +0200338static int
339mgag200_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Dave Airlie414c4532012-04-17 15:01:25 +0100340{
Thomas Zimmermann3c8923c2020-06-05 15:58:01 +0200341 struct mga_device *mdev;
Thomas Zimmermann9623ecb2019-12-05 10:02:52 +0100342 struct drm_device *dev;
343 int ret;
344
Thomas Zimmermann6848c292021-04-12 15:10:42 +0200345 ret = drm_aperture_remove_conflicting_pci_framebuffers(pdev, "mgag200drmfb");
346 if (ret)
347 return ret;
Dave Airlie08ef8e42012-06-01 11:12:39 +0100348
Thomas Zimmermann044e0932020-06-05 15:57:52 +0200349 ret = pcim_enable_device(pdev);
Thomas Zimmermann9623ecb2019-12-05 10:02:52 +0100350 if (ret)
351 return ret;
352
Thomas Zimmermann832eddf2020-06-05 15:58:02 +0200353 mdev = mgag200_device_create(pdev, ent->driver_data);
Thomas Zimmermannfb7ba0c2020-06-05 15:58:03 +0200354 if (IS_ERR(mdev))
355 return PTR_ERR(mdev);
Thomas Zimmermann832eddf2020-06-05 15:58:02 +0200356 dev = &mdev->base;
Thomas Zimmermann9623ecb2019-12-05 10:02:52 +0100357
358 ret = drm_dev_register(dev, ent->driver_data);
359 if (ret)
Thomas Zimmermannfb7ba0c2020-06-05 15:58:03 +0200360 return ret;
Thomas Zimmermann9623ecb2019-12-05 10:02:52 +0100361
Thomas Zimmermannfc540482020-04-08 10:26:37 +0200362 drm_fbdev_generic_setup(dev, 0);
363
Thomas Zimmermann9623ecb2019-12-05 10:02:52 +0100364 return 0;
Dave Airlie414c4532012-04-17 15:01:25 +0100365}
366
Thomas Zimmermannfdcb6b12020-06-05 15:57:58 +0200367static void mgag200_pci_remove(struct pci_dev *pdev)
Dave Airlie414c4532012-04-17 15:01:25 +0100368{
369 struct drm_device *dev = pci_get_drvdata(pdev);
370
Thomas Zimmermann9623ecb2019-12-05 10:02:52 +0100371 drm_dev_unregister(dev);
Dave Airlie414c4532012-04-17 15:01:25 +0100372}
373
Dave Airlie414c4532012-04-17 15:01:25 +0100374static struct pci_driver mgag200_pci_driver = {
375 .name = DRIVER_NAME,
Thomas Zimmermannfdcb6b12020-06-05 15:57:58 +0200376 .id_table = mgag200_pciidlist,
377 .probe = mgag200_pci_probe,
378 .remove = mgag200_pci_remove,
Dave Airlie414c4532012-04-17 15:01:25 +0100379};
380
381static int __init mgag200_init(void)
382{
383 if (vgacon_text_force() && mgag200_modeset == -1)
384 return -EINVAL;
Dave Airlie414c4532012-04-17 15:01:25 +0100385
386 if (mgag200_modeset == 0)
387 return -EINVAL;
Daniel Vetter10631d72017-05-24 16:51:40 +0200388
389 return pci_register_driver(&mgag200_pci_driver);
Dave Airlie414c4532012-04-17 15:01:25 +0100390}
391
392static void __exit mgag200_exit(void)
393{
Daniel Vetter10631d72017-05-24 16:51:40 +0200394 pci_unregister_driver(&mgag200_pci_driver);
Dave Airlie414c4532012-04-17 15:01:25 +0100395}
396
397module_init(mgag200_init);
398module_exit(mgag200_exit);
399
400MODULE_AUTHOR(DRIVER_AUTHOR);
401MODULE_DESCRIPTION(DRIVER_DESC);
402MODULE_LICENSE("GPL");