Merge branch 'vendor/TCPDUMP' (version 4.3.0 -> 4.9.3)
[dragonfly.git] / sys / dev / drm / radeon / radeon_irq_kms.c
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 #include <drm/drmP.h>
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/radeon_drm.h>
31 #include "radeon_reg.h"
32 #include "radeon_irq_kms.h"
33 #include "radeon.h"
34 #include "atom.h"
35
36 #ifdef PM_TODO
37 #include <linux/pm_runtime.h>
38 #endif
39
40 #define RADEON_WAIT_IDLE_TIMEOUT 200
41
42 /**
43  * radeon_driver_irq_handler_kms - irq handler for KMS
44  *
45  * @void *arg: 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  */
51 irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
52 {
53         struct drm_device *dev = (struct drm_device *) arg;
54         struct radeon_device *rdev = dev->dev_private;
55         irqreturn_t ret;
56
57         ret = radeon_irq_process(rdev);
58 #ifdef PM_TODO
59         if (ret == IRQ_HANDLED)
60                 pm_runtime_mark_last_busy(dev->dev);
61 #endif
62         return ret;
63 }
64
65 /*
66  * Handle hotplug events outside the interrupt handler proper.
67  */
68 /**
69  * radeon_hotplug_work_func - display hotplug work handler
70  *
71  * @work: work struct
72  *
73  * This is the hot plug event work handler (all asics).
74  * The work gets scheduled from the irq handler if there
75  * was a hot plug interrupt.  It walks the connector table
76  * and calls the hotplug handler for each one, then sends
77  * a drm hotplug event to alert userspace.
78  */
79 static void radeon_hotplug_work_func(struct work_struct *work)
80 {
81         struct radeon_device *rdev = container_of(work, struct radeon_device,
82                                                   hotplug_work.work);
83         struct drm_device *dev = rdev->ddev;
84         struct drm_mode_config *mode_config = &dev->mode_config;
85         struct drm_connector *connector;
86
87         /* we can race here at startup, some boards seem to trigger
88          * hotplug irqs when they shouldn't. */
89         if (!rdev->mode_info.mode_config_initialized)
90                 return;
91
92         mutex_lock(&mode_config->mutex);
93         if (mode_config->num_connector) {
94                 list_for_each_entry(connector, &mode_config->connector_list, head)
95                         radeon_connector_hotplug(connector);
96         }
97         mutex_unlock(&mode_config->mutex);
98         /* Just fire off a uevent and let userspace tell us what to do */
99         drm_helper_hpd_irq_event(dev);
100 }
101
102 static void radeon_dp_work_func(struct work_struct *work)
103 {
104         struct radeon_device *rdev = container_of(work, struct radeon_device,
105                                                   dp_work);
106         struct drm_device *dev = rdev->ddev;
107         struct drm_mode_config *mode_config = &dev->mode_config;
108         struct drm_connector *connector;
109
110         /* this should take a mutex */
111         if (mode_config->num_connector) {
112                 list_for_each_entry(connector, &mode_config->connector_list, head)
113                         radeon_connector_hotplug(connector);
114         }
115 }
116 /**
117  * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
118  *
119  * @dev: drm dev pointer
120  *
121  * Gets the hw ready to enable irqs (all asics).
122  * This function disables all interrupt sources on the GPU.
123  */
124 void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
125 {
126         struct radeon_device *rdev = dev->dev_private;
127         unsigned i;
128
129         lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
130         /* Disable *all* interrupts */
131         for (i = 0; i < RADEON_NUM_RINGS; i++)
132                 atomic_set(&rdev->irq.ring_int[i], 0);
133         rdev->irq.dpm_thermal = false;
134         for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
135                 rdev->irq.hpd[i] = false;
136         for (i = 0; i < RADEON_MAX_CRTCS; i++) {
137                 rdev->irq.crtc_vblank_int[i] = false;
138                 atomic_set(&rdev->irq.pflip[i], 0);
139                 rdev->irq.afmt[i] = false;
140         }
141         radeon_irq_set(rdev);
142         lockmgr(&rdev->irq.lock, LK_RELEASE);
143         /* Clear bits */
144         radeon_irq_process(rdev);
145 }
146
147 /**
148  * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
149  *
150  * @dev: drm dev pointer
151  *
152  * Handles stuff to be done after enabling irqs (all asics).
153  * Returns 0 on success.
154  */
155 int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
156 {
157         struct radeon_device *rdev = dev->dev_private;
158
159         if (ASIC_IS_AVIVO(rdev))
160                 dev->max_vblank_count = 0x00ffffff;
161         else
162                 dev->max_vblank_count = 0x001fffff;
163
164         return 0;
165 }
166
167 /**
168  * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
169  *
170  * @dev: drm dev pointer
171  *
172  * This function disables all interrupt sources on the GPU (all asics).
173  */
174 void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
175 {
176         struct radeon_device *rdev = dev->dev_private;
177         unsigned i;
178
179         if (rdev == NULL) {
180                 return;
181         }
182         lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
183         /* Disable *all* interrupts */
184         for (i = 0; i < RADEON_NUM_RINGS; i++)
185                 atomic_set(&rdev->irq.ring_int[i], 0);
186         rdev->irq.dpm_thermal = false;
187         for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
188                 rdev->irq.hpd[i] = false;
189         for (i = 0; i < RADEON_MAX_CRTCS; i++) {
190                 rdev->irq.crtc_vblank_int[i] = false;
191                 atomic_set(&rdev->irq.pflip[i], 0);
192                 rdev->irq.afmt[i] = false;
193         }
194         radeon_irq_set(rdev);
195         lockmgr(&rdev->irq.lock, LK_RELEASE);
196 }
197
198 /**
199  * radeon_msi_ok - asic specific msi checks
200  *
201  * @rdev: radeon device pointer
202  *
203  * Handles asic specific MSI checks to determine if
204  * MSIs should be enabled on a particular chip (all asics).
205  * Returns true if MSIs should be enabled, false if MSIs
206  * should not be enabled.
207  */
208 int radeon_msi_ok(struct drm_device *rdev, unsigned long flags)
209 {
210         int family;
211
212         family = flags & RADEON_FAMILY_MASK;
213
214         /* RV370/RV380 was first asic with MSI support */
215         if (family < CHIP_RV380)
216                 return false;
217
218         /* MSIs don't work on AGP */
219         if (drm_pci_device_is_agp(rdev))
220                 return false;
221
222         /*
223          * Older chips have a HW limitation, they can only generate 40 bits
224          * of address for "64-bit" MSIs which breaks on some platforms, notably
225          * IBM POWER servers, so we limit them
226          */
227         if (family < CHIP_BONAIRE) {
228                 dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
229                 rdev->pdev->no_64bit_msi = 1;
230         }
231
232         /* force MSI on */
233         if (radeon_msi == 1)
234                 return true;
235         else if (radeon_msi == 0)
236                 return false;
237
238         /* Quirks */
239         /* HP RS690 only seems to work with MSIs. */
240         if ((rdev->pdev->device == 0x791f) &&
241             (rdev->pdev->subsystem_vendor == 0x103c) &&
242             (rdev->pdev->subsystem_device == 0x30c2))
243                 return true;
244
245         /* Dell RS690 only seems to work with MSIs. */
246         if ((rdev->pdev->device == 0x791f) &&
247             (rdev->pdev->subsystem_vendor == 0x1028) &&
248             (rdev->pdev->subsystem_device == 0x01fc))
249                 return true;
250
251         /* Dell RS690 only seems to work with MSIs. */
252         if ((rdev->pdev->device == 0x791f) &&
253             (rdev->pdev->subsystem_vendor == 0x1028) &&
254             (rdev->pdev->subsystem_device == 0x01fd))
255                 return true;
256
257         /* Gateway RS690 only seems to work with MSIs. */
258         if ((rdev->pdev->device == 0x791f) &&
259             (rdev->pdev->subsystem_vendor == 0x107b) &&
260             (rdev->pdev->subsystem_device == 0x0185))
261                 return true;
262
263         /* try and enable MSIs by default on all RS690s */
264         if (family == CHIP_RS690)
265                 return true;
266
267         /* RV515 seems to have MSI issues where it loses
268          * MSI rearms occasionally. This leads to lockups and freezes.
269          * disable it by default.
270          */
271         if (family == CHIP_RV515)
272                 return false;
273         if (flags & RADEON_IS_IGP) {
274                 /* APUs work fine with MSIs */
275                 if (family >= CHIP_PALM)
276                         return true;
277                 /* lots of IGPs have problems with MSIs */
278                 return false;
279         }
280
281         return true;
282 }
283
284 /**
285  * radeon_irq_kms_init - init driver interrupt info
286  *
287  * @rdev: radeon device pointer
288  *
289  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
290  * Returns 0 for success, error for failure.
291  */
292 int radeon_irq_kms_init(struct radeon_device *rdev)
293 {
294         int r = 0;
295
296         lockinit(&rdev->irq.lock, "drdil", 0, LK_CANRECURSE);
297         r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
298         if (r) {
299                 return r;
300         }
301
302         /* enable msi */
303         rdev->msi_enabled = (rdev->ddev->pdev->_irq_type == PCI_INTR_TYPE_MSI);
304
305 #ifndef __DragonFly__
306         if (radeon_msi_ok(rdev)) {
307                 int ret = pci_enable_msi(rdev->pdev);
308                 if (!ret) {
309                         rdev->msi_enabled = 1;
310                         dev_info(rdev->dev, "radeon: using MSI.\n");
311                 }
312         }
313 #endif
314
315         INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
316         INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
317         INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
318
319         rdev->irq.installed = true;
320         r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
321         if (r) {
322                 rdev->irq.installed = false;
323                 flush_delayed_work(&rdev->hotplug_work);
324                 return r;
325         }
326
327         DRM_INFO("radeon: irq initialized.\n");
328         return 0;
329 }
330
331 /**
332  * radeon_irq_kms_fini - tear down driver interrupt info
333  *
334  * @rdev: radeon device pointer
335  *
336  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
337  */
338 void radeon_irq_kms_fini(struct radeon_device *rdev)
339 {
340         drm_vblank_cleanup(rdev->ddev);
341         if (rdev->irq.installed) {
342                 drm_irq_uninstall(rdev->ddev);
343                 rdev->irq.installed = false;
344 #ifndef __DragonFly__
345                 if (rdev->msi_enabled)
346                         pci_disable_msi(rdev->pdev);
347 #endif
348                 flush_delayed_work(&rdev->hotplug_work);
349         }
350 }
351
352 /**
353  * radeon_irq_kms_sw_irq_get - enable software interrupt
354  *
355  * @rdev: radeon device pointer
356  * @ring: ring whose interrupt you want to enable
357  *
358  * Enables the software interrupt for a specific ring (all asics).
359  * The software interrupt is generally used to signal a fence on
360  * a particular ring.
361  */
362 void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
363 {
364         if (!rdev->ddev->irq_enabled)
365                 return;
366
367         if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
368                 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
369                 radeon_irq_set(rdev);
370                 lockmgr(&rdev->irq.lock, LK_RELEASE);
371         }
372 }
373
374 /**
375  * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
376  *
377  * @rdev: radeon device pointer
378  * @ring: ring whose interrupt you want to enable
379  *
380  * Enables the software interrupt for a specific ring (all asics).
381  * The software interrupt is generally used to signal a fence on
382  * a particular ring.
383  */
384 bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
385 {
386         return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
387 }
388
389 /**
390  * radeon_irq_kms_sw_irq_put - disable software interrupt
391  *
392  * @rdev: radeon device pointer
393  * @ring: ring whose interrupt you want to disable
394  *
395  * Disables the software interrupt for a specific ring (all asics).
396  * The software interrupt is generally used to signal a fence on
397  * a particular ring.
398  */
399 void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
400 {
401         if (!rdev->ddev->irq_enabled)
402                 return;
403
404         if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
405                 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
406                 radeon_irq_set(rdev);
407                 lockmgr(&rdev->irq.lock, LK_RELEASE);
408         }
409 }
410
411 /**
412  * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
413  *
414  * @rdev: radeon device pointer
415  * @crtc: crtc whose interrupt you want to enable
416  *
417  * Enables the pageflip interrupt for a specific crtc (all asics).
418  * For pageflips we use the vblank interrupt source.
419  */
420 void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
421 {
422         if (crtc < 0 || crtc >= rdev->num_crtc)
423                 return;
424
425         if (!rdev->ddev->irq_enabled)
426                 return;
427
428         if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
429                 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
430                 radeon_irq_set(rdev);
431                 lockmgr(&rdev->irq.lock, LK_RELEASE);
432         }
433 }
434
435 /**
436  * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
437  *
438  * @rdev: radeon device pointer
439  * @crtc: crtc whose interrupt you want to disable
440  *
441  * Disables the pageflip interrupt for a specific crtc (all asics).
442  * For pageflips we use the vblank interrupt source.
443  */
444 void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
445 {
446         if (crtc < 0 || crtc >= rdev->num_crtc)
447                 return;
448
449         if (!rdev->ddev->irq_enabled)
450                 return;
451
452         if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
453                 lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
454                 radeon_irq_set(rdev);
455                 lockmgr(&rdev->irq.lock, LK_RELEASE);
456         }
457 }
458
459 /**
460  * radeon_irq_kms_enable_afmt - enable audio format change interrupt
461  *
462  * @rdev: radeon device pointer
463  * @block: afmt block whose interrupt you want to enable
464  *
465  * Enables the afmt change interrupt for a specific afmt block (all asics).
466  */
467 void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
468 {
469         if (!rdev->ddev->irq_enabled)
470                 return;
471
472         lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
473         rdev->irq.afmt[block] = true;
474         radeon_irq_set(rdev);
475         lockmgr(&rdev->irq.lock, LK_RELEASE);
476 }
477
478 /**
479  * radeon_irq_kms_disable_afmt - disable audio format change interrupt
480  *
481  * @rdev: radeon device pointer
482  * @block: afmt block whose interrupt you want to disable
483  *
484  * Disables the afmt change interrupt for a specific afmt block (all asics).
485  */
486 void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
487 {
488         if (!rdev->ddev->irq_enabled)
489                 return;
490
491         lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
492         rdev->irq.afmt[block] = false;
493         radeon_irq_set(rdev);
494         lockmgr(&rdev->irq.lock, LK_RELEASE);
495 }
496
497 /**
498  * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
499  *
500  * @rdev: radeon device pointer
501  * @hpd_mask: mask of hpd pins you want to enable.
502  *
503  * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
504  */
505 void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
506 {
507         int i;
508
509         if (!rdev->ddev->irq_enabled)
510                 return;
511
512         lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
513         for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
514                 rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
515         radeon_irq_set(rdev);
516         lockmgr(&rdev->irq.lock, LK_RELEASE);
517 }
518
519 /**
520  * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
521  *
522  * @rdev: radeon device pointer
523  * @hpd_mask: mask of hpd pins you want to disable.
524  *
525  * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
526  */
527 void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
528 {
529         int i;
530
531         if (!rdev->ddev->irq_enabled)
532                 return;
533
534         lockmgr(&rdev->irq.lock, LK_EXCLUSIVE);
535         for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
536                 rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
537         radeon_irq_set(rdev);
538         lockmgr(&rdev->irq.lock, LK_RELEASE);
539 }
540