blob: d0eba48dd74e1dc42e07b7e8335e421360fa4421 [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>
41#include "drm_crtc_helper.h"
Jerome Glisse771fe6b2009-06-05 14:42:42 +020042/*
43 * KMS wrapper.
Dave Airlie0de1a572010-03-01 16:32:15 +100044 * - 2.0.0 - initial interface
45 * - 2.1.0 - add square tiling interface
Alex Deucherfdb43522010-03-26 15:24:14 -040046 * - 2.2.0 - add r6xx/r7xx const buffer support
Marek Olšákcae94b02010-02-21 21:24:15 +010047 * - 2.3.0 - add MSPOS + 3D texture + r500 VAP regs
Jerome Glissebc35afd2010-05-12 18:01:13 +020048 * - 2.4.0 - add crtc id query
Alex Deucher148a03b2010-06-03 19:00:03 -040049 * - 2.5.0 - add get accel 2 to work around ddx breakage for evergreen
Dave Airlieab9e1f52010-07-13 11:11:11 +100050 * - 2.6.0 - add tiling config query (r6xx+), add initial HiZ support (r300->r500)
Alex Deucher71901cc2010-10-21 13:45:30 -040051 * 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 -050052 * 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 +100053 * 2.9.0 - r600 tiling (s3tc,rgtc) working, SET_PREDICATION packet 3 on r600 + eg, backend query
Alex Deucherb8709892011-07-27 04:17:25 +000054 * 2.10.0 - fusion 2D tiling
55 * 2.11.0 - backend map, initial compute support for the CS checker
Marek Olšáke70f2242011-10-25 01:38:45 +020056 * 2.12.0 - RADEON_CS_KEEP_TILING_FLAGS
Marek Olšákdd220a02012-01-27 12:17:59 -050057 * 2.13.0 - virtual memory support, streamout
Jerome Glisse285484e2011-12-16 17:03:42 -050058 * 2.14.0 - add evergreen tiling informations
Tom Stellard609c1e12012-03-20 17:17:55 -040059 * 2.15.0 - add max_pipes query
Jerome Glissed2609872012-06-09 10:57:41 -040060 * 2.16.0 - fix evergreen 2D tiled surface calculation
Alex Deucher7c77bf22012-06-14 22:06:37 +020061 * 2.17.0 - add STRMOUT_BASE_UPDATE for r7xx
Marek Olšák0f457e42012-07-29 16:24:57 +020062 * 2.18.0 - r600-eg: allow "invalid" DB formats
Marek Olšákb51ad122012-08-09 16:34:16 +020063 * 2.19.0 - r600-eg: MSAA textures
Marek Olšák6759a0a2012-08-09 16:34:17 +020064 * 2.20.0 - r600-si: RADEON_INFO_TIMESTAMP query
Marek Olšákc116cc92012-08-19 02:22:09 +020065 * 2.21.0 - r600-r700: FMASK and CMASK
Marek Olšák523885d2012-08-24 14:27:36 +020066 * 2.22.0 - r600 only: RESOLVE_BOX allowed
Marek Olšák46fc8782012-09-25 01:45:33 +020067 * 2.23.0 - allow STRMOUT_BASE_UPDATE on RS780 and RS880
Marek Olšák61051af2012-09-25 03:34:01 +020068 * 2.24.0 - eg only: allow MIP_ADDRESS=0 for MSAA textures
Alex Deucher71bfe912012-12-07 20:00:30 -050069 * 2.25.0 - eg+: new info request for num SE and num SH
Jerome Glisse4ac05332012-12-13 12:08:11 -050070 * 2.26.0 - r600-eg: fix htile size computation
Alex Deucher8696e332012-12-13 18:57:07 -050071 * 2.27.0 - r600-SI: Add CS ioctl support for async DMA
Jerome Glisse4613ca12012-12-19 12:26:45 -050072 * 2.28.0 - r600-eg: Add MEM_WRITE packet support
Marek Olšákc18b1172013-01-12 04:19:37 +010073 * 2.29.0 - R500 FP16 color clear registers
Marek Olšák774c3892013-03-01 13:40:31 +010074 * 2.30.0 - fix for FMASK texturing
Samuel Lia0a53aa2013-04-08 17:25:47 -040075 * 2.31.0 - Add fastfb support for rs690
Christian König902aaef2013-04-09 10:35:42 -040076 * 2.32.0 - new info request for rings working
Jerome Glisse64d7b8b2013-04-09 11:17:08 -040077 * 2.33.0 - Add SI tiling mode array query
Alex Deucher39aee492013-04-10 13:41:25 -040078 * 2.34.0 - Add CIK tiling mode array query
Michel Dänzer32f79a82013-11-18 18:26:00 +090079 * 2.35.0 - Add CIK macrotile mode array query
Alex Deucher9482d0d2013-12-23 11:31:44 -050080 * 2.36.0 - Fix CIK DCE tiling setup
Dave Airlie7c4c62a2014-01-30 14:11:12 +100081 * 2.37.0 - allow GS ring setup on r6xx/r7xx
Marek Olšák020ff542014-03-22 16:20:43 +010082 * 2.38.0 - RADEON_GEM_OP (GET_INITIAL_DOMAIN, SET_INITIAL_DOMAIN),
83 * CIK: 1D and linear tiling modes contain valid PIPE_CONFIG
Jerome Glisse771fe6b2009-06-05 14:42:42 +020084 */
85#define KMS_DRIVER_MAJOR 2
Marek Olšákbda72d52014-03-02 00:56:17 +010086#define KMS_DRIVER_MINOR 38
Jerome Glisse771fe6b2009-06-05 14:42:42 +020087#define KMS_DRIVER_PATCHLEVEL 0
88int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags);
89int radeon_driver_unload_kms(struct drm_device *dev);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020090void radeon_driver_lastclose_kms(struct drm_device *dev);
91int radeon_driver_open_kms(struct drm_device *dev, struct drm_file *file_priv);
92void radeon_driver_postclose_kms(struct drm_device *dev,
93 struct drm_file *file_priv);
94void radeon_driver_preclose_kms(struct drm_device *dev,
95 struct drm_file *file_priv);
Dave Airlie10ebc0b2012-09-17 14:40:31 +100096int radeon_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon);
97int radeon_resume_kms(struct drm_device *dev, bool resume, bool fbcon);
Jerome Glisse771fe6b2009-06-05 14:42:42 +020098u32 radeon_get_vblank_counter_kms(struct drm_device *dev, int crtc);
99int radeon_enable_vblank_kms(struct drm_device *dev, int crtc);
100void radeon_disable_vblank_kms(struct drm_device *dev, int crtc);
Mario Kleinerf5a80202010-10-23 04:42:17 +0200101int radeon_get_vblank_timestamp_kms(struct drm_device *dev, int crtc,
102 int *max_error,
103 struct timeval *vblank_time,
104 unsigned flags);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200105void radeon_driver_irq_preinstall_kms(struct drm_device *dev);
106int radeon_driver_irq_postinstall_kms(struct drm_device *dev);
107void radeon_driver_irq_uninstall_kms(struct drm_device *dev);
Daniel Vettere9f0d762013-12-11 11:34:42 +0100108irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200109void radeon_gem_object_free(struct drm_gem_object *obj);
Jerome Glisse721604a2012-01-05 22:11:05 -0500110int radeon_gem_object_open(struct drm_gem_object *obj,
111 struct drm_file *file_priv);
112void radeon_gem_object_close(struct drm_gem_object *obj,
113 struct drm_file *file_priv);
Mario Kleinerf5a80202010-10-23 04:42:17 +0200114extern int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc,
Ville Syrjäläabca9e452013-10-28 20:50:48 +0200115 unsigned int flags,
Mario Kleinerd47abc52013-10-30 05:13:07 +0100116 int *vpos, int *hpos, ktime_t *stime,
117 ktime_t *etime);
Rob Clarkbaa70942013-08-02 13:27:49 -0400118extern const struct drm_ioctl_desc radeon_ioctls_kms[];
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200119extern int radeon_max_kms_ioctl;
120int radeon_mmap(struct file *filp, struct vm_area_struct *vma);
Dave Airlieff72145b2011-02-07 12:16:14 +1000121int radeon_mode_dumb_mmap(struct drm_file *filp,
122 struct drm_device *dev,
123 uint32_t handle, uint64_t *offset_p);
124int radeon_mode_dumb_create(struct drm_file *file_priv,
125 struct drm_device *dev,
126 struct drm_mode_create_dumb *args);
Aaron Plattner1e6d17a2013-01-15 20:47:44 +0000127struct sg_table *radeon_gem_prime_get_sg_table(struct drm_gem_object *obj);
128struct drm_gem_object *radeon_gem_prime_import_sg_table(struct drm_device *dev,
129 size_t size,
130 struct sg_table *sg);
131int radeon_gem_prime_pin(struct drm_gem_object *obj);
Maarten Lankhorst280cf212013-06-27 13:38:18 +0200132void radeon_gem_prime_unpin(struct drm_gem_object *obj);
Aaron Plattner1e6d17a2013-01-15 20:47:44 +0000133void *radeon_gem_prime_vmap(struct drm_gem_object *obj);
134void radeon_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr);
Christian König14adc892013-01-21 13:58:46 +0100135extern long radeon_kms_compat_ioctl(struct file *filp, unsigned int cmd,
136 unsigned long arg);
Dave Airlieff72145b2011-02-07 12:16:14 +1000137
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200138#if defined(CONFIG_DEBUG_FS)
139int radeon_debugfs_init(struct drm_minor *minor);
140void radeon_debugfs_cleanup(struct drm_minor *minor);
141#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200142
Christian König14adc892013-01-21 13:58:46 +0100143/* atpx handler */
144#if defined(CONFIG_VGA_SWITCHEROO)
145void radeon_register_atpx_handler(void);
146void radeon_unregister_atpx_handler(void);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000147bool radeon_is_px(void);
Christian König14adc892013-01-21 13:58:46 +0100148#else
149static inline void radeon_register_atpx_handler(void) {}
150static inline void radeon_unregister_atpx_handler(void) {}
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000151static inline bool radeon_is_px(void) { return false; }
Christian König14adc892013-01-21 13:58:46 +0100152#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Dave Airlie689b9d72005-09-30 17:09:07 +1000154int radeon_no_wb;
Dave Airliee9ced8e2013-05-15 01:23:36 +0000155int radeon_modeset = -1;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200156int radeon_dynclks = -1;
157int radeon_r4xx_atom = 0;
158int radeon_agpmode = 0;
159int radeon_vram_limit = 0;
Alex Deucheredcd26e2013-07-05 17:16:51 -0400160int radeon_gart_size = -1; /* auto */
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200161int radeon_benchmarking = 0;
Michel Dänzerecc0b322009-07-21 11:23:57 +0200162int radeon_testing = 0;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200163int radeon_connector_table = 0;
Dave Airlie4ce001a2009-08-13 16:32:14 +1000164int radeon_tv = 1;
Alex Deucher108dc8e2013-10-14 13:17:50 -0400165int radeon_audio = -1;
Alex Deucherf46c0122010-03-31 00:33:27 -0400166int radeon_disp_priority = 0;
Alex Deuchere2b0a8e2010-03-17 02:07:37 -0400167int radeon_hw_i2c = 0;
Dave Airlie197bbb32012-06-27 08:35:54 +0100168int radeon_pcie_gen2 = -1;
Alex Deuchera18cee12011-11-01 14:20:30 -0400169int radeon_msi = -1;
Christian König3368ff02012-05-02 15:11:21 +0200170int radeon_lockup_timeout = 10000;
Samuel Lia0a53aa2013-04-08 17:25:47 -0400171int radeon_fastfb = 0;
Alex Deucherda321c82013-04-12 13:55:22 -0400172int radeon_dpm = -1;
Alex Deucher1294d4a2013-07-16 15:58:50 -0400173int radeon_aspm = -1;
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000174int radeon_runtime_pm = -1;
Alex Deucher363eb0b2014-01-08 17:55:08 -0500175int radeon_hard_reset = 0;
Dave Airlie689b9d72005-09-30 17:09:07 +1000176
Niels de Vos61a2d072008-07-31 00:07:23 -0700177MODULE_PARM_DESC(no_wb, "Disable AGP writeback for scratch registers");
Dave Airlie689b9d72005-09-30 17:09:07 +1000178module_param_named(no_wb, radeon_no_wb, int, 0444);
179
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200180MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
181module_param_named(modeset, radeon_modeset, int, 0400);
182
183MODULE_PARM_DESC(dynclks, "Disable/Enable dynamic clocks");
184module_param_named(dynclks, radeon_dynclks, int, 0444);
185
186MODULE_PARM_DESC(r4xx_atom, "Enable ATOMBIOS modesetting for R4xx");
187module_param_named(r4xx_atom, radeon_r4xx_atom, int, 0444);
188
189MODULE_PARM_DESC(vramlimit, "Restrict VRAM for testing");
190module_param_named(vramlimit, radeon_vram_limit, int, 0600);
191
192MODULE_PARM_DESC(agpmode, "AGP Mode (-1 == PCI)");
193module_param_named(agpmode, radeon_agpmode, int, 0444);
194
Alex Deucheredcd26e2013-07-05 17:16:51 -0400195MODULE_PARM_DESC(gartsize, "Size of PCIE/IGP gart to setup in megabytes (32, 64, etc., -1 = auto)");
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200196module_param_named(gartsize, radeon_gart_size, int, 0600);
197
198MODULE_PARM_DESC(benchmark, "Run benchmark");
199module_param_named(benchmark, radeon_benchmarking, int, 0444);
200
Michel Dänzerecc0b322009-07-21 11:23:57 +0200201MODULE_PARM_DESC(test, "Run tests");
202module_param_named(test, radeon_testing, int, 0444);
203
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200204MODULE_PARM_DESC(connector_table, "Force connector table");
205module_param_named(connector_table, radeon_connector_table, int, 0444);
Dave Airlie4ce001a2009-08-13 16:32:14 +1000206
207MODULE_PARM_DESC(tv, "TV enable (0 = disable)");
208module_param_named(tv, radeon_tv, int, 0444);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200209
Alex Deucher108dc8e2013-10-14 13:17:50 -0400210MODULE_PARM_DESC(audio, "Audio enable (-1 = auto, 0 = disable, 1 = enable)");
Christian Koenigdafc3bd2009-10-11 23:49:13 +0200211module_param_named(audio, radeon_audio, int, 0444);
212
Alex Deucherf46c0122010-03-31 00:33:27 -0400213MODULE_PARM_DESC(disp_priority, "Display Priority (0 = auto, 1 = normal, 2 = high)");
214module_param_named(disp_priority, radeon_disp_priority, int, 0444);
215
Alex Deuchere2b0a8e2010-03-17 02:07:37 -0400216MODULE_PARM_DESC(hw_i2c, "hw i2c engine enable (0 = disable)");
217module_param_named(hw_i2c, radeon_hw_i2c, int, 0444);
218
Dave Airlie197bbb32012-06-27 08:35:54 +0100219MODULE_PARM_DESC(pcie_gen2, "PCIE Gen2 mode (-1 = auto, 0 = disable, 1 = enable)");
Alex Deucherd42dd572011-01-12 20:05:11 -0500220module_param_named(pcie_gen2, radeon_pcie_gen2, int, 0444);
221
Alex Deuchera18cee12011-11-01 14:20:30 -0400222MODULE_PARM_DESC(msi, "MSI support (1 = enable, 0 = disable, -1 = auto)");
223module_param_named(msi, radeon_msi, int, 0444);
224
Christian König3368ff02012-05-02 15:11:21 +0200225MODULE_PARM_DESC(lockup_timeout, "GPU lockup timeout in ms (defaul 10000 = 10 seconds, 0 = disable)");
226module_param_named(lockup_timeout, radeon_lockup_timeout, int, 0444);
227
Samuel Lia0a53aa2013-04-08 17:25:47 -0400228MODULE_PARM_DESC(fastfb, "Direct FB access for IGP chips (0 = disable, 1 = enable)");
229module_param_named(fastfb, radeon_fastfb, int, 0444);
230
Alex Deucherda321c82013-04-12 13:55:22 -0400231MODULE_PARM_DESC(dpm, "DPM support (1 = enable, 0 = disable, -1 = auto)");
232module_param_named(dpm, radeon_dpm, int, 0444);
233
Alex Deucher1294d4a2013-07-16 15:58:50 -0400234MODULE_PARM_DESC(aspm, "ASPM support (1 = enable, 0 = disable, -1 = auto)");
235module_param_named(aspm, radeon_aspm, int, 0444);
236
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000237MODULE_PARM_DESC(runpm, "PX runtime pm (1 = force enable, 0 = disable, -1 = PX only default)");
238module_param_named(runpm, radeon_runtime_pm, int, 0444);
239
Alex Deucher363eb0b2014-01-08 17:55:08 -0500240MODULE_PARM_DESC(hard_reset, "PCI config reset (1 = force enable, 0 = disable (default))");
241module_param_named(hard_reset, radeon_hard_reset, int, 0444);
242
Christian König14adc892013-01-21 13:58:46 +0100243static struct pci_device_id pciidlist[] = {
244 radeon_PCI_IDS
245};
246
247MODULE_DEVICE_TABLE(pci, pciidlist);
248
249#ifdef CONFIG_DRM_RADEON_UMS
250
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700251static int radeon_suspend(struct drm_device *dev, pm_message_t state)
252{
253 drm_radeon_private_t *dev_priv = dev->dev_private;
254
Dave Airlie03efb882009-03-10 18:36:38 +1000255 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
256 return 0;
257
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700258 /* Disable *all* interrupts */
Alex Deucher800b6992009-03-06 11:47:54 -0500259 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700260 RADEON_WRITE(R500_DxMODE_INT_MASK, 0);
261 RADEON_WRITE(RADEON_GEN_INT_CNTL, 0);
262 return 0;
263}
264
265static int radeon_resume(struct drm_device *dev)
266{
267 drm_radeon_private_t *dev_priv = dev->dev_private;
268
Dave Airlie03efb882009-03-10 18:36:38 +1000269 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_R600)
270 return 0;
271
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700272 /* Restore interrupt registers */
Alex Deucher800b6992009-03-06 11:47:54 -0500273 if ((dev_priv->flags & RADEON_FAMILY_MASK) >= CHIP_RS600)
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700274 RADEON_WRITE(R500_DxMODE_INT_MASK, dev_priv->r500_disp_irq_reg);
275 RADEON_WRITE(RADEON_GEN_INT_CNTL, dev_priv->irq_enable_reg);
276 return 0;
277}
278
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000279
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700280static const struct file_operations radeon_driver_old_fops = {
281 .owner = THIS_MODULE,
282 .open = drm_open,
283 .release = drm_release,
284 .unlocked_ioctl = drm_ioctl,
285 .mmap = drm_mmap,
286 .poll = drm_poll,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700287 .read = drm_read,
288#ifdef CONFIG_COMPAT
289 .compat_ioctl = radeon_compat_ioctl,
290#endif
291 .llseek = noop_llseek,
292};
293
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200294static struct drm_driver driver_old = {
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000295 .driver_features =
Daniel Vetter28185642013-08-08 15:41:27 +0200296 DRIVER_USE_AGP | DRIVER_PCI_DMA | DRIVER_SG |
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700297 DRIVER_HAVE_IRQ | DRIVER_HAVE_DMA | DRIVER_IRQ_SHARED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 .dev_priv_size = sizeof(drm_radeon_buf_priv_t),
Dave Airlie22eae942005-11-10 22:16:34 +1100299 .load = radeon_driver_load,
300 .firstopen = radeon_driver_firstopen,
301 .open = radeon_driver_open,
302 .preclose = radeon_driver_preclose,
303 .postclose = radeon_driver_postclose,
304 .lastclose = radeon_driver_lastclose,
305 .unload = radeon_driver_unload,
Jesse Barnes0a3e67a2008-09-30 12:14:26 -0700306 .suspend = radeon_suspend,
307 .resume = radeon_resume,
308 .get_vblank_counter = radeon_get_vblank_counter,
309 .enable_vblank = radeon_enable_vblank,
310 .disable_vblank = radeon_disable_vblank,
Dave Airlie60f2ee02008-12-19 10:22:02 +1100311 .master_create = radeon_master_create,
312 .master_destroy = radeon_master_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 .irq_preinstall = radeon_driver_irq_preinstall,
314 .irq_postinstall = radeon_driver_irq_postinstall,
315 .irq_uninstall = radeon_driver_irq_uninstall,
316 .irq_handler = radeon_driver_irq_handler,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .ioctls = radeon_ioctls,
318 .dma_ioctl = radeon_cp_buffers,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700319 .fops = &radeon_driver_old_fops,
Dave Airlie22eae942005-11-10 22:16:34 +1100320 .name = DRIVER_NAME,
321 .desc = DRIVER_DESC,
322 .date = DRIVER_DATE,
323 .major = DRIVER_MAJOR,
324 .minor = DRIVER_MINOR,
325 .patchlevel = DRIVER_PATCHLEVEL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326};
327
Christian König14adc892013-01-21 13:58:46 +0100328#endif
329
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200330static struct drm_driver kms_driver;
331
Tommi Rantala30238152012-11-09 09:19:39 +0000332static int radeon_kick_out_firmware_fb(struct pci_dev *pdev)
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000333{
334 struct apertures_struct *ap;
335 bool primary = false;
336
337 ap = alloc_apertures(1);
Tommi Rantala30238152012-11-09 09:19:39 +0000338 if (!ap)
339 return -ENOMEM;
340
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000341 ap->ranges[0].base = pci_resource_start(pdev, 0);
342 ap->ranges[0].size = pci_resource_len(pdev, 0);
343
344#ifdef CONFIG_X86
345 primary = pdev->resource[PCI_ROM_RESOURCE].flags & IORESOURCE_ROM_SHADOW;
346#endif
347 remove_conflicting_framebuffers(ap, "radeondrmfb", primary);
348 kfree(ap);
Tommi Rantala30238152012-11-09 09:19:39 +0000349
350 return 0;
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000351}
352
Greg Kroah-Hartman56550d92012-12-21 15:09:25 -0800353static int radeon_pci_probe(struct pci_dev *pdev,
354 const struct pci_device_id *ent)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200355{
Tommi Rantala30238152012-11-09 09:19:39 +0000356 int ret;
357
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000358 /* Get rid of things like offb */
Tommi Rantala30238152012-11-09 09:19:39 +0000359 ret = radeon_kick_out_firmware_fb(pdev);
360 if (ret)
361 return ret;
Benjamin Herrenschmidta56f7422010-10-06 16:39:07 +0000362
Jordan Crousedcdb1672010-05-27 13:40:25 -0600363 return drm_get_pci_dev(pdev, ent, &kms_driver);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200364}
365
366static void
367radeon_pci_remove(struct pci_dev *pdev)
368{
369 struct drm_device *dev = pci_get_drvdata(pdev);
370
371 drm_put_dev(dev);
372}
373
Dave Airlie7473e832012-09-13 12:02:30 +1000374static int radeon_pmops_suspend(struct device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200375{
Dave Airlie7473e832012-09-13 12:02:30 +1000376 struct pci_dev *pdev = to_pci_dev(dev);
377 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000378 return radeon_suspend_kms(drm_dev, true, true);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200379}
380
Dave Airlie7473e832012-09-13 12:02:30 +1000381static int radeon_pmops_resume(struct device *dev)
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200382{
Dave Airlie7473e832012-09-13 12:02:30 +1000383 struct pci_dev *pdev = to_pci_dev(dev);
384 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000385 return radeon_resume_kms(drm_dev, true, true);
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200386}
387
Dave Airlie7473e832012-09-13 12:02:30 +1000388static int radeon_pmops_freeze(struct device *dev)
389{
390 struct pci_dev *pdev = to_pci_dev(dev);
391 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000392 return radeon_suspend_kms(drm_dev, false, true);
Dave Airlie7473e832012-09-13 12:02:30 +1000393}
394
395static int radeon_pmops_thaw(struct device *dev)
396{
397 struct pci_dev *pdev = to_pci_dev(dev);
398 struct drm_device *drm_dev = pci_get_drvdata(pdev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000399 return radeon_resume_kms(drm_dev, false, true);
400}
401
402static int radeon_pmops_runtime_suspend(struct device *dev)
403{
404 struct pci_dev *pdev = to_pci_dev(dev);
405 struct drm_device *drm_dev = pci_get_drvdata(pdev);
406 int ret;
407
Dave Airlie1d8eec82014-03-27 14:09:18 +1000408 if (radeon_runtime_pm == 0) {
409 pm_runtime_forbid(dev);
410 return -EBUSY;
411 }
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000412
Dave Airlie1d8eec82014-03-27 14:09:18 +1000413 if (radeon_runtime_pm == -1 && !radeon_is_px()) {
414 pm_runtime_forbid(dev);
415 return -EBUSY;
416 }
Alex Deucher9babd352014-01-24 14:59:42 -0500417
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000418 drm_dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
419 drm_kms_helper_poll_disable(drm_dev);
420 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_OFF);
421
422 ret = radeon_suspend_kms(drm_dev, false, false);
423 pci_save_state(pdev);
424 pci_disable_device(pdev);
425 pci_set_power_state(pdev, PCI_D3cold);
426 drm_dev->switch_power_state = DRM_SWITCH_POWER_DYNAMIC_OFF;
427
428 return 0;
429}
430
431static int radeon_pmops_runtime_resume(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
437 if (radeon_runtime_pm == 0)
438 return -EINVAL;
439
Alex Deucher9babd352014-01-24 14:59:42 -0500440 if (radeon_runtime_pm == -1 && !radeon_is_px())
441 return -EINVAL;
442
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000443 drm_dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
444
445 pci_set_power_state(pdev, PCI_D0);
446 pci_restore_state(pdev);
447 ret = pci_enable_device(pdev);
448 if (ret)
449 return ret;
450 pci_set_master(pdev);
451
452 ret = radeon_resume_kms(drm_dev, false, false);
453 drm_kms_helper_poll_enable(drm_dev);
454 vga_switcheroo_set_dynamic_switch(pdev, VGA_SWITCHEROO_ON);
455 drm_dev->switch_power_state = DRM_SWITCH_POWER_ON;
456 return 0;
457}
458
459static int radeon_pmops_runtime_idle(struct device *dev)
460{
461 struct pci_dev *pdev = to_pci_dev(dev);
462 struct drm_device *drm_dev = pci_get_drvdata(pdev);
463 struct drm_crtc *crtc;
464
Dave Airlie1d8eec82014-03-27 14:09:18 +1000465 if (radeon_runtime_pm == 0) {
466 pm_runtime_forbid(dev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000467 return -EBUSY;
Dave Airlie1d8eec82014-03-27 14:09:18 +1000468 }
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000469
470 /* are we PX enabled? */
471 if (radeon_runtime_pm == -1 && !radeon_is_px()) {
472 DRM_DEBUG_DRIVER("failing to power off - not px\n");
Dave Airlie1d8eec82014-03-27 14:09:18 +1000473 pm_runtime_forbid(dev);
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000474 return -EBUSY;
475 }
476
477 list_for_each_entry(crtc, &drm_dev->mode_config.crtc_list, head) {
478 if (crtc->enabled) {
479 DRM_DEBUG_DRIVER("failing to power off - crtc active\n");
480 return -EBUSY;
481 }
482 }
483
484 pm_runtime_mark_last_busy(dev);
485 pm_runtime_autosuspend(dev);
486 /* we don't want the main rpm_idle to call suspend - we want to autosuspend */
487 return 1;
488}
489
490long radeon_drm_ioctl(struct file *filp,
491 unsigned int cmd, unsigned long arg)
492{
493 struct drm_file *file_priv = filp->private_data;
494 struct drm_device *dev;
495 long ret;
496 dev = file_priv->minor->dev;
497 ret = pm_runtime_get_sync(dev->dev);
498 if (ret < 0)
499 return ret;
500
501 ret = drm_ioctl(filp, cmd, arg);
502
503 pm_runtime_mark_last_busy(dev->dev);
504 pm_runtime_put_autosuspend(dev->dev);
505 return ret;
Dave Airlie7473e832012-09-13 12:02:30 +1000506}
507
508static const struct dev_pm_ops radeon_pm_ops = {
509 .suspend = radeon_pmops_suspend,
510 .resume = radeon_pmops_resume,
511 .freeze = radeon_pmops_freeze,
512 .thaw = radeon_pmops_thaw,
513 .poweroff = radeon_pmops_freeze,
514 .restore = radeon_pmops_resume,
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000515 .runtime_suspend = radeon_pmops_runtime_suspend,
516 .runtime_resume = radeon_pmops_runtime_resume,
517 .runtime_idle = radeon_pmops_runtime_idle,
Dave Airlie7473e832012-09-13 12:02:30 +1000518};
519
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700520static const struct file_operations radeon_driver_kms_fops = {
521 .owner = THIS_MODULE,
522 .open = drm_open,
523 .release = drm_release,
Dave Airlie10ebc0b2012-09-17 14:40:31 +1000524 .unlocked_ioctl = radeon_drm_ioctl,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700525 .mmap = radeon_mmap,
526 .poll = drm_poll,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700527 .read = drm_read,
528#ifdef CONFIG_COMPAT
529 .compat_ioctl = radeon_kms_compat_ioctl,
530#endif
531};
532
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200533static struct drm_driver kms_driver = {
534 .driver_features =
Daniel Vetter28185642013-08-08 15:41:27 +0200535 DRIVER_USE_AGP |
Daniel Vetter81e95692013-07-10 14:11:49 +0200536 DRIVER_HAVE_IRQ | DRIVER_IRQ_SHARED | DRIVER_GEM |
Christian Königf33bcab2013-08-25 18:29:03 +0200537 DRIVER_PRIME | DRIVER_RENDER,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200538 .dev_priv_size = 0,
539 .load = radeon_driver_load_kms,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200540 .open = radeon_driver_open_kms,
541 .preclose = radeon_driver_preclose_kms,
542 .postclose = radeon_driver_postclose_kms,
543 .lastclose = radeon_driver_lastclose_kms,
544 .unload = radeon_driver_unload_kms,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200545 .get_vblank_counter = radeon_get_vblank_counter_kms,
546 .enable_vblank = radeon_enable_vblank_kms,
547 .disable_vblank = radeon_disable_vblank_kms,
Mario Kleinerf5a80202010-10-23 04:42:17 +0200548 .get_vblank_timestamp = radeon_get_vblank_timestamp_kms,
549 .get_scanout_position = radeon_get_crtc_scanoutpos,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200550#if defined(CONFIG_DEBUG_FS)
551 .debugfs_init = radeon_debugfs_init,
552 .debugfs_cleanup = radeon_debugfs_cleanup,
553#endif
554 .irq_preinstall = radeon_driver_irq_preinstall_kms,
555 .irq_postinstall = radeon_driver_irq_postinstall_kms,
556 .irq_uninstall = radeon_driver_irq_uninstall_kms,
557 .irq_handler = radeon_driver_irq_handler_kms,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200558 .ioctls = radeon_ioctls_kms,
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200559 .gem_free_object = radeon_gem_object_free,
Jerome Glisse721604a2012-01-05 22:11:05 -0500560 .gem_open_object = radeon_gem_object_open,
561 .gem_close_object = radeon_gem_object_close,
Dave Airlieff72145b2011-02-07 12:16:14 +1000562 .dumb_create = radeon_mode_dumb_create,
563 .dumb_map_offset = radeon_mode_dumb_mmap,
Daniel Vetter43387b32013-07-16 09:12:04 +0200564 .dumb_destroy = drm_gem_dumb_destroy,
Arjan van de Vene08e96d2011-10-31 07:28:57 -0700565 .fops = &radeon_driver_kms_fops,
Alex Deucher40f5cf92012-05-10 18:33:13 -0400566
567 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
568 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
Aaron Plattner1e6d17a2013-01-15 20:47:44 +0000569 .gem_prime_export = drm_gem_prime_export,
570 .gem_prime_import = drm_gem_prime_import,
571 .gem_prime_pin = radeon_gem_prime_pin,
Maarten Lankhorst280cf212013-06-27 13:38:18 +0200572 .gem_prime_unpin = radeon_gem_prime_unpin,
Aaron Plattner1e6d17a2013-01-15 20:47:44 +0000573 .gem_prime_get_sg_table = radeon_gem_prime_get_sg_table,
574 .gem_prime_import_sg_table = radeon_gem_prime_import_sg_table,
575 .gem_prime_vmap = radeon_gem_prime_vmap,
576 .gem_prime_vunmap = radeon_gem_prime_vunmap,
Alex Deucher40f5cf92012-05-10 18:33:13 -0400577
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200578 .name = DRIVER_NAME,
579 .desc = DRIVER_DESC,
580 .date = DRIVER_DATE,
581 .major = KMS_DRIVER_MAJOR,
582 .minor = KMS_DRIVER_MINOR,
583 .patchlevel = KMS_DRIVER_PATCHLEVEL,
584};
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200585
586static struct drm_driver *driver;
Dave Airlie8410ea32010-12-15 03:16:38 +1000587static struct pci_driver *pdriver;
588
Christian König14adc892013-01-21 13:58:46 +0100589#ifdef CONFIG_DRM_RADEON_UMS
Dave Airlie8410ea32010-12-15 03:16:38 +1000590static struct pci_driver radeon_pci_driver = {
591 .name = DRIVER_NAME,
592 .id_table = pciidlist,
593};
Christian König14adc892013-01-21 13:58:46 +0100594#endif
Dave Airlie8410ea32010-12-15 03:16:38 +1000595
596static struct pci_driver radeon_kms_pci_driver = {
597 .name = DRIVER_NAME,
598 .id_table = pciidlist,
599 .probe = radeon_pci_probe,
600 .remove = radeon_pci_remove,
Dave Airlie7473e832012-09-13 12:02:30 +1000601 .driver.pm = &radeon_pm_ops,
Dave Airlie8410ea32010-12-15 03:16:38 +1000602};
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604static int __init radeon_init(void)
605{
Dave Airliee9ced8e2013-05-15 01:23:36 +0000606#ifdef CONFIG_VGA_CONSOLE
607 if (vgacon_text_force() && radeon_modeset == -1) {
608 DRM_INFO("VGACON disable radeon kernel modesetting.\n");
609 radeon_modeset = 0;
610 }
611#endif
612 /* set to modesetting by default if not nomodeset */
613 if (radeon_modeset == -1)
614 radeon_modeset = 1;
615
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200616 if (radeon_modeset == 1) {
617 DRM_INFO("radeon kernel modesetting enabled.\n");
618 driver = &kms_driver;
Dave Airlie8410ea32010-12-15 03:16:38 +1000619 pdriver = &radeon_kms_pci_driver;
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200620 driver->driver_features |= DRIVER_MODESET;
621 driver->num_ioctls = radeon_max_kms_ioctl;
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000622 radeon_register_atpx_handler();
Christian König14adc892013-01-21 13:58:46 +0100623
624 } else {
625#ifdef CONFIG_DRM_RADEON_UMS
626 DRM_INFO("radeon userspace modesetting enabled.\n");
627 driver = &driver_old;
628 pdriver = &radeon_pci_driver;
629 driver->driver_features &= ~DRIVER_MODESET;
630 driver->num_ioctls = radeon_max_ioctl;
631#else
632 DRM_ERROR("No UMS support in radeon module!\n");
633 return -EINVAL;
634#endif
Jerome Glisse771fe6b2009-06-05 14:42:42 +0200635 }
Christian König14adc892013-01-21 13:58:46 +0100636
637 /* let modprobe override vga console setting */
Dave Airlie8410ea32010-12-15 03:16:38 +1000638 return drm_pci_init(driver, pdriver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641static void __exit radeon_exit(void)
642{
Dave Airlie8410ea32010-12-15 03:16:38 +1000643 drm_pci_exit(driver, pdriver);
Dave Airlie6a9ee8a2010-02-01 15:38:10 +1000644 radeon_unregister_atpx_handler();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Jerome Glisse176f613e2009-06-22 18:16:13 +0200647module_init(radeon_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648module_exit(radeon_exit);
649
Dave Airlieb5e89ed2005-09-25 14:28:13 +1000650MODULE_AUTHOR(DRIVER_AUTHOR);
651MODULE_DESCRIPTION(DRIVER_DESC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652MODULE_LICENSE("GPL and additional rights");