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/module.h>
10 #include <linux/console.h>
11 
12 #include <drm/drm_drv.h>
13 #include <drm/drm_file.h>
14 #include <drm/drm_ioctl.h>
15 #include <drm/drm_pci.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 static struct drm_driver driver;
31 
32 static const struct pci_device_id pciidlist[] = {
33 	{ PCI_VENDOR_ID_MATROX, 0x522, PCI_VENDOR_ID_SUN, 0x4852, 0, 0,
34 		G200_SE_A | MGAG200_FLAG_HW_BUG_NO_STARTADD},
35 	{ PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_A },
36 	{ PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B },
37 	{ PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV },
38 	{ PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB },
39 	{ PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH },
40 	{ PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER },
41 	{ PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 },
42 	{ PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 },
43 	{0,}
44 };
45 
46 MODULE_DEVICE_TABLE(pci, pciidlist);
47 
48 
49 static int mga_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
50 {
51 	drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "mgag200drmfb");
52 
53 	return drm_get_pci_dev(pdev, ent, &driver);
54 }
55 
56 static void mga_pci_remove(struct pci_dev *pdev)
57 {
58 	struct drm_device *dev = pci_get_drvdata(pdev);
59 
60 	drm_put_dev(dev);
61 }
62 
63 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
64 
65 static bool mgag200_pin_bo_at_0(const struct mga_device *mdev)
66 {
67 	return mdev->flags & MGAG200_FLAG_HW_BUG_NO_STARTADD;
68 }
69 
70 int mgag200_driver_dumb_create(struct drm_file *file,
71 			       struct drm_device *dev,
72 			       struct drm_mode_create_dumb *args)
73 {
74 	struct mga_device *mdev = dev->dev_private;
75 	unsigned long pg_align;
76 
77 	if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
78 		return -EINVAL;
79 
80 	pg_align = 0ul;
81 
82 	/*
83 	 * Aligning scanout buffers to the size of the video ram forces
84 	 * placement at offset 0. Works around a bug where HW does not
85 	 * respect 'startadd' field.
86 	 */
87 	if (mgag200_pin_bo_at_0(mdev))
88 		pg_align = PFN_UP(mdev->mc.vram_size);
89 
90 	return drm_gem_vram_fill_create_dumb(file, dev, &dev->vram_mm->bdev,
91 					     pg_align, false, args);
92 }
93 
94 static struct drm_driver driver = {
95 	.driver_features = DRIVER_GEM | DRIVER_MODESET,
96 	.load = mgag200_driver_load,
97 	.unload = mgag200_driver_unload,
98 	.fops = &mgag200_driver_fops,
99 	.name = DRIVER_NAME,
100 	.desc = DRIVER_DESC,
101 	.date = DRIVER_DATE,
102 	.major = DRIVER_MAJOR,
103 	.minor = DRIVER_MINOR,
104 	.patchlevel = DRIVER_PATCHLEVEL,
105 	.debugfs_init = drm_vram_mm_debugfs_init,
106 	.dumb_create = mgag200_driver_dumb_create,
107 	.dumb_map_offset = drm_gem_vram_driver_dumb_mmap_offset,
108 	.gem_prime_mmap = drm_gem_prime_mmap,
109 };
110 
111 static struct pci_driver mgag200_pci_driver = {
112 	.name = DRIVER_NAME,
113 	.id_table = pciidlist,
114 	.probe = mga_pci_probe,
115 	.remove = mga_pci_remove,
116 };
117 
118 static int __init mgag200_init(void)
119 {
120 	if (vgacon_text_force() && mgag200_modeset == -1)
121 		return -EINVAL;
122 
123 	if (mgag200_modeset == 0)
124 		return -EINVAL;
125 
126 	return pci_register_driver(&mgag200_pci_driver);
127 }
128 
129 static void __exit mgag200_exit(void)
130 {
131 	pci_unregister_driver(&mgag200_pci_driver);
132 }
133 
134 module_init(mgag200_init);
135 module_exit(mgag200_exit);
136 
137 MODULE_AUTHOR(DRIVER_AUTHOR);
138 MODULE_DESCRIPTION(DRIVER_DESC);
139 MODULE_LICENSE("GPL");
140