5 * \author Gareth Hughes <gareth@valinux.com>
9 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
10 * All Rights Reserved.
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:
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
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.
33 #include <drm/amdgpu_drm.h>
34 #include <drm/drm_gem.h>
35 #include "amdgpu_drv.h"
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"
45 #include "amdgpu_irq.h"
49 * - 3.0.0 - initial driver
51 #define KMS_DRIVER_MAJOR 3
52 #define KMS_DRIVER_MINOR 0
53 #define KMS_DRIVER_PATCHLEVEL 0
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;
64 int amdgpu_lockup_timeout
= 10000;
66 int amdgpu_smc_load_fw
= 1;
68 int amdgpu_runtime_pm
= -1;
69 int amdgpu_hard_reset
= 0;
70 unsigned amdgpu_ip_block_mask
= 0xffffffff;
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;
77 MODULE_PARM_DESC(vramlimit
, "Restrict VRAM for testing, in megabytes");
78 module_param_named(vramlimit
, amdgpu_vram_limit
, int, 0600);
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);
83 MODULE_PARM_DESC(benchmark
, "Run benchmark");
84 module_param_named(benchmark
, amdgpu_benchmarking
, int, 0444);
86 MODULE_PARM_DESC(test
, "Run tests");
87 module_param_named(test
, amdgpu_testing
, int, 0444);
89 MODULE_PARM_DESC(audio
, "Audio enable (-1 = auto, 0 = disable, 1 = enable)");
90 module_param_named(audio
, amdgpu_audio
, int, 0444);
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);
95 MODULE_PARM_DESC(hw_i2c
, "hw i2c engine enable (0 = disable)");
96 module_param_named(hw_i2c
, amdgpu_hw_i2c
, int, 0444);
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);
101 MODULE_PARM_DESC(msi
, "MSI support (1 = enable, 0 = disable, -1 = auto)");
102 module_param_named(msi
, amdgpu_msi
, int, 0444);
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);
107 MODULE_PARM_DESC(dpm
, "DPM support (1 = enable, 0 = disable, -1 = auto)");
108 module_param_named(dpm
, amdgpu_dpm
, int, 0444);
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);
113 MODULE_PARM_DESC(aspm
, "ASPM support (1 = enable, 0 = disable, -1 = auto)");
114 module_param_named(aspm
, amdgpu_aspm
, int, 0444);
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);
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);
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);
125 MODULE_PARM_DESC(bapm
, "BAPM support (1 = enable, 0 = disable, -1 = auto)");
126 module_param_named(bapm
, amdgpu_bapm
, int, 0444);
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);
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);
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);
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);
140 static struct pci_device_id pciidlist
[] = {
145 MODULE_DEVICE_TABLE(pci
, pciidlist
);
147 static struct drm_driver kms_driver
;
149 static int amdgpu_kick_out_firmware_fb(struct pci_dev
*pdev
)
151 struct apertures_struct
*ap
;
152 bool primary
= false;
154 ap
= alloc_apertures(1);
158 ap
->ranges
[0].base
= pci_resource_start(pdev
, 0);
159 ap
->ranges
[0].size
= pci_resource_len(pdev
, 0);
162 primary
= pdev
->resource
[PCI_ROM_RESOURCE
].flags
& IORESOURCE_ROM_SHADOW
;
164 remove_conflicting_framebuffers(ap
, "amdgpudrmfb", primary
);
170 static int amdgpu_pci_probe(struct pci_dev
*pdev
,
171 const struct pci_device_id
*ent
)
173 unsigned long flags
= ent
->driver_data
;
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");
182 /* Get rid of things like offb */
183 ret
= amdgpu_kick_out_firmware_fb(pdev
);
187 return drm_get_pci_dev(pdev
, ent
, &kms_driver
);
191 amdgpu_pci_remove(struct pci_dev
*pdev
)
193 struct drm_device
*dev
= pci_get_drvdata(pdev
);
198 static int amdgpu_pmops_suspend(struct device
*dev
)
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);
205 static int amdgpu_pmops_resume(struct device
*dev
)
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);
212 static int amdgpu_pmops_freeze(struct device
*dev
)
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);
219 static int amdgpu_pmops_thaw(struct device
*dev
)
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);
226 static int amdgpu_pmops_runtime_suspend(struct device
*dev
)
228 struct pci_dev
*pdev
= to_pci_dev(dev
);
229 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
232 if (!amdgpu_device_is_px(drm_dev
)) {
233 pm_runtime_forbid(dev
);
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
);
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
;
251 static int amdgpu_pmops_runtime_resume(struct device
*dev
)
253 struct pci_dev
*pdev
= to_pci_dev(dev
);
254 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
257 if (!amdgpu_device_is_px(drm_dev
))
260 drm_dev
->switch_power_state
= DRM_SWITCH_POWER_CHANGING
;
262 pci_set_power_state(pdev
, PCI_D0
);
263 pci_restore_state(pdev
);
264 ret
= pci_enable_device(pdev
);
267 pci_set_master(pdev
);
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
;
276 static int amdgpu_pmops_runtime_idle(struct device
*dev
)
278 struct pci_dev
*pdev
= to_pci_dev(dev
);
279 struct drm_device
*drm_dev
= pci_get_drvdata(pdev
);
280 struct drm_crtc
*crtc
;
282 if (!amdgpu_device_is_px(drm_dev
)) {
283 pm_runtime_forbid(dev
);
287 list_for_each_entry(crtc
, &drm_dev
->mode_config
.crtc_list
, head
) {
289 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
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 */
300 long amdgpu_drm_ioctl(struct file
*filp
,
301 unsigned int cmd
, unsigned long arg
)
303 struct drm_file
*file_priv
= filp
->private_data
;
304 struct drm_device
*dev
;
306 dev
= file_priv
->minor
->dev
;
307 ret
= pm_runtime_get_sync(dev
->dev
);
311 ret
= drm_ioctl(filp
, cmd
, arg
);
313 pm_runtime_mark_last_busy(dev
->dev
);
314 pm_runtime_put_autosuspend(dev
->dev
);
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
,
330 static const struct file_operations amdgpu_driver_kms_fops
= {
331 .owner
= THIS_MODULE
,
333 .release
= drm_release
,
334 .unlocked_ioctl
= amdgpu_drm_ioctl
,
339 .compat_ioctl
= amdgpu_kms_compat_ioctl
,
343 static struct drm_driver kms_driver
= {
346 DRIVER_HAVE_IRQ
| DRIVER_IRQ_SHARED
| DRIVER_GEM
|
347 DRIVER_PRIME
| DRIVER_RENDER
,
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
,
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
,
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
,
393 .major
= KMS_DRIVER_MAJOR
,
394 .minor
= KMS_DRIVER_MINOR
,
395 .patchlevel
= KMS_DRIVER_PATCHLEVEL
,
398 static struct drm_driver
*driver
;
399 static struct pci_driver
*pdriver
;
401 static struct pci_driver amdgpu_kms_pci_driver
= {
403 .id_table
= pciidlist
,
404 .probe
= amdgpu_pci_probe
,
405 .remove
= amdgpu_pci_remove
,
406 .driver
.pm
= &amdgpu_pm_ops
,
409 static int __init
amdgpu_init(void)
411 #ifdef CONFIG_VGA_CONSOLE
412 if (vgacon_text_force()) {
413 DRM_ERROR("VGACON disables amdgpu kernel modesetting.\n");
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();
424 /* let modprobe override vga console setting */
425 return drm_pci_init(driver
, pdriver
);
428 static void __exit
amdgpu_exit(void)
430 drm_pci_exit(driver
, pdriver
);
431 amdgpu_unregister_atpx_handler();
434 module_init(amdgpu_init
);
435 module_exit(amdgpu_exit
);
437 MODULE_AUTHOR(DRIVER_AUTHOR
);
438 MODULE_DESCRIPTION(DRIVER_DESC
);
439 MODULE_LICENSE("GPL and additional rights");