xref: /linux/drivers/net/wwan/iosm/iosm_ipc_pcie.c (revision 1f52d7b622854b8bd7a1be3de095ca2e1f77098e)
17e98d785SM Chetan Kumar // SPDX-License-Identifier: GPL-2.0-only
27e98d785SM Chetan Kumar /*
37e98d785SM Chetan Kumar  * Copyright (C) 2020-21 Intel Corporation.
47e98d785SM Chetan Kumar  */
57e98d785SM Chetan Kumar 
67e98d785SM Chetan Kumar #include <linux/acpi.h>
77e98d785SM Chetan Kumar #include <linux/bitfield.h>
87e98d785SM Chetan Kumar #include <linux/module.h>
97e98d785SM Chetan Kumar #include <net/rtnetlink.h>
107e98d785SM Chetan Kumar 
117e98d785SM Chetan Kumar #include "iosm_ipc_imem.h"
127e98d785SM Chetan Kumar #include "iosm_ipc_pcie.h"
137e98d785SM Chetan Kumar #include "iosm_ipc_protocol.h"
147e98d785SM Chetan Kumar 
157e98d785SM Chetan Kumar MODULE_DESCRIPTION("IOSM Driver");
167e98d785SM Chetan Kumar MODULE_LICENSE("GPL v2");
177e98d785SM Chetan Kumar 
187e98d785SM Chetan Kumar /* WWAN GUID */
197e98d785SM Chetan Kumar static guid_t wwan_acpi_guid = GUID_INIT(0xbad01b75, 0x22a8, 0x4f48, 0x87, 0x92,
207e98d785SM Chetan Kumar 				       0xbd, 0xde, 0x94, 0x67, 0x74, 0x7d);
217e98d785SM Chetan Kumar 
227e98d785SM Chetan Kumar static void ipc_pcie_resources_release(struct iosm_pcie *ipc_pcie)
237e98d785SM Chetan Kumar {
247e98d785SM Chetan Kumar 	/* Free the MSI resources. */
257e98d785SM Chetan Kumar 	ipc_release_irq(ipc_pcie);
267e98d785SM Chetan Kumar 
277e98d785SM Chetan Kumar 	/* Free mapped doorbell scratchpad bus memory into CPU space. */
287e98d785SM Chetan Kumar 	iounmap(ipc_pcie->scratchpad);
297e98d785SM Chetan Kumar 
307e98d785SM Chetan Kumar 	/* Free mapped IPC_REGS bus memory into CPU space. */
317e98d785SM Chetan Kumar 	iounmap(ipc_pcie->ipc_regs);
327e98d785SM Chetan Kumar 
337e98d785SM Chetan Kumar 	/* Releases all PCI I/O and memory resources previously reserved by a
347e98d785SM Chetan Kumar 	 * successful call to pci_request_regions.  Call this function only
357e98d785SM Chetan Kumar 	 * after all use of the PCI regions has ceased.
367e98d785SM Chetan Kumar 	 */
377e98d785SM Chetan Kumar 	pci_release_regions(ipc_pcie->pci);
387e98d785SM Chetan Kumar }
397e98d785SM Chetan Kumar 
407e98d785SM Chetan Kumar static void ipc_pcie_cleanup(struct iosm_pcie *ipc_pcie)
417e98d785SM Chetan Kumar {
427e98d785SM Chetan Kumar 	/* Free the shared memory resources. */
437e98d785SM Chetan Kumar 	ipc_imem_cleanup(ipc_pcie->imem);
447e98d785SM Chetan Kumar 
457e98d785SM Chetan Kumar 	ipc_pcie_resources_release(ipc_pcie);
467e98d785SM Chetan Kumar 
477e98d785SM Chetan Kumar 	/* Signal to the system that the PCI device is not in use. */
487e98d785SM Chetan Kumar 	pci_disable_device(ipc_pcie->pci);
497e98d785SM Chetan Kumar }
507e98d785SM Chetan Kumar 
517e98d785SM Chetan Kumar static void ipc_pcie_deinit(struct iosm_pcie *ipc_pcie)
527e98d785SM Chetan Kumar {
537e98d785SM Chetan Kumar 	kfree(ipc_pcie->imem);
547e98d785SM Chetan Kumar 	kfree(ipc_pcie);
557e98d785SM Chetan Kumar }
567e98d785SM Chetan Kumar 
577e98d785SM Chetan Kumar static void ipc_pcie_remove(struct pci_dev *pci)
587e98d785SM Chetan Kumar {
597e98d785SM Chetan Kumar 	struct iosm_pcie *ipc_pcie = pci_get_drvdata(pci);
607e98d785SM Chetan Kumar 
617e98d785SM Chetan Kumar 	ipc_pcie_cleanup(ipc_pcie);
627e98d785SM Chetan Kumar 
637e98d785SM Chetan Kumar 	ipc_pcie_deinit(ipc_pcie);
647e98d785SM Chetan Kumar }
657e98d785SM Chetan Kumar 
667e98d785SM Chetan Kumar static int ipc_pcie_resources_request(struct iosm_pcie *ipc_pcie)
677e98d785SM Chetan Kumar {
687e98d785SM Chetan Kumar 	struct pci_dev *pci = ipc_pcie->pci;
697e98d785SM Chetan Kumar 	u32 cap = 0;
707e98d785SM Chetan Kumar 	u32 ret;
717e98d785SM Chetan Kumar 
727e98d785SM Chetan Kumar 	/* Reserved PCI I/O and memory resources.
737e98d785SM Chetan Kumar 	 * Mark all PCI regions associated with PCI device pci as
747e98d785SM Chetan Kumar 	 * being reserved by owner IOSM_IPC.
757e98d785SM Chetan Kumar 	 */
767e98d785SM Chetan Kumar 	ret = pci_request_regions(pci, "IOSM_IPC");
777e98d785SM Chetan Kumar 	if (ret) {
787e98d785SM Chetan Kumar 		dev_err(ipc_pcie->dev, "failed pci request regions");
797e98d785SM Chetan Kumar 		goto pci_request_region_fail;
807e98d785SM Chetan Kumar 	}
817e98d785SM Chetan Kumar 
827e98d785SM Chetan Kumar 	/* Reserve the doorbell IPC REGS memory resources.
837e98d785SM Chetan Kumar 	 * Remap the memory into CPU space. Arrange for the physical address
847e98d785SM Chetan Kumar 	 * (BAR) to be visible from this driver.
857e98d785SM Chetan Kumar 	 * pci_ioremap_bar() ensures that the memory is marked uncachable.
867e98d785SM Chetan Kumar 	 */
877e98d785SM Chetan Kumar 	ipc_pcie->ipc_regs = pci_ioremap_bar(pci, ipc_pcie->ipc_regs_bar_nr);
887e98d785SM Chetan Kumar 
897e98d785SM Chetan Kumar 	if (!ipc_pcie->ipc_regs) {
907e98d785SM Chetan Kumar 		dev_err(ipc_pcie->dev, "IPC REGS ioremap error");
917e98d785SM Chetan Kumar 		ret = -EBUSY;
927e98d785SM Chetan Kumar 		goto ipc_regs_remap_fail;
937e98d785SM Chetan Kumar 	}
947e98d785SM Chetan Kumar 
957e98d785SM Chetan Kumar 	/* Reserve the MMIO scratchpad memory resources.
967e98d785SM Chetan Kumar 	 * Remap the memory into CPU space. Arrange for the physical address
977e98d785SM Chetan Kumar 	 * (BAR) to be visible from this driver.
987e98d785SM Chetan Kumar 	 * pci_ioremap_bar() ensures that the memory is marked uncachable.
997e98d785SM Chetan Kumar 	 */
1007e98d785SM Chetan Kumar 	ipc_pcie->scratchpad =
1017e98d785SM Chetan Kumar 		pci_ioremap_bar(pci, ipc_pcie->scratchpad_bar_nr);
1027e98d785SM Chetan Kumar 
1037e98d785SM Chetan Kumar 	if (!ipc_pcie->scratchpad) {
1047e98d785SM Chetan Kumar 		dev_err(ipc_pcie->dev, "doorbell scratchpad ioremap error");
1057e98d785SM Chetan Kumar 		ret = -EBUSY;
1067e98d785SM Chetan Kumar 		goto scratch_remap_fail;
1077e98d785SM Chetan Kumar 	}
1087e98d785SM Chetan Kumar 
1097e98d785SM Chetan Kumar 	/* Install the irq handler triggered by CP. */
1107e98d785SM Chetan Kumar 	ret = ipc_acquire_irq(ipc_pcie);
1117e98d785SM Chetan Kumar 	if (ret) {
1127e98d785SM Chetan Kumar 		dev_err(ipc_pcie->dev, "acquiring MSI irq failed!");
1137e98d785SM Chetan Kumar 		goto irq_acquire_fail;
1147e98d785SM Chetan Kumar 	}
1157e98d785SM Chetan Kumar 
1167e98d785SM Chetan Kumar 	/* Enable bus-mastering for the IOSM IPC device. */
1177e98d785SM Chetan Kumar 	pci_set_master(pci);
1187e98d785SM Chetan Kumar 
1197e98d785SM Chetan Kumar 	/* Enable LTR if possible
1207e98d785SM Chetan Kumar 	 * This is needed for L1.2!
1217e98d785SM Chetan Kumar 	 */
1227e98d785SM Chetan Kumar 	pcie_capability_read_dword(ipc_pcie->pci, PCI_EXP_DEVCAP2, &cap);
1237e98d785SM Chetan Kumar 	if (cap & PCI_EXP_DEVCAP2_LTR)
1247e98d785SM Chetan Kumar 		pcie_capability_set_word(ipc_pcie->pci, PCI_EXP_DEVCTL2,
1257e98d785SM Chetan Kumar 					 PCI_EXP_DEVCTL2_LTR_EN);
1267e98d785SM Chetan Kumar 
1277e98d785SM Chetan Kumar 	dev_dbg(ipc_pcie->dev, "link between AP and CP is fully on");
1287e98d785SM Chetan Kumar 
1297e98d785SM Chetan Kumar 	return ret;
1307e98d785SM Chetan Kumar 
1317e98d785SM Chetan Kumar irq_acquire_fail:
1327e98d785SM Chetan Kumar 	iounmap(ipc_pcie->scratchpad);
1337e98d785SM Chetan Kumar scratch_remap_fail:
1347e98d785SM Chetan Kumar 	iounmap(ipc_pcie->ipc_regs);
1357e98d785SM Chetan Kumar ipc_regs_remap_fail:
1367e98d785SM Chetan Kumar 	pci_release_regions(pci);
1377e98d785SM Chetan Kumar pci_request_region_fail:
1387e98d785SM Chetan Kumar 	return ret;
1397e98d785SM Chetan Kumar }
1407e98d785SM Chetan Kumar 
1417e98d785SM Chetan Kumar bool ipc_pcie_check_aspm_enabled(struct iosm_pcie *ipc_pcie,
1427e98d785SM Chetan Kumar 				 bool parent)
1437e98d785SM Chetan Kumar {
1447e98d785SM Chetan Kumar 	struct pci_dev *pdev;
1457e98d785SM Chetan Kumar 	u16 value = 0;
1467e98d785SM Chetan Kumar 	u32 enabled;
1477e98d785SM Chetan Kumar 
1487e98d785SM Chetan Kumar 	if (parent)
1497e98d785SM Chetan Kumar 		pdev = ipc_pcie->pci->bus->self;
1507e98d785SM Chetan Kumar 	else
1517e98d785SM Chetan Kumar 		pdev = ipc_pcie->pci;
1527e98d785SM Chetan Kumar 
1537e98d785SM Chetan Kumar 	pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &value);
1547e98d785SM Chetan Kumar 	enabled = value & PCI_EXP_LNKCTL_ASPMC;
1557e98d785SM Chetan Kumar 	dev_dbg(ipc_pcie->dev, "ASPM L1: 0x%04X 0x%03X", pdev->device, value);
1567e98d785SM Chetan Kumar 
1577e98d785SM Chetan Kumar 	return (enabled == PCI_EXP_LNKCTL_ASPM_L1 ||
1587e98d785SM Chetan Kumar 		enabled == PCI_EXP_LNKCTL_ASPMC);
1597e98d785SM Chetan Kumar }
1607e98d785SM Chetan Kumar 
1617e98d785SM Chetan Kumar bool ipc_pcie_check_data_link_active(struct iosm_pcie *ipc_pcie)
1627e98d785SM Chetan Kumar {
1637e98d785SM Chetan Kumar 	struct pci_dev *parent;
1647e98d785SM Chetan Kumar 	u16 link_status = 0;
1657e98d785SM Chetan Kumar 
1667e98d785SM Chetan Kumar 	if (!ipc_pcie->pci->bus || !ipc_pcie->pci->bus->self) {
1677e98d785SM Chetan Kumar 		dev_err(ipc_pcie->dev, "root port not found");
1687e98d785SM Chetan Kumar 		return false;
1697e98d785SM Chetan Kumar 	}
1707e98d785SM Chetan Kumar 
1717e98d785SM Chetan Kumar 	parent = ipc_pcie->pci->bus->self;
1727e98d785SM Chetan Kumar 
1737e98d785SM Chetan Kumar 	pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &link_status);
1747e98d785SM Chetan Kumar 	dev_dbg(ipc_pcie->dev, "Link status: 0x%04X", link_status);
1757e98d785SM Chetan Kumar 
1767e98d785SM Chetan Kumar 	return link_status & PCI_EXP_LNKSTA_DLLLA;
1777e98d785SM Chetan Kumar }
1787e98d785SM Chetan Kumar 
1797e98d785SM Chetan Kumar static bool ipc_pcie_check_aspm_supported(struct iosm_pcie *ipc_pcie,
1807e98d785SM Chetan Kumar 					  bool parent)
1817e98d785SM Chetan Kumar {
1827e98d785SM Chetan Kumar 	struct pci_dev *pdev;
1837e98d785SM Chetan Kumar 	u32 support;
1847e98d785SM Chetan Kumar 	u32 cap = 0;
1857e98d785SM Chetan Kumar 
1867e98d785SM Chetan Kumar 	if (parent)
1877e98d785SM Chetan Kumar 		pdev = ipc_pcie->pci->bus->self;
1887e98d785SM Chetan Kumar 	else
1897e98d785SM Chetan Kumar 		pdev = ipc_pcie->pci;
1907e98d785SM Chetan Kumar 	pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &cap);
1917e98d785SM Chetan Kumar 	support = u32_get_bits(cap, PCI_EXP_LNKCAP_ASPMS);
1927e98d785SM Chetan Kumar 	if (support < PCI_EXP_LNKCTL_ASPM_L1) {
1937e98d785SM Chetan Kumar 		dev_dbg(ipc_pcie->dev, "ASPM L1 not supported: 0x%04X",
1947e98d785SM Chetan Kumar 			pdev->device);
1957e98d785SM Chetan Kumar 		return false;
1967e98d785SM Chetan Kumar 	}
1977e98d785SM Chetan Kumar 	return true;
1987e98d785SM Chetan Kumar }
1997e98d785SM Chetan Kumar 
2007e98d785SM Chetan Kumar void ipc_pcie_config_aspm(struct iosm_pcie *ipc_pcie)
2017e98d785SM Chetan Kumar {
2027e98d785SM Chetan Kumar 	bool parent_aspm_enabled, dev_aspm_enabled;
2037e98d785SM Chetan Kumar 
2047e98d785SM Chetan Kumar 	/* check if both root port and child supports ASPM L1 */
2057e98d785SM Chetan Kumar 	if (!ipc_pcie_check_aspm_supported(ipc_pcie, true) ||
2067e98d785SM Chetan Kumar 	    !ipc_pcie_check_aspm_supported(ipc_pcie, false))
2077e98d785SM Chetan Kumar 		return;
2087e98d785SM Chetan Kumar 
2097e98d785SM Chetan Kumar 	parent_aspm_enabled = ipc_pcie_check_aspm_enabled(ipc_pcie, true);
2107e98d785SM Chetan Kumar 	dev_aspm_enabled = ipc_pcie_check_aspm_enabled(ipc_pcie, false);
2117e98d785SM Chetan Kumar 
2127e98d785SM Chetan Kumar 	dev_dbg(ipc_pcie->dev, "ASPM parent: %s device: %s",
2137e98d785SM Chetan Kumar 		parent_aspm_enabled ? "Enabled" : "Disabled",
2147e98d785SM Chetan Kumar 		dev_aspm_enabled ? "Enabled" : "Disabled");
2157e98d785SM Chetan Kumar }
2167e98d785SM Chetan Kumar 
2177e98d785SM Chetan Kumar /* Initializes PCIe endpoint configuration */
2187e98d785SM Chetan Kumar static void ipc_pcie_config_init(struct iosm_pcie *ipc_pcie)
2197e98d785SM Chetan Kumar {
2207e98d785SM Chetan Kumar 	/* BAR0 is used for doorbell */
2217e98d785SM Chetan Kumar 	ipc_pcie->ipc_regs_bar_nr = IPC_DOORBELL_BAR0;
2227e98d785SM Chetan Kumar 
2237e98d785SM Chetan Kumar 	/* update HW configuration */
2247e98d785SM Chetan Kumar 	ipc_pcie->scratchpad_bar_nr = IPC_SCRATCHPAD_BAR2;
2257e98d785SM Chetan Kumar 	ipc_pcie->doorbell_reg_offset = IPC_DOORBELL_CH_OFFSET;
2267e98d785SM Chetan Kumar 	ipc_pcie->doorbell_write = IPC_WRITE_PTR_REG_0;
2277e98d785SM Chetan Kumar 	ipc_pcie->doorbell_capture = IPC_CAPTURE_PTR_REG_0;
2287e98d785SM Chetan Kumar }
2297e98d785SM Chetan Kumar 
2307e98d785SM Chetan Kumar /* This will read the BIOS WWAN RTD3 settings:
2317e98d785SM Chetan Kumar  * D0L1.2/D3L2/Disabled
2327e98d785SM Chetan Kumar  */
2337e98d785SM Chetan Kumar static enum ipc_pcie_sleep_state ipc_pcie_read_bios_cfg(struct device *dev)
2347e98d785SM Chetan Kumar {
2357e98d785SM Chetan Kumar 	union acpi_object *object;
2367e98d785SM Chetan Kumar 	acpi_handle handle_acpi;
2377e98d785SM Chetan Kumar 
2387e98d785SM Chetan Kumar 	handle_acpi = ACPI_HANDLE(dev);
2397e98d785SM Chetan Kumar 	if (!handle_acpi) {
2407e98d785SM Chetan Kumar 		pr_debug("pci device is NOT ACPI supporting device\n");
2417e98d785SM Chetan Kumar 		goto default_ret;
2427e98d785SM Chetan Kumar 	}
2437e98d785SM Chetan Kumar 
2447e98d785SM Chetan Kumar 	object = acpi_evaluate_dsm(handle_acpi, &wwan_acpi_guid, 0, 3, NULL);
2457e98d785SM Chetan Kumar 
2467e98d785SM Chetan Kumar 	if (object && object->integer.value == 3)
2477e98d785SM Chetan Kumar 		return IPC_PCIE_D3L2;
2487e98d785SM Chetan Kumar 
2497e98d785SM Chetan Kumar default_ret:
2507e98d785SM Chetan Kumar 	return IPC_PCIE_D0L12;
2517e98d785SM Chetan Kumar }
2527e98d785SM Chetan Kumar 
2537e98d785SM Chetan Kumar static int ipc_pcie_probe(struct pci_dev *pci,
2547e98d785SM Chetan Kumar 			  const struct pci_device_id *pci_id)
2557e98d785SM Chetan Kumar {
2567e98d785SM Chetan Kumar 	struct iosm_pcie *ipc_pcie = kzalloc(sizeof(*ipc_pcie), GFP_KERNEL);
2577e98d785SM Chetan Kumar 
2587e98d785SM Chetan Kumar 	pr_debug("Probing device 0x%X from the vendor 0x%X", pci_id->device,
2597e98d785SM Chetan Kumar 		 pci_id->vendor);
2607e98d785SM Chetan Kumar 
2617e98d785SM Chetan Kumar 	if (!ipc_pcie)
2627e98d785SM Chetan Kumar 		goto ret_fail;
2637e98d785SM Chetan Kumar 
2647e98d785SM Chetan Kumar 	/* Initialize ipc dbg component for the PCIe device */
2657e98d785SM Chetan Kumar 	ipc_pcie->dev = &pci->dev;
2667e98d785SM Chetan Kumar 
2677e98d785SM Chetan Kumar 	/* Set the driver specific data. */
2687e98d785SM Chetan Kumar 	pci_set_drvdata(pci, ipc_pcie);
2697e98d785SM Chetan Kumar 
2707e98d785SM Chetan Kumar 	/* Save the address of the PCI device configuration. */
2717e98d785SM Chetan Kumar 	ipc_pcie->pci = pci;
2727e98d785SM Chetan Kumar 
2737e98d785SM Chetan Kumar 	/* Update platform configuration */
2747e98d785SM Chetan Kumar 	ipc_pcie_config_init(ipc_pcie);
2757e98d785SM Chetan Kumar 
2767e98d785SM Chetan Kumar 	/* Initialize the device before it is used. Ask low-level code
2777e98d785SM Chetan Kumar 	 * to enable I/O and memory. Wake up the device if it was suspended.
2787e98d785SM Chetan Kumar 	 */
2797e98d785SM Chetan Kumar 	if (pci_enable_device(pci)) {
2807e98d785SM Chetan Kumar 		dev_err(ipc_pcie->dev, "failed to enable the AP PCIe device");
2817e98d785SM Chetan Kumar 		/* If enable of PCIe device has failed then calling
2827e98d785SM Chetan Kumar 		 * ipc_pcie_cleanup will panic the system. More over
2837e98d785SM Chetan Kumar 		 * ipc_pcie_cleanup() is required to be called after
2847e98d785SM Chetan Kumar 		 * ipc_imem_mount()
2857e98d785SM Chetan Kumar 		 */
2867e98d785SM Chetan Kumar 		goto pci_enable_fail;
2877e98d785SM Chetan Kumar 	}
2887e98d785SM Chetan Kumar 
2897e98d785SM Chetan Kumar 	ipc_pcie_config_aspm(ipc_pcie);
2907e98d785SM Chetan Kumar 	dev_dbg(ipc_pcie->dev, "PCIe device enabled.");
2917e98d785SM Chetan Kumar 
2927e98d785SM Chetan Kumar 	/* Read WWAN RTD3 BIOS Setting
2937e98d785SM Chetan Kumar 	 */
2947e98d785SM Chetan Kumar 	ipc_pcie->d3l2_support = ipc_pcie_read_bios_cfg(&pci->dev);
2957e98d785SM Chetan Kumar 
2967e98d785SM Chetan Kumar 	ipc_pcie->suspend = 0;
2977e98d785SM Chetan Kumar 
2987e98d785SM Chetan Kumar 	if (ipc_pcie_resources_request(ipc_pcie))
2997e98d785SM Chetan Kumar 		goto resources_req_fail;
3007e98d785SM Chetan Kumar 
3017e98d785SM Chetan Kumar 	/* Establish the link to the imem layer. */
3027e98d785SM Chetan Kumar 	ipc_pcie->imem = ipc_imem_init(ipc_pcie, pci->device,
3037e98d785SM Chetan Kumar 				       ipc_pcie->scratchpad, ipc_pcie->dev);
3047e98d785SM Chetan Kumar 	if (!ipc_pcie->imem) {
3057e98d785SM Chetan Kumar 		dev_err(ipc_pcie->dev, "failed to init imem");
3067e98d785SM Chetan Kumar 		goto imem_init_fail;
3077e98d785SM Chetan Kumar 	}
3087e98d785SM Chetan Kumar 
3097e98d785SM Chetan Kumar 	return 0;
3107e98d785SM Chetan Kumar 
3117e98d785SM Chetan Kumar imem_init_fail:
3127e98d785SM Chetan Kumar 	ipc_pcie_resources_release(ipc_pcie);
3137e98d785SM Chetan Kumar resources_req_fail:
3147e98d785SM Chetan Kumar 	pci_disable_device(pci);
3157e98d785SM Chetan Kumar pci_enable_fail:
3167e98d785SM Chetan Kumar 	kfree(ipc_pcie);
3177e98d785SM Chetan Kumar ret_fail:
3187e98d785SM Chetan Kumar 	return -EIO;
3197e98d785SM Chetan Kumar }
3207e98d785SM Chetan Kumar 
3217e98d785SM Chetan Kumar static const struct pci_device_id iosm_ipc_ids[] = {
3227e98d785SM Chetan Kumar 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, INTEL_CP_DEVICE_7560_ID) },
323*1f52d7b6SM Chetan Kumar 	{ PCI_DEVICE(PCI_VENDOR_ID_INTEL, INTEL_CP_DEVICE_7360_ID) },
3247e98d785SM Chetan Kumar 	{}
3257e98d785SM Chetan Kumar };
32695d359edSZou Wei MODULE_DEVICE_TABLE(pci, iosm_ipc_ids);
3277e98d785SM Chetan Kumar 
3287e98d785SM Chetan Kumar /* Enter sleep in s2idle case
3297e98d785SM Chetan Kumar  */
3307e98d785SM Chetan Kumar static int __maybe_unused ipc_pcie_suspend_s2idle(struct iosm_pcie *ipc_pcie)
3317e98d785SM Chetan Kumar {
3327e98d785SM Chetan Kumar 	ipc_cp_irq_sleep_control(ipc_pcie, IPC_MEM_DEV_PM_FORCE_SLEEP);
3337e98d785SM Chetan Kumar 
3347e98d785SM Chetan Kumar 	/* Complete all memory stores before setting bit */
3357e98d785SM Chetan Kumar 	smp_mb__before_atomic();
3367e98d785SM Chetan Kumar 
3377e98d785SM Chetan Kumar 	set_bit(0, &ipc_pcie->suspend);
3387e98d785SM Chetan Kumar 
3397e98d785SM Chetan Kumar 	/* Complete all memory stores after setting bit */
3407e98d785SM Chetan Kumar 	smp_mb__after_atomic();
3417e98d785SM Chetan Kumar 
3427e98d785SM Chetan Kumar 	ipc_imem_pm_s2idle_sleep(ipc_pcie->imem, true);
3437e98d785SM Chetan Kumar 
3447e98d785SM Chetan Kumar 	return 0;
3457e98d785SM Chetan Kumar }
3467e98d785SM Chetan Kumar 
3477e98d785SM Chetan Kumar /* Resume from sleep in s2idle case
3487e98d785SM Chetan Kumar  */
3497e98d785SM Chetan Kumar static int __maybe_unused ipc_pcie_resume_s2idle(struct iosm_pcie *ipc_pcie)
3507e98d785SM Chetan Kumar {
3517e98d785SM Chetan Kumar 	ipc_cp_irq_sleep_control(ipc_pcie, IPC_MEM_DEV_PM_FORCE_ACTIVE);
3527e98d785SM Chetan Kumar 
3537e98d785SM Chetan Kumar 	ipc_imem_pm_s2idle_sleep(ipc_pcie->imem, false);
3547e98d785SM Chetan Kumar 
3557e98d785SM Chetan Kumar 	/* Complete all memory stores before clearing bit. */
3567e98d785SM Chetan Kumar 	smp_mb__before_atomic();
3577e98d785SM Chetan Kumar 
3587e98d785SM Chetan Kumar 	clear_bit(0, &ipc_pcie->suspend);
3597e98d785SM Chetan Kumar 
3607e98d785SM Chetan Kumar 	/* Complete all memory stores after clearing bit. */
3617e98d785SM Chetan Kumar 	smp_mb__after_atomic();
3627e98d785SM Chetan Kumar 	return 0;
3637e98d785SM Chetan Kumar }
3647e98d785SM Chetan Kumar 
3657e98d785SM Chetan Kumar int __maybe_unused ipc_pcie_suspend(struct iosm_pcie *ipc_pcie)
3667e98d785SM Chetan Kumar {
3677e98d785SM Chetan Kumar 	/* The HAL shall ask the shared memory layer whether D3 is allowed. */
3687e98d785SM Chetan Kumar 	ipc_imem_pm_suspend(ipc_pcie->imem);
3697e98d785SM Chetan Kumar 
3707e98d785SM Chetan Kumar 	dev_dbg(ipc_pcie->dev, "SUSPEND done");
3718f58e29eSKai-Heng Feng 	return 0;
3727e98d785SM Chetan Kumar }
3737e98d785SM Chetan Kumar 
3747e98d785SM Chetan Kumar int __maybe_unused ipc_pcie_resume(struct iosm_pcie *ipc_pcie)
3757e98d785SM Chetan Kumar {
3767e98d785SM Chetan Kumar 	/* The HAL shall inform the shared memory layer that the device is
3777e98d785SM Chetan Kumar 	 * active.
3787e98d785SM Chetan Kumar 	 */
3797e98d785SM Chetan Kumar 	ipc_imem_pm_resume(ipc_pcie->imem);
3807e98d785SM Chetan Kumar 
3817e98d785SM Chetan Kumar 	dev_dbg(ipc_pcie->dev, "RESUME done");
3828f58e29eSKai-Heng Feng 	return 0;
3837e98d785SM Chetan Kumar }
3847e98d785SM Chetan Kumar 
3857e98d785SM Chetan Kumar static int __maybe_unused ipc_pcie_suspend_cb(struct device *dev)
3867e98d785SM Chetan Kumar {
3877e98d785SM Chetan Kumar 	struct iosm_pcie *ipc_pcie;
3887e98d785SM Chetan Kumar 	struct pci_dev *pdev;
3897e98d785SM Chetan Kumar 
3907e98d785SM Chetan Kumar 	pdev = to_pci_dev(dev);
3917e98d785SM Chetan Kumar 
3927e98d785SM Chetan Kumar 	ipc_pcie = pci_get_drvdata(pdev);
3937e98d785SM Chetan Kumar 
3947e98d785SM Chetan Kumar 	switch (ipc_pcie->d3l2_support) {
3957e98d785SM Chetan Kumar 	case IPC_PCIE_D0L12:
3967e98d785SM Chetan Kumar 		ipc_pcie_suspend_s2idle(ipc_pcie);
3977e98d785SM Chetan Kumar 		break;
3987e98d785SM Chetan Kumar 	case IPC_PCIE_D3L2:
3997e98d785SM Chetan Kumar 		ipc_pcie_suspend(ipc_pcie);
4007e98d785SM Chetan Kumar 		break;
4017e98d785SM Chetan Kumar 	}
4027e98d785SM Chetan Kumar 
4037e98d785SM Chetan Kumar 	return 0;
4047e98d785SM Chetan Kumar }
4057e98d785SM Chetan Kumar 
4067e98d785SM Chetan Kumar static int __maybe_unused ipc_pcie_resume_cb(struct device *dev)
4077e98d785SM Chetan Kumar {
4087e98d785SM Chetan Kumar 	struct iosm_pcie *ipc_pcie;
4097e98d785SM Chetan Kumar 	struct pci_dev *pdev;
4107e98d785SM Chetan Kumar 
4117e98d785SM Chetan Kumar 	pdev = to_pci_dev(dev);
4127e98d785SM Chetan Kumar 
4137e98d785SM Chetan Kumar 	ipc_pcie = pci_get_drvdata(pdev);
4147e98d785SM Chetan Kumar 
4157e98d785SM Chetan Kumar 	switch (ipc_pcie->d3l2_support) {
4167e98d785SM Chetan Kumar 	case IPC_PCIE_D0L12:
4177e98d785SM Chetan Kumar 		ipc_pcie_resume_s2idle(ipc_pcie);
4187e98d785SM Chetan Kumar 		break;
4197e98d785SM Chetan Kumar 	case IPC_PCIE_D3L2:
4207e98d785SM Chetan Kumar 		ipc_pcie_resume(ipc_pcie);
4217e98d785SM Chetan Kumar 		break;
4227e98d785SM Chetan Kumar 	}
4237e98d785SM Chetan Kumar 
4247e98d785SM Chetan Kumar 	return 0;
4257e98d785SM Chetan Kumar }
4267e98d785SM Chetan Kumar 
4277e98d785SM Chetan Kumar static SIMPLE_DEV_PM_OPS(iosm_ipc_pm, ipc_pcie_suspend_cb, ipc_pcie_resume_cb);
4287e98d785SM Chetan Kumar 
4297e98d785SM Chetan Kumar static struct pci_driver iosm_ipc_driver = {
4307e98d785SM Chetan Kumar 	.name = KBUILD_MODNAME,
4317e98d785SM Chetan Kumar 	.probe = ipc_pcie_probe,
4327e98d785SM Chetan Kumar 	.remove = ipc_pcie_remove,
4337e98d785SM Chetan Kumar 	.driver = {
4347e98d785SM Chetan Kumar 		.pm = &iosm_ipc_pm,
4357e98d785SM Chetan Kumar 	},
4367e98d785SM Chetan Kumar 	.id_table = iosm_ipc_ids,
4377e98d785SM Chetan Kumar };
4387f8b20d0SAndy Shevchenko module_pci_driver(iosm_ipc_driver);
4397e98d785SM Chetan Kumar 
4407e98d785SM Chetan Kumar int ipc_pcie_addr_map(struct iosm_pcie *ipc_pcie, unsigned char *data,
4417e98d785SM Chetan Kumar 		      size_t size, dma_addr_t *mapping, int direction)
4427e98d785SM Chetan Kumar {
4437e98d785SM Chetan Kumar 	if (ipc_pcie->pci) {
4447e98d785SM Chetan Kumar 		*mapping = dma_map_single(&ipc_pcie->pci->dev, data, size,
4457e98d785SM Chetan Kumar 					  direction);
4467e98d785SM Chetan Kumar 		if (dma_mapping_error(&ipc_pcie->pci->dev, *mapping)) {
4477e98d785SM Chetan Kumar 			dev_err(ipc_pcie->dev, "dma mapping failed");
4487e98d785SM Chetan Kumar 			return -EINVAL;
4497e98d785SM Chetan Kumar 		}
4507e98d785SM Chetan Kumar 	}
4517e98d785SM Chetan Kumar 	return 0;
4527e98d785SM Chetan Kumar }
4537e98d785SM Chetan Kumar 
4547e98d785SM Chetan Kumar void ipc_pcie_addr_unmap(struct iosm_pcie *ipc_pcie, size_t size,
4557e98d785SM Chetan Kumar 			 dma_addr_t mapping, int direction)
4567e98d785SM Chetan Kumar {
4577e98d785SM Chetan Kumar 	if (!mapping)
4587e98d785SM Chetan Kumar 		return;
4597e98d785SM Chetan Kumar 	if (ipc_pcie->pci)
4607e98d785SM Chetan Kumar 		dma_unmap_single(&ipc_pcie->pci->dev, mapping, size, direction);
4617e98d785SM Chetan Kumar }
4627e98d785SM Chetan Kumar 
4637e98d785SM Chetan Kumar struct sk_buff *ipc_pcie_alloc_local_skb(struct iosm_pcie *ipc_pcie,
4647e98d785SM Chetan Kumar 					 gfp_t flags, size_t size)
4657e98d785SM Chetan Kumar {
4667e98d785SM Chetan Kumar 	struct sk_buff *skb;
4677e98d785SM Chetan Kumar 
4687e98d785SM Chetan Kumar 	if (!ipc_pcie || !size) {
4697e98d785SM Chetan Kumar 		pr_err("invalid pcie object or size");
4707e98d785SM Chetan Kumar 		return NULL;
4717e98d785SM Chetan Kumar 	}
4727e98d785SM Chetan Kumar 
4737e98d785SM Chetan Kumar 	skb = __netdev_alloc_skb(NULL, size, flags);
4747e98d785SM Chetan Kumar 	if (!skb)
4757e98d785SM Chetan Kumar 		return NULL;
4767e98d785SM Chetan Kumar 
4777e98d785SM Chetan Kumar 	IPC_CB(skb)->op_type = (u8)UL_DEFAULT;
4787e98d785SM Chetan Kumar 	IPC_CB(skb)->mapping = 0;
4797e98d785SM Chetan Kumar 
4807e98d785SM Chetan Kumar 	return skb;
4817e98d785SM Chetan Kumar }
4827e98d785SM Chetan Kumar 
4837e98d785SM Chetan Kumar struct sk_buff *ipc_pcie_alloc_skb(struct iosm_pcie *ipc_pcie, size_t size,
4847e98d785SM Chetan Kumar 				   gfp_t flags, dma_addr_t *mapping,
4857e98d785SM Chetan Kumar 				   int direction, size_t headroom)
4867e98d785SM Chetan Kumar {
4877e98d785SM Chetan Kumar 	struct sk_buff *skb = ipc_pcie_alloc_local_skb(ipc_pcie, flags,
4887e98d785SM Chetan Kumar 						       size + headroom);
4897e98d785SM Chetan Kumar 	if (!skb)
4907e98d785SM Chetan Kumar 		return NULL;
4917e98d785SM Chetan Kumar 
4927e98d785SM Chetan Kumar 	if (headroom)
4937e98d785SM Chetan Kumar 		skb_reserve(skb, headroom);
4947e98d785SM Chetan Kumar 
4957e98d785SM Chetan Kumar 	if (ipc_pcie_addr_map(ipc_pcie, skb->data, size, mapping, direction)) {
4967e98d785SM Chetan Kumar 		dev_kfree_skb(skb);
4977e98d785SM Chetan Kumar 		return NULL;
4987e98d785SM Chetan Kumar 	}
4997e98d785SM Chetan Kumar 
5007e98d785SM Chetan Kumar 	BUILD_BUG_ON(sizeof(*IPC_CB(skb)) > sizeof(skb->cb));
5017e98d785SM Chetan Kumar 
5027e98d785SM Chetan Kumar 	/* Store the mapping address in skb scratch pad for later usage */
5037e98d785SM Chetan Kumar 	IPC_CB(skb)->mapping = *mapping;
5047e98d785SM Chetan Kumar 	IPC_CB(skb)->direction = direction;
5057e98d785SM Chetan Kumar 	IPC_CB(skb)->len = size;
5067e98d785SM Chetan Kumar 
5077e98d785SM Chetan Kumar 	return skb;
5087e98d785SM Chetan Kumar }
5097e98d785SM Chetan Kumar 
5107e98d785SM Chetan Kumar void ipc_pcie_kfree_skb(struct iosm_pcie *ipc_pcie, struct sk_buff *skb)
5117e98d785SM Chetan Kumar {
5127e98d785SM Chetan Kumar 	if (!skb)
5137e98d785SM Chetan Kumar 		return;
5147e98d785SM Chetan Kumar 
5157e98d785SM Chetan Kumar 	ipc_pcie_addr_unmap(ipc_pcie, IPC_CB(skb)->len, IPC_CB(skb)->mapping,
5167e98d785SM Chetan Kumar 			    IPC_CB(skb)->direction);
5177e98d785SM Chetan Kumar 	IPC_CB(skb)->mapping = 0;
5187e98d785SM Chetan Kumar 	dev_kfree_skb(skb);
5197e98d785SM Chetan Kumar }
520