amdgpu_irq.c 15 KB

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