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 	return 0;
81 
82 err_mgag200_driver_unload:
83 	mgag200_driver_unload(dev);
84 err_drm_dev_put:
85 	drm_dev_put(dev);
86 err_pci_disable_device:
87 	pci_disable_device(pdev);
88 	return ret;
89 }
90 
91 static void mga_pci_remove(struct pci_dev *pdev)
92 {
93 	struct drm_device *dev = pci_get_drvdata(pdev);
94 
95 	drm_dev_unregister(dev);
96 	mgag200_driver_unload(dev);
97 	drm_dev_put(dev);
98 }
99 
100 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
101 
102 static bool mgag200_pin_bo_at_0(const struct mga_device *mdev)
103 {
104 	if (mgag200_hw_bug_no_startadd > 0) {
105 		DRM_WARN_ONCE("Option hw_bug_no_startradd is enabled. Please "
106 			      "report the output of 'lspci -vvnn' to "
107 			      "<dri-devel@lists.freedesktop.org> if this "
108 			      "option is required to make mgag200 work "
109 			      "correctly on your system.\n");
110 		return true;
111 	} else if (!mgag200_hw_bug_no_startadd) {
112 		return false;
113 	}
114 	return mdev->flags & MGAG200_FLAG_HW_BUG_NO_STARTADD;
115 }
116 
117 int mgag200_driver_dumb_create(struct drm_file *file,
118 			       struct drm_device *dev,
119 			       struct drm_mode_create_dumb *args)
120 {
121 	struct mga_device *mdev = dev->dev_private;
122 	unsigned long pg_align;
123 
124 	if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
125 		return -EINVAL;
126 
127 	pg_align = 0ul;
128 
129 	/*
130 	 * Aligning scanout buffers to the size of the video ram forces
131 	 * placement at offset 0. Works around a bug where HW does not
132 	 * respect 'startadd' field.
133 	 */
134 	if (mgag200_pin_bo_at_0(mdev))
135 		pg_align = PFN_UP(mdev->mc.vram_size);
136 
137 	return drm_gem_vram_fill_create_dumb(file, dev, pg_align, 0, args);
138 }
139 
140 static struct drm_driver driver = {
141 	.driver_features = DRIVER_GEM | DRIVER_MODESET,
142 	.fops = &mgag200_driver_fops,
143 	.name = DRIVER_NAME,
144 	.desc = DRIVER_DESC,
145 	.date = DRIVER_DATE,
146 	.major = DRIVER_MAJOR,
147 	.minor = DRIVER_MINOR,
148 	.patchlevel = DRIVER_PATCHLEVEL,
149 	.debugfs_init = drm_vram_mm_debugfs_init,
150 	.dumb_create = mgag200_driver_dumb_create,
151 	.dumb_map_offset = drm_gem_vram_driver_dumb_mmap_offset,
152 	.gem_prime_mmap = drm_gem_prime_mmap,
153 };
154 
155 static struct pci_driver mgag200_pci_driver = {
156 	.name = DRIVER_NAME,
157 	.id_table = pciidlist,
158 	.probe = mga_pci_probe,
159 	.remove = mga_pci_remove,
160 };
161 
162 static int __init mgag200_init(void)
163 {
164 	if (vgacon_text_force() && mgag200_modeset == -1)
165 		return -EINVAL;
166 
167 	if (mgag200_modeset == 0)
168 		return -EINVAL;
169 
170 	return pci_register_driver(&mgag200_pci_driver);
171 }
172 
173 static void __exit mgag200_exit(void)
174 {
175 	pci_unregister_driver(&mgag200_pci_driver);
176 }
177 
178 module_init(mgag200_init);
179 module_exit(mgag200_exit);
180 
181 MODULE_AUTHOR(DRIVER_AUTHOR);
182 MODULE_DESCRIPTION(DRIVER_DESC);
183 MODULE_LICENSE("GPL");
184