radeon_irq_kms.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. list_for_each_entry(connector, &mode_config->connector_list, head)
  82. radeon_connector_hotplug(connector);
  83. mutex_unlock(&mode_config->mutex);
  84. /* Just fire off a uevent and let userspace tell us what to do */
  85. drm_helper_hpd_irq_event(dev);
  86. }
  87. static void radeon_dp_work_func(struct work_struct *work)
  88. {
  89. struct radeon_device *rdev = container_of(work, struct radeon_device,
  90. dp_work);
  91. struct drm_device *dev = rdev->ddev;
  92. struct drm_mode_config *mode_config = &dev->mode_config;
  93. struct drm_connector *connector;
  94. /* this should take a mutex */
  95. list_for_each_entry(connector, &mode_config->connector_list, head)
  96. radeon_connector_hotplug(connector);
  97. }
  98. /**
  99. * radeon_driver_irq_preinstall_kms - drm irq preinstall callback
  100. *
  101. * @dev: drm dev pointer
  102. *
  103. * Gets the hw ready to enable irqs (all asics).
  104. * This function disables all interrupt sources on the GPU.
  105. */
  106. void radeon_driver_irq_preinstall_kms(struct drm_device *dev)
  107. {
  108. struct radeon_device *rdev = dev->dev_private;
  109. unsigned long irqflags;
  110. unsigned i;
  111. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  112. /* Disable *all* interrupts */
  113. for (i = 0; i < RADEON_NUM_RINGS; i++)
  114. atomic_set(&rdev->irq.ring_int[i], 0);
  115. rdev->irq.dpm_thermal = false;
  116. for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
  117. rdev->irq.hpd[i] = false;
  118. for (i = 0; i < RADEON_MAX_CRTCS; i++) {
  119. rdev->irq.crtc_vblank_int[i] = false;
  120. atomic_set(&rdev->irq.pflip[i], 0);
  121. rdev->irq.afmt[i] = false;
  122. }
  123. radeon_irq_set(rdev);
  124. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  125. /* Clear bits */
  126. radeon_irq_process(rdev);
  127. }
  128. /**
  129. * radeon_driver_irq_postinstall_kms - drm irq preinstall callback
  130. *
  131. * @dev: drm dev pointer
  132. *
  133. * Handles stuff to be done after enabling irqs (all asics).
  134. * Returns 0 on success.
  135. */
  136. int radeon_driver_irq_postinstall_kms(struct drm_device *dev)
  137. {
  138. struct radeon_device *rdev = dev->dev_private;
  139. if (ASIC_IS_AVIVO(rdev))
  140. dev->max_vblank_count = 0x00ffffff;
  141. else
  142. dev->max_vblank_count = 0x001fffff;
  143. return 0;
  144. }
  145. /**
  146. * radeon_driver_irq_uninstall_kms - drm irq uninstall callback
  147. *
  148. * @dev: drm dev pointer
  149. *
  150. * This function disables all interrupt sources on the GPU (all asics).
  151. */
  152. void radeon_driver_irq_uninstall_kms(struct drm_device *dev)
  153. {
  154. struct radeon_device *rdev = dev->dev_private;
  155. unsigned long irqflags;
  156. unsigned i;
  157. if (rdev == NULL) {
  158. return;
  159. }
  160. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  161. /* Disable *all* interrupts */
  162. for (i = 0; i < RADEON_NUM_RINGS; i++)
  163. atomic_set(&rdev->irq.ring_int[i], 0);
  164. rdev->irq.dpm_thermal = false;
  165. for (i = 0; i < RADEON_MAX_HPD_PINS; i++)
  166. rdev->irq.hpd[i] = false;
  167. for (i = 0; i < RADEON_MAX_CRTCS; i++) {
  168. rdev->irq.crtc_vblank_int[i] = false;
  169. atomic_set(&rdev->irq.pflip[i], 0);
  170. rdev->irq.afmt[i] = false;
  171. }
  172. radeon_irq_set(rdev);
  173. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  174. }
  175. /**
  176. * radeon_msi_ok - asic specific msi checks
  177. *
  178. * @rdev: radeon device pointer
  179. *
  180. * Handles asic specific MSI checks to determine if
  181. * MSIs should be enabled on a particular chip (all asics).
  182. * Returns true if MSIs should be enabled, false if MSIs
  183. * should not be enabled.
  184. */
  185. static bool radeon_msi_ok(struct radeon_device *rdev)
  186. {
  187. /* RV370/RV380 was first asic with MSI support */
  188. if (rdev->family < CHIP_RV380)
  189. return false;
  190. /* MSIs don't work on AGP */
  191. if (rdev->flags & RADEON_IS_AGP)
  192. return false;
  193. /*
  194. * Older chips have a HW limitation, they can only generate 40 bits
  195. * of address for "64-bit" MSIs which breaks on some platforms, notably
  196. * IBM POWER servers, so we limit them
  197. */
  198. if (rdev->family < CHIP_BONAIRE) {
  199. dev_info(rdev->dev, "radeon: MSI limited to 32-bit\n");
  200. rdev->pdev->no_64bit_msi = 1;
  201. }
  202. /* force MSI on */
  203. if (radeon_msi == 1)
  204. return true;
  205. else if (radeon_msi == 0)
  206. return false;
  207. /* Quirks */
  208. /* HP RS690 only seems to work with MSIs. */
  209. if ((rdev->pdev->device == 0x791f) &&
  210. (rdev->pdev->subsystem_vendor == 0x103c) &&
  211. (rdev->pdev->subsystem_device == 0x30c2))
  212. return true;
  213. /* Dell RS690 only seems to work with MSIs. */
  214. if ((rdev->pdev->device == 0x791f) &&
  215. (rdev->pdev->subsystem_vendor == 0x1028) &&
  216. (rdev->pdev->subsystem_device == 0x01fc))
  217. return true;
  218. /* Dell RS690 only seems to work with MSIs. */
  219. if ((rdev->pdev->device == 0x791f) &&
  220. (rdev->pdev->subsystem_vendor == 0x1028) &&
  221. (rdev->pdev->subsystem_device == 0x01fd))
  222. return true;
  223. /* Gateway RS690 only seems to work with MSIs. */
  224. if ((rdev->pdev->device == 0x791f) &&
  225. (rdev->pdev->subsystem_vendor == 0x107b) &&
  226. (rdev->pdev->subsystem_device == 0x0185))
  227. return true;
  228. /* try and enable MSIs by default on all RS690s */
  229. if (rdev->family == CHIP_RS690)
  230. return true;
  231. /* RV515 seems to have MSI issues where it loses
  232. * MSI rearms occasionally. This leads to lockups and freezes.
  233. * disable it by default.
  234. */
  235. if (rdev->family == CHIP_RV515)
  236. return false;
  237. if (rdev->flags & RADEON_IS_IGP) {
  238. /* APUs work fine with MSIs */
  239. if (rdev->family >= CHIP_PALM)
  240. return true;
  241. /* lots of IGPs have problems with MSIs */
  242. return false;
  243. }
  244. return true;
  245. }
  246. /**
  247. * radeon_irq_kms_init - init driver interrupt info
  248. *
  249. * @rdev: radeon device pointer
  250. *
  251. * Sets up the work irq handlers, vblank init, MSIs, etc. (all asics).
  252. * Returns 0 for success, error for failure.
  253. */
  254. int radeon_irq_kms_init(struct radeon_device *rdev)
  255. {
  256. int r = 0;
  257. spin_lock_init(&rdev->irq.lock);
  258. /* Disable vblank irqs aggressively for power-saving */
  259. rdev->ddev->vblank_disable_immediate = true;
  260. r = drm_vblank_init(rdev->ddev, rdev->num_crtc);
  261. if (r) {
  262. return r;
  263. }
  264. /* enable msi */
  265. rdev->msi_enabled = 0;
  266. if (radeon_msi_ok(rdev)) {
  267. int ret = pci_enable_msi(rdev->pdev);
  268. if (!ret) {
  269. rdev->msi_enabled = 1;
  270. dev_info(rdev->dev, "radeon: using MSI.\n");
  271. }
  272. }
  273. INIT_DELAYED_WORK(&rdev->hotplug_work, radeon_hotplug_work_func);
  274. INIT_WORK(&rdev->dp_work, radeon_dp_work_func);
  275. INIT_WORK(&rdev->audio_work, r600_audio_update_hdmi);
  276. rdev->irq.installed = true;
  277. r = drm_irq_install(rdev->ddev, rdev->ddev->pdev->irq);
  278. if (r) {
  279. rdev->irq.installed = false;
  280. flush_delayed_work(&rdev->hotplug_work);
  281. return r;
  282. }
  283. DRM_INFO("radeon: irq initialized.\n");
  284. return 0;
  285. }
  286. /**
  287. * radeon_irq_kms_fini - tear down driver interrupt info
  288. *
  289. * @rdev: radeon device pointer
  290. *
  291. * Tears down the work irq handlers, vblank handlers, MSIs, etc. (all asics).
  292. */
  293. void radeon_irq_kms_fini(struct radeon_device *rdev)
  294. {
  295. if (rdev->irq.installed) {
  296. drm_irq_uninstall(rdev->ddev);
  297. rdev->irq.installed = false;
  298. if (rdev->msi_enabled)
  299. pci_disable_msi(rdev->pdev);
  300. flush_delayed_work(&rdev->hotplug_work);
  301. }
  302. }
  303. /**
  304. * radeon_irq_kms_sw_irq_get - enable software interrupt
  305. *
  306. * @rdev: radeon device pointer
  307. * @ring: ring whose interrupt you want to enable
  308. *
  309. * Enables the software interrupt for a specific ring (all asics).
  310. * The software interrupt is generally used to signal a fence on
  311. * a particular ring.
  312. */
  313. void radeon_irq_kms_sw_irq_get(struct radeon_device *rdev, int ring)
  314. {
  315. unsigned long irqflags;
  316. if (!rdev->ddev->irq_enabled)
  317. return;
  318. if (atomic_inc_return(&rdev->irq.ring_int[ring]) == 1) {
  319. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  320. radeon_irq_set(rdev);
  321. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  322. }
  323. }
  324. /**
  325. * radeon_irq_kms_sw_irq_get_delayed - enable software interrupt
  326. *
  327. * @rdev: radeon device pointer
  328. * @ring: ring whose interrupt you want to enable
  329. *
  330. * Enables the software interrupt for a specific ring (all asics).
  331. * The software interrupt is generally used to signal a fence on
  332. * a particular ring.
  333. */
  334. bool radeon_irq_kms_sw_irq_get_delayed(struct radeon_device *rdev, int ring)
  335. {
  336. return atomic_inc_return(&rdev->irq.ring_int[ring]) == 1;
  337. }
  338. /**
  339. * radeon_irq_kms_sw_irq_put - disable software interrupt
  340. *
  341. * @rdev: radeon device pointer
  342. * @ring: ring whose interrupt you want to disable
  343. *
  344. * Disables the software interrupt for a specific ring (all asics).
  345. * The software interrupt is generally used to signal a fence on
  346. * a particular ring.
  347. */
  348. void radeon_irq_kms_sw_irq_put(struct radeon_device *rdev, int ring)
  349. {
  350. unsigned long irqflags;
  351. if (!rdev->ddev->irq_enabled)
  352. return;
  353. if (atomic_dec_and_test(&rdev->irq.ring_int[ring])) {
  354. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  355. radeon_irq_set(rdev);
  356. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  357. }
  358. }
  359. /**
  360. * radeon_irq_kms_pflip_irq_get - enable pageflip interrupt
  361. *
  362. * @rdev: radeon device pointer
  363. * @crtc: crtc whose interrupt you want to enable
  364. *
  365. * Enables the pageflip interrupt for a specific crtc (all asics).
  366. * For pageflips we use the vblank interrupt source.
  367. */
  368. void radeon_irq_kms_pflip_irq_get(struct radeon_device *rdev, int crtc)
  369. {
  370. unsigned long irqflags;
  371. if (crtc < 0 || crtc >= rdev->num_crtc)
  372. return;
  373. if (!rdev->ddev->irq_enabled)
  374. return;
  375. if (atomic_inc_return(&rdev->irq.pflip[crtc]) == 1) {
  376. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  377. radeon_irq_set(rdev);
  378. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  379. }
  380. }
  381. /**
  382. * radeon_irq_kms_pflip_irq_put - disable pageflip interrupt
  383. *
  384. * @rdev: radeon device pointer
  385. * @crtc: crtc whose interrupt you want to disable
  386. *
  387. * Disables the pageflip interrupt for a specific crtc (all asics).
  388. * For pageflips we use the vblank interrupt source.
  389. */
  390. void radeon_irq_kms_pflip_irq_put(struct radeon_device *rdev, int crtc)
  391. {
  392. unsigned long irqflags;
  393. if (crtc < 0 || crtc >= rdev->num_crtc)
  394. return;
  395. if (!rdev->ddev->irq_enabled)
  396. return;
  397. if (atomic_dec_and_test(&rdev->irq.pflip[crtc])) {
  398. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  399. radeon_irq_set(rdev);
  400. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  401. }
  402. }
  403. /**
  404. * radeon_irq_kms_enable_afmt - enable audio format change interrupt
  405. *
  406. * @rdev: radeon device pointer
  407. * @block: afmt block whose interrupt you want to enable
  408. *
  409. * Enables the afmt change interrupt for a specific afmt block (all asics).
  410. */
  411. void radeon_irq_kms_enable_afmt(struct radeon_device *rdev, int block)
  412. {
  413. unsigned long irqflags;
  414. if (!rdev->ddev->irq_enabled)
  415. return;
  416. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  417. rdev->irq.afmt[block] = true;
  418. radeon_irq_set(rdev);
  419. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  420. }
  421. /**
  422. * radeon_irq_kms_disable_afmt - disable audio format change interrupt
  423. *
  424. * @rdev: radeon device pointer
  425. * @block: afmt block whose interrupt you want to disable
  426. *
  427. * Disables the afmt change interrupt for a specific afmt block (all asics).
  428. */
  429. void radeon_irq_kms_disable_afmt(struct radeon_device *rdev, int block)
  430. {
  431. unsigned long irqflags;
  432. if (!rdev->ddev->irq_enabled)
  433. return;
  434. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  435. rdev->irq.afmt[block] = false;
  436. radeon_irq_set(rdev);
  437. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  438. }
  439. /**
  440. * radeon_irq_kms_enable_hpd - enable hotplug detect interrupt
  441. *
  442. * @rdev: radeon device pointer
  443. * @hpd_mask: mask of hpd pins you want to enable.
  444. *
  445. * Enables the hotplug detect interrupt for a specific hpd pin (all asics).
  446. */
  447. void radeon_irq_kms_enable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
  448. {
  449. unsigned long irqflags;
  450. int i;
  451. if (!rdev->ddev->irq_enabled)
  452. return;
  453. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  454. for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
  455. rdev->irq.hpd[i] |= !!(hpd_mask & (1 << i));
  456. radeon_irq_set(rdev);
  457. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  458. }
  459. /**
  460. * radeon_irq_kms_disable_hpd - disable hotplug detect interrupt
  461. *
  462. * @rdev: radeon device pointer
  463. * @hpd_mask: mask of hpd pins you want to disable.
  464. *
  465. * Disables the hotplug detect interrupt for a specific hpd pin (all asics).
  466. */
  467. void radeon_irq_kms_disable_hpd(struct radeon_device *rdev, unsigned hpd_mask)
  468. {
  469. unsigned long irqflags;
  470. int i;
  471. if (!rdev->ddev->irq_enabled)
  472. return;
  473. spin_lock_irqsave(&rdev->irq.lock, irqflags);
  474. for (i = 0; i < RADEON_MAX_HPD_PINS; ++i)
  475. rdev->irq.hpd[i] &= !(hpd_mask & (1 << i));
  476. radeon_irq_set(rdev);
  477. spin_unlock_irqrestore(&rdev->irq.lock, irqflags);
  478. }
  479. /**
  480. * radeon_irq_kms_update_int_n - helper for updating interrupt enable registers
  481. *
  482. * @rdev: radeon device pointer
  483. * @reg: the register to write to enable/disable interrupts
  484. * @mask: the mask that enables the interrupts
  485. * @enable: whether to enable or disable the interrupt register
  486. * @name: the name of the interrupt register to print to the kernel log
  487. * @num: the number of the interrupt register to print to the kernel log
  488. *
  489. * Helper for updating the enable state of interrupt registers. Checks whether
  490. * or not the interrupt matches the enable state we want. If it doesn't, then
  491. * we update it and print a debugging message to the kernel log indicating the
  492. * new state of the interrupt register.
  493. *
  494. * Used for updating sequences of interrupts registers like HPD1, HPD2, etc.
  495. */
  496. void radeon_irq_kms_set_irq_n_enabled(struct radeon_device *rdev,
  497. u32 reg, u32 mask,
  498. bool enable, const char *name, unsigned n)
  499. {
  500. u32 tmp = RREG32(reg);
  501. /* Interrupt state didn't change */
  502. if (!!(tmp & mask) == enable)
  503. return;
  504. if (enable) {
  505. DRM_DEBUG("%s%d interrupts enabled\n", name, n);
  506. WREG32(reg, tmp |= mask);
  507. } else {
  508. DRM_DEBUG("%s%d interrupts disabled\n", name, n);
  509. WREG32(reg, tmp & ~mask);
  510. }
  511. }