0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef TB_H_
0010 #define TB_H_
0011
0012 #include <linux/nvmem-provider.h>
0013 #include <linux/pci.h>
0014 #include <linux/thunderbolt.h>
0015 #include <linux/uuid.h>
0016 #include <linux/bitfield.h>
0017
0018 #include "tb_regs.h"
0019 #include "ctl.h"
0020 #include "dma_port.h"
0021
0022 #define NVM_MIN_SIZE SZ_32K
0023 #define NVM_MAX_SIZE SZ_512K
0024 #define NVM_DATA_DWORDS 16
0025
0026
0027 #define NVM_DEVID 0x05
0028 #define NVM_VERSION 0x08
0029 #define NVM_FLASH_SIZE 0x45
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 struct tb_nvm {
0050 struct device *dev;
0051 u8 major;
0052 u8 minor;
0053 int id;
0054 struct nvmem_device *active;
0055 struct nvmem_device *non_active;
0056 void *buf;
0057 size_t buf_data_size;
0058 bool authenticating;
0059 bool flushed;
0060 };
0061
0062 enum tb_nvm_write_ops {
0063 WRITE_AND_AUTHENTICATE = 1,
0064 WRITE_ONLY = 2,
0065 AUTHENTICATE_ONLY = 3,
0066 };
0067
0068 #define TB_SWITCH_KEY_SIZE 32
0069 #define TB_SWITCH_MAX_DEPTH 6
0070 #define USB4_SWITCH_MAX_DEPTH 5
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 enum tb_switch_tmu_rate {
0083 TB_SWITCH_TMU_RATE_OFF = 0,
0084 TB_SWITCH_TMU_RATE_HIFI = 16,
0085 TB_SWITCH_TMU_RATE_NORMAL = 1000,
0086 };
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104 struct tb_switch_tmu {
0105 int cap;
0106 bool has_ucap;
0107 enum tb_switch_tmu_rate rate;
0108 bool unidirectional;
0109 bool unidirectional_request;
0110 enum tb_switch_tmu_rate rate_request;
0111 };
0112
0113 enum tb_clx {
0114 TB_CLX_DISABLE,
0115
0116 TB_CL1,
0117 TB_CL2,
0118 };
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177 struct tb_switch {
0178 struct device dev;
0179 struct tb_regs_switch_header config;
0180 struct tb_port *ports;
0181 struct tb_dma_port *dma_port;
0182 struct tb_switch_tmu tmu;
0183 struct tb *tb;
0184 u64 uid;
0185 uuid_t *uuid;
0186 u16 vendor;
0187 u16 device;
0188 const char *vendor_name;
0189 const char *device_name;
0190 unsigned int link_speed;
0191 unsigned int link_width;
0192 bool link_usb4;
0193 unsigned int generation;
0194 int cap_plug_events;
0195 int cap_vsec_tmu;
0196 int cap_lc;
0197 int cap_lp;
0198 bool is_unplugged;
0199 u8 *drom;
0200 struct tb_nvm *nvm;
0201 bool no_nvm_upgrade;
0202 bool safe_mode;
0203 bool boot;
0204 bool rpm;
0205 unsigned int authorized;
0206 enum tb_security_level security_level;
0207 struct dentry *debugfs_dir;
0208 u8 *key;
0209 u8 connection_id;
0210 u8 connection_key;
0211 u8 link;
0212 u8 depth;
0213 struct completion rpm_complete;
0214 unsigned long quirks;
0215 bool credit_allocation;
0216 unsigned int max_usb3_credits;
0217 unsigned int min_dp_aux_credits;
0218 unsigned int min_dp_main_credits;
0219 unsigned int max_pcie_credits;
0220 unsigned int max_dma_credits;
0221 enum tb_clx clx;
0222 };
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 struct tb_port {
0253 struct tb_regs_port_header config;
0254 struct tb_switch *sw;
0255 struct tb_port *remote;
0256 struct tb_xdomain *xdomain;
0257 int cap_phy;
0258 int cap_tmu;
0259 int cap_adap;
0260 int cap_usb4;
0261 struct usb4_port *usb4;
0262 u8 port;
0263 bool disabled;
0264 bool bonded;
0265 struct tb_port *dual_link_port;
0266 u8 link_nr:1;
0267 struct ida in_hopids;
0268 struct ida out_hopids;
0269 struct list_head list;
0270 unsigned int total_credits;
0271 unsigned int ctl_credits;
0272 unsigned int dma_credits;
0273 };
0274
0275
0276
0277
0278
0279
0280
0281
0282
0283 struct usb4_port {
0284 struct device dev;
0285 struct tb_port *port;
0286 bool can_offline;
0287 bool offline;
0288 };
0289
0290
0291
0292
0293
0294
0295
0296
0297
0298
0299
0300
0301 struct tb_retimer {
0302 struct device dev;
0303 struct tb *tb;
0304 u8 index;
0305 u32 vendor;
0306 u32 device;
0307 struct tb_port *port;
0308 struct tb_nvm *nvm;
0309 u32 auth_status;
0310 };
0311
0312
0313
0314
0315
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336
0337
0338 struct tb_path_hop {
0339 struct tb_port *in_port;
0340 struct tb_port *out_port;
0341 int in_hop_index;
0342 int in_counter_index;
0343 int next_hop_index;
0344 unsigned int initial_credits;
0345 unsigned int nfc_credits;
0346 };
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356 enum tb_path_port {
0357 TB_PATH_NONE = 0,
0358 TB_PATH_SOURCE = 1,
0359 TB_PATH_INTERNAL = 2,
0360 TB_PATH_DESTINATION = 4,
0361 TB_PATH_ALL = 7,
0362 };
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386 struct tb_path {
0387 struct tb *tb;
0388 const char *name;
0389 enum tb_path_port ingress_shared_buffer;
0390 enum tb_path_port egress_shared_buffer;
0391 enum tb_path_port ingress_fc_enable;
0392 enum tb_path_port egress_fc_enable;
0393
0394 unsigned int priority:3;
0395 int weight:4;
0396 bool drop_packages;
0397 bool activated;
0398 bool clear_fc;
0399 struct tb_path_hop *hops;
0400 int path_length;
0401 bool alloc_hopid;
0402 };
0403
0404
0405 #define TB_PATH_MIN_HOPID 8
0406
0407
0408
0409
0410 #define TB_PATH_MAX_HOPS (7 * 2)
0411
0412
0413 #define TB_WAKE_ON_CONNECT BIT(0)
0414 #define TB_WAKE_ON_DISCONNECT BIT(1)
0415 #define TB_WAKE_ON_USB4 BIT(2)
0416 #define TB_WAKE_ON_USB3 BIT(3)
0417 #define TB_WAKE_ON_PCIE BIT(4)
0418 #define TB_WAKE_ON_DP BIT(5)
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428
0429
0430
0431
0432
0433
0434
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454
0455 struct tb_cm_ops {
0456 int (*driver_ready)(struct tb *tb);
0457 int (*start)(struct tb *tb);
0458 void (*stop)(struct tb *tb);
0459 int (*suspend_noirq)(struct tb *tb);
0460 int (*resume_noirq)(struct tb *tb);
0461 int (*suspend)(struct tb *tb);
0462 int (*freeze_noirq)(struct tb *tb);
0463 int (*thaw_noirq)(struct tb *tb);
0464 void (*complete)(struct tb *tb);
0465 int (*runtime_suspend)(struct tb *tb);
0466 int (*runtime_resume)(struct tb *tb);
0467 int (*runtime_suspend_switch)(struct tb_switch *sw);
0468 int (*runtime_resume_switch)(struct tb_switch *sw);
0469 void (*handle_event)(struct tb *tb, enum tb_cfg_pkg_type,
0470 const void *buf, size_t size);
0471 int (*get_boot_acl)(struct tb *tb, uuid_t *uuids, size_t nuuids);
0472 int (*set_boot_acl)(struct tb *tb, const uuid_t *uuids, size_t nuuids);
0473 int (*disapprove_switch)(struct tb *tb, struct tb_switch *sw);
0474 int (*approve_switch)(struct tb *tb, struct tb_switch *sw);
0475 int (*add_switch_key)(struct tb *tb, struct tb_switch *sw);
0476 int (*challenge_switch_key)(struct tb *tb, struct tb_switch *sw,
0477 const u8 *challenge, u8 *response);
0478 int (*disconnect_pcie_paths)(struct tb *tb);
0479 int (*approve_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
0480 int transmit_path, int transmit_ring,
0481 int receive_path, int receive_ring);
0482 int (*disconnect_xdomain_paths)(struct tb *tb, struct tb_xdomain *xd,
0483 int transmit_path, int transmit_ring,
0484 int receive_path, int receive_ring);
0485 int (*usb4_switch_op)(struct tb_switch *sw, u16 opcode, u32 *metadata,
0486 u8 *status, const void *tx_data, size_t tx_data_len,
0487 void *rx_data, size_t rx_data_len);
0488 int (*usb4_switch_nvm_authenticate_status)(struct tb_switch *sw,
0489 u32 *status);
0490 };
0491
0492 static inline void *tb_priv(struct tb *tb)
0493 {
0494 return (void *)tb->privdata;
0495 }
0496
0497 #define TB_AUTOSUSPEND_DELAY 15000
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507
0508
0509
0510
0511 static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
0512 {
0513 return &sw->ports[sw->config.upstream_port_number];
0514 }
0515
0516
0517
0518
0519
0520
0521
0522
0523 static inline bool tb_is_upstream_port(const struct tb_port *port)
0524 {
0525 const struct tb_port *upstream_port = tb_upstream_port(port->sw);
0526 return port == upstream_port || port->dual_link_port == upstream_port;
0527 }
0528
0529 static inline u64 tb_route(const struct tb_switch *sw)
0530 {
0531 return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
0532 }
0533
0534 static inline struct tb_port *tb_port_at(u64 route, struct tb_switch *sw)
0535 {
0536 u8 port;
0537
0538 port = route >> (sw->config.depth * 8);
0539 if (WARN_ON(port > sw->config.max_port_number))
0540 return NULL;
0541 return &sw->ports[port];
0542 }
0543
0544
0545
0546
0547
0548
0549
0550 static inline bool tb_port_has_remote(const struct tb_port *port)
0551 {
0552 if (tb_is_upstream_port(port))
0553 return false;
0554 if (!port->remote)
0555 return false;
0556 if (port->dual_link_port && port->link_nr)
0557 return false;
0558
0559 return true;
0560 }
0561
0562 static inline bool tb_port_is_null(const struct tb_port *port)
0563 {
0564 return port && port->port && port->config.type == TB_TYPE_PORT;
0565 }
0566
0567 static inline bool tb_port_is_nhi(const struct tb_port *port)
0568 {
0569 return port && port->config.type == TB_TYPE_NHI;
0570 }
0571
0572 static inline bool tb_port_is_pcie_down(const struct tb_port *port)
0573 {
0574 return port && port->config.type == TB_TYPE_PCIE_DOWN;
0575 }
0576
0577 static inline bool tb_port_is_pcie_up(const struct tb_port *port)
0578 {
0579 return port && port->config.type == TB_TYPE_PCIE_UP;
0580 }
0581
0582 static inline bool tb_port_is_dpin(const struct tb_port *port)
0583 {
0584 return port && port->config.type == TB_TYPE_DP_HDMI_IN;
0585 }
0586
0587 static inline bool tb_port_is_dpout(const struct tb_port *port)
0588 {
0589 return port && port->config.type == TB_TYPE_DP_HDMI_OUT;
0590 }
0591
0592 static inline bool tb_port_is_usb3_down(const struct tb_port *port)
0593 {
0594 return port && port->config.type == TB_TYPE_USB3_DOWN;
0595 }
0596
0597 static inline bool tb_port_is_usb3_up(const struct tb_port *port)
0598 {
0599 return port && port->config.type == TB_TYPE_USB3_UP;
0600 }
0601
0602 static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
0603 enum tb_cfg_space space, u32 offset, u32 length)
0604 {
0605 if (sw->is_unplugged)
0606 return -ENODEV;
0607 return tb_cfg_read(sw->tb->ctl,
0608 buffer,
0609 tb_route(sw),
0610 0,
0611 space,
0612 offset,
0613 length);
0614 }
0615
0616 static inline int tb_sw_write(struct tb_switch *sw, const void *buffer,
0617 enum tb_cfg_space space, u32 offset, u32 length)
0618 {
0619 if (sw->is_unplugged)
0620 return -ENODEV;
0621 return tb_cfg_write(sw->tb->ctl,
0622 buffer,
0623 tb_route(sw),
0624 0,
0625 space,
0626 offset,
0627 length);
0628 }
0629
0630 static inline int tb_port_read(struct tb_port *port, void *buffer,
0631 enum tb_cfg_space space, u32 offset, u32 length)
0632 {
0633 if (port->sw->is_unplugged)
0634 return -ENODEV;
0635 return tb_cfg_read(port->sw->tb->ctl,
0636 buffer,
0637 tb_route(port->sw),
0638 port->port,
0639 space,
0640 offset,
0641 length);
0642 }
0643
0644 static inline int tb_port_write(struct tb_port *port, const void *buffer,
0645 enum tb_cfg_space space, u32 offset, u32 length)
0646 {
0647 if (port->sw->is_unplugged)
0648 return -ENODEV;
0649 return tb_cfg_write(port->sw->tb->ctl,
0650 buffer,
0651 tb_route(port->sw),
0652 port->port,
0653 space,
0654 offset,
0655 length);
0656 }
0657
0658 #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg)
0659 #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg)
0660 #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg)
0661 #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg)
0662 #define tb_dbg(tb, fmt, arg...) dev_dbg(&(tb)->nhi->pdev->dev, fmt, ## arg)
0663
0664 #define __TB_SW_PRINT(level, sw, fmt, arg...) \
0665 do { \
0666 const struct tb_switch *__sw = (sw); \
0667 level(__sw->tb, "%llx: " fmt, \
0668 tb_route(__sw), ## arg); \
0669 } while (0)
0670 #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg)
0671 #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg)
0672 #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg)
0673 #define tb_sw_dbg(sw, fmt, arg...) __TB_SW_PRINT(tb_dbg, sw, fmt, ##arg)
0674
0675 #define __TB_PORT_PRINT(level, _port, fmt, arg...) \
0676 do { \
0677 const struct tb_port *__port = (_port); \
0678 level(__port->sw->tb, "%llx:%u: " fmt, \
0679 tb_route(__port->sw), __port->port, ## arg); \
0680 } while (0)
0681 #define tb_port_WARN(port, fmt, arg...) \
0682 __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg)
0683 #define tb_port_warn(port, fmt, arg...) \
0684 __TB_PORT_PRINT(tb_warn, port, fmt, ##arg)
0685 #define tb_port_info(port, fmt, arg...) \
0686 __TB_PORT_PRINT(tb_info, port, fmt, ##arg)
0687 #define tb_port_dbg(port, fmt, arg...) \
0688 __TB_PORT_PRINT(tb_dbg, port, fmt, ##arg)
0689
0690 struct tb *icm_probe(struct tb_nhi *nhi);
0691 struct tb *tb_probe(struct tb_nhi *nhi);
0692
0693 extern struct device_type tb_domain_type;
0694 extern struct device_type tb_retimer_type;
0695 extern struct device_type tb_switch_type;
0696 extern struct device_type usb4_port_device_type;
0697
0698 int tb_domain_init(void);
0699 void tb_domain_exit(void);
0700 int tb_xdomain_init(void);
0701 void tb_xdomain_exit(void);
0702
0703 struct tb *tb_domain_alloc(struct tb_nhi *nhi, int timeout_msec, size_t privsize);
0704 int tb_domain_add(struct tb *tb);
0705 void tb_domain_remove(struct tb *tb);
0706 int tb_domain_suspend_noirq(struct tb *tb);
0707 int tb_domain_resume_noirq(struct tb *tb);
0708 int tb_domain_suspend(struct tb *tb);
0709 int tb_domain_freeze_noirq(struct tb *tb);
0710 int tb_domain_thaw_noirq(struct tb *tb);
0711 void tb_domain_complete(struct tb *tb);
0712 int tb_domain_runtime_suspend(struct tb *tb);
0713 int tb_domain_runtime_resume(struct tb *tb);
0714 int tb_domain_disapprove_switch(struct tb *tb, struct tb_switch *sw);
0715 int tb_domain_approve_switch(struct tb *tb, struct tb_switch *sw);
0716 int tb_domain_approve_switch_key(struct tb *tb, struct tb_switch *sw);
0717 int tb_domain_challenge_switch_key(struct tb *tb, struct tb_switch *sw);
0718 int tb_domain_disconnect_pcie_paths(struct tb *tb);
0719 int tb_domain_approve_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
0720 int transmit_path, int transmit_ring,
0721 int receive_path, int receive_ring);
0722 int tb_domain_disconnect_xdomain_paths(struct tb *tb, struct tb_xdomain *xd,
0723 int transmit_path, int transmit_ring,
0724 int receive_path, int receive_ring);
0725 int tb_domain_disconnect_all_paths(struct tb *tb);
0726
0727 static inline struct tb *tb_domain_get(struct tb *tb)
0728 {
0729 if (tb)
0730 get_device(&tb->dev);
0731 return tb;
0732 }
0733
0734 static inline void tb_domain_put(struct tb *tb)
0735 {
0736 put_device(&tb->dev);
0737 }
0738
0739 struct tb_nvm *tb_nvm_alloc(struct device *dev);
0740 int tb_nvm_add_active(struct tb_nvm *nvm, size_t size, nvmem_reg_read_t reg_read);
0741 int tb_nvm_write_buf(struct tb_nvm *nvm, unsigned int offset, void *val,
0742 size_t bytes);
0743 int tb_nvm_add_non_active(struct tb_nvm *nvm, size_t size,
0744 nvmem_reg_write_t reg_write);
0745 void tb_nvm_free(struct tb_nvm *nvm);
0746 void tb_nvm_exit(void);
0747
0748 typedef int (*read_block_fn)(void *, unsigned int, void *, size_t);
0749 typedef int (*write_block_fn)(void *, unsigned int, const void *, size_t);
0750
0751 int tb_nvm_read_data(unsigned int address, void *buf, size_t size,
0752 unsigned int retries, read_block_fn read_block,
0753 void *read_block_data);
0754 int tb_nvm_write_data(unsigned int address, const void *buf, size_t size,
0755 unsigned int retries, write_block_fn write_next_block,
0756 void *write_block_data);
0757
0758 struct tb_switch *tb_switch_alloc(struct tb *tb, struct device *parent,
0759 u64 route);
0760 struct tb_switch *tb_switch_alloc_safe_mode(struct tb *tb,
0761 struct device *parent, u64 route);
0762 int tb_switch_configure(struct tb_switch *sw);
0763 int tb_switch_add(struct tb_switch *sw);
0764 void tb_switch_remove(struct tb_switch *sw);
0765 void tb_switch_suspend(struct tb_switch *sw, bool runtime);
0766 int tb_switch_resume(struct tb_switch *sw);
0767 int tb_switch_reset(struct tb_switch *sw);
0768 int tb_switch_wait_for_bit(struct tb_switch *sw, u32 offset, u32 bit,
0769 u32 value, int timeout_msec);
0770 void tb_sw_set_unplugged(struct tb_switch *sw);
0771 struct tb_port *tb_switch_find_port(struct tb_switch *sw,
0772 enum tb_port_type type);
0773 struct tb_switch *tb_switch_find_by_link_depth(struct tb *tb, u8 link,
0774 u8 depth);
0775 struct tb_switch *tb_switch_find_by_uuid(struct tb *tb, const uuid_t *uuid);
0776 struct tb_switch *tb_switch_find_by_route(struct tb *tb, u64 route);
0777
0778
0779
0780
0781
0782
0783
0784
0785 #define tb_switch_for_each_port(sw, p) \
0786 for ((p) = &(sw)->ports[1]; \
0787 (p) <= &(sw)->ports[(sw)->config.max_port_number]; (p)++)
0788
0789 static inline struct tb_switch *tb_switch_get(struct tb_switch *sw)
0790 {
0791 if (sw)
0792 get_device(&sw->dev);
0793 return sw;
0794 }
0795
0796 static inline void tb_switch_put(struct tb_switch *sw)
0797 {
0798 put_device(&sw->dev);
0799 }
0800
0801 static inline bool tb_is_switch(const struct device *dev)
0802 {
0803 return dev->type == &tb_switch_type;
0804 }
0805
0806 static inline struct tb_switch *tb_to_switch(struct device *dev)
0807 {
0808 if (tb_is_switch(dev))
0809 return container_of(dev, struct tb_switch, dev);
0810 return NULL;
0811 }
0812
0813 static inline struct tb_switch *tb_switch_parent(struct tb_switch *sw)
0814 {
0815 return tb_to_switch(sw->dev.parent);
0816 }
0817
0818 static inline bool tb_switch_is_light_ridge(const struct tb_switch *sw)
0819 {
0820 return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
0821 sw->config.device_id == PCI_DEVICE_ID_INTEL_LIGHT_RIDGE;
0822 }
0823
0824 static inline bool tb_switch_is_eagle_ridge(const struct tb_switch *sw)
0825 {
0826 return sw->config.vendor_id == PCI_VENDOR_ID_INTEL &&
0827 sw->config.device_id == PCI_DEVICE_ID_INTEL_EAGLE_RIDGE;
0828 }
0829
0830 static inline bool tb_switch_is_cactus_ridge(const struct tb_switch *sw)
0831 {
0832 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0833 switch (sw->config.device_id) {
0834 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_2C:
0835 case PCI_DEVICE_ID_INTEL_CACTUS_RIDGE_4C:
0836 return true;
0837 }
0838 }
0839 return false;
0840 }
0841
0842 static inline bool tb_switch_is_falcon_ridge(const struct tb_switch *sw)
0843 {
0844 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0845 switch (sw->config.device_id) {
0846 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_2C_BRIDGE:
0847 case PCI_DEVICE_ID_INTEL_FALCON_RIDGE_4C_BRIDGE:
0848 return true;
0849 }
0850 }
0851 return false;
0852 }
0853
0854 static inline bool tb_switch_is_alpine_ridge(const struct tb_switch *sw)
0855 {
0856 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0857 switch (sw->config.device_id) {
0858 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_2C_BRIDGE:
0859 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_4C_BRIDGE:
0860 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_LP_BRIDGE:
0861 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_4C_BRIDGE:
0862 case PCI_DEVICE_ID_INTEL_ALPINE_RIDGE_C_2C_BRIDGE:
0863 return true;
0864 }
0865 }
0866 return false;
0867 }
0868
0869 static inline bool tb_switch_is_titan_ridge(const struct tb_switch *sw)
0870 {
0871 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0872 switch (sw->config.device_id) {
0873 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_2C_BRIDGE:
0874 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_4C_BRIDGE:
0875 case PCI_DEVICE_ID_INTEL_TITAN_RIDGE_DD_BRIDGE:
0876 return true;
0877 }
0878 }
0879 return false;
0880 }
0881
0882 static inline bool tb_switch_is_tiger_lake(const struct tb_switch *sw)
0883 {
0884 if (sw->config.vendor_id == PCI_VENDOR_ID_INTEL) {
0885 switch (sw->config.device_id) {
0886 case PCI_DEVICE_ID_INTEL_TGL_NHI0:
0887 case PCI_DEVICE_ID_INTEL_TGL_NHI1:
0888 case PCI_DEVICE_ID_INTEL_TGL_H_NHI0:
0889 case PCI_DEVICE_ID_INTEL_TGL_H_NHI1:
0890 return true;
0891 }
0892 }
0893 return false;
0894 }
0895
0896
0897
0898
0899
0900
0901
0902 static inline bool tb_switch_is_usb4(const struct tb_switch *sw)
0903 {
0904 return sw->config.thunderbolt_version == USB4_VERSION_1_0;
0905 }
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916 static inline bool tb_switch_is_icm(const struct tb_switch *sw)
0917 {
0918 return !sw->config.enabled;
0919 }
0920
0921 int tb_switch_lane_bonding_enable(struct tb_switch *sw);
0922 void tb_switch_lane_bonding_disable(struct tb_switch *sw);
0923 int tb_switch_configure_link(struct tb_switch *sw);
0924 void tb_switch_unconfigure_link(struct tb_switch *sw);
0925
0926 bool tb_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
0927 int tb_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
0928 void tb_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
0929
0930 int tb_switch_tmu_init(struct tb_switch *sw);
0931 int tb_switch_tmu_post_time(struct tb_switch *sw);
0932 int tb_switch_tmu_disable(struct tb_switch *sw);
0933 int tb_switch_tmu_enable(struct tb_switch *sw);
0934 void tb_switch_tmu_configure(struct tb_switch *sw,
0935 enum tb_switch_tmu_rate rate,
0936 bool unidirectional);
0937 void tb_switch_enable_tmu_1st_child(struct tb_switch *sw,
0938 enum tb_switch_tmu_rate rate);
0939
0940
0941
0942
0943
0944
0945
0946
0947 static inline bool tb_switch_tmu_is_enabled(const struct tb_switch *sw,
0948 bool unidirectional)
0949 {
0950 return sw->tmu.rate == sw->tmu.rate_request &&
0951 sw->tmu.unidirectional == unidirectional;
0952 }
0953
0954 static inline const char *tb_switch_clx_name(enum tb_clx clx)
0955 {
0956 switch (clx) {
0957
0958 case TB_CL1:
0959 return "CL0s/CL1";
0960 default:
0961 return "unknown";
0962 }
0963 }
0964
0965 int tb_switch_enable_clx(struct tb_switch *sw, enum tb_clx clx);
0966 int tb_switch_disable_clx(struct tb_switch *sw, enum tb_clx clx);
0967
0968
0969
0970
0971
0972
0973
0974
0975
0976 static inline bool tb_switch_is_clx_enabled(const struct tb_switch *sw,
0977 enum tb_clx clx)
0978 {
0979 return sw->clx == clx;
0980 }
0981
0982
0983
0984
0985
0986 static inline bool tb_switch_is_clx_supported(const struct tb_switch *sw)
0987 {
0988 return tb_switch_is_usb4(sw) || tb_switch_is_titan_ridge(sw);
0989 }
0990
0991 int tb_switch_mask_clx_objections(struct tb_switch *sw);
0992
0993 int tb_switch_pcie_l1_enable(struct tb_switch *sw);
0994
0995 int tb_switch_xhci_connect(struct tb_switch *sw);
0996 void tb_switch_xhci_disconnect(struct tb_switch *sw);
0997
0998 int tb_port_state(struct tb_port *port);
0999 int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged);
1000 int tb_port_add_nfc_credits(struct tb_port *port, int credits);
1001 int tb_port_clear_counter(struct tb_port *port, int counter);
1002 int tb_port_unlock(struct tb_port *port);
1003 int tb_port_enable(struct tb_port *port);
1004 int tb_port_disable(struct tb_port *port);
1005 int tb_port_alloc_in_hopid(struct tb_port *port, int hopid, int max_hopid);
1006 void tb_port_release_in_hopid(struct tb_port *port, int hopid);
1007 int tb_port_alloc_out_hopid(struct tb_port *port, int hopid, int max_hopid);
1008 void tb_port_release_out_hopid(struct tb_port *port, int hopid);
1009 struct tb_port *tb_next_port_on_path(struct tb_port *start, struct tb_port *end,
1010 struct tb_port *prev);
1011
1012 static inline bool tb_port_use_credit_allocation(const struct tb_port *port)
1013 {
1014 return tb_port_is_null(port) && port->sw->credit_allocation;
1015 }
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025 #define tb_for_each_port_on_path(src, dst, p) \
1026 for ((p) = tb_next_port_on_path((src), (dst), NULL); (p); \
1027 (p) = tb_next_port_on_path((src), (dst), (p)))
1028
1029 int tb_port_get_link_speed(struct tb_port *port);
1030 int tb_port_get_link_width(struct tb_port *port);
1031 int tb_port_set_link_width(struct tb_port *port, unsigned int width);
1032 int tb_port_set_lane_bonding(struct tb_port *port, bool bonding);
1033 int tb_port_lane_bonding_enable(struct tb_port *port);
1034 void tb_port_lane_bonding_disable(struct tb_port *port);
1035 int tb_port_wait_for_link_width(struct tb_port *port, int width,
1036 int timeout_msec);
1037 int tb_port_update_credits(struct tb_port *port);
1038
1039 int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec);
1040 int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap);
1041 int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset);
1042 int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap);
1043 int tb_port_next_cap(struct tb_port *port, unsigned int offset);
1044 bool tb_port_is_enabled(struct tb_port *port);
1045
1046 bool tb_usb3_port_is_enabled(struct tb_port *port);
1047 int tb_usb3_port_enable(struct tb_port *port, bool enable);
1048
1049 bool tb_pci_port_is_enabled(struct tb_port *port);
1050 int tb_pci_port_enable(struct tb_port *port, bool enable);
1051
1052 int tb_dp_port_hpd_is_active(struct tb_port *port);
1053 int tb_dp_port_hpd_clear(struct tb_port *port);
1054 int tb_dp_port_set_hops(struct tb_port *port, unsigned int video,
1055 unsigned int aux_tx, unsigned int aux_rx);
1056 bool tb_dp_port_is_enabled(struct tb_port *port);
1057 int tb_dp_port_enable(struct tb_port *port, bool enable);
1058
1059 struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
1060 struct tb_port *dst, int dst_hopid,
1061 struct tb_port **last, const char *name,
1062 bool alloc_hopid);
1063 struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
1064 struct tb_port *dst, int dst_hopid, int link_nr,
1065 const char *name);
1066 void tb_path_free(struct tb_path *path);
1067 int tb_path_activate(struct tb_path *path);
1068 void tb_path_deactivate(struct tb_path *path);
1069 bool tb_path_is_invalid(struct tb_path *path);
1070 bool tb_path_port_on_path(const struct tb_path *path,
1071 const struct tb_port *port);
1072
1073
1074
1075
1076
1077
1078
1079
1080 #define tb_path_for_each_hop(path, hop) \
1081 for ((hop) = &(path)->hops[0]; \
1082 (hop) <= &(path)->hops[(path)->path_length - 1]; (hop)++)
1083
1084 int tb_drom_read(struct tb_switch *sw);
1085 int tb_drom_read_uid_only(struct tb_switch *sw, u64 *uid);
1086
1087 int tb_lc_read_uuid(struct tb_switch *sw, u32 *uuid);
1088 int tb_lc_configure_port(struct tb_port *port);
1089 void tb_lc_unconfigure_port(struct tb_port *port);
1090 int tb_lc_configure_xdomain(struct tb_port *port);
1091 void tb_lc_unconfigure_xdomain(struct tb_port *port);
1092 int tb_lc_start_lane_initialization(struct tb_port *port);
1093 bool tb_lc_is_clx_supported(struct tb_port *port);
1094 bool tb_lc_is_usb_plugged(struct tb_port *port);
1095 bool tb_lc_is_xhci_connected(struct tb_port *port);
1096 int tb_lc_xhci_connect(struct tb_port *port);
1097 void tb_lc_xhci_disconnect(struct tb_port *port);
1098 int tb_lc_set_wake(struct tb_switch *sw, unsigned int flags);
1099 int tb_lc_set_sleep(struct tb_switch *sw);
1100 bool tb_lc_lane_bonding_possible(struct tb_switch *sw);
1101 bool tb_lc_dp_sink_query(struct tb_switch *sw, struct tb_port *in);
1102 int tb_lc_dp_sink_alloc(struct tb_switch *sw, struct tb_port *in);
1103 int tb_lc_dp_sink_dealloc(struct tb_switch *sw, struct tb_port *in);
1104 int tb_lc_force_power(struct tb_switch *sw);
1105
1106 static inline int tb_route_length(u64 route)
1107 {
1108 return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
1109 }
1110
1111
1112
1113
1114
1115
1116
1117
1118 static inline u64 tb_downstream_route(struct tb_port *port)
1119 {
1120 return tb_route(port->sw)
1121 | ((u64) port->port << (port->sw->config.depth * 8));
1122 }
1123
1124 bool tb_is_xdomain_enabled(void);
1125 bool tb_xdomain_handle_request(struct tb *tb, enum tb_cfg_pkg_type type,
1126 const void *buf, size_t size);
1127 struct tb_xdomain *tb_xdomain_alloc(struct tb *tb, struct device *parent,
1128 u64 route, const uuid_t *local_uuid,
1129 const uuid_t *remote_uuid);
1130 void tb_xdomain_add(struct tb_xdomain *xd);
1131 void tb_xdomain_remove(struct tb_xdomain *xd);
1132 struct tb_xdomain *tb_xdomain_find_by_link_depth(struct tb *tb, u8 link,
1133 u8 depth);
1134
1135 int tb_retimer_scan(struct tb_port *port, bool add);
1136 void tb_retimer_remove_all(struct tb_port *port);
1137
1138 static inline bool tb_is_retimer(const struct device *dev)
1139 {
1140 return dev->type == &tb_retimer_type;
1141 }
1142
1143 static inline struct tb_retimer *tb_to_retimer(struct device *dev)
1144 {
1145 if (tb_is_retimer(dev))
1146 return container_of(dev, struct tb_retimer, dev);
1147 return NULL;
1148 }
1149
1150 int usb4_switch_setup(struct tb_switch *sw);
1151 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid);
1152 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
1153 size_t size);
1154 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw);
1155 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags);
1156 int usb4_switch_set_sleep(struct tb_switch *sw);
1157 int usb4_switch_nvm_sector_size(struct tb_switch *sw);
1158 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
1159 size_t size);
1160 int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address);
1161 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
1162 const void *buf, size_t size);
1163 int usb4_switch_nvm_authenticate(struct tb_switch *sw);
1164 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status);
1165 int usb4_switch_credits_init(struct tb_switch *sw);
1166 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in);
1167 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
1168 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in);
1169 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
1170 const struct tb_port *port);
1171 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
1172 const struct tb_port *port);
1173 int usb4_switch_add_ports(struct tb_switch *sw);
1174 void usb4_switch_remove_ports(struct tb_switch *sw);
1175
1176 int usb4_port_unlock(struct tb_port *port);
1177 int usb4_port_configure(struct tb_port *port);
1178 void usb4_port_unconfigure(struct tb_port *port);
1179 int usb4_port_configure_xdomain(struct tb_port *port);
1180 void usb4_port_unconfigure_xdomain(struct tb_port *port);
1181 int usb4_port_router_offline(struct tb_port *port);
1182 int usb4_port_router_online(struct tb_port *port);
1183 int usb4_port_enumerate_retimers(struct tb_port *port);
1184 bool usb4_port_clx_supported(struct tb_port *port);
1185
1186 int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index);
1187 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1188 u8 size);
1189 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1190 const void *buf, u8 size);
1191 int usb4_port_retimer_is_last(struct tb_port *port, u8 index);
1192 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index);
1193 int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1194 unsigned int address);
1195 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index,
1196 unsigned int address, const void *buf,
1197 size_t size);
1198 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index);
1199 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1200 u32 *status);
1201 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
1202 unsigned int address, void *buf, size_t size);
1203
1204 int usb4_usb3_port_max_link_rate(struct tb_port *port);
1205 int usb4_usb3_port_actual_link_rate(struct tb_port *port);
1206 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
1207 int *downstream_bw);
1208 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
1209 int *downstream_bw);
1210 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
1211 int *downstream_bw);
1212
1213 static inline bool tb_is_usb4_port_device(const struct device *dev)
1214 {
1215 return dev->type == &usb4_port_device_type;
1216 }
1217
1218 static inline struct usb4_port *tb_to_usb4_port_device(struct device *dev)
1219 {
1220 if (tb_is_usb4_port_device(dev))
1221 return container_of(dev, struct usb4_port, dev);
1222 return NULL;
1223 }
1224
1225 struct usb4_port *usb4_port_device_add(struct tb_port *port);
1226 void usb4_port_device_remove(struct usb4_port *usb4);
1227 int usb4_port_device_resume(struct usb4_port *usb4);
1228
1229
1230 #define QUIRK_FORCE_POWER_LINK_CONTROLLER BIT(0)
1231
1232 void tb_check_quirks(struct tb_switch *sw);
1233
1234 #ifdef CONFIG_ACPI
1235 void tb_acpi_add_links(struct tb_nhi *nhi);
1236
1237 bool tb_acpi_is_native(void);
1238 bool tb_acpi_may_tunnel_usb3(void);
1239 bool tb_acpi_may_tunnel_dp(void);
1240 bool tb_acpi_may_tunnel_pcie(void);
1241 bool tb_acpi_is_xdomain_allowed(void);
1242
1243 int tb_acpi_init(void);
1244 void tb_acpi_exit(void);
1245 int tb_acpi_power_on_retimers(struct tb_port *port);
1246 int tb_acpi_power_off_retimers(struct tb_port *port);
1247 #else
1248 static inline void tb_acpi_add_links(struct tb_nhi *nhi) { }
1249
1250 static inline bool tb_acpi_is_native(void) { return true; }
1251 static inline bool tb_acpi_may_tunnel_usb3(void) { return true; }
1252 static inline bool tb_acpi_may_tunnel_dp(void) { return true; }
1253 static inline bool tb_acpi_may_tunnel_pcie(void) { return true; }
1254 static inline bool tb_acpi_is_xdomain_allowed(void) { return true; }
1255
1256 static inline int tb_acpi_init(void) { return 0; }
1257 static inline void tb_acpi_exit(void) { }
1258 static inline int tb_acpi_power_on_retimers(struct tb_port *port) { return 0; }
1259 static inline int tb_acpi_power_off_retimers(struct tb_port *port) { return 0; }
1260 #endif
1261
1262 #ifdef CONFIG_DEBUG_FS
1263 void tb_debugfs_init(void);
1264 void tb_debugfs_exit(void);
1265 void tb_switch_debugfs_init(struct tb_switch *sw);
1266 void tb_switch_debugfs_remove(struct tb_switch *sw);
1267 void tb_service_debugfs_init(struct tb_service *svc);
1268 void tb_service_debugfs_remove(struct tb_service *svc);
1269 #else
1270 static inline void tb_debugfs_init(void) { }
1271 static inline void tb_debugfs_exit(void) { }
1272 static inline void tb_switch_debugfs_init(struct tb_switch *sw) { }
1273 static inline void tb_switch_debugfs_remove(struct tb_switch *sw) { }
1274 static inline void tb_service_debugfs_init(struct tb_service *svc) { }
1275 static inline void tb_service_debugfs_remove(struct tb_service *svc) { }
1276 #endif
1277
1278 #endif