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
29 #include <drm/drm_crtc_helper.h>
30 #include <drm/amdgpu_drm.h>
32 #include "amdgpu_ih.h"
34 #include "amdgpu_connectors.h"
36 #include <linux/pm_runtime.h>
38 #define AMDGPU_WAIT_IDLE_TIMEOUT 200
41 * Handle hotplug events outside the interrupt handler proper.
44 * amdgpu_hotplug_work_func - display hotplug work handler
48 * This is the hot plug event work handler (all asics).
49 * The work gets scheduled from the irq handler if there
50 * was a hot plug interrupt. It walks the connector table
51 * and calls the hotplug handler for each one, then sends
52 * a drm hotplug event to alert userspace.
54 static void amdgpu_hotplug_work_func(struct work_struct *work)
56 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
58 struct drm_device *dev = adev->ddev;
59 struct drm_mode_config *mode_config = &dev->mode_config;
60 struct drm_connector *connector;
62 if (mode_config->num_connector) {
63 list_for_each_entry(connector, &mode_config->connector_list, head)
64 amdgpu_connector_hotplug(connector);
66 /* Just fire off a uevent and let userspace tell us what to do */
67 drm_helper_hpd_irq_event(dev);
71 * amdgpu_irq_reset_work_func - execute gpu reset
75 * Execute scheduled gpu reset (cayman+).
76 * This function is called when the irq handler
77 * thinks we need a gpu reset.
79 static void amdgpu_irq_reset_work_func(struct work_struct *work)
81 struct amdgpu_device *adev = container_of(work, struct amdgpu_device,
84 amdgpu_gpu_reset(adev);
87 /* Disable *all* interrupts */
88 static void amdgpu_irq_disable_all(struct amdgpu_device *adev)
90 unsigned long irqflags;
94 spin_lock_irqsave(&adev->irq.lock, irqflags);
95 for (i = 0; i < AMDGPU_MAX_IRQ_SRC_ID; ++i) {
96 struct amdgpu_irq_src *src = adev->irq.sources[i];
98 if (!src || !src->funcs->set || !src->num_types)
101 for (j = 0; j < src->num_types; ++j) {
102 atomic_set(&src->enabled_types[j], 0);
103 r = src->funcs->set(adev, src, j,
104 AMDGPU_IRQ_STATE_DISABLE);
106 DRM_ERROR("error disabling interrupt (%d)\n",
110 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
114 * amdgpu_irq_preinstall - drm irq preinstall callback
116 * @dev: drm dev pointer
118 * Gets the hw ready to enable irqs (all asics).
119 * This function disables all interrupt sources on the GPU.
121 void amdgpu_irq_preinstall(struct drm_device *dev)
123 struct amdgpu_device *adev = dev->dev_private;
125 /* Disable *all* interrupts */
126 amdgpu_irq_disable_all(adev);
128 amdgpu_ih_process(adev);
132 * amdgpu_irq_postinstall - drm irq preinstall callback
134 * @dev: drm dev pointer
136 * Handles stuff to be done after enabling irqs (all asics).
137 * Returns 0 on success.
139 int amdgpu_irq_postinstall(struct drm_device *dev)
141 dev->max_vblank_count = 0x001fffff;
146 * amdgpu_irq_uninstall - drm irq uninstall callback
148 * @dev: drm dev pointer
150 * This function disables all interrupt sources on the GPU (all asics).
152 void amdgpu_irq_uninstall(struct drm_device *dev)
154 struct amdgpu_device *adev = dev->dev_private;
159 amdgpu_irq_disable_all(adev);
163 * amdgpu_irq_handler - irq handler
165 * @int irq, void *arg: args
167 * This is the irq handler for the amdgpu driver (all asics).
169 irqreturn_t amdgpu_irq_handler(int irq, void *arg)
171 struct drm_device *dev = (struct drm_device *) arg;
172 struct amdgpu_device *adev = dev->dev_private;
175 ret = amdgpu_ih_process(adev);
176 if (ret == IRQ_HANDLED)
177 pm_runtime_mark_last_busy(dev->dev);
182 * amdgpu_msi_ok - asic specific msi checks
184 * @adev: amdgpu device pointer
186 * Handles asic specific MSI checks to determine if
187 * MSIs should be enabled on a particular chip (all asics).
188 * Returns true if MSIs should be enabled, false if MSIs
189 * should not be enabled.
191 static bool amdgpu_msi_ok(struct amdgpu_device *adev)
196 else if (amdgpu_msi == 0)
203 * amdgpu_irq_init - init driver interrupt info
205 * @adev: amdgpu device pointer
207 * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
208 * Returns 0 for success, error for failure.
210 int amdgpu_irq_init(struct amdgpu_device *adev)
214 spin_lock_init(&adev->irq.lock);
215 r = drm_vblank_init(adev->ddev, adev->mode_info.num_crtc);
220 adev->irq.msi_enabled = false;
222 if (amdgpu_msi_ok(adev)) {
223 int ret = pci_enable_msi(adev->pdev);
225 adev->irq.msi_enabled = true;
226 dev_info(adev->dev, "amdgpu: using MSI.\n");
230 INIT_WORK(&adev->hotplug_work, amdgpu_hotplug_work_func);
231 INIT_WORK(&adev->reset_work, amdgpu_irq_reset_work_func);
233 adev->irq.installed = true;
234 r = drm_irq_install(adev->ddev, adev->ddev->pdev->irq);
236 adev->irq.installed = false;
237 flush_work(&adev->hotplug_work);
241 DRM_INFO("amdgpu: irq initialized.\n");
246 * amdgpu_irq_fini - tear down driver interrupt info
248 * @adev: amdgpu device pointer
250 * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
252 void amdgpu_irq_fini(struct amdgpu_device *adev)
256 drm_vblank_cleanup(adev->ddev);
257 if (adev->irq.installed) {
258 drm_irq_uninstall(adev->ddev);
259 adev->irq.installed = false;
260 if (adev->irq.msi_enabled)
261 pci_disable_msi(adev->pdev);
262 flush_work(&adev->hotplug_work);
265 for (i = 0; i < AMDGPU_MAX_IRQ_SRC_ID; ++i) {
266 struct amdgpu_irq_src *src = adev->irq.sources[i];
271 kfree(src->enabled_types);
272 src->enabled_types = NULL;
277 * amdgpu_irq_add_id - register irq source
279 * @adev: amdgpu device pointer
280 * @src_id: source id for this source
281 * @source: irq source
284 int amdgpu_irq_add_id(struct amdgpu_device *adev, unsigned src_id,
285 struct amdgpu_irq_src *source)
287 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID)
290 if (adev->irq.sources[src_id] != NULL)
296 if (source->num_types && !source->enabled_types) {
299 types = kcalloc(source->num_types, sizeof(atomic_t),
304 source->enabled_types = types;
307 adev->irq.sources[src_id] = source;
312 * amdgpu_irq_dispatch - dispatch irq to IP blocks
314 * @adev: amdgpu device pointer
315 * @entry: interrupt vector
317 * Dispatches the irq to the different IP blocks
319 void amdgpu_irq_dispatch(struct amdgpu_device *adev,
320 struct amdgpu_iv_entry *entry)
322 unsigned src_id = entry->src_id;
323 struct amdgpu_irq_src *src;
326 if (src_id >= AMDGPU_MAX_IRQ_SRC_ID) {
327 DRM_DEBUG("Invalid src_id in IV: %d\n", src_id);
331 src = adev->irq.sources[src_id];
333 DRM_DEBUG("Unhandled interrupt src_id: %d\n", src_id);
337 r = src->funcs->process(adev, src, entry);
339 DRM_ERROR("error processing interrupt (%d)\n", r);
343 * amdgpu_irq_update - update hw interrupt state
345 * @adev: amdgpu device pointer
346 * @src: interrupt src you want to enable
347 * @type: type of interrupt you want to update
349 * Updates the interrupt state for a specific src (all asics).
351 int amdgpu_irq_update(struct amdgpu_device *adev,
352 struct amdgpu_irq_src *src, unsigned type)
354 unsigned long irqflags;
355 enum amdgpu_interrupt_state state;
358 spin_lock_irqsave(&adev->irq.lock, irqflags);
360 /* we need to determine after taking the lock, otherwise
361 we might disable just enabled interrupts again */
362 if (amdgpu_irq_enabled(adev, src, type))
363 state = AMDGPU_IRQ_STATE_ENABLE;
365 state = AMDGPU_IRQ_STATE_DISABLE;
367 r = src->funcs->set(adev, src, type, state);
368 spin_unlock_irqrestore(&adev->irq.lock, irqflags);
373 * amdgpu_irq_get - enable interrupt
375 * @adev: amdgpu device pointer
376 * @src: interrupt src you want to enable
377 * @type: type of interrupt you want to enable
379 * Enables the interrupt type for a specific src (all asics).
381 int amdgpu_irq_get(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
384 if (!adev->ddev->irq_enabled)
387 if (type >= src->num_types)
390 if (!src->enabled_types || !src->funcs->set)
393 if (atomic_inc_return(&src->enabled_types[type]) == 1)
394 return amdgpu_irq_update(adev, src, type);
399 bool amdgpu_irq_get_delayed(struct amdgpu_device *adev,
400 struct amdgpu_irq_src *src,
403 if ((type >= src->num_types) || !src->enabled_types)
405 return atomic_inc_return(&src->enabled_types[type]) == 1;
409 * amdgpu_irq_put - disable interrupt
411 * @adev: amdgpu device pointer
412 * @src: interrupt src you want to disable
413 * @type: type of interrupt you want to disable
415 * Disables the interrupt type for a specific src (all asics).
417 int amdgpu_irq_put(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
420 if (!adev->ddev->irq_enabled)
423 if (type >= src->num_types)
426 if (!src->enabled_types || !src->funcs->set)
429 if (atomic_dec_and_test(&src->enabled_types[type]))
430 return amdgpu_irq_update(adev, src, type);
436 * amdgpu_irq_enabled - test if irq is enabled or not
438 * @adev: amdgpu device pointer
439 * @idx: interrupt src you want to test
441 * Tests if the given interrupt source is enabled or not
443 bool amdgpu_irq_enabled(struct amdgpu_device *adev, struct amdgpu_irq_src *src,
446 if (!adev->ddev->irq_enabled)
449 if (type >= src->num_types)
452 if (!src->enabled_types || !src->funcs->set)
455 return !!atomic_read(&src->enabled_types[type]);