2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 * Authors: Dave Airlie
28 #include <linux/irq.h>
30 #include <drm/drm_crtc_helper.h>
31 #include <drm/amdgpu_drm.h>
33 #include "amdgpu_ih.h"
35 #include "amdgpu_connectors.h"
37 #include <linux/pm_runtime.h>
39 #define AMDGPU_WAIT_IDLE_TIMEOUT 200
42 * Handle hotplug events outside the interrupt handler proper.
45 * amdgpu_hotplug_work_func - display hotplug work handler
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.
55 static void amdgpu_hotplug_work_func(struct work_struct *work)
57 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
59 struct drm_device *dev = adev->ddev;
60 struct drm_mode_config *mode_config = &dev->mode_config;
61 struct drm_connector *connector;
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);
72 * amdgpu_irq_reset_work_func - execute gpu reset
76 * Execute scheduled gpu reset (cayman+).
77 * This function is called when the irq handler
78 * thinks we need a gpu reset.
80 static void amdgpu_irq_reset_work_func(struct work_struct *work)
82 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
85 amdgpu_gpu_reset(adev);
88 /* Disable *all* interrupts */
89 static void amdgpu_irq_disable_all(struct amdgpu_device *adev)
91 unsigned long irqflags;
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)
100 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
101 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
103 if (!src || !src->funcs->set || !src->num_types)
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);
111 DRM_ERROR("error disabling interrupt (%d)\n",
116 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
120 * amdgpu_irq_preinstall - drm irq preinstall callback
122 * @dev: drm dev pointer
124 * Gets the hw ready to enable irqs (all asics).
125 * This function disables all interrupt sources on the GPU.
127 void amdgpu_irq_preinstall(struct drm_device *dev)
129 struct amdgpu_device *adev = dev->dev_private;
131 /* Disable *all* interrupts */
132 amdgpu_irq_disable_all(adev);
134 amdgpu_ih_process(adev);
138 * amdgpu_irq_postinstall - drm irq preinstall callback
140 * @dev: drm dev pointer
142 * Handles stuff to be done after enabling irqs (all asics).
143 * Returns 0 on success.
145 int amdgpu_irq_postinstall(struct drm_device *dev)
147 dev->max_vblank_count = 0x00ffffff;
152 * amdgpu_irq_uninstall - drm irq uninstall callback
154 * @dev: drm dev pointer
156 * This function disables all interrupt sources on the GPU (all asics).
158 void amdgpu_irq_uninstall(struct drm_device *dev)
160 struct amdgpu_device *adev = dev->dev_private;
165 amdgpu_irq_disable_all(adev);
169 * amdgpu_irq_handler - irq handler
171 * @int irq, void *arg: args
173 * This is the irq handler for the amdgpu driver (all asics).
175 irqreturn_t amdgpu_irq_handler(int irq, void *arg)
177 struct drm_device *dev = (struct drm_device *) arg;
178 struct amdgpu_device *adev = dev->dev_private;
181 ret = amdgpu_ih_process(adev);
182 if (ret == IRQ_HANDLED)
183 pm_runtime_mark_last_busy(dev->dev);
188 * amdgpu_msi_ok - asic specific msi checks
190 * @adev: amdgpu device pointer
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.
197 static bool amdgpu_msi_ok(struct amdgpu_device *adev)
202 else if (amdgpu_msi == 0)
209 * amdgpu_irq_init - init driver interrupt info
211 * @adev: amdgpu device pointer
213 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
214 * Returns 0 for success, error for failure.
216 int amdgpu_irq_init(struct amdgpu_device *adev)
220 spin_lock_init(&adev->irq.lock);
221 r = drm_vblank_init(adev->ddev, adev->mode_info.num_crtc);
227 adev->irq.msi_enabled = false;
229 if (amdgpu_msi_ok(adev)) {
230 int ret = pci_enable_msi(adev->pdev);
232 adev->irq.msi_enabled = true;
233 dev_info(adev->dev, "amdgpu: using MSI.\n");
237 INIT_WORK(&adev->hotplug_work, amdgpu_hotplug_work_func);
238 INIT_WORK(&adev->reset_work, amdgpu_irq_reset_work_func);
240 adev->irq.installed = true;
241 r = drm_irq_install(adev->ddev, adev->ddev->pdev->irq);
243 adev->irq.installed = false;
244 flush_work(&adev->hotplug_work);
245 cancel_work_sync(&adev->reset_work);
249 DRM_INFO("amdgpu: irq initialized.\n");
254 * amdgpu_irq_fini - tear down driver interrupt info
256 * @adev: amdgpu device pointer
258 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
260 void amdgpu_irq_fini(struct amdgpu_device *adev)
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);
274 for (i = 0; i < AMDGPU_IH_CLIENTID_MAX; ++i) {
275 if (!adev->irq.client[i].sources)
278 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
279 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
284 kfree(src->enabled_types);
285 src->enabled_types = NULL;
289 adev->irq.client[i].sources[j] = NULL;
292 kfree(adev->irq.client[i].sources);
297 * amdgpu_irq_add_id - register irq source
299 * @adev: amdgpu device pointer
300 * @src_id: source id for this source
301 * @source: irq source
304 int amdgpu_irq_add_id(struct amdgpu_device *adev,
305 unsigned client_id, unsigned src_id,
306 struct amdgpu_irq_src *source)
308 if (client_id >= AMDGPU_IH_CLIENTID_MAX)
311 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
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),
321 if (!adev->irq.client[client_id].sources)
325 if (adev->irq.client[client_id].sources[src_id] != NULL)
328 if (source->num_types && !source->enabled_types) {
331 types = kcalloc(source->num_types, sizeof(atomic_t),
336 source->enabled_types = types;
339 adev->irq.client[client_id].sources[src_id] = source;
344 * amdgpu_irq_dispatch - dispatch irq to IP blocks
346 * @adev: amdgpu device pointer
347 * @entry: interrupt vector
349 * Dispatches the irq to the different IP blocks
351 void amdgpu_irq_dispatch(struct amdgpu_device *adev,
352 struct amdgpu_iv_entry *entry)
354 unsigned client_id = entry->client_id;
355 unsigned src_id = entry->src_id;
356 struct amdgpu_irq_src *src;
359 if (client_id >= AMDGPU_IH_CLIENTID_MAX) {
360 DRM_DEBUG("Invalid client_id in IV: %d\n", client_id);
364 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
365 DRM_DEBUG("Invalid src_id in IV: %d\n", src_id);
369 if (adev->irq.virq[src_id]) {
370 generic_handle_irq(irq_find_mapping(adev->irq.domain, src_id));
372 if (!adev->irq.client[client_id].sources) {
373 DRM_DEBUG("Unregistered interrupt client_id: %d src_id: %d\n",
378 src = adev->irq.client[client_id].sources[src_id];
380 DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id);
384 r = src->funcs->process(adev, src, entry);
386 DRM_ERROR("error processing interrupt (%d)\n", r);
391 * amdgpu_irq_update - update hw interrupt state
393 * @adev: amdgpu device pointer
394 * @src: interrupt src you want to enable
395 * @type: type of interrupt you want to update
397 * Updates the interrupt state for a specific src (all asics).
399 int amdgpu_irq_update(struct amdgpu_device *adev,
400 struct amdgpu_irq_src *src, unsigned type)
402 unsigned long irqflags;
403 enum amdgpu_interrupt_state state;
406 spin_lock_irqsave(&adev->irq.lock, irqflags);
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;
413 state = AMDGPU_IRQ_STATE_DISABLE;
415 r = src->funcs->set(adev, src, type, state);
416 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
420 void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev)
424 for (i = 0; i < AMDGPU_IH_CLIENTID_MAX; ++i) {
425 if (!adev->irq.client[i].sources)
428 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
429 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
433 for (k = 0; k < src->num_types; k++)
434 amdgpu_irq_update(adev, src, k);
440 * amdgpu_irq_get - enable interrupt
442 * @adev: amdgpu device pointer
443 * @src: interrupt src you want to enable
444 * @type: type of interrupt you want to enable
446 * Enables the interrupt type for a specific src (all asics).
448 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
451 if (!adev->ddev->irq_enabled)
454 if (type >= src->num_types)
457 if (!src->enabled_types || !src->funcs->set)
460 if (atomic_inc_return(&src->enabled_types[type]) == 1)
461 return amdgpu_irq_update(adev, src, type);
467 * amdgpu_irq_put - disable interrupt
469 * @adev: amdgpu device pointer
470 * @src: interrupt src you want to disable
471 * @type: type of interrupt you want to disable
473 * Disables the interrupt type for a specific src (all asics).
475 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
478 if (!adev->ddev->irq_enabled)
481 if (type >= src->num_types)
484 if (!src->enabled_types || !src->funcs->set)
487 if (atomic_dec_and_test(&src->enabled_types[type]))
488 return amdgpu_irq_update(adev, src, type);
494 * amdgpu_irq_enabled - test if irq is enabled or not
496 * @adev: amdgpu device pointer
497 * @idx: interrupt src you want to test
499 * Tests if the given interrupt source is enabled or not
501 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
504 if (!adev->ddev->irq_enabled)
507 if (type >= src->num_types)
510 if (!src->enabled_types || !src->funcs->set)
513 return !!atomic_read(&src->enabled_types[type]);
517 static void amdgpu_irq_mask(struct irq_data *irqd)
522 static void amdgpu_irq_unmask(struct irq_data *irqd)
527 static struct irq_chip amdgpu_irq_chip = {
529 .irq_mask = amdgpu_irq_mask,
530 .irq_unmask = amdgpu_irq_unmask,
533 static int amdgpu_irqdomain_map(struct irq_domain *d,
534 unsigned int irq, irq_hw_number_t hwirq)
536 if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID)
539 irq_set_chip_and_handler(irq,
540 &amdgpu_irq_chip, handle_simple_irq);
544 static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = {
545 .map = amdgpu_irqdomain_map,
549 * amdgpu_irq_add_domain - create a linear irq domain
551 * @adev: amdgpu device pointer
553 * Create an irq domain for GPU interrupt sources
554 * that may be driven by another driver (e.g., ACP).
556 int amdgpu_irq_add_domain(struct amdgpu_device *adev)
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");
569 * amdgpu_irq_remove_domain - remove the irq domain
571 * @adev: amdgpu device pointer
573 * Remove the irq domain for GPU interrupt sources
574 * that may be driven by another driver (e.g., ACP).
576 void amdgpu_irq_remove_domain(struct amdgpu_device *adev)
578 if (adev->irq.domain) {
579 irq_domain_remove(adev->irq.domain);
580 adev->irq.domain = NULL;
585 * amdgpu_irq_create_mapping - create a mapping between a domain irq and a
588 * @adev: amdgpu device pointer
589 * @src_id: IH source id
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.
596 unsigned amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned src_id)
598 adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id);
600 return adev->irq.virq[src_id];