iosf_mbi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * IOSF-SB MailBox Interface Driver
  4. * Copyright (c) 2013, Intel Corporation.
  5. *
  6. * The IOSF-SB is a fabric bus available on Atom based SOC's that uses a
  7. * mailbox interface (MBI) to communicate with multiple devices. This
  8. * driver implements access to this interface for those platforms that can
  9. * enumerate the device using PCI.
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/pci.h>
  16. #include <linux/debugfs.h>
  17. #include <linux/capability.h>
  18. #include <linux/pm_qos.h>
  19. #include <linux/wait.h>
  20. #include <asm/iosf_mbi.h>
  21. #define PCI_DEVICE_ID_INTEL_BAYTRAIL 0x0F00
  22. #define PCI_DEVICE_ID_INTEL_BRASWELL 0x2280
  23. #define PCI_DEVICE_ID_INTEL_QUARK_X1000 0x0958
  24. #define PCI_DEVICE_ID_INTEL_TANGIER 0x1170
  25. static struct pci_dev *mbi_pdev;
  26. static DEFINE_SPINLOCK(iosf_mbi_lock);
  27. /**************** Generic iosf_mbi access helpers ****************/
  28. static inline u32 iosf_mbi_form_mcr(u8 op, u8 port, u8 offset)
  29. {
  30. return (op << 24) | (port << 16) | (offset << 8) | MBI_ENABLE;
  31. }
  32. static int iosf_mbi_pci_read_mdr(u32 mcrx, u32 mcr, u32 *mdr)
  33. {
  34. int result;
  35. if (!mbi_pdev)
  36. return -ENODEV;
  37. if (mcrx) {
  38. result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
  39. mcrx);
  40. if (result < 0)
  41. goto fail_read;
  42. }
  43. result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
  44. if (result < 0)
  45. goto fail_read;
  46. result = pci_read_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
  47. if (result < 0)
  48. goto fail_read;
  49. return 0;
  50. fail_read:
  51. dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
  52. return pcibios_err_to_errno(result);
  53. }
  54. static int iosf_mbi_pci_write_mdr(u32 mcrx, u32 mcr, u32 mdr)
  55. {
  56. int result;
  57. if (!mbi_pdev)
  58. return -ENODEV;
  59. result = pci_write_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
  60. if (result < 0)
  61. goto fail_write;
  62. if (mcrx) {
  63. result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
  64. mcrx);
  65. if (result < 0)
  66. goto fail_write;
  67. }
  68. result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
  69. if (result < 0)
  70. goto fail_write;
  71. return 0;
  72. fail_write:
  73. dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
  74. return pcibios_err_to_errno(result);
  75. }
  76. int iosf_mbi_read(u8 port, u8 opcode, u32 offset, u32 *mdr)
  77. {
  78. u32 mcr, mcrx;
  79. unsigned long flags;
  80. int ret;
  81. /* Access to the GFX unit is handled by GPU code */
  82. if (port == BT_MBI_UNIT_GFX) {
  83. WARN_ON(1);
  84. return -EPERM;
  85. }
  86. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  87. mcrx = offset & MBI_MASK_HI;
  88. spin_lock_irqsave(&iosf_mbi_lock, flags);
  89. ret = iosf_mbi_pci_read_mdr(mcrx, mcr, mdr);
  90. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  91. return ret;
  92. }
  93. EXPORT_SYMBOL(iosf_mbi_read);
  94. int iosf_mbi_write(u8 port, u8 opcode, u32 offset, u32 mdr)
  95. {
  96. u32 mcr, mcrx;
  97. unsigned long flags;
  98. int ret;
  99. /* Access to the GFX unit is handled by GPU code */
  100. if (port == BT_MBI_UNIT_GFX) {
  101. WARN_ON(1);
  102. return -EPERM;
  103. }
  104. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  105. mcrx = offset & MBI_MASK_HI;
  106. spin_lock_irqsave(&iosf_mbi_lock, flags);
  107. ret = iosf_mbi_pci_write_mdr(mcrx, mcr, mdr);
  108. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  109. return ret;
  110. }
  111. EXPORT_SYMBOL(iosf_mbi_write);
  112. int iosf_mbi_modify(u8 port, u8 opcode, u32 offset, u32 mdr, u32 mask)
  113. {
  114. u32 mcr, mcrx;
  115. u32 value;
  116. unsigned long flags;
  117. int ret;
  118. /* Access to the GFX unit is handled by GPU code */
  119. if (port == BT_MBI_UNIT_GFX) {
  120. WARN_ON(1);
  121. return -EPERM;
  122. }
  123. mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
  124. mcrx = offset & MBI_MASK_HI;
  125. spin_lock_irqsave(&iosf_mbi_lock, flags);
  126. /* Read current mdr value */
  127. ret = iosf_mbi_pci_read_mdr(mcrx, mcr & MBI_RD_MASK, &value);
  128. if (ret < 0) {
  129. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  130. return ret;
  131. }
  132. /* Apply mask */
  133. value &= ~mask;
  134. mdr &= mask;
  135. value |= mdr;
  136. /* Write back */
  137. ret = iosf_mbi_pci_write_mdr(mcrx, mcr | MBI_WR_MASK, value);
  138. spin_unlock_irqrestore(&iosf_mbi_lock, flags);
  139. return ret;
  140. }
  141. EXPORT_SYMBOL(iosf_mbi_modify);
  142. bool iosf_mbi_available(void)
  143. {
  144. /* Mbi isn't hot-pluggable. No remove routine is provided */
  145. return mbi_pdev;
  146. }
  147. EXPORT_SYMBOL(iosf_mbi_available);
  148. /*
  149. **************** P-Unit/kernel shared I2C bus arbitration ****************
  150. *
  151. * Some Bay Trail and Cherry Trail devices have the P-Unit and us (the kernel)
  152. * share a single I2C bus to the PMIC. Below are helpers to arbitrate the
  153. * accesses between the kernel and the P-Unit.
  154. *
  155. * See arch/x86/include/asm/iosf_mbi.h for kernel-doc text for each function.
  156. */
  157. #define SEMAPHORE_TIMEOUT 500
  158. #define PUNIT_SEMAPHORE_BYT 0x7
  159. #define PUNIT_SEMAPHORE_CHT 0x10e
  160. #define PUNIT_SEMAPHORE_BIT BIT(0)
  161. #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
  162. static DEFINE_MUTEX(iosf_mbi_pmic_access_mutex);
  163. static BLOCKING_NOTIFIER_HEAD(iosf_mbi_pmic_bus_access_notifier);
  164. static DECLARE_WAIT_QUEUE_HEAD(iosf_mbi_pmic_access_waitq);
  165. static u32 iosf_mbi_pmic_punit_access_count;
  166. static u32 iosf_mbi_pmic_i2c_access_count;
  167. static u32 iosf_mbi_sem_address;
  168. static unsigned long iosf_mbi_sem_acquired;
  169. static struct pm_qos_request iosf_mbi_pm_qos;
  170. void iosf_mbi_punit_acquire(void)
  171. {
  172. /* Wait for any I2C PMIC accesses from in kernel drivers to finish. */
  173. mutex_lock(&iosf_mbi_pmic_access_mutex);
  174. while (iosf_mbi_pmic_i2c_access_count != 0) {
  175. mutex_unlock(&iosf_mbi_pmic_access_mutex);
  176. wait_event(iosf_mbi_pmic_access_waitq,
  177. iosf_mbi_pmic_i2c_access_count == 0);
  178. mutex_lock(&iosf_mbi_pmic_access_mutex);
  179. }
  180. /*
  181. * We do not need to do anything to allow the PUNIT to safely access
  182. * the PMIC, other then block in kernel accesses to the PMIC.
  183. */
  184. iosf_mbi_pmic_punit_access_count++;
  185. mutex_unlock(&iosf_mbi_pmic_access_mutex);
  186. }
  187. EXPORT_SYMBOL(iosf_mbi_punit_acquire);
  188. void iosf_mbi_punit_release(void)
  189. {
  190. bool do_wakeup;
  191. mutex_lock(&iosf_mbi_pmic_access_mutex);
  192. iosf_mbi_pmic_punit_access_count--;
  193. do_wakeup = iosf_mbi_pmic_punit_access_count == 0;
  194. mutex_unlock(&iosf_mbi_pmic_access_mutex);
  195. if (do_wakeup)
  196. wake_up(&iosf_mbi_pmic_access_waitq);
  197. }
  198. EXPORT_SYMBOL(iosf_mbi_punit_release);
  199. static int iosf_mbi_get_sem(u32 *sem)
  200. {
  201. int ret;
  202. ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ,
  203. iosf_mbi_sem_address, sem);
  204. if (ret) {
  205. dev_err(&mbi_pdev->dev, "Error P-Unit semaphore read failed\n");
  206. return ret;
  207. }
  208. *sem &= PUNIT_SEMAPHORE_BIT;
  209. return 0;
  210. }
  211. static void iosf_mbi_reset_semaphore(void)
  212. {
  213. if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ,
  214. iosf_mbi_sem_address, 0, PUNIT_SEMAPHORE_BIT))
  215. dev_err(&mbi_pdev->dev, "Error P-Unit semaphore reset failed\n");
  216. cpu_latency_qos_update_request(&iosf_mbi_pm_qos, PM_QOS_DEFAULT_VALUE);
  217. blocking_notifier_call_chain(&iosf_mbi_pmic_bus_access_notifier,
  218. MBI_PMIC_BUS_ACCESS_END, NULL);
  219. }
  220. /*
  221. * This function blocks P-Unit accesses to the PMIC I2C bus, so that kernel
  222. * I2C code, such as e.g. a fuel-gauge driver, can access it safely.
  223. *
  224. * This function may be called by I2C controller code while an I2C driver has
  225. * already blocked P-Unit accesses because it wants them blocked over multiple
  226. * i2c-transfers, for e.g. read-modify-write of an I2C client register.
  227. *
  228. * To allow safe PMIC i2c bus accesses this function takes the following steps:
  229. *
  230. * 1) Some code sends request to the P-Unit which make it access the PMIC
  231. * I2C bus. Testing has shown that the P-Unit does not check its internal
  232. * PMIC bus semaphore for these requests. Callers of these requests call
  233. * iosf_mbi_punit_acquire()/_release() around their P-Unit accesses, these
  234. * functions increase/decrease iosf_mbi_pmic_punit_access_count, so first
  235. * we wait for iosf_mbi_pmic_punit_access_count to become 0.
  236. *
  237. * 2) Check iosf_mbi_pmic_i2c_access_count, if access has already
  238. * been blocked by another caller, we only need to increment
  239. * iosf_mbi_pmic_i2c_access_count and we can skip the other steps.
  240. *
  241. * 3) Some code makes such P-Unit requests from atomic contexts where it
  242. * cannot call iosf_mbi_punit_acquire() as that may sleep.
  243. * As the second step we call a notifier chain which allows any code
  244. * needing P-Unit resources from atomic context to acquire them before
  245. * we take control over the PMIC I2C bus.
  246. *
  247. * 4) When CPU cores enter C6 or C7 the P-Unit needs to talk to the PMIC
  248. * if this happens while the kernel itself is accessing the PMIC I2C bus
  249. * the SoC hangs.
  250. * As the third step we call cpu_latency_qos_update_request() to disallow the
  251. * CPU to enter C6 or C7.
  252. *
  253. * 5) The P-Unit has a PMIC bus semaphore which we can request to stop
  254. * autonomous P-Unit tasks from accessing the PMIC I2C bus while we hold it.
  255. * As the fourth and final step we request this semaphore and wait for our
  256. * request to be acknowledged.
  257. */
  258. int iosf_mbi_block_punit_i2c_access(void)
  259. {
  260. unsigned long start, end;
  261. int ret = 0;
  262. u32 sem;
  263. if (WARN_ON(!mbi_pdev || !iosf_mbi_sem_address))
  264. return -ENXIO;
  265. mutex_lock(&iosf_mbi_pmic_access_mutex);
  266. while (iosf_mbi_pmic_punit_access_count != 0) {
  267. mutex_unlock(&iosf_mbi_pmic_access_mutex);
  268. wait_event(iosf_mbi_pmic_access_waitq,
  269. iosf_mbi_pmic_punit_access_count == 0);
  270. mutex_lock(&iosf_mbi_pmic_access_mutex);
  271. }
  272. if (iosf_mbi_pmic_i2c_access_count > 0)
  273. goto success;
  274. blocking_notifier_call_chain(&iosf_mbi_pmic_bus_access_notifier,
  275. MBI_PMIC_BUS_ACCESS_BEGIN, NULL);
  276. /*
  277. * Disallow the CPU to enter C6 or C7 state, entering these states
  278. * requires the P-Unit to talk to the PMIC and if this happens while
  279. * we're holding the semaphore, the SoC hangs.
  280. */
  281. cpu_latency_qos_update_request(&iosf_mbi_pm_qos, 0);
  282. /* host driver writes to side band semaphore register */
  283. ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE,
  284. iosf_mbi_sem_address, PUNIT_SEMAPHORE_ACQUIRE);
  285. if (ret) {
  286. dev_err(&mbi_pdev->dev, "Error P-Unit semaphore request failed\n");
  287. goto error;
  288. }
  289. /* host driver waits for bit 0 to be set in semaphore register */
  290. start = jiffies;
  291. end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
  292. do {
  293. ret = iosf_mbi_get_sem(&sem);
  294. if (!ret && sem) {
  295. iosf_mbi_sem_acquired = jiffies;
  296. dev_dbg(&mbi_pdev->dev, "P-Unit semaphore acquired after %ums\n",
  297. jiffies_to_msecs(jiffies - start));
  298. goto success;
  299. }
  300. usleep_range(1000, 2000);
  301. } while (time_before(jiffies, end));
  302. ret = -ETIMEDOUT;
  303. dev_err(&mbi_pdev->dev, "Error P-Unit semaphore timed out, resetting\n");
  304. error:
  305. iosf_mbi_reset_semaphore();
  306. if (!iosf_mbi_get_sem(&sem))
  307. dev_err(&mbi_pdev->dev, "P-Unit semaphore: %d\n", sem);
  308. success:
  309. if (!WARN_ON(ret))
  310. iosf_mbi_pmic_i2c_access_count++;
  311. mutex_unlock(&iosf_mbi_pmic_access_mutex);
  312. return ret;
  313. }
  314. EXPORT_SYMBOL(iosf_mbi_block_punit_i2c_access);
  315. void iosf_mbi_unblock_punit_i2c_access(void)
  316. {
  317. bool do_wakeup = false;
  318. mutex_lock(&iosf_mbi_pmic_access_mutex);
  319. iosf_mbi_pmic_i2c_access_count--;
  320. if (iosf_mbi_pmic_i2c_access_count == 0) {
  321. iosf_mbi_reset_semaphore();
  322. dev_dbg(&mbi_pdev->dev, "punit semaphore held for %ums\n",
  323. jiffies_to_msecs(jiffies - iosf_mbi_sem_acquired));
  324. do_wakeup = true;
  325. }
  326. mutex_unlock(&iosf_mbi_pmic_access_mutex);
  327. if (do_wakeup)
  328. wake_up(&iosf_mbi_pmic_access_waitq);
  329. }
  330. EXPORT_SYMBOL(iosf_mbi_unblock_punit_i2c_access);
  331. int iosf_mbi_register_pmic_bus_access_notifier(struct notifier_block *nb)
  332. {
  333. int ret;
  334. /* Wait for the bus to go inactive before registering */
  335. iosf_mbi_punit_acquire();
  336. ret = blocking_notifier_chain_register(
  337. &iosf_mbi_pmic_bus_access_notifier, nb);
  338. iosf_mbi_punit_release();
  339. return ret;
  340. }
  341. EXPORT_SYMBOL(iosf_mbi_register_pmic_bus_access_notifier);
  342. int iosf_mbi_unregister_pmic_bus_access_notifier_unlocked(
  343. struct notifier_block *nb)
  344. {
  345. iosf_mbi_assert_punit_acquired();
  346. return blocking_notifier_chain_unregister(
  347. &iosf_mbi_pmic_bus_access_notifier, nb);
  348. }
  349. EXPORT_SYMBOL(iosf_mbi_unregister_pmic_bus_access_notifier_unlocked);
  350. int iosf_mbi_unregister_pmic_bus_access_notifier(struct notifier_block *nb)
  351. {
  352. int ret;
  353. /* Wait for the bus to go inactive before unregistering */
  354. iosf_mbi_punit_acquire();
  355. ret = iosf_mbi_unregister_pmic_bus_access_notifier_unlocked(nb);
  356. iosf_mbi_punit_release();
  357. return ret;
  358. }
  359. EXPORT_SYMBOL(iosf_mbi_unregister_pmic_bus_access_notifier);
  360. void iosf_mbi_assert_punit_acquired(void)
  361. {
  362. WARN_ON(iosf_mbi_pmic_punit_access_count == 0);
  363. }
  364. EXPORT_SYMBOL(iosf_mbi_assert_punit_acquired);
  365. /**************** iosf_mbi debug code ****************/
  366. #ifdef CONFIG_IOSF_MBI_DEBUG
  367. static u32 dbg_mdr;
  368. static u32 dbg_mcr;
  369. static u32 dbg_mcrx;
  370. static int mcr_get(void *data, u64 *val)
  371. {
  372. *val = *(u32 *)data;
  373. return 0;
  374. }
  375. static int mcr_set(void *data, u64 val)
  376. {
  377. u8 command = ((u32)val & 0xFF000000) >> 24,
  378. port = ((u32)val & 0x00FF0000) >> 16,
  379. offset = ((u32)val & 0x0000FF00) >> 8;
  380. int err;
  381. *(u32 *)data = val;
  382. if (!capable(CAP_SYS_RAWIO))
  383. return -EACCES;
  384. if (command & 1u)
  385. err = iosf_mbi_write(port,
  386. command,
  387. dbg_mcrx | offset,
  388. dbg_mdr);
  389. else
  390. err = iosf_mbi_read(port,
  391. command,
  392. dbg_mcrx | offset,
  393. &dbg_mdr);
  394. return err;
  395. }
  396. DEFINE_SIMPLE_ATTRIBUTE(iosf_mcr_fops, mcr_get, mcr_set , "%llx\n");
  397. static struct dentry *iosf_dbg;
  398. static void iosf_sideband_debug_init(void)
  399. {
  400. iosf_dbg = debugfs_create_dir("iosf_sb", NULL);
  401. /* mdr */
  402. debugfs_create_x32("mdr", 0660, iosf_dbg, &dbg_mdr);
  403. /* mcrx */
  404. debugfs_create_x32("mcrx", 0660, iosf_dbg, &dbg_mcrx);
  405. /* mcr - initiates mailbox transaction */
  406. debugfs_create_file("mcr", 0660, iosf_dbg, &dbg_mcr, &iosf_mcr_fops);
  407. }
  408. static void iosf_debugfs_init(void)
  409. {
  410. iosf_sideband_debug_init();
  411. }
  412. static void iosf_debugfs_remove(void)
  413. {
  414. debugfs_remove_recursive(iosf_dbg);
  415. }
  416. #else
  417. static inline void iosf_debugfs_init(void) { }
  418. static inline void iosf_debugfs_remove(void) { }
  419. #endif /* CONFIG_IOSF_MBI_DEBUG */
  420. static int iosf_mbi_probe(struct pci_dev *pdev,
  421. const struct pci_device_id *dev_id)
  422. {
  423. int ret;
  424. ret = pci_enable_device(pdev);
  425. if (ret < 0) {
  426. dev_err(&pdev->dev, "error: could not enable device\n");
  427. return ret;
  428. }
  429. mbi_pdev = pci_dev_get(pdev);
  430. iosf_mbi_sem_address = dev_id->driver_data;
  431. return 0;
  432. }
  433. static const struct pci_device_id iosf_mbi_pci_ids[] = {
  434. { PCI_DEVICE_DATA(INTEL, BAYTRAIL, PUNIT_SEMAPHORE_BYT) },
  435. { PCI_DEVICE_DATA(INTEL, BRASWELL, PUNIT_SEMAPHORE_CHT) },
  436. { PCI_DEVICE_DATA(INTEL, QUARK_X1000, 0) },
  437. { PCI_DEVICE_DATA(INTEL, TANGIER, 0) },
  438. { 0, },
  439. };
  440. MODULE_DEVICE_TABLE(pci, iosf_mbi_pci_ids);
  441. static struct pci_driver iosf_mbi_pci_driver = {
  442. .name = "iosf_mbi_pci",
  443. .probe = iosf_mbi_probe,
  444. .id_table = iosf_mbi_pci_ids,
  445. };
  446. static int __init iosf_mbi_init(void)
  447. {
  448. iosf_debugfs_init();
  449. cpu_latency_qos_add_request(&iosf_mbi_pm_qos, PM_QOS_DEFAULT_VALUE);
  450. return pci_register_driver(&iosf_mbi_pci_driver);
  451. }
  452. static void __exit iosf_mbi_exit(void)
  453. {
  454. iosf_debugfs_remove();
  455. pci_unregister_driver(&iosf_mbi_pci_driver);
  456. pci_dev_put(mbi_pdev);
  457. mbi_pdev = NULL;
  458. cpu_latency_qos_remove_request(&iosf_mbi_pm_qos);
  459. }
  460. module_init(iosf_mbi_init);
  461. module_exit(iosf_mbi_exit);
  462. MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
  463. MODULE_DESCRIPTION("IOSF Mailbox Interface accessor");
  464. MODULE_LICENSE("GPL v2");