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 int mgag200_modeset = -1;
21 MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
22 module_param_named(modeset, mgag200_modeset, int, 0400);
23 
24 /*
25  * DRM driver
26  */
27 
28 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
29 
30 static struct drm_driver mgag200_driver = {
31 	.driver_features = DRIVER_ATOMIC | DRIVER_GEM | DRIVER_MODESET,
32 	.fops = &mgag200_driver_fops,
33 	.name = DRIVER_NAME,
34 	.desc = DRIVER_DESC,
35 	.date = DRIVER_DATE,
36 	.major = DRIVER_MAJOR,
37 	.minor = DRIVER_MINOR,
38 	.patchlevel = DRIVER_PATCHLEVEL,
39 	DRM_GEM_SHMEM_DRIVER_OPS,
40 };
41 
42 /*
43  * DRM device
44  */
45 
46 static int mgag200_device_init(struct mga_device *mdev, unsigned long flags)
47 {
48 	struct drm_device *dev = &mdev->base;
49 	int ret, option;
50 
51 	mdev->flags = mgag200_flags_from_driver_data(flags);
52 	mdev->type = mgag200_type_from_driver_data(flags);
53 
54 	pci_read_config_dword(dev->pdev, PCI_MGA_OPTION, &option);
55 	mdev->has_sdram = !(option & (1 << 14));
56 
57 	/* BAR 0 is the framebuffer, BAR 1 contains registers */
58 	mdev->rmmio_base = pci_resource_start(dev->pdev, 1);
59 	mdev->rmmio_size = pci_resource_len(dev->pdev, 1);
60 
61 	if (!devm_request_mem_region(dev->dev, mdev->rmmio_base,
62 				     mdev->rmmio_size, "mgadrmfb_mmio")) {
63 		drm_err(dev, "can't reserve mmio registers\n");
64 		return -ENOMEM;
65 	}
66 
67 	mdev->rmmio = pcim_iomap(dev->pdev, 1, 0);
68 	if (mdev->rmmio == NULL)
69 		return -ENOMEM;
70 
71 	/* stash G200 SE model number for later use */
72 	if (IS_G200_SE(mdev)) {
73 		mdev->unique_rev_id = RREG32(0x1e24);
74 		drm_dbg(dev, "G200 SE unique revision id is 0x%x\n",
75 			mdev->unique_rev_id);
76 	}
77 
78 	ret = mgag200_mm_init(mdev);
79 	if (ret)
80 		return ret;
81 
82 	ret = mgag200_modeset_init(mdev);
83 	if (ret) {
84 		drm_err(dev, "Fatal error during modeset init: %d\n", ret);
85 		return ret;
86 	}
87 
88 	return 0;
89 }
90 
91 static struct mga_device *
92 mgag200_device_create(struct pci_dev *pdev, unsigned long flags)
93 {
94 	struct drm_device *dev;
95 	struct mga_device *mdev;
96 	int ret;
97 
98 	mdev = devm_drm_dev_alloc(&pdev->dev, &mgag200_driver,
99 				  struct mga_device, base);
100 	if (IS_ERR(mdev))
101 		return mdev;
102 	dev = &mdev->base;
103 
104 	dev->pdev = pdev;
105 	pci_set_drvdata(pdev, dev);
106 
107 	ret = mgag200_device_init(mdev, flags);
108 	if (ret)
109 		return ERR_PTR(ret);
110 
111 	return mdev;
112 }
113 
114 /*
115  * PCI driver
116  */
117 
118 static const struct pci_device_id mgag200_pciidlist[] = {
119 	{ PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
120 		G200_SE_A | MGAG200_FLAG_HW_BUG_NO_STARTADD},
121 	{ PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B },
122 	{ PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV },
123 	{ PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB },
124 	{ PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH },
125 	{ PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER },
126 	{ PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 },
127 	{ PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 },
128 	{0,}
129 };
130 
131 MODULE_DEVICE_TABLE(pci, mgag200_pciidlist);
132 
133 static int
134 mgag200_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
135 {
136 	struct mga_device *mdev;
137 	struct drm_device *dev;
138 	int ret;
139 
140 	drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "mgag200drmfb");
141 
142 	ret = pcim_enable_device(pdev);
143 	if (ret)
144 		return ret;
145 
146 	mdev = mgag200_device_create(pdev, ent->driver_data);
147 	if (IS_ERR(mdev))
148 		return PTR_ERR(mdev);
149 	dev = &mdev->base;
150 
151 	ret = drm_dev_register(dev, ent->driver_data);
152 	if (ret)
153 		return ret;
154 
155 	drm_fbdev_generic_setup(dev, 0);
156 
157 	return 0;
158 }
159 
160 static void mgag200_pci_remove(struct pci_dev *pdev)
161 {
162 	struct drm_device *dev = pci_get_drvdata(pdev);
163 
164 	drm_dev_unregister(dev);
165 }
166 
167 static struct pci_driver mgag200_pci_driver = {
168 	.name = DRIVER_NAME,
169 	.id_table = mgag200_pciidlist,
170 	.probe = mgag200_pci_probe,
171 	.remove = mgag200_pci_remove,
172 };
173 
174 static int __init mgag200_init(void)
175 {
176 	if (vgacon_text_force() && mgag200_modeset == -1)
177 		return -EINVAL;
178 
179 	if (mgag200_modeset == 0)
180 		return -EINVAL;
181 
182 	return pci_register_driver(&mgag200_pci_driver);
183 }
184 
185 static void __exit mgag200_exit(void)
186 {
187 	pci_unregister_driver(&mgag200_pci_driver);
188 }
189 
190 module_init(mgag200_init);
191 module_exit(mgag200_exit);
192 
193 MODULE_AUTHOR(DRIVER_AUTHOR);
194 MODULE_DESCRIPTION(DRIVER_DESC);
195 MODULE_LICENSE("GPL");
196