1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2012 Red Hat
4  *
5  * Authors: Matthew Garrett
6  *          Dave Airlie
7  */
8 
9 #include <linux/console.h>
10 #include <linux/module.h>
11 #include <linux/pci.h>
12 
13 #include <drm/drm_drv.h>
14 #include <drm/drm_file.h>
15 #include <drm/drm_ioctl.h>
16 #include <drm/drm_pciids.h>
17 
18 #include "mgag200_drv.h"
19 
20 /*
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
23  * functions
24  */
25 int mgag200_modeset = -1;
26 
27 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
28 module_param_named(modeset, mgag200_modeset, int, 0400);
29 
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);
33 
34 static struct drm_driver driver;
35 
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 },
46 	{0,}
47 };
48 
49 MODULE_DEVICE_TABLE(pci, pciidlist);
50 
51 
52 static int mga_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
53 {
54 	struct drm_device *dev;
55 	int ret;
56 
57 	drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "mgag200drmfb");
58 
59 	ret = pci_enable_device(pdev);
60 	if (ret)
61 		return ret;
62 
63 	dev = drm_dev_alloc(&driver, &pdev->dev);
64 	if (IS_ERR(dev)) {
65 		ret = PTR_ERR(dev);
66 		goto err_pci_disable_device;
67 	}
68 
69 	dev->pdev = pdev;
70 	pci_set_drvdata(pdev, dev);
71 
72 	ret = mgag200_driver_load(dev, ent->driver_data);
73 	if (ret)
74 		goto err_drm_dev_put;
75 
76 	ret = drm_dev_register(dev, ent->driver_data);
77 	if (ret)
78 		goto err_mgag200_driver_unload;
79 
80 	drm_fbdev_generic_setup(dev, 0);
81 
82 	return 0;
83 
84 err_mgag200_driver_unload:
85 	mgag200_driver_unload(dev);
86 err_drm_dev_put:
87 	drm_dev_put(dev);
88 err_pci_disable_device:
89 	pci_disable_device(pdev);
90 	return ret;
91 }
92 
93 static void mga_pci_remove(struct pci_dev *pdev)
94 {
95 	struct drm_device *dev = pci_get_drvdata(pdev);
96 
97 	drm_dev_unregister(dev);
98 	mgag200_driver_unload(dev);
99 	drm_dev_put(dev);
100 }
101 
102 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
103 
104 static bool mgag200_pin_bo_at_0(const struct mga_device *mdev)
105 {
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");
112 		return true;
113 	} else if (!mgag200_hw_bug_no_startadd) {
114 		return false;
115 	}
116 	return mdev->flags & MGAG200_FLAG_HW_BUG_NO_STARTADD;
117 }
118 
119 int mgag200_driver_dumb_create(struct drm_file *file,
120 			       struct drm_device *dev,
121 			       struct drm_mode_create_dumb *args)
122 {
123 	struct mga_device *mdev = to_mga_device(dev);
124 	unsigned long pg_align;
125 
126 	if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
127 		return -EINVAL;
128 
129 	pg_align = 0ul;
130 
131 	/*
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.
135 	 */
136 	if (mgag200_pin_bo_at_0(mdev))
137 		pg_align = PFN_UP(mdev->mc.vram_size);
138 
139 	return drm_gem_vram_fill_create_dumb(file, dev, pg_align, 0, args);
140 }
141 
142 static struct drm_driver driver = {
143 	.driver_features = DRIVER_GEM | DRIVER_MODESET,
144 	.fops = &mgag200_driver_fops,
145 	.name = DRIVER_NAME,
146 	.desc = DRIVER_DESC,
147 	.date = DRIVER_DATE,
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,
155 };
156 
157 static struct pci_driver mgag200_pci_driver = {
158 	.name = DRIVER_NAME,
159 	.id_table = pciidlist,
160 	.probe = mga_pci_probe,
161 	.remove = mga_pci_remove,
162 };
163 
164 static int __init mgag200_init(void)
165 {
166 	if (vgacon_text_force() && mgag200_modeset == -1)
167 		return -EINVAL;
168 
169 	if (mgag200_modeset == 0)
170 		return -EINVAL;
171 
172 	return pci_register_driver(&mgag200_pci_driver);
173 }
174 
175 static void __exit mgag200_exit(void)
176 {
177 	pci_unregister_driver(&mgag200_pci_driver);
178 }
179 
180 module_init(mgag200_init);
181 module_exit(mgag200_exit);
182 
183 MODULE_AUTHOR(DRIVER_AUTHOR);
184 MODULE_DESCRIPTION(DRIVER_DESC);
185 MODULE_LICENSE("GPL");
186