]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c
drm/amdgpu/vcn:Replace value with defined macro
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_irq.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
29 /**
30  * DOC: Interrupt Handling
31  *
32  * Interrupts generated within GPU hardware raise interrupt requests that are
33  * passed to amdgpu IRQ handler which is responsible for detecting source and
34  * type of the interrupt and dispatching matching handlers. If handling an
35  * interrupt requires calling kernel functions that may sleep processing is
36  * dispatched to work handlers.
37  *
38  * If MSI functionality is not disabled by module parameter then MSI
39  * support will be enabled.
40  *
41  * For GPU interrupt sources that may be driven by another driver, IRQ domain
42  * support is used (with mapping between virtual and hardware IRQs).
43  */
44
45 #include <linux/irq.h>
46 #include <drm/drmP.h>
47 #include <drm/drm_crtc_helper.h>
48 #include <drm/amdgpu_drm.h>
49 #include "amdgpu.h"
50 #include "amdgpu_ih.h"
51 #include "atom.h"
52 #include "amdgpu_connectors.h"
53 #include "amdgpu_trace.h"
54 #include "amdgpu_amdkfd.h"
55
56 #include <linux/pm_runtime.h>
57
58 #ifdef CONFIG_DRM_AMD_DC
59 #include "amdgpu_dm_irq.h"
60 #endif
61
62 #define AMDGPU_WAIT_IDLE_TIMEOUT 200
63
64 /**
65  * amdgpu_hotplug_work_func - work handler for display hotplug event
66  *
67  * @work: work struct pointer
68  *
69  * This is the hotplug event work handler (all ASICs).
70  * The work gets scheduled from the IRQ handler if there
71  * was a hotplug interrupt.  It walks through the connector table
72  * and calls hotplug handler for each connector. After this, it sends
73  * a DRM hotplug event to alert userspace.
74  *
75  * This design approach is required in order to defer hotplug event handling
76  * from the IRQ handler to a work handler because hotplug handler has to use
77  * mutexes which cannot be locked in an IRQ handler (since &mutex_lock may
78  * sleep).
79  */
80 static void amdgpu_hotplug_work_func(struct work_struct *work)
81 {
82         struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
83                                                   hotplug_work);
84         struct drm_device *dev = adev->ddev;
85         struct drm_mode_config *mode_config = &dev->mode_config;
86         struct drm_connector *connector;
87
88         mutex_lock(&mode_config->mutex);
89         list_for_each_entry(connector, &mode_config->connector_list, head)
90                 amdgpu_connector_hotplug(connector);
91         mutex_unlock(&mode_config->mutex);
92         /* Just fire off a uevent and let userspace tell us what to do */
93         drm_helper_hpd_irq_event(dev);
94 }
95
96 /**
97  * amdgpu_irq_reset_work_func - execute GPU reset
98  *
99  * @work: work struct pointer
100  *
101  * Execute scheduled GPU reset (Cayman+).
102  * This function is called when the IRQ handler thinks we need a GPU reset.
103  */
104 static void amdgpu_irq_reset_work_func(struct work_struct *work)
105 {
106         struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
107                                                   reset_work);
108
109         if (!amdgpu_sriov_vf(adev) && amdgpu_device_should_recover_gpu(adev))
110                 amdgpu_device_gpu_recover(adev, NULL);
111 }
112
113 /**
114  * amdgpu_irq_disable_all - disable *all* interrupts
115  *
116  * @adev: amdgpu device pointer
117  *
118  * Disable all types of interrupts from all sources.
119  */
120 void amdgpu_irq_disable_all(struct amdgpu_device *adev)
121 {
122         unsigned long irqflags;
123         unsigned i, j, k;
124         int r;
125
126         spin_lock_irqsave(&adev->irq.lock, irqflags);
127         for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
128                 if (!adev->irq.client[i].sources)
129                         continue;
130
131                 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
132                         struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
133
134                         if (!src || !src->funcs->set || !src->num_types)
135                                 continue;
136
137                         for (k = 0; k < src->num_types; ++k) {
138                                 atomic_set(&src->enabled_types[k], 0);
139                                 r = src->funcs->set(adev, src, k,
140                                                     AMDGPU_IRQ_STATE_DISABLE);
141                                 if (r)
142                                         DRM_ERROR("error disabling interrupt (%d)\n",
143                                                   r);
144                         }
145                 }
146         }
147         spin_unlock_irqrestore(&adev->irq.lock, irqflags);
148 }
149
150 /**
151  * amdgpu_irq_callback - callback from the IH ring
152  *
153  * @adev: amdgpu device pointer
154  * @ih: amdgpu ih ring
155  *
156  * Callback from IH ring processing to handle the entry at the current position
157  * and advance the read pointer.
158  */
159 static void amdgpu_irq_callback(struct amdgpu_device *adev,
160                                 struct amdgpu_ih_ring *ih)
161 {
162         u32 ring_index = ih->rptr >> 2;
163         struct amdgpu_iv_entry entry;
164
165         /* Prescreening of high-frequency interrupts */
166         if (!amdgpu_ih_prescreen_iv(adev))
167                 return;
168
169         /* Before dispatching irq to IP blocks, send it to amdkfd */
170         amdgpu_amdkfd_interrupt(adev, (const void *) &ih->ring[ring_index]);
171
172         entry.iv_entry = (const uint32_t *)&ih->ring[ring_index];
173         amdgpu_ih_decode_iv(adev, &entry);
174
175         amdgpu_irq_dispatch(adev, &entry);
176 }
177
178 /**
179  * amdgpu_irq_handler - IRQ handler
180  *
181  * @irq: IRQ number (unused)
182  * @arg: pointer to DRM device
183  *
184  * IRQ handler for amdgpu driver (all ASICs).
185  *
186  * Returns:
187  * result of handling the IRQ, as defined by &irqreturn_t
188  */
189 irqreturn_t amdgpu_irq_handler(int irq, void *arg)
190 {
191         struct drm_device *dev = (struct drm_device *) arg;
192         struct amdgpu_device *adev = dev->dev_private;
193         irqreturn_t ret;
194
195         ret = amdgpu_ih_process(adev, &adev->irq.ih, amdgpu_irq_callback);
196         if (ret == IRQ_HANDLED)
197                 pm_runtime_mark_last_busy(dev->dev);
198         return ret;
199 }
200
201 /**
202  * amdgpu_msi_ok - check whether MSI functionality is enabled
203  *
204  * @adev: amdgpu device pointer (unused)
205  *
206  * Checks whether MSI functionality has been disabled via module parameter
207  * (all ASICs).
208  *
209  * Returns:
210  * *true* if MSIs are allowed to be enabled or *false* otherwise
211  */
212 static bool amdgpu_msi_ok(struct amdgpu_device *adev)
213 {
214         if (amdgpu_msi == 1)
215                 return true;
216         else if (amdgpu_msi == 0)
217                 return false;
218
219         return true;
220 }
221
222 /**
223  * amdgpu_irq_init - initialize interrupt handling
224  *
225  * @adev: amdgpu device pointer
226  *
227  * Sets up work functions for hotplug and reset interrupts, enables MSI
228  * functionality, initializes vblank, hotplug and reset interrupt handling.
229  *
230  * Returns:
231  * 0 on success or error code on failure
232  */
233 int amdgpu_irq_init(struct amdgpu_device *adev)
234 {
235         int r = 0;
236
237         spin_lock_init(&adev->irq.lock);
238
239         /* Enable MSI if not disabled by module parameter */
240         adev->irq.msi_enabled = false;
241
242         if (amdgpu_msi_ok(adev)) {
243                 int ret = pci_enable_msi(adev->pdev);
244                 if (!ret) {
245                         adev->irq.msi_enabled = true;
246                         dev_dbg(adev->dev, "amdgpu: using MSI.\n");
247                 }
248         }
249
250         if (!amdgpu_device_has_dc_support(adev)) {
251                 if (!adev->enable_virtual_display)
252                         /* Disable vblank IRQs aggressively for power-saving */
253                         /* XXX: can this be enabled for DC? */
254                         adev->ddev->vblank_disable_immediate = true;
255
256                 r = drm_vblank_init(adev->ddev, adev->mode_info.num_crtc);
257                 if (r)
258                         return r;
259
260                 /* Pre-DCE11 */
261                 INIT_WORK(&adev->hotplug_work,
262                                 amdgpu_hotplug_work_func);
263         }
264
265         INIT_WORK(&adev->reset_work, amdgpu_irq_reset_work_func);
266
267         adev->irq.installed = true;
268         r = drm_irq_install(adev->ddev, adev->ddev->pdev->irq);
269         if (r) {
270                 adev->irq.installed = false;
271                 if (!amdgpu_device_has_dc_support(adev))
272                         flush_work(&adev->hotplug_work);
273                 cancel_work_sync(&adev->reset_work);
274                 return r;
275         }
276         adev->ddev->max_vblank_count = 0x00ffffff;
277
278         DRM_DEBUG("amdgpu: irq initialized.\n");
279         return 0;
280 }
281
282 /**
283  * amdgpu_irq_fini - shut down interrupt handling
284  *
285  * @adev: amdgpu device pointer
286  *
287  * Tears down work functions for hotplug and reset interrupts, disables MSI
288  * functionality, shuts down vblank, hotplug and reset interrupt handling,
289  * turns off interrupts from all sources (all ASICs).
290  */
291 void amdgpu_irq_fini(struct amdgpu_device *adev)
292 {
293         unsigned i, j;
294
295         if (adev->irq.installed) {
296                 drm_irq_uninstall(adev->ddev);
297                 adev->irq.installed = false;
298                 if (adev->irq.msi_enabled)
299                         pci_disable_msi(adev->pdev);
300                 if (!amdgpu_device_has_dc_support(adev))
301                         flush_work(&adev->hotplug_work);
302                 cancel_work_sync(&adev->reset_work);
303         }
304
305         for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
306                 if (!adev->irq.client[i].sources)
307                         continue;
308
309                 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
310                         struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
311
312                         if (!src)
313                                 continue;
314
315                         kfree(src->enabled_types);
316                         src->enabled_types = NULL;
317                         if (src->data) {
318                                 kfree(src->data);
319                                 kfree(src);
320                                 adev->irq.client[i].sources[j] = NULL;
321                         }
322                 }
323                 kfree(adev->irq.client[i].sources);
324                 adev->irq.client[i].sources = NULL;
325         }
326 }
327
328 /**
329  * amdgpu_irq_add_id - register IRQ source
330  *
331  * @adev: amdgpu device pointer
332  * @client_id: client id
333  * @src_id: source id
334  * @source: IRQ source pointer
335  *
336  * Registers IRQ source on a client.
337  *
338  * Returns:
339  * 0 on success or error code otherwise
340  */
341 int amdgpu_irq_add_id(struct amdgpu_device *adev,
342                       unsigned client_id, unsigned src_id,
343                       struct amdgpu_irq_src *source)
344 {
345         if (client_id >= AMDGPU_IRQ_CLIENTID_MAX)
346                 return -EINVAL;
347
348         if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
349                 return -EINVAL;
350
351         if (!source->funcs)
352                 return -EINVAL;
353
354         if (!adev->irq.client[client_id].sources) {
355                 adev->irq.client[client_id].sources =
356                         kcalloc(AMDGPU_MAX_IRQ_SRC_ID,
357                                 sizeof(struct amdgpu_irq_src *),
358                                 GFP_KERNEL);
359                 if (!adev->irq.client[client_id].sources)
360                         return -ENOMEM;
361         }
362
363         if (adev->irq.client[client_id].sources[src_id] != NULL)
364                 return -EINVAL;
365
366         if (source->num_types && !source->enabled_types) {
367                 atomic_t *types;
368
369                 types = kcalloc(source->num_types, sizeof(atomic_t),
370                                 GFP_KERNEL);
371                 if (!types)
372                         return -ENOMEM;
373
374                 source->enabled_types = types;
375         }
376
377         adev->irq.client[client_id].sources[src_id] = source;
378         return 0;
379 }
380
381 /**
382  * amdgpu_irq_dispatch - dispatch IRQ to IP blocks
383  *
384  * @adev: amdgpu device pointer
385  * @entry: interrupt vector pointer
386  *
387  * Dispatches IRQ to IP blocks.
388  */
389 void amdgpu_irq_dispatch(struct amdgpu_device *adev,
390                          struct amdgpu_iv_entry *entry)
391 {
392         unsigned client_id = entry->client_id;
393         unsigned src_id = entry->src_id;
394         struct amdgpu_irq_src *src;
395         int r;
396
397         trace_amdgpu_iv(entry);
398
399         if (client_id >= AMDGPU_IRQ_CLIENTID_MAX) {
400                 DRM_DEBUG("Invalid client_id in IV: %d\n", client_id);
401                 return;
402         }
403
404         if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
405                 DRM_DEBUG("Invalid src_id in IV: %d\n", src_id);
406                 return;
407         }
408
409         if (adev->irq.virq[src_id]) {
410                 generic_handle_irq(irq_find_mapping(adev->irq.domain, src_id));
411         } else {
412                 if (!adev->irq.client[client_id].sources) {
413                         DRM_DEBUG("Unregistered interrupt client_id: %d src_id: %d\n",
414                                   client_id, src_id);
415                         return;
416                 }
417
418                 src = adev->irq.client[client_id].sources[src_id];
419                 if (!src) {
420                         DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id);
421                         return;
422                 }
423
424                 r = src->funcs->process(adev, src, entry);
425                 if (r)
426                         DRM_ERROR("error processing interrupt (%d)\n", r);
427         }
428 }
429
430 /**
431  * amdgpu_irq_update - update hardware interrupt state
432  *
433  * @adev: amdgpu device pointer
434  * @src: interrupt source pointer
435  * @type: type of interrupt
436  *
437  * Updates interrupt state for the specific source (all ASICs).
438  */
439 int amdgpu_irq_update(struct amdgpu_device *adev,
440                              struct amdgpu_irq_src *src, unsigned type)
441 {
442         unsigned long irqflags;
443         enum amdgpu_interrupt_state state;
444         int r;
445
446         spin_lock_irqsave(&adev->irq.lock, irqflags);
447
448         /* We need to determine after taking the lock, otherwise
449            we might disable just enabled interrupts again */
450         if (amdgpu_irq_enabled(adev, src, type))
451                 state = AMDGPU_IRQ_STATE_ENABLE;
452         else
453                 state = AMDGPU_IRQ_STATE_DISABLE;
454
455         r = src->funcs->set(adev, src, type, state);
456         spin_unlock_irqrestore(&adev->irq.lock, irqflags);
457         return r;
458 }
459
460 /**
461  * amdgpu_irq_gpu_reset_resume_helper - update interrupt states on all sources
462  *
463  * @adev: amdgpu device pointer
464  *
465  * Updates state of all types of interrupts on all sources on resume after
466  * reset.
467  */
468 void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev)
469 {
470         int i, j, k;
471
472         for (i = 0; i < AMDGPU_IRQ_CLIENTID_MAX; ++i) {
473                 if (!adev->irq.client[i].sources)
474                         continue;
475
476                 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
477                         struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
478
479                         if (!src)
480                                 continue;
481                         for (k = 0; k < src->num_types; k++)
482                                 amdgpu_irq_update(adev, src, k);
483                 }
484         }
485 }
486
487 /**
488  * amdgpu_irq_get - enable interrupt
489  *
490  * @adev: amdgpu device pointer
491  * @src: interrupt source pointer
492  * @type: type of interrupt
493  *
494  * Enables specified type of interrupt on the specified source (all ASICs).
495  *
496  * Returns:
497  * 0 on success or error code otherwise
498  */
499 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
500                    unsigned type)
501 {
502         if (!adev->ddev->irq_enabled)
503                 return -ENOENT;
504
505         if (type >= src->num_types)
506                 return -EINVAL;
507
508         if (!src->enabled_types || !src->funcs->set)
509                 return -EINVAL;
510
511         if (atomic_inc_return(&src->enabled_types[type]) == 1)
512                 return amdgpu_irq_update(adev, src, type);
513
514         return 0;
515 }
516
517 /**
518  * amdgpu_irq_put - disable interrupt
519  *
520  * @adev: amdgpu device pointer
521  * @src: interrupt source pointer
522  * @type: type of interrupt
523  *
524  * Enables specified type of interrupt on the specified source (all ASICs).
525  *
526  * Returns:
527  * 0 on success or error code otherwise
528  */
529 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
530                    unsigned type)
531 {
532         if (!adev->ddev->irq_enabled)
533                 return -ENOENT;
534
535         if (type >= src->num_types)
536                 return -EINVAL;
537
538         if (!src->enabled_types || !src->funcs->set)
539                 return -EINVAL;
540
541         if (atomic_dec_and_test(&src->enabled_types[type]))
542                 return amdgpu_irq_update(adev, src, type);
543
544         return 0;
545 }
546
547 /**
548  * amdgpu_irq_enabled - check whether interrupt is enabled or not
549  *
550  * @adev: amdgpu device pointer
551  * @src: interrupt source pointer
552  * @type: type of interrupt
553  *
554  * Checks whether the given type of interrupt is enabled on the given source.
555  *
556  * Returns:
557  * *true* if interrupt is enabled, *false* if interrupt is disabled or on
558  * invalid parameters
559  */
560 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
561                         unsigned type)
562 {
563         if (!adev->ddev->irq_enabled)
564                 return false;
565
566         if (type >= src->num_types)
567                 return false;
568
569         if (!src->enabled_types || !src->funcs->set)
570                 return false;
571
572         return !!atomic_read(&src->enabled_types[type]);
573 }
574
575 /* XXX: Generic IRQ handling */
576 static void amdgpu_irq_mask(struct irq_data *irqd)
577 {
578         /* XXX */
579 }
580
581 static void amdgpu_irq_unmask(struct irq_data *irqd)
582 {
583         /* XXX */
584 }
585
586 /* amdgpu hardware interrupt chip descriptor */
587 static struct irq_chip amdgpu_irq_chip = {
588         .name = "amdgpu-ih",
589         .irq_mask = amdgpu_irq_mask,
590         .irq_unmask = amdgpu_irq_unmask,
591 };
592
593 /**
594  * amdgpu_irqdomain_map - create mapping between virtual and hardware IRQ numbers
595  *
596  * @d: amdgpu IRQ domain pointer (unused)
597  * @irq: virtual IRQ number
598  * @hwirq: hardware irq number
599  *
600  * Current implementation assigns simple interrupt handler to the given virtual
601  * IRQ.
602  *
603  * Returns:
604  * 0 on success or error code otherwise
605  */
606 static int amdgpu_irqdomain_map(struct irq_domain *d,
607                                 unsigned int irq, irq_hw_number_t hwirq)
608 {
609         if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID)
610                 return -EPERM;
611
612         irq_set_chip_and_handler(irq,
613                                  &amdgpu_irq_chip, handle_simple_irq);
614         return 0;
615 }
616
617 /* Implementation of methods for amdgpu IRQ domain */
618 static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = {
619         .map = amdgpu_irqdomain_map,
620 };
621
622 /**
623  * amdgpu_irq_add_domain - create a linear IRQ domain
624  *
625  * @adev: amdgpu device pointer
626  *
627  * Creates an IRQ domain for GPU interrupt sources
628  * that may be driven by another driver (e.g., ACP).
629  *
630  * Returns:
631  * 0 on success or error code otherwise
632  */
633 int amdgpu_irq_add_domain(struct amdgpu_device *adev)
634 {
635         adev->irq.domain = irq_domain_add_linear(NULL, AMDGPU_MAX_IRQ_SRC_ID,
636                                                  &amdgpu_hw_irqdomain_ops, adev);
637         if (!adev->irq.domain) {
638                 DRM_ERROR("GPU irq add domain failed\n");
639                 return -ENODEV;
640         }
641
642         return 0;
643 }
644
645 /**
646  * amdgpu_irq_remove_domain - remove the IRQ domain
647  *
648  * @adev: amdgpu device pointer
649  *
650  * Removes the IRQ domain for GPU interrupt sources
651  * that may be driven by another driver (e.g., ACP).
652  */
653 void amdgpu_irq_remove_domain(struct amdgpu_device *adev)
654 {
655         if (adev->irq.domain) {
656                 irq_domain_remove(adev->irq.domain);
657                 adev->irq.domain = NULL;
658         }
659 }
660
661 /**
662  * amdgpu_irq_create_mapping - create mapping between domain Linux IRQs
663  *
664  * @adev: amdgpu device pointer
665  * @src_id: IH source id
666  *
667  * Creates mapping between a domain IRQ (GPU IH src id) and a Linux IRQ
668  * Use this for components that generate a GPU interrupt, but are driven
669  * by a different driver (e.g., ACP).
670  *
671  * Returns:
672  * Linux IRQ
673  */
674 unsigned amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned src_id)
675 {
676         adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id);
677
678         return adev->irq.virq[src_id];
679 }