1 // SPDX-License-Identifier: GPL-2.0
3 * USB4 specific functionality
5 * Copyright (C) 2019, Intel Corporation
6 * Authors: Mika Westerberg <mika.westerberg@linux.intel.com>
7 * Rajmohan Mani <rajmohan.mani@intel.com>
10 #include <linux/delay.h>
11 #include <linux/ktime.h>
12 #include <linux/units.h>
17 #define USB4_DATA_RETRIES 3
20 USB4_SB_TARGET_ROUTER,
21 USB4_SB_TARGET_PARTNER,
22 USB4_SB_TARGET_RETIMER,
25 #define USB4_NVM_READ_OFFSET_MASK GENMASK(23, 2)
26 #define USB4_NVM_READ_OFFSET_SHIFT 2
27 #define USB4_NVM_READ_LENGTH_MASK GENMASK(27, 24)
28 #define USB4_NVM_READ_LENGTH_SHIFT 24
30 #define USB4_NVM_SET_OFFSET_MASK USB4_NVM_READ_OFFSET_MASK
31 #define USB4_NVM_SET_OFFSET_SHIFT USB4_NVM_READ_OFFSET_SHIFT
33 #define USB4_DROM_ADDRESS_MASK GENMASK(14, 2)
34 #define USB4_DROM_ADDRESS_SHIFT 2
35 #define USB4_DROM_SIZE_MASK GENMASK(19, 15)
36 #define USB4_DROM_SIZE_SHIFT 15
38 #define USB4_NVM_SECTOR_SIZE_MASK GENMASK(23, 0)
40 #define USB4_BA_LENGTH_MASK GENMASK(7, 0)
41 #define USB4_BA_INDEX_MASK GENMASK(15, 0)
44 USB4_BA_MAX_USB3 = 0x1,
45 USB4_BA_MIN_DP_AUX = 0x2,
46 USB4_BA_MIN_DP_MAIN = 0x3,
47 USB4_BA_MAX_PCIE = 0x4,
51 #define USB4_BA_VALUE_MASK GENMASK(31, 16)
52 #define USB4_BA_VALUE_SHIFT 16
54 static int usb4_native_switch_op(struct tb_switch *sw, u16 opcode,
55 u32 *metadata, u8 *status,
56 const void *tx_data, size_t tx_dwords,
57 void *rx_data, size_t rx_dwords)
63 ret = tb_sw_write(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
68 ret = tb_sw_write(sw, tx_data, TB_CFG_SWITCH, ROUTER_CS_9,
74 val = opcode | ROUTER_CS_26_OV;
75 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
79 ret = tb_switch_wait_for_bit(sw, ROUTER_CS_26, ROUTER_CS_26_OV, 0, 500);
83 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
87 if (val & ROUTER_CS_26_ONS)
91 *status = (val & ROUTER_CS_26_STATUS_MASK) >>
92 ROUTER_CS_26_STATUS_SHIFT;
95 ret = tb_sw_read(sw, metadata, TB_CFG_SWITCH, ROUTER_CS_25, 1);
100 ret = tb_sw_read(sw, rx_data, TB_CFG_SWITCH, ROUTER_CS_9,
109 static int __usb4_switch_op(struct tb_switch *sw, u16 opcode, u32 *metadata,
110 u8 *status, const void *tx_data, size_t tx_dwords,
111 void *rx_data, size_t rx_dwords)
113 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
115 if (tx_dwords > NVM_DATA_DWORDS || rx_dwords > NVM_DATA_DWORDS)
119 * If the connection manager implementation provides USB4 router
120 * operation proxy callback, call it here instead of running the
121 * operation natively.
123 if (cm_ops->usb4_switch_op) {
126 ret = cm_ops->usb4_switch_op(sw, opcode, metadata, status,
127 tx_data, tx_dwords, rx_data,
129 if (ret != -EOPNOTSUPP)
133 * If the proxy was not supported then run the native
134 * router operation instead.
138 return usb4_native_switch_op(sw, opcode, metadata, status, tx_data,
139 tx_dwords, rx_data, rx_dwords);
142 static inline int usb4_switch_op(struct tb_switch *sw, u16 opcode,
143 u32 *metadata, u8 *status)
145 return __usb4_switch_op(sw, opcode, metadata, status, NULL, 0, NULL, 0);
148 static inline int usb4_switch_op_data(struct tb_switch *sw, u16 opcode,
149 u32 *metadata, u8 *status,
150 const void *tx_data, size_t tx_dwords,
151 void *rx_data, size_t rx_dwords)
153 return __usb4_switch_op(sw, opcode, metadata, status, tx_data,
154 tx_dwords, rx_data, rx_dwords);
157 static void usb4_switch_check_wakes(struct tb_switch *sw)
159 bool wakeup_usb4 = false;
160 struct usb4_port *usb4;
161 struct tb_port *port;
165 if (!device_may_wakeup(&sw->dev))
169 if (tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1))
172 tb_sw_dbg(sw, "PCIe wake: %s, USB3 wake: %s\n",
173 (val & ROUTER_CS_6_WOPS) ? "yes" : "no",
174 (val & ROUTER_CS_6_WOUS) ? "yes" : "no");
176 wakeup = val & (ROUTER_CS_6_WOPS | ROUTER_CS_6_WOUS);
180 * Check for any downstream ports for USB4 wake,
181 * connection wake and disconnection wake.
183 tb_switch_for_each_port(sw, port) {
187 if (tb_port_read(port, &val, TB_CFG_PORT,
188 port->cap_usb4 + PORT_CS_18, 1))
191 tb_port_dbg(port, "USB4 wake: %s, connection wake: %s, disconnection wake: %s\n",
192 (val & PORT_CS_18_WOU4S) ? "yes" : "no",
193 (val & PORT_CS_18_WOCS) ? "yes" : "no",
194 (val & PORT_CS_18_WODS) ? "yes" : "no");
196 wakeup_usb4 = val & (PORT_CS_18_WOU4S | PORT_CS_18_WOCS |
200 if (device_may_wakeup(&usb4->dev) && wakeup_usb4)
201 pm_wakeup_event(&usb4->dev, 0);
203 wakeup |= wakeup_usb4;
207 pm_wakeup_event(&sw->dev, 0);
210 static bool link_is_usb4(struct tb_port *port)
217 if (tb_port_read(port, &val, TB_CFG_PORT,
218 port->cap_usb4 + PORT_CS_18, 1))
221 return !(val & PORT_CS_18_TCM);
225 * usb4_switch_setup() - Additional setup for USB4 device
226 * @sw: USB4 router to setup
228 * USB4 routers need additional settings in order to enable all the
229 * tunneling. This function enables USB and PCIe tunneling if it can be
230 * enabled (e.g the parent switch also supports them). If USB tunneling
231 * is not available for some reason (like that there is Thunderbolt 3
232 * switch upstream) then the internal xHCI controller is enabled
235 int usb4_switch_setup(struct tb_switch *sw)
237 struct tb_port *downstream_port;
238 struct tb_switch *parent;
243 usb4_switch_check_wakes(sw);
248 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_6, 1);
252 parent = tb_switch_parent(sw);
253 downstream_port = tb_port_at(tb_route(sw), parent);
254 sw->link_usb4 = link_is_usb4(downstream_port);
255 tb_sw_dbg(sw, "link: %s\n", sw->link_usb4 ? "USB4" : "TBT");
257 xhci = val & ROUTER_CS_6_HCI;
258 tbt3 = !(val & ROUTER_CS_6_TNS);
260 tb_sw_dbg(sw, "TBT3 support: %s, xHCI: %s\n",
261 tbt3 ? "yes" : "no", xhci ? "yes" : "no");
263 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
267 if (tb_acpi_may_tunnel_usb3() && sw->link_usb4 &&
268 tb_switch_find_port(parent, TB_TYPE_USB3_DOWN)) {
269 val |= ROUTER_CS_5_UTO;
274 * Only enable PCIe tunneling if the parent router supports it
275 * and it is not disabled.
277 if (tb_acpi_may_tunnel_pcie() &&
278 tb_switch_find_port(parent, TB_TYPE_PCIE_DOWN)) {
279 val |= ROUTER_CS_5_PTO;
281 * xHCI can be enabled if PCIe tunneling is supported
282 * and the parent does not have any USB3 dowstream
283 * adapters (so we cannot do USB 3.x tunneling).
286 val |= ROUTER_CS_5_HCO;
289 /* TBT3 supported by the CM */
290 val |= ROUTER_CS_5_C3S;
291 /* Tunneling configuration is ready now */
292 val |= ROUTER_CS_5_CV;
294 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
298 return tb_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_CR,
303 * usb4_switch_read_uid() - Read UID from USB4 router
305 * @uid: UID is stored here
307 * Reads 64-bit UID from USB4 router config space.
309 int usb4_switch_read_uid(struct tb_switch *sw, u64 *uid)
311 return tb_sw_read(sw, uid, TB_CFG_SWITCH, ROUTER_CS_7, 2);
314 static int usb4_switch_drom_read_block(void *data,
315 unsigned int dwaddress, void *buf,
318 struct tb_switch *sw = data;
323 metadata = (dwords << USB4_DROM_SIZE_SHIFT) & USB4_DROM_SIZE_MASK;
324 metadata |= (dwaddress << USB4_DROM_ADDRESS_SHIFT) &
325 USB4_DROM_ADDRESS_MASK;
327 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_DROM_READ, &metadata,
328 &status, NULL, 0, buf, dwords);
332 return status ? -EIO : 0;
336 * usb4_switch_drom_read() - Read arbitrary bytes from USB4 router DROM
338 * @address: Byte address inside DROM to start reading
339 * @buf: Buffer where the DROM content is stored
340 * @size: Number of bytes to read from DROM
342 * Uses USB4 router operations to read router DROM. For devices this
343 * should always work but for hosts it may return %-EOPNOTSUPP in which
344 * case the host router does not have DROM.
346 int usb4_switch_drom_read(struct tb_switch *sw, unsigned int address, void *buf,
349 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
350 usb4_switch_drom_read_block, sw);
354 * usb4_switch_lane_bonding_possible() - Are conditions met for lane bonding
357 * Checks whether conditions are met so that lane bonding can be
358 * established with the upstream router. Call only for device routers.
360 bool usb4_switch_lane_bonding_possible(struct tb_switch *sw)
366 up = tb_upstream_port(sw);
367 ret = tb_port_read(up, &val, TB_CFG_PORT, up->cap_usb4 + PORT_CS_18, 1);
371 return !!(val & PORT_CS_18_BE);
375 * usb4_switch_set_wake() - Enabled/disable wake
377 * @flags: Wakeup flags (%0 to disable)
379 * Enables/disables router to wake up from sleep.
381 int usb4_switch_set_wake(struct tb_switch *sw, unsigned int flags)
383 struct usb4_port *usb4;
384 struct tb_port *port;
385 u64 route = tb_route(sw);
390 * Enable wakes coming from all USB4 downstream ports (from
391 * child routers). For device routers do this also for the
392 * upstream USB4 port.
394 tb_switch_for_each_port(sw, port) {
395 if (!tb_port_is_null(port))
397 if (!route && tb_is_upstream_port(port))
402 ret = tb_port_read(port, &val, TB_CFG_PORT,
403 port->cap_usb4 + PORT_CS_19, 1);
407 val &= ~(PORT_CS_19_WOC | PORT_CS_19_WOD | PORT_CS_19_WOU4);
409 if (tb_is_upstream_port(port)) {
410 val |= PORT_CS_19_WOU4;
412 bool configured = val & PORT_CS_19_PC;
415 if (((flags & TB_WAKE_ON_CONNECT) |
416 device_may_wakeup(&usb4->dev)) && !configured)
417 val |= PORT_CS_19_WOC;
418 if (((flags & TB_WAKE_ON_DISCONNECT) |
419 device_may_wakeup(&usb4->dev)) && configured)
420 val |= PORT_CS_19_WOD;
421 if ((flags & TB_WAKE_ON_USB4) && configured)
422 val |= PORT_CS_19_WOU4;
425 ret = tb_port_write(port, &val, TB_CFG_PORT,
426 port->cap_usb4 + PORT_CS_19, 1);
432 * Enable wakes from PCIe, USB 3.x and DP on this router. Only
433 * needed for device routers.
436 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
440 val &= ~(ROUTER_CS_5_WOP | ROUTER_CS_5_WOU | ROUTER_CS_5_WOD);
441 if (flags & TB_WAKE_ON_USB3)
442 val |= ROUTER_CS_5_WOU;
443 if (flags & TB_WAKE_ON_PCIE)
444 val |= ROUTER_CS_5_WOP;
445 if (flags & TB_WAKE_ON_DP)
446 val |= ROUTER_CS_5_WOD;
448 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
457 * usb4_switch_set_sleep() - Prepare the router to enter sleep
460 * Sets sleep bit for the router. Returns when the router sleep ready
461 * bit has been asserted.
463 int usb4_switch_set_sleep(struct tb_switch *sw)
468 /* Set sleep bit and wait for sleep ready to be asserted */
469 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
473 val |= ROUTER_CS_5_SLP;
475 ret = tb_sw_write(sw, &val, TB_CFG_SWITCH, ROUTER_CS_5, 1);
479 return tb_switch_wait_for_bit(sw, ROUTER_CS_6, ROUTER_CS_6_SLPR,
480 ROUTER_CS_6_SLPR, 500);
484 * usb4_switch_nvm_sector_size() - Return router NVM sector size
487 * If the router supports NVM operations this function returns the NVM
488 * sector size in bytes. If NVM operations are not supported returns
491 int usb4_switch_nvm_sector_size(struct tb_switch *sw)
497 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SECTOR_SIZE, &metadata,
503 return status == 0x2 ? -EOPNOTSUPP : -EIO;
505 return metadata & USB4_NVM_SECTOR_SIZE_MASK;
508 static int usb4_switch_nvm_read_block(void *data,
509 unsigned int dwaddress, void *buf, size_t dwords)
511 struct tb_switch *sw = data;
516 metadata = (dwords << USB4_NVM_READ_LENGTH_SHIFT) &
517 USB4_NVM_READ_LENGTH_MASK;
518 metadata |= (dwaddress << USB4_NVM_READ_OFFSET_SHIFT) &
519 USB4_NVM_READ_OFFSET_MASK;
521 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_READ, &metadata,
522 &status, NULL, 0, buf, dwords);
526 return status ? -EIO : 0;
530 * usb4_switch_nvm_read() - Read arbitrary bytes from router NVM
532 * @address: Starting address in bytes
533 * @buf: Read data is placed here
534 * @size: How many bytes to read
536 * Reads NVM contents of the router. If NVM is not supported returns
539 int usb4_switch_nvm_read(struct tb_switch *sw, unsigned int address, void *buf,
542 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
543 usb4_switch_nvm_read_block, sw);
547 * usb4_switch_nvm_set_offset() - Set NVM write offset
549 * @address: Start offset
551 * Explicitly sets NVM write offset. Normally when writing to NVM this
552 * is done automatically by usb4_switch_nvm_write().
554 * Returns %0 in success and negative errno if there was a failure.
556 int usb4_switch_nvm_set_offset(struct tb_switch *sw, unsigned int address)
558 u32 metadata, dwaddress;
562 dwaddress = address / 4;
563 metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
564 USB4_NVM_SET_OFFSET_MASK;
566 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_SET_OFFSET, &metadata,
571 return status ? -EIO : 0;
574 static int usb4_switch_nvm_write_next_block(void *data, unsigned int dwaddress,
575 const void *buf, size_t dwords)
577 struct tb_switch *sw = data;
581 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_NVM_WRITE, NULL, &status,
582 buf, dwords, NULL, 0);
586 return status ? -EIO : 0;
590 * usb4_switch_nvm_write() - Write to the router NVM
592 * @address: Start address where to write in bytes
593 * @buf: Pointer to the data to write
594 * @size: Size of @buf in bytes
596 * Writes @buf to the router NVM using USB4 router operations. If NVM
597 * write is not supported returns %-EOPNOTSUPP.
599 int usb4_switch_nvm_write(struct tb_switch *sw, unsigned int address,
600 const void *buf, size_t size)
604 ret = usb4_switch_nvm_set_offset(sw, address);
608 return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
609 usb4_switch_nvm_write_next_block, sw);
613 * usb4_switch_nvm_authenticate() - Authenticate new NVM
616 * After the new NVM has been written via usb4_switch_nvm_write(), this
617 * function triggers NVM authentication process. The router gets power
618 * cycled and if the authentication is successful the new NVM starts
619 * running. In case of failure returns negative errno.
621 * The caller should call usb4_switch_nvm_authenticate_status() to read
622 * the status of the authentication after power cycle. It should be the
623 * first router operation to avoid the status being lost.
625 int usb4_switch_nvm_authenticate(struct tb_switch *sw)
629 ret = usb4_switch_op(sw, USB4_SWITCH_OP_NVM_AUTH, NULL, NULL);
632 * The router is power cycled once NVM_AUTH is started so it is
633 * expected to get any of the following errors back.
646 * usb4_switch_nvm_authenticate_status() - Read status of last NVM authenticate
648 * @status: Status code of the operation
650 * The function checks if there is status available from the last NVM
651 * authenticate router operation. If there is status then %0 is returned
652 * and the status code is placed in @status. Returns negative errno in case
655 * Must be called before any other router operation.
657 int usb4_switch_nvm_authenticate_status(struct tb_switch *sw, u32 *status)
659 const struct tb_cm_ops *cm_ops = sw->tb->cm_ops;
664 if (cm_ops->usb4_switch_nvm_authenticate_status) {
665 ret = cm_ops->usb4_switch_nvm_authenticate_status(sw, status);
666 if (ret != -EOPNOTSUPP)
670 ret = tb_sw_read(sw, &val, TB_CFG_SWITCH, ROUTER_CS_26, 1);
674 /* Check that the opcode is correct */
675 opcode = val & ROUTER_CS_26_OPCODE_MASK;
676 if (opcode == USB4_SWITCH_OP_NVM_AUTH) {
677 if (val & ROUTER_CS_26_OV)
679 if (val & ROUTER_CS_26_ONS)
682 *status = (val & ROUTER_CS_26_STATUS_MASK) >>
683 ROUTER_CS_26_STATUS_SHIFT;
692 * usb4_switch_credits_init() - Read buffer allocation parameters
695 * Reads @sw buffer allocation parameters and initializes @sw buffer
696 * allocation fields accordingly. Specifically @sw->credits_allocation
697 * is set to %true if these parameters can be used in tunneling.
699 * Returns %0 on success and negative errno otherwise.
701 int usb4_switch_credits_init(struct tb_switch *sw)
703 int max_usb3, min_dp_aux, min_dp_main, max_pcie, max_dma;
704 int ret, length, i, nports;
705 const struct tb_port *port;
706 u32 data[NVM_DATA_DWORDS];
710 memset(data, 0, sizeof(data));
711 ret = usb4_switch_op_data(sw, USB4_SWITCH_OP_BUFFER_ALLOC, &metadata,
712 &status, NULL, 0, data, ARRAY_SIZE(data));
718 length = metadata & USB4_BA_LENGTH_MASK;
719 if (WARN_ON(length > ARRAY_SIZE(data)))
728 tb_sw_dbg(sw, "credit allocation parameters:\n");
730 for (i = 0; i < length; i++) {
733 index = data[i] & USB4_BA_INDEX_MASK;
734 value = (data[i] & USB4_BA_VALUE_MASK) >> USB4_BA_VALUE_SHIFT;
737 case USB4_BA_MAX_USB3:
738 tb_sw_dbg(sw, " USB3: %u\n", value);
741 case USB4_BA_MIN_DP_AUX:
742 tb_sw_dbg(sw, " DP AUX: %u\n", value);
745 case USB4_BA_MIN_DP_MAIN:
746 tb_sw_dbg(sw, " DP main: %u\n", value);
749 case USB4_BA_MAX_PCIE:
750 tb_sw_dbg(sw, " PCIe: %u\n", value);
754 tb_sw_dbg(sw, " DMA: %u\n", value);
758 tb_sw_dbg(sw, " unknown credit allocation index %#x, skipping\n",
765 * Validate the buffer allocation preferences. If we find
766 * issues, log a warning and fall back using the hard-coded
770 /* Host router must report baMaxHI */
771 if (!tb_route(sw) && max_dma < 0) {
772 tb_sw_warn(sw, "host router is missing baMaxHI\n");
777 tb_switch_for_each_port(sw, port) {
778 if (tb_port_is_null(port))
782 /* Must have DP buffer allocation (multiple USB4 ports) */
783 if (nports > 2 && (min_dp_aux < 0 || min_dp_main < 0)) {
784 tb_sw_warn(sw, "multiple USB4 ports require baMinDPaux/baMinDPmain\n");
788 tb_switch_for_each_port(sw, port) {
789 if (tb_port_is_dpout(port) && min_dp_main < 0) {
790 tb_sw_warn(sw, "missing baMinDPmain");
793 if ((tb_port_is_dpin(port) || tb_port_is_dpout(port)) &&
795 tb_sw_warn(sw, "missing baMinDPaux");
798 if ((tb_port_is_usb3_down(port) || tb_port_is_usb3_up(port)) &&
800 tb_sw_warn(sw, "missing baMaxUSB3");
803 if ((tb_port_is_pcie_down(port) || tb_port_is_pcie_up(port)) &&
805 tb_sw_warn(sw, "missing baMaxPCIe");
811 * Buffer allocation passed the validation so we can use it in
814 sw->credit_allocation = true;
816 sw->max_usb3_credits = max_usb3;
818 sw->min_dp_aux_credits = min_dp_aux;
820 sw->min_dp_main_credits = min_dp_main;
822 sw->max_pcie_credits = max_pcie;
824 sw->max_dma_credits = max_dma;
833 * usb4_switch_query_dp_resource() - Query availability of DP IN resource
837 * For DP tunneling this function can be used to query availability of
838 * DP IN resource. Returns true if the resource is available for DP
839 * tunneling, false otherwise.
841 bool usb4_switch_query_dp_resource(struct tb_switch *sw, struct tb_port *in)
843 u32 metadata = in->port;
847 ret = usb4_switch_op(sw, USB4_SWITCH_OP_QUERY_DP_RESOURCE, &metadata,
850 * If DP resource allocation is not supported assume it is
853 if (ret == -EOPNOTSUPP)
862 * usb4_switch_alloc_dp_resource() - Allocate DP IN resource
866 * Allocates DP IN resource for DP tunneling using USB4 router
867 * operations. If the resource was allocated returns %0. Otherwise
868 * returns negative errno, in particular %-EBUSY if the resource is
871 int usb4_switch_alloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
873 u32 metadata = in->port;
877 ret = usb4_switch_op(sw, USB4_SWITCH_OP_ALLOC_DP_RESOURCE, &metadata,
879 if (ret == -EOPNOTSUPP)
884 return status ? -EBUSY : 0;
888 * usb4_switch_dealloc_dp_resource() - Releases allocated DP IN resource
892 * Releases the previously allocated DP IN resource.
894 int usb4_switch_dealloc_dp_resource(struct tb_switch *sw, struct tb_port *in)
896 u32 metadata = in->port;
900 ret = usb4_switch_op(sw, USB4_SWITCH_OP_DEALLOC_DP_RESOURCE, &metadata,
902 if (ret == -EOPNOTSUPP)
907 return status ? -EIO : 0;
910 static int usb4_port_idx(const struct tb_switch *sw, const struct tb_port *port)
915 /* Assume port is primary */
916 tb_switch_for_each_port(sw, p) {
917 if (!tb_port_is_null(p))
919 if (tb_is_upstream_port(p))
932 * usb4_switch_map_pcie_down() - Map USB4 port to a PCIe downstream adapter
936 * USB4 routers have direct mapping between USB4 ports and PCIe
937 * downstream adapters where the PCIe topology is extended. This
938 * function returns the corresponding downstream PCIe adapter or %NULL
939 * if no such mapping was possible.
941 struct tb_port *usb4_switch_map_pcie_down(struct tb_switch *sw,
942 const struct tb_port *port)
944 int usb4_idx = usb4_port_idx(sw, port);
948 /* Find PCIe down port matching usb4_port */
949 tb_switch_for_each_port(sw, p) {
950 if (!tb_port_is_pcie_down(p))
953 if (pcie_idx == usb4_idx)
963 * usb4_switch_map_usb3_down() - Map USB4 port to a USB3 downstream adapter
967 * USB4 routers have direct mapping between USB4 ports and USB 3.x
968 * downstream adapters where the USB 3.x topology is extended. This
969 * function returns the corresponding downstream USB 3.x adapter or
970 * %NULL if no such mapping was possible.
972 struct tb_port *usb4_switch_map_usb3_down(struct tb_switch *sw,
973 const struct tb_port *port)
975 int usb4_idx = usb4_port_idx(sw, port);
979 /* Find USB3 down port matching usb4_port */
980 tb_switch_for_each_port(sw, p) {
981 if (!tb_port_is_usb3_down(p))
984 if (usb_idx == usb4_idx)
994 * usb4_switch_add_ports() - Add USB4 ports for this router
997 * For USB4 router finds all USB4 ports and registers devices for each.
998 * Can be called to any router.
1000 * Return %0 in case of success and negative errno in case of failure.
1002 int usb4_switch_add_ports(struct tb_switch *sw)
1004 struct tb_port *port;
1006 if (tb_switch_is_icm(sw) || !tb_switch_is_usb4(sw))
1009 tb_switch_for_each_port(sw, port) {
1010 struct usb4_port *usb4;
1012 if (!tb_port_is_null(port))
1014 if (!port->cap_usb4)
1017 usb4 = usb4_port_device_add(port);
1019 usb4_switch_remove_ports(sw);
1020 return PTR_ERR(usb4);
1030 * usb4_switch_remove_ports() - Removes USB4 ports from this router
1033 * Unregisters previously registered USB4 ports.
1035 void usb4_switch_remove_ports(struct tb_switch *sw)
1037 struct tb_port *port;
1039 tb_switch_for_each_port(sw, port) {
1041 usb4_port_device_remove(port->usb4);
1048 * usb4_port_unlock() - Unlock USB4 downstream port
1049 * @port: USB4 port to unlock
1051 * Unlocks USB4 downstream port so that the connection manager can
1052 * access the router below this port.
1054 int usb4_port_unlock(struct tb_port *port)
1059 ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
1063 val &= ~ADP_CS_4_LCK;
1064 return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_4, 1);
1068 * usb4_port_hotplug_enable() - Enables hotplug for a port
1069 * @port: USB4 port to operate on
1071 * Enables hot plug events on a given port. This is only intended
1072 * to be used on lane, DP-IN, and DP-OUT adapters.
1074 int usb4_port_hotplug_enable(struct tb_port *port)
1079 ret = tb_port_read(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
1083 val &= ~ADP_CS_5_DHP;
1084 return tb_port_write(port, &val, TB_CFG_PORT, ADP_CS_5, 1);
1087 static int usb4_port_set_configured(struct tb_port *port, bool configured)
1092 if (!port->cap_usb4)
1095 ret = tb_port_read(port, &val, TB_CFG_PORT,
1096 port->cap_usb4 + PORT_CS_19, 1);
1101 val |= PORT_CS_19_PC;
1103 val &= ~PORT_CS_19_PC;
1105 return tb_port_write(port, &val, TB_CFG_PORT,
1106 port->cap_usb4 + PORT_CS_19, 1);
1110 * usb4_port_configure() - Set USB4 port configured
1111 * @port: USB4 router
1113 * Sets the USB4 link to be configured for power management purposes.
1115 int usb4_port_configure(struct tb_port *port)
1117 return usb4_port_set_configured(port, true);
1121 * usb4_port_unconfigure() - Set USB4 port unconfigured
1122 * @port: USB4 router
1124 * Sets the USB4 link to be unconfigured for power management purposes.
1126 void usb4_port_unconfigure(struct tb_port *port)
1128 usb4_port_set_configured(port, false);
1131 static int usb4_set_xdomain_configured(struct tb_port *port, bool configured)
1136 if (!port->cap_usb4)
1139 ret = tb_port_read(port, &val, TB_CFG_PORT,
1140 port->cap_usb4 + PORT_CS_19, 1);
1145 val |= PORT_CS_19_PID;
1147 val &= ~PORT_CS_19_PID;
1149 return tb_port_write(port, &val, TB_CFG_PORT,
1150 port->cap_usb4 + PORT_CS_19, 1);
1154 * usb4_port_configure_xdomain() - Configure port for XDomain
1155 * @port: USB4 port connected to another host
1156 * @xd: XDomain that is connected to the port
1158 * Marks the USB4 port as being connected to another host and updates
1159 * the link type. Returns %0 in success and negative errno in failure.
1161 int usb4_port_configure_xdomain(struct tb_port *port, struct tb_xdomain *xd)
1163 xd->link_usb4 = link_is_usb4(port);
1164 return usb4_set_xdomain_configured(port, true);
1168 * usb4_port_unconfigure_xdomain() - Unconfigure port for XDomain
1169 * @port: USB4 port that was connected to another host
1171 * Clears USB4 port from being marked as XDomain.
1173 void usb4_port_unconfigure_xdomain(struct tb_port *port)
1175 usb4_set_xdomain_configured(port, false);
1178 static int usb4_port_wait_for_bit(struct tb_port *port, u32 offset, u32 bit,
1179 u32 value, int timeout_msec)
1181 ktime_t timeout = ktime_add_ms(ktime_get(), timeout_msec);
1187 ret = tb_port_read(port, &val, TB_CFG_PORT, offset, 1);
1191 if ((val & bit) == value)
1194 usleep_range(50, 100);
1195 } while (ktime_before(ktime_get(), timeout));
1200 static int usb4_port_read_data(struct tb_port *port, void *data, size_t dwords)
1202 if (dwords > NVM_DATA_DWORDS)
1205 return tb_port_read(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
1209 static int usb4_port_write_data(struct tb_port *port, const void *data,
1212 if (dwords > NVM_DATA_DWORDS)
1215 return tb_port_write(port, data, TB_CFG_PORT, port->cap_usb4 + PORT_CS_2,
1219 static int usb4_port_sb_read(struct tb_port *port, enum usb4_sb_target target,
1220 u8 index, u8 reg, void *buf, u8 size)
1222 size_t dwords = DIV_ROUND_UP(size, 4);
1226 if (!port->cap_usb4)
1230 val |= size << PORT_CS_1_LENGTH_SHIFT;
1231 val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1232 if (target == USB4_SB_TARGET_RETIMER)
1233 val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1234 val |= PORT_CS_1_PND;
1236 ret = tb_port_write(port, &val, TB_CFG_PORT,
1237 port->cap_usb4 + PORT_CS_1, 1);
1241 ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1242 PORT_CS_1_PND, 0, 500);
1246 ret = tb_port_read(port, &val, TB_CFG_PORT,
1247 port->cap_usb4 + PORT_CS_1, 1);
1251 if (val & PORT_CS_1_NR)
1253 if (val & PORT_CS_1_RC)
1256 return buf ? usb4_port_read_data(port, buf, dwords) : 0;
1259 static int usb4_port_sb_write(struct tb_port *port, enum usb4_sb_target target,
1260 u8 index, u8 reg, const void *buf, u8 size)
1262 size_t dwords = DIV_ROUND_UP(size, 4);
1266 if (!port->cap_usb4)
1270 ret = usb4_port_write_data(port, buf, dwords);
1276 val |= size << PORT_CS_1_LENGTH_SHIFT;
1277 val |= PORT_CS_1_WNR_WRITE;
1278 val |= (target << PORT_CS_1_TARGET_SHIFT) & PORT_CS_1_TARGET_MASK;
1279 if (target == USB4_SB_TARGET_RETIMER)
1280 val |= (index << PORT_CS_1_RETIMER_INDEX_SHIFT);
1281 val |= PORT_CS_1_PND;
1283 ret = tb_port_write(port, &val, TB_CFG_PORT,
1284 port->cap_usb4 + PORT_CS_1, 1);
1288 ret = usb4_port_wait_for_bit(port, port->cap_usb4 + PORT_CS_1,
1289 PORT_CS_1_PND, 0, 500);
1293 ret = tb_port_read(port, &val, TB_CFG_PORT,
1294 port->cap_usb4 + PORT_CS_1, 1);
1298 if (val & PORT_CS_1_NR)
1300 if (val & PORT_CS_1_RC)
1306 static int usb4_port_sb_opcode_err_to_errno(u32 val)
1311 case USB4_SB_OPCODE_ERR:
1313 case USB4_SB_OPCODE_ONS:
1320 static int usb4_port_sb_op(struct tb_port *port, enum usb4_sb_target target,
1321 u8 index, enum usb4_sb_opcode opcode, int timeout_msec)
1328 ret = usb4_port_sb_write(port, target, index, USB4_SB_OPCODE, &val,
1333 timeout = ktime_add_ms(ktime_get(), timeout_msec);
1337 ret = usb4_port_sb_read(port, target, index, USB4_SB_OPCODE,
1343 return usb4_port_sb_opcode_err_to_errno(val);
1344 } while (ktime_before(ktime_get(), timeout));
1349 static int usb4_port_set_router_offline(struct tb_port *port, bool offline)
1354 ret = usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1355 USB4_SB_METADATA, &val, sizeof(val));
1359 val = USB4_SB_OPCODE_ROUTER_OFFLINE;
1360 return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1361 USB4_SB_OPCODE, &val, sizeof(val));
1365 * usb4_port_router_offline() - Put the USB4 port to offline mode
1368 * This function puts the USB4 port into offline mode. In this mode the
1369 * port does not react on hotplug events anymore. This needs to be
1370 * called before retimer access is done when the USB4 links is not up.
1372 * Returns %0 in case of success and negative errno if there was an
1375 int usb4_port_router_offline(struct tb_port *port)
1377 return usb4_port_set_router_offline(port, true);
1381 * usb4_port_router_online() - Put the USB4 port back to online
1384 * Makes the USB4 port functional again.
1386 int usb4_port_router_online(struct tb_port *port)
1388 return usb4_port_set_router_offline(port, false);
1392 * usb4_port_enumerate_retimers() - Send RT broadcast transaction
1395 * This forces the USB4 port to send broadcast RT transaction which
1396 * makes the retimers on the link to assign index to themselves. Returns
1397 * %0 in case of success and negative errno if there was an error.
1399 int usb4_port_enumerate_retimers(struct tb_port *port)
1403 val = USB4_SB_OPCODE_ENUMERATE_RETIMERS;
1404 return usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1405 USB4_SB_OPCODE, &val, sizeof(val));
1409 * usb4_port_clx_supported() - Check if CLx is supported by the link
1410 * @port: Port to check for CLx support for
1412 * PORT_CS_18_CPS bit reflects if the link supports CLx including
1413 * active cables (if connected on the link).
1415 bool usb4_port_clx_supported(struct tb_port *port)
1420 ret = tb_port_read(port, &val, TB_CFG_PORT,
1421 port->cap_usb4 + PORT_CS_18, 1);
1425 return !!(val & PORT_CS_18_CPS);
1429 * usb4_port_margining_caps() - Read USB4 port marginig capabilities
1431 * @caps: Array with at least two elements to hold the results
1433 * Reads the USB4 port lane margining capabilities into @caps.
1435 int usb4_port_margining_caps(struct tb_port *port, u32 *caps)
1439 ret = usb4_port_sb_op(port, USB4_SB_TARGET_ROUTER, 0,
1440 USB4_SB_OPCODE_READ_LANE_MARGINING_CAP, 500);
1444 return usb4_port_sb_read(port, USB4_SB_TARGET_ROUTER, 0,
1445 USB4_SB_DATA, caps, sizeof(*caps) * 2);
1449 * usb4_port_hw_margin() - Run hardware lane margining on port
1451 * @lanes: Which lanes to run (must match the port capabilities). Can be
1453 * @ber_level: BER level contour value
1454 * @timing: Perform timing margining instead of voltage
1455 * @right_high: Use Right/high margin instead of left/low
1456 * @results: Array with at least two elements to hold the results
1458 * Runs hardware lane margining on USB4 port and returns the result in
1461 int usb4_port_hw_margin(struct tb_port *port, unsigned int lanes,
1462 unsigned int ber_level, bool timing, bool right_high,
1470 val |= USB4_MARGIN_HW_TIME;
1472 val |= USB4_MARGIN_HW_RH;
1474 val |= (ber_level << USB4_MARGIN_HW_BER_SHIFT) &
1475 USB4_MARGIN_HW_BER_MASK;
1477 ret = usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1478 USB4_SB_METADATA, &val, sizeof(val));
1482 ret = usb4_port_sb_op(port, USB4_SB_TARGET_ROUTER, 0,
1483 USB4_SB_OPCODE_RUN_HW_LANE_MARGINING, 2500);
1487 return usb4_port_sb_read(port, USB4_SB_TARGET_ROUTER, 0,
1488 USB4_SB_DATA, results, sizeof(*results) * 2);
1492 * usb4_port_sw_margin() - Run software lane margining on port
1494 * @lanes: Which lanes to run (must match the port capabilities). Can be
1496 * @timing: Perform timing margining instead of voltage
1497 * @right_high: Use Right/high margin instead of left/low
1498 * @counter: What to do with the error counter
1500 * Runs software lane margining on USB4 port. Read back the error
1501 * counters by calling usb4_port_sw_margin_errors(). Returns %0 in
1502 * success and negative errno otherwise.
1504 int usb4_port_sw_margin(struct tb_port *port, unsigned int lanes, bool timing,
1505 bool right_high, u32 counter)
1512 val |= USB4_MARGIN_SW_TIME;
1514 val |= USB4_MARGIN_SW_RH;
1515 val |= (counter << USB4_MARGIN_SW_COUNTER_SHIFT) &
1516 USB4_MARGIN_SW_COUNTER_MASK;
1518 ret = usb4_port_sb_write(port, USB4_SB_TARGET_ROUTER, 0,
1519 USB4_SB_METADATA, &val, sizeof(val));
1523 return usb4_port_sb_op(port, USB4_SB_TARGET_ROUTER, 0,
1524 USB4_SB_OPCODE_RUN_SW_LANE_MARGINING, 2500);
1528 * usb4_port_sw_margin_errors() - Read the software margining error counters
1530 * @errors: Error metadata is copied here.
1532 * This reads back the software margining error counters from the port.
1533 * Returns %0 in success and negative errno otherwise.
1535 int usb4_port_sw_margin_errors(struct tb_port *port, u32 *errors)
1539 ret = usb4_port_sb_op(port, USB4_SB_TARGET_ROUTER, 0,
1540 USB4_SB_OPCODE_READ_SW_MARGIN_ERR, 150);
1544 return usb4_port_sb_read(port, USB4_SB_TARGET_ROUTER, 0,
1545 USB4_SB_METADATA, errors, sizeof(*errors));
1548 static inline int usb4_port_retimer_op(struct tb_port *port, u8 index,
1549 enum usb4_sb_opcode opcode,
1552 return usb4_port_sb_op(port, USB4_SB_TARGET_RETIMER, index, opcode,
1557 * usb4_port_retimer_set_inbound_sbtx() - Enable sideband channel transactions
1559 * @index: Retimer index
1561 * Enables sideband channel transations on SBTX. Can be used when USB4
1562 * link does not go up, for example if there is no device connected.
1564 int usb4_port_retimer_set_inbound_sbtx(struct tb_port *port, u8 index)
1568 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_SET_INBOUND_SBTX,
1575 * Per the USB4 retimer spec, the retimer is not required to
1576 * send an RT (Retimer Transaction) response for the first
1577 * SET_INBOUND_SBTX command
1579 return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_SET_INBOUND_SBTX,
1584 * usb4_port_retimer_read() - Read from retimer sideband registers
1586 * @index: Retimer index
1587 * @reg: Sideband register to read
1588 * @buf: Data from @reg is stored here
1589 * @size: Number of bytes to read
1591 * Function reads retimer sideband registers starting from @reg. The
1592 * retimer is connected to @port at @index. Returns %0 in case of
1593 * success, and read data is copied to @buf. If there is no retimer
1594 * present at given @index returns %-ENODEV. In any other failure
1595 * returns negative errno.
1597 int usb4_port_retimer_read(struct tb_port *port, u8 index, u8 reg, void *buf,
1600 return usb4_port_sb_read(port, USB4_SB_TARGET_RETIMER, index, reg, buf,
1605 * usb4_port_retimer_write() - Write to retimer sideband registers
1607 * @index: Retimer index
1608 * @reg: Sideband register to write
1609 * @buf: Data that is written starting from @reg
1610 * @size: Number of bytes to write
1612 * Writes retimer sideband registers starting from @reg. The retimer is
1613 * connected to @port at @index. Returns %0 in case of success. If there
1614 * is no retimer present at given @index returns %-ENODEV. In any other
1615 * failure returns negative errno.
1617 int usb4_port_retimer_write(struct tb_port *port, u8 index, u8 reg,
1618 const void *buf, u8 size)
1620 return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index, reg, buf,
1625 * usb4_port_retimer_is_last() - Is the retimer last on-board retimer
1627 * @index: Retimer index
1629 * If the retimer at @index is last one (connected directly to the
1630 * Type-C port) this function returns %1. If it is not returns %0. If
1631 * the retimer is not present returns %-ENODEV. Otherwise returns
1634 int usb4_port_retimer_is_last(struct tb_port *port, u8 index)
1639 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_QUERY_LAST_RETIMER,
1644 ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA, &metadata,
1646 return ret ? ret : metadata & 1;
1650 * usb4_port_retimer_nvm_sector_size() - Read retimer NVM sector size
1652 * @index: Retimer index
1654 * Reads NVM sector size (in bytes) of a retimer at @index. This
1655 * operation can be used to determine whether the retimer supports NVM
1656 * upgrade for example. Returns sector size in bytes or negative errno
1657 * in case of error. Specifically returns %-ENODEV if there is no
1658 * retimer at @index.
1660 int usb4_port_retimer_nvm_sector_size(struct tb_port *port, u8 index)
1665 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_GET_NVM_SECTOR_SIZE,
1670 ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA, &metadata,
1672 return ret ? ret : metadata & USB4_NVM_SECTOR_SIZE_MASK;
1676 * usb4_port_retimer_nvm_set_offset() - Set NVM write offset
1678 * @index: Retimer index
1679 * @address: Start offset
1681 * Exlicitly sets NVM write offset. Normally when writing to NVM this is
1682 * done automatically by usb4_port_retimer_nvm_write().
1684 * Returns %0 in success and negative errno if there was a failure.
1686 int usb4_port_retimer_nvm_set_offset(struct tb_port *port, u8 index,
1687 unsigned int address)
1689 u32 metadata, dwaddress;
1692 dwaddress = address / 4;
1693 metadata = (dwaddress << USB4_NVM_SET_OFFSET_SHIFT) &
1694 USB4_NVM_SET_OFFSET_MASK;
1696 ret = usb4_port_retimer_write(port, index, USB4_SB_METADATA, &metadata,
1701 return usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_SET_OFFSET,
1705 struct retimer_info {
1706 struct tb_port *port;
1710 static int usb4_port_retimer_nvm_write_next_block(void *data,
1711 unsigned int dwaddress, const void *buf, size_t dwords)
1714 const struct retimer_info *info = data;
1715 struct tb_port *port = info->port;
1716 u8 index = info->index;
1719 ret = usb4_port_retimer_write(port, index, USB4_SB_DATA,
1724 return usb4_port_retimer_op(port, index,
1725 USB4_SB_OPCODE_NVM_BLOCK_WRITE, 1000);
1729 * usb4_port_retimer_nvm_write() - Write to retimer NVM
1731 * @index: Retimer index
1732 * @address: Byte address where to start the write
1733 * @buf: Data to write
1734 * @size: Size in bytes how much to write
1736 * Writes @size bytes from @buf to the retimer NVM. Used for NVM
1737 * upgrade. Returns %0 if the data was written successfully and negative
1738 * errno in case of failure. Specifically returns %-ENODEV if there is
1739 * no retimer at @index.
1741 int usb4_port_retimer_nvm_write(struct tb_port *port, u8 index, unsigned int address,
1742 const void *buf, size_t size)
1744 struct retimer_info info = { .port = port, .index = index };
1747 ret = usb4_port_retimer_nvm_set_offset(port, index, address);
1751 return tb_nvm_write_data(address, buf, size, USB4_DATA_RETRIES,
1752 usb4_port_retimer_nvm_write_next_block, &info);
1756 * usb4_port_retimer_nvm_authenticate() - Start retimer NVM upgrade
1758 * @index: Retimer index
1760 * After the new NVM image has been written via usb4_port_retimer_nvm_write()
1761 * this function can be used to trigger the NVM upgrade process. If
1762 * successful the retimer restarts with the new NVM and may not have the
1763 * index set so one needs to call usb4_port_enumerate_retimers() to
1764 * force index to be assigned.
1766 int usb4_port_retimer_nvm_authenticate(struct tb_port *port, u8 index)
1771 * We need to use the raw operation here because once the
1772 * authentication completes the retimer index is not set anymore
1773 * so we do not get back the status now.
1775 val = USB4_SB_OPCODE_NVM_AUTH_WRITE;
1776 return usb4_port_sb_write(port, USB4_SB_TARGET_RETIMER, index,
1777 USB4_SB_OPCODE, &val, sizeof(val));
1781 * usb4_port_retimer_nvm_authenticate_status() - Read status of NVM upgrade
1783 * @index: Retimer index
1784 * @status: Raw status code read from metadata
1786 * This can be called after usb4_port_retimer_nvm_authenticate() and
1787 * usb4_port_enumerate_retimers() to fetch status of the NVM upgrade.
1789 * Returns %0 if the authentication status was successfully read. The
1790 * completion metadata (the result) is then stored into @status. If
1791 * reading the status fails, returns negative errno.
1793 int usb4_port_retimer_nvm_authenticate_status(struct tb_port *port, u8 index,
1799 ret = usb4_port_retimer_read(port, index, USB4_SB_OPCODE, &val,
1804 ret = usb4_port_sb_opcode_err_to_errno(val);
1811 ret = usb4_port_retimer_read(port, index, USB4_SB_METADATA,
1812 &metadata, sizeof(metadata));
1816 *status = metadata & USB4_SB_METADATA_NVM_AUTH_WRITE_MASK;
1824 static int usb4_port_retimer_nvm_read_block(void *data, unsigned int dwaddress,
1825 void *buf, size_t dwords)
1827 const struct retimer_info *info = data;
1828 struct tb_port *port = info->port;
1829 u8 index = info->index;
1833 metadata = dwaddress << USB4_NVM_READ_OFFSET_SHIFT;
1834 if (dwords < NVM_DATA_DWORDS)
1835 metadata |= dwords << USB4_NVM_READ_LENGTH_SHIFT;
1837 ret = usb4_port_retimer_write(port, index, USB4_SB_METADATA, &metadata,
1842 ret = usb4_port_retimer_op(port, index, USB4_SB_OPCODE_NVM_READ, 500);
1846 return usb4_port_retimer_read(port, index, USB4_SB_DATA, buf,
1851 * usb4_port_retimer_nvm_read() - Read contents of retimer NVM
1853 * @index: Retimer index
1854 * @address: NVM address (in bytes) to start reading
1855 * @buf: Data read from NVM is stored here
1856 * @size: Number of bytes to read
1858 * Reads retimer NVM and copies the contents to @buf. Returns %0 if the
1859 * read was successful and negative errno in case of failure.
1860 * Specifically returns %-ENODEV if there is no retimer at @index.
1862 int usb4_port_retimer_nvm_read(struct tb_port *port, u8 index,
1863 unsigned int address, void *buf, size_t size)
1865 struct retimer_info info = { .port = port, .index = index };
1867 return tb_nvm_read_data(address, buf, size, USB4_DATA_RETRIES,
1868 usb4_port_retimer_nvm_read_block, &info);
1872 * usb4_usb3_port_max_link_rate() - Maximum support USB3 link rate
1873 * @port: USB3 adapter port
1875 * Return maximum supported link rate of a USB3 adapter in Mb/s.
1876 * Negative errno in case of error.
1878 int usb4_usb3_port_max_link_rate(struct tb_port *port)
1883 if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
1886 ret = tb_port_read(port, &val, TB_CFG_PORT,
1887 port->cap_adap + ADP_USB3_CS_4, 1);
1891 lr = (val & ADP_USB3_CS_4_MSLR_MASK) >> ADP_USB3_CS_4_MSLR_SHIFT;
1892 return lr == ADP_USB3_CS_4_MSLR_20G ? 20000 : 10000;
1896 * usb4_usb3_port_actual_link_rate() - Established USB3 link rate
1897 * @port: USB3 adapter port
1899 * Return actual established link rate of a USB3 adapter in Mb/s. If the
1900 * link is not up returns %0 and negative errno in case of failure.
1902 int usb4_usb3_port_actual_link_rate(struct tb_port *port)
1907 if (!tb_port_is_usb3_down(port) && !tb_port_is_usb3_up(port))
1910 ret = tb_port_read(port, &val, TB_CFG_PORT,
1911 port->cap_adap + ADP_USB3_CS_4, 1);
1915 if (!(val & ADP_USB3_CS_4_ULV))
1918 lr = val & ADP_USB3_CS_4_ALR_MASK;
1919 return lr == ADP_USB3_CS_4_ALR_20G ? 20000 : 10000;
1922 static int usb4_usb3_port_cm_request(struct tb_port *port, bool request)
1927 if (!tb_port_is_usb3_down(port))
1929 if (tb_route(port->sw))
1932 ret = tb_port_read(port, &val, TB_CFG_PORT,
1933 port->cap_adap + ADP_USB3_CS_2, 1);
1938 val |= ADP_USB3_CS_2_CMR;
1940 val &= ~ADP_USB3_CS_2_CMR;
1942 ret = tb_port_write(port, &val, TB_CFG_PORT,
1943 port->cap_adap + ADP_USB3_CS_2, 1);
1948 * We can use val here directly as the CMR bit is in the same place
1949 * as HCA. Just mask out others.
1951 val &= ADP_USB3_CS_2_CMR;
1952 return usb4_port_wait_for_bit(port, port->cap_adap + ADP_USB3_CS_1,
1953 ADP_USB3_CS_1_HCA, val, 1500);
1956 static inline int usb4_usb3_port_set_cm_request(struct tb_port *port)
1958 return usb4_usb3_port_cm_request(port, true);
1961 static inline int usb4_usb3_port_clear_cm_request(struct tb_port *port)
1963 return usb4_usb3_port_cm_request(port, false);
1966 static unsigned int usb3_bw_to_mbps(u32 bw, u8 scale)
1968 unsigned long uframes;
1970 uframes = bw * 512UL << scale;
1971 return DIV_ROUND_CLOSEST(uframes * 8000, MEGA);
1974 static u32 mbps_to_usb3_bw(unsigned int mbps, u8 scale)
1976 unsigned long uframes;
1978 /* 1 uframe is 1/8 ms (125 us) -> 1 / 8000 s */
1979 uframes = ((unsigned long)mbps * MEGA) / 8000;
1980 return DIV_ROUND_UP(uframes, 512UL << scale);
1983 static int usb4_usb3_port_read_allocated_bandwidth(struct tb_port *port,
1990 ret = tb_port_read(port, &val, TB_CFG_PORT,
1991 port->cap_adap + ADP_USB3_CS_2, 1);
1995 ret = tb_port_read(port, &scale, TB_CFG_PORT,
1996 port->cap_adap + ADP_USB3_CS_3, 1);
2000 scale &= ADP_USB3_CS_3_SCALE_MASK;
2002 bw = val & ADP_USB3_CS_2_AUBW_MASK;
2003 *upstream_bw = usb3_bw_to_mbps(bw, scale);
2005 bw = (val & ADP_USB3_CS_2_ADBW_MASK) >> ADP_USB3_CS_2_ADBW_SHIFT;
2006 *downstream_bw = usb3_bw_to_mbps(bw, scale);
2012 * usb4_usb3_port_allocated_bandwidth() - Bandwidth allocated for USB3
2013 * @port: USB3 adapter port
2014 * @upstream_bw: Allocated upstream bandwidth is stored here
2015 * @downstream_bw: Allocated downstream bandwidth is stored here
2017 * Stores currently allocated USB3 bandwidth into @upstream_bw and
2018 * @downstream_bw in Mb/s. Returns %0 in case of success and negative
2021 int usb4_usb3_port_allocated_bandwidth(struct tb_port *port, int *upstream_bw,
2026 ret = usb4_usb3_port_set_cm_request(port);
2030 ret = usb4_usb3_port_read_allocated_bandwidth(port, upstream_bw,
2032 usb4_usb3_port_clear_cm_request(port);
2037 static int usb4_usb3_port_read_consumed_bandwidth(struct tb_port *port,
2044 ret = tb_port_read(port, &val, TB_CFG_PORT,
2045 port->cap_adap + ADP_USB3_CS_1, 1);
2049 ret = tb_port_read(port, &scale, TB_CFG_PORT,
2050 port->cap_adap + ADP_USB3_CS_3, 1);
2054 scale &= ADP_USB3_CS_3_SCALE_MASK;
2056 bw = val & ADP_USB3_CS_1_CUBW_MASK;
2057 *upstream_bw = usb3_bw_to_mbps(bw, scale);
2059 bw = (val & ADP_USB3_CS_1_CDBW_MASK) >> ADP_USB3_CS_1_CDBW_SHIFT;
2060 *downstream_bw = usb3_bw_to_mbps(bw, scale);
2065 static int usb4_usb3_port_write_allocated_bandwidth(struct tb_port *port,
2069 u32 val, ubw, dbw, scale;
2072 /* Read the used scale, hardware default is 0 */
2073 ret = tb_port_read(port, &scale, TB_CFG_PORT,
2074 port->cap_adap + ADP_USB3_CS_3, 1);
2078 scale &= ADP_USB3_CS_3_SCALE_MASK;
2079 ubw = mbps_to_usb3_bw(upstream_bw, scale);
2080 dbw = mbps_to_usb3_bw(downstream_bw, scale);
2082 ret = tb_port_read(port, &val, TB_CFG_PORT,
2083 port->cap_adap + ADP_USB3_CS_2, 1);
2087 val &= ~(ADP_USB3_CS_2_AUBW_MASK | ADP_USB3_CS_2_ADBW_MASK);
2088 val |= dbw << ADP_USB3_CS_2_ADBW_SHIFT;
2091 return tb_port_write(port, &val, TB_CFG_PORT,
2092 port->cap_adap + ADP_USB3_CS_2, 1);
2096 * usb4_usb3_port_allocate_bandwidth() - Allocate bandwidth for USB3
2097 * @port: USB3 adapter port
2098 * @upstream_bw: New upstream bandwidth
2099 * @downstream_bw: New downstream bandwidth
2101 * This can be used to set how much bandwidth is allocated for the USB3
2102 * tunneled isochronous traffic. @upstream_bw and @downstream_bw are the
2103 * new values programmed to the USB3 adapter allocation registers. If
2104 * the values are lower than what is currently consumed the allocation
2105 * is set to what is currently consumed instead (consumed bandwidth
2106 * cannot be taken away by CM). The actual new values are returned in
2107 * @upstream_bw and @downstream_bw.
2109 * Returns %0 in case of success and negative errno if there was a
2112 int usb4_usb3_port_allocate_bandwidth(struct tb_port *port, int *upstream_bw,
2115 int ret, consumed_up, consumed_down, allocate_up, allocate_down;
2117 ret = usb4_usb3_port_set_cm_request(port);
2121 ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
2126 /* Don't allow it go lower than what is consumed */
2127 allocate_up = max(*upstream_bw, consumed_up);
2128 allocate_down = max(*downstream_bw, consumed_down);
2130 ret = usb4_usb3_port_write_allocated_bandwidth(port, allocate_up,
2135 *upstream_bw = allocate_up;
2136 *downstream_bw = allocate_down;
2139 usb4_usb3_port_clear_cm_request(port);
2144 * usb4_usb3_port_release_bandwidth() - Release allocated USB3 bandwidth
2145 * @port: USB3 adapter port
2146 * @upstream_bw: New allocated upstream bandwidth
2147 * @downstream_bw: New allocated downstream bandwidth
2149 * Releases USB3 allocated bandwidth down to what is actually consumed.
2150 * The new bandwidth is returned in @upstream_bw and @downstream_bw.
2152 * Returns 0% in success and negative errno in case of failure.
2154 int usb4_usb3_port_release_bandwidth(struct tb_port *port, int *upstream_bw,
2157 int ret, consumed_up, consumed_down;
2159 ret = usb4_usb3_port_set_cm_request(port);
2163 ret = usb4_usb3_port_read_consumed_bandwidth(port, &consumed_up,
2169 * Always keep 1000 Mb/s to make sure xHCI has at least some
2170 * bandwidth available for isochronous traffic.
2172 if (consumed_up < 1000)
2174 if (consumed_down < 1000)
2175 consumed_down = 1000;
2177 ret = usb4_usb3_port_write_allocated_bandwidth(port, consumed_up,
2182 *upstream_bw = consumed_up;
2183 *downstream_bw = consumed_down;
2186 usb4_usb3_port_clear_cm_request(port);
2190 static bool is_usb4_dpin(const struct tb_port *port)
2192 if (!tb_port_is_dpin(port))
2194 if (!tb_switch_is_usb4(port->sw))
2200 * usb4_dp_port_set_cm_id() - Assign CM ID to the DP IN adapter
2201 * @port: DP IN adapter
2202 * @cm_id: CM ID to assign
2204 * Sets CM ID for the @port. Returns %0 on success and negative errno
2205 * otherwise. Speficially returns %-EOPNOTSUPP if the @port does not
2208 int usb4_dp_port_set_cm_id(struct tb_port *port, int cm_id)
2213 if (!is_usb4_dpin(port))
2216 ret = tb_port_read(port, &val, TB_CFG_PORT,
2217 port->cap_adap + ADP_DP_CS_2, 1);
2221 val &= ~ADP_DP_CS_2_CM_ID_MASK;
2222 val |= cm_id << ADP_DP_CS_2_CM_ID_SHIFT;
2224 return tb_port_write(port, &val, TB_CFG_PORT,
2225 port->cap_adap + ADP_DP_CS_2, 1);
2229 * usb4_dp_port_bw_mode_supported() - Is the bandwidth allocation mode supported
2230 * @port: DP IN adapter to check
2232 * Can be called to any DP IN adapter. Returns true if the adapter
2233 * supports USB4 bandwidth allocation mode, false otherwise.
2235 bool usb4_dp_port_bw_mode_supported(struct tb_port *port)
2240 if (!is_usb4_dpin(port))
2243 ret = tb_port_read(port, &val, TB_CFG_PORT,
2244 port->cap_adap + DP_LOCAL_CAP, 1);
2248 return !!(val & DP_COMMON_CAP_BW_MODE);
2252 * usb4_dp_port_bw_mode_enabled() - Is the bandwidth allocation mode enabled
2253 * @port: DP IN adapter to check
2255 * Can be called to any DP IN adapter. Returns true if the bandwidth
2256 * allocation mode has been enabled, false otherwise.
2258 bool usb4_dp_port_bw_mode_enabled(struct tb_port *port)
2263 if (!is_usb4_dpin(port))
2266 ret = tb_port_read(port, &val, TB_CFG_PORT,
2267 port->cap_adap + ADP_DP_CS_8, 1);
2271 return !!(val & ADP_DP_CS_8_DPME);
2275 * usb4_dp_port_set_cm_bw_mode_supported() - Set/clear CM support for bandwidth allocation mode
2276 * @port: DP IN adapter
2277 * @supported: Does the CM support bandwidth allocation mode
2279 * Can be called to any DP IN adapter. Sets or clears the CM support bit
2280 * of the DP IN adapter. Returns %0 in success and negative errno
2281 * otherwise. Specifically returns %-OPNOTSUPP if the passed in adapter
2282 * does not support this.
2284 int usb4_dp_port_set_cm_bw_mode_supported(struct tb_port *port, bool supported)
2289 if (!is_usb4_dpin(port))
2292 ret = tb_port_read(port, &val, TB_CFG_PORT,
2293 port->cap_adap + ADP_DP_CS_2, 1);
2298 val |= ADP_DP_CS_2_CMMS;
2300 val &= ~ADP_DP_CS_2_CMMS;
2302 return tb_port_write(port, &val, TB_CFG_PORT,
2303 port->cap_adap + ADP_DP_CS_2, 1);
2307 * usb4_dp_port_group_id() - Return Group ID assigned for the adapter
2308 * @port: DP IN adapter
2310 * Reads bandwidth allocation Group ID from the DP IN adapter and
2311 * returns it. If the adapter does not support setting Group_ID
2312 * %-EOPNOTSUPP is returned.
2314 int usb4_dp_port_group_id(struct tb_port *port)
2319 if (!is_usb4_dpin(port))
2322 ret = tb_port_read(port, &val, TB_CFG_PORT,
2323 port->cap_adap + ADP_DP_CS_2, 1);
2327 return (val & ADP_DP_CS_2_GROUP_ID_MASK) >> ADP_DP_CS_2_GROUP_ID_SHIFT;
2331 * usb4_dp_port_set_group_id() - Set adapter Group ID
2332 * @port: DP IN adapter
2333 * @group_id: Group ID for the adapter
2335 * Sets bandwidth allocation mode Group ID for the DP IN adapter.
2336 * Returns %0 in case of success and negative errno otherwise.
2337 * Specifically returns %-EOPNOTSUPP if the adapter does not support
2340 int usb4_dp_port_set_group_id(struct tb_port *port, int group_id)
2345 if (!is_usb4_dpin(port))
2348 ret = tb_port_read(port, &val, TB_CFG_PORT,
2349 port->cap_adap + ADP_DP_CS_2, 1);
2353 val &= ~ADP_DP_CS_2_GROUP_ID_MASK;
2354 val |= group_id << ADP_DP_CS_2_GROUP_ID_SHIFT;
2356 return tb_port_write(port, &val, TB_CFG_PORT,
2357 port->cap_adap + ADP_DP_CS_2, 1);
2361 * usb4_dp_port_nrd() - Read non-reduced rate and lanes
2362 * @port: DP IN adapter
2363 * @rate: Non-reduced rate in Mb/s is placed here
2364 * @lanes: Non-reduced lanes are placed here
2366 * Reads the non-reduced rate and lanes from the DP IN adapter. Returns
2367 * %0 in success and negative errno otherwise. Specifically returns
2368 * %-EOPNOTSUPP if the adapter does not support this.
2370 int usb4_dp_port_nrd(struct tb_port *port, int *rate, int *lanes)
2375 if (!is_usb4_dpin(port))
2378 ret = tb_port_read(port, &val, TB_CFG_PORT,
2379 port->cap_adap + ADP_DP_CS_2, 1);
2383 tmp = (val & ADP_DP_CS_2_NRD_MLR_MASK) >> ADP_DP_CS_2_NRD_MLR_SHIFT;
2385 case DP_COMMON_CAP_RATE_RBR:
2388 case DP_COMMON_CAP_RATE_HBR:
2391 case DP_COMMON_CAP_RATE_HBR2:
2394 case DP_COMMON_CAP_RATE_HBR3:
2399 tmp = val & ADP_DP_CS_2_NRD_MLC_MASK;
2401 case DP_COMMON_CAP_1_LANE:
2404 case DP_COMMON_CAP_2_LANES:
2407 case DP_COMMON_CAP_4_LANES:
2416 * usb4_dp_port_set_nrd() - Set non-reduced rate and lanes
2417 * @port: DP IN adapter
2418 * @rate: Non-reduced rate in Mb/s
2419 * @lanes: Non-reduced lanes
2421 * Before the capabilities reduction this function can be used to set
2422 * the non-reduced values for the DP IN adapter. Returns %0 in success
2423 * and negative errno otherwise. If the adapter does not support this
2424 * %-EOPNOTSUPP is returned.
2426 int usb4_dp_port_set_nrd(struct tb_port *port, int rate, int lanes)
2431 if (!is_usb4_dpin(port))
2434 ret = tb_port_read(port, &val, TB_CFG_PORT,
2435 port->cap_adap + ADP_DP_CS_2, 1);
2439 val &= ~ADP_DP_CS_2_NRD_MLR_MASK;
2445 val |= (DP_COMMON_CAP_RATE_HBR << ADP_DP_CS_2_NRD_MLR_SHIFT)
2446 & ADP_DP_CS_2_NRD_MLR_MASK;
2449 val |= (DP_COMMON_CAP_RATE_HBR2 << ADP_DP_CS_2_NRD_MLR_SHIFT)
2450 & ADP_DP_CS_2_NRD_MLR_MASK;
2453 val |= (DP_COMMON_CAP_RATE_HBR3 << ADP_DP_CS_2_NRD_MLR_SHIFT)
2454 & ADP_DP_CS_2_NRD_MLR_MASK;
2460 val &= ~ADP_DP_CS_2_NRD_MLC_MASK;
2466 val |= DP_COMMON_CAP_2_LANES;
2469 val |= DP_COMMON_CAP_4_LANES;
2475 return tb_port_write(port, &val, TB_CFG_PORT,
2476 port->cap_adap + ADP_DP_CS_2, 1);
2480 * usb4_dp_port_granularity() - Return granularity for the bandwidth values
2481 * @port: DP IN adapter
2483 * Reads the programmed granularity from @port. If the DP IN adapter does
2484 * not support bandwidth allocation mode returns %-EOPNOTSUPP and negative
2485 * errno in other error cases.
2487 int usb4_dp_port_granularity(struct tb_port *port)
2492 if (!is_usb4_dpin(port))
2495 ret = tb_port_read(port, &val, TB_CFG_PORT,
2496 port->cap_adap + ADP_DP_CS_2, 1);
2500 val &= ADP_DP_CS_2_GR_MASK;
2501 val >>= ADP_DP_CS_2_GR_SHIFT;
2504 case ADP_DP_CS_2_GR_0_25G:
2506 case ADP_DP_CS_2_GR_0_5G:
2508 case ADP_DP_CS_2_GR_1G:
2516 * usb4_dp_port_set_granularity() - Set granularity for the bandwidth values
2517 * @port: DP IN adapter
2518 * @granularity: Granularity in Mb/s. Supported values: 1000, 500 and 250.
2520 * Sets the granularity used with the estimated, allocated and requested
2521 * bandwidth. Returns %0 in success and negative errno otherwise. If the
2522 * adapter does not support this %-EOPNOTSUPP is returned.
2524 int usb4_dp_port_set_granularity(struct tb_port *port, int granularity)
2529 if (!is_usb4_dpin(port))
2532 ret = tb_port_read(port, &val, TB_CFG_PORT,
2533 port->cap_adap + ADP_DP_CS_2, 1);
2537 val &= ~ADP_DP_CS_2_GR_MASK;
2539 switch (granularity) {
2541 val |= ADP_DP_CS_2_GR_0_25G << ADP_DP_CS_2_GR_SHIFT;
2544 val |= ADP_DP_CS_2_GR_0_5G << ADP_DP_CS_2_GR_SHIFT;
2547 val |= ADP_DP_CS_2_GR_1G << ADP_DP_CS_2_GR_SHIFT;
2553 return tb_port_write(port, &val, TB_CFG_PORT,
2554 port->cap_adap + ADP_DP_CS_2, 1);
2558 * usb4_dp_port_set_estimated_bw() - Set estimated bandwidth
2559 * @port: DP IN adapter
2560 * @bw: Estimated bandwidth in Mb/s.
2562 * Sets the estimated bandwidth to @bw. Set the granularity by calling
2563 * usb4_dp_port_set_granularity() before calling this. The @bw is round
2564 * down to the closest granularity multiplier. Returns %0 in success
2565 * and negative errno otherwise. Specifically returns %-EOPNOTSUPP if
2566 * the adapter does not support this.
2568 int usb4_dp_port_set_estimated_bw(struct tb_port *port, int bw)
2570 u32 val, granularity;
2573 if (!is_usb4_dpin(port))
2576 ret = usb4_dp_port_granularity(port);
2581 ret = tb_port_read(port, &val, TB_CFG_PORT,
2582 port->cap_adap + ADP_DP_CS_2, 1);
2586 val &= ~ADP_DP_CS_2_ESTIMATED_BW_MASK;
2587 val |= (bw / granularity) << ADP_DP_CS_2_ESTIMATED_BW_SHIFT;
2589 return tb_port_write(port, &val, TB_CFG_PORT,
2590 port->cap_adap + ADP_DP_CS_2, 1);
2594 * usb4_dp_port_allocated_bw() - Return allocated bandwidth
2595 * @port: DP IN adapter
2597 * Reads and returns allocated bandwidth for @port in Mb/s (taking into
2598 * account the programmed granularity). Returns negative errno in case
2601 int usb4_dp_port_allocated_bw(struct tb_port *port)
2603 u32 val, granularity;
2606 if (!is_usb4_dpin(port))
2609 ret = usb4_dp_port_granularity(port);
2614 ret = tb_port_read(port, &val, TB_CFG_PORT,
2615 port->cap_adap + DP_STATUS, 1);
2619 val &= DP_STATUS_ALLOCATED_BW_MASK;
2620 val >>= DP_STATUS_ALLOCATED_BW_SHIFT;
2622 return val * granularity;
2625 static int __usb4_dp_port_set_cm_ack(struct tb_port *port, bool ack)
2630 ret = tb_port_read(port, &val, TB_CFG_PORT,
2631 port->cap_adap + ADP_DP_CS_2, 1);
2636 val |= ADP_DP_CS_2_CA;
2638 val &= ~ADP_DP_CS_2_CA;
2640 return tb_port_write(port, &val, TB_CFG_PORT,
2641 port->cap_adap + ADP_DP_CS_2, 1);
2644 static inline int usb4_dp_port_set_cm_ack(struct tb_port *port)
2646 return __usb4_dp_port_set_cm_ack(port, true);
2649 static int usb4_dp_port_wait_and_clear_cm_ack(struct tb_port *port,
2656 ret = __usb4_dp_port_set_cm_ack(port, false);
2660 end = ktime_add_ms(ktime_get(), timeout_msec);
2662 ret = tb_port_read(port, &val, TB_CFG_PORT,
2663 port->cap_adap + ADP_DP_CS_8, 1);
2667 if (!(val & ADP_DP_CS_8_DR))
2670 usleep_range(50, 100);
2671 } while (ktime_before(ktime_get(), end));
2673 if (val & ADP_DP_CS_8_DR)
2676 ret = tb_port_read(port, &val, TB_CFG_PORT,
2677 port->cap_adap + ADP_DP_CS_2, 1);
2681 val &= ~ADP_DP_CS_2_CA;
2682 return tb_port_write(port, &val, TB_CFG_PORT,
2683 port->cap_adap + ADP_DP_CS_2, 1);
2687 * usb4_dp_port_allocate_bw() - Set allocated bandwidth
2688 * @port: DP IN adapter
2689 * @bw: New allocated bandwidth in Mb/s
2691 * Communicates the new allocated bandwidth with the DPCD (graphics
2692 * driver). Takes into account the programmed granularity. Returns %0 in
2693 * success and negative errno in case of error.
2695 int usb4_dp_port_allocate_bw(struct tb_port *port, int bw)
2697 u32 val, granularity;
2700 if (!is_usb4_dpin(port))
2703 ret = usb4_dp_port_granularity(port);
2708 ret = tb_port_read(port, &val, TB_CFG_PORT,
2709 port->cap_adap + DP_STATUS, 1);
2713 val &= ~DP_STATUS_ALLOCATED_BW_MASK;
2714 val |= (bw / granularity) << DP_STATUS_ALLOCATED_BW_SHIFT;
2716 ret = tb_port_write(port, &val, TB_CFG_PORT,
2717 port->cap_adap + DP_STATUS, 1);
2721 ret = usb4_dp_port_set_cm_ack(port);
2725 return usb4_dp_port_wait_and_clear_cm_ack(port, 500);
2729 * usb4_dp_port_requested_bw() - Read requested bandwidth
2730 * @port: DP IN adapter
2732 * Reads the DPCD (graphics driver) requested bandwidth and returns it
2733 * in Mb/s. Takes the programmed granularity into account. In case of
2734 * error returns negative errno. Specifically returns %-EOPNOTSUPP if
2735 * the adapter does not support bandwidth allocation mode, and %ENODATA
2736 * if there is no active bandwidth request from the graphics driver.
2738 int usb4_dp_port_requested_bw(struct tb_port *port)
2740 u32 val, granularity;
2743 if (!is_usb4_dpin(port))
2746 ret = usb4_dp_port_granularity(port);
2751 ret = tb_port_read(port, &val, TB_CFG_PORT,
2752 port->cap_adap + ADP_DP_CS_8, 1);
2756 if (!(val & ADP_DP_CS_8_DR))
2759 return (val & ADP_DP_CS_8_REQUESTED_BW_MASK) * granularity;