amdgpu_irq.c 13 KB

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