xref: /linux/drivers/gpu/drm/mgag200/mgag200_drv.c (revision 6fdcba32711044c35c0e1b094cbd8f3f0b4472c9)
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 static struct drm_driver driver;
31 
32 static const struct pci_device_id pciidlist[] = {
33 	{ PCI_VENDOR_ID_MATROX, 0x522, PCI_ANY_ID, PCI_ANY_ID, 0, 0,
34 		G200_SE_A | MGAG200_FLAG_HW_BUG_NO_STARTADD},
35 	{ PCI_VENDOR_ID_MATROX, 0x524, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_SE_B },
36 	{ PCI_VENDOR_ID_MATROX, 0x530, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EV },
37 	{ PCI_VENDOR_ID_MATROX, 0x532, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_WB },
38 	{ PCI_VENDOR_ID_MATROX, 0x533, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH },
39 	{ PCI_VENDOR_ID_MATROX, 0x534, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_ER },
40 	{ PCI_VENDOR_ID_MATROX, 0x536, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EW3 },
41 	{ PCI_VENDOR_ID_MATROX, 0x538, PCI_ANY_ID, PCI_ANY_ID, 0, 0, G200_EH3 },
42 	{0,}
43 };
44 
45 MODULE_DEVICE_TABLE(pci, pciidlist);
46 
47 
48 static int mga_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
49 {
50 	struct drm_device *dev;
51 	int ret;
52 
53 	drm_fb_helper_remove_conflicting_pci_framebuffers(pdev, "mgag200drmfb");
54 
55 	ret = pci_enable_device(pdev);
56 	if (ret)
57 		return ret;
58 
59 	dev = drm_dev_alloc(&driver, &pdev->dev);
60 	if (IS_ERR(dev)) {
61 		ret = PTR_ERR(dev);
62 		goto err_pci_disable_device;
63 	}
64 
65 	dev->pdev = pdev;
66 	pci_set_drvdata(pdev, dev);
67 
68 	ret = mgag200_driver_load(dev, ent->driver_data);
69 	if (ret)
70 		goto err_drm_dev_put;
71 
72 	ret = drm_dev_register(dev, ent->driver_data);
73 	if (ret)
74 		goto err_mgag200_driver_unload;
75 
76 	return 0;
77 
78 err_mgag200_driver_unload:
79 	mgag200_driver_unload(dev);
80 err_drm_dev_put:
81 	drm_dev_put(dev);
82 err_pci_disable_device:
83 	pci_disable_device(pdev);
84 	return ret;
85 }
86 
87 static void mga_pci_remove(struct pci_dev *pdev)
88 {
89 	struct drm_device *dev = pci_get_drvdata(pdev);
90 
91 	drm_dev_unregister(dev);
92 	mgag200_driver_unload(dev);
93 	drm_dev_put(dev);
94 }
95 
96 DEFINE_DRM_GEM_FOPS(mgag200_driver_fops);
97 
98 static bool mgag200_pin_bo_at_0(const struct mga_device *mdev)
99 {
100 	return mdev->flags & MGAG200_FLAG_HW_BUG_NO_STARTADD;
101 }
102 
103 int mgag200_driver_dumb_create(struct drm_file *file,
104 			       struct drm_device *dev,
105 			       struct drm_mode_create_dumb *args)
106 {
107 	struct mga_device *mdev = dev->dev_private;
108 	unsigned long pg_align;
109 
110 	if (WARN_ONCE(!dev->vram_mm, "VRAM MM not initialized"))
111 		return -EINVAL;
112 
113 	pg_align = 0ul;
114 
115 	/*
116 	 * Aligning scanout buffers to the size of the video ram forces
117 	 * placement at offset 0. Works around a bug where HW does not
118 	 * respect 'startadd' field.
119 	 */
120 	if (mgag200_pin_bo_at_0(mdev))
121 		pg_align = PFN_UP(mdev->mc.vram_size);
122 
123 	return drm_gem_vram_fill_create_dumb(file, dev, &dev->vram_mm->bdev,
124 					     pg_align, false, args);
125 }
126 
127 static struct drm_driver driver = {
128 	.driver_features = DRIVER_GEM | DRIVER_MODESET,
129 	.fops = &mgag200_driver_fops,
130 	.name = DRIVER_NAME,
131 	.desc = DRIVER_DESC,
132 	.date = DRIVER_DATE,
133 	.major = DRIVER_MAJOR,
134 	.minor = DRIVER_MINOR,
135 	.patchlevel = DRIVER_PATCHLEVEL,
136 	.debugfs_init = drm_vram_mm_debugfs_init,
137 	.dumb_create = mgag200_driver_dumb_create,
138 	.dumb_map_offset = drm_gem_vram_driver_dumb_mmap_offset,
139 	.gem_prime_mmap = drm_gem_prime_mmap,
140 };
141 
142 static struct pci_driver mgag200_pci_driver = {
143 	.name = DRIVER_NAME,
144 	.id_table = pciidlist,
145 	.probe = mga_pci_probe,
146 	.remove = mga_pci_remove,
147 };
148 
149 static int __init mgag200_init(void)
150 {
151 	if (vgacon_text_force() && mgag200_modeset == -1)
152 		return -EINVAL;
153 
154 	if (mgag200_modeset == 0)
155 		return -EINVAL;
156 
157 	return pci_register_driver(&mgag200_pci_driver);
158 }
159 
160 static void __exit mgag200_exit(void)
161 {
162 	pci_unregister_driver(&mgag200_pci_driver);
163 }
164 
165 module_init(mgag200_init);
166 module_exit(mgag200_exit);
167 
168 MODULE_AUTHOR(DRIVER_AUTHOR);
169 MODULE_DESCRIPTION(DRIVER_DESC);
170 MODULE_LICENSE("GPL");
171