amdgpu_irq.c 15 KB

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