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"
36 #include "amdgpu_trace.h"
38 #include <linux/pm_runtime.h>
40 #define AMDGPU_WAIT_IDLE_TIMEOUT 200
43 * Handle hotplug events outside the interrupt handler proper.
46 * amdgpu_hotplug_work_func - display hotplug work handler
50 * This is the hot plug event work handler (all asics).
51 * The work gets scheduled from the irq handler if there
52 * was a hot plug interrupt. It walks the connector table
53 * and calls the hotplug handler for each one, then sends
54 * a drm hotplug event to alert userspace.
56 static void amdgpu_hotplug_work_func(struct work_struct *work)
58 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
60 struct drm_device *dev = adev->ddev;
61 struct drm_mode_config *mode_config = &dev->mode_config;
62 struct drm_connector *connector;
64 mutex_lock(&mode_config->mutex);
65 list_for_each_entry(connector, &mode_config->connector_list, head)
66 amdgpu_connector_hotplug(connector);
67 mutex_unlock(&mode_config->mutex);
68 /* Just fire off a uevent and let userspace tell us what to do */
69 drm_helper_hpd_irq_event(dev);
73 * amdgpu_irq_reset_work_func - execute gpu reset
77 * Execute scheduled gpu reset (cayman+).
78 * This function is called when the irq handler
79 * thinks we need a gpu reset.
81 static void amdgpu_irq_reset_work_func(struct work_struct *work)
83 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
86 if (!amdgpu_sriov_vf(adev))
87 amdgpu_gpu_reset(adev);
90 /* Disable *all* interrupts */
91 static void amdgpu_irq_disable_all(struct amdgpu_device *adev)
93 unsigned long irqflags;
97 spin_lock_irqsave(&adev->irq.lock, irqflags);
98 for (i = 0; i < AMDGPU_IH_CLIENTID_MAX; ++i) {
99 if (!adev->irq.client[i].sources)
102 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
103 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
105 if (!src || !src->funcs->set || !src->num_types)
108 for (k = 0; k < src->num_types; ++k) {
109 atomic_set(&src->enabled_types[k], 0);
110 r = src->funcs->set(adev, src, k,
111 AMDGPU_IRQ_STATE_DISABLE);
113 DRM_ERROR("error disabling interrupt (%d)\n",
118 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
122 * amdgpu_irq_preinstall - drm irq preinstall callback
124 * @dev: drm dev pointer
126 * Gets the hw ready to enable irqs (all asics).
127 * This function disables all interrupt sources on the GPU.
129 void amdgpu_irq_preinstall(struct drm_device *dev)
131 struct amdgpu_device *adev = dev->dev_private;
133 /* Disable *all* interrupts */
134 amdgpu_irq_disable_all(adev);
136 amdgpu_ih_process(adev);
140 * amdgpu_irq_postinstall - drm irq preinstall callback
142 * @dev: drm dev pointer
144 * Handles stuff to be done after enabling irqs (all asics).
145 * Returns 0 on success.
147 int amdgpu_irq_postinstall(struct drm_device *dev)
149 dev->max_vblank_count = 0x00ffffff;
154 * amdgpu_irq_uninstall - drm irq uninstall callback
156 * @dev: drm dev pointer
158 * This function disables all interrupt sources on the GPU (all asics).
160 void amdgpu_irq_uninstall(struct drm_device *dev)
162 struct amdgpu_device *adev = dev->dev_private;
167 amdgpu_irq_disable_all(adev);
171 * amdgpu_irq_handler - irq handler
173 * @int irq, void *arg: args
175 * This is the irq handler for the amdgpu driver (all asics).
177 irqreturn_t amdgpu_irq_handler(int irq, void *arg)
179 struct drm_device *dev = (struct drm_device *) arg;
180 struct amdgpu_device *adev = dev->dev_private;
183 ret = amdgpu_ih_process(adev);
184 if (ret == IRQ_HANDLED)
185 pm_runtime_mark_last_busy(dev->dev);
190 * amdgpu_msi_ok - asic specific msi checks
192 * @adev: amdgpu device pointer
194 * Handles asic specific MSI checks to determine if
195 * MSIs should be enabled on a particular chip (all asics).
196 * Returns true if MSIs should be enabled, false if MSIs
197 * should not be enabled.
199 static bool amdgpu_msi_ok(struct amdgpu_device *adev)
204 else if (amdgpu_msi == 0)
211 * amdgpu_irq_init - init driver interrupt info
213 * @adev: amdgpu device pointer
215 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
216 * Returns 0 for success, error for failure.
218 int amdgpu_irq_init(struct amdgpu_device *adev)
222 spin_lock_init(&adev->irq.lock);
224 if (!adev->enable_virtual_display)
225 /* Disable vblank irqs aggressively for power-saving */
226 adev->ddev->vblank_disable_immediate = true;
228 r = drm_vblank_init(adev->ddev, adev->mode_info.num_crtc);
234 adev->irq.msi_enabled = false;
236 if (amdgpu_msi_ok(adev)) {
237 int ret = pci_enable_msi(adev->pdev);
239 adev->irq.msi_enabled = true;
240 dev_info(adev->dev, "amdgpu: using MSI.\n");
244 INIT_WORK(&adev->hotplug_work, amdgpu_hotplug_work_func);
245 INIT_WORK(&adev->reset_work, amdgpu_irq_reset_work_func);
247 adev->irq.installed = true;
248 r = drm_irq_install(adev->ddev, adev->ddev->pdev->irq);
250 adev->irq.installed = false;
251 flush_work(&adev->hotplug_work);
252 cancel_work_sync(&adev->reset_work);
256 DRM_INFO("amdgpu: irq initialized.\n");
261 * amdgpu_irq_fini - tear down driver interrupt info
263 * @adev: amdgpu device pointer
265 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
267 void amdgpu_irq_fini(struct amdgpu_device *adev)
271 if (adev->irq.installed) {
272 drm_irq_uninstall(adev->ddev);
273 adev->irq.installed = false;
274 if (adev->irq.msi_enabled)
275 pci_disable_msi(adev->pdev);
276 flush_work(&adev->hotplug_work);
277 cancel_work_sync(&adev->reset_work);
280 for (i = 0; i < AMDGPU_IH_CLIENTID_MAX; ++i) {
281 if (!adev->irq.client[i].sources)
284 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
285 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
290 kfree(src->enabled_types);
291 src->enabled_types = NULL;
295 adev->irq.client[i].sources[j] = NULL;
298 kfree(adev->irq.client[i].sources);
303 * amdgpu_irq_add_id - register irq source
305 * @adev: amdgpu device pointer
306 * @src_id: source id for this source
307 * @source: irq source
310 int amdgpu_irq_add_id(struct amdgpu_device *adev,
311 unsigned client_id, unsigned src_id,
312 struct amdgpu_irq_src *source)
314 if (client_id >= AMDGPU_IH_CLIENTID_MAX)
317 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
323 if (!adev->irq.client[client_id].sources) {
324 adev->irq.client[client_id].sources =
325 kcalloc(AMDGPU_MAX_IRQ_SRC_ID,
326 sizeof(struct amdgpu_irq_src *),
328 if (!adev->irq.client[client_id].sources)
332 if (adev->irq.client[client_id].sources[src_id] != NULL)
335 if (source->num_types && !source->enabled_types) {
338 types = kcalloc(source->num_types, sizeof(atomic_t),
343 source->enabled_types = types;
346 adev->irq.client[client_id].sources[src_id] = source;
351 * amdgpu_irq_dispatch - dispatch irq to IP blocks
353 * @adev: amdgpu device pointer
354 * @entry: interrupt vector
356 * Dispatches the irq to the different IP blocks
358 void amdgpu_irq_dispatch(struct amdgpu_device *adev,
359 struct amdgpu_iv_entry *entry)
361 unsigned client_id = entry->client_id;
362 unsigned src_id = entry->src_id;
363 struct amdgpu_irq_src *src;
366 trace_amdgpu_iv(entry);
368 if (client_id >= AMDGPU_IH_CLIENTID_MAX) {
369 DRM_DEBUG("Invalid client_id in IV: %d\n", client_id);
373 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
374 DRM_DEBUG("Invalid src_id in IV: %d\n", src_id);
378 if (adev->irq.virq[src_id]) {
379 generic_handle_irq(irq_find_mapping(adev->irq.domain, src_id));
381 if (!adev->irq.client[client_id].sources) {
382 DRM_DEBUG("Unregistered interrupt client_id: %d src_id: %d\n",
387 src = adev->irq.client[client_id].sources[src_id];
389 DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id);
393 r = src->funcs->process(adev, src, entry);
395 DRM_ERROR("error processing interrupt (%d)\n", r);
400 * amdgpu_irq_update - update hw interrupt state
402 * @adev: amdgpu device pointer
403 * @src: interrupt src you want to enable
404 * @type: type of interrupt you want to update
406 * Updates the interrupt state for a specific src (all asics).
408 int amdgpu_irq_update(struct amdgpu_device *adev,
409 struct amdgpu_irq_src *src, unsigned type)
411 unsigned long irqflags;
412 enum amdgpu_interrupt_state state;
415 spin_lock_irqsave(&adev->irq.lock, irqflags);
417 /* we need to determine after taking the lock, otherwise
418 we might disable just enabled interrupts again */
419 if (amdgpu_irq_enabled(adev, src, type))
420 state = AMDGPU_IRQ_STATE_ENABLE;
422 state = AMDGPU_IRQ_STATE_DISABLE;
424 r = src->funcs->set(adev, src, type, state);
425 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
429 void amdgpu_irq_gpu_reset_resume_helper(struct amdgpu_device *adev)
433 for (i = 0; i < AMDGPU_IH_CLIENTID_MAX; ++i) {
434 if (!adev->irq.client[i].sources)
437 for (j = 0; j < AMDGPU_MAX_IRQ_SRC_ID; ++j) {
438 struct amdgpu_irq_src *src = adev->irq.client[i].sources[j];
442 for (k = 0; k < src->num_types; k++)
443 amdgpu_irq_update(adev, src, k);
449 * amdgpu_irq_get - enable interrupt
451 * @adev: amdgpu device pointer
452 * @src: interrupt src you want to enable
453 * @type: type of interrupt you want to enable
455 * Enables the interrupt type for a specific src (all asics).
457 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
460 if (!adev->ddev->irq_enabled)
463 if (type >= src->num_types)
466 if (!src->enabled_types || !src->funcs->set)
469 if (atomic_inc_return(&src->enabled_types[type]) == 1)
470 return amdgpu_irq_update(adev, src, type);
476 * amdgpu_irq_put - disable interrupt
478 * @adev: amdgpu device pointer
479 * @src: interrupt src you want to disable
480 * @type: type of interrupt you want to disable
482 * Disables the interrupt type for a specific src (all asics).
484 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
487 if (!adev->ddev->irq_enabled)
490 if (type >= src->num_types)
493 if (!src->enabled_types || !src->funcs->set)
496 if (atomic_dec_and_test(&src->enabled_types[type]))
497 return amdgpu_irq_update(adev, src, type);
503 * amdgpu_irq_enabled - test if irq is enabled or not
505 * @adev: amdgpu device pointer
506 * @idx: interrupt src you want to test
508 * Tests if the given interrupt source is enabled or not
510 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
513 if (!adev->ddev->irq_enabled)
516 if (type >= src->num_types)
519 if (!src->enabled_types || !src->funcs->set)
522 return !!atomic_read(&src->enabled_types[type]);
526 static void amdgpu_irq_mask(struct irq_data *irqd)
531 static void amdgpu_irq_unmask(struct irq_data *irqd)
536 static struct irq_chip amdgpu_irq_chip = {
538 .irq_mask = amdgpu_irq_mask,
539 .irq_unmask = amdgpu_irq_unmask,
542 static int amdgpu_irqdomain_map(struct irq_domain *d,
543 unsigned int irq, irq_hw_number_t hwirq)
545 if (hwirq >= AMDGPU_MAX_IRQ_SRC_ID)
548 irq_set_chip_and_handler(irq,
549 &amdgpu_irq_chip, handle_simple_irq);
553 static const struct irq_domain_ops amdgpu_hw_irqdomain_ops = {
554 .map = amdgpu_irqdomain_map,
558 * amdgpu_irq_add_domain - create a linear irq domain
560 * @adev: amdgpu device pointer
562 * Create an irq domain for GPU interrupt sources
563 * that may be driven by another driver (e.g., ACP).
565 int amdgpu_irq_add_domain(struct amdgpu_device *adev)
567 adev->irq.domain = irq_domain_add_linear(NULL, AMDGPU_MAX_IRQ_SRC_ID,
568 &amdgpu_hw_irqdomain_ops, adev);
569 if (!adev->irq.domain) {
570 DRM_ERROR("GPU irq add domain failed\n");
578 * amdgpu_irq_remove_domain - remove the irq domain
580 * @adev: amdgpu device pointer
582 * Remove the irq domain for GPU interrupt sources
583 * that may be driven by another driver (e.g., ACP).
585 void amdgpu_irq_remove_domain(struct amdgpu_device *adev)
587 if (adev->irq.domain) {
588 irq_domain_remove(adev->irq.domain);
589 adev->irq.domain = NULL;
594 * amdgpu_irq_create_mapping - create a mapping between a domain irq and a
597 * @adev: amdgpu device pointer
598 * @src_id: IH source id
600 * Create a mapping between a domain irq (GPU IH src id) and a Linux irq
601 * Use this for components that generate a GPU interrupt, but are driven
602 * by a different driver (e.g., ACP).
603 * Returns the Linux irq.
605 unsigned amdgpu_irq_create_mapping(struct amdgpu_device *adev, unsigned src_id)
607 adev->irq.virq[src_id] = irq_create_mapping(adev->irq.domain, src_id);
609 return adev->irq.virq[src_id];