iosf_mbi.c 15 KB

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