drm/amdgpu: add core driver (v4)
[deliverable/linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_drv.c
1 /**
2 * \file amdgpu_drv.c
3 * AMD Amdgpu driver
4 *
5 * \author Gareth Hughes <gareth@valinux.com>
6 */
7
8 /*
9 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
10 * All Rights Reserved.
11 *
12 * Permission is hereby granted, free of charge, to any person obtaining a
13 * copy of this software and associated documentation files (the "Software"),
14 * to deal in the Software without restriction, including without limitation
15 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 * and/or sell copies of the Software, and to permit persons to whom the
17 * Software is furnished to do so, subject to the following conditions:
18 *
19 * The above copyright notice and this permission notice (including the next
20 * paragraph) shall be included in all copies or substantial portions of the
21 * Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * VA LINUX SYSTEMS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
27 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
28 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
29 * OTHER DEALINGS IN THE SOFTWARE.
30 */
31
32 #include <drm/drmP.h>
33 #include <drm/amdgpu_drm.h>
34 #include <drm/drm_gem.h>
35 #include "amdgpu_drv.h"
36
37 #include <drm/drm_pciids.h>
38 #include <linux/console.h>
39 #include <linux/module.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/vga_switcheroo.h>
42 #include "drm_crtc_helper.h"
43
44 #include "amdgpu.h"
45 #include "amdgpu_irq.h"
46
47 /*
48 * KMS wrapper.
49 * - 3.0.0 - initial driver
50 */
51 #define KMS_DRIVER_MAJOR 3
52 #define KMS_DRIVER_MINOR 0
53 #define KMS_DRIVER_PATCHLEVEL 0
54
55 int amdgpu_vram_limit = 0;
56 int amdgpu_gart_size = -1; /* auto */
57 int amdgpu_benchmarking = 0;
58 int amdgpu_testing = 0;
59 int amdgpu_audio = -1;
60 int amdgpu_disp_priority = 0;
61 int amdgpu_hw_i2c = 0;
62 int amdgpu_pcie_gen2 = -1;
63 int amdgpu_msi = -1;
64 int amdgpu_lockup_timeout = 10000;
65 int amdgpu_dpm = -1;
66 int amdgpu_smc_load_fw = 1;
67 int amdgpu_aspm = -1;
68 int amdgpu_runtime_pm = -1;
69 int amdgpu_hard_reset = 0;
70 unsigned amdgpu_ip_block_mask = 0xffffffff;
71 int amdgpu_bapm = -1;
72 int amdgpu_deep_color = 0;
73 int amdgpu_vm_size = 8;
74 int amdgpu_vm_block_size = -1;
75 int amdgpu_exp_hw_support = 0;
76
77 MODULE_PARM_DESC(vramlimit, "Restrict VRAM for testing, in megabytes");
78 module_param_named(vramlimit, amdgpu_vram_limit, int, 0600);
79
80 MODULE_PARM_DESC(gartsize, "Size of PCIE/IGP gart to setup in megabytes (32, 64, etc., -1 = auto)");
81 module_param_named(gartsize, amdgpu_gart_size, int, 0600);
82
83 MODULE_PARM_DESC(benchmark, "Run benchmark");
84 module_param_named(benchmark, amdgpu_benchmarking, int, 0444);
85
86 MODULE_PARM_DESC(test, "Run tests");
87 module_param_named(test, amdgpu_testing, int, 0444);
88
89 MODULE_PARM_DESC(audio, "Audio enable (-1 = auto, 0 = disable, 1 = enable)");
90 module_param_named(audio, amdgpu_audio, int, 0444);
91
92 MODULE_PARM_DESC(disp_priority, "Display Priority (0 = auto, 1 = normal, 2 = high)");
93 module_param_named(disp_priority, amdgpu_disp_priority, int, 0444);
94
95 MODULE_PARM_DESC(hw_i2c, "hw i2c engine enable (0 = disable)");
96 module_param_named(hw_i2c, amdgpu_hw_i2c, int, 0444);
97
98 MODULE_PARM_DESC(pcie_gen2, "PCIE Gen2 mode (-1 = auto, 0 = disable, 1 = enable)");
99 module_param_named(pcie_gen2, amdgpu_pcie_gen2, int, 0444);
100
101 MODULE_PARM_DESC(msi, "MSI support (1 = enable, 0 = disable, -1 = auto)");
102 module_param_named(msi, amdgpu_msi, int, 0444);
103
104 MODULE_PARM_DESC(lockup_timeout, "GPU lockup timeout in ms (defaul 10000 = 10 seconds, 0 = disable)");
105 module_param_named(lockup_timeout, amdgpu_lockup_timeout, int, 0444);
106
107 MODULE_PARM_DESC(dpm, "DPM support (1 = enable, 0 = disable, -1 = auto)");
108 module_param_named(dpm, amdgpu_dpm, int, 0444);
109
110 MODULE_PARM_DESC(smc_load_fw, "SMC firmware loading(1 = enable, 0 = disable)");
111 module_param_named(smc_load_fw, amdgpu_smc_load_fw, int, 0444);
112
113 MODULE_PARM_DESC(aspm, "ASPM support (1 = enable, 0 = disable, -1 = auto)");
114 module_param_named(aspm, amdgpu_aspm, int, 0444);
115
116 MODULE_PARM_DESC(runpm, "PX runtime pm (1 = force enable, 0 = disable, -1 = PX only default)");
117 module_param_named(runpm, amdgpu_runtime_pm, int, 0444);
118
119 MODULE_PARM_DESC(hard_reset, "PCI config reset (1 = force enable, 0 = disable (default))");
120 module_param_named(hard_reset, amdgpu_hard_reset, int, 0444);
121
122 MODULE_PARM_DESC(ip_block_mask, "IP Block Mask (all blocks enabled (default))");
123 module_param_named(ip_block_mask, amdgpu_ip_block_mask, uint, 0444);
124
125 MODULE_PARM_DESC(bapm, "BAPM support (1 = enable, 0 = disable, -1 = auto)");
126 module_param_named(bapm, amdgpu_bapm, int, 0444);
127
128 MODULE_PARM_DESC(deep_color, "Deep Color support (1 = enable, 0 = disable (default))");
129 module_param_named(deep_color, amdgpu_deep_color, int, 0444);
130
131 MODULE_PARM_DESC(vm_size, "VM address space size in gigabytes (default 4GB)");
132 module_param_named(vm_size, amdgpu_vm_size, int, 0444);
133
134 MODULE_PARM_DESC(vm_block_size, "VM page table size in bits (default depending on vm_size)");
135 module_param_named(vm_block_size, amdgpu_vm_block_size, int, 0444);
136
137 MODULE_PARM_DESC(exp_hw_support, "experimental hw support (1 = enable, 0 = disable (default))");
138 module_param_named(exp_hw_support, amdgpu_exp_hw_support, int, 0444);
139
140 static struct pci_device_id pciidlist[] = {
141
142 {0, 0, 0}
143 };
144
145 MODULE_DEVICE_TABLE(pci, pciidlist);
146
147 static struct drm_driver kms_driver;
148
149 static int amdgpu_kick_out_firmware_fb(struct pci_dev *pdev)
150 {
151 struct apertures_struct *ap;
152 bool primary = false;
153
154 ap = alloc_apertures(1);
155 if (!ap)
156 return -ENOMEM;
157
158 ap->ranges[0].base = pci_resource_start(pdev, 0);
159 ap->ranges[0].size = pci_resource_len(pdev, 0);
160
161 #ifdef CONFIG_X86
162 primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
163 #endif
164 remove_conflicting_framebuffers(ap, "amdgpudrmfb", primary);
165 kfree(ap);
166
167 return 0;
168 }
169
170 static int amdgpu_pci_probe(struct pci_dev *pdev,
171 const struct pci_device_id *ent)
172 {
173 unsigned long flags = ent->driver_data;
174 int ret;
175
176 if ((flags & AMDGPU_EXP_HW_SUPPORT) && !amdgpu_exp_hw_support) {
177 DRM_INFO("This hardware requires experimental hardware support.\n"
178 "See modparam exp_hw_support\n");
179 return -ENODEV;
180 }
181
182 /* Get rid of things like offb */
183 ret = amdgpu_kick_out_firmware_fb(pdev);
184 if (ret)
185 return ret;
186
187 return drm_get_pci_dev(pdev, ent, &kms_driver);
188 }
189
190 static void
191 amdgpu_pci_remove(struct pci_dev *pdev)
192 {
193 struct drm_device *dev = pci_get_drvdata(pdev);
194
195 drm_put_dev(dev);
196 }
197
198 static int amdgpu_pmops_suspend(struct device *dev)
199 {
200 struct pci_dev *pdev = to_pci_dev(dev);
201 struct drm_device *drm_dev = pci_get_drvdata(pdev);
202 return amdgpu_suspend_kms(drm_dev, true, true);
203 }
204
205 static int amdgpu_pmops_resume(struct device *dev)
206 {
207 struct pci_dev *pdev = to_pci_dev(dev);
208 struct drm_device *drm_dev = pci_get_drvdata(pdev);
209 return amdgpu_resume_kms(drm_dev, true, true);
210 }
211
212 static int amdgpu_pmops_freeze(struct device *dev)
213 {
214 struct pci_dev *pdev = to_pci_dev(dev);
215 struct drm_device *drm_dev = pci_get_drvdata(pdev);
216 return amdgpu_suspend_kms(drm_dev, false, true);
217 }
218
219 static int amdgpu_pmops_thaw(struct device *dev)
220 {
221 struct pci_dev *pdev = to_pci_dev(dev);
222 struct drm_device *drm_dev = pci_get_drvdata(pdev);
223 return amdgpu_resume_kms(drm_dev, false, true);
224 }
225
226 static int amdgpu_pmops_runtime_suspend(struct device *dev)
227 {
228 struct pci_dev *pdev = to_pci_dev(dev);
229 struct drm_device *drm_dev = pci_get_drvdata(pdev);
230 int ret;
231
232 if (!amdgpu_device_is_px(drm_dev)) {
233 pm_runtime_forbid(dev);
234 return -EBUSY;
235 }
236
237 drm_dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
238 drm_kms_helper_poll_disable(drm_dev);
239 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
240
241 ret = amdgpu_suspend_kms(drm_dev, false, false);
242 pci_save_state(pdev);
243 pci_disable_device(pdev);
244 pci_ignore_hotplug(pdev);
245 pci_set_power_state(pdev, PCI_D3cold);
246 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
247
248 return 0;
249 }
250
251 static int amdgpu_pmops_runtime_resume(struct device *dev)
252 {
253 struct pci_dev *pdev = to_pci_dev(dev);
254 struct drm_device *drm_dev = pci_get_drvdata(pdev);
255 int ret;
256
257 if (!amdgpu_device_is_px(drm_dev))
258 return -EINVAL;
259
260 drm_dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
261
262 pci_set_power_state(pdev, PCI_D0);
263 pci_restore_state(pdev);
264 ret = pci_enable_device(pdev);
265 if (ret)
266 return ret;
267 pci_set_master(pdev);
268
269 ret = amdgpu_resume_kms(drm_dev, false, false);
270 drm_kms_helper_poll_enable(drm_dev);
271 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
272 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
273 return 0;
274 }
275
276 static int amdgpu_pmops_runtime_idle(struct device *dev)
277 {
278 struct pci_dev *pdev = to_pci_dev(dev);
279 struct drm_device *drm_dev = pci_get_drvdata(pdev);
280 struct drm_crtc *crtc;
281
282 if (!amdgpu_device_is_px(drm_dev)) {
283 pm_runtime_forbid(dev);
284 return -EBUSY;
285 }
286
287 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
288 if (crtc->enabled) {
289 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
290 return -EBUSY;
291 }
292 }
293
294 pm_runtime_mark_last_busy(dev);
295 pm_runtime_autosuspend(dev);
296 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
297 return 1;
298 }
299
300 long amdgpu_drm_ioctl(struct file *filp,
301 unsigned int cmd, unsigned long arg)
302 {
303 struct drm_file *file_priv = filp->private_data;
304 struct drm_device *dev;
305 long ret;
306 dev = file_priv->minor->dev;
307 ret = pm_runtime_get_sync(dev->dev);
308 if (ret < 0)
309 return ret;
310
311 ret = drm_ioctl(filp, cmd, arg);
312
313 pm_runtime_mark_last_busy(dev->dev);
314 pm_runtime_put_autosuspend(dev->dev);
315 return ret;
316 }
317
318 static const struct dev_pm_ops amdgpu_pm_ops = {
319 .suspend = amdgpu_pmops_suspend,
320 .resume = amdgpu_pmops_resume,
321 .freeze = amdgpu_pmops_freeze,
322 .thaw = amdgpu_pmops_thaw,
323 .poweroff = amdgpu_pmops_freeze,
324 .restore = amdgpu_pmops_resume,
325 .runtime_suspend = amdgpu_pmops_runtime_suspend,
326 .runtime_resume = amdgpu_pmops_runtime_resume,
327 .runtime_idle = amdgpu_pmops_runtime_idle,
328 };
329
330 static const struct file_operations amdgpu_driver_kms_fops = {
331 .owner = THIS_MODULE,
332 .open = drm_open,
333 .release = drm_release,
334 .unlocked_ioctl = amdgpu_drm_ioctl,
335 .mmap = amdgpu_mmap,
336 .poll = drm_poll,
337 .read = drm_read,
338 #ifdef CONFIG_COMPAT
339 .compat_ioctl = amdgpu_kms_compat_ioctl,
340 #endif
341 };
342
343 static struct drm_driver kms_driver = {
344 .driver_features =
345 DRIVER_USE_AGP |
346 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |
347 DRIVER_PRIME | DRIVER_RENDER,
348 .dev_priv_size = 0,
349 .load = amdgpu_driver_load_kms,
350 .open = amdgpu_driver_open_kms,
351 .preclose = amdgpu_driver_preclose_kms,
352 .postclose = amdgpu_driver_postclose_kms,
353 .lastclose = amdgpu_driver_lastclose_kms,
354 .set_busid = drm_pci_set_busid,
355 .unload = amdgpu_driver_unload_kms,
356 .get_vblank_counter = amdgpu_get_vblank_counter_kms,
357 .enable_vblank = amdgpu_enable_vblank_kms,
358 .disable_vblank = amdgpu_disable_vblank_kms,
359 .get_vblank_timestamp = amdgpu_get_vblank_timestamp_kms,
360 .get_scanout_position = amdgpu_get_crtc_scanoutpos,
361 #if defined(CONFIG_DEBUG_FS)
362 .debugfs_init = amdgpu_debugfs_init,
363 .debugfs_cleanup = amdgpu_debugfs_cleanup,
364 #endif
365 .irq_preinstall = amdgpu_irq_preinstall,
366 .irq_postinstall = amdgpu_irq_postinstall,
367 .irq_uninstall = amdgpu_irq_uninstall,
368 .irq_handler = amdgpu_irq_handler,
369 .ioctls = amdgpu_ioctls_kms,
370 .gem_free_object = amdgpu_gem_object_free,
371 .gem_open_object = amdgpu_gem_object_open,
372 .gem_close_object = amdgpu_gem_object_close,
373 .dumb_create = amdgpu_mode_dumb_create,
374 .dumb_map_offset = amdgpu_mode_dumb_mmap,
375 .dumb_destroy = drm_gem_dumb_destroy,
376 .fops = &amdgpu_driver_kms_fops,
377
378 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
379 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
380 .gem_prime_export = amdgpu_gem_prime_export,
381 .gem_prime_import = drm_gem_prime_import,
382 .gem_prime_pin = amdgpu_gem_prime_pin,
383 .gem_prime_unpin = amdgpu_gem_prime_unpin,
384 .gem_prime_res_obj = amdgpu_gem_prime_res_obj,
385 .gem_prime_get_sg_table = amdgpu_gem_prime_get_sg_table,
386 .gem_prime_import_sg_table = amdgpu_gem_prime_import_sg_table,
387 .gem_prime_vmap = amdgpu_gem_prime_vmap,
388 .gem_prime_vunmap = amdgpu_gem_prime_vunmap,
389
390 .name = DRIVER_NAME,
391 .desc = DRIVER_DESC,
392 .date = DRIVER_DATE,
393 .major = KMS_DRIVER_MAJOR,
394 .minor = KMS_DRIVER_MINOR,
395 .patchlevel = KMS_DRIVER_PATCHLEVEL,
396 };
397
398 static struct drm_driver *driver;
399 static struct pci_driver *pdriver;
400
401 static struct pci_driver amdgpu_kms_pci_driver = {
402 .name = DRIVER_NAME,
403 .id_table = pciidlist,
404 .probe = amdgpu_pci_probe,
405 .remove = amdgpu_pci_remove,
406 .driver.pm = &amdgpu_pm_ops,
407 };
408
409 static int __init amdgpu_init(void)
410 {
411 #ifdef CONFIG_VGA_CONSOLE
412 if (vgacon_text_force()) {
413 DRM_ERROR("VGACON disables amdgpu kernel modesetting.\n");
414 return -EINVAL;
415 }
416 #endif
417 DRM_INFO("amdgpu kernel modesetting enabled.\n");
418 driver = &kms_driver;
419 pdriver = &amdgpu_kms_pci_driver;
420 driver->driver_features |= DRIVER_MODESET;
421 driver->num_ioctls = amdgpu_max_kms_ioctl;
422 amdgpu_register_atpx_handler();
423
424 /* let modprobe override vga console setting */
425 return drm_pci_init(driver, pdriver);
426 }
427
428 static void __exit amdgpu_exit(void)
429 {
430 drm_pci_exit(driver, pdriver);
431 amdgpu_unregister_atpx_handler();
432 }
433
434 module_init(amdgpu_init);
435 module_exit(amdgpu_exit);
436
437 MODULE_AUTHOR(DRIVER_AUTHOR);
438 MODULE_DESCRIPTION(DRIVER_DESC);
439 MODULE_LICENSE("GPL and additional rights");
This page took 0.051871 seconds and 5 git commands to generate.