1 // SPDX-License-Identifier: GPL-2.0-only
3 * IOSF-SB MailBox Interface Driver
4 * Copyright (c) 2013, Intel Corporation.
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.
12 #include <linux/delay.h>
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/spinlock.h>
16 #include <linux/pci.h>
17 #include <linux/debugfs.h>
18 #include <linux/capability.h>
19 #include <linux/pm_qos.h>
21 #include <asm/iosf_mbi.h>
23 #define PCI_DEVICE_ID_INTEL_BAYTRAIL 0x0F00
24 #define PCI_DEVICE_ID_INTEL_BRASWELL 0x2280
25 #define PCI_DEVICE_ID_INTEL_QUARK_X1000 0x0958
26 #define PCI_DEVICE_ID_INTEL_TANGIER 0x1170
28 static struct pci_dev *mbi_pdev;
29 static DEFINE_SPINLOCK(iosf_mbi_lock);
31 /**************** Generic iosf_mbi access helpers ****************/
33 static inline u32 iosf_mbi_form_mcr(u8 op, u8 port, u8 offset)
35 return (op << 24) | (port << 16) | (offset << 8) | MBI_ENABLE;
38 static int iosf_mbi_pci_read_mdr(u32 mcrx, u32 mcr, u32 *mdr)
46 result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
52 result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
56 result = pci_read_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
63 dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
67 static int iosf_mbi_pci_write_mdr(u32 mcrx, u32 mcr, u32 mdr)
74 result = pci_write_config_dword(mbi_pdev, MBI_MDR_OFFSET, mdr);
79 result = pci_write_config_dword(mbi_pdev, MBI_MCRX_OFFSET,
85 result = pci_write_config_dword(mbi_pdev, MBI_MCR_OFFSET, mcr);
92 dev_err(&mbi_pdev->dev, "PCI config access failed with %d\n", result);
96 int iosf_mbi_read(u8 port, u8 opcode, u32 offset, u32 *mdr)
102 /* Access to the GFX unit is handled by GPU code */
103 if (port == BT_MBI_UNIT_GFX) {
108 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
109 mcrx = offset & MBI_MASK_HI;
111 spin_lock_irqsave(&iosf_mbi_lock, flags);
112 ret = iosf_mbi_pci_read_mdr(mcrx, mcr, mdr);
113 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
117 EXPORT_SYMBOL(iosf_mbi_read);
119 int iosf_mbi_write(u8 port, u8 opcode, u32 offset, u32 mdr)
125 /* Access to the GFX unit is handled by GPU code */
126 if (port == BT_MBI_UNIT_GFX) {
131 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
132 mcrx = offset & MBI_MASK_HI;
134 spin_lock_irqsave(&iosf_mbi_lock, flags);
135 ret = iosf_mbi_pci_write_mdr(mcrx, mcr, mdr);
136 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
140 EXPORT_SYMBOL(iosf_mbi_write);
142 int iosf_mbi_modify(u8 port, u8 opcode, u32 offset, u32 mdr, u32 mask)
149 /* Access to the GFX unit is handled by GPU code */
150 if (port == BT_MBI_UNIT_GFX) {
155 mcr = iosf_mbi_form_mcr(opcode, port, offset & MBI_MASK_LO);
156 mcrx = offset & MBI_MASK_HI;
158 spin_lock_irqsave(&iosf_mbi_lock, flags);
160 /* Read current mdr value */
161 ret = iosf_mbi_pci_read_mdr(mcrx, mcr & MBI_RD_MASK, &value);
163 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
173 ret = iosf_mbi_pci_write_mdr(mcrx, mcr | MBI_WR_MASK, value);
175 spin_unlock_irqrestore(&iosf_mbi_lock, flags);
179 EXPORT_SYMBOL(iosf_mbi_modify);
181 bool iosf_mbi_available(void)
183 /* Mbi isn't hot-pluggable. No remove routine is provided */
186 EXPORT_SYMBOL(iosf_mbi_available);
189 **************** P-Unit/kernel shared I2C bus arbritration ****************
191 * Some Bay Trail and Cherry Trail devices have the P-Unit and us (the kernel)
192 * share a single I2C bus to the PMIC. Below are helpers to arbitrate the
193 * accesses between the kernel and the P-Unit.
195 * See arch/x86/include/asm/iosf_mbi.h for kernel-doc text for each function.
198 #define SEMAPHORE_TIMEOUT 500
199 #define PUNIT_SEMAPHORE_BYT 0x7
200 #define PUNIT_SEMAPHORE_CHT 0x10e
201 #define PUNIT_SEMAPHORE_BIT BIT(0)
202 #define PUNIT_SEMAPHORE_ACQUIRE BIT(1)
204 static DEFINE_MUTEX(iosf_mbi_punit_mutex);
205 static DEFINE_MUTEX(iosf_mbi_block_punit_i2c_access_count_mutex);
206 static BLOCKING_NOTIFIER_HEAD(iosf_mbi_pmic_bus_access_notifier);
207 static u32 iosf_mbi_block_punit_i2c_access_count;
208 static u32 iosf_mbi_sem_address;
209 static unsigned long iosf_mbi_sem_acquired;
210 static struct pm_qos_request iosf_mbi_pm_qos;
212 void iosf_mbi_punit_acquire(void)
214 mutex_lock(&iosf_mbi_punit_mutex);
216 EXPORT_SYMBOL(iosf_mbi_punit_acquire);
218 void iosf_mbi_punit_release(void)
220 mutex_unlock(&iosf_mbi_punit_mutex);
222 EXPORT_SYMBOL(iosf_mbi_punit_release);
224 static int iosf_mbi_get_sem(u32 *sem)
228 ret = iosf_mbi_read(BT_MBI_UNIT_PMC, MBI_REG_READ,
229 iosf_mbi_sem_address, sem);
231 dev_err(&mbi_pdev->dev, "Error P-Unit semaphore read failed\n");
235 *sem &= PUNIT_SEMAPHORE_BIT;
239 static void iosf_mbi_reset_semaphore(void)
241 if (iosf_mbi_modify(BT_MBI_UNIT_PMC, MBI_REG_READ,
242 iosf_mbi_sem_address, 0, PUNIT_SEMAPHORE_BIT))
243 dev_err(&mbi_pdev->dev, "Error P-Unit semaphore reset failed\n");
245 pm_qos_update_request(&iosf_mbi_pm_qos, PM_QOS_DEFAULT_VALUE);
247 blocking_notifier_call_chain(&iosf_mbi_pmic_bus_access_notifier,
248 MBI_PMIC_BUS_ACCESS_END, NULL);
252 * This function blocks P-Unit accesses to the PMIC I2C bus, so that kernel
253 * I2C code, such as e.g. a fuel-gauge driver, can access it safely.
255 * This function may be called by I2C controller code while an I2C driver has
256 * already blocked P-Unit accesses because it wants them blocked over multiple
257 * i2c-transfers, for e.g. read-modify-write of an I2C client register.
259 * The P-Unit accesses already being blocked is tracked through the
260 * iosf_mbi_block_punit_i2c_access_count variable which is protected by the
261 * iosf_mbi_block_punit_i2c_access_count_mutex this mutex is hold for the
262 * entire duration of the function.
264 * If access is not blocked yet, this function takes the following steps:
266 * 1) Some code sends request to the P-Unit which make it access the PMIC
267 * I2C bus. Testing has shown that the P-Unit does not check its internal
268 * PMIC bus semaphore for these requests. Callers of these requests call
269 * iosf_mbi_punit_acquire()/_release() around their P-Unit accesses, these
270 * functions lock/unlock the iosf_mbi_punit_mutex.
271 * As the first step we lock the iosf_mbi_punit_mutex, to wait for any in
272 * flight requests to finish and to block any new requests.
274 * 2) Some code makes such P-Unit requests from atomic contexts where it
275 * cannot call iosf_mbi_punit_acquire() as that may sleep.
276 * As the second step we call a notifier chain which allows any code
277 * needing P-Unit resources from atomic context to acquire them before
278 * we take control over the PMIC I2C bus.
280 * 3) When CPU cores enter C6 or C7 the P-Unit needs to talk to the PMIC
281 * if this happens while the kernel itself is accessing the PMIC I2C bus
283 * As the third step we call pm_qos_update_request() to disallow the CPU
286 * 4) The P-Unit has a PMIC bus semaphore which we can request to stop
287 * autonomous P-Unit tasks from accessing the PMIC I2C bus while we hold it.
288 * As the fourth and final step we request this semaphore and wait for our
289 * request to be acknowledged.
291 int iosf_mbi_block_punit_i2c_access(void)
293 unsigned long start, end;
297 if (WARN_ON(!mbi_pdev || !iosf_mbi_sem_address))
300 mutex_lock(&iosf_mbi_block_punit_i2c_access_count_mutex);
302 if (iosf_mbi_block_punit_i2c_access_count > 0)
305 mutex_lock(&iosf_mbi_punit_mutex);
306 blocking_notifier_call_chain(&iosf_mbi_pmic_bus_access_notifier,
307 MBI_PMIC_BUS_ACCESS_BEGIN, NULL);
310 * Disallow the CPU to enter C6 or C7 state, entering these states
311 * requires the P-Unit to talk to the PMIC and if this happens while
312 * we're holding the semaphore, the SoC hangs.
314 pm_qos_update_request(&iosf_mbi_pm_qos, 0);
316 /* host driver writes to side band semaphore register */
317 ret = iosf_mbi_write(BT_MBI_UNIT_PMC, MBI_REG_WRITE,
318 iosf_mbi_sem_address, PUNIT_SEMAPHORE_ACQUIRE);
320 dev_err(&mbi_pdev->dev, "Error P-Unit semaphore request failed\n");
324 /* host driver waits for bit 0 to be set in semaphore register */
326 end = start + msecs_to_jiffies(SEMAPHORE_TIMEOUT);
328 ret = iosf_mbi_get_sem(&sem);
330 iosf_mbi_sem_acquired = jiffies;
331 dev_dbg(&mbi_pdev->dev, "P-Unit semaphore acquired after %ums\n",
332 jiffies_to_msecs(jiffies - start));
334 * Success, keep iosf_mbi_punit_mutex locked till
335 * iosf_mbi_unblock_punit_i2c_access() gets called.
340 usleep_range(1000, 2000);
341 } while (time_before(jiffies, end));
344 dev_err(&mbi_pdev->dev, "Error P-Unit semaphore timed out, resetting\n");
346 iosf_mbi_reset_semaphore();
347 mutex_unlock(&iosf_mbi_punit_mutex);
349 if (!iosf_mbi_get_sem(&sem))
350 dev_err(&mbi_pdev->dev, "P-Unit semaphore: %d\n", sem);
353 iosf_mbi_block_punit_i2c_access_count++;
355 mutex_unlock(&iosf_mbi_block_punit_i2c_access_count_mutex);
359 EXPORT_SYMBOL(iosf_mbi_block_punit_i2c_access);
361 void iosf_mbi_unblock_punit_i2c_access(void)
363 mutex_lock(&iosf_mbi_block_punit_i2c_access_count_mutex);
365 iosf_mbi_block_punit_i2c_access_count--;
366 if (iosf_mbi_block_punit_i2c_access_count == 0) {
367 iosf_mbi_reset_semaphore();
368 mutex_unlock(&iosf_mbi_punit_mutex);
369 dev_dbg(&mbi_pdev->dev, "punit semaphore held for %ums\n",
370 jiffies_to_msecs(jiffies - iosf_mbi_sem_acquired));
373 mutex_unlock(&iosf_mbi_block_punit_i2c_access_count_mutex);
375 EXPORT_SYMBOL(iosf_mbi_unblock_punit_i2c_access);
377 int iosf_mbi_register_pmic_bus_access_notifier(struct notifier_block *nb)
381 /* Wait for the bus to go inactive before registering */
382 mutex_lock(&iosf_mbi_punit_mutex);
383 ret = blocking_notifier_chain_register(
384 &iosf_mbi_pmic_bus_access_notifier, nb);
385 mutex_unlock(&iosf_mbi_punit_mutex);
389 EXPORT_SYMBOL(iosf_mbi_register_pmic_bus_access_notifier);
391 int iosf_mbi_unregister_pmic_bus_access_notifier_unlocked(
392 struct notifier_block *nb)
394 iosf_mbi_assert_punit_acquired();
396 return blocking_notifier_chain_unregister(
397 &iosf_mbi_pmic_bus_access_notifier, nb);
399 EXPORT_SYMBOL(iosf_mbi_unregister_pmic_bus_access_notifier_unlocked);
401 int iosf_mbi_unregister_pmic_bus_access_notifier(struct notifier_block *nb)
405 /* Wait for the bus to go inactive before unregistering */
406 mutex_lock(&iosf_mbi_punit_mutex);
407 ret = iosf_mbi_unregister_pmic_bus_access_notifier_unlocked(nb);
408 mutex_unlock(&iosf_mbi_punit_mutex);
412 EXPORT_SYMBOL(iosf_mbi_unregister_pmic_bus_access_notifier);
414 void iosf_mbi_assert_punit_acquired(void)
416 WARN_ON(!mutex_is_locked(&iosf_mbi_punit_mutex));
418 EXPORT_SYMBOL(iosf_mbi_assert_punit_acquired);
420 /**************** iosf_mbi debug code ****************/
422 #ifdef CONFIG_IOSF_MBI_DEBUG
427 static int mcr_get(void *data, u64 *val)
433 static int mcr_set(void *data, u64 val)
435 u8 command = ((u32)val & 0xFF000000) >> 24,
436 port = ((u32)val & 0x00FF0000) >> 16,
437 offset = ((u32)val & 0x0000FF00) >> 8;
442 if (!capable(CAP_SYS_RAWIO))
446 err = iosf_mbi_write(port,
451 err = iosf_mbi_read(port,
458 DEFINE_SIMPLE_ATTRIBUTE(iosf_mcr_fops, mcr_get, mcr_set , "%llx\n");
460 static struct dentry *iosf_dbg;
462 static void iosf_sideband_debug_init(void)
466 iosf_dbg = debugfs_create_dir("iosf_sb", NULL);
467 if (IS_ERR_OR_NULL(iosf_dbg))
471 d = debugfs_create_x32("mdr", 0660, iosf_dbg, &dbg_mdr);
476 d = debugfs_create_x32("mcrx", 0660, iosf_dbg, &dbg_mcrx);
480 /* mcr - initiates mailbox tranaction */
481 d = debugfs_create_file("mcr", 0660, iosf_dbg, &dbg_mcr, &iosf_mcr_fops);
488 debugfs_remove_recursive(d);
491 static void iosf_debugfs_init(void)
493 iosf_sideband_debug_init();
496 static void iosf_debugfs_remove(void)
498 debugfs_remove_recursive(iosf_dbg);
501 static inline void iosf_debugfs_init(void) { }
502 static inline void iosf_debugfs_remove(void) { }
503 #endif /* CONFIG_IOSF_MBI_DEBUG */
505 static int iosf_mbi_probe(struct pci_dev *pdev,
506 const struct pci_device_id *dev_id)
510 ret = pci_enable_device(pdev);
512 dev_err(&pdev->dev, "error: could not enable device\n");
516 mbi_pdev = pci_dev_get(pdev);
517 iosf_mbi_sem_address = dev_id->driver_data;
522 static const struct pci_device_id iosf_mbi_pci_ids[] = {
523 { PCI_DEVICE_DATA(INTEL, BAYTRAIL, PUNIT_SEMAPHORE_BYT) },
524 { PCI_DEVICE_DATA(INTEL, BRASWELL, PUNIT_SEMAPHORE_CHT) },
525 { PCI_DEVICE_DATA(INTEL, QUARK_X1000, 0) },
526 { PCI_DEVICE_DATA(INTEL, TANGIER, 0) },
529 MODULE_DEVICE_TABLE(pci, iosf_mbi_pci_ids);
531 static struct pci_driver iosf_mbi_pci_driver = {
532 .name = "iosf_mbi_pci",
533 .probe = iosf_mbi_probe,
534 .id_table = iosf_mbi_pci_ids,
537 static int __init iosf_mbi_init(void)
541 pm_qos_add_request(&iosf_mbi_pm_qos, PM_QOS_CPU_DMA_LATENCY,
542 PM_QOS_DEFAULT_VALUE);
544 return pci_register_driver(&iosf_mbi_pci_driver);
547 static void __exit iosf_mbi_exit(void)
549 iosf_debugfs_remove();
551 pci_unregister_driver(&iosf_mbi_pci_driver);
552 pci_dev_put(mbi_pdev);
555 pm_qos_remove_request(&iosf_mbi_pm_qos);
558 module_init(iosf_mbi_init);
559 module_exit(iosf_mbi_exit);
561 MODULE_AUTHOR("David E. Box <david.e.box@linux.intel.com>");
562 MODULE_DESCRIPTION("IOSF Mailbox Interface accessor");
563 MODULE_LICENSE("GPL v2");