amdgpu_irq.c 14 KB

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