1/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 *          Alex Deucher
26 *          Jerome Glisse
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <dev/drm2/drmP.h>
33#include <dev/drm2/drm_crtc_helper.h>
34#include <dev/drm2/radeon/radeon_drm.h>
35#include "radeon_reg.h"
36#include "radeon_irq_kms.h"
37#include "radeon.h"
38#include "atom.h"
39
40#define RADEON_WAIT_IDLE_TIMEOUT 200
41
42/**
43 * radeon_driver_irq_handler_kms - irq handler for KMS
44 *
45 * @DRM_IRQ_ARGS: args
46 *
47 * This is the irq handler for the radeon KMS driver (all asics).
48 * radeon_irq_process is a macro that points to the per-asic
49 * irq handler callback.
50 */
51irqreturn_t radeon_driver_irq_handler_kms(DRM_IRQ_ARGS)
52{
53	struct drm_device *dev = (struct drm_device *) arg;
54	struct radeon_device *rdev = dev->dev_private;
55
56	return radeon_irq_process(rdev);
57}
58
59/*
60 * Handle hotplug events outside the interrupt handler proper.
61 */
62/**
63 * radeon_hotplug_work_func - display hotplug work handler
64 *
65 * @work: work struct
66 *
67 * This is the hot plug event work handler (all asics).
68 * The work gets scheduled from the irq handler if there
69 * was a hot plug interrupt.  It walks the connector table
70 * and calls the hotplug handler for each one, then sends
71 * a drm hotplug event to alert userspace.
72 */
73static void radeon_hotplug_work_func(void *arg, int pending)
74{
75	struct radeon_device *rdev = arg;
76	struct drm_device *dev = rdev->ddev;
77	struct drm_mode_config *mode_config = &dev->mode_config;
78	struct drm_connector *connector;
79
80	if (mode_config->num_connector) {
81		list_for_each_entry(connector, &mode_config->connector_list, head)
82			radeon_connector_hotplug(connector);
83	}
84	/* Just fire off a uevent and let userspace tell us what to do */
85	drm_helper_hpd_irq_event(dev);
86}
87
88/**
89 * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
90 *
91 * @dev: drm dev pointer
92 *
93 * Gets the hw ready to enable irqs (all asics).
94 * This function disables all interrupt sources on the GPU.
95 */
96void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
97{
98	struct radeon_device *rdev = dev->dev_private;
99	unsigned long irqflags;
100	unsigned i;
101
102	DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
103	/* Disable *all* interrupts */
104	for (i = 0; i < RADEON_NUM_RINGS; i++)
105		atomic_set(&rdev->irq.ring_int[i], 0);
106	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
107		rdev->irq.hpd[i] = false;
108	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
109		rdev->irq.crtc_vblank_int[i] = false;
110		atomic_set(&rdev->irq.pflip[i], 0);
111		rdev->irq.afmt[i] = false;
112	}
113	radeon_irq_set(rdev);
114	DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
115	/* Clear bits */
116	radeon_irq_process(rdev);
117}
118
119/**
120 * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
121 *
122 * @dev: drm dev pointer
123 *
124 * Handles stuff to be done after enabling irqs (all asics).
125 * Returns 0 on success.
126 */
127int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
128{
129	dev->max_vblank_count = 0x001fffff;
130	return 0;
131}
132
133/**
134 * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
135 *
136 * @dev: drm dev pointer
137 *
138 * This function disables all interrupt sources on the GPU (all asics).
139 */
140void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
141{
142	struct radeon_device *rdev = dev->dev_private;
143	unsigned long irqflags;
144	unsigned i;
145
146	if (rdev == NULL) {
147		return;
148	}
149	DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
150	/* Disable *all* interrupts */
151	for (i = 0; i < RADEON_NUM_RINGS; i++)
152		atomic_set(&rdev->irq.ring_int[i], 0);
153	for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
154		rdev->irq.hpd[i] = false;
155	for (i = 0; i < RADEON_MAX_CRTCS; i++) {
156		rdev->irq.crtc_vblank_int[i] = false;
157		atomic_set(&rdev->irq.pflip[i], 0);
158		rdev->irq.afmt[i] = false;
159	}
160	radeon_irq_set(rdev);
161	DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
162}
163
164/**
165 * radeon_msi_ok - asic specific msi checks
166 *
167 * @rdev: radeon device pointer
168 *
169 * Handles asic specific MSI checks to determine if
170 * MSIs should be enabled on a particular chip (all asics).
171 * Returns true if MSIs should be enabled, false if MSIs
172 * should not be enabled.
173 */
174static bool radeon_msi_ok(struct radeon_device *rdev)
175{
176	/* RV370/RV380 was first asic with MSI support */
177	if (rdev->family < CHIP_RV380)
178		return false;
179
180	/* MSIs don't work on AGP */
181	if (rdev->flags & RADEON_IS_AGP)
182		return false;
183
184	/* force MSI on */
185	if (radeon_msi == 1)
186		return true;
187	else if (radeon_msi == 0)
188		return false;
189
190	/* Quirks */
191	/* HP RS690 only seems to work with MSIs. */
192	if ((rdev->ddev->pci_device == 0x791f) &&
193	    (rdev->ddev->pci_subvendor == 0x103c) &&
194	    (rdev->ddev->pci_subdevice == 0x30c2))
195		return true;
196
197	/* Dell RS690 only seems to work with MSIs. */
198	if ((rdev->ddev->pci_device == 0x791f) &&
199	    (rdev->ddev->pci_subvendor == 0x1028) &&
200	    (rdev->ddev->pci_subdevice == 0x01fc))
201		return true;
202
203	/* Dell RS690 only seems to work with MSIs. */
204	if ((rdev->ddev->pci_device == 0x791f) &&
205	    (rdev->ddev->pci_subvendor == 0x1028) &&
206	    (rdev->ddev->pci_subdevice == 0x01fd))
207		return true;
208
209	/* Gateway RS690 only seems to work with MSIs. */
210	if ((rdev->ddev->pci_device == 0x791f) &&
211	    (rdev->ddev->pci_subvendor == 0x107b) &&
212	    (rdev->ddev->pci_subdevice == 0x0185))
213		return true;
214
215	/* try and enable MSIs by default on all RS690s */
216	if (rdev->family == CHIP_RS690)
217		return true;
218
219	/* RV515 seems to have MSI issues where it loses
220	 * MSI rearms occasionally. This leads to lockups and freezes.
221	 * disable it by default.
222	 */
223	if (rdev->family == CHIP_RV515)
224		return false;
225	if (rdev->flags & RADEON_IS_IGP) {
226		/* APUs work fine with MSIs */
227		if (rdev->family >= CHIP_PALM)
228			return true;
229		/* lots of IGPs have problems with MSIs */
230		return false;
231	}
232
233	return true;
234}
235
236/**
237 * radeon_irq_kms_init - init driver interrupt info
238 *
239 * @rdev: radeon device pointer
240 *
241 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
242 * Returns 0 for success, error for failure.
243 */
244int radeon_irq_kms_init(struct radeon_device *rdev)
245{
246	int r = 0;
247
248	TASK_INIT(&rdev->hotplug_work, 0, radeon_hotplug_work_func, rdev);
249	TASK_INIT(&rdev->audio_work, 0, r600_audio_update_hdmi, rdev);
250
251	DRM_SPININIT(&rdev->irq.lock, "drm__radeon_device__irq__lock");
252	r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
253	if (r) {
254		return r;
255	}
256	/* enable msi */
257	rdev->msi_enabled = 0;
258
259	if (radeon_msi_ok(rdev)) {
260		int ret = drm_pci_enable_msi(rdev->ddev);
261		if (!ret) {
262			rdev->msi_enabled = 1;
263			dev_info(rdev->dev, "radeon: using MSI.\n");
264		}
265	}
266	rdev->irq.installed = true;
267	r = drm_irq_install(rdev->ddev);
268	if (r) {
269		rdev->irq.installed = false;
270		return r;
271	}
272	DRM_INFO("radeon: irq initialized.\n");
273	return 0;
274}
275
276/**
277 * radeon_irq_kms_fini - tear down driver interrrupt info
278 *
279 * @rdev: radeon device pointer
280 *
281 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
282 */
283void radeon_irq_kms_fini(struct radeon_device *rdev)
284{
285	drm_vblank_cleanup(rdev->ddev);
286	if (rdev->irq.installed) {
287		drm_irq_uninstall(rdev->ddev);
288		rdev->irq.installed = false;
289		if (rdev->msi_enabled)
290			drm_pci_disable_msi(rdev->ddev);
291	}
292	taskqueue_drain(rdev->tq, &rdev->hotplug_work);
293}
294
295/**
296 * radeon_irq_kms_sw_irq_get - enable software interrupt
297 *
298 * @rdev: radeon device pointer
299 * @ring: ring whose interrupt you want to enable
300 *
301 * Enables the software interrupt for a specific ring (all asics).
302 * The software interrupt is generally used to signal a fence on
303 * a particular ring.
304 */
305void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
306{
307	unsigned long irqflags;
308
309	if (!rdev->ddev->irq_enabled)
310		return;
311
312	if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
313		DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
314		radeon_irq_set(rdev);
315		DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
316	}
317}
318
319/**
320 * radeon_irq_kms_sw_irq_put - disable software interrupt
321 *
322 * @rdev: radeon device pointer
323 * @ring: ring whose interrupt you want to disable
324 *
325 * Disables the software interrupt for a specific ring (all asics).
326 * The software interrupt is generally used to signal a fence on
327 * a particular ring.
328 */
329void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
330{
331	unsigned long irqflags;
332
333	if (!rdev->ddev->irq_enabled)
334		return;
335
336	if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
337		DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
338		radeon_irq_set(rdev);
339		DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
340	}
341}
342
343/**
344 * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
345 *
346 * @rdev: radeon device pointer
347 * @crtc: crtc whose interrupt you want to enable
348 *
349 * Enables the pageflip interrupt for a specific crtc (all asics).
350 * For pageflips we use the vblank interrupt source.
351 */
352void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
353{
354	unsigned long irqflags;
355
356	if (crtc < 0 || crtc >= rdev->num_crtc)
357		return;
358
359	if (!rdev->ddev->irq_enabled)
360		return;
361
362	if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
363		DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
364		radeon_irq_set(rdev);
365		DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
366	}
367}
368
369/**
370 * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
371 *
372 * @rdev: radeon device pointer
373 * @crtc: crtc whose interrupt you want to disable
374 *
375 * Disables the pageflip interrupt for a specific crtc (all asics).
376 * For pageflips we use the vblank interrupt source.
377 */
378void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
379{
380	unsigned long irqflags;
381
382	if (crtc < 0 || crtc >= rdev->num_crtc)
383		return;
384
385	if (!rdev->ddev->irq_enabled)
386		return;
387
388	if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
389		DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
390		radeon_irq_set(rdev);
391		DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
392	}
393}
394
395/**
396 * radeon_irq_kms_enable_afmt - enable audio format change interrupt
397 *
398 * @rdev: radeon device pointer
399 * @block: afmt block whose interrupt you want to enable
400 *
401 * Enables the afmt change interrupt for a specific afmt block (all asics).
402 */
403void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
404{
405	unsigned long irqflags;
406
407	if (!rdev->ddev->irq_enabled)
408		return;
409
410	DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
411	rdev->irq.afmt[block] = true;
412	radeon_irq_set(rdev);
413	DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
414
415}
416
417/**
418 * radeon_irq_kms_disable_afmt - disable audio format change interrupt
419 *
420 * @rdev: radeon device pointer
421 * @block: afmt block whose interrupt you want to disable
422 *
423 * Disables the afmt change interrupt for a specific afmt block (all asics).
424 */
425void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
426{
427	unsigned long irqflags;
428
429	if (!rdev->ddev->irq_enabled)
430		return;
431
432	DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
433	rdev->irq.afmt[block] = false;
434	radeon_irq_set(rdev);
435	DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
436}
437
438/**
439 * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
440 *
441 * @rdev: radeon device pointer
442 * @hpd_mask: mask of hpd pins you want to enable.
443 *
444 * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
445 */
446void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
447{
448	unsigned long irqflags;
449	int i;
450
451	if (!rdev->ddev->irq_enabled)
452		return;
453
454	DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
455	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
456		rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
457	radeon_irq_set(rdev);
458	DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
459}
460
461/**
462 * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
463 *
464 * @rdev: radeon device pointer
465 * @hpd_mask: mask of hpd pins you want to disable.
466 *
467 * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
468 */
469void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
470{
471	unsigned long irqflags;
472	int i;
473
474	if (!rdev->ddev->irq_enabled)
475		return;
476
477	DRM_SPINLOCK_IRQSAVE(&rdev->irq.lock, irqflags);
478	for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
479		rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
480	radeon_irq_set(rdev);
481	DRM_SPINUNLOCK_IRQRESTORE(&rdev->irq.lock, irqflags);
482}
483
484