amdgpu_irq.c 13 KB

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