blob: de108427a19745246bb171f5ce714702384426e8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/**
2 * \file radeon_drv.c
3 * ATI Radeon 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
David Howells760285e2012-10-02 18:01:07 +010032#include <drm/drmP.h>
33#include <drm/radeon_drm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "radeon_drv.h"
35
David Howells760285e2012-10-02 18:01:07 +010036#include <drm/drm_pciids.h>
Jerome Glisse771fe6b2009-06-05 14:42:42 +020037#include <linux/console.h>
Paul Gortmakere0cd3602011-08-30 11:04:30 -040038#include <linux/module.h>
Dave Airlie10ebc0b2012-09-17 14:40:31 +100039#include <linux/pm_runtime.h>
40#include <linux/vga_switcheroo.h>
Daniel Vetterd9fc9412014-09-23 15:46:53 +020041#include <drm/drm_gem.h>
42
Dave Airlie10ebc0b2012-09-17 14:40:31 +100043#include "drm_crtc_helper.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020044/*
45 * KMS wrapper.
Dave Airlie0de1a572010-03-01 16:32:15 +100046 * - 2.0.0 - initial interface
47 * - 2.1.0 - add square tiling interface
Alex Deucherfdb43522010-03-26 15:24:14 -040048 * - 2.2.0 - add r6xx/r7xx const buffer support
Marek Olšákcae94b02010-02-21 21:24:15 +010049 * - 2.3.0 - add MSPOS + 3D texture + r500 VAP regs
Jerome Glissebc35afd2010-05-12 18:01:13 +020050 * - 2.4.0 - add crtc id query
Alex Deucher148a03b2010-06-03 19:00:03 -040051 * - 2.5.0 - add get accel 2 to work around ddx breakage for evergreen
Dave Airlieab9e1f52010-07-13 11:11:11 +100052 * - 2.6.0 - add tiling config query (r6xx+), add initial HiZ support (r300->r500)
Alex Deucher71901cc2010-10-21 13:45:30 -040053 * 2.7.0 - fixups for r600 2D tiling support. (no external ABI change), add eg dyn gpr regs
Alex Deucher58bbf012011-01-24 17:14:26 -050054 * 2.8.0 - pageflip support, r500 US_FORMAT regs. r500 ARGB2101010 colorbuf, r300->r500 CMASK, clock crystal query
Dave Airlie486af182011-03-01 14:32:27 +100055 * 2.9.0 - r600 tiling (s3tc,rgtc) working, SET_PREDICATION packet 3 on r600 + eg, backend query
Alex Deucherb8709892011-07-27 04:17:25 +000056 * 2.10.0 - fusion 2D tiling
57 * 2.11.0 - backend map, initial compute support for the CS checker
Marek Olšáke70f2242011-10-25 01:38:45 +020058 * 2.12.0 - RADEON_CS_KEEP_TILING_FLAGS
Marek Olšákdd220a02012-01-27 12:17:59 -050059 * 2.13.0 - virtual memory support, streamout
Jerome Glisse285484e2011-12-16 17:03:42 -050060 * 2.14.0 - add evergreen tiling informations
Tom Stellard609c1e12012-03-20 17:17:55 -040061 * 2.15.0 - add max_pipes query
Jerome Glissed2609872012-06-09 10:57:41 -040062 * 2.16.0 - fix evergreen 2D tiled surface calculation
Alex Deucher7c77bf22012-06-14 22:06:37 +020063 * 2.17.0 - add STRMOUT_BASE_UPDATE for r7xx
Marek Olšák0f457e42012-07-29 16:24:57 +020064 * 2.18.0 - r600-eg: allow "invalid" DB formats
Marek Olšákb51ad122012-08-09 16:34:16 +020065 * 2.19.0 - r600-eg: MSAA textures
Marek Olšák6759a0a2012-08-09 16:34:17 +020066 * 2.20.0 - r600-si: RADEON_INFO_TIMESTAMP query
Marek Olšákc116cc92012-08-19 02:22:09 +020067 * 2.21.0 - r600-r700: FMASK and CMASK
Marek Olšák523885d2012-08-24 14:27:36 +020068 * 2.22.0 - r600 only: RESOLVE_BOX allowed
Marek Olšák46fc8782012-09-25 01:45:33 +020069 * 2.23.0 - allow STRMOUT_BASE_UPDATE on RS780 and RS880
Marek Olšák61051af2012-09-25 03:34:01 +020070 * 2.24.0 - eg only: allow MIP_ADDRESS=0 for MSAA textures
Alex Deucher71bfe912012-12-07 20:00:30 -050071 * 2.25.0 - eg+: new info request for num SE and num SH
Jerome Glisse4ac05332012-12-13 12:08:11 -050072 * 2.26.0 - r600-eg: fix htile size computation
Alex Deucher8696e332012-12-13 18:57:07 -050073 * 2.27.0 - r600-SI: Add CS ioctl support for async DMA
Jerome Glisse4613ca12012-12-19 12:26:45 -050074 * 2.28.0 - r600-eg: Add MEM_WRITE packet support
Marek Olšákc18b1172013-01-12 04:19:37 +010075 * 2.29.0 - R500 FP16 color clear registers
Marek Olšák774c3892013-03-01 13:40:31 +010076 * 2.30.0 - fix for FMASK texturing
Samuel Lia0a53aa2013-04-08 17:25:47 -040077 * 2.31.0 - Add fastfb support for rs690
Christian König902aaef2013-04-09 10:35:42 -040078 * 2.32.0 - new info request for rings working
Jerome Glisse64d7b8b2013-04-09 11:17:08 -040079 * 2.33.0 - Add SI tiling mode array query
Alex Deucher39aee492013-04-10 13:41:25 -040080 * 2.34.0 - Add CIK tiling mode array query
Michel Dänzer32f79a82013-11-18 18:26:00 +090081 * 2.35.0 - Add CIK macrotile mode array query
Alex Deucher9482d0d2013-12-23 11:31:44 -050082 * 2.36.0 - Fix CIK DCE tiling setup
Dave Airlie7c4c62a2014-01-30 14:11:12 +100083 * 2.37.0 - allow GS ring setup on r6xx/r7xx
Marek Olšák020ff542014-03-22 16:20:43 +010084 * 2.38.0 - RADEON_GEM_OP (GET_INITIAL_DOMAIN, SET_INITIAL_DOMAIN),
85 * CIK: 1D and linear tiling modes contain valid PIPE_CONFIG
Alex Deucher65fcf662014-06-02 16:13:21 -040086 * 2.39.0 - Add INFO query for number of active CUs
Michel Dänzer72a99872014-07-31 18:43:49 +090087 * 2.40.0 - Add RADEON_GEM_GTT_WC/UC, flush HDP cache before submitting
88 * CS to GPU
Jerome Glisse771fe6b2009-06-05 14:42:42 +020089 */
90#define KMS_DRIVER_MAJOR 2
Michel Dänzer72a99872014-07-31 18:43:49 +090091#define KMS_DRIVER_MINOR 40
Jerome Glisse771fe6b2009-06-05 14:42:42 +020092#define KMS_DRIVER_PATCHLEVEL 0
93int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags);
94int radeon_driver_unload_kms(struct drm_device *dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020095void radeon_driver_lastclose_kms(struct drm_device *dev);
96int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv);
97void radeon_driver_postclose_kms(struct drm_device *dev,
98 struct drm_file *file_priv);
99void radeon_driver_preclose_kms(struct drm_device *dev,
100 struct drm_file *file_priv);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000101int radeon_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon);
102int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200103u32 radeon_get_vblank_counter_kms(struct drm_device *dev, int crtc);
104int radeon_enable_vblank_kms(struct drm_device *dev, int crtc);
105void radeon_disable_vblank_kms(struct drm_device *dev, int crtc);
Mario Kleinerf5a80202010-10-23 04:42:17 +0200106int radeon_get_vblank_timestamp_kms(struct drm_device *dev, int crtc,
107 int *max_error,
108 struct timeval *vblank_time,
109 unsigned flags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200110void radeon_driver_irq_preinstall_kms(struct drm_device *dev);
111int radeon_driver_irq_postinstall_kms(struct drm_device *dev);
112void radeon_driver_irq_uninstall_kms(struct drm_device *dev);
Daniel Vettere9f0d762013-12-11 11:34:42 +0100113irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200114void radeon_gem_object_free(struct drm_gem_object *obj);
Jerome Glisse721604a2012-01-05 22:11:05 -0500115int radeon_gem_object_open(struct drm_gem_object *obj,
116 struct drm_file *file_priv);
117void radeon_gem_object_close(struct drm_gem_object *obj,
118 struct drm_file *file_priv);
Christian Königf72a113a2014-08-07 09:36:00 +0200119struct dma_buf *radeon_gem_prime_export(struct drm_device *dev,
120 struct drm_gem_object *gobj,
121 int flags);
Mario Kleinerf5a80202010-10-23 04:42:17 +0200122extern int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc,
Ville Syrjäläabca9e42013-10-28 20:50:48 +0200123 unsigned int flags,
Mario Kleinerd47abc52013-10-30 05:13:07 +0100124 int *vpos, int *hpos, ktime_t *stime,
125 ktime_t *etime);
Alex Deucher90c4cde2014-04-10 22:29:01 -0400126extern bool radeon_is_px(struct drm_device *dev);
Rob Clarkbaa70942013-08-02 13:27:49 -0400127extern const struct drm_ioctl_desc radeon_ioctls_kms[];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200128extern int radeon_max_kms_ioctl;
129int radeon_mmap(struct file *filp, struct vm_area_struct *vma);
Dave Airlieff72145b2011-02-07 12:16:14 +1000130int radeon_mode_dumb_mmap(struct drm_file *filp,
131 struct drm_device *dev,
132 uint32_t handle, uint64_t *offset_p);
133int radeon_mode_dumb_create(struct drm_file *file_priv,
134 struct drm_device *dev,
135 struct drm_mode_create_dumb *args);
Aaron Plattner1e6d17a2013-01-15 20:47:44 +0000136struct sg_table *radeon_gem_prime_get_sg_table(struct drm_gem_object *obj);
137struct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev,
138 size_t size,
139 struct sg_table *sg);
140int radeon_gem_prime_pin(struct drm_gem_object *obj);
Maarten Lankhorst280cf212013-06-27 13:38:18 +0200141void radeon_gem_prime_unpin(struct drm_gem_object *obj);
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200142struct reservation_object *radeon_gem_prime_res_obj(struct drm_gem_object *);
Aaron Plattner1e6d17a2013-01-15 20:47:44 +0000143void *radeon_gem_prime_vmap(struct drm_gem_object *obj);
144void radeon_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
Christian König14adc892013-01-21 13:58:46 +0100145extern long radeon_kms_compat_ioctl(struct file *filp, unsigned int cmd,
146 unsigned long arg);
Dave Airlieff72145b2011-02-07 12:16:14 +1000147
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200148#if defined(CONFIG_DEBUG_FS)
149int radeon_debugfs_init(struct drm_minor *minor);
150void radeon_debugfs_cleanup(struct drm_minor *minor);
151#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200152
Christian König14adc892013-01-21 13:58:46 +0100153/* atpx handler */
154#if defined(CONFIG_VGA_SWITCHEROO)
155void radeon_register_atpx_handler(void);
156void radeon_unregister_atpx_handler(void);
157#else
158static inline void radeon_register_atpx_handler(void) {}
159static inline void radeon_unregister_atpx_handler(void) {}
160#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Dave Airlie689b9d72005-09-30 17:09:07 +1000162int radeon_no_wb;
Dave Airliee9ced8e2013-05-15 01:23:36 +0000163int radeon_modeset = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200164int radeon_dynclks = -1;
165int radeon_r4xx_atom = 0;
166int radeon_agpmode = 0;
167int radeon_vram_limit = 0;
Alex Deucheredcd26e2013-07-05 17:16:51 -0400168int radeon_gart_size = -1; /* auto */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200169int radeon_benchmarking = 0;
Michel Dänzerecc0b322009-07-21 11:23:57 +0200170int radeon_testing = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200171int radeon_connector_table = 0;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000172int radeon_tv = 1;
Alex Deucher108dc8e2013-10-14 13:17:50 -0400173int radeon_audio = -1;
Alex Deucherf46c0122010-03-31 00:33:27 -0400174int radeon_disp_priority = 0;
Alex Deuchere2b0a8e2010-03-17 02:07:37 -0400175int radeon_hw_i2c = 0;
Dave Airlie197bbb32012-06-27 08:35:54 +0100176int radeon_pcie_gen2 = -1;
Alex Deuchera18cee12011-11-01 14:20:30 -0400177int radeon_msi = -1;
Christian König3368ff02012-05-02 15:11:21 +0200178int radeon_lockup_timeout = 10000;
Samuel Lia0a53aa2013-04-08 17:25:47 -0400179int radeon_fastfb = 0;
Alex Deucherda321c82013-04-12 13:55:22 -0400180int radeon_dpm = -1;
Alex Deucher1294d4a2013-07-16 15:58:50 -0400181int radeon_aspm = -1;
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000182int radeon_runtime_pm = -1;
Alex Deucher363eb0b2014-01-08 17:55:08 -0500183int radeon_hard_reset = 0;
Christian Königdfc230f2014-07-19 13:55:58 +0200184int radeon_vm_size = 8;
185int radeon_vm_block_size = -1;
Alex Deuchera624f422014-07-01 11:23:03 -0400186int radeon_deep_color = 0;
Mario Kleiner39dc5452014-07-29 06:21:44 +0200187int radeon_use_pflipirq = 2;
Alex Deucher6e909f72014-08-07 09:28:31 -0400188int radeon_bapm = -1;
Dave Airlie689b9d72005-09-30 17:09:07 +1000189
Niels de Vos61a2d072008-07-31 00:07:23 -0700190MODULE_PARM_DESC(no_wb, "Disable AGP writeback for scratch registers");
Dave Airlie689b9d72005-09-30 17:09:07 +1000191module_param_named(no_wb, radeon_no_wb, int, 0444);
192
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200193MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
194module_param_named(modeset, radeon_modeset, int, 0400);
195
196MODULE_PARM_DESC(dynclks, "Disable/Enable dynamic clocks");
197module_param_named(dynclks, radeon_dynclks, int, 0444);
198
199MODULE_PARM_DESC(r4xx_atom, "Enable ATOMBIOS modesetting for R4xx");
200module_param_named(r4xx_atom, radeon_r4xx_atom, int, 0444);
201
Lauri Kasanen8902e6f2014-04-08 13:39:36 +0300202MODULE_PARM_DESC(vramlimit, "Restrict VRAM for testing, in megabytes");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200203module_param_named(vramlimit, radeon_vram_limit, int, 0600);
204
205MODULE_PARM_DESC(agpmode, "AGP Mode (-1 == PCI)");
206module_param_named(agpmode, radeon_agpmode, int, 0444);
207
Alex Deucheredcd26e2013-07-05 17:16:51 -0400208MODULE_PARM_DESC(gartsize, "Size of PCIE/IGP gart to setup in megabytes (32, 64, etc., -1 = auto)");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200209module_param_named(gartsize, radeon_gart_size, int, 0600);
210
211MODULE_PARM_DESC(benchmark, "Run benchmark");
212module_param_named(benchmark, radeon_benchmarking, int, 0444);
213
Michel Dänzerecc0b322009-07-21 11:23:57 +0200214MODULE_PARM_DESC(test, "Run tests");
215module_param_named(test, radeon_testing, int, 0444);
216
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200217MODULE_PARM_DESC(connector_table, "Force connector table");
218module_param_named(connector_table, radeon_connector_table, int, 0444);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000219
220MODULE_PARM_DESC(tv, "TV enable (0 = disable)");
221module_param_named(tv, radeon_tv, int, 0444);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200222
Alex Deucher108dc8e2013-10-14 13:17:50 -0400223MODULE_PARM_DESC(audio, "Audio enable (-1 = auto, 0 = disable, 1 = enable)");
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200224module_param_named(audio, radeon_audio, int, 0444);
225
Alex Deucherf46c0122010-03-31 00:33:27 -0400226MODULE_PARM_DESC(disp_priority, "Display Priority (0 = auto, 1 = normal, 2 = high)");
227module_param_named(disp_priority, radeon_disp_priority, int, 0444);
228
Alex Deuchere2b0a8e2010-03-17 02:07:37 -0400229MODULE_PARM_DESC(hw_i2c, "hw i2c engine enable (0 = disable)");
230module_param_named(hw_i2c, radeon_hw_i2c, int, 0444);
231
Dave Airlie197bbb32012-06-27 08:35:54 +0100232MODULE_PARM_DESC(pcie_gen2, "PCIE Gen2 mode (-1 = auto, 0 = disable, 1 = enable)");
Alex Deucherd42dd572011-01-12 20:05:11 -0500233module_param_named(pcie_gen2, radeon_pcie_gen2, int, 0444);
234
Alex Deuchera18cee12011-11-01 14:20:30 -0400235MODULE_PARM_DESC(msi, "MSI support (1 = enable, 0 = disable, -1 = auto)");
236module_param_named(msi, radeon_msi, int, 0444);
237
Christian König3368ff02012-05-02 15:11:21 +0200238MODULE_PARM_DESC(lockup_timeout, "GPU lockup timeout in ms (defaul 10000 = 10 seconds, 0 = disable)");
239module_param_named(lockup_timeout, radeon_lockup_timeout, int, 0444);
240
Samuel Lia0a53aa2013-04-08 17:25:47 -0400241MODULE_PARM_DESC(fastfb, "Direct FB access for IGP chips (0 = disable, 1 = enable)");
242module_param_named(fastfb, radeon_fastfb, int, 0444);
243
Alex Deucherda321c82013-04-12 13:55:22 -0400244MODULE_PARM_DESC(dpm, "DPM support (1 = enable, 0 = disable, -1 = auto)");
245module_param_named(dpm, radeon_dpm, int, 0444);
246
Alex Deucher1294d4a2013-07-16 15:58:50 -0400247MODULE_PARM_DESC(aspm, "ASPM support (1 = enable, 0 = disable, -1 = auto)");
248module_param_named(aspm, radeon_aspm, int, 0444);
249
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000250MODULE_PARM_DESC(runpm, "PX runtime pm (1 = force enable, 0 = disable, -1 = PX only default)");
251module_param_named(runpm, radeon_runtime_pm, int, 0444);
252
Alex Deucher363eb0b2014-01-08 17:55:08 -0500253MODULE_PARM_DESC(hard_reset, "PCI config reset (1 = force enable, 0 = disable (default))");
254module_param_named(hard_reset, radeon_hard_reset, int, 0444);
255
Christian König20b26562014-07-18 13:56:56 +0200256MODULE_PARM_DESC(vm_size, "VM address space size in gigabytes (default 4GB)");
Christian Königc1c44132014-06-05 23:47:32 -0400257module_param_named(vm_size, radeon_vm_size, int, 0444);
258
Christian Königdfc230f2014-07-19 13:55:58 +0200259MODULE_PARM_DESC(vm_block_size, "VM page table size in bits (default depending on vm_size)");
Christian König4510fb92014-06-05 23:56:50 -0400260module_param_named(vm_block_size, radeon_vm_block_size, int, 0444);
261
Alex Deuchera624f422014-07-01 11:23:03 -0400262MODULE_PARM_DESC(deep_color, "Deep Color support (1 = enable, 0 = disable (default))");
263module_param_named(deep_color, radeon_deep_color, int, 0444);
264
Mario Kleiner39dc5452014-07-29 06:21:44 +0200265MODULE_PARM_DESC(use_pflipirq, "Pflip irqs for pageflip completion (0 = disable, 1 = as fallback, 2 = exclusive (default))");
266module_param_named(use_pflipirq, radeon_use_pflipirq, int, 0444);
267
Alex Deucher6e909f72014-08-07 09:28:31 -0400268MODULE_PARM_DESC(bapm, "BAPM support (1 = enable, 0 = disable, -1 = auto)");
269module_param_named(bapm, radeon_bapm, int, 0444);
270
Christian König14adc892013-01-21 13:58:46 +0100271static struct pci_device_id pciidlist[] = {
272 radeon_PCI_IDS
273};
274
275MODULE_DEVICE_TABLE(pci, pciidlist);
276
277#ifdef CONFIG_DRM_RADEON_UMS
278
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700279static int radeon_suspend(struct drm_device *dev, pm_message_t state)
280{
281 drm_radeon_private_t *dev_priv = dev->dev_private;
282
Dave Airlie03efb882009-03-10 18:36:38 +1000283 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
284 return 0;
285
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700286 /* Disable *all* interrupts */
Alex Deucher800b6992009-03-06 11:47:54 -0500287 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700288 RADEON_WRITE(R500_DxMODE_INT_MASK, 0);
289 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
290 return 0;
291}
292
293static int radeon_resume(struct drm_device *dev)
294{
295 drm_radeon_private_t *dev_priv = dev->dev_private;
296
Dave Airlie03efb882009-03-10 18:36:38 +1000297 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
298 return 0;
299
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700300 /* Restore interrupt registers */
Alex Deucher800b6992009-03-06 11:47:54 -0500301 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700302 RADEON_WRITE(R500_DxMODE_INT_MASK, dev_priv->r500_disp_irq_reg);
303 RADEON_WRITE(RADEON_GEN_INT_CNTL, dev_priv->irq_enable_reg);
304 return 0;
305}
306
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000307
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700308static const struct file_operations radeon_driver_old_fops = {
309 .owner = THIS_MODULE,
310 .open = drm_open,
311 .release = drm_release,
312 .unlocked_ioctl = drm_ioctl,
Daniel Vetterbfbf3c82014-09-23 15:46:49 +0200313 .mmap = drm_legacy_mmap,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700314 .poll = drm_poll,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700315 .read = drm_read,
316#ifdef CONFIG_COMPAT
317 .compat_ioctl = radeon_compat_ioctl,
318#endif
319 .llseek = noop_llseek,
320};
321
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200322static struct drm_driver driver_old = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000323 .driver_features =
Daniel Vetter28185642013-08-08 15:41:27 +0200324 DRIVER_USE_AGP | DRIVER_PCI_DMA | DRIVER_SG |
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700325 DRIVER_HAVE_IRQ | DRIVER_HAVE_DMA | DRIVER_IRQ_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 .dev_priv_size = sizeof(drm_radeon_buf_priv_t),
Dave Airlie22eae942005-11-10 22:16:34 +1100327 .load = radeon_driver_load,
328 .firstopen = radeon_driver_firstopen,
329 .open = radeon_driver_open,
330 .preclose = radeon_driver_preclose,
331 .postclose = radeon_driver_postclose,
332 .lastclose = radeon_driver_lastclose,
David Herrmann915b4d12014-08-29 12:12:43 +0200333 .set_busid = drm_pci_set_busid,
Dave Airlie22eae942005-11-10 22:16:34 +1100334 .unload = radeon_driver_unload,
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700335 .suspend = radeon_suspend,
336 .resume = radeon_resume,
337 .get_vblank_counter = radeon_get_vblank_counter,
338 .enable_vblank = radeon_enable_vblank,
339 .disable_vblank = radeon_disable_vblank,
Dave Airlie60f2ee02008-12-19 10:22:02 +1100340 .master_create = radeon_master_create,
341 .master_destroy = radeon_master_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 .irq_preinstall = radeon_driver_irq_preinstall,
343 .irq_postinstall = radeon_driver_irq_postinstall,
344 .irq_uninstall = radeon_driver_irq_uninstall,
345 .irq_handler = radeon_driver_irq_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 .ioctls = radeon_ioctls,
347 .dma_ioctl = radeon_cp_buffers,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700348 .fops = &radeon_driver_old_fops,
Dave Airlie22eae942005-11-10 22:16:34 +1100349 .name = DRIVER_NAME,
350 .desc = DRIVER_DESC,
351 .date = DRIVER_DATE,
352 .major = DRIVER_MAJOR,
353 .minor = DRIVER_MINOR,
354 .patchlevel = DRIVER_PATCHLEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355};
356
Christian König14adc892013-01-21 13:58:46 +0100357#endif
358
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200359static struct drm_driver kms_driver;
360
Tommi Rantala30238152012-11-09 09:19:39 +0000361static int radeon_kick_out_firmware_fb(struct pci_dev *pdev)
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000362{
363 struct apertures_struct *ap;
364 bool primary = false;
365
366 ap = alloc_apertures(1);
Tommi Rantala30238152012-11-09 09:19:39 +0000367 if (!ap)
368 return -ENOMEM;
369
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000370 ap->ranges[0].base = pci_resource_start(pdev, 0);
371 ap->ranges[0].size = pci_resource_len(pdev, 0);
372
373#ifdef CONFIG_X86
374 primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
375#endif
376 remove_conflicting_framebuffers(ap, "radeondrmfb", primary);
377 kfree(ap);
Tommi Rantala30238152012-11-09 09:19:39 +0000378
379 return 0;
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000380}
381
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800382static int radeon_pci_probe(struct pci_dev *pdev,
383 const struct pci_device_id *ent)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200384{
Tommi Rantala30238152012-11-09 09:19:39 +0000385 int ret;
386
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000387 /* Get rid of things like offb */
Tommi Rantala30238152012-11-09 09:19:39 +0000388 ret = radeon_kick_out_firmware_fb(pdev);
389 if (ret)
390 return ret;
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000391
Jordan Crousedcdb1672010-05-27 13:40:25 -0600392 return drm_get_pci_dev(pdev, ent, &kms_driver);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200393}
394
395static void
396radeon_pci_remove(struct pci_dev *pdev)
397{
398 struct drm_device *dev = pci_get_drvdata(pdev);
399
400 drm_put_dev(dev);
401}
402
Dave Airlie7473e832012-09-13 12:02:30 +1000403static int radeon_pmops_suspend(struct device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200404{
Dave Airlie7473e832012-09-13 12:02:30 +1000405 struct pci_dev *pdev = to_pci_dev(dev);
406 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000407 return radeon_suspend_kms(drm_dev, true, true);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200408}
409
Dave Airlie7473e832012-09-13 12:02:30 +1000410static int radeon_pmops_resume(struct device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200411{
Dave Airlie7473e832012-09-13 12:02:30 +1000412 struct pci_dev *pdev = to_pci_dev(dev);
413 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000414 return radeon_resume_kms(drm_dev, true, true);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200415}
416
Dave Airlie7473e832012-09-13 12:02:30 +1000417static int radeon_pmops_freeze(struct device *dev)
418{
419 struct pci_dev *pdev = to_pci_dev(dev);
420 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000421 return radeon_suspend_kms(drm_dev, false, true);
Dave Airlie7473e832012-09-13 12:02:30 +1000422}
423
424static int radeon_pmops_thaw(struct device *dev)
425{
426 struct pci_dev *pdev = to_pci_dev(dev);
427 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000428 return radeon_resume_kms(drm_dev, false, true);
429}
430
431static int radeon_pmops_runtime_suspend(struct device *dev)
432{
433 struct pci_dev *pdev = to_pci_dev(dev);
434 struct drm_device *drm_dev = pci_get_drvdata(pdev);
435 int ret;
436
Alex Deucher90c4cde2014-04-10 22:29:01 -0400437 if (!radeon_is_px(drm_dev)) {
Dave Airlie1d8eec82014-03-27 14:09:18 +1000438 pm_runtime_forbid(dev);
439 return -EBUSY;
440 }
Alex Deucher9babd352014-01-24 14:59:42 -0500441
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000442 drm_dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
443 drm_kms_helper_poll_disable(drm_dev);
444 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
445
446 ret = radeon_suspend_kms(drm_dev, false, false);
447 pci_save_state(pdev);
448 pci_disable_device(pdev);
449 pci_set_power_state(pdev, PCI_D3cold);
450 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
451
452 return 0;
453}
454
455static int radeon_pmops_runtime_resume(struct device *dev)
456{
457 struct pci_dev *pdev = to_pci_dev(dev);
458 struct drm_device *drm_dev = pci_get_drvdata(pdev);
459 int ret;
460
Alex Deucher90c4cde2014-04-10 22:29:01 -0400461 if (!radeon_is_px(drm_dev))
Alex Deucher9babd352014-01-24 14:59:42 -0500462 return -EINVAL;
463
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000464 drm_dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
465
466 pci_set_power_state(pdev, PCI_D0);
467 pci_restore_state(pdev);
468 ret = pci_enable_device(pdev);
469 if (ret)
470 return ret;
471 pci_set_master(pdev);
472
473 ret = radeon_resume_kms(drm_dev, false, false);
474 drm_kms_helper_poll_enable(drm_dev);
475 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
476 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
477 return 0;
478}
479
480static int radeon_pmops_runtime_idle(struct device *dev)
481{
482 struct pci_dev *pdev = to_pci_dev(dev);
483 struct drm_device *drm_dev = pci_get_drvdata(pdev);
484 struct drm_crtc *crtc;
485
Alex Deucher90c4cde2014-04-10 22:29:01 -0400486 if (!radeon_is_px(drm_dev)) {
Dave Airlie1d8eec82014-03-27 14:09:18 +1000487 pm_runtime_forbid(dev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000488 return -EBUSY;
489 }
490
491 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
492 if (crtc->enabled) {
493 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
494 return -EBUSY;
495 }
496 }
497
498 pm_runtime_mark_last_busy(dev);
499 pm_runtime_autosuspend(dev);
500 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
501 return 1;
502}
503
504long radeon_drm_ioctl(struct file *filp,
505 unsigned int cmd, unsigned long arg)
506{
507 struct drm_file *file_priv = filp->private_data;
508 struct drm_device *dev;
509 long ret;
510 dev = file_priv->minor->dev;
511 ret = pm_runtime_get_sync(dev->dev);
512 if (ret < 0)
513 return ret;
514
515 ret = drm_ioctl(filp, cmd, arg);
516
517 pm_runtime_mark_last_busy(dev->dev);
518 pm_runtime_put_autosuspend(dev->dev);
519 return ret;
Dave Airlie7473e832012-09-13 12:02:30 +1000520}
521
522static const struct dev_pm_ops radeon_pm_ops = {
523 .suspend = radeon_pmops_suspend,
524 .resume = radeon_pmops_resume,
525 .freeze = radeon_pmops_freeze,
526 .thaw = radeon_pmops_thaw,
527 .poweroff = radeon_pmops_freeze,
528 .restore = radeon_pmops_resume,
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000529 .runtime_suspend = radeon_pmops_runtime_suspend,
530 .runtime_resume = radeon_pmops_runtime_resume,
531 .runtime_idle = radeon_pmops_runtime_idle,
Dave Airlie7473e832012-09-13 12:02:30 +1000532};
533
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700534static const struct file_operations radeon_driver_kms_fops = {
535 .owner = THIS_MODULE,
536 .open = drm_open,
537 .release = drm_release,
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000538 .unlocked_ioctl = radeon_drm_ioctl,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700539 .mmap = radeon_mmap,
540 .poll = drm_poll,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700541 .read = drm_read,
542#ifdef CONFIG_COMPAT
543 .compat_ioctl = radeon_kms_compat_ioctl,
544#endif
545};
546
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200547static struct drm_driver kms_driver = {
548 .driver_features =
Daniel Vetter28185642013-08-08 15:41:27 +0200549 DRIVER_USE_AGP |
Daniel Vetter81e95692013-07-10 14:11:49 +0200550 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |
Christian Königf33bcab2013-08-25 18:29:03 +0200551 DRIVER_PRIME | DRIVER_RENDER,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200552 .load = radeon_driver_load_kms,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200553 .open = radeon_driver_open_kms,
554 .preclose = radeon_driver_preclose_kms,
555 .postclose = radeon_driver_postclose_kms,
556 .lastclose = radeon_driver_lastclose_kms,
David Herrmann915b4d12014-08-29 12:12:43 +0200557 .set_busid = drm_pci_set_busid,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200558 .unload = radeon_driver_unload_kms,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200559 .get_vblank_counter = radeon_get_vblank_counter_kms,
560 .enable_vblank = radeon_enable_vblank_kms,
561 .disable_vblank = radeon_disable_vblank_kms,
Mario Kleinerf5a80202010-10-23 04:42:17 +0200562 .get_vblank_timestamp = radeon_get_vblank_timestamp_kms,
563 .get_scanout_position = radeon_get_crtc_scanoutpos,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200564#if defined(CONFIG_DEBUG_FS)
565 .debugfs_init = radeon_debugfs_init,
566 .debugfs_cleanup = radeon_debugfs_cleanup,
567#endif
568 .irq_preinstall = radeon_driver_irq_preinstall_kms,
569 .irq_postinstall = radeon_driver_irq_postinstall_kms,
570 .irq_uninstall = radeon_driver_irq_uninstall_kms,
571 .irq_handler = radeon_driver_irq_handler_kms,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200572 .ioctls = radeon_ioctls_kms,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200573 .gem_free_object = radeon_gem_object_free,
Jerome Glisse721604a2012-01-05 22:11:05 -0500574 .gem_open_object = radeon_gem_object_open,
575 .gem_close_object = radeon_gem_object_close,
Dave Airlieff72145b2011-02-07 12:16:14 +1000576 .dumb_create = radeon_mode_dumb_create,
577 .dumb_map_offset = radeon_mode_dumb_mmap,
Daniel Vetter43387b32013-07-16 09:12:04 +0200578 .dumb_destroy = drm_gem_dumb_destroy,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700579 .fops = &radeon_driver_kms_fops,
Alex Deucher40f5cf92012-05-10 18:33:13 -0400580
581 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
582 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Christian Königf72a113a2014-08-07 09:36:00 +0200583 .gem_prime_export = radeon_gem_prime_export,
Aaron Plattner1e6d17a2013-01-15 20:47:44 +0000584 .gem_prime_import = drm_gem_prime_import,
585 .gem_prime_pin = radeon_gem_prime_pin,
Maarten Lankhorst280cf212013-06-27 13:38:18 +0200586 .gem_prime_unpin = radeon_gem_prime_unpin,
Maarten Lankhorst3aac4502014-07-01 12:57:26 +0200587 .gem_prime_res_obj = radeon_gem_prime_res_obj,
Aaron Plattner1e6d17a2013-01-15 20:47:44 +0000588 .gem_prime_get_sg_table = radeon_gem_prime_get_sg_table,
589 .gem_prime_import_sg_table = radeon_gem_prime_import_sg_table,
590 .gem_prime_vmap = radeon_gem_prime_vmap,
591 .gem_prime_vunmap = radeon_gem_prime_vunmap,
Alex Deucher40f5cf92012-05-10 18:33:13 -0400592
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200593 .name = DRIVER_NAME,
594 .desc = DRIVER_DESC,
595 .date = DRIVER_DATE,
596 .major = KMS_DRIVER_MAJOR,
597 .minor = KMS_DRIVER_MINOR,
598 .patchlevel = KMS_DRIVER_PATCHLEVEL,
599};
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200600
601static struct drm_driver *driver;
Dave Airlie8410ea32010-12-15 03:16:38 +1000602static struct pci_driver *pdriver;
603
Christian König14adc892013-01-21 13:58:46 +0100604#ifdef CONFIG_DRM_RADEON_UMS
Dave Airlie8410ea32010-12-15 03:16:38 +1000605static struct pci_driver radeon_pci_driver = {
606 .name = DRIVER_NAME,
607 .id_table = pciidlist,
608};
Christian König14adc892013-01-21 13:58:46 +0100609#endif
Dave Airlie8410ea32010-12-15 03:16:38 +1000610
611static struct pci_driver radeon_kms_pci_driver = {
612 .name = DRIVER_NAME,
613 .id_table = pciidlist,
614 .probe = radeon_pci_probe,
615 .remove = radeon_pci_remove,
Dave Airlie7473e832012-09-13 12:02:30 +1000616 .driver.pm = &radeon_pm_ops,
Dave Airlie8410ea32010-12-15 03:16:38 +1000617};
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619static int __init radeon_init(void)
620{
Dave Airliee9ced8e2013-05-15 01:23:36 +0000621#ifdef CONFIG_VGA_CONSOLE
622 if (vgacon_text_force() && radeon_modeset == -1) {
623 DRM_INFO("VGACON disable radeon kernel modesetting.\n");
624 radeon_modeset = 0;
625 }
626#endif
627 /* set to modesetting by default if not nomodeset */
628 if (radeon_modeset == -1)
629 radeon_modeset = 1;
630
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200631 if (radeon_modeset == 1) {
632 DRM_INFO("radeon kernel modesetting enabled.\n");
633 driver = &kms_driver;
Dave Airlie8410ea32010-12-15 03:16:38 +1000634 pdriver = &radeon_kms_pci_driver;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200635 driver->driver_features |= DRIVER_MODESET;
636 driver->num_ioctls = radeon_max_kms_ioctl;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000637 radeon_register_atpx_handler();
Christian König14adc892013-01-21 13:58:46 +0100638
639 } else {
640#ifdef CONFIG_DRM_RADEON_UMS
641 DRM_INFO("radeon userspace modesetting enabled.\n");
642 driver = &driver_old;
643 pdriver = &radeon_pci_driver;
644 driver->driver_features &= ~DRIVER_MODESET;
645 driver->num_ioctls = radeon_max_ioctl;
646#else
647 DRM_ERROR("No UMS support in radeon module!\n");
648 return -EINVAL;
649#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200650 }
Christian König14adc892013-01-21 13:58:46 +0100651
652 /* let modprobe override vga console setting */
Dave Airlie8410ea32010-12-15 03:16:38 +1000653 return drm_pci_init(driver, pdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656static void __exit radeon_exit(void)
657{
Dave Airlie8410ea32010-12-15 03:16:38 +1000658 drm_pci_exit(driver, pdriver);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000659 radeon_unregister_atpx_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
Jerome Glisse176f6132009-06-22 18:16:13 +0200662module_init(radeon_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663module_exit(radeon_exit);
664
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000665MODULE_AUTHOR(DRIVER_AUTHOR);
666MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667MODULE_LICENSE("GPL and additional rights");