]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_irq.c
drm/amdgpu: switch ih handling to two levels (v3)
[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 #include <linux/irq.h>
29 #include <drm/drmP.h>
30 #include <drm/drm_crtc_helper.h>
31 #include <drm/amdgpu_drm.h>
32 #include "amdgpu.h"
33 #include "amdgpu_ih.h"
34 #include "atom.h"
35 #include "amdgpu_connectors.h"
36
37 #include <linux/pm_runtime.h>
38
39 #define AMDGPU_WAIT_IDLE_TIMEOUT 200
40
41 /*
42  * Handle hotplug events outside the interrupt handler proper.
43  */
44 /**
45  * amdgpu_hotplug_work_func - display hotplug work handler
46  *
47  * @work: work struct
48  *
49  * This is the hot plug event work handler (all asics).
50  * The work gets scheduled from the irq handler if there
51  * was a hot plug interrupt.  It walks the connector table
52  * and calls the hotplug handler for each one, then sends
53  * a drm hotplug event to alert userspace.
54  */
55 static void amdgpu_hotplug_work_func(struct work_struct *work)
56 {
57         struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
58                                                   hotplug_work);
59         struct drm_device *dev = adev->ddev;
60         struct drm_mode_config *mode_config = &dev->mode_config;
61         struct drm_connector *connector;
62
63         mutex_lock(&mode_config->mutex);
64         list_for_each_entry(connector, &mode_config->connector_list, head)
65                 amdgpu_connector_hotplug(connector);
66         mutex_unlock(&mode_config->mutex);
67         /* Just fire off a uevent and let userspace tell us what to do */
68         drm_helper_hpd_irq_event(dev);
69 }
70
71 /**
72  * amdgpu_irq_reset_work_func - execute gpu reset
73  *
74  * @work: work struct
75  *
76  * Execute scheduled gpu reset (cayman+).
77  * This function is called when the irq handler
78  * thinks we need a gpu reset.
79  */
80 static void amdgpu_irq_reset_work_func(struct work_struct *work)
81 {
82         struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
83                                                   reset_work);
84
85         amdgpu_gpu_reset(adev);
86 }
87
88 /* Disable *all* interrupts */
89 static void amdgpu_irq_disable_all(struct amdgpu_device *adev)
90 {
91         unsigned long irqflags;
92         unsigned i, j, k;
93         int r;
94
95         spin_lock_irqsave(&adev->irq.lock, irqflags);
96         for (i = 0; i < AMDGPU_IH_CLIENTID_MAX; ++i) {
97                 if (!adev->irq.client[i].sources)
98                         continue;
99
100                 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
101                         struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
102
103                         if (!src || !src->funcs->set || !src->num_types)
104                                 continue;
105
106                         for (k = 0; k < src->num_types; ++k) {
107                                 atomic_set(&src->enabled_types[k], 0);
108                                 r = src->funcs->set(adev, src, k,
109                                                     AMDGPU_IRQ_STATE_DISABLE);
110                                 if (r)
111                                         DRM_ERROR("error disabling interrupt (%d)\n",
112                                                   r);
113                         }
114                 }
115         }
116         spin_unlock_irqrestore(&adev->irq.lock, irqflags);
117 }
118
119 /**
120  * amdgpu_irq_preinstall - drm irq preinstall callback
121  *
122  * @dev: drm dev pointer
123  *
124  * Gets the hw ready to enable irqs (all asics).
125  * This function disables all interrupt sources on the GPU.
126  */
127 void amdgpu_irq_preinstall(struct drm_device *dev)
128 {
129         struct amdgpu_device *adev = dev->dev_private;
130
131         /* Disable *all* interrupts */
132         amdgpu_irq_disable_all(adev);
133         /* Clear bits */
134         amdgpu_ih_process(adev);
135 }
136
137 /**
138  * amdgpu_irq_postinstall - drm irq preinstall callback
139  *
140  * @dev: drm dev pointer
141  *
142  * Handles stuff to be done after enabling irqs (all asics).
143  * Returns 0 on success.
144  */
145 int amdgpu_irq_postinstall(struct drm_device *dev)
146 {
147         dev->max_vblank_count = 0x00ffffff;
148         return 0;
149 }
150
151 /**
152  * amdgpu_irq_uninstall - drm irq uninstall callback
153  *
154  * @dev: drm dev pointer
155  *
156  * This function disables all interrupt sources on the GPU (all asics).
157  */
158 void amdgpu_irq_uninstall(struct drm_device *dev)
159 {
160         struct amdgpu_device *adev = dev->dev_private;
161
162         if (adev == NULL) {
163                 return;
164         }
165         amdgpu_irq_disable_all(adev);
166 }
167
168 /**
169  * amdgpu_irq_handler - irq handler
170  *
171  * @int irq, void *arg: args
172  *
173  * This is the irq handler for the amdgpu driver (all asics).
174  */
175 irqreturn_t amdgpu_irq_handler(int irq, void *arg)
176 {
177         struct drm_device *dev = (struct drm_device *) arg;
178         struct amdgpu_device *adev = dev->dev_private;
179         irqreturn_t ret;
180
181         ret = amdgpu_ih_process(adev);
182         if (ret == IRQ_HANDLED)
183                 pm_runtime_mark_last_busy(dev->dev);
184         return ret;
185 }
186
187 /**
188  * amdgpu_msi_ok - asic specific msi checks
189  *
190  * @adev: amdgpu device pointer
191  *
192  * Handles asic specific MSI checks to determine if
193  * MSIs should be enabled on a particular chip (all asics).
194  * Returns true if MSIs should be enabled, false if MSIs
195  * should not be enabled.
196  */
197 static bool amdgpu_msi_ok(struct amdgpu_device *adev)
198 {
199         /* force MSI on */
200         if (amdgpu_msi == 1)
201                 return true;
202         else if (amdgpu_msi == 0)
203                 return false;
204
205         return true;
206 }
207
208 /**
209  * amdgpu_irq_init - init driver interrupt info
210  *
211  * @adev: amdgpu device pointer
212  *
213  * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
214  * Returns 0 for success, error for failure.
215  */
216 int amdgpu_irq_init(struct amdgpu_device *adev)
217 {
218         int r = 0;
219
220         spin_lock_init(&adev->irq.lock);
221         r = drm_vblank_init(adev->ddev, adev->mode_info.num_crtc);
222         if (r) {
223                 return r;
224         }
225
226         /* enable msi */
227         adev->irq.msi_enabled = false;
228
229         if (amdgpu_msi_ok(adev)) {
230                 int ret = pci_enable_msi(adev->pdev);
231                 if (!ret) {
232                         adev->irq.msi_enabled = true;
233                         dev_info(adev->dev, "amdgpu: using MSI.\n");
234                 }
235         }
236
237         INIT_WORK(&adev->hotplug_work, amdgpu_hotplug_work_func);
238         INIT_WORK(&adev->reset_work, amdgpu_irq_reset_work_func);
239
240         adev->irq.installed = true;
241         r = drm_irq_install(adev->ddev, adev->ddev->pdev->irq);
242         if (r) {
243                 adev->irq.installed = false;
244                 flush_work(&adev->hotplug_work);
245                 cancel_work_sync(&adev->reset_work);
246                 return r;
247         }
248
249         DRM_INFO("amdgpu: irq initialized.\n");
250         return 0;
251 }
252
253 /**
254  * amdgpu_irq_fini - tear down driver interrupt info
255  *
256  * @adev: amdgpu device pointer
257  *
258  * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
259  */
260 void amdgpu_irq_fini(struct amdgpu_device *adev)
261 {
262         unsigned i, j;
263
264         drm_vblank_cleanup(adev->ddev);
265         if (adev->irq.installed) {
266                 drm_irq_uninstall(adev->ddev);
267                 adev->irq.installed = false;
268                 if (adev->irq.msi_enabled)
269                         pci_disable_msi(adev->pdev);
270                 flush_work(&adev->hotplug_work);
271                 cancel_work_sync(&adev->reset_work);
272         }
273
274         for (i = 0; i < AMDGPU_IH_CLIENTID_MAX; ++i) {
275                 if (!adev->irq.client[i].sources)
276                         continue;
277
278                 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
279                         struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
280
281                         if (!src)
282                                 continue;
283
284                         kfree(src->enabled_types);
285                         src->enabled_types = NULL;
286                         if (src->data) {
287                                 kfree(src->data);
288                                 kfree(src);
289                                 adev->irq.client[i].sources[j] = NULL;
290                         }
291                 }
292                 kfree(adev->irq.client[i].sources);
293         }
294 }
295
296 /**
297  * amdgpu_irq_add_id - register irq source
298  *
299  * @adev: amdgpu device pointer
300  * @src_id: source id for this source
301  * @source: irq source
302  *
303  */
304 int amdgpu_irq_add_id(struct amdgpu_device *adev,
305                       unsigned client_id, unsigned src_id,
306                       struct amdgpu_irq_src *source)
307 {
308         if (client_id >= AMDGPU_IH_CLIENTID_MAX)
309                 return -EINVAL;
310
311         if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
312                 return -EINVAL;
313
314         if (!source->funcs)
315                 return -EINVAL;
316
317         if (!adev->irq.client[client_id].sources) {
318                 adev->irq.client[client_id].sources = kcalloc(AMDGPU_MAX_IRQ_SRC_ID,
319                                                               sizeof(struct amdgpu_irq_src),
320                                                               GFP_KERNEL);
321                 if (!adev->irq.client[client_id].sources)
322                         return -ENOMEM;
323         }
324
325         if (adev->irq.client[client_id].sources[src_id] != NULL)
326                 return -EINVAL;
327
328         if (source->num_types && !source->enabled_types) {
329                 atomic_t *types;
330
331                 types = kcalloc(source->num_types, sizeof(atomic_t),
332                                 GFP_KERNEL);
333                 if (!types)
334                         return -ENOMEM;
335
336                 source->enabled_types = types;
337         }
338
339         adev->irq.client[client_id].sources[src_id] = source;
340         return 0;
341 }
342
343 /**
344  * amdgpu_irq_dispatch - dispatch irq to IP blocks
345  *
346  * @adev: amdgpu device pointer
347  * @entry: interrupt vector
348  *
349  * Dispatches the irq to the different IP blocks
350  */
351 void amdgpu_irq_dispatch(struct amdgpu_device *adev,
352                          struct amdgpu_iv_entry *entry)
353 {
354         unsigned client_id = entry->client_id;
355         unsigned src_id = entry->src_id;
356         struct amdgpu_irq_src *src;
357         int r;
358
359         if (client_id >= AMDGPU_IH_CLIENTID_MAX) {
360                 DRM_DEBUG("Invalid client_id in IV: %d\n", client_id);
361                 return;
362         }
363
364         if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
365                 DRM_DEBUG("Invalid src_id in IV: %d\n", src_id);
366                 return;
367         }
368
369         if (adev->irq.virq[src_id]) {
370                 generic_handle_irq(irq_find_mapping(adev->irq.domain, src_id));
371         } else {
372                 if (!adev->irq.client[client_id].sources) {
373                         DRM_DEBUG("Unregistered interrupt client_id: %d src_id: %d\n",
374                                   client_id, src_id);
375                         return;
376                 }
377
378                 src = adev->irq.client[client_id].sources[src_id];
379                 if (!src) {
380                         DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id);
381                         return;
382                 }
383
384                 r = src->funcs->process(adev, src, entry);
385                 if (r)
386                         DRM_ERROR("error processing interrupt (%d)\n", r);
387         }
388 }
389
390 /**
391  * amdgpu_irq_update - update hw interrupt state
392  *
393  * @adev: amdgpu device pointer
394  * @src: interrupt src you want to enable
395  * @type: type of interrupt you want to update
396  *
397  * Updates the interrupt state for a specific src (all asics).
398  */
399 int amdgpu_irq_update(struct amdgpu_device *adev,
400                              struct amdgpu_irq_src *src, unsigned type)
401 {
402         unsigned long irqflags;
403         enum amdgpu_interrupt_state state;
404         int r;
405
406         spin_lock_irqsave(&adev->irq.lock, irqflags);
407
408         /* we need to determine after taking the lock, otherwise
409            we might disable just enabled interrupts again */
410         if (amdgpu_irq_enabled(adev, src, type))
411                 state = AMDGPU_IRQ_STATE_ENABLE;
412         else
413                 state = AMDGPU_IRQ_STATE_DISABLE;
414
415         r = src->funcs->set(adev, src, type, state);
416         spin_unlock_irqrestore(&adev->irq.lock, irqflags);
417         return r;
418 }
419
420 void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev)
421 {
422         int i, j, k;
423
424         for (i = 0; i < AMDGPU_IH_CLIENTID_MAX; ++i) {
425                 if (!adev->irq.client[i].sources)
426                         continue;
427
428                 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
429                         struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
430
431                         if (!src)
432                                 continue;
433                         for (k = 0; k < src->num_types; k++)
434                                 amdgpu_irq_update(adev, src, k);
435                 }
436         }
437 }
438
439 /**
440  * amdgpu_irq_get - enable interrupt
441  *
442  * @adev: amdgpu device pointer
443  * @src: interrupt src you want to enable
444  * @type: type of interrupt you want to enable
445  *
446  * Enables the interrupt type for a specific src (all asics).
447  */
448 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
449                    unsigned type)
450 {
451         if (!adev->ddev->irq_enabled)
452                 return -ENOENT;
453
454         if (type >= src->num_types)
455                 return -EINVAL;
456
457         if (!src->enabled_types || !src->funcs->set)
458                 return -EINVAL;
459
460         if (atomic_inc_return(&src->enabled_types[type]) == 1)
461                 return amdgpu_irq_update(adev, src, type);
462
463         return 0;
464 }
465
466 /**
467  * amdgpu_irq_put - disable interrupt
468  *
469  * @adev: amdgpu device pointer
470  * @src: interrupt src you want to disable
471  * @type: type of interrupt you want to disable
472  *
473  * Disables the interrupt type for a specific src (all asics).
474  */
475 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
476                    unsigned type)
477 {
478         if (!adev->ddev->irq_enabled)
479                 return -ENOENT;
480
481         if (type >= src->num_types)
482                 return -EINVAL;
483
484         if (!src->enabled_types || !src->funcs->set)
485                 return -EINVAL;
486
487         if (atomic_dec_and_test(&src->enabled_types[type]))
488                 return amdgpu_irq_update(adev, src, type);
489
490         return 0;
491 }
492
493 /**
494  * amdgpu_irq_enabled - test if irq is enabled or not
495  *
496  * @adev: amdgpu device pointer
497  * @idx: interrupt src you want to test
498  *
499  * Tests if the given interrupt source is enabled or not
500  */
501 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
502                         unsigned type)
503 {
504         if (!adev->ddev->irq_enabled)
505                 return false;
506
507         if (type >= src->num_types)
508                 return false;
509
510         if (!src->enabled_types || !src->funcs->set)
511                 return false;
512
513         return !!atomic_read(&src->enabled_types[type]);
514 }
515
516 /* gen irq */
517 static void amdgpu_irq_mask(struct irq_data *irqd)
518 {
519         /* XXX */
520 }
521
522 static void amdgpu_irq_unmask(struct irq_data *irqd)
523 {
524         /* XXX */
525 }
526
527 static struct irq_chip amdgpu_irq_chip = {
528         .name = "amdgpu-ih",
529         .irq_mask = amdgpu_irq_mask,
530         .irq_unmask = amdgpu_irq_unmask,
531 };
532
533 static int amdgpu_irqdomain_map(struct irq_domain *d,
534                                 unsigned int irq, irq_hw_number_t hwirq)
535 {
536         if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID)
537                 return -EPERM;
538
539         irq_set_chip_and_handler(irq,
540                                  &amdgpu_irq_chip, handle_simple_irq);
541         return 0;
542 }
543
544 static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = {
545         .map = amdgpu_irqdomain_map,
546 };
547
548 /**
549  * amdgpu_irq_add_domain - create a linear irq domain
550  *
551  * @adev: amdgpu device pointer
552  *
553  * Create an irq domain for GPU interrupt sources
554  * that may be driven by another driver (e.g., ACP).
555  */
556 int amdgpu_irq_add_domain(struct amdgpu_device *adev)
557 {
558         adev->irq.domain = irq_domain_add_linear(NULL, AMDGPU_MAX_IRQ_SRC_ID,
559                                                  &amdgpu_hw_irqdomain_ops, adev);
560         if (!adev->irq.domain) {
561                 DRM_ERROR("GPU irq add domain failed\n");
562                 return -ENODEV;
563         }
564
565         return 0;
566 }
567
568 /**
569  * amdgpu_irq_remove_domain - remove the irq domain
570  *
571  * @adev: amdgpu device pointer
572  *
573  * Remove the irq domain for GPU interrupt sources
574  * that may be driven by another driver (e.g., ACP).
575  */
576 void amdgpu_irq_remove_domain(struct amdgpu_device *adev)
577 {
578         if (adev->irq.domain) {
579                 irq_domain_remove(adev->irq.domain);
580                 adev->irq.domain = NULL;
581         }
582 }
583
584 /**
585  * amdgpu_irq_create_mapping - create a mapping between a domain irq and a
586  *                             Linux irq
587  *
588  * @adev: amdgpu device pointer
589  * @src_id: IH source id
590  *
591  * Create a mapping between a domain irq (GPU IH src id) and a Linux irq
592  * Use this for components that generate a GPU interrupt, but are driven
593  * by a different driver (e.g., ACP).
594  * Returns the Linux irq.
595  */
596 unsigned amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned src_id)
597 {
598         adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id);
599
600         return adev->irq.virq[src_id];
601 }
This page took 0.06835 seconds and 4 git commands to generate.