1 // SPDX-License-Identifier: GPL-2.0
4 * Copyright 2016-2019 HabanaLabs, Ltd.
8 #include "habanalabs.h"
9 #include "../include/hw_ip/pci/pci_general.h"
11 #include <linux/pci.h>
13 #define HL_PLDM_PCI_ELBI_TIMEOUT_MSEC (HL_PCI_ELBI_TIMEOUT_MSEC * 10)
15 #define IATU_REGION_CTRL_REGION_EN_MASK BIT(31)
16 #define IATU_REGION_CTRL_MATCH_MODE_MASK BIT(30)
17 #define IATU_REGION_CTRL_NUM_MATCH_EN_MASK BIT(19)
18 #define IATU_REGION_CTRL_BAR_NUM_MASK GENMASK(10, 8)
21 * hl_pci_bars_map() - Map PCI BARs.
22 * @hdev: Pointer to hl_device structure.
23 * @name: Array of BAR names.
24 * @is_wc: Array with flag per BAR whether a write-combined mapping is needed.
26 * Request PCI regions and map them to kernel virtual addresses.
28 * Return: 0 on success, non-zero for failure.
30 int hl_pci_bars_map(struct hl_device *hdev, const char * const name[3],
33 struct pci_dev *pdev = hdev->pdev;
36 rc = pci_request_regions(pdev, HL_NAME);
38 dev_err(hdev->dev, "Cannot obtain PCI resources\n");
42 for (i = 0 ; i < 3 ; i++) {
43 bar = i * 2; /* 64-bit BARs */
44 hdev->pcie_bar[bar] = is_wc[i] ?
45 pci_ioremap_wc_bar(pdev, bar) :
46 pci_ioremap_bar(pdev, bar);
47 if (!hdev->pcie_bar[bar]) {
48 dev_err(hdev->dev, "pci_ioremap%s_bar failed for %s\n",
49 is_wc[i] ? "_wc" : "", name[i]);
58 for (i = 2 ; i >= 0 ; i--) {
59 bar = i * 2; /* 64-bit BARs */
60 if (hdev->pcie_bar[bar])
61 iounmap(hdev->pcie_bar[bar]);
64 pci_release_regions(pdev);
70 * hl_pci_bars_unmap() - Unmap PCI BARS.
71 * @hdev: Pointer to hl_device structure.
73 * Release all PCI BARs and unmap their virtual addresses.
75 static void hl_pci_bars_unmap(struct hl_device *hdev)
77 struct pci_dev *pdev = hdev->pdev;
80 for (i = 2 ; i >= 0 ; i--) {
81 bar = i * 2; /* 64-bit BARs */
82 iounmap(hdev->pcie_bar[bar]);
85 pci_release_regions(pdev);
89 * hl_pci_elbi_write() - Write through the ELBI interface.
90 * @hdev: Pointer to hl_device structure.
91 * @addr: Address to write to
92 * @data: Data to write
94 * Return: 0 on success, negative value for failure.
96 static int hl_pci_elbi_write(struct hl_device *hdev, u64 addr, u32 data)
98 struct pci_dev *pdev = hdev->pdev;
104 msec = HL_PLDM_PCI_ELBI_TIMEOUT_MSEC;
106 msec = HL_PCI_ELBI_TIMEOUT_MSEC;
108 /* Clear previous status */
109 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, 0);
111 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_ADDR, (u32) addr);
112 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_DATA, data);
113 pci_write_config_dword(pdev, mmPCI_CONFIG_ELBI_CTRL,
114 PCI_CONFIG_ELBI_CTRL_WRITE);
116 timeout = ktime_add_ms(ktime_get(), msec);
118 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS, &val);
119 if (val & PCI_CONFIG_ELBI_STS_MASK)
121 if (ktime_compare(ktime_get(), timeout) > 0) {
122 pci_read_config_dword(pdev, mmPCI_CONFIG_ELBI_STS,
127 usleep_range(300, 500);
130 if ((val & PCI_CONFIG_ELBI_STS_MASK) == PCI_CONFIG_ELBI_STS_DONE)
133 if (val & PCI_CONFIG_ELBI_STS_ERR)
136 if (!(val & PCI_CONFIG_ELBI_STS_MASK)) {
137 dev_err(hdev->dev, "ELBI write didn't finish in time\n");
141 dev_err(hdev->dev, "ELBI write has undefined bits in status\n");
146 * hl_pci_iatu_write() - iatu write routine.
147 * @hdev: Pointer to hl_device structure.
148 * @addr: Address to write to
149 * @data: Data to write
151 * Return: 0 on success, negative value for failure.
153 int hl_pci_iatu_write(struct hl_device *hdev, u32 addr, u32 data)
155 struct asic_fixed_properties *prop = &hdev->asic_prop;
159 dbi_offset = addr & 0xFFF;
161 /* Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
162 * in case the firmware security is enabled
164 hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0x00300000);
166 rc = hl_pci_elbi_write(hdev, prop->pcie_dbi_base_address + dbi_offset,
176 * hl_pci_reset_link_through_bridge() - Reset PCI link.
177 * @hdev: Pointer to hl_device structure.
179 static void hl_pci_reset_link_through_bridge(struct hl_device *hdev)
181 struct pci_dev *pdev = hdev->pdev;
182 struct pci_dev *parent_port;
185 parent_port = pdev->bus->self;
186 pci_read_config_word(parent_port, PCI_BRIDGE_CONTROL, &val);
187 val |= PCI_BRIDGE_CTL_BUS_RESET;
188 pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
191 val &= ~(PCI_BRIDGE_CTL_BUS_RESET);
192 pci_write_config_word(parent_port, PCI_BRIDGE_CONTROL, val);
197 * hl_pci_set_inbound_region() - Configure inbound region
198 * @hdev: Pointer to hl_device structure.
199 * @region: Inbound region number.
200 * @pci_region: Inbound region parameters.
202 * Configure the iATU inbound region.
204 * Return: 0 on success, negative value for failure.
206 int hl_pci_set_inbound_region(struct hl_device *hdev, u8 region,
207 struct hl_inbound_pci_region *pci_region)
209 struct asic_fixed_properties *prop = &hdev->asic_prop;
210 u64 bar_phys_base, region_base, region_end_address;
211 u32 offset, ctrl_reg_val;
215 offset = (0x200 * region) + 0x100;
217 if (pci_region->mode == PCI_ADDRESS_MATCH_MODE) {
218 bar_phys_base = hdev->pcie_bar_phys[pci_region->bar];
219 region_base = bar_phys_base + pci_region->offset_in_bar;
220 region_end_address = region_base + pci_region->size - 1;
222 rc |= hl_pci_iatu_write(hdev, offset + 0x8,
223 lower_32_bits(region_base));
224 rc |= hl_pci_iatu_write(hdev, offset + 0xC,
225 upper_32_bits(region_base));
226 rc |= hl_pci_iatu_write(hdev, offset + 0x10,
227 lower_32_bits(region_end_address));
230 /* Point to the specified address */
231 rc |= hl_pci_iatu_write(hdev, offset + 0x14,
232 lower_32_bits(pci_region->addr));
233 rc |= hl_pci_iatu_write(hdev, offset + 0x18,
234 upper_32_bits(pci_region->addr));
235 rc |= hl_pci_iatu_write(hdev, offset + 0x0, 0);
237 /* Enable + bar/address match + match enable + bar number */
238 ctrl_reg_val = FIELD_PREP(IATU_REGION_CTRL_REGION_EN_MASK, 1);
239 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_MATCH_MODE_MASK,
241 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_NUM_MATCH_EN_MASK, 1);
243 if (pci_region->mode == PCI_BAR_MATCH_MODE)
244 ctrl_reg_val |= FIELD_PREP(IATU_REGION_CTRL_BAR_NUM_MASK,
247 rc |= hl_pci_iatu_write(hdev, offset + 0x4, ctrl_reg_val);
249 /* Return the DBI window to the default location
250 * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
251 * in case the firmware security is enabled
253 hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
256 dev_err(hdev->dev, "failed to map bar %u to 0x%08llx\n",
257 pci_region->bar, pci_region->addr);
263 * hl_pci_set_outbound_region() - Configure outbound region 0
264 * @hdev: Pointer to hl_device structure.
265 * @pci_region: Outbound region parameters.
267 * Configure the iATU outbound region 0.
269 * Return: 0 on success, negative value for failure.
271 int hl_pci_set_outbound_region(struct hl_device *hdev,
272 struct hl_outbound_pci_region *pci_region)
274 struct asic_fixed_properties *prop = &hdev->asic_prop;
275 u64 outbound_region_end_address;
278 /* Outbound Region 0 */
279 outbound_region_end_address =
280 pci_region->addr + pci_region->size - 1;
281 rc |= hl_pci_iatu_write(hdev, 0x008,
282 lower_32_bits(pci_region->addr));
283 rc |= hl_pci_iatu_write(hdev, 0x00C,
284 upper_32_bits(pci_region->addr));
285 rc |= hl_pci_iatu_write(hdev, 0x010,
286 lower_32_bits(outbound_region_end_address));
287 rc |= hl_pci_iatu_write(hdev, 0x014, 0);
289 if ((hdev->power9_64bit_dma_enable) && (hdev->dma_mask == 64))
290 rc |= hl_pci_iatu_write(hdev, 0x018, 0x08000000);
292 rc |= hl_pci_iatu_write(hdev, 0x018, 0);
294 rc |= hl_pci_iatu_write(hdev, 0x020,
295 upper_32_bits(outbound_region_end_address));
296 /* Increase region size */
297 rc |= hl_pci_iatu_write(hdev, 0x000, 0x00002000);
299 rc |= hl_pci_iatu_write(hdev, 0x004, 0x80000000);
301 /* Return the DBI window to the default location
302 * Ignore result of writing to pcie_aux_dbi_reg_addr as it could fail
303 * in case the firmware security is enabled
305 hl_pci_elbi_write(hdev, prop->pcie_aux_dbi_reg_addr, 0);
311 * hl_pci_set_dma_mask() - Set DMA masks for the device.
312 * @hdev: Pointer to hl_device structure.
314 * This function sets the DMA masks (regular and consistent) for a specified
315 * value. If it doesn't succeed, it tries to set it to a fall-back value
317 * Return: 0 on success, non-zero for failure.
319 static int hl_pci_set_dma_mask(struct hl_device *hdev)
321 struct pci_dev *pdev = hdev->pdev;
325 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask));
328 "Failed to set pci dma mask to %d bits, error %d\n",
333 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(hdev->dma_mask));
336 "Failed to set pci consistent dma mask to %d bits, error %d\n",
345 * hl_pci_init() - PCI initialization code.
346 * @hdev: Pointer to hl_device structure.
347 * @cpu_boot_status_reg: status register of the device's CPU
348 * @boot_err0_reg: boot error register of the device's CPU
349 * @preboot_ver_timeout: how much to wait before bailing out on reading
350 * the preboot version
352 * Set DMA masks, initialize the PCI controller and map the PCI BARs.
354 * Return: 0 on success, non-zero for failure.
356 int hl_pci_init(struct hl_device *hdev, u32 cpu_boot_status_reg,
357 u32 boot_err0_reg, u32 preboot_ver_timeout)
359 struct pci_dev *pdev = hdev->pdev;
362 if (hdev->reset_pcilink)
363 hl_pci_reset_link_through_bridge(hdev);
365 rc = pci_enable_device_mem(pdev);
367 dev_err(hdev->dev, "can't enable PCI device\n");
371 pci_set_master(pdev);
373 rc = hdev->asic_funcs->pci_bars_map(hdev);
375 dev_err(hdev->dev, "Failed to initialize PCI BARs\n");
379 rc = hdev->asic_funcs->init_iatu(hdev);
381 dev_err(hdev->dev, "Failed to initialize iATU\n");
385 rc = hl_pci_set_dma_mask(hdev);
389 /* Before continuing in the initialization, we need to read the preboot
390 * version to determine whether we run with a security-enabled firmware
391 * The check will be done in each ASIC's specific code
393 rc = hl_fw_read_preboot_ver(hdev, cpu_boot_status_reg, boot_err0_reg,
394 preboot_ver_timeout);
401 hl_pci_bars_unmap(hdev);
403 pci_clear_master(pdev);
404 pci_disable_device(pdev);
410 * hl_fw_fini() - PCI finalization code.
411 * @hdev: Pointer to hl_device structure
413 * Unmap PCI bars and disable PCI device.
415 void hl_pci_fini(struct hl_device *hdev)
417 hl_pci_bars_unmap(hdev);
419 pci_clear_master(hdev->pdev);
420 pci_disable_device(hdev->pdev);