1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright 2012 Red Hat
5 * Authors: Matthew Garrett
9 #include <linux/console.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
13 #include <drm/drm_drv.h>
14 #include <drm/drm_file.h>
15 #include <drm/drm_ioctl.h>
16 #include <drm/drm_pciids.h>
18 #include "mgag200_drv.h"
21 * This is the generic driver code. This binds the driver to the drm core,
22 * which then performs further device association and calls our graphics init
25 int mgag200_modeset = -1;
27 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
28 module_param_named(modeset, mgag200_modeset, int, 0400);
30 int mgag200_hw_bug_no_startadd = -1;
31 MODULE_PARM_DESC(modeset, "HW does not interpret scanout-buffer start address correctly");
32 module_param_named(hw_bug_no_startadd, mgag200_hw_bug_no_startadd, int, 0400);
34 static struct drm_driver driver;
36 static const struct pci_device_id pciidlist[] = {
37 { PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
38 G200_SE_A | MGAG200_FLAG_HW_BUG_NO_STARTADD},
39 { PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B },
40 { PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV },
41 { PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB },
42 { PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH },
43 { PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER },
44 { PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 },
45 { PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 },
49 MODULE_DEVICE_TABLE(pci, pciidlist);
52 static int mga_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
54 struct drm_device *dev;
57 drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "mgag200drmfb");
59 ret = pci_enable_device(pdev);
63 dev = drm_dev_alloc(&driver, &pdev->dev);
66 goto err_pci_disable_device;
70 pci_set_drvdata(pdev, dev);
72 ret = mgag200_driver_load(dev, ent->driver_data);
76 ret = drm_dev_register(dev, ent->driver_data);
78 goto err_mgag200_driver_unload;
80 drm_fbdev_generic_setup(dev, 0);
84 err_mgag200_driver_unload:
85 mgag200_driver_unload(dev);
88 err_pci_disable_device:
89 pci_disable_device(pdev);
93 static void mga_pci_remove(struct pci_dev *pdev)
95 struct drm_device *dev = pci_get_drvdata(pdev);
97 drm_dev_unregister(dev);
98 mgag200_driver_unload(dev);
102 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
104 static bool mgag200_pin_bo_at_0(const struct mga_device *mdev)
106 if (mgag200_hw_bug_no_startadd > 0) {
107 DRM_WARN_ONCE("Option hw_bug_no_startradd is enabled. Please "
108 "report the output of 'lspci -vvnn' to "
109 "<dri-devel@lists.freedesktop.org> if this "
110 "option is required to make mgag200 work "
111 "correctly on your system.\n");
113 } else if (!mgag200_hw_bug_no_startadd) {
116 return mdev->flags & MGAG200_FLAG_HW_BUG_NO_STARTADD;
119 int mgag200_driver_dumb_create(struct drm_file *file,
120 struct drm_device *dev,
121 struct drm_mode_create_dumb *args)
123 struct mga_device *mdev = to_mga_device(dev);
124 unsigned long pg_align;
126 if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
132 * Aligning scanout buffers to the size of the video ram forces
133 * placement at offset 0. Works around a bug where HW does not
134 * respect 'startadd' field.
136 if (mgag200_pin_bo_at_0(mdev))
137 pg_align = PFN_UP(mdev->mc.vram_size);
139 return drm_gem_vram_fill_create_dumb(file, dev, pg_align, 0, args);
142 static struct drm_driver driver = {
143 .driver_features = DRIVER_GEM | DRIVER_MODESET,
144 .fops = &mgag200_driver_fops,
148 .major = DRIVER_MAJOR,
149 .minor = DRIVER_MINOR,
150 .patchlevel = DRIVER_PATCHLEVEL,
151 .debugfs_init = drm_vram_mm_debugfs_init,
152 .dumb_create = mgag200_driver_dumb_create,
153 .dumb_map_offset = drm_gem_vram_driver_dumb_mmap_offset,
154 .gem_prime_mmap = drm_gem_prime_mmap,
157 static struct pci_driver mgag200_pci_driver = {
159 .id_table = pciidlist,
160 .probe = mga_pci_probe,
161 .remove = mga_pci_remove,
164 static int __init mgag200_init(void)
166 if (vgacon_text_force() && mgag200_modeset == -1)
169 if (mgag200_modeset == 0)
172 return pci_register_driver(&mgag200_pci_driver);
175 static void __exit mgag200_exit(void)
177 pci_unregister_driver(&mgag200_pci_driver);
180 module_init(mgag200_init);
181 module_exit(mgag200_exit);
183 MODULE_AUTHOR(DRIVER_AUTHOR);
184 MODULE_DESCRIPTION(DRIVER_DESC);
185 MODULE_LICENSE("GPL");