1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020-21 Intel Corporation.
4 */
5
6 #include <linux/acpi.h>
7 #include <linux/bitfield.h>
8 #include <linux/module.h>
9 #include <net/rtnetlink.h>
10
11 #include "iosm_ipc_imem.h"
12 #include "iosm_ipc_pcie.h"
13 #include "iosm_ipc_protocol.h"
14
15 MODULE_DESCRIPTION("IOSM Driver");
16 MODULE_LICENSE("GPL v2");
17
18 /* WWAN GUID */
19 static guid_t wwan_acpi_guid = GUID_INIT(0xbad01b75, 0x22a8, 0x4f48, 0x87, 0x92,
20 0xbd, 0xde, 0x94, 0x67, 0x74, 0x7d);
21
ipc_pcie_resources_release(struct iosm_pcie * ipc_pcie)22 static void ipc_pcie_resources_release(struct iosm_pcie *ipc_pcie)
23 {
24 /* Free the MSI resources. */
25 ipc_release_irq(ipc_pcie);
26
27 /* Free mapped doorbell scratchpad bus memory into CPU space. */
28 iounmap(ipc_pcie->scratchpad);
29
30 /* Free mapped IPC_REGS bus memory into CPU space. */
31 iounmap(ipc_pcie->ipc_regs);
32
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.
36 */
37 pci_release_regions(ipc_pcie->pci);
38 }
39
ipc_pcie_cleanup(struct iosm_pcie * ipc_pcie)40 static void ipc_pcie_cleanup(struct iosm_pcie *ipc_pcie)
41 {
42 /* Free the shared memory resources. */
43 ipc_imem_cleanup(ipc_pcie->imem);
44
45 ipc_pcie_resources_release(ipc_pcie);
46
47 /* Signal to the system that the PCI device is not in use. */
48 pci_disable_device(ipc_pcie->pci);
49 }
50
ipc_pcie_deinit(struct iosm_pcie * ipc_pcie)51 static void ipc_pcie_deinit(struct iosm_pcie *ipc_pcie)
52 {
53 kfree(ipc_pcie->imem);
54 kfree(ipc_pcie);
55 }
56
ipc_pcie_remove(struct pci_dev * pci)57 static void ipc_pcie_remove(struct pci_dev *pci)
58 {
59 struct iosm_pcie *ipc_pcie = pci_get_drvdata(pci);
60
61 ipc_pcie_cleanup(ipc_pcie);
62
63 ipc_pcie_deinit(ipc_pcie);
64 }
65
ipc_pcie_resources_request(struct iosm_pcie * ipc_pcie)66 static int ipc_pcie_resources_request(struct iosm_pcie *ipc_pcie)
67 {
68 struct pci_dev *pci = ipc_pcie->pci;
69 u32 cap = 0;
70 u32 ret;
71
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.
75 */
76 ret = pci_request_regions(pci, "IOSM_IPC");
77 if (ret) {
78 dev_err(ipc_pcie->dev, "failed pci request regions");
79 goto pci_request_region_fail;
80 }
81
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.
86 */
87 ipc_pcie->ipc_regs = pci_ioremap_bar(pci, ipc_pcie->ipc_regs_bar_nr);
88
89 if (!ipc_pcie->ipc_regs) {
90 dev_err(ipc_pcie->dev, "IPC REGS ioremap error");
91 ret = -EBUSY;
92 goto ipc_regs_remap_fail;
93 }
94
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.
99 */
100 ipc_pcie->scratchpad =
101 pci_ioremap_bar(pci, ipc_pcie->scratchpad_bar_nr);
102
103 if (!ipc_pcie->scratchpad) {
104 dev_err(ipc_pcie->dev, "doorbell scratchpad ioremap error");
105 ret = -EBUSY;
106 goto scratch_remap_fail;
107 }
108
109 /* Install the irq handler triggered by CP. */
110 ret = ipc_acquire_irq(ipc_pcie);
111 if (ret) {
112 dev_err(ipc_pcie->dev, "acquiring MSI irq failed!");
113 goto irq_acquire_fail;
114 }
115
116 /* Enable bus-mastering for the IOSM IPC device. */
117 pci_set_master(pci);
118
119 /* Enable LTR if possible
120 * This is needed for L1.2!
121 */
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);
126
127 dev_dbg(ipc_pcie->dev, "link between AP and CP is fully on");
128
129 return ret;
130
131 irq_acquire_fail:
132 iounmap(ipc_pcie->scratchpad);
133 scratch_remap_fail:
134 iounmap(ipc_pcie->ipc_regs);
135 ipc_regs_remap_fail:
136 pci_release_regions(pci);
137 pci_request_region_fail:
138 return ret;
139 }
140
ipc_pcie_check_aspm_enabled(struct iosm_pcie * ipc_pcie,bool parent)141 bool ipc_pcie_check_aspm_enabled(struct iosm_pcie *ipc_pcie,
142 bool parent)
143 {
144 struct pci_dev *pdev;
145 u16 value = 0;
146 u32 enabled;
147
148 if (parent)
149 pdev = ipc_pcie->pci->bus->self;
150 else
151 pdev = ipc_pcie->pci;
152
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);
156
157 return (enabled == PCI_EXP_LNKCTL_ASPM_L1 ||
158 enabled == PCI_EXP_LNKCTL_ASPMC);
159 }
160
ipc_pcie_check_data_link_active(struct iosm_pcie * ipc_pcie)161 bool ipc_pcie_check_data_link_active(struct iosm_pcie *ipc_pcie)
162 {
163 struct pci_dev *parent;
164 u16 link_status = 0;
165
166 if (!ipc_pcie->pci->bus || !ipc_pcie->pci->bus->self) {
167 dev_err(ipc_pcie->dev, "root port not found");
168 return false;
169 }
170
171 parent = ipc_pcie->pci->bus->self;
172
173 pcie_capability_read_word(parent, PCI_EXP_LNKSTA, &link_status);
174 dev_dbg(ipc_pcie->dev, "Link status: 0x%04X", link_status);
175
176 return link_status & PCI_EXP_LNKSTA_DLLLA;
177 }
178
ipc_pcie_check_aspm_supported(struct iosm_pcie * ipc_pcie,bool parent)179 static bool ipc_pcie_check_aspm_supported(struct iosm_pcie *ipc_pcie,
180 bool parent)
181 {
182 struct pci_dev *pdev;
183 u32 support;
184 u32 cap = 0;
185
186 if (parent)
187 pdev = ipc_pcie->pci->bus->self;
188 else
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",
194 pdev->device);
195 return false;
196 }
197 return true;
198 }
199
ipc_pcie_config_aspm(struct iosm_pcie * ipc_pcie)200 void ipc_pcie_config_aspm(struct iosm_pcie *ipc_pcie)
201 {
202 bool parent_aspm_enabled, dev_aspm_enabled;
203
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))
207 return;
208
209 parent_aspm_enabled = ipc_pcie_check_aspm_enabled(ipc_pcie, true);
210 dev_aspm_enabled = ipc_pcie_check_aspm_enabled(ipc_pcie, false);
211
212 dev_dbg(ipc_pcie->dev, "ASPM parent: %s device: %s",
213 parent_aspm_enabled ? "Enabled" : "Disabled",
214 dev_aspm_enabled ? "Enabled" : "Disabled");
215 }
216
217 /* Initializes PCIe endpoint configuration */
ipc_pcie_config_init(struct iosm_pcie * ipc_pcie)218 static void ipc_pcie_config_init(struct iosm_pcie *ipc_pcie)
219 {
220 /* BAR0 is used for doorbell */
221 ipc_pcie->ipc_regs_bar_nr = IPC_DOORBELL_BAR0;
222
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;
228 }
229
230 /* This will read the BIOS WWAN RTD3 settings:
231 * D0L1.2/D3L2/Disabled
232 */
ipc_pcie_read_bios_cfg(struct device * dev)233 static enum ipc_pcie_sleep_state ipc_pcie_read_bios_cfg(struct device *dev)
234 {
235 enum ipc_pcie_sleep_state sleep_state = IPC_PCIE_D0L12;
236 union acpi_object *object;
237 acpi_handle handle_acpi;
238
239 handle_acpi = ACPI_HANDLE(dev);
240 if (!handle_acpi) {
241 pr_debug("pci device is NOT ACPI supporting device\n");
242 goto default_ret;
243 }
244
245 object = acpi_evaluate_dsm(handle_acpi, &wwan_acpi_guid, 0, 3, NULL);
246 if (!object)
247 goto default_ret;
248
249 if (object->integer.value == 3)
250 sleep_state = IPC_PCIE_D3L2;
251
252 ACPI_FREE(object);
253
254 default_ret:
255 return sleep_state;
256 }
257
ipc_pcie_probe(struct pci_dev * pci,const struct pci_device_id * pci_id)258 static int ipc_pcie_probe(struct pci_dev *pci,
259 const struct pci_device_id *pci_id)
260 {
261 struct iosm_pcie *ipc_pcie = kzalloc(sizeof(*ipc_pcie), GFP_KERNEL);
262
263 pr_debug("Probing device 0x%X from the vendor 0x%X", pci_id->device,
264 pci_id->vendor);
265
266 if (!ipc_pcie)
267 goto ret_fail;
268
269 /* Initialize ipc dbg component for the PCIe device */
270 ipc_pcie->dev = &pci->dev;
271
272 /* Set the driver specific data. */
273 pci_set_drvdata(pci, ipc_pcie);
274
275 /* Save the address of the PCI device configuration. */
276 ipc_pcie->pci = pci;
277
278 /* Update platform configuration */
279 ipc_pcie_config_init(ipc_pcie);
280
281 /* Initialize the device before it is used. Ask low-level code
282 * to enable I/O and memory. Wake up the device if it was suspended.
283 */
284 if (pci_enable_device(pci)) {
285 dev_err(ipc_pcie->dev, "failed to enable the AP PCIe device");
286 /* If enable of PCIe device has failed then calling
287 * ipc_pcie_cleanup will panic the system. More over
288 * ipc_pcie_cleanup() is required to be called after
289 * ipc_imem_mount()
290 */
291 goto pci_enable_fail;
292 }
293
294 ipc_pcie_config_aspm(ipc_pcie);
295 dev_dbg(ipc_pcie->dev, "PCIe device enabled.");
296
297 /* Read WWAN RTD3 BIOS Setting
298 */
299 ipc_pcie->d3l2_support = ipc_pcie_read_bios_cfg(&pci->dev);
300
301 ipc_pcie->suspend = 0;
302
303 if (ipc_pcie_resources_request(ipc_pcie))
304 goto resources_req_fail;
305
306 /* Establish the link to the imem layer. */
307 ipc_pcie->imem = ipc_imem_init(ipc_pcie, pci->device,
308 ipc_pcie->scratchpad, ipc_pcie->dev);
309 if (!ipc_pcie->imem) {
310 dev_err(ipc_pcie->dev, "failed to init imem");
311 goto imem_init_fail;
312 }
313
314 return 0;
315
316 imem_init_fail:
317 ipc_pcie_resources_release(ipc_pcie);
318 resources_req_fail:
319 pci_disable_device(pci);
320 pci_enable_fail:
321 kfree(ipc_pcie);
322 ret_fail:
323 return -EIO;
324 }
325
326 static const struct pci_device_id iosm_ipc_ids[] = {
327 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, INTEL_CP_DEVICE_7560_ID) },
328 {}
329 };
330 MODULE_DEVICE_TABLE(pci, iosm_ipc_ids);
331
332 /* Enter sleep in s2idle case
333 */
ipc_pcie_suspend_s2idle(struct iosm_pcie * ipc_pcie)334 static int __maybe_unused ipc_pcie_suspend_s2idle(struct iosm_pcie *ipc_pcie)
335 {
336 ipc_cp_irq_sleep_control(ipc_pcie, IPC_MEM_DEV_PM_FORCE_SLEEP);
337
338 /* Complete all memory stores before setting bit */
339 smp_mb__before_atomic();
340
341 set_bit(0, &ipc_pcie->suspend);
342
343 /* Complete all memory stores after setting bit */
344 smp_mb__after_atomic();
345
346 ipc_imem_pm_s2idle_sleep(ipc_pcie->imem, true);
347
348 return 0;
349 }
350
351 /* Resume from sleep in s2idle case
352 */
ipc_pcie_resume_s2idle(struct iosm_pcie * ipc_pcie)353 static int __maybe_unused ipc_pcie_resume_s2idle(struct iosm_pcie *ipc_pcie)
354 {
355 ipc_cp_irq_sleep_control(ipc_pcie, IPC_MEM_DEV_PM_FORCE_ACTIVE);
356
357 ipc_imem_pm_s2idle_sleep(ipc_pcie->imem, false);
358
359 /* Complete all memory stores before clearing bit. */
360 smp_mb__before_atomic();
361
362 clear_bit(0, &ipc_pcie->suspend);
363
364 /* Complete all memory stores after clearing bit. */
365 smp_mb__after_atomic();
366 return 0;
367 }
368
ipc_pcie_suspend(struct iosm_pcie * ipc_pcie)369 int __maybe_unused ipc_pcie_suspend(struct iosm_pcie *ipc_pcie)
370 {
371 struct pci_dev *pdev;
372 int ret;
373
374 pdev = ipc_pcie->pci;
375
376 /* Execute D3 one time. */
377 if (pdev->current_state != PCI_D0) {
378 dev_dbg(ipc_pcie->dev, "done for PM=%d", pdev->current_state);
379 return 0;
380 }
381
382 /* The HAL shall ask the shared memory layer whether D3 is allowed. */
383 ipc_imem_pm_suspend(ipc_pcie->imem);
384
385 /* Save the PCI configuration space of a device before suspending. */
386 ret = pci_save_state(pdev);
387
388 if (ret) {
389 dev_err(ipc_pcie->dev, "pci_save_state error=%d", ret);
390 return ret;
391 }
392
393 /* Set the power state of a PCI device.
394 * Transition a device to a new power state, using the device's PCI PM
395 * registers.
396 */
397 ret = pci_set_power_state(pdev, PCI_D3cold);
398
399 if (ret) {
400 dev_err(ipc_pcie->dev, "pci_set_power_state error=%d", ret);
401 return ret;
402 }
403
404 dev_dbg(ipc_pcie->dev, "SUSPEND done");
405 return ret;
406 }
407
ipc_pcie_resume(struct iosm_pcie * ipc_pcie)408 int __maybe_unused ipc_pcie_resume(struct iosm_pcie *ipc_pcie)
409 {
410 int ret;
411
412 /* Set the power state of a PCI device.
413 * Transition a device to a new power state, using the device's PCI PM
414 * registers.
415 */
416 ret = pci_set_power_state(ipc_pcie->pci, PCI_D0);
417
418 if (ret) {
419 dev_err(ipc_pcie->dev, "pci_set_power_state error=%d", ret);
420 return ret;
421 }
422
423 pci_restore_state(ipc_pcie->pci);
424
425 /* The HAL shall inform the shared memory layer that the device is
426 * active.
427 */
428 ipc_imem_pm_resume(ipc_pcie->imem);
429
430 dev_dbg(ipc_pcie->dev, "RESUME done");
431 return ret;
432 }
433
ipc_pcie_suspend_cb(struct device * dev)434 static int __maybe_unused ipc_pcie_suspend_cb(struct device *dev)
435 {
436 struct iosm_pcie *ipc_pcie;
437 struct pci_dev *pdev;
438
439 pdev = to_pci_dev(dev);
440
441 ipc_pcie = pci_get_drvdata(pdev);
442
443 switch (ipc_pcie->d3l2_support) {
444 case IPC_PCIE_D0L12:
445 ipc_pcie_suspend_s2idle(ipc_pcie);
446 break;
447 case IPC_PCIE_D3L2:
448 ipc_pcie_suspend(ipc_pcie);
449 break;
450 }
451
452 return 0;
453 }
454
ipc_pcie_resume_cb(struct device * dev)455 static int __maybe_unused ipc_pcie_resume_cb(struct device *dev)
456 {
457 struct iosm_pcie *ipc_pcie;
458 struct pci_dev *pdev;
459
460 pdev = to_pci_dev(dev);
461
462 ipc_pcie = pci_get_drvdata(pdev);
463
464 switch (ipc_pcie->d3l2_support) {
465 case IPC_PCIE_D0L12:
466 ipc_pcie_resume_s2idle(ipc_pcie);
467 break;
468 case IPC_PCIE_D3L2:
469 ipc_pcie_resume(ipc_pcie);
470 break;
471 }
472
473 return 0;
474 }
475
476 static SIMPLE_DEV_PM_OPS(iosm_ipc_pm, ipc_pcie_suspend_cb, ipc_pcie_resume_cb);
477
478 static struct pci_driver iosm_ipc_driver = {
479 .name = KBUILD_MODNAME,
480 .probe = ipc_pcie_probe,
481 .remove = ipc_pcie_remove,
482 .driver = {
483 .pm = &iosm_ipc_pm,
484 },
485 .id_table = iosm_ipc_ids,
486 };
487 module_pci_driver(iosm_ipc_driver);
488
ipc_pcie_addr_map(struct iosm_pcie * ipc_pcie,unsigned char * data,size_t size,dma_addr_t * mapping,int direction)489 int ipc_pcie_addr_map(struct iosm_pcie *ipc_pcie, unsigned char *data,
490 size_t size, dma_addr_t *mapping, int direction)
491 {
492 if (ipc_pcie->pci) {
493 *mapping = dma_map_single(&ipc_pcie->pci->dev, data, size,
494 direction);
495 if (dma_mapping_error(&ipc_pcie->pci->dev, *mapping)) {
496 dev_err(ipc_pcie->dev, "dma mapping failed");
497 return -EINVAL;
498 }
499 }
500 return 0;
501 }
502
ipc_pcie_addr_unmap(struct iosm_pcie * ipc_pcie,size_t size,dma_addr_t mapping,int direction)503 void ipc_pcie_addr_unmap(struct iosm_pcie *ipc_pcie, size_t size,
504 dma_addr_t mapping, int direction)
505 {
506 if (!mapping)
507 return;
508 if (ipc_pcie->pci)
509 dma_unmap_single(&ipc_pcie->pci->dev, mapping, size, direction);
510 }
511
ipc_pcie_alloc_local_skb(struct iosm_pcie * ipc_pcie,gfp_t flags,size_t size)512 struct sk_buff *ipc_pcie_alloc_local_skb(struct iosm_pcie *ipc_pcie,
513 gfp_t flags, size_t size)
514 {
515 struct sk_buff *skb;
516
517 if (!ipc_pcie || !size) {
518 pr_err("invalid pcie object or size");
519 return NULL;
520 }
521
522 skb = __netdev_alloc_skb(NULL, size, flags);
523 if (!skb)
524 return NULL;
525
526 IPC_CB(skb)->op_type = (u8)UL_DEFAULT;
527 IPC_CB(skb)->mapping = 0;
528
529 return skb;
530 }
531
ipc_pcie_alloc_skb(struct iosm_pcie * ipc_pcie,size_t size,gfp_t flags,dma_addr_t * mapping,int direction,size_t headroom)532 struct sk_buff *ipc_pcie_alloc_skb(struct iosm_pcie *ipc_pcie, size_t size,
533 gfp_t flags, dma_addr_t *mapping,
534 int direction, size_t headroom)
535 {
536 struct sk_buff *skb = ipc_pcie_alloc_local_skb(ipc_pcie, flags,
537 size + headroom);
538 if (!skb)
539 return NULL;
540
541 if (headroom)
542 skb_reserve(skb, headroom);
543
544 if (ipc_pcie_addr_map(ipc_pcie, skb->data, size, mapping, direction)) {
545 dev_kfree_skb(skb);
546 return NULL;
547 }
548
549 BUILD_BUG_ON(sizeof(*IPC_CB(skb)) > sizeof(skb->cb));
550
551 /* Store the mapping address in skb scratch pad for later usage */
552 IPC_CB(skb)->mapping = *mapping;
553 IPC_CB(skb)->direction = direction;
554 IPC_CB(skb)->len = size;
555
556 return skb;
557 }
558
ipc_pcie_kfree_skb(struct iosm_pcie * ipc_pcie,struct sk_buff * skb)559 void ipc_pcie_kfree_skb(struct iosm_pcie *ipc_pcie, struct sk_buff *skb)
560 {
561 if (!skb)
562 return;
563
564 ipc_pcie_addr_unmap(ipc_pcie, IPC_CB(skb)->len, IPC_CB(skb)->mapping,
565 IPC_CB(skb)->direction);
566 IPC_CB(skb)->mapping = 0;
567 dev_kfree_skb(skb);
568 }
569