radeon_drv.c revision 282199
1/**
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
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: stable/10/sys/dev/drm2/radeon/radeon_drv.c 282199 2015-04-28 19:35:05Z dumbbell $");
34
35#include <dev/drm2/drmP.h>
36#include <dev/drm2/radeon/radeon_drm.h>
37#include "radeon_drv.h"
38#include "radeon_gem.h"
39#include "radeon_kms.h"
40#include "radeon_irq_kms.h"
41
42#include <dev/drm2/drm_pciids.h>
43
44#include "fb_if.h"
45
46/*
47 * KMS wrapper.
48 * - 2.0.0 - initial interface
49 * - 2.1.0 - add square tiling interface
50 * - 2.2.0 - add r6xx/r7xx const buffer support
51 * - 2.3.0 - add MSPOS + 3D texture + r500 VAP regs
52 * - 2.4.0 - add crtc id query
53 * - 2.5.0 - add get accel 2 to work around ddx breakage for evergreen
54 * - 2.6.0 - add tiling config query (r6xx+), add initial HiZ support (r300->r500)
55 *   2.7.0 - fixups for r600 2D tiling support. (no external ABI change), add eg dyn gpr regs
56 *   2.8.0 - pageflip support, r500 US_FORMAT regs. r500 ARGB2101010 colorbuf, r300->r500 CMASK, clock crystal query
57 *   2.9.0 - r600 tiling (s3tc,rgtc) working, SET_PREDICATION packet 3 on r600 + eg, backend query
58 *   2.10.0 - fusion 2D tiling
59 *   2.11.0 - backend map, initial compute support for the CS checker
60 *   2.12.0 - RADEON_CS_KEEP_TILING_FLAGS
61 *   2.13.0 - virtual memory support, streamout
62 *   2.14.0 - add evergreen tiling informations
63 *   2.15.0 - add max_pipes query
64 *   2.16.0 - fix evergreen 2D tiled surface calculation
65 *   2.17.0 - add STRMOUT_BASE_UPDATE for r7xx
66 *   2.18.0 - r600-eg: allow "invalid" DB formats
67 *   2.19.0 - r600-eg: MSAA textures
68 *   2.20.0 - r600-si: RADEON_INFO_TIMESTAMP query
69 *   2.21.0 - r600-r700: FMASK and CMASK
70 *   2.22.0 - r600 only: RESOLVE_BOX allowed
71 *   2.23.0 - allow STRMOUT_BASE_UPDATE on RS780 and RS880
72 *   2.24.0 - eg only: allow MIP_ADDRESS=0 for MSAA textures
73 *   2.25.0 - eg+: new info request for num SE and num SH
74 *   2.26.0 - r600-eg: fix htile size computation
75 *   2.27.0 - r600-SI: Add CS ioctl support for async DMA
76 *   2.28.0 - r600-eg: Add MEM_WRITE packet support
77 *   2.29.0 - R500 FP16 color clear registers
78 */
79#define KMS_DRIVER_MAJOR	2
80#define KMS_DRIVER_MINOR	29
81#define KMS_DRIVER_PATCHLEVEL	0
82int radeon_suspend_kms(struct drm_device *dev);
83int radeon_resume_kms(struct drm_device *dev);
84extern int radeon_get_crtc_scanoutpos(struct drm_device *dev, int crtc,
85				      int *vpos, int *hpos);
86extern struct drm_ioctl_desc radeon_ioctls_kms[];
87extern int radeon_max_kms_ioctl;
88#ifdef FREEBSD_WIP
89int radeon_mmap(struct file *filp, struct vm_area_struct *vma);
90#endif /* FREEBSD_WIP */
91int radeon_mode_dumb_mmap(struct drm_file *filp,
92			  struct drm_device *dev,
93			  uint32_t handle, uint64_t *offset_p);
94int radeon_mode_dumb_create(struct drm_file *file_priv,
95			    struct drm_device *dev,
96			    struct drm_mode_create_dumb *args);
97int radeon_mode_dumb_destroy(struct drm_file *file_priv,
98			     struct drm_device *dev,
99			     uint32_t handle);
100struct dma_buf *radeon_gem_prime_export(struct drm_device *dev,
101					struct drm_gem_object *obj,
102					int flags);
103struct drm_gem_object *radeon_gem_prime_import(struct drm_device *dev,
104					       struct dma_buf *dma_buf);
105
106#if defined(CONFIG_DEBUG_FS)
107int radeon_debugfs_init(struct drm_minor *minor);
108void radeon_debugfs_cleanup(struct drm_minor *minor);
109#endif
110
111
112int radeon_no_wb;
113int radeon_modeset = 1;
114int radeon_dynclks = -1;
115int radeon_r4xx_atom = 0;
116int radeon_agpmode = 0;
117int radeon_vram_limit = 0;
118int radeon_gart_size = 512; /* default gart size */
119int radeon_benchmarking = 0;
120int radeon_testing = 0;
121int radeon_connector_table = 0;
122int radeon_tv = 1;
123int radeon_audio = 0;
124int radeon_disp_priority = 0;
125int radeon_hw_i2c = 0;
126int radeon_pcie_gen2 = -1;
127int radeon_msi = -1;
128int radeon_lockup_timeout = 10000;
129
130MODULE_PARM_DESC(no_wb, "Disable AGP writeback for scratch registers");
131module_param_named(no_wb, radeon_no_wb, int, 0444);
132
133MODULE_PARM_DESC(modeset, "Disable/Enable modesetting");
134module_param_named(modeset, radeon_modeset, int, 0400);
135
136MODULE_PARM_DESC(dynclks, "Disable/Enable dynamic clocks");
137module_param_named(dynclks, radeon_dynclks, int, 0444);
138
139MODULE_PARM_DESC(r4xx_atom, "Enable ATOMBIOS modesetting for R4xx");
140module_param_named(r4xx_atom, radeon_r4xx_atom, int, 0444);
141
142MODULE_PARM_DESC(vramlimit, "Restrict VRAM for testing");
143module_param_named(vramlimit, radeon_vram_limit, int, 0600);
144
145MODULE_PARM_DESC(agpmode, "AGP Mode (-1 == PCI)");
146module_param_named(agpmode, radeon_agpmode, int, 0444);
147
148MODULE_PARM_DESC(gartsize, "Size of PCIE/IGP gart to setup in megabytes (32, 64, etc)");
149module_param_named(gartsize, radeon_gart_size, int, 0600);
150
151MODULE_PARM_DESC(benchmark, "Run benchmark");
152module_param_named(benchmark, radeon_benchmarking, int, 0444);
153
154MODULE_PARM_DESC(test, "Run tests");
155module_param_named(test, radeon_testing, int, 0444);
156
157MODULE_PARM_DESC(connector_table, "Force connector table");
158module_param_named(connector_table, radeon_connector_table, int, 0444);
159
160MODULE_PARM_DESC(tv, "TV enable (0 = disable)");
161module_param_named(tv, radeon_tv, int, 0444);
162
163MODULE_PARM_DESC(audio, "Audio enable (1 = enable)");
164module_param_named(audio, radeon_audio, int, 0444);
165
166MODULE_PARM_DESC(disp_priority, "Display Priority (0 = auto, 1 = normal, 2 = high)");
167module_param_named(disp_priority, radeon_disp_priority, int, 0444);
168
169MODULE_PARM_DESC(hw_i2c, "hw i2c engine enable (0 = disable)");
170module_param_named(hw_i2c, radeon_hw_i2c, int, 0444);
171
172MODULE_PARM_DESC(pcie_gen2, "PCIE Gen2 mode (-1 = auto, 0 = disable, 1 = enable)");
173module_param_named(pcie_gen2, radeon_pcie_gen2, int, 0444);
174
175MODULE_PARM_DESC(msi, "MSI support (1 = enable, 0 = disable, -1 = auto)");
176module_param_named(msi, radeon_msi, int, 0444);
177
178MODULE_PARM_DESC(lockup_timeout, "GPU lockup timeout in ms (defaul 10000 = 10 seconds, 0 = disable)");
179module_param_named(lockup_timeout, radeon_lockup_timeout, int, 0444);
180
181static drm_pci_id_list_t pciidlist[] = {
182	radeon_PCI_IDS
183};
184
185static struct drm_driver kms_driver;
186
187static int radeon_sysctl_init(struct drm_device *dev, struct sysctl_ctx_list *ctx,
188			      struct sysctl_oid *top)
189{
190	return drm_add_busid_modesetting(dev, ctx, top);
191}
192
193static struct drm_driver kms_driver = {
194	.driver_features =
195	    DRIVER_USE_AGP | DRIVER_USE_MTRR | DRIVER_PCI_DMA | DRIVER_SG |
196	    DRIVER_HAVE_IRQ | DRIVER_HAVE_DMA | DRIVER_IRQ_SHARED | DRIVER_GEM |
197	    DRIVER_PRIME,
198#ifdef FREEBSD_WIP
199	.dev_priv_size = 0,
200#endif /* FREEBSD_WIP */
201	.load = radeon_driver_load_kms,
202	.firstopen = radeon_driver_firstopen_kms,
203	.open = radeon_driver_open_kms,
204	.preclose = radeon_driver_preclose_kms,
205	.postclose = radeon_driver_postclose_kms,
206	.lastclose = radeon_driver_lastclose_kms,
207	.unload = radeon_driver_unload_kms,
208#ifdef FREEBSD_WIP
209	.suspend = radeon_suspend_kms,
210	.resume = radeon_resume_kms,
211#endif /* FREEBSD_WIP */
212	.get_vblank_counter = radeon_get_vblank_counter_kms,
213	.enable_vblank = radeon_enable_vblank_kms,
214	.disable_vblank = radeon_disable_vblank_kms,
215	.get_vblank_timestamp = radeon_get_vblank_timestamp_kms,
216	.get_scanout_position = radeon_get_crtc_scanoutpos,
217#if defined(CONFIG_DEBUG_FS)
218	.debugfs_init = radeon_debugfs_init,
219	.debugfs_cleanup = radeon_debugfs_cleanup,
220#endif
221	.irq_preinstall = radeon_driver_irq_preinstall_kms,
222	.irq_postinstall = radeon_driver_irq_postinstall_kms,
223	.irq_uninstall = radeon_driver_irq_uninstall_kms,
224	.irq_handler = radeon_driver_irq_handler_kms,
225	.sysctl_init = radeon_sysctl_init,
226	.ioctls = radeon_ioctls_kms,
227	.gem_init_object = radeon_gem_object_init,
228	.gem_free_object = radeon_gem_object_free,
229	.gem_open_object = radeon_gem_object_open,
230	.gem_close_object = radeon_gem_object_close,
231	.dma_ioctl = radeon_dma_ioctl_kms,
232	.dumb_create = radeon_mode_dumb_create,
233	.dumb_map_offset = radeon_mode_dumb_mmap,
234	.dumb_destroy = radeon_mode_dumb_destroy,
235#ifdef FREEBSD_WIP
236	.fops = &radeon_driver_kms_fops,
237#endif /* FREEBSD_WIP */
238
239#ifdef FREEBSD_WIP
240	.prime_handle_to_fd = drm_gem_prime_handle_to_fd,
241	.prime_fd_to_handle = drm_gem_prime_fd_to_handle,
242	.gem_prime_export = radeon_gem_prime_export,
243	.gem_prime_import = radeon_gem_prime_import,
244#endif /* FREEBSD_WIP */
245
246	.name = DRIVER_NAME,
247	.desc = DRIVER_DESC,
248	.date = DRIVER_DATE,
249	.major = KMS_DRIVER_MAJOR,
250	.minor = KMS_DRIVER_MINOR,
251	.patchlevel = KMS_DRIVER_PATCHLEVEL,
252};
253
254#ifdef FREEBSD_WIP
255static int __init radeon_init(void)
256{
257	driver = &driver_old;
258	pdriver = &radeon_pci_driver;
259	driver->num_ioctls = radeon_max_ioctl;
260#ifdef CONFIG_VGA_CONSOLE
261	if (vgacon_text_force() && radeon_modeset == -1) {
262		DRM_INFO("VGACON disable radeon kernel modesetting.\n");
263		driver = &driver_old;
264		pdriver = &radeon_pci_driver;
265		driver->driver_features &= ~DRIVER_MODESET;
266		radeon_modeset = 0;
267	}
268#endif
269	/* if enabled by default */
270	if (radeon_modeset == -1) {
271#ifdef CONFIG_DRM_RADEON_KMS
272		DRM_INFO("radeon defaulting to kernel modesetting.\n");
273		radeon_modeset = 1;
274#else
275		DRM_INFO("radeon defaulting to userspace modesetting.\n");
276		radeon_modeset = 0;
277#endif
278	}
279	if (radeon_modeset == 1) {
280		DRM_INFO("radeon kernel modesetting enabled.\n");
281		driver = &kms_driver;
282		pdriver = &radeon_kms_pci_driver;
283		driver->driver_features |= DRIVER_MODESET;
284		driver->num_ioctls = radeon_max_kms_ioctl;
285		radeon_register_atpx_handler();
286	}
287	/* if the vga console setting is enabled still
288	 * let modprobe override it */
289	return drm_pci_init(driver, pdriver);
290}
291
292static void __exit radeon_exit(void)
293{
294	drm_pci_exit(driver, pdriver);
295	radeon_unregister_atpx_handler();
296}
297#endif /* FREEBSD_WIP */
298
299/* =================================================================== */
300
301static int
302radeon_probe(device_t kdev)
303{
304
305	return (-drm_probe_helper(kdev, pciidlist));
306}
307
308static int
309radeon_attach(device_t kdev)
310{
311
312	if (radeon_modeset == 1) {
313		kms_driver.driver_features |= DRIVER_MODESET;
314		kms_driver.num_ioctls = radeon_max_kms_ioctl;
315#ifdef COMPAT_FREEBSD32
316		kms_driver.compat_ioctls = radeon_compat_ioctls;
317		kms_driver.num_compat_ioctls = &radeon_num_compat_ioctls;
318#endif
319		radeon_register_atpx_handler();
320	}
321	return (-drm_attach_helper(kdev, pciidlist, &kms_driver));
322}
323
324static int
325radeon_suspend(device_t kdev)
326{
327	struct drm_device *dev;
328	int ret;
329
330	dev = device_get_softc(kdev);
331	ret = radeon_suspend_kms(dev);
332	if (ret)
333		return (-ret);
334
335	ret = bus_generic_suspend(kdev);
336
337	return (ret);
338}
339
340static int
341radeon_resume(device_t kdev)
342{
343	struct drm_device *dev;
344	int ret;
345
346	dev = device_get_softc(kdev);
347	ret = radeon_resume_kms(dev);
348	if (ret)
349		return (-ret);
350
351	ret = bus_generic_resume(kdev);
352
353	return (ret);
354}
355
356extern struct fb_info *	radeon_fb_helper_getinfo(device_t kdev);
357
358static device_method_t radeon_methods[] = {
359	/* Device interface */
360	DEVMETHOD(device_probe,		radeon_probe),
361	DEVMETHOD(device_attach,	radeon_attach),
362	DEVMETHOD(device_suspend,	radeon_suspend),
363	DEVMETHOD(device_resume,	radeon_resume),
364	DEVMETHOD(device_detach,	drm_generic_detach),
365
366	/* Framebuffer service methods */
367	DEVMETHOD(fb_getinfo,		radeon_fb_helper_getinfo),
368
369	DEVMETHOD_END
370};
371
372static driver_t radeon_driver = {
373	"drmn",
374	radeon_methods,
375	sizeof(struct drm_device)
376};
377
378extern devclass_t drm_devclass;
379DRIVER_MODULE_ORDERED(radeonkms, vgapci, radeon_driver, drm_devclass,
380    NULL, NULL, SI_ORDER_ANY);
381MODULE_DEPEND(radeonkms, drmn, 1, 1, 1);
382MODULE_DEPEND(radeonkms, agp, 1, 1, 1);
383MODULE_DEPEND(radeonkms, iicbus, 1, 1, 1);
384MODULE_DEPEND(radeonkms, iic, 1, 1, 1);
385MODULE_DEPEND(radeonkms, iicbb, 1, 1, 1);
386MODULE_DEPEND(radeonkms, firmware, 1, 1, 1);
387