amdgpu_irq.c 13 KB

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