radeon_irq_kms.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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/radeon_drm.h>
  31. #include "radeon_reg.h"
  32. #include "radeon.h"
  33. #include "atom.h"
  34. #include <linux/pm_runtime.h>
  35. #define RADEON_WAIT_IDLE_TIMEOUT 200
  36. /**
  37. * radeon_driver_irq_handler_kms - irq handler for KMS
  38. *
  39. * @int irq, void *arg: args
  40. *
  41. * This is the irq handler for the radeon KMS driver (all asics).
  42. * radeon_irq_process is a macro that points to the per-asic
  43. * irq handler callback.
  44. */
  45. irqreturn_t radeon_driver_irq_handler_kms(int irq, void *arg)
  46. {
  47. struct drm_device *dev = (struct drm_device *) arg;
  48. struct radeon_device *rdev = dev->dev_private;
  49. irqreturn_t ret;
  50. ret = radeon_irq_process(rdev);
  51. if (ret == IRQ_HANDLED)
  52. pm_runtime_mark_last_busy(dev->dev);
  53. return ret;
  54. }
  55. /*
  56. * Handle hotplug events outside the interrupt handler proper.
  57. */
  58. /**
  59. * radeon_hotplug_work_func - display hotplug work handler
  60. *
  61. * @work: work struct
  62. *
  63. * This is the hot plug event work handler (all asics).
  64. * The work gets scheduled from the irq handler if there
  65. * was a hot plug interrupt. It walks the connector table
  66. * and calls the hotplug handler for each one, then sends
  67. * a drm hotplug event to alert userspace.
  68. */
  69. static void radeon_hotplug_work_func(struct work_struct *work)
  70. {
  71. struct radeon_device *rdev = container_of(work, struct radeon_device,
  72. hotplug_work.work);
  73. struct drm_device *dev = rdev->ddev;
  74. struct drm_mode_config *mode_config = &dev->mode_config;
  75. struct drm_connector *connector;
  76. /* we can race here at startup, some boards seem to trigger
  77. * hotplug irqs when they shouldn't. */
  78. if (!rdev->mode_info.mode_config_initialized)
  79. return;
  80. mutex_lock(&mode_config->mutex);
  81. if (mode_config->num_connector) {
  82. list_for_each_entry(connector, &mode_config->connector_list, head)
  83. radeon_connector_hotplug(connector);
  84. }
  85. mutex_unlock(&mode_config->mutex);
  86. /* Just fire off a uevent and let userspace tell us what to do */
  87. drm_helper_hpd_irq_event(dev);
  88. }
  89. static void radeon_dp_work_func(struct work_struct *work)
  90. {
  91. struct radeon_device *rdev = container_of(work, struct radeon_device,
  92. dp_work);
  93. struct drm_device *dev = rdev->ddev;
  94. struct drm_mode_config *mode_config = &dev->mode_config;
  95. struct drm_connector *connector;
  96. /* this should take a mutex */
  97. if (mode_config->num_connector) {
  98. list_for_each_entry(connector, &mode_config->connector_list, head)
  99. radeon_connector_hotplug(connector);
  100. }
  101. }
  102. /**
  103. * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
  104. *
  105. * @dev: drm dev pointer
  106. *
  107. * Gets the hw ready to enable irqs (all asics).
  108. * This function disables all interrupt sources on the GPU.
  109. */
  110. void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
  111. {
  112. struct radeon_device *rdev = dev->dev_private;
  113. unsigned long irqflags;
  114. unsigned i;
  115. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  116. /* Disable *all* interrupts */
  117. for (i = 0; i < RADEON_NUM_RINGS; i++)
  118. atomic_set(&rdev->irq.ring_int[i], 0);
  119. rdev->irq.dpm_thermal = false;
  120. for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
  121. rdev->irq.hpd[i] = false;
  122. for (i = 0; i < RADEON_MAX_CRTCS; i++) {
  123. rdev->irq.crtc_vblank_int[i] = false;
  124. atomic_set(&rdev->irq.pflip[i], 0);
  125. rdev->irq.afmt[i] = false;
  126. }
  127. radeon_irq_set(rdev);
  128. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  129. /* Clear bits */
  130. radeon_irq_process(rdev);
  131. }
  132. /**
  133. * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
  134. *
  135. * @dev: drm dev pointer
  136. *
  137. * Handles stuff to be done after enabling irqs (all asics).
  138. * Returns 0 on success.
  139. */
  140. int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
  141. {
  142. struct radeon_device *rdev = dev->dev_private;
  143. if (ASIC_IS_AVIVO(rdev))
  144. dev->max_vblank_count = 0x00ffffff;
  145. else
  146. dev->max_vblank_count = 0x001fffff;
  147. return 0;
  148. }
  149. /**
  150. * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
  151. *
  152. * @dev: drm dev pointer
  153. *
  154. * This function disables all interrupt sources on the GPU (all asics).
  155. */
  156. void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
  157. {
  158. struct radeon_device *rdev = dev->dev_private;
  159. unsigned long irqflags;
  160. unsigned i;
  161. if (rdev == NULL) {
  162. return;
  163. }
  164. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  165. /* Disable *all* interrupts */
  166. for (i = 0; i < RADEON_NUM_RINGS; i++)
  167. atomic_set(&rdev->irq.ring_int[i], 0);
  168. rdev->irq.dpm_thermal = false;
  169. for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
  170. rdev->irq.hpd[i] = false;
  171. for (i = 0; i < RADEON_MAX_CRTCS; i++) {
  172. rdev->irq.crtc_vblank_int[i] = false;
  173. atomic_set(&rdev->irq.pflip[i], 0);
  174. rdev->irq.afmt[i] = false;
  175. }
  176. radeon_irq_set(rdev);
  177. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  178. }
  179. /**
  180. * radeon_msi_ok - asic specific msi checks
  181. *
  182. * @rdev: radeon device pointer
  183. *
  184. * Handles asic specific MSI checks to determine if
  185. * MSIs should be enabled on a particular chip (all asics).
  186. * Returns true if MSIs should be enabled, false if MSIs
  187. * should not be enabled.
  188. */
  189. static bool radeon_msi_ok(struct radeon_device *rdev)
  190. {
  191. /* RV370/RV380 was first asic with MSI support */
  192. if (rdev->family < CHIP_RV380)
  193. return false;
  194. /* MSIs don't work on AGP */
  195. if (rdev->flags & RADEON_IS_AGP)
  196. return false;
  197. /*
  198. * Older chips have a HW limitation, they can only generate 40 bits
  199. * of address for "64-bit" MSIs which breaks on some platforms, notably
  200. * IBM POWER servers, so we limit them
  201. */
  202. if (rdev->family < CHIP_BONAIRE) {
  203. dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
  204. rdev->pdev->no_64bit_msi = 1;
  205. }
  206. /* force MSI on */
  207. if (radeon_msi == 1)
  208. return true;
  209. else if (radeon_msi == 0)
  210. return false;
  211. /* Quirks */
  212. /* HP RS690 only seems to work with MSIs. */
  213. if ((rdev->pdev->device == 0x791f) &&
  214. (rdev->pdev->subsystem_vendor == 0x103c) &&
  215. (rdev->pdev->subsystem_device == 0x30c2))
  216. return true;
  217. /* Dell RS690 only seems to work with MSIs. */
  218. if ((rdev->pdev->device == 0x791f) &&
  219. (rdev->pdev->subsystem_vendor == 0x1028) &&
  220. (rdev->pdev->subsystem_device == 0x01fc))
  221. return true;
  222. /* Dell RS690 only seems to work with MSIs. */
  223. if ((rdev->pdev->device == 0x791f) &&
  224. (rdev->pdev->subsystem_vendor == 0x1028) &&
  225. (rdev->pdev->subsystem_device == 0x01fd))
  226. return true;
  227. /* Gateway RS690 only seems to work with MSIs. */
  228. if ((rdev->pdev->device == 0x791f) &&
  229. (rdev->pdev->subsystem_vendor == 0x107b) &&
  230. (rdev->pdev->subsystem_device == 0x0185))
  231. return true;
  232. /* try and enable MSIs by default on all RS690s */
  233. if (rdev->family == CHIP_RS690)
  234. return true;
  235. /* RV515 seems to have MSI issues where it loses
  236. * MSI rearms occasionally. This leads to lockups and freezes.
  237. * disable it by default.
  238. */
  239. if (rdev->family == CHIP_RV515)
  240. return false;
  241. if (rdev->flags & RADEON_IS_IGP) {
  242. /* APUs work fine with MSIs */
  243. if (rdev->family >= CHIP_PALM)
  244. return true;
  245. /* lots of IGPs have problems with MSIs */
  246. return false;
  247. }
  248. return true;
  249. }
  250. /**
  251. * radeon_irq_kms_init - init driver interrupt info
  252. *
  253. * @rdev: radeon device pointer
  254. *
  255. * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
  256. * Returns 0 for success, error for failure.
  257. */
  258. int radeon_irq_kms_init(struct radeon_device *rdev)
  259. {
  260. int r = 0;
  261. spin_lock_init(&rdev->irq.lock);
  262. r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
  263. if (r) {
  264. return r;
  265. }
  266. /* enable msi */
  267. rdev->msi_enabled = 0;
  268. if (radeon_msi_ok(rdev)) {
  269. int ret = pci_enable_msi(rdev->pdev);
  270. if (!ret) {
  271. rdev->msi_enabled = 1;
  272. dev_info(rdev->dev, "radeon: using MSI.\n");
  273. }
  274. }
  275. INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
  276. INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
  277. INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
  278. rdev->irq.installed = true;
  279. r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
  280. if (r) {
  281. rdev->irq.installed = false;
  282. flush_delayed_work(&rdev->hotplug_work);
  283. return r;
  284. }
  285. DRM_INFO("radeon: irq initialized.\n");
  286. return 0;
  287. }
  288. /**
  289. * radeon_irq_kms_fini - tear down driver interrupt info
  290. *
  291. * @rdev: radeon device pointer
  292. *
  293. * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
  294. */
  295. void radeon_irq_kms_fini(struct radeon_device *rdev)
  296. {
  297. drm_vblank_cleanup(rdev->ddev);
  298. if (rdev->irq.installed) {
  299. drm_irq_uninstall(rdev->ddev);
  300. rdev->irq.installed = false;
  301. if (rdev->msi_enabled)
  302. pci_disable_msi(rdev->pdev);
  303. flush_delayed_work(&rdev->hotplug_work);
  304. }
  305. }
  306. /**
  307. * radeon_irq_kms_sw_irq_get - enable software interrupt
  308. *
  309. * @rdev: radeon device pointer
  310. * @ring: ring whose interrupt you want to enable
  311. *
  312. * Enables the software interrupt for a specific ring (all asics).
  313. * The software interrupt is generally used to signal a fence on
  314. * a particular ring.
  315. */
  316. void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
  317. {
  318. unsigned long irqflags;
  319. if (!rdev->ddev->irq_enabled)
  320. return;
  321. if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
  322. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  323. radeon_irq_set(rdev);
  324. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  325. }
  326. }
  327. /**
  328. * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
  329. *
  330. * @rdev: radeon device pointer
  331. * @ring: ring whose interrupt you want to enable
  332. *
  333. * Enables the software interrupt for a specific ring (all asics).
  334. * The software interrupt is generally used to signal a fence on
  335. * a particular ring.
  336. */
  337. bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
  338. {
  339. return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
  340. }
  341. /**
  342. * radeon_irq_kms_sw_irq_put - disable software interrupt
  343. *
  344. * @rdev: radeon device pointer
  345. * @ring: ring whose interrupt you want to disable
  346. *
  347. * Disables the software interrupt for a specific ring (all asics).
  348. * The software interrupt is generally used to signal a fence on
  349. * a particular ring.
  350. */
  351. void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
  352. {
  353. unsigned long irqflags;
  354. if (!rdev->ddev->irq_enabled)
  355. return;
  356. if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
  357. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  358. radeon_irq_set(rdev);
  359. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  360. }
  361. }
  362. /**
  363. * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
  364. *
  365. * @rdev: radeon device pointer
  366. * @crtc: crtc whose interrupt you want to enable
  367. *
  368. * Enables the pageflip interrupt for a specific crtc (all asics).
  369. * For pageflips we use the vblank interrupt source.
  370. */
  371. void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
  372. {
  373. unsigned long irqflags;
  374. if (crtc < 0 || crtc >= rdev->num_crtc)
  375. return;
  376. if (!rdev->ddev->irq_enabled)
  377. return;
  378. if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
  379. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  380. radeon_irq_set(rdev);
  381. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  382. }
  383. }
  384. /**
  385. * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
  386. *
  387. * @rdev: radeon device pointer
  388. * @crtc: crtc whose interrupt you want to disable
  389. *
  390. * Disables the pageflip interrupt for a specific crtc (all asics).
  391. * For pageflips we use the vblank interrupt source.
  392. */
  393. void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
  394. {
  395. unsigned long irqflags;
  396. if (crtc < 0 || crtc >= rdev->num_crtc)
  397. return;
  398. if (!rdev->ddev->irq_enabled)
  399. return;
  400. if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
  401. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  402. radeon_irq_set(rdev);
  403. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  404. }
  405. }
  406. /**
  407. * radeon_irq_kms_enable_afmt - enable audio format change interrupt
  408. *
  409. * @rdev: radeon device pointer
  410. * @block: afmt block whose interrupt you want to enable
  411. *
  412. * Enables the afmt change interrupt for a specific afmt block (all asics).
  413. */
  414. void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
  415. {
  416. unsigned long irqflags;
  417. if (!rdev->ddev->irq_enabled)
  418. return;
  419. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  420. rdev->irq.afmt[block] = true;
  421. radeon_irq_set(rdev);
  422. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  423. }
  424. /**
  425. * radeon_irq_kms_disable_afmt - disable audio format change interrupt
  426. *
  427. * @rdev: radeon device pointer
  428. * @block: afmt block whose interrupt you want to disable
  429. *
  430. * Disables the afmt change interrupt for a specific afmt block (all asics).
  431. */
  432. void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
  433. {
  434. unsigned long irqflags;
  435. if (!rdev->ddev->irq_enabled)
  436. return;
  437. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  438. rdev->irq.afmt[block] = false;
  439. radeon_irq_set(rdev);
  440. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  441. }
  442. /**
  443. * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
  444. *
  445. * @rdev: radeon device pointer
  446. * @hpd_mask: mask of hpd pins you want to enable.
  447. *
  448. * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
  449. */
  450. void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
  451. {
  452. unsigned long irqflags;
  453. int i;
  454. if (!rdev->ddev->irq_enabled)
  455. return;
  456. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  457. for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
  458. rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
  459. radeon_irq_set(rdev);
  460. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  461. }
  462. /**
  463. * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
  464. *
  465. * @rdev: radeon device pointer
  466. * @hpd_mask: mask of hpd pins you want to disable.
  467. *
  468. * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
  469. */
  470. void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
  471. {
  472. unsigned long irqflags;
  473. int i;
  474. if (!rdev->ddev->irq_enabled)
  475. return;
  476. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  477. for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
  478. rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
  479. radeon_irq_set(rdev);
  480. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  481. }