Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | /* |
| 4 | * Copyright 2016-2019 HabanaLabs, Ltd. |
| 5 | * All Rights Reserved. |
| 6 | */ |
| 7 | |
| 8 | #include "habanalabs.h" |
Greg Kroah-Hartman | 7b16a15 | 2020-07-28 19:18:51 +0200 | [diff] [blame] | 9 | #include "../include/hw_ip/pci/pci_general.h" |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 10 | |
| 11 | #include <linux/pci.h> |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 12 | #include <linux/bitfield.h> |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 13 | |
Omer Shpigelman | a1e537b | 2019-05-13 14:44:50 +0300 | [diff] [blame] | 14 | #define HL_PLDM_PCI_ELBI_TIMEOUT_MSEC (HL_PCI_ELBI_TIMEOUT_MSEC * 10) |
| 15 | |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 16 | #define IATU_REGION_CTRL_REGION_EN_MASK BIT(31) |
| 17 | #define IATU_REGION_CTRL_MATCH_MODE_MASK BIT(30) |
| 18 | #define IATU_REGION_CTRL_NUM_MATCH_EN_MASK BIT(19) |
| 19 | #define IATU_REGION_CTRL_BAR_NUM_MASK GENMASK(10, 8) |
| 20 | |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 21 | /** |
| 22 | * hl_pci_bars_map() - Map PCI BARs. |
| 23 | * @hdev: Pointer to hl_device structure. |
Lee Jones | df123c9 | 2020-07-01 09:58:37 +0100 | [diff] [blame] | 24 | * @name: Array of BAR names. |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 25 | * @is_wc: Array with flag per BAR whether a write-combined mapping is needed. |
| 26 | * |
| 27 | * Request PCI regions and map them to kernel virtual addresses. |
| 28 | * |
| 29 | * Return: 0 on success, non-zero for failure. |
| 30 | */ |
| 31 | int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3], |
| 32 | bool is_wc[3]) |
| 33 | { |
| 34 | struct pci_dev *pdev = hdev->pdev; |
| 35 | int rc, i, bar; |
| 36 | |
| 37 | rc = pci_request_regions(pdev, HL_NAME); |
| 38 | if (rc) { |
| 39 | dev_err(hdev->dev, "Cannot obtain PCI resources\n"); |
| 40 | return rc; |
| 41 | } |
| 42 | |
| 43 | for (i = 0 ; i < 3 ; i++) { |
| 44 | bar = i * 2; /* 64-bit BARs */ |
| 45 | hdev->pcie_bar[bar] = is_wc[i] ? |
| 46 | pci_ioremap_wc_bar(pdev, bar) : |
| 47 | pci_ioremap_bar(pdev, bar); |
| 48 | if (!hdev->pcie_bar[bar]) { |
| 49 | dev_err(hdev->dev, "pci_ioremap%s_bar failed for %s\n", |
| 50 | is_wc[i] ? "_wc" : "", name[i]); |
| 51 | rc = -ENODEV; |
| 52 | goto err; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return 0; |
| 57 | |
| 58 | err: |
| 59 | for (i = 2 ; i >= 0 ; i--) { |
| 60 | bar = i * 2; /* 64-bit BARs */ |
| 61 | if (hdev->pcie_bar[bar]) |
| 62 | iounmap(hdev->pcie_bar[bar]); |
| 63 | } |
| 64 | |
| 65 | pci_release_regions(pdev); |
| 66 | |
| 67 | return rc; |
| 68 | } |
| 69 | |
Lee Jones | df123c9 | 2020-07-01 09:58:37 +0100 | [diff] [blame] | 70 | /** |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 71 | * hl_pci_bars_unmap() - Unmap PCI BARS. |
| 72 | * @hdev: Pointer to hl_device structure. |
| 73 | * |
| 74 | * Release all PCI BARs and unmap their virtual addresses. |
| 75 | */ |
| 76 | static void hl_pci_bars_unmap(struct hl_device *hdev) |
| 77 | { |
| 78 | struct pci_dev *pdev = hdev->pdev; |
| 79 | int i, bar; |
| 80 | |
| 81 | for (i = 2 ; i >= 0 ; i--) { |
| 82 | bar = i * 2; /* 64-bit BARs */ |
| 83 | iounmap(hdev->pcie_bar[bar]); |
| 84 | } |
| 85 | |
| 86 | pci_release_regions(pdev); |
| 87 | } |
| 88 | |
Lee Jones | df123c9 | 2020-07-01 09:58:37 +0100 | [diff] [blame] | 89 | /** |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 90 | * hl_pci_elbi_write() - Write through the ELBI interface. |
| 91 | * @hdev: Pointer to hl_device structure. |
Lee Jones | df123c9 | 2020-07-01 09:58:37 +0100 | [diff] [blame] | 92 | * @addr: Address to write to |
| 93 | * @data: Data to write |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 94 | * |
| 95 | * Return: 0 on success, negative value for failure. |
| 96 | */ |
| 97 | static int hl_pci_elbi_write(struct hl_device *hdev, u64 addr, u32 data) |
| 98 | { |
| 99 | struct pci_dev *pdev = hdev->pdev; |
| 100 | ktime_t timeout; |
Omer Shpigelman | a1e537b | 2019-05-13 14:44:50 +0300 | [diff] [blame] | 101 | u64 msec; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 102 | u32 val; |
| 103 | |
Omer Shpigelman | a1e537b | 2019-05-13 14:44:50 +0300 | [diff] [blame] | 104 | if (hdev->pldm) |
| 105 | msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC; |
| 106 | else |
| 107 | msec = HL_PCI_ELBI_TIMEOUT_MSEC; |
| 108 | |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 109 | /* Clear previous status */ |
| 110 | pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0); |
| 111 | |
| 112 | pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr); |
| 113 | pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data); |
| 114 | pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL, |
| 115 | PCI_CONFIG_ELBI_CTRL_WRITE); |
| 116 | |
Omer Shpigelman | a1e537b | 2019-05-13 14:44:50 +0300 | [diff] [blame] | 117 | timeout = ktime_add_ms(ktime_get(), msec); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 118 | for (;;) { |
| 119 | pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val); |
| 120 | if (val & PCI_CONFIG_ELBI_STS_MASK) |
| 121 | break; |
| 122 | if (ktime_compare(ktime_get(), timeout) > 0) { |
| 123 | pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, |
| 124 | &val); |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | usleep_range(300, 500); |
| 129 | } |
| 130 | |
| 131 | if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE) |
| 132 | return 0; |
| 133 | |
| 134 | if (val & PCI_CONFIG_ELBI_STS_ERR) { |
| 135 | dev_err(hdev->dev, "Error writing to ELBI\n"); |
| 136 | return -EIO; |
| 137 | } |
| 138 | |
| 139 | if (!(val & PCI_CONFIG_ELBI_STS_MASK)) { |
| 140 | dev_err(hdev->dev, "ELBI write didn't finish in time\n"); |
| 141 | return -EIO; |
| 142 | } |
| 143 | |
| 144 | dev_err(hdev->dev, "ELBI write has undefined bits in status\n"); |
| 145 | return -EIO; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * hl_pci_iatu_write() - iatu write routine. |
| 150 | * @hdev: Pointer to hl_device structure. |
Lee Jones | df123c9 | 2020-07-01 09:58:37 +0100 | [diff] [blame] | 151 | * @addr: Address to write to |
| 152 | * @data: Data to write |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 153 | * |
| 154 | * Return: 0 on success, negative value for failure. |
| 155 | */ |
| 156 | int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data) |
| 157 | { |
| 158 | struct asic_fixed_properties *prop = &hdev->asic_prop; |
| 159 | u32 dbi_offset; |
| 160 | int rc; |
| 161 | |
| 162 | dbi_offset = addr & 0xFFF; |
| 163 | |
| 164 | rc = hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0x00300000); |
| 165 | rc |= hl_pci_elbi_write(hdev, prop->pcie_dbi_base_address + dbi_offset, |
| 166 | data); |
| 167 | |
| 168 | if (rc) |
| 169 | return -EIO; |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
Lee Jones | df123c9 | 2020-07-01 09:58:37 +0100 | [diff] [blame] | 174 | /** |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 175 | * hl_pci_reset_link_through_bridge() - Reset PCI link. |
| 176 | * @hdev: Pointer to hl_device structure. |
| 177 | */ |
| 178 | static void hl_pci_reset_link_through_bridge(struct hl_device *hdev) |
| 179 | { |
| 180 | struct pci_dev *pdev = hdev->pdev; |
| 181 | struct pci_dev *parent_port; |
| 182 | u16 val; |
| 183 | |
| 184 | parent_port = pdev->bus->self; |
| 185 | pci_read_config_word(parent_port, PCI_BRIDGE_CONTROL, &val); |
| 186 | val |= PCI_BRIDGE_CTL_BUS_RESET; |
| 187 | pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val); |
| 188 | ssleep(1); |
| 189 | |
| 190 | val &= ~(PCI_BRIDGE_CTL_BUS_RESET); |
| 191 | pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val); |
| 192 | ssleep(3); |
| 193 | } |
| 194 | |
| 195 | /** |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 196 | * hl_pci_set_inbound_region() - Configure inbound region |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 197 | * @hdev: Pointer to hl_device structure. |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 198 | * @region: Inbound region number. |
| 199 | * @pci_region: Inbound region parameters. |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 200 | * |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 201 | * Configure the iATU inbound region. |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 202 | * |
| 203 | * Return: 0 on success, negative value for failure. |
| 204 | */ |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 205 | int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region, |
| 206 | struct hl_inbound_pci_region *pci_region) |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 207 | { |
| 208 | struct asic_fixed_properties *prop = &hdev->asic_prop; |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 209 | u64 bar_phys_base, region_base, region_end_address; |
| 210 | u32 offset, ctrl_reg_val; |
| 211 | int rc = 0; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 212 | |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 213 | /* region offset */ |
| 214 | offset = (0x200 * region) + 0x100; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 215 | |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 216 | if (pci_region->mode == PCI_ADDRESS_MATCH_MODE) { |
| 217 | bar_phys_base = hdev->pcie_bar_phys[pci_region->bar]; |
| 218 | region_base = bar_phys_base + pci_region->offset_in_bar; |
| 219 | region_end_address = region_base + pci_region->size - 1; |
| 220 | |
| 221 | rc |= hl_pci_iatu_write(hdev, offset + 0x8, |
| 222 | lower_32_bits(region_base)); |
| 223 | rc |= hl_pci_iatu_write(hdev, offset + 0xC, |
| 224 | upper_32_bits(region_base)); |
| 225 | rc |= hl_pci_iatu_write(hdev, offset + 0x10, |
| 226 | lower_32_bits(region_end_address)); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* Point to the specified address */ |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 230 | rc = hl_pci_iatu_write(hdev, offset + 0x14, |
| 231 | lower_32_bits(pci_region->addr)); |
| 232 | rc |= hl_pci_iatu_write(hdev, offset + 0x18, |
| 233 | upper_32_bits(pci_region->addr)); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 234 | rc |= hl_pci_iatu_write(hdev, offset + 0x0, 0); |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 235 | |
| 236 | /* Enable + bar/address match + match enable + bar number */ |
| 237 | ctrl_reg_val = FIELD_PREP(IATU_REGION_CTRL_REGION_EN_MASK, 1); |
| 238 | ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_MATCH_MODE_MASK, |
| 239 | pci_region->mode); |
| 240 | ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_NUM_MATCH_EN_MASK, 1); |
| 241 | |
| 242 | if (pci_region->mode == PCI_BAR_MATCH_MODE) |
| 243 | ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_BAR_NUM_MASK, |
| 244 | pci_region->bar); |
| 245 | |
| 246 | rc |= hl_pci_iatu_write(hdev, offset + 0x4, ctrl_reg_val); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 247 | |
| 248 | /* Return the DBI window to the default location */ |
| 249 | rc |= hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0); |
| 250 | rc |= hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr + 4, 0); |
| 251 | |
| 252 | if (rc) |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 253 | dev_err(hdev->dev, "failed to map bar %u to 0x%08llx\n", |
| 254 | pci_region->bar, pci_region->addr); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 255 | |
| 256 | return rc; |
| 257 | } |
| 258 | |
| 259 | /** |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 260 | * hl_pci_set_outbound_region() - Configure outbound region 0 |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 261 | * @hdev: Pointer to hl_device structure. |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 262 | * @pci_region: Outbound region parameters. |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 263 | * |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 264 | * Configure the iATU outbound region 0. |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 265 | * |
| 266 | * Return: 0 on success, negative value for failure. |
| 267 | */ |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 268 | int hl_pci_set_outbound_region(struct hl_device *hdev, |
| 269 | struct hl_outbound_pci_region *pci_region) |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 270 | { |
| 271 | struct asic_fixed_properties *prop = &hdev->asic_prop; |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 272 | u64 outbound_region_end_address; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 273 | int rc = 0; |
| 274 | |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 275 | /* Outbound Region 0 */ |
| 276 | outbound_region_end_address = |
| 277 | pci_region->addr + pci_region->size - 1; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 278 | rc |= hl_pci_iatu_write(hdev, 0x008, |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 279 | lower_32_bits(pci_region->addr)); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 280 | rc |= hl_pci_iatu_write(hdev, 0x00C, |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 281 | upper_32_bits(pci_region->addr)); |
| 282 | rc |= hl_pci_iatu_write(hdev, 0x010, |
| 283 | lower_32_bits(outbound_region_end_address)); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 284 | rc |= hl_pci_iatu_write(hdev, 0x014, 0); |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 285 | |
| 286 | if ((hdev->power9_64bit_dma_enable) && (hdev->dma_mask == 64)) |
| 287 | rc |= hl_pci_iatu_write(hdev, 0x018, 0x08000000); |
| 288 | else |
| 289 | rc |= hl_pci_iatu_write(hdev, 0x018, 0); |
| 290 | |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 291 | rc |= hl_pci_iatu_write(hdev, 0x020, |
| 292 | upper_32_bits(outbound_region_end_address)); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 293 | /* Increase region size */ |
| 294 | rc |= hl_pci_iatu_write(hdev, 0x000, 0x00002000); |
| 295 | /* Enable */ |
| 296 | rc |= hl_pci_iatu_write(hdev, 0x004, 0x80000000); |
| 297 | |
| 298 | /* Return the DBI window to the default location */ |
| 299 | rc |= hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0); |
| 300 | rc |= hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr + 4, 0); |
| 301 | |
Ofir Bitton | f4cbfd2 | 2020-06-15 17:45:12 +0300 | [diff] [blame] | 302 | return rc; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | /** |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 306 | * hl_pci_set_dma_mask() - Set DMA masks for the device. |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 307 | * @hdev: Pointer to hl_device structure. |
| 308 | * |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 309 | * This function sets the DMA masks (regular and consistent) for a specified |
| 310 | * value. If it doesn't succeed, it tries to set it to a fall-back value |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 311 | * |
| 312 | * Return: 0 on success, non-zero for failure. |
| 313 | */ |
kbuild test robot | ba7193c | 2020-04-06 00:01:31 +0800 | [diff] [blame] | 314 | static int hl_pci_set_dma_mask(struct hl_device *hdev) |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 315 | { |
| 316 | struct pci_dev *pdev = hdev->pdev; |
| 317 | int rc; |
| 318 | |
| 319 | /* set DMA mask */ |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 320 | rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask)); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 321 | if (rc) { |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 322 | dev_err(hdev->dev, |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 323 | "Failed to set pci dma mask to %d bits, error %d\n", |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 324 | hdev->dma_mask, rc); |
| 325 | return rc; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 326 | } |
| 327 | |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 328 | rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask)); |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 329 | if (rc) { |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 330 | dev_err(hdev->dev, |
| 331 | "Failed to set pci consistent dma mask to %d bits, error %d\n", |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 332 | hdev->dma_mask, rc); |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 333 | return rc; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /** |
| 340 | * hl_pci_init() - PCI initialization code. |
| 341 | * @hdev: Pointer to hl_device structure. |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 342 | * |
| 343 | * Set DMA masks, initialize the PCI controller and map the PCI BARs. |
| 344 | * |
| 345 | * Return: 0 on success, non-zero for failure. |
| 346 | */ |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 347 | int hl_pci_init(struct hl_device *hdev) |
Oded Gabbay | d997387 | 2019-03-07 18:03:23 +0200 | [diff] [blame] | 348 | { |
| 349 | struct pci_dev *pdev = hdev->pdev; |
| 350 | int rc; |
| 351 | |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 352 | if (hdev->reset_pcilink) |
| 353 | hl_pci_reset_link_through_bridge(hdev); |
| 354 | |
| 355 | rc = pci_enable_device_mem(pdev); |
| 356 | if (rc) { |
| 357 | dev_err(hdev->dev, "can't enable PCI device\n"); |
| 358 | return rc; |
| 359 | } |
| 360 | |
| 361 | pci_set_master(pdev); |
| 362 | |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 363 | rc = hdev->asic_funcs->pci_bars_map(hdev); |
| 364 | if (rc) { |
| 365 | dev_err(hdev->dev, "Failed to initialize PCI BARs\n"); |
| 366 | goto disable_device; |
| 367 | } |
| 368 | |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 369 | rc = hdev->asic_funcs->init_iatu(hdev); |
| 370 | if (rc) { |
| 371 | dev_err(hdev->dev, "Failed to initialize iATU\n"); |
| 372 | goto disable_device; |
| 373 | } |
| 374 | |
Oded Gabbay | cb056b9 | 2020-03-29 13:18:30 +0300 | [diff] [blame] | 375 | rc = hl_pci_set_dma_mask(hdev); |
| 376 | if (rc) |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 377 | goto disable_device; |
Tomer Tayar | b6f897d | 2019-03-05 16:48:42 +0200 | [diff] [blame] | 378 | |
| 379 | return 0; |
| 380 | |
| 381 | disable_device: |
| 382 | pci_clear_master(pdev); |
| 383 | pci_disable_device(pdev); |
| 384 | |
| 385 | return rc; |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * hl_fw_fini() - PCI finalization code. |
| 390 | * @hdev: Pointer to hl_device structure |
| 391 | * |
| 392 | * Unmap PCI bars and disable PCI device. |
| 393 | */ |
| 394 | void hl_pci_fini(struct hl_device *hdev) |
| 395 | { |
| 396 | hl_pci_bars_unmap(hdev); |
| 397 | |
| 398 | pci_clear_master(hdev->pdev); |
| 399 | pci_disable_device(hdev->pdev); |
| 400 | } |