1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020-21 Intel Corporation.
6 #include <linux/acpi.h>
7 #include <linux/bitfield.h>
8 #include <linux/module.h>
9 #include <net/rtnetlink.h>
11 #include "iosm_ipc_imem.h"
12 #include "iosm_ipc_pcie.h"
13 #include "iosm_ipc_protocol.h"
15 MODULE_DESCRIPTION("IOSM Driver");
16 MODULE_LICENSE("GPL v2");
19 static guid_t wwan_acpi_guid = GUID_INIT(0xbad01b75, 0x22a8, 0x4f48, 0x87, 0x92,
20 0xbd, 0xde, 0x94, 0x67, 0x74, 0x7d);
22 static void ipc_pcie_resources_release(struct iosm_pcie *ipc_pcie)
24 /* Free the MSI resources. */
25 ipc_release_irq(ipc_pcie);
27 /* Free mapped doorbell scratchpad bus memory into CPU space. */
28 iounmap(ipc_pcie->scratchpad);
30 /* Free mapped IPC_REGS bus memory into CPU space. */
31 iounmap(ipc_pcie->ipc_regs);
33 /* Releases all PCI I/O and memory resources previously reserved by a
34 * successful call to pci_request_regions. Call this function only
35 * after all use of the PCI regions has ceased.
37 pci_release_regions(ipc_pcie->pci);
40 static void ipc_pcie_cleanup(struct iosm_pcie *ipc_pcie)
42 /* Free the shared memory resources. */
43 ipc_imem_cleanup(ipc_pcie->imem);
45 ipc_pcie_resources_release(ipc_pcie);
47 /* Signal to the system that the PCI device is not in use. */
48 pci_disable_device(ipc_pcie->pci);
51 static void ipc_pcie_deinit(struct iosm_pcie *ipc_pcie)
53 kfree(ipc_pcie->imem);
57 static void ipc_pcie_remove(struct pci_dev *pci)
59 struct iosm_pcie *ipc_pcie = pci_get_drvdata(pci);
61 ipc_pcie_cleanup(ipc_pcie);
63 ipc_pcie_deinit(ipc_pcie);
66 static int ipc_pcie_resources_request(struct iosm_pcie *ipc_pcie)
68 struct pci_dev *pci = ipc_pcie->pci;
72 /* Reserved PCI I/O and memory resources.
73 * Mark all PCI regions associated with PCI device pci as
74 * being reserved by owner IOSM_IPC.
76 ret = pci_request_regions(pci, "IOSM_IPC");
78 dev_err(ipc_pcie->dev, "failed pci request regions");
79 goto pci_request_region_fail;
82 /* Reserve the doorbell IPC REGS memory resources.
83 * Remap the memory into CPU space. Arrange for the physical address
84 * (BAR) to be visible from this driver.
85 * pci_ioremap_bar() ensures that the memory is marked uncachable.
87 ipc_pcie->ipc_regs = pci_ioremap_bar(pci, ipc_pcie->ipc_regs_bar_nr);
89 if (!ipc_pcie->ipc_regs) {
90 dev_err(ipc_pcie->dev, "IPC REGS ioremap error");
92 goto ipc_regs_remap_fail;
95 /* Reserve the MMIO scratchpad memory resources.
96 * Remap the memory into CPU space. Arrange for the physical address
97 * (BAR) to be visible from this driver.
98 * pci_ioremap_bar() ensures that the memory is marked uncachable.
100 ipc_pcie->scratchpad =
101 pci_ioremap_bar(pci, ipc_pcie->scratchpad_bar_nr);
103 if (!ipc_pcie->scratchpad) {
104 dev_err(ipc_pcie->dev, "doorbell scratchpad ioremap error");
106 goto scratch_remap_fail;
109 /* Install the irq handler triggered by CP. */
110 ret = ipc_acquire_irq(ipc_pcie);
112 dev_err(ipc_pcie->dev, "acquiring MSI irq failed!");
113 goto irq_acquire_fail;
116 /* Enable bus-mastering for the IOSM IPC device. */
119 /* Enable LTR if possible
120 * This is needed for L1.2!
122 pcie_capability_read_dword(ipc_pcie->pci, PCI_EXP_DEVCAP2, &cap);
123 if (cap & PCI_EXP_DEVCAP2_LTR)
124 pcie_capability_set_word(ipc_pcie->pci, PCI_EXP_DEVCTL2,
125 PCI_EXP_DEVCTL2_LTR_EN);
127 dev_dbg(ipc_pcie->dev, "link between AP and CP is fully on");
132 iounmap(ipc_pcie->scratchpad);
134 iounmap(ipc_pcie->ipc_regs);
136 pci_release_regions(pci);
137 pci_request_region_fail:
141 bool ipc_pcie_check_aspm_enabled(struct iosm_pcie *ipc_pcie,
144 struct pci_dev *pdev;
149 pdev = ipc_pcie->pci->bus->self;
151 pdev = ipc_pcie->pci;
153 pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &value);
154 enabled = value & PCI_EXP_LNKCTL_ASPMC;
155 dev_dbg(ipc_pcie->dev, "ASPM L1: 0x%04X 0x%03X", pdev->device, value);
157 return (enabled == PCI_EXP_LNKCTL_ASPM_L1 ||
158 enabled == PCI_EXP_LNKCTL_ASPMC);
161 bool ipc_pcie_check_data_link_active(struct iosm_pcie *ipc_pcie)
163 struct pci_dev *parent;
166 if (!ipc_pcie->pci->bus || !ipc_pcie->pci->bus->self) {
167 dev_err(ipc_pcie->dev, "root port not found");
171 parent = ipc_pcie->pci->bus->self;
173 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &link_status);
174 dev_dbg(ipc_pcie->dev, "Link status: 0x%04X", link_status);
176 return link_status & PCI_EXP_LNKSTA_DLLLA;
179 static bool ipc_pcie_check_aspm_supported(struct iosm_pcie *ipc_pcie,
182 struct pci_dev *pdev;
187 pdev = ipc_pcie->pci->bus->self;
189 pdev = ipc_pcie->pci;
190 pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &cap);
191 support = u32_get_bits(cap, PCI_EXP_LNKCAP_ASPMS);
192 if (support < PCI_EXP_LNKCTL_ASPM_L1) {
193 dev_dbg(ipc_pcie->dev, "ASPM L1 not supported: 0x%04X",
200 void ipc_pcie_config_aspm(struct iosm_pcie *ipc_pcie)
202 bool parent_aspm_enabled, dev_aspm_enabled;
204 /* check if both root port and child supports ASPM L1 */
205 if (!ipc_pcie_check_aspm_supported(ipc_pcie, true) ||
206 !ipc_pcie_check_aspm_supported(ipc_pcie, false))
209 parent_aspm_enabled = ipc_pcie_check_aspm_enabled(ipc_pcie, true);
210 dev_aspm_enabled = ipc_pcie_check_aspm_enabled(ipc_pcie, false);
212 dev_dbg(ipc_pcie->dev, "ASPM parent: %s device: %s",
213 parent_aspm_enabled ? "Enabled" : "Disabled",
214 dev_aspm_enabled ? "Enabled" : "Disabled");
217 /* Initializes PCIe endpoint configuration */
218 static void ipc_pcie_config_init(struct iosm_pcie *ipc_pcie)
220 /* BAR0 is used for doorbell */
221 ipc_pcie->ipc_regs_bar_nr = IPC_DOORBELL_BAR0;
223 /* update HW configuration */
224 ipc_pcie->scratchpad_bar_nr = IPC_SCRATCHPAD_BAR2;
225 ipc_pcie->doorbell_reg_offset = IPC_DOORBELL_CH_OFFSET;
226 ipc_pcie->doorbell_write = IPC_WRITE_PTR_REG_0;
227 ipc_pcie->doorbell_capture = IPC_CAPTURE_PTR_REG_0;
230 /* This will read the BIOS WWAN RTD3 settings:
231 * D0L1.2/D3L2/Disabled
233 static enum ipc_pcie_sleep_state ipc_pcie_read_bios_cfg(struct device *dev)
235 union acpi_object *object;
236 acpi_handle handle_acpi;
238 handle_acpi = ACPI_HANDLE(dev);
240 pr_debug("pci device is NOT ACPI supporting device\n");
244 object = acpi_evaluate_dsm(handle_acpi, &wwan_acpi_guid, 0, 3, NULL);
246 if (object && object->integer.value == 3)
247 return IPC_PCIE_D3L2;
250 return IPC_PCIE_D0L12;
253 static int ipc_pcie_probe(struct pci_dev *pci,
254 const struct pci_device_id *pci_id)
256 struct iosm_pcie *ipc_pcie = kzalloc(sizeof(*ipc_pcie), GFP_KERNEL);
258 pr_debug("Probing device 0x%X from the vendor 0x%X", pci_id->device,
264 /* Initialize ipc dbg component for the PCIe device */
265 ipc_pcie->dev = &pci->dev;
267 /* Set the driver specific data. */
268 pci_set_drvdata(pci, ipc_pcie);
270 /* Save the address of the PCI device configuration. */
273 /* Update platform configuration */
274 ipc_pcie_config_init(ipc_pcie);
276 /* Initialize the device before it is used. Ask low-level code
277 * to enable I/O and memory. Wake up the device if it was suspended.
279 if (pci_enable_device(pci)) {
280 dev_err(ipc_pcie->dev, "failed to enable the AP PCIe device");
281 /* If enable of PCIe device has failed then calling
282 * ipc_pcie_cleanup will panic the system. More over
283 * ipc_pcie_cleanup() is required to be called after
286 goto pci_enable_fail;
289 ipc_pcie_config_aspm(ipc_pcie);
290 dev_dbg(ipc_pcie->dev, "PCIe device enabled.");
292 /* Read WWAN RTD3 BIOS Setting
294 ipc_pcie->d3l2_support = ipc_pcie_read_bios_cfg(&pci->dev);
296 ipc_pcie->suspend = 0;
298 if (ipc_pcie_resources_request(ipc_pcie))
299 goto resources_req_fail;
301 /* Establish the link to the imem layer. */
302 ipc_pcie->imem = ipc_imem_init(ipc_pcie, pci->device,
303 ipc_pcie->scratchpad, ipc_pcie->dev);
304 if (!ipc_pcie->imem) {
305 dev_err(ipc_pcie->dev, "failed to init imem");
312 ipc_pcie_resources_release(ipc_pcie);
314 pci_disable_device(pci);
321 static const struct pci_device_id iosm_ipc_ids[] = {
322 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, INTEL_CP_DEVICE_7560_ID) },
325 MODULE_DEVICE_TABLE(pci, iosm_ipc_ids);
327 /* Enter sleep in s2idle case
329 static int __maybe_unused ipc_pcie_suspend_s2idle(struct iosm_pcie *ipc_pcie)
331 ipc_cp_irq_sleep_control(ipc_pcie, IPC_MEM_DEV_PM_FORCE_SLEEP);
333 /* Complete all memory stores before setting bit */
334 smp_mb__before_atomic();
336 set_bit(0, &ipc_pcie->suspend);
338 /* Complete all memory stores after setting bit */
339 smp_mb__after_atomic();
341 ipc_imem_pm_s2idle_sleep(ipc_pcie->imem, true);
346 /* Resume from sleep in s2idle case
348 static int __maybe_unused ipc_pcie_resume_s2idle(struct iosm_pcie *ipc_pcie)
350 ipc_cp_irq_sleep_control(ipc_pcie, IPC_MEM_DEV_PM_FORCE_ACTIVE);
352 ipc_imem_pm_s2idle_sleep(ipc_pcie->imem, false);
354 /* Complete all memory stores before clearing bit. */
355 smp_mb__before_atomic();
357 clear_bit(0, &ipc_pcie->suspend);
359 /* Complete all memory stores after clearing bit. */
360 smp_mb__after_atomic();
364 int __maybe_unused ipc_pcie_suspend(struct iosm_pcie *ipc_pcie)
366 struct pci_dev *pdev;
369 pdev = ipc_pcie->pci;
371 /* Execute D3 one time. */
372 if (pdev->current_state != PCI_D0) {
373 dev_dbg(ipc_pcie->dev, "done for PM=%d", pdev->current_state);
377 /* The HAL shall ask the shared memory layer whether D3 is allowed. */
378 ipc_imem_pm_suspend(ipc_pcie->imem);
380 /* Save the PCI configuration space of a device before suspending. */
381 ret = pci_save_state(pdev);
384 dev_err(ipc_pcie->dev, "pci_save_state error=%d", ret);
388 /* Set the power state of a PCI device.
389 * Transition a device to a new power state, using the device's PCI PM
392 ret = pci_set_power_state(pdev, PCI_D3cold);
395 dev_err(ipc_pcie->dev, "pci_set_power_state error=%d", ret);
399 dev_dbg(ipc_pcie->dev, "SUSPEND done");
403 int __maybe_unused ipc_pcie_resume(struct iosm_pcie *ipc_pcie)
407 /* Set the power state of a PCI device.
408 * Transition a device to a new power state, using the device's PCI PM
411 ret = pci_set_power_state(ipc_pcie->pci, PCI_D0);
414 dev_err(ipc_pcie->dev, "pci_set_power_state error=%d", ret);
418 pci_restore_state(ipc_pcie->pci);
420 /* The HAL shall inform the shared memory layer that the device is
423 ipc_imem_pm_resume(ipc_pcie->imem);
425 dev_dbg(ipc_pcie->dev, "RESUME done");
429 static int __maybe_unused ipc_pcie_suspend_cb(struct device *dev)
431 struct iosm_pcie *ipc_pcie;
432 struct pci_dev *pdev;
434 pdev = to_pci_dev(dev);
436 ipc_pcie = pci_get_drvdata(pdev);
438 switch (ipc_pcie->d3l2_support) {
440 ipc_pcie_suspend_s2idle(ipc_pcie);
443 ipc_pcie_suspend(ipc_pcie);
450 static int __maybe_unused ipc_pcie_resume_cb(struct device *dev)
452 struct iosm_pcie *ipc_pcie;
453 struct pci_dev *pdev;
455 pdev = to_pci_dev(dev);
457 ipc_pcie = pci_get_drvdata(pdev);
459 switch (ipc_pcie->d3l2_support) {
461 ipc_pcie_resume_s2idle(ipc_pcie);
464 ipc_pcie_resume(ipc_pcie);
471 static SIMPLE_DEV_PM_OPS(iosm_ipc_pm, ipc_pcie_suspend_cb, ipc_pcie_resume_cb);
473 static struct pci_driver iosm_ipc_driver = {
474 .name = KBUILD_MODNAME,
475 .probe = ipc_pcie_probe,
476 .remove = ipc_pcie_remove,
480 .id_table = iosm_ipc_ids,
483 int ipc_pcie_addr_map(struct iosm_pcie *ipc_pcie, unsigned char *data,
484 size_t size, dma_addr_t *mapping, int direction)
487 *mapping = dma_map_single(&ipc_pcie->pci->dev, data, size,
489 if (dma_mapping_error(&ipc_pcie->pci->dev, *mapping)) {
490 dev_err(ipc_pcie->dev, "dma mapping failed");
497 void ipc_pcie_addr_unmap(struct iosm_pcie *ipc_pcie, size_t size,
498 dma_addr_t mapping, int direction)
503 dma_unmap_single(&ipc_pcie->pci->dev, mapping, size, direction);
506 struct sk_buff *ipc_pcie_alloc_local_skb(struct iosm_pcie *ipc_pcie,
507 gfp_t flags, size_t size)
511 if (!ipc_pcie || !size) {
512 pr_err("invalid pcie object or size");
516 skb = __netdev_alloc_skb(NULL, size, flags);
520 IPC_CB(skb)->op_type = (u8)UL_DEFAULT;
521 IPC_CB(skb)->mapping = 0;
526 struct sk_buff *ipc_pcie_alloc_skb(struct iosm_pcie *ipc_pcie, size_t size,
527 gfp_t flags, dma_addr_t *mapping,
528 int direction, size_t headroom)
530 struct sk_buff *skb = ipc_pcie_alloc_local_skb(ipc_pcie, flags,
536 skb_reserve(skb, headroom);
538 if (ipc_pcie_addr_map(ipc_pcie, skb->data, size, mapping, direction)) {
543 BUILD_BUG_ON(sizeof(*IPC_CB(skb)) > sizeof(skb->cb));
545 /* Store the mapping address in skb scratch pad for later usage */
546 IPC_CB(skb)->mapping = *mapping;
547 IPC_CB(skb)->direction = direction;
548 IPC_CB(skb)->len = size;
553 void ipc_pcie_kfree_skb(struct iosm_pcie *ipc_pcie, struct sk_buff *skb)
558 ipc_pcie_addr_unmap(ipc_pcie, IPC_CB(skb)->len, IPC_CB(skb)->mapping,
559 IPC_CB(skb)->direction);
560 IPC_CB(skb)->mapping = 0;
564 static int __init iosm_ipc_driver_init(void)
566 if (pci_register_driver(&iosm_ipc_driver)) {
567 pr_err("registering of IOSM PCIe driver failed");
574 static void __exit iosm_ipc_driver_exit(void)
576 pci_unregister_driver(&iosm_ipc_driver);
579 module_init(iosm_ipc_driver_init);
580 module_exit(iosm_ipc_driver_exit);